From a517bf27210fdea4e2be905dcb7d7879fdcfcaa5 Mon Sep 17 00:00:00 2001 From: 54k1 <54k1@protonmail.com> Date: Tue, 29 Dec 2020 21:24:28 +0530 Subject: [PATCH 1/2] Modify environment binding behaviour of function --- .../ast/node/declaration/function_decl/mod.rs | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/boa/src/syntax/ast/node/declaration/function_decl/mod.rs b/boa/src/syntax/ast/node/declaration/function_decl/mod.rs index 5838cf49615..a292fbcbcf7 100644 --- a/boa/src/syntax/ast/node/declaration/function_decl/mod.rs +++ b/boa/src/syntax/ast/node/declaration/function_decl/mod.rs @@ -94,17 +94,23 @@ impl Executable for FunctionDecl { // Set the name and assign it in the current environment val.set_field("name", self.name()); - context - .realm_mut() - .environment - .create_mutable_binding(self.name().to_owned(), false, VariableScope::Function) - .map_err(|e| e.to_error(context))?; - - context - .realm_mut() - .environment - .initialize_binding(self.name(), val) - .map_err(|e| e.to_error(context))?; + + let environment = &mut context.realm_mut().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) + .map_err(|e| e.to_error(context))?; + + context + .realm_mut() + .environment + .initialize_binding(self.name(), val) + .map_err(|e| e.to_error(context))?; + } Ok(Value::undefined()) } From 5b5d83516c2a84de6c689dd409bd26dc3c4bcfff Mon Sep 17 00:00:00 2001 From: 54k1 <54k1@protonmail.com> Date: Sun, 3 Jan 2021 09:56:14 +0530 Subject: [PATCH 2/2] Add basic test --- boa/src/syntax/ast/node/declaration/mod.rs | 3 +++ boa/src/syntax/ast/node/declaration/tests.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 boa/src/syntax/ast/node/declaration/tests.rs diff --git a/boa/src/syntax/ast/node/declaration/mod.rs b/boa/src/syntax/ast/node/declaration/mod.rs index a256a75e87f..ac5a0dbbb92 100644 --- a/boa/src/syntax/ast/node/declaration/mod.rs +++ b/boa/src/syntax/ast/node/declaration/mod.rs @@ -19,3 +19,6 @@ pub use self::{ let_decl_list::{LetDecl, LetDeclList}, var_decl_list::{VarDecl, VarDeclList}, }; + +#[cfg(test)] +mod tests; diff --git a/boa/src/syntax/ast/node/declaration/tests.rs b/boa/src/syntax/ast/node/declaration/tests.rs new file mode 100644 index 00000000000..a98ac86f523 --- /dev/null +++ b/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"); +}