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
@@ -22,6 +22,7 @@ import {
import * as React from 'react';
import NativeCxxModuleExample, {
EnumInt,
EnumNone,
} from '../../../NativeCxxModuleExample/NativeCxxModuleExample';
type State = {|
@@ -40,7 +41,9 @@ type Examples =
| 'getArray'
| 'getBool'
| 'getConstants'
| 'getEnum'
| 'getCustomEnum'
| 'getNumEnum'
| 'getStrEnum'
| 'getMap'
| 'getNumber'
| 'getObject'
@@ -74,8 +77,9 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
]),
getBool: () => NativeCxxModuleExample?.getBool(true),
getConstants: () => NativeCxxModuleExample?.getConstants(),
getEnum: () => NativeCxxModuleExample?.getEnum(EnumInt.A),
getMap: () => NativeCxxModuleExample?.getMap({a: 1, b: null, c: 3}),
getCustomEnum: () => NativeCxxModuleExample?.getCustomEnum(EnumInt.IB),
getNumEnum: () => NativeCxxModuleExample?.getNumEnum(EnumInt.IB),
getStrEnum: () => NativeCxxModuleExample?.getStrEnum(EnumNone.NB),
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),
getObject: () =>
NativeCxxModuleExample?.getObject({a: 1, b: 'foo', c: null}),