1 /*
2  * Copyright (c) 2024 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 "napi_reference.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
20 namespace LIBZIP {
NapiReference()21 NapiReference::NapiReference()
22 {}
23 
NapiReference(NapiValue val)24 NapiReference::NapiReference(NapiValue val)
25 {
26     if (val) {
27         env_ = val.env_;
28         napi_create_reference(val.env_, val.val_, 1, &ref_);
29     }
30 }
31 
~NapiReference()32 NapiReference::~NapiReference()
33 {
34     if (ref_) {
35         napi_delete_reference(env_, ref_);
36     }
37 }
38 
39 NapiReference::operator bool() const
40 {
41     return ref_ != nullptr;
42 }
43 
Deref(napi_env env)44 NapiValue NapiReference::Deref(napi_env env)
45 {
46     if (!ref_) {
47         return NapiValue();
48     }
49 
50     napi_value val = nullptr;
51     napi_get_reference_value(env, ref_, &val);
52     return {env, val};
53 }
54 }  // namespace LIBZIP
55 }  // namespace AppExecFwk
56 }  // namespace OHOS