From f41fdd144de087ce19f8774ce0eeb66a36631e39 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 16:21:56 -0400 Subject: [PATCH 01/12] Remove napi_is_construct_call and introduce napi_get_new_target --- src/node_api.cc | 17 +++++++++++------ src/node_api.h | 6 +++--- test/addons-napi/6_object_wrap/myobject.cc | 5 +++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index b84a33e510f264..7b2312e6f242db 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -439,11 +439,13 @@ class CallbackWrapper { : _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; napi_value This() { return _this; } + size_t ArgsLength() { return _args_length; } void* Data() { return _data; } @@ -469,6 +471,7 @@ class CallbackWrapperBase : public CallbackWrapper { /*virtual*/ bool IsConstructCall() override { return false; } + napi_value NewTarget() override { return nullptr; } protected: void InvokeCallback() { @@ -518,6 +521,9 @@ class FunctionCallbackWrapper /*virtual*/ bool IsConstructCall() override { return _cbinfo.IsConstructCall(); } + napi_value NewTarget() override { + return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget()); + } /*virtual*/ void Args(napi_value* buffer, size_t buffer_length) override { @@ -1810,18 +1816,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(cbinfo); + reinterpret_cast(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 0cf0ba046997d3..750fcd377a7b1d 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -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, diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index 56b00ddae49a32..d44efcd1940364 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 != NULL ); size_t argc = 1; napi_value args[1]; From c99c54dbcd2117c09953eeee40815ab08306bf58 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 17:06:50 -0400 Subject: [PATCH 02/12] Fix indentation typo in node_api.cc --- src/node_api.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index 7b2312e6f242db..5b680c74ce8572 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1824,7 +1824,7 @@ napi_status napi_get_new_target(napi_env env, CHECK_ARG(env, result); v8impl::CallbackWrapper* info = - reinterpret_cast(cbinfo); + reinterpret_cast(cbinfo); *result = info->NewTarget(); return napi_clear_last_error(env); From b1a24a9ec1df5394506b3e834cd9eed50d2c501a Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 17:08:07 -0400 Subject: [PATCH 03/12] Remove extra line --- src/node_api.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index 5b680c74ce8572..d785fa4c55d3f4 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -445,7 +445,6 @@ class CallbackWrapper { napi_value This() { return _this; } - size_t ArgsLength() { return _args_length; } void* Data() { return _data; } From 108fe66cd9d327144e60d038024cb66162e5e186 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 17:09:47 -0400 Subject: [PATCH 04/12] Remove CallbackWrapper.IsConstructCall --- src/node_api.cc | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index d785fa4c55d3f4..54d3b498c3a515 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -438,7 +438,6 @@ 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; @@ -468,8 +467,6 @@ class CallbackWrapperBase : public CallbackWrapper { ->Value(); } - /*virtual*/ - bool IsConstructCall() override { return false; } napi_value NewTarget() override { return nullptr; } protected: @@ -518,8 +515,6 @@ class FunctionCallbackWrapper const v8::FunctionCallbackInfo& cbinfo) : CallbackWrapperBase(cbinfo, cbinfo.Length()) {} - /*virtual*/ - bool IsConstructCall() override { return _cbinfo.IsConstructCall(); } napi_value NewTarget() override { return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget()); } From 9b83e2291a6a0943be9b28d44579185c3c11bde6 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 17:13:21 -0400 Subject: [PATCH 05/12] Change NULL to nullptr in addons-napi test --- test/addons-napi/6_object_wrap/myobject.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/addons-napi/6_object_wrap/myobject.cc b/test/addons-napi/6_object_wrap/myobject.cc index d44efcd1940364..6e8cd2aecb4ecc 100644 --- a/test/addons-napi/6_object_wrap/myobject.cc +++ b/test/addons-napi/6_object_wrap/myobject.cc @@ -34,7 +34,7 @@ void MyObject::Init(napi_env env, napi_value exports) { napi_value MyObject::New(napi_env env, napi_callback_info info) { napi_value new_target; NAPI_CALL(env, napi_get_new_target(env, info, &new_target)); - bool is_constructor = ( new_target != NULL ); + bool is_constructor = ( new_target != nullptr ); size_t argc = 1; napi_value args[1]; From 6fe53be326fcf2c53d94c73eb84294b962610d1b Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 17:29:43 -0400 Subject: [PATCH 06/12] Update n-api doc for napi_get_new_target --- doc/api/n-api.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 04af7a62723c70..0a749e586457a3 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -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* ```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 construct call. Returns `napi_ok` if the API succeeded. -This API checks if the the current callback was due to a -consructor call. +This API return the `new.target` of the construct call. If the current +callback is not a constructor call, the result equals to `nulltpr`. ### *napi_new_instance* ```C napi_status napi_get_new_target(napi_env env, From b733c7858f6565ffe4f0da7dcc803c1083f4d3ee Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 17:59:03 -0400 Subject: [PATCH 10/12] Rephase using constructor call --- doc/api/n-api.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 68e70cd6e234ef..dacafd11f3264e 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2901,11 +2901,11 @@ napi_status napi_get_new_target(napi_env env, - `[in] env`: The environment that the API is invoked under. - `[in] cbinfo`: The callback info passed into the callback function. -- `[out] result`: The `new.target` of the construct call. +- `[out] result`: The `new.target` of the constructor call. Returns `napi_ok` if the API succeeded. -This API returns the `new.target` of the construct call. If the current +This API returns the `new.target` of the constructor call. If the current callback is not a constructor call, the result is `nulltpr`. ### *napi_new_instance* From f6ec28b6bfcc7dfe4c26db39ff00442b5314e030 Mon Sep 17 00:00:00 2001 From: Sampson Gao Date: Tue, 8 Aug 2017 18:02:59 -0400 Subject: [PATCH 11/12] Fix typo in doc --- doc/api/n-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index dacafd11f3264e..765b1d18170ab6 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -2906,7 +2906,7 @@ napi_status napi_get_new_target(napi_env env, Returns `napi_ok` if the API succeeded. This API returns the `new.target` of the constructor call. If the current -callback is not a constructor call, the result is `nulltpr`. +callback is not a constructor call, the result is `nullptr`. ### *napi_new_instance*