forked from react/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCppHelpers.js
More file actions
328 lines (306 loc) · 9.19 KB
/
Copy pathCppHelpers.js
File metadata and controls
328 lines (306 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
'use strict';
import type {
EventTypeAnnotation,
NamedShape,
PropTypeAnnotation,
} from '../../CodegenSchema';
const {getEnumName, toSafeCppString} = require('../Utils');
function toIntEnumValueName(propName: string, value: number): string {
return `${toSafeCppString(propName)}${value}`;
}
function getCppTypeForAnnotation(
type:
| 'BooleanTypeAnnotation'
| 'StringTypeAnnotation'
| 'Int32TypeAnnotation'
| 'DoubleTypeAnnotation'
| 'FloatTypeAnnotation'
| 'MixedTypeAnnotation',
): string {
switch (type) {
case 'BooleanTypeAnnotation':
return 'bool';
case 'StringTypeAnnotation':
return 'std::string';
case 'Int32TypeAnnotation':
return 'int';
case 'DoubleTypeAnnotation':
return 'double';
case 'FloatTypeAnnotation':
return 'Float';
case 'MixedTypeAnnotation':
return 'folly::dynamic';
default:
(type: empty);
throw new Error(`Received invalid typeAnnotation ${type}`);
}
}
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>>
| $ReadOnlyArray<NamedShape<EventTypeAnnotation>>,
): Set<string> {
const imports: Set<string> = new Set();
function addImportsForNativeName(
name:
| 'ColorPrimitive'
| 'EdgeInsetsPrimitive'
| 'ImageRequestPrimitive'
| 'ImageSourcePrimitive'
| 'PointPrimitive'
| 'DimensionPrimitive',
) {
switch (name) {
case 'ColorPrimitive':
return;
case 'PointPrimitive':
return;
case 'EdgeInsetsPrimitive':
return;
case 'ImageRequestPrimitive':
return;
case 'ImageSourcePrimitive':
imports.add('#include <react/renderer/components/image/conversions.h>');
return;
case 'DimensionPrimitive':
imports.add('#include <react/renderer/components/view/conversions.h>');
return;
default:
(name: empty);
throw new Error(`Invalid name, got ${name}`);
}
}
properties.forEach(prop => {
const typeAnnotation = prop.typeAnnotation;
if (typeAnnotation.type === 'ReservedPropTypeAnnotation') {
addImportsForNativeName(typeAnnotation.name);
}
if (
typeAnnotation.type === 'ArrayTypeAnnotation' &&
typeAnnotation.elementType.type === 'ReservedPropTypeAnnotation'
) {
addImportsForNativeName(typeAnnotation.elementType.name);
}
if (typeAnnotation.type === 'MixedTypeAnnotation') {
imports.add('#include <folly/dynamic.h>');
}
if (typeAnnotation.type === 'ObjectTypeAnnotation') {
const objectImports = getImports(typeAnnotation.properties);
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
objectImports.forEach(imports.add, imports);
}
});
return imports;
}
function generateEventStructName(parts: $ReadOnlyArray<string> = []): string {
return parts.map(toSafeCppString).join('');
}
function generateStructName(
componentName: string,
parts: $ReadOnlyArray<string> = [],
): string {
const additional = parts.map(toSafeCppString).join('');
return `${componentName}${additional}Struct`;
}
function getEnumMaskName(enumName: string): string {
return `${enumName}Mask`;
}
function getDefaultInitializerString(
componentName: string,
prop: NamedShape<PropTypeAnnotation>,
): string {
const defaultValue = convertDefaultTypeToString(componentName, prop);
return `{${defaultValue}}`;
}
function convertDefaultTypeToString(
componentName: string,
prop: NamedShape<PropTypeAnnotation>,
fromBuilder: boolean = false,
): string {
const typeAnnotation = prop.typeAnnotation;
switch (typeAnnotation.type) {
case 'BooleanTypeAnnotation':
if (typeAnnotation.default == null) {
return '';
}
return String(typeAnnotation.default);
case 'StringTypeAnnotation':
if (typeAnnotation.default == null) {
return '';
}
return `"${typeAnnotation.default}"`;
case 'Int32TypeAnnotation':
return String(typeAnnotation.default);
case 'DoubleTypeAnnotation':
const defaultDoubleVal = typeAnnotation.default;
return parseInt(defaultDoubleVal, 10) === defaultDoubleVal
? typeAnnotation.default.toFixed(1)
: String(typeAnnotation.default);
case 'FloatTypeAnnotation':
const defaultFloatVal = typeAnnotation.default;
if (defaultFloatVal == null) {
return '';
}
return parseInt(defaultFloatVal, 10) === defaultFloatVal
? defaultFloatVal.toFixed(1)
: String(typeAnnotation.default);
case 'ReservedPropTypeAnnotation':
switch (typeAnnotation.name) {
case 'ColorPrimitive':
return '';
case 'ImageSourcePrimitive':
return '';
case 'ImageRequestPrimitive':
return '';
case 'PointPrimitive':
return '';
case 'EdgeInsetsPrimitive':
return '';
case 'DimensionPrimitive':
return '';
default:
(typeAnnotation.name: empty);
throw new Error(
`Unsupported type annotation: ${typeAnnotation.name}`,
);
}
case 'ArrayTypeAnnotation': {
const elementType = typeAnnotation.elementType;
switch (elementType.type) {
case 'StringEnumTypeAnnotation':
if (elementType.default == null) {
throw new Error(
'A default is required for array StringEnumTypeAnnotation',
);
}
const enumName = getEnumName(componentName, prop.name);
const enumMaskName = getEnumMaskName(enumName);
const defaultValue = `${enumName}::${toSafeCppString(
elementType.default,
)}`;
if (fromBuilder) {
return `${enumMaskName}Wrapped{ .value = static_cast<${enumMaskName}>(${defaultValue})}`;
}
return `static_cast<${enumMaskName}>(${defaultValue})`;
default:
return '';
}
}
case 'ObjectTypeAnnotation': {
return '';
}
case 'StringEnumTypeAnnotation':
return `${getEnumName(componentName, prop.name)}::${toSafeCppString(
typeAnnotation.default,
)}`;
case 'Int32EnumTypeAnnotation':
return `${getEnumName(componentName, prop.name)}::${toIntEnumValueName(
prop.name,
typeAnnotation.default,
)}`;
case 'MixedTypeAnnotation':
return '';
default:
(typeAnnotation: empty);
throw new Error(`Unsupported type annotation: ${typeAnnotation.type}`);
}
}
function getSourceProp(
componentName: string,
prop: NamedShape<PropTypeAnnotation>,
): string {
const typeAnnotation = prop.typeAnnotation;
switch (typeAnnotation.type) {
case 'ArrayTypeAnnotation':
const elementType = typeAnnotation.elementType;
switch (elementType.type) {
case 'StringEnumTypeAnnotation':
const enumName = getEnumName(componentName, prop.name);
const enumMaskName = getEnumMaskName(enumName);
return `${enumMaskName}Wrapped{ .value = sourceProps.${prop.name} }`;
}
}
return `sourceProps.${prop.name}`;
}
function isWrappedPropType(prop: NamedShape<PropTypeAnnotation>): boolean {
const typeAnnotation = prop.typeAnnotation;
switch (typeAnnotation.type) {
case 'ArrayTypeAnnotation':
const elementType = typeAnnotation.elementType;
switch (elementType.type) {
case 'StringEnumTypeAnnotation':
return true;
}
}
return false;
}
const IncludeTemplate = ({
headerPrefix,
file,
}: {
headerPrefix: string,
file: string,
}): string => {
if (headerPrefix === '') {
return `#include "${file}"`;
}
return `#include <${headerPrefix}${file}>`;
};
module.exports = {
getDefaultInitializerString,
convertDefaultTypeToString,
getCppArrayTypeForAnnotation,
getCppTypeForAnnotation,
getEnumMaskName,
getImports,
toIntEnumValueName,
generateStructName,
generateEventStructName,
IncludeTemplate,
getSourceProp,
isWrappedPropType,
};