• 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_NAPI_ACE_PLUGIN_UTIL_H
17 #define OHOS_NAPI_ACE_PLUGIN_UTIL_H
18 
19 #include "js_plugin_data.h"
20 #include "napi/native_api.h"
21 #include "napi/native_common.h"
22 #include "napi/native_node_api.h"
23 
24 namespace OHOS::Ace::Napi {
25 bool AceIsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType);
26 bool AceIsArrayForNapiValue(napi_env env, napi_value param, uint32_t& arraySize);
27 
28 napi_value AceWrapVoidToJS(napi_env env);
29 napi_value AceWrapUndefinedToJS(napi_env env);
30 
31 napi_value AceCreateJSObject(napi_env env);
32 
33 napi_value AceWrapInt32ToJS(napi_env env, int32_t value);
34 int AceUnwrapInt32FromJS(napi_env env, napi_value param, int defaultValue = 0);
35 bool AceUnwrapInt32FromJS2(napi_env env, napi_value param, int& value);
36 
37 napi_value AceWrapLongToJS(napi_env env, long value);
38 long AceUnwrapLongFromJS(napi_env env, napi_value param, long defaultValue = 0);
39 bool AceUnwrapLongFromJS2(napi_env env, napi_value param, long& value);
40 
41 napi_value AceWrapInt64ToJS(napi_env env, int64_t value);
42 int64_t AceUnwrapInt64FromJS(napi_env env, napi_value param, int64_t defaultValue = 0);
43 bool AceUnwrapInt64FromJS2(napi_env env, napi_value param, int64_t& value);
44 
45 napi_value AceWrapBoolToJS(napi_env env, bool value);
46 bool AceUnWrapBoolFromJS(napi_env env, napi_value param, bool defaultValue = false);
47 bool AceUnwrapBoolFromJS2(napi_env env, napi_value param, bool& value);
48 
49 napi_value AceWrapDoubleToJS(napi_env env, double value);
50 double AceUnWrapDoubleFromJS(napi_env env, napi_value param, double defaultValue = 0.0);
51 bool AceUnWrapDoubleFromJS2(napi_env env, napi_value param, double& value);
52 
53 napi_value AceWrapStringToJS(napi_env env, const std::string& value);
54 std::string AceUnwrapStringFromJS(napi_env env, napi_value param, const std::string& defaultValue = "");
55 bool AceUnwrapStringFromJS2(napi_env env, napi_value param, std::string& value);
56 
57 napi_value AceWrapArrayInt32ToJS(napi_env env, const std::vector<int>& value);
58 bool AceUnwrapArrayInt32FromJS(napi_env env, napi_value param, std::vector<int>& value);
59 
60 napi_value AceWrapArrayLongToJS(napi_env env, const std::vector<long>& value);
61 bool AceUnwrapArrayLongFromJS(napi_env env, napi_value param, std::vector<long>& value);
62 
63 napi_value AceWrapArrayInt64ToJS(napi_env env, const std::vector<int64_t>& value);
64 bool AceUnwrapArrayInt64FromJS(napi_env env, napi_value param, std::vector<int64_t>& value);
65 
66 napi_value AceWrapArrayDoubleToJS(napi_env env, const std::vector<double>& value);
67 bool AceUnwrapArrayDoubleFromJS(napi_env env, napi_value param, std::vector<double>& value);
68 
69 napi_value AceWrapArrayBoolToJS(napi_env env, const std::vector<bool>& value);
70 bool AceUnwrapArrayBoolFromJS(napi_env env, napi_value param, std::vector<bool>& value);
71 
72 napi_value AceWrapArrayStringToJS(napi_env env, const std::vector<std::string>& value);
73 bool AceUnwrapArrayStringFromJS(napi_env env, napi_value param, std::vector<std::string>& value);
74 
75 bool AceKVObjectToString(napi_env env, napi_value param, std::string& value);
76 napi_value AceStringToKVObject(napi_env env, const std::string& jsonString);
77 
78 bool AceUnwrapArrayComplexFromJS(napi_env env, napi_value param, ACEComplexArrayData& value);
79 
80 bool AceIsSameFuncFromJS(ACECallbackInfo& left, ACECallbackInfo& right);
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 AceIsExistsByPropertyName(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 AceGetPropertyValueByPropertyName(
103     napi_env env, napi_value jsObject, const char* propertyName, napi_valuetype expectType);
104 
105 bool AceSetPropertyValueByPropertyName(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 AceUnwrapInt32ByPropertyName(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 AceUnwrapDoubleByPropertyName(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 AceUnwrapBooleanByPropertyName(napi_env env, napi_value jsObject, const char* propertyName, bool& value);
142 bool AceUnwrapBooleanArrayByPropertyName(
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 AceUnwrapStringByPropertyName(napi_env env, napi_value jsObject, const char* propertyName, std::string& value);
156 bool AceUnwrapStringArrayByPropertyName(
157     napi_env env, napi_value jsObject, const char* propertyName, std::vector<std::string>& value);
158 
159 void AceClearThreadReturnData(ACEThreadReturnData* data);
160 
161 napi_value AceGetCallbackErrorValue(napi_env env, int errCode);
162 
163 /**
164  * @brief Create asynchronous data.
165  *
166  * @param env The environment that the Node-API call is invoked under.
167  *
168  * @return Return a pointer to ACEAsyncJSCallbackInfo on success, nullptr on failure
169  */
170 ACEAsyncJSCallbackInfo* AceCreateAsyncJSCallbackInfo(napi_env env);
171 void AceFreeAsyncJSCallbackInfo(ACEAsyncJSCallbackInfo** asyncCallbackInfo);
172 
173 /**
174  * @brief Convert local data to JS data.
175  *
176  * @param env The environment that the Node-API call is invoked under.
177  * @param data The local data.
178  * @param value the JS data.
179  *
180  * @return The return value from NAPI C++ to JS for the module.
181  */
182 bool AceWrapThreadReturnData(napi_env env, const ACEThreadReturnData* data, napi_value* value);
183 
184 /**
185  * @brief Create asynchronous data.
186  *
187  * @param env The environment that the Node-API call is invoked under.
188  * @param param Parameter list.
189  * @param callback Point to asynchronous processing of data.
190  *
191  * @return Return true successfully, otherwise return false.
192  */
193 bool AceCreateAsyncCallback(napi_env env, napi_value param, ACEAsyncJSCallbackInfo* callback);
194 
195 napi_ref AceCreateCallbackRefFromJS(napi_env env, napi_value param);
196 
197 /**
198  * @brief Asynchronous callback processing.
199  *
200  * @param env The environment that the Node-API call is invoked under.
201  * @param asyncCallbackInfo Process data asynchronously.
202  * @param param other param.
203  *
204  * @return Return JS data successfully, otherwise return nullptr.
205  */
206 napi_value AceExecuteAsyncCallbackWork(napi_env env, ACEAsyncJSCallbackInfo* asyncCallbackInfo,
207     const ACEAsyncParamEx* param);
208 
209 /**
210  * @brief Asynchronous promise processing.
211  *
212  * @param env The environment that the Node-API call is invoked under.
213  * @param asyncCallbackInfo Process data asynchronously.
214  * @param param other param.
215  *
216  * @return Return JS data successfully, otherwise return nullptr.
217  */
218 napi_value AceExecutePromiseCallbackWork(napi_env env, ACEAsyncJSCallbackInfo* asyncCallbackInfo,
219     const ACEAsyncParamEx* param);
220 
221 /**
222  * @brief The callback at the end of the asynchronous callback.
223  *
224  * @param env The environment that the Node-API call is invoked under.
225  * @param data Point to asynchronous processing of data.
226  */
227 void AceCompleteAsyncCallbackWork(napi_env env, napi_status status, void* data);
228 
229 /**
230  * @brief The callback at the end of the Promise callback.
231  *
232  * @param env The environment that the Node-API call is invoked under.
233  * @param data Point to asynchronous processing of data.
234  */
235 void AceCompletePromiseCallbackWork(napi_env env, napi_status status, void* data);
236 }  // namespace OHOS::Ace::Napi
237 #endif  // OHOS_NAPI_ACE_PLUGIN_UTIL_H
238