diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp index 571e00ac3d..6d43aee9b9 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.cpp @@ -304,4 +304,27 @@ ThrowCompletionOr put_by_value(VM& vm, Value base, Value property_key_valu return {}; } +ThrowCompletionOr 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(*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)); +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h index fdb7427fb8..6a4d33bd74 100644 --- a/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h +++ b/Userland/Libraries/LibJS/Bytecode/CommonImplementations.h @@ -23,5 +23,6 @@ ThrowCompletionOr typeof_variable(VM&, DeprecatedFlyString const&); ThrowCompletionOr set_variable(VM&, DeprecatedFlyString const&, Value, Op::EnvironmentMode, Op::SetVariable::InitializationMode); Value new_function(VM&, FunctionExpression const&, Optional const& lhs_name, Optional const& home_object); ThrowCompletionOr put_by_value(VM&, Value base, Value property_key_value, Value value, Op::PropertyKind); +ThrowCompletionOr get_variable(Bytecode::Interpreter&, DeprecatedFlyString const& name, u32 cache_index); } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 46b19adf38..8b3e35eb65 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -780,27 +780,7 @@ ThrowCompletionOr ConcatString::execute_impl(Bytecode::Interpreter& interp ThrowCompletionOr 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(*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 {}; } diff --git a/Userland/Libraries/LibJS/JIT/Compiler.cpp b/Userland/Libraries/LibJS/JIT/Compiler.cpp index f742d1915a..1fdbaac0ad 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.cpp +++ b/Userland/Libraries/LibJS/JIT/Compiler.cpp @@ -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(&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 Compiler::compile(Bytecode::Executable& bytecode_execut case Bytecode::Instruction::Type::GetGlobal: compiler.compile_get_global(static_cast(op)); break; + case Bytecode::Instruction::Type::GetVariable: + compiler.compile_get_variable(static_cast(op)); + break; case Bytecode::Instruction::Type::PutById: compiler.compile_put_by_id(static_cast(op)); break; diff --git a/Userland/Libraries/LibJS/JIT/Compiler.h b/Userland/Libraries/LibJS/JIT/Compiler.h index edafd2797d..afd4ff74ca 100644 --- a/Userland/Libraries/LibJS/JIT/Compiler.h +++ b/Userland/Libraries/LibJS/JIT/Compiler.h @@ -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&);