1 /* 2 * Copyright (c) 2021-2022 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, int64_t value); 38 int64_t AceUnwrapLongFromJS(napi_env env, napi_value param, int64_t defaultValue = 0); 39 bool AceUnwrapLongFromJS2(napi_env env, napi_value param, int64_t& 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<int64_t>& value); 61 bool AceUnwrapArrayLongFromJS(napi_env env, napi_value param, std::vector<int64_t>& 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 void AceSetNamedPropertyByString(napi_env env, napi_value jsObject, const char* objName, const char* propName); 81 82 bool AceIsSameFuncFromJS(ACECallbackInfo& left, ACECallbackInfo& right); 83 /** 84 * @brief Indicates the specified attribute exists in the object passed by JS. 85 * 86 * @param env The environment that the Node-API call is invoked under. 87 * @param jsObject Indicates object passed by JS. 88 * @param propertyName Indicates the name of the property. 89 * 90 * @return Returns true if the attribute exists, else returns false. 91 */ 92 bool AceIsExistsByPropertyName(napi_env env, napi_value jsObject, const char* propertyName); 93 94 /** 95 * @brief Get the JSValue of the specified name from the JS object. 96 * 97 * @param env The environment that the Node-API call is invoked under. 98 * @param jsObject Indicates object passed by JS. 99 * @param propertyName Indicates the name of the property. 100 * @param expectType Indicates expected JS data type. 101 * 102 * @return Return the property value of the specified property name int jsObject on success, otherwise return nullptr. 103 */ 104 napi_value AceGetPropertyValueByPropertyName( 105 napi_env env, napi_value jsObject, const char* propertyName, napi_valuetype expectType); 106 107 bool AceSetPropertyValueByPropertyName(napi_env env, napi_value jsObject, const char* propertyName, napi_value value); 108 109 /** 110 * @brief Get the native number(int32) from the JSObject of the given property name. 111 * 112 * @param env The environment that the Node-API call is invoked under. 113 * @param jsObject Indicates object passed by JS. 114 * @param propertyName Indicates the name of the property. 115 * @param value Indicates the returned native value. 116 * 117 * @return Return true if successful, else return false. 118 */ 119 bool AceUnwrapInt32ByPropertyName(napi_env env, napi_value jsObject, const char* propertyName, int32_t& value); 120 121 /** 122 * @brief Get the native number(double) from the JSObject of the given property name. 123 * 124 * @param env The environment that the Node-API call is invoked under. 125 * @param jsObject Indicates object passed by JS. 126 * @param propertyName Indicates the name of the property. 127 * @param value Indicates the returned native value. 128 * 129 * @return Return true if successful, else return false. 130 */ 131 bool AceUnwrapDoubleByPropertyName(napi_env env, napi_value jsObject, const char* propertyName, double& value); 132 133 /** 134 * @brief Get the native boolean from the JSObject of the given property name. 135 * 136 * @param env The environment that the Node-API call is invoked under. 137 * @param jsObject Indicates object passed by JS. 138 * @param propertyName Indicates the name of the property. 139 * @param value Indicates the returned native value. 140 * 141 * @return Return true if successful, else return false. 142 */ 143 bool AceUnwrapBooleanByPropertyName(napi_env env, napi_value jsObject, const char* propertyName, bool& value); 144 bool AceUnwrapBooleanArrayByPropertyName( 145 napi_env env, napi_value jsObject, const char* propertyName, std::vector<bool>& value); 146 147 /** 148 * @brief Get the native string from the JSObject of the given property name. 149 * 150 * @param env The environment that the Node-API call is invoked under. 151 * @param jsObject Indicates object passed by JS. 152 * @param propertyName Indicates the name of the property. 153 * @param value Indicates the returned native value. 154 * 155 * @return Return true if successful, else return false. 156 */ 157 bool AceUnwrapStringByPropertyName(napi_env env, napi_value jsObject, const char* propertyName, std::string& value); 158 bool AceUnwrapStringArrayByPropertyName( 159 napi_env env, napi_value jsObject, const char* propertyName, std::vector<std::string>& value); 160 161 void AceClearThreadReturnData(ACEThreadReturnData* data); 162 163 napi_value AceGetCallbackErrorValue(napi_env env, int errCode); 164 165 /** 166 * @brief Create asynchronous data. 167 * 168 * @param env The environment that the Node-API call is invoked under. 169 * 170 * @return Return a pointer to ACEAsyncJSCallbackInfo on success, nullptr on failure 171 */ 172 ACEAsyncJSCallbackInfo* AceCreateAsyncJSCallbackInfo(napi_env env); 173 void AceFreeAsyncJSCallbackInfo(ACEAsyncJSCallbackInfo** asyncCallbackInfo); 174 175 /** 176 * @brief Convert local data to JS data. 177 * 178 * @param env The environment that the Node-API call is invoked under. 179 * @param data The local data. 180 * @param value the JS data. 181 * 182 * @return The return value from NAPI C++ to JS for the module. 183 */ 184 bool AceWrapThreadReturnData(napi_env env, const ACEThreadReturnData* data, napi_value* value); 185 186 /** 187 * @brief Create asynchronous data. 188 * 189 * @param env The environment that the Node-API call is invoked under. 190 * @param param Parameter list. 191 * @param callback Point to asynchronous processing of data. 192 * 193 * @return Return true successfully, otherwise return false. 194 */ 195 bool AceCreateAsyncCallback(napi_env env, napi_value param, ACEAsyncJSCallbackInfo* callback); 196 197 napi_ref AceCreateCallbackRefFromJS(napi_env env, napi_value param); 198 199 /** 200 * @brief The callback at the end of the asynchronous callback. 201 * 202 * @param env The environment that the Node-API call is invoked under. 203 * @param asyncCallbackInfo Point to asynchronous processing of data. 204 */ 205 void AceCompleteAsyncCallbackWork(napi_env env, ACEAsyncJSCallbackInfo* asyncCallbackInfo); 206 207 /** 208 * @brief The callback at the end of the Promise callback. 209 * 210 * @param env The environment that the Node-API call is invoked under. 211 * @param asyncCallbackInfo Point to asynchronous processing of data. 212 */ 213 void AceCompletePromiseCallbackWork(napi_env env, ACEAsyncJSCallbackInfo* asyncCallbackInfo); 214 } // namespace OHOS::Ace::Napi 215 #endif // OHOS_NAPI_ACE_PLUGIN_UTIL_H 216