Skip to content

Commit b22c802

Browse files
guybedfordDarksonn
authored andcommitted
wasm: link emscripten_promise_await for Emscripten 4.x compatibility (#8528)
1 parent 16b1f9d commit b22c802

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎tokio/src/lib.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@
483483
//!
484484
//! When the build links WebAssembly JavaScript Promise Integration (JSPI), a
485485
//! wait that would block suspends on the host event loop rather than blocking.
486-
//! This requires Emscripten 6.0.10 or later. Without JSPI, such a wait panics.
486+
//! JSPI builds require Emscripten 6.0.10 or later; builds without JSPI link
487+
//! against any Emscripten the Rust target supports, and such a wait panics.
487488
//!
488489
//! Suspension requires the current export to have been wrapped with
489490
//! `WebAssembly.promising`. A wait from any other activation throws

‎tokio/src/runtime/jspi.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,27 @@ extern "C" {
3939
fn emscripten_clear_immediate(id: i32);
4040
}
4141

42+
/// `em_settled_result_t`, returned by value.
43+
#[repr(C)]
44+
struct SettledResult {
45+
result: i32,
46+
value: *mut c_void,
47+
}
48+
4249
extern "C-unwind" {
4350
// Suspending import (Emscripten marks it `__async`): resolves to the
44-
// promise's value once it settles. Under `-sJSPI` the wrapper is
51+
// promise's settled result. Under `-sJSPI` the wrapper is
4552
// `Asyncify.handleAsync`, which keeps the runtime alive across the
46-
// suspension. Linkable without JSPI, where it aborts if reached.
53+
// suspension. Linkable without JSPI, where it aborts if reached. Present
54+
// in every Emscripten the Rust target supports, unlike the newer
55+
// `emscripten_promise_await_unchecked`.
4756
//
4857
// Suspension needs a `WebAssembly.promising` activation on the stack.
4958
// From any other activation (a plain host callback into the module) the
5059
// engine throws `WebAssembly.SuspendError` out of this import instead. It
5160
// is a foreign exception to Rust: drops run as it unwinds, `catch_unwind`
5261
// does not catch it, and it aborts at the first `extern "C"` frame.
53-
fn emscripten_promise_await_unchecked(promise: Promise) -> *mut c_void;
62+
fn emscripten_promise_await(promise: Promise) -> SettledResult;
5463
}
5564

5665
/// Whether JSPI suspension is available: linked with `-sJSPI`.
@@ -160,8 +169,9 @@ pub(crate) fn park(slot: &Slot, dur: Option<Duration>) {
160169

161170
// SAFETY: the handle is live. Under `-sJSPI` this suspends the
162171
// activation; the caller has checked `jspi_enabled`. A `SuspendError`
163-
// unwinding out of it drops `park`, clearing the timer and handle.
164-
unsafe { emscripten_promise_await_unchecked(park.promise) };
172+
// unwinding out of it drops `park`, clearing the timer and handle. The
173+
// promise is only ever fulfilled, so the result carries nothing.
174+
let _ = unsafe { emscripten_promise_await(park.promise) };
165175
}
166176

167177
/// Resume the activation parked on `slot`, if any.

0 commit comments

Comments
 (0)