• 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 #ifndef CORE_SERVICE_COMMON_NAPI_NAPI_PARAMETER_UTIL_H
17 #define CORE_SERVICE_COMMON_NAPI_NAPI_PARAMETER_UTIL_H
18 
19 #include <tuple>
20 #include <type_traits>
21 
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 namespace {
28 template<typename T, std::enable_if_t<std::is_same_v<T, int32_t>, int32_t> = 0>
GetInputArgvType(T *)29 napi_valuetype GetInputArgvType(T *)
30 {
31     return napi_number;
32 }
33 
34 template<typename T, std::enable_if_t<std::is_same_v<T, napi_ref>, int32_t> = 0>
GetInputArgvType(T *)35 napi_valuetype GetInputArgvType(T *)
36 {
37     return napi_function;
38 }
39 
40 template<typename T, std::enable_if_t<std::is_same_v<T, char>, int32_t> = 0>
GetInputArgvType(T *)41 napi_valuetype GetInputArgvType(T *)
42 {
43     return napi_string;
44 }
45 
46 template<typename T, std::enable_if_t<std::is_same_v<T, std::string>, int32_t> = 0>
GetInputArgvType(T *)47 napi_valuetype GetInputArgvType(T *)
48 {
49     return napi_string;
50 }
51 
52 template<typename T, std::enable_if_t<std::is_same_v<T, napi_value>, int32_t> = 0>
GetInputArgvType(T *)53 napi_valuetype GetInputArgvType(T *)
54 {
55     return napi_object;
56 }
57 
58 template<typename T, std::enable_if_t<std::is_same_v<T, int32_t>, int32_t> = 0>
NapiValueConverted(napi_env env,napi_value arg,T * val)59 napi_status NapiValueConverted(napi_env env, napi_value arg, T *val)
60 {
61     return napi_get_value_int32(env, arg, val);
62 }
63 
64 template<typename T, std::enable_if_t<std::is_same_v<T, napi_ref>, int32_t> = 0>
NapiValueConverted(napi_env env,napi_value arg,T * ref)65 napi_status NapiValueConverted(napi_env env, napi_value arg, T *ref)
66 {
67     return napi_create_reference(env, arg, 1, ref);
68 }
69 
70 template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int32_t> = 0>
NapiValueConverted(napi_env env,napi_value arg,T * res)71 napi_status NapiValueConverted(napi_env env, napi_value arg, T *res)
72 {
73     return napi_get_value_bool(env, arg, res);
74 }
75 
76 template<typename T, std::enable_if_t<std::is_same_v<T, char>, int32_t> = 0>
NapiValueConverted(napi_env env,napi_value arg,T * buf)77 napi_status NapiValueConverted(napi_env env, napi_value arg, T *buf)
78 {
79     size_t result {0};
80     constexpr size_t bufSize {64};
81     return napi_get_value_string_utf8(env, arg, buf, bufSize, &result);
82 }
83 
84 template<typename T, std::enable_if_t<std::is_same_v<T, napi_value>, int32_t> = 0>
NapiValueConverted(napi_env env,napi_value arg,T * npaiValue)85 napi_status NapiValueConverted(napi_env env, napi_value arg, T *npaiValue)
86 {
87     *npaiValue = arg;
88     return napi_ok;
89 }
90 } // namespace
91 
92 template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int32_t> = 0>
GetNapiValue(napi_env env,T val)93 napi_value GetNapiValue(napi_env env, T val)
94 {
95     napi_value result = nullptr;
96     NAPI_CALL(env, napi_get_boolean(env, val, &result));
97     return result;
98 }
99 
100 template<typename T, std::enable_if_t<std::is_same_v<T, int32_t>, int32_t> = 0>
GetNapiValue(napi_env env,T val)101 napi_value GetNapiValue(napi_env env, T val)
102 {
103     napi_value result = nullptr;
104     NAPI_CALL(env, napi_create_int32(env, val, &result));
105     return result;
106 }
107 
108 template<typename T, std::enable_if_t<std::is_same_v<T, int64_t>, int64_t> = 0>
GetNapiValue(napi_env env,T val)109 napi_value GetNapiValue(napi_env env, T val)
110 {
111     napi_value result = nullptr;
112     NAPI_CALL(env, napi_create_int64(env, val, &result));
113     return result;
114 }
115 
116 template<typename T, std::enable_if_t<std::is_same_v<T, std::string>, int32_t> = 0>
GetNapiValue(napi_env env,const T & val)117 napi_value GetNapiValue(napi_env env, const T &val)
118 {
119     napi_value result = nullptr;
120     NAPI_CALL(env, napi_create_string_utf8(env, val.c_str(), val.length(), &result));
121     return result;
122 }
123 
124 template<typename T, std::enable_if_t<std::is_same_v<T, char>, int32_t> = 0>
GetNapiValue(napi_env env,const T * val)125 napi_value GetNapiValue(napi_env env, const T *val)
126 {
127     napi_value result = nullptr;
128     NAPI_CALL(env, napi_create_string_utf8(env, val, NAPI_AUTO_LENGTH, &result));
129     return result;
130 }
131 
132 template<typename T, std::enable_if_t<std::is_same_v<T, napi_value>, int32_t> = 0>
GetNapiValue(napi_env env,T val)133 napi_value GetNapiValue(napi_env env, T val)
134 {
135     return val;
136 }
137 
138 template<typename T>
SetPropertyToNapiObject(napi_env env,napi_value object,std::string_view name,T value)139 void SetPropertyToNapiObject(napi_env env, napi_value object, std::string_view name, T value)
140 {
141     napi_value propertyValue = GetNapiValue(env, value);
142     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name.data(), propertyValue));
143 }
144 
145 template<typename T>
NapiValueToCppValue(napi_env env,napi_value arg,napi_valuetype argType,T * val)146 napi_status NapiValueToCppValue(napi_env env, napi_value arg, napi_valuetype argType, T *val)
147 {
148     napi_valuetype valueType = napi_undefined;
149     napi_typeof(env, arg, &valueType);
150     if (valueType == argType) {
151         return NapiValueConverted(env, arg, val);
152     }
153     return napi_invalid_arg;
154 }
155 
156 template<typename... Ts, size_t N>
MatchParameters(napi_env env,const napi_value (& argv)[N],size_t argc,std::tuple<Ts...> & theTuple)157 std::optional<NapiError> MatchParameters(
158     napi_env env, const napi_value (&argv)[N], size_t argc, std::tuple<Ts...> &theTuple)
159 {
160     std::vector<napi_valuetype> typeStd(argc, napi_undefined);
161     std::apply(
162         [argc, &argv, &typeStd](Ts &...tupleArgs) {
163             size_t index {0};
164             ((index < argc ? (typeStd[index++] = GetInputArgvType(tupleArgs)) : true), ...);
165         },
166         theTuple);
167 
168     if (argc == typeStd.size()) {
169         bool typeMatched = true;
170         for (size_t i = 0; i < argc; i++) {
171             napi_valuetype valueType = napi_undefined;
172             napi_typeof(env, argv[i], &valueType);
173             typeMatched &= (valueType == typeStd[i]);
174         }
175 
176         if (typeMatched) {
177             std::apply(
178                 [env, argc, &argv](Ts &...tupleArgs) {
179                     size_t index {0};
180                     ((index < argc ? NapiValueConverted(env, argv[index++], tupleArgs) : napi_ok), ...);
181                 },
182                 theTuple);
183             return std::nullopt;
184         }
185         return std::optional<NapiError>(ERROR_PARAMETER_TYPE_INVALID);
186     }
187     return std::optional<NapiError>(ERROR_PARAMETER_COUNTS_INVALID);
188 }
189 } // namespace Telephony
190 } // namespace OHOS
191 #endif