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 char data[] = "hello";
14
GetFreeCallCount(const FunctionCallbackInfo<Value> & args)15 void GetFreeCallCount(const FunctionCallbackInfo<Value>& args) {
16 args.GetReturnValue().Set(free_call_count);
17 }
18
Initialize(Local<Object> exports,Local<Value> module,Local<Context> context)19 void Initialize(Local<Object> exports,
20 Local<Value> module,
21 Local<Context> context) {
22 Isolate* isolate = context->GetIsolate();
23 NODE_SET_METHOD(exports, "getFreeCallCount", GetFreeCallCount);
24 exports->Set(context,
25 v8::String::NewFromUtf8(
26 isolate, "buffer", v8::NewStringType::kNormal)
27 .ToLocalChecked(),
28 node::Buffer::New(
29 isolate,
30 data,
31 sizeof(char),
32 [](char* data, void* hint) {
33 free_call_count++;
34 },
35 nullptr).ToLocalChecked()).Check();
36 }
37
38 NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
39