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: 2016-12-18 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
684
class TimerWrap : public HandleWrap {
25
 public:
26
1727
  static void Initialize(Local<Object> target,
27
                         Local<Value> unused,
28
                         Local<Context> context) {
29
1727
    Environment* env = Environment::GetCurrent(context);
30
3454
    Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
31
3454
    constructor->InstanceTemplate()->SetInternalFieldCount(1);
32
1727
    constructor->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"));
33
6908
    constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
34
1727
                     Integer::New(env->isolate(), kOnTimeout));
35
36
1727
    env->SetTemplateMethod(constructor, "now", Now);
37
38
1727
    env->SetProtoMethod(constructor, "close", HandleWrap::Close);
39
1727
    env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
40
1727
    env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
41
1727
    env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
42
43
1727
    env->SetProtoMethod(constructor, "start", Start);
44
1727
    env->SetProtoMethod(constructor, "stop", Stop);
45
46
8635
    target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
47
1727
                constructor->GetFunction());
48
1727
  }
49
50
1
  size_t self_size() const override { return sizeof(*this); }
51
52
 private:
53
1150
  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
1150
    CHECK(args.IsConstructCall());
58
1150
    Environment* env = Environment::GetCurrent(args);
59
1150
    new TimerWrap(env, args.This());
60
1150
  }
61
62
1150
  TimerWrap(Environment* env, Local<Object> object)
63
      : HandleWrap(env,
64
                   object,
65
                   reinterpret_cast<uv_handle_t*>(&handle_),
66
1150
                   AsyncWrap::PROVIDER_TIMERWRAP) {
67
2300
    int r = uv_timer_init(env->event_loop(), &handle_);
68
1150
    CHECK_EQ(r, 0);
69
1150
  }
70
71
4362
  static void Start(const FunctionCallbackInfo<Value>& args) {
72
4362
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
73
74
4362
    CHECK(HandleWrap::IsAlive(wrap));
75
76
4362
    int64_t timeout = args[0]->IntegerValue();
77
4362
    int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);
78
8724
    args.GetReturnValue().Set(err);
79
4362
  }
80
81
77
  static void Stop(const FunctionCallbackInfo<Value>& args) {
82
77
    TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
83
84
77
    CHECK(HandleWrap::IsAlive(wrap));
85
86
77
    int err = uv_timer_stop(&wrap->handle_);
87
154
    args.GetReturnValue().Set(err);
88
77
  }
89
90
3840
  static void OnTimeout(uv_timer_t* handle) {
91
3840
    TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
92
3840
    Environment* env = wrap->env();
93
7674
    HandleScope handle_scope(env->isolate());
94
11514
    Context::Scope context_scope(env->context());
95
3840
    wrap->MakeCallback(kOnTimeout, 0, nullptr);
96
3834
  }
97
98
4380886
  static void Now(const FunctionCallbackInfo<Value>& args) {
99
4380886
    Environment* env = Environment::GetCurrent(args);
100
4380886
    uv_update_time(env->event_loop());
101
4380886
    uint64_t now = uv_now(env->event_loop());
102
4380886
    CHECK(now >= env->timer_base());
103
4380886
    now -= env->timer_base();
104
4380886
    if (now <= 0xfffffff)
105
8761772
      args.GetReturnValue().Set(static_cast<uint32_t>(now));
106
    else
107
      args.GetReturnValue().Set(static_cast<double>(now));
108
4380886
  }
109
110
  uv_timer_t handle_;
111
};
112
113
114
}  // namespace node
115
116
1734
NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)