@@ -83,6 +83,7 @@ public struct ImportTS {
8383 var abiReturnType : WasmCoreType ?
8484 // Track destructured variable names for multiple lowered parameters
8585 var destructuredVarNames : [ String ] = [ ]
86+ var stackLoweringStmts : [ CodeBlockItemSyntax ] = [ ]
8687
8788 init ( moduleName: String , abiName: String , context: BridgeContext = . importTS) {
8889 self . moduleName = moduleName
@@ -93,11 +94,6 @@ public struct ImportTS {
9394 func lowerParameter( param: Parameter ) throws {
9495 let loweringInfo = try param. type. loweringParameterInfo ( context: context)
9596
96- // Generate destructured variable names for all lowered parameters
97- let destructuredNames = loweringInfo. loweredParameters. map {
98- " \( param. name) \( $0. name. capitalizedFirstLetter) "
99- }
100-
10197 let initializerExpr : ExprSyntax
10298 switch param. type {
10399 case . closure( let signature) :
@@ -108,6 +104,33 @@ public struct ImportTS {
108104 initializerExpr = ExprSyntax ( " \( raw: param. name) .bridgeJSLowerParameter() " )
109105 }
110106
107+ if loweringInfo. loweredParameters. isEmpty {
108+ let stmt = CodeBlockItemSyntax (
109+ item: . decl(
110+ DeclSyntax (
111+ VariableDeclSyntax (
112+ bindingSpecifier: . keyword( . let) ,
113+ bindings: PatternBindingListSyntax {
114+ PatternBindingSyntax (
115+ pattern: PatternSyntax (
116+ IdentifierPatternSyntax ( identifier: . wildcardToken( ) )
117+ ) ,
118+ initializer: InitializerClauseSyntax ( value: initializerExpr)
119+ )
120+ }
121+ )
122+ )
123+ )
124+ )
125+ stackLoweringStmts. insert ( stmt, at: 0 )
126+ return
127+ }
128+
129+ // Generate destructured variable names for all lowered parameters
130+ let destructuredNames = loweringInfo. loweredParameters. map {
131+ " \( param. name) \( $0. name. capitalizedFirstLetter) "
132+ }
133+
111134 // Always add destructuring statement to body (unified for single and multiple)
112135 let pattern : PatternSyntax
113136 if destructuredNames. count == 1 {
@@ -166,7 +189,9 @@ public struct ImportTS {
166189 }
167190
168191 func call( returnType: BridgeType ) throws {
169- // Build function call expression
192+ let liftingInfo = try returnType. liftingReturnInfo ( context: context)
193+ body. append ( contentsOf: stackLoweringStmts)
194+
170195 let callExpr = FunctionCallExprSyntax (
171196 calledExpression: ExprSyntax ( " \( raw: abiName) " ) ,
172197 leftParen: . leftParenToken( ) ,
@@ -183,6 +208,8 @@ public struct ImportTS {
183208 } else if returnType. usesSideChannelForOptionalReturn ( ) {
184209 // Side channel returns don't need "let ret ="
185210 body. append ( CodeBlockItemSyntax ( item: . stmt( StmtSyntax ( ExpressionStmtSyntax ( expression: callExpr) ) ) ) )
211+ } else if liftingInfo. valueToLift == nil {
212+ body. append ( CodeBlockItemSyntax ( item: . stmt( StmtSyntax ( ExpressionStmtSyntax ( expression: callExpr) ) ) ) )
186213 } else {
187214 body. append ( " let ret = \( raw: callExpr) " )
188215 }
@@ -922,13 +949,8 @@ extension BridgeType {
922949 return LoweringParameterInfo ( loweredParameters: [ ( " value " , . i32) ] )
923950 }
924951 case . rawValueEnum( _, let rawType) :
925- switch context {
926- case . importTS:
927- return LoweringParameterInfo ( loweredParameters: [ ( " value " , rawType. wasmCoreType ?? . i32) ] )
928- case . exportSwift:
929- // For protocol export we return .i32 for String raw value type instead of nil
930- return LoweringParameterInfo ( loweredParameters: [ ( " value " , rawType. wasmCoreType ?? . i32) ] )
931- }
952+ let wasmType = rawType. wasmCoreType ?? . i32
953+ return LoweringParameterInfo ( loweredParameters: [ ( " value " , wasmType) ] )
932954 case . associatedValueEnum:
933955 switch context {
934956 case . importTS:
@@ -952,12 +974,7 @@ extension BridgeType {
952974 params. append ( contentsOf: wrappedInfo. loweredParameters)
953975 return LoweringParameterInfo ( loweredParameters: params)
954976 case . array:
955- switch context {
956- case . importTS:
957- throw BridgeJSCoreError ( " Array types are not yet supported in TypeScript imports " )
958- case . exportSwift:
959- return LoweringParameterInfo ( loweredParameters: [ ] )
960- }
977+ return LoweringParameterInfo ( loweredParameters: [ ] )
961978 }
962979 }
963980
@@ -1011,13 +1028,8 @@ extension BridgeType {
10111028 return LiftingReturnInfo ( valueToLift: . i32)
10121029 }
10131030 case . rawValueEnum( _, let rawType) :
1014- switch context {
1015- case . importTS:
1016- return LiftingReturnInfo ( valueToLift: rawType. wasmCoreType ?? . i32)
1017- case . exportSwift:
1018- // For protocol export we return .i32 for String raw value type instead of nil
1019- return LiftingReturnInfo ( valueToLift: rawType. wasmCoreType ?? . i32)
1020- }
1031+ let wasmType = rawType. wasmCoreType ?? . i32
1032+ return LiftingReturnInfo ( valueToLift: wasmType)
10211033 case . associatedValueEnum:
10221034 switch context {
10231035 case . importTS:
@@ -1039,12 +1051,7 @@ extension BridgeType {
10391051 let wrappedInfo = try wrappedType. liftingReturnInfo ( context: context)
10401052 return LiftingReturnInfo ( valueToLift: wrappedInfo. valueToLift)
10411053 case . array:
1042- switch context {
1043- case . importTS:
1044- throw BridgeJSCoreError ( " Array types are not yet supported in TypeScript imports " )
1045- case . exportSwift:
1046- return LiftingReturnInfo ( valueToLift: nil )
1047- }
1054+ return LiftingReturnInfo ( valueToLift: nil )
10481055 }
10491056 }
10501057}
0 commit comments