1 #include <js_native_api.h> 2 3 // Empty value so that macros here are able to return NULL or void 4 #define NODE_API_RETVAL_NOTHING // Intentionally blank #define 5 6 #define GET_AND_THROW_LAST_ERROR(env) \ 7 do { \ 8 const napi_extended_error_info *error_info; \ 9 napi_get_last_error_info((env), &error_info); \ 10 bool is_pending; \ 11 const char* err_message = error_info->error_message; \ 12 napi_is_exception_pending((env), &is_pending); \ 13 /* If an exception is already pending, don't rethrow it */ \ 14 if (!is_pending) { \ 15 const char* error_message = err_message != NULL ? \ 16 err_message : \ 17 "empty error message"; \ 18 napi_throw_error((env), NULL, error_message); \ 19 } \ 20 } while (0) 21 22 #define NODE_API_ASSERT_BASE(env, assertion, message, ret_val) \ 23 do { \ 24 if (!(assertion)) { \ 25 napi_throw_error( \ 26 (env), \ 27 NULL, \ 28 "assertion (" #assertion ") failed: " message); \ 29 return ret_val; \ 30 } \ 31 } while (0) 32 33 // Returns NULL on failed assertion. 34 // This is meant to be used inside napi_callback methods. 35 #define NODE_API_ASSERT(env, assertion, message) \ 36 NODE_API_ASSERT_BASE(env, assertion, message, NULL) 37 38 // Returns empty on failed assertion. 39 // This is meant to be used inside functions with void return type. 40 #define NODE_API_ASSERT_RETURN_VOID(env, assertion, message) \ 41 NODE_API_ASSERT_BASE(env, assertion, message, NODE_API_RETVAL_NOTHING) 42 43 #define NODE_API_CALL_BASE(env, the_call, ret_val) \ 44 do { \ 45 if ((the_call) != napi_ok) { \ 46 GET_AND_THROW_LAST_ERROR((env)); \ 47 return ret_val; \ 48 } \ 49 } while (0) 50 51 // Returns NULL if the_call doesn't return napi_ok. 52 #define NODE_API_CALL(env, the_call) \ 53 NODE_API_CALL_BASE(env, the_call, NULL) 54 55 // Returns empty if the_call doesn't return napi_ok. 56 #define NODE_API_CALL_RETURN_VOID(env, the_call) \ 57 NODE_API_CALL_BASE(env, the_call, NODE_API_RETVAL_NOTHING) 58 59 #define NODE_API_CHECK_STATUS(the_call) \ 60 do { \ 61 napi_status status = (the_call); \ 62 if (status != napi_ok) { \ 63 return status; \ 64 } \ 65 } while (0) 66 67 #define NODE_API_ASSERT_STATUS(env, assertion, message) \ 68 NODE_API_ASSERT_BASE(env, assertion, message, napi_generic_failure) 69 70 #define DECLARE_NODE_API_PROPERTY(name, func) \ 71 { (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL } 72 73 #define DECLARE_NODE_API_GETTER(name, func) \ 74 { (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL } 75 76 #define DECLARE_NODE_API_PROPERTY_VALUE(name, value) \ 77 { (name), NULL, NULL, NULL, NULL, (value), napi_default, NULL } 78 79 void add_returned_status(napi_env env, 80 const char* key, 81 napi_value object, 82 char* expected_message, 83 napi_status expected_status, 84 napi_status actual_status); 85 86 void add_last_status(napi_env env, const char* key, napi_value return_value); 87