Skip to content

Commit 774b08b

Browse files
committed
Revert "Subscript-based JSValue conversion"
This reverts commit 4373e95.
1 parent 0f240c3 commit 774b08b

File tree

5 files changed

+34
-35
lines changed

5 files changed

+34
-35
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,14 @@ Call_Function_With_This: do {
271271

272272
Object_Conversion: do {
273273
// let array1 = [1, 2, 3]
274-
// let jsArray1 = JSValue(from: array1).object!
274+
// let jsArray1 = array1.jsValue().object!
275275
// try expectEqual(jsArray1.length, .number(3))
276276
// try expectEqual(jsArray1[0], .number(1))
277277
// try expectEqual(jsArray1[1], .number(2))
278278
// try expectEqual(jsArray1[2], .number(3))
279279

280280
let array2: [JSValueConvertible] = [1, "str", false]
281-
let jsArray2 = JSValue(from: array2).object!
281+
let jsArray2 = array2.jsValue().object!
282282
try expectEqual(jsArray2.length, .number(3))
283283
try expectEqual(jsArray2[0], .number(1))
284284
try expectEqual(jsArray2[1], .string("str"))
@@ -293,7 +293,7 @@ Object_Conversion: do {
293293
"prop1": 1,
294294
"prop2": "foo",
295295
]
296-
let jsDict1 = JSValue(from: dict1).object!
296+
let jsDict1 = dict1.jsValue().object!
297297
try expectEqual(jsDict1.prop1, .number(1))
298298
try expectEqual(jsDict1.prop2, .string("foo"))
299299
} catch {

Sources/JavaScriptKit/JSFunction.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class JSFunctionRef: JSObjectRef {
2121
return result
2222
}
2323
}
24-
return JSValue(from: result)
24+
return result.jsValue()
2525
}
2626

2727
@discardableResult
@@ -57,7 +57,7 @@ public class JSFunctionRef: JSObjectRef {
5757
fatalError("unavailable")
5858
}
5959

