mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 17:37:35 +00:00
LibJS: Do not assume that a call frame exists in {get,set}_variable
This commit is contained in:
parent
69566bd4d6
commit
992467cca3
1 changed files with 16 additions and 12 deletions
|
@ -133,16 +133,18 @@ void Interpreter::exit_scope(const ScopeNode& scope_node)
|
||||||
|
|
||||||
void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
|
void Interpreter::set_variable(const FlyString& name, Value value, bool first_assignment)
|
||||||
{
|
{
|
||||||
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
|
if (m_call_stack.size()) {
|
||||||
auto possible_match = environment->get(name);
|
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
|
||||||
if (possible_match.has_value()) {
|
auto possible_match = environment->get(name);
|
||||||
if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) {
|
if (possible_match.has_value()) {
|
||||||
throw_exception<TypeError>("Assignment to constant variable");
|
if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) {
|
||||||
|
throw_exception<TypeError>("Assignment to constant variable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
environment->set(name, { value, possible_match.value().declaration_kind });
|
||||||
return;
|
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<Value> Interpreter::get_variable(const FlyString& name)
|
Optional<Value> Interpreter::get_variable(const FlyString& name)
|
||||||
{
|
{
|
||||||
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
|
if (m_call_stack.size()) {
|
||||||
auto possible_match = environment->get(name);
|
for (auto* environment = current_environment(); environment; environment = environment->parent()) {
|
||||||
if (possible_match.has_value())
|
auto possible_match = environment->get(name);
|
||||||
return possible_match.value().value;
|
if (possible_match.has_value())
|
||||||
|
return possible_match.value().value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return global_object().get(name);
|
return global_object().get(name);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue