|
| 1 | +// """ |
| 2 | +// Copyright Node.js contributors. All rights reserved. |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to |
| 6 | +// deal in the Software without restriction, including without limitation the |
| 7 | +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | +// sell copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | +// IN THE SOFTWARE. |
| 21 | +// """ |
| 22 | + |
| 23 | +// Empty value so that macros here are able to return NULL or void |
| 24 | +#define NAPI_RETVAL_NOTHING // Intentionally blank #define |
| 25 | + |
| 26 | +#define GET_AND_THROW_LAST_ERROR(env) \ |
| 27 | + do { \ |
| 28 | + const napi_extended_error_info *error_info; \ |
| 29 | + napi_get_last_error_info((env), &error_info); \ |
| 30 | + bool is_pending; \ |
| 31 | + napi_is_exception_pending((env), &is_pending); \ |
| 32 | + /* If an exception is already pending, don't rethrow it */ \ |
| 33 | + if (!is_pending) { \ |
| 34 | + const char* error_message = error_info->error_message != NULL ? \ |
| 35 | + error_info->error_message : \ |
| 36 | + "empty error message"; \ |
| 37 | + napi_throw_error((env), NULL, error_message); \ |
| 38 | + } \ |
| 39 | + } while (0) |
| 40 | + |
| 41 | +#define NAPI_ASSERT_BASE(env, assertion, message, ret_val) \ |
| 42 | + do { \ |
| 43 | + if (!(assertion)) { \ |
| 44 | + napi_throw_error( \ |
| 45 | + (env), \ |
| 46 | + NULL, \ |
| 47 | + "assertion (" #assertion ") failed: " message); \ |
| 48 | + return ret_val; \ |
| 49 | + } \ |
| 50 | + } while (0) |
| 51 | + |
| 52 | +// Returns NULL on failed assertion. |
| 53 | +// This is meant to be used inside napi_callback methods. |
| 54 | +#define NAPI_ASSERT(env, assertion, message) \ |
| 55 | + NAPI_ASSERT_BASE(env, assertion, message, NULL) |
| 56 | + |
| 57 | +// Returns empty on failed assertion. |
| 58 | +// This is meant to be used inside functions with void return type. |
| 59 | +#define NAPI_ASSERT_RETURN_VOID(env, assertion, message) \ |
| 60 | + NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING) |
| 61 | + |
| 62 | +#define NAPI_CALL_BASE(env, the_call, ret_val) \ |
| 63 | + do { \ |
| 64 | + if ((the_call) != napi_ok) { \ |
| 65 | + GET_AND_THROW_LAST_ERROR((env)); \ |
| 66 | + return ret_val; \ |
| 67 | + } \ |
| 68 | + } while (0) |
| 69 | + |
| 70 | +// Returns NULL if the_call doesn't return napi_ok. |
| 71 | +#define NAPI_CALL(env, the_call) \ |
| 72 | + NAPI_CALL_BASE(env, the_call, NULL) |
| 73 | + |
| 74 | +// Returns empty if the_call doesn't return napi_ok. |
| 75 | +#define NAPI_CALL_RETURN_VOID(env, the_call) \ |
| 76 | + NAPI_CALL_BASE(env, the_call, NAPI_RETVAL_NOTHING) |
| 77 | + |
| 78 | +// Returns empty if the_call doesn't return napi_ok. |
| 79 | +#define NAPI_CALL_RETURN_STATUS(env, the_call) \ |
| 80 | + do { \ |
| 81 | + napi_status status = (the_call); \ |
| 82 | + if (status != napi_ok) { \ |
| 83 | + GET_AND_THROW_LAST_ERROR((env)); \ |
| 84 | + return status; \ |
| 85 | + } \ |
| 86 | + } while (0) |
| 87 | + |
| 88 | +#define DECLARE_NAPI_PROPERTY(name, func) \ |
| 89 | + { (name), NULL, (func), NULL, NULL, NULL, napi_default, NULL } |
| 90 | + |
| 91 | +#define DECLARE_NAPI_GETTER(name, func) \ |
| 92 | + { (name), NULL, NULL, (func), NULL, NULL, napi_default, NULL } |
0 commit comments