Skip to content

Commit 77408be

Browse files
committed
Improve unwrap_pyresult for wasm
1 parent cc25b49 commit 77408be

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
@@ -548,14 +548,30 @@ impl VirtualMachine {
548548

549549
// TODO: #[track_caller] when stabilized
550550
fn _py_panic_failed(&self, exc: &PyBaseExceptionRef, msg: &str) -> ! {
551-
let show_backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |v| &v != "0");
552-
let after = if show_backtrace {
553-
exceptions::print_exception(self, exc);
554-
"exception backtrace above"
555-
} else {
556-
"run with RUST_BACKTRACE=1 to see Python backtrace"
557-
};
558-
panic!("{}; {}", msg, after)
551+
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
552+
{
553+
let show_backtrace = env::var_os("RUST_BACKTRACE").map_or(false, |v| &v != "0");
554+
let after = if show_backtrace {
555+
exceptions::print_exception(self, exc);
556+
"exception backtrace above"
557+
} else {
558+
"run with RUST_BACKTRACE=1 to see Python backtrace"
559+
};
560+
panic!("{}; {}", msg, after)
561+
}
562+
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
563+
{
564+
use wasm_bindgen::prelude::*;
565+
#[wasm_bindgen]
566+
extern "C" {
567+
#[wasm_bindgen(js_namespace = console)]
568+
fn error(s: &str);
569+
}
570+
let mut s = Vec::<u8>::new();
571+
exceptions::write_exception(&mut s, self, exc).unwrap();
572+
error(std::str::from_utf8(&s).unwrap());
573+
panic!("{}; exception backtrace above", msg)
574+
}
559575
}
560576
pub fn unwrap_pyresult<T>(&self, result: PyResult<T>) -> T {
561577
result.unwrap_or_else(|exc| {

0 commit comments

Comments
 (0)