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