diff --git a/Cargo.lock b/Cargo.lock index 97df8d2eee3..8912d759ae7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,7 +2,7 @@ # It is not intended for manual editing. [[package]] name = "Boa" -version = "0.6.0" +version = "0.7.0" dependencies = [ "criterion", "gc", @@ -57,7 +57,7 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "boa_cli" -version = "0.6.0" +version = "0.7.0" dependencies = [ "Boa", "structopt", diff --git a/boa/src/environment/declarative_environment_record.rs b/boa/src/environment/declarative_environment_record.rs index 553f720d92f..1be0d191c6f 100644 --- a/boa/src/environment/declarative_environment_record.rs +++ b/boa/src/environment/declarative_environment_record.rs @@ -150,6 +150,10 @@ impl EnvironmentRecordTrait for DeclarativeEnvironmentRecord { false } + fn get_this_binding(&self) -> Value { + Gc::new(ValueData::Undefined) + } + fn has_super_binding(&self) -> bool { false } diff --git a/boa/src/environment/environment_record_trait.rs b/boa/src/environment/environment_record_trait.rs index f2166289e1b..297ecb2215d 100644 --- a/boa/src/environment/environment_record_trait.rs +++ b/boa/src/environment/environment_record_trait.rs @@ -60,6 +60,9 @@ pub trait EnvironmentRecordTrait: Debug + Trace + Finalize { /// Return true if it does and false if it does not. fn has_this_binding(&self) -> bool; + /// Return the `this` binding from the environment + fn get_this_binding(&self) -> Value; + /// Determine if an Environment Record establishes a super method binding. /// Return true if it does and false if it does not. fn has_super_binding(&self) -> bool; diff --git a/boa/src/environment/function_environment_record.rs b/boa/src/environment/function_environment_record.rs index e18abcc327f..8d27918d319 100644 --- a/boa/src/environment/function_environment_record.rs +++ b/boa/src/environment/function_environment_record.rs @@ -75,21 +75,6 @@ impl FunctionEnvironmentRecord { } } } - - pub fn get_this_binding(&self) -> Value { - match self.this_binding_status { - BindingStatus::Lexical => { - // TODO: change this when error handling comes into play - panic!("There is no this for a lexical function record"); - } - BindingStatus::Uninitialized => { - // TODO: change this when error handling comes into play - panic!("Reference Error: Unitialised binding for this function"); - } - - BindingStatus::Initialized => self.this_value.clone(), - } - } } impl EnvironmentRecordTrait for FunctionEnvironmentRecord { @@ -116,6 +101,21 @@ impl EnvironmentRecordTrait for FunctionEnvironmentRecord { ); } + fn get_this_binding(&self) -> Value { + match self.this_binding_status { + BindingStatus::Lexical => { + // TODO: change this when error handling comes into play + panic!("There is no this for a lexical function record"); + } + BindingStatus::Uninitialized => { + // TODO: change this when error handling comes into play + panic!("Reference Error: Unitialised binding for this function"); + } + + BindingStatus::Initialized => self.this_value.clone(), + } + } + fn create_immutable_binding(&mut self, name: String, strict: bool) -> bool { if self.env_rec.contains_key(&name) { // TODO: change this when error handling comes into play diff --git a/boa/src/environment/global_environment_record.rs b/boa/src/environment/global_environment_record.rs index 372fef25698..38e3a643608 100644 --- a/boa/src/environment/global_environment_record.rs +++ b/boa/src/environment/global_environment_record.rs @@ -29,10 +29,6 @@ pub struct GlobalEnvironmentRecord { } impl GlobalEnvironmentRecord { - pub fn get_this_binding(&self) -> Value { - self.global_this_binding.clone() - } - pub fn has_var_declaration(&self, name: &str) -> bool { self.var_names.contains(name) } @@ -91,6 +87,10 @@ impl GlobalEnvironmentRecord { } impl EnvironmentRecordTrait for GlobalEnvironmentRecord { + fn get_this_binding(&self) -> Value { + self.global_this_binding.clone() + } + fn has_binding(&self, name: &str) -> bool { if self.declarative_record.has_binding(name) { return true; diff --git a/boa/src/environment/lexical_environment.rs b/boa/src/environment/lexical_environment.rs index 77e1840fca7..653cbf8e991 100644 --- a/boa/src/environment/lexical_environment.rs +++ b/boa/src/environment/lexical_environment.rs @@ -113,6 +113,11 @@ impl LexicalEnvironment { .get_global_object() } + pub fn get_this_binding(&self) -> Value { + let env = self.environment_stack.get(0).expect("").borrow(); + env.get_this_binding() + } + pub fn create_mutable_binding(&mut self, name: String, deletion: bool, scope: VariableScope) { match scope { VariableScope::Block => self diff --git a/boa/src/environment/object_environment_record.rs b/boa/src/environment/object_environment_record.rs index a17049f0191..f951c019eee 100644 --- a/boa/src/environment/object_environment_record.rs +++ b/boa/src/environment/object_environment_record.rs @@ -91,6 +91,10 @@ impl EnvironmentRecordTrait for ObjectEnvironmentRecord { false } + fn get_this_binding(&self) -> Value { + Gc::new(ValueData::Undefined) + } + fn has_super_binding(&self) -> bool { false } diff --git a/boa/src/exec/mod.rs b/boa/src/exec/mod.rs index aaf4b16496d..c8dc8dce138 100644 --- a/boa/src/exec/mod.rs +++ b/boa/src/exec/mod.rs @@ -569,6 +569,10 @@ impl Executor for Interpreter { // TODO: for now we can do nothing but return the value as-is Ok(Gc::new((*self.run(node)?).clone())) } + Node::This => { + // Will either return `this` binding or undefined + Ok(self.realm.environment.get_this_binding()) + } ref i => unimplemented!("{}", i), } }