xref: /third_party/node/test/node-api/test_make_callback_recurse/binding.c (revision 1cb0ef41)
  • Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
  • only in /third_party/node/test/node-api/test_make_callback_recurse/
1#include <node_api.h>
2#include "../../js-native-api/common.h"
3
4static 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  NODE_API_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  NODE_API_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    NODE_API_CALL(env,
24      napi_throw_error((env),
25                        NULL,
26                        "error when only pending exception expected"));
27  }
28
29  return recv;
30}
31
32static napi_value Init(napi_env env, napi_value exports) {
33  napi_value fn;
34  NODE_API_CALL(env, napi_create_function(
35      // NOLINTNEXTLINE (readability/null_usage)
36      env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
37  NODE_API_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
38  return exports;
39}
40
41NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
42

Indexes created Thu Nov 07 10:32:03 CST 2024