Implemented fn catch_any_panic<> to catch any panic from "free" extern "C" functions returning bool. Modified "free" extern "C" functions to now be falliable by returning bool value.

This commit is contained in:
Angel 2019-07-22 17:25:29 -05:00
parent 1547620469
commit dde4f71257

View file

@ -15,6 +15,12 @@ use simpleservo::{Coordinates, EventLoopWaker, HostTrait, InitOptions, VRInitOpt
use std::ffi::{CStr, CString}; use std::ffi::{CStr, CString};
use std::mem; use std::mem;
use std::os::raw::{c_char, c_void}; use std::os::raw::{c_char, c_void};
use std::panic::{self, AssertUnwindSafe, UnwindSafe};
/// Catch any panic function used by extern "C" functions.
fn catch_any_panic<F: FnOnce() + UnwindSafe>(function: F) -> bool {
panic::catch_unwind(function).is_ok()
}
fn call<F>(f: F) fn call<F>(f: F)
where where
@ -66,11 +72,18 @@ pub struct CInitOptions {
/// The returned string is not freed. This will leak. /// The returned string is not freed. This will leak.
#[no_mangle] #[no_mangle]
pub extern "C" fn servo_version() -> *const c_char { pub extern "C" fn servo_version() -> *const c_char {
let v = simpleservo::servo_version(); let result = panic::catch_unwind(AssertUnwindSafe(|| {
let text = CString::new(v).expect("Can't create string"); let v = simpleservo::servo_version();
let ptr = text.as_ptr(); let text = CString::new(v).expect("Can't create string");
mem::forget(text); let ptr: *const c_char = text.as_ptr();
ptr mem::forget(text);
ptr
}));
match result {
Ok(ptr) => ptr,
Err(_) => std::ptr::null(),
}
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@ -146,19 +159,21 @@ pub extern "C" fn init_with_egl(
opts: CInitOptions, opts: CInitOptions,
wakeup: extern "C" fn(), wakeup: extern "C" fn(),
callbacks: CHostCallbacks, callbacks: CHostCallbacks,
) { ) -> bool {
init_logger(); catch_any_panic(|| {
let gl = gl_glue::egl::init().unwrap(); init_logger();
unsafe { let gl = gl_glue::egl::init().unwrap();
init( unsafe {
opts, init(
gl.gl_wrapper, opts,
Some(gl.gl_context), gl.gl_wrapper,
Some(gl.display), Some(gl.gl_context),
wakeup, Some(gl.display),
callbacks, wakeup,
) callbacks,
} )
}
})
} }
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))] #[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
@ -167,10 +182,12 @@ pub extern "C" fn init_with_gl(
opts: CInitOptions, opts: CInitOptions,
wakeup: extern "C" fn(), wakeup: extern "C" fn(),
callbacks: CHostCallbacks, callbacks: CHostCallbacks,
) { ) -> bool {
init_logger(); catch_any_panic(|| {
let gl = gl_glue::gl::init().unwrap(); init_logger();
unsafe { init(opts, gl, None, None, wakeup, callbacks) } let gl = gl_glue::gl::init().unwrap();
unsafe { init(opts, gl, None, None, wakeup, callbacks) }
})
} }
#[no_mangle] #[no_mangle]
@ -201,113 +218,149 @@ pub extern "C" fn resize(width: i32, height: i32) {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn perform_updates() { pub extern "C" fn perform_updates() -> bool {
debug!("perform_updates"); catch_any_panic(|| {
call(|s| s.perform_updates()); debug!("perform_updates");
call(|s| s.perform_updates());
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn load_uri(url: *const c_char) { pub extern "C" fn load_uri(url: *const c_char) -> bool {
debug!("load_url"); catch_any_panic(|| {
let url = unsafe { CStr::from_ptr(url) }; debug!("load_url");
let url = url.to_str().expect("Can't read string"); let url = unsafe { CStr::from_ptr(url) };
call(|s| s.load_uri(url)); let url = url.to_str().expect("Can't read string");
call(|s| s.load_uri(url));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn reload() { pub extern "C" fn reload() -> bool {
debug!("reload"); catch_any_panic(|| {
call(|s| s.reload()); debug!("reload");
call(|s| s.reload());
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn stop() { pub extern "C" fn stop() -> bool {
debug!("stop"); catch_any_panic(|| {
call(|s| s.stop()); debug!("stop");
call(|s| s.stop());
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn refresh() { pub extern "C" fn refresh() -> bool {
debug!("refresh"); catch_any_panic(|| {
call(|s| s.refresh()); debug!("refresh");
call(|s| s.refresh());
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn go_back() { pub extern "C" fn go_back() -> bool {
debug!("go_back"); catch_any_panic(|| {
call(|s| s.go_back()); debug!("go_back");
call(|s| s.go_back());
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn go_forward() { pub extern "C" fn go_forward() -> bool {
debug!("go_forward"); catch_any_panic(|| {
call(|s| s.go_forward()); debug!("go_forward");
call(|s| s.go_forward());
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn scroll_start(dx: i32, dy: i32, x: i32, y: i32) { pub extern "C" fn scroll_start(dx: i32, dy: i32, x: i32, y: i32) -> bool {
debug!("scroll_start"); catch_any_panic(|| {
call(|s| s.scroll_start(dx as f32, dy as f32, x, y)); debug!("scroll_start");
call(|s| s.scroll_start(dx as f32, dy as f32, x, y));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn scroll_end(dx: i32, dy: i32, x: i32, y: i32) { pub extern "C" fn scroll_end(dx: i32, dy: i32, x: i32, y: i32) -> bool {
debug!("scroll_end"); catch_any_panic(|| {
call(|s| s.scroll_end(dx as f32, dy as f32, x, y)); debug!("scroll_end");
call(|s| s.scroll_end(dx as f32, dy as f32, x, y));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn scroll(dx: i32, dy: i32, x: i32, y: i32) { pub extern "C" fn scroll(dx: i32, dy: i32, x: i32, y: i32) -> bool {
debug!("scroll"); catch_any_panic(|| {
call(|s| s.scroll(dx as f32, dy as f32, x, y)); debug!("scroll");
call(|s| s.scroll(dx as f32, dy as f32, x, y));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn touch_down(x: f32, y: f32, pointer_id: i32) { pub extern "C" fn touch_down(x: f32, y: f32, pointer_id: i32) -> bool {
debug!("touch down"); catch_any_panic(|| {
call(|s| s.touch_down(x, y, pointer_id)); debug!("touch down");
call(|s| s.touch_down(x, y, pointer_id));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn touch_up(x: f32, y: f32, pointer_id: i32) { pub extern "C" fn touch_up(x: f32, y: f32, pointer_id: i32) -> bool {
debug!("touch up"); catch_any_panic(|| {
call(|s| s.touch_up(x, y, pointer_id)); debug!("touch up");
call(|s| s.touch_up(x, y, pointer_id));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn touch_move(x: f32, y: f32, pointer_id: i32) { pub extern "C" fn touch_move(x: f32, y: f32, pointer_id: i32) -> bool {
debug!("touch move"); catch_any_panic(|| {
call(|s| s.touch_move(x, y, pointer_id)); debug!("touch move");
call(|s| s.touch_move(x, y, pointer_id));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn touch_cancel(x: f32, y: f32, pointer_id: i32) { pub extern "C" fn touch_cancel(x: f32, y: f32, pointer_id: i32) -> bool {
debug!("touch cancel"); catch_any_panic(|| {
call(|s| s.touch_cancel(x, y, pointer_id)); debug!("touch cancel");
call(|s| s.touch_cancel(x, y, pointer_id));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn pinchzoom_start(factor: f32, x: i32, y: i32) { pub extern "C" fn pinchzoom_start(factor: f32, x: i32, y: i32) -> bool {
debug!("pinchzoom_start"); catch_any_panic(|| {
call(|s| s.pinchzoom_start(factor, x as u32, y as u32)); debug!("pinchzoom_start");
call(|s| s.pinchzoom_start(factor, x as u32, y as u32));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn pinchzoom(factor: f32, x: i32, y: i32) { pub extern "C" fn pinchzoom(factor: f32, x: i32, y: i32) -> bool {
debug!("pinchzoom"); catch_any_panic(|| {
call(|s| s.pinchzoom(factor, x as u32, y as u32)); debug!("pinchzoom");
call(|s| s.pinchzoom(factor, x as u32, y as u32));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn pinchzoom_end(factor: f32, x: i32, y: i32) { pub extern "C" fn pinchzoom_end(factor: f32, x: i32, y: i32) -> bool {
debug!("pinchzoom_end"); catch_any_panic(|| {
call(|s| s.pinchzoom_end(factor, x as u32, y as u32)); debug!("pinchzoom_end");
call(|s| s.pinchzoom_end(factor, x as u32, y as u32));
})
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn click(x: i32, y: i32) { pub extern "C" fn click(x: i32, y: i32) -> bool {
debug!("click"); catch_any_panic(|| {
call(|s| s.click(x as f32, y as f32)); debug!("click");
call(|s| s.click(x as f32, y as f32));
})
} }
pub struct WakeupCallback(extern "C" fn()); pub struct WakeupCallback(extern "C" fn());