From b27fa80fdc525e75797084799c5ecc63847decee Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Wed, 7 Mar 2018 20:30:14 -0800 Subject: [PATCH] src: update `napi_get_new_target` to match docs The implementation for `napi_get_new_target` needs to return nullptr when the function is not being called as a constructor. PR-URL: https://github.com/nodejs/node-chakracore/pull/490 Reviewed-By: Taylor Woll Reviewed-By: Jimmy Thomson --- src/node_api_jsrt.cc | 12 ++++++++++-- test/addons-napi/test_new_target/binding.c | 9 +++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/node_api_jsrt.cc b/src/node_api_jsrt.cc index b0eb85780db..f3cdcdee73a 100644 --- a/src/node_api_jsrt.cc +++ b/src/node_api_jsrt.cc @@ -89,9 +89,10 @@ static void napi_clear_last_error(); struct CallbackInfo { napi_value newTarget; napi_value thisArg; - uint16_t argc; napi_value* argv; void* data; + uint16_t argc; + bool isConstructCall; }; struct napi_env__ { @@ -240,6 +241,7 @@ class ExternalCallback { CallbackInfo cbInfo; cbInfo.thisArg = reinterpret_cast(info->thisArg); cbInfo.newTarget = reinterpret_cast(info->newTargetArg); + cbInfo.isConstructCall = info->isConstructCall; cbInfo.argc = argumentCount - 1; cbInfo.argv = reinterpret_cast(arguments + 1); cbInfo.data = externalCallback->_data; @@ -1386,8 +1388,14 @@ napi_status napi_get_new_target(napi_env env, napi_value* result) { CHECK_ARG(cbinfo); CHECK_ARG(result); + const CallbackInfo *info = reinterpret_cast(cbinfo); - *result = info->newTarget; + if (info->isConstructCall) { + *result = info->newTarget; + } else { + *result = nullptr; + } + return napi_ok; } diff --git a/test/addons-napi/test_new_target/binding.c b/test/addons-napi/test_new_target/binding.c index b511ca4624d..3cee0d4d8a3 100644 --- a/test/addons-napi/test_new_target/binding.c +++ b/test/addons-napi/test_new_target/binding.c @@ -15,6 +15,7 @@ napi_value BaseClass(napi_env env, napi_callback_info info) { NAPI_ASSERT(env, !result, "this !== new.target"); // new.target !== undefined because we should be called as a new expression + NAPI_ASSERT(env, newTargetArg != NULL, "newTargetArg != NULL"); NAPI_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result)); NAPI_ASSERT(env, !result, "new.target !== undefined"); @@ -33,6 +34,7 @@ napi_value Constructor(napi_env env, napi_callback_info info) { NAPI_CALL(env, napi_get_undefined(env, &undefined)); // new.target !== undefined because we should be called as a new expression + NAPI_ASSERT(env, newTargetArg != NULL, "newTargetArg != NULL"); NAPI_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result)); NAPI_ASSERT(env, !result, "new.target !== undefined"); @@ -44,15 +46,10 @@ napi_value Constructor(napi_env env, napi_callback_info info) { } napi_value OrdinaryFunction(napi_env env, napi_callback_info info) { - bool result; napi_value newTargetArg; NAPI_CALL(env, napi_get_new_target(env, info, &newTargetArg)); - napi_value undefined; - NAPI_CALL(env, napi_get_undefined(env, &undefined)); - // new.target === undefined because we are not called as a new expression - NAPI_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result)); - NAPI_ASSERT(env, result, "new.target === undefined"); + NAPI_ASSERT(env, newTargetArg == NULL, "newTargetArg == NULL"); napi_value _true; NAPI_CALL(env, napi_get_boolean(env, true, &_true));