Skip to content

Commit 6a5c915

Browse files
committed
Using Nan for Node 11 compatibility
1 parent 842ce2e commit 6a5c915

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

binding.gyp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
{
44
"target_name": "monitor",
55
"sources": [ "src/monitor.cc" ],
6-
"cflags_cc" : ["-fexceptions"]
6+
"cflags_cc" : ["-fexceptions"],
7+
"include_dirs" : [
8+
"<(node_root_dir)/deps/cares/include",
9+
"<!(node -e \"require('nan')\")"
10+
]
711
}
812
]
913
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "monitr",
33
"description": "Node process monitoring tool",
4-
"version": "0.0.1",
4+
"version": "0.0.2",
55
"author": "Rohini Harendra <[email protected]>",
66
"os": [ "linux" ],
77
"cpu": [ "x64", "ia32" ],
@@ -23,10 +23,11 @@
2323
],
2424
"engines": { "node": ">=0.6" },
2525
"dependencies": {
26-
"bindings": "*"
26+
"bindings": "*",
27+
"nan":"*"
2728
},
2829
"devDependencies": {
29-
"unix-dgram": ">=0.0.3",
30+
"unix-dgram": ">=0.0.4",
3031
"jshint": "~0.9.0",
3132
"yui-lint": "~0.1.1",
3233
"istanbul": "~0.1.8",

src/monitor.cc

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <dirent.h>
1010
#include <math.h>
1111
#include <node.h>
12+
#include <node_internals.h>
1213
#include <v8.h>
1314
#include <v8-debug.h>
1415
#include <unistd.h>
@@ -28,6 +29,8 @@
2829
#include <sys/time.h>
2930
#include <algorithm>
3031

32+
#include <nan.h>
33+
3134
#ifdef __APPLE__
3235
#include <sys/sysctl.h>
3336
#include <crt_externs.h>
@@ -37,9 +40,9 @@
3740
#endif
3841

3942
#define THROW_BAD_ARGS() \
40-
return ThrowException( \
43+
NanReturnValue(ThrowException( \
4144
Exception::TypeError(String::New(__FUNCTION__)) \
42-
)
45+
))
4346

4447

4548
using namespace std;
@@ -135,7 +138,11 @@ void NodeMonitor::setStatistics() {
135138

136139
// obtain memory ration
137140
v8::HeapStatistics v8stats;
141+
#if (NODE_MODULE_VERSION > 0x000B)
142+
nan_isolate->GetHeapStatistics(&v8stats);
143+
#else
138144
V8::GetHeapStatistics(&v8stats);
145+
#endif
139146
double pmem = (v8stats.used_heap_size() / (double) v8stats.total_heap_size());
140147

141148
// Obtains the CPU usage
@@ -318,7 +325,7 @@ void CpuUsageTracker::CalculateCpuUsage(CpuUsage* cur_usage,
318325

319326
// calls the function which return the Int value
320327
unsigned int NodeMonitor::getIntFunction(const char* funcName) {
321-
HandleScope scope;
328+
NanScope();
322329

323330
Local<Value> pr = Context::GetCurrent()->Global()->Get(String::New("process"));
324331

@@ -329,7 +336,7 @@ unsigned int NodeMonitor::getIntFunction(const char* funcName) {
329336
if (fval->IsFunction()) {
330337
Local<Function> fn = Local<Function>::Cast(fval);
331338
Local<Value> argv[1];
332-
argv[0] = Local<Value>::New(Null());
339+
argv[0] = NanNewLocal<Value>(Null());
333340
Local<Value> res = fn->Call(Context::GetCurrent()->Global(), 1, argv);
334341
if (res->IsNumber()) {
335342
return res->Uint32Value();
@@ -508,29 +515,32 @@ NodeMonitor::NodeMonitor() :
508515
memset(&ipcAddr_, 0,sizeof(struct sockaddr_un));
509516
}
510517

511-
static Handle<Value> GetterIPCMonitorPath(Local<String> property,
512-
const AccessorInfo& info) {
513-
return String::New(_ipcMonitorPath.c_str());
518+
static NAN_GETTER(GetterIPCMonitorPath) {
519+
NanScope();
520+
NanReturnValue(String::New(_ipcMonitorPath.c_str()));
514521
}
515522

516-
static Handle<Value> SetterIPCMonitorPath(const Arguments& args) {
523+
static NAN_METHOD(SetterIPCMonitorPath) {
524+
NanScope();
517525
if (args.Length() < 1 ||
518526
(!args[0]->IsString() && !args[0]->IsUndefined() && !args[0]->IsNull())) {
519527
THROW_BAD_ARGS();
520528
}
521529
String::Utf8Value ipcMonitorPath(args[0]);
522530
_ipcMonitorPath = *ipcMonitorPath;
523-
return Undefined();;
531+
NanReturnValue(Undefined());
524532
}
525533

526-
static Handle<Value> StartMonitor(const Arguments& args) {
534+
static NAN_METHOD(StartMonitor) {
535+
NanScope();
527536
NodeMonitor::Initialize();
528-
return Undefined();
537+
NanReturnValue(Undefined());
529538
}
530539

531-
static Handle<Value> StopMonitor(const Arguments& args) {
540+
static NAN_METHOD(StopMonitor) {
541+
NanScope();
532542
NodeMonitor::Stop();
533-
return Undefined();
543+
NanReturnValue(Undefined());
534544
}
535545

536546
void LogStackTrace(Handle<Object> obj) {
@@ -573,19 +583,31 @@ void DebugEventHandler(DebugEvent event,
573583
LogStackTrace(exec_state);
574584
}
575585

586+
void DebugEventHandler2(const v8::Debug::EventDetails& event_details) {
587+
LogStackTrace(event_details.GetExecutionState());
588+
}
589+
576590
static void SignalHangupHandler(int signal) {
591+
#if (NODE_MODULE_VERSION > 0x000B)
592+
// Node 0.11+
593+
v8::Debug::SetDebugEventListener2(DebugEventHandler2);
594+
#else
577595
v8::Debug::SetDebugEventListener(DebugEventHandler);
596+
#endif
578597
v8::Debug::DebugBreak();
579598
}
580599

581600
extern "C" void
582601
init(Handle<Object> target) {
583-
HandleScope scope;
602+
NanScope();
584603

585604
NODE_PROT_RO_PROPERTY(target, "ipcMonitorPath", GetterIPCMonitorPath);
586-
NODE_SET_METHOD(target, "setIpcMonitorPath", SetterIPCMonitorPath);
587-
NODE_SET_METHOD(target, "start", StartMonitor);
588-
NODE_SET_METHOD(target, "stop", StopMonitor);
605+
target->Set(NanSymbol("setIpcMonitorPath"),
606+
FunctionTemplate::New(SetterIPCMonitorPath)->GetFunction());
607+
target->Set(NanSymbol("start"),
608+
FunctionTemplate::New(StartMonitor)->GetFunction());
609+
target->Set(NanSymbol("stop"),
610+
FunctionTemplate::New(StopMonitor)->GetFunction());
589611

590612
RegisterSignalHandler(SIGHUP, SignalHangupHandler);
591613
}

0 commit comments

Comments
 (0)