From 992467cca3e4b0cb26a802b8078bb6575c1682ba Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Sun, 19 Apr 2020 18:42:41 +0430 Subject: [PATCH] LibJS: Do not assume that a call frame exists in {get,set}_variable --- Libraries/LibJS/Interpreter.cpp | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/Libraries/LibJS/Interpreter.cpp b/Libraries/LibJS/Interpreter.cpp index 3dcc3ec250c..8c740a73ab4 100644 --- a/Libraries/LibJS/Interpreter.cpp +++ b/Libraries/LibJS/Interpreter.cpp @@ -133,16 +133,18 @@ void Interpreter::exit_scope(const ScopeNode& scope_node) void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment) { - for (auto* environment = current_environment(); environment; environment = environment->parent()) { - auto possible_match = environment->get(name); - if (possible_match.has_value()) { - if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) { - throw_exception("Assignment to constant variable"); + 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()) { + if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) { + throw_exception("Assignment to constant variable"); + return; + } + + environment->set(name, { value, possible_match.value().declaration_kind }); return; } - - environment->set(name, { value, possible_match.value().declaration_kind }); - return; } } @@ -151,10 +153,12 @@ void Interpreter::set_variable(const FlyString& name, Value value, bool first_as Optional Interpreter::get_variable(const FlyString& name) { - 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; + 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); }