Skip to content

Commit 684635b

Browse files
committed
Convert partial interfaces into extensions
1 parent eca6448 commit 684635b

File tree

4 files changed

+27
-38
lines changed

4 files changed

+27
-38
lines changed

Sources/DOM/Generated.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15806,21 +15806,13 @@ public class ULongRange: BridgedDictionary {
1580615806
public var min: UInt32
1580715807
}
1580815808

15809-
public class URL: JSBridgedClass {
15810-
@inlinable public class var constructor: JSFunction? { JSObject.global[Strings.URL].function }
15811-
15812-
public let jsObject: JSObject
15813-
15814-
public required init(unsafelyWrapping jsObject: JSObject) {
15815-
self.jsObject = jsObject
15816-
}
15817-
15818-
@inlinable public class func createObjectURL(obj: Blob_or_MediaSource) -> String {
15809+
public extension URL {
15810+
@inlinable class func createObjectURL(obj: Blob_or_MediaSource) -> String {
1581915811
let this = constructor!
1582015812
return this[Strings.createObjectURL].function!(this: this, arguments: [_toJSValue(obj)]).fromJSValue()!
1582115813
}
1582215814

15823-
@inlinable public class func revokeObjectURL(url: String) {
15815+
@inlinable class func revokeObjectURL(url: String) {
1582415816
let this = constructor!
1582515817
_ = this[Strings.revokeObjectURL].function!(this: this, arguments: [_toJSValue(url)])
1582615818
}

Sources/Gamepad/Generated.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,16 +362,8 @@ public class GamepadTouch: JSBridgedClass {
362362
public var surfaceDimensions: Uint32Array?
363363
}
364364

365-
public class Navigator: JSBridgedClass {
366-
@inlinable public class var constructor: JSFunction? { JSObject.global[Strings.Navigator].function }
367-
368-
public let jsObject: JSObject
369-
370-
public required init(unsafelyWrapping jsObject: JSObject) {
371-
self.jsObject = jsObject
372-
}
373-
374-
@inlinable public func getGamepads() -> [Gamepad?] {
365+
public extension Navigator {
366+
@inlinable func getGamepads() -> [Gamepad?] {
375367
let this = jsObject
376368
return this[Strings.getGamepads].function!(this: this, arguments: []).fromJSValue()!
377369
}

Sources/WebIDLToSwift/MergeDeclarations.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ enum DeclarationMerger {
121121
grouping: allNodes(ofType: IDLInterface.self).map {
122122
MergedInterface(
123123
name: $0.name,
124+
partial: $0.partial,
124125
parentClasses: [$0.inheritance]
125126
.compactMap { $0 }
126127
.filter { !Self.ignoredParents.contains($0) },
@@ -137,6 +138,7 @@ enum DeclarationMerger {
137138
by: \.name
138139
).mapValues { toMerge -> MergedInterface in
139140
var interface = toMerge.dropFirst().reduce(into: toMerge.first!) { partialResult, interface in
141+
partialResult.partial = partialResult.partial && interface.partial
140142
partialResult.parentClasses += interface.parentClasses
141143
partialResult.members += interface.members
142144
partialResult.exposed.formUnion(interface.exposed)
@@ -287,6 +289,7 @@ struct MergedDictionary: DeclarationFile {
287289

288290
struct MergedInterface: DeclarationFile {
289291
let name: String
292+
var partial: Bool
290293
var parentClasses: [String]
291294
var mixins: [String] = []
292295
var members: [IDLInterfaceMember]

Sources/WebIDLToSwift/WebIDL+SwiftRepresentation.swift

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ extension MergedInterface: SwiftRepresentable {
222222
}.joined(separator: "\n\n")
223223
}
224224

225-
let inheritance = (parentClasses.isEmpty ? ["JSBridgedClass"] : parentClasses) + mixins
226225
// Allow cross-module subclassing with `open` access modifier for classes that require this.
227226
let openClasses = [
228227
"DocumentFragment",
@@ -237,29 +236,32 @@ extension MergedInterface: SwiftRepresentable {
237236
]
238237
let access: SwiftSource = openClasses.contains(name) ? "open" : "public"
239238

240-
let globalAccessor: SwiftSource
241-
if global {
242-
globalAccessor = """
243-
@inlinable public static var global: \(name) {
244-
\(name)(unsafelyWrapping: JSObject.global)
245-
}
246-
"""
239+
let header: SwiftSource
240+
if partial {
241+
header = "public extension \(name)"
247242
} else {
248-
globalAccessor = ""
243+
let inheritance = (parentClasses.isEmpty ? ["JSBridgedClass"] : parentClasses) + mixins
244+
header = "\(access) class \(name): \(sequence: inheritance.map(SwiftSource.init(_:)))"
249245
}
250246

251247
return """
252-
\(access) class \(name): \(sequence: inheritance.map(SwiftSource.init(_:))) {
253-
@inlinable \(access)\(parentClasses.isEmpty ? "" : " override") class var constructor: JSFunction? { \(constructor) }
248+
\(header) {
249+
\(partial ? "" : """
250+
@inlinable \(access)\(parentClasses.isEmpty ? "" : " override") class var constructor: JSFunction? { \(constructor) }
254251
255-
\(parentClasses.isEmpty ? "public let jsObject: JSObject" : "")
252+
\(parentClasses.isEmpty ? "public let jsObject: JSObject" : "")
256253
257-
\(globalAccessor)
254+
\(global ? """
255+
@inlinable public static var global: \(name) {
256+
\(name)(unsafelyWrapping: JSObject.global)
257+
}
258+
""" : "")
258259
259-
public required init(unsafelyWrapping jsObject: JSObject) {
260-
\(memberInits.joined(separator: "\n"))
261-
\(parentClasses.isEmpty ? "self.jsObject = jsObject" : "super.init(unsafelyWrapping: jsObject)")
262-
}
260+
public required init(unsafelyWrapping jsObject: JSObject) {
261+
\(memberInits.joined(separator: "\n"))
262+
\(parentClasses.isEmpty ? "self.jsObject = jsObject" : "super.init(unsafelyWrapping: jsObject)")
263+
}
264+
""")
263265
264266
\(body)
265267
}

0 commit comments

Comments
 (0)