Skip to content

Commit

Permalink
n-api: factor out calling pattern
Browse files Browse the repository at this point in the history
Factor out how we handle a `napi_status`-valued return internally.

Signed-off-by: Gabriel Schulhof <[email protected]>
PR-URL: #36113
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
Gabriel Schulhof authored and codebytere committed Nov 22, 2020
1 parent 0ff384b commit 89da0c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 38 deletions.
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);
}
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

0 comments on commit 89da0c3

Please sign in to comment.