Permalink
Browse files

Replace getInt with asInt in native collections

Reviewed By: mhorowitz

Differential Revision: D5904751

fbshipit-source-id: 995412196fd76ea60a65253719e49852322be6e6
  • Loading branch information...
johnislarry authored and facebook-github-bot committed Oct 5, 2017
1 parent f8e1386 commit 790eabcdff18470571b3f9a5355c924495015918
@@ -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<decltype(integer), int64_t>::value,
"folly::dynamic int is not int64_t");
jint javaint = static_cast<jint>(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) {
@@ -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<jint>(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<jstring> ReadableNativeMap::getStringKey(const std::string& key) {
@@ -146,5 +141,29 @@ void ReadableNativeMapKeySetIterator::registerNatives() {
});
}
jint makeJIntOrThrow(int64_t integer) {
jint javaint = static_cast<jint>(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<int64_t>(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
@@ -55,5 +55,8 @@ struct ReadableNativeMapKeySetIterator : jni::HybridClass<ReadableNativeMapKeySe
const folly::dynamic& map_;
};
jint makeJIntOrThrow(int64_t integer);
int64_t convertDynamicIfIntegral(const folly::dynamic&);
} // namespace react
} // namespace facebook

0 comments on commit 790eabc

Please sign in to comment.