1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <node_api.h>
4 #include <assert.h>
5 #include "../../js-native-api/common.h"
6
7 uint32_t free_call_count = 0;
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 free(finalize_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
33 char* data = malloc(sizeof(char));
34
35 NAPI_CALL(env, napi_create_external_arraybuffer(
36 env,
37 data,
38 sizeof(char),
39 finalize_cb,
40 NULL,
41 &buffer));
42
43 NAPI_CALL(env, napi_set_named_property(env, exports, "buffer", buffer));
44
45 return exports;
46 }
47