Skip to content

Commit 458f576

Browse files
author
Sampson Gao
committed
Replace napi_is_construct_call with napi_get_new_target
1 parent 790de9f commit 458f576

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

napi-inl.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,11 +2015,15 @@ inline CallbackInfo::~CallbackInfo() {
20152015
}
20162016
}
20172017

2018+
inline Value CallbackInfo::NewTarget() const {
2019+
napi_value newTarget;
2020+
napi_status status = napi_get_new_target(_env, _info, &newTarget);
2021+
NAPI_THROW_IF_FAILED(_env, status, Value());
2022+
return Value(_env, newTarget);
2023+
}
2024+
20182025
inline bool CallbackInfo::IsConstructCall() const {
2019-
bool isConstructCall;
2020-
napi_status status = napi_is_construct_call(_env, _info, &isConstructCall);
2021-
NAPI_THROW_IF_FAILED(_env, status, false);
2022-
return isConstructCall;
2026+
return !NewTarget().IsEmpty();
20232027
}
20242028

20252029
inline Napi::Env CallbackInfo::Env() const {
@@ -2470,10 +2474,11 @@ template <typename T>
24702474
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
24712475
napi_env env,
24722476
napi_callback_info info) {
2473-
bool isConstructCall;
2474-
napi_status status = napi_is_construct_call(env, info, &isConstructCall);
2477+
napi_value new_target;
2478+
napi_status status = napi_get_new_target(env, info, &new_target);
24752479
if (status != napi_ok) return nullptr;
24762480

2481+
bool isConstructCall = (new_target != nullptr);
24772482
if (!isConstructCall) {
24782483
napi_throw_type_error(env, nullptr, "Class constructors cannot be invoked without 'new'");
24792484
return nullptr;

napi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ namespace Napi {
11261126
~CallbackInfo();
11271127

11281128
Napi::Env Env() const;
1129+
Value NewTarget() const;
11291130
bool IsConstructCall() const;
11301131
size_t Length() const;
11311132
const Value operator [](size_t index) const;

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)