Replace getInt with asInt in native collections

Reviewed By: mhorowitz

Differential Revision: D5904751

fbshipit-source-id: 995412196fd76ea60a65253719e49852322be6e6
This commit is contained in:
Max Sherman 2017-10-05 09:57:46 -07:00 committed by Facebook Github Bot
parent f8e13868dd
commit 790eabcdff
3 changed files with 33 additions and 18 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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