Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
timer_wrap.cc
Go to the documentation of this file.
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #include "async-wrap.h"
23 #include "async-wrap-inl.h"
24 #include "env.h"
25 #include "env-inl.h"
26 #include "handle_wrap.h"
27 #include "util.h"
28 #include "util-inl.h"
29 
30 #include <stdint.h>
31 
32 namespace node {
33 namespace {
34 
35 using v8::Context;
36 using v8::FunctionCallbackInfo;
37 using v8::FunctionTemplate;
38 using v8::HandleScope;
39 using v8::Integer;
40 using v8::Local;
41 using v8::Object;
42 using v8::String;
43 using v8::Value;
44 
45 const uint32_t kOnTimeout = 0;
46 
47 class TimerWrap : public HandleWrap {
48  public:
49  static void Initialize(Local<Object> target,
50  Local<Value> unused,
51  Local<Context> context) {
52  Environment* env = Environment::GetCurrent(context);
53  Local<FunctionTemplate> constructor = env->NewFunctionTemplate(New);
54  Local<String> timerString = FIXED_ONE_BYTE_STRING(env->isolate(), "Timer");
55  constructor->InstanceTemplate()->SetInternalFieldCount(1);
56  constructor->SetClassName(timerString);
57  constructor->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "kOnTimeout"),
58  Integer::New(env->isolate(), kOnTimeout));
59 
60  env->SetTemplateMethod(constructor, "now", Now);
61 
62  AsyncWrap::AddWrapMethods(env, constructor);
63 
64  env->SetProtoMethod(constructor, "close", HandleWrap::Close);
65  env->SetProtoMethod(constructor, "ref", HandleWrap::Ref);
66  env->SetProtoMethod(constructor, "unref", HandleWrap::Unref);
67  env->SetProtoMethod(constructor, "hasRef", HandleWrap::HasRef);
68 
69  env->SetProtoMethod(constructor, "start", Start);
70  env->SetProtoMethod(constructor, "stop", Stop);
71 
72  target->Set(timerString, constructor->GetFunction());
73  }
74 
75  size_t self_size() const override { return sizeof(*this); }
76 
77  private:
78  static void New(const FunctionCallbackInfo<Value>& args) {
79  // This constructor should not be exposed to public javascript.
80  // Therefore we assert that we are not trying to call this as a
81  // normal function.
82  CHECK(args.IsConstructCall());
83  Environment* env = Environment::GetCurrent(args);
84  new TimerWrap(env, args.This());
85  }
86 
87  TimerWrap(Environment* env, Local<Object> object)
88  : HandleWrap(env,
89  object,
90  reinterpret_cast<uv_handle_t*>(&handle_),
91  AsyncWrap::PROVIDER_TIMERWRAP) {
92  int r = uv_timer_init(env->event_loop(), &handle_);
93  CHECK_EQ(r, 0);
94  }
95 
96  static void Start(const FunctionCallbackInfo<Value>& args) {
97  TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
98 
99  CHECK(HandleWrap::IsAlive(wrap));
100 
101  int64_t timeout = args[0]->IntegerValue();
102  int err = uv_timer_start(&wrap->handle_, OnTimeout, timeout, 0);
103  args.GetReturnValue().Set(err);
104  }
105 
106  static void Stop(const FunctionCallbackInfo<Value>& args) {
107  TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());
108 
109  CHECK(HandleWrap::IsAlive(wrap));
110 
111  int err = uv_timer_stop(&wrap->handle_);
112  args.GetReturnValue().Set(err);
113  }
114 
115  static void OnTimeout(uv_timer_t* handle) {
116  TimerWrap* wrap = static_cast<TimerWrap*>(handle->data);
117  Environment* env = wrap->env();
118  HandleScope handle_scope(env->isolate());
119  Context::Scope context_scope(env->context());
120  wrap->MakeCallback(kOnTimeout, 0, nullptr);
121  }
122 
123  static void Now(const FunctionCallbackInfo<Value>& args) {
124  Environment* env = Environment::GetCurrent(args);
125  uv_update_time(env->event_loop());
126  uint64_t now = uv_now(env->event_loop());
127  CHECK(now >= env->timer_base());
128  now -= env->timer_base();
129  if (now <= 0xfffffff)
130  args.GetReturnValue().Set(static_cast<uint32_t>(now));
131  else
132  args.GetReturnValue().Set(static_cast<double>(now));
133  }
134 
135  uv_timer_t handle_;
136 };
137 
138 
139 } // anonymous namespace
140 } // namespace node
141 
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
QueryWrap * wrap
Definition: cares_wrap.cc:478
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
Definition: node_http2.cc:1172
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536