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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use crate::{
ecmascript::{
Agent, Environment, JsResult, Object, PropertyKey, PropertyKeySet, Value, array_create,
copy_data_properties_into_object, get, initialize_referenced_binding, put_value,
resolve_binding, to_object, try_create_data_property_or_throw, unwrap_try,
},
engine::{
ActiveIterator, Bindable, Executable, GcScope, Instruction, Scopable, ScopableCollection,
Scoped, Vm, VmIteratorRecord,
},
};
use super::with_vm_gc;
pub(super) fn execute_simple_array_binding<'a>(
agent: &mut Agent,
vm: &mut Vm,
executable: Scoped<Executable>,
environment: Option<Scoped<Environment>>,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, ()> {
let mut iterator_is_done = false;
loop {
let instr = executable.get_instruction(agent, &mut vm.ip).unwrap();
if agent.options.print_internals {
eprintln!("Executing: {:?}", instr.kind);
}
let mut break_after_bind = false;
let value = match instr.kind {
Instruction::Debug => {
if agent.options.print_internals {
eprintln!("Debug: {vm:#?}");
}
continue;
}
Instruction::BindingPatternBind
| Instruction::BindingPatternBindToIndex
| Instruction::BindingPatternGetValue
| Instruction::BindingPatternSkip => {
let result = with_vm_gc(
agent,
vm,
|agent, gc| ActiveIterator::new(agent, gc.nogc()).step_value(agent, gc),
gc.reborrow(),
)
.unbind()
.bind(gc.nogc());
result.map(|r| {
iterator_is_done = r.is_none();
r.unwrap_or(Value::Undefined)
})
}
Instruction::BindingPatternBindRest
| Instruction::BindingPatternGetRestValue
| Instruction::BindingPatternBindRestToIndex => {
break_after_bind = true;
if iterator_is_done {
Ok(array_create(agent, 0, 0, None, gc.nogc()).unwrap().into())
} else {
with_vm_gc(
agent,
vm,
|agent, mut gc| {
let mut iterator = ActiveIterator::new(agent, gc.nogc());
let capacity = iterator.remaining_length_estimate(agent).unwrap_or(0);
let rest = array_create(agent, 0, capacity, None, gc.nogc())
.unwrap()
.scope(agent, gc.nogc());
let mut idx = 0u32;
while let Some(result) = iterator
.step_value(agent, gc.reborrow())
.unbind()?
.bind(gc.nogc())
{
unwrap_try(try_create_data_property_or_throw(
agent,
rest.get(agent),
PropertyKey::from(idx),
result.unbind(),
None,
gc.nogc(),
));
idx += 1;
}
iterator_is_done = true;
// SAFETY: rest is not shared
let rest: Value = unsafe { rest.take(agent).into() };
JsResult::Ok(rest)
},
gc.reborrow(),
)
.unbind()
.bind(gc.nogc())
}
}
Instruction::FinishBindingPattern => break,
_ => unreachable!(),
};
let value = match value {
Ok(value) => value,
Err(err) => {
// IteratorStep threw an error: this means that the iterator is
// immediately marked as closed and IteratorClose should not be
// observably called by our error handler. To ensure that, we
// replace the iterator with the empty slice iterator that will
// simply ignore a return call.
*vm.get_active_iterator_mut() = VmIteratorRecord::EmptySliceIterator;
// Now we're ready to rethrow the error.
return Err(err.unbind());
}
};
match instr.kind {
Instruction::BindingPatternSkip => {
if break_after_bind {
break;
} else {
continue;
}
}
Instruction::BindingPatternBind | Instruction::BindingPatternBindRest => {
let value = value.unbind();
with_vm_gc(
agent,
vm,
|agent, mut gc| {
let value = value.scope(agent, gc.nogc());
let binding_id =
executable.fetch_identifier(agent, instr.get_first_index(), gc.nogc());
let lhs = resolve_binding(
agent,
binding_id.unbind(),
None,
environment.as_ref().map(|v| v.get(agent)),
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
if environment.is_none() {
put_value(
agent,
&lhs.unbind(),
// SAFETY: value is not shared.
unsafe { value.take(agent) },
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
} else {
initialize_referenced_binding(
agent,
lhs.unbind(),
// SAFETY: value is not shared.
unsafe { value.take(agent) },
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
}
JsResult::Ok(())
},
gc.reborrow(),
)
.unbind()?;
}
Instruction::BindingPatternBindToIndex | Instruction::BindingPatternBindRestToIndex => {
let stack_slot = instr.get_first_index();
vm.stack[stack_slot] = value.unbind();
}
Instruction::BindingPatternGetValue | Instruction::BindingPatternGetRestValue => {
execute_nested_simple_binding(
agent,
vm,
executable.clone(),
value.unbind(),
environment.clone(),
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
}
_ => unreachable!(),
};
if break_after_bind {
break;
}
}
// 8.6.2 Runtime Semantics: BindingInitialization
// BindingPattern : ArrayBindingPattern
// 3. If iteratorRecord.[[Done]] is false, return
// ? IteratorClose(iteratorRecord, result).
// NOTE: `result` here is always UNUSED. We use `undefined` as a stand-in
// since that way we don't need to implement a separate iterator_close.
if !iterator_is_done {
let iter = vm.get_active_iterator_mut();
if iter.requires_return_call(agent, gc.nogc()) {
let result = with_vm_gc(
agent,
vm,
|agent, gc| {
ActiveIterator::new(agent, gc.nogc()).r#return(
agent,
Some(Value::Undefined),
gc,
)
},
gc,
)?;
vm.result = result.unbind();
}
}
Ok(())
}
pub(super) fn execute_simple_object_binding<'a>(
agent: &mut Agent,
vm: &mut Vm,
executable: Scoped<Executable>,
object: Object,
environment: Option<Scoped<Environment>>,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, ()> {
let object = object.scope(agent, gc.nogc());
let mut excluded_names = PropertyKeySet::new(gc.nogc()).scope(agent, gc.nogc());
loop {
let instr = executable.get_instruction(agent, &mut vm.ip).unwrap();
if agent.options.print_internals {
eprintln!("Executing: {:?}", instr.kind);
}
match instr.kind {
Instruction::Debug => {
if agent.options.print_internals {
eprintln!("Debug: {vm:#?}");
}
}
Instruction::BindingPatternBind | Instruction::BindingPatternBindNamed => {
with_vm_gc(
agent,
vm,
|agent, mut gc| {
let binding_id =
executable.fetch_identifier(agent, instr.get_first_index(), gc.nogc());
let property_key = if instr.kind == Instruction::BindingPatternBind {
binding_id.into()
} else {
let key_value = executable.fetch_constant(
agent,
instr.get_second_index(),
gc.nogc(),
);
// SAFETY: It should be impossible for binding pattern
// names to be integer strings.
unsafe { PropertyKey::from_value_unchecked(key_value) }
};
excluded_names.insert(agent, property_key);
let property_key = property_key.scope(agent, gc.nogc());
let lhs = resolve_binding(
agent,
binding_id.unbind(),
None,
environment.as_ref().map(|v| v.get(agent)),
gc.reborrow(),
)
.unbind()?;
let v = get(
agent,
object.get(agent),
// SAFETY: property_key is not shared.
unsafe { property_key.take(agent) },
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
if environment.is_none() {
put_value(agent, &lhs, v.unbind(), gc.reborrow()).unbind()?;
} else {
initialize_referenced_binding(agent, lhs, v.unbind(), gc.reborrow())
.unbind()?
}
JsResult::Ok(())
},
gc.reborrow(),
)
.unbind()?;
}
Instruction::BindingPatternBindToIndex => {
let value = with_vm_gc(
agent,
vm,
|agent, gc| {
let key_value =
executable.fetch_constant(agent, instr.get_second_index(), gc.nogc());
// SAFETY: It should be impossible for binding pattern
// names to be integer strings.
let key_value = unsafe { PropertyKey::from_value_unchecked(key_value) };
excluded_names.insert(agent, key_value);
get(agent, object.get(agent), key_value.unbind(), gc)
},
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
let stack_slot = instr.get_first_index();
vm.stack[stack_slot] = value.unbind();
}
Instruction::BindingPatternGetValueNamed => {
let v = with_vm_gc(
agent,
vm,
|agent, gc| {
// SAFETY: The constant was created using PropertyKey::from_str
// which checks for integer-ness, and then converted to Value
// without conversion, or is a floating point number string.
let property_key = unsafe {
PropertyKey::from_value_unchecked(executable.fetch_constant(
agent,
instr.get_first_index(),
gc.nogc(),
))
};
excluded_names.insert(agent, property_key);
get(agent, object.get(agent), property_key.unbind(), gc)
},
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
execute_nested_simple_binding(
agent,
vm,
executable.clone(),
v.unbind(),
environment.clone(),
gc.reborrow(),
)
.unbind()?;
}
Instruction::BindingPatternBindRest => {
with_vm_gc(
agent,
vm,
|agent, mut gc| {
// 1. Let lhs be ? ResolveBinding(StringValue of BindingIdentifier, environment).
let binding_id =
executable.fetch_identifier(agent, instr.get_first_index(), gc.nogc());
let lhs = resolve_binding(
agent,
binding_id.unbind(),
None,
environment.as_ref().map(|v| v.get(agent)),
gc.reborrow(),
)
.unbind()?;
// 2. Let restObj be OrdinaryObjectCreate(%Object.prototype%).
// 3. Perform ? CopyDataProperties(restObj, value, excludedNames).
let rest_obj = copy_data_properties_into_object(
agent,
object.get(agent),
excluded_names,
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
// 4. If environment is undefined, return ? PutValue(lhs, restObj).
// 5. Return ? InitializeReferencedBinding(lhs, restObj).
if environment.is_none() {
put_value(agent, &lhs, rest_obj.unbind().into(), gc.reborrow())
.unbind()?;
} else {
initialize_referenced_binding(
agent,
lhs,
rest_obj.unbind().into(),
gc.reborrow(),
)
.unbind()?;
}
JsResult::Ok(())
},
gc.reborrow(),
)
.unbind()?;
break;
}
Instruction::BindingPatternBindRestToIndex => {
let rest_obj = with_vm_gc(
agent,
vm,
|agent, gc| {
// 1. Let lhs be ? ResolveBinding(StringValue of BindingIdentifier, environment).
// 2. Let restObj be OrdinaryObjectCreate(%Object.prototype%).
// 3. Perform ? CopyDataProperties(restObj, value, excludedNames).
copy_data_properties_into_object(
agent,
object.get(agent),
excluded_names,
gc,
)
},
gc.reborrow(),
)
.unbind()?;
let stack_slot = instr.get_first_index();
vm.stack[stack_slot] = rest_obj.unbind().into();
break;
}
Instruction::FinishBindingPattern => break,
_ => unreachable!(),
}
}
Ok(())
}
pub(super) fn execute_nested_simple_binding<'a>(
agent: &mut Agent,
vm: &mut Vm,
executable: Scoped<Executable>,
value: Value,
environment: Option<Scoped<Environment>>,
mut gc: GcScope<'a, '_>,
) -> JsResult<'a, ()> {
let instr = executable.get_instruction(agent, &mut vm.ip).unwrap();
if agent.options.print_internals {
eprintln!("Executing: {:?}", instr.kind);
}
match instr.kind {
Instruction::BeginSimpleArrayBindingPattern => {
let result = with_vm_gc(
agent,
vm,
|agent, gc| VmIteratorRecord::from_value(agent, value, gc),
gc.reborrow(),
)
.unbind()?
.bind(gc.nogc());
vm.iterator_stack.push(result.unbind());
let result = execute_simple_array_binding(
agent,
vm,
executable,
environment.clone(),
gc.reborrow(),
)
.unbind();
let gc = gc.into_nogc();
let result = result.bind(gc);
vm.pop_iterator(gc);
result
}
Instruction::BeginSimpleObjectBindingPattern => {
let object = to_object(agent, value, gc.nogc()).unbind()?.bind(gc.nogc());
execute_simple_object_binding(
agent,
vm,
executable,
object.unbind(),
environment.clone(),
gc,
)
}
_ => unreachable!(),
}
}