Skip to content

Commit

Permalink
Complete initial port to N-API
Browse files Browse the repository at this point in the history
  • Loading branch information
NickNaso authored and wadey committed Jan 28, 2019
1 parent 9845b5b commit f770009
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
21 changes: 20 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@
{
'target_name': 'microtime',
'sources': [ 'src/microtime.cc' ],
'include_dirs' : [ '<!(node -e "require(\'nan\')")' ]
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
'include_dirs': ["<!@(node -p \"require('node-addon-api').include\")"],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'conditions': [
['OS=="win"', {
"msvs_settings": {
"VCCLCompilerTool": {
"ExceptionHandling": 1
}
}
}],
['OS=="mac"', {
"xcode_settings": {
"CLANG_CXX_LIBRARY": "libc++",
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'MACOSX_DEPLOYMENT_TARGET': '10.7'
}
}]
]
}
]
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"gettimeofday"
],
"engines": {
"node": ">= 0.8.0"
"node": ">= 4.0.0"
},
"scripts": {
"install": "prebuild-install || node-gyp rebuild",
Expand All @@ -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": {
Expand Down
47 changes: 19 additions & 28 deletions src/microtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <errno.h>

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

#if defined(_MSC_VER)
#include <time.h>
Expand Down Expand Up @@ -39,57 +39,48 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) {
#include <sys/time.h>
#endif

void Now(const Nan::FunctionCallbackInfo<v8::Value> &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<v8::Number>((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<v8::Value> &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<v8::Number>(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<v8::Value> &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<v8::Array> array = Nan::New<v8::Array>(2);
array->Set(Nan::New<v8::Integer>(0), Nan::New<v8::Number>((double)t.tv_sec));
array->Set(Nan::New<v8::Integer>(1), Nan::New<v8::Number>((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);

0 comments on commit f770009

Please sign in to comment.