diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index 9a5ca84aaf..342fbc96ac 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -535,7 +535,7 @@ Value VariableDeclaration::execute(Interpreter& interpreter) const interpreter.declare_variable(name().string(), m_declaration_type); if (m_initializer) { auto initalizer_result = m_initializer->execute(interpreter); - interpreter.set_variable(name().string(), initalizer_result); + interpreter.set_variable(name().string(), initalizer_result, true); } return js_undefined(); diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 1f5c6d1e1c..5ca89f28f2 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -108,14 +108,14 @@ void Interpreter::declare_variable(String name, DeclarationType declaration_type } } -void Interpreter::set_variable(String name, Value value) +void Interpreter::set_variable(String name, Value value, bool first_assignment) { for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) { auto& scope = m_scope_stack.at(i); auto possible_match = scope.variables.get(name); if (possible_match.has_value()) { - if (possible_match.value().declaration_type == DeclarationType::Const) + if (!first_assignment && possible_match.value().declaration_type == DeclarationType::Const) ASSERT_NOT_REACHED(); scope.variables.set(move(name), { move(value), possible_match.value().declaration_type }); diff --git a/Libraries/LibJS/Interpreter.h b/Libraries/LibJS/Interpreter.h index abad03da81..a3c45a831f 100644 --- a/Libraries/LibJS/Interpreter.h +++ b/Libraries/LibJS/Interpreter.h @@ -71,7 +71,7 @@ public: void do_return(); Value get_variable(const String& name); - void set_variable(String name, Value); + void set_variable(String name, Value, bool first_assignment = false); void declare_variable(String name, DeclarationType); void gather_roots(Badge, HashTable&);