Skip to content

Commit

Permalink
Fixes enum codegen value cases (facebook#44654)
Browse files Browse the repository at this point in the history
Summary:
Fixes facebook#44632

## Changelog:

[GENERAL] [FIXED] - [codegen] Fixes enum codegen value cases

Pull Request resolved: facebook#44654

Test Plan: facebook#44632

Reviewed By: cipolleschi, dmytrorykun

Differential Revision: D58135645

Pulled By: cortinico

fbshipit-source-id: 5c0634ef1d1d7375d2ecfcf7f916d67fd39b7300
  • Loading branch information
zhongwuzw authored and facebook-github-bot committed Jun 7, 2024
1 parent 538dd17 commit 1a1795a
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<PROJECT_ROOT>/packages/react-native/template/.*
<PROJECT_ROOT>/packages/react-native/sdks/.*

; Ignore the codegen e2e tests
<PROJECT_ROOT>/packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeEnumTurboModule.js

; Ignore the Dangerfile
<PROJECT_ROOT>/packages/react-native-bots/dangerfile.js

Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@

packages/*/dist
vendor

packages/react-native-codegen/e2e/__test_fixtures__/modules/NativeEnumTurboModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,30 @@ export enum StatusNumEnum {
Off = 0,
}

export enum StatusLowerCaseEnum {
Active = 'active',
Paused = 'paused',
Off = 'off',
}

export type StateTypeWithEnums = {|
state: string,
regular: StatusRegularEnum,
str: StatusStrEnum,
num: StatusNumEnum,
lowerCase: StatusLowerCaseEnum,
|};

export interface Spec extends TurboModule {
+getStatusRegular: (statusProp: StateType) => StatusRegularEnum;
+getStatusStr: (statusProp: StateType) => StatusStrEnum;
+getStatusNum: (statusProp: StateType) => StatusNumEnum;
+getStatusLowerCase: (statusProp: StateType) => StatusLowerCaseEnum;
+getStateType: (
a: StatusRegularEnum,
b: StatusStrEnum,
c: StatusNumEnum,
d: StatusLowerCaseEnum,
) => StateType;
+getStateTypeWithEnums: (
paramOfTypeWithEnums: StateTypeWithEnums,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,19 @@ static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusNum(js
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asObject(rt)
);
}
static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusLowerCase(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeEnumTurboModuleCxxSpecJSI *>(&turboModule)->getStatusLowerCase(
rt,
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asObject(rt)
);
}
static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateType(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeEnumTurboModuleCxxSpecJSI *>(&turboModule)->getStateType(
rt,
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asString(rt),
count <= 1 ? throw jsi::JSError(rt, \\"Expected argument in position 1 to be passed\\") : args[1].asString(rt),
count <= 2 ? throw jsi::JSError(rt, \\"Expected argument in position 2 to be passed\\") : args[2].asNumber()
count <= 2 ? throw jsi::JSError(rt, \\"Expected argument in position 2 to be passed\\") : args[2].asNumber(),
count <= 3 ? throw jsi::JSError(rt, \\"Expected argument in position 3 to be passed\\") : args[3].asString(rt)
);
}
static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateTypeWithEnums(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
Expand All @@ -116,7 +123,8 @@ NativeEnumTurboModuleCxxSpecJSI::NativeEnumTurboModuleCxxSpecJSI(std::shared_ptr
methodMap_[\\"getStatusRegular\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusRegular};
methodMap_[\\"getStatusStr\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusStr};
methodMap_[\\"getStatusNum\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusNum};
methodMap_[\\"getStateType\\"] = MethodMetadata {3, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateType};
methodMap_[\\"getStatusLowerCase\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusLowerCase};
methodMap_[\\"getStateType\\"] = MethodMetadata {4, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateType};
methodMap_[\\"getStateTypeWithEnums\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateTypeWithEnums};
}
static jsi::Value __hostFunction_NativeNullableTurboModuleCxxSpecJSI_getBool(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
Expand Down Expand Up @@ -909,12 +917,19 @@ static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusNum(js
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asObject(rt)
);
}
static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusLowerCase(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeEnumTurboModuleCxxSpecJSI *>(&turboModule)->getStatusLowerCase(
rt,
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asObject(rt)
);
}
static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateType(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<NativeEnumTurboModuleCxxSpecJSI *>(&turboModule)->getStateType(
rt,
count <= 0 ? throw jsi::JSError(rt, \\"Expected argument in position 0 to be passed\\") : args[0].asString(rt),
count <= 1 ? throw jsi::JSError(rt, \\"Expected argument in position 1 to be passed\\") : args[1].asString(rt),
count <= 2 ? throw jsi::JSError(rt, \\"Expected argument in position 2 to be passed\\") : args[2].asNumber()
count <= 2 ? throw jsi::JSError(rt, \\"Expected argument in position 2 to be passed\\") : args[2].asNumber(),
count <= 3 ? throw jsi::JSError(rt, \\"Expected argument in position 3 to be passed\\") : args[3].asString(rt)
);
}
static jsi::Value __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateTypeWithEnums(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
Expand All @@ -929,7 +944,8 @@ NativeEnumTurboModuleCxxSpecJSI::NativeEnumTurboModuleCxxSpecJSI(std::shared_ptr
methodMap_[\\"getStatusRegular\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusRegular};
methodMap_[\\"getStatusStr\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusStr};
methodMap_[\\"getStatusNum\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusNum};
methodMap_[\\"getStateType\\"] = MethodMetadata {3, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateType};
methodMap_[\\"getStatusLowerCase\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStatusLowerCase};
methodMap_[\\"getStateType\\"] = MethodMetadata {4, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateType};
methodMap_[\\"getStateTypeWithEnums\\"] = MethodMetadata {1, __hostFunction_NativeEnumTurboModuleCxxSpecJSI_getStateTypeWithEnums};
}
static jsi::Value __hostFunction_NativeNullableTurboModuleCxxSpecJSI_getBool(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,38 @@ private:
};


#pragma mark - NativeEnumTurboModuleStatusLowerCaseEnum

enum class NativeEnumTurboModuleStatusLowerCaseEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusLowerCaseEnum> {
static NativeEnumTurboModuleStatusLowerCaseEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
std::string value = rawValue.utf8(rt);
if (value == \\"active\\") {
return NativeEnumTurboModuleStatusLowerCaseEnum::Active;
} else if (value == \\"paused\\") {
return NativeEnumTurboModuleStatusLowerCaseEnum::Paused;
} else if (value == \\"off\\") {
return NativeEnumTurboModuleStatusLowerCaseEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusLowerCaseEnum value) {
if (value == NativeEnumTurboModuleStatusLowerCaseEnum::Active) {
return bridging::toJs(rt, \\"active\\");
} else if (value == NativeEnumTurboModuleStatusLowerCaseEnum::Paused) {
return bridging::toJs(rt, \\"paused\\");
} else if (value == NativeEnumTurboModuleStatusLowerCaseEnum::Off) {
return bridging::toJs(rt, \\"off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusNumEnum

enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
Expand Down Expand Up @@ -331,14 +363,15 @@ struct NativeEnumTurboModuleStateTypeBridging {

#pragma mark - NativeEnumTurboModuleStateTypeWithEnums

template <typename P0, typename P1, typename P2, typename P3>
template <typename P0, typename P1, typename P2, typename P3, typename P4>
struct NativeEnumTurboModuleStateTypeWithEnums {
P0 state;
P1 regular;
P2 str;
P3 num;
P4 lowerCase;
bool operator==(const NativeEnumTurboModuleStateTypeWithEnums &other) const {
return state == other.state && regular == other.regular && str == other.str && num == other.num;
return state == other.state && regular == other.regular && str == other.str && num == other.num && lowerCase == other.lowerCase;
}
};

Expand All @@ -354,7 +387,8 @@ struct NativeEnumTurboModuleStateTypeWithEnumsBridging {
bridging::fromJs<decltype(types.state)>(rt, value.getProperty(rt, \\"state\\"), jsInvoker),
bridging::fromJs<decltype(types.regular)>(rt, value.getProperty(rt, \\"regular\\"), jsInvoker),
bridging::fromJs<decltype(types.str)>(rt, value.getProperty(rt, \\"str\\"), jsInvoker),
bridging::fromJs<decltype(types.num)>(rt, value.getProperty(rt, \\"num\\"), jsInvoker)};
bridging::fromJs<decltype(types.num)>(rt, value.getProperty(rt, \\"num\\"), jsInvoker),
bridging::fromJs<decltype(types.lowerCase)>(rt, value.getProperty(rt, \\"lowerCase\\"), jsInvoker)};
return result;
}

Expand All @@ -374,6 +408,10 @@ struct NativeEnumTurboModuleStateTypeWithEnumsBridging {
static jsi::Value numToJs(jsi::Runtime &rt, decltype(types.num) value) {
return bridging::toJs(rt, value);
}

static jsi::String lowerCaseToJs(jsi::Runtime &rt, decltype(types.lowerCase) value) {
return bridging::toJs(rt, value);
}
#endif

static jsi::Object toJs(
Expand All @@ -385,6 +423,7 @@ struct NativeEnumTurboModuleStateTypeWithEnumsBridging {
result.setProperty(rt, \\"regular\\", bridging::toJs(rt, value.regular, jsInvoker));
result.setProperty(rt, \\"str\\", bridging::toJs(rt, value.str, jsInvoker));
result.setProperty(rt, \\"num\\", bridging::toJs(rt, value.num, jsInvoker));
result.setProperty(rt, \\"lowerCase\\", bridging::toJs(rt, value.lowerCase, jsInvoker));
return result;
}
};
Expand All @@ -397,7 +436,8 @@ public:
virtual jsi::String getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::String getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::Value getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c) = 0;
virtual jsi::String getStatusLowerCase(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c, jsi::String d) = 0;
virtual jsi::Object getStateTypeWithEnums(jsi::Runtime &rt, jsi::Object paramOfTypeWithEnums) = 0;

};
Expand Down Expand Up @@ -446,13 +486,21 @@ private:
return bridging::callFromJs<jsi::Value>(
rt, &T::getStatusNum, jsInvoker_, instance_, std::move(statusProp));
}
jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c) override {
jsi::String getStatusLowerCase(jsi::Runtime &rt, jsi::Object statusProp) override {
static_assert(
bridging::getParameterCount(&T::getStatusLowerCase) == 2,
\\"Expected getStatusLowerCase(...) to have 2 parameters\\");

return bridging::callFromJs<jsi::String>(
rt, &T::getStatusLowerCase, jsInvoker_, instance_, std::move(statusProp));
}
jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c, jsi::String d) override {
static_assert(
bridging::getParameterCount(&T::getStateType) == 4,
\\"Expected getStateType(...) to have 4 parameters\\");
bridging::getParameterCount(&T::getStateType) == 5,
\\"Expected getStateType(...) to have 5 parameters\\");

return bridging::callFromJs<jsi::Object>(
rt, &T::getStateType, jsInvoker_, instance_, std::move(a), std::move(b), std::move(c));
rt, &T::getStateType, jsInvoker_, instance_, std::move(a), std::move(b), std::move(c), std::move(d));
}
jsi::Object getStateTypeWithEnums(jsi::Runtime &rt, jsi::Object paramOfTypeWithEnums) override {
static_assert(
Expand Down Expand Up @@ -2146,6 +2194,38 @@ private:
};


#pragma mark - NativeEnumTurboModuleStatusLowerCaseEnum

enum class NativeEnumTurboModuleStatusLowerCaseEnum { Active, Paused, Off };

template <>
struct Bridging<NativeEnumTurboModuleStatusLowerCaseEnum> {
static NativeEnumTurboModuleStatusLowerCaseEnum fromJs(jsi::Runtime &rt, const jsi::String &rawValue) {
std::string value = rawValue.utf8(rt);
if (value == \\"active\\") {
return NativeEnumTurboModuleStatusLowerCaseEnum::Active;
} else if (value == \\"paused\\") {
return NativeEnumTurboModuleStatusLowerCaseEnum::Paused;
} else if (value == \\"off\\") {
return NativeEnumTurboModuleStatusLowerCaseEnum::Off;
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for value\\");
}
}

static jsi::String toJs(jsi::Runtime &rt, NativeEnumTurboModuleStatusLowerCaseEnum value) {
if (value == NativeEnumTurboModuleStatusLowerCaseEnum::Active) {
return bridging::toJs(rt, \\"active\\");
} else if (value == NativeEnumTurboModuleStatusLowerCaseEnum::Paused) {
return bridging::toJs(rt, \\"paused\\");
} else if (value == NativeEnumTurboModuleStatusLowerCaseEnum::Off) {
return bridging::toJs(rt, \\"off\\");
} else {
throw jsi::JSError(rt, \\"No appropriate enum member found for enum value\\");
}
}
};

#pragma mark - NativeEnumTurboModuleStatusNumEnum

enum class NativeEnumTurboModuleStatusNumEnum { Active, Paused, Off };
Expand Down Expand Up @@ -2285,14 +2365,15 @@ struct NativeEnumTurboModuleStateTypeBridging {

#pragma mark - NativeEnumTurboModuleStateTypeWithEnums

template <typename P0, typename P1, typename P2, typename P3>
template <typename P0, typename P1, typename P2, typename P3, typename P4>
struct NativeEnumTurboModuleStateTypeWithEnums {
P0 state;
P1 regular;
P2 str;
P3 num;
P4 lowerCase;
bool operator==(const NativeEnumTurboModuleStateTypeWithEnums &other) const {
return state == other.state && regular == other.regular && str == other.str && num == other.num;
return state == other.state && regular == other.regular && str == other.str && num == other.num && lowerCase == other.lowerCase;
}
};

Expand All @@ -2308,7 +2389,8 @@ struct NativeEnumTurboModuleStateTypeWithEnumsBridging {
bridging::fromJs<decltype(types.state)>(rt, value.getProperty(rt, \\"state\\"), jsInvoker),
bridging::fromJs<decltype(types.regular)>(rt, value.getProperty(rt, \\"regular\\"), jsInvoker),
bridging::fromJs<decltype(types.str)>(rt, value.getProperty(rt, \\"str\\"), jsInvoker),
bridging::fromJs<decltype(types.num)>(rt, value.getProperty(rt, \\"num\\"), jsInvoker)};
bridging::fromJs<decltype(types.num)>(rt, value.getProperty(rt, \\"num\\"), jsInvoker),
bridging::fromJs<decltype(types.lowerCase)>(rt, value.getProperty(rt, \\"lowerCase\\"), jsInvoker)};
return result;
}

Expand All @@ -2328,6 +2410,10 @@ struct NativeEnumTurboModuleStateTypeWithEnumsBridging {
static jsi::Value numToJs(jsi::Runtime &rt, decltype(types.num) value) {
return bridging::toJs(rt, value);
}

static jsi::String lowerCaseToJs(jsi::Runtime &rt, decltype(types.lowerCase) value) {
return bridging::toJs(rt, value);
}
#endif

static jsi::Object toJs(
Expand All @@ -2339,6 +2425,7 @@ struct NativeEnumTurboModuleStateTypeWithEnumsBridging {
result.setProperty(rt, \\"regular\\", bridging::toJs(rt, value.regular, jsInvoker));
result.setProperty(rt, \\"str\\", bridging::toJs(rt, value.str, jsInvoker));
result.setProperty(rt, \\"num\\", bridging::toJs(rt, value.num, jsInvoker));
result.setProperty(rt, \\"lowerCase\\", bridging::toJs(rt, value.lowerCase, jsInvoker));
return result;
}
};
Expand All @@ -2351,7 +2438,8 @@ public:
virtual jsi::String getStatusRegular(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::String getStatusStr(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::Value getStatusNum(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c) = 0;
virtual jsi::String getStatusLowerCase(jsi::Runtime &rt, jsi::Object statusProp) = 0;
virtual jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c, jsi::String d) = 0;
virtual jsi::Object getStateTypeWithEnums(jsi::Runtime &rt, jsi::Object paramOfTypeWithEnums) = 0;

};
Expand Down Expand Up @@ -2400,13 +2488,21 @@ private:
return bridging::callFromJs<jsi::Value>(
rt, &T::getStatusNum, jsInvoker_, instance_, std::move(statusProp));
}
jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c) override {
jsi::String getStatusLowerCase(jsi::Runtime &rt, jsi::Object statusProp) override {
static_assert(
bridging::getParameterCount(&T::getStatusLowerCase) == 2,
\\"Expected getStatusLowerCase(...) to have 2 parameters\\");

return bridging::callFromJs<jsi::String>(
rt, &T::getStatusLowerCase, jsInvoker_, instance_, std::move(statusProp));
}
jsi::Object getStateType(jsi::Runtime &rt, jsi::String a, jsi::String b, jsi::Value c, jsi::String d) override {
static_assert(
bridging::getParameterCount(&T::getStateType) == 4,
\\"Expected getStateType(...) to have 4 parameters\\");
bridging::getParameterCount(&T::getStateType) == 5,
\\"Expected getStateType(...) to have 5 parameters\\");

return bridging::callFromJs<jsi::Object>(
rt, &T::getStateType, jsInvoker_, instance_, std::move(a), std::move(b), std::move(c));
rt, &T::getStateType, jsInvoker_, instance_, std::move(a), std::move(b), std::move(c), std::move(d));
}
jsi::Object getStateTypeWithEnums(jsi::Runtime &rt, jsi::Object paramOfTypeWithEnums) override {
static_assert(
Expand Down
Loading

0 comments on commit 1a1795a

Please sign in to comment.