diff --git a/src/lib/exec.rs b/src/lib/exec.rs index d921e77be9..a09fb64f04 100644 --- a/src/lib/exec.rs +++ b/src/lib/exec.rs @@ -32,19 +32,18 @@ pub struct Interpreter { environment: LexicalEnvironment, } +/// Builder for the [`Interpreter`] +/// +/// [`Interpreter`]: struct.Interpreter.html +#[derive(Debug)] +pub struct InterpreterBuilder { + /// The global object + global: Value, +} + impl Executor for Interpreter { 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 { - environment: LexicalEnvironment::new(global.clone()), - } + InterpreterBuilder::new().build() } #[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(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 { /// https://tc39.es/ecma262/#sec-call fn call(&mut self, f: &Value, v: &Value, arguments_list: Vec) -> ResultValue { diff --git a/src/lib/js/mod.rs b/src/lib/js/mod.rs index 0895cdc6a7..06b3e33be3 100644 --- a/src/lib/js/mod.rs +++ b/src/lib/js/mod.rs @@ -17,4 +17,4 @@ pub mod string; /// Javascript values, utility methods and conversion between Javascript values and Rust values pub mod value; // Property, used by `Object` -pub mod property; \ No newline at end of file +pub mod property; diff --git a/src/lib/js/value.rs b/src/lib/js/value.rs index 6fff1f3be3..5078a7aab3 100644 --- a/src/lib/js/value.rs +++ b/src/lib/js/value.rs @@ -1,7 +1,7 @@ use crate::js::{ function::{Function, NativeFunction, NativeFunctionData}, object::{Object, ObjectKind, INSTANCE_PROTOTYPE, PROTOTYPE}, - property::Property + property::Property, }; use gc::{Gc, GcCell}; use gc_derive::{Finalize, Trace}; @@ -853,5 +853,4 @@ mod tests { assert_eq!((-1.0).to_value().is_true(), true); assert_eq!(NAN.to_value().is_true(), false); } - } diff --git a/src/lib/syntax/lexer.rs b/src/lib/syntax/lexer.rs index 56663b4322..141b1c2cb9 100644 --- a/src/lib/syntax/lexer.rs +++ b/src/lib/syntax/lexer.rs @@ -894,5 +894,4 @@ mod tests { assert_eq!(lexer.tokens[0].data, TokenData::NumericLiteral(1.0)); assert_eq!(lexer.tokens[1].data, TokenData::Punctuator(Punctuator::Dot)); } - }