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/node_object_wrap.h | |
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/node_object_wrap.h')
-rw-r--r-- | src/node_object_wrap.h | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/src/node_object_wrap.h b/src/node_object_wrap.h index 19f536dfe9a..990c82b1dbd 100644 --- a/src/node_object_wrap.h +++ b/src/node_object_wrap.h @@ -38,49 +38,56 @@ namespace node { class NODE_EXTERN ObjectWrap { - public: - ObjectWrap ( ) { +public: + ObjectWrap() { refs_ = 0; } - virtual ~ObjectWrap ( ) { - if (!handle_.IsEmpty()) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - assert(handle_.IsNearDeath(isolate)); - handle_.ClearWeak(isolate); - handle_->SetAlignedPointerInInternalField(0, 0); - handle_.Dispose(isolate); - handle_.Clear(); - } + virtual ~ObjectWrap() { + if (persistent().IsEmpty()) return; + assert(persistent().IsNearDeath()); + persistent().ClearWeak(); + persistent().Dispose(); } template <class T> - static inline T* Unwrap (v8::Handle<v8::Object> handle) { + static inline T* Unwrap(v8::Handle<v8::Object> handle) { assert(!handle.IsEmpty()); assert(handle->InternalFieldCount() > 0); return static_cast<T*>(handle->GetAlignedPointerFromInternalField(0)); } - v8::Persistent<v8::Object> handle_; // ro + inline v8::Local<v8::Object> handle() { + return handle(v8::Isolate::GetCurrent()); + } + + + inline v8::Local<v8::Object> handle(v8::Isolate* isolate) { + return v8::Local<v8::Object>::New(isolate, persistent()); + } + - protected: - inline void Wrap (v8::Handle<v8::Object> handle) { - assert(handle_.IsEmpty()); + inline v8::Persistent<v8::Object>& persistent() { + return handle_; + } + + +protected: + inline void Wrap(v8::Handle<v8::Object> handle) { + assert(persistent().IsEmpty()); assert(handle->InternalFieldCount() > 0); - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - handle_ = v8::Persistent<v8::Object>::New(isolate, handle); - handle_->SetAlignedPointerInInternalField(0, this); + handle->SetAlignedPointerInInternalField(0, this); + persistent().Reset(v8::Isolate::GetCurrent(), handle); MakeWeak(); } - inline void MakeWeak (void) { - v8::Isolate* isolate = v8::Isolate::GetCurrent(); - handle_.MakeWeak(isolate, this, WeakCallback); - handle_.MarkIndependent(isolate); + inline void MakeWeak(void) { + persistent().MakeWeak(this, WeakCallback); + persistent().MarkIndependent(); } /* Ref() marks the object as being attached to an event loop. @@ -88,9 +95,9 @@ class NODE_EXTERN ObjectWrap { * all references are lost. */ virtual void Ref() { - assert(!handle_.IsEmpty()); + assert(!persistent().IsEmpty()); + persistent().ClearWeak(); refs_++; - handle_.ClearWeak(v8::Isolate::GetCurrent()); } /* Unref() marks an object as detached from the event loop. This is its @@ -103,26 +110,26 @@ class NODE_EXTERN ObjectWrap { * DO NOT CALL THIS FROM DESTRUCTOR */ virtual void Unref() { - assert(!handle_.IsEmpty()); - assert(!handle_.IsWeak(v8::Isolate::GetCurrent())); + assert(!persistent().IsEmpty()); + assert(!persistent().IsWeak()); assert(refs_ > 0); - if (--refs_ == 0) { MakeWeak(); } + if (--refs_ == 0) MakeWeak(); } + int refs_; // ro - int refs_; // ro - - - private: +private: static void WeakCallback(v8::Isolate* isolate, v8::Persistent<v8::Object>* pobj, ObjectWrap* wrap) { v8::HandleScope scope(isolate); assert(wrap->refs_ == 0); - assert(*pobj == wrap->handle_); - assert((*pobj).IsNearDeath(isolate)); + assert(*pobj == wrap->persistent()); + assert((*pobj).IsNearDeath()); delete wrap; } + + v8::Persistent<v8::Object> handle_; }; } // namespace node |