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