-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
277683b
commit 4cb3040
Showing
2 changed files
with
168 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <node_api.h> | ||
|
||
// 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) | ||
|
||
#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 \ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters