The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / src / signal_wrap.cc @ 6f92da2d
History | View | Annotate | Download (3.66 KB)
1 | 600a6468 | Bert Belder | // 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 "node.h" |
||
23 | #include "handle_wrap.h" |
||
24 | |||
25 | |||
26 | namespace node {
|
||
27 | |||
28 | b9165252 | Ben Noordhuis | using v8::Function;
|
29 | 110a9cd8 | Ben Noordhuis | using v8::FunctionCallbackInfo;
|
30 | b9165252 | Ben Noordhuis | using v8::FunctionTemplate;
|
31 | 600a6468 | Bert Belder | using v8::Handle;
|
32 | b9165252 | Ben Noordhuis | using v8::HandleScope;
|
33 | using v8::Integer;
|
||
34 | 600a6468 | Bert Belder | using v8::Local;
|
35 | b9165252 | Ben Noordhuis | using v8::Object;
|
36 | 600a6468 | Bert Belder | using v8::String;
|
37 | b9165252 | Ben Noordhuis | using v8::Value;
|
38 | 600a6468 | Bert Belder | |
39 | 110a9cd8 | Ben Noordhuis | static Cached<String> onsignal_sym;
|
40 | 600a6468 | Bert Belder | |
41 | |||
42 | class SignalWrap : public HandleWrap { |
||
43 | public:
|
||
44 | static void Initialize(Handle<Object> target) { |
||
45 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
46 | 600a6468 | Bert Belder | |
47 | HandleWrap::Initialize(target); |
||
48 | |||
49 | Local<FunctionTemplate> constructor = FunctionTemplate::New(New); |
||
50 | constructor->InstanceTemplate()->SetInternalFieldCount(1);
|
||
51 | constructor->SetClassName(String::NewSymbol("Signal"));
|
||
52 | |||
53 | NODE_SET_PROTOTYPE_METHOD(constructor, "close", HandleWrap::Close);
|
||
54 | NODE_SET_PROTOTYPE_METHOD(constructor, "ref", HandleWrap::Ref);
|
||
55 | NODE_SET_PROTOTYPE_METHOD(constructor, "unref", HandleWrap::Unref);
|
||
56 | NODE_SET_PROTOTYPE_METHOD(constructor, "start", Start);
|
||
57 | NODE_SET_PROTOTYPE_METHOD(constructor, "stop", Stop);
|
||
58 | |||
59 | 110a9cd8 | Ben Noordhuis | onsignal_sym = String::New("onsignal");
|
60 | 600a6468 | Bert Belder | |
61 | target->Set(String::NewSymbol("Signal"), constructor->GetFunction());
|
||
62 | } |
||
63 | |||
64 | private:
|
||
65 | 110a9cd8 | Ben Noordhuis | static void New(const FunctionCallbackInfo<Value>& args) { |
66 | 600a6468 | Bert Belder | // This constructor should not be exposed to public javascript.
|
67 | // Therefore we assert that we are not trying to call this as a
|
||
68 | // normal function.
|
||
69 | assert(args.IsConstructCall()); |
||
70 | |||
71 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
72 | 2cdf427d | Ben Noordhuis | new SignalWrap(args.This());
|
73 | 600a6468 | Bert Belder | } |
74 | |||
75 | 8e29ce9f | Fedor Indutny | explicit SignalWrap(Handle<Object> object)
|
76 | 600a6468 | Bert Belder | : HandleWrap(object, reinterpret_cast<uv_handle_t*>(&handle_)) {
|
77 | int r = uv_signal_init(uv_default_loop(), &handle_);
|
||
78 | assert(r == 0);
|
||
79 | } |
||
80 | |||
81 | ~SignalWrap() { |
||
82 | } |
||
83 | |||
84 | 110a9cd8 | Ben Noordhuis | static void Start(const FunctionCallbackInfo<Value>& args) { |
85 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
86 | 600a6468 | Bert Belder | UNWRAP(SignalWrap) |
87 | |||
88 | int signum = args[0]->Int32Value(); |
||
89 | ca9eb718 | Ben Noordhuis | int err = uv_signal_start(&wrap->handle_, OnSignal, signum);
|
90 | args.GetReturnValue().Set(err); |
||
91 | 600a6468 | Bert Belder | } |
92 | |||
93 | 110a9cd8 | Ben Noordhuis | static void Stop(const FunctionCallbackInfo<Value>& args) { |
94 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
95 | 600a6468 | Bert Belder | UNWRAP(SignalWrap) |
96 | |||
97 | ca9eb718 | Ben Noordhuis | int err = uv_signal_stop(&wrap->handle_);
|
98 | args.GetReturnValue().Set(err); |
||
99 | 600a6468 | Bert Belder | } |
100 | |||
101 | static void OnSignal(uv_signal_t* handle, int signum) { |
||
102 | f65e14ed | Trevor Norris | HandleScope scope(node_isolate); |
103 | 600a6468 | Bert Belder | |
104 | SignalWrap* wrap = container_of(handle, SignalWrap, handle_); |
||
105 | assert(wrap); |
||
106 | |||
107 | f65e14ed | Trevor Norris | Local<Value> argv[1] = { Integer::New(signum, node_isolate) };
|
108 | 110a9cd8 | Ben Noordhuis | MakeCallback(wrap->object(), onsignal_sym, ARRAY_SIZE(argv), argv); |
109 | 600a6468 | Bert Belder | } |
110 | |||
111 | uv_signal_t handle_; |
||
112 | }; |
||
113 | |||
114 | |||
115 | } // namespace node
|
||
116 | |||
117 | |||
118 | NODE_MODULE(node_signal_wrap, node::SignalWrap::Initialize) |