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(