diff --git a/Sources/Napoli/Generated/ObjectReferenceMethods.generated.swift b/Sources/Napoli/Generated/ObjectReferenceMethods.generated.swift new file mode 100644 index 0000000..f8746ab --- /dev/null +++ b/Sources/Napoli/Generated/ObjectReferenceMethods.generated.swift @@ -0,0 +1,314 @@ +// swiftformat:disable opaqueGenericParameters + +import Foundation +import NAPIC + +// 9 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7, _ p8: P8) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible, P8: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction9 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5, p6, p7, p8) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7, _ p8: P8) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible, P8: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction9 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5, p6, p7, p8) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7, _ p8: P8) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible, P8: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction9 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5, p6, p7, p8) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7, _ p8: P8) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible, P8: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction9 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5, p6, p7, p8) + } +} + +// 8 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction8 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5, p6, p7) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction8 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5, p6, p7) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction8 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5, p6, p7) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6, _ p7: P7) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible, P7: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction8 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5, p6, p7) + } +} + +// 7 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction7 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5, p6) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction7 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5, p6) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction7 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5, p6) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5, _ p6: P6) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible, P6: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction7 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5, p6) + } +} + +// 6 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction6 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction6 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction6 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4, p5) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4, _ p5: P5) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible, P5: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction6 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4, p5) + } +} + +// 5 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction5 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction5 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction5 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3, p4) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3, _ p4: P4) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible, P4: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction5 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3, p4) + } +} + +// 4 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction4 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction4 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction4 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2, p3) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2, _ p3: P3) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible, P3: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction4 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2, p3) + } +} + +// 3 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction3 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction3 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction3 = try get(env, name) + return try function.call(env, this: self, p0, p1, p2) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1, _ p2: P2) throws where P0: ValueConvertible, P1: ValueConvertible, P2: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction3 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1, p2) + } +} + +// 2 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction2 = try get(env, name) + return try function.call(env, this: self, p0, p1) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1) throws -> Result where Result: ValueConvertible, P0: ValueConvertible, P1: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction2 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1) throws where P0: ValueConvertible, P1: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction2 = try get(env, name) + return try function.call(env, this: self, p0, p1) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0, _ p1: P1) throws where P0: ValueConvertible, P1: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction2 = try get(env, name) + return try function.call(env, this: Undefined.default, p0, p1) + } +} + +// 1 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0) throws -> Result where Result: ValueConvertible, P0: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction1 = try get(env, name) + return try function.call(env, this: self, p0) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0) throws -> Result where Result: ValueConvertible, P0: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction1 = try get(env, name) + return try function.call(env, this: Undefined.default, p0) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String, _ p0: P0) throws where P0: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction1 = try get(env, name) + return try function.call(env, this: self, p0) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String, _ p0: P0) throws where P0: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction1 = try get(env, name) + return try function.call(env, this: Undefined.default, p0) + } +} + +// 0 param methods +public extension ObjectReference { + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String) throws -> Result where Result: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction0 = try get(env, name) + return try function.call(env, this: self) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String) throws -> Result where Result: ValueConvertible { + let env = env ?? storedEnvironment + let function: TypedFunction0 = try get(env, name) + return try function.call(env, this: Undefined.default) + } + + @available(*, noasync) + func callThis(_ env: Environment? = nil, _ name: String) throws { + let env = env ?? storedEnvironment + let function: TypedFunction0 = try get(env, name) + return try function.call(env, this: self) + } + + @available(*, noasync) + func call(_ env: Environment? = nil, _ name: String) throws { + let env = env ?? storedEnvironment + let function: TypedFunction0 = try get(env, name) + return try function.call(env, this: Undefined.default) + } +} diff --git a/Sources/Napoli/Global.swift b/Sources/Napoli/Global.swift index 5903390..d6612cf 100644 --- a/Sources/Napoli/Global.swift +++ b/Sources/Napoli/Global.swift @@ -3,13 +3,15 @@ import NAPIC public extension Environment { final class Global: ObjectReference { - public convenience init(_ env: Environment) throws { + @available(*, noasync) + fileprivate convenience init(_ env: Environment) throws { var global: napi_value? try napi_get_global(env.env, &global).throwIfError() try self.init(env, from: global!) } } + @available(*, noasync) func global() throws -> Global { try Global(self) } @@ -17,18 +19,19 @@ public extension Environment { public extension Environment.Global { final class JSON: ObjectReference { - public func stringify(_ value: V) throws -> String { - let function: TypedFunction1 = try get(storedEnvironment, "stringify") - return try function.call(storedEnvironment, value) + @available(*, noasync) + public func stringify(env: Environment? = nil, _ value: V) throws -> String { + try call(env, "stringify", value) } - public func parse(_ string: String) throws -> V { - let function: TypedFunction1 = try get(storedEnvironment, "parse") - return try function.call(storedEnvironment, string) + @available(*, noasync) + public func parse(env: Environment? = nil, _ string: String) throws -> V { + try call(env, "parse", string) } } - func json() throws -> JSON { - try get(storedEnvironment, "JSON") + @available(*, noasync) + func json(_ env: Environment? = nil) throws -> JSON { + try get(env, "JSON") } } diff --git a/Sources/Napoli/Promise.swift b/Sources/Napoli/Promise.swift index 283a783..59a534a 100644 --- a/Sources/Napoli/Promise.swift +++ b/Sources/Napoli/Promise.swift @@ -9,22 +9,20 @@ private class JSPromise: ObjectReference { try attachDummyCatch(env) } + @available(*, noasync) func attachDummyCatch(_ env: Environment? = nil) throws { - let env = env ?? storedEnvironment - let catchFunction: TypedFunction1> = try get(env, "catch") - try catchFunction.call(env, this: self, .init(named: "dummy") { _, _ in - Null.default - }) + typealias CatchCallback = TypedFunction1 + try callThis(env, "catch", CatchCallback(named: "dummyCatch", { _, _ in })) } + @available(*, noasync) func then(_ env: Environment? = nil, onFulfilled: @escaping (Environment, V) -> Void, onReject: @escaping (Environment, JSError) -> Void) throws { - let env = env ?? storedEnvironment - let function: TypedFunction2, TypedFunction1> = try get(env, "then") - - try function.call(env, - this: self, - .init(named: "onFulfilled", onFulfilled), - .init(named: "onReject", onReject)) + typealias OnFullfillCallback = TypedFunction1 + typealias OnRejectCallback = TypedFunction1 + try callThis(env, + "then", + OnFullfillCallback(named: "onFulfilled", onFulfilled), + OnRejectCallback(named: "onReject", onReject)) } func getValue() async throws -> V { diff --git a/bin/codegen b/bin/codegen index 2493466..d5a4f01 100755 --- a/bin/codegen +++ b/bin/codegen @@ -1,5 +1,7 @@ #!/usr/bin/env bash +set -e + SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd)" cd "$SCRIPT_DIR/../codegen/" @@ -9,4 +11,4 @@ mkdir -p "${SCRIPT_DIR}/../Sources/Napoli/Generated" swift run codegen "${SCRIPT_DIR}/../Sources/Napoli/Generated" cd "${SCRIPT_DIR}/../Sources/Napoli/Generated" -swiftformat *.generated.swift \ No newline at end of file +swiftformat *.generated.swift diff --git a/codegen/Sources/codegen/Codegen.swift b/codegen/Sources/codegen/Codegen.swift index f7ae636..d43e10c 100644 --- a/codegen/Sources/codegen/Codegen.swift +++ b/codegen/Sources/codegen/Codegen.swift @@ -36,6 +36,7 @@ struct Codegen: ParsableCommand { try ThreadsafeTypedFunction.generate(maxParams: params).result().write(to: generatedFile(named: "ThreadsafeTypedFunction"), atomically: true, encoding: .utf8) try Method.generate(maxParams: params).result().write(to: generatedFile(named: "MethodDescriptor"), atomically: true, encoding: .utf8) try InstanceMethod.generate(maxParams: params).result().write(to: generatedFile(named: "InstanceMethodDescriptor"), atomically: true, encoding: .utf8) + try ObjectReferenceMethods.generate(maxParams: params).result().write(to: generatedFile(named: "ObjectReferenceMethods"), atomically: true, encoding: .utf8) } } diff --git a/codegen/Sources/codegen/Generators/ObjectReferenceMethods.swift b/codegen/Sources/codegen/Generators/ObjectReferenceMethods.swift new file mode 100644 index 0000000..914885c --- /dev/null +++ b/codegen/Sources/codegen/Generators/ObjectReferenceMethods.swift @@ -0,0 +1,72 @@ +import Foundation + +enum ObjectReferenceMethods { + static func generate(maxParams: Int) throws -> Source { + var source = Source() + source.add(""" + // swiftformat:disable opaqueGenericParameters + + import Foundation + import NAPIC + """) + source.newline() + + for i in (0 ..< maxParams).reversed() { + try generateExtension(source: &source, paramCount: i) + } + + return source + } + + static func generateExtension(source: inout Source, paramCount: Int) throws { + let resultGeneric = Generic(type: "Result") + let inGenerics = [Generic](prefix: "P", count: paramCount) + let allGenerics = [resultGeneric] + inGenerics + let allWheres = allGenerics.conforming(to: Types.valueConvertible).value + let inWheres = inGenerics.conforming(to: Types.valueConvertible).value + + let commaSeparatedInGenerics = inGenerics.map(\.type).commaSeparated + + let inGenericsAsArgs: String + if paramCount > 0 { + inGenericsAsArgs = ", " + inGenerics.map { "_ \($0.type.lowercased()): \($0.type)" }.commaSeparated + } else { + inGenericsAsArgs = "" + } + + let inGenericsAsCallArgs = inGenerics.map { $0.type.lowercased() }.commaSeparated.prefixCommaIfNotEmpty() + + source.add(""" + // \(paramCount) param methods + public extension ObjectReference { + @available(*, noasync) + func callThis\(allGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws -> Result\(allWheres.backspaceIfNotEmpty()) { + let env = env ?? storedEnvironment + let function: TypedFunction\(paramCount)\(allGenerics.bracketedOrNone) = try get(env, name) + return try function.call(env, this: self\(inGenericsAsCallArgs)) + } + + @available(*, noasync) + func call\(allGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws -> Result\(allWheres.backspaceIfNotEmpty()) { + let env = env ?? storedEnvironment + let function: TypedFunction\(paramCount)\(allGenerics.bracketedOrNone) = try get(env, name) + return try function.call(env, this: Undefined.default\(inGenericsAsCallArgs)) + } + + @available(*, noasync) + func callThis\(inGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws \(inWheres.backspaceIfNotEmpty()) { + let env = env ?? storedEnvironment + let function: TypedFunction\(paramCount) = try get(env, name) + return try function.call(env, this: self\(inGenericsAsCallArgs)) + } + + @available(*, noasync) + func call\(inGenerics.bracketedOrNone)(_ env: Environment? = nil, _ name: String\(inGenericsAsArgs)) throws \(inWheres.backspaceIfNotEmpty()) { + let env = env ?? storedEnvironment + let function: TypedFunction\(paramCount) = try get(env, name) + return try function.call(env, this: Undefined.default\(inGenericsAsCallArgs)) + } + } + """) + } +} diff --git a/codegen/Sources/codegen/Generators/ThreadsafeTypedFunction.swift b/codegen/Sources/codegen/Generators/ThreadsafeTypedFunction.swift index 82e03cf..3d7f155 100644 --- a/codegen/Sources/codegen/Generators/ThreadsafeTypedFunction.swift +++ b/codegen/Sources/codegen/Generators/ThreadsafeTypedFunction.swift @@ -51,7 +51,8 @@ enum ThreadsafeTypedFunction { if try! exceptionIsPending(env) { var errorResult: napi_value! try! napi_get_and_clear_last_exception(env.env, &errorResult).throwIfError() - callbackData.continuation.resume(throwing: JSException(value: errorResult)) + let error = try! JSError(env, from: errorResult) + callbackData.continuation.resume(throwing: error) } else { callbackData.continuation.resume(throwing: error) } diff --git a/codegen/Sources/codegen/Generators/TypedFunction.swift b/codegen/Sources/codegen/Generators/TypedFunction.swift index 72e30bc..c772a8d 100644 --- a/codegen/Sources/codegen/Generators/TypedFunction.swift +++ b/codegen/Sources/codegen/Generators/TypedFunction.swift @@ -65,7 +65,7 @@ enum TypedFunction { } return try data.callback(env, this, usedArgs).napiValue(env) - } catch Napoli.Error.pendingException { + } catch NAPIError.pendingException { return nil } catch { if try! exceptionIsPending(env) == false { try! throwError(env, error) }