Skip to content

Commit 08657e7

Browse files
Gabriel SchulhofBethGriggs
Gabriel Schulhof
authored andcommittedDec 15, 2020
n-api: factor out calling pattern
Factor out how we handle a `napi_status`-valued return internally. Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com> PR-URL: #36113 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent 800e1db commit 08657e7

File tree

3 files changed

+21
-38
lines changed

3 files changed

+21
-38
lines changed
 

‎src/js_native_api_v8.cc

+14-34
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,7 @@ napi_status napi_define_class(napi_env env,
816816
}
817817

818818
v8::Local<v8::Name> property_name;
819-
napi_status status =
820-
v8impl::V8NameFromPropertyDescriptor(env, p, &property_name);
821-
822-
if (status != napi_ok) {
823-
return napi_set_last_error(env, status);
824-
}
819+
STATUS_CALL(v8impl::V8NameFromPropertyDescriptor(env, p, &property_name));
825820

826821
v8::PropertyAttribute attributes =
827822
v8impl::V8PropertyAttributesFromDescriptor(p);
@@ -888,12 +883,10 @@ napi_status napi_define_class(napi_env env,
888883
}
889884
}
890885

891-
napi_status status =
892-
napi_define_properties(env,
893-
*result,
894-
static_descriptors.size(),
895-
static_descriptors.data());
896-
if (status != napi_ok) return status;
886+
STATUS_CALL(napi_define_properties(env,
887+
*result,
888+
static_descriptors.size(),
889+
static_descriptors.data()));
897890
}
898891

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

12701263
v8::Local<v8::Name> property_name;
1271-
napi_status status =
1272-
v8impl::V8NameFromPropertyDescriptor(env, p, &property_name);
1273-
1274-
if (status != napi_ok) {
1275-
return napi_set_last_error(env, status);
1276-
}
1264+
STATUS_CALL(v8impl::V8NameFromPropertyDescriptor(env, p, &property_name));
12771265

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

17251713
v8::Local<v8::Value> error_obj =
17261714
v8::Exception::Error(message_value.As<v8::String>());
1727-
napi_status status = set_error_code(env, error_obj, code, nullptr);
1728-
if (status != napi_ok) return status;
1715+
STATUS_CALL(set_error_code(env, error_obj, code, nullptr));
17291716

17301717
*result = v8impl::JsValueFromV8LocalValue(error_obj);
17311718

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

17461733
v8::Local<v8::Value> error_obj =
17471734
v8::Exception::TypeError(message_value.As<v8::String>());
1748-
napi_status status = set_error_code(env, error_obj, code, nullptr);
1749-
if (status != napi_ok) return status;
1735+
STATUS_CALL(set_error_code(env, error_obj, code, nullptr));
17501736

17511737
*result = v8impl::JsValueFromV8LocalValue(error_obj);
17521738

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

17671753
v8::Local<v8::Value> error_obj =
17681754
v8::Exception::RangeError(message_value.As<v8::String>());
1769-
napi_status status = set_error_code(env, error_obj, code, nullptr);
1770-
if (status != napi_ok) return status;
1755+
STATUS_CALL(set_error_code(env, error_obj, code, nullptr));
17711756

17721757
*result = v8impl::JsValueFromV8LocalValue(error_obj);
17731758

@@ -1947,8 +1932,7 @@ napi_status napi_throw_error(napi_env env,
19471932
CHECK_NEW_FROM_UTF8(env, str, msg);
19481933

19491934
v8::Local<v8::Value> error_obj = v8::Exception::Error(str);
1950-
napi_status status = set_error_code(env, error_obj, nullptr, code);
1951-
if (status != napi_ok) return status;
1935+
STATUS_CALL(set_error_code(env, error_obj, nullptr, code));
19521936

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

19681952
v8::Local<v8::Value> error_obj = v8::Exception::TypeError(str);
1969-
napi_status status = set_error_code(env, error_obj, nullptr, code);
1970-
if (status != napi_ok) return status;
1953+
STATUS_CALL(set_error_code(env, error_obj, nullptr, code));
19711954

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

19871970
v8::Local<v8::Value> error_obj = v8::Exception::RangeError(str);
1988-
napi_status status = set_error_code(env, error_obj, nullptr, code);
1989-
if (status != napi_ok) return status;
1971+
STATUS_CALL(set_error_code(env, error_obj, nullptr, code));
19901972

19911973
isolate->ThrowException(error_obj);
19921974
// any VM calls after this point and before returning
@@ -2785,15 +2767,13 @@ napi_status napi_create_external_arraybuffer(napi_env env,
27852767
// and is able to use napi_env. Implementing that properly is hard, so use the
27862768
// `Buffer` variant for easier implementation.
27872769
napi_value buffer;
2788-
napi_status status;
2789-
status = napi_create_external_buffer(
2770+
STATUS_CALL(napi_create_external_buffer(
27902771
env,
27912772
byte_length,
27922773
external_data,
27932774
finalize_cb,
27942775
finalize_hint,
2795-
&buffer);
2796-
if (status != napi_ok) return status;
2776+
&buffer));
27972777
return napi_get_typedarray_info(
27982778
env,
27992779
buffer,

‎src/js_native_api_v8.h

+6
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,10 @@ class TryCatch : public v8::TryCatch {
337337

338338
} // end of namespace v8impl
339339

340+
#define STATUS_CALL(call) \
341+
do { \
342+
napi_status status = (call); \
343+
if (status != napi_ok) return status; \
344+
} while (0)
345+
340346
#endif // SRC_JS_NATIVE_API_V8_H_

‎src/node_api.cc

+1-4
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,8 @@ napi_status napi_queue_async_work(napi_env env, napi_async_work work) {
11301130
CHECK_ENV(env);
11311131
CHECK_ARG(env, work);
11321132

1133-
napi_status status;
11341133
uv_loop_t* event_loop = nullptr;
1135-
status = napi_get_uv_event_loop(env, &event_loop);
1136-
if (status != napi_ok)
1137-
return napi_set_last_error(env, status);
1134+
STATUS_CALL(napi_get_uv_event_loop(env, &event_loop));
11381135

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

0 commit comments

Comments
 (0)
Please sign in to comment.