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 = callSiteCache.get(hash); | |
arv (Not doing code reviews)
2014/11/19 06:09:11
This needs to be be %MapGet(callSiteCache, hash) o
caitp (gmail)
2014/11/19 07:15:05
Done.
| |
25 | |
26 if (IS_UNDEFINED(obj)) return; | |
27 | |
28 if ("raw" in obj) { | |
caitp (gmail)
2014/11/19 03:50:33
I know this is broken if Array.prototype is messed
arv (Not doing code reviews)
2014/11/19 06:09:11
Why is this needed. Can we not make sure we only s
caitp (gmail)
2014/11/19 07:15:05
In an unlikely event where we have hash collisions
| |
29 // Common case | |
30 if (SameCallSiteElements(siteObj, obj)) return obj; | |
31 } else if (IS_ARRAY(obj)) { | |
32 // Rare case | |
33 for (var i = 0; i < obj.length; ++i) { | |
34 if (SameCallSiteElements(siteObj, obj[i])) return obj[i]; | |
35 } | |
36 } else { | |
37 throw "UNREACHABLE"; | |
38 } | |
39 } | |
40 | |
41 | |
42 function SetCachedCallSite(siteObj, hash) { | |
43 var obj = callSiteCache.get(hash); | |
44 | |
45 if (IS_UNDEFINED(obj)) { | |
46 callSiteCache.set(hash, siteObj); | |
arv (Not doing code reviews)
2014/11/19 06:09:11
%MapSet
| |
47 } else if ("raw" in obj) { | |
caitp (gmail)
2014/11/19 03:50:33
Same here... what about using a Set of an array?
arv (Not doing code reviews)
2014/11/19 06:09:11
I'm not sure why this is needed? Don't we only get
| |
48 callSiteCache.set(hash, [obj, siteObj]); | |
49 } else if (IS_ARRAY(obj)) { | |
arv (Not doing code reviews)
2014/11/19 06:09:11
How can obj be anything but an array?
caitp (gmail)
2014/11/19 07:15:05
Explained above --- it can be a callSiteObj, or an
| |
50 callSiteCache.set(hash, obj.concat(siteObj)); | |
51 } else { | |
52 throw "UNREACHABLE"; | |
53 } | |
54 | |
55 return siteObj; | |
56 } | |
57 | |
58 | |
59 function GetTemplateCallSite(siteObj, rawStrings, hash) { | |
60 var cached = GetCachedCallSite(rawStrings, hash); | |
61 | |
62 if (!IS_UNDEFINED(cached)) return cached; | |
9 | 63 |
10 %AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings), | 64 %AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings), |
11 READ_ONLY | DONT_ENUM | DONT_DELETE); | 65 READ_ONLY | DONT_ENUM | DONT_DELETE); |
12 | 66 |
13 return %ObjectFreeze(siteObj); | 67 return SetCachedCallSite(%ObjectFreeze(siteObj), hash); |
14 } | 68 } |
OLD | NEW |