mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Add a BinaryTreeNode example for Cxx TMs (#41767)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/41767 Changelog: [Internal] Adds a simple example showing a direct recursive node in a Cxx TM. Currently we can't auto-generate [the necessary C++ Types](https://reactnative.dev/docs/next/the-new-architecture/cxx-custom-types#struct-generator) - but we can add it later if this scenarios becomes really common. Direct recursive nodes, can't be value types - it would require infinite memory. Hence they are nullable and managed by a smart pointer. Reviewed By: rshest Differential Revision: D51784136 fbshipit-source-id: f6f0710d03583bdf1e6e72ba42d8df7f8ff8d915
This commit is contained in:
committed by
Facebook GitHub Bot
parent
5754b4a123
commit
ead73de464
+5
-3
@@ -322,6 +322,7 @@ describe('parseObjectProperty', () => {
|
||||
);
|
||||
expect(() =>
|
||||
parseObjectProperty(
|
||||
null, // parentObject
|
||||
property,
|
||||
moduleName,
|
||||
types,
|
||||
@@ -356,6 +357,7 @@ describe('parseObjectProperty', () => {
|
||||
);
|
||||
expect(() =>
|
||||
parseObjectProperty(
|
||||
null, // parentObject
|
||||
property,
|
||||
moduleName,
|
||||
types,
|
||||
@@ -1139,7 +1141,7 @@ describe('buildModuleSchema', () => {
|
||||
const contents = `
|
||||
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
|
||||
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
|
||||
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
+getBool: (arg: boolean) => boolean; }
|
||||
export interface SpecOther extends TurboModule {
|
||||
@@ -1189,11 +1191,11 @@ describe('buildModuleSchema', () => {
|
||||
const contents = `
|
||||
import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport';
|
||||
import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry';
|
||||
|
||||
|
||||
export interface MisnamedSpec extends TurboModule {
|
||||
+getArray: (a: Array<any>) => Array<string>;
|
||||
}
|
||||
|
||||
|
||||
export default (TurboModuleRegistry.getEnforcing<Spec>(
|
||||
'SampleTurboModule',
|
||||
): Spec);
|
||||
|
||||
+13
@@ -731,10 +731,23 @@ export enum StringOptions {
|
||||
Three = 'three',
|
||||
}
|
||||
|
||||
export type BinaryTreeNode = {
|
||||
left?: BinaryTreeNode,
|
||||
value: number,
|
||||
right?: BinaryTreeNode,
|
||||
};
|
||||
|
||||
export type GraphNode = {
|
||||
label: string,
|
||||
neighbors?: Array<GraphNode>,
|
||||
};
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
+getCallback: () => () => void;
|
||||
+getMixed: (arg: mixed) => mixed;
|
||||
+getEnums: (quality: Quality, resolution?: Resolution, floppy: Floppy, stringOptions: StringOptions) => string;
|
||||
+getBinaryTreeNode: (arg: BinaryTreeNode) => BinaryTreeNode;
|
||||
+getGraphNode: (arg: GraphNode) => GraphNode;
|
||||
+getMap: (arg: {[a: string]: ?number}) => {[b: string]: ?number};
|
||||
+getAnotherMap: (arg: {[string]: string}) => {[string]: string};
|
||||
+getUnion: (chooseInt: ChooseInt, chooseFloat: ChooseFloat, chooseObject: ChooseObject, chooseString: ChooseString) => ChooseObject;
|
||||
|
||||
+95
-1
@@ -51,7 +51,59 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
|
||||
'modules': {
|
||||
'NativeSampleTurboModule': {
|
||||
'type': 'NativeModule',
|
||||
'aliasMap': {},
|
||||
'aliasMap': {
|
||||
'BinaryTreeNode': {
|
||||
'type': 'ObjectTypeAnnotation',
|
||||
'properties': [
|
||||
{
|
||||
'name': 'left',
|
||||
'optional': true,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'value',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'NumberTypeAnnotation'
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'right',
|
||||
'optional': true,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
'GraphNode': {
|
||||
'type': 'ObjectTypeAnnotation',
|
||||
'properties': [
|
||||
{
|
||||
'name': 'label',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'StringTypeAnnotation'
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'neighbors',
|
||||
'optional': true,
|
||||
'typeAnnotation': {
|
||||
'type': 'ArrayTypeAnnotation',
|
||||
'elementType': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'GraphNode'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
'enumMap': {
|
||||
'Quality': {
|
||||
'name': 'Quality',
|
||||
@@ -202,6 +254,48 @@ exports[`RN Codegen Flow Parser can generate fixture CXX_ONLY_NATIVE_MODULE 1`]
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'getBinaryTreeNode',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'FunctionTypeAnnotation',
|
||||
'returnTypeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
},
|
||||
'params': [
|
||||
{
|
||||
'name': 'arg',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'getGraphNode',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'FunctionTypeAnnotation',
|
||||
'returnTypeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'GraphNode'
|
||||
},
|
||||
'params': [
|
||||
{
|
||||
'name': 'arg',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'GraphNode'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'getMap',
|
||||
'optional': false,
|
||||
|
||||
@@ -176,6 +176,7 @@ function translateTypeAnnotation(
|
||||
property => {
|
||||
return tryParse(() => {
|
||||
return parseObjectProperty(
|
||||
flowTypeAnnotation,
|
||||
property,
|
||||
hasteModuleName,
|
||||
types,
|
||||
|
||||
@@ -171,6 +171,7 @@ function isObjectProperty(property: $FlowFixMe, language: ParserType): boolean {
|
||||
}
|
||||
|
||||
function parseObjectProperty(
|
||||
parentObject?: $FlowFixMe,
|
||||
property: $FlowFixMe,
|
||||
hasteModuleName: string,
|
||||
types: TypeDeclarationMap,
|
||||
@@ -191,6 +192,34 @@ function parseObjectProperty(
|
||||
? property.typeAnnotation.typeAnnotation
|
||||
: property.value;
|
||||
|
||||
// Handle recursive types
|
||||
if (parentObject) {
|
||||
const propertyType = parser.getResolveTypeAnnotationFN()(
|
||||
languageTypeAnnotation,
|
||||
types,
|
||||
parser,
|
||||
);
|
||||
if (
|
||||
propertyType.typeResolutionStatus.successful === true &&
|
||||
propertyType.typeResolutionStatus.type === 'alias' &&
|
||||
(language === 'TypeScript'
|
||||
? parentObject.typeName &&
|
||||
parentObject.typeName.name === languageTypeAnnotation.typeName?.name
|
||||
: parentObject.id &&
|
||||
parentObject.id.name === languageTypeAnnotation.id?.name)
|
||||
) {
|
||||
return {
|
||||
name,
|
||||
optional,
|
||||
typeAnnotation: {
|
||||
type: 'TypeAliasTypeAnnotation',
|
||||
name: propertyType.typeResolutionStatus.name,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Handle non-recursive types
|
||||
const [propertyTypeAnnotation, isPropertyNullable] =
|
||||
unwrapNullable<$FlowFixMe>(
|
||||
translateTypeAnnotation(
|
||||
|
||||
+13
@@ -825,10 +825,23 @@ export type ChooseFloat = 1.44 | 2.88 | 5.76;
|
||||
export type ChooseObject = {} | {low: string};
|
||||
export type ChooseString = 'One' | 'Two' | 'Three';
|
||||
|
||||
export type BinaryTreeNode = {
|
||||
left?: BinaryTreeNode,
|
||||
value: number,
|
||||
right?: BinaryTreeNode,
|
||||
};
|
||||
|
||||
export type GraphNode = {
|
||||
label: string,
|
||||
neighbors?: Array<GraphNode>,
|
||||
};
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
readonly getCallback: () => () => void;
|
||||
readonly getMixed: (arg: unknown) => unknown;
|
||||
readonly getEnums: (quality: Quality, resolution?: Resolution, floppy: Floppy, stringOptions: StringOptions) => string;
|
||||
readonly getBinaryTreeNode: (arg: BinaryTreeNode) => BinaryTreeNode;
|
||||
readonly getGraphNode: (arg: GraphNode) => GraphNode;
|
||||
readonly getMap: (arg: {[a: string]: number | null;}) => {[b: string]: number | null;};
|
||||
readonly getAnotherMap: (arg: {[key: string]: string}) => {[key: string]: string};
|
||||
readonly getUnion: (chooseInt: ChooseInt, chooseFloat: ChooseFloat, chooseObject: ChooseObject, chooseString: ChooseString) => ChooseObject;
|
||||
|
||||
+95
-1
@@ -42,7 +42,59 @@ exports[`RN Codegen TypeScript Parser can generate fixture CXX_ONLY_NATIVE_MODUL
|
||||
'modules': {
|
||||
'NativeSampleTurboModule': {
|
||||
'type': 'NativeModule',
|
||||
'aliasMap': {},
|
||||
'aliasMap': {
|
||||
'BinaryTreeNode': {
|
||||
'type': 'ObjectTypeAnnotation',
|
||||
'properties': [
|
||||
{
|
||||
'name': 'left',
|
||||
'optional': true,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'value',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'NumberTypeAnnotation'
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'right',
|
||||
'optional': true,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
'GraphNode': {
|
||||
'type': 'ObjectTypeAnnotation',
|
||||
'properties': [
|
||||
{
|
||||
'name': 'label',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'StringTypeAnnotation'
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'neighbors',
|
||||
'optional': true,
|
||||
'typeAnnotation': {
|
||||
'type': 'ArrayTypeAnnotation',
|
||||
'elementType': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'GraphNode'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
'enumMap': {
|
||||
'Quality': {
|
||||
'name': 'Quality',
|
||||
@@ -193,6 +245,48 @@ exports[`RN Codegen TypeScript Parser can generate fixture CXX_ONLY_NATIVE_MODUL
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'getBinaryTreeNode',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'FunctionTypeAnnotation',
|
||||
'returnTypeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
},
|
||||
'params': [
|
||||
{
|
||||
'name': 'arg',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'BinaryTreeNode'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'getGraphNode',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'FunctionTypeAnnotation',
|
||||
'returnTypeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'GraphNode'
|
||||
},
|
||||
'params': [
|
||||
{
|
||||
'name': 'arg',
|
||||
'optional': false,
|
||||
'typeAnnotation': {
|
||||
'type': 'TypeAliasTypeAnnotation',
|
||||
'name': 'GraphNode'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
'name': 'getMap',
|
||||
'optional': false,
|
||||
|
||||
@@ -50,6 +50,7 @@ function translateObjectTypeAnnotation(
|
||||
/**
|
||||
* TODO(T108222691): Use flow-types for @babel/parser
|
||||
*/
|
||||
typeScriptTypeAnnotation: $FlowFixMe,
|
||||
nullable: boolean,
|
||||
objectMembers: $ReadOnlyArray<$FlowFixMe>,
|
||||
typeResolutionStatus: TypeResolutionStatus,
|
||||
@@ -66,6 +67,7 @@ function translateObjectTypeAnnotation(
|
||||
.map<?NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>>(property => {
|
||||
return tryParse(() => {
|
||||
return parseObjectProperty(
|
||||
typeScriptTypeAnnotation,
|
||||
property,
|
||||
hasteModuleName,
|
||||
types,
|
||||
@@ -266,6 +268,7 @@ function translateTypeAnnotation(
|
||||
|
||||
return translateObjectTypeAnnotation(
|
||||
hasteModuleName,
|
||||
typeScriptTypeAnnotation,
|
||||
nullable,
|
||||
flattenProperties([typeAnnotation], types, parser),
|
||||
typeResolutionStatus,
|
||||
@@ -281,6 +284,7 @@ function translateTypeAnnotation(
|
||||
case 'TSIntersectionType': {
|
||||
return translateObjectTypeAnnotation(
|
||||
hasteModuleName,
|
||||
typeScriptTypeAnnotation,
|
||||
nullable,
|
||||
flattenProperties(
|
||||
flattenIntersectionType(typeAnnotation, types),
|
||||
@@ -324,6 +328,7 @@ function translateTypeAnnotation(
|
||||
|
||||
return translateObjectTypeAnnotation(
|
||||
hasteModuleName,
|
||||
typeScriptTypeAnnotation,
|
||||
nullable,
|
||||
typeAnnotation.members,
|
||||
typeResolutionStatus,
|
||||
|
||||
@@ -65,6 +65,12 @@ std::string NativeCxxModuleExample::consumeCustomHostObject(
|
||||
return value->a_ + std::to_string(value->b_);
|
||||
}
|
||||
|
||||
BinaryTreeNode NativeCxxModuleExample::getBinaryTreeNode(
|
||||
jsi::Runtime& rt,
|
||||
BinaryTreeNode arg) {
|
||||
return arg;
|
||||
}
|
||||
|
||||
GraphNode NativeCxxModuleExample::getGraphNode(
|
||||
jsi::Runtime& rt,
|
||||
GraphNode arg) {
|
||||
|
||||
@@ -91,6 +91,50 @@ struct CustomHostObjectRef {
|
||||
using CustomHostObject = HostObjectWrapper<CustomHostObjectRef>;
|
||||
|
||||
#pragma mark - recursive objects
|
||||
struct BinaryTreeNode {
|
||||
std::unique_ptr<BinaryTreeNode> left;
|
||||
int32_t value;
|
||||
std::unique_ptr<BinaryTreeNode> right;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Bridging<BinaryTreeNode> {
|
||||
static BinaryTreeNode fromJs(
|
||||
jsi::Runtime& rt,
|
||||
const jsi::Object& value,
|
||||
const std::shared_ptr<CallInvoker>& jsInvoker) {
|
||||
BinaryTreeNode result{
|
||||
value.hasProperty(rt, "left")
|
||||
? std::make_unique<BinaryTreeNode>(bridging::fromJs<BinaryTreeNode>(
|
||||
rt, value.getProperty(rt, "left"), jsInvoker))
|
||||
: nullptr,
|
||||
bridging::fromJs<int32_t>(
|
||||
rt, value.getProperty(rt, "value"), jsInvoker),
|
||||
value.hasProperty(rt, "right")
|
||||
? std::make_unique<BinaryTreeNode>(bridging::fromJs<BinaryTreeNode>(
|
||||
rt, value.getProperty(rt, "right"), jsInvoker))
|
||||
: nullptr};
|
||||
return result;
|
||||
}
|
||||
|
||||
static jsi::Object toJs(
|
||||
jsi::Runtime& rt,
|
||||
const BinaryTreeNode& value,
|
||||
const std::shared_ptr<CallInvoker>& jsInvoker) {
|
||||
auto result = facebook::jsi::Object(rt);
|
||||
if (value.left) {
|
||||
result.setProperty(
|
||||
rt, "left", bridging::toJs(rt, *value.left, jsInvoker));
|
||||
}
|
||||
result.setProperty(rt, "value", bridging::toJs(rt, value.value, jsInvoker));
|
||||
if (value.right) {
|
||||
result.setProperty(
|
||||
rt, "right", bridging::toJs(rt, *value.right, jsInvoker));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
struct GraphNode {
|
||||
std::string label;
|
||||
std::optional<std::vector<GraphNode>> neighbors;
|
||||
@@ -156,6 +200,8 @@ class NativeCxxModuleExample
|
||||
jsi::Runtime& rt,
|
||||
std::shared_ptr<CustomHostObject> arg);
|
||||
|
||||
BinaryTreeNode getBinaryTreeNode(jsi::Runtime& rt, BinaryTreeNode arg);
|
||||
|
||||
GraphNode getGraphNode(jsi::Runtime& rt, GraphNode arg);
|
||||
|
||||
NativeCxxModuleExampleCxxEnumFloat getNumEnum(
|
||||
|
||||
@@ -56,6 +56,12 @@ export type ValueStruct = {|
|
||||
|
||||
export type CustomHostObject = {};
|
||||
|
||||
export type BinaryTreeNode = {
|
||||
left?: BinaryTreeNode,
|
||||
value: number,
|
||||
right?: BinaryTreeNode,
|
||||
};
|
||||
|
||||
export type GraphNode = {
|
||||
label: string,
|
||||
neighbors?: Array<GraphNode>,
|
||||
@@ -68,6 +74,7 @@ export interface Spec extends TurboModule {
|
||||
+getCustomEnum: (arg: EnumInt) => EnumInt;
|
||||
+getCustomHostObject: () => CustomHostObject;
|
||||
+consumeCustomHostObject: (customHostObject: CustomHostObject) => string;
|
||||
+getBinaryTreeNode: (arg: BinaryTreeNode) => BinaryTreeNode;
|
||||
+getGraphNode: (arg: GraphNode) => GraphNode;
|
||||
+getNumEnum: (arg: EnumInt) => EnumFloat;
|
||||
+getStrEnum: (arg: EnumNone) => EnumStr;
|
||||
|
||||
@@ -43,6 +43,7 @@ type Examples =
|
||||
| 'getConstants'
|
||||
| 'getCustomEnum'
|
||||
| 'getCustomHostObject'
|
||||
| 'getBinaryTreeNode'
|
||||
| 'getGraphNode'
|
||||
| 'getNumEnum'
|
||||
| 'getStrEnum'
|
||||
@@ -104,6 +105,12 @@ class NativeCxxModuleExampleExample extends React.Component<{||}, State> {
|
||||
NativeCxxModuleExample?.consumeCustomHostObject(
|
||||
NativeCxxModuleExample?.getCustomHostObject(),
|
||||
),
|
||||
getBinaryTreeNode: () =>
|
||||
NativeCxxModuleExample?.getBinaryTreeNode({
|
||||
left: {value: 1},
|
||||
value: 0,
|
||||
right: {value: 2},
|
||||
}),
|
||||
getGraphNode: () =>
|
||||
NativeCxxModuleExample?.getGraphNode({
|
||||
label: 'root',
|
||||
|
||||
Reference in New Issue
Block a user