Skip to content

Question about Napi::TypeError ctor #1271

Closed as not planned
Closed as not planned

Description

@JckXia

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!

Activity

  1. gabrielschulhof commented on Mar 22, 2023

    @gabrielschulhof
    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.
  2. github-actions commented on Jun 21, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions