Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSBridgedType and JSBridgedClass #26

Merged
merged 26 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename constructor
  • Loading branch information
j-f1 committed Sep 15, 2020
commit e6b6705993c801f9d1aa4471dc8b8686fcccc47b
8 changes: 2 additions & 6 deletions Sources/JavaScriptKit/JSValueConvertible.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _CJavaScriptKit

public protocol JSBridgedType: JSValueCodable, CustomStringConvertible {
static var constructor: JSFunction? { get }
static var classRef: JSFunction { get }

var objectRef: JSObject { get }
init(objectRef: JSObject)
Expand All @@ -21,11 +21,7 @@ public typealias JSValueCodable = JSValueConvertible & JSValueConstructible

extension JSBridgedType {
public static func canDecode(from jsValue: JSValue) -> Bool {
if let constructor = Self.constructor {
return jsValue.isInstanceOf(constructor)
} else {
return jsValue.isObject
}
jsValue.isInstanceOf(Self.classRef)
}

public init(jsValue: JSValue) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/Support.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public func staticCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type {
}

public func dynamicCast<Type: JSBridgedType>(_ ref: JSBridgedType) -> Type? {
guard let constructor = Type.constructor, ref.objectRef.isInstanceOf(constructor) else {
guard ref.objectRef.isInstanceOf(Type.classRef) else {
return nil
}
return staticCast(ref)
Expand Down