mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
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:
committed by
Facebook GitHub Bot
parent
bf3656b758
commit
ceb1d0dea6
@@ -13,8 +13,23 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
|
||||
import {TurboModuleRegistry} from 'react-native';
|
||||
|
||||
export enum EnumInt {
|
||||
A = 23,
|
||||
B = 42,
|
||||
IA = 23,
|
||||
IB = 42,
|
||||
}
|
||||
|
||||
export enum EnumFloat {
|
||||
FA = 1.23,
|
||||
FB = 4.56,
|
||||
}
|
||||
|
||||
export enum EnumNone {
|
||||
NA,
|
||||
NB,
|
||||
}
|
||||
|
||||
export enum EnumStr {
|
||||
SA = 's---a',
|
||||
SB = 's---b',
|
||||
}
|
||||
|
||||
export type UnionFloat = 1.44 | 2.88 | 5.76;
|
||||
@@ -43,7 +58,9 @@ export interface Spec extends TurboModule {
|
||||
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
|
||||
+getBool: (arg: boolean) => boolean;
|
||||
+getConstants: () => ConstantsStruct;
|
||||
+getEnum: (arg: EnumInt) => EnumInt;
|
||||
+getCustomEnum: (arg: EnumInt) => EnumInt;
|
||||
+getNumEnum: (arg: EnumInt) => EnumFloat;
|
||||
+getStrEnum: (arg: EnumNone) => EnumStr;
|
||||
+getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number};
|
||||
+getNumber: (arg: number) => number;
|
||||
+getObject: (arg: ObjectStruct) => ObjectStruct;
|
||||
|
||||
Reference in New Issue
Block a user