mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-21 18:37:58 +03:00
LibJS: Fix assignment of const variable on declaration
We were previously assuming that we were reassigning a variable even when we were not, oops, my bad. :/
This commit is contained in:
parent
2a32330257
commit
7aad10d984
Notes:
sideshowbarker
2024-07-19 08:17:03 +09:00
Author: https://github.com/0xtechnobabble Commit: https://github.com/SerenityOS/serenity/commit/7aad10d9849 Pull-request: https://github.com/SerenityOS/serenity/pull/1470 Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/oriko1010
@ -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();
|
||||
|
@ -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 });
|
||||
|
@ -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<Heap>, HashTable<Cell*>&);
|
||||
|
Loading…
Reference in New Issue
Block a user