summaryrefslogtreecommitdiffstats
path: root/tests/run-make/wasm-exceptions-nostd/src/lib.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:59:35 +0000
commitd1b2d29528b7794b41e66fc2136e395a02f8529b (patch)
treea4a17504b260206dec3cf55b2dca82929a348ac2 /tests/run-make/wasm-exceptions-nostd/src/lib.rs
parentReleasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz
rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/run-make/wasm-exceptions-nostd/src/lib.rs')
-rw-r--r--tests/run-make/wasm-exceptions-nostd/src/lib.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/run-make/wasm-exceptions-nostd/src/lib.rs b/tests/run-make/wasm-exceptions-nostd/src/lib.rs
new file mode 100644
index 000000000..7049d2fd9
--- /dev/null
+++ b/tests/run-make/wasm-exceptions-nostd/src/lib.rs
@@ -0,0 +1,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
+}