Skip to content

Commit 44d49bc

Browse files
authored
Update to JavaScriptKit 0.9, add Global helpers (#3)
Updates JavaScriptKit dependency to upstream 0.9 instead of using a fork as previously. I've updated a few helpers to use non-deprecated names and also added `Global` type and `public let global = Global()` with a few extensions on DOM types which for some reason don't seem to be declared in WebIDL. Additional `DOMKitDemo` target and product are set up for easier manual testing. * Update to JavaScriptKit 0.9, add `Global` helpers * Use `globalThis` instead of `global` # Conflicts: # Tests/DOMKitTests/DOMKitTests.swift
1 parent c62aafd commit 44d49bc

File tree

7 files changed

+49
-23
lines changed

7 files changed

+49
-23
lines changed

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,24 @@ import PackageDescription
66
let package = Package(
77
name: "DOMKit",
88
products: [
9+
.executable(
10+
name: "DOMKitDemo",
11+
targets: ["DOMKitDemo"]),
912
.library(
1013
name: "DOMKit",
1114
targets: ["DOMKit"]),
1215
],
1316
dependencies: [
14-
.package(name: "JavaScriptKit", url: "https://github.com/j-f1/forked-JavaScriptKit.git", .revision("4429d88")),
17+
.package(
18+
name: "JavaScriptKit",
19+
url: "https://github.com/swiftwasm/JavaScriptKit.git",
20+
.upToNextMinor(from: "0.9.0")),
1521
],
1622
targets: [
23+
.target(
24+
name: "DOMKitDemo",
25+
dependencies: ["DOMKit"]
26+
),
1727
.target(
1828
name: "DOMKit",
1929
dependencies: ["JavaScriptKit"]),

Sources/DOMKit/ECMAScript/ArrayBuffer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ArrayBuffer: JSBridgedClass {
2929
self.init(unsafelyWrapping: Self.constructor.new( length))
3030
}
3131

32-
public static func isView(_ object: JSValueCodable) -> Bool {
33-
return JSObject.global.ArrayBuffer.object!.isView!(object).fromJSValue()!
32+
public static func isView(_ object: JSValueCompatible) -> Bool {
33+
JSObject.global.ArrayBuffer.object!.isView!(object).fromJSValue()!
3434
}
3535
}

Sources/DOMKit/ECMAScript/Support.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44

55
import JavaScriptKit
66

7-
public class Promise<Type>: JSBridgedClass {
8-
9-
public static var constructor: JSFunction { JSObject.global.Promise.function! }
10-
11-
public let jsObject: JSObject
7+
public extension Window {
8+
public var document: Document { Document(unsafelyWrapping: jsObject.document.object!) }
9+
}
1210

13-
public required init(unsafelyWrapping jsObject: JSObject) {
11+
public extension Document {
12+
var body: HTMLElement {
13+
.init(unsafelyWrapping: jsObject.body.object!)
14+
}
15+
}
1416

15-
self.jsObject = jsObject
17+
public extension HTMLElement {
18+
convenience init?(from element: Element) {
19+
self.init(from: .object(element.jsObject))
1620
}
1721
}
1822

23+
public let globalThis = Window(from: JSObject.global.jsValue())!
24+
1925
public class ReadableStream: JSBridgedClass {
2026

2127
public static var constructor: JSFunction { JSObject.global.ReadableStream.function! }
@@ -32,7 +38,7 @@ public class ReadableStream: JSBridgedClass {
3238
}
3339
}
3440

35-
@propertyWrapper public struct ClosureHandler<ArgumentType: JSValueCodable, ReturnType: JSValueCodable> {
41+
@propertyWrapper public struct ClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {
3642

3743
let jsObject: JSObject
3844
let name: String
@@ -52,7 +58,7 @@ public class ReadableStream: JSBridgedClass {
5258
}
5359
}
5460

55-
@propertyWrapper public struct OptionalClosureHandler<ArgumentType: JSValueCodable, ReturnType: JSValueCodable> {
61+
@propertyWrapper public struct OptionalClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {
5662

5763
let jsObject: JSObject
5864
let name: String
@@ -79,7 +85,7 @@ public class ReadableStream: JSBridgedClass {
7985
}
8086
}
8187

82-
@propertyWrapper public struct ReadWriteAttribute<Wrapped: JSValueCodable> {
88+
@propertyWrapper public struct ReadWriteAttribute<Wrapped: JSValueCompatible> {
8389

8490
let jsObject: JSObject
8591
let name: String
@@ -99,7 +105,7 @@ public class ReadableStream: JSBridgedClass {
99105
}
100106
}
101107

102-
@propertyWrapper public struct ReadonlyAttribute<Wrapped: JSValueConstructible> {
108+
@propertyWrapper public struct ReadonlyAttribute<Wrapped: ConstructibleFromJSValue> {
103109

104110
let jsObject: JSObject
105111
let name: String
@@ -116,7 +122,7 @@ public class ReadableStream: JSBridgedClass {
116122
}
117123
}
118124

119-
public class ValueIterableIterator<SequenceType: JSBridgedClass & Sequence>: IteratorProtocol where SequenceType.Element: JSValueConstructible {
125+
public class ValueIterableIterator<SequenceType: JSBridgedClass & Sequence>: IteratorProtocol where SequenceType.Element: ConstructibleFromJSValue {
120126

121127
private var index: Int = 0
122128
private let sequence: SequenceType
@@ -142,7 +148,7 @@ public protocol KeyValueSequence: Sequence where Element == (String, Value) {
142148
associatedtype Value
143149
}
144150

145-
public class PairIterableIterator<SequenceType: JSBridgedClass & KeyValueSequence>: IteratorProtocol where SequenceType.Value: JSValueConstructible {
151+
public class PairIterableIterator<SequenceType: JSBridgedClass & KeyValueSequence>: IteratorProtocol where SequenceType.Value: ConstructibleFromJSValue {
146152

147153
private let iterator: JSObject
148154
private let sequence: SequenceType

Sources/DOMKit/WebIDL/Blob.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public class Blob: JSBridgedClass {
5050
return jsObject.stream!().fromJSValue()!
5151
}
5252

53-
public func text() -> Promise<String> {
53+
public func text() -> JSPromise<String, JSError> {
5454
return jsObject.text!().fromJSValue()!
5555
}
5656

57-
public func arrayBuffer() -> Promise<ArrayBuffer> {
57+
public func arrayBuffer() -> JSPromise<ArrayBuffer, JSError> {
5858
return jsObject.arrayBuffer!().fromJSValue()!
5959
}
6060
}

Sources/DOMKitDemo/main.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import JavaScriptKit
2+
import DOMKit
3+
4+
let document = global.document
5+
6+
let header = HTMLElement(from: document.createElement(localName: "h1"))!
7+
header.innerText = "Hello World!"
8+
_ = document.body.appendChild(node: header)
9+
10+
console.log(data: "Hello, world!")

Tests/DOMKitTests/DOMKitTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import JavaScriptKit
44

55
final class DOMKitTests: XCTestCase {
66
func testExample() {
7-
let document = DOMKit.Document(from: JSObject.global.document)!
7+
let document = globalThis.document
88
let button = document.createElement(localName: "button")
99
button.textContent = "Hello, world"
1010
button.addEventListener(type: "click", callback: { event in

0 commit comments

Comments
 (0)