Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 89 lines (75 sloc) 2.14 kb
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
1 #ifndef object_wrap_h
2 #define object_wrap_h
3
4 #include <v8.h>
5 #include <assert.h>
6
7 namespace node {
8
9 class ObjectWrap {
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
10 public:
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
11 ObjectWrap ( ) {
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
12 refs_ = 0;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
13 }
14
15 virtual ~ObjectWrap ( ) {
935f843 @ry Allow ObjectWrap destructors before Wrap()
ry authored
16 if (!handle_.IsEmpty()) {
17 assert(handle_.IsNearDeath());
18 handle_->SetInternalField(0, v8::Undefined());
19 handle_.Dispose();
20 handle_.Clear();
21 }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
22 }
23
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
24 template <class T>
25 static inline T* Unwrap (v8::Handle<v8::Object> handle)
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
26 {
27 assert(!handle.IsEmpty());
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
28 assert(handle->InternalFieldCount() > 0);
5d42cc3 @ry Use SetPointerInInternalField
ry authored
29 return static_cast<T*>(handle->GetPointerFromInternalField(0));
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
30 }
31
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
32 v8::Persistent<v8::Object> handle_; // ro
33
34 protected:
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
35 inline void Wrap (v8::Handle<v8::Object> handle)
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
36 {
37 assert(handle_.IsEmpty());
38 assert(handle->InternalFieldCount() > 0);
39 handle_ = v8::Persistent<v8::Object>::New(handle);
5d42cc3 @ry Use SetPointerInInternalField
ry authored
40 handle_->SetPointerInInternalField(0, this);
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
41 MakeWeak();
42 }
43
44 inline void MakeWeak (void)
45 {
46 handle_.MakeWeak(this, WeakCallback);
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
47 }
48
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
49 /* Ref() marks the object as being attached to an event loop.
50 * Refed objects will not be garbage collected, even if
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
51 * all references are lost.
52 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
53 virtual void Ref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
54 assert(!handle_.IsEmpty());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
55 refs_++;
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
56 handle_.ClearWeak();
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
57 }
116f4de @ry lint
ry authored
58
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
59 /* Unref() marks an object as detached from the event loop. This is its
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
60 * default state. When an object with a "weak" reference changes from
61 * attached to detached state it will be freed. Be careful not to access
62 * the object after making this call as it might be gone!
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
63 * (A "weak reference" means an object that only has a
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
64 * persistant handle.)
51e77c3 @ry Do not call Detach() from Server::~Server.
ry authored
65 *
66 * DO NOT CALL THIS FROM DESTRUCTOR
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
67 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
68 virtual void Unref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
69 assert(!handle_.IsEmpty());
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
70 assert(!handle_.IsWeak());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
71 assert(refs_ > 0);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
72 if (--refs_ == 0) { MakeWeak(); }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
73 }
74
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
75 int refs_; // ro
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
76
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
77 private:
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
78 static void WeakCallback (v8::Persistent<v8::Value> value, void *data)
79 {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
80 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
81 assert(value == obj->handle_);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
82 assert(!obj->refs_);
83 if (value.IsNearDeath()) delete obj;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
84 }
85 };
86
87 } // namespace node
88 #endif // object_wrap_h
Something went wrong with that request. Please try again.