Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build and test

on:
pull_request:
push:
branches: [main]

jobs:
macos_test:
runs-on: macos-11.0

steps:
- uses: actions/checkout@v2
- name: Run the test suite on macOS
shell: bash
run: |
set -ex
sudo xcode-select --switch /Applications/Xcode_12.3.app/Contents/Developer/

brew install swiftwasm/tap/carton

carton test --environment defaultBrowser
8 changes: 4 additions & 4 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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()!
}
}
59 changes: 28 additions & 31 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!)
}
}

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,34 +38,20 @@ public class ReadableStream: JSBridgedClass {
}
}

@propertyWrapper public struct ClosureHandler<ArgumentType: JSValueCodable, ReturnType: JSValueCodable> {
@propertyWrapper public final class OptionalClosureHandler<ArgumentType, ReturnType>
where ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible {

let jsObject: JSObject
let name: String
var closure: JSClosure?

public init(jsObject: JSObject, name: String) {
self.jsObject = jsObject
self.name = name
}

public var wrappedValue: (ArgumentType) -> ReturnType {
get {
{ arg in jsObject[name]!(arg).fromJSValue()! }
}
set {
jsObject[name] = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }.jsValue()
}
}
}

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

let jsObject: JSObject
let name: String

public init(jsObject: JSObject, name: String) {
self.jsObject = jsObject
self.name = name
deinit {
closure?.release()
}

public var wrappedValue: ((ArgumentType) -> ReturnType)? {
Expand All @@ -70,16 +62,21 @@ public class ReadableStream: JSBridgedClass {
return { function($0.jsValue()).fromJSValue()! }
}
set {
if let closure = closure {
closure.release()
}
if let newValue = newValue {
jsObject[name] = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }.jsValue()
let closure = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }
jsObject[name] = closure.jsValue()
self.closure = closure
} else {
jsObject[name] = .null
}
}
}
}

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

let jsObject: JSObject
let name: String
Expand All @@ -99,7 +96,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 +113,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 +139,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/AddEventListenerOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public struct AddEventListenerOptions: ExpressibleByDictionaryLiteral, JSBridged

private let dictionary: [String: JSValue]

