Skip to content

Commit f98a747

Browse files
author
Sampson Gao
committed
Replace napi_is_construct_call with napi_get_new_target
1 parent ca92548 commit f98a747

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

napi-inl.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,8 +2016,9 @@ inline CallbackInfo::~CallbackInfo() {
20162016
}
20172017

20182018
inline bool CallbackInfo::IsConstructCall() const {
2019-
bool isConstructCall;
2020-
napi_status status = napi_is_construct_call(_env, _info, &isConstructCall);
2019+
napi_value new_target;
2020+
napi_status status = napi_get_new_target(_env, _info, &new_target);
2021+
bool isConstructCall = (new_target != nullptr);
20212022
NAPI_THROW_IF_FAILED(_env, status, false);
20222023
return isConstructCall;
20232024
}
@@ -2470,10 +2471,11 @@ template <typename T>
24702471
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
24712472
napi_env env,
24722473
napi_callback_info info) {
2473-
bool isConstructCall;
2474-
napi_status status = napi_is_construct_call(env, info, &isConstructCall);
2474+
napi_value new_target;
2475+
napi_status status = napi_get_new_target(env, info, &new_target);
24752476
if (status != napi_ok) return nullptr;
24762477

2478+
bool isConstructCall = (new_target != nullptr);
24772479
if (!isConstructCall) {
24782480
napi_throw_type_error(env, nullptr, "Class constructors cannot be invoked without 'new'");
24792481
return nullptr;

src/node_api.cc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ class CallbackWrapper {
438438
CallbackWrapper(napi_value this_arg, size_t args_length, void* data)
439439
: _this(this_arg), _args_length(args_length), _data(data) {}
440440

441-
virtual bool IsConstructCall() = 0;
441+
virtual napi_value NewTarget() = 0;
442442
virtual void Args(napi_value* buffer, size_t bufferlength) = 0;
443443
virtual void SetReturnValue(napi_value value) = 0;
444444

@@ -467,8 +467,7 @@ class CallbackWrapperBase : public CallbackWrapper {
467467
->Value();
468468
}
469469

470-
/*virtual*/
471-
bool IsConstructCall() override { return false; }
470+
napi_value NewTarget() override { return nullptr; }
472471

473472
protected:
474473
void InvokeCallback() {
@@ -516,8 +515,13 @@ class FunctionCallbackWrapper
516515
const v8::FunctionCallbackInfo<v8::Value>& cbinfo)
517516
: CallbackWrapperBase(cbinfo, cbinfo.Length()) {}
518517

519-
/*virtual*/
520-
bool IsConstructCall() override { return _cbinfo.IsConstructCall(); }
518+
napi_value NewTarget() override {
519+
if (_cbinfo.IsConstructCall()) {
520+
return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget());
521+
} else {
522+
return nullptr;
523+
}
524+
}
521525

522526
/*virtual*/
523527
void Args(napi_value* buffer, size_t buffer_length) override {
@@ -1810,18 +1814,17 @@ napi_status napi_get_cb_info(
18101814
return napi_clear_last_error(env);
18111815
}
18121816

1813-
napi_status napi_is_construct_call(napi_env env,
1814-
napi_callback_info cbinfo,
1815-
bool* result) {
1816-
// Omit NAPI_PREAMBLE and GET_RETURN_STATUS because no V8 APIs are called.
1817+
napi_status napi_get_new_target(napi_env env,
1818+
napi_callback_info cbinfo,
1819+
napi_value* result) {
18171820
CHECK_ENV(env);
18181821
CHECK_ARG(env, cbinfo);
18191822
CHECK_ARG(env, result);
18201823

18211824
v8impl::CallbackWrapper* info =
18221825
reinterpret_cast<v8impl::CallbackWrapper*>(cbinfo);
18231826

1824-
*result = info->IsConstructCall();
1827+
*result = info->NewTarget();
18251828
return napi_clear_last_error(env);
18261829
}
18271830

src/node_api.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,9 @@ NAPI_EXTERN napi_status napi_get_cb_info(
340340
napi_value* this_arg, // [out] Receives the JS 'this' arg for the call
341341
void** data); // [out] Receives the data pointer for the callback.
342342

343-
NAPI_EXTERN napi_status napi_is_construct_call(napi_env env,
344-
napi_callback_info cbinfo,
345-
bool* result);
343+
NAPI_EXTERN napi_status napi_get_new_target(napi_env env,
344+
napi_callback_info cbinfo,
345+
napi_value* result);
346346
NAPI_EXTERN napi_status
347347
napi_define_class(napi_env env,
348348
const char* utf8name,

0 commit comments

Comments
 (0)