• 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 OHOS_ABILITY_RUNTIME_NAPI_COMMON_UTIL_H
17 #define OHOS_ABILITY_RUNTIME_NAPI_COMMON_UTIL_H
18 
19 #include "napi/native_api.h"
20 #include "napi/native_common.h"
21 #include "napi/native_node_api.h"
22 #include "napi_common_data.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 static constexpr int32_t DEFAULT_BUF_SIZE = 1024;
27 static constexpr int32_t ASYNC_RST_SIZE = 2;
28 
29 bool IsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType);
30 bool IsArrayForNapiValue(napi_env env, napi_value param, uint32_t &arraySize);
31 
32 napi_value WrapVoidToJS(napi_env env);
33 napi_value WrapUndefinedToJS(napi_env env);
34 
35 napi_value CreateJSObject(napi_env env);
36 
37 napi_value WrapInt32ToJS(napi_env env, int32_t value);
38 int UnwrapInt32FromJS(napi_env env, napi_value param, int defaultValue = 0);
39 bool UnwrapInt32FromJS2(napi_env env, napi_value param, int &value);
40 
41 napi_value WrapLongToJS(napi_env env, long value);
42 long UnwrapLongFromJS(napi_env env, napi_value param, long defaultValue = 0);
43 bool UnwrapLongFromJS2(napi_env env, napi_value param, long &value);
44 
45 napi_value WrapInt64ToJS(napi_env env, int64_t value);
46 int64_t UnwrapInt64FromJS(napi_env env, napi_value param, int64_t defaultValue = 0);
47 bool UnwrapInt64FromJS2(napi_env env, napi_value param, int64_t &value);
48 
49 napi_value WrapBoolToJS(napi_env env, bool value);
50 bool UnWrapBoolFromJS(napi_env env, napi_value param, bool defaultValue = false);
51 bool UnwrapBoolFromJS2(napi_env env, napi_value param, bool &value);
52 
53 napi_value WrapDoubleToJS(napi_env env, double value);
54 double UnWrapDoubleFromJS(napi_env env, napi_value param, double defaultValue = 0.0);
55 bool UnWrapDoubleFromJS2(napi_env env, napi_value param, double &value);
56 
57 napi_value WrapStringToJS(napi_env env, const std::string &value);
58 std::string UnwrapStringFromJS(napi_env env, napi_value param, const std::string &defaultValue = "");
59 bool UnwrapStringFromJS2(napi_env env, napi_value param, std::string &value);
60 
61 napi_value WrapArrayInt32ToJS(napi_env env, const std::vector<int> &value);
62 bool UnwrapArrayInt32FromJS(napi_env env, napi_value param, std::vector<int> &value);
63 
64 napi_value WrapArrayLongToJS(napi_env env, const std::vector<long> &value);
65 bool UnwrapArrayLongFromJS(napi_env env, napi_value param, std::vector<long> &value);
66 
67 napi_value WrapArrayInt64ToJS(napi_env env, const std::vector<int64_t> &value);
68 bool UnwrapArrayInt64FromJS(napi_env env, napi_value param, std::vector<int64_t> &value);
69 
70 napi_value WrapArrayDoubleToJS(napi_env env, const std::vector<double> &value);
71 bool UnwrapArrayDoubleFromJS(napi_env env, napi_value param, std::vector<double> &value);
72 
73 napi_value WrapArrayBoolToJS(napi_env env, const std::vector<bool> &value);
74 bool UnwrapArrayBoolFromJS(napi_env env, napi_value param, std::vector<bool> &value);
75 
76 napi_value WrapArrayStringToJS(napi_env env, const std::vector<std::string> &value);
77 bool UnwrapArrayStringFromJS(napi_env env, napi_value param, std::vector<std::string> &value);
78 
79 bool UnwrapArrayComplexFromJS(napi_env env, napi_value param, ComplexArrayData &value);
80 
81 /**
82  * @brief Indicates the specified attribute exists in the object passed by JS.
83  *
84  * @param env The environment that the Node-API call is invoked under.
85  * @param jsObject Indicates object passed by JS.
86  * @param propertyName Indicates the name of the property.
87  *
88  * @return Returns true if the attribute exists, else returns false.
89  */
90 bool IsExistsByPropertyName(napi_env env, napi_value jsObject, const char *propertyName);
91 
92 /**
93  * @brief Get the JSValue of the specified name from the JS object.
94  *
95  * @param env The environment that the Node-API call is invoked under.
96  * @param jsObject Indicates object passed by JS.
97  * @param propertyName Indicates the name of the property.
98  * @param expectType Indicates expected JS data type.
99  *
100  * @return Return the property value of the specified property name int jsObject on success, otherwise return nullptr.
101  */
102 napi_value GetPropertyValueByPropertyName(
103     napi_env env, napi_value jsObject, const char *propertyName, napi_valuetype expectType);
104 
105 bool SetPropertyValueByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, napi_value value);
106 
107 /**
108  * @brief Get the native number(int32) from the JSObject of the given property name.
109  *
110  * @param env The environment that the Node-API call is invoked under.
111  * @param jsObject Indicates object passed by JS.
112  * @param propertyName Indicates the name of the property.
113  * @param value Indicates the returned native value.
114  *
115  * @return Return true if successful, else return false.
116  */
117 bool UnwrapInt32ByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, int32_t &value);
118 
119 /**
120  * @brief Get the native number(double) from the JSObject of the given property name.
121  *
122  * @param env The environment that the Node-API call is invoked under.
123  * @param jsObject Indicates object passed by JS.
124  * @param propertyName Indicates the name of the property.
125  * @param value Indicates the returned native value.
126  *
127  * @return Return true if successful, else return false.
128  */
129 bool UnwrapDoubleByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, double &value);
130 
131 /**
132  * @brief Get the native boolean from the JSObject of the given property name.
133  *
134  * @param env The environment that the Node-API call is invoked under.
135  * @param jsObject Indicates object passed by JS.
136  * @param propertyName Indicates the name of the property.
137  * @param value Indicates the returned native value.
138  *
139  * @return Return true if successful, else return false.
140  */
141 bool UnwrapBooleanByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, bool &value);
142 bool UnwrapBooleanArrayByPropertyName(
143     napi_env env, napi_value jsObject, const char *propertyName, std::vector<bool> &value);
144 
145 /**
146  * @brief Get the native string from the JSObject of the given property name.
147  *
148  * @param env The environment that the Node-API call is invoked under.
149  * @param jsObject Indicates object passed by JS.
150  * @param propertyName Indicates the name of the property.
151  * @param value Indicates the returned native value.
152  *
153  * @return Return true if successful, else return false.
154  */
155 bool UnwrapStringByPropertyName(napi_env env, napi_value jsObject, const char *propertyName, std::string &value);
156 bool UnwrapStringArrayByPropertyName(
157     napi_env env, napi_value jsObject, const char *propertyName, std::vector<std::string> &value);
158 
159 bool UnwrapComplexArrayByPropertyName(
160     napi_env env, napi_value jsObject, const char *propertyName, ComplexArrayData &value);
161 
162 void ClearThreadReturnData(ThreadReturnData *data);
163 
164 napi_value GetCallbackErrorValue(napi_env env, int errCode);
165 
166 /**
167  * @brief Create asynchronous data.
168  *
169  * @param env The environment that the Node-API call is invoked under.
170  *
171  * @return Return a pointer to AsyncJSCallbackInfo on success, nullptr on failure
172  */
173 AsyncJSCallbackInfo *CreateAsyncJSCallbackInfo(napi_env env);
174 void FreeAsyncJSCallbackInfo(AsyncJSCallbackInfo **asyncCallbackInfo);
175 
176 /**
177  * @brief Convert local data to JS data.
178  *
179  * @param env The environment that the Node-API call is invoked under.
180  * @param data The local data.
181  * @param value the JS data.
182  *
183  * @return The return value from NAPI C++ to JS for the module.
184  */
185 bool WrapThreadReturnData(napi_env env, const ThreadReturnData *data, napi_value *value);
186 
187 /**
188  * @brief Create asynchronous data.
189  *
190  * @param env The environment that the Node-API call is invoked under.
191  * @param param Parameter list.
192  * @param callback Point to asynchronous processing of data.
193  *
194  * @return Return true successfully, otherwise return false.
195  */
196 bool CreateAsyncCallback(napi_env env, napi_value param, AsyncJSCallbackInfo *callback);
197 
198 napi_ref CreateCallbackRefFromJS(napi_env env, napi_value param);
199 
200 /**
201  * @brief Asynchronous callback processing.
202  *
203  * @param env The environment that the Node-API call is invoked under.
204  * @param asyncCallbackInfo Process data asynchronously.
205  * @param param other param.
206  *
207  * @return Return JS data successfully, otherwise return nullptr.
208  */
209 napi_value ExecuteAsyncCallbackWork(napi_env env, AsyncJSCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param);
210 
211 /**
212  * @brief Asynchronous promise processing.
213  *
214  * @param env The environment that the Node-API call is invoked under.
215  * @param asyncCallbackInfo Process data asynchronously.
216  * @param param other param.
217  *
218  * @return Return JS data successfully, otherwise return nullptr.
219  */
220 napi_value ExecutePromiseCallbackWork(napi_env env, AsyncJSCallbackInfo *asyncCallbackInfo, const AsyncParamEx *param);
221 
222 /**
223  * @brief The callback at the end of the asynchronous callback.
224  *
225  * @param env The environment that the Node-API call is invoked under.
226  * @param data Point to asynchronous processing of data.
227  */
228 void CompleteAsyncCallbackWork(napi_env env, napi_status status, void *data);
229 
230 /**
231  * @brief The callback at the end of the asynchronous callback.
232  *
233  * @param env The environment that the Node-API call is invoked under.
234  * @param data Point to asynchronous processing of data.
235  */
236 void CompleteAsyncVoidCallbackWork(napi_env env, napi_status status, void *data);
237 
238 /**
239  * @brief The callback at the end of the Promise callback.
240  *
241  * @param env The environment that the Node-API call is invoked under.
242  * @param data Point to asynchronous processing of data.
243  */
244 void CompletePromiseCallbackWork(napi_env env, napi_status status, void *data);
245 
246 /**
247  * @brief The callback at the end of the Promise callback.
248  *
249  * @param env The environment that the Node-API call is invoked under.
250  * @param data Point to asynchronous processing of data.
251  */
252 void CompletePromiseVoidCallbackWork(napi_env env, napi_status status, void *data);
253 
254 std::vector<uint8_t> ConvertU8Vector(napi_env env, napi_value jsValue);
255 
256 std::vector<std::string> ConvertStringVector(napi_env env, napi_value jsValue);
257 }  // namespace AppExecFwk
258 }  // namespace OHOS
259 #endif  // OHOS_ABILITY_RUNTIME_NAPI_COMMON_UTIL_H
260