Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
base-object-inl.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_BASE_OBJECT_INL_H_
23 #define SRC_BASE_OBJECT_INL_H_
24 
25 #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26 
27 #include "base-object.h"
28 #include "env.h"
29 #include "env-inl.h"
30 #include "util.h"
31 #include "util-inl.h"
32 #include "v8.h"
33 
34 namespace node {
35 
36 inline BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> handle)
37  : persistent_handle_(env->isolate(), handle),
38  env_(env) {
39  CHECK_EQ(false, handle.IsEmpty());
40  // The zero field holds a pointer to the handle. Immediately set it to
41  // nullptr in case it's accessed by the user before construction is complete.
42  if (handle->InternalFieldCount() > 0)
43  handle->SetAlignedPointerInInternalField(0, nullptr);
44 }
45 
46 
47 inline BaseObject::~BaseObject() {
48  CHECK(persistent_handle_.IsEmpty());
49 }
50 
51 
52 inline v8::Persistent<v8::Object>& BaseObject::persistent() {
53  return persistent_handle_;
54 }
55 
56 
57 inline v8::Local<v8::Object> BaseObject::object() {
58  return PersistentToLocal(env_->isolate(), persistent_handle_);
59 }
60 
61 
62 inline Environment* BaseObject::env() const {
63  return env_;
64 }
65 
66 
67 template <typename Type>
68 inline void BaseObject::WeakCallback(
69  const v8::WeakCallbackInfo<Type>& data) {
70  Type* self = data.GetParameter();
71  self->persistent().Reset();
72  delete self;
73 }
74 
75 
76 template <typename Type>
77 inline void BaseObject::MakeWeak(Type* ptr) {
78  v8::HandleScope scope(env_->isolate());
79  v8::Local<v8::Object> handle = object();
80  CHECK_GT(handle->InternalFieldCount(), 0);
81  Wrap(handle, ptr);
82  persistent_handle_.MarkIndependent();
83  persistent_handle_.SetWeak<Type>(ptr, WeakCallback<Type>,
84  v8::WeakCallbackType::kParameter);
85 }
86 
87 
88 inline void BaseObject::ClearWeak() {
89  persistent_handle_.ClearWeak();
90 }
91 
92 } // namespace node
93 
94 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
95 
96 #endif // SRC_BASE_OBJECT_INL_H_
Environment *const env_
union node::cares_wrap::@8::CaresAsyncData::@0 data