GCC Code Coverage Report
Directory: ../src/ 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-12-16 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
54
  static void Initialize(Local<Object> target,
24
                         Local<Value> unused,
25
                         Local<Context> context) {
26
54
    Environment* env = Environment::GetCurrent(context);
27
108
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
28
108
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
29
54
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"));
30
31
54
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
32
54
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
33
54
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
34
54
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
35
54
    env->SetProtoMethod(constructor, "start", Start);
36
54
    env->SetProtoMethod(constructor, "stop", Stop);
37
38
270
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Signal"),
39
54
                constructor->GetFunction());
40
54
  }
41
42
  size_t self_size() const override { return sizeof(*this); }
43
44
 private:
45
615
  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
615
    CHECK(args.IsConstructCall());
50
615
    Environment* env = Environment::GetCurrent(args);
51
615
    new SignalWrap(env, args.This());
52
615
  }
53
54
615
  SignalWrap(Environment* env, Local<Object> object)
55
      : HandleWrap(env,
56
                   object,
57
                   reinterpret_cast<uv_handle_t*>(&handle_),
58
615
                   AsyncWrap::PROVIDER_SIGNALWRAP) {
59
1230
    int r = uv_signal_init(env->event_loop(), &handle_);
60
615
    CHECK_EQ(r, 0);
61
615
  }
62
63
614
  static void Start(const FunctionCallbackInfo<Value>& args) {
64
    SignalWrap* wrap;
65
614
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
66
614
    int signum = args[0]->Int32Value();
67
#if defined(__POSIX__) && HAVE_INSPECTOR
68
614
    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
614
    int err = uv_signal_start(&wrap->handle_, OnSignal, signum);
77
1228
    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
17
  static void OnSignal(uv_signal_t* handle, int signum) {
88
17
    SignalWrap* wrap = ContainerOf(&SignalWrap::handle_, handle);
89
17
    Environment* env = wrap->env();
90
27
    HandleScope handle_scope(env->isolate());
91
44
    Context::Scope context_scope(env->context());
92
93
34
    Local<Value> arg = Integer::New(env->isolate(), signum);
94
17
    wrap->MakeCallback(env->onsignal_string(), 1, &arg);
95
10
  }
96
97
  uv_signal_t handle_;
98
};
99
100
101
}  // namespace node
102
103
104
1729
NODE_MODULE_CONTEXT_AWARE_BUILTIN(signal_wrap, node::SignalWrap::Initialize)