| 860 | } |
| 861 | |
| 862 | double HermesJavaScriptContext::valueToDouble(const JSValue& value, JSExceptionTracker& exceptionTracker) { |
| 863 | hermes::vm::GCScope gcScope(*_runtime); |
| 864 | auto hermesValue = toHermesValue(value); |
| 865 | if (hermesValue.isUndefined() || hermesValue.isNull()) { |
| 866 | return 0.0; |
| 867 | } else if (hermesValue.isNumber()) { |
| 868 | return hermesValue.getNumber(); |
| 869 | } else if (hermesValue.isBool()) { |
| 870 | return hermesValue.getBool() ? 1.0 : 0.0; |
| 871 | } else if (hermesValue.isString()) { |
| 872 | auto stringView = |
| 873 | hermes::vm::StringPrimitive::createStringView(*_runtime, _runtime->makeHandle(hermesValue.getString())); |
| 874 | std::string utf8; |
| 875 | if (stringView.isASCII()) { |
| 876 | utf8 += std::string_view(stringView.castToCharPtr(), stringView.length()); |
| 877 | ; |
| 878 | } else { |
| 879 | auto converted = Valdi::utf16ToUtf8(stringView.castToChar16Ptr(), stringView.length()); |
| 880 | utf8 += std::string_view(converted.first, converted.second); |
| 881 | } |
| 882 | |
| 883 | return atof(utf8.c_str()); // NOLINT(cert-err34-c) |
| 884 | } else { |
| 885 | return 0.0; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | int32_t HermesJavaScriptContext::valueToInt(const JSValue& value, JSExceptionTracker& exceptionTracker) { |
| 890 | return static_cast<int32_t>(valueToDouble(value, exceptionTracker)); |
nothing calls this directly
no test coverage detected