1 #include "async-wrap.h"
2 #include "async-wrap-inl.h"
5 #include "handle_wrap.h"
15 using v8::FunctionCallbackInfo;
16 using v8::FunctionTemplate;
17 using v8::HandleScope;
23 const uint32_t kOnTimeout = 0;
25 class TimerWrap : public HandleWrap {
27 static void Initialize(Local<Object> target,
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));
37 env->SetTemplateMethod(constructor, "now", Now);
39 env->SetProtoMethod(constructor, "close", HandleWrap::Close);
40 env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
41 env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
43 env->SetProtoMethod(constructor, "start", Start);
44 env->SetProtoMethod(constructor, "stop", Stop);
46 target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Timer"),
47 constructor->GetFunction());
50 size_t self_size() const override { return sizeof(*this); }
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
57 CHECK(args.IsConstructCall());
58 Environment* env = Environment::GetCurrent(args);
59 new TimerWrap(env, args.This());
62 TimerWrap(Environment* env, Local<Object> object)
65 reinterpret_cast<uv_handle_t*>(&handle_),
66 AsyncWrap::PROVIDER_TIMERWRAP) {
67 int r = uv_timer_init(env->event_loop(), &handle_);
71 static void Start(const FunctionCallbackInfo<Value>& args) {
72 TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
74 CHECK(HandleWrap::IsAlive(wrap));
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);
82 static void Stop(const FunctionCallbackInfo<Value>& args) {
83 TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
85 CHECK(HandleWrap::IsAlive(wrap));
87 int err = uv_timer_stop(&wrap->handle_);
88 args.GetReturnValue().Set(err);
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);
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));
108 args.GetReturnValue().Set(static_cast<double>(now));
117 NODE_MODULE_CONTEXT_AWARE_BUILTIN(timer_wrap, node::TimerWrap::Initialize)