Skip to content

Commit

Permalink
Use nan 2.0 to support all node version till 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Dohse committed Nov 23, 2015
1 parent 339ac72 commit 6aec270
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ language: node_js
node_js:
- 0.8
- 0.10
- 0.12

matrix:
include:
- node_js: 4
env: CC=clang CXX=clang++

notifications:
email:
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
'target_name': 'toobusy',
'include_dirs': [
"<!(node -e \"require('nan')\")",
],
'sources': [
'toobusy.cc',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"homepage": "https://github.com/lloyd/node-toobusy",
"version": "0.2.4",
"dependencies": {
"bindings": "1.1.0"
"bindings": "1.1.0",
"nan": "2.0.0"
},
"devDependencies": {
"should": "1.2.1",
Expand Down
57 changes: 30 additions & 27 deletions toobusy.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <v8.h>
#include <nan.h>
#include <node.h>
#include <uv.h>
#include <stdlib.h>
Expand All @@ -22,7 +23,8 @@ static uv_timer_t s_timer;
static uint32_t s_currentLag;
static uint64_t s_lastMark;

Handle<Value> TooBusy(const Arguments& args) {
NAN_METHOD(TooBusy) {
Nan::HandleScope scope;
// No HandleScope required, because this function allocates no
// v8 classes that reside on the heap.
bool block = false;
Expand All @@ -34,44 +36,40 @@ Handle<Value> TooBusy(const Arguments& args) {
double r = (rand() / (double) RAND_MAX) * 100.0;
if (r < pctToBlock) block = true;
}
return block ? True() : False();
info.GetReturnValue().Set(Nan::New(block));
}

Handle<Value> ShutDown(const Arguments& args) {
NAN_METHOD(ShutDown) {
// No HandleScope required, because this function allocates no
// v8 classes that reside on the heap.

uv_timer_stop(&s_timer);
return Undefined();
}

Handle<Value> Lag(const Arguments& args) {
HandleScope scope;
return scope.Close(Integer::New(s_currentLag));
NAN_METHOD(Lag) {
Nan::HandleScope scope;
info.GetReturnValue().Set(Nan::New(s_currentLag));
}

Handle<Value> HighWaterMark(const Arguments& args) {
HandleScope scope;

if (args.Length() >= 1) {
if (!args[0]->IsNumber()) {
return v8::ThrowException(
v8::Exception::Error(
v8::String::New("expected numeric first argument")));
NAN_METHOD(HighWaterMark) {
Nan::HandleScope scope;
if (info.Length() >= 1) {
if (!info[0]->IsNumber()) {
Nan::ThrowError("expected numeric first argument");
return;
}
int hwm = args[0]->Int32Value();
int hwm = info[0]->Int32Value();
if (hwm < 10) {
return v8::ThrowException(
v8::Exception::Error(
v8::String::New("maximum lag should be greater than 10ms")));
Nan::ThrowError("maximum lag should be greater than 10ms");
return;
}
HIGH_WATER_MARK_MS = hwm;
}

return scope.Close(Number::New(HIGH_WATER_MARK_MS));
info.GetReturnValue().Set(Nan::New(HIGH_WATER_MARK_MS));
}

static void every_second(uv_timer_t* handle, int status)
static void every_second(uv_timer_t* handle)
{
uint64_t now = uv_hrtime();

Expand All @@ -85,13 +83,18 @@ static void every_second(uv_timer_t* handle, int status)
s_lastMark = now;
};

extern "C" void init(Handle<Object> target) {
HandleScope scope;
__attribute__((unused)) static void every_second(uv_timer_t* handle, int) {
every_second(handle);
}

extern "C" NAN_MODULE_INIT(init) {
Nan::HandleScope scope;

Nan::SetMethod(target, "toobusy", TooBusy);
Nan::SetMethod(target, "shutdown", ShutDown);
Nan::SetMethod(target, "lag", Lag);
Nan::SetMethod(target, "maxLag", HighWaterMark);

target->Set(String::New("toobusy"), FunctionTemplate::New(TooBusy)->GetFunction());
target->Set(String::New("shutdown"), FunctionTemplate::New(ShutDown)->GetFunction());
target->Set(String::New("lag"), FunctionTemplate::New(Lag)->GetFunction());
target->Set(String::New("maxLag"), FunctionTemplate::New(HighWaterMark)->GetFunction());
uv_timer_init(uv_default_loop(), &s_timer);
uv_timer_start(&s_timer, every_second, POLL_PERIOD_MS, POLL_PERIOD_MS);
};
Expand Down

0 comments on commit 6aec270

Please sign in to comment.