• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "n_ref.h"
17 #include "js_native_api.h"
18 
19 namespace OHOS {
20 namespace FileManagement {
21 namespace LibN {
NRef()22 NRef::NRef() {}
23 
NRef(NVal val)24 NRef::NRef(NVal val)
25 {
26     if (val) {
27         env_ = val.env_;
28         napi_create_reference(val.env_, val.val_, 1, &ref_);
29     }
30 }
31 
~NRef()32 NRef::~NRef()
33 {
34     if (ref_) {
35         napi_delete_reference(env_, ref_);
36     }
37 }
38 
operator bool() const39 NRef::operator bool() const
40 {
41     return ref_ != nullptr;
42 }
43 
Deref(napi_env env)44 NVal NRef::Deref(napi_env env)
45 {
46     if (!ref_) {
47         return NVal();
48     }
49 
50     napi_value val = nullptr;
51     napi_get_reference_value(env, ref_, &val);
52     return {env, val};
53 }
54 } // namespace LibN
55 } // namespace FileManagement
56 } // namespace OHOS
57