Skip to content

Commit

Permalink
Ported to Nan2
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkhan committed Sep 1, 2015
1 parent a4af0b6 commit 25a2bbb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
3 changes: 3 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{
"target_name": "GcProfiler",
"sources": [ "src/GcProfiler.cc" ],
"include_dirs": [
"<!(node -e \"require('nan')\")"
]
}
]
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"description": "Allows you to profile when the garbage collector runs, and how long it takes.",
"version": "1.2.0",
"author": "Bret Copeland <[email protected]>",
"contributors": [{
"name": "Daniel Khan",
"email": "[email protected]"
}],
"main": "./main.js",
"gypfile": true,
"repository": {
"type": "git",
"url": "https://github.com/bretcope/node-gc-profiler.git"
Expand All @@ -16,6 +21,6 @@
],
"license": "MIT",
"dependencies": {
"nan": "^1.6.2"
"nan": "^2.0.8"
}
}
57 changes: 29 additions & 28 deletions src/GcProfiler.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <node.h>
#include "../node_modules/nan/nan.h"
#include <nan.h>
#include <time.h>

#ifdef WIN32
Expand All @@ -26,8 +26,8 @@ namespace GcProfiler

// static variables
GcProfilerData * _data;
Persistent<Function> _callback;
Persistent<Context> _context;
Nan::Persistent<v8::Function> _callback;
// Nan::Persistent<Context> _context;

#ifdef WIN32

Expand All @@ -41,7 +41,7 @@ namespace GcProfiler
#endif

// function prototypes
void Init(Handle<Object> exports);
void Init(v8::Local<v8::Object> exports, v8::Local<v8::Object> module);
NAN_METHOD(LoadProfiler);
NAN_GC_CALLBACK(Before);
NAN_GC_CALLBACK(After);
Expand All @@ -55,27 +55,28 @@ namespace GcProfiler

// --- functions ---

void Init (Handle<Object> exports)
void Init (v8::Local<v8::Object> exports, v8::Local<v8::Object> module)
{
NODE_SET_METHOD(exports, "loadProfiler", LoadProfiler);
exports->Set(Nan::New("loadProfiler").ToLocalChecked(), Nan::New<v8::FunctionTemplate>(LoadProfiler)->GetFunction());
}

NAN_METHOD(LoadProfiler)
{
NanScope();

if (args.Length() == 0 || !args[0]->IsFunction())
{
NanThrowTypeError("Must provide a callback function to the profiler.");
}

NanAssignPersistent(_callback, args[0].As<Function>());
NanAddGCPrologueCallback(Before);
NanAddGCEpilogueCallback(After);

NanReturnUndefined();

void LoadProfiler(const Nan::FunctionCallbackInfo<v8::Value>& info) {

if (info.Length() == 0 || !info[0]->IsFunction())
{
Nan::ThrowTypeError("Must provide a callback function to the profiler.");
return;
}

_callback.Reset(Nan::Persistent<v8::Function>(info[0].As<v8::Function>()));

This comment has been minimized.

Copy link
@kkoopa

kkoopa Sep 8, 2015

This should be _callback.Reset(info[0].As<v8::Function>()); The current code leaks a persistent reference due to the Nan::Persistent<> constructor.


Nan::AddGCPrologueCallback(Before);
Nan::AddGCEpilogueCallback(After);

return info.GetReturnValue().SetUndefined();
}

NAN_GC_CALLBACK(Before)
{
_data = new GcProfilerData();
Expand All @@ -101,20 +102,20 @@ namespace GcProfiler

void UvAsyncAfter(uv_work_t * req)
{
NanScope();
Nan::HandleScope scope;

GcProfilerData * data = (GcProfilerData*)req->data;

const unsigned argc = 4;
Handle<Value> argv[argc] = {
NanNew<Number>(data->startTime),
NanNew<Number>(data->duration),
NanNew<Number>((int)data->type),
NanNew<Number>((int)data->flags)
v8::Local<v8::Value> argv[argc] = {
Nan::New<Number>(data->startTime),
Nan::New<Number>(data->duration),
Nan::New<Number>((int)data->type),
Nan::New<Number>((int)data->flags)
};

delete data;
NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(_callback), argc, argv);
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), Nan::New(_callback), argc, argv);
}

#ifdef __MACH__
Expand Down

0 comments on commit 25a2bbb

Please sign in to comment.