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