summaryrefslogtreecommitdiffstats
path: root/tests/run-make/wasm-exceptions-nostd/src/lib.rs
blob: 7049d2fd9e0d1737630f84d5db38b277bbe4e8f4 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![no_std]
#![crate_type = "cdylib"]

// Allow a few unstable features because we create a panic
// runtime for native wasm exceptions from scratch

#![feature(core_intrinsics)]
#![feature(lang_items)]
#![feature(link_llvm_intrinsics)]
#![feature(panic_info_message)]

extern crate alloc;

/// This module allows us to use `Box`, `String`, ... even in no-std
mod arena_alloc;

/// This module allows logging text, even in no-std
mod logging;

/// This module allows exceptions, even in no-std
#[cfg(target_arch = "wasm32")]
mod panicking;

use alloc::boxed::Box;
use alloc::string::String;

struct LogOnDrop;

impl Drop for LogOnDrop {
    fn drop(&mut self) {
        logging::log_str("Dropped");
    }
}

#[allow(unreachable_code)]
#[allow(unconditional_panic)]
#[no_mangle]
pub extern "C" fn start() -> usize {
    let data = 0x1234usize as *mut u8; // Something to recognize

    unsafe {
        core::intrinsics::r#try(|data: *mut u8| {
            let _log_on_drop = LogOnDrop;

            logging::log_str(&alloc::format!("`r#try` called with ptr {:?}", data));
            let x = [12];
            let _ = x[4]; // should panic

            logging::log_str("This line should not be visible! :(");
        }, data, |data, exception| {
            let exception = *Box::from_raw(exception as *mut String);
            logging::log_str("Caught something!");
            logging::log_str(&alloc::format!("  data     : {:?}", data));
            logging::log_str(&alloc::format!("  exception: {:?}", exception));
        });
    }

    logging::log_str("This program terminates correctly.");
    0
}