• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <node_api.h>
3 #include <assert.h>
4 #include <stdlib.h>
5 #include "../../js-native-api/common.h"
6 
7 #define BUFFER_SIZE 4
8 
9 int wrappedNativeData;
10 napi_ref ref;
WrapFinalizer(napi_env env,void * data,void * hint)11 void WrapFinalizer(napi_env env, void* data, void* hint) {
12   uint32_t count;
13   NODE_API_CALL_RETURN_VOID(env, napi_reference_unref(env, ref, &count));
14   NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, ref));
15 }
16 
BufferFinalizer(napi_env env,void * data,void * hint)17 void BufferFinalizer(napi_env env, void* data, void* hint) {
18   free(hint);
19 }
20 
Test(napi_env env,napi_callback_info info)21 napi_value Test(napi_env env, napi_callback_info info) {
22   size_t argc = 1;
23   napi_value argv[1];
24   napi_value result;
25   void* bufferData = malloc(BUFFER_SIZE);
26 
27   NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
28   NODE_API_CALL(env,
29       napi_create_external_buffer(
30           env, BUFFER_SIZE, bufferData, BufferFinalizer, bufferData, &result));
31   NODE_API_CALL(env, napi_create_reference(env, result, 1, &ref));
32   NODE_API_CALL(env,
33       napi_wrap(
34           env, argv[0], (void*) &wrappedNativeData, WrapFinalizer, NULL, NULL));
35   return NULL;
36 }
37 
Init(napi_env env,napi_value exports)38 napi_value Init(napi_env env, napi_value exports) {
39   napi_property_descriptor properties[] = {
40     DECLARE_NODE_API_PROPERTY("Test", Test)
41   };
42 
43   NODE_API_CALL(env, napi_define_properties(
44       env, exports, sizeof(properties) / sizeof(*properties), properties));
45 
46   return exports;
47 }
48 
49 // Do not start using NAPI_MODULE_INIT() here, so that we can test
50 // compatibility of Workers with NAPI_MODULE().
51 NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
52