NativeModule.ts and Codegen
React Native provides its own Codegen to route calls to C++ TurboModules, via Objective-C and Java/JNI.
uniffi-bindgen-react-native uses this to “install” the C++ in to the jsi::Runtime.
The install flow is sketched as follows:
- when the first time the package is imported, the
installRustCratetypescript method is called. This is in the input file for Codegen, theNative{namespace}.tsfile. - this invokes the corresponding machinery generated by
Codegen, in Objective C and Java. - once in Objective C and Java, we find the
jsi::Runtimeand thefacebook::react::CallInvokerfrom the- Objective C and
- Java.
- this then passes the
RuntimeandCallInvokerto the C++ Turbo-Module proper. - This then calls into the generated
cpp/bindings/{namespace}.cppwhich implements thesrc/bindings/{namespace}-ffi.ts.
Every other call from JS goes directly to this C++, rather than via Objective-C and Java.
This pattern of using the Codegen just for the installation flow for the bindings allowed for testing outside React Native, and could then be relatively simply templated.