Skip to content

Commit

Permalink
Expose get_this_binding as an environment record trait method
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Oct 23, 2019
1 parent 8596079 commit 46c9af0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/lib/environment/environment_record_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! There are 5 Environment record kinds. They all have methods in common, these are implemented as a the `EnvironmentRecordTrait`
//!
use crate::{
builtins::value::Value,
builtins::value::{self, Value},
environment::lexical_environment::{Environment, EnvironmentType},
};
use gc::{Finalize, Trace};
Expand Down Expand Up @@ -60,6 +60,11 @@ pub trait EnvironmentRecordTrait: Debug + Trace + Finalize {
/// Return true if it does and false if it does not.
fn has_this_binding(&self) -> bool;

fn get_this_binding(&self) -> Value {
debug_assert!(self.has_this_binding());
value::undefined()
}

/// 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 src/lib/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 Down Expand Up @@ -221,6 +206,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 with_base_object(&self) -> Value {
Gc::new(ValueData::Undefined)
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/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 @@ -166,6 +162,10 @@ impl EnvironmentRecordTrait for GlobalEnvironmentRecord {
true
}

fn get_this_binding(&self) -> Value {
self.global_this_binding.clone()
}

fn has_super_binding(&self) -> bool {
false
}
Expand Down

0 comments on commit 46c9af0

Please sign in to comment.