@@ -100,6 +100,15 @@ public class ExportSwift {
100100 }
101101 }
102102 }
103+
104+ withSpan ( " Render Aliases " ) { [ self ] in
105+ let aliasCodegen = AliasCodegen ( )
106+ for alias in skeleton. aliases {
107+ if let aliasExtension = aliasCodegen. renderAliasConformance ( alias) {
108+ decls. append ( aliasExtension)
109+ }
110+ }
111+ }
103112 return withSpan ( " Format Export Glue " ) {
104113 return decls. map { $0. description } . joined ( separator: " \n \n " )
105114 }
@@ -227,15 +236,15 @@ public class ExportSwift {
227236 } else {
228237 optionalSwiftType = " JSUndefinedOr "
229238 }
230- typeNameForIntrinsic = " \( optionalSwiftType) < \( wrappedType. unaliased . swiftType) > "
231- let liftCall =
232- " \( typeNameForIntrinsic) .bridgeJSLiftParameter( \( argumentsToLift. joined ( separator: " , " ) ) ) "
233- liftingExpr = " \( raw : param . type . liftAliases ( expression : liftCall ) ) "
239+ typeNameForIntrinsic = " \( optionalSwiftType) < \( wrappedType. swiftType) > "
240+ liftingExpr = ExprSyntax (
241+ " \( raw : typeNameForIntrinsic) .bridgeJSLiftParameter( \( raw : argumentsToLift. joined ( separator: " , " ) ) ) "
242+ )
234243 default :
235- typeNameForIntrinsic = param. type. unaliased . swiftType
236- let liftCall =
237- " \( typeNameForIntrinsic) .bridgeJSLiftParameter( \( argumentsToLift. joined ( separator: " , " ) ) ) "
238- liftingExpr = " \( raw : param . type . liftAliases ( expression : liftCall ) ) "
244+ typeNameForIntrinsic = param. type. swiftType
245+ liftingExpr = ExprSyntax (
246+ " \( raw : typeNameForIntrinsic) .bridgeJSLiftParameter( \( raw : argumentsToLift. joined ( separator: " , " ) ) ) "
247+ )
239248 }
240249
241250 liftedParameterExprs. append ( liftingExpr)
@@ -280,8 +289,7 @@ public class ExportSwift {
280289 }
281290
282291 if effects. isAsync, returnType != . void {
283- let lowered = returnType. lowerAliases ( expression: callExpr. description)
284- return CodeBlockItemSyntax ( item: . init( StmtSyntax ( " return \( raw: lowered) " ) ) )
292+ return CodeBlockItemSyntax ( item: . init( StmtSyntax ( " return \( raw: callExpr) " ) ) )
285293 }
286294
287295 if returnType == . void {
@@ -394,18 +402,17 @@ public class ExportSwift {
394402 return
395403 }
396404
397- let returnAccessor = returnType. lowerAliases ( expression: " ret " )
398405 switch returnType {
399406 case . closure( _, useJSTypedClosure: false ) :
400407 append ( " return JSTypedClosure(ret).bridgeJSLowerReturn() " )
401408 case . array, . nullable( . array, _) :
402409 let stackCodegen = StackCodegen ( )
403- for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: returnAccessor , varPrefix: " ret " ) {
410+ for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: " ret " , varPrefix: " ret " ) {
404411 append ( stmt)
405412 }
406413 case . dictionary( . swiftProtocol) :
407414 let stackCodegen = StackCodegen ( )
408- for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: returnAccessor , varPrefix: " ret " ) {
415+ for stmt in stackCodegen. lowerStatements ( for: returnType, accessor: " ret " , varPrefix: " ret " ) {
409416 append ( stmt)
410417 }
411418 case . swiftProtocol:
@@ -421,7 +428,7 @@ public class ExportSwift {
421428 """
422429 )
423430 default :
424- append ( " return \( raw : returnAccessor ) .bridgeJSLowerReturn() " )
431+ append ( " return ret .bridgeJSLowerReturn() " )
425432 }
426433 }
427434
@@ -880,7 +887,7 @@ struct StackCodegen {
880887 case . string, . integer, . bool, . float, . double,
881888 . jsObject( nil ) , . jsValue, . swiftStruct, . swiftHeapObject, . unsafePointer,
882889 . swiftProtocol, . caseEnum, . associatedValueEnum, . rawValueEnum, . array, . dictionary, . alias:
883- return " \( raw: type. liftAliases ( expression : " \( type . unaliased . swiftType) .bridgeJSStackPop() " ) ) "
890+ return " \( raw: type. swiftType) .bridgeJSStackPop() "
884891 case . jsObject( let className? ) :
885892 return " \( raw: className) (unsafelyWrapping: JSObject.bridgeJSStackPop()) "
886893 case . nullable( let wrappedType, let kind) :
@@ -898,9 +905,7 @@ struct StackCodegen {
898905 case . string, . integer, . bool, . float, . double, . jsObject( nil ) , . jsValue,
899906 . swiftStruct, . swiftHeapObject, . caseEnum, . associatedValueEnum, . rawValueEnum,
900907 . 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) ) "
908+ return " \( raw: typeName) < \( raw: wrappedType. swiftType) >.bridgeJSStackPop() "
904909 case . jsObject( let className? ) :
905910 return " \( raw: typeName) <JSObject>.bridgeJSStackPop().map { \( raw: className) (unsafelyWrapping: $0) } "
906911 case . nullable, . void, . namespaceEnum, . closure, . unsafePointer, . swiftProtocol:
@@ -1208,10 +1213,9 @@ struct EnumCodegen {
12081213 ) {
12091214 for (index, associatedValue) in associatedValues. enumerated ( ) {
12101215 let paramName = associatedValue. label ?? " param \( index) "
1211- let accessor = associatedValue. type. lowerAliases ( expression: paramName)
12121216 let statements = stackCodegen. lowerStatements (
12131217 for: associatedValue. type,
1214- accessor: accessor ,
1218+ accessor: paramName ,
12151219 varPrefix: paramName
12161220 )
12171221 for statement in statements {
@@ -1344,10 +1348,9 @@ struct StructCodegen {
13441348 let instanceProps = structDef. properties. filter { !$0. isStatic }
13451349
13461350 for property in instanceProps {
1347- let accessor = property. type. lowerAliases ( expression: " self. \( property. name) " )
13481351 let statements = stackCodegen. lowerStatements (
13491352 for: property. type,
1350- accessor: accessor ,
1353+ accessor: " self. \( property . name ) " ,
13511354 varPrefix: property. name
13521355 )
13531356 for statement in statements {
@@ -1359,6 +1362,18 @@ struct StructCodegen {
13591362 }
13601363}
13611364
1365+ // MARK: - AliasCodegen
1366+
1367+ struct AliasCodegen {
1368+ func renderAliasConformance( _ alias: ExportedAlias ) -> DeclSyntax ? {
1369+ guard let protocols = alias. underlying. aliasConformanceProtocols else {
1370+ return nil
1371+ }
1372+ let conformances = ( [ " _BridgedSwiftAlias " ] + protocols) . joined ( separator: " , " )
1373+ return " extension \( raw: alias. swiftCallName) : \( raw: conformances) {} "
1374+ }
1375+ }
1376+
13621377// MARK: - ProtocolCodegen
13631378
13641379struct ProtocolCodegen {
@@ -1570,61 +1585,20 @@ extension UnsafePointerType {
15701585}
15711586
15721587extension 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 {
1588+ var aliasConformanceProtocols : [ String ] ? {
16111589 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
1590+ case . swiftHeapObject, . jsObject, . integer, . float, . double, . bool, . string, . jsValue:
1591+ return [ " _BridgedSwiftStackType " ]
1592+ case . swiftStruct:
1593+ return [ " _BridgedSwiftStruct " ]
1594+ case . caseEnum:
1595+ return [ " _BridgedSwiftCaseEnum " ]
1596+ case . associatedValueEnum:
1597+ return [ " _BridgedSwiftAssociatedValueEnum " ]
1598+ case . rawValueEnum, . void, . unsafePointer, . namespaceEnum,
1599+ . swiftProtocol, . closure, . nullable, . array, . dictionary, . alias:
1600+ // Not supported yet.
1601+ return nil
16281602 }
16291603 }
16301604
0 commit comments