• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
NapiFuncArg(napi_env env,napi_callback_info info)24 NapiFuncArg::NapiFuncArg(napi_env env, napi_callback_info info) : env_(env), info_(info)
25 {}
26 
~NapiFuncArg()27 NapiFuncArg::~NapiFuncArg()
28 {}
29 
SetArgc(size_t argc)30 void NapiFuncArg::SetArgc(size_t argc)
31 {
32     argc_ = argc;
33 }
34 
SetThisVar(napi_value thisVar)35 void NapiFuncArg::SetThisVar(napi_value thisVar)
36 {
37     thisVar_ = thisVar;
38 }
39 
GetArgc(void) const40 size_t NapiFuncArg::GetArgc(void) const
41 {
42     return argc_;
43 }
44 
GetThisVar(void) const45 napi_value NapiFuncArg::GetThisVar(void) const
46 {
47     return thisVar_;
48 }
49 
GetArg(size_t argPos) const50 napi_value NapiFuncArg::GetArg(size_t argPos) const
51 {
52     return (argPos < GetArgc()) ? argv_[argPos] : nullptr;
53 }
54 
operator [](size_t argPos) const55 napi_value NapiFuncArg::operator[](size_t argPos) const
56 {
57     return GetArg(argPos);
58 }
59 
InitArgs(std::function<bool ()> argcChecker)60 bool NapiFuncArg::InitArgs(std::function<bool()> argcChecker)
61 {
62     SetArgc(0);
63     argv_.reset();
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         APP_LOGE("Cannot get num of func args for %{public}d", status);
69         return false;
70     }
71 
72     if (argc) {
73         argv_ = std::make_unique<napi_value[]>(argc);
74         status = napi_get_cb_info(env_, info_, &argc, argv_.get(), &thisVar, nullptr);
75         if (status != napi_ok) {
76             APP_LOGE("Cannot get func args for %{public}d", status);
77             return false;
78         }
79     }
80 
81     SetArgc(argc);
82     SetThisVar(thisVar);
83     return argcChecker();
84 }
85 
InitArgs(size_t argc)86 bool NapiFuncArg::InitArgs(size_t argc)
87 {
88     return InitArgs([argc, this]() {
89         size_t realArgc = GetArgc();
90         if (argc != realArgc) {
91             APP_LOGE("Num of args recved eq %zu while expecting %{public}zu", realArgc, argc);
92             return false;
93         }
94         return true;
95     });
96 }
97 
InitArgs(size_t minArgc,size_t maxArgc)98 bool NapiFuncArg::InitArgs(size_t minArgc, size_t maxArgc)
99 {
100     return InitArgs([minArgc, maxArgc, this]() {
101         size_t realArgc = GetArgc();
102         if (minArgc > realArgc || maxArgc < realArgc) {
103             APP_LOGE("Num of args recved eq %zu while expecting %{public}zu ~ %{public}zu", realArgc, minArgc, maxArgc);
104             return false;
105         }
106         return true;
107     });
108 }
109 }  // namespace LIBZIP
110 }  // namespace AppExecFwk
111 }  // namespace OHOS