Skip to content

Commit 44739dc

Browse files
committed
Update to JavaScriptKit 0.9, add Global helpers
1 parent 0ab2b22 commit 44739dc

File tree

6 files changed

+52
-21
lines changed

6 files changed

+52
-21
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: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@
44

55
import JavaScriptKit
66

7-
public class Promise<Type>: JSBridgedClass {
7+
public class Global {
8+
public let jsObject = JSObject.global
9+
public let document: Document
810

9-
public static var constructor: JSFunction { JSObject.global.Promise.function! }
10-
11-
public let jsObject: JSObject
11+
init() {
12+
document = Document(unsafelyWrapping: jsObject.document.object!)
13+
}
14+
}
1215

13-
public required init(unsafelyWrapping jsObject: JSObject) {
16+
public extension Document {
17+
var body: HTMLElement {
18+
.init(unsafelyWrapping: jsObject.body.object!)
19+
}
20+
}
1421

15-
self.jsObject = jsObject
22+
public extension HTMLElement {
23+
convenience init?(from element: Element) {
24+
self.init(from: .object(element.jsObject))
1625
}
1726
}
1827

28+
public let global = Global()
29+
1930
public class ReadableStream: JSBridgedClass {
2031

2132
public static var constructor: JSFunction { JSObject.global.ReadableStream.function! }
@@ -32,7 +43,7 @@ public class ReadableStream: JSBridgedClass {
3243
}
3344
}
3445

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

3748
let jsObject: JSObject
3849
let name: String
@@ -52,7 +63,7 @@ public class ReadableStream: JSBridgedClass {
5263
}
5364
}
5465

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

5768
let jsObject: JSObject
5869
let name: String
@@ -79,7 +90,7 @@ public class ReadableStream: JSBridgedClass {
7990
}
8091
}
8192

82-
@propertyWrapper public struct ReadWriteAttribute<Wrapped: JSValueCodable> {
93+
@propertyWrapper public struct ReadWriteAttribute<Wrapped: JSValueCompatible> {
8394

8495
let jsObject: JSObject
8596
let name: String
@@ -99,7 +110,7 @@ public class ReadableStream: JSBridgedClass {
99110
}
100111
}
101112

102-
@propertyWrapper public struct ReadonlyAttribute<Wrapped: JSValueConstructible> {
113+
@propertyWrapper public struct ReadonlyAttribute<Wrapped: ConstructibleFromJSValue> {
103114

104115
let jsObject: JSObject
105116
let name: String
@@ -116,7 +127,7 @@ public class ReadableStream: JSBridgedClass {
116127
}
117128
}
118129

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

121132
private var index: Int = 0
122133
private let sequence: SequenceType
@@ -142,7 +153,7 @@ public protocol KeyValueSequence: Sequence where Element == (String, Value) {
142153
associatedtype Value
143154
}
144155

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

147158
private let iterator: JSObject
148159
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!")

0 commit comments

Comments
 (0)