Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 115 lines (94 sloc) 3.35 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
25 #include <v8.h>
26 #include <assert.h>
27
28 namespace node {
29
3e696d1 @piscisaureus Windows: make Buffer and ObjectWrap available to compiled extensions
piscisaureus authored
30 class NODE_EXTERN ObjectWrap {
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
31 public:
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
32 ObjectWrap ( ) {
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
33 refs_ = 0;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
34 }
35
efdd73c @ry Fix style in node_object_wrap.h
ry authored
36
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
37 virtual ~ObjectWrap ( ) {
935f843 @ry Allow ObjectWrap destructors before Wrap()
ry authored
38 if (!handle_.IsEmpty()) {
39 assert(handle_.IsNearDeath());
dbf0d6b @ry ClearWeak on ObjectWraps. I /think/ this is the correct semantics
ry authored
40 handle_.ClearWeak();
935f843 @ry Allow ObjectWrap destructors before Wrap()
ry authored
41 handle_->SetInternalField(0, v8::Undefined());
42 handle_.Dispose();
43 handle_.Clear();
44 }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
45 }
46
efdd73c @ry Fix style in node_object_wrap.h
ry authored
47
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
48 template <class T>
efdd73c @ry Fix style in node_object_wrap.h
ry authored
49 static inline T* Unwrap (v8::Handle<v8::Object> handle) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
50 assert(!handle.IsEmpty());
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
51 assert(handle->InternalFieldCount() > 0);
5d42cc3 @ry Use SetPointerInInternalField
ry authored
52 return static_cast<T*>(handle->GetPointerFromInternalField(0));
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
53 }
54
efdd73c @ry Fix style in node_object_wrap.h
ry authored
55
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
56 v8::Persistent<v8::Object> handle_; // ro
57
58 protected:
efdd73c @ry Fix style in node_object_wrap.h
ry authored
59 inline void Wrap (v8::Handle<v8::Object> handle) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
60 assert(handle_.IsEmpty());
61 assert(handle->InternalFieldCount() > 0);
62 handle_ = v8::Persistent<v8::Object>::New(handle);
5d42cc3 @ry Use SetPointerInInternalField
ry authored
63 handle_->SetPointerInInternalField(0, this);
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
64 MakeWeak();
65 }
66
efdd73c @ry Fix style in node_object_wrap.h
ry authored
67
68 inline void MakeWeak (void) {
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
69 handle_.MakeWeak(this, WeakCallback);
061811f @ry ObjectWraps should be MarkIndependent
ry authored
70 handle_.MarkIndependent();
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
71 }
72
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
73 /* Ref() marks the object as being attached to an event loop.
74 * Refed objects will not be garbage collected, even if
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
75 * all references are lost.
76 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
77 virtual void Ref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
78 assert(!handle_.IsEmpty());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
79 refs_++;
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
80 handle_.ClearWeak();
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
81 }
116f4de @ry lint
ry authored
82
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
83 /* Unref() marks an object as detached from the event loop. This is its
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
84 * default state. When an object with a "weak" reference changes from
85 * attached to detached state it will be freed. Be careful not to access
86 * the object after making this call as it might be gone!
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
87 * (A "weak reference" means an object that only has a
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
88 * persistant handle.)
51e77c3 @ry Do not call Detach() from Server::~Server.
ry authored
89 *
90 * DO NOT CALL THIS FROM DESTRUCTOR
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
91 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
92 virtual void Unref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
93 assert(!handle_.IsEmpty());
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
94 assert(!handle_.IsWeak());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
95 assert(refs_ > 0);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
96 if (--refs_ == 0) { MakeWeak(); }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
97 }
98
efdd73c @ry Fix style in node_object_wrap.h
ry authored
99
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
100 int refs_; // ro
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
101
efdd73c @ry Fix style in node_object_wrap.h
ry authored
102
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
103 private:
efdd73c @ry Fix style in node_object_wrap.h
ry authored
104 static void WeakCallback (v8::Persistent<v8::Value> value, void *data) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
105 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
106 assert(value == obj->handle_);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
107 assert(!obj->refs_);
dbf0d6b @ry ClearWeak on ObjectWraps. I /think/ this is the correct semantics
ry authored
108 assert(value.IsNearDeath());
109 delete obj;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
110 }
111 };
112
113 } // namespace node
114 #endif // object_wrap_h
Something went wrong with that request. Please try again.