@@ -735,15 +735,14 @@ public class ExportSwift {
735
735
}
736
736
737
737
func lookupType( for type: TypeSyntax ) -> BridgeType ? {
738
- // 1. Handle T? syntax (OptionalTypeSyntax)
738
+ // T?
739
739
if let optionalType = type. as ( OptionalTypeSyntax . self) {
740
740
let wrappedType = optionalType. wrappedType
741
741
if let baseType = lookupType ( for: wrappedType) {
742
742
return . optional( baseType)
743
743
}
744
744
}
745
-
746
- // 2. Handle Optional<T> syntax (IdentifierTypeSyntax with generic arguments)
745
+ // Optional<T>
747
746
if let identifierType = type. as ( IdentifierTypeSyntax . self) ,
748
747
identifierType. name. text == " Optional " ,
749
748
let genericArgs = identifierType. genericArgumentClause? . arguments,
@@ -753,8 +752,7 @@ public class ExportSwift {
753
752
return . optional( baseType)
754
753
}
755
754
}
756
-
757
- // 3. Handle Swift.Optional<T> syntax (MemberTypeSyntax)
755
+ // Swift.Optional<T>
758
756
if let memberType = type. as ( MemberTypeSyntax . self) ,
759
757
let baseType = memberType. baseType. as ( IdentifierTypeSyntax . self) ,
760
758
baseType. name. text == " Swift " ,
@@ -766,22 +764,17 @@ public class ExportSwift {
766
764
return . optional( wrappedType)
767
765
}
768
766
}
769
-
770
- // 4. Handle type aliases - try to resolve through type declaration resolver first
771
767
if let aliasDecl = typeDeclResolver. resolveTypeAlias ( type) {
772
- // Recursively lookup the aliased type
773
768
if let resolvedType = lookupType ( for: aliasDecl. initializer. value) {
774
769
return resolvedType
775
770
}
776
771
}
777
772
778
- // 5. Handle primitive types before falling back to other type declarations
779
773
let typeName = type. trimmedDescription
780
774
if let primitiveType = BridgeType . primitive ( swiftType: typeName) {
781
775
return primitiveType
782
776
}
783
777
784
- // 6. Try to resolve other type declarations
785
778
guard let typeDecl = typeDeclResolver. resolve ( type) else {
786
779
return nil
787
780
}
@@ -898,7 +891,6 @@ public class ExportSwift {
898
891
argumentsToLift = liftingInfo. parameters. map { ( name, _) in param. name + name. capitalizedFirstLetter }
899
892
}
900
893
901
- // For optional types, use Optional<WrappedType> syntax instead of WrappedType?
902
894
let typeNameForIntrinsic : String
903
895
switch param. type {
904
896
case . optional( let wrappedType) :
@@ -1004,7 +996,6 @@ public class ExportSwift {
1004
996
return
1005
997
}
1006
998
1007
- // Handle optional types that use special storage functions and return Void
1008
999
if case . optional( _) = returnType {
1009
1000
append ( " return ret.bridgeJSLowerReturn() " )
1010
1001
return
@@ -1095,7 +1086,7 @@ public class ExportSwift {
1095
1086
let valueSwitch = ( [ " switch self { " ] + valueCases + [ " } " ] ) . joined ( separator: " \n " )
1096
1087
1097
1088
return """
1098
- extension \( raw: typeName) {
1089
+ extension \( raw: typeName) : _BridgedSwiftCaseEnum {
1099
1090
@_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> Int32 {
1100
1091
return bridgeJSRawValue
1101
1092
}
@@ -1123,15 +1114,15 @@ public class ExportSwift {
1123
1114
func renderAssociatedValueEnumHelpers( _ enumDef: ExportedEnum ) -> DeclSyntax {
1124
1115
let typeName = enumDef. swiftCallName
1125
1116
return """
1126
- private extension \( raw: typeName) {
1127
- static func bridgeJSLiftParameter(_ caseId: Int32) -> \( raw: typeName) {
1117
+ extension \( raw: typeName) : _BridgedSwiftAssociatedValueEnum {
1118
+ @_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter(_ caseId: Int32) -> \( raw: typeName) {
1128
1119
switch caseId {
1129
1120
\( raw: generateStackLiftSwitchCases ( enumDef: enumDef) . joined ( separator: " \n " ) )
1130
1121
default: fatalError( " Unknown \( raw: typeName) case ID: \\ (caseId) " )
1131
1122
}
1132
1123
}
1133
1124
1134
- func bridgeJSLowerReturn() {
1125
+ @_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() {
1135
1126
switch self {
1136
1127
\( raw: generateReturnSwitchCases ( enumDef: enumDef) . joined ( separator: " \n " ) )
1137
1128
}
@@ -1168,7 +1159,7 @@ public class ExportSwift {
1168
1159
case . double:
1169
1160
return " \( paramName) Double.bridgeJSLiftParameter(_swift_js_pop_param_f64()) "
1170
1161
case . optional( _) :
1171
- return " /* Optional associated value lifting not yet implemented */ nil " // Placeholder
1162
+ return " nil "
1172
1163
default :
1173
1164
return " \( paramName) Int.bridgeJSLiftParameter(_swift_js_pop_param_int32()) "
1174
1165
}
@@ -1205,8 +1196,6 @@ public class ExportSwift {
1205
1196
bodyLines. append ( " _swift_js_push_f32( \( paramName) ) " )
1206
1197
case . double:
1207
1198
bodyLines. append ( " _swift_js_push_f64( \( paramName) ) " )
1208
- case . optional( _) :
1209
- bodyLines. append ( " // Optional associated value lowering not yet implemented " ) // Placeholder
1210
1199
default :
1211
1200
bodyLines. append (
1212
1201
" preconditionFailure( \" BridgeJS: unsupported associated value type in generated code \" ) "
@@ -1494,7 +1483,7 @@ extension BridgeType {
1494
1483
case . jsObject( let name? ) : return name
1495
1484
case . swiftHeapObject( let name) : return name
1496
1485
case . void: return " Void "
1497
- case . optional( let wrappedType) : return " \( wrappedType. swiftType) ? "
1486
+ case . optional( let wrappedType) : return " Optional< \( wrappedType. swiftType) > "
1498
1487
case . caseEnum( let name) : return name
1499
1488
case . rawValueEnum( let name, _) : return name
1500
1489
case . associatedValueEnum( let name) : return name
@@ -1531,7 +1520,6 @@ extension BridgeType {
1531
1520
case . swiftHeapObject: return . swiftHeapObject
1532
1521
case . void: return . void
1533
1522
case . optional( let wrappedType) :
1534
- // Optional parameters include optionality flag plus wrapped type parameters
1535
1523
var optionalParams : [ ( name: String , type: WasmCoreType ) ] = [ ( " isSome " , . i32) ]
1536
1524
optionalParams. append ( contentsOf: try wrappedType. liftParameterInfo ( ) . parameters)
1537
1525
return LiftingIntrinsicInfo ( parameters: optionalParams)
@@ -1570,6 +1558,7 @@ extension BridgeType {
1570
1558
static let caseEnum = LoweringIntrinsicInfo ( returnType: . i32)
1571
1559
static let rawValueEnum = LoweringIntrinsicInfo ( returnType: . i32)
1572
1560
static let associatedValueEnum = LoweringIntrinsicInfo ( returnType: nil )
1561
+ static let optional = LoweringIntrinsicInfo ( returnType: nil )
1573
1562
}
1574
1563
1575
1564
func loweringReturnInfo( ) throws -> LoweringIntrinsicInfo {
@@ -1582,9 +1571,7 @@ extension BridgeType {
1582
1571
case . jsObject: return . jsObject
1583
1572
case . swiftHeapObject: return . swiftHeapObject
1584
1573
case . void: return . void
1585
- case . optional( _) :
1586
- // Optional return values use special storage functions and return void
1587
- return LoweringIntrinsicInfo ( returnType: nil )
1574
+ case . optional: return . optional
1588
1575
case . caseEnum: return . caseEnum
1589
1576
case . rawValueEnum( _, let rawType) :
1590
1577
switch rawType {
@@ -1602,7 +1589,7 @@ extension BridgeType {
1602
1589
case . associatedValueEnum:
1603
1590
return . associatedValueEnum
1604
1591
case . namespaceEnum:
1605
- throw BridgeJSCoreError ( " Namespace enums are not supported to pass as parameters " )
1592
+ throw BridgeJSCoreError ( " Namespace enums are not supported as return types " )
1606
1593
}
1607
1594
}
1608
1595
}
0 commit comments