pub struct Interp<'a> {
pub heap: &'a Heap,
pub realm: Realm,
pub output: String,
pub max_frames: usize,
pub modules: BTreeMap<String, Gc<ModuleRecord>>,
pub module_loader: Option<Box<dyn ModuleLoader>>,
pub main_module_name: Option<String>,
pub module_stack: Vec<String>,
/* 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.
modules: BTreeMap<String, Gc<ModuleRecord>>The module registry, keyed by canonical name.
module_loader: Option<Box<dyn ModuleLoader>>How the host turns a specifier into bytes.
main_module_name: Option<String>The module the host was asked to run, for import.meta.main.
module_stack: Vec<String>Module bodies currently running, innermost last.
A dynamic import() resolves its specifier relative to the module that ran it, and
this is the only place that knows which module that is.
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 new_iterator_prototype(
&mut self,
tag: &str,
next: NativeFn,
) -> Gc<JsObject>
pub fn new_iterator_prototype( &mut self, tag: &str, next: NativeFn, ) -> Gc<JsObject>
A prototype for a built-in iterator, inheriting from Iterator.prototype.
Every built-in iterator has one — %ArrayIteratorPrototype%,
%StringIteratorPrototype% and so on — and the whole point of the chain is that
such an iterator is an Iterator: [1,2].values().map(f) resolves map by
walking up to Iterator.prototype. These used to be plain objects hanging off
Object.prototype, so .map was undefined and Object.prototype.toString
said [object Object] instead of [object Array Iterator].
The prototype carries next, [Symbol.iterator] answering this, and the
@@toStringTag that names it. next is passed in because each iterator kind has
its own.
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 run_async_body(
&mut self,
code: Gc<CompiledCode>,
env: Gc<ClosureEnv>,
) -> JsResult<Value>
pub fn run_async_body( &mut self, code: Gc<CompiledCode>, env: Gc<ClosureEnv>, ) -> JsResult<Value>
The coroutine an async body runs in.
Generator-shaped and never exposed to the script: what makes await work is the
same frame-detach-and-resume the generator protocol uses, so this reuses it rather
than growing a second state machine. The difference is only in the ending — a
return fulfils the caller’s promise here instead of producing {value, done}.
Run code as an async function body, answering the promise it settles.
Used for a module with top-level await: its body is an async function whose
caller is the host, so there is no function object to pass through
Interp::new_async_function.
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_ctor(
&self,
name: impl Into<String>,
length: u32,
func: NativeFn,
) -> Gc<JsObject>
pub fn new_native_ctor( &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.
A native constructor that builds its own instance after validating its arguments.
new hands it this = undefined rather than a pre-made object, so any argument
check runs before new.target.prototype is read.
Sourcepub fn new_native_constructor(
&self,
name: impl Into<String>,
length: u32,
func: NativeFn,
) -> Gc<JsObject>
pub fn new_native_constructor( &self, name: impl Into<String>, length: u32, func: NativeFn, ) -> Gc<JsObject>
A native constructor that lets new build the instance first.
The ordinary case for a built-in constructor: new Array(3) has no argument
validation that must precede reading new.target.prototype, so the object can be
created before the implementation runs. Distinct from Interp::new_native,
which makes a method and therefore not a constructor at all.
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.
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 eval_builtin_source(&mut self, source: &str) -> JsResult<Value>
pub fn eval_builtin_source(&mut self, source: &str) -> JsResult<Value>
Evaluate code as a script body and return its completion value.
Compile and run source for the engine’s own use, returning its completion value.
This is how a builtin can be written in JavaScript: [crate::builtins::from_async]
needs to be a real async function, and the engine already runs those as coroutines,
so compiling the algorithm is cheaper than rebuilding a coroutine by hand.
Differences from Self::eval_in_global, both deliberate:
- It does not drain the microtask queue. Install runs while the realm is still
being built, and the source is an IIFE that only constructs a closure, so there
is nothing to drain — and draining here would run script-queued jobs in the
middle of
install. - It reports a syntax error as an
InternalErrorthrow rather than as the usualSyntaxErrorwith a position: a malformed builtin source is this crate’s bug, and it should not look like the user wrote something wrong.
Sourcepub fn run_script(&mut self, code: CompiledFunction) -> JsResult<Value>
pub fn run_script(&mut self, code: CompiledFunction) -> JsResult<Value>
Run an already-parsed script and drain the microtasks it queued.
The drain is what lets an async function’s body advance past its first await
before the host sees the result: nothing else pumps the job queue.
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 array_length_of(&mut self, value: &Value) -> JsResult<u32>
pub fn array_length_of(&mut self, value: &Value) -> JsResult<u32>
ToUint32 with ArraySetLength’s refusal, for an array’s length.
ToUint32 is ToNumber then the modulo, but an object that yields a fraction or
NaN falls through to ToString and is parsed as decimal digits. That is why
{toString: () => "2"} is a legal length while {valueOf: () => 2.5} is a
RangeError — and why this cannot be a plain ToNumber.
Sourcepub fn property_key_checked(&mut self, value: &Value) -> JsResult<PropertyKey>
pub fn property_key_checked(&mut self, value: &Value) -> JsResult<PropertyKey>
ToPropertyKey that can fail.
Interp::property_key swallows a throwing ToPrimitive and answers
"[object Object]", which is fine for a bytecode operand that has already been
coerced but wrong for Reflect.get, where a key whose toString throws has to
propagate that error (return-abrupt-from-property-key).
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 has_iterator(&mut self, value: &Value) -> JsResult<bool>
pub fn has_iterator(&mut self, value: &Value) -> JsResult<bool>
The iterator for for-of: the object and its next method.
An object that already carries a next method is used as it is — that is what
arr.values() returns — and only arrays and strings get the built-in
sequence iterator.
Whether value has a callable Symbol.iterator, i.e. GetMethod succeeds.
Callers that must choose between the iterable and the array-like protocol — the typed-array constructor is the main one — need to ask before committing to one.
Sourcepub fn collect_iterable(&mut self, value: &Value) -> JsResult<Vec<Value>>
pub fn collect_iterable(&mut self, value: &Value) -> JsResult<Vec<Value>>
Collect everything an iterable yields, closing it if the walk is abandoned.
The cap is a guard against a generator that never ends: an unbounded collect would be an out-of-memory abort rather than a catchable error.
Sourcepub fn new_ctor_instance(
&mut self,
default_proto: Gc<JsObject>,
) -> JsResult<Gc<JsObject>>
pub fn new_ctor_instance( &mut self, default_proto: Gc<JsObject>, ) -> JsResult<Gc<JsObject>>
OrdinaryCreateFromConstructor(new.target, default_proto).
A native constructor calls this once it has validated its arguments, which is the
order the spec uses for Promise, the typed arrays and DataView: a bad argument is
a TypeError before new.target.prototype is read, so a throwing getter there is
never reached. The prototype comes from new.target when it is an object, and from
default_proto otherwise.
Sourcepub fn current_native(&self) -> Option<Gc<JsObject>>
pub fn current_native(&self) -> Option<Gc<JsObject>>
Whether this call is a construction, i.e. new.target is defined.
A native constructor receives this = undefined (it builds its own instance), so it
cannot use “this is not an object” to tell Ctor() from new Ctor() — this can.
The native function currently executing, if any.
Sourcepub fn is_constructing(&self) -> bool
pub fn is_constructing(&self) -> bool
Whether this call is a construction, i.e. new.target is defined.
Sourcepub fn new_target_is_custom(&mut self, callee: &Gc<JsObject>) -> bool
pub fn new_target_is_custom(&mut self, callee: &Gc<JsObject>) -> bool
Whether the pending new.target needs its own prototype honoured by the constructor.
True whenever new.target is not the callee itself, or when it has no usable
prototype — the cases where reading the property is observable at all.
Sourcepub fn internal_slot(&mut self, name: &'static str) -> PropertyKey
pub fn internal_slot(&mut self, name: &'static str) -> PropertyKey
The symbol key for one of the engine’s own hidden slots.
Cached on the realm, so every object of a kind shares one key and a slot can be read back by name.
Sourcepub fn internal_get(
&self,
object: &Gc<JsObject>,
name: &'static str,
) -> Option<Value>
pub fn internal_get( &self, object: &Gc<JsObject>, name: &'static str, ) -> Option<Value>
Read a hidden slot, or None when the object has none of that name.
Sourcepub fn internal_set(
&mut self,
object: &Gc<JsObject>,
name: &'static str,
value: Value,
)
pub fn internal_set( &mut self, object: &Gc<JsObject>, name: &'static str, value: Value, )
Define a hidden slot.
Sourcepub fn iterable_values(&mut self, value: &Value) -> JsResult<Option<Vec<Value>>>
pub fn iterable_values(&mut self, value: &Value) -> JsResult<Option<Vec<Value>>>
Drain anything with a Symbol.iterator method, or answer None when the value is
not iterable.
Shared so that Array.from, the Promise combinators and for…of all agree on
what “iterable” means. A Map and a Set have no length, so an array-like-only
reader silently produced an empty array for both — Array.from(new Set([1,2]))
was [].
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.
Source§impl<'a> Interp<'a>
impl<'a> Interp<'a>
Sourcepub fn module_registry(&self) -> &BTreeMap<String, Gc<ModuleRecord>>
pub fn module_registry(&self) -> &BTreeMap<String, Gc<ModuleRecord>>
The module registry: one entry per canonical name, so each file is compiled once.
Sourcepub fn run_main_module(&mut self, specifier: &str) -> JsResult<Value>
pub fn run_main_module(&mut self, specifier: &str) -> JsResult<Value>
Evaluate specifier as the entry module, the way qjs -m file.js does.
Sourcepub fn load_module(
&mut self,
specifier: &str,
attributes: &[(String, String)],
referrer: &str,
) -> JsResult<Gc<ModuleRecord>>
pub fn load_module( &mut self, specifier: &str, attributes: &[(String, String)], referrer: &str, ) -> JsResult<Gc<ModuleRecord>>
Load and compile one module.
Sourcepub fn link_module(&mut self, module: &Gc<ModuleRecord>) -> JsResult<()>
pub fn link_module(&mut self, module: &Gc<ModuleRecord>) -> JsResult<()>
Resolve every import of module and validate its exports, depth-first.
Sourcepub fn resolve_export(
&mut self,
module: &Gc<ModuleRecord>,
name: &str,
) -> JsResult<Gc<Cell>>
pub fn resolve_export( &mut self, module: &Gc<ModuleRecord>, name: &str, ) -> JsResult<Gc<Cell>>
The cell behind name in module, following re-exports.
Sourcepub fn module_export_names(
&mut self,
module: &Gc<ModuleRecord>,
) -> JsResult<Vec<String>>
pub fn module_export_names( &mut self, module: &Gc<ModuleRecord>, ) -> JsResult<Vec<String>>
The names module exports, sorted the way §16.2.1.6 requires.
Sourcepub fn module_namespace(&mut self, module: &Gc<ModuleRecord>) -> JsResult<Value>
pub fn module_namespace(&mut self, module: &Gc<ModuleRecord>) -> JsResult<Value>
The namespace object of module, built on first use.
An ordinary object with one read-only getter per export, inserted in sorted order.
The getter reads the cell, which is what makes the binding live: export let x
followed by x = 1 in the exporting module is visible here without copying.
Sourcepub fn evaluate_module_body(
&mut self,
module: &Gc<ModuleRecord>,
) -> JsResult<()>
pub fn evaluate_module_body( &mut self, module: &Gc<ModuleRecord>, ) -> JsResult<()>
Run module’s body, evaluating its dependencies first.
Post-order, once each: a module already evaluated is skipped, which is what §16.2.1.5.2.2’s “evaluated or evaluating” guard amounts to, and what makes a cycle terminate instead of recursing.
Sourcepub fn dynamic_import(&mut self, specifier: Value) -> JsResult<Value>
pub fn dynamic_import(&mut self, specifier: Value) -> JsResult<Value>
import(specifier): load, link, evaluate — asynchronously.
The work happens in a job, because §13.3.10.1 makes dynamic import settle through
the promise machinery: import("x").then(f) must call f after the current
script finishes, never inside the import expression.