-
Notifications
You must be signed in to change notification settings - Fork 286
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
(wip) try_catch #533
(wip) try_catch #533
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ pub mod convert; | |
pub mod class; | ||
pub mod task; | ||
pub mod handler; | ||
pub mod try_catch; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// Creates a `Nan::TryCatch` | ||
pub use neon_sys::Neon_TryCatch_New as new; | ||
|
||
/// Returns a boolean indicating if an exception has been caught | ||
pub use neon_sys::Neon_TryCatch_HasCaught as has_caught; | ||
|
||
/// Returns an exception if one has been caught | ||
pub use neon_sys::Neon_TryCatch_Exception as exception; | ||
|
||
/// Deletes an instance of `Nan::TryCatch` | ||
pub use neon_sys::Neon_TryCatch_Delete as delete; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,16 @@ pub type JsResult<'b, T> = NeonResult<Handle<'b, T>>; | |
pub trait JsResultExt<'a, V: Value> { | ||
fn or_throw<'b, C: Context<'b>>(self, cx: &mut C) -> JsResult<'a, V>; | ||
} | ||
|
||
impl <'a, V, E> JsResultExt<'a, V> for Result<Handle<'a, V>, Handle<'a, E>> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand the use case for this. If I wrap something in a Is this for cases where you want to map and do something back in the JS VM? Like: cx.try_catch(do_something)
.and_then(|v| {
... cx ... // do stuff in the JS VM
})
.or_throw(cx) |
||
where | ||
V: Value, | ||
E: Value, | ||
{ | ||
fn or_throw<'b, C: Context<'b>>(self, cx: &mut C) -> JsResult<'a, V> { | ||
match self { | ||
Ok(v) => Ok(v), | ||
Err(err) => cx.throw(err), | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this isn't going to work. :( a
TryCatch
has to be allocated on the stack. There are a couple ways to do this, both of which are pretty annoying:void*
to C along with a Rust glue function that takes the Rust closure as an argument and calls in. Then in C you stack-allocate theTryCatch
and invoke the Rust glue function.TryCatch
on the Rust stack by creating a parallel Rust struct type that's big enough to contain the sizeof the C++TryCatch
. For this you have to have some way at build time to determine the sizeof aTryCatch
and use a macro or a const declaration to declare the Rust type. And then the constructor and destructor for that Rust type can call intoneon_runtime
with a pointer toself
for the C++ code to explicitly call the C++ constructor and destructor on it. This is howHandleScope
works, which also has to be stack allocated.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that the reason they should be stack allocated is for nesting to work properly they need to be deallocated in the same order they were created.
RII also enforces this. I think deleting with
Drop
would also accomplish this.With that said, I think your suggestion of passing the Rust closure to be invoked by C is best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, panicking across FFI is undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this is neat. rust-lang/rust#65646