• 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 <iostream>
19 #include <sstream>
20 
21 #include "../log.h"
22 #include "../uni_error.h"
23 
24 namespace OHOS {
25 namespace DistributedFS {
26 using namespace std;
27 
NFuncArg(napi_env env,napi_callback_info info)28 NFuncArg::NFuncArg(napi_env env, napi_callback_info info) : env_(env), info_(info) {}
29 
~NFuncArg()30 NFuncArg::~NFuncArg() {}
31 
SetArgc(size_t argc)32 void NFuncArg::SetArgc(size_t argc)
33 {
34     argc_ = argc;
35 }
SetThisVar(napi_value thisVar)36 void NFuncArg::SetThisVar(napi_value thisVar)
37 {
38     thisVar_ = thisVar;
39 }
40 
GetArgc(void) const41 size_t NFuncArg::GetArgc(void) const
42 {
43     return argc_;
44 }
45 
GetThisVar(void) const46 napi_value NFuncArg::GetThisVar(void) const
47 {
48     return thisVar_;
49 }
50 
GetArg(size_t argPos) const51 napi_value NFuncArg::GetArg(size_t argPos) const
52 {
53     return (argPos < GetArgc()) ? argv_[argPos] : nullptr;
54 }
55 
operator [](size_t argPos) const56 napi_value NFuncArg::operator[](size_t argPos) const
57 {
58     return GetArg(argPos);
59 }
60 
InitArgs(std::function<bool ()> argcChecker)61 bool NFuncArg::InitArgs(std::function<bool()> argcChecker)
62 {
63     SetArgc(0);
64     argv_.reset();
65 
66     size_t argc;
67     napi_value thisVar;
68     napi_status status = napi_get_cb_info(env_, info_, &argc, nullptr, &thisVar, nullptr);
69     if (status != napi_ok) {
70         HILOGE("Cannot get num of func args for %{public}d", status);
71         return false;
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             HILOGE("Cannot get func args for %{public}d", status);
78             return false;
79         }
80     }
81     SetArgc(argc);
82     SetThisVar(thisVar);
83 
84     return argcChecker();
85 }
86 
InitArgs(size_t argc)87 bool NFuncArg::InitArgs(size_t argc)
88 {
89     return InitArgs([argc, this]() {
90         size_t realArgc = GetArgc();
91         if (argc != realArgc) {
92             HILOGE("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 NFuncArg::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             HILOGE("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 DistributedFS
111 } // namespace OHOS
112