-
Notifications
You must be signed in to change notification settings - Fork 285
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
Migrated error type to napi error #505
Conversation
@dherman There is a problem with handling |
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.
Thanks for contributing to neon! Let me know if you have any questions on my comments.
pub unsafe extern "C" fn new_error(_out: &mut Local, _msg: Local) { unimplemented!() } | ||
pub unsafe extern "C" fn throw(env:Env,error: Local)->bool { | ||
let status=napi::napi_throw(env,error); | ||
status==napi::napi_status::napi_ok |
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.
#Fix: This should assert napi_ok
similar to other methods instead of returning a boolean. The Rust code is not checking the result.
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.
fixed
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.
Let's still remove the bool
return type from this function as @kjvalencik suggests. Once you've asserted that status
is napi_ok
it's not helpful to test that comparison a second time and then ignore the result anyway.
.gitignore
Outdated
@@ -10,3 +10,5 @@ cli/lib | |||
test/cli/lib | |||
npm-debug.log | |||
rls*.log | |||
.idea |
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.
#Fix: Remove this line. I recommend creating a global git ignore.
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.
fixed
|
||
pub unsafe extern "C" fn new_type_error(_out: &mut Local, _msg: Local) { unimplemented!() } | ||
pub unsafe extern "C" fn new_error(out: &mut Local,env:Env,code:Local,msg:Local)->bool { |
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.
#Fix: Please follow formatting of other functions.
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.
fixed
src/context/mod.rs
Outdated
@@ -115,22 +115,22 @@ impl<'a> Lock<'a> { | |||
} | |||
|
|||
/// An _execution context_, which provides context-sensitive access to the JavaScript engine. Most operations that interact with the engine require passing a reference to a context. | |||
/// | |||
/// |
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.
#Fix: Please remove unrelated formatting changes.
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.
fixed
src/types/error.rs
Outdated
@@ -36,6 +36,7 @@ impl Object for JsError { } | |||
|
|||
impl JsError { | |||
/// Creates a direct instance of the [`Error`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error) class. | |||
#[cfg(feature = "legacy-runtime")] |
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.
#Answer: Since these functions share a function signature, what do you think of putting the cfg flag on the body instead of on the function to ensure conformance?
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.
fixed
@@ -44,7 +45,23 @@ impl JsError { | |||
}) | |||
} | |||
|
|||
#[cfg(feature = "napi-runtime")] | |||
pub fn error<'a, C: Context<'a>, S: AsRef<str>>(cx: &mut C, msg: S) -> NeonResult<Handle<'a, JsError>> { | |||
let (ptr, len) = if let Some(small) = Utf8::from(msg.as_ref()).into_small() { |
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.
Are we able to re-use the JsString
code for this?
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.
JsString will create a struct and then i have to use to_raw on it is not ideal according to me
@kjvalencik I am still stuck with panic hook problem. So the problem is that all napi function require napi_env as there first argument and since it is not available in panic hook i am not able to implement it properly. |
I see the problem I think at the very least it's not any less safe because that's what |
@kjvalencik would you moving forward with this pr? |
I donot think aliasing will cause a problem since the |
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.
Lots of good stuff in here, thanks so much! I have a few more requested changes. Thanks!
pub unsafe extern "C" fn new_error(_out: &mut Local, _msg: Local) { unimplemented!() } | ||
pub unsafe extern "C" fn throw(env:Env,error: Local)->bool { | ||
let status=napi::napi_throw(env,error); | ||
status==napi::napi_status::napi_ok |
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.
Let's still remove the bool
return type from this function as @kjvalencik suggests. Once you've asserted that status
is napi_ok
it's not helpful to test that comparison a second time and then ignore the result anyway.
@anshulrgoyal Thanks so much for your contribution! I'm sorry that I let this fall behind. I took the liberty of picking up your PR, resolving conflicts, and closing out the last couple of remaining questions. Thanks so much! #542 |
Description
This pull request try to migrate nan error management to napi implementation using nodejs-sys as backend. The napi provide few methods for error which are documented below:
napi_throw
this throws an error.napi_create_error
this create a new error.napi_create_type_error
this create a new type error.napi_create_range_error
this create a new range error.Related Issue
#444