public init(uniqueKeysWithValues elements: [(Key, JSValueConvertible)]) {
public init(uniqueKeysWithValues elements: [(Key, ConvertibleToJSValue)]) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

public init(dictionaryLiteral elements: (Key, JSValueConvertible)...) {
public init(dictionaryLiteral elements: (Key, ConvertibleToJSValue)...) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

Expand Down
16 changes: 0 additions & 16 deletions Sources/DOMKit/WebIDL/AnyDocumentAndElementEventHandlers.swift

This file was deleted.

16 changes: 0 additions & 16 deletions Sources/DOMKit/WebIDL/AnyGlobalEventHandlers.swift

This file was deleted.

56 changes: 55 additions & 1 deletion Sources/DOMKit/WebIDL/ArrayBufferView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,58 @@

import JavaScriptKit

public typealias ArrayBufferView = Int8ArrayOrInt16ArrayOrInt32ArrayOrUint8ArrayOrUint16ArrayOrUint32ArrayOrUint8ClampedArrayOrFloat32ArrayOrFloat64ArrayOrDataView
public enum ArrayBufferView: JSBridgedType {
case int8Array(JSTypedArray<Int8>)
case int16Array(JSTypedArray<Int16>)
case int32Array(JSTypedArray<Int32>)
case uint8Array(JSTypedArray<UInt8>)
case uint16Array(JSTypedArray<UInt16>)
case uint32Array(JSTypedArray<UInt32>)
case uint8ClampedArray(JSTypedArray<UInt8>)
case float32Array(JSTypedArray<Float>)
case float64Array(JSTypedArray<Double>)
case dataView(DataView)

public init?(from value: JSValue) {
if let decoded: JSTypedArray<Int8> = value.fromJSValue() {
self = .int8Array(decoded)
} else if let decoded: JSTypedArray<Int16> = value.fromJSValue() {
self = .int16Array(decoded)
} else if let decoded: JSTypedArray<Int32> = value.fromJSValue() {
self = .int32Array(decoded)
} else if let decoded: JSTypedArray<UInt8> = value.fromJSValue() {
self = .uint8Array(decoded)
} else if let decoded: JSTypedArray<UInt16> = value.fromJSValue() {
self = .uint16Array(decoded)
} else if let decoded: JSTypedArray<UInt32> = value.fromJSValue() {
self = .uint32Array(decoded)
} else if let decoded: JSTypedArray<UInt8> = value.fromJSValue() {
self = .uint8ClampedArray(decoded)
} else if let decoded: JSTypedArray<Float> = value.fromJSValue() {
self = .float32Array(decoded)
} else if let decoded: JSTypedArray<Double> = value.fromJSValue() {
self = .float64Array(decoded)
} else if let decoded: DataView = value.fromJSValue() {
self = .dataView(decoded)
} else {
return nil
}
}

public var value: JSValue { jsValue() }

public func jsValue() -> JSValue {
switch self {
case let .int8Array(v): return v.jsValue()
case let .int16Array(v): return v.jsValue()
case let .int32Array(v): return v.jsValue()
case let .uint8Array(v): return v.jsValue()
case let .uint16Array(v): return v.jsValue()
case let .uint32Array(v): return v.jsValue()
case let .uint8ClampedArray(v): return v.jsValue()
case let .float32Array(v): return v.jsValue()
case let .float64Array(v): return v.jsValue()
case let .dataView(v): return v.jsValue()
}
}
}
4 changes: 2 additions & 2 deletions Sources/DOMKit/WebIDL/AssignedNodesOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public struct AssignedNodesOptions: ExpressibleByDictionaryLiteral, JSBridgedTyp

private let dictionary: [String: JSValue]

public init(uniqueKeysWithValues elements: [(Key, JSValueConvertible)]) {
public init(uniqueKeysWithValues elements: [(Key, ConvertibleToJSValue)]) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

public init(dictionaryLiteral elements: (Key, JSValueConvertible)...) {
public init(dictionaryLiteral elements: (Key, ConvertibleToJSValue)...) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

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> {
return jsObject.arrayBuffer!().fromJSValue()!
}
}
4 changes: 2 additions & 2 deletions Sources/DOMKit/WebIDL/BlobPropertyBag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public struct BlobPropertyBag: ExpressibleByDictionaryLiteral, JSBridgedType {

private let dictionary: [String: JSValue]

public init(uniqueKeysWithValues elements: [(Key, JSValueConvertible)]) {
public init(uniqueKeysWithValues elements: [(Key, ConvertibleToJSValue)]) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

public init(dictionaryLiteral elements: (Key, JSValueConvertible)...) {
public init(dictionaryLiteral elements: (Key, ConvertibleToJSValue)...) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/DOMKit/WebIDL/CustomEventInit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public struct CustomEventInit: ExpressibleByDictionaryLiteral, JSBridgedType {

private let dictionary: [String: JSValue]

public init(uniqueKeysWithValues elements: [(Key, JSValueConvertible)]) {
public init(uniqueKeysWithValues elements: [(Key, ConvertibleToJSValue)]) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

public init(dictionaryLiteral elements: (Key, JSValueConvertible)...) {
public init(dictionaryLiteral elements: (Key, ConvertibleToJSValue)...) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/DOMKit/WebIDL/ElementCreationOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public struct ElementCreationOptions: ExpressibleByDictionaryLiteral, JSBridgedT

private let dictionary: [String: JSValue]

public init(uniqueKeysWithValues elements: [(Key, JSValueConvertible)]) {
public init(uniqueKeysWithValues elements: [(Key, ConvertibleToJSValue)]) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

public init(dictionaryLiteral elements: (Key, JSValueConvertible)...) {
public init(dictionaryLiteral elements: (Key, ConvertibleToJSValue)...) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/DOMKit/WebIDL/EndingType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import JavaScriptKit

public enum EndingType: String, JSValueCodable {
public enum EndingType: String, JSValueCompatible {
public static func construct(from jsValue: JSValue) -> EndingType? {
if let string = jsValue.string,
let value = EndingType(rawValue: string)
Expand Down
4 changes: 2 additions & 2 deletions Sources/DOMKit/WebIDL/EventInit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public struct EventInit: ExpressibleByDictionaryLiteral, JSBridgedType {

private let dictionary: [String: JSValue]

public init(uniqueKeysWithValues elements: [(Key, JSValueConvertible)]) {
public init(uniqueKeysWithValues elements: [(Key, ConvertibleToJSValue)]) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

public init(dictionaryLiteral elements: (Key, JSValueConvertible)...) {
public init(dictionaryLiteral elements: (Key, ConvertibleToJSValue)...) {
dictionary = Dictionary(uniqueKeysWithValues: elements.map { ($0.0.rawValue, $0.1.jsValue()) })
}

Expand Down
Loading