-
-
Notifications
You must be signed in to change notification settings - Fork 8
jsvalue becomes a struct, to align with actual JavaScriptKit #104
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| extension JSValue { | ||
| @frozen @usableFromInline enum Storage { | ||
| case boolean(Bool) | ||
| case string(JSString) | ||
| case number(Double) | ||
| case object(JSObject) | ||
| case null | ||
| case undefined | ||
| case symbol(JSSymbol) | ||
| case bigInt(JSBigInt) | ||
| } | ||
| } |
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,18 +1,40 @@ | ||
| import JSON | ||
|
|
||
| @frozen public enum JSValue { | ||
| case boolean(Bool) | ||
| case string(JSString) | ||
| case number(Double) | ||
| case object(JSObject) | ||
| case null | ||
| case undefined | ||
| case symbol(JSSymbol) | ||
| case bigInt(JSBigInt) | ||
| @frozen public struct JSValue { | ||
| @usableFromInline let storage: Storage | ||
| @inlinable init(storage: Storage) { | ||
| self.storage = storage | ||
| } | ||
| } | ||
| extension JSValue { | ||
| @inlinable public static func boolean(_ value: Bool) -> Self { | ||
| .init(storage: .boolean(value)) | ||
| } | ||
| @inlinable public static func string(_ value: JSString) -> Self { | ||
| .init(storage: .string(value)) | ||
| } | ||
| @inlinable public static func number(_ value: Double) -> Self { | ||
| .init(storage: .number(value)) | ||
| } | ||
| @inlinable public static func object(_ value: JSObject) -> Self { | ||
| .init(storage: .object(value)) | ||
| } | ||
| @inlinable public static var null: Self { | ||
| .init(storage: .null) | ||
| } | ||
| @inlinable public static var undefined: Self { | ||
| .init(storage: .undefined) | ||
| } | ||
| @inlinable public static func symbol(_ value: JSSymbol) -> Self { | ||
| .init(storage: .symbol(value)) | ||
| } | ||
| @inlinable public static func bigInt(_ value: JSBigInt) -> Self { | ||
| .init(storage: .bigInt(value)) | ||
| } | ||
| } | ||
| extension JSValue: JSONEncodable { | ||
| public func encode(to json: inout JSON) { | ||
| switch self { | ||
| switch self.storage { | ||
| case .null: | ||
| (nil as Never?).encode(to: &json) | ||
| case .boolean(let js): | ||
|
|
@@ -108,64 +130,66 @@ extension JSValue: ConvertibleToJSValue { | |
| @inlinable public var jsValue: JSValue { self } | ||
| } | ||
| extension JSValue { | ||
| @available( | ||
| *, unavailable, | ||
| message: "code that expects a numeric value should check for 'BigInt' as well" | ||
| ) @inlinable public var number: Double? { | ||
| guard case .number(let value) = self else { | ||
| @inlinable public var number: Double? { | ||
| guard case .number(let value) = self.storage else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| @available( | ||
| *, unavailable, | ||
| message: "code that expects a numeric value should check for 'Double' as well" | ||
| ) @inlinable public var bigInt: JSBigInt? { | ||
| guard case .bigInt(let value) = self else { | ||
| @inlinable public var bigInt: JSBigInt? { | ||
| guard case .bigInt(let value) = self.storage else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
| } | ||
| extension JSValue { | ||
| @inlinable public var jsString: JSString? { | ||
| guard case .string(let value) = self.storage else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
| } | ||
| extension JSValue { | ||
| @inlinable public var boolean: Bool? { | ||
| guard case .boolean(let value) = self else { | ||
| guard case .boolean(let value) = self.storage else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| @inlinable public var string: String? { | ||
| guard case .string(let value) = self else { | ||
| guard case .string(let value) = self.storage else { | ||
| return nil | ||
| } | ||
| return value.string | ||
| } | ||
|
|
||
| @inlinable public var object: JSObject? { | ||
| guard case .object(let value) = self else { | ||
| guard case .object(let value) = self.storage else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| @inlinable public var symbol: JSSymbol? { | ||
| guard case .symbol(let value) = self else { | ||
| guard case .symbol(let value) = self.storage else { | ||
| return nil | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| @inlinable public var isNull: Bool { | ||
| guard case .null = self else { | ||
| guard case .null = self.storage else { | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
Comment on lines
184
to
189
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. The implementation of @inlinable public var isNull: Bool {
switch self.storage {
case .null:
return true
default:
return false
}
} |
||
|
|
||
| @inlinable public var isUndefined: Bool { | ||
| guard case .undefined = self else { | ||
| guard case .undefined = self.storage else { | ||
| return false | ||
| } | ||
| return true | ||
|
|
||
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.
The
@available(*, unavailable, ...)attributes on thenumberandbigIntproperties have been removed. These attributes served an important purpose: they forced developers to consciously handle bothDoubleandJSBigIntrepresentations for numeric values, preventing potential bugs with large numbers that can't be represented asDouble. Removing these checks makes the API easier to misuse. If this change was intentional, it would be beneficial to document the rationale and guide users on correct handling of numeric types. If it was an oversight, please consider reintroducing a mechanism to ensure both numeric types are handled correctly.