@@ -16,6 +16,7 @@ import type {
1616 NativeModuleTypeAnnotation ,
1717 NativeModuleFunctionTypeAnnotation ,
1818 NativeModulePropertyShape ,
19+ NativeModuleAliasMap ,
1920} from '../../CodegenSchema' ;
2021
2122import type { AliasResolver } from './Utils' ;
@@ -28,8 +29,13 @@ type FilesOutput = Map<string, string>;
2829const ModuleClassDeclarationTemplate = ( {
2930 hasteModuleName,
3031 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 {
3339protected:
3440 ${ hasteModuleName } CxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker);
3541
@@ -186,6 +192,76 @@ function translatePrimitiveJSTypeToCpp(
186192 }
187193}
188194
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+
189265function translatePropertyToCpp (
190266 prop : NativeModulePropertyShape ,
191267 resolveAlias : AliasResolver ,
@@ -251,13 +327,15 @@ module.exports = {
251327 moduleNames : [ moduleName ] ,
252328 } = nativeModules [ hasteModuleName ] ;
253329 const resolveAlias = createAliasResolver ( aliases ) ;
330+ const structs = createStructs ( moduleName , aliases , resolveAlias ) ;
254331
255332 return [
256333 ModuleClassDeclarationTemplate ( {
257334 hasteModuleName,
258335 moduleProperties : properties . map ( prop =>
259336 translatePropertyToCpp ( prop , resolveAlias , true ) ,
260337 ) ,
338+ structs,
261339 } ) ,
262340 ModuleSpecClassDeclarationTemplate ( {
263341 hasteModuleName,
0 commit comments