1 #include <node.h>
2 #include <node_buffer.h>
3 #include <v8.h>
4 
5 #include <assert.h>
6 
7 static int alive;
8 
FreeCallback(char* data, void* hint)9 static void FreeCallback(char* data, void* hint) {
10   assert(data == nullptr);
11   alive--;
12 }
13 
IsAlive(const v8::FunctionCallbackInfo<v8::Value>& args)14 void IsAlive(const v8::FunctionCallbackInfo<v8::Value>& args) {
15   args.GetReturnValue().Set(alive);
16 }
17 
Run(const v8::FunctionCallbackInfo<v8::Value>& args)18 void Run(const v8::FunctionCallbackInfo<v8::Value>& args) {
19   v8::Isolate* isolate = args.GetIsolate();
20   alive++;
21 
22   {
23     v8::HandleScope scope(isolate);
24     v8::Local<v8::Object> buf = node::Buffer::New(
25           isolate,
26           nullptr,
27           0,
28           FreeCallback,
29           nullptr).ToLocalChecked();
30 
31     char* data = node::Buffer::Data(buf);
32     assert(data == nullptr);
33   }
34 }
35 
init(v8::Local<v8::Object> exports)36 void init(v8::Local<v8::Object> exports) {
37   NODE_SET_METHOD(exports, "run", Run);
38   NODE_SET_METHOD(exports, "isAlive", IsAlive);
39 }
40 
41 NODE_MODULE(NODE_GYP_MODULE_NAME, init)
42