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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
//! Entering a function — §10.2.1's `[[Call]]`, as the interpreter performs it.
//!
//! Three things are decided here and none of them belongs to the function object: the environment
//! the call runs in, the `this` it sees, and the frame that says how to get back. A function
//! object holds only the two halves that *are* its own — the code, and the environment it was
//! written in.
use super::{Fault, Vm};
use crate::compile::Chunk;
use crate::heap::{Callable, EnvironmentId, Heap, Object};
use crate::realm::NativeError;
use crate::value::{Abrupt, Value};
use std::rc::Rc;
impl Vm {
/// Call whatever is on the stack, leaving the interpreter running inside it.
///
/// The callee sits under its arguments, because it was pushed first — and a method call has
/// its receiver under that again. Nothing recurses: the frame is pushed, the code is swapped,
/// and [`Vm::run`]'s loop goes round again in the callee.
pub(super) fn enter(
&mut self,
how: Entry,
count: u32,
heap: &mut Heap,
chunk: &Chunk,
current: &mut Option<Rc<Chunk>>,
at: &mut usize,
) -> Result<(), Fault> {
self.enter_at(how, count, heap, chunk, current, at, false)
}
/// The same, told whether §15.10.4's `PrepareForTailCall` runs first — DR-0027.
///
/// A tail call is the ordinary entry with one thing done before it: the frame this call is made
/// from is taken down, so the callee answers to whoever *that* frame was answering to and
/// `frames` gains one entry and loses one. Everything else — the environment, the parameters,
/// the arguments object, the realm — is identical, which is why this is a parameter rather than
/// a second function.
#[allow(clippy::too_many_arguments)] // one more than `enter`, and the reason is the same
pub(super) fn enter_at(
&mut self,
how: Entry,
count: u32,
heap: &mut Heap,
chunk: &Chunk,
current: &mut Option<Rc<Chunk>>,
at: &mut usize,
tail: bool,
) -> Result<(), Fault> {
let count = count as usize;
// Which ways in have a value under the callee. A method call has its receiver there, and
// §28.1.2's has the `new.target` the caller named — a construction makes its own receiver,
// so that slot is free and this is what it is free for.
let method = matches!(how, Entry::Method | Entry::Named);
// The callee sits under its arguments, because it was pushed first — and a
// method call has its receiver under *that*.
let Some(callee_at) = self.stack.len().checked_sub(count + 1) else {
return Err(Fault::StackUnderflow);
};
let receiver_at = if method {
match callee_at.checked_sub(1) {
Some(at) => at,
None => return Err(Fault::StackUnderflow),
}
} else {
callee_at
};
let callee = self.stack[callee_at];
let Value::Object(object) = callee else {
self.raise(
Abrupt::type_error("what was called is not a function"),
heap,
chunk,
current,
at,
)?;
return Ok(());
};
let lexical = heap.object(object).and_then(Object::lexical);
let Some(callable) = heap.object(object).and_then(Object::call).cloned() else {
self.raise(
Abrupt::type_error("what was called is not a function"),
heap,
chunk,
current,
at,
)?;
return Ok(());
};
// §7.3.13 `Construct` requires an `IsConstructor`, and being callable is not it. An arrow
// (§15.3) and nearly every built-in (§10.3) have a `[[Call]]` and no `[[Construct]]`, so
// `new` in front of one is a TypeError rather than an object that nothing could be an
// instance of. Asked here, before the bound chain is flattened, because a bound function
// answers for itself — it is a constructor exactly when its target is, and it recorded
// that when it was made.
if matches!(how, Entry::Construct | Entry::Super | Entry::Named) && !callable.constructs() {
self.raise(
Abrupt::type_error("what was used with `new` is not a constructor"),
heap,
chunk,
current,
at,
)?;
return Ok(());
}
// §10.4.1 — a bound function is not a function of its own: it stands in front of another
// one with a receiver and some arguments already decided. Resolved here, before anything
// else, because what is actually being entered is the target.
if matches!(callable, Callable::Bound(_)) {
return self.enter_bound(
object,
how,
callee_at,
receiver_at,
count,
heap,
chunk,
current,
at,
);
}
// §10.3.1 — a built-in's `[[Call]]` does no receiver substitution and pushes no frame.
// It runs to completion and leaves one value where the callee and its arguments were,
// which is why it is answered here rather than joining the machinery below.
let body = match callable {
// Answered above; listed so that a third kind cannot arrive here unnoticed.
Callable::Bound(_) => return Err(Fault::MissingFunction),
// §27.5.1's three resumptions, which are not functions with bodies at all: what they
// enter is a generator's parked execution. Answered here, beside the built-ins, because
// like a built-in they push no frame of the callee's own — and unlike one, they may
// leave the loop running inside a body.
// §27.7.5.3's two closures. Like a resumption they enter the loop rather than running
// in Rust, and unlike one they answer to nobody: the job that calls them discards the
// completion, which is why nothing here is left on the stack for a caller.
Callable::Revive { kind, context } => {
return self.enter_revive(
kind,
context,
callee_at,
receiver_at,
count,
heap,
current,
at,
);
}
// §27.6.1's three methods and §27.5.1's read the same and are not the same: one
// answers a promise and the other an iterator result, and each refuses the other's
// receiver. Which prototype the method came off is carried on the function object,
// because by the time it is called there is nothing else left to ask.
Callable::Resume { kind, asynchronous } if asynchronous => {
return self.enter_async_resume(
kind,
how,
callee_at,
receiver_at,
count,
heap,
chunk,
current,
at,
);
}
Callable::Resume { kind, .. } => {
return self.enter_resume(
kind,
how,
callee_at,
receiver_at,
count,
heap,
chunk,
current,
at,
);
}
Callable::Native { native, .. } => {
return self.enter_native(
native,
object,
how,
callee_at,
receiver_at,
count,
heap,
chunk,
current,
at,
);
}
Callable::Bytecode { code: body, .. } => body,
};
// §15.7.14 — a class constructor has a `[[Construct]]` and its `[[Call]]` does nothing but
// refuse. Checked here rather than at the class definition because the two are separated by
// any amount of program: what arrives at a call site is a function object, and the body it
// holds is the only thing that still remembers how it was written.
if body.is_class_constructor()
&& !matches!(how, Entry::Construct | Entry::Super | Entry::Named)
{
self.raise(
Abrupt::type_error("a class constructor cannot be called without `new`"),
heap,
chunk,
current,
at,
)?;
return Ok(());
}
// §9.1.1.3 — `[[NewTarget]]` is the constructor a `new` named, `undefined` for every other
// way in, and *inherited* by a `super()`: §10.2.2 passes newTarget down rather than
// replacing it, which is what makes a chain of `extends` produce an instance of the class
// that was written after `new`. Computed before the receiver because the receiver is made
// from it.
//
// An arrow has no function environment of its own, so §13.3.12's lookup walks outward and
// arrives at whatever was in force where the arrow was written — the same walk, and the same
// answer, as the `this` below.
let new_target = match lexical {
Some(captured) => captured.new_target,
None => match how {
Entry::Construct => callee,
Entry::Super => self.new_target,
// The receiver slot carries it: a construction makes its own receiver from
// `new.target`, so the slot is free and this is what it is free for.
Entry::Named => self.stack[receiver_at],
Entry::Plain | Entry::Method => Value::Undefined,
},
};
// §10.2.2 — a *derived* constructor is not given a receiver at all. `this` starts unbound
// and `super()` creates it, which is DR-0015; making one here would be an object the
// constructor could never see and the parent would make a second.
let derived = body.derived_this().is_some();
// §10.2.2 step 5's `OrdinaryCreateFromConstructor`: `new` *makes* the receiver, out of
// **new.target's** `prototype` property. For a plain `new` new.target and the callee are
// the same object, which is why this was invisible until `super()` existed: there the
// callee is the parent and new.target is the class written after `new`, and only the
// second has the prototype the instance must inherit from.
//
// Built here rather than in the `match` below because reading that property is a `[[Get]]`
// — a getter, or a proxy's trap — and so may throw, which an arm producing a value cannot.
let made = if matches!(how, Entry::Construct | Entry::Super | Entry::Named) && !derived {
let from = match new_target {
Value::Object(target) => target,
// Unreachable from source — a construction always has one — and `object` is what
// §10.2.2 would fall back to, since it is the constructor being entered.
_ => object,
};
match self.prototype_property(from, heap) {
Ok(prototype) => Some(Value::Object(heap.new_object(Some(prototype)))),
Err(error) => {
self.raise(error, heap, chunk, current, at)?;
return Ok(());
}
}
} else {
None
};
// §10.2.1.2 and §10.2.2 — where the receiver comes from, and it comes from somewhere
// different in each of the ways in.
let receiver = match how {
// §10.2.1.2 `OrdinaryCallBindThis` — the substitution belongs to the **function**
// rather than to the shape of the call: a non-strict function is given the global
// object whenever the receiver is `undefined` or `null`, however it was called. So
// `f()` and `f.call()` and `f.call(null)` all agree, and a method call only differs
// because its receiver is an object already.
//
// Step 6.b.i wraps a *primitive* receiver, so a sloppy function called on a number is
// handed a **Number object**: `f.call(1)` writes `this.x` onto something that survives
// the call and can be returned, and `this instanceof Number` is true. A strict one is
// handed the primitive as it stands, which is what the rows below do.
// A plain call has no receiver slot at all — `receiver_at` is the callee — so what
// it passes is `undefined`, and the substitution then applies to that.
// …and **strict mode keeps the `undefined`**, which is step 3 of the same operation and
// the reason the flag has to reach the callee's body rather than the call site: a strict
// function called from sloppy code is still strict, so the caller cannot answer for it.
Entry::Plain if body.is_strict() => Value::Undefined,
Entry::Plain => Value::Object(self.realm.global()),
Entry::Method if body.is_strict() => self.stack[receiver_at],
Entry::Method => {
let given = self.stack[receiver_at];
// Steps 6.a and 6.b in one question, because `ToObject` has no answer for exactly
// the two values step 6.a is about. Asking "is it nullish" first and converting
// after would be a branch no input could tell from this one.
match self.wrapped(given, heap) {
Some(receiver) => receiver,
None => Value::Object(self.realm.global()),
}
}
// §10.2.2 step 5's `OrdinaryCreateFromConstructor`: `new` *makes* the receiver, out
// of the constructor's own `prototype` property. A `prototype` that is not an object
// — a script may assign anything to it — falls back to `Object.prototype`, which is
// what §10.1.13 says rather than an error.
Entry::Construct | Entry::Super | Entry::Named if derived => Value::Undefined,
Entry::Construct | Entry::Super | Entry::Named => match made {
Some(receiver) => receiver,
// `made` is filled in for exactly these entries, so this is a shape the types
// cannot rule out and nothing produces.
None => Value::Undefined,
},
};
if self.frames.len() >= MAX_CALL_DEPTH {
let thrown = self
.realm
.error(heap, NativeError::Range, "too much recursion");
self.unwind(thrown, chunk, current, at)?;
return Ok(());
}
// §10.2.11 — a new environment per call, written inside the one the function
// was *defined* in. That parent is the whole of what a closure is: the
// caller's environment has nothing to do with it, which is the difference
// between lexical scope and dynamic scope.
let Some(defined_in) = heap.object(object).and_then(Object::environment) else {
return Err(Fault::MissingFunction);
};
let environment =
heap.new_named_environment(Some(defined_in), body.locals(), Rc::clone(body.bindings()));
for offset in 0..body.parameters().min(count) {
let argument = self.stack[callee_at + 1 + offset];
let index = u32::try_from(offset).unwrap_or(u32::MAX);
heap.set_variable(environment, index, argument);
}
// §15.1 — a rest parameter takes every argument past the named ones, as an ordinary
// Array. Built here because this is the only place they exist: the body sees slots, and
// the arguments beyond the last slot are on the stack and nowhere else.
if let Some(slot) = body.rest() {
let from = callee_at + 1 + body.parameters().min(count);
let extra: Vec<Value> =
self.stack[from.min(self.stack.len())..callee_at + 1 + count].to_vec();
let array = heap.new_array(self.realm.array_prototype(), 0);
for (at, value) in extra.iter().enumerate() {
let index = heap.index_key(u32::try_from(at).unwrap_or(u32::MAX));
heap.define_own_property(
array,
index,
&crate::heap::PropertyDescriptor::data(*value),
);
}
heap.set_variable(environment, slot, Value::Object(array));
}
// §10.2.11 step 22 `CreateMappedArgumentsObject`, and only when the body reads the name.
// The values are every argument the call was given, and the map joins the first
// `parameters` of them to the slots filled just above — which is what makes
// `arguments[0]` and the first parameter one variable rather than two with equal values.
//
// **Two conditions and not one**, which is what step 22 actually says: the list must be
// simple *and the code must be sloppy*. Asking only about the list gave a strict function
// with plain parameters the mapped object, and the join is observable — `function f(a)
// { 'use strict'; a = 2; return arguments[0]; }` answered 2 where §10.2.11 says 1, because
// strict mode is where that link was taken away. It is also what a program uses to reach
// %ThrowTypeError% at all: the poisoned `callee` is the unmapped object's.
if let Some(slot) = body.arguments() {
let values: Vec<Value> = self.stack[callee_at + 1..callee_at + 1 + count].to_vec();
let prototype = self.realm.object_prototype();
let arguments = heap.new_arguments(
prototype,
&crate::heap::Incoming {
environment,
values: &values,
parameters: body.parameters(),
callee: object,
thrower: self.realm.thrower(),
mapped: body.simple_parameters() && !body.is_strict(),
iteration: heap
.well_known(crate::builtins::well_known_at("iterator"))
.map(|symbol| (symbol, self.realm.array_values())),
},
);
heap.set_variable(environment, slot, Value::Object(arguments));
}
// §27.7.5.1 `AsyncFunctionStart` — a promise, a context to park into, and then the body
// runs *now*: unlike a generator this pushes a frame and lets the loop carry on. What the
// call answers with is decided by whatever stops the body, and both `Await` and `Return`
// leave the same promise.
// §27.6 has no context object of its own: the async generator *is* the thing a body parks
// into and the thing that holds the promises, so making an `Await` context here would give
// the body two places to be parked and one of them would win.
let context = match body.is_async() && !body.is_generator() {
true => self.begin_async(heap),
false => None,
};
// §15.5.4 `EvaluateGeneratorBody` — everything above this line is
// `FunctionDeclarationInstantiation`, which a generator performs exactly as an ordinary
// function does, and so does everything below: a generator body is entered as an ordinary
// call and parks itself at `Instruction::GeneratorStart`, which the compiler puts after the
// parameters. Diverting here instead put the parameter list inside the parked body, where
// it ran at the first `next` — see that instruction.
// §15.10.4 `PrepareForTailCall` — the frame this call is made from goes down *before* the
// call, and the frame pushed for the callee inherits its return target whole: its code, its
// instruction, its `this`, its `new.target`, its realm, its environment and both its floors.
// That is the whole of DR-0027, and why the answer is indistinguishable from the ordinary
// call's: the callee answers to whoever this frame was going to answer to.
//
// `None` is either an ordinary call or one of the two shapes the source could not see — a
// construction or a suspendable body — and then this is the frame it always was.
let mut frame = match tail.then(|| self.take_frame_for_tail(heap)).flatten() {
Some(replaced) => replaced,
None => Frame {
code: (*current).take(),
at: *at,
this_value: self.this_value,
new_target: self.new_target,
realm: self.realm.id(),
environment: self.environment,
stack_base: receiver_at,
handlers_base: self.handlers.len(),
constructed: None,
function: None,
generator: None,
},
};
// §10.2.2 step 13 — a constructor's call answers with the object it was given
// unless its body returned an object of its own. A primitive `return` is ignored,
// which is why `function F() { return 1; }` still constructs an `F`.
// …and nothing for a derived one, whose answer §10.2.2 step 13 settles in the body:
// `CompleteDerivedReturn` puts the object on the stack, so by the time `Return` runs
// there is nothing left for the frame to prefer.
frame.constructed = match how {
Entry::Construct | Entry::Super | Entry::Named if !derived => Some(receiver),
_ => None,
};
frame.function = Some(object);
// An `async` function's context object, or nothing. A **generator's** own object is not
// known yet and reaches the frame later: the body is entered as an ordinary call and
// `Vm::start_generator` fills this in when `Instruction::GeneratorStart` runs, which is
// after the parameters. This used to say a generator's frame was not pushed here at
// all, which stopped being true when that instruction arrived — and contradicted the
// comment twenty lines above it.
frame.generator = context;
// To the *replaced* frame's floor for a tail call, which is what stops the operand stack
// growing where the frame no longer does: `switch (0) { case 0: return f(n - 1) }` leaves
// its discriminant above this line, and a hundred thousand of those is the leak this
// truncation is the absence of. Safe here and nowhere earlier — the arguments were copied
// into the environment several steps above.
self.stack.truncate(frame.stack_base);
self.frames.push(frame);
self.environment = environment;
// §10.2.1.2 step 1 — an arrow's `[[ThisMode]]` is `lexical`, so `OrdinaryCallBindThis`
// returns without binding anything and the receiver computed above is discarded. What the
// body sees instead is the `this` the arrow was *written* beside, which it has held since
// it was made. This is why `f.call(other)` cannot move an arrow's `this` and why passing
// one as a callback keeps it: neither is a place the arrow was written.
self.this_value = match lexical {
Some(captured) => captured.this_value,
None => receiver,
};
self.new_target = new_target;
// §10.2.1 step 3 — the execution context this call pushes has the **callee's** realm, so a
// function taken out of another realm and called here resolves its intrinsics against the
// realm it was written in. `own_realm` rather than `realm_of`: what is being entered has a
// `[[Realm]]` of its own or it is not a body, and something without one must not move the
// running realm at all.
if let Some(callee_realm) = self.own_realm(object, heap) {
self.realm = callee_realm;
}
*current = Some(body);
*at = 0;
Ok(())
}
/// Take this frame down for a tail call, unless its return is one that still has work to do.
///
/// **The two cases the compiler cannot see**, because both are facts about how the frame was
/// *entered* rather than about what the body says — the same body runs for `f(1)` and for
/// `new f(1)`:
///
/// - A construction. §10.2.2 step 13 answers with the object that was made unless the body
/// returned one of its own, so the frame is still holding the answer.
/// - A suspendable body. A generator answers `{ value, done: true }`, an `async` function
/// resolves a promise, an async generator settles a request — none of them answers with the
/// value returned, so none of them can hand its return over to a callee.
///
/// `None` in either case, and the call then proceeds as an ordinary one. That is observable
/// only as the stack growing, which is what an engine with no §15.10 at all does everywhere.
fn take_frame_for_tail(&mut self, heap: &Heap) -> Option<Frame> {
let frame = self.frames.last()?;
if frame.constructed.is_some() || Self::suspendable_of(frame, heap).is_some() {
return None;
}
self.frames.pop()
}
/// Enter what a bound function stands in front of — §10.4.1.1 and §10.4.1.2.
///
/// The chain is flattened rather than followed by recursing. `f.bind(a).bind(b)` is a bound
/// function whose target is a bound function, and a program may write as many of those as it
/// likes — so recursing here would put a Rust frame on the stack per `bind`, and DR-0002 does
/// not allow a script to decide how deep the Rust stack goes.
///
/// Walking outwards, each binding's arguments go in *front* of the ones collected so far,
/// because the outermost `bind` is the one nearest the call. The receiver is the innermost
/// binding's, for the same reason in reverse: `f.bind(a).bind(b)` calls `f` with `a`, since
/// the second `bind` binds the already-bound function and §10.4.1.1 never looks past its own
/// target.
#[allow(clippy::too_many_arguments)] // the call's shape, threaded rather than shared
fn enter_bound(
&mut self,
bound: crate::heap::ObjectId,
how: Entry,
callee_at: usize,
receiver_at: usize,
count: usize,
heap: &mut Heap,
chunk: &Chunk,
current: &mut Option<Rc<Chunk>>,
at: &mut usize,
) -> Result<(), Fault> {
let given: Vec<Value> = self.stack[callee_at + 1..callee_at + 1 + count].to_vec();
let mut prefix: Vec<Value> = Vec::new();
let mut receiver = Value::Undefined;
let mut target = bound;
// Bounded because a hand-built heap could point a bound function at itself; no `bind` can
// make such a cycle, since it binds a function that already exists.
for _ in 0..MAX_CALL_DEPTH {
let Some(Callable::Bound(binding)) =
heap.object(target).and_then(Object::call).cloned()
else {
let mut arguments = prefix;
arguments.extend_from_slice(&given);
return self.enter_flattened(
target,
receiver,
&arguments,
how,
receiver_at,
heap,
chunk,
current,
at,
);
};
let mut ahead = binding.arguments;
ahead.extend_from_slice(&prefix);
prefix = ahead;
receiver = binding.this_value;
target = binding.target;
}
let thrown = self
.realm
.error(heap, NativeError::Range, "too much recursion");
self.unwind(thrown, chunk, current, at)?;
Ok(())
}
/// Call `target` with the receiver and arguments a chain of bindings settled on.
#[allow(clippy::too_many_arguments)] // as above
fn enter_flattened(
&mut self,
target: crate::heap::ObjectId,
receiver: Value,
arguments: &[Value],
how: Entry,
receiver_at: usize,
heap: &mut Heap,
chunk: &Chunk,
current: &mut Option<Rc<Chunk>>,
at: &mut usize,
) -> Result<(), Fault> {
let Ok(count) = u32::try_from(arguments.len()) else {
let thrown = self
.realm
.error(heap, NativeError::Range, "too many arguments");
self.unwind(thrown, chunk, current, at)?;
return Ok(());
};
self.stack.truncate(receiver_at);
// §10.4.1.2 — `new` on a bound function constructs the *target*, and the bound `this` is
// not consulted at all: `new` makes its own receiver, so there is nothing for it to say.
let how = match how {
// A named `new.target` survives the bind, because §10.4.1.2 step 5 passes it through:
// `Reflect.construct(f.bind(x), [], G)` makes an object from `G.prototype`, and the
// bound receiver is no more consulted than it is for a plain `new`.
Entry::Construct | Entry::Named => how,
_ => {
self.stack.push(receiver);
Entry::Method
}
};
self.stack.push(Value::Object(target));
self.stack.extend_from_slice(arguments);
self.enter(how, count, heap, chunk, current, at)
}
/// Run a built-in and leave its answer where the call was — §10.3.1 and §10.3.2.
///
/// Nothing is suspended. A built-in is Rust: it runs, it answers, and the interpreter carries
/// on at the next instruction, so there is no frame to push and none to come back to. That is
/// also why the recursion limit does not apply — a built-in cannot recurse into the
/// interpreter, because it has no way to reach it.
#[allow(clippy::too_many_arguments)] // the call's shape, threaded rather than shared
fn enter_native(
&mut self,
native: crate::heap::Native,
function: crate::heap::ObjectId,
how: Entry,
callee_at: usize,
receiver_at: usize,
count: usize,
heap: &mut Heap,
chunk: &Chunk,
current: &mut Option<Rc<Chunk>>,
at: &mut usize,
) -> Result<(), Fault> {
// §10.3.1 step 3 passes `thisArgument` straight through — no global-object substitution,
// which is why `Error.prototype.toString.call(undefined)` throws where a sloppy-mode
// JavaScript function would have been handed the global object instead.
//
// §10.3.2's `[[Construct]]` does not make the receiver either: a built-in constructor
// makes its own object, out of its own `prototype`, which is the whole reason
// `Error("x")` and `new Error("x")` come to the same thing.
let this_value = match how {
Entry::Method => self.stack[receiver_at],
Entry::Plain | Entry::Construct | Entry::Super | Entry::Named => Value::Undefined,
};
let arguments = self.stack[callee_at + 1..callee_at + 1 + count].to_vec();
let call = crate::heap::NativeCall {
function,
this_value,
arguments: &arguments,
// §9.4's `[[NewTarget]]`, and for a `super()` it is the *inherited* one:
// `class D extends Error {}` runs `Error` with a target of `D`, which is the only thing
// that knows the object must inherit from `D.prototype`. A native is never an arrow, so
// there is no captured target that could take precedence over these.
new_target: match how {
Entry::Construct => Value::Object(function),
Entry::Super => self.new_target,
// §28.1.2 — the third thing the caller named, carried in the receiver slot because
// a construction has no other use for it. This is what a built-in's
// `prototype_from` reads, so `Reflect.construct(Array, [], D)` really does make an
// array that inherits from `D.prototype`.
Entry::Named => self.stack[receiver_at],
Entry::Plain | Entry::Method => Value::Undefined,
},
};
// §10.3.1 step 3 — a built-in runs with **its own** realm as the running one, not with the
// caller's. That is what makes `new other.Function()` produce a function belonging to the
// other realm, and what makes an error thrown out of `other.Array.prototype.map` be the
// other realm's TypeError. Restored afterwards whatever the call came to, because a throw
// leaves through this same path — DR-0025.
//
// Saved and put back here rather than on a `Frame`, because a native pushes none: it runs to
// completion inside this Rust call, which is the same reason its receiver and arguments are
// handled here instead of by the machinery below.
// `own_realm` and not `realm_of`: a proxy has no `[[Realm]]`, and §10.5.12 is an *internal
// method* rather than a built-in — calling through one pushes no execution context, so the
// running realm stays the caller's and the trap's arguments array is made in it.
let caller_realm = self.realm;
if let Some(callee_realm) = self.own_realm(function, heap) {
self.realm = callee_realm;
}
let answer = native(self, heap, &call);
// The callee, its receiver and its arguments all go, and the answer takes their place —
// exactly what a return from a JavaScript function leaves behind.
self.stack.truncate(receiver_at);
// `None` is a throw a handler took, and it has already moved the program counter — so
// there is nothing to push and nothing else to do.
//
// **Settled before the realm goes back**, which is not tidiness: `settle` is where an
// `Abrupt` becomes an error *object*, and §10.3.1's context is still the callee's when it
// does. Restoring first gives a built-in from another realm the caller's `TypeError`, and
// `RegExp/prototype/*/cross-realm.js` asks for the other one by identity.
let settled = self.settle(answer, heap, chunk, current, at);
self.realm = caller_realm;
if let Some(value) = settled? {
self.stack.push(value);
}
Ok(())
}
/// The object a constructor's instances inherit from — §10.2.2 step 5.
///
/// A function's `prototype` is an ordinary writable property, so a script may put anything
/// there. §10.1.13 says an instance falls back to `%Object.prototype%` when it is not an
/// object, rather than the construction failing — which is why `F.prototype = 1; new F()`
/// works and gives an ordinary object.
fn prototype_property(
&mut self,
constructor: crate::heap::ObjectId,
heap: &mut Heap,
) -> crate::value::Completion<crate::heap::ObjectId> {
self.prototype_for(constructor, self.realm.object_prototype(), heap)
}
/// The same, with the intrinsic §10.1.13 falls back to named by the caller.
///
/// `new f()` falls back to `%Object.prototype%` and a generator to `%GeneratorPrototype%`, and
/// the clause is the same one both times: `GetPrototypeFromConstructor` takes the intrinsic as
/// an argument. Two callers, one walk, and the fallback is the only thing that differs.
pub(super) fn prototype_for(
&mut self,
constructor: crate::heap::ObjectId,
fallback: crate::heap::ObjectId,
heap: &mut Heap,
) -> crate::value::Completion<crate::heap::ObjectId> {
let key = crate::heap::PropertyKey::from_units(
heap,
&"prototype".encode_utf16().collect::<Vec<_>>(),
);
// §10.1.13's `GetPrototypeFromConstructor` asks `Get(constructor, "prototype")`, which is
// a full `[[Get]]`: a getter runs, and a proxy `new.target` answers with its trap. Reading
// the property table instead finds nothing on a proxy, and every instance built through
// one then inherited from `Object.prototype`.
let value = self.get_property_key(Value::Object(constructor), key, heap)?;
Ok(match value {
Value::Object(prototype) => prototype,
// A `prototype` that is not an object — a script may assign anything to it — falls
// back to the intrinsic the caller named, which is what §10.1.13 says rather than an
// error.
_ => fallback,
})
}
}
/// One suspended call — where to come back to, and what to put back when we do.
///
/// A call does **not** recurse into the interpreter. The loop stays one loop and a frame is a
/// record, which is why a thousand-deep JavaScript recursion costs a thousand small structs
/// rather than a thousand Rust stack frames — and why the limit on it can be a number rather than
/// a guess about the host's stack.
#[derive(Debug)]
pub(super) struct Frame {
/// The code that was running, and the instruction to come back to.
pub(super) code: Option<Rc<Chunk>>,
pub(super) at: usize,
/// The object `new` made, if this call was a construction that made one.
///
/// §10.2.2 step 13: a constructor answers with the object it was given unless its body
/// returned an object of its own, so the answer has to be kept until the return decides.
pub(super) constructed: Option<Value>,
/// The function object this call entered — §10.2.2's *active function object*.
///
/// `None` only for the script, which no function entered. What needs it is `super()`: §10.2.2's
/// `GetSuperConstructor` reads the running function's `[[Prototype]]`, and it reads it *now*
/// rather than at the class definition, because `Object.setPrototypeOf(D, Other)` changes what
/// `super()` reaches. So the answer cannot be compiled in and the frame is the only thing that
/// still knows which function is running.
pub(super) function: Option<crate::heap::ObjectId>,
/// The generator whose body this frame runs, if it runs one — §27.5.1's other direction.
///
/// `None` for every ordinary call, which is nearly all of them. What needs it is `Return`: a
/// generator's body answers with an iterator result rather than with the value it returned, and
/// the generator has to be marked completed — and by then the frame is the only thing that
/// still knows which object this execution belongs to.
pub(super) generator: Option<crate::heap::ObjectId>,
/// The `this` to go back to.
pub(super) this_value: Value,
/// The `new.target` to go back to.
///
/// Saved beside the `this` because §9.1.1.3 keeps the two in one record: a call decides both,
/// and a return has to put both back or a constructor that called a plain function would find
/// its own `new.target` gone when the call came back.
pub(super) new_target: Value,
/// The realm to go back to — §10.2.1's execution context has one, and so does the caller's.
///
/// Saved beside the `this` and the `new.target` for the same reason and by the same rule: a call
/// decides it, and a return has to put it back. What decides it is §10.3.1 step 3 and §10.2.1
/// step 3 together — a function runs with **its own** realm as the running one, so a function
/// taken out of another realm and called here resolves its intrinsics against the realm it was
/// written in. DR-0025.
///
/// A [`RealmId`] and **not** a `Realm`, for the reason the record gives about a function object
/// and for a sharper one here: a `Realm` is 616 bytes, a frame is pushed per call, and DR-0019
/// measured a call's retained cost at 74. Holding the realm itself took a frame from 120 bytes
/// to 736 and made every deep recursion six times as expensive — measured, not guessed.
pub(super) realm: crate::heap::RealmId,
/// The environment to go back to.
///
/// Not the callee's — that one may outlive the call, if the callee made a closure over it.
pub(super) environment: EnvironmentId,
/// Where this frame's operands begin.
///
/// A floor rather than a count: returning truncates back to it, which is what makes a
/// `return` from the middle of an expression leave nothing of that expression behind.
pub(super) stack_base: usize,
/// How many handlers were installed when the call began.
///
/// A `try` inside the callee must not catch on the caller's behalf, and a throw that escapes
/// the callee must find the caller's handlers intact — so unwinding pops frames and handlers
/// together, down to this mark.
pub(super) handlers_base: usize,
}
/// How many calls may be waiting at once before a further one is a **RangeError**.
///
/// Every engine has one and none of them is in the specification: §9.4's note says an
/// implementation may limit recursion and should report it as a RangeError, which is the
/// "Maximum call stack size exceeded" every browser prints.
///
/// The number is about memory rather than about the host's stack, because a call here is a frame
/// *record* and not a Rust frame — the interpreter's loop stays one loop however deep the
/// JavaScript goes. Ten thousand is deeper than any recursion a program means to make and
/// shallow enough that overrunning it costs a few hundred kilobytes rather than the machine.
pub(super) const MAX_CALL_DEPTH: usize = 10_000;
/// The three ways into a function, and they differ only in where the receiver comes from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Entry {
/// `f()` — no receiver, so §10.2.1.2 substitutes the global object.
Plain,
/// `o.m()` — the object the method was found on.
Method,
/// `new f()` — a fresh object, made from the constructor's `prototype`.
Construct,
/// `super()` — a construction whose `new.target` is inherited rather than being the callee.
///
/// Its own way in rather than `Construct` with a flag, because the two differ in the one thing
/// that decides what object is made: §10.2.2 step 5 builds the receiver from **new.target's**
/// `prototype`, and for a plain `new` that is the callee's own while for a `super()` it is the
/// derived class's. That is what makes `new E()` an `E` however many `extends` clauses it passes
/// through, and reading the parent's `prototype` instead would quietly produce a `B`.
Super,
/// §28.1.2's `Reflect.construct(target, args, newTarget)` — a construction whose `new.target`
/// is neither the callee nor the running one, but a third thing the caller named.
///
/// Its own way in for the reason `Super` is: what it changes is the one thing that decides what
/// object is made. It is the only way in the language to build an X whose prototype came from a
/// Y, and there is nowhere else the third value could come from — a plain `new` has two.
Named,
}
#[cfg(test)]
mod tests {
use super::Frame;
#[test]
fn a_frame_stays_small_because_one_is_pushed_per_call() {
// A structural test, and the only kind that can hold this: nothing a program does tells a
// 128-byte frame from a 736-byte one, so no behavioural test can fail when it grows.
//
// DR-0019 measured a call's retained cost at 74 bytes and DR-0023's collection schedule was
// chosen against that number. DR-0025 then gave the frame a realm, and holding the `Realm`
// itself — 616 bytes, `Copy`, and tempting because every other realm value here is one —
// took the frame to **736**. A thousand-deep recursion went from 120 KiB to 720 KiB of
// arena for a fact that fits in four bytes.
//
// The number is a ceiling with room in it rather than the size itself: a field worth adding
// should not have to move a test, and a field that doubles the frame should.
assert!(
std::mem::size_of::<Frame>() <= 192,
"a frame is {} bytes — see this test for why that matters",
std::mem::size_of::<Frame>()
);
}
}