Repository navigation
Commit 788dc10
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: #596981 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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
520 | 520 | | |
521 | 521 | | |
522 | 522 | | |
523 | | - | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
524 | 527 | | |
525 | 528 | | |
526 | 529 | | |
527 | 530 | | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
538 | 540 | | |
539 | 541 | | |
540 | 542 | | |
| |||
720 | 722 | | |
721 | 723 | | |
722 | 724 | | |
723 | | - | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
724 | 728 | | |
725 | 729 | | |
726 | 730 | | |
727 | 731 | | |
728 | 732 | | |
729 | | - | |
730 | | - | |
731 | | - | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
732 | 739 | | |
733 | 740 | | |
734 | 741 | | |
| |||
0 commit comments