• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <assert.h>
2 #include <node.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <v8.h>
6 #include <uv.h>
7 
8 using v8::Context;
9 using v8::HandleScope;
10 using v8::Isolate;
11 using v8::Local;
12 using v8::Object;
13 using v8::Value;
14 
15 size_t count = 0;
16 
17 struct statically_allocated {
statically_allocatedstatically_allocated18   statically_allocated() {
19     assert(count == 0);
20     printf("ctor ");
21   }
~statically_allocatedstatically_allocated22   ~statically_allocated() {
23     assert(count == 0);
24     printf("dtor ");
25   }
26 } var;
27 
Dummy(void *)28 void Dummy(void*) {
29   assert(0);
30 }
31 
Cleanup(void * str)32 void Cleanup(void* str) {
33   printf("%s ", static_cast<const char*>(str));
34 }
35 
Initialize(Local<Object> exports,Local<Value> module,Local<Context> context)36 void Initialize(Local<Object> exports,
37                 Local<Value> module,
38                 Local<Context> context) {
39   node::AddEnvironmentCleanupHook(
40       context->GetIsolate(),
41       Cleanup,
42       const_cast<void*>(static_cast<const void*>("cleanup")));
43   node::AddEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr);
44   node::RemoveEnvironmentCleanupHook(context->GetIsolate(), Dummy, nullptr);
45 
46   if (getenv("addExtraItemToEventLoop") != nullptr) {
47     // Add an item to the event loop that we do not clean up in order to make
48     // sure that for the main thread, this addon's memory persists even after
49     // the Environment instance has been destroyed.
50     static uv_async_t extra_async;
51     uv_loop_t* loop = node::GetCurrentEventLoop(context->GetIsolate());
52     int err = uv_async_init(loop, &extra_async, [](uv_async_t*) {});
53     assert(err == 0);
54     uv_unref(reinterpret_cast<uv_handle_t*>(&extra_async));
55   }
56 }
57 
58 NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize)
59