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
installRustCrate
typescript method is called. This is in the input file for Codegen, theNative{namespace}.ts
file. - this invokes the corresponding machinery generated by
Codegen
, in Objective C and Java. - once in Objective C and Java, we find the
jsi::Runtime
and thefacebook::react::CallInvoker
from the- Objective C and
- Java.
- this then passes the
Runtime
andCallInvoker
to the C++ Turbo-Module proper. - This then calls into the generated
cpp/bindings/{namespace}.cpp
which 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.