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

Question about Napi::TypeError ctor #1271

Closed
JckXia opened this issue Jan 15, 2023 · 2 comments
Closed

Question about Napi::TypeError ctor #1271

JckXia opened this issue Jan 15, 2023 · 2 comments
Labels

Comments

@JckXia
Copy link
Member

JckXia commented Jan 15, 2023

Hello everyone, just a question regarding the expected behavior of TypeError(napi_env env, napi_value value). According to the docs, it initializes a Napi::TypeError C++ instance from an existing error object, where value is a Napi::Error.

The first problem is that we do not have a cast operator overload that converts a Napi::Error object to a napi_value , so we can't call this ctor with a Napi::Error object directly.

Second of all, my assumption was this ctor accepts an error object and creates a JS TypeError in V8 and a C++ stack object. However, it looks like we simply call the Error ctor with the napi_value. For example, If we give the ctor a napi_value passed from JS, where the napi_value is a RangeError, the resulting Napi::TypedError, when ThrowAsJavaScript, will yield an RangeError still.

So my question is, what is the correct interpretation of this particular API, and what should we do to tweak the implementation to comply with that spec? Thank you!

@gabrielschulhof
Copy link
Contributor

I think this is a general problem with all our constructors of the form

JSValueType::JSValueType(napi_env env, napi_value value): JSValueSuperClass(env, value) {}

the purpose of which is to wrap a value coming in from C. You can wrap any JS value using any of our classes, without validation.

In the next major version, we may wanna

  1. add a ::Wrap(napi_env env, napi_value value) static method to these classes, which performs validation, rejecting, for each class, a napi_value that does not have the corresponding JS type, and
  2. restrict the visibility of the constructor.

@github-actions
Copy link
Contributor

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

@github-actions github-actions bot added the stale label Jun 21, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants