1 // Copyright 2011 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 #ifndef V8_HANDLES_H_ 29 #define V8_HANDLES_H_ 30 31 #include "allocation.h" 32 #include "apiutils.h" 33 34 namespace v8 { 35 namespace internal { 36 37 // ---------------------------------------------------------------------------- 38 // A Handle provides a reference to an object that survives relocation by 39 // the garbage collector. 40 // Handles are only valid within a HandleScope. 41 // When a handle is created for an object a cell is allocated in the heap. 42 43 template<typename T> 44 class Handle { 45 public: INLINE(explicit Handle (T ** location))46 INLINE(explicit Handle(T** location)) { location_ = location; } 47 INLINE(explicit Handle(T* obj)); 48 INLINE(Handle(T* obj, Isolate* isolate)); 49 INLINE(Handle ())50 INLINE(Handle()) : location_(NULL) {} 51 52 // Constructor for handling automatic up casting. 53 // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. Handle(Handle<S> handle)54 template <class S> Handle(Handle<S> handle) { 55 #ifdef DEBUG 56 T* a = NULL; 57 S* b = NULL; 58 a = b; // Fake assignment to enforce type checks. 59 USE(a); 60 #endif 61 location_ = reinterpret_cast<T**>(handle.location()); 62 } 63 64 INLINE(T* operator ->() const) { return operator*(); } 65 66 // Check if this handle refers to the exact same object as the other handle. is_identical_to(const Handle<T> other)67 bool is_identical_to(const Handle<T> other) const { 68 return operator*() == *other; 69 } 70 71 // Provides the C++ dereference operator. 72 INLINE(T* operator*() const); 73 74 // Returns the address to where the raw pointer is stored. location()75 T** location() const { 76 ASSERT(location_ == NULL || 77 reinterpret_cast<Address>(*location_) != kZapValue); 78 return location_; 79 } 80 cast(Handle<S> that)81 template <class S> static Handle<T> cast(Handle<S> that) { 82 T::cast(*that); 83 return Handle<T>(reinterpret_cast<T**>(that.location())); 84 } 85 null()86 static Handle<T> null() { return Handle<T>(); } is_null()87 bool is_null() const { return location_ == NULL; } 88 89 // Closes the given scope, but lets this handle escape. See 90 // implementation in api.h. 91 inline Handle<T> EscapeFrom(v8::HandleScope* scope); 92 93 private: 94 T** location_; 95 }; 96 97 98 // A stack-allocated class that governs a number of local handles. 99 // After a handle scope has been created, all local handles will be 100 // allocated within that handle scope until either the handle scope is 101 // deleted or another handle scope is created. If there is already a 102 // handle scope and a new one is created, all allocations will take 103 // place in the new handle scope until it is deleted. After that, 104 // new handles will again be allocated in the original handle scope. 105 // 106 // After the handle scope of a local handle has been deleted the 107 // garbage collector will no longer track the object stored in the 108 // handle and may deallocate it. The behavior of accessing a handle 109 // for which the handle scope has been deleted is undefined. 110 class HandleScope { 111 public: 112 inline HandleScope(); 113 explicit inline HandleScope(Isolate* isolate); 114 115 inline ~HandleScope(); 116 117 // Counts the number of allocated handles. 118 static int NumberOfHandles(); 119 120 // Creates a new handle with the given value. 121 template <typename T> 122 static inline T** CreateHandle(T* value, Isolate* isolate); 123 124 // Deallocates any extensions used by the current scope. 125 static void DeleteExtensions(Isolate* isolate); 126 127 static Address current_next_address(); 128 static Address current_limit_address(); 129 static Address current_level_address(); 130 131 // Closes the HandleScope (invalidating all handles 132 // created in the scope of the HandleScope) and returns 133 // a Handle backed by the parent scope holding the 134 // value of the argument handle. 135 template <typename T> 136 Handle<T> CloseAndEscape(Handle<T> handle_value); 137 isolate()138 Isolate* isolate() { return isolate_; } 139 140 private: 141 // Prevent heap allocation or illegal handle scopes. 142 HandleScope(const HandleScope&); 143 void operator=(const HandleScope&); 144 void* operator new(size_t size); 145 void operator delete(void* size_t); 146 147 inline void CloseScope(); 148 149 Isolate* isolate_; 150 Object** prev_next_; 151 Object** prev_limit_; 152 153 // Extend the handle scope making room for more handles. 154 static internal::Object** Extend(); 155 156 // Zaps the handles in the half-open interval [start, end). 157 static void ZapRange(internal::Object** start, internal::Object** end); 158 159 friend class v8::HandleScope; 160 friend class v8::ImplementationUtilities; 161 }; 162 163 164 // ---------------------------------------------------------------------------- 165 // Handle operations. 166 // They might invoke garbage collection. The result is an handle to 167 // an object of expected type, or the handle is an error if running out 168 // of space or encountering an internal error. 169 170 // Flattens a string. 171 void FlattenString(Handle<String> str); 172 173 // Flattens a string and returns the underlying external or sequential 174 // string. 175 Handle<String> FlattenGetString(Handle<String> str); 176 177 int Utf8Length(Handle<String> str); 178 179 Handle<Object> SetProperty(Handle<Object> object, 180 Handle<Object> key, 181 Handle<Object> value, 182 PropertyAttributes attributes, 183 StrictModeFlag strict_mode); 184 185 Handle<Object> ForceSetProperty(Handle<JSObject> object, 186 Handle<Object> key, 187 Handle<Object> value, 188 PropertyAttributes attributes); 189 190 Handle<Object> ForceDeleteProperty(Handle<JSObject> object, 191 Handle<Object> key); 192 193 Handle<Object> GetProperty(Handle<JSReceiver> obj, 194 const char* name); 195 196 Handle<Object> GetProperty(Handle<Object> obj, 197 Handle<Object> key); 198 199 Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver, 200 Handle<JSObject> holder, 201 Handle<String> name, 202 PropertyAttributes* attributes); 203 204 Handle<Object> SetPrototype(Handle<JSObject> obj, Handle<Object> value); 205 206 Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index); 207 208 Handle<JSObject> Copy(Handle<JSObject> obj); 209 210 Handle<Object> SetAccessor(Handle<JSObject> obj, Handle<AccessorInfo> info); 211 212 Handle<FixedArray> AddKeysFromJSArray(Handle<FixedArray>, 213 Handle<JSArray> array); 214 215 // Get the JS object corresponding to the given script; create it 216 // if none exists. 217 Handle<JSValue> GetScriptWrapper(Handle<Script> script); 218 219 // Script line number computations. 220 void InitScriptLineEnds(Handle<Script> script); 221 // For string calculates an array of line end positions. If the string 222 // does not end with a new line character, this character may optionally be 223 // imagined. 224 Handle<FixedArray> CalculateLineEnds(Handle<String> string, 225 bool with_imaginary_last_new_line); 226 int GetScriptLineNumber(Handle<Script> script, int code_position); 227 // The safe version does not make heap allocations but may work much slower. 228 int GetScriptLineNumberSafe(Handle<Script> script, int code_position); 229 int GetScriptColumnNumber(Handle<Script> script, int code_position); 230 231 // Computes the enumerable keys from interceptors. Used for debug mirrors and 232 // by GetKeysInFixedArrayFor below. 233 v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSReceiver> receiver, 234 Handle<JSObject> object); 235 v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSReceiver> receiver, 236 Handle<JSObject> object); 237 238 enum KeyCollectionType { LOCAL_ONLY, INCLUDE_PROTOS }; 239 240 // Computes the enumerable keys for a JSObject. Used for implementing 241 // "for (n in object) { }". 242 Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSReceiver> object, 243 KeyCollectionType type, 244 bool* threw); 245 Handle<JSArray> GetKeysFor(Handle<JSReceiver> object, bool* threw); 246 Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object, 247 bool cache_result); 248 249 // Computes the union of keys and return the result. 250 // Used for implementing "for (n in object) { }" 251 Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first, 252 Handle<FixedArray> second); 253 254 Handle<String> SubString(Handle<String> str, 255 int start, 256 int end, 257 PretenureFlag pretenure = NOT_TENURED); 258 259 // Sets the expected number of properties for the function's instances. 260 void SetExpectedNofProperties(Handle<JSFunction> func, int nof); 261 262 // Sets the prototype property for a function instance. 263 void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value); 264 265 // Sets the expected number of properties based on estimate from compiler. 266 void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared, 267 int estimate); 268 269 270 Handle<JSGlobalProxy> ReinitializeJSGlobalProxy( 271 Handle<JSFunction> constructor, 272 Handle<JSGlobalProxy> global); 273 274 Handle<Object> SetPrototype(Handle<JSFunction> function, 275 Handle<Object> prototype); 276 277 Handle<ObjectHashSet> ObjectHashSetAdd(Handle<ObjectHashSet> table, 278 Handle<Object> key); 279 280 Handle<ObjectHashSet> ObjectHashSetRemove(Handle<ObjectHashSet> table, 281 Handle<Object> key); 282 283 Handle<ObjectHashTable> PutIntoObjectHashTable(Handle<ObjectHashTable> table, 284 Handle<Object> key, 285 Handle<Object> value); 286 287 class NoHandleAllocation BASE_EMBEDDED { 288 public: 289 #ifndef DEBUG NoHandleAllocation()290 NoHandleAllocation() {} ~NoHandleAllocation()291 ~NoHandleAllocation() {} 292 #else 293 inline NoHandleAllocation(); 294 inline ~NoHandleAllocation(); 295 private: 296 int level_; 297 #endif 298 }; 299 300 } } // namespace v8::internal 301 302 #endif // V8_HANDLES_H_ 303