GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/signal_wrap.cc Lines: 45 50 90.0 %
Date: 2017-11-19 Branches: 7 14 50.0 %

Line Branch Exec Source
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 "async_wrap-inl.h"
23
#include "env-inl.h"
24
#include "handle_wrap.h"
25
#include "util-inl.h"
26
#include "v8.h"
27
28
namespace node {
29
30
using v8::Context;
31
using v8::FunctionCallbackInfo;
32
using v8::FunctionTemplate;
33
using v8::HandleScope;
34
using v8::Integer;
35
using v8::Local;
36
using v8::Object;
37
using v8::String;
38
using v8::Value;
39
40
namespace {
41
42
22
class SignalWrap : public HandleWrap {
43
 public:
44
48
  static void Initialize(Local<Object> target,
45
                         Local<Value> unused,
46
                         Local<Context> context) {
47
48
    Environment* env = Environment::GetCurrent(context);
48
48
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
49
96
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
50
    Local<String> signalString =
51
48
        FIXED_ONE_BYTE_STRING(env->isolate(), "Signal");
52
48
    constructor->SetClassName(signalString);
53
54
48
    AsyncWrap::AddWrapMethods(env, constructor);
55
48
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
56
48
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
57
48
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
58
48
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
59
48
    env->SetProtoMethod(constructor, "start", Start);
60
48
    env->SetProtoMethod(constructor, "stop", Stop);
61
62
96
    target->Set(signalString, constructor->GetFunction());
63
48
  }
64
65
  size_t self_size() const override { return sizeof(*this); }
66
67
 private:
68
63
  static void New(const FunctionCallbackInfo<Value>& 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
63
    CHECK(args.IsConstructCall());
73
63
    Environment* env = Environment::GetCurrent(args);
74
63
    new SignalWrap(env, args.This());
75
63
  }
76
77
63
  SignalWrap(Environment* env, Local<Object> object)
78
      : HandleWrap(env,
79
                   object,
80
                   reinterpret_cast<uv_handle_t*>(&handle_),
81
63
                   AsyncWrap::PROVIDER_SIGNALWRAP) {
82
63
    int r = uv_signal_init(env->event_loop(), &handle_);
83
63
    CHECK_EQ(r, 0);
84
63
  }
85
86
61
  static void Start(const FunctionCallbackInfo<Value>& args) {
87
    SignalWrap* wrap;
88
62
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
89
122
    int signum = args[0]->Int32Value();
90
#if defined(__POSIX__) && HAVE_INSPECTOR
91
61
    if (signum == SIGPROF) {
92
1
      Environment* env = Environment::GetCurrent(args);
93
1
      if (env->inspector_agent()->IsStarted()) {
94
        ProcessEmitWarning(env,
95
1
                           "process.on(SIGPROF) is reserved while debugging");
96
1
        return;
97
      }
98
    }
99
#endif
100
60
    int err = uv_signal_start(&wrap->handle_, OnSignal, signum);
101
120
    args.GetReturnValue().Set(err);
102
  }
103
104
  static void Stop(const FunctionCallbackInfo<Value>& args) {
105
    SignalWrap* wrap;
106
    ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
107
    int err = uv_signal_stop(&wrap->handle_);
108
    args.GetReturnValue().Set(err);
109
  }
110
111
21
  static void OnSignal(uv_signal_t* handle, int signum) {
112
21
    SignalWrap* wrap = ContainerOf(&SignalWrap::handle_, handle);
113
21
    Environment* env = wrap->env();
114
21
    HandleScope handle_scope(env->isolate());
115
21
    Context::Scope context_scope(env->context());
116
117
21
    Local<Value> arg = Integer::New(env->isolate(), signum);
118
42
    wrap->MakeCallback(env->onsignal_string(), 1, &arg);
119
21
  }
120
121
  uv_signal_t handle_;
122
};
123
124
125
}  // anonymous namespace
126
}  // namespace node
127
128
129
3332
NODE_BUILTIN_MODULE_CONTEXT_AWARE(signal_wrap, node::SignalWrap::Initialize)