// Copyright 2006-2008 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef V8_HANDLES_H_ #define V8_HANDLES_H_ #include "apiutils.h" namespace v8 { namespace internal { // ---------------------------------------------------------------------------- // A Handle provides a reference to an object that survives relocation by // the garbage collector. // Handles are only valid within a HandleScope. // When a handle is created for an object a cell is allocated in the heap. template class Handle { public: INLINE(Handle(T** location)) { location_ = location; } INLINE(explicit Handle(T* obj)); INLINE(Handle()) : location_(NULL) {} // Constructor for handling automatic up casting. // Ex. Handle can be passed when Handle is expected. template Handle(Handle handle) { #ifdef DEBUG T* a = NULL; S* b = NULL; a = b; // Fake assignment to enforce type checks. USE(a); #endif location_ = reinterpret_cast(handle.location()); } INLINE(T* operator ->() const) { return operator*(); } // Check if this handle refers to the exact same object as the other handle. bool is_identical_to(const Handle other) const { return operator*() == *other; } // Provides the C++ dereference operator. INLINE(T* operator*() const); // Returns the address to where the raw pointer is stored. T** location() const { ASSERT(location_ == NULL || reinterpret_cast
(*location_) != kZapValue); return location_; } template static Handle cast(Handle that) { T::cast(*that); return Handle(reinterpret_cast(that.location())); } static Handle null() { return Handle(); } bool is_null() { return location_ == NULL; } // Closes the given scope, but lets this handle escape. See // implementation in api.h. inline Handle EscapeFrom(v8::HandleScope* scope); private: T** location_; }; // 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. class HandleScope { public: HandleScope() : previous_(current_) { current_.extensions = 0; } ~HandleScope() { Leave(&previous_); } // Counts the number of allocated handles. static int NumberOfHandles(); // Creates a new handle with the given value. template static inline T** CreateHandle(T* value) { internal::Object** cur = current_.next; if (cur == current_.limit) cur = Extend(); // Update the current next field, set the value in the created // handle, and return the result. ASSERT(cur < current_.limit); current_.next = cur + 1; T** result = reinterpret_cast(cur); *result = value; return result; } private: // Prevent heap allocation or illegal handle scopes. HandleScope(const HandleScope&); void operator=(const HandleScope&); void* operator new(size_t size); void operator delete(void* size_t); static v8::ImplementationUtilities::HandleScopeData current_; const v8::ImplementationUtilities::HandleScopeData previous_; // Pushes a fresh handle scope to be used when allocating new handles. static void Enter( v8::ImplementationUtilities::HandleScopeData* previous) { *previous = current_; current_.extensions = 0; } // Re-establishes the previous scope state. Should be called only // once, and only for the current scope. static void Leave( const v8::ImplementationUtilities::HandleScopeData* previous) { if (current_.extensions > 0) { DeleteExtensions(); } current_ = *previous; #ifdef DEBUG ZapRange(current_.next, current_.limit); #endif } // Extend the handle scope making room for more handles. static internal::Object** Extend(); // Deallocates any extensions used by the current scope. static void DeleteExtensions(); // Zaps the handles in the half-open interval [start, end). static void ZapRange(internal::Object** start, internal::Object** end); friend class v8::HandleScope; friend class v8::ImplementationUtilities; }; // ---------------------------------------------------------------------------- // Handle operations. // They might invoke garbage collection. The result is an handle to // an object of expected type, or the handle is an error if running out // of space or encountering an internal error. void NormalizeProperties(Handle object, PropertyNormalizationMode mode, int expected_additional_properties); void NormalizeElements(Handle object); void TransformToFastProperties(Handle object, int unused_property_fields); void FlattenString(Handle str); Handle SetProperty(Handle object, Handle key, Handle value, PropertyAttributes attributes); Handle SetProperty(Handle object, Handle key, Handle value, PropertyAttributes attributes); Handle ForceSetProperty(Handle object, Handle key, Handle value, PropertyAttributes attributes); Handle ForceDeleteProperty(Handle object, Handle key); Handle IgnoreAttributesAndSetLocalProperty(Handle object, Handle key, Handle value, PropertyAttributes attributes); Handle SetPropertyWithInterceptor(Handle object, Handle key, Handle value, PropertyAttributes attributes); Handle SetElement(Handle object, uint32_t index, Handle value); Handle GetProperty(Handle obj, const char* name); Handle GetProperty(Handle obj, Handle key); Handle GetPropertyWithInterceptor(Handle receiver, Handle holder, Handle name, PropertyAttributes* attributes); Handle GetPrototype(Handle obj); // Return the object's hidden properties object. If the object has no hidden // properties and create_if_needed is true, then a new hidden property object // will be allocated. Otherwise the Heap::undefined_value is returned. Handle GetHiddenProperties(Handle obj, bool create_if_needed); Handle DeleteElement(Handle obj, uint32_t index); Handle DeleteProperty(Handle obj, Handle prop); Handle LookupSingleCharacterStringFromCode(uint32_t index); Handle Copy(Handle obj); Handle AddKeysFromJSArray(Handle, Handle array); // Get the JS object corresponding to the given script; create it // if none exists. Handle GetScriptWrapper(Handle