diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index 89a692ca02807fc9735bdb3eb83a8ce5253206d7..801e0c6e6594ae6d38ad593054eb88991b1777ff 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -86,6 +86,26 @@ std::tuple> TypeConverter::ToOptionalInt64(ani_env return { false, {} }; } +tuple> TypeConverter::ToOptionalDouble(ani_env *env, const ani_object &value) +{ + if (env == nullptr) { + return { false, {} }; + } + + ani_boolean isUndefined; + env->Reference_IsUndefined(value, &isUndefined); + if (isUndefined) { + return { true, nullopt }; + } + + ani_double doubleValue; + if (ANI_OK == env->Object_CallMethodByName_Double(value, "toDouble", nullptr, &doubleValue)) { + return { true, make_optional(doubleValue) }; + } + + return { false, {} }; +} + std::tuple TypeConverter::ToAniString(ani_env *env, std::string str) { if (env == nullptr) { diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.h b/interfaces/kits/js/src/common/ani_helper/type_converter.h index a0176dc196555be6850ceca270b970c1b6f1bf0b..863640f7af2757ecd29d37a6f382249c7a8358d9 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.h +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.h @@ -32,6 +32,7 @@ public: static std::tuple ToUTF8String(ani_env *env, const ani_string &path); static std::tuple> ToOptionalInt32(ani_env *env, const ani_object &value); static std::tuple> ToOptionalInt64(ani_env *env, const ani_object &value); + static std::tuple> ToOptionalDouble(ani_env *env, const ani_object &value); static std::tuple ToAniArrayBuffer(ani_env *env, void *buffer, size_t length); static std::tuple ToAniString(ani_env *env, std::string str); static std::tuple ToAniString(ani_env *env, std::string str, size_t size);