From ce83ceb1349ac6eaf18f4331cbc4287c0ca02998 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Mon, 21 Sep 2015 17:02:44 +0200 Subject: [PATCH 1/5] nan2 --- .travis.yml | 33 ++++++++---- node-stringprep.cc | 131 ++++++++++++++++++++++----------------------- package.json | 16 +++--- 3 files changed, 94 insertions(+), 86 deletions(-) diff --git a/.travis.yml b/.travis.yml index c6180b6..287f836 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,25 @@ +sudo: false + language: node_js + +env: + - CXX="g++-4.8" + node_js: - - "0.8" - - "0.10" - - "0.11" - - iojs + # - '0.8' + # - '0.10' + # - '0.12' + - '4.0' + # - '4.1' + +addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 + - gcc-4.8 + before_install: - - "sudo apt-get install libicu-dev" - # Workaround for a permissions issue with Travis virtual machine images - # that breaks Python's multiprocessing: - # https://github.com/travis-ci/travis-cookbooks/issues/155 - - sudo rm -rf /dev/shm - - sudo ln -s /run/shm /dev/shm - - npm i -g npm@1.X + # npm shipped with Node.js 0.8 doesn't support carret so let's update it + - 'if [ "$TRAVIS_NODE_VERSION" == "0.8" ]; then npm i -g npm; fi' diff --git a/node-stringprep.cc b/node-stringprep.cc index 4b5b371..a0f4cb8 100644 --- a/node-stringprep.cc +++ b/node-stringprep.cc @@ -13,19 +13,19 @@ class UnknownProfileException : public std::exception { }; // protect constructor from GC -static Persistent stringprep_constructor; +static Nan::Persistent stringprep_constructor; -class StringPrep : public ObjectWrap { +class StringPrep : public Nan::ObjectWrap { public: - static void Initialize(Handle target) + static void Initialize(Local target) { - NanScope(); - Local t = NanNew(New); - NanAssignPersistent(stringprep_constructor, t); + Nan::HandleScope scope; + Local t = Nan::New(New); + stringprep_constructor.Reset(t); t->InstanceTemplate()->SetInternalFieldCount(1); - NODE_SET_PROTOTYPE_METHOD(t, "prepare", Prepare); + Nan::SetPrototypeMethod(t, "prepare", Prepare); - target->Set(NanNew("StringPrep"), t->GetFunction()); + Nan::Set(target, Nan::New("StringPrep").ToLocalChecked(), t->GetFunction()); } bool good() const @@ -43,11 +43,9 @@ class StringPrep : public ObjectWrap { static NAN_METHOD(New) { - NanScope(); - - if (args.Length() >= 1 && args[0]->IsString()) + if (info.Length() >= 1 && info[0]->IsString()) { - String::Utf8Value arg0(args[0]->ToString()); + Nan::Utf8String arg0(info[0]->ToString()); UStringPrepProfileType profileType; try { @@ -55,27 +53,28 @@ class StringPrep : public ObjectWrap { } catch (UnknownProfileException &) { - NanThrowTypeError("Unknown StringPrep profile"); - NanReturnUndefined(); + Nan::ThrowTypeError("Unknown StringPrep profile"); + return; } StringPrep *self = new StringPrep(profileType); if (self->good()) { - self->Wrap(args.This()); - NanReturnValue(args.This()); + self->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); + return; } else { const char* err = self->errorName(); delete self; - NanThrowError(err); - NanReturnUndefined(); + Nan::ThrowError(err); + return; } } else { - NanThrowTypeError("Bad argument."); - NanReturnUndefined(); + Nan::ThrowTypeError("Bad argument."); + return; } } @@ -97,27 +96,27 @@ class StringPrep : public ObjectWrap { static NAN_METHOD(Prepare) { - NanScope(); - - if (args.Length() >= 1 && args[0]->IsString()) + if (info.Length() >= 1 && info[0]->IsString()) { - StringPrep *self = ObjectWrap::Unwrap(args.This()); - String::Value arg0(args[0]->ToString()); - NanReturnValue(self->prepare(arg0)); + StringPrep *self = Nan::ObjectWrap::Unwrap(info.This()); + String::Value arg0(info[0]->ToString()); + info.GetReturnValue().Set(self->prepare(arg0)); + return; } else { - NanThrowTypeError("Bad argument."); - NanReturnUndefined(); + Nan::ThrowTypeError("Bad argument."); + return; } } - Handle prepare(String::Value &str) + Local prepare(String::Value &str) { + Nan::EscapableHandleScope scope size_t destLen = str.length() + 1; UChar *dest = NULL; while(!dest) { - error = U_ZERO_ERROR; + UErrorCode error = U_ZERO_ERROR; dest = new UChar[destLen]; size_t w = usprep_prepare(profile, *str, str.length(), @@ -131,27 +130,26 @@ class StringPrep : public ObjectWrap { delete[] dest; dest = NULL; } - else if (!good()) + else if (!U_SUCCESS(error)) { // other error, just bail out delete[] dest; - NanThrowError(errorName()); - return NanUndefined(); + Nan::ThrowTypeError(u_errorName(error)); + return scope.Escape(Nan::Undefined()); } else destLen = w; } - Local result = NanNew(dest, destLen); delete[] dest; - return result; + return scope.Escape(Nan::New(dest, destLen).ToLocalChecked()); } private: UStringPrepProfile *profile; UErrorCode error; - static enum UStringPrepProfileType parseProfileType(String::Utf8Value &profile) + static enum UStringPrepProfileType parseProfileType(Nan::Utf8String &profile) throw(UnknownProfileException) { if (strcasecmp(*profile, "nameprep") == 0) @@ -192,12 +190,10 @@ class StringPrep : public ObjectWrap { NAN_METHOD(ToUnicode) { - NanScope(); - - if (args.Length() >= 2 && args[0]->IsString() && args[1]->IsInt32()) + if (info.Length() >= 2 && info[0]->IsString() && info[1]->IsInt32()) { - String::Value str(args[0]->ToString()); - int32_t options = args[1]->ToInt32()->Value(); + String::Value str(info[0]->ToString()); + int32_t options = info[1]->ToInt32()->Value(); // ASCII encoding (xn--*--*) should be longer than Unicode size_t destLen = str.length() + 1; @@ -210,7 +206,7 @@ NAN_METHOD(ToUnicode) dest, destLen, options, NULL, &error); - + if (error == U_BUFFER_OVERFLOW_ERROR) { // retry with a dest buffer twice as large @@ -222,31 +218,30 @@ NAN_METHOD(ToUnicode) { // other error, just bail out delete[] dest; - NanThrowError(u_errorName(error)); - NanReturnUndefined(); + Nan::ThrowError(u_errorName(error)); + return; } else destLen = w; } - Local result = NanNew(dest, destLen); + Local result = Nan::New(dest, destLen); delete[] dest; - NanReturnValue(result); + info.GetReturnValue().Set(result); + return; } else { - NanThrowTypeError("Bad argument."); - NanReturnUndefined(); + Nan::ThrowTypeError("Bad argument."); + return; } } NAN_METHOD(ToASCII) { - NanScope(); - - if (args.Length() >= 2 && args[0]->IsString() && args[1]->IsInt32()) + if (info.Length() >= 2 && info[0]->IsString() && info[1]->IsInt32()) { - String::Value str(args[0]->ToString()); - int32_t options = args[1]->ToInt32()->Value(); + String::Value str(info[0]->ToString()); + int32_t options = info[1]->ToInt32()->Value(); // find out length first UErrorCode error = U_ZERO_ERROR; @@ -270,28 +265,30 @@ NAN_METHOD(ToASCII) { // other error, just bail out delete[] dest; - NanThrowError(u_errorName(error)); - NanReturnUndefined(); + Nan::ThrowError(u_errorName(error)); + return; } - Local result = NanNew(dest, destLen); + Local result = Nan::New(dest, destLen); delete[] dest; - NanReturnValue(result); + info.GetReturnValue().Set(result); + return; } else { - NanThrowTypeError("Bad argument."); - NanReturnUndefined(); + Nan::ThrowTypeError("Bad argument."); + return; } } /*** Initialization ***/ -extern "C" void init(Handle target) -{ - NanScope(); - StringPrep::Initialize(target); - NODE_SET_METHOD(target, "toUnicode", ToUnicode); - NODE_SET_METHOD(target, "toASCII", ToASCII); +extern "C" { + static void init (Local target) + { + Nan::HandleScope scope; + StringPrep::Initialize(target); + Nan::SetMethod(target, "toUnicode", ToUnicode); + Nan::SetMethod(target, "toASCII", ToASCII); + } + NODE_MODULE(node_stringprep, init) } - -NODE_MODULE(node_stringprep, init) diff --git a/package.json b/package.json index f2664e4..5fb5730 100644 --- a/package.json +++ b/package.json @@ -12,17 +12,17 @@ "test": "./node_modules/.bin/grunt test" }, "dependencies": { - "bindings": "~1.2.1", - "debug": "~2.0.0", - "nan": "~1.8.4" + "bindings": "^1.2.1", + "debug": "^2.2.0", + "nan": "^2.0.9" }, "devDependencies": { - "grunt": "~0.4.2", + "grunt": "^0.4.2", "grunt-cli": "^0.1.13", - "grunt-contrib-jshint": "~0.7.2", - "grunt-mocha-cli": "~1.3.0", - "proxyquire": "~0.5.2", - "should": "~2.1.1" + "grunt-contrib-jshint": "^0.11.3", + "grunt-mocha-cli": "^1.14.0", + "proxyquire": "^1.7.2", + "should": "^7.1.0" }, "repository": { "type": "git", From 9f9b3d2df0b849b0e76a0e763cf64a5f1d9db4c2 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Tue, 29 Sep 2015 18:04:51 +0200 Subject: [PATCH 2/5] init --- node-stringprep.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/node-stringprep.cc b/node-stringprep.cc index a0f4cb8..6d40639 100644 --- a/node-stringprep.cc +++ b/node-stringprep.cc @@ -282,10 +282,8 @@ NAN_METHOD(ToASCII) /*** Initialization ***/ -extern "C" { - static void init (Local target) +NAN_MODULE_INIT(init) { - Nan::HandleScope scope; StringPrep::Initialize(target); Nan::SetMethod(target, "toUnicode", ToUnicode); Nan::SetMethod(target, "toASCII", ToASCII); From 1a3bbf13199de72a1e223a4b112c3a3e84d5cf0a Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Tue, 29 Sep 2015 18:10:17 +0200 Subject: [PATCH 3/5] f --- node-stringprep.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/node-stringprep.cc b/node-stringprep.cc index 6d40639..f413afc 100644 --- a/node-stringprep.cc +++ b/node-stringprep.cc @@ -283,10 +283,10 @@ NAN_METHOD(ToASCII) /*** Initialization ***/ NAN_MODULE_INIT(init) - { - StringPrep::Initialize(target); - Nan::SetMethod(target, "toUnicode", ToUnicode); - Nan::SetMethod(target, "toASCII", ToASCII); - } - NODE_MODULE(node_stringprep, init) +{ + StringPrep::Initialize(target); + Nan::SetMethod(target, "toUnicode", ToUnicode); + Nan::SetMethod(target, "toASCII", ToASCII); } + +NODE_MODULE(node_stringprep, init) From e199f5aee2ee339db2f99df638cdc343c561ae51 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Wed, 30 Sep 2015 10:55:27 +0200 Subject: [PATCH 4/5] f --- node-stringprep.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node-stringprep.cc b/node-stringprep.cc index f413afc..559d6f8 100644 --- a/node-stringprep.cc +++ b/node-stringprep.cc @@ -111,7 +111,7 @@ class StringPrep : public Nan::ObjectWrap { Local prepare(String::Value &str) { - Nan::EscapableHandleScope scope + Nan::EscapableHandleScope scope; size_t destLen = str.length() + 1; UChar *dest = NULL; while(!dest) From faffe6bf6dc5defbf48e7121dbeed5a7ce3b19cc Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Wed, 30 Sep 2015 10:57:36 +0200 Subject: [PATCH 5/5] f --- node-stringprep.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/node-stringprep.cc b/node-stringprep.cc index 559d6f8..55d14f8 100644 --- a/node-stringprep.cc +++ b/node-stringprep.cc @@ -225,7 +225,7 @@ NAN_METHOD(ToUnicode) destLen = w; } - Local result = Nan::New(dest, destLen); + Local result = Nan::New(dest, destLen).ToLocalChecked(); delete[] dest; info.GetReturnValue().Set(result); return; @@ -269,7 +269,7 @@ NAN_METHOD(ToASCII) return; } - Local result = Nan::New(dest, destLen); + Local result = Nan::New(dest, destLen).ToLocalChecked(); delete[] dest; info.GetReturnValue().Set(result); return;