Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 127 lines (103 sloc) 3.755 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
e8fd808 @piscisaureus windows: avoid MSVC warnings about templates not having a dll interface
piscisaureus authored
29 // Explicitly instantiate some template classes, so we're sure they will be
30 // present in the binary / shared object. There isn't much doubt that they will
31 // be, but MSVC tends to complain about these things.
772b3bf @piscisaureus unix: don't explicitly instantiate v8::Persistent<x> templates
piscisaureus authored
32 #ifdef _MSC_VER
33 template class NODE_EXTERN v8::Persistent<v8::Object>;
34 template class NODE_EXTERN v8::Persistent<v8::FunctionTemplate>;
35 #endif
36
e8fd808 @piscisaureus windows: avoid MSVC warnings about templates not having a dll interface
piscisaureus authored
37
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
38 namespace node {
39
3e696d1 @piscisaureus Windows: make Buffer and ObjectWrap available to compiled extensions
piscisaureus authored
40 class NODE_EXTERN ObjectWrap {
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
41 public:
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
42 ObjectWrap ( ) {
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
43 refs_ = 0;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
44 }
45
efdd73c @ry Fix style in node_object_wrap.h
ry authored
46
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
47 virtual ~ObjectWrap ( ) {
935f843 @ry Allow ObjectWrap destructors before Wrap()
ry authored
48 if (!handle_.IsEmpty()) {
49 assert(handle_.IsNearDeath());
dbf0d6b @ry ClearWeak on ObjectWraps. I /think/ this is the correct semantics
ry authored
50 handle_.ClearWeak();
935f843 @ry Allow ObjectWrap destructors before Wrap()
ry authored
51 handle_->SetInternalField(0, v8::Undefined());
52 handle_.Dispose();
53 handle_.Clear();
54 }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
55 }
56
efdd73c @ry Fix style in node_object_wrap.h
ry authored
57
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
58 template <class T>
efdd73c @ry Fix style in node_object_wrap.h
ry authored
59 static inline T* Unwrap (v8::Handle<v8::Object> handle) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
60 assert(!handle.IsEmpty());
22c3a1e @ry Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
ry authored
61 assert(handle->InternalFieldCount() > 0);
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
62 return static_cast<T*>(handle->GetPointerFromInternalField(0));
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
63 }
64
efdd73c @ry Fix style in node_object_wrap.h
ry authored
65
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
66 v8::Persistent<v8::Object> handle_; // ro
67
68 protected:
efdd73c @ry Fix style in node_object_wrap.h
ry authored
69 inline void Wrap (v8::Handle<v8::Object> handle) {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
70 assert(handle_.IsEmpty());
71 assert(handle->InternalFieldCount() > 0);
72 handle_ = v8::Persistent<v8::Object>::New(handle);
51f6e6a @bnoordhuis src, test: downgrade to v8 3.14 api
bnoordhuis authored
73 handle_->SetPointerInInternalField(0, this);
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
74 MakeWeak();
75 }
76
efdd73c @ry Fix style in node_object_wrap.h
ry authored
77
78 inline void MakeWeak (void) {
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
79 handle_.MakeWeak(this, WeakCallback);
061811f @ry ObjectWraps should be MarkIndependent
ry authored
80 handle_.MarkIndependent();
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
81 }
82
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
83 /* Ref() marks the object as being attached to an event loop.
84 * Refed objects will not be garbage collected, even if
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
85 * all references are lost.
86 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
87 virtual void Ref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
88 assert(!handle_.IsEmpty());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
89 refs_++;
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
90 handle_.ClearWeak();
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
91 }
116f4de @ry lint
ry authored
92
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
93 /* Unref() marks an object as detached from the event loop. This is its
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
94 * default state. When an object with a "weak" reference changes from
95 * attached to detached state it will be freed. Be careful not to access
96 * the object after making this call as it might be gone!
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
97 * (A "weak reference" means an object that only has a
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
98 * persistant handle.)
51e77c3 @ry Do not call Detach() from Server::~Server.
ry authored
99 *
100 * DO NOT CALL THIS FROM DESTRUCTOR
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
101 */
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
102 virtual void Unref() {
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
103 assert(!handle_.IsEmpty());
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
104 assert(!handle_.IsWeak());
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
105 assert(refs_ > 0);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
106 if (--refs_ == 0) { MakeWeak(); }
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
107 }
108
efdd73c @ry Fix style in node_object_wrap.h
ry authored
109
c1baa70 @ry Attach/Detach -> Ref/Unref
ry authored
110 int refs_; // ro
0462b5d @ry ObjectWrap: MakeWeak again after each Weak callback.
ry authored
111
efdd73c @ry Fix style in node_object_wrap.h
ry authored
112
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
113 private:
efdd73c @ry Fix style in node_object_wrap.h
ry authored
114 static void WeakCallback (v8::Persistent<v8::Value> value, void *data) {
97056c0 @indutny object_wrap: add missing HandleScope
indutny authored
115 v8::HandleScope scope;
116
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
117 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
118 assert(value == obj->handle_);
b06dda1 @ry ObjectWrap fixed - buffers working!
ry authored
119 assert(!obj->refs_);
dbf0d6b @ry ClearWeak on ObjectWraps. I /think/ this is the correct semantics
ry authored
120 assert(value.IsNearDeath());
121 delete obj;
1fc4dce @ry Simplify and cleanup ObjectWrap.
ry authored
122 }
123 };
124
125 } // namespace node
126 #endif // object_wrap_h
Something went wrong with that request. Please try again.