-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_addon_api.cc
50 lines (41 loc) · 1.6 KB
/
node_addon_api.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <napi.h>
namespace {
class MyObject : public Napi::ObjectWrap<MyObject> {
public:
static Napi::Object Init(Napi::Env env, Napi::Object exports);
MyObject(const Napi::CallbackInfo& info);
// Force trigger gc on weak references on finalization.
//
// Explanation:
// Napi::Reference (base of Napi::ObjectWrap) calls napi_delete_reference on
// finalization, which would force v8impl::RefBase::Delete to delete the
// v8impl::Reference before weak callbacks.
// Conditions like nyc would load plenty of codes/coverage informations in
// the instrumenting process, and offloads those data on exit, which
// may eventually trigger a GC (I didn't figure out what the exact reason the
// GC was triggered -- it was been triggered anyway, we force the gc here to
// produce a stable crash, as it's valid to call any javascript here) after
// addon tear down, may apply on node processes used node-addon-api.
void Finalize(Napi::Env env) override;
};
void Cleanup(Napi::Env env, void*) {
env.Global().Get("cleanup").As<Napi::Function>().Call({});
}
Napi::Object MyObject::Init(Napi::Env env, Napi::Object exports) {
Napi::Function func =
DefineClass(env,
"MyObject",
{});
exports.Set("MyObject", func);
return exports;
}
MyObject::MyObject(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<MyObject>(info) {}
void MyObject::Finalize(Napi::Env env) {
env.Global().Get("cleanup").As<Napi::Function>().Call({});
}
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
return MyObject::Init(env, exports);
}
NODE_API_MODULE(addon, InitAll)
}