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