2011-06-08 08:13:31 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifndef V8_HANDLES_H_
|
|
|
|
#define V8_HANDLES_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/objects.h"
|
2009-01-23 17:22:23 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
// Forward declarations.
|
|
|
|
class DeferredHandles;
|
|
|
|
class HandleScopeImplementer;
|
|
|
|
|
|
|
|
|
2014-04-03 05:57:43 +00:00
|
|
|
// A Handle can be converted into a MaybeHandle. Converting a MaybeHandle
|
|
|
|
// into a Handle requires checking that it does not point to NULL. This
|
|
|
|
// ensures NULL checks before use.
|
2014-04-03 12:30:37 +00:00
|
|
|
// Do not use MaybeHandle as argument type.
|
2014-04-03 05:57:43 +00:00
|
|
|
template<typename T>
|
|
|
|
class MaybeHandle {
|
|
|
|
public:
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE MaybeHandle() : location_(nullptr) {}
|
2014-04-03 05:57:43 +00:00
|
|
|
|
|
|
|
// Constructor for handling automatic up casting from Handle.
|
|
|
|
// Ex. Handle<JSArray> can be passed when MaybeHandle<Object> is expected.
|
|
|
|
template <class S> MaybeHandle(Handle<S> handle) {
|
|
|
|
#ifdef DEBUG
|
2015-05-05 12:48:14 +00:00
|
|
|
T* a = nullptr;
|
|
|
|
S* b = nullptr;
|
2014-04-03 05:57:43 +00:00
|
|
|
a = b; // Fake assignment to enforce type checks.
|
|
|
|
USE(a);
|
|
|
|
#endif
|
|
|
|
this->location_ = reinterpret_cast<T**>(handle.location());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor for handling automatic up casting.
|
|
|
|
// Ex. MaybeHandle<JSArray> can be passed when Handle<Object> is expected.
|
|
|
|
template <class S> MaybeHandle(MaybeHandle<S> maybe_handle) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
T* a = NULL;
|
|
|
|
S* b = NULL;
|
|
|
|
a = b; // Fake assignment to enforce type checks.
|
|
|
|
USE(a);
|
|
|
|
#endif
|
|
|
|
location_ = reinterpret_cast<T**>(maybe_handle.location_);
|
|
|
|
}
|
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE void Assert() const { DCHECK_NOT_NULL(location_); }
|
|
|
|
V8_INLINE void Check() const { CHECK_NOT_NULL(location_); }
|
2014-04-04 12:06:11 +00:00
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE Handle<T> ToHandleChecked() const {
|
2014-04-04 12:06:11 +00:00
|
|
|
Check();
|
2014-04-03 05:57:43 +00:00
|
|
|
return Handle<T>(location_);
|
|
|
|
}
|
|
|
|
|
2014-04-03 12:30:08 +00:00
|
|
|
// Convert to a Handle with a type that can be upcasted to.
|
2014-10-02 08:38:37 +00:00
|
|
|
template <class S>
|
2014-10-07 07:36:21 +00:00
|
|
|
V8_INLINE bool ToHandle(Handle<S>* out) const {
|
2014-04-03 05:57:43 +00:00
|
|
|
if (location_ == NULL) {
|
|
|
|
*out = Handle<T>::null();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
*out = Handle<T>(location_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 12:30:08 +00:00
|
|
|
bool is_null() const { return location_ == NULL; }
|
|
|
|
|
2014-04-03 05:57:43 +00:00
|
|
|
protected:
|
|
|
|
T** location_;
|
|
|
|
|
|
|
|
// MaybeHandles of different classes are allowed to access each
|
|
|
|
// other's location_.
|
|
|
|
template<class S> friend class MaybeHandle;
|
|
|
|
};
|
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
|
|
|
|
// Base class for Handles. Don't use this directly.
|
|
|
|
class HandleBase {
|
|
|
|
public:
|
|
|
|
V8_INLINE explicit HandleBase(Object** location = nullptr)
|
|
|
|
: location_(location) {}
|
|
|
|
V8_INLINE explicit HandleBase(HandleBase const& other)
|
|
|
|
: location_(other.location_) {}
|
|
|
|
explicit HandleBase(HeapObject* object);
|
|
|
|
explicit HandleBase(Object* object, Isolate* isolate);
|
|
|
|
V8_INLINE ~HandleBase() {}
|
|
|
|
|
|
|
|
// Check if this handle refers to the exact same object as the other handle.
|
|
|
|
V8_INLINE bool is_identical_to(HandleBase const& other) const {
|
|
|
|
// Dereferencing deferred handles to check object equality is safe.
|
|
|
|
SLOW_DCHECK(is_null() || IsDereferenceAllowed(NO_DEFERRED_CHECK));
|
|
|
|
SLOW_DCHECK(other.is_null() ||
|
|
|
|
other.IsDereferenceAllowed(NO_DEFERRED_CHECK));
|
|
|
|
if (location_ == other.location_) return true;
|
|
|
|
if (location_ == nullptr || other.location_ == nullptr) return false;
|
|
|
|
return *location_ == *other.location_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this handle is a NULL handle.
|
|
|
|
V8_INLINE bool is_null() const { return location_ == nullptr; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Provides the C++ deference operator.
|
|
|
|
V8_INLINE Object* operator*() const {
|
|
|
|
SLOW_DCHECK(IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK));
|
|
|
|
return *location_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the address to where the raw pointer is stored.
|
|
|
|
V8_INLINE Object** location() const {
|
|
|
|
SLOW_DCHECK(location_ == nullptr ||
|
|
|
|
IsDereferenceAllowed(INCLUDE_DEFERRED_CHECK));
|
|
|
|
return location_;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DereferenceCheckMode { INCLUDE_DEFERRED_CHECK, NO_DEFERRED_CHECK };
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool IsDereferenceAllowed(DereferenceCheckMode mode) const;
|
|
|
|
#else
|
|
|
|
V8_INLINE bool IsDereferenceAllowed(DereferenceCheckMode) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
Object** location_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// A Handle provides a reference to an object that survives relocation by
|
|
|
|
// the garbage collector.
|
2009-01-23 17:22:23 +00:00
|
|
|
// Handles are only valid within a HandleScope.
|
2008-07-03 15:10:15 +00:00
|
|
|
// When a handle is created for an object a cell is allocated in the heap.
|
2015-05-05 12:48:14 +00:00
|
|
|
template <typename T>
|
|
|
|
class Handle final : public HandleBase {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE explicit Handle(T** location)
|
|
|
|
: HandleBase(reinterpret_cast<Object**>(location)) {}
|
|
|
|
V8_INLINE explicit Handle(T* object) : HandleBase(object) {}
|
|
|
|
V8_INLINE Handle(T* object, Isolate* isolate) : HandleBase(object, isolate) {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-04-03 05:57:43 +00:00
|
|
|
// TODO(yangguo): Values that contain empty handles should be declared as
|
|
|
|
// MaybeHandle to force validation before being used as handles.
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE Handle() {}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Constructor for handling automatic up casting.
|
|
|
|
// Ex. Handle<JSFunction> can be passed when Handle<Object> is expected.
|
2015-05-05 12:48:14 +00:00
|
|
|
template <class S>
|
|
|
|
V8_INLINE Handle(Handle<S> const& other)
|
|
|
|
: HandleBase(other) {
|
|
|
|
T* a = nullptr;
|
|
|
|
S* b = nullptr;
|
2008-07-03 15:10:15 +00:00
|
|
|
a = b; // Fake assignment to enforce type checks.
|
|
|
|
USE(a);
|
|
|
|
}
|
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE T* operator->() const { return operator*(); }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Provides the C++ dereference operator.
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE T* operator*() const {
|
|
|
|
return reinterpret_cast<T*>(HandleBase::operator*());
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Returns the address to where the raw pointer is stored.
|
2015-05-05 12:48:14 +00:00
|
|
|
V8_INLINE T** location() const {
|
|
|
|
return reinterpret_cast<T**>(HandleBase::location());
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
template <class S>
|
|
|
|
V8_INLINE static Handle<T> cast(Handle<S> const& other) {
|
|
|
|
T::cast(*reinterpret_cast<T**>(other.location_));
|
|
|
|
return Handle<T>(reinterpret_cast<T**>(other.location_));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 05:57:43 +00:00
|
|
|
// TODO(yangguo): Values that contain empty handles should be declared as
|
|
|
|
// MaybeHandle to force validation before being used as handles.
|
2008-07-03 15:10:15 +00:00
|
|
|
static Handle<T> null() { return Handle<T>(); }
|
|
|
|
|
|
|
|
// Closes the given scope, but lets this handle escape. See
|
|
|
|
// implementation in api.h.
|
2013-12-02 18:12:01 +00:00
|
|
|
inline Handle<T> EscapeFrom(v8::EscapableHandleScope* scope);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
private:
|
2013-01-23 13:52:00 +00:00
|
|
|
// Handles of different classes are allowed to access each other's location_.
|
|
|
|
template<class S> friend class Handle;
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-11-06 12:32:36 +00:00
|
|
|
// Convenience wrapper.
|
|
|
|
template<class T>
|
2012-11-16 08:38:11 +00:00
|
|
|
inline Handle<T> handle(T* t, Isolate* isolate) {
|
|
|
|
return Handle<T>(t, isolate);
|
2012-11-06 12:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-05 09:38:29 +00:00
|
|
|
// Convenience wrapper.
|
2015-05-05 12:48:14 +00:00
|
|
|
template <class T>
|
2013-07-05 09:38:29 +00:00
|
|
|
inline Handle<T> handle(T* t) {
|
2015-05-05 12:48:14 +00:00
|
|
|
return Handle<T>(t);
|
2013-07-05 09:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-02 06:37:54 +00:00
|
|
|
// Key comparison function for Map handles.
|
|
|
|
inline bool operator<(const Handle<Map>& lhs, const Handle<Map>& rhs) {
|
|
|
|
// This is safe because maps don't move.
|
|
|
|
return *lhs < *rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-23 17:22:23 +00:00
|
|
|
// A stack-allocated class that governs a number of local handles.
|
|
|
|
// After a handle scope has been created, all local handles will be
|
|
|
|
// allocated within that handle scope until either the handle scope is
|
|
|
|
// deleted or another handle scope is created. If there is already a
|
|
|
|
// handle scope and a new one is created, all allocations will take
|
|
|
|
// place in the new handle scope until it is deleted. After that,
|
|
|
|
// new handles will again be allocated in the original handle scope.
|
|
|
|
//
|
|
|
|
// After the handle scope of a local handle has been deleted the
|
|
|
|
// garbage collector will no longer track the object stored in the
|
|
|
|
// handle and may deallocate it. The behavior of accessing a handle
|
|
|
|
// for which the handle scope has been deleted is undefined.
|
2015-05-05 12:48:14 +00:00
|
|
|
class HandleScope final {
|
2009-01-23 17:22:23 +00:00
|
|
|
public:
|
2015-05-05 12:48:14 +00:00
|
|
|
explicit HandleScope(Isolate* isolate);
|
|
|
|
~HandleScope();
|
2009-01-23 17:22:23 +00:00
|
|
|
|
|
|
|
// Counts the number of allocated handles.
|
2013-02-25 14:46:09 +00:00
|
|
|
static int NumberOfHandles(Isolate* isolate);
|
2009-01-23 17:22:23 +00:00
|
|
|
|
|
|
|
// Creates a new handle with the given value.
|
2015-05-05 12:48:14 +00:00
|
|
|
static Object** CreateHandle(Isolate* isolate, Object* value);
|
2009-08-14 17:19:51 +00:00
|
|
|
template <typename T>
|
2015-05-05 12:48:14 +00:00
|
|
|
static T** CreateHandle(Isolate* isolate, T* value) {
|
|
|
|
return reinterpret_cast<T**>(
|
|
|
|
CreateHandle(isolate, static_cast<Object*>(value)));
|
|
|
|
}
|
2009-01-23 17:22:23 +00:00
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
// Deallocates any extensions used by the current scope.
|
2011-03-18 20:35:07 +00:00
|
|
|
static void DeleteExtensions(Isolate* isolate);
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2013-02-25 14:46:09 +00:00
|
|
|
static Address current_next_address(Isolate* isolate);
|
|
|
|
static Address current_limit_address(Isolate* isolate);
|
|
|
|
static Address current_level_address(Isolate* isolate);
|
2009-11-04 08:51:48 +00:00
|
|
|
|
2011-03-01 14:00:55 +00:00
|
|
|
// Closes the HandleScope (invalidating all handles
|
|
|
|
// created in the scope of the HandleScope) and returns
|
|
|
|
// a Handle backed by the parent scope holding the
|
|
|
|
// value of the argument handle.
|
2015-05-05 12:48:14 +00:00
|
|
|
Handle<Object> CloseAndEscape(Handle<Object> handle);
|
2011-03-01 14:00:55 +00:00
|
|
|
template <typename T>
|
2015-05-05 12:48:14 +00:00
|
|
|
Handle<T> CloseAndEscape(Handle<T> handle) {
|
|
|
|
return Handle<T>::cast(CloseAndEscape(Handle<Object>::cast(handle)));
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
Isolate* isolate() const { return isolate_; }
|
2011-03-01 14:00:55 +00:00
|
|
|
|
2009-01-23 17:22:23 +00:00
|
|
|
private:
|
2015-05-05 12:48:14 +00:00
|
|
|
friend class v8::HandleScope;
|
|
|
|
friend class v8::internal::DeferredHandles;
|
|
|
|
friend class v8::internal::HandleScopeImplementer;
|
|
|
|
friend class v8::internal::Isolate;
|
2009-01-23 17:22:23 +00:00
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
// Prevent heap allocation or illegal handle scopes.
|
|
|
|
void* operator new(size_t size) = delete;
|
|
|
|
void operator delete(void* size_t) = delete;
|
2009-01-23 17:22:23 +00:00
|
|
|
|
2013-05-29 11:09:01 +00:00
|
|
|
// Close the handle scope resetting limits to a previous state.
|
2015-05-05 12:48:14 +00:00
|
|
|
static void CloseScope(Isolate* isolate, Object** prev_next,
|
|
|
|
Object** prev_limit);
|
2013-05-29 11:09:01 +00:00
|
|
|
|
2009-03-18 18:50:35 +00:00
|
|
|
// Extend the handle scope making room for more handles.
|
2015-05-05 12:48:14 +00:00
|
|
|
static Object** Extend(Isolate* isolate);
|
2009-03-18 18:50:35 +00:00
|
|
|
|
2013-09-30 11:56:52 +00:00
|
|
|
#ifdef ENABLE_HANDLE_ZAPPING
|
2009-01-23 17:22:23 +00:00
|
|
|
// Zaps the handles in the half-open interval [start, end).
|
2013-05-29 11:09:01 +00:00
|
|
|
static void ZapRange(Object** start, Object** end);
|
2013-03-22 13:40:13 +00:00
|
|
|
#endif
|
2009-01-23 17:22:23 +00:00
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
Isolate* const isolate_;
|
|
|
|
Object** prev_next_;
|
|
|
|
Object** prev_limit_;
|
2009-01-23 17:22:23 +00:00
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(HandleScope);
|
|
|
|
};
|
2012-07-06 09:31:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeferredHandleScope {
|
|
|
|
public:
|
|
|
|
explicit DeferredHandleScope(Isolate* isolate);
|
|
|
|
// The DeferredHandles object returned stores the Handles created
|
|
|
|
// since the creation of this DeferredHandleScope. The Handles are
|
|
|
|
// alive as long as the DeferredHandles object is alive.
|
|
|
|
DeferredHandles* Detach();
|
|
|
|
~DeferredHandleScope();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Object** prev_limit_;
|
|
|
|
Object** prev_next_;
|
|
|
|
HandleScopeImplementer* impl_;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool handles_detached_;
|
|
|
|
int prev_level_;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
friend class HandleScopeImplementer;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-06-03 15:32:22 +00:00
|
|
|
// Seal off the current HandleScope so that new handles can only be created
|
|
|
|
// if a new HandleScope is entered.
|
2015-05-05 12:48:14 +00:00
|
|
|
class SealHandleScope final {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
|
|
|
#ifndef DEBUG
|
2013-06-03 15:32:22 +00:00
|
|
|
explicit SealHandleScope(Isolate* isolate) {}
|
|
|
|
~SealHandleScope() {}
|
2008-07-03 15:10:15 +00:00
|
|
|
#else
|
2015-05-05 12:48:14 +00:00
|
|
|
explicit SealHandleScope(Isolate* isolate);
|
|
|
|
~SealHandleScope();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2015-05-05 12:48:14 +00:00
|
|
|
Isolate* const isolate_;
|
2013-05-29 11:09:01 +00:00
|
|
|
Object** limit_;
|
2010-10-21 14:21:00 +00:00
|
|
|
int level_;
|
2015-05-05 12:48:14 +00:00
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SealHandleScope);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
|
2014-01-16 08:17:40 +00:00
|
|
|
struct HandleScopeData {
|
|
|
|
internal::Object** next;
|
|
|
|
internal::Object** limit;
|
|
|
|
int level;
|
|
|
|
|
|
|
|
void Initialize() {
|
2015-05-05 12:48:14 +00:00
|
|
|
next = limit = nullptr;
|
2014-01-16 08:17:40 +00:00
|
|
|
level = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-05 12:48:14 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#endif // V8_HANDLES_H_
|