Skip to content

Commit

Permalink
implement this
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilliams committed Apr 15, 2020
1 parent 505df91 commit 464e0b1
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 21 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions boa/src/environment/declarative_environment_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions boa/src/environment/environment_record_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 15 additions & 15 deletions boa/src/environment/function_environment_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions boa/src/environment/global_environment_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions boa/src/environment/lexical_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boa/src/environment/object_environment_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions boa/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down

0 comments on commit 464e0b1

Please sign in to comment.