Skip to content

Commit

Permalink
Use hasteModuleName for C++ Turbo Module structs (#44630)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #44630

Changelog:
[General][Breaking] Use hasteModuleName for C++ Turbo Module structs

This changes the names of C++ Turbo Modules structs to use the `hasteModuleName`.

Example: `NativeMyAbcModule.js` with this spec:
```
export type ValueStruct = {
  x: number,
  y: string,
  z: ObjectStruct,
};

export interface Spec extends TurboModule {
  +getValueStruct: () => ValueStruct
}

export default (TurboModuleRegistry.get<Spec>('MyAbcModuleCxx'): ?Spec);
```

Before now we generated a base C++ struct with the name:
```
MyAbcModuleCxxValueStruct
           ^^^
```

Now the generate name is:
```
NativeMyAbcModuleValueStruct
^^^^^^
```

## Changes:
- No `Cxx` injected anymore
- Ensure base struct is `Native` prefixed (all RN JS TM specs start with it)

## Why?
- The `Cxx` extension is a temporary hint to react-native-codegen to enable extra capabilities and might disappear eventually
- The C++ base struct name should be 'stable'
- The name of the exported TM JS spec `TurboModuleRegistry.get<Spec>(...)` is abritrary, the hasteName is not
- The name of the RN JS TM spec must start with `Native` which better guarantees a consistent naming scheme for these generated base class
- The C++ Turbo Module base class has now the same prefix as the generated structs - `NativeMyAbcModule` for the example above

Reviewed By: cipolleschi

Differential Revision: D57599257

fbshipit-source-id: 4fafe6c7e920737fa766bd7e8e68e521f608e775
  • Loading branch information
christophpurrer authored and facebook-github-bot committed May 23, 2024
1 parent 95de14d commit 07261d0
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -923,18 +923,18 @@ private:



#pragma mark - SampleTurboModuleAnimal
#pragma mark - NativeSampleTurboModuleAnimal

template <typename P0>
struct SampleTurboModuleAnimal {
struct NativeSampleTurboModuleAnimal {
P0 name;
bool operator==(const SampleTurboModuleAnimal &other) const {
bool operator==(const NativeSampleTurboModuleAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleAnimalBridging {
struct NativeSampleTurboModuleAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -1117,18 +1117,18 @@ private:



#pragma mark - SampleTurboModuleArraysAnimal
#pragma mark - NativeSampleTurboModuleArraysAnimal

template <typename P0>
struct SampleTurboModuleArraysAnimal {
struct NativeSampleTurboModuleArraysAnimal {
P0 name;
bool operator==(const SampleTurboModuleArraysAnimal &other) const {
bool operator==(const NativeSampleTurboModuleArraysAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleArraysAnimalBridging {
struct NativeSampleTurboModuleArraysAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -1311,18 +1311,18 @@ private:



#pragma mark - SampleTurboModuleNullableAnimal
#pragma mark - NativeSampleTurboModuleNullableAnimal

template <typename P0>
struct SampleTurboModuleNullableAnimal {
struct NativeSampleTurboModuleNullableAnimal {
P0 name;
bool operator==(const SampleTurboModuleNullableAnimal &other) const {
bool operator==(const NativeSampleTurboModuleNullableAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleNullableAnimalBridging {
struct NativeSampleTurboModuleNullableAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -1505,18 +1505,18 @@ private:



#pragma mark - SampleTurboModuleNullableAndOptionalAnimal
#pragma mark - NativeSampleTurboModuleNullableAndOptionalAnimal

template <typename P0>
struct SampleTurboModuleNullableAndOptionalAnimal {
struct NativeSampleTurboModuleNullableAndOptionalAnimal {
P0 name;
bool operator==(const SampleTurboModuleNullableAndOptionalAnimal &other) const {
bool operator==(const NativeSampleTurboModuleNullableAndOptionalAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleNullableAndOptionalAnimalBridging {
struct NativeSampleTurboModuleNullableAndOptionalAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -1701,18 +1701,18 @@ private:



#pragma mark - SampleTurboModuleOptionalAnimal
#pragma mark - NativeSampleTurboModuleOptionalAnimal

template <typename P0>
struct SampleTurboModuleOptionalAnimal {
struct NativeSampleTurboModuleOptionalAnimal {
P0 name;
bool operator==(const SampleTurboModuleOptionalAnimal &other) const {
bool operator==(const NativeSampleTurboModuleOptionalAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleOptionalAnimalBridging {
struct NativeSampleTurboModuleOptionalAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -2877,18 +2877,18 @@ private:



#pragma mark - SampleTurboModuleAnimal
#pragma mark - NativeSampleTurboModuleAnimal

template <typename P0>
struct SampleTurboModuleAnimal {
struct NativeSampleTurboModuleAnimal {
P0 name;
bool operator==(const SampleTurboModuleAnimal &other) const {
bool operator==(const NativeSampleTurboModuleAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleAnimalBridging {
struct NativeSampleTurboModuleAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -3071,18 +3071,18 @@ private:



#pragma mark - SampleTurboModuleArraysAnimal
#pragma mark - NativeSampleTurboModuleArraysAnimal

template <typename P0>
struct SampleTurboModuleArraysAnimal {
struct NativeSampleTurboModuleArraysAnimal {
P0 name;
bool operator==(const SampleTurboModuleArraysAnimal &other) const {
bool operator==(const NativeSampleTurboModuleArraysAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleArraysAnimalBridging {
struct NativeSampleTurboModuleArraysAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -3265,18 +3265,18 @@ private:



#pragma mark - SampleTurboModuleNullableAnimal
#pragma mark - NativeSampleTurboModuleNullableAnimal

template <typename P0>
struct SampleTurboModuleNullableAnimal {
struct NativeSampleTurboModuleNullableAnimal {
P0 name;
bool operator==(const SampleTurboModuleNullableAnimal &other) const {
bool operator==(const NativeSampleTurboModuleNullableAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleNullableAnimalBridging {
struct NativeSampleTurboModuleNullableAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -3459,18 +3459,18 @@ private:



#pragma mark - SampleTurboModuleNullableAndOptionalAnimal
#pragma mark - NativeSampleTurboModuleNullableAndOptionalAnimal

template <typename P0>
struct SampleTurboModuleNullableAndOptionalAnimal {
struct NativeSampleTurboModuleNullableAndOptionalAnimal {
P0 name;
bool operator==(const SampleTurboModuleNullableAndOptionalAnimal &other) const {
bool operator==(const NativeSampleTurboModuleNullableAndOptionalAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleNullableAndOptionalAnimalBridging {
struct NativeSampleTurboModuleNullableAndOptionalAnimalBridging {
static T types;

static T fromJs(
Expand Down Expand Up @@ -3655,18 +3655,18 @@ private:



#pragma mark - SampleTurboModuleOptionalAnimal
#pragma mark - NativeSampleTurboModuleOptionalAnimal

template <typename P0>
struct SampleTurboModuleOptionalAnimal {
struct NativeSampleTurboModuleOptionalAnimal {
P0 name;
bool operator==(const SampleTurboModuleOptionalAnimal &other) const {
bool operator==(const NativeSampleTurboModuleOptionalAnimal &other) const {
return name == other.name;
}
};

template <typename T>
struct SampleTurboModuleOptionalAnimalBridging {
struct NativeSampleTurboModuleOptionalAnimalBridging {
static T types;

static T fromJs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function translatePrimitiveJSTypeToCpp(
}

function createStructsString(
moduleName: string,
hasteModuleName: string,
aliasMap: NativeModuleAliasMap,
resolveAlias: AliasResolver,
enumMap: NativeModuleEnumMap,
Expand All @@ -221,7 +221,7 @@ function createStructsString(
v: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>,
) =>
translatePrimitiveJSTypeToCpp(
moduleName,
hasteModuleName,
parentObjectAlias,
v.typeAnnotation,
false,
Expand All @@ -236,7 +236,7 @@ function createStructsString(
if (value.properties.length === 0) {
return '';
}
const structName = `${moduleName}${alias}`;
const structName = `${hasteModuleName}${alias}`;
const templateParameter = value.properties.filter(
v =>
!isDirectRecursiveMember(alias, v.typeAnnotation) &&
Expand Down Expand Up @@ -530,7 +530,7 @@ module.exports = {
} = nativeModules[hasteModuleName];
const resolveAlias = createAliasResolver(aliasMap);
const structs = createStructsString(
moduleName,
hasteModuleName,
aliasMap,
resolveAlias,
enumMap,
Expand Down
Loading

0 comments on commit 07261d0

Please sign in to comment.