Skip to content

Commit 112eea1

Browse files
committed
Improve unwrap_pyresult for wasm
1 parent bc2e681 commit 112eea1

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

vm/src/vm.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,30 @@ impl VirtualMachine {
558558

559559
// TODO: #[track_caller] when stabilized
560560
fn _py_panic_failed(&self, exc: &PyBaseExceptionRef, msg: &str) -> ! {
561-
let show_backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |v| &v != "0");
562-
let after = if show_backtrace {
563-
exceptions::print_exception(self, exc);
564-
"exception backtrace above"
565-
} else {
566-
"run with RUST_BACKTRACE=1 to see Python backtrace"
567-
};
568-
panic!("{}; {}", msg, after)
561+
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
562+
{
563+
let show_backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |v| &v != "0");
564+
let after = if show_backtrace {
565+
exceptions::print_exception(self, exc);
566+
"exception backtrace above"
567+
} else {
568+
"run with RUST_BACKTRACE=1 to see Python backtrace"
569+
};
570+
panic!("{}; {}", msg, after)
571+
}
572+
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
573+
{
574+
use wasm_bindgen::prelude::*;
575+
#[wasm_bindgen]
576+
extern "C" {
577+
#[wasm_bindgen(js_namespace = console)]
578+
fn error(s: &str);
579+
}
580+
let mut s = Vec::<u8>::new();
581+
exceptions::write_exception(&mut s, self, exc).unwrap();
582+
error(std::str::from_utf8(&s).unwrap());
583+
panic!("{}; exception backtrace above", msg)
584+
}
569585
}
570586
pub fn unwrap_pyresult<T>(&self, result: PyResult<T>) -> T {
571587
result.unwrap_or_else(|exc| {

0 commit comments

Comments
 (0)