Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 116 lines (95 sloc) 3.368 kB
55048cd @ry Update copyright headers
ry authored
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
22 #ifndef object_wrap_h
23 #define object_wrap_h
24
ff4a9d3 @bnoordhuis core: use proper #include directives
bnoordhuis authored
25 #include "node.h"
26 #include "v8.h"
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
27 #include <assert.h>
28
29 namespace node {
30
3e696d1 @piscisaureus Windows: make Buffer and ObjectWrap available to compiled extensions
piscisaureus authored
31 class NODE_EXTERN ObjectWrap {
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
32 public:
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
33 ObjectWrap ( ) {
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
34 refs_ = 0;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
35 }
36
efdd73c @ry Fix style in node_object_wrap.h
ry authored
37
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
38 virtual ~ObjectWrap ( ) {
935f843 @ry Allow ObjectWrap destructors before Wrap()
ry authored
39 if (!handle_.IsEmpty()) {
40 assert(handle_.IsNearDeath());
dbf0d6b @ry ClearWeak on ObjectWraps. I /think/ this is the correct semantics
ry authored
41 handle_.ClearWeak();
935f843 @ry Allow ObjectWrap destructors before Wrap()
ry authored
42 handle_->SetInternalField(0, v8::Undefined());
43 handle_.Dispose();
44 handle_.Clear();
45 }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
46 }
47
efdd73c @ry Fix style in node_object_wrap.h
ry authored
48
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
49 template <class T>
efdd73c @ry Fix style in node_object_wrap.h
ry authored
50 static inline T* Unwrap (v8::Handle<v8::Object> handle) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
51 assert(!handle.IsEmpty());
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
52 assert(handle->InternalFieldCount() > 0);
5d42cc3 @ry Use SetPointerInInternalField
ry authored
53 return static_cast<T*>(handle->GetPointerFromInternalField(0));
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
54 }
55
efdd73c @ry Fix style in node_object_wrap.h
ry authored
56
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
57 v8::Persistent<v8::Object> handle_; // ro
58
59 protected:
efdd73c @ry Fix style in node_object_wrap.h
ry authored
60 inline void Wrap (v8::Handle<v8::Object> handle) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
61 assert(handle_.IsEmpty());
62 assert(handle->InternalFieldCount() > 0);
63 handle_ = v8::Persistent<v8::Object>::New(handle);
5d42cc3 @ry Use SetPointerInInternalField
ry authored
64 handle_->SetPointerInInternalField(0, this);
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
65 MakeWeak();
66 }
67
efdd73c @ry Fix style in node_object_wrap.h
ry authored
68
69 inline void MakeWeak (void) {
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
70 handle_.MakeWeak(this, WeakCallback);
061811f @ry ObjectWraps should be MarkIndependent
ry authored
71 handle_.MarkIndependent();
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
72 }
73
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
74 /* Ref() marks the object as being attached to an event loop.
75 * Refed objects will not be garbage collected, even if
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
76 * all references are lost.
77 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
78 virtual void Ref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
79 assert(!handle_.IsEmpty());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
80 refs_++;
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
81 handle_.ClearWeak();
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
82 }
116f4de @ry lint
ry authored
83
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
84 /* Unref() marks an object as detached from the event loop. This is its
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
85 * default state. When an object with a "weak" reference changes from
86 * attached to detached state it will be freed. Be careful not to access
87 * the object after making this call as it might be gone!
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
88 * (A "weak reference" means an object that only has a
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
89 * persistant handle.)
51e77c3 @ry Do not call Detach() from Server::~Server.
ry authored
90 *
91 * DO NOT CALL THIS FROM DESTRUCTOR
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
92 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
93 virtual void Unref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
94 assert(!handle_.IsEmpty());
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
95 assert(!handle_.IsWeak());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
96 assert(refs_ > 0);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
97 if (--refs_ == 0) { MakeWeak(); }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
98 }
99
efdd73c @ry Fix style in node_object_wrap.h
ry authored
100
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
101 int refs_; // ro
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
102
efdd73c @ry Fix style in node_object_wrap.h
ry authored
103
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
104 private:
efdd73c @ry Fix style in node_object_wrap.h
ry authored
105 static void WeakCallback (v8::Persistent<v8::Value> value, void *data) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
106 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
107 assert(value == obj->handle_);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
108 assert(!obj->refs_);
dbf0d6b @ry ClearWeak on ObjectWraps. I /think/ this is the correct semantics
ry authored
109 assert(value.IsNearDeath());
110 delete obj;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
111 }
112 };
113
114 } // namespace node
115 #endif // object_wrap_h
Something went wrong with that request. Please try again.