GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/timer_wrap.cc Lines: 59 60 98.3 %
Date: 2017-01-02 Branches: 6 12 50.0 %

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
9
#include <stdint.h>
10
11
namespace node {
12
13
using v8::Context;
14
using v8::FunctionCallbackInfo;
15
using v8::FunctionTemplate;
16
using v8::HandleScope;
17
using v8::Integer;
18
using v8::Local;
19
using v8::Object;
20
using v8::Value;
21
22
const uint32_t kOnTimeout = 0;
23
24
668
class TimerWrap : public HandleWrap {
25
 public:
26
1750
  static void Initialize(Local<Object> target,
27
                         Local<Value> unused,
28
                         Local<Context> context) {
29
1750
    Environment* env = Environment::GetCurrent(context);
30
3500
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
31
3500
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
32
1750
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"));
33
7000
    constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
34
1750
                     Integer::New(env->isolate(), kOnTimeout));
35
36
1750
    env->SetTemplateMethod(constructor, "now", Now);
37
38
1750
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
39
1750
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
40
1750
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
41
1750
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
42
43
1750
    env->SetProtoMethod(constructor, "start", Start);
44
1750
    env->SetProtoMethod(constructor, "stop", Stop);
45
46
8750
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
47
1750
                constructor->GetFunction());
48
1750
  }
49
50
1
  size_t self_size() const override { return sizeof(*this); }
51
52
 private:
53
1132
  static void New(const FunctionCallbackInfo<Value>& args) {
54
    // This constructor should not be exposed to public javascript.
55
    // Therefore we assert that we are not trying to call this as a
56
    // normal function.
57
1132
    CHECK(args.IsConstructCall());
58
1132
    Environment* env = Environment::GetCurrent(args);
59
1132
    new TimerWrap(env, args.This());
60
1132
  }
61
62
1132
  TimerWrap(Environment* env, Local<Object> object)
63
      : HandleWrap(env,
64
                   object,
65
                   reinterpret_cast<uv_handle_t*>(&handle_),
66
1132
                   AsyncWrap::PROVIDER_TIMERWRAP) {
67
2264
    int r = uv_timer_init(env->event_loop(), &handle_);
68
1132
    CHECK_EQ(r, 0);
69
1132
  }
70
71
4549
  static void Start(const FunctionCallbackInfo<Value>& args) {
72
4549
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
73
74
4549
    CHECK(HandleWrap::IsAlive(wrap));
75
76
4549
    int64_t timeout = args[0]->IntegerValue();
77
4549
    int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);
78
9098
    args.GetReturnValue().Set(err);
79
4549
  }
80
81
76
  static void Stop(const FunctionCallbackInfo<Value>& args) {
82
76
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
83
84
76
    CHECK(HandleWrap::IsAlive(wrap));
85
86
76
    int err = uv_timer_stop(&wrap->handle_);
87
152
    args.GetReturnValue().Set(err);
88
76
  }
89
90
4030
  static void OnTimeout(uv_timer_t* handle) {
91
4030
    TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
92
4030
    Environment* env = wrap->env();
93
8054
    HandleScope handle_scope(env->isolate());
94
12084
    Context::Scope context_scope(env->context());
95
4030
    wrap->MakeCallback(kOnTimeout, 0, nullptr);
96
4024
  }
97
98
4499584
  static void Now(const FunctionCallbackInfo<Value>& args) {
99
4499584
    Environment* env = Environment::GetCurrent(args);
100
4499584
    uv_update_time(env->event_loop());
101
4499584
    uint64_t now = uv_now(env->event_loop());
102
4499584
    CHECK(now >= env->timer_base());
103
4499584
    now -= env->timer_base();
104
4499584
    if (now <= 0xfffffff)
105
8999168
      args.GetReturnValue().Set(static_cast<uint32_t>(now));
106
    else
107
      args.GetReturnValue().Set(static_cast<double>(now));
108
4499584
  }
109
110
  uv_timer_t handle_;
111
};
112
113
114
}  // namespace node
115
116
1757
NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)