diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index e7a16401369f42..0a3494ce700c93 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -816,12 +816,7 @@ napi_status napi_define_class(napi_env env, } v8::Local 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); @@ -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); @@ -1268,12 +1261,7 @@ napi_status napi_define_properties(napi_env env, const napi_property_descriptor* p = &properties[i]; v8::Local 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 local_getter; @@ -1724,8 +1712,7 @@ napi_status napi_create_error(napi_env env, v8::Local error_obj = v8::Exception::Error(message_value.As()); - 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); @@ -1745,8 +1732,7 @@ napi_status napi_create_type_error(napi_env env, v8::Local error_obj = v8::Exception::TypeError(message_value.As()); - 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); @@ -1766,8 +1752,7 @@ napi_status napi_create_range_error(napi_env env, v8::Local error_obj = v8::Exception::RangeError(message_value.As()); - 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); @@ -1947,8 +1932,7 @@ napi_status napi_throw_error(napi_env env, CHECK_NEW_FROM_UTF8(env, str, msg); v8::Local 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 @@ -1966,8 +1950,7 @@ napi_status napi_throw_type_error(napi_env env, CHECK_NEW_FROM_UTF8(env, str, msg); v8::Local 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 @@ -1985,8 +1968,7 @@ napi_status napi_throw_range_error(napi_env env, CHECK_NEW_FROM_UTF8(env, str, msg); v8::Local 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 @@ -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, diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h index 06b8049ec46db0..1a62782c1ad24f 100644 --- a/src/js_native_api_v8.h +++ b/src/js_native_api_v8.h @@ -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_ diff --git a/src/node_api.cc b/src/node_api.cc index 4e932c19c2bf8a..f1a5265b6a7234 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -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(work);