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;
2223static uint32_t s_currentLag;
2324static 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