• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
SetThisVar(napi_value thisVar)32 void NFuncArg::SetThisVar(napi_value thisVar)
33 {
34     thisVar_ = thisVar;
35 }
36 
GetArgc(void) const37 size_t NFuncArg::GetArgc(void) const
38 {
39     return argc_;
40 }
41 
GetThisVar(void) const42 napi_value NFuncArg::GetThisVar(void) const
43 {
44     return thisVar_;
45 }
46 
GetArg(size_t argPos) const47 napi_value NFuncArg::GetArg(size_t argPos) const
48 {
49     return (argPos < GetArgc()) ? argv_[argPos] : nullptr;
50 }
51 
operator [](size_t argPos) const52 napi_value NFuncArg::operator[](size_t argPos) const
53 {
54     return GetArg(argPos);
55 }
56 
InitArgs(std::function<bool ()> argcChecker)57 bool NFuncArg::InitArgs(std::function<bool()> argcChecker)
58 {
59     SetArgc(0);
60     argv_.reset();
61 
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     if (argc) {
70         argv_ = make_unique<napi_value[]>(argc);
71         status = napi_get_cb_info(env_, info_, &argc, argv_.get(), &thisVar, nullptr);
72         if (status != napi_ok) {
73             HILOGE("Cannot get func args for %{public}d", status);
74             return false;
75         }
76     }
77     SetArgc(argc);
78     SetThisVar(thisVar);
79 
80     return argcChecker();
81 }
82 
InitArgs(size_t argc)83 bool NFuncArg::InitArgs(size_t argc)
84 {
85     return InitArgs([argc, this]() {
86         size_t realArgc = GetArgc();
87         if (argc != realArgc) {
88             HILOGE("Num of args recved eq %zu while expecting %{public}zu", realArgc, argc);
89             return false;
90         }
91         return true;
92     });
93 }
94 
InitArgs(size_t minArgc,size_t maxArgc)95 bool NFuncArg::InitArgs(size_t minArgc, size_t maxArgc)
96 {
97     return InitArgs([minArgc, maxArgc, this]() {
98         size_t realArgc = GetArgc();
99         if (minArgc > realArgc || maxArgc < realArgc) {
100             HILOGE("Num of args recved eq %zu while expecting %{public}zu ~ %{public}zu", realArgc, minArgc, maxArgc);
101             return false;
102         }
103         return true;
104     });
105 }
106 } // namespace DistributedFS
107 } // namespace OHOS
108