Skip to content

BridgeJS: Fix reject path of zero-parameter async throwing exports - #760

Merged
krodak merged 1 commit into
swiftwasm:mainfrom
PassiveLogic:kr/fix-async-thunk-captureless
Jun 10, 2026
Merged

krodak merged 1 commit into
swiftwasm:mainfrom
PassiveLogic:kr/fix-async-thunk-captureless

Conversation

@krodak

@krodak krodak commented Jun 10, 2026

Copy link
Copy Markdown
Member

Overview

A zero-parameter async throws(JSException) export traps the Wasm instance when it throws. The generated thunk wraps the call in a _bjs_makePromise body closure; with no parameters there is nothing to capture, so the closure lowers via thin_to_thick_function, and invoking that value with an indirect typed error miscompiles on Wasm (swiftlang/swift#89320): the thrown JSException arrives corrupted in Promise_reject and lowering it traps with RuntimeError: table index is out of bounds (or the promise rejects with a garbage value). Exports with parameters are unaffected because their hoisted parameter locals are captured, which makes the body closure a partial apply with a matching call signature.

@JS func ping() async throws(JSException) -> String {
    throw JSException(JSError(message: "failure").jsValue)
}

Generated thunk before:

return _bjs_makePromise(resolve: Promise_resolve_SS, reject: Promise_reject) { () async throws(JSException) -> String in
    return try await ping()
}

After:

let __bjs_capture = 0
return _bjs_makePromise(resolve: Promise_resolve_SS, reject: Promise_reject) { [__bjs_capture] () async throws(JSException) -> String in
    _ = __bjs_capture
    return try await ping()
}

1. Force a capture in captureless async throwing thunk bodies. When the body closure would otherwise capture nothing (zero-parameter top-level functions, static methods, and constructors share this shape), the generator emits a dummy local captured by the closure. The body must also read the captured value: an unread capture list entry is dropped by capture analysis, leaving the closure thin and the bug in place. The read is what produces a real partial_apply.

2. Tests. A codegen snapshot covers the zero-parameter shape, and an end-to-end regression test asserts the rejection arrives with the thrown message instead of trapping. Existing thunks are emitted byte-identically.

This is a codegen-level workaround for swiftlang/swift#89320; once the IRGen fix in swiftlang/swift#89715 lands in a release toolchain, the forced capture can be removed.

@kateinoigakukun kateinoigakukun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is fine right now, can you create an issue on JSKit repo to track this FIXME item?

@krodak
krodak merged commit ab2bdb3 into swiftwasm:main Jun 10, 2026
13 checks passed
@krodak

krodak commented Jun 10, 2026

Copy link
Copy Markdown
Member Author

@kateinoigakukun sure, done: #761

krodak added a commit to PassiveLogic/JavaScriptKit that referenced this pull request Sep 16, 2026
Generic exported functions and methods may now be throws(JSException),
matching what generic imports already supported:

    @js public func pickOrThrow<T: BridgedSwiftGenericBridgeable>(_ value: T, _ fail: Bool) throws(JSException) -> T

The concrete entry thunk wraps the existential-opening call chain in the
same do/catch every throwing export uses — the exception crosses through
the _swift_js_throw side channel — and the open-chain helpers become
throws(JSException) so the typed error propagates without erasure. On
the JavaScript side the wrapper rethrows the exception right after the
wasm call and before lifting the result, so a thrown call reads nothing
back and leaves the shared value stack balanced; the wrappers emitted
through the thunk builder get this from the existing effects-driven
exception check, and the struct-instance method path emits the same
sequence explicitly.

async generic exports remain rejected with a diagnostic. There is no
compiler obstacle — the wasm32 typed-throws closure issues are already
worked around by the forced-capture emission (swiftwasm#760) and JSException
storage boxing (swiftwasm#766) — but promise settlement is per-type today: each
async export settles through a Promise_resolve_<type> helper paired
with a JS settle handler that lifts a concrete value. A generic result
needs a codec-driven settlement path (stash the call's codec with the
promise's settlers, settle through the value stack), which is its own
ABI addition and lands separately.

Runtime tests cover the happy path, the surfaced exception, and that a
thrown call leaves the stacks balanced for the next generic call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants