Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to JavaScriptKit 0.9, add Global helpers #3

Merged
merged 2 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"pins": [
{
"package": "JavaScriptKit",
"repositoryURL": "https://github.com/j-f1/forked-JavaScriptKit.git",
"repositoryURL": "https://github.com/swiftwasm/JavaScriptKit.git",
"state": {
"branch": "4429d88",
"revision": "4429d8893f197df5651a0e628e0b78701f19fcf3",
"version": null
"branch": null,
"revision": "b7a02434c6e973c08c3fd5069105ef44dd82b891",
"version": "0.9.0"
}
}
]
Expand Down
12 changes: 11 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import PackageDescription
let package = Package(
name: "DOMKit",
products: [
.executable(
name: "DOMKitDemo",
targets: ["DOMKitDemo"]),
.library(
name: "DOMKit",
targets: ["DOMKit"]),
],
dependencies: [
.package(name: "JavaScriptKit", url: "https://github.com/j-f1/forked-JavaScriptKit.git", .revision("4429d88")),
.package(
name: "JavaScriptKit",
url: "https://github.com/swiftwasm/JavaScriptKit.git",
.upToNextMinor(from: "0.9.0")),
],
targets: [
.target(
name: "DOMKitDemo",
dependencies: ["DOMKit"]
),
.target(
name: "DOMKit",
dependencies: ["JavaScriptKit"]),
Expand Down
4 changes: 2 additions & 2 deletions Sources/DOMKit/ECMAScript/ArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ArrayBuffer: JSBridgedClass {
self.init(unsafelyWrapping: Self.constructor.new( length))
}

public static func isView(_ object: JSValueCodable) -> Bool {
return JSObject.global.ArrayBuffer.object!.isView!(object).fromJSValue()!
public static func isView(_ object: JSValueCompatible) -> Bool {
JSObject.global.ArrayBuffer.object!.isView!(object).fromJSValue()!
}
}
32 changes: 19 additions & 13 deletions Sources/DOMKit/ECMAScript/Support.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@

import JavaScriptKit

public class Promise<Type>: JSBridgedClass {

public static var constructor: JSFunction { JSObject.global.Promise.function! }

public let jsObject: JSObject
public extension Window {
public var document: Document { Document(unsafelyWrapping: jsObject.document.object!) }
}

public required init(unsafelyWrapping jsObject: JSObject) {
public extension Document {
var body: HTMLElement {
.init(unsafelyWrapping: jsObject.body.object!)
}
}
Comment on lines +11 to +15
Copy link
Member

@j-f1 j-f1 Dec 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is defined as a property on the Document object by the spec (https://html.spec.whatwg.org/#the-document-object:dom-document-body) but the dom.webidl doesn’t reference it so it isn’t properly generated. I mentioned parsing the HTML spec as a stumbling block earlier but may give it a go.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


self.jsObject = jsObject
public extension HTMLElement {
convenience init?(from element: Element) {
self.init(from: .object(element.jsObject))
}
}

public let globalThis = Window(from: JSObject.global.jsValue())!

public class ReadableStream: JSBridgedClass {

public static var constructor: JSFunction { JSObject.global.ReadableStream.function! }
Expand All @@ -32,7 +38,7 @@ public class ReadableStream: JSBridgedClass {
}
}

@propertyWrapper public struct ClosureHandler<ArgumentType: JSValueCodable, ReturnType: JSValueCodable> {
@propertyWrapper public struct ClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {

let jsObject: JSObject
let name: String
Expand All @@ -52,7 +58,7 @@ public class ReadableStream: JSBridgedClass {
}
}

@propertyWrapper public struct OptionalClosureHandler<ArgumentType: JSValueCodable, ReturnType: JSValueCodable> {
@propertyWrapper public struct OptionalClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {

let jsObject: JSObject
let name: String
Expand All @@ -79,7 +85,7 @@ public class ReadableStream: JSBridgedClass {
}
}

@propertyWrapper public struct ReadWriteAttribute<Wrapped: JSValueCodable> {
@propertyWrapper public struct ReadWriteAttribute<Wrapped: JSValueCompatible> {

let jsObject: JSObject
let name: String
Expand All @@ -99,7 +105,7 @@ public class ReadableStream: JSBridgedClass {
}
}

@propertyWrapper public struct ReadonlyAttribute<Wrapped: JSValueConstructible> {
@propertyWrapper public struct ReadonlyAttribute<Wrapped: ConstructibleFromJSValue> {

let jsObject: JSObject
let name: String
Expand All @@ -116,7 +122,7 @@ public class ReadableStream: JSBridgedClass {
}
}

public class ValueIterableIterator<SequenceType: JSBridgedClass & Sequence>: IteratorProtocol where SequenceType.Element: JSValueConstructible {
public class ValueIterableIterator<SequenceType: JSBridgedClass & Sequence>: IteratorProtocol where SequenceType.Element: ConstructibleFromJSValue {

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

public class PairIterableIterator<SequenceType: JSBridgedClass & KeyValueSequence>: IteratorProtocol where SequenceType.Value: JSValueConstructible {
public class PairIterableIterator<SequenceType: JSBridgedClass & KeyValueSequence>: IteratorProtocol where SequenceType.Value: ConstructibleFromJSValue {

private let iterator: JSObject
private let sequence: SequenceType
Expand Down
4 changes: 2 additions & 2 deletions Sources/DOMKit/WebIDL/Blob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public class Blob: JSBridgedClass {
return jsObject.stream!().fromJSValue()!
}

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

public func arrayBuffer() -> Promise<ArrayBuffer> {
public func arrayBuffer() -> JSPromise<ArrayBuffer, JSError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this change in webidl2swift and re-run it to regenerate these files?

Copy link
Contributor Author

@MaxDesiatov MaxDesiatov Dec 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can fix this in webidl2swift, but I'm not sure if we can rely on it long-term. It's great to provide initial scaffolding, but there's also some generated junk to clean up and manual handling of JSClosure is still required. I've done some of that manual work in #4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webidl2swift changes are now available for review in Apodini/webidl2swift#10.

return jsObject.arrayBuffer!().fromJSValue()!
}
}
10 changes: 10 additions & 0 deletions Sources/DOMKitDemo/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import JavaScriptKit
import DOMKit

let document = global.document

let header = HTMLElement(from: document.createElement(localName: "h1"))!
header.innerText = "Hello World!"
_ = document.body.appendChild(node: header)

console.log(data: "Hello, world!")
2 changes: 1 addition & 1 deletion Tests/DOMKitTests/DOMKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import JavaScriptKit

final class DOMKitTests: XCTestCase {
func testExample() {
let document = DOMKit.Document(from: JSObject.global.document)!
let document = globalThis.document
let button = document.createElement(localName: "button")
button.textContent = "Hello, world"
button.addEventListener(type: "click", callback: { event in
Expand Down