• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_engine.h"
17 
18 #include "ark_native_reference.h"
19 
20 #ifdef ENABLE_CONTAINER_SCOPE
21 #include "core/common/container_scope.h"
22 #endif
23 
24 #include "utils/log.h"
25 
ArkNativeReference(ArkNativeEngine * engine,NativeValue * value,uint32_t initialRefcount,NativeFinalize callback,void * data,void * hint)26 ArkNativeReference::ArkNativeReference(ArkNativeEngine* engine, NativeValue* value, uint32_t initialRefcount,
27     NativeFinalize callback, void* data, void* hint)
28     : engine_(engine),
29       value_(Global<JSValueRef>(engine->GetEcmaVm(), JSValueRef::Undefined(engine->GetEcmaVm()))),
30       refCount_(initialRefcount), callback_(callback), data_(data), hint_(hint)
31 {
32     Global<JSValueRef> oldValue = *value;
33     auto vm = engine->GetEcmaVm();
34     LocalScope scope(vm);
35     Global<JSValueRef> newValue(vm, oldValue.ToLocal(vm));
36     value_ = newValue;
37     if (initialRefcount == 0) {
38         value_.SetWeak();
39     }
40 
41 #ifdef ENABLE_CONTAINER_SCOPE
42     scopeId_ = OHOS::Ace::ContainerScope::CurrentId();
43 #endif
44 }
45 
~ArkNativeReference()46 ArkNativeReference::~ArkNativeReference()
47 {
48     if (value_.IsEmpty()) {
49         return;
50     }
51     value_.FreeGlobalHandleAddr();
52     refCount_ = 0;
53     FinalizeCallback();
54 }
55 
Ref()56 uint32_t ArkNativeReference::Ref()
57 {
58     ++refCount_;
59     if (refCount_ == 1) {
60         value_.ClearWeak();
61     }
62     return refCount_;
63 }
64 
Unref()65 uint32_t ArkNativeReference::Unref()
66 {
67     if (refCount_ == 0) {
68         return refCount_;
69     }
70     --refCount_;
71     if (refCount_ == 0) {
72         value_.SetWeak();
73         FinalizeCallback();
74     }
75     return refCount_;
76 }
77 
Get()78 NativeValue* ArkNativeReference::Get()
79 {
80     auto vm = engine_->GetEcmaVm();
81     LocalScope scope(vm);
82     Local<JSValueRef> value = value_.ToLocal(vm);
83 #ifdef ENABLE_CONTAINER_SCOPE
84     OHOS::Ace::ContainerScope containerScope(scopeId_);
85 #endif
86     return ArkNativeEngine::ArkValueToNativeValue(engine_, value);
87 }
88 
operator NativeValue*()89 ArkNativeReference::operator NativeValue*()
90 {
91     return Get();
92 }
93 
FinalizeCallback()94 void ArkNativeReference::FinalizeCallback()
95 {
96     if (callback_ != nullptr) {
97         callback_(engine_, data_, hint_);
98     }
99     callback_ = nullptr;
100     data_ = nullptr;
101     hint_ = nullptr;
102 }
103