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

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