//! Bytecode: the instructions a function, a script or eval code compiles to, and the verifier //! that checks them before they run. //! //! The interpreter is a stack machine. Each instruction takes its operands from the top of the //! frame's operand stack and pushes its result there; jumps name an instruction index. The //! verifier walks every path through the code and checks that each jump target, exception handler //! and `finally` dispatch target is an instruction, that each constant, name and function index is //! in its table, that the stack never goes below the frame's base, that every path reaching an //! instruction arrives with the same stack depth, and that no path runs past the last //! instruction. Code that fails is reported as an internal error and never runs. use std::rc::Rc; use crate::LineTable; use crate::ast::BinaryOp; use crate::heap::Key; use crate::value::Str; #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) enum Op { Undefined, Null, True, False, /// Pushes `numbers[i]`. Number(u32), /// Pushes `strings[i]`. String(u32), This, Pop, /// Removes the `n` values under the top one. PopUnder(u32), Dup, /// `a b` → `a b a b`. Dup2, Swap, /// `a b c` → `b c a`. Rot3, /// Pushes the value of the variable `names[i]`. GetVar(u32), /// Pushes the value of the variable and the `this` a call to it gets. GetVarThis(u32), /// `value` → `value`, assigned to the variable. SetVar(u32), /// Pushes the base of the reference to the variable `names[i]`, resolved before the /// right-hand side of an assignment runs. RefVar(u32), /// `base` → `value`: the variable's value read through the resolved base. GetRef(u32), /// `base value` → `value`, assigned to the variable through the resolved base. PutRef(u32), TypeofVar(u32), DeleteVar(u32), IncVar { name: u32, prefix: bool, dec: bool, }, /// `object` → `object.name`. GetMember(u32), /// `object` → `object.name object`. GetMemberThis(u32), /// `object value` → `value`. SetMember(u32), DeleteMember(u32), IncMember { name: u32, prefix: bool, dec: bool, }, /// `object key` → `object name`: checks that the object can have properties and turns the key /// into a property name, before the right-hand side of an assignment runs. ToKey, /// `object` → `object`: throws a `TypeError` when it is `undefined` or `null`, before the /// right-hand side of an assignment to one of its properties runs. CheckObject, /// `object key` → `object[key]`. GetIndex, /// `object key` → `object[key] object`. GetIndexThis, /// `object key value` → `value`. SetIndex, DeleteIndex, IncIndex { prefix: bool, dec: bool, }, /// `function this arg…` → result. Call(u32), /// Like `Call`, but a direct eval when the function is the global `eval`. CallEval(u32), /// `function arg…` → the new object. New(u32), Binary(BinaryOp), Neg, Plus, BitNot, Not, Typeof, ToNumber, Jump(u32), /// Pops the top value and jumps when it is falsy. JumpIfFalse(u32), JumpIfTrue(u32), /// Jumps, keeping the top value, when it is falsy; otherwise pops it. JumpIfFalseKeep(u32), JumpIfTrueKeep(u32), /// Jumps, keeping the top value, when it is neither `undefined` nor `null`; otherwise pops /// it (`??`). JumpIfNotNullishKeep(u32), /// When the top value is `undefined` or `null`, pops `drop` values (the top one among them) /// and jumps; otherwise goes on with the stack as it was (a step of an optional chain). JumpIfNullish { target: u32, drop: u32, }, Return, /// Returns the value of the last expression statement (script and eval code). ReturnCompletion, Throw, /// Throws a `ReferenceError` with the message `strings[i]` (an assignment to something that /// cannot be assigned to). ThrowReferenceError(u32), /// Ends a `finally` block: `value kind` on the stack say how it was entered. Kind 0 (normal) /// pops both and goes on, kind 1 throws the value, and kind `2 + k` jumps to entry `k` of /// `finally_tables[i]` with the value left on the stack. EndFinally(u32), NewObject, NewArray, /// `array value` → `array`, the value appended. ArrayPush, /// `array` → `array`, one longer. ArrayHole, /// `object value` → `object`. InitProp(u32), /// `object function` → `object`. InitGetter(u32), InitSetter(u32), /// Pushes a new function made from `functions[i]` and the current environment. Closure(u32), /// Pushes a new regular expression object for `regexps[i]`. RegExp(u32), /// `object` → nothing: enters a `with` statement's environment. PushWith, /// `exception` → nothing: enters a catch clause's environment, binding `names[i]`. PushCatch(u32), PopEnv, /// `object` → `iterator`. ForInStart, /// `iterator` → `iterator name`, or jumps with `iterator` when the names are used up. ForInNext(u32), /// Pops the value of an expression statement into the completion value. SetCompletion, /// Pushes the completion value, which a `finally` block in script and eval code keeps and /// sets back with `SetCompletion` when it ends normally. GetCompletion, /// `[qualifier] name` → an E4X name object. XmlMakeName { attribute: bool, qualified: bool, }, /// `object name` → the descendants that match. XmlDescendants, /// `value` → the string an embedded expression puts in a literal (0 content, 1 attribute /// value, 2 name, 3 run of attributes). XmlEscape(u8), /// The `parts` strings → the XML or XMLList they spell. XmlLiteral { parts: u32, list: bool, }, /// `name` → the value of the name on the nearest XML value in scope that holds it. XmlScopeGet, /// `name` → `object name`: the nearest XML value in scope that holds the name, for an /// assignment, update or `delete`. XmlScopeRef, /// `object` → the filter's state. XmlFilterStart, /// `state` → `state item`, or jumps with `state` when the items are used up. XmlFilterNext(u32), /// `state value` → `state`, the current item kept when the value is truthy. XmlFilterTest, /// `state` → the XMLList of the items kept. XmlFilterEnd, /// `iterator name x₁…xₙ` → `iterator name x₁…xₙ value`: the value under the name a `for each` /// got, read after the `n` values its target pushed. ForEachValue(u32), /// `value` → nothing: sets the default XML namespace of the running code. SetDefaultNamespace, /// Enters a block's environment: the `let` and `const` names of `scopes[i]`, not yet /// initialized, and its function declarations. PushBlock(u32), /// Replaces the current environment with a copy of it, for a `for` loop's next iteration. CopyEnv, /// `value` → nothing: initializes the nearest binding of `names[i]`, a `let`, a `const` or /// a parameter of a function with default or rest parameters. InitVar(u32), /// Pushes the value of the parameter `names[i]` of the running function, initialized or not. LoadParam(u32), /// Ends the parameters of a function with default or rest parameters: enters the environment /// of its body's variables and function declarations, and of its `let` and `const` names. BodyEnv, /// `value` → nothing: assigns the function declared in a block outside strict mode code to /// the variable `names[i]` of the running code (ECMA-262 2015 Annex B.3.3). AnnexB(u32), /// `array value` → `array`, with the elements of the value appended (spread). ArraySpread, /// `function this array` → result: a call with the array's elements as its arguments, a /// direct eval when `eval` is true and the function is the global `eval`. CallSpread(bool), /// `function array` → the new object, constructed with the array's elements as arguments. NewSpread, /// `value` → its string (a template literal's substitution). ToStr, /// Pushes the template object of `templates[i]`, the same object for each evaluation. TemplateObject(u32), } /// The names a block declares, entered with [`Op::PushBlock`]. #[derive(Debug, Default)] pub(crate) struct BlockScope { /// The `let` and `const` names, each with whether it is a `const`. pub(crate) lets: Vec<(Key, bool)>, /// The function declarations: their names and indices in `functions`. pub(crate) funcs: Vec<(Key, u32)>, } /// A tagged template's strings: as read and as written. #[derive(Debug)] pub(crate) struct Template { pub(crate) cooked: Vec, pub(crate) raw: Vec, } /// What code is: a function body, a script, or eval code. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum CodeKind { Function, Global, Eval, } /// A range of instructions an exception thrown in jumps out of: to `target`, with the operand /// stack cut back to `depth` values and the environments to `env_depth`, and the exception pushed. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct Handler { pub(crate) start: u32, pub(crate) end: u32, pub(crate) target: u32, pub(crate) depth: u32, pub(crate) env_depth: u32, } /// The source a piece of code came from, for error positions and `Function.prototype.toString`. #[derive(Debug)] pub(crate) struct Source { pub(crate) lines: LineTable, pub(crate) text: Rc<[u16]>, /// Which source this is, as [`crate::Realm::error_source`] gives it. pub(crate) tag: u32, /// Writes to read-only and getter-only properties, and new properties on objects that are /// not extensible, throw in this source's code even where it is not strict mode code. pub(crate) strict_writes: bool, } #[derive(Debug)] pub(crate) struct Code { pub(crate) ops: Vec, /// The source offset each instruction comes from. pub(crate) positions: Vec, pub(crate) numbers: Vec, pub(crate) strings: Vec, pub(crate) names: Vec, pub(crate) functions: Vec>, pub(crate) regexps: Vec<(Str, Str)>, pub(crate) handlers: Vec, pub(crate) finally_tables: Vec>, /// The most values the code keeps on the operand stack, set by the verifier. pub(crate) max_stack: u32, pub(crate) strict: bool, /// A function expression's own name, bound inside it. pub(crate) self_name: Option, /// A function declaration's name, which `$.stack` shows. pub(crate) decl_name: Option, pub(crate) params: Vec, pub(crate) vars: Vec, /// Function declarations to instantiate on entry: their names and indices in `functions`. pub(crate) fun_decls: Vec<(Key, u32)>, /// The body refers to `arguments` or calls `eval`. pub(crate) uses_arguments: bool, pub(crate) source: Rc, /// Where the function's text starts and ends in the source. pub(crate) span: (u32, u32), /// The block scopes [`Op::PushBlock`] enters. pub(crate) scopes: Vec, /// The scope of the `let` and `const` names at the top of the body, script or eval code. pub(crate) top_scope: Option, /// The tagged templates [`Op::TemplateObject`] makes objects of. pub(crate) templates: Vec