@@ -16,6 +16,7 @@ import type {
16
16
NativeModuleTypeAnnotation ,
17
17
NativeModuleFunctionTypeAnnotation ,
18
18
NativeModulePropertyShape ,
19
+ NativeModuleAliasMap ,
19
20
} from '../../CodegenSchema' ;
20
21
21
22
import type { AliasResolver } from './Utils' ;
@@ -28,8 +29,13 @@ type FilesOutput = Map<string, string>;
28
29
const ModuleClassDeclarationTemplate = ( {
29
30
hasteModuleName,
30
31
moduleProperties,
31
- } : $ReadOnly < { hasteModuleName : string , moduleProperties : string [ ] } > ) => {
32
- return `class JSI_EXPORT ${ hasteModuleName } CxxSpecJSI : public TurboModule {
32
+ structs,
33
+ } : $ReadOnly < {
34
+ hasteModuleName : string ,
35
+ moduleProperties : string [ ] ,
36
+ structs : string ,
37
+ } > ) => {
38
+ return `${ structs } class JSI_EXPORT ${ hasteModuleName } CxxSpecJSI : public TurboModule {
33
39
protected:
34
40
${ hasteModuleName } CxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker);
35
41
@@ -186,6 +192,76 @@ function translatePrimitiveJSTypeToCpp(
186
192
}
187
193
}
188
194
195
+ function createStructs (
196
+ moduleName : string ,
197
+ aliasMap : NativeModuleAliasMap ,
198
+ resolveAlias : AliasResolver ,
199
+ ) : string {
200
+ return Object . keys ( aliasMap )
201
+ . map ( alias => {
202
+ const value = aliasMap [ alias ] ;
203
+ if ( value . properties . length === 0 ) {
204
+ return '' ;
205
+ }
206
+ const structName = `${ moduleName } Base${ alias } ` ;
207
+ const templateParameterWithTypename = value . properties
208
+ . map ( ( v , i ) => 'typename P' + i )
209
+ . join ( ', ' ) ;
210
+ const templateParameter = value . properties
211
+ . map ( ( v , i ) => 'P' + i )
212
+ . join ( ', ' ) ;
213
+ return `#pragma mark - ${ structName }
214
+
215
+ template <${ templateParameterWithTypename } >
216
+ struct ${ structName } {
217
+ ${ value . properties . map ( ( v , i ) => ' P' + i + ' ' + v . name ) . join ( ';\n' ) } ;
218
+ bool operator==(const ${ structName } &other) const {
219
+ return ${ value . properties
220
+ . map ( v => `${ v . name } == other.${ v . name } ` )
221
+ . join ( ' && ' ) } ;
222
+ }
223
+ };
224
+
225
+ template <${ templateParameterWithTypename } >
226
+ struct ${ structName } Bridging {
227
+ static ${ structName } <${ templateParameter } > fromJs(
228
+ jsi::Runtime &rt,
229
+ const jsi::Object &value,
230
+ const std::shared_ptr<CallInvoker> &jsInvoker) {
231
+ ${ structName } <${ templateParameter } > result{
232
+ ${ value . properties
233
+ . map (
234
+ ( v , i ) =>
235
+ ` bridging::fromJs<P${ i } >(rt, value.getProperty(rt, "${ v . name } "), jsInvoker)` ,
236
+ )
237
+ . join ( ',\n' ) } };
238
+ return result;
239
+ }
240
+
241
+ static jsi::Object toJs(
242
+ jsi::Runtime &rt,
243
+ const ${ structName } <${ templateParameter } > &value) {
244
+ auto result = facebook::jsi::Object(rt);
245
+ ${ value . properties
246
+ . map ( ( v , i ) => {
247
+ if ( v . optional ) {
248
+ return ` if (value.${ v . name } ) {
249
+ result.setProperty(rt, "${ v . name } ", bridging::toJs(rt, value.${ v . name } .value()));
250
+ }` ;
251
+ } else {
252
+ return ` result.setProperty(rt, "${ v . name } ", bridging::toJs(rt, value.${ v . name } ));` ;
253
+ }
254
+ } )
255
+ . join ( '\n' ) }
256
+ return result;
257
+ }
258
+ };
259
+
260
+ ` ;
261
+ } )
262
+ . join ( '\n' ) ;
263
+ }
264
+
189
265
function translatePropertyToCpp (
190
266
prop : NativeModulePropertyShape ,
191
267
resolveAlias : AliasResolver ,
@@ -251,13 +327,15 @@ module.exports = {
251
327
moduleNames : [ moduleName ] ,
252
328
} = nativeModules [ hasteModuleName ] ;
253
329
const resolveAlias = createAliasResolver ( aliases ) ;
330
+ const structs = createStructs ( moduleName , aliases , resolveAlias ) ;
254
331
255
332
return [
256
333
ModuleClassDeclarationTemplate ( {
257
334
hasteModuleName,
258
335
moduleProperties : properties . map ( prop =>
259
336
translatePropertyToCpp ( prop , resolveAlias , true ) ,
260
337
) ,
338
+ structs,
261
339
} ) ,
262
340
ModuleSpecClassDeclarationTemplate ( {
263
341
hasteModuleName,
0 commit comments