Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators (#36030)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36030

Generate enum types in c++ turbo modules.

For enums in the ts schema file such as:
```
export enum NumEnum {
  ONE = 1,
  TWO = 2,
}
```
This would export enums and the relevant Bridging to js and from js code to the spec H files such as:
```
#pragma mark - SampleTurboModuleCxxNumEnum

enum SampleTurboModuleCxxNumEnum { ONE, TWO };

template <>
struct Bridging<SampleTurboModuleCxxNumEnum> {
  static SampleTurboModuleCxxNumEnum fromJs(jsi::Runtime &rt, int32_t value) {

    if (value == 1) {
      return SampleTurboModuleCxxNumEnum::ONE;
    } else if (value == 2) {
      return SampleTurboModuleCxxNumEnum::TWO;
    } else {
      throw jsi::JSError(rt, "No appropriate enum member found for value");
    }
  }

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

```
That code would allow us to use these enums in the cxx files like this:
```
  NativeCxxModuleExampleCxxEnumInt getNumEnum(
      jsi::Runtime &rt,
      NativeCxxModuleExampleCxxEnumInt arg);
```

Changelog: [General] [Added] Generate enum types that would be allowed to be used as well as string/number in c++ turbo modules generators

Reviewed By: christophpurrer

Differential Revision: D42884147

fbshipit-source-id: d34d1fc7ba268b570821dc108444196f69a431b2
This commit is contained in:
Vitali Zaidman
2023-02-13 15:09:44 -08:00
committed by Facebook GitHub Bot
parent bf3656b758
commit ceb1d0dea6
21 changed files with 973 additions and 106 deletions
@@ -54,21 +54,21 @@ struct Bridging<ValueStruct> : NativeCxxModuleExampleCxxBaseValueStructBridging<
ObjectStruct> {};
#pragma mark - enums
enum EnumInt { A = 23, B = 42 };
enum CustomEnumInt { A = 23, B = 42 };
template <>
struct Bridging<EnumInt> {
static EnumInt fromJs(jsi::Runtime &rt, int32_t value) {
struct Bridging<CustomEnumInt> {
static CustomEnumInt fromJs(jsi::Runtime &rt, int32_t value) {
if (value == 23) {
return EnumInt::A;
return CustomEnumInt::A;
} else if (value == 42) {
return EnumInt::B;
return CustomEnumInt::B;
} else {
throw jsi::JSError(rt, "Invalid enum value");
}
}
static jsi::Value toJs(jsi::Runtime &rt, EnumInt value) {
static jsi::Value toJs(jsi::Runtime &rt, CustomEnumInt value) {
return bridging::toJs(rt, static_cast<int32_t>(value));
}
};
@@ -91,7 +91,15 @@ class NativeCxxModuleExample
ConstantsStruct getConstants(jsi::Runtime &rt);
EnumInt getEnum(jsi::Runtime &rt, EnumInt arg);
CustomEnumInt getCustomEnum(jsi::Runtime &rt, CustomEnumInt arg);
NativeCxxModuleExampleCxxEnumFloat getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg);
NativeCxxModuleExampleCxxEnumStr getStrEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumNone arg);
std::map<std::string, std::optional<int32_t>> getMap(
jsi::Runtime &rt,