Skip to content

Commit ee16c2d

Browse files
committed
update using nan and v0.11 support added
1 parent 475ce02 commit ee16c2d

9 files changed

+141
-135
lines changed

.travis.yml

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
language: node_js
22

33
node_js:
4-
- 0.6
5-
- 0.8
64
- 0.10
7-
8-
notifications:
9-
email:
10-
5+
- 0.11

binding.gyp

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
{
44
'target_name': 'memwatch',
55
'include_dirs': [
6+
'<!(node -p -e "require(\'path\').dirname(require.resolve(\'nan\'))")'
67
],
78
'sources': [
89
'src/heapdiff.cc',
910
'src/init.cc',
1011
'src/memwatch.cc',
1112
'src/util.cc'
12-
],
13+
]
1314
}
1415
]
1516
}

include.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const
2-
magic = require('./build/Release/memwatch'),
2+
magic = require('bindings')('memwatch'),
33
events = require('events');
44

55
module.exports = new events.EventEmitter();

package.json

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,37 @@
33
"description": "Keep an eye on your memory usage, and discover and isolate leaks.",
44
"version": "0.2.2",
55
"author": "Lloyd Hilaiel (http://lloyd.io)",
6-
"engines": { "node": ">= 0.6.0" },
6+
"engines": {
7+
"node": ">= 0.8.0"
8+
},
79
"repository": {
810
"type": "git",
911
"url": "https://github.com/lloyd/node-memwatch.git"
1012
},
1113
"main": "include.js",
12-
"licenses": [ { "type": "wtfpl" } ],
14+
"licenses": [
15+
{
16+
"type": "wtfpl"
17+
}
18+
],
1319
"bugs": {
14-
"url" : "https://github.com/lloyd/node-memwatch/issues"
20+
"url": "https://github.com/lloyd/node-memwatch/issues"
1521
},
1622
"scripts": {
1723
"install": "node-gyp rebuild",
18-
"test": "mocha tests"
24+
"test": "mocha tests --reporter spec"
1925
},
2026
"devDependencies": {
2127
"mocha": "1.2.2",
22-
"should": "0.6.3",
23-
"node-gyp": "0.5.7"
28+
"should": "0.6.3"
2429
},
2530
"contributors": [
2631
"Jed Parsons (@jedp)",
2732
"Jeff Haynie (@jhaynie)",
2833
"Justin Matthews (@jmatthewsr-ms)"
29-
]
34+
],
35+
"dependencies": {
36+
"bindings": "^1.2.0",
37+
"nan": "^1.2.0"
38+
}
3039
}

src/heapdiff.cc

+62-61
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
/*
22
* 2012|lloyd|http://wtfpl.org
33
*/
4-
5-
#include "heapdiff.hh"
6-
#include "util.hh"
7-
8-
#include <node.h>
9-
104
#include <map>
115
#include <string>
126
#include <set>
@@ -15,14 +9,17 @@
159
#include <stdlib.h> // abs()
1610
#include <time.h> // time()
1711

12+
#include "heapdiff.hh"
13+
#include "util.hh"
14+
1815
using namespace v8;
1916
using namespace node;
2017
using namespace std;
2118

2219
static bool s_inProgress = false;
2320
static time_t s_startTime;
2421

25-
bool heapdiff::HeapDiff::InProgress()
22+
bool heapdiff::HeapDiff::InProgress()
2623
{
2724
return s_inProgress;
2825
}
@@ -48,29 +45,27 @@ heapdiff::HeapDiff::~HeapDiff()
4845
void
4946
heapdiff::HeapDiff::Initialize ( v8::Handle<v8::Object> target )
5047
{
51-
v8::HandleScope scope;
52-
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
48+
NanScope();
49+
50+
v8::Local<v8::FunctionTemplate> t = NanNew<v8::FunctionTemplate>(New);
5351
t->InstanceTemplate()->SetInternalFieldCount(1);
54-
t->SetClassName(String::NewSymbol("HeapDiff"));
52+
t->SetClassName(NanNew("HeapDiff"));
5553

5654
NODE_SET_PROTOTYPE_METHOD(t, "end", End);
5755

58-
target->Set(v8::String::NewSymbol( "HeapDiff"), t->GetFunction());
56+
target->Set(NanNew( "HeapDiff"), t->GetFunction());
5957
}
6058

