This repository has been archived by the owner on Jan 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathSpec.html
318 lines (274 loc) · 15.1 KB
/
Spec.html
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!doctype html>
<meta charset="utf8">
<link rel="stylesheet" href="ecmarkup.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css">
<script src="ecmarkup.js"></script>
<pre class=metadata>
title: Object Rest/Spread Properties
stage: 3
contributors: Sebastian Markbåge
</pre>
<emu-intro id="intro">
<h1>Introduction</h1>
<p>Rest properties collect the remaining field names that are not already picked off by the destructuring pattern. Those keys and their values are copied onto a new object.</p>
<pre><code class="javascript">
1: let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
2: x; // 1
3: y; // 2
4: z; // { a: 3, b: 4 }
</code></pre>
<p>Spread properties in object initializers copies field names and their values from a provided object onto the newly created object.</p>
<pre><code class="javascript">
1: let n = { x, y, ...z };
2: n; // { x: 1, y: 2, a: 3, b: 4 }
</code></pre>
</emu-intro>
<emu-clause id="Spread">
<h1>Spread Properties</h1>
<h2>Syntax</h2>
<emu-grammar>
PropertyDefinition[Yield, Await] :
IdentifierReference[?Yield, ?Await]
CoverInitializedName[?Yield, ?Await]
PropertyName[?Yield, ?Await]
<ins class="block">`...` AssignmentExpression[In, ?Yield, ?Await]</ins>
</emu-grammar>
<emu-clause id="Spread-RuntimeSemantics-PropertyDefinitionEvaluation">
<h1>Runtime Semantics: PropertyDefinitionEvaluation</h1>
<p>With parameters _object_ and _enumerable_.</p>
<ins class="block">
<emu-grammar>PropertyDefinition : `...` AssignmentExpression</emu-grammar>
<emu-alg>
1. Let _exprValue_ be the result of evaluating _AssignmentExpression_.
1. Let _fromValue_ be GetValue(_exprValue_).
1. ReturnIfAbrupt(_fromValue_).
1. Let _excludedNames_ be a new empty List.
1. Return ? CopyDataProperties(_object_, _fromValue_, _excludedNames_).
</emu-alg>
</ins>
</emu-clause>
</emu-clause>
<emu-clause id="Rest">
<h1>Rest Properties</h1>
<h2>Syntax</h2>
<emu-grammar>
ObjectAssignmentPattern[Yield, Await] :
`{` <ins>AssignmentRestProperty[?Yield, ?Await]?</ins> `}`
`{` AssignmentPropertyList[?Yield, ?Await] `}`
`{` AssignmentPropertyList[?Yield, ?Await] `,` <ins>AssignmentRestProperty[?Yield, ?Await]?</ins> `}`
<ins class="block">
AssignmentRestProperty[Yield, Await] :
`...` DestructuringAssignmentTarget[?Yield, ?Await]
</ins>
ObjectBindingPattern[Yield, Await] :
`{` <ins>BindingRestProperty[?Yield, ?Await]?</ins> `}`
`{` BindingPropertyList[?Yield, ?Await] `}`
`{` BindingPropertyList[?Yield, ?Await] `,` <ins>BindingRestProperty[?Yield, ?Await]?</ins> `}`
<ins class="block">
BindingRestProperty[Yield, Await] :
`...` BindingIdentifier[?Yield, ?Await]
</ins>
</emu-grammar>
<emu-clause id="Rest-RuntimeSemantics-DestructuringAssignmentEvaluation">
<h1>Runtime Semantics: DestructuringAssignmentEvaluation</h1>
<p>With parameter _value_.</p>
<emu-grammar>
ObjectAssignmentPattern :
`{` AssignmentPropertyList `}`
`{` AssignmentPropertyList `,` `}`
</emu-grammar>
<emu-alg>
1. Perform ? RequireObjectCoercible(_value_).
1. Perform <ins>? Property</ins>DestructuringAssignmentEvaluation for |AssignmentPropertyList| using _value_ as the argument.
1. <ins>Return NormalCompletion(~empty~).</ins>
</emu-alg>
<ins class="block">
<emu-grammar>ObjectAssignmentPattern : `{` AssignmentRestProperty `}`</emu-grammar>
<emu-alg>
1. Let _excludedNames_ be a new empty List.
1. Return the result of performing RestDestructuringAssignmentEvaluation of _AssignmentRestProperty_ with _value_ and _excludedNames_ as the arguments.
</emu-alg>
<emu-grammar>ObjectAssignmentPattern : `{` AssignmentPropertyList `,` AssignmentRestProperty `}`</emu-grammar>
<emu-alg>
1. Let _excludedNames_ be the result of performing ? PropertyDestructuringAssignmentEvaluation for _AssignmentPropertyList_ using _value_ as the argument.
1. Return the result of performing RestDestructuringAssignmentEvaluation of _AssignmentRestProperty_ with _value_ and _excludedNames_ as the arguments.
</emu-alg>
</ins>
<emu-note>The following productions have moved to PropertyDestructuringAssignmentEvaluation to keep the return type of DestructuringAssignmentEvaluation consistent.</emu-note>
<del class="block">
<emu-grammar>AssignmentPropertyList : AssignmentPropertyList `,` AssignmentProperty</emu-grammar>
<emu-grammar>AssignmentProperty : IdentifierReference Initializer?</emu-grammar>
<emu-grammar>AssignmentProperty : PropertyName `:` AssignmentElement</emu-grammar>
</del>
</emu-clause>
<ins class="block">
<emu-clause id="nested-rest-prohibition">
<h1>Static Semantics: Early Errors</h1>
<emu-grammar>
AssignmentRestProperty[Yield, Await] :
`...` DestructuringAssignmentTarget[Yield, Await]
</emu-grammar>
<ul><li>It is a Syntax Error if |DestructuringAssignmentTarget| is an |ArrayLiteral| or an |ObjectLiteral|.</li></ul>
</emu-clause>
<emu-clause id="Rest-RuntimeSemantics-PropertyDestructuringAssignmentEvaluation">
<h1>Runtime Semantics: PropertyDestructuringAssignmentEvaluation</h1>
<p>With parameters _value_.</p>
<emu-note>These collect a list of all destructured property names rather than just empty completion.</emu-note>
<emu-grammar>AssignmentPropertyList : AssignmentPropertyList `,` AssignmentProperty</emu-grammar>
<emu-alg>
1. Let <del>_status_</del> <ins>_propertyNames_</ins> be the result of performing <ins>Property</ins>DestructuringAssignmentEvaluation for |AssignmentPropertyList| using _value_ as the argument.
1. ReturnIfAbrupt(<del>_status_</del> <ins>_propertyNames_</ins>).
1. <del>Return</del> <ins>Let _nextNames_ be</ins> the result of performing <ins>Property</ins>DestructuringAssignmentEvaluation for |AssignmentProperty| using _value_ as the argument.
1. <ins>ReturnIfAbrupt(_nextNames_).</ins>
1. <ins>Append each item in _nextNames_ to the end of _propertyNames_.</ins>
1. <ins>Return _propertyNames_.</ins>
</emu-alg>
<emu-grammar>AssignmentProperty : IdentifierReference Initializer?</emu-grammar>
<emu-alg>
1. Let _P_ be StringValue of |IdentifierReference|.
1. Let _lref_ be ? ResolveBinding(_P_).
1. Let _v_ be ? GetV(_value_, _P_).
1. If |Initializer_opt| is present and _v_ is *undefined*, then
1. Let _defaultValue_ be the result of evaluating |Initializer|.
1. Let _v_ be ? GetValue(_defaultValue_).
1. If IsAnonymousFunctionDefinition(|Initializer|) is *true*, then
1. Let _hasNameProperty_ be ? HasOwnProperty(_v_, `"name"`).
1. If _hasNameProperty_ is *false*, perform SetFunctionName(_v_, _P_).
1. <del>Return</del> <ins>Perform</ins> ? PutValue(_lref_, _v_).
1. <ins>Return a new List containing _P_.</ins>
</emu-alg>
<emu-grammar>AssignmentProperty : PropertyName `:` AssignmentElement</emu-grammar>
<emu-alg>
1. Let _name_ be the result of evaluating |PropertyName|.
1. ReturnIfAbrupt(_name_).
1. <del>Return</del> <ins>Let _status_ be</ins> the result of performing KeyedDestructuringAssignmentEvaluation of |AssignmentElement| with _value_ and _name_ as the arguments.
1. <ins>ReturnIfAbrupt(_status_).</ins>
1. <ins>Return a new List containing _name_.</ins>
</emu-alg>
</emu-clause>
</ins>
<ins class="block">
<emu-clause id="Rest-RuntimeSemantics-RestDestructuringAssignmentEvaluation">
<h1>Runtime Semantics: RestDestructuringAssignmentEvaluation</h1>
<p>With parameters _value_ and _excludedNames_.</p>
<emu-grammar>AssignmentRestProperty[Yield, Await] : `...` DestructuringAssignmentTarget</emu-grammar>
<emu-alg>
1. Let _lref_ be the result of evaluating |DestructuringAssignmentTarget|.
1. ReturnIfAbrupt(_lref_).
1. Let _restObj_ be ObjectCreate(%ObjectPrototype%).
1. Let _assignStatus_ be CopyDataProperties(_restObj_, _value_, _excludedNames_).
1. ReturnIfAbrupt(_assignStatus_).
1. Return PutValue(_lref_, _restObj_).
</emu-alg>
</emu-clause>
</ins>
<emu-clause id="Rest-RuntimeSemantics-BindingInitialization">
<h1>Runtime Semantics: BindingInitialization</h1>
<p>With parameters _value_ and _environment_.</p>
<ins class="block">
<emu-grammar>
ObjectBindingPattern :
`{` BindingPropertyList `}`
`{` BindingPropertyList `,` `}`
</emu-grammar>
<emu-alg>
1. Let _excludedNames_ be the result of performing PropertyBindingInitialization for |BindingPropertyList| using _value_ and _environment_ as the argument.
1. ReturnIfAbrupt(_excludedNames_).
1. Return NormalCompletion(~empty~).
</emu-alg>
<emu-grammar>ObjectBindingPattern : `{` BindingRestProperty `}`</emu-grammar>
<emu-alg>
1. Let _excludedNames_ be a new empty List.
1. Return the result of performing RestBindingInitialization of _BindingRestProperty_ with _value_, _environment_ and _excludedNames_ as the arguments.
</emu-alg>
<emu-grammar>ObjectBindingPattern : `{` BindingPropertyList `,` BindingRestProperty `}`</emu-grammar>
<emu-alg>
1. Let _excludedNames_ be the result of performing PropertyBindingInitialization of |BindingPropertyList| using _value_ and _environment_ as arguments.
1. ReturnIfAbrupt(_excludedNames_).
1. Return the result of performing RestBindingInitialization of _BindingRestProperty_ with _value_, _environment_ and _excludedNames_ as the arguments.
</emu-alg>
</ins>
<emu-note>The following productions have moved to PropertyBindingInitialization to keep the return type of BindingInitialization consistent.</emu-note>
<del class="block">
<emu-grammar>BindingPropertyList : BindingPropertyList `,` BindingProperty</emu-grammar>
<emu-grammar>BindingProperty : SingleNameBinding</emu-grammar>
<emu-grammar>BindingProperty : PropertyName `:` BindingElement</emu-grammar>
</del>
</emu-clause>
<ins class="block">
<emu-clause id="Rest-RuntimeSemantics-PropertyBindingInitialization">
<h1>Runtime Semantics: PropertyBindingInitialization</h1>
<p>With parameters _value_ and _environment_.</p>
<emu-note>These collect a list of all bound property names rather than just empty completion.</emu-note>
<emu-grammar>BindingPropertyList : BindingPropertyList `,` BindingProperty</emu-grammar>
<emu-alg>
1. Let <del>_status_</del> <ins>_boundNames_</ins> be the result of performing <ins>Property</ins>BindingInitialization for |BindingPropertyList| using _value_ and _environment_ as arguments.
1. ReturnIfAbrupt(<del>_status_</del> <ins>_boundNames_</ins>).
1. <del>Return</del> <ins>Let _nextNames_ be</ins> the result of performing <ins>Property</ins>BindingInitialization for |BindingProperty| using _value_ and _environment_ as arguments.
1. <ins>ReturnIfAbrupt(_nextNames_).</ins>
1. <ins>Append each item in _nextNames_ to the end of _boundNames_.</ins>
1. <ins>Return _boundNames_.</ins>
</emu-alg>
<emu-grammar>BindingProperty : SingleNameBinding</emu-grammar>
<emu-alg>
1. Let _name_ be the string that is the only element of BoundNames of |SingleNameBinding|.
1. <del>Return</del> <ins>Let _status_ be</ins> the result of performing KeyedBindingInitialization for |SingleNameBinding| using _value_, _environment_, and _name_ as the arguments.
1. <ins>ReturnIfAbrupt(_status_).</ins>
1. <ins>Return a new List containing _name_.</ins>
</emu-alg>
<emu-grammar>BindingProperty : PropertyName `:` BindingElement</emu-grammar>
<emu-alg>
1. Let _P_ be the result of evaluating |PropertyName|.
1. ReturnIfAbrupt(_P_).
1. <del>Return</del> <ins>Let _status_ be</ins> the result of performing KeyedBindingInitialization of |BindingElement| with _value_, _environment_, and _P_ as the arguments.
1. <ins>ReturnIfAbrupt(_status_).</ins>
1. <ins>Return a new List containing _P_.</ins>
</emu-alg>
</emu-clause>
</ins>
<ins class="block">
<emu-clause id="Rest-RuntimeSemantics-RestBindingInitialization">
<h1>Runtime Semantics: RestBindingInitialization</h1>
<p>With parameters _value_, _environment_ and _excludedNames_.</p>
<emu-grammar>BindingRestProperty : `...` BindingIdentifier</emu-grammar>
<emu-alg>
1. Let _restObj_ be ObjectCreate(%ObjectPrototype%).
1. Let _assignStatus_ be CopyDataProperties(_restObj_, _value_, _excludedNames_).
1. ReturnIfAbrupt(_assignStatus_).
1. Let _bindingId_ be StringValue of |BindingIdentifier|.
1. Let _lhs_ be ResolveBinding(_bindingId_, _environment_).
1. ReturnIfAbrupt(_lhs_).
1. If _environment_ is *undefined*, return PutValue(_lhs_, _restObj_).
1. Return InitializeReferencedBinding(_lhs_, _restObj_).
</emu-alg>
</emu-clause>
</ins>
</emu-clause>
<emu-clause id="AbstractOperations">
<h1>Abstract Operations</h1>
<emu-clause id="AbstractOperations-CopyDataProperties" aoid="OrdinaryToPrimitive">
<h1>CopyDataProperties (_target_, _source_, _excluded_)</h1>
<emu-note>These spec proposals refer to the abstract operation CopyDataProperties(_target_, _source_, _excluded_) which follow similar semantics as the ES2015 Object.assign(_target_, ..._sources_) with the addition of the _excluded_ argument which is a list of property names to be excluded. It also uses the CreateDataProperty operation instead of Set.</emu-note>
<emu-note>The target passed is in here is always a newly created object which doesn't leak in case of an error being thrown. It's not observable when the actual object construction and initialization happens nor whether it is interleaved with reading the properties off the original object.</emu-note>
<p>When the abstract operation CopyDataProperties is called with arguments _target_, _source_ and _excluded_, the following steps are taken:</p>
<emu-alg>
1. Assert: Type(_target_) is Object.
1. Assert: Type(_excluded_) is List.
1. If _source_ is *undefined* or *null*, let _keys_ be a new empty List.
1. Else,
1. Let _from_ be ! ToObject(_source_).
1. Let _keys_ be ? _from_.[[OwnPropertyKeys]]().
1. Repeat for each element _nextKey_ of _keys_ in List order,
1. Let _found_ be *false*.
1. Repeat for each element _e_ of _excluded_,
1. If _e_ is not empty and SameValue(_e_, _nextKey_) is true, then
1. Set _found_ to *true*.
1. If _found_ is *false*, then
1. Let _desc_ be ? _from_.[[GetOwnProperty]](_nextKey_).
1. If _desc_ is not *undefined* and _desc_.[[Enumerable]] is *true*, then
1. Let _propValue_ be ? Get(_from_, _nextKey_).
1. Perform ! CreateDataProperty(_target_, _nextKey_, _propValue_).
1. Return _target_.
</emu-alg>
</emu-clause>
</emu-clause>