Skip to content

Commit

Permalink
timer_wrap: remove HandleScopes, check return size
Browse files Browse the repository at this point in the history
Calls from JS to C++ have an implicit HandleScope. So there is no need
to instantiate a new HandleScope in these basic cases.

Check if the returned int64_t is an SMI and cast the return value to
uint32_t instead of a double. Prevents needing to box the return value,
and saves a small amount of execution time.

Signed-off-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
trevnorris committed Sep 29, 2014
1 parent 2122a77 commit de312cf
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/timer_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ class TimerWrap : public HandleWrap {
}

static void Start(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

int64_t timeout = args[0]->IntegerValue();
Expand All @@ -108,26 +106,20 @@ class TimerWrap : public HandleWrap {
}

static void Stop(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

int err = uv_timer_stop(&wrap->handle_);
args.GetReturnValue().Set(err);
}

static void Again(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

int err = uv_timer_again(&wrap->handle_);
args.GetReturnValue().Set(err);
}

static void SetRepeat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

int64_t repeat = args[0]->IntegerValue();
Expand All @@ -136,12 +128,13 @@ class TimerWrap : public HandleWrap {
}

static void GetRepeat(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args.GetIsolate());
HandleScope scope(env->isolate());
TimerWrap* wrap = Unwrap<TimerWrap>(args.Holder());

int64_t repeat = uv_timer_get_repeat(&wrap->handle_);
args.GetReturnValue().Set(static_cast<double>(repeat));
if (repeat <= 0xfffffff)
args.GetReturnValue().Set(static_cast<uint32_t>(repeat));
else
args.GetReturnValue().Set(static_cast<double>(repeat));
}

static void OnTimeout(uv_timer_t* handle) {
Expand All @@ -153,11 +146,13 @@ class TimerWrap : public HandleWrap {
}

static void Now(const FunctionCallbackInfo<Value>& args) {
HandleScope handle_scope(args.GetIsolate());
Environment* env = Environment::GetCurrent(args.GetIsolate());
uv_update_time(env->event_loop());
double now = static_cast<double>(uv_now(env->event_loop()));
args.GetReturnValue().Set(now);
uint64_t now = uv_now(env->event_loop());
if (now <= 0xfffffff)
args.GetReturnValue().Set(static_cast<uint32_t>(now));
else
args.GetReturnValue().Set(static_cast<double>(now));
}

uv_timer_t handle_;
Expand Down

0 comments on commit de312cf

Please sign in to comment.