Skip to content

Commit f7b56eb

Browse files
Revert "Updates for DOMKit (#174)"
This reverts commit 95d0c4c.
1 parent 78a50d9 commit f7b56eb

File tree

13 files changed

+80
-108
lines changed

13 files changed

+80
-108
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

Lines changed: 12 additions & 12 deletions
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: JSValue] = [
378-
"prop1": 1.jsValue,
379-
"prop2": "foo".jsValue,
377+
let dict1: [String: ConvertibleToJSValue] = [
378+
"prop1": 1,
379+
"prop2": "foo",
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import _CJavaScriptEventLoop
55

66
#if compiler(>=5.5)
77

8-
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
8+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.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 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
100+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import _CJavaScriptEventLoop
66

77
#if compiler(>=5.5)
88

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

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

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

61-
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
61+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.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

Lines changed: 1 addition & 1 deletion
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 var jsValue: JSValue {
37+
public func jsValue() -> JSValue {
3838
.object(jsObject)
3939
}
4040
}

Sources/JavaScriptKit/BasicObjects/JSPromise.swift

Lines changed: 4 additions & 4 deletions
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-
success($0[0]).jsValue
82+
return 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-
success($0[0]).jsValue
93+
return success($0[0]).jsValue()
9494
}
9595
let failureClosure = JSOneshotClosure {
96-
failure($0[0]).jsValue
96+
return 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-
failure($0[0]).jsValue
106+
return failure($0[0]).jsValue()
107107
}
108108
return .init(unsafelyWrapping: jsObject.catch!(closure).object!)
109109
}

Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift

Lines changed: 5 additions & 8 deletions
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 class var constructor: JSFunction { Element.typedArrayClass }
16+
public static 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 = Self.constructor.new(length)
33+
jsObject = Element.typedArrayClass.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(Self.constructor.id, ptr.baseAddress!, Int32(array.count))
48+
_create_typed_array(Element.typedArrayClass.id, ptr.baseAddress!, Int32(array.count))
4949
}
5050
self.init(unsafelyWrapping: JSObject(id: jsArrayRef))
5151
}
@@ -116,10 +116,7 @@ extension Int8: TypedArrayElement {
116116
extension UInt8: TypedArrayElement {
117117
public static var typedArrayClass = JSObject.global.Uint8Array.function!
118118
}
119-
120-
public class JSUInt8ClampedArray: JSTypedArray<UInt8> {
121-
public class override var constructor: JSFunction { JSObject.global.Uint8ClampedArray.function! }
122-
}
119+
// TODO: Support Uint8ClampedArray?
123120

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

Sources/JavaScriptKit/ConvertibleToJSValue.swift

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ 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-
var jsValue: JSValue { get }
7-
}
8-
9-
extension ConvertibleToJSValue {
10-
@available(*, deprecated, message: "Use the .jsValue property instead")
11-
public func jsValue() -> JSValue { jsValue }
6+
func jsValue() -> JSValue
127
}
138

149
public typealias JSValueCompatible = ConvertibleToJSValue & ConstructibleFromJSValue
@@ -18,67 +13,67 @@ extension JSValue: JSValueCompatible {
1813
return value
1914
}
2015

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

2419
extension Bool: ConvertibleToJSValue {
25-
public var jsValue: JSValue { .boolean(self) }
20+
public func jsValue() -> JSValue { .boolean(self) }
2621
}
2722

2823
extension Int: ConvertibleToJSValue {
29-
public var jsValue: JSValue { .number(Double(self)) }
24+
public func jsValue() -> JSValue { .number(Double(self)) }
3025
}
3126

3227
extension UInt: ConvertibleToJSValue {
33-
public var jsValue: JSValue { .number(Double(self)) }
28+
public func jsValue() -> JSValue { .number(Double(self)) }
3429
}
3530

3631
extension Float: ConvertibleToJSValue {
37-
public var jsValue: JSValue { .number(Double(self)) }
32+
public func jsValue() -> JSValue { .number(Double(self)) }
3833
}
3934

4035
extension Double: ConvertibleToJSValue {
41-
public var jsValue: JSValue { .number(self) }
36+
public func jsValue() -> JSValue { .number(self) }
4237
}
4338

4439
extension String: ConvertibleToJSValue {
45-
public var jsValue: JSValue { .string(JSString(self)) }
40+
public func jsValue() -> JSValue { .string(JSString(self)) }
4641
}
4742

4843
extension UInt8: ConvertibleToJSValue {
49-
public var jsValue: JSValue { .number(Double(self)) }
44+
public func jsValue() -> JSValue { .number(Double(self)) }
5045
}
5146

5247
extension UInt16: ConvertibleToJSValue {
53-
public var jsValue: JSValue { .number(Double(self)) }
48+
public func jsValue() -> JSValue { .number(Double(self)) }
5449
}
5550

