Skip to content
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

n-api: factor out calling pattern #36113

Closed
wants to merge 3 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
48 changes: 14 additions & 34 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,7 @@ napi_status napi_define_class(napi_env env,
}

v8::Local<v8::Name> property_name;
napi_status status =
v8impl::V8NameFromPropertyDescriptor(env, p, &property_name);

if (status != napi_ok) {
return napi_set_last_error(env, status);
Copy link
Member

@legendecas legendecas Nov 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STATUS_CALL simply returns on non-napi_ok without calling napi_set_last_error. The behavior seems not equivalent with the change. Is this intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@legendecas yes, it is, because it is assumed that whichever call returned non-napi_ok did so via return napi_set_last_error(napi_<not_ok>); I checked to make sure that was the case in all locations where I made the change.

}
STATUS_CALL(v8impl::V8NameFromPropertyDescriptor(env, p, &property_name));

v8::PropertyAttribute attributes =
v8impl::V8PropertyAttributesFromDescriptor(p);
Expand Down Expand Up @@ -888,12 +883,10 @@ napi_status napi_define_class(napi_env env,
}
}

napi_status status =
napi_define_properties(env,
*result,
static_descriptors.size(),
static_descriptors.data());
if (status != napi_ok) return status;
STATUS_CALL(napi_define_properties(env,
*result,
static_descriptors.size(),
static_descriptors.data()));
}

return GET_RETURN_STATUS(env);
Expand Down Expand Up @@ -1268,12 +1261,7 @@ napi_status napi_define_properties(napi_env env,
const napi_property_descriptor* p = &properties[i];

v8::Local<v8::Name> property_name;
napi_status status =
v8impl::V8NameFromPropertyDescriptor(env, p, &property_name);

if (status != napi_ok) {
return napi_set_last_error(env, status);
}
STATUS_CALL(v8impl::V8NameFromPropertyDescriptor(env, p, &property_name));

if (p->getter != nullptr || p->setter != nullptr) {
v8::Local<v8::Value> local_getter;
Expand Down Expand Up @@ -1724,8 +1712,7 @@ napi_status napi_create_error(napi_env env,

v8::Local<v8::Value> error_obj =
v8::Exception::Error(message_value.As<v8::String>());
napi_status status = set_error_code(env, error_obj, code, nullptr);
if (status != napi_ok) return status;
STATUS_CALL(set_error_code(env, error_obj, code, nullptr));

*result = v8impl::JsValueFromV8LocalValue(error_obj);

Expand All @@ -1745,8 +1732,7 @@ napi_status napi_create_type_error(napi_env env,

v8::Local<v8::Value> error_obj =
v8::Exception::TypeError(message_value.As<v8::String>());
napi_status status = set_error_code(env, error_obj, code, nullptr);
if (status != napi_ok) return status;
STATUS_CALL(set_error_code(env, error_obj, code, nullptr));

*result = v8impl::JsValueFromV8LocalValue(error_obj);

Expand All @@ -1766,8 +1752,7 @@ napi_status napi_create_range_error(napi_env env,

v8::Local<v8::Value> error_obj =
v8::Exception::RangeError(message_value.As<v8::String>());
napi_status status = set_error_code(env, error_obj, code, nullptr);
if (status != napi_ok) return status;
STATUS_CALL(set_error_code(env, error_obj, code, nullptr));

*result = v8impl::JsValueFromV8LocalValue(error_obj);

Expand Down Expand Up @@ -1947,8 +1932,7 @@ napi_status napi_throw_error(napi_env env,
CHECK_NEW_FROM_UTF8(env, str, msg);

v8::Local<v8::Value> error_obj = v8::Exception::Error(str);
napi_status status = set_error_code(env, error_obj, nullptr, code);
if (status != napi_ok) return status;
STATUS_CALL(set_error_code(env, error_obj, nullptr, code));

isolate->ThrowException(error_obj);
// any VM calls after this point and before returning
Expand All @@ -1966,8 +1950,7 @@ napi_status napi_throw_type_error(napi_env env,
CHECK_NEW_FROM_UTF8(env, str, msg);

v8::Local<v8::Value> error_obj = v8::Exception::TypeError(str);
napi_status status = set_error_code(env, error_obj, nullptr, code);
if (status != napi_ok) return status;
STATUS_CALL(set_error_code(env, error_obj, nullptr, code));

isolate->ThrowException(error_obj);
// any VM calls after this point and before returning
Expand All @@ -1985,8 +1968,7 @@ napi_status napi_throw_range_error(napi_env env,
CHECK_NEW_FROM_UTF8(env, str, msg);

v8::Local<v8::Value> error_obj = v8::Exception::RangeError(str);
napi_status status = set_error_code(env, error_obj, nullptr, code);
if (status != napi_ok) return status;
STATUS_CALL(set_error_code(env, error_obj, nullptr, code));

isolate->ThrowException(error_obj);
// any VM calls after this point and before returning
Expand Down Expand Up @@ -2785,15 +2767,13 @@ napi_status napi_create_external_arraybuffer(napi_env env,
// and is able to use napi_env. Implementing that properly is hard, so use the
// `Buffer` variant for easier implementation.
napi_value buffer;
napi_status status;
status = napi_create_external_buffer(
STATUS_CALL(napi_create_external_buffer(
env,
byte_length,
external_data,
finalize_cb,
finalize_hint,
&buffer);
if (status != napi_ok) return status;
&buffer));
return napi_get_typedarray_info(
env,
buffer,
Expand Down
6 changes: 6 additions & 0 deletions src/js_native_api_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,10 @@ class TryCatch : public v8::TryCatch {

} // end of namespace v8impl

#define STATUS_CALL(call) \
do { \
napi_status status = (call); \
if (status != napi_ok) return status; \
} while (0)

#endif // SRC_JS_NATIVE_API_V8_H_
5 changes: 1 addition & 4 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1130,11 +1130,8 @@ napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
CHECK_ENV(env);
CHECK_ARG(env, work);

napi_status status;
uv_loop_t* event_loop = nullptr;
status = napi_get_uv_event_loop(env, &event_loop);
if (status != napi_ok)
return napi_set_last_error(env, status);
STATUS_CALL(napi_get_uv_event_loop(env, &event_loop));

uvimpl::Work* w = reinterpret_cast<uvimpl::Work*>(work);

Expand Down