Skip to content

Commit 4373e95

Browse files
committed
Subscript-based JSValue conversion
1 parent 4b70a6a commit 4373e95

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

Sources/JavaScriptKit/JSFunction.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class JSFunctionRef: JSObjectRef {
1616
return result
1717
}
1818
}
19-
return result.jsValue()
19+
return JSValue(from: result)
2020
}
2121

2222
public func apply(this: JSObjectRef, arguments: JSValueConvertible...) -> JSValue {
@@ -35,7 +35,7 @@ public class JSFunctionRef: JSObjectRef {
3535
return result
3636
}
3737
}
38-
return result.jsValue()
38+
return JSValue(from: result)
3939
}
4040

4141
public func new(_ arguments: JSValueConvertible...) -> JSObjectRef {
@@ -58,7 +58,7 @@ public class JSFunctionRef: JSObjectRef {
5858
fatalError("unavailable")
5959
}
6060

61-
override public func jsValue() -> JSValue {
61+
public override subscript(jsValue _: ()) -> JSValue {
6262
.function(self)
6363
}
6464
}
@@ -106,9 +106,7 @@ public func _call_host_function(
106106
guard let hostFunc = JSClosure.sharedFunctions[hostFuncRef] else {
107107
fatalError("The function was already released")
108108
}
109-
let args = UnsafeBufferPointer(start: argv, count: Int(argc)).map {
110-
$0.jsValue()
111-
}
109+
let args = UnsafeBufferPointer(start: argv, count: Int(argc)).map(JSValue.init(from:))
112110
let result = hostFunc(args)
113111
let callbackFuncRef = JSFunctionRef(id: callbackFuncRef)
114112
_ = 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 func jsValue() -> JSValue {
46+
public subscript(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 rawValue.jsValue()
77+
return JSValue(from: rawValue)
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 rawValue.jsValue()
91+
return JSValue(from: rawValue)
9292
}
9393

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

Sources/JavaScriptKit/JSValueConvertible.swift

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

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

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

1114
extension Bool: JSValueConvertible {
12-
public func jsValue() -> JSValue { .boolean(self) }
15+
public subscript(jsValue _: ()) -> JSValue { .boolean(self) }
1316
}
1417

1518
extension Int: JSValueConvertible {
16-
public func jsValue() -> JSValue { .number(Double(self)) }
19+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
1720
}
1821

1922
extension Int8: JSValueConvertible {
20-
public func jsValue() -> JSValue { .number(Double(self)) }
23+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
2124
}
2225

2326
extension Int16: JSValueConvertible {
24-
public func jsValue() -> JSValue { .number(Double(self)) }
27+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
2528
}
2629

2730
extension Int32: JSValueConvertible {
28-
public func jsValue() -> JSValue { .number(Double(self)) }
31+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
2932
}
3033

3134
extension UInt: JSValueConvertible {
32-
public func jsValue() -> JSValue { .number(Double(self)) }
35+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
3336
}
3437

3538
extension UInt8: JSValueConvertible {
36-
public func jsValue() -> JSValue { .number(Double(self)) }
39+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
3740
}
3841

3942
extension UInt16: JSValueConvertible {
40-
public func jsValue() -> JSValue { .number(Double(self)) }
43+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
4144
}
4245

4346
extension Float: JSValueConvertible {
44-
public func jsValue() -> JSValue { .number(Double(self)) }
47+
public subscript(jsValue _: ()) -> JSValue { .number(Double(self)) }
4548
}
4649

4750
extension Double: JSValueConvertible {
48-
public func jsValue() -> JSValue { .number(self) }
51+
public subscript(jsValue _: ()) -> JSValue { .number(self) }
4952
}
5053

5154
extension String: JSValueConvertible {
52-
public func jsValue() -> JSValue { .string(self) }
55+
public subscript(jsValue _: ()) -> JSValue { .string(self) }
5356
}
5457

5558
extension JSObjectRef: JSValueConvertible {
@@ -60,16 +63,16 @@ extension JSObjectRef: JSValueConvertible {
6063
private let Object = JSObjectRef.global.Object.function!
6164

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

6871
extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key == String {
69-
public func jsValue() -> JSValue {
72+
public subscript(jsValue _: ()) -> JSValue {
7073
let object = Object.new()
7174
for (key, value) in self {
72-
object[key] = value.jsValue()
75+
object[key] = JSValue(from: value)
7376
}
7477
return .object(object)
7578
}
@@ -78,23 +81,23 @@ extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key
7881
private let Array = JSObjectRef.global.Array.function!
7982

8083
extension Array where Element: JSValueConvertible {
81-
public func jsValue() -> JSValue {
82-
Swift.Array<JSValueConvertible>.jsValue(self)()
84+
public subscript(jsValue _: ()) -> JSValue {
85+
JSValue(from: self as Swift.Array<JSValueConvertible>)
8386
}
8487
}
8588

8689
extension Array: JSValueConvertible where Element == JSValueConvertible {
87-
public func jsValue() -> JSValue {
90+
public subscript(jsValue _: ()) -> JSValue {
8891
let array = Array.new(count)
8992
for (index, element) in enumerated() {
90-
array[index] = element.jsValue()
93+
array[index] = JSValue(from: element)
9194
}
9295
return .object(array)
9396
}
9497
}
9598

9699
extension RawJSValue: JSValueConvertible {
97-
public func jsValue() -> JSValue {
100+
public subscript(jsValue _: ()) -> JSValue {
98101
switch kind {
99102
case .invalid:
100103
fatalError()
@@ -176,7 +179,7 @@ extension Array where Element == JSValueConvertible {
176179
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
177180
) -> T {
178181
if index == values.count { return body(results) }
179-
return values[index].jsValue().withRawJSValue { (rawValue) -> T in
182+
return JSValue(from: values[index]).withRawJSValue { (rawValue) -> T in
180183
results.append(rawValue)
181184
return _withRawJSValues(values, index + 1, &results, body)
182185
}

0 commit comments

Comments
 (0)