Skip to content
This repository
Newer
Older
100644 126 lines (103 sloc) 3.755 kb
55048cdf » ry
2011-03-10 Update copyright headers
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
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
22 #ifndef object_wrap_h
23 #define object_wrap_h
24
ff4a9d38 » bnoordhuis
2012-03-10 core: use proper #include directives
25 #include "node.h"
26 #include "v8.h"
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
27 #include <assert.h>
28
e8fd808d » piscisaureus
2012-08-17 windows: avoid MSVC warnings about templates not having a dll interface
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.
d38d7bea » piscisaureus
2012-08-20 unix: don't explicitly instantiate v8::Persistent<x> templates
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
e8fd808d » piscisaureus
2012-08-17 windows: avoid MSVC warnings about templates not having a dll interface
37
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
38 namespace node {
39
3e696d18 » piscisaureus
2011-11-17 Windows: make Buffer and ObjectWrap available to compiled extensions
40 class NODE_EXTERN ObjectWrap {
22c3a1e2 » ry
2009-07-10 Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
41 public:
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
42 ObjectWrap ( ) {
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
43 refs_ = 0;
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
44 }
45
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
46
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
47 virtual ~ObjectWrap ( ) {
935f8437 » ry
2010-06-04 Allow ObjectWrap destructors before Wrap()
48 if (!handle_.IsEmpty()) {
49 assert(handle_.IsNearDeath());
dbf0d6bb » ry
2010-10-04 ClearWeak on ObjectWraps. I /think/ this is the correct semantics
50 handle_.ClearWeak();
935f8437 » ry
2010-06-04 Allow ObjectWrap destructors before Wrap()
51 handle_->SetInternalField(0, v8::Undefined());
52 handle_.Dispose();
53 handle_.Clear();
54 }
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
55 }
56
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
57
22c3a1e2 » ry
2009-07-10 Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
58 template <class T>
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
59 static inline T* Unwrap (v8::Handle<v8::Object> handle) {
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
60 assert(!handle.IsEmpty());
22c3a1e2 » ry
2009-07-10 Templatize ObjectWrap::Unwrap. Remove NODE_UNWRAP macro.
61 assert(handle->InternalFieldCount() > 0);
5d42cc3a » ry
2010-09-09 Use SetPointerInInternalField
62 return static_cast<T*>(handle->GetPointerFromInternalField(0));
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
63 }
64
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
65
b06dda1f » ry
2010-01-27 ObjectWrap fixed - buffers working!
66 v8::Persistent<v8::Object> handle_; // ro
67
68 protected:
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
69 inline void Wrap (v8::Handle<v8::Object> handle) {
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
70 assert(handle_.IsEmpty());
71 assert(handle->InternalFieldCount() > 0);
72 handle_ = v8::Persistent<v8::Object>::New(handle);
5d42cc3a » ry
2010-09-09 Use SetPointerInInternalField
73 handle_->SetPointerInInternalField(0, this);
0462b5d1 » ry
2009-07-23 ObjectWrap: MakeWeak again after each Weak callback.
74 MakeWeak();
75 }
76
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
77
78 inline void MakeWeak (void) {
0462b5d1 » ry
2009-07-23 ObjectWrap: MakeWeak again after each Weak callback.
79 handle_.MakeWeak(this, WeakCallback);
061811f3 » ry
2011-09-05 ObjectWraps should be MarkIndependent
80 handle_.MarkIndependent();
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
81 }
82
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
83 /* Ref() marks the object as being attached to an event loop.
84 * Refed objects will not be garbage collected, even if
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
85 * all references are lost.
86 */
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
87 virtual void Ref() {
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
88 assert(!handle_.IsEmpty());
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
89 refs_++;
b06dda1f » ry
2010-01-27 ObjectWrap fixed - buffers working!
90 handle_.ClearWeak();
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
91 }
116f4dea » ry
2009-08-26 lint
92
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
93 /* Unref() marks an object as detached from the event loop. This is its
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
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!
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
97 * (A "weak reference" means an object that only has a
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
98 * persistant handle.)
51e77c37 » ry
2009-07-11 Do not call Detach() from Server::~Server.
99 *
100 * DO NOT CALL THIS FROM DESTRUCTOR
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
101 */
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
102 virtual void Unref() {
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
103 assert(!handle_.IsEmpty());
b06dda1f » ry
2010-01-27 ObjectWrap fixed - buffers working!
104 assert(!handle_.IsWeak());
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
105 assert(refs_ > 0);
b06dda1f » ry
2010-01-27 ObjectWrap fixed - buffers working!
106 if (--refs_ == 0) { MakeWeak(); }
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
107 }
108
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
109
c1baa70f » ry
2009-12-07 Attach/Detach -> Ref/Unref
110 int refs_; // ro
0462b5d1 » ry
2009-07-23 ObjectWrap: MakeWeak again after each Weak callback.
111
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
112
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
113 private:
efdd73c8 » ry
2010-09-13 Fix style in node_object_wrap.h
114 static void WeakCallback (v8::Persistent<v8::Value> value, void *data) {
7a2ae4c9 » indutny
2013-01-04 object_wrap: add missing HandleScope
115 v8::HandleScope scope;
116
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
117 ObjectWrap *obj = static_cast<ObjectWrap*>(data);
118 assert(value == obj->handle_);
b06dda1f » ry
2010-01-27 ObjectWrap fixed - buffers working!
119 assert(!obj->refs_);
dbf0d6bb » ry
2010-10-04 ClearWeak on ObjectWraps. I /think/ this is the correct semantics
120 assert(value.IsNearDeath());
121 delete obj;
1fc4dce0 » ry
2009-07-10 Simplify and cleanup ObjectWrap.
122 }
123 };
124
125 } // namespace node
126 #endif // object_wrap_h
Something went wrong with that request. Please try again.