Skip to main content

Interp

Struct Interp 

Source
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 Heap

The heap everything is allocated on.

§realm: Realm

The realm.

§output: String

Text 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: usize

The most frames that may be live at once.

Implementations§

Source§

impl<'a> Interp<'a>

Source

pub fn new(heap: &'a Heap) -> Interp<'a>

Build an interpreter over heap, with a fresh realm.

Source

pub fn string(&self, text: impl Into<String>) -> Value

A new string value.

Source

pub fn new_object(&self, proto: Option<Gc<JsObject>>) -> Gc<JsObject>

A new ordinary object with the given prototype.

Source

pub fn new_array(&self, elements: Vec<Value>) -> Gc<JsObject>

A new array.

Source

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.

Source

pub fn new_bound( &self, target: Value, this: Value, args: Vec<Value>, ) -> Gc<JsObject>

A new bound function.

Source

pub fn new_cell(&self, value: Value) -> Gc<Cell>

A new reference cell.

Source

pub fn run_script(&mut self, code: CompiledFunction) -> JsResult<Value>

Evaluate code as a script body and return its completion value.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_property( &mut self, object: &Gc<JsObject>, key: &str, value: Value, ) -> JsResult<()>

Store object[key] = value, honouring setters and inherited properties.

Source

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.

Source

pub fn delete_property(&mut self, object: &Gc<JsObject>, key: &str) -> bool

Delete object[key].

Source

pub fn call_value( &mut self, callee: Value, this: Value, args: Vec<Value>, ) -> JsResult<Value>

Call callee with this and args.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn type_error(&self, message: impl AsRef<str>) -> Signal

Throw a TypeError carrying message.

Source

pub fn range_error(&self, message: impl AsRef<str>) -> Signal

Throw a RangeError carrying message.

Source

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.

Source

pub fn to_js_string(&mut self, value: &Value) -> JsResult<String>

ToString with the object case going through ToPrimitive.

Source

pub fn to_js_number(&mut self, value: &Value) -> JsResult<f64>

ToNumber with the object case going through ToPrimitive.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Interp<'a>

§

impl<'a> !Send for Interp<'a>

§

impl<'a> !Sync for Interp<'a>

§

impl<'a> !UnwindSafe for Interp<'a>

§

impl<'a> Forget for Interp<'a>

§

impl<'a> Freeze for Interp<'a>

§

impl<'a> Move for Interp<'a>

§

impl<'a> Unpin for Interp<'a>

§

impl<'a> UnsafeUnpin for Interp<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.