1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 03:58:12 +00:00

LibJS: Do not assume that a call frame exists in {get,set}_variable

This commit is contained in:
AnotherTest 2020-04-19 18:42:41 +04:30 committed by Andreas Kling
parent 69566bd4d6
commit 992467cca3

View file

@ -133,6 +133,7 @@ void Interpreter::exit_scope(const ScopeNode& scope_node)
void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
{
if (m_call_stack.size()) {
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
auto possible_match = environment->get(name);
if (possible_match.has_value()) {
@ -145,17 +146,20 @@ void Interpreter::set_variable(const FlyString& name, Value value, bool first_as
return;
}
}
}
global_object().put(move(name), move(value));
}
Optional<Value> Interpreter::get_variable(const FlyString& name)
{
if (m_call_stack.size()) {
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
auto possible_match = environment->get(name);
if (possible_match.has_value())
return possible_match.value().value;
}
}
return global_object().get(name);
}