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 constexpr size_t BUF_SIZE = 1024;
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 { 1024 };
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, std::string>, int32_t> = 0>
NapiValueConverted(napi_env env,napi_value arg,T * str)85 napi_status NapiValueConverted(napi_env env, napi_value arg, T *str)
86 {
87 size_t result { 0 };
88 char buf[BUF_SIZE] = { 0 };
89 napi_status res = napi_get_value_string_utf8(env, arg, buf, BUF_SIZE, &result);
90 *str = std::string(buf);
91 return res;
92 }
93
94 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)95 napi_status NapiValueConverted(napi_env env, napi_value arg, T *npaiValue)
96 {
97 *npaiValue = arg;
98 return napi_ok;
99 }
100
101 template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int32_t> = 0>
GetNapiValue(napi_env env,T val)102 napi_value GetNapiValue(napi_env env, T val)
103 {
104 napi_value result = nullptr;
105 NAPI_CALL(env, napi_get_boolean(env, val, &result));
106 return result;
107 }
108
109 template<typename T, std::enable_if_t<std::is_same_v<T, int32_t>, int32_t> = 0>
GetNapiValue(napi_env env,T val)110 napi_value GetNapiValue(napi_env env, T val)
111 {
112 napi_value result = nullptr;
113 NAPI_CALL(env, napi_create_int32(env, val, &result));
114 return result;
115 }
116
117 template<typename T, std::enable_if_t<std::is_same_v<T, uint32_t>, int32_t> = 0>
GetNapiValue(napi_env env,T val)118 napi_value GetNapiValue(napi_env env, T val)
119 {
120 napi_value result = nullptr;
121 NAPI_CALL(env, napi_create_uint32(env, val, &result));
122 return result;
123 }
124
125 template<typename T, std::enable_if_t<std::is_same_v<T, int64_t>, int64_t> = 0>
GetNapiValue(napi_env env,T val)126 napi_value GetNapiValue(napi_env env, T val)
127 {
128 napi_value result = nullptr;
129 NAPI_CALL(env, napi_create_int64(env, val, &result));
130 return result;
131 }
132
133 template<typename T, std::enable_if_t<std::is_same_v<T, std::string>, int32_t> = 0>
GetNapiValue(napi_env env,const T & val)134 napi_value GetNapiValue(napi_env env, const T &val)
135 {
136 napi_value result = nullptr;
137 NAPI_CALL(env, napi_create_string_utf8(env, val.c_str(), val.length(), &result));
138 return result;
139 }
140
141 template<typename T, std::enable_if_t<std::is_same_v<T, char>, int32_t> = 0>
GetNapiValue(napi_env env,const T * val)142 napi_value GetNapiValue(napi_env env, const T *val)
143 {
144 napi_value result = nullptr;
145 NAPI_CALL(env, napi_create_string_utf8(env, val, NAPI_AUTO_LENGTH, &result));
146 return result;
147 }
148
149 template<typename T, std::enable_if_t<std::is_same_v<T, napi_value>, int32_t> = 0>
GetNapiValue(napi_env env,T val)150 napi_value GetNapiValue(napi_env env, T val)
151 {
152 return val;
153 }
154
155 template<typename T>
SetPropertyToNapiObject(napi_env env,napi_value object,std::string_view name,T value)156 void SetPropertyToNapiObject(napi_env env, napi_value object, std::string_view name, T value)
157 {
158 napi_value propertyValue = GetNapiValue(env, value);
159 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, object, name.data(), propertyValue));
160 }
161
162 template<typename T>
NapiValueToCppValue(napi_env env,napi_value arg,napi_valuetype argType,T * val)163 napi_status NapiValueToCppValue(napi_env env, napi_value arg, napi_valuetype argType, T *val)
164 {
165 napi_valuetype valueType = napi_undefined;
166 napi_typeof(env, arg, &valueType);
167 if (valueType == argType) {
168 return NapiValueConverted(env, arg, val);
169 }
170 return napi_invalid_arg;
171 }
172
173 template<typename T, std::enable_if_t<std::is_same_v<T, bool>, int32_t> = 0>
GetInputArgvType(T *)174 napi_valuetype GetInputArgvType(T *)
175 {
176 return napi_boolean;
177 }
178
179 template<typename... Ts, size_t N>
MatchParameters(napi_env env,const napi_value (& argv)[N],size_t argc,std::tuple<Ts...> & theTuple)180 std::optional<NapiError> MatchParameters(
181 napi_env env, const napi_value (&argv)[N], size_t argc, std::tuple<Ts...> &theTuple)
182 {
183 int32_t typeSize = sizeof...(Ts);
184 napi_valuetype valueType = napi_undefined;
185 napi_typeof(env, argv[typeSize - 1], &valueType);
186 if (valueType != napi_function) {
187 typeSize--;
188 }
189 std::vector<napi_valuetype> typeStd(typeSize, napi_undefined);
190
191 if (argc != typeStd.size()) {
192 return std::optional<NapiError>(ERROR_PARAMETER_COUNTS_INVALID);
193 }
194 bool typeMatched = true;
195 std::apply(
196 [argc, &argv, &typeStd](Ts &... tupleArgs) {
197 size_t index { 0 };
198 ((index < argc ? (typeStd[index++] = GetInputArgvType(tupleArgs)) : true), ...);
199 },
200 theTuple);
201 for (size_t i = 0; i < argc; i++) {
202 napi_valuetype valueType = napi_undefined;
203 napi_typeof(env, argv[i], &valueType);
204 if (valueType != typeStd[i]) {
205 typeMatched = false;
206 break;
207 }
208 }
209 if (typeMatched) {
210 std::apply(
211 [env, argc, &argv](Ts &... tupleArgs) {
212 size_t index { 0 };
213 ((index < argc ? NapiValueConverted(env, argv[index++], tupleArgs) : napi_ok), ...);
214 },
215 theTuple);
216 return std::nullopt;
217 }
218 return std::optional<NapiError>(ERROR_PARAMETER_TYPE_INVALID);
219 }
220 } // namespace Telephony
221 } // namespace OHOS
222 #endif
223