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