1
1
import _CJavaScriptKit
2
2
3
3
public protocol JSBridgedType : JSValueCodable , CustomStringConvertible {
4
+ static var constructor : JSFunctionRef ? { get }
5
+
4
6
var objectRef : JSObjectRef { get }
5
7
init ( objectRef: JSObjectRef )
6
8
}
7
9
10
+ extension JSBridgedType {
11
+ public var description : String {
12
+ return objectRef. toString!( ) . fromJSValue ( )
13
+ }
14
+ }
15
+
8
16
public protocol JSValueEncodable {
9
17
subscript( jsValue _: ( ) ) -> JSValue { get }
10
18
}
@@ -15,19 +23,17 @@ public protocol JSValueDecodable {
15
23
static func canDecode( from jsValue: JSValue ) -> Bool
16
24
}
17
25
18
- extension JSBridgedType {
19
-
20
- public var description : String {
21
- return objectRef. toString!( ) . fromJSValue ( )
22
- }
23
- }
24
-
25
26
public typealias JSValueCodable = JSValueEncodable & JSValueDecodable
26
27
27
28
extension JSBridgedType {
28
-
29
29
public static func canDecode( from jsValue: JSValue ) -> Bool {
30
- jsValue. isObject && jsValue. instanceof ( String ( describing: Self . self) )
30
+ if let object = jsValue. object {
31
+ if let constructor = Self . constructor {
32
+ return JSObjectRef . instanceof ( object, constructor: constructor)
33
+ }
34
+ return true
35
+ }
36
+ return false
31
37
}
32
38
33
39
public init ( jsValue: JSValue ) {
@@ -130,24 +136,29 @@ extension String: JSValueCodable {
130
136
}
131
137
132
138
extension JSObjectRef : JSValueCodable {
133
-
134
139
// `JSObjectRef.jsValue` is defined in JSObjectRef.swift to be able to overridden
135
140
// from `JSFunctionRef`
136
141
}
137
142
138
- private let Object = JSObjectRef . global. Object. function!
139
-
140
- extension Dictionary : JSValueEncodable where Value: JSValueEncodable , Key == String {
143
+ private let JSObject = JSObjectRef . global. Object. function!
141
144
145
+ extension Dictionary : JSValueEncodable where Value == JSValueEncodable , Key == String {
142
146
public subscript( jsValue _: ( ) ) -> JSValue {
143
- let object = Object ( . new)
147
+ let object = JSObject ( . new)
144
148
for (key, value) in self {
145
149
object [ key] = JSValue ( from: value)
146
150
}
147
151
return . object( object)
148
152
}
149
153
}
150
154
155
+ extension Dictionary where Value: JSValueEncodable , Key == String {
156
+ public func jsValue( ) -> JSValue {
157
+ Dictionary < Key , JSValueEncodable > . jsValue ( self ) ( )
158
+ }
159
+ }
160
+
161
+
151
162
extension Dictionary : JSValueDecodable where Value: JSValueDecodable , Key == String {
152
163
153
164
public static func canDecode( from jsValue: JSValue ) -> Bool {
@@ -158,7 +169,7 @@ extension Dictionary: JSValueDecodable where Value: JSValueDecodable, Key == Str
158
169
159
170
let objectRef : JSObjectRef = jsValue. object!
160
171
161
- let keys : [ String ] = Object . keys!( JSValue ( from: objectRef. jsValue) ) . fromJSValue ( )
172
+ let keys : [ String ] = JSObject . keys!( JSValue ( from: objectRef. jsValue) ) . fromJSValue ( )
162
173
self = Dictionary ( uniqueKeysWithValues: keys. map ( {
163
174
return ( $0, objectRef [ dynamicMember: $0] . fromJSValue ( ) )
164
175
} ) )
@@ -193,8 +204,7 @@ extension Optional: JSValueEncodable where Wrapped: JSValueEncodable {
193
204
194
205
private let JSArray = JSObjectRef . global. Array. function!
195
206
196
- extension Array : JSValueEncodable where Element: JSValueEncodable {
197
-
207
+ extension Array : JSValueEncodable where Element == JSValueEncodable {
198
208
199
209
public subscript( jsValue _: ( ) ) -> JSValue {
200
210
let array = JSArray ( new: count)
@@ -205,6 +215,12 @@ extension Array: JSValueEncodable where Element: JSValueEncodable {
205
215
}
206
216
}
207
217
218
+ extension Array where Element: JSValueEncodable {
219
+ public func jsValue( ) -> JSValue {
220
+ Array < JSValueEncodable > . jsValue ( self ) ( )
221
+ }
222
+ }
223
+
208
224
extension Array : JSValueDecodable where Element: JSValueDecodable {
209
225
210
226
public static func canDecode( from jsValue: JSValue ) -> Bool {
@@ -318,6 +334,6 @@ extension Array where Element == JSValueEncodable {
318
334
319
335
extension Array where Element: JSValueEncodable {
320
336
func withRawJSValues< T> ( _ body: ( [ RawJSValue ] ) -> T ) -> T {
321
- Swift . Array < JSValueEncodable > . withRawJSValues ( self ) ( body)
337
+ Array < JSValueEncodable > . withRawJSValues ( self ) ( body)
322
338
}
323
339
}
0 commit comments