Skip to content

Commit 1852eff

Browse files
RSNarafacebook-github-bot
authored andcommitted
Default initialize properties in custom structs
Summary: If a native module spec declares a custom type: https://www.internalfb.com/code/fbsource/[f6aeb413dbc3]/xplat/js/RKJSModules/Libraries/hsr/tm/code/editable_object/NativeEditableObjectModule.js?lines=151-154 The codegen will generate a c++ struct for that type, like so: ``` template <typename P0, typename P1> struct NativeEditableObjectModuleChangedProperty { P0 propertyPath; P1 value; bool operator==(const NativeEditableObjectModuleChangedProperty &other) const { return propertyPath == other.propertyPath && value == other.value; } }; ``` ## Problem People can sometimes forget to default initialize this struct: - **Report:** [post](https://fb.workplace.com/groups/rn.support/permalink/28506993098922601/) - **Diff #1:** D74868762 - **Diff #2:** D77182083 In this scenario, all the members contain garbage data. ## Changes This diff ensures that all the members are always default initialized, like so: ``` template <typename P0, typename P1> struct NativeEditableObjectModuleChangedProperty { P0 propertyPath{}; P1 value{}; bool operator==(const NativeEditableObjectModuleChangedProperty &other) const { return propertyPath == other.propertyPath && value == other.value; } }; ``` This way, even if you forget to default initailize the struct, the members will still be default initialized. ## Concerns/Risks This could cause compilation issues in some of our existing native modules. (if one of the member types doesn't have a default constructor). Code could start behaving differently, now that all the members are default initialized. (my hunch is that the risk from this causing a problem is low). Changelog: [General][Changed] c++ tm codegen: Default initialize properties in custom structs Reviewed By: christophpurrer Differential Revision: D77315742
1 parent 847721d commit 1852eff

File tree

3 files changed

+112
-82
lines changed

3 files changed

+112
-82
lines changed

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

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
348348

