Skip to content

Commit 745bb00

Browse files
committed
Allow JSBridgedType (but not JSBridgedClass) to hold non-objects
1 parent a87af4e commit 745bb00

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed
+10-13
Original file line numberDiff line numberDiff line change
@@ -1,34 +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: JSObjectRef { get }
5-
init?(objectRef: JSObjectRef)
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: JSFunctionRef { get }
21+
var objectRef: JSObjectRef { get }
2622
init(withCompatibleObject objectRef: JSObjectRef)
2723
}
2824

2925
extension JSBridgedClass {
30-
public init?(objectRef: JSObjectRef) {
31-
guard objectRef.isInstanceOf(Self.classRef) else { return nil }
32-
self.init(withCompatibleObject: objectRef)
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)
3330
}
3431
}

Sources/JavaScriptKit/JSValue.swift

+19
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,22 @@ extension JSValue {
156156
}
157157
}
158158
}
159+
160+
extension JSValue: CustomStringConvertible {
161+
public var description: String {
162+
switch self {
163+
case let .boolean(boolean):
164+
return boolean.description
165+
case .string(let string):
166+
return string
167+
case .number(let number):
168+
return number.description
169+
case .object(let object), .function(let object as JSObjectRef):
170+
return object.toString!().fromJSValue()!
171+
case .null:
172+
return "null"
173+
case .undefined:
174+
return "undefined"
175+
}
176+
}
177+
}

Sources/JavaScriptKit/Support.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct AnyJSValueCodable: JSValueCodable, ExpressibleByNilLiteral {
4646
}
4747

4848
public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
49-
return Type(objectRef: ref.objectRef)
49+
return Type(from: ref.value)
5050
}
5151

5252
public func dynamicCast<Type: JSBridgedClass>(_ ref: JSBridgedClass) -> Type? {

0 commit comments

Comments
 (0)