From 58e92589971de267c20090ac52b0287b202b30aa Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 13 Nov 2020 13:07:36 -0800 Subject: [PATCH 1/3] n-api: factor out calling pattern Factor out how we handle a `napi_status`-valued return internally. Signed-off-by: Gabriel Schulhof --- src/js_native_api_v8.cc | 48 ++++++++++++----------------------------- src/js_native_api_v8.h | 6 ++++++ src/node_api.cc | 4 +--- 3 files changed, 21 insertions(+), 37 deletions(-) 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..6f50e655a2e7d0 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..d2e4ec0a4a9754 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1132,9 +1132,7 @@ napi_status napi_queue_async_work(napi_env env, napi_async_work 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); From 913d5404713a079b313d69e59166bac9fd434701 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 13 Nov 2020 13:42:15 -0800 Subject: [PATCH 2/3] fixup! remove unused variable --- src/node_api.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node_api.cc b/src/node_api.cc index d2e4ec0a4a9754..f1a5265b6a7234 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1130,7 +1130,6 @@ 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_CALL(napi_get_uv_event_loop(env, &event_loop)); From 8e4b5756378e249faa399f507ac1730692a0eed3 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Sun, 15 Nov 2020 09:12:51 -0800 Subject: [PATCH 3/3] fixup! Update src/js_native_api_v8.h Remove semicolon from macro definition Co-authored-by: Anna Henningsen --- src/js_native_api_v8.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h index 6f50e655a2e7d0..1a62782c1ad24f 100644 --- a/src/js_native_api_v8.h +++ b/src/js_native_api_v8.h @@ -341,6 +341,6 @@ class TryCatch : public v8::TryCatch { do { \ napi_status status = (call); \ if (status != napi_ok) return status; \ - } while (0); + } while (0) #endif // SRC_JS_NATIVE_API_V8_H_