Auto merge of #23828 - angelortiz1007:winrt-servo, r=jdm

Made all "free" extern "C" APIs in simpleservo fallible by returning …

…null pointr or boolean value.  ServoApp will need to be modified to check return values.

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #23514 (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because _ServoApp will test/verify.__

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23828)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-07-24 22:33:55 -04:00 committed by GitHub
commit d93b652c11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,12 @@ use simpleservo::{Coordinates, EventLoopWaker, HostTrait, InitOptions, VRInitOpt
use std::ffi::{CStr, CString};
use std::mem;
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)
where
@ -67,11 +73,18 @@ pub struct CInitOptions {
/// The returned string is not freed. This will leak.
#[no_mangle]
pub extern "C" fn servo_version() -> *const c_char {
let result = panic::catch_unwind(AssertUnwindSafe(|| {
let v = simpleservo::servo_version();
let text = CString::new(v).expect("Can't create string");
let ptr = text.as_ptr();
let ptr: *const c_char = text.as_ptr();
mem::forget(text);
ptr
}));
match result {
Ok(ptr) => ptr,
Err(_) => std::ptr::null(),
}
}
#[cfg(target_os = "windows")]
@ -147,7 +160,8 @@ pub extern "C" fn init_with_egl(
opts: CInitOptions,
wakeup: extern "C" fn(),
callbacks: CHostCallbacks,
) {
) -> bool {
catch_any_panic(|| {
init_logger();
let gl = gl_glue::egl::init().unwrap();
unsafe {
@ -160,6 +174,7 @@ pub extern "C" fn init_with_egl(
callbacks,
)
}
})
}
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
@ -168,10 +183,12 @@ pub extern "C" fn init_with_gl(
opts: CInitOptions,
wakeup: extern "C" fn(),
callbacks: CHostCallbacks,
) {
) -> bool {
catch_any_panic(|| {
init_logger();
let gl = gl_glue::gl::init().unwrap();
unsafe { init(opts, gl, None, None, wakeup, callbacks) }
})
}
#[no_mangle]
@ -202,113 +219,149 @@ pub extern "C" fn resize(width: i32, height: i32) {
}
#[no_mangle]
pub extern "C" fn perform_updates() {
pub extern "C" fn perform_updates() -> bool {
catch_any_panic(|| {
debug!("perform_updates");
call(|s| s.perform_updates());
})
}
#[no_mangle]
pub extern "C" fn load_uri(url: *const c_char) {
pub extern "C" fn load_uri(url: *const c_char) -> bool {
catch_any_panic(|| {
debug!("load_url");
let url = unsafe { CStr::from_ptr(url) };
let url = url.to_str().expect("Can't read string");
call(|s| s.load_uri(url));
})
}
#[no_mangle]
pub extern "C" fn reload() {
pub extern "C" fn reload() -> bool {
catch_any_panic(|| {
debug!("reload");
call(|s| s.reload());
})
}
#[no_mangle]
pub extern "C" fn stop() {
pub extern "C" fn stop() -> bool {
catch_any_panic(|| {
debug!("stop");
call(|s| s.stop());
})
}
#[no_mangle]
pub extern "C" fn refresh() {
pub extern "C" fn refresh() -> bool {
catch_any_panic(|| {
debug!("refresh");
call(|s| s.refresh());
})
}
#[no_mangle]
pub extern "C" fn go_back() {
pub extern "C" fn go_back() -> bool {
catch_any_panic(|| {
debug!("go_back");
call(|s| s.go_back());
})
}
#[no_mangle]
pub extern "C" fn go_forward() {
pub extern "C" fn go_forward() -> bool {
catch_any_panic(|| {
debug!("go_forward");
call(|s| s.go_forward());
})
}
#[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 {
catch_any_panic(|| {
debug!("scroll_start");
call(|s| s.scroll_start(dx as f32, dy as f32, x, y));
})
}
#[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 {
catch_any_panic(|| {
debug!("scroll_end");
call(|s| s.scroll_end(dx as f32, dy as f32, x, y));
})
}
#[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 {
catch_any_panic(|| {
debug!("scroll");
call(|s| s.scroll(dx as f32, dy as f32, x, y));
})
}
#[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 {
catch_any_panic(|| {
debug!("touch down");
call(|s| s.touch_down(x, y, pointer_id));
})
}
#[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 {
catch_any_panic(|| {
debug!("touch up");
call(|s| s.touch_up(x, y, pointer_id));
})
}
#[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 {
catch_any_panic(|| {
debug!("touch move");
call(|s| s.touch_move(x, y, pointer_id));
})
}
#[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 {
catch_any_panic(|| {
debug!("touch cancel");
call(|s| s.touch_cancel(x, y, pointer_id));
})
}
#[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 {
catch_any_panic(|| {
debug!("pinchzoom_start");
call(|s| s.pinchzoom_start(factor, x as u32, y as u32));
})
}
#[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 {
catch_any_panic(|| {
debug!("pinchzoom");
call(|s| s.pinchzoom(factor, x as u32, y as u32));
})
}
#[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 {
catch_any_panic(|| {
debug!("pinchzoom_end");
call(|s| s.pinchzoom_end(factor, x as u32, y as u32));
})
}
#[no_mangle]
pub extern "C" fn click(x: i32, y: i32) {
pub extern "C" fn click(x: i32, y: i32) -> bool {
catch_any_panic(|| {
debug!("click");
call(|s| s.click(x as f32, y as f32));
})
}
pub struct WakeupCallback(extern "C" fn());