diff --git a/binding.gyp b/binding.gyp index d6c8903..fb8d034 100644 --- a/binding.gyp +++ b/binding.gyp @@ -3,7 +3,26 @@ { 'target_name': 'microtime', 'sources': [ 'src/microtime.cc' ], - 'include_dirs' : [ '= 0.8.0" + "node": ">= 4.0.0" }, "scripts": { "install": "prebuild-install || node-gyp rebuild", @@ -25,7 +25,7 @@ }, "dependencies": { "bindings": "^1.3.1", - "nan": "2.12.x", + "node-addon-api": "^1.2.0", "prebuild-install": "^5.2.2" }, "devDependencies": { diff --git a/src/microtime.cc b/src/microtime.cc index 8d0b7a8..6085af3 100644 --- a/src/microtime.cc +++ b/src/microtime.cc @@ -6,7 +6,7 @@ #include -#include +#include #if defined(_MSC_VER) #include @@ -39,57 +39,48 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) { #include #endif -void Now(const Nan::FunctionCallbackInfo &info) { +Napi::Value Now(const Napi::CallbackInfo& info) { timeval t; int r = gettimeofday(&t, NULL); if (r < 0) { - return Nan::ThrowError(Nan::ErrnoException(errno, "gettimeofday")); + throw Napi::Error::New(info.Env(), "gettimeofday"); } - info.GetReturnValue().Set( - Nan::New((t.tv_sec * 1000000.0) + t.tv_usec)); + return Napi::Number::New(info.Env(), ((t.tv_sec * 1000000.0) + t.tv_usec)); } -void NowDouble(const Nan::FunctionCallbackInfo &info) { +Napi::Value NowDouble(const Napi::CallbackInfo& info) { timeval t; int r = gettimeofday(&t, NULL); if (r < 0) { - return Nan::ThrowError(Nan::ErrnoException(errno, "gettimeofday")); + throw Napi::Error::New(info.Env(), "gettimeofday"); } - info.GetReturnValue().Set( - Nan::New(t.tv_sec + (t.tv_usec * 0.000001))); + return Napi::Number::New(info.Env(), t.tv_sec + (t.tv_usec * 0.000001)); } -void NowStruct(const Nan::FunctionCallbackInfo &info) { +Napi::Value NowStruct(const Napi::CallbackInfo& info) { timeval t; int r = gettimeofday(&t, NULL); if (r < 0) { - return Nan::ThrowError(Nan::ErrnoException(errno, "gettimeofday")); + throw Napi::Error::New(info.Env(), "gettimeofday"); } - v8::Local array = Nan::New(2); - array->Set(Nan::New(0), Nan::New((double)t.tv_sec)); - array->Set(Nan::New(1), Nan::New((double)t.tv_usec)); + Napi::Array array = Napi::Array::New(info.Env(), 2); + array.Set((uint32_t)0, (double)t.tv_sec); + array.Set((uint32_t)1, (double)t.tv_usec); - info.GetReturnValue().Set(array); + return array; } -NAN_MODULE_INIT(InitAll) { - Nan::Export(target, "now", Now); - Nan::Export(target, "nowDouble", NowDouble); - Nan::Export(target, "nowStruct", NowStruct); - -#if defined(_MSC_VER) - getSystemTime = (WinGetSystemTime)GetProcAddress( - GetModuleHandle(TEXT("kernel32.dll")), "GetSystemTimePreciseAsFileTime"); - if (getSystemTime == NULL) { - getSystemTime = &GetSystemTimeAsFileTime; - } -#endif +Napi::Object Init(Napi::Env env, Napi::Object exports) { + exports.Set(Napi::String::New(env, "now"), Napi::Function::New(env, Now)); + exports.Set(Napi::String::New(env, "nowDouble"), Napi::Function::New(env, NowDouble)); + exports.Set(Napi::String::New(env, "nowStruct"), Napi::Function::New(env, NowStruct)); + return exports; } -NODE_MODULE(microtime, InitAll) +NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init); \ No newline at end of file