Skip to content

Commit 76a7af6

Browse files
committed
Add global static var for global objects, e.g. Window.global
1 parent 61144ad commit 76a7af6

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Sources/DOM/Generated.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16817,6 +16817,10 @@ public class WheelEventInit: BridgedDictionary {
1681716817
public class Window: EventTarget, GlobalEventHandlers, WindowEventHandlers, WindowOrWorkerGlobalScope, AnimationFrameProvider, WindowSessionStorage, WindowLocalStorage {
1681816818
@inlinable override public class var constructor: JSFunction? { JSObject.global[Strings.Window].function }
1681916819

16820+
@inlinable public static var global: Window {
16821+
Window(unsafelyWrapping: JSObject.global)
16822+
}
16823+
1682016824
public required init(unsafelyWrapping jsObject: JSObject) {
1682116825
_event = ReadonlyAttribute(jsObject: jsObject, name: Strings.event)
1682216826
_window = ReadonlyAttribute(jsObject: jsObject, name: Strings.window)

Sources/WebIDLToSwift/MergeDeclarations.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ enum DeclarationMerger {
128128
.filter { $0.name == "Exposed" }
129129
.flatMap { $0.rhs?.identifiers ?? [] }
130130
),
131-
exposedToAll: $0.extAttrs.contains { $0.name == "Exposed" && $0.rhs == .wildcard }
131+
exposedToAll: $0.extAttrs.contains { $0.name == "Exposed" && $0.rhs == .wildcard },
132+
global: $0.extAttrs.contains { $0.name == "Global" }
132133
)
133134
},
134135
by: \.name
@@ -138,6 +139,7 @@ enum DeclarationMerger {
138139
partialResult.members += interface.members
139140
partialResult.exposed.formUnion(interface.exposed)
140141
partialResult.exposedToAll = partialResult.exposedToAll || interface.exposedToAll
142+
partialResult.global = partialResult.global || interface.global
141143
}
142144
interface.mixins = includes[interface.name, default: []]
143145
if let decl = interface.members.first(where: { $0 is IDLIterableDeclaration }) as? IDLIterableDeclaration {
@@ -287,6 +289,7 @@ struct MergedInterface: DeclarationFile {
287289
var members: [IDLInterfaceMember]
288290
var exposed: Set<String>
289291
var exposedToAll: Bool
292+
var global: Bool
290293
}
291294

292295
struct Typedefs: DeclarationFile, SwiftRepresentable {

Sources/WebIDLToSwift/WebIDL+SwiftRepresentation.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,25 @@ extension MergedInterface: SwiftRepresentable {
237237
]
238238
let access: SwiftSource = openClasses.contains(name) ? "open" : "public"
239239

240+
let globalAccessor: SwiftSource
241+
if global {
242+
globalAccessor = """
243+
@inlinable public static var global: \(name) {
244+
\(name)(unsafelyWrapping: JSObject.global)
245+
}
246+
"""
247+
} else {
248+
globalAccessor = ""
249+
}
250+
240251
return """
241252
\(access) class \(name): \(sequence: inheritance.map(SwiftSource.init(_:))) {
242253
@inlinable \(access)\(parentClasses.isEmpty ? "" : " override") class var constructor: JSFunction? { \(constructor) }
243254
244255
\(parentClasses.isEmpty ? "public let jsObject: JSObject" : "")
245256
257+
\(globalAccessor)
258+
246259
public required init(unsafelyWrapping jsObject: JSObject) {
247260
\(memberInits.joined(separator: "\n"))
248261
\(parentClasses.isEmpty ? "self.jsObject = jsObject" : "super.init(unsafelyWrapping: jsObject)")

0 commit comments

Comments
 (0)