Skip to content

Commit

Permalink
Initial work on port to N-API (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNaso authored and lovell committed Jun 19, 2019
1 parent 742f3fa commit 2db8ee6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 114 deletions.
11 changes: 8 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
'src/upstream/farmhash.cc',
'src/bindings.cc'
],
'include_dirs': [
'<!(node -e "require(\'nan\')")'
'include_dirs' : [
"<!@(node -p \"require('node-addon-api').include\")"
],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'cflags_cc': [
'-fexceptions',
'-Wall',
'-Ofast',
'-funroll-loops'
],
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'conditions': [
['OS=="win"', {
'defines': [
Expand All @@ -32,6 +34,9 @@
}]
],
'xcode_settings': {
"CLANG_CXX_LIBRARY": "libc++",
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'MACOSX_DEPLOYMENT_TARGET': '10.7',
'OTHER_CPLUSPLUSFLAGS': [
'-fexceptions',
'-Wall',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"fingerprint"
],
"dependencies": {
"nan": "^2.10.0",
"prebuild-install": "^2.5.3"
"prebuild-install": "^2.5.3",
"node-addon-api": "^1.2.0"
},
"devDependencies": {
"cc": "^1.0.2",
Expand Down
200 changes: 91 additions & 109 deletions src/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <node.h>
#include <nan.h>
#include <napi.h>
#include <sstream>

#include "upstream/farmhash.h"
Expand All @@ -28,149 +27,132 @@ std::string Uint64ToString(const T& t) {

// Hash methods - platform dependent

NAN_METHOD(Hash32Buffer) {
Nan::HandleScope();
v8::Local<v8::Object> buffer = info[0].As<v8::Object>();
uint32_t hash = util::Hash32(node::Buffer::Data(buffer), node::Buffer::Length(buffer));
info.GetReturnValue().Set(Nan::New<v8::Uint32>(hash));
Napi::Value Hash32Buffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
uint32_t hash = util::Hash32(buffer.Data(), buffer.ByteLength());
return Napi::Number::New(env, hash);
}

NAN_METHOD(Hash32String) {
Nan::HandleScope();
std::string input = *Nan::Utf8String(info[0]);
Napi::Value Hash32String(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string input = info[0].As<Napi::String>().Utf8Value();
uint32_t hash = util::Hash32(input);
info.GetReturnValue().Set(Nan::New<v8::Uint32>(hash));
return Napi::Number::New(env, hash);
}

NAN_METHOD(Hash32WithSeedBuffer) {
Nan::HandleScope();
v8::Local<v8::Object> buffer = info[0].As<v8::Object>();
uint32_t seed = Nan::To<uint32_t>(info[1]).FromJust();
uint32_t hash = util::Hash32WithSeed(node::Buffer::Data(buffer), node::Buffer::Length(buffer), seed);
info.GetReturnValue().Set(Nan::New<v8::Uint32>(hash));
Napi::Value Hash32WithSeedBuffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
uint32_t seed = info[1].As<Napi::Number>().Uint32Value();
uint32_t hash = util::Hash32WithSeed(buffer.Data(), buffer.ByteLength(), seed);
return Napi::Number::New(env, hash);
}

NAN_METHOD(Hash32WithSeedString) {
Nan::HandleScope();
std::string input = *Nan::Utf8String(info[0]);
uint32_t seed = Nan::To<uint32_t>(info[1]).FromJust();
Napi::Value Hash32WithSeedString(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string input = info[0].As<Napi::String>().Utf8Value();
uint32_t seed = info[1].As<Napi::Number>().Uint32Value();
uint32_t hash = util::Hash32WithSeed(input, seed);
info.GetReturnValue().Set(Nan::New<v8::Uint32>(hash));
return Napi::Number::New(env, hash);
}

NAN_METHOD(Hash64Buffer) {
Nan::HandleScope();
v8::Local<v8::Object> buffer = info[0].As<v8::Object>();
uint64_t hash = util::Hash64(node::Buffer::Data(buffer), node::Buffer::Length(buffer));
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
Napi::Value Hash64Buffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
uint64_t hash = util::Hash64(buffer.Data(), buffer.ByteLength());
return Napi::String::New(env, Uint64ToString(hash));
}

NAN_METHOD(Hash64String) {
Nan::HandleScope();
std::string input = *Nan::Utf8String(info[0]);
Napi::Value Hash64String(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string input = info[0].As<Napi::String>().Utf8Value();
uint64_t hash = util::Hash64(input);
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
return Napi::String::New(env, Uint64ToString(hash));
}

NAN_METHOD(Hash64WithSeedBuffer) {
Nan::HandleScope();
v8::Local<v8::Object> buffer = info[0].As<v8::Object>();
uint64_t seed = static_cast<uint64_t>(Nan::To<uint32_t>(info[1]).FromJust());
uint64_t hash = util::Hash64WithSeed(node::Buffer::Data(buffer), node::Buffer::Length(buffer), seed);
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
Napi::Value Hash64WithSeedBuffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
uint64_t seed = static_cast<uint64_t>(info[1].As<Napi::Number>().Uint32Value());
uint64_t hash = util::Hash64WithSeed(buffer.Data(), buffer.ByteLength(), seed);
return Napi::String::New(env, Uint64ToString(hash));
}

NAN_METHOD(Hash64WithSeedString) {
Nan::HandleScope();
std::string input = *Nan::Utf8String(info[0]);
uint64_t seed = static_cast<uint64_t>(Nan::To<uint32_t>(info[1]).FromJust());
Napi::Value Hash64WithSeedString(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string input = info[0].As<Napi::String>().Utf8Value();
uint64_t seed = static_cast<uint64_t>(info[1].As<Napi::Number>().Uint32Value());
uint64_t hash = util::Hash64WithSeed(input, seed);
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
return Napi::String::New(env, Uint64ToString(hash));
}

NAN_METHOD(Hash64WithSeedsBuffer) {
Nan::HandleScope();
v8::Local<v8::Object> buffer = info[0].As<v8::Object>();
uint64_t seed1 = static_cast<uint64_t>(Nan::To<uint32_t>(info[1]).FromJust());
uint64_t seed2 = static_cast<uint64_t>(Nan::To<uint32_t>(info[2]).FromJust());
uint64_t hash = util::Hash64WithSeeds(node::Buffer::Data(buffer), node::Buffer::Length(buffer), seed1, seed2);
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
Napi::Value Hash64WithSeedsBuffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
uint64_t seed1 = static_cast<uint64_t>(info[1].As<Napi::Number>().Uint32Value());
uint64_t seed2 = static_cast<uint64_t>(info[2].As<Napi::Number>().Uint32Value());
uint64_t hash = util::Hash64WithSeeds(buffer.Data(), buffer.ByteLength(), seed1, seed2);
return Napi::String::New(env, Uint64ToString(hash));
}

NAN_METHOD(Hash64WithSeedsString) {
Nan::HandleScope();
std::string input = *Nan::Utf8String(info[0]);
uint64_t seed1 = static_cast<uint64_t>(Nan::To<uint32_t>(info[1]).FromJust());
uint64_t seed2 = static_cast<uint64_t>(Nan::To<uint32_t>(info[2]).FromJust());
Napi::Value Hash64WithSeedsString(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string input = info[0].As<Napi::String>().Utf8Value();
uint64_t seed1 = static_cast<uint64_t>(info[1].As<Napi::Number>().Uint32Value());
uint64_t seed2 = static_cast<uint64_t>(info[2].As<Napi::Number>().Uint32Value());
uint64_t hash = util::Hash64WithSeeds(input, seed1, seed2);
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
return Napi::String::New(env, Uint64ToString(hash));
}

// Fingerprint methods - platform independent

NAN_METHOD(Fingerprint32Buffer) {
Nan::HandleScope();
v8::Local<v8::Object> buffer = info[0].As<v8::Object>();
uint32_t hash = util::Fingerprint32(node::Buffer::Data(buffer), node::Buffer::Length(buffer));
info.GetReturnValue().Set(Nan::New<v8::Uint32>(hash));
Napi::Value Fingerprint32Buffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
uint32_t hash = util::Fingerprint32(buffer.Data(), buffer.ByteLength());
return Napi::Number::New(env, hash);
}

NAN_METHOD(Fingerprint32String) {
Nan::HandleScope();
std::string input = *Nan::Utf8String(info[0]);
Napi::Value Fingerprint32String(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string input = info[0].As<Napi::String>().Utf8Value();
uint32_t hash = util::Fingerprint32(input);
info.GetReturnValue().Set(Nan::New<v8::Uint32>(hash));
return Napi::Number::New(env, hash);
}

NAN_METHOD(Fingerprint64Buffer) {
Nan::HandleScope();
v8::Local<v8::Object> buffer = info[0].As<v8::Object>();
uint64_t hash = util::Fingerprint64(node::Buffer::Data(buffer), node::Buffer::Length(buffer));
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
Napi::Value Fingerprint64Buffer(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Buffer<char> buffer = info[0].As<Napi::Buffer<char>>();
uint64_t hash = util::Fingerprint64(buffer.Data(), buffer.ByteLength());
return Napi::String::New(env, Uint64ToString(hash));
}

NAN_METHOD(Fingerprint64String) {
Nan::HandleScope();
std::string input = *Nan::Utf8String(info[0]);
Napi::Value Fingerprint64String(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string input = info[0].As<Napi::String>().Utf8Value();
uint64_t hash = util::Fingerprint64(input);
info.GetReturnValue().Set(Nan::New(Uint64ToString(hash)).ToLocalChecked());
return Napi::String::New(env, Uint64ToString(hash));
}

// Init

NAN_MODULE_INIT(init) {
Nan::Set(target, Nan::New("Hash32Buffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash32Buffer)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash32String").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash32String)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash32WithSeedBuffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash32WithSeedBuffer)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash32WithSeedString").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash32WithSeedString)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash64Buffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash64Buffer)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash64String").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash64String)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash64WithSeedBuffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash64WithSeedBuffer)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash64WithSeedString").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash64WithSeedString)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash64WithSeedsBuffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash64WithSeedsBuffer)).ToLocalChecked());
Nan::Set(target, Nan::New("Hash64WithSeedsString").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Hash64WithSeedsString)).ToLocalChecked());
Nan::Set(target, Nan::New("Fingerprint32Buffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Fingerprint32Buffer)).ToLocalChecked());
Nan::Set(target, Nan::New("Fingerprint32String").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Fingerprint32String)).ToLocalChecked());
Nan::Set(target, Nan::New("Fingerprint64Buffer").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Fingerprint64Buffer)).ToLocalChecked());
Nan::Set(target, Nan::New("Fingerprint64String").ToLocalChecked(),
Nan::GetFunction(Nan::New<v8::FunctionTemplate>(Fingerprint64String)).ToLocalChecked());
}

#ifdef FARMHASH_LEGACY
NODE_MODULE(farmhash_legacy, init)
#else
NODE_MODULE(farmhash, init)
#endif
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "Hash32Buffer"), Napi::Function::New(env, Hash32Buffer));
exports.Set(Napi::String::New(env, "Hash32String"), Napi::Function::New(env, Hash32String));
exports.Set(Napi::String::New(env, "Hash32WithSeedBuffer"), Napi::Function::New(env, Hash32WithSeedBuffer));
exports.Set(Napi::String::New(env, "Hash32WithSeedString"), Napi::Function::New(env, Hash32WithSeedString));
exports.Set(Napi::String::New(env, "Hash64Buffer"), Napi::Function::New(env, Hash64Buffer));
exports.Set(Napi::String::New(env, "Hash64String"), Napi::Function::New(env, Hash64String));
exports.Set(Napi::String::New(env, "Hash64WithSeedBuffer"), Napi::Function::New(env, Hash64WithSeedBuffer));
exports.Set(Napi::String::New(env, "Hash64WithSeedString"), Napi::Function::New(env, Hash64WithSeedString));
exports.Set(Napi::String::New(env, "Hash64WithSeedsBuffer"), Napi::Function::New(env, Hash64WithSeedsBuffer));
exports.Set(Napi::String::New(env, "Hash64WithSeedsString"), Napi::Function::New(env, Hash64WithSeedsString));
exports.Set(Napi::String::New(env, "Fingerprint32Buffer"), Napi::Function::New(env, Fingerprint32Buffer));
exports.Set(Napi::String::New(env, "Fingerprint32String"), Napi::Function::New(env, Fingerprint32String));
exports.Set(Napi::String::New(env, "Fingerprint64Buffer"), Napi::Function::New(env, Fingerprint64Buffer));
exports.Set(Napi::String::New(env, "Fingerprint64String"), Napi::Function::New(env, Fingerprint64String));
return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)

0 comments on commit 2db8ee6

Please sign in to comment.