Skip to content

Commit 58b7a5f

Browse files
author
Jonas Dohse
committed
Use nan to support node 0.12
1 parent 339ac72 commit 58b7a5f

4 files changed

Lines changed: 39 additions & 25 deletions

File tree

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ language: node_js
33
node_js:
44
- 0.8
55
- 0.10
6+
- 0.12
7+
- iojs
68

79
notifications:
810
email:

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{
44
'target_name': 'toobusy',
55
'include_dirs': [
6+
"<!(node -e \"require('nan')\")",
67
],
78
'sources': [
89
'toobusy.cc',

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"homepage": "https://github.com/lloyd/node-toobusy",
55
"version": "0.2.4",
66
"dependencies": {
7-
"bindings": "1.1.0"
7+
"bindings": "1.1.0",
8+
"nan": "1.8.4"
89
},
910
"devDependencies": {
1011
"should": "1.2.1",

toobusy.cc

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <v8.h>
2+
#include <nan.h>
23
#include <node.h>
34
#include <uv.h>
45
#include <stdlib.h>
@@ -22,7 +23,8 @@ static uv_timer_t s_timer;
2223
static uint32_t s_currentLag;
2324
static uint64_t s_lastMark;
2425

25-
Handle<Value> TooBusy(const Arguments& args) {
26+
NAN_METHOD(TooBusy) {
27+
NanScope();
2628
// No HandleScope required, because this function allocates no
2729
// v8 classes that reside on the heap.
2830
bool block = false;
@@ -34,44 +36,42 @@ Handle<Value> TooBusy(const Arguments& args) {
3436
double r = (rand() / (double) RAND_MAX) * 100.0;
3537
if (r < pctToBlock) block = true;
3638
}
37-
return block ? True() : False();
39+
NanReturnValue(NanNew(block));
3840
}
3941

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

4446
uv_timer_stop(&s_timer);
45-
return Undefined();
46-
}
4747

48-
Handle<Value> Lag(const Arguments& args) {
49-
HandleScope scope;
50-
return scope.Close(Integer::New(s_currentLag));
48+
NanReturnUndefined();
5149
}
5250

53-
Handle<Value> HighWaterMark(const Arguments& args) {
54-
HandleScope scope;
51+
NAN_METHOD(Lag) {
52+
NanScope();
53+
NanReturnValue(NanNew(s_currentLag));
54+
}
5555

56+
NAN_METHOD(HighWaterMark) {
57+
NanScope();
5658
if (args.Length() >= 1) {
5759
if (!args[0]->IsNumber()) {
58-
return v8::ThrowException(
59-
v8::Exception::Error(
60-
v8::String::New("expected numeric first argument")));
60+
NanThrowError("expected numeric first argument");
61+
NanReturnUndefined();
6162
}
6263
int hwm = args[0]->Int32Value();
6364
if (hwm < 10) {
64-
return v8::ThrowException(
65-
v8::Exception::Error(
66-
v8::String::New("maximum lag should be greater than 10ms")));
65+
NanThrowError("maximum lag should be greater than 10ms");
66+
NanReturnUndefined();
6767
}
6868
HIGH_WATER_MARK_MS = hwm;
6969
}
7070

71-
return scope.Close(Number::New(HIGH_WATER_MARK_MS));
71+
NanReturnValue(NanNew(HIGH_WATER_MARK_MS));
7272
}
7373

74-
static void every_second(uv_timer_t* handle, int status)
74+
static void every_second(uv_timer_t* handle)
7575
{
7676
uint64_t now = uv_hrtime();
7777

@@ -85,13 +85,23 @@ static void every_second(uv_timer_t* handle, int status)
8585
s_lastMark = now;
8686
};
8787

88-
extern "C" void init(Handle<Object> target) {
89-
HandleScope scope;
88+
#pragma GCC diagnostic ignored "-Wunused-function"
89+
static void every_second(uv_timer_t* handle, int) {
90+
every_second(handle);
91+
}
92+
93+
extern "C" void init(Handle<Object> exports) {
94+
NanScope();
95+
96+
exports->Set(NanNew("toobusy"),
97+
NanNew<FunctionTemplate>(TooBusy)->GetFunction());
98+
exports->Set(NanNew("shutdown"),
99+
NanNew<FunctionTemplate>(ShutDown)->GetFunction());
100+
exports->Set(NanNew("lag"),
101+
NanNew<FunctionTemplate>(Lag)->GetFunction());
102+
exports->Set(NanNew("maxLag"),
103+
NanNew<FunctionTemplate>(HighWaterMark)->GetFunction());
90104

91-
target->Set(String::New("toobusy"), FunctionTemplate::New(TooBusy)->GetFunction());
92-
target->Set(String::New("shutdown"), FunctionTemplate::New(ShutDown)->GetFunction());
93-
target->Set(String::New("lag"), FunctionTemplate::New(Lag)->GetFunction());
94-
target->Set(String::New("maxLag"), FunctionTemplate::New(HighWaterMark)->GetFunction());
95105
uv_timer_init(uv_default_loop(), &s_timer);
96106
uv_timer_start(&s_timer, every_second, POLL_PERIOD_MS, POLL_PERIOD_MS);
97107
};

0 commit comments

Comments
 (0)