|
1 | 1 | // Use this protocol when your type has no single JavaScript class.
|
2 | 2 | // For example, a union type of multiple classes.
|
3 | 3 | public protocol JSBridgedType: JSValueCodable, CustomStringConvertible {
|
4 |
| - var objectRef: JSObject { get } |
5 |
| - init?(objectRef: JSObject) |
| 4 | + var value: JSValue { get } |
| 5 | + init?(from value: JSValue) |
6 | 6 | }
|
7 | 7 |
|
8 | 8 | extension JSBridgedType {
|
9 | 9 | 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) |
12 | 11 | }
|
13 | 12 |
|
14 |
| - public func jsValue() -> JSValue { |
15 |
| - .object(objectRef) |
16 |
| - } |
| 13 | + public func jsValue() -> JSValue { value } |
17 | 14 |
|
18 |
| - public var description: String { |
19 |
| - return objectRef.toString!().fromJSValue()! |
20 |
| - } |
| 15 | + public var description: String { value.description } |
21 | 16 | }
|
22 | 17 |
|
23 | 18 |
|
24 | 19 | public protocol JSBridgedClass: JSBridgedType {
|
25 | 20 | static var classRef: JSFunction { get }
|
| 21 | + var objectRef: JSObject { get } |
26 | 22 | init(withCompatibleObject objectRef: JSObject)
|
27 | 23 | }
|
28 | 24 |
|
29 | 25 | 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) |
43 | 30 | }
|
44 |
| - return staticCast(ref) |
45 | 31 | }
|
0 commit comments