forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertibleToJSValue.swift
More file actions
275 lines (238 loc) · 8.09 KB
/
ConvertibleToJSValue.swift
File metadata and controls
275 lines (238 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import _CJavaScriptKit
/// Objects that can be converted to a JavaScript value, preferably in a lossless manner.
public protocol ConvertibleToJSValue {
/// Create a JSValue that represents this object
var jsValue: JSValue { get }
}
extension ConvertibleToJSValue {
@available(*, deprecated, message: "Use the .jsValue property instead")
public func jsValue() -> JSValue { jsValue }
}
public typealias JSValueCompatible = ConvertibleToJSValue & ConstructibleFromJSValue
extension JSValue: JSValueCompatible {
public static func construct(from value: JSValue) -> Self? {
return value
}
public var jsValue: JSValue { self }
}
extension Bool: ConvertibleToJSValue {
public var jsValue: JSValue { .boolean(self) }
}
extension Int: ConvertibleToJSValue {
public var jsValue: JSValue {
assert(Self.bitWidth == 32)
return .number(Double(self))
}
}
extension UInt: ConvertibleToJSValue {
public var jsValue: JSValue {
assert(Self.bitWidth == 32)
return .number(Double(self))
}
}
extension Float: ConvertibleToJSValue {
public var jsValue: JSValue { .number(Double(self)) }
}
extension Double: ConvertibleToJSValue {
public var jsValue: JSValue { .number(self) }
}
extension String: ConvertibleToJSValue {
public var jsValue: JSValue { .string(JSString(self)) }
}
extension UInt8: ConvertibleToJSValue {
public var jsValue: JSValue { .number(Double(self)) }
}
extension UInt16: ConvertibleToJSValue {
public var jsValue: JSValue { .number(Double(self)) }
}
extension UInt32: ConvertibleToJSValue {
public var jsValue: JSValue { .number(Double(self)) }
}
extension Int8: ConvertibleToJSValue {
public var jsValue: JSValue { .number(Double(self)) }
}
extension Int16: ConvertibleToJSValue {
public var jsValue: JSValue { .number(Double(self)) }
}
extension Int32: ConvertibleToJSValue {
public var jsValue: JSValue { .number(Double(self)) }
}
extension JSString: ConvertibleToJSValue {
public var jsValue: JSValue { .string(self) }
}
extension JSObject: JSValueCompatible {
// `JSObject.jsValue` is defined in JSObject.swift to be able to overridden
// from `JSFunction`
}
private let objectConstructor = JSObject.global.Object.function!
private let arrayConstructor = JSObject.global.Array.function!
extension Dictionary where Value == ConvertibleToJSValue, Key == String {
public var jsValue: JSValue {
let object = objectConstructor.new()
for (key, value) in self {
object[key] = value.jsValue
}
return .object(object)
}
}
extension Dictionary: ConvertibleToJSValue where Value: ConvertibleToJSValue, Key == String {
public var jsValue: JSValue {
let object = objectConstructor.new()
for (key, value) in self {
object[key] = value.jsValue
}
return .object(object)
}
}
extension Dictionary: ConstructibleFromJSValue where Value: ConstructibleFromJSValue, Key == String {
public static func construct(from value: JSValue) -> Self? {
guard
let objectRef = value.object,
let keys: [String] = objectConstructor.keys!(objectRef.jsValue).fromJSValue()
else { return nil }
var entries = [(String, Value)]()
entries.reserveCapacity(keys.count)
for key in keys {
guard let value: Value = objectRef[key].fromJSValue() else {
return nil
}
entries.append((key, value))
}
return Dictionary(uniqueKeysWithValues: entries)
}
}
extension Optional: ConstructibleFromJSValue where Wrapped: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
switch value {
case .null, .undefined:
return nil
default:
return Wrapped.construct(from: value)
}
}
}
extension Optional: ConvertibleToJSValue where Wrapped: ConvertibleToJSValue {
public var jsValue: JSValue {
switch self {
case .none: return .null
case let .some(wrapped): return wrapped.jsValue
}
}
}
extension Array: ConvertibleToJSValue where Element: ConvertibleToJSValue {
public var jsValue: JSValue {
let array = arrayConstructor.new(count)
for (index, element) in enumerated() {
array[index] = element.jsValue
}
return .object(array)
}
}
extension Array where Element == ConvertibleToJSValue {
public var jsValue: JSValue {
let array = arrayConstructor.new(count)
for (index, element) in enumerated() {
array[index] = element.jsValue
}
return .object(array)
}
}
extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> [Element]? {
guard
let objectRef = value.object,
objectRef.isInstanceOf(JSObject.global.Array.function!)
else { return nil }
let count: Int = objectRef.length.fromJSValue()!
var array = [Element]()
array.reserveCapacity(count)
for i in 0 ..< count {
guard let value: Element = objectRef[i].fromJSValue() else { return nil }
array.append(value)
}
return array
}
}
extension RawJSValue: ConvertibleToJSValue {
public var jsValue: JSValue {
switch kind {
case .boolean:
return .boolean(payload1 != 0)
case .number:
return .number(payload2)
case .string:
return .string(JSString(jsRef: payload1))
case .object:
return .object(JSObject(id: UInt32(payload1)))
case .null:
return .null
case .undefined:
return .undefined
case .function:
return .function(JSFunction(id: UInt32(payload1)))
case .symbol:
return .symbol(JSSymbol(id: UInt32(payload1)))
case .bigInt:
return .bigInt(JSBigInt(id: UInt32(payload1)))
}
}
}
extension JSValue {
func withRawJSValue<T>(_ body: (RawJSValue) -> T) -> T {
let kind: JavaScriptValueKind
let payload1: JavaScriptPayload1
var payload2: JavaScriptPayload2 = 0
switch self {
case let .boolean(boolValue):
kind = .boolean
payload1 = boolValue ? 1 : 0
case let .number(numberValue):
kind = .number
payload1 = 0
payload2 = numberValue
case let .string(string):
return string.withRawJSValue(body)
case let .object(ref):
kind = .object
payload1 = JavaScriptPayload1(ref.id)
case .null:
kind = .null
payload1 = 0
case .undefined:
kind = .undefined
payload1 = 0
case let .function(functionRef):
kind = .function
payload1 = JavaScriptPayload1(functionRef.id)
case let .symbol(symbolRef):
kind = .symbol
payload1 = JavaScriptPayload1(symbolRef.id)
case let .bigInt(bigIntRef):
kind = .bigInt
payload1 = JavaScriptPayload1(bigIntRef.id)
}
let rawValue = RawJSValue(kind: kind, payload1: payload1, payload2: payload2)
return body(rawValue)
}
}
extension Array where Element == ConvertibleToJSValue {
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
func _withRawJSValues<T>(
_ values: [ConvertibleToJSValue], _ index: Int,
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
) -> T {
if index == values.count { return body(results) }
return values[index].jsValue.withRawJSValue { (rawValue) -> T in
results.append(rawValue)
return _withRawJSValues(values, index + 1, &results, body)
}
}
var _results = [RawJSValue]()
return _withRawJSValues(self, 0, &_results, body)
}
}
extension Array where Element: ConvertibleToJSValue {
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
[ConvertibleToJSValue].withRawJSValues(self)(body)
}
}