Skip to content

Commit

Permalink
[unity]重构,之前FreeIsolate和CreateIsolate不对称,前者多了MainContext释放,但MainConte…
Browse files Browse the repository at this point in the history
…xt却是从另外一函数传入
  • Loading branch information
chexiongsheng committed Apr 8, 2024
1 parent 8abb25f commit 3851f58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
9 changes: 4 additions & 5 deletions unity/native_src/Inc/BackendEnv.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ namespace PUERTS_NAMESPACE
{
class BackendEnv
{
private:
public:
v8::Isolate* MainIsolate;

v8::Global<v8::Context> MainContext;

public:
~BackendEnv() {
PathToModuleMap.clear();
ScriptIdToPathMap.clear();
Expand Down Expand Up @@ -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<v8::Context> Context);
void InitInject();

void CreateInspector(v8::Isolate* Isolate, const v8::Global<v8::Context>* ContextGlobal, int32_t Port);

Expand Down
27 changes: 20 additions & 7 deletions unity/native_src/Src/BackendEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<v8::Context> Context = (external_quickjs_runtime && external_quickjs_context) ? v8::Context::New(Isolate, external_quickjs_context) : v8::Context::New(Isolate);
#else
v8::Local<v8::Context> Context = v8::Context::New(Isolate);
#endif
MainContext.Reset(Isolate, Context);
}

void BackendEnv::FreeIsolate()
void BackendEnv::UnInitialize()
{
#if WITH_NODEJS
// node::EmitExit(NodeEnv);
Expand Down Expand Up @@ -308,9 +320,10 @@ void BackendEnv::LogicTick()
#endif
}

void BackendEnv::InitInject(v8::Isolate* Isolate, v8::Local<v8::Context> Context)
void BackendEnv::InitInject()
{
MainContext.Reset(Isolate, Context);
v8::Isolate* Isolate = MainIsolate;
v8::Local<v8::Context> Context = MainContext.Get(Isolate);
#if defined(WITH_NODEJS)
v8::Local<v8::Object> Global = Context->Global();
auto strConsole = v8::String::NewFromUtf8(Isolate, "console").ToLocalChecked();
Expand Down
21 changes: 9 additions & 12 deletions unity/native_src/Src/JSEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,41 +108,38 @@ namespace PUERTS_NAMESPACE
#endif
v8::V8::SetFlagsFromString(Flags.c_str(), static_cast<int>(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);
#endif
v8::Isolate::Scope Isolatescope(Isolate);
v8::HandleScope HandleScope(Isolate);

#if WITH_QUICKJS
v8::Local<v8::Context> Context = (external_quickjs_runtime && external_quickjs_context) ? v8::Context::New(Isolate, external_quickjs_context) : v8::Context::New(Isolate);
#else
v8::Local<v8::Context> Context = v8::Context::New(Isolate);
#endif
v8::Local<v8::Context> Context = BackendEnv.MainContext.Get(Isolate);
v8::Context::Scope ContextScope(Context);
ResultInfo.Context.Reset(Isolate, Context);
v8::Local<v8::Object> 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();

JSObjectIdMap.Reset(Isolate, v8::Map::New(Isolate));

JSObjectValueGetter = CreateJSFunction(
MainIsolate, Context,
Isolate, Context,
v8::FunctionTemplate::New(Isolate, &JSObjectValueGetterFunction)->GetFunction(Context).ToLocalChecked()
);
}
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 3851f58

Please sign in to comment.