@@ -126,10 +126,13 @@ struct BridgeJSLink {
126126 let tmpRetBytes;
127127 let tmpRetException;
128128 return {
129- /** @param {WebAssembly.Imports} importObject */
130- addImports: (importObject) => {
129+ /**
130+ * @param {WebAssembly.Imports} importObject
131+ */
132+ addImports: (importObject, importsContext) => {
131133 const bjs = {};
132134 importObject[ " bjs " ] = bjs;
135+ const imports = options.getImports(importsContext);
133136 bjs[ " swift_js_return_string " ] = function(ptr, len) {
134137 const bytes = new Uint8Array(memory.buffer, ptr, len) \( sharedMemory ? " .slice() " : " " ) ;
135138 tmpRetString = textDecoder.decode(bytes);
@@ -239,6 +242,14 @@ struct BridgeJSLink {
239242 }
240243
241244 func call( abiName: String , returnType: BridgeType ) -> String ? {
245+ if effects. isAsync {
246+ return _call ( abiName: abiName, returnType: . jsObject( nil ) )
247+ } else {
248+ return _call ( abiName: abiName, returnType: returnType)
249+ }
250+ }
251+
252+ private func _call( abiName: String , returnType: BridgeType ) -> String ? {
242253 let call = " instance.exports. \( abiName) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
243254 var returnExpr : String ?
244255
@@ -312,8 +323,14 @@ struct BridgeJSLink {
312323 }
313324 }
314325
315- private func renderTSSignature( parameters: [ Parameter ] , returnType: BridgeType ) -> String {
316- return " ( \( parameters. map { " \( $0. name) : \( $0. type. tsType) " } . joined ( separator: " , " ) ) ): \( returnType. tsType) "
326+ private func renderTSSignature( parameters: [ Parameter ] , returnType: BridgeType , effects: Effects ) -> String {
327+ let returnTypeWithEffect : String
328+ if effects. isAsync {
329+ returnTypeWithEffect = " Promise< \( returnType. tsType) > "
330+ } else {
331+ returnTypeWithEffect = returnType. tsType
332+ }
333+ return " ( \( parameters. map { " \( $0. name) : \( $0. type. tsType) " } . joined ( separator: " , " ) ) ): \( returnTypeWithEffect) "
317334 }
318335
319336 func renderExportedFunction( function: ExportedFunction ) -> ( js: [ String ] , dts: [ String ] ) {
@@ -331,7 +348,7 @@ struct BridgeJSLink {
331348 )
332349 var dtsLines : [ String ] = [ ]
333350 dtsLines. append (
334- " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType) ) ; "
351+ " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType, effects : function . effects ) ) ; "
335352 )
336353
337354 return ( funcLines, dtsLines)
@@ -362,7 +379,7 @@ struct BridgeJSLink {
362379 jsLines. append ( contentsOf: funcLines. map { $0. indent ( count: 4 ) } )
363380
364381 dtsExportEntryLines. append (
365- " new \( renderTSSignature ( parameters: constructor. parameters, returnType: . swiftHeapObject( klass. name) ) ) ; "
382+ " new \( renderTSSignature ( parameters: constructor. parameters, returnType: . swiftHeapObject( klass. name) , effects : constructor . effects ) ) ; "
366383 . indent ( count: 4 )
367384 )
368385 }
@@ -384,7 +401,7 @@ struct BridgeJSLink {
384401 ) . map { $0. indent ( count: 4 ) }
385402 )
386403 dtsTypeLines. append (
387- " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType) ) ; "
404+ " \( method. name) \( renderTSSignature ( parameters: method. parameters, returnType: method. returnType, effects : method . effects ) ) ; "
388405 . indent ( count: 4 )
389406 )
390407 }
@@ -446,7 +463,7 @@ struct BridgeJSLink {
446463 }
447464
448465 func call( name: String , returnType: BridgeType ) {
449- let call = " options. imports.\( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
466+ let call = " imports. \( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
450467 if returnType == . void {
451468 bodyLines. append ( " \( call) ; " )
452469 } else {
@@ -455,7 +472,7 @@ struct BridgeJSLink {
455472 }
456473
457474 func callConstructor( name: String ) {
458- let call = " new options. imports. \( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
475+ let call = " new imports. \( name) ( \( parameterForwardings. joined ( separator: " , " ) ) ) "
459476 bodyLines. append ( " let ret = \( call) ; " )
460477 }
461478
@@ -533,9 +550,10 @@ struct BridgeJSLink {
533550 returnExpr: returnExpr,
534551 returnType: function. returnType
535552 )
553+ let effects = Effects ( isAsync: false , isThrows: false )
536554 importObjectBuilder. appendDts (
537555 [
538- " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType) ) ; "
556+ " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType, effects : effects ) ) ; "
539557 ]
540558 )
541559 importObjectBuilder. assignToImportObject ( name: function. abiName ( context: nil ) , function: funcLines)
@@ -610,7 +628,8 @@ struct BridgeJSLink {
610628 importObjectBuilder. assignToImportObject ( name: abiName, function: funcLines)
611629 importObjectBuilder. appendDts ( [
612630 " \( type. name) : { " ,
613- " new \( renderTSSignature ( parameters: constructor. parameters, returnType: returnType) ) ; " . indent ( count: 4 ) ,
631+ " new \( renderTSSignature ( parameters: constructor. parameters, returnType: returnType, effects: Effects ( isAsync: false , isThrows: false ) ) ) ; "
632+ . indent ( count: 4 ) ,
614633 " } " ,
615634 ] )
616635 }
0 commit comments