349349
template <typename P0>
350350
struct NativeEnumTurboModuleStateType {
351-
P0 state;
351+
P0 state{};
352+
352353
bool operator==(const NativeEnumTurboModuleStateType &other) const {
353354
return state == other.state;
354355
}
@@ -389,11 +390,12 @@ struct NativeEnumTurboModuleStateTypeBridging {
389390

390391
template <typename P0, typename P1, typename P2, typename P3, typename P4>
391392
struct NativeEnumTurboModuleStateTypeWithEnums {
392-
P0 state;
393-
P1 regular;
394-
P2 str;
395-
P3 num;
396-
P4 lowerCase;
393+
P0 state{};
394+
P1 regular{};
395+
P2 str{};
396+
P3 num{};
397+
P4 lowerCase{};
398+
397399
bool operator==(const NativeEnumTurboModuleStateTypeWithEnums &other) const {
398400
return state == other.state && regular == other.regular && str == other.str && num == other.num && lowerCase == other.lowerCase;
399401
}
@@ -862,8 +864,9 @@ private:
862864

863865
template <typename P0, typename P1>
864866
struct NativePartialAnnotationTurboModuleSomeObj {
865-
P0 a;
866-
P1 b;
867+
P0 a{};
868+
P1 b{};
869+
867870
bool operator==(const NativePartialAnnotationTurboModuleSomeObj &other) const {
868871
return a == other.a && b == other.b;
869872
}
@@ -1055,7 +1058,8 @@ private:
10551058

10561059
template <typename P0>
10571060
struct NativeSampleTurboModuleAnimal {
1058-
P0 name;
1061+
P0 name{};
1062+
10591063
bool operator==(const NativeSampleTurboModuleAnimal &other) const {
10601064
return name == other.name;
10611065
}
@@ -1257,7 +1261,8 @@ private:
12571261

12581262
template <typename P0>
12591263
struct NativeSampleTurboModuleArraysAnimal {
1260-
P0 name;
1264+
P0 name{};
1265+
12611266
bool operator==(const NativeSampleTurboModuleArraysAnimal &other) const {
12621267
return name == other.name;
12631268
}
@@ -1459,7 +1464,8 @@ private:
14591464

14601465
template <typename P0>
14611466
struct NativeSampleTurboModuleNullableAnimal {
1462-
P0 name;
1467+
P0 name{};
1468+
14631469
bool operator==(const NativeSampleTurboModuleNullableAnimal &other) const {
14641470
return name == other.name;
14651471
}
@@ -1661,7 +1667,8 @@ private:
16611667

16621668
template <typename P0>
16631669
struct NativeSampleTurboModuleNullableAndOptionalAnimal {
1664-
P0 name;
1670+
P0 name{};
1671+
16651672
bool operator==(const NativeSampleTurboModuleNullableAndOptionalAnimal &other) const {
16661673
return name == other.name;
16671674
}
@@ -1865,7 +1872,8 @@ private:
18651872

18661873
template <typename P0>
18671874
struct NativeSampleTurboModuleOptionalAnimal {
1868-
P0 name;
1875+
P0 name{};
1876+
18691877
bool operator==(const NativeSampleTurboModuleOptionalAnimal &other) const {
18701878
return name == other.name;
18711879
}
@@ -2478,7 +2486,8 @@ struct Bridging<NativeEnumTurboModuleStatusStrEnum> {
24782486

24792487
template <typename P0>
24802488
struct NativeEnumTurboModuleStateType {
2481-
P0 state;
2489+
P0 state{};
2490+
24822491
bool operator==(const NativeEnumTurboModuleStateType &other) const {
24832492
return state == other.state;
24842493
}
@@ -2519,11 +2528,12 @@ struct NativeEnumTurboModuleStateTypeBridging {
25192528

25202529
template <typename P0, typename P1, typename P2, typename P3, typename P4>
25212530
struct NativeEnumTurboModuleStateTypeWithEnums {
2522-
P0 state;
2523-
P1 regular;
2524-
P2 str;
2525-
P3 num;
2526-
P4 lowerCase;
2531+
P0 state{};
2532+
P1 regular{};
2533+
P2 str{};
2534+
P3 num{};
2535+
P4 lowerCase{};
2536+
25272537
bool operator==(const NativeEnumTurboModuleStateTypeWithEnums &other) const {
25282538
return state == other.state && regular == other.regular && str == other.str && num == other.num && lowerCase == other.lowerCase;
25292539
}
@@ -2992,8 +3002,9 @@ private:
29923002

29933003
template <typename P0, typename P1>
29943004
struct NativePartialAnnotationTurboModuleSomeObj {
2995-
P0 a;
2996-
P1 b;
3005+
P0 a{};
3006+
P1 b{};
3007+
29973008
bool operator==(const NativePartialAnnotationTurboModuleSomeObj &other) const {
29983009
return a == other.a && b == other.b;
29993010
}
@@ -3185,7 +3196,8 @@ private:
31853196

31863197
template <typename P0>
31873198
struct NativeSampleTurboModuleAnimal {
3188-
P0 name;
3199+
P0 name{};
3200+
31893201
bool operator==(const NativeSampleTurboModuleAnimal &other) const {
31903202
return name == other.name;
31913203
}
@@ -3387,7 +3399,8 @@ private:
33873399

33883400
template <typename P0>
33893401
struct NativeSampleTurboModuleArraysAnimal {
3390-
P0 name;
3402+
P0 name{};
3403+
33913404
bool operator==(const NativeSampleTurboModuleArraysAnimal &other) const {
33923405
return name == other.name;
33933406
}
@@ -3589,7 +3602,8 @@ private:
35893602

35903603
template <typename P0>
35913604
struct NativeSampleTurboModuleNullableAnimal {
3592-
P0 name;
3605+
P0 name{};
3606+
35933607
bool operator==(const NativeSampleTurboModuleNullableAnimal &other) const {
35943608
return name == other.name;
35953609
}
@@ -3791,7 +3805,8 @@ private:
37913805

37923806
template <typename P0>
37933807
struct NativeSampleTurboModuleNullableAndOptionalAnimal {
3794-
P0 name;
3808+
P0 name{};
3809+
37953810
bool operator==(const NativeSampleTurboModuleNullableAndOptionalAnimal &other) const {
37963811
return name == other.name;
37973812
}
@@ -3995,7 +4010,8 @@ private:
39954010

39964011
template <typename P0>
39974012
struct NativeSampleTurboModuleOptionalAnimal {
3998-
P0 name;
4013+
P0 name{};
4014+
39994015
bool operator==(const NativeSampleTurboModuleOptionalAnimal &other) const {
40004016
return name == other.name;
40014017
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ function createStructsString(
298298
299299
template <${templateParameterWithTypename}>
300300
struct ${structName} {
301-
${templateMemberTypes.map(v => ' ' + v).join(';\n')};
301+
${templateMemberTypes.map(v => ' ' + v + '{};\n').join('')}
302302
bool operator==(const ${structName} &other) const {
303303
return ${value.properties
304304
.map(v => `${v.name} == other.${v.name}`)

0 commit comments

Comments
 (0)