From acef2de9b368a3c18c4abe6089d18dbcee235ad4 Mon Sep 17 00:00:00 2001 From: Nathan Lawrence Date: Tue, 25 Aug 2015 14:53:49 -0700 Subject: [PATCH] Upgrade xxhash to use Nan>=2.0, add tests --- .gitignore | 2 ++ package.json | 14 +++++--- src/hash.cc | 84 ++++++++++++++++++++------------------------- test/test_xxhash.js | 7 ++++ 4 files changed, 57 insertions(+), 50 deletions(-) create mode 100644 .gitignore create mode 100644 test/test_xxhash.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e3fbd98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +node_modules diff --git a/package.json b/package.json index f98fcf4..a692cdd 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,21 @@ { "name": "xxhash", - "version": "0.2.2", + "version": "0.2.3", "author": "Brian White ", "description": "An xxhash binding for node.js", "main": "./lib/xxhash", "dependencies": { - "nan": "^1.8.4", + "nan": "^2.0.5", "readable-stream": "~1.0.0" }, + "devDependencies": { + "nodeunit": "0.8.1" + }, "scripts": { "install": "node-gyp rebuild" }, "engines": { "node" : ">=0.6.0" }, "keywords": [ "hash", "xxhash", "fast", "streaming" ], "licenses": [ { "type": "MIT", "url": "https://raw.github.com/mscdex/node-xxhash/master/LICENSE" } ], - "repository": { "type": "git", "url": "https://github.com/mscdex/node-xxhash.git" } -} \ No newline at end of file + "repository": { "type": "git", "url": "https://github.com/mscdex/node-xxhash.git" }, + "scripts": { + "test": "nodeunit test" + } +} diff --git a/src/hash.cc b/src/hash.cc index 6ecf024..ebd7e4e 100644 --- a/src/hash.cc +++ b/src/hash.cc @@ -7,7 +7,7 @@ using namespace v8; -static Persistent constructor; +static Nan::Persistent constructor; class Hash : public node::ObjectWrap { public: @@ -21,103 +21,95 @@ class Hash : public node::ObjectWrap { } static NAN_METHOD(New) { - NanScope(); + if (!info.IsConstructCall()) + return Nan::ThrowError("Use `new` to create instances of this object."); - if (!args.IsConstructCall()) - return NanThrowError("Use `new` to create instances of this object."); + if (info.Length() == 0 || !info[0]->IsUint32()) + return Nan::ThrowTypeError("Expected unsigned integer seed argument"); - if (args.Length() == 0 || !args[0]->IsUint32()) - return NanThrowTypeError("Expected unsigned integer seed argument"); + Hash* obj = new Hash(info[0]->Uint32Value()); + obj->Wrap(info.This()); - Hash* obj = new Hash(args[0]->Uint32Value()); - obj->Wrap(args.This()); - - NanReturnValue(args.This()); + info.GetReturnValue().Set(info.This()); } static NAN_METHOD(Update) { - NanScope(); - Hash* obj = ObjectWrap::Unwrap(args.This()); + Hash* obj = ObjectWrap::Unwrap(info.This()); - if (!node::Buffer::HasInstance(args[0])) - return NanThrowTypeError("data argument must be a Buffer"); + if (!node::Buffer::HasInstance(info[0])) + return Nan::ThrowTypeError("data argument must be a Buffer"); #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 10 - Local data = args[0]->ToObject(); + Local data = info[0]->ToObject(); #else - Local data = args[0]; + Local data = info[0]; #endif size_t buflen = node::Buffer::Length(data); /*if (buflen > 2147483647 || buflen == 0) - return NanThrowTypeError("data length must be 0 < n <= 2147483647");*/ + return Nan::ThrowTypeError("data length must be 0 < n <= 2147483647");*/ XXH32_update(&obj->state, node::Buffer::Data(data), buflen); - NanReturnUndefined(); + info.GetReturnValue().SetUndefined(); } static NAN_METHOD(Digest) { - NanScope(); - Hash* obj = ObjectWrap::Unwrap(args.This()); + Hash* obj = ObjectWrap::Unwrap(info.This()); uint32_t result = XXH32_digest(&obj->state); - NanReturnValue(NanNew(result)); + info.GetReturnValue().Set(Nan::New(result)); } static NAN_METHOD(StaticHash) { - NanScope(); - - if (args.Length() < 2) - return NanThrowTypeError("Expected data and seed arguments"); + if (info.Length() < 2) + return Nan::ThrowTypeError("Expected data and seed arguments"); - if (!node::Buffer::HasInstance(args[0])) - return NanThrowTypeError("data argument must be a Buffer"); - else if (!args[1]->IsUint32()) - return NanThrowTypeError("seed argument must be an unsigned integer"); + if (!node::Buffer::HasInstance(info[0])) + return Nan::ThrowTypeError("data argument must be a Buffer"); + else if (!info[1]->IsUint32()) + return Nan::ThrowTypeError("seed argument must be an unsigned integer"); #if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION < 10 - Local data = args[0]->ToObject(); + Local data = info[0]->ToObject(); #else - Local data = args[0]; + Local data = info[0]; #endif size_t buflen = node::Buffer::Length(data); /*if (buflen > 2147483647 || buflen == 0) - return NanThrowTypeError("data length must be 0 < n <= 2147483647");*/ + return Nan::ThrowTypeError("data length must be 0 < n <= 2147483647");*/ uint32_t result = XXH32(node::Buffer::Data(data), buflen, - args[1]->Uint32Value()); + info[1]->Uint32Value()); - NanReturnValue(NanNew(result)); + info.GetReturnValue().Set(Nan::New(result)); } static void Initialize(Handle target) { - NanScope(); - - Local tpl = NanNew(New); + Local tpl = Nan::New(New); - NanAssignPersistent(constructor, tpl); + constructor.Reset(tpl); tpl->InstanceTemplate()->SetInternalFieldCount(1); - tpl->SetClassName(NanNew("XXHash")); + tpl->SetClassName(Nan::New("XXHash").ToLocalChecked()); - NODE_SET_PROTOTYPE_METHOD(tpl, "update", Update); - NODE_SET_PROTOTYPE_METHOD(tpl, "digest", Digest); - tpl->Set(NanNew("hash"), - NanNew(StaticHash)->GetFunction()); + Nan::SetPrototypeMethod(tpl, "update", Update); + Nan::SetPrototypeMethod(tpl, "digest", Digest); - target->Set(NanNew("XXHash"), tpl->GetFunction()); + tpl->Set(Nan::New("hash").ToLocalChecked(), + Nan::New(StaticHash)->GetFunction()); + target->Set(Nan::New("XXHash").ToLocalChecked(), + tpl->GetFunction()); } }; extern "C" { void Init(Handle target) { - NanScope(); Hash::Initialize(target); } NODE_MODULE(hash, Init); -} \ No newline at end of file +} diff --git a/test/test_xxhash.js b/test/test_xxhash.js new file mode 100644 index 0000000..5e3d965 --- /dev/null +++ b/test/test_xxhash.js @@ -0,0 +1,7 @@ +var xxhash = require('../lib/xxhash') + +exports.testXXHash = function (test) { + var hash = xxhash.hash(new Buffer('hello'), 0xDEADBEEF) + test.equal(hash, '2717969635') + test.done() +}