Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Arrays in generator #37145

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/react-native-codegen/src/CodegenSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ export type EventTypeAnnotation =
| FloatTypeAnnotation
| Int32TypeAnnotation
| StringEnumTypeAnnotation
| ObjectTypeAnnotation<EventTypeAnnotation>;
| ObjectTypeAnnotation<EventTypeAnnotation>
| {
readonly type: 'ArrayTypeAnnotation';
readonly elementType: EventTypeAnnotation
};

export type PropTypeAnnotation =
| {
Expand Down Expand Up @@ -352,4 +356,3 @@ export type NativeModuleReturnOnlyTypeAnnotation =
| NativeModuleFunctionTypeAnnotation
| NativeModulePromiseTypeAnnotation
| VoidTypeAnnotation;

6 changes: 5 additions & 1 deletion packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ export type EventTypeAnnotation =
| Int32TypeAnnotation
| MixedTypeAnnotation
| StringEnumTypeAnnotation
| ObjectTypeAnnotation<EventTypeAnnotation>;
| ObjectTypeAnnotation<EventTypeAnnotation>
| $ReadOnly<{
type: 'ArrayTypeAnnotation',
elementType: EventTypeAnnotation,
}>;

export type PropTypeAnnotation =
| $ReadOnly<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ function getCppTypeForAnnotation(
}
}

function getCppArrayTypeForAnnotation(
typeElement: EventTypeAnnotation,
structParts?: string[],
): string {
switch (typeElement.type) {
case 'BooleanTypeAnnotation':
case 'StringTypeAnnotation':
case 'DoubleTypeAnnotation':
case 'FloatTypeAnnotation':
case 'Int32TypeAnnotation':
case 'MixedTypeAnnotation':
return `std::vector<${getCppTypeForAnnotation(typeElement.type)}>`;
case 'StringEnumTypeAnnotation':
case 'ObjectTypeAnnotation':
if (!structParts) {
throw new Error(
`Trying to generate the event emitter for an Array of ${typeElement.type} without informations to generate the generic type`,
);
}
return `std::vector<${generateEventStructName(structParts)}>`;
case 'ArrayTypeAnnotation':
return `std::vector<${getCppArrayTypeForAnnotation(
typeElement.elementType,
structParts,
)}>`;
default:
throw new Error(
`Can't determine array type with typeElement: ${JSON.stringify(
typeElement,
null,
2,
)}`,
);
}
}