5651
extension UInt32: ConvertibleToJSValue {
57-
public var jsValue: JSValue { .number(Double(self)) }
52+
public func jsValue() -> JSValue { .number(Double(self)) }
5853
}
5954

6055
extension UInt64: ConvertibleToJSValue {
61-
public var jsValue: JSValue { .number(Double(self)) }
56+
public func jsValue() -> JSValue { .number(Double(self)) }
6257
}
6358

6459
extension Int8: ConvertibleToJSValue {
65-
public var jsValue: JSValue { .number(Double(self)) }
60+
public func jsValue() -> JSValue { .number(Double(self)) }
6661
}
6762

6863
extension Int16: ConvertibleToJSValue {
69-
public var jsValue: JSValue { .number(Double(self)) }
64+
public func jsValue() -> JSValue { .number(Double(self)) }
7065
}
7166

7267
extension Int32: ConvertibleToJSValue {
73-
public var jsValue: JSValue { .number(Double(self)) }
68+
public func jsValue() -> JSValue { .number(Double(self)) }
7469
}
7570

7671
extension Int64: ConvertibleToJSValue {
77-
public var jsValue: JSValue { .number(Double(self)) }
72+
public func jsValue() -> JSValue { .number(Double(self)) }
7873
}
7974

8075
extension JSString: ConvertibleToJSValue {
81-
public var jsValue: JSValue { .string(self) }
76+
public func jsValue() -> JSValue { .string(self) }
8277
}
8378

8479
extension JSObject: JSValueCompatible {
@@ -89,21 +84,17 @@ extension JSObject: JSValueCompatible {
8984
private let objectConstructor = JSObject.global.Object.function!
9085
private let arrayConstructor = JSObject.global.Array.function!
9186

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)
87+
extension Dictionary where Value: ConvertibleToJSValue, Key == String {
88+
public func jsValue() -> JSValue {
89+
Swift.Dictionary<Key, ConvertibleToJSValue>.jsValue(self)()
9990
}
10091
}
10192

102-
extension Dictionary: ConvertibleToJSValue where Value: ConvertibleToJSValue, Key == String {
103-
public var jsValue: JSValue {
93+
extension Dictionary: ConvertibleToJSValue where Value == ConvertibleToJSValue, Key == String {
94+
public func jsValue() -> JSValue {
10495
let object = objectConstructor.new()
10596
for (key, value) in self {
106-
object[key] = value.jsValue
97+
object[key] = value.jsValue()
10798
}
10899
return .object(object)
109100
}
@@ -113,7 +104,7 @@ extension Dictionary: ConstructibleFromJSValue where Value: ConstructibleFromJSV
113104
public static func construct(from value: JSValue) -> Self? {
114105
guard
115106
let objectRef = value.object,
116-
let keys: [String] = objectConstructor.keys!(objectRef.jsValue).fromJSValue()
107+
let keys: [String] = objectConstructor.keys!(objectRef.jsValue()).fromJSValue()
117108
else { return nil }
118109

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

142133
extension Optional: ConvertibleToJSValue where Wrapped: ConvertibleToJSValue {
143-
public var jsValue: JSValue {
134+
public func jsValue() -> JSValue {
144135
switch self {
145136
case .none: return .null
146-
case let .some(wrapped): return wrapped.jsValue
137+
case let .some(wrapped): return wrapped.jsValue()
147138
}
148139
}
149140
}
150141

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)
142+
extension Array where Element: ConvertibleToJSValue {
143+
public func jsValue() -> JSValue {
144+
Array<ConvertibleToJSValue>.jsValue(self)()
158145
}
159146
}
160147

161-
extension Array where Element == ConvertibleToJSValue {
162-
public var jsValue: JSValue {
148+
extension Array: ConvertibleToJSValue where Element == ConvertibleToJSValue {
149+
public func jsValue() -> JSValue {
163150
let array = arrayConstructor.new(count)
164151
for (index, element) in enumerated() {
165-
array[index] = element.jsValue
152+
array[index] = element.jsValue()
166153
}
167154
return .object(array)
168155
}
@@ -189,7 +176,7 @@ extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValu
189176
}
190177

191178
extension RawJSValue: ConvertibleToJSValue {
192-
public var jsValue: JSValue {
179+
public func jsValue() -> JSValue {
193180
switch kind {
194181
case .invalid:
195182
fatalError()
@@ -256,7 +243,7 @@ extension Array where Element == ConvertibleToJSValue {
256243
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
257244
) -> T {
258245
if index == values.count { return body(results) }
259-
return values[index].jsValue.withRawJSValue { (rawValue) -> T in
246+
return values[index].jsValue().withRawJSValue { (rawValue) -> T in
260247
results.append(rawValue)
261248
return _withRawJSValues(values, index + 1, &results, body)
262249
}

0 commit comments

Comments
 (0)