Skip to content

Make throw more efficient #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions extern/exception.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <objc/objc.h>
#include <objc/NSObject.h>

void RustObjCExceptionThrow(id exception) {
@throw exception;
}

int RustObjCExceptionTryCatch(void (*try)(void *), void *context, id *error) {
@try {
try(context);
Expand Down
19 changes: 15 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ use std::os::raw::{c_int, c_void};
use std::ptr;

#[link(name = "objc", kind = "dylib")]
extern { }
extern "C" { // TODO: "C-unwind"
/// See [`objc-exception.h`][objc-exception].
///
/// [objc-exception]: https://opensource.apple.com/source/objc4/objc4-818.2/runtime/objc-exception.h.auto.html
// Header marks this with _Nonnull, but LLVM output shows otherwise
fn objc_exception_throw(exception: *mut c_void) -> !;
// fn objc_exception_rethrow();
}

extern {
fn RustObjCExceptionThrow(exception: *mut c_void);
fn RustObjCExceptionTryCatch(try: extern fn(*mut c_void),
context: *mut c_void, error: *mut *mut c_void) -> c_int;
}
Expand All @@ -20,9 +26,14 @@ pub enum Exception { }
/// The argument must be a pointer to an Objective-C object.
///
/// Unsafe because this unwinds from Objective-C.
///
/// This also invokes undefined behaviour until `C-unwind` is stabilized, see
/// [RFC-2945].
///
/// [RFC-2945]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html
#[inline]
pub unsafe fn throw(exception: *mut Exception) -> ! {
RustObjCExceptionThrow(exception as *mut _);
unreachable!();
objc_exception_throw(exception as *mut _)
}

unsafe fn try_no_ret<F>(closure: F) -> Result<(), *mut Exception>
Expand Down