61-
v8::Handle<v8::Value>
62-
heapdiff::HeapDiff::New (const v8::Arguments& args)
59+
NAN_METHOD(heapdiff::HeapDiff::New)
6360
{
6461
// Don't blow up when the caller says "new require('memwatch').HeapDiff()"
6562
// issue #30
6663
// stolen from: https://github.com/kkaefer/node-cpp-modules/commit/bd9432026affafd8450ecfd9b49b7dc647b6d348
6764
if (!args.IsConstructCall()) {
68-
return ThrowException(
69-
Exception::TypeError(
70-
String::New("Use the new operator to create instances of this object.")));
65+
return NanThrowTypeError("Use the new operator to create instances of this object.");
7166
}
7267

73-
v8::HandleScope scope;
68+
NanScope();
7469

7570
// allocate the underlying c++ class and wrap it up in the this pointer
7671
HeapDiff * self = new HeapDiff();
@@ -79,22 +74,28 @@ heapdiff::HeapDiff::New (const v8::Arguments& args)
7974
// take a snapshot and save a pointer to it
8075
s_inProgress = true;
8176
s_startTime = time(NULL);
82-
self->before = v8::HeapProfiler::TakeSnapshot(v8::String::New(""));
77+
78+
#if (NODE_MODULE_VERSION > 0x000B)
79+
self->before = v8::Isolate::GetCurrent()->GetHeapProfiler()->TakeHeapSnapshot(NanNew<v8::String>(""), NULL);
80+
#else
81+
self->before = v8::HeapProfiler::TakeSnapshot(NanNew<v8::String>(""), HeapSnapshot::kFull, NULL);
82+
#endif
83+
8384
s_inProgress = false;
8485

85-
return args.This();
86+
NanReturnValue(args.This());
8687
}
8788

8889
static string handleToStr(const Handle<Value> & str)
8990
{
9091
String::Utf8Value utfString(str->ToString());
91-
return *utfString;
92+
return *utfString;
9293
}
9394

