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_func_arg.h"
17
18 #include "n_error.h"
19 #include "filemgmt_libhilog.h"
20
21 namespace OHOS {
22 namespace FileManagement {
23 namespace LibN {
24 using namespace std;
25
NFuncArg(napi_env env,napi_callback_info info)26 NFuncArg::NFuncArg(napi_env env, napi_callback_info info) : env_(env), info_(info) {}
27
~NFuncArg()28 NFuncArg::~NFuncArg() {}
29
SetArgc(size_t argc)30 void NFuncArg::SetArgc(size_t argc)
31 {
32 argc_ = argc;
33 }
SetThisVar(napi_value thisVar)34 void NFuncArg::SetThisVar(napi_value thisVar)
35 {
36 thisVar_ = thisVar;
37 }
38
GetArgc(void) const39 size_t NFuncArg::GetArgc(void) const
40 {
41 return argc_;
42 }
43
GetThisVar(void) const44 napi_value NFuncArg::GetThisVar(void) const
45 {
46 return thisVar_;
47 }
48
GetArg(size_t argPos) const49 napi_value NFuncArg::GetArg(size_t argPos) const
50 {
51 return (argPos < GetArgc()) ? argv_[argPos] : nullptr;
52 }
53
operator [](size_t argPos) const54 napi_value NFuncArg::operator[](size_t argPos) const
55 {
56 return GetArg(argPos);
57 }
58
59 bool NFuncArg::InitArgs(std::function<bool()> argcChecker)
60 {
61 SetArgc(0);
62 argv_.reset();
63
64 size_t argc;
65 napi_value thisVar;
66 napi_status status = napi_get_cb_info(env_, info_, &argc, nullptr, &thisVar, nullptr);
67 if (status != napi_ok) {
68 HILOGE("Cannot get num of func args for %{public}d", status);
69 return false;
70 }
71 if (argc) {
72 argv_ = make_unique<napi_value[]>(argc);
73 status = napi_get_cb_info(env_, info_, &argc, argv_.get(), &thisVar, nullptr);
74 if (status != napi_ok) {
75 HILOGE("Cannot get func args for %{public}d", status);
76 return false;
77 }
78 }
79 SetArgc(argc);
80 SetThisVar(thisVar);
81
82 return argcChecker();
83 }
84
InitArgs(size_t argc)85 bool NFuncArg::InitArgs(size_t argc)
86 {
87 return InitArgs([argc, this]() {
88 size_t realArgc = GetArgc();
89 if (argc != realArgc) {
90 HILOGE("Num of args recved eq %zu while expecting %{public}zu", realArgc, argc);
91 return false;
92 }
93 return true;
94 });
95 }
96
InitArgs(size_t minArgc,size_t maxArgc)97 bool NFuncArg::InitArgs(size_t minArgc, size_t maxArgc)
98 {
99 return InitArgs([minArgc, maxArgc, this]() {
100 size_t realArgc = GetArgc();
101 if (minArgc > realArgc || maxArgc < realArgc) {
102 HILOGE("Num of args recved eq %zu while expecting %{public}zu ~ %{public}zu", realArgc, minArgc, maxArgc);
103 return false;
104 }
105 return true;
106 });
107 }
108 } // namespace LibN
109 } // namespace FileManagement
110 } // namespace OHOS
111