Skip to content

Commit

Permalink
Make catch thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Mar 9, 2024
1 parent ad4e443 commit 7362288
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down

0 comments on commit 7362288

Please sign in to comment.