Skip to content

Commit

Permalink
add --cfg=wasm_bindgen_unwrap_throw_debug flag to defer to std's un…
Browse files Browse the repository at this point in the history
…wrap in unwrap_throw
  • Loading branch information
alecmocatta committed Apr 2, 2024
1 parent 78463a2 commit 03f7287
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,7 @@ pub fn anyref_heap_live_count() -> u32 {
pub trait UnwrapThrowExt<T>: Sized {
/// Unwrap this `Option` or `Result`, but instead of panicking on failure,
/// throw an exception to JavaScript.
#[cfg_attr(debug_assertions, track_caller)]
#[cfg_attr(any(debug_assertions, wasm_bindgen_unwrap_throw_debug), track_caller)]
fn unwrap_throw(self) -> T {
if cfg!(all(debug_assertions, feature = "std")) {
let loc = core::panic::Location::caller();
Expand All @@ -1346,16 +1346,26 @@ pub trait UnwrapThrowExt<T>: Sized {
/// Unwrap this container's `T` value, or throw an error to JS with the
/// given message if the `T` value is unavailable (e.g. an `Option<T>` is
/// `None`).
#[cfg_attr(debug_assertions, track_caller)]
#[cfg_attr(any(debug_assertions, wasm_bindgen_unwrap_throw_debug), track_caller)]
fn expect_throw(self, message: &str) -> T;
}

impl<T> UnwrapThrowExt<T> for Option<T> {
#[cfg_attr(debug_assertions, track_caller)]
#[cfg(wasm_bindgen_unwrap_throw_debug)]
#[track_caller]
fn unwrap_throw(self) -> T {
self.unwrap()
}

#[cfg_attr(any(debug_assertions, wasm_bindgen_unwrap_throw_debug), track_caller)]
fn expect_throw(self, message: &str) -> T {
if cfg!(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
not(any(
wasm_bindgen_unwrap_throw_debug,
target_os = "emscripten",
target_os = "wasi"
))
)) {
match self {
Some(val) => val,
Expand All @@ -1371,11 +1381,15 @@ impl<T, E> UnwrapThrowExt<T> for Result<T, E>
where
E: core::fmt::Debug,
{
#[cfg_attr(debug_assertions, track_caller)]
#[cfg_attr(any(debug_assertions, wasm_bindgen_unwrap_throw_debug), track_caller)]
fn expect_throw(self, message: &str) -> T {
if cfg!(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
not(any(
wasm_bindgen_unwrap_throw_debug,
target_os = "emscripten",
target_os = "wasi"
))
)) {
match self {
Ok(val) => val,
Expand Down

0 comments on commit 03f7287

Please sign in to comment.