Skip to content

Commit

Permalink
Normative: Cache templates per site, rather than by contents
Browse files Browse the repository at this point in the history
The previous definition of template caching had a few issue:
 - (from @syg) Template strings may live forever due to putting them
   in a WeakMap
 - (from @ajklein) Because of this logic, it's rather difficult to
   implement any GC at all of template objects
 - (from @erights) The template string facility cannot be extended
   to expose anything about the site, as it's site-independent

This patch makes template caching key off the Parse Node where the
template occurs in source, rather than the List of Strings that the
template evaluates into.

These semantics seem to match SpiderMonkey's implementation of templates.
V8, ChakraCore and JSC, on the other hand, implement the prior semantics.

Resolves tc39#840
  • Loading branch information
littledan committed Feb 3, 2018
1 parent f812bc5 commit 8b5698a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -6121,10 +6121,11 @@ <h1>Realms</h1>
[[TemplateMap]]
</td>
<td>
A List of Record { [[Strings]]: List, [[Array]]: Object}.
A List of Record { [[Site]]: Parse Node, [[Array]]: Object}.
</td>
<td>
Template objects are canonicalized separately for each realm using its Realm Record's [[TemplateMap]]. Each [[Strings]] value is a List containing, in source text order, the raw String values of a |TemplateLiteral| that has been evaluated. The associated [[Array]] value is the corresponding template object that is passed to a tag function.
Template objects are canonicalized separately for each realm using its Realm Record's [[TemplateMap]]. Each [[Site]] value is a Parse Node containing a |TemplateLiteral|. The associated [[Array]] value is the corresponding template object that is passed to a tag function.
<emu-note>Once a Parse Node becomes unreachable, the corresponding [[Array]] is also unreachable, and it would be unobservable if an implementation removed the pair from the [[TemplateMap]] list.</emu-note>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -12079,7 +12080,7 @@ <h1>Runtime Semantics: GetTemplateObject ( _templateLiteral_ )</h1>
1. Let _realm_ be the current Realm Record.
1. Let _templateRegistry_ be _realm_.[[TemplateMap]].
1. For each element _e_ of _templateRegistry_, do
1. If _e_.[[Strings]] and _rawStrings_ contain the same values in the same order, then
1. If _e_.[[Site]] is the same Parse Node as _templateLiteral_, then
1. Return _e_.[[Array]].
1. Let _cookedStrings_ be TemplateStrings of _templateLiteral_ with argument *false*.
1. Let _count_ be the number of elements in the List _cookedStrings_.
Expand All @@ -12097,7 +12098,7 @@ <h1>Runtime Semantics: GetTemplateObject ( _templateLiteral_ )</h1>
1. Perform SetIntegrityLevel(_rawObj_, `"frozen"`).
1. Call _template_.[[DefineOwnProperty]](`"raw"`, PropertyDescriptor{[[Value]]: _rawObj_, [[Writable]]: *false*, [[Enumerable]]: *false*, [[Configurable]]: *false*}).
1. Perform SetIntegrityLevel(_template_, `"frozen"`).
1. Append the Record{[[Strings]]: _rawStrings_, [[Array]]: _template_} to _templateRegistry_.
1. Append the Record{[[Site]]: _templateLiteral_, [[Array]]: _template_} to _templateRegistry_.
1. Return _template_.
</emu-alg>
<emu-note>
Expand Down

0 comments on commit 8b5698a

Please sign in to comment.