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
Changes from all commits
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
4 changes: 2 additions & 2 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
@@ -286,7 +286,7 @@ try test("Object Conversion") {
try expectEqual(jsArray1[1], .number(2))
try expectEqual(jsArray1[2], .number(3))

let array2: [JSValueConvertible] = [1, "str", false]
let array2: [ConvertibleToJSValue] = [1, "str", false]
let jsArray2 = array2.jsValue().object!
try expectEqual(jsArray2.length, .number(3))
try expectEqual(jsArray2[0], .number(1))
@@ -298,7 +298,7 @@ try test("Object Conversion") {

try expectEqual(jsArray2[4], .object(jsArray1))

let dict1: [String: JSValueConvertible] = [
let dict1: [String: ConvertibleToJSValue] = [
"prop1": 1,
"prop2": "foo",
]
18 changes: 9 additions & 9 deletions Sources/JavaScriptKit/BasicObjects/JSPromise.swift
Original file line number Diff line number Diff line change
@@ -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

@@ -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.
*/
@@ -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.
*/
@@ -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(
@@ -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
@@ -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
@@ -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
@@ -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
2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift
Original file line number Diff line number Diff line change
@@ -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 }
}
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.
///
@@ -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
}
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
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 JSValueCompatible = ConvertibleToJSValue & ConstructibleFromJSValue

extension JSValue: JSValueCodable {
extension JSValue: JSValueCompatible {
public static func construct(from value: JSValue) -> Self? {
return value
}

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) }
}

extension JSObject: JSValueCodable {
extension JSObject: JSValueCompatible {
// `JSObject.jsValue` is defined in JSObject.swift to be able to overridden
// from `JSFunction`
}

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 {
@@ -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,
@@ -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:
@@ -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
@@ -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() {
@@ -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,
@@ -175,7 +175,7 @@ extension Array: JSValueConstructible where Element: JSValueConstructible {
}
}

extension RawJSValue: JSValueConvertible {
extension RawJSValue: ConvertibleToJSValue {
public func jsValue() -> JSValue {
switch kind {
case .invalid:
@@ -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) }
@@ -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)
}
}
9 changes: 9 additions & 0 deletions Sources/JavaScriptKit/Deprecated.swift
Original file line number Diff line number Diff line change
@@ -6,3 +6,12 @@ public typealias JSArrayRef = JSArray

@available(*, deprecated, renamed: "JSFunction")
public typealias JSFunctionRef = JSFunction

@available(*, deprecated, renamed: "ConvertibleToJSValue")
public typealias JSValueConvertible = ConvertibleToJSValue

@available(*, deprecated, renamed: "ConstructibleFromJSValue")
public typealias JSValueConstructible = ConstructibleFromJSValue

@available(*, deprecated, renamed: "JSValueCompatible")
public typealias JSValueCodable = JSValueCompatible
8 changes: 4 additions & 4 deletions Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ public class JSFunction: JSObject {
/// - arguments: Arguments to be passed to this function.
/// - Returns: The result of this call.
@discardableResult
public func callAsFunction(this: JSObject? = nil, arguments: [JSValueConvertible]) -> JSValue {
public func callAsFunction(this: JSObject? = nil, arguments: [ConvertibleToJSValue]) -> JSValue {
let result = arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
let argv = bufferPointer.baseAddress
@@ -42,7 +42,7 @@ public class JSFunction: JSObject {

/// A variadic arguments version of `callAsFunction`.
@discardableResult
public func callAsFunction(this: JSObject? = nil, _ arguments: JSValueConvertible...) -> JSValue {
public func callAsFunction(this: JSObject? = nil, _ arguments: ConvertibleToJSValue...) -> JSValue {
self(this: this, arguments: arguments)
}

@@ -56,7 +56,7 @@ public class JSFunction: JSObject {
///
/// - Parameter arguments: Arguments to be passed to this constructor function.
/// - Returns: A new instance of this constructor.
public func new(arguments: [JSValueConvertible]) -> JSObject {
public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
arguments.withRawJSValues { rawValues in
rawValues.withUnsafeBufferPointer { bufferPointer in
let argv = bufferPointer.baseAddress
@@ -69,7 +69,7 @@ public class JSFunction: JSObject {
}

/// A variadic arguments version of `new`.
public func new(_ arguments: JSValueConvertible...) -> JSObject {
public func new(_ arguments: ConvertibleToJSValue...) -> JSObject {
new(arguments: arguments)
}

8 changes: 4 additions & 4 deletions Sources/JavaScriptKit/FundamentalObjects/JSObject.swift
Original file line number Diff line number Diff line change
@@ -32,17 +32,17 @@ public class JSObject: Equatable {
/// - Parameter name: The name of this object's member to access.
/// - Returns: The `name` member method binding this object as `this` context.
@_disfavoredOverload
public subscript(_ name: String) -> ((JSValueConvertible...) -> JSValue)? {
public subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)? {
guard let function = self[name].function else { return nil }
return { (arguments: JSValueConvertible...) in
return { (arguments: ConvertibleToJSValue...) in
function(this: self, arguments: arguments)
}
}

/// A convenience method of `subscript(_ name: String) -> ((JSValueConvertible...) -> JSValue)?`
/// A convenience method of `subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)?`
/// to access the member through Dynamic Member Lookup.
@_disfavoredOverload
public subscript(dynamicMember name: String) -> ((JSValueConvertible...) -> JSValue)? {
public subscript(dynamicMember name: String) -> ((ConvertibleToJSValue...) -> JSValue)? {
self[name]
}

2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/JSBridgedType.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// 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 {
public protocol JSBridgedType: JSValueCompatible, CustomStringConvertible {
/// This is the value your class wraps.
var value: JSValue { get }

2 changes: 1 addition & 1 deletion Sources/JavaScriptKit/JSValue.swift
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ public enum JSValue: Equatable {
}

extension JSValue {
public func fromJSValue<Type>() -> Type? where Type: JSValueConstructible {
public func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue {
return Type.construct(from: self)
}
}
8 changes: 4 additions & 4 deletions Sources/JavaScriptKit/JSValueDecoder.swift
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ private struct _KeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainerPr
try _decode(forKey: key).isNull
}

func decode<T>(_: T.Type, forKey key: Key) throws -> T where T: JSValueConstructible & Decodable {
func decode<T>(_: T.Type, forKey key: Key) throws -> T where T: ConstructibleFromJSValue & Decodable {
return try _throwTypeMismatchIfNil(forKey: key) { T.construct(from: $0) }
}

@@ -177,7 +177,7 @@ private struct _UnkeyedDecodingContainer: UnkeyedDecodingContainer {
return _currentValue().isNull
}

mutating func decode<T>(_: T.Type) throws -> T where T: JSValueConstructible & Decodable {
mutating func decode<T>(_: T.Type) throws -> T where T: ConstructibleFromJSValue & Decodable {
try _throwTypeMismatchIfNil { T.construct(from: $0) }
}

@@ -214,13 +214,13 @@ extension _Decoder: SingleValueDecodingContainer {
node.isNull
}

func decode<T>(_: T.Type) throws -> T where T: JSValueConstructible & Decodable {
func decode<T>(_: T.Type) throws -> T where T: ConstructibleFromJSValue & Decodable {
try _throwTypeMismatchIfNil { T.construct(from: $0) }
}

func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
let primitive = { (node: JSValue) -> T? in
guard let constructibleType = type as? JSValueConstructible.Type else {
guard let constructibleType = type as? ConstructibleFromJSValue.Type else {
return nil
}
return constructibleType.construct(from: node) as? T