Global handle releases resources when copying and moving assignments

Signed-off-by: linxiang <linxiang8@huawei.com>
This commit is contained in:
linxiang 2021-09-14 14:36:03 +08:00
parent e76ce09076
commit cb84832288
2 changed files with 33 additions and 24 deletions

View File

@ -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<typename S>
inline Local(const Local<S> &current) : address_(reinterpret_cast<uintptr_t>(*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<T *>(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<NativePointerRef> New(const EcmaVM *vm, void *nativePointer);
static Local<NativePointerRef> New(const EcmaVM *vm,
void *nativePointer,
NativePointerCallback callBack,
static Local<NativePointerRef> 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<typename T>
friend class Global;
template<typename T>
@ -815,12 +831,6 @@ private:
};
template<typename T>
Global<T>::Global(const EcmaVM *vm)
{
address_ = JSNApi::GetGlobalUndefinedAddr(vm);
}
template<typename T>
template<typename S>
Global<T>::Global(const EcmaVM *vm, const Local<S> &current) : vm_(vm)
@ -828,6 +838,16 @@ Global<T>::Global(const EcmaVM *vm, const Local<S> &current) : vm_(vm)
address_ = JSNApi::GetGlobalHandleAddr(vm_, reinterpret_cast<uintptr_t>(*current));
}
template<typename T>
void Global<T>::Update(const Global &that)
{
if (address_ != 0) {
JSNApi::DisposeGlobalHandleAddr(vm_, address_);
}
address_ = that.address_;
vm_ = that.vm_;
}
template<typename T>
void Global<T>::FreeGlobalHandleAddr()
{
@ -851,12 +871,6 @@ bool Global<T>::IsWeak() const
}
// ---------------------------------- Local --------------------------------------------
template<typename T>
Local<T>::Local(const EcmaVM *vm)
{
address_ = JSNApi::GetGlobalUndefinedAddr(vm);
}
template<typename T>
Local<T>::Local(const EcmaVM *vm, const Global<T> &current)
{

View File

@ -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<JSValueRef> value, Local<JSValueRef> transfer)
{
ecmascript::JSThread *thread = vm->GetJSThread();