GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/timer_wrap.cc Lines: 60 61 98.4 %
Date: 2017-10-21 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.h"
23
#include "async-wrap-inl.h"
24
#include "env.h"
25
#include "env-inl.h"
26
#include "handle_wrap.h"
27
#include "util.h"
28
#include "util-inl.h"
29
30
#include <stdint.h>
31
32
namespace node {
33
namespace {
34
35
using v8::Context;
36
using v8::FunctionCallbackInfo;
37
using v8::FunctionTemplate;
38
using v8::HandleScope;
39
using v8::Integer;
40
using v8::Local;
41
using v8::Object;
42
using v8::String;
43
using v8::Value;
44
45
const uint32_t kOnTimeout = 0;
46
47
1520
class TimerWrap : public HandleWrap {
48
 public:
49
3200
  static void Initialize(Local<Object> target,
50
                         Local<Value> unused,
51
                         Local<Context> context) {
52
3200
    Environment* env = Environment::GetCurrent(context);
53
3200
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
54
3200
    Local<String> timerString = FIXED_ONE_BYTE_STRING(env->isolate(), "Timer");
55
6400
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
56
3200
    constructor->SetClassName(timerString);
57
3200
    constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
58
9600
                     Integer::New(env->isolate(), kOnTimeout));
59
60
3200
    env->SetTemplateMethod(constructor, "now", Now);
61
62
3200
    AsyncWrap::AddWrapMethods(env, constructor);
63
64
3200
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
65
3200
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
66
3200
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
67
3200
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
68
69
3200
    env->SetProtoMethod(constructor, "start", Start);
70
3200
    env->SetProtoMethod(constructor, "stop", Stop);
71
72
6400
    target->Set(timerString, constructor->GetFunction());
73
3200
  }
74
75
1
  size_t self_size() const override { return sizeof(*this); }
76
77
 private:
78
1598
  static void New(const FunctionCallbackInfo<Value>& args) {
79
    // This constructor should not be exposed to public javascript.
80
    // Therefore we assert that we are not trying to call this as a
81
    // normal function.
82
1598
    CHECK(args.IsConstructCall());
83
1598
    Environment* env = Environment::GetCurrent(args);
84
1598
    new TimerWrap(env, args.This());
85
1598
  }
86
87
1598
  TimerWrap(Environment* env, Local<Object> object)
88
      : HandleWrap(env,
89
                   object,
90
                   reinterpret_cast<uv_handle_t*>(&handle_),
91
1598
                   AsyncWrap::PROVIDER_TIMERWRAP) {
92
1598
    int r = uv_timer_init(env->event_loop(), &handle_);
93
1598
    CHECK_EQ(r, 0);
94
1598
  }
95
96
31744
  static void Start(const FunctionCallbackInfo<Value>& args) {
97
31744
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
98
99
31744
    CHECK(HandleWrap::IsAlive(wrap));
100
101
63488
    int64_t timeout = args[0]->IntegerValue();
102
31744
    int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);
103
63488
    args.GetReturnValue().Set(err);
104
31744
  }
105
106
151
  static void Stop(const FunctionCallbackInfo<Value>& args) {
107
151
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
108
109
151
    CHECK(HandleWrap::IsAlive(wrap));
110
111
151
    int err = uv_timer_stop(&wrap->handle_);
112
302
    args.GetReturnValue().Set(err);
113
151
  }
114
115
30795
  static void OnTimeout(uv_timer_t* handle) {
116
30795
    TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
117
30795
    Environment* env = wrap->env();
118
30795
    HandleScope handle_scope(env->isolate());
119
30795
    Context::Scope context_scope(env->context());
120
61573
    wrap->MakeCallback(kOnTimeout, 0, nullptr);
121
30778
  }
122
123
3651660
  static void Now(const FunctionCallbackInfo<Value>& args) {
124
3651660
    Environment* env = Environment::GetCurrent(args);
125
3651660
    uv_update_time(env->event_loop());
126
3651660
    uint64_t now = uv_now(env->event_loop());
127
3651660
    CHECK(now >= env->timer_base());
128
3651660
    now -= env->timer_base();
129
3651660
    if (now <= 0xfffffff)
130
10954980
      args.GetReturnValue().Set(static_cast<uint32_t>(now));
131
    else
132
      args.GetReturnValue().Set(static_cast<double>(now));
133
3651660
  }
134
135
  uv_timer_t handle_;
136
};
137
138
139
}  // anonymous namespace
140
}  // namespace node
141
142
3245
NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)