Skip to content

Commit

Permalink
[unreal][p-api]userdata字段另有用途,新增bool MustCallFinalize记录是否得调用析构
Browse files Browse the repository at this point in the history
  • Loading branch information
chexiongsheng committed Oct 22, 2024
1 parent 5f64516 commit a83b286
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 4 additions & 4 deletions unreal/Puerts/Source/JsEnv/Private/CppObjectMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void FCppObjectMapper::BindCppObject(

if (!PassByPointer)
{
CacheNodePtr->UserData = ClassDefinition;
CacheNodePtr->MustCallFinalize = true;
CacheNodePtr->Value.SetWeak<JSClassDefinition>(
ClassDefinition, CDataGarbageCollectedWithFree, v8::WeakCallbackType::kInternalFields);
}
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 13 additions & 3 deletions unreal/Puerts/Source/JsEnv/Private/ObjectCacheNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down Expand Up @@ -117,6 +125,8 @@ class FObjectCacheNode

v8::UniquePersistent<v8::Value> Value;

bool MustCallFinalize;

FObjectCacheNode(const FObjectCacheNode&) = delete;
void operator=(const FObjectCacheNode&) = delete;
};
Expand Down

0 comments on commit a83b286

Please sign in to comment.