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

add node-addon-api support #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: node_js

node_js:
- "12"
- "10"
- "8"
- "6"
- "4"

os:
- linux
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ environment:
NODE_PRE_GYP_GITHUB_TOKEN:
secure: I1J9L1ylzwS2WgQh67uUdYifKjCLGL7CibDFxpUgEjT9sNBsyjpljByCQw6jHt2F
matrix:
- nodejs_version: "12"
- nodejs_version: "10"
- nodejs_version: "9"
- nodejs_version: "8"
- nodejs_version: "6"
- nodejs_version: "4"

platform:
- x64
Expand Down
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"product_extension": "node",
"defines": [ "V8_DEPRECATION_WARNINGS=1" ],
"include_dirs": [
"<!(node -e require('nan'))",
"<!@(node -p \"require('node-addon-api').include\")",
"src/windows-kill-library/"
],
"libraries": [ "-lDbghelp" ],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
]
},
"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"dependencies": {
"nan": "^2.10.0",
"node-addon-api": "*",
"node-pre-gyp": "=0.9.1"
},
"devDependencies": {
Expand Down
58 changes: 29 additions & 29 deletions src/node-windows-kill.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
#include <nan.h>
#include <napi.h>
#include <stdexcept>
#include "windows-kill-library.h"

using WindowsKillLibrary::sendSignal;
using WindowsKillLibrary::warmUp;
using WindowsKillLibrary::SIGNAL_TYPE_CTRL_C;
using WindowsKillLibrary::SIGNAL_TYPE_CTRL_BREAK;

#define NODEWINDOWSKILL_VERSION "0.3.0"

void send(const Nan::FunctionCallbackInfo<v8::Value>& info) {
Napi::Value SendSig(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (info.Length() < 2) {
Nan::ThrowTypeError("Wrong number of arguments");
return;
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}

if (!info[0]->IsNumber() || !info[1]->IsNumber()) {
Nan::ThrowTypeError("Wrong arguments");
return;
if (!info[0].IsNumber() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "Wrong arguments").ThrowAsJavaScriptException();
return env.Null();
}

DWORD signal_pid = (DWORD)info[0]->NumberValue();
DWORD signal_type = (DWORD)info[1]->NumberValue();
DWORD signal_pid = (DWORD)info[0].As<Napi::Number>().Int32Value();
DWORD signal_type = (DWORD)info[1].As<Napi::Number>().Int32Value();

try {
if (signal_type == 1) {
Expand All @@ -30,48 +31,47 @@ void send(const Nan::FunctionCallbackInfo<v8::Value>& info) {
else {
sendSignal(signal_pid, SIGNAL_TYPE_CTRL_C);
}
info.GetReturnValue().Set(0);
// info.GetReturnValue().Set(0);
return Napi::Number::New(env,0);
}
catch (const std::invalid_argument& exception) {
if (strcmp(exception.what(), "ESRCH") == 0) {
info.GetReturnValue().Set(Nan::New<v8::String>("ESRCH").ToLocalChecked());
return;
return Napi::String::New(env, "ESRCH");
}
info.GetReturnValue().Set(Nan::New<v8::String>("ESYS").ToLocalChecked());
return Napi::String::New(env, "ESYS");
}
catch (const std::runtime_error& exception) {
if (strcmp(exception.what(), "EPERM") == 0) {
info.GetReturnValue().Set(Nan::New<v8::String>("EPERM").ToLocalChecked());
return;
return Napi::String::New(env, "EPERM");
}
info.GetReturnValue().Set(Nan::New<v8::String>("ESYS").ToLocalChecked());
return Napi::String::New(env, "ESYS");
}
catch (std::exception) {
info.GetReturnValue().Set(Nan::New<v8::String>("ESYS").ToLocalChecked());
return Napi::String::New(env, "ESYS");
}
}

void warmUp(const Nan::FunctionCallbackInfo<v8::Value>& info) {
Napi::Value WarmUp(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
try {
warmUp();
}
catch (const std::invalid_argument& exception) {
if (strcmp(exception.what(), "Invalid which argument.") == 0) {
info.GetReturnValue().Set(Nan::New<v8::String>("Invalid which argument.").ToLocalChecked());
return;
return Napi::String::New(env, "Invalid which argument.");
}
info.GetReturnValue().Set(Nan::New<v8::String>("ESYS").ToLocalChecked());
return Napi::String::New(env, "ESYS");
}
catch (std::exception) {
info.GetReturnValue().Set(Nan::New<v8::String>("ESYS").ToLocalChecked());
return Napi::String::New(env, "ESYS");
}
return env.Null();
}

void Init(v8::Local<v8::Object> exports) {
exports->Set(Nan::New("send").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(send)->GetFunction());
exports->Set(Nan::New("warmUp").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(warmUp)->GetFunction());
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "send"), Napi::Function::New(env, SendSig));
exports.Set(Napi::String::New(env, "warmUp"), Napi::Function::New(env, WarmUp));
return exports;
}

NODE_MODULE(windowskill, Init)
NODE_API_MODULE(windowskill, Init)