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: wrap test macros in do/while #14095

Closed
wants to merge 1 commit 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
26 changes: 15 additions & 11 deletions test/addons-napi/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
"empty error message"; \
napi_throw_error((env), error_message); \
} \
} while(0);
} while (0)

#define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \
if (!(assertion)) { \
napi_throw_error( \
(env), \
"assertion (" #assertion ") failed: " message); \
return ret_val; \
}
do { \
if (!(assertion)) { \
napi_throw_error( \
(env), \
"assertion (" #assertion ") failed: " message); \
return ret_val; \
} \
} while (0)

// Returns NULL on failed assertion.
// This is meant to be used inside napi_callback methods.
Expand All @@ -35,10 +37,12 @@
NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)

#define NAPI_CALL_BASE(env, the_call, ret_val) \
if ((the_call) != napi_ok) { \
GET_AND_THROW_LAST_ERROR((env)); \
return ret_val; \
}
do { \
if ((the_call) != napi_ok) { \
GET_AND_THROW_LAST_ERROR((env)); \
return ret_val; \
} \
} while (0)

// Returns NULL if the_call doesn't return napi_ok.
#define NAPI_CALL(env, the_call) \
Expand Down
2 changes: 1 addition & 1 deletion test/addons-napi/test_object/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ napi_value Set(napi_env env, napi_callback_info info) {
NAPI_CALL(env, napi_set_property(env, args[0], args[1], args[2]));

napi_value valuetrue;
NAPI_CALL(env, napi_get_boolean(env, true, &valuetrue))
NAPI_CALL(env, napi_get_boolean(env, true, &valuetrue));

return valuetrue;
}
Expand Down
4 changes: 2 additions & 2 deletions test/addons-napi/test_reference/test_reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ napi_value CheckExternal(napi_env env, napi_callback_info info) {
napi_valuetype argtype;
NAPI_CALL(env, napi_typeof(env, arg, &argtype));

NAPI_ASSERT(env, argtype == napi_external, "Expected an external value.")
NAPI_ASSERT(env, argtype == napi_external, "Expected an external value.");

void* data;
NAPI_CALL(env, napi_get_value_external(env, arg, &data));

NAPI_ASSERT(env, data != NULL && *(int*)data == test_value,
"An external data value of 1 was expected.")
"An external data value of 1 was expected.");

return NULL;
}
Expand Down