-
-
Notifications
You must be signed in to change notification settings - Fork 51
Gracefully handle unavailable JSBridgedClass
#190
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
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
/// A wrapper around [the JavaScript Array class](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array) | ||
/// that exposes its properties in a type-safe and Swifty way. | ||
public class JSArray: JSBridgedClass { | ||
public static let constructor = JSObject.global.Array.function! | ||
public static let constructor = JSObject.global.Array.function | ||
|
||
static func isArray(_ object: JSObject) -> Bool { | ||
constructor.isArray!(object).boolean! | ||
constructor!.isArray!(object).boolean! | ||
} | ||
|
||
public let jsObject: JSObject | ||
|
@@ -94,8 +94,8 @@ private func getObjectValuesLength(_ object: JSObject) -> Int { | |
return Int(values.length.number!) | ||
} | ||
|
||
extension JSValue { | ||
public var array: JSArray? { | ||
public extension JSValue { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix applied by the formatter, as the rest of similar changes here. |
||
var array: JSArray? { | ||
object.flatMap(JSArray.init) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,32 +1,32 @@ | ||
/** A wrapper around the [JavaScript Date | ||
class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) that | ||
exposes its properties in a type-safe way. This doesn't 100% match the JS API, for example | ||
`getMonth`/`setMonth` etc accessor methods are converted to properties, but the rest of it matches | ||
in the naming. Parts of the JavaScript `Date` API that are not consistent across browsers and JS | ||
implementations are not exposed in a type-safe manner, you should access the underlying `jsObject` | ||
property if you need those. | ||
*/ | ||
/** A wrapper around the [JavaScript Date | ||
class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) that | ||
exposes its properties in a type-safe way. This doesn't 100% match the JS API, for example | ||
`getMonth`/`setMonth` etc accessor methods are converted to properties, but the rest of it matches | ||
in the naming. Parts of the JavaScript `Date` API that are not consistent across browsers and JS | ||
implementations are not exposed in a type-safe manner, you should access the underlying `jsObject` | ||
property if you need those. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix applied by the formatter. |
||
public final class JSDate: JSBridgedClass { | ||
/// The constructor function used to create new `Date` objects. | ||
public static let constructor = JSObject.global.Date.function! | ||
public static let constructor = JSObject.global.Date.function | ||
|
||
/// The underlying JavaScript `Date` object. | ||
public let jsObject: JSObject | ||
|
||
/** Creates a new instance of the JavaScript `Date` class with a given amount of milliseconds | ||
that passed since midnight 01 January 1970 UTC. | ||
*/ | ||
that passed since midnight 01 January 1970 UTC. | ||
*/ | ||
public init(millisecondsSinceEpoch: Double? = nil) { | ||
if let milliseconds = millisecondsSinceEpoch { | ||
jsObject = Self.constructor.new(milliseconds) | ||
jsObject = Self.constructor!.new(milliseconds) | ||
} else { | ||
jsObject = Self.constructor.new() | ||
jsObject = Self.constructor!.new() | ||
} | ||
} | ||
|
||
/** According to the standard, `monthIndex` is zero-indexed, where `11` is December. `day` | ||
represents a day of the month starting at `1`. | ||
*/ | ||
/** According to the standard, `monthIndex` is zero-indexed, where `11` is December. `day` | ||
represents a day of the month starting at `1`. | ||
*/ | ||
public init( | ||
year: Int, | ||
monthIndex: Int, | ||
|
@@ -36,7 +36,7 @@ public final class JSDate: JSBridgedClass { | |
seconds: Int = 0, | ||
milliseconds: Int = 0 | ||
) { | ||
jsObject = Self.constructor.new(year, monthIndex, day, hours, minutes, seconds, milliseconds) | ||
jsObject = Self.constructor!.new(year, monthIndex, day, hours, minutes, seconds, milliseconds) | ||
} | ||
|
||
public init(unsafelyWrapping jsObject: JSObject) { | ||
|
@@ -198,7 +198,7 @@ public final class JSDate: JSBridgedClass { | |
Int(jsObject.getTimezoneOffset!().number!) | ||
} | ||
|
||
/// Returns a string conforming to ISO 8601 that contains date and time, e.g. | ||
/// Returns a string conforming to ISO 8601 that contains date and time, e.g. | ||
/// `"2020-09-15T08:56:54.811Z"`. | ||
public func toISOString() -> String { | ||
jsObject.toISOString!().string! | ||
|
@@ -214,25 +214,25 @@ public final class JSDate: JSBridgedClass { | |
jsObject.toLocaleTimeString!().string! | ||
} | ||
|
||
/** Returns a string formatted according to | ||
[rfc7231](https://tools.ietf.org/html/rfc7231#section-7.1.1.1) and modified according to | ||
[ecma-262](https://www.ecma-international.org/ecma-262/10.0/index.html#sec-date.prototype.toutcstring), | ||
e.g. `Tue, 15 Sep 2020 09:04:40 GMT`. | ||
*/ | ||
/** Returns a string formatted according to | ||
[rfc7231](https://tools.ietf.org/html/rfc7231#section-7.1.1.1) and modified according to | ||
[ecma-262](https://www.ecma-international.org/ecma-262/10.0/index.html#sec-date.prototype.toutcstring), | ||
e.g. `Tue, 15 Sep 2020 09:04:40 GMT`. | ||
*/ | ||
public func toUTCString() -> String { | ||
jsObject.toUTCString!().string! | ||
} | ||
|
||
/** Number of milliseconds since midnight 01 January 1970 UTC to the present moment ignoring | ||
leap seconds. | ||
*/ | ||
/** Number of milliseconds since midnight 01 January 1970 UTC to the present moment ignoring | ||
leap seconds. | ||
*/ | ||
public static func now() -> Double { | ||
constructor.now!().number! | ||
constructor!.now!().number! | ||
} | ||
|
||
/** Number of milliseconds since midnight 01 January 1970 UTC to the given date ignoring leap | ||
seconds. | ||
*/ | ||
/** Number of milliseconds since midnight 01 January 1970 UTC to the given date ignoring leap | ||
seconds. | ||
*/ | ||
public func valueOf() -> Double { | ||
jsObject.valueOf!().number! | ||
} | ||
|
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
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.
For types like these where the global must be available, how do you feel about declaring
constructor
asJSFunction!
?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.
I tried this, but then you have to add an explicit
JSFunction?
type signature on top, which means you still have to unwrap with!
or?
wherever thisconstructor
is used. So after all, there's no benefit of unwrapping it and then wrapping back again intoJSFunction?
to satisfy the protocol requirement.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.
Ah, I had remembered the
Foo!
andFoo?
types being compatible with each other but it seems that isn’t the case (i.e. you can’t conform to a protocol with aFoo?
requirement using a member of typeFoo!
). So this seems like the best approach. 👍