Skip to content

Commit

Permalink
Add enum example to Android/iOS rn-tester TurboModule (#35133)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #35133

Add enum example to Android/iOS rn-tester TurboModule

Changelog:
[General][Added] - Add enum example to Android/iOS rn-tester TurboModule

Differential Revision: D40711269

fbshipit-source-id: 43c2cf3ffdd0cfa3a61ecbb637c4e241c38884b6
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Nov 3, 2022
1 parent f32a3a5 commit bc2ac20
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ flow/

[options]
emoji=true
enums=true

exact_by_default=true

Expand Down
1 change: 1 addition & 0 deletions .flowconfig.android
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ flow/

[options]
emoji=true
enums=true

exact_by_default=true

Expand Down
1 change: 1 addition & 0 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ rn_library(
"//xplat/js:node_modules__base64_19js",
"//xplat/js:node_modules__deprecated_19react_19native_19prop_19types",
"//xplat/js:node_modules__event_19target_19shim",
"//xplat/js:node_modules__flow_19enums_19runtime",
"//xplat/js:node_modules__invariant",
"//xplat/js:node_modules__memoize_19one",
"//xplat/js:node_modules__nullthrows",
Expand Down
6 changes: 6 additions & 0 deletions Libraries/TurboModule/samples/NativeSampleTurboModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import type {RootTag, TurboModule} from '../RCTExport';

import * as TurboModuleRegistry from '../TurboModuleRegistry';

export enum EnumInt {
A = 23,
B = 42,
}

export interface Spec extends TurboModule {
// Exported methods.
+getConstants: () => {|
Expand All @@ -22,6 +27,7 @@ export interface Spec extends TurboModule {
|};
+voidFunc: () => void;
+getBool: (arg: boolean) => boolean;
+getEnum?: (arg: EnumInt) => EnumInt;
+getNumber: (arg: number) => number;
+getString: (arg: string) => string;
+getArray: (arg: Array<any>) => Array<any>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool(
->getBool(rt, args[0].getBool()));
}

static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getEnum(
jsi::Runtime &rt,
TurboModule &turboModule,
const jsi::Value *args,
size_t count) {
return jsi::Value(
static_cast<NativeSampleTurboCxxModuleSpecJSI *>(&turboModule)
->getEnum(rt, args[0].getNumber()));
}

static jsi::Value __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber(
jsi::Runtime &rt,
TurboModule &turboModule,
Expand Down Expand Up @@ -119,6 +129,8 @@ NativeSampleTurboCxxModuleSpecJSI::NativeSampleTurboCxxModuleSpecJSI(
0, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_voidFunc};
methodMap_["getBool"] = MethodMetadata{
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getBool};
methodMap_["getEnum"] = MethodMetadata{
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getEnum};
methodMap_["getNumber"] = MethodMetadata{
1, __hostFunction_NativeSampleTurboCxxModuleSpecJSI_getNumber};
methodMap_["getString"] = MethodMetadata{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class JSI_EXPORT NativeSampleTurboCxxModuleSpecJSI : public TurboModule {
public:
virtual void voidFunc(jsi::Runtime &rt) = 0;
virtual bool getBool(jsi::Runtime &rt, bool arg) = 0;
virtual double getEnum(jsi::Runtime &rt, double arg) = 0;
virtual double getNumber(jsi::Runtime &rt, double arg) = 0;
virtual jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) = 0;
virtual jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ bool SampleTurboCxxModule::getBool(jsi::Runtime &rt, bool arg) {
return arg;
}

double SampleTurboCxxModule::getEnum(jsi::Runtime &rt, double arg) {
return arg;
}

double SampleTurboCxxModule::getNumber(jsi::Runtime &rt, double arg) {
return arg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SampleTurboCxxModule : public NativeSampleTurboCxxModuleSpecJSI {

void voidFunc(jsi::Runtime &rt) override;
bool getBool(jsi::Runtime &rt, bool arg) override;
double getEnum(jsi::Runtime &rt, double arg) override;
double getNumber(jsi::Runtime &rt, double arg) override;
jsi::String getString(jsi::Runtime &rt, const jsi::String &arg) override;
jsi::Array getArray(jsi::Runtime &rt, const jsi::Array &arg) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public NativeSampleTurboModuleSpec(ReactApplicationContext reactContext) {
@ReactMethod(isBlockingSynchronousMethod = true)
public abstract boolean getBool(boolean arg);

@ReactMethod(isBlockingSynchronousMethod = true)
public abstract double getEnum(double arg);

protected abstract Map<String, Object> getTypedExportedConstants();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ __hostFunction_NativeSampleTurboModuleSpecJSI_getBool(
rt, BooleanKind, "getBool", "(Z)Z", args, count, cachedMethodId);
}

static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getEnum(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count) {
static jmethodID cachedMethodId = nullptr;
return static_cast<JavaTurboModule &>(turboModule)
.invokeJavaMethod(
rt, NumberKind, "getEnum", "(D)D", args, count, cachedMethodId);
}

static facebook::jsi::Value
__hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(
facebook::jsi::Runtime &rt,
Expand Down Expand Up @@ -195,6 +207,9 @@ NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(
methodMap_["getBool"] =
MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getBool};

methodMap_["getEnum"] =
MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum};

methodMap_["getNumber"] = MethodMetadata{
1, __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public boolean getBool(boolean arg) {
return arg;
}

@DoNotStrip
@SuppressWarnings("unused")
@Override
public double getEnum(double arg) {
log("getEnum", arg, arg);
return arg;
}

@Override
protected Map<String, Object> getTypedExportedConstants() {
Map<String, Object> result = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

- (void)voidFunc;
- (NSNumber *)getBool:(BOOL)arg;
- (NSNumber *)getEnum:(double)arg;
- (NSNumber *)getNumber:(double)arg;
- (NSString *)getString:(NSString *)arg;
- (NSArray<id<NSObject>> *)getArray:(NSArray *)arg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@
.invokeObjCMethod(rt, BooleanKind, "getBool", @selector(getBool:), args, count);
}

static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
const facebook::jsi::Value *args,
size_t count)
{
return static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, NumberKind, "getEnum", @selector(getEnum:), args, count);
}

static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber(
facebook::jsi::Runtime &rt,
TurboModule &turboModule,
Expand Down Expand Up @@ -136,6 +146,7 @@
{
methodMap_["voidFunc"] = MethodMetadata{0, __hostFunction_NativeSampleTurboModuleSpecJSI_voidFunc};
methodMap_["getBool"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getBool};
methodMap_["getEnum"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getEnum};
methodMap_["getNumber"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getNumber};
methodMap_["getString"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getString};
methodMap_["getArray"] = MethodMetadata{1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArray};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ - (NSDictionary *)constantsToExport
return @(arg);
}

RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getEnum : (double)arg)
{
return @(arg);
}

RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSNumber *, getNumber : (double)arg)
{
return @(arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ std::vector<CxxModule::Method> SampleTurboCxxModuleLegacyImpl::getMethods() {
return getBool(xplat::jsArgAsBool(args, 0));
},
CxxModule::SyncTag),
CxxModule::Method(
"getEnum",
[this](folly::dynamic args) {
return getEnum(xplat::jsArgAsDouble(args, 0));
},
CxxModule::SyncTag),
CxxModule::Method(
"getNumber",
[this](folly::dynamic args) {
Expand Down Expand Up @@ -114,6 +120,10 @@ bool SampleTurboCxxModuleLegacyImpl::getBool(bool arg) {
return arg;
}

double SampleTurboCxxModuleLegacyImpl::getEnum(double arg) {
return arg;
}

double SampleTurboCxxModuleLegacyImpl::getNumber(double arg) {
return arg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class SampleTurboCxxModuleLegacyImpl
// API
void voidFunc();
bool getBool(bool arg);
double getEnum(double arg);
double getNumber(double arg);
std::string getString(const std::string &arg);
folly::dynamic getArray(const folly::dynamic &arg);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"base64-js": "^1.1.2",
"deprecated-react-native-prop-types": "^2.3.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": "^250230.2.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/rn-tester/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["babel-plugin-transform-flow-enums"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import NativeSampleTurboModule from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
import {EnumInt} from 'react-native/Libraries/TurboModule/samples/NativeSampleTurboModule';
import type {RootTag} from 'react-native/Libraries/ReactNative/RootTag';
import {
StyleSheet,
Expand Down Expand Up @@ -57,6 +58,10 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
getConstants: () => NativeSampleTurboModule.getConstants(),
voidFunc: () => NativeSampleTurboModule.voidFunc(),
getBool: () => NativeSampleTurboModule.getBool(true),
getEnum: () =>
NativeSampleTurboModule.getEnum
? NativeSampleTurboModule.getEnum(EnumInt.A)
: null,
getNumber: () => NativeSampleTurboModule.getNumber(99.95),
getString: () => NativeSampleTurboModule.getString('Hello'),
getArray: () =>
Expand All @@ -80,6 +85,7 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
| 'callback'
| 'getArray'
| 'getBool'
| 'getEnum'
| 'getConstants'
| 'getNumber'
| 'getObject'
Expand Down Expand Up @@ -120,6 +126,7 @@ class SampleTurboModuleExample extends React.Component<{||}, State> {
| 'callback'
| 'getArray'
| 'getBool'
| 'getEnum'
| 'getConstants'
| 'getNumber'
| 'getObject'
Expand Down
3 changes: 3 additions & 0 deletions packages/rn-tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"invariant": "^2.2.4",
"flow-enums-runtime": "^0.0.5",
"nullthrows": "^1.1.1"
},
"peerDependencies": {
Expand All @@ -29,6 +30,8 @@
},
"devDependencies": {
"connect": "^3.6.5",
"babel-plugin-transform-flow-enums":"^0.0.2",
"jscodeshift": "^0.13.1",
"ws": "^6.2.2"
},
"codegenConfig": {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4528,6 +4528,11 @@ flow-bin@^0.191.0:
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.191.0.tgz#9324c9498584b60575fd8d77a2897ee4f384443b"
integrity sha512-IhaDGoOtRDdGUJLjm7KQlHK8BAzOVJmpx+CIR6bCju4pF7zon2v7WNrds5706WZqDE3rD2c8cM4GdhDnIFYXtg==

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"
Expand Down

0 comments on commit bc2ac20

Please sign in to comment.