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