2019-02-08 07:53:55 -06:00
|
|
|
//! Unwinding for *emscripten* target.
|
2016-09-27 16:25:52 -05:00
|
|
|
//!
|
|
|
|
//! Whereas Rust's usual unwinding implementation for Unix platforms
|
2019-02-08 07:53:55 -06:00
|
|
|
//! calls into the libunwind APIs directly, on Emscripten we instead
|
2016-09-27 16:25:52 -05:00
|
|
|
//! call into the C++ unwinding APIs. This is just an expedience since
|
2019-02-08 07:53:55 -06:00
|
|
|
//! Emscripten's runtime always implements those APIs and does not
|
2016-09-27 16:25:52 -05:00
|
|
|
//! implement libunwind.
|
|
|
|
|
2016-09-22 14:55:42 -05:00
|
|
|
#![allow(private_no_mangle_fns)]
|
|
|
|
|
|
|
|
use core::any::Any;
|
|
|
|
use core::ptr;
|
2019-02-02 12:00:02 -06:00
|
|
|
use core::mem;
|
2016-09-22 14:55:42 -05:00
|
|
|
use alloc::boxed::Box;
|
|
|
|
use libc::{self, c_int};
|
|
|
|
use unwind as uw;
|
|
|
|
|
|
|
|
pub fn payload() -> *mut u8 {
|
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
|
2018-07-11 10:11:08 -05:00
|
|
|
pub unsafe fn cleanup(ptr: *mut u8) -> Box<dyn Any + Send> {
|
2016-09-22 14:55:42 -05:00
|
|
|
assert!(!ptr.is_null());
|
|
|
|
let ex = ptr::read(ptr as *mut _);
|
|
|
|
__cxa_free_exception(ptr as *mut _);
|
|
|
|
ex
|
|
|
|
}
|
|
|
|
|
2018-07-11 10:11:08 -05:00
|
|
|
pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
|
2016-09-22 14:55:42 -05:00
|
|
|
let sz = mem::size_of_val(&data);
|
|
|
|
let exception = __cxa_allocate_exception(sz);
|
|
|
|
if exception == ptr::null_mut() {
|
|
|
|
return uw::_URC_FATAL_PHASE1_ERROR as u32;
|
|
|
|
}
|
2018-07-11 10:11:08 -05:00
|
|
|
let exception = exception as *mut Box<dyn Any + Send>;
|
2016-09-22 14:55:42 -05:00
|
|
|
ptr::write(exception, data);
|
|
|
|
__cxa_throw(exception as *mut _, ptr::null_mut(), ptr::null_mut());
|
|
|
|
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[lang = "eh_personality"]
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn rust_eh_personality(version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
exception_class: uw::_Unwind_Exception_Class,
|
|
|
|
exception_object: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context)
|
|
|
|
-> uw::_Unwind_Reason_Code {
|
2016-10-18 12:39:47 -05:00
|
|
|
__gxx_personality_v0(version, actions, exception_class, exception_object, context)
|
2016-09-22 14:55:42 -05:00
|
|
|
}
|
|
|
|
|
2016-10-18 12:39:47 -05:00
|
|
|
extern "C" {
|
2016-09-22 14:55:42 -05:00
|
|
|
fn __cxa_allocate_exception(thrown_size: libc::size_t) -> *mut libc::c_void;
|
|
|
|
fn __cxa_free_exception(thrown_exception: *mut libc::c_void);
|
|
|
|
fn __cxa_throw(thrown_exception: *mut libc::c_void,
|
|
|
|
tinfo: *mut libc::c_void,
|
|
|
|
dest: *mut libc::c_void);
|
|
|
|
fn __gxx_personality_v0(version: c_int,
|
|
|
|
actions: uw::_Unwind_Action,
|
|
|
|
exception_class: uw::_Unwind_Exception_Class,
|
|
|
|
exception_object: *mut uw::_Unwind_Exception,
|
|
|
|
context: *mut uw::_Unwind_Context)
|
|
|
|
-> uw::_Unwind_Reason_Code;
|
|
|
|
}
|