Skip to content

Commit

Permalink
When converting arguments JS->Java, handle integers correctly
Browse files Browse the repository at this point in the history
Summary:
If a folly::dynamic contains a double when a Java method is
declared to take an int, getInt() will be called, and a C++ exception
will be thrown.  This change uses similar logic to
NativeReadableMap.getInt() to convert a folly::dynamic double to a
jint.  This will still throw an exception if the JS value cannot be
represented as a jint without loss.

Reviewed By: fromcelticpark

Differential Revision: D10209492

fbshipit-source-id: fd96416200c6b283ce5c8f8fa4c227ceb8f43054
  • Loading branch information
mhorowitz authored and facebook-github-bot committed Oct 5, 2018
1 parent 5bfa39e commit bb9b9a8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ReactAndroid/src/main/jni/react/jni/MethodInvoker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<jint>(dbl);
if (dbl != result) {
throw std::invalid_argument(
folly::to<std::string>(
"Tried to convert jint argument, but got a non-integral double: ", dbl));
}
return result;
}

local_ref<JCxxCallbackImpl::jhybridobject> extractCallback(std::weak_ptr<Instance>& instance, const folly::dynamic& value) {
if (value.isNull()) {
return local_ref<JCxxCallbackImpl::jhybridobject>(nullptr);
Expand Down Expand Up @@ -101,10 +117,10 @@ jvalue extract(std::weak_ptr<Instance>& instance, char type, dynamic_iterator& i
value.l = JBoolean::valueOf(static_cast<jboolean>(arg.getBool())).release();
break;
case 'i':
value.i = static_cast<jint>(arg.getInt());
value.i = extractInteger(arg);
break;
case 'I':
value.l = JInteger::valueOf(static_cast<jint>(arg.getInt())).release();
value.l = JInteger::valueOf(extractInteger(arg)).release();
break;
case 'f':
value.f = static_cast<jfloat>(extractDouble(arg));
Expand Down

0 comments on commit bb9b9a8

Please sign in to comment.