n-api: wrap test macros in do/while
PR-URL: https://github.com/nodejs/node/pull/14095 Reviewed-By: Jason Ginchereau <jasongin@microsoft.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
d5b397c9b6
commit
f52c707853
@ -14,15 +14,17 @@
|
|||||||
"empty error message"; \
|
"empty error message"; \
|
||||||
napi_throw_error((env), error_message); \
|
napi_throw_error((env), error_message); \
|
||||||
} \
|
} \
|
||||||
} while(0);
|
} while (0)
|
||||||
|
|
||||||
#define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \
|
#define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \
|
||||||
if (!(assertion)) { \
|
do { \
|
||||||
napi_throw_error( \
|
if (!(assertion)) { \
|
||||||
(env), \
|
napi_throw_error( \
|
||||||
"assertion (" #assertion ") failed: " message); \
|
(env), \
|
||||||
return ret_val; \
|
"assertion (" #assertion ") failed: " message); \
|
||||||
}
|
return ret_val; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
// Returns NULL on failed assertion.
|
// Returns NULL on failed assertion.
|
||||||
// This is meant to be used inside napi_callback methods.
|
// This is meant to be used inside napi_callback methods.
|
||||||
@ -35,10 +37,12 @@
|
|||||||
NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)
|
NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)
|
||||||
|
|
||||||
#define NAPI_CALL_BASE(env, the_call, ret_val) \
|
#define NAPI_CALL_BASE(env, the_call, ret_val) \
|
||||||
if ((the_call) != napi_ok) { \
|
do { \
|
||||||
GET_AND_THROW_LAST_ERROR((env)); \
|
if ((the_call) != napi_ok) { \
|
||||||
return ret_val; \
|
GET_AND_THROW_LAST_ERROR((env)); \
|
||||||
}
|
return ret_val; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
// Returns NULL if the_call doesn't return napi_ok.
|
// Returns NULL if the_call doesn't return napi_ok.
|
||||||
#define NAPI_CALL(env, the_call) \
|
#define NAPI_CALL(env, the_call) \
|
||||||
|
@ -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_CALL(env, napi_set_property(env, args[0], args[1], args[2]));
|
||||||
|
|
||||||
napi_value valuetrue;
|
napi_value valuetrue;
|
||||||
NAPI_CALL(env, napi_get_boolean(env, true, &valuetrue))
|
NAPI_CALL(env, napi_get_boolean(env, true, &valuetrue));
|
||||||
|
|
||||||
return valuetrue;
|
return valuetrue;
|
||||||
}
|
}
|
||||||
|
@ -58,13 +58,13 @@ napi_value CheckExternal(napi_env env, napi_callback_info info) {
|
|||||||
napi_valuetype argtype;
|
napi_valuetype argtype;
|
||||||
NAPI_CALL(env, napi_typeof(env, arg, &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;
|
void* data;
|
||||||
NAPI_CALL(env, napi_get_value_external(env, arg, &data));
|
NAPI_CALL(env, napi_get_value_external(env, arg, &data));
|
||||||
|
|
||||||
NAPI_ASSERT(env, data != NULL && *(int*)data == test_value,
|
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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user