diff --git a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift index 821087b66..e54343db7 100644 --- a/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift +++ b/IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift @@ -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", ] diff --git a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift index a98945cd5..dd46ac47b 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift @@ -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: JSValueConvertible, JSValueConstructible { +public final class JSPromise: 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( + public func then( 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( + public func then( success: @escaping (Success) -> JSPromise, 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`( + public func `catch`( 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`( + public func `catch`( failure: @escaping (Failure) -> JSPromise, file: StaticString = #file, line: Int = #line diff --git a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift index 71c54e657..ce00cc736 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift @@ -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 } } diff --git a/Sources/JavaScriptKit/JSValueConstructible.swift b/Sources/JavaScriptKit/ConstructibleFromJSValue.swift similarity index 74% rename from Sources/JavaScriptKit/JSValueConstructible.swift rename to Sources/JavaScriptKit/ConstructibleFromJSValue.swift index e1bf3072e..063597a9e 100644 --- a/Sources/JavaScriptKit/JSValueConstructible.swift +++ b/Sources/JavaScriptKit/ConstructibleFromJSValue.swift @@ -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 } diff --git a/Sources/JavaScriptKit/JSValueConvertible.swift b/Sources/JavaScriptKit/ConvertibleToJSValue.swift similarity index 76% rename from Sources/JavaScriptKit/JSValueConvertible.swift rename to Sources/JavaScriptKit/ConvertibleToJSValue.swift index 63162f5f3..7917c32cb 100644 --- a/Sources/JavaScriptKit/JSValueConvertible.swift +++ b/Sources/JavaScriptKit/ConvertibleToJSValue.swift @@ -1,14 +1,14 @@ 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 } @@ -16,67 +16,67 @@ 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) } } -extension JSObject: JSValueCodable { +extension JSObject: JSValueCompatible { // `JSObject.jsValue` is defined in JSObject.swift to be able to overridden // from `JSFunction` } @@ -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.jsValue(self)() + Swift.Dictionary.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.jsValue(self)() + Array.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(_ body: ([RawJSValue]) -> T) -> T { func _withRawJSValues( - _ 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(_ body: ([RawJSValue]) -> T) -> T { - [JSValueConvertible].withRawJSValues(self)(body) + [ConvertibleToJSValue].withRawJSValues(self)(body) } } diff --git a/Sources/JavaScriptKit/Deprecated.swift b/Sources/JavaScriptKit/Deprecated.swift index 69306ac5f..0d81888c9 100644 --- a/Sources/JavaScriptKit/Deprecated.swift +++ b/Sources/JavaScriptKit/Deprecated.swift @@ -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 diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift index bd232d9a7..03edf7da5 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift @@ -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) } diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift index 1b324805d..63b1b75ae 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift @@ -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] } diff --git a/Sources/JavaScriptKit/JSBridgedType.swift b/Sources/JavaScriptKit/JSBridgedType.swift index d47be81ab..029a26663 100644 --- a/Sources/JavaScriptKit/JSBridgedType.swift +++ b/Sources/JavaScriptKit/JSBridgedType.swift @@ -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 } diff --git a/Sources/JavaScriptKit/JSValue.swift b/Sources/JavaScriptKit/JSValue.swift index 696e3c617..00f79e1c8 100644 --- a/Sources/JavaScriptKit/JSValue.swift +++ b/Sources/JavaScriptKit/JSValue.swift @@ -80,7 +80,7 @@ public enum JSValue: Equatable { } extension JSValue { - public func fromJSValue() -> Type? where Type: JSValueConstructible { + public func fromJSValue() -> Type? where Type: ConstructibleFromJSValue { return Type.construct(from: self) } } diff --git a/Sources/JavaScriptKit/JSValueDecoder.swift b/Sources/JavaScriptKit/JSValueDecoder.swift index 0c6daf824..c70dd8e27 100644 --- a/Sources/JavaScriptKit/JSValueDecoder.swift +++ b/Sources/JavaScriptKit/JSValueDecoder.swift @@ -113,7 +113,7 @@ private struct _KeyedDecodingContainer: KeyedDecodingContainerPr try _decode(forKey: key).isNull } - func decode(_: T.Type, forKey key: Key) throws -> T where T: JSValueConstructible & Decodable { + func decode(_: 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.Type) throws -> T where T: JSValueConstructible & Decodable { + mutating func decode(_: 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.Type) throws -> T where T: JSValueConstructible & Decodable { + func decode(_: T.Type) throws -> T where T: ConstructibleFromJSValue & Decodable { try _throwTypeMismatchIfNil { T.construct(from: $0) } } func decode(_ 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