pub struct Interp<'a> {
pub heap: &'a Heap,
pub realm: Realm,
pub output: String,
pub max_frames: usize,
/* private fields */
}Expand description
The interpreter: a realm plus a frame stack.
Fields§
§heap: &'a HeapThe heap everything is allocated on.
realm: RealmThe realm.
output: StringText written by print and friends, in order.
Buffered rather than streamed so a test can inspect it; the CLI flushes it once at the end, which is also when upstream’s line buffering becomes visible for the cases compared here.
max_frames: usizeThe most frames that may be live at once.
Implementations§
Source§impl<'a> Interp<'a>
impl<'a> Interp<'a>
Sourcepub fn bigint(&self, value: BigInt) -> Value
pub fn bigint(&self, value: BigInt) -> Value
A new string value.
A BigInt value from a computed big integer.
Sourcepub fn string_wtf8(&self, text: Wtf8) -> Value
pub fn string_wtf8(&self, text: Wtf8) -> Value
A string value from bytes that are already WTF-8, which is how a string with an
unpaired surrogate reaches a Value.
Sourcepub fn string(&self, text: impl Into<String>) -> Value
pub fn string(&self, text: impl Into<String>) -> Value
A new string value, from text that is valid UTF-8.
Sourcepub fn new_object(&self, proto: Option<Gc<JsObject>>) -> Gc<JsObject>
pub fn new_object(&self, proto: Option<Gc<JsObject>>) -> Gc<JsObject>
A new ordinary object with the given prototype.
Sourcepub fn object_own_property_keys(
&mut self,
object: &Gc<JsObject>,
) -> JsResult<Vec<PropertyKey>>
pub fn object_own_property_keys( &mut self, object: &Gc<JsObject>, ) -> JsResult<Vec<PropertyKey>>
[[OwnPropertyKeys]], with the ownKeys trap when there is one.
A trap’s list is used as returned: unlike an ordinary object, whose integer keys the engine sorts, a proxy is specified to hand back exactly what the handler produced. That is why the two branches are not merged.
Sourcepub fn promise_state(&self, object: &Gc<JsObject>) -> Option<PromiseRef>
pub fn promise_state(&self, object: &Gc<JsObject>) -> Option<PromiseRef>
The state behind a promise object, or None when object is not one.
Sourcepub fn new_promise(&mut self) -> Gc<JsObject>
pub fn new_promise(&mut self) -> Gc<JsObject>
A new pending promise.
Sourcepub fn fulfill_promise(&mut self, promise: &Gc<JsObject>, value: Value)
pub fn fulfill_promise(&mut self, promise: &Gc<JsObject>, value: Value)
Fulfil promise, unless it has already settled.
Settling is one-shot, and a script can see that: resolve called twice, or called
after reject, changes nothing.
Sourcepub fn reject_promise(&mut self, promise: &Gc<JsObject>, reason: Value)
pub fn reject_promise(&mut self, promise: &Gc<JsObject>, reason: Value)
Reject promise, unless it has already settled.
Sourcepub fn new_promise_capability(&mut self) -> (Gc<JsObject>, Value, Value)
pub fn new_promise_capability(&mut self) -> (Gc<JsObject>, Value, Value)
NewPromiseCapability as the executor receives it: the promise plus its
resolve/reject pair.
The pair are callable objects rather than Rust closures, because the script gets them and may stash them for later.
Sourcepub fn promise_resolve_value(&mut self, value: Value) -> JsResult<Value>
pub fn promise_resolve_value(&mut self, value: Value) -> JsResult<Value>
PromiseResolve(value): the value itself when it is already a promise, otherwise a
new promise resolved with it.
Sourcepub fn resolve_promise_with(
&mut self,
promise: &Gc<JsObject>,
value: Value,
) -> JsResult<()>
pub fn resolve_promise_with( &mut self, promise: &Gc<JsObject>, value: Value, ) -> JsResult<()>
ResolvePromise: fulfil, unless the value is a thenable — then follow it.
Sourcepub fn perform_then(
&mut self,
promise: &Gc<JsObject>,
on_fulfilled: Option<Value>,
on_rejected: Option<Value>,
) -> JsResult<Value>
pub fn perform_then( &mut self, promise: &Gc<JsObject>, on_fulfilled: Option<Value>, on_rejected: Option<Value>, ) -> JsResult<Value>
Promise.prototype.then: register a reaction and answer the derived promise.
Sourcepub fn take_unhandled_rejections(&mut self) -> Vec<String>
pub fn take_unhandled_rejections(&mut self) -> Vec<String>
The rejections nobody listened to, formatted the way the reference reports them.
Each line is Possibly unhandled promise rejection: <value> followed by the stack
when there is one. The adjective is the reference’s: a rejection is only possibly
unhandled at the point the queue drains, because a then may still be attached
before the host acts on it.
Sourcepub fn drain_jobs(&mut self) -> JsResult<()>
pub fn drain_jobs(&mut self) -> JsResult<()>
Run the microtask queue to exhaustion.
Jobs queued while draining run in the same drain, FIFO, which is what makes
Promise.resolve().then(f).then(g) call f before g.
Sourcepub fn new_async_function(
&mut self,
function: Value,
this: Value,
args: Vec<Value>,
) -> JsResult<Value>
pub fn new_async_function( &mut self, function: Value, this: Value, args: Vec<Value>, ) -> JsResult<Value>
Build an async function’s result: a promise whose body runs as a coroutine.
Sourcepub fn object_get_own_property(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<Option<Property>>
pub fn object_get_own_property( &mut self, object: &Gc<JsObject>, key: &PropertyKey, ) -> JsResult<Option<Property>>
[[GetOwnProperty]], with the getOwnPropertyDescriptor trap when there is one.
Sourcepub fn object_define_own_property(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
property: Property,
) -> JsResult<bool>
pub fn object_define_own_property( &mut self, object: &Gc<JsObject>, key: &PropertyKey, property: Property, ) -> JsResult<bool>
[[DefineOwnProperty]], with the defineProperty trap when there is one.
Sourcepub fn new_proxy(&self, target: Gc<JsObject>, handler: Value) -> Gc<JsObject>
pub fn new_proxy(&self, target: Gc<JsObject>, handler: Value) -> Gc<JsObject>
Build a proxy object over target, with its traps read from handler.
Sourcepub fn new_revoker(&self, slots: ProxyRef) -> Gc<JsObject>
pub fn new_revoker(&self, slots: ProxyRef) -> Gc<JsObject>
The revoke function for a proxy’s slots.
Sourcepub fn new_native(
&self,
name: impl Into<String>,
length: u32,
func: NativeFn,
) -> Gc<JsObject>
pub fn new_native( &self, name: impl Into<String>, length: u32, func: NativeFn, ) -> Gc<JsObject>
A new native function object.
The observable name and length are installed as properties here, which is
what Function.prototype.name and .length read; nothing else stores them,
so f.name = "x" behaves like the writable property it is.
Sourcepub fn new_bound(
&self,
target: Value,
this: Value,
args: Vec<Value>,
) -> Gc<JsObject>
pub fn new_bound( &self, target: Value, this: Value, args: Vec<Value>, ) -> Gc<JsObject>
A new bound function.
Sourcepub fn run_script(&mut self, code: CompiledFunction) -> JsResult<Value>
pub fn run_script(&mut self, code: CompiledFunction) -> JsResult<Value>
Evaluate code as a script body and return its completion value.
Sourcepub fn eval_in_global(&mut self, source: &str) -> JsResult<Value>
pub fn eval_in_global(&mut self, source: &str) -> JsResult<Value>
Compile and run source in global scope, returning its completion value.
This is what eval and new Function use. Running it as a script means it
sees the global object and nothing else, which is right for eval("1+1") and
for a Function body; a direct eval that reads a local variable would need
the compiler to hand its variable environment over, which is not implemented.
Sourcepub fn run_function_source(&mut self, code: CompiledFunction) -> JsResult<Value>
pub fn run_function_source(&mut self, code: CompiledFunction) -> JsResult<Value>
Run a compiled script whose completion value is a function, returning it.
new Function(...) compiles (function(params) { body }) and this runs it to
get the closure out, which keeps the constant-pool plumbing in one place.
Sourcepub fn current_strict(&self) -> bool
pub fn current_strict(&self) -> bool
Whether the code currently running is strict.
Read off the frame’s function, which is where the parser’s answer ended up; a native function never runs strict-only checks, and there is no frame at all before the first script.
Sourcepub fn get_property(
&mut self,
object: &Gc<JsObject>,
key: &str,
) -> JsResult<Option<Value>>
pub fn get_property( &mut self, object: &Gc<JsObject>, key: &str, ) -> JsResult<Option<Value>>
Read object[key], following the prototype chain.
An inherited accessor runs with the receiver as this, which is why the
lookup returns the owner rather than just the value.
Sourcepub fn get_property_key(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<Option<Value>>
pub fn get_property_key( &mut self, object: &Gc<JsObject>, key: &PropertyKey, ) -> JsResult<Option<Value>>
Interp::get_property with a key that may be a symbol.
Sourcepub fn get_property_key_for(
&mut self,
object: &Gc<JsObject>,
receiver: Option<&Value>,
key: &PropertyKey,
) -> JsResult<Option<Value>>
pub fn get_property_key_for( &mut self, object: &Gc<JsObject>, receiver: Option<&Value>, key: &PropertyKey, ) -> JsResult<Option<Value>>
Interp::get_property_key, with an explicit this for an inherited accessor.
The receiver matters when the property is found on a prototype: a getter has to
see the object the read started from. For a primitive that is the primitive
itself — Symbol("x").description runs the getter with the symbol as this,
not with Symbol.prototype — which is why the receiver cannot be inferred from
the owner.
Sourcepub fn set_property(
&mut self,
object: &Gc<JsObject>,
key: &str,
value: Value,
) -> JsResult<()>
pub fn set_property( &mut self, object: &Gc<JsObject>, key: &str, value: Value, ) -> JsResult<()>
Store object[key] = value, honouring setters and inherited properties.
Sourcepub fn set_property_with(
&mut self,
object: &Gc<JsObject>,
key: &str,
value: Value,
on_failure: DeleteFailure,
) -> JsResult<()>
pub fn set_property_with( &mut self, object: &Gc<JsObject>, key: &str, value: Value, on_failure: DeleteFailure, ) -> JsResult<()>
Store a property, choosing what to do when the object refuses.
A rejected store is a TypeError in strict mode and a silent no-op in sloppy
mode, which is the one place assignment differs by mode. Native code (the array
methods, JSON) always asks for the throw.
Sourcepub fn set_property_with_key(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
value: Value,
on_failure: DeleteFailure,
) -> JsResult<()>
pub fn set_property_with_key( &mut self, object: &Gc<JsObject>, key: &PropertyKey, value: Value, on_failure: DeleteFailure, ) -> JsResult<()>
Interp::set_property_with with a key that may be a symbol.
Sourcepub fn delete_property(
&mut self,
object: &Gc<JsObject>,
key: &str,
) -> JsResult<bool>
pub fn delete_property( &mut self, object: &Gc<JsObject>, key: &str, ) -> JsResult<bool>
Delete object[key].
Sourcepub fn delete_property_key(
&mut self,
object: &Gc<JsObject>,
key: &PropertyKey,
) -> JsResult<bool>
pub fn delete_property_key( &mut self, object: &Gc<JsObject>, key: &PropertyKey, ) -> JsResult<bool>
Interp::delete_property with a key that may be a symbol.
Sourcepub fn call_value(
&mut self,
callee: Value,
this: Value,
args: Vec<Value>,
) -> JsResult<Value>
pub fn call_value( &mut self, callee: Value, this: Value, args: Vec<Value>, ) -> JsResult<Value>
Call callee with this and args.
Sourcepub fn using_method_for(
&mut self,
value: &Value,
await_form: bool,
) -> JsResult<Value>
pub fn using_method_for( &mut self, value: &Value, await_form: bool, ) -> JsResult<Value>
The dispose method a using declaration should snapshot, or an error.
await-using prefers Symbol.asyncDispose and falls back to Symbol.dispose, which
is what makes a resource that only knows the synchronous protocol usable in both
forms.
Sourcepub fn merge_dispose_error(
&mut self,
new_error: Value,
error_state: Value,
) -> Value
pub fn merge_dispose_error( &mut self, new_error: Value, error_state: Value, ) -> Value
Combine a disposal failure with whatever was already going wrong.
Sourcepub fn new_suppressed_error(&mut self, error: Value, suppressed: Value) -> Value
pub fn new_suppressed_error(&mut self, error: Value, suppressed: Value) -> Value
A SuppressedError instance built straight from its prototype.
Not through the constructor: a script that rewrites
SuppressedError.prototype.constructor must not be able to divert the engine’s own
error wrapping.
Sourcepub fn array_like_to_args(&mut self, value: &Value) -> JsResult<Vec<Value>>
pub fn array_like_to_args(&mut self, value: &Value) -> JsResult<Vec<Value>>
Call callee, or push its frame and let the main loop run it.
This is what keeps deep JavaScript recursion off the Rust stack: a bytecode
callee becomes a frame on Interp::frames and the instruction loop simply
continues, so function f(){ return f() } costs heap and stops at
Interp::max_frames instead of aborting the process. Only a native callee
is entered directly, and natives do not re-enter JavaScript in this
milestone.
Ok(None) means a frame was pushed: the caller must not push a result,
because the returning frame will.
The elements of an array-like value, as a call argument list.
apply accepts any array-like, not only a real array: the reference reads
length and then each index, so {length: 2, 0: "a", 1: "b"} works too.
Sourcepub fn new_generator(
&mut self,
function: Value,
this: Value,
args: Vec<Value>,
) -> JsResult<Value>
pub fn new_generator( &mut self, function: Value, this: Value, args: Vec<Value>, ) -> JsResult<Value>
Turn a call to a generator function into a generator object.
The frame is built exactly as an ordinary call would build it, then detached and
stored in the generator before any body instruction runs — which is what makes
function* g(){ sideEffect() } inert until next(). The one thing that does run
is initial_yield, the first instruction of every suspending body.
Sourcepub fn resume_generator(
&mut self,
generator: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<Value>
pub fn resume_generator( &mut self, generator: &Gc<JsObject>, kind: ResumeKind, value: Value, ) -> JsResult<Value>
Advance a generator object, as %GeneratorPrototype%.next and friends do.
Returns the {value, done} object itself rather than the pair, because a
yield* suspension already has one — the inner iterator’s own result — and the
protocol says to hand that straight through rather than nest it.
Sourcepub fn resume_async_generator(
&mut self,
generator: &Gc<JsObject>,
kind: ResumeKind,
value: Value,
) -> JsResult<Value>
pub fn resume_async_generator( &mut self, generator: &Gc<JsObject>, kind: ResumeKind, value: Value, ) -> JsResult<Value>
next/return/throw on an async generator, which answer a promise.
The body is the same coroutine a sync generator runs; what changes is that an
await inside it has to settle before the method can answer, so the step is
driven off the job queue rather than returning inline.
Sourcepub fn dispose_async_all(
&mut self,
resources: Vec<(Value, Value)>,
) -> JsResult<Value>
pub fn dispose_async_all( &mut self, resources: Vec<(Value, Value)>, ) -> JsResult<Value>
Dispose a list of (resource, disposeMethod) pairs, awaiting each disposer.
Returns the promise AsyncDisposableStack.disposeAsync hands back. The first
disposer runs synchronously — the reference starts it before returning, which
a test can observe by logging after the call — and the rest follow once their
predecessor settles.
Sourcepub fn iterator_result(&mut self, value: Value, done: bool) -> JsResult<Value>
pub fn iterator_result(&mut self, value: Value, done: bool) -> JsResult<Value>
The {value, done} object an iterator method answers with.
Sourcepub fn construct(
&mut self,
callee: Value,
new_target: Value,
args: Vec<Value>,
) -> JsResult<Value>
pub fn construct( &mut self, callee: Value, new_target: Value, args: Vec<Value>, ) -> JsResult<Value>
new callee(args).
A constructor differs from a plain call in three ways: it gets a fresh object
as this whose prototype is the constructor’s prototype property, its
return value is replaced by that object unless it returns an object itself,
and a native constructor is expected to initialise the object it is given
rather than build one.
Sourcepub fn make_error(&self, name: &str, message: &str) -> Value
pub fn make_error(&self, name: &str, message: &str) -> Value
A TypeError/ReferenceError/RangeError object, as the value to throw.
The error classes themselves arrive with the builtins milestone; what matters here is that the thrown value stringifies the way a script printing the caught value expects.
Sourcepub fn add_stack(&self, object: &Gc<JsObject>)
pub fn add_stack(&self, object: &Gc<JsObject>)
Give an error object its stack property, from the current frame stack.
Only objects get one — throw 42 has no stack anywhere — and an object that
already has a stack keeps it, so an error caught and rethrown, or one whose
stack a script set, is not rewritten.
Sourcepub fn add_stack_skipping(&self, object: &Gc<JsObject>, skip: usize)
pub fn add_stack_skipping(&self, object: &Gc<JsObject>, skip: usize)
Interp::add_stack, omitting the skip innermost frames.
An Error constructor passes 1: the reference builds those traces with
JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL, so new Error() does not report the
constructor itself, while a RangeError raised inside toISOString does
report at toISOString (native).
Sourcepub fn current_callee(&self) -> Option<Gc<JsObject>>
pub fn current_callee(&self) -> Option<Gc<JsObject>>
The function object currently being called, if any.
A native function is a bare fn pointer, so it cannot capture which class
it belongs to; it reads that from the callee instead. This is also what a
stack trace would use.
Sourcepub fn prototype_of(&self, constructor: &Gc<JsObject>) -> Gc<JsObject>
pub fn prototype_of(&self, constructor: &Gc<JsObject>) -> Gc<JsObject>
The prototype property of a constructor, falling back to
Object.prototype for anything that is not shaped like one.
Sourcepub fn type_error(&self, message: impl AsRef<str>) -> Signal
pub fn type_error(&self, message: impl AsRef<str>) -> Signal
Throw a TypeError carrying message.
Sourcepub fn range_error(&self, message: impl AsRef<str>) -> Signal
pub fn range_error(&self, message: impl AsRef<str>) -> Signal
Throw a RangeError carrying message.
Sourcepub fn to_primitive(&mut self, value: Value, hint: Hint) -> JsResult<Value>
pub fn to_primitive(&mut self, value: Value, hint: Hint) -> JsResult<Value>
ToPrimitive, which is what makes "" + obj produce obj.toString().
The order depends on the hint: the “number” order tries valueOf first and
toString second, the “string” order the other way round, and the default
hint uses the number order. A result that is still an object is a TypeError,
because no built-in valueOf/toString returns one.
Sourcepub fn to_js_string(&mut self, value: &Value) -> JsResult<String>
pub fn to_js_string(&mut self, value: &Value) -> JsResult<String>
ToString with the object case going through ToPrimitive.
Sourcepub fn checked_to_wtf8(&mut self, value: &Value) -> JsResult<Wtf8>
pub fn checked_to_wtf8(&mut self, value: &Value) -> JsResult<Wtf8>
ToString that keeps the string’s code units instead of flattening them.
This is what + needs: String.fromCharCode(0xD800) + "a" must keep the unpaired
surrogate, and a lossy String in between would turn it into U+FFFD.
Sourcepub fn to_js_number(&mut self, value: &Value) -> JsResult<f64>
pub fn to_js_number(&mut self, value: &Value) -> JsResult<f64>
ToNumber with the object case going through ToPrimitive.
Sourcepub fn read_field(&mut self, object: &Value, key: &str) -> JsResult<Value>
pub fn read_field(&mut self, object: &Value, key: &str) -> JsResult<Value>
Read object[key], returning undefined when the property is missing.
Unlike Interp::get_property this takes a runtime Value, so it works on a
primitive (looking through its prototype) as well as an object — which is what
get_field/get_array_el need and what a builtin reading a property off an
argument needs too.
Sourcepub fn read_field_key(
&mut self,
object: &Value,
key: &PropertyKey,
) -> JsResult<Value>
pub fn read_field_key( &mut self, object: &Value, key: &PropertyKey, ) -> JsResult<Value>
Interp::read_field with a key that may be a symbol.
A symbol key never reaches the string shortcuts: a string’s length and its
indices are string properties, and a symbol named length is a different
property that a string primitive cannot have.
Sourcepub fn checked_to_string(&mut self, value: &Value) -> JsResult<String>
pub fn checked_to_string(&mut self, value: &Value) -> JsResult<String>
ToString, as the spec defines it: a symbol is a TypeError.
Value::to_js_string cannot fail, so it answers the descriptive form for a
symbol. That is right for String(sym) and for error messages, and wrong for
everything the spec routes through ToString — "" + sym, ${sym},
Number.parseInt(sym) — so those callers use this instead.
Sourcepub fn property_key(&mut self, value: &Value) -> PropertyKey
pub fn property_key(&mut self, value: &Value) -> PropertyKey
ToPropertyKey for a builtin that takes a key argument.
Distinct from Interp::propkey, which is fn-private to the instruction
loop; the builtins reach this one.
Sourcepub fn key_value(&self, key: &PropertyKey) -> Value
pub fn key_value(&self, key: &PropertyKey) -> Value
A PropertyKey as the Value a script sees — what Reflect.ownKeys and
Object.getOwnPropertySymbols put in their result array.
Sourcepub fn object_prototype_of(
&mut self,
object: &Gc<JsObject>,
) -> JsResult<Option<Gc<JsObject>>>
pub fn object_prototype_of( &mut self, object: &Gc<JsObject>, ) -> JsResult<Option<Gc<JsObject>>>
[[GetPrototypeOf]], with the proxy trap when there is one.
Sourcepub fn object_set_prototype_of(
&mut self,
object: &Gc<JsObject>,
proto: Option<Gc<JsObject>>,
) -> JsResult<bool>
pub fn object_set_prototype_of( &mut self, object: &Gc<JsObject>, proto: Option<Gc<JsObject>>, ) -> JsResult<bool>
[[SetPrototypeOf]], with the proxy trap when there is one.
Sourcepub fn object_is_extensible(&mut self, object: &Gc<JsObject>) -> JsResult<bool>
pub fn object_is_extensible(&mut self, object: &Gc<JsObject>) -> JsResult<bool>
[[IsExtensible]], with the proxy trap when there is one.
Sourcepub fn object_prevent_extensions(
&mut self,
object: &Gc<JsObject>,
) -> JsResult<bool>
pub fn object_prevent_extensions( &mut self, object: &Gc<JsObject>, ) -> JsResult<bool>
[[PreventExtensions]], with the proxy trap when there is one.
Sourcepub fn describe_exception(&mut self, value: &Value) -> String
pub fn describe_exception(&mut self, value: &Value) -> String
How an uncaught exception is reported.
The reference reports a pending exception through JS_PrintValue, which ends
up calling the thrown value’s toString, so throw new TypeError("x") prints
TypeError: x. Reading name/message off the object directly got this wrong
two ways: name usually lives on the prototype rather than on the instance, and
a thrown non-error object was not consulted at all, coming out as
[object Object]. Routing through ToString keeps every case on one path and
honours a user-defined toString, as the reference does.
A toString that throws must not take the reporter down with it, so that case
falls back to the type-level rendering. The reference’s printer also walks into
plain objects (throw 1, {a:1} prints { a: 1 }); that formatting is a gap
shared with console.log.
Sourcepub fn stack_trace(&self) -> String
pub fn stack_trace(&self) -> String
The current call stack, formatted the way qjs prints error.stack.
One at name (file:line:col) line per active frame, innermost first. The
position is the one recorded for the instruction each frame is stopped on, which
is why the compiler builds a pc-to-position table per function: without it a
trace could only name the function, not the line inside it.
A frame whose code has no positions prints just (file), matching the
reference’s output for stripped bytecode.
Sourcepub fn declare_var_global(&mut self, name: &str)
pub fn declare_var_global(&mut self, name: &str)
Declare a global binding, creating its reference cell.
Declare a var or function global, whose home is the global object.
Writable and enumerable but not configurable: globalThis.v reads it,
for-in lists it, delete refuses. A name that is already there — a built-in
such as print, or a redeclaration — is left alone.
Sourcepub fn declare_global(&mut self, name: &str, is_const: bool)
pub fn declare_global(&mut self, name: &str, is_const: bool)
Declare a lexical global binding, which starts in its dead zone.
Only let/const/class need this: a var global is a property of the
global object and is created when its initialiser runs.