|
2 | 2 | #include <stdio.h> |
3 | 3 | #include <stdlib.h> |
4 | 4 | #include <string.h> |
5 | | - |
6 | | -#define NODE_API_RETVAL_NOTHING // Intentionally blank #define |
7 | | - |
8 | | -#define GET_AND_THROW_LAST_ERROR(env) \ |
9 | | - do { \ |
10 | | - const napi_extended_error_info* error_info; \ |
11 | | - napi_get_last_error_info((env), &error_info); \ |
12 | | - bool is_pending; \ |
13 | | - const char* err_message = error_info->error_message; \ |
14 | | - napi_is_exception_pending((env), &is_pending); \ |
15 | | - /* If an exception is already pending, don't rethrow it */ \ |
16 | | - if (!is_pending) { \ |
17 | | - const char* error_message = \ |
18 | | - err_message != NULL ? err_message : "empty error message"; \ |
19 | | - napi_throw_error((env), NULL, error_message); \ |
20 | | - } \ |
21 | | - } while (0) |
22 | | - |
23 | | -// The basic version of GET_AND_THROW_LAST_ERROR. We cannot access any |
24 | | -// exceptions and we cannot fail by way of JS exception, so we abort. |
25 | | -#define FATALLY_FAIL_WITH_LAST_ERROR(env) \ |
26 | | - do { \ |
27 | | - const napi_extended_error_info* error_info; \ |
28 | | - napi_get_last_error_info((env), &error_info); \ |
29 | | - const char* err_message = error_info->error_message; \ |
30 | | - const char* error_message = \ |
31 | | - err_message != NULL ? err_message : "empty error message"; \ |
32 | | - fprintf(stderr, "%s\n", error_message); \ |
33 | | - abort(); \ |
34 | | - } while (0) |
35 | | - |
36 | | -#define NODE_API_ASSERT_BASE(env, assertion, message, ret_val) \ |
37 | | - do { \ |
38 | | - if (!(assertion)) { \ |
39 | | - napi_throw_error( \ |
40 | | - (env), NULL, "assertion (" #assertion ") failed: " message); \ |
41 | | - return ret_val; \ |
42 | | - } \ |
43 | | - } while (0) |
44 | | - |
45 | | -#define NODE_API_BASIC_ASSERT_BASE(assertion, message, ret_val) \ |
46 | | - do { \ |
47 | | - if (!(assertion)) { \ |
48 | | - fprintf(stderr, "assertion (" #assertion ") failed: " message); \ |
49 | | - abort(); \ |
50 | | - return ret_val; \ |
51 | | - } \ |
52 | | - } while (0) |
53 | | - |
54 | | -// Returns NULL on failed assertion. |
55 | | -// This is meant to be used inside napi_callback methods. |
56 | | -#define NODE_API_ASSERT(env, assertion, message) \ |
57 | | - NODE_API_ASSERT_BASE(env, assertion, message, NULL) |
58 | | - |
59 | | -// Returns empty on failed assertion. |
60 | | -// This is meant to be used inside functions with void return type. |
61 | | -#define NODE_API_ASSERT_RETURN_VOID(env, assertion, message) \ |
62 | | - NODE_API_ASSERT_BASE(env, assertion, message, NODE_API_RETVAL_NOTHING) |
63 | | - |
64 | | -#define NODE_API_BASIC_ASSERT_RETURN_VOID(assertion, message) \ |
65 | | - NODE_API_BASIC_ASSERT_BASE(assertion, message, NODE_API_RETVAL_NOTHING) |
66 | | - |
67 | | -#define NODE_API_CALL_BASE(env, the_call, ret_val) \ |
68 | | - do { \ |
69 | | - if ((the_call) != napi_ok) { \ |
70 | | - GET_AND_THROW_LAST_ERROR((env)); \ |
71 | | - return ret_val; \ |
72 | | - } \ |
73 | | - } while (0) |
74 | | - |
75 | | -#define NODE_API_BASIC_CALL_BASE(env, the_call, ret_val) \ |
76 | | - do { \ |
77 | | - if ((the_call) != napi_ok) { \ |
78 | | - FATALLY_FAIL_WITH_LAST_ERROR((env)); \ |
79 | | - return ret_val; \ |
80 | | - } \ |
81 | | - } while (0) |
82 | | - |
83 | | -// Returns NULL if the_call doesn't return napi_ok. |
84 | | -#define NODE_API_CALL(env, the_call) NODE_API_CALL_BASE(env, the_call, NULL) |
85 | | - |
86 | | -// Returns empty if the_call doesn't return napi_ok. |
87 | | -#define NODE_API_CALL_RETURN_VOID(env, the_call) \ |
88 | | - NODE_API_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING) |
89 | | - |
90 | | -#define NODE_API_BASIC_CALL_RETURN_VOID(env, the_call) \ |
91 | | - NODE_API_BASIC_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING) |
92 | | - |
93 | | -#define NODE_API_CHECK_STATUS(the_call) \ |
94 | | - do { \ |
95 | | - napi_status status = (the_call); \ |
96 | | - if (status != napi_ok) { \ |
97 | | - return status; \ |
98 | | - } \ |
99 | | - } while (0) |
100 | | - |
101 | | -#define NODE_API_ASSERT_STATUS(env, assertion, message) \ |
102 | | - NODE_API_ASSERT_BASE(env, assertion, message, napi_generic_failure) |
103 | | - |
104 | | -#define DECLARE_NODE_API_PROPERTY(name, func) \ |
105 | | - {(name), NULL, (func), NULL, NULL, NULL, napi_default, NULL} |
106 | | - |
107 | | -#define DECLARE_NODE_API_GETTER(name, func) \ |
108 | | - {(name), NULL, NULL, (func), NULL, NULL, napi_default, NULL} |
109 | | - |
110 | | -#define DECLARE_NODE_API_PROPERTY_VALUE(name, value) \ |
111 | | - {(name), NULL, NULL, NULL, NULL, (value), napi_default, NULL} |
| 5 | +#include "../RuntimeNodeApiTestsCommon.h" |
112 | 6 |
|
113 | 7 | static inline void add_returned_status(napi_env env, |
114 | 8 | const char* key, |
|
0 commit comments