summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSampson Gao <sampsong@ca.ibm.com>2017-08-08 16:21:56 -0400
committerMichael Dawson <mdawson@devrus.com>2017-09-14 16:21:27 -0400
commit973c12f631c53a9833e0bcd11f5457ebec5269c4 (patch)
tree1fe815b2d1bb2f47d4f5b23d902635c3b4ae93df
parent0c258bdc4040fcc4ab590fc80dbcd2182b4c74ae (diff)
downloadandroid-node-v8-973c12f631c53a9833e0bcd11f5457ebec5269c4.tar.gz
android-node-v8-973c12f631c53a9833e0bcd11f5457ebec5269c4.tar.bz2
android-node-v8-973c12f631c53a9833e0bcd11f5457ebec5269c4.zip
n-api: napi_is_construct_call->napi_get_new_target
Remove napi_is_construct_call and introduce napi_get_new_target. PR-URL: https://github.com/nodejs/node/pull/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>
-rw-r--r--doc/api/n-api.md17
-rw-r--r--src/node_api.cc23
-rw-r--r--src/node_api.h6
-rw-r--r--test/addons-napi/6_object_wrap/myobject.cc5
4 files changed, 27 insertions, 24 deletions
diff --git a/doc/api/n-api.md b/doc/api/n-api.md
index d039e92589..00bba55a4c 100644
--- a/doc/api/n-api.md
+++ b/doc/api/n-api.md
@@ -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
diff --git a/src/node_api.cc b/src/node_api.cc
index ef51599648..724d5c3ee9 100644
--- a/src/node_api.cc
+++ b/src/node_api.cc
@@ -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;
@@ -473,8 +473,7 @@ class CallbackWrapperBase : public CallbackWrapper {
->Value();
}
- /*virtual*/
- bool IsConstructCall() override { return false; }
+ napi_value NewTarget() override { return nullptr; }
protected:
void InvokeCallback() {
@@ -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 {
@@ -1878,10 +1882,9 @@ 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);
@@ -1889,7 +1892,7 @@ napi_status napi_is_construct_call(napi_env env,
v8impl::CallbackWrapper* info =
reinterpret_cast<v8impl::CallbackWrapper*>(cbinfo);
- *result = info->IsConstructCall();
+ *result = info->NewTarget();
return napi_clear_last_error(env);
}
diff --git a/src/node_api.h b/src/node_api.h
index c7d5e746f1..e1a40983a9 100644
--- a/src/node_api.h
+++ b/src/node_api.h
@@ -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,
diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc
index c214e014b9..90815253ad 100644
--- a/test/addons-napi/6_object_wrap/myobject.cc
+++ b/test/addons-napi/6_object_wrap/myobject.cc
@@ -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];