|
| 1 | +//@ compile-flags: -O --target wasm32-unknown-emscripten -Z emscripten-wasm-eh |
| 2 | +//@ needs-llvm-components: webassembly |
| 3 | + |
| 4 | +// Emscripten catch_unwind using wasm exceptions |
| 5 | + |
| 6 | +#![feature(no_core, lang_items, intrinsics, rustc_attrs)] |
| 7 | +#![crate_type = "lib"] |
| 8 | +#![no_std] |
| 9 | +#![no_core] |
| 10 | + |
| 11 | +#[lang = "sized"] |
| 12 | +trait Sized {} |
| 13 | +#[lang = "freeze"] |
| 14 | +trait Freeze {} |
| 15 | +#[lang = "copy"] |
| 16 | +trait Copy {} |
| 17 | + |
| 18 | +impl<T> Copy for *mut T {} |
| 19 | + |
| 20 | +#[rustc_intrinsic] |
| 21 | +fn size_of<T>() -> usize { |
| 22 | + loop {} |
| 23 | +} |
| 24 | + |
| 25 | +extern "rust-intrinsic" { |
| 26 | + fn catch_unwind( |
| 27 | + try_fn: fn(_: *mut u8), |
| 28 | + data: *mut u8, |
| 29 | + catch_fn: fn(_: *mut u8, _: *mut u8), |
| 30 | + ) -> i32; |
| 31 | +} |
| 32 | + |
| 33 | +// CHECK-LABEL: @ptr_size |
| 34 | +#[no_mangle] |
| 35 | +pub fn ptr_size() -> usize { |
| 36 | + // CHECK: ret [[PTR_SIZE:.*]] |
| 37 | + size_of::<*mut u8>() |
| 38 | +} |
| 39 | + |
| 40 | +// CHECK-LABEL: @test_catch_unwind |
| 41 | +#[no_mangle] |
| 42 | +pub unsafe fn test_catch_unwind( |
| 43 | + try_fn: fn(_: *mut u8), |
| 44 | + data: *mut u8, |
| 45 | + catch_fn: fn(_: *mut u8, _: *mut u8), |
| 46 | +) -> i32 { |
| 47 | + // CHECK: start: |
| 48 | + // CHECK: invoke void %try_fn(ptr %data) |
| 49 | + // CHECK: to label %__rust_try.exit unwind label %catchswitch.i |
| 50 | + // CHECK: catchswitch.i: ; preds = %start |
| 51 | + // CHECK: %catchswitch1.i = catchswitch within none [label %catchpad.i] unwind to caller |
| 52 | + |
| 53 | + // CHECK: catchpad.i: ; preds = %catchswitch.i |
| 54 | + // CHECK: %catchpad2.i = catchpad within %catchswitch1.i [ptr null] |
| 55 | + // CHECK: %0 = tail call ptr @llvm.wasm.get.exception(token %catchpad2.i) |
| 56 | + // CHECK: %1 = tail call i32 @llvm.wasm.get.ehselector(token %catchpad2.i) |
| 57 | + // CHECK: call void %catch_fn(ptr %data, ptr %0) [ "funclet"(token %catchpad2.i) ] |
| 58 | + // CHECK: catchret from %catchpad2.i to label %__rust_try.exit |
| 59 | + |
| 60 | + // CHECK: __rust_try.exit: ; preds = %start, %catchpad.i |
| 61 | + // CHECK: %common.ret.op.i = phi i32 [ 0, %start ], [ 1, %catchpad.i ] |
| 62 | + // CHECK: ret i32 %common.ret.op.i |
| 63 | + |
| 64 | + catch_unwind(try_fn, data, catch_fn) |
| 65 | +} |
0 commit comments