1 #include <js_native_api.h> 2 3 // Empty value so that macros here are able to return NULL or void 4 #define NAPI_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 napi_is_exception_pending((env), &is_pending); \ 12 /* If an exception is already pending, don't rethrow it */ \ 13 if (!is_pending) { \ 14 const char* error_message = error_info->error_message != NULL ? \ 15 error_info->error_message : \ 16 "empty error message"; \ 17 napi_throw_error((env), NULL, error_message); \ 18 } \ 19 } while (0) 20 21 #define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \ 22 do { \ 23 if (!(assertion)) { \ 24 napi_throw_error( \ 25 (env), \ 26 NULL, \ 27 "assertion (" #assertion ") failed: " message); \ 28 return ret_val; \ 29 } \ 30 } while (0) 31 32 // Returns NULL on failed assertion. 33 // This is meant to be used inside napi_callback methods. 34 #define NAPI_ASSERT(env, assertion, message) \ 35 NAPI_ASSERT_BASE(env, assertion, message, NULL) 36 37 // Returns empty on failed assertion. 38 // This is meant to be used inside functions with void return type. 39 #define NAPI_ASSERT_RETURN_VOID(env, assertion, message) \ 40 NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING) 41 42 #define NAPI_CALL_BASE(env, the_call, ret_val) \ 43 do { \ 44 if ((the_call) != napi_ok) { \ 45 GET_AND_THROW_LAST_ERROR((env)); \ 46 return ret_val; \ 47 } \ 48 } while (0) 49 50 // Returns NULL if the_call doesn't return napi_ok. 51 #define NAPI_CALL(env, the_call) \ 52 NAPI_CALL_BASE(env, the_call, NULL) 53 54 // Returns empty if the_call doesn't return napi_ok. 55 #define NAPI_CALL_RETURN_VOID(env, the_call) \ 56 NAPI_CALL_BASE(env, the_call, NAPI_RETVAL_NOTHING) 57 58 #define DECLARE_NAPI_PROPERTY(name, func) \ 59 { (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL } 60 61 #define DECLARE_NAPI_GETTER(name, func) \ 62 { (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL } 63 64 void add_returned_status(napi_env env, 65 const char* key, 66 napi_value object, 67 char* expected_message, 68 napi_status expected_status, 69 napi_status actual_status); 70 71 void add_last_status(napi_env env, const char* key, napi_value return_value); 72