Skip to content

Commit 4b70a6a

Browse files
committed
Revert "[not working] attempt at new JSValueConvertible with no instance methods"
This reverts commit c4d2e12.
1 parent c4d2e12 commit 4b70a6a

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

Sources/JavaScriptKit/JSValueConvertible.swift

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,55 @@
11
import _CJavaScriptKit
22

33
public protocol JSValueConvertible {
4-
static func jsValue(from: Self) -> JSValue
5-
}
6-
7-
extension JSValue {
8-
public init<T: JSValueConvertible>(from jsValue: T) {
9-
self = T.jsValue(from: jsValue)
10-
}
4+
func jsValue() -> JSValue
115
}
126

137
extension JSValue: JSValueConvertible {
14-
public static func jsValue(from jsValue: Self) -> JSValue { jsValue }
8+
public func jsValue() -> JSValue { self }
159
}
1610

1711
extension Bool: JSValueConvertible {
18-
public static func jsValue(from bool: Self) -> JSValue { .boolean(bool) }
12+
public func jsValue() -> JSValue { .boolean(self) }
1913
}
2014

2115
extension Int: JSValueConvertible {
22-
public static func jsValue(from int: Self) -> JSValue { .number(Double(int)) }
16+
public func jsValue() -> JSValue { .number(Double(self)) }
2317
}
2418

2519
extension Int8: JSValueConvertible {
26-
public static func jsValue(from int8: Self) -> JSValue { .number(Double(int8)) }
20+
public func jsValue() -> JSValue { .number(Double(self)) }
2721
}
2822

2923
extension Int16: JSValueConvertible {
30-
public static func jsValue(from int16: Self) -> JSValue { .number(Double(int16)) }
24+
public func jsValue() -> JSValue { .number(Double(self)) }
3125
}
3226

3327
extension Int32: JSValueConvertible {
34-
public static func jsValue(from int32: Self) -> JSValue { .number(Double(int32)) }
28+
public func jsValue() -> JSValue { .number(Double(self)) }
3529
}
3630

3731
extension UInt: JSValueConvertible {
38-
public static func jsValue(from uint: Self) -> JSValue { .number(Double(uint)) }
32+
public func jsValue() -> JSValue { .number(Double(self)) }
3933
}
4034

4135
extension UInt8: JSValueConvertible {
42-
public static func jsValue(from uint8: Self) -> JSValue { .number(Double(uint8)) }
36+
public func jsValue() -> JSValue { .number(Double(self)) }
4337
}
4438

4539
extension UInt16: JSValueConvertible {
46-
public static func jsValue(from uint16: Self) -> JSValue { .number(Double(uint16)) }
40+
public func jsValue() -> JSValue { .number(Double(self)) }
4741
}
4842

4943
extension Float: JSValueConvertible {
50-
public static func jsValue(from float: Self) -> JSValue { .number(Double(float)) }
44+
public func jsValue() -> JSValue { .number(Double(self)) }
5145
}
5246

5347
extension Double: JSValueConvertible {
54-
public static func jsValue(from double: Self) -> JSValue { .number(double) }
48+
public func jsValue() -> JSValue { .number(self) }
5549
}
5650

5751
extension String: JSValueConvertible {
58-
public static func jsValue(from string: Self) -> JSValue { .string(string) }
52+
public func jsValue() -> JSValue { .string(self) }
5953
}
6054

6155
extension JSObjectRef: JSValueConvertible {
@@ -65,53 +59,65 @@ extension JSObjectRef: JSValueConvertible {
6559

6660
private let Object = JSObjectRef.global.Object.function!
6761

68-
extension Dictionary: JSValueConvertible where Value: JSValueConvertible, Key == String {
69-
public static func jsValue(from dict: Self) -> JSValue {
62+
extension Dictionary where Value: JSValueConvertible, Key == String {
63+
public func jsValue() -> JSValue {
64+
Swift.Dictionary<Key, JSValueConvertible>.jsValue(self)()
65+
}
66+
}
67+
68+
extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key == String {
69+
public func jsValue() -> JSValue {
7070
let object = Object.new()
71-
for (key, value) in dict {
72-
object[key] = JSValue(from: value)
71+
for (key, value) in self {
72+
object[key] = value.jsValue()
7373
}
7474
return .object(object)
7575
}
7676
}
7777

7878
private let Array = JSObjectRef.global.Array.function!
7979

80-
extension Array: JSValueConvertible where Element: JSValueConvertible {
81-
public static func jsValue(from array: Self) -> JSValue {
82-
let jsArray = Array.new(array.count)
83-
for (index, element) in array.enumerated() {
84-
jsArray[index] = JSValue(from: element)
80+
extension Array where Element: JSValueConvertible {
81+
public func jsValue() -> JSValue {
82+
Swift.Array<JSValueConvertible>.jsValue(self)()
83+
}
84+
}
85+
86+
extension Array: JSValueConvertible where Element == JSValueConvertible {
87+
public func jsValue() -> JSValue {
88+
let array = Array.new(count)
89+
for (index, element) in enumerated() {
90+
array[index] = element.jsValue()
8591
}
86-
return .object(jsArray)
92+
return .object(array)
8793
}
8894
}
8995

9096
extension RawJSValue: JSValueConvertible {
91-
public static func jsValue(from rawJSValue: Self) -> JSValue {
92-
switch rawJSValue.kind {
97+
public func jsValue() -> JSValue {
98+
switch kind {
9399
case .invalid:
94100
fatalError()
95101
case .boolean:
96-
return .boolean(rawJSValue.payload1 != 0)
102+
return .boolean(payload1 != 0)
97103
case .number:
98-
return .number(rawJSValue.payload3)
104+
return .number(payload3)
99105
case .string:
100106
// +1 for null terminator
101-
let buffer = malloc(Int(rawJSValue.payload2 + 1))!.assumingMemoryBound(to: UInt8.self)
107+
let buffer = malloc(Int(payload2 + 1))!.assumingMemoryBound(to: UInt8.self)
102108
defer { free(buffer) }
103-
_load_string(JavaScriptObjectRef(rawJSValue.payload1), buffer)
104-
buffer[Int(rawJSValue.payload2)] = 0
109+
_load_string(JavaScriptObjectRef(payload1), buffer)
110+
buffer[Int(payload2)] = 0
105111
let string = String(decodingCString: UnsafePointer(buffer), as: UTF8.self)
106112
return .string(string)
107113
case .object:
108-
return .object(JSObjectRef(id: UInt32(rawJSValue.payload1)))
114+
return .object(JSObjectRef(id: UInt32(payload1)))
109115
case .null:
110116
return .null
111117
case .undefined:
112118
return .undefined
113119
case .function:
114-
return .function(JSFunctionRef(id: UInt32(rawJSValue.payload1)))
120+
return .function(JSFunctionRef(id: UInt32(payload1)))
115121
default:
116122
fatalError("unreachable")
117123
}

0 commit comments

Comments
 (0)