summaryrefslogtreecommitdiffstats
path: root/tests/run-make/wasm-exceptions-nostd/src/panicking.rs
blob: 4a8923fd43db6881d4f7e82960132e4a4973e07a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#[lang = "eh_personality"]
fn eh_personality() {}

mod internal {
    extern "C" {
        #[link_name = "llvm.wasm.throw"]
        pub fn wasm_throw(tag: i32, ptr: *mut u8) -> !;
    }
}

unsafe fn wasm_throw(ptr: *mut u8) -> ! {
    internal::wasm_throw(0, ptr);
}

#[panic_handler]
fn panic_handler(info: &core::panic::PanicInfo<'_>) -> ! {
    use alloc::boxed::Box;
    use alloc::string::ToString;

    let msg = info
        .message()
        .map(|msg| msg.to_string())
        .unwrap_or("(no message)".to_string());
    let exception = Box::new(msg.to_string());
    unsafe {
        let exception_raw = Box::into_raw(exception);
        wasm_throw(exception_raw as *mut u8);
    }
}