• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <node_api.h>
3 #include <assert.h>
4 #include "../../js-native-api/common.h"
5 
6 uint32_t free_call_count = 0;
7 char data[] = "hello";
8 
GetFreeCallCount(napi_env env,napi_callback_info info)9 napi_value GetFreeCallCount(napi_env env, napi_callback_info info) {
10   napi_value value;
11   NAPI_CALL(env, napi_create_uint32(env, free_call_count, &value));
12   return value;
13 }
14 
finalize_cb(napi_env env,void * finalize_data,void * hint)15 static void finalize_cb(napi_env env, void* finalize_data, void* hint) {
16   assert(finalize_data == data);
17   free_call_count++;
18 }
19 
NAPI_MODULE_INIT()20 NAPI_MODULE_INIT() {
21   napi_property_descriptor properties[] = {
22     DECLARE_NAPI_PROPERTY("getFreeCallCount", GetFreeCallCount)
23   };
24 
25   NAPI_CALL(env, napi_define_properties(
26       env, exports, sizeof(properties) / sizeof(*properties), properties));
27 
28   // This is a slight variation on the non-N-API test: We create an ArrayBuffer
29   // rather than a Node.js Buffer, since testing the latter would only test
30   // the same code paths and not the ones specific to N-API.
31   napi_value buffer;
32   NAPI_CALL(env, napi_create_external_arraybuffer(
33       env,
34       data,
35       sizeof(char),
36       finalize_cb,
37       NULL,
38       &buffer));
39 
40   NAPI_CALL(env, napi_set_named_property(env, exports, "buffer", buffer));
41 
42   return exports;
43 }
44