blob: 6029ad545b4a68c08945b9a2b1ad66e5a058b1f4 [file] [log] [blame]
Ben Murdochf87a2032010-10-22 12:50:53 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "smart-pointer.h"
33#include "unicode-inl.h"
Steve Block3ce2e202009-11-05 08:53:23 +000034#if V8_TARGET_ARCH_ARM
35#include "arm/constants-arm.h"
Andrei Popescu31002712010-02-23 13:46:05 +000036#elif V8_TARGET_ARCH_MIPS
37#include "mips/constants-mips.h"
Steve Block3ce2e202009-11-05 08:53:23 +000038#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40//
Kristian Monsen50ef84f2010-07-29 15:18:00 +010041// Most object types in the V8 JavaScript are described in this file.
Steve Blocka7e24c12009-10-30 11:49:00 +000042//
43// Inheritance hierarchy:
John Reck59135872010-11-02 12:39:01 -070044// - MaybeObject (an object or a failure)
45// - Failure (immediate for marking failed operation)
Steve Blocka7e24c12009-10-30 11:49:00 +000046// - Object
47// - Smi (immediate small integer)
Steve Blocka7e24c12009-10-30 11:49:00 +000048// - HeapObject (superclass for everything allocated in the heap)
49// - JSObject
50// - JSArray
51// - JSRegExp
52// - JSFunction
53// - GlobalObject
54// - JSGlobalObject
55// - JSBuiltinsObject
56// - JSGlobalProxy
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010057// - JSValue
58// - ByteArray
59// - PixelArray
60// - ExternalArray
61// - ExternalByteArray
62// - ExternalUnsignedByteArray
63// - ExternalShortArray
64// - ExternalUnsignedShortArray
65// - ExternalIntArray
66// - ExternalUnsignedIntArray
67// - ExternalFloatArray
68// - FixedArray
69// - DescriptorArray
70// - HashTable
71// - Dictionary
72// - SymbolTable
73// - CompilationCacheTable
74// - CodeCacheHashTable
75// - MapCache
76// - Context
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010077// - JSFunctionResultCache
Kristian Monsen50ef84f2010-07-29 15:18:00 +010078// - SerializedScopeInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000079// - String
80// - SeqString
81// - SeqAsciiString
82// - SeqTwoByteString
83// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000084// - ExternalString
85// - ExternalAsciiString
86// - ExternalTwoByteString
87// - HeapNumber
88// - Code
89// - Map
90// - Oddball
91// - Proxy
92// - SharedFunctionInfo
93// - Struct
94// - AccessorInfo
95// - AccessCheckInfo
96// - InterceptorInfo
97// - CallHandlerInfo
98// - TemplateInfo
99// - FunctionTemplateInfo
100// - ObjectTemplateInfo
101// - Script
102// - SignatureInfo
103// - TypeSwitchInfo
104// - DebugInfo
105// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100106// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000107//
108// Formats of Object*:
109// Smi: [31 bit signed int] 0
110// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
111// Failure: [30 bit signed int] 11
112
113// Ecma-262 3rd 8.6.1
114enum PropertyAttributes {
115 NONE = v8::None,
116 READ_ONLY = v8::ReadOnly,
117 DONT_ENUM = v8::DontEnum,
118 DONT_DELETE = v8::DontDelete,
119 ABSENT = 16 // Used in runtime to indicate a property is absent.
120 // ABSENT can never be stored in or returned from a descriptor's attributes
121 // bitfield. It is only used as a return value meaning the attributes of
122 // a non-existent property.
123};
124
125namespace v8 {
126namespace internal {
127
128
129// PropertyDetails captures type and attributes for a property.
130// They are used both in property dictionaries and instance descriptors.
131class PropertyDetails BASE_EMBEDDED {
132 public:
133
134 PropertyDetails(PropertyAttributes attributes,
135 PropertyType type,
136 int index = 0) {
137 ASSERT(TypeField::is_valid(type));
138 ASSERT(AttributesField::is_valid(attributes));
139 ASSERT(IndexField::is_valid(index));
140
141 value_ = TypeField::encode(type)
142 | AttributesField::encode(attributes)
143 | IndexField::encode(index);
144
145 ASSERT(type == this->type());
146 ASSERT(attributes == this->attributes());
147 ASSERT(index == this->index());
148 }
149
150 // Conversion for storing details as Object*.
151 inline PropertyDetails(Smi* smi);
152 inline Smi* AsSmi();
153
154 PropertyType type() { return TypeField::decode(value_); }
155
156 bool IsTransition() {
157 PropertyType t = type();
158 ASSERT(t != INTERCEPTOR);
159 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION;
160 }
161
162 bool IsProperty() {
163 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
164 }
165
166 PropertyAttributes attributes() { return AttributesField::decode(value_); }
167
168 int index() { return IndexField::decode(value_); }
169
170 inline PropertyDetails AsDeleted();
171
172 static bool IsValidIndex(int index) { return IndexField::is_valid(index); }
173
174 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
175 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
176 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
177 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
178
179 // Bit fields in value_ (type, shift, size). Must be public so the
180 // constants can be embedded in generated code.
181 class TypeField: public BitField<PropertyType, 0, 3> {};
182 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
183 class DeletedField: public BitField<uint32_t, 6, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000184 class IndexField: public BitField<uint32_t, 7, 32-7> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
186 static const int kInitialIndex = 1;
187 private:
188 uint32_t value_;
189};
190
191
192// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
193enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
194
195
196// PropertyNormalizationMode is used to specify whether to keep
197// inobject properties when normalizing properties of a JSObject.
198enum PropertyNormalizationMode {
199 CLEAR_INOBJECT_PROPERTIES,
200 KEEP_INOBJECT_PROPERTIES
201};
202
203
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100204// NormalizedMapSharingMode is used to specify whether a map may be shared
205// by different objects with normalized properties.
206enum NormalizedMapSharingMode {
207 UNIQUE_NORMALIZED_MAP,
208 SHARED_NORMALIZED_MAP
209};
210
211
Steve Block791712a2010-08-27 10:21:07 +0100212// Instance size sentinel for objects of variable size.
213static const int kVariableSizeSentinel = 0;
214
215
Steve Blocka7e24c12009-10-30 11:49:00 +0000216// All Maps have a field instance_type containing a InstanceType.
217// It describes the type of the instances.
218//
219// As an example, a JavaScript object is a heap object and its map
220// instance_type is JS_OBJECT_TYPE.
221//
222// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000223// mirror their encoding in the instance_type field of the map. The default
224// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
225// encoding is mentioned explicitly in the name. Likewise, the default
226// representation is considered sequential. It is not mentioned in the
227// name. The other representations (eg, CONS, EXTERNAL) are explicitly
228// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
229// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000230//
231// NOTE: The following things are some that depend on the string types having
232// instance_types that are less than those of all other types:
233// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
234// Object::IsString.
235//
236// NOTE: Everything following JS_VALUE_TYPE is considered a
237// JSObject for GC purposes. The first four entries here have typeof
238// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000239#define INSTANCE_TYPE_LIST_ALL(V) \
240 V(SYMBOL_TYPE) \
241 V(ASCII_SYMBOL_TYPE) \
242 V(CONS_SYMBOL_TYPE) \
243 V(CONS_ASCII_SYMBOL_TYPE) \
244 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100245 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000246 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
247 V(STRING_TYPE) \
248 V(ASCII_STRING_TYPE) \
249 V(CONS_STRING_TYPE) \
250 V(CONS_ASCII_STRING_TYPE) \
251 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100252 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000253 V(EXTERNAL_ASCII_STRING_TYPE) \
254 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
255 \
256 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000257 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000258 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100259 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000260 \
261 V(HEAP_NUMBER_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000262 V(PROXY_TYPE) \
263 V(BYTE_ARRAY_TYPE) \
264 V(PIXEL_ARRAY_TYPE) \
265 /* Note: the order of these external array */ \
266 /* types is relied upon in */ \
267 /* Object::IsExternalArray(). */ \
268 V(EXTERNAL_BYTE_ARRAY_TYPE) \
269 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
270 V(EXTERNAL_SHORT_ARRAY_TYPE) \
271 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
272 V(EXTERNAL_INT_ARRAY_TYPE) \
273 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
274 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
275 V(FILLER_TYPE) \
276 \
277 V(ACCESSOR_INFO_TYPE) \
278 V(ACCESS_CHECK_INFO_TYPE) \
279 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000280 V(CALL_HANDLER_INFO_TYPE) \
281 V(FUNCTION_TEMPLATE_INFO_TYPE) \
282 V(OBJECT_TEMPLATE_INFO_TYPE) \
283 V(SIGNATURE_INFO_TYPE) \
284 V(TYPE_SWITCH_INFO_TYPE) \
285 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100286 V(CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000287 \
Iain Merrick75681382010-08-19 15:07:18 +0100288 V(FIXED_ARRAY_TYPE) \
289 V(SHARED_FUNCTION_INFO_TYPE) \
290 \
Steve Blockd0582a62009-12-15 09:54:21 +0000291 V(JS_VALUE_TYPE) \
292 V(JS_OBJECT_TYPE) \
293 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
294 V(JS_GLOBAL_OBJECT_TYPE) \
295 V(JS_BUILTINS_OBJECT_TYPE) \
296 V(JS_GLOBAL_PROXY_TYPE) \
297 V(JS_ARRAY_TYPE) \
298 V(JS_REGEXP_TYPE) \
299 \
300 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000301
302#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000303#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
304 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 V(BREAK_POINT_INFO_TYPE)
306#else
307#define INSTANCE_TYPE_LIST_DEBUGGER(V)
308#endif
309
Steve Blockd0582a62009-12-15 09:54:21 +0000310#define INSTANCE_TYPE_LIST(V) \
311 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 INSTANCE_TYPE_LIST_DEBUGGER(V)
313
314
315// Since string types are not consecutive, this macro is used to
316// iterate over them.
317#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000318 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100319 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000320 symbol, \
321 Symbol) \
322 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100323 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000324 ascii_symbol, \
325 AsciiSymbol) \
326 V(CONS_SYMBOL_TYPE, \
327 ConsString::kSize, \
328 cons_symbol, \
329 ConsSymbol) \
330 V(CONS_ASCII_SYMBOL_TYPE, \
331 ConsString::kSize, \
332 cons_ascii_symbol, \
333 ConsAsciiSymbol) \
334 V(EXTERNAL_SYMBOL_TYPE, \
335 ExternalTwoByteString::kSize, \
336 external_symbol, \
337 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100338 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
339 ExternalTwoByteString::kSize, \
340 external_symbol_with_ascii_data, \
341 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000342 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
343 ExternalAsciiString::kSize, \
344 external_ascii_symbol, \
345 ExternalAsciiSymbol) \
346 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100347 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000348 string, \
349 String) \
350 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100351 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000352 ascii_string, \
353 AsciiString) \
354 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000356 cons_string, \
357 ConsString) \
358 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000360 cons_ascii_string, \
361 ConsAsciiString) \
362 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000364 external_string, \
365 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100366 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
367 ExternalTwoByteString::kSize, \
368 external_string_with_ascii_data, \
369 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000370 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000372 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100373 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000374
375// A struct is a simple object a set of object-valued fields. Including an
376// object type in this causes the compiler to generate most of the boilerplate
377// code for the class including allocation and garbage collection routines,
378// casts and predicates. All you need to define is the class, methods and
379// object verification routines. Easy, no?
380//
381// Note that for subtle reasons related to the ordering or numerical values of
382// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
383// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000384#define STRUCT_LIST_ALL(V) \
385 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
386 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
387 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
388 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
389 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
390 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
391 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
392 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100393 V(SCRIPT, Script, script) \
394 V(CODE_CACHE, CodeCache, code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000395
396#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000397#define STRUCT_LIST_DEBUGGER(V) \
398 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
400#else
401#define STRUCT_LIST_DEBUGGER(V)
402#endif
403
Steve Blockd0582a62009-12-15 09:54:21 +0000404#define STRUCT_LIST(V) \
405 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 STRUCT_LIST_DEBUGGER(V)
407
408// We use the full 8 bits of the instance_type field to encode heap object
409// instance types. The high-order bit (bit 7) is set if the object is not a
410// string, and cleared if it is a string.
411const uint32_t kIsNotStringMask = 0x80;
412const uint32_t kStringTag = 0x0;
413const uint32_t kNotStringTag = 0x80;
414
Leon Clarkee46be812010-01-19 14:06:41 +0000415// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
416// There are not enough types that the non-string types (with bit 7 set) can
417// have bit 6 set too.
418const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000419const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000420const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000421
Steve Blocka7e24c12009-10-30 11:49:00 +0000422// If bit 7 is clear then bit 2 indicates whether the string consists of
423// two-byte characters or one-byte characters.
424const uint32_t kStringEncodingMask = 0x4;
425const uint32_t kTwoByteStringTag = 0x0;
426const uint32_t kAsciiStringTag = 0x4;
427
428// If bit 7 is clear, the low-order 2 bits indicate the representation
429// of the string.
430const uint32_t kStringRepresentationMask = 0x03;
431enum StringRepresentationTag {
432 kSeqStringTag = 0x0,
433 kConsStringTag = 0x1,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100434 kExternalStringTag = 0x2
Steve Blocka7e24c12009-10-30 11:49:00 +0000435};
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100436const uint32_t kIsConsStringMask = 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000437
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100438// If bit 7 is clear, then bit 3 indicates whether this two-byte
439// string actually contains ascii data.
440const uint32_t kAsciiDataHintMask = 0x08;
441const uint32_t kAsciiDataHintTag = 0x08;
442
Steve Blocka7e24c12009-10-30 11:49:00 +0000443
444// A ConsString with an empty string as the right side is a candidate
445// for being shortcut by the garbage collector unless it is a
446// symbol. It's not common to have non-flat symbols, so we do not
447// shortcut them thereby avoiding turning symbols into strings. See
448// heap.cc and mark-compact.cc.
449const uint32_t kShortcutTypeMask =
450 kIsNotStringMask |
451 kIsSymbolMask |
452 kStringRepresentationMask;
453const uint32_t kShortcutTypeTag = kConsStringTag;
454
455
456enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000457 // String types.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100458 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000459 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100460 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000461 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100462 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
463 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
464 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000465 EXTERNAL_ASCII_SYMBOL_TYPE =
466 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100467 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000468 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100469 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000470 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100471 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
472 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
473 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000474 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
475 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000476
Leon Clarkee46be812010-01-19 14:06:41 +0000477 // Objects allocated in their own spaces (never in new space).
478 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 CODE_TYPE,
480 ODDBALL_TYPE,
481 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000482
483 // "Data", objects that cannot contain non-map-word pointers to heap
484 // objects.
485 HEAP_NUMBER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000486 PROXY_TYPE,
487 BYTE_ARRAY_TYPE,
488 PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000489 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000490 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
491 EXTERNAL_SHORT_ARRAY_TYPE,
492 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
493 EXTERNAL_INT_ARRAY_TYPE,
494 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000495 EXTERNAL_FLOAT_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
496 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000497
Leon Clarkee46be812010-01-19 14:06:41 +0000498 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 ACCESSOR_INFO_TYPE,
500 ACCESS_CHECK_INFO_TYPE,
501 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 CALL_HANDLER_INFO_TYPE,
503 FUNCTION_TEMPLATE_INFO_TYPE,
504 OBJECT_TEMPLATE_INFO_TYPE,
505 SIGNATURE_INFO_TYPE,
506 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000507 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100508 CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100509 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
510 // is defined. However as include/v8.h contain some of the instance type
511 // constants always having them avoids them getting different numbers
512 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 DEBUG_INFO_TYPE,
514 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000515
Leon Clarkee46be812010-01-19 14:06:41 +0000516 FIXED_ARRAY_TYPE,
517 SHARED_FUNCTION_INFO_TYPE,
518
519 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 JS_OBJECT_TYPE,
521 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
522 JS_GLOBAL_OBJECT_TYPE,
523 JS_BUILTINS_OBJECT_TYPE,
524 JS_GLOBAL_PROXY_TYPE,
525 JS_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000526 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000527
528 JS_FUNCTION_TYPE,
529
530 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000533 INVALID_TYPE = FIRST_TYPE - 1,
534 FIRST_NONSTRING_TYPE = MAP_TYPE,
535 // Boundaries for testing for an external array.
536 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
537 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_FLOAT_ARRAY_TYPE,
538 // Boundary for promotion to old data space/old pointer space.
539 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 // Boundaries for testing the type is a JavaScript "object". Note that
541 // function objects are not counted as objects, even though they are
542 // implemented as such; only values whose typeof is "object" are included.
543 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
544 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE
545};
546
547
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100548STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
549STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
550STATIC_CHECK(PROXY_TYPE == Internals::kProxyType);
551
552
Steve Blocka7e24c12009-10-30 11:49:00 +0000553enum CompareResult {
554 LESS = -1,
555 EQUAL = 0,
556 GREATER = 1,
557
558 NOT_EQUAL = GREATER
559};
560
561
562#define DECL_BOOLEAN_ACCESSORS(name) \
563 inline bool name(); \
564 inline void set_##name(bool value); \
565
566
567#define DECL_ACCESSORS(name, type) \
568 inline type* name(); \
569 inline void set_##name(type* value, \
570 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
571
572
573class StringStream;
574class ObjectVisitor;
575
576struct ValueInfo : public Malloced {
577 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
578 InstanceType type;
579 Object* ptr;
580 const char* str;
581 double number;
582};
583
584
585// A template-ized version of the IsXXX functions.
586template <class C> static inline bool Is(Object* obj);
587
John Reck59135872010-11-02 12:39:01 -0700588class MaybeObject BASE_EMBEDDED {
589 public:
590 inline bool IsFailure();
591 inline bool IsRetryAfterGC();
592 inline bool IsOutOfMemory();
593 inline bool IsException();
594 INLINE(bool IsTheHole());
595 inline bool ToObject(Object** obj) {
596 if (IsFailure()) return false;
597 *obj = reinterpret_cast<Object*>(this);
598 return true;
599 }
600 inline Object* ToObjectUnchecked() {
601 ASSERT(!IsFailure());
602 return reinterpret_cast<Object*>(this);
603 }
604 inline Object* ToObjectChecked() {
605 CHECK(!IsFailure());
606 return reinterpret_cast<Object*>(this);
607 }
608
609#ifdef DEBUG
610 // Prints this object with details.
611 void Print();
612 void PrintLn();
613 // Verifies the object.
614 void Verify();
615#endif
616};
Steve Blocka7e24c12009-10-30 11:49:00 +0000617
618// Object is the abstract superclass for all classes in the
619// object hierarchy.
620// Object does not use any virtual functions to avoid the
621// allocation of the C++ vtable.
622// Since Smi and Failure are subclasses of Object no
623// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700624class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 public:
626 // Type testing.
627 inline bool IsSmi();
628 inline bool IsHeapObject();
629 inline bool IsHeapNumber();
630 inline bool IsString();
631 inline bool IsSymbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000632 // See objects-inl.h for more details
633 inline bool IsSeqString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 inline bool IsExternalString();
635 inline bool IsExternalTwoByteString();
636 inline bool IsExternalAsciiString();
637 inline bool IsSeqTwoByteString();
638 inline bool IsSeqAsciiString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000639 inline bool IsConsString();
640
641 inline bool IsNumber();
642 inline bool IsByteArray();
643 inline bool IsPixelArray();
Steve Block3ce2e202009-11-05 08:53:23 +0000644 inline bool IsExternalArray();
645 inline bool IsExternalByteArray();
646 inline bool IsExternalUnsignedByteArray();
647 inline bool IsExternalShortArray();
648 inline bool IsExternalUnsignedShortArray();
649 inline bool IsExternalIntArray();
650 inline bool IsExternalUnsignedIntArray();
651 inline bool IsExternalFloatArray();
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 inline bool IsJSObject();
653 inline bool IsJSContextExtensionObject();
654 inline bool IsMap();
655 inline bool IsFixedArray();
656 inline bool IsDescriptorArray();
657 inline bool IsContext();
658 inline bool IsCatchContext();
659 inline bool IsGlobalContext();
660 inline bool IsJSFunction();
661 inline bool IsCode();
662 inline bool IsOddball();
663 inline bool IsSharedFunctionInfo();
664 inline bool IsJSValue();
665 inline bool IsStringWrapper();
666 inline bool IsProxy();
667 inline bool IsBoolean();
668 inline bool IsJSArray();
669 inline bool IsJSRegExp();
670 inline bool IsHashTable();
671 inline bool IsDictionary();
672 inline bool IsSymbolTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100673 inline bool IsJSFunctionResultCache();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100674 inline bool IsNormalizedMapCache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000675 inline bool IsCompilationCacheTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100676 inline bool IsCodeCacheHashTable();
Steve Blocka7e24c12009-10-30 11:49:00 +0000677 inline bool IsMapCache();
678 inline bool IsPrimitive();
679 inline bool IsGlobalObject();
680 inline bool IsJSGlobalObject();
681 inline bool IsJSBuiltinsObject();
682 inline bool IsJSGlobalProxy();
683 inline bool IsUndetectableObject();
684 inline bool IsAccessCheckNeeded();
685 inline bool IsJSGlobalPropertyCell();
686
687 // Returns true if this object is an instance of the specified
688 // function template.
689 inline bool IsInstanceOf(FunctionTemplateInfo* type);
690
691 inline bool IsStruct();
692#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
693 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
694#undef DECLARE_STRUCT_PREDICATE
695
696 // Oddball testing.
697 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000698 INLINE(bool IsNull());
699 INLINE(bool IsTrue());
700 INLINE(bool IsFalse());
701
702 // Extract the number.
703 inline double Number();
704
705 inline bool HasSpecificClassOf(String* name);
706
John Reck59135872010-11-02 12:39:01 -0700707 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
708 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000709
710 // Convert to a JSObject if needed.
711 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700712 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000713
714 // Converts this to a Smi if possible.
715 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700716 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000717
718 void Lookup(String* name, LookupResult* result);
719
720 // Property access.
John Reck59135872010-11-02 12:39:01 -0700721 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
722 MUST_USE_RESULT inline MaybeObject* GetProperty(
723 String* key,
724 PropertyAttributes* attributes);
725 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
726 Object* receiver,
727 String* key,
728 PropertyAttributes* attributes);
729 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
730 LookupResult* result,
731 String* key,
732 PropertyAttributes* attributes);
733 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
734 Object* structure,
735 String* name,
736 Object* holder);
737 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
738 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000739
John Reck59135872010-11-02 12:39:01 -0700740 inline MaybeObject* GetElement(uint32_t index);
741 // For use when we know that no exception can be thrown.
742 inline Object* GetElementNoExceptionThrown(uint32_t index);
743 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000744
745 // Return the object's prototype (might be Heap::null_value()).
746 Object* GetPrototype();
747
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100748 // Tries to convert an object to an array index. Returns true and sets
749 // the output parameter if it succeeds.
750 inline bool ToArrayIndex(uint32_t* index);
751
Steve Blocka7e24c12009-10-30 11:49:00 +0000752 // Returns true if this is a JSValue containing a string and the index is
753 // < the length of the string. Used to implement [] on strings.
754 inline bool IsStringObjectWithCharacterAt(uint32_t index);
755
756#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000757 // Verify a pointer is a valid object pointer.
758 static void VerifyPointer(Object* p);
759#endif
760
761 // Prints this object without details.
762 void ShortPrint();
763
764 // Prints this object without details to a message accumulator.
765 void ShortPrint(StringStream* accumulator);
766
767 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
768 static Object* cast(Object* value) { return value; }
769
770 // Layout description.
771 static const int kHeaderSize = 0; // Object does not take up any space.
772
773 private:
774 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
775};
776
777
778// Smi represents integer Numbers that can be stored in 31 bits.
779// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000780// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000781// For long smis it has the following format:
782// [32 bit signed int] [31 bits zero padding] 0
783// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000784class Smi: public Object {
785 public:
786 // Returns the integer value.
787 inline int value();
788
789 // Convert a value to a Smi object.
790 static inline Smi* FromInt(int value);
791
792 static inline Smi* FromIntptr(intptr_t value);
793
794 // Returns whether value can be represented in a Smi.
795 static inline bool IsValid(intptr_t value);
796
Steve Blocka7e24c12009-10-30 11:49:00 +0000797 // Casting.
798 static inline Smi* cast(Object* object);
799
800 // Dispatched behavior.
801 void SmiPrint();
802 void SmiPrint(StringStream* accumulator);
803#ifdef DEBUG
804 void SmiVerify();
805#endif
806
Steve Block3ce2e202009-11-05 08:53:23 +0000807 static const int kMinValue = (-1 << (kSmiValueSize - 1));
808 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000809
810 private:
811 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
812};
813
814
815// Failure is used for reporting out of memory situations and
816// propagating exceptions through the runtime system. Failure objects
817// are transient and cannot occur as part of the object graph.
818//
819// Failures are a single word, encoded as follows:
820// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +0100821// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000822// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000823// 7 6 4 32 10
824//
Steve Blocka7e24c12009-10-30 11:49:00 +0000825//
826// The low two bits, 0-1, are the failure tag, 11. The next two bits,
827// 2-3, are a failure type tag 'tt' with possible values:
828// 00 RETRY_AFTER_GC
829// 01 EXCEPTION
830// 10 INTERNAL_ERROR
831// 11 OUT_OF_MEMORY_EXCEPTION
832//
833// The next three bits, 4-6, are an allocation space tag 'sss'. The
834// allocation space tag is 000 for all failure types except
835// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
836// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +0000837
838// Failure type tag info.
839const int kFailureTypeTagSize = 2;
840const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
841
John Reck59135872010-11-02 12:39:01 -0700842class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000843 public:
844 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
845 enum Type {
846 RETRY_AFTER_GC = 0,
847 EXCEPTION = 1, // Returning this marker tells the real exception
848 // is in Top::pending_exception.
849 INTERNAL_ERROR = 2,
850 OUT_OF_MEMORY_EXCEPTION = 3
851 };
852
853 inline Type type() const;
854
855 // Returns the space that needs to be collected for RetryAfterGC failures.
856 inline AllocationSpace allocation_space() const;
857
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 inline bool IsInternalError() const;
859 inline bool IsOutOfMemoryException() const;
860
Ben Murdochf87a2032010-10-22 12:50:53 +0100861 static inline Failure* RetryAfterGC(AllocationSpace space);
862 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000863 static inline Failure* Exception();
864 static inline Failure* InternalError();
865 static inline Failure* OutOfMemoryException();
866 // Casting.
John Reck59135872010-11-02 12:39:01 -0700867 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000868
869 // Dispatched behavior.
870 void FailurePrint();
871 void FailurePrint(StringStream* accumulator);
872#ifdef DEBUG
873 void FailureVerify();
874#endif
875
876 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000877 inline intptr_t value() const;
878 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000879
880 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
881};
882
883
884// Heap objects typically have a map pointer in their first word. However,
885// during GC other data (eg, mark bits, forwarding addresses) is sometimes
886// encoded in the first word. The class MapWord is an abstraction of the
887// value in a heap object's first word.
888class MapWord BASE_EMBEDDED {
889 public:
890 // Normal state: the map word contains a map pointer.
891
892 // Create a map word from a map pointer.
893 static inline MapWord FromMap(Map* map);
894
895 // View this map word as a map pointer.
896 inline Map* ToMap();
897
898
899 // Scavenge collection: the map word of live objects in the from space
900 // contains a forwarding address (a heap object pointer in the to space).
901
902 // True if this map word is a forwarding address for a scavenge
903 // collection. Only valid during a scavenge collection (specifically,
904 // when all map words are heap object pointers, ie. not during a full GC).
905 inline bool IsForwardingAddress();
906
907 // Create a map word from a forwarding address.
908 static inline MapWord FromForwardingAddress(HeapObject* object);
909
910 // View this map word as a forwarding address.
911 inline HeapObject* ToForwardingAddress();
912
Steve Blocka7e24c12009-10-30 11:49:00 +0000913 // Marking phase of full collection: the map word of live objects is
914 // marked, and may be marked as overflowed (eg, the object is live, its
915 // children have not been visited, and it does not fit in the marking
916 // stack).
917
918 // True if this map word's mark bit is set.
919 inline bool IsMarked();
920
921 // Return this map word but with its mark bit set.
922 inline void SetMark();
923
924 // Return this map word but with its mark bit cleared.
925 inline void ClearMark();
926
927 // True if this map word's overflow bit is set.
928 inline bool IsOverflowed();
929
930 // Return this map word but with its overflow bit set.
931 inline void SetOverflow();
932
933 // Return this map word but with its overflow bit cleared.
934 inline void ClearOverflow();
935
936
937 // Compacting phase of a full compacting collection: the map word of live
938 // objects contains an encoding of the original map address along with the
939 // forwarding address (represented as an offset from the first live object
940 // in the same page as the (old) object address).
941
942 // Create a map word from a map address and a forwarding address offset.
943 static inline MapWord EncodeAddress(Address map_address, int offset);
944
945 // Return the map address encoded in this map word.
946 inline Address DecodeMapAddress(MapSpace* map_space);
947
948 // Return the forwarding offset encoded in this map word.
949 inline int DecodeOffset();
950
951
952 // During serialization: the map word is used to hold an encoded
953 // address, and possibly a mark bit (set and cleared with SetMark
954 // and ClearMark).
955
956 // Create a map word from an encoded address.
957 static inline MapWord FromEncodedAddress(Address address);
958
959 inline Address ToEncodedAddress();
960
961 // Bits used by the marking phase of the garbage collector.
962 //
963 // The first word of a heap object is normally a map pointer. The last two
964 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
965 // mark an object as live and/or overflowed:
966 // last bit = 0, marked as alive
967 // second bit = 1, overflowed
968 // An object is only marked as overflowed when it is marked as live while
969 // the marking stack is overflowed.
970 static const int kMarkingBit = 0; // marking bit
971 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
972 static const int kOverflowBit = 1; // overflow bit
973 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
974
Leon Clarkee46be812010-01-19 14:06:41 +0000975 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
976 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +0000977 // +-----------------+------------------+-----------------+
978 // |forwarding offset|page offset of map|page index of map|
979 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +0000980 // ^ ^ ^
981 // | | |
982 // | | kMapPageIndexBits
983 // | kMapPageOffsetBits
984 // kForwardingOffsetBits
985 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
986 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
987#ifdef V8_HOST_ARCH_64_BIT
988 static const int kMapPageIndexBits = 16;
989#else
990 // Use all the 32-bits to encode on a 32-bit platform.
991 static const int kMapPageIndexBits =
992 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
993#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000994
995 static const int kMapPageIndexShift = 0;
996 static const int kMapPageOffsetShift =
997 kMapPageIndexShift + kMapPageIndexBits;
998 static const int kForwardingOffsetShift =
999 kMapPageOffsetShift + kMapPageOffsetBits;
1000
Leon Clarkee46be812010-01-19 14:06:41 +00001001 // Bit masks covering the different parts the encoding.
1002 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001003 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001004 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001006 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001007 ~(kMapPageIndexMask | kMapPageOffsetMask);
1008
1009 private:
1010 // HeapObject calls the private constructor and directly reads the value.
1011 friend class HeapObject;
1012
1013 explicit MapWord(uintptr_t value) : value_(value) {}
1014
1015 uintptr_t value_;
1016};
1017
1018
1019// HeapObject is the superclass for all classes describing heap allocated
1020// objects.
1021class HeapObject: public Object {
1022 public:
1023 // [map]: Contains a map which contains the object's reflective
1024 // information.
1025 inline Map* map();
1026 inline void set_map(Map* value);
1027
1028 // During garbage collection, the map word of a heap object does not
1029 // necessarily contain a map pointer.
1030 inline MapWord map_word();
1031 inline void set_map_word(MapWord map_word);
1032
1033 // Converts an address to a HeapObject pointer.
1034 static inline HeapObject* FromAddress(Address address);
1035
1036 // Returns the address of this HeapObject.
1037 inline Address address();
1038
1039 // Iterates over pointers contained in the object (including the Map)
1040 void Iterate(ObjectVisitor* v);
1041
1042 // Iterates over all pointers contained in the object except the
1043 // first map pointer. The object type is given in the first
1044 // parameter. This function does not access the map pointer in the
1045 // object, and so is safe to call while the map pointer is modified.
1046 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1047
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 // Returns the heap object's size in bytes
1049 inline int Size();
1050
1051 // Given a heap object's map pointer, returns the heap size in bytes
1052 // Useful when the map pointer field is used for other purposes.
1053 // GC internal.
1054 inline int SizeFromMap(Map* map);
1055
1056 // Support for the marking heap objects during the marking phase of GC.
1057 // True if the object is marked live.
1058 inline bool IsMarked();
1059
1060 // Mutate this object's map pointer to indicate that the object is live.
1061 inline void SetMark();
1062
1063 // Mutate this object's map pointer to remove the indication that the
1064 // object is live (ie, partially restore the map pointer).
1065 inline void ClearMark();
1066
1067 // True if this object is marked as overflowed. Overflowed objects have
1068 // been reached and marked during marking of the heap, but their children
1069 // have not necessarily been marked and they have not been pushed on the
1070 // marking stack.
1071 inline bool IsOverflowed();
1072
1073 // Mutate this object's map pointer to indicate that the object is
1074 // overflowed.
1075 inline void SetOverflow();
1076
1077 // Mutate this object's map pointer to remove the indication that the
1078 // object is overflowed (ie, partially restore the map pointer).
1079 inline void ClearOverflow();
1080
1081 // Returns the field at offset in obj, as a read/write Object* reference.
1082 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001083 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001084 // during marking GC.
1085 static inline Object** RawField(HeapObject* obj, int offset);
1086
1087 // Casting.
1088 static inline HeapObject* cast(Object* obj);
1089
Leon Clarke4515c472010-02-03 11:58:03 +00001090 // Return the write barrier mode for this. Callers of this function
1091 // must be able to present a reference to an AssertNoAllocation
1092 // object as a sign that they are not going to use this function
1093 // from code that allocates and thus invalidates the returned write
1094 // barrier mode.
1095 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001096
1097 // Dispatched behavior.
1098 void HeapObjectShortPrint(StringStream* accumulator);
1099#ifdef DEBUG
1100 void HeapObjectPrint();
1101 void HeapObjectVerify();
1102 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001103 inline void VerifySmiField(int offset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001104
1105 void PrintHeader(const char* id);
1106
1107 // Verify a pointer is a valid HeapObject pointer that points to object
1108 // areas in the heap.
1109 static void VerifyHeapPointer(Object* p);
1110#endif
1111
1112 // Layout description.
1113 // First field in a heap object is map.
1114 static const int kMapOffset = Object::kHeaderSize;
1115 static const int kHeaderSize = kMapOffset + kPointerSize;
1116
1117 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1118
1119 protected:
1120 // helpers for calling an ObjectVisitor to iterate over pointers in the
1121 // half-open range [start, end) specified as integer offsets
1122 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1123 // as above, for the single element at "offset"
1124 inline void IteratePointer(ObjectVisitor* v, int offset);
1125
Steve Blocka7e24c12009-10-30 11:49:00 +00001126 private:
1127 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1128};
1129
1130
Iain Merrick75681382010-08-19 15:07:18 +01001131#define SLOT_ADDR(obj, offset) \
1132 reinterpret_cast<Object**>((obj)->address() + offset)
1133
1134// This class describes a body of an object of a fixed size
1135// in which all pointer fields are located in the [start_offset, end_offset)
1136// interval.
1137template<int start_offset, int end_offset, int size>
1138class FixedBodyDescriptor {
1139 public:
1140 static const int kStartOffset = start_offset;
1141 static const int kEndOffset = end_offset;
1142 static const int kSize = size;
1143
1144 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1145
1146 template<typename StaticVisitor>
1147 static inline void IterateBody(HeapObject* obj) {
1148 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1149 SLOT_ADDR(obj, end_offset));
1150 }
1151};
1152
1153
1154// This class describes a body of an object of a variable size
1155// in which all pointer fields are located in the [start_offset, object_size)
1156// interval.
1157template<int start_offset>
1158class FlexibleBodyDescriptor {
1159 public:
1160 static const int kStartOffset = start_offset;
1161
1162 static inline void IterateBody(HeapObject* obj,
1163 int object_size,
1164 ObjectVisitor* v);
1165
1166 template<typename StaticVisitor>
1167 static inline void IterateBody(HeapObject* obj, int object_size) {
1168 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1169 SLOT_ADDR(obj, object_size));
1170 }
1171};
1172
1173#undef SLOT_ADDR
1174
1175
Steve Blocka7e24c12009-10-30 11:49:00 +00001176// The HeapNumber class describes heap allocated numbers that cannot be
1177// represented in a Smi (small integer)
1178class HeapNumber: public HeapObject {
1179 public:
1180 // [value]: number value.
1181 inline double value();
1182 inline void set_value(double value);
1183
1184 // Casting.
1185 static inline HeapNumber* cast(Object* obj);
1186
1187 // Dispatched behavior.
1188 Object* HeapNumberToBoolean();
1189 void HeapNumberPrint();
1190 void HeapNumberPrint(StringStream* accumulator);
1191#ifdef DEBUG
1192 void HeapNumberVerify();
1193#endif
1194
Steve Block6ded16b2010-05-10 14:33:55 +01001195 inline int get_exponent();
1196 inline int get_sign();
1197
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 // Layout description.
1199 static const int kValueOffset = HeapObject::kHeaderSize;
1200 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1201 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1202 // little endian apart from non-EABI arm which is little endian with big
1203 // endian floating point word ordering!
Steve Block3ce2e202009-11-05 08:53:23 +00001204#if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI)
Steve Blocka7e24c12009-10-30 11:49:00 +00001205 static const int kMantissaOffset = kValueOffset;
1206 static const int kExponentOffset = kValueOffset + 4;
1207#else
1208 static const int kMantissaOffset = kValueOffset + 4;
1209 static const int kExponentOffset = kValueOffset;
1210# define BIG_ENDIAN_FLOATING_POINT 1
1211#endif
1212 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001213 static const uint32_t kSignMask = 0x80000000u;
1214 static const uint32_t kExponentMask = 0x7ff00000u;
1215 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001216 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001217 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001218 static const int kExponentBias = 1023;
1219 static const int kExponentShift = 20;
1220 static const int kMantissaBitsInTopWord = 20;
1221 static const int kNonMantissaBitsInTopWord = 12;
1222
1223 private:
1224 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1225};
1226
1227
1228// The JSObject describes real heap allocated JavaScript objects with
1229// properties.
1230// Note that the map of JSObject changes during execution to enable inline
1231// caching.
1232class JSObject: public HeapObject {
1233 public:
1234 enum DeleteMode { NORMAL_DELETION, FORCE_DELETION };
1235 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001236 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001238 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001240 PIXEL_ELEMENTS,
1241 EXTERNAL_BYTE_ELEMENTS,
1242 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1243 EXTERNAL_SHORT_ELEMENTS,
1244 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1245 EXTERNAL_INT_ELEMENTS,
1246 EXTERNAL_UNSIGNED_INT_ELEMENTS,
1247 EXTERNAL_FLOAT_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001248 };
1249
1250 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001251 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001252 // slow case.
1253 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1254 inline void initialize_properties();
1255 inline bool HasFastProperties();
1256 inline StringDictionary* property_dictionary(); // Gets slow properties.
1257
1258 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001259 //
1260 // Elements can be in two general modes: fast and slow. Each mode
1261 // corrensponds to a set of object representations of elements that
1262 // have something in common.
1263 //
1264 // In the fast mode elements is a FixedArray and so each element can
1265 // be quickly accessed. This fact is used in the generated code. The
1266 // elements array can have one of the two maps in this mode:
1267 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1268 // arrays). In the latter case the elements array may be shared by a
1269 // few objects and so before writing to any element the array must
1270 // be copied. Use EnsureWritableFastElements in this case.
1271 //
1272 // In the slow mode elements is either a NumberDictionary or a
1273 // PixelArray or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001274 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001275 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001276 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001277 inline ElementsKind GetElementsKind();
1278 inline bool HasFastElements();
1279 inline bool HasDictionaryElements();
1280 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001281 inline bool HasExternalArrayElements();
1282 inline bool HasExternalByteElements();
1283 inline bool HasExternalUnsignedByteElements();
1284 inline bool HasExternalShortElements();
1285 inline bool HasExternalUnsignedShortElements();
1286 inline bool HasExternalIntElements();
1287 inline bool HasExternalUnsignedIntElements();
1288 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001289 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001290 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001291 // Requires: this->HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001292 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001293
1294 // Collects elements starting at index 0.
1295 // Undefined values are placed after non-undefined values.
1296 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001297 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001298 // As PrepareElementsForSort, but only on objects where elements is
1299 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001300 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001301
John Reck59135872010-11-02 12:39:01 -07001302 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1303 Object* value,
1304 PropertyAttributes attributes);
1305 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1306 String* key,
1307 Object* value,
1308 PropertyAttributes attributes);
1309 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1310 LookupResult* result,
1311 String* name,
1312 Object* value);
1313 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(Object* structure,
1314 String* name,
1315 Object* value,
1316 JSObject* holder);
1317 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1318 Object* value);
1319 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1320 String* name,
1321 Object* value,
1322 PropertyAttributes attributes);
1323 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1324 String* name,
1325 Object* value,
1326 PropertyAttributes attributes);
1327 MUST_USE_RESULT MaybeObject* IgnoreAttributesAndSetLocalProperty(
1328 String* key,
1329 Object* value,
1330 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001331
1332 // Retrieve a value in a normalized object given a lookup result.
1333 // Handles the special representation of JS global objects.
1334 Object* GetNormalizedProperty(LookupResult* result);
1335
1336 // Sets the property value in a normalized object given a lookup result.
1337 // Handles the special representation of JS global objects.
1338 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1339
1340 // Sets the property value in a normalized object given (key, value, details).
1341 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001342 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1343 Object* value,
1344 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001345
1346 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001347 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1348 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001349
Steve Blocka7e24c12009-10-30 11:49:00 +00001350 // Returns the class name ([[Class]] property in the specification).
1351 String* class_name();
1352
1353 // Returns the constructor name (the name (possibly, inferred name) of the
1354 // function that was used to instantiate the object).
1355 String* constructor_name();
1356
1357 // Retrieve interceptors.
1358 InterceptorInfo* GetNamedInterceptor();
1359 InterceptorInfo* GetIndexedInterceptor();
1360
1361 inline PropertyAttributes GetPropertyAttribute(String* name);
1362 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1363 String* name);
1364 PropertyAttributes GetLocalPropertyAttribute(String* name);
1365
John Reck59135872010-11-02 12:39:01 -07001366 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1367 bool is_getter,
1368 JSFunction* fun,
1369 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001370 Object* LookupAccessor(String* name, bool is_getter);
1371
John Reck59135872010-11-02 12:39:01 -07001372 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001373
Steve Blocka7e24c12009-10-30 11:49:00 +00001374 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001375 MaybeObject* GetPropertyWithFailedAccessCheck(
1376 Object* receiver,
1377 LookupResult* result,
1378 String* name,
1379 PropertyAttributes* attributes);
1380 MaybeObject* GetPropertyWithInterceptor(
1381 JSObject* receiver,
1382 String* name,
1383 PropertyAttributes* attributes);
1384 MaybeObject* GetPropertyPostInterceptor(
1385 JSObject* receiver,
1386 String* name,
1387 PropertyAttributes* attributes);
1388 MaybeObject* GetLocalPropertyPostInterceptor(JSObject* receiver,
1389 String* name,
1390 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001391
1392 // Returns true if this is an instance of an api function and has
1393 // been modified since it was created. May give false positives.
1394 bool IsDirty();
1395
1396 bool HasProperty(String* name) {
1397 return GetPropertyAttribute(name) != ABSENT;
1398 }
1399
1400 // Can cause a GC if it hits an interceptor.
1401 bool HasLocalProperty(String* name) {
1402 return GetLocalPropertyAttribute(name) != ABSENT;
1403 }
1404
Steve Blockd0582a62009-12-15 09:54:21 +00001405 // If the receiver is a JSGlobalProxy this method will return its prototype,
1406 // otherwise the result is the receiver itself.
1407 inline Object* BypassGlobalProxy();
1408
1409 // Accessors for hidden properties object.
1410 //
1411 // Hidden properties are not local properties of the object itself.
1412 // Instead they are stored on an auxiliary JSObject stored as a local
1413 // property with a special name Heap::hidden_symbol(). But if the
1414 // receiver is a JSGlobalProxy then the auxiliary object is a property
1415 // of its prototype.
1416 //
1417 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1418 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1419 // holder.
1420 //
1421 // These accessors do not touch interceptors or accessors.
1422 inline bool HasHiddenPropertiesObject();
1423 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001424 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1425 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001426
John Reck59135872010-11-02 12:39:01 -07001427 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1428 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001429
1430 // Tests for the fast common case for property enumeration.
1431 bool IsSimpleEnum();
1432
1433 // Do we want to keep the elements in fast case when increasing the
1434 // capacity?
1435 bool ShouldConvertToSlowElements(int new_capacity);
1436 // Returns true if the backing storage for the slow-case elements of
1437 // this object takes up nearly as much space as a fast-case backing
1438 // storage would. In that case the JSObject should have fast
1439 // elements.
1440 bool ShouldConvertToFastElements();
1441
1442 // Return the object's prototype (might be Heap::null_value()).
1443 inline Object* GetPrototype();
1444
Andrei Popescu402d9372010-02-26 13:31:12 +00001445 // Set the object's prototype (only JSObject and null are allowed).
John Reck59135872010-11-02 12:39:01 -07001446 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1447 bool skip_hidden_prototypes);
Andrei Popescu402d9372010-02-26 13:31:12 +00001448
Steve Blocka7e24c12009-10-30 11:49:00 +00001449 // Tells whether the index'th element is present.
1450 inline bool HasElement(uint32_t index);
1451 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001452
1453 // Tells whether the index'th element is present and how it is stored.
1454 enum LocalElementType {
1455 // There is no element with given index.
1456 UNDEFINED_ELEMENT,
1457
1458 // Element with given index is handled by interceptor.
1459 INTERCEPTED_ELEMENT,
1460
1461 // Element with given index is character in string.
1462 STRING_CHARACTER_ELEMENT,
1463
1464 // Element with given index is stored in fast backing store.
1465 FAST_ELEMENT,
1466
1467 // Element with given index is stored in slow backing store.
1468 DICTIONARY_ELEMENT
1469 };
1470
1471 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001472
1473 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1474 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1475
John Reck59135872010-11-02 12:39:01 -07001476 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001477
1478 // Set the index'th array element.
1479 // A Failure object is returned if GC is needed.
John Reck59135872010-11-02 12:39:01 -07001480 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001481
1482 // Returns the index'th element.
1483 // The undefined object if index is out of bounds.
John Reck59135872010-11-02 12:39:01 -07001484 MaybeObject* GetElementWithReceiver(JSObject* receiver, uint32_t index);
1485 MaybeObject* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001486
John Reck59135872010-11-02 12:39:01 -07001487 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1488 int length);
1489 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001490
1491 // Lookup interceptors are used for handling properties controlled by host
1492 // objects.
1493 inline bool HasNamedInterceptor();
1494 inline bool HasIndexedInterceptor();
1495
1496 // Support functions for v8 api (needed for correct interceptor behavior).
1497 bool HasRealNamedProperty(String* key);
1498 bool HasRealElementProperty(uint32_t index);
1499 bool HasRealNamedCallbackProperty(String* key);
1500
1501 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001502 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001503
1504 // Get the header size for a JSObject. Used to compute the index of
1505 // internal fields as well as the number of internal fields.
1506 inline int GetHeaderSize();
1507
1508 inline int GetInternalFieldCount();
1509 inline Object* GetInternalField(int index);
1510 inline void SetInternalField(int index, Object* value);
1511
1512 // Lookup a property. If found, the result is valid and has
1513 // detailed information.
1514 void LocalLookup(String* name, LookupResult* result);
1515 void Lookup(String* name, LookupResult* result);
1516
1517 // The following lookup functions skip interceptors.
1518 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1519 void LookupRealNamedProperty(String* name, LookupResult* result);
1520 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1521 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Leon Clarkef7060e22010-06-03 12:02:55 +01001522 bool SetElementWithCallbackSetterInPrototypes(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001523 void LookupCallback(String* name, LookupResult* result);
1524
1525 // Returns the number of properties on this object filtering out properties
1526 // with the specified attributes (ignoring interceptors).
1527 int NumberOfLocalProperties(PropertyAttributes filter);
1528 // Returns the number of enumerable properties (ignoring interceptors).
1529 int NumberOfEnumProperties();
1530 // Fill in details for properties into storage starting at the specified
1531 // index.
1532 void GetLocalPropertyNames(FixedArray* storage, int index);
1533
1534 // Returns the number of properties on this object filtering out properties
1535 // with the specified attributes (ignoring interceptors).
1536 int NumberOfLocalElements(PropertyAttributes filter);
1537 // Returns the number of enumerable elements (ignoring interceptors).
1538 int NumberOfEnumElements();
1539 // Returns the number of elements on this object filtering out elements
1540 // with the specified attributes (ignoring interceptors).
1541 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1542 // Count and fill in the enumerable elements into storage.
1543 // (storage->length() == NumberOfEnumElements()).
1544 // If storage is NULL, will count the elements without adding
1545 // them to any storage.
1546 // Returns the number of enumerable elements.
1547 int GetEnumElementKeys(FixedArray* storage);
1548
1549 // Add a property to a fast-case object using a map transition to
1550 // new_map.
John Reck59135872010-11-02 12:39:01 -07001551 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1552 String* name,
1553 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001554
1555 // Add a constant function property to a fast-case object.
1556 // This leaves a CONSTANT_TRANSITION in the old map, and
1557 // if it is called on a second object with this map, a
1558 // normal property is added instead, with a map transition.
1559 // This avoids the creation of many maps with the same constant
1560 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001561 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1562 String* name,
1563 JSFunction* function,
1564 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001565
John Reck59135872010-11-02 12:39:01 -07001566 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1567 String* name,
1568 Object* value,
1569 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001570
1571 // Converts a descriptor of any other type to a real field,
1572 // backed by the properties array. Descriptors of visible
1573 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1574 // Converts the descriptor on the original object's map to a
1575 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001576 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001577 String* name,
1578 Object* new_value,
1579 PropertyAttributes attributes);
1580
1581 // Converts a descriptor of any other type to a real field,
1582 // backed by the properties array. Descriptors of visible
1583 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001584 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1585 String* name,
1586 Object* new_value,
1587 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001588
1589 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001590 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1591 Object* value,
1592 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001593
1594 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001595 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1596 Object* value,
1597 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001598
1599 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001600 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1601 Object* value,
1602 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001603
1604 // Convert the object to use the canonical dictionary
1605 // representation. If the object is expected to have additional properties
1606 // added this number can be indicated to have the backing store allocated to
1607 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001608 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1609 PropertyNormalizationMode mode,
1610 int expected_additional_properties);
1611 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001612
John Reck59135872010-11-02 12:39:01 -07001613 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001614
Steve Blocka7e24c12009-10-30 11:49:00 +00001615 // Transform slow named properties to fast variants.
1616 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001617 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1618 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001619
1620 // Access fast-case object properties at index.
1621 inline Object* FastPropertyAt(int index);
1622 inline Object* FastPropertyAtPut(int index, Object* value);
1623
1624 // Access to in object properties.
1625 inline Object* InObjectPropertyAt(int index);
1626 inline Object* InObjectPropertyAtPut(int index,
1627 Object* value,
1628 WriteBarrierMode mode
1629 = UPDATE_WRITE_BARRIER);
1630
1631 // initializes the body after properties slot, properties slot is
1632 // initialized by set_properties
1633 // Note: this call does not update write barrier, it is caller's
1634 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001635 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001636
1637 // Check whether this object references another object
1638 bool ReferencesObject(Object* obj);
1639
1640 // Casting.
1641 static inline JSObject* cast(Object* obj);
1642
Steve Block8defd9f2010-07-08 12:39:36 +01001643 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001644 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001645
1646
Steve Blocka7e24c12009-10-30 11:49:00 +00001647 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001648 void JSObjectShortPrint(StringStream* accumulator);
1649#ifdef DEBUG
1650 void JSObjectPrint();
1651 void JSObjectVerify();
1652 void PrintProperties();
1653 void PrintElements();
1654
1655 // Structure for collecting spill information about JSObjects.
1656 class SpillInformation {
1657 public:
1658 void Clear();
1659 void Print();
1660 int number_of_objects_;
1661 int number_of_objects_with_fast_properties_;
1662 int number_of_objects_with_fast_elements_;
1663 int number_of_fast_used_fields_;
1664 int number_of_fast_unused_fields_;
1665 int number_of_slow_used_properties_;
1666 int number_of_slow_unused_properties_;
1667 int number_of_fast_used_elements_;
1668 int number_of_fast_unused_elements_;
1669 int number_of_slow_used_elements_;
1670 int number_of_slow_unused_elements_;
1671 };
1672
1673 void IncrementSpillStatistics(SpillInformation* info);
1674#endif
1675 Object* SlowReverseLookup(Object* value);
1676
Steve Block8defd9f2010-07-08 12:39:36 +01001677 // Maximal number of fast properties for the JSObject. Used to
1678 // restrict the number of map transitions to avoid an explosion in
1679 // the number of maps for objects used as dictionaries.
1680 inline int MaxFastProperties();
1681
Leon Clarkee46be812010-01-19 14:06:41 +00001682 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1683 // Also maximal value of JSArray's length property.
1684 static const uint32_t kMaxElementCount = 0xffffffffu;
1685
Steve Blocka7e24c12009-10-30 11:49:00 +00001686 static const uint32_t kMaxGap = 1024;
1687 static const int kMaxFastElementsLength = 5000;
1688 static const int kInitialMaxFastElementArray = 100000;
1689 static const int kMaxFastProperties = 8;
1690 static const int kMaxInstanceSize = 255 * kPointerSize;
1691 // When extending the backing storage for property values, we increase
1692 // its size by more than the 1 entry necessary, so sequentially adding fields
1693 // to the same object requires fewer allocations and copies.
1694 static const int kFieldsAdded = 3;
1695
1696 // Layout description.
1697 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1698 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1699 static const int kHeaderSize = kElementsOffset + kPointerSize;
1700
1701 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1702
Iain Merrick75681382010-08-19 15:07:18 +01001703 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1704 public:
1705 static inline int SizeOf(Map* map, HeapObject* object);
1706 };
1707
Steve Blocka7e24c12009-10-30 11:49:00 +00001708 private:
John Reck59135872010-11-02 12:39:01 -07001709 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1710 Object* structure,
1711 uint32_t index,
1712 Object* holder);
1713 MaybeObject* SetElementWithCallback(Object* structure,
1714 uint32_t index,
1715 Object* value,
1716 JSObject* holder);
1717 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(uint32_t index,
1718 Object* value);
1719 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(uint32_t index,
1720 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001721
John Reck59135872010-11-02 12:39:01 -07001722 MaybeObject* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001723
John Reck59135872010-11-02 12:39:01 -07001724 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1725 DeleteMode mode);
1726 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001727
John Reck59135872010-11-02 12:39:01 -07001728 MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1729 DeleteMode mode);
1730 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001731
1732 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1733 String* name,
1734 bool continue_search);
1735 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1736 String* name,
1737 bool continue_search);
1738 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1739 Object* receiver,
1740 LookupResult* result,
1741 String* name,
1742 bool continue_search);
1743 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1744 LookupResult* result,
1745 String* name,
1746 bool continue_search);
1747
1748 // Returns true if most of the elements backing storage is used.
1749 bool HasDenseElements();
1750
Leon Clarkef7060e22010-06-03 12:02:55 +01001751 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07001752 MUST_USE_RESULT MaybeObject* SetElementCallback(
1753 uint32_t index,
1754 Object* structure,
1755 PropertyAttributes attributes);
1756 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1757 String* name,
1758 Object* structure,
1759 PropertyAttributes attributes);
1760 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
1761 String* name,
1762 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001763
1764 void LookupInDescriptor(String* name, LookupResult* result);
1765
1766 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1767};
1768
1769
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001770// FixedArray describes fixed-sized arrays with element type Object*.
1771class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001772 public:
1773 // [length]: length of the array.
1774 inline int length();
1775 inline void set_length(int value);
1776
Steve Blocka7e24c12009-10-30 11:49:00 +00001777 // Setter and getter for elements.
1778 inline Object* get(int index);
1779 // Setter that uses write barrier.
1780 inline void set(int index, Object* value);
1781
1782 // Setter that doesn't need write barrier).
1783 inline void set(int index, Smi* value);
1784 // Setter with explicit barrier mode.
1785 inline void set(int index, Object* value, WriteBarrierMode mode);
1786
1787 // Setters for frequently used oddballs located in old space.
1788 inline void set_undefined(int index);
1789 inline void set_null(int index);
1790 inline void set_the_hole(int index);
1791
Iain Merrick75681382010-08-19 15:07:18 +01001792 // Setters with less debug checks for the GC to use.
1793 inline void set_unchecked(int index, Smi* value);
1794 inline void set_null_unchecked(int index);
Ben Murdochf87a2032010-10-22 12:50:53 +01001795 inline void set_unchecked(int index, Object* value, WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01001796
Steve Block6ded16b2010-05-10 14:33:55 +01001797 // Gives access to raw memory which stores the array's data.
1798 inline Object** data_start();
1799
Steve Blocka7e24c12009-10-30 11:49:00 +00001800 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07001801 MUST_USE_RESULT inline MaybeObject* Copy();
1802 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001803
1804 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07001805 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00001806
1807 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07001808 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00001809
1810 // Copy a sub array from the receiver to dest.
1811 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1812
1813 // Garbage collection support.
1814 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1815
1816 // Code Generation support.
1817 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1818
1819 // Casting.
1820 static inline FixedArray* cast(Object* obj);
1821
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001822 // Layout description.
1823 // Length is smi tagged when it is stored.
1824 static const int kLengthOffset = HeapObject::kHeaderSize;
1825 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001826
1827 // Maximal allowed size, in bytes, of a single FixedArray.
1828 // Prevents overflowing size computations, as well as extreme memory
1829 // consumption.
1830 static const int kMaxSize = 512 * MB;
1831 // Maximally allowed length of a FixedArray.
1832 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001833
1834 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001835#ifdef DEBUG
1836 void FixedArrayPrint();
1837 void FixedArrayVerify();
1838 // Checks if two FixedArrays have identical contents.
1839 bool IsEqualTo(FixedArray* other);
1840#endif
1841
1842 // Swap two elements in a pair of arrays. If this array and the
1843 // numbers array are the same object, the elements are only swapped
1844 // once.
1845 void SwapPairs(FixedArray* numbers, int i, int j);
1846
1847 // Sort prefix of this array and the numbers array as pairs wrt. the
1848 // numbers. If the numbers array and the this array are the same
1849 // object, the prefix of this array is sorted.
1850 void SortPairs(FixedArray* numbers, uint32_t len);
1851
Iain Merrick75681382010-08-19 15:07:18 +01001852 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
1853 public:
1854 static inline int SizeOf(Map* map, HeapObject* object) {
1855 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
1856 }
1857 };
1858
Steve Blocka7e24c12009-10-30 11:49:00 +00001859 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001860 // Set operation on FixedArray without using write barriers. Can
1861 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001862 static inline void fast_set(FixedArray* array, int index, Object* value);
1863
1864 private:
1865 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1866};
1867
1868
1869// DescriptorArrays are fixed arrays used to hold instance descriptors.
1870// The format of the these objects is:
1871// [0]: point to a fixed array with (value, detail) pairs.
1872// [1]: next enumeration index (Smi), or pointer to small fixed array:
1873// [0]: next enumeration index (Smi)
1874// [1]: pointer to fixed array with enum cache
1875// [2]: first key
1876// [length() - 1]: last key
1877//
1878class DescriptorArray: public FixedArray {
1879 public:
1880 // Is this the singleton empty_descriptor_array?
1881 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001882
Steve Blocka7e24c12009-10-30 11:49:00 +00001883 // Returns the number of descriptors in the array.
1884 int number_of_descriptors() {
1885 return IsEmpty() ? 0 : length() - kFirstIndex;
1886 }
1887
1888 int NextEnumerationIndex() {
1889 if (IsEmpty()) return PropertyDetails::kInitialIndex;
1890 Object* obj = get(kEnumerationIndexIndex);
1891 if (obj->IsSmi()) {
1892 return Smi::cast(obj)->value();
1893 } else {
1894 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1895 return Smi::cast(index)->value();
1896 }
1897 }
1898
1899 // Set next enumeration index and flush any enum cache.
1900 void SetNextEnumerationIndex(int value) {
1901 if (!IsEmpty()) {
1902 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1903 }
1904 }
1905 bool HasEnumCache() {
1906 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
1907 }
1908
1909 Object* GetEnumCache() {
1910 ASSERT(HasEnumCache());
1911 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1912 return bridge->get(kEnumCacheBridgeCacheIndex);
1913 }
1914
1915 // Initialize or change the enum cache,
1916 // using the supplied storage for the small "bridge".
1917 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1918
1919 // Accessors for fetching instance descriptor at descriptor number.
1920 inline String* GetKey(int descriptor_number);
1921 inline Object* GetValue(int descriptor_number);
1922 inline Smi* GetDetails(int descriptor_number);
1923 inline PropertyType GetType(int descriptor_number);
1924 inline int GetFieldIndex(int descriptor_number);
1925 inline JSFunction* GetConstantFunction(int descriptor_number);
1926 inline Object* GetCallbacksObject(int descriptor_number);
1927 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
1928 inline bool IsProperty(int descriptor_number);
1929 inline bool IsTransition(int descriptor_number);
1930 inline bool IsNullDescriptor(int descriptor_number);
1931 inline bool IsDontEnum(int descriptor_number);
1932
1933 // Accessor for complete descriptor.
1934 inline void Get(int descriptor_number, Descriptor* desc);
1935 inline void Set(int descriptor_number, Descriptor* desc);
1936
1937 // Transfer complete descriptor from another descriptor array to
1938 // this one.
1939 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
1940
1941 // Copy the descriptor array, insert a new descriptor and optionally
1942 // remove map transitions. If the descriptor is already present, it is
1943 // replaced. If a replaced descriptor is a real property (not a transition
1944 // or null), its enumeration index is kept as is.
1945 // If adding a real property, map transitions must be removed. If adding
1946 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07001947 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
1948 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00001949
1950 // Remove all transitions. Return a copy of the array with all transitions
1951 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07001952 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001953
1954 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001955 // Does not check for duplicates.
1956 void SortUnchecked();
1957
1958 // Sort the instance descriptors by the hash codes of their keys.
1959 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00001960 void Sort();
1961
1962 // Search the instance descriptors for given name.
1963 inline int Search(String* name);
1964
Iain Merrick75681382010-08-19 15:07:18 +01001965 // As the above, but uses DescriptorLookupCache and updates it when
1966 // necessary.
1967 inline int SearchWithCache(String* name);
1968
Steve Blocka7e24c12009-10-30 11:49:00 +00001969 // Tells whether the name is present int the array.
1970 bool Contains(String* name) { return kNotFound != Search(name); }
1971
1972 // Perform a binary search in the instance descriptors represented
1973 // by this fixed array. low and high are descriptor indices. If there
1974 // are three instance descriptors in this array it should be called
1975 // with low=0 and high=2.
1976 int BinarySearch(String* name, int low, int high);
1977
1978 // Perform a linear search in the instance descriptors represented
1979 // by this fixed array. len is the number of descriptor indices that are
1980 // valid. Does not require the descriptors to be sorted.
1981 int LinearSearch(String* name, int len);
1982
1983 // Allocates a DescriptorArray, but returns the singleton
1984 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07001985 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00001986
1987 // Casting.
1988 static inline DescriptorArray* cast(Object* obj);
1989
1990 // Constant for denoting key was not found.
1991 static const int kNotFound = -1;
1992
1993 static const int kContentArrayIndex = 0;
1994 static const int kEnumerationIndexIndex = 1;
1995 static const int kFirstIndex = 2;
1996
1997 // The length of the "bridge" to the enum cache.
1998 static const int kEnumCacheBridgeLength = 2;
1999 static const int kEnumCacheBridgeEnumIndex = 0;
2000 static const int kEnumCacheBridgeCacheIndex = 1;
2001
2002 // Layout description.
2003 static const int kContentArrayOffset = FixedArray::kHeaderSize;
2004 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2005 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2006
2007 // Layout description for the bridge array.
2008 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2009 static const int kEnumCacheBridgeCacheOffset =
2010 kEnumCacheBridgeEnumOffset + kPointerSize;
2011
2012#ifdef DEBUG
2013 // Print all the descriptors.
2014 void PrintDescriptors();
2015
2016 // Is the descriptor array sorted and without duplicates?
2017 bool IsSortedNoDuplicates();
2018
2019 // Are two DescriptorArrays equal?
2020 bool IsEqualTo(DescriptorArray* other);
2021#endif
2022
2023 // The maximum number of descriptors we want in a descriptor array (should
2024 // fit in a page).
2025 static const int kMaxNumberOfDescriptors = 1024 + 512;
2026
2027 private:
2028 // Conversion from descriptor number to array indices.
2029 static int ToKeyIndex(int descriptor_number) {
2030 return descriptor_number+kFirstIndex;
2031 }
Leon Clarkee46be812010-01-19 14:06:41 +00002032
2033 static int ToDetailsIndex(int descriptor_number) {
2034 return (descriptor_number << 1) + 1;
2035 }
2036
Steve Blocka7e24c12009-10-30 11:49:00 +00002037 static int ToValueIndex(int descriptor_number) {
2038 return descriptor_number << 1;
2039 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002040
2041 bool is_null_descriptor(int descriptor_number) {
2042 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2043 NULL_DESCRIPTOR;
2044 }
2045 // Swap operation on FixedArray without using write barriers.
2046 static inline void fast_swap(FixedArray* array, int first, int second);
2047
2048 // Swap descriptor first and second.
2049 inline void Swap(int first, int second);
2050
2051 FixedArray* GetContentArray() {
2052 return FixedArray::cast(get(kContentArrayIndex));
2053 }
2054 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2055};
2056
2057
2058// HashTable is a subclass of FixedArray that implements a hash table
2059// that uses open addressing and quadratic probing.
2060//
2061// In order for the quadratic probing to work, elements that have not
2062// yet been used and elements that have been deleted are
2063// distinguished. Probing continues when deleted elements are
2064// encountered and stops when unused elements are encountered.
2065//
2066// - Elements with key == undefined have not been used yet.
2067// - Elements with key == null have been deleted.
2068//
2069// The hash table class is parameterized with a Shape and a Key.
2070// Shape must be a class with the following interface:
2071// class ExampleShape {
2072// public:
2073// // Tells whether key matches other.
2074// static bool IsMatch(Key key, Object* other);
2075// // Returns the hash value for key.
2076// static uint32_t Hash(Key key);
2077// // Returns the hash value for object.
2078// static uint32_t HashForObject(Key key, Object* object);
2079// // Convert key to an object.
2080// static inline Object* AsObject(Key key);
2081// // The prefix size indicates number of elements in the beginning
2082// // of the backing storage.
2083// static const int kPrefixSize = ..;
2084// // The Element size indicates number of elements per entry.
2085// static const int kEntrySize = ..;
2086// };
Steve Block3ce2e202009-11-05 08:53:23 +00002087// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002088// beginning of the backing storage that can be used for non-element
2089// information by subclasses.
2090
2091template<typename Shape, typename Key>
2092class HashTable: public FixedArray {
2093 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002094 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002095 int NumberOfElements() {
2096 return Smi::cast(get(kNumberOfElementsIndex))->value();
2097 }
2098
Leon Clarkee46be812010-01-19 14:06:41 +00002099 // Returns the number of deleted elements in the hash table.
2100 int NumberOfDeletedElements() {
2101 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2102 }
2103
Steve Block3ce2e202009-11-05 08:53:23 +00002104 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002105 int Capacity() {
2106 return Smi::cast(get(kCapacityIndex))->value();
2107 }
2108
2109 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002110 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002111 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2112
2113 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002114 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002115 void ElementRemoved() {
2116 SetNumberOfElements(NumberOfElements() - 1);
2117 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2118 }
2119 void ElementsRemoved(int n) {
2120 SetNumberOfElements(NumberOfElements() - n);
2121 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2122 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002123
Steve Block3ce2e202009-11-05 08:53:23 +00002124 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002125 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002126 int at_least_space_for,
2127 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002128
2129 // Returns the key at entry.
2130 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2131
2132 // Tells whether k is a real key. Null and undefined are not allowed
2133 // as keys and can be used to indicate missing or deleted elements.
2134 bool IsKey(Object* k) {
2135 return !k->IsNull() && !k->IsUndefined();
2136 }
2137
2138 // Garbage collection support.
2139 void IteratePrefix(ObjectVisitor* visitor);
2140 void IterateElements(ObjectVisitor* visitor);
2141
2142 // Casting.
2143 static inline HashTable* cast(Object* obj);
2144
2145 // Compute the probe offset (quadratic probing).
2146 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2147 return (n + n * n) >> 1;
2148 }
2149
2150 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002151 static const int kNumberOfDeletedElementsIndex = 1;
2152 static const int kCapacityIndex = 2;
2153 static const int kPrefixStartIndex = 3;
2154 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002155 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002156 static const int kEntrySize = Shape::kEntrySize;
2157 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002158 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002159 static const int kCapacityOffset =
2160 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002161
2162 // Constant used for denoting a absent entry.
2163 static const int kNotFound = -1;
2164
Leon Clarkee46be812010-01-19 14:06:41 +00002165 // Maximal capacity of HashTable. Based on maximal length of underlying
2166 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2167 // cannot overflow.
2168 static const int kMaxCapacity =
2169 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2170
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002171 // Find entry for key otherwise return kNotFound.
Steve Blocka7e24c12009-10-30 11:49:00 +00002172 int FindEntry(Key key);
2173
2174 protected:
2175
2176 // Find the entry at which to insert element with the given key that
2177 // has the given hash value.
2178 uint32_t FindInsertionEntry(uint32_t hash);
2179
2180 // Returns the index for an entry (of the key)
2181 static inline int EntryToIndex(int entry) {
2182 return (entry * kEntrySize) + kElementsStartIndex;
2183 }
2184
Steve Block3ce2e202009-11-05 08:53:23 +00002185 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002186 void SetNumberOfElements(int nof) {
2187 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2188 }
2189
Leon Clarkee46be812010-01-19 14:06:41 +00002190 // Update the number of deleted elements in the hash table.
2191 void SetNumberOfDeletedElements(int nod) {
2192 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2193 }
2194
Steve Blocka7e24c12009-10-30 11:49:00 +00002195 // Sets the capacity of the hash table.
2196 void SetCapacity(int capacity) {
2197 // To scale a computed hash code to fit within the hash table, we
2198 // use bit-wise AND with a mask, so the capacity must be positive
2199 // and non-zero.
2200 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002201 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002202 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2203 }
2204
2205
2206 // Returns probe entry.
2207 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2208 ASSERT(IsPowerOf2(size));
2209 return (hash + GetProbeOffset(number)) & (size - 1);
2210 }
2211
Leon Clarkee46be812010-01-19 14:06:41 +00002212 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2213 return hash & (size - 1);
2214 }
2215
2216 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2217 return (last + number) & (size - 1);
2218 }
2219
Steve Blocka7e24c12009-10-30 11:49:00 +00002220 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002221 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002222};
2223
2224
2225
2226// HashTableKey is an abstract superclass for virtual key behavior.
2227class HashTableKey {
2228 public:
2229 // Returns whether the other object matches this key.
2230 virtual bool IsMatch(Object* other) = 0;
2231 // Returns the hash value for this key.
2232 virtual uint32_t Hash() = 0;
2233 // Returns the hash value for object.
2234 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002235 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002236 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002237 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002238 // Required.
2239 virtual ~HashTableKey() {}
2240};
2241
2242class SymbolTableShape {
2243 public:
2244 static bool IsMatch(HashTableKey* key, Object* value) {
2245 return key->IsMatch(value);
2246 }
2247 static uint32_t Hash(HashTableKey* key) {
2248 return key->Hash();
2249 }
2250 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2251 return key->HashForObject(object);
2252 }
John Reck59135872010-11-02 12:39:01 -07002253 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002254 return key->AsObject();
2255 }
2256
2257 static const int kPrefixSize = 0;
2258 static const int kEntrySize = 1;
2259};
2260
2261// SymbolTable.
2262//
2263// No special elements in the prefix and the element size is 1
2264// because only the symbol itself (the key) needs to be stored.
2265class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2266 public:
2267 // Find symbol in the symbol table. If it is not there yet, it is
2268 // added. The return value is the symbol table which might have
2269 // been enlarged. If the return value is not a failure, the symbol
2270 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002271 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
2272 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002273
2274 // Looks up a symbol that is equal to the given string and returns
2275 // true if it is found, assigning the symbol to the given output
2276 // parameter.
2277 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002278 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002279
2280 // Casting.
2281 static inline SymbolTable* cast(Object* obj);
2282
2283 private:
John Reck59135872010-11-02 12:39:01 -07002284 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002285
2286 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2287};
2288
2289
2290class MapCacheShape {
2291 public:
2292 static bool IsMatch(HashTableKey* key, Object* value) {
2293 return key->IsMatch(value);
2294 }
2295 static uint32_t Hash(HashTableKey* key) {
2296 return key->Hash();
2297 }
2298
2299 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2300 return key->HashForObject(object);
2301 }
2302
John Reck59135872010-11-02 12:39:01 -07002303 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002304 return key->AsObject();
2305 }
2306
2307 static const int kPrefixSize = 0;
2308 static const int kEntrySize = 2;
2309};
2310
2311
2312// MapCache.
2313//
2314// Maps keys that are a fixed array of symbols to a map.
2315// Used for canonicalize maps for object literals.
2316class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2317 public:
2318 // Find cached value for a string key, otherwise return null.
2319 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002320 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002321 static inline MapCache* cast(Object* obj);
2322
2323 private:
2324 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2325};
2326
2327
2328template <typename Shape, typename Key>
2329class Dictionary: public HashTable<Shape, Key> {
2330 public:
2331
2332 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2333 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2334 }
2335
2336 // Returns the value at entry.
2337 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002338 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002339 }
2340
2341 // Set the value for entry.
2342 void ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002343 // Check that this value can actually be written.
2344 PropertyDetails details = DetailsAt(entry);
2345 // If a value has not been initilized we allow writing to it even if
2346 // it is read only (a declared const that has not been initialized).
2347 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) return;
Steve Block6ded16b2010-05-10 14:33:55 +01002348 this->set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002349 }
2350
2351 // Returns the property details for the property at entry.
2352 PropertyDetails DetailsAt(int entry) {
2353 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2354 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002355 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002356 }
2357
2358 // Set the details for entry.
2359 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002360 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002361 }
2362
2363 // Sorting support
2364 void CopyValuesTo(FixedArray* elements);
2365
2366 // Delete a property from the dictionary.
2367 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2368
2369 // Returns the number of elements in the dictionary filtering out properties
2370 // with the specified attributes.
2371 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2372
2373 // Returns the number of enumerable elements in the dictionary.
2374 int NumberOfEnumElements();
2375
2376 // Copies keys to preallocated fixed array.
2377 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2378 // Fill in details for properties into storage.
2379 void CopyKeysTo(FixedArray* storage);
2380
2381 // Accessors for next enumeration index.
2382 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002383 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002384 }
2385
2386 int NextEnumerationIndex() {
2387 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2388 }
2389
2390 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002391 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002392
2393 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002394 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002395
2396#ifdef DEBUG
2397 void Print();
2398#endif
2399 // Returns the key (slow).
2400 Object* SlowReverseLookup(Object* value);
2401
2402 // Sets the entry to (key, value) pair.
2403 inline void SetEntry(int entry,
2404 Object* key,
2405 Object* value,
2406 PropertyDetails details);
2407
John Reck59135872010-11-02 12:39:01 -07002408 MUST_USE_RESULT MaybeObject* Add(Key key,
2409 Object* value,
2410 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002411
2412 protected:
2413 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002414 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002415
2416 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002417 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2418 Object* value,
2419 PropertyDetails details,
2420 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002421
2422 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002423 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002424 static const int kMaxNumberKeyIndex =
2425 HashTable<Shape, Key>::kPrefixStartIndex;
2426 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2427};
2428
2429
2430class StringDictionaryShape {
2431 public:
2432 static inline bool IsMatch(String* key, Object* other);
2433 static inline uint32_t Hash(String* key);
2434 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002435 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002436 static const int kPrefixSize = 2;
2437 static const int kEntrySize = 3;
2438 static const bool kIsEnumerable = true;
2439};
2440
2441
2442class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2443 public:
2444 static inline StringDictionary* cast(Object* obj) {
2445 ASSERT(obj->IsDictionary());
2446 return reinterpret_cast<StringDictionary*>(obj);
2447 }
2448
2449 // Copies enumerable keys to preallocated fixed array.
2450 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2451
2452 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002453 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2454 JSObject* obj,
2455 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002456
2457 // Find entry for key otherwise return kNotFound. Optimzed version of
2458 // HashTable::FindEntry.
2459 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002460};
2461
2462
2463class NumberDictionaryShape {
2464 public:
2465 static inline bool IsMatch(uint32_t key, Object* other);
2466 static inline uint32_t Hash(uint32_t key);
2467 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002468 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002469 static const int kPrefixSize = 2;
2470 static const int kEntrySize = 3;
2471 static const bool kIsEnumerable = false;
2472};
2473
2474
2475class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2476 public:
2477 static NumberDictionary* cast(Object* obj) {
2478 ASSERT(obj->IsDictionary());
2479 return reinterpret_cast<NumberDictionary*>(obj);
2480 }
2481
2482 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002483 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2484 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2485 Object* value,
2486 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002487
2488 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002489 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2490 Object* value,
2491 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002492
2493 void UpdateMaxNumberKey(uint32_t key);
2494
2495 // If slow elements are required we will never go back to fast-case
2496 // for the elements kept in this dictionary. We require slow
2497 // elements if an element has been added at an index larger than
2498 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2499 // when defining a getter or setter with a number key.
2500 inline bool requires_slow_elements();
2501 inline void set_requires_slow_elements();
2502
2503 // Get the value of the max number key that has been added to this
2504 // dictionary. max_number_key can only be called if
2505 // requires_slow_elements returns false.
2506 inline uint32_t max_number_key();
2507
2508 // Remove all entries were key is a number and (from <= key && key < to).
2509 void RemoveNumberEntries(uint32_t from, uint32_t to);
2510
2511 // Bit masks.
2512 static const int kRequiresSlowElementsMask = 1;
2513 static const int kRequiresSlowElementsTagSize = 1;
2514 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2515};
2516
2517
Steve Block6ded16b2010-05-10 14:33:55 +01002518// JSFunctionResultCache caches results of some JSFunction invocation.
2519// It is a fixed array with fixed structure:
2520// [0]: factory function
2521// [1]: finger index
2522// [2]: current cache size
2523// [3]: dummy field.
2524// The rest of array are key/value pairs.
2525class JSFunctionResultCache: public FixedArray {
2526 public:
2527 static const int kFactoryIndex = 0;
2528 static const int kFingerIndex = kFactoryIndex + 1;
2529 static const int kCacheSizeIndex = kFingerIndex + 1;
2530 static const int kDummyIndex = kCacheSizeIndex + 1;
2531 static const int kEntriesIndex = kDummyIndex + 1;
2532
2533 static const int kEntrySize = 2; // key + value
2534
Kristian Monsen25f61362010-05-21 11:50:48 +01002535 static const int kFactoryOffset = kHeaderSize;
2536 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2537 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2538
Steve Block6ded16b2010-05-10 14:33:55 +01002539 inline void MakeZeroSize();
2540 inline void Clear();
2541
2542 // Casting
2543 static inline JSFunctionResultCache* cast(Object* obj);
2544
2545#ifdef DEBUG
2546 void JSFunctionResultCacheVerify();
2547#endif
2548};
2549
2550
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002551// The cache for maps used by normalized (dictionary mode) objects.
2552// Such maps do not have property descriptors, so a typical program
2553// needs very limited number of distinct normalized maps.
2554class NormalizedMapCache: public FixedArray {
2555 public:
2556 static const int kEntries = 64;
2557
John Reck59135872010-11-02 12:39:01 -07002558 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2559 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002560
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002561 void Clear();
2562
2563 // Casting
2564 static inline NormalizedMapCache* cast(Object* obj);
2565
2566#ifdef DEBUG
2567 void NormalizedMapCacheVerify();
2568#endif
2569
2570 private:
2571 static int Hash(Map* fast);
2572
2573 static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2574};
2575
2576
Steve Blocka7e24c12009-10-30 11:49:00 +00002577// ByteArray represents fixed sized byte arrays. Used by the outside world,
2578// such as PCRE, and also by the memory allocator and garbage collector to
2579// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002580class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002581 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002582 // [length]: length of the array.
2583 inline int length();
2584 inline void set_length(int value);
2585
Steve Blocka7e24c12009-10-30 11:49:00 +00002586 // Setter and getter.
2587 inline byte get(int index);
2588 inline void set(int index, byte value);
2589
2590 // Treat contents as an int array.
2591 inline int get_int(int index);
2592
2593 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002594 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002595 }
2596 // We use byte arrays for free blocks in the heap. Given a desired size in
2597 // bytes that is a multiple of the word size and big enough to hold a byte
2598 // array, this function returns the number of elements a byte array should
2599 // have.
2600 static int LengthFor(int size_in_bytes) {
2601 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2602 ASSERT(size_in_bytes >= kHeaderSize);
2603 return size_in_bytes - kHeaderSize;
2604 }
2605
2606 // Returns data start address.
2607 inline Address GetDataStartAddress();
2608
2609 // Returns a pointer to the ByteArray object for a given data start address.
2610 static inline ByteArray* FromDataStartAddress(Address address);
2611
2612 // Casting.
2613 static inline ByteArray* cast(Object* obj);
2614
2615 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002616 inline int ByteArraySize() {
2617 return SizeFor(this->length());
2618 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002619#ifdef DEBUG
2620 void ByteArrayPrint();
2621 void ByteArrayVerify();
2622#endif
2623
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002624 // Layout description.
2625 // Length is smi tagged when it is stored.
2626 static const int kLengthOffset = HeapObject::kHeaderSize;
2627 static const int kHeaderSize = kLengthOffset + kPointerSize;
2628
2629 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002630
Leon Clarkee46be812010-01-19 14:06:41 +00002631 // Maximal memory consumption for a single ByteArray.
2632 static const int kMaxSize = 512 * MB;
2633 // Maximal length of a single ByteArray.
2634 static const int kMaxLength = kMaxSize - kHeaderSize;
2635
Steve Blocka7e24c12009-10-30 11:49:00 +00002636 private:
2637 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2638};
2639
2640
2641// A PixelArray represents a fixed-size byte array with special semantics
2642// used for implementing the CanvasPixelArray object. Please see the
2643// specification at:
2644// http://www.whatwg.org/specs/web-apps/current-work/
2645// multipage/the-canvas-element.html#canvaspixelarray
2646// In particular, write access clamps the value written to 0 or 255 if the
2647// value written is outside this range.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002648class PixelArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002649 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002650 // [length]: length of the array.
2651 inline int length();
2652 inline void set_length(int value);
2653
Steve Blocka7e24c12009-10-30 11:49:00 +00002654 // [external_pointer]: The pointer to the external memory area backing this
2655 // pixel array.
2656 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2657
2658 // Setter and getter.
2659 inline uint8_t get(int index);
2660 inline void set(int index, uint8_t value);
2661
2662 // This accessor applies the correct conversion from Smi, HeapNumber and
2663 // undefined and clamps the converted value between 0 and 255.
2664 Object* SetValue(uint32_t index, Object* value);
2665
2666 // Casting.
2667 static inline PixelArray* cast(Object* obj);
2668
2669#ifdef DEBUG
2670 void PixelArrayPrint();
2671 void PixelArrayVerify();
2672#endif // DEBUG
2673
Steve Block3ce2e202009-11-05 08:53:23 +00002674 // Maximal acceptable length for a pixel array.
2675 static const int kMaxLength = 0x3fffffff;
2676
Steve Blocka7e24c12009-10-30 11:49:00 +00002677 // PixelArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002678 static const int kLengthOffset = HeapObject::kHeaderSize;
2679 static const int kExternalPointerOffset =
2680 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002681 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002682 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002683
2684 private:
2685 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2686};
2687
2688
Steve Block3ce2e202009-11-05 08:53:23 +00002689// An ExternalArray represents a fixed-size array of primitive values
2690// which live outside the JavaScript heap. Its subclasses are used to
2691// implement the CanvasArray types being defined in the WebGL
2692// specification. As of this writing the first public draft is not yet
2693// available, but Khronos members can access the draft at:
2694// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2695//
2696// The semantics of these arrays differ from CanvasPixelArray.
2697// Out-of-range values passed to the setter are converted via a C
2698// cast, not clamping. Out-of-range indices cause exceptions to be
2699// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002700class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002701 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002702 // [length]: length of the array.
2703 inline int length();
2704 inline void set_length(int value);
2705
Steve Block3ce2e202009-11-05 08:53:23 +00002706 // [external_pointer]: The pointer to the external memory area backing this
2707 // external array.
2708 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2709
2710 // Casting.
2711 static inline ExternalArray* cast(Object* obj);
2712
2713 // Maximal acceptable length for an external array.
2714 static const int kMaxLength = 0x3fffffff;
2715
2716 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002717 static const int kLengthOffset = HeapObject::kHeaderSize;
2718 static const int kExternalPointerOffset =
2719 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002720 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002721 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002722
2723 private:
2724 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2725};
2726
2727
2728class ExternalByteArray: public ExternalArray {
2729 public:
2730 // Setter and getter.
2731 inline int8_t get(int index);
2732 inline void set(int index, int8_t value);
2733
2734 // This accessor applies the correct conversion from Smi, HeapNumber
2735 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002736 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002737
2738 // Casting.
2739 static inline ExternalByteArray* cast(Object* obj);
2740
2741#ifdef DEBUG
2742 void ExternalByteArrayPrint();
2743 void ExternalByteArrayVerify();
2744#endif // DEBUG
2745
2746 private:
2747 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2748};
2749
2750
2751class ExternalUnsignedByteArray: public ExternalArray {
2752 public:
2753 // Setter and getter.
2754 inline uint8_t get(int index);
2755 inline void set(int index, uint8_t value);
2756
2757 // This accessor applies the correct conversion from Smi, HeapNumber
2758 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002759 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002760
2761 // Casting.
2762 static inline ExternalUnsignedByteArray* cast(Object* obj);
2763
2764#ifdef DEBUG
2765 void ExternalUnsignedByteArrayPrint();
2766 void ExternalUnsignedByteArrayVerify();
2767#endif // DEBUG
2768
2769 private:
2770 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2771};
2772
2773
2774class ExternalShortArray: public ExternalArray {
2775 public:
2776 // Setter and getter.
2777 inline int16_t get(int index);
2778 inline void set(int index, int16_t value);
2779
2780 // This accessor applies the correct conversion from Smi, HeapNumber
2781 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002782 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002783
2784 // Casting.
2785 static inline ExternalShortArray* cast(Object* obj);
2786
2787#ifdef DEBUG
2788 void ExternalShortArrayPrint();
2789 void ExternalShortArrayVerify();
2790#endif // DEBUG
2791
2792 private:
2793 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2794};
2795
2796
2797class ExternalUnsignedShortArray: public ExternalArray {
2798 public:
2799 // Setter and getter.
2800 inline uint16_t get(int index);
2801 inline void set(int index, uint16_t value);
2802
2803 // This accessor applies the correct conversion from Smi, HeapNumber
2804 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002805 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002806
2807 // Casting.
2808 static inline ExternalUnsignedShortArray* cast(Object* obj);
2809
2810#ifdef DEBUG
2811 void ExternalUnsignedShortArrayPrint();
2812 void ExternalUnsignedShortArrayVerify();
2813#endif // DEBUG
2814
2815 private:
2816 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2817};
2818
2819
2820class ExternalIntArray: public ExternalArray {
2821 public:
2822 // Setter and getter.
2823 inline int32_t get(int index);
2824 inline void set(int index, int32_t value);
2825
2826 // This accessor applies the correct conversion from Smi, HeapNumber
2827 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002828 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002829
2830 // Casting.
2831 static inline ExternalIntArray* cast(Object* obj);
2832
2833#ifdef DEBUG
2834 void ExternalIntArrayPrint();
2835 void ExternalIntArrayVerify();
2836#endif // DEBUG
2837
2838 private:
2839 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
2840};
2841
2842
2843class ExternalUnsignedIntArray: public ExternalArray {
2844 public:
2845 // Setter and getter.
2846 inline uint32_t get(int index);
2847 inline void set(int index, uint32_t value);
2848
2849 // This accessor applies the correct conversion from Smi, HeapNumber
2850 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002851 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002852
2853 // Casting.
2854 static inline ExternalUnsignedIntArray* cast(Object* obj);
2855
2856#ifdef DEBUG
2857 void ExternalUnsignedIntArrayPrint();
2858 void ExternalUnsignedIntArrayVerify();
2859#endif // DEBUG
2860
2861 private:
2862 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
2863};
2864
2865
2866class ExternalFloatArray: public ExternalArray {
2867 public:
2868 // Setter and getter.
2869 inline float get(int index);
2870 inline void set(int index, float value);
2871
2872 // This accessor applies the correct conversion from Smi, HeapNumber
2873 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002874 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002875
2876 // Casting.
2877 static inline ExternalFloatArray* cast(Object* obj);
2878
2879#ifdef DEBUG
2880 void ExternalFloatArrayPrint();
2881 void ExternalFloatArrayVerify();
2882#endif // DEBUG
2883
2884 private:
2885 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
2886};
2887
2888
Steve Blocka7e24c12009-10-30 11:49:00 +00002889// Code describes objects with on-the-fly generated machine code.
2890class Code: public HeapObject {
2891 public:
2892 // Opaque data type for encapsulating code flags like kind, inline
2893 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01002894 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
2895 // enumeration type has correct value range (see Issue 830 for more details).
2896 enum Flags {
2897 FLAGS_MIN_VALUE = kMinInt,
2898 FLAGS_MAX_VALUE = kMaxInt
2899 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002900
2901 enum Kind {
2902 FUNCTION,
2903 STUB,
2904 BUILTIN,
2905 LOAD_IC,
2906 KEYED_LOAD_IC,
2907 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002908 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00002909 STORE_IC,
2910 KEYED_STORE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002911 BINARY_OP_IC,
2912 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00002913 // Flags.
2914
2915 // Pseudo-kinds.
2916 REGEXP = BUILTIN,
2917 FIRST_IC_KIND = LOAD_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002918 LAST_IC_KIND = BINARY_OP_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00002919 };
2920
2921 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002922 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00002923 };
2924
2925#ifdef ENABLE_DISASSEMBLER
2926 // Printing
2927 static const char* Kind2String(Kind kind);
2928 static const char* ICState2String(InlineCacheState state);
2929 static const char* PropertyType2String(PropertyType type);
2930 void Disassemble(const char* name);
2931#endif // ENABLE_DISASSEMBLER
2932
2933 // [instruction_size]: Size of the native instructions
2934 inline int instruction_size();
2935 inline void set_instruction_size(int value);
2936
Leon Clarkeac952652010-07-15 11:15:24 +01002937 // [relocation_info]: Code relocation information
2938 DECL_ACCESSORS(relocation_info, ByteArray)
2939
2940 // Unchecked accessor to be used during GC.
2941 inline ByteArray* unchecked_relocation_info();
2942
Steve Blocka7e24c12009-10-30 11:49:00 +00002943 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00002944
Steve Blocka7e24c12009-10-30 11:49:00 +00002945 // [flags]: Various code flags.
2946 inline Flags flags();
2947 inline void set_flags(Flags flags);
2948
2949 // [flags]: Access to specific code flags.
2950 inline Kind kind();
2951 inline InlineCacheState ic_state(); // Only valid for IC stubs.
2952 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
2953 inline PropertyType type(); // Only valid for monomorphic IC stubs.
2954 inline int arguments_count(); // Only valid for call IC stubs.
2955
2956 // Testers for IC stub kinds.
2957 inline bool is_inline_cache_stub();
2958 inline bool is_load_stub() { return kind() == LOAD_IC; }
2959 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
2960 inline bool is_store_stub() { return kind() == STORE_IC; }
2961 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
2962 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002963 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00002964
Steve Block6ded16b2010-05-10 14:33:55 +01002965 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002966 inline int major_key();
2967 inline void set_major_key(int major);
Steve Blocka7e24c12009-10-30 11:49:00 +00002968
2969 // Flags operations.
2970 static inline Flags ComputeFlags(Kind kind,
2971 InLoopFlag in_loop = NOT_IN_LOOP,
2972 InlineCacheState ic_state = UNINITIALIZED,
2973 PropertyType type = NORMAL,
Steve Block8defd9f2010-07-08 12:39:36 +01002974 int argc = -1,
2975 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00002976
2977 static inline Flags ComputeMonomorphicFlags(
2978 Kind kind,
2979 PropertyType type,
Steve Block8defd9f2010-07-08 12:39:36 +01002980 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00002981 InLoopFlag in_loop = NOT_IN_LOOP,
2982 int argc = -1);
2983
2984 static inline Kind ExtractKindFromFlags(Flags flags);
2985 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
2986 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
2987 static inline PropertyType ExtractTypeFromFlags(Flags flags);
2988 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01002989 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00002990 static inline Flags RemoveTypeFromFlags(Flags flags);
2991
2992 // Convert a target address into a code object.
2993 static inline Code* GetCodeFromTargetAddress(Address address);
2994
Steve Block791712a2010-08-27 10:21:07 +01002995 // Convert an entry address into an object.
2996 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
2997
Steve Blocka7e24c12009-10-30 11:49:00 +00002998 // Returns the address of the first instruction.
2999 inline byte* instruction_start();
3000
Leon Clarkeac952652010-07-15 11:15:24 +01003001 // Returns the address right after the last instruction.
3002 inline byte* instruction_end();
3003
Steve Blocka7e24c12009-10-30 11:49:00 +00003004 // Returns the size of the instructions, padding, and relocation information.
3005 inline int body_size();
3006
3007 // Returns the address of the first relocation info (read backwards!).
3008 inline byte* relocation_start();
3009
3010 // Code entry point.
3011 inline byte* entry();
3012
3013 // Returns true if pc is inside this object's instructions.
3014 inline bool contains(byte* pc);
3015
Steve Blocka7e24c12009-10-30 11:49:00 +00003016 // Relocate the code by delta bytes. Called to signal that this code
3017 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003018 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003019
3020 // Migrate code described by desc.
3021 void CopyFrom(const CodeDesc& desc);
3022
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003023 // Returns the object size for a given body (used for allocation).
3024 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003025 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003026 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003027 }
3028
3029 // Calculate the size of the code object to report for log events. This takes
3030 // the layout of the code object into account.
3031 int ExecutableSize() {
3032 // Check that the assumptions about the layout of the code object holds.
3033 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3034 Code::kHeaderSize);
3035 return instruction_size() + Code::kHeaderSize;
3036 }
3037
3038 // Locating source position.
3039 int SourcePosition(Address pc);
3040 int SourceStatementPosition(Address pc);
3041
3042 // Casting.
3043 static inline Code* cast(Object* obj);
3044
3045 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003046 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003047 inline void CodeIterateBody(ObjectVisitor* v);
3048
3049 template<typename StaticVisitor>
3050 inline void CodeIterateBody();
Steve Blocka7e24c12009-10-30 11:49:00 +00003051#ifdef DEBUG
3052 void CodePrint();
3053 void CodeVerify();
3054#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003055 // Layout description.
3056 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003057 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003058 static const int kFlagsOffset = kRelocationInfoOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003059 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
3060 // Add padding to align the instruction start following right after
3061 // the Code object header.
3062 static const int kHeaderSize =
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003063 CODE_POINTER_ALIGN(kKindSpecificFlagsOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003064
3065 // Byte offsets within kKindSpecificFlagsOffset.
3066 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset + 1;
3067
3068 // Flags layout.
3069 static const int kFlagsICStateShift = 0;
3070 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003071 static const int kFlagsTypeShift = 4;
3072 static const int kFlagsKindShift = 7;
Steve Block8defd9f2010-07-08 12:39:36 +01003073 static const int kFlagsICHolderShift = 11;
3074 static const int kFlagsArgumentsCountShift = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00003075
Steve Block6ded16b2010-05-10 14:33:55 +01003076 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3077 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003078 static const int kFlagsTypeMask = 0x00000070; // 00001110000
3079 static const int kFlagsKindMask = 0x00000780; // 11110000000
Steve Block8defd9f2010-07-08 12:39:36 +01003080 static const int kFlagsCacheInPrototypeMapMask = 0x00000800;
3081 static const int kFlagsArgumentsCountMask = 0xFFFFF000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003082
3083 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003084 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003085
3086 private:
3087 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3088};
3089
3090
3091// All heap objects have a Map that describes their structure.
3092// A Map contains information about:
3093// - Size information about the object
3094// - How to iterate over an object (for garbage collection)
3095class Map: public HeapObject {
3096 public:
3097 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003098 // Size in bytes or kVariableSizeSentinel if instances do not have
3099 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003100 inline int instance_size();
3101 inline void set_instance_size(int value);
3102
3103 // Count of properties allocated in the object.
3104 inline int inobject_properties();
3105 inline void set_inobject_properties(int value);
3106
3107 // Count of property fields pre-allocated in the object when first allocated.
3108 inline int pre_allocated_property_fields();
3109 inline void set_pre_allocated_property_fields(int value);
3110
3111 // Instance type.
3112 inline InstanceType instance_type();
3113 inline void set_instance_type(InstanceType value);
3114
3115 // Tells how many unused property fields are available in the
3116 // instance (only used for JSObject in fast mode).
3117 inline int unused_property_fields();
3118 inline void set_unused_property_fields(int value);
3119
3120 // Bit field.
3121 inline byte bit_field();
3122 inline void set_bit_field(byte value);
3123
3124 // Bit field 2.
3125 inline byte bit_field2();
3126 inline void set_bit_field2(byte value);
3127
3128 // Tells whether the object in the prototype property will be used
3129 // for instances created from this function. If the prototype
3130 // property is set to a value that is not a JSObject, the prototype
3131 // property will not be used to create instances of the function.
3132 // See ECMA-262, 13.2.2.
3133 inline void set_non_instance_prototype(bool value);
3134 inline bool has_non_instance_prototype();
3135
Steve Block6ded16b2010-05-10 14:33:55 +01003136 // Tells whether function has special prototype property. If not, prototype
3137 // property will not be created when accessed (will return undefined),
3138 // and construction from this function will not be allowed.
3139 inline void set_function_with_prototype(bool value);
3140 inline bool function_with_prototype();
3141
Steve Blocka7e24c12009-10-30 11:49:00 +00003142 // Tells whether the instance with this map should be ignored by the
3143 // __proto__ accessor.
3144 inline void set_is_hidden_prototype() {
3145 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3146 }
3147
3148 inline bool is_hidden_prototype() {
3149 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3150 }
3151
3152 // Records and queries whether the instance has a named interceptor.
3153 inline void set_has_named_interceptor() {
3154 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3155 }
3156
3157 inline bool has_named_interceptor() {
3158 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3159 }
3160
3161 // Records and queries whether the instance has an indexed interceptor.
3162 inline void set_has_indexed_interceptor() {
3163 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3164 }
3165
3166 inline bool has_indexed_interceptor() {
3167 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3168 }
3169
3170 // Tells whether the instance is undetectable.
3171 // An undetectable object is a special class of JSObject: 'typeof' operator
3172 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3173 // a normal JS object. It is useful for implementing undetectable
3174 // document.all in Firefox & Safari.
3175 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3176 inline void set_is_undetectable() {
3177 set_bit_field(bit_field() | (1 << kIsUndetectable));
3178 }
3179
3180 inline bool is_undetectable() {
3181 return ((1 << kIsUndetectable) & bit_field()) != 0;
3182 }
3183
Steve Blocka7e24c12009-10-30 11:49:00 +00003184 // Tells whether the instance has a call-as-function handler.
3185 inline void set_has_instance_call_handler() {
3186 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3187 }
3188
3189 inline bool has_instance_call_handler() {
3190 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3191 }
3192
Steve Block8defd9f2010-07-08 12:39:36 +01003193 inline void set_is_extensible(bool value);
3194 inline bool is_extensible();
3195
3196 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003197 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3198 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003199 if (value) {
3200 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3201 } else {
3202 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3203 }
Leon Clarkee46be812010-01-19 14:06:41 +00003204 }
3205
Iain Merrick75681382010-08-19 15:07:18 +01003206 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003207 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003208 }
3209
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003210 // Tells whether the map is attached to SharedFunctionInfo
3211 // (for inobject slack tracking).
3212 inline void set_attached_to_shared_function_info(bool value);
3213
3214 inline bool attached_to_shared_function_info();
3215
3216 // Tells whether the map is shared between objects that may have different
3217 // behavior. If true, the map should never be modified, instead a clone
3218 // should be created and modified.
3219 inline void set_is_shared(bool value);
3220
3221 inline bool is_shared();
3222
Steve Blocka7e24c12009-10-30 11:49:00 +00003223 // Tells whether the instance needs security checks when accessing its
3224 // properties.
3225 inline void set_is_access_check_needed(bool access_check_needed);
3226 inline bool is_access_check_needed();
3227
3228 // [prototype]: implicit prototype object.
3229 DECL_ACCESSORS(prototype, Object)
3230
3231 // [constructor]: points back to the function responsible for this map.
3232 DECL_ACCESSORS(constructor, Object)
3233
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003234 inline JSFunction* unchecked_constructor();
3235
Steve Blocka7e24c12009-10-30 11:49:00 +00003236 // [instance descriptors]: describes the object.
3237 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3238
3239 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003240 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003241
John Reck59135872010-11-02 12:39:01 -07003242 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003243
John Reck59135872010-11-02 12:39:01 -07003244 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3245 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00003246
3247 // Returns a copy of the map, with all transitions dropped from the
3248 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07003249 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00003250
Steve Block8defd9f2010-07-08 12:39:36 +01003251 // Returns this map if it has the fast elements bit set, otherwise
3252 // returns a copy of the map, with all transitions dropped from the
3253 // descriptors and the fast elements bit set.
John Reck59135872010-11-02 12:39:01 -07003254 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003255
3256 // Returns this map if it has the fast elements bit cleared,
3257 // otherwise returns a copy of the map, with all transitions dropped
3258 // from the descriptors and the fast elements bit cleared.
John Reck59135872010-11-02 12:39:01 -07003259 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003260
Steve Blocka7e24c12009-10-30 11:49:00 +00003261 // Returns the property index for name (only valid for FAST MODE).
3262 int PropertyIndexFor(String* name);
3263
3264 // Returns the next free property index (only valid for FAST MODE).
3265 int NextFreePropertyIndex();
3266
3267 // Returns the number of properties described in instance_descriptors.
3268 int NumberOfDescribedProperties();
3269
3270 // Casting.
3271 static inline Map* cast(Object* obj);
3272
3273 // Locate an accessor in the instance descriptor.
3274 AccessorDescriptor* FindAccessor(String* name);
3275
3276 // Code cache operations.
3277
3278 // Clears the code cache.
3279 inline void ClearCodeCache();
3280
3281 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07003282 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003283
3284 // Returns the found code or undefined if absent.
3285 Object* FindInCodeCache(String* name, Code::Flags flags);
3286
3287 // Returns the non-negative index of the code object if it is in the
3288 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003289 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003290
3291 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003292 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003293
3294 // For every transition in this map, makes the transition's
3295 // target's prototype pointer point back to this map.
3296 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3297 void CreateBackPointers();
3298
3299 // Set all map transitions from this map to dead maps to null.
3300 // Also, restore the original prototype on the targets of these
3301 // transitions, so that we do not process this map again while
3302 // following back pointers.
3303 void ClearNonLiveTransitions(Object* real_prototype);
3304
3305 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00003306#ifdef DEBUG
3307 void MapPrint();
3308 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003309 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00003310#endif
3311
Iain Merrick75681382010-08-19 15:07:18 +01003312 inline int visitor_id();
3313 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003314
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003315 typedef void (*TraverseCallback)(Map* map, void* data);
3316
3317 void TraverseTransitionTree(TraverseCallback callback, void* data);
3318
Steve Blocka7e24c12009-10-30 11:49:00 +00003319 static const int kMaxPreAllocatedPropertyFields = 255;
3320
3321 // Layout description.
3322 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3323 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3324 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3325 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3326 static const int kInstanceDescriptorsOffset =
3327 kConstructorOffset + kPointerSize;
3328 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003329 static const int kPadStart = kCodeCacheOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003330 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3331
3332 // Layout of pointer fields. Heap iteration code relies on them
3333 // being continiously allocated.
3334 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3335 static const int kPointerFieldsEndOffset =
3336 Map::kCodeCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003337
3338 // Byte offsets within kInstanceSizesOffset.
3339 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3340 static const int kInObjectPropertiesByte = 1;
3341 static const int kInObjectPropertiesOffset =
3342 kInstanceSizesOffset + kInObjectPropertiesByte;
3343 static const int kPreAllocatedPropertyFieldsByte = 2;
3344 static const int kPreAllocatedPropertyFieldsOffset =
3345 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003346 static const int kVisitorIdByte = 3;
3347 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00003348
3349 // Byte offsets within kInstanceAttributesOffset attributes.
3350 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3351 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3352 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3353 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3354
3355 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3356
3357 // Bit positions for bit field.
3358 static const int kUnused = 0; // To be used for marking recently used maps.
3359 static const int kHasNonInstancePrototype = 1;
3360 static const int kIsHiddenPrototype = 2;
3361 static const int kHasNamedInterceptor = 3;
3362 static const int kHasIndexedInterceptor = 4;
3363 static const int kIsUndetectable = 5;
3364 static const int kHasInstanceCallHandler = 6;
3365 static const int kIsAccessCheckNeeded = 7;
3366
3367 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003368 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003369 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003370 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003371 static const int kStringWrapperSafeForDefaultValueOf = 3;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003372 static const int kAttachedToSharedFunctionInfo = 4;
3373 static const int kIsShared = 5;
Steve Block6ded16b2010-05-10 14:33:55 +01003374
3375 // Layout of the default cache. It holds alternating name and code objects.
3376 static const int kCodeCacheEntrySize = 2;
3377 static const int kCodeCacheEntryNameOffset = 0;
3378 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003379
Iain Merrick75681382010-08-19 15:07:18 +01003380 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3381 kPointerFieldsEndOffset,
3382 kSize> BodyDescriptor;
3383
Steve Blocka7e24c12009-10-30 11:49:00 +00003384 private:
3385 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3386};
3387
3388
3389// An abstract superclass, a marker class really, for simple structure classes.
3390// It doesn't carry much functionality but allows struct classes to me
3391// identified in the type system.
3392class Struct: public HeapObject {
3393 public:
3394 inline void InitializeBody(int object_size);
3395 static inline Struct* cast(Object* that);
3396};
3397
3398
3399// Script describes a script which has been added to the VM.
3400class Script: public Struct {
3401 public:
3402 // Script types.
3403 enum Type {
3404 TYPE_NATIVE = 0,
3405 TYPE_EXTENSION = 1,
3406 TYPE_NORMAL = 2
3407 };
3408
3409 // Script compilation types.
3410 enum CompilationType {
3411 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003412 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003413 };
3414
3415 // [source]: the script source.
3416 DECL_ACCESSORS(source, Object)
3417
3418 // [name]: the script name.
3419 DECL_ACCESSORS(name, Object)
3420
3421 // [id]: the script id.
3422 DECL_ACCESSORS(id, Object)
3423
3424 // [line_offset]: script line offset in resource from where it was extracted.
3425 DECL_ACCESSORS(line_offset, Smi)
3426
3427 // [column_offset]: script column offset in resource from where it was
3428 // extracted.
3429 DECL_ACCESSORS(column_offset, Smi)
3430
3431 // [data]: additional data associated with this script.
3432 DECL_ACCESSORS(data, Object)
3433
3434 // [context_data]: context data for the context this script was compiled in.
3435 DECL_ACCESSORS(context_data, Object)
3436
3437 // [wrapper]: the wrapper cache.
3438 DECL_ACCESSORS(wrapper, Proxy)
3439
3440 // [type]: the script type.
3441 DECL_ACCESSORS(type, Smi)
3442
3443 // [compilation]: how the the script was compiled.
3444 DECL_ACCESSORS(compilation_type, Smi)
3445
Steve Blockd0582a62009-12-15 09:54:21 +00003446 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003447 DECL_ACCESSORS(line_ends, Object)
3448
Steve Blockd0582a62009-12-15 09:54:21 +00003449 // [eval_from_shared]: for eval scripts the shared funcion info for the
3450 // function from which eval was called.
3451 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003452
3453 // [eval_from_instructions_offset]: the instruction offset in the code for the
3454 // function from which eval was called where eval was called.
3455 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3456
3457 static inline Script* cast(Object* obj);
3458
Steve Block3ce2e202009-11-05 08:53:23 +00003459 // If script source is an external string, check that the underlying
3460 // resource is accessible. Otherwise, always return true.
3461 inline bool HasValidSource();
3462
Steve Blocka7e24c12009-10-30 11:49:00 +00003463#ifdef DEBUG
3464 void ScriptPrint();
3465 void ScriptVerify();
3466#endif
3467
3468 static const int kSourceOffset = HeapObject::kHeaderSize;
3469 static const int kNameOffset = kSourceOffset + kPointerSize;
3470 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3471 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3472 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3473 static const int kContextOffset = kDataOffset + kPointerSize;
3474 static const int kWrapperOffset = kContextOffset + kPointerSize;
3475 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3476 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3477 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3478 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003479 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003480 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003481 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003482 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3483
3484 private:
3485 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3486};
3487
3488
3489// SharedFunctionInfo describes the JSFunction information that can be
3490// shared by multiple instances of the function.
3491class SharedFunctionInfo: public HeapObject {
3492 public:
3493 // [name]: Function name.
3494 DECL_ACCESSORS(name, Object)
3495
3496 // [code]: Function code.
3497 DECL_ACCESSORS(code, Code)
3498
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003499 // [scope_info]: Scope info.
3500 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
3501
Steve Blocka7e24c12009-10-30 11:49:00 +00003502 // [construct stub]: Code stub for constructing instances of this function.
3503 DECL_ACCESSORS(construct_stub, Code)
3504
Iain Merrick75681382010-08-19 15:07:18 +01003505 inline Code* unchecked_code();
3506
Steve Blocka7e24c12009-10-30 11:49:00 +00003507 // Returns if this function has been compiled to native code yet.
3508 inline bool is_compiled();
3509
3510 // [length]: The function length - usually the number of declared parameters.
3511 // Use up to 2^30 parameters.
3512 inline int length();
3513 inline void set_length(int value);
3514
3515 // [formal parameter count]: The declared number of parameters.
3516 inline int formal_parameter_count();
3517 inline void set_formal_parameter_count(int value);
3518
3519 // Set the formal parameter count so the function code will be
3520 // called without using argument adaptor frames.
3521 inline void DontAdaptArguments();
3522
3523 // [expected_nof_properties]: Expected number of properties for the function.
3524 inline int expected_nof_properties();
3525 inline void set_expected_nof_properties(int value);
3526
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003527 // Inobject slack tracking is the way to reclaim unused inobject space.
3528 //
3529 // The instance size is initially determined by adding some slack to
3530 // expected_nof_properties (to allow for a few extra properties added
3531 // after the constructor). There is no guarantee that the extra space
3532 // will not be wasted.
3533 //
3534 // Here is the algorithm to reclaim the unused inobject space:
3535 // - Detect the first constructor call for this SharedFunctionInfo.
3536 // When it happens enter the "in progress" state: remember the
3537 // constructor's initial_map and install a special construct stub that
3538 // counts constructor calls.
3539 // - While the tracking is in progress create objects filled with
3540 // one_pointer_filler_map instead of undefined_value. This way they can be
3541 // resized quickly and safely.
3542 // - Once enough (kGenerousAllocationCount) objects have been created
3543 // compute the 'slack' (traverse the map transition tree starting from the
3544 // initial_map and find the lowest value of unused_property_fields).
3545 // - Traverse the transition tree again and decrease the instance size
3546 // of every map. Existing objects will resize automatically (they are
3547 // filled with one_pointer_filler_map). All further allocations will
3548 // use the adjusted instance size.
3549 // - Decrease expected_nof_properties so that an allocations made from
3550 // another context will use the adjusted instance size too.
3551 // - Exit "in progress" state by clearing the reference to the initial_map
3552 // and setting the regular construct stub (generic or inline).
3553 //
3554 // The above is the main event sequence. Some special cases are possible
3555 // while the tracking is in progress:
3556 //
3557 // - GC occurs.
3558 // Check if the initial_map is referenced by any live objects (except this
3559 // SharedFunctionInfo). If it is, continue tracking as usual.
3560 // If it is not, clear the reference and reset the tracking state. The
3561 // tracking will be initiated again on the next constructor call.
3562 //
3563 // - The constructor is called from another context.
3564 // Immediately complete the tracking, perform all the necessary changes
3565 // to maps. This is necessary because there is no efficient way to track
3566 // multiple initial_maps.
3567 // Proceed to create an object in the current context (with the adjusted
3568 // size).
3569 //
3570 // - A different constructor function sharing the same SharedFunctionInfo is
3571 // called in the same context. This could be another closure in the same
3572 // context, or the first function could have been disposed.
3573 // This is handled the same way as the previous case.
3574 //
3575 // Important: inobject slack tracking is not attempted during the snapshot
3576 // creation.
3577
Ben Murdochf87a2032010-10-22 12:50:53 +01003578 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003579
3580 // [construction_count]: Counter for constructor calls made during
3581 // the tracking phase.
3582 inline int construction_count();
3583 inline void set_construction_count(int value);
3584
3585 // [initial_map]: initial map of the first function called as a constructor.
3586 // Saved for the duration of the tracking phase.
3587 // This is a weak link (GC resets it to undefined_value if no other live
3588 // object reference this map).
3589 DECL_ACCESSORS(initial_map, Object)
3590
3591 // True if the initial_map is not undefined and the countdown stub is
3592 // installed.
3593 inline bool IsInobjectSlackTrackingInProgress();
3594
3595 // Starts the tracking.
3596 // Stores the initial map and installs the countdown stub.
3597 // IsInobjectSlackTrackingInProgress is normally true after this call,
3598 // except when tracking have not been started (e.g. the map has no unused
3599 // properties or the snapshot is being built).
3600 void StartInobjectSlackTracking(Map* map);
3601
3602 // Completes the tracking.
3603 // IsInobjectSlackTrackingInProgress is false after this call.
3604 void CompleteInobjectSlackTracking();
3605
3606 // Clears the initial_map before the GC marking phase to ensure the reference
3607 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
3608 void DetachInitialMap();
3609
3610 // Restores the link to the initial map after the GC marking phase.
3611 // IsInobjectSlackTrackingInProgress is true after this call.
3612 void AttachInitialMap(Map* map);
3613
3614 // False if there are definitely no live objects created from this function.
3615 // True if live objects _may_ exist (existence not guaranteed).
3616 // May go back from true to false after GC.
3617 inline bool live_objects_may_exist();
3618
3619 inline void set_live_objects_may_exist(bool value);
3620
Steve Blocka7e24c12009-10-30 11:49:00 +00003621 // [instance class name]: class name for instances.
3622 DECL_ACCESSORS(instance_class_name, Object)
3623
Steve Block6ded16b2010-05-10 14:33:55 +01003624 // [function data]: This field holds some additional data for function.
3625 // Currently it either has FunctionTemplateInfo to make benefit the API
Kristian Monsen25f61362010-05-21 11:50:48 +01003626 // or Smi identifying a custom call generator.
Steve Blocka7e24c12009-10-30 11:49:00 +00003627 // In the long run we don't want all functions to have this field but
3628 // we can fix that when we have a better model for storing hidden data
3629 // on objects.
3630 DECL_ACCESSORS(function_data, Object)
3631
Steve Block6ded16b2010-05-10 14:33:55 +01003632 inline bool IsApiFunction();
3633 inline FunctionTemplateInfo* get_api_func_data();
3634 inline bool HasCustomCallGenerator();
Kristian Monsen25f61362010-05-21 11:50:48 +01003635 inline int custom_call_generator_id();
Steve Block6ded16b2010-05-10 14:33:55 +01003636
Steve Blocka7e24c12009-10-30 11:49:00 +00003637 // [script info]: Script from which the function originates.
3638 DECL_ACCESSORS(script, Object)
3639
Steve Block6ded16b2010-05-10 14:33:55 +01003640 // [num_literals]: Number of literals used by this function.
3641 inline int num_literals();
3642 inline void set_num_literals(int value);
3643
Steve Blocka7e24c12009-10-30 11:49:00 +00003644 // [start_position_and_type]: Field used to store both the source code
3645 // position, whether or not the function is a function expression,
3646 // and whether or not the function is a toplevel function. The two
3647 // least significants bit indicates whether the function is an
3648 // expression and the rest contains the source code position.
3649 inline int start_position_and_type();
3650 inline void set_start_position_and_type(int value);
3651
3652 // [debug info]: Debug information.
3653 DECL_ACCESSORS(debug_info, Object)
3654
3655 // [inferred name]: Name inferred from variable or property
3656 // assignment of this function. Used to facilitate debugging and
3657 // profiling of JavaScript code written in OO style, where almost
3658 // all functions are anonymous but are assigned to object
3659 // properties.
3660 DECL_ACCESSORS(inferred_name, String)
3661
Ben Murdochf87a2032010-10-22 12:50:53 +01003662 // The function's name if it is non-empty, otherwise the inferred name.
3663 String* DebugName();
3664
Steve Blocka7e24c12009-10-30 11:49:00 +00003665 // Position of the 'function' token in the script source.
3666 inline int function_token_position();
3667 inline void set_function_token_position(int function_token_position);
3668
3669 // Position of this function in the script source.
3670 inline int start_position();
3671 inline void set_start_position(int start_position);
3672
3673 // End position of this function in the script source.
3674 inline int end_position();
3675 inline void set_end_position(int end_position);
3676
3677 // Is this function a function expression in the source code.
3678 inline bool is_expression();
3679 inline void set_is_expression(bool value);
3680
3681 // Is this function a top-level function (scripts, evals).
3682 inline bool is_toplevel();
3683 inline void set_is_toplevel(bool value);
3684
3685 // Bit field containing various information collected by the compiler to
3686 // drive optimization.
3687 inline int compiler_hints();
3688 inline void set_compiler_hints(int value);
3689
3690 // Add information on assignments of the form this.x = ...;
3691 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00003692 bool has_only_simple_this_property_assignments,
3693 FixedArray* this_property_assignments);
3694
3695 // Clear information on assignments of the form this.x = ...;
3696 void ClearThisPropertyAssignmentsInfo();
3697
3698 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00003699 // this.x = y; where y is either a constant or refers to an argument.
3700 inline bool has_only_simple_this_property_assignments();
3701
Leon Clarked91b9f72010-01-27 17:25:45 +00003702 inline bool try_full_codegen();
3703 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00003704
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003705 // Indicates if this function can be lazy compiled.
3706 // This is used to determine if we can safely flush code from a function
3707 // when doing GC if we expect that the function will no longer be used.
3708 inline bool allows_lazy_compilation();
3709 inline void set_allows_lazy_compilation(bool flag);
3710
Iain Merrick75681382010-08-19 15:07:18 +01003711 // Indicates how many full GCs this function has survived with assigned
3712 // code object. Used to determine when it is relatively safe to flush
3713 // this code object and replace it with lazy compilation stub.
3714 // Age is reset when GC notices that the code object is referenced
3715 // from the stack or compilation cache.
3716 inline int code_age();
3717 inline void set_code_age(int age);
3718
3719
Andrei Popescu402d9372010-02-26 13:31:12 +00003720 // Check whether a inlined constructor can be generated with the given
3721 // prototype.
3722 bool CanGenerateInlineConstructor(Object* prototype);
3723
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003724 // Prevents further attempts to generate inline constructors.
3725 // To be called if generation failed for any reason.
3726 void ForbidInlineConstructor();
3727
Steve Blocka7e24c12009-10-30 11:49:00 +00003728 // For functions which only contains this property assignments this provides
3729 // access to the names for the properties assigned.
3730 DECL_ACCESSORS(this_property_assignments, Object)
3731 inline int this_property_assignments_count();
3732 inline void set_this_property_assignments_count(int value);
3733 String* GetThisPropertyAssignmentName(int index);
3734 bool IsThisPropertyAssignmentArgument(int index);
3735 int GetThisPropertyAssignmentArgument(int index);
3736 Object* GetThisPropertyAssignmentConstant(int index);
3737
3738 // [source code]: Source code for the function.
3739 bool HasSourceCode();
3740 Object* GetSourceCode();
3741
3742 // Calculate the instance size.
3743 int CalculateInstanceSize();
3744
3745 // Calculate the number of in-object properties.
3746 int CalculateInObjectProperties();
3747
3748 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00003749 // Set max_length to -1 for unlimited length.
3750 void SourceCodePrint(StringStream* accumulator, int max_length);
3751#ifdef DEBUG
3752 void SharedFunctionInfoPrint();
3753 void SharedFunctionInfoVerify();
3754#endif
3755
3756 // Casting.
3757 static inline SharedFunctionInfo* cast(Object* obj);
3758
3759 // Constants.
3760 static const int kDontAdaptArgumentsSentinel = -1;
3761
3762 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01003763 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00003764 static const int kNameOffset = HeapObject::kHeaderSize;
3765 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003766 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
3767 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003768 static const int kInstanceClassNameOffset =
3769 kConstructStubOffset + kPointerSize;
3770 static const int kFunctionDataOffset =
3771 kInstanceClassNameOffset + kPointerSize;
3772 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
3773 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
3774 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003775 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003776 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003777 static const int kThisPropertyAssignmentsOffset =
3778 kInitialMapOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003779#if V8_HOST_ARCH_32_BIT
3780 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01003781 static const int kLengthOffset =
3782 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003783 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
3784 static const int kExpectedNofPropertiesOffset =
3785 kFormalParameterCountOffset + kPointerSize;
3786 static const int kNumLiteralsOffset =
3787 kExpectedNofPropertiesOffset + kPointerSize;
3788 static const int kStartPositionAndTypeOffset =
3789 kNumLiteralsOffset + kPointerSize;
3790 static const int kEndPositionOffset =
3791 kStartPositionAndTypeOffset + kPointerSize;
3792 static const int kFunctionTokenPositionOffset =
3793 kEndPositionOffset + kPointerSize;
3794 static const int kCompilerHintsOffset =
3795 kFunctionTokenPositionOffset + kPointerSize;
3796 static const int kThisPropertyAssignmentsCountOffset =
3797 kCompilerHintsOffset + kPointerSize;
3798 // Total size.
3799 static const int kSize = kThisPropertyAssignmentsCountOffset + kPointerSize;
3800#else
3801 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003802 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003803 // garbage collections.
3804 // To avoid wasting space on 64-bit architectures we use
3805 // the following trick: we group integer fields into pairs
3806 // First integer in each pair is shifted left by 1.
3807 // By doing this we guarantee that LSB of each kPointerSize aligned
3808 // word is not set and thus this word cannot be treated as pointer
3809 // to HeapObject during old space traversal.
3810 static const int kLengthOffset =
3811 kThisPropertyAssignmentsOffset + kPointerSize;
3812 static const int kFormalParameterCountOffset =
3813 kLengthOffset + kIntSize;
3814
Steve Blocka7e24c12009-10-30 11:49:00 +00003815 static const int kExpectedNofPropertiesOffset =
3816 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003817 static const int kNumLiteralsOffset =
3818 kExpectedNofPropertiesOffset + kIntSize;
3819
3820 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003821 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003822 static const int kStartPositionAndTypeOffset =
3823 kEndPositionOffset + kIntSize;
3824
3825 static const int kFunctionTokenPositionOffset =
3826 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003827 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00003828 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003829
Steve Blocka7e24c12009-10-30 11:49:00 +00003830 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003831 kCompilerHintsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003832
Steve Block6ded16b2010-05-10 14:33:55 +01003833 // Total size.
3834 static const int kSize = kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003835
3836#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003837
3838 // The construction counter for inobject slack tracking is stored in the
3839 // most significant byte of compiler_hints which is otherwise unused.
3840 // Its offset depends on the endian-ness of the architecture.
3841#if __BYTE_ORDER == __LITTLE_ENDIAN
3842 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
3843#elif __BYTE_ORDER == __BIG_ENDIAN
3844 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
3845#else
3846#error Unknown byte ordering
3847#endif
3848
Steve Block6ded16b2010-05-10 14:33:55 +01003849 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003850
Iain Merrick75681382010-08-19 15:07:18 +01003851 typedef FixedBodyDescriptor<kNameOffset,
3852 kThisPropertyAssignmentsOffset + kPointerSize,
3853 kSize> BodyDescriptor;
3854
Steve Blocka7e24c12009-10-30 11:49:00 +00003855 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00003856 // Bit positions in start_position_and_type.
3857 // The source code start position is in the 30 most significant bits of
3858 // the start_position_and_type field.
3859 static const int kIsExpressionBit = 0;
3860 static const int kIsTopLevelBit = 1;
3861 static const int kStartPositionShift = 2;
3862 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
3863
3864 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00003865 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00003866 static const int kTryFullCodegen = 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003867 static const int kAllowLazyCompilation = 2;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003868 static const int kLiveObjectsMayExist = 3;
3869 static const int kCodeAgeShift = 4;
Iain Merrick75681382010-08-19 15:07:18 +01003870 static const int kCodeAgeMask = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +00003871
3872 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
3873};
3874
3875
3876// JSFunction describes JavaScript functions.
3877class JSFunction: public JSObject {
3878 public:
3879 // [prototype_or_initial_map]:
3880 DECL_ACCESSORS(prototype_or_initial_map, Object)
3881
3882 // [shared_function_info]: The information about the function that
3883 // can be shared by instances.
3884 DECL_ACCESSORS(shared, SharedFunctionInfo)
3885
Iain Merrick75681382010-08-19 15:07:18 +01003886 inline SharedFunctionInfo* unchecked_shared();
3887
Steve Blocka7e24c12009-10-30 11:49:00 +00003888 // [context]: The context for this function.
3889 inline Context* context();
3890 inline Object* unchecked_context();
3891 inline void set_context(Object* context);
3892
3893 // [code]: The generated code object for this function. Executed
3894 // when the function is invoked, e.g. foo() or new foo(). See
3895 // [[Call]] and [[Construct]] description in ECMA-262, section
3896 // 8.6.2, page 27.
3897 inline Code* code();
3898 inline void set_code(Code* value);
3899
Iain Merrick75681382010-08-19 15:07:18 +01003900 inline Code* unchecked_code();
3901
Steve Blocka7e24c12009-10-30 11:49:00 +00003902 // Tells whether this function is builtin.
3903 inline bool IsBuiltin();
3904
3905 // [literals]: Fixed array holding the materialized literals.
3906 //
3907 // If the function contains object, regexp or array literals, the
3908 // literals array prefix contains the object, regexp, and array
3909 // function to be used when creating these literals. This is
3910 // necessary so that we do not dynamically lookup the object, regexp
3911 // or array functions. Performing a dynamic lookup, we might end up
3912 // using the functions from a new context that we should not have
3913 // access to.
3914 DECL_ACCESSORS(literals, FixedArray)
3915
3916 // The initial map for an object created by this constructor.
3917 inline Map* initial_map();
3918 inline void set_initial_map(Map* value);
3919 inline bool has_initial_map();
3920
3921 // Get and set the prototype property on a JSFunction. If the
3922 // function has an initial map the prototype is set on the initial
3923 // map. Otherwise, the prototype is put in the initial map field
3924 // until an initial map is needed.
3925 inline bool has_prototype();
3926 inline bool has_instance_prototype();
3927 inline Object* prototype();
3928 inline Object* instance_prototype();
3929 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07003930 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003931
Steve Block6ded16b2010-05-10 14:33:55 +01003932 // After prototype is removed, it will not be created when accessed, and
3933 // [[Construct]] from this function will not be allowed.
3934 Object* RemovePrototype();
3935 inline bool should_have_prototype();
3936
Steve Blocka7e24c12009-10-30 11:49:00 +00003937 // Accessor for this function's initial map's [[class]]
3938 // property. This is primarily used by ECMA native functions. This
3939 // method sets the class_name field of this function's initial map
3940 // to a given value. It creates an initial map if this function does
3941 // not have one. Note that this method does not copy the initial map
3942 // if it has one already, but simply replaces it with the new value.
3943 // Instances created afterwards will have a map whose [[class]] is
3944 // set to 'value', but there is no guarantees on instances created
3945 // before.
3946 Object* SetInstanceClassName(String* name);
3947
3948 // Returns if this function has been compiled to native code yet.
3949 inline bool is_compiled();
3950
3951 // Casting.
3952 static inline JSFunction* cast(Object* obj);
3953
Steve Block791712a2010-08-27 10:21:07 +01003954 // Iterates the objects, including code objects indirectly referenced
3955 // through pointers to the first instruction in the code object.
3956 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
3957
Steve Blocka7e24c12009-10-30 11:49:00 +00003958 // Dispatched behavior.
3959#ifdef DEBUG
3960 void JSFunctionPrint();
3961 void JSFunctionVerify();
3962#endif
3963
3964 // Returns the number of allocated literals.
3965 inline int NumberOfLiterals();
3966
3967 // Retrieve the global context from a function's literal array.
3968 static Context* GlobalContextFromLiterals(FixedArray* literals);
3969
3970 // Layout descriptors.
Steve Block791712a2010-08-27 10:21:07 +01003971 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01003972 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01003973 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003974 static const int kSharedFunctionInfoOffset =
3975 kPrototypeOrInitialMapOffset + kPointerSize;
3976 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
3977 static const int kLiteralsOffset = kContextOffset + kPointerSize;
3978 static const int kSize = kLiteralsOffset + kPointerSize;
3979
3980 // Layout of the literals array.
3981 static const int kLiteralsPrefixSize = 1;
3982 static const int kLiteralGlobalContextIndex = 0;
3983 private:
3984 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
3985};
3986
3987
3988// JSGlobalProxy's prototype must be a JSGlobalObject or null,
3989// and the prototype is hidden. JSGlobalProxy always delegates
3990// property accesses to its prototype if the prototype is not null.
3991//
3992// A JSGlobalProxy can be reinitialized which will preserve its identity.
3993//
3994// Accessing a JSGlobalProxy requires security check.
3995
3996class JSGlobalProxy : public JSObject {
3997 public:
3998 // [context]: the owner global context of this proxy object.
3999 // It is null value if this object is not used by any context.
4000 DECL_ACCESSORS(context, Object)
4001
4002 // Casting.
4003 static inline JSGlobalProxy* cast(Object* obj);
4004
4005 // Dispatched behavior.
4006#ifdef DEBUG
4007 void JSGlobalProxyPrint();
4008 void JSGlobalProxyVerify();
4009#endif
4010
4011 // Layout description.
4012 static const int kContextOffset = JSObject::kHeaderSize;
4013 static const int kSize = kContextOffset + kPointerSize;
4014
4015 private:
4016
4017 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
4018};
4019
4020
4021// Forward declaration.
4022class JSBuiltinsObject;
4023
4024// Common super class for JavaScript global objects and the special
4025// builtins global objects.
4026class GlobalObject: public JSObject {
4027 public:
4028 // [builtins]: the object holding the runtime routines written in JS.
4029 DECL_ACCESSORS(builtins, JSBuiltinsObject)
4030
4031 // [global context]: the global context corresponding to this global object.
4032 DECL_ACCESSORS(global_context, Context)
4033
4034 // [global receiver]: the global receiver object of the context
4035 DECL_ACCESSORS(global_receiver, JSObject)
4036
4037 // Retrieve the property cell used to store a property.
4038 Object* GetPropertyCell(LookupResult* result);
4039
John Reck59135872010-11-02 12:39:01 -07004040 // This is like GetProperty, but is used when you know the lookup won't fail
4041 // by throwing an exception. This is for the debug and builtins global
4042 // objects, where it is known which properties can be expected to be present
4043 // on the object.
4044 Object* GetPropertyNoExceptionThrown(String* key) {
4045 Object* answer = GetProperty(key)->ToObjectUnchecked();
4046 return answer;
4047 }
4048
Steve Blocka7e24c12009-10-30 11:49:00 +00004049 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07004050 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00004051
4052 // Casting.
4053 static inline GlobalObject* cast(Object* obj);
4054
4055 // Layout description.
4056 static const int kBuiltinsOffset = JSObject::kHeaderSize;
4057 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
4058 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
4059 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
4060
4061 private:
4062 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4063
4064 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
4065};
4066
4067
4068// JavaScript global object.
4069class JSGlobalObject: public GlobalObject {
4070 public:
4071
4072 // Casting.
4073 static inline JSGlobalObject* cast(Object* obj);
4074
4075 // Dispatched behavior.
4076#ifdef DEBUG
4077 void JSGlobalObjectPrint();
4078 void JSGlobalObjectVerify();
4079#endif
4080
4081 // Layout description.
4082 static const int kSize = GlobalObject::kHeaderSize;
4083
4084 private:
4085 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
4086};
4087
4088
4089// Builtins global object which holds the runtime routines written in
4090// JavaScript.
4091class JSBuiltinsObject: public GlobalObject {
4092 public:
4093 // Accessors for the runtime routines written in JavaScript.
4094 inline Object* javascript_builtin(Builtins::JavaScript id);
4095 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
4096
Steve Block6ded16b2010-05-10 14:33:55 +01004097 // Accessors for code of the runtime routines written in JavaScript.
4098 inline Code* javascript_builtin_code(Builtins::JavaScript id);
4099 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
4100
Steve Blocka7e24c12009-10-30 11:49:00 +00004101 // Casting.
4102 static inline JSBuiltinsObject* cast(Object* obj);
4103
4104 // Dispatched behavior.
4105#ifdef DEBUG
4106 void JSBuiltinsObjectPrint();
4107 void JSBuiltinsObjectVerify();
4108#endif
4109
4110 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01004111 // room for two pointers per runtime routine written in javascript
4112 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00004113 static const int kJSBuiltinsCount = Builtins::id_count;
4114 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004115 static const int kJSBuiltinsCodeOffset =
4116 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004117 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01004118 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
4119
4120 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
4121 return kJSBuiltinsOffset + id * kPointerSize;
4122 }
4123
4124 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
4125 return kJSBuiltinsCodeOffset + id * kPointerSize;
4126 }
4127
Steve Blocka7e24c12009-10-30 11:49:00 +00004128 private:
4129 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
4130};
4131
4132
4133// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
4134class JSValue: public JSObject {
4135 public:
4136 // [value]: the object being wrapped.
4137 DECL_ACCESSORS(value, Object)
4138
4139 // Casting.
4140 static inline JSValue* cast(Object* obj);
4141
4142 // Dispatched behavior.
4143#ifdef DEBUG
4144 void JSValuePrint();
4145 void JSValueVerify();
4146#endif
4147
4148 // Layout description.
4149 static const int kValueOffset = JSObject::kHeaderSize;
4150 static const int kSize = kValueOffset + kPointerSize;
4151
4152 private:
4153 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
4154};
4155
4156// Regular expressions
4157// The regular expression holds a single reference to a FixedArray in
4158// the kDataOffset field.
4159// The FixedArray contains the following data:
4160// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
4161// - reference to the original source string
4162// - reference to the original flag string
4163// If it is an atom regexp
4164// - a reference to a literal string to search for
4165// If it is an irregexp regexp:
4166// - a reference to code for ASCII inputs (bytecode or compiled).
4167// - a reference to code for UC16 inputs (bytecode or compiled).
4168// - max number of registers used by irregexp implementations.
4169// - number of capture registers (output values) of the regexp.
4170class JSRegExp: public JSObject {
4171 public:
4172 // Meaning of Type:
4173 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
4174 // ATOM: A simple string to match against using an indexOf operation.
4175 // IRREGEXP: Compiled with Irregexp.
4176 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
4177 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
4178 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
4179
4180 class Flags {
4181 public:
4182 explicit Flags(uint32_t value) : value_(value) { }
4183 bool is_global() { return (value_ & GLOBAL) != 0; }
4184 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
4185 bool is_multiline() { return (value_ & MULTILINE) != 0; }
4186 uint32_t value() { return value_; }
4187 private:
4188 uint32_t value_;
4189 };
4190
4191 DECL_ACCESSORS(data, Object)
4192
4193 inline Type TypeTag();
4194 inline int CaptureCount();
4195 inline Flags GetFlags();
4196 inline String* Pattern();
4197 inline Object* DataAt(int index);
4198 // Set implementation data after the object has been prepared.
4199 inline void SetDataAt(int index, Object* value);
4200 static int code_index(bool is_ascii) {
4201 if (is_ascii) {
4202 return kIrregexpASCIICodeIndex;
4203 } else {
4204 return kIrregexpUC16CodeIndex;
4205 }
4206 }
4207
4208 static inline JSRegExp* cast(Object* obj);
4209
4210 // Dispatched behavior.
4211#ifdef DEBUG
4212 void JSRegExpVerify();
4213#endif
4214
4215 static const int kDataOffset = JSObject::kHeaderSize;
4216 static const int kSize = kDataOffset + kPointerSize;
4217
4218 // Indices in the data array.
4219 static const int kTagIndex = 0;
4220 static const int kSourceIndex = kTagIndex + 1;
4221 static const int kFlagsIndex = kSourceIndex + 1;
4222 static const int kDataIndex = kFlagsIndex + 1;
4223 // The data fields are used in different ways depending on the
4224 // value of the tag.
4225 // Atom regexps (literal strings).
4226 static const int kAtomPatternIndex = kDataIndex;
4227
4228 static const int kAtomDataSize = kAtomPatternIndex + 1;
4229
4230 // Irregexp compiled code or bytecode for ASCII. If compilation
4231 // fails, this fields hold an exception object that should be
4232 // thrown if the regexp is used again.
4233 static const int kIrregexpASCIICodeIndex = kDataIndex;
4234 // Irregexp compiled code or bytecode for UC16. If compilation
4235 // fails, this fields hold an exception object that should be
4236 // thrown if the regexp is used again.
4237 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
4238 // Maximal number of registers used by either ASCII or UC16.
4239 // Only used to check that there is enough stack space
4240 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
4241 // Number of captures in the compiled regexp.
4242 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
4243
4244 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00004245
4246 // Offsets directly into the data fixed array.
4247 static const int kDataTagOffset =
4248 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
4249 static const int kDataAsciiCodeOffset =
4250 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00004251 static const int kDataUC16CodeOffset =
4252 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00004253 static const int kIrregexpCaptureCountOffset =
4254 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004255
4256 // In-object fields.
4257 static const int kSourceFieldIndex = 0;
4258 static const int kGlobalFieldIndex = 1;
4259 static const int kIgnoreCaseFieldIndex = 2;
4260 static const int kMultilineFieldIndex = 3;
4261 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01004262 static const int kInObjectFieldCount = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00004263};
4264
4265
4266class CompilationCacheShape {
4267 public:
4268 static inline bool IsMatch(HashTableKey* key, Object* value) {
4269 return key->IsMatch(value);
4270 }
4271
4272 static inline uint32_t Hash(HashTableKey* key) {
4273 return key->Hash();
4274 }
4275
4276 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4277 return key->HashForObject(object);
4278 }
4279
John Reck59135872010-11-02 12:39:01 -07004280 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004281 return key->AsObject();
4282 }
4283
4284 static const int kPrefixSize = 0;
4285 static const int kEntrySize = 2;
4286};
4287
Steve Block3ce2e202009-11-05 08:53:23 +00004288
Steve Blocka7e24c12009-10-30 11:49:00 +00004289class CompilationCacheTable: public HashTable<CompilationCacheShape,
4290 HashTableKey*> {
4291 public:
4292 // Find cached value for a string key, otherwise return null.
4293 Object* Lookup(String* src);
4294 Object* LookupEval(String* src, Context* context);
4295 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07004296 MaybeObject* Put(String* src, Object* value);
4297 MaybeObject* PutEval(String* src, Context* context, Object* value);
4298 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004299
4300 static inline CompilationCacheTable* cast(Object* obj);
4301
4302 private:
4303 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
4304};
4305
4306
Steve Block6ded16b2010-05-10 14:33:55 +01004307class CodeCache: public Struct {
4308 public:
4309 DECL_ACCESSORS(default_cache, FixedArray)
4310 DECL_ACCESSORS(normal_type_cache, Object)
4311
4312 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07004313 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004314
4315 // Lookup code object in the cache. Returns code object if found and undefined
4316 // if not.
4317 Object* Lookup(String* name, Code::Flags flags);
4318
4319 // Get the internal index of a code object in the cache. Returns -1 if the
4320 // code object is not in that cache. This index can be used to later call
4321 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
4322 // RemoveByIndex.
4323 int GetIndex(Object* name, Code* code);
4324
4325 // Remove an object from the cache with the provided internal index.
4326 void RemoveByIndex(Object* name, Code* code, int index);
4327
4328 static inline CodeCache* cast(Object* obj);
4329
4330#ifdef DEBUG
4331 void CodeCachePrint();
4332 void CodeCacheVerify();
4333#endif
4334
4335 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
4336 static const int kNormalTypeCacheOffset =
4337 kDefaultCacheOffset + kPointerSize;
4338 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
4339
4340 private:
John Reck59135872010-11-02 12:39:01 -07004341 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
4342 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004343 Object* LookupDefaultCache(String* name, Code::Flags flags);
4344 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
4345
4346 // Code cache layout of the default cache. Elements are alternating name and
4347 // code objects for non normal load/store/call IC's.
4348 static const int kCodeCacheEntrySize = 2;
4349 static const int kCodeCacheEntryNameOffset = 0;
4350 static const int kCodeCacheEntryCodeOffset = 1;
4351
4352 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
4353};
4354
4355
4356class CodeCacheHashTableShape {
4357 public:
4358 static inline bool IsMatch(HashTableKey* key, Object* value) {
4359 return key->IsMatch(value);
4360 }
4361
4362 static inline uint32_t Hash(HashTableKey* key) {
4363 return key->Hash();
4364 }
4365
4366 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4367 return key->HashForObject(object);
4368 }
4369
John Reck59135872010-11-02 12:39:01 -07004370 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01004371 return key->AsObject();
4372 }
4373
4374 static const int kPrefixSize = 0;
4375 static const int kEntrySize = 2;
4376};
4377
4378
4379class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
4380 HashTableKey*> {
4381 public:
4382 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07004383 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004384
4385 int GetIndex(String* name, Code::Flags flags);
4386 void RemoveByIndex(int index);
4387
4388 static inline CodeCacheHashTable* cast(Object* obj);
4389
4390 // Initial size of the fixed array backing the hash table.
4391 static const int kInitialSize = 64;
4392
4393 private:
4394 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
4395};
4396
4397
Steve Blocka7e24c12009-10-30 11:49:00 +00004398enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
4399enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
4400
4401
4402class StringHasher {
4403 public:
4404 inline StringHasher(int length);
4405
4406 // Returns true if the hash of this string can be computed without
4407 // looking at the contents.
4408 inline bool has_trivial_hash();
4409
4410 // Add a character to the hash and update the array index calculation.
4411 inline void AddCharacter(uc32 c);
4412
4413 // Adds a character to the hash but does not update the array index
4414 // calculation. This can only be called when it has been verified
4415 // that the input is not an array index.
4416 inline void AddCharacterNoIndex(uc32 c);
4417
4418 // Returns the value to store in the hash field of a string with
4419 // the given length and contents.
4420 uint32_t GetHashField();
4421
4422 // Returns true if the characters seen so far make up a legal array
4423 // index.
4424 bool is_array_index() { return is_array_index_; }
4425
4426 bool is_valid() { return is_valid_; }
4427
4428 void invalidate() { is_valid_ = false; }
4429
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004430 // Calculated hash value for a string consisting of 1 to
4431 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
4432 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01004433 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004434
Steve Blocka7e24c12009-10-30 11:49:00 +00004435 private:
4436
4437 uint32_t array_index() {
4438 ASSERT(is_array_index());
4439 return array_index_;
4440 }
4441
4442 inline uint32_t GetHash();
4443
4444 int length_;
4445 uint32_t raw_running_hash_;
4446 uint32_t array_index_;
4447 bool is_array_index_;
4448 bool is_first_char_;
4449 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00004450 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00004451};
4452
4453
4454// The characteristics of a string are stored in its map. Retrieving these
4455// few bits of information is moderately expensive, involving two memory
4456// loads where the second is dependent on the first. To improve efficiency
4457// the shape of the string is given its own class so that it can be retrieved
4458// once and used for several string operations. A StringShape is small enough
4459// to be passed by value and is immutable, but be aware that flattening a
4460// string can potentially alter its shape. Also be aware that a GC caused by
4461// something else can alter the shape of a string due to ConsString
4462// shortcutting. Keeping these restrictions in mind has proven to be error-
4463// prone and so we no longer put StringShapes in variables unless there is a
4464// concrete performance benefit at that particular point in the code.
4465class StringShape BASE_EMBEDDED {
4466 public:
4467 inline explicit StringShape(String* s);
4468 inline explicit StringShape(Map* s);
4469 inline explicit StringShape(InstanceType t);
4470 inline bool IsSequential();
4471 inline bool IsExternal();
4472 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00004473 inline bool IsExternalAscii();
4474 inline bool IsExternalTwoByte();
4475 inline bool IsSequentialAscii();
4476 inline bool IsSequentialTwoByte();
4477 inline bool IsSymbol();
4478 inline StringRepresentationTag representation_tag();
4479 inline uint32_t full_representation_tag();
4480 inline uint32_t size_tag();
4481#ifdef DEBUG
4482 inline uint32_t type() { return type_; }
4483 inline void invalidate() { valid_ = false; }
4484 inline bool valid() { return valid_; }
4485#else
4486 inline void invalidate() { }
4487#endif
4488 private:
4489 uint32_t type_;
4490#ifdef DEBUG
4491 inline void set_valid() { valid_ = true; }
4492 bool valid_;
4493#else
4494 inline void set_valid() { }
4495#endif
4496};
4497
4498
4499// The String abstract class captures JavaScript string values:
4500//
4501// Ecma-262:
4502// 4.3.16 String Value
4503// A string value is a member of the type String and is a finite
4504// ordered sequence of zero or more 16-bit unsigned integer values.
4505//
4506// All string values have a length field.
4507class String: public HeapObject {
4508 public:
4509 // Get and set the length of the string.
4510 inline int length();
4511 inline void set_length(int value);
4512
Steve Blockd0582a62009-12-15 09:54:21 +00004513 // Get and set the hash field of the string.
4514 inline uint32_t hash_field();
4515 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004516
4517 inline bool IsAsciiRepresentation();
4518 inline bool IsTwoByteRepresentation();
4519
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004520 // Returns whether this string has ascii chars, i.e. all of them can
4521 // be ascii encoded. This might be the case even if the string is
4522 // two-byte. Such strings may appear when the embedder prefers
4523 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01004524 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004525 // NOTE: this should be considered only a hint. False negatives are
4526 // possible.
4527 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01004528
Steve Blocka7e24c12009-10-30 11:49:00 +00004529 // Get and set individual two byte chars in the string.
4530 inline void Set(int index, uint16_t value);
4531 // Get individual two byte char in the string. Repeated calls
4532 // to this method are not efficient unless the string is flat.
4533 inline uint16_t Get(int index);
4534
Leon Clarkef7060e22010-06-03 12:02:55 +01004535 // Try to flatten the string. Checks first inline to see if it is
4536 // necessary. Does nothing if the string is not a cons string.
4537 // Flattening allocates a sequential string with the same data as
4538 // the given string and mutates the cons string to a degenerate
4539 // form, where the first component is the new sequential string and
4540 // the second component is the empty string. If allocation fails,
4541 // this function returns a failure. If flattening succeeds, this
4542 // function returns the sequential string that is now the first
4543 // component of the cons string.
4544 //
4545 // Degenerate cons strings are handled specially by the garbage
4546 // collector (see IsShortcutCandidate).
4547 //
4548 // Use FlattenString from Handles.cc to flatten even in case an
4549 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07004550 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004551
Leon Clarkef7060e22010-06-03 12:02:55 +01004552 // Convenience function. Has exactly the same behavior as
4553 // TryFlatten(), except in the case of failure returns the original
4554 // string.
4555 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
4556
Steve Blocka7e24c12009-10-30 11:49:00 +00004557 Vector<const char> ToAsciiVector();
4558 Vector<const uc16> ToUC16Vector();
4559
4560 // Mark the string as an undetectable object. It only applies to
4561 // ascii and two byte string types.
4562 bool MarkAsUndetectable();
4563
Steve Blockd0582a62009-12-15 09:54:21 +00004564 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07004565 MUST_USE_RESULT MaybeObject* SubString(int from,
4566 int to,
4567 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004568
4569 // String equality operations.
4570 inline bool Equals(String* other);
4571 bool IsEqualTo(Vector<const char> str);
4572
4573 // Return a UTF8 representation of the string. The string is null
4574 // terminated but may optionally contain nulls. Length is returned
4575 // in length_output if length_output is not a null pointer The string
4576 // should be nearly flat, otherwise the performance of this method may
4577 // be very slow (quadratic in the length). Setting robustness_flag to
4578 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4579 // handles unexpected data without causing assert failures and it does not
4580 // do any heap allocations. This is useful when printing stack traces.
4581 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
4582 RobustnessFlag robustness_flag,
4583 int offset,
4584 int length,
4585 int* length_output = 0);
4586 SmartPointer<char> ToCString(
4587 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
4588 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
4589 int* length_output = 0);
4590
4591 int Utf8Length();
4592
4593 // Return a 16 bit Unicode representation of the string.
4594 // The string should be nearly flat, otherwise the performance of
4595 // of this method may be very bad. Setting robustness_flag to
4596 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4597 // handles unexpected data without causing assert failures and it does not
4598 // do any heap allocations. This is useful when printing stack traces.
4599 SmartPointer<uc16> ToWideCString(
4600 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
4601
4602 // Tells whether the hash code has been computed.
4603 inline bool HasHashCode();
4604
4605 // Returns a hash value used for the property table
4606 inline uint32_t Hash();
4607
Steve Blockd0582a62009-12-15 09:54:21 +00004608 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
4609 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00004610
4611 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
4612 uint32_t* index,
4613 int length);
4614
4615 // Externalization.
4616 bool MakeExternal(v8::String::ExternalStringResource* resource);
4617 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
4618
4619 // Conversion.
4620 inline bool AsArrayIndex(uint32_t* index);
4621
4622 // Casting.
4623 static inline String* cast(Object* obj);
4624
4625 void PrintOn(FILE* out);
4626
4627 // For use during stack traces. Performs rudimentary sanity check.
4628 bool LooksValid();
4629
4630 // Dispatched behavior.
4631 void StringShortPrint(StringStream* accumulator);
4632#ifdef DEBUG
4633 void StringPrint();
4634 void StringVerify();
4635#endif
4636 inline bool IsFlat();
4637
4638 // Layout description.
4639 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004640 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004641 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004642
Steve Blockd0582a62009-12-15 09:54:21 +00004643 // Maximum number of characters to consider when trying to convert a string
4644 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00004645 static const int kMaxArrayIndexSize = 10;
4646
4647 // Max ascii char code.
4648 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
4649 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
4650 static const int kMaxUC16CharCode = 0xffff;
4651
Steve Blockd0582a62009-12-15 09:54:21 +00004652 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00004653 static const int kMinNonFlatLength = 13;
4654
4655 // Mask constant for checking if a string has a computed hash code
4656 // and if it is an array index. The least significant bit indicates
4657 // whether a hash code has been computed. If the hash code has been
4658 // computed the 2nd bit tells whether the string can be used as an
4659 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004660 static const int kHashNotComputedMask = 1;
4661 static const int kIsNotArrayIndexMask = 1 << 1;
4662 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00004663
Steve Blockd0582a62009-12-15 09:54:21 +00004664 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004665 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00004666
Steve Blocka7e24c12009-10-30 11:49:00 +00004667 // Array index strings this short can keep their index in the hash
4668 // field.
4669 static const int kMaxCachedArrayIndexLength = 7;
4670
Steve Blockd0582a62009-12-15 09:54:21 +00004671 // For strings which are array indexes the hash value has the string length
4672 // mixed into the hash, mainly to avoid a hash value of zero which would be
4673 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004674 static const int kArrayIndexValueBits = 24;
4675 static const int kArrayIndexLengthBits =
4676 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
4677
4678 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01004679 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004680
4681 static const int kArrayIndexHashLengthShift =
4682 kArrayIndexValueBits + kNofHashBitFields;
4683
Steve Blockd0582a62009-12-15 09:54:21 +00004684 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004685
4686 static const int kArrayIndexValueMask =
4687 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
4688
4689 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
4690 // could use a mask to test if the length of string is less than or equal to
4691 // kMaxCachedArrayIndexLength.
4692 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
4693
4694 static const int kContainsCachedArrayIndexMask =
4695 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
4696 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004697
4698 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004699 static const int kEmptyHashField =
4700 kIsNotArrayIndexMask | kHashNotComputedMask;
4701
4702 // Value of hash field containing computed hash equal to zero.
4703 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004704
4705 // Maximal string length.
4706 static const int kMaxLength = (1 << (32 - 2)) - 1;
4707
4708 // Max length for computing hash. For strings longer than this limit the
4709 // string length is used as the hash value.
4710 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00004711
4712 // Limit for truncation in short printing.
4713 static const int kMaxShortPrintLength = 1024;
4714
4715 // Support for regular expressions.
4716 const uc16* GetTwoByteData();
4717 const uc16* GetTwoByteData(unsigned start);
4718
4719 // Support for StringInputBuffer
4720 static const unibrow::byte* ReadBlock(String* input,
4721 unibrow::byte* util_buffer,
4722 unsigned capacity,
4723 unsigned* remaining,
4724 unsigned* offset);
4725 static const unibrow::byte* ReadBlock(String** input,
4726 unibrow::byte* util_buffer,
4727 unsigned capacity,
4728 unsigned* remaining,
4729 unsigned* offset);
4730
4731 // Helper function for flattening strings.
4732 template <typename sinkchar>
4733 static void WriteToFlat(String* source,
4734 sinkchar* sink,
4735 int from,
4736 int to);
4737
4738 protected:
4739 class ReadBlockBuffer {
4740 public:
4741 ReadBlockBuffer(unibrow::byte* util_buffer_,
4742 unsigned cursor_,
4743 unsigned capacity_,
4744 unsigned remaining_) :
4745 util_buffer(util_buffer_),
4746 cursor(cursor_),
4747 capacity(capacity_),
4748 remaining(remaining_) {
4749 }
4750 unibrow::byte* util_buffer;
4751 unsigned cursor;
4752 unsigned capacity;
4753 unsigned remaining;
4754 };
4755
Steve Blocka7e24c12009-10-30 11:49:00 +00004756 static inline const unibrow::byte* ReadBlock(String* input,
4757 ReadBlockBuffer* buffer,
4758 unsigned* offset,
4759 unsigned max_chars);
4760 static void ReadBlockIntoBuffer(String* input,
4761 ReadBlockBuffer* buffer,
4762 unsigned* offset_ptr,
4763 unsigned max_chars);
4764
4765 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01004766 // Try to flatten the top level ConsString that is hiding behind this
4767 // string. This is a no-op unless the string is a ConsString. Flatten
4768 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07004769 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01004770
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004771 static inline bool IsHashFieldComputed(uint32_t field);
4772
Steve Blocka7e24c12009-10-30 11:49:00 +00004773 // Slow case of String::Equals. This implementation works on any strings
4774 // but it is most efficient on strings that are almost flat.
4775 bool SlowEquals(String* other);
4776
4777 // Slow case of AsArrayIndex.
4778 bool SlowAsArrayIndex(uint32_t* index);
4779
4780 // Compute and set the hash code.
4781 uint32_t ComputeAndSetHash();
4782
4783 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
4784};
4785
4786
4787// The SeqString abstract class captures sequential string values.
4788class SeqString: public String {
4789 public:
4790
4791 // Casting.
4792 static inline SeqString* cast(Object* obj);
4793
Steve Blocka7e24c12009-10-30 11:49:00 +00004794 private:
4795 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
4796};
4797
4798
4799// The AsciiString class captures sequential ascii string objects.
4800// Each character in the AsciiString is an ascii character.
4801class SeqAsciiString: public SeqString {
4802 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004803 static const bool kHasAsciiEncoding = true;
4804
Steve Blocka7e24c12009-10-30 11:49:00 +00004805 // Dispatched behavior.
4806 inline uint16_t SeqAsciiStringGet(int index);
4807 inline void SeqAsciiStringSet(int index, uint16_t value);
4808
4809 // Get the address of the characters in this string.
4810 inline Address GetCharsAddress();
4811
4812 inline char* GetChars();
4813
4814 // Casting
4815 static inline SeqAsciiString* cast(Object* obj);
4816
4817 // Garbage collection support. This method is called by the
4818 // garbage collector to compute the actual size of an AsciiString
4819 // instance.
4820 inline int SeqAsciiStringSize(InstanceType instance_type);
4821
4822 // Computes the size for an AsciiString instance of a given length.
4823 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004824 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004825 }
4826
4827 // Layout description.
4828 static const int kHeaderSize = String::kSize;
4829 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4830
Leon Clarkee46be812010-01-19 14:06:41 +00004831 // Maximal memory usage for a single sequential ASCII string.
4832 static const int kMaxSize = 512 * MB;
4833 // Maximal length of a single sequential ASCII string.
4834 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4835 static const int kMaxLength = (kMaxSize - kHeaderSize);
4836
Steve Blocka7e24c12009-10-30 11:49:00 +00004837 // Support for StringInputBuffer.
4838 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4839 unsigned* offset,
4840 unsigned chars);
4841 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
4842 unsigned* offset,
4843 unsigned chars);
4844
4845 private:
4846 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
4847};
4848
4849
4850// The TwoByteString class captures sequential unicode string objects.
4851// Each character in the TwoByteString is a two-byte uint16_t.
4852class SeqTwoByteString: public SeqString {
4853 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004854 static const bool kHasAsciiEncoding = false;
4855
Steve Blocka7e24c12009-10-30 11:49:00 +00004856 // Dispatched behavior.
4857 inline uint16_t SeqTwoByteStringGet(int index);
4858 inline void SeqTwoByteStringSet(int index, uint16_t value);
4859
4860 // Get the address of the characters in this string.
4861 inline Address GetCharsAddress();
4862
4863 inline uc16* GetChars();
4864
4865 // For regexp code.
4866 const uint16_t* SeqTwoByteStringGetData(unsigned start);
4867
4868 // Casting
4869 static inline SeqTwoByteString* cast(Object* obj);
4870
4871 // Garbage collection support. This method is called by the
4872 // garbage collector to compute the actual size of a TwoByteString
4873 // instance.
4874 inline int SeqTwoByteStringSize(InstanceType instance_type);
4875
4876 // Computes the size for a TwoByteString instance of a given length.
4877 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004878 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004879 }
4880
4881 // Layout description.
4882 static const int kHeaderSize = String::kSize;
4883 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4884
Leon Clarkee46be812010-01-19 14:06:41 +00004885 // Maximal memory usage for a single sequential two-byte string.
4886 static const int kMaxSize = 512 * MB;
4887 // Maximal length of a single sequential two-byte string.
4888 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4889 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
4890
Steve Blocka7e24c12009-10-30 11:49:00 +00004891 // Support for StringInputBuffer.
4892 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4893 unsigned* offset_ptr,
4894 unsigned chars);
4895
4896 private:
4897 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
4898};
4899
4900
4901// The ConsString class describes string values built by using the
4902// addition operator on strings. A ConsString is a pair where the
4903// first and second components are pointers to other string values.
4904// One or both components of a ConsString can be pointers to other
4905// ConsStrings, creating a binary tree of ConsStrings where the leaves
4906// are non-ConsString string values. The string value represented by
4907// a ConsString can be obtained by concatenating the leaf string
4908// values in a left-to-right depth-first traversal of the tree.
4909class ConsString: public String {
4910 public:
4911 // First string of the cons cell.
4912 inline String* first();
4913 // Doesn't check that the result is a string, even in debug mode. This is
4914 // useful during GC where the mark bits confuse the checks.
4915 inline Object* unchecked_first();
4916 inline void set_first(String* first,
4917 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4918
4919 // Second string of the cons cell.
4920 inline String* second();
4921 // Doesn't check that the result is a string, even in debug mode. This is
4922 // useful during GC where the mark bits confuse the checks.
4923 inline Object* unchecked_second();
4924 inline void set_second(String* second,
4925 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4926
4927 // Dispatched behavior.
4928 uint16_t ConsStringGet(int index);
4929
4930 // Casting.
4931 static inline ConsString* cast(Object* obj);
4932
Steve Blocka7e24c12009-10-30 11:49:00 +00004933 // Layout description.
4934 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
4935 static const int kSecondOffset = kFirstOffset + kPointerSize;
4936 static const int kSize = kSecondOffset + kPointerSize;
4937
4938 // Support for StringInputBuffer.
4939 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
4940 unsigned* offset_ptr,
4941 unsigned chars);
4942 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4943 unsigned* offset_ptr,
4944 unsigned chars);
4945
4946 // Minimum length for a cons string.
4947 static const int kMinLength = 13;
4948
Iain Merrick75681382010-08-19 15:07:18 +01004949 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
4950 BodyDescriptor;
4951
Steve Blocka7e24c12009-10-30 11:49:00 +00004952 private:
4953 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
4954};
4955
4956
Steve Blocka7e24c12009-10-30 11:49:00 +00004957// The ExternalString class describes string values that are backed by
4958// a string resource that lies outside the V8 heap. ExternalStrings
4959// consist of the length field common to all strings, a pointer to the
4960// external resource. It is important to ensure (externally) that the
4961// resource is not deallocated while the ExternalString is live in the
4962// V8 heap.
4963//
4964// The API expects that all ExternalStrings are created through the
4965// API. Therefore, ExternalStrings should not be used internally.
4966class ExternalString: public String {
4967 public:
4968 // Casting
4969 static inline ExternalString* cast(Object* obj);
4970
4971 // Layout description.
4972 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
4973 static const int kSize = kResourceOffset + kPointerSize;
4974
4975 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
4976
4977 private:
4978 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
4979};
4980
4981
4982// The ExternalAsciiString class is an external string backed by an
4983// ASCII string.
4984class ExternalAsciiString: public ExternalString {
4985 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004986 static const bool kHasAsciiEncoding = true;
4987
Steve Blocka7e24c12009-10-30 11:49:00 +00004988 typedef v8::String::ExternalAsciiStringResource Resource;
4989
4990 // The underlying resource.
4991 inline Resource* resource();
4992 inline void set_resource(Resource* buffer);
4993
4994 // Dispatched behavior.
4995 uint16_t ExternalAsciiStringGet(int index);
4996
4997 // Casting.
4998 static inline ExternalAsciiString* cast(Object* obj);
4999
Steve Blockd0582a62009-12-15 09:54:21 +00005000 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005001 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
5002
5003 template<typename StaticVisitor>
5004 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00005005
Steve Blocka7e24c12009-10-30 11:49:00 +00005006 // Support for StringInputBuffer.
5007 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
5008 unsigned* offset,
5009 unsigned chars);
5010 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5011 unsigned* offset,
5012 unsigned chars);
5013
Steve Blocka7e24c12009-10-30 11:49:00 +00005014 private:
5015 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
5016};
5017
5018
5019// The ExternalTwoByteString class is an external string backed by a UTF-16
5020// encoded string.
5021class ExternalTwoByteString: public ExternalString {
5022 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005023 static const bool kHasAsciiEncoding = false;
5024
Steve Blocka7e24c12009-10-30 11:49:00 +00005025 typedef v8::String::ExternalStringResource Resource;
5026
5027 // The underlying string resource.
5028 inline Resource* resource();
5029 inline void set_resource(Resource* buffer);
5030
5031 // Dispatched behavior.
5032 uint16_t ExternalTwoByteStringGet(int index);
5033
5034 // For regexp code.
5035 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
5036
5037 // Casting.
5038 static inline ExternalTwoByteString* cast(Object* obj);
5039
Steve Blockd0582a62009-12-15 09:54:21 +00005040 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005041 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
5042
5043 template<typename StaticVisitor>
5044 inline void ExternalTwoByteStringIterateBody();
5045
Steve Blockd0582a62009-12-15 09:54:21 +00005046
Steve Blocka7e24c12009-10-30 11:49:00 +00005047 // Support for StringInputBuffer.
5048 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5049 unsigned* offset_ptr,
5050 unsigned chars);
5051
Steve Blocka7e24c12009-10-30 11:49:00 +00005052 private:
5053 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
5054};
5055
5056
5057// Utility superclass for stack-allocated objects that must be updated
5058// on gc. It provides two ways for the gc to update instances, either
5059// iterating or updating after gc.
5060class Relocatable BASE_EMBEDDED {
5061 public:
5062 inline Relocatable() : prev_(top_) { top_ = this; }
5063 virtual ~Relocatable() {
5064 ASSERT_EQ(top_, this);
5065 top_ = prev_;
5066 }
5067 virtual void IterateInstance(ObjectVisitor* v) { }
5068 virtual void PostGarbageCollection() { }
5069
5070 static void PostGarbageCollectionProcessing();
5071 static int ArchiveSpacePerThread();
5072 static char* ArchiveState(char* to);
5073 static char* RestoreState(char* from);
5074 static void Iterate(ObjectVisitor* v);
5075 static void Iterate(ObjectVisitor* v, Relocatable* top);
5076 static char* Iterate(ObjectVisitor* v, char* t);
5077 private:
5078 static Relocatable* top_;
5079 Relocatable* prev_;
5080};
5081
5082
5083// A flat string reader provides random access to the contents of a
5084// string independent of the character width of the string. The handle
5085// must be valid as long as the reader is being used.
5086class FlatStringReader : public Relocatable {
5087 public:
5088 explicit FlatStringReader(Handle<String> str);
5089 explicit FlatStringReader(Vector<const char> input);
5090 void PostGarbageCollection();
5091 inline uc32 Get(int index);
5092 int length() { return length_; }
5093 private:
5094 String** str_;
5095 bool is_ascii_;
5096 int length_;
5097 const void* start_;
5098};
5099
5100
5101// Note that StringInputBuffers are not valid across a GC! To fix this
5102// it would have to store a String Handle instead of a String* and
5103// AsciiStringReadBlock would have to be modified to use memcpy.
5104//
5105// StringInputBuffer is able to traverse any string regardless of how
5106// deeply nested a sequence of ConsStrings it is made of. However,
5107// performance will be better if deep strings are flattened before they
5108// are traversed. Since flattening requires memory allocation this is
5109// not always desirable, however (esp. in debugging situations).
5110class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
5111 public:
5112 virtual void Seek(unsigned pos);
5113 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
5114 inline StringInputBuffer(String* backing):
5115 unibrow::InputBuffer<String, String*, 1024>(backing) {}
5116};
5117
5118
5119class SafeStringInputBuffer
5120 : public unibrow::InputBuffer<String, String**, 256> {
5121 public:
5122 virtual void Seek(unsigned pos);
5123 inline SafeStringInputBuffer()
5124 : unibrow::InputBuffer<String, String**, 256>() {}
5125 inline SafeStringInputBuffer(String** backing)
5126 : unibrow::InputBuffer<String, String**, 256>(backing) {}
5127};
5128
5129
5130template <typename T>
5131class VectorIterator {
5132 public:
5133 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
5134 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
5135 T GetNext() { return data_[index_++]; }
5136 bool has_more() { return index_ < data_.length(); }
5137 private:
5138 Vector<const T> data_;
5139 int index_;
5140};
5141
5142
5143// The Oddball describes objects null, undefined, true, and false.
5144class Oddball: public HeapObject {
5145 public:
5146 // [to_string]: Cached to_string computed at startup.
5147 DECL_ACCESSORS(to_string, String)
5148
5149 // [to_number]: Cached to_number computed at startup.
5150 DECL_ACCESSORS(to_number, Object)
5151
5152 // Casting.
5153 static inline Oddball* cast(Object* obj);
5154
5155 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00005156#ifdef DEBUG
5157 void OddballVerify();
5158#endif
5159
5160 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07005161 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
5162 Object* to_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00005163
5164 // Layout description.
5165 static const int kToStringOffset = HeapObject::kHeaderSize;
5166 static const int kToNumberOffset = kToStringOffset + kPointerSize;
5167 static const int kSize = kToNumberOffset + kPointerSize;
5168
Iain Merrick75681382010-08-19 15:07:18 +01005169 typedef FixedBodyDescriptor<kToStringOffset,
5170 kToNumberOffset + kPointerSize,
5171 kSize> BodyDescriptor;
5172
Steve Blocka7e24c12009-10-30 11:49:00 +00005173 private:
5174 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
5175};
5176
5177
5178class JSGlobalPropertyCell: public HeapObject {
5179 public:
5180 // [value]: value of the global property.
5181 DECL_ACCESSORS(value, Object)
5182
5183 // Casting.
5184 static inline JSGlobalPropertyCell* cast(Object* obj);
5185
Steve Blocka7e24c12009-10-30 11:49:00 +00005186#ifdef DEBUG
5187 void JSGlobalPropertyCellVerify();
5188 void JSGlobalPropertyCellPrint();
5189#endif
5190
5191 // Layout description.
5192 static const int kValueOffset = HeapObject::kHeaderSize;
5193 static const int kSize = kValueOffset + kPointerSize;
5194
Iain Merrick75681382010-08-19 15:07:18 +01005195 typedef FixedBodyDescriptor<kValueOffset,
5196 kValueOffset + kPointerSize,
5197 kSize> BodyDescriptor;
5198
Steve Blocka7e24c12009-10-30 11:49:00 +00005199 private:
5200 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
5201};
5202
5203
5204
5205// Proxy describes objects pointing from JavaScript to C structures.
5206// Since they cannot contain references to JS HeapObjects they can be
5207// placed in old_data_space.
5208class Proxy: public HeapObject {
5209 public:
5210 // [proxy]: field containing the address.
5211 inline Address proxy();
5212 inline void set_proxy(Address value);
5213
5214 // Casting.
5215 static inline Proxy* cast(Object* obj);
5216
5217 // Dispatched behavior.
5218 inline void ProxyIterateBody(ObjectVisitor* v);
Iain Merrick75681382010-08-19 15:07:18 +01005219
5220 template<typename StaticVisitor>
5221 inline void ProxyIterateBody();
5222
Steve Blocka7e24c12009-10-30 11:49:00 +00005223#ifdef DEBUG
5224 void ProxyPrint();
5225 void ProxyVerify();
5226#endif
5227
5228 // Layout description.
5229
5230 static const int kProxyOffset = HeapObject::kHeaderSize;
5231 static const int kSize = kProxyOffset + kPointerSize;
5232
5233 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
5234
5235 private:
5236 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
5237};
5238
5239
5240// The JSArray describes JavaScript Arrays
5241// Such an array can be in one of two modes:
5242// - fast, backing storage is a FixedArray and length <= elements.length();
5243// Please note: push and pop can be used to grow and shrink the array.
5244// - slow, backing storage is a HashTable with numbers as keys.
5245class JSArray: public JSObject {
5246 public:
5247 // [length]: The length property.
5248 DECL_ACCESSORS(length, Object)
5249
Leon Clarke4515c472010-02-03 11:58:03 +00005250 // Overload the length setter to skip write barrier when the length
5251 // is set to a smi. This matches the set function on FixedArray.
5252 inline void set_length(Smi* length);
5253
John Reck59135872010-11-02 12:39:01 -07005254 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
5255 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005256
5257 // Initialize the array with the given capacity. The function may
5258 // fail due to out-of-memory situations, but only if the requested
5259 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07005260 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00005261
5262 // Set the content of the array to the content of storage.
5263 inline void SetContent(FixedArray* storage);
5264
5265 // Casting.
5266 static inline JSArray* cast(Object* obj);
5267
5268 // Uses handles. Ensures that the fixed array backing the JSArray has at
5269 // least the stated size.
5270 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
5271
5272 // Dispatched behavior.
5273#ifdef DEBUG
5274 void JSArrayPrint();
5275 void JSArrayVerify();
5276#endif
5277
5278 // Number of element slots to pre-allocate for an empty array.
5279 static const int kPreallocatedArrayElements = 4;
5280
5281 // Layout description.
5282 static const int kLengthOffset = JSObject::kHeaderSize;
5283 static const int kSize = kLengthOffset + kPointerSize;
5284
5285 private:
5286 // Expand the fixed array backing of a fast-case JSArray to at least
5287 // the requested size.
5288 void Expand(int minimum_size_of_backing_fixed_array);
5289
5290 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
5291};
5292
5293
Steve Block6ded16b2010-05-10 14:33:55 +01005294// JSRegExpResult is just a JSArray with a specific initial map.
5295// This initial map adds in-object properties for "index" and "input"
5296// properties, as assigned by RegExp.prototype.exec, which allows
5297// faster creation of RegExp exec results.
5298// This class just holds constants used when creating the result.
5299// After creation the result must be treated as a JSArray in all regards.
5300class JSRegExpResult: public JSArray {
5301 public:
5302 // Offsets of object fields.
5303 static const int kIndexOffset = JSArray::kSize;
5304 static const int kInputOffset = kIndexOffset + kPointerSize;
5305 static const int kSize = kInputOffset + kPointerSize;
5306 // Indices of in-object properties.
5307 static const int kIndexIndex = 0;
5308 static const int kInputIndex = 1;
5309 private:
5310 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
5311};
5312
5313
Steve Blocka7e24c12009-10-30 11:49:00 +00005314// An accessor must have a getter, but can have no setter.
5315//
5316// When setting a property, V8 searches accessors in prototypes.
5317// If an accessor was found and it does not have a setter,
5318// the request is ignored.
5319//
5320// If the accessor in the prototype has the READ_ONLY property attribute, then
5321// a new value is added to the local object when the property is set.
5322// This shadows the accessor in the prototype.
5323class AccessorInfo: public Struct {
5324 public:
5325 DECL_ACCESSORS(getter, Object)
5326 DECL_ACCESSORS(setter, Object)
5327 DECL_ACCESSORS(data, Object)
5328 DECL_ACCESSORS(name, Object)
5329 DECL_ACCESSORS(flag, Smi)
Steve Blockd0582a62009-12-15 09:54:21 +00005330 DECL_ACCESSORS(load_stub_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00005331
5332 inline bool all_can_read();
5333 inline void set_all_can_read(bool value);
5334
5335 inline bool all_can_write();
5336 inline void set_all_can_write(bool value);
5337
5338 inline bool prohibits_overwriting();
5339 inline void set_prohibits_overwriting(bool value);
5340
5341 inline PropertyAttributes property_attributes();
5342 inline void set_property_attributes(PropertyAttributes attributes);
5343
5344 static inline AccessorInfo* cast(Object* obj);
5345
5346#ifdef DEBUG
5347 void AccessorInfoPrint();
5348 void AccessorInfoVerify();
5349#endif
5350
5351 static const int kGetterOffset = HeapObject::kHeaderSize;
5352 static const int kSetterOffset = kGetterOffset + kPointerSize;
5353 static const int kDataOffset = kSetterOffset + kPointerSize;
5354 static const int kNameOffset = kDataOffset + kPointerSize;
5355 static const int kFlagOffset = kNameOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00005356 static const int kLoadStubCacheOffset = kFlagOffset + kPointerSize;
5357 static const int kSize = kLoadStubCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005358
5359 private:
5360 // Bit positions in flag.
5361 static const int kAllCanReadBit = 0;
5362 static const int kAllCanWriteBit = 1;
5363 static const int kProhibitsOverwritingBit = 2;
5364 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
5365
5366 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
5367};
5368
5369
5370class AccessCheckInfo: public Struct {
5371 public:
5372 DECL_ACCESSORS(named_callback, Object)
5373 DECL_ACCESSORS(indexed_callback, Object)
5374 DECL_ACCESSORS(data, Object)
5375
5376 static inline AccessCheckInfo* cast(Object* obj);
5377
5378#ifdef DEBUG
5379 void AccessCheckInfoPrint();
5380 void AccessCheckInfoVerify();
5381#endif
5382
5383 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
5384 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
5385 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
5386 static const int kSize = kDataOffset + kPointerSize;
5387
5388 private:
5389 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
5390};
5391
5392
5393class InterceptorInfo: public Struct {
5394 public:
5395 DECL_ACCESSORS(getter, Object)
5396 DECL_ACCESSORS(setter, Object)
5397 DECL_ACCESSORS(query, Object)
5398 DECL_ACCESSORS(deleter, Object)
5399 DECL_ACCESSORS(enumerator, Object)
5400 DECL_ACCESSORS(data, Object)
5401
5402 static inline InterceptorInfo* cast(Object* obj);
5403
5404#ifdef DEBUG
5405 void InterceptorInfoPrint();
5406 void InterceptorInfoVerify();
5407#endif
5408
5409 static const int kGetterOffset = HeapObject::kHeaderSize;
5410 static const int kSetterOffset = kGetterOffset + kPointerSize;
5411 static const int kQueryOffset = kSetterOffset + kPointerSize;
5412 static const int kDeleterOffset = kQueryOffset + kPointerSize;
5413 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
5414 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
5415 static const int kSize = kDataOffset + kPointerSize;
5416
5417 private:
5418 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
5419};
5420
5421
5422class CallHandlerInfo: public Struct {
5423 public:
5424 DECL_ACCESSORS(callback, Object)
5425 DECL_ACCESSORS(data, Object)
5426
5427 static inline CallHandlerInfo* cast(Object* obj);
5428
5429#ifdef DEBUG
5430 void CallHandlerInfoPrint();
5431 void CallHandlerInfoVerify();
5432#endif
5433
5434 static const int kCallbackOffset = HeapObject::kHeaderSize;
5435 static const int kDataOffset = kCallbackOffset + kPointerSize;
5436 static const int kSize = kDataOffset + kPointerSize;
5437
5438 private:
5439 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
5440};
5441
5442
5443class TemplateInfo: public Struct {
5444 public:
5445 DECL_ACCESSORS(tag, Object)
5446 DECL_ACCESSORS(property_list, Object)
5447
5448#ifdef DEBUG
5449 void TemplateInfoVerify();
5450#endif
5451
5452 static const int kTagOffset = HeapObject::kHeaderSize;
5453 static const int kPropertyListOffset = kTagOffset + kPointerSize;
5454 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
5455 protected:
5456 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
5457 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
5458};
5459
5460
5461class FunctionTemplateInfo: public TemplateInfo {
5462 public:
5463 DECL_ACCESSORS(serial_number, Object)
5464 DECL_ACCESSORS(call_code, Object)
5465 DECL_ACCESSORS(property_accessors, Object)
5466 DECL_ACCESSORS(prototype_template, Object)
5467 DECL_ACCESSORS(parent_template, Object)
5468 DECL_ACCESSORS(named_property_handler, Object)
5469 DECL_ACCESSORS(indexed_property_handler, Object)
5470 DECL_ACCESSORS(instance_template, Object)
5471 DECL_ACCESSORS(class_name, Object)
5472 DECL_ACCESSORS(signature, Object)
5473 DECL_ACCESSORS(instance_call_handler, Object)
5474 DECL_ACCESSORS(access_check_info, Object)
5475 DECL_ACCESSORS(flag, Smi)
5476
5477 // Following properties use flag bits.
5478 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
5479 DECL_BOOLEAN_ACCESSORS(undetectable)
5480 // If the bit is set, object instances created by this function
5481 // requires access check.
5482 DECL_BOOLEAN_ACCESSORS(needs_access_check)
5483
5484 static inline FunctionTemplateInfo* cast(Object* obj);
5485
5486#ifdef DEBUG
5487 void FunctionTemplateInfoPrint();
5488 void FunctionTemplateInfoVerify();
5489#endif
5490
5491 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
5492 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
5493 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
5494 static const int kPrototypeTemplateOffset =
5495 kPropertyAccessorsOffset + kPointerSize;
5496 static const int kParentTemplateOffset =
5497 kPrototypeTemplateOffset + kPointerSize;
5498 static const int kNamedPropertyHandlerOffset =
5499 kParentTemplateOffset + kPointerSize;
5500 static const int kIndexedPropertyHandlerOffset =
5501 kNamedPropertyHandlerOffset + kPointerSize;
5502 static const int kInstanceTemplateOffset =
5503 kIndexedPropertyHandlerOffset + kPointerSize;
5504 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
5505 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
5506 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
5507 static const int kAccessCheckInfoOffset =
5508 kInstanceCallHandlerOffset + kPointerSize;
5509 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
5510 static const int kSize = kFlagOffset + kPointerSize;
5511
5512 private:
5513 // Bit position in the flag, from least significant bit position.
5514 static const int kHiddenPrototypeBit = 0;
5515 static const int kUndetectableBit = 1;
5516 static const int kNeedsAccessCheckBit = 2;
5517
5518 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
5519};
5520
5521
5522class ObjectTemplateInfo: public TemplateInfo {
5523 public:
5524 DECL_ACCESSORS(constructor, Object)
5525 DECL_ACCESSORS(internal_field_count, Object)
5526
5527 static inline ObjectTemplateInfo* cast(Object* obj);
5528
5529#ifdef DEBUG
5530 void ObjectTemplateInfoPrint();
5531 void ObjectTemplateInfoVerify();
5532#endif
5533
5534 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
5535 static const int kInternalFieldCountOffset =
5536 kConstructorOffset + kPointerSize;
5537 static const int kSize = kInternalFieldCountOffset + kPointerSize;
5538};
5539
5540
5541class SignatureInfo: public Struct {
5542 public:
5543 DECL_ACCESSORS(receiver, Object)
5544 DECL_ACCESSORS(args, Object)
5545
5546 static inline SignatureInfo* cast(Object* obj);
5547
5548#ifdef DEBUG
5549 void SignatureInfoPrint();
5550 void SignatureInfoVerify();
5551#endif
5552
5553 static const int kReceiverOffset = Struct::kHeaderSize;
5554 static const int kArgsOffset = kReceiverOffset + kPointerSize;
5555 static const int kSize = kArgsOffset + kPointerSize;
5556
5557 private:
5558 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
5559};
5560
5561
5562class TypeSwitchInfo: public Struct {
5563 public:
5564 DECL_ACCESSORS(types, Object)
5565
5566 static inline TypeSwitchInfo* cast(Object* obj);
5567
5568#ifdef DEBUG
5569 void TypeSwitchInfoPrint();
5570 void TypeSwitchInfoVerify();
5571#endif
5572
5573 static const int kTypesOffset = Struct::kHeaderSize;
5574 static const int kSize = kTypesOffset + kPointerSize;
5575};
5576
5577
5578#ifdef ENABLE_DEBUGGER_SUPPORT
5579// The DebugInfo class holds additional information for a function being
5580// debugged.
5581class DebugInfo: public Struct {
5582 public:
5583 // The shared function info for the source being debugged.
5584 DECL_ACCESSORS(shared, SharedFunctionInfo)
5585 // Code object for the original code.
5586 DECL_ACCESSORS(original_code, Code)
5587 // Code object for the patched code. This code object is the code object
5588 // currently active for the function.
5589 DECL_ACCESSORS(code, Code)
5590 // Fixed array holding status information for each active break point.
5591 DECL_ACCESSORS(break_points, FixedArray)
5592
5593 // Check if there is a break point at a code position.
5594 bool HasBreakPoint(int code_position);
5595 // Get the break point info object for a code position.
5596 Object* GetBreakPointInfo(int code_position);
5597 // Clear a break point.
5598 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
5599 int code_position,
5600 Handle<Object> break_point_object);
5601 // Set a break point.
5602 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
5603 int source_position, int statement_position,
5604 Handle<Object> break_point_object);
5605 // Get the break point objects for a code position.
5606 Object* GetBreakPointObjects(int code_position);
5607 // Find the break point info holding this break point object.
5608 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
5609 Handle<Object> break_point_object);
5610 // Get the number of break points for this function.
5611 int GetBreakPointCount();
5612
5613 static inline DebugInfo* cast(Object* obj);
5614
5615#ifdef DEBUG
5616 void DebugInfoPrint();
5617 void DebugInfoVerify();
5618#endif
5619
5620 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
5621 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
5622 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
5623 static const int kActiveBreakPointsCountIndex =
5624 kPatchedCodeIndex + kPointerSize;
5625 static const int kBreakPointsStateIndex =
5626 kActiveBreakPointsCountIndex + kPointerSize;
5627 static const int kSize = kBreakPointsStateIndex + kPointerSize;
5628
5629 private:
5630 static const int kNoBreakPointInfo = -1;
5631
5632 // Lookup the index in the break_points array for a code position.
5633 int GetBreakPointInfoIndex(int code_position);
5634
5635 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
5636};
5637
5638
5639// The BreakPointInfo class holds information for break points set in a
5640// function. The DebugInfo object holds a BreakPointInfo object for each code
5641// position with one or more break points.
5642class BreakPointInfo: public Struct {
5643 public:
5644 // The position in the code for the break point.
5645 DECL_ACCESSORS(code_position, Smi)
5646 // The position in the source for the break position.
5647 DECL_ACCESSORS(source_position, Smi)
5648 // The position in the source for the last statement before this break
5649 // position.
5650 DECL_ACCESSORS(statement_position, Smi)
5651 // List of related JavaScript break points.
5652 DECL_ACCESSORS(break_point_objects, Object)
5653
5654 // Removes a break point.
5655 static void ClearBreakPoint(Handle<BreakPointInfo> info,
5656 Handle<Object> break_point_object);
5657 // Set a break point.
5658 static void SetBreakPoint(Handle<BreakPointInfo> info,
5659 Handle<Object> break_point_object);
5660 // Check if break point info has this break point object.
5661 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
5662 Handle<Object> break_point_object);
5663 // Get the number of break points for this code position.
5664 int GetBreakPointCount();
5665
5666 static inline BreakPointInfo* cast(Object* obj);
5667
5668#ifdef DEBUG
5669 void BreakPointInfoPrint();
5670 void BreakPointInfoVerify();
5671#endif
5672
5673 static const int kCodePositionIndex = Struct::kHeaderSize;
5674 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
5675 static const int kStatementPositionIndex =
5676 kSourcePositionIndex + kPointerSize;
5677 static const int kBreakPointObjectsIndex =
5678 kStatementPositionIndex + kPointerSize;
5679 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
5680
5681 private:
5682 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
5683};
5684#endif // ENABLE_DEBUGGER_SUPPORT
5685
5686
5687#undef DECL_BOOLEAN_ACCESSORS
5688#undef DECL_ACCESSORS
5689
5690
5691// Abstract base class for visiting, and optionally modifying, the
5692// pointers contained in Objects. Used in GC and serialization/deserialization.
5693class ObjectVisitor BASE_EMBEDDED {
5694 public:
5695 virtual ~ObjectVisitor() {}
5696
5697 // Visits a contiguous arrays of pointers in the half-open range
5698 // [start, end). Any or all of the values may be modified on return.
5699 virtual void VisitPointers(Object** start, Object** end) = 0;
5700
5701 // To allow lazy clearing of inline caches the visitor has
5702 // a rich interface for iterating over Code objects..
5703
5704 // Visits a code target in the instruction stream.
5705 virtual void VisitCodeTarget(RelocInfo* rinfo);
5706
Steve Block791712a2010-08-27 10:21:07 +01005707 // Visits a code entry in a JS function.
5708 virtual void VisitCodeEntry(Address entry_address);
5709
Steve Blocka7e24c12009-10-30 11:49:00 +00005710 // Visits a runtime entry in the instruction stream.
5711 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
5712
Steve Blockd0582a62009-12-15 09:54:21 +00005713 // Visits the resource of an ASCII or two-byte string.
5714 virtual void VisitExternalAsciiString(
5715 v8::String::ExternalAsciiStringResource** resource) {}
5716 virtual void VisitExternalTwoByteString(
5717 v8::String::ExternalStringResource** resource) {}
5718
Steve Blocka7e24c12009-10-30 11:49:00 +00005719 // Visits a debug call target in the instruction stream.
5720 virtual void VisitDebugTarget(RelocInfo* rinfo);
5721
5722 // Handy shorthand for visiting a single pointer.
5723 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
5724
5725 // Visits a contiguous arrays of external references (references to the C++
5726 // heap) in the half-open range [start, end). Any or all of the values
5727 // may be modified on return.
5728 virtual void VisitExternalReferences(Address* start, Address* end) {}
5729
5730 inline void VisitExternalReference(Address* p) {
5731 VisitExternalReferences(p, p + 1);
5732 }
5733
5734#ifdef DEBUG
5735 // Intended for serialization/deserialization checking: insert, or
5736 // check for the presence of, a tag at this position in the stream.
5737 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00005738#else
5739 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00005740#endif
5741};
5742
5743
Iain Merrick75681382010-08-19 15:07:18 +01005744class StructBodyDescriptor : public
5745 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
5746 public:
5747 static inline int SizeOf(Map* map, HeapObject* object) {
5748 return map->instance_size();
5749 }
5750};
5751
5752
Steve Blocka7e24c12009-10-30 11:49:00 +00005753// BooleanBit is a helper class for setting and getting a bit in an
5754// integer or Smi.
5755class BooleanBit : public AllStatic {
5756 public:
5757 static inline bool get(Smi* smi, int bit_position) {
5758 return get(smi->value(), bit_position);
5759 }
5760
5761 static inline bool get(int value, int bit_position) {
5762 return (value & (1 << bit_position)) != 0;
5763 }
5764
5765 static inline Smi* set(Smi* smi, int bit_position, bool v) {
5766 return Smi::FromInt(set(smi->value(), bit_position, v));
5767 }
5768
5769 static inline int set(int value, int bit_position, bool v) {
5770 if (v) {
5771 value |= (1 << bit_position);
5772 } else {
5773 value &= ~(1 << bit_position);
5774 }
5775 return value;
5776 }
5777};
5778
5779} } // namespace v8::internal
5780
5781#endif // V8_OBJECTS_H_