GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/signal_wrap.cc Lines: 40 48 83.3 %
Date: 2016-07-23 Branches: 4 12 33.3 %

Line Exec Source
1
#include "async-wrap.h"
2
#include "async-wrap-inl.h"
3
#include "env.h"
4
#include "env-inl.h"
5
#include "handle_wrap.h"
6
#include "util.h"
7
#include "util-inl.h"
8
#include "v8.h"
9
10
namespace node {
11
12
using v8::Context;
13
using v8::Function;
14
using v8::FunctionCallbackInfo;
15
using v8::FunctionTemplate;
16
using v8::HandleScope;
17
using v8::Integer;
18
using v8::Local;
19
using v8::Object;
20
using v8::Value;
21
22
2
class SignalWrap : public HandleWrap {
23
 public:
24
24
  static void Initialize(Local<Object> target,
25
                         Local<Value> unused,
26
                         Local<Context> context) {
27
24
    Environment* env = Environment::GetCurrent(context);
28
48
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
29
48
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
30
24
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"));
31
32
24
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
33
24
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
34
24
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
35
24
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
36
24
    env->SetProtoMethod(constructor, "start", Start);
37
24
    env->SetProtoMethod(constructor, "stop", Stop);
38
39
120
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"),
40
24
                constructor->GetFunction());
41
24
  }
42
43
  size_t self_size() const override { return sizeof(*this); }
44
45
 private:
46
56
  static void New(const FunctionCallbackInfo<Value>& args) {
47
    // This constructor should not be exposed to public javascript.
48
    // Therefore we assert that we are not trying to call this as a
49
    // normal function.
50
56
    CHECK(args.IsConstructCall());
51
56
    Environment* env = Environment::GetCurrent(args);
52
56
    new SignalWrap(env, args.This());
53
56
  }
54
55
56
  SignalWrap(Environment* env, Local<Object> object)
56
      : HandleWrap(env,
57
                   object,
58
                   reinterpret_cast<uv_handle_t*>(&handle_),
59
56
                   AsyncWrap::PROVIDER_SIGNALWRAP) {
60
112
    int r = uv_signal_init(env->event_loop(), &handle_);
61
56
    CHECK_EQ(r, 0);
62
56
  }
63
64
55
  static void Start(const FunctionCallbackInfo<Value>& args) {
65
    SignalWrap* wrap;
66
55
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
67
55
    int signum = args[0]->Int32Value();
68
#if defined(__POSIX__) && HAVE_INSPECTOR
69
55
    if (signum == SIGPROF) {
70
      Environment* env = Environment::GetCurrent(args);
71
      if (env->inspector_agent()->IsStarted()) {
72
        fprintf(stderr, "process.on(SIGPROF) is reserved while debugging\n");
73
        return;
74
      }
75
    }
76
#endif
77
55
    int err = uv_signal_start(&wrap->handle_, OnSignal, signum);
78
110
    args.GetReturnValue().Set(err);
79
  }
80
81
  static void Stop(const FunctionCallbackInfo<Value>& args) {
82
    SignalWrap* wrap;
83
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
84
    int err = uv_signal_stop(&wrap->handle_);
85
    args.GetReturnValue().Set(err);
86
  }
87
88
15
  static void OnSignal(uv_signal_t* handle, int signum) {
89
15
    SignalWrap* wrap = ContainerOf(&SignalWrap::handle_, handle);
90
15
    Environment* env = wrap->env();
91
25
    HandleScope handle_scope(env->isolate());
92
40
    Context::Scope context_scope(env->context());
93
94
30
    Local<Value> arg = Integer::New(env->isolate(), signum);
95
15
    wrap->MakeCallback(env->onsignal_string(), 1, &arg);
96
10
  }
97
98
  uv_signal_t handle_;
99
};
100
101
102
}  // namespace node
103
104
105
1569
NODE_MODULE_CONTEXT_AWARE_BUILTIN(signal_wrap, node::SignalWrap::Initialize)