From 292a9904c45c2e6f936169321bc658c61f8d033d Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Wed, 8 Feb 2023 20:24:28 -0800 Subject: [PATCH] react-native code-gen > C++ TurboModules enum example (#36083) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36083 Changelog: [Internal] Reviewed By: javache Differential Revision: D43036612 fbshipit-source-id: fc70650bc4ba48d11f489556d1290ae9e7e58016 --- .babelrc | 8 +++++++ package.json | 1 + packages/rn-tester/BUCK | 1 + .../NativeCxxModuleExample.cpp | 2 +- .../NativeCxxModuleExample.h | 22 ++++++++++++++++++- .../NativeCxxModuleExample.js | 5 +---- .../NativeCxxModuleExampleExample.js | 6 +++-- packages/rn-tester/package.json | 4 +++- yarn.lock | 14 +++++++++++- 9 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 00000000000000..f625e70226abf0 --- /dev/null +++ b/.babelrc @@ -0,0 +1,8 @@ +{ + "presets": [ + "module:metro-react-native-babel-preset" + ], + "plugins": [ + "babel-plugin-transform-flow-enums" + ] +} diff --git a/package.json b/package.json index e17aa48a77c965..791480ef357d93 100644 --- a/package.json +++ b/package.json @@ -124,6 +124,7 @@ "base64-js": "^1.1.2", "deprecated-react-native-prop-types": "^4.0.0", "event-target-shim": "^5.0.1", + "flow-enums-runtime": "^0.0.5", "invariant": "^2.2.4", "jest-environment-node": "^29.2.1", "jsc-android": "^250231.0.0", diff --git a/packages/rn-tester/BUCK b/packages/rn-tester/BUCK index 0a8b6b5259eb40..3e87501c010695 100644 --- a/packages/rn-tester/BUCK +++ b/packages/rn-tester/BUCK @@ -75,6 +75,7 @@ rn_library( skip_processors = True, visibility = ["PUBLIC"], deps = [ + "//xplat/js:node_modules__flow_19enums_19runtime", "//xplat/js:node_modules__nullthrows", "//xplat/js/RKJSModules/Libraries/Core:Core", "//xplat/js/RKJSModules/vendor/react:react", diff --git a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp index 78c9c1e3a27c17..200112714168ac 100644 --- a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp +++ b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.cpp @@ -33,7 +33,7 @@ ConstantsStruct NativeCxxModuleExample::getConstants(jsi::Runtime &rt) { return ConstantsStruct{true, 69, "react-native"}; } -int32_t NativeCxxModuleExample::getEnum(jsi::Runtime &rt, int32_t arg) { +EnumInt NativeCxxModuleExample::getEnum(jsi::Runtime &rt, EnumInt arg) { return arg; } diff --git a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h index e968db5848ee2e..bd36f16e7482a0 100644 --- a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h +++ b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.h @@ -53,6 +53,26 @@ struct Bridging : NativeCxxModuleExampleCxxBaseValueStructBridging< std::string, ObjectStruct> {}; +#pragma mark - enums +enum EnumInt { A = 23, B = 42 }; + +template <> +struct Bridging { + static EnumInt fromJs(jsi::Runtime &rt, int32_t value) { + if (value == 23) { + return EnumInt::A; + } else if (value == 42) { + return EnumInt::B; + } else { + throw jsi::JSError(rt, "Invalid enum value"); + } + } + + static jsi::Value toJs(jsi::Runtime &rt, EnumInt value) { + return bridging::toJs(rt, static_cast(value)); + } +}; + #pragma mark - implementation class NativeCxxModuleExample : public NativeCxxModuleExampleCxxSpec { @@ -71,7 +91,7 @@ class NativeCxxModuleExample ConstantsStruct getConstants(jsi::Runtime &rt); - int32_t getEnum(jsi::Runtime &rt, int32_t arg); + EnumInt getEnum(jsi::Runtime &rt, EnumInt arg); std::map> getMap( jsi::Runtime &rt, diff --git a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js index d48614a2ca9171..d4e320077f73c0 100644 --- a/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js +++ b/packages/rn-tester/NativeCxxModuleExample/NativeCxxModuleExample.js @@ -12,12 +12,10 @@ import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; import {TurboModuleRegistry} from 'react-native'; -/** FIXME: Enable flow-enum support export enum EnumInt { A = 23, B = 42, } -*/ export type UnionFloat = 1.44 | 2.88 | 5.76; export type UnionString = 'One' | 'Two' | 'Three'; @@ -45,8 +43,7 @@ export interface Spec extends TurboModule { +getArray: (arg: Array) => Array; +getBool: (arg: boolean) => boolean; +getConstants: () => ConstantsStruct; - // FIXME: Enable flow-enum support - +getEnum: (arg: number /*EnumInt*/) => number /*EnumInt*/; + +getEnum: (arg: EnumInt) => EnumInt; +getMap: (arg: {[key: string]: ?number}) => {[key: string]: ?number}; +getNumber: (arg: number) => number; +getObject: (arg: ObjectStruct) => ObjectStruct; diff --git a/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js b/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js index 2dc07e40852630..9c2946883c5702 100644 --- a/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js +++ b/packages/rn-tester/js/examples/TurboModule/NativeCxxModuleExampleExample.js @@ -20,7 +20,9 @@ import { RootTagContext, } from 'react-native'; import * as React from 'react'; -import NativeCxxModuleExample /*EnumInt,*/ from '../../../NativeCxxModuleExample/NativeCxxModuleExample'; +import NativeCxxModuleExample, { + EnumInt, +} from '../../../NativeCxxModuleExample/NativeCxxModuleExample'; type State = {| testResults: { @@ -72,7 +74,7 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> { ]), getBool: () => NativeCxxModuleExample?.getBool(true), getConstants: () => NativeCxxModuleExample?.getConstants(), - getEnum: () => NativeCxxModuleExample?.getEnum(/*EnumInt.A*/ 2), + getEnum: () => NativeCxxModuleExample?.getEnum(EnumInt.A), getMap: () => NativeCxxModuleExample?.getMap({a: 1, b: null, c: 3}), getNumber: () => NativeCxxModuleExample?.getNumber(99.95), getObject: () => diff --git a/packages/rn-tester/package.json b/packages/rn-tester/package.json index 6f4b740cd47400..c973e8a7f134ea 100644 --- a/packages/rn-tester/package.json +++ b/packages/rn-tester/package.json @@ -21,13 +21,15 @@ }, "dependencies": { "invariant": "^2.2.4", - "nullthrows": "^1.1.1" + "nullthrows": "^1.1.1", + "flow-enums-runtime": "^0.0.5" }, "peerDependencies": { "react": "18.2.0", "react-native": "*" }, "devDependencies": { + "babel-plugin-transform-flow-enums":"^0.0.2", "connect": "^3.6.5", "ws": "^6.2.2" }, diff --git a/yarn.lock b/yarn.lock index 742db74a6ba207..ffbee903c0c052 100644 --- a/yarn.lock +++ b/yarn.lock @@ -494,7 +494,7 @@ dependencies: "@babel/helper-plugin-utils" "^7.8.3" -"@babel/plugin-syntax-flow@^7.0.0", "@babel/plugin-syntax-flow@^7.18.6", "@babel/plugin-syntax-flow@^7.2.0": +"@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": version "7.18.6" resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-flow/-/plugin-syntax-flow-7.18.6.tgz#774d825256f2379d06139be0c723c4dd444f3ca1" integrity sha512-LUbR+KNTBWCUAqRG9ex5Gnzu2IOkt8jRJbHHXFT9q+L9zm7M/QQbEqXyw1n1pohYvOyWC8CjeyjrSaIwiYjK7A== @@ -2951,6 +2951,13 @@ babel-plugin-syntax-trailing-function-commas@^7.0.0-beta.0: resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-7.0.0-beta.0.tgz#aa213c1435e2bffeb6fca842287ef534ad05d5cf" integrity sha512-Xj9XuRuz3nTSbaTXWv3itLOcxyF4oPD8douBBmj7U9BBC6nEBYfyOJYQMf/8PJAFotC62UY5dFfIGEPr7WswzQ== +babel-plugin-transform-flow-enums@^0.0.2: + version "0.0.2" + resolved "https://registry.yarnpkg.com/babel-plugin-transform-flow-enums/-/babel-plugin-transform-flow-enums-0.0.2.tgz#d1d0cc9bdc799c850ca110d0ddc9f21b9ec3ef25" + integrity sha512-g4aaCrDDOsWjbm0PUUeVnkcVd6AKJsVc/MbnPhEotEpkeJQP6b8nzewohQi7+QS8UyPehOhGWn0nOwjvWpmMvQ== + dependencies: + "@babel/plugin-syntax-flow" "^7.12.1" + babel-preset-current-node-syntax@^1.0.0: version "1.0.1" 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: resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.199.1.tgz#678eac2303fa898227f4d103264b6ce49f4430c1" integrity sha512-Ic0Mp9iZ2exbH0mNj/XhzUWPZa9JylHb6uQARZnnYCTRwumOpjNOP0qwyRTltWrbCpfHjnWngNO9VLaVKHz2aQ== +flow-enums-runtime@^0.0.5: + version "0.0.5" + resolved "https://registry.yarnpkg.com/flow-enums-runtime/-/flow-enums-runtime-0.0.5.tgz#95884bfcc82edaf27eef7e1dd09732331cfbafbc" + integrity sha512-PSZF9ZuaZD03sT9YaIs0FrGJ7lSUw7rHZIex+73UYVXg46eL/wxN5PaVcPJFudE2cJu5f0fezitV5aBkLHPUOQ== + flow-parser@0.*, flow-parser@^0.185.0: version "0.185.0" resolved "https://registry.yarnpkg.com/flow-parser/-/flow-parser-0.185.0.tgz#56bde60805bad19b2934ebfc50c9485e5c5424f9"