diff --git a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp index 2c6b6a69f7..62c4522514 100644 --- a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp @@ -70,7 +70,7 @@ void FCppObjectMapper::Initialize(v8::Isolate* InIsolate, v8::Local { auto LocalTemplate = v8::FunctionTemplate::New(InIsolate, PointerNew); LocalTemplate->InstanceTemplate()->SetInternalFieldCount(4); // 0 Ptr, 1, CDataName - PointerConstructor = v8::UniquePersistent(InIsolate, LocalTemplate->GetFunction(InContext).ToLocalChecked()); + PointerTemplate = v8::UniquePersistent(InIsolate, LocalTemplate); #ifndef WITH_QUICKJS PrivateKey.Reset(InIsolate, v8::Symbol::New(InIsolate)); #endif @@ -107,7 +107,7 @@ v8::Local FCppObjectMapper::FindOrAddCppObject( } else { - auto Result = PointerConstructor.Get(Isolate)->NewInstance(Context, 0, nullptr).ToLocalChecked(); + auto Result = PointerTemplate.Get(Isolate)->InstanceTemplate()->NewInstance(Context).ToLocalChecked(); DataTransfer::SetPointer(Isolate, Result, Ptr, 0); DataTransfer::SetPointer(Isolate, Result, TypeId, 1); return Result; @@ -164,8 +164,8 @@ static void CDataNew(const v8::FunctionCallbackInfo& Info) v8::Local FCppObjectMapper::GetTemplateOfClass(v8::Isolate* Isolate, const JSClassDefinition* ClassDefinition) { - auto Iter = CDataNameToTemplateMap.find(ClassDefinition->TypeId); - if (Iter == CDataNameToTemplateMap.end()) + auto Iter = TypeIdToTemplateMap.find(ClassDefinition->TypeId); + if (Iter == TypeIdToTemplateMap.end()) { auto Template = v8::FunctionTemplate::New( Isolate, CDataNew, v8::External::New(Isolate, const_cast(reinterpret_cast(ClassDefinition)))); @@ -284,7 +284,7 @@ v8::Local FCppObjectMapper::GetTemplateOfClass(v8::Isolate } } - CDataNameToTemplateMap[ClassDefinition->TypeId] = v8::UniquePersistent(Isolate, Template); + TypeIdToTemplateMap[ClassDefinition->TypeId] = v8::UniquePersistent(Isolate, Template); return Template; } @@ -425,11 +425,11 @@ void FCppObjectMapper::UnInitialize(v8::Isolate* InIsolate) } } CDataCache.clear(); - CDataNameToTemplateMap.clear(); + TypeIdToTemplateMap.clear(); #ifndef WITH_QUICKJS PrivateKey.Reset(); #endif - PointerConstructor.Reset(); + PointerTemplate.Reset(); } } // namespace PUERTS_NAMESPACE diff --git a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.h b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.h index 12934d6563..a277a183e2 100644 --- a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.h +++ b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.h @@ -70,9 +70,9 @@ class FCppObjectMapper final : public ICppObjectMapper private: std::unordered_map CDataCache; - std::unordered_map, PointerHash, PointerEqual> CDataNameToTemplateMap; + std::unordered_map, PointerHash, PointerEqual> TypeIdToTemplateMap; - v8::UniquePersistent PointerConstructor; + v8::UniquePersistent PointerTemplate; #ifndef WITH_QUICKJS v8::Global PrivateKey; diff --git a/unreal/Puerts/Source/JsEnv/Private/JSClassRegister.cpp b/unreal/Puerts/Source/JsEnv/Private/JSClassRegister.cpp index 02f382c2bb..3d5f20b45b 100644 --- a/unreal/Puerts/Source/JsEnv/Private/JSClassRegister.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/JSClassRegister.cpp @@ -84,6 +84,10 @@ class JSClassRegister const JSClassDefinition* LoadClassByID(const void* TypeId) { + if (!TypeId) + { + return nullptr; + } auto clsDef = FindClassByID(TypeId); if (!clsDef && ClassNotFoundCallback) { @@ -213,6 +217,10 @@ void JSClassRegister::SetClassTypeInfo(const void* TypeId, const NamedFunctionIn const JSClassDefinition* JSClassRegister::FindClassByID(const void* TypeId) { + if (!TypeId) + { + return nullptr; + } auto Iter = CDataIdToClassDefinition.find(TypeId); if (Iter == CDataIdToClassDefinition.end()) {