Skip to content

Fix some Embedded Swift issues in JavaScriptEventLoop #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/JavaScriptEventLoop/JSSending.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import _Concurrency
@_spi(JSObject_id) import JavaScriptKit
import _CJavaScriptKit

Expand Down
21 changes: 11 additions & 10 deletions Sources/JavaScriptEventLoop/JavaScriptEventLoop.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import JavaScriptKit
import _Concurrency
import _CJavaScriptEventLoop
import _CJavaScriptKit

Expand Down Expand Up @@ -259,38 +260,38 @@ extension JavaScriptEventLoop {
extension JSPromise {
/// Wait for the promise to complete, returning (or throwing) its result.
public var value: JSValue {
get async throws {
try await withUnsafeThrowingContinuation { [self] continuation in
get async throws(JSException) {
try await withUnsafeContinuation { [self] continuation in
self.then(
success: {
continuation.resume(returning: $0)
continuation.resume(returning: Swift.Result<JSValue, JSException>.success($0))
return JSValue.undefined
},
failure: {
continuation.resume(throwing: JSException($0))
continuation.resume(returning: Swift.Result<JSValue, JSException>.failure(.init($0)))
return JSValue.undefined
}
)
}
}.get()
}
}

/// Wait for the promise to complete, returning its result or exception as a Result.
///
/// - Note: Calling this function does not switch from the caller's isolation domain.
public func value(isolation: isolated (any Actor)? = #isolation) async throws -> JSValue {
try await withUnsafeThrowingContinuation(isolation: isolation) { [self] continuation in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a typed-throws version of withContinuation 😔

public func value(isolation: isolated (any Actor)? = #isolation) async throws(JSException) -> JSValue {
try await withUnsafeContinuation(isolation: isolation) { [self] continuation in
self.then(
success: {
continuation.resume(returning: $0)
continuation.resume(returning: Swift.Result<JSValue, JSException>.success($0))
return JSValue.undefined
},
failure: {
continuation.resume(throwing: JSException($0))
continuation.resume(returning: Swift.Result<JSValue, JSException>.failure(.init($0)))
return JSValue.undefined
}
)
}
}.get()
}

/// Wait for the promise to complete, returning its result or exception as a Result.
Expand Down
1 change: 1 addition & 0 deletions Sources/JavaScriptEventLoop/JobQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The current implementation is much simple to be easily debugged, but should be re-implemented
// using priority queue ideally.

import _Concurrency
import _CJavaScriptEventLoop

#if compiler(>=5.5)
Expand Down
3 changes: 3 additions & 0 deletions Sources/JavaScriptEventLoop/WebWorkerDedicatedExecutor.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if !hasFeature(Embedded)
import JavaScriptKit
import _CJavaScriptEventLoop
import _Concurrency

#if canImport(Synchronization)
import Synchronization
Expand Down Expand Up @@ -60,3 +62,4 @@ public final class WebWorkerDedicatedExecutor: SerialExecutor {
self.underlying.enqueue(job)
}
}
#endif
2 changes: 1 addition & 1 deletion Sources/JavaScriptEventLoop/WebWorkerTaskExecutor.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if compiler(>=6.0) // `TaskExecutor` is available since Swift 6.0
#if compiler(>=6.0) && !hasFeature(Embedded) // `TaskExecutor` is available since Swift 6.0, no multi-threading for embedded Wasm yet.

import JavaScriptKit
import _CJavaScriptKit
Expand Down