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

Complete initial port to N-API #57

Closed
wants to merge 5 commits 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
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.x",
"nan": "2.8.x",
"node-addon-api": "^1.2.0",
"prebuild-install": "^2.1.0"
},
"devDependencies": {
Expand Down
54 changes: 32 additions & 22 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,67 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) {
#include <sys/time.h>
#endif

void Now(const Nan::FunctionCallbackInfo<v8::Value> &info) {
// A very basic version of Node::ErrnoException since Napi doesn't expose it
Napi::Error ErrnoException(Napi::Env env, int errorno) {
Napi::Error e = Napi::Error::New(env, strerror(errorno));
e.Set("syscall", Napi::String::New(env, "gettimeofday"));
e.Set("errno", Napi::Number::New(env, errno));
// NOTE: in Node::ErrnoException this would be the string of the code
// like "EFAULT", just simplify with the number here.
e.Set("code", Napi::Number::New(env, errno));
return e;
}

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 ErrnoException(info.Env(), errno);
}

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 ErrnoException(info.Env(), errno);
}

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 ErrnoException(info.Env(), errno);
}

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);

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));
#if defined(_MSC_VER)
getSystemTime = (WinGetSystemTime)GetProcAddress(
GetModuleHandle(TEXT("kernel32.dll")), "GetSystemTimePreciseAsFileTime");
if (getSystemTime == NULL) {
getSystemTime = &GetSystemTimeAsFileTime;
}
#endif
return exports;
}

NODE_MODULE(microtime, InitAll)
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init);