@@ -32,6 +32,7 @@ const {
32
32
getEnumMaskName,
33
33
getEnumName,
34
34
generateStructName,
35
+ getImports,
35
36
} = require ( './CppHelpers.js' ) ;
36
37
37
38
function getNativeTypeFromAnnotation (
@@ -77,6 +78,8 @@ function getNativeTypeFromAnnotation(
77
78
return 'SharedColor ';
78
79
case 'ImageSourcePrimitive ':
79
80
return 'ImageSource ';
81
+ case 'ImageRequestPrimitive ':
82
+ return 'ImageRequest ';
80
83
case 'PointPrimitive ':
81
84
return 'Point ';
82
85
case 'EdgeInsetsPrimitive ':
@@ -152,7 +155,181 @@ function getStateConstituents(
152
155
} ;
153
156
}
154
157
158
+ /// This function process some types if we need to customize them
159
+ /// For example, the ImageSource and the reserved types could be trasformed into
160
+ /// const address instead of using them as plain types.
161
+ function convertTypesToConstAddressIfNeeded (
162
+ type : string ,
163
+ convertibleTypes : Set < string > ,
164
+ ) : string {
165
+ if ( convertibleTypes . has ( type ) ) {
166
+ return `${ type } const &` ;
167
+ }
168
+ return type ;
169
+ }
170
+
171
+ function convertValueToSharedPointerWithMove (
172
+ type : string ,
173
+ value : string ,
174
+ convertibleTypes : Set < string > ,
175
+ ) : string {
176
+ if ( convertibleTypes . has ( type ) ) {
177
+ return `std::make_shared<${ type } >(std::move(${ value } ))` ;
178
+ }
179
+ return value ;
180
+ }
181
+
182
+ function convertVariableToSharedPointer (
183
+ type : string ,
184
+ convertibleTypes : Set < string > ,
185
+ ) : string {
186
+ if ( convertibleTypes . has ( type ) ) {
187
+ return `std::shared_ptr<${ type } >` ;
188
+ }
189
+ return type ;
190
+ }
191
+
192
+ function convertVariableToPointer (
193
+ type : string ,
194
+ value : string ,
195
+ convertibleTypes : Set < string > ,
196
+ ) : string {
197
+ if ( convertibleTypes . has ( type ) ) {
198
+ return `*${ value } ` ;
199
+ }
200
+ return value ;
201
+ }
202
+
203
+ const convertCtorParamToAddressType = ( type : string ) : string => {
204
+ const typesToConvert : Set < string > = new Set();
205
+ typesToConvert.add('ImageSource');
206
+
207
+ return convertTypesToConstAddressIfNeeded(type, typesToConvert);
208
+ } ;
209
+
210
+ const convertCtorInitToSharedPointers = (
211
+ type : string ,
212
+ value : string ,
213
+ ) : string => {
214
+ const typesToConvert : Set < string > = new Set();
215
+ typesToConvert.add('ImageRequest');
216
+
217
+ return convertValueToSharedPointerWithMove(type, value, typesToConvert);
218
+ } ;
219
+
220
+ const convertGettersReturnTypeToAddressType = ( type : string ) : string => {
221
+ const typesToConvert : Set < string > = new Set();
222
+ typesToConvert.add('ImageRequest');
223
+
224
+ return convertTypesToConstAddressIfNeeded(type, typesToConvert);
225
+ } ;
226
+
227
+ const convertVarTypeToSharedPointer = ( type : string ) : string => {
228
+ const typesToConvert : Set < string > = new Set();
229
+ typesToConvert.add('ImageRequest');
230
+
231
+ return convertVariableToSharedPointer(type, typesToConvert);
232
+ } ;
233
+
234
+ const convertVarValueToPointer = ( type : string , value : string ) : string => {
235
+ const typesToConvert : Set < string > = new Set();
236
+ typesToConvert.add('ImageRequest');
237
+
238
+ return convertVariableToPointer(type, value, typesToConvert);
239
+ } ;
240
+
241
+ function getLocalImports (
242
+ properties :
243
+ | $ReadOnlyArray < NamedShape < PropTypeAnnotation >>
244
+ | $ReadOnlyArray < NamedShape < StateTypeAnnotation >> ,
245
+ ) : Set < string > {
246
+ const imports : Set < string > = new Set ( ) ;
247
+
248
+ function addImportsForNativeName (
249
+ name :
250
+ | 'ColorPrimitive'
251
+ | 'EdgeInsetsPrimitive'
252
+ | 'ImageSourcePrimitive'
253
+ | 'PointPrimitive'
254
+ | 'ImageRequestPrimitive' ,
255
+ ) {
256
+ switch ( name ) {
257
+ case 'ColorPrimitive' :
258
+ imports . add ( '#include <react/renderer/graphics/Color.h>' ) ;
259
+ return ;
260
+ case 'ImageSourcePrimitive' :
261
+ imports . add ( '#include <react/renderer/imagemanager/primitives.h>' ) ;
262
+ return ;
263
+ case 'ImageRequestPrimitive' :
264
+ imports . add ( '#include <react/renderer/imagemanager/ImageRequest.h>' ) ;
265
+ return ;
266
+ case 'PointPrimitive' :
267
+ imports . add ( '#include <react/renderer/graphics/Geometry.h>' ) ;
268
+ return ;
269
+ case 'EdgeInsetsPrimitive' :
270
+ imports . add ( '#include <react/renderer/graphics/Geometry.h>' ) ;
271
+ return ;
272
+ default :
273
+ ( name : empty ) ;
274
+ throw new Error ( `Invalid ReservedPropTypeAnnotation name, got ${ name } ` ) ;
275
+ }
276
+ }
277
+
278
+ properties . forEach ( prop => {
279
+ const typeAnnotation = prop . typeAnnotation ;
280
+
281
+ if ( typeAnnotation . type === 'ReservedPropTypeAnnotation' ) {
282
+ addImportsForNativeName ( typeAnnotation . name ) ;
283
+ }
284
+
285
+ if ( typeAnnotation . type === 'ArrayTypeAnnotation' ) {
286
+ imports . add ( '#include <vector>' ) ;
287
+ if ( typeAnnotation . elementType . type === 'StringEnumTypeAnnotation' ) {
288
+ imports . add ( '#include <cinttypes>' ) ;
289
+ }
290
+ }
291
+
292
+ if (
293
+ typeAnnotation . type === 'ArrayTypeAnnotation' &&
294
+ typeAnnotation . elementType . type === 'ReservedPropTypeAnnotation'
295
+ ) {
296
+ addImportsForNativeName ( typeAnnotation . elementType . name ) ;
297
+ }
298
+
299
+ if (
300
+ typeAnnotation . type === 'ArrayTypeAnnotation' &&
301
+ typeAnnotation . elementType . type === 'ObjectTypeAnnotation'
302
+ ) {
303
+ const objectProps = typeAnnotation . elementType . properties ;
304
+ const objectImports = getImports ( objectProps ) ;
305
+ const localImports = getLocalImports ( objectProps ) ;
306
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
307
+ objectImports . forEach ( imports . add , imports ) ;
308
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
309
+ localImports . forEach ( imports . add , imports ) ;
310
+ }
311
+
312
+ if ( typeAnnotation . type === 'ObjectTypeAnnotation' ) {
313
+ imports . add ( '#include <react/renderer/core/propsConversions.h>' ) ;
314
+ const objectImports = getImports ( typeAnnotation . properties ) ;
315
+ const localImports = getLocalImports ( typeAnnotation . properties ) ;
316
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
317
+ objectImports . forEach ( imports . add , imports ) ;
318
+ // $FlowFixMe[method-unbinding] added when improving typing for this parameters
319
+ localImports . forEach ( imports . add , imports ) ;
320
+ }
321
+ } ) ;
322
+
323
+ return imports ;
324
+ }
325
+
155
326
module . exports = {
156
327
getNativeTypeFromAnnotation,
157
328
getStateConstituents,
329
+ convertCtorParamToAddressType,
330
+ convertGettersReturnTypeToAddressType,
331
+ convertCtorInitToSharedPointers,
332
+ convertVarTypeToSharedPointer,
333
+ convertVarValueToPointer,
334
+ getLocalImports,
158
335
} ;
0 commit comments