1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-23 18:15:06 +00:00

LibJS/JIT: Support the GetVariable bytecode instruction

This commit is contained in:
Andreas Kling 2023-10-27 13:37:29 +02:00
parent 17657d012f
commit dabaaabfc0
5 changed files with 47 additions and 21 deletions

View file

@ -304,4 +304,27 @@ ThrowCompletionOr<void> put_by_value(VM& vm, Value base, Value property_key_valu
return {};
}
ThrowCompletionOr<Value> get_variable(Bytecode::Interpreter& interpreter, DeprecatedFlyString const& name, u32 cache_index)
{
auto& vm = interpreter.vm();
auto& cached_environment_coordinate = interpreter.current_executable().environment_variable_caches[cache_index];
if (cached_environment_coordinate.has_value()) {
auto environment = vm.running_execution_context().lexical_environment;
for (size_t i = 0; i < cached_environment_coordinate->hops; ++i)
environment = environment->outer_environment();
VERIFY(environment);
VERIFY(environment->is_declarative_environment());
if (!environment->is_permanently_screwed_by_eval()) {
return TRY(verify_cast<DeclarativeEnvironment>(*environment).get_binding_value_direct(vm, cached_environment_coordinate.value().index, vm.in_strict_mode()));
}
cached_environment_coordinate = {};
}
auto reference = TRY(vm.resolve_binding(name));
if (reference.environment_coordinate().has_value())
cached_environment_coordinate = reference.environment_coordinate();
return TRY(reference.get_value(vm));
}
}