Skip to content

Commit ed9e4b7

Browse files
committed
Allow JSBridgedType (but not JSBridgedClass) to hold non-objects
1 parent b50a00f commit ed9e4b7

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed
+10-24
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,31 @@
11
// Use this protocol when your type has no single JavaScript class.
22
// For example, a union type of multiple classes.
33
public protocol JSBridgedType: JSValueCodable, CustomStringConvertible {
4-
var objectRef: JSObject { get }
5-
init?(objectRef: JSObject)
4+
var value: JSValue { get }
5+
init?(from value: JSValue)
66
}
77

88
extension JSBridgedType {
99
public static func construct(from value: JSValue) -> Self? {
10-
guard let object = value.object else { return nil }
11-
return Self.init(objectRef: object)
10+
return Self.init(from: value)
1211
}
1312

14-
public func jsValue() -> JSValue {
15-
.object(objectRef)
16-
}
13+
public func jsValue() -> JSValue { value }
1714

18-
public var description: String {
19-
return objectRef.toString!().fromJSValue()!
20-
}
15+
public var description: String { value.description }
2116
}
2217

2318

2419
public protocol JSBridgedClass: JSBridgedType {
2520
static var classRef: JSFunction { get }
21+
var objectRef: JSObject { get }
2622
init(withCompatibleObject objectRef: JSObject)
2723
}
2824

2925
extension JSBridgedClass {
30-
public init?(objectRef: JSObject) {
31-
guard objectRef.isInstanceOf(Self.classRef) else { return nil }
32-
self.init(withCompatibleObject: objectRef)
33-
}
34-
}
35-
36-
public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
37-
return Type(objectRef: ref.objectRef)
38-
}
39-
40-
public func dynamicCast<Type: JSBridgedClass>(_ ref: JSBridgedClass) -> Type? {
41-
guard ref.objectRef.isInstanceOf(Type.classRef) else {
42-
return nil
26+
public var value: JSValue { objectRef.jsValue() }
27+
public init?(from value: JSValue) {
28+
guard let object = value.object, object.isInstanceOf(Self.classRef) else { return nil }
29+
self.init(withCompatibleObject: object)
4330
}
44-
return staticCast(ref)
4531
}

Sources/JavaScriptKit/JSValue.swift

+19
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,22 @@ extension JSValue {
152152
}
153153
}
154154
}
155+
156+
extension JSValue: CustomStringConvertible {
157+
public var description: String {
158+
switch self {
159+
case let .boolean(boolean):
160+
return boolean.description
161+
case .string(let string):
162+
return string
163+
case .number(let number):
164+
return number.description
165+
case .object(let object), .function(let object as JSObject):
166+
return object.toString!().fromJSValue()!
167+
case .null:
168+
return "null"
169+
case .undefined:
170+
return "undefined"
171+
}
172+
}
173+
}

Sources/JavaScriptKit/Support.swift

+11
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ public struct AnyJSValueCodable: JSValueCodable, ExpressibleByNilLiteral {
4444
self.jsValue().fromJSValue()
4545
}
4646
}
47+
48+
public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
49+
return Type(from: ref.value)
50+
}
51+
52+
public func dynamicCast<Type: JSBridgedClass>(_ ref: JSBridgedClass) -> Type? {
53+
guard ref.objectRef.isInstanceOf(Type.classRef) else {
54+
return nil
55+
}
56+
return staticCast(ref)
57+
}

0 commit comments

Comments
 (0)