1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:57:44 +00:00

LibJS: Support deleting local variables with operator delete

To make this cleaner i also moved the logic into Reference::delete_.
This commit is contained in:
Idan Horowitz 2021-06-08 04:00:53 +03:00 committed by Linus Groh
parent af58779def
commit 064ed8279e
14 changed files with 82 additions and 10 deletions

View file

@ -161,6 +161,29 @@ void VM::set_variable(const FlyString& name, Value value, GlobalObject& global_o
global_object.put(name, value);
}
bool VM::delete_variable(FlyString const& name)
{
ScopeObject* specific_scope = nullptr;
Optional<Variable> possible_match;
if (!m_call_stack.is_empty()) {
for (auto* scope = current_scope(); scope; scope = scope->parent()) {
possible_match = scope->get_from_scope(name);
if (possible_match.has_value()) {
specific_scope = scope;
break;
}
}
}
if (!possible_match.has_value())
return false;
if (possible_match.value().declaration_kind == DeclarationKind::Const)
return false;
VERIFY(specific_scope);
return specific_scope->delete_from_scope(name);
}
void VM::assign(const FlyString& target, Value value, GlobalObject& global_object, bool first_assignment, ScopeObject* specific_scope)
{
set_variable(target, move(value), global_object, first_assignment, specific_scope);