Skip to content

Commit 67f3be5

Browse files
authored
BridgeJS: Support any T and (T) (#819)
1 parent c68ee9b commit 67f3be5

5 files changed

Lines changed: 30 additions & 15 deletions

File tree

‎Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,21 @@ public final class SwiftToSkeleton {
436436
return lookupType(for: attributedType.baseType, errors: &errors)
437437
}
438438

439+
// any P
440+
if let existentialType = type.as(SomeOrAnyTypeSyntax.self),
441+
existentialType.someOrAnySpecifier.tokenKind == .keyword(.any)
442+
{
443+
return lookupType(for: existentialType.constraint, errors: &errors)
444+
}
445+
446+
// (T)
447+
if let tupleType = type.as(TupleTypeSyntax.self),
448+
tupleType.elements.count == 1,
449+
let element = tupleType.elements.first
450+
{
451+
return lookupType(for: element.type, errors: &errors)
452+
}
453+
439454
// JSTypedArray<T>
440455
if let identifierType = type.as(IdentifierTypeSyntax.self),
441456
identifierType.name.text == "JSTypedArray",

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Protocol.swift‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ import JavaScriptKit
6666
var delegate: MyViewControllerDelegate
6767

6868
@JS
69-
var secondDelegate: MyViewControllerDelegate?
69+
var secondDelegate: (any MyViewControllerDelegate)?
7070

71-
@JS init(delegate: MyViewControllerDelegate) {
71+
@JS init(delegate: any MyViewControllerDelegate) {
7272
self.delegate = delegate
7373
}
7474

@@ -103,7 +103,7 @@ import JavaScriptKit
103103
var delegates: [MyViewControllerDelegate]
104104

105105
@JS
106-
var delegatesByName: [String: MyViewControllerDelegate]
106+
var delegatesByName: [String: any MyViewControllerDelegate]
107107

108108
@JS init(delegates: [MyViewControllerDelegate]) {
109109
self.delegates = delegates
@@ -117,7 +117,7 @@ import JavaScriptKit
117117
}
118118
}
119119

120-
@JS func processDelegates(_ delegates: [MyViewControllerDelegate]) -> [MyViewControllerDelegate]
120+
@JS func processDelegates(_ delegates: [any MyViewControllerDelegate]) -> [any MyViewControllerDelegate]
121121

122122
@JS func processDelegatesByName(
123123
_ delegates: [String: MyViewControllerDelegate]

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/ProtocolInClosure.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ import JavaScriptKit
1414

1515
@JS func processRenderable(_ item: Renderable, transform: (Renderable) -> String) -> String
1616
@JS func makeRenderableFactory(defaultName: String) -> () -> Renderable
17-
@JS func roundtripRenderable(_ callback: (Renderable) -> Renderable) -> (Renderable) -> Renderable
18-
@JS func processOptionalRenderable(_ callback: (Renderable?) -> String) -> String
17+
@JS func roundtripRenderable(_ callback: (any Renderable) -> any Renderable) -> (any Renderable) -> any Renderable
18+
@JS func processOptionalRenderable(_ callback: ((any Renderable)?) -> String) -> String

‎Tests/BridgeJSRuntimeTests/ArraySupportTests.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ final class ArraySupportTests: XCTestCase {
195195
@JS static func roundTripStructArray(_ v: [DataPoint]) -> [DataPoint] { v }
196196
@JS static func roundTripSwiftClassArray(_ v: [Greeter]) -> [Greeter] { v }
197197
@JS static func roundTripNamespacedSwiftClassArray(_ v: [Utils.Converter]) -> [Utils.Converter] { v }
198-
@JS static func roundTripProtocolArray(_ v: [ArrayElementProtocol]) -> [ArrayElementProtocol] { v }
198+
@JS static func roundTripProtocolArray(_ v: [any ArrayElementProtocol]) -> [any ArrayElementProtocol] { v }
199199
@JS static func roundTripJSClassArray(_ v: [ArrayElementObject]) -> [ArrayElementObject] { v }
200200

201201
@JS static func roundTripOptionalIntArray(_ v: [Int?]) -> [Int?] { v }

‎Tests/BridgeJSRuntimeTests/ExportAPITests.swift‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,9 @@ enum GraphOperations {
966966
@JS class DataProcessorManager {
967967

968968
@JS var processor: DataProcessor
969-
@JS var backupProcessor: DataProcessor?
969+
@JS var backupProcessor: (any DataProcessor)?
970970

971-
@JS init(processor: DataProcessor) {
971+
@JS init(processor: any DataProcessor) {
972972
self.processor = processor
973973
self.backupProcessor = nil
974974
}
@@ -1124,23 +1124,23 @@ enum GraphOperations {
11241124
return SwiftDataProcessor()
11251125
}
11261126

1127-
@JS static func createNativeProcessorOptional() -> DataProcessor? {
1127+
@JS static func createNativeProcessorOptional() -> (any DataProcessor)? {
11281128
return SwiftDataProcessor()
11291129
}
11301130

11311131
@JS static func createNativeProcessorNil() -> DataProcessor? {
11321132
return nil
11331133
}
11341134

1135-
@JS static func createNativeProcessorArray() -> [DataProcessor] {
1135+
@JS static func createNativeProcessorArray() -> [any DataProcessor] {
11361136
let p1 = SwiftDataProcessor()
11371137
p1.count = 10
11381138
let p2 = SwiftDataProcessor()
11391139
p2.count = 20
11401140
return [p1, p2]
11411141
}
11421142

1143-
@JS static func createNativeProcessorDictionary() -> [String: DataProcessor] {
1143+
@JS static func createNativeProcessorDictionary() -> [String: any DataProcessor] {
11441144
let p1 = SwiftDataProcessor()
11451145
p1.count = 10
11461146
let p2 = SwiftDataProcessor()
@@ -1317,12 +1317,12 @@ enum GraphOperations {
13171317
}
13181318

13191319
@JS func roundtripDataProcessor(
1320-
_ callback: @escaping (DataProcessor) -> DataProcessor
1321-
) -> (DataProcessor) -> DataProcessor {
1320+
_ callback: @escaping (any DataProcessor) -> any DataProcessor
1321+
) -> (any DataProcessor) -> any DataProcessor {
13221322
return callback
13231323
}
13241324

1325-
@JS func processOptionalDataProcessor(_ callback: (DataProcessor?) -> String) -> String {
1325+
@JS func processOptionalDataProcessor(_ callback: ((any DataProcessor)?) -> String) -> String {
13261326
let processor = SwiftDataProcessor()
13271327
processor.increment(by: 7)
13281328
return callback(processor) + " | " + callback(nil)

0 commit comments

Comments
 (0)