1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:08:12 +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));
}
}

View file

@ -23,5 +23,6 @@ ThrowCompletionOr<Value> typeof_variable(VM&, DeprecatedFlyString const&);
ThrowCompletionOr<void> set_variable(VM&, DeprecatedFlyString const&, Value, Op::EnvironmentMode, Op::SetVariable::InitializationMode);
Value new_function(VM&, FunctionExpression const&, Optional<IdentifierTableIndex> const& lhs_name, Optional<Register> const& home_object);
ThrowCompletionOr<void> put_by_value(VM&, Value base, Value property_key_value, Value value, Op::PropertyKind);
ThrowCompletionOr<Value> get_variable(Bytecode::Interpreter&, DeprecatedFlyString const& name, u32 cache_index);
}

View file

@ -780,27 +780,7 @@ ThrowCompletionOr<void> ConcatString::execute_impl(Bytecode::Interpreter& interp
ThrowCompletionOr<void> GetVariable::execute_impl(Bytecode::Interpreter& interpreter) const
{
auto& vm = interpreter.vm();
auto& cached_environment_coordinate = interpreter.current_executable().environment_variable_caches[m_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()) {
interpreter.accumulator() = TRY(verify_cast<DeclarativeEnvironment>(*environment).get_binding_value_direct(vm, cached_environment_coordinate.value().index, vm.in_strict_mode()));
return {};
}
cached_environment_coordinate = {};
}
auto const& string = interpreter.current_executable().get_identifier(m_identifier);
auto reference = TRY(vm.resolve_binding(string));
if (reference.environment_coordinate().has_value())
cached_environment_coordinate = reference.environment_coordinate();
interpreter.accumulator() = TRY(reference.get_value(vm));
interpreter.accumulator() = TRY(get_variable(interpreter, interpreter.current_executable().get_identifier(m_identifier), m_cache_index));
return {};
}

View file

@ -656,6 +656,24 @@ void Compiler::compile_get_global(Bytecode::Op::GetGlobal const& op)
check_exception();
}
static Value cxx_get_variable(VM& vm, DeprecatedFlyString const& name, u32 cache_index)
{
return TRY_OR_SET_EXCEPTION(Bytecode::get_variable(vm.bytecode_interpreter(), name, cache_index));
}
void Compiler::compile_get_variable(Bytecode::Op::GetVariable const& op)
{
m_assembler.mov(
Assembler::Operand::Register(ARG1),
Assembler::Operand::Imm64(bit_cast<u64>(&m_bytecode_executable.get_identifier(op.identifier()))));
m_assembler.mov(
Assembler::Operand::Register(ARG2),
Assembler::Operand::Imm64(op.cache_index()));
m_assembler.native_call((void*)cxx_get_variable);
store_vm_register(Bytecode::Register::accumulator(), RET);
check_exception();
}
static Value cxx_to_numeric(VM& vm, Value value)
{
return TRY_OR_SET_EXCEPTION(value.to_numeric(vm));
@ -903,6 +921,9 @@ OwnPtr<NativeExecutable> Compiler::compile(Bytecode::Executable& bytecode_execut
case Bytecode::Instruction::Type::GetGlobal:
compiler.compile_get_global(static_cast<Bytecode::Op::GetGlobal const&>(op));
break;
case Bytecode::Instruction::Type::GetVariable:
compiler.compile_get_variable(static_cast<Bytecode::Op::GetVariable const&>(op));
break;
case Bytecode::Instruction::Type::PutById:
compiler.compile_put_by_id(static_cast<Bytecode::Op::PutById const&>(op));
break;

View file

@ -95,6 +95,7 @@ private:
void compile_get_by_id(Bytecode::Op::GetById const&);
void compile_get_by_value(Bytecode::Op::GetByValue const&);
void compile_get_global(Bytecode::Op::GetGlobal const&);
void compile_get_variable(Bytecode::Op::GetVariable const&);
void compile_put_by_id(Bytecode::Op::PutById const&);
void compile_put_by_value(Bytecode::Op::PutByValue const&);