Skip to content

Commit cd7d4ad

Browse files
authored
Fixes for compatibility with Embedded Swift (#61)
1 parent 51d7fc7 commit cd7d4ad

File tree

10 files changed

+194
-205
lines changed

10 files changed

+194
-205
lines changed

Patches/CSSOM.patch

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1+
diff --git a/Sources/CSSOM/Generated.swift b/Sources/CSSOM/Generated.swift
2+
index 9cd2fb3..7aec5a8 100644
13
--- a/Sources/CSSOM/Generated.swift
24
+++ b/Sources/CSSOM/Generated.swift
3-
@@ -379,7 +379,8 @@ public class CSSColorValue: CSSStyleValue {
4-
super.init(unsafelyWrapping: jsObject)
5-
}
6-
5+
@@ -411,7 +411,7 @@ public class CSSColorValue: CSSStyleValue {
6+
7+
public required init(unsafelyWrapping jsObject: JSObject) { super.init(unsafelyWrapping: jsObject) }
8+
79
- @inlinable override public class func parse(cssText: String) -> CSSColorValue_or_CSSStyleValue {
8-
+ // returns CSSStyleValue | CSSColorValue
9-
+ @inlinable public class func parse(cssText: String) -> CSSStyleValue {
10+
+ @inlinable public class func parse(cssText: String) -> CSSColorValue {
1011
let this = constructor!
1112
return this[Strings.parse].function!(this: this, arguments: [_toJSValue(cssText)]).fromJSValue()!
1213
}
13-
@@ -944,7 +945,7 @@ public class CSSNumericValue: CSSStyleValue {
14+
@@ -947,7 +947,7 @@ public class CSSNumericValue: CSSStyleValue {
1415
return this[Strings.type].function!(this: this, arguments: []).fromJSValue()!
1516
}
16-
17-
- @inlinable override public class func parse(cssText: String) -> Self {
18-
+ @inlinable public class func parse(cssText: String) -> Self {
17+
18+
- @inlinable override public class func parse(cssText: String) -> CSSNumericValue {
19+
+ @inlinable public class func parse(cssText: String) -> CSSNumericValue {
1920
let this = constructor!
2021
return this[Strings.parse].function!(this: this, arguments: [_toJSValue(cssText)]).fromJSValue()!
2122
}
22-
@@ -1974,9 +1975,10 @@ public class StylePropertyMapReadOnly: JSBridgedClass, Sequence {
23+
@@ -2072,9 +2072,10 @@ public class StylePropertyMapReadOnly: JSBridgedClass, Sequence {
2324
ValueIterableIterator(sequence: self)
2425
}
25-
26-
- @inlinable public func get(property: String) -> CSSStyleValue_or_Void {
26+
27+
- @inlinable final public func get(property: String) -> CSSStyleValue_or_Void {
2728
+ // TODO: remove patch once https://github.com/w3c/css-houdini-drafts/issues/1095 is fixed
28-
+ @inlinable public func get(property: String) -> CSSStyleValue? {
29+
+ @inlinable final public func get(property: String) -> CSSStyleValue? {
2930
let this = jsObject
3031
- return this[Strings.get].function!(this: this, arguments: [_toJSValue(property)]).fromJSValue()!
3132
+ return this[Strings.get].function!(this: this, arguments: [_toJSValue(property)]).fromJSValue()
3233
}
33-
34-
@inlinable public func getAll(property: String) -> [CSSStyleValue] {
35-
@@ -2646,58 +2648,6 @@ public enum CSSStyleValue_or_String: JSValueCompatible, Any_CSSStyleValue_or_Str
34+
35+
@inlinable final public func getAll(property: String) -> [CSSStyleValue] {
36+
@@ -2706,49 +2707,6 @@ public enum CSSStyleValue_or_String: JSValueCompatible, Any_CSSStyleValue_or_Str
37+
}
3638
}
3739
}
38-
3940
-public protocol Any_CSSStyleValue_or_Void: ConvertibleToJSValue {}
4041
-extension CSSStyleValue: Any_CSSStyleValue_or_Void {}
4142
-extension Void: Any_CSSStyleValue_or_Void {}
@@ -44,12 +45,11 @@
4445
- case cssStyleValue(CSSStyleValue)
4546
- case void(Void)
4647
-
47-
- init(_ cssStyleValue: CSSStyleValue) {
48+
- public init(_ cssStyleValue: CSSStyleValue) {
4849
- let val: CSSStyleValue_or_Void = .cssStyleValue(cssStyleValue)
4950
- self = val
5051
- }
51-
-
52-
- init(_ void: Void) {
52+
- public init(_ void: Void) {
5353
- let val: CSSStyleValue_or_Void = .void(void)
5454
- self = val
5555
- }
@@ -60,7 +60,6 @@
6060
- default: return nil
6161
- }
6262
- }
63-
-
6463
- public var void: Void? {
6564
- switch self {
6665
- case let .void(void): return void
@@ -69,25 +68,18 @@
6968
- }
7069
-
7170
- public static func construct(from value: JSValue) -> Self? {
72-
- if let cssStyleValue: CSSStyleValue = value.fromJSValue() {
73-
- return .cssStyleValue(cssStyleValue)
74-
- }
75-
- if let void: Void = value.fromJSValue() {
76-
- return .void(void)
77-
- }
71+
- if let cssStyleValue: CSSStyleValue = value.fromJSValue() { return .cssStyleValue(cssStyleValue) }
72+
- if let void: Void = value.fromJSValue() { return .void(void) }
7873
- return nil
7974
- }
8075
-
8176
- public var jsValue: JSValue {
8277
- switch self {
83-
- case let .cssStyleValue(cssStyleValue):
84-
- return cssStyleValue.jsValue
85-
- case let .void(void):
86-
- return void.jsValue
78+
- case let .cssStyleValue(cssStyleValue): return cssStyleValue.jsValue
79+
- case let .void(void): return void.jsValue
8780
- }
8881
- }
8982
-}
90-
-
9183
public protocol Any_CSSUnparsedSegment: ConvertibleToJSValue {}
9284
extension CSSVariableReferenceValue: Any_CSSUnparsedSegment {}
9385
extension String: Any_CSSUnparsedSegment {}

Patches/DOM.patch

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,52 @@
1+
diff --git a/Sources/DOM/Generated.swift b/Sources/DOM/Generated.swift
2+
index 9fa75ba..0339568 100644
13
--- a/Sources/DOM/Generated.swift
24
+++ b/Sources/DOM/Generated.swift
3-
@@ -1007,8 +1007,15 @@ public class BeforeUnloadEvent: Event {
4-
super.init(unsafelyWrapping: jsObject)
5-
}
6-
5+
@@ -1094,6 +1094,17 @@ public class BeforeUnloadEvent: Event {
6+
7+
public required init(unsafelyWrapping jsObject: JSObject) { super.init(unsafelyWrapping: jsObject) }
8+
79
+ @available(*, unavailable)
810
+ override public var returnValue: Bool {
9-
+ get { !_returnValue.wrappedValue.isEmpty }
11+
+ get { !self.returnValueAsString.isEmpty }
1012
+ set {}
1113
+ }
1214
+
13-
@usableFromInline let _returnValue: ReadWriteAttribute<String>
14-
- @inlinable override public var returnValue: String {
1515
+ // renamed because `String` is not compatible with `Bool`
1616
+ @inlinable public var returnValueAsString: String {
17-
get { _returnValue.wrappedValue }
18-
set { _returnValue.wrappedValue = newValue }
19-
}
20-
@@ -7095,7 +7102,8 @@ public class HTMLFormControlsCollection: HTMLCollection {
21-
jsObject[key].fromJSValue()
22-
}
23-
24-
- @inlinable override public func namedItem(name: String) -> Element_or_RadioNodeList? {
25-
+ // `override` removed since the superclass returns a more constrained type `Element`
26-
+ @inlinable func namedItem(name: String) -> Element_or_RadioNodeList? {
27-
let this = jsObject
28-
return this[Strings.namedItem].function!(this: this, arguments: [_toJSValue(name)]).fromJSValue()
29-
}
30-
@@ -17153,7 +17161,6 @@ public class VisibilityStateEntry: PerformanceEntry {
31-
_name = ReadonlyAttribute(jsObject: jsObject, name: Strings.name)
32-
_entryType = ReadonlyAttribute(jsObject: jsObject, name: Strings.entryType)
33-
_startTime = ReadonlyAttribute(jsObject: jsObject, name: Strings.startTime)
34-
- _duration = ReadonlyAttribute(jsObject: jsObject, name: Strings.duration)
35-
super.init(unsafelyWrapping: jsObject)
36-
}
37-
38-
@@ -17166,8 +17173,8 @@ public class VisibilityStateEntry: PerformanceEntry {
39-
@usableFromInline let _startTime: ReadonlyAttribute<DOMHighResTimeStamp>
40-
@inlinable override public var startTime: DOMHighResTimeStamp { _startTime.wrappedValue }
41-
42-
- @usableFromInline let _duration: ReadonlyAttribute<UInt32>
43-
- @inlinable override public var duration: UInt32 { _duration.wrappedValue }
44-
+ // XXX: override of property `duration` removed because the type here is UInt32 but the
45-
+ // type in the superclass is DOMHighResTimestamp (Double).
17+
+ get { self.jsObject[Strings.returnValue].string! }
18+
+ set { self.jsObject[Strings.returnValue] = .string(newValue) }
19+
+ }
4620
}
47-
48-
public class VisualViewport: EventTarget {
49-
@@ -20687,19 +20694,9 @@ public enum CanvasImageSource: JSValueCompatible, Any_CanvasImageSource {
21+
22+
public enum BitrateMode: JSString, JSValueCompatible {
23+
@@ -24231,14 +24242,6 @@ public enum CanvasImageSource: JSValueCompatible, Any_CanvasImageSource {
24+
let val: CanvasImageSource = .htmlOrSVGImageElement(htmlOrSVGImageElement)
5025
self = val
5126
}
52-
53-
- init(_ htmlOrSVGImageElement: HTMLOrSVGImageElement) {
54-
- let val: CanvasImageSource = .htmlOrSVGImageElement(htmlOrSVGImageElement)
55-
- self = val
56-
- }
57-
-
58-
init(_ htmlImageElement: HTMLImageElement) {
27+
- public init(_ htmlImageElement: HTMLImageElement) {
5928
- let val: HTMLOrSVGImageElement = .htmlImageElement(htmlImageElement)
6029
- self = .init(val)
6130
- }
62-
-
63-
- init(_ svgImageElement: SVGImageElement) {
31+
- public init(_ svgImageElement: SVGImageElement) {
6432
- let val: HTMLOrSVGImageElement = .svgImageElement(svgImageElement)
6533
- self = .init(val)
66-
+ let val: CanvasImageSource = .htmlOrSVGImageElement(htmlImageElement)
67-
+ self = val
68-
}
69-
70-
init(_ htmlVideoElement: HTMLVideoElement) {
71-
@@ -21947,18 +21944,8 @@ public enum ImageBitmapSource: JSValueCompatible, Any_ImageBitmapSource {
34+
- }
35+
public init(_ htmlVideoElement: HTMLVideoElement) {
36+
let val: CanvasImageSource = .htmlVideoElement(htmlVideoElement)
37+
self = val
38+
@@ -25289,14 +25292,6 @@ public enum ImageBitmapSource: JSValueCompatible, Any_ImageBitmapSource {
39+
let val: CanvasImageSource = .htmlOrSVGImageElement(htmlOrSVGImageElement)
7240
self = .init(val)
7341
}
74-
75-
- init(_ htmlOrSVGImageElement: HTMLOrSVGImageElement) {
76-
- let val: CanvasImageSource = .htmlOrSVGImageElement(htmlOrSVGImageElement)
77-
- self = .init(val)
78-
- }
79-
-
80-
init(_ htmlImageElement: HTMLImageElement) {
42+
- public init(_ htmlImageElement: HTMLImageElement) {
8143
- let val: HTMLOrSVGImageElement = .htmlImageElement(htmlImageElement)
8244
- self = .init(val)
8345
- }
84-
-
85-
- init(_ svgImageElement: SVGImageElement) {
46+
- public init(_ svgImageElement: SVGImageElement) {
8647
- let val: HTMLOrSVGImageElement = .svgImageElement(svgImageElement)
87-
+ let val: CanvasImageSource = .htmlOrSVGImageElement(htmlImageElement)
48+
- self = .init(val)
49+
- }
50+
public init(_ htmlVideoElement: HTMLVideoElement) {
51+
let val: CanvasImageSource = .htmlVideoElement(htmlVideoElement)
8852
self = .init(val)
89-
}
90-

Patches/SVG.patch

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1+
diff --git a/Sources/SVG/Generated.swift b/Sources/SVG/Generated.swift
2+
index 176385b..bf8f1dd 100644
13
--- a/Sources/SVG/Generated.swift
24
+++ b/Sources/SVG/Generated.swift
3-
@@ -544,14 +544,16 @@ public class SVGElement: Element, GlobalEventHandlers, DocumentAndElementEventHa
4-
@inlinable override public class var constructor: JSFunction? { JSObject.global[Strings.SVGElement].function }
5+
@@ -524,7 +524,9 @@ public class SVGElement: Element, GlobalEventHandlers, SVGElementInstance, HTMLO
56

6-
public required init(unsafelyWrapping jsObject: JSObject) {
7-
- _className = ReadonlyAttribute(jsObject: jsObject, name: Strings.className)
8-
+ _svgClassName = ReadonlyAttribute(jsObject: jsObject, name: Strings.className)
9-
_ownerSVGElement = ReadonlyAttribute(jsObject: jsObject, name: Strings.ownerSVGElement)
10-
_viewportElement = ReadonlyAttribute(jsObject: jsObject, name: Strings.viewportElement)
11-
super.init(unsafelyWrapping: jsObject)
12-
}
7+
public required init(unsafelyWrapping jsObject: JSObject) { super.init(unsafelyWrapping: jsObject) }
138

9+
- @inlinable public var className: SVGAnimatedString { jsObject[Strings.className].fromJSValue()! }
1410
+ // Renamed because superclass has a `className` property of type `String`
1511
+ // NOTE! Accessing `className` on an SVGElement will crash your app
16-
@ReadonlyAttribute
17-
- public var className: SVGAnimatedString
18-
+ public var svgClassName: SVGAnimatedString
12+
+ @inlinable public var svgClassName: SVGAnimatedString { jsObject[Strings.className].fromJSValue()! }
13+
14+
@inlinable public var ownerSVGElement: SVGSVGElement? { jsObject[Strings.ownerSVGElement].fromJSValue() }
1915

20-
@ReadonlyAttribute
21-
public var ownerSVGElement: SVGSVGElement?

Patches/WebAudio.patch

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1+
diff --git a/Sources/WebAudio/Generated.swift b/Sources/WebAudio/Generated.swift
2+
index fff1563..d672d8c 100644
13
--- a/Sources/WebAudio/Generated.swift
24
+++ b/Sources/WebAudio/Generated.swift
3-
@@ -195,7 +195,8 @@ public class AudioBufferSourceNode: AudioScheduledSourceNode {
4-
@ReadWriteAttribute
5-
public var loopEnd: Double
5+
@@ -535,11 +535,11 @@ public class AudioNode: EventTarget {
66

7-
- @inlinable override public func start(when: Double? = nil, offset: Double? = nil, duration: Double? = nil) {
8-
+ // `override` removed since the superclass function has fewer parameters
9-
+ @inlinable func start(when: Double? = nil, offset: Double? = nil, duration: Double? = nil) {
10-
let this = jsObject
11-
_ = this[Strings.start].function!(this: this, arguments: [_toJSValue(when), _toJSValue(offset), _toJSValue(duration)])
12-
}
13-
@@ -492,7 +493,7 @@ public class AudioNode: EventTarget {
14-
super.init(unsafelyWrapping: jsObject)
15-
}
7+
public required init(unsafelyWrapping jsObject: JSObject) { super.init(unsafelyWrapping: jsObject) }
168

17-
- @inlinable public func connect(destinationNode: AudioNode, output: UInt32? = nil, input: UInt32? = nil) -> Self {
18-
+ @discardableResult @inlinable public func connect<NodeType: AudioNode>(destinationNode: NodeType, output: UInt32? = nil, input: UInt32? = nil) -> NodeType {
9+
- @inlinable final public func connect(
10+
- destinationNode: AudioNode,
11+
+ @discardableResult @inlinable public final func connect<NodeType: AudioNode>(
12+
+ destinationNode: NodeType,
13+
output: UInt32? = nil,
14+
input: UInt32? = nil
15+
- ) -> AudioNode {
16+
+ ) -> NodeType {
1917
let this = jsObject
20-
return this[Strings.connect].function!(this: this, arguments: [_toJSValue(destinationNode), _toJSValue(output), _toJSValue(input)]).fromJSValue()!
21-
}
18+
return this[Strings.connect].function!(
19+
this: this,

Sources/WebIDLToSwift/IDLBuilder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import WebIDL
33

44
enum IDLBuilder {
55
static let basicDependencies = ["ECMAScript", "JavaScriptKit"]
6-
static let optionalDependencies = ["JavaScriptEventLoop"]
6+
static let optionalDependencies = ["JavaScriptEventLoop", "_Concurrency"]
77

88
static let preamble = """
99
// This file was auto-generated by WebIDLToSwift. DO NOT EDIT!
@@ -44,7 +44,7 @@ enum IDLBuilder {
4444
}
4545

4646
let formedPreamble = preamble + (optionalDependencies.map { """
47-
#if canImport\($0)
47+
#if canImport(\($0))
4848
import \($0)
4949
#endif
5050
""" } + dependencies.map { "import \($0)" }).joined(separator: "\n")

Sources/WebIDLToSwift/Module.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ let domModule = Module(
2727
"css-pseudo",
2828
"geometry",
2929
"cssom-view",
30+
"css-view-transitions",
3031
"hr-time",
3132
"FileAPI",
3233
"xhr",
@@ -42,6 +43,8 @@ let domModule = Module(
4243
"performance-timeline",
4344
"permissions",
4445
"mathml-core",
46+
"trusted-types",
47+
"urlpattern",
4548
],
4649
dependencies: ["WebAPIBase"]
4750
)

Sources/WebIDLToSwift/PackageManifest.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// swiftlint:disable function_body_length
22
func generateManifest(_ modules: [Module]) -> String {
33
#"""
4-
// swift-tools-version:5.5
4+
// swift-tools-version: 6.1
55
// The swift-tools-version declares the minimum version of Swift required to build this package.
66
77
import PackageDescription
88
99
let package = Package(
1010
name: "WebAPIKit",
11-
platforms: [.macOS(.v10_13)],
11+
platforms: [.macOS(.v10_15)],
1212
products: [
1313
.executable(
1414
name: "WebAPIKitDemo",
@@ -29,7 +29,7 @@ func generateManifest(_ modules: [Module]) -> String {
2929
dependencies: [
3030
.package(
3131
url: "https://github.com/swiftwasm/JavaScriptKit.git",
32-
.upToNextMajor(from: "0.16.0")
32+
.upToNextMajor(from: "0.29.0")
3333
),
3434
],
3535
targets: [
@@ -62,7 +62,8 @@ func generateManifest(_ modules: [Module]) -> String {
6262
name: "WebAPIKitTests",
6363
dependencies: ["DOM"]
6464
),
65-
]
65+
],
66+
swiftLanguageModes: [.v5]
6667
)
6768
"""#
6869
}

Sources/WebIDLToSwift/Shell.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
enum Shell {
44
static func format(source: String) {
55
print("Formatting generated Swift files...")
6-
run(executable: "swiftformat", arguments: ["--swiftversion", "5.5", source])
6+
run(executable: "swift", arguments: ["format", "format", "--parallel", "--in-place", source])
77
}
88

99
private static let projectRoot = URL(fileURLWithPath: #file)

0 commit comments

Comments
 (0)