Skip to content

Commit

Permalink
Backout Generate Custom Native State from Codegen
Browse files Browse the repository at this point in the history
Summary:
This is the second diffs that backs out the Custom Native State from the Codegen. The reason why we are backing it out are:

1. It forces users to create new types in JS that are not ctually used there. For example, the NativeState you define, and eventually exports, in JS is not used anywhere in your JS code.
2. You need to put in the JS native state some types that does not exists in JS, only to have them generated by the Codegen. ImageRequest, for example, does not exists in JS, but you need it in your (iOS) state to load images
3. There are a lot of edge cases due to how C++ handles variables. Some variables needs to be created as pointers. Some others as `const &`. It does not scale to hard code all of them and there is the risk to have the same type that needs to be a pointer in some case and something else in others.
4. It is better to instruct the users on how to properly create a component with Custom State, Shadow Node and Descriptor.

## Changelog:
[General][Removed] - Back out parsing and generation of Custom Native State from Codegen

Reviewed By: cortinico

Differential Revision: D40426134

fbshipit-source-id: c368e122cc31ee8df056fe1bf6cecaab482140a4
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Oct 18, 2022
1 parent aace662 commit 62da9b8
Show file tree
Hide file tree
Showing 35 changed files with 5,323 additions and 20,783 deletions.
3 changes: 0 additions & 3 deletions packages/react-native-codegen/src/CodegenSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ export type ComponentShape = $ReadOnly<{
events: $ReadOnlyArray<EventTypeShape>,
props: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
commands: $ReadOnlyArray<NamedShape<CommandTypeAnnotation>>,
state?: $ReadOnlyArray<NamedShape<StateTypeAnnotation>>,
}>;

export type OptionsShape = $ReadOnly<{
Expand Down Expand Up @@ -187,8 +186,6 @@ export type ReservedPropTypeAnnotation = $ReadOnly<{
| 'ImageRequestPrimitive',
}>;

export type StateTypeAnnotation = PropTypeAnnotation;

export type CommandTypeAnnotation = FunctionTypeAnnotation<
CommandParamTypeAnnotation,
VoidTypeAnnotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@

'use strict';

import type {
NamedShape,
PropTypeAnnotation,
StateTypeAnnotation,
} from '../../CodegenSchema';
import type {NamedShape, PropTypeAnnotation} from '../../CodegenSchema';

import type {
StringTypeAnnotation,
Expand All @@ -27,7 +23,6 @@ import type {
} from '../../CodegenSchema';

const {
convertDefaultTypeToString,
getCppTypeForAnnotation,
getEnumMaskName,
getEnumName,
Expand All @@ -39,7 +34,6 @@ function getNativeTypeFromAnnotation(
componentName: string,
prop:
| NamedShape<PropTypeAnnotation>
| NamedShape<StateTypeAnnotation>
| {
name: string,
typeAnnotation:
Expand Down Expand Up @@ -133,28 +127,6 @@ function getNativeTypeFromAnnotation(
}
}

function getStateConstituents(
componentName: string,
stateShape: NamedShape<StateTypeAnnotation>,
): {
name: string,
varName: string,
type: string,
defaultValue: $FlowFixMe,
} {
const name = stateShape.name;
const varName = `${name}_`;
const type = getNativeTypeFromAnnotation(componentName, stateShape, []);
const defaultValue = convertDefaultTypeToString(componentName, stateShape);

return {
name,
varName,
type,
defaultValue,
};
}

/// This function process some types if we need to customize them
/// For example, the ImageSource and the reserved types could be trasformed into
/// const address instead of using them as plain types.
Expand Down Expand Up @@ -239,9 +211,7 @@ const convertVarValueToPointer = (type: string, value: string): string => {
};

function getLocalImports(
properties:
| $ReadOnlyArray<NamedShape<PropTypeAnnotation>>
| $ReadOnlyArray<NamedShape<StateTypeAnnotation>>,
properties: $ReadOnlyArray<NamedShape<PropTypeAnnotation>>,
): Set<string> {
const imports: Set<string> = new Set();

Expand Down Expand Up @@ -325,7 +295,6 @@ function getLocalImports(

module.exports = {
getNativeTypeFromAnnotation,
getStateConstituents,
convertCtorParamToAddressType,
convertGettersReturnTypeToAddressType,
convertCtorInitToSharedPointers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,17 @@

'use strict';

import type {
NamedShape,
SchemaType,
StateTypeAnnotation,
} from '../../CodegenSchema';
const {capitalize} = require('../Utils.js');
const {
getStateConstituents,
convertGettersReturnTypeToAddressType,
convertVarValueToPointer,
} = require('./ComponentsGeneratorUtils.js');
import type {NamedShape, SchemaType} from '../../CodegenSchema';

// File path -> contents
type FilesOutput = Map<string, string>;

const FileTemplate = ({
libraryName,
stateGetters,
stateClasses,
}: {
libraryName: string,
stateGetters: string,
stateClasses: string,
}) => `
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
Expand All @@ -45,34 +35,13 @@ const FileTemplate = ({
namespace facebook {
namespace react {
${stateGetters}
${stateClasses}
} // namespace react
} // namespace facebook
`;

function generateStrings(
componentName: string,
state: $ReadOnlyArray<NamedShape<StateTypeAnnotation>>,
) {
let getters = '';
state.forEach(stateShape => {
const {name, varName, type} = getStateConstituents(
componentName,
stateShape,
);

getters += `
${convertGettersReturnTypeToAddressType(
type,
)} ${componentName}State::get${capitalize(name)}() const {
return ${convertVarValueToPointer(type, varName)};
}
`;
});

return getters.trim();
}
const StateTemplate = ({stateName}: {stateName: string}) => '';

module.exports = {
generate(
Expand All @@ -83,7 +52,7 @@ module.exports = {
): FilesOutput {
const fileName = 'States.cpp';

const stateGetters = Object.keys(schema.modules)
const stateClasses = Object.keys(schema.modules)
.map(moduleName => {
const module = schema.modules[moduleName];
if (module.type !== 'Component') {
Expand All @@ -103,11 +72,9 @@ module.exports = {
return null;
}

const state = component.state;
if (!state) {
return '';
}
return generateStrings(componentName, state);
return StateTemplate({
stateName: `${componentName}State`,
});
})
.filter(Boolean)
.join('\n');
Expand All @@ -117,7 +84,7 @@ module.exports = {

const replacedTemplate = FileTemplate({
libraryName,
stateGetters,
stateClasses,
});

return new Map([[fileName, replacedTemplate]]);
Expand Down
Loading

0 comments on commit 62da9b8

Please sign in to comment.