From cb84832288b39706bb69be4326c938413752542c Mon Sep 17 00:00:00 2001 From: linxiang Date: Tue, 14 Sep 2021 14:36:03 +0800 Subject: [PATCH] Global handle releases resources when copying and moving assignments Signed-off-by: linxiang --- ecmascript/napi/include/jsnapi.h | 52 ++++++++++++++++++++------------ ecmascript/napi/jsnapi.cpp | 5 --- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/ecmascript/napi/include/jsnapi.h b/ecmascript/napi/include/jsnapi.h index 323f573562..eb5db58702 100644 --- a/ecmascript/napi/include/jsnapi.h +++ b/ecmascript/napi/include/jsnapi.h @@ -66,9 +66,6 @@ class PUBLIC_API Local { // NOLINT(cppcoreguidelines-special-member-functions, public: inline Local() = default; - // Non-empty initial value. - explicit Local(const EcmaVM *vm); - template inline Local(const Local ¤t) : address_(reinterpret_cast(*current)) { @@ -125,6 +122,27 @@ class PUBLIC_API Global { // NOLINTNEXTLINE(cppcoreguidelines-special-member-fu public: inline Global() = default; + inline Global(const Global &that) { + address_ = that.address_; + vm_ = that.vm_; + } + + inline Global &operator=(const Global &that) + { + Update(that); + return *this; + } + + inline Global(Global &&that) { + address_ = that.address_; + vm_ = that.vm_; + } + + inline Global &operator=(Global &&that) + { + Update(that); + return *this; + } // Non-empty initial value. explicit Global(const EcmaVM *vm); @@ -169,6 +187,7 @@ private: { return reinterpret_cast(address_); }; + inline void Update(const Global &that); uintptr_t address_ = 0U; const EcmaVM *vm_{nullptr}; }; @@ -350,9 +369,7 @@ using NativePointerCallback = void (*)(void* value, void* hint); class PUBLIC_API NativePointerRef : public JSValueRef { public: static Local New(const EcmaVM *vm, void *nativePointer); - static Local New(const EcmaVM *vm, - void *nativePointer, - NativePointerCallback callBack, + static Local New(const EcmaVM *vm, void *nativePointer, NativePointerCallback callBack, void *data); void *Value(); }; @@ -806,7 +823,6 @@ private: static uintptr_t SetWeak(const EcmaVM *vm, uintptr_t localAddress); static bool IsWeak(const EcmaVM *vm, uintptr_t localAddress); static void DisposeGlobalHandleAddr(const EcmaVM *vm, uintptr_t addr); - static uintptr_t GetGlobalUndefinedAddr(const EcmaVM *vm); template friend class Global; template @@ -815,12 +831,6 @@ private: }; -template -Global::Global(const EcmaVM *vm) -{ - address_ = JSNApi::GetGlobalUndefinedAddr(vm); -} - template template Global::Global(const EcmaVM *vm, const Local ¤t) : vm_(vm) @@ -828,6 +838,16 @@ Global::Global(const EcmaVM *vm, const Local ¤t) : vm_(vm) address_ = JSNApi::GetGlobalHandleAddr(vm_, reinterpret_cast(*current)); } +template +void Global::Update(const Global &that) +{ + if (address_ != 0) { + JSNApi::DisposeGlobalHandleAddr(vm_, address_); + } + address_ = that.address_; + vm_ = that.vm_; +} + template void Global::FreeGlobalHandleAddr() { @@ -851,12 +871,6 @@ bool Global::IsWeak() const } // ---------------------------------- Local -------------------------------------------- -template -Local::Local(const EcmaVM *vm) -{ - address_ = JSNApi::GetGlobalUndefinedAddr(vm); -} - template Local::Local(const EcmaVM *vm, const Global ¤t) { diff --git a/ecmascript/napi/jsnapi.cpp b/ecmascript/napi/jsnapi.cpp index ff7223a2c1..60d3168abc 100644 --- a/ecmascript/napi/jsnapi.cpp +++ b/ecmascript/napi/jsnapi.cpp @@ -314,11 +314,6 @@ void JSNApi::DisposeGlobalHandleAddr(const EcmaVM *vm, uintptr_t addr) vm->GetJSThread()->GetEcmaGlobalStorage()->DisposeGlobalHandle(addr); } -uintptr_t JSNApi::GetGlobalUndefinedAddr(const EcmaVM *vm) -{ - return vm->GetJSThread()->GlobalConstants()->GetGlobalConstantAddr(ecmascript::ConstantIndex::UNDEFINED_INDEX); -} - void *JSNApi::SerializeValue(const EcmaVM *vm, Local value, Local transfer) { ecmascript::JSThread *thread = vm->GetJSThread();