@@ -83,6 +83,8 @@ public struct ImportTS {
8383 var abiReturnType : WasmCoreType ?
8484 // Track destructured variable names for multiple lowered parameters
8585 var destructuredVarNames : [ String ] = [ ]
86+ // Stack-lowered parameters should be evaluated in reverse order to match LIFO stacks
87+ var stackLoweringStmts : [ CodeBlockItemSyntax ] = [ ]
8688
8789 init ( moduleName : String , abiName : String , context : BridgeContext = . importTS ) {
8890 self . moduleName = moduleName
@@ -93,11 +95,6 @@ public struct ImportTS {
9395 func lowerParameter ( param : Parameter ) throws {
9496 let loweringInfo = try param . type . loweringParameterInfo ( context : context )
9597
96- // Generate destructured variable names for all lowered parameters
97- let destructuredNames = loweringInfo . loweredParameters . map {
98- " \( param . name ) \( $0 . name . capitalizedFirstLetter ) "
99- }
100-
10198 let initializerExpr : ExprSyntax
10299 switch param . type {
103100 case . closure ( let signature ) :
@@ -108,6 +105,33 @@ public struct ImportTS {
108105 initializerExpr = ExprSyntax ( " \( raw : param . name ) .bridgeJSLowerParameter() " )
109106 }
110107
108+ if loweringInfo . loweredParameters . isEmpty {
109+ let stmt = CodeBlockItemSyntax (
110+ item : . decl (
111+ DeclSyntax (
112+ VariableDeclSyntax (
113+ bindingSpecifier : . keyword ( . let ) ,
114+ bindings : PatternBindingListSyntax {
115+ PatternBindingSyntax (
116+ pattern : PatternSyntax (
117+ IdentifierPatternSyntax ( identifier : . wildcardToken ( ) )
118+ ) ,
119+ initializer : InitializerClauseSyntax ( value : initializerExpr )
120+ )
121+ }
122+ )
123+ )
124+ )
125+ )
126+ stackLoweringStmts . insert ( stmt , at : 0 )
127+ return
128+ }
129+
130+ // Generate destructured variable names for all lowered parameters
131+ let destructuredNames = loweringInfo . loweredParameters . map {
132+ " \( param . name ) \( $0 . name . capitalizedFirstLetter ) "
133+ }
134+
111135 // Always add destructuring statement to body (unified for single and multiple)
112136 let pattern : PatternSyntax
113137 if destructuredNames . count == 1 {
@@ -166,7 +190,9 @@ public struct ImportTS {
166190 }
167191
168192 func call ( returnType : BridgeType ) throws {
169- // Build function call expression
193+ let liftingInfo = try returnType . liftingReturnInfo ( context : context )
194+ body . append ( contentsOf : stackLoweringStmts )
195+
170196 let callExpr = FunctionCallExprSyntax (
171197 calledExpression : ExprSyntax ( " \( raw : abiName ) " ) ,
172198 leftParen : . leftParenToken ( ) ,
@@ -178,19 +204,15 @@ public struct ImportTS {
178204 rightParen : . rightParenToken ( )
179205 )
180206
181- let needsRetBinding : Bool
182- let liftingInfo = try returnType . liftingReturnInfo ( context : context )
183- if liftingInfo . valueToLift == nil || returnType . usesSideChannelForOptionalReturn ( ) {
184- // Void and side-channel returns don't need "let ret ="
185- needsRetBinding = false
207+ if returnType == . void {
208+ body . append ( CodeBlockItemSyntax ( item : . stmt ( StmtSyntax ( ExpressionStmtSyntax ( expression : callExpr ) ) ) ) )
209+ } else if returnType . usesSideChannelForOptionalReturn ( ) {
210+ // Side channel returns don't need "let ret ="
211+ body . append ( CodeBlockItemSyntax ( item : . stmt ( StmtSyntax ( ExpressionStmtSyntax ( expression : callExpr ) ) ) ) )
212+ } else if liftingInfo . valueToLift == nil {
213+ body . append ( CodeBlockItemSyntax ( item : . stmt ( StmtSyntax ( ExpressionStmtSyntax ( expression : callExpr ) ) ) ) )
186214 } else {
187- needsRetBinding = true
188- }
189-
190- if needsRetBinding {
191215 body . append ( " let ret = \( raw : callExpr ) " )
192- } else {
193- body . append ( CodeBlockItemSyntax ( item : . stmt ( StmtSyntax ( ExpressionStmtSyntax ( expression : callExpr ) ) ) ) )
194216 }
195217
196218 // Add exception check for ImportTS context
@@ -934,13 +956,8 @@ extension BridgeType {
934956 return LoweringParameterInfo ( loweredParameters : [ ( " value " , . i32 ) ] )
935957 }
936958 case . rawValueEnum ( _, let rawType ) :
937- switch context {
938- case . importTS :
939- return LoweringParameterInfo ( loweredParameters : [ ( " value " , rawType . wasmCoreType ?? . i32 ) ] )
940- case . exportSwift :
941- // For protocol export we return .i32 for String raw value type instead of nil
942- return LoweringParameterInfo ( loweredParameters : [ ( " value " , rawType . wasmCoreType ?? . i32 ) ] )
943- }
959+ let wasmType = rawType . wasmCoreType ?? . i32
960+ return LoweringParameterInfo ( loweredParameters : [ ( " value " , wasmType ) ] )
944961 case . associatedValueEnum :
945962 switch context {
946963 case . importTS :
@@ -964,12 +981,7 @@ extension BridgeType {
964981 params . append ( contentsOf : wrappedInfo . loweredParameters )
965982 return LoweringParameterInfo ( loweredParameters : params )
966983 case . array :
967- switch context {
968- case . importTS :
969- throw BridgeJSCoreError ( " Array types are not yet supported in TypeScript imports " )
970- case . exportSwift :
971- return LoweringParameterInfo ( loweredParameters : [ ] )
972- }
984+ return LoweringParameterInfo ( loweredParameters : [ ] )
973985 }
974986 }
975987
@@ -1025,13 +1037,8 @@ extension BridgeType {
10251037 return LiftingReturnInfo ( valueToLift : . i32 )
10261038 }
10271039 case . rawValueEnum ( _, let rawType ) :
1028- switch context {
1029- case . importTS :
1030- return LiftingReturnInfo ( valueToLift : rawType . wasmCoreType ?? . i32 )
1031- case . exportSwift :
1032- // For protocol export we return .i32 for String raw value type instead of nil
1033- return LiftingReturnInfo ( valueToLift : rawType . wasmCoreType ?? . i32 )
1034- }
1040+ let wasmType = rawType . wasmCoreType ?? . i32
1041+ return LiftingReturnInfo ( valueToLift : wasmType )
10351042 case . associatedValueEnum :
10361043 switch context {
10371044 case . importTS :
@@ -1053,12 +1060,7 @@ extension BridgeType {
10531060 let wrappedInfo = try wrappedType . liftingReturnInfo ( context : context )
10541061 return LiftingReturnInfo ( valueToLift : wrappedInfo . valueToLift )
10551062 case . array :
1056- switch context {
1057- case . importTS :
1058- throw BridgeJSCoreError ( " Array types are not yet supported in TypeScript imports " )
1059- case . exportSwift :
1060- return LiftingReturnInfo ( valueToLift : nil )
1061- }
1063+ return LiftingReturnInfo ( valueToLift : nil )
10621064 }
10631065 }
10641066}
0 commit comments