Skip to content

Commit 788dc10

Browse files
authored
Throw a JS error when TurboModule constants can't be converted
## Summary: `JavaTurboModule` converts a module's `getConstants()` result with `Arguments.makeNativeMap()`. That throws `IllegalArgumentException` for values it can't convert, such as a `Date` or a `Uri`. The result wasn't checked, so the null map was used with the Java exception still pending, and ART aborted the app with `JNI DETECTED ERROR IN APPLICATION: obj == null`. This change: - Calls `Arguments.makeNativeMap()` through an fbjni `JavaClass`, so a value it can't convert throws through fbjni's usual error handling and becomes a JS error. - Looks up `Arguments` and `makeNativeMap` once, instead of calling `FindClass` and leaving its local reference behind on every `getConstants()` call. - Releases the local reference to the constants map. ## Changelog: [ANDROID] [FIXED] - Throw a JS error instead of crashing when a TurboModule's constants contain a value that can't be converted ## Test Plan: Release RNTester (`./gradlew :packages:rn-tester:android:app:assembleRelease -PreactNativeArchitectures=arm64-v8a`) with these temporary, uncommitted changes: - `SampleTurboModule` adds a `java.util.Date` constant when asked to. - A Playground screen that then calls `getConstants()`. - R8 turned off, because `SampleTurboModule` can't initialize in a minified build, which is a separate issue. On a Galaxy A16 (Android 16): | Call | `main` | This PR | |---|---|---| | `getConstants()` with a `Date` constant | SIGABRT: `JNI DETECTED ERROR IN APPLICATION: obj == null`, from `GetObjectField` in `JavaTurboModule::invokeJavaMethod` | JS error: `Exception in HostFunction: java.lang.IllegalArgumentException: Could not convert class java.util.Date`, followed by the Java stack trace | | `getConstants()` with valid constants | `{"const3":"something","const1":true,"const2":1080}` | Same value | Not run: an automated test. `JavaTurboModule` has no host-side tests, and Fantom doesn't load Java modules. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Differential Revision: D129043188 Pull Request resolved: #59698
1 parent 6b26cb5 commit 788dc10

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

  • packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon

‎packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -520,21 +520,23 @@ JNIArgs convertJSIArgsToJNIArgs(
520520
return jniArgs;
521521
}
522522

523-
jsi::Value convertFromJMapToValue(JNIEnv* env, jsi::Runtime& rt, jobject arg) {
523+
struct JArguments : public jni::JavaClass<JArguments> {
524+
constexpr static auto kJavaDescriptor =
525+
"Lcom/facebook/react/bridge/Arguments;";
526+
524527
// We currently use Java Argument.makeNativeMap() method to do this conversion
525528
// This could also be done purely in C++, but iterative over map methods
526529
// but those may end up calling reflection methods anyway
527530
// TODO (axe) Investigate the best way to convert Java Map to Value
528-
jclass jArguments = env->FindClass("com/facebook/react/bridge/Arguments");
529-
static jmethodID jMakeNativeMap = env->GetStaticMethodID(
530-
jArguments,
531-
"makeNativeMap",
532-
"(Ljava/util/Map;)Lcom/facebook/react/bridge/WritableNativeMap;");
533-
auto constants = env->CallStaticObjectMethod(jArguments, jMakeNativeMap, arg);
534-
auto jResult = jni::adopt_local(constants);
535-
auto result = jni::static_ref_cast<NativeMap::jhybridobject>(jResult);
536-
return jsi::valueFromDynamic(rt, result->cthis()->consume());
537-
}
531+
static jni::local_ref<WritableNativeMap::jhybridobject> makeNativeMap(
532+
jni::alias_ref<jni::JMap<jstring, jobject>> map) {
533+
static const auto method =
534+
javaClassStatic()
535+
->getStaticMethod<WritableNativeMap::jhybridobject(
536+
jni::alias_ref<jni::JMap<jstring, jobject>>)>("makeNativeMap");
537+
return method(javaClassStatic(), map);
538+
}
539+
};
538540

539541
/**
540542
* Creates JSError with current JS runtime stack and Throwable stack trace.
@@ -720,15 +722,20 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
720722
TMPL::syncMethodCallArgConversionEnd(moduleName, methodName);
721723
TMPL::syncMethodCallExecutionStart(moduleName, methodName);
722724

723-
auto constantsMap = (jobject)env->CallObjectMethod(instance, methodID);
725+
auto constantsMap = jni::adopt_local(
726+
static_cast<jni::JMap<jstring, jobject>::javaobject>(
727+
env->CallObjectMethod(instance, methodID)));
724728
checkJNIErrorForMethodCall();
725729

726730
TMPL::syncMethodCallExecutionEnd(moduleName, methodName);
727731
TMPL::syncMethodCallReturnConversionStart(moduleName, methodName);
728732

729-
jsi::Value returnValue = constantsMap == nullptr
730-
? jsi::Value::undefined()
731-
: convertFromJMapToValue(env, runtime, constantsMap);
733+
jsi::Value returnValue = jsi::Value::undefined();
734+
if (constantsMap) {
735+
auto nativeMap = JArguments::makeNativeMap(constantsMap);
736+
returnValue =
737+
jsi::valueFromDynamic(runtime, nativeMap->cthis()->consume());
738+
}
732739

733740
TMPL::syncMethodCallReturnConversionEnd(moduleName, methodName);
734741
TMPL::syncMethodCallEnd(moduleName, methodName);

0 commit comments

Comments
 (0)