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-23 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
1563
  static void Initialize(Local<Object> target,
28
                         Local<Value> unused,
29
                         Local<Context> context) {
30
1563
    Environment* env = Environment::GetCurrent(context);
31
3126
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
32
3126
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
33
1563
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"));
34
6252
    constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
35
1563
                     Integer::New(env->isolate(), kOnTimeout));
36
37
1563
    env->SetTemplateMethod(constructor, "now", Now);
38
39
1563
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
40
1563
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
41
1563
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
42
1563
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
43
44
1563
    env->SetProtoMethod(constructor, "start", Start);
45
1563
    env->SetProtoMethod(constructor, "stop", Stop);
46
47
7815
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
48
1563
                constructor->GetFunction());
49
1563
  }
50
51
1
  size_t self_size() const override { return sizeof(*this); }
52
53
 private:
54
1123
  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
1123
    CHECK(args.IsConstructCall());
59
1123
    Environment* env = Environment::GetCurrent(args);
60
1123
    new TimerWrap(env, args.This());
61
1123
  }
62
63
1123
  TimerWrap(Environment* env, Local<Object> object)
64
      : HandleWrap(env,
65
                   object,
66
                   reinterpret_cast<uv_handle_t*>(&handle_),
67
1123
                   AsyncWrap::PROVIDER_TIMERWRAP) {
68
2246
    int r = uv_timer_init(env->event_loop(), &handle_);
69
1123
    CHECK_EQ(r, 0);
70
1123
  }
71
72
5756
  static void Start(const FunctionCallbackInfo<Value>& args) {
73
5756
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
74
75
5756
    CHECK(HandleWrap::IsAlive(wrap));
76
77
5756
    int64_t timeout = args[0]->IntegerValue();
78
5756
    int64_t repeat = args[1]->IntegerValue();
79
5756
    int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, repeat);
80
11512
    args.GetReturnValue().Set(err);
81
5756
  }
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
5263
  static void OnTimeout(uv_timer_t* handle) {
93
5263
    TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
94
5263
    Environment* env = wrap->env();
95
10517
    HandleScope handle_scope(env->isolate());
96
15780
    Context::Scope context_scope(env->context());
97
5263
    wrap->MakeCallback(kOnTimeout, 0, nullptr);
98
5254
  }
99
100
1914724
  static void Now(const FunctionCallbackInfo<Value>& args) {
101
1914724
    Environment* env = Environment::GetCurrent(args);
102
1914724
    uv_update_time(env->event_loop());
103
1914724
    uint64_t now = uv_now(env->event_loop());
104
1914724
    CHECK(now >= env->timer_base());
105
1914724
    now -= env->timer_base();
106
1914724
    if (now <= 0xfffffff)
107
3829448
      args.GetReturnValue().Set(static_cast<uint32_t>(now));
108
    else
109
      args.GetReturnValue().Set(static_cast<double>(now));
110
1914724
  }
111
112
  uv_timer_t handle_;
113
};
114
115
116
}  // namespace node
117
118
1569
NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)