19596a2c1Sopenharmony_ci/* 29596a2c1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 39596a2c1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49596a2c1Sopenharmony_ci * you may not use this file except in compliance with the License. 59596a2c1Sopenharmony_ci * You may obtain a copy of the License at 69596a2c1Sopenharmony_ci * 79596a2c1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89596a2c1Sopenharmony_ci * 99596a2c1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109596a2c1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119596a2c1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129596a2c1Sopenharmony_ci * See the License for the specific language governing permissions and 139596a2c1Sopenharmony_ci * limitations under the License. 149596a2c1Sopenharmony_ci */ 159596a2c1Sopenharmony_ci 169596a2c1Sopenharmony_ci#include "error_util.h" 179596a2c1Sopenharmony_ci#include "i18n_hilog.h" 189596a2c1Sopenharmony_ci#include "js_utils.h" 199596a2c1Sopenharmony_ci#include "variable_convertor.h" 209596a2c1Sopenharmony_ci 219596a2c1Sopenharmony_cinamespace OHOS { 229596a2c1Sopenharmony_cinamespace Global { 239596a2c1Sopenharmony_cinamespace I18n { 249596a2c1Sopenharmony_cibool VariableConvertor::CheckNapiValueType(napi_env env, napi_value value) 259596a2c1Sopenharmony_ci{ 269596a2c1Sopenharmony_ci if (value != nullptr) { 279596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 289596a2c1Sopenharmony_ci napi_typeof(env, value, &valueType); 299596a2c1Sopenharmony_ci if (valueType != napi_valuetype::napi_undefined && valueType != napi_valuetype::napi_null) { 309596a2c1Sopenharmony_ci return true; 319596a2c1Sopenharmony_ci } 329596a2c1Sopenharmony_ci } 339596a2c1Sopenharmony_ci return false; 349596a2c1Sopenharmony_ci} 359596a2c1Sopenharmony_ci 369596a2c1Sopenharmony_civoid VariableConvertor::GetOptionValue(napi_env env, napi_value options, const std::string &optionName, 379596a2c1Sopenharmony_ci std::string &value) 389596a2c1Sopenharmony_ci{ 399596a2c1Sopenharmony_ci napi_value optionValue = nullptr; 409596a2c1Sopenharmony_ci napi_valuetype type = napi_undefined; 419596a2c1Sopenharmony_ci napi_status status = napi_typeof(env, options, &type); 429596a2c1Sopenharmony_ci if (status != napi_ok && type != napi_object) { 439596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetOptionValue: Get option failed, option is not an object"); 449596a2c1Sopenharmony_ci return; 459596a2c1Sopenharmony_ci } 469596a2c1Sopenharmony_ci bool hasProperty = false; 479596a2c1Sopenharmony_ci napi_status propStatus = napi_has_named_property(env, options, optionName.c_str(), &hasProperty); 489596a2c1Sopenharmony_ci if (propStatus == napi_ok && hasProperty) { 499596a2c1Sopenharmony_ci status = napi_get_named_property(env, options, optionName.c_str(), &optionValue); 509596a2c1Sopenharmony_ci if (status == napi_ok) { 519596a2c1Sopenharmony_ci size_t len; 529596a2c1Sopenharmony_ci napi_get_value_string_utf8(env, optionValue, nullptr, 0, &len); 539596a2c1Sopenharmony_ci std::vector<char> optionBuf(len + 1); 549596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, optionValue, optionBuf.data(), len + 1, &len); 559596a2c1Sopenharmony_ci if (status != napi_ok) { 569596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetOptionValue: Failed to get string item"); 579596a2c1Sopenharmony_ci return; 589596a2c1Sopenharmony_ci } 599596a2c1Sopenharmony_ci value = optionBuf.data(); 609596a2c1Sopenharmony_ci } 619596a2c1Sopenharmony_ci } 629596a2c1Sopenharmony_ci} 639596a2c1Sopenharmony_ci 649596a2c1Sopenharmony_cibool VariableConvertor::GetBoolOptionValue(napi_env env, napi_value &options, const std::string &optionName, 659596a2c1Sopenharmony_ci bool &boolVal) 669596a2c1Sopenharmony_ci{ 679596a2c1Sopenharmony_ci napi_valuetype type = napi_undefined; 689596a2c1Sopenharmony_ci napi_status status = napi_typeof(env, options, &type); 699596a2c1Sopenharmony_ci if (status != napi_ok && type != napi_object) { 709596a2c1Sopenharmony_ci HILOG_ERROR_I18N("option is not an object"); 719596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_VALID, optionName, "a valid object", true); 729596a2c1Sopenharmony_ci return false; 739596a2c1Sopenharmony_ci } 749596a2c1Sopenharmony_ci bool hasProperty = false; 759596a2c1Sopenharmony_ci status = napi_has_named_property(env, options, optionName.c_str(), &hasProperty); 769596a2c1Sopenharmony_ci if (status != napi_ok || !hasProperty) { 779596a2c1Sopenharmony_ci HILOG_INFO_I18N("option don't have property %{public}s", optionName.c_str()); 789596a2c1Sopenharmony_ci return false; 799596a2c1Sopenharmony_ci } 809596a2c1Sopenharmony_ci napi_value optionValue = nullptr; 819596a2c1Sopenharmony_ci status = napi_get_named_property(env, options, optionName.c_str(), &optionValue); 829596a2c1Sopenharmony_ci if (status != napi_ok) { 839596a2c1Sopenharmony_ci HILOG_INFO_I18N("get option %{public}s failed", optionName.c_str()); 849596a2c1Sopenharmony_ci return false; 859596a2c1Sopenharmony_ci } 869596a2c1Sopenharmony_ci napi_get_value_bool(env, optionValue, &boolVal); 879596a2c1Sopenharmony_ci return true; 889596a2c1Sopenharmony_ci} 899596a2c1Sopenharmony_ci 909596a2c1Sopenharmony_cistd::string VariableConvertor::GetString(napi_env &env, napi_value &value, int32_t &code) 919596a2c1Sopenharmony_ci{ 929596a2c1Sopenharmony_ci size_t len = 0; 939596a2c1Sopenharmony_ci napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &len); 949596a2c1Sopenharmony_ci if (status != napi_ok) { 959596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Get string failed"); 969596a2c1Sopenharmony_ci code = 1; 979596a2c1Sopenharmony_ci return ""; 989596a2c1Sopenharmony_ci } 999596a2c1Sopenharmony_ci std::vector<char> buf(len + 1); 1009596a2c1Sopenharmony_ci status = napi_get_value_string_utf8(env, value, buf.data(), len + 1, &len); 1019596a2c1Sopenharmony_ci if (status != napi_ok) { 1029596a2c1Sopenharmony_ci HILOG_ERROR_I18N("Create string failed"); 1039596a2c1Sopenharmony_ci code = 1; 1049596a2c1Sopenharmony_ci return ""; 1059596a2c1Sopenharmony_ci } 1069596a2c1Sopenharmony_ci std::string result(buf.data()); 1079596a2c1Sopenharmony_ci return result; 1089596a2c1Sopenharmony_ci} 1099596a2c1Sopenharmony_ci 1109596a2c1Sopenharmony_cibool VariableConvertor::GetStringArrayFromJsParam(napi_env env, napi_value &jsArray, const std::string& valueName, 1119596a2c1Sopenharmony_ci std::vector<std::string> &strArray) 1129596a2c1Sopenharmony_ci{ 1139596a2c1Sopenharmony_ci if (jsArray == nullptr) { 1149596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, valueName, "", true); 1159596a2c1Sopenharmony_ci return false; 1169596a2c1Sopenharmony_ci } 1179596a2c1Sopenharmony_ci bool isArray = false; 1189596a2c1Sopenharmony_ci napi_status status = napi_is_array(env, jsArray, &isArray); 1199596a2c1Sopenharmony_ci if (status != napi_ok) { 1209596a2c1Sopenharmony_ci return false; 1219596a2c1Sopenharmony_ci } else if (!isArray) { 1229596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, valueName, "an Array", true); 1239596a2c1Sopenharmony_ci return false; 1249596a2c1Sopenharmony_ci } 1259596a2c1Sopenharmony_ci uint32_t arrayLength = 0; 1269596a2c1Sopenharmony_ci napi_get_array_length(env, jsArray, &arrayLength); 1279596a2c1Sopenharmony_ci napi_value element = nullptr; 1289596a2c1Sopenharmony_ci int32_t code = 0; 1299596a2c1Sopenharmony_ci for (uint32_t i = 0; i < arrayLength; ++i) { 1309596a2c1Sopenharmony_ci napi_get_element(env, jsArray, i, &element); 1319596a2c1Sopenharmony_ci std::string str = GetString(env, element, code); 1329596a2c1Sopenharmony_ci if (code != 0) { 1339596a2c1Sopenharmony_ci HILOG_ERROR_I18N("GetStringArrayFromJsParam: Failed to obtain the parameter."); 1349596a2c1Sopenharmony_ci return false; 1359596a2c1Sopenharmony_ci } 1369596a2c1Sopenharmony_ci strArray.push_back(str); 1379596a2c1Sopenharmony_ci } 1389596a2c1Sopenharmony_ci return true; 1399596a2c1Sopenharmony_ci} 1409596a2c1Sopenharmony_ci 1419596a2c1Sopenharmony_cinapi_value VariableConvertor::CreateString(napi_env env, const std::string &str) 1429596a2c1Sopenharmony_ci{ 1439596a2c1Sopenharmony_ci napi_value result; 1449596a2c1Sopenharmony_ci napi_status status = napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &result); 1459596a2c1Sopenharmony_ci if (status != napi_ok) { 1469596a2c1Sopenharmony_ci HILOG_ERROR_I18N("create string js variable failed."); 1479596a2c1Sopenharmony_ci return nullptr; 1489596a2c1Sopenharmony_ci } 1499596a2c1Sopenharmony_ci return result; 1509596a2c1Sopenharmony_ci} 1519596a2c1Sopenharmony_ci 1529596a2c1Sopenharmony_cinapi_value VariableConvertor::CreateNumber(napi_env env, const int32_t &num) 1539596a2c1Sopenharmony_ci{ 1549596a2c1Sopenharmony_ci napi_value result; 1559596a2c1Sopenharmony_ci napi_status status = napi_create_int32(env, num, &result); 1569596a2c1Sopenharmony_ci if (status != napi_ok) { 1579596a2c1Sopenharmony_ci HILOG_ERROR_I18N("create number js variable failed."); 1589596a2c1Sopenharmony_ci return nullptr; 1599596a2c1Sopenharmony_ci } 1609596a2c1Sopenharmony_ci return result; 1619596a2c1Sopenharmony_ci} 1629596a2c1Sopenharmony_ci 1639596a2c1Sopenharmony_civoid VariableConvertor::VerifyType(napi_env env, const std::string& valueName, const std::string& type, 1649596a2c1Sopenharmony_ci napi_value argv) 1659596a2c1Sopenharmony_ci{ 1669596a2c1Sopenharmony_ci napi_valuetype valueType = napi_valuetype::napi_undefined; 1679596a2c1Sopenharmony_ci napi_typeof(env, argv, &valueType); 1689596a2c1Sopenharmony_ci if (type == "string" && valueType != napi_valuetype::napi_string) { 1699596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, valueName, type, true); 1709596a2c1Sopenharmony_ci } else if (type == "number" && valueType != napi_valuetype::napi_number) { 1719596a2c1Sopenharmony_ci ErrorUtil::NapiThrow(env, I18N_NOT_FOUND, valueName, type, true); 1729596a2c1Sopenharmony_ci } 1739596a2c1Sopenharmony_ci} 1749596a2c1Sopenharmony_ci} // namespace I18n 1759596a2c1Sopenharmony_ci} // namespace Global 1769596a2c1Sopenharmony_ci} // namespace OHOS