|
| 1 | +#include <node_api.h> |
| 2 | + |
| 3 | +#define DECLARE_NAPI_METHOD(func) \ |
| 4 | + { #func, func, 0, 0, 0, napi_default, 0 } |
| 5 | + |
| 6 | +void createNapiError(napi_env env, napi_callback_info info) { |
| 7 | + napi_status status; |
| 8 | + napi_value value; |
| 9 | + double double_value; |
| 10 | + |
| 11 | + status = napi_create_string_utf8(env, "xyz", 3, &value); |
| 12 | + if (status != napi_ok) return; |
| 13 | + |
| 14 | + status = napi_get_value_double(env, value, &double_value); |
| 15 | + if (status == napi_ok) { |
| 16 | + napi_throw_error(env, "Failed to produce error condition"); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +void testNapiErrorCleanup(napi_env env, napi_callback_info info) { |
| 21 | + napi_status status; |
| 22 | + const napi_extended_error_info *error_info = 0; |
| 23 | + napi_value result; |
| 24 | + |
| 25 | + status = napi_get_last_error_info(env, &error_info); |
| 26 | + if (status != napi_ok) return; |
| 27 | + |
| 28 | + status = napi_get_boolean(env, (error_info->error_code == napi_ok), &result); |
| 29 | + if (status != napi_ok) return; |
| 30 | + |
| 31 | + napi_set_return_value(env, info, result); |
| 32 | +} |
| 33 | + |
| 34 | +void Init(napi_env env, napi_value exports, napi_value module, void* priv) { |
| 35 | + napi_status status; |
| 36 | + |
| 37 | + napi_property_descriptor descriptors[] = { |
| 38 | + DECLARE_NAPI_METHOD(createNapiError), |
| 39 | + DECLARE_NAPI_METHOD(testNapiErrorCleanup) |
| 40 | + }; |
| 41 | + |
| 42 | + status = napi_define_properties( |
| 43 | + env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors); |
| 44 | + if (status != napi_ok) return; |
| 45 | +} |
| 46 | + |
| 47 | +NAPI_MODULE(addon, Init) |
0 commit comments