1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ark_native_reference.h"
17
18 #ifdef ENABLE_HITRACE
19 #include "hitrace/trace.h"
20 #include "hitrace_meter.h"
21 #endif
22 #include "native_engine/native_utils.h"
23 #include "utils/log.h"
24
ArkNativeReference(ArkNativeEngine * engine,const EcmaVM * vm,napi_value value,uint32_t initialRefcount,bool deleteSelf,NapiNativeFinalize napiCallback,void * data,void * hint)25 ArkNativeReference::ArkNativeReference(ArkNativeEngine* engine,
26 const EcmaVM* vm,
27 napi_value value,
28 uint32_t initialRefcount,
29 bool deleteSelf,
30 NapiNativeFinalize napiCallback,
31 void* data,
32 void* hint)
33 : engine_(engine),
34 vm_(vm),
35 value_(vm, LocalValueFromJsValue(value)),
36 refCount_(initialRefcount),
37 deleteSelf_(deleteSelf),
38 napiCallback_(napiCallback),
39 data_(data),
40 hint_(hint)
41 {
42 ArkNativeReferenceConstructor(initialRefcount, deleteSelf);
43 }
44
ArkNativeReference(ArkNativeEngine * engine,const EcmaVM * vm,Local<JSValueRef> value,uint32_t initialRefcount,bool deleteSelf,NapiNativeFinalize napiCallback,void * data,void * hint)45 ArkNativeReference::ArkNativeReference(ArkNativeEngine* engine,
46 const EcmaVM* vm,
47 Local<JSValueRef> value,
48 uint32_t initialRefcount,
49 bool deleteSelf,
50 NapiNativeFinalize napiCallback,
51 void* data,
52 void* hint)
53 : engine_(engine),
54 vm_(vm),
55 value_(vm, value),
56 refCount_(initialRefcount),
57 deleteSelf_(deleteSelf),
58 napiCallback_(napiCallback),
59 data_(data),
60 hint_(hint)
61 {
62 ArkNativeReferenceConstructor(initialRefcount, deleteSelf);
63 }
64
~ArkNativeReference()65 ArkNativeReference::~ArkNativeReference()
66 {
67 if (deleteSelf_ && engine_->GetReferenceManager()) {
68 engine_->GetReferenceManager()->ReleaseHandler(this);
69 prev_ = nullptr;
70 next_ = nullptr;
71 }
72 if (value_.IsEmpty()) {
73 return;
74 }
75 hasDelete_ = true;
76 value_.FreeGlobalHandleAddr();
77 FinalizeCallback();
78 }
79
Ref()80 uint32_t ArkNativeReference::Ref()
81 {
82 ++refCount_;
83 if (refCount_ == 1) {
84 value_.ClearWeak();
85 }
86 return refCount_;
87 }
88
Unref()89 uint32_t ArkNativeReference::Unref()
90 {
91 if (refCount_ == 0) {
92 return refCount_;
93 }
94 --refCount_;
95 if (value_.IsEmpty()) {
96 return refCount_;
97 }
98 if (refCount_ == 0) {
99 value_.SetWeakCallback(reinterpret_cast<void*>(this), FreeGlobalCallBack, NativeFinalizeCallBack);
100 }
101 return refCount_;
102 }
103
Get()104 napi_value ArkNativeReference::Get()
105 {
106 if (value_.IsEmpty()) {
107 return nullptr;
108 }
109 Local<JSValueRef> value = value_.ToLocal(vm_);
110 return JsValueFromLocalValue(value);
111 }
112
operator napi_value()113 ArkNativeReference::operator napi_value()
114 {
115 return Get();
116 }
117
GetData()118 void* ArkNativeReference::GetData()
119 {
120 return data_;
121 }
122
FinalizeCallback()123 void ArkNativeReference::FinalizeCallback()
124 {
125 if (napiCallback_ != nullptr) {
126 napiCallback_(reinterpret_cast<napi_env>(engine_), data_, hint_);
127 }
128 napiCallback_ = nullptr;
129 data_ = nullptr;
130 hint_ = nullptr;
131 finalRun_ = true;
132
133 if (deleteSelf_ && !hasDelete_) {
134 delete this;
135 }
136 }
137
FreeGlobalCallBack(void * ref)138 void ArkNativeReference::FreeGlobalCallBack(void* ref)
139 {
140 auto that = reinterpret_cast<ArkNativeReference*>(ref);
141 that->value_.FreeGlobalHandleAddr();
142 }
143
NativeFinalizeCallBack(void * ref)144 void ArkNativeReference::NativeFinalizeCallBack(void* ref)
145 {
146 auto that = reinterpret_cast<ArkNativeReference*>(ref);
147 that->FinalizeCallback();
148 }
149
SetDeleteSelf()150 void ArkNativeReference::SetDeleteSelf()
151 {
152 deleteSelf_ = true;
153 }
154
GetRefCount()155 uint32_t ArkNativeReference::GetRefCount()
156 {
157 return refCount_;
158 }
159
GetFinalRun()160 bool ArkNativeReference::GetFinalRun()
161 {
162 return finalRun_;
163 }
164
GetNapiValue()165 napi_value ArkNativeReference::GetNapiValue()
166 {
167 return Get();
168 }