From 283871354984d28f66aa1ec0765800bfeb581e81 Mon Sep 17 00:00:00 2001 From: Tangxinwei Date: Tue, 14 Feb 2023 10:47:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20https://github.com/Tencent?= =?UTF-8?q?/puerts/issues/1203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/JsEnv/Private/CppObjectMapper.cpp | 28 +++++++++---------- .../Puerts/Source/JsEnv/Private/JsEnvImpl.cpp | 10 ++++++- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp index d639d65fbc..6f854b664c 100644 --- a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp @@ -251,29 +251,27 @@ void FCppObjectMapper::BindCppObject( DataTransfer::SetPointer(Isolate, JSObject, Ptr, 0); DataTransfer::SetPointer(Isolate, JSObject, ClassDefinition->TypeId, 1); - if (!PassByPointer) + auto Iter = CDataCache.find(Ptr); + FObjectCacheNode* CacheNodePtr; + if (Iter != CDataCache.end()) + { + CacheNodePtr = Iter->second.Add(ClassDefinition->TypeId); + } + else { auto Ret = CDataCache.insert({Ptr, FObjectCacheNode(ClassDefinition->TypeId)}); - auto CacheNodePtr = &Ret.first->second; - CacheNodePtr->Value.Reset(Isolate, JSObject); + CacheNodePtr = &Ret.first->second; + } + CacheNodePtr->Value.Reset(Isolate, JSObject); + + if (!PassByPointer) + { CDataFinalizeMap[Ptr] = ClassDefinition->Finalize; CacheNodePtr->Value.SetWeak( ClassDefinition, CDataGarbageCollectedWithFree, v8::WeakCallbackType::kInternalFields); } else { - auto Iter = CDataCache.find(Ptr); - FObjectCacheNode* CacheNodePtr; - if (Iter != CDataCache.end()) - { - CacheNodePtr = Iter->second.Add(ClassDefinition->TypeId); - } - else - { - auto Ret = CDataCache.insert({Ptr, FObjectCacheNode(ClassDefinition->TypeId)}); - CacheNodePtr = &Ret.first->second; - } - CacheNodePtr->Value.Reset(Isolate, JSObject); CacheNodePtr->Value.SetWeak( ClassDefinition, CDataGarbageCollectedWithoutFree, v8::WeakCallbackType::kInternalFields); } diff --git a/unreal/Puerts/Source/JsEnv/Private/JsEnvImpl.cpp b/unreal/Puerts/Source/JsEnv/Private/JsEnvImpl.cpp index 880ae77875..0d02d1a03f 100644 --- a/unreal/Puerts/Source/JsEnv/Private/JsEnvImpl.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/JsEnvImpl.cpp @@ -2635,7 +2635,15 @@ void FJsEnvImpl::BindStruct( auto MemoryHolder = v8::ArrayBuffer::New(MainIsolate, std::move(Backing)); __USE(JSObject->Set(MainIsolate->GetCurrentContext(), 0, MemoryHolder)); #else - auto CacheNodePtr = &StructCache.Emplace(Ptr, FObjectCacheNode(ScriptStructWrapper->Struct.Get())); + auto CacheNodePtr = StructCache.Find(Ptr); + if (CacheNodePtr) + { + CacheNodePtr = CacheNodePtr->Add(ScriptStructWrapper->Struct.Get()); + } + else + { + CacheNodePtr = &StructCache.Emplace(Ptr, FObjectCacheNode(ScriptStructWrapper->Struct.Get())); + } CacheNodePtr->Value.Reset(MainIsolate, JSObject); ScriptStructFinalizeInfoMap.Add(Ptr, {ScriptStructWrapper->Struct, ScriptStructWrapper->ExternalFinalize}); CacheNodePtr->Value.SetWeak(