mirror of
https://github.com/react/react-native.git
synced 2026-09-29 16:58:04 +08:00
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
This commit is contained in:
committed by
Facebook GitHub Bot
parent
2e3f55aced
commit
292a9904c4
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"presets": [
|
||||
"module:metro-react-native-babel-preset"
|
||||
],
|
||||
"plugins": [
|
||||
"babel-plugin-transform-flow-enums"
|
||||
]
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,26 @@ struct Bridging<ValueStruct> : NativeCxxModuleExampleCxxBaseValueStructBridging<
|
||||
std::string,
|
||||
ObjectStruct> {};
|
||||
|
||||
#pragma mark - enums
|
||||
enum EnumInt { A = 23, B = 42 };
|
||||
|
||||
template <>
|
||||
struct Bridging<EnumInt> {
|
||||
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<int32_t>(value));
|
||||
}
|
||||
};
|
||||
|
||||
#pragma mark - implementation
|
||||
class NativeCxxModuleExample
|
||||
: public NativeCxxModuleExampleCxxSpec<NativeCxxModuleExample> {
|
||||
@@ -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<std::string, std::optional<int32_t>> getMap(
|
||||
jsi::Runtime &rt,
|
||||
|
||||
@@ -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<ObjectStruct | null>) => Array<ObjectStruct | null>;
|
||||
+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;
|
||||
|
||||
@@ -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: () =>
|
||||
|
||||
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user