Skip to content

Commit fbf9f76

Browse files
committed
Move & rename JS*BridgedType
1 parent 0f79f43 commit fbf9f76

File tree

3 files changed

+46
-44
lines changed

3 files changed

+46
-44
lines changed
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Use this protocol when your type has no single JavaScript class.
2+
// For example, a union type of multiple classes.
3+
public protocol JSBridgedType: JSValueCodable, CustomStringConvertible {
4+
var objectRef: JSObject { get }
5+
init?(objectRef: JSObject)
6+
static func canDecode(_ object: JSObject) -> Bool
7+
}
8+
9+
extension JSBridgedType {
10+
public static func construct(from value: JSValue) -> Self? {
11+
guard let object = value.object, canDecode(object) else {
12+
return nil
13+
}
14+
return Self.init(objectRef: object)
15+
}
16+
17+
public func jsValue() -> JSValue {
18+
.object(objectRef)
19+
}
20+
21+
public var description: String {
22+
return objectRef.toString!().fromJSValue()!
23+
}
24+
}
25+
26+
27+
public protocol JSBridgedClass: JSBridgedType {
28+
static var classRef: JSFunction { get }
29+
}
30+
31+
extension JSBridgedClass {
32+
public static func canDecode(from jsValue: JSValue) -> Bool {
33+
jsValue.isInstanceOf(Self.classRef)
34+
}
35+
}
36+
37+
public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
38+
return Type(objectRef: ref.objectRef)
39+
}
40+
41+
public func dynamicCast<Type: JSBridgedClass>(_ ref: JSBridgedClass) -> Type? {
42+
guard ref.objectRef.isInstanceOf(Type.classRef) else {
43+
return nil
44+
}
45+
return staticCast(ref)
46+
}

Sources/JavaScriptKit/JSValueConvertible.swift

-33
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,11 @@
11
import _CJavaScriptKit
22

3-
// Use this protocol when your type has no single JavaScript class.
4-
// For example, a union type of multiple classes.
5-
public protocol JSAbstractBridgedType: JSValueCodable, CustomStringConvertible {
6-
var objectRef: JSObject { get }
7-
init(objectRef: JSObject)
8-
}
9-
10-
extension JSAbstractBridgedType {
11-
public var description: String {
12-
return objectRef.toString!().fromJSValue()!
13-
}
14-
}
15-
16-
public protocol JSBridgedType: JSAbstractBridgedType {
17-
static var classRef: JSFunction { get }
18-
}
19-
203
public protocol JSValueConvertible {
214
func jsValue() -> JSValue
225
}
236

247
public typealias JSValueCodable = JSValueConvertible & JSValueConstructible
258

26-
extension JSAbstractBridgedType {
27-
public init(jsValue: JSValue) {
28-
self.init(objectRef: jsValue.object!)
29-
}
30-
31-
public func jsValue() -> JSValue {
32-
.object(objectRef)
33-
}
34-
}
35-
36-
extension JSBridgedType {
37-
public static func canDecode(from jsValue: JSValue) -> Bool {
38-
jsValue.isInstanceOf(Self.classRef)
39-
}
40-
}
41-
429
extension JSValue: JSValueCodable {
4310
public static func construct(from value: JSValue) -> Self? {
4411
return value

Sources/JavaScriptKit/Support.swift

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

0 commit comments

Comments
 (0)