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:

  1. when the first time the package is imported, the installRustCrate typescript method is called. This is in the input file for Codegen, the Native{namespace}.ts file.
  2. this invokes the corresponding machinery generated by Codegen, in Objective C and Java.
  3. once in Objective C and Java, we find the jsi::Runtime and the facebook::react::CallInvoker from the
  4. this then passes the Runtime and CallInvoker to the C++ Turbo-Module proper.
  5. This then calls into the generated cpp/bindings/{namespace}.cpp which implements the src/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.