@@ -227,15 +227,15 @@ public class ExportSwift {
227227 } else {
228228 optionalSwiftType = " JSUndefinedOr "
229229 }
230- typeNameForIntrinsic = " \( optionalSwiftType) < \( wrappedType. swiftType) > "
231- liftingExpr = ExprSyntax (
232- " \( raw : typeNameForIntrinsic) .bridgeJSLiftParameter( \( raw : argumentsToLift. joined ( separator: " , " ) ) ) "
233- )
230+ typeNameForIntrinsic = " \( optionalSwiftType) < \( wrappedType. unaliased . swiftType) > "
231+ let liftCall =
232+ " \( typeNameForIntrinsic) .bridgeJSLiftParameter( \( argumentsToLift. joined ( separator: " , " ) ) ) "
233+ liftingExpr = " \( raw : param . type . liftAliases ( expression : liftCall ) ) "
234234 default :
235- typeNameForIntrinsic = param. type. swiftType
236- liftingExpr = ExprSyntax (
237- " \( raw : typeNameForIntrinsic) .bridgeJSLiftParameter( \( raw : argumentsToLift. joined ( separator: " , " ) ) ) "
238- )
235+ typeNameForIntrinsic = param. type. unaliased . swiftType
236+ let liftCall =
237+ " \( typeNameForIntrinsic) .bridgeJSLiftParameter( \( argumentsToLift. joined ( separator: " , " ) ) ) "
238+ liftingExpr = " \( raw : param . type . liftAliases ( expression : liftCall ) ) "
239239 }
240240
241241 liftedParameterExprs. append ( liftingExpr)
@@ -280,7 +280,8 @@ public class ExportSwift {
280280 }
281281
282282 if effects. isAsync, returnType != . void {
283- return CodeBlockItemSyntax ( item: . init( StmtSyntax ( " return \( raw: callExpr) " ) ) )
283+ let lowered = returnType. lowerAliases ( expression: callExpr. description)
284+ return CodeBlockItemSyntax ( item: . init( StmtSyntax ( " return \( raw: lowered) " ) ) )
284285 }
285286
286287 if returnType == . void {
@@ -393,17 +394,18 @@ public class ExportSwift {
393394 return
394395 }
395396
397+ let returnAccessor = returnType. lowerAliases ( expression: " ret " )
396398 switch returnType {
397399 case . closure( _, useJSTypedClosure: false ) :
398400 append ( " return JSTypedClosure(ret).bridgeJSLowerReturn() " )
399401 case . array, . nullable( . array, _) :
400402 let stackCodegen = StackCodegen ( )
401- for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: " ret " , varPrefix: " ret " ) {
403+ for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: returnAccessor , varPrefix: " ret " ) {
402404 append ( stmt)
403405 }
404406 case . dictionary( . swiftProtocol) :
405407 let stackCodegen = StackCodegen ( )
406- for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: " ret " , varPrefix: " ret " ) {
408+ for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: returnAccessor , varPrefix: " ret " ) {
407409 append ( stmt)
408410 }
409411 case . swiftProtocol:
@@ -419,7 +421,7 @@ public class ExportSwift {
419421 """
420422 )
421423 default :
422- append ( " return ret .bridgeJSLowerReturn() " )
424+ append ( " return \( raw : returnAccessor ) .bridgeJSLowerReturn() " )
423425 }
424426 }
425427
@@ -877,8 +879,8 @@ struct StackCodegen {
877879 switch type {
878880 case . string, . integer, . bool, . float, . double,
879881 . jsObject( nil ) , . jsValue, . swiftStruct, . swiftHeapObject, . unsafePointer,
880- . swiftProtocol, . caseEnum, . associatedValueEnum, . rawValueEnum, . array, . dictionary:
881- return " \( raw: type. swiftType) .bridgeJSStackPop() "
882+ . swiftProtocol, . caseEnum, . associatedValueEnum, . rawValueEnum, . array, . dictionary, . alias :
883+ return " \( raw: type. liftAliases ( expression : " \( type . unaliased . swiftType) .bridgeJSStackPop() " ) ) "
882884 case . jsObject( let className? ) :
883885 return " \( raw: className) (unsafelyWrapping: JSObject.bridgeJSStackPop()) "
884886 case . nullable( let wrappedType, let kind) :
@@ -895,8 +897,10 @@ struct StackCodegen {
895897 switch wrappedType {
896898 case . string, . integer, . bool, . float, . double, . jsObject( nil ) , . jsValue,
897899 . swiftStruct, . swiftHeapObject, . caseEnum, . associatedValueEnum, . rawValueEnum,
898- . array, . dictionary:
899- return " \( raw: typeName) < \( raw: wrappedType. swiftType) >.bridgeJSStackPop() "
900+ . array, . dictionary, . alias:
901+ let popCall = " \( typeName) < \( wrappedType. unaliased. swiftType) >.bridgeJSStackPop() "
902+ let nullableType = BridgeType . nullable ( wrappedType, kind)
903+ return " \( raw: nullableType. liftAliases ( expression: popCall) ) "
900904 case . jsObject( let className? ) :
901905 return " \( raw: typeName) <JSObject>.bridgeJSStackPop().map { \( raw: className) (unsafelyWrapping: $0) } "
902906 case . nullable, . void, . namespaceEnum, . closure, . unsafePointer, . swiftProtocol:
@@ -918,7 +922,7 @@ struct StackCodegen {
918922 switch type {
919923 case . string, . integer, . bool, . float, . double, . jsValue,
920924 . jsObject( nil ) , . swiftHeapObject, . unsafePointer, . closure,
921- . caseEnum, . rawValueEnum, . associatedValueEnum, . swiftStruct, . nullable:
925+ . caseEnum, . rawValueEnum, . associatedValueEnum, . swiftStruct, . nullable, . alias :
922926 return [ " \( raw: accessor) .bridgeJSStackPush() " ]
923927 case . jsObject( _? ) :
924928 return [ " \( raw: accessor) .jsObject.bridgeJSStackPush() " ]
@@ -1204,9 +1208,10 @@ struct EnumCodegen {
12041208 ) {
12051209 for (index, associatedValue) in associatedValues. enumerated ( ) {
12061210 let paramName = associatedValue. label ?? " param \( index) "
1211+ let accessor = associatedValue. type. lowerAliases ( expression: paramName)
12071212 let statements = stackCodegen. lowerStatements (
12081213 for: associatedValue. type,
1209- accessor: paramName ,
1214+ accessor: accessor ,
12101215 varPrefix: paramName
12111216 )
12121217 for statement in statements {
@@ -1339,7 +1344,7 @@ struct StructCodegen {
13391344 let instanceProps = structDef. properties. filter { !$0. isStatic }
13401345
13411346 for property in instanceProps {
1342- let accessor = " self. \( property. name) "
1347+ let accessor = property . type . lowerAliases ( expression : " self. \( property. name) " )
13431348 let statements = stackCodegen. lowerStatements (
13441349 for: property. type,
13451350 accessor: accessor,
@@ -1565,6 +1570,64 @@ extension UnsafePointerType {
15651570}
15661571
15671572extension BridgeType {
1573+ var unaliased : BridgeType {
1574+ switch self {
1575+ case . alias( _, let underlying) : return underlying. unaliased
1576+ case . nullable( let wrapped, let kind) : return . nullable( wrapped. unaliased, kind)
1577+ case . array( let element) : return . array( element. unaliased)
1578+ case . dictionary( let value) : return . dictionary( value. unaliased)
1579+ case . bool, . integer, . float, . double, . string, . jsValue, . jsObject,
1580+ . swiftHeapObject, . unsafePointer, . swiftProtocol, . void,
1581+ . caseEnum, . rawValueEnum, . associatedValueEnum, . swiftStruct,
1582+ . namespaceEnum, . closure:
1583+ return self
1584+ }
1585+ }
1586+
1587+ /// If this type contains an alias, convert the expression with a type of the alias to the underlying type.
1588+ func liftAliases( expression: String ) -> String {
1589+ switch self {
1590+ case . alias( let name, _) :
1591+ return " \( name) .bridgeFromJS( \( expression) ) "
1592+ case . nullable( let wrapped, _) :
1593+ let lifted = wrapped. liftAliases ( expression: " $0 " )
1594+ return lifted == " $0 " ? expression : " \( expression) .map { \( lifted) } "
1595+ case . array( let element) :
1596+ let lifted = element. liftAliases ( expression: " $0 " )
1597+ return lifted == " $0 " ? expression : " \( expression) .map { \( lifted) } "
1598+ case . dictionary( let value) :
1599+ let lifted = value. liftAliases ( expression: " $0 " )
1600+ return lifted == " $0 " ? expression : " \( expression) .mapValues { \( lifted) } "
1601+ case . bool, . integer, . float, . double, . string, . jsValue, . jsObject,
1602+ . swiftHeapObject, . unsafePointer, . swiftProtocol, . void,
1603+ . caseEnum, . rawValueEnum, . associatedValueEnum, . swiftStruct,
1604+ . namespaceEnum, . closure:
1605+ return expression
1606+ }
1607+ }
1608+
1609+ /// Opposite of `liftAliases`: if this type contains an alias, convert the expression with a type of the underlying to the alias type.
1610+ func lowerAliases( expression: String ) -> String {
1611+ switch self {
1612+ case . alias:
1613+ return " \( expression) .bridgeToJS() "
1614+ case . nullable( let wrapped, _) :
1615+ let lowered = wrapped. lowerAliases ( expression: " $0 " )
1616+ return lowered == " $0 " ? expression : " \( expression) .map { \( lowered) } "
1617+ case . array( let element) :
1618+ let lowered = element. lowerAliases ( expression: " $0 " )
1619+ return lowered == " $0 " ? expression : " \( expression) .map { \( lowered) } "
1620+ case . dictionary( let value) :
1621+ let lowered = value. lowerAliases ( expression: " $0 " )
1622+ return lowered == " $0 " ? expression : " \( expression) .mapValues { \( lowered) } "
1623+ case . bool, . integer, . float, . double, . string, . jsValue, . jsObject,
1624+ . swiftHeapObject, . unsafePointer, . swiftProtocol, . void,
1625+ . caseEnum, . rawValueEnum, . associatedValueEnum, . swiftStruct,
1626+ . namespaceEnum, . closure:
1627+ return expression
1628+ }
1629+ }
1630+
15681631 var swiftType : String {
15691632 switch self {
15701633 case . bool: return " Bool "
@@ -1593,6 +1656,7 @@ extension BridgeType {
15931656 let effectsStr = ( signature. isAsync ? " async " : " " ) + ( signature. isThrows ? " throws " : " " )
15941657 let closureType = " ( \( paramTypes) ) \( effectsStr) -> \( signature. returnType. swiftType) "
15951658 return useJSTypedClosure ? " JSTypedClosure< \( closureType) > " : closureType
1659+ case . alias( let name, _) : return name
15961660 }
15971661 }
15981662
@@ -1617,6 +1681,8 @@ extension BridgeType {
16171681 return true
16181682 case . nullable( let wrapped, _) :
16191683 return wrapped. isStackUsingParameter
1684+ case . alias( _, let underlying) :
1685+ return underlying. isStackUsingParameter
16201686 default :
16211687 return false
16221688 }
@@ -1675,6 +1741,8 @@ extension BridgeType {
16751741 return LiftingIntrinsicInfo ( parameters: [ ( " callbackId " , . i32) ] )
16761742 case . array, . dictionary:
16771743 return LiftingIntrinsicInfo ( parameters: [ ] )
1744+ case . alias( _, let underlying) :
1745+ return try underlying. liftParameterInfo ( )
16781746 }
16791747 }
16801748
@@ -1726,6 +1794,8 @@ extension BridgeType {
17261794 return . jsObject
17271795 case . array, . dictionary:
17281796 return . array
1797+ case . alias( _, let underlying) :
1798+ return try underlying. loweringReturnInfo ( )
17291799 }
17301800 }
17311801}
0 commit comments