1
// Copyright Joyent, Inc. and other Node contributors.
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
23
#include "handle_wrap.h"
33
using v8::HandleScope;
34
using v8::FunctionTemplate;
42
static Persistent<String> onsignal_sym;
45
class SignalWrap : public HandleWrap {
47
static void Initialize(Handle<Object> target) {
50
HandleWrap::Initialize(target);
52
Local<FunctionTemplate> constructor = FunctionTemplate::New(New);
53
constructor->InstanceTemplate()->SetInternalFieldCount(1);
54
constructor->SetClassName(String::NewSymbol("Signal"));
56
NODE_SET_PROTOTYPE_METHOD(constructor, "close", HandleWrap::Close);
57
NODE_SET_PROTOTYPE_METHOD(constructor, "ref", HandleWrap::Ref);
58
NODE_SET_PROTOTYPE_METHOD(constructor, "unref", HandleWrap::Unref);
59
NODE_SET_PROTOTYPE_METHOD(constructor, "start", Start);
60
NODE_SET_PROTOTYPE_METHOD(constructor, "stop", Stop);
62
onsignal_sym = NODE_PSYMBOL("onsignal");
64
target->Set(String::NewSymbol("Signal"), constructor->GetFunction());
68
static Handle<Value> New(const Arguments& args) {
69
// This constructor should not be exposed to public javascript.
70
// Therefore we assert that we are not trying to call this as a
72
assert(args.IsConstructCall());
75
new SignalWrap(args.This());
77
return scope.Close(args.This());
80
SignalWrap(Handle<Object> object)
81
: HandleWrap(object, reinterpret_cast<uv_handle_t*>(&handle_)) {
82
int r = uv_signal_init(uv_default_loop(), &handle_);
89
static Handle<Value> Start(const Arguments& args) {
94
int signum = args[0]->Int32Value();
96
int r = uv_signal_start(&wrap->handle_, OnSignal, signum);
98
if (r) SetErrno(uv_last_error(uv_default_loop()));
100
return scope.Close(Integer::New(r));
103
static Handle<Value> Stop(const Arguments& args) {
108
int r = uv_signal_stop(&wrap->handle_);
110
if (r) SetErrno(uv_last_error(uv_default_loop()));
112
return scope.Close(Integer::New(r));
115
static void OnSignal(uv_signal_t* handle, int signum) {
118
SignalWrap* wrap = container_of(handle, SignalWrap, handle_);
121
Local<Value> argv[1] = { Integer::New(signum) };
122
MakeCallback(wrap->object_, onsignal_sym, ARRAY_SIZE(argv), argv);
132
NODE_MODULE(node_signal_wrap, node::SignalWrap::Initialize)