Skip to content

Commit 292a990

Browse files
react-native code-gen > C++ TurboModules enum example (facebook#36083)
Summary: Pull Request resolved: facebook#36083 Changelog: [Internal] Reviewed By: javache Differential Revision: D43036612 fbshipit-source-id: fc70650bc4ba48d11f489556d1290ae9e7e58016
1 parent 2e3f55a commit 292a990

File tree

9 files changed

+53
-10
lines changed

9 files changed

+53
-10
lines changed

.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
"module:metro-react-native-babel-preset"
4+
],
5+
"plugins": [
6+
"babel-plugin-transform-flow-enums"
7+
]
8+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"base64-js": "^1.1.2",
125125
"deprecated-react-native-prop-types": "^4.0.0",
126126
"event-target-shim": "^5.0.1",
127+
"flow-enums-runtime": "^0.0.5",
127128
"invariant": "^2.2.4",
128129
"jest-environment-node": "^29.2.1",
129130
"jsc-android": "^250231.0.0",

packages/rn-tester/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ rn_library(
7575
skip_processors = True,
7676
visibility = ["PUBLIC"],
7777
deps = [
78+
"//xplat/js:node_modules__flow_19enums_19runtime",
7879
"//xplat/js:node_modules__nullthrows",
7980
"//xplat/js/RKJSModules/Libraries/Core:Core",
8081
"//xplat/js/RKJSModules/vendor/react:react",

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ConstantsStruct NativeCxxModuleExample::getConstants(jsi::Runtime &rt) {
3333
return ConstantsStruct{true, 69, "react-native"};
3434
}
3535

36-
int32_t NativeCxxModuleExample::getEnum(jsi::Runtime &rt, int32_t arg) {
36+
EnumInt NativeCxxModuleExample::getEnum(jsi::Runtime &rt, EnumInt arg) {
3737
return arg;
3838
}
3939

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h

+21-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,26 @@ struct Bridging<ValueStruct> : NativeCxxModuleExampleCxxBaseValueStructBridging<
5353
std::string,
5454
ObjectStruct> {};
5555

56+
#pragma mark - enums
57+
enum EnumInt { A = 23, B = 42 };
58+
59+
template <>
60+
struct Bridging<EnumInt> {
61+
static EnumInt fromJs(jsi::Runtime &rt, int32_t value) {
62+
if (value == 23) {
63+
return EnumInt::A;
64+
} else if (value == 42) {
65+
return EnumInt::B;
66+
} else {
67+
throw jsi::JSError(rt, "Invalid enum value");
68+
}
69+
}
70+
71+
static jsi::Value toJs(jsi::Runtime &rt, EnumInt value) {
72+
return bridging::toJs(rt, static_cast<int32_t>(value));
73+
}
74+
};
75+
5676
#pragma mark - implementation
5777
class NativeCxxModuleExample
5878
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
@@ -71,7 +91,7 @@ class NativeCxxModuleExample
7191

7292
ConstantsStruct getConstants(jsi::Runtime &rt);
7393

74-
int32_t getEnum(jsi::Runtime &rt, int32_t arg);
94+
EnumInt getEnum(jsi::Runtime &rt, EnumInt arg);
7595

7696
std::map<std::string, std::optional<int32_t>> getMap(
7797
jsi::Runtime &rt,

packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
1212

1313
import {TurboModuleRegistry} from 'react-native';
1414

15-
/** FIXME: Enable flow-enum support
1615
export enum EnumInt {
1716
A = 23,
1817
B = 42,
1918
}
20-
*/
2119

2220
export type UnionFloat = 1.44 | 2.88 | 5.76;
2321
export type UnionString = 'One' | 'Two' | 'Three';
@@ -45,8 +43,7 @@ export interface Spec extends TurboModule {
4543
+getArray: (arg: Array<ObjectStruct | null>) => Array<ObjectStruct | null>;
4644
+getBool: (arg: boolean) => boolean;
4745
+getConstants: () => ConstantsStruct;
48-
// FIXME: Enable flow-enum support
49-
+getEnum: (arg: number /*EnumInt*/) => number /*EnumInt*/;
46+
+getEnum: (arg: EnumInt) => EnumInt;
5047
+getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number};
5148
+getNumber: (arg: number) => number;
5249
+getObject: (arg: ObjectStruct) => ObjectStruct;

packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import {
2020
RootTagContext,
2121
} from 'react-native';
2222
import * as React from 'react';
23-
import NativeCxxModuleExample /*EnumInt,*/ from '../../../NativeCxxModuleExample/NativeCxxModuleExample';
23+
import NativeCxxModuleExample, {
24+
EnumInt,
25+
} from '../../../NativeCxxModuleExample/NativeCxxModuleExample';
2426

2527
type State = {|
2628
testResults: {
@@ -72,7 +74,7 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
7274
]),
7375
getBool: () => NativeCxxModuleExample?.getBool(true),
7476
getConstants: () => NativeCxxModuleExample?.getConstants(),
75-
getEnum: () => NativeCxxModuleExample?.getEnum(/*EnumInt.A*/ 2),
77+
getEnum: () => NativeCxxModuleExample?.getEnum(EnumInt.A),
7678
getMap: () => NativeCxxModuleExample?.getMap({a: 1, b: null, c: 3}),
7779
getNumber: () => NativeCxxModuleExample?.getNumber(99.95),
7880
getObject: () =>

packages/rn-tester/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
},
2222
"dependencies": {
2323
"invariant": "^2.2.4",
24-
"nullthrows": "^1.1.1"
24+
"nullthrows": "^1.1.1",
25+
"flow-enums-runtime": "^0.0.5"
2526
},
2627
"peerDependencies": {
2728
"react": "18.2.0",
2829
"react-native": "*"
2930
},
3031
"devDependencies": {
32+
"babel-plugin-transform-flow-enums":"^0.0.2",
3133
"connect": "^3.6.5",
3234
"ws": "^6.2.2"
3335
},

yarn.lock

+13-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@
494494
dependencies:
495495
"@babel/helper-plugin-utils" "^7.8.3"
496496

497-
"@babel/plugin-syntax-flow@^7.0.0", "@babel/plugin-syntax-flow@^7.18.6", "@babel/plugin-syntax-flow@^7.2.0":
497+
"@babel/plugin-syntax-flow@^7.0.0", "@babel/plugin-syntax-flow@^7.12.1", "@babel/plugin-syntax-flow@^7.18.6", "@babel/plugin-syntax-flow@^7.2.0":
498498
version "7.18.6"
499499
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-flow/-/plugin-syntax-flow-7.18.6.tgz#774d825256f2379d06139be0c723c4dd444f3ca1"
500500
integrity sha512-LUbR+KNTBWCUAqRG9ex5Gnzu2IOkt8jRJbHHXFT9q+L9zm7M/QQbEqXyw1n1pohYvOyWC8CjeyjrSaIwiYjK7A==
@@ -2951,6 +2951,13 @@ babel-plugin-syntax-trailing-function-commas@^7.0.0-beta.0:
29512951
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-7.0.0-beta.0.tgz#aa213c1435e2bffeb6fca842287ef534ad05d5cf"
29522952
integrity sha512-Xj9XuRuz3nTSbaTXWv3itLOcxyF4oPD8douBBmj7U9BBC6nEBYfyOJYQMf/8PJAFotC62UY5dFfIGEPr7WswzQ==
29532953

2954+
babel-plugin-transform-flow-enums@^0.0.2:
2955+
version "0.0.2"
2956+
resolved "https://registry.yarnpkg.com/babel-plugin-transform-flow-enums/-/babel-plugin-transform-flow-enums-0.0.2.tgz#d1d0cc9bdc799c850ca110d0ddc9f21b9ec3ef25"
2957+
integrity sha512-g4aaCrDDOsWjbm0PUUeVnkcVd6AKJsVc/MbnPhEotEpkeJQP6b8nzewohQi7+QS8UyPehOhGWn0nOwjvWpmMvQ==
2958+
dependencies:
2959+
"@babel/plugin-syntax-flow" "^7.12.1"
2960+
29542961
babel-preset-current-node-syntax@^1.0.0:
29552962
version "1.0.1"
29562963
resolved "https://registry.yarnpkg.com/babel-preset-current-node-syntax/-/babel-preset-current-node-syntax-1.0.1.tgz#b4399239b89b2a011f9ddbe3e4f401fc40cff73b"
@@ -4404,6 +4411,11 @@ flow-bin@^0.199.1:
44044411
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.199.1.tgz#678eac2303fa898227f4d103264b6ce49f4430c1"
44054412
integrity sha512-Ic0Mp9iZ2exbH0mNj/XhzUWPZa9JylHb6uQARZnnYCTRwumOpjNOP0qwyRTltWrbCpfHjnWngNO9VLaVKHz2aQ==
44064413

4414+
flow-enums-runtime@^0.0.5:
4415+
version "0.0.5"
4416+
resolved "https://registry.yarnpkg.com/flow-enums-runtime/-/flow-enums-runtime-0.0.5.tgz#95884bfcc82edaf27eef7e1dd09732331cfbafbc"
4417+
integrity sha512-PSZF9ZuaZD03sT9YaIs0FrGJ7lSUw7rHZIex+73UYVXg46eL/wxN5PaVcPJFudE2cJu5f0fezitV5aBkLHPUOQ==
4418+
44074419
flow-parser@0.*, flow-parser@^0.185.0:
44084420
version "0.185.0"
44094421
resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.185.0.tgz#56bde60805bad19b2934ebfc50c9485e5c5424f9"

0 commit comments

Comments
 (0)