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