1bc03f14fSopenharmony_ci/* 2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License. 5bc03f14fSopenharmony_ci * You may obtain a copy of the License at 6bc03f14fSopenharmony_ci * 7bc03f14fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bc03f14fSopenharmony_ci * 9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and 13bc03f14fSopenharmony_ci * limitations under the License. 14bc03f14fSopenharmony_ci */ 15bc03f14fSopenharmony_ci#include "napi_data_utils.h" 16bc03f14fSopenharmony_ci 17bc03f14fSopenharmony_ci#include "napi_queue.h" 18bc03f14fSopenharmony_ci 19bc03f14fSopenharmony_ciusing namespace OHOS::MiscServices; 20bc03f14fSopenharmony_cinamespace OHOS { 21bc03f14fSopenharmony_cinamespace MiscServicesNapi { 22bc03f14fSopenharmony_ciconstexpr int32_t STR_MAX_LENGTH = 4096; 23bc03f14fSopenharmony_ciconstexpr size_t STR_TAIL_LENGTH = 1; 24bc03f14fSopenharmony_ci 25bc03f14fSopenharmony_ci/* napi_value <-> bool */ 26bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, bool &out) 27bc03f14fSopenharmony_ci{ 28bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- bool"); 29bc03f14fSopenharmony_ci return napi_get_value_bool(env, in, &out); 30bc03f14fSopenharmony_ci} 31bc03f14fSopenharmony_ci 32bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const bool &in, napi_value &out) 33bc03f14fSopenharmony_ci{ 34bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> bool"); 35bc03f14fSopenharmony_ci return napi_get_boolean(env, in, &out); 36bc03f14fSopenharmony_ci} 37bc03f14fSopenharmony_ci 38bc03f14fSopenharmony_ci/* napi_value <-> int32_t */ 39bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, int32_t &out) 40bc03f14fSopenharmony_ci{ 41bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> int32_t"); 42bc03f14fSopenharmony_ci return napi_get_value_int32(env, in, &out); 43bc03f14fSopenharmony_ci} 44bc03f14fSopenharmony_ci 45bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const int32_t &in, napi_value &out) 46bc03f14fSopenharmony_ci{ 47bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- int32_t"); 48bc03f14fSopenharmony_ci return napi_create_int32(env, in, &out); 49bc03f14fSopenharmony_ci} 50bc03f14fSopenharmony_ci 51bc03f14fSopenharmony_ci/* napi_value <-> int64_t */ 52bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, int64_t &out) 53bc03f14fSopenharmony_ci{ 54bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> int64_t"); 55bc03f14fSopenharmony_ci return napi_get_value_int64(env, in, &out); 56bc03f14fSopenharmony_ci} 57bc03f14fSopenharmony_ci 58bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const int64_t &in, napi_value &out) 59bc03f14fSopenharmony_ci{ 60bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- int64_t"); 61bc03f14fSopenharmony_ci return napi_create_int64(env, in, &out); 62bc03f14fSopenharmony_ci} 63bc03f14fSopenharmony_ci 64bc03f14fSopenharmony_ci/* napi_value <-> float */ 65bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, float &out) 66bc03f14fSopenharmony_ci{ 67bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> float"); 68bc03f14fSopenharmony_ci double tmp; 69bc03f14fSopenharmony_ci napi_status status = napi_get_value_double(env, in, &tmp); 70bc03f14fSopenharmony_ci out = tmp; 71bc03f14fSopenharmony_ci return status; 72bc03f14fSopenharmony_ci} 73bc03f14fSopenharmony_ci 74bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const float &in, napi_value &out) 75bc03f14fSopenharmony_ci{ 76bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- float"); 77bc03f14fSopenharmony_ci double tmp = in; 78bc03f14fSopenharmony_ci return napi_create_double(env, tmp, &out); 79bc03f14fSopenharmony_ci} 80bc03f14fSopenharmony_ci 81bc03f14fSopenharmony_ci/* napi_value <-> double */ 82bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, double &out) 83bc03f14fSopenharmony_ci{ 84bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> double"); 85bc03f14fSopenharmony_ci return napi_get_value_double(env, in, &out); 86bc03f14fSopenharmony_ci} 87bc03f14fSopenharmony_ci 88bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const double &in, napi_value &out) 89bc03f14fSopenharmony_ci{ 90bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- double"); 91bc03f14fSopenharmony_ci return napi_create_double(env, in, &out); 92bc03f14fSopenharmony_ci} 93bc03f14fSopenharmony_ci 94bc03f14fSopenharmony_ci/* napi_value <-> std::string */ 95bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::string &out) 96bc03f14fSopenharmony_ci{ 97bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- string"); 98bc03f14fSopenharmony_ci napi_valuetype type = napi_undefined; 99bc03f14fSopenharmony_ci napi_status status = napi_typeof(env, in, &type); 100bc03f14fSopenharmony_ci if (!((status == napi_ok) && (type == napi_string))) { 101bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_typeof failed, type=%{public}d status=%{public}d", 102bc03f14fSopenharmony_ci static_cast<int32_t>(type), status); 103bc03f14fSopenharmony_ci return napi_invalid_arg; 104bc03f14fSopenharmony_ci } 105bc03f14fSopenharmony_ci 106bc03f14fSopenharmony_ci size_t maxLen = STR_MAX_LENGTH; 107bc03f14fSopenharmony_ci status = napi_get_value_string_utf8(env, in, NULL, 0, &maxLen); 108bc03f14fSopenharmony_ci if (maxLen == 0) { 109bc03f14fSopenharmony_ci return status; 110bc03f14fSopenharmony_ci } 111bc03f14fSopenharmony_ci char *buf = new (std::nothrow) char[maxLen + STR_TAIL_LENGTH]; 112bc03f14fSopenharmony_ci if (buf != nullptr) { 113bc03f14fSopenharmony_ci size_t len = 0; 114bc03f14fSopenharmony_ci status = napi_get_value_string_utf8(env, in, buf, maxLen + STR_TAIL_LENGTH, &len); 115bc03f14fSopenharmony_ci if (status == napi_ok) { 116bc03f14fSopenharmony_ci buf[len] = 0; 117bc03f14fSopenharmony_ci out = std::string(buf); 118bc03f14fSopenharmony_ci } 119bc03f14fSopenharmony_ci delete[] buf; 120bc03f14fSopenharmony_ci } else { 121bc03f14fSopenharmony_ci status = napi_generic_failure; 122bc03f14fSopenharmony_ci } 123bc03f14fSopenharmony_ci return status; 124bc03f14fSopenharmony_ci} 125bc03f14fSopenharmony_ci 126bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::string &in, napi_value &out) 127bc03f14fSopenharmony_ci{ 128bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::string %{public}d", (int)in.length()); 129bc03f14fSopenharmony_ci return napi_create_string_utf8(env, in.c_str(), in.size(), &out); 130bc03f14fSopenharmony_ci} 131bc03f14fSopenharmony_ci 132bc03f14fSopenharmony_ci/* napi_value <-> std::vector<std::string> */ 133bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::vector<std::string> &out) 134bc03f14fSopenharmony_ci{ 135bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::vector<std::string>"); 136bc03f14fSopenharmony_ci bool isArray = false; 137bc03f14fSopenharmony_ci napi_is_array(env, in, &isArray); 138bc03f14fSopenharmony_ci if (!isArray) { 139bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_value is not an array"); 140bc03f14fSopenharmony_ci return napi_invalid_arg; 141bc03f14fSopenharmony_ci } 142bc03f14fSopenharmony_ci 143bc03f14fSopenharmony_ci uint32_t length = 0; 144bc03f14fSopenharmony_ci napi_status status = napi_get_array_length(env, in, &length); 145bc03f14fSopenharmony_ci if (!((status == napi_ok) && (length > 0))) { 146bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "get_array failed, status=%{public}d length=%{public}u", 147bc03f14fSopenharmony_ci status, length); 148bc03f14fSopenharmony_ci return napi_invalid_arg; 149bc03f14fSopenharmony_ci } 150bc03f14fSopenharmony_ci for (uint32_t i = 0; i < length; ++i) { 151bc03f14fSopenharmony_ci napi_value item = nullptr; 152bc03f14fSopenharmony_ci status = napi_get_element(env, in, i, &item); 153bc03f14fSopenharmony_ci if (!((item != nullptr) && (status == napi_ok))) { 154bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_get_element failed, status=%{public}d", status); 155bc03f14fSopenharmony_ci return napi_invalid_arg; 156bc03f14fSopenharmony_ci } 157bc03f14fSopenharmony_ci std::string value; 158bc03f14fSopenharmony_ci status = GetValue(env, item, value); 159bc03f14fSopenharmony_ci if (status != napi_ok) { 160bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_value is nota string, status=%{public}d", status); 161bc03f14fSopenharmony_ci return napi_invalid_arg; 162bc03f14fSopenharmony_ci } 163bc03f14fSopenharmony_ci out.push_back(value); 164bc03f14fSopenharmony_ci } 165bc03f14fSopenharmony_ci return status; 166bc03f14fSopenharmony_ci} 167bc03f14fSopenharmony_ci 168bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::vector<std::string> &in, napi_value &out) 169bc03f14fSopenharmony_ci{ 170bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::vector<std::string>"); 171bc03f14fSopenharmony_ci napi_status status = napi_create_array_with_length(env, in.size(), &out); 172bc03f14fSopenharmony_ci if (status != napi_ok) { 173bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "create array failed, status=%{public}d", status); 174bc03f14fSopenharmony_ci return status; 175bc03f14fSopenharmony_ci } 176bc03f14fSopenharmony_ci int index = 0; 177bc03f14fSopenharmony_ci for (auto &item : in) { 178bc03f14fSopenharmony_ci napi_value element = nullptr; 179bc03f14fSopenharmony_ci SetValue(env, item, element); 180bc03f14fSopenharmony_ci status = napi_set_element(env, out, index++, element); 181bc03f14fSopenharmony_ci if (status != napi_ok) { 182bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "napi_set_element failed, status=%{public}d", status); 183bc03f14fSopenharmony_ci return status; 184bc03f14fSopenharmony_ci } 185bc03f14fSopenharmony_ci } 186bc03f14fSopenharmony_ci return status; 187bc03f14fSopenharmony_ci} 188bc03f14fSopenharmony_ci 189bc03f14fSopenharmony_ci/* napi_value <-> std::vector<uint8_t> */ 190bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::vector<uint8_t> &out) 191bc03f14fSopenharmony_ci{ 192bc03f14fSopenharmony_ci out.clear(); 193bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::vector<uint8_t> "); 194bc03f14fSopenharmony_ci napi_typedarray_type type = napi_biguint64_array; 195bc03f14fSopenharmony_ci size_t length = 0; 196bc03f14fSopenharmony_ci napi_value buffer = nullptr; 197bc03f14fSopenharmony_ci size_t offset = 0; 198bc03f14fSopenharmony_ci void *data = nullptr; 199bc03f14fSopenharmony_ci napi_status status = napi_get_typedarray_info(env, in, &type, &length, &data, &buffer, &offset); 200bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, 201bc03f14fSopenharmony_ci "array type=%{public}d length=%{public}d offset=%{public}d status=%{public}d", 202bc03f14fSopenharmony_ci (int)type, (int)length, (int)offset, status); 203bc03f14fSopenharmony_ci if (!((status == napi_ok) && (length > 0) && (type == napi_uint8_array) && (data != nullptr))) { 204bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, 205bc03f14fSopenharmony_ci "array type=%{public}d length=%{public}d status=%{public}d", 206bc03f14fSopenharmony_ci static_cast<int32_t>(type), static_cast<int32_t>(length), status); 207bc03f14fSopenharmony_ci return napi_invalid_arg; 208bc03f14fSopenharmony_ci } 209bc03f14fSopenharmony_ci out.assign(reinterpret_cast<uint8_t *>(data), reinterpret_cast<uint8_t *>(data) + length); 210bc03f14fSopenharmony_ci return status; 211bc03f14fSopenharmony_ci} 212bc03f14fSopenharmony_ci 213bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::vector<uint8_t> &in, napi_value &out) 214bc03f14fSopenharmony_ci{ 215bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::vector<uint8_t> "); 216bc03f14fSopenharmony_ci if (in.size() <= 0) { 217bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "invalid std::vector<uint8_t>"); 218bc03f14fSopenharmony_ci return napi_invalid_arg; 219bc03f14fSopenharmony_ci } 220bc03f14fSopenharmony_ci void *data = nullptr; 221bc03f14fSopenharmony_ci napi_value buffer = nullptr; 222bc03f14fSopenharmony_ci napi_status status = napi_create_arraybuffer(env, in.size(), &data, &buffer); 223bc03f14fSopenharmony_ci if (status != napi_ok) { 224bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "create array buffer failed, status=%{public}d", status); 225bc03f14fSopenharmony_ci return status; 226bc03f14fSopenharmony_ci } 227bc03f14fSopenharmony_ci 228bc03f14fSopenharmony_ci if (memcpy_s(data, in.size(), in.data(), in.size()) != EOK) { 229bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "memcpy_s not EOK"); 230bc03f14fSopenharmony_ci return napi_invalid_arg; 231bc03f14fSopenharmony_ci } 232bc03f14fSopenharmony_ci status = napi_create_typedarray(env, napi_uint8_array, in.size(), buffer, 0, &out); 233bc03f14fSopenharmony_ci if (status != napi_ok) { 234bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, 235bc03f14fSopenharmony_ci "napi_value <- std::vector<uint8_t> invalid value, status=%{public}d", status); 236bc03f14fSopenharmony_ci return status; 237bc03f14fSopenharmony_ci } 238bc03f14fSopenharmony_ci return status; 239bc03f14fSopenharmony_ci} 240bc03f14fSopenharmony_ci 241bc03f14fSopenharmony_ci/* napi_value <-> std::map<std::string, int32_t> */ 242bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::map<std::string, int32_t> &out) 243bc03f14fSopenharmony_ci{ 244bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::map<std::string, int32_t> "); 245bc03f14fSopenharmony_ci (void)(env); 246bc03f14fSopenharmony_ci (void)(in); 247bc03f14fSopenharmony_ci (void)(out); 248bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "std::map<std::string, uint32_t> from napi_value, unsupported!"); 249bc03f14fSopenharmony_ci return napi_invalid_arg; 250bc03f14fSopenharmony_ci} 251bc03f14fSopenharmony_ci 252bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::map<std::string, int32_t> &in, napi_value &out) 253bc03f14fSopenharmony_ci{ 254bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::map<std::string, int32_t> "); 255bc03f14fSopenharmony_ci napi_status status = napi_create_array_with_length(env, in.size(), &out); 256bc03f14fSopenharmony_ci if (status != napi_ok) { 257bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "invalid object, status=%{public}d", status); 258bc03f14fSopenharmony_ci return status; 259bc03f14fSopenharmony_ci } 260bc03f14fSopenharmony_ci int index = 0; 261bc03f14fSopenharmony_ci for (const auto &[key, value] : in) { 262bc03f14fSopenharmony_ci napi_value element = nullptr; 263bc03f14fSopenharmony_ci napi_create_array_with_length(env, TUPLE_SIZE, &element); 264bc03f14fSopenharmony_ci napi_value jsKey = nullptr; 265bc03f14fSopenharmony_ci napi_create_string_utf8(env, key.c_str(), key.size(), &jsKey); 266bc03f14fSopenharmony_ci napi_set_element(env, element, TUPLE_KEY, jsKey); 267bc03f14fSopenharmony_ci napi_value jsValue = nullptr; 268bc03f14fSopenharmony_ci napi_create_int32(env, static_cast<int32_t>(value), &jsValue); 269bc03f14fSopenharmony_ci napi_set_element(env, element, TUPLE_VALUE, jsValue); 270bc03f14fSopenharmony_ci napi_set_element(env, out, index++, element); 271bc03f14fSopenharmony_ci } 272bc03f14fSopenharmony_ci return status; 273bc03f14fSopenharmony_ci} 274bc03f14fSopenharmony_ci 275bc03f14fSopenharmony_ci/* napi_value <-> std::map<std::string, int64_t> */ 276bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::map<std::string, int64_t> &out) 277bc03f14fSopenharmony_ci{ 278bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::map<std::string, int64_t> "); 279bc03f14fSopenharmony_ci (void)(env); 280bc03f14fSopenharmony_ci (void)(in); 281bc03f14fSopenharmony_ci (void)(out); 282bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "std::map<std::string, int64_t> from napi_value, unsupported!"); 283bc03f14fSopenharmony_ci return napi_invalid_arg; 284bc03f14fSopenharmony_ci} 285bc03f14fSopenharmony_ci 286bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::map<std::string, int64_t> &in, napi_value &out) 287bc03f14fSopenharmony_ci{ 288bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::map<std::string, int64_t> "); 289bc03f14fSopenharmony_ci napi_status status = napi_create_array_with_length(env, in.size(), &out); 290bc03f14fSopenharmony_ci if (status != napi_ok) { 291bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "invalid object, status=%{public}d", status); 292bc03f14fSopenharmony_ci return status; 293bc03f14fSopenharmony_ci } 294bc03f14fSopenharmony_ci int index = 0; 295bc03f14fSopenharmony_ci for (const auto &[key, value] : in) { 296bc03f14fSopenharmony_ci napi_value element = nullptr; 297bc03f14fSopenharmony_ci napi_create_array_with_length(env, TUPLE_SIZE, &element); 298bc03f14fSopenharmony_ci napi_value jsKey = nullptr; 299bc03f14fSopenharmony_ci napi_create_string_utf8(env, key.c_str(), key.size(), &jsKey); 300bc03f14fSopenharmony_ci napi_set_element(env, element, TUPLE_KEY, jsKey); 301bc03f14fSopenharmony_ci napi_value jsValue = nullptr; 302bc03f14fSopenharmony_ci napi_create_int64(env, static_cast<int64_t>(value), &jsValue); 303bc03f14fSopenharmony_ci napi_set_element(env, element, TUPLE_VALUE, jsValue); 304bc03f14fSopenharmony_ci napi_set_element(env, out, index++, element); 305bc03f14fSopenharmony_ci } 306bc03f14fSopenharmony_ci return status; 307bc03f14fSopenharmony_ci} 308bc03f14fSopenharmony_ci 309bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::shared_ptr<OHOS::Media::PixelMap> &pixelMap) 310bc03f14fSopenharmony_ci{ 311bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::shared_ptr<OHOS::Media::PixelMap>"); 312bc03f14fSopenharmony_ci pixelMap = OHOS::Media::PixelMapNapi::GetPixelMap(env, in); 313bc03f14fSopenharmony_ci return napi_ok; 314bc03f14fSopenharmony_ci} 315bc03f14fSopenharmony_ci 316bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::shared_ptr<OHOS::Media::PixelMap> &in, napi_value &out) 317bc03f14fSopenharmony_ci{ 318bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::shared_ptr<OHOS::Media::PixelMap>"); 319bc03f14fSopenharmony_ci out = OHOS::Media::PixelMapNapi::CreatePixelMap(env, in); 320bc03f14fSopenharmony_ci return napi_ok; 321bc03f14fSopenharmony_ci} 322bc03f14fSopenharmony_ci 323bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::shared_ptr<OHOS::AAFwk::Want> &wantPtr) 324bc03f14fSopenharmony_ci{ 325bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::shared_ptr<OHOS::AAFwk::Want>"); 326bc03f14fSopenharmony_ci OHOS::AAFwk::Want want; 327bc03f14fSopenharmony_ci AppExecFwk::UnwrapWant(env, in, want); 328bc03f14fSopenharmony_ci wantPtr = std::make_shared<OHOS::AAFwk::Want>(want); 329bc03f14fSopenharmony_ci return napi_ok; 330bc03f14fSopenharmony_ci} 331bc03f14fSopenharmony_ci 332bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::shared_ptr<OHOS::AAFwk::Want> &in, napi_value &out) 333bc03f14fSopenharmony_ci{ 334bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::shared_ptr<OHOS::AAFwk::Want>"); 335bc03f14fSopenharmony_ci out = OHOS::AppExecFwk::WrapWant(env, *in); 336bc03f14fSopenharmony_ci return napi_ok; 337bc03f14fSopenharmony_ci} 338bc03f14fSopenharmony_ci 339bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::shared_ptr<UDMF::Object> &object) 340bc03f14fSopenharmony_ci{ 341bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::GetValue Object"); 342bc03f14fSopenharmony_ci napi_value attributeNames = nullptr; 343bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_get_property_names(env, in, &attributeNames), napi_invalid_arg); 344bc03f14fSopenharmony_ci uint32_t attributesNum = 0; 345bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_get_array_length(env, attributeNames, &attributesNum), napi_invalid_arg); 346bc03f14fSopenharmony_ci for (uint32_t i = 0; i < attributesNum; i++) { 347bc03f14fSopenharmony_ci napi_value attributeNameNapi = nullptr; 348bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_get_element(env, attributeNames, i, &attributeNameNapi), napi_invalid_arg); 349bc03f14fSopenharmony_ci size_t len = 0; 350bc03f14fSopenharmony_ci char str[STR_MAX_SIZE] = { 0 }; 351bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_get_value_string_utf8( 352bc03f14fSopenharmony_ci env, attributeNameNapi, str, STR_MAX_SIZE, &len), napi_invalid_arg); 353bc03f14fSopenharmony_ci std::string attributeName = str; 354bc03f14fSopenharmony_ci napi_value attributeValueNapi = nullptr; 355bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_get_named_property(env, in, str, &attributeValueNapi), napi_invalid_arg); 356bc03f14fSopenharmony_ci 357bc03f14fSopenharmony_ci bool isArrayBuffer = false; 358bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_is_arraybuffer(env, attributeValueNapi, &isArrayBuffer), napi_invalid_arg); 359bc03f14fSopenharmony_ci if (isArrayBuffer) { 360bc03f14fSopenharmony_ci void *data = nullptr; 361bc03f14fSopenharmony_ci size_t dataLen = 0; 362bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_get_arraybuffer_info(env, attributeValueNapi, &data, &dataLen), napi_invalid_arg); 363bc03f14fSopenharmony_ci object->value_[attributeName] = std::vector<uint8_t>( 364bc03f14fSopenharmony_ci reinterpret_cast<uint8_t *>(data), reinterpret_cast<uint8_t *>(data) + dataLen); 365bc03f14fSopenharmony_ci continue; 366bc03f14fSopenharmony_ci } 367bc03f14fSopenharmony_ci napi_valuetype valueType = napi_undefined; 368bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_typeof(env, attributeValueNapi, &valueType), napi_invalid_arg); 369bc03f14fSopenharmony_ci switch (valueType) { 370bc03f14fSopenharmony_ci case napi_valuetype::napi_object: 371bc03f14fSopenharmony_ci object->value_[attributeName] = std::make_shared<UDMF::Object>(); 372bc03f14fSopenharmony_ci break; 373bc03f14fSopenharmony_ci case napi_valuetype::napi_number: 374bc03f14fSopenharmony_ci object->value_[attributeName] = double(); 375bc03f14fSopenharmony_ci break; 376bc03f14fSopenharmony_ci case napi_valuetype::napi_string: 377bc03f14fSopenharmony_ci object->value_[attributeName] = std::string(); 378bc03f14fSopenharmony_ci break; 379bc03f14fSopenharmony_ci case napi_valuetype::napi_boolean: 380bc03f14fSopenharmony_ci object->value_[attributeName] = bool(); 381bc03f14fSopenharmony_ci break; 382bc03f14fSopenharmony_ci case napi_valuetype::napi_undefined: 383bc03f14fSopenharmony_ci object->value_[attributeName] = std::monostate(); 384bc03f14fSopenharmony_ci break; 385bc03f14fSopenharmony_ci case napi_valuetype::napi_null: 386bc03f14fSopenharmony_ci object->value_[attributeName] = nullptr; 387bc03f14fSopenharmony_ci break; 388bc03f14fSopenharmony_ci default: 389bc03f14fSopenharmony_ci return napi_invalid_arg; 390bc03f14fSopenharmony_ci } 391bc03f14fSopenharmony_ci napi_status status = napi_ok; 392bc03f14fSopenharmony_ci std::visit([&](auto &value) {status = NapiDataUtils::GetValue(env, attributeValueNapi, value);}, 393bc03f14fSopenharmony_ci object->value_[attributeName]); 394bc03f14fSopenharmony_ci if (status != napi_ok) { 395bc03f14fSopenharmony_ci return status; 396bc03f14fSopenharmony_ci } 397bc03f14fSopenharmony_ci } 398bc03f14fSopenharmony_ci return napi_ok; 399bc03f14fSopenharmony_ci} 400bc03f14fSopenharmony_ci 401bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::shared_ptr<UDMF::Object> &object, napi_value &out) 402bc03f14fSopenharmony_ci{ 403bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::GetValue Object"); 404bc03f14fSopenharmony_ci napi_create_object(env, &out); 405bc03f14fSopenharmony_ci for (auto &[key, value] : object->value_) { 406bc03f14fSopenharmony_ci napi_value valueNapi = nullptr; 407bc03f14fSopenharmony_ci if (std::holds_alternative<std::vector<uint8_t>>(value)) { 408bc03f14fSopenharmony_ci auto array = std::get<std::vector<uint8_t>>(value); 409bc03f14fSopenharmony_ci void *data = nullptr; 410bc03f14fSopenharmony_ci size_t len = array.size(); 411bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, napi_create_arraybuffer(env, len, &data, &valueNapi), napi_generic_failure); 412bc03f14fSopenharmony_ci if (memcpy_s(data, len, reinterpret_cast<const void *>(array.data()), len) != 0) { 413bc03f14fSopenharmony_ci PASTEBOARD_HILOGE(PASTEBOARD_MODULE_JS_NAPI, "memcpy_s failed"); 414bc03f14fSopenharmony_ci return napi_generic_failure; 415bc03f14fSopenharmony_ci } 416bc03f14fSopenharmony_ci } else { 417bc03f14fSopenharmony_ci std::visit([&](const auto &value) {NapiDataUtils::SetValue(env, value, valueNapi);}, value); 418bc03f14fSopenharmony_ci } 419bc03f14fSopenharmony_ci napi_set_named_property(env, out, key.c_str(), valueNapi); 420bc03f14fSopenharmony_ci } 421bc03f14fSopenharmony_ci return napi_ok; 422bc03f14fSopenharmony_ci} 423bc03f14fSopenharmony_ci 424bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, std::monostate &out) 425bc03f14fSopenharmony_ci{ 426bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> std::monostate"); 427bc03f14fSopenharmony_ci out = std::monostate{}; 428bc03f14fSopenharmony_ci return napi_ok; 429bc03f14fSopenharmony_ci} 430bc03f14fSopenharmony_ci 431bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const std::monostate &in, napi_value &out) 432bc03f14fSopenharmony_ci{ 433bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- std::monostate"); 434bc03f14fSopenharmony_ci return napi_get_undefined(env, &out); 435bc03f14fSopenharmony_ci} 436bc03f14fSopenharmony_ci 437bc03f14fSopenharmony_cinapi_status NapiDataUtils::GetValue(napi_env env, napi_value in, nullptr_t &out) 438bc03f14fSopenharmony_ci{ 439bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value -> null"); 440bc03f14fSopenharmony_ci out = nullptr; 441bc03f14fSopenharmony_ci return napi_ok; 442bc03f14fSopenharmony_ci} 443bc03f14fSopenharmony_ci 444bc03f14fSopenharmony_cinapi_status NapiDataUtils::SetValue(napi_env env, const nullptr_t &in, napi_value &out) 445bc03f14fSopenharmony_ci{ 446bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, "napi_value <- null"); 447bc03f14fSopenharmony_ci return napi_get_null(env, &out); 448bc03f14fSopenharmony_ci} 449bc03f14fSopenharmony_ci 450bc03f14fSopenharmony_cibool NapiDataUtils::IsTypeForNapiValue(napi_env env, napi_value param, napi_valuetype expectType) 451bc03f14fSopenharmony_ci{ 452bc03f14fSopenharmony_ci napi_valuetype valueType = napi_undefined; 453bc03f14fSopenharmony_ci 454bc03f14fSopenharmony_ci if (param == nullptr) { 455bc03f14fSopenharmony_ci return false; 456bc03f14fSopenharmony_ci } 457bc03f14fSopenharmony_ci 458bc03f14fSopenharmony_ci if (napi_typeof(env, param, &valueType) != napi_ok) { 459bc03f14fSopenharmony_ci return false; 460bc03f14fSopenharmony_ci } 461bc03f14fSopenharmony_ci 462bc03f14fSopenharmony_ci return valueType == expectType; 463bc03f14fSopenharmony_ci} 464bc03f14fSopenharmony_ci 465bc03f14fSopenharmony_cibool NapiDataUtils::IsNull(napi_env env, napi_value value) 466bc03f14fSopenharmony_ci{ 467bc03f14fSopenharmony_ci napi_valuetype type = napi_undefined; 468bc03f14fSopenharmony_ci napi_status status = napi_typeof(env, value, &type); 469bc03f14fSopenharmony_ci if (status == napi_ok && (type == napi_undefined || type == napi_null)) { 470bc03f14fSopenharmony_ci return true; 471bc03f14fSopenharmony_ci } 472bc03f14fSopenharmony_ci if (type == napi_string) { 473bc03f14fSopenharmony_ci size_t len; 474bc03f14fSopenharmony_ci napi_get_value_string_utf8(env, value, NULL, 0, &len); 475bc03f14fSopenharmony_ci return len == 0; 476bc03f14fSopenharmony_ci } 477bc03f14fSopenharmony_ci return false; 478bc03f14fSopenharmony_ci} 479bc03f14fSopenharmony_ci 480bc03f14fSopenharmony_cinapi_value NapiDataUtils::DefineClass(napi_env env, const std::string &name, 481bc03f14fSopenharmony_ci const napi_property_descriptor *properties, size_t count, napi_callback newcb) 482bc03f14fSopenharmony_ci{ 483bc03f14fSopenharmony_ci // base64("data.udmf") as rootPropName, i.e. global.<root> 484bc03f14fSopenharmony_ci const std::string rootPropName = "ZGF0YS51ZG1m"; 485bc03f14fSopenharmony_ci napi_value root = nullptr; 486bc03f14fSopenharmony_ci bool hasRoot = false; 487bc03f14fSopenharmony_ci napi_value global = nullptr; 488bc03f14fSopenharmony_ci napi_get_global(env, &global); 489bc03f14fSopenharmony_ci napi_has_named_property(env, global, rootPropName.c_str(), &hasRoot); 490bc03f14fSopenharmony_ci if (hasRoot) { 491bc03f14fSopenharmony_ci napi_get_named_property(env, global, rootPropName.c_str(), &root); 492bc03f14fSopenharmony_ci } else { 493bc03f14fSopenharmony_ci napi_create_object(env, &root); 494bc03f14fSopenharmony_ci napi_set_named_property(env, global, rootPropName.c_str(), root); 495bc03f14fSopenharmony_ci } 496bc03f14fSopenharmony_ci 497bc03f14fSopenharmony_ci std::string propName = "constructor_of_" + name; 498bc03f14fSopenharmony_ci napi_value constructor = nullptr; 499bc03f14fSopenharmony_ci bool hasProp = false; 500bc03f14fSopenharmony_ci napi_has_named_property(env, root, propName.c_str(), &hasProp); 501bc03f14fSopenharmony_ci if (hasProp) { 502bc03f14fSopenharmony_ci napi_get_named_property(env, root, propName.c_str(), &constructor); 503bc03f14fSopenharmony_ci if (constructor != nullptr) { 504bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, 505bc03f14fSopenharmony_ci "got data.distributeddata.%{public}s as constructor", propName.c_str()); 506bc03f14fSopenharmony_ci return constructor; 507bc03f14fSopenharmony_ci } 508bc03f14fSopenharmony_ci hasProp = false; // no constructor. 509bc03f14fSopenharmony_ci } 510bc03f14fSopenharmony_ci 511bc03f14fSopenharmony_ci NAPI_CALL_BASE(env, 512bc03f14fSopenharmony_ci napi_define_class(env, name.c_str(), name.size(), newcb, nullptr, count, properties, &constructor), 513bc03f14fSopenharmony_ci nullptr); 514bc03f14fSopenharmony_ci NAPI_ASSERT(env, constructor != nullptr, "napi_define_class failed!"); 515bc03f14fSopenharmony_ci 516bc03f14fSopenharmony_ci if (!hasProp) { 517bc03f14fSopenharmony_ci napi_set_named_property(env, root, propName.c_str(), constructor); 518bc03f14fSopenharmony_ci PASTEBOARD_HILOGD(PASTEBOARD_MODULE_JS_NAPI, 519bc03f14fSopenharmony_ci "save constructor to data.distributeddata.%{public}s", propName.c_str()); 520bc03f14fSopenharmony_ci } 521bc03f14fSopenharmony_ci return constructor; 522bc03f14fSopenharmony_ci} 523bc03f14fSopenharmony_ci} // namespace MiscServicesNapi 524bc03f14fSopenharmony_ci} // namespace OHOS 525