summaryrefslogtreecommitdiff
path: root/test/node-api/test_worker_buffer_callback/test_worker_buffer_callback.c
blob: b911fd86380644320a6fd7f056cf1cd47f7d1a3e (plain)
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
38
39
40
41
42
43
#include <stdio.h>
#include <node_api.h>
#include <assert.h>
#include "../../js-native-api/common.h"

uint32_t free_call_count = 0;
char data[] = "hello";

napi_value GetFreeCallCount(napi_env env, napi_callback_info info) {
  napi_value value;
  NAPI_CALL(env, napi_create_uint32(env, free_call_count, &value));
  return value;
}

static void finalize_cb(napi_env env, void* finalize_data, void* hint) {
  assert(finalize_data == data);
  free_call_count++;
}

NAPI_MODULE_INIT() {
  napi_property_descriptor properties[] = {
    DECLARE_NAPI_PROPERTY("getFreeCallCount", GetFreeCallCount)
  };

  NAPI_CALL(env, napi_define_properties(
      env, exports, sizeof(properties) / sizeof(*properties), properties));

  // This is a slight variation on the non-N-API test: We create an ArrayBuffer
  // rather than a Node.js Buffer, since testing the latter would only test
  // the same code paths and not the ones specific to N-API.
  napi_value buffer;
  NAPI_CALL(env, napi_create_external_arraybuffer(
      env,
      data,
      sizeof(data),
      finalize_cb,
      NULL,
      &buffer));

  NAPI_CALL(env, napi_set_named_property(env, exports, "buffer", buffer));

  return exports;
}