Skip to content

Rename JSValueConvertible/Constructible/Codable #88

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 4 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
18 changes: 9 additions & 9 deletions Sources/JavaScriptKit/BasicObjects/JSPromise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ executed.
If the actual `Promise` object in JavaScript environment lives longer than this `JSPromise`, it may
attempt to call a deallocated `JSClosure`.
*/
public final class JSPromise<Success, Failure>: JSValueConvertible, JSValueConstructible {
public final class JSPromise<Success, Failure>: ConvertibleToJSValue, ConstructibleFromJSValue {
/// The underlying JavaScript `Promise` object.
public let jsObject: JSObject

Expand Down Expand Up @@ -88,7 +88,7 @@ extension JSPromise where Success == (), Failure == Never {
}
}

extension JSPromise where Failure: JSValueConvertible {
extension JSPromise where Failure: ConvertibleToJSValue {
/** Creates a new `JSPromise` instance from a given `resolver` closure. `resolver` takes
two closure that your code should call to either resolve or reject this `JSPromise` instance.
*/
Expand All @@ -113,7 +113,7 @@ extension JSPromise where Failure: JSValueConvertible {
}
}

extension JSPromise where Success: JSValueConvertible, Failure: JSError {
extension JSPromise where Success: ConvertibleToJSValue, Failure: JSError {
/** Creates a new `JSPromise` instance from a given `resolver` closure. `resolver` takes
a closure that your code should call to either resolve or reject this `JSPromise` instance.
*/
Expand All @@ -138,7 +138,7 @@ extension JSPromise where Success: JSValueConvertible, Failure: JSError {
}
}

extension JSPromise where Success: JSValueConstructible {
extension JSPromise where Success: ConstructibleFromJSValue {
/** Schedules the `success` closure to be invoked on sucessful completion of `self`.
*/
public func then(
Expand All @@ -160,7 +160,7 @@ extension JSPromise where Success: JSValueConstructible {
closure invoked on sucessful completion of `self`. The returned promise will have a new
`Success` type equal to the return type of `success`.
*/
public func then<ResultType: JSValueConvertible>(
public func then<ResultType: ConvertibleToJSValue>(
success: @escaping (Success) -> ResultType,
file: StaticString = #file,
line: Int = #line
Expand All @@ -179,7 +179,7 @@ extension JSPromise where Success: JSValueConstructible {
closure invoked on sucessful completion of `self`. The returned promise will have a new type
equal to the return type of `success`.
*/
public func then<ResultSuccess: JSValueConvertible, ResultFailure: JSValueConstructible>(
public func then<ResultSuccess: ConvertibleToJSValue, ResultFailure: ConstructibleFromJSValue>(
success: @escaping (Success) -> JSPromise<ResultSuccess, ResultFailure>,
file: StaticString = #file,
line: Int = #line
Expand All @@ -195,12 +195,12 @@ extension JSPromise where Success: JSValueConstructible {
}
}

extension JSPromise where Failure: JSValueConstructible {
extension JSPromise where Failure: ConstructibleFromJSValue {
/** Returns a new promise created from chaining the current `self` promise with the `failure`
closure invoked on rejected completion of `self`. The returned promise will have a new `Success`
type equal to the return type of the callback, while the `Failure` type becomes `Never`.
*/
public func `catch`<ResultSuccess: JSValueConvertible>(
public func `catch`<ResultSuccess: ConvertibleToJSValue>(
failure: @escaping (Failure) -> ResultSuccess,
file: StaticString = #file,
line: Int = #line
Expand Down Expand Up @@ -236,7 +236,7 @@ extension JSPromise where Failure: JSValueConstructible {
closure invoked on rejected completion of `self`. The returned promise will have a new type
equal to the return type of `success`.
*/
public func `catch`<ResultSuccess: JSValueConvertible, ResultFailure: JSValueConstructible>(
public func `catch`<ResultSuccess: ConvertibleToJSValue, ResultFailure: ConstructibleFromJSValue>(
failure: @escaping (Failure) -> JSPromise<ResultSuccess, ResultFailure>,
file: StaticString = #file,
line: Int = #line
Expand Down
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import _CJavaScriptKit

/// A protocol that allows a Swift numeric type to be mapped to the JavaScript TypedArray that holds integers of its type
public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible {
public protocol TypedArrayElement: ConvertibleToJSValue, ConstructibleFromJSValue {
/// The constructor function for the TypedArray class for this particular kind of number
static var typedArrayClass: JSFunction { get }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Types conforming to this protocol can be constructed from `JSValue`.
public protocol JSValueConstructible {
public protocol ConstructibleFromJSValue {
/// Construct an instance of `Self`, if possible, from the given `JSValue`.
/// Return `nil` if the value is not compatible with the conforming Swift type.
///
Expand All @@ -8,91 +8,91 @@ public protocol JSValueConstructible {
static func construct(from value: JSValue) -> Self?
}

extension Bool: JSValueConstructible {
extension Bool: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Bool? {
value.boolean
}
}

extension String: JSValueConstructible {
extension String: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> String? {
value.string
}
}

extension Double: JSValueConstructible {
extension Double: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Double? {
return value.number
}
}

extension Float: JSValueConstructible {
extension Float: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Float? {
return value.number.map(Float.init)
}
}

extension Int: JSValueConstructible {
extension Int: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension Int8: JSValueConstructible {
extension Int8: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension Int16: JSValueConstructible {
extension Int16: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension Int32: JSValueConstructible {
extension Int32: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension Int64: JSValueConstructible {
extension Int64: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension UInt: JSValueConstructible {
extension UInt: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension UInt8: JSValueConstructible {
extension UInt8: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension UInt16: JSValueConstructible {
extension UInt16: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension UInt32: JSValueConstructible {
extension UInt32: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension UInt64: JSValueConstructible {
extension UInt64: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
value.number.map(Self.init)
}
}

extension JSString: JSValueConstructible {
extension JSString: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> JSString? {
value.jsString
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import _CJavaScriptKit

/// Objects that can be converted to a JavaScript value, preferably in a lossless manner.
public protocol JSValueConvertible {
public protocol ConvertibleToJSValue {
/// Create a JSValue that represents this object
func jsValue() -> JSValue
}

public typealias JSValueCodable = JSValueConvertible & JSValueConstructible
public typealias JSValueCodable = ConvertibleToJSValue & ConstructibleFromJSValue

extension JSValue: JSValueCodable {
public static func construct(from value: JSValue) -> Self? {
Expand All @@ -16,63 +16,63 @@ extension JSValue: JSValueCodable {
public func jsValue() -> JSValue { self }
}

extension Bool: JSValueConvertible {
extension Bool: ConvertibleToJSValue {
public func jsValue() -> JSValue { .boolean(self) }
}

extension Int: JSValueConvertible {
extension Int: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension UInt: JSValueConvertible {
extension UInt: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension Float: JSValueConvertible {
extension Float: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension Double: JSValueConvertible {
extension Double: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(self) }
}

extension String: JSValueConvertible {
extension String: ConvertibleToJSValue {
public func jsValue() -> JSValue { .string(JSString(self)) }
}

extension UInt8: JSValueConvertible {
extension UInt8: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension UInt16: JSValueConvertible {
extension UInt16: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension UInt32: JSValueConvertible {
extension UInt32: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension UInt64: JSValueConvertible {
extension UInt64: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension Int8: JSValueConvertible {
extension Int8: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension Int16: JSValueConvertible {
extension Int16: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension Int32: JSValueConvertible {
extension Int32: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension Int64: JSValueConvertible {
extension Int64: ConvertibleToJSValue {
public func jsValue() -> JSValue { .number(Double(self)) }
}

extension JSString: JSValueConvertible {
extension JSString: ConvertibleToJSValue {
public func jsValue() -> JSValue { .string(self) }
}

Expand All @@ -84,13 +84,13 @@ extension JSObject: JSValueCodable {
private let objectConstructor = JSObject.global.Object.function!
private let arrayConstructor = JSObject.global.Array.function!

extension Dictionary where Value: JSValueConvertible, Key == String {
extension Dictionary where Value: ConvertibleToJSValue, Key == String {
public func jsValue() -> JSValue {
Swift.Dictionary<Key, JSValueConvertible>.jsValue(self)()
Swift.Dictionary<Key, ConvertibleToJSValue>.jsValue(self)()
}
}

extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key == String {
extension Dictionary: ConvertibleToJSValue where Value == ConvertibleToJSValue, Key == String {
public func jsValue() -> JSValue {
let object = objectConstructor.new()
for (key, value) in self {
Expand All @@ -100,7 +100,7 @@ extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key
}
}

extension Dictionary: JSValueConstructible where Value: JSValueConstructible, Key == String {
extension Dictionary: ConstructibleFromJSValue where Value: ConstructibleFromJSValue, Key == String {
public static func construct(from value: JSValue) -> Self? {
guard
let objectRef = value.object,
Expand All @@ -119,7 +119,7 @@ extension Dictionary: JSValueConstructible where Value: JSValueConstructible, Ke
}
}

extension Optional: JSValueConstructible where Wrapped: JSValueConstructible {
extension Optional: ConstructibleFromJSValue where Wrapped: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> Self? {
switch value {
case .null, .undefined:
Expand All @@ -130,7 +130,7 @@ extension Optional: JSValueConstructible where Wrapped: JSValueConstructible {
}
}

extension Optional: JSValueConvertible where Wrapped: JSValueConvertible {
extension Optional: ConvertibleToJSValue where Wrapped: ConvertibleToJSValue {
public func jsValue() -> JSValue {
switch self {
case .none: return .null
Expand All @@ -139,13 +139,13 @@ extension Optional: JSValueConvertible where Wrapped: JSValueConvertible {
}
}

extension Array where Element: JSValueConvertible {
extension Array where Element: ConvertibleToJSValue {
public func jsValue() -> JSValue {
Array<JSValueConvertible>.jsValue(self)()
Array<ConvertibleToJSValue>.jsValue(self)()
}
}

extension Array: JSValueConvertible where Element == JSValueConvertible {
extension Array: ConvertibleToJSValue where Element == ConvertibleToJSValue {
public func jsValue() -> JSValue {
let array = arrayConstructor.new(count)
for (index, element) in enumerated() {
Expand All @@ -155,7 +155,7 @@ extension Array: JSValueConvertible where Element == JSValueConvertible {
}
}

extension Array: JSValueConstructible where Element: JSValueConstructible {
extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValue {
public static func construct(from value: JSValue) -> [Element]? {
guard
let objectRef = value.object,
Expand All @@ -175,7 +175,7 @@ extension Array: JSValueConstructible where Element: JSValueConstructible {
}
}

extension RawJSValue: JSValueConvertible {
extension RawJSValue: ConvertibleToJSValue {
public func jsValue() -> JSValue {
switch kind {
case .invalid:
Expand Down Expand Up @@ -231,10 +231,10 @@ extension JSValue {
}
}

extension Array where Element == JSValueConvertible {
extension Array where Element == ConvertibleToJSValue {
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
func _withRawJSValues<T>(
_ values: [JSValueConvertible], _ index: Int,
_ values: [ConvertibleToJSValue], _ index: Int,
_ results: inout [RawJSValue], _ body: ([RawJSValue]) -> T
) -> T {
if index == values.count { return body(results) }
Expand All @@ -248,8 +248,8 @@ extension Array where Element == JSValueConvertible {
}
}

extension Array where Element: JSValueConvertible {
extension Array where Element: ConvertibleToJSValue {
func withRawJSValues<T>(_ body: ([RawJSValue]) -> T) -> T {
[JSValueConvertible].withRawJSValues(self)(body)
[ConvertibleToJSValue].withRawJSValues(self)(body)
}
}
Loading