-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add JSBridgedType and JSBridgedClass #26
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
b123600
Changes related to WebIDL support.
mjburghard e6b6705
Rename constructor
j-f1 5982bef
wip
j-f1 a5ce5e4
Add JSAbstractBridgedType
j-f1 0f79f43
More updates
j-f1 fbf9f76
Move & rename JS*BridgedType
j-f1 e31a989
wip
j-f1 e4d657e
Update JSTypedArray.swift
j-f1 c07db3d
Update JSTypedArray.swift
j-f1 be1f916
Update JSTypedArray.swift
j-f1 ba3316e
Update JSTypedArray.swift
j-f1 5fe4b96
Allow JSBridgedType (but not JSBridgedClass) to hold non-objects
j-f1 b62b78e
Make JS{Array,Date,Error} conform to JSBridgedClass
j-f1 8b181c7
Clean up AnyJSValueConvertible
j-f1 de5f52e
Docs for JSTypedArray
j-f1 479ad20
Docs for JSValueCoable-adjacent types
j-f1 b4edc19
Update based on review suggestions
j-f1 cabda58
Remove AnyJSValueConvertible
j-f1 d0da61a
Merge branch 'master' into webidl
j-f1 e19788a
Remove is* helpers
j-f1 ff1f4ac
withCompatibleObject → unsafelyWrapping
j-f1 c74e3d1
Apply suggestions from code review
j-f1 ed9f8d4
Rename Object and Array constructors
j-f1 29a30e7
Document & fix the behavior of isInstanceOf
j-f1 4429d88
Merge branch 'master' into webidl
j-f1 767d05d
Fix indent
j-f1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/// Use this protocol when your type has no single JavaScript class. | ||
/// For example, a union type of multiple classes or primitive values. | ||
public protocol JSBridgedType: JSValueCodable, CustomStringConvertible { | ||
/// This is the value your class wraps. | ||
var value: JSValue { get } | ||
|
||
/// If your class is incompatible with the provided value, return `nil`. | ||
init?(from value: JSValue) | ||
} | ||
|
||
extension JSBridgedType { | ||
public static func construct(from value: JSValue) -> Self? { | ||
return Self.init(from: value) | ||
} | ||
|
||
public func jsValue() -> JSValue { value } | ||
|
||
public var description: String { value.description } | ||
} | ||
|
||
/// Conform to this protocol when your Swift class wraps a JavaScript class. | ||
public protocol JSBridgedClass: JSBridgedType { | ||
/// The constructor function for the JavaScript class | ||
static var constructor: JSFunction { get } | ||
|
||
/// The JavaScript object wrapped by this instance. | ||
/// You may assume that `jsObject instanceof Self.constructor == true` | ||
var jsObject: JSObject { get } | ||
|
||
/// Create an instannce wrapping the given JavaScript object. | ||
/// You may assume that `jsObject instanceof Self.constructor` | ||
init(unsafelyWrapping jsObject: JSObject) | ||
} | ||
|
||
extension JSBridgedClass { | ||
public var value: JSValue { jsObject.jsValue() } | ||
public init?(from value: JSValue) { | ||
guard let object = value.object, object.isInstanceOf(Self.constructor) else { return nil } | ||
self.init(unsafelyWrapping: object) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this comment give an example of such type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Would it be OK to link to DOMKit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Here’s an example: https://github.com/swiftwasm/DOMKit/blob/main/Sources/DOMKit/WebIDL/FileOrString.swift)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please do 👍