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: 59 60 98.3 %
Date: 2017-06-14 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::Value;
43
44
const uint32_t kOnTimeout = 0;
45
46
1578
class TimerWrap : public HandleWrap {
47
 public:
48
2401
  static void Initialize(Local<Object> target,
49
                         Local<Value> unused,
50
                         Local<Context> context) {
51
2401
    Environment* env = Environment::GetCurrent(context);
52
2401
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
53
4802
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
54
4802
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"));
55
2401
    constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
56
7203
                     Integer::New(env->isolate(), kOnTimeout));
57
58
2401
    env->SetTemplateMethod(constructor, "now", Now);
59
60
2401
    env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId);
61
62
2401
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
63
2401
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
64
2401
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
65
2401
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
66
67
2401
    env->SetProtoMethod(constructor, "start", Start);
68
2401
    env->SetProtoMethod(constructor, "stop", Stop);
69
70
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
71
7203
                constructor->GetFunction());
72
2401
  }
73
74
1
  size_t self_size() const override { return sizeof(*this); }
75
76
 private:
77
1458
  static void New(const FunctionCallbackInfo<Value>& args) {
78
    // This constructor should not be exposed to public javascript.
79
    // Therefore we assert that we are not trying to call this as a
80
    // normal function.
81
1458
    CHECK(args.IsConstructCall());
82
1458
    Environment* env = Environment::GetCurrent(args);
83
1458
    new TimerWrap(env, args.This());
84
1458
  }
85
86
1458
  TimerWrap(Environment* env, Local<Object> object)
87
      : HandleWrap(env,
88
                   object,
89
                   reinterpret_cast<uv_handle_t*>(&handle_),
90
1458
                   AsyncWrap::PROVIDER_TIMERWRAP) {
91
1458
    int r = uv_timer_init(env->event_loop(), &handle_);
92
1458
    CHECK_EQ(r, 0);
93
1458
  }
94
95
35955
  static void Start(const FunctionCallbackInfo<Value>& args) {
96
35955
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
97
98
35955
    CHECK(HandleWrap::IsAlive(wrap));
99
100
71910
    int64_t timeout = args[0]->IntegerValue();
101
35955
    int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);
102
71910
    args.GetReturnValue().Set(err);
103
35955
  }
104
105
133
  static void Stop(const FunctionCallbackInfo<Value>& args) {
106
133
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
107
108
133
    CHECK(HandleWrap::IsAlive(wrap));
109
110
133
    int err = uv_timer_stop(&wrap->handle_);
111
266
    args.GetReturnValue().Set(err);
112
133
  }
113
114
35233
  static void OnTimeout(uv_timer_t* handle) {
115
35233
    TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
116
35233
    Environment* env = wrap->env();
117
35233
    HandleScope handle_scope(env->isolate());
118
35233
    Context::Scope context_scope(env->context());
119
70407
    wrap->MakeCallback(kOnTimeout, 0, nullptr);
120
35174
  }
121
122
2711924
  static void Now(const FunctionCallbackInfo<Value>& args) {
123
2711924
    Environment* env = Environment::GetCurrent(args);
124
2711924
    uv_update_time(env->event_loop());
125
2711924
    uint64_t now = uv_now(env->event_loop());
126
2711924
    CHECK(now >= env->timer_base());
127
2711924
    now -= env->timer_base();
128
2711924
    if (now <= 0xfffffff)
129
8135772
      args.GetReturnValue().Set(static_cast<uint32_t>(now));
130
    else
131
      args.GetReturnValue().Set(static_cast<double>(now));
132
2711924
  }
133
134
  uv_timer_t handle_;
135
};
136
137
138
}  // anonymous namespace
139
}  // namespace node
140
141
2441
NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)