BridgeJS: Fix reject path of zero-parameter async throwing exports - #760
Merged
krodak merged 1 commit intoJun 10, 2026
Merged
Conversation
kateinoigakukun
approved these changes
Jun 10, 2026
Member
Author
|
@kateinoigakukun sure, done: #761 |
This was referenced Jun 10, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
A zero-parameter
async throws(JSException)export traps the Wasm instance when it throws. The generated thunk wraps the call in a_bjs_makePromisebody closure; with no parameters there is nothing to capture, so the closure lowers viathin_to_thick_function, and invoking that value with an indirect typed error miscompiles on Wasm (swiftlang/swift#89320): the thrownJSExceptionarrives corrupted inPromise_rejectand lowering it traps withRuntimeError: 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.Generated thunk before:
After:
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.