From 3851f58c317341635a905544279ce59425d72e72 Mon Sep 17 00:00:00 2001 From: johnche Date: Mon, 8 Apr 2024 15:10:47 +0800 Subject: [PATCH] =?UTF-8?q?[unity]=E9=87=8D=E6=9E=84=EF=BC=8C=E4=B9=8B?= =?UTF-8?q?=E5=89=8DFreeIsolate=E5=92=8CCreateIsolate=E4=B8=8D=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=EF=BC=8C=E5=89=8D=E8=80=85=E5=A4=9A=E4=BA=86MainConte?= =?UTF-8?q?xt=E9=87=8A=E6=94=BE=EF=BC=8C=E4=BD=86MainContext=E5=8D=B4?= =?UTF-8?q?=E6=98=AF=E4=BB=8E=E5=8F=A6=E5=A4=96=E4=B8=80=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E4=BC=A0=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- unity/native_src/Inc/BackendEnv.h | 9 ++++----- unity/native_src/Src/BackendEnv.cpp | 27 ++++++++++++++++++++------- unity/native_src/Src/JSEngine.cpp | 21 +++++++++------------ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/unity/native_src/Inc/BackendEnv.h b/unity/native_src/Inc/BackendEnv.h index b49aa0e301..12001a51f2 100644 --- a/unity/native_src/Inc/BackendEnv.h +++ b/unity/native_src/Inc/BackendEnv.h @@ -29,12 +29,11 @@ namespace PUERTS_NAMESPACE { class BackendEnv { - private: + public: v8::Isolate* MainIsolate; v8::Global MainContext; - public: ~BackendEnv() { PathToModuleMap.clear(); ScriptIdToPathMap.clear(); @@ -108,11 +107,11 @@ namespace PUERTS_NAMESPACE } static void GlobalPrepare(); - v8::Isolate* CreateIsolate(void* external_quickjs_runtime); + void Initialize(void* external_quickjs_runtime, void* external_quickjs_context); - void FreeIsolate(); + void UnInitialize(); - void InitInject(v8::Isolate* Isolate, v8::Local Context); + void InitInject(); void CreateInspector(v8::Isolate* Isolate, const v8::Global* ContextGlobal, int32_t Port); diff --git a/unity/native_src/Src/BackendEnv.cpp b/unity/native_src/Src/BackendEnv.cpp index 05ed6aae60..d43fd6dd59 100644 --- a/unity/native_src/Src/BackendEnv.cpp +++ b/unity/native_src/Src/BackendEnv.cpp @@ -228,7 +228,7 @@ void BackendEnv::GlobalPrepare() } } -v8::Isolate* BackendEnv::CreateIsolate(void* external_quickjs_runtime) +void BackendEnv::Initialize(void* external_quickjs_runtime, void* external_quickjs_context) { #if defined(WITH_NODEJS) const int Ret = uv_loop_init(&NodeUVLoop); @@ -254,16 +254,28 @@ v8::Isolate* BackendEnv::CreateIsolate(void* external_quickjs_runtime) CreateParams->array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); #if WITH_QUICKJS - MainIsolate = (external_quickjs_runtime == nullptr) ? v8::Isolate::New(*CreateParams) : v8::Isolate::New(external_quickjs_runtime); + MainIsolate = (external_quickjs_runtime == nullptr) ? v8::Isolate::New(*CreateParams) : v8::Isolate::New(external_quickjs_runtime); #else - MainIsolate = v8::Isolate::New(*CreateParams); + MainIsolate = v8::Isolate::New(*CreateParams); #endif #endif - return MainIsolate; + auto Isolate = MainIsolate; +#ifdef THREAD_SAFE + v8::Locker Locker(Isolate); +#endif + v8::Isolate::Scope Isolatescope(Isolate); + v8::HandleScope HandleScope(Isolate); + +#if WITH_QUICKJS + v8::Local Context = (external_quickjs_runtime && external_quickjs_context) ? v8::Context::New(Isolate, external_quickjs_context) : v8::Context::New(Isolate); +#else + v8::Local Context = v8::Context::New(Isolate); +#endif + MainContext.Reset(Isolate, Context); } -void BackendEnv::FreeIsolate() +void BackendEnv::UnInitialize() { #if WITH_NODEJS // node::EmitExit(NodeEnv); @@ -308,9 +320,10 @@ void BackendEnv::LogicTick() #endif } -void BackendEnv::InitInject(v8::Isolate* Isolate, v8::Local Context) +void BackendEnv::InitInject() { - MainContext.Reset(Isolate, Context); + v8::Isolate* Isolate = MainIsolate; + v8::Local Context = MainContext.Get(Isolate); #if defined(WITH_NODEJS) v8::Local Global = Context->Global(); auto strConsole = v8::String::NewFromUtf8(Isolate, "console").ToLocalChecked(); diff --git a/unity/native_src/Src/JSEngine.cpp b/unity/native_src/Src/JSEngine.cpp index cd68f5e71d..80f2c233c6 100644 --- a/unity/native_src/Src/JSEngine.cpp +++ b/unity/native_src/Src/JSEngine.cpp @@ -108,15 +108,16 @@ namespace PUERTS_NAMESPACE #endif v8::V8::SetFlagsFromString(Flags.c_str(), static_cast(Flags.size())); - MainIsolate = BackendEnv.CreateIsolate(external_quickjs_runtime); + BackendEnv.Initialize(external_quickjs_runtime, external_quickjs_context); + MainIsolate = BackendEnv.MainIsolate; auto Isolate = MainIsolate; #ifdef MULT_BACKENDS ResultInfo.PuertsPlugin = InPuertsPlugin; #endif - ResultInfo.Isolate = MainIsolate; - MainIsolate->SetData(0, this); - MainIsolate->SetData(1, &BackendEnv); + ResultInfo.Isolate = Isolate; + Isolate->SetData(0, this); + Isolate->SetData(1, &BackendEnv); #ifdef THREAD_SAFE v8::Locker Locker(Isolate); @@ -124,17 +125,13 @@ namespace PUERTS_NAMESPACE v8::Isolate::Scope Isolatescope(Isolate); v8::HandleScope HandleScope(Isolate); -#if WITH_QUICKJS - v8::Local Context = (external_quickjs_runtime && external_quickjs_context) ? v8::Context::New(Isolate, external_quickjs_context) : v8::Context::New(Isolate); -#else - v8::Local Context = v8::Context::New(Isolate); -#endif + v8::Local Context = BackendEnv.MainContext.Get(Isolate); v8::Context::Scope ContextScope(Context); ResultInfo.Context.Reset(Isolate, Context); v8::Local Global = Context->Global(); if (external_quickjs_runtime == nullptr) { - BackendEnv.InitInject(MainIsolate, Context); + BackendEnv.InitInject(); Global->Set(Context, FV8Utils::V8String(Isolate, "__puertsGetLastException"), v8::FunctionTemplate::New(Isolate, &GetLastException)->GetFunction(Context).ToLocalChecked()).Check(); } Global->Set(Context, FV8Utils::V8String(Isolate, "__tgjsEvalScript"), v8::FunctionTemplate::New(Isolate, &EvalWithPath)->GetFunction(Context).ToLocalChecked()).Check(); @@ -142,7 +139,7 @@ namespace PUERTS_NAMESPACE JSObjectIdMap.Reset(Isolate, v8::Map::New(Isolate)); JSObjectValueGetter = CreateJSFunction( - MainIsolate, Context, + Isolate, Context, v8::FunctionTemplate::New(Isolate, &JSObjectValueGetterFunction)->GetFunction(Context).ToLocalChecked() ); } @@ -214,7 +211,7 @@ namespace PUERTS_NAMESPACE ResultInfo.Context.Reset(); ResultInfo.Result.Reset(); - BackendEnv.FreeIsolate(); + BackendEnv.UnInitialize(); for (int i = 0; i < CallbackInfos.size(); ++i) {