@@ -1667,6 +1667,7 @@ public class ExportSwift {
16671667 }
16681668
16691669 func call( name: String , returnType: BridgeType ) {
1670+ generateParameterLifting ( )
16701671 let item = renderCallStatement ( callee: " \( raw: name) " , returnType: returnType)
16711672 append ( item)
16721673 }
@@ -1694,6 +1695,7 @@ public class ExportSwift {
16941695
16951696 func callMethod( klassName: String , methodName: String , returnType: BridgeType ) {
16961697 let ( _, selfExpr) = removeFirstLiftedParameter ( )
1698+ generateParameterLifting ( )
16971699 let item = renderCallStatement (
16981700 callee: " \( raw: selfExpr) . \( raw: methodName) " ,
16991701 returnType: returnType
@@ -1827,6 +1829,29 @@ public class ExportSwift {
18271829 func returnSignature( ) -> String {
18281830 return abiReturnType? . swiftType ?? " Void "
18291831 }
1832+
1833+ /// Generates intermediate variables for stack-using parameters if needed for LIFO compatibility
1834+ private func generateParameterLifting( ) {
1835+ let stackParamIndices = parameters. enumerated ( ) . compactMap { index, param -> Int ? in
1836+ switch param. type {
1837+ case . optional( . associatedValueEnum) :
1838+ return index
1839+ default :
1840+ return nil
1841+ }
1842+ }
1843+
1844+ guard stackParamIndices. count > 1 else { return }
1845+
1846+ for index in stackParamIndices. reversed ( ) {
1847+ let param = parameters [ index]
1848+ let expr = liftedParameterExprs [ index]
1849+ let varName = " _tmp_ \( param. name) "
1850+
1851+ append ( " let \( raw: varName) = \( expr) " )
1852+ liftedParameterExprs [ index] = ExprSyntax ( DeclReferenceExprSyntax ( baseName: . identifier( varName) ) )
1853+ }
1854+ }
18301855 }
18311856
18321857 private struct ClosureCodegen {
0 commit comments