9495
static void
9596
buildIDSet(set<uint64_t> * seen, const HeapGraphNode* cur, int & s)
9697
{
97-
v8::HandleScope scope;
98+
NanScope();
9899

99100
// cycle detection
100101
if (seen->find(cur->GetId()) != seen->end()) {
@@ -210,66 +211,66 @@ static void manageChange(changeset & changes, const HeapGraphNode * node, bool a
210211

211212
static Handle<Value> changesetToObject(changeset & changes)
212213
{
213-
v8::HandleScope scope;
214-
Local<Array> a = Array::New();
214+
NanEscapableScope();
215+
Local<Array> a = NanNew<v8::Array>();
215216

216217
for (changeset::iterator i = changes.begin(); i != changes.end(); i++) {
217-
Local<Object> d = Object::New();
218-
d->Set(String::New("what"), String::New(i->first.c_str()));
219-
d->Set(String::New("size_bytes"), Integer::New(i->second.size));
220-
d->Set(String::New("size"), String::New(mw_util::niceSize(i->second.size).c_str()));
221-
d->Set(String::New("+"), Integer::New(i->second.added));
222-
d->Set(String::New("-"), Integer::New(i->second.released));
218+
Local<Object> d = NanNew<v8::Object>();
219+
d->Set(NanNew("what"), NanNew(i->first.c_str()));
220+
d->Set(NanNew("size_bytes"), NanNew<v8::Number>(i->second.size));
221+
d->Set(NanNew("size"), NanNew(mw_util::niceSize(i->second.size).c_str()));
222+
d->Set(NanNew("+"), NanNew<v8::Number>(i->second.added));
223+
d->Set(NanNew("-"), NanNew<v8::Number>(i->second.released));
223224
a->Set(a->Length(), d);
224225
}
225226

226-
return scope.Close(a);
227+
return NanEscapeScope(a);
227228
}
228229

229230

230231
static v8::Handle<Value>
231232
compare(const v8::HeapSnapshot * before, const v8::HeapSnapshot * after)
232233
{
233-
v8::HandleScope scope;
234+
NanEscapableScope();
234235
int s, diffBytes;
235236

236-
Local<Object> o = Object::New();
237+
Local<Object> o = NanNew<v8::Object>();
237238

238239
// first let's append summary information
239-
Local<Object> b = Object::New();
240-
b->Set(String::New("nodes"), Integer::New(before->GetNodesCount()));
241-
b->Set(String::New("time"), NODE_UNIXTIME_V8(s_startTime));
242-
o->Set(String::New("before"), b);
240+
Local<Object> b = NanNew<v8::Object>();
241+
b->Set(NanNew("nodes"), NanNew(before->GetNodesCount()));
242+
//b->Set(NanNew("time"), s_startTime);
243+
o->Set(NanNew("before"), b);
243244

244-
Local<Object> a = Object::New();
245-
a->Set(String::New("nodes"), Integer::New(after->GetNodesCount()));
246-
a->Set(String::New("time"), NODE_UNIXTIME_V8(time(NULL)));
247-
o->Set(String::New("after"), a);
245+
Local<Object> a = NanNew<v8::Object>();
246+
a->Set(NanNew("nodes"), NanNew(after->GetNodesCount()));
247+
//a->Set(NanNew("time"), time(NULL));
248+
o->Set(NanNew("after"), a);
248249

249250
// now let's get allocations by name
250251
set<uint64_t> beforeIDs, afterIDs;
251252
s = 0;
252253
buildIDSet(&beforeIDs, before->GetRoot(), s);
253-
b->Set(String::New("size_bytes"), Integer::New(s));
254-
b->Set(String::New("size"), String::New(mw_util::niceSize(s).c_str()));
254+
b->Set(NanNew("size_bytes"), NanNew(s));
255+
b->Set(NanNew("size"), NanNew(mw_util::niceSize(s).c_str()));
255256

256257
diffBytes = s;
257258
s = 0;
258259
buildIDSet(&afterIDs, after->GetRoot(), s);
259-
a->Set(String::New("size_bytes"), Integer::New(s));
260-
a->Set(String::New("size"), String::New(mw_util::niceSize(s).c_str()));
260+
a->Set(NanNew("size_bytes"), NanNew(s));
261+
a->Set(NanNew("size"), NanNew(mw_util::niceSize(s).c_str()));
261262

262263
diffBytes = s - diffBytes;
263264

264-
Local<Object> c = Object::New();
265-
c->Set(String::New("size_bytes"), Integer::New(diffBytes));
266-
c->Set(String::New("size"), String::New(mw_util::niceSize(diffBytes).c_str()));
267-
o->Set(String::New("change"), c);
265+
Local<Object> c = NanNew<v8::Object>();
266+
c->Set(NanNew("size_bytes"), NanNew(diffBytes));
267+
c->Set(NanNew("size"), NanNew(mw_util::niceSize(diffBytes).c_str()));
268+
o->Set(NanNew("change"), c);
268269

269270
// before - after will reveal nodes released (memory freed)
270271
vector<uint64_t> changedIDs;
271272
setDiff(beforeIDs, afterIDs, changedIDs);
272-
c->Set(String::New("freed_nodes"), Integer::New(changedIDs.size()));
273+
c->Set(NanNew("freed_nodes"), NanNew<v8::Number>(changedIDs.size()));
273274

274275
// here's where we'll collect all the summary information
275276
changeset changes;
@@ -285,39 +286,39 @@ compare(const v8::HeapSnapshot * before, const v8::HeapSnapshot * after)
285286
// after - before will reveal nodes added (memory allocated)
286287
setDiff(afterIDs, beforeIDs, changedIDs);
287288

288-
c->Set(String::New("allocated_nodes"), Integer::New(changedIDs.size()));
289+
c->Set(NanNew("allocated_nodes"), NanNew<v8::Number>(changedIDs.size()));
289290

290291
for (unsigned long i = 0; i < changedIDs.size(); i++) {
291292
const HeapGraphNode * n = after->GetNodeById(changedIDs[i]);
292293
manageChange(changes, n, true);
293294
}
294295

295-
c->Set(String::New("details"), changesetToObject(changes));
296+
c->Set(NanNew("details"), changesetToObject(changes));
296297

297-
return scope.Close(o);
298+
return NanEscapeScope(o);
298299
}
299300

300-
v8::Handle<Value>
301-
heapdiff::HeapDiff::End( const Arguments& args )
301+
NAN_METHOD(heapdiff::HeapDiff::End)
302302
{
303303
// take another snapshot and compare them
304-
v8::HandleScope scope;
304+
NanScope();
305305

306306
HeapDiff *t = Unwrap<HeapDiff>( args.This() );
307307

308308
// How shall we deal with double .end()ing? The only reasonable
309309
// approach seems to be an exception, cause nothing else makes
310310
// sense.
311311
if (t->ended) {
312-
return v8::ThrowException(
313-
v8::Exception::Error(
314-
v8::String::New("attempt to end() a HeapDiff that was "
315-
"already ended")));
312+
return NanThrowError("attempt to end() a HeapDiff that was already ended");
316313
}
317314
t->ended = true;
318315

319316
s_inProgress = true;
320-
t->after = v8::HeapProfiler::TakeSnapshot(v8::String::New(""));
317+
#if (NODE_MODULE_VERSION > 0x000B)
318+
t->after = v8::Isolate::GetCurrent()->GetHeapProfiler()->TakeHeapSnapshot(NanNew<v8::String>(""), NULL);
319+
#else
320+
t->after = v8::HeapProfiler::TakeSnapshot(NanNew<v8::String>(""), HeapSnapshot::kFull, NULL);
321+
#endif
321322
s_inProgress = false;
322323

323324
v8::Handle<Value> comparison = compare(t->before, t->after);
@@ -328,5 +329,5 @@ heapdiff::HeapDiff::End( const Arguments& args )
328329
((HeapSnapshot *) t->after)->Delete();
329330
t->after = NULL;
330331

331-
return scope.Close(comparison);
332+
NanReturnValue(comparison);
332333
}

src/heapdiff.hh

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
#include <v8.h>
99
#include <v8-profiler.h>
1010
#include <node.h>
11+
#include <nan.h>
1112

12-
namespace heapdiff
13+
namespace heapdiff
1314
{
1415
class HeapDiff : public node::ObjectWrap
1516
{
1617
public:
1718
static void Initialize ( v8::Handle<v8::Object> target );
1819

19-
static v8::Handle<v8::Value> New( const v8::Arguments& args );
20-
static v8::Handle<v8::Value> End( const v8::Arguments& args );
20+
static NAN_METHOD(New);
21+
static NAN_METHOD(End);
2122
static bool InProgress();
2223

2324
protected:

src/init.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
extern "C" {
1212
void init (v8::Handle<v8::Object> target)
1313
{
14-
v8::HandleScope scope;
14+
NanScope();
1515
heapdiff::HeapDiff::Initialize(target);
1616

1717
NODE_SET_METHOD(target, "upon_gc", memwatch::upon_gc);

0 commit comments

Comments
 (0)