[TM][C++] Improve C++ TurboModule system
@RSNara is already working on this.
Since Feb 19, 2020.
- Dominant language
- C++
- Stars
- 127k
- Forks
- 25.3k
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 4
Description
Context
The C++ TurboModule system, unlike the ObjC and Java systems, is fairly immature. It has a lot of problems that need to be addressed, and gaps that need to be filled. Unfortunately, at Facebook, we only have a handful of C++ NativeModules. Additionally, there's no urgency to convert these NativeModules to C++-only TurboModules, because these NativeModules are already going through a bridging layer in the TurboModule system (i.e: we could turn off the old NativeModule system and still have these NativeModules fully functional). Therefore, it's difficult to prioritize development of the C++ TurboModule system internally.
For transparency, this is my focus in H1 2020:
- Open source and internal iOS NativeModule migration.
- Internal rollout of the TurboModule system (on both iOS and Android).
- The effort to make TurboModules and Codegen open source ready.
With all the things on my plate this half, I find it unlikely that the C++ TurboModule system will be finished any time soon. So, this GitHub issue documents the work that's required to build out the C++ TurboModule system. If anyone is interested in taking ownership over this portion of the TurboModule project, please feel free to comment below. I'd love to work together and see this completed. 😁
Problem
This is currently the interface of our C++-only TurboModules:
class SampleTurboCxxModule : public NativeSampleTurboCxxModuleSpecJSI {
public:
SampleTurboCxxModule(std::shared_ptr<CallInvoker> jsInvoker);
void voidFunc(jsi::Runtime &rt) override;
bool getBool(jsi::Runtime &rt, bool arg) override;
double getNumber(jsi::Runtime &rt, double arg) override;
jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) override;
jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) override;
jsi::Object getObject(jsi::Runtime &rt, const jsi::Object &arg) override;
jsi::Object getValue(
jsi::Runtime &rt,
double x,
const jsi::String &y,
const jsi::Object &z) override;
void getValueWithCallback(jsi::Runtime &rt, const jsi::Function &callback)
override;
jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) override;
jsi::Object getConstants(jsi::Runtime &rt) override;
};
There are a few problems with this API.
- Hermes is a "bring your own locks" VM. The application/framework using Hermes is responsible for making sure that
jsi::Runtimeis accessed safely. This simply isn't possible if we provide all C++-only TurboModules with access to thejsi::Runtime. - It's not a good idea to give C++-only TurboModules ownership of JSI objects (eg:
jsi::Function,jsi::String,jsi::Array,jsi::Object). First, this isn't a very clean API. Second, by design, many of these JSI objects (jsi::Functionespecially) can't outlive thejsi::Runtime(see the jsi.h). So, we should had C++-only TurboModules safe wrappers around these objects, so that the TurboModule framework can manage their lifecycles.
Other things to think about:
- How do we perform cleanup for C++-only TurboModules? Should we expect C++-only TurboModules to have a custom invalidate method, or is using their destructor fine?
- There is no
CxxTurboModuleclass, like we haveJavaTurboModule, andObjCTurboModule. If we want to make C++-only TurboModules more robust without bloating the codegen, this class would be necessary.
How do I start?
There is currently only one pure C++ TurboModule: SampleTurboCxxModule. This TurboModule extends its "code-generated" spec NativeSampleTurboCxxModuleSpecJSI (the spec is really just hand-written). This spec directly extends TurboModule, as you can see here.
- Create a new
CxxTurboModuleclass and have it extend TurboModule. HaveNativeSampleTurboCxxModuleSpecJSIextendCxxTurboModule. - You can call into CxxTurboModule from the "code-generated" __hostFunctions in
NativeSampleTurboCxxModuleSpecJSI. Feel free to modify the contents of the __hostFunctions in the codegen. Please look atRCTSampleTurboModuleSpec.mmfor an example of how we do this in iOS. Some of the responsibilities ofCxxTurboModule:- Convert JS arguments (JSI objects) to vanilla C++ objects.
- What do we convert the
Objecttype to? Should we usefolly::dynamic? - What do we convert object literals (eg:
{| foo: bar |}) to? In ObjC, we convert them to structs, since we know the type of each property. - What do we convert
Array<T>to? Should we just use an STL container?
- What do we convert the
- Convert C++ returns to JSI objects.
- How do we handle
Promisereturns? - Do we simply pass in the C++-only TurboModule method a
resolveand arejectlambda, like in ObjC, or do we pass in a "Promise" object, like we do in Android. Alternatively, should we leverage some STL data structure (std::promise)?
- How do we handle
- Dispatch async method calls appropriately on each platform. This will require the
CxxTurboModuleconstructor to accept a nativeCallInvokerthat dispatches work to a background thread.- On iOS, every C++ NativeModule calls its async methods on its own ObjC method queue.
- On Android, all NativeModule async methods dispatch to the same NativeModules thread.
- Asynchronously invoke JS callbacks (ex:
jsi::Functions) on the JS Thread. This will require theCxxTurboModuleconstructor to accept aCallInvokerthat dispatches work to the JS thread. Also, we'll have to store all thejsi::Functionobjects passed from JS to C++ so that they can be invoked later by the JS thread. This presents an additional problem:- When the TurboModuleManager is destroyed, ensure that all held
jsi::Functions (i.e: JS callbacks that were passed into C++ from JS) are destroyed. In iOS and Android, we accomplish this by usingLongLivedObjectCollection, andCallbackWrapper.LongLivedObjectCollectionis cleared when the runtime is destroyed.
- When the TurboModuleManager is destroyed, ensure that all held
- Convert JS arguments (JSI objects) to vanilla C++ objects.
How do I test?
To test your changes, in RNTester on iOS, access the TurboModule in JS via TurboModuleRegistry.get('SampleTurboCxxModule').
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.