src: deduplicate CHECK_EQ/CHECK_NE macros
[platform/upstream/nodejs.git] / src / signal_wrap.cc
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22 #include "async-wrap.h"
23 #include "async-wrap-inl.h"
24 #include "env.h"
25 #include "env-inl.h"
26 #include "handle_wrap.h"
27 #include "util.h"
28 #include "util-inl.h"
29 #include "v8.h"
30
31 namespace node {
32
33 using v8::Context;
34 using v8::Function;
35 using v8::FunctionCallbackInfo;
36 using v8::FunctionTemplate;
37 using v8::Handle;
38 using v8::HandleScope;
39 using v8::Integer;
40 using v8::Local;
41 using v8::Object;
42 using v8::Value;
43
44 class SignalWrap : public HandleWrap {
45  public:
46   static void Initialize(Handle<Object> target,
47                          Handle<Value> unused,
48                          Handle<Context> context) {
49     Environment* env = Environment::GetCurrent(context);
50     Local<FunctionTemplate> constructor = FunctionTemplate::New(env->isolate(),
51                                                                 New);
52     constructor->InstanceTemplate()->SetInternalFieldCount(1);
53     constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"));
54
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);
60
61     target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"),
62                 constructor->GetFunction());
63   }
64
65  private:
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
69     // normal function.
70     assert(args.IsConstructCall());
71     HandleScope handle_scope(args.GetIsolate());
72     Environment* env = Environment::GetCurrent(args.GetIsolate());
73     new SignalWrap(env, args.This());
74   }
75
76   SignalWrap(Environment* env, Handle<Object> object)
77       : HandleWrap(env,
78                    object,
79                    reinterpret_cast<uv_handle_t*>(&handle_),
80                    AsyncWrap::PROVIDER_SIGNALWRAP) {
81     int r = uv_signal_init(env->event_loop(), &handle_);
82     assert(r == 0);
83   }
84
85   ~SignalWrap() {
86   }
87
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());
92
93     int signum = args[0]->Int32Value();
94     int err = uv_signal_start(&wrap->handle_, OnSignal, signum);
95     args.GetReturnValue().Set(err);
96   }
97
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());
102
103     int err = uv_signal_stop(&wrap->handle_);
104     args.GetReturnValue().Set(err);
105   }
106
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());
112
113     Local<Value> arg = Integer::New(env->isolate(), signum);
114     wrap->MakeCallback(env->onsignal_string(), 1, &arg);
115   }
116
117   uv_signal_t handle_;
118 };
119
120
121 }  // namespace node
122
123
124 NODE_MODULE_CONTEXT_AWARE_BUILTIN(signal_wrap, node::SignalWrap::Initialize)