1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <node_api.h>
4
addon_free(napi_env env,void * data,void * hint)5 static void addon_free(napi_env env, void* data, void* hint) {
6 napi_ref* ref = data;
7 napi_delete_reference(env, *ref);
8 free(ref);
9 fprintf(stderr, "addon_free");
10 }
11
addon_new(napi_env env,napi_value exports,bool ref_first)12 napi_value addon_new(napi_env env, napi_value exports, bool ref_first) {
13 napi_ref* ref = malloc(sizeof(*ref));
14 if (ref_first) {
15 napi_create_reference(env, exports, 1, ref);
16 napi_set_instance_data(env, ref, addon_free, NULL);
17 } else {
18 napi_set_instance_data(env, ref, addon_free, NULL);
19 napi_create_reference(env, exports, 1, ref);
20 }
21 return exports;
22 }
23