File: binding.c

package info (click to toggle)
nodejs 20.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 219,072 kB
  • sloc: cpp: 1,277,408; javascript: 565,332; ansic: 129,476; python: 58,536; sh: 3,841; makefile: 2,725; asm: 1,732; perl: 248; lisp: 222; xml: 42
file content (37 lines) | stat: -rw-r--r-- 1,364 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdlib.h>
#include <node_api.h>
#include "../../js-native-api/common.h"

static void MyObject_fini(napi_env env, void* data, void* hint) {
  napi_ref* ref = data;
  napi_value global;
  napi_value cleanup;
  NODE_API_CALL_RETURN_VOID(env, napi_get_global(env, &global));
  NODE_API_CALL_RETURN_VOID(
      env, napi_get_named_property(env, global, "cleanup", &cleanup));
  napi_status status = napi_call_function(env, global, cleanup, 0, NULL, NULL);
  // We may not be allowed to call into JS, in which case a pending exception
  // will be returned.
  NODE_API_ASSERT_RETURN_VOID(env,
      status == napi_ok || status == napi_pending_exception,
      "Unexpected status for napi_call_function");
  NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, *ref));
  free(ref);
}

static napi_value MyObject(napi_env env, napi_callback_info info) {
  napi_value js_this;
  napi_ref* ref = malloc(sizeof(*ref));
  NODE_API_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &js_this, NULL));
  NODE_API_CALL(env, napi_wrap(env, js_this, ref, MyObject_fini, NULL, ref));
  return NULL;
}

NAPI_MODULE_INIT() {
  napi_value ctor;
  NODE_API_CALL(
      env, napi_define_class(
          env, "MyObject", NAPI_AUTO_LENGTH, MyObject, NULL, 0, NULL, &ctor));
  NODE_API_CALL(env, napi_set_named_property(env, exports, "MyObject",  ctor));
  return exports;
}