|
| 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 "node.h" |
| 23 | +#include "handle_wrap.h" |
| 24 | + |
| 25 | + |
| 26 | +namespace node { |
| 27 | + |
| 28 | +using v8::Object; |
| 29 | +using v8::Handle; |
| 30 | +using v8::Local; |
| 31 | +using v8::Persistent; |
| 32 | +using v8::Value; |
| 33 | +using v8::HandleScope; |
| 34 | +using v8::FunctionTemplate; |
| 35 | +using v8::String; |
| 36 | +using v8::Function; |
| 37 | +using v8::TryCatch; |
| 38 | +using v8::Context; |
| 39 | +using v8::Arguments; |
| 40 | +using v8::Integer; |
| 41 | + |
| 42 | +static Persistent<String> onsignal_sym; |
| 43 | + |
| 44 | + |
| 45 | +class SignalWrap : public HandleWrap { |
| 46 | + public: |
| 47 | + static void Initialize(Handle<Object> target) { |
| 48 | + HandleScope scope; |
| 49 | + |
| 50 | + HandleWrap::Initialize(target); |
| 51 | + |
| 52 | + Local<FunctionTemplate> constructor = FunctionTemplate::New(New); |
| 53 | + constructor->InstanceTemplate()->SetInternalFieldCount(1); |
| 54 | + constructor->SetClassName(String::NewSymbol("Signal")); |
| 55 | + |
| 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); |
| 61 | + |
| 62 | + onsignal_sym = NODE_PSYMBOL("onsignal"); |
| 63 | + |
| 64 | + target->Set(String::NewSymbol("Signal"), constructor->GetFunction()); |
| 65 | + } |
| 66 | + |
| 67 | + private: |
| 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 |
| 71 | + // normal function. |
| 72 | + assert(args.IsConstructCall()); |
| 73 | + |
| 74 | + HandleScope scope; |
| 75 | + SignalWrap* wrap = new SignalWrap(args.This()); |
| 76 | + |
| 77 | + return scope.Close(args.This()); |
| 78 | + } |
| 79 | + |
| 80 | + SignalWrap(Handle<Object> object) |
| 81 | + : HandleWrap(object, reinterpret_cast<uv_handle_t*>(&handle_)) { |
| 82 | + int r = uv_signal_init(uv_default_loop(), &handle_); |
| 83 | + assert(r == 0); |
| 84 | + } |
| 85 | + |
| 86 | + ~SignalWrap() { |
| 87 | + } |
| 88 | + |
| 89 | + static Handle<Value> Start(const Arguments& args) { |
| 90 | + HandleScope scope; |
| 91 | + |
| 92 | + UNWRAP(SignalWrap) |
| 93 | + |
| 94 | + int signum = args[0]->Int32Value(); |
| 95 | + |
| 96 | + int r = uv_signal_start(&wrap->handle_, OnSignal, signum); |
| 97 | + |
| 98 | + if (r) SetErrno(uv_last_error(uv_default_loop())); |
| 99 | + |
| 100 | + return scope.Close(Integer::New(r)); |
| 101 | + } |
| 102 | + |
| 103 | + static Handle<Value> Stop(const Arguments& args) { |
| 104 | + HandleScope scope; |
| 105 | + |
| 106 | + UNWRAP(SignalWrap) |
| 107 | + |
| 108 | + int r = uv_signal_stop(&wrap->handle_); |
| 109 | + |
| 110 | + if (r) SetErrno(uv_last_error(uv_default_loop())); |
| 111 | + |
| 112 | + return scope.Close(Integer::New(r)); |
| 113 | + } |
| 114 | + |
| 115 | + static void OnSignal(uv_signal_t* handle, int signum) { |
| 116 | + HandleScope scope; |
| 117 | + |
| 118 | + SignalWrap* wrap = container_of(handle, SignalWrap, handle_); |
| 119 | + assert(wrap); |
| 120 | + |
| 121 | + Local<Value> argv[1] = { Integer::New(signum) }; |
| 122 | + MakeCallback(wrap->object_, onsignal_sym, ARRAY_SIZE(argv), argv); |
| 123 | + } |
| 124 | + |
| 125 | + uv_signal_t handle_; |
| 126 | +}; |
| 127 | + |
| 128 | + |
| 129 | +} // namespace node |
| 130 | + |
| 131 | + |
| 132 | +NODE_MODULE(node_signal_wrap, node::SignalWrap::Initialize) |
0 commit comments