From a83b286355cd48e1aeb24fb1a9725086eb1bc48a Mon Sep 17 00:00:00 2001 From: johnche Date: Tue, 22 Oct 2024 15:44:39 +0800 Subject: [PATCH] =?UTF-8?q?[unreal][p-api]userdata=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=8F=A6=E6=9C=89=E7=94=A8=E9=80=94=EF=BC=8C=E6=96=B0=E5=A2=9E?= =?UTF-8?q?bool=20MustCallFinalize=E8=AE=B0=E5=BD=95=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=BE=97=E8=B0=83=E7=94=A8=E6=9E=90=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Source/JsEnv/Private/CppObjectMapper.cpp | 8 ++++---- .../Source/JsEnv/Private/ObjectCacheNode.h | 16 +++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp index 841acb52ae..afb904830e 100644 --- a/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp +++ b/unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp @@ -321,7 +321,7 @@ void FCppObjectMapper::BindCppObject( if (!PassByPointer) { - CacheNodePtr->UserData = ClassDefinition; + CacheNodePtr->MustCallFinalize = true; CacheNodePtr->Value.SetWeak( ClassDefinition, CDataGarbageCollectedWithFree, v8::WeakCallbackType::kInternalFields); } @@ -387,14 +387,14 @@ void FCppObjectMapper::UnInitialize(v8::Isolate* InIsolate) FObjectCacheNode* PNode = &KV.second; while (PNode) { - if (PNode->UserData) + if (PNode->MustCallFinalize) { - JSClassDefinition* ClassDefinition = (JSClassDefinition*) (PNode->UserData); + const JSClassDefinition* ClassDefinition = FindClassByID(PNode->TypeId); if (ClassDefinition && ClassDefinition->Finalize) { ClassDefinition->Finalize(KV.first, ClassDefinition->Data, PData); } - PNode->UserData = nullptr; + PNode->MustCallFinalize = false; } PNode = PNode->Next; } diff --git a/unreal/Puerts/Source/JsEnv/Private/ObjectCacheNode.h b/unreal/Puerts/Source/JsEnv/Private/ObjectCacheNode.h index 7957e00874..4a5e679d5f 100644 --- a/unreal/Puerts/Source/JsEnv/Private/ObjectCacheNode.h +++ b/unreal/Puerts/Source/JsEnv/Private/ObjectCacheNode.h @@ -21,20 +21,26 @@ namespace PUERTS_NAMESPACE class FObjectCacheNode { public: - V8_INLINE FObjectCacheNode(const void* TypeId_) : TypeId(TypeId_), UserData(nullptr), Next(nullptr) + V8_INLINE FObjectCacheNode(const void* TypeId_) : TypeId(TypeId_), UserData(nullptr), Next(nullptr), MustCallFinalize(false) { } - V8_INLINE FObjectCacheNode(const void* TypeId_, FObjectCacheNode* Next_) : TypeId(TypeId_), UserData(nullptr), Next(Next_) + V8_INLINE FObjectCacheNode(const void* TypeId_, FObjectCacheNode* Next_) + : TypeId(TypeId_), UserData(nullptr), Next(Next_), MustCallFinalize(false) { } V8_INLINE FObjectCacheNode(FObjectCacheNode&& other) noexcept - : TypeId(other.TypeId), UserData(other.UserData), Next(other.Next), Value(std::move(other.Value)) + : TypeId(other.TypeId) + , UserData(other.UserData) + , Next(other.Next) + , Value(std::move(other.Value)) + , MustCallFinalize(other.MustCallFinalize) { other.TypeId = nullptr; other.UserData = nullptr; other.Next = nullptr; + other.MustCallFinalize = false; } V8_INLINE FObjectCacheNode& operator=(FObjectCacheNode&& rhs) noexcept @@ -43,9 +49,11 @@ class FObjectCacheNode Next = rhs.Next; Value = std::move(rhs.Value); UserData = rhs.UserData; + MustCallFinalize = rhs.MustCallFinalize; rhs.UserData = nullptr; rhs.TypeId = nullptr; rhs.Next = nullptr; + rhs.MustCallFinalize = false; return *this; } @@ -117,6 +125,8 @@ class FObjectCacheNode v8::UniquePersistent Value; + bool MustCallFinalize; + FObjectCacheNode(const FObjectCacheNode&) = delete; void operator=(const FObjectCacheNode&) = delete; };