From 09fb31caaec7787b0f51542c1b799fd840b00191 Mon Sep 17 00:00:00 2001 From: Greg Cotten Date: Mon, 17 Oct 2022 13:55:51 -0700 Subject: [PATCH] modernize to 5.7 --- .swift-version | 1 + Sources/NAPI/Class.swift | 14 ++--- Sources/NAPI/Error.swift | 2 +- Sources/NAPI/Function.swift | 74 ++++++++++++------------- Sources/NAPI/NAPI.swift | 6 +- Sources/NAPI/Null.swift | 6 +- Sources/NAPI/PropertyDescriptor.swift | 56 +++++++++---------- Sources/NAPI/RunLoop.swift | 4 +- Sources/NAPI/Undefined.swift | 6 +- Sources/NAPI/Value.swift | 46 +++++++-------- Sources/NAPI/ValueConvertible.swift | 18 +++--- Sources/NAPI/Wrap.swift | 2 +- Tests/Sources/NAPITests/NAPITests.swift | 18 +++--- 13 files changed, 127 insertions(+), 126 deletions(-) create mode 100644 .swift-version diff --git a/.swift-version b/.swift-version new file mode 100644 index 0000000..760606e --- /dev/null +++ b/.swift-version @@ -0,0 +1 @@ +5.7 diff --git a/Sources/NAPI/Class.swift b/Sources/NAPI/Class.swift index f27108a..3e60f84 100644 --- a/Sources/NAPI/Class.swift +++ b/Sources/NAPI/Class.swift @@ -1,6 +1,6 @@ import NAPIC -fileprivate func defineClass(_ env: napi_env, named name: String, _ constructor: @escaping Callback, _ properties: [PropertyDescriptor]) throws -> napi_value { +private func defineClass(_ env: napi_env, named name: String, _ constructor: @escaping Callback, _ properties: [PropertyDescriptor]) throws -> napi_value { var result: napi_value? let nameData = name.data(using: .utf8)! let props = try properties.map { try $0.value(env) } @@ -19,7 +19,7 @@ fileprivate func defineClass(_ env: napi_env, named name: String, _ constructor: return result! } -fileprivate enum InternalClass { +private enum InternalClass { case swift(String, Callback, [PropertyDescriptor]) case javascript(napi_value) } @@ -27,18 +27,18 @@ fileprivate enum InternalClass { public class Class: ValueConvertible { fileprivate let value: InternalClass - public required init(_ env: napi_env, from: napi_value) throws { - self.value = .javascript(from) + public required init(_: napi_env, from: napi_value) throws { + value = .javascript(from) } public init(named name: String, _ constructor: @escaping Callback, _ properties: [PropertyDescriptor]) { - self.value = .swift(name, constructor, properties) + value = .swift(name, constructor, properties) } public func napiValue(_ env: napi_env) throws -> napi_value { switch value { - case .swift(let name, let constructor, let properties): return try defineClass(env, named: name, constructor, properties) - case .javascript(let value): return value + case let .swift(name, constructor, properties): return try defineClass(env, named: name, constructor, properties) + case let .javascript(value): return value } } } diff --git a/Sources/NAPI/Error.swift b/Sources/NAPI/Error.swift index 3835bde..b7815b0 100644 --- a/Sources/NAPI/Error.swift +++ b/Sources/NAPI/Error.swift @@ -56,7 +56,7 @@ extension Error { case .booleanExpected: return napi_throw_type_error(env, nil, "Expected boolean") case .arrayExpected: return napi_throw_type_error(env, nil, "Expected array") case .bigintExpected: return napi_throw_type_error(env, nil, "Expected BigInt") - default: return napi_throw_error(env, nil, self.localizedDescription) + default: return napi_throw_error(env, nil, localizedDescription) } } } diff --git a/Sources/NAPI/Function.swift b/Sources/NAPI/Function.swift index c083493..72eef71 100644 --- a/Sources/NAPI/Function.swift +++ b/Sources/NAPI/Function.swift @@ -1,6 +1,6 @@ import NAPIC -fileprivate func createFunction(_ env: napi_env, named name: String, _ function: @escaping Callback) throws -> napi_value { +private func createFunction(_ env: napi_env, named name: String, _ function: @escaping Callback) throws -> napi_value { var result: napi_value? let nameData = name.data(using: .utf8)! @@ -16,7 +16,7 @@ fileprivate func createFunction(_ env: napi_env, named name: String, _ function: return result! } -fileprivate enum InternalFunction { +private enum InternalFunction { case javascript(napi_value) case swift(String, Callback) } @@ -24,57 +24,57 @@ fileprivate enum InternalFunction { public class Function: ValueConvertible { fileprivate let value: InternalFunction - public required init(_ env: napi_env, from: napi_value) throws { - self.value = .javascript(from) + public required init(_: napi_env, from: napi_value) throws { + value = .javascript(from) } public init(named name: String, _ callback: @escaping Callback) { - self.value = .swift(name, callback) + value = .swift(name, callback) } public func napiValue(_ env: napi_env) throws -> napi_value { switch value { - case .swift(let name, let callback): return try createFunction(env, named: name, callback) - case .javascript(let value): return value + case let .swift(name, callback): return try createFunction(env, named: name, callback) + case let .javascript(value): return value } } } /* constructor overloads */ -extension Function { +public extension Function { /* (...) -> Void */ - public convenience init(named name: String, _ callback: @escaping () throws -> Void) { - self.init(named: name, { (_, _) in try callback(); return Value.undefined }) + convenience init(named name: String, _ callback: @escaping () throws -> Void) { + self.init(named: name) { _, _ in try callback(); return Value.undefined } } - public convenience init(named name: String, _ callback: @escaping (A) throws -> Void) { - self.init(named: name, { (env, args) in try callback(A(env, from: args.0)); return Value.undefined }) + convenience init(named name: String, _ callback: @escaping (A) throws -> Void) { + self.init(named: name) { env, args in try callback(A(env, from: args.0)); return Value.undefined } } - public convenience init(named name: String, _ callback: @escaping (A, B) throws -> Void) { - self.init(named: name, { (env, args) in try callback(A(env, from: args.0), B(env, from: args.1)); return Value.undefined }) + convenience init(named name: String, _ callback: @escaping (A, B) throws -> Void) { + self.init(named: name) { env, args in try callback(A(env, from: args.0), B(env, from: args.1)); return Value.undefined } } /* (env, ...) -> Void */ - public convenience init(named name: String, _ callback: @escaping (napi_env) throws -> Void) { - self.init(named: name, { (env, _) in try callback(env); return Value.undefined }) + convenience init(named name: String, _ callback: @escaping (napi_env) throws -> Void) { + self.init(named: name) { env, _ in try callback(env); return Value.undefined } } - public convenience init(named name: String, _ callback: @escaping (napi_env, A) throws -> Void) { - self.init(named: name, { (env, args) in try callback(env, A(env, from: args.0)); return Value.undefined }) + convenience init(named name: String, _ callback: @escaping (napi_env, A) throws -> Void) { + self.init(named: name) { env, args in try callback(env, A(env, from: args.0)); return Value.undefined } } - public convenience init(named name: String, _ callback: @escaping (napi_env, A, B) throws -> Void) { - self.init(named: name, { (env, args) in try callback(env, A(env, from: args.0), B(env, from: args.1)); return Value.undefined }) + convenience init(named name: String, _ callback: @escaping (napi_env, A, B) throws -> Void) { + self.init(named: name) { env, args in try callback(env, A(env, from: args.0), B(env, from: args.1)); return Value.undefined } } } /* call(...) */ -extension Function { - fileprivate func _call(_ env: napi_env, this: napi_value, args: [napi_value?]) throws -> Void { - let handle = try self.napiValue(env) +public extension Function { + private func _call(_ env: napi_env, this: napi_value, args: [napi_value?]) throws { + let handle = try napiValue(env) let status = args.withUnsafeBufferPointer { argsBytes in napi_call_function(env, this, handle, args.count, argsBytes.baseAddress, nil) @@ -83,8 +83,8 @@ extension Function { guard status == napi_ok else { throw NAPI.Error(status) } } - fileprivate func _call(_ env: napi_env, this: napi_value, args: [napi_value?]) throws -> Result { - let handle = try self.napiValue(env) + private func _call(_ env: napi_env, this: napi_value, args: [napi_value?]) throws -> Result { + let handle = try napiValue(env) var result: napi_value? let status = args.withUnsafeBufferPointer { argsBytes in @@ -98,29 +98,29 @@ extension Function { /* (...) -> Void */ - public func call(_ env: napi_env) throws -> Void { - return try _call(env, this: Value.undefined.napiValue(env), args: []) + func call(_ env: napi_env) throws { + try _call(env, this: Value.undefined.napiValue(env), args: []) } - public func call(_ env: napi_env, _ a: A) throws -> Void { - return try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)]) + func call(_ env: napi_env, _ a: A) throws -> Void { + try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)]) } - public func call(_ env: napi_env, _ a: A, _ b: B) throws -> Void { - return try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env), b.napiValue(env)]) + func call(_ env: napi_env, _ a: A, _ b: B) throws -> Void { + try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env), b.napiValue(env)]) } /* (...) -> ValueConvertible */ - public func call(_ env: napi_env) throws -> Result { - return try _call(env, this: Value.undefined.napiValue(env), args: []) + func call(_ env: napi_env) throws -> Result { + try _call(env, this: Value.undefined.napiValue(env), args: []) } - public func call(_ env: napi_env, _ a: A) throws -> Result { - return try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)]) + func call(_ env: napi_env, _ a: A) throws -> Result { + try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)]) } - public func call(_ env: napi_env, _ a: A, _ b: B) throws -> Result { - return try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env), b.napiValue(env)]) + func call(_ env: napi_env, _ a: A, _ b: B) throws -> Result { + try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env), b.napiValue(env)]) } } diff --git a/Sources/NAPI/NAPI.swift b/Sources/NAPI/NAPI.swift index 0a402fd..6794870 100644 --- a/Sources/NAPI/NAPI.swift +++ b/Sources/NAPI/NAPI.swift @@ -8,15 +8,15 @@ public func strictlyEquals(_ env: napi_env, lhs: napi_value, rhs: napi_value) th } public func strictlyEquals(_ env: napi_env, lhs: napi_value, rhs: ValueConvertible) throws -> Bool { - return try strictlyEquals(env, lhs: lhs, rhs: rhs.napiValue(env)) + try strictlyEquals(env, lhs: lhs, rhs: rhs.napiValue(env)) } public func strictlyEquals(_ env: napi_env, lhs: ValueConvertible, rhs: napi_value) throws -> Bool { - return try strictlyEquals(env, lhs: lhs.napiValue(env), rhs: rhs) + try strictlyEquals(env, lhs: lhs.napiValue(env), rhs: rhs) } public func strictlyEquals(_ env: napi_env, lhs: ValueConvertible, rhs: ValueConvertible) throws -> Bool { - return try strictlyEquals(env, lhs: lhs.napiValue(env), rhs: rhs.napiValue(env)) + try strictlyEquals(env, lhs: lhs.napiValue(env), rhs: rhs.napiValue(env)) } public func defineProperties(_ env: napi_env, _ object: napi_value, _ properties: [PropertyDescriptor]) throws { diff --git a/Sources/NAPI/Null.swift b/Sources/NAPI/Null.swift index 1c54811..8c12d78 100644 --- a/Sources/NAPI/Null.swift +++ b/Sources/NAPI/Null.swift @@ -24,12 +24,12 @@ public struct Null: ValueConvertible { extension Null: CustomStringConvertible { public var description: String { - return "null" + "null" } } extension Null: Equatable { - public static func ==(_: Null, _: Null) -> Bool { - return true + public static func == (_: Null, _: Null) -> Bool { + true } } diff --git a/Sources/NAPI/PropertyDescriptor.swift b/Sources/NAPI/PropertyDescriptor.swift index 3388169..4d1f684 100644 --- a/Sources/NAPI/PropertyDescriptor.swift +++ b/Sources/NAPI/PropertyDescriptor.swift @@ -1,7 +1,7 @@ -import NAPIC import Foundation +import NAPIC -fileprivate enum InternalPropertyDescriptor { +private enum InternalPropertyDescriptor { case method(String, Callback, napi_property_attributes) case value(String, ValueConvertible, napi_property_attributes) } @@ -9,89 +9,89 @@ fileprivate enum InternalPropertyDescriptor { public struct PropertyDescriptor { fileprivate let value: InternalPropertyDescriptor - fileprivate init(_ value: InternalPropertyDescriptor) { + private init(_ value: InternalPropertyDescriptor) { self.value = value } func value(_ env: napi_env) throws -> napi_property_descriptor { - switch self.value { - case .method(let name, let callback, let attributes): - let _name = try name.napiValue(env) - let data = CallbackData(callback: callback) - let dataPointer = Unmanaged.passRetained(data).toOpaque() - return napi_property_descriptor(utf8name: nil, name: _name, method: swiftNAPICallback, getter: nil, setter: nil, value: nil, attributes: attributes, data: dataPointer) - case .value(let name, let value, let attributes): - let _name = try name.napiValue(env) - let _value = try value.napiValue(env) - return napi_property_descriptor(utf8name: nil, name: _name, method: nil, getter: nil, setter: nil, value: _value, attributes: attributes, data: nil) + switch value { + case let .method(name, callback, attributes): + let _name = try name.napiValue(env) + let data = CallbackData(callback: callback) + let dataPointer = Unmanaged.passRetained(data).toOpaque() + return napi_property_descriptor(utf8name: nil, name: _name, method: swiftNAPICallback, getter: nil, setter: nil, value: nil, attributes: attributes, data: dataPointer) + case let .value(name, value, attributes): + let _name = try name.napiValue(env) + let _value = try value.napiValue(env) + return napi_property_descriptor(utf8name: nil, name: _name, method: nil, getter: nil, setter: nil, value: _value, attributes: attributes, data: nil) } } public static func value(_ name: String, _ value: ValueConvertible, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.value(name, value, attributes)) + .init(.value(name, value, attributes)) } public static func `class`(_ name: String, _ constructor: @escaping () throws -> This, _ properties: [PropertyDescriptor], attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.value(name, Class(named: name, { (env, args) in let native = try constructor(); try Wrap.wrap(env, jsObject: args.this, nativeObject: native); return nil }, properties), attributes)) + .init(.value(name, Class(named: name, { env, args in let native = try constructor(); try Wrap.wrap(env, jsObject: args.this, nativeObject: native); return nil }, properties), attributes)) } public static func `class`(_ name: String, _ constructor: @escaping (napi_env) throws -> This, _ properties: [PropertyDescriptor], attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.value(name, Class(named: name, { (env, args) in let native = try constructor(env); try Wrap.wrap(env, jsObject: args.this, nativeObject: native); return nil }, properties), attributes)) + .init(.value(name, Class(named: name, { env, args in let native = try constructor(env); try Wrap.wrap(env, jsObject: args.this, nativeObject: native); return nil }, properties), attributes)) } /* (...) -> Void */ public static func function(_ name: String, _ callback: @escaping () throws -> Void, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (_, _) in try callback(); return Value.undefined }, attributes)) + .init(.method(name, { _, _ in try callback(); return Value.undefined }, attributes)) } public static func function(_ name: String, _ callback: @escaping (A) throws -> Void, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in try callback(A(env, from: args.0)); return Value.undefined }, attributes)) + .init(.method(name, { env, args in try callback(A(env, from: args.0)); return Value.undefined }, attributes)) } public static func function(_ name: String, _ callback: @escaping (A, B) throws -> Void, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in try callback(A(env, from: args.0), B(env, from: args.1)); return Value.undefined }, attributes)) + .init(.method(name, { env, args in try callback(A(env, from: args.0), B(env, from: args.1)); return Value.undefined }, attributes)) } /* (...) -> ValueConvertible */ public static func function(_ name: String, _ callback: @escaping () throws -> ValueConvertible, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (_, _) in return try callback() }, attributes)) + .init(.method(name, { _, _ in try callback() }, attributes)) } public static func function(_ name: String, _ callback: @escaping (A) throws -> ValueConvertible, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in return try callback(A(env, from: args.0)) }, attributes)) + .init(.method(name, { env, args in try callback(A(env, from: args.0)) }, attributes)) } public static func function(_ name: String, _ callback: @escaping (A, B) throws -> ValueConvertible, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in return try callback(A(env, from: args.0), B(env, from: args.1)) }, attributes)) + .init(.method(name, { env, args in try callback(A(env, from: args.0), B(env, from: args.1)) }, attributes)) } /* (this, ...) -> Void */ public static func method(_ name: String, _ callback: @escaping (This) throws -> Void, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in try callback(Wrap.unwrap(env, jsObject: args.this)); return Value.undefined }, attributes)) + .init(.method(name, { env, args in try callback(Wrap.unwrap(env, jsObject: args.this)); return Value.undefined }, attributes)) } public static func method(_ name: String, _ callback: @escaping (This, A) throws -> Void, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0)); return Value.undefined }, attributes)) + .init(.method(name, { env, args in try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0)); return Value.undefined }, attributes)) } public static func method(_ name: String, _ callback: @escaping (This, A, B) throws -> Void, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0), B(env, from: args.1)); return Value.undefined }, attributes)) + .init(.method(name, { env, args in try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0), B(env, from: args.1)); return Value.undefined }, attributes)) } /* (this, ...) -> ValueConvertible */ public static func method(_ name: String, _ callback: @escaping (This) throws -> ValueConvertible, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in return try callback(Wrap.unwrap(env, jsObject: args.this)) }, attributes)) + .init(.method(name, { env, args in try callback(Wrap.unwrap(env, jsObject: args.this)) }, attributes)) } public static func method(_ name: String, _ callback: @escaping (This, A) throws -> ValueConvertible, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in return try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0)) }, attributes)) + .init(.method(name, { env, args in try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0)) }, attributes)) } public static func method(_ name: String, _ callback: @escaping (This, A, B) throws -> ValueConvertible, attributes: napi_property_attributes = napi_default) -> PropertyDescriptor { - return .init(.method(name, { (env, args) in return try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0), B(env, from: args.1)) }, attributes)) + .init(.method(name, { env, args in try callback(Wrap.unwrap(env, jsObject: args.this), A(env, from: args.0), B(env, from: args.1)) }, attributes)) } } diff --git a/Sources/NAPI/RunLoop.swift b/Sources/NAPI/RunLoop.swift index 9e90054..0a97a8b 100644 --- a/Sources/NAPI/RunLoop.swift +++ b/Sources/NAPI/RunLoop.swift @@ -1,7 +1,7 @@ import Foundation import NAPIC -fileprivate func setTimeout(_ env: napi_env, _ fn: Function, _ ms: Double) throws { +private func setTimeout(_ env: napi_env, _ fn: Function, _ ms: Double) throws { var status: napi_status! var global: napi_value! @@ -15,7 +15,7 @@ fileprivate func setTimeout(_ env: napi_env, _ fn: Function, _ ms: Double) throw try Function(env, from: setTimeout).call(env, fn, ms) } -public class RunLoop { +public enum RunLoop { private static var refCount = 0 private static var scheduled = false diff --git a/Sources/NAPI/Undefined.swift b/Sources/NAPI/Undefined.swift index 215c771..897b6c5 100644 --- a/Sources/NAPI/Undefined.swift +++ b/Sources/NAPI/Undefined.swift @@ -24,12 +24,12 @@ public struct Undefined: ValueConvertible { extension Undefined: CustomStringConvertible { public var description: String { - return "undefined" + "undefined" } } extension Undefined: Equatable { - public static func ==(_: Undefined, _: Undefined) -> Bool { - return true + public static func == (_: Undefined, _: Undefined) -> Bool { + true } } diff --git a/Sources/NAPI/Value.swift b/Sources/NAPI/Value.swift index 5a931a4..31d4eae 100644 --- a/Sources/NAPI/Value.swift +++ b/Sources/NAPI/Value.swift @@ -1,12 +1,12 @@ -import NAPIC import Foundation +import NAPIC public protocol ErrorConvertible: Swift.Error { var message: String { get } var code: String? { get } } -fileprivate func throwError(_ env: napi_env, _ error: Swift.Error) throws { +private func throwError(_ env: napi_env, _ error: Swift.Error) throws { if let error = error as? NAPI.Error { let status = error.napi_throw(env) guard status == napi_ok else { throw NAPI.Error(status) } @@ -22,8 +22,8 @@ fileprivate func throwError(_ env: napi_env, _ error: Swift.Error) throws { } } -fileprivate func exceptionIsPending(_ env: napi_env) throws -> Bool { - var result: Bool = false +private func exceptionIsPending(_ env: napi_env) throws -> Bool { + var result = false let status = napi_is_exception_pending(env, &result) guard status == napi_ok else { throw NAPI.Error(status) } @@ -69,21 +69,21 @@ public enum Value: ValueConvertible { case null case undefined - public init(_ env: napi_env, from: napi_value) throws { + public init(_: napi_env, from _: napi_value) throws { fatalError("Not implemented") } public func napiValue(_ env: napi_env) throws -> napi_value { switch self { - case .class(let `class`): return try `class`.napiValue(env) - case .function(let function): return try function.napiValue(env) - case .object(let object): return try object.napiValue(env) - case .array(let array): return try array.napiValue(env) - case .string(let string): return try string.napiValue(env) - case .number(let number): return try number.napiValue(env) - case .boolean(let boolean): return try boolean.napiValue(env) - case .null: return try Null.default.napiValue(env) - case .undefined: return try Undefined.default.napiValue(env) + case let .class(`class`): return try `class`.napiValue(env) + case let .function(function): return try function.napiValue(env) + case let .object(object): return try object.napiValue(env) + case let .array(array): return try array.napiValue(env) + case let .string(string): return try string.napiValue(env) + case let .number(number): return try number.napiValue(env) + case let .boolean(boolean): return try boolean.napiValue(env) + case .null: return try Null.default.napiValue(env) + case .undefined: return try Undefined.default.napiValue(env) } } } @@ -113,15 +113,15 @@ extension Value: Decodable { extension Value: CustomStringConvertible { public var description: String { switch self { - case .class(_): return "[Function: ...]" - case .function(_): return "[Function: ...]" - case .object(let object): return "{ \(object.map({ "\($0): \($1)" }).joined(separator: ", "))) }" - case .array(let array): return "[ \(array.map({ String(describing: $0) }).joined(separator: ", ")) ]" - case .string(let string): return string - case .number(let number): return String(describing: number) - case .boolean(let boolean): return boolean ? "true" : "false" - case .null: return "null" - case .undefined: return "undefined" + case .class: return "[Function: ...]" + case .function: return "[Function: ...]" + case let .object(object): return "{ \(object.map { "\($0): \($1)" }.joined(separator: ", "))) }" + case let .array(array): return "[ \(array.map { String(describing: $0) }.joined(separator: ", ")) ]" + case let .string(string): return string + case let .number(number): return String(describing: number) + case let .boolean(boolean): return boolean ? "true" : "false" + case .null: return "null" + case .undefined: return "undefined" } } } diff --git a/Sources/NAPI/ValueConvertible.swift b/Sources/NAPI/ValueConvertible.swift index 3d9cdd9..125b00f 100644 --- a/Sources/NAPI/ValueConvertible.swift +++ b/Sources/NAPI/ValueConvertible.swift @@ -1,5 +1,5 @@ -import NAPIC import Foundation +import NAPIC public protocol ValueConvertible { init(_ env: napi_env, from: napi_value) throws @@ -18,16 +18,16 @@ extension Optional: ValueConvertible where Wrapped: ValueConvertible { return } - self = .some(try Wrapped.init(env, from: from)) + self = .some(try Wrapped(env, from: from)) } public func napiValue(_ env: napi_env) throws -> napi_value { - return try self?.napiValue(env) ?? Value.null.napiValue(env) + try self?.napiValue(env) ?? Value.null.napiValue(env) } } extension Dictionary: ValueConvertible where Key == String, Value: ValueConvertible { - public init(_ env: napi_env, from: napi_value) throws { + public init(_: napi_env, from _: napi_value) throws { fatalError("Not implemented") } @@ -48,7 +48,7 @@ extension Dictionary: ValueConvertible where Key == String, Value: ValueConverti } extension Array: ValueConvertible where Element: ValueConvertible { - public init(_ env: napi_env, from: napi_value) throws { + public init(_: napi_env, from _: napi_value) throws { fatalError("Not implemented") } @@ -56,10 +56,10 @@ extension Array: ValueConvertible where Element: ValueConvertible { var status: napi_status! var result: napi_value! - status = napi_create_array_with_length(env, self.count, &result) + status = napi_create_array_with_length(env, count, &result) guard status == napi_ok else { throw NAPI.Error(status) } - for (index, element) in self.enumerated() { + for (index, element) in enumerated() { status = napi_set_element(env, result, UInt32(index), try element.napiValue(env)) guard status == napi_ok else { throw NAPI.Error(status) } } @@ -71,7 +71,7 @@ extension Array: ValueConvertible where Element: ValueConvertible { extension String: ValueConvertible { public init(_ env: napi_env, from: napi_value) throws { var status: napi_status! - var length: Int = 0 + var length = 0 status = napi_get_value_string_utf8(env, from, nil, 0, &length) guard status == napi_ok else { throw NAPI.Error(status) } @@ -88,7 +88,7 @@ extension String: ValueConvertible { public func napiValue(_ env: napi_env) throws -> napi_value { var result: napi_value? - let data = self.data(using: .utf8)! + let data = data(using: .utf8)! let status = data.withUnsafeBytes { (bytes: UnsafePointer) in napi_create_string_utf8(env, bytes, data.count, &result) diff --git a/Sources/NAPI/Wrap.swift b/Sources/NAPI/Wrap.swift index 32e7d7b..0db7ae2 100644 --- a/Sources/NAPI/Wrap.swift +++ b/Sources/NAPI/Wrap.swift @@ -1,6 +1,6 @@ import NAPIC -func swiftNAPIDeinit(_ env: napi_env!, pointer: UnsafeMutableRawPointer?, hint: UnsafeMutableRawPointer?) { +func swiftNAPIDeinit(_: napi_env!, pointer: UnsafeMutableRawPointer?, hint _: UnsafeMutableRawPointer?) { Unmanaged.fromOpaque(pointer!).release() } diff --git a/Tests/Sources/NAPITests/NAPITests.swift b/Tests/Sources/NAPITests/NAPITests.swift index 12d2062..60a9091 100644 --- a/Tests/Sources/NAPITests/NAPITests.swift +++ b/Tests/Sources/NAPITests/NAPITests.swift @@ -12,23 +12,23 @@ func assertEqual(expected: T, actual: T) } func returnString() -> String { - return "a string" + "a string" } func returnNumber() -> Double { - return 1337 + 1337 } func returnBoolean() -> Bool { - return true + true } func returnNull() -> Value { - return .null + .null } func returnUndefined() -> Value { - return .undefined + .undefined } func takeString(value: String) throws { @@ -52,15 +52,15 @@ func takeUndefined(value: Undefined) throws { } func takeOptionalString(value: String?) -> String { - return value ?? "a string" + value ?? "a string" } func takeOptionalNumber(value: Double?) -> Double { - return value ?? 1337 + value ?? 1337 } func takeOptionalBoolean(value: Bool?) -> Bool { - return value ?? true + value ?? true } func throwError() throws { @@ -69,7 +69,7 @@ func throwError() throws { @_cdecl("_init_napi_tests") func initNAPITests(env: OpaquePointer, exports: OpaquePointer) -> OpaquePointer? { - return initModule(env, exports, [ + initModule(env, exports, [ .function("returnString", returnString), .function("returnNumber", returnNumber), .function("returnBoolean", returnBoolean),