Skip to content

Commit

Permalink
n-api: napi_is_construct_call->napi_get_new_target
Browse files Browse the repository at this point in the history
Remove napi_is_construct_call and introduce napi_get_new_target.

PR-URL: #14698
Reviewed-By: Jason Ginchereau <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Kyle Farnung <[email protected]>
  • Loading branch information
Sampson Gao authored and mhdawson committed Sep 14, 2017
1 parent 0c258bd commit 973c12f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
17 changes: 8 additions & 9 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2928,25 +2928,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 @@ -444,7 +444,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 @@ -473,8 +473,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 @@ -522,8 +521,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 @@ -1878,18 +1882,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 @@ -330,9 +330,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,
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

0 comments on commit 973c12f

Please sign in to comment.