diff --git a/interfaces/js/kits/src/i18n_addon.cpp b/interfaces/js/kits/src/i18n_addon.cpp index 71fb08fae91b65c3d4bdac5ccbaa4700fb464c8e..3915067be0eeae289bedd3bf0186244e0d84bd33 100644 --- a/interfaces/js/kits/src/i18n_addon.cpp +++ b/interfaces/js/kits/src/i18n_addon.cpp @@ -244,6 +244,9 @@ napi_value I18nAddon::UnitConvert(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } std::string fromUnit; GetOptionValue(env, argv[0], "unit", fromUnit); std::string fromMeasSys; @@ -261,7 +264,6 @@ napi_value I18nAddon::UnitConvert(napi_env env, napi_callback_info info) // 3 is the index of value status = napi_get_value_string_utf8(env, argv[3], localeBuf.data(), len + 1, &len); if (status != napi_ok) { - HiLog::Error(LABEL, "Failed to get string item"); return nullptr; } std::vector localeTags; @@ -269,7 +271,6 @@ napi_value I18nAddon::UnitConvert(napi_env env, napi_callback_info info) std::map map = {}; map.insert(std::make_pair("style", "unit")); if (!convertStatus) { - HiLog::Error(LABEL, "Do not support the conversion"); map.insert(std::make_pair("unit", fromUnit)); } else { map.insert(std::make_pair("unit", toUnit)); @@ -295,6 +296,9 @@ napi_value I18nAddon::GetDateOrder(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector languageBuf(len + 1); @@ -627,6 +631,9 @@ napi_value I18nAddon::IsDigitAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -655,6 +662,9 @@ napi_value I18nAddon::IsSpaceCharAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -683,6 +693,9 @@ napi_value I18nAddon::IsWhiteSpaceAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -711,6 +724,9 @@ napi_value I18nAddon::IsRTLCharacterAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -739,6 +755,9 @@ napi_value I18nAddon::IsIdeoGraphicAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -767,6 +786,9 @@ napi_value I18nAddon::IsLetterAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -795,6 +817,9 @@ napi_value I18nAddon::IsLowerCaseAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -823,6 +848,9 @@ napi_value I18nAddon::IsUpperCaseAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -851,6 +879,9 @@ napi_value I18nAddon::GetTypeAddon(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { @@ -905,6 +936,9 @@ napi_value I18nAddon::GetSystemCountries(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector localeBuf(len + 1); @@ -981,6 +1015,9 @@ napi_value I18nAddon::GetDisplayLanguage(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector localeBuf(len + 1); @@ -1020,6 +1057,9 @@ napi_value I18nAddon::GetDisplayCountry(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector localeBuf(len + 1); @@ -1058,6 +1098,9 @@ napi_value I18nAddon::IsSuggested(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector languageBuf(len + 1); @@ -1095,6 +1138,9 @@ napi_value I18nAddon::SetSystemLanguage(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector languageBuf(len + 1); @@ -1120,6 +1166,9 @@ napi_value I18nAddon::SetSystemRegion(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector regionBuf(len + 1); @@ -1145,6 +1194,9 @@ napi_value I18nAddon::SetSystemLocale(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector localeBuf(len + 1); @@ -1170,6 +1222,9 @@ napi_value I18nAddon::IsRTL(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } size_t len = 0; napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); std::vector localeBuf(len + 1); @@ -1231,6 +1286,9 @@ void GetOptionValue(napi_env env, napi_value options, const std::string &optionN napi_get_value_string_utf8(env, optionValue, nullptr, 0, &len); std::vector optionBuf(len + 1); status = napi_get_value_string_utf8(env, optionValue, optionBuf.data(), len + 1, &len); + if (status != napi_ok) { + return; + } map.insert(make_pair(optionName, optionBuf.data())); } } @@ -1243,51 +1301,45 @@ napi_value I18nAddon::PhoneNumberFormatConstructor(napi_env env, napi_callback_i napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); if (valueType != napi_valuetype::napi_string) { napi_throw_type_error(env, nullptr, "Parameter type does not match"); return nullptr; } - size_t len = 0; status = napi_get_value_string_utf8(env, argv[0], nullptr, 0, &len); if (status != napi_ok) { HiLog::Error(LABEL, "Get country tag length failed"); return nullptr; } - std::vector country (len + 1); status = napi_get_value_string_utf8(env, argv[0], country.data(), len + 1, &len); if (status != napi_ok) { HiLog::Error(LABEL, "Get country tag failed"); return nullptr; } - std::map options; GetOptionValue(env, argv[1], "type", options); - std::unique_ptr obj = nullptr; obj = std::make_unique(); if (!obj) { HiLog::Error(LABEL, "Create I18nAddon failed"); return nullptr; } - status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), I18nAddon::Destructor, nullptr, &obj->wrapper_); if (status != napi_ok) { HiLog::Error(LABEL, "Wrap I18nAddon failed"); return nullptr; } - if (!obj->InitPhoneNumberFormatContext(env, info, country.data(), options)) { return nullptr; } - obj.release(); - return thisVar; } @@ -2391,6 +2443,9 @@ napi_value I18nAddon::IndexUtilConstructor(napi_env env, napi_callback_info info napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } std::string localeTag = ""; if (argv[0] != nullptr) { napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -2604,6 +2659,9 @@ napi_value I18nAddon::Set24HourClock(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } bool option = false; status = napi_get_value_bool(env, argv[0], &option); @@ -2628,6 +2686,9 @@ napi_value I18nAddon::AddPreferredLanguage(napi_env env, napi_callback_info info napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); @@ -2672,6 +2733,9 @@ napi_value I18nAddon::RemovePreferredLanguage(napi_env env, napi_callback_info i napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; napi_typeof(env, argv[0], &valueType); @@ -2926,6 +2990,9 @@ napi_value I18nAddon::GetTimeZoneDisplayName(napi_env env, napi_callback_info in napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } I18nAddon *obj = nullptr; status = napi_unwrap(env, thisVar, reinterpret_cast(&obj)); @@ -2968,6 +3035,9 @@ napi_value I18nAddon::GetOffset(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } double date = 0; if (argv[0]) { diff --git a/interfaces/js/kits/src/intl_addon.cpp b/interfaces/js/kits/src/intl_addon.cpp index 31195e8b5d9c9221b28c7331f20210f650d40175..795d6f699f609c47cdc730b1217ea9f0aef492d0 100644 --- a/interfaces/js/kits/src/intl_addon.cpp +++ b/interfaces/js/kits/src/intl_addon.cpp @@ -181,6 +181,9 @@ void GetOptionValue(napi_env env, napi_value options, const std::string &optionN napi_get_value_string_utf8(env, optionValue, nullptr, 0, &len); std::vector optionBuf(len + 1); status = napi_get_value_string_utf8(env, optionValue, optionBuf.data(), len + 1, &len); + if (status != napi_ok) { + return; + } map.insert(make_pair(optionName, optionBuf.data())); } } @@ -300,6 +303,9 @@ napi_value IntlAddon::LocaleConstructor(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } std::string localeTag = GetLocaleTag(env, argv[0]); std::map map = {}; @@ -364,12 +370,14 @@ void GetLocaleTags(napi_env env, napi_value rawLocaleTag, std::vector localeTags; if (argv[0] != nullptr) { napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -429,13 +437,14 @@ bool IntlAddon::InitDateTimeFormatContext(napi_env env, napi_callback_info info, napi_value IntlAddon::RelativeTimeFormatConstructor(napi_env env, napi_callback_info info) { - // Need to get one parameter of a locale in string format to create DateTimeFormat object. size_t argc = 2; napi_value argv[2] = { 0 }; napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); - + if (status != napi_ok) { + return nullptr; + } std::vector localeTags; if (argv[0] != nullptr) { napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -454,31 +463,26 @@ napi_value IntlAddon::RelativeTimeFormatConstructor(napi_env env, napi_callback_ } } } - std::map map = {}; if (argv[1] != nullptr) { GetRelativeTimeOptionValues(env, argv[1], map); } - std::unique_ptr obj = nullptr; obj = std::make_unique(); if (!obj) { HiLog::Error(LABEL, "Create IntlAddon failed"); return nullptr; } - status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), IntlAddon::Destructor, nullptr, &obj->wrapper_); if (status != napi_ok) { HiLog::Error(LABEL, "Wrap IntlAddon failed"); return nullptr; } - if (!obj->InitRelativeTimeFormatContext(env, info, localeTags, map)) { HiLog::Error(LABEL, "Init RelativeTimeFormat failed"); return nullptr; } - obj.release(); return thisVar; } @@ -595,13 +599,14 @@ void GetNumberOptionValues(napi_env env, napi_value options, std::map localeTags; if (argv[0] != nullptr) { napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -621,31 +626,25 @@ napi_value IntlAddon::NumberFormatConstructor(napi_env env, napi_callback_info i } } } - std::map map = {}; if (argv[1] != nullptr) { GetNumberOptionValues(env, argv[1], map); } - std::unique_ptr obj = nullptr; obj = std::make_unique(); if (!obj) { - HiLog::Error(LABEL, "Create IntlAddon failed"); return nullptr; } - status = napi_wrap(env, thisVar, reinterpret_cast(obj.get()), IntlAddon::Destructor, nullptr, &obj->wrapper_); if (status != napi_ok) { HiLog::Error(LABEL, "Wrap IntlAddon failed"); return nullptr; } - if (!obj->InitNumberFormatContext(env, info, localeTags, map)) { HiLog::Error(LABEL, "Init NumberFormat failed"); return nullptr; } - obj.release(); return thisVar; } @@ -1486,6 +1485,9 @@ napi_value IntlAddon::CollatorConstructor(napi_env env, napi_callback_info info) napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } std::vector localeTags; if (argv[0] != nullptr) { napi_valuetype valueType = napi_valuetype::napi_undefined; @@ -1504,12 +1506,10 @@ napi_value IntlAddon::CollatorConstructor(napi_env env, napi_callback_info info) } } } - std::map map = {}; if (argv[1] != nullptr) { GetCollatorOptionValue(env, argv[1], map); } - std::unique_ptr obj = nullptr; obj = std::make_unique(); if (!obj) { @@ -1869,6 +1869,9 @@ napi_value IntlAddon::PluralRulesConstructor(napi_env env, napi_callback_info in napi_value thisVar = nullptr; void *data = nullptr; napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + if (status != napi_ok) { + return nullptr; + } napi_valuetype valueType = napi_valuetype::napi_undefined; std::vector localeTags; if (argv[0] != nullptr) {