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