Skip to content

Commit

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

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

This is a follow up to facebook#44630

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

Example: `NativeMyAbcModule.js` with this spec:
```
export enum EnumNone {
  NA,
  NB,
}

export interface Spec extends TurboModule {
  +getStrEnum: (arg: EnumNone) => EnumStr;t
}

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

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

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

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

Reviewed By: cipolleschi

Differential Revision: D57602082

fbshipit-source-id: 9ebd68b8059dfbc6e2ec11065915cf049aa3cb0b
  • Loading branch information
christophpurrer authored and facebook-github-bot committed May 23, 2024
1 parent b98c5b9 commit b7fc586
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,12 @@ struct Bridging<${enumName}> {
};

function generateEnum(
moduleName: string,
hasteModuleName: string,
origEnumName: string,
members: NativeModuleEnumMembers,
memberType: NativeModuleEnumMemberType,
): string {
const enumName = getEnumName(moduleName, origEnumName);
const enumName = getEnumName(hasteModuleName, origEnumName);

const nativeEnumMemberType: NativeEnumMemberValueType =
memberType === 'StringTypeAnnotation' ? 'std::string' : 'int32_t';
Expand Down Expand Up @@ -437,14 +437,14 @@ function generateEnum(
}

function createEnums(
moduleName: string,
hasteModuleName: string,
enumMap: NativeModuleEnumMap,
resolveAlias: AliasResolver,
): string {
return Object.entries(enumMap)
.map(([enumName, enumNode]) => {
return generateEnum(
moduleName,
hasteModuleName,
enumName,
enumNode.members,
enumNode.memberType,
Expand All @@ -455,7 +455,7 @@ function createEnums(
}

function translatePropertyToCpp(
moduleName: string,
hasteModuleName: string,
prop: NativeModulePropertyShape,
resolveAlias: AliasResolver,
enumMap: NativeModuleEnumMap,
Expand All @@ -470,7 +470,7 @@ function translatePropertyToCpp(

const paramTypes = propTypeAnnotation.params.map(param => {
const translatedParam = translatePrimitiveJSTypeToCpp(
moduleName,
hasteModuleName,
null,
param.typeAnnotation,
param.optional,
Expand All @@ -483,7 +483,7 @@ function translatePropertyToCpp(
});

const returnType = translatePrimitiveJSTypeToCpp(
moduleName,
hasteModuleName,
null,
propTypeAnnotation.returnTypeAnnotation,
false,
Expand Down Expand Up @@ -535,14 +535,14 @@ module.exports = {
resolveAlias,
enumMap,
);
const enums = createEnums(moduleName, enumMap, resolveAlias);
const enums = createEnums(hasteModuleName, enumMap, resolveAlias);

return [
ModuleClassDeclarationTemplate({
hasteModuleName,
moduleProperties: properties.map(prop =>
translatePropertyToCpp(
moduleName,
hasteModuleName,
prop,
resolveAlias,
enumMap,
Expand All @@ -556,7 +556,12 @@ module.exports = {
hasteModuleName,
moduleName,
moduleProperties: properties.map(prop =>
translatePropertyToCpp(moduleName, prop, resolveAlias, enumMap),
translatePropertyToCpp(
hasteModuleName,
prop,
resolveAlias,
enumMap,
),
),
}),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,111 +204,111 @@ Map {
namespace facebook::react {


#pragma mark - SampleTurboModuleCxxEnumInt
#pragma mark - NativeSampleTurboModuleEnumInt

enum class SampleTurboModuleCxxEnumInt { IA, IB };
enum class NativeSampleTurboModuleEnumInt { IA, IB };

template <>
struct Bridging<SampleTurboModuleCxxEnumInt> {
static SampleTurboModuleCxxEnumInt fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
struct Bridging<NativeSampleTurboModuleEnumInt> {
static NativeSampleTurboModuleEnumInt fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
double value = (double)rawValue.asNumber();
if (value == 23) {
return SampleTurboModuleCxxEnumInt::IA;
return NativeSampleTurboModuleEnumInt::IA;
} else if (value == 42) {
return SampleTurboModuleCxxEnumInt::IB;
return NativeSampleTurboModuleEnumInt::IB;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxEnumInt value) {
if (value == SampleTurboModuleCxxEnumInt::IA) {
static jsi::Value toJs(jsi::Runtime &rt, NativeSampleTurboModuleEnumInt value) {
if (value == NativeSampleTurboModuleEnumInt::IA) {
return bridging::toJs(rt, 23);
} else if (value == SampleTurboModuleCxxEnumInt::IB) {
} else if (value == NativeSampleTurboModuleEnumInt::IB) {
return bridging::toJs(rt, 42);
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - SampleTurboModuleCxxEnumFloat
#pragma mark - NativeSampleTurboModuleEnumFloat

enum class SampleTurboModuleCxxEnumFloat { FA, FB };
enum class NativeSampleTurboModuleEnumFloat { FA, FB };

template <>
struct Bridging<SampleTurboModuleCxxEnumFloat> {
static SampleTurboModuleCxxEnumFloat fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
struct Bridging<NativeSampleTurboModuleEnumFloat> {
static NativeSampleTurboModuleEnumFloat fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
double value = (double)rawValue.asNumber();
if (value == 1.23) {
return SampleTurboModuleCxxEnumFloat::FA;
return NativeSampleTurboModuleEnumFloat::FA;
} else if (value == 4.56) {
return SampleTurboModuleCxxEnumFloat::FB;
return NativeSampleTurboModuleEnumFloat::FB;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleCxxEnumFloat value) {
if (value == SampleTurboModuleCxxEnumFloat::FA) {
static jsi::Value toJs(jsi::Runtime &rt, NativeSampleTurboModuleEnumFloat value) {
if (value == NativeSampleTurboModuleEnumFloat::FA) {
return bridging::toJs(rt, 1.23);
} else if (value == SampleTurboModuleCxxEnumFloat::FB) {
} else if (value == NativeSampleTurboModuleEnumFloat::FB) {
return bridging::toJs(rt, 4.56);
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - SampleTurboModuleCxxEnumNone
#pragma mark - NativeSampleTurboModuleEnumNone

enum class SampleTurboModuleCxxEnumNone { NA, NB };
enum class NativeSampleTurboModuleEnumNone { NA, NB };

template <>
struct Bridging<SampleTurboModuleCxxEnumNone> {
static SampleTurboModuleCxxEnumNone fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
struct Bridging<NativeSampleTurboModuleEnumNone> {
static NativeSampleTurboModuleEnumNone fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
std::string value = rawValue.utf8(rt);
if (value == \\"NA\\") {
return SampleTurboModuleCxxEnumNone::NA;
return NativeSampleTurboModuleEnumNone::NA;
} else if (value == \\"NB\\") {
return SampleTurboModuleCxxEnumNone::NB;
return NativeSampleTurboModuleEnumNone::NB;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, SampleTurboModuleCxxEnumNone value) {
if (value == SampleTurboModuleCxxEnumNone::NA) {
static jsi::String toJs(jsi::Runtime &rt, NativeSampleTurboModuleEnumNone value) {
if (value == NativeSampleTurboModuleEnumNone::NA) {
return bridging::toJs(rt, \\"NA\\");
} else if (value == SampleTurboModuleCxxEnumNone::NB) {
} else if (value == NativeSampleTurboModuleEnumNone::NB) {
return bridging::toJs(rt, \\"NB\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - SampleTurboModuleCxxEnumStr
#pragma mark - NativeSampleTurboModuleEnumStr

enum class SampleTurboModuleCxxEnumStr { SA, SB };
enum class NativeSampleTurboModuleEnumStr { SA, SB };

template <>
struct Bridging<SampleTurboModuleCxxEnumStr> {
static SampleTurboModuleCxxEnumStr fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
struct Bridging<NativeSampleTurboModuleEnumStr> {
static NativeSampleTurboModuleEnumStr fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
std::string value = rawValue.utf8(rt);
if (value == \\"s---a\\") {
return SampleTurboModuleCxxEnumStr::SA;
return NativeSampleTurboModuleEnumStr::SA;
} else if (value == \\"s---b\\") {
return SampleTurboModuleCxxEnumStr::SB;
return NativeSampleTurboModuleEnumStr::SB;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, SampleTurboModuleCxxEnumStr value) {
if (value == SampleTurboModuleCxxEnumStr::SA) {
static jsi::String toJs(jsi::Runtime &rt, NativeSampleTurboModuleEnumStr value) {
if (value == NativeSampleTurboModuleEnumStr::SA) {
return bridging::toJs(rt, \\"s---a\\");
} else if (value == SampleTurboModuleCxxEnumStr::SB) {
} else if (value == NativeSampleTurboModuleEnumStr::SB) {
return bridging::toJs(rt, \\"s---b\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
Expand Down Expand Up @@ -1779,87 +1779,87 @@ Map {
namespace facebook::react {


#pragma mark - SampleTurboModuleNumEnum
#pragma mark - NativeSampleTurboModuleNumEnum

enum class SampleTurboModuleNumEnum { ONE, TWO };
enum class NativeSampleTurboModuleNumEnum { ONE, TWO };

template <>
struct Bridging<SampleTurboModuleNumEnum> {
static SampleTurboModuleNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
struct Bridging<NativeSampleTurboModuleNumEnum> {
static NativeSampleTurboModuleNumEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
double value = (double)rawValue.asNumber();
if (value == 1) {
return SampleTurboModuleNumEnum::ONE;
return NativeSampleTurboModuleNumEnum::ONE;
} else if (value == 2) {
return SampleTurboModuleNumEnum::TWO;
return NativeSampleTurboModuleNumEnum::TWO;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleNumEnum value) {
if (value == SampleTurboModuleNumEnum::ONE) {
static jsi::Value toJs(jsi::Runtime &rt, NativeSampleTurboModuleNumEnum value) {
if (value == NativeSampleTurboModuleNumEnum::ONE) {
return bridging::toJs(rt, 1);
} else if (value == SampleTurboModuleNumEnum::TWO) {
} else if (value == NativeSampleTurboModuleNumEnum::TWO) {
return bridging::toJs(rt, 2);
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - SampleTurboModuleFloatEnum
#pragma mark - NativeSampleTurboModuleFloatEnum

enum class SampleTurboModuleFloatEnum { POINT_ZERO, POINT_ONE, POINT_TWO };
enum class NativeSampleTurboModuleFloatEnum { POINT_ZERO, POINT_ONE, POINT_TWO };

template <>
struct Bridging<SampleTurboModuleFloatEnum> {
static SampleTurboModuleFloatEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
struct Bridging<NativeSampleTurboModuleFloatEnum> {
static NativeSampleTurboModuleFloatEnum fromJs(jsi::Runtime &rt, const jsi::Value &rawValue) {
double value = (double)rawValue.asNumber();
if (value == 0.0) {
return SampleTurboModuleFloatEnum::POINT_ZERO;
return NativeSampleTurboModuleFloatEnum::POINT_ZERO;
} else if (value == 0.1) {
return SampleTurboModuleFloatEnum::POINT_ONE;
return NativeSampleTurboModuleFloatEnum::POINT_ONE;
} else if (value == 0.2) {
return SampleTurboModuleFloatEnum::POINT_TWO;
return NativeSampleTurboModuleFloatEnum::POINT_TWO;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::Value toJs(jsi::Runtime &rt, SampleTurboModuleFloatEnum value) {
if (value == SampleTurboModuleFloatEnum::POINT_ZERO) {
static jsi::Value toJs(jsi::Runtime &rt, NativeSampleTurboModuleFloatEnum value) {
if (value == NativeSampleTurboModuleFloatEnum::POINT_ZERO) {
return bridging::toJs(rt, 0.0);
} else if (value == SampleTurboModuleFloatEnum::POINT_ONE) {
} else if (value == NativeSampleTurboModuleFloatEnum::POINT_ONE) {
return bridging::toJs(rt, 0.1);
} else if (value == SampleTurboModuleFloatEnum::POINT_TWO) {
} else if (value == NativeSampleTurboModuleFloatEnum::POINT_TWO) {
return bridging::toJs(rt, 0.2);
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - SampleTurboModuleStringEnum
#pragma mark - NativeSampleTurboModuleStringEnum

enum class SampleTurboModuleStringEnum { HELLO, GoodBye };
enum class NativeSampleTurboModuleStringEnum { HELLO, GoodBye };

template <>
struct Bridging<SampleTurboModuleStringEnum> {
static SampleTurboModuleStringEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
struct Bridging<NativeSampleTurboModuleStringEnum> {
static NativeSampleTurboModuleStringEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
std::string value = rawValue.utf8(rt);
if (value == \\"hello\\") {
return SampleTurboModuleStringEnum::HELLO;
return NativeSampleTurboModuleStringEnum::HELLO;
} else if (value == \\"goodbye\\") {
return SampleTurboModuleStringEnum::GoodBye;
return NativeSampleTurboModuleStringEnum::GoodBye;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, SampleTurboModuleStringEnum value) {
if (value == SampleTurboModuleStringEnum::HELLO) {
static jsi::String toJs(jsi::Runtime &rt, NativeSampleTurboModuleStringEnum value) {
if (value == NativeSampleTurboModuleStringEnum::HELLO) {
return bridging::toJs(rt, \\"hello\\");
} else if (value == SampleTurboModuleStringEnum::GoodBye) {
} else if (value == NativeSampleTurboModuleStringEnum::GoodBye) {
return bridging::toJs(rt, \\"goodbye\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ GraphNode NativeCxxModuleExample::getGraphNode(
return arg;
}

NativeCxxModuleExampleCxxEnumInt NativeCxxModuleExample::getNumEnum(
NativeCxxModuleExampleEnumInt NativeCxxModuleExample::getNumEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumInt arg) {
NativeCxxModuleExampleEnumInt arg) {
return arg;
}

NativeCxxModuleExampleCxxEnumStr NativeCxxModuleExample::getStrEnum(
NativeCxxModuleExampleEnumStr NativeCxxModuleExample::getStrEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumNone arg) {
return NativeCxxModuleExampleCxxEnumStr::SB;
NativeCxxModuleExampleEnumNone /*arg*/) {
return NativeCxxModuleExampleEnumStr::SB;
}

std::map<std::string, std::optional<int32_t>> NativeCxxModuleExample::getMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ class NativeCxxModuleExample

GraphNode getGraphNode(jsi::Runtime& rt, GraphNode arg);

NativeCxxModuleExampleCxxEnumInt getNumEnum(
NativeCxxModuleExampleEnumInt getNumEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumInt arg);
NativeCxxModuleExampleEnumInt arg);

NativeCxxModuleExampleCxxEnumStr getStrEnum(
NativeCxxModuleExampleEnumStr getStrEnum(
jsi::Runtime& rt,
NativeCxxModuleExampleCxxEnumNone arg);
NativeCxxModuleExampleEnumNone arg);

std::map<std::string, std::optional<int32_t>> getMap(
jsi::Runtime& rt,
Expand Down

0 comments on commit b7fc586

Please sign in to comment.