|
|
@ -32,19 +32,18 @@ pub struct Interpreter { |
|
|
|
environment: LexicalEnvironment, |
|
|
|
environment: LexicalEnvironment, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Builder for the [`Interpreter`]
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// [`Interpreter`]: struct.Interpreter.html
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
|
|
|
|
|
pub struct InterpreterBuilder { |
|
|
|
|
|
|
|
/// The global object
|
|
|
|
|
|
|
|
global: Value, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Executor for Interpreter { |
|
|
|
impl Executor for Interpreter { |
|
|
|
fn new() -> Self { |
|
|
|
fn new() -> Self { |
|
|
|
let global = ValueData::new_obj(None); |
|
|
|
InterpreterBuilder::new().build() |
|
|
|
object::init(&global); |
|
|
|
|
|
|
|
console::init(&global); |
|
|
|
|
|
|
|
math::init(&global); |
|
|
|
|
|
|
|
array::init(&global); |
|
|
|
|
|
|
|
function::init(&global); |
|
|
|
|
|
|
|
json::init(&global); |
|
|
|
|
|
|
|
string::init(&global); |
|
|
|
|
|
|
|
Self { |
|
|
|
|
|
|
|
environment: LexicalEnvironment::new(global.clone()), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[allow(clippy::match_same_arms)] |
|
|
|
#[allow(clippy::match_same_arms)] |
|
|
@ -376,6 +375,38 @@ impl Executor for Interpreter { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl InterpreterBuilder { |
|
|
|
|
|
|
|
pub fn new() -> Self { |
|
|
|
|
|
|
|
let global = ValueData::new_obj(None); |
|
|
|
|
|
|
|
object::init(&global); |
|
|
|
|
|
|
|
console::init(&global); |
|
|
|
|
|
|
|
math::init(&global); |
|
|
|
|
|
|
|
array::init(&global); |
|
|
|
|
|
|
|
function::init(&global); |
|
|
|
|
|
|
|
json::init(&global); |
|
|
|
|
|
|
|
string::init(&global); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Self { global } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn init_globals<F: FnOnce(&Value)>(self, init_fn: F) -> Self { |
|
|
|
|
|
|
|
init_fn(&self.global); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn build(self) -> Interpreter { |
|
|
|
|
|
|
|
Interpreter { |
|
|
|
|
|
|
|
environment: LexicalEnvironment::new(self.global.clone()), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for InterpreterBuilder { |
|
|
|
|
|
|
|
fn default() -> Self { |
|
|
|
|
|
|
|
Self::new() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Interpreter { |
|
|
|
impl Interpreter { |
|
|
|
/// https://tc39.es/ecma262/#sec-call
|
|
|
|
/// https://tc39.es/ecma262/#sec-call
|
|
|
|
fn call(&mut self, f: &Value, v: &Value, arguments_list: Vec<Value>) -> ResultValue { |
|
|
|
fn call(&mut self, f: &Value, v: &Value, arguments_list: Vec<Value>) -> ResultValue { |
|
|
|