@@ -172,10 +172,13 @@ struct BridgeJSLink {
172172 let tmpRetBytes;
173173 let tmpRetException;
174174 return {
175- /** @param {WebAssembly.Imports} importObject */
176- addImports: (importObject) => {
175+ /**
176+ * @param {WebAssembly.Imports} importObject
177+ */
178+ addImports: (importObject, importsContext) => {
177179 const bjs = {};
178180 importObject[ " bjs " ] = bjs;
181+ const imports = options.getImports(importsContext);
179182 bjs[ " swift_js_return_string " ] = function(ptr, len) {
180183 const bytes = new Uint8Array(memory.buffer, ptr, len) \( sharedMemory ? " .slice() " : " " ) ;
181184 tmpRetString = textDecoder.decode(bytes);
@@ -294,7 +297,7 @@ struct BridgeJSLink {
294297 // Add methods
295298 for method in type. methods {
296299 let methodSignature =
297- " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType) ) ; "
300+ " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType, effects : Effects ( isAsync : false , isThrows : false ) ) ) ; "
298301 typeDefinitions. append ( methodSignature. indent ( count: 4 ) )
299302 }
300303
@@ -368,7 +371,7 @@ struct BridgeJSLink {
368371
369372 for method in klass. methods {
370373 let methodSignature =
371- " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType) ) ; "
374+ " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType, effects : method . effects ) ) ; "
372375 dtsLines. append ( " \( methodSignature) " . indent ( count: identBaseSize * ( parts. count + 2 ) ) )
373376 }
374377
@@ -394,7 +397,7 @@ struct BridgeJSLink {
394397
395398 for function in functions {
396399 let signature =
397- " function \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType) ) ; "
400+ " function \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType, effects : function . effects ) ) ; "
398401 dtsLines. append ( " \( signature) " . indent ( count: identBaseSize * ( parts. count + 1 ) ) )
399402 }
400403
@@ -446,6 +449,14 @@ struct BridgeJSLink {
446449 }
447450
448451 func call( abiName: String , returnType: BridgeType ) -> String ? {
452+ if effects. isAsync {
453+ return _call ( abiName: abiName, returnType: . jsObject( nil ) )
454+ } else {
455+ return _call ( abiName: abiName, returnType: returnType)
456+ }
457+ }
458+
459+ private func _call( abiName: String , returnType: BridgeType ) -> String ? {
449460 let call = " instance.exports. \( abiName) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
450461 var returnExpr : String ?
451462
@@ -519,8 +530,15 @@ struct BridgeJSLink {
519530 }
520531 }
521532
522- private func renderTSSignature( parameters: [ Parameter ] , returnType: BridgeType ) -> String {
523- return " ( \( parameters. map { " \( $0. name) : \( $0. type. tsType) " } . joined ( separator: " , " ) ) ): \( returnType. tsType) "
533+ private func renderTSSignature( parameters: [ Parameter ] , returnType: BridgeType , effects: Effects ) -> String {
534+ let returnTypeWithEffect : String
535+ if effects. isAsync {
536+ returnTypeWithEffect = " Promise< \( returnType. tsType) > "
537+ } else {
538+ returnTypeWithEffect = returnType. tsType
539+ }
540+ return
541+ " ( \( parameters. map { " \( $0. name) : \( $0. type. tsType) " } . joined ( separator: " , " ) ) ): \( returnTypeWithEffect) "
524542 }
525543
526544 func renderExportedFunction( function: ExportedFunction ) -> ( js: [ String ] , dts: [ String ] ) {
@@ -538,7 +556,7 @@ struct BridgeJSLink {
538556 )
539557 var dtsLines : [ String ] = [ ]
540558 dtsLines. append (
541- " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType) ) ; "
559+ " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType, effects : function . effects ) ) ; "
542560 )
543561
544562 return ( funcLines, dtsLines)
@@ -581,7 +599,7 @@ struct BridgeJSLink {
581599 jsLines. append ( contentsOf: funcLines. map { $0. indent ( count: 4 ) } )
582600
583601 dtsExportEntryLines. append (
584- " constructor \( renderTSSignature ( parameters: constructor. parameters, returnType: . swiftHeapObject( klass. name) ) ) ; "
602+ " constructor \( renderTSSignature ( parameters: constructor. parameters, returnType: . swiftHeapObject( klass. name) , effects : constructor . effects ) ) ; "
585603 . indent ( count: 4 )
586604 )
587605 }
@@ -603,7 +621,7 @@ struct BridgeJSLink {
603621 ) . map { $0. indent ( count: 4 ) }
604622 )
605623 dtsTypeLines. append (
606- " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType) ) ; "
624+ " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType, effects : method . effects ) ) ; "
607625 . indent ( count: 4 )
608626 )
609627 }
@@ -712,7 +730,7 @@ struct BridgeJSLink {
712730 }
713731
714732 func call( name: String , returnType: BridgeType ) {
715- let call = " options. imports.\( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
733+ let call = " imports. \( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
716734 if returnType == . void {
717735 bodyLines. append ( " \( call) ; " )
718736 } else {
@@ -721,7 +739,7 @@ struct BridgeJSLink {
721739 }
722740
723741 func callConstructor( name: String ) {
724- let call = " new options. imports. \( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
742+ let call = " new imports. \( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
725743 bodyLines. append ( " let ret = \( call) ; " )
726744 }
727745
@@ -801,9 +819,10 @@ struct BridgeJSLink {
801819 returnExpr: returnExpr,
802820 returnType: function. returnType
803821 )
822+ let effects = Effects ( isAsync: false , isThrows: false )
804823 importObjectBuilder. appendDts (
805824 [
806- " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType) ) ; "
825+ " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType, effects : effects ) ) ; "
807826 ]
808827 )
809828 importObjectBuilder. assignToImportObject ( name: function. abiName ( context: nil ) , function: funcLines)
@@ -878,7 +897,8 @@ struct BridgeJSLink {
878897 importObjectBuilder. assignToImportObject ( name: abiName, function: funcLines)
879898 importObjectBuilder. appendDts ( [
880899 " \( type. name) : { " ,
881- " new \( renderTSSignature ( parameters: constructor. parameters, returnType: returnType) ) ; " . indent ( count: 4 ) ,
900+ " new \( renderTSSignature ( parameters: constructor. parameters, returnType: returnType, effects: Effects ( isAsync: false , isThrows: false ) ) ) ; "
901+ . indent ( count: 4 ) ,
882902 " } " ,
883903 ] )
884904 }
0 commit comments