-
Notifications
You must be signed in to change notification settings - Fork 7
/
spec.emu
82 lines (68 loc) · 3.82 KB
/
spec.emu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!doctype html>
<meta charset="utf8">
<link rel="stylesheet" href="./spec.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css">
<script src="./spec.js"></script>
<pre class="metadata">
title: Array.prototype.isTemplateObject
stage: 2
copyright: false
contributors: Mike Samuel, Krzysztof Kotowicz
</pre>
<h2>Changes to ArrayCreate</h2>
<p>To enable the new API, this proposal modifies ArrayCreate to add an internal slot to each array.</p>
<emu-clause id="sec-arraycreate" aoid="ArrayCreate">
<h1>ArrayCreate ( _length_ [ , _proto_ ] )</h1>
<p>The abstract operation ArrayCreate takes argument _length_ (a non-negative integer) and optional argument _proto_. It is used to specify the creation of new Array exotic objects. It performs the following steps when called:</p>
<emu-alg>
1. If _length_ > 2<sup>32</sup> - 1, throw a *RangeError* exception.
1. If _proto_ is not present, set _proto_ to %Array.prototype%.
1. Let _A_ be ! MakeBasicObject(« [[Prototype]], [[Extensible]]<ins>, [[TemplateObject]]</ins> »).
1. Set _A_.[[Prototype]] to _proto_.
1. <ins>Set _A_.[[TemplateObject]] to *false*.</ins>
1. Set _A_.[[DefineOwnProperty]] as specified in <emu-xref href="#sec-array-exotic-objects-defineownproperty-p-desc"></emu-xref>.
1. Perform ! OrdinaryDefineOwnProperty(_A_, *"length"*, PropertyDescriptor { [[Value]]: 𝔽(_length_), [[Writable]]: *true*, [[Enumerable]]: *false*, [[Configurable]]: *false* }).
1. Return _A_.
</emu-alg>
</emu-clause>
<h2>Changes to GetTemplateObject</h2>
<p>To enable the new API, this proposal modifies GetTemplateObject to change the value of the new internal slot.</p>
<emu-clause id="sec-gettemplateobject" aoid="GetTemplateObject">
<h1>Runtime Semantics: GetTemplateObject ( _templateLiteral_ )</h1>
<p>The abstract operation GetTemplateObject is called with a Parse Node, _templateLiteral_, as an argument. It performs the following steps:</p>
<emu-alg>
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_.[[Site]] is the same Parse Node as _templateLiteral_, then
1. Return _e_.[[Array]].
1. Let _rawStrings_ be TemplateStrings of _templateLiteral_ with argument *true*.
1. Let _cookedStrings_ be TemplateStrings of _templateLiteral_ with argument *false*.
1. Let _count_ be the number of elements in the List _cookedStrings_.
1. Assert: _count_ ≤ 2<sup>32</sup> - 1.
1. Let _template_ be ! ArrayCreate(_count_).
1. <ins>Set _template_.[[TemplateObject]] to *true*.</ins>
1. Let _rawObj_ be ! ArrayCreate(_count_).
1. Let _index_ be 0.
1. ...
</emu-clause>
<h2>New APIs</h2>
<p>This adds a builtin Array prototype method to allow distinguishing template string literal objects.</p>
<emu-clause id="sec-Array.prototype.isTemplateObject">
<h1><ins>Array.prototype.isTemplateObject()</ins></h1>
<p><ins>When the `isTemplateObject` method is called, the following steps are taken:</ins></p>
<emu-alg>
1. <ins>Let _O_ be ! ToObject(*this* value).</ins>
1. <ins>If IsArray(_O_) is *true* and _O_.[[TemplateObject]] is *true* then</ins>
1. <ins>Return *true*.</ins>
1. <ins>Return *false*.</ins>
</emu-alg>
<emu-note>
<p><ins>IsTemplateObject is realm-agnostic. Since template objects are frozen before
escaping GetTemplateObject, testing (IsTemplateObject(_x_) and _x_.[[Prototype]]
is the _realm_'s %ArrayPrototype%) is sufficient to determine whether an _x_ is
a template object in a particular _realm_.</ins></p>
<p><ins>In user code, `maybeTemplate.isTemplateObject() && maybeTemplate instanceof Array` is an equivalent
test, assuming no changes to builtins.</ins></p>
</emu-note>
</emu-clause>