Skip to content

Commit c09ac69

Browse files
akawrykowfacebook-github-bot
authored andcommitted
Respect enum values for TurboModule c++ codegen
Summary: Defining an enum like this: ```js export enum CustomPropertyEditor { BitMask = 0, Entity = 1, Slider = 2, AudioEvent = 3, CollisionLayer = 4, MaterialComponentDeprecatedProperty = 5, MeshMaterialList = 6, Submesh = 7, // CoreUiLayout (8) removed — rendered identically to ClassOrList MaterialMapJson = 9, AnimationTable = 10, SkeletonAsset = 11, NavMeshAreaType = 12, VFXAsset = 13, Table = 14, VariableTable = 15, AudioBus = 16, LodSettings = 17, WorldSearch = 18, EntityMaterialList = 19, NpcId = 20, LightingModelVersion = 21, PlatformSelector = 22, ComponentReference = 23, } ``` (notice number 8 ), will cause the generated enum to actually look like: ``` enum class NativeEditableObjectModuleCustomPropertyEditor { BitMask, Entity, Slider, AudioEvent, CollisionLayer, MaterialComponentDeprecatedProperty, MeshMaterialList, Submesh, MaterialMapJson, AnimationTable, ... }; ``` in other words, the values don't match up after the core ui value. This is quite dangerous and I'm surprised no one has ever noticed that This diff fixes things such that if an integer value is explicitly assigned, the value is preserved in the generated enum. Differential Revision: D101229471
1 parent f8fa76f commit c09ac69

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ struct Bridging<NativeEnumTurboModuleStatusLowerCaseEnum> {
152152

153153
#pragma mark - NativeEnumTurboModuleStatusNumEnum
154154

155-
enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
155+
enum class NativeEnumTurboModuleStatusNumEnum { Active = 2, Paused = 1, Off = 0 };
156156

157157
template <>
158158
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {
@@ -1749,7 +1749,7 @@ struct Bridging<NativeEnumTurboModuleStatusLowerCaseEnum> {
17491749

17501750
#pragma mark - NativeEnumTurboModuleStatusNumEnum
17511751

1752-
enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
1752+
enum class NativeEnumTurboModuleStatusNumEnum { Active = 2, Paused = 1, Off = 0 };
17531753

17541754
template <>
17551755
struct Bridging<NativeEnumTurboModuleStatusNumEnum> {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,15 @@ function generateEnum(
537537

538538
return EnumTemplate({
539539
enumName,
540-
values: members.map(member => toSafeCppString(member.name)).join(', '),
540+
values: members
541+
.map(member => {
542+
const name = toSafeCppString(member.name);
543+
if (Number.isInteger(member.value.value)) {
544+
return `${name} = ${member.value.value}`;
545+
}
546+
return name;
547+
})
548+
.join(', '),
541549
fromCases,
542550
toCases,
543551
nativeEnumMemberType,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ namespace facebook::react {
156156

157157
#pragma mark - NativeSampleTurboModuleEnumInt
158158

159-
enum class NativeSampleTurboModuleEnumInt { IA, IB };
159+
enum class NativeSampleTurboModuleEnumInt { IA = 23, IB = 42 };
160160

161161
template <>
162162
struct Bridging<NativeSampleTurboModuleEnumInt> {
@@ -1713,7 +1713,7 @@ namespace facebook::react {
17131713

17141714
#pragma mark - NativeSampleTurboModuleNumEnum
17151715

1716-
enum class NativeSampleTurboModuleNumEnum { ONE, TWO };
1716+
enum class NativeSampleTurboModuleNumEnum { ONE = 1, TWO = 2 };
17171717

17181718
template <>
17191719
struct Bridging<NativeSampleTurboModuleNumEnum> {
@@ -1741,7 +1741,7 @@ struct Bridging<NativeSampleTurboModuleNumEnum> {
17411741

17421742
#pragma mark - NativeSampleTurboModuleFloatEnum
17431743

1744-
enum class NativeSampleTurboModuleFloatEnum { POINT_ZERO, POINT_ONE, POINT_TWO };
1744+
enum class NativeSampleTurboModuleFloatEnum { POINT_ZERO = 0, POINT_ONE, POINT_TWO };
17451745

17461746
template <>
17471747
struct Bridging<NativeSampleTurboModuleFloatEnum> {

0 commit comments

Comments
 (0)