Skip to content

Default initialize properties in custom structs - #52677

Closed
RSNara wants to merge 1 commit into
react:mainfrom
RSNara:export-D77315742
Closed

Default initialize properties in custom structs#52677
RSNara wants to merge 1 commit into
react:mainfrom
RSNara:export-D77315742

Conversation

@RSNara

Copy link
Copy Markdown
Contributor

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:

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

@meta-clameta-claBot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 17, 2025
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D77315742

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 react#1:** D74868762
- **Diff react#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
@facebook-github-bot

Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D77315742

@react-native-bot

Copy link
Copy Markdown
Collaborator

This PR is stale because it has been open for 180 days with no activity. It will be closed in 7 days unless you comment on it or remove the "Stale" label.

@react-native-botreact-native-bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jan 14, 2026
@react-native-bot

Copy link
Copy Markdown
Collaborator

This PR was closed because it has been stalled for 7 days with no activity.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA SignedThis label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.fb-exportedp: FacebookPartner: FacebookPartnerStaleThere has been a lack of activity on this issue and it may be closed soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@RSNara@facebook-github-bot@react-native-bot