Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 90 lines (76 sloc) 2.189 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);
29 return static_cast<T*>(v8::Handle<v8::External>::Cast(
30 handle->GetInternalField(0))->Value());
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
31 }
32
b06dda1 ry ObjectWrap fixed - buffers working!
ry authored
33 v8::Persistent<v8::Object> handle_; // ro
34
35 protected:
0462b5d ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
36 inline void Wrap (v8::Handle<v8::Object> handle)
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
37 {
38 assert(handle_.IsEmpty());
39 assert(handle->InternalFieldCount() > 0);
40 handle_ = v8::Persistent<v8::Object>::New(handle);
41 handle_->SetInternalField(0, v8::External::New(this));
0462b5d ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
42 MakeWeak();
43 }
44
45 inline void MakeWeak (void)
46 {
47 handle_.MakeWeak(this, WeakCallback);
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
48 }
49
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
50 /* Ref() marks the object as being attached to an event loop.
51 * Refed objects will not be garbage collected, even if
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
52 * all references are lost.
53 */
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
54 virtual void Ref() {
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
55 assert(!handle_.IsEmpty());
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
56 refs_++;
b06dda1 ry ObjectWrap fixed - buffers working!
ry authored
57 handle_.ClearWeak();
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
58 }
116f4de ry lint
ry authored
59
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
60 /* Unref() marks an object as detached from the event loop. This is its
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
61 * default state. When an object with a "weak" reference changes from
62 * attached to detached state it will be freed. Be careful not to access
63 * the object after making this call as it might be gone!
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
64 * (A "weak reference" means an object that only has a
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
65 * persistant handle.)
51e77c3 ry Do not call Detach() from Server::~Server.
ry authored
66 *
67 * DO NOT CALL THIS FROM DESTRUCTOR
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
68 */
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
69 virtual void Unref() {
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
70 assert(!handle_.IsEmpty());
b06dda1 ry ObjectWrap fixed - buffers working!
ry authored
71 assert(!handle_.IsWeak());
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
72 assert(refs_ > 0);
b06dda1 ry ObjectWrap fixed - buffers working!
ry authored
73 if (--refs_ == 0) { MakeWeak(); }
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
74 }
75
c1baa70 ry Attach/Detach -> Ref/Unref
ry authored
76 int refs_; // ro
0462b5d ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
77
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
78 private:
0462b5d ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
79 static void WeakCallback (v8::Persistent<v8::Value> value, void *data)
80 {
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
81 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
82 assert(value == obj->handle_);
b06dda1 ry ObjectWrap fixed - buffers working!
ry authored
83 assert(!obj->refs_);
84 if (value.IsNearDeath()) delete obj;
1fc4dce ry Simplify and cleanup ObjectWrap.
ry authored
85 }
86 };
87
88 } // namespace node
89 #endif // object_wrap_h
Something went wrong with that request. Please try again.