function getImports(
properties:
| $ReadOnlyArray<NamedShape<PropTypeAnnotation>>
Expand Down Expand Up @@ -222,6 +258,7 @@ function convertDefaultTypeToString(

module.exports = {
convertDefaultTypeToString,
getCppArrayTypeForAnnotation,
getCppTypeForAnnotation,
getEnumMaskName,
getImports,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,12 @@ function generateSetter(
variableName: string,
propertyName: string,
propertyParts: $ReadOnlyArray<string>,
usingEvent: boolean,
valueMapper: string => string = value => value,
) {
const eventChain = `$event.${[...propertyParts, propertyName].join('.')}`;
const eventChain = usingEvent
? `$event.${[...propertyParts, propertyName].join('.')}`
: [propertyParts, propertyName].join('.');
return `${variableName}.setProperty(runtime, "${propertyName}", ${valueMapper(
eventChain,
)});`;
Expand All @@ -116,6 +119,7 @@ function generateObjectSetter(
propertyParts: $ReadOnlyArray<string>,
typeAnnotation: ObjectTypeAnnotation<EventTypeAnnotation>,
extraIncludes: Set<string>,
usingEvent: boolean,
) {
return `
{
Expand All @@ -126,6 +130,7 @@ function generateObjectSetter(
typeAnnotation.properties,
propertyParts.concat([propertyName]),
extraIncludes,
usingEvent,
),
2,
)}
Expand All @@ -134,11 +139,165 @@ function generateObjectSetter(
`.trim();
}

function setValueAtIndex(
propertyName: string,
indexVariable: string,
loopLocalVariable: string,
mappingFunction: string => string = value => value,
) {
return `${propertyName}.setValueAtIndex(runtime, ${indexVariable}++, ${mappingFunction(
loopLocalVariable,
)});`;
}

function generateArraySetter(
variableName: string,
propertyName: string,
propertyParts: $ReadOnlyArray<string>,
elementType: EventTypeAnnotation,
extraIncludes: Set<string>,
usingEvent: boolean,
): string {
const eventChain = usingEvent
? `$event.${[...propertyParts, propertyName].join('.')}`
: [propertyParts, propertyName].join('.');
const indexVar = `${propertyName}Index`;
const innerLoopVar = `${propertyName}Value`;
return `
auto ${propertyName} = jsi::Array(runtime, ${eventChain}.size());
size_t ${indexVar} = 0;
for (auto ${innerLoopVar} : ${eventChain}) {
${handleArrayElementType(
elementType,
propertyName,
indexVar,
innerLoopVar,
propertyParts,
extraIncludes,
usingEvent,
)}
}
${variableName}.setProperty(runtime, "${propertyName}", ${propertyName});
`;
}

function handleArrayElementType(
elementType: EventTypeAnnotation,
propertyName: string,
indexVariable: string,
loopLocalVariable: string,
propertyParts: $ReadOnlyArray<string>,
extraIncludes: Set<string>,
usingEvent: boolean,
): string {
switch (elementType.type) {
case 'BooleanTypeAnnotation':
return setValueAtIndex(
propertyName,
indexVariable,
loopLocalVariable,
val => `(bool)${val}`,
);
case 'StringTypeAnnotation':
case 'Int32TypeAnnotation':
case 'DoubleTypeAnnotation':
case 'FloatTypeAnnotation':
return setValueAtIndex(propertyName, indexVariable, loopLocalVariable);
case 'MixedTypeAnnotation':
return setValueAtIndex(
propertyName,
indexVariable,
loopLocalVariable,
val => `jsi::valueFromDynamic(runtime, ${val})`,
);
case 'StringEnumTypeAnnotation':
return setValueAtIndex(
propertyName,
indexVariable,
loopLocalVariable,
val => `toString(${val})`,
);
case 'ObjectTypeAnnotation':
return convertObjectTypeArray(
propertyName,
indexVariable,
loopLocalVariable,
propertyParts,
elementType,
extraIncludes,
);
case 'ArrayTypeAnnotation':
return convertArrayTypeArray(
propertyName,
indexVariable,
loopLocalVariable,
propertyParts,
elementType,
extraIncludes,
usingEvent,
);
default:
throw new Error(
`Received invalid elementType for array ${elementType.type}`,
);
}
}

function convertObjectTypeArray(
propertyName: string,
indexVariable: string,
loopLocalVariable: string,
propertyParts: $ReadOnlyArray<string>,
objectTypeAnnotation: ObjectTypeAnnotation<EventTypeAnnotation>,
extraIncludes: Set<string>,
): string {
return `auto ${propertyName}Object = jsi::Object(runtime);
${generateSetters(
`${propertyName}Object`,
objectTypeAnnotation.properties,
[].concat([loopLocalVariable]),
extraIncludes,
false,
)}
${setValueAtIndex(propertyName, indexVariable, `${propertyName}Object`)}`;
}

function convertArrayTypeArray(
propertyName: string,
indexVariable: string,
loopLocalVariable: string,
propertyParts: $ReadOnlyArray<string>,
eventTypeAnnotation: EventTypeAnnotation,
extraIncludes: Set<string>,
usingEvent: boolean,
): string {
if (eventTypeAnnotation.type !== 'ArrayTypeAnnotation') {
throw new Error(
`Inconsistent eventTypeAnnotation received. Expected type = 'ArrayTypeAnnotation'; received = ${eventTypeAnnotation.type}`,
);
}
return `auto ${propertyName}Array = jsi::Array(runtime, ${loopLocalVariable}.size());
size_t ${indexVariable}Internal = 0;
for (auto ${loopLocalVariable}Internal : ${loopLocalVariable}) {
${handleArrayElementType(
eventTypeAnnotation.elementType,
`${propertyName}Array`,
`${indexVariable}Internal`,
`${loopLocalVariable}Internal`,
propertyParts,
extraIncludes,
usingEvent,
)}
}
${setValueAtIndex(propertyName, indexVariable, `${propertyName}Array`)}`;
}

function generateSetters(
parentPropertyName: string,
properties: $ReadOnlyArray<NamedShape<EventTypeAnnotation>>,
propertyParts: $ReadOnlyArray<string>,
extraIncludes: Set<string>,
usingEvent: boolean = true,
): string {
const propSetters = properties
.map(eventProperty => {
Expand All @@ -153,20 +312,23 @@ function generateSetters(
parentPropertyName,
eventProperty.name,
propertyParts,
usingEvent,
);
case 'MixedTypeAnnotation':
extraIncludes.add('#include <jsi/JSIDynamic.h>');
return generateSetter(
parentPropertyName,
eventProperty.name,
propertyParts,
usingEvent,
prop => `jsi::valueFromDynamic(runtime, ${prop})`,
);
case 'StringEnumTypeAnnotation':
return generateSetter(
parentPropertyName,
eventProperty.name,
propertyParts,
usingEvent,
prop => `toString(${prop})`,
);
case 'ObjectTypeAnnotation':
Expand All @@ -176,6 +338,16 @@ function generateSetters(
propertyParts,
typeAnnotation,
extraIncludes,
usingEvent,
);
case 'ArrayTypeAnnotation':
return generateArraySetter(
parentPropertyName,
eventProperty.name,
propertyParts,
typeAnnotation.elementType,
extraIncludes,
usingEvent,
);
default:
(typeAnnotation.type: empty);
Expand Down
Loading