diff --git a/ReactAndroid/src/main/jni/react/jni/ReadableNativeArray.cpp b/ReactAndroid/src/main/jni/react/jni/ReadableNativeArray.cpp index b1528a93e..bc445a493 100644 --- a/ReactAndroid/src/main/jni/react/jni/ReadableNativeArray.cpp +++ b/ReactAndroid/src/main/jni/react/jni/ReadableNativeArray.cpp @@ -43,16 +43,9 @@ jdouble ReadableNativeArray::getDouble(jint index) { } jint ReadableNativeArray::getInt(jint index) { - auto integer = array_.at(index).getInt(); - static_assert(std::is_same::value, - "folly::dynamic int is not int64_t"); - jint javaint = static_cast(integer); - if (integer != javaint) { - throwNewJavaException( - exceptions::gUnexpectedNativeTypeExceptionClass, - "Value '%lld' doesn't fit into a 32 bit signed int", integer); - } - return javaint; + const folly::dynamic& val = array_.at(index); + int64_t integer = convertDynamicIfIntegral(val); + return makeJIntOrThrow(integer); } const char* ReadableNativeArray::getString(jint index) { diff --git a/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.cpp b/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.cpp index 7b8d4a1fa..b057f7fe1 100644 --- a/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.cpp +++ b/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.cpp @@ -46,14 +46,9 @@ double ReadableNativeMap::getDoubleKey(const std::string& key) { } jint ReadableNativeMap::getIntKey(const std::string& key) { - auto integer = getMapValue(key).getInt(); - jint javaint = static_cast(integer); - if (integer != javaint) { - throwNewJavaException( - exceptions::gUnexpectedNativeTypeExceptionClass, - "Value '%lld' doesn't fit into a 32 bit signed int", integer); - } - return javaint; + const folly::dynamic& val = getMapValue(key); + int64_t integer = convertDynamicIfIntegral(val); + return makeJIntOrThrow(integer); } local_ref ReadableNativeMap::getStringKey(const std::string& key) { @@ -146,5 +141,29 @@ void ReadableNativeMapKeySetIterator::registerNatives() { }); } +jint makeJIntOrThrow(int64_t integer) { + jint javaint = static_cast(integer); + if (integer != javaint) { + throwNewJavaException( + exceptions::gUnexpectedNativeTypeExceptionClass, + "Value '%lld' doesn't fit into a 32 bit signed int", integer); + } + return javaint; +} + +int64_t convertDynamicIfIntegral(const folly::dynamic& val) { + if (val.isInt()) { + return val.getInt(); + } + double dbl = val.getDouble(); + int64_t result = static_cast(dbl); + if (dbl != result) { + throwNewJavaException( + exceptions::gUnexpectedNativeTypeExceptionClass, + "Tried to read an int, but got a non-integral double: %f", dbl); + } + return result; +} + } // namespace react } // namespace facebook diff --git a/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.h b/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.h index 65284a6cf..92f36c37a 100644 --- a/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.h +++ b/ReactAndroid/src/main/jni/react/jni/ReadableNativeMap.h @@ -55,5 +55,8 @@ struct ReadableNativeMapKeySetIterator : jni::HybridClass