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