1 /* 2 * Copyright (c) 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 #ifndef OHOS_JS_UTIL_H 16 #define OHOS_JS_UTIL_H 17 18 #include "napi/native_node_api.h" 19 #include "object_types.h" 20 21 namespace OHOS::ObjectStore { 22 class JSUtil final { 23 public: 24 /* napi_value <-> bool */ 25 static napi_status GetValue(napi_env env, napi_value in, bool &out); 26 static napi_status SetValue(napi_env env, const bool &in, napi_value &out); 27 28 /* napi_value <-> double */ 29 static napi_status GetValue(napi_env env, napi_value in, double &out); 30 static napi_status SetValue(napi_env env, const double &in, napi_value &out); 31 32 /* napi_value <-> std::string */ 33 static napi_status GetValue(napi_env env, napi_value in, std::string &out); 34 static napi_status SetValue(napi_env env, const std::string &in, napi_value &out); 35 36 /* napi_value <-> int32_t */ 37 static napi_status GetValue(napi_env env, napi_value in, int32_t& out); 38 static napi_status SetValue(napi_env env, const int32_t& in, napi_value& out); 39 40 static napi_status GetValue(napi_env env, napi_value in, uint32_t& out); 41 static napi_status SetValue(napi_env env, const uint32_t& in, napi_value& out); 42 43 /* napi_value <-> int64_t */ 44 static napi_status GetValue(napi_env env, napi_value in, int64_t& out); 45 static napi_status SetValue(napi_env env, const int64_t& in, napi_value& out); 46 47 /* napi_value <-> std::vector<std::string> */ 48 static napi_status GetValue(napi_env env, napi_value in, std::vector<std::string> &out); 49 static napi_status SetValue(napi_env env, const std::vector<std::string> &in, napi_value &out); 50 51 /* napi_value <-> std::vector<uint8_t> */ 52 static napi_status GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out); 53 static napi_status SetValue(napi_env env, const std::vector<uint8_t> &in, napi_value &out); 54 55 static napi_status GetValue(napi_env env, napi_value in, Assets &assets); 56 57 static napi_status GetValue(napi_env env, napi_value in, Asset &asset); 58 59 static napi_status GetValue(napi_env env, napi_value in, AssetBindInfo &out); 60 61 static napi_status GetValue(napi_env env, napi_value in, ValuesBucket &out); 62 63 static napi_status GetValue(napi_env env, napi_value jsValue, std::monostate &out); 64 65 static void GenerateNapiError(napi_env env, int32_t status, int32_t &errCode, std::string &errMessage); 66 67 static bool IsNull(napi_env env, napi_value value); 68 69 template <typename T> 70 static inline napi_status GetNamedProperty(napi_env env, napi_value in, const std::string& prop, 71 T& value, bool optional = false) 72 { 73 bool hasProp = false; 74 napi_status status = napi_has_named_property(env, in, prop.c_str(), &hasProp); 75 if (!hasProp) { 76 status = optional ? napi_ok : napi_generic_failure; 77 return status; 78 } 79 80 if ((status == napi_ok) && hasProp) { 81 napi_value inner = nullptr; 82 status = napi_get_named_property(env, in, prop.c_str(), &inner); 83 if (!optional && IsNull(env, inner)) { 84 return napi_generic_failure; 85 } 86 if ((status == napi_ok) && (inner != nullptr)) { 87 return GetValue(env, inner, value); 88 } 89 } 90 return napi_invalid_arg; 91 }; 92 93 template<typename T> GetValues(napi_env env,napi_value jsValue,T & value)94 static napi_status GetValues(napi_env env, napi_value jsValue, T &value) 95 { 96 return napi_invalid_arg; 97 } 98 99 template<typename T, typename First, typename... Types> GetValues(napi_env env,napi_value jsValue,T & value)100 static napi_status GetValues(napi_env env, napi_value jsValue, T &value) 101 { 102 First cValue; 103 auto ret = GetValue(env, jsValue, cValue); 104 if (ret == napi_ok) { 105 value = cValue; 106 return ret; 107 } 108 return GetValues<T, Types...>(env, jsValue, value); 109 } 110 111 template<typename... Types> GetValue(napi_env env,napi_value jsValue,std::variant<Types...> & value)112 static napi_status GetValue(napi_env env, napi_value jsValue, std::variant<Types...> &value) 113 { 114 napi_valuetype type; 115 napi_status status = napi_typeof(env, jsValue, &type); 116 if (status != napi_ok) { 117 return napi_invalid_arg; 118 } 119 if (type == napi_undefined) { 120 return napi_generic_failure; 121 } 122 123 return GetValues<decltype(value), Types...>(env, jsValue, value); 124 }; 125 }; 126 127 #define NAPI_ASSERT_ERRCODE_V9(env, assertion, version, err) \ 128 do { \ 129 if (!(assertion)) { \ 130 if ((version) >= 9) { \ 131 napi_throw_error((env), std::to_string((err)->GetCode()).c_str(), (err)->GetMessage().c_str()); \ 132 } \ 133 return nullptr; \ 134 } \ 135 } while (0) 136 137 #define NAPI_ASSERT_ERRCODE(env, condition, err) \ 138 do { \ 139 if (!(condition)) { \ 140 napi_throw_error((env), std::to_string((err)->GetCode()).c_str(), (err)->GetMessage().c_str()); \ 141 return nullptr; \ 142 } \ 143 } while (0) 144 145 #define CHECH_STATUS_RETURN_VOID(env, condition, ctxt, info) \ 146 do { \ 147 if (!(condition)) { \ 148 LOG_ERROR(info); \ 149 (ctxt)->status = napi_generic_failure; \ 150 (ctxt)->message = std::string(info); \ 151 return; \ 152 } \ 153 } while (0) 154 155 #define LOG_ERROR_RETURN(condition, message, retVal) \ 156 do { \ 157 if (!(condition)) { \ 158 LOG_ERROR("test (" #condition ") failed: " message); \ 159 return retVal; \ 160 } \ 161 } while (0) 162 163 #define LOG_ERROR_RETURN_VOID(condition, message) \ 164 do { \ 165 if (!(condition)) { \ 166 LOG_ERROR("test (" #condition ") failed: " message); \ 167 return; \ 168 } \ 169 } while (0) 170 } // namespace OHOS::ObjectStore 171 #endif // OHOS_JS_UTIL_H 172