Browse Source

Implement setting global values (#93)

* Use builder pattern to create interpreter

* Add method to initialize global object

* Run rustfmt to fix CI build

* Run rustfmt beta to fix CI build
pull/92/head
Sophie Tauchert 5 years ago committed by Jason Williams
parent
commit
8af9945dde
  1. 53
      src/lib/exec.rs
  2. 3
      src/lib/js/value.rs
  3. 1
      src/lib/syntax/lexer.rs

53
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<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 {
/// https://tc39.es/ecma262/#sec-call
fn call(&mut self, f: &Value, v: &Value, arguments_list: Vec<Value>) -> ResultValue {

3
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);
}
}

1
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));
}
}

Loading…
Cancel
Save