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