web3api_wasm_rs/
abort.rs

1#[link(wasm_import_module = "w3")]
2extern "C" {
3    /// Get Abort Arguments
4    #[link_name = "__w3_abort"]
5    pub fn __w3_abort(
6        msg_ptr: u32,
7        msg_len: u32,
8        file_ptr: u32,
9        file_len: u32,
10        line: u32,
11        column: u32,
12    );
13}
14
15/// Helper for aborting
16pub fn w3_abort_setup() {
17    std::panic::set_hook(Box::new(|panic_info| {
18        let payload = panic_info.payload();
19        let message = match payload
20            .downcast_ref::<String>()
21            .map(String::as_str)
22            .or_else(|| payload.downcast_ref::<&'static str>().copied())
23        {
24            Some(msg) => msg.to_string(),
25            None => "unknown error".to_string(),
26        };
27        let msg_len = message.len() as u32;
28        let location = panic_info.location();
29        let file = match location {
30            Some(location) => location.file(),
31            None => "unknown file",
32        };
33        let file_len = file.len() as u32;
34        let line = match location {
35            Some(location) => location.line(),
36            None => 0,
37        };
38        let column = match location {
39            Some(location) => location.column(),
40            None => 0,
41        };
42        unsafe {
43            __w3_abort(
44                message.as_ptr() as u32,
45                msg_len,
46                file.as_ptr() as u32,
47                file_len,
48                line,
49                column,
50            )
51        };
52    }))
53}