Skip to content

Commit c4d2e12

Browse files
committed
[not working] attempt at new JSValueConvertible with no instance methods
1 parent 959379f commit c4d2e12

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

Sources/JavaScriptKit/JSValueConvertible.swift

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

33
public protocol JSValueConvertible {
4-
func jsValue() -> JSValue
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+
}
511
}
612

713
extension JSValue: JSValueConvertible {
8-
public func jsValue() -> JSValue { self }
14+
public static func jsValue(from jsValue: Self) -> JSValue { jsValue }
915
}
1016

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

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

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

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

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

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

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

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

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

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

5157
extension String: JSValueConvertible {
52-
public func jsValue() -> JSValue { .string(self) }
58+
public static func jsValue(from string: Self) -> JSValue { .string(string) }
5359
}
5460

5561
extension JSObjectRef: JSValueConvertible {
@@ -59,65 +65,53 @@ extension JSObjectRef: JSValueConvertible {
5965

6066
private let Object = JSObjectRef.global.Object.function!
6167

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

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

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()
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)
9185
}
92-
return .object(array)
86+
return .object(jsArray)
9387
}
9488
}
9589

9690
extension RawJSValue: JSValueConvertible {
97-
public func jsValue() -> JSValue {
98-
switch kind {
91+
public static func jsValue(from rawJSValue: Self) -> JSValue {
92+
switch rawJSValue.kind {
9993
case .invalid:
10094
fatalError()
10195
case .boolean:
102-
return .boolean(payload1 != 0)
96+
return .boolean(rawJSValue.payload1 != 0)
10397
case .number:
104-
return .number(payload3)
98+
return .number(rawJSValue.payload3)
10599
case .string:
106100
// +1 for null terminator
107-
let buffer = malloc(Int(payload2 + 1))!.assumingMemoryBound(to: UInt8.self)
101+
let buffer = malloc(Int(rawJSValue.payload2 + 1))!.assumingMemoryBound(to: UInt8.self)
108102
defer { free(buffer) }
109-
_load_string(JavaScriptObjectRef(payload1), buffer)
110-
buffer[Int(payload2)] = 0
103+
_load_string(JavaScriptObjectRef(rawJSValue.payload1), buffer)
104+
buffer[Int(rawJSValue.payload2)] = 0
111105
let string = String(decodingCString: UnsafePointer(buffer), as: UTF8.self)
112106
return .string(string)
113107
case .object:
114-
return .object(JSObjectRef(id: UInt32(payload1)))
108+
return .object(JSObjectRef(id: UInt32(rawJSValue.payload1)))
115109
case .null:
116110
return .null
117111
case .undefined:
118112
return .undefined
119113
case .function:
120-
return .function(JSFunctionRef(id: UInt32(payload1)))
114+
return .function(JSFunctionRef(id: UInt32(rawJSValue.payload1)))
121115
default:
122116
fatalError("unreachable")
123117
}

0 commit comments

Comments
 (0)