Skip to content

Commit

Permalink
[unity]Template::GetFunction不是必然成功的,需要处理错误,直接ToLocalChecked会崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Jan 30, 2024
1 parent 2f981bb commit 3d7d3b6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion unity/native_src/Inc/JSEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class JSEngine

v8::Local<v8::Value> GetClassConstructor(int ClassID);

v8::Local<v8::Value> FindOrAddObject(v8::Isolate* Isolate, v8::Local<v8::Context> Context, int ClassID, void *Ptr);
v8::MaybeLocal<v8::Value> FindOrAddObject(v8::Isolate* Isolate, v8::Local<v8::Context> Context, int ClassID, void *Ptr);

void BindObject(FLifeCycleInfo* LifeCycleInfo, void* Ptr, v8::Local<v8::Object> JSObject);

Expand Down
17 changes: 14 additions & 3 deletions unity/native_src/Src/JSEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ namespace puerts
return Result;
}

v8::Local<v8::Value> JSEngine::FindOrAddObject(v8::Isolate* Isolate, v8::Local<v8::Context> Context, int ClassID, void *Ptr)
v8::MaybeLocal<v8::Value> JSEngine::FindOrAddObject(v8::Isolate* Isolate, v8::Local<v8::Context> Context, int ClassID, void *Ptr)
{
if (!Ptr)
{
Expand All @@ -597,11 +597,22 @@ namespace puerts
{
auto BindTo = v8::External::New(Context->GetIsolate(), Ptr);
v8::Local<v8::Value> Args[] = { BindTo };
return Templates[ClassID].Get(Isolate)->GetFunction(Context).ToLocalChecked()->NewInstance(Context, 1, Args).ToLocalChecked();
auto MaybeFunc = Templates[ClassID].Get(Isolate)->GetFunction(Context);
v8::Local<v8::Function> Func;
if (MaybeFunc.ToLocal(&Func))
{
auto MaybeObj = Func->NewInstance(Context, 1, Args);
v8::Local<v8::Object> Obj;
if (MaybeObj.ToLocal(&Obj))
{
return v8::MaybeLocal<v8::Value>(Obj);
}
}
return v8::MaybeLocal<v8::Value>();
}
else
{
return v8::Local<v8::Value>::New(Isolate, Iter->second);
return v8::MaybeLocal<v8::Value>(v8::Local<v8::Value>::New(Isolate, Iter->second));
}
}

Expand Down
11 changes: 10 additions & 1 deletion unity/native_src/Src/JSFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,23 @@ namespace puerts
v8::Local<v8::Context> Context = ResultInfo.Context.Get(Isolate);
v8::Context::Scope ContextScope(Context);

v8::TryCatch TryCatch(Isolate);
std::vector< v8::Local<v8::Value>> V8Args;
for (int i = 0; i < Arguments.size(); ++i)
{
V8Args.push_back(ToV8(Isolate, Context, Arguments[i]));
Arguments[i].Persistent.Reset();
}
Arguments.clear();
v8::TryCatch TryCatch(Isolate);

if (TryCatch.HasCaught())
{
v8::Local<v8::Value> Exception = TryCatch.Exception();
LastException.Reset(Isolate, Exception);
LastExceptionInfo = FV8Utils::ExceptionToString(Isolate, Exception);
return false;
}

auto maybeValue = GFunction.Get(Isolate)->Call(Context, Context->Global(), static_cast<int>(V8Args.size()), V8Args.data());

if (TryCatch.HasCaught())
Expand Down
27 changes: 20 additions & 7 deletions unity/native_src/Src/Puerts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,13 @@ V8_EXPORT void SetObjectToOutValue(v8::Isolate* Isolate, v8::Value *Value, int C
{
auto Context = Isolate->GetCurrentContext();
auto JsEngine = FV8Utils::IsolateData<JSEngine>(Isolate);
auto Object = JsEngine->FindOrAddObject(Isolate, Context, ClassID, Ptr);
auto Outer = Value->ToObject(Context).ToLocalChecked();
auto ReturnVal = Outer->Set(Context, 0, Object);
auto MaybeObj = JsEngine->FindOrAddObject(Isolate, Context, ClassID, Ptr);
v8::Local<v8::Value> Obj;
if (MaybeObj.ToLocal(&Obj))
{
auto Outer = Value->ToObject(Context).ToLocalChecked();
auto ReturnVal = Outer->Set(Context, 0, Obj);
}
}
}

Expand Down Expand Up @@ -546,7 +550,12 @@ V8_EXPORT void ReturnClass(v8::Isolate* Isolate, const v8::FunctionCallbackInfo<
V8_EXPORT void ReturnObject(v8::Isolate* Isolate, const v8::FunctionCallbackInfo<v8::Value>& Info, int ClassID, void* Ptr)
{
auto JsEngine = FV8Utils::IsolateData<JSEngine>(Isolate);
Info.GetReturnValue().Set(JsEngine->FindOrAddObject(Isolate, Isolate->GetCurrentContext(), ClassID, Ptr));
auto MaybeObj = JsEngine->FindOrAddObject(Isolate, Isolate->GetCurrentContext(), ClassID, Ptr);
v8::Local<v8::Value> Obj;
if (MaybeObj.ToLocal(&Obj))
{
Info.GetReturnValue().Set(Obj);
}
}

V8_EXPORT void ReturnNumber(v8::Isolate* Isolate, const v8::FunctionCallbackInfo<v8::Value>& Info, double Number)
Expand Down Expand Up @@ -685,9 +694,13 @@ V8_EXPORT void PushObjectForJSFunction(JSFunction *Function, int ClassID, void*
v8::Local<v8::Context> Context = Function->ResultInfo.Context.Get(Isolate);
v8::Context::Scope ContextScope(Context);
auto JsEngine = FV8Utils::IsolateData<JSEngine>(Isolate);
auto localObj = JsEngine->FindOrAddObject(Isolate, Context, ClassID, Ptr);
Value.Persistent.Reset(Isolate, localObj);
Function->Arguments.push_back(std::move(Value));
auto MaybeObj = JsEngine->FindOrAddObject(Isolate, Context, ClassID, Ptr);
v8::Local<v8::Value> Obj;
if (MaybeObj.ToLocal(&Obj))
{
Value.Persistent.Reset(Isolate, Obj);
Function->Arguments.push_back(std::move(Value));
}
}

V8_EXPORT void PushJSFunctionForJSFunction(JSFunction *F, JSFunction *V)
Expand Down

0 comments on commit 3d7d3b6

Please sign in to comment.