• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-inl.h"
29 #include "util.h"
30 
31 #include "v8.h"
32 
33 namespace node {
34 
BaseObject(Environment * env,v8::Local<v8::Object> object)35 BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
36     : BaseObject(env->principal_realm(), object) {
37   // TODO(legendecas): Check the shorthand is only used in the principal realm
38   // while allowing to create a BaseObject in a vm context.
39 }
40 
41 // static
GetConstructorTemplate(Environment * env)42 v8::Local<v8::FunctionTemplate> BaseObject::GetConstructorTemplate(
43     Environment* env) {
44   return BaseObject::GetConstructorTemplate(env->isolate_data());
45 }
46 
Detach()47 void BaseObject::Detach() {
48   CHECK_GT(pointer_data()->strong_ptr_count, 0);
49   pointer_data()->is_detached = true;
50 }
51 
persistent()52 v8::Global<v8::Object>& BaseObject::persistent() {
53   return persistent_handle_;
54 }
55 
56 
object()57 v8::Local<v8::Object> BaseObject::object() const {
58   return PersistentToLocal::Default(env()->isolate(), persistent_handle_);
59 }
60 
object(v8::Isolate * isolate)61 v8::Local<v8::Object> BaseObject::object(v8::Isolate* isolate) const {
62   v8::Local<v8::Object> handle = object();
63 
64   DCHECK_EQ(handle->GetCreationContext().ToLocalChecked()->GetIsolate(),
65             isolate);
66   DCHECK_EQ(env()->isolate(), isolate);
67 
68   return handle;
69 }
70 
env()71 Environment* BaseObject::env() const {
72   return realm_->env();
73 }
74 
realm()75 Realm* BaseObject::realm() const {
76   return realm_;
77 }
78 
TagNodeObject(v8::Local<v8::Object> object)79 void BaseObject::TagNodeObject(v8::Local<v8::Object> object) {
80   DCHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
81   object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
82                                            &kNodeEmbedderId);
83 }
84 
SetInternalFields(v8::Local<v8::Object> object,void * slot)85 void BaseObject::SetInternalFields(v8::Local<v8::Object> object, void* slot) {
86   TagNodeObject(object);
87   object->SetAlignedPointerInInternalField(BaseObject::kSlot, slot);
88 }
89 
FromJSObject(v8::Local<v8::Value> value)90 BaseObject* BaseObject::FromJSObject(v8::Local<v8::Value> value) {
91   v8::Local<v8::Object> obj = value.As<v8::Object>();
92   DCHECK_GE(obj->InternalFieldCount(), BaseObject::kInternalFieldCount);
93   return static_cast<BaseObject*>(
94       obj->GetAlignedPointerFromInternalField(BaseObject::kSlot));
95 }
96 
97 template <typename T>
FromJSObject(v8::Local<v8::Value> object)98 T* BaseObject::FromJSObject(v8::Local<v8::Value> object) {
99   return static_cast<T*>(FromJSObject(object));
100 }
101 
OnGCCollect()102 void BaseObject::OnGCCollect() {
103   delete this;
104 }
105 
ClearWeak()106 void BaseObject::ClearWeak() {
107   if (has_pointer_data())
108     pointer_data()->wants_weak_jsobj = false;
109 
110   persistent_handle_.ClearWeak();
111 }
112 
IsWeakOrDetached()113 bool BaseObject::IsWeakOrDetached() const {
114   if (persistent_handle_.IsWeak()) return true;
115 
116   if (!has_pointer_data()) return false;
117   const PointerData* pd = const_cast<BaseObject*>(this)->pointer_data();
118   return pd->wants_weak_jsobj || pd->is_detached;
119 }
120 
GetDetachedness()121 v8::EmbedderGraph::Node::Detachedness BaseObject::GetDetachedness() const {
122   return IsWeakOrDetached() ? v8::EmbedderGraph::Node::Detachedness::kDetached
123                             : v8::EmbedderGraph::Node::Detachedness::kUnknown;
124 }
125 
126 template <int Field>
InternalFieldGet(v8::Local<v8::String> property,const v8::PropertyCallbackInfo<v8::Value> & info)127 void BaseObject::InternalFieldGet(
128     v8::Local<v8::String> property,
129     const v8::PropertyCallbackInfo<v8::Value>& info) {
130   info.GetReturnValue().Set(
131       info.This()->GetInternalField(Field).As<v8::Value>());
132 }
133 
134 template <int Field, bool (v8::Value::* typecheck)() const>
InternalFieldSet(v8::Local<v8::String> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<void> & info)135 void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
136                                   v8::Local<v8::Value> value,
137                                   const v8::PropertyCallbackInfo<void>& info) {
138   // This could be e.g. value->IsFunction().
139   CHECK(((*value)->*typecheck)());
140   info.This()->SetInternalField(Field, value);
141 }
142 
has_pointer_data()143 bool BaseObject::has_pointer_data() const {
144   return pointer_data_ != nullptr;
145 }
146 
147 template <typename T, bool kIsWeak>
148 BaseObject::PointerData*
pointer_data()149 BaseObjectPtrImpl<T, kIsWeak>::pointer_data() const {
150   if constexpr (kIsWeak) {
151     return data_.pointer_data;
152   }
153   if (get_base_object() == nullptr) {
154     return nullptr;
155   }
156   return get_base_object()->pointer_data();
157 }
158 
159 template <typename T, bool kIsWeak>
get_base_object()160 BaseObject* BaseObjectPtrImpl<T, kIsWeak>::get_base_object() const {
161   if constexpr (kIsWeak) {
162     if (pointer_data() == nullptr) {
163       return nullptr;
164     }
165     return pointer_data()->self;
166   }
167   return data_.target;
168 }
169 
170 template <typename T, bool kIsWeak>
~BaseObjectPtrImpl()171 BaseObjectPtrImpl<T, kIsWeak>::~BaseObjectPtrImpl() {
172   if constexpr (kIsWeak) {
173     if (pointer_data() != nullptr &&
174         --pointer_data()->weak_ptr_count == 0 &&
175         pointer_data()->self == nullptr) {
176       delete pointer_data();
177     }
178   } else if (get() != nullptr) {
179     get()->decrease_refcount();
180   }
181 }
182 
183 template <typename T, bool kIsWeak>
BaseObjectPtrImpl()184 BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl() {
185   data_.target = nullptr;
186 }
187 
188 template <typename T, bool kIsWeak>
BaseObjectPtrImpl(T * target)189 BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(T* target)
190   : BaseObjectPtrImpl() {
191   if (target == nullptr) return;
192   if constexpr (kIsWeak) {
193     data_.pointer_data = target->pointer_data();
194     CHECK_NOT_NULL(pointer_data());
195     pointer_data()->weak_ptr_count++;
196   } else {
197     data_.target = target;
198     CHECK_NOT_NULL(pointer_data());
199     get()->increase_refcount();
200   }
201 }
202 
203 template <typename T, bool kIsWeak>
204 template <typename U, bool kW>
BaseObjectPtrImpl(const BaseObjectPtrImpl<U,kW> & other)205 BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(
206     const BaseObjectPtrImpl<U, kW>& other)
207   : BaseObjectPtrImpl(other.get()) {}
208 
209 template <typename T, bool kIsWeak>
BaseObjectPtrImpl(const BaseObjectPtrImpl & other)210 BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(const BaseObjectPtrImpl& other)
211   : BaseObjectPtrImpl(other.get()) {}
212 
213 template <typename T, bool kIsWeak>
214 template <typename U, bool kW>
215 BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
216     const BaseObjectPtrImpl<U, kW>& other) {
217   if (other.get() == get()) return *this;
218   this->~BaseObjectPtrImpl();
219   return *new (this) BaseObjectPtrImpl(other);
220 }
221 
222 template <typename T, bool kIsWeak>
223 BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
224     const BaseObjectPtrImpl& other) {
225   if (other.get() == get()) return *this;
226   this->~BaseObjectPtrImpl();
227   return *new (this) BaseObjectPtrImpl(other);
228 }
229 
230 template <typename T, bool kIsWeak>
BaseObjectPtrImpl(BaseObjectPtrImpl && other)231 BaseObjectPtrImpl<T, kIsWeak>::BaseObjectPtrImpl(BaseObjectPtrImpl&& other)
232   : data_(other.data_) {
233   if constexpr (kIsWeak)
234     other.data_.target = nullptr;
235   else
236     other.data_.pointer_data = nullptr;
237 }
238 
239 template <typename T, bool kIsWeak>
240 BaseObjectPtrImpl<T, kIsWeak>& BaseObjectPtrImpl<T, kIsWeak>::operator=(
241     BaseObjectPtrImpl&& other) {
242   if (&other == this) return *this;
243   this->~BaseObjectPtrImpl();
244   return *new (this) BaseObjectPtrImpl(std::move(other));
245 }
246 
247 template <typename T, bool kIsWeak>
reset(T * ptr)248 void BaseObjectPtrImpl<T, kIsWeak>::reset(T* ptr) {
249   *this = BaseObjectPtrImpl(ptr);
250 }
251 
252 template <typename T, bool kIsWeak>
get()253 T* BaseObjectPtrImpl<T, kIsWeak>::get() const {
254   return static_cast<T*>(get_base_object());
255 }
256 
257 template <typename T, bool kIsWeak>
258 T& BaseObjectPtrImpl<T, kIsWeak>::operator*() const {
259   return *get();
260 }
261 
262 template <typename T, bool kIsWeak>
263 T* BaseObjectPtrImpl<T, kIsWeak>::operator->() const {
264   return get();
265 }
266 
267 template <typename T, bool kIsWeak>
268 BaseObjectPtrImpl<T, kIsWeak>::operator bool() const {
269   return get() != nullptr;
270 }
271 
272 template <typename T, bool kIsWeak>
273 template <typename U, bool kW>
274 bool BaseObjectPtrImpl<T, kIsWeak>::operator ==(
275     const BaseObjectPtrImpl<U, kW>& other) const {
276   return get() == other.get();
277 }
278 
279 template <typename T, bool kIsWeak>
280 template <typename U, bool kW>
281 bool BaseObjectPtrImpl<T, kIsWeak>::operator !=(
282     const BaseObjectPtrImpl<U, kW>& other) const {
283   return get() != other.get();
284 }
285 
286 template <typename T, typename... Args>
MakeBaseObject(Args &&...args)287 BaseObjectPtr<T> MakeBaseObject(Args&&... args) {
288   return BaseObjectPtr<T>(new T(std::forward<Args>(args)...));
289 }
290 
291 template <typename T, typename... Args>
MakeDetachedBaseObject(Args &&...args)292 BaseObjectPtr<T> MakeDetachedBaseObject(Args&&... args) {
293   BaseObjectPtr<T> target = MakeBaseObject<T>(std::forward<Args>(args)...);
294   target->Detach();
295   return target;
296 }
297 
298 }  // namespace node
299 
300 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
301 
302 #endif  // SRC_BASE_OBJECT_INL_H_
303