@@ -7,38 +7,46 @@ namespace node {
77using namespace v8 ;
88
99Persistent<FunctionTemplate> SignalWatcher::constructor_template;
10- static Persistent<String> signal_symbol ;
10+ static Persistent<String> callback_symbol ;
1111
1212void SignalWatcher::Initialize (Handle<Object> target) {
1313 HandleScope scope;
1414
1515 Local<FunctionTemplate> t = FunctionTemplate::New (SignalWatcher::New);
1616 constructor_template = Persistent<FunctionTemplate>::New (t);
17- constructor_template->Inherit (EventEmitter::constructor_template);
1817 constructor_template->InstanceTemplate ()->SetInternalFieldCount (1 );
1918 constructor_template->SetClassName (String::NewSymbol (" SignalWatcher" ));
2019
20+ NODE_SET_PROTOTYPE_METHOD (constructor_template, " start" , SignalWatcher::Start);
2121 NODE_SET_PROTOTYPE_METHOD (constructor_template, " stop" , SignalWatcher::Stop);
2222
23- signal_symbol = NODE_PSYMBOL (" signal" );
24-
2523 target->Set (String::NewSymbol (" SignalWatcher" ),
2624 constructor_template->GetFunction ());
25+
26+ callback_symbol = NODE_PSYMBOL (" callback" );
2727}
2828
29- void SignalWatcher::OnSignal (EV_P_ ev_signal *watcher, int revents) {
29+ void SignalWatcher::Callback (EV_P_ ev_signal *watcher, int revents) {
3030 SignalWatcher *w = static_cast <SignalWatcher*>(watcher->data );
31+
32+ assert (watcher == &w->watcher_ );
33+
3134 HandleScope scope;
3235
33- assert (revents == EV_SIGNAL);
36+ Local<Value> callback_v = w->handle_ ->Get (callback_symbol);
37+ if (!callback_v->IsFunction ()) {
38+ w->Stop ();
39+ return ;
40+ }
3441
35- w->Emit (signal_symbol, 0 , NULL );
36- }
42+ Local<Function> callback = Local<Function>::Cast (callback_v);
3743
38- SignalWatcher::~SignalWatcher () {
39- if (watcher_.active ) {
40- ev_ref (EV_DEFAULT_UC);
41- ev_signal_stop (EV_DEFAULT_UC_ &watcher_);
44+ TryCatch try_catch;
45+
46+ callback->Call (w->handle_ , 0 , NULL );
47+
48+ if (try_catch.HasCaught ()) {
49+ FatalException (try_catch);
4250 }
4351}
4452
@@ -51,30 +59,40 @@ Handle<Value> SignalWatcher::New(const Arguments& args) {
5159
5260 int sig = args[0 ]->Int32Value ();
5361
54- SignalWatcher *w = new SignalWatcher ();
62+ SignalWatcher *w = new SignalWatcher (sig );
5563 w->Wrap (args.Holder ());
5664
57- ev_signal_init (&w->watcher_ , SignalWatcher::OnSignal, sig);
58- w->watcher_ .data = w;
59- // Give signal handlers very high priority. The only thing that has higher
60- // priority is the garbage collector check.
61- ev_set_priority (&w->watcher_ , EV_MAXPRI-1 );
62- ev_signal_start (EV_DEFAULT_UC_ &w->watcher_ );
63- ev_unref (EV_DEFAULT_UC);
65+ return args.This ();
66+ }
6467
65- w->Ref ();
68+ Handle<Value> SignalWatcher::Start (const Arguments& args) {
69+ HandleScope scope;
70+ SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder ());
71+ w->Start ();
72+ return Undefined ();
73+ }
6674
67- return args.This ();
75+ void SignalWatcher::Start () {
76+ if (!watcher_.active ) {
77+ ev_signal_start (EV_DEFAULT_UC_ &watcher_);
78+ ev_unref (EV_DEFAULT_UC);
79+ Ref ();
80+ }
6881}
6982
7083Handle<Value> SignalWatcher::Stop (const Arguments& args) {
7184 HandleScope scope;
72-
7385 SignalWatcher *w = ObjectWrap::Unwrap<SignalWatcher>(args.Holder ());
74- ev_ref (EV_DEFAULT_UC);
75- ev_signal_stop (EV_DEFAULT_UC_ &w->watcher_ );
76- w->Unref ();
86+ w->Stop ();
7787 return Undefined ();
7888}
7989
90+ void SignalWatcher::Stop () {
91+ if (watcher_.active ) {
92+ ev_ref (EV_DEFAULT_UC);
93+ ev_signal_stop (EV_DEFAULT_UC_ &watcher_);
94+ Unref ();
95+ }
96+ }
97+
8098} // namespace node
0 commit comments