v8 10.2.154 (node 18.16.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-local-handle.h
Go to the documentation of this file.
1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef INCLUDE_V8_LOCAL_HANDLE_H_
6#define INCLUDE_V8_LOCAL_HANDLE_H_
7
8#include <stddef.h>
9
10#include <type_traits>
11
12#include "v8-internal.h" // NOLINT(build/include_directory)
13
14namespace v8 {
15
16class Boolean;
17template <class T>
18class BasicTracedReference;
19class Context;
20class EscapableHandleScope;
21template <class F>
22class Eternal;
23template <class F>
24class FunctionCallbackInfo;
25class Isolate;
26template <class F>
27class MaybeLocal;
28template <class T>
29class NonCopyablePersistentTraits;
30class Object;
31template <class T, class M = NonCopyablePersistentTraits<T>>
32class Persistent;
33template <class T>
34class PersistentBase;
35template <class F1, class F2, class F3>
36class PersistentValueMapBase;
37template <class F1, class F2>
38class PersistentValueVector;
39class Primitive;
40class Private;
41template <class F>
42class PropertyCallbackInfo;
43template <class F>
44class ReturnValue;
45class String;
46template <class F>
47class Traced;
48template <class F>
49class TracedReference;
51class Utils;
52
53namespace internal {
54template <typename T>
56} // namespace internal
57
58namespace api_internal {
59// Called when ToLocalChecked is called on an empty Local.
61} // namespace api_internal
62
78 public:
79 explicit HandleScope(Isolate* isolate);
80
82
86 static int NumberOfHandles(Isolate* isolate);
87
89 return reinterpret_cast<Isolate*>(isolate_);
90 }
91
92 HandleScope(const HandleScope&) = delete;
93 void operator=(const HandleScope&) = delete;
94
95 protected:
97
98 void Initialize(Isolate* isolate);
99
100 static internal::Address* CreateHandle(internal::Isolate* isolate,
101 internal::Address value);
102
103 private:
104 // Declaring operator new and delete as deleted is not spec compliant.
105 // Therefore declare them private instead to disable dynamic alloc
106 void* operator new(size_t size);
107 void* operator new[](size_t size);
108 void operator delete(void*, size_t);
109 void operator delete[](void*, size_t);
110
111 internal::Isolate* isolate_;
112 internal::Address* prev_next_;
113 internal::Address* prev_limit_;
114
115 // Local::New uses CreateHandle with an Isolate* parameter.
116 template <class F>
117 friend class Local;
118
119 // Object::GetInternalField and Context::GetEmbedderData use CreateHandle with
120 // a HeapObject in their shortcuts.
121 friend class Object;
122 friend class Context;
123};
124
154template <class T>
155class Local {
156 public:
157 V8_INLINE Local() : val_(nullptr) {}
158 template <class S>
159 V8_INLINE Local(Local<S> that) : val_(reinterpret_cast<T*>(*that)) {
165 static_assert(std::is_base_of<T, S>::value, "type check");
166 }
167
171 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
172
176 V8_INLINE void Clear() { val_ = nullptr; }
177
178 V8_INLINE T* operator->() const { return val_; }
179
180 V8_INLINE T* operator*() const { return val_; }
181
192 template <class S>
193 V8_INLINE bool operator==(const Local<S>& that) const {
194 internal::Address* a = reinterpret_cast<internal::Address*>(this->val_);
195 internal::Address* b = reinterpret_cast<internal::Address*>(that.val_);
196 if (a == nullptr) return b == nullptr;
197 if (b == nullptr) return false;
198 return *a == *b;
199 }
200
201 template <class S>
202 V8_INLINE bool operator==(const PersistentBase<S>& that) const {
203 internal::Address* a = reinterpret_cast<internal::Address*>(this->val_);
204 internal::Address* b = reinterpret_cast<internal::Address*>(that.val_);
205 if (a == nullptr) return b == nullptr;
206 if (b == nullptr) return false;
207 return *a == *b;
208 }
209
220 template <class S>
221 V8_INLINE bool operator!=(const Local<S>& that) const {
222 return !operator==(that);
223 }
224
225 template <class S>
226 V8_INLINE bool operator!=(const Persistent<S>& that) const {
227 return !operator==(that);
228 }
229
235 template <class S>
237#ifdef V8_ENABLE_CHECKS
238 // If we're going to perform the type check then we have to check
239 // that the handle isn't empty before doing the checked cast.
240 if (that.IsEmpty()) return Local<T>();
241#endif
242 return Local<T>(T::Cast(*that));
243 }
244
250 template <class S>
252 return Local<S>::Cast(*this);
253 }
254
260 V8_INLINE static Local<T> New(Isolate* isolate, Local<T> that) {
261 return New(isolate, that.val_);
262 }
263
264 V8_INLINE static Local<T> New(Isolate* isolate,
265 const PersistentBase<T>& that) {
266 return New(isolate, that.val_);
267 }
268
269 V8_INLINE static Local<T> New(Isolate* isolate,
270 const BasicTracedReference<T>& that) {
271 return New(isolate, *that);
272 }
273
274 private:
276 friend class Utils;
277 template <class F>
278 friend class Eternal;
279 template <class F>
280 friend class PersistentBase;
281 template <class F, class M>
282 friend class Persistent;
283 template <class F>
284 friend class Local;
285 template <class F>
286 friend class MaybeLocal;
287 template <class F>
289 template <class F>
291 friend class String;
292 friend class Object;
293 friend class Context;
294 friend class Isolate;
295 friend class Private;
296 template <class F>
298 friend Local<Primitive> Undefined(Isolate* isolate);
299 friend Local<Primitive> Null(Isolate* isolate);
300 friend Local<Boolean> True(Isolate* isolate);
301 friend Local<Boolean> False(Isolate* isolate);
302 friend class HandleScope;
304 template <class F1, class F2, class F3>
306 template <class F1, class F2>
308 template <class F>
309 friend class ReturnValue;
310 template <class F>
311 friend class Traced;
312 template <class F>
314 template <class F>
315 friend class TracedReference;
316
317 explicit V8_INLINE Local(T* that) : val_(that) {}
318 V8_INLINE static Local<T> New(Isolate* isolate, T* that) {
319 if (that == nullptr) return Local<T>();
320 T* that_ptr = that;
321 internal::Address* p = reinterpret_cast<internal::Address*>(that_ptr);
322 return Local<T>(reinterpret_cast<T*>(HandleScope::CreateHandle(
323 reinterpret_cast<internal::Isolate*>(isolate), *p)));
324 }
325 T* val_;
326};
327
328#if !defined(V8_IMMINENT_DEPRECATION_WARNINGS)
329// Handle is an alias for Local for historical reasons.
330template <class T>
332#endif
333
344template <class T>
346 public:
347 V8_INLINE MaybeLocal() : val_(nullptr) {}
348 template <class S>
349 V8_INLINE MaybeLocal(Local<S> that) : val_(reinterpret_cast<T*>(*that)) {
350 static_assert(std::is_base_of<T, S>::value, "type check");
351 }
352
353 V8_INLINE bool IsEmpty() const { return val_ == nullptr; }
354
359 template <class S>
361 out->val_ = IsEmpty() ? nullptr : this->val_;
362 return !IsEmpty();
363 }
364
370 if (V8_UNLIKELY(val_ == nullptr)) api_internal::ToLocalEmpty();
371 return Local<T>(val_);
372 }
373
378 template <class S>
379 V8_INLINE Local<S> FromMaybe(Local<S> default_value) const {
380 return IsEmpty() ? default_value : Local<S>(val_);
381 }
382
383 private:
384 T* val_;
385};
386
392 public:
393 explicit EscapableHandleScope(Isolate* isolate);
395
400 template <class T>
402 internal::Address* slot =
403 Escape(reinterpret_cast<internal::Address*>(*value));
404 return Local<T>(reinterpret_cast<T*>(slot));
405 }
406
407 template <class T>
409 return Escape(value.FromMaybe(Local<T>()));
410 }
411
413 void operator=(const EscapableHandleScope&) = delete;
414
415 private:
416 // Declaring operator new and delete as deleted is not spec compliant.
417 // Therefore declare them private instead to disable dynamic alloc
418 void* operator new(size_t size);
419 void* operator new[](size_t size);
420 void operator delete(void*, size_t);
421 void operator delete[](void*, size_t);
422
423 internal::Address* Escape(internal::Address* escape_value);
424 internal::Address* escape_slot_;
425};
426
433 public:
434 explicit SealHandleScope(Isolate* isolate);
436
438 void operator=(const SealHandleScope&) = delete;
439
440 private:
441 // Declaring operator new and delete as deleted is not spec compliant.
442 // Therefore declare them private instead to disable dynamic alloc
443 void* operator new(size_t size);
444 void* operator new[](size_t size);
445 void operator delete(void*, size_t);
446 void operator delete[](void*, size_t);
447
448 internal::Isolate* const isolate_;
449 internal::Address* prev_limit_;
450 int prev_sealed_level_;
451};
452
453} // namespace v8
454
455#endif // INCLUDE_V8_LOCAL_HANDLE_H_
EscapableHandleScope(const EscapableHandleScope &)=delete
void operator=(const EscapableHandleScope &)=delete
V8_INLINE Local< T > Escape(Local< T > value)
EscapableHandleScope(Isolate *isolate)
V8_INLINE MaybeLocal< T > EscapeMaybe(MaybeLocal< T > value)
V8_INLINE ~EscapableHandleScope()=default
void Initialize(Isolate *isolate)
V8_INLINE HandleScope()=default
static int NumberOfHandles(Isolate *isolate)
V8_INLINE Isolate * GetIsolate() const
static internal::Address * CreateHandle(internal::Isolate *isolate, internal::Address value)
HandleScope(const HandleScope &)=delete
HandleScope(Isolate *isolate)
void operator=(const HandleScope &)=delete
friend Local< Primitive > Null(Isolate *isolate)
V8_INLINE T * operator->() const
V8_INLINE Local(Local< S > that)
V8_INLINE bool operator!=(const Local< S > &that) const
static V8_INLINE Local< T > New(Isolate *isolate, const PersistentBase< T > &that)
static V8_INLINE Local< T > Cast(Local< S > that)
V8_INLINE bool IsEmpty() const
V8_INLINE bool operator==(const Local< S > &that) const
friend Local< Boolean > False(Isolate *isolate)
friend Local< Primitive > Undefined(Isolate *isolate)
static V8_INLINE Local< T > New(Isolate *isolate, const BasicTracedReference< T > &that)
V8_INLINE Local< S > As() const
V8_INLINE bool operator==(const PersistentBase< S > &that) const
friend Local< Boolean > True(Isolate *isolate)
V8_INLINE Local()
V8_INLINE void Clear()
friend class Utils
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
V8_INLINE T * operator*() const
V8_INLINE bool operator!=(const Persistent< S > &that) const
friend class Local
V8_INLINE Local< T > ToLocalChecked()
V8_INLINE bool IsEmpty() const
V8_INLINE MaybeLocal(Local< S > that)
V8_INLINE MaybeLocal()
V8_INLINE Local< S > FromMaybe(Local< S > default_value) const
V8_WARN_UNUSED_RESULT V8_INLINE bool ToLocal(Local< S > *out) const
void operator=(const SealHandleScope &)=delete
SealHandleScope(Isolate *isolate)
SealHandleScope(const SealHandleScope &)=delete
internal::BasicPersistent< T, internal::StrongPersistentPolicy > Persistent
Definition persistent.h:356
V8_EXPORT void ToLocalEmpty()
uintptr_t Address
Definition v8-internal.h:29
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:499
#define V8_UNLIKELY(condition)
Definition v8config.h:488
#define V8_NODISCARD
Definition v8config.h:513