60-
public override subscript(jsValue _: ()) -> JSValue {
60+
override public func jsValue() -> JSValue {
6161
.function(self)
6262
}
6363
}
@@ -105,7 +105,9 @@ public func _call_host_function(
105105
guard let hostFunc = JSClosure.sharedFunctions[hostFuncRef] else {
106106
fatalError("The function was already released")
107107
}
108-
let args = UnsafeBufferPointer(start: argv, count: Int(argc)).map(JSValue.init(from:))
108+
let args = UnsafeBufferPointer(start: argv, count: Int(argc)).map {
109+
$0.jsValue()
110+
}
109111
let result = hostFunc(args)
110112
let callbackFuncRef = JSFunctionRef(id: callbackFuncRef)
111113
_ = callbackFuncRef(result)

Sources/JavaScriptKit/JSObject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class JSObjectRef: Equatable {
4343
return lhs.id == rhs.id
4444
}
4545

46-
public subscript(jsValue _: ()) -> JSValue {
46+
public func jsValue() -> JSValue {
4747
.object(self)
4848
}
4949
}

Sources/JavaScriptKit/JSValue.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public func getJSValue(this: JSObjectRef, name: String) -> JSValue {
7474
_get_prop(this.id, name, Int32(name.count),
7575
&rawValue.kind,
7676
&rawValue.payload1, &rawValue.payload2, &rawValue.payload3)
77-
return JSValue(from: rawValue)
77+
return rawValue.jsValue()
7878
}
7979

8080
public func setJSValue(this: JSObjectRef, name: String, value: JSValue) {
@@ -88,7 +88,7 @@ public func getJSValue(this: JSObjectRef, index: Int32) -> JSValue {
8888
_get_subscript(this.id, index,
8989
&rawValue.kind,
9090
&rawValue.payload1, &rawValue.payload2, &rawValue.payload3)
91-
return JSValue(from: rawValue)
91+
return rawValue.jsValue()
9292
}
9393

9494
public func setJSValue(this: JSObjectRef, index: Int32, value: JSValue) {

Sources/JavaScriptKit/JSValueConvertible.swift

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,55 @@
11
import _CJavaScriptKit
22

33
public protocol JSValueConvertible {
4-
subscript(jsValue _: ()) -> JSValue { get }
4+
func jsValue() -> JSValue
55
}
66

77
extension JSValue: JSValueConvertible {
8-
public init(from convertible: JSValueConvertible) {
9-
self = convertible[jsValue: ()]
10-
}
11-
public subscript(jsValue _: ()) -> JSValue { self }
8+
public func jsValue() -> JSValue { self }
129
}
1310

1411
extension Bool: JSValueConvertible {
15-
public subscript(jsValue _: ()) -> JSValue { .boolean(self) }
12+
public func jsValue() -> JSValue { .boolean(self) }
1613
}
1714

1815
extension Int: JSValueConvertible {
19-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
16+
public func jsValue() -> JSValue { .number(Double(self)) }
2017
}
2118

2219
extension Int8: JSValueConvertible {
23-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
20+
public func jsValue() -> JSValue { .number(Double(self)) }
2421
}
2522

2623
extension Int16: JSValueConvertible {
27-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
24+
public func jsValue() -> JSValue { .number(Double(self)) }
2825
}
2926

3027
extension Int32: JSValueConvertible {
31-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
28+
public func jsValue() -> JSValue { .number(Double(self)) }
3229
}
3330

3431
extension UInt: JSValueConvertible {
35-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
32+
public func jsValue() -> JSValue { .number(Double(self)) }
3633
}
3734

3835
extension UInt8: JSValueConvertible {
39-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
36+
public func jsValue() -> JSValue { .number(Double(self)) }
4037
}
4138

4239
extension UInt16: JSValueConvertible {
43-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
40+
public func jsValue() -> JSValue { .number(Double(self)) }
4441
}
4542

4643
extension Float: JSValueConvertible {
47-
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
44+
public func jsValue() -> JSValue { .number(Double(self)) }
4845
}
4946

5047
extension Double: JSValueConvertible {
51-
public subscript(jsValue _: ()) -> JSValue { .number(self) }
48+
public func jsValue() -> JSValue { .number(self) }
5249
}
5350

5451
extension String: JSValueConvertible {
55-
public subscript(jsValue _: ()) -> JSValue { .string(self) }
52+
public func jsValue() -> JSValue { .string(self) }
5653
}
5754

5855
extension JSObjectRef: JSValueConvertible {
@@ -63,16 +60,16 @@ extension JSObjectRef: JSValueConvertible {
6360
private let Object = JSObjectRef.global.Object.function!
6461

6562
extension Dictionary where Value: JSValueConvertible, Key == String {
66-
public subscript(jsValue _: ()) -> JSValue {
67-
JSValue(from: self as Dictionary<Key, JSValueConvertible>)
63+
public func jsValue() -> JSValue {
64+
Swift.Dictionary<Key, JSValueConvertible>.jsValue(self)()
6865
}
6966
}
7067

7168
extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key == String {
72-
public subscript(jsValue _: ()) -> JSValue {
69+
public func jsValue() -> JSValue {
7370
let object = Object.new()
7471
for (key, value) in self {
75-
object[key] = JSValue(from: value)
72+
object[key] = value.jsValue()
7673
}
7774
return .object(object)
7875
}
@@ -81,23 +78,23 @@ extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key
8178
private let Array = JSObjectRef.global.Array.function!
8279

8380
extension Array where Element: JSValueConvertible {
84-
public subscript(jsValue _: ()) -> JSValue {
85-
JSValue(from: self as Swift.Array<JSValueConvertible>)
81+
public func jsValue() -> JSValue {
82+
Swift.Array<JSValueConvertible>.jsValue(self)()
8683
}
8784
}
8885

8986
extension Array: JSValueConvertible where Element == JSValueConvertible {
90-
public subscript(jsValue _: ()) -> JSValue {
87+
public func jsValue() -> JSValue {
9188
let array = Array.new(count)
9289
for (index, element) in enumerated() {
93-
array[index] = JSValue(from: element)
90+
array[index] = element.jsValue()
9491
}
9592
return .object(array)
9693
}
9794
}
9895

9996
extension RawJSValue: JSValueConvertible {
100-
public subscript(jsValue _: ()) -> JSValue {
97+
public func jsValue() -> JSValue {
10198
switch kind {
10299
case .invalid:
103100
fatalError()
@@ -179,7 +176,7 @@ extension Array where Element == JSValueConvertible {
179176
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
180177
) -> T {
181178
if index == values.count { return body(results) }
182-
return JSValue(from: values[index]).withRawJSValue { (rawValue) -> T in
179+
return values[index].jsValue().withRawJSValue { (rawValue) -> T in
183180
results.append(rawValue)
184181
return _withRawJSValues(values, index + 1, &results, body)
185182
}

0 commit comments

Comments
 (0)