Skip to content
Permalink
Browse files

n-api: napi_is_construct_call->napi_get_new_target

Remove napi_is_construct_call and introduce napi_get_new_target.

PR-URL: #14698
Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Kyle Farnung <kfarnung@microsoft.com>
  • Loading branch information...
Sampson Gao authored and jasnell committed Aug 8, 2017
1 parent 4d26c68 commit a2d340fb0083fa92c1e054de4c986fc7cbbac18e
Showing with 27 additions and 24 deletions.
  1. +8 −9 doc/api/n-api.md
  2. +13 −10 src/node_api.cc
  3. +3 −3 src/node_api.h
  4. +3 −2 test/addons-napi/6_object_wrap/myobject.cc
@@ -2918,25 +2918,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
@@ -446,7 +446,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;

@@ -475,8 +475,7 @@ class CallbackWrapperBase : public CallbackWrapper {
->Value();
}

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

protected:
void InvokeCallback() {
@@ -524,8 +523,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 {
@@ -1874,18 +1878,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);
}

@@ -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,
napi_callback_info cbinfo,
napi_value* result);
NAPI_EXTERN napi_status
napi_define_class(napi_env env,
const char* utf8name,
@@ -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];

0 comments on commit a2d340f

Please sign in to comment.
You can’t perform that action at this time.