1 #include <node.h>
2 #include <node_buffer.h>
3 #include <v8.h>
4
5 using v8::Context;
6 using v8::FunctionCallbackInfo;
7 using v8::Isolate;
8 using v8::Local;
9 using v8::Object;
10 using v8::Value;
11
12 uint32_t free_call_count = 0;
13
GetFreeCallCount(const FunctionCallbackInfo<Value>& args)14 void GetFreeCallCount(const FunctionCallbackInfo<Value>& args) {
15 args.GetReturnValue().Set(free_call_count);
16 }
17
Initialize(Local<Object> exports, Local<Value> module, Local<Context> context)18 void Initialize(Local<Object> exports,
19 Local<Value> module,
20 Local<Context> context) {
21 Isolate* isolate = context->GetIsolate();
22 NODE_SET_METHOD(exports, "getFreeCallCount", GetFreeCallCount);
23
24 char* data = new char;
25
26 exports->Set(context,
27 v8::String::NewFromUtf8(
28 isolate, "buffer").ToLocalChecked(),
29 node::Buffer::New(
30 isolate,
31 data,
32 sizeof(char),
33 [](char* data, void* hint) {
34 delete data;
35 free_call_count++;
36 },
37 nullptr).ToLocalChecked()).Check();
38 }
39
40 NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
41