Skip to content

Replace napi_is_construct_call with napi_get_new_target #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2015,11 +2015,15 @@ inline CallbackInfo::~CallbackInfo() {
}
}

inline Value CallbackInfo::NewTarget() const {
napi_value newTarget;
napi_status status = napi_get_new_target(_env, _info, &newTarget);
NAPI_THROW_IF_FAILED(_env, status, Value());
return Value(_env, newTarget);
}

inline bool CallbackInfo::IsConstructCall() const {
bool isConstructCall;
napi_status status = napi_is_construct_call(_env, _info, &isConstructCall);
NAPI_THROW_IF_FAILED(_env, status, false);
return isConstructCall;
return !NewTarget().IsEmpty();
}

inline Napi::Env CallbackInfo::Env() const {
Expand Down Expand Up @@ -2470,10 +2474,11 @@ template <typename T>
inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
napi_env env,
napi_callback_info info) {
bool isConstructCall;
napi_status status = napi_is_construct_call(env, info, &isConstructCall);
napi_value new_target;
napi_status status = napi_get_new_target(env, info, &new_target);
if (status != napi_ok) return nullptr;

bool isConstructCall = (new_target != nullptr);
if (!isConstructCall) {
napi_throw_type_error(env, nullptr, "Class constructors cannot be invoked without 'new'");
return nullptr;
Expand Down
1 change: 1 addition & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ namespace Napi {
~CallbackInfo();

Napi::Env Env() const;
Value NewTarget() const;
bool IsConstructCall() const;
size_t Length() const;
const Value operator [](size_t index) const;
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ 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;

Expand Down Expand Up @@ -467,8 +467,7 @@ class CallbackWrapperBase : public CallbackWrapper {
->Value();
}

/*virtual*/
bool IsConstructCall() override { return false; }
napi_value NewTarget() override { return nullptr; }

protected:
void InvokeCallback() {
Expand Down Expand Up @@ -516,8 +515,13 @@ class FunctionCallbackWrapper
const v8::FunctionCallbackInfo<v8::Value>& cbinfo)
: CallbackWrapperBase(cbinfo, cbinfo.Length()) {}

/*virtual*/
bool IsConstructCall() override { return _cbinfo.IsConstructCall(); }
napi_value NewTarget() override {
if (_cbinfo.IsConstructCall()) {
return v8impl::JsValueFromV8LocalValue(_cbinfo.NewTarget());
} else {
return nullptr;
}
}

/*virtual*/
void Args(napi_value* buffer, size_t buffer_length) override {
Expand Down Expand Up @@ -1810,18 +1814,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<v8impl::CallbackWrapper*>(cbinfo);

*result = info->IsConstructCall();
*result = info->NewTarget();
return napi_clear_last_error(env);
}

Expand Down
6 changes: 3 additions & 3 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down