diff --git a/binding.gyp b/binding.gyp index 5e8245c..481b8bb 100644 --- a/binding.gyp +++ b/binding.gyp @@ -5,7 +5,10 @@ "sources": [ "src/crc-64-jones.c", "src/Crc64.cc" - ] + ], + "include_dirs": [ + "", "licenses": [ @@ -22,10 +22,11 @@ "Parser" ], "dependencies": { - "stream-parser": "~0.1.0", - "lzf": "~0.1.3", - "int64-native": "~0.2.0", - "buffer-equal": "0.0.0" + "buffer-equal": "0.0.0", + "int64-native-node12": "^0.4.0", + "lzf": "git+https://github.com/kieut/node-lzf.git", + "nan": "^1.8.4", + "stream-parser": "~0.1.0" }, "devDependencies": { "mocha": "~1.11.0", diff --git a/src/Crc64.cc b/src/Crc64.cc index 84b49d7..3820151 100644 --- a/src/Crc64.cc +++ b/src/Crc64.cc @@ -12,75 +12,88 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include -#include - -#include "Crc64.h" +#include "nan.h" using namespace v8; using namespace node; + extern "C" uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l); -void Crc64::Init(Handle exports) { - Local tpl = FunctionTemplate::New(New); - tpl->SetClassName(String::NewSymbol("Crc64")); - tpl->InstanceTemplate()->SetInternalFieldCount(1); - tpl->PrototypeTemplate()->Set(String::NewSymbol("push"), FunctionTemplate::New(Push)->GetFunction()); - tpl->PrototypeTemplate()->Set(String::NewSymbol("value"), FunctionTemplate::New(GetValue)->GetFunction()); +class Crc64 : public ObjectWrap { - Persistent constructor = Persistent::New(tpl->GetFunction()); - exports->Set(String::NewSymbol("Crc64"), constructor); -} +public: -Crc64::Crc64() { - crc = 0; -} + static void Init(Handle exports) { + NanScope(); + Local tmpl = NanNew(Crc64::New); + + tmpl->SetClassName(NanNew("Crc64")); + tmpl->InstanceTemplate()->SetInternalFieldCount(1); -Crc64::~Crc64() {} + NODE_SET_PROTOTYPE_METHOD(tmpl, "push", Push); + NODE_SET_PROTOTYPE_METHOD(tmpl, "value", GetValue); -Handle Crc64::New(const Arguments& args) { - if (args.Length() != 0) { - return ThrowException(Exception::Error(String::New("Unexpected arguments"))); - } + NanAssignPersistent(functionTemplate, tmpl); + NanAssignPersistent(constructor, tmpl->GetFunction()); + exports->Set(NanNew("Crc64"), tmpl->GetFunction()); + } + +private: + + Crc64() { + crc = 0; + } - HandleScope scope; - Crc64* obj = new Crc64(); - obj->Wrap(args.This()); - return args.This(); -} + ~Crc64() { + } -Handle Crc64::Push(const Arguments& args) { - if (args.Length() != 1 || !Buffer::HasInstance(args[0])) { - return ThrowException(Exception::Error(String::New("Expecting a single Buffer argument"))); - } + static NAN_METHOD(New) { + NanScope(); + Crc64* instance = new Crc64(); + instance->Wrap(args.This()); + NanReturnThis(); + } - HandleScope scope; - Crc64* obj = ObjectWrap::Unwrap(args.This()); + static NAN_METHOD(Push) { + NanScope(); + if (args.Length() != 1 || !Buffer::HasInstance(args[0])) + return NanThrowError("Expecting a single Buffer argument"); + + Crc64* instance = ObjectWrap::Unwrap(args.Holder()); Local bytes = args[0]->ToObject(); + instance->crc = crc64(instance->crc, (unsigned char *)Buffer::Data(bytes), Buffer::Length(bytes)); + NanReturnUndefined(); + } - obj->crc = crc64(obj->crc, (unsigned char *)Buffer::Data(bytes), Buffer::Length(bytes)); + static NAN_METHOD(GetValue) { + NanScope(); + if (args.Length() != 0) + return NanThrowError("Unexpected arguments"); - return Undefined(); -} + Crc64* instance = ObjectWrap::Unwrap(args.Holder()); + Local BufferOut = NanNewBufferHandle((char*)&(instance->crc), sizeof(uint64_t)); + NanReturnValue(BufferOut); + } -Handle Crc64::GetValue(const Arguments& args) { - if (args.Length() != 0) { - return ThrowException(Exception::Error(String::New("Unexpected arguments"))); - } + static Persistent functionTemplate; + static Persistent constructor; - HandleScope scope; - Crc64* obj = ObjectWrap::Unwrap(args.This()); - Buffer* BufferOut = Buffer::New((char*)&(obj->crc), sizeof(uint64_t)); - return scope.Close(BufferOut->handle_); -} + uint64_t crc; +}; -extern "C" -void init(Handle exports) { + +Persistent Crc64::functionTemplate; +Persistent Crc64::constructor; + + +extern "C" { + + static void init(Handle exports) { Crc64::Init(exports); -} + } -NODE_MODULE(Crc64, init) + NODE_MODULE(Crc64, init) +}; diff --git a/src/Crc64.h b/src/Crc64.h deleted file mode 100644 index f77a4e1..0000000 --- a/src/Crc64.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2013 Danny Yates - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 - -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef CRC64_H -#define CRC64_H - -#include -#include - -using namespace node; -using namespace v8; - -class Crc64 : public ObjectWrap { - public: - static void Init(Handle exports); - - private: - Crc64(); - ~Crc64(); - - static Handle New(const Arguments& args); - static Handle Push(const Arguments& args); - static Handle GetValue(const Arguments& args); - - uint64_t crc; -}; - -#endif