From d7908a2a7475fa901a327c9f318cb18a8300b9e8 Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Fri, 3 May 2019 11:47:37 -0700 Subject: [PATCH] Support node up to and including 12.x --- .travis.yml | 5 +++++ core.cc | 61 +++++++++++++++++++++++++++++----------------------- package.json | 2 +- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5739413..1c219c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,11 @@ node_js: - '4' - '5' - '6' + - '8' + - '10' + - '11' + - '12' +sudo: false addons: apt: sources: diff --git a/core.cc b/core.cc index 4b3f23c..a038b95 100644 --- a/core.cc +++ b/core.cc @@ -3,10 +3,9 @@ #include using v8::Function; -using v8::FunctionTemplate; -using v8::Handle; +using v8::Int32; +using v8::Isolate; using v8::Local; -using v8::Null; using v8::Number; using v8::Object; using v8::String; @@ -24,14 +23,15 @@ class Worker: public Nan::AsyncWorker { } void Execute() { - syslog(priority, "%s", message); + if(message) + syslog(priority, "%s", message); } void HandleOKCallback() { Nan::HandleScope scope; if(callback) - callback->Call(0, NULL); + callback->Call(0, NULL, async_resource); }; private: @@ -44,18 +44,26 @@ static char ident[1024]; // wrap: void openlog(const char *ident, int option, int facility); NAN_METHOD(OpenLog) { - // openlog requires ident be statically allocated. Write doesn't guarantee - // NULL-termination, so preserve last byte as NULL. - info[0]->ToString()->WriteUtf8(ident, sizeof(ident)-1); - int option = info[1]->Int32Value(); - int facility = info[2]->Int32Value(); + const Nan::Utf8String arg0(info[0]); + Local arg1 = Nan::To(info[1]).ToLocalChecked(); + Local arg2 = Nan::To(info[2]).ToLocalChecked(); + + // openlog() requires ident be statically allocated. + size_t length = arg0.length(); + if(length) { + if(length > sizeof(ident)-1) + length = sizeof(ident)-1; + strncpy(ident, *arg0, length); + } + int option = arg1->Value(); + int facility = arg2->Value(); openlog(ident, option, facility); return; } -static char* dupBuf(const Handle& arg) { +static char* dupBuf(const Local& arg) { const char* mem = node::Buffer::Data(arg); size_t memsz = node::Buffer::Length(arg); char* s = new char[memsz + 1]; @@ -64,38 +72,36 @@ static char* dupBuf(const Handle& arg) { return s; } -static char* dupStr(const Local& m) { - if(m.IsEmpty()) - return NULL; - - // Exact calculation of UTF length involves double traversal. Avoid this - // because we know UTF8 expansion is < 4 bytes out per byte in. - char* s = new char[m->Length() * 4]; - m->WriteUtf8(s); +static char* dupStr(const Local& arg) { + const Nan::Utf8String str(arg); + const char* mem = *str; + size_t memsz = str.length(); + char* s = new char[memsz + 1]; + memcpy(s, mem, memsz); + s[memsz] = 0; return s; } // wrap: void syslog(int priority, const char *format, ...); NAN_METHOD(SysLog) { - int priority = info[0]->Int32Value(); + Local arg0 = Nan::To(info[0]).ToLocalChecked(); + + int priority = arg0->Value(); char* message = NULL; Nan::Callback *callback = NULL; - if (info[2]->IsFunction()) + if(info[2]->IsFunction()) callback = new Nan::Callback(info[2].As()); if(node::Buffer::HasInstance(info[1])) { message = dupBuf(info[1]); } else { - message = dupStr(info[1]->ToString()); + message = dupStr(info[1]); } - if (message) { + if(message || callback) { Nan::AsyncQueueWorker(new Worker(callback, priority, message)); - } else if(callback) { - callback->Call(0, NULL); - delete callback; } return; @@ -104,7 +110,8 @@ NAN_METHOD(SysLog) { // wrap: int setlogmask(int mask); NAN_METHOD(SetLogMask) { - int mask = info[0]->Int32Value(); + Local arg0 = Nan::To(info[0]).ToLocalChecked(); + int mask = arg0->Value(); int last = setlogmask(mask); info.GetReturnValue().Set(Nan::New(last)); diff --git a/package.json b/package.json index b350f33..78a2951 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "tap": "^1.3.2" }, "dependencies": { - "nan": "^2.0.5" + "nan": "^2.13.2" }, "tags": [ "log",