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::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
@ -67,11 +73,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")]
@ -147,19 +160,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"))]
@ -168,10 +183,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]
@ -202,113 +219,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());