• 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 "jerryscript_native_engine.h"
17 #include "./native_value/jerryscript_native_object.h"
18 #include "jerryscript_native_reference.h"
19 
JerryScriptNativeReference(JerryScriptNativeEngine * engine,NativeValue * value,uint32_t initialRefcount,NativeFinalize callback,void * data,void * hint)20 JerryScriptNativeReference::JerryScriptNativeReference(JerryScriptNativeEngine* engine,
21                                                        NativeValue* value,
22                                                        uint32_t initialRefcount,
23                                                        NativeFinalize callback,
24                                                        void* data,
25                                                        void* hint)
26     : engine_(engine), value_(*value), refCount_(initialRefcount),
27     callback_(callback), data_(data), hint_(hint)
28 {
29     for (uint32_t i = 0; i < refCount_; i++) {
30         jerry_acquire_value(value_);
31     }
32 }
33 
~JerryScriptNativeReference()34 JerryScriptNativeReference::~JerryScriptNativeReference()
35 {
36     FinalizeCallback();
37     for (uint32_t i = 0; i < refCount_; i++) {
38         jerry_release_value(value_);
39     }
40 }
41 
Ref()42 uint32_t JerryScriptNativeReference::Ref()
43 {
44     if (refCount_ == 0) {
45         return 0;
46     }
47     jerry_acquire_value(value_);
48     return ++refCount_;
49 }
50 
Unref()51 uint32_t JerryScriptNativeReference::Unref()
52 {
53     if (refCount_ == 0) {
54         return 0;
55     }
56 
57     --refCount_;
58     if (refCount_ == 0) {
59         FinalizeCallback();
60     }
61     jerry_release_value(value_);
62     return refCount_;
63 }
64 
Get()65 NativeValue* JerryScriptNativeReference::Get()
66 {
67     if (refCount_ > 0) {
68         return JerryScriptNativeEngine::JerryValueToNativeValue(engine_, jerry_acquire_value(value_));
69     } else {
70         return nullptr;
71     }
72 }
73 
operator NativeValue*()74 JerryScriptNativeReference::operator NativeValue*()
75 {
76     return Get();
77 }
78 
FinalizeCallback(void)79 void JerryScriptNativeReference::FinalizeCallback(void)
80 {
81     if (callback_ != nullptr) {
82         JerryScriptNativeObject* nativeObject = new JerryScriptNativeObject(engine_, jerry_value_to_object(value_));
83         if (nativeObject != nullptr) {
84             nativeObject->AddFinalizer(nullptr, nullptr, nullptr);
85             delete nativeObject;
86         }
87         callback_(engine_, data_, hint_);
88     }
89     callback_ = nullptr;
90     data_ = nullptr;
91     hint_ = nullptr;
92 }
93