Skip to content

Commit

Permalink
macos: replace AppState::catch_unwind with AppState::propagate_panic
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Aug 21, 2024
1 parent 227137b commit 9295afd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
21 changes: 8 additions & 13 deletions reflector-platform/src/backend/cocoa/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::panic::{self, AssertUnwindSafe};
use std::panic;
use std::rc::Rc;
use std::time::Duration;

Expand Down Expand Up @@ -79,18 +79,13 @@ impl AppState {
}
}

pub(crate) fn catch_unwind<F: FnOnce()>(&self, f: F) {
let result = panic::catch_unwind(AssertUnwindSafe(f));

if let Err(panic) = result {
if self.running.get() {
// If we own the event loop, exit and propagate the panic upwards.
self.panic.set(Some(panic));
self.exit();
} else {
// Otherwise, just abort.
std::process::abort();
}
pub(crate) fn propagate_panic(&self, panic: Box<dyn Any + Send + 'static>) {
// If we own the event loop, exit and propagate the panic upwards. Otherwise, just abort.
if self.running.get() {
self.panic.set(Some(panic));
self.exit();
} else {
std::process::abort();
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions reflector-platform/src/backend/cocoa/display_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extern "C" fn release(info: *const c_void) {
extern "C" fn perform(info: *const c_void) {
let state = unsafe { &*(info as *mut DisplayState) };

state.app_state.catch_unwind(|| {
let result = panic::catch_unwind(AssertUnwindSafe(|| {
let windows: Vec<*const View> = state.app_state.windows.borrow().keys().copied().collect();
for ptr in windows {
let window_state = state.app_state.windows.borrow().get(&ptr).cloned();
Expand All @@ -83,7 +83,11 @@ extern "C" fn perform(info: *const c_void) {
}
}
}
});
}));

if let Err(panic) = result {
state.app_state.propagate_panic(panic);
}
}

struct DisplayState {
Expand Down
22 changes: 13 additions & 9 deletions reflector-platform/src/backend/cocoa/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,23 @@ extern "C" fn release(info: *const c_void) {
}

extern "C" fn callback(_timer: CFRunLoopTimerRef, info: *mut c_void) {
let state_rc = unsafe { Rc::from_raw(info as *const TimerState) };
let state = Rc::clone(&state_rc);
let _ = Rc::into_raw(state_rc);

let app = AppHandle::from_inner(AppInner {
state: Rc::clone(&state.app_state),
});
let result = panic::catch_unwind(AssertUnwindSafe(|| {
let state_rc = unsafe { Rc::from_raw(info as *const TimerState) };
let state = Rc::clone(&state_rc);
let _ = Rc::into_raw(state_rc);

app.inner.state.catch_unwind(|| {
let app = AppHandle::from_inner(AppInner {
state: Rc::clone(&state.app_state),
});
let timer = Timer::from_inner(TimerInner { state });
let cx = TimerContext::new(&app, &timer);
timer.inner.state.handler.borrow_mut()(&cx);
});
}));

if let Err(panic) = result {
let state = unsafe { &*(info as *const TimerState) };
state.app_state.propagate_panic(panic);
}
}

struct TimerState {
Expand Down
32 changes: 20 additions & 12 deletions reflector-platform/src/backend/cocoa/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ impl View {
unsafe { &*(self.state_ivar().get() as *const WindowState) }
}

fn catch_unwind<F: FnOnce()>(&self, f: F) {
let result = panic::catch_unwind(AssertUnwindSafe(f));

if let Err(panic) = result {
self.state().app_state.propagate_panic(panic);
}
}

pub fn handle_event(&self, event: Event) -> Option<Response> {
let state_rc = unsafe { Rc::from_raw(self.state_ivar().get() as *const WindowState) };
let state = Rc::clone(&state_rc);
Expand Down Expand Up @@ -207,19 +215,19 @@ impl View {
}

unsafe extern "C" fn mouse_entered(&self, _: Sel, _event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
self.handle_event(Event::MouseEnter);
});
}

unsafe extern "C" fn mouse_exited(&self, _: Sel, _event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
self.handle_event(Event::MouseExit);
});
}

unsafe extern "C" fn mouse_moved(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let Some(event) = event else {
return;
};
Expand All @@ -233,7 +241,7 @@ impl View {
}

unsafe extern "C" fn mouse_down(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let result = self.handle_event(Event::MouseDown(MouseButton::Left));

if result != Some(Response::Capture) {
Expand All @@ -243,7 +251,7 @@ impl View {
}

unsafe extern "C" fn mouse_up(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let result = self.handle_event(Event::MouseUp(MouseButton::Left));

if result != Some(Response::Capture) {
Expand All @@ -253,7 +261,7 @@ impl View {
}

unsafe extern "C" fn right_mouse_down(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let result = self.handle_event(Event::MouseDown(MouseButton::Right));

if result != Some(Response::Capture) {
Expand All @@ -263,7 +271,7 @@ impl View {
}

unsafe extern "C" fn right_mouse_up(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let result = self.handle_event(Event::MouseUp(MouseButton::Right));

if result != Some(Response::Capture) {
Expand All @@ -273,7 +281,7 @@ impl View {
}

unsafe extern "C" fn other_mouse_down(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let Some(event) = event else {
return;
};
Expand All @@ -292,7 +300,7 @@ impl View {
}

unsafe extern "C" fn other_mouse_up(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let Some(event) = event else {
return;
};
Expand All @@ -311,7 +319,7 @@ impl View {
}

unsafe extern "C" fn scroll_wheel(&self, _: Sel, event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
let Some(event) = event else {
return;
};
Expand All @@ -332,13 +340,13 @@ impl View {
}

unsafe extern "C" fn cursor_update(&self, _: Sel, _event: Option<&NSEvent>) {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
self.state().update_cursor();
});
}

unsafe extern "C" fn window_should_close(&self, _: Sel, _sender: &NSWindow) -> Bool {
self.state().app_state.catch_unwind(|| {
self.catch_unwind(|| {
self.handle_event(Event::Close);
});

Expand Down

0 comments on commit 9295afd

Please sign in to comment.