Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
src: update napi_get_new_target to match docs
Browse files Browse the repository at this point in the history
The implementation for `napi_get_new_target` needs to return nullptr
when the function is not being called as a constructor.

PR-URL: #490
Reviewed-By: Taylor Woll <[email protected]>
Reviewed-By: Jimmy Thomson <[email protected]>
  • Loading branch information
kfarnung committed Mar 8, 2018
1 parent 6566808 commit c8b29ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/node_api_jsrt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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__ {
Expand Down Expand Up @@ -240,6 +241,7 @@ class ExternalCallback {
CallbackInfo cbInfo;
cbInfo.thisArg = reinterpret_cast<napi_value>(info->thisArg);
cbInfo.newTarget = reinterpret_cast<napi_value>(info->newTargetArg);
cbInfo.isConstructCall = info->isConstructCall;
cbInfo.argc = argumentCount - 1;
cbInfo.argv = reinterpret_cast<napi_value*>(arguments + 1);
cbInfo.data = externalCallback->_data;
Expand Down Expand Up @@ -1385,8 +1387,14 @@ napi_status napi_get_new_target(napi_env env,
napi_value* result) {
CHECK_ARG(cbinfo);
CHECK_ARG(result);

const CallbackInfo *info = reinterpret_cast<CallbackInfo*>(cbinfo);
*result = info->newTarget;
if (info->isConstructCall) {
*result = info->newTarget;
} else {
*result = nullptr;
}

return napi_ok;
}

Expand Down
9 changes: 3 additions & 6 deletions test/addons-napi/test_new_target/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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");

Expand All @@ -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));
Expand Down

0 comments on commit c8b29ba

Please sign in to comment.