Commit dc418c2
Make codegen'd TurboModule event emitters no-op when the emitter callback is absent (#57893)
Summary:
The codegen'd TurboModule `emitOn<Event>` methods invoked their `EventEmitterCallback` without checking it was set. That callback is installed by the generated `*SpecJSI` constructor, which runs when JS first looks the module up — so a native module that emits before that point invoked an empty `std::function` on iOS (`std::bad_function_call`) or a null field on Android (NPE). Native code commonly holds the module instance and pushes events well before any JS surface mounts, so call sites had to wrap every emit in a try/catch to stay crash-free.
Both generators now read the callback into a local and no-op when it is absent:
- ObjC++ (`serializeEventEmitter.js`): copies `_eventEmitterCallback`, calls it only if non-empty.
- Java (`GenerateModuleJavaSpec.js`): copies the `Nullable CxxCallbackImpl` field and null-checks it.
The local is for readability, not synchronization: the callback is installed once and never cleared, and Java reference reads are already atomic.
The `setEventEmitterCallback` lambdas had a separate lifetime bug: they captured `eventEmitterMap_` by reference and looked events up with `operator[]`. The Java/ObjC module owns the callback and can outlive the C++ `*SpecJSI` that installed it, so a stale callback dereferenced a dangling map; and `operator[]` silently default-inserted a null `shared_ptr` for an unknown event name, which the next line dereferenced. They now capture a copy of the map and use a checked `find` (`serializeModule.js`, `JavaTurboModule.cpp`). Every emitter is registered before the callback is installed, so the copy is complete.
Note the scope of the guarantee: it covers the ObjC++ and Java generators, which route through an `EventEmitterCallback`. A C++-only TurboModule (`<Module>CxxSpec`, from `GenerateModuleH.js`) has no callback to check — it emits through `eventEmitterMap_` entries its own constructor registers — so the "emit unconditionally" guidance is about the callback, not about emitting before construction finishes.
Changelog:
[General][Fixed] - TurboModule event emitters no longer throw when an event is emitted before the emitter callback is installed
Differential Revision: D1151307671 parent 3e962d3 commit dc418c2
7 files changed
Lines changed: 148 additions & 39 deletions
File tree
- packages
- react-native-codegen/src/generators/modules
- GenerateModuleObjCpp
- source
- __tests__/__snapshots__
- react-native/ReactCommon/react
- bridging/tests
- nativemodule/core/platform/android/ReactCommon
Lines changed: 14 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
85 | 91 | | |
86 | 92 | | |
87 | 93 | | |
88 | 94 | | |
89 | 95 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
95 | 104 | | |
96 | 105 | | |
97 | 106 | | |
| |||
packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeEventEmitter.js
Lines changed: 16 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
81 | 86 | | |
82 | 87 | | |
83 | 88 | | |
84 | 89 | | |
85 | 90 | | |
86 | 91 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
95 | 103 | | |
96 | 104 | | |
97 | 105 | | |
| |||
Lines changed: 9 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
86 | 90 | | |
87 | 91 | | |
88 | | - | |
89 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
90 | 97 | | |
91 | 98 | | |
92 | 99 | | |
| |||
Lines changed: 30 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
| 232 | + | |
232 | 233 | | |
233 | 234 | | |
234 | 235 | | |
| |||
250 | 251 | | |
251 | 252 | | |
252 | 253 | | |
253 | | - | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
254 | 258 | | |
255 | 259 | | |
256 | 260 | | |
257 | | - | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
258 | 265 | | |
259 | 266 | | |
260 | 267 | | |
261 | | - | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
262 | 272 | | |
263 | 273 | | |
264 | 274 | | |
265 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
266 | 279 | | |
267 | 280 | | |
268 | 281 | | |
269 | | - | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
270 | 286 | | |
271 | 287 | | |
272 | 288 | | |
273 | | - | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
274 | 293 | | |
275 | 294 | | |
276 | 295 | | |
| |||
583 | 602 | | |
584 | 603 | | |
585 | 604 | | |
| 605 | + | |
586 | 606 | | |
587 | 607 | | |
588 | 608 | | |
| |||
602 | 622 | | |
603 | 623 | | |
604 | 624 | | |
605 | | - | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
606 | 629 | | |
607 | 630 | | |
608 | 631 | | |
| |||
Lines changed: 38 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
326 | 326 | | |
327 | 327 | | |
328 | 328 | | |
329 | | - | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
330 | 333 | | |
331 | 334 | | |
332 | 335 | | |
333 | | - | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
334 | 340 | | |
335 | 341 | | |
336 | 342 | | |
337 | | - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
338 | 347 | | |
339 | 348 | | |
340 | 349 | | |
341 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
342 | 354 | | |
343 | 355 | | |
344 | 356 | | |
345 | | - | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
346 | 361 | | |
347 | 362 | | |
348 | 363 | | |
349 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
350 | 368 | | |
351 | 369 | | |
352 | 370 | | |
| |||
373 | 391 | | |
374 | 392 | | |
375 | 393 | | |
376 | | - | |
377 | | - | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
378 | 399 | | |
379 | 400 | | |
380 | 401 | | |
| |||
734 | 755 | | |
735 | 756 | | |
736 | 757 | | |
737 | | - | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
738 | 762 | | |
739 | 763 | | |
740 | 764 | | |
| |||
756 | 780 | | |
757 | 781 | | |
758 | 782 | | |
759 | | - | |
760 | | - | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
761 | 788 | | |
762 | 789 | | |
763 | 790 | | |
| |||
Lines changed: 27 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
542 | 542 | | |
543 | 543 | | |
544 | 544 | | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
545 | 572 | | |
546 | 573 | | |
547 | 574 | | |
| |||
Lines changed: 14 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1049 | 1049 | | |
1050 | 1050 | | |
1051 | 1051 | | |
1052 | | - | |
1053 | | - | |
1054 | | - | |
1055 | | - | |
1056 | | - | |
1057 | | - | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
1058 | 1066 | | |
1059 | 1067 | | |
1060 | 1068 | | |
| |||
0 commit comments