-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspec.emu
250 lines (232 loc) · 11.1 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!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: Symbol as WeakMap Keys Proposal
stage: 2
contributors:
- Robin Ricard
- Rick Button
- Daniel Ehrenberg
- Leo Balter
- Caridy Patiño
- Rick Waldron
- Ashley Claymore
</pre>
<emu-clause id="sec-executable-code-and-execution-contexts">
<h1>Executable Code and Execution Contexts</h1>
<emu-clause id="sec-weakref-processing-model">
<h1>Processing Model of WeakRef and FinalizationRegistry Objects</h1>
<emu-note>
Full set of proposed changes here: <a href="https://arai-a.github.io/ecma262-compare/?pr=2777">https://arai-a.github.io/ecma262-compare/?pr=2777</a>
</emu-note>
</emu-clause>
<emu-clause id="sec-addtokeptobjects" type="abstract operation">
<h1>
AddToKeptObjects (
<del>_object_: an Object,</del>
<ins>_value_: an Object or a Symbol,</ins>
): ~unused~
</h1>
<dl class="header">
</dl>
<emu-alg>
1. Let _agentRecord_ be the surrounding agent's Agent Record.
1. <del>Append _object_ to _agentRecord_.[[KeptAlive]].</del>
1. <ins>Append _value_ to _agentRecord_.[[KeptAlive]].</ins>
1. Return ~unused~.
</emu-alg>
<emu-note>
When the abstract operation AddToKeptObjects is called with a target object <ins>, or symbol,</ins> reference, it adds the target to a list that will point strongly at the target until ClearKeptObjects is called.
</emu-note>
</emu-clause>
</emu-clause>
<emu-clause id="sec-canbeheldweakly-abstract-operation" type="abstract operation">
<h1>
CanBeHeldWeakly (
_v_: unknown
): a Boolean
</h1>
<dl class="header">
</dl>
<emu-alg>
1. If Type(_v_) is Object, return *true*.
1. If Type(_v_) is Symbol, then
1. For each element _e_ of the GlobalSymbolRegistry List (see <emu-xref href="#sec-symbol.for"></emu-xref>), do
1. If SameValue(_e_.[[Symbol]], _v_) is *true*, return *false*.
1. Return *true*.
1. Return *false*.
</emu-alg>
<emu-note type="editor">
<p>This abstract operation determines if _v_ is considered to have an identity suitable for a weak reference. i.e. Is _v_ a valid candidate key of a WeakMap, or entry in a WeakSet, or target of a WeakRef or FinalizationRegistry.</p>
<p>'Registered symbols' are not considered to have a suitable identity because their identity is similar to strings. Implementations may attempt to collect them when they are unreachable.</p>
<p>'Well-Known symbols' <emu-xref href="#sec-well-known-symbols"></emu-xref> return *true* - this should not be interpreted as evidence that using them as a key in a WeakMap will not result in a memory leak.</p>
</emu-note>
</emu-clause>
<emu-clause id="sec-modifications-of-weakmap">
<h1>Modifications to the properties of WeakMap.prototype</h1>
<emu-clause id="sec-weakmap.prototype.delete">
<h1>WeakMap.prototype.delete ( _key_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[WeakMapData]]).
1. Let _entries_ be the List that is _M_.[[WeakMapData]].
1. <del>If Type(_key_) is not Object, return *false*.</del>
1. <ins>If CanBeHeldWeakly(_key_) is *false*, return *false*.</ins>
1. For each Record { [[Key]], [[Value]] } _p_ of _entries_, do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, then
1. Set _p_.[[Key]] to ~empty~.
1. Set _p_.[[Value]] to ~empty~.
1. Return *true*.
1. Return *false*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-weakmap.prototype.get">
<h1>WeakMap.prototype.get ( _key_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[WeakMapData]]).
1. Let _entries_ be the List that is _M_.[[WeakMapData]].
1. <del>If Type(_key_) is not Object, return *undefined*.</del>
1. <ins>If CanBeHeldWeakly(_key_) is *false*, return *undefined*.</ins>
1. For each Record { [[Key]], [[Value]] } _p_ of _entries_, do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, return _p_.[[Value]].
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-weakmap.prototype.has">
<h1>WeakMap.prototype.has ( _key_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[WeakMapData]]).
1. Let _entries_ be the List that is _M_.[[WeakMapData]].
1. <del>If Type(_key_) is not Object, return *false*.</del>
1. <ins>If CanBeHeldWeakly(_key_) is *false*, return *false*.</ins>
1. For each Record { [[Key]], [[Value]] } _p_ of _entries_, do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, return *true*.
1. Return *false*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-weakmap.prototype.set">
<h1>WeakMap.prototype.set ( _key_, _value_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[WeakMapData]]).
1. Let _entries_ be the List that is _M_.[[WeakMapData]].
1. <del>If Type(_key_) is not Object, throw a *TypeError* exception.</del>
1. <ins>If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.</ins>
1. For each Record { [[Key]], [[Value]] } _p_ of _entries_, do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, then
1. Set _p_.[[Value]] to _value_.
1. Return _M_.
1. Let _p_ be the Record { [[Key]]: _key_, [[Value]]: _value_ }.
1. Append _p_ as the last element of _entries_.
1. Return _M_.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-modifications-of-weakset">
<h1>Modifications to the properties of WeakSet.prototype</h1>
<emu-clause id="sec-weakset.prototype.add">
<h1>WeakSet.prototype.add ( _value_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _S_ be the *this* value.
1. Perform ? RequireInternalSlot(_S_, [[WeakSetData]]).
1. <del>If Type(_value_) is not Object, throw a *TypeError* exception.</del>
1. <ins>If CanBeHeldWeakly(_value_) is *false*, throw a *TypeError* exception.</ins>
1. Let _entries_ be the List that is _S_.[[WeakSetData]].
1. For each element _e_ of _entries_, do
1. If _e_ is not ~empty~ and SameValue(_e_, _value_) is *true*, then
1. Return _S_.
1. Append _value_ as the last element of _entries_.
1. Return _S_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-weakset.prototype.delete">
<h1>WeakSet.prototype.delete ( _value_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _S_ be the *this* value.
1. Perform ? RequireInternalSlot(_S_, [[WeakSetData]]).
1. <del>If Type(_value_) is not Object, return *false*.</del>
1. <ins>If CanBeHeldWeakly(_value_) is *false*, return *false*.</ins>
1. Let _entries_ be the List that is _S_.[[WeakSetData]].
1. For each element _e_ of _entries_, do
1. If _e_ is not ~empty~ and SameValue(_e_, _value_) is *true*, then
1. Replace the element of _entries_ whose value is _e_ with an element whose value is ~empty~.
1. Return *true*.
1. Return *false*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-weakset.prototype.has">
<h1>WeakSet.prototype.has ( _value_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _S_ be the *this* value.
1. Perform ? RequireInternalSlot(_S_, [[WeakSetData]]).
1. Let _entries_ be the List that is _S_.[[WeakSetData]].
1. <del>If Type(_value_) is not Object, return *false*.</del>
1. <ins>If CanBeHeldWeakly(_value_) is *false*, return *false*.</ins>
1. For each element _e_ of _entries_, do
1. If _e_ is not ~empty~ and SameValue(_e_, _value_) is *true*, return *true*.
1. Return *false*.
</emu-alg>
</emu-clause>
</emu-clause>
<emu-clause id="sec-modifications-of-weakref-finalization">
<h1>Modifications to WeakRef and FinalizationRegistry</h1>
<emu-clause id="sec-weak-ref-target">
<h1>WeakRef ( _target_ )</h1>
<p>When the `WeakRef` function is called with argument _target_, the following steps are taken:</p>
<emu-alg>
1. If NewTarget is *undefined*, throw a *TypeError* exception.
1. <del>If Type(_target_) is not Object, throw a *TypeError* exception.</del>
1. <ins>If CanBeHeldWeakly(_target_) is *false*, throw a *TypeError* exception.</ins>
1. Let _weakRef_ be ? OrdinaryCreateFromConstructor(NewTarget, *"%WeakRef.prototype%"*, « [[WeakRefTarget]] »).
1. Perform ! AddToKeptObjects(_target_).
1. Set _weakRef_.[[WeakRefTarget]] to _target_.
1. Return _weakRef_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-finalization-registry.prototype.register">
<h1>FinalizationRegistry.prototype.register ( _target_, _heldValue_ [ , _unregisterToken_ ] )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _finalizationRegistry_ be the *this* value.
1. Perform ? RequireInternalSlot(_finalizationRegistry_, [[Cells]]).
1. <del>If Type(_target_) is not Object, throw a *TypeError* exception.</del>
1. <ins>If CanBeHeldWeakly(_target_) is *false*, throw a *TypeError* exception.</ins>
1. If SameValue(_target_, _heldValue_) is *true*, throw a *TypeError* exception.
1. <del>If Type(_unregisterToken_) is not Object, then</del>
1. <ins>If CanBeHeldWeakly(_unregisterToken_) is *false*, then</ins>
1. If _unregisterToken_ is not *undefined*, throw a *TypeError* exception.
1. Set _unregisterToken_ to ~empty~.
1. Let _cell_ be the Record { [[WeakRefTarget]]: _target_, [[HeldValue]]: _heldValue_, [[UnregisterToken]]: _unregisterToken_ }.
1. Append _cell_ to _finalizationRegistry_.[[Cells]].
1. Return *undefined*.
</emu-alg>
</emu-clause>
<emu-clause id="sec-finalization-registry.prototype.unregister">
<h1>FinalizationRegistry.prototype.unregister ( _unregisterToken_ )</h1>
<p>The following steps are taken:</p>
<emu-alg>
1. Let _finalizationRegistry_ be the *this* value.
1. Perform ? RequireInternalSlot(_finalizationRegistry_, [[Cells]]).
1. <del>If Type(_unregisterToken_) is not Object, throw a *TypeError* exception.</del>
1. <ins>If CanBeHeldWeakly(_unregisterToken_) is *false*, throw a *TypeError* exception.</ins>
1. Let _removed_ be *false*.
1. For each Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } _cell_ of _finalizationRegistry_.[[Cells]], do
1. If _cell_.[[UnregisterToken]] is not ~empty~ and SameValue(_cell_.[[UnregisterToken]], _unregisterToken_) is *true*, then
1. Remove _cell_ from _finalizationRegistry_.[[Cells]].
1. Set _removed_ to *true*.
1. Return _removed_.
</emu-alg>
</emu-clause>
</emu-clause>