Skip to content

Commit c24c337

Browse files
committed
Several fixes & updates
1 parent 3f7bba2 commit c24c337

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

Sources/JavaScriptKit/JSFunction.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ public enum _JSFunctionConstructorSymbol {
66

77
public class JSFunctionRef: JSObjectRef {
88

9-
public override class func canDecode(from jsValue: JSValue) -> Bool {
10-
return jsValue.isFunction
11-
}
12-
139
@discardableResult
1410
func callAsFunction(this: JSObjectRef? = nil, args: [JSValueEncodable]) -> JSValue {
1511
let result = args.withRawJSValues { rawValues in
@@ -59,6 +55,10 @@ public class JSFunctionRef: JSObjectRef {
5955
fatalError("unavailable")
6056
}
6157

58+
public override class func canDecode(from jsValue: JSValue) -> Bool {
59+
return jsValue.isFunction
60+
}
61+
6262
public convenience required init(jsValue: JSValue) {
6363
switch jsValue {
6464
case .function(let value):
@@ -92,8 +92,13 @@ public class JSClosure: JSFunctionRef {
9292
}
9393

9494
public convenience required init(jsValue: JSValue) {
95-
let fun = JSFunctionRef(jsValue: jsValue)
96-
self.init { fun(args: $0) }
95+
switch jsValue {
96+
case .function(let value):
97+
let fun = JSFunctionRef(jsValue: jsValue)
98+
self.init { fun(args: $0) }
99+
default:
100+
fatalError()
101+
}
97102
}
98103

99104
public func release() {

Sources/JavaScriptKit/JSObject.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import _CJavaScriptKit
22

33
@dynamicMemberLookup
44
public class JSObjectRef: Equatable {
5-
public class func canDecode(from jsValue: JSValue) -> Bool {
6-
return jsValue.isObject
7-
}
85

96
internal var id: UInt32
107
init(id: UInt32) {
@@ -59,4 +56,9 @@ public class JSObjectRef: Equatable {
5956
public subscript(jsValue _: ()) -> JSValue {
6057
.object(self)
6158
}
59+
60+
public class func canDecode(from jsValue: JSValue) -> Bool {
61+
return jsValue.isObject
62+
}
6263
}
64+

Sources/JavaScriptKit/JSValue.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ public func setJSValue(this: JSObjectRef, index: Int32, value: JSValue) {
144144

145145
extension JSValue {
146146

147-
public func instanceof(_ constructor: String) -> Bool {
148-
147+
public func instanceof(_ constructor: JSFunctionRef) -> Bool {
148+
149149
switch self {
150150
case .boolean, .string, .number:
151151
return false

Sources/JavaScriptKit/JSValueConvertible.swift

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import _CJavaScriptKit
22

33
public protocol JSBridgedType: JSValueCodable, CustomStringConvertible {
4+
static var constructor: JSFunctionRef? { get }
5+
46
var objectRef: JSObjectRef { get }
57
init(objectRef: JSObjectRef)
68
}
79

10+
extension JSBridgedType {
11+
public var description: String {
12+
return objectRef.toString!().fromJSValue()
13+
}
14+
}
15+
816
public protocol JSValueEncodable {
917
subscript(jsValue _: ()) -> JSValue { get }
1018
}
@@ -15,19 +23,17 @@ public protocol JSValueDecodable {
1523
static func canDecode(from jsValue: JSValue) -> Bool
1624
}
1725

18-
extension JSBridgedType {
19-
20-
public var description: String {
21-
return objectRef.toString!().fromJSValue()
22-
}
23-
}
24-
2526
public typealias JSValueCodable = JSValueEncodable & JSValueDecodable
2627

2728
extension JSBridgedType {
28-
2929
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
3137
}
3238

3339
public init(jsValue: JSValue) {
@@ -130,24 +136,29 @@ extension String: JSValueCodable {
130136
}
131137

132138
extension JSObjectRef: JSValueCodable {
133-
134139
// `JSObjectRef.jsValue` is defined in JSObjectRef.swift to be able to overridden
135140
// from `JSFunctionRef`
136141
}
137142

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!
141144

145+
extension Dictionary: JSValueEncodable where Value == JSValueEncodable, Key == String {
142146
public subscript(jsValue _: ()) -> JSValue {
143-
let object = Object(.new)
147+
let object = JSObject(.new)
144148
for (key, value) in self {
145149
object[key] = JSValue(from: value)
146150
}
147151
return .object(object)
148152
}
149153
}
150154

155+
extension Dictionary where Value: JSValueEncodable, Key == String {
156+
public func jsValue() -> JSValue {
157+
Dictionary<Key, JSValueEncodable>.jsValue(self)()
158+
}
159+
}
160+
161+
151162
extension Dictionary: JSValueDecodable where Value: JSValueDecodable, Key == String {
152163

153164
public static func canDecode(from jsValue: JSValue) -> Bool {
@@ -158,7 +169,7 @@ extension Dictionary: JSValueDecodable where Value: JSValueDecodable, Key == Str
158169

159170
let objectRef: JSObjectRef = jsValue.object!
160171

161-
let keys: [String] = Object.keys!(JSValue(from: objectRef.jsValue)).fromJSValue()
172+
let keys: [String] = JSObject.keys!(JSValue(from: objectRef.jsValue)).fromJSValue()
162173
self = Dictionary(uniqueKeysWithValues: keys.map({
163174
return ($0, objectRef[dynamicMember: $0].fromJSValue())
164175
}))
@@ -193,8 +204,7 @@ extension Optional: JSValueEncodable where Wrapped: JSValueEncodable {
193204

194205
private let JSArray = JSObjectRef.global.Array.function!
195206

196-
extension Array: JSValueEncodable where Element: JSValueEncodable {
197-
207+
extension Array: JSValueEncodable where Element == JSValueEncodable {
198208

199209
public subscript(jsValue _: ()) -> JSValue {
200210
let array = JSArray(new: count)
@@ -205,6 +215,12 @@ extension Array: JSValueEncodable where Element: JSValueEncodable {
205215
}
206216
}
207217

218+
extension Array where Element: JSValueEncodable {
219+
public func jsValue() -> JSValue {
220+
Array<JSValueEncodable>.jsValue(self)()
221+
}
222+
}
223+
208224
extension Array: JSValueDecodable where Element: JSValueDecodable {
209225

210226
public static func canDecode(from jsValue: JSValue) -> Bool {
@@ -318,6 +334,6 @@ extension Array where Element == JSValueEncodable {
318334

319335
extension Array where Element: JSValueEncodable {
320336
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
321-
Swift.Array<JSValueEncodable>.withRawJSValues(self)(body)
337+
Array<JSValueEncodable>.withRawJSValues(self)(body)
322338
}
323339
}

Sources/JavaScriptKit/Support.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
extension UInt8: JSValueEncodable, JSValueDecodable {
32

43
public static func canDecode(from jsValue: JSValue) -> Bool {
@@ -234,10 +233,9 @@ public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type {
234233
return Type(objectRef: ref.objectRef)
235234
}
236235

237-
//public func dynamicCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
238-
//
239-
// guard ref.objectRef.instanceof(String(describing: Type.self)) else {
240-
// return nil
241-
// }
242-
// return staticCast(ref)
243-
//}
236+
public func dynamicCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
237+
guard let constructor = Type.constructor, JSObjectRef.instanceof(ref.objectRef, constructor: constructor) else {
238+
return nil
239+
}
240+
return staticCast(ref)
241+
}

0 commit comments

Comments
 (0)