GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/timer_wrap.cc Lines: 59 60 98.3 %
Date: 2016-09-05 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
682
class TimerWrap : public HandleWrap {
25
 public:
26
1626
  static void Initialize(Local<Object> target,
27
                         Local<Value> unused,
28
                         Local<Context> context) {
29
1626
    Environment* env = Environment::GetCurrent(context);
30
3252
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
31
3252
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
32
1626
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"));
33
6504
    constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
34
1626
                     Integer::New(env->isolate(), kOnTimeout));
35
36
1626
    env->SetTemplateMethod(constructor, "now", Now);
37
38
1626
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
39
1626
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
40
1626
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
41
1626
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
42
43
1626
    env->SetProtoMethod(constructor, "start", Start);
44
1626
    env->SetProtoMethod(constructor, "stop", Stop);
45
46
8130
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
47
1626
                constructor->GetFunction());
48
1626
  }
49
50
1
  size_t self_size() const override { return sizeof(*this); }
51
52
 private:
53
1128
  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
1128
    CHECK(args.IsConstructCall());
58
1128
    Environment* env = Environment::GetCurrent(args);
59
1128
    new TimerWrap(env, args.This());
60
1128
  }
61
62
1128
  TimerWrap(Environment* env, Local<Object> object)
63
      : HandleWrap(env,
64
                   object,
65
                   reinterpret_cast<uv_handle_t*>(&handle_),
66
1128
                   AsyncWrap::PROVIDER_TIMERWRAP) {
67
2256
    int r = uv_timer_init(env->event_loop(), &handle_);
68
1128
    CHECK_EQ(r, 0);
69
1128
  }
70
71
5324
  static void Start(const FunctionCallbackInfo<Value>& args) {
72
5324
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
73
74
5324
    CHECK(HandleWrap::IsAlive(wrap));
75
76
5324
    int64_t timeout = args[0]->IntegerValue();
77
5324
    int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);
78
10648
    args.GetReturnValue().Set(err);
79
5324
  }
80
81
74
  static void Stop(const FunctionCallbackInfo<Value>& args) {
82
74
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
83
84
74
    CHECK(HandleWrap::IsAlive(wrap));
85
86
74
    int err = uv_timer_stop(&wrap->handle_);
87
148
    args.GetReturnValue().Set(err);
88
74
  }
89
90
4827
  static void OnTimeout(uv_timer_t* handle) {
91
4827
    TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
92
4827
    Environment* env = wrap->env();
93
9645
    HandleScope handle_scope(env->isolate());
94
14472
    Context::Scope context_scope(env->context());
95
4827
    wrap->MakeCallback(kOnTimeout, 0, nullptr);
96
4818
  }
97
98
2004608
  static void Now(const FunctionCallbackInfo<Value>& args) {
99
2004608
    Environment* env = Environment::GetCurrent(args);
100
2004608
    uv_update_time(env->event_loop());
101
2004608
    uint64_t now = uv_now(env->event_loop());
102
2004608
    CHECK(now >= env->timer_base());
103
2004608
    now -= env->timer_base();
104
2004608
    if (now <= 0xfffffff)
105
4009216
      args.GetReturnValue().Set(static_cast<uint32_t>(now));
106
    else
107
      args.GetReturnValue().Set(static_cast<double>(now));
108
2004608
  }
109
110
  uv_timer_t handle_;
111
};
112
113
114
}  // namespace node
115
116
1632
NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)