!191 ark native reference modify

Merge pull request !191 from yaojian16/bugfix_20220307
This commit is contained in:
openharmony_ci
2022-03-15 13:18:36 +00:00
committed by Gitee
4 changed files with 45 additions and 29 deletions
+1 -1
View File
@@ -542,7 +542,7 @@ NativeValue* ArkNativeEngine::CreateInstance(NativeValue* constructor, NativeVal
NativeReference* ArkNativeEngine::CreateReference(NativeValue* value, uint32_t initialRefcount,
NativeFinalize callback, void* data, void* hint)
{
return new ArkNativeReference(this, value, initialRefcount);
return new ArkNativeReference(this, value, initialRefcount, false);
}
bool ArkNativeEngine::Throw(NativeValue* error)
@@ -23,11 +23,20 @@
#include "utils/log.h"
ArkNativeReference::ArkNativeReference(ArkNativeEngine* engine, NativeValue* value, uint32_t initialRefcount,
NativeFinalize callback, void* data, void* hint)
ArkNativeReference::ArkNativeReference(ArkNativeEngine* engine,
NativeValue* value,
uint32_t initialRefcount,
bool deleteSelf,
NativeFinalize callback,
void* data,
void* hint)
: engine_(engine),
value_(Global<JSValueRef>(engine->GetEcmaVm(), JSValueRef::Undefined(engine->GetEcmaVm()))),
refCount_(initialRefcount), callback_(callback), data_(data), hint_(hint)
refCount_(initialRefcount),
deleteSelf_(deleteSelf),
callback_(callback),
data_(data),
hint_(hint)
{
Global<JSValueRef> oldValue = *value;
auto vm = engine->GetEcmaVm();
@@ -40,10 +49,21 @@ ArkNativeReference::ArkNativeReference(ArkNativeEngine* engine, NativeValue* val
#ifdef ENABLE_CONTAINER_SCOPE
scopeId_ = OHOS::Ace::ContainerScope::CurrentId();
#endif
if (deleteSelf) {
NativeReferenceManager* referenceManager = engine->GetReferenceManager();
if (referenceManager != nullptr) {
referenceManager->CreateHandler(this);
}
}
}
ArkNativeReference::~ArkNativeReference()
{
if (deleteSelf_ && engine_->GetReferenceManager()) {
engine_->GetReferenceManager()->ReleaseHandler(this);
}
if (!value_.IsWeak()) {
value_.SetWeak();
}
@@ -89,6 +109,11 @@ ArkNativeReference::operator NativeValue*()
return Get();
}
void* ArkNativeReference::GetData()
{
return data_;
}
void ArkNativeReference::FinalizeCallback()
{
if (callback_ != nullptr) {
@@ -28,19 +28,26 @@ using panda::Local;
class ArkNativeReference : public NativeReference {
public:
ArkNativeReference(ArkNativeEngine* engine, NativeValue* value, uint32_t initialRefcount,
NativeFinalize callback = nullptr, void* data = nullptr, void* hint = nullptr);
ArkNativeReference(ArkNativeEngine* engine,
NativeValue* value,
uint32_t initialRefcount,
bool deleteSelf,
NativeFinalize callback = nullptr,
void* data = nullptr,
void* hint = nullptr);
~ArkNativeReference() override;
uint32_t Ref() override;
uint32_t Unref() override;
NativeValue* Get() override;
void* GetData() override;
operator NativeValue*() override;
private:
ArkNativeEngine* engine_;
Global<JSValueRef> value_;
uint32_t refCount_;
bool deleteSelf_;
#ifdef ENABLE_CONTAINER_SCOPE
int32_t scopeId_ = -1;
@@ -56,33 +56,16 @@ void ArkNativeObject::SetNativePointer(void* pointer, NativeFinalize cb, void* h
Local<StringRef> key = StringRef::NewFromUtf8(vm, "_napiwrapper");
if (value->Has(vm, key) && pointer == nullptr) {
Local<ObjectRef> wrapper = value->Get(vm, key);
auto oldInfo = reinterpret_cast<NativeObjectInfo*>(wrapper->GetNativePointerField(0));
auto ref = reinterpret_cast<ArkNativeReference*>(wrapper->GetNativePointerField(0));
// Try to remove native pointer from ArrayDataList
wrapper->SetNativePointerField(0, nullptr, nullptr, nullptr);
value->Delete(vm, key);
delete oldInfo;
delete ref;
} else {
NativeObjectInfo* objInfo = NativeObjectInfo::CreateNewInstance();
objInfo->engine = engine_;
objInfo->nativeObject = pointer;
objInfo->callback = cb;
objInfo->hint = hint;
Local<ObjectRef> object = ObjectRef::New(vm);
ArkNativeReference* ref = new ArkNativeReference(engine_, this, 1, true, cb, pointer, hint);
object->SetNativePointerFieldCount(1);
object->SetNativePointerField(0, objInfo,
[](void* data, void* info) {
auto externalInfo = reinterpret_cast<NativeObjectInfo*>(data);
auto engine = externalInfo->engine;
auto nativeObject = externalInfo->nativeObject;
auto callback = externalInfo->callback;
auto hint = externalInfo->hint;
if (callback != nullptr) {
callback(engine, nativeObject, hint);
}
delete externalInfo;
},
nullptr);
object->SetNativePointerField(0, ref, nullptr, nullptr);
value->Set(vm, key, object);
}
}
@@ -97,9 +80,10 @@ void* ArkNativeObject::GetNativePointer()
void* result = nullptr;
if (val->IsObject()) {
Local<ObjectRef> ext(val);
result = ext->GetNativePointerField(0);
auto ref = reinterpret_cast<ArkNativeReference*>(ext->GetNativePointerField(0));
result = ref->GetData();
}
return (result != nullptr) ? reinterpret_cast<NativeObjectInfo*>(result)->nativeObject : nullptr;
return result;
}
NativeValue* ArkNativeObject::GetPropertyNames()