Browse Source

Modify environment binding behaviour of function (#1010)

* Modify environment binding behaviour of function

* Add basic test
pull/1049/head
54k1 4 years ago committed by GitHub
parent
commit
d9916fcc9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      boa/src/syntax/ast/node/declaration/function_decl/mod.rs
  2. 3
      boa/src/syntax/ast/node/declaration/mod.rs
  3. 12
      boa/src/syntax/ast/node/declaration/tests.rs

13
boa/src/syntax/ast/node/declaration/function_decl/mod.rs

@ -94,9 +94,14 @@ impl Executable for FunctionDecl {
// Set the name and assign it in the current environment // Set the name and assign it in the current environment
val.set_field("name", self.name(), context)?; val.set_field("name", self.name(), context)?;
context
.realm_mut() let environment = &mut context.realm_mut().environment;
.environment if environment.has_binding(self.name()) {
environment
.set_mutable_binding(self.name(), val, true)
.map_err(|e| e.to_error(context))?;
} else {
environment
.create_mutable_binding(self.name().to_owned(), false, VariableScope::Function) .create_mutable_binding(self.name().to_owned(), false, VariableScope::Function)
.map_err(|e| e.to_error(context))?; .map_err(|e| e.to_error(context))?;
@ -105,7 +110,7 @@ impl Executable for FunctionDecl {
.environment .environment
.initialize_binding(self.name(), val) .initialize_binding(self.name(), val)
.map_err(|e| e.to_error(context))?; .map_err(|e| e.to_error(context))?;
}
Ok(Value::undefined()) Ok(Value::undefined())
} }
} }

3
boa/src/syntax/ast/node/declaration/mod.rs

@ -19,3 +19,6 @@ pub use self::{
let_decl_list::{LetDecl, LetDeclList}, let_decl_list::{LetDecl, LetDeclList},
var_decl_list::{VarDecl, VarDeclList}, var_decl_list::{VarDecl, VarDeclList},
}; };
#[cfg(test)]
mod tests;

12
boa/src/syntax/ast/node/declaration/tests.rs

@ -0,0 +1,12 @@
use crate::exec;
#[test]
fn duplicate_function_name() {
let scenario = r#"
function f () {}
function f () {return 12;}
f()
"#;
assert_eq!(&exec(scenario), "12");
}
Loading…
Cancel
Save