From c56e8b953f8b583649f1d9cd17085c27117e982d Mon Sep 17 00:00:00 2001 From: Jonas Dohse Date: Wed, 11 Feb 2015 23:44:53 +0000 Subject: [PATCH] Use nan to support node 0.12 --- .travis.yml | 2 ++ binding.gyp | 1 + package.json | 3 ++- toobusy.cc | 60 +++++++++++++++++++++++++++++++--------------------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index cf7fe07..54b8c60 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ language: node_js node_js: - 0.8 - 0.10 + - 0.12 + - iojs notifications: email: diff --git a/binding.gyp b/binding.gyp index 83e219d..8046e8f 100644 --- a/binding.gyp +++ b/binding.gyp @@ -3,6 +3,7 @@ { 'target_name': 'toobusy', 'include_dirs': [ + " +#include #include #include #include @@ -22,7 +23,8 @@ static uv_timer_t s_timer; static uint32_t s_currentLag; static uint64_t s_lastMark; -Handle TooBusy(const Arguments& args) { +NAN_METHOD(TooBusy) { + NanScope(); // No HandleScope required, because this function allocates no // v8 classes that reside on the heap. bool block = false; @@ -34,44 +36,42 @@ Handle TooBusy(const Arguments& args) { double r = (rand() / (double) RAND_MAX) * 100.0; if (r < pctToBlock) block = true; } - return block ? True() : False(); + NanReturnValue(NanNew(block)); } -Handle 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 Lag(const Arguments& args) { - HandleScope scope; - return scope.Close(Integer::New(s_currentLag)); + NanReturnUndefined(); } -Handle HighWaterMark(const Arguments& args) { - HandleScope scope; +NAN_METHOD(Lag) { + NanScope(); + NanReturnValue(NanNew(s_currentLag)); +} +NAN_METHOD(HighWaterMark) { + NanScope(); if (args.Length() >= 1) { if (!args[0]->IsNumber()) { - return v8::ThrowException( - v8::Exception::Error( - v8::String::New("expected numeric first argument"))); + NanThrowError("expected numeric first argument"); + NanReturnUndefined(); } int hwm = args[0]->Int32Value(); if (hwm < 10) { - return v8::ThrowException( - v8::Exception::Error( - v8::String::New("maximum lag should be greater than 10ms"))); + NanThrowError("maximum lag should be greater than 10ms"); + NanReturnUndefined(); } HIGH_WATER_MARK_MS = hwm; } - return scope.Close(Number::New(HIGH_WATER_MARK_MS)); + NanReturnValue(NanNew(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(); @@ -85,13 +85,25 @@ static void every_second(uv_timer_t* handle, int status) s_lastMark = now; }; -extern "C" void init(Handle target) { - HandleScope scope; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +static void every_second(uv_timer_t* handle, int) { + every_second(handle); +} +#pragma GCC diagnostic pop + +extern "C" void init(Handle exports) { + NanScope(); + + exports->Set(NanNew("toobusy"), + NanNew(TooBusy)->GetFunction()); + exports->Set(NanNew("shutdown"), + NanNew(ShutDown)->GetFunction()); + exports->Set(NanNew("lag"), + NanNew(Lag)->GetFunction()); + exports->Set(NanNew("maxLag"), + NanNew(HighWaterMark)->GetFunction()); - 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); };