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