Skip to content

Commit

Permalink
Add a BinaryTreeNode example for Cxx TMs (facebook#41767)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#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
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Dec 4, 2023
1 parent 5754b4a commit ead73de
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ describe('parseObjectProperty', () => {
);
expect(() =>
parseObjectProperty(
null, // parentObject
property,
moduleName,
types,
Expand Down Expand Up @@ -356,6 +357,7 @@ describe('parseObjectProperty', () => {
);
expect(() =>
parseObjectProperty(
null, // parentObject
property,
moduleName,
types,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function translateTypeAnnotation(
property => {
return tryParse(() => {
return parseObjectProperty(
flowTypeAnnotation,
property,
hasteModuleName,
types,
Expand Down
29 changes: 29 additions & 0 deletions packages/react-native-codegen/src/parsers/parsers-commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ function isObjectProperty(property: $FlowFixMe, language: ParserType): boolean {
}

function parseObjectProperty(
parentObject?: $FlowFixMe,
property: $FlowFixMe,
hasteModuleName: string,
types: TypeDeclarationMap,
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function translateObjectTypeAnnotation(
/**
* TODO(T108222691): Use flow-types for @babel/parser
*/
typeScriptTypeAnnotation: $FlowFixMe,
nullable: boolean,
objectMembers: $ReadOnlyArray<$FlowFixMe>,
typeResolutionStatus: TypeResolutionStatus,
Expand All @@ -66,6 +67,7 @@ function translateObjectTypeAnnotation(
.map<?NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>>(property => {
return tryParse(() => {
return parseObjectProperty(
typeScriptTypeAnnotation,
property,
hasteModuleName,
types,
Expand Down Expand Up @@ -266,6 +268,7 @@ function translateTypeAnnotation(

return translateObjectTypeAnnotation(
hasteModuleName,
typeScriptTypeAnnotation,
nullable,
flattenProperties([typeAnnotation], types, parser),
typeResolutionStatus,
Expand All @@ -281,6 +284,7 @@ function translateTypeAnnotation(
case 'TSIntersectionType': {
return translateObjectTypeAnnotation(
hasteModuleName,
typeScriptTypeAnnotation,
nullable,
flattenProperties(
flattenIntersectionType(typeAnnotation, types),
Expand Down Expand Up @@ -324,6 +328,7 @@ function translateTypeAnnotation(

return translateObjectTypeAnnotation(
hasteModuleName,
typeScriptTypeAnnotation,
nullable,
typeAnnotation.members,
typeResolutionStatus,
Expand Down
Loading

0 comments on commit ead73de

Please sign in to comment.