Skip to content

Commit e4b50a4

Browse files
authored
Cleanup unused code, fix event handlers crash (#4)
Some weird names and typealiases were cleaned up (I don't think that `Int8ArrayOrInt16ArrayOrInt32ArrayOrUint8ArrayOrUint16ArrayOrUint32ArrayOrUint8ClampedArrayOrFloat32ArrayOrFloat64ArrayOrDataView` or `typealias VoidFunction = (() -> Void)` could be useful in any way). `GlobalEventHandlers` protocol which was only inherited by a single concrete type was removed with its extension becoming the extension of the corresponding class. I've also cleaned up event handlers to retain and release closures when needed, which makes our single basic test pass in browsers. Basic CI config to verify that is added here. A few other uses of `JSClosure` were updated accordingly to fix memory management. Unused `ClosureHandler` property wrapper is removed, leaving only `OptionalClosureHandler`. * Update to JavaScriptKit 0.9, add `Global` helpers * Cleanup unused code, fix event handlers crash * Add GitHub Actions workflow, assert in the test * Remove ClosureHandler, fix OptionalClosureHandler * Fix `MutationObserver` build error * Use `globalThis` instead of `global`
2 parents 44d49bc + 12bc9b7 commit e4b50a4

31 files changed

+668
-325
lines changed

.github/workflows/test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Build and test
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
macos_test:
10+
runs-on: macos-11.0
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Run the test suite on macOS
15+
shell: bash
16+
run: |
17+
set -ex
18+
sudo xcode-select --switch /Applications/Xcode_12.3.app/Contents/Developer/
19+
20+
brew install swiftwasm/tap/carton
21+
22+
carton test --environment defaultBrowser

Sources/DOMKit/ECMAScript/Support.swift

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,19 @@ public class ReadableStream: JSBridgedClass {
3838
}
3939
}
4040

41-
@propertyWrapper public struct ClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {
42-
41+
@propertyWrapper public final class OptionalClosureHandler<ArgumentType, ReturnType>
42+
where ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible {
4343
let jsObject: JSObject
4444
let name: String
45+
var closure: JSClosure?
4546

4647
public init(jsObject: JSObject, name: String) {
4748
self.jsObject = jsObject
4849
self.name = name
4950
}
5051

51-
public var wrappedValue: (ArgumentType) -> ReturnType {
52-
get {
53-
{ arg in jsObject[name]!(arg).fromJSValue()! }
54-
}
55-
set {
56-
jsObject[name] = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }.jsValue()
57-
}
58-
}
59-
}
60-
61-
@propertyWrapper public struct OptionalClosureHandler<ArgumentType: JSValueCompatible, ReturnType: JSValueCompatible> {
62-
63-
let jsObject: JSObject
64-
let name: String
65-
66-
public init(jsObject: JSObject, name: String) {
67-
self.jsObject = jsObject
68-
self.name = name
52+
deinit {
53+
closure?.release()
6954
}
7055

7156
public var wrappedValue: ((ArgumentType) -> ReturnType)? {
@@ -76,8 +61,13 @@ public class ReadableStream: JSBridgedClass {
7661
return { function($0.jsValue()).fromJSValue()! }
7762
}
7863
set {
64+
if let closure = closure {
65+
closure.release()
66+
}
7967
if let newValue = newValue {
80-
jsObject[name] = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }.jsValue()
68+
let closure = JSClosure { newValue($0[0].fromJSValue()!).jsValue() }
69+
jsObject[name] = closure.jsValue()
70+
self.closure = closure
8171
} else {
8272
jsObject[name] = .null
8373
}

Sources/DOMKit/WebIDL/AddEventListenerOptions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public struct AddEventListenerOptions: ExpressibleByDictionaryLiteral, JSBridged
1212

1313
private let dictionary: [String: JSValue]
1414

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

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

Sources/DOMKit/WebIDL/AnyDocumentAndElementEventHandlers.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

Sources/DOMKit/WebIDL/AnyGlobalEventHandlers.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

Sources/DOMKit/WebIDL/ArrayBufferView.swift

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,58 @@
55

66
import JavaScriptKit
77

8-
public typealias ArrayBufferView = Int8ArrayOrInt16ArrayOrInt32ArrayOrUint8ArrayOrUint16ArrayOrUint32ArrayOrUint8ClampedArrayOrFloat32ArrayOrFloat64ArrayOrDataView
8+
public enum ArrayBufferView: JSBridgedType {
9+
case int8Array(JSTypedArray<Int8>)
10+
case int16Array(JSTypedArray<Int16>)
11+
case int32Array(JSTypedArray<Int32>)
12+
case uint8Array(JSTypedArray<UInt8>)
13+
case uint16Array(JSTypedArray<UInt16>)
14+
case uint32Array(JSTypedArray<UInt32>)
15+
case uint8ClampedArray(JSTypedArray<UInt8>)
16+
case float32Array(JSTypedArray<Float>)
17+
case float64Array(JSTypedArray<Double>)
18+
case dataView(DataView)
19+
20+
public init?(from value: JSValue) {
21+
if let decoded: JSTypedArray<Int8> = value.fromJSValue() {
22+
self = .int8Array(decoded)
23+
} else if let decoded: JSTypedArray<Int16> = value.fromJSValue() {
24+
self = .int16Array(decoded)
25+
} else if let decoded: JSTypedArray<Int32> = value.fromJSValue() {
26+
self = .int32Array(decoded)
27+
} else if let decoded: JSTypedArray<UInt8> = value.fromJSValue() {
28+
self = .uint8Array(decoded)
29+
} else if let decoded: JSTypedArray<UInt16> = value.fromJSValue() {
30+
self = .uint16Array(decoded)
31+
} else if let decoded: JSTypedArray<UInt32> = value.fromJSValue() {
32+
self = .uint32Array(decoded)
33+
} else if let decoded: JSTypedArray<UInt8> = value.fromJSValue() {
34+
self = .uint8ClampedArray(decoded)
35+
} else if let decoded: JSTypedArray<Float> = value.fromJSValue() {
36+
self = .float32Array(decoded)
37+
} else if let decoded: JSTypedArray<Double> = value.fromJSValue() {
38+
self = .float64Array(decoded)
39+
} else if let decoded: DataView = value.fromJSValue() {
40+
self = .dataView(decoded)
41+
} else {
42+
return nil
43+
}
44+
}
45+
46+
public var value: JSValue { jsValue() }
47+
48+
public func jsValue() -> JSValue {
49+
switch self {
50+
case let .int8Array(v): return v.jsValue()
51+
case let .int16Array(v): return v.jsValue()
52+
case let .int32Array(v): return v.jsValue()
53+
case let .uint8Array(v): return v.jsValue()
54+
case let .uint16Array(v): return v.jsValue()
55+
case let .uint32Array(v): return v.jsValue()
56+
case let .uint8ClampedArray(v): return v.jsValue()
57+
case let .float32Array(v): return v.jsValue()
58+
case let .float64Array(v): return v.jsValue()
59+
case let .dataView(v): return v.jsValue()
60+
}
61+
}
62+
}

Sources/DOMKit/WebIDL/AssignedNodesOptions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public struct AssignedNodesOptions: ExpressibleByDictionaryLiteral, JSBridgedTyp
1212

1313
private let dictionary: [String: JSValue]
1414

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

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

Sources/DOMKit/WebIDL/BlobPropertyBag.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public struct BlobPropertyBag: ExpressibleByDictionaryLiteral, JSBridgedType {
1212

1313
private let dictionary: [String: JSValue]
1414

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

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

Sources/DOMKit/WebIDL/CustomEventInit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public struct CustomEventInit: ExpressibleByDictionaryLiteral, JSBridgedType {
1212

1313
private let dictionary: [String: JSValue]
1414

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

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

Sources/DOMKit/WebIDL/ElementCreationOptions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public struct ElementCreationOptions: ExpressibleByDictionaryLiteral, JSBridgedT
1212

1313
private let dictionary: [String: JSValue]
1414

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

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

0 commit comments

Comments
 (0)