diff --git a/module/src/CScriptRuntimeInfo.h b/module/src/CScriptRuntimeInfo.h new file mode 100644 index 0000000..aa66042 --- /dev/null +++ b/module/src/CScriptRuntimeInfo.h @@ -0,0 +1,34 @@ +#pragma once + +#include "v8.h" +#include + +class CScriptRuntimeInfo +{ +private: + v8::Isolate* isolate; + std::unique_ptr platform; + +public: + v8::Isolate* GetIsolate() { return isolate; } + + void Instanciate() + { + v8::V8::SetFlagsFromString("--harmony-import-assertions --short-builtin-calls --no-lazy --no-flush-bytecode --no-enable-lazy-source-positions"); + platform = v8::platform::NewDefaultPlatform(); + v8::V8::InitializePlatform(platform.get()); + + v8::V8::Initialize(); + + auto createParams = v8::Isolate::CreateParams{}; + createParams.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + + isolate = v8::Isolate::New(createParams); + } + + static CScriptRuntimeInfo& Instance() + { + static CScriptRuntimeInfo runtimeInfo; + return runtimeInfo; + } +}; diff --git a/module/src/main.cpp b/module/src/main.cpp index 13faa6e..2f9b293 100644 --- a/module/src/main.cpp +++ b/module/src/main.cpp @@ -1,3 +1,4 @@ +#include "CScriptRuntimeInfo.h" #include "SDK.h" #include "version/version.h" #include "Log.h" @@ -34,11 +35,10 @@ EXPORT bool altMain(alt::ICore* core) { alt::ICore::SetInstance(core); - auto& runtime = JSBytecodeRuntime::Instance(); - core->RegisterScriptRuntime("jsb", &runtime); + CScriptRuntimeInfo::Instance().Instanciate(); - auto& runtimeV2 = JSBytecodeRuntimeV2::Instance(); - core->RegisterScriptRuntime("jsv2b", &runtimeV2); + core->RegisterScriptRuntime("jsb", &JSBytecodeRuntime::Instance()); + core->RegisterScriptRuntime("jsv2b", &JSBytecodeRuntimeV2::Instance()); core->SubscribeCommand("jsb-module", &CommandHandler); diff --git a/module/src/runtime.cpp b/module/src/runtime.cpp index 8113453..189cc52 100644 --- a/module/src/runtime.cpp +++ b/module/src/runtime.cpp @@ -1,23 +1,13 @@ #include "runtime.h" #include "Log.h" #include "compiler.h" +#include "CScriptRuntimeInfo.h" #include "package.h" #include "logger.h" -JSBytecodeRuntime::JSBytecodeRuntime() -{ - v8::V8::SetFlagsFromString("--harmony-import-assertions --short-builtin-calls --no-lazy --no-flush-bytecode --no-enable-lazy-source-positions"); - platform = v8::platform::NewDefaultPlatform(); - v8::V8::InitializePlatform(platform.get()); - v8::V8::Initialize(); - - create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); - - isolate = v8::Isolate::New(create_params); -} - void JSBytecodeRuntime::ProcessClientFile(alt::IResource* resource, alt::IPackage* package) { + v8::Isolate* isolate = CScriptRuntimeInfo::Instance().GetIsolate(); v8::Isolate::Scope isolateScope(isolate); v8::HandleScope handleScope(isolate); diff --git a/module/src/runtime.h b/module/src/runtime.h index 7e234a1..6de9329 100644 --- a/module/src/runtime.h +++ b/module/src/runtime.h @@ -2,25 +2,13 @@ #include "SDK.h" #include "v8.h" -#include "libplatform/libplatform.h" class JSBytecodeRuntime : public alt::IScriptRuntime { - v8::Isolate* isolate; - v8::Isolate::CreateParams create_params; - std::unique_ptr platform; - public: - JSBytecodeRuntime(); - bool GetProcessClientType(std::string& clientType) override; void ProcessClientFile(alt::IResource* resource, alt::IPackage* clientPackage) override; - v8::Isolate* GetIsolate() - { - return isolate; - } - alt::IResource::Impl* CreateImpl(alt::IResource* resource) override { return nullptr; diff --git a/module/src/runtimev2.cpp b/module/src/runtimev2.cpp index 03874b7..2742e94 100644 --- a/module/src/runtimev2.cpp +++ b/module/src/runtimev2.cpp @@ -1,23 +1,13 @@ #include "runtimev2.h" #include "Log.h" #include "compiler.h" +#include "CScriptRuntimeInfo.h" #include "package.h" #include "logger.h" -JSBytecodeRuntimeV2::JSBytecodeRuntimeV2() -{ - v8::V8::SetFlagsFromString("--harmony-import-assertions --short-builtin-calls --no-lazy --no-flush-bytecode --no-enable-lazy-source-positions"); - platform = v8::platform::NewDefaultPlatform(); - v8::V8::InitializePlatform(platform.get()); - v8::V8::Initialize(); - - create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); - - isolate = v8::Isolate::New(create_params); -} - void JSBytecodeRuntimeV2::ProcessClientFile(alt::IResource* resource, alt::IPackage* package) { + v8::Isolate* isolate = CScriptRuntimeInfo::Instance().GetIsolate(); v8::Isolate::Scope isolateScope(isolate); v8::HandleScope handleScope(isolate); diff --git a/module/src/runtimev2.h b/module/src/runtimev2.h index 0e4c4a0..bccb0e2 100644 --- a/module/src/runtimev2.h +++ b/module/src/runtimev2.h @@ -2,25 +2,13 @@ #include "SDK.h" #include "v8.h" -#include "libplatform/libplatform.h" class JSBytecodeRuntimeV2 : public alt::IScriptRuntime { - v8::Isolate* isolate; - v8::Isolate::CreateParams create_params; - std::unique_ptr platform; - public: - JSBytecodeRuntimeV2(); - bool GetProcessClientType(std::string& clientType) override; void ProcessClientFile(alt::IResource* resource, alt::IPackage* clientPackage) override; - v8::Isolate* GetIsolate() - { - return isolate; - } - alt::IResource::Impl* CreateImpl(alt::IResource* resource) override { return nullptr;