Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2009 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_OBJECTS_H_ |
| 29 | #define V8_OBJECTS_H_ |
| 30 | |
| 31 | #include "builtins.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 32 | #include "smart-pointer.h" |
| 33 | #include "unicode-inl.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 34 | #if V8_TARGET_ARCH_ARM |
| 35 | #include "arm/constants-arm.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 36 | #elif V8_TARGET_ARCH_MIPS |
| 37 | #include "mips/constants-mips.h" |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 38 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| 40 | // |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 41 | // Most object types in the V8 JavaScript are described in this file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | // |
| 43 | // Inheritance hierarchy: |
| 44 | // - Object |
| 45 | // - Smi (immediate small integer) |
| 46 | // - Failure (immediate for marking failed operation) |
| 47 | // - HeapObject (superclass for everything allocated in the heap) |
| 48 | // - JSObject |
| 49 | // - JSArray |
| 50 | // - JSRegExp |
| 51 | // - JSFunction |
| 52 | // - GlobalObject |
| 53 | // - JSGlobalObject |
| 54 | // - JSBuiltinsObject |
| 55 | // - JSGlobalProxy |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 56 | // - JSValue |
| 57 | // - ByteArray |
| 58 | // - PixelArray |
| 59 | // - ExternalArray |
| 60 | // - ExternalByteArray |
| 61 | // - ExternalUnsignedByteArray |
| 62 | // - ExternalShortArray |
| 63 | // - ExternalUnsignedShortArray |
| 64 | // - ExternalIntArray |
| 65 | // - ExternalUnsignedIntArray |
| 66 | // - ExternalFloatArray |
| 67 | // - FixedArray |
| 68 | // - DescriptorArray |
| 69 | // - HashTable |
| 70 | // - Dictionary |
| 71 | // - SymbolTable |
| 72 | // - CompilationCacheTable |
| 73 | // - CodeCacheHashTable |
| 74 | // - MapCache |
| 75 | // - Context |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 76 | // - JSFunctionResultCache |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 77 | // - SerializedScopeInfo |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | // - String |
| 79 | // - SeqString |
| 80 | // - SeqAsciiString |
| 81 | // - SeqTwoByteString |
| 82 | // - ConsString |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | // - ExternalString |
| 84 | // - ExternalAsciiString |
| 85 | // - ExternalTwoByteString |
| 86 | // - HeapNumber |
| 87 | // - Code |
| 88 | // - Map |
| 89 | // - Oddball |
| 90 | // - Proxy |
| 91 | // - SharedFunctionInfo |
| 92 | // - Struct |
| 93 | // - AccessorInfo |
| 94 | // - AccessCheckInfo |
| 95 | // - InterceptorInfo |
| 96 | // - CallHandlerInfo |
| 97 | // - TemplateInfo |
| 98 | // - FunctionTemplateInfo |
| 99 | // - ObjectTemplateInfo |
| 100 | // - Script |
| 101 | // - SignatureInfo |
| 102 | // - TypeSwitchInfo |
| 103 | // - DebugInfo |
| 104 | // - BreakPointInfo |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 105 | // - CodeCache |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | // |
| 107 | // Formats of Object*: |
| 108 | // Smi: [31 bit signed int] 0 |
| 109 | // HeapObject: [32 bit direct pointer] (4 byte aligned) | 01 |
| 110 | // Failure: [30 bit signed int] 11 |
| 111 | |
| 112 | // Ecma-262 3rd 8.6.1 |
| 113 | enum PropertyAttributes { |
| 114 | NONE = v8::None, |
| 115 | READ_ONLY = v8::ReadOnly, |
| 116 | DONT_ENUM = v8::DontEnum, |
| 117 | DONT_DELETE = v8::DontDelete, |
| 118 | ABSENT = 16 // Used in runtime to indicate a property is absent. |
| 119 | // ABSENT can never be stored in or returned from a descriptor's attributes |
| 120 | // bitfield. It is only used as a return value meaning the attributes of |
| 121 | // a non-existent property. |
| 122 | }; |
| 123 | |
| 124 | namespace v8 { |
| 125 | namespace internal { |
| 126 | |
| 127 | |
| 128 | // PropertyDetails captures type and attributes for a property. |
| 129 | // They are used both in property dictionaries and instance descriptors. |
| 130 | class PropertyDetails BASE_EMBEDDED { |
| 131 | public: |
| 132 | |
| 133 | PropertyDetails(PropertyAttributes attributes, |
| 134 | PropertyType type, |
| 135 | int index = 0) { |
| 136 | ASSERT(TypeField::is_valid(type)); |
| 137 | ASSERT(AttributesField::is_valid(attributes)); |
| 138 | ASSERT(IndexField::is_valid(index)); |
| 139 | |
| 140 | value_ = TypeField::encode(type) |
| 141 | | AttributesField::encode(attributes) |
| 142 | | IndexField::encode(index); |
| 143 | |
| 144 | ASSERT(type == this->type()); |
| 145 | ASSERT(attributes == this->attributes()); |
| 146 | ASSERT(index == this->index()); |
| 147 | } |
| 148 | |
| 149 | // Conversion for storing details as Object*. |
| 150 | inline PropertyDetails(Smi* smi); |
| 151 | inline Smi* AsSmi(); |
| 152 | |
| 153 | PropertyType type() { return TypeField::decode(value_); } |
| 154 | |
| 155 | bool IsTransition() { |
| 156 | PropertyType t = type(); |
| 157 | ASSERT(t != INTERCEPTOR); |
| 158 | return t == MAP_TRANSITION || t == CONSTANT_TRANSITION; |
| 159 | } |
| 160 | |
| 161 | bool IsProperty() { |
| 162 | return type() < FIRST_PHANTOM_PROPERTY_TYPE; |
| 163 | } |
| 164 | |
| 165 | PropertyAttributes attributes() { return AttributesField::decode(value_); } |
| 166 | |
| 167 | int index() { return IndexField::decode(value_); } |
| 168 | |
| 169 | inline PropertyDetails AsDeleted(); |
| 170 | |
| 171 | static bool IsValidIndex(int index) { return IndexField::is_valid(index); } |
| 172 | |
| 173 | bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; } |
| 174 | bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; } |
| 175 | bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; } |
| 176 | bool IsDeleted() { return DeletedField::decode(value_) != 0;} |
| 177 | |
| 178 | // Bit fields in value_ (type, shift, size). Must be public so the |
| 179 | // constants can be embedded in generated code. |
| 180 | class TypeField: public BitField<PropertyType, 0, 3> {}; |
| 181 | class AttributesField: public BitField<PropertyAttributes, 3, 3> {}; |
| 182 | class DeletedField: public BitField<uint32_t, 6, 1> {}; |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 183 | class IndexField: public BitField<uint32_t, 7, 32-7> {}; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 184 | |
| 185 | static const int kInitialIndex = 1; |
| 186 | private: |
| 187 | uint32_t value_; |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | // Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER. |
| 192 | enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER }; |
| 193 | |
| 194 | |
| 195 | // PropertyNormalizationMode is used to specify whether to keep |
| 196 | // inobject properties when normalizing properties of a JSObject. |
| 197 | enum PropertyNormalizationMode { |
| 198 | CLEAR_INOBJECT_PROPERTIES, |
| 199 | KEEP_INOBJECT_PROPERTIES |
| 200 | }; |
| 201 | |
| 202 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 203 | // NormalizedMapSharingMode is used to specify whether a map may be shared |
| 204 | // by different objects with normalized properties. |
| 205 | enum NormalizedMapSharingMode { |
| 206 | UNIQUE_NORMALIZED_MAP, |
| 207 | SHARED_NORMALIZED_MAP |
| 208 | }; |
| 209 | |
| 210 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 211 | // Instance size sentinel for objects of variable size. |
| 212 | static const int kVariableSizeSentinel = 0; |
| 213 | |
| 214 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | // All Maps have a field instance_type containing a InstanceType. |
| 216 | // It describes the type of the instances. |
| 217 | // |
| 218 | // As an example, a JavaScript object is a heap object and its map |
| 219 | // instance_type is JS_OBJECT_TYPE. |
| 220 | // |
| 221 | // The names of the string instance types are intended to systematically |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 222 | // mirror their encoding in the instance_type field of the map. The default |
| 223 | // encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII |
| 224 | // encoding is mentioned explicitly in the name. Likewise, the default |
| 225 | // representation is considered sequential. It is not mentioned in the |
| 226 | // name. The other representations (eg, CONS, EXTERNAL) are explicitly |
| 227 | // mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a |
| 228 | // symbol) or a STRING_TYPE (if it is not a symbol). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | // |
| 230 | // NOTE: The following things are some that depend on the string types having |
| 231 | // instance_types that are less than those of all other types: |
| 232 | // HeapObject::Size, HeapObject::IterateBody, the typeof operator, and |
| 233 | // Object::IsString. |
| 234 | // |
| 235 | // NOTE: Everything following JS_VALUE_TYPE is considered a |
| 236 | // JSObject for GC purposes. The first four entries here have typeof |
| 237 | // 'object', whereas JS_FUNCTION_TYPE has typeof 'function'. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 238 | #define INSTANCE_TYPE_LIST_ALL(V) \ |
| 239 | V(SYMBOL_TYPE) \ |
| 240 | V(ASCII_SYMBOL_TYPE) \ |
| 241 | V(CONS_SYMBOL_TYPE) \ |
| 242 | V(CONS_ASCII_SYMBOL_TYPE) \ |
| 243 | V(EXTERNAL_SYMBOL_TYPE) \ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 244 | V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 245 | V(EXTERNAL_ASCII_SYMBOL_TYPE) \ |
| 246 | V(STRING_TYPE) \ |
| 247 | V(ASCII_STRING_TYPE) \ |
| 248 | V(CONS_STRING_TYPE) \ |
| 249 | V(CONS_ASCII_STRING_TYPE) \ |
| 250 | V(EXTERNAL_STRING_TYPE) \ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 251 | V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 252 | V(EXTERNAL_ASCII_STRING_TYPE) \ |
| 253 | V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \ |
| 254 | \ |
| 255 | V(MAP_TYPE) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 256 | V(CODE_TYPE) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 257 | V(ODDBALL_TYPE) \ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 258 | V(JS_GLOBAL_PROPERTY_CELL_TYPE) \ |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 259 | \ |
| 260 | V(HEAP_NUMBER_TYPE) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 261 | V(PROXY_TYPE) \ |
| 262 | V(BYTE_ARRAY_TYPE) \ |
| 263 | V(PIXEL_ARRAY_TYPE) \ |
| 264 | /* Note: the order of these external array */ \ |
| 265 | /* types is relied upon in */ \ |
| 266 | /* Object::IsExternalArray(). */ \ |
| 267 | V(EXTERNAL_BYTE_ARRAY_TYPE) \ |
| 268 | V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \ |
| 269 | V(EXTERNAL_SHORT_ARRAY_TYPE) \ |
| 270 | V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \ |
| 271 | V(EXTERNAL_INT_ARRAY_TYPE) \ |
| 272 | V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \ |
| 273 | V(EXTERNAL_FLOAT_ARRAY_TYPE) \ |
| 274 | V(FILLER_TYPE) \ |
| 275 | \ |
| 276 | V(ACCESSOR_INFO_TYPE) \ |
| 277 | V(ACCESS_CHECK_INFO_TYPE) \ |
| 278 | V(INTERCEPTOR_INFO_TYPE) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 279 | V(CALL_HANDLER_INFO_TYPE) \ |
| 280 | V(FUNCTION_TEMPLATE_INFO_TYPE) \ |
| 281 | V(OBJECT_TEMPLATE_INFO_TYPE) \ |
| 282 | V(SIGNATURE_INFO_TYPE) \ |
| 283 | V(TYPE_SWITCH_INFO_TYPE) \ |
| 284 | V(SCRIPT_TYPE) \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 285 | V(CODE_CACHE_TYPE) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 286 | \ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 287 | V(FIXED_ARRAY_TYPE) \ |
| 288 | V(SHARED_FUNCTION_INFO_TYPE) \ |
| 289 | \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 290 | V(JS_VALUE_TYPE) \ |
| 291 | V(JS_OBJECT_TYPE) \ |
| 292 | V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \ |
| 293 | V(JS_GLOBAL_OBJECT_TYPE) \ |
| 294 | V(JS_BUILTINS_OBJECT_TYPE) \ |
| 295 | V(JS_GLOBAL_PROXY_TYPE) \ |
| 296 | V(JS_ARRAY_TYPE) \ |
| 297 | V(JS_REGEXP_TYPE) \ |
| 298 | \ |
| 299 | V(JS_FUNCTION_TYPE) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | |
| 301 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 302 | #define INSTANCE_TYPE_LIST_DEBUGGER(V) \ |
| 303 | V(DEBUG_INFO_TYPE) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | V(BREAK_POINT_INFO_TYPE) |
| 305 | #else |
| 306 | #define INSTANCE_TYPE_LIST_DEBUGGER(V) |
| 307 | #endif |
| 308 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 309 | #define INSTANCE_TYPE_LIST(V) \ |
| 310 | INSTANCE_TYPE_LIST_ALL(V) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 311 | INSTANCE_TYPE_LIST_DEBUGGER(V) |
| 312 | |
| 313 | |
| 314 | // Since string types are not consecutive, this macro is used to |
| 315 | // iterate over them. |
| 316 | #define STRING_TYPE_LIST(V) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 317 | V(SYMBOL_TYPE, \ |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 318 | kVariableSizeSentinel, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 319 | symbol, \ |
| 320 | Symbol) \ |
| 321 | V(ASCII_SYMBOL_TYPE, \ |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 322 | kVariableSizeSentinel, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 323 | ascii_symbol, \ |
| 324 | AsciiSymbol) \ |
| 325 | V(CONS_SYMBOL_TYPE, \ |
| 326 | ConsString::kSize, \ |
| 327 | cons_symbol, \ |
| 328 | ConsSymbol) \ |
| 329 | V(CONS_ASCII_SYMBOL_TYPE, \ |
| 330 | ConsString::kSize, \ |
| 331 | cons_ascii_symbol, \ |
| 332 | ConsAsciiSymbol) \ |
| 333 | V(EXTERNAL_SYMBOL_TYPE, \ |
| 334 | ExternalTwoByteString::kSize, \ |
| 335 | external_symbol, \ |
| 336 | ExternalSymbol) \ |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 337 | V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \ |
| 338 | ExternalTwoByteString::kSize, \ |
| 339 | external_symbol_with_ascii_data, \ |
| 340 | ExternalSymbolWithAsciiData) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 341 | V(EXTERNAL_ASCII_SYMBOL_TYPE, \ |
| 342 | ExternalAsciiString::kSize, \ |
| 343 | external_ascii_symbol, \ |
| 344 | ExternalAsciiSymbol) \ |
| 345 | V(STRING_TYPE, \ |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 346 | kVariableSizeSentinel, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 347 | string, \ |
| 348 | String) \ |
| 349 | V(ASCII_STRING_TYPE, \ |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 350 | kVariableSizeSentinel, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 351 | ascii_string, \ |
| 352 | AsciiString) \ |
| 353 | V(CONS_STRING_TYPE, \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 354 | ConsString::kSize, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 355 | cons_string, \ |
| 356 | ConsString) \ |
| 357 | V(CONS_ASCII_STRING_TYPE, \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 358 | ConsString::kSize, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 359 | cons_ascii_string, \ |
| 360 | ConsAsciiString) \ |
| 361 | V(EXTERNAL_STRING_TYPE, \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 362 | ExternalTwoByteString::kSize, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 363 | external_string, \ |
| 364 | ExternalString) \ |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 365 | V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \ |
| 366 | ExternalTwoByteString::kSize, \ |
| 367 | external_string_with_ascii_data, \ |
| 368 | ExternalStringWithAsciiData) \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 369 | V(EXTERNAL_ASCII_STRING_TYPE, \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | ExternalAsciiString::kSize, \ |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 371 | external_ascii_string, \ |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 372 | ExternalAsciiString) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | |
| 374 | // A struct is a simple object a set of object-valued fields. Including an |
| 375 | // object type in this causes the compiler to generate most of the boilerplate |
| 376 | // code for the class including allocation and garbage collection routines, |
| 377 | // casts and predicates. All you need to define is the class, methods and |
| 378 | // object verification routines. Easy, no? |
| 379 | // |
| 380 | // Note that for subtle reasons related to the ordering or numerical values of |
| 381 | // type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST |
| 382 | // manually. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 383 | #define STRUCT_LIST_ALL(V) \ |
| 384 | V(ACCESSOR_INFO, AccessorInfo, accessor_info) \ |
| 385 | V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \ |
| 386 | V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \ |
| 387 | V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \ |
| 388 | V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \ |
| 389 | V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \ |
| 390 | V(SIGNATURE_INFO, SignatureInfo, signature_info) \ |
| 391 | V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \ |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 392 | V(SCRIPT, Script, script) \ |
| 393 | V(CODE_CACHE, CodeCache, code_cache) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 394 | |
| 395 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 396 | #define STRUCT_LIST_DEBUGGER(V) \ |
| 397 | V(DEBUG_INFO, DebugInfo, debug_info) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 398 | V(BREAK_POINT_INFO, BreakPointInfo, break_point_info) |
| 399 | #else |
| 400 | #define STRUCT_LIST_DEBUGGER(V) |
| 401 | #endif |
| 402 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 403 | #define STRUCT_LIST(V) \ |
| 404 | STRUCT_LIST_ALL(V) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 405 | STRUCT_LIST_DEBUGGER(V) |
| 406 | |
| 407 | // We use the full 8 bits of the instance_type field to encode heap object |
| 408 | // instance types. The high-order bit (bit 7) is set if the object is not a |
| 409 | // string, and cleared if it is a string. |
| 410 | const uint32_t kIsNotStringMask = 0x80; |
| 411 | const uint32_t kStringTag = 0x0; |
| 412 | const uint32_t kNotStringTag = 0x80; |
| 413 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 414 | // Bit 6 indicates that the object is a symbol (if set) or not (if cleared). |
| 415 | // There are not enough types that the non-string types (with bit 7 set) can |
| 416 | // have bit 6 set too. |
| 417 | const uint32_t kIsSymbolMask = 0x40; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 418 | const uint32_t kNotSymbolTag = 0x0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 419 | const uint32_t kSymbolTag = 0x40; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 420 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 421 | // If bit 7 is clear then bit 2 indicates whether the string consists of |
| 422 | // two-byte characters or one-byte characters. |
| 423 | const uint32_t kStringEncodingMask = 0x4; |
| 424 | const uint32_t kTwoByteStringTag = 0x0; |
| 425 | const uint32_t kAsciiStringTag = 0x4; |
| 426 | |
| 427 | // If bit 7 is clear, the low-order 2 bits indicate the representation |
| 428 | // of the string. |
| 429 | const uint32_t kStringRepresentationMask = 0x03; |
| 430 | enum StringRepresentationTag { |
| 431 | kSeqStringTag = 0x0, |
| 432 | kConsStringTag = 0x1, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 433 | kExternalStringTag = 0x2 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 434 | }; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 435 | const uint32_t kIsConsStringMask = 0x1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 437 | // If bit 7 is clear, then bit 3 indicates whether this two-byte |
| 438 | // string actually contains ascii data. |
| 439 | const uint32_t kAsciiDataHintMask = 0x08; |
| 440 | const uint32_t kAsciiDataHintTag = 0x08; |
| 441 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 442 | |
| 443 | // A ConsString with an empty string as the right side is a candidate |
| 444 | // for being shortcut by the garbage collector unless it is a |
| 445 | // symbol. It's not common to have non-flat symbols, so we do not |
| 446 | // shortcut them thereby avoiding turning symbols into strings. See |
| 447 | // heap.cc and mark-compact.cc. |
| 448 | const uint32_t kShortcutTypeMask = |
| 449 | kIsNotStringMask | |
| 450 | kIsSymbolMask | |
| 451 | kStringRepresentationMask; |
| 452 | const uint32_t kShortcutTypeTag = kConsStringTag; |
| 453 | |
| 454 | |
| 455 | enum InstanceType { |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 456 | // String types. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 457 | SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 458 | ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 459 | CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 460 | CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 461 | EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag, |
| 462 | EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE = |
| 463 | kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 464 | EXTERNAL_ASCII_SYMBOL_TYPE = |
| 465 | kAsciiStringTag | kSymbolTag | kExternalStringTag, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 466 | STRING_TYPE = kTwoByteStringTag | kSeqStringTag, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 467 | ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 468 | CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 469 | CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 470 | EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag, |
| 471 | EXTERNAL_STRING_WITH_ASCII_DATA_TYPE = |
| 472 | kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag, |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 473 | EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag, |
| 474 | PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 475 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 476 | // Objects allocated in their own spaces (never in new space). |
| 477 | MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 478 | CODE_TYPE, |
| 479 | ODDBALL_TYPE, |
| 480 | JS_GLOBAL_PROPERTY_CELL_TYPE, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 481 | |
| 482 | // "Data", objects that cannot contain non-map-word pointers to heap |
| 483 | // objects. |
| 484 | HEAP_NUMBER_TYPE, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 485 | PROXY_TYPE, |
| 486 | BYTE_ARRAY_TYPE, |
| 487 | PIXEL_ARRAY_TYPE, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 488 | EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 489 | EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE, |
| 490 | EXTERNAL_SHORT_ARRAY_TYPE, |
| 491 | EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE, |
| 492 | EXTERNAL_INT_ARRAY_TYPE, |
| 493 | EXTERNAL_UNSIGNED_INT_ARRAY_TYPE, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 494 | EXTERNAL_FLOAT_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE |
| 495 | FILLER_TYPE, // LAST_DATA_TYPE |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 496 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 497 | // Structs. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 498 | ACCESSOR_INFO_TYPE, |
| 499 | ACCESS_CHECK_INFO_TYPE, |
| 500 | INTERCEPTOR_INFO_TYPE, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 501 | CALL_HANDLER_INFO_TYPE, |
| 502 | FUNCTION_TEMPLATE_INFO_TYPE, |
| 503 | OBJECT_TEMPLATE_INFO_TYPE, |
| 504 | SIGNATURE_INFO_TYPE, |
| 505 | TYPE_SWITCH_INFO_TYPE, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 506 | SCRIPT_TYPE, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 507 | CODE_CACHE_TYPE, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 508 | // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT |
| 509 | // is defined. However as include/v8.h contain some of the instance type |
| 510 | // constants always having them avoids them getting different numbers |
| 511 | // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 512 | DEBUG_INFO_TYPE, |
| 513 | BREAK_POINT_INFO_TYPE, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 514 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 515 | FIXED_ARRAY_TYPE, |
| 516 | SHARED_FUNCTION_INFO_TYPE, |
| 517 | |
| 518 | JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | JS_OBJECT_TYPE, |
| 520 | JS_CONTEXT_EXTENSION_OBJECT_TYPE, |
| 521 | JS_GLOBAL_OBJECT_TYPE, |
| 522 | JS_BUILTINS_OBJECT_TYPE, |
| 523 | JS_GLOBAL_PROXY_TYPE, |
| 524 | JS_ARRAY_TYPE, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 525 | JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 526 | |
| 527 | JS_FUNCTION_TYPE, |
| 528 | |
| 529 | // Pseudo-types |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 530 | FIRST_TYPE = 0x0, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 531 | LAST_TYPE = JS_FUNCTION_TYPE, |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 532 | INVALID_TYPE = FIRST_TYPE - 1, |
| 533 | FIRST_NONSTRING_TYPE = MAP_TYPE, |
| 534 | // Boundaries for testing for an external array. |
| 535 | FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE, |
| 536 | LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_FLOAT_ARRAY_TYPE, |
| 537 | // Boundary for promotion to old data space/old pointer space. |
| 538 | LAST_DATA_TYPE = FILLER_TYPE, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | // Boundaries for testing the type is a JavaScript "object". Note that |
| 540 | // function objects are not counted as objects, even though they are |
| 541 | // implemented as such; only values whose typeof is "object" are included. |
| 542 | FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE, |
| 543 | LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE |
| 544 | }; |
| 545 | |
| 546 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 547 | STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType); |
| 548 | STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType); |
| 549 | STATIC_CHECK(PROXY_TYPE == Internals::kProxyType); |
| 550 | |
| 551 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 552 | enum CompareResult { |
| 553 | LESS = -1, |
| 554 | EQUAL = 0, |
| 555 | GREATER = 1, |
| 556 | |
| 557 | NOT_EQUAL = GREATER |
| 558 | }; |
| 559 | |
| 560 | |
| 561 | #define DECL_BOOLEAN_ACCESSORS(name) \ |
| 562 | inline bool name(); \ |
| 563 | inline void set_##name(bool value); \ |
| 564 | |
| 565 | |
| 566 | #define DECL_ACCESSORS(name, type) \ |
| 567 | inline type* name(); \ |
| 568 | inline void set_##name(type* value, \ |
| 569 | WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \ |
| 570 | |
| 571 | |
| 572 | class StringStream; |
| 573 | class ObjectVisitor; |
| 574 | |
| 575 | struct ValueInfo : public Malloced { |
| 576 | ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { } |
| 577 | InstanceType type; |
| 578 | Object* ptr; |
| 579 | const char* str; |
| 580 | double number; |
| 581 | }; |
| 582 | |
| 583 | |
| 584 | // A template-ized version of the IsXXX functions. |
| 585 | template <class C> static inline bool Is(Object* obj); |
| 586 | |
| 587 | |
| 588 | // Object is the abstract superclass for all classes in the |
| 589 | // object hierarchy. |
| 590 | // Object does not use any virtual functions to avoid the |
| 591 | // allocation of the C++ vtable. |
| 592 | // Since Smi and Failure are subclasses of Object no |
| 593 | // data members can be present in Object. |
| 594 | class Object BASE_EMBEDDED { |
| 595 | public: |
| 596 | // Type testing. |
| 597 | inline bool IsSmi(); |
| 598 | inline bool IsHeapObject(); |
| 599 | inline bool IsHeapNumber(); |
| 600 | inline bool IsString(); |
| 601 | inline bool IsSymbol(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 602 | // See objects-inl.h for more details |
| 603 | inline bool IsSeqString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 604 | inline bool IsExternalString(); |
| 605 | inline bool IsExternalTwoByteString(); |
| 606 | inline bool IsExternalAsciiString(); |
| 607 | inline bool IsSeqTwoByteString(); |
| 608 | inline bool IsSeqAsciiString(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 609 | inline bool IsConsString(); |
| 610 | |
| 611 | inline bool IsNumber(); |
| 612 | inline bool IsByteArray(); |
| 613 | inline bool IsPixelArray(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 614 | inline bool IsExternalArray(); |
| 615 | inline bool IsExternalByteArray(); |
| 616 | inline bool IsExternalUnsignedByteArray(); |
| 617 | inline bool IsExternalShortArray(); |
| 618 | inline bool IsExternalUnsignedShortArray(); |
| 619 | inline bool IsExternalIntArray(); |
| 620 | inline bool IsExternalUnsignedIntArray(); |
| 621 | inline bool IsExternalFloatArray(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 622 | inline bool IsFailure(); |
| 623 | inline bool IsRetryAfterGC(); |
| 624 | inline bool IsOutOfMemoryFailure(); |
| 625 | inline bool IsException(); |
| 626 | inline bool IsJSObject(); |
| 627 | inline bool IsJSContextExtensionObject(); |
| 628 | inline bool IsMap(); |
| 629 | inline bool IsFixedArray(); |
| 630 | inline bool IsDescriptorArray(); |
| 631 | inline bool IsContext(); |
| 632 | inline bool IsCatchContext(); |
| 633 | inline bool IsGlobalContext(); |
| 634 | inline bool IsJSFunction(); |
| 635 | inline bool IsCode(); |
| 636 | inline bool IsOddball(); |
| 637 | inline bool IsSharedFunctionInfo(); |
| 638 | inline bool IsJSValue(); |
| 639 | inline bool IsStringWrapper(); |
| 640 | inline bool IsProxy(); |
| 641 | inline bool IsBoolean(); |
| 642 | inline bool IsJSArray(); |
| 643 | inline bool IsJSRegExp(); |
| 644 | inline bool IsHashTable(); |
| 645 | inline bool IsDictionary(); |
| 646 | inline bool IsSymbolTable(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 647 | inline bool IsJSFunctionResultCache(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 648 | inline bool IsNormalizedMapCache(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 649 | inline bool IsCompilationCacheTable(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 650 | inline bool IsCodeCacheHashTable(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | inline bool IsMapCache(); |
| 652 | inline bool IsPrimitive(); |
| 653 | inline bool IsGlobalObject(); |
| 654 | inline bool IsJSGlobalObject(); |
| 655 | inline bool IsJSBuiltinsObject(); |
| 656 | inline bool IsJSGlobalProxy(); |
| 657 | inline bool IsUndetectableObject(); |
| 658 | inline bool IsAccessCheckNeeded(); |
| 659 | inline bool IsJSGlobalPropertyCell(); |
| 660 | |
| 661 | // Returns true if this object is an instance of the specified |
| 662 | // function template. |
| 663 | inline bool IsInstanceOf(FunctionTemplateInfo* type); |
| 664 | |
| 665 | inline bool IsStruct(); |
| 666 | #define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name(); |
| 667 | STRUCT_LIST(DECLARE_STRUCT_PREDICATE) |
| 668 | #undef DECLARE_STRUCT_PREDICATE |
| 669 | |
| 670 | // Oddball testing. |
| 671 | INLINE(bool IsUndefined()); |
| 672 | INLINE(bool IsTheHole()); |
| 673 | INLINE(bool IsNull()); |
| 674 | INLINE(bool IsTrue()); |
| 675 | INLINE(bool IsFalse()); |
| 676 | |
| 677 | // Extract the number. |
| 678 | inline double Number(); |
| 679 | |
| 680 | inline bool HasSpecificClassOf(String* name); |
| 681 | |
| 682 | Object* ToObject(); // ECMA-262 9.9. |
| 683 | Object* ToBoolean(); // ECMA-262 9.2. |
| 684 | |
| 685 | // Convert to a JSObject if needed. |
| 686 | // global_context is used when creating wrapper object. |
| 687 | Object* ToObject(Context* global_context); |
| 688 | |
| 689 | // Converts this to a Smi if possible. |
| 690 | // Failure is returned otherwise. |
| 691 | inline Object* ToSmi(); |
| 692 | |
| 693 | void Lookup(String* name, LookupResult* result); |
| 694 | |
| 695 | // Property access. |
| 696 | inline Object* GetProperty(String* key); |
| 697 | inline Object* GetProperty(String* key, PropertyAttributes* attributes); |
| 698 | Object* GetPropertyWithReceiver(Object* receiver, |
| 699 | String* key, |
| 700 | PropertyAttributes* attributes); |
| 701 | Object* GetProperty(Object* receiver, |
| 702 | LookupResult* result, |
| 703 | String* key, |
| 704 | PropertyAttributes* attributes); |
| 705 | Object* GetPropertyWithCallback(Object* receiver, |
| 706 | Object* structure, |
| 707 | String* name, |
| 708 | Object* holder); |
| 709 | Object* GetPropertyWithDefinedGetter(Object* receiver, |
| 710 | JSFunction* getter); |
| 711 | |
| 712 | inline Object* GetElement(uint32_t index); |
| 713 | Object* GetElementWithReceiver(Object* receiver, uint32_t index); |
| 714 | |
| 715 | // Return the object's prototype (might be Heap::null_value()). |
| 716 | Object* GetPrototype(); |
| 717 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 718 | // Tries to convert an object to an array index. Returns true and sets |
| 719 | // the output parameter if it succeeds. |
| 720 | inline bool ToArrayIndex(uint32_t* index); |
| 721 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 722 | // Returns true if this is a JSValue containing a string and the index is |
| 723 | // < the length of the string. Used to implement [] on strings. |
| 724 | inline bool IsStringObjectWithCharacterAt(uint32_t index); |
| 725 | |
| 726 | #ifdef DEBUG |
| 727 | // Prints this object with details. |
| 728 | void Print(); |
| 729 | void PrintLn(); |
| 730 | // Verifies the object. |
| 731 | void Verify(); |
| 732 | |
| 733 | // Verify a pointer is a valid object pointer. |
| 734 | static void VerifyPointer(Object* p); |
| 735 | #endif |
| 736 | |
| 737 | // Prints this object without details. |
| 738 | void ShortPrint(); |
| 739 | |
| 740 | // Prints this object without details to a message accumulator. |
| 741 | void ShortPrint(StringStream* accumulator); |
| 742 | |
| 743 | // Casting: This cast is only needed to satisfy macros in objects-inl.h. |
| 744 | static Object* cast(Object* value) { return value; } |
| 745 | |
| 746 | // Layout description. |
| 747 | static const int kHeaderSize = 0; // Object does not take up any space. |
| 748 | |
| 749 | private: |
| 750 | DISALLOW_IMPLICIT_CONSTRUCTORS(Object); |
| 751 | }; |
| 752 | |
| 753 | |
| 754 | // Smi represents integer Numbers that can be stored in 31 bits. |
| 755 | // Smis are immediate which means they are NOT allocated in the heap. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 756 | // The this pointer has the following format: [31 bit signed int] 0 |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 757 | // For long smis it has the following format: |
| 758 | // [32 bit signed int] [31 bits zero padding] 0 |
| 759 | // Smi stands for small integer. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 760 | class Smi: public Object { |
| 761 | public: |
| 762 | // Returns the integer value. |
| 763 | inline int value(); |
| 764 | |
| 765 | // Convert a value to a Smi object. |
| 766 | static inline Smi* FromInt(int value); |
| 767 | |
| 768 | static inline Smi* FromIntptr(intptr_t value); |
| 769 | |
| 770 | // Returns whether value can be represented in a Smi. |
| 771 | static inline bool IsValid(intptr_t value); |
| 772 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 773 | // Casting. |
| 774 | static inline Smi* cast(Object* object); |
| 775 | |
| 776 | // Dispatched behavior. |
| 777 | void SmiPrint(); |
| 778 | void SmiPrint(StringStream* accumulator); |
| 779 | #ifdef DEBUG |
| 780 | void SmiVerify(); |
| 781 | #endif |
| 782 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 783 | static const int kMinValue = (-1 << (kSmiValueSize - 1)); |
| 784 | static const int kMaxValue = -(kMinValue + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 785 | |
| 786 | private: |
| 787 | DISALLOW_IMPLICIT_CONSTRUCTORS(Smi); |
| 788 | }; |
| 789 | |
| 790 | |
| 791 | // Failure is used for reporting out of memory situations and |
| 792 | // propagating exceptions through the runtime system. Failure objects |
| 793 | // are transient and cannot occur as part of the object graph. |
| 794 | // |
| 795 | // Failures are a single word, encoded as follows: |
| 796 | // +-------------------------+---+--+--+ |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 797 | // |...rrrrrrrrrrrrrrrrrrrrrr|sss|tt|11| |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 798 | // +-------------------------+---+--+--+ |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 799 | // 7 6 4 32 10 |
| 800 | // |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 801 | // |
| 802 | // The low two bits, 0-1, are the failure tag, 11. The next two bits, |
| 803 | // 2-3, are a failure type tag 'tt' with possible values: |
| 804 | // 00 RETRY_AFTER_GC |
| 805 | // 01 EXCEPTION |
| 806 | // 10 INTERNAL_ERROR |
| 807 | // 11 OUT_OF_MEMORY_EXCEPTION |
| 808 | // |
| 809 | // The next three bits, 4-6, are an allocation space tag 'sss'. The |
| 810 | // allocation space tag is 000 for all failure types except |
| 811 | // RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the |
| 812 | // allocation spaces (the encoding is found in globals.h). |
| 813 | // |
| 814 | // The remaining bits is the size of the allocation request in units |
| 815 | // of the pointer size, and is zeroed except for RETRY_AFTER_GC |
| 816 | // failures. The 25 bits (on a 32 bit platform) gives a representable |
| 817 | // range of 2^27 bytes (128MB). |
| 818 | |
| 819 | // Failure type tag info. |
| 820 | const int kFailureTypeTagSize = 2; |
| 821 | const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1; |
| 822 | |
| 823 | class Failure: public Object { |
| 824 | public: |
| 825 | // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code. |
| 826 | enum Type { |
| 827 | RETRY_AFTER_GC = 0, |
| 828 | EXCEPTION = 1, // Returning this marker tells the real exception |
| 829 | // is in Top::pending_exception. |
| 830 | INTERNAL_ERROR = 2, |
| 831 | OUT_OF_MEMORY_EXCEPTION = 3 |
| 832 | }; |
| 833 | |
| 834 | inline Type type() const; |
| 835 | |
| 836 | // Returns the space that needs to be collected for RetryAfterGC failures. |
| 837 | inline AllocationSpace allocation_space() const; |
| 838 | |
| 839 | // Returns the number of bytes requested (up to the representable maximum) |
| 840 | // for RetryAfterGC failures. |
| 841 | inline int requested() const; |
| 842 | |
| 843 | inline bool IsInternalError() const; |
| 844 | inline bool IsOutOfMemoryException() const; |
| 845 | |
| 846 | static Failure* RetryAfterGC(int requested_bytes, AllocationSpace space); |
| 847 | static inline Failure* RetryAfterGC(int requested_bytes); // NEW_SPACE |
| 848 | static inline Failure* Exception(); |
| 849 | static inline Failure* InternalError(); |
| 850 | static inline Failure* OutOfMemoryException(); |
| 851 | // Casting. |
| 852 | static inline Failure* cast(Object* object); |
| 853 | |
| 854 | // Dispatched behavior. |
| 855 | void FailurePrint(); |
| 856 | void FailurePrint(StringStream* accumulator); |
| 857 | #ifdef DEBUG |
| 858 | void FailureVerify(); |
| 859 | #endif |
| 860 | |
| 861 | private: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 862 | inline intptr_t value() const; |
| 863 | static inline Failure* Construct(Type type, intptr_t value = 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 864 | |
| 865 | DISALLOW_IMPLICIT_CONSTRUCTORS(Failure); |
| 866 | }; |
| 867 | |
| 868 | |
| 869 | // Heap objects typically have a map pointer in their first word. However, |
| 870 | // during GC other data (eg, mark bits, forwarding addresses) is sometimes |
| 871 | // encoded in the first word. The class MapWord is an abstraction of the |
| 872 | // value in a heap object's first word. |
| 873 | class MapWord BASE_EMBEDDED { |
| 874 | public: |
| 875 | // Normal state: the map word contains a map pointer. |
| 876 | |
| 877 | // Create a map word from a map pointer. |
| 878 | static inline MapWord FromMap(Map* map); |
| 879 | |
| 880 | // View this map word as a map pointer. |
| 881 | inline Map* ToMap(); |
| 882 | |
| 883 | |
| 884 | // Scavenge collection: the map word of live objects in the from space |
| 885 | // contains a forwarding address (a heap object pointer in the to space). |
| 886 | |
| 887 | // True if this map word is a forwarding address for a scavenge |
| 888 | // collection. Only valid during a scavenge collection (specifically, |
| 889 | // when all map words are heap object pointers, ie. not during a full GC). |
| 890 | inline bool IsForwardingAddress(); |
| 891 | |
| 892 | // Create a map word from a forwarding address. |
| 893 | static inline MapWord FromForwardingAddress(HeapObject* object); |
| 894 | |
| 895 | // View this map word as a forwarding address. |
| 896 | inline HeapObject* ToForwardingAddress(); |
| 897 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 898 | // Marking phase of full collection: the map word of live objects is |
| 899 | // marked, and may be marked as overflowed (eg, the object is live, its |
| 900 | // children have not been visited, and it does not fit in the marking |
| 901 | // stack). |
| 902 | |
| 903 | // True if this map word's mark bit is set. |
| 904 | inline bool IsMarked(); |
| 905 | |
| 906 | // Return this map word but with its mark bit set. |
| 907 | inline void SetMark(); |
| 908 | |
| 909 | // Return this map word but with its mark bit cleared. |
| 910 | inline void ClearMark(); |
| 911 | |
| 912 | // True if this map word's overflow bit is set. |
| 913 | inline bool IsOverflowed(); |
| 914 | |
| 915 | // Return this map word but with its overflow bit set. |
| 916 | inline void SetOverflow(); |
| 917 | |
| 918 | // Return this map word but with its overflow bit cleared. |
| 919 | inline void ClearOverflow(); |
| 920 | |
| 921 | |
| 922 | // Compacting phase of a full compacting collection: the map word of live |
| 923 | // objects contains an encoding of the original map address along with the |
| 924 | // forwarding address (represented as an offset from the first live object |
| 925 | // in the same page as the (old) object address). |
| 926 | |
| 927 | // Create a map word from a map address and a forwarding address offset. |
| 928 | static inline MapWord EncodeAddress(Address map_address, int offset); |
| 929 | |
| 930 | // Return the map address encoded in this map word. |
| 931 | inline Address DecodeMapAddress(MapSpace* map_space); |
| 932 | |
| 933 | // Return the forwarding offset encoded in this map word. |
| 934 | inline int DecodeOffset(); |
| 935 | |
| 936 | |
| 937 | // During serialization: the map word is used to hold an encoded |
| 938 | // address, and possibly a mark bit (set and cleared with SetMark |
| 939 | // and ClearMark). |
| 940 | |
| 941 | // Create a map word from an encoded address. |
| 942 | static inline MapWord FromEncodedAddress(Address address); |
| 943 | |
| 944 | inline Address ToEncodedAddress(); |
| 945 | |
| 946 | // Bits used by the marking phase of the garbage collector. |
| 947 | // |
| 948 | // The first word of a heap object is normally a map pointer. The last two |
| 949 | // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to |
| 950 | // mark an object as live and/or overflowed: |
| 951 | // last bit = 0, marked as alive |
| 952 | // second bit = 1, overflowed |
| 953 | // An object is only marked as overflowed when it is marked as live while |
| 954 | // the marking stack is overflowed. |
| 955 | static const int kMarkingBit = 0; // marking bit |
| 956 | static const int kMarkingMask = (1 << kMarkingBit); // marking mask |
| 957 | static const int kOverflowBit = 1; // overflow bit |
| 958 | static const int kOverflowMask = (1 << kOverflowBit); // overflow mask |
| 959 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 960 | // Forwarding pointers and map pointer encoding. On 32 bit all the bits are |
| 961 | // used. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 962 | // +-----------------+------------------+-----------------+ |
| 963 | // |forwarding offset|page offset of map|page index of map| |
| 964 | // +-----------------+------------------+-----------------+ |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 965 | // ^ ^ ^ |
| 966 | // | | | |
| 967 | // | | kMapPageIndexBits |
| 968 | // | kMapPageOffsetBits |
| 969 | // kForwardingOffsetBits |
| 970 | static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits; |
| 971 | static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits; |
| 972 | #ifdef V8_HOST_ARCH_64_BIT |
| 973 | static const int kMapPageIndexBits = 16; |
| 974 | #else |
| 975 | // Use all the 32-bits to encode on a 32-bit platform. |
| 976 | static const int kMapPageIndexBits = |
| 977 | 32 - (kMapPageOffsetBits + kForwardingOffsetBits); |
| 978 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 979 | |
| 980 | static const int kMapPageIndexShift = 0; |
| 981 | static const int kMapPageOffsetShift = |
| 982 | kMapPageIndexShift + kMapPageIndexBits; |
| 983 | static const int kForwardingOffsetShift = |
| 984 | kMapPageOffsetShift + kMapPageOffsetBits; |
| 985 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 986 | // Bit masks covering the different parts the encoding. |
| 987 | static const uintptr_t kMapPageIndexMask = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 988 | (1 << kMapPageOffsetShift) - 1; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 989 | static const uintptr_t kMapPageOffsetMask = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 990 | ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 991 | static const uintptr_t kForwardingOffsetMask = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 992 | ~(kMapPageIndexMask | kMapPageOffsetMask); |
| 993 | |
| 994 | private: |
| 995 | // HeapObject calls the private constructor and directly reads the value. |
| 996 | friend class HeapObject; |
| 997 | |
| 998 | explicit MapWord(uintptr_t value) : value_(value) {} |
| 999 | |
| 1000 | uintptr_t value_; |
| 1001 | }; |
| 1002 | |
| 1003 | |
| 1004 | // HeapObject is the superclass for all classes describing heap allocated |
| 1005 | // objects. |
| 1006 | class HeapObject: public Object { |
| 1007 | public: |
| 1008 | // [map]: Contains a map which contains the object's reflective |
| 1009 | // information. |
| 1010 | inline Map* map(); |
| 1011 | inline void set_map(Map* value); |
| 1012 | |
| 1013 | // During garbage collection, the map word of a heap object does not |
| 1014 | // necessarily contain a map pointer. |
| 1015 | inline MapWord map_word(); |
| 1016 | inline void set_map_word(MapWord map_word); |
| 1017 | |
| 1018 | // Converts an address to a HeapObject pointer. |
| 1019 | static inline HeapObject* FromAddress(Address address); |
| 1020 | |
| 1021 | // Returns the address of this HeapObject. |
| 1022 | inline Address address(); |
| 1023 | |
| 1024 | // Iterates over pointers contained in the object (including the Map) |
| 1025 | void Iterate(ObjectVisitor* v); |
| 1026 | |
| 1027 | // Iterates over all pointers contained in the object except the |
| 1028 | // first map pointer. The object type is given in the first |
| 1029 | // parameter. This function does not access the map pointer in the |
| 1030 | // object, and so is safe to call while the map pointer is modified. |
| 1031 | void IterateBody(InstanceType type, int object_size, ObjectVisitor* v); |
| 1032 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1033 | // Returns the heap object's size in bytes |
| 1034 | inline int Size(); |
| 1035 | |
| 1036 | // Given a heap object's map pointer, returns the heap size in bytes |
| 1037 | // Useful when the map pointer field is used for other purposes. |
| 1038 | // GC internal. |
| 1039 | inline int SizeFromMap(Map* map); |
| 1040 | |
| 1041 | // Support for the marking heap objects during the marking phase of GC. |
| 1042 | // True if the object is marked live. |
| 1043 | inline bool IsMarked(); |
| 1044 | |
| 1045 | // Mutate this object's map pointer to indicate that the object is live. |
| 1046 | inline void SetMark(); |
| 1047 | |
| 1048 | // Mutate this object's map pointer to remove the indication that the |
| 1049 | // object is live (ie, partially restore the map pointer). |
| 1050 | inline void ClearMark(); |
| 1051 | |
| 1052 | // True if this object is marked as overflowed. Overflowed objects have |
| 1053 | // been reached and marked during marking of the heap, but their children |
| 1054 | // have not necessarily been marked and they have not been pushed on the |
| 1055 | // marking stack. |
| 1056 | inline bool IsOverflowed(); |
| 1057 | |
| 1058 | // Mutate this object's map pointer to indicate that the object is |
| 1059 | // overflowed. |
| 1060 | inline void SetOverflow(); |
| 1061 | |
| 1062 | // Mutate this object's map pointer to remove the indication that the |
| 1063 | // object is overflowed (ie, partially restore the map pointer). |
| 1064 | inline void ClearOverflow(); |
| 1065 | |
| 1066 | // Returns the field at offset in obj, as a read/write Object* reference. |
| 1067 | // Does no checking, and is safe to use during GC, while maps are invalid. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1068 | // Does not invoke write barrier, so should only be assigned to |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1069 | // during marking GC. |
| 1070 | static inline Object** RawField(HeapObject* obj, int offset); |
| 1071 | |
| 1072 | // Casting. |
| 1073 | static inline HeapObject* cast(Object* obj); |
| 1074 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1075 | // Return the write barrier mode for this. Callers of this function |
| 1076 | // must be able to present a reference to an AssertNoAllocation |
| 1077 | // object as a sign that they are not going to use this function |
| 1078 | // from code that allocates and thus invalidates the returned write |
| 1079 | // barrier mode. |
| 1080 | inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1081 | |
| 1082 | // Dispatched behavior. |
| 1083 | void HeapObjectShortPrint(StringStream* accumulator); |
| 1084 | #ifdef DEBUG |
| 1085 | void HeapObjectPrint(); |
| 1086 | void HeapObjectVerify(); |
| 1087 | inline void VerifyObjectField(int offset); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1088 | inline void VerifySmiField(int offset); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1089 | |
| 1090 | void PrintHeader(const char* id); |
| 1091 | |
| 1092 | // Verify a pointer is a valid HeapObject pointer that points to object |
| 1093 | // areas in the heap. |
| 1094 | static void VerifyHeapPointer(Object* p); |
| 1095 | #endif |
| 1096 | |
| 1097 | // Layout description. |
| 1098 | // First field in a heap object is map. |
| 1099 | static const int kMapOffset = Object::kHeaderSize; |
| 1100 | static const int kHeaderSize = kMapOffset + kPointerSize; |
| 1101 | |
| 1102 | STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset); |
| 1103 | |
| 1104 | protected: |
| 1105 | // helpers for calling an ObjectVisitor to iterate over pointers in the |
| 1106 | // half-open range [start, end) specified as integer offsets |
| 1107 | inline void IteratePointers(ObjectVisitor* v, int start, int end); |
| 1108 | // as above, for the single element at "offset" |
| 1109 | inline void IteratePointer(ObjectVisitor* v, int offset); |
| 1110 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1111 | private: |
| 1112 | DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject); |
| 1113 | }; |
| 1114 | |
| 1115 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1116 | #define SLOT_ADDR(obj, offset) \ |
| 1117 | reinterpret_cast<Object**>((obj)->address() + offset) |
| 1118 | |
| 1119 | // This class describes a body of an object of a fixed size |
| 1120 | // in which all pointer fields are located in the [start_offset, end_offset) |
| 1121 | // interval. |
| 1122 | template<int start_offset, int end_offset, int size> |
| 1123 | class FixedBodyDescriptor { |
| 1124 | public: |
| 1125 | static const int kStartOffset = start_offset; |
| 1126 | static const int kEndOffset = end_offset; |
| 1127 | static const int kSize = size; |
| 1128 | |
| 1129 | static inline void IterateBody(HeapObject* obj, ObjectVisitor* v); |
| 1130 | |
| 1131 | template<typename StaticVisitor> |
| 1132 | static inline void IterateBody(HeapObject* obj) { |
| 1133 | StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset), |
| 1134 | SLOT_ADDR(obj, end_offset)); |
| 1135 | } |
| 1136 | }; |
| 1137 | |
| 1138 | |
| 1139 | // This class describes a body of an object of a variable size |
| 1140 | // in which all pointer fields are located in the [start_offset, object_size) |
| 1141 | // interval. |
| 1142 | template<int start_offset> |
| 1143 | class FlexibleBodyDescriptor { |
| 1144 | public: |
| 1145 | static const int kStartOffset = start_offset; |
| 1146 | |
| 1147 | static inline void IterateBody(HeapObject* obj, |
| 1148 | int object_size, |
| 1149 | ObjectVisitor* v); |
| 1150 | |
| 1151 | template<typename StaticVisitor> |
| 1152 | static inline void IterateBody(HeapObject* obj, int object_size) { |
| 1153 | StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset), |
| 1154 | SLOT_ADDR(obj, object_size)); |
| 1155 | } |
| 1156 | }; |
| 1157 | |
| 1158 | #undef SLOT_ADDR |
| 1159 | |
| 1160 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1161 | // The HeapNumber class describes heap allocated numbers that cannot be |
| 1162 | // represented in a Smi (small integer) |
| 1163 | class HeapNumber: public HeapObject { |
| 1164 | public: |
| 1165 | // [value]: number value. |
| 1166 | inline double value(); |
| 1167 | inline void set_value(double value); |
| 1168 | |
| 1169 | // Casting. |
| 1170 | static inline HeapNumber* cast(Object* obj); |
| 1171 | |
| 1172 | // Dispatched behavior. |
| 1173 | Object* HeapNumberToBoolean(); |
| 1174 | void HeapNumberPrint(); |
| 1175 | void HeapNumberPrint(StringStream* accumulator); |
| 1176 | #ifdef DEBUG |
| 1177 | void HeapNumberVerify(); |
| 1178 | #endif |
| 1179 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1180 | inline int get_exponent(); |
| 1181 | inline int get_sign(); |
| 1182 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1183 | // Layout description. |
| 1184 | static const int kValueOffset = HeapObject::kHeaderSize; |
| 1185 | // IEEE doubles are two 32 bit words. The first is just mantissa, the second |
| 1186 | // is a mixture of sign, exponent and mantissa. Our current platforms are all |
| 1187 | // little endian apart from non-EABI arm which is little endian with big |
| 1188 | // endian floating point word ordering! |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1189 | #if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1190 | static const int kMantissaOffset = kValueOffset; |
| 1191 | static const int kExponentOffset = kValueOffset + 4; |
| 1192 | #else |
| 1193 | static const int kMantissaOffset = kValueOffset + 4; |
| 1194 | static const int kExponentOffset = kValueOffset; |
| 1195 | # define BIG_ENDIAN_FLOATING_POINT 1 |
| 1196 | #endif |
| 1197 | static const int kSize = kValueOffset + kDoubleSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1198 | static const uint32_t kSignMask = 0x80000000u; |
| 1199 | static const uint32_t kExponentMask = 0x7ff00000u; |
| 1200 | static const uint32_t kMantissaMask = 0xfffffu; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1201 | static const int kMantissaBits = 52; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1202 | static const int kExponentBits = 11; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1203 | static const int kExponentBias = 1023; |
| 1204 | static const int kExponentShift = 20; |
| 1205 | static const int kMantissaBitsInTopWord = 20; |
| 1206 | static const int kNonMantissaBitsInTopWord = 12; |
| 1207 | |
| 1208 | private: |
| 1209 | DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber); |
| 1210 | }; |
| 1211 | |
| 1212 | |
| 1213 | // The JSObject describes real heap allocated JavaScript objects with |
| 1214 | // properties. |
| 1215 | // Note that the map of JSObject changes during execution to enable inline |
| 1216 | // caching. |
| 1217 | class JSObject: public HeapObject { |
| 1218 | public: |
| 1219 | enum DeleteMode { NORMAL_DELETION, FORCE_DELETION }; |
| 1220 | enum ElementsKind { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1221 | // The only "fast" kind. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1222 | FAST_ELEMENTS, |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1223 | // All the kinds below are "slow". |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1224 | DICTIONARY_ELEMENTS, |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1225 | PIXEL_ELEMENTS, |
| 1226 | EXTERNAL_BYTE_ELEMENTS, |
| 1227 | EXTERNAL_UNSIGNED_BYTE_ELEMENTS, |
| 1228 | EXTERNAL_SHORT_ELEMENTS, |
| 1229 | EXTERNAL_UNSIGNED_SHORT_ELEMENTS, |
| 1230 | EXTERNAL_INT_ELEMENTS, |
| 1231 | EXTERNAL_UNSIGNED_INT_ELEMENTS, |
| 1232 | EXTERNAL_FLOAT_ELEMENTS |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1233 | }; |
| 1234 | |
| 1235 | // [properties]: Backing storage for properties. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1236 | // properties is a FixedArray in the fast case and a Dictionary in the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1237 | // slow case. |
| 1238 | DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties. |
| 1239 | inline void initialize_properties(); |
| 1240 | inline bool HasFastProperties(); |
| 1241 | inline StringDictionary* property_dictionary(); // Gets slow properties. |
| 1242 | |
| 1243 | // [elements]: The elements (properties with names that are integers). |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1244 | // |
| 1245 | // Elements can be in two general modes: fast and slow. Each mode |
| 1246 | // corrensponds to a set of object representations of elements that |
| 1247 | // have something in common. |
| 1248 | // |
| 1249 | // In the fast mode elements is a FixedArray and so each element can |
| 1250 | // be quickly accessed. This fact is used in the generated code. The |
| 1251 | // elements array can have one of the two maps in this mode: |
| 1252 | // fixed_array_map or fixed_cow_array_map (for copy-on-write |
| 1253 | // arrays). In the latter case the elements array may be shared by a |
| 1254 | // few objects and so before writing to any element the array must |
| 1255 | // be copied. Use EnsureWritableFastElements in this case. |
| 1256 | // |
| 1257 | // In the slow mode elements is either a NumberDictionary or a |
| 1258 | // PixelArray or an ExternalArray. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1259 | DECL_ACCESSORS(elements, HeapObject) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1260 | inline void initialize_elements(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1261 | inline Object* ResetElements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1262 | inline ElementsKind GetElementsKind(); |
| 1263 | inline bool HasFastElements(); |
| 1264 | inline bool HasDictionaryElements(); |
| 1265 | inline bool HasPixelElements(); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1266 | inline bool HasExternalArrayElements(); |
| 1267 | inline bool HasExternalByteElements(); |
| 1268 | inline bool HasExternalUnsignedByteElements(); |
| 1269 | inline bool HasExternalShortElements(); |
| 1270 | inline bool HasExternalUnsignedShortElements(); |
| 1271 | inline bool HasExternalIntElements(); |
| 1272 | inline bool HasExternalUnsignedIntElements(); |
| 1273 | inline bool HasExternalFloatElements(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1274 | inline bool AllowsSetElementsLength(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1275 | inline NumberDictionary* element_dictionary(); // Gets slow elements. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1276 | // Requires: this->HasFastElements(). |
| 1277 | inline Object* EnsureWritableFastElements(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1278 | |
| 1279 | // Collects elements starting at index 0. |
| 1280 | // Undefined values are placed after non-undefined values. |
| 1281 | // Returns the number of non-undefined values. |
| 1282 | Object* PrepareElementsForSort(uint32_t limit); |
| 1283 | // As PrepareElementsForSort, but only on objects where elements is |
| 1284 | // a dictionary, and it will stay a dictionary. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1285 | MUST_USE_RESULT Object* PrepareSlowElementsForSort(uint32_t limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1286 | |
| 1287 | Object* SetProperty(String* key, |
| 1288 | Object* value, |
| 1289 | PropertyAttributes attributes); |
| 1290 | Object* SetProperty(LookupResult* result, |
| 1291 | String* key, |
| 1292 | Object* value, |
| 1293 | PropertyAttributes attributes); |
| 1294 | Object* SetPropertyWithFailedAccessCheck(LookupResult* result, |
| 1295 | String* name, |
| 1296 | Object* value); |
| 1297 | Object* SetPropertyWithCallback(Object* structure, |
| 1298 | String* name, |
| 1299 | Object* value, |
| 1300 | JSObject* holder); |
| 1301 | Object* SetPropertyWithDefinedSetter(JSFunction* setter, |
| 1302 | Object* value); |
| 1303 | Object* SetPropertyWithInterceptor(String* name, |
| 1304 | Object* value, |
| 1305 | PropertyAttributes attributes); |
| 1306 | Object* SetPropertyPostInterceptor(String* name, |
| 1307 | Object* value, |
| 1308 | PropertyAttributes attributes); |
| 1309 | Object* IgnoreAttributesAndSetLocalProperty(String* key, |
| 1310 | Object* value, |
| 1311 | PropertyAttributes attributes); |
| 1312 | |
| 1313 | // Retrieve a value in a normalized object given a lookup result. |
| 1314 | // Handles the special representation of JS global objects. |
| 1315 | Object* GetNormalizedProperty(LookupResult* result); |
| 1316 | |
| 1317 | // Sets the property value in a normalized object given a lookup result. |
| 1318 | // Handles the special representation of JS global objects. |
| 1319 | Object* SetNormalizedProperty(LookupResult* result, Object* value); |
| 1320 | |
| 1321 | // Sets the property value in a normalized object given (key, value, details). |
| 1322 | // Handles the special representation of JS global objects. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1323 | MUST_USE_RESULT Object* SetNormalizedProperty(String* name, |
| 1324 | Object* value, |
| 1325 | PropertyDetails details); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Deletes the named property in a normalized object. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1328 | MUST_USE_RESULT Object* DeleteNormalizedProperty(String* name, |
| 1329 | DeleteMode mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1330 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1331 | // Returns the class name ([[Class]] property in the specification). |
| 1332 | String* class_name(); |
| 1333 | |
| 1334 | // Returns the constructor name (the name (possibly, inferred name) of the |
| 1335 | // function that was used to instantiate the object). |
| 1336 | String* constructor_name(); |
| 1337 | |
| 1338 | // Retrieve interceptors. |
| 1339 | InterceptorInfo* GetNamedInterceptor(); |
| 1340 | InterceptorInfo* GetIndexedInterceptor(); |
| 1341 | |
| 1342 | inline PropertyAttributes GetPropertyAttribute(String* name); |
| 1343 | PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver, |
| 1344 | String* name); |
| 1345 | PropertyAttributes GetLocalPropertyAttribute(String* name); |
| 1346 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1347 | MUST_USE_RESULT Object* DefineAccessor(String* name, |
| 1348 | bool is_getter, |
| 1349 | JSFunction* fun, |
| 1350 | PropertyAttributes attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1351 | Object* LookupAccessor(String* name, bool is_getter); |
| 1352 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1353 | MUST_USE_RESULT Object* DefineAccessor(AccessorInfo* info); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1354 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1355 | // Used from Object::GetProperty(). |
| 1356 | Object* GetPropertyWithFailedAccessCheck(Object* receiver, |
| 1357 | LookupResult* result, |
| 1358 | String* name, |
| 1359 | PropertyAttributes* attributes); |
| 1360 | Object* GetPropertyWithInterceptor(JSObject* receiver, |
| 1361 | String* name, |
| 1362 | PropertyAttributes* attributes); |
| 1363 | Object* GetPropertyPostInterceptor(JSObject* receiver, |
| 1364 | String* name, |
| 1365 | PropertyAttributes* attributes); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1366 | Object* GetLocalPropertyPostInterceptor(JSObject* receiver, |
| 1367 | String* name, |
| 1368 | PropertyAttributes* attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1369 | |
| 1370 | // Returns true if this is an instance of an api function and has |
| 1371 | // been modified since it was created. May give false positives. |
| 1372 | bool IsDirty(); |
| 1373 | |
| 1374 | bool HasProperty(String* name) { |
| 1375 | return GetPropertyAttribute(name) != ABSENT; |
| 1376 | } |
| 1377 | |
| 1378 | // Can cause a GC if it hits an interceptor. |
| 1379 | bool HasLocalProperty(String* name) { |
| 1380 | return GetLocalPropertyAttribute(name) != ABSENT; |
| 1381 | } |
| 1382 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 1383 | // If the receiver is a JSGlobalProxy this method will return its prototype, |
| 1384 | // otherwise the result is the receiver itself. |
| 1385 | inline Object* BypassGlobalProxy(); |
| 1386 | |
| 1387 | // Accessors for hidden properties object. |
| 1388 | // |
| 1389 | // Hidden properties are not local properties of the object itself. |
| 1390 | // Instead they are stored on an auxiliary JSObject stored as a local |
| 1391 | // property with a special name Heap::hidden_symbol(). But if the |
| 1392 | // receiver is a JSGlobalProxy then the auxiliary object is a property |
| 1393 | // of its prototype. |
| 1394 | // |
| 1395 | // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be |
| 1396 | // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real |
| 1397 | // holder. |
| 1398 | // |
| 1399 | // These accessors do not touch interceptors or accessors. |
| 1400 | inline bool HasHiddenPropertiesObject(); |
| 1401 | inline Object* GetHiddenPropertiesObject(); |
| 1402 | inline Object* SetHiddenPropertiesObject(Object* hidden_obj); |
| 1403 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1404 | MUST_USE_RESULT Object* DeleteProperty(String* name, DeleteMode mode); |
| 1405 | MUST_USE_RESULT Object* DeleteElement(uint32_t index, DeleteMode mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1406 | |
| 1407 | // Tests for the fast common case for property enumeration. |
| 1408 | bool IsSimpleEnum(); |
| 1409 | |
| 1410 | // Do we want to keep the elements in fast case when increasing the |
| 1411 | // capacity? |
| 1412 | bool ShouldConvertToSlowElements(int new_capacity); |
| 1413 | // Returns true if the backing storage for the slow-case elements of |
| 1414 | // this object takes up nearly as much space as a fast-case backing |
| 1415 | // storage would. In that case the JSObject should have fast |
| 1416 | // elements. |
| 1417 | bool ShouldConvertToFastElements(); |
| 1418 | |
| 1419 | // Return the object's prototype (might be Heap::null_value()). |
| 1420 | inline Object* GetPrototype(); |
| 1421 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 1422 | // Set the object's prototype (only JSObject and null are allowed). |
| 1423 | Object* SetPrototype(Object* value, bool skip_hidden_prototypes); |
| 1424 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1425 | // Tells whether the index'th element is present. |
| 1426 | inline bool HasElement(uint32_t index); |
| 1427 | bool HasElementWithReceiver(JSObject* receiver, uint32_t index); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 1428 | |
| 1429 | // Tells whether the index'th element is present and how it is stored. |
| 1430 | enum LocalElementType { |
| 1431 | // There is no element with given index. |
| 1432 | UNDEFINED_ELEMENT, |
| 1433 | |
| 1434 | // Element with given index is handled by interceptor. |
| 1435 | INTERCEPTED_ELEMENT, |
| 1436 | |
| 1437 | // Element with given index is character in string. |
| 1438 | STRING_CHARACTER_ELEMENT, |
| 1439 | |
| 1440 | // Element with given index is stored in fast backing store. |
| 1441 | FAST_ELEMENT, |
| 1442 | |
| 1443 | // Element with given index is stored in slow backing store. |
| 1444 | DICTIONARY_ELEMENT |
| 1445 | }; |
| 1446 | |
| 1447 | LocalElementType HasLocalElement(uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1448 | |
| 1449 | bool HasElementWithInterceptor(JSObject* receiver, uint32_t index); |
| 1450 | bool HasElementPostInterceptor(JSObject* receiver, uint32_t index); |
| 1451 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1452 | MUST_USE_RESULT Object* SetFastElement(uint32_t index, Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1453 | |
| 1454 | // Set the index'th array element. |
| 1455 | // A Failure object is returned if GC is needed. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1456 | MUST_USE_RESULT Object* SetElement(uint32_t index, Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1457 | |
| 1458 | // Returns the index'th element. |
| 1459 | // The undefined object if index is out of bounds. |
| 1460 | Object* GetElementWithReceiver(JSObject* receiver, uint32_t index); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1461 | Object* GetElementWithInterceptor(JSObject* receiver, uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1462 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1463 | MUST_USE_RESULT Object* SetFastElementsCapacityAndLength(int capacity, |
| 1464 | int length); |
| 1465 | MUST_USE_RESULT Object* SetSlowElements(Object* length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1466 | |
| 1467 | // Lookup interceptors are used for handling properties controlled by host |
| 1468 | // objects. |
| 1469 | inline bool HasNamedInterceptor(); |
| 1470 | inline bool HasIndexedInterceptor(); |
| 1471 | |
| 1472 | // Support functions for v8 api (needed for correct interceptor behavior). |
| 1473 | bool HasRealNamedProperty(String* key); |
| 1474 | bool HasRealElementProperty(uint32_t index); |
| 1475 | bool HasRealNamedCallbackProperty(String* key); |
| 1476 | |
| 1477 | // Initializes the array to a certain length |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1478 | MUST_USE_RESULT Object* SetElementsLength(Object* length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Get the header size for a JSObject. Used to compute the index of |
| 1481 | // internal fields as well as the number of internal fields. |
| 1482 | inline int GetHeaderSize(); |
| 1483 | |
| 1484 | inline int GetInternalFieldCount(); |
| 1485 | inline Object* GetInternalField(int index); |
| 1486 | inline void SetInternalField(int index, Object* value); |
| 1487 | |
| 1488 | // Lookup a property. If found, the result is valid and has |
| 1489 | // detailed information. |
| 1490 | void LocalLookup(String* name, LookupResult* result); |
| 1491 | void Lookup(String* name, LookupResult* result); |
| 1492 | |
| 1493 | // The following lookup functions skip interceptors. |
| 1494 | void LocalLookupRealNamedProperty(String* name, LookupResult* result); |
| 1495 | void LookupRealNamedProperty(String* name, LookupResult* result); |
| 1496 | void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result); |
| 1497 | void LookupCallbackSetterInPrototypes(String* name, LookupResult* result); |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1498 | bool SetElementWithCallbackSetterInPrototypes(uint32_t index, Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1499 | void LookupCallback(String* name, LookupResult* result); |
| 1500 | |
| 1501 | // Returns the number of properties on this object filtering out properties |
| 1502 | // with the specified attributes (ignoring interceptors). |
| 1503 | int NumberOfLocalProperties(PropertyAttributes filter); |
| 1504 | // Returns the number of enumerable properties (ignoring interceptors). |
| 1505 | int NumberOfEnumProperties(); |
| 1506 | // Fill in details for properties into storage starting at the specified |
| 1507 | // index. |
| 1508 | void GetLocalPropertyNames(FixedArray* storage, int index); |
| 1509 | |
| 1510 | // Returns the number of properties on this object filtering out properties |
| 1511 | // with the specified attributes (ignoring interceptors). |
| 1512 | int NumberOfLocalElements(PropertyAttributes filter); |
| 1513 | // Returns the number of enumerable elements (ignoring interceptors). |
| 1514 | int NumberOfEnumElements(); |
| 1515 | // Returns the number of elements on this object filtering out elements |
| 1516 | // with the specified attributes (ignoring interceptors). |
| 1517 | int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter); |
| 1518 | // Count and fill in the enumerable elements into storage. |
| 1519 | // (storage->length() == NumberOfEnumElements()). |
| 1520 | // If storage is NULL, will count the elements without adding |
| 1521 | // them to any storage. |
| 1522 | // Returns the number of enumerable elements. |
| 1523 | int GetEnumElementKeys(FixedArray* storage); |
| 1524 | |
| 1525 | // Add a property to a fast-case object using a map transition to |
| 1526 | // new_map. |
| 1527 | Object* AddFastPropertyUsingMap(Map* new_map, |
| 1528 | String* name, |
| 1529 | Object* value); |
| 1530 | |
| 1531 | // Add a constant function property to a fast-case object. |
| 1532 | // This leaves a CONSTANT_TRANSITION in the old map, and |
| 1533 | // if it is called on a second object with this map, a |
| 1534 | // normal property is added instead, with a map transition. |
| 1535 | // This avoids the creation of many maps with the same constant |
| 1536 | // function, all orphaned. |
| 1537 | Object* AddConstantFunctionProperty(String* name, |
| 1538 | JSFunction* function, |
| 1539 | PropertyAttributes attributes); |
| 1540 | |
| 1541 | Object* ReplaceSlowProperty(String* name, |
| 1542 | Object* value, |
| 1543 | PropertyAttributes attributes); |
| 1544 | |
| 1545 | // Converts a descriptor of any other type to a real field, |
| 1546 | // backed by the properties array. Descriptors of visible |
| 1547 | // types, such as CONSTANT_FUNCTION, keep their enumeration order. |
| 1548 | // Converts the descriptor on the original object's map to a |
| 1549 | // map transition, and the the new field is on the object's new map. |
| 1550 | Object* ConvertDescriptorToFieldAndMapTransition( |
| 1551 | String* name, |
| 1552 | Object* new_value, |
| 1553 | PropertyAttributes attributes); |
| 1554 | |
| 1555 | // Converts a descriptor of any other type to a real field, |
| 1556 | // backed by the properties array. Descriptors of visible |
| 1557 | // types, such as CONSTANT_FUNCTION, keep their enumeration order. |
| 1558 | Object* ConvertDescriptorToField(String* name, |
| 1559 | Object* new_value, |
| 1560 | PropertyAttributes attributes); |
| 1561 | |
| 1562 | // Add a property to a fast-case object. |
| 1563 | Object* AddFastProperty(String* name, |
| 1564 | Object* value, |
| 1565 | PropertyAttributes attributes); |
| 1566 | |
| 1567 | // Add a property to a slow-case object. |
| 1568 | Object* AddSlowProperty(String* name, |
| 1569 | Object* value, |
| 1570 | PropertyAttributes attributes); |
| 1571 | |
| 1572 | // Add a property to an object. |
| 1573 | Object* AddProperty(String* name, |
| 1574 | Object* value, |
| 1575 | PropertyAttributes attributes); |
| 1576 | |
| 1577 | // Convert the object to use the canonical dictionary |
| 1578 | // representation. If the object is expected to have additional properties |
| 1579 | // added this number can be indicated to have the backing store allocated to |
| 1580 | // an initial capacity for holding these properties. |
| 1581 | Object* NormalizeProperties(PropertyNormalizationMode mode, |
| 1582 | int expected_additional_properties); |
| 1583 | Object* NormalizeElements(); |
| 1584 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1585 | Object* UpdateMapCodeCache(String* name, Code* code); |
| 1586 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1587 | // Transform slow named properties to fast variants. |
| 1588 | // Returns failure if allocation failed. |
| 1589 | Object* TransformToFastProperties(int unused_property_fields); |
| 1590 | |
| 1591 | // Access fast-case object properties at index. |
| 1592 | inline Object* FastPropertyAt(int index); |
| 1593 | inline Object* FastPropertyAtPut(int index, Object* value); |
| 1594 | |
| 1595 | // Access to in object properties. |
| 1596 | inline Object* InObjectPropertyAt(int index); |
| 1597 | inline Object* InObjectPropertyAtPut(int index, |
| 1598 | Object* value, |
| 1599 | WriteBarrierMode mode |
| 1600 | = UPDATE_WRITE_BARRIER); |
| 1601 | |
| 1602 | // initializes the body after properties slot, properties slot is |
| 1603 | // initialized by set_properties |
| 1604 | // Note: this call does not update write barrier, it is caller's |
| 1605 | // reponsibility to ensure that *v* can be collected without WB here. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 1606 | inline void InitializeBody(int object_size, Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1607 | |
| 1608 | // Check whether this object references another object |
| 1609 | bool ReferencesObject(Object* obj); |
| 1610 | |
| 1611 | // Casting. |
| 1612 | static inline JSObject* cast(Object* obj); |
| 1613 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1614 | // Disalow further properties to be added to the object. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1615 | MUST_USE_RESULT Object* PreventExtensions(); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1616 | |
| 1617 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1618 | // Dispatched behavior. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1619 | void JSObjectShortPrint(StringStream* accumulator); |
| 1620 | #ifdef DEBUG |
| 1621 | void JSObjectPrint(); |
| 1622 | void JSObjectVerify(); |
| 1623 | void PrintProperties(); |
| 1624 | void PrintElements(); |
| 1625 | |
| 1626 | // Structure for collecting spill information about JSObjects. |
| 1627 | class SpillInformation { |
| 1628 | public: |
| 1629 | void Clear(); |
| 1630 | void Print(); |
| 1631 | int number_of_objects_; |
| 1632 | int number_of_objects_with_fast_properties_; |
| 1633 | int number_of_objects_with_fast_elements_; |
| 1634 | int number_of_fast_used_fields_; |
| 1635 | int number_of_fast_unused_fields_; |
| 1636 | int number_of_slow_used_properties_; |
| 1637 | int number_of_slow_unused_properties_; |
| 1638 | int number_of_fast_used_elements_; |
| 1639 | int number_of_fast_unused_elements_; |
| 1640 | int number_of_slow_used_elements_; |
| 1641 | int number_of_slow_unused_elements_; |
| 1642 | }; |
| 1643 | |
| 1644 | void IncrementSpillStatistics(SpillInformation* info); |
| 1645 | #endif |
| 1646 | Object* SlowReverseLookup(Object* value); |
| 1647 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 1648 | // Maximal number of fast properties for the JSObject. Used to |
| 1649 | // restrict the number of map transitions to avoid an explosion in |
| 1650 | // the number of maps for objects used as dictionaries. |
| 1651 | inline int MaxFastProperties(); |
| 1652 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1653 | // Maximal number of elements (numbered 0 .. kMaxElementCount - 1). |
| 1654 | // Also maximal value of JSArray's length property. |
| 1655 | static const uint32_t kMaxElementCount = 0xffffffffu; |
| 1656 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1657 | static const uint32_t kMaxGap = 1024; |
| 1658 | static const int kMaxFastElementsLength = 5000; |
| 1659 | static const int kInitialMaxFastElementArray = 100000; |
| 1660 | static const int kMaxFastProperties = 8; |
| 1661 | static const int kMaxInstanceSize = 255 * kPointerSize; |
| 1662 | // When extending the backing storage for property values, we increase |
| 1663 | // its size by more than the 1 entry necessary, so sequentially adding fields |
| 1664 | // to the same object requires fewer allocations and copies. |
| 1665 | static const int kFieldsAdded = 3; |
| 1666 | |
| 1667 | // Layout description. |
| 1668 | static const int kPropertiesOffset = HeapObject::kHeaderSize; |
| 1669 | static const int kElementsOffset = kPropertiesOffset + kPointerSize; |
| 1670 | static const int kHeaderSize = kElementsOffset + kPointerSize; |
| 1671 | |
| 1672 | STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize); |
| 1673 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1674 | class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> { |
| 1675 | public: |
| 1676 | static inline int SizeOf(Map* map, HeapObject* object); |
| 1677 | }; |
| 1678 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1679 | private: |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1680 | Object* GetElementWithCallback(Object* receiver, |
| 1681 | Object* structure, |
| 1682 | uint32_t index, |
| 1683 | Object* holder); |
| 1684 | Object* SetElementWithCallback(Object* structure, |
| 1685 | uint32_t index, |
| 1686 | Object* value, |
| 1687 | JSObject* holder); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1688 | MUST_USE_RESULT Object* SetElementWithInterceptor(uint32_t index, |
| 1689 | Object* value); |
| 1690 | MUST_USE_RESULT Object* SetElementWithoutInterceptor(uint32_t index, |
| 1691 | Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1692 | |
| 1693 | Object* GetElementPostInterceptor(JSObject* receiver, uint32_t index); |
| 1694 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1695 | MUST_USE_RESULT Object* DeletePropertyPostInterceptor(String* name, |
| 1696 | DeleteMode mode); |
| 1697 | MUST_USE_RESULT Object* DeletePropertyWithInterceptor(String* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1698 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1699 | MUST_USE_RESULT Object* DeleteElementPostInterceptor(uint32_t index, |
| 1700 | DeleteMode mode); |
| 1701 | MUST_USE_RESULT Object* DeleteElementWithInterceptor(uint32_t index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1702 | |
| 1703 | PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver, |
| 1704 | String* name, |
| 1705 | bool continue_search); |
| 1706 | PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver, |
| 1707 | String* name, |
| 1708 | bool continue_search); |
| 1709 | PropertyAttributes GetPropertyAttributeWithFailedAccessCheck( |
| 1710 | Object* receiver, |
| 1711 | LookupResult* result, |
| 1712 | String* name, |
| 1713 | bool continue_search); |
| 1714 | PropertyAttributes GetPropertyAttribute(JSObject* receiver, |
| 1715 | LookupResult* result, |
| 1716 | String* name, |
| 1717 | bool continue_search); |
| 1718 | |
| 1719 | // Returns true if most of the elements backing storage is used. |
| 1720 | bool HasDenseElements(); |
| 1721 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1722 | bool CanSetCallback(String* name); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1723 | MUST_USE_RESULT Object* SetElementCallback(uint32_t index, |
| 1724 | Object* structure, |
| 1725 | PropertyAttributes attributes); |
| 1726 | MUST_USE_RESULT Object* SetPropertyCallback(String* name, |
| 1727 | Object* structure, |
| 1728 | PropertyAttributes attributes); |
| 1729 | MUST_USE_RESULT Object* DefineGetterSetter(String* name, |
| 1730 | PropertyAttributes attributes); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1731 | |
| 1732 | void LookupInDescriptor(String* name, LookupResult* result); |
| 1733 | |
| 1734 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject); |
| 1735 | }; |
| 1736 | |
| 1737 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1738 | // FixedArray describes fixed-sized arrays with element type Object*. |
| 1739 | class FixedArray: public HeapObject { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1740 | public: |
| 1741 | // [length]: length of the array. |
| 1742 | inline int length(); |
| 1743 | inline void set_length(int value); |
| 1744 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1745 | // Setter and getter for elements. |
| 1746 | inline Object* get(int index); |
| 1747 | // Setter that uses write barrier. |
| 1748 | inline void set(int index, Object* value); |
| 1749 | |
| 1750 | // Setter that doesn't need write barrier). |
| 1751 | inline void set(int index, Smi* value); |
| 1752 | // Setter with explicit barrier mode. |
| 1753 | inline void set(int index, Object* value, WriteBarrierMode mode); |
| 1754 | |
| 1755 | // Setters for frequently used oddballs located in old space. |
| 1756 | inline void set_undefined(int index); |
| 1757 | inline void set_null(int index); |
| 1758 | inline void set_the_hole(int index); |
| 1759 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1760 | // Setters with less debug checks for the GC to use. |
| 1761 | inline void set_unchecked(int index, Smi* value); |
| 1762 | inline void set_null_unchecked(int index); |
| 1763 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1764 | // Gives access to raw memory which stores the array's data. |
| 1765 | inline Object** data_start(); |
| 1766 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1767 | // Copy operations. |
| 1768 | inline Object* Copy(); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1769 | MUST_USE_RESULT Object* CopySize(int new_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1770 | |
| 1771 | // Add the elements of a JSArray to this FixedArray. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1772 | MUST_USE_RESULT Object* AddKeysFromJSArray(JSArray* array); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1773 | |
| 1774 | // Compute the union of this and other. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1775 | MUST_USE_RESULT Object* UnionOfKeys(FixedArray* other); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1776 | |
| 1777 | // Copy a sub array from the receiver to dest. |
| 1778 | void CopyTo(int pos, FixedArray* dest, int dest_pos, int len); |
| 1779 | |
| 1780 | // Garbage collection support. |
| 1781 | static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; } |
| 1782 | |
| 1783 | // Code Generation support. |
| 1784 | static int OffsetOfElementAt(int index) { return SizeFor(index); } |
| 1785 | |
| 1786 | // Casting. |
| 1787 | static inline FixedArray* cast(Object* obj); |
| 1788 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 1789 | // Layout description. |
| 1790 | // Length is smi tagged when it is stored. |
| 1791 | static const int kLengthOffset = HeapObject::kHeaderSize; |
| 1792 | static const int kHeaderSize = kLengthOffset + kPointerSize; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1793 | |
| 1794 | // Maximal allowed size, in bytes, of a single FixedArray. |
| 1795 | // Prevents overflowing size computations, as well as extreme memory |
| 1796 | // consumption. |
| 1797 | static const int kMaxSize = 512 * MB; |
| 1798 | // Maximally allowed length of a FixedArray. |
| 1799 | static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1800 | |
| 1801 | // Dispatched behavior. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1802 | #ifdef DEBUG |
| 1803 | void FixedArrayPrint(); |
| 1804 | void FixedArrayVerify(); |
| 1805 | // Checks if two FixedArrays have identical contents. |
| 1806 | bool IsEqualTo(FixedArray* other); |
| 1807 | #endif |
| 1808 | |
| 1809 | // Swap two elements in a pair of arrays. If this array and the |
| 1810 | // numbers array are the same object, the elements are only swapped |
| 1811 | // once. |
| 1812 | void SwapPairs(FixedArray* numbers, int i, int j); |
| 1813 | |
| 1814 | // Sort prefix of this array and the numbers array as pairs wrt. the |
| 1815 | // numbers. If the numbers array and the this array are the same |
| 1816 | // object, the prefix of this array is sorted. |
| 1817 | void SortPairs(FixedArray* numbers, uint32_t len); |
| 1818 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1819 | class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> { |
| 1820 | public: |
| 1821 | static inline int SizeOf(Map* map, HeapObject* object) { |
| 1822 | return SizeFor(reinterpret_cast<FixedArray*>(object)->length()); |
| 1823 | } |
| 1824 | }; |
| 1825 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1826 | protected: |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 1827 | // Set operation on FixedArray without using write barriers. Can |
| 1828 | // only be used for storing old space objects or smis. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1829 | static inline void fast_set(FixedArray* array, int index, Object* value); |
| 1830 | |
| 1831 | private: |
| 1832 | DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray); |
| 1833 | }; |
| 1834 | |
| 1835 | |
| 1836 | // DescriptorArrays are fixed arrays used to hold instance descriptors. |
| 1837 | // The format of the these objects is: |
| 1838 | // [0]: point to a fixed array with (value, detail) pairs. |
| 1839 | // [1]: next enumeration index (Smi), or pointer to small fixed array: |
| 1840 | // [0]: next enumeration index (Smi) |
| 1841 | // [1]: pointer to fixed array with enum cache |
| 1842 | // [2]: first key |
| 1843 | // [length() - 1]: last key |
| 1844 | // |
| 1845 | class DescriptorArray: public FixedArray { |
| 1846 | public: |
| 1847 | // Is this the singleton empty_descriptor_array? |
| 1848 | inline bool IsEmpty(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1849 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1850 | // Returns the number of descriptors in the array. |
| 1851 | int number_of_descriptors() { |
| 1852 | return IsEmpty() ? 0 : length() - kFirstIndex; |
| 1853 | } |
| 1854 | |
| 1855 | int NextEnumerationIndex() { |
| 1856 | if (IsEmpty()) return PropertyDetails::kInitialIndex; |
| 1857 | Object* obj = get(kEnumerationIndexIndex); |
| 1858 | if (obj->IsSmi()) { |
| 1859 | return Smi::cast(obj)->value(); |
| 1860 | } else { |
| 1861 | Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex); |
| 1862 | return Smi::cast(index)->value(); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | // Set next enumeration index and flush any enum cache. |
| 1867 | void SetNextEnumerationIndex(int value) { |
| 1868 | if (!IsEmpty()) { |
| 1869 | fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value)); |
| 1870 | } |
| 1871 | } |
| 1872 | bool HasEnumCache() { |
| 1873 | return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi(); |
| 1874 | } |
| 1875 | |
| 1876 | Object* GetEnumCache() { |
| 1877 | ASSERT(HasEnumCache()); |
| 1878 | FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex)); |
| 1879 | return bridge->get(kEnumCacheBridgeCacheIndex); |
| 1880 | } |
| 1881 | |
| 1882 | // Initialize or change the enum cache, |
| 1883 | // using the supplied storage for the small "bridge". |
| 1884 | void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache); |
| 1885 | |
| 1886 | // Accessors for fetching instance descriptor at descriptor number. |
| 1887 | inline String* GetKey(int descriptor_number); |
| 1888 | inline Object* GetValue(int descriptor_number); |
| 1889 | inline Smi* GetDetails(int descriptor_number); |
| 1890 | inline PropertyType GetType(int descriptor_number); |
| 1891 | inline int GetFieldIndex(int descriptor_number); |
| 1892 | inline JSFunction* GetConstantFunction(int descriptor_number); |
| 1893 | inline Object* GetCallbacksObject(int descriptor_number); |
| 1894 | inline AccessorDescriptor* GetCallbacks(int descriptor_number); |
| 1895 | inline bool IsProperty(int descriptor_number); |
| 1896 | inline bool IsTransition(int descriptor_number); |
| 1897 | inline bool IsNullDescriptor(int descriptor_number); |
| 1898 | inline bool IsDontEnum(int descriptor_number); |
| 1899 | |
| 1900 | // Accessor for complete descriptor. |
| 1901 | inline void Get(int descriptor_number, Descriptor* desc); |
| 1902 | inline void Set(int descriptor_number, Descriptor* desc); |
| 1903 | |
| 1904 | // Transfer complete descriptor from another descriptor array to |
| 1905 | // this one. |
| 1906 | inline void CopyFrom(int index, DescriptorArray* src, int src_index); |
| 1907 | |
| 1908 | // Copy the descriptor array, insert a new descriptor and optionally |
| 1909 | // remove map transitions. If the descriptor is already present, it is |
| 1910 | // replaced. If a replaced descriptor is a real property (not a transition |
| 1911 | // or null), its enumeration index is kept as is. |
| 1912 | // If adding a real property, map transitions must be removed. If adding |
| 1913 | // a transition, they must not be removed. All null descriptors are removed. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1914 | MUST_USE_RESULT Object* CopyInsert(Descriptor* descriptor, |
| 1915 | TransitionFlag transition_flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1916 | |
| 1917 | // Remove all transitions. Return a copy of the array with all transitions |
| 1918 | // removed, or a Failure object if the new array could not be allocated. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1919 | MUST_USE_RESULT Object* RemoveTransitions(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1920 | |
| 1921 | // Sort the instance descriptors by the hash codes of their keys. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 1922 | // Does not check for duplicates. |
| 1923 | void SortUnchecked(); |
| 1924 | |
| 1925 | // Sort the instance descriptors by the hash codes of their keys. |
| 1926 | // Checks the result for duplicates. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1927 | void Sort(); |
| 1928 | |
| 1929 | // Search the instance descriptors for given name. |
| 1930 | inline int Search(String* name); |
| 1931 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 1932 | // As the above, but uses DescriptorLookupCache and updates it when |
| 1933 | // necessary. |
| 1934 | inline int SearchWithCache(String* name); |
| 1935 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1936 | // Tells whether the name is present int the array. |
| 1937 | bool Contains(String* name) { return kNotFound != Search(name); } |
| 1938 | |
| 1939 | // Perform a binary search in the instance descriptors represented |
| 1940 | // by this fixed array. low and high are descriptor indices. If there |
| 1941 | // are three instance descriptors in this array it should be called |
| 1942 | // with low=0 and high=2. |
| 1943 | int BinarySearch(String* name, int low, int high); |
| 1944 | |
| 1945 | // Perform a linear search in the instance descriptors represented |
| 1946 | // by this fixed array. len is the number of descriptor indices that are |
| 1947 | // valid. Does not require the descriptors to be sorted. |
| 1948 | int LinearSearch(String* name, int len); |
| 1949 | |
| 1950 | // Allocates a DescriptorArray, but returns the singleton |
| 1951 | // empty descriptor array object if number_of_descriptors is 0. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 1952 | MUST_USE_RESULT static Object* Allocate(int number_of_descriptors); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1953 | |
| 1954 | // Casting. |
| 1955 | static inline DescriptorArray* cast(Object* obj); |
| 1956 | |
| 1957 | // Constant for denoting key was not found. |
| 1958 | static const int kNotFound = -1; |
| 1959 | |
| 1960 | static const int kContentArrayIndex = 0; |
| 1961 | static const int kEnumerationIndexIndex = 1; |
| 1962 | static const int kFirstIndex = 2; |
| 1963 | |
| 1964 | // The length of the "bridge" to the enum cache. |
| 1965 | static const int kEnumCacheBridgeLength = 2; |
| 1966 | static const int kEnumCacheBridgeEnumIndex = 0; |
| 1967 | static const int kEnumCacheBridgeCacheIndex = 1; |
| 1968 | |
| 1969 | // Layout description. |
| 1970 | static const int kContentArrayOffset = FixedArray::kHeaderSize; |
| 1971 | static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize; |
| 1972 | static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize; |
| 1973 | |
| 1974 | // Layout description for the bridge array. |
| 1975 | static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize; |
| 1976 | static const int kEnumCacheBridgeCacheOffset = |
| 1977 | kEnumCacheBridgeEnumOffset + kPointerSize; |
| 1978 | |
| 1979 | #ifdef DEBUG |
| 1980 | // Print all the descriptors. |
| 1981 | void PrintDescriptors(); |
| 1982 | |
| 1983 | // Is the descriptor array sorted and without duplicates? |
| 1984 | bool IsSortedNoDuplicates(); |
| 1985 | |
| 1986 | // Are two DescriptorArrays equal? |
| 1987 | bool IsEqualTo(DescriptorArray* other); |
| 1988 | #endif |
| 1989 | |
| 1990 | // The maximum number of descriptors we want in a descriptor array (should |
| 1991 | // fit in a page). |
| 1992 | static const int kMaxNumberOfDescriptors = 1024 + 512; |
| 1993 | |
| 1994 | private: |
| 1995 | // Conversion from descriptor number to array indices. |
| 1996 | static int ToKeyIndex(int descriptor_number) { |
| 1997 | return descriptor_number+kFirstIndex; |
| 1998 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1999 | |
| 2000 | static int ToDetailsIndex(int descriptor_number) { |
| 2001 | return (descriptor_number << 1) + 1; |
| 2002 | } |
| 2003 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2004 | static int ToValueIndex(int descriptor_number) { |
| 2005 | return descriptor_number << 1; |
| 2006 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2007 | |
| 2008 | bool is_null_descriptor(int descriptor_number) { |
| 2009 | return PropertyDetails(GetDetails(descriptor_number)).type() == |
| 2010 | NULL_DESCRIPTOR; |
| 2011 | } |
| 2012 | // Swap operation on FixedArray without using write barriers. |
| 2013 | static inline void fast_swap(FixedArray* array, int first, int second); |
| 2014 | |
| 2015 | // Swap descriptor first and second. |
| 2016 | inline void Swap(int first, int second); |
| 2017 | |
| 2018 | FixedArray* GetContentArray() { |
| 2019 | return FixedArray::cast(get(kContentArrayIndex)); |
| 2020 | } |
| 2021 | DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray); |
| 2022 | }; |
| 2023 | |
| 2024 | |
| 2025 | // HashTable is a subclass of FixedArray that implements a hash table |
| 2026 | // that uses open addressing and quadratic probing. |
| 2027 | // |
| 2028 | // In order for the quadratic probing to work, elements that have not |
| 2029 | // yet been used and elements that have been deleted are |
| 2030 | // distinguished. Probing continues when deleted elements are |
| 2031 | // encountered and stops when unused elements are encountered. |
| 2032 | // |
| 2033 | // - Elements with key == undefined have not been used yet. |
| 2034 | // - Elements with key == null have been deleted. |
| 2035 | // |
| 2036 | // The hash table class is parameterized with a Shape and a Key. |
| 2037 | // Shape must be a class with the following interface: |
| 2038 | // class ExampleShape { |
| 2039 | // public: |
| 2040 | // // Tells whether key matches other. |
| 2041 | // static bool IsMatch(Key key, Object* other); |
| 2042 | // // Returns the hash value for key. |
| 2043 | // static uint32_t Hash(Key key); |
| 2044 | // // Returns the hash value for object. |
| 2045 | // static uint32_t HashForObject(Key key, Object* object); |
| 2046 | // // Convert key to an object. |
| 2047 | // static inline Object* AsObject(Key key); |
| 2048 | // // The prefix size indicates number of elements in the beginning |
| 2049 | // // of the backing storage. |
| 2050 | // static const int kPrefixSize = ..; |
| 2051 | // // The Element size indicates number of elements per entry. |
| 2052 | // static const int kEntrySize = ..; |
| 2053 | // }; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2054 | // The prefix size indicates an amount of memory in the |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2055 | // beginning of the backing storage that can be used for non-element |
| 2056 | // information by subclasses. |
| 2057 | |
| 2058 | template<typename Shape, typename Key> |
| 2059 | class HashTable: public FixedArray { |
| 2060 | public: |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2061 | // Returns the number of elements in the hash table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2062 | int NumberOfElements() { |
| 2063 | return Smi::cast(get(kNumberOfElementsIndex))->value(); |
| 2064 | } |
| 2065 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2066 | // Returns the number of deleted elements in the hash table. |
| 2067 | int NumberOfDeletedElements() { |
| 2068 | return Smi::cast(get(kNumberOfDeletedElementsIndex))->value(); |
| 2069 | } |
| 2070 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2071 | // Returns the capacity of the hash table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2072 | int Capacity() { |
| 2073 | return Smi::cast(get(kCapacityIndex))->value(); |
| 2074 | } |
| 2075 | |
| 2076 | // ElementAdded should be called whenever an element is added to a |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2077 | // hash table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2078 | void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); } |
| 2079 | |
| 2080 | // ElementRemoved should be called whenever an element is removed from |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2081 | // a hash table. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2082 | void ElementRemoved() { |
| 2083 | SetNumberOfElements(NumberOfElements() - 1); |
| 2084 | SetNumberOfDeletedElements(NumberOfDeletedElements() + 1); |
| 2085 | } |
| 2086 | void ElementsRemoved(int n) { |
| 2087 | SetNumberOfElements(NumberOfElements() - n); |
| 2088 | SetNumberOfDeletedElements(NumberOfDeletedElements() + n); |
| 2089 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2090 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2091 | // Returns a new HashTable object. Might return Failure. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2092 | MUST_USE_RESULT static Object* Allocate( |
| 2093 | int at_least_space_for, |
| 2094 | PretenureFlag pretenure = NOT_TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2095 | |
| 2096 | // Returns the key at entry. |
| 2097 | Object* KeyAt(int entry) { return get(EntryToIndex(entry)); } |
| 2098 | |
| 2099 | // Tells whether k is a real key. Null and undefined are not allowed |
| 2100 | // as keys and can be used to indicate missing or deleted elements. |
| 2101 | bool IsKey(Object* k) { |
| 2102 | return !k->IsNull() && !k->IsUndefined(); |
| 2103 | } |
| 2104 | |
| 2105 | // Garbage collection support. |
| 2106 | void IteratePrefix(ObjectVisitor* visitor); |
| 2107 | void IterateElements(ObjectVisitor* visitor); |
| 2108 | |
| 2109 | // Casting. |
| 2110 | static inline HashTable* cast(Object* obj); |
| 2111 | |
| 2112 | // Compute the probe offset (quadratic probing). |
| 2113 | INLINE(static uint32_t GetProbeOffset(uint32_t n)) { |
| 2114 | return (n + n * n) >> 1; |
| 2115 | } |
| 2116 | |
| 2117 | static const int kNumberOfElementsIndex = 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2118 | static const int kNumberOfDeletedElementsIndex = 1; |
| 2119 | static const int kCapacityIndex = 2; |
| 2120 | static const int kPrefixStartIndex = 3; |
| 2121 | static const int kElementsStartIndex = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2122 | kPrefixStartIndex + Shape::kPrefixSize; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2123 | static const int kEntrySize = Shape::kEntrySize; |
| 2124 | static const int kElementsStartOffset = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2125 | kHeaderSize + kElementsStartIndex * kPointerSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2126 | static const int kCapacityOffset = |
| 2127 | kHeaderSize + kCapacityIndex * kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2128 | |
| 2129 | // Constant used for denoting a absent entry. |
| 2130 | static const int kNotFound = -1; |
| 2131 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2132 | // Maximal capacity of HashTable. Based on maximal length of underlying |
| 2133 | // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex |
| 2134 | // cannot overflow. |
| 2135 | static const int kMaxCapacity = |
| 2136 | (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize; |
| 2137 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2138 | // Find entry for key otherwise return kNotFound. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2139 | int FindEntry(Key key); |
| 2140 | |
| 2141 | protected: |
| 2142 | |
| 2143 | // Find the entry at which to insert element with the given key that |
| 2144 | // has the given hash value. |
| 2145 | uint32_t FindInsertionEntry(uint32_t hash); |
| 2146 | |
| 2147 | // Returns the index for an entry (of the key) |
| 2148 | static inline int EntryToIndex(int entry) { |
| 2149 | return (entry * kEntrySize) + kElementsStartIndex; |
| 2150 | } |
| 2151 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2152 | // Update the number of elements in the hash table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2153 | void SetNumberOfElements(int nof) { |
| 2154 | fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof)); |
| 2155 | } |
| 2156 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2157 | // Update the number of deleted elements in the hash table. |
| 2158 | void SetNumberOfDeletedElements(int nod) { |
| 2159 | fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod)); |
| 2160 | } |
| 2161 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2162 | // Sets the capacity of the hash table. |
| 2163 | void SetCapacity(int capacity) { |
| 2164 | // To scale a computed hash code to fit within the hash table, we |
| 2165 | // use bit-wise AND with a mask, so the capacity must be positive |
| 2166 | // and non-zero. |
| 2167 | ASSERT(capacity > 0); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2168 | ASSERT(capacity <= kMaxCapacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2169 | fast_set(this, kCapacityIndex, Smi::FromInt(capacity)); |
| 2170 | } |
| 2171 | |
| 2172 | |
| 2173 | // Returns probe entry. |
| 2174 | static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) { |
| 2175 | ASSERT(IsPowerOf2(size)); |
| 2176 | return (hash + GetProbeOffset(number)) & (size - 1); |
| 2177 | } |
| 2178 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2179 | static uint32_t FirstProbe(uint32_t hash, uint32_t size) { |
| 2180 | return hash & (size - 1); |
| 2181 | } |
| 2182 | |
| 2183 | static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) { |
| 2184 | return (last + number) & (size - 1); |
| 2185 | } |
| 2186 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2187 | // Ensure enough space for n additional elements. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2188 | MUST_USE_RESULT Object* EnsureCapacity(int n, Key key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2189 | }; |
| 2190 | |
| 2191 | |
| 2192 | |
| 2193 | // HashTableKey is an abstract superclass for virtual key behavior. |
| 2194 | class HashTableKey { |
| 2195 | public: |
| 2196 | // Returns whether the other object matches this key. |
| 2197 | virtual bool IsMatch(Object* other) = 0; |
| 2198 | // Returns the hash value for this key. |
| 2199 | virtual uint32_t Hash() = 0; |
| 2200 | // Returns the hash value for object. |
| 2201 | virtual uint32_t HashForObject(Object* key) = 0; |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2202 | // Returns the key object for storing into the hash table. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2203 | // If allocations fails a failure object is returned. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2204 | MUST_USE_RESULT virtual Object* AsObject() = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2205 | // Required. |
| 2206 | virtual ~HashTableKey() {} |
| 2207 | }; |
| 2208 | |
| 2209 | class SymbolTableShape { |
| 2210 | public: |
| 2211 | static bool IsMatch(HashTableKey* key, Object* value) { |
| 2212 | return key->IsMatch(value); |
| 2213 | } |
| 2214 | static uint32_t Hash(HashTableKey* key) { |
| 2215 | return key->Hash(); |
| 2216 | } |
| 2217 | static uint32_t HashForObject(HashTableKey* key, Object* object) { |
| 2218 | return key->HashForObject(object); |
| 2219 | } |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2220 | MUST_USE_RESULT static Object* AsObject(HashTableKey* key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2221 | return key->AsObject(); |
| 2222 | } |
| 2223 | |
| 2224 | static const int kPrefixSize = 0; |
| 2225 | static const int kEntrySize = 1; |
| 2226 | }; |
| 2227 | |
| 2228 | // SymbolTable. |
| 2229 | // |
| 2230 | // No special elements in the prefix and the element size is 1 |
| 2231 | // because only the symbol itself (the key) needs to be stored. |
| 2232 | class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> { |
| 2233 | public: |
| 2234 | // Find symbol in the symbol table. If it is not there yet, it is |
| 2235 | // added. The return value is the symbol table which might have |
| 2236 | // been enlarged. If the return value is not a failure, the symbol |
| 2237 | // pointer *s is set to the symbol found. |
| 2238 | Object* LookupSymbol(Vector<const char> str, Object** s); |
| 2239 | Object* LookupString(String* key, Object** s); |
| 2240 | |
| 2241 | // Looks up a symbol that is equal to the given string and returns |
| 2242 | // true if it is found, assigning the symbol to the given output |
| 2243 | // parameter. |
| 2244 | bool LookupSymbolIfExists(String* str, String** symbol); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2245 | bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2246 | |
| 2247 | // Casting. |
| 2248 | static inline SymbolTable* cast(Object* obj); |
| 2249 | |
| 2250 | private: |
| 2251 | Object* LookupKey(HashTableKey* key, Object** s); |
| 2252 | |
| 2253 | DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable); |
| 2254 | }; |
| 2255 | |
| 2256 | |
| 2257 | class MapCacheShape { |
| 2258 | public: |
| 2259 | static bool IsMatch(HashTableKey* key, Object* value) { |
| 2260 | return key->IsMatch(value); |
| 2261 | } |
| 2262 | static uint32_t Hash(HashTableKey* key) { |
| 2263 | return key->Hash(); |
| 2264 | } |
| 2265 | |
| 2266 | static uint32_t HashForObject(HashTableKey* key, Object* object) { |
| 2267 | return key->HashForObject(object); |
| 2268 | } |
| 2269 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2270 | MUST_USE_RESULT static Object* AsObject(HashTableKey* key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2271 | return key->AsObject(); |
| 2272 | } |
| 2273 | |
| 2274 | static const int kPrefixSize = 0; |
| 2275 | static const int kEntrySize = 2; |
| 2276 | }; |
| 2277 | |
| 2278 | |
| 2279 | // MapCache. |
| 2280 | // |
| 2281 | // Maps keys that are a fixed array of symbols to a map. |
| 2282 | // Used for canonicalize maps for object literals. |
| 2283 | class MapCache: public HashTable<MapCacheShape, HashTableKey*> { |
| 2284 | public: |
| 2285 | // Find cached value for a string key, otherwise return null. |
| 2286 | Object* Lookup(FixedArray* key); |
| 2287 | Object* Put(FixedArray* key, Map* value); |
| 2288 | static inline MapCache* cast(Object* obj); |
| 2289 | |
| 2290 | private: |
| 2291 | DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache); |
| 2292 | }; |
| 2293 | |
| 2294 | |
| 2295 | template <typename Shape, typename Key> |
| 2296 | class Dictionary: public HashTable<Shape, Key> { |
| 2297 | public: |
| 2298 | |
| 2299 | static inline Dictionary<Shape, Key>* cast(Object* obj) { |
| 2300 | return reinterpret_cast<Dictionary<Shape, Key>*>(obj); |
| 2301 | } |
| 2302 | |
| 2303 | // Returns the value at entry. |
| 2304 | Object* ValueAt(int entry) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2305 | return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | // Set the value for entry. |
| 2309 | void ValueAtPut(int entry, Object* value) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 2310 | // Check that this value can actually be written. |
| 2311 | PropertyDetails details = DetailsAt(entry); |
| 2312 | // If a value has not been initilized we allow writing to it even if |
| 2313 | // it is read only (a declared const that has not been initialized). |
| 2314 | if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2315 | this->set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | // Returns the property details for the property at entry. |
| 2319 | PropertyDetails DetailsAt(int entry) { |
| 2320 | ASSERT(entry >= 0); // Not found is -1, which is not caught by get(). |
| 2321 | return PropertyDetails( |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2322 | Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | // Set the details for entry. |
| 2326 | void DetailsAtPut(int entry, PropertyDetails value) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2327 | this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | // Sorting support |
| 2331 | void CopyValuesTo(FixedArray* elements); |
| 2332 | |
| 2333 | // Delete a property from the dictionary. |
| 2334 | Object* DeleteProperty(int entry, JSObject::DeleteMode mode); |
| 2335 | |
| 2336 | // Returns the number of elements in the dictionary filtering out properties |
| 2337 | // with the specified attributes. |
| 2338 | int NumberOfElementsFilterAttributes(PropertyAttributes filter); |
| 2339 | |
| 2340 | // Returns the number of enumerable elements in the dictionary. |
| 2341 | int NumberOfEnumElements(); |
| 2342 | |
| 2343 | // Copies keys to preallocated fixed array. |
| 2344 | void CopyKeysTo(FixedArray* storage, PropertyAttributes filter); |
| 2345 | // Fill in details for properties into storage. |
| 2346 | void CopyKeysTo(FixedArray* storage); |
| 2347 | |
| 2348 | // Accessors for next enumeration index. |
| 2349 | void SetNextEnumerationIndex(int index) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2350 | this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| 2353 | int NextEnumerationIndex() { |
| 2354 | return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value(); |
| 2355 | } |
| 2356 | |
| 2357 | // Returns a new array for dictionary usage. Might return Failure. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2358 | MUST_USE_RESULT static Object* Allocate(int at_least_space_for); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2359 | |
| 2360 | // Ensure enough space for n additional elements. |
| 2361 | Object* EnsureCapacity(int n, Key key); |
| 2362 | |
| 2363 | #ifdef DEBUG |
| 2364 | void Print(); |
| 2365 | #endif |
| 2366 | // Returns the key (slow). |
| 2367 | Object* SlowReverseLookup(Object* value); |
| 2368 | |
| 2369 | // Sets the entry to (key, value) pair. |
| 2370 | inline void SetEntry(int entry, |
| 2371 | Object* key, |
| 2372 | Object* value, |
| 2373 | PropertyDetails details); |
| 2374 | |
| 2375 | Object* Add(Key key, Object* value, PropertyDetails details); |
| 2376 | |
| 2377 | protected: |
| 2378 | // Generic at put operation. |
| 2379 | Object* AtPut(Key key, Object* value); |
| 2380 | |
| 2381 | // Add entry to dictionary. |
| 2382 | Object* AddEntry(Key key, |
| 2383 | Object* value, |
| 2384 | PropertyDetails details, |
| 2385 | uint32_t hash); |
| 2386 | |
| 2387 | // Generate new enumeration indices to avoid enumeration index overflow. |
| 2388 | Object* GenerateNewEnumerationIndices(); |
| 2389 | static const int kMaxNumberKeyIndex = |
| 2390 | HashTable<Shape, Key>::kPrefixStartIndex; |
| 2391 | static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1; |
| 2392 | }; |
| 2393 | |
| 2394 | |
| 2395 | class StringDictionaryShape { |
| 2396 | public: |
| 2397 | static inline bool IsMatch(String* key, Object* other); |
| 2398 | static inline uint32_t Hash(String* key); |
| 2399 | static inline uint32_t HashForObject(String* key, Object* object); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2400 | MUST_USE_RESULT static inline Object* AsObject(String* key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2401 | static const int kPrefixSize = 2; |
| 2402 | static const int kEntrySize = 3; |
| 2403 | static const bool kIsEnumerable = true; |
| 2404 | }; |
| 2405 | |
| 2406 | |
| 2407 | class StringDictionary: public Dictionary<StringDictionaryShape, String*> { |
| 2408 | public: |
| 2409 | static inline StringDictionary* cast(Object* obj) { |
| 2410 | ASSERT(obj->IsDictionary()); |
| 2411 | return reinterpret_cast<StringDictionary*>(obj); |
| 2412 | } |
| 2413 | |
| 2414 | // Copies enumerable keys to preallocated fixed array. |
| 2415 | void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array); |
| 2416 | |
| 2417 | // For transforming properties of a JSObject. |
| 2418 | Object* TransformPropertiesToFastFor(JSObject* obj, |
| 2419 | int unused_property_fields); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2420 | |
| 2421 | // Find entry for key otherwise return kNotFound. Optimzed version of |
| 2422 | // HashTable::FindEntry. |
| 2423 | int FindEntry(String* key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2424 | }; |
| 2425 | |
| 2426 | |
| 2427 | class NumberDictionaryShape { |
| 2428 | public: |
| 2429 | static inline bool IsMatch(uint32_t key, Object* other); |
| 2430 | static inline uint32_t Hash(uint32_t key); |
| 2431 | static inline uint32_t HashForObject(uint32_t key, Object* object); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2432 | MUST_USE_RESULT static inline Object* AsObject(uint32_t key); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2433 | static const int kPrefixSize = 2; |
| 2434 | static const int kEntrySize = 3; |
| 2435 | static const bool kIsEnumerable = false; |
| 2436 | }; |
| 2437 | |
| 2438 | |
| 2439 | class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> { |
| 2440 | public: |
| 2441 | static NumberDictionary* cast(Object* obj) { |
| 2442 | ASSERT(obj->IsDictionary()); |
| 2443 | return reinterpret_cast<NumberDictionary*>(obj); |
| 2444 | } |
| 2445 | |
| 2446 | // Type specific at put (default NONE attributes is used when adding). |
| 2447 | Object* AtNumberPut(uint32_t key, Object* value); |
| 2448 | Object* AddNumberEntry(uint32_t key, |
| 2449 | Object* value, |
| 2450 | PropertyDetails details); |
| 2451 | |
| 2452 | // Set an existing entry or add a new one if needed. |
| 2453 | Object* Set(uint32_t key, Object* value, PropertyDetails details); |
| 2454 | |
| 2455 | void UpdateMaxNumberKey(uint32_t key); |
| 2456 | |
| 2457 | // If slow elements are required we will never go back to fast-case |
| 2458 | // for the elements kept in this dictionary. We require slow |
| 2459 | // elements if an element has been added at an index larger than |
| 2460 | // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called |
| 2461 | // when defining a getter or setter with a number key. |
| 2462 | inline bool requires_slow_elements(); |
| 2463 | inline void set_requires_slow_elements(); |
| 2464 | |
| 2465 | // Get the value of the max number key that has been added to this |
| 2466 | // dictionary. max_number_key can only be called if |
| 2467 | // requires_slow_elements returns false. |
| 2468 | inline uint32_t max_number_key(); |
| 2469 | |
| 2470 | // Remove all entries were key is a number and (from <= key && key < to). |
| 2471 | void RemoveNumberEntries(uint32_t from, uint32_t to); |
| 2472 | |
| 2473 | // Bit masks. |
| 2474 | static const int kRequiresSlowElementsMask = 1; |
| 2475 | static const int kRequiresSlowElementsTagSize = 1; |
| 2476 | static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1; |
| 2477 | }; |
| 2478 | |
| 2479 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2480 | // JSFunctionResultCache caches results of some JSFunction invocation. |
| 2481 | // It is a fixed array with fixed structure: |
| 2482 | // [0]: factory function |
| 2483 | // [1]: finger index |
| 2484 | // [2]: current cache size |
| 2485 | // [3]: dummy field. |
| 2486 | // The rest of array are key/value pairs. |
| 2487 | class JSFunctionResultCache: public FixedArray { |
| 2488 | public: |
| 2489 | static const int kFactoryIndex = 0; |
| 2490 | static const int kFingerIndex = kFactoryIndex + 1; |
| 2491 | static const int kCacheSizeIndex = kFingerIndex + 1; |
| 2492 | static const int kDummyIndex = kCacheSizeIndex + 1; |
| 2493 | static const int kEntriesIndex = kDummyIndex + 1; |
| 2494 | |
| 2495 | static const int kEntrySize = 2; // key + value |
| 2496 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 2497 | static const int kFactoryOffset = kHeaderSize; |
| 2498 | static const int kFingerOffset = kFactoryOffset + kPointerSize; |
| 2499 | static const int kCacheSizeOffset = kFingerOffset + kPointerSize; |
| 2500 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2501 | inline void MakeZeroSize(); |
| 2502 | inline void Clear(); |
| 2503 | |
| 2504 | // Casting |
| 2505 | static inline JSFunctionResultCache* cast(Object* obj); |
| 2506 | |
| 2507 | #ifdef DEBUG |
| 2508 | void JSFunctionResultCacheVerify(); |
| 2509 | #endif |
| 2510 | }; |
| 2511 | |
| 2512 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2513 | // The cache for maps used by normalized (dictionary mode) objects. |
| 2514 | // Such maps do not have property descriptors, so a typical program |
| 2515 | // needs very limited number of distinct normalized maps. |
| 2516 | class NormalizedMapCache: public FixedArray { |
| 2517 | public: |
| 2518 | static const int kEntries = 64; |
| 2519 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2520 | Object* Get(JSObject* object, PropertyNormalizationMode mode); |
| 2521 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2522 | void Clear(); |
| 2523 | |
| 2524 | // Casting |
| 2525 | static inline NormalizedMapCache* cast(Object* obj); |
| 2526 | |
| 2527 | #ifdef DEBUG |
| 2528 | void NormalizedMapCacheVerify(); |
| 2529 | #endif |
| 2530 | |
| 2531 | private: |
| 2532 | static int Hash(Map* fast); |
| 2533 | |
| 2534 | static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode); |
| 2535 | }; |
| 2536 | |
| 2537 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2538 | // ByteArray represents fixed sized byte arrays. Used by the outside world, |
| 2539 | // such as PCRE, and also by the memory allocator and garbage collector to |
| 2540 | // fill in free blocks in the heap. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2541 | class ByteArray: public HeapObject { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2542 | public: |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2543 | // [length]: length of the array. |
| 2544 | inline int length(); |
| 2545 | inline void set_length(int value); |
| 2546 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2547 | // Setter and getter. |
| 2548 | inline byte get(int index); |
| 2549 | inline void set(int index, byte value); |
| 2550 | |
| 2551 | // Treat contents as an int array. |
| 2552 | inline int get_int(int index); |
| 2553 | |
| 2554 | static int SizeFor(int length) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2555 | return OBJECT_POINTER_ALIGN(kHeaderSize + length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2556 | } |
| 2557 | // We use byte arrays for free blocks in the heap. Given a desired size in |
| 2558 | // bytes that is a multiple of the word size and big enough to hold a byte |
| 2559 | // array, this function returns the number of elements a byte array should |
| 2560 | // have. |
| 2561 | static int LengthFor(int size_in_bytes) { |
| 2562 | ASSERT(IsAligned(size_in_bytes, kPointerSize)); |
| 2563 | ASSERT(size_in_bytes >= kHeaderSize); |
| 2564 | return size_in_bytes - kHeaderSize; |
| 2565 | } |
| 2566 | |
| 2567 | // Returns data start address. |
| 2568 | inline Address GetDataStartAddress(); |
| 2569 | |
| 2570 | // Returns a pointer to the ByteArray object for a given data start address. |
| 2571 | static inline ByteArray* FromDataStartAddress(Address address); |
| 2572 | |
| 2573 | // Casting. |
| 2574 | static inline ByteArray* cast(Object* obj); |
| 2575 | |
| 2576 | // Dispatched behavior. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2577 | inline int ByteArraySize() { |
| 2578 | return SizeFor(this->length()); |
| 2579 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2580 | #ifdef DEBUG |
| 2581 | void ByteArrayPrint(); |
| 2582 | void ByteArrayVerify(); |
| 2583 | #endif |
| 2584 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2585 | // Layout description. |
| 2586 | // Length is smi tagged when it is stored. |
| 2587 | static const int kLengthOffset = HeapObject::kHeaderSize; |
| 2588 | static const int kHeaderSize = kLengthOffset + kPointerSize; |
| 2589 | |
| 2590 | static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2591 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 2592 | // Maximal memory consumption for a single ByteArray. |
| 2593 | static const int kMaxSize = 512 * MB; |
| 2594 | // Maximal length of a single ByteArray. |
| 2595 | static const int kMaxLength = kMaxSize - kHeaderSize; |
| 2596 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2597 | private: |
| 2598 | DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray); |
| 2599 | }; |
| 2600 | |
| 2601 | |
| 2602 | // A PixelArray represents a fixed-size byte array with special semantics |
| 2603 | // used for implementing the CanvasPixelArray object. Please see the |
| 2604 | // specification at: |
| 2605 | // http://www.whatwg.org/specs/web-apps/current-work/ |
| 2606 | // multipage/the-canvas-element.html#canvaspixelarray |
| 2607 | // In particular, write access clamps the value written to 0 or 255 if the |
| 2608 | // value written is outside this range. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2609 | class PixelArray: public HeapObject { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2610 | public: |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2611 | // [length]: length of the array. |
| 2612 | inline int length(); |
| 2613 | inline void set_length(int value); |
| 2614 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2615 | // [external_pointer]: The pointer to the external memory area backing this |
| 2616 | // pixel array. |
| 2617 | DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store. |
| 2618 | |
| 2619 | // Setter and getter. |
| 2620 | inline uint8_t get(int index); |
| 2621 | inline void set(int index, uint8_t value); |
| 2622 | |
| 2623 | // This accessor applies the correct conversion from Smi, HeapNumber and |
| 2624 | // undefined and clamps the converted value between 0 and 255. |
| 2625 | Object* SetValue(uint32_t index, Object* value); |
| 2626 | |
| 2627 | // Casting. |
| 2628 | static inline PixelArray* cast(Object* obj); |
| 2629 | |
| 2630 | #ifdef DEBUG |
| 2631 | void PixelArrayPrint(); |
| 2632 | void PixelArrayVerify(); |
| 2633 | #endif // DEBUG |
| 2634 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2635 | // Maximal acceptable length for a pixel array. |
| 2636 | static const int kMaxLength = 0x3fffffff; |
| 2637 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2638 | // PixelArray headers are not quadword aligned. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2639 | static const int kLengthOffset = HeapObject::kHeaderSize; |
| 2640 | static const int kExternalPointerOffset = |
| 2641 | POINTER_SIZE_ALIGN(kLengthOffset + kIntSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2642 | static const int kHeaderSize = kExternalPointerOffset + kPointerSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2643 | static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2644 | |
| 2645 | private: |
| 2646 | DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray); |
| 2647 | }; |
| 2648 | |
| 2649 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2650 | // An ExternalArray represents a fixed-size array of primitive values |
| 2651 | // which live outside the JavaScript heap. Its subclasses are used to |
| 2652 | // implement the CanvasArray types being defined in the WebGL |
| 2653 | // specification. As of this writing the first public draft is not yet |
| 2654 | // available, but Khronos members can access the draft at: |
| 2655 | // https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html |
| 2656 | // |
| 2657 | // The semantics of these arrays differ from CanvasPixelArray. |
| 2658 | // Out-of-range values passed to the setter are converted via a C |
| 2659 | // cast, not clamping. Out-of-range indices cause exceptions to be |
| 2660 | // raised rather than being silently ignored. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2661 | class ExternalArray: public HeapObject { |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2662 | public: |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2663 | // [length]: length of the array. |
| 2664 | inline int length(); |
| 2665 | inline void set_length(int value); |
| 2666 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2667 | // [external_pointer]: The pointer to the external memory area backing this |
| 2668 | // external array. |
| 2669 | DECL_ACCESSORS(external_pointer, void) // Pointer to the data store. |
| 2670 | |
| 2671 | // Casting. |
| 2672 | static inline ExternalArray* cast(Object* obj); |
| 2673 | |
| 2674 | // Maximal acceptable length for an external array. |
| 2675 | static const int kMaxLength = 0x3fffffff; |
| 2676 | |
| 2677 | // ExternalArray headers are not quadword aligned. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2678 | static const int kLengthOffset = HeapObject::kHeaderSize; |
| 2679 | static const int kExternalPointerOffset = |
| 2680 | POINTER_SIZE_ALIGN(kLengthOffset + kIntSize); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2681 | static const int kHeaderSize = kExternalPointerOffset + kPointerSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2682 | static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize); |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 2683 | |
| 2684 | private: |
| 2685 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray); |
| 2686 | }; |
| 2687 | |
| 2688 | |
| 2689 | class ExternalByteArray: public ExternalArray { |
| 2690 | public: |
| 2691 | // Setter and getter. |
| 2692 | inline int8_t get(int index); |
| 2693 | inline void set(int index, int8_t value); |
| 2694 | |
| 2695 | // This accessor applies the correct conversion from Smi, HeapNumber |
| 2696 | // and undefined. |
| 2697 | Object* SetValue(uint32_t index, Object* value); |
| 2698 | |
| 2699 | // Casting. |
| 2700 | static inline ExternalByteArray* cast(Object* obj); |
| 2701 | |
| 2702 | #ifdef DEBUG |
| 2703 | void ExternalByteArrayPrint(); |
| 2704 | void ExternalByteArrayVerify(); |
| 2705 | #endif // DEBUG |
| 2706 | |
| 2707 | private: |
| 2708 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray); |
| 2709 | }; |
| 2710 | |
| 2711 | |
| 2712 | class ExternalUnsignedByteArray: public ExternalArray { |
| 2713 | public: |
| 2714 | // Setter and getter. |
| 2715 | inline uint8_t get(int index); |
| 2716 | inline void set(int index, uint8_t value); |
| 2717 | |
| 2718 | // This accessor applies the correct conversion from Smi, HeapNumber |
| 2719 | // and undefined. |
| 2720 | Object* SetValue(uint32_t index, Object* value); |
| 2721 | |
| 2722 | // Casting. |
| 2723 | static inline ExternalUnsignedByteArray* cast(Object* obj); |
| 2724 | |
| 2725 | #ifdef DEBUG |
| 2726 | void ExternalUnsignedByteArrayPrint(); |
| 2727 | void ExternalUnsignedByteArrayVerify(); |
| 2728 | #endif // DEBUG |
| 2729 | |
| 2730 | private: |
| 2731 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray); |
| 2732 | }; |
| 2733 | |
| 2734 | |
| 2735 | class ExternalShortArray: public ExternalArray { |
| 2736 | public: |
| 2737 | // Setter and getter. |
| 2738 | inline int16_t get(int index); |
| 2739 | inline void set(int index, int16_t value); |
| 2740 | |
| 2741 | // This accessor applies the correct conversion from Smi, HeapNumber |
| 2742 | // and undefined. |
| 2743 | Object* SetValue(uint32_t index, Object* value); |
| 2744 | |
| 2745 | // Casting. |
| 2746 | static inline ExternalShortArray* cast(Object* obj); |
| 2747 | |
| 2748 | #ifdef DEBUG |
| 2749 | void ExternalShortArrayPrint(); |
| 2750 | void ExternalShortArrayVerify(); |
| 2751 | #endif // DEBUG |
| 2752 | |
| 2753 | private: |
| 2754 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray); |
| 2755 | }; |
| 2756 | |
| 2757 | |
| 2758 | class ExternalUnsignedShortArray: public ExternalArray { |
| 2759 | public: |
| 2760 | // Setter and getter. |
| 2761 | inline uint16_t get(int index); |
| 2762 | inline void set(int index, uint16_t value); |
| 2763 | |
| 2764 | // This accessor applies the correct conversion from Smi, HeapNumber |
| 2765 | // and undefined. |
| 2766 | Object* SetValue(uint32_t index, Object* value); |
| 2767 | |
| 2768 | // Casting. |
| 2769 | static inline ExternalUnsignedShortArray* cast(Object* obj); |
| 2770 | |
| 2771 | #ifdef DEBUG |
| 2772 | void ExternalUnsignedShortArrayPrint(); |
| 2773 | void ExternalUnsignedShortArrayVerify(); |
| 2774 | #endif // DEBUG |
| 2775 | |
| 2776 | private: |
| 2777 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray); |
| 2778 | }; |
| 2779 | |
| 2780 | |
| 2781 | class ExternalIntArray: public ExternalArray { |
| 2782 | public: |
| 2783 | // Setter and getter. |
| 2784 | inline int32_t get(int index); |
| 2785 | inline void set(int index, int32_t value); |
| 2786 | |
| 2787 | // This accessor applies the correct conversion from Smi, HeapNumber |
| 2788 | // and undefined. |
| 2789 | Object* SetValue(uint32_t index, Object* value); |
| 2790 | |
| 2791 | // Casting. |
| 2792 | static inline ExternalIntArray* cast(Object* obj); |
| 2793 | |
| 2794 | #ifdef DEBUG |
| 2795 | void ExternalIntArrayPrint(); |
| 2796 | void ExternalIntArrayVerify(); |
| 2797 | #endif // DEBUG |
| 2798 | |
| 2799 | private: |
| 2800 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray); |
| 2801 | }; |
| 2802 | |
| 2803 | |
| 2804 | class ExternalUnsignedIntArray: public ExternalArray { |
| 2805 | public: |
| 2806 | // Setter and getter. |
| 2807 | inline uint32_t get(int index); |
| 2808 | inline void set(int index, uint32_t value); |
| 2809 | |
| 2810 | // This accessor applies the correct conversion from Smi, HeapNumber |
| 2811 | // and undefined. |
| 2812 | Object* SetValue(uint32_t index, Object* value); |
| 2813 | |
| 2814 | // Casting. |
| 2815 | static inline ExternalUnsignedIntArray* cast(Object* obj); |
| 2816 | |
| 2817 | #ifdef DEBUG |
| 2818 | void ExternalUnsignedIntArrayPrint(); |
| 2819 | void ExternalUnsignedIntArrayVerify(); |
| 2820 | #endif // DEBUG |
| 2821 | |
| 2822 | private: |
| 2823 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray); |
| 2824 | }; |
| 2825 | |
| 2826 | |
| 2827 | class ExternalFloatArray: public ExternalArray { |
| 2828 | public: |
| 2829 | // Setter and getter. |
| 2830 | inline float get(int index); |
| 2831 | inline void set(int index, float value); |
| 2832 | |
| 2833 | // This accessor applies the correct conversion from Smi, HeapNumber |
| 2834 | // and undefined. |
| 2835 | Object* SetValue(uint32_t index, Object* value); |
| 2836 | |
| 2837 | // Casting. |
| 2838 | static inline ExternalFloatArray* cast(Object* obj); |
| 2839 | |
| 2840 | #ifdef DEBUG |
| 2841 | void ExternalFloatArrayPrint(); |
| 2842 | void ExternalFloatArrayVerify(); |
| 2843 | #endif // DEBUG |
| 2844 | |
| 2845 | private: |
| 2846 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray); |
| 2847 | }; |
| 2848 | |
| 2849 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2850 | // Code describes objects with on-the-fly generated machine code. |
| 2851 | class Code: public HeapObject { |
| 2852 | public: |
| 2853 | // Opaque data type for encapsulating code flags like kind, inline |
| 2854 | // cache state, and arguments count. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 2855 | // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that |
| 2856 | // enumeration type has correct value range (see Issue 830 for more details). |
| 2857 | enum Flags { |
| 2858 | FLAGS_MIN_VALUE = kMinInt, |
| 2859 | FLAGS_MAX_VALUE = kMaxInt |
| 2860 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2861 | |
| 2862 | enum Kind { |
| 2863 | FUNCTION, |
| 2864 | STUB, |
| 2865 | BUILTIN, |
| 2866 | LOAD_IC, |
| 2867 | KEYED_LOAD_IC, |
| 2868 | CALL_IC, |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2869 | KEYED_CALL_IC, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2870 | STORE_IC, |
| 2871 | KEYED_STORE_IC, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2872 | BINARY_OP_IC, |
| 2873 | // No more than 16 kinds. The value currently encoded in four bits in |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2874 | // Flags. |
| 2875 | |
| 2876 | // Pseudo-kinds. |
| 2877 | REGEXP = BUILTIN, |
| 2878 | FIRST_IC_KIND = LOAD_IC, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2879 | LAST_IC_KIND = BINARY_OP_IC |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2880 | }; |
| 2881 | |
| 2882 | enum { |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 2883 | NUMBER_OF_KINDS = LAST_IC_KIND + 1 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2884 | }; |
| 2885 | |
| 2886 | #ifdef ENABLE_DISASSEMBLER |
| 2887 | // Printing |
| 2888 | static const char* Kind2String(Kind kind); |
| 2889 | static const char* ICState2String(InlineCacheState state); |
| 2890 | static const char* PropertyType2String(PropertyType type); |
| 2891 | void Disassemble(const char* name); |
| 2892 | #endif // ENABLE_DISASSEMBLER |
| 2893 | |
| 2894 | // [instruction_size]: Size of the native instructions |
| 2895 | inline int instruction_size(); |
| 2896 | inline void set_instruction_size(int value); |
| 2897 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2898 | // [relocation_info]: Code relocation information |
| 2899 | DECL_ACCESSORS(relocation_info, ByteArray) |
| 2900 | |
| 2901 | // Unchecked accessor to be used during GC. |
| 2902 | inline ByteArray* unchecked_relocation_info(); |
| 2903 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2904 | inline int relocation_size(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2905 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2906 | // [flags]: Various code flags. |
| 2907 | inline Flags flags(); |
| 2908 | inline void set_flags(Flags flags); |
| 2909 | |
| 2910 | // [flags]: Access to specific code flags. |
| 2911 | inline Kind kind(); |
| 2912 | inline InlineCacheState ic_state(); // Only valid for IC stubs. |
| 2913 | inline InLoopFlag ic_in_loop(); // Only valid for IC stubs. |
| 2914 | inline PropertyType type(); // Only valid for monomorphic IC stubs. |
| 2915 | inline int arguments_count(); // Only valid for call IC stubs. |
| 2916 | |
| 2917 | // Testers for IC stub kinds. |
| 2918 | inline bool is_inline_cache_stub(); |
| 2919 | inline bool is_load_stub() { return kind() == LOAD_IC; } |
| 2920 | inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; } |
| 2921 | inline bool is_store_stub() { return kind() == STORE_IC; } |
| 2922 | inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; } |
| 2923 | inline bool is_call_stub() { return kind() == CALL_IC; } |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 2924 | inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2925 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 2926 | // [major_key]: For kind STUB or BINARY_OP_IC, the major key. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 2927 | inline int major_key(); |
| 2928 | inline void set_major_key(int major); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2929 | |
| 2930 | // Flags operations. |
| 2931 | static inline Flags ComputeFlags(Kind kind, |
| 2932 | InLoopFlag in_loop = NOT_IN_LOOP, |
| 2933 | InlineCacheState ic_state = UNINITIALIZED, |
| 2934 | PropertyType type = NORMAL, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2935 | int argc = -1, |
| 2936 | InlineCacheHolderFlag holder = OWN_MAP); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2937 | |
| 2938 | static inline Flags ComputeMonomorphicFlags( |
| 2939 | Kind kind, |
| 2940 | PropertyType type, |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2941 | InlineCacheHolderFlag holder = OWN_MAP, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2942 | InLoopFlag in_loop = NOT_IN_LOOP, |
| 2943 | int argc = -1); |
| 2944 | |
| 2945 | static inline Kind ExtractKindFromFlags(Flags flags); |
| 2946 | static inline InlineCacheState ExtractICStateFromFlags(Flags flags); |
| 2947 | static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags); |
| 2948 | static inline PropertyType ExtractTypeFromFlags(Flags flags); |
| 2949 | static inline int ExtractArgumentsCountFromFlags(Flags flags); |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 2950 | static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2951 | static inline Flags RemoveTypeFromFlags(Flags flags); |
| 2952 | |
| 2953 | // Convert a target address into a code object. |
| 2954 | static inline Code* GetCodeFromTargetAddress(Address address); |
| 2955 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 2956 | // Convert an entry address into an object. |
| 2957 | static inline Object* GetObjectFromEntryAddress(Address location_of_address); |
| 2958 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2959 | // Returns the address of the first instruction. |
| 2960 | inline byte* instruction_start(); |
| 2961 | |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 2962 | // Returns the address right after the last instruction. |
| 2963 | inline byte* instruction_end(); |
| 2964 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2965 | // Returns the size of the instructions, padding, and relocation information. |
| 2966 | inline int body_size(); |
| 2967 | |
| 2968 | // Returns the address of the first relocation info (read backwards!). |
| 2969 | inline byte* relocation_start(); |
| 2970 | |
| 2971 | // Code entry point. |
| 2972 | inline byte* entry(); |
| 2973 | |
| 2974 | // Returns true if pc is inside this object's instructions. |
| 2975 | inline bool contains(byte* pc); |
| 2976 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2977 | // Relocate the code by delta bytes. Called to signal that this code |
| 2978 | // object has been moved by delta bytes. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 2979 | void Relocate(intptr_t delta); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2980 | |
| 2981 | // Migrate code described by desc. |
| 2982 | void CopyFrom(const CodeDesc& desc); |
| 2983 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2984 | // Returns the object size for a given body (used for allocation). |
| 2985 | static int SizeFor(int body_size) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2986 | ASSERT_SIZE_TAG_ALIGNED(body_size); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 2987 | return RoundUp(kHeaderSize + body_size, kCodeAlignment); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | // Calculate the size of the code object to report for log events. This takes |
| 2991 | // the layout of the code object into account. |
| 2992 | int ExecutableSize() { |
| 2993 | // Check that the assumptions about the layout of the code object holds. |
| 2994 | ASSERT_EQ(static_cast<int>(instruction_start() - address()), |
| 2995 | Code::kHeaderSize); |
| 2996 | return instruction_size() + Code::kHeaderSize; |
| 2997 | } |
| 2998 | |
| 2999 | // Locating source position. |
| 3000 | int SourcePosition(Address pc); |
| 3001 | int SourceStatementPosition(Address pc); |
| 3002 | |
| 3003 | // Casting. |
| 3004 | static inline Code* cast(Object* obj); |
| 3005 | |
| 3006 | // Dispatched behavior. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 3007 | int CodeSize() { return SizeFor(body_size()); } |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3008 | inline void CodeIterateBody(ObjectVisitor* v); |
| 3009 | |
| 3010 | template<typename StaticVisitor> |
| 3011 | inline void CodeIterateBody(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3012 | #ifdef DEBUG |
| 3013 | void CodePrint(); |
| 3014 | void CodeVerify(); |
| 3015 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3016 | // Layout description. |
| 3017 | static const int kInstructionSizeOffset = HeapObject::kHeaderSize; |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 3018 | static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 3019 | static const int kFlagsOffset = kRelocationInfoOffset + kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3020 | static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize; |
| 3021 | // Add padding to align the instruction start following right after |
| 3022 | // the Code object header. |
| 3023 | static const int kHeaderSize = |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3024 | CODE_POINTER_ALIGN(kKindSpecificFlagsOffset + kIntSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3025 | |
| 3026 | // Byte offsets within kKindSpecificFlagsOffset. |
| 3027 | static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset + 1; |
| 3028 | |
| 3029 | // Flags layout. |
| 3030 | static const int kFlagsICStateShift = 0; |
| 3031 | static const int kFlagsICInLoopShift = 3; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3032 | static const int kFlagsTypeShift = 4; |
| 3033 | static const int kFlagsKindShift = 7; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3034 | static const int kFlagsICHolderShift = 11; |
| 3035 | static const int kFlagsArgumentsCountShift = 12; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3036 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3037 | static const int kFlagsICStateMask = 0x00000007; // 00000000111 |
| 3038 | static const int kFlagsICInLoopMask = 0x00000008; // 00000001000 |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 3039 | static const int kFlagsTypeMask = 0x00000070; // 00001110000 |
| 3040 | static const int kFlagsKindMask = 0x00000780; // 11110000000 |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3041 | static const int kFlagsCacheInPrototypeMapMask = 0x00000800; |
| 3042 | static const int kFlagsArgumentsCountMask = 0xFFFFF000; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3043 | |
| 3044 | static const int kFlagsNotUsedInLookup = |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3045 | (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3046 | |
| 3047 | private: |
| 3048 | DISALLOW_IMPLICIT_CONSTRUCTORS(Code); |
| 3049 | }; |
| 3050 | |
| 3051 | |
| 3052 | // All heap objects have a Map that describes their structure. |
| 3053 | // A Map contains information about: |
| 3054 | // - Size information about the object |
| 3055 | // - How to iterate over an object (for garbage collection) |
| 3056 | class Map: public HeapObject { |
| 3057 | public: |
| 3058 | // Instance size. |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3059 | // Size in bytes or kVariableSizeSentinel if instances do not have |
| 3060 | // a fixed size. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3061 | inline int instance_size(); |
| 3062 | inline void set_instance_size(int value); |
| 3063 | |
| 3064 | // Count of properties allocated in the object. |
| 3065 | inline int inobject_properties(); |
| 3066 | inline void set_inobject_properties(int value); |
| 3067 | |
| 3068 | // Count of property fields pre-allocated in the object when first allocated. |
| 3069 | inline int pre_allocated_property_fields(); |
| 3070 | inline void set_pre_allocated_property_fields(int value); |
| 3071 | |
| 3072 | // Instance type. |
| 3073 | inline InstanceType instance_type(); |
| 3074 | inline void set_instance_type(InstanceType value); |
| 3075 | |
| 3076 | // Tells how many unused property fields are available in the |
| 3077 | // instance (only used for JSObject in fast mode). |
| 3078 | inline int unused_property_fields(); |
| 3079 | inline void set_unused_property_fields(int value); |
| 3080 | |
| 3081 | // Bit field. |
| 3082 | inline byte bit_field(); |
| 3083 | inline void set_bit_field(byte value); |
| 3084 | |
| 3085 | // Bit field 2. |
| 3086 | inline byte bit_field2(); |
| 3087 | inline void set_bit_field2(byte value); |
| 3088 | |
| 3089 | // Tells whether the object in the prototype property will be used |
| 3090 | // for instances created from this function. If the prototype |
| 3091 | // property is set to a value that is not a JSObject, the prototype |
| 3092 | // property will not be used to create instances of the function. |
| 3093 | // See ECMA-262, 13.2.2. |
| 3094 | inline void set_non_instance_prototype(bool value); |
| 3095 | inline bool has_non_instance_prototype(); |
| 3096 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3097 | // Tells whether function has special prototype property. If not, prototype |
| 3098 | // property will not be created when accessed (will return undefined), |
| 3099 | // and construction from this function will not be allowed. |
| 3100 | inline void set_function_with_prototype(bool value); |
| 3101 | inline bool function_with_prototype(); |
| 3102 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3103 | // Tells whether the instance with this map should be ignored by the |
| 3104 | // __proto__ accessor. |
| 3105 | inline void set_is_hidden_prototype() { |
| 3106 | set_bit_field(bit_field() | (1 << kIsHiddenPrototype)); |
| 3107 | } |
| 3108 | |
| 3109 | inline bool is_hidden_prototype() { |
| 3110 | return ((1 << kIsHiddenPrototype) & bit_field()) != 0; |
| 3111 | } |
| 3112 | |
| 3113 | // Records and queries whether the instance has a named interceptor. |
| 3114 | inline void set_has_named_interceptor() { |
| 3115 | set_bit_field(bit_field() | (1 << kHasNamedInterceptor)); |
| 3116 | } |
| 3117 | |
| 3118 | inline bool has_named_interceptor() { |
| 3119 | return ((1 << kHasNamedInterceptor) & bit_field()) != 0; |
| 3120 | } |
| 3121 | |
| 3122 | // Records and queries whether the instance has an indexed interceptor. |
| 3123 | inline void set_has_indexed_interceptor() { |
| 3124 | set_bit_field(bit_field() | (1 << kHasIndexedInterceptor)); |
| 3125 | } |
| 3126 | |
| 3127 | inline bool has_indexed_interceptor() { |
| 3128 | return ((1 << kHasIndexedInterceptor) & bit_field()) != 0; |
| 3129 | } |
| 3130 | |
| 3131 | // Tells whether the instance is undetectable. |
| 3132 | // An undetectable object is a special class of JSObject: 'typeof' operator |
| 3133 | // returns undefined, ToBoolean returns false. Otherwise it behaves like |
| 3134 | // a normal JS object. It is useful for implementing undetectable |
| 3135 | // document.all in Firefox & Safari. |
| 3136 | // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549. |
| 3137 | inline void set_is_undetectable() { |
| 3138 | set_bit_field(bit_field() | (1 << kIsUndetectable)); |
| 3139 | } |
| 3140 | |
| 3141 | inline bool is_undetectable() { |
| 3142 | return ((1 << kIsUndetectable) & bit_field()) != 0; |
| 3143 | } |
| 3144 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3145 | // Tells whether the instance has a call-as-function handler. |
| 3146 | inline void set_has_instance_call_handler() { |
| 3147 | set_bit_field(bit_field() | (1 << kHasInstanceCallHandler)); |
| 3148 | } |
| 3149 | |
| 3150 | inline bool has_instance_call_handler() { |
| 3151 | return ((1 << kHasInstanceCallHandler) & bit_field()) != 0; |
| 3152 | } |
| 3153 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3154 | inline void set_is_extensible(bool value); |
| 3155 | inline bool is_extensible(); |
| 3156 | |
| 3157 | // Tells whether the instance has fast elements. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3158 | // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS. |
| 3159 | inline void set_has_fast_elements(bool value) { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3160 | if (value) { |
| 3161 | set_bit_field2(bit_field2() | (1 << kHasFastElements)); |
| 3162 | } else { |
| 3163 | set_bit_field2(bit_field2() & ~(1 << kHasFastElements)); |
| 3164 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3167 | inline bool has_fast_elements() { |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3168 | return ((1 << kHasFastElements) & bit_field2()) != 0; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3171 | // Tells whether the map is attached to SharedFunctionInfo |
| 3172 | // (for inobject slack tracking). |
| 3173 | inline void set_attached_to_shared_function_info(bool value); |
| 3174 | |
| 3175 | inline bool attached_to_shared_function_info(); |
| 3176 | |
| 3177 | // Tells whether the map is shared between objects that may have different |
| 3178 | // behavior. If true, the map should never be modified, instead a clone |
| 3179 | // should be created and modified. |
| 3180 | inline void set_is_shared(bool value); |
| 3181 | |
| 3182 | inline bool is_shared(); |
| 3183 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3184 | // Tells whether the instance needs security checks when accessing its |
| 3185 | // properties. |
| 3186 | inline void set_is_access_check_needed(bool access_check_needed); |
| 3187 | inline bool is_access_check_needed(); |
| 3188 | |
| 3189 | // [prototype]: implicit prototype object. |
| 3190 | DECL_ACCESSORS(prototype, Object) |
| 3191 | |
| 3192 | // [constructor]: points back to the function responsible for this map. |
| 3193 | DECL_ACCESSORS(constructor, Object) |
| 3194 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3195 | inline JSFunction* unchecked_constructor(); |
| 3196 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3197 | // [instance descriptors]: describes the object. |
| 3198 | DECL_ACCESSORS(instance_descriptors, DescriptorArray) |
| 3199 | |
| 3200 | // [stub cache]: contains stubs compiled for this map. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3201 | DECL_ACCESSORS(code_cache, Object) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3202 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3203 | MUST_USE_RESULT Object* CopyDropDescriptors(); |
| 3204 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3205 | MUST_USE_RESULT Object* CopyNormalized(PropertyNormalizationMode mode, |
| 3206 | NormalizedMapSharingMode sharing); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3207 | |
| 3208 | // Returns a copy of the map, with all transitions dropped from the |
| 3209 | // instance descriptors. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3210 | MUST_USE_RESULT Object* CopyDropTransitions(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3211 | |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3212 | // Returns this map if it has the fast elements bit set, otherwise |
| 3213 | // returns a copy of the map, with all transitions dropped from the |
| 3214 | // descriptors and the fast elements bit set. |
| 3215 | inline Object* GetFastElementsMap(); |
| 3216 | |
| 3217 | // Returns this map if it has the fast elements bit cleared, |
| 3218 | // otherwise returns a copy of the map, with all transitions dropped |
| 3219 | // from the descriptors and the fast elements bit cleared. |
| 3220 | inline Object* GetSlowElementsMap(); |
| 3221 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3222 | // Returns the property index for name (only valid for FAST MODE). |
| 3223 | int PropertyIndexFor(String* name); |
| 3224 | |
| 3225 | // Returns the next free property index (only valid for FAST MODE). |
| 3226 | int NextFreePropertyIndex(); |
| 3227 | |
| 3228 | // Returns the number of properties described in instance_descriptors. |
| 3229 | int NumberOfDescribedProperties(); |
| 3230 | |
| 3231 | // Casting. |
| 3232 | static inline Map* cast(Object* obj); |
| 3233 | |
| 3234 | // Locate an accessor in the instance descriptor. |
| 3235 | AccessorDescriptor* FindAccessor(String* name); |
| 3236 | |
| 3237 | // Code cache operations. |
| 3238 | |
| 3239 | // Clears the code cache. |
| 3240 | inline void ClearCodeCache(); |
| 3241 | |
| 3242 | // Update code cache. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3243 | MUST_USE_RESULT Object* UpdateCodeCache(String* name, Code* code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3244 | |
| 3245 | // Returns the found code or undefined if absent. |
| 3246 | Object* FindInCodeCache(String* name, Code::Flags flags); |
| 3247 | |
| 3248 | // Returns the non-negative index of the code object if it is in the |
| 3249 | // cache and -1 otherwise. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3250 | int IndexInCodeCache(Object* name, Code* code); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3251 | |
| 3252 | // Removes a code object from the code cache at the given index. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3253 | void RemoveFromCodeCache(String* name, Code* code, int index); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3254 | |
| 3255 | // For every transition in this map, makes the transition's |
| 3256 | // target's prototype pointer point back to this map. |
| 3257 | // This is undone in MarkCompactCollector::ClearNonLiveTransitions(). |
| 3258 | void CreateBackPointers(); |
| 3259 | |
| 3260 | // Set all map transitions from this map to dead maps to null. |
| 3261 | // Also, restore the original prototype on the targets of these |
| 3262 | // transitions, so that we do not process this map again while |
| 3263 | // following back pointers. |
| 3264 | void ClearNonLiveTransitions(Object* real_prototype); |
| 3265 | |
| 3266 | // Dispatched behavior. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3267 | #ifdef DEBUG |
| 3268 | void MapPrint(); |
| 3269 | void MapVerify(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3270 | void SharedMapVerify(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3271 | #endif |
| 3272 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3273 | inline int visitor_id(); |
| 3274 | inline void set_visitor_id(int visitor_id); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 3275 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3276 | typedef void (*TraverseCallback)(Map* map, void* data); |
| 3277 | |
| 3278 | void TraverseTransitionTree(TraverseCallback callback, void* data); |
| 3279 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3280 | static const int kMaxPreAllocatedPropertyFields = 255; |
| 3281 | |
| 3282 | // Layout description. |
| 3283 | static const int kInstanceSizesOffset = HeapObject::kHeaderSize; |
| 3284 | static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize; |
| 3285 | static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize; |
| 3286 | static const int kConstructorOffset = kPrototypeOffset + kPointerSize; |
| 3287 | static const int kInstanceDescriptorsOffset = |
| 3288 | kConstructorOffset + kPointerSize; |
| 3289 | static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 3290 | static const int kPadStart = kCodeCacheOffset + kPointerSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3291 | static const int kSize = MAP_POINTER_ALIGN(kPadStart); |
| 3292 | |
| 3293 | // Layout of pointer fields. Heap iteration code relies on them |
| 3294 | // being continiously allocated. |
| 3295 | static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset; |
| 3296 | static const int kPointerFieldsEndOffset = |
| 3297 | Map::kCodeCacheOffset + kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3298 | |
| 3299 | // Byte offsets within kInstanceSizesOffset. |
| 3300 | static const int kInstanceSizeOffset = kInstanceSizesOffset + 0; |
| 3301 | static const int kInObjectPropertiesByte = 1; |
| 3302 | static const int kInObjectPropertiesOffset = |
| 3303 | kInstanceSizesOffset + kInObjectPropertiesByte; |
| 3304 | static const int kPreAllocatedPropertyFieldsByte = 2; |
| 3305 | static const int kPreAllocatedPropertyFieldsOffset = |
| 3306 | kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte; |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 3307 | static const int kVisitorIdByte = 3; |
| 3308 | static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3309 | |
| 3310 | // Byte offsets within kInstanceAttributesOffset attributes. |
| 3311 | static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0; |
| 3312 | static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1; |
| 3313 | static const int kBitFieldOffset = kInstanceAttributesOffset + 2; |
| 3314 | static const int kBitField2Offset = kInstanceAttributesOffset + 3; |
| 3315 | |
| 3316 | STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset); |
| 3317 | |
| 3318 | // Bit positions for bit field. |
| 3319 | static const int kUnused = 0; // To be used for marking recently used maps. |
| 3320 | static const int kHasNonInstancePrototype = 1; |
| 3321 | static const int kIsHiddenPrototype = 2; |
| 3322 | static const int kHasNamedInterceptor = 3; |
| 3323 | static const int kHasIndexedInterceptor = 4; |
| 3324 | static const int kIsUndetectable = 5; |
| 3325 | static const int kHasInstanceCallHandler = 6; |
| 3326 | static const int kIsAccessCheckNeeded = 7; |
| 3327 | |
| 3328 | // Bit positions for bit field 2 |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 3329 | static const int kIsExtensible = 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3330 | static const int kFunctionWithPrototype = 1; |
Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 3331 | static const int kHasFastElements = 2; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3332 | static const int kStringWrapperSafeForDefaultValueOf = 3; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3333 | static const int kAttachedToSharedFunctionInfo = 4; |
| 3334 | static const int kIsShared = 5; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3335 | |
| 3336 | // Layout of the default cache. It holds alternating name and code objects. |
| 3337 | static const int kCodeCacheEntrySize = 2; |
| 3338 | static const int kCodeCacheEntryNameOffset = 0; |
| 3339 | static const int kCodeCacheEntryCodeOffset = 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3340 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3341 | typedef FixedBodyDescriptor<kPointerFieldsBeginOffset, |
| 3342 | kPointerFieldsEndOffset, |
| 3343 | kSize> BodyDescriptor; |
| 3344 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3345 | private: |
| 3346 | DISALLOW_IMPLICIT_CONSTRUCTORS(Map); |
| 3347 | }; |
| 3348 | |
| 3349 | |
| 3350 | // An abstract superclass, a marker class really, for simple structure classes. |
| 3351 | // It doesn't carry much functionality but allows struct classes to me |
| 3352 | // identified in the type system. |
| 3353 | class Struct: public HeapObject { |
| 3354 | public: |
| 3355 | inline void InitializeBody(int object_size); |
| 3356 | static inline Struct* cast(Object* that); |
| 3357 | }; |
| 3358 | |
| 3359 | |
| 3360 | // Script describes a script which has been added to the VM. |
| 3361 | class Script: public Struct { |
| 3362 | public: |
| 3363 | // Script types. |
| 3364 | enum Type { |
| 3365 | TYPE_NATIVE = 0, |
| 3366 | TYPE_EXTENSION = 1, |
| 3367 | TYPE_NORMAL = 2 |
| 3368 | }; |
| 3369 | |
| 3370 | // Script compilation types. |
| 3371 | enum CompilationType { |
| 3372 | COMPILATION_TYPE_HOST = 0, |
| 3373 | COMPILATION_TYPE_EVAL = 1, |
| 3374 | COMPILATION_TYPE_JSON = 2 |
| 3375 | }; |
| 3376 | |
| 3377 | // [source]: the script source. |
| 3378 | DECL_ACCESSORS(source, Object) |
| 3379 | |
| 3380 | // [name]: the script name. |
| 3381 | DECL_ACCESSORS(name, Object) |
| 3382 | |
| 3383 | // [id]: the script id. |
| 3384 | DECL_ACCESSORS(id, Object) |
| 3385 | |
| 3386 | // [line_offset]: script line offset in resource from where it was extracted. |
| 3387 | DECL_ACCESSORS(line_offset, Smi) |
| 3388 | |
| 3389 | // [column_offset]: script column offset in resource from where it was |
| 3390 | // extracted. |
| 3391 | DECL_ACCESSORS(column_offset, Smi) |
| 3392 | |
| 3393 | // [data]: additional data associated with this script. |
| 3394 | DECL_ACCESSORS(data, Object) |
| 3395 | |
| 3396 | // [context_data]: context data for the context this script was compiled in. |
| 3397 | DECL_ACCESSORS(context_data, Object) |
| 3398 | |
| 3399 | // [wrapper]: the wrapper cache. |
| 3400 | DECL_ACCESSORS(wrapper, Proxy) |
| 3401 | |
| 3402 | // [type]: the script type. |
| 3403 | DECL_ACCESSORS(type, Smi) |
| 3404 | |
| 3405 | // [compilation]: how the the script was compiled. |
| 3406 | DECL_ACCESSORS(compilation_type, Smi) |
| 3407 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3408 | // [line_ends]: FixedArray of line ends positions. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3409 | DECL_ACCESSORS(line_ends, Object) |
| 3410 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3411 | // [eval_from_shared]: for eval scripts the shared funcion info for the |
| 3412 | // function from which eval was called. |
| 3413 | DECL_ACCESSORS(eval_from_shared, Object) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3414 | |
| 3415 | // [eval_from_instructions_offset]: the instruction offset in the code for the |
| 3416 | // function from which eval was called where eval was called. |
| 3417 | DECL_ACCESSORS(eval_from_instructions_offset, Smi) |
| 3418 | |
| 3419 | static inline Script* cast(Object* obj); |
| 3420 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 3421 | // If script source is an external string, check that the underlying |
| 3422 | // resource is accessible. Otherwise, always return true. |
| 3423 | inline bool HasValidSource(); |
| 3424 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3425 | #ifdef DEBUG |
| 3426 | void ScriptPrint(); |
| 3427 | void ScriptVerify(); |
| 3428 | #endif |
| 3429 | |
| 3430 | static const int kSourceOffset = HeapObject::kHeaderSize; |
| 3431 | static const int kNameOffset = kSourceOffset + kPointerSize; |
| 3432 | static const int kLineOffsetOffset = kNameOffset + kPointerSize; |
| 3433 | static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize; |
| 3434 | static const int kDataOffset = kColumnOffsetOffset + kPointerSize; |
| 3435 | static const int kContextOffset = kDataOffset + kPointerSize; |
| 3436 | static const int kWrapperOffset = kContextOffset + kPointerSize; |
| 3437 | static const int kTypeOffset = kWrapperOffset + kPointerSize; |
| 3438 | static const int kCompilationTypeOffset = kTypeOffset + kPointerSize; |
| 3439 | static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize; |
| 3440 | static const int kIdOffset = kLineEndsOffset + kPointerSize; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3441 | static const int kEvalFromSharedOffset = kIdOffset + kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3442 | static const int kEvalFrominstructionsOffsetOffset = |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3443 | kEvalFromSharedOffset + kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3444 | static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize; |
| 3445 | |
| 3446 | private: |
| 3447 | DISALLOW_IMPLICIT_CONSTRUCTORS(Script); |
| 3448 | }; |
| 3449 | |
| 3450 | |
| 3451 | // SharedFunctionInfo describes the JSFunction information that can be |
| 3452 | // shared by multiple instances of the function. |
| 3453 | class SharedFunctionInfo: public HeapObject { |
| 3454 | public: |
| 3455 | // [name]: Function name. |
| 3456 | DECL_ACCESSORS(name, Object) |
| 3457 | |
| 3458 | // [code]: Function code. |
| 3459 | DECL_ACCESSORS(code, Code) |
| 3460 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 3461 | // [scope_info]: Scope info. |
| 3462 | DECL_ACCESSORS(scope_info, SerializedScopeInfo) |
| 3463 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3464 | // [construct stub]: Code stub for constructing instances of this function. |
| 3465 | DECL_ACCESSORS(construct_stub, Code) |
| 3466 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3467 | inline Code* unchecked_code(); |
| 3468 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3469 | // Returns if this function has been compiled to native code yet. |
| 3470 | inline bool is_compiled(); |
| 3471 | |
| 3472 | // [length]: The function length - usually the number of declared parameters. |
| 3473 | // Use up to 2^30 parameters. |
| 3474 | inline int length(); |
| 3475 | inline void set_length(int value); |
| 3476 | |
| 3477 | // [formal parameter count]: The declared number of parameters. |
| 3478 | inline int formal_parameter_count(); |
| 3479 | inline void set_formal_parameter_count(int value); |
| 3480 | |
| 3481 | // Set the formal parameter count so the function code will be |
| 3482 | // called without using argument adaptor frames. |
| 3483 | inline void DontAdaptArguments(); |
| 3484 | |
| 3485 | // [expected_nof_properties]: Expected number of properties for the function. |
| 3486 | inline int expected_nof_properties(); |
| 3487 | inline void set_expected_nof_properties(int value); |
| 3488 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3489 | // Inobject slack tracking is the way to reclaim unused inobject space. |
| 3490 | // |
| 3491 | // The instance size is initially determined by adding some slack to |
| 3492 | // expected_nof_properties (to allow for a few extra properties added |
| 3493 | // after the constructor). There is no guarantee that the extra space |
| 3494 | // will not be wasted. |
| 3495 | // |
| 3496 | // Here is the algorithm to reclaim the unused inobject space: |
| 3497 | // - Detect the first constructor call for this SharedFunctionInfo. |
| 3498 | // When it happens enter the "in progress" state: remember the |
| 3499 | // constructor's initial_map and install a special construct stub that |
| 3500 | // counts constructor calls. |
| 3501 | // - While the tracking is in progress create objects filled with |
| 3502 | // one_pointer_filler_map instead of undefined_value. This way they can be |
| 3503 | // resized quickly and safely. |
| 3504 | // - Once enough (kGenerousAllocationCount) objects have been created |
| 3505 | // compute the 'slack' (traverse the map transition tree starting from the |
| 3506 | // initial_map and find the lowest value of unused_property_fields). |
| 3507 | // - Traverse the transition tree again and decrease the instance size |
| 3508 | // of every map. Existing objects will resize automatically (they are |
| 3509 | // filled with one_pointer_filler_map). All further allocations will |
| 3510 | // use the adjusted instance size. |
| 3511 | // - Decrease expected_nof_properties so that an allocations made from |
| 3512 | // another context will use the adjusted instance size too. |
| 3513 | // - Exit "in progress" state by clearing the reference to the initial_map |
| 3514 | // and setting the regular construct stub (generic or inline). |
| 3515 | // |
| 3516 | // The above is the main event sequence. Some special cases are possible |
| 3517 | // while the tracking is in progress: |
| 3518 | // |
| 3519 | // - GC occurs. |
| 3520 | // Check if the initial_map is referenced by any live objects (except this |
| 3521 | // SharedFunctionInfo). If it is, continue tracking as usual. |
| 3522 | // If it is not, clear the reference and reset the tracking state. The |
| 3523 | // tracking will be initiated again on the next constructor call. |
| 3524 | // |
| 3525 | // - The constructor is called from another context. |
| 3526 | // Immediately complete the tracking, perform all the necessary changes |
| 3527 | // to maps. This is necessary because there is no efficient way to track |
| 3528 | // multiple initial_maps. |
| 3529 | // Proceed to create an object in the current context (with the adjusted |
| 3530 | // size). |
| 3531 | // |
| 3532 | // - A different constructor function sharing the same SharedFunctionInfo is |
| 3533 | // called in the same context. This could be another closure in the same |
| 3534 | // context, or the first function could have been disposed. |
| 3535 | // This is handled the same way as the previous case. |
| 3536 | // |
| 3537 | // Important: inobject slack tracking is not attempted during the snapshot |
| 3538 | // creation. |
| 3539 | |
| 3540 | static const int kGenerousAllocationCount = 16; |
| 3541 | |
| 3542 | // [construction_count]: Counter for constructor calls made during |
| 3543 | // the tracking phase. |
| 3544 | inline int construction_count(); |
| 3545 | inline void set_construction_count(int value); |
| 3546 | |
| 3547 | // [initial_map]: initial map of the first function called as a constructor. |
| 3548 | // Saved for the duration of the tracking phase. |
| 3549 | // This is a weak link (GC resets it to undefined_value if no other live |
| 3550 | // object reference this map). |
| 3551 | DECL_ACCESSORS(initial_map, Object) |
| 3552 | |
| 3553 | // True if the initial_map is not undefined and the countdown stub is |
| 3554 | // installed. |
| 3555 | inline bool IsInobjectSlackTrackingInProgress(); |
| 3556 | |
| 3557 | // Starts the tracking. |
| 3558 | // Stores the initial map and installs the countdown stub. |
| 3559 | // IsInobjectSlackTrackingInProgress is normally true after this call, |
| 3560 | // except when tracking have not been started (e.g. the map has no unused |
| 3561 | // properties or the snapshot is being built). |
| 3562 | void StartInobjectSlackTracking(Map* map); |
| 3563 | |
| 3564 | // Completes the tracking. |
| 3565 | // IsInobjectSlackTrackingInProgress is false after this call. |
| 3566 | void CompleteInobjectSlackTracking(); |
| 3567 | |
| 3568 | // Clears the initial_map before the GC marking phase to ensure the reference |
| 3569 | // is weak. IsInobjectSlackTrackingInProgress is false after this call. |
| 3570 | void DetachInitialMap(); |
| 3571 | |
| 3572 | // Restores the link to the initial map after the GC marking phase. |
| 3573 | // IsInobjectSlackTrackingInProgress is true after this call. |
| 3574 | void AttachInitialMap(Map* map); |
| 3575 | |
| 3576 | // False if there are definitely no live objects created from this function. |
| 3577 | // True if live objects _may_ exist (existence not guaranteed). |
| 3578 | // May go back from true to false after GC. |
| 3579 | inline bool live_objects_may_exist(); |
| 3580 | |
| 3581 | inline void set_live_objects_may_exist(bool value); |
| 3582 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3583 | // [instance class name]: class name for instances. |
| 3584 | DECL_ACCESSORS(instance_class_name, Object) |
| 3585 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3586 | // [function data]: This field holds some additional data for function. |
| 3587 | // Currently it either has FunctionTemplateInfo to make benefit the API |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 3588 | // or Smi identifying a custom call generator. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3589 | // In the long run we don't want all functions to have this field but |
| 3590 | // we can fix that when we have a better model for storing hidden data |
| 3591 | // on objects. |
| 3592 | DECL_ACCESSORS(function_data, Object) |
| 3593 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3594 | inline bool IsApiFunction(); |
| 3595 | inline FunctionTemplateInfo* get_api_func_data(); |
| 3596 | inline bool HasCustomCallGenerator(); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 3597 | inline int custom_call_generator_id(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3598 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3599 | // [script info]: Script from which the function originates. |
| 3600 | DECL_ACCESSORS(script, Object) |
| 3601 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3602 | // [num_literals]: Number of literals used by this function. |
| 3603 | inline int num_literals(); |
| 3604 | inline void set_num_literals(int value); |
| 3605 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3606 | // [start_position_and_type]: Field used to store both the source code |
| 3607 | // position, whether or not the function is a function expression, |
| 3608 | // and whether or not the function is a toplevel function. The two |
| 3609 | // least significants bit indicates whether the function is an |
| 3610 | // expression and the rest contains the source code position. |
| 3611 | inline int start_position_and_type(); |
| 3612 | inline void set_start_position_and_type(int value); |
| 3613 | |
| 3614 | // [debug info]: Debug information. |
| 3615 | DECL_ACCESSORS(debug_info, Object) |
| 3616 | |
| 3617 | // [inferred name]: Name inferred from variable or property |
| 3618 | // assignment of this function. Used to facilitate debugging and |
| 3619 | // profiling of JavaScript code written in OO style, where almost |
| 3620 | // all functions are anonymous but are assigned to object |
| 3621 | // properties. |
| 3622 | DECL_ACCESSORS(inferred_name, String) |
| 3623 | |
| 3624 | // Position of the 'function' token in the script source. |
| 3625 | inline int function_token_position(); |
| 3626 | inline void set_function_token_position(int function_token_position); |
| 3627 | |
| 3628 | // Position of this function in the script source. |
| 3629 | inline int start_position(); |
| 3630 | inline void set_start_position(int start_position); |
| 3631 | |
| 3632 | // End position of this function in the script source. |
| 3633 | inline int end_position(); |
| 3634 | inline void set_end_position(int end_position); |
| 3635 | |
| 3636 | // Is this function a function expression in the source code. |
| 3637 | inline bool is_expression(); |
| 3638 | inline void set_is_expression(bool value); |
| 3639 | |
| 3640 | // Is this function a top-level function (scripts, evals). |
| 3641 | inline bool is_toplevel(); |
| 3642 | inline void set_is_toplevel(bool value); |
| 3643 | |
| 3644 | // Bit field containing various information collected by the compiler to |
| 3645 | // drive optimization. |
| 3646 | inline int compiler_hints(); |
| 3647 | inline void set_compiler_hints(int value); |
| 3648 | |
| 3649 | // Add information on assignments of the form this.x = ...; |
| 3650 | void SetThisPropertyAssignmentsInfo( |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3651 | bool has_only_simple_this_property_assignments, |
| 3652 | FixedArray* this_property_assignments); |
| 3653 | |
| 3654 | // Clear information on assignments of the form this.x = ...; |
| 3655 | void ClearThisPropertyAssignmentsInfo(); |
| 3656 | |
| 3657 | // Indicate that this function only consists of assignments of the form |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3658 | // this.x = y; where y is either a constant or refers to an argument. |
| 3659 | inline bool has_only_simple_this_property_assignments(); |
| 3660 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3661 | inline bool try_full_codegen(); |
| 3662 | inline void set_try_full_codegen(bool flag); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3663 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3664 | // Indicates if this function can be lazy compiled. |
| 3665 | // This is used to determine if we can safely flush code from a function |
| 3666 | // when doing GC if we expect that the function will no longer be used. |
| 3667 | inline bool allows_lazy_compilation(); |
| 3668 | inline void set_allows_lazy_compilation(bool flag); |
| 3669 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3670 | // Indicates how many full GCs this function has survived with assigned |
| 3671 | // code object. Used to determine when it is relatively safe to flush |
| 3672 | // this code object and replace it with lazy compilation stub. |
| 3673 | // Age is reset when GC notices that the code object is referenced |
| 3674 | // from the stack or compilation cache. |
| 3675 | inline int code_age(); |
| 3676 | inline void set_code_age(int age); |
| 3677 | |
| 3678 | |
Andrei Popescu | 402d937 | 2010-02-26 13:31:12 +0000 | [diff] [blame] | 3679 | // Check whether a inlined constructor can be generated with the given |
| 3680 | // prototype. |
| 3681 | bool CanGenerateInlineConstructor(Object* prototype); |
| 3682 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3683 | // Prevents further attempts to generate inline constructors. |
| 3684 | // To be called if generation failed for any reason. |
| 3685 | void ForbidInlineConstructor(); |
| 3686 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3687 | // For functions which only contains this property assignments this provides |
| 3688 | // access to the names for the properties assigned. |
| 3689 | DECL_ACCESSORS(this_property_assignments, Object) |
| 3690 | inline int this_property_assignments_count(); |
| 3691 | inline void set_this_property_assignments_count(int value); |
| 3692 | String* GetThisPropertyAssignmentName(int index); |
| 3693 | bool IsThisPropertyAssignmentArgument(int index); |
| 3694 | int GetThisPropertyAssignmentArgument(int index); |
| 3695 | Object* GetThisPropertyAssignmentConstant(int index); |
| 3696 | |
| 3697 | // [source code]: Source code for the function. |
| 3698 | bool HasSourceCode(); |
| 3699 | Object* GetSourceCode(); |
| 3700 | |
| 3701 | // Calculate the instance size. |
| 3702 | int CalculateInstanceSize(); |
| 3703 | |
| 3704 | // Calculate the number of in-object properties. |
| 3705 | int CalculateInObjectProperties(); |
| 3706 | |
| 3707 | // Dispatched behavior. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3708 | // Set max_length to -1 for unlimited length. |
| 3709 | void SourceCodePrint(StringStream* accumulator, int max_length); |
| 3710 | #ifdef DEBUG |
| 3711 | void SharedFunctionInfoPrint(); |
| 3712 | void SharedFunctionInfoVerify(); |
| 3713 | #endif |
| 3714 | |
| 3715 | // Casting. |
| 3716 | static inline SharedFunctionInfo* cast(Object* obj); |
| 3717 | |
| 3718 | // Constants. |
| 3719 | static const int kDontAdaptArgumentsSentinel = -1; |
| 3720 | |
| 3721 | // Layout description. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3722 | // Pointer fields. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3723 | static const int kNameOffset = HeapObject::kHeaderSize; |
| 3724 | static const int kCodeOffset = kNameOffset + kPointerSize; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 3725 | static const int kScopeInfoOffset = kCodeOffset + kPointerSize; |
| 3726 | static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3727 | static const int kInstanceClassNameOffset = |
| 3728 | kConstructStubOffset + kPointerSize; |
| 3729 | static const int kFunctionDataOffset = |
| 3730 | kInstanceClassNameOffset + kPointerSize; |
| 3731 | static const int kScriptOffset = kFunctionDataOffset + kPointerSize; |
| 3732 | static const int kDebugInfoOffset = kScriptOffset + kPointerSize; |
| 3733 | static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3734 | static const int kInitialMapOffset = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3735 | kInferredNameOffset + kPointerSize; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3736 | static const int kThisPropertyAssignmentsOffset = |
| 3737 | kInitialMapOffset + kPointerSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3738 | #if V8_HOST_ARCH_32_BIT |
| 3739 | // Smi fields. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3740 | static const int kLengthOffset = |
| 3741 | kThisPropertyAssignmentsOffset + kPointerSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3742 | static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize; |
| 3743 | static const int kExpectedNofPropertiesOffset = |
| 3744 | kFormalParameterCountOffset + kPointerSize; |
| 3745 | static const int kNumLiteralsOffset = |
| 3746 | kExpectedNofPropertiesOffset + kPointerSize; |
| 3747 | static const int kStartPositionAndTypeOffset = |
| 3748 | kNumLiteralsOffset + kPointerSize; |
| 3749 | static const int kEndPositionOffset = |
| 3750 | kStartPositionAndTypeOffset + kPointerSize; |
| 3751 | static const int kFunctionTokenPositionOffset = |
| 3752 | kEndPositionOffset + kPointerSize; |
| 3753 | static const int kCompilerHintsOffset = |
| 3754 | kFunctionTokenPositionOffset + kPointerSize; |
| 3755 | static const int kThisPropertyAssignmentsCountOffset = |
| 3756 | kCompilerHintsOffset + kPointerSize; |
| 3757 | // Total size. |
| 3758 | static const int kSize = kThisPropertyAssignmentsCountOffset + kPointerSize; |
| 3759 | #else |
| 3760 | // The only reason to use smi fields instead of int fields |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3761 | // is to allow iteration without maps decoding during |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3762 | // garbage collections. |
| 3763 | // To avoid wasting space on 64-bit architectures we use |
| 3764 | // the following trick: we group integer fields into pairs |
| 3765 | // First integer in each pair is shifted left by 1. |
| 3766 | // By doing this we guarantee that LSB of each kPointerSize aligned |
| 3767 | // word is not set and thus this word cannot be treated as pointer |
| 3768 | // to HeapObject during old space traversal. |
| 3769 | static const int kLengthOffset = |
| 3770 | kThisPropertyAssignmentsOffset + kPointerSize; |
| 3771 | static const int kFormalParameterCountOffset = |
| 3772 | kLengthOffset + kIntSize; |
| 3773 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3774 | static const int kExpectedNofPropertiesOffset = |
| 3775 | kFormalParameterCountOffset + kIntSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3776 | static const int kNumLiteralsOffset = |
| 3777 | kExpectedNofPropertiesOffset + kIntSize; |
| 3778 | |
| 3779 | static const int kEndPositionOffset = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3780 | kNumLiteralsOffset + kIntSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3781 | static const int kStartPositionAndTypeOffset = |
| 3782 | kEndPositionOffset + kIntSize; |
| 3783 | |
| 3784 | static const int kFunctionTokenPositionOffset = |
| 3785 | kStartPositionAndTypeOffset + kIntSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3786 | static const int kCompilerHintsOffset = |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3787 | kFunctionTokenPositionOffset + kIntSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3788 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3789 | static const int kThisPropertyAssignmentsCountOffset = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3790 | kCompilerHintsOffset + kIntSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3791 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3792 | // Total size. |
| 3793 | static const int kSize = kThisPropertyAssignmentsCountOffset + kIntSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3794 | |
| 3795 | #endif |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3796 | |
| 3797 | // The construction counter for inobject slack tracking is stored in the |
| 3798 | // most significant byte of compiler_hints which is otherwise unused. |
| 3799 | // Its offset depends on the endian-ness of the architecture. |
| 3800 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 3801 | static const int kConstructionCountOffset = kCompilerHintsOffset + 3; |
| 3802 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 3803 | static const int kConstructionCountOffset = kCompilerHintsOffset + 0; |
| 3804 | #else |
| 3805 | #error Unknown byte ordering |
| 3806 | #endif |
| 3807 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3808 | static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3809 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3810 | typedef FixedBodyDescriptor<kNameOffset, |
| 3811 | kThisPropertyAssignmentsOffset + kPointerSize, |
| 3812 | kSize> BodyDescriptor; |
| 3813 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3814 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3815 | // Bit positions in start_position_and_type. |
| 3816 | // The source code start position is in the 30 most significant bits of |
| 3817 | // the start_position_and_type field. |
| 3818 | static const int kIsExpressionBit = 0; |
| 3819 | static const int kIsTopLevelBit = 1; |
| 3820 | static const int kStartPositionShift = 2; |
| 3821 | static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1); |
| 3822 | |
| 3823 | // Bit positions in compiler_hints. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 3824 | static const int kHasOnlySimpleThisPropertyAssignments = 0; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 3825 | static const int kTryFullCodegen = 1; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 3826 | static const int kAllowLazyCompilation = 2; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame^] | 3827 | static const int kLiveObjectsMayExist = 3; |
| 3828 | static const int kCodeAgeShift = 4; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3829 | static const int kCodeAgeMask = 7; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3830 | |
| 3831 | DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo); |
| 3832 | }; |
| 3833 | |
| 3834 | |
| 3835 | // JSFunction describes JavaScript functions. |
| 3836 | class JSFunction: public JSObject { |
| 3837 | public: |
| 3838 | // [prototype_or_initial_map]: |
| 3839 | DECL_ACCESSORS(prototype_or_initial_map, Object) |
| 3840 | |
| 3841 | // [shared_function_info]: The information about the function that |
| 3842 | // can be shared by instances. |
| 3843 | DECL_ACCESSORS(shared, SharedFunctionInfo) |
| 3844 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3845 | inline SharedFunctionInfo* unchecked_shared(); |
| 3846 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3847 | // [context]: The context for this function. |
| 3848 | inline Context* context(); |
| 3849 | inline Object* unchecked_context(); |
| 3850 | inline void set_context(Object* context); |
| 3851 | |
| 3852 | // [code]: The generated code object for this function. Executed |
| 3853 | // when the function is invoked, e.g. foo() or new foo(). See |
| 3854 | // [[Call]] and [[Construct]] description in ECMA-262, section |
| 3855 | // 8.6.2, page 27. |
| 3856 | inline Code* code(); |
| 3857 | inline void set_code(Code* value); |
| 3858 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3859 | inline Code* unchecked_code(); |
| 3860 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3861 | // Tells whether this function is builtin. |
| 3862 | inline bool IsBuiltin(); |
| 3863 | |
| 3864 | // [literals]: Fixed array holding the materialized literals. |
| 3865 | // |
| 3866 | // If the function contains object, regexp or array literals, the |
| 3867 | // literals array prefix contains the object, regexp, and array |
| 3868 | // function to be used when creating these literals. This is |
| 3869 | // necessary so that we do not dynamically lookup the object, regexp |
| 3870 | // or array functions. Performing a dynamic lookup, we might end up |
| 3871 | // using the functions from a new context that we should not have |
| 3872 | // access to. |
| 3873 | DECL_ACCESSORS(literals, FixedArray) |
| 3874 | |
| 3875 | // The initial map for an object created by this constructor. |
| 3876 | inline Map* initial_map(); |
| 3877 | inline void set_initial_map(Map* value); |
| 3878 | inline bool has_initial_map(); |
| 3879 | |
| 3880 | // Get and set the prototype property on a JSFunction. If the |
| 3881 | // function has an initial map the prototype is set on the initial |
| 3882 | // map. Otherwise, the prototype is put in the initial map field |
| 3883 | // until an initial map is needed. |
| 3884 | inline bool has_prototype(); |
| 3885 | inline bool has_instance_prototype(); |
| 3886 | inline Object* prototype(); |
| 3887 | inline Object* instance_prototype(); |
| 3888 | Object* SetInstancePrototype(Object* value); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 3889 | MUST_USE_RESULT Object* SetPrototype(Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3890 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 3891 | // After prototype is removed, it will not be created when accessed, and |
| 3892 | // [[Construct]] from this function will not be allowed. |
| 3893 | Object* RemovePrototype(); |
| 3894 | inline bool should_have_prototype(); |
| 3895 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3896 | // Accessor for this function's initial map's [[class]] |
| 3897 | // property. This is primarily used by ECMA native functions. This |
| 3898 | // method sets the class_name field of this function's initial map |
| 3899 | // to a given value. It creates an initial map if this function does |
| 3900 | // not have one. Note that this method does not copy the initial map |
| 3901 | // if it has one already, but simply replaces it with the new value. |
| 3902 | // Instances created afterwards will have a map whose [[class]] is |
| 3903 | // set to 'value', but there is no guarantees on instances created |
| 3904 | // before. |
| 3905 | Object* SetInstanceClassName(String* name); |
| 3906 | |
| 3907 | // Returns if this function has been compiled to native code yet. |
| 3908 | inline bool is_compiled(); |
| 3909 | |
| 3910 | // Casting. |
| 3911 | static inline JSFunction* cast(Object* obj); |
| 3912 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3913 | // Iterates the objects, including code objects indirectly referenced |
| 3914 | // through pointers to the first instruction in the code object. |
| 3915 | void JSFunctionIterateBody(int object_size, ObjectVisitor* v); |
| 3916 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3917 | // Dispatched behavior. |
| 3918 | #ifdef DEBUG |
| 3919 | void JSFunctionPrint(); |
| 3920 | void JSFunctionVerify(); |
| 3921 | #endif |
| 3922 | |
| 3923 | // Returns the number of allocated literals. |
| 3924 | inline int NumberOfLiterals(); |
| 3925 | |
| 3926 | // Retrieve the global context from a function's literal array. |
| 3927 | static Context* GlobalContextFromLiterals(FixedArray* literals); |
| 3928 | |
| 3929 | // Layout descriptors. |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3930 | static const int kCodeEntryOffset = JSObject::kHeaderSize; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 3931 | static const int kPrototypeOrInitialMapOffset = |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 3932 | kCodeEntryOffset + kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 3933 | static const int kSharedFunctionInfoOffset = |
| 3934 | kPrototypeOrInitialMapOffset + kPointerSize; |
| 3935 | static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize; |
| 3936 | static const int kLiteralsOffset = kContextOffset + kPointerSize; |
| 3937 | static const int kSize = kLiteralsOffset + kPointerSize; |
| 3938 | |
| 3939 | // Layout of the literals array. |
| 3940 | static const int kLiteralsPrefixSize = 1; |
| 3941 | static const int kLiteralGlobalContextIndex = 0; |
| 3942 | private: |
| 3943 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction); |
| 3944 | }; |
| 3945 | |
| 3946 | |
| 3947 | // JSGlobalProxy's prototype must be a JSGlobalObject or null, |
| 3948 | // and the prototype is hidden. JSGlobalProxy always delegates |
| 3949 | // property accesses to its prototype if the prototype is not null. |
| 3950 | // |
| 3951 | // A JSGlobalProxy can be reinitialized which will preserve its identity. |
| 3952 | // |
| 3953 | // Accessing a JSGlobalProxy requires security check. |
| 3954 | |
| 3955 | class JSGlobalProxy : public JSObject { |
| 3956 | public: |
| 3957 | // [context]: the owner global context of this proxy object. |
| 3958 | // It is null value if this object is not used by any context. |
| 3959 | DECL_ACCESSORS(context, Object) |
| 3960 | |
| 3961 | // Casting. |
| 3962 | static inline JSGlobalProxy* cast(Object* obj); |
| 3963 | |
| 3964 | // Dispatched behavior. |
| 3965 | #ifdef DEBUG |
| 3966 | void JSGlobalProxyPrint(); |
| 3967 | void JSGlobalProxyVerify(); |
| 3968 | #endif |
| 3969 | |
| 3970 | // Layout description. |
| 3971 | static const int kContextOffset = JSObject::kHeaderSize; |
| 3972 | static const int kSize = kContextOffset + kPointerSize; |
| 3973 | |
| 3974 | private: |
| 3975 | |
| 3976 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy); |
| 3977 | }; |
| 3978 | |
| 3979 | |
| 3980 | // Forward declaration. |
| 3981 | class JSBuiltinsObject; |
| 3982 | |
| 3983 | // Common super class for JavaScript global objects and the special |
| 3984 | // builtins global objects. |
| 3985 | class GlobalObject: public JSObject { |
| 3986 | public: |
| 3987 | // [builtins]: the object holding the runtime routines written in JS. |
| 3988 | DECL_ACCESSORS(builtins, JSBuiltinsObject) |
| 3989 | |
| 3990 | // [global context]: the global context corresponding to this global object. |
| 3991 | DECL_ACCESSORS(global_context, Context) |
| 3992 | |
| 3993 | // [global receiver]: the global receiver object of the context |
| 3994 | DECL_ACCESSORS(global_receiver, JSObject) |
| 3995 | |
| 3996 | // Retrieve the property cell used to store a property. |
| 3997 | Object* GetPropertyCell(LookupResult* result); |
| 3998 | |
| 3999 | // Ensure that the global object has a cell for the given property name. |
| 4000 | Object* EnsurePropertyCell(String* name); |
| 4001 | |
| 4002 | // Casting. |
| 4003 | static inline GlobalObject* cast(Object* obj); |
| 4004 | |
| 4005 | // Layout description. |
| 4006 | static const int kBuiltinsOffset = JSObject::kHeaderSize; |
| 4007 | static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize; |
| 4008 | static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize; |
| 4009 | static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize; |
| 4010 | |
| 4011 | private: |
| 4012 | friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs; |
| 4013 | |
| 4014 | DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject); |
| 4015 | }; |
| 4016 | |
| 4017 | |
| 4018 | // JavaScript global object. |
| 4019 | class JSGlobalObject: public GlobalObject { |
| 4020 | public: |
| 4021 | |
| 4022 | // Casting. |
| 4023 | static inline JSGlobalObject* cast(Object* obj); |
| 4024 | |
| 4025 | // Dispatched behavior. |
| 4026 | #ifdef DEBUG |
| 4027 | void JSGlobalObjectPrint(); |
| 4028 | void JSGlobalObjectVerify(); |
| 4029 | #endif |
| 4030 | |
| 4031 | // Layout description. |
| 4032 | static const int kSize = GlobalObject::kHeaderSize; |
| 4033 | |
| 4034 | private: |
| 4035 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject); |
| 4036 | }; |
| 4037 | |
| 4038 | |
| 4039 | // Builtins global object which holds the runtime routines written in |
| 4040 | // JavaScript. |
| 4041 | class JSBuiltinsObject: public GlobalObject { |
| 4042 | public: |
| 4043 | // Accessors for the runtime routines written in JavaScript. |
| 4044 | inline Object* javascript_builtin(Builtins::JavaScript id); |
| 4045 | inline void set_javascript_builtin(Builtins::JavaScript id, Object* value); |
| 4046 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4047 | // Accessors for code of the runtime routines written in JavaScript. |
| 4048 | inline Code* javascript_builtin_code(Builtins::JavaScript id); |
| 4049 | inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value); |
| 4050 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4051 | // Casting. |
| 4052 | static inline JSBuiltinsObject* cast(Object* obj); |
| 4053 | |
| 4054 | // Dispatched behavior. |
| 4055 | #ifdef DEBUG |
| 4056 | void JSBuiltinsObjectPrint(); |
| 4057 | void JSBuiltinsObjectVerify(); |
| 4058 | #endif |
| 4059 | |
| 4060 | // Layout description. The size of the builtins object includes |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4061 | // room for two pointers per runtime routine written in javascript |
| 4062 | // (function and code object). |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4063 | static const int kJSBuiltinsCount = Builtins::id_count; |
| 4064 | static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4065 | static const int kJSBuiltinsCodeOffset = |
| 4066 | GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4067 | static const int kSize = |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4068 | kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize); |
| 4069 | |
| 4070 | static int OffsetOfFunctionWithId(Builtins::JavaScript id) { |
| 4071 | return kJSBuiltinsOffset + id * kPointerSize; |
| 4072 | } |
| 4073 | |
| 4074 | static int OffsetOfCodeWithId(Builtins::JavaScript id) { |
| 4075 | return kJSBuiltinsCodeOffset + id * kPointerSize; |
| 4076 | } |
| 4077 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4078 | private: |
| 4079 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject); |
| 4080 | }; |
| 4081 | |
| 4082 | |
| 4083 | // Representation for JS Wrapper objects, String, Number, Boolean, Date, etc. |
| 4084 | class JSValue: public JSObject { |
| 4085 | public: |
| 4086 | // [value]: the object being wrapped. |
| 4087 | DECL_ACCESSORS(value, Object) |
| 4088 | |
| 4089 | // Casting. |
| 4090 | static inline JSValue* cast(Object* obj); |
| 4091 | |
| 4092 | // Dispatched behavior. |
| 4093 | #ifdef DEBUG |
| 4094 | void JSValuePrint(); |
| 4095 | void JSValueVerify(); |
| 4096 | #endif |
| 4097 | |
| 4098 | // Layout description. |
| 4099 | static const int kValueOffset = JSObject::kHeaderSize; |
| 4100 | static const int kSize = kValueOffset + kPointerSize; |
| 4101 | |
| 4102 | private: |
| 4103 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue); |
| 4104 | }; |
| 4105 | |
| 4106 | // Regular expressions |
| 4107 | // The regular expression holds a single reference to a FixedArray in |
| 4108 | // the kDataOffset field. |
| 4109 | // The FixedArray contains the following data: |
| 4110 | // - tag : type of regexp implementation (not compiled yet, atom or irregexp) |
| 4111 | // - reference to the original source string |
| 4112 | // - reference to the original flag string |
| 4113 | // If it is an atom regexp |
| 4114 | // - a reference to a literal string to search for |
| 4115 | // If it is an irregexp regexp: |
| 4116 | // - a reference to code for ASCII inputs (bytecode or compiled). |
| 4117 | // - a reference to code for UC16 inputs (bytecode or compiled). |
| 4118 | // - max number of registers used by irregexp implementations. |
| 4119 | // - number of capture registers (output values) of the regexp. |
| 4120 | class JSRegExp: public JSObject { |
| 4121 | public: |
| 4122 | // Meaning of Type: |
| 4123 | // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet. |
| 4124 | // ATOM: A simple string to match against using an indexOf operation. |
| 4125 | // IRREGEXP: Compiled with Irregexp. |
| 4126 | // IRREGEXP_NATIVE: Compiled to native code with Irregexp. |
| 4127 | enum Type { NOT_COMPILED, ATOM, IRREGEXP }; |
| 4128 | enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 }; |
| 4129 | |
| 4130 | class Flags { |
| 4131 | public: |
| 4132 | explicit Flags(uint32_t value) : value_(value) { } |
| 4133 | bool is_global() { return (value_ & GLOBAL) != 0; } |
| 4134 | bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; } |
| 4135 | bool is_multiline() { return (value_ & MULTILINE) != 0; } |
| 4136 | uint32_t value() { return value_; } |
| 4137 | private: |
| 4138 | uint32_t value_; |
| 4139 | }; |
| 4140 | |
| 4141 | DECL_ACCESSORS(data, Object) |
| 4142 | |
| 4143 | inline Type TypeTag(); |
| 4144 | inline int CaptureCount(); |
| 4145 | inline Flags GetFlags(); |
| 4146 | inline String* Pattern(); |
| 4147 | inline Object* DataAt(int index); |
| 4148 | // Set implementation data after the object has been prepared. |
| 4149 | inline void SetDataAt(int index, Object* value); |
| 4150 | static int code_index(bool is_ascii) { |
| 4151 | if (is_ascii) { |
| 4152 | return kIrregexpASCIICodeIndex; |
| 4153 | } else { |
| 4154 | return kIrregexpUC16CodeIndex; |
| 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | static inline JSRegExp* cast(Object* obj); |
| 4159 | |
| 4160 | // Dispatched behavior. |
| 4161 | #ifdef DEBUG |
| 4162 | void JSRegExpVerify(); |
| 4163 | #endif |
| 4164 | |
| 4165 | static const int kDataOffset = JSObject::kHeaderSize; |
| 4166 | static const int kSize = kDataOffset + kPointerSize; |
| 4167 | |
| 4168 | // Indices in the data array. |
| 4169 | static const int kTagIndex = 0; |
| 4170 | static const int kSourceIndex = kTagIndex + 1; |
| 4171 | static const int kFlagsIndex = kSourceIndex + 1; |
| 4172 | static const int kDataIndex = kFlagsIndex + 1; |
| 4173 | // The data fields are used in different ways depending on the |
| 4174 | // value of the tag. |
| 4175 | // Atom regexps (literal strings). |
| 4176 | static const int kAtomPatternIndex = kDataIndex; |
| 4177 | |
| 4178 | static const int kAtomDataSize = kAtomPatternIndex + 1; |
| 4179 | |
| 4180 | // Irregexp compiled code or bytecode for ASCII. If compilation |
| 4181 | // fails, this fields hold an exception object that should be |
| 4182 | // thrown if the regexp is used again. |
| 4183 | static const int kIrregexpASCIICodeIndex = kDataIndex; |
| 4184 | // Irregexp compiled code or bytecode for UC16. If compilation |
| 4185 | // fails, this fields hold an exception object that should be |
| 4186 | // thrown if the regexp is used again. |
| 4187 | static const int kIrregexpUC16CodeIndex = kDataIndex + 1; |
| 4188 | // Maximal number of registers used by either ASCII or UC16. |
| 4189 | // Only used to check that there is enough stack space |
| 4190 | static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2; |
| 4191 | // Number of captures in the compiled regexp. |
| 4192 | static const int kIrregexpCaptureCountIndex = kDataIndex + 3; |
| 4193 | |
| 4194 | static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4195 | |
| 4196 | // Offsets directly into the data fixed array. |
| 4197 | static const int kDataTagOffset = |
| 4198 | FixedArray::kHeaderSize + kTagIndex * kPointerSize; |
| 4199 | static const int kDataAsciiCodeOffset = |
| 4200 | FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 4201 | static const int kDataUC16CodeOffset = |
| 4202 | FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4203 | static const int kIrregexpCaptureCountOffset = |
| 4204 | FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4205 | |
| 4206 | // In-object fields. |
| 4207 | static const int kSourceFieldIndex = 0; |
| 4208 | static const int kGlobalFieldIndex = 1; |
| 4209 | static const int kIgnoreCaseFieldIndex = 2; |
| 4210 | static const int kMultilineFieldIndex = 3; |
| 4211 | static const int kLastIndexFieldIndex = 4; |
Ben Murdoch | bb769b2 | 2010-08-11 14:56:33 +0100 | [diff] [blame] | 4212 | static const int kInObjectFieldCount = 5; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4213 | }; |
| 4214 | |
| 4215 | |
| 4216 | class CompilationCacheShape { |
| 4217 | public: |
| 4218 | static inline bool IsMatch(HashTableKey* key, Object* value) { |
| 4219 | return key->IsMatch(value); |
| 4220 | } |
| 4221 | |
| 4222 | static inline uint32_t Hash(HashTableKey* key) { |
| 4223 | return key->Hash(); |
| 4224 | } |
| 4225 | |
| 4226 | static inline uint32_t HashForObject(HashTableKey* key, Object* object) { |
| 4227 | return key->HashForObject(object); |
| 4228 | } |
| 4229 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4230 | MUST_USE_RESULT static Object* AsObject(HashTableKey* key) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4231 | return key->AsObject(); |
| 4232 | } |
| 4233 | |
| 4234 | static const int kPrefixSize = 0; |
| 4235 | static const int kEntrySize = 2; |
| 4236 | }; |
| 4237 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 4238 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4239 | class CompilationCacheTable: public HashTable<CompilationCacheShape, |
| 4240 | HashTableKey*> { |
| 4241 | public: |
| 4242 | // Find cached value for a string key, otherwise return null. |
| 4243 | Object* Lookup(String* src); |
| 4244 | Object* LookupEval(String* src, Context* context); |
| 4245 | Object* LookupRegExp(String* source, JSRegExp::Flags flags); |
| 4246 | Object* Put(String* src, Object* value); |
| 4247 | Object* PutEval(String* src, Context* context, Object* value); |
| 4248 | Object* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value); |
| 4249 | |
| 4250 | static inline CompilationCacheTable* cast(Object* obj); |
| 4251 | |
| 4252 | private: |
| 4253 | DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable); |
| 4254 | }; |
| 4255 | |
| 4256 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4257 | class CodeCache: public Struct { |
| 4258 | public: |
| 4259 | DECL_ACCESSORS(default_cache, FixedArray) |
| 4260 | DECL_ACCESSORS(normal_type_cache, Object) |
| 4261 | |
| 4262 | // Add the code object to the cache. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4263 | MUST_USE_RESULT Object* Update(String* name, Code* code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4264 | |
| 4265 | // Lookup code object in the cache. Returns code object if found and undefined |
| 4266 | // if not. |
| 4267 | Object* Lookup(String* name, Code::Flags flags); |
| 4268 | |
| 4269 | // Get the internal index of a code object in the cache. Returns -1 if the |
| 4270 | // code object is not in that cache. This index can be used to later call |
| 4271 | // RemoveByIndex. The cache cannot be modified between a call to GetIndex and |
| 4272 | // RemoveByIndex. |
| 4273 | int GetIndex(Object* name, Code* code); |
| 4274 | |
| 4275 | // Remove an object from the cache with the provided internal index. |
| 4276 | void RemoveByIndex(Object* name, Code* code, int index); |
| 4277 | |
| 4278 | static inline CodeCache* cast(Object* obj); |
| 4279 | |
| 4280 | #ifdef DEBUG |
| 4281 | void CodeCachePrint(); |
| 4282 | void CodeCacheVerify(); |
| 4283 | #endif |
| 4284 | |
| 4285 | static const int kDefaultCacheOffset = HeapObject::kHeaderSize; |
| 4286 | static const int kNormalTypeCacheOffset = |
| 4287 | kDefaultCacheOffset + kPointerSize; |
| 4288 | static const int kSize = kNormalTypeCacheOffset + kPointerSize; |
| 4289 | |
| 4290 | private: |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4291 | MUST_USE_RESULT Object* UpdateDefaultCache(String* name, Code* code); |
| 4292 | MUST_USE_RESULT Object* UpdateNormalTypeCache(String* name, Code* code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4293 | Object* LookupDefaultCache(String* name, Code::Flags flags); |
| 4294 | Object* LookupNormalTypeCache(String* name, Code::Flags flags); |
| 4295 | |
| 4296 | // Code cache layout of the default cache. Elements are alternating name and |
| 4297 | // code objects for non normal load/store/call IC's. |
| 4298 | static const int kCodeCacheEntrySize = 2; |
| 4299 | static const int kCodeCacheEntryNameOffset = 0; |
| 4300 | static const int kCodeCacheEntryCodeOffset = 1; |
| 4301 | |
| 4302 | DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache); |
| 4303 | }; |
| 4304 | |
| 4305 | |
| 4306 | class CodeCacheHashTableShape { |
| 4307 | public: |
| 4308 | static inline bool IsMatch(HashTableKey* key, Object* value) { |
| 4309 | return key->IsMatch(value); |
| 4310 | } |
| 4311 | |
| 4312 | static inline uint32_t Hash(HashTableKey* key) { |
| 4313 | return key->Hash(); |
| 4314 | } |
| 4315 | |
| 4316 | static inline uint32_t HashForObject(HashTableKey* key, Object* object) { |
| 4317 | return key->HashForObject(object); |
| 4318 | } |
| 4319 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4320 | MUST_USE_RESULT static Object* AsObject(HashTableKey* key) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4321 | return key->AsObject(); |
| 4322 | } |
| 4323 | |
| 4324 | static const int kPrefixSize = 0; |
| 4325 | static const int kEntrySize = 2; |
| 4326 | }; |
| 4327 | |
| 4328 | |
| 4329 | class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape, |
| 4330 | HashTableKey*> { |
| 4331 | public: |
| 4332 | Object* Lookup(String* name, Code::Flags flags); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4333 | MUST_USE_RESULT Object* Put(String* name, Code* code); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4334 | |
| 4335 | int GetIndex(String* name, Code::Flags flags); |
| 4336 | void RemoveByIndex(int index); |
| 4337 | |
| 4338 | static inline CodeCacheHashTable* cast(Object* obj); |
| 4339 | |
| 4340 | // Initial size of the fixed array backing the hash table. |
| 4341 | static const int kInitialSize = 64; |
| 4342 | |
| 4343 | private: |
| 4344 | DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable); |
| 4345 | }; |
| 4346 | |
| 4347 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4348 | enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS}; |
| 4349 | enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL}; |
| 4350 | |
| 4351 | |
| 4352 | class StringHasher { |
| 4353 | public: |
| 4354 | inline StringHasher(int length); |
| 4355 | |
| 4356 | // Returns true if the hash of this string can be computed without |
| 4357 | // looking at the contents. |
| 4358 | inline bool has_trivial_hash(); |
| 4359 | |
| 4360 | // Add a character to the hash and update the array index calculation. |
| 4361 | inline void AddCharacter(uc32 c); |
| 4362 | |
| 4363 | // Adds a character to the hash but does not update the array index |
| 4364 | // calculation. This can only be called when it has been verified |
| 4365 | // that the input is not an array index. |
| 4366 | inline void AddCharacterNoIndex(uc32 c); |
| 4367 | |
| 4368 | // Returns the value to store in the hash field of a string with |
| 4369 | // the given length and contents. |
| 4370 | uint32_t GetHashField(); |
| 4371 | |
| 4372 | // Returns true if the characters seen so far make up a legal array |
| 4373 | // index. |
| 4374 | bool is_array_index() { return is_array_index_; } |
| 4375 | |
| 4376 | bool is_valid() { return is_valid_; } |
| 4377 | |
| 4378 | void invalidate() { is_valid_ = false; } |
| 4379 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4380 | // Calculated hash value for a string consisting of 1 to |
| 4381 | // String::kMaxArrayIndexSize digits with no leading zeros (except "0"). |
| 4382 | // value is represented decimal value. |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 4383 | static uint32_t MakeArrayIndexHash(uint32_t value, int length); |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 4384 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4385 | private: |
| 4386 | |
| 4387 | uint32_t array_index() { |
| 4388 | ASSERT(is_array_index()); |
| 4389 | return array_index_; |
| 4390 | } |
| 4391 | |
| 4392 | inline uint32_t GetHash(); |
| 4393 | |
| 4394 | int length_; |
| 4395 | uint32_t raw_running_hash_; |
| 4396 | uint32_t array_index_; |
| 4397 | bool is_array_index_; |
| 4398 | bool is_first_char_; |
| 4399 | bool is_valid_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4400 | friend class TwoCharHashTableKey; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4401 | }; |
| 4402 | |
| 4403 | |
| 4404 | // The characteristics of a string are stored in its map. Retrieving these |
| 4405 | // few bits of information is moderately expensive, involving two memory |
| 4406 | // loads where the second is dependent on the first. To improve efficiency |
| 4407 | // the shape of the string is given its own class so that it can be retrieved |
| 4408 | // once and used for several string operations. A StringShape is small enough |
| 4409 | // to be passed by value and is immutable, but be aware that flattening a |
| 4410 | // string can potentially alter its shape. Also be aware that a GC caused by |
| 4411 | // something else can alter the shape of a string due to ConsString |
| 4412 | // shortcutting. Keeping these restrictions in mind has proven to be error- |
| 4413 | // prone and so we no longer put StringShapes in variables unless there is a |
| 4414 | // concrete performance benefit at that particular point in the code. |
| 4415 | class StringShape BASE_EMBEDDED { |
| 4416 | public: |
| 4417 | inline explicit StringShape(String* s); |
| 4418 | inline explicit StringShape(Map* s); |
| 4419 | inline explicit StringShape(InstanceType t); |
| 4420 | inline bool IsSequential(); |
| 4421 | inline bool IsExternal(); |
| 4422 | inline bool IsCons(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4423 | inline bool IsExternalAscii(); |
| 4424 | inline bool IsExternalTwoByte(); |
| 4425 | inline bool IsSequentialAscii(); |
| 4426 | inline bool IsSequentialTwoByte(); |
| 4427 | inline bool IsSymbol(); |
| 4428 | inline StringRepresentationTag representation_tag(); |
| 4429 | inline uint32_t full_representation_tag(); |
| 4430 | inline uint32_t size_tag(); |
| 4431 | #ifdef DEBUG |
| 4432 | inline uint32_t type() { return type_; } |
| 4433 | inline void invalidate() { valid_ = false; } |
| 4434 | inline bool valid() { return valid_; } |
| 4435 | #else |
| 4436 | inline void invalidate() { } |
| 4437 | #endif |
| 4438 | private: |
| 4439 | uint32_t type_; |
| 4440 | #ifdef DEBUG |
| 4441 | inline void set_valid() { valid_ = true; } |
| 4442 | bool valid_; |
| 4443 | #else |
| 4444 | inline void set_valid() { } |
| 4445 | #endif |
| 4446 | }; |
| 4447 | |
| 4448 | |
| 4449 | // The String abstract class captures JavaScript string values: |
| 4450 | // |
| 4451 | // Ecma-262: |
| 4452 | // 4.3.16 String Value |
| 4453 | // A string value is a member of the type String and is a finite |
| 4454 | // ordered sequence of zero or more 16-bit unsigned integer values. |
| 4455 | // |
| 4456 | // All string values have a length field. |
| 4457 | class String: public HeapObject { |
| 4458 | public: |
| 4459 | // Get and set the length of the string. |
| 4460 | inline int length(); |
| 4461 | inline void set_length(int value); |
| 4462 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4463 | // Get and set the hash field of the string. |
| 4464 | inline uint32_t hash_field(); |
| 4465 | inline void set_hash_field(uint32_t value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4466 | |
| 4467 | inline bool IsAsciiRepresentation(); |
| 4468 | inline bool IsTwoByteRepresentation(); |
| 4469 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4470 | // Returns whether this string has ascii chars, i.e. all of them can |
| 4471 | // be ascii encoded. This might be the case even if the string is |
| 4472 | // two-byte. Such strings may appear when the embedder prefers |
| 4473 | // two-byte external representations even for ascii data. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4474 | // |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 4475 | // NOTE: this should be considered only a hint. False negatives are |
| 4476 | // possible. |
| 4477 | inline bool HasOnlyAsciiChars(); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4478 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4479 | // Get and set individual two byte chars in the string. |
| 4480 | inline void Set(int index, uint16_t value); |
| 4481 | // Get individual two byte char in the string. Repeated calls |
| 4482 | // to this method are not efficient unless the string is flat. |
| 4483 | inline uint16_t Get(int index); |
| 4484 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4485 | // Try to flatten the string. Checks first inline to see if it is |
| 4486 | // necessary. Does nothing if the string is not a cons string. |
| 4487 | // Flattening allocates a sequential string with the same data as |
| 4488 | // the given string and mutates the cons string to a degenerate |
| 4489 | // form, where the first component is the new sequential string and |
| 4490 | // the second component is the empty string. If allocation fails, |
| 4491 | // this function returns a failure. If flattening succeeds, this |
| 4492 | // function returns the sequential string that is now the first |
| 4493 | // component of the cons string. |
| 4494 | // |
| 4495 | // Degenerate cons strings are handled specially by the garbage |
| 4496 | // collector (see IsShortcutCandidate). |
| 4497 | // |
| 4498 | // Use FlattenString from Handles.cc to flatten even in case an |
| 4499 | // allocation failure happens. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4500 | inline Object* TryFlatten(PretenureFlag pretenure = NOT_TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4501 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4502 | // Convenience function. Has exactly the same behavior as |
| 4503 | // TryFlatten(), except in the case of failure returns the original |
| 4504 | // string. |
| 4505 | inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED); |
| 4506 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4507 | Vector<const char> ToAsciiVector(); |
| 4508 | Vector<const uc16> ToUC16Vector(); |
| 4509 | |
| 4510 | // Mark the string as an undetectable object. It only applies to |
| 4511 | // ascii and two byte string types. |
| 4512 | bool MarkAsUndetectable(); |
| 4513 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4514 | // Return a substring. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4515 | Object* SubString(int from, int to, PretenureFlag pretenure = NOT_TENURED); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4516 | |
| 4517 | // String equality operations. |
| 4518 | inline bool Equals(String* other); |
| 4519 | bool IsEqualTo(Vector<const char> str); |
| 4520 | |
| 4521 | // Return a UTF8 representation of the string. The string is null |
| 4522 | // terminated but may optionally contain nulls. Length is returned |
| 4523 | // in length_output if length_output is not a null pointer The string |
| 4524 | // should be nearly flat, otherwise the performance of this method may |
| 4525 | // be very slow (quadratic in the length). Setting robustness_flag to |
| 4526 | // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it |
| 4527 | // handles unexpected data without causing assert failures and it does not |
| 4528 | // do any heap allocations. This is useful when printing stack traces. |
| 4529 | SmartPointer<char> ToCString(AllowNullsFlag allow_nulls, |
| 4530 | RobustnessFlag robustness_flag, |
| 4531 | int offset, |
| 4532 | int length, |
| 4533 | int* length_output = 0); |
| 4534 | SmartPointer<char> ToCString( |
| 4535 | AllowNullsFlag allow_nulls = DISALLOW_NULLS, |
| 4536 | RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL, |
| 4537 | int* length_output = 0); |
| 4538 | |
| 4539 | int Utf8Length(); |
| 4540 | |
| 4541 | // Return a 16 bit Unicode representation of the string. |
| 4542 | // The string should be nearly flat, otherwise the performance of |
| 4543 | // of this method may be very bad. Setting robustness_flag to |
| 4544 | // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it |
| 4545 | // handles unexpected data without causing assert failures and it does not |
| 4546 | // do any heap allocations. This is useful when printing stack traces. |
| 4547 | SmartPointer<uc16> ToWideCString( |
| 4548 | RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL); |
| 4549 | |
| 4550 | // Tells whether the hash code has been computed. |
| 4551 | inline bool HasHashCode(); |
| 4552 | |
| 4553 | // Returns a hash value used for the property table |
| 4554 | inline uint32_t Hash(); |
| 4555 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4556 | static uint32_t ComputeHashField(unibrow::CharacterStream* buffer, |
| 4557 | int length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4558 | |
| 4559 | static bool ComputeArrayIndex(unibrow::CharacterStream* buffer, |
| 4560 | uint32_t* index, |
| 4561 | int length); |
| 4562 | |
| 4563 | // Externalization. |
| 4564 | bool MakeExternal(v8::String::ExternalStringResource* resource); |
| 4565 | bool MakeExternal(v8::String::ExternalAsciiStringResource* resource); |
| 4566 | |
| 4567 | // Conversion. |
| 4568 | inline bool AsArrayIndex(uint32_t* index); |
| 4569 | |
| 4570 | // Casting. |
| 4571 | static inline String* cast(Object* obj); |
| 4572 | |
| 4573 | void PrintOn(FILE* out); |
| 4574 | |
| 4575 | // For use during stack traces. Performs rudimentary sanity check. |
| 4576 | bool LooksValid(); |
| 4577 | |
| 4578 | // Dispatched behavior. |
| 4579 | void StringShortPrint(StringStream* accumulator); |
| 4580 | #ifdef DEBUG |
| 4581 | void StringPrint(); |
| 4582 | void StringVerify(); |
| 4583 | #endif |
| 4584 | inline bool IsFlat(); |
| 4585 | |
| 4586 | // Layout description. |
| 4587 | static const int kLengthOffset = HeapObject::kHeaderSize; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 4588 | static const int kHashFieldOffset = kLengthOffset + kPointerSize; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4589 | static const int kSize = kHashFieldOffset + kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4590 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4591 | // Maximum number of characters to consider when trying to convert a string |
| 4592 | // value into an array index. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4593 | static const int kMaxArrayIndexSize = 10; |
| 4594 | |
| 4595 | // Max ascii char code. |
| 4596 | static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar; |
| 4597 | static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar; |
| 4598 | static const int kMaxUC16CharCode = 0xffff; |
| 4599 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4600 | // Minimum length for a cons string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4601 | static const int kMinNonFlatLength = 13; |
| 4602 | |
| 4603 | // Mask constant for checking if a string has a computed hash code |
| 4604 | // and if it is an array index. The least significant bit indicates |
| 4605 | // whether a hash code has been computed. If the hash code has been |
| 4606 | // computed the 2nd bit tells whether the string can be used as an |
| 4607 | // array index. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4608 | static const int kHashNotComputedMask = 1; |
| 4609 | static const int kIsNotArrayIndexMask = 1 << 1; |
| 4610 | static const int kNofHashBitFields = 2; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4611 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4612 | // Shift constant retrieving hash code from hash field. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4613 | static const int kHashShift = kNofHashBitFields; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4614 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4615 | // Array index strings this short can keep their index in the hash |
| 4616 | // field. |
| 4617 | static const int kMaxCachedArrayIndexLength = 7; |
| 4618 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4619 | // For strings which are array indexes the hash value has the string length |
| 4620 | // mixed into the hash, mainly to avoid a hash value of zero which would be |
| 4621 | // the case for the string '0'. 24 bits are used for the array index value. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4622 | static const int kArrayIndexValueBits = 24; |
| 4623 | static const int kArrayIndexLengthBits = |
| 4624 | kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields; |
| 4625 | |
| 4626 | STATIC_CHECK((kArrayIndexLengthBits > 0)); |
Iain Merrick | 9ac36c9 | 2010-09-13 15:29:50 +0100 | [diff] [blame] | 4627 | STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits)); |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4628 | |
| 4629 | static const int kArrayIndexHashLengthShift = |
| 4630 | kArrayIndexValueBits + kNofHashBitFields; |
| 4631 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4632 | static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1; |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4633 | |
| 4634 | static const int kArrayIndexValueMask = |
| 4635 | ((1 << kArrayIndexValueBits) - 1) << kHashShift; |
| 4636 | |
| 4637 | // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we |
| 4638 | // could use a mask to test if the length of string is less than or equal to |
| 4639 | // kMaxCachedArrayIndexLength. |
| 4640 | STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1)); |
| 4641 | |
| 4642 | static const int kContainsCachedArrayIndexMask = |
| 4643 | (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) | |
| 4644 | kIsNotArrayIndexMask; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4645 | |
| 4646 | // Value of empty hash field indicating that the hash is not computed. |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4647 | static const int kEmptyHashField = |
| 4648 | kIsNotArrayIndexMask | kHashNotComputedMask; |
| 4649 | |
| 4650 | // Value of hash field containing computed hash equal to zero. |
| 4651 | static const int kZeroHash = kIsNotArrayIndexMask; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4652 | |
| 4653 | // Maximal string length. |
| 4654 | static const int kMaxLength = (1 << (32 - 2)) - 1; |
| 4655 | |
| 4656 | // Max length for computing hash. For strings longer than this limit the |
| 4657 | // string length is used as the hash value. |
| 4658 | static const int kMaxHashCalcLength = 16383; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4659 | |
| 4660 | // Limit for truncation in short printing. |
| 4661 | static const int kMaxShortPrintLength = 1024; |
| 4662 | |
| 4663 | // Support for regular expressions. |
| 4664 | const uc16* GetTwoByteData(); |
| 4665 | const uc16* GetTwoByteData(unsigned start); |
| 4666 | |
| 4667 | // Support for StringInputBuffer |
| 4668 | static const unibrow::byte* ReadBlock(String* input, |
| 4669 | unibrow::byte* util_buffer, |
| 4670 | unsigned capacity, |
| 4671 | unsigned* remaining, |
| 4672 | unsigned* offset); |
| 4673 | static const unibrow::byte* ReadBlock(String** input, |
| 4674 | unibrow::byte* util_buffer, |
| 4675 | unsigned capacity, |
| 4676 | unsigned* remaining, |
| 4677 | unsigned* offset); |
| 4678 | |
| 4679 | // Helper function for flattening strings. |
| 4680 | template <typename sinkchar> |
| 4681 | static void WriteToFlat(String* source, |
| 4682 | sinkchar* sink, |
| 4683 | int from, |
| 4684 | int to); |
| 4685 | |
| 4686 | protected: |
| 4687 | class ReadBlockBuffer { |
| 4688 | public: |
| 4689 | ReadBlockBuffer(unibrow::byte* util_buffer_, |
| 4690 | unsigned cursor_, |
| 4691 | unsigned capacity_, |
| 4692 | unsigned remaining_) : |
| 4693 | util_buffer(util_buffer_), |
| 4694 | cursor(cursor_), |
| 4695 | capacity(capacity_), |
| 4696 | remaining(remaining_) { |
| 4697 | } |
| 4698 | unibrow::byte* util_buffer; |
| 4699 | unsigned cursor; |
| 4700 | unsigned capacity; |
| 4701 | unsigned remaining; |
| 4702 | }; |
| 4703 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4704 | static inline const unibrow::byte* ReadBlock(String* input, |
| 4705 | ReadBlockBuffer* buffer, |
| 4706 | unsigned* offset, |
| 4707 | unsigned max_chars); |
| 4708 | static void ReadBlockIntoBuffer(String* input, |
| 4709 | ReadBlockBuffer* buffer, |
| 4710 | unsigned* offset_ptr, |
| 4711 | unsigned max_chars); |
| 4712 | |
| 4713 | private: |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 4714 | // Try to flatten the top level ConsString that is hiding behind this |
| 4715 | // string. This is a no-op unless the string is a ConsString. Flatten |
| 4716 | // mutates the ConsString and might return a failure. |
| 4717 | Object* SlowTryFlatten(PretenureFlag pretenure); |
| 4718 | |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4719 | static inline bool IsHashFieldComputed(uint32_t field); |
| 4720 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4721 | // Slow case of String::Equals. This implementation works on any strings |
| 4722 | // but it is most efficient on strings that are almost flat. |
| 4723 | bool SlowEquals(String* other); |
| 4724 | |
| 4725 | // Slow case of AsArrayIndex. |
| 4726 | bool SlowAsArrayIndex(uint32_t* index); |
| 4727 | |
| 4728 | // Compute and set the hash code. |
| 4729 | uint32_t ComputeAndSetHash(); |
| 4730 | |
| 4731 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
| 4732 | }; |
| 4733 | |
| 4734 | |
| 4735 | // The SeqString abstract class captures sequential string values. |
| 4736 | class SeqString: public String { |
| 4737 | public: |
| 4738 | |
| 4739 | // Casting. |
| 4740 | static inline SeqString* cast(Object* obj); |
| 4741 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4742 | private: |
| 4743 | DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString); |
| 4744 | }; |
| 4745 | |
| 4746 | |
| 4747 | // The AsciiString class captures sequential ascii string objects. |
| 4748 | // Each character in the AsciiString is an ascii character. |
| 4749 | class SeqAsciiString: public SeqString { |
| 4750 | public: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 4751 | static const bool kHasAsciiEncoding = true; |
| 4752 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4753 | // Dispatched behavior. |
| 4754 | inline uint16_t SeqAsciiStringGet(int index); |
| 4755 | inline void SeqAsciiStringSet(int index, uint16_t value); |
| 4756 | |
| 4757 | // Get the address of the characters in this string. |
| 4758 | inline Address GetCharsAddress(); |
| 4759 | |
| 4760 | inline char* GetChars(); |
| 4761 | |
| 4762 | // Casting |
| 4763 | static inline SeqAsciiString* cast(Object* obj); |
| 4764 | |
| 4765 | // Garbage collection support. This method is called by the |
| 4766 | // garbage collector to compute the actual size of an AsciiString |
| 4767 | // instance. |
| 4768 | inline int SeqAsciiStringSize(InstanceType instance_type); |
| 4769 | |
| 4770 | // Computes the size for an AsciiString instance of a given length. |
| 4771 | static int SizeFor(int length) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4772 | return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | // Layout description. |
| 4776 | static const int kHeaderSize = String::kSize; |
| 4777 | static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize); |
| 4778 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4779 | // Maximal memory usage for a single sequential ASCII string. |
| 4780 | static const int kMaxSize = 512 * MB; |
| 4781 | // Maximal length of a single sequential ASCII string. |
| 4782 | // Q.v. String::kMaxLength which is the maximal size of concatenated strings. |
| 4783 | static const int kMaxLength = (kMaxSize - kHeaderSize); |
| 4784 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4785 | // Support for StringInputBuffer. |
| 4786 | inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer, |
| 4787 | unsigned* offset, |
| 4788 | unsigned chars); |
| 4789 | inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining, |
| 4790 | unsigned* offset, |
| 4791 | unsigned chars); |
| 4792 | |
| 4793 | private: |
| 4794 | DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString); |
| 4795 | }; |
| 4796 | |
| 4797 | |
| 4798 | // The TwoByteString class captures sequential unicode string objects. |
| 4799 | // Each character in the TwoByteString is a two-byte uint16_t. |
| 4800 | class SeqTwoByteString: public SeqString { |
| 4801 | public: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 4802 | static const bool kHasAsciiEncoding = false; |
| 4803 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4804 | // Dispatched behavior. |
| 4805 | inline uint16_t SeqTwoByteStringGet(int index); |
| 4806 | inline void SeqTwoByteStringSet(int index, uint16_t value); |
| 4807 | |
| 4808 | // Get the address of the characters in this string. |
| 4809 | inline Address GetCharsAddress(); |
| 4810 | |
| 4811 | inline uc16* GetChars(); |
| 4812 | |
| 4813 | // For regexp code. |
| 4814 | const uint16_t* SeqTwoByteStringGetData(unsigned start); |
| 4815 | |
| 4816 | // Casting |
| 4817 | static inline SeqTwoByteString* cast(Object* obj); |
| 4818 | |
| 4819 | // Garbage collection support. This method is called by the |
| 4820 | // garbage collector to compute the actual size of a TwoByteString |
| 4821 | // instance. |
| 4822 | inline int SeqTwoByteStringSize(InstanceType instance_type); |
| 4823 | |
| 4824 | // Computes the size for a TwoByteString instance of a given length. |
| 4825 | static int SizeFor(int length) { |
Ben Murdoch | 7f4d5bd | 2010-06-15 11:15:29 +0100 | [diff] [blame] | 4826 | return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | // Layout description. |
| 4830 | static const int kHeaderSize = String::kSize; |
| 4831 | static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize); |
| 4832 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 4833 | // Maximal memory usage for a single sequential two-byte string. |
| 4834 | static const int kMaxSize = 512 * MB; |
| 4835 | // Maximal length of a single sequential two-byte string. |
| 4836 | // Q.v. String::kMaxLength which is the maximal size of concatenated strings. |
| 4837 | static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t); |
| 4838 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4839 | // Support for StringInputBuffer. |
| 4840 | inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer, |
| 4841 | unsigned* offset_ptr, |
| 4842 | unsigned chars); |
| 4843 | |
| 4844 | private: |
| 4845 | DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString); |
| 4846 | }; |
| 4847 | |
| 4848 | |
| 4849 | // The ConsString class describes string values built by using the |
| 4850 | // addition operator on strings. A ConsString is a pair where the |
| 4851 | // first and second components are pointers to other string values. |
| 4852 | // One or both components of a ConsString can be pointers to other |
| 4853 | // ConsStrings, creating a binary tree of ConsStrings where the leaves |
| 4854 | // are non-ConsString string values. The string value represented by |
| 4855 | // a ConsString can be obtained by concatenating the leaf string |
| 4856 | // values in a left-to-right depth-first traversal of the tree. |
| 4857 | class ConsString: public String { |
| 4858 | public: |
| 4859 | // First string of the cons cell. |
| 4860 | inline String* first(); |
| 4861 | // Doesn't check that the result is a string, even in debug mode. This is |
| 4862 | // useful during GC where the mark bits confuse the checks. |
| 4863 | inline Object* unchecked_first(); |
| 4864 | inline void set_first(String* first, |
| 4865 | WriteBarrierMode mode = UPDATE_WRITE_BARRIER); |
| 4866 | |
| 4867 | // Second string of the cons cell. |
| 4868 | inline String* second(); |
| 4869 | // Doesn't check that the result is a string, even in debug mode. This is |
| 4870 | // useful during GC where the mark bits confuse the checks. |
| 4871 | inline Object* unchecked_second(); |
| 4872 | inline void set_second(String* second, |
| 4873 | WriteBarrierMode mode = UPDATE_WRITE_BARRIER); |
| 4874 | |
| 4875 | // Dispatched behavior. |
| 4876 | uint16_t ConsStringGet(int index); |
| 4877 | |
| 4878 | // Casting. |
| 4879 | static inline ConsString* cast(Object* obj); |
| 4880 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4881 | // Layout description. |
| 4882 | static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize); |
| 4883 | static const int kSecondOffset = kFirstOffset + kPointerSize; |
| 4884 | static const int kSize = kSecondOffset + kPointerSize; |
| 4885 | |
| 4886 | // Support for StringInputBuffer. |
| 4887 | inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer, |
| 4888 | unsigned* offset_ptr, |
| 4889 | unsigned chars); |
| 4890 | inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer, |
| 4891 | unsigned* offset_ptr, |
| 4892 | unsigned chars); |
| 4893 | |
| 4894 | // Minimum length for a cons string. |
| 4895 | static const int kMinLength = 13; |
| 4896 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4897 | typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize> |
| 4898 | BodyDescriptor; |
| 4899 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4900 | private: |
| 4901 | DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString); |
| 4902 | }; |
| 4903 | |
| 4904 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4905 | // The ExternalString class describes string values that are backed by |
| 4906 | // a string resource that lies outside the V8 heap. ExternalStrings |
| 4907 | // consist of the length field common to all strings, a pointer to the |
| 4908 | // external resource. It is important to ensure (externally) that the |
| 4909 | // resource is not deallocated while the ExternalString is live in the |
| 4910 | // V8 heap. |
| 4911 | // |
| 4912 | // The API expects that all ExternalStrings are created through the |
| 4913 | // API. Therefore, ExternalStrings should not be used internally. |
| 4914 | class ExternalString: public String { |
| 4915 | public: |
| 4916 | // Casting |
| 4917 | static inline ExternalString* cast(Object* obj); |
| 4918 | |
| 4919 | // Layout description. |
| 4920 | static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize); |
| 4921 | static const int kSize = kResourceOffset + kPointerSize; |
| 4922 | |
| 4923 | STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset); |
| 4924 | |
| 4925 | private: |
| 4926 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString); |
| 4927 | }; |
| 4928 | |
| 4929 | |
| 4930 | // The ExternalAsciiString class is an external string backed by an |
| 4931 | // ASCII string. |
| 4932 | class ExternalAsciiString: public ExternalString { |
| 4933 | public: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 4934 | static const bool kHasAsciiEncoding = true; |
| 4935 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4936 | typedef v8::String::ExternalAsciiStringResource Resource; |
| 4937 | |
| 4938 | // The underlying resource. |
| 4939 | inline Resource* resource(); |
| 4940 | inline void set_resource(Resource* buffer); |
| 4941 | |
| 4942 | // Dispatched behavior. |
| 4943 | uint16_t ExternalAsciiStringGet(int index); |
| 4944 | |
| 4945 | // Casting. |
| 4946 | static inline ExternalAsciiString* cast(Object* obj); |
| 4947 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4948 | // Garbage collection support. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4949 | inline void ExternalAsciiStringIterateBody(ObjectVisitor* v); |
| 4950 | |
| 4951 | template<typename StaticVisitor> |
| 4952 | inline void ExternalAsciiStringIterateBody(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4953 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4954 | // Support for StringInputBuffer. |
| 4955 | const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining, |
| 4956 | unsigned* offset, |
| 4957 | unsigned chars); |
| 4958 | inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer, |
| 4959 | unsigned* offset, |
| 4960 | unsigned chars); |
| 4961 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4962 | private: |
| 4963 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString); |
| 4964 | }; |
| 4965 | |
| 4966 | |
| 4967 | // The ExternalTwoByteString class is an external string backed by a UTF-16 |
| 4968 | // encoded string. |
| 4969 | class ExternalTwoByteString: public ExternalString { |
| 4970 | public: |
Leon Clarke | ac95265 | 2010-07-15 11:15:24 +0100 | [diff] [blame] | 4971 | static const bool kHasAsciiEncoding = false; |
| 4972 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4973 | typedef v8::String::ExternalStringResource Resource; |
| 4974 | |
| 4975 | // The underlying string resource. |
| 4976 | inline Resource* resource(); |
| 4977 | inline void set_resource(Resource* buffer); |
| 4978 | |
| 4979 | // Dispatched behavior. |
| 4980 | uint16_t ExternalTwoByteStringGet(int index); |
| 4981 | |
| 4982 | // For regexp code. |
| 4983 | const uint16_t* ExternalTwoByteStringGetData(unsigned start); |
| 4984 | |
| 4985 | // Casting. |
| 4986 | static inline ExternalTwoByteString* cast(Object* obj); |
| 4987 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4988 | // Garbage collection support. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 4989 | inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v); |
| 4990 | |
| 4991 | template<typename StaticVisitor> |
| 4992 | inline void ExternalTwoByteStringIterateBody(); |
| 4993 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 4994 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4995 | // Support for StringInputBuffer. |
| 4996 | void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer, |
| 4997 | unsigned* offset_ptr, |
| 4998 | unsigned chars); |
| 4999 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5000 | private: |
| 5001 | DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString); |
| 5002 | }; |
| 5003 | |
| 5004 | |
| 5005 | // Utility superclass for stack-allocated objects that must be updated |
| 5006 | // on gc. It provides two ways for the gc to update instances, either |
| 5007 | // iterating or updating after gc. |
| 5008 | class Relocatable BASE_EMBEDDED { |
| 5009 | public: |
| 5010 | inline Relocatable() : prev_(top_) { top_ = this; } |
| 5011 | virtual ~Relocatable() { |
| 5012 | ASSERT_EQ(top_, this); |
| 5013 | top_ = prev_; |
| 5014 | } |
| 5015 | virtual void IterateInstance(ObjectVisitor* v) { } |
| 5016 | virtual void PostGarbageCollection() { } |
| 5017 | |
| 5018 | static void PostGarbageCollectionProcessing(); |
| 5019 | static int ArchiveSpacePerThread(); |
| 5020 | static char* ArchiveState(char* to); |
| 5021 | static char* RestoreState(char* from); |
| 5022 | static void Iterate(ObjectVisitor* v); |
| 5023 | static void Iterate(ObjectVisitor* v, Relocatable* top); |
| 5024 | static char* Iterate(ObjectVisitor* v, char* t); |
| 5025 | private: |
| 5026 | static Relocatable* top_; |
| 5027 | Relocatable* prev_; |
| 5028 | }; |
| 5029 | |
| 5030 | |
| 5031 | // A flat string reader provides random access to the contents of a |
| 5032 | // string independent of the character width of the string. The handle |
| 5033 | // must be valid as long as the reader is being used. |
| 5034 | class FlatStringReader : public Relocatable { |
| 5035 | public: |
| 5036 | explicit FlatStringReader(Handle<String> str); |
| 5037 | explicit FlatStringReader(Vector<const char> input); |
| 5038 | void PostGarbageCollection(); |
| 5039 | inline uc32 Get(int index); |
| 5040 | int length() { return length_; } |
| 5041 | private: |
| 5042 | String** str_; |
| 5043 | bool is_ascii_; |
| 5044 | int length_; |
| 5045 | const void* start_; |
| 5046 | }; |
| 5047 | |
| 5048 | |
| 5049 | // Note that StringInputBuffers are not valid across a GC! To fix this |
| 5050 | // it would have to store a String Handle instead of a String* and |
| 5051 | // AsciiStringReadBlock would have to be modified to use memcpy. |
| 5052 | // |
| 5053 | // StringInputBuffer is able to traverse any string regardless of how |
| 5054 | // deeply nested a sequence of ConsStrings it is made of. However, |
| 5055 | // performance will be better if deep strings are flattened before they |
| 5056 | // are traversed. Since flattening requires memory allocation this is |
| 5057 | // not always desirable, however (esp. in debugging situations). |
| 5058 | class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> { |
| 5059 | public: |
| 5060 | virtual void Seek(unsigned pos); |
| 5061 | inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {} |
| 5062 | inline StringInputBuffer(String* backing): |
| 5063 | unibrow::InputBuffer<String, String*, 1024>(backing) {} |
| 5064 | }; |
| 5065 | |
| 5066 | |
| 5067 | class SafeStringInputBuffer |
| 5068 | : public unibrow::InputBuffer<String, String**, 256> { |
| 5069 | public: |
| 5070 | virtual void Seek(unsigned pos); |
| 5071 | inline SafeStringInputBuffer() |
| 5072 | : unibrow::InputBuffer<String, String**, 256>() {} |
| 5073 | inline SafeStringInputBuffer(String** backing) |
| 5074 | : unibrow::InputBuffer<String, String**, 256>(backing) {} |
| 5075 | }; |
| 5076 | |
| 5077 | |
| 5078 | template <typename T> |
| 5079 | class VectorIterator { |
| 5080 | public: |
| 5081 | VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { } |
| 5082 | explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { } |
| 5083 | T GetNext() { return data_[index_++]; } |
| 5084 | bool has_more() { return index_ < data_.length(); } |
| 5085 | private: |
| 5086 | Vector<const T> data_; |
| 5087 | int index_; |
| 5088 | }; |
| 5089 | |
| 5090 | |
| 5091 | // The Oddball describes objects null, undefined, true, and false. |
| 5092 | class Oddball: public HeapObject { |
| 5093 | public: |
| 5094 | // [to_string]: Cached to_string computed at startup. |
| 5095 | DECL_ACCESSORS(to_string, String) |
| 5096 | |
| 5097 | // [to_number]: Cached to_number computed at startup. |
| 5098 | DECL_ACCESSORS(to_number, Object) |
| 5099 | |
| 5100 | // Casting. |
| 5101 | static inline Oddball* cast(Object* obj); |
| 5102 | |
| 5103 | // Dispatched behavior. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5104 | #ifdef DEBUG |
| 5105 | void OddballVerify(); |
| 5106 | #endif |
| 5107 | |
| 5108 | // Initialize the fields. |
| 5109 | Object* Initialize(const char* to_string, Object* to_number); |
| 5110 | |
| 5111 | // Layout description. |
| 5112 | static const int kToStringOffset = HeapObject::kHeaderSize; |
| 5113 | static const int kToNumberOffset = kToStringOffset + kPointerSize; |
| 5114 | static const int kSize = kToNumberOffset + kPointerSize; |
| 5115 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5116 | typedef FixedBodyDescriptor<kToStringOffset, |
| 5117 | kToNumberOffset + kPointerSize, |
| 5118 | kSize> BodyDescriptor; |
| 5119 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5120 | private: |
| 5121 | DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball); |
| 5122 | }; |
| 5123 | |
| 5124 | |
| 5125 | class JSGlobalPropertyCell: public HeapObject { |
| 5126 | public: |
| 5127 | // [value]: value of the global property. |
| 5128 | DECL_ACCESSORS(value, Object) |
| 5129 | |
| 5130 | // Casting. |
| 5131 | static inline JSGlobalPropertyCell* cast(Object* obj); |
| 5132 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5133 | #ifdef DEBUG |
| 5134 | void JSGlobalPropertyCellVerify(); |
| 5135 | void JSGlobalPropertyCellPrint(); |
| 5136 | #endif |
| 5137 | |
| 5138 | // Layout description. |
| 5139 | static const int kValueOffset = HeapObject::kHeaderSize; |
| 5140 | static const int kSize = kValueOffset + kPointerSize; |
| 5141 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5142 | typedef FixedBodyDescriptor<kValueOffset, |
| 5143 | kValueOffset + kPointerSize, |
| 5144 | kSize> BodyDescriptor; |
| 5145 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5146 | private: |
| 5147 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell); |
| 5148 | }; |
| 5149 | |
| 5150 | |
| 5151 | |
| 5152 | // Proxy describes objects pointing from JavaScript to C structures. |
| 5153 | // Since they cannot contain references to JS HeapObjects they can be |
| 5154 | // placed in old_data_space. |
| 5155 | class Proxy: public HeapObject { |
| 5156 | public: |
| 5157 | // [proxy]: field containing the address. |
| 5158 | inline Address proxy(); |
| 5159 | inline void set_proxy(Address value); |
| 5160 | |
| 5161 | // Casting. |
| 5162 | static inline Proxy* cast(Object* obj); |
| 5163 | |
| 5164 | // Dispatched behavior. |
| 5165 | inline void ProxyIterateBody(ObjectVisitor* v); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5166 | |
| 5167 | template<typename StaticVisitor> |
| 5168 | inline void ProxyIterateBody(); |
| 5169 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5170 | #ifdef DEBUG |
| 5171 | void ProxyPrint(); |
| 5172 | void ProxyVerify(); |
| 5173 | #endif |
| 5174 | |
| 5175 | // Layout description. |
| 5176 | |
| 5177 | static const int kProxyOffset = HeapObject::kHeaderSize; |
| 5178 | static const int kSize = kProxyOffset + kPointerSize; |
| 5179 | |
| 5180 | STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset); |
| 5181 | |
| 5182 | private: |
| 5183 | DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy); |
| 5184 | }; |
| 5185 | |
| 5186 | |
| 5187 | // The JSArray describes JavaScript Arrays |
| 5188 | // Such an array can be in one of two modes: |
| 5189 | // - fast, backing storage is a FixedArray and length <= elements.length(); |
| 5190 | // Please note: push and pop can be used to grow and shrink the array. |
| 5191 | // - slow, backing storage is a HashTable with numbers as keys. |
| 5192 | class JSArray: public JSObject { |
| 5193 | public: |
| 5194 | // [length]: The length property. |
| 5195 | DECL_ACCESSORS(length, Object) |
| 5196 | |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 5197 | // Overload the length setter to skip write barrier when the length |
| 5198 | // is set to a smi. This matches the set function on FixedArray. |
| 5199 | inline void set_length(Smi* length); |
| 5200 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5201 | MUST_USE_RESULT Object* JSArrayUpdateLengthFromIndex(uint32_t index, |
| 5202 | Object* value); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5203 | |
| 5204 | // Initialize the array with the given capacity. The function may |
| 5205 | // fail due to out-of-memory situations, but only if the requested |
| 5206 | // capacity is non-zero. |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 5207 | MUST_USE_RESULT Object* Initialize(int capacity); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5208 | |
| 5209 | // Set the content of the array to the content of storage. |
| 5210 | inline void SetContent(FixedArray* storage); |
| 5211 | |
| 5212 | // Casting. |
| 5213 | static inline JSArray* cast(Object* obj); |
| 5214 | |
| 5215 | // Uses handles. Ensures that the fixed array backing the JSArray has at |
| 5216 | // least the stated size. |
| 5217 | inline void EnsureSize(int minimum_size_of_backing_fixed_array); |
| 5218 | |
| 5219 | // Dispatched behavior. |
| 5220 | #ifdef DEBUG |
| 5221 | void JSArrayPrint(); |
| 5222 | void JSArrayVerify(); |
| 5223 | #endif |
| 5224 | |
| 5225 | // Number of element slots to pre-allocate for an empty array. |
| 5226 | static const int kPreallocatedArrayElements = 4; |
| 5227 | |
| 5228 | // Layout description. |
| 5229 | static const int kLengthOffset = JSObject::kHeaderSize; |
| 5230 | static const int kSize = kLengthOffset + kPointerSize; |
| 5231 | |
| 5232 | private: |
| 5233 | // Expand the fixed array backing of a fast-case JSArray to at least |
| 5234 | // the requested size. |
| 5235 | void Expand(int minimum_size_of_backing_fixed_array); |
| 5236 | |
| 5237 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray); |
| 5238 | }; |
| 5239 | |
| 5240 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 5241 | // JSRegExpResult is just a JSArray with a specific initial map. |
| 5242 | // This initial map adds in-object properties for "index" and "input" |
| 5243 | // properties, as assigned by RegExp.prototype.exec, which allows |
| 5244 | // faster creation of RegExp exec results. |
| 5245 | // This class just holds constants used when creating the result. |
| 5246 | // After creation the result must be treated as a JSArray in all regards. |
| 5247 | class JSRegExpResult: public JSArray { |
| 5248 | public: |
| 5249 | // Offsets of object fields. |
| 5250 | static const int kIndexOffset = JSArray::kSize; |
| 5251 | static const int kInputOffset = kIndexOffset + kPointerSize; |
| 5252 | static const int kSize = kInputOffset + kPointerSize; |
| 5253 | // Indices of in-object properties. |
| 5254 | static const int kIndexIndex = 0; |
| 5255 | static const int kInputIndex = 1; |
| 5256 | private: |
| 5257 | DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult); |
| 5258 | }; |
| 5259 | |
| 5260 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5261 | // An accessor must have a getter, but can have no setter. |
| 5262 | // |
| 5263 | // When setting a property, V8 searches accessors in prototypes. |
| 5264 | // If an accessor was found and it does not have a setter, |
| 5265 | // the request is ignored. |
| 5266 | // |
| 5267 | // If the accessor in the prototype has the READ_ONLY property attribute, then |
| 5268 | // a new value is added to the local object when the property is set. |
| 5269 | // This shadows the accessor in the prototype. |
| 5270 | class AccessorInfo: public Struct { |
| 5271 | public: |
| 5272 | DECL_ACCESSORS(getter, Object) |
| 5273 | DECL_ACCESSORS(setter, Object) |
| 5274 | DECL_ACCESSORS(data, Object) |
| 5275 | DECL_ACCESSORS(name, Object) |
| 5276 | DECL_ACCESSORS(flag, Smi) |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5277 | DECL_ACCESSORS(load_stub_cache, Object) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5278 | |
| 5279 | inline bool all_can_read(); |
| 5280 | inline void set_all_can_read(bool value); |
| 5281 | |
| 5282 | inline bool all_can_write(); |
| 5283 | inline void set_all_can_write(bool value); |
| 5284 | |
| 5285 | inline bool prohibits_overwriting(); |
| 5286 | inline void set_prohibits_overwriting(bool value); |
| 5287 | |
| 5288 | inline PropertyAttributes property_attributes(); |
| 5289 | inline void set_property_attributes(PropertyAttributes attributes); |
| 5290 | |
| 5291 | static inline AccessorInfo* cast(Object* obj); |
| 5292 | |
| 5293 | #ifdef DEBUG |
| 5294 | void AccessorInfoPrint(); |
| 5295 | void AccessorInfoVerify(); |
| 5296 | #endif |
| 5297 | |
| 5298 | static const int kGetterOffset = HeapObject::kHeaderSize; |
| 5299 | static const int kSetterOffset = kGetterOffset + kPointerSize; |
| 5300 | static const int kDataOffset = kSetterOffset + kPointerSize; |
| 5301 | static const int kNameOffset = kDataOffset + kPointerSize; |
| 5302 | static const int kFlagOffset = kNameOffset + kPointerSize; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5303 | static const int kLoadStubCacheOffset = kFlagOffset + kPointerSize; |
| 5304 | static const int kSize = kLoadStubCacheOffset + kPointerSize; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5305 | |
| 5306 | private: |
| 5307 | // Bit positions in flag. |
| 5308 | static const int kAllCanReadBit = 0; |
| 5309 | static const int kAllCanWriteBit = 1; |
| 5310 | static const int kProhibitsOverwritingBit = 2; |
| 5311 | class AttributesField: public BitField<PropertyAttributes, 3, 3> {}; |
| 5312 | |
| 5313 | DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo); |
| 5314 | }; |
| 5315 | |
| 5316 | |
| 5317 | class AccessCheckInfo: public Struct { |
| 5318 | public: |
| 5319 | DECL_ACCESSORS(named_callback, Object) |
| 5320 | DECL_ACCESSORS(indexed_callback, Object) |
| 5321 | DECL_ACCESSORS(data, Object) |
| 5322 | |
| 5323 | static inline AccessCheckInfo* cast(Object* obj); |
| 5324 | |
| 5325 | #ifdef DEBUG |
| 5326 | void AccessCheckInfoPrint(); |
| 5327 | void AccessCheckInfoVerify(); |
| 5328 | #endif |
| 5329 | |
| 5330 | static const int kNamedCallbackOffset = HeapObject::kHeaderSize; |
| 5331 | static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize; |
| 5332 | static const int kDataOffset = kIndexedCallbackOffset + kPointerSize; |
| 5333 | static const int kSize = kDataOffset + kPointerSize; |
| 5334 | |
| 5335 | private: |
| 5336 | DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo); |
| 5337 | }; |
| 5338 | |
| 5339 | |
| 5340 | class InterceptorInfo: public Struct { |
| 5341 | public: |
| 5342 | DECL_ACCESSORS(getter, Object) |
| 5343 | DECL_ACCESSORS(setter, Object) |
| 5344 | DECL_ACCESSORS(query, Object) |
| 5345 | DECL_ACCESSORS(deleter, Object) |
| 5346 | DECL_ACCESSORS(enumerator, Object) |
| 5347 | DECL_ACCESSORS(data, Object) |
| 5348 | |
| 5349 | static inline InterceptorInfo* cast(Object* obj); |
| 5350 | |
| 5351 | #ifdef DEBUG |
| 5352 | void InterceptorInfoPrint(); |
| 5353 | void InterceptorInfoVerify(); |
| 5354 | #endif |
| 5355 | |
| 5356 | static const int kGetterOffset = HeapObject::kHeaderSize; |
| 5357 | static const int kSetterOffset = kGetterOffset + kPointerSize; |
| 5358 | static const int kQueryOffset = kSetterOffset + kPointerSize; |
| 5359 | static const int kDeleterOffset = kQueryOffset + kPointerSize; |
| 5360 | static const int kEnumeratorOffset = kDeleterOffset + kPointerSize; |
| 5361 | static const int kDataOffset = kEnumeratorOffset + kPointerSize; |
| 5362 | static const int kSize = kDataOffset + kPointerSize; |
| 5363 | |
| 5364 | private: |
| 5365 | DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo); |
| 5366 | }; |
| 5367 | |
| 5368 | |
| 5369 | class CallHandlerInfo: public Struct { |
| 5370 | public: |
| 5371 | DECL_ACCESSORS(callback, Object) |
| 5372 | DECL_ACCESSORS(data, Object) |
| 5373 | |
| 5374 | static inline CallHandlerInfo* cast(Object* obj); |
| 5375 | |
| 5376 | #ifdef DEBUG |
| 5377 | void CallHandlerInfoPrint(); |
| 5378 | void CallHandlerInfoVerify(); |
| 5379 | #endif |
| 5380 | |
| 5381 | static const int kCallbackOffset = HeapObject::kHeaderSize; |
| 5382 | static const int kDataOffset = kCallbackOffset + kPointerSize; |
| 5383 | static const int kSize = kDataOffset + kPointerSize; |
| 5384 | |
| 5385 | private: |
| 5386 | DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo); |
| 5387 | }; |
| 5388 | |
| 5389 | |
| 5390 | class TemplateInfo: public Struct { |
| 5391 | public: |
| 5392 | DECL_ACCESSORS(tag, Object) |
| 5393 | DECL_ACCESSORS(property_list, Object) |
| 5394 | |
| 5395 | #ifdef DEBUG |
| 5396 | void TemplateInfoVerify(); |
| 5397 | #endif |
| 5398 | |
| 5399 | static const int kTagOffset = HeapObject::kHeaderSize; |
| 5400 | static const int kPropertyListOffset = kTagOffset + kPointerSize; |
| 5401 | static const int kHeaderSize = kPropertyListOffset + kPointerSize; |
| 5402 | protected: |
| 5403 | friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs; |
| 5404 | DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo); |
| 5405 | }; |
| 5406 | |
| 5407 | |
| 5408 | class FunctionTemplateInfo: public TemplateInfo { |
| 5409 | public: |
| 5410 | DECL_ACCESSORS(serial_number, Object) |
| 5411 | DECL_ACCESSORS(call_code, Object) |
| 5412 | DECL_ACCESSORS(property_accessors, Object) |
| 5413 | DECL_ACCESSORS(prototype_template, Object) |
| 5414 | DECL_ACCESSORS(parent_template, Object) |
| 5415 | DECL_ACCESSORS(named_property_handler, Object) |
| 5416 | DECL_ACCESSORS(indexed_property_handler, Object) |
| 5417 | DECL_ACCESSORS(instance_template, Object) |
| 5418 | DECL_ACCESSORS(class_name, Object) |
| 5419 | DECL_ACCESSORS(signature, Object) |
| 5420 | DECL_ACCESSORS(instance_call_handler, Object) |
| 5421 | DECL_ACCESSORS(access_check_info, Object) |
| 5422 | DECL_ACCESSORS(flag, Smi) |
| 5423 | |
| 5424 | // Following properties use flag bits. |
| 5425 | DECL_BOOLEAN_ACCESSORS(hidden_prototype) |
| 5426 | DECL_BOOLEAN_ACCESSORS(undetectable) |
| 5427 | // If the bit is set, object instances created by this function |
| 5428 | // requires access check. |
| 5429 | DECL_BOOLEAN_ACCESSORS(needs_access_check) |
| 5430 | |
| 5431 | static inline FunctionTemplateInfo* cast(Object* obj); |
| 5432 | |
| 5433 | #ifdef DEBUG |
| 5434 | void FunctionTemplateInfoPrint(); |
| 5435 | void FunctionTemplateInfoVerify(); |
| 5436 | #endif |
| 5437 | |
| 5438 | static const int kSerialNumberOffset = TemplateInfo::kHeaderSize; |
| 5439 | static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize; |
| 5440 | static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize; |
| 5441 | static const int kPrototypeTemplateOffset = |
| 5442 | kPropertyAccessorsOffset + kPointerSize; |
| 5443 | static const int kParentTemplateOffset = |
| 5444 | kPrototypeTemplateOffset + kPointerSize; |
| 5445 | static const int kNamedPropertyHandlerOffset = |
| 5446 | kParentTemplateOffset + kPointerSize; |
| 5447 | static const int kIndexedPropertyHandlerOffset = |
| 5448 | kNamedPropertyHandlerOffset + kPointerSize; |
| 5449 | static const int kInstanceTemplateOffset = |
| 5450 | kIndexedPropertyHandlerOffset + kPointerSize; |
| 5451 | static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize; |
| 5452 | static const int kSignatureOffset = kClassNameOffset + kPointerSize; |
| 5453 | static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize; |
| 5454 | static const int kAccessCheckInfoOffset = |
| 5455 | kInstanceCallHandlerOffset + kPointerSize; |
| 5456 | static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize; |
| 5457 | static const int kSize = kFlagOffset + kPointerSize; |
| 5458 | |
| 5459 | private: |
| 5460 | // Bit position in the flag, from least significant bit position. |
| 5461 | static const int kHiddenPrototypeBit = 0; |
| 5462 | static const int kUndetectableBit = 1; |
| 5463 | static const int kNeedsAccessCheckBit = 2; |
| 5464 | |
| 5465 | DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo); |
| 5466 | }; |
| 5467 | |
| 5468 | |
| 5469 | class ObjectTemplateInfo: public TemplateInfo { |
| 5470 | public: |
| 5471 | DECL_ACCESSORS(constructor, Object) |
| 5472 | DECL_ACCESSORS(internal_field_count, Object) |
| 5473 | |
| 5474 | static inline ObjectTemplateInfo* cast(Object* obj); |
| 5475 | |
| 5476 | #ifdef DEBUG |
| 5477 | void ObjectTemplateInfoPrint(); |
| 5478 | void ObjectTemplateInfoVerify(); |
| 5479 | #endif |
| 5480 | |
| 5481 | static const int kConstructorOffset = TemplateInfo::kHeaderSize; |
| 5482 | static const int kInternalFieldCountOffset = |
| 5483 | kConstructorOffset + kPointerSize; |
| 5484 | static const int kSize = kInternalFieldCountOffset + kPointerSize; |
| 5485 | }; |
| 5486 | |
| 5487 | |
| 5488 | class SignatureInfo: public Struct { |
| 5489 | public: |
| 5490 | DECL_ACCESSORS(receiver, Object) |
| 5491 | DECL_ACCESSORS(args, Object) |
| 5492 | |
| 5493 | static inline SignatureInfo* cast(Object* obj); |
| 5494 | |
| 5495 | #ifdef DEBUG |
| 5496 | void SignatureInfoPrint(); |
| 5497 | void SignatureInfoVerify(); |
| 5498 | #endif |
| 5499 | |
| 5500 | static const int kReceiverOffset = Struct::kHeaderSize; |
| 5501 | static const int kArgsOffset = kReceiverOffset + kPointerSize; |
| 5502 | static const int kSize = kArgsOffset + kPointerSize; |
| 5503 | |
| 5504 | private: |
| 5505 | DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo); |
| 5506 | }; |
| 5507 | |
| 5508 | |
| 5509 | class TypeSwitchInfo: public Struct { |
| 5510 | public: |
| 5511 | DECL_ACCESSORS(types, Object) |
| 5512 | |
| 5513 | static inline TypeSwitchInfo* cast(Object* obj); |
| 5514 | |
| 5515 | #ifdef DEBUG |
| 5516 | void TypeSwitchInfoPrint(); |
| 5517 | void TypeSwitchInfoVerify(); |
| 5518 | #endif |
| 5519 | |
| 5520 | static const int kTypesOffset = Struct::kHeaderSize; |
| 5521 | static const int kSize = kTypesOffset + kPointerSize; |
| 5522 | }; |
| 5523 | |
| 5524 | |
| 5525 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 5526 | // The DebugInfo class holds additional information for a function being |
| 5527 | // debugged. |
| 5528 | class DebugInfo: public Struct { |
| 5529 | public: |
| 5530 | // The shared function info for the source being debugged. |
| 5531 | DECL_ACCESSORS(shared, SharedFunctionInfo) |
| 5532 | // Code object for the original code. |
| 5533 | DECL_ACCESSORS(original_code, Code) |
| 5534 | // Code object for the patched code. This code object is the code object |
| 5535 | // currently active for the function. |
| 5536 | DECL_ACCESSORS(code, Code) |
| 5537 | // Fixed array holding status information for each active break point. |
| 5538 | DECL_ACCESSORS(break_points, FixedArray) |
| 5539 | |
| 5540 | // Check if there is a break point at a code position. |
| 5541 | bool HasBreakPoint(int code_position); |
| 5542 | // Get the break point info object for a code position. |
| 5543 | Object* GetBreakPointInfo(int code_position); |
| 5544 | // Clear a break point. |
| 5545 | static void ClearBreakPoint(Handle<DebugInfo> debug_info, |
| 5546 | int code_position, |
| 5547 | Handle<Object> break_point_object); |
| 5548 | // Set a break point. |
| 5549 | static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position, |
| 5550 | int source_position, int statement_position, |
| 5551 | Handle<Object> break_point_object); |
| 5552 | // Get the break point objects for a code position. |
| 5553 | Object* GetBreakPointObjects(int code_position); |
| 5554 | // Find the break point info holding this break point object. |
| 5555 | static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info, |
| 5556 | Handle<Object> break_point_object); |
| 5557 | // Get the number of break points for this function. |
| 5558 | int GetBreakPointCount(); |
| 5559 | |
| 5560 | static inline DebugInfo* cast(Object* obj); |
| 5561 | |
| 5562 | #ifdef DEBUG |
| 5563 | void DebugInfoPrint(); |
| 5564 | void DebugInfoVerify(); |
| 5565 | #endif |
| 5566 | |
| 5567 | static const int kSharedFunctionInfoIndex = Struct::kHeaderSize; |
| 5568 | static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize; |
| 5569 | static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize; |
| 5570 | static const int kActiveBreakPointsCountIndex = |
| 5571 | kPatchedCodeIndex + kPointerSize; |
| 5572 | static const int kBreakPointsStateIndex = |
| 5573 | kActiveBreakPointsCountIndex + kPointerSize; |
| 5574 | static const int kSize = kBreakPointsStateIndex + kPointerSize; |
| 5575 | |
| 5576 | private: |
| 5577 | static const int kNoBreakPointInfo = -1; |
| 5578 | |
| 5579 | // Lookup the index in the break_points array for a code position. |
| 5580 | int GetBreakPointInfoIndex(int code_position); |
| 5581 | |
| 5582 | DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo); |
| 5583 | }; |
| 5584 | |
| 5585 | |
| 5586 | // The BreakPointInfo class holds information for break points set in a |
| 5587 | // function. The DebugInfo object holds a BreakPointInfo object for each code |
| 5588 | // position with one or more break points. |
| 5589 | class BreakPointInfo: public Struct { |
| 5590 | public: |
| 5591 | // The position in the code for the break point. |
| 5592 | DECL_ACCESSORS(code_position, Smi) |
| 5593 | // The position in the source for the break position. |
| 5594 | DECL_ACCESSORS(source_position, Smi) |
| 5595 | // The position in the source for the last statement before this break |
| 5596 | // position. |
| 5597 | DECL_ACCESSORS(statement_position, Smi) |
| 5598 | // List of related JavaScript break points. |
| 5599 | DECL_ACCESSORS(break_point_objects, Object) |
| 5600 | |
| 5601 | // Removes a break point. |
| 5602 | static void ClearBreakPoint(Handle<BreakPointInfo> info, |
| 5603 | Handle<Object> break_point_object); |
| 5604 | // Set a break point. |
| 5605 | static void SetBreakPoint(Handle<BreakPointInfo> info, |
| 5606 | Handle<Object> break_point_object); |
| 5607 | // Check if break point info has this break point object. |
| 5608 | static bool HasBreakPointObject(Handle<BreakPointInfo> info, |
| 5609 | Handle<Object> break_point_object); |
| 5610 | // Get the number of break points for this code position. |
| 5611 | int GetBreakPointCount(); |
| 5612 | |
| 5613 | static inline BreakPointInfo* cast(Object* obj); |
| 5614 | |
| 5615 | #ifdef DEBUG |
| 5616 | void BreakPointInfoPrint(); |
| 5617 | void BreakPointInfoVerify(); |
| 5618 | #endif |
| 5619 | |
| 5620 | static const int kCodePositionIndex = Struct::kHeaderSize; |
| 5621 | static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize; |
| 5622 | static const int kStatementPositionIndex = |
| 5623 | kSourcePositionIndex + kPointerSize; |
| 5624 | static const int kBreakPointObjectsIndex = |
| 5625 | kStatementPositionIndex + kPointerSize; |
| 5626 | static const int kSize = kBreakPointObjectsIndex + kPointerSize; |
| 5627 | |
| 5628 | private: |
| 5629 | DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo); |
| 5630 | }; |
| 5631 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 5632 | |
| 5633 | |
| 5634 | #undef DECL_BOOLEAN_ACCESSORS |
| 5635 | #undef DECL_ACCESSORS |
| 5636 | |
| 5637 | |
| 5638 | // Abstract base class for visiting, and optionally modifying, the |
| 5639 | // pointers contained in Objects. Used in GC and serialization/deserialization. |
| 5640 | class ObjectVisitor BASE_EMBEDDED { |
| 5641 | public: |
| 5642 | virtual ~ObjectVisitor() {} |
| 5643 | |
| 5644 | // Visits a contiguous arrays of pointers in the half-open range |
| 5645 | // [start, end). Any or all of the values may be modified on return. |
| 5646 | virtual void VisitPointers(Object** start, Object** end) = 0; |
| 5647 | |
| 5648 | // To allow lazy clearing of inline caches the visitor has |
| 5649 | // a rich interface for iterating over Code objects.. |
| 5650 | |
| 5651 | // Visits a code target in the instruction stream. |
| 5652 | virtual void VisitCodeTarget(RelocInfo* rinfo); |
| 5653 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 5654 | // Visits a code entry in a JS function. |
| 5655 | virtual void VisitCodeEntry(Address entry_address); |
| 5656 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5657 | // Visits a runtime entry in the instruction stream. |
| 5658 | virtual void VisitRuntimeEntry(RelocInfo* rinfo) {} |
| 5659 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5660 | // Visits the resource of an ASCII or two-byte string. |
| 5661 | virtual void VisitExternalAsciiString( |
| 5662 | v8::String::ExternalAsciiStringResource** resource) {} |
| 5663 | virtual void VisitExternalTwoByteString( |
| 5664 | v8::String::ExternalStringResource** resource) {} |
| 5665 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5666 | // Visits a debug call target in the instruction stream. |
| 5667 | virtual void VisitDebugTarget(RelocInfo* rinfo); |
| 5668 | |
| 5669 | // Handy shorthand for visiting a single pointer. |
| 5670 | virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); } |
| 5671 | |
| 5672 | // Visits a contiguous arrays of external references (references to the C++ |
| 5673 | // heap) in the half-open range [start, end). Any or all of the values |
| 5674 | // may be modified on return. |
| 5675 | virtual void VisitExternalReferences(Address* start, Address* end) {} |
| 5676 | |
| 5677 | inline void VisitExternalReference(Address* p) { |
| 5678 | VisitExternalReferences(p, p + 1); |
| 5679 | } |
| 5680 | |
| 5681 | #ifdef DEBUG |
| 5682 | // Intended for serialization/deserialization checking: insert, or |
| 5683 | // check for the presence of, a tag at this position in the stream. |
| 5684 | virtual void Synchronize(const char* tag) {} |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 5685 | #else |
| 5686 | inline void Synchronize(const char* tag) {} |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5687 | #endif |
| 5688 | }; |
| 5689 | |
| 5690 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 5691 | class StructBodyDescriptor : public |
| 5692 | FlexibleBodyDescriptor<HeapObject::kHeaderSize> { |
| 5693 | public: |
| 5694 | static inline int SizeOf(Map* map, HeapObject* object) { |
| 5695 | return map->instance_size(); |
| 5696 | } |
| 5697 | }; |
| 5698 | |
| 5699 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5700 | // BooleanBit is a helper class for setting and getting a bit in an |
| 5701 | // integer or Smi. |
| 5702 | class BooleanBit : public AllStatic { |
| 5703 | public: |
| 5704 | static inline bool get(Smi* smi, int bit_position) { |
| 5705 | return get(smi->value(), bit_position); |
| 5706 | } |
| 5707 | |
| 5708 | static inline bool get(int value, int bit_position) { |
| 5709 | return (value & (1 << bit_position)) != 0; |
| 5710 | } |
| 5711 | |
| 5712 | static inline Smi* set(Smi* smi, int bit_position, bool v) { |
| 5713 | return Smi::FromInt(set(smi->value(), bit_position, v)); |
| 5714 | } |
| 5715 | |
| 5716 | static inline int set(int value, int bit_position, bool v) { |
| 5717 | if (v) { |
| 5718 | value |= (1 << bit_position); |
| 5719 | } else { |
| 5720 | value &= ~(1 << bit_position); |
| 5721 | } |
| 5722 | return value; |
| 5723 | } |
| 5724 | }; |
| 5725 | |
| 5726 | } } // namespace v8::internal |
| 5727 | |
| 5728 | #endif // V8_OBJECTS_H_ |