Skip to content

Commit 95d0c4c

Browse files
authored
Updates for DOMKit (#174)
1 parent 081784b commit 95d0c4c

File tree

13 files changed

+108
-80
lines changed

13 files changed

+108
-80
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,14 @@ try test("Call Function With This") {
356356

357357
try test("Object Conversion") {
358358
let array1 = [1, 2, 3]
359-
let jsArray1 = array1.jsValue().object!
359+
let jsArray1 = array1.jsValue.object!
360360
try expectEqual(jsArray1.length, .number(3))
361361
try expectEqual(jsArray1[0], .number(1))
362362
try expectEqual(jsArray1[1], .number(2))
363363
try expectEqual(jsArray1[2], .number(3))
364364

365365
let array2: [ConvertibleToJSValue] = [1, "str", false]
366-
let jsArray2 = array2.jsValue().object!
366+
let jsArray2 = array2.jsValue.object!
367367
try expectEqual(jsArray2.length, .number(3))
368368
try expectEqual(jsArray2[0], .number(1))
369369
try expectEqual(jsArray2[1], .string("str"))
@@ -374,11 +374,11 @@ try test("Object Conversion") {
374374

375375
try expectEqual(jsArray2[4], .object(jsArray1))
376376

377-
let dict1: [String: ConvertibleToJSValue] = [
378-
"prop1": 1,
379-
"prop2": "foo",
377+
let dict1: [String: JSValue] = [
378+
"prop1": 1.jsValue,
379+
"prop2": "foo".jsValue,
380380
]
381-
let jsDict1 = dict1.jsValue().object!
381+
let jsDict1 = dict1.jsValue.object!
382382
try expectEqual(jsDict1.prop1, .number(1))
383383
try expectEqual(jsDict1.prop2, .string("foo"))
384384
}
@@ -425,7 +425,7 @@ try test("Closure Identifiers") {
425425
#endif
426426

427427
func checkArray<T>(_ array: [T]) throws where T: TypedArrayElement & Equatable {
428-
try expectEqual(toString(JSTypedArray(array).jsValue().object!), jsStringify(array))
428+
try expectEqual(toString(JSTypedArray(array).jsValue.object!), jsStringify(array))
429429
try checkArrayUnsafeBytes(array)
430430
}
431431

@@ -488,7 +488,7 @@ try test("TypedArray_Mutation") {
488488
for i in 0..<100 {
489489
try expectEqual(i, array[i])
490490
}
491-
try expectEqual(toString(array.jsValue().object!), jsStringify(Array(0..<100)))
491+
try expectEqual(toString(array.jsValue.object!), jsStringify(Array(0..<100)))
492492
}
493493

494494
try test("Date") {
@@ -797,9 +797,9 @@ try test("Hashable Conformance") {
797797

798798
let objectConstructor = JSObject.global.Object.function!
799799
let obj = objectConstructor.new()
800-
obj.a = 1.jsValue()
800+
obj.a = 1.jsValue
801801
let firstHash = obj.hashValue
802-
obj.b = 2.jsValue()
802+
obj.b = 2.jsValue
803803
let secondHash = obj.hashValue
804804
try expectEqual(firstHash, secondHash)
805805
}
@@ -819,11 +819,11 @@ try test("Symbols") {
819819
// })
820820
// }.prop
821821
let hasInstanceObject = JSObject.global.Object.function!.new()
822-
hasInstanceObject.prop = JSClosure { _ in .undefined }.jsValue()
822+
hasInstanceObject.prop = JSClosure { _ in .undefined }.jsValue
823823
let hasInstanceClass = hasInstanceObject.prop.function!
824824
hasInstanceClass[JSSymbol.hasInstance] = JSClosure { _ in
825825
return .boolean(true)
826-
}.jsValue()
826+
}.jsValue
827827
try expectEqual(hasInstanceClass[JSSymbol.hasInstance].function!().boolean, true)
828828
try expectEqual(JSObject.global.Object.isInstanceOf(hasInstanceClass), true)
829829
}

Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import _CJavaScriptEventLoop
55

66
#if compiler(>=5.5)
77

8-
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
8+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
99
public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
1010

1111
/// A function that queues a given closure as a microtask into JavaScript event loop.
@@ -97,7 +97,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable {
9797
}
9898
}
9999

100-
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
100+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
101101
public extension JSPromise {
102102
/// Wait for the promise to complete, returning (or throwing) its result.
103103
var value: JSValue {

Sources/JavaScriptEventLoop/JobQueue.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import _CJavaScriptEventLoop
66

77
#if compiler(>=5.5)
88

9-
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
9+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
1010
struct QueueState: Sendable {
1111
fileprivate var headJob: UnownedJob? = nil
1212
fileprivate var isSpinning: Bool = false
1313
}
1414

15-
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
15+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
1616
extension JavaScriptEventLoop {
1717

1818
func insertJobQueue(job newJob: UnownedJob) {
@@ -58,7 +58,7 @@ extension JavaScriptEventLoop {
5858
}
5959
}
6060

61-
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
61+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
6262
fileprivate extension UnownedJob {
6363
private func asImpl() -> UnsafeMutablePointer<_CJavaScriptEventLoop.Job> {
6464
unsafeBitCast(self, to: UnsafeMutablePointer<_CJavaScriptEventLoop.Job>.self)

Sources/JavaScriptKit/BasicObjects/JSError.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final class JSError: Error, JSBridgedClass {
3434
}
3535

3636
/// Creates a new `JSValue` from this `JSError` instance.
37-
public func jsValue() -> JSValue {
37+
public var jsValue: JSValue {
3838
.object(jsObject)
3939
}
4040
}

Sources/JavaScriptKit/BasicObjects/JSPromise.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public final class JSPromise: JSBridgedClass {
7979
@discardableResult
8080
public func then(success: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise {
8181
let closure = JSOneshotClosure {
82-
return success($0[0]).jsValue()
82+
success($0[0]).jsValue
8383
}
8484
return JSPromise(unsafelyWrapping: jsObject.then!(closure).object!)
8585
}
@@ -90,10 +90,10 @@ public final class JSPromise: JSBridgedClass {
9090
public func then(success: @escaping (JSValue) -> ConvertibleToJSValue,
9191
failure: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise {
9292
let successClosure = JSOneshotClosure {
93-
return success($0[0]).jsValue()
93+
success($0[0]).jsValue
9494
}
9595
let failureClosure = JSOneshotClosure {
96-
return failure($0[0]).jsValue()
96+
failure($0[0]).jsValue
9797
}
9898
return JSPromise(unsafelyWrapping: jsObject.then!(successClosure, failureClosure).object!)
9999
}
@@ -103,7 +103,7 @@ public final class JSPromise: JSBridgedClass {
103103
@discardableResult
104104
public func `catch`(failure: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise {
105105
let closure = JSOneshotClosure {
106-
return failure($0[0]).jsValue()
106+
failure($0[0]).jsValue
107107
}
108108
return .init(unsafelyWrapping: jsObject.catch!(closure).object!)
109109
}

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public protocol TypedArrayElement: ConvertibleToJSValue, ConstructibleFromJSValu
1313
/// A wrapper around all JavaScript [TypedArray](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypedArray) classes that exposes their properties in a type-safe way.
1414
/// FIXME: [BigInt-based TypedArrays are currently not supported](https://github.com/swiftwasm/JavaScriptKit/issues/56).
1515
public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral where Element: TypedArrayElement {
16-
public static var constructor: JSFunction { Element.typedArrayClass }
16+
public class var constructor: JSFunction { Element.typedArrayClass }
1717
public var jsObject: JSObject
1818

1919
public subscript(_ index: Int) -> Element {
2020
get {
2121
return Element.construct(from: jsObject[index])!
2222
}
2323
set {
24-
self.jsObject[index] = newValue.jsValue()
24+
self.jsObject[index] = newValue.jsValue
2525
}
2626
}
2727

@@ -30,7 +30,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
3030
///
3131
/// - Parameter length: The number of elements that will be allocated.
3232
public init(length: Int) {
33-
jsObject = Element.typedArrayClass.new(length)
33+
jsObject = Self.constructor.new(length)
3434
}
3535

3636
required public init(unsafelyWrapping jsObject: JSObject) {
@@ -45,7 +45,7 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
4545
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
4646
public convenience init(_ array: [Element]) {
4747
let jsArrayRef = array.withUnsafeBufferPointer { ptr in
48-
_create_typed_array(Element.typedArrayClass.id, ptr.baseAddress!, Int32(array.count))
48+
_create_typed_array(Self.constructor.id, ptr.baseAddress!, Int32(array.count))
4949
}
5050
self.init(unsafelyWrapping: JSObject(id: jsArrayRef))
5151
}
@@ -116,7 +116,10 @@ extension Int8: TypedArrayElement {
116116
extension UInt8: TypedArrayElement {
117117
public static var typedArrayClass = JSObject.global.Uint8Array.function!
118118
}
119-
// TODO: Support Uint8ClampedArray?
119+
120+
public class JSUInt8ClampedArray: JSTypedArray<UInt8> {
121+
public class override var constructor: JSFunction { JSObject.global.Uint8ClampedArray.function! }
122+
}
120123

121124
extension Int16: TypedArrayElement {
122125
public static var typedArrayClass = JSObject.global.Int16Array.function!

Sources/JavaScriptKit/ConvertibleToJSValue.swift

+47-34
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import _CJavaScriptKit
33
/// Objects that can be converted to a JavaScript value, preferably in a lossless manner.
44
public protocol ConvertibleToJSValue {
55
/// Create a JSValue that represents this object
6-
func jsValue() -> JSValue
6+
var jsValue: JSValue { get }
7+
}
8+
9+
extension ConvertibleToJSValue {
10+
@available(*, deprecated, message: "Use the .jsValue property instead")
11+
public func jsValue() -> JSValue { jsValue }
712
}
813

914
public typealias JSValueCompatible = ConvertibleToJSValue & ConstructibleFromJSValue
@@ -13,67 +18,67 @@ extension JSValue: JSValueCompatible {
1318
return value
1419
}
1520

16-
public func jsValue() -> JSValue { self }
21+
public var jsValue: JSValue { self }
1722
}
1823

1924
extension Bool: ConvertibleToJSValue {
20-
public func jsValue() -> JSValue { .boolean(self) }
25+
public var jsValue: JSValue { .boolean(self) }
2126
}
2227

2328
extension Int: ConvertibleToJSValue {
24-
public func jsValue() -> JSValue { .number(Double(self)) }
29+
public var jsValue: JSValue { .number(Double(self)) }
2530
}
2631

2732
extension UInt: ConvertibleToJSValue {
28-
public func jsValue() -> JSValue { .number(Double(self)) }
33+
public var jsValue: JSValue { .number(Double(self)) }
2934
}
3035

3136
extension Float: ConvertibleToJSValue {
32-
public func jsValue() -> JSValue { .number(Double(self)) }
37+
public var jsValue: JSValue { .number(Double(self)) }
3338
}
3439

3540
extension Double: ConvertibleToJSValue {
36-
public func jsValue() -> JSValue { .number(self) }
41+
public var jsValue: JSValue { .number(self) }
3742
}
3843

3944
extension String: ConvertibleToJSValue {
40-
public func jsValue() -> JSValue { .string(JSString(self)) }
45+
public var jsValue: JSValue { .string(JSString(self)) }
4146
}
4247

4348
extension UInt8: ConvertibleToJSValue {
44-
public func jsValue() -> JSValue { .number(Double(self)) }
49+
public var jsValue: JSValue { .number(Double(self)) }
4550
}
4651

4752
extension UInt16: ConvertibleToJSValue {
48-
public func jsValue() -> JSValue { .number(Double(self)) }
53+
public var jsValue: JSValue { .number(Double(self)) }
4954
}
5055

5156
extension UInt32: ConvertibleToJSValue {
52-
public func jsValue() -> JSValue { .number(Double(self)) }
57+
public var jsValue: JSValue { .number(Double(self)) }
5358
}
5459

5560
extension UInt64: ConvertibleToJSValue {
56-
public func jsValue() -> JSValue { .number(Double(self)) }
61+
public var jsValue: JSValue { .number(Double(self)) }
5762
}
5863

5964
extension Int8: ConvertibleToJSValue {
60-
public func jsValue() -> JSValue { .number(Double(self)) }
65+
public var jsValue: JSValue { .number(Double(self)) }
6166
}
6267

6368
extension Int16: ConvertibleToJSValue {
64-
public func jsValue() -> JSValue { .number(Double(self)) }
69+
public var jsValue: JSValue { .number(Double(self)) }
6570
}
6671

6772
extension Int32: ConvertibleToJSValue {
68-
public func jsValue() -> JSValue { .number(Double(self)) }
73+
public var jsValue: JSValue { .number(Double(self)) }
6974
}
7075

7176
extension Int64: ConvertibleToJSValue {
72-
public func jsValue() -> JSValue { .number(Double(self)) }
77+
public var jsValue: JSValue { .number(Double(self)) }
7378
}
7479

7580
extension JSString: ConvertibleToJSValue {
76-
public func jsValue() -> JSValue { .string(self) }
81+
public var jsValue: JSValue { .string(self) }
7782
}
7883

7984
extension JSObject: JSValueCompatible {
@@ -84,17 +89,21 @@ extension JSObject: JSValueCompatible {
8489
private let objectConstructor = JSObject.global.Object.function!
8590
private let arrayConstructor = JSObject.global.Array.function!
8691

87-
extension Dictionary where Value: ConvertibleToJSValue, Key == String {
88-
public func jsValue() -> JSValue {
89-
Swift.Dictionary<Key, ConvertibleToJSValue>.jsValue(self)()
92+
extension Dictionary where Value == ConvertibleToJSValue, Key == String {
93+
public var jsValue: JSValue {
94+
let object = objectConstructor.new()
95+
for (key, value) in self {
96+
object[key] = value.jsValue
97+
}
98+
return .object(object)
9099
}
91100
}
92101

93-
extension Dictionary: ConvertibleToJSValue where Value == ConvertibleToJSValue, Key == String {
94-
public func jsValue() -> JSValue {
102+
extension Dictionary: ConvertibleToJSValue where Value: ConvertibleToJSValue, Key == String {
103+
public var jsValue: JSValue {
95104
let object = objectConstructor.new()
96105
for (key, value) in self {
97-
object[key] = value.jsValue()
106+
object[key] = value.jsValue
98107
}
99108
return .object(object)
100109
}
@@ -104,7 +113,7 @@ extension Dictionary: ConstructibleFromJSValue where Value: ConstructibleFromJSV
104113
public static func construct(from value: JSValue) -> Self? {
105114
guard
106115
let objectRef = value.object,
107-
let keys: [String] = objectConstructor.keys!(objectRef.jsValue()).fromJSValue()
116+
let keys: [String] = objectConstructor.keys!(objectRef.jsValue).fromJSValue()
108117
else { return nil }
109118

110119
var entries = [(String, Value)]()
@@ -131,25 +140,29 @@ extension Optional: ConstructibleFromJSValue where Wrapped: ConstructibleFromJSV
131140
}
132141

133142
extension Optional: ConvertibleToJSValue where Wrapped: ConvertibleToJSValue {
134-
public func jsValue() -> JSValue {
143+
public var jsValue: JSValue {
135144
switch self {
136145
case .none: return .null
137-
case let .some(wrapped): return wrapped.jsValue()
146+
case let .some(wrapped): return wrapped.jsValue
138147
}
139148
}
140149
}
141150

142-
extension Array where Element: ConvertibleToJSValue {
143-
public func jsValue() -> JSValue {
144-
Array<ConvertibleToJSValue>.jsValue(self)()
151+
extension Array: ConvertibleToJSValue where Element: ConvertibleToJSValue {
152+
public var jsValue: JSValue {
153+
let array = arrayConstructor.new(count)
154+
for (index, element) in enumerated() {
155+
array[index] = element.jsValue
156+
}
157+
return .object(array)
145158
}
146159
}
147160

148-
extension Array: ConvertibleToJSValue where Element == ConvertibleToJSValue {
149-
public func jsValue() -> JSValue {
161+
extension Array where Element == ConvertibleToJSValue {
162+
public var jsValue: JSValue {
150163
let array = arrayConstructor.new(count)
151164
for (index, element) in enumerated() {
152-
array[index] = element.jsValue()
165+
array[index] = element.jsValue
153166
}
154167
return .object(array)
155168
}
@@ -176,7 +189,7 @@ extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValu
176189
}
177190

178191
extension RawJSValue: ConvertibleToJSValue {
179-
public func jsValue() -> JSValue {
192+
public var jsValue: JSValue {
180193
switch kind {
181194
case .invalid:
182195
fatalError()
@@ -243,7 +256,7 @@ extension Array where Element == ConvertibleToJSValue {
243256
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
244257
) -> T {
245258
if index == values.count { return body(results) }
246-
return values[index].jsValue().withRawJSValue { (rawValue) -> T in
259+
return values[index].jsValue.withRawJSValue { (rawValue) -> T in
247260
results.append(rawValue)
248261
return _withRawJSValues(values, index + 1, &results, body)
249262
}

0 commit comments

Comments
 (0)