Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade xxhash to use Nan>=2.0, add unit test #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
node_modules
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
{ "name": "xxhash",
"version": "0.2.2",
"version": "0.2.3",
"author": "Brian White <[email protected]>",
"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" }
}
"repository": { "type": "git", "url": "https://github.com/mscdex/node-xxhash.git" },
"scripts": {
"test": "nodeunit test"
}
}
84 changes: 38 additions & 46 deletions src/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

using namespace v8;

static Persistent<FunctionTemplate> constructor;
static Nan::Persistent<FunctionTemplate> constructor;

class Hash : public node::ObjectWrap {
public:
Expand All @@ -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<Hash>(args.This());
Hash* obj = ObjectWrap::Unwrap<Hash>(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<Object> data = args[0]->ToObject();
Local<Object> data = info[0]->ToObject();
#else
Local<Value> data = args[0];
Local<Value> 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<Hash>(args.This());
Hash* obj = ObjectWrap::Unwrap<Hash>(info.This());

uint32_t result = XXH32_digest(&obj->state);

NanReturnValue(NanNew<Integer>(result));
info.GetReturnValue().Set(Nan::New<Integer>(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<Object> data = args[0]->ToObject();
Local<Object> data = info[0]->ToObject();
#else
Local<Value> data = args[0];
Local<Value> 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<Integer>(result));
info.GetReturnValue().Set(Nan::New<Integer>(result));
}


static void Initialize(Handle<Object> target) {
NanScope();

Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);

NanAssignPersistent(constructor, tpl);
constructor.Reset(tpl);
tpl->InstanceTemplate()->SetInternalFieldCount(1);
tpl->SetClassName(NanNew<String>("XXHash"));
tpl->SetClassName(Nan::New<String>("XXHash").ToLocalChecked());

NODE_SET_PROTOTYPE_METHOD(tpl, "update", Update);
NODE_SET_PROTOTYPE_METHOD(tpl, "digest", Digest);
tpl->Set(NanNew<String>("hash"),
NanNew<FunctionTemplate>(StaticHash)->GetFunction());
Nan::SetPrototypeMethod(tpl, "update", Update);
Nan::SetPrototypeMethod(tpl, "digest", Digest);

target->Set(NanNew<String>("XXHash"), tpl->GetFunction());
tpl->Set(Nan::New<String>("hash").ToLocalChecked(),
Nan::New<FunctionTemplate>(StaticHash)->GetFunction());
target->Set(Nan::New<String>("XXHash").ToLocalChecked(),
tpl->GetFunction());
}
};

extern "C" {
void Init(Handle<Object> target) {
NanScope();
Hash::Initialize(target);
}

NODE_MODULE(hash, Init);
}
}
7 changes: 7 additions & 0 deletions test/test_xxhash.js
Original file line number Diff line number Diff line change
@@ -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()
}