From 7362288acf6455a40bb5380bffe97fc052517a55 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sat, 9 Mar 2024 23:45:13 +0100 Subject: [PATCH] Make `catch` thread safe --- CHANGELOG.md | 5 +++++ src/lib.rs | 22 ++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index be9404e05f6e..580a0eed76e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ * Copy port from headless test server when using `WASM_BINDGEN_TEST_ADDRESS`. [#3873](https://github.com/rustwasm/wasm-bindgen/pull/3873) +* Fix `catch` not being thread-safe. + [#3879](https://github.com/rustwasm/wasm-bindgen/pull/3879) + +-------------------------------------------------------------------------------- + ## [0.2.92](https://github.com/rustwasm/wasm-bindgen/compare/0.2.91...0.2.92) Released 2024-03-04 diff --git a/src/lib.rs b/src/lib.rs index 2f9e48e6d773..fdef0e8d39ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1687,26 +1687,28 @@ pub mod __rt { crate::externref::link_intrinsics(); } - static mut GLOBAL_EXNDATA: [u32; 2] = [0; 2]; + std::thread_local! { + static GLOBAL_EXNDATA: Cell<[u32; 2]> = Cell::new([0; 2]); + } #[no_mangle] pub unsafe extern "C" fn __wbindgen_exn_store(idx: u32) { - debug_assert_eq!(GLOBAL_EXNDATA[0], 0); - GLOBAL_EXNDATA[0] = 1; - GLOBAL_EXNDATA[1] = idx; + GLOBAL_EXNDATA.with(|data| { + debug_assert_eq!(data.get()[0], 0); + data.set([1, idx]); + }); } pub fn take_last_exception() -> Result<(), super::JsValue> { - unsafe { - let ret = if GLOBAL_EXNDATA[0] == 1 { - Err(super::JsValue::_new(GLOBAL_EXNDATA[1])) + GLOBAL_EXNDATA.with(|data| { + let ret = if data.get()[0] == 1 { + Err(super::JsValue::_new(data.get()[1])) } else { Ok(()) }; - GLOBAL_EXNDATA[0] = 0; - GLOBAL_EXNDATA[1] = 0; + data.set([0, 0]); ret - } + }) } /// An internal helper trait for usage in `#[wasm_bindgen]` on `async`