Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_object_wrap.h
Go to the documentation of this file.
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 
22 #ifndef SRC_NODE_OBJECT_WRAP_H_
23 #define SRC_NODE_OBJECT_WRAP_H_
24 
25 #include "v8.h"
26 #include <assert.h>
27 
28 
29 namespace node {
30 
31 class ObjectWrap {
32  public:
34  refs_ = 0;
35  }
36 
37 
38  virtual ~ObjectWrap() {
39  if (persistent().IsEmpty())
40  return;
41  assert(persistent().IsNearDeath());
42  persistent().ClearWeak();
43  persistent().Reset();
44  }
45 
46 
47  template <class T>
48  static inline T* Unwrap(v8::Local<v8::Object> handle) {
49  assert(!handle.IsEmpty());
50  assert(handle->InternalFieldCount() > 0);
51  // Cast to ObjectWrap before casting to T. A direct cast from void
52  // to T won't work right when T has more than one base class.
53  void* ptr = handle->GetAlignedPointerFromInternalField(0);
54  ObjectWrap* wrap = static_cast<ObjectWrap*>(ptr);
55  return static_cast<T*>(wrap);
56  }
57 
58 
59  inline v8::Local<v8::Object> handle() {
60  return handle(v8::Isolate::GetCurrent());
61  }
62 
63 
64  inline v8::Local<v8::Object> handle(v8::Isolate* isolate) {
65  return v8::Local<v8::Object>::New(isolate, persistent());
66  }
67 
68 
69  inline v8::Persistent<v8::Object>& persistent() {
70  return handle_;
71  }
72 
73 
74  protected:
75  inline void Wrap(v8::Local<v8::Object> handle) {
76  assert(persistent().IsEmpty());
77  assert(handle->InternalFieldCount() > 0);
78  handle->SetAlignedPointerInInternalField(0, this);
79  persistent().Reset(v8::Isolate::GetCurrent(), handle);
80  MakeWeak();
81  }
82 
83 
84  inline void MakeWeak(void) {
85  persistent().SetWeak(this, WeakCallback, v8::WeakCallbackType::kParameter);
86  persistent().MarkIndependent();
87  }
88 
89  /* Ref() marks the object as being attached to an event loop.
90  * Refed objects will not be garbage collected, even if
91  * all references are lost.
92  */
93  virtual void Ref() {
94  assert(!persistent().IsEmpty());
95  persistent().ClearWeak();
96  refs_++;
97  }
98 
99  /* Unref() marks an object as detached from the event loop. This is its
100  * default state. When an object with a "weak" reference changes from
101  * attached to detached state it will be freed. Be careful not to access
102  * the object after making this call as it might be gone!
103  * (A "weak reference" means an object that only has a
104  * persistent handle.)
105  *
106  * DO NOT CALL THIS FROM DESTRUCTOR
107  */
108  virtual void Unref() {
109  assert(!persistent().IsEmpty());
110  assert(!persistent().IsWeak());
111  assert(refs_ > 0);
112  if (--refs_ == 0)
113  MakeWeak();
114  }
115 
116  int refs_; // ro
117 
118  private:
119  static void WeakCallback(
120  const v8::WeakCallbackInfo<ObjectWrap>& data) {
121  ObjectWrap* wrap = data.GetParameter();
122  assert(wrap->refs_ == 0);
123  wrap->handle_.Reset();
124  delete wrap;
125  }
126 
127  v8::Persistent<v8::Object> handle_;
128 };
129 
130 } // namespace node
131 
132 #endif // SRC_NODE_OBJECT_WRAP_H_
static T * Unwrap(v8::Local< v8::Object > handle)
void Wrap(v8::Local< v8::Object > handle)
QueryWrap * wrap
Definition: cares_wrap.cc:478
virtual void Unref()
union node::cares_wrap::@8::CaresAsyncData::@0 data
v8::Local< v8::Object > handle(v8::Isolate *isolate)
virtual void Ref()
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
v8::Local< v8::Object > handle()
v8::Persistent< v8::Object > & persistent()