diff --git a/ReactAndroid/src/main/jni/react/jni/MethodInvoker.cpp b/ReactAndroid/src/main/jni/react/jni/MethodInvoker.cpp index e12355e208ff9e..9be87fc9957c05 100644 --- a/ReactAndroid/src/main/jni/react/jni/MethodInvoker.cpp +++ b/ReactAndroid/src/main/jni/react/jni/MethodInvoker.cpp @@ -49,6 +49,22 @@ jdouble extractDouble(const folly::dynamic& value) { } } +jint extractInteger(const folly::dynamic& value) { + // The logic here is taken from convertDynamicIfIntegral, but the + // return type and exception are different. + if (value.isInt()) { + return value.getInt(); + } + double dbl = value.getDouble(); + jint result = static_cast(dbl); + if (dbl != result) { + throw std::invalid_argument( + folly::to( + "Tried to convert jint argument, but got a non-integral double: ", dbl)); + } + return result; +} + local_ref extractCallback(std::weak_ptr& instance, const folly::dynamic& value) { if (value.isNull()) { return local_ref(nullptr); @@ -101,10 +117,10 @@ jvalue extract(std::weak_ptr& instance, char type, dynamic_iterator& i value.l = JBoolean::valueOf(static_cast(arg.getBool())).release(); break; case 'i': - value.i = static_cast(arg.getInt()); + value.i = extractInteger(arg); break; case 'I': - value.l = JInteger::valueOf(static_cast(arg.getInt())).release(); + value.l = JInteger::valueOf(extractInteger(arg)).release(); break; case 'f': value.f = static_cast(extractDouble(arg));