Implemented C++ TurboModule compile time spec validation #4656
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The main difference between NativeModules and TurboModules is that the TurboModules have a module API spec written in Flow language that can be used to generate a spec in a native language such as C++ so that the API shape is the same between JavaScript and the native code.
Next time when spec is changed, the native code spec is regenerated and any API mismatch could be found at compile time.
In this PR we are introducing the shape of the C++ TurboModule spec and its match against the hand-written TurboModule code.
There are two important design decisions:
REACT_METHODorREACT_SYNC_METHODto associate the method with a JS name.structwith custom attributes that can be inherited from any other type or not inherited at all.What is the TurboModule spec and how it is verified?
Each TurboModule spec is a class that has two important parts:
constexpr ValidateModulemethod that validates the method signatures against the provided module class.For example, a Turbo module with two asynchronous methods
AddandNegatePromiseand one synchronous methodSayHelloSynccould have the following generated code:Note that the biggest part of the generated code are the error messages. The goal is to help developers to write the code that matches the TurboModule specification.
Currently we do the following checks at compile time:
REACT_METHODorREACT_SYNC_METHODcustom attribute that exports it with a name used in the spec. Name of the C++ method could be different from the JS name. We only check the JS name.The module matching the spec could be implemented as the following:
This PR has only the initial change. We plan to iterate over it. The issues to be addressed in future:
Reactnamespace instead of the longwinrt::Microsoft::ReactNative.Microsoft Reviewers: Open in CodeFlow