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
@@ -33,10 +33,24 @@ ConstantsStruct NativeCxxModuleExample::getConstants(jsi::Runtime &rt) {
return ConstantsStruct{true, 69, "react-native"};
}
EnumInt NativeCxxModuleExample::getEnum(jsi::Runtime &rt, EnumInt arg) {
CustomEnumInt NativeCxxModuleExample::getCustomEnum(
jsi::Runtime &rt,
CustomEnumInt arg) {
return arg;
}
NativeCxxModuleExampleCxxEnumFloat NativeCxxModuleExample::getNumEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumInt arg) {
return NativeCxxModuleExampleCxxEnumFloat::FB;
}
NativeCxxModuleExampleCxxEnumStr NativeCxxModuleExample::getStrEnum(
jsi::Runtime &rt,
NativeCxxModuleExampleCxxEnumNone arg) {
return NativeCxxModuleExampleCxxEnumStr::SB;
}
std::map<std::string, std::optional<int32_t>> NativeCxxModuleExample::getMap(
jsi::Runtime &rt,
std::map<std::string, std::optional<int32_t>> arg) {