blob: 0c146656aa4d4ddc7ac003e2fea855cae6ad370b [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();
1194 inline ElementsKind GetElementsKind();
1195 inline bool HasFastElements();
1196 inline bool HasDictionaryElements();
1197 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001198 inline bool HasExternalArrayElements();
1199 inline bool HasExternalByteElements();
1200 inline bool HasExternalUnsignedByteElements();
1201 inline bool HasExternalShortElements();
1202 inline bool HasExternalUnsignedShortElements();
1203 inline bool HasExternalIntElements();
1204 inline bool HasExternalUnsignedIntElements();
1205 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001206 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001207 inline NumberDictionary* element_dictionary(); // Gets slow elements.
1208
1209 // Collects elements starting at index 0.
1210 // Undefined values are placed after non-undefined values.
1211 // Returns the number of non-undefined values.
1212 Object* PrepareElementsForSort(uint32_t limit);
1213 // As PrepareElementsForSort, but only on objects where elements is
1214 // a dictionary, and it will stay a dictionary.
1215 Object* PrepareSlowElementsForSort(uint32_t limit);
1216
1217 Object* SetProperty(String* key,
1218 Object* value,
1219 PropertyAttributes attributes);
1220 Object* SetProperty(LookupResult* result,
1221 String* key,
1222 Object* value,
1223 PropertyAttributes attributes);
1224 Object* SetPropertyWithFailedAccessCheck(LookupResult* result,
1225 String* name,
1226 Object* value);
1227 Object* SetPropertyWithCallback(Object* structure,
1228 String* name,
1229 Object* value,
1230 JSObject* holder);
1231 Object* SetPropertyWithDefinedSetter(JSFunction* setter,
1232 Object* value);
1233 Object* SetPropertyWithInterceptor(String* name,
1234 Object* value,
1235 PropertyAttributes attributes);
1236 Object* SetPropertyPostInterceptor(String* name,
1237 Object* value,
1238 PropertyAttributes attributes);
1239 Object* IgnoreAttributesAndSetLocalProperty(String* key,
1240 Object* value,
1241 PropertyAttributes attributes);
1242
1243 // Retrieve a value in a normalized object given a lookup result.
1244 // Handles the special representation of JS global objects.
1245 Object* GetNormalizedProperty(LookupResult* result);
1246
1247 // Sets the property value in a normalized object given a lookup result.
1248 // Handles the special representation of JS global objects.
1249 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1250
1251 // Sets the property value in a normalized object given (key, value, details).
1252 // Handles the special representation of JS global objects.
1253 Object* SetNormalizedProperty(String* name,
1254 Object* value,
1255 PropertyDetails details);
1256
1257 // Deletes the named property in a normalized object.
1258 Object* DeleteNormalizedProperty(String* name, DeleteMode mode);
1259
Steve Blocka7e24c12009-10-30 11:49:00 +00001260 // Returns the class name ([[Class]] property in the specification).
1261 String* class_name();
1262
1263 // Returns the constructor name (the name (possibly, inferred name) of the
1264 // function that was used to instantiate the object).
1265 String* constructor_name();
1266
1267 // Retrieve interceptors.
1268 InterceptorInfo* GetNamedInterceptor();
1269 InterceptorInfo* GetIndexedInterceptor();
1270
1271 inline PropertyAttributes GetPropertyAttribute(String* name);
1272 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1273 String* name);
1274 PropertyAttributes GetLocalPropertyAttribute(String* name);
1275
1276 Object* DefineAccessor(String* name, bool is_getter, JSFunction* fun,
1277 PropertyAttributes attributes);
1278 Object* LookupAccessor(String* name, bool is_getter);
1279
Leon Clarkef7060e22010-06-03 12:02:55 +01001280 Object* DefineAccessor(AccessorInfo* info);
1281
Steve Blocka7e24c12009-10-30 11:49:00 +00001282 // Used from Object::GetProperty().
1283 Object* GetPropertyWithFailedAccessCheck(Object* receiver,
1284 LookupResult* result,
1285 String* name,
1286 PropertyAttributes* attributes);
1287 Object* GetPropertyWithInterceptor(JSObject* receiver,
1288 String* name,
1289 PropertyAttributes* attributes);
1290 Object* GetPropertyPostInterceptor(JSObject* receiver,
1291 String* name,
1292 PropertyAttributes* attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001293 Object* GetLocalPropertyPostInterceptor(JSObject* receiver,
1294 String* name,
1295 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001296
1297 // Returns true if this is an instance of an api function and has
1298 // been modified since it was created. May give false positives.
1299 bool IsDirty();
1300
1301 bool HasProperty(String* name) {
1302 return GetPropertyAttribute(name) != ABSENT;
1303 }
1304
1305 // Can cause a GC if it hits an interceptor.
1306 bool HasLocalProperty(String* name) {
1307 return GetLocalPropertyAttribute(name) != ABSENT;
1308 }
1309
Steve Blockd0582a62009-12-15 09:54:21 +00001310 // If the receiver is a JSGlobalProxy this method will return its prototype,
1311 // otherwise the result is the receiver itself.
1312 inline Object* BypassGlobalProxy();
1313
1314 // Accessors for hidden properties object.
1315 //
1316 // Hidden properties are not local properties of the object itself.
1317 // Instead they are stored on an auxiliary JSObject stored as a local
1318 // property with a special name Heap::hidden_symbol(). But if the
1319 // receiver is a JSGlobalProxy then the auxiliary object is a property
1320 // of its prototype.
1321 //
1322 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1323 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1324 // holder.
1325 //
1326 // These accessors do not touch interceptors or accessors.
1327 inline bool HasHiddenPropertiesObject();
1328 inline Object* GetHiddenPropertiesObject();
1329 inline Object* SetHiddenPropertiesObject(Object* hidden_obj);
1330
Steve Blocka7e24c12009-10-30 11:49:00 +00001331 Object* DeleteProperty(String* name, DeleteMode mode);
1332 Object* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001333
1334 // Tests for the fast common case for property enumeration.
1335 bool IsSimpleEnum();
1336
1337 // Do we want to keep the elements in fast case when increasing the
1338 // capacity?
1339 bool ShouldConvertToSlowElements(int new_capacity);
1340 // Returns true if the backing storage for the slow-case elements of
1341 // this object takes up nearly as much space as a fast-case backing
1342 // storage would. In that case the JSObject should have fast
1343 // elements.
1344 bool ShouldConvertToFastElements();
1345
1346 // Return the object's prototype (might be Heap::null_value()).
1347 inline Object* GetPrototype();
1348
Andrei Popescu402d9372010-02-26 13:31:12 +00001349 // Set the object's prototype (only JSObject and null are allowed).
1350 Object* SetPrototype(Object* value, bool skip_hidden_prototypes);
1351
Steve Blocka7e24c12009-10-30 11:49:00 +00001352 // Tells whether the index'th element is present.
1353 inline bool HasElement(uint32_t index);
1354 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
1355 bool HasLocalElement(uint32_t index);
1356
1357 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1358 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1359
1360 Object* SetFastElement(uint32_t index, Object* value);
1361
1362 // Set the index'th array element.
1363 // A Failure object is returned if GC is needed.
1364 Object* SetElement(uint32_t index, Object* value);
1365
1366 // Returns the index'th element.
1367 // The undefined object if index is out of bounds.
1368 Object* GetElementWithReceiver(JSObject* receiver, uint32_t index);
1369
1370 void SetFastElements(FixedArray* elements);
1371 Object* SetSlowElements(Object* length);
1372
1373 // Lookup interceptors are used for handling properties controlled by host
1374 // objects.
1375 inline bool HasNamedInterceptor();
1376 inline bool HasIndexedInterceptor();
1377
1378 // Support functions for v8 api (needed for correct interceptor behavior).
1379 bool HasRealNamedProperty(String* key);
1380 bool HasRealElementProperty(uint32_t index);
1381 bool HasRealNamedCallbackProperty(String* key);
1382
1383 // Initializes the array to a certain length
1384 Object* SetElementsLength(Object* length);
1385
1386 // Get the header size for a JSObject. Used to compute the index of
1387 // internal fields as well as the number of internal fields.
1388 inline int GetHeaderSize();
1389
1390 inline int GetInternalFieldCount();
1391 inline Object* GetInternalField(int index);
1392 inline void SetInternalField(int index, Object* value);
1393
1394 // Lookup a property. If found, the result is valid and has
1395 // detailed information.
1396 void LocalLookup(String* name, LookupResult* result);
1397 void Lookup(String* name, LookupResult* result);
1398
1399 // The following lookup functions skip interceptors.
1400 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1401 void LookupRealNamedProperty(String* name, LookupResult* result);
1402 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1403 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Leon Clarkef7060e22010-06-03 12:02:55 +01001404 bool SetElementWithCallbackSetterInPrototypes(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001405 void LookupCallback(String* name, LookupResult* result);
1406
1407 // Returns the number of properties on this object filtering out properties
1408 // with the specified attributes (ignoring interceptors).
1409 int NumberOfLocalProperties(PropertyAttributes filter);
1410 // Returns the number of enumerable properties (ignoring interceptors).
1411 int NumberOfEnumProperties();
1412 // Fill in details for properties into storage starting at the specified
1413 // index.
1414 void GetLocalPropertyNames(FixedArray* storage, int index);
1415
1416 // Returns the number of properties on this object filtering out properties
1417 // with the specified attributes (ignoring interceptors).
1418 int NumberOfLocalElements(PropertyAttributes filter);
1419 // Returns the number of enumerable elements (ignoring interceptors).
1420 int NumberOfEnumElements();
1421 // Returns the number of elements on this object filtering out elements
1422 // with the specified attributes (ignoring interceptors).
1423 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1424 // Count and fill in the enumerable elements into storage.
1425 // (storage->length() == NumberOfEnumElements()).
1426 // If storage is NULL, will count the elements without adding
1427 // them to any storage.
1428 // Returns the number of enumerable elements.
1429 int GetEnumElementKeys(FixedArray* storage);
1430
1431 // Add a property to a fast-case object using a map transition to
1432 // new_map.
1433 Object* AddFastPropertyUsingMap(Map* new_map,
1434 String* name,
1435 Object* value);
1436
1437 // Add a constant function property to a fast-case object.
1438 // This leaves a CONSTANT_TRANSITION in the old map, and
1439 // if it is called on a second object with this map, a
1440 // normal property is added instead, with a map transition.
1441 // This avoids the creation of many maps with the same constant
1442 // function, all orphaned.
1443 Object* AddConstantFunctionProperty(String* name,
1444 JSFunction* function,
1445 PropertyAttributes attributes);
1446
1447 Object* ReplaceSlowProperty(String* name,
1448 Object* value,
1449 PropertyAttributes attributes);
1450
1451 // Converts a descriptor of any other type to a real field,
1452 // backed by the properties array. Descriptors of visible
1453 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1454 // Converts the descriptor on the original object's map to a
1455 // map transition, and the the new field is on the object's new map.
1456 Object* ConvertDescriptorToFieldAndMapTransition(
1457 String* name,
1458 Object* new_value,
1459 PropertyAttributes attributes);
1460
1461 // Converts a descriptor of any other type to a real field,
1462 // backed by the properties array. Descriptors of visible
1463 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1464 Object* ConvertDescriptorToField(String* name,
1465 Object* new_value,
1466 PropertyAttributes attributes);
1467
1468 // Add a property to a fast-case object.
1469 Object* AddFastProperty(String* name,
1470 Object* value,
1471 PropertyAttributes attributes);
1472
1473 // Add a property to a slow-case object.
1474 Object* AddSlowProperty(String* name,
1475 Object* value,
1476 PropertyAttributes attributes);
1477
1478 // Add a property to an object.
1479 Object* AddProperty(String* name,
1480 Object* value,
1481 PropertyAttributes attributes);
1482
1483 // Convert the object to use the canonical dictionary
1484 // representation. If the object is expected to have additional properties
1485 // added this number can be indicated to have the backing store allocated to
1486 // an initial capacity for holding these properties.
1487 Object* NormalizeProperties(PropertyNormalizationMode mode,
1488 int expected_additional_properties);
1489 Object* NormalizeElements();
1490
1491 // Transform slow named properties to fast variants.
1492 // Returns failure if allocation failed.
1493 Object* TransformToFastProperties(int unused_property_fields);
1494
1495 // Access fast-case object properties at index.
1496 inline Object* FastPropertyAt(int index);
1497 inline Object* FastPropertyAtPut(int index, Object* value);
1498
1499 // Access to in object properties.
1500 inline Object* InObjectPropertyAt(int index);
1501 inline Object* InObjectPropertyAtPut(int index,
1502 Object* value,
1503 WriteBarrierMode mode
1504 = UPDATE_WRITE_BARRIER);
1505
1506 // initializes the body after properties slot, properties slot is
1507 // initialized by set_properties
1508 // Note: this call does not update write barrier, it is caller's
1509 // reponsibility to ensure that *v* can be collected without WB here.
1510 inline void InitializeBody(int object_size);
1511
1512 // Check whether this object references another object
1513 bool ReferencesObject(Object* obj);
1514
1515 // Casting.
1516 static inline JSObject* cast(Object* obj);
1517
1518 // Dispatched behavior.
1519 void JSObjectIterateBody(int object_size, ObjectVisitor* v);
1520 void JSObjectShortPrint(StringStream* accumulator);
1521#ifdef DEBUG
1522 void JSObjectPrint();
1523 void JSObjectVerify();
1524 void PrintProperties();
1525 void PrintElements();
1526
1527 // Structure for collecting spill information about JSObjects.
1528 class SpillInformation {
1529 public:
1530 void Clear();
1531 void Print();
1532 int number_of_objects_;
1533 int number_of_objects_with_fast_properties_;
1534 int number_of_objects_with_fast_elements_;
1535 int number_of_fast_used_fields_;
1536 int number_of_fast_unused_fields_;
1537 int number_of_slow_used_properties_;
1538 int number_of_slow_unused_properties_;
1539 int number_of_fast_used_elements_;
1540 int number_of_fast_unused_elements_;
1541 int number_of_slow_used_elements_;
1542 int number_of_slow_unused_elements_;
1543 };
1544
1545 void IncrementSpillStatistics(SpillInformation* info);
1546#endif
1547 Object* SlowReverseLookup(Object* value);
1548
Leon Clarkee46be812010-01-19 14:06:41 +00001549 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1550 // Also maximal value of JSArray's length property.
1551 static const uint32_t kMaxElementCount = 0xffffffffu;
1552
Steve Blocka7e24c12009-10-30 11:49:00 +00001553 static const uint32_t kMaxGap = 1024;
1554 static const int kMaxFastElementsLength = 5000;
1555 static const int kInitialMaxFastElementArray = 100000;
1556 static const int kMaxFastProperties = 8;
1557 static const int kMaxInstanceSize = 255 * kPointerSize;
1558 // When extending the backing storage for property values, we increase
1559 // its size by more than the 1 entry necessary, so sequentially adding fields
1560 // to the same object requires fewer allocations and copies.
1561 static const int kFieldsAdded = 3;
1562
1563 // Layout description.
1564 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1565 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1566 static const int kHeaderSize = kElementsOffset + kPointerSize;
1567
1568 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1569
1570 Object* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
1571
1572 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01001573 Object* GetElementWithCallback(Object* receiver,
1574 Object* structure,
1575 uint32_t index,
1576 Object* holder);
1577 Object* SetElementWithCallback(Object* structure,
1578 uint32_t index,
1579 Object* value,
1580 JSObject* holder);
Steve Blocka7e24c12009-10-30 11:49:00 +00001581 Object* SetElementWithInterceptor(uint32_t index, Object* value);
1582 Object* SetElementWithoutInterceptor(uint32_t index, Object* value);
1583
1584 Object* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
1585
1586 Object* DeletePropertyPostInterceptor(String* name, DeleteMode mode);
1587 Object* DeletePropertyWithInterceptor(String* name);
1588
1589 Object* DeleteElementPostInterceptor(uint32_t index, DeleteMode mode);
1590 Object* DeleteElementWithInterceptor(uint32_t index);
1591
1592 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1593 String* name,
1594 bool continue_search);
1595 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1596 String* name,
1597 bool continue_search);
1598 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1599 Object* receiver,
1600 LookupResult* result,
1601 String* name,
1602 bool continue_search);
1603 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1604 LookupResult* result,
1605 String* name,
1606 bool continue_search);
1607
1608 // Returns true if most of the elements backing storage is used.
1609 bool HasDenseElements();
1610
Leon Clarkef7060e22010-06-03 12:02:55 +01001611 bool CanSetCallback(String* name);
1612 Object* SetElementCallback(uint32_t index,
1613 Object* structure,
1614 PropertyAttributes attributes);
1615 Object* SetPropertyCallback(String* name,
1616 Object* structure,
1617 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001618 Object* DefineGetterSetter(String* name, PropertyAttributes attributes);
1619
1620 void LookupInDescriptor(String* name, LookupResult* result);
1621
1622 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1623};
1624
1625
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001626// FixedArray describes fixed-sized arrays with element type Object*.
1627class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001628 public:
1629 // [length]: length of the array.
1630 inline int length();
1631 inline void set_length(int value);
1632
Steve Blocka7e24c12009-10-30 11:49:00 +00001633 // Setter and getter for elements.
1634 inline Object* get(int index);
1635 // Setter that uses write barrier.
1636 inline void set(int index, Object* value);
1637
1638 // Setter that doesn't need write barrier).
1639 inline void set(int index, Smi* value);
1640 // Setter with explicit barrier mode.
1641 inline void set(int index, Object* value, WriteBarrierMode mode);
1642
1643 // Setters for frequently used oddballs located in old space.
1644 inline void set_undefined(int index);
1645 inline void set_null(int index);
1646 inline void set_the_hole(int index);
1647
Steve Block6ded16b2010-05-10 14:33:55 +01001648 // Gives access to raw memory which stores the array's data.
1649 inline Object** data_start();
1650
Steve Blocka7e24c12009-10-30 11:49:00 +00001651 // Copy operations.
1652 inline Object* Copy();
1653 Object* CopySize(int new_length);
1654
1655 // Add the elements of a JSArray to this FixedArray.
1656 Object* AddKeysFromJSArray(JSArray* array);
1657
1658 // Compute the union of this and other.
1659 Object* UnionOfKeys(FixedArray* other);
1660
1661 // Copy a sub array from the receiver to dest.
1662 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1663
1664 // Garbage collection support.
1665 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1666
1667 // Code Generation support.
1668 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1669
1670 // Casting.
1671 static inline FixedArray* cast(Object* obj);
1672
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001673 // Layout description.
1674 // Length is smi tagged when it is stored.
1675 static const int kLengthOffset = HeapObject::kHeaderSize;
1676 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001677
1678 // Maximal allowed size, in bytes, of a single FixedArray.
1679 // Prevents overflowing size computations, as well as extreme memory
1680 // consumption.
1681 static const int kMaxSize = 512 * MB;
1682 // Maximally allowed length of a FixedArray.
1683 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001684
1685 // Dispatched behavior.
1686 int FixedArraySize() { return SizeFor(length()); }
1687 void FixedArrayIterateBody(ObjectVisitor* v);
1688#ifdef DEBUG
1689 void FixedArrayPrint();
1690 void FixedArrayVerify();
1691 // Checks if two FixedArrays have identical contents.
1692 bool IsEqualTo(FixedArray* other);
1693#endif
1694
1695 // Swap two elements in a pair of arrays. If this array and the
1696 // numbers array are the same object, the elements are only swapped
1697 // once.
1698 void SwapPairs(FixedArray* numbers, int i, int j);
1699
1700 // Sort prefix of this array and the numbers array as pairs wrt. the
1701 // numbers. If the numbers array and the this array are the same
1702 // object, the prefix of this array is sorted.
1703 void SortPairs(FixedArray* numbers, uint32_t len);
1704
1705 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001706 // Set operation on FixedArray without using write barriers. Can
1707 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001708 static inline void fast_set(FixedArray* array, int index, Object* value);
1709
1710 private:
1711 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1712};
1713
1714
1715// DescriptorArrays are fixed arrays used to hold instance descriptors.
1716// The format of the these objects is:
1717// [0]: point to a fixed array with (value, detail) pairs.
1718// [1]: next enumeration index (Smi), or pointer to small fixed array:
1719// [0]: next enumeration index (Smi)
1720// [1]: pointer to fixed array with enum cache
1721// [2]: first key
1722// [length() - 1]: last key
1723//
1724class DescriptorArray: public FixedArray {
1725 public:
1726 // Is this the singleton empty_descriptor_array?
1727 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001728
Steve Blocka7e24c12009-10-30 11:49:00 +00001729 // Returns the number of descriptors in the array.
1730 int number_of_descriptors() {
1731 return IsEmpty() ? 0 : length() - kFirstIndex;
1732 }
1733
1734 int NextEnumerationIndex() {
1735 if (IsEmpty()) return PropertyDetails::kInitialIndex;
1736 Object* obj = get(kEnumerationIndexIndex);
1737 if (obj->IsSmi()) {
1738 return Smi::cast(obj)->value();
1739 } else {
1740 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1741 return Smi::cast(index)->value();
1742 }
1743 }
1744
1745 // Set next enumeration index and flush any enum cache.
1746 void SetNextEnumerationIndex(int value) {
1747 if (!IsEmpty()) {
1748 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1749 }
1750 }
1751 bool HasEnumCache() {
1752 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
1753 }
1754
1755 Object* GetEnumCache() {
1756 ASSERT(HasEnumCache());
1757 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1758 return bridge->get(kEnumCacheBridgeCacheIndex);
1759 }
1760
1761 // Initialize or change the enum cache,
1762 // using the supplied storage for the small "bridge".
1763 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1764
1765 // Accessors for fetching instance descriptor at descriptor number.
1766 inline String* GetKey(int descriptor_number);
1767 inline Object* GetValue(int descriptor_number);
1768 inline Smi* GetDetails(int descriptor_number);
1769 inline PropertyType GetType(int descriptor_number);
1770 inline int GetFieldIndex(int descriptor_number);
1771 inline JSFunction* GetConstantFunction(int descriptor_number);
1772 inline Object* GetCallbacksObject(int descriptor_number);
1773 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
1774 inline bool IsProperty(int descriptor_number);
1775 inline bool IsTransition(int descriptor_number);
1776 inline bool IsNullDescriptor(int descriptor_number);
1777 inline bool IsDontEnum(int descriptor_number);
1778
1779 // Accessor for complete descriptor.
1780 inline void Get(int descriptor_number, Descriptor* desc);
1781 inline void Set(int descriptor_number, Descriptor* desc);
1782
1783 // Transfer complete descriptor from another descriptor array to
1784 // this one.
1785 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
1786
1787 // Copy the descriptor array, insert a new descriptor and optionally
1788 // remove map transitions. If the descriptor is already present, it is
1789 // replaced. If a replaced descriptor is a real property (not a transition
1790 // or null), its enumeration index is kept as is.
1791 // If adding a real property, map transitions must be removed. If adding
1792 // a transition, they must not be removed. All null descriptors are removed.
1793 Object* CopyInsert(Descriptor* descriptor, TransitionFlag transition_flag);
1794
1795 // Remove all transitions. Return a copy of the array with all transitions
1796 // removed, or a Failure object if the new array could not be allocated.
1797 Object* RemoveTransitions();
1798
1799 // Sort the instance descriptors by the hash codes of their keys.
1800 void Sort();
1801
1802 // Search the instance descriptors for given name.
1803 inline int Search(String* name);
1804
1805 // Tells whether the name is present int the array.
1806 bool Contains(String* name) { return kNotFound != Search(name); }
1807
1808 // Perform a binary search in the instance descriptors represented
1809 // by this fixed array. low and high are descriptor indices. If there
1810 // are three instance descriptors in this array it should be called
1811 // with low=0 and high=2.
1812 int BinarySearch(String* name, int low, int high);
1813
1814 // Perform a linear search in the instance descriptors represented
1815 // by this fixed array. len is the number of descriptor indices that are
1816 // valid. Does not require the descriptors to be sorted.
1817 int LinearSearch(String* name, int len);
1818
1819 // Allocates a DescriptorArray, but returns the singleton
1820 // empty descriptor array object if number_of_descriptors is 0.
1821 static Object* Allocate(int number_of_descriptors);
1822
1823 // Casting.
1824 static inline DescriptorArray* cast(Object* obj);
1825
1826 // Constant for denoting key was not found.
1827 static const int kNotFound = -1;
1828
1829 static const int kContentArrayIndex = 0;
1830 static const int kEnumerationIndexIndex = 1;
1831 static const int kFirstIndex = 2;
1832
1833 // The length of the "bridge" to the enum cache.
1834 static const int kEnumCacheBridgeLength = 2;
1835 static const int kEnumCacheBridgeEnumIndex = 0;
1836 static const int kEnumCacheBridgeCacheIndex = 1;
1837
1838 // Layout description.
1839 static const int kContentArrayOffset = FixedArray::kHeaderSize;
1840 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
1841 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
1842
1843 // Layout description for the bridge array.
1844 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
1845 static const int kEnumCacheBridgeCacheOffset =
1846 kEnumCacheBridgeEnumOffset + kPointerSize;
1847
1848#ifdef DEBUG
1849 // Print all the descriptors.
1850 void PrintDescriptors();
1851
1852 // Is the descriptor array sorted and without duplicates?
1853 bool IsSortedNoDuplicates();
1854
1855 // Are two DescriptorArrays equal?
1856 bool IsEqualTo(DescriptorArray* other);
1857#endif
1858
1859 // The maximum number of descriptors we want in a descriptor array (should
1860 // fit in a page).
1861 static const int kMaxNumberOfDescriptors = 1024 + 512;
1862
1863 private:
1864 // Conversion from descriptor number to array indices.
1865 static int ToKeyIndex(int descriptor_number) {
1866 return descriptor_number+kFirstIndex;
1867 }
Leon Clarkee46be812010-01-19 14:06:41 +00001868
1869 static int ToDetailsIndex(int descriptor_number) {
1870 return (descriptor_number << 1) + 1;
1871 }
1872
Steve Blocka7e24c12009-10-30 11:49:00 +00001873 static int ToValueIndex(int descriptor_number) {
1874 return descriptor_number << 1;
1875 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001876
1877 bool is_null_descriptor(int descriptor_number) {
1878 return PropertyDetails(GetDetails(descriptor_number)).type() ==
1879 NULL_DESCRIPTOR;
1880 }
1881 // Swap operation on FixedArray without using write barriers.
1882 static inline void fast_swap(FixedArray* array, int first, int second);
1883
1884 // Swap descriptor first and second.
1885 inline void Swap(int first, int second);
1886
1887 FixedArray* GetContentArray() {
1888 return FixedArray::cast(get(kContentArrayIndex));
1889 }
1890 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
1891};
1892
1893
1894// HashTable is a subclass of FixedArray that implements a hash table
1895// that uses open addressing and quadratic probing.
1896//
1897// In order for the quadratic probing to work, elements that have not
1898// yet been used and elements that have been deleted are
1899// distinguished. Probing continues when deleted elements are
1900// encountered and stops when unused elements are encountered.
1901//
1902// - Elements with key == undefined have not been used yet.
1903// - Elements with key == null have been deleted.
1904//
1905// The hash table class is parameterized with a Shape and a Key.
1906// Shape must be a class with the following interface:
1907// class ExampleShape {
1908// public:
1909// // Tells whether key matches other.
1910// static bool IsMatch(Key key, Object* other);
1911// // Returns the hash value for key.
1912// static uint32_t Hash(Key key);
1913// // Returns the hash value for object.
1914// static uint32_t HashForObject(Key key, Object* object);
1915// // Convert key to an object.
1916// static inline Object* AsObject(Key key);
1917// // The prefix size indicates number of elements in the beginning
1918// // of the backing storage.
1919// static const int kPrefixSize = ..;
1920// // The Element size indicates number of elements per entry.
1921// static const int kEntrySize = ..;
1922// };
Steve Block3ce2e202009-11-05 08:53:23 +00001923// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001924// beginning of the backing storage that can be used for non-element
1925// information by subclasses.
1926
1927template<typename Shape, typename Key>
1928class HashTable: public FixedArray {
1929 public:
Steve Block3ce2e202009-11-05 08:53:23 +00001930 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00001931 int NumberOfElements() {
1932 return Smi::cast(get(kNumberOfElementsIndex))->value();
1933 }
1934
Leon Clarkee46be812010-01-19 14:06:41 +00001935 // Returns the number of deleted elements in the hash table.
1936 int NumberOfDeletedElements() {
1937 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
1938 }
1939
Steve Block3ce2e202009-11-05 08:53:23 +00001940 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00001941 int Capacity() {
1942 return Smi::cast(get(kCapacityIndex))->value();
1943 }
1944
1945 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00001946 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00001947 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
1948
1949 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00001950 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00001951 void ElementRemoved() {
1952 SetNumberOfElements(NumberOfElements() - 1);
1953 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
1954 }
1955 void ElementsRemoved(int n) {
1956 SetNumberOfElements(NumberOfElements() - n);
1957 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
1958 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001959
Steve Block3ce2e202009-11-05 08:53:23 +00001960 // Returns a new HashTable object. Might return Failure.
Steve Block6ded16b2010-05-10 14:33:55 +01001961 static Object* Allocate(int at_least_space_for,
1962 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00001963
1964 // Returns the key at entry.
1965 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
1966
1967 // Tells whether k is a real key. Null and undefined are not allowed
1968 // as keys and can be used to indicate missing or deleted elements.
1969 bool IsKey(Object* k) {
1970 return !k->IsNull() && !k->IsUndefined();
1971 }
1972
1973 // Garbage collection support.
1974 void IteratePrefix(ObjectVisitor* visitor);
1975 void IterateElements(ObjectVisitor* visitor);
1976
1977 // Casting.
1978 static inline HashTable* cast(Object* obj);
1979
1980 // Compute the probe offset (quadratic probing).
1981 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
1982 return (n + n * n) >> 1;
1983 }
1984
1985 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00001986 static const int kNumberOfDeletedElementsIndex = 1;
1987 static const int kCapacityIndex = 2;
1988 static const int kPrefixStartIndex = 3;
1989 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00001990 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001991 static const int kEntrySize = Shape::kEntrySize;
1992 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00001993 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01001994 static const int kCapacityOffset =
1995 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001996
1997 // Constant used for denoting a absent entry.
1998 static const int kNotFound = -1;
1999
Leon Clarkee46be812010-01-19 14:06:41 +00002000 // Maximal capacity of HashTable. Based on maximal length of underlying
2001 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2002 // cannot overflow.
2003 static const int kMaxCapacity =
2004 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2005
Steve Blocka7e24c12009-10-30 11:49:00 +00002006 // Find entry for key otherwise return -1.
2007 int FindEntry(Key key);
2008
2009 protected:
2010
2011 // Find the entry at which to insert element with the given key that
2012 // has the given hash value.
2013 uint32_t FindInsertionEntry(uint32_t hash);
2014
2015 // Returns the index for an entry (of the key)
2016 static inline int EntryToIndex(int entry) {
2017 return (entry * kEntrySize) + kElementsStartIndex;
2018 }
2019
Steve Block3ce2e202009-11-05 08:53:23 +00002020 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002021 void SetNumberOfElements(int nof) {
2022 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2023 }
2024
Leon Clarkee46be812010-01-19 14:06:41 +00002025 // Update the number of deleted elements in the hash table.
2026 void SetNumberOfDeletedElements(int nod) {
2027 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2028 }
2029
Steve Blocka7e24c12009-10-30 11:49:00 +00002030 // Sets the capacity of the hash table.
2031 void SetCapacity(int capacity) {
2032 // To scale a computed hash code to fit within the hash table, we
2033 // use bit-wise AND with a mask, so the capacity must be positive
2034 // and non-zero.
2035 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002036 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002037 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2038 }
2039
2040
2041 // Returns probe entry.
2042 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2043 ASSERT(IsPowerOf2(size));
2044 return (hash + GetProbeOffset(number)) & (size - 1);
2045 }
2046
Leon Clarkee46be812010-01-19 14:06:41 +00002047 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2048 return hash & (size - 1);
2049 }
2050
2051 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2052 return (last + number) & (size - 1);
2053 }
2054
Steve Blocka7e24c12009-10-30 11:49:00 +00002055 // Ensure enough space for n additional elements.
2056 Object* EnsureCapacity(int n, Key key);
2057};
2058
2059
2060
2061// HashTableKey is an abstract superclass for virtual key behavior.
2062class HashTableKey {
2063 public:
2064 // Returns whether the other object matches this key.
2065 virtual bool IsMatch(Object* other) = 0;
2066 // Returns the hash value for this key.
2067 virtual uint32_t Hash() = 0;
2068 // Returns the hash value for object.
2069 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002070 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002071 // If allocations fails a failure object is returned.
2072 virtual Object* AsObject() = 0;
2073 // Required.
2074 virtual ~HashTableKey() {}
2075};
2076
2077class SymbolTableShape {
2078 public:
2079 static bool IsMatch(HashTableKey* key, Object* value) {
2080 return key->IsMatch(value);
2081 }
2082 static uint32_t Hash(HashTableKey* key) {
2083 return key->Hash();
2084 }
2085 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2086 return key->HashForObject(object);
2087 }
2088 static Object* AsObject(HashTableKey* key) {
2089 return key->AsObject();
2090 }
2091
2092 static const int kPrefixSize = 0;
2093 static const int kEntrySize = 1;
2094};
2095
2096// SymbolTable.
2097//
2098// No special elements in the prefix and the element size is 1
2099// because only the symbol itself (the key) needs to be stored.
2100class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2101 public:
2102 // Find symbol in the symbol table. If it is not there yet, it is
2103 // added. The return value is the symbol table which might have
2104 // been enlarged. If the return value is not a failure, the symbol
2105 // pointer *s is set to the symbol found.
2106 Object* LookupSymbol(Vector<const char> str, Object** s);
2107 Object* LookupString(String* key, Object** s);
2108
2109 // Looks up a symbol that is equal to the given string and returns
2110 // true if it is found, assigning the symbol to the given output
2111 // parameter.
2112 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002113 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002114
2115 // Casting.
2116 static inline SymbolTable* cast(Object* obj);
2117
2118 private:
2119 Object* LookupKey(HashTableKey* key, Object** s);
2120
2121 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2122};
2123
2124
2125class MapCacheShape {
2126 public:
2127 static bool IsMatch(HashTableKey* key, Object* value) {
2128 return key->IsMatch(value);
2129 }
2130 static uint32_t Hash(HashTableKey* key) {
2131 return key->Hash();
2132 }
2133
2134 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2135 return key->HashForObject(object);
2136 }
2137
2138 static Object* AsObject(HashTableKey* key) {
2139 return key->AsObject();
2140 }
2141
2142 static const int kPrefixSize = 0;
2143 static const int kEntrySize = 2;
2144};
2145
2146
2147// MapCache.
2148//
2149// Maps keys that are a fixed array of symbols to a map.
2150// Used for canonicalize maps for object literals.
2151class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2152 public:
2153 // Find cached value for a string key, otherwise return null.
2154 Object* Lookup(FixedArray* key);
2155 Object* Put(FixedArray* key, Map* value);
2156 static inline MapCache* cast(Object* obj);
2157
2158 private:
2159 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2160};
2161
2162
2163template <typename Shape, typename Key>
2164class Dictionary: public HashTable<Shape, Key> {
2165 public:
2166
2167 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2168 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2169 }
2170
2171 // Returns the value at entry.
2172 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002173 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002174 }
2175
2176 // Set the value for entry.
2177 void ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002178 // Check that this value can actually be written.
2179 PropertyDetails details = DetailsAt(entry);
2180 // If a value has not been initilized we allow writing to it even if
2181 // it is read only (a declared const that has not been initialized).
2182 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) return;
Steve Block6ded16b2010-05-10 14:33:55 +01002183 this->set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002184 }
2185
2186 // Returns the property details for the property at entry.
2187 PropertyDetails DetailsAt(int entry) {
2188 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2189 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002190 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002191 }
2192
2193 // Set the details for entry.
2194 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002195 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002196 }
2197
2198 // Sorting support
2199 void CopyValuesTo(FixedArray* elements);
2200
2201 // Delete a property from the dictionary.
2202 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2203
2204 // Returns the number of elements in the dictionary filtering out properties
2205 // with the specified attributes.
2206 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2207
2208 // Returns the number of enumerable elements in the dictionary.
2209 int NumberOfEnumElements();
2210
2211 // Copies keys to preallocated fixed array.
2212 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2213 // Fill in details for properties into storage.
2214 void CopyKeysTo(FixedArray* storage);
2215
2216 // Accessors for next enumeration index.
2217 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002218 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002219 }
2220
2221 int NextEnumerationIndex() {
2222 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2223 }
2224
2225 // Returns a new array for dictionary usage. Might return Failure.
2226 static Object* Allocate(int at_least_space_for);
2227
2228 // Ensure enough space for n additional elements.
2229 Object* EnsureCapacity(int n, Key key);
2230
2231#ifdef DEBUG
2232 void Print();
2233#endif
2234 // Returns the key (slow).
2235 Object* SlowReverseLookup(Object* value);
2236
2237 // Sets the entry to (key, value) pair.
2238 inline void SetEntry(int entry,
2239 Object* key,
2240 Object* value,
2241 PropertyDetails details);
2242
2243 Object* Add(Key key, Object* value, PropertyDetails details);
2244
2245 protected:
2246 // Generic at put operation.
2247 Object* AtPut(Key key, Object* value);
2248
2249 // Add entry to dictionary.
2250 Object* AddEntry(Key key,
2251 Object* value,
2252 PropertyDetails details,
2253 uint32_t hash);
2254
2255 // Generate new enumeration indices to avoid enumeration index overflow.
2256 Object* GenerateNewEnumerationIndices();
2257 static const int kMaxNumberKeyIndex =
2258 HashTable<Shape, Key>::kPrefixStartIndex;
2259 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2260};
2261
2262
2263class StringDictionaryShape {
2264 public:
2265 static inline bool IsMatch(String* key, Object* other);
2266 static inline uint32_t Hash(String* key);
2267 static inline uint32_t HashForObject(String* key, Object* object);
2268 static inline Object* AsObject(String* key);
2269 static const int kPrefixSize = 2;
2270 static const int kEntrySize = 3;
2271 static const bool kIsEnumerable = true;
2272};
2273
2274
2275class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2276 public:
2277 static inline StringDictionary* cast(Object* obj) {
2278 ASSERT(obj->IsDictionary());
2279 return reinterpret_cast<StringDictionary*>(obj);
2280 }
2281
2282 // Copies enumerable keys to preallocated fixed array.
2283 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2284
2285 // For transforming properties of a JSObject.
2286 Object* TransformPropertiesToFastFor(JSObject* obj,
2287 int unused_property_fields);
2288};
2289
2290
2291class NumberDictionaryShape {
2292 public:
2293 static inline bool IsMatch(uint32_t key, Object* other);
2294 static inline uint32_t Hash(uint32_t key);
2295 static inline uint32_t HashForObject(uint32_t key, Object* object);
2296 static inline Object* AsObject(uint32_t key);
2297 static const int kPrefixSize = 2;
2298 static const int kEntrySize = 3;
2299 static const bool kIsEnumerable = false;
2300};
2301
2302
2303class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2304 public:
2305 static NumberDictionary* cast(Object* obj) {
2306 ASSERT(obj->IsDictionary());
2307 return reinterpret_cast<NumberDictionary*>(obj);
2308 }
2309
2310 // Type specific at put (default NONE attributes is used when adding).
2311 Object* AtNumberPut(uint32_t key, Object* value);
2312 Object* AddNumberEntry(uint32_t key,
2313 Object* value,
2314 PropertyDetails details);
2315
2316 // Set an existing entry or add a new one if needed.
2317 Object* Set(uint32_t key, Object* value, PropertyDetails details);
2318
2319 void UpdateMaxNumberKey(uint32_t key);
2320
2321 // If slow elements are required we will never go back to fast-case
2322 // for the elements kept in this dictionary. We require slow
2323 // elements if an element has been added at an index larger than
2324 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2325 // when defining a getter or setter with a number key.
2326 inline bool requires_slow_elements();
2327 inline void set_requires_slow_elements();
2328
2329 // Get the value of the max number key that has been added to this
2330 // dictionary. max_number_key can only be called if
2331 // requires_slow_elements returns false.
2332 inline uint32_t max_number_key();
2333
2334 // Remove all entries were key is a number and (from <= key && key < to).
2335 void RemoveNumberEntries(uint32_t from, uint32_t to);
2336
2337 // Bit masks.
2338 static const int kRequiresSlowElementsMask = 1;
2339 static const int kRequiresSlowElementsTagSize = 1;
2340 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2341};
2342
2343
Steve Block6ded16b2010-05-10 14:33:55 +01002344// JSFunctionResultCache caches results of some JSFunction invocation.
2345// It is a fixed array with fixed structure:
2346// [0]: factory function
2347// [1]: finger index
2348// [2]: current cache size
2349// [3]: dummy field.
2350// The rest of array are key/value pairs.
2351class JSFunctionResultCache: public FixedArray {
2352 public:
2353 static const int kFactoryIndex = 0;
2354 static const int kFingerIndex = kFactoryIndex + 1;
2355 static const int kCacheSizeIndex = kFingerIndex + 1;
2356 static const int kDummyIndex = kCacheSizeIndex + 1;
2357 static const int kEntriesIndex = kDummyIndex + 1;
2358
2359 static const int kEntrySize = 2; // key + value
2360
Kristian Monsen25f61362010-05-21 11:50:48 +01002361 static const int kFactoryOffset = kHeaderSize;
2362 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2363 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2364
Steve Block6ded16b2010-05-10 14:33:55 +01002365 inline void MakeZeroSize();
2366 inline void Clear();
2367
2368 // Casting
2369 static inline JSFunctionResultCache* cast(Object* obj);
2370
2371#ifdef DEBUG
2372 void JSFunctionResultCacheVerify();
2373#endif
2374};
2375
2376
Steve Blocka7e24c12009-10-30 11:49:00 +00002377// ByteArray represents fixed sized byte arrays. Used by the outside world,
2378// such as PCRE, and also by the memory allocator and garbage collector to
2379// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002380class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002381 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002382 // [length]: length of the array.
2383 inline int length();
2384 inline void set_length(int value);
2385
Steve Blocka7e24c12009-10-30 11:49:00 +00002386 // Setter and getter.
2387 inline byte get(int index);
2388 inline void set(int index, byte value);
2389
2390 // Treat contents as an int array.
2391 inline int get_int(int index);
2392
2393 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002394 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002395 }
2396 // We use byte arrays for free blocks in the heap. Given a desired size in
2397 // bytes that is a multiple of the word size and big enough to hold a byte
2398 // array, this function returns the number of elements a byte array should
2399 // have.
2400 static int LengthFor(int size_in_bytes) {
2401 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2402 ASSERT(size_in_bytes >= kHeaderSize);
2403 return size_in_bytes - kHeaderSize;
2404 }
2405
2406 // Returns data start address.
2407 inline Address GetDataStartAddress();
2408
2409 // Returns a pointer to the ByteArray object for a given data start address.
2410 static inline ByteArray* FromDataStartAddress(Address address);
2411
2412 // Casting.
2413 static inline ByteArray* cast(Object* obj);
2414
2415 // Dispatched behavior.
2416 int ByteArraySize() { return SizeFor(length()); }
2417#ifdef DEBUG
2418 void ByteArrayPrint();
2419 void ByteArrayVerify();
2420#endif
2421
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002422 // Layout description.
2423 // Length is smi tagged when it is stored.
2424 static const int kLengthOffset = HeapObject::kHeaderSize;
2425 static const int kHeaderSize = kLengthOffset + kPointerSize;
2426
2427 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002428
Leon Clarkee46be812010-01-19 14:06:41 +00002429 // Maximal memory consumption for a single ByteArray.
2430 static const int kMaxSize = 512 * MB;
2431 // Maximal length of a single ByteArray.
2432 static const int kMaxLength = kMaxSize - kHeaderSize;
2433
Steve Blocka7e24c12009-10-30 11:49:00 +00002434 private:
2435 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2436};
2437
2438
2439// A PixelArray represents a fixed-size byte array with special semantics
2440// used for implementing the CanvasPixelArray object. Please see the
2441// specification at:
2442// http://www.whatwg.org/specs/web-apps/current-work/
2443// multipage/the-canvas-element.html#canvaspixelarray
2444// In particular, write access clamps the value written to 0 or 255 if the
2445// value written is outside this range.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002446class PixelArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002447 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002448 // [length]: length of the array.
2449 inline int length();
2450 inline void set_length(int value);
2451
Steve Blocka7e24c12009-10-30 11:49:00 +00002452 // [external_pointer]: The pointer to the external memory area backing this
2453 // pixel array.
2454 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2455
2456 // Setter and getter.
2457 inline uint8_t get(int index);
2458 inline void set(int index, uint8_t value);
2459
2460 // This accessor applies the correct conversion from Smi, HeapNumber and
2461 // undefined and clamps the converted value between 0 and 255.
2462 Object* SetValue(uint32_t index, Object* value);
2463
2464 // Casting.
2465 static inline PixelArray* cast(Object* obj);
2466
2467#ifdef DEBUG
2468 void PixelArrayPrint();
2469 void PixelArrayVerify();
2470#endif // DEBUG
2471
Steve Block3ce2e202009-11-05 08:53:23 +00002472 // Maximal acceptable length for a pixel array.
2473 static const int kMaxLength = 0x3fffffff;
2474
Steve Blocka7e24c12009-10-30 11:49:00 +00002475 // PixelArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002476 static const int kLengthOffset = HeapObject::kHeaderSize;
2477 static const int kExternalPointerOffset =
2478 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002479 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002480 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002481
2482 private:
2483 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2484};
2485
2486
Steve Block3ce2e202009-11-05 08:53:23 +00002487// An ExternalArray represents a fixed-size array of primitive values
2488// which live outside the JavaScript heap. Its subclasses are used to
2489// implement the CanvasArray types being defined in the WebGL
2490// specification. As of this writing the first public draft is not yet
2491// available, but Khronos members can access the draft at:
2492// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2493//
2494// The semantics of these arrays differ from CanvasPixelArray.
2495// Out-of-range values passed to the setter are converted via a C
2496// cast, not clamping. Out-of-range indices cause exceptions to be
2497// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002498class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002499 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002500 // [length]: length of the array.
2501 inline int length();
2502 inline void set_length(int value);
2503
Steve Block3ce2e202009-11-05 08:53:23 +00002504 // [external_pointer]: The pointer to the external memory area backing this
2505 // external array.
2506 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2507
2508 // Casting.
2509 static inline ExternalArray* cast(Object* obj);
2510
2511 // Maximal acceptable length for an external array.
2512 static const int kMaxLength = 0x3fffffff;
2513
2514 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002515 static const int kLengthOffset = HeapObject::kHeaderSize;
2516 static const int kExternalPointerOffset =
2517 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002518 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002519 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002520
2521 private:
2522 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2523};
2524
2525
2526class ExternalByteArray: public ExternalArray {
2527 public:
2528 // Setter and getter.
2529 inline int8_t get(int index);
2530 inline void set(int index, int8_t value);
2531
2532 // This accessor applies the correct conversion from Smi, HeapNumber
2533 // and undefined.
2534 Object* SetValue(uint32_t index, Object* value);
2535
2536 // Casting.
2537 static inline ExternalByteArray* cast(Object* obj);
2538
2539#ifdef DEBUG
2540 void ExternalByteArrayPrint();
2541 void ExternalByteArrayVerify();
2542#endif // DEBUG
2543
2544 private:
2545 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2546};
2547
2548
2549class ExternalUnsignedByteArray: public ExternalArray {
2550 public:
2551 // Setter and getter.
2552 inline uint8_t get(int index);
2553 inline void set(int index, uint8_t value);
2554
2555 // This accessor applies the correct conversion from Smi, HeapNumber
2556 // and undefined.
2557 Object* SetValue(uint32_t index, Object* value);
2558
2559 // Casting.
2560 static inline ExternalUnsignedByteArray* cast(Object* obj);
2561
2562#ifdef DEBUG
2563 void ExternalUnsignedByteArrayPrint();
2564 void ExternalUnsignedByteArrayVerify();
2565#endif // DEBUG
2566
2567 private:
2568 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2569};
2570
2571
2572class ExternalShortArray: public ExternalArray {
2573 public:
2574 // Setter and getter.
2575 inline int16_t get(int index);
2576 inline void set(int index, int16_t value);
2577
2578 // This accessor applies the correct conversion from Smi, HeapNumber
2579 // and undefined.
2580 Object* SetValue(uint32_t index, Object* value);
2581
2582 // Casting.
2583 static inline ExternalShortArray* cast(Object* obj);
2584
2585#ifdef DEBUG
2586 void ExternalShortArrayPrint();
2587 void ExternalShortArrayVerify();
2588#endif // DEBUG
2589
2590 private:
2591 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2592};
2593
2594
2595class ExternalUnsignedShortArray: public ExternalArray {
2596 public:
2597 // Setter and getter.
2598 inline uint16_t get(int index);
2599 inline void set(int index, uint16_t value);
2600
2601 // This accessor applies the correct conversion from Smi, HeapNumber
2602 // and undefined.
2603 Object* SetValue(uint32_t index, Object* value);
2604
2605 // Casting.
2606 static inline ExternalUnsignedShortArray* cast(Object* obj);
2607
2608#ifdef DEBUG
2609 void ExternalUnsignedShortArrayPrint();
2610 void ExternalUnsignedShortArrayVerify();
2611#endif // DEBUG
2612
2613 private:
2614 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2615};
2616
2617
2618class ExternalIntArray: public ExternalArray {
2619 public:
2620 // Setter and getter.
2621 inline int32_t get(int index);
2622 inline void set(int index, int32_t value);
2623
2624 // This accessor applies the correct conversion from Smi, HeapNumber
2625 // and undefined.
2626 Object* SetValue(uint32_t index, Object* value);
2627
2628 // Casting.
2629 static inline ExternalIntArray* cast(Object* obj);
2630
2631#ifdef DEBUG
2632 void ExternalIntArrayPrint();
2633 void ExternalIntArrayVerify();
2634#endif // DEBUG
2635
2636 private:
2637 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
2638};
2639
2640
2641class ExternalUnsignedIntArray: public ExternalArray {
2642 public:
2643 // Setter and getter.
2644 inline uint32_t get(int index);
2645 inline void set(int index, uint32_t value);
2646
2647 // This accessor applies the correct conversion from Smi, HeapNumber
2648 // and undefined.
2649 Object* SetValue(uint32_t index, Object* value);
2650
2651 // Casting.
2652 static inline ExternalUnsignedIntArray* cast(Object* obj);
2653
2654#ifdef DEBUG
2655 void ExternalUnsignedIntArrayPrint();
2656 void ExternalUnsignedIntArrayVerify();
2657#endif // DEBUG
2658
2659 private:
2660 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
2661};
2662
2663
2664class ExternalFloatArray: public ExternalArray {
2665 public:
2666 // Setter and getter.
2667 inline float get(int index);
2668 inline void set(int index, float value);
2669
2670 // This accessor applies the correct conversion from Smi, HeapNumber
2671 // and undefined.
2672 Object* SetValue(uint32_t index, Object* value);
2673
2674 // Casting.
2675 static inline ExternalFloatArray* cast(Object* obj);
2676
2677#ifdef DEBUG
2678 void ExternalFloatArrayPrint();
2679 void ExternalFloatArrayVerify();
2680#endif // DEBUG
2681
2682 private:
2683 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
2684};
2685
2686
Steve Blocka7e24c12009-10-30 11:49:00 +00002687// Code describes objects with on-the-fly generated machine code.
2688class Code: public HeapObject {
2689 public:
2690 // Opaque data type for encapsulating code flags like kind, inline
2691 // cache state, and arguments count.
2692 enum Flags { };
2693
2694 enum Kind {
2695 FUNCTION,
2696 STUB,
2697 BUILTIN,
2698 LOAD_IC,
2699 KEYED_LOAD_IC,
2700 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002701 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00002702 STORE_IC,
2703 KEYED_STORE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002704 BINARY_OP_IC,
2705 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00002706 // Flags.
2707
2708 // Pseudo-kinds.
2709 REGEXP = BUILTIN,
2710 FIRST_IC_KIND = LOAD_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002711 LAST_IC_KIND = BINARY_OP_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00002712 };
2713
2714 enum {
2715 NUMBER_OF_KINDS = KEYED_STORE_IC + 1
2716 };
2717
2718#ifdef ENABLE_DISASSEMBLER
2719 // Printing
2720 static const char* Kind2String(Kind kind);
2721 static const char* ICState2String(InlineCacheState state);
2722 static const char* PropertyType2String(PropertyType type);
2723 void Disassemble(const char* name);
2724#endif // ENABLE_DISASSEMBLER
2725
2726 // [instruction_size]: Size of the native instructions
2727 inline int instruction_size();
2728 inline void set_instruction_size(int value);
2729
2730 // [relocation_size]: Size of relocation information.
2731 inline int relocation_size();
2732 inline void set_relocation_size(int value);
2733
2734 // [sinfo_size]: Size of scope information.
2735 inline int sinfo_size();
2736 inline void set_sinfo_size(int value);
2737
2738 // [flags]: Various code flags.
2739 inline Flags flags();
2740 inline void set_flags(Flags flags);
2741
2742 // [flags]: Access to specific code flags.
2743 inline Kind kind();
2744 inline InlineCacheState ic_state(); // Only valid for IC stubs.
2745 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
2746 inline PropertyType type(); // Only valid for monomorphic IC stubs.
2747 inline int arguments_count(); // Only valid for call IC stubs.
2748
2749 // Testers for IC stub kinds.
2750 inline bool is_inline_cache_stub();
2751 inline bool is_load_stub() { return kind() == LOAD_IC; }
2752 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
2753 inline bool is_store_stub() { return kind() == STORE_IC; }
2754 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
2755 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002756 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00002757
Steve Block6ded16b2010-05-10 14:33:55 +01002758 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Steve Blocka7e24c12009-10-30 11:49:00 +00002759 inline CodeStub::Major major_key();
2760 inline void set_major_key(CodeStub::Major major);
2761
2762 // Flags operations.
2763 static inline Flags ComputeFlags(Kind kind,
2764 InLoopFlag in_loop = NOT_IN_LOOP,
2765 InlineCacheState ic_state = UNINITIALIZED,
2766 PropertyType type = NORMAL,
2767 int argc = -1);
2768
2769 static inline Flags ComputeMonomorphicFlags(
2770 Kind kind,
2771 PropertyType type,
2772 InLoopFlag in_loop = NOT_IN_LOOP,
2773 int argc = -1);
2774
2775 static inline Kind ExtractKindFromFlags(Flags flags);
2776 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
2777 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
2778 static inline PropertyType ExtractTypeFromFlags(Flags flags);
2779 static inline int ExtractArgumentsCountFromFlags(Flags flags);
2780 static inline Flags RemoveTypeFromFlags(Flags flags);
2781
2782 // Convert a target address into a code object.
2783 static inline Code* GetCodeFromTargetAddress(Address address);
2784
2785 // Returns the address of the first instruction.
2786 inline byte* instruction_start();
2787
2788 // Returns the size of the instructions, padding, and relocation information.
2789 inline int body_size();
2790
2791 // Returns the address of the first relocation info (read backwards!).
2792 inline byte* relocation_start();
2793
2794 // Code entry point.
2795 inline byte* entry();
2796
2797 // Returns true if pc is inside this object's instructions.
2798 inline bool contains(byte* pc);
2799
2800 // Returns the address of the scope information.
2801 inline byte* sinfo_start();
2802
2803 // Relocate the code by delta bytes. Called to signal that this code
2804 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002805 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002806
2807 // Migrate code described by desc.
2808 void CopyFrom(const CodeDesc& desc);
2809
2810 // Returns the object size for a given body and sinfo size (Used for
2811 // allocation).
2812 static int SizeFor(int body_size, int sinfo_size) {
2813 ASSERT_SIZE_TAG_ALIGNED(body_size);
2814 ASSERT_SIZE_TAG_ALIGNED(sinfo_size);
2815 return RoundUp(kHeaderSize + body_size + sinfo_size, kCodeAlignment);
2816 }
2817
2818 // Calculate the size of the code object to report for log events. This takes
2819 // the layout of the code object into account.
2820 int ExecutableSize() {
2821 // Check that the assumptions about the layout of the code object holds.
2822 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
2823 Code::kHeaderSize);
2824 return instruction_size() + Code::kHeaderSize;
2825 }
2826
2827 // Locating source position.
2828 int SourcePosition(Address pc);
2829 int SourceStatementPosition(Address pc);
2830
2831 // Casting.
2832 static inline Code* cast(Object* obj);
2833
2834 // Dispatched behavior.
2835 int CodeSize() { return SizeFor(body_size(), sinfo_size()); }
2836 void CodeIterateBody(ObjectVisitor* v);
2837#ifdef DEBUG
2838 void CodePrint();
2839 void CodeVerify();
2840#endif
2841 // Code entry points are aligned to 32 bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002842 static const int kCodeAlignmentBits = 5;
2843 static const int kCodeAlignment = 1 << kCodeAlignmentBits;
Steve Blocka7e24c12009-10-30 11:49:00 +00002844 static const int kCodeAlignmentMask = kCodeAlignment - 1;
2845
2846 // Layout description.
2847 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
2848 static const int kRelocationSizeOffset = kInstructionSizeOffset + kIntSize;
2849 static const int kSInfoSizeOffset = kRelocationSizeOffset + kIntSize;
2850 static const int kFlagsOffset = kSInfoSizeOffset + kIntSize;
2851 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
2852 // Add padding to align the instruction start following right after
2853 // the Code object header.
2854 static const int kHeaderSize =
2855 (kKindSpecificFlagsOffset + kIntSize + kCodeAlignmentMask) &
2856 ~kCodeAlignmentMask;
2857
2858 // Byte offsets within kKindSpecificFlagsOffset.
2859 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset + 1;
2860
2861 // Flags layout.
2862 static const int kFlagsICStateShift = 0;
2863 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002864 static const int kFlagsTypeShift = 4;
2865 static const int kFlagsKindShift = 7;
Steve Block6ded16b2010-05-10 14:33:55 +01002866 static const int kFlagsArgumentsCountShift = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00002867
Steve Block6ded16b2010-05-10 14:33:55 +01002868 static const int kFlagsICStateMask = 0x00000007; // 00000000111
2869 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002870 static const int kFlagsTypeMask = 0x00000070; // 00001110000
2871 static const int kFlagsKindMask = 0x00000780; // 11110000000
Steve Block6ded16b2010-05-10 14:33:55 +01002872 static const int kFlagsArgumentsCountMask = 0xFFFFF800;
Steve Blocka7e24c12009-10-30 11:49:00 +00002873
2874 static const int kFlagsNotUsedInLookup =
2875 (kFlagsICInLoopMask | kFlagsTypeMask);
2876
2877 private:
2878 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
2879};
2880
2881
2882// All heap objects have a Map that describes their structure.
2883// A Map contains information about:
2884// - Size information about the object
2885// - How to iterate over an object (for garbage collection)
2886class Map: public HeapObject {
2887 public:
2888 // Instance size.
2889 inline int instance_size();
2890 inline void set_instance_size(int value);
2891
2892 // Count of properties allocated in the object.
2893 inline int inobject_properties();
2894 inline void set_inobject_properties(int value);
2895
2896 // Count of property fields pre-allocated in the object when first allocated.
2897 inline int pre_allocated_property_fields();
2898 inline void set_pre_allocated_property_fields(int value);
2899
2900 // Instance type.
2901 inline InstanceType instance_type();
2902 inline void set_instance_type(InstanceType value);
2903
2904 // Tells how many unused property fields are available in the
2905 // instance (only used for JSObject in fast mode).
2906 inline int unused_property_fields();
2907 inline void set_unused_property_fields(int value);
2908
2909 // Bit field.
2910 inline byte bit_field();
2911 inline void set_bit_field(byte value);
2912
2913 // Bit field 2.
2914 inline byte bit_field2();
2915 inline void set_bit_field2(byte value);
2916
2917 // Tells whether the object in the prototype property will be used
2918 // for instances created from this function. If the prototype
2919 // property is set to a value that is not a JSObject, the prototype
2920 // property will not be used to create instances of the function.
2921 // See ECMA-262, 13.2.2.
2922 inline void set_non_instance_prototype(bool value);
2923 inline bool has_non_instance_prototype();
2924
Steve Block6ded16b2010-05-10 14:33:55 +01002925 // Tells whether function has special prototype property. If not, prototype
2926 // property will not be created when accessed (will return undefined),
2927 // and construction from this function will not be allowed.
2928 inline void set_function_with_prototype(bool value);
2929 inline bool function_with_prototype();
2930
Steve Blocka7e24c12009-10-30 11:49:00 +00002931 // Tells whether the instance with this map should be ignored by the
2932 // __proto__ accessor.
2933 inline void set_is_hidden_prototype() {
2934 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
2935 }
2936
2937 inline bool is_hidden_prototype() {
2938 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
2939 }
2940
2941 // Records and queries whether the instance has a named interceptor.
2942 inline void set_has_named_interceptor() {
2943 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
2944 }
2945
2946 inline bool has_named_interceptor() {
2947 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
2948 }
2949
2950 // Records and queries whether the instance has an indexed interceptor.
2951 inline void set_has_indexed_interceptor() {
2952 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
2953 }
2954
2955 inline bool has_indexed_interceptor() {
2956 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
2957 }
2958
2959 // Tells whether the instance is undetectable.
2960 // An undetectable object is a special class of JSObject: 'typeof' operator
2961 // returns undefined, ToBoolean returns false. Otherwise it behaves like
2962 // a normal JS object. It is useful for implementing undetectable
2963 // document.all in Firefox & Safari.
2964 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
2965 inline void set_is_undetectable() {
2966 set_bit_field(bit_field() | (1 << kIsUndetectable));
2967 }
2968
2969 inline bool is_undetectable() {
2970 return ((1 << kIsUndetectable) & bit_field()) != 0;
2971 }
2972
Steve Blocka7e24c12009-10-30 11:49:00 +00002973 // Tells whether the instance has a call-as-function handler.
2974 inline void set_has_instance_call_handler() {
2975 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
2976 }
2977
2978 inline bool has_instance_call_handler() {
2979 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
2980 }
2981
Leon Clarkee46be812010-01-19 14:06:41 +00002982 inline void set_is_extensible() {
2983 set_bit_field2(bit_field2() | (1 << kIsExtensible));
2984 }
2985
2986 inline bool is_extensible() {
2987 return ((1 << kIsExtensible) & bit_field2()) != 0;
2988 }
2989
Steve Blocka7e24c12009-10-30 11:49:00 +00002990 // Tells whether the instance needs security checks when accessing its
2991 // properties.
2992 inline void set_is_access_check_needed(bool access_check_needed);
2993 inline bool is_access_check_needed();
2994
2995 // [prototype]: implicit prototype object.
2996 DECL_ACCESSORS(prototype, Object)
2997
2998 // [constructor]: points back to the function responsible for this map.
2999 DECL_ACCESSORS(constructor, Object)
3000
3001 // [instance descriptors]: describes the object.
3002 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3003
3004 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003005 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003006
Steve Blocka7e24c12009-10-30 11:49:00 +00003007 Object* CopyDropDescriptors();
3008
3009 // Returns a copy of the map, with all transitions dropped from the
3010 // instance descriptors.
3011 Object* CopyDropTransitions();
3012
3013 // Returns the property index for name (only valid for FAST MODE).
3014 int PropertyIndexFor(String* name);
3015
3016 // Returns the next free property index (only valid for FAST MODE).
3017 int NextFreePropertyIndex();
3018
3019 // Returns the number of properties described in instance_descriptors.
3020 int NumberOfDescribedProperties();
3021
3022 // Casting.
3023 static inline Map* cast(Object* obj);
3024
3025 // Locate an accessor in the instance descriptor.
3026 AccessorDescriptor* FindAccessor(String* name);
3027
3028 // Code cache operations.
3029
3030 // Clears the code cache.
3031 inline void ClearCodeCache();
3032
3033 // Update code cache.
3034 Object* UpdateCodeCache(String* name, Code* code);
3035
3036 // Returns the found code or undefined if absent.
3037 Object* FindInCodeCache(String* name, Code::Flags flags);
3038
3039 // Returns the non-negative index of the code object if it is in the
3040 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003041 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003042
3043 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003044 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003045
3046 // For every transition in this map, makes the transition's
3047 // target's prototype pointer point back to this map.
3048 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3049 void CreateBackPointers();
3050
3051 // Set all map transitions from this map to dead maps to null.
3052 // Also, restore the original prototype on the targets of these
3053 // transitions, so that we do not process this map again while
3054 // following back pointers.
3055 void ClearNonLiveTransitions(Object* real_prototype);
3056
3057 // Dispatched behavior.
3058 void MapIterateBody(ObjectVisitor* v);
3059#ifdef DEBUG
3060 void MapPrint();
3061 void MapVerify();
3062#endif
3063
3064 static const int kMaxPreAllocatedPropertyFields = 255;
3065
3066 // Layout description.
3067 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3068 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3069 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3070 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3071 static const int kInstanceDescriptorsOffset =
3072 kConstructorOffset + kPointerSize;
3073 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00003074 static const int kPadStart = kCodeCacheOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003075 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3076
3077 // Layout of pointer fields. Heap iteration code relies on them
3078 // being continiously allocated.
3079 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3080 static const int kPointerFieldsEndOffset =
3081 Map::kCodeCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003082
3083 // Byte offsets within kInstanceSizesOffset.
3084 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3085 static const int kInObjectPropertiesByte = 1;
3086 static const int kInObjectPropertiesOffset =
3087 kInstanceSizesOffset + kInObjectPropertiesByte;
3088 static const int kPreAllocatedPropertyFieldsByte = 2;
3089 static const int kPreAllocatedPropertyFieldsOffset =
3090 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
3091 // The byte at position 3 is not in use at the moment.
3092
3093 // Byte offsets within kInstanceAttributesOffset attributes.
3094 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3095 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3096 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3097 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3098
3099 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3100
3101 // Bit positions for bit field.
3102 static const int kUnused = 0; // To be used for marking recently used maps.
3103 static const int kHasNonInstancePrototype = 1;
3104 static const int kIsHiddenPrototype = 2;
3105 static const int kHasNamedInterceptor = 3;
3106 static const int kHasIndexedInterceptor = 4;
3107 static const int kIsUndetectable = 5;
3108 static const int kHasInstanceCallHandler = 6;
3109 static const int kIsAccessCheckNeeded = 7;
3110
3111 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003112 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003113 static const int kFunctionWithPrototype = 1;
3114
3115 // Layout of the default cache. It holds alternating name and code objects.
3116 static const int kCodeCacheEntrySize = 2;
3117 static const int kCodeCacheEntryNameOffset = 0;
3118 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003119
3120 private:
3121 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3122};
3123
3124
3125// An abstract superclass, a marker class really, for simple structure classes.
3126// It doesn't carry much functionality but allows struct classes to me
3127// identified in the type system.
3128class Struct: public HeapObject {
3129 public:
3130 inline void InitializeBody(int object_size);
3131 static inline Struct* cast(Object* that);
3132};
3133
3134
3135// Script describes a script which has been added to the VM.
3136class Script: public Struct {
3137 public:
3138 // Script types.
3139 enum Type {
3140 TYPE_NATIVE = 0,
3141 TYPE_EXTENSION = 1,
3142 TYPE_NORMAL = 2
3143 };
3144
3145 // Script compilation types.
3146 enum CompilationType {
3147 COMPILATION_TYPE_HOST = 0,
3148 COMPILATION_TYPE_EVAL = 1,
3149 COMPILATION_TYPE_JSON = 2
3150 };
3151
3152 // [source]: the script source.
3153 DECL_ACCESSORS(source, Object)
3154
3155 // [name]: the script name.
3156 DECL_ACCESSORS(name, Object)
3157
3158 // [id]: the script id.
3159 DECL_ACCESSORS(id, Object)
3160
3161 // [line_offset]: script line offset in resource from where it was extracted.
3162 DECL_ACCESSORS(line_offset, Smi)
3163
3164 // [column_offset]: script column offset in resource from where it was
3165 // extracted.
3166 DECL_ACCESSORS(column_offset, Smi)
3167
3168 // [data]: additional data associated with this script.
3169 DECL_ACCESSORS(data, Object)
3170
3171 // [context_data]: context data for the context this script was compiled in.
3172 DECL_ACCESSORS(context_data, Object)
3173
3174 // [wrapper]: the wrapper cache.
3175 DECL_ACCESSORS(wrapper, Proxy)
3176
3177 // [type]: the script type.
3178 DECL_ACCESSORS(type, Smi)
3179
3180 // [compilation]: how the the script was compiled.
3181 DECL_ACCESSORS(compilation_type, Smi)
3182
Steve Blockd0582a62009-12-15 09:54:21 +00003183 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003184 DECL_ACCESSORS(line_ends, Object)
3185
Steve Blockd0582a62009-12-15 09:54:21 +00003186 // [eval_from_shared]: for eval scripts the shared funcion info for the
3187 // function from which eval was called.
3188 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003189
3190 // [eval_from_instructions_offset]: the instruction offset in the code for the
3191 // function from which eval was called where eval was called.
3192 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3193
3194 static inline Script* cast(Object* obj);
3195
Steve Block3ce2e202009-11-05 08:53:23 +00003196 // If script source is an external string, check that the underlying
3197 // resource is accessible. Otherwise, always return true.
3198 inline bool HasValidSource();
3199
Steve Blocka7e24c12009-10-30 11:49:00 +00003200#ifdef DEBUG
3201 void ScriptPrint();
3202 void ScriptVerify();
3203#endif
3204
3205 static const int kSourceOffset = HeapObject::kHeaderSize;
3206 static const int kNameOffset = kSourceOffset + kPointerSize;
3207 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3208 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3209 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3210 static const int kContextOffset = kDataOffset + kPointerSize;
3211 static const int kWrapperOffset = kContextOffset + kPointerSize;
3212 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3213 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3214 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3215 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003216 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003217 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003218 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003219 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3220
3221 private:
3222 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3223};
3224
3225
3226// SharedFunctionInfo describes the JSFunction information that can be
3227// shared by multiple instances of the function.
3228class SharedFunctionInfo: public HeapObject {
3229 public:
3230 // [name]: Function name.
3231 DECL_ACCESSORS(name, Object)
3232
3233 // [code]: Function code.
3234 DECL_ACCESSORS(code, Code)
3235
3236 // [construct stub]: Code stub for constructing instances of this function.
3237 DECL_ACCESSORS(construct_stub, Code)
3238
3239 // Returns if this function has been compiled to native code yet.
3240 inline bool is_compiled();
3241
3242 // [length]: The function length - usually the number of declared parameters.
3243 // Use up to 2^30 parameters.
3244 inline int length();
3245 inline void set_length(int value);
3246
3247 // [formal parameter count]: The declared number of parameters.
3248 inline int formal_parameter_count();
3249 inline void set_formal_parameter_count(int value);
3250
3251 // Set the formal parameter count so the function code will be
3252 // called without using argument adaptor frames.
3253 inline void DontAdaptArguments();
3254
3255 // [expected_nof_properties]: Expected number of properties for the function.
3256 inline int expected_nof_properties();
3257 inline void set_expected_nof_properties(int value);
3258
3259 // [instance class name]: class name for instances.
3260 DECL_ACCESSORS(instance_class_name, Object)
3261
Steve Block6ded16b2010-05-10 14:33:55 +01003262 // [function data]: This field holds some additional data for function.
3263 // Currently it either has FunctionTemplateInfo to make benefit the API
Kristian Monsen25f61362010-05-21 11:50:48 +01003264 // or Smi identifying a custom call generator.
Steve Blocka7e24c12009-10-30 11:49:00 +00003265 // In the long run we don't want all functions to have this field but
3266 // we can fix that when we have a better model for storing hidden data
3267 // on objects.
3268 DECL_ACCESSORS(function_data, Object)
3269
Steve Block6ded16b2010-05-10 14:33:55 +01003270 inline bool IsApiFunction();
3271 inline FunctionTemplateInfo* get_api_func_data();
3272 inline bool HasCustomCallGenerator();
Kristian Monsen25f61362010-05-21 11:50:48 +01003273 inline int custom_call_generator_id();
Steve Block6ded16b2010-05-10 14:33:55 +01003274
Steve Blocka7e24c12009-10-30 11:49:00 +00003275 // [script info]: Script from which the function originates.
3276 DECL_ACCESSORS(script, Object)
3277
Steve Block6ded16b2010-05-10 14:33:55 +01003278 // [num_literals]: Number of literals used by this function.
3279 inline int num_literals();
3280 inline void set_num_literals(int value);
3281
Steve Blocka7e24c12009-10-30 11:49:00 +00003282 // [start_position_and_type]: Field used to store both the source code
3283 // position, whether or not the function is a function expression,
3284 // and whether or not the function is a toplevel function. The two
3285 // least significants bit indicates whether the function is an
3286 // expression and the rest contains the source code position.
3287 inline int start_position_and_type();
3288 inline void set_start_position_and_type(int value);
3289
3290 // [debug info]: Debug information.
3291 DECL_ACCESSORS(debug_info, Object)
3292
3293 // [inferred name]: Name inferred from variable or property
3294 // assignment of this function. Used to facilitate debugging and
3295 // profiling of JavaScript code written in OO style, where almost
3296 // all functions are anonymous but are assigned to object
3297 // properties.
3298 DECL_ACCESSORS(inferred_name, String)
3299
3300 // Position of the 'function' token in the script source.
3301 inline int function_token_position();
3302 inline void set_function_token_position(int function_token_position);
3303
3304 // Position of this function in the script source.
3305 inline int start_position();
3306 inline void set_start_position(int start_position);
3307
3308 // End position of this function in the script source.
3309 inline int end_position();
3310 inline void set_end_position(int end_position);
3311
3312 // Is this function a function expression in the source code.
3313 inline bool is_expression();
3314 inline void set_is_expression(bool value);
3315
3316 // Is this function a top-level function (scripts, evals).
3317 inline bool is_toplevel();
3318 inline void set_is_toplevel(bool value);
3319
3320 // Bit field containing various information collected by the compiler to
3321 // drive optimization.
3322 inline int compiler_hints();
3323 inline void set_compiler_hints(int value);
3324
3325 // Add information on assignments of the form this.x = ...;
3326 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00003327 bool has_only_simple_this_property_assignments,
3328 FixedArray* this_property_assignments);
3329
3330 // Clear information on assignments of the form this.x = ...;
3331 void ClearThisPropertyAssignmentsInfo();
3332
3333 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00003334 // this.x = y; where y is either a constant or refers to an argument.
3335 inline bool has_only_simple_this_property_assignments();
3336
Leon Clarked91b9f72010-01-27 17:25:45 +00003337 inline bool try_full_codegen();
3338 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00003339
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003340 // Indicates if this function can be lazy compiled.
3341 // This is used to determine if we can safely flush code from a function
3342 // when doing GC if we expect that the function will no longer be used.
3343 inline bool allows_lazy_compilation();
3344 inline void set_allows_lazy_compilation(bool flag);
3345
Andrei Popescu402d9372010-02-26 13:31:12 +00003346 // Check whether a inlined constructor can be generated with the given
3347 // prototype.
3348 bool CanGenerateInlineConstructor(Object* prototype);
3349
Steve Blocka7e24c12009-10-30 11:49:00 +00003350 // For functions which only contains this property assignments this provides
3351 // access to the names for the properties assigned.
3352 DECL_ACCESSORS(this_property_assignments, Object)
3353 inline int this_property_assignments_count();
3354 inline void set_this_property_assignments_count(int value);
3355 String* GetThisPropertyAssignmentName(int index);
3356 bool IsThisPropertyAssignmentArgument(int index);
3357 int GetThisPropertyAssignmentArgument(int index);
3358 Object* GetThisPropertyAssignmentConstant(int index);
3359
3360 // [source code]: Source code for the function.
3361 bool HasSourceCode();
3362 Object* GetSourceCode();
3363
3364 // Calculate the instance size.
3365 int CalculateInstanceSize();
3366
3367 // Calculate the number of in-object properties.
3368 int CalculateInObjectProperties();
3369
3370 // Dispatched behavior.
3371 void SharedFunctionInfoIterateBody(ObjectVisitor* v);
3372 // Set max_length to -1 for unlimited length.
3373 void SourceCodePrint(StringStream* accumulator, int max_length);
3374#ifdef DEBUG
3375 void SharedFunctionInfoPrint();
3376 void SharedFunctionInfoVerify();
3377#endif
3378
3379 // Casting.
3380 static inline SharedFunctionInfo* cast(Object* obj);
3381
3382 // Constants.
3383 static const int kDontAdaptArgumentsSentinel = -1;
3384
3385 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01003386 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00003387 static const int kNameOffset = HeapObject::kHeaderSize;
3388 static const int kCodeOffset = kNameOffset + kPointerSize;
3389 static const int kConstructStubOffset = kCodeOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003390 static const int kInstanceClassNameOffset =
3391 kConstructStubOffset + kPointerSize;
3392 static const int kFunctionDataOffset =
3393 kInstanceClassNameOffset + kPointerSize;
3394 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
3395 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
3396 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
3397 static const int kThisPropertyAssignmentsOffset =
3398 kInferredNameOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003399#if V8_HOST_ARCH_32_BIT
3400 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01003401 static const int kLengthOffset =
3402 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003403 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
3404 static const int kExpectedNofPropertiesOffset =
3405 kFormalParameterCountOffset + kPointerSize;
3406 static const int kNumLiteralsOffset =
3407 kExpectedNofPropertiesOffset + kPointerSize;
3408 static const int kStartPositionAndTypeOffset =
3409 kNumLiteralsOffset + kPointerSize;
3410 static const int kEndPositionOffset =
3411 kStartPositionAndTypeOffset + kPointerSize;
3412 static const int kFunctionTokenPositionOffset =
3413 kEndPositionOffset + kPointerSize;
3414 static const int kCompilerHintsOffset =
3415 kFunctionTokenPositionOffset + kPointerSize;
3416 static const int kThisPropertyAssignmentsCountOffset =
3417 kCompilerHintsOffset + kPointerSize;
3418 // Total size.
3419 static const int kSize = kThisPropertyAssignmentsCountOffset + kPointerSize;
3420#else
3421 // The only reason to use smi fields instead of int fields
3422 // is to allow interation without maps decoding during
3423 // garbage collections.
3424 // To avoid wasting space on 64-bit architectures we use
3425 // the following trick: we group integer fields into pairs
3426 // First integer in each pair is shifted left by 1.
3427 // By doing this we guarantee that LSB of each kPointerSize aligned
3428 // word is not set and thus this word cannot be treated as pointer
3429 // to HeapObject during old space traversal.
3430 static const int kLengthOffset =
3431 kThisPropertyAssignmentsOffset + kPointerSize;
3432 static const int kFormalParameterCountOffset =
3433 kLengthOffset + kIntSize;
3434
Steve Blocka7e24c12009-10-30 11:49:00 +00003435 static const int kExpectedNofPropertiesOffset =
3436 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003437 static const int kNumLiteralsOffset =
3438 kExpectedNofPropertiesOffset + kIntSize;
3439
3440 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003441 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003442 static const int kStartPositionAndTypeOffset =
3443 kEndPositionOffset + kIntSize;
3444
3445 static const int kFunctionTokenPositionOffset =
3446 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003447 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00003448 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003449
Steve Blocka7e24c12009-10-30 11:49:00 +00003450 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003451 kCompilerHintsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003452
Steve Block6ded16b2010-05-10 14:33:55 +01003453 // Total size.
3454 static const int kSize = kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003455
3456#endif
Steve Block6ded16b2010-05-10 14:33:55 +01003457 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003458
3459 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00003460 // Bit positions in start_position_and_type.
3461 // The source code start position is in the 30 most significant bits of
3462 // the start_position_and_type field.
3463 static const int kIsExpressionBit = 0;
3464 static const int kIsTopLevelBit = 1;
3465 static const int kStartPositionShift = 2;
3466 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
3467
3468 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00003469 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00003470 static const int kTryFullCodegen = 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003471 static const int kAllowLazyCompilation = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00003472
3473 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
3474};
3475
3476
3477// JSFunction describes JavaScript functions.
3478class JSFunction: public JSObject {
3479 public:
3480 // [prototype_or_initial_map]:
3481 DECL_ACCESSORS(prototype_or_initial_map, Object)
3482
3483 // [shared_function_info]: The information about the function that
3484 // can be shared by instances.
3485 DECL_ACCESSORS(shared, SharedFunctionInfo)
3486
3487 // [context]: The context for this function.
3488 inline Context* context();
3489 inline Object* unchecked_context();
3490 inline void set_context(Object* context);
3491
3492 // [code]: The generated code object for this function. Executed
3493 // when the function is invoked, e.g. foo() or new foo(). See
3494 // [[Call]] and [[Construct]] description in ECMA-262, section
3495 // 8.6.2, page 27.
3496 inline Code* code();
3497 inline void set_code(Code* value);
3498
Steve Blocka7e24c12009-10-30 11:49:00 +00003499 // Tells whether this function is builtin.
3500 inline bool IsBuiltin();
3501
3502 // [literals]: Fixed array holding the materialized literals.
3503 //
3504 // If the function contains object, regexp or array literals, the
3505 // literals array prefix contains the object, regexp, and array
3506 // function to be used when creating these literals. This is
3507 // necessary so that we do not dynamically lookup the object, regexp
3508 // or array functions. Performing a dynamic lookup, we might end up
3509 // using the functions from a new context that we should not have
3510 // access to.
3511 DECL_ACCESSORS(literals, FixedArray)
3512
3513 // The initial map for an object created by this constructor.
3514 inline Map* initial_map();
3515 inline void set_initial_map(Map* value);
3516 inline bool has_initial_map();
3517
3518 // Get and set the prototype property on a JSFunction. If the
3519 // function has an initial map the prototype is set on the initial
3520 // map. Otherwise, the prototype is put in the initial map field
3521 // until an initial map is needed.
3522 inline bool has_prototype();
3523 inline bool has_instance_prototype();
3524 inline Object* prototype();
3525 inline Object* instance_prototype();
3526 Object* SetInstancePrototype(Object* value);
3527 Object* SetPrototype(Object* value);
3528
Steve Block6ded16b2010-05-10 14:33:55 +01003529 // After prototype is removed, it will not be created when accessed, and
3530 // [[Construct]] from this function will not be allowed.
3531 Object* RemovePrototype();
3532 inline bool should_have_prototype();
3533
Steve Blocka7e24c12009-10-30 11:49:00 +00003534 // Accessor for this function's initial map's [[class]]
3535 // property. This is primarily used by ECMA native functions. This
3536 // method sets the class_name field of this function's initial map
3537 // to a given value. It creates an initial map if this function does
3538 // not have one. Note that this method does not copy the initial map
3539 // if it has one already, but simply replaces it with the new value.
3540 // Instances created afterwards will have a map whose [[class]] is
3541 // set to 'value', but there is no guarantees on instances created
3542 // before.
3543 Object* SetInstanceClassName(String* name);
3544
3545 // Returns if this function has been compiled to native code yet.
3546 inline bool is_compiled();
3547
3548 // Casting.
3549 static inline JSFunction* cast(Object* obj);
3550
3551 // Dispatched behavior.
3552#ifdef DEBUG
3553 void JSFunctionPrint();
3554 void JSFunctionVerify();
3555#endif
3556
3557 // Returns the number of allocated literals.
3558 inline int NumberOfLiterals();
3559
3560 // Retrieve the global context from a function's literal array.
3561 static Context* GlobalContextFromLiterals(FixedArray* literals);
3562
3563 // Layout descriptors.
3564 static const int kPrototypeOrInitialMapOffset = JSObject::kHeaderSize;
3565 static const int kSharedFunctionInfoOffset =
3566 kPrototypeOrInitialMapOffset + kPointerSize;
3567 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
3568 static const int kLiteralsOffset = kContextOffset + kPointerSize;
3569 static const int kSize = kLiteralsOffset + kPointerSize;
3570
3571 // Layout of the literals array.
3572 static const int kLiteralsPrefixSize = 1;
3573 static const int kLiteralGlobalContextIndex = 0;
3574 private:
3575 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
3576};
3577
3578
3579// JSGlobalProxy's prototype must be a JSGlobalObject or null,
3580// and the prototype is hidden. JSGlobalProxy always delegates
3581// property accesses to its prototype if the prototype is not null.
3582//
3583// A JSGlobalProxy can be reinitialized which will preserve its identity.
3584//
3585// Accessing a JSGlobalProxy requires security check.
3586
3587class JSGlobalProxy : public JSObject {
3588 public:
3589 // [context]: the owner global context of this proxy object.
3590 // It is null value if this object is not used by any context.
3591 DECL_ACCESSORS(context, Object)
3592
3593 // Casting.
3594 static inline JSGlobalProxy* cast(Object* obj);
3595
3596 // Dispatched behavior.
3597#ifdef DEBUG
3598 void JSGlobalProxyPrint();
3599 void JSGlobalProxyVerify();
3600#endif
3601
3602 // Layout description.
3603 static const int kContextOffset = JSObject::kHeaderSize;
3604 static const int kSize = kContextOffset + kPointerSize;
3605
3606 private:
3607
3608 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
3609};
3610
3611
3612// Forward declaration.
3613class JSBuiltinsObject;
3614
3615// Common super class for JavaScript global objects and the special
3616// builtins global objects.
3617class GlobalObject: public JSObject {
3618 public:
3619 // [builtins]: the object holding the runtime routines written in JS.
3620 DECL_ACCESSORS(builtins, JSBuiltinsObject)
3621
3622 // [global context]: the global context corresponding to this global object.
3623 DECL_ACCESSORS(global_context, Context)
3624
3625 // [global receiver]: the global receiver object of the context
3626 DECL_ACCESSORS(global_receiver, JSObject)
3627
3628 // Retrieve the property cell used to store a property.
3629 Object* GetPropertyCell(LookupResult* result);
3630
3631 // Ensure that the global object has a cell for the given property name.
3632 Object* EnsurePropertyCell(String* name);
3633
3634 // Casting.
3635 static inline GlobalObject* cast(Object* obj);
3636
3637 // Layout description.
3638 static const int kBuiltinsOffset = JSObject::kHeaderSize;
3639 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
3640 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
3641 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
3642
3643 private:
3644 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
3645
3646 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
3647};
3648
3649
3650// JavaScript global object.
3651class JSGlobalObject: public GlobalObject {
3652 public:
3653
3654 // Casting.
3655 static inline JSGlobalObject* cast(Object* obj);
3656
3657 // Dispatched behavior.
3658#ifdef DEBUG
3659 void JSGlobalObjectPrint();
3660 void JSGlobalObjectVerify();
3661#endif
3662
3663 // Layout description.
3664 static const int kSize = GlobalObject::kHeaderSize;
3665
3666 private:
3667 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
3668};
3669
3670
3671// Builtins global object which holds the runtime routines written in
3672// JavaScript.
3673class JSBuiltinsObject: public GlobalObject {
3674 public:
3675 // Accessors for the runtime routines written in JavaScript.
3676 inline Object* javascript_builtin(Builtins::JavaScript id);
3677 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
3678
Steve Block6ded16b2010-05-10 14:33:55 +01003679 // Accessors for code of the runtime routines written in JavaScript.
3680 inline Code* javascript_builtin_code(Builtins::JavaScript id);
3681 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
3682
Steve Blocka7e24c12009-10-30 11:49:00 +00003683 // Casting.
3684 static inline JSBuiltinsObject* cast(Object* obj);
3685
3686 // Dispatched behavior.
3687#ifdef DEBUG
3688 void JSBuiltinsObjectPrint();
3689 void JSBuiltinsObjectVerify();
3690#endif
3691
3692 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01003693 // room for two pointers per runtime routine written in javascript
3694 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00003695 static const int kJSBuiltinsCount = Builtins::id_count;
3696 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003697 static const int kJSBuiltinsCodeOffset =
3698 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003699 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01003700 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
3701
3702 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
3703 return kJSBuiltinsOffset + id * kPointerSize;
3704 }
3705
3706 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
3707 return kJSBuiltinsCodeOffset + id * kPointerSize;
3708 }
3709
Steve Blocka7e24c12009-10-30 11:49:00 +00003710 private:
3711 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
3712};
3713
3714
3715// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
3716class JSValue: public JSObject {
3717 public:
3718 // [value]: the object being wrapped.
3719 DECL_ACCESSORS(value, Object)
3720
3721 // Casting.
3722 static inline JSValue* cast(Object* obj);
3723
3724 // Dispatched behavior.
3725#ifdef DEBUG
3726 void JSValuePrint();
3727 void JSValueVerify();
3728#endif
3729
3730 // Layout description.
3731 static const int kValueOffset = JSObject::kHeaderSize;
3732 static const int kSize = kValueOffset + kPointerSize;
3733
3734 private:
3735 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
3736};
3737
3738// Regular expressions
3739// The regular expression holds a single reference to a FixedArray in
3740// the kDataOffset field.
3741// The FixedArray contains the following data:
3742// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
3743// - reference to the original source string
3744// - reference to the original flag string
3745// If it is an atom regexp
3746// - a reference to a literal string to search for
3747// If it is an irregexp regexp:
3748// - a reference to code for ASCII inputs (bytecode or compiled).
3749// - a reference to code for UC16 inputs (bytecode or compiled).
3750// - max number of registers used by irregexp implementations.
3751// - number of capture registers (output values) of the regexp.
3752class JSRegExp: public JSObject {
3753 public:
3754 // Meaning of Type:
3755 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
3756 // ATOM: A simple string to match against using an indexOf operation.
3757 // IRREGEXP: Compiled with Irregexp.
3758 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
3759 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
3760 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
3761
3762 class Flags {
3763 public:
3764 explicit Flags(uint32_t value) : value_(value) { }
3765 bool is_global() { return (value_ & GLOBAL) != 0; }
3766 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
3767 bool is_multiline() { return (value_ & MULTILINE) != 0; }
3768 uint32_t value() { return value_; }
3769 private:
3770 uint32_t value_;
3771 };
3772
3773 DECL_ACCESSORS(data, Object)
3774
3775 inline Type TypeTag();
3776 inline int CaptureCount();
3777 inline Flags GetFlags();
3778 inline String* Pattern();
3779 inline Object* DataAt(int index);
3780 // Set implementation data after the object has been prepared.
3781 inline void SetDataAt(int index, Object* value);
3782 static int code_index(bool is_ascii) {
3783 if (is_ascii) {
3784 return kIrregexpASCIICodeIndex;
3785 } else {
3786 return kIrregexpUC16CodeIndex;
3787 }
3788 }
3789
3790 static inline JSRegExp* cast(Object* obj);
3791
3792 // Dispatched behavior.
3793#ifdef DEBUG
3794 void JSRegExpVerify();
3795#endif
3796
3797 static const int kDataOffset = JSObject::kHeaderSize;
3798 static const int kSize = kDataOffset + kPointerSize;
3799
3800 // Indices in the data array.
3801 static const int kTagIndex = 0;
3802 static const int kSourceIndex = kTagIndex + 1;
3803 static const int kFlagsIndex = kSourceIndex + 1;
3804 static const int kDataIndex = kFlagsIndex + 1;
3805 // The data fields are used in different ways depending on the
3806 // value of the tag.
3807 // Atom regexps (literal strings).
3808 static const int kAtomPatternIndex = kDataIndex;
3809
3810 static const int kAtomDataSize = kAtomPatternIndex + 1;
3811
3812 // Irregexp compiled code or bytecode for ASCII. If compilation
3813 // fails, this fields hold an exception object that should be
3814 // thrown if the regexp is used again.
3815 static const int kIrregexpASCIICodeIndex = kDataIndex;
3816 // Irregexp compiled code or bytecode for UC16. If compilation
3817 // fails, this fields hold an exception object that should be
3818 // thrown if the regexp is used again.
3819 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
3820 // Maximal number of registers used by either ASCII or UC16.
3821 // Only used to check that there is enough stack space
3822 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
3823 // Number of captures in the compiled regexp.
3824 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
3825
3826 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00003827
3828 // Offsets directly into the data fixed array.
3829 static const int kDataTagOffset =
3830 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
3831 static const int kDataAsciiCodeOffset =
3832 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00003833 static const int kDataUC16CodeOffset =
3834 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00003835 static const int kIrregexpCaptureCountOffset =
3836 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003837
3838 // In-object fields.
3839 static const int kSourceFieldIndex = 0;
3840 static const int kGlobalFieldIndex = 1;
3841 static const int kIgnoreCaseFieldIndex = 2;
3842 static const int kMultilineFieldIndex = 3;
3843 static const int kLastIndexFieldIndex = 4;
Steve Blocka7e24c12009-10-30 11:49:00 +00003844};
3845
3846
3847class CompilationCacheShape {
3848 public:
3849 static inline bool IsMatch(HashTableKey* key, Object* value) {
3850 return key->IsMatch(value);
3851 }
3852
3853 static inline uint32_t Hash(HashTableKey* key) {
3854 return key->Hash();
3855 }
3856
3857 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
3858 return key->HashForObject(object);
3859 }
3860
3861 static Object* AsObject(HashTableKey* key) {
3862 return key->AsObject();
3863 }
3864
3865 static const int kPrefixSize = 0;
3866 static const int kEntrySize = 2;
3867};
3868
Steve Block3ce2e202009-11-05 08:53:23 +00003869
Steve Blocka7e24c12009-10-30 11:49:00 +00003870class CompilationCacheTable: public HashTable<CompilationCacheShape,
3871 HashTableKey*> {
3872 public:
3873 // Find cached value for a string key, otherwise return null.
3874 Object* Lookup(String* src);
3875 Object* LookupEval(String* src, Context* context);
3876 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
3877 Object* Put(String* src, Object* value);
3878 Object* PutEval(String* src, Context* context, Object* value);
3879 Object* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
3880
3881 static inline CompilationCacheTable* cast(Object* obj);
3882
3883 private:
3884 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
3885};
3886
3887
Steve Block6ded16b2010-05-10 14:33:55 +01003888class CodeCache: public Struct {
3889 public:
3890 DECL_ACCESSORS(default_cache, FixedArray)
3891 DECL_ACCESSORS(normal_type_cache, Object)
3892
3893 // Add the code object to the cache.
3894 Object* Update(String* name, Code* code);
3895
3896 // Lookup code object in the cache. Returns code object if found and undefined
3897 // if not.
3898 Object* Lookup(String* name, Code::Flags flags);
3899
3900 // Get the internal index of a code object in the cache. Returns -1 if the
3901 // code object is not in that cache. This index can be used to later call
3902 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
3903 // RemoveByIndex.
3904 int GetIndex(Object* name, Code* code);
3905
3906 // Remove an object from the cache with the provided internal index.
3907 void RemoveByIndex(Object* name, Code* code, int index);
3908
3909 static inline CodeCache* cast(Object* obj);
3910
3911#ifdef DEBUG
3912 void CodeCachePrint();
3913 void CodeCacheVerify();
3914#endif
3915
3916 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
3917 static const int kNormalTypeCacheOffset =
3918 kDefaultCacheOffset + kPointerSize;
3919 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
3920
3921 private:
3922 Object* UpdateDefaultCache(String* name, Code* code);
3923 Object* UpdateNormalTypeCache(String* name, Code* code);
3924 Object* LookupDefaultCache(String* name, Code::Flags flags);
3925 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
3926
3927 // Code cache layout of the default cache. Elements are alternating name and
3928 // code objects for non normal load/store/call IC's.
3929 static const int kCodeCacheEntrySize = 2;
3930 static const int kCodeCacheEntryNameOffset = 0;
3931 static const int kCodeCacheEntryCodeOffset = 1;
3932
3933 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
3934};
3935
3936
3937class CodeCacheHashTableShape {
3938 public:
3939 static inline bool IsMatch(HashTableKey* key, Object* value) {
3940 return key->IsMatch(value);
3941 }
3942
3943 static inline uint32_t Hash(HashTableKey* key) {
3944 return key->Hash();
3945 }
3946
3947 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
3948 return key->HashForObject(object);
3949 }
3950
3951 static Object* AsObject(HashTableKey* key) {
3952 return key->AsObject();
3953 }
3954
3955 static const int kPrefixSize = 0;
3956 static const int kEntrySize = 2;
3957};
3958
3959
3960class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
3961 HashTableKey*> {
3962 public:
3963 Object* Lookup(String* name, Code::Flags flags);
3964 Object* Put(String* name, Code* code);
3965
3966 int GetIndex(String* name, Code::Flags flags);
3967 void RemoveByIndex(int index);
3968
3969 static inline CodeCacheHashTable* cast(Object* obj);
3970
3971 // Initial size of the fixed array backing the hash table.
3972 static const int kInitialSize = 64;
3973
3974 private:
3975 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
3976};
3977
3978
Steve Blocka7e24c12009-10-30 11:49:00 +00003979enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
3980enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
3981
3982
3983class StringHasher {
3984 public:
3985 inline StringHasher(int length);
3986
3987 // Returns true if the hash of this string can be computed without
3988 // looking at the contents.
3989 inline bool has_trivial_hash();
3990
3991 // Add a character to the hash and update the array index calculation.
3992 inline void AddCharacter(uc32 c);
3993
3994 // Adds a character to the hash but does not update the array index
3995 // calculation. This can only be called when it has been verified
3996 // that the input is not an array index.
3997 inline void AddCharacterNoIndex(uc32 c);
3998
3999 // Returns the value to store in the hash field of a string with
4000 // the given length and contents.
4001 uint32_t GetHashField();
4002
4003 // Returns true if the characters seen so far make up a legal array
4004 // index.
4005 bool is_array_index() { return is_array_index_; }
4006
4007 bool is_valid() { return is_valid_; }
4008
4009 void invalidate() { is_valid_ = false; }
4010
4011 private:
4012
4013 uint32_t array_index() {
4014 ASSERT(is_array_index());
4015 return array_index_;
4016 }
4017
4018 inline uint32_t GetHash();
4019
4020 int length_;
4021 uint32_t raw_running_hash_;
4022 uint32_t array_index_;
4023 bool is_array_index_;
4024 bool is_first_char_;
4025 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00004026 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00004027};
4028
4029
4030// The characteristics of a string are stored in its map. Retrieving these
4031// few bits of information is moderately expensive, involving two memory
4032// loads where the second is dependent on the first. To improve efficiency
4033// the shape of the string is given its own class so that it can be retrieved
4034// once and used for several string operations. A StringShape is small enough
4035// to be passed by value and is immutable, but be aware that flattening a
4036// string can potentially alter its shape. Also be aware that a GC caused by
4037// something else can alter the shape of a string due to ConsString
4038// shortcutting. Keeping these restrictions in mind has proven to be error-
4039// prone and so we no longer put StringShapes in variables unless there is a
4040// concrete performance benefit at that particular point in the code.
4041class StringShape BASE_EMBEDDED {
4042 public:
4043 inline explicit StringShape(String* s);
4044 inline explicit StringShape(Map* s);
4045 inline explicit StringShape(InstanceType t);
4046 inline bool IsSequential();
4047 inline bool IsExternal();
4048 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00004049 inline bool IsExternalAscii();
4050 inline bool IsExternalTwoByte();
4051 inline bool IsSequentialAscii();
4052 inline bool IsSequentialTwoByte();
4053 inline bool IsSymbol();
4054 inline StringRepresentationTag representation_tag();
4055 inline uint32_t full_representation_tag();
4056 inline uint32_t size_tag();
4057#ifdef DEBUG
4058 inline uint32_t type() { return type_; }
4059 inline void invalidate() { valid_ = false; }
4060 inline bool valid() { return valid_; }
4061#else
4062 inline void invalidate() { }
4063#endif
4064 private:
4065 uint32_t type_;
4066#ifdef DEBUG
4067 inline void set_valid() { valid_ = true; }
4068 bool valid_;
4069#else
4070 inline void set_valid() { }
4071#endif
4072};
4073
4074
4075// The String abstract class captures JavaScript string values:
4076//
4077// Ecma-262:
4078// 4.3.16 String Value
4079// A string value is a member of the type String and is a finite
4080// ordered sequence of zero or more 16-bit unsigned integer values.
4081//
4082// All string values have a length field.
4083class String: public HeapObject {
4084 public:
4085 // Get and set the length of the string.
4086 inline int length();
4087 inline void set_length(int value);
4088
Steve Blockd0582a62009-12-15 09:54:21 +00004089 // Get and set the hash field of the string.
4090 inline uint32_t hash_field();
4091 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004092
4093 inline bool IsAsciiRepresentation();
4094 inline bool IsTwoByteRepresentation();
4095
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004096 // Returns whether this string has ascii chars, i.e. all of them can
4097 // be ascii encoded. This might be the case even if the string is
4098 // two-byte. Such strings may appear when the embedder prefers
4099 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01004100 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004101 // NOTE: this should be considered only a hint. False negatives are
4102 // possible.
4103 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01004104
Steve Blocka7e24c12009-10-30 11:49:00 +00004105 // Get and set individual two byte chars in the string.
4106 inline void Set(int index, uint16_t value);
4107 // Get individual two byte char in the string. Repeated calls
4108 // to this method are not efficient unless the string is flat.
4109 inline uint16_t Get(int index);
4110
Leon Clarkef7060e22010-06-03 12:02:55 +01004111 // Try to flatten the string. Checks first inline to see if it is
4112 // necessary. Does nothing if the string is not a cons string.
4113 // Flattening allocates a sequential string with the same data as
4114 // the given string and mutates the cons string to a degenerate
4115 // form, where the first component is the new sequential string and
4116 // the second component is the empty string. If allocation fails,
4117 // this function returns a failure. If flattening succeeds, this
4118 // function returns the sequential string that is now the first
4119 // component of the cons string.
4120 //
4121 // Degenerate cons strings are handled specially by the garbage
4122 // collector (see IsShortcutCandidate).
4123 //
4124 // Use FlattenString from Handles.cc to flatten even in case an
4125 // allocation failure happens.
Steve Block6ded16b2010-05-10 14:33:55 +01004126 inline Object* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004127
Leon Clarkef7060e22010-06-03 12:02:55 +01004128 // Convenience function. Has exactly the same behavior as
4129 // TryFlatten(), except in the case of failure returns the original
4130 // string.
4131 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
4132
Steve Blocka7e24c12009-10-30 11:49:00 +00004133 Vector<const char> ToAsciiVector();
4134 Vector<const uc16> ToUC16Vector();
4135
4136 // Mark the string as an undetectable object. It only applies to
4137 // ascii and two byte string types.
4138 bool MarkAsUndetectable();
4139
Steve Blockd0582a62009-12-15 09:54:21 +00004140 // Return a substring.
Steve Block6ded16b2010-05-10 14:33:55 +01004141 Object* SubString(int from, int to, PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004142
4143 // String equality operations.
4144 inline bool Equals(String* other);
4145 bool IsEqualTo(Vector<const char> str);
4146
4147 // Return a UTF8 representation of the string. The string is null
4148 // terminated but may optionally contain nulls. Length is returned
4149 // in length_output if length_output is not a null pointer The string
4150 // should be nearly flat, otherwise the performance of this method may
4151 // be very slow (quadratic in the length). Setting robustness_flag to
4152 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4153 // handles unexpected data without causing assert failures and it does not
4154 // do any heap allocations. This is useful when printing stack traces.
4155 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
4156 RobustnessFlag robustness_flag,
4157 int offset,
4158 int length,
4159 int* length_output = 0);
4160 SmartPointer<char> ToCString(
4161 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
4162 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
4163 int* length_output = 0);
4164
4165 int Utf8Length();
4166
4167 // Return a 16 bit Unicode representation of the string.
4168 // The string should be nearly flat, otherwise the performance of
4169 // of this method may be very bad. Setting robustness_flag to
4170 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4171 // handles unexpected data without causing assert failures and it does not
4172 // do any heap allocations. This is useful when printing stack traces.
4173 SmartPointer<uc16> ToWideCString(
4174 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
4175
4176 // Tells whether the hash code has been computed.
4177 inline bool HasHashCode();
4178
4179 // Returns a hash value used for the property table
4180 inline uint32_t Hash();
4181
Steve Blockd0582a62009-12-15 09:54:21 +00004182 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
4183 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00004184
4185 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
4186 uint32_t* index,
4187 int length);
4188
4189 // Externalization.
4190 bool MakeExternal(v8::String::ExternalStringResource* resource);
4191 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
4192
4193 // Conversion.
4194 inline bool AsArrayIndex(uint32_t* index);
4195
4196 // Casting.
4197 static inline String* cast(Object* obj);
4198
4199 void PrintOn(FILE* out);
4200
4201 // For use during stack traces. Performs rudimentary sanity check.
4202 bool LooksValid();
4203
4204 // Dispatched behavior.
4205 void StringShortPrint(StringStream* accumulator);
4206#ifdef DEBUG
4207 void StringPrint();
4208 void StringVerify();
4209#endif
4210 inline bool IsFlat();
4211
4212 // Layout description.
4213 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004214 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004215 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004216
Steve Blockd0582a62009-12-15 09:54:21 +00004217 // Maximum number of characters to consider when trying to convert a string
4218 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00004219 static const int kMaxArrayIndexSize = 10;
4220
4221 // Max ascii char code.
4222 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
4223 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
4224 static const int kMaxUC16CharCode = 0xffff;
4225
Steve Blockd0582a62009-12-15 09:54:21 +00004226 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00004227 static const int kMinNonFlatLength = 13;
4228
4229 // Mask constant for checking if a string has a computed hash code
4230 // and if it is an array index. The least significant bit indicates
4231 // whether a hash code has been computed. If the hash code has been
4232 // computed the 2nd bit tells whether the string can be used as an
4233 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004234 static const int kHashNotComputedMask = 1;
4235 static const int kIsNotArrayIndexMask = 1 << 1;
4236 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00004237
Steve Blockd0582a62009-12-15 09:54:21 +00004238 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004239 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00004240
Steve Blocka7e24c12009-10-30 11:49:00 +00004241 // Array index strings this short can keep their index in the hash
4242 // field.
4243 static const int kMaxCachedArrayIndexLength = 7;
4244
Steve Blockd0582a62009-12-15 09:54:21 +00004245 // For strings which are array indexes the hash value has the string length
4246 // mixed into the hash, mainly to avoid a hash value of zero which would be
4247 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004248 static const int kArrayIndexValueBits = 24;
4249 static const int kArrayIndexLengthBits =
4250 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
4251
4252 STATIC_CHECK((kArrayIndexLengthBits > 0));
4253
4254 static const int kArrayIndexHashLengthShift =
4255 kArrayIndexValueBits + kNofHashBitFields;
4256
Steve Blockd0582a62009-12-15 09:54:21 +00004257 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004258
4259 static const int kArrayIndexValueMask =
4260 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
4261
4262 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
4263 // could use a mask to test if the length of string is less than or equal to
4264 // kMaxCachedArrayIndexLength.
4265 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
4266
4267 static const int kContainsCachedArrayIndexMask =
4268 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
4269 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004270
4271 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004272 static const int kEmptyHashField =
4273 kIsNotArrayIndexMask | kHashNotComputedMask;
4274
4275 // Value of hash field containing computed hash equal to zero.
4276 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004277
4278 // Maximal string length.
4279 static const int kMaxLength = (1 << (32 - 2)) - 1;
4280
4281 // Max length for computing hash. For strings longer than this limit the
4282 // string length is used as the hash value.
4283 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00004284
4285 // Limit for truncation in short printing.
4286 static const int kMaxShortPrintLength = 1024;
4287
4288 // Support for regular expressions.
4289 const uc16* GetTwoByteData();
4290 const uc16* GetTwoByteData(unsigned start);
4291
4292 // Support for StringInputBuffer
4293 static const unibrow::byte* ReadBlock(String* input,
4294 unibrow::byte* util_buffer,
4295 unsigned capacity,
4296 unsigned* remaining,
4297 unsigned* offset);
4298 static const unibrow::byte* ReadBlock(String** input,
4299 unibrow::byte* util_buffer,
4300 unsigned capacity,
4301 unsigned* remaining,
4302 unsigned* offset);
4303
4304 // Helper function for flattening strings.
4305 template <typename sinkchar>
4306 static void WriteToFlat(String* source,
4307 sinkchar* sink,
4308 int from,
4309 int to);
4310
4311 protected:
4312 class ReadBlockBuffer {
4313 public:
4314 ReadBlockBuffer(unibrow::byte* util_buffer_,
4315 unsigned cursor_,
4316 unsigned capacity_,
4317 unsigned remaining_) :
4318 util_buffer(util_buffer_),
4319 cursor(cursor_),
4320 capacity(capacity_),
4321 remaining(remaining_) {
4322 }
4323 unibrow::byte* util_buffer;
4324 unsigned cursor;
4325 unsigned capacity;
4326 unsigned remaining;
4327 };
4328
Steve Blocka7e24c12009-10-30 11:49:00 +00004329 static inline const unibrow::byte* ReadBlock(String* input,
4330 ReadBlockBuffer* buffer,
4331 unsigned* offset,
4332 unsigned max_chars);
4333 static void ReadBlockIntoBuffer(String* input,
4334 ReadBlockBuffer* buffer,
4335 unsigned* offset_ptr,
4336 unsigned max_chars);
4337
4338 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01004339 // Try to flatten the top level ConsString that is hiding behind this
4340 // string. This is a no-op unless the string is a ConsString. Flatten
4341 // mutates the ConsString and might return a failure.
4342 Object* SlowTryFlatten(PretenureFlag pretenure);
4343
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004344 static inline bool IsHashFieldComputed(uint32_t field);
4345
Steve Blocka7e24c12009-10-30 11:49:00 +00004346 // Slow case of String::Equals. This implementation works on any strings
4347 // but it is most efficient on strings that are almost flat.
4348 bool SlowEquals(String* other);
4349
4350 // Slow case of AsArrayIndex.
4351 bool SlowAsArrayIndex(uint32_t* index);
4352
4353 // Compute and set the hash code.
4354 uint32_t ComputeAndSetHash();
4355
4356 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
4357};
4358
4359
4360// The SeqString abstract class captures sequential string values.
4361class SeqString: public String {
4362 public:
4363
4364 // Casting.
4365 static inline SeqString* cast(Object* obj);
4366
Steve Blocka7e24c12009-10-30 11:49:00 +00004367 private:
4368 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
4369};
4370
4371
4372// The AsciiString class captures sequential ascii string objects.
4373// Each character in the AsciiString is an ascii character.
4374class SeqAsciiString: public SeqString {
4375 public:
4376 // Dispatched behavior.
4377 inline uint16_t SeqAsciiStringGet(int index);
4378 inline void SeqAsciiStringSet(int index, uint16_t value);
4379
4380 // Get the address of the characters in this string.
4381 inline Address GetCharsAddress();
4382
4383 inline char* GetChars();
4384
4385 // Casting
4386 static inline SeqAsciiString* cast(Object* obj);
4387
4388 // Garbage collection support. This method is called by the
4389 // garbage collector to compute the actual size of an AsciiString
4390 // instance.
4391 inline int SeqAsciiStringSize(InstanceType instance_type);
4392
4393 // Computes the size for an AsciiString instance of a given length.
4394 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004395 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004396 }
4397
4398 // Layout description.
4399 static const int kHeaderSize = String::kSize;
4400 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4401
Leon Clarkee46be812010-01-19 14:06:41 +00004402 // Maximal memory usage for a single sequential ASCII string.
4403 static const int kMaxSize = 512 * MB;
4404 // Maximal length of a single sequential ASCII string.
4405 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4406 static const int kMaxLength = (kMaxSize - kHeaderSize);
4407
Steve Blocka7e24c12009-10-30 11:49:00 +00004408 // Support for StringInputBuffer.
4409 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4410 unsigned* offset,
4411 unsigned chars);
4412 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
4413 unsigned* offset,
4414 unsigned chars);
4415
4416 private:
4417 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
4418};
4419
4420
4421// The TwoByteString class captures sequential unicode string objects.
4422// Each character in the TwoByteString is a two-byte uint16_t.
4423class SeqTwoByteString: public SeqString {
4424 public:
4425 // Dispatched behavior.
4426 inline uint16_t SeqTwoByteStringGet(int index);
4427 inline void SeqTwoByteStringSet(int index, uint16_t value);
4428
4429 // Get the address of the characters in this string.
4430 inline Address GetCharsAddress();
4431
4432 inline uc16* GetChars();
4433
4434 // For regexp code.
4435 const uint16_t* SeqTwoByteStringGetData(unsigned start);
4436
4437 // Casting
4438 static inline SeqTwoByteString* cast(Object* obj);
4439
4440 // Garbage collection support. This method is called by the
4441 // garbage collector to compute the actual size of a TwoByteString
4442 // instance.
4443 inline int SeqTwoByteStringSize(InstanceType instance_type);
4444
4445 // Computes the size for a TwoByteString instance of a given length.
4446 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004447 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004448 }
4449
4450 // Layout description.
4451 static const int kHeaderSize = String::kSize;
4452 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4453
Leon Clarkee46be812010-01-19 14:06:41 +00004454 // Maximal memory usage for a single sequential two-byte string.
4455 static const int kMaxSize = 512 * MB;
4456 // Maximal length of a single sequential two-byte string.
4457 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4458 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
4459
Steve Blocka7e24c12009-10-30 11:49:00 +00004460 // Support for StringInputBuffer.
4461 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4462 unsigned* offset_ptr,
4463 unsigned chars);
4464
4465 private:
4466 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
4467};
4468
4469
4470// The ConsString class describes string values built by using the
4471// addition operator on strings. A ConsString is a pair where the
4472// first and second components are pointers to other string values.
4473// One or both components of a ConsString can be pointers to other
4474// ConsStrings, creating a binary tree of ConsStrings where the leaves
4475// are non-ConsString string values. The string value represented by
4476// a ConsString can be obtained by concatenating the leaf string
4477// values in a left-to-right depth-first traversal of the tree.
4478class ConsString: public String {
4479 public:
4480 // First string of the cons cell.
4481 inline String* first();
4482 // Doesn't check that the result is a string, even in debug mode. This is
4483 // useful during GC where the mark bits confuse the checks.
4484 inline Object* unchecked_first();
4485 inline void set_first(String* first,
4486 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4487
4488 // Second string of the cons cell.
4489 inline String* second();
4490 // Doesn't check that the result is a string, even in debug mode. This is
4491 // useful during GC where the mark bits confuse the checks.
4492 inline Object* unchecked_second();
4493 inline void set_second(String* second,
4494 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4495
4496 // Dispatched behavior.
4497 uint16_t ConsStringGet(int index);
4498
4499 // Casting.
4500 static inline ConsString* cast(Object* obj);
4501
4502 // Garbage collection support. This method is called during garbage
4503 // collection to iterate through the heap pointers in the body of
4504 // the ConsString.
4505 void ConsStringIterateBody(ObjectVisitor* v);
4506
4507 // Layout description.
4508 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
4509 static const int kSecondOffset = kFirstOffset + kPointerSize;
4510 static const int kSize = kSecondOffset + kPointerSize;
4511
4512 // Support for StringInputBuffer.
4513 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
4514 unsigned* offset_ptr,
4515 unsigned chars);
4516 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4517 unsigned* offset_ptr,
4518 unsigned chars);
4519
4520 // Minimum length for a cons string.
4521 static const int kMinLength = 13;
4522
4523 private:
4524 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
4525};
4526
4527
Steve Blocka7e24c12009-10-30 11:49:00 +00004528// The ExternalString class describes string values that are backed by
4529// a string resource that lies outside the V8 heap. ExternalStrings
4530// consist of the length field common to all strings, a pointer to the
4531// external resource. It is important to ensure (externally) that the
4532// resource is not deallocated while the ExternalString is live in the
4533// V8 heap.
4534//
4535// The API expects that all ExternalStrings are created through the
4536// API. Therefore, ExternalStrings should not be used internally.
4537class ExternalString: public String {
4538 public:
4539 // Casting
4540 static inline ExternalString* cast(Object* obj);
4541
4542 // Layout description.
4543 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
4544 static const int kSize = kResourceOffset + kPointerSize;
4545
4546 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
4547
4548 private:
4549 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
4550};
4551
4552
4553// The ExternalAsciiString class is an external string backed by an
4554// ASCII string.
4555class ExternalAsciiString: public ExternalString {
4556 public:
4557 typedef v8::String::ExternalAsciiStringResource Resource;
4558
4559 // The underlying resource.
4560 inline Resource* resource();
4561 inline void set_resource(Resource* buffer);
4562
4563 // Dispatched behavior.
4564 uint16_t ExternalAsciiStringGet(int index);
4565
4566 // Casting.
4567 static inline ExternalAsciiString* cast(Object* obj);
4568
Steve Blockd0582a62009-12-15 09:54:21 +00004569 // Garbage collection support.
4570 void ExternalAsciiStringIterateBody(ObjectVisitor* v);
4571
Steve Blocka7e24c12009-10-30 11:49:00 +00004572 // Support for StringInputBuffer.
4573 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
4574 unsigned* offset,
4575 unsigned chars);
4576 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4577 unsigned* offset,
4578 unsigned chars);
4579
Steve Blocka7e24c12009-10-30 11:49:00 +00004580 private:
4581 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
4582};
4583
4584
4585// The ExternalTwoByteString class is an external string backed by a UTF-16
4586// encoded string.
4587class ExternalTwoByteString: public ExternalString {
4588 public:
4589 typedef v8::String::ExternalStringResource Resource;
4590
4591 // The underlying string resource.
4592 inline Resource* resource();
4593 inline void set_resource(Resource* buffer);
4594
4595 // Dispatched behavior.
4596 uint16_t ExternalTwoByteStringGet(int index);
4597
4598 // For regexp code.
4599 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
4600
4601 // Casting.
4602 static inline ExternalTwoByteString* cast(Object* obj);
4603
Steve Blockd0582a62009-12-15 09:54:21 +00004604 // Garbage collection support.
4605 void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
4606
Steve Blocka7e24c12009-10-30 11:49:00 +00004607 // Support for StringInputBuffer.
4608 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4609 unsigned* offset_ptr,
4610 unsigned chars);
4611
Steve Blocka7e24c12009-10-30 11:49:00 +00004612 private:
4613 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
4614};
4615
4616
4617// Utility superclass for stack-allocated objects that must be updated
4618// on gc. It provides two ways for the gc to update instances, either
4619// iterating or updating after gc.
4620class Relocatable BASE_EMBEDDED {
4621 public:
4622 inline Relocatable() : prev_(top_) { top_ = this; }
4623 virtual ~Relocatable() {
4624 ASSERT_EQ(top_, this);
4625 top_ = prev_;
4626 }
4627 virtual void IterateInstance(ObjectVisitor* v) { }
4628 virtual void PostGarbageCollection() { }
4629
4630 static void PostGarbageCollectionProcessing();
4631 static int ArchiveSpacePerThread();
4632 static char* ArchiveState(char* to);
4633 static char* RestoreState(char* from);
4634 static void Iterate(ObjectVisitor* v);
4635 static void Iterate(ObjectVisitor* v, Relocatable* top);
4636 static char* Iterate(ObjectVisitor* v, char* t);
4637 private:
4638 static Relocatable* top_;
4639 Relocatable* prev_;
4640};
4641
4642
4643// A flat string reader provides random access to the contents of a
4644// string independent of the character width of the string. The handle
4645// must be valid as long as the reader is being used.
4646class FlatStringReader : public Relocatable {
4647 public:
4648 explicit FlatStringReader(Handle<String> str);
4649 explicit FlatStringReader(Vector<const char> input);
4650 void PostGarbageCollection();
4651 inline uc32 Get(int index);
4652 int length() { return length_; }
4653 private:
4654 String** str_;
4655 bool is_ascii_;
4656 int length_;
4657 const void* start_;
4658};
4659
4660
4661// Note that StringInputBuffers are not valid across a GC! To fix this
4662// it would have to store a String Handle instead of a String* and
4663// AsciiStringReadBlock would have to be modified to use memcpy.
4664//
4665// StringInputBuffer is able to traverse any string regardless of how
4666// deeply nested a sequence of ConsStrings it is made of. However,
4667// performance will be better if deep strings are flattened before they
4668// are traversed. Since flattening requires memory allocation this is
4669// not always desirable, however (esp. in debugging situations).
4670class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
4671 public:
4672 virtual void Seek(unsigned pos);
4673 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
4674 inline StringInputBuffer(String* backing):
4675 unibrow::InputBuffer<String, String*, 1024>(backing) {}
4676};
4677
4678
4679class SafeStringInputBuffer
4680 : public unibrow::InputBuffer<String, String**, 256> {
4681 public:
4682 virtual void Seek(unsigned pos);
4683 inline SafeStringInputBuffer()
4684 : unibrow::InputBuffer<String, String**, 256>() {}
4685 inline SafeStringInputBuffer(String** backing)
4686 : unibrow::InputBuffer<String, String**, 256>(backing) {}
4687};
4688
4689
4690template <typename T>
4691class VectorIterator {
4692 public:
4693 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
4694 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
4695 T GetNext() { return data_[index_++]; }
4696 bool has_more() { return index_ < data_.length(); }
4697 private:
4698 Vector<const T> data_;
4699 int index_;
4700};
4701
4702
4703// The Oddball describes objects null, undefined, true, and false.
4704class Oddball: public HeapObject {
4705 public:
4706 // [to_string]: Cached to_string computed at startup.
4707 DECL_ACCESSORS(to_string, String)
4708
4709 // [to_number]: Cached to_number computed at startup.
4710 DECL_ACCESSORS(to_number, Object)
4711
4712 // Casting.
4713 static inline Oddball* cast(Object* obj);
4714
4715 // Dispatched behavior.
4716 void OddballIterateBody(ObjectVisitor* v);
4717#ifdef DEBUG
4718 void OddballVerify();
4719#endif
4720
4721 // Initialize the fields.
4722 Object* Initialize(const char* to_string, Object* to_number);
4723
4724 // Layout description.
4725 static const int kToStringOffset = HeapObject::kHeaderSize;
4726 static const int kToNumberOffset = kToStringOffset + kPointerSize;
4727 static const int kSize = kToNumberOffset + kPointerSize;
4728
4729 private:
4730 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
4731};
4732
4733
4734class JSGlobalPropertyCell: public HeapObject {
4735 public:
4736 // [value]: value of the global property.
4737 DECL_ACCESSORS(value, Object)
4738
4739 // Casting.
4740 static inline JSGlobalPropertyCell* cast(Object* obj);
4741
4742 // Dispatched behavior.
4743 void JSGlobalPropertyCellIterateBody(ObjectVisitor* v);
4744#ifdef DEBUG
4745 void JSGlobalPropertyCellVerify();
4746 void JSGlobalPropertyCellPrint();
4747#endif
4748
4749 // Layout description.
4750 static const int kValueOffset = HeapObject::kHeaderSize;
4751 static const int kSize = kValueOffset + kPointerSize;
4752
4753 private:
4754 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
4755};
4756
4757
4758
4759// Proxy describes objects pointing from JavaScript to C structures.
4760// Since they cannot contain references to JS HeapObjects they can be
4761// placed in old_data_space.
4762class Proxy: public HeapObject {
4763 public:
4764 // [proxy]: field containing the address.
4765 inline Address proxy();
4766 inline void set_proxy(Address value);
4767
4768 // Casting.
4769 static inline Proxy* cast(Object* obj);
4770
4771 // Dispatched behavior.
4772 inline void ProxyIterateBody(ObjectVisitor* v);
4773#ifdef DEBUG
4774 void ProxyPrint();
4775 void ProxyVerify();
4776#endif
4777
4778 // Layout description.
4779
4780 static const int kProxyOffset = HeapObject::kHeaderSize;
4781 static const int kSize = kProxyOffset + kPointerSize;
4782
4783 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
4784
4785 private:
4786 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
4787};
4788
4789
4790// The JSArray describes JavaScript Arrays
4791// Such an array can be in one of two modes:
4792// - fast, backing storage is a FixedArray and length <= elements.length();
4793// Please note: push and pop can be used to grow and shrink the array.
4794// - slow, backing storage is a HashTable with numbers as keys.
4795class JSArray: public JSObject {
4796 public:
4797 // [length]: The length property.
4798 DECL_ACCESSORS(length, Object)
4799
Leon Clarke4515c472010-02-03 11:58:03 +00004800 // Overload the length setter to skip write barrier when the length
4801 // is set to a smi. This matches the set function on FixedArray.
4802 inline void set_length(Smi* length);
4803
Steve Blocka7e24c12009-10-30 11:49:00 +00004804 Object* JSArrayUpdateLengthFromIndex(uint32_t index, Object* value);
4805
4806 // Initialize the array with the given capacity. The function may
4807 // fail due to out-of-memory situations, but only if the requested
4808 // capacity is non-zero.
4809 Object* Initialize(int capacity);
4810
4811 // Set the content of the array to the content of storage.
4812 inline void SetContent(FixedArray* storage);
4813
4814 // Casting.
4815 static inline JSArray* cast(Object* obj);
4816
4817 // Uses handles. Ensures that the fixed array backing the JSArray has at
4818 // least the stated size.
4819 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
4820
4821 // Dispatched behavior.
4822#ifdef DEBUG
4823 void JSArrayPrint();
4824 void JSArrayVerify();
4825#endif
4826
4827 // Number of element slots to pre-allocate for an empty array.
4828 static const int kPreallocatedArrayElements = 4;
4829
4830 // Layout description.
4831 static const int kLengthOffset = JSObject::kHeaderSize;
4832 static const int kSize = kLengthOffset + kPointerSize;
4833
4834 private:
4835 // Expand the fixed array backing of a fast-case JSArray to at least
4836 // the requested size.
4837 void Expand(int minimum_size_of_backing_fixed_array);
4838
4839 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
4840};
4841
4842
Steve Block6ded16b2010-05-10 14:33:55 +01004843// JSRegExpResult is just a JSArray with a specific initial map.
4844// This initial map adds in-object properties for "index" and "input"
4845// properties, as assigned by RegExp.prototype.exec, which allows
4846// faster creation of RegExp exec results.
4847// This class just holds constants used when creating the result.
4848// After creation the result must be treated as a JSArray in all regards.
4849class JSRegExpResult: public JSArray {
4850 public:
4851 // Offsets of object fields.
4852 static const int kIndexOffset = JSArray::kSize;
4853 static const int kInputOffset = kIndexOffset + kPointerSize;
4854 static const int kSize = kInputOffset + kPointerSize;
4855 // Indices of in-object properties.
4856 static const int kIndexIndex = 0;
4857 static const int kInputIndex = 1;
4858 private:
4859 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
4860};
4861
4862
Steve Blocka7e24c12009-10-30 11:49:00 +00004863// An accessor must have a getter, but can have no setter.
4864//
4865// When setting a property, V8 searches accessors in prototypes.
4866// If an accessor was found and it does not have a setter,
4867// the request is ignored.
4868//
4869// If the accessor in the prototype has the READ_ONLY property attribute, then
4870// a new value is added to the local object when the property is set.
4871// This shadows the accessor in the prototype.
4872class AccessorInfo: public Struct {
4873 public:
4874 DECL_ACCESSORS(getter, Object)
4875 DECL_ACCESSORS(setter, Object)
4876 DECL_ACCESSORS(data, Object)
4877 DECL_ACCESSORS(name, Object)
4878 DECL_ACCESSORS(flag, Smi)
Steve Blockd0582a62009-12-15 09:54:21 +00004879 DECL_ACCESSORS(load_stub_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004880
4881 inline bool all_can_read();
4882 inline void set_all_can_read(bool value);
4883
4884 inline bool all_can_write();
4885 inline void set_all_can_write(bool value);
4886
4887 inline bool prohibits_overwriting();
4888 inline void set_prohibits_overwriting(bool value);
4889
4890 inline PropertyAttributes property_attributes();
4891 inline void set_property_attributes(PropertyAttributes attributes);
4892
4893 static inline AccessorInfo* cast(Object* obj);
4894
4895#ifdef DEBUG
4896 void AccessorInfoPrint();
4897 void AccessorInfoVerify();
4898#endif
4899
4900 static const int kGetterOffset = HeapObject::kHeaderSize;
4901 static const int kSetterOffset = kGetterOffset + kPointerSize;
4902 static const int kDataOffset = kSetterOffset + kPointerSize;
4903 static const int kNameOffset = kDataOffset + kPointerSize;
4904 static const int kFlagOffset = kNameOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00004905 static const int kLoadStubCacheOffset = kFlagOffset + kPointerSize;
4906 static const int kSize = kLoadStubCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004907
4908 private:
4909 // Bit positions in flag.
4910 static const int kAllCanReadBit = 0;
4911 static const int kAllCanWriteBit = 1;
4912 static const int kProhibitsOverwritingBit = 2;
4913 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
4914
4915 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
4916};
4917
4918
4919class AccessCheckInfo: public Struct {
4920 public:
4921 DECL_ACCESSORS(named_callback, Object)
4922 DECL_ACCESSORS(indexed_callback, Object)
4923 DECL_ACCESSORS(data, Object)
4924
4925 static inline AccessCheckInfo* cast(Object* obj);
4926
4927#ifdef DEBUG
4928 void AccessCheckInfoPrint();
4929 void AccessCheckInfoVerify();
4930#endif
4931
4932 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
4933 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
4934 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
4935 static const int kSize = kDataOffset + kPointerSize;
4936
4937 private:
4938 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
4939};
4940
4941
4942class InterceptorInfo: public Struct {
4943 public:
4944 DECL_ACCESSORS(getter, Object)
4945 DECL_ACCESSORS(setter, Object)
4946 DECL_ACCESSORS(query, Object)
4947 DECL_ACCESSORS(deleter, Object)
4948 DECL_ACCESSORS(enumerator, Object)
4949 DECL_ACCESSORS(data, Object)
4950
4951 static inline InterceptorInfo* cast(Object* obj);
4952
4953#ifdef DEBUG
4954 void InterceptorInfoPrint();
4955 void InterceptorInfoVerify();
4956#endif
4957
4958 static const int kGetterOffset = HeapObject::kHeaderSize;
4959 static const int kSetterOffset = kGetterOffset + kPointerSize;
4960 static const int kQueryOffset = kSetterOffset + kPointerSize;
4961 static const int kDeleterOffset = kQueryOffset + kPointerSize;
4962 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
4963 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
4964 static const int kSize = kDataOffset + kPointerSize;
4965
4966 private:
4967 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
4968};
4969
4970
4971class CallHandlerInfo: public Struct {
4972 public:
4973 DECL_ACCESSORS(callback, Object)
4974 DECL_ACCESSORS(data, Object)
4975
4976 static inline CallHandlerInfo* cast(Object* obj);
4977
4978#ifdef DEBUG
4979 void CallHandlerInfoPrint();
4980 void CallHandlerInfoVerify();
4981#endif
4982
4983 static const int kCallbackOffset = HeapObject::kHeaderSize;
4984 static const int kDataOffset = kCallbackOffset + kPointerSize;
4985 static const int kSize = kDataOffset + kPointerSize;
4986
4987 private:
4988 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
4989};
4990
4991
4992class TemplateInfo: public Struct {
4993 public:
4994 DECL_ACCESSORS(tag, Object)
4995 DECL_ACCESSORS(property_list, Object)
4996
4997#ifdef DEBUG
4998 void TemplateInfoVerify();
4999#endif
5000
5001 static const int kTagOffset = HeapObject::kHeaderSize;
5002 static const int kPropertyListOffset = kTagOffset + kPointerSize;
5003 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
5004 protected:
5005 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
5006 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
5007};
5008
5009
5010class FunctionTemplateInfo: public TemplateInfo {
5011 public:
5012 DECL_ACCESSORS(serial_number, Object)
5013 DECL_ACCESSORS(call_code, Object)
5014 DECL_ACCESSORS(property_accessors, Object)
5015 DECL_ACCESSORS(prototype_template, Object)
5016 DECL_ACCESSORS(parent_template, Object)
5017 DECL_ACCESSORS(named_property_handler, Object)
5018 DECL_ACCESSORS(indexed_property_handler, Object)
5019 DECL_ACCESSORS(instance_template, Object)
5020 DECL_ACCESSORS(class_name, Object)
5021 DECL_ACCESSORS(signature, Object)
5022 DECL_ACCESSORS(instance_call_handler, Object)
5023 DECL_ACCESSORS(access_check_info, Object)
5024 DECL_ACCESSORS(flag, Smi)
5025
5026 // Following properties use flag bits.
5027 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
5028 DECL_BOOLEAN_ACCESSORS(undetectable)
5029 // If the bit is set, object instances created by this function
5030 // requires access check.
5031 DECL_BOOLEAN_ACCESSORS(needs_access_check)
5032
5033 static inline FunctionTemplateInfo* cast(Object* obj);
5034
5035#ifdef DEBUG
5036 void FunctionTemplateInfoPrint();
5037 void FunctionTemplateInfoVerify();
5038#endif
5039
5040 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
5041 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
5042 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
5043 static const int kPrototypeTemplateOffset =
5044 kPropertyAccessorsOffset + kPointerSize;
5045 static const int kParentTemplateOffset =
5046 kPrototypeTemplateOffset + kPointerSize;
5047 static const int kNamedPropertyHandlerOffset =
5048 kParentTemplateOffset + kPointerSize;
5049 static const int kIndexedPropertyHandlerOffset =
5050 kNamedPropertyHandlerOffset + kPointerSize;
5051 static const int kInstanceTemplateOffset =
5052 kIndexedPropertyHandlerOffset + kPointerSize;
5053 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
5054 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
5055 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
5056 static const int kAccessCheckInfoOffset =
5057 kInstanceCallHandlerOffset + kPointerSize;
5058 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
5059 static const int kSize = kFlagOffset + kPointerSize;
5060
5061 private:
5062 // Bit position in the flag, from least significant bit position.
5063 static const int kHiddenPrototypeBit = 0;
5064 static const int kUndetectableBit = 1;
5065 static const int kNeedsAccessCheckBit = 2;
5066
5067 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
5068};
5069
5070
5071class ObjectTemplateInfo: public TemplateInfo {
5072 public:
5073 DECL_ACCESSORS(constructor, Object)
5074 DECL_ACCESSORS(internal_field_count, Object)
5075
5076 static inline ObjectTemplateInfo* cast(Object* obj);
5077
5078#ifdef DEBUG
5079 void ObjectTemplateInfoPrint();
5080 void ObjectTemplateInfoVerify();
5081#endif
5082
5083 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
5084 static const int kInternalFieldCountOffset =
5085 kConstructorOffset + kPointerSize;
5086 static const int kSize = kInternalFieldCountOffset + kPointerSize;
5087};
5088
5089
5090class SignatureInfo: public Struct {
5091 public:
5092 DECL_ACCESSORS(receiver, Object)
5093 DECL_ACCESSORS(args, Object)
5094
5095 static inline SignatureInfo* cast(Object* obj);
5096
5097#ifdef DEBUG
5098 void SignatureInfoPrint();
5099 void SignatureInfoVerify();
5100#endif
5101
5102 static const int kReceiverOffset = Struct::kHeaderSize;
5103 static const int kArgsOffset = kReceiverOffset + kPointerSize;
5104 static const int kSize = kArgsOffset + kPointerSize;
5105
5106 private:
5107 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
5108};
5109
5110
5111class TypeSwitchInfo: public Struct {
5112 public:
5113 DECL_ACCESSORS(types, Object)
5114
5115 static inline TypeSwitchInfo* cast(Object* obj);
5116
5117#ifdef DEBUG
5118 void TypeSwitchInfoPrint();
5119 void TypeSwitchInfoVerify();
5120#endif
5121
5122 static const int kTypesOffset = Struct::kHeaderSize;
5123 static const int kSize = kTypesOffset + kPointerSize;
5124};
5125
5126
5127#ifdef ENABLE_DEBUGGER_SUPPORT
5128// The DebugInfo class holds additional information for a function being
5129// debugged.
5130class DebugInfo: public Struct {
5131 public:
5132 // The shared function info for the source being debugged.
5133 DECL_ACCESSORS(shared, SharedFunctionInfo)
5134 // Code object for the original code.
5135 DECL_ACCESSORS(original_code, Code)
5136 // Code object for the patched code. This code object is the code object
5137 // currently active for the function.
5138 DECL_ACCESSORS(code, Code)
5139 // Fixed array holding status information for each active break point.
5140 DECL_ACCESSORS(break_points, FixedArray)
5141
5142 // Check if there is a break point at a code position.
5143 bool HasBreakPoint(int code_position);
5144 // Get the break point info object for a code position.
5145 Object* GetBreakPointInfo(int code_position);
5146 // Clear a break point.
5147 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
5148 int code_position,
5149 Handle<Object> break_point_object);
5150 // Set a break point.
5151 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
5152 int source_position, int statement_position,
5153 Handle<Object> break_point_object);
5154 // Get the break point objects for a code position.
5155 Object* GetBreakPointObjects(int code_position);
5156 // Find the break point info holding this break point object.
5157 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
5158 Handle<Object> break_point_object);
5159 // Get the number of break points for this function.
5160 int GetBreakPointCount();
5161
5162 static inline DebugInfo* cast(Object* obj);
5163
5164#ifdef DEBUG
5165 void DebugInfoPrint();
5166 void DebugInfoVerify();
5167#endif
5168
5169 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
5170 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
5171 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
5172 static const int kActiveBreakPointsCountIndex =
5173 kPatchedCodeIndex + kPointerSize;
5174 static const int kBreakPointsStateIndex =
5175 kActiveBreakPointsCountIndex + kPointerSize;
5176 static const int kSize = kBreakPointsStateIndex + kPointerSize;
5177
5178 private:
5179 static const int kNoBreakPointInfo = -1;
5180
5181 // Lookup the index in the break_points array for a code position.
5182 int GetBreakPointInfoIndex(int code_position);
5183
5184 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
5185};
5186
5187
5188// The BreakPointInfo class holds information for break points set in a
5189// function. The DebugInfo object holds a BreakPointInfo object for each code
5190// position with one or more break points.
5191class BreakPointInfo: public Struct {
5192 public:
5193 // The position in the code for the break point.
5194 DECL_ACCESSORS(code_position, Smi)
5195 // The position in the source for the break position.
5196 DECL_ACCESSORS(source_position, Smi)
5197 // The position in the source for the last statement before this break
5198 // position.
5199 DECL_ACCESSORS(statement_position, Smi)
5200 // List of related JavaScript break points.
5201 DECL_ACCESSORS(break_point_objects, Object)
5202
5203 // Removes a break point.
5204 static void ClearBreakPoint(Handle<BreakPointInfo> info,
5205 Handle<Object> break_point_object);
5206 // Set a break point.
5207 static void SetBreakPoint(Handle<BreakPointInfo> info,
5208 Handle<Object> break_point_object);
5209 // Check if break point info has this break point object.
5210 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
5211 Handle<Object> break_point_object);
5212 // Get the number of break points for this code position.
5213 int GetBreakPointCount();
5214
5215 static inline BreakPointInfo* cast(Object* obj);
5216
5217#ifdef DEBUG
5218 void BreakPointInfoPrint();
5219 void BreakPointInfoVerify();
5220#endif
5221
5222 static const int kCodePositionIndex = Struct::kHeaderSize;
5223 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
5224 static const int kStatementPositionIndex =
5225 kSourcePositionIndex + kPointerSize;
5226 static const int kBreakPointObjectsIndex =
5227 kStatementPositionIndex + kPointerSize;
5228 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
5229
5230 private:
5231 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
5232};
5233#endif // ENABLE_DEBUGGER_SUPPORT
5234
5235
5236#undef DECL_BOOLEAN_ACCESSORS
5237#undef DECL_ACCESSORS
5238
5239
5240// Abstract base class for visiting, and optionally modifying, the
5241// pointers contained in Objects. Used in GC and serialization/deserialization.
5242class ObjectVisitor BASE_EMBEDDED {
5243 public:
5244 virtual ~ObjectVisitor() {}
5245
5246 // Visits a contiguous arrays of pointers in the half-open range
5247 // [start, end). Any or all of the values may be modified on return.
5248 virtual void VisitPointers(Object** start, Object** end) = 0;
5249
5250 // To allow lazy clearing of inline caches the visitor has
5251 // a rich interface for iterating over Code objects..
5252
5253 // Visits a code target in the instruction stream.
5254 virtual void VisitCodeTarget(RelocInfo* rinfo);
5255
5256 // Visits a runtime entry in the instruction stream.
5257 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
5258
Steve Blockd0582a62009-12-15 09:54:21 +00005259 // Visits the resource of an ASCII or two-byte string.
5260 virtual void VisitExternalAsciiString(
5261 v8::String::ExternalAsciiStringResource** resource) {}
5262 virtual void VisitExternalTwoByteString(
5263 v8::String::ExternalStringResource** resource) {}
5264
Steve Blocka7e24c12009-10-30 11:49:00 +00005265 // Visits a debug call target in the instruction stream.
5266 virtual void VisitDebugTarget(RelocInfo* rinfo);
5267
5268 // Handy shorthand for visiting a single pointer.
5269 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
5270
5271 // Visits a contiguous arrays of external references (references to the C++
5272 // heap) in the half-open range [start, end). Any or all of the values
5273 // may be modified on return.
5274 virtual void VisitExternalReferences(Address* start, Address* end) {}
5275
5276 inline void VisitExternalReference(Address* p) {
5277 VisitExternalReferences(p, p + 1);
5278 }
5279
5280#ifdef DEBUG
5281 // Intended for serialization/deserialization checking: insert, or
5282 // check for the presence of, a tag at this position in the stream.
5283 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00005284#else
5285 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00005286#endif
5287};
5288
5289
5290// BooleanBit is a helper class for setting and getting a bit in an
5291// integer or Smi.
5292class BooleanBit : public AllStatic {
5293 public:
5294 static inline bool get(Smi* smi, int bit_position) {
5295 return get(smi->value(), bit_position);
5296 }
5297
5298 static inline bool get(int value, int bit_position) {
5299 return (value & (1 << bit_position)) != 0;
5300 }
5301
5302 static inline Smi* set(Smi* smi, int bit_position, bool v) {
5303 return Smi::FromInt(set(smi->value(), bit_position, v));
5304 }
5305
5306 static inline int set(int value, int bit_position, bool v) {
5307 if (v) {
5308 value |= (1 << bit_position);
5309 } else {
5310 value &= ~(1 << bit_position);
5311 }
5312 return value;
5313 }
5314};
5315
5316} } // namespace v8::internal
5317
5318#endif // V8_OBJECTS_H_