-
Notifications
You must be signed in to change notification settings - Fork 1
/
gc-listener.cc
61 lines (45 loc) · 1.61 KB
/
gc-listener.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
51
52
53
54
55
56
57
58
59
60
61
#include "nan.h"
#include <uv.h>
using Nan::FunctionCallbackInfo;
using Nan::Persistent;
using v8::Value;
using v8::Object;
using v8::Function;
using v8::FunctionTemplate;
using v8::Local;
using v8::GCType;
using v8::GCCallbackFlags;
using v8::V8;
using v8::kGCTypeMarkSweepCompact;
Persistent<Function> gCB_BeforeGC;
Persistent<Function> gCB_AfterGC;
void doNothing(uv_work_t *) {}
NAN_GC_CALLBACK(cbAfterGC) {
Nan::MakeCallback( Nan::GetCurrentContext()->Global(), Nan::New(gCB_AfterGC), 0, {} );
}
void CallBeforeGC(uv_work_t* request) {
Nan::HandleScope scope;
Nan::MakeCallback( Nan::GetCurrentContext()->Global(), Nan::New(gCB_BeforeGC), 0, {} );
}
NAN_GC_CALLBACK( cbBeforeGC ) {
uv_queue_work( uv_default_loop(), new uv_work_t(), doNothing, uv_after_work_cb(CallBeforeGC) );
}
NAN_METHOD( SetCB_BeforeGC ) {
if( info.Length() == 1 && info[0]->IsFunction() ) {
gCB_BeforeGC.Reset( info[0].As<v8::Function>() );
Nan::AddGCPrologueCallback( cbBeforeGC, kGCTypeMarkSweepCompact );
}
}
NAN_METHOD( SetCB_AfterGC ) {
if( info.Length() == 1 && info[0]->IsFunction() ) {
gCB_AfterGC.Reset( info[0].As<v8::Function>() );
Nan::AddGCEpilogueCallback( cbAfterGC, kGCTypeMarkSweepCompact );
}
}
NAN_MODULE_INIT(Init) {
Local<Function> beforeGC = Nan::GetFunction( Nan::New<FunctionTemplate>(SetCB_BeforeGC) ).ToLocalChecked();
Nan::Set(target, Nan::New("before").ToLocalChecked(), beforeGC );
Local<Function> afterGC = Nan::GetFunction( Nan::New<FunctionTemplate>(SetCB_AfterGC) ).ToLocalChecked();
Nan::Set(target, Nan::New("after").ToLocalChecked(), afterGC );
}
NODE_MODULE(gclistener, Init);