Browse Source

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>
v6
Kyle Farnung 7 years ago
committed by Michael Dawson
parent
commit
f52c707853
  1. 26
      test/addons-napi/common.h
  2. 2
      test/addons-napi/test_object/test_object.c
  3. 4
      test/addons-napi/test_reference/test_reference.c

26
test/addons-napi/common.h

@ -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.
@ -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) \

2
test/addons-napi/test_object/test_object.c

@ -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;
}

4
test/addons-napi/test_reference/test_reference.c

@ -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;
}

Loading…
Cancel
Save