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.
22 #include "async-wrap.h"
23 #include "async-wrap-inl.h"
26 #include "handle_wrap.h"
35 using v8::FunctionCallbackInfo;
36 using v8::FunctionTemplate;
38 using v8::HandleScope;
44 class SignalWrap : public HandleWrap {
46 static void Initialize(Handle<Object> target,
48 Handle<Context> context) {
49 Environment* env = Environment::GetCurrent(context);
50 Local<FunctionTemplate> constructor = FunctionTemplate::New(env->isolate(),
52 constructor->InstanceTemplate()->SetInternalFieldCount(1);
53 constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"));
55 NODE_SET_PROTOTYPE_METHOD(constructor, "close", HandleWrap::Close);
56 NODE_SET_PROTOTYPE_METHOD(constructor, "ref", HandleWrap::Ref);
57 NODE_SET_PROTOTYPE_METHOD(constructor, "unref", HandleWrap::Unref);
58 NODE_SET_PROTOTYPE_METHOD(constructor, "start", Start);
59 NODE_SET_PROTOTYPE_METHOD(constructor, "stop", Stop);
61 target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"),
62 constructor->GetFunction());
66 static void New(const FunctionCallbackInfo<Value>& args) {
67 // This constructor should not be exposed to public javascript.
68 // Therefore we assert that we are not trying to call this as a
70 assert(args.IsConstructCall());
71 HandleScope handle_scope(args.GetIsolate());
72 Environment* env = Environment::GetCurrent(args.GetIsolate());
73 new SignalWrap(env, args.This());
76 SignalWrap(Environment* env, Handle<Object> object)
79 reinterpret_cast<uv_handle_t*>(&handle_),
80 AsyncWrap::PROVIDER_SIGNALWRAP) {
81 int r = uv_signal_init(env->event_loop(), &handle_);
88 static void Start(const FunctionCallbackInfo<Value>& args) {
89 Environment* env = Environment::GetCurrent(args.GetIsolate());
90 HandleScope scope(env->isolate());
91 SignalWrap* wrap = Unwrap<SignalWrap>(args.This());
93 int signum = args[0]->Int32Value();
94 int err = uv_signal_start(&wrap->handle_, OnSignal, signum);
95 args.GetReturnValue().Set(err);
98 static void Stop(const FunctionCallbackInfo<Value>& args) {
99 Environment* env = Environment::GetCurrent(args.GetIsolate());
100 HandleScope scope(env->isolate());
101 SignalWrap* wrap = Unwrap<SignalWrap>(args.This());
103 int err = uv_signal_stop(&wrap->handle_);
104 args.GetReturnValue().Set(err);
107 static void OnSignal(uv_signal_t* handle, int signum) {
108 SignalWrap* wrap = CONTAINER_OF(handle, SignalWrap, handle_);
109 Environment* env = wrap->env();
110 HandleScope handle_scope(env->isolate());
111 Context::Scope context_scope(env->context());
113 Local<Value> arg = Integer::New(env->isolate(), signum);
114 wrap->MakeCallback(env->onsignal_string(), 1, &arg);
124 NODE_MODULE_CONTEXT_AWARE_BUILTIN(signal_wrap, node::SignalWrap::Initialize)