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

napi: Remove napi_is_construct_call and introduce napi_get_new_target #14698

Closed
wants to merge 12 commits into from
17 changes: 8 additions & 9 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2889,25 +2889,24 @@ Returns `napi_ok` if the API succeeded.
This method is used within a callback function to retrieve details about the
call like the arguments and the `this` pointer from a given callback info.

### *napi_is_construct_call*
### *napi_get_new_target*
<!-- YAML
added: v8.0.0
added: REPLACEME
-->
```C
napi_status napi_is_construct_call(napi_env env,
napi_callback_info cbinfo,
bool* result)
napi_status napi_get_new_target(napi_env env,
napi_callback_info cbinfo,
napi_value* result)
```

- `[in] env`: The environment that the API is invoked under.
- `[in] cbinfo`: The callback info passed into the callback function.
- `[out] result`: Whether the native function is being invoked as
a constructor call.
- `[out] result`: The `new.target` of the constructor call.

Returns `napi_ok` if the API succeeded.

This API checks if the the current callback was due to a
consructor call.
This API returns the `new.target` of the constructor call. If the current
callback is not a constructor call, the result is `nullptr`.

### *napi_new_instance*
<!-- YAML
Expand Down
23 changes: 13 additions & 10 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ class CallbackWrapper {
CallbackWrapper(napi_value this_arg, size_t args_length, void* data)
: _this(this_arg), _args_length(args_length), _data(data) {}

virtual bool IsConstructCall() = 0;
virtual napi_value NewTarget() = 0;
virtual void Args(napi_value* buffer, size_t bufferlength) = 0;
virtual void SetReturnValue(napi_value value) = 0;

Expand Down Expand Up @@ -467,8 +467,7 @@ class CallbackWrapperBase : public CallbackWrapper {
->Value();
}

/*virtual*/
bool IsConstructCall() override { return false; }
napi_value NewTarget() override { return nullptr; }

protected:
void InvokeCallback() {
Expand Down Expand Up @@ -516,8 +515,13 @@ class FunctionCallbackWrapper
const v8::FunctionCallbackInfo<v8::Value>& cbinfo)
: CallbackWrapperBase(cbinfo, cbinfo.Length()) {}

/*virtual*/
bool IsConstructCall() override { return _cbinfo.IsConstructCall(); }
napi_value NewTarget() override {
if (_cbinfo.IsConstructCall()) {
return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget());
} else {
return nullptr;
}
}

/*virtual*/
void Args(napi_value* buffer, size_t buffer_length) override {
Expand Down Expand Up @@ -1810,18 +1814,17 @@ napi_status napi_get_cb_info(
return napi_clear_last_error(env);
}

napi_status napi_is_construct_call(napi_env env,
napi_callback_info cbinfo,
bool* result) {
// Omit NAPI_PREAMBLE and GET_RETURN_STATUS because no V8 APIs are called.
napi_status napi_get_new_target(napi_env env,
napi_callback_info cbinfo,
napi_value* result) {
CHECK_ENV(env);
CHECK_ARG(env, cbinfo);
CHECK_ARG(env, result);

v8impl::CallbackWrapper* info =
reinterpret_cast<v8impl::CallbackWrapper*>(cbinfo);

*result = info->IsConstructCall();
*result = info->NewTarget();
return napi_clear_last_error(env);
}

Expand Down
6 changes: 3 additions & 3 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ NAPI_EXTERN napi_status napi_get_cb_info(
napi_value* this_arg, // [out] Receives the JS 'this' arg for the call
void** data); // [out] Receives the data pointer for the callback.

NAPI_EXTERN napi_status napi_is_construct_call(napi_env env,
napi_callback_info cbinfo,
bool* result);
NAPI_EXTERN napi_status napi_get_new_target(napi_env env,
Copy link
Member

Choose a reason for hiding this comment

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

Need to also update doc\api\n-api.md.

napi_callback_info cbinfo,
napi_value* result);
NAPI_EXTERN napi_status
napi_define_class(napi_env env,
const char* utf8name,
Expand Down
5 changes: 3 additions & 2 deletions test/addons-napi/6_object_wrap/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ void MyObject::Init(napi_env env, napi_value exports) {
}

napi_value MyObject::New(napi_env env, napi_callback_info info) {
bool is_constructor;
NAPI_CALL(env, napi_is_construct_call(env, info, &is_constructor));
napi_value new_target;
NAPI_CALL(env, napi_get_new_target(env, info, &new_target));
bool is_constructor = (new_target != nullptr);

size_t argc = 1;
napi_value args[1];
Expand Down