1 // Copyright 2014 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Called from a desugaring in the parser. 6 7 (function(global, utils) { 8 9 "use strict"; 10 11 %CheckIsBootstrapping(); 12 13 // ------------------------------------------------------------------- 14 // Imports 15 16 var GlobalMap = global.Map; 17 var InternalArray = utils.InternalArray; 18 19 // ------------------------------------------------------------------- 20 21 var callSiteCache = new GlobalMap; 22 var mapGetFn = GlobalMap.prototype.get; 23 var mapSetFn = GlobalMap.prototype.set; 24 25 26 function SameCallSiteElements(rawStrings, other) { 27 var length = rawStrings.length; 28 var other = other.raw; 29 30 if (length !== other.length) return false; 31 32 for (var i = 0; i < length; ++i) { 33 if (rawStrings[i] !== other[i]) return false; 34 } 35 36 return true; 37 } 38 39 40 function GetCachedCallSite(siteObj, hash) { 41 var obj = %_Call(mapGetFn, callSiteCache, hash); 42 43 if (IS_UNDEFINED(obj)) return; 44 45 var length = obj.length; 46 for (var i = 0; i < length; ++i) { 47 if (SameCallSiteElements(siteObj, obj[i])) return obj[i]; 48 } 49 } 50 51 52 function SetCachedCallSite(siteObj, hash) { 53 var obj = %_Call(mapGetFn, callSiteCache, hash); 54 var array; 55 56 if (IS_UNDEFINED(obj)) { 57 array = new InternalArray(1); 58 array[0] = siteObj; 59 %_Call(mapSetFn, callSiteCache, hash, array); 60 } else { 61 obj.push(siteObj); 62 } 63 64 return siteObj; 65 } 66 67 68 function GetTemplateCallSite(siteObj, rawStrings, hash) { 69 var cached = GetCachedCallSite(rawStrings, hash); 70 71 if (!IS_UNDEFINED(cached)) return cached; 72 73 %AddNamedProperty(siteObj, "raw", %object_freeze(rawStrings), 74 READ_ONLY | DONT_ENUM | DONT_DELETE); 75 76 return SetCachedCallSite(%object_freeze(siteObj), hash); 77 } 78 79 // ---------------------------------------------------------------------------- 80 // Exports 81 82 %InstallToContext(["get_template_call_site", GetTemplateCallSite]); 83 84 }) 85