ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | namespace v8 { namespace internal { |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | // A Handle provides a reference to an object that survives relocation by |
| 35 | // the garbage collector. |
| 36 | // Handles are only valid withing a HandleScope. |
| 37 | // When a handle is created for an object a cell is allocated in the heap. |
| 38 | |
| 39 | template<class T> |
| 40 | class Handle { |
| 41 | public: |
| 42 | INLINE(Handle(T** location)) { location_ = location; } |
| 43 | INLINE(explicit Handle(T* obj)); |
| 44 | |
| 45 | INLINE(Handle()) : location_(NULL) {} |
| 46 | |
| 47 | // Constructor for handling automatic up casting. |
| 48 | // Ex. Handle<JSFunction> can be passed when Handle<Object> is expected. |
| 49 | template <class S> Handle(Handle<S> handle) { |
| 50 | #ifdef DEBUG |
| 51 | T* a = NULL; |
| 52 | S* b = NULL; |
| 53 | a = b; // Fake assignment to enforce type checks. |
| 54 | USE(a); |
| 55 | #endif |
| 56 | location_ = reinterpret_cast<T**>(handle.location()); |
| 57 | } |
| 58 | |
| 59 | INLINE(T* operator ->() const) { return operator*(); } |
| 60 | |
| 61 | // Check if this handle refers to the exact same object as the other handle. |
| 62 | bool is_identical_to(const Handle<T> other) const { |
| 63 | return operator*() == *other; |
| 64 | } |
| 65 | |
| 66 | // Provides the C++ dereference operator. |
| 67 | INLINE(T* operator*() const); |
| 68 | |
| 69 | // Returns the address to where the raw pointer is stored. |
| 70 | T** location() const { |
| 71 | ASSERT(location_ == NULL || |
| 72 | reinterpret_cast<Address>(*location_) != kZapValue); |
| 73 | return location_; |
| 74 | } |
| 75 | |
| 76 | template <class S> static Handle<T> cast(Handle<S> that) { |
| 77 | T::cast(*that); |
| 78 | return Handle<T>(reinterpret_cast<T**>(that.location())); |
| 79 | } |
| 80 | |
| 81 | static Handle<T> null() { return Handle<T>(); } |
| 82 | bool is_null() {return location_ == NULL; } |
| 83 | |
| 84 | // Closes the given scope, but lets this handle escape. See |
| 85 | // implementation in api.h. |
| 86 | inline Handle<T> EscapeFrom(HandleScope* scope); |
| 87 | |
| 88 | private: |
| 89 | T** location_; |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | // ---------------------------------------------------------------------------- |
| 94 | // Handle operations. |
| 95 | // They might invoke garbage collection. The result is an handle to |
| 96 | // an object of expected type, or the handle is an error if running out |
| 97 | // of space or encounting an internal error. |
| 98 | |
| 99 | void NormalizeProperties(Handle<JSObject> object); |
| 100 | void NormalizeElements(Handle<JSObject> object); |
| 101 | void TransformToFastProperties(Handle<JSObject> object, |
| 102 | int unused_property_fields); |
| 103 | void FlattenString(Handle<String> str); |
| 104 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | Handle<Object> SetProperty(Handle<JSObject> object, |
| 106 | Handle<String> key, |
| 107 | Handle<Object> value, |
| 108 | PropertyAttributes attributes); |
| 109 | |
| 110 | Handle<Object> SetProperty(Handle<Object> object, |
| 111 | Handle<Object> key, |
| 112 | Handle<Object> value, |
| 113 | PropertyAttributes attributes); |
| 114 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 115 | Handle<Object> IgnoreAttributesAndSetLocalProperty(Handle<JSObject> object, |
| 116 | Handle<String> key, |
| 117 | Handle<Object> value, |
| 118 | PropertyAttributes attributes); |
| 119 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 120 | Handle<Object> SetPropertyWithInterceptor(Handle<JSObject> object, |
| 121 | Handle<String> key, |
| 122 | Handle<Object> value, |
| 123 | PropertyAttributes attributes); |
| 124 | |
| 125 | Handle<Object> SetElement(Handle<JSObject> object, |
| 126 | uint32_t index, |
| 127 | Handle<Object> value); |
| 128 | |
| 129 | Handle<Object> GetProperty(Handle<JSObject> obj, |
| 130 | const char* name); |
| 131 | |
| 132 | Handle<Object> GetProperty(Handle<Object> obj, |
| 133 | Handle<Object> key); |
| 134 | |
| 135 | Handle<Object> GetPropertyWithInterceptor(Handle<JSObject> receiver, |
| 136 | Handle<JSObject> holder, |
| 137 | Handle<String> name, |
| 138 | PropertyAttributes* attributes); |
| 139 | |
| 140 | Handle<Object> GetPrototype(Handle<Object> obj); |
| 141 | |
| 142 | Handle<Object> DeleteElement(Handle<JSObject> obj, uint32_t index); |
| 143 | Handle<Object> DeleteProperty(Handle<JSObject> obj, Handle<String> prop); |
| 144 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 145 | Handle<Object> LookupSingleCharacterStringFromCode(uint32_t index); |
| 146 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 147 | Handle<JSObject> Copy(Handle<JSObject> obj); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | |
| 149 | // Get the JS object corresponding to the given script; create it |
| 150 | // if none exists. |
| 151 | Handle<JSValue> GetScriptWrapper(Handle<Script> script); |
| 152 | |
| 153 | // Computes the enumerable keys from interceptors. Used for debug mirrors and |
| 154 | // by GetKeysInFixedArrayFor below. |
| 155 | v8::Handle<v8::Array> GetKeysForNamedInterceptor(Handle<JSObject> receiver, |
| 156 | Handle<JSObject> object); |
| 157 | v8::Handle<v8::Array> GetKeysForIndexedInterceptor(Handle<JSObject> receiver, |
| 158 | Handle<JSObject> object); |
| 159 | // Computes the enumerable keys for a JSObject. Used for implementing |
| 160 | // "for (n in object) { }". |
| 161 | Handle<FixedArray> GetKeysInFixedArrayFor(Handle<JSObject> object); |
| 162 | Handle<JSArray> GetKeysFor(Handle<JSObject> object); |
| 163 | Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object); |
| 164 | |
| 165 | // Computes the union of keys and return the result. |
| 166 | // Used for implementing "for (n in object) { }" |
| 167 | Handle<FixedArray> UnionOfKeys(Handle<FixedArray> first, |
| 168 | Handle<FixedArray> second); |
| 169 | |
| 170 | Handle<String> SubString(Handle<String> str, int start, int end); |
| 171 | |
| 172 | |
| 173 | // Sets the expected number of properties for the function's instances. |
| 174 | void SetExpectedNofProperties(Handle<JSFunction> func, int nof); |
| 175 | |
| 176 | // Sets the prototype property for a function instance. |
| 177 | void SetPrototypeProperty(Handle<JSFunction> func, Handle<JSObject> value); |
| 178 | |
| 179 | // Sets the expected number of properties based on estimate from compiler. |
| 180 | void SetExpectedNofPropertiesFromEstimate(Handle<SharedFunctionInfo> shared, |
| 181 | int estimate); |
| 182 | void SetExpectedNofPropertiesFromEstimate(Handle<JSFunction> func, |
| 183 | int estimate); |
| 184 | |
| 185 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 186 | Handle<JSGlobalProxy> ReinitializeJSGlobalProxy( |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 187 | Handle<JSFunction> constructor, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 188 | Handle<JSGlobalProxy> global); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 189 | |
| 190 | Handle<Object> SetPrototype(Handle<JSFunction> function, |
| 191 | Handle<Object> prototype); |
| 192 | |
| 193 | |
| 194 | // Do lazy compilation of the given function. Returns true on success |
| 195 | // and false if the compilation resulted in a stack overflow. |
| 196 | enum ClearExceptionFlag { KEEP_EXCEPTION, CLEAR_EXCEPTION }; |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 197 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 198 | bool CompileLazyShared(Handle<SharedFunctionInfo> shared, |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 199 | ClearExceptionFlag flag, |
| 200 | int loop_nesting); |
| 201 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | bool CompileLazy(Handle<JSFunction> function, ClearExceptionFlag flag); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 203 | bool CompileLazyInLoop(Handle<JSFunction> function, ClearExceptionFlag flag); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | |
| 205 | // These deal with lazily loaded properties. |
| 206 | void SetupLazy(Handle<JSFunction> fun, |
| 207 | int index, |
| 208 | Handle<Context> compile_context, |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 209 | Handle<Context> function_context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 210 | void LoadLazy(Handle<JSFunction> fun, bool* pending_exception); |
| 211 | |
| 212 | class NoHandleAllocation BASE_EMBEDDED { |
| 213 | public: |
| 214 | #ifndef DEBUG |
| 215 | NoHandleAllocation() {} |
| 216 | ~NoHandleAllocation() {} |
| 217 | #else |
| 218 | inline NoHandleAllocation(); |
| 219 | inline ~NoHandleAllocation(); |
| 220 | private: |
| 221 | int extensions_; |
| 222 | #endif |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | // ---------------------------------------------------------------------------- |
| 227 | |
| 228 | |
| 229 | // Stack allocated wrapper call for optimizing adding multiple |
| 230 | // properties to an object. |
| 231 | class OptimizedObjectForAddingMultipleProperties BASE_EMBEDDED { |
| 232 | public: |
| 233 | OptimizedObjectForAddingMultipleProperties(Handle<JSObject> object, |
| 234 | bool condition = true); |
| 235 | ~OptimizedObjectForAddingMultipleProperties(); |
| 236 | private: |
| 237 | bool has_been_transformed_; // Tells whether the object has been transformed. |
| 238 | int unused_property_fields_; // Captures the unused number of field. |
| 239 | Handle<JSObject> object_; // The object being optimized. |
| 240 | }; |
| 241 | |
| 242 | |
| 243 | } } // namespace v8::internal |
| 244 | |
| 245 | #endif // V8_HANDLES_H_ |