OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 function GetTemplateCallSite(siteObj, rawStrings) { | 7 var callSiteCache = new $Map; |
8 // TODO(caitp): ensure same template callsite is used for subsequent tag calls | 8 |
9 function SameCallSiteElements(rawStrings, other) { | |
10 var length = rawStrings.length; | |
11 var other = other.raw; | |
12 | |
13 if (length !== other.length) return false; | |
14 | |
15 for (var i = 0; i < length; ++i) { | |
16 if (rawStrings[i] !== other[i]) return false; | |
17 } | |
18 | |
19 return true; | |
20 } | |
21 | |
22 | |
23 function GetCachedCallSite(siteObj, hash) { | |
24 var obj = %MapGet(callSiteCache, hash); | |
25 | |
26 if (IS_UNDEFINED(obj)) return; | |
27 | |
28 var length = obj.length; | |
29 for (var i = 0; i < length; ++i) { | |
30 if (SameCallSiteElements(siteObj, obj[i])) return obj[i]; | |
31 } | |
32 } | |
33 | |
34 | |
35 function SetCachedCallSite(siteObj, hash) { | |
36 var obj = %MapGet(callSiteCache, hash); | |
37 | |
38 if (IS_UNDEFINED(obj)) { | |
39 %MapSet(callSiteCache, hash, new InternalArray(siteObj)); | |
arv (Not doing code reviews)
2014/11/19 21:38:57
Lets not use the strange overloading of params for
| |
40 } else { | |
41 // Unlikely hash collision case. | |
42 obj.push(siteObj); | |
43 } | |
44 | |
45 return siteObj; | |
46 } | |
47 | |
48 | |
49 function GetTemplateCallSite(siteObj, rawStrings, hash) { | |
50 var cached = GetCachedCallSite(rawStrings, hash); | |
51 | |
52 if (!IS_UNDEFINED(cached)) return cached; | |
9 | 53 |
10 %AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings), | 54 %AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings), |
11 READ_ONLY | DONT_ENUM | DONT_DELETE); | 55 READ_ONLY | DONT_ENUM | DONT_DELETE); |
12 | 56 |
13 return %ObjectFreeze(siteObj); | 57 return SetCachedCallSite(%ObjectFreeze(siteObj), hash); |
14 } | 58 } |
15 | 59 |
16 | 60 |
17 // ES6 Draft 10-14-2014, section 21.1.2.4 | 61 // ES6 Draft 10-14-2014, section 21.1.2.4 |
18 function StringRaw(callSite) { | 62 function StringRaw(callSite) { |
19 // TODO(caitp): Use rest parameters when implemented | 63 // TODO(caitp): Use rest parameters when implemented |
20 var numberOfSubstitutions = %_ArgumentsLength(); | 64 var numberOfSubstitutions = %_ArgumentsLength(); |
21 var cooked = ToObject(callSite); | 65 var cooked = ToObject(callSite); |
22 var raw = ToObject(cooked.raw); | 66 var raw = ToObject(cooked.raw); |
23 var literalSegments = ToLength(raw.length); | 67 var literalSegments = ToLength(raw.length); |
(...skipping 15 matching lines...) Expand all Loading... | |
39 function ExtendStringForTemplates() { | 83 function ExtendStringForTemplates() { |
40 %CheckIsBootstrapping(); | 84 %CheckIsBootstrapping(); |
41 | 85 |
42 // Set up the non-enumerable functions on the String object. | 86 // Set up the non-enumerable functions on the String object. |
43 InstallFunctions($String, DONT_ENUM, $Array( | 87 InstallFunctions($String, DONT_ENUM, $Array( |
44 "raw", StringRaw | 88 "raw", StringRaw |
45 )); | 89 )); |
46 } | 90 } |
47 | 91 |
48 ExtendStringForTemplates(); | 92 ExtendStringForTemplates(); |
OLD | NEW |