Skip to content

Commit ceb1d0d

Browse files
vzaidmanfacebook-github-bot
authored andcommitted
Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)
Summary: Pull Request resolved: #36030 Generate enum types in c++ turbo modules. For enums in the ts schema file such as: ``` export enum NumEnum { ONE = 1, TWO = 2, } ``` This would export enums and the relevant Bridging to js and from js code to the spec H files such as: ``` #pragma mark - SampleTurboModuleCxxNumEnum enum SampleTurboModuleCxxNumEnum { ONE, TWO }; template <> struct Bridging<SampleTurboModuleCxxNumEnum> { static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) { if (value == 1) { return SampleTurboModuleCxxNumEnum::ONE; } else if (value == 2) { return SampleTurboModuleCxxNumEnum::TWO; } else { throw jsi::JSError(rt, "No appropriate enum member found for value"); } } static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxNumEnum value) { if (value == SampleTurboModuleCxxNumEnum::ONE) { return bridging::toJs(rt, 1); } else if (value == SampleTurboModuleCxxNumEnum::TWO) { return bridging::toJs(rt, 2); } else { throw jsi::JSError(rt, "No appropriate enum member found for enum value"); } } }; ``` That code would allow us to use these enums in the cxx files like this: ``` NativeCxxModuleExampleCxxEnumInt getNumEnum( jsi::Runtime &rt, NativeCxxModuleExampleCxxEnumInt arg); ``` Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators Reviewed By: christophpurrer Differential Revision: D42884147 fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
1 parent bf3656b commit ceb1d0d

21 files changed

+973
-106
lines changed

packages/react-native-codegen/e2e/__tests__/modules/__snapshots__/GenerateModuleH-test.js.snap

Lines changed: 330 additions & 44 deletions
Large diffs are not rendered by default.

packages/react-native-codegen/src/generators/Utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,27 @@ function indent(nice: string, spaces: number): string {
2727
.join('\n');
2828
}
2929

30+
function toPascalCase(inString: string): string {
31+
if (inString.length === 0) {
32+
return inString;
33+
}
34+
35+
return inString[0].toUpperCase() + inString.slice(1);
36+
}
37+
38+
function toSafeCppString(input: string): string {
39+
return input.split('-').map(toPascalCase).join('');
40+
}
41+
42+
function getEnumName(moduleName: string, origEnumName: string): string {
43+
const uppercasedPropName = toSafeCppString(origEnumName);
44+
return `${moduleName}${uppercasedPropName}`;
45+
}
46+
3047
module.exports = {
3148
capitalize,
3249
indent,
50+
toPascalCase,
51+
toSafeCppString,
52+
getEnumName,
3353
};

packages/react-native-codegen/src/generators/components/ComponentsGeneratorUtils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import type {
2525
const {
2626
getCppTypeForAnnotation,
2727
getEnumMaskName,
28-
getEnumName,
2928
generateStructName,
3029
getImports,
3130
} = require('./CppHelpers.js');
3231

32+
const {getEnumName} = require('../Utils');
33+
3334
function getNativeTypeFromAnnotation(
3435
componentName: string,
3536
prop:

packages/react-native-codegen/src/generators/components/CppHelpers.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,7 @@
1111
'use strict';
1212
import type {NamedShape, PropTypeAnnotation} from '../../CodegenSchema';
1313

14-
function upperCaseFirst(inString: string): string {
15-
if (inString.length === 0) {
16-
return inString;
17-
}
18-
19-
return inString[0].toUpperCase() + inString.slice(1);
20-
}
21-
22-
function toSafeCppString(input: string): string {
23-
return input.split('-').map(upperCaseFirst).join('');
24-
}
14+
const {getEnumName, toSafeCppString} = require('../Utils');
2515

2616
function toIntEnumValueName(propName: string, value: number): string {
2717
return `${toSafeCppString(propName)}${value}`;
@@ -124,11 +114,6 @@ function generateStructName(
124114
return `${componentName}${additional}Struct`;
125115
}
126116

127-
function getEnumName(componentName: string, propName: string): string {
128-
const uppercasedPropName = toSafeCppString(propName);
129-
return `${componentName}${uppercasedPropName}`;
130-
}
131-
132117
function getEnumMaskName(enumName: string): string {
133118
return `${enumName}Mask`;
134119
}
@@ -224,10 +209,8 @@ function convertDefaultTypeToString(
224209
module.exports = {
225210
convertDefaultTypeToString,
226211
getCppTypeForAnnotation,
227-
getEnumName,
228212
getEnumMaskName,
229213
getImports,
230-
toSafeCppString,
231214
toIntEnumValueName,
232215
generateStructName,
233216
generateEventStructName,

packages/react-native-codegen/src/generators/components/GenerateEventEmitterH.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ const nullthrows = require('nullthrows');
1414

1515
const {
1616
getCppTypeForAnnotation,
17-
toSafeCppString,
1817
generateEventStructName,
1918
} = require('./CppHelpers');
20-
const {indent} = require('../Utils');
19+
const {indent, toSafeCppString} = require('../Utils');
2120

2221
import type {
2322
ComponentShape,

packages/react-native-codegen/src/generators/components/GeneratePropsH.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ const {
1919
const {
2020
convertDefaultTypeToString,
2121
getEnumMaskName,
22-
getEnumName,
23-
toSafeCppString,
2422
generateStructName,
2523
toIntEnumValueName,
2624
} = require('./CppHelpers.js');
2725

26+
const {getEnumName, toSafeCppString} = require('../Utils');
27+
2828
import type {
2929
ExtendsPropsShape,
3030
NamedShape,

packages/react-native-codegen/src/generators/components/GenerateTests.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import type {ComponentShape, PropTypeAnnotation} from '../../CodegenSchema';
1313
import type {SchemaType} from '../../CodegenSchema';
1414

15-
const {getImports, toSafeCppString} = require('./CppHelpers');
15+
const {getImports} = require('./CppHelpers');
16+
17+
const {toSafeCppString} = require('../Utils');
1618

1719
type FilesOutput = Map<string, string>;
1820
type PropValueType = string | number | boolean;

packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
NativeModuleFunctionTypeAnnotation,
1919
NativeModuleParamTypeAnnotation,
2020
NativeModuleTypeAnnotation,
21+
NativeModuleEnumMap,
2122
} from '../../CodegenSchema';
2223

2324
import type {AliasResolver} from './Utils';
@@ -114,9 +115,11 @@ ${modules}
114115
type Param = NamedShape<Nullable<NativeModuleParamTypeAnnotation>>;
115116

116117
function serializeArg(
118+
moduleName: string,
117119
arg: Param,
118120
index: number,
119121
resolveAlias: AliasResolver,
122+
enumMap: NativeModuleEnumMap,
120123
): string {
121124
const {typeAnnotation: nullableTypeAnnotation, optional} = arg;
122125
const [typeAnnotation, nullable] =
@@ -208,9 +211,11 @@ function serializeArg(
208211
}
209212

210213
function serializePropertyIntoHostFunction(
214+
moduleName: string,
211215
hasteModuleName: string,
212216
property: NativeModulePropertyShape,
213217
resolveAlias: AliasResolver,
218+
enumMap: NativeModuleEnumMap,
214219
): string {
215220
const [propertyTypeAnnotation] =
216221
unwrapNullable<NativeModuleFunctionTypeAnnotation>(property.typeAnnotation);
@@ -220,7 +225,7 @@ function serializePropertyIntoHostFunction(
220225
methodName: property.name,
221226
returnTypeAnnotation: propertyTypeAnnotation.returnTypeAnnotation,
222227
args: propertyTypeAnnotation.params.map((p, i) =>
223-
serializeArg(p, i, resolveAlias),
228+
serializeArg(moduleName, p, i, resolveAlias, enumMap),
224229
),
225230
});
226231
}
@@ -239,15 +244,18 @@ module.exports = {
239244
const nativeModule = nativeModules[hasteModuleName];
240245
const {
241246
aliasMap,
247+
enumMap,
242248
spec: {properties},
243249
moduleName,
244250
} = nativeModule;
245251
const resolveAlias = createAliasResolver(aliasMap);
246252
const hostFunctions = properties.map(property =>
247253
serializePropertyIntoHostFunction(
254+
moduleName,
248255
hasteModuleName,
249256
property,
250257
resolveAlias,
258+
enumMap,
251259
),
252260
);
253261

0 commit comments

Comments
 (0)