Skip to content

Commit

Permalink
modernize to 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
gregcotten committed Oct 17, 2022
1 parent b7a2db9 commit 09fb31c
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 126 deletions.
1 change: 1 addition & 0 deletions .swift-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5.7
14 changes: 7 additions & 7 deletions Sources/NAPI/Class.swift
Original file line number Diff line number Diff line change
@@ -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) }
Expand All @@ -19,26 +19,26 @@ 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)
}

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
}
}
}
2 changes: 1 addition & 1 deletion Sources/NAPI/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
74 changes: 37 additions & 37 deletions Sources/NAPI/Function.swift
Original file line number Diff line number Diff line change
@@ -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)!

Expand All @@ -16,65 +16,65 @@ 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)
}

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<A: ValueConvertible>(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<A: ValueConvertible>(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<A: ValueConvertible, B: ValueConvertible>(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<A: ValueConvertible, B: ValueConvertible>(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<A: ValueConvertible>(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<A: ValueConvertible>(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<A: ValueConvertible, B: ValueConvertible>(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<A: ValueConvertible, B: ValueConvertible>(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)
Expand All @@ -83,8 +83,8 @@ extension Function {
guard status == napi_ok else { throw NAPI.Error(status) }
}

fileprivate func _call<Result: ValueConvertible>(_ env: napi_env, this: napi_value, args: [napi_value?]) throws -> Result {
let handle = try self.napiValue(env)
private func _call<Result: ValueConvertible>(_ 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
Expand All @@ -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<A: ValueConvertible>(_ env: napi_env, _ a: A) throws -> Void {
return try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)])
func call<A: ValueConvertible>(_ env: napi_env, _ a: A) throws -> Void {
try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)])
}

public func call<A: ValueConvertible, B: ValueConvertible>(_ 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<A: ValueConvertible, B: ValueConvertible>(_ 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<Result: ValueConvertible>(_ env: napi_env) throws -> Result {
return try _call(env, this: Value.undefined.napiValue(env), args: [])
func call<Result: ValueConvertible>(_ env: napi_env) throws -> Result {
try _call(env, this: Value.undefined.napiValue(env), args: [])
}

public func call<Result: ValueConvertible, A: ValueConvertible>(_ env: napi_env, _ a: A) throws -> Result {
return try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)])
func call<Result: ValueConvertible, A: ValueConvertible>(_ env: napi_env, _ a: A) throws -> Result {
try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env)])
}

public func call<Result: ValueConvertible, A: ValueConvertible, B: ValueConvertible>(_ 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<Result: ValueConvertible, A: ValueConvertible, B: ValueConvertible>(_ env: napi_env, _ a: A, _ b: B) throws -> Result {
try _call(env, this: Value.undefined.napiValue(env), args: [a.napiValue(env), b.napiValue(env)])
}
}
6 changes: 3 additions & 3 deletions Sources/NAPI/NAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions Sources/NAPI/Null.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Loading

0 comments on commit 09fb31c

Please sign in to comment.