From 0265fec29ba9d29d0d3edbf6c304b85fe5f0946c Mon Sep 17 00:00:00 2001 From: johnche Date: Wed, 23 Oct 2024 11:54:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=90=8D=E5=AD=97?= =?UTF-8?q?=EF=BC=8C=E7=A9=BATypeId=E6=9F=A5=E6=89=BE=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=A9=BA=EF=BC=8C=E6=8C=87=E9=92=88=E9=80=9A=E8=BF=87InstanceT?= =?UTF-8?q?emplate=E6=9E=84=E9=80=A0=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/JsEnv/Private/CppObjectMapper.cpp | 14 +++++++------- .../Puerts/Source/JsEnv/Private/CppObjectMapper.h | 4 ++-- .../Source/JsEnv/Private/JSClassRegister.cpp | 8 ++++++++ 3 files changed, 17 insertions(+), 9 deletions(-) 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()) {