GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/src/timer_wrap.cc Lines: 60 61 98.4 %
Date: 2016-07-26 Branches: 6 12 50.0 %

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