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 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_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 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 delete_property(&mut self, object: &Gc<JsObject>, key: &str) -> bool
pub fn delete_property(&mut self, object: &Gc<JsObject>, key: &str) -> bool
Delete object[key].
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 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 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 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 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.