mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
react-native code-gen > C++ TurboModules struct support (#35265)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/35265 This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs. You have to define concrete types for those templates to use them in your C++ TurboModule. E.g. for the JS flow type: ``` export type ObjectStruct = {| a: number, b: string, c?: ?string, |}; ``` code-gen will now generate the following template code: ``` #pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStruct { P0 a; P1 b; P2 c; bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const { return a == other.a && b == other.b && c == other.c; } }; template <typename P0, typename P1, typename P2> struct NativeCxxModuleExampleCxxBaseObjectStructBridging { static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs( jsi::Runtime &rt, const jsi::Object &value, const std::shared_ptr<CallInvoker> &jsInvoker) { NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{ bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker), bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker), bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)}; return result; } static jsi::Object toJs( jsi::Runtime &rt, const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) { auto result = facebook::jsi::Object(rt); result.setProperty(rt, "a", bridging::toJs(rt, value.a)); result.setProperty(rt, "b", bridging::toJs(rt, value.b)); if (value.c) { result.setProperty(rt, "c", bridging::toJs(rt, value.c.value())); } return result; } }; ``` and you can use it in our C++ TurboModule for example as: ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< int32_t, std::string, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< int32_t, std::string, std::optional<std::string>> {}; ``` or as ``` using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct< float, folly::StringPiece, std::optional<std::string>>; template <> struct Bridging<ObjectStruct> : NativeCxxModuleExampleCxxBaseObjectStructBridging< float, folly::StringPiece, std::optional<std::string>> {}; ``` Or as ... Changelog: [Internal] Reviewed By: rshest Differential Revision: D41133761 fbshipit-source-id: fdf36e51073cb46c5234f6121842c79a884899c7
This commit is contained in:
committed by
Facebook GitHub Bot
parent
64ea7ce6c5
commit
c0a06d2e6f
@@ -16,6 +16,7 @@ import type {
|
||||
NativeModuleTypeAnnotation,
|
||||
NativeModuleFunctionTypeAnnotation,
|
||||
NativeModulePropertyShape,
|
||||
NativeModuleAliasMap,
|
||||
} from '../../CodegenSchema';
|
||||
|
||||
import type {AliasResolver} from './Utils';
|
||||
@@ -28,8 +29,13 @@ type FilesOutput = Map<string, string>;
|
||||
const ModuleClassDeclarationTemplate = ({
|
||||
hasteModuleName,
|
||||
moduleProperties,
|
||||
}: $ReadOnly<{hasteModuleName: string, moduleProperties: string[]}>) => {
|
||||
return `class JSI_EXPORT ${hasteModuleName}CxxSpecJSI : public TurboModule {
|
||||
structs,
|
||||
}: $ReadOnly<{
|
||||
hasteModuleName: string,
|
||||
moduleProperties: string[],
|
||||
structs: string,
|
||||
}>) => {
|
||||
return `${structs}class JSI_EXPORT ${hasteModuleName}CxxSpecJSI : public TurboModule {
|
||||
protected:
|
||||
${hasteModuleName}CxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker);
|
||||
|
||||
@@ -186,6 +192,76 @@ function translatePrimitiveJSTypeToCpp(
|
||||
}
|
||||
}
|
||||
|
||||
function createStructs(
|
||||
moduleName: string,
|
||||
aliasMap: NativeModuleAliasMap,
|
||||
resolveAlias: AliasResolver,
|
||||
): string {
|
||||
return Object.keys(aliasMap)
|
||||
.map(alias => {
|
||||
const value = aliasMap[alias];
|
||||
if (value.properties.length === 0) {
|
||||
return '';
|
||||
}
|
||||
const structName = `${moduleName}Base${alias}`;
|
||||
const templateParameterWithTypename = value.properties
|
||||
.map((v, i) => 'typename P' + i)
|
||||
.join(', ');
|
||||
const templateParameter = value.properties
|
||||
.map((v, i) => 'P' + i)
|
||||
.join(', ');
|
||||
return `#pragma mark - ${structName}
|
||||
|
||||
template <${templateParameterWithTypename}>
|
||||
struct ${structName} {
|
||||
${value.properties.map((v, i) => ' P' + i + ' ' + v.name).join(';\n')};
|
||||
bool operator==(const ${structName} &other) const {
|
||||
return ${value.properties
|
||||
.map(v => `${v.name} == other.${v.name}`)
|
||||
.join(' && ')};
|
||||
}
|
||||
};
|
||||
|
||||
template <${templateParameterWithTypename}>
|
||||
struct ${structName}Bridging {
|
||||
static ${structName}<${templateParameter}> fromJs(
|
||||
jsi::Runtime &rt,
|
||||
const jsi::Object &value,
|
||||
const std::shared_ptr<CallInvoker> &jsInvoker) {
|
||||
${structName}<${templateParameter}> result{
|
||||
${value.properties
|
||||
.map(
|
||||
(v, i) =>
|
||||
` bridging::fromJs<P${i}>(rt, value.getProperty(rt, "${v.name}"), jsInvoker)`,
|
||||
)
|
||||
.join(',\n')}};
|
||||
return result;
|
||||
}
|
||||
|
||||
static jsi::Object toJs(
|
||||
jsi::Runtime &rt,
|
||||
const ${structName}<${templateParameter}> &value) {
|
||||
auto result = facebook::jsi::Object(rt);
|
||||
${value.properties
|
||||
.map((v, i) => {
|
||||
if (v.optional) {
|
||||
return ` if (value.${v.name}) {
|
||||
result.setProperty(rt, "${v.name}", bridging::toJs(rt, value.${v.name}.value()));
|
||||
}`;
|
||||
} else {
|
||||
return ` result.setProperty(rt, "${v.name}", bridging::toJs(rt, value.${v.name}));`;
|
||||
}
|
||||
})
|
||||
.join('\n')}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
`;
|
||||
})
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
function translatePropertyToCpp(
|
||||
prop: NativeModulePropertyShape,
|
||||
resolveAlias: AliasResolver,
|
||||
@@ -251,6 +327,7 @@ module.exports = {
|
||||
moduleNames: [moduleName],
|
||||
} = nativeModules[hasteModuleName];
|
||||
const resolveAlias = createAliasResolver(aliases);
|
||||
const structs = createStructs(moduleName, aliases, resolveAlias);
|
||||
|
||||
return [
|
||||
ModuleClassDeclarationTemplate({
|
||||
@@ -258,6 +335,7 @@ module.exports = {
|
||||
moduleProperties: properties.map(prop =>
|
||||
translatePropertyToCpp(prop, resolveAlias, true),
|
||||
),
|
||||
structs,
|
||||
}),
|
||||
ModuleSpecClassDeclarationTemplate({
|
||||
hasteModuleName,
|
||||
|
||||
Reference in New Issue
Block a user