diff --git a/Userland/Libraries/LibJS/Bytecode/Executable.cpp b/Userland/Libraries/LibJS/Bytecode/Executable.cpp index de6b4315d3..ed2288d3fa 100644 --- a/Userland/Libraries/LibJS/Bytecode/Executable.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Executable.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace JS::Bytecode { @@ -52,4 +53,13 @@ void Executable::dump() const } } +JIT::NativeExecutable const* Executable::get_or_create_native_executable() +{ + if (!m_did_try_jitting) { + m_did_try_jitting = true; + m_native_executable = JIT::Compiler::compile(*this); + } + return m_native_executable; +} + } diff --git a/Userland/Libraries/LibJS/Bytecode/Executable.h b/Userland/Libraries/LibJS/Bytecode/Executable.h index 5587f9f0cb..27f48d047c 100644 --- a/Userland/Libraries/LibJS/Bytecode/Executable.h +++ b/Userland/Libraries/LibJS/Bytecode/Executable.h @@ -8,10 +8,12 @@ #include #include +#include #include #include #include #include +#include #include namespace JS::Bytecode { @@ -63,6 +65,12 @@ public: DeprecatedFlyString const& get_identifier(IdentifierTableIndex index) const { return identifier_table->get(index); } void dump() const; + + JIT::NativeExecutable const* get_or_create_native_executable(); + +private: + OwnPtr m_native_executable; + bool m_did_try_jitting { false }; }; } diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 73dce5e60b..bb94c023ae 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -351,7 +351,7 @@ Interpreter::ValueAndFrame Interpreter::run_and_return_frame(Executable& executa else push_call_frame(make(), executable.number_of_registers); - if (auto native_executable = JIT::Compiler::compile(executable)) { + if (auto native_executable = executable.get_or_create_native_executable()) { native_executable->run(vm()); for (size_t i = 0; i < vm().running_execution_context().local_variables.size(); ++i) { diff --git a/Userland/Libraries/LibJS/JIT/NativeExecutable.cpp b/Userland/Libraries/LibJS/JIT/NativeExecutable.cpp index d9a2a913ae..af71d1c151 100644 --- a/Userland/Libraries/LibJS/JIT/NativeExecutable.cpp +++ b/Userland/Libraries/LibJS/JIT/NativeExecutable.cpp @@ -22,7 +22,7 @@ NativeExecutable::~NativeExecutable() munmap(m_code, m_size); } -void NativeExecutable::run(VM& vm) +void NativeExecutable::run(VM& vm) const { typedef void (*JITCode)(VM&, Value* registers, Value* locals); ((JITCode)m_code)(vm, diff --git a/Userland/Libraries/LibJS/JIT/NativeExecutable.h b/Userland/Libraries/LibJS/JIT/NativeExecutable.h index dc90d50c64..332a2a7054 100644 --- a/Userland/Libraries/LibJS/JIT/NativeExecutable.h +++ b/Userland/Libraries/LibJS/JIT/NativeExecutable.h @@ -20,7 +20,7 @@ public: NativeExecutable(void* code, size_t size); ~NativeExecutable(); - void run(VM&); + void run(VM&) const; private: void* m_code { nullptr };