diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2013-07-03 06:23:44 +0400 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-07-06 19:44:44 +0400 |
commit | 110a9cd8db515c4d1a9ac5cd8837291da7c6c5ea (patch) | |
tree | 71e5a14a98131d89d670f842eb36bfcccab00b7b /src/timer_wrap.cc | |
parent | 9b3de60d3537df657e75887436a5b1df5ed80c2d (diff) |
lib, src: upgrade after v8 api change
This is a big commit that touches just about every file in the src/
directory. The V8 API has changed in significant ways. The most
important changes are:
* Binding functions take a const v8::FunctionCallbackInfo<T>& argument
rather than a const v8::Arguments& argument.
* Binding functions return void rather than v8::Handle<v8::Value>. The
return value is returned with the args.GetReturnValue().Set() family
of functions.
* v8::Persistent<T> no longer derives from v8::Handle<T> and no longer
allows you to directly dereference the object that the persistent
handle points to. This means that the common pattern of caching
oft-used JS values in a persistent handle no longer quite works,
you first need to reconstruct a v8::Local<T> from the persistent
handle with the Local<T>::New(isolate, persistent) factory method.
A handful of (internal) convenience classes and functions have been
added to make dealing with the new API a little easier.
The most visible one is node::Cached<T>, which wraps a v8::Persistent<T>
with some template sugar. It can hold arbitrary types but so far it's
exclusively used for v8::Strings (which was by far the most commonly
cached handle type.)
Diffstat (limited to 'src/timer_wrap.cc')
-rw-r--r-- | src/timer_wrap.cc | 58 |
1 files changed, 18 insertions, 40 deletions
diff --git a/src/timer_wrap.cc b/src/timer_wrap.cc index 23abc5c4186..40bc3edd49c 100644 --- a/src/timer_wrap.cc +++ b/src/timer_wrap.cc @@ -24,19 +24,18 @@ namespace node { -using v8::Arguments; using v8::Function; +using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Handle; using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Object; -using v8::Persistent; using v8::String; using v8::Value; -static Persistent<String> ontimeout_sym; +static Cached<String> ontimeout_sym; class TimerWrap : public HandleWrap { public: @@ -61,23 +60,19 @@ class TimerWrap : public HandleWrap { NODE_SET_PROTOTYPE_METHOD(constructor, "getRepeat", GetRepeat); NODE_SET_PROTOTYPE_METHOD(constructor, "again", Again); - ontimeout_sym = NODE_PSYMBOL("ontimeout"); + ontimeout_sym = String::New("ontimeout"); target->Set(String::NewSymbol("Timer"), constructor->GetFunction()); } private: - static Handle<Value> New(const Arguments& args) { + static void New(const FunctionCallbackInfo<Value>& args) { // This constructor should not be exposed to public javascript. // Therefore we assert that we are not trying to call this as a // normal function. assert(args.IsConstructCall()); - HandleScope scope(node_isolate); - TimerWrap *wrap = new TimerWrap(args.This()); - assert(wrap); - - return scope.Close(args.This()); + new TimerWrap(args.This()); } TimerWrap(Handle<Object> object) @@ -89,67 +84,51 @@ class TimerWrap : public HandleWrap { ~TimerWrap() { } - static Handle<Value> Start(const Arguments& args) { + static void Start(const FunctionCallbackInfo<Value>& args) { HandleScope scope(node_isolate); - UNWRAP(TimerWrap) int64_t timeout = args[0]->IntegerValue(); int64_t repeat = args[1]->IntegerValue(); - int r = uv_timer_start(&wrap->handle_, OnTimeout, timeout, repeat); - if (r) SetErrno(uv_last_error(uv_default_loop())); - - return scope.Close(Integer::New(r, node_isolate)); + args.GetReturnValue().Set(r); } - static Handle<Value> Stop(const Arguments& args) { + static void Stop(const FunctionCallbackInfo<Value>& args) { HandleScope scope(node_isolate); - UNWRAP(TimerWrap) int r = uv_timer_stop(&wrap->handle_); - if (r) SetErrno(uv_last_error(uv_default_loop())); - - return scope.Close(Integer::New(r, node_isolate)); + args.GetReturnValue().Set(r); } - static Handle<Value> Again(const Arguments& args) { + static void Again(const FunctionCallbackInfo<Value>& args) { HandleScope scope(node_isolate); - UNWRAP(TimerWrap) int r = uv_timer_again(&wrap->handle_); - if (r) SetErrno(uv_last_error(uv_default_loop())); - - return scope.Close(Integer::New(r, node_isolate)); + args.GetReturnValue().Set(r); } - static Handle<Value> SetRepeat(const Arguments& args) { + static void SetRepeat(const FunctionCallbackInfo<Value>& args) { HandleScope scope(node_isolate); - UNWRAP(TimerWrap) int64_t repeat = args[0]->IntegerValue(); - uv_timer_set_repeat(&wrap->handle_, repeat); - - return scope.Close(Integer::New(0, node_isolate)); + args.GetReturnValue().Set(0); } - static Handle<Value> GetRepeat(const Arguments& args) { + static void GetRepeat(const FunctionCallbackInfo<Value>& args) { HandleScope scope(node_isolate); - UNWRAP(TimerWrap) int64_t repeat = uv_timer_get_repeat(&wrap->handle_); - if (repeat < 0) SetErrno(uv_last_error(uv_default_loop())); - - return scope.Close(Integer::New(repeat, node_isolate)); + args.GetReturnValue().Set(static_cast<double>(repeat)); } static void OnTimeout(uv_timer_t* handle, int status) { @@ -159,14 +138,13 @@ class TimerWrap : public HandleWrap { assert(wrap); Local<Value> argv[1] = { Integer::New(status, node_isolate) }; - MakeCallback(wrap->object_, ontimeout_sym, ARRAY_SIZE(argv), argv); + MakeCallback(wrap->object(), ontimeout_sym, ARRAY_SIZE(argv), argv); } - static Handle<Value> Now(const Arguments& args) { + static void Now(const FunctionCallbackInfo<Value>& args) { HandleScope scope(node_isolate); - double now = static_cast<double>(uv_now(uv_default_loop())); - return scope.Close(v8::Number::New(now)); + args.GetReturnValue().Set(now); } uv_timer_t handle_; |