Skip to content
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

Closed
wants to merge 14 commits into from

Conversation

anshulrgoyal
Copy link
Contributor

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

@anshulrgoyal
Copy link
Contributor Author

@dherman There is a problem with handling panic!(error) all the napi function require napi_env which is not available in the panic_hook.

Copy link
Member

@kjvalencik kjvalencik left a 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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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 {
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -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.
///
///
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -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")]
Copy link
Member

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?

Copy link
Contributor Author

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() {
Copy link
Member

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?

Copy link
Contributor Author

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

@anshulrgoyal
Copy link
Contributor Author

@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.

@kjvalencik
Copy link
Member

kjvalencik commented Mar 25, 2020

Which piece of code are you referring to as the panic hook problem? Do you mean convert_panics?

I see the problem convert_panics requires an env, but, it was consumed by the call. We could potentially alias the raw pointer to napi_env via to_raw(), but, I would need to think about the implications and if that's safe.

I think at the very least it's not any less safe because that's what Isolate::current() is implicitly doing.

@anshulrgoyal
Copy link
Contributor Author

@kjvalencik would you moving forward with this pr?

@anshulrgoyal
Copy link
Contributor Author

Which piece of code are you referring to as the panic hook problem? Do you mean convert_panics?

I see the problem convert_panics requires an env, but, it was consumed by the call. We could potentially alias the raw pointer to napi_env via to_raw(), but, I would need to think about the implications and if that's safe.

I think at the very least it's not any less safe because that's what Isolate::current() is implicitly doing.

I donot think aliasing will cause a problem since the napi_env is valid till the addon is mounted.

Copy link
Collaborator

@dherman dherman left a 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
Copy link
Collaborator

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.

crates/neon-runtime/src/napi/error.rs Outdated Show resolved Hide resolved
src/types/error.rs Show resolved Hide resolved
crates/neon-runtime/src/napi/error.rs Outdated Show resolved Hide resolved
src/object/class/internal.rs Outdated Show resolved Hide resolved
src/object/class/internal.rs Outdated Show resolved Hide resolved
src/types/error.rs Show resolved Hide resolved
src/types/error.rs Show resolved Hide resolved
src/types/error.rs Show resolved Hide resolved
@kjvalencik
Copy link
Member

kjvalencik commented Jul 7, 2020

@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

@kjvalencik kjvalencik closed this Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants