]> git.lizzy.rs Git - rust.git/blob - src/libpanic_unwind/emcc.rs
Fix type filter in rustdoc js
[rust.git] / src / libpanic_unwind / emcc.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Unwinding for emscripten
12 //!
13 //! Whereas Rust's usual unwinding implementation for Unix platforms
14 //! calls into the libunwind APIs directly, on emscripten we instead
15 //! call into the C++ unwinding APIs. This is just an expedience since
16 //! emscripten's runtime always implements those APIs and does not
17 //! implement libunwind.
18
19 #![allow(private_no_mangle_fns)]
20
21 use core::any::Any;
22 use core::ptr;
23 use alloc::boxed::Box;
24 use libc::{self, c_int};
25 use unwind as uw;
26 use core::mem;
27
28 pub fn payload() -> *mut u8 {
29     ptr::null_mut()
30 }
31
32 pub unsafe fn cleanup(ptr: *mut u8) -> Box<Any + Send> {
33     assert!(!ptr.is_null());
34     let ex = ptr::read(ptr as *mut _);
35     __cxa_free_exception(ptr as *mut _);
36     ex
37 }
38
39 pub unsafe fn panic(data: Box<Any + Send>) -> u32 {
40     let sz = mem::size_of_val(&data);
41     let exception = __cxa_allocate_exception(sz);
42     if exception == ptr::null_mut() {
43         return uw::_URC_FATAL_PHASE1_ERROR as u32;
44     }
45     let exception = exception as *mut Box<Any + Send>;
46     ptr::write(exception, data);
47     __cxa_throw(exception as *mut _, ptr::null_mut(), ptr::null_mut());
48
49     unreachable!()
50 }
51
52 #[lang = "eh_personality"]
53 #[no_mangle]
54 unsafe extern "C" fn rust_eh_personality(version: c_int,
55                                          actions: uw::_Unwind_Action,
56                                          exception_class: uw::_Unwind_Exception_Class,
57                                          exception_object: *mut uw::_Unwind_Exception,
58                                          context: *mut uw::_Unwind_Context)
59                                          -> uw::_Unwind_Reason_Code {
60     __gxx_personality_v0(version, actions, exception_class, exception_object, context)
61 }
62
63 extern "C" {
64     fn __cxa_allocate_exception(thrown_size: libc::size_t) -> *mut libc::c_void;
65     fn __cxa_free_exception(thrown_exception: *mut libc::c_void);
66     fn __cxa_throw(thrown_exception: *mut libc::c_void,
67                    tinfo: *mut libc::c_void,
68                    dest: *mut libc::c_void);
69     fn __gxx_personality_v0(version: c_int,
70                             actions: uw::_Unwind_Action,
71                             exception_class: uw::_Unwind_Exception_Class,
72                             exception_object: *mut uw::_Unwind_Exception,
73                             context: *mut uw::_Unwind_Context)
74                             -> uw::_Unwind_Reason_Code;
75 }