1 #include <node_api.h>
2 #include "../../js-native-api/common.h"
3
MakeCallback(napi_env env,napi_callback_info info)4 static napi_value MakeCallback(napi_env env, napi_callback_info info) {
5 size_t argc = 2;
6 napi_value args[2];
7 // NOLINTNEXTLINE (readability/null_usage)
8 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
9
10 napi_value recv = args[0];
11 napi_value func = args[1];
12
13 napi_status status = napi_make_callback(env, NULL /* async_context */,
14 recv, func, 0 /* argc */, NULL /* argv */, NULL /* result */);
15
16 bool isExceptionPending;
17 NAPI_CALL(env, napi_is_exception_pending(env, &isExceptionPending));
18 if (isExceptionPending && !(status == napi_pending_exception)) {
19 // if there is an exception pending we don't expect any
20 // other error
21 napi_value pending_error;
22 status = napi_get_and_clear_last_exception(env, &pending_error);
23 NAPI_CALL(env,
24 napi_throw_error((env),
25 NULL,
26 "error when only pending exception expected"));
27 }
28
29 return recv;
30 }
31
Init(napi_env env,napi_value exports)32 static napi_value Init(napi_env env, napi_value exports) {
33 napi_value fn;
34 NAPI_CALL(env, napi_create_function(
35 // NOLINTNEXTLINE (readability/null_usage)
36 env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
37 NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
38 return exports;
39 }
40
41 NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
42