• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "weak_ref.h"
17 
18 namespace NapiApi {
19 
WeakRef(const NapiApi::WeakRef & ref)20 WeakRef::WeakRef(const NapiApi::WeakRef& ref)
21 {
22     if (!ref.IsEmpty()) {
23         napi_status stat;
24         env_ = ref.env_;
25         stat = napi_create_reference(env_, ref.GetValue(), 0, &ref_);
26     }
27 }
28 
WeakRef(NapiApi::WeakRef && ref)29 WeakRef::WeakRef(NapiApi::WeakRef&& ref) noexcept
30 {
31     env_ = ref.env_;
32     ref_ = ref.ref_;
33     ref.env_ = nullptr;
34     ref.ref_ = nullptr;
35 }
36 
WeakRef(NapiApi::Object obj)37 WeakRef::WeakRef(NapiApi::Object obj)
38 {
39     env_ = obj.GetEnv();
40     napi_create_reference(env_, obj.ToNapiValue(), 0, &ref_);
41 }
42 
WeakRef(napi_env env,napi_value obj)43 WeakRef::WeakRef(napi_env env, napi_value obj)
44 {
45     env_ = env;
46     napi_create_reference(env_, obj, 0, &ref_);
47 }
48 
operator =(const NapiApi::WeakRef & ref)49 NapiApi::WeakRef& WeakRef::operator=(const NapiApi::WeakRef& ref)
50 {
51     if (&ref != this) {
52         if (!ref.IsEmpty()) {
53             napi_status stat;
54             // unh just create a new one..
55             env_ = ref.env_;
56             stat = napi_create_reference(env_, ref.GetValue(), 0, &ref_);
57         }
58     }
59     return *this;
60 }
61 
operator =(NapiApi::WeakRef && ref)62 NapiApi::WeakRef& WeakRef::operator=(NapiApi::WeakRef&& ref) noexcept
63 {
64     env_ = ref.env_;
65     ref_ = ref.ref_;
66     ref.env_ = nullptr;
67     ref.ref_ = nullptr;
68     return *this;
69 }
70 
~WeakRef()71 WeakRef::~WeakRef()
72 {
73     Reset();
74 }
75 
Reset()76 void WeakRef::Reset()
77 {
78     if (env_ && ref_) {
79         napi_delete_reference(env_, ref_);
80     }
81     env_ = nullptr;
82     ref_ = nullptr;
83 }
84 
IsEmpty() const85 bool WeakRef::IsEmpty() const
86 {
87     if (env_ && ref_) {
88         // possibly actually check the ref?
89         return false;
90     }
91     return true;
92 }
93 
GetEnv() const94 napi_env WeakRef::GetEnv() const
95 {
96     return env_;
97 }
98 
GetValue() const99 napi_value WeakRef::GetValue() const
100 {
101     if (env_ && ref_) {
102         napi_value value;
103         napi_get_reference_value(env_, ref_, &value);
104         return value;
105     }
106     return {};
107 }
108 
GetObject() const109 NapiApi::Object WeakRef::GetObject() const
110 {
111     if (env_ && ref_) {
112         napi_value value;
113         napi_get_reference_value(env_, ref_, &value);
114         return NapiApi::Object(env_, value);
115     }
116     return {};
117 }
118 
119 } // namespace NapiApi
120