From dc1f09b7cd4022e67504c4b66634c81f459bc8e4 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 1 May 2025 12:36:00 +0100 Subject: [PATCH 1/8] Fix `JavaScriptEventLoop` not building with Embedded Swift The change fixes some issues in the JavaScriptKit library when build with Embedded Swift support. Specifically, `@MainActor` type is not available in Embedded Swift, thus `Atomic` type is used instead. Similarly, existential types are not available either, so they're replaced with concrete `some` types and generics. --- .../JavaScriptEventLoop.swift | 30 +++++++++++++++++-- .../BasicObjects/JSPromise.swift | 20 ++++++------- .../FundamentalObjects/JSClosure.swift | 3 +- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift index 8fccea7d..df302030 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift @@ -3,6 +3,10 @@ import _Concurrency import _CJavaScriptEventLoop import _CJavaScriptKit +#if hasFeature(Embedded) +import Synchronization +#endif + // NOTE: `@available` annotations are semantically wrong, but they make it easier to develop applications targeting WebAssembly in Xcode. #if compiler(>=5.5) @@ -105,7 +109,12 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { return eventLoop } - @MainActor private static var didInstallGlobalExecutor = false + #if !hasFeature(Embedded) + @MainActor + private static var didInstallGlobalExecutor = false + #else + private static let didInstallGlobalExecutor = Atomic(false) + #endif /// Set JavaScript event loop based executor to be the global executor /// Note that this should be called before any of the jobs are created. @@ -113,13 +122,26 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { /// introduced officially. See also [a draft proposal for custom /// executors](https://github.com/rjmccall/swift-evolution/blob/custom-executors/proposals/0000-custom-executors.md#the-default-global-concurrent-executor) public static func installGlobalExecutor() { + #if !hasFeature(Embedded) MainActor.assumeIsolated { Self.installGlobalExecutorIsolated() } + #else + Self.installGlobalExecutorIsolated() + #endif } - @MainActor private static func installGlobalExecutorIsolated() { + #if !hasFeature(Embedded) + @MainActor + #endif + private static func installGlobalExecutorIsolated() { + #if !hasFeature(Embedded) guard !didInstallGlobalExecutor else { return } + #else + guard !didInstallGlobalExecutor.load(ordering: .sequentiallyConsistent) else { + return + } + #endif #if compiler(>=5.9) typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) ( @@ -189,7 +211,11 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { to: UnsafeMutableRawPointer?.self ) + #if !hasFeature(Embedded) didInstallGlobalExecutor = true + #else + didInstallGlobalExecutor.store(true, ordering: .sequentiallyConsistent) + #endif } private func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) { diff --git a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift index 7502bb5f..505be1a2 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift @@ -84,10 +84,9 @@ public final class JSPromise: JSBridgedClass { } #endif - #if !hasFeature(Embedded) /// Schedules the `success` closure to be invoked on successful completion of `self`. @discardableResult - public func then(success: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise { + public func then(success: @escaping (JSValue) -> some ConvertibleToJSValue) -> JSPromise { let closure = JSOneshotClosure { success($0[0]).jsValue } @@ -98,7 +97,7 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `success` closure to be invoked on successful completion of `self`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult - public func then(success: sending @escaping (sending JSValue) async throws -> ConvertibleToJSValue) -> JSPromise { + public func then(success: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue) -> JSPromise { let closure = JSOneshotClosure.async { try await success($0[0]).jsValue } @@ -109,8 +108,8 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `success` closure to be invoked on successful completion of `self`. @discardableResult public func then( - success: @escaping (sending JSValue) -> ConvertibleToJSValue, - failure: @escaping (sending JSValue) -> ConvertibleToJSValue + success: @escaping (sending JSValue) -> some ConvertibleToJSValue, + failure: @escaping (sending JSValue) -> some ConvertibleToJSValue ) -> JSPromise { let successClosure = JSOneshotClosure { success($0[0]).jsValue @@ -126,8 +125,8 @@ public final class JSPromise: JSBridgedClass { @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult public func then( - success: sending @escaping (sending JSValue) async throws -> ConvertibleToJSValue, - failure: sending @escaping (sending JSValue) async throws -> ConvertibleToJSValue + success: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue, + failure: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue ) -> JSPromise { let successClosure = JSOneshotClosure.async { try await success($0[0]).jsValue @@ -141,7 +140,9 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `failure` closure to be invoked on rejected completion of `self`. @discardableResult - public func `catch`(failure: @escaping (sending JSValue) -> ConvertibleToJSValue) -> JSPromise { + public func `catch`(failure: @escaping (sending JSValue) -> some ConvertibleToJSValue) + -> JSPromise + { let closure = JSOneshotClosure { failure($0[0]).jsValue } @@ -152,7 +153,7 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `failure` closure to be invoked on rejected completion of `self`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult - public func `catch`(failure: sending @escaping (sending JSValue) async throws -> ConvertibleToJSValue) -> JSPromise + public func `catch`(failure: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue) -> JSPromise { let closure = JSOneshotClosure.async { try await failure($0[0]).jsValue @@ -171,5 +172,4 @@ public final class JSPromise: JSBridgedClass { } return .init(unsafelyWrapping: jsObject.finally!(closure).object!) } - #endif } diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift index fa713c3b..8436d006 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift @@ -1,4 +1,5 @@ import _CJavaScriptKit +import _Concurrency /// `JSClosureProtocol` wraps Swift closure objects for use in JavaScript. Conforming types /// are responsible for managing the lifetime of the closure they wrap, but can delegate that @@ -40,7 +41,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol { fatalError("JSOneshotClosure does not support dictionary literal initialization") } - #if compiler(>=5.5) && !hasFeature(Embedded) + #if compiler(>=5.5) @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public static func async(_ body: sending @escaping (sending [JSValue]) async throws -> JSValue) -> JSOneshotClosure { From 6bd0492f6ebe42aa303dd41aee7de09c24489c18 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 20:37:28 +0800 Subject: [PATCH 2/8] Unify Embedded and non-Embedded code paths for `didInstallGlobalExecutor` --- .../JavaScriptEventLoop.swift | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift index df302030..385ba362 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift @@ -2,10 +2,7 @@ import JavaScriptKit import _Concurrency import _CJavaScriptEventLoop import _CJavaScriptKit - -#if hasFeature(Embedded) import Synchronization -#endif // NOTE: `@available` annotations are semantically wrong, but they make it easier to develop applications targeting WebAssembly in Xcode. @@ -109,12 +106,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { return eventLoop } - #if !hasFeature(Embedded) - @MainActor - private static var didInstallGlobalExecutor = false - #else private static let didInstallGlobalExecutor = Atomic(false) - #endif /// Set JavaScript event loop based executor to be the global executor /// Note that this should be called before any of the jobs are created. @@ -122,26 +114,13 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { /// introduced officially. See also [a draft proposal for custom /// executors](https://github.com/rjmccall/swift-evolution/blob/custom-executors/proposals/0000-custom-executors.md#the-default-global-concurrent-executor) public static func installGlobalExecutor() { - #if !hasFeature(Embedded) - MainActor.assumeIsolated { - Self.installGlobalExecutorIsolated() - } - #else Self.installGlobalExecutorIsolated() - #endif } - #if !hasFeature(Embedded) - @MainActor - #endif private static func installGlobalExecutorIsolated() { - #if !hasFeature(Embedded) - guard !didInstallGlobalExecutor else { return } - #else guard !didInstallGlobalExecutor.load(ordering: .sequentiallyConsistent) else { return } - #endif #if compiler(>=5.9) typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) ( @@ -211,11 +190,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { to: UnsafeMutableRawPointer?.self ) - #if !hasFeature(Embedded) - didInstallGlobalExecutor = true - #else didInstallGlobalExecutor.store(true, ordering: .sequentiallyConsistent) - #endif } private func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) { From 12f6fb6b9107921c3335be22004ec9bcae8bd732 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 20:39:58 +0800 Subject: [PATCH 3/8] Fix test case compilation where `then` block returns nothing --- Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift b/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift index 1da56e68..fc6b4584 100644 --- a/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift +++ b/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift @@ -171,7 +171,7 @@ final class JavaScriptEventLoopTests: XCTestCase { 100 ) } - let failingPromise2 = failingPromise.then { _ in + let failingPromise2 = failingPromise.then { _ -> JSValue in throw MessageError("Should not be called", file: #file, line: #line, column: #column) } failure: { err in return err From 7a6fdd9ce41796056272ebebfdb2732f2c0ff049 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 20:40:59 +0800 Subject: [PATCH 4/8] ./Utilities/format.swift --- Sources/JavaScriptKit/BasicObjects/JSPromise.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift index 505be1a2..34d28e15 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift @@ -97,7 +97,9 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `success` closure to be invoked on successful completion of `self`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult - public func then(success: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue) -> JSPromise { + public func then( + success: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue + ) -> JSPromise { let closure = JSOneshotClosure.async { try await success($0[0]).jsValue } @@ -140,7 +142,9 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `failure` closure to be invoked on rejected completion of `self`. @discardableResult - public func `catch`(failure: @escaping (sending JSValue) -> some ConvertibleToJSValue) + public func `catch`( + failure: @escaping (sending JSValue) -> some ConvertibleToJSValue + ) -> JSPromise { let closure = JSOneshotClosure { @@ -153,8 +157,9 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `failure` closure to be invoked on rejected completion of `self`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult - public func `catch`(failure: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue) -> JSPromise - { + public func `catch`( + failure: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue + ) -> JSPromise { let closure = JSOneshotClosure.async { try await failure($0[0]).jsValue } From 84af891f52a9995c52a16afea97bffe88e501c52 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 20:56:56 +0800 Subject: [PATCH 5/8] Avoid using `Synchronization` in the JavaScriptEventLoop It required us to update the minimum deployment target but it's not worth doing so just for this. --- Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift index 385ba362..d7394a0d 100644 --- a/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift +++ b/Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift @@ -2,7 +2,6 @@ import JavaScriptKit import _Concurrency import _CJavaScriptEventLoop import _CJavaScriptKit -import Synchronization // NOTE: `@available` annotations are semantically wrong, but they make it easier to develop applications targeting WebAssembly in Xcode. @@ -106,7 +105,7 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { return eventLoop } - private static let didInstallGlobalExecutor = Atomic(false) + private nonisolated(unsafe) static var didInstallGlobalExecutor = false /// Set JavaScript event loop based executor to be the global executor /// Note that this should be called before any of the jobs are created. @@ -118,9 +117,8 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { } private static func installGlobalExecutorIsolated() { - guard !didInstallGlobalExecutor.load(ordering: .sequentiallyConsistent) else { - return - } + guard !didInstallGlobalExecutor else { return } + didInstallGlobalExecutor = true #if compiler(>=5.9) typealias swift_task_asyncMainDrainQueue_hook_Fn = @convention(thin) ( @@ -189,8 +187,6 @@ public final class JavaScriptEventLoop: SerialExecutor, @unchecked Sendable { swift_task_enqueueMainExecutor_hook_impl, to: UnsafeMutableRawPointer?.self ) - - didInstallGlobalExecutor.store(true, ordering: .sequentiallyConsistent) } private func enqueue(_ job: UnownedJob, withDelay nanoseconds: UInt64) { From 5eed2c645874d87018bf8e954cc72b3ab69bb088 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 20:58:43 +0800 Subject: [PATCH 6/8] Use `JSValue` instead for `JSPromise`'s closure return types Returning `some ConvertibleToJSValue` was not consistent with `JSClosure` initializers, which always return `JSValue`. Also it emits `Capture of non-sendable type '(some ConvertibleToJSValue).Type' in an isolated closure` for some reasons. --- .../JavaScriptKit/BasicObjects/JSPromise.swift | 16 ++++++++-------- .../JSPromiseTests.swift | 6 +++--- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift index 34d28e15..36124b10 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift @@ -86,7 +86,7 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `success` closure to be invoked on successful completion of `self`. @discardableResult - public func then(success: @escaping (JSValue) -> some ConvertibleToJSValue) -> JSPromise { + public func then(success: @escaping (JSValue) -> JSValue) -> JSPromise { let closure = JSOneshotClosure { success($0[0]).jsValue } @@ -98,7 +98,7 @@ public final class JSPromise: JSBridgedClass { @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult public func then( - success: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue + success: sending @escaping (sending JSValue) async throws -> JSValue ) -> JSPromise { let closure = JSOneshotClosure.async { try await success($0[0]).jsValue @@ -110,8 +110,8 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `success` closure to be invoked on successful completion of `self`. @discardableResult public func then( - success: @escaping (sending JSValue) -> some ConvertibleToJSValue, - failure: @escaping (sending JSValue) -> some ConvertibleToJSValue + success: @escaping (sending JSValue) -> JSValue, + failure: @escaping (sending JSValue) -> JSValue ) -> JSPromise { let successClosure = JSOneshotClosure { success($0[0]).jsValue @@ -127,8 +127,8 @@ public final class JSPromise: JSBridgedClass { @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult public func then( - success: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue, - failure: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue + success: sending @escaping (sending JSValue) async throws -> JSValue, + failure: sending @escaping (sending JSValue) async throws -> JSValue ) -> JSPromise { let successClosure = JSOneshotClosure.async { try await success($0[0]).jsValue @@ -143,7 +143,7 @@ public final class JSPromise: JSBridgedClass { /// Schedules the `failure` closure to be invoked on rejected completion of `self`. @discardableResult public func `catch`( - failure: @escaping (sending JSValue) -> some ConvertibleToJSValue + failure: @escaping (sending JSValue) -> JSValue ) -> JSPromise { @@ -158,7 +158,7 @@ public final class JSPromise: JSBridgedClass { @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult public func `catch`( - failure: sending @escaping (sending JSValue) async throws -> some ConvertibleToJSValue + failure: sending @escaping (sending JSValue) async throws -> JSValue ) -> JSPromise { let closure = JSOneshotClosure.async { try await failure($0[0]).jsValue diff --git a/Tests/JavaScriptEventLoopTests/JSPromiseTests.swift b/Tests/JavaScriptEventLoopTests/JSPromiseTests.swift index 962b0442..c3429e8c 100644 --- a/Tests/JavaScriptEventLoopTests/JSPromiseTests.swift +++ b/Tests/JavaScriptEventLoopTests/JSPromiseTests.swift @@ -9,14 +9,14 @@ final class JSPromiseTests: XCTestCase { p1 = p1.then { value in XCTAssertEqual(value, .null) continuation.resume() - return JSValue.number(1.0) + return JSValue.number(1.0).jsValue } } await withCheckedContinuation { continuation in p1 = p1.then { value in XCTAssertEqual(value, .number(1.0)) continuation.resume() - return JSPromise.resolve(JSValue.boolean(true)) + return JSPromise.resolve(JSValue.boolean(true)).jsValue } } await withCheckedContinuation { continuation in @@ -48,7 +48,7 @@ final class JSPromiseTests: XCTestCase { p2 = p2.then { value in XCTAssertEqual(value, .boolean(true)) continuation.resume() - return JSPromise.reject(JSValue.number(2.0)) + return JSPromise.reject(JSValue.number(2.0)).jsValue } } await withCheckedContinuation { continuation in From 5b407039650b7e632be588c0bc0e6e8c9ab50b13 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 21:03:13 +0800 Subject: [PATCH 7/8] Fix test case compilation for `then` returning String --- Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift b/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift index fc6b4584..866b3945 100644 --- a/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift +++ b/Tests/JavaScriptEventLoopTests/JavaScriptEventLoopTests.swift @@ -151,7 +151,7 @@ final class JavaScriptEventLoopTests: XCTestCase { } let promise2 = promise.then { result in try await Task.sleep(nanoseconds: 100_000_000) - return String(result.number!) + return .string(String(result.number!)) } let thenDiff = try await measureTime { let result = try await promise2.value From 697f06bdf820460867b577c66eef29c31b05b70d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 1 May 2025 21:28:29 +0800 Subject: [PATCH 8/8] Use _Concurrency module only if non-Embedded or Embedded on WASI --- Sources/JavaScriptKit/BasicObjects/JSPromise.swift | 6 +++--- Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift index 36124b10..f0ef6da9 100644 --- a/Sources/JavaScriptKit/BasicObjects/JSPromise.swift +++ b/Sources/JavaScriptKit/BasicObjects/JSPromise.swift @@ -93,7 +93,7 @@ public final class JSPromise: JSBridgedClass { return JSPromise(unsafelyWrapping: jsObject.then!(closure).object!) } - #if compiler(>=5.5) + #if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI)) /// Schedules the `success` closure to be invoked on successful completion of `self`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult @@ -122,7 +122,7 @@ public final class JSPromise: JSBridgedClass { return JSPromise(unsafelyWrapping: jsObject.then!(successClosure, failureClosure).object!) } - #if compiler(>=5.5) + #if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI)) /// Schedules the `success` closure to be invoked on successful completion of `self`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult @@ -153,7 +153,7 @@ public final class JSPromise: JSBridgedClass { return .init(unsafelyWrapping: jsObject.catch!(closure).object!) } - #if compiler(>=5.5) + #if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI)) /// Schedules the `failure` closure to be invoked on rejected completion of `self`. @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) @discardableResult diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift index 8436d006..7aaba9ed 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift @@ -1,5 +1,7 @@ import _CJavaScriptKit +#if hasFeature(Embedded) && os(WASI) import _Concurrency +#endif /// `JSClosureProtocol` wraps Swift closure objects for use in JavaScript. Conforming types /// are responsible for managing the lifetime of the closure they wrap, but can delegate that @@ -41,7 +43,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol { fatalError("JSOneshotClosure does not support dictionary literal initialization") } - #if compiler(>=5.5) + #if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI)) @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public static func async(_ body: sending @escaping (sending [JSValue]) async throws -> JSValue) -> JSOneshotClosure { @@ -133,7 +135,7 @@ public class JSClosure: JSFunction, JSClosureProtocol { fatalError("JSClosure does not support dictionary literal initialization") } - #if compiler(>=5.5) && !hasFeature(Embedded) + #if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI)) @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) public static func async(_ body: @Sendable @escaping (sending [JSValue]) async throws -> JSValue) -> JSClosure { JSClosure(makeAsyncClosure(body)) @@ -149,7 +151,7 @@ public class JSClosure: JSFunction, JSClosureProtocol { #endif } -#if compiler(>=5.5) && !hasFeature(Embedded) +#if compiler(>=5.5) && (!hasFeature(Embedded) || os(WASI)) @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) private func makeAsyncClosure( _ body: sending @escaping (sending [JSValue]) async throws -> JSValue