From 4749c3a99b861ac18cf210dadb22dbdb9793b770 Mon Sep 17 00:00:00 2001 From: 0x7D2B <0x7D2B@protonmail.com> Date: Wed, 17 Mar 2021 17:20:27 +0000 Subject: [PATCH] Handle different declaration types --- boa/src/syntax/ast/node/declaration/mod.rs | 34 ++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/boa/src/syntax/ast/node/declaration/mod.rs b/boa/src/syntax/ast/node/declaration/mod.rs index 51ba04b3f32..f96f5d9b5fc 100644 --- a/boa/src/syntax/ast/node/declaration/mod.rs +++ b/boa/src/syntax/ast/node/declaration/mod.rs @@ -61,18 +61,28 @@ impl Executable for DeclarationList { (_, Some(init)) => init.run(context)?, (_, None) => Value::undefined(), }; - context - .realm_mut() - .environment - .create_mutable_binding( - decl.name().to_owned(), - false, - match &self.declaration_type { - Var => VariableScope::Function, - _ => VariableScope::Block, - }, - ) - .map_err(|e| e.to_error(context))?; + + let environment = &mut context.realm_mut().environment; + + if self.declaration_type == Var && environment.has_binding(decl.name()) { + environment + .set_mutable_binding(decl.name(), val, true) + .map_err(|e| e.to_error(context))?; + return Ok(Value::undefined()); + } + + match &self.declaration_type { + Const => environment + .create_immutable_binding(decl.name().to_owned(), false, VariableScope::Block) + .map_err(|e| e.to_error(context))?, + Let => environment + .create_mutable_binding(decl.name().to_owned(), false, VariableScope::Block) + .map_err(|e| e.to_error(context))?, + Var => environment + .create_mutable_binding(decl.name().to_owned(), false, VariableScope::Function) + .map_err(|e| e.to_error(context))?, + } + context .realm_mut() .environment