-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow JSBridgedType (but not JSBridgedClass) to hold non-objects
- Loading branch information
Showing
3 changed files
with
40 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,31 @@ | ||
// Use this protocol when your type has no single JavaScript class. | ||
// For example, a union type of multiple classes. | ||
public protocol JSBridgedType: JSValueCodable, CustomStringConvertible { | ||
var objectRef: JSObject { get } | ||
init?(objectRef: JSObject) | ||
var value: JSValue { get } | ||
init?(from value: JSValue) | ||
} | ||
|
||
extension JSBridgedType { | ||
public static func construct(from value: JSValue) -> Self? { | ||
guard let object = value.object else { return nil } | ||
return Self.init(objectRef: object) | ||
return Self.init(from: value) | ||
} | ||
|
||
public func jsValue() -> JSValue { | ||
.object(objectRef) | ||
} | ||
public func jsValue() -> JSValue { value } | ||
|
||
public var description: String { | ||
return objectRef.toString!().fromJSValue()! | ||
} | ||
public var description: String { value.description } | ||
} | ||
|
||
|
||
public protocol JSBridgedClass: JSBridgedType { | ||
static var classRef: JSFunction { get } | ||
var objectRef: JSObject { get } | ||
init(withCompatibleObject objectRef: JSObject) | ||
} | ||
|
||
extension JSBridgedClass { | ||
public init?(objectRef: JSObject) { | ||
guard objectRef.isInstanceOf(Self.classRef) else { return nil } | ||
self.init(withCompatibleObject: objectRef) | ||
} | ||
} | ||
|
||
public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? { | ||
return Type(objectRef: ref.objectRef) | ||
} | ||
|
||
public func dynamicCast<Type: JSBridgedClass>(_ ref: JSBridgedClass) -> Type? { | ||
guard ref.objectRef.isInstanceOf(Type.classRef) else { | ||
return nil | ||
public var value: JSValue { objectRef.jsValue() } | ||
public init?(from value: JSValue) { | ||
guard let object = value.object, object.isInstanceOf(Self.classRef) else { return nil } | ||
self.init(withCompatibleObject: object) | ||
} | ||
return staticCast(ref) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters