diff --git a/CHANGELOG.md b/CHANGELOG.md index be9404e0..580a0eed 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/crates/cli/tests/reference/anyref-import-catch.wat b/crates/cli/tests/reference/anyref-import-catch.wat index 48071a31..16adbbe6 100644 --- a/crates/cli/tests/reference/anyref-import-catch.wat +++ b/crates/cli/tests/reference/anyref-import-catch.wat @@ -4,10 +4,10 @@ (type (;2;) (func (param i32))) (type (;3;) (func (param i32) (result i32))) (import "./reference_test_bg.js" "__wbindgen_init_externref_table" (func (;0;) (type 0))) - (func $__wbindgen_exn_store (;1;) (type 2) (param i32)) - (func $__externref_table_dealloc (;2;) (type 2) (param i32)) - (func $exported (;3;) (type 2) (param i32)) - (func $__externref_table_alloc (;4;) (type 1) (result i32)) + (func $__externref_table_dealloc (;1;) (type 2) (param i32)) + (func $exported (;2;) (type 2) (param i32)) + (func $__externref_table_alloc (;3;) (type 1) (result i32)) + (func $__wbindgen_exn_store (;4;) (type 2) (param i32)) (func $__wbindgen_add_to_stack_pointer (;5;) (type 3) (param i32) (result i32)) (table (;0;) 128 externref) (memory (;0;) 17) diff --git a/crates/cli/tests/reference/import-catch.wat b/crates/cli/tests/reference/import-catch.wat index f291a4a4..614e0b61 100644 --- a/crates/cli/tests/reference/import-catch.wat +++ b/crates/cli/tests/reference/import-catch.wat @@ -1,8 +1,8 @@ (module (type (;0;) (func (param i32))) (type (;1;) (func (param i32) (result i32))) - (func $__wbindgen_exn_store (;0;) (type 0) (param i32)) - (func $exported (;1;) (type 0) (param i32)) + (func $exported (;0;) (type 0) (param i32)) + (func $__wbindgen_exn_store (;1;) (type 0) (param i32)) (func $__wbindgen_add_to_stack_pointer (;2;) (type 1) (param i32) (result i32)) (memory (;0;) 17) (export "memory" (memory 0)) diff --git a/src/lib.rs b/src/lib.rs index 2f9e48e6..fdef0e8d 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`