summaryrefslogtreecommitdiff
path: root/test/js-native-api/common.h
diff options
context:
space:
mode:
authorGabriel Schulhof <gabriel.schulhof@intel.com>2018-11-17 12:34:54 -0800
committerGabriel Schulhof <gabriel.schulhof@intel.com>2018-12-04 13:58:17 -0800
commit938e11882b96e19b443477571455088baaa054d8 (patch)
treeeb828a60957a2881995ba9a83f44a32a18fbff16 /test/js-native-api/common.h
parent83ee137c4565112177f22f2c735b266b22262220 (diff)
downloadandroid-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.gz
android-node-v8-938e11882b96e19b443477571455088baaa054d8.tar.bz2
android-node-v8-938e11882b96e19b443477571455088baaa054d8.zip
test: partition N-API tests
Partition test/addons-napi into test/js-native-api and test/node-api to isolate the Node.js-agnostic portion of the N-API tests from the Node.js-specific portion. PR-URL: https://github.com/nodejs/node/pull/24557 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'test/js-native-api/common.h')
-rw-r--r--test/js-native-api/common.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/js-native-api/common.h b/test/js-native-api/common.h
new file mode 100644
index 0000000000..b16f944771
--- /dev/null
+++ b/test/js-native-api/common.h
@@ -0,0 +1,60 @@
+// Empty value so that macros here are able to return NULL or void
+#define NAPI_RETVAL_NOTHING // Intentionally blank #define
+
+#define GET_AND_THROW_LAST_ERROR(env) \
+ do { \
+ const napi_extended_error_info *error_info; \
+ napi_get_last_error_info((env), &error_info); \
+ bool is_pending; \
+ napi_is_exception_pending((env), &is_pending); \
+ /* If an exception is already pending, don't rethrow it */ \
+ if (!is_pending) { \
+ const char* error_message = error_info->error_message != NULL ? \
+ error_info->error_message : \
+ "empty error message"; \
+ napi_throw_error((env), NULL, error_message); \
+ } \
+ } while (0)
+
+#define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \
+ do { \
+ if (!(assertion)) { \
+ napi_throw_error( \
+ (env), \
+ NULL, \
+ "assertion (" #assertion ") failed: " message); \
+ return ret_val; \
+ } \
+ } while (0)
+
+// Returns NULL on failed assertion.
+// This is meant to be used inside napi_callback methods.
+#define NAPI_ASSERT(env, assertion, message) \
+ NAPI_ASSERT_BASE(env, assertion, message, NULL)
+
+// Returns empty on failed assertion.
+// This is meant to be used inside functions with void return type.
+#define NAPI_ASSERT_RETURN_VOID(env, assertion, message) \
+ NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)
+
+#define NAPI_CALL_BASE(env, the_call, 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) \
+ NAPI_CALL_BASE(env, the_call, NULL)
+
+// Returns empty if the_call doesn't return napi_ok.
+#define NAPI_CALL_RETURN_VOID(env, the_call) \
+ NAPI_CALL_BASE(env, the_call, NAPI_RETVAL_NOTHING)
+
+#define DECLARE_NAPI_PROPERTY(name, func) \
+ { (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL }
+
+#define DECLARE_NAPI_GETTER(name, func) \
+ { (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL }