blob: d7b87c65d72df64da143200773db08e2295fa343 [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
Steve Block1e0659c2011-05-24 12:43:12 +010057// - JSValue
58// - JSMessageObject
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010059// - ByteArray
60// - PixelArray
61// - ExternalArray
62// - ExternalByteArray
63// - ExternalUnsignedByteArray
64// - ExternalShortArray
65// - ExternalUnsignedShortArray
66// - ExternalIntArray
67// - ExternalUnsignedIntArray
68// - ExternalFloatArray
69// - FixedArray
70// - DescriptorArray
71// - HashTable
72// - Dictionary
73// - SymbolTable
74// - CompilationCacheTable
75// - CodeCacheHashTable
76// - MapCache
77// - Context
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010078// - JSFunctionResultCache
Kristian Monsen50ef84f2010-07-29 15:18:00 +010079// - SerializedScopeInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000080// - String
81// - SeqString
82// - SeqAsciiString
83// - SeqTwoByteString
84// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000085// - ExternalString
86// - ExternalAsciiString
87// - ExternalTwoByteString
88// - HeapNumber
89// - Code
90// - Map
91// - Oddball
92// - Proxy
93// - SharedFunctionInfo
94// - Struct
95// - AccessorInfo
96// - AccessCheckInfo
97// - InterceptorInfo
98// - CallHandlerInfo
99// - TemplateInfo
100// - FunctionTemplateInfo
101// - ObjectTemplateInfo
102// - Script
103// - SignatureInfo
104// - TypeSwitchInfo
105// - DebugInfo
106// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100107// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000108//
109// Formats of Object*:
110// Smi: [31 bit signed int] 0
111// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
112// Failure: [30 bit signed int] 11
113
114// Ecma-262 3rd 8.6.1
115enum PropertyAttributes {
116 NONE = v8::None,
117 READ_ONLY = v8::ReadOnly,
118 DONT_ENUM = v8::DontEnum,
119 DONT_DELETE = v8::DontDelete,
120 ABSENT = 16 // Used in runtime to indicate a property is absent.
121 // ABSENT can never be stored in or returned from a descriptor's attributes
122 // bitfield. It is only used as a return value meaning the attributes of
123 // a non-existent property.
124};
125
126namespace v8 {
127namespace internal {
128
129
130// PropertyDetails captures type and attributes for a property.
131// They are used both in property dictionaries and instance descriptors.
132class PropertyDetails BASE_EMBEDDED {
133 public:
134
135 PropertyDetails(PropertyAttributes attributes,
136 PropertyType type,
137 int index = 0) {
138 ASSERT(TypeField::is_valid(type));
139 ASSERT(AttributesField::is_valid(attributes));
140 ASSERT(IndexField::is_valid(index));
141
142 value_ = TypeField::encode(type)
143 | AttributesField::encode(attributes)
144 | IndexField::encode(index);
145
146 ASSERT(type == this->type());
147 ASSERT(attributes == this->attributes());
148 ASSERT(index == this->index());
149 }
150
151 // Conversion for storing details as Object*.
152 inline PropertyDetails(Smi* smi);
153 inline Smi* AsSmi();
154
155 PropertyType type() { return TypeField::decode(value_); }
156
157 bool IsTransition() {
158 PropertyType t = type();
159 ASSERT(t != INTERCEPTOR);
160 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION;
161 }
162
163 bool IsProperty() {
164 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
165 }
166
167 PropertyAttributes attributes() { return AttributesField::decode(value_); }
168
169 int index() { return IndexField::decode(value_); }
170
171 inline PropertyDetails AsDeleted();
172
173 static bool IsValidIndex(int index) { return IndexField::is_valid(index); }
174
175 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
176 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
177 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
178 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
179
180 // Bit fields in value_ (type, shift, size). Must be public so the
181 // constants can be embedded in generated code.
182 class TypeField: public BitField<PropertyType, 0, 3> {};
183 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
184 class DeletedField: public BitField<uint32_t, 6, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000185 class IndexField: public BitField<uint32_t, 7, 32-7> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000186
187 static const int kInitialIndex = 1;
188 private:
189 uint32_t value_;
190};
191
192
193// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
194enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
195
196
197// PropertyNormalizationMode is used to specify whether to keep
198// inobject properties when normalizing properties of a JSObject.
199enum PropertyNormalizationMode {
200 CLEAR_INOBJECT_PROPERTIES,
201 KEEP_INOBJECT_PROPERTIES
202};
203
204
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100205// NormalizedMapSharingMode is used to specify whether a map may be shared
206// by different objects with normalized properties.
207enum NormalizedMapSharingMode {
208 UNIQUE_NORMALIZED_MAP,
209 SHARED_NORMALIZED_MAP
210};
211
212
Steve Block791712a2010-08-27 10:21:07 +0100213// Instance size sentinel for objects of variable size.
214static const int kVariableSizeSentinel = 0;
215
216
Steve Blocka7e24c12009-10-30 11:49:00 +0000217// All Maps have a field instance_type containing a InstanceType.
218// It describes the type of the instances.
219//
220// As an example, a JavaScript object is a heap object and its map
221// instance_type is JS_OBJECT_TYPE.
222//
223// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000224// mirror their encoding in the instance_type field of the map. The default
225// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
226// encoding is mentioned explicitly in the name. Likewise, the default
227// representation is considered sequential. It is not mentioned in the
228// name. The other representations (eg, CONS, EXTERNAL) are explicitly
229// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
230// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000231//
232// NOTE: The following things are some that depend on the string types having
233// instance_types that are less than those of all other types:
234// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
235// Object::IsString.
236//
237// NOTE: Everything following JS_VALUE_TYPE is considered a
238// JSObject for GC purposes. The first four entries here have typeof
239// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000240#define INSTANCE_TYPE_LIST_ALL(V) \
241 V(SYMBOL_TYPE) \
242 V(ASCII_SYMBOL_TYPE) \
243 V(CONS_SYMBOL_TYPE) \
244 V(CONS_ASCII_SYMBOL_TYPE) \
245 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100246 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000247 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
248 V(STRING_TYPE) \
249 V(ASCII_STRING_TYPE) \
250 V(CONS_STRING_TYPE) \
251 V(CONS_ASCII_STRING_TYPE) \
252 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100253 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000254 V(EXTERNAL_ASCII_STRING_TYPE) \
255 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
256 \
257 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000258 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000259 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100260 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000261 \
262 V(HEAP_NUMBER_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000263 V(PROXY_TYPE) \
264 V(BYTE_ARRAY_TYPE) \
265 V(PIXEL_ARRAY_TYPE) \
266 /* Note: the order of these external array */ \
267 /* types is relied upon in */ \
268 /* Object::IsExternalArray(). */ \
269 V(EXTERNAL_BYTE_ARRAY_TYPE) \
270 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
271 V(EXTERNAL_SHORT_ARRAY_TYPE) \
272 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
273 V(EXTERNAL_INT_ARRAY_TYPE) \
274 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
275 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
276 V(FILLER_TYPE) \
277 \
278 V(ACCESSOR_INFO_TYPE) \
279 V(ACCESS_CHECK_INFO_TYPE) \
280 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000281 V(CALL_HANDLER_INFO_TYPE) \
282 V(FUNCTION_TEMPLATE_INFO_TYPE) \
283 V(OBJECT_TEMPLATE_INFO_TYPE) \
284 V(SIGNATURE_INFO_TYPE) \
285 V(TYPE_SWITCH_INFO_TYPE) \
286 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100287 V(CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000288 \
Iain Merrick75681382010-08-19 15:07:18 +0100289 V(FIXED_ARRAY_TYPE) \
290 V(SHARED_FUNCTION_INFO_TYPE) \
291 \
Steve Block1e0659c2011-05-24 12:43:12 +0100292 V(JS_MESSAGE_OBJECT_TYPE) \
293 \
Steve Blockd0582a62009-12-15 09:54:21 +0000294 V(JS_VALUE_TYPE) \
295 V(JS_OBJECT_TYPE) \
296 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
297 V(JS_GLOBAL_OBJECT_TYPE) \
298 V(JS_BUILTINS_OBJECT_TYPE) \
299 V(JS_GLOBAL_PROXY_TYPE) \
300 V(JS_ARRAY_TYPE) \
301 V(JS_REGEXP_TYPE) \
302 \
303 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000304
305#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000306#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
307 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000308 V(BREAK_POINT_INFO_TYPE)
309#else
310#define INSTANCE_TYPE_LIST_DEBUGGER(V)
311#endif
312
Steve Blockd0582a62009-12-15 09:54:21 +0000313#define INSTANCE_TYPE_LIST(V) \
314 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000315 INSTANCE_TYPE_LIST_DEBUGGER(V)
316
317
318// Since string types are not consecutive, this macro is used to
319// iterate over them.
320#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000321 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100322 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000323 symbol, \
324 Symbol) \
325 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100326 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000327 ascii_symbol, \
328 AsciiSymbol) \
329 V(CONS_SYMBOL_TYPE, \
330 ConsString::kSize, \
331 cons_symbol, \
332 ConsSymbol) \
333 V(CONS_ASCII_SYMBOL_TYPE, \
334 ConsString::kSize, \
335 cons_ascii_symbol, \
336 ConsAsciiSymbol) \
337 V(EXTERNAL_SYMBOL_TYPE, \
338 ExternalTwoByteString::kSize, \
339 external_symbol, \
340 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100341 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
342 ExternalTwoByteString::kSize, \
343 external_symbol_with_ascii_data, \
344 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000345 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
346 ExternalAsciiString::kSize, \
347 external_ascii_symbol, \
348 ExternalAsciiSymbol) \
349 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100350 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000351 string, \
352 String) \
353 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100354 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000355 ascii_string, \
356 AsciiString) \
357 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000358 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000359 cons_string, \
360 ConsString) \
361 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000363 cons_ascii_string, \
364 ConsAsciiString) \
365 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000367 external_string, \
368 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100369 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
370 ExternalTwoByteString::kSize, \
371 external_string_with_ascii_data, \
372 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000373 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000375 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100376 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000377
378// A struct is a simple object a set of object-valued fields. Including an
379// object type in this causes the compiler to generate most of the boilerplate
380// code for the class including allocation and garbage collection routines,
381// casts and predicates. All you need to define is the class, methods and
382// object verification routines. Easy, no?
383//
384// Note that for subtle reasons related to the ordering or numerical values of
385// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
386// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000387#define STRUCT_LIST_ALL(V) \
388 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
389 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
390 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
391 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
392 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
393 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
394 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
395 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100396 V(SCRIPT, Script, script) \
397 V(CODE_CACHE, CodeCache, code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000398
399#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000400#define STRUCT_LIST_DEBUGGER(V) \
401 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
403#else
404#define STRUCT_LIST_DEBUGGER(V)
405#endif
406
Steve Blockd0582a62009-12-15 09:54:21 +0000407#define STRUCT_LIST(V) \
408 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000409 STRUCT_LIST_DEBUGGER(V)
410
411// We use the full 8 bits of the instance_type field to encode heap object
412// instance types. The high-order bit (bit 7) is set if the object is not a
413// string, and cleared if it is a string.
414const uint32_t kIsNotStringMask = 0x80;
415const uint32_t kStringTag = 0x0;
416const uint32_t kNotStringTag = 0x80;
417
Leon Clarkee46be812010-01-19 14:06:41 +0000418// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
419// There are not enough types that the non-string types (with bit 7 set) can
420// have bit 6 set too.
421const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000422const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000423const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000424
Steve Blocka7e24c12009-10-30 11:49:00 +0000425// If bit 7 is clear then bit 2 indicates whether the string consists of
426// two-byte characters or one-byte characters.
427const uint32_t kStringEncodingMask = 0x4;
428const uint32_t kTwoByteStringTag = 0x0;
429const uint32_t kAsciiStringTag = 0x4;
430
431// If bit 7 is clear, the low-order 2 bits indicate the representation
432// of the string.
433const uint32_t kStringRepresentationMask = 0x03;
434enum StringRepresentationTag {
435 kSeqStringTag = 0x0,
436 kConsStringTag = 0x1,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100437 kExternalStringTag = 0x2
Steve Blocka7e24c12009-10-30 11:49:00 +0000438};
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100439const uint32_t kIsConsStringMask = 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000440
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100441// If bit 7 is clear, then bit 3 indicates whether this two-byte
442// string actually contains ascii data.
443const uint32_t kAsciiDataHintMask = 0x08;
444const uint32_t kAsciiDataHintTag = 0x08;
445
Steve Blocka7e24c12009-10-30 11:49:00 +0000446
447// A ConsString with an empty string as the right side is a candidate
448// for being shortcut by the garbage collector unless it is a
449// symbol. It's not common to have non-flat symbols, so we do not
450// shortcut them thereby avoiding turning symbols into strings. See
451// heap.cc and mark-compact.cc.
452const uint32_t kShortcutTypeMask =
453 kIsNotStringMask |
454 kIsSymbolMask |
455 kStringRepresentationMask;
456const uint32_t kShortcutTypeTag = kConsStringTag;
457
458
459enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000460 // String types.
Steve Block1e0659c2011-05-24 12:43:12 +0100461 // FIRST_STRING_TYPE
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100462 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000463 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100464 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000465 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100466 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
467 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
468 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000469 EXTERNAL_ASCII_SYMBOL_TYPE =
470 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100471 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000472 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100473 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000474 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100475 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
476 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
477 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Block1e0659c2011-05-24 12:43:12 +0100478 // LAST_STRING_TYPE
Steve Blockd0582a62009-12-15 09:54:21 +0000479 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
480 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000481
Leon Clarkee46be812010-01-19 14:06:41 +0000482 // Objects allocated in their own spaces (never in new space).
483 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000484 CODE_TYPE,
485 ODDBALL_TYPE,
486 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000487
488 // "Data", objects that cannot contain non-map-word pointers to heap
489 // objects.
490 HEAP_NUMBER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000491 PROXY_TYPE,
492 BYTE_ARRAY_TYPE,
493 PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000494 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000495 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
496 EXTERNAL_SHORT_ARRAY_TYPE,
497 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
498 EXTERNAL_INT_ARRAY_TYPE,
499 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000500 EXTERNAL_FLOAT_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
501 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000502
Leon Clarkee46be812010-01-19 14:06:41 +0000503 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 ACCESSOR_INFO_TYPE,
505 ACCESS_CHECK_INFO_TYPE,
506 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 CALL_HANDLER_INFO_TYPE,
508 FUNCTION_TEMPLATE_INFO_TYPE,
509 OBJECT_TEMPLATE_INFO_TYPE,
510 SIGNATURE_INFO_TYPE,
511 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000512 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100513 CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100514 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
515 // is defined. However as include/v8.h contain some of the instance type
516 // constants always having them avoids them getting different numbers
517 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000518 DEBUG_INFO_TYPE,
519 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000520
Leon Clarkee46be812010-01-19 14:06:41 +0000521 FIXED_ARRAY_TYPE,
522 SHARED_FUNCTION_INFO_TYPE,
523
Steve Block1e0659c2011-05-24 12:43:12 +0100524 JS_MESSAGE_OBJECT_TYPE,
525
Leon Clarkee46be812010-01-19 14:06:41 +0000526 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 JS_OBJECT_TYPE,
528 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
529 JS_GLOBAL_OBJECT_TYPE,
530 JS_BUILTINS_OBJECT_TYPE,
531 JS_GLOBAL_PROXY_TYPE,
532 JS_ARRAY_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100533
534 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE, FIRST_FUNCTION_CLASS_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000535
536 JS_FUNCTION_TYPE,
537
538 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000539 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000541 INVALID_TYPE = FIRST_TYPE - 1,
542 FIRST_NONSTRING_TYPE = MAP_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100543 FIRST_STRING_TYPE = FIRST_TYPE,
544 LAST_STRING_TYPE = FIRST_NONSTRING_TYPE - 1,
Leon Clarkee46be812010-01-19 14:06:41 +0000545 // Boundaries for testing for an external array.
546 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
547 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_FLOAT_ARRAY_TYPE,
548 // Boundary for promotion to old data space/old pointer space.
549 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000550 // Boundaries for testing the type is a JavaScript "object". Note that
551 // function objects are not counted as objects, even though they are
552 // implemented as such; only values whose typeof is "object" are included.
553 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100554 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE,
555 // RegExp objects have [[Class]] "function" because they are callable.
556 // All types from this type and above are objects with [[Class]] "function".
557 FIRST_FUNCTION_CLASS_TYPE = JS_REGEXP_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000558};
559
560
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100561STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
562STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
563STATIC_CHECK(PROXY_TYPE == Internals::kProxyType);
564
565
Steve Blocka7e24c12009-10-30 11:49:00 +0000566enum CompareResult {
567 LESS = -1,
568 EQUAL = 0,
569 GREATER = 1,
570
571 NOT_EQUAL = GREATER
572};
573
574
575#define DECL_BOOLEAN_ACCESSORS(name) \
576 inline bool name(); \
577 inline void set_##name(bool value); \
578
579
580#define DECL_ACCESSORS(name, type) \
581 inline type* name(); \
582 inline void set_##name(type* value, \
583 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
584
585
586class StringStream;
587class ObjectVisitor;
588
589struct ValueInfo : public Malloced {
590 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
591 InstanceType type;
592 Object* ptr;
593 const char* str;
594 double number;
595};
596
597
598// A template-ized version of the IsXXX functions.
599template <class C> static inline bool Is(Object* obj);
600
Ben Murdochb0fe1622011-05-05 13:52:32 +0100601
John Reck59135872010-11-02 12:39:01 -0700602class MaybeObject BASE_EMBEDDED {
603 public:
604 inline bool IsFailure();
605 inline bool IsRetryAfterGC();
606 inline bool IsOutOfMemory();
607 inline bool IsException();
608 INLINE(bool IsTheHole());
609 inline bool ToObject(Object** obj) {
610 if (IsFailure()) return false;
611 *obj = reinterpret_cast<Object*>(this);
612 return true;
613 }
614 inline Object* ToObjectUnchecked() {
615 ASSERT(!IsFailure());
616 return reinterpret_cast<Object*>(this);
617 }
618 inline Object* ToObjectChecked() {
619 CHECK(!IsFailure());
620 return reinterpret_cast<Object*>(this);
621 }
622
Ben Murdochb0fe1622011-05-05 13:52:32 +0100623#ifdef OBJECT_PRINT
John Reck59135872010-11-02 12:39:01 -0700624 // Prints this object with details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100625 inline void Print() {
626 Print(stdout);
627 };
628 inline void PrintLn() {
629 PrintLn(stdout);
630 }
631 void Print(FILE* out);
632 void PrintLn(FILE* out);
633#endif
634#ifdef DEBUG
John Reck59135872010-11-02 12:39:01 -0700635 // Verifies the object.
636 void Verify();
637#endif
638};
Steve Blocka7e24c12009-10-30 11:49:00 +0000639
Ben Murdochb8e0da22011-05-16 14:20:40 +0100640
641#define OBJECT_TYPE_LIST(V) \
642 V(Smi) \
643 V(HeapObject) \
644 V(Number) \
645
646#define HEAP_OBJECT_TYPE_LIST(V) \
647 V(HeapNumber) \
648 V(String) \
649 V(Symbol) \
650 V(SeqString) \
651 V(ExternalString) \
652 V(ConsString) \
653 V(ExternalTwoByteString) \
654 V(ExternalAsciiString) \
655 V(SeqTwoByteString) \
656 V(SeqAsciiString) \
657 \
658 V(PixelArray) \
659 V(ExternalArray) \
660 V(ExternalByteArray) \
661 V(ExternalUnsignedByteArray) \
662 V(ExternalShortArray) \
663 V(ExternalUnsignedShortArray) \
664 V(ExternalIntArray) \
665 V(ExternalUnsignedIntArray) \
666 V(ExternalFloatArray) \
667 V(ByteArray) \
668 V(JSObject) \
669 V(JSContextExtensionObject) \
670 V(Map) \
671 V(DescriptorArray) \
672 V(DeoptimizationInputData) \
673 V(DeoptimizationOutputData) \
674 V(FixedArray) \
675 V(Context) \
676 V(CatchContext) \
677 V(GlobalContext) \
678 V(JSFunction) \
679 V(Code) \
680 V(Oddball) \
681 V(SharedFunctionInfo) \
682 V(JSValue) \
Steve Block1e0659c2011-05-24 12:43:12 +0100683 V(JSMessageObject) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100684 V(StringWrapper) \
685 V(Proxy) \
686 V(Boolean) \
687 V(JSArray) \
688 V(JSRegExp) \
689 V(HashTable) \
690 V(Dictionary) \
691 V(SymbolTable) \
692 V(JSFunctionResultCache) \
693 V(NormalizedMapCache) \
694 V(CompilationCacheTable) \
695 V(CodeCacheHashTable) \
696 V(MapCache) \
697 V(Primitive) \
698 V(GlobalObject) \
699 V(JSGlobalObject) \
700 V(JSBuiltinsObject) \
701 V(JSGlobalProxy) \
702 V(UndetectableObject) \
703 V(AccessCheckNeeded) \
704 V(JSGlobalPropertyCell) \
705
Steve Blocka7e24c12009-10-30 11:49:00 +0000706// Object is the abstract superclass for all classes in the
707// object hierarchy.
708// Object does not use any virtual functions to avoid the
709// allocation of the C++ vtable.
710// Since Smi and Failure are subclasses of Object no
711// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700712class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000713 public:
714 // Type testing.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100715#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
716 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
717 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
718#undef IS_TYPE_FUNCTION_DECL
Steve Blocka7e24c12009-10-30 11:49:00 +0000719
720 // Returns true if this object is an instance of the specified
721 // function template.
722 inline bool IsInstanceOf(FunctionTemplateInfo* type);
723
724 inline bool IsStruct();
725#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
726 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
727#undef DECLARE_STRUCT_PREDICATE
728
729 // Oddball testing.
730 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000731 INLINE(bool IsNull());
732 INLINE(bool IsTrue());
733 INLINE(bool IsFalse());
Ben Murdoch086aeea2011-05-13 15:57:08 +0100734 inline bool IsArgumentsMarker();
Steve Blocka7e24c12009-10-30 11:49:00 +0000735
736 // Extract the number.
737 inline double Number();
738
739 inline bool HasSpecificClassOf(String* name);
740
John Reck59135872010-11-02 12:39:01 -0700741 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
742 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000743
744 // Convert to a JSObject if needed.
745 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700746 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000747
748 // Converts this to a Smi if possible.
749 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700750 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000751
752 void Lookup(String* name, LookupResult* result);
753
754 // Property access.
John Reck59135872010-11-02 12:39:01 -0700755 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
756 MUST_USE_RESULT inline MaybeObject* GetProperty(
757 String* key,
758 PropertyAttributes* attributes);
759 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
760 Object* receiver,
761 String* key,
762 PropertyAttributes* attributes);
763 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
764 LookupResult* result,
765 String* key,
766 PropertyAttributes* attributes);
767 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
768 Object* structure,
769 String* name,
770 Object* holder);
771 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
772 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000773
John Reck59135872010-11-02 12:39:01 -0700774 inline MaybeObject* GetElement(uint32_t index);
775 // For use when we know that no exception can be thrown.
776 inline Object* GetElementNoExceptionThrown(uint32_t index);
777 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000778
779 // Return the object's prototype (might be Heap::null_value()).
780 Object* GetPrototype();
781
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100782 // Tries to convert an object to an array index. Returns true and sets
783 // the output parameter if it succeeds.
784 inline bool ToArrayIndex(uint32_t* index);
785
Steve Blocka7e24c12009-10-30 11:49:00 +0000786 // Returns true if this is a JSValue containing a string and the index is
787 // < the length of the string. Used to implement [] on strings.
788 inline bool IsStringObjectWithCharacterAt(uint32_t index);
789
790#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000791 // Verify a pointer is a valid object pointer.
792 static void VerifyPointer(Object* p);
793#endif
794
795 // Prints this object without details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100796 inline void ShortPrint() {
797 ShortPrint(stdout);
798 }
799 void ShortPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000800
801 // Prints this object without details to a message accumulator.
802 void ShortPrint(StringStream* accumulator);
803
804 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
805 static Object* cast(Object* value) { return value; }
806
807 // Layout description.
808 static const int kHeaderSize = 0; // Object does not take up any space.
809
810 private:
811 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
812};
813
814
815// Smi represents integer Numbers that can be stored in 31 bits.
816// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000817// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000818// For long smis it has the following format:
819// [32 bit signed int] [31 bits zero padding] 0
820// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000821class Smi: public Object {
822 public:
823 // Returns the integer value.
824 inline int value();
825
826 // Convert a value to a Smi object.
827 static inline Smi* FromInt(int value);
828
829 static inline Smi* FromIntptr(intptr_t value);
830
831 // Returns whether value can be represented in a Smi.
832 static inline bool IsValid(intptr_t value);
833
Steve Blocka7e24c12009-10-30 11:49:00 +0000834 // Casting.
835 static inline Smi* cast(Object* object);
836
837 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100838 inline void SmiPrint() {
839 SmiPrint(stdout);
840 }
841 void SmiPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000842 void SmiPrint(StringStream* accumulator);
843#ifdef DEBUG
844 void SmiVerify();
845#endif
846
Steve Block3ce2e202009-11-05 08:53:23 +0000847 static const int kMinValue = (-1 << (kSmiValueSize - 1));
848 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000849
850 private:
851 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
852};
853
854
855// Failure is used for reporting out of memory situations and
856// propagating exceptions through the runtime system. Failure objects
857// are transient and cannot occur as part of the object graph.
858//
859// Failures are a single word, encoded as follows:
860// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +0100861// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000862// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000863// 7 6 4 32 10
864//
Steve Blocka7e24c12009-10-30 11:49:00 +0000865//
866// The low two bits, 0-1, are the failure tag, 11. The next two bits,
867// 2-3, are a failure type tag 'tt' with possible values:
868// 00 RETRY_AFTER_GC
869// 01 EXCEPTION
870// 10 INTERNAL_ERROR
871// 11 OUT_OF_MEMORY_EXCEPTION
872//
873// The next three bits, 4-6, are an allocation space tag 'sss'. The
874// allocation space tag is 000 for all failure types except
875// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
876// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +0000877
878// Failure type tag info.
879const int kFailureTypeTagSize = 2;
880const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
881
John Reck59135872010-11-02 12:39:01 -0700882class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 public:
884 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
885 enum Type {
886 RETRY_AFTER_GC = 0,
887 EXCEPTION = 1, // Returning this marker tells the real exception
888 // is in Top::pending_exception.
889 INTERNAL_ERROR = 2,
890 OUT_OF_MEMORY_EXCEPTION = 3
891 };
892
893 inline Type type() const;
894
895 // Returns the space that needs to be collected for RetryAfterGC failures.
896 inline AllocationSpace allocation_space() const;
897
Steve Blocka7e24c12009-10-30 11:49:00 +0000898 inline bool IsInternalError() const;
899 inline bool IsOutOfMemoryException() const;
900
Ben Murdochf87a2032010-10-22 12:50:53 +0100901 static inline Failure* RetryAfterGC(AllocationSpace space);
902 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000903 static inline Failure* Exception();
904 static inline Failure* InternalError();
905 static inline Failure* OutOfMemoryException();
906 // Casting.
John Reck59135872010-11-02 12:39:01 -0700907 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000908
909 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100910 inline void FailurePrint() {
911 FailurePrint(stdout);
912 }
913 void FailurePrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000914 void FailurePrint(StringStream* accumulator);
915#ifdef DEBUG
916 void FailureVerify();
917#endif
918
919 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000920 inline intptr_t value() const;
921 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000922
923 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
924};
925
926
927// Heap objects typically have a map pointer in their first word. However,
928// during GC other data (eg, mark bits, forwarding addresses) is sometimes
929// encoded in the first word. The class MapWord is an abstraction of the
930// value in a heap object's first word.
931class MapWord BASE_EMBEDDED {
932 public:
933 // Normal state: the map word contains a map pointer.
934
935 // Create a map word from a map pointer.
936 static inline MapWord FromMap(Map* map);
937
938 // View this map word as a map pointer.
939 inline Map* ToMap();
940
941
942 // Scavenge collection: the map word of live objects in the from space
943 // contains a forwarding address (a heap object pointer in the to space).
944
945 // True if this map word is a forwarding address for a scavenge
946 // collection. Only valid during a scavenge collection (specifically,
947 // when all map words are heap object pointers, ie. not during a full GC).
948 inline bool IsForwardingAddress();
949
950 // Create a map word from a forwarding address.
951 static inline MapWord FromForwardingAddress(HeapObject* object);
952
953 // View this map word as a forwarding address.
954 inline HeapObject* ToForwardingAddress();
955
Steve Blocka7e24c12009-10-30 11:49:00 +0000956 // Marking phase of full collection: the map word of live objects is
957 // marked, and may be marked as overflowed (eg, the object is live, its
958 // children have not been visited, and it does not fit in the marking
959 // stack).
960
961 // True if this map word's mark bit is set.
962 inline bool IsMarked();
963
964 // Return this map word but with its mark bit set.
965 inline void SetMark();
966
967 // Return this map word but with its mark bit cleared.
968 inline void ClearMark();
969
970 // True if this map word's overflow bit is set.
971 inline bool IsOverflowed();
972
973 // Return this map word but with its overflow bit set.
974 inline void SetOverflow();
975
976 // Return this map word but with its overflow bit cleared.
977 inline void ClearOverflow();
978
979
980 // Compacting phase of a full compacting collection: the map word of live
981 // objects contains an encoding of the original map address along with the
982 // forwarding address (represented as an offset from the first live object
983 // in the same page as the (old) object address).
984
985 // Create a map word from a map address and a forwarding address offset.
986 static inline MapWord EncodeAddress(Address map_address, int offset);
987
988 // Return the map address encoded in this map word.
989 inline Address DecodeMapAddress(MapSpace* map_space);
990
991 // Return the forwarding offset encoded in this map word.
992 inline int DecodeOffset();
993
994
995 // During serialization: the map word is used to hold an encoded
996 // address, and possibly a mark bit (set and cleared with SetMark
997 // and ClearMark).
998
999 // Create a map word from an encoded address.
1000 static inline MapWord FromEncodedAddress(Address address);
1001
1002 inline Address ToEncodedAddress();
1003
1004 // Bits used by the marking phase of the garbage collector.
1005 //
1006 // The first word of a heap object is normally a map pointer. The last two
1007 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1008 // mark an object as live and/or overflowed:
1009 // last bit = 0, marked as alive
1010 // second bit = 1, overflowed
1011 // An object is only marked as overflowed when it is marked as live while
1012 // the marking stack is overflowed.
1013 static const int kMarkingBit = 0; // marking bit
1014 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1015 static const int kOverflowBit = 1; // overflow bit
1016 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1017
Leon Clarkee46be812010-01-19 14:06:41 +00001018 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1019 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +00001020 // +-----------------+------------------+-----------------+
1021 // |forwarding offset|page offset of map|page index of map|
1022 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +00001023 // ^ ^ ^
1024 // | | |
1025 // | | kMapPageIndexBits
1026 // | kMapPageOffsetBits
1027 // kForwardingOffsetBits
1028 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1029 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1030#ifdef V8_HOST_ARCH_64_BIT
1031 static const int kMapPageIndexBits = 16;
1032#else
1033 // Use all the 32-bits to encode on a 32-bit platform.
1034 static const int kMapPageIndexBits =
1035 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1036#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001037
1038 static const int kMapPageIndexShift = 0;
1039 static const int kMapPageOffsetShift =
1040 kMapPageIndexShift + kMapPageIndexBits;
1041 static const int kForwardingOffsetShift =
1042 kMapPageOffsetShift + kMapPageOffsetBits;
1043
Leon Clarkee46be812010-01-19 14:06:41 +00001044 // Bit masks covering the different parts the encoding.
1045 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001046 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001047 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001048 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001049 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001050 ~(kMapPageIndexMask | kMapPageOffsetMask);
1051
1052 private:
1053 // HeapObject calls the private constructor and directly reads the value.
1054 friend class HeapObject;
1055
1056 explicit MapWord(uintptr_t value) : value_(value) {}
1057
1058 uintptr_t value_;
1059};
1060
1061
1062// HeapObject is the superclass for all classes describing heap allocated
1063// objects.
1064class HeapObject: public Object {
1065 public:
1066 // [map]: Contains a map which contains the object's reflective
1067 // information.
1068 inline Map* map();
1069 inline void set_map(Map* value);
1070
1071 // During garbage collection, the map word of a heap object does not
1072 // necessarily contain a map pointer.
1073 inline MapWord map_word();
1074 inline void set_map_word(MapWord map_word);
1075
1076 // Converts an address to a HeapObject pointer.
1077 static inline HeapObject* FromAddress(Address address);
1078
1079 // Returns the address of this HeapObject.
1080 inline Address address();
1081
1082 // Iterates over pointers contained in the object (including the Map)
1083 void Iterate(ObjectVisitor* v);
1084
1085 // Iterates over all pointers contained in the object except the
1086 // first map pointer. The object type is given in the first
1087 // parameter. This function does not access the map pointer in the
1088 // object, and so is safe to call while the map pointer is modified.
1089 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1090
Steve Blocka7e24c12009-10-30 11:49:00 +00001091 // Returns the heap object's size in bytes
1092 inline int Size();
1093
1094 // Given a heap object's map pointer, returns the heap size in bytes
1095 // Useful when the map pointer field is used for other purposes.
1096 // GC internal.
1097 inline int SizeFromMap(Map* map);
1098
1099 // Support for the marking heap objects during the marking phase of GC.
1100 // True if the object is marked live.
1101 inline bool IsMarked();
1102
1103 // Mutate this object's map pointer to indicate that the object is live.
1104 inline void SetMark();
1105
1106 // Mutate this object's map pointer to remove the indication that the
1107 // object is live (ie, partially restore the map pointer).
1108 inline void ClearMark();
1109
1110 // True if this object is marked as overflowed. Overflowed objects have
1111 // been reached and marked during marking of the heap, but their children
1112 // have not necessarily been marked and they have not been pushed on the
1113 // marking stack.
1114 inline bool IsOverflowed();
1115
1116 // Mutate this object's map pointer to indicate that the object is
1117 // overflowed.
1118 inline void SetOverflow();
1119
1120 // Mutate this object's map pointer to remove the indication that the
1121 // object is overflowed (ie, partially restore the map pointer).
1122 inline void ClearOverflow();
1123
1124 // Returns the field at offset in obj, as a read/write Object* reference.
1125 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001126 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001127 // during marking GC.
1128 static inline Object** RawField(HeapObject* obj, int offset);
1129
1130 // Casting.
1131 static inline HeapObject* cast(Object* obj);
1132
Leon Clarke4515c472010-02-03 11:58:03 +00001133 // Return the write barrier mode for this. Callers of this function
1134 // must be able to present a reference to an AssertNoAllocation
1135 // object as a sign that they are not going to use this function
1136 // from code that allocates and thus invalidates the returned write
1137 // barrier mode.
1138 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001139
1140 // Dispatched behavior.
1141 void HeapObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001142#ifdef OBJECT_PRINT
1143 inline void HeapObjectPrint() {
1144 HeapObjectPrint(stdout);
1145 }
1146 void HeapObjectPrint(FILE* out);
1147#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001148#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001149 void HeapObjectVerify();
1150 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001151 inline void VerifySmiField(int offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001152#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001153
Ben Murdochb0fe1622011-05-05 13:52:32 +01001154#ifdef OBJECT_PRINT
1155 void PrintHeader(FILE* out, const char* id);
1156#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001157
Ben Murdochb0fe1622011-05-05 13:52:32 +01001158#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 // Verify a pointer is a valid HeapObject pointer that points to object
1160 // areas in the heap.
1161 static void VerifyHeapPointer(Object* p);
1162#endif
1163
1164 // Layout description.
1165 // First field in a heap object is map.
1166 static const int kMapOffset = Object::kHeaderSize;
1167 static const int kHeaderSize = kMapOffset + kPointerSize;
1168
1169 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1170
1171 protected:
1172 // helpers for calling an ObjectVisitor to iterate over pointers in the
1173 // half-open range [start, end) specified as integer offsets
1174 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1175 // as above, for the single element at "offset"
1176 inline void IteratePointer(ObjectVisitor* v, int offset);
1177
Steve Blocka7e24c12009-10-30 11:49:00 +00001178 private:
1179 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1180};
1181
1182
Iain Merrick75681382010-08-19 15:07:18 +01001183#define SLOT_ADDR(obj, offset) \
1184 reinterpret_cast<Object**>((obj)->address() + offset)
1185
1186// This class describes a body of an object of a fixed size
1187// in which all pointer fields are located in the [start_offset, end_offset)
1188// interval.
1189template<int start_offset, int end_offset, int size>
1190class FixedBodyDescriptor {
1191 public:
1192 static const int kStartOffset = start_offset;
1193 static const int kEndOffset = end_offset;
1194 static const int kSize = size;
1195
1196 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1197
1198 template<typename StaticVisitor>
1199 static inline void IterateBody(HeapObject* obj) {
1200 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1201 SLOT_ADDR(obj, end_offset));
1202 }
1203};
1204
1205
1206// This class describes a body of an object of a variable size
1207// in which all pointer fields are located in the [start_offset, object_size)
1208// interval.
1209template<int start_offset>
1210class FlexibleBodyDescriptor {
1211 public:
1212 static const int kStartOffset = start_offset;
1213
1214 static inline void IterateBody(HeapObject* obj,
1215 int object_size,
1216 ObjectVisitor* v);
1217
1218 template<typename StaticVisitor>
1219 static inline void IterateBody(HeapObject* obj, int object_size) {
1220 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1221 SLOT_ADDR(obj, object_size));
1222 }
1223};
1224
1225#undef SLOT_ADDR
1226
1227
Steve Blocka7e24c12009-10-30 11:49:00 +00001228// The HeapNumber class describes heap allocated numbers that cannot be
1229// represented in a Smi (small integer)
1230class HeapNumber: public HeapObject {
1231 public:
1232 // [value]: number value.
1233 inline double value();
1234 inline void set_value(double value);
1235
1236 // Casting.
1237 static inline HeapNumber* cast(Object* obj);
1238
1239 // Dispatched behavior.
1240 Object* HeapNumberToBoolean();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001241 inline void HeapNumberPrint() {
1242 HeapNumberPrint(stdout);
1243 }
1244 void HeapNumberPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001245 void HeapNumberPrint(StringStream* accumulator);
1246#ifdef DEBUG
1247 void HeapNumberVerify();
1248#endif
1249
Steve Block6ded16b2010-05-10 14:33:55 +01001250 inline int get_exponent();
1251 inline int get_sign();
1252
Steve Blocka7e24c12009-10-30 11:49:00 +00001253 // Layout description.
1254 static const int kValueOffset = HeapObject::kHeaderSize;
1255 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1256 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1257 // little endian apart from non-EABI arm which is little endian with big
1258 // endian floating point word ordering!
Steve Block3ce2e202009-11-05 08:53:23 +00001259#if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI)
Steve Blocka7e24c12009-10-30 11:49:00 +00001260 static const int kMantissaOffset = kValueOffset;
1261 static const int kExponentOffset = kValueOffset + 4;
1262#else
1263 static const int kMantissaOffset = kValueOffset + 4;
1264 static const int kExponentOffset = kValueOffset;
1265# define BIG_ENDIAN_FLOATING_POINT 1
1266#endif
1267 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001268 static const uint32_t kSignMask = 0x80000000u;
1269 static const uint32_t kExponentMask = 0x7ff00000u;
1270 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001271 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001272 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001273 static const int kExponentBias = 1023;
1274 static const int kExponentShift = 20;
1275 static const int kMantissaBitsInTopWord = 20;
1276 static const int kNonMantissaBitsInTopWord = 12;
1277
1278 private:
1279 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1280};
1281
1282
1283// The JSObject describes real heap allocated JavaScript objects with
1284// properties.
1285// Note that the map of JSObject changes during execution to enable inline
1286// caching.
1287class JSObject: public HeapObject {
1288 public:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001289 enum DeleteMode {
1290 NORMAL_DELETION,
1291 STRICT_DELETION,
1292 FORCE_DELETION
1293 };
1294
Steve Blocka7e24c12009-10-30 11:49:00 +00001295 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001296 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001297 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001298 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001299 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001300 PIXEL_ELEMENTS,
1301 EXTERNAL_BYTE_ELEMENTS,
1302 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1303 EXTERNAL_SHORT_ELEMENTS,
1304 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1305 EXTERNAL_INT_ELEMENTS,
1306 EXTERNAL_UNSIGNED_INT_ELEMENTS,
1307 EXTERNAL_FLOAT_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001308 };
1309
1310 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001311 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001312 // slow case.
1313 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1314 inline void initialize_properties();
1315 inline bool HasFastProperties();
1316 inline StringDictionary* property_dictionary(); // Gets slow properties.
1317
1318 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001319 //
1320 // Elements can be in two general modes: fast and slow. Each mode
1321 // corrensponds to a set of object representations of elements that
1322 // have something in common.
1323 //
1324 // In the fast mode elements is a FixedArray and so each element can
1325 // be quickly accessed. This fact is used in the generated code. The
1326 // elements array can have one of the two maps in this mode:
1327 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1328 // arrays). In the latter case the elements array may be shared by a
1329 // few objects and so before writing to any element the array must
1330 // be copied. Use EnsureWritableFastElements in this case.
1331 //
1332 // In the slow mode elements is either a NumberDictionary or a
1333 // PixelArray or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001334 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001335 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001336 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001337 inline ElementsKind GetElementsKind();
1338 inline bool HasFastElements();
1339 inline bool HasDictionaryElements();
1340 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001341 inline bool HasExternalArrayElements();
1342 inline bool HasExternalByteElements();
1343 inline bool HasExternalUnsignedByteElements();
1344 inline bool HasExternalShortElements();
1345 inline bool HasExternalUnsignedShortElements();
1346 inline bool HasExternalIntElements();
1347 inline bool HasExternalUnsignedIntElements();
1348 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001349 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001350 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001351 // Requires: this->HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001352 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001353
1354 // Collects elements starting at index 0.
1355 // Undefined values are placed after non-undefined values.
1356 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001357 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001358 // As PrepareElementsForSort, but only on objects where elements is
1359 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001360 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001361
John Reck59135872010-11-02 12:39:01 -07001362 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1363 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001364 PropertyAttributes attributes,
1365 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001366 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1367 String* key,
1368 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001369 PropertyAttributes attributes,
1370 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001371 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1372 LookupResult* result,
1373 String* name,
Ben Murdoch086aeea2011-05-13 15:57:08 +01001374 Object* value,
1375 bool check_prototype);
John Reck59135872010-11-02 12:39:01 -07001376 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(Object* structure,
1377 String* name,
1378 Object* value,
1379 JSObject* holder);
1380 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1381 Object* value);
1382 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1383 String* name,
1384 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001385 PropertyAttributes attributes,
1386 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001387 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1388 String* name,
1389 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001390 PropertyAttributes attributes,
1391 StrictModeFlag strict_mode);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001392 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
John Reck59135872010-11-02 12:39:01 -07001393 String* key,
1394 Object* value,
1395 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001396
1397 // Retrieve a value in a normalized object given a lookup result.
1398 // Handles the special representation of JS global objects.
1399 Object* GetNormalizedProperty(LookupResult* result);
1400
1401 // Sets the property value in a normalized object given a lookup result.
1402 // Handles the special representation of JS global objects.
1403 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1404
1405 // Sets the property value in a normalized object given (key, value, details).
1406 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001407 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1408 Object* value,
1409 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001410
1411 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001412 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1413 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001414
Steve Blocka7e24c12009-10-30 11:49:00 +00001415 // Returns the class name ([[Class]] property in the specification).
1416 String* class_name();
1417
1418 // Returns the constructor name (the name (possibly, inferred name) of the
1419 // function that was used to instantiate the object).
1420 String* constructor_name();
1421
1422 // Retrieve interceptors.
1423 InterceptorInfo* GetNamedInterceptor();
1424 InterceptorInfo* GetIndexedInterceptor();
1425
1426 inline PropertyAttributes GetPropertyAttribute(String* name);
1427 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1428 String* name);
1429 PropertyAttributes GetLocalPropertyAttribute(String* name);
1430
John Reck59135872010-11-02 12:39:01 -07001431 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1432 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001433 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001434 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001435 Object* LookupAccessor(String* name, bool is_getter);
1436
John Reck59135872010-11-02 12:39:01 -07001437 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001438
Steve Blocka7e24c12009-10-30 11:49:00 +00001439 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001440 MaybeObject* GetPropertyWithFailedAccessCheck(
1441 Object* receiver,
1442 LookupResult* result,
1443 String* name,
1444 PropertyAttributes* attributes);
1445 MaybeObject* GetPropertyWithInterceptor(
1446 JSObject* receiver,
1447 String* name,
1448 PropertyAttributes* attributes);
1449 MaybeObject* GetPropertyPostInterceptor(
1450 JSObject* receiver,
1451 String* name,
1452 PropertyAttributes* attributes);
1453 MaybeObject* GetLocalPropertyPostInterceptor(JSObject* receiver,
1454 String* name,
1455 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001456
1457 // Returns true if this is an instance of an api function and has
1458 // been modified since it was created. May give false positives.
1459 bool IsDirty();
1460
1461 bool HasProperty(String* name) {
1462 return GetPropertyAttribute(name) != ABSENT;
1463 }
1464
1465 // Can cause a GC if it hits an interceptor.
1466 bool HasLocalProperty(String* name) {
1467 return GetLocalPropertyAttribute(name) != ABSENT;
1468 }
1469
Steve Blockd0582a62009-12-15 09:54:21 +00001470 // If the receiver is a JSGlobalProxy this method will return its prototype,
1471 // otherwise the result is the receiver itself.
1472 inline Object* BypassGlobalProxy();
1473
1474 // Accessors for hidden properties object.
1475 //
1476 // Hidden properties are not local properties of the object itself.
1477 // Instead they are stored on an auxiliary JSObject stored as a local
1478 // property with a special name Heap::hidden_symbol(). But if the
1479 // receiver is a JSGlobalProxy then the auxiliary object is a property
1480 // of its prototype.
1481 //
1482 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1483 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1484 // holder.
1485 //
1486 // These accessors do not touch interceptors or accessors.
1487 inline bool HasHiddenPropertiesObject();
1488 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001489 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1490 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001491
John Reck59135872010-11-02 12:39:01 -07001492 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1493 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001494
1495 // Tests for the fast common case for property enumeration.
1496 bool IsSimpleEnum();
1497
1498 // Do we want to keep the elements in fast case when increasing the
1499 // capacity?
1500 bool ShouldConvertToSlowElements(int new_capacity);
1501 // Returns true if the backing storage for the slow-case elements of
1502 // this object takes up nearly as much space as a fast-case backing
1503 // storage would. In that case the JSObject should have fast
1504 // elements.
1505 bool ShouldConvertToFastElements();
1506
1507 // Return the object's prototype (might be Heap::null_value()).
1508 inline Object* GetPrototype();
1509
Andrei Popescu402d9372010-02-26 13:31:12 +00001510 // Set the object's prototype (only JSObject and null are allowed).
John Reck59135872010-11-02 12:39:01 -07001511 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1512 bool skip_hidden_prototypes);
Andrei Popescu402d9372010-02-26 13:31:12 +00001513
Steve Blocka7e24c12009-10-30 11:49:00 +00001514 // Tells whether the index'th element is present.
1515 inline bool HasElement(uint32_t index);
1516 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001517
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001518 // Computes the new capacity when expanding the elements of a JSObject.
1519 static int NewElementsCapacity(int old_capacity) {
1520 // (old_capacity + 50%) + 16
1521 return old_capacity + (old_capacity >> 1) + 16;
1522 }
1523
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001524 // Tells whether the index'th element is present and how it is stored.
1525 enum LocalElementType {
1526 // There is no element with given index.
1527 UNDEFINED_ELEMENT,
1528
1529 // Element with given index is handled by interceptor.
1530 INTERCEPTED_ELEMENT,
1531
1532 // Element with given index is character in string.
1533 STRING_CHARACTER_ELEMENT,
1534
1535 // Element with given index is stored in fast backing store.
1536 FAST_ELEMENT,
1537
1538 // Element with given index is stored in slow backing store.
1539 DICTIONARY_ELEMENT
1540 };
1541
1542 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001543
1544 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1545 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1546
Steve Block9fac8402011-05-12 15:51:54 +01001547 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1548 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001549 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001550 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001551
1552 // Set the index'th array element.
1553 // A Failure object is returned if GC is needed.
Steve Block9fac8402011-05-12 15:51:54 +01001554 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1555 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001556 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001557 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001558
1559 // Returns the index'th element.
1560 // The undefined object if index is out of bounds.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001561 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
1562 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
1563
1564 // Get external element value at index if there is one and undefined
1565 // otherwise. Can return a failure if allocation of a heap number
1566 // failed.
1567 MaybeObject* GetExternalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001568
John Reck59135872010-11-02 12:39:01 -07001569 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1570 int length);
1571 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001572
1573 // Lookup interceptors are used for handling properties controlled by host
1574 // objects.
1575 inline bool HasNamedInterceptor();
1576 inline bool HasIndexedInterceptor();
1577
1578 // Support functions for v8 api (needed for correct interceptor behavior).
1579 bool HasRealNamedProperty(String* key);
1580 bool HasRealElementProperty(uint32_t index);
1581 bool HasRealNamedCallbackProperty(String* key);
1582
1583 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001584 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001585
1586 // Get the header size for a JSObject. Used to compute the index of
1587 // internal fields as well as the number of internal fields.
1588 inline int GetHeaderSize();
1589
1590 inline int GetInternalFieldCount();
1591 inline Object* GetInternalField(int index);
1592 inline void SetInternalField(int index, Object* value);
1593
1594 // Lookup a property. If found, the result is valid and has
1595 // detailed information.
1596 void LocalLookup(String* name, LookupResult* result);
1597 void Lookup(String* name, LookupResult* result);
1598
1599 // The following lookup functions skip interceptors.
1600 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1601 void LookupRealNamedProperty(String* name, LookupResult* result);
1602 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1603 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Steve Block1e0659c2011-05-24 12:43:12 +01001604 MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
1605 uint32_t index, Object* value, bool* found);
Steve Blocka7e24c12009-10-30 11:49:00 +00001606 void LookupCallback(String* name, LookupResult* result);
1607
1608 // Returns the number of properties on this object filtering out properties
1609 // with the specified attributes (ignoring interceptors).
1610 int NumberOfLocalProperties(PropertyAttributes filter);
1611 // Returns the number of enumerable properties (ignoring interceptors).
1612 int NumberOfEnumProperties();
1613 // Fill in details for properties into storage starting at the specified
1614 // index.
1615 void GetLocalPropertyNames(FixedArray* storage, int index);
1616
1617 // Returns the number of properties on this object filtering out properties
1618 // with the specified attributes (ignoring interceptors).
1619 int NumberOfLocalElements(PropertyAttributes filter);
1620 // Returns the number of enumerable elements (ignoring interceptors).
1621 int NumberOfEnumElements();
1622 // Returns the number of elements on this object filtering out elements
1623 // with the specified attributes (ignoring interceptors).
1624 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1625 // Count and fill in the enumerable elements into storage.
1626 // (storage->length() == NumberOfEnumElements()).
1627 // If storage is NULL, will count the elements without adding
1628 // them to any storage.
1629 // Returns the number of enumerable elements.
1630 int GetEnumElementKeys(FixedArray* storage);
1631
1632 // Add a property to a fast-case object using a map transition to
1633 // new_map.
John Reck59135872010-11-02 12:39:01 -07001634 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1635 String* name,
1636 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001637
1638 // Add a constant function property to a fast-case object.
1639 // This leaves a CONSTANT_TRANSITION in the old map, and
1640 // if it is called on a second object with this map, a
1641 // normal property is added instead, with a map transition.
1642 // This avoids the creation of many maps with the same constant
1643 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001644 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1645 String* name,
1646 JSFunction* function,
1647 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001648
John Reck59135872010-11-02 12:39:01 -07001649 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1650 String* name,
1651 Object* value,
1652 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001653
1654 // Converts a descriptor of any other type to a real field,
1655 // backed by the properties array. Descriptors of visible
1656 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1657 // Converts the descriptor on the original object's map to a
1658 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001659 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001660 String* name,
1661 Object* new_value,
1662 PropertyAttributes attributes);
1663
1664 // Converts a descriptor of any other type to a real field,
1665 // backed by the properties array. Descriptors of visible
1666 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001667 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1668 String* name,
1669 Object* new_value,
1670 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001671
1672 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001673 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1674 Object* value,
1675 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001676
1677 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001678 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1679 Object* value,
1680 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001681
1682 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001683 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1684 Object* value,
1685 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001686
1687 // Convert the object to use the canonical dictionary
1688 // representation. If the object is expected to have additional properties
1689 // added this number can be indicated to have the backing store allocated to
1690 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001691 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1692 PropertyNormalizationMode mode,
1693 int expected_additional_properties);
1694 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001695
John Reck59135872010-11-02 12:39:01 -07001696 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001697
Steve Blocka7e24c12009-10-30 11:49:00 +00001698 // Transform slow named properties to fast variants.
1699 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001700 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1701 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001702
1703 // Access fast-case object properties at index.
1704 inline Object* FastPropertyAt(int index);
1705 inline Object* FastPropertyAtPut(int index, Object* value);
1706
1707 // Access to in object properties.
1708 inline Object* InObjectPropertyAt(int index);
1709 inline Object* InObjectPropertyAtPut(int index,
1710 Object* value,
1711 WriteBarrierMode mode
1712 = UPDATE_WRITE_BARRIER);
1713
1714 // initializes the body after properties slot, properties slot is
1715 // initialized by set_properties
1716 // Note: this call does not update write barrier, it is caller's
1717 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001718 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001719
1720 // Check whether this object references another object
1721 bool ReferencesObject(Object* obj);
1722
1723 // Casting.
1724 static inline JSObject* cast(Object* obj);
1725
Steve Block8defd9f2010-07-08 12:39:36 +01001726 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001727 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001728
1729
Steve Blocka7e24c12009-10-30 11:49:00 +00001730 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001731 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001732#ifdef OBJECT_PRINT
1733 inline void JSObjectPrint() {
1734 JSObjectPrint(stdout);
1735 }
1736 void JSObjectPrint(FILE* out);
1737#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001738#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001739 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001740#endif
1741#ifdef OBJECT_PRINT
1742 inline void PrintProperties() {
1743 PrintProperties(stdout);
1744 }
1745 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001746
Ben Murdochb0fe1622011-05-05 13:52:32 +01001747 inline void PrintElements() {
1748 PrintElements(stdout);
1749 }
1750 void PrintElements(FILE* out);
1751#endif
1752
1753#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001754 // Structure for collecting spill information about JSObjects.
1755 class SpillInformation {
1756 public:
1757 void Clear();
1758 void Print();
1759 int number_of_objects_;
1760 int number_of_objects_with_fast_properties_;
1761 int number_of_objects_with_fast_elements_;
1762 int number_of_fast_used_fields_;
1763 int number_of_fast_unused_fields_;
1764 int number_of_slow_used_properties_;
1765 int number_of_slow_unused_properties_;
1766 int number_of_fast_used_elements_;
1767 int number_of_fast_unused_elements_;
1768 int number_of_slow_used_elements_;
1769 int number_of_slow_unused_elements_;
1770 };
1771
1772 void IncrementSpillStatistics(SpillInformation* info);
1773#endif
1774 Object* SlowReverseLookup(Object* value);
1775
Steve Block8defd9f2010-07-08 12:39:36 +01001776 // Maximal number of fast properties for the JSObject. Used to
1777 // restrict the number of map transitions to avoid an explosion in
1778 // the number of maps for objects used as dictionaries.
1779 inline int MaxFastProperties();
1780
Leon Clarkee46be812010-01-19 14:06:41 +00001781 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1782 // Also maximal value of JSArray's length property.
1783 static const uint32_t kMaxElementCount = 0xffffffffu;
1784
Steve Blocka7e24c12009-10-30 11:49:00 +00001785 static const uint32_t kMaxGap = 1024;
1786 static const int kMaxFastElementsLength = 5000;
1787 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001788 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00001789 static const int kMaxInstanceSize = 255 * kPointerSize;
1790 // When extending the backing storage for property values, we increase
1791 // its size by more than the 1 entry necessary, so sequentially adding fields
1792 // to the same object requires fewer allocations and copies.
1793 static const int kFieldsAdded = 3;
1794
1795 // Layout description.
1796 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1797 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1798 static const int kHeaderSize = kElementsOffset + kPointerSize;
1799
1800 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1801
Iain Merrick75681382010-08-19 15:07:18 +01001802 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1803 public:
1804 static inline int SizeOf(Map* map, HeapObject* object);
1805 };
1806
Steve Blocka7e24c12009-10-30 11:49:00 +00001807 private:
John Reck59135872010-11-02 12:39:01 -07001808 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1809 Object* structure,
1810 uint32_t index,
1811 Object* holder);
1812 MaybeObject* SetElementWithCallback(Object* structure,
1813 uint32_t index,
1814 Object* value,
1815 JSObject* holder);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001816 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
1817 uint32_t index,
1818 Object* value,
1819 StrictModeFlag strict_mode,
1820 bool check_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01001821 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
1822 uint32_t index,
1823 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001824 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001825 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00001826
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001827 MaybeObject* GetElementPostInterceptor(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001828
John Reck59135872010-11-02 12:39:01 -07001829 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1830 DeleteMode mode);
1831 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001832
John Reck59135872010-11-02 12:39:01 -07001833 MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1834 DeleteMode mode);
1835 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001836
1837 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1838 String* name,
1839 bool continue_search);
1840 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1841 String* name,
1842 bool continue_search);
1843 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1844 Object* receiver,
1845 LookupResult* result,
1846 String* name,
1847 bool continue_search);
1848 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1849 LookupResult* result,
1850 String* name,
1851 bool continue_search);
1852
1853 // Returns true if most of the elements backing storage is used.
1854 bool HasDenseElements();
1855
Leon Clarkef7060e22010-06-03 12:02:55 +01001856 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07001857 MUST_USE_RESULT MaybeObject* SetElementCallback(
1858 uint32_t index,
1859 Object* structure,
1860 PropertyAttributes attributes);
1861 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1862 String* name,
1863 Object* structure,
1864 PropertyAttributes attributes);
1865 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
1866 String* name,
1867 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001868
1869 void LookupInDescriptor(String* name, LookupResult* result);
1870
1871 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1872};
1873
1874
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001875// FixedArray describes fixed-sized arrays with element type Object*.
1876class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001877 public:
1878 // [length]: length of the array.
1879 inline int length();
1880 inline void set_length(int value);
1881
Steve Blocka7e24c12009-10-30 11:49:00 +00001882 // Setter and getter for elements.
1883 inline Object* get(int index);
1884 // Setter that uses write barrier.
1885 inline void set(int index, Object* value);
1886
1887 // Setter that doesn't need write barrier).
1888 inline void set(int index, Smi* value);
1889 // Setter with explicit barrier mode.
1890 inline void set(int index, Object* value, WriteBarrierMode mode);
1891
1892 // Setters for frequently used oddballs located in old space.
1893 inline void set_undefined(int index);
1894 inline void set_null(int index);
1895 inline void set_the_hole(int index);
1896
Iain Merrick75681382010-08-19 15:07:18 +01001897 // Setters with less debug checks for the GC to use.
1898 inline void set_unchecked(int index, Smi* value);
1899 inline void set_null_unchecked(int index);
Ben Murdochf87a2032010-10-22 12:50:53 +01001900 inline void set_unchecked(int index, Object* value, WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01001901
Steve Block6ded16b2010-05-10 14:33:55 +01001902 // Gives access to raw memory which stores the array's data.
1903 inline Object** data_start();
1904
Steve Blocka7e24c12009-10-30 11:49:00 +00001905 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07001906 MUST_USE_RESULT inline MaybeObject* Copy();
1907 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001908
1909 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07001910 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00001911
1912 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07001913 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00001914
1915 // Copy a sub array from the receiver to dest.
1916 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1917
1918 // Garbage collection support.
1919 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1920
1921 // Code Generation support.
1922 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1923
1924 // Casting.
1925 static inline FixedArray* cast(Object* obj);
1926
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001927 // Layout description.
1928 // Length is smi tagged when it is stored.
1929 static const int kLengthOffset = HeapObject::kHeaderSize;
1930 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001931
1932 // Maximal allowed size, in bytes, of a single FixedArray.
1933 // Prevents overflowing size computations, as well as extreme memory
1934 // consumption.
1935 static const int kMaxSize = 512 * MB;
1936 // Maximally allowed length of a FixedArray.
1937 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001938
1939 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001940#ifdef OBJECT_PRINT
1941 inline void FixedArrayPrint() {
1942 FixedArrayPrint(stdout);
1943 }
1944 void FixedArrayPrint(FILE* out);
1945#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001946#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001947 void FixedArrayVerify();
1948 // Checks if two FixedArrays have identical contents.
1949 bool IsEqualTo(FixedArray* other);
1950#endif
1951
1952 // Swap two elements in a pair of arrays. If this array and the
1953 // numbers array are the same object, the elements are only swapped
1954 // once.
1955 void SwapPairs(FixedArray* numbers, int i, int j);
1956
1957 // Sort prefix of this array and the numbers array as pairs wrt. the
1958 // numbers. If the numbers array and the this array are the same
1959 // object, the prefix of this array is sorted.
1960 void SortPairs(FixedArray* numbers, uint32_t len);
1961
Iain Merrick75681382010-08-19 15:07:18 +01001962 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
1963 public:
1964 static inline int SizeOf(Map* map, HeapObject* object) {
1965 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
1966 }
1967 };
1968
Steve Blocka7e24c12009-10-30 11:49:00 +00001969 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001970 // Set operation on FixedArray without using write barriers. Can
1971 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001972 static inline void fast_set(FixedArray* array, int index, Object* value);
1973
1974 private:
1975 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1976};
1977
1978
1979// DescriptorArrays are fixed arrays used to hold instance descriptors.
1980// The format of the these objects is:
1981// [0]: point to a fixed array with (value, detail) pairs.
1982// [1]: next enumeration index (Smi), or pointer to small fixed array:
1983// [0]: next enumeration index (Smi)
1984// [1]: pointer to fixed array with enum cache
1985// [2]: first key
1986// [length() - 1]: last key
1987//
1988class DescriptorArray: public FixedArray {
1989 public:
1990 // Is this the singleton empty_descriptor_array?
1991 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001992
Steve Blocka7e24c12009-10-30 11:49:00 +00001993 // Returns the number of descriptors in the array.
1994 int number_of_descriptors() {
1995 return IsEmpty() ? 0 : length() - kFirstIndex;
1996 }
1997
1998 int NextEnumerationIndex() {
1999 if (IsEmpty()) return PropertyDetails::kInitialIndex;
2000 Object* obj = get(kEnumerationIndexIndex);
2001 if (obj->IsSmi()) {
2002 return Smi::cast(obj)->value();
2003 } else {
2004 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2005 return Smi::cast(index)->value();
2006 }
2007 }
2008
2009 // Set next enumeration index and flush any enum cache.
2010 void SetNextEnumerationIndex(int value) {
2011 if (!IsEmpty()) {
2012 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2013 }
2014 }
2015 bool HasEnumCache() {
2016 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
2017 }
2018
2019 Object* GetEnumCache() {
2020 ASSERT(HasEnumCache());
2021 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2022 return bridge->get(kEnumCacheBridgeCacheIndex);
2023 }
2024
2025 // Initialize or change the enum cache,
2026 // using the supplied storage for the small "bridge".
2027 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2028
2029 // Accessors for fetching instance descriptor at descriptor number.
2030 inline String* GetKey(int descriptor_number);
2031 inline Object* GetValue(int descriptor_number);
2032 inline Smi* GetDetails(int descriptor_number);
2033 inline PropertyType GetType(int descriptor_number);
2034 inline int GetFieldIndex(int descriptor_number);
2035 inline JSFunction* GetConstantFunction(int descriptor_number);
2036 inline Object* GetCallbacksObject(int descriptor_number);
2037 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2038 inline bool IsProperty(int descriptor_number);
2039 inline bool IsTransition(int descriptor_number);
2040 inline bool IsNullDescriptor(int descriptor_number);
2041 inline bool IsDontEnum(int descriptor_number);
2042
2043 // Accessor for complete descriptor.
2044 inline void Get(int descriptor_number, Descriptor* desc);
2045 inline void Set(int descriptor_number, Descriptor* desc);
2046
2047 // Transfer complete descriptor from another descriptor array to
2048 // this one.
2049 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2050
2051 // Copy the descriptor array, insert a new descriptor and optionally
2052 // remove map transitions. If the descriptor is already present, it is
2053 // replaced. If a replaced descriptor is a real property (not a transition
2054 // or null), its enumeration index is kept as is.
2055 // If adding a real property, map transitions must be removed. If adding
2056 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002057 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2058 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002059
2060 // Remove all transitions. Return a copy of the array with all transitions
2061 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002062 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002063
2064 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002065 // Does not check for duplicates.
2066 void SortUnchecked();
2067
2068 // Sort the instance descriptors by the hash codes of their keys.
2069 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00002070 void Sort();
2071
2072 // Search the instance descriptors for given name.
2073 inline int Search(String* name);
2074
Iain Merrick75681382010-08-19 15:07:18 +01002075 // As the above, but uses DescriptorLookupCache and updates it when
2076 // necessary.
2077 inline int SearchWithCache(String* name);
2078
Steve Blocka7e24c12009-10-30 11:49:00 +00002079 // Tells whether the name is present int the array.
2080 bool Contains(String* name) { return kNotFound != Search(name); }
2081
2082 // Perform a binary search in the instance descriptors represented
2083 // by this fixed array. low and high are descriptor indices. If there
2084 // are three instance descriptors in this array it should be called
2085 // with low=0 and high=2.
2086 int BinarySearch(String* name, int low, int high);
2087
2088 // Perform a linear search in the instance descriptors represented
2089 // by this fixed array. len is the number of descriptor indices that are
2090 // valid. Does not require the descriptors to be sorted.
2091 int LinearSearch(String* name, int len);
2092
2093 // Allocates a DescriptorArray, but returns the singleton
2094 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002095 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002096
2097 // Casting.
2098 static inline DescriptorArray* cast(Object* obj);
2099
2100 // Constant for denoting key was not found.
2101 static const int kNotFound = -1;
2102
2103 static const int kContentArrayIndex = 0;
2104 static const int kEnumerationIndexIndex = 1;
2105 static const int kFirstIndex = 2;
2106
2107 // The length of the "bridge" to the enum cache.
2108 static const int kEnumCacheBridgeLength = 2;
2109 static const int kEnumCacheBridgeEnumIndex = 0;
2110 static const int kEnumCacheBridgeCacheIndex = 1;
2111
2112 // Layout description.
2113 static const int kContentArrayOffset = FixedArray::kHeaderSize;
2114 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2115 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2116
2117 // Layout description for the bridge array.
2118 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2119 static const int kEnumCacheBridgeCacheOffset =
2120 kEnumCacheBridgeEnumOffset + kPointerSize;
2121
Ben Murdochb0fe1622011-05-05 13:52:32 +01002122#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002123 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002124 inline void PrintDescriptors() {
2125 PrintDescriptors(stdout);
2126 }
2127 void PrintDescriptors(FILE* out);
2128#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002129
Ben Murdochb0fe1622011-05-05 13:52:32 +01002130#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002131 // Is the descriptor array sorted and without duplicates?
2132 bool IsSortedNoDuplicates();
2133
2134 // Are two DescriptorArrays equal?
2135 bool IsEqualTo(DescriptorArray* other);
2136#endif
2137
2138 // The maximum number of descriptors we want in a descriptor array (should
2139 // fit in a page).
2140 static const int kMaxNumberOfDescriptors = 1024 + 512;
2141
2142 private:
2143 // Conversion from descriptor number to array indices.
2144 static int ToKeyIndex(int descriptor_number) {
2145 return descriptor_number+kFirstIndex;
2146 }
Leon Clarkee46be812010-01-19 14:06:41 +00002147
2148 static int ToDetailsIndex(int descriptor_number) {
2149 return (descriptor_number << 1) + 1;
2150 }
2151
Steve Blocka7e24c12009-10-30 11:49:00 +00002152 static int ToValueIndex(int descriptor_number) {
2153 return descriptor_number << 1;
2154 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002155
2156 bool is_null_descriptor(int descriptor_number) {
2157 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2158 NULL_DESCRIPTOR;
2159 }
2160 // Swap operation on FixedArray without using write barriers.
2161 static inline void fast_swap(FixedArray* array, int first, int second);
2162
2163 // Swap descriptor first and second.
2164 inline void Swap(int first, int second);
2165
2166 FixedArray* GetContentArray() {
2167 return FixedArray::cast(get(kContentArrayIndex));
2168 }
2169 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2170};
2171
2172
2173// HashTable is a subclass of FixedArray that implements a hash table
2174// that uses open addressing and quadratic probing.
2175//
2176// In order for the quadratic probing to work, elements that have not
2177// yet been used and elements that have been deleted are
2178// distinguished. Probing continues when deleted elements are
2179// encountered and stops when unused elements are encountered.
2180//
2181// - Elements with key == undefined have not been used yet.
2182// - Elements with key == null have been deleted.
2183//
2184// The hash table class is parameterized with a Shape and a Key.
2185// Shape must be a class with the following interface:
2186// class ExampleShape {
2187// public:
2188// // Tells whether key matches other.
2189// static bool IsMatch(Key key, Object* other);
2190// // Returns the hash value for key.
2191// static uint32_t Hash(Key key);
2192// // Returns the hash value for object.
2193// static uint32_t HashForObject(Key key, Object* object);
2194// // Convert key to an object.
2195// static inline Object* AsObject(Key key);
2196// // The prefix size indicates number of elements in the beginning
2197// // of the backing storage.
2198// static const int kPrefixSize = ..;
2199// // The Element size indicates number of elements per entry.
2200// static const int kEntrySize = ..;
2201// };
Steve Block3ce2e202009-11-05 08:53:23 +00002202// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002203// beginning of the backing storage that can be used for non-element
2204// information by subclasses.
2205
2206template<typename Shape, typename Key>
2207class HashTable: public FixedArray {
2208 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002209 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002210 int NumberOfElements() {
2211 return Smi::cast(get(kNumberOfElementsIndex))->value();
2212 }
2213
Leon Clarkee46be812010-01-19 14:06:41 +00002214 // Returns the number of deleted elements in the hash table.
2215 int NumberOfDeletedElements() {
2216 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2217 }
2218
Steve Block3ce2e202009-11-05 08:53:23 +00002219 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002220 int Capacity() {
2221 return Smi::cast(get(kCapacityIndex))->value();
2222 }
2223
2224 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002225 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002226 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2227
2228 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002229 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002230 void ElementRemoved() {
2231 SetNumberOfElements(NumberOfElements() - 1);
2232 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2233 }
2234 void ElementsRemoved(int n) {
2235 SetNumberOfElements(NumberOfElements() - n);
2236 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2237 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002238
Steve Block3ce2e202009-11-05 08:53:23 +00002239 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002240 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002241 int at_least_space_for,
2242 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002243
2244 // Returns the key at entry.
2245 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2246
2247 // Tells whether k is a real key. Null and undefined are not allowed
2248 // as keys and can be used to indicate missing or deleted elements.
2249 bool IsKey(Object* k) {
2250 return !k->IsNull() && !k->IsUndefined();
2251 }
2252
2253 // Garbage collection support.
2254 void IteratePrefix(ObjectVisitor* visitor);
2255 void IterateElements(ObjectVisitor* visitor);
2256
2257 // Casting.
2258 static inline HashTable* cast(Object* obj);
2259
2260 // Compute the probe offset (quadratic probing).
2261 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2262 return (n + n * n) >> 1;
2263 }
2264
2265 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002266 static const int kNumberOfDeletedElementsIndex = 1;
2267 static const int kCapacityIndex = 2;
2268 static const int kPrefixStartIndex = 3;
2269 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002270 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002271 static const int kEntrySize = Shape::kEntrySize;
2272 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002273 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002274 static const int kCapacityOffset =
2275 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002276
2277 // Constant used for denoting a absent entry.
2278 static const int kNotFound = -1;
2279
Leon Clarkee46be812010-01-19 14:06:41 +00002280 // Maximal capacity of HashTable. Based on maximal length of underlying
2281 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2282 // cannot overflow.
2283 static const int kMaxCapacity =
2284 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2285
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002286 // Find entry for key otherwise return kNotFound.
Steve Blocka7e24c12009-10-30 11:49:00 +00002287 int FindEntry(Key key);
2288
2289 protected:
2290
2291 // Find the entry at which to insert element with the given key that
2292 // has the given hash value.
2293 uint32_t FindInsertionEntry(uint32_t hash);
2294
2295 // Returns the index for an entry (of the key)
2296 static inline int EntryToIndex(int entry) {
2297 return (entry * kEntrySize) + kElementsStartIndex;
2298 }
2299
Steve Block3ce2e202009-11-05 08:53:23 +00002300 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002301 void SetNumberOfElements(int nof) {
2302 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2303 }
2304
Leon Clarkee46be812010-01-19 14:06:41 +00002305 // Update the number of deleted elements in the hash table.
2306 void SetNumberOfDeletedElements(int nod) {
2307 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2308 }
2309
Steve Blocka7e24c12009-10-30 11:49:00 +00002310 // Sets the capacity of the hash table.
2311 void SetCapacity(int capacity) {
2312 // To scale a computed hash code to fit within the hash table, we
2313 // use bit-wise AND with a mask, so the capacity must be positive
2314 // and non-zero.
2315 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002316 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002317 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2318 }
2319
2320
2321 // Returns probe entry.
2322 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2323 ASSERT(IsPowerOf2(size));
2324 return (hash + GetProbeOffset(number)) & (size - 1);
2325 }
2326
Leon Clarkee46be812010-01-19 14:06:41 +00002327 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2328 return hash & (size - 1);
2329 }
2330
2331 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2332 return (last + number) & (size - 1);
2333 }
2334
Steve Blocka7e24c12009-10-30 11:49:00 +00002335 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002336 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002337};
2338
2339
2340
2341// HashTableKey is an abstract superclass for virtual key behavior.
2342class HashTableKey {
2343 public:
2344 // Returns whether the other object matches this key.
2345 virtual bool IsMatch(Object* other) = 0;
2346 // Returns the hash value for this key.
2347 virtual uint32_t Hash() = 0;
2348 // Returns the hash value for object.
2349 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002350 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002351 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002352 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002353 // Required.
2354 virtual ~HashTableKey() {}
2355};
2356
2357class SymbolTableShape {
2358 public:
2359 static bool IsMatch(HashTableKey* key, Object* value) {
2360 return key->IsMatch(value);
2361 }
2362 static uint32_t Hash(HashTableKey* key) {
2363 return key->Hash();
2364 }
2365 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2366 return key->HashForObject(object);
2367 }
John Reck59135872010-11-02 12:39:01 -07002368 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002369 return key->AsObject();
2370 }
2371
2372 static const int kPrefixSize = 0;
2373 static const int kEntrySize = 1;
2374};
2375
2376// SymbolTable.
2377//
2378// No special elements in the prefix and the element size is 1
2379// because only the symbol itself (the key) needs to be stored.
2380class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2381 public:
2382 // Find symbol in the symbol table. If it is not there yet, it is
2383 // added. The return value is the symbol table which might have
2384 // been enlarged. If the return value is not a failure, the symbol
2385 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002386 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002387 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2388 Object** s);
2389 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2390 Object** s);
John Reck59135872010-11-02 12:39:01 -07002391 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002392
2393 // Looks up a symbol that is equal to the given string and returns
2394 // true if it is found, assigning the symbol to the given output
2395 // parameter.
2396 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002397 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002398
2399 // Casting.
2400 static inline SymbolTable* cast(Object* obj);
2401
2402 private:
John Reck59135872010-11-02 12:39:01 -07002403 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002404
2405 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2406};
2407
2408
2409class MapCacheShape {
2410 public:
2411 static bool IsMatch(HashTableKey* key, Object* value) {
2412 return key->IsMatch(value);
2413 }
2414 static uint32_t Hash(HashTableKey* key) {
2415 return key->Hash();
2416 }
2417
2418 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2419 return key->HashForObject(object);
2420 }
2421
John Reck59135872010-11-02 12:39:01 -07002422 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002423 return key->AsObject();
2424 }
2425
2426 static const int kPrefixSize = 0;
2427 static const int kEntrySize = 2;
2428};
2429
2430
2431// MapCache.
2432//
2433// Maps keys that are a fixed array of symbols to a map.
2434// Used for canonicalize maps for object literals.
2435class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2436 public:
2437 // Find cached value for a string key, otherwise return null.
2438 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002439 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002440 static inline MapCache* cast(Object* obj);
2441
2442 private:
2443 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2444};
2445
2446
2447template <typename Shape, typename Key>
2448class Dictionary: public HashTable<Shape, Key> {
2449 public:
2450
2451 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2452 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2453 }
2454
2455 // Returns the value at entry.
2456 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002457 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002458 }
2459
2460 // Set the value for entry.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002461 // Returns false if the put wasn't performed due to property being read only.
2462 // Returns true on successful put.
2463 bool ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002464 // Check that this value can actually be written.
2465 PropertyDetails details = DetailsAt(entry);
2466 // If a value has not been initilized we allow writing to it even if
2467 // it is read only (a declared const that has not been initialized).
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002468 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2469 return false;
2470 }
2471 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2472 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002473 }
2474
2475 // Returns the property details for the property at entry.
2476 PropertyDetails DetailsAt(int entry) {
2477 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2478 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002479 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002480 }
2481
2482 // Set the details for entry.
2483 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002484 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002485 }
2486
2487 // Sorting support
2488 void CopyValuesTo(FixedArray* elements);
2489
2490 // Delete a property from the dictionary.
2491 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2492
2493 // Returns the number of elements in the dictionary filtering out properties
2494 // with the specified attributes.
2495 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2496
2497 // Returns the number of enumerable elements in the dictionary.
2498 int NumberOfEnumElements();
2499
2500 // Copies keys to preallocated fixed array.
2501 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2502 // Fill in details for properties into storage.
2503 void CopyKeysTo(FixedArray* storage);
2504
2505 // Accessors for next enumeration index.
2506 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002507 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002508 }
2509
2510 int NextEnumerationIndex() {
2511 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2512 }
2513
2514 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002515 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002516
2517 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002518 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002519
Ben Murdochb0fe1622011-05-05 13:52:32 +01002520#ifdef OBJECT_PRINT
2521 inline void Print() {
2522 Print(stdout);
2523 }
2524 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002525#endif
2526 // Returns the key (slow).
2527 Object* SlowReverseLookup(Object* value);
2528
2529 // Sets the entry to (key, value) pair.
2530 inline void SetEntry(int entry,
2531 Object* key,
2532 Object* value,
2533 PropertyDetails details);
2534
John Reck59135872010-11-02 12:39:01 -07002535 MUST_USE_RESULT MaybeObject* Add(Key key,
2536 Object* value,
2537 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002538
2539 protected:
2540 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002541 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002542
2543 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002544 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2545 Object* value,
2546 PropertyDetails details,
2547 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002548
2549 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002550 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002551 static const int kMaxNumberKeyIndex =
2552 HashTable<Shape, Key>::kPrefixStartIndex;
2553 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2554};
2555
2556
2557class StringDictionaryShape {
2558 public:
2559 static inline bool IsMatch(String* key, Object* other);
2560 static inline uint32_t Hash(String* key);
2561 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002562 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002563 static const int kPrefixSize = 2;
2564 static const int kEntrySize = 3;
2565 static const bool kIsEnumerable = true;
2566};
2567
2568
2569class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2570 public:
2571 static inline StringDictionary* cast(Object* obj) {
2572 ASSERT(obj->IsDictionary());
2573 return reinterpret_cast<StringDictionary*>(obj);
2574 }
2575
2576 // Copies enumerable keys to preallocated fixed array.
2577 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2578
2579 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002580 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2581 JSObject* obj,
2582 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002583
2584 // Find entry for key otherwise return kNotFound. Optimzed version of
2585 // HashTable::FindEntry.
2586 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002587};
2588
2589
2590class NumberDictionaryShape {
2591 public:
2592 static inline bool IsMatch(uint32_t key, Object* other);
2593 static inline uint32_t Hash(uint32_t key);
2594 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002595 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002596 static const int kPrefixSize = 2;
2597 static const int kEntrySize = 3;
2598 static const bool kIsEnumerable = false;
2599};
2600
2601
2602class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2603 public:
2604 static NumberDictionary* cast(Object* obj) {
2605 ASSERT(obj->IsDictionary());
2606 return reinterpret_cast<NumberDictionary*>(obj);
2607 }
2608
2609 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002610 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2611 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2612 Object* value,
2613 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002614
2615 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002616 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2617 Object* value,
2618 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002619
2620 void UpdateMaxNumberKey(uint32_t key);
2621
2622 // If slow elements are required we will never go back to fast-case
2623 // for the elements kept in this dictionary. We require slow
2624 // elements if an element has been added at an index larger than
2625 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2626 // when defining a getter or setter with a number key.
2627 inline bool requires_slow_elements();
2628 inline void set_requires_slow_elements();
2629
2630 // Get the value of the max number key that has been added to this
2631 // dictionary. max_number_key can only be called if
2632 // requires_slow_elements returns false.
2633 inline uint32_t max_number_key();
2634
2635 // Remove all entries were key is a number and (from <= key && key < to).
2636 void RemoveNumberEntries(uint32_t from, uint32_t to);
2637
2638 // Bit masks.
2639 static const int kRequiresSlowElementsMask = 1;
2640 static const int kRequiresSlowElementsTagSize = 1;
2641 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2642};
2643
2644
Steve Block6ded16b2010-05-10 14:33:55 +01002645// JSFunctionResultCache caches results of some JSFunction invocation.
2646// It is a fixed array with fixed structure:
2647// [0]: factory function
2648// [1]: finger index
2649// [2]: current cache size
2650// [3]: dummy field.
2651// The rest of array are key/value pairs.
2652class JSFunctionResultCache: public FixedArray {
2653 public:
2654 static const int kFactoryIndex = 0;
2655 static const int kFingerIndex = kFactoryIndex + 1;
2656 static const int kCacheSizeIndex = kFingerIndex + 1;
2657 static const int kDummyIndex = kCacheSizeIndex + 1;
2658 static const int kEntriesIndex = kDummyIndex + 1;
2659
2660 static const int kEntrySize = 2; // key + value
2661
Kristian Monsen25f61362010-05-21 11:50:48 +01002662 static const int kFactoryOffset = kHeaderSize;
2663 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2664 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2665
Steve Block6ded16b2010-05-10 14:33:55 +01002666 inline void MakeZeroSize();
2667 inline void Clear();
2668
Ben Murdochb8e0da22011-05-16 14:20:40 +01002669 inline int size();
2670 inline void set_size(int size);
2671 inline int finger_index();
2672 inline void set_finger_index(int finger_index);
2673
Steve Block6ded16b2010-05-10 14:33:55 +01002674 // Casting
2675 static inline JSFunctionResultCache* cast(Object* obj);
2676
2677#ifdef DEBUG
2678 void JSFunctionResultCacheVerify();
2679#endif
2680};
2681
2682
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002683// The cache for maps used by normalized (dictionary mode) objects.
2684// Such maps do not have property descriptors, so a typical program
2685// needs very limited number of distinct normalized maps.
2686class NormalizedMapCache: public FixedArray {
2687 public:
2688 static const int kEntries = 64;
2689
John Reck59135872010-11-02 12:39:01 -07002690 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2691 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002692
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002693 void Clear();
2694
2695 // Casting
2696 static inline NormalizedMapCache* cast(Object* obj);
2697
2698#ifdef DEBUG
2699 void NormalizedMapCacheVerify();
2700#endif
2701
2702 private:
2703 static int Hash(Map* fast);
2704
2705 static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2706};
2707
2708
Steve Blocka7e24c12009-10-30 11:49:00 +00002709// ByteArray represents fixed sized byte arrays. Used by the outside world,
2710// such as PCRE, and also by the memory allocator and garbage collector to
2711// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002712class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002713 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002714 // [length]: length of the array.
2715 inline int length();
2716 inline void set_length(int value);
2717
Steve Blocka7e24c12009-10-30 11:49:00 +00002718 // Setter and getter.
2719 inline byte get(int index);
2720 inline void set(int index, byte value);
2721
2722 // Treat contents as an int array.
2723 inline int get_int(int index);
2724
2725 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002726 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002727 }
2728 // We use byte arrays for free blocks in the heap. Given a desired size in
2729 // bytes that is a multiple of the word size and big enough to hold a byte
2730 // array, this function returns the number of elements a byte array should
2731 // have.
2732 static int LengthFor(int size_in_bytes) {
2733 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2734 ASSERT(size_in_bytes >= kHeaderSize);
2735 return size_in_bytes - kHeaderSize;
2736 }
2737
2738 // Returns data start address.
2739 inline Address GetDataStartAddress();
2740
2741 // Returns a pointer to the ByteArray object for a given data start address.
2742 static inline ByteArray* FromDataStartAddress(Address address);
2743
2744 // Casting.
2745 static inline ByteArray* cast(Object* obj);
2746
2747 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002748 inline int ByteArraySize() {
2749 return SizeFor(this->length());
2750 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002751#ifdef OBJECT_PRINT
2752 inline void ByteArrayPrint() {
2753 ByteArrayPrint(stdout);
2754 }
2755 void ByteArrayPrint(FILE* out);
2756#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002757#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002758 void ByteArrayVerify();
2759#endif
2760
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002761 // Layout description.
2762 // Length is smi tagged when it is stored.
2763 static const int kLengthOffset = HeapObject::kHeaderSize;
2764 static const int kHeaderSize = kLengthOffset + kPointerSize;
2765
2766 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002767
Leon Clarkee46be812010-01-19 14:06:41 +00002768 // Maximal memory consumption for a single ByteArray.
2769 static const int kMaxSize = 512 * MB;
2770 // Maximal length of a single ByteArray.
2771 static const int kMaxLength = kMaxSize - kHeaderSize;
2772
Steve Blocka7e24c12009-10-30 11:49:00 +00002773 private:
2774 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2775};
2776
2777
2778// A PixelArray represents a fixed-size byte array with special semantics
2779// used for implementing the CanvasPixelArray object. Please see the
2780// specification at:
2781// http://www.whatwg.org/specs/web-apps/current-work/
2782// multipage/the-canvas-element.html#canvaspixelarray
2783// In particular, write access clamps the value written to 0 or 255 if the
2784// value written is outside this range.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002785class PixelArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002786 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002787 // [length]: length of the array.
2788 inline int length();
2789 inline void set_length(int value);
2790
Steve Blocka7e24c12009-10-30 11:49:00 +00002791 // [external_pointer]: The pointer to the external memory area backing this
2792 // pixel array.
2793 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2794
2795 // Setter and getter.
2796 inline uint8_t get(int index);
2797 inline void set(int index, uint8_t value);
2798
2799 // This accessor applies the correct conversion from Smi, HeapNumber and
2800 // undefined and clamps the converted value between 0 and 255.
2801 Object* SetValue(uint32_t index, Object* value);
2802
2803 // Casting.
2804 static inline PixelArray* cast(Object* obj);
2805
Ben Murdochb0fe1622011-05-05 13:52:32 +01002806#ifdef OBJECT_PRINT
2807 inline void PixelArrayPrint() {
2808 PixelArrayPrint(stdout);
2809 }
2810 void PixelArrayPrint(FILE* out);
2811#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002812#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002813 void PixelArrayVerify();
2814#endif // DEBUG
2815
Steve Block3ce2e202009-11-05 08:53:23 +00002816 // Maximal acceptable length for a pixel array.
2817 static const int kMaxLength = 0x3fffffff;
2818
Steve Blocka7e24c12009-10-30 11:49:00 +00002819 // PixelArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002820 static const int kLengthOffset = HeapObject::kHeaderSize;
2821 static const int kExternalPointerOffset =
2822 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002823 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002824 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002825
2826 private:
2827 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2828};
2829
2830
Steve Block3ce2e202009-11-05 08:53:23 +00002831// An ExternalArray represents a fixed-size array of primitive values
2832// which live outside the JavaScript heap. Its subclasses are used to
2833// implement the CanvasArray types being defined in the WebGL
2834// specification. As of this writing the first public draft is not yet
2835// available, but Khronos members can access the draft at:
2836// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2837//
2838// The semantics of these arrays differ from CanvasPixelArray.
2839// Out-of-range values passed to the setter are converted via a C
2840// cast, not clamping. Out-of-range indices cause exceptions to be
2841// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002842class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002843 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002844 // [length]: length of the array.
2845 inline int length();
2846 inline void set_length(int value);
2847
Steve Block3ce2e202009-11-05 08:53:23 +00002848 // [external_pointer]: The pointer to the external memory area backing this
2849 // external array.
2850 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2851
2852 // Casting.
2853 static inline ExternalArray* cast(Object* obj);
2854
2855 // Maximal acceptable length for an external array.
2856 static const int kMaxLength = 0x3fffffff;
2857
2858 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002859 static const int kLengthOffset = HeapObject::kHeaderSize;
2860 static const int kExternalPointerOffset =
2861 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002862 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002863 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002864
2865 private:
2866 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2867};
2868
2869
2870class ExternalByteArray: public ExternalArray {
2871 public:
2872 // Setter and getter.
2873 inline int8_t get(int index);
2874 inline void set(int index, int8_t value);
2875
2876 // This accessor applies the correct conversion from Smi, HeapNumber
2877 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002878 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002879
2880 // Casting.
2881 static inline ExternalByteArray* cast(Object* obj);
2882
Ben Murdochb0fe1622011-05-05 13:52:32 +01002883#ifdef OBJECT_PRINT
2884 inline void ExternalByteArrayPrint() {
2885 ExternalByteArrayPrint(stdout);
2886 }
2887 void ExternalByteArrayPrint(FILE* out);
2888#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002889#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002890 void ExternalByteArrayVerify();
2891#endif // DEBUG
2892
2893 private:
2894 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2895};
2896
2897
2898class ExternalUnsignedByteArray: public ExternalArray {
2899 public:
2900 // Setter and getter.
2901 inline uint8_t get(int index);
2902 inline void set(int index, uint8_t value);
2903
2904 // This accessor applies the correct conversion from Smi, HeapNumber
2905 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002906 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002907
2908 // Casting.
2909 static inline ExternalUnsignedByteArray* cast(Object* obj);
2910
Ben Murdochb0fe1622011-05-05 13:52:32 +01002911#ifdef OBJECT_PRINT
2912 inline void ExternalUnsignedByteArrayPrint() {
2913 ExternalUnsignedByteArrayPrint(stdout);
2914 }
2915 void ExternalUnsignedByteArrayPrint(FILE* out);
2916#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002917#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002918 void ExternalUnsignedByteArrayVerify();
2919#endif // DEBUG
2920
2921 private:
2922 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2923};
2924
2925
2926class ExternalShortArray: public ExternalArray {
2927 public:
2928 // Setter and getter.
2929 inline int16_t get(int index);
2930 inline void set(int index, int16_t value);
2931
2932 // This accessor applies the correct conversion from Smi, HeapNumber
2933 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002934 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002935
2936 // Casting.
2937 static inline ExternalShortArray* cast(Object* obj);
2938
Ben Murdochb0fe1622011-05-05 13:52:32 +01002939#ifdef OBJECT_PRINT
2940 inline void ExternalShortArrayPrint() {
2941 ExternalShortArrayPrint(stdout);
2942 }
2943 void ExternalShortArrayPrint(FILE* out);
2944#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002945#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002946 void ExternalShortArrayVerify();
2947#endif // DEBUG
2948
2949 private:
2950 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2951};
2952
2953
2954class ExternalUnsignedShortArray: public ExternalArray {
2955 public:
2956 // Setter and getter.
2957 inline uint16_t get(int index);
2958 inline void set(int index, uint16_t value);
2959
2960 // This accessor applies the correct conversion from Smi, HeapNumber
2961 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002962 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002963
2964 // Casting.
2965 static inline ExternalUnsignedShortArray* cast(Object* obj);
2966
Ben Murdochb0fe1622011-05-05 13:52:32 +01002967#ifdef OBJECT_PRINT
2968 inline void ExternalUnsignedShortArrayPrint() {
2969 ExternalUnsignedShortArrayPrint(stdout);
2970 }
2971 void ExternalUnsignedShortArrayPrint(FILE* out);
2972#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002973#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002974 void ExternalUnsignedShortArrayVerify();
2975#endif // DEBUG
2976
2977 private:
2978 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2979};
2980
2981
2982class ExternalIntArray: public ExternalArray {
2983 public:
2984 // Setter and getter.
2985 inline int32_t get(int index);
2986 inline void set(int index, int32_t value);
2987
2988 // This accessor applies the correct conversion from Smi, HeapNumber
2989 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002990 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002991
2992 // Casting.
2993 static inline ExternalIntArray* cast(Object* obj);
2994
Ben Murdochb0fe1622011-05-05 13:52:32 +01002995#ifdef OBJECT_PRINT
2996 inline void ExternalIntArrayPrint() {
2997 ExternalIntArrayPrint(stdout);
2998 }
2999 void ExternalIntArrayPrint(FILE* out);
3000#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003001#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003002 void ExternalIntArrayVerify();
3003#endif // DEBUG
3004
3005 private:
3006 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3007};
3008
3009
3010class ExternalUnsignedIntArray: public ExternalArray {
3011 public:
3012 // Setter and getter.
3013 inline uint32_t get(int index);
3014 inline void set(int index, uint32_t value);
3015
3016 // This accessor applies the correct conversion from Smi, HeapNumber
3017 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003018 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003019
3020 // Casting.
3021 static inline ExternalUnsignedIntArray* cast(Object* obj);
3022
Ben Murdochb0fe1622011-05-05 13:52:32 +01003023#ifdef OBJECT_PRINT
3024 inline void ExternalUnsignedIntArrayPrint() {
3025 ExternalUnsignedIntArrayPrint(stdout);
3026 }
3027 void ExternalUnsignedIntArrayPrint(FILE* out);
3028#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003029#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003030 void ExternalUnsignedIntArrayVerify();
3031#endif // DEBUG
3032
3033 private:
3034 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3035};
3036
3037
3038class ExternalFloatArray: public ExternalArray {
3039 public:
3040 // Setter and getter.
3041 inline float get(int index);
3042 inline void set(int index, float value);
3043
3044 // This accessor applies the correct conversion from Smi, HeapNumber
3045 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003046 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003047
3048 // Casting.
3049 static inline ExternalFloatArray* cast(Object* obj);
3050
Ben Murdochb0fe1622011-05-05 13:52:32 +01003051#ifdef OBJECT_PRINT
3052 inline void ExternalFloatArrayPrint() {
3053 ExternalFloatArrayPrint(stdout);
3054 }
3055 void ExternalFloatArrayPrint(FILE* out);
3056#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003057#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003058 void ExternalFloatArrayVerify();
3059#endif // DEBUG
3060
3061 private:
3062 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3063};
3064
3065
Ben Murdochb0fe1622011-05-05 13:52:32 +01003066// DeoptimizationInputData is a fixed array used to hold the deoptimization
3067// data for code generated by the Hydrogen/Lithium compiler. It also
3068// contains information about functions that were inlined. If N different
3069// functions were inlined then first N elements of the literal array will
3070// contain these functions.
3071//
3072// It can be empty.
3073class DeoptimizationInputData: public FixedArray {
3074 public:
3075 // Layout description. Indices in the array.
3076 static const int kTranslationByteArrayIndex = 0;
3077 static const int kInlinedFunctionCountIndex = 1;
3078 static const int kLiteralArrayIndex = 2;
3079 static const int kOsrAstIdIndex = 3;
3080 static const int kOsrPcOffsetIndex = 4;
3081 static const int kFirstDeoptEntryIndex = 5;
3082
3083 // Offsets of deopt entry elements relative to the start of the entry.
3084 static const int kAstIdOffset = 0;
3085 static const int kTranslationIndexOffset = 1;
3086 static const int kArgumentsStackHeightOffset = 2;
3087 static const int kDeoptEntrySize = 3;
3088
3089 // Simple element accessors.
3090#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3091 type* name() { \
3092 return type::cast(get(k##name##Index)); \
3093 } \
3094 void Set##name(type* value) { \
3095 set(k##name##Index, value); \
3096 }
3097
3098 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3099 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3100 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3101 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3102 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3103
3104 // Unchecked accessor to be used during GC.
3105 FixedArray* UncheckedLiteralArray() {
3106 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3107 }
3108
3109#undef DEFINE_ELEMENT_ACCESSORS
3110
3111 // Accessors for elements of the ith deoptimization entry.
3112#define DEFINE_ENTRY_ACCESSORS(name, type) \
3113 type* name(int i) { \
3114 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3115 } \
3116 void Set##name(int i, type* value) { \
3117 set(IndexForEntry(i) + k##name##Offset, value); \
3118 }
3119
3120 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3121 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3122 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3123
3124#undef DEFINE_ENTRY_ACCESSORS
3125
3126 int DeoptCount() {
3127 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3128 }
3129
3130 // Allocates a DeoptimizationInputData.
3131 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3132 PretenureFlag pretenure);
3133
3134 // Casting.
3135 static inline DeoptimizationInputData* cast(Object* obj);
3136
3137#ifdef OBJECT_PRINT
3138 void DeoptimizationInputDataPrint(FILE* out);
3139#endif
3140
3141 private:
3142 static int IndexForEntry(int i) {
3143 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3144 }
3145
3146 static int LengthFor(int entry_count) {
3147 return IndexForEntry(entry_count);
3148 }
3149};
3150
3151
3152// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3153// data for code generated by the full compiler.
3154// The format of the these objects is
3155// [i * 2]: Ast ID for ith deoptimization.
3156// [i * 2 + 1]: PC and state of ith deoptimization
3157class DeoptimizationOutputData: public FixedArray {
3158 public:
3159 int DeoptPoints() { return length() / 2; }
3160 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3161 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3162 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3163 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3164
3165 static int LengthOfFixedArray(int deopt_points) {
3166 return deopt_points * 2;
3167 }
3168
3169 // Allocates a DeoptimizationOutputData.
3170 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3171 PretenureFlag pretenure);
3172
3173 // Casting.
3174 static inline DeoptimizationOutputData* cast(Object* obj);
3175
3176#ifdef OBJECT_PRINT
3177 void DeoptimizationOutputDataPrint(FILE* out);
3178#endif
3179};
3180
3181
Ben Murdochb8e0da22011-05-16 14:20:40 +01003182class SafepointEntry;
3183
3184
Steve Blocka7e24c12009-10-30 11:49:00 +00003185// Code describes objects with on-the-fly generated machine code.
3186class Code: public HeapObject {
3187 public:
3188 // Opaque data type for encapsulating code flags like kind, inline
3189 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003190 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3191 // enumeration type has correct value range (see Issue 830 for more details).
3192 enum Flags {
3193 FLAGS_MIN_VALUE = kMinInt,
3194 FLAGS_MAX_VALUE = kMaxInt
3195 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003196
3197 enum Kind {
3198 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003199 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003200 STUB,
3201 BUILTIN,
3202 LOAD_IC,
3203 KEYED_LOAD_IC,
3204 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003205 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003206 STORE_IC,
3207 KEYED_STORE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003208 BINARY_OP_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003209 TYPE_RECORDING_BINARY_OP_IC,
3210 COMPARE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003211 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003212 // Flags.
3213
3214 // Pseudo-kinds.
3215 REGEXP = BUILTIN,
3216 FIRST_IC_KIND = LOAD_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003217 LAST_IC_KIND = COMPARE_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003218 };
3219
3220 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003221 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003222 };
3223
Ben Murdochb8e0da22011-05-16 14:20:40 +01003224 typedef int ExtraICState;
3225
3226 static const ExtraICState kNoExtraICState = 0;
3227
Steve Blocka7e24c12009-10-30 11:49:00 +00003228#ifdef ENABLE_DISASSEMBLER
3229 // Printing
3230 static const char* Kind2String(Kind kind);
3231 static const char* ICState2String(InlineCacheState state);
3232 static const char* PropertyType2String(PropertyType type);
Steve Block1e0659c2011-05-24 12:43:12 +01003233 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003234 inline void Disassemble(const char* name) {
3235 Disassemble(name, stdout);
3236 }
3237 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003238#endif // ENABLE_DISASSEMBLER
3239
3240 // [instruction_size]: Size of the native instructions
3241 inline int instruction_size();
3242 inline void set_instruction_size(int value);
3243
Leon Clarkeac952652010-07-15 11:15:24 +01003244 // [relocation_info]: Code relocation information
3245 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003246 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003247
Ben Murdochb0fe1622011-05-05 13:52:32 +01003248 // [deoptimization_data]: Array containing data for deopt.
3249 DECL_ACCESSORS(deoptimization_data, FixedArray)
3250
3251 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003252 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003253 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003254
Steve Blocka7e24c12009-10-30 11:49:00 +00003255 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003256
Steve Blocka7e24c12009-10-30 11:49:00 +00003257 // [flags]: Various code flags.
3258 inline Flags flags();
3259 inline void set_flags(Flags flags);
3260
3261 // [flags]: Access to specific code flags.
3262 inline Kind kind();
3263 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003264 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00003265 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
3266 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3267 inline int arguments_count(); // Only valid for call IC stubs.
3268
3269 // Testers for IC stub kinds.
3270 inline bool is_inline_cache_stub();
3271 inline bool is_load_stub() { return kind() == LOAD_IC; }
3272 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3273 inline bool is_store_stub() { return kind() == STORE_IC; }
3274 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3275 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003276 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003277 inline bool is_binary_op_stub() { return kind() == BINARY_OP_IC; }
3278 inline bool is_type_recording_binary_op_stub() {
3279 return kind() == TYPE_RECORDING_BINARY_OP_IC;
3280 }
3281 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00003282
Steve Block6ded16b2010-05-10 14:33:55 +01003283 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003284 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003285 inline void set_major_key(int value);
3286
3287 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3288 inline bool optimizable();
3289 inline void set_optimizable(bool value);
3290
3291 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3292 // deoptimization support.
3293 inline bool has_deoptimization_support();
3294 inline void set_has_deoptimization_support(bool value);
3295
3296 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3297 // how long the function has been marked for OSR and therefore which
3298 // level of loop nesting we are willing to do on-stack replacement
3299 // for.
3300 inline void set_allow_osr_at_loop_nesting_level(int level);
3301 inline int allow_osr_at_loop_nesting_level();
3302
3303 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3304 // reserved in the code prologue.
3305 inline unsigned stack_slots();
3306 inline void set_stack_slots(unsigned slots);
3307
3308 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3309 // the instruction stream where the safepoint table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003310 inline unsigned safepoint_table_offset();
3311 inline void set_safepoint_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003312
3313 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3314 // instruction stream where the stack check table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003315 inline unsigned stack_check_table_offset();
3316 inline void set_stack_check_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003317
3318 // [check type]: For kind CALL_IC, tells how to check if the
3319 // receiver is valid for the given call.
3320 inline CheckType check_type();
3321 inline void set_check_type(CheckType value);
3322
3323 // [binary op type]: For all BINARY_OP_IC.
3324 inline byte binary_op_type();
3325 inline void set_binary_op_type(byte value);
3326
3327 // [type-recording binary op type]: For all TYPE_RECORDING_BINARY_OP_IC.
3328 inline byte type_recording_binary_op_type();
3329 inline void set_type_recording_binary_op_type(byte value);
3330 inline byte type_recording_binary_op_result_type();
3331 inline void set_type_recording_binary_op_result_type(byte value);
3332
3333 // [compare state]: For kind compare IC stubs, tells what state the
3334 // stub is in.
3335 inline byte compare_state();
3336 inline void set_compare_state(byte value);
3337
Ben Murdochb8e0da22011-05-16 14:20:40 +01003338 // Get the safepoint entry for the given pc.
3339 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003340
3341 // Mark this code object as not having a stack check table. Assumes kind
3342 // is FUNCTION.
3343 void SetNoStackCheckTable();
3344
3345 // Find the first map in an IC stub.
3346 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003347
3348 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003349 static inline Flags ComputeFlags(
3350 Kind kind,
3351 InLoopFlag in_loop = NOT_IN_LOOP,
3352 InlineCacheState ic_state = UNINITIALIZED,
3353 ExtraICState extra_ic_state = kNoExtraICState,
3354 PropertyType type = NORMAL,
3355 int argc = -1,
3356 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003357
3358 static inline Flags ComputeMonomorphicFlags(
3359 Kind kind,
3360 PropertyType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003361 ExtraICState extra_ic_state = kNoExtraICState,
Steve Block8defd9f2010-07-08 12:39:36 +01003362 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003363 InLoopFlag in_loop = NOT_IN_LOOP,
3364 int argc = -1);
3365
3366 static inline Kind ExtractKindFromFlags(Flags flags);
3367 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003368 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003369 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
3370 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3371 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003372 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003373 static inline Flags RemoveTypeFromFlags(Flags flags);
3374
3375 // Convert a target address into a code object.
3376 static inline Code* GetCodeFromTargetAddress(Address address);
3377
Steve Block791712a2010-08-27 10:21:07 +01003378 // Convert an entry address into an object.
3379 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3380
Steve Blocka7e24c12009-10-30 11:49:00 +00003381 // Returns the address of the first instruction.
3382 inline byte* instruction_start();
3383
Leon Clarkeac952652010-07-15 11:15:24 +01003384 // Returns the address right after the last instruction.
3385 inline byte* instruction_end();
3386
Steve Blocka7e24c12009-10-30 11:49:00 +00003387 // Returns the size of the instructions, padding, and relocation information.
3388 inline int body_size();
3389
3390 // Returns the address of the first relocation info (read backwards!).
3391 inline byte* relocation_start();
3392
3393 // Code entry point.
3394 inline byte* entry();
3395
3396 // Returns true if pc is inside this object's instructions.
3397 inline bool contains(byte* pc);
3398
Steve Blocka7e24c12009-10-30 11:49:00 +00003399 // Relocate the code by delta bytes. Called to signal that this code
3400 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003401 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003402
3403 // Migrate code described by desc.
3404 void CopyFrom(const CodeDesc& desc);
3405
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003406 // Returns the object size for a given body (used for allocation).
3407 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003408 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003409 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003410 }
3411
3412 // Calculate the size of the code object to report for log events. This takes
3413 // the layout of the code object into account.
3414 int ExecutableSize() {
3415 // Check that the assumptions about the layout of the code object holds.
3416 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3417 Code::kHeaderSize);
3418 return instruction_size() + Code::kHeaderSize;
3419 }
3420
3421 // Locating source position.
3422 int SourcePosition(Address pc);
3423 int SourceStatementPosition(Address pc);
3424
3425 // Casting.
3426 static inline Code* cast(Object* obj);
3427
3428 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003429 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003430 inline void CodeIterateBody(ObjectVisitor* v);
3431
3432 template<typename StaticVisitor>
3433 inline void CodeIterateBody();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003434#ifdef OBJECT_PRINT
3435 inline void CodePrint() {
3436 CodePrint(stdout);
3437 }
3438 void CodePrint(FILE* out);
3439#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003440#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003441 void CodeVerify();
3442#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003443
3444 // Max loop nesting marker used to postpose OSR. We don't take loop
3445 // nesting that is deeper than 5 levels into account.
3446 static const int kMaxLoopNestingMarker = 6;
3447
Steve Blocka7e24c12009-10-30 11:49:00 +00003448 // Layout description.
3449 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003450 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003451 static const int kDeoptimizationDataOffset =
3452 kRelocationInfoOffset + kPointerSize;
3453 static const int kFlagsOffset = kDeoptimizationDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003454 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003455
3456 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3457
3458 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3459 kKindSpecificFlagsSize;
3460
Steve Blocka7e24c12009-10-30 11:49:00 +00003461 // Add padding to align the instruction start following right after
3462 // the Code object header.
3463 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003464 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003465
3466 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003467 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3468 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3469 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3470 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3471
3472 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3473 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
3474 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3475
3476 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3477 static const int kAllowOSRAtLoopNestingLevelOffset =
3478 kHasDeoptimizationSupportOffset + 1;
3479
Steve Block1e0659c2011-05-24 12:43:12 +01003480 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3481 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003482
3483 // Flags layout.
3484 static const int kFlagsICStateShift = 0;
3485 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003486 static const int kFlagsTypeShift = 4;
3487 static const int kFlagsKindShift = 7;
Steve Block8defd9f2010-07-08 12:39:36 +01003488 static const int kFlagsICHolderShift = 11;
Ben Murdochb8e0da22011-05-16 14:20:40 +01003489 static const int kFlagsExtraICStateShift = 12;
3490 static const int kFlagsArgumentsCountShift = 14;
Steve Blocka7e24c12009-10-30 11:49:00 +00003491
Steve Block6ded16b2010-05-10 14:33:55 +01003492 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3493 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003494 static const int kFlagsTypeMask = 0x00000070; // 00001110000
3495 static const int kFlagsKindMask = 0x00000780; // 11110000000
Steve Block8defd9f2010-07-08 12:39:36 +01003496 static const int kFlagsCacheInPrototypeMapMask = 0x00000800;
Ben Murdochb8e0da22011-05-16 14:20:40 +01003497 static const int kFlagsExtraICStateMask = 0x00003000;
3498 static const int kFlagsArgumentsCountMask = 0xFFFFC000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003499
3500 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003501 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003502
3503 private:
3504 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3505};
3506
3507
3508// All heap objects have a Map that describes their structure.
3509// A Map contains information about:
3510// - Size information about the object
3511// - How to iterate over an object (for garbage collection)
3512class Map: public HeapObject {
3513 public:
3514 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003515 // Size in bytes or kVariableSizeSentinel if instances do not have
3516 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003517 inline int instance_size();
3518 inline void set_instance_size(int value);
3519
3520 // Count of properties allocated in the object.
3521 inline int inobject_properties();
3522 inline void set_inobject_properties(int value);
3523
3524 // Count of property fields pre-allocated in the object when first allocated.
3525 inline int pre_allocated_property_fields();
3526 inline void set_pre_allocated_property_fields(int value);
3527
3528 // Instance type.
3529 inline InstanceType instance_type();
3530 inline void set_instance_type(InstanceType value);
3531
3532 // Tells how many unused property fields are available in the
3533 // instance (only used for JSObject in fast mode).
3534 inline int unused_property_fields();
3535 inline void set_unused_property_fields(int value);
3536
3537 // Bit field.
3538 inline byte bit_field();
3539 inline void set_bit_field(byte value);
3540
3541 // Bit field 2.
3542 inline byte bit_field2();
3543 inline void set_bit_field2(byte value);
3544
3545 // Tells whether the object in the prototype property will be used
3546 // for instances created from this function. If the prototype
3547 // property is set to a value that is not a JSObject, the prototype
3548 // property will not be used to create instances of the function.
3549 // See ECMA-262, 13.2.2.
3550 inline void set_non_instance_prototype(bool value);
3551 inline bool has_non_instance_prototype();
3552
Steve Block6ded16b2010-05-10 14:33:55 +01003553 // Tells whether function has special prototype property. If not, prototype
3554 // property will not be created when accessed (will return undefined),
3555 // and construction from this function will not be allowed.
3556 inline void set_function_with_prototype(bool value);
3557 inline bool function_with_prototype();
3558
Steve Blocka7e24c12009-10-30 11:49:00 +00003559 // Tells whether the instance with this map should be ignored by the
3560 // __proto__ accessor.
3561 inline void set_is_hidden_prototype() {
3562 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3563 }
3564
3565 inline bool is_hidden_prototype() {
3566 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3567 }
3568
3569 // Records and queries whether the instance has a named interceptor.
3570 inline void set_has_named_interceptor() {
3571 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3572 }
3573
3574 inline bool has_named_interceptor() {
3575 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3576 }
3577
3578 // Records and queries whether the instance has an indexed interceptor.
3579 inline void set_has_indexed_interceptor() {
3580 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3581 }
3582
3583 inline bool has_indexed_interceptor() {
3584 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3585 }
3586
3587 // Tells whether the instance is undetectable.
3588 // An undetectable object is a special class of JSObject: 'typeof' operator
3589 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3590 // a normal JS object. It is useful for implementing undetectable
3591 // document.all in Firefox & Safari.
3592 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3593 inline void set_is_undetectable() {
3594 set_bit_field(bit_field() | (1 << kIsUndetectable));
3595 }
3596
3597 inline bool is_undetectable() {
3598 return ((1 << kIsUndetectable) & bit_field()) != 0;
3599 }
3600
Steve Blocka7e24c12009-10-30 11:49:00 +00003601 // Tells whether the instance has a call-as-function handler.
3602 inline void set_has_instance_call_handler() {
3603 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3604 }
3605
3606 inline bool has_instance_call_handler() {
3607 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3608 }
3609
Steve Block8defd9f2010-07-08 12:39:36 +01003610 inline void set_is_extensible(bool value);
3611 inline bool is_extensible();
3612
3613 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003614 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3615 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003616 if (value) {
3617 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3618 } else {
3619 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3620 }
Leon Clarkee46be812010-01-19 14:06:41 +00003621 }
3622
Iain Merrick75681382010-08-19 15:07:18 +01003623 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003624 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003625 }
3626
Steve Block1e0659c2011-05-24 12:43:12 +01003627 // Tells whether an instance has pixel array elements.
3628 inline void set_has_pixel_array_elements(bool value) {
3629 if (value) {
3630 set_bit_field2(bit_field2() | (1 << kHasPixelArrayElements));
3631 } else {
3632 set_bit_field2(bit_field2() & ~(1 << kHasPixelArrayElements));
3633 }
3634 }
3635
3636 inline bool has_pixel_array_elements() {
3637 return ((1 << kHasPixelArrayElements) & bit_field2()) != 0;
3638 }
3639
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003640 // Tells whether the map is attached to SharedFunctionInfo
3641 // (for inobject slack tracking).
3642 inline void set_attached_to_shared_function_info(bool value);
3643
3644 inline bool attached_to_shared_function_info();
3645
3646 // Tells whether the map is shared between objects that may have different
3647 // behavior. If true, the map should never be modified, instead a clone
3648 // should be created and modified.
3649 inline void set_is_shared(bool value);
3650
3651 inline bool is_shared();
3652
Steve Blocka7e24c12009-10-30 11:49:00 +00003653 // Tells whether the instance needs security checks when accessing its
3654 // properties.
3655 inline void set_is_access_check_needed(bool access_check_needed);
3656 inline bool is_access_check_needed();
3657
3658 // [prototype]: implicit prototype object.
3659 DECL_ACCESSORS(prototype, Object)
3660
3661 // [constructor]: points back to the function responsible for this map.
3662 DECL_ACCESSORS(constructor, Object)
3663
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003664 inline JSFunction* unchecked_constructor();
3665
Steve Blocka7e24c12009-10-30 11:49:00 +00003666 // [instance descriptors]: describes the object.
3667 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3668
3669 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003670 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003671
Ben Murdochb0fe1622011-05-05 13:52:32 +01003672 // Lookup in the map's instance descriptors and fill out the result
3673 // with the given holder if the name is found. The holder may be
3674 // NULL when this function is used from the compiler.
3675 void LookupInDescriptors(JSObject* holder,
3676 String* name,
3677 LookupResult* result);
3678
John Reck59135872010-11-02 12:39:01 -07003679 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003680
John Reck59135872010-11-02 12:39:01 -07003681 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3682 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00003683
3684 // Returns a copy of the map, with all transitions dropped from the
3685 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07003686 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00003687
Steve Block8defd9f2010-07-08 12:39:36 +01003688 // Returns this map if it has the fast elements bit set, otherwise
3689 // returns a copy of the map, with all transitions dropped from the
3690 // descriptors and the fast elements bit set.
John Reck59135872010-11-02 12:39:01 -07003691 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003692
3693 // Returns this map if it has the fast elements bit cleared,
3694 // otherwise returns a copy of the map, with all transitions dropped
3695 // from the descriptors and the fast elements bit cleared.
John Reck59135872010-11-02 12:39:01 -07003696 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003697
Steve Block1e0659c2011-05-24 12:43:12 +01003698 // Returns this map if it has the pixel array elements bit is set, otherwise
3699 // returns a copy of the map, with all transitions dropped from the
3700 // descriptors and the pixel array elements bit set.
3701 MUST_USE_RESULT inline MaybeObject* GetPixelArrayElementsMap();
3702
Steve Blocka7e24c12009-10-30 11:49:00 +00003703 // Returns the property index for name (only valid for FAST MODE).
3704 int PropertyIndexFor(String* name);
3705
3706 // Returns the next free property index (only valid for FAST MODE).
3707 int NextFreePropertyIndex();
3708
3709 // Returns the number of properties described in instance_descriptors.
3710 int NumberOfDescribedProperties();
3711
3712 // Casting.
3713 static inline Map* cast(Object* obj);
3714
3715 // Locate an accessor in the instance descriptor.
3716 AccessorDescriptor* FindAccessor(String* name);
3717
3718 // Code cache operations.
3719
3720 // Clears the code cache.
3721 inline void ClearCodeCache();
3722
3723 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07003724 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003725
3726 // Returns the found code or undefined if absent.
3727 Object* FindInCodeCache(String* name, Code::Flags flags);
3728
3729 // Returns the non-negative index of the code object if it is in the
3730 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003731 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003732
3733 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003734 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003735
3736 // For every transition in this map, makes the transition's
3737 // target's prototype pointer point back to this map.
3738 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3739 void CreateBackPointers();
3740
3741 // Set all map transitions from this map to dead maps to null.
3742 // Also, restore the original prototype on the targets of these
3743 // transitions, so that we do not process this map again while
3744 // following back pointers.
3745 void ClearNonLiveTransitions(Object* real_prototype);
3746
3747 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003748#ifdef OBJECT_PRINT
3749 inline void MapPrint() {
3750 MapPrint(stdout);
3751 }
3752 void MapPrint(FILE* out);
3753#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003754#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003755 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003756 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00003757#endif
3758
Iain Merrick75681382010-08-19 15:07:18 +01003759 inline int visitor_id();
3760 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003761
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003762 typedef void (*TraverseCallback)(Map* map, void* data);
3763
3764 void TraverseTransitionTree(TraverseCallback callback, void* data);
3765
Steve Blocka7e24c12009-10-30 11:49:00 +00003766 static const int kMaxPreAllocatedPropertyFields = 255;
3767
3768 // Layout description.
3769 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3770 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3771 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3772 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3773 static const int kInstanceDescriptorsOffset =
3774 kConstructorOffset + kPointerSize;
3775 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003776 static const int kPadStart = kCodeCacheOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003777 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3778
3779 // Layout of pointer fields. Heap iteration code relies on them
3780 // being continiously allocated.
3781 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3782 static const int kPointerFieldsEndOffset =
3783 Map::kCodeCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003784
3785 // Byte offsets within kInstanceSizesOffset.
3786 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3787 static const int kInObjectPropertiesByte = 1;
3788 static const int kInObjectPropertiesOffset =
3789 kInstanceSizesOffset + kInObjectPropertiesByte;
3790 static const int kPreAllocatedPropertyFieldsByte = 2;
3791 static const int kPreAllocatedPropertyFieldsOffset =
3792 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003793 static const int kVisitorIdByte = 3;
3794 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00003795
3796 // Byte offsets within kInstanceAttributesOffset attributes.
3797 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3798 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3799 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3800 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3801
3802 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3803
3804 // Bit positions for bit field.
3805 static const int kUnused = 0; // To be used for marking recently used maps.
3806 static const int kHasNonInstancePrototype = 1;
3807 static const int kIsHiddenPrototype = 2;
3808 static const int kHasNamedInterceptor = 3;
3809 static const int kHasIndexedInterceptor = 4;
3810 static const int kIsUndetectable = 5;
3811 static const int kHasInstanceCallHandler = 6;
3812 static const int kIsAccessCheckNeeded = 7;
3813
3814 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003815 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003816 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003817 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003818 static const int kStringWrapperSafeForDefaultValueOf = 3;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003819 static const int kAttachedToSharedFunctionInfo = 4;
3820 static const int kIsShared = 5;
Steve Block1e0659c2011-05-24 12:43:12 +01003821 static const int kHasPixelArrayElements = 6;
Steve Block6ded16b2010-05-10 14:33:55 +01003822
3823 // Layout of the default cache. It holds alternating name and code objects.
3824 static const int kCodeCacheEntrySize = 2;
3825 static const int kCodeCacheEntryNameOffset = 0;
3826 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003827
Iain Merrick75681382010-08-19 15:07:18 +01003828 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3829 kPointerFieldsEndOffset,
3830 kSize> BodyDescriptor;
3831
Steve Blocka7e24c12009-10-30 11:49:00 +00003832 private:
3833 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3834};
3835
3836
3837// An abstract superclass, a marker class really, for simple structure classes.
3838// It doesn't carry much functionality but allows struct classes to me
3839// identified in the type system.
3840class Struct: public HeapObject {
3841 public:
3842 inline void InitializeBody(int object_size);
3843 static inline Struct* cast(Object* that);
3844};
3845
3846
3847// Script describes a script which has been added to the VM.
3848class Script: public Struct {
3849 public:
3850 // Script types.
3851 enum Type {
3852 TYPE_NATIVE = 0,
3853 TYPE_EXTENSION = 1,
3854 TYPE_NORMAL = 2
3855 };
3856
3857 // Script compilation types.
3858 enum CompilationType {
3859 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003860 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003861 };
3862
3863 // [source]: the script source.
3864 DECL_ACCESSORS(source, Object)
3865
3866 // [name]: the script name.
3867 DECL_ACCESSORS(name, Object)
3868
3869 // [id]: the script id.
3870 DECL_ACCESSORS(id, Object)
3871
3872 // [line_offset]: script line offset in resource from where it was extracted.
3873 DECL_ACCESSORS(line_offset, Smi)
3874
3875 // [column_offset]: script column offset in resource from where it was
3876 // extracted.
3877 DECL_ACCESSORS(column_offset, Smi)
3878
3879 // [data]: additional data associated with this script.
3880 DECL_ACCESSORS(data, Object)
3881
3882 // [context_data]: context data for the context this script was compiled in.
3883 DECL_ACCESSORS(context_data, Object)
3884
3885 // [wrapper]: the wrapper cache.
3886 DECL_ACCESSORS(wrapper, Proxy)
3887
3888 // [type]: the script type.
3889 DECL_ACCESSORS(type, Smi)
3890
3891 // [compilation]: how the the script was compiled.
3892 DECL_ACCESSORS(compilation_type, Smi)
3893
Steve Blockd0582a62009-12-15 09:54:21 +00003894 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003895 DECL_ACCESSORS(line_ends, Object)
3896
Steve Blockd0582a62009-12-15 09:54:21 +00003897 // [eval_from_shared]: for eval scripts the shared funcion info for the
3898 // function from which eval was called.
3899 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003900
3901 // [eval_from_instructions_offset]: the instruction offset in the code for the
3902 // function from which eval was called where eval was called.
3903 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3904
3905 static inline Script* cast(Object* obj);
3906
Steve Block3ce2e202009-11-05 08:53:23 +00003907 // If script source is an external string, check that the underlying
3908 // resource is accessible. Otherwise, always return true.
3909 inline bool HasValidSource();
3910
Ben Murdochb0fe1622011-05-05 13:52:32 +01003911#ifdef OBJECT_PRINT
3912 inline void ScriptPrint() {
3913 ScriptPrint(stdout);
3914 }
3915 void ScriptPrint(FILE* out);
3916#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003917#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003918 void ScriptVerify();
3919#endif
3920
3921 static const int kSourceOffset = HeapObject::kHeaderSize;
3922 static const int kNameOffset = kSourceOffset + kPointerSize;
3923 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3924 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3925 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3926 static const int kContextOffset = kDataOffset + kPointerSize;
3927 static const int kWrapperOffset = kContextOffset + kPointerSize;
3928 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3929 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3930 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3931 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003932 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003933 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003934 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003935 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3936
3937 private:
3938 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3939};
3940
3941
Ben Murdochb0fe1622011-05-05 13:52:32 +01003942// List of builtin functions we want to identify to improve code
3943// generation.
3944//
3945// Each entry has a name of a global object property holding an object
3946// optionally followed by ".prototype", a name of a builtin function
3947// on the object (the one the id is set for), and a label.
3948//
3949// Installation of ids for the selected builtin functions is handled
3950// by the bootstrapper.
3951//
3952// NOTE: Order is important: math functions should be at the end of
3953// the list and MathFloor should be the first math function.
3954#define FUNCTIONS_WITH_ID_LIST(V) \
3955 V(Array.prototype, push, ArrayPush) \
3956 V(Array.prototype, pop, ArrayPop) \
3957 V(String.prototype, charCodeAt, StringCharCodeAt) \
3958 V(String.prototype, charAt, StringCharAt) \
3959 V(String, fromCharCode, StringFromCharCode) \
3960 V(Math, floor, MathFloor) \
3961 V(Math, round, MathRound) \
3962 V(Math, ceil, MathCeil) \
3963 V(Math, abs, MathAbs) \
3964 V(Math, log, MathLog) \
3965 V(Math, sin, MathSin) \
3966 V(Math, cos, MathCos) \
3967 V(Math, tan, MathTan) \
3968 V(Math, asin, MathASin) \
3969 V(Math, acos, MathACos) \
3970 V(Math, atan, MathATan) \
3971 V(Math, exp, MathExp) \
3972 V(Math, sqrt, MathSqrt) \
3973 V(Math, pow, MathPow)
3974
3975
3976enum BuiltinFunctionId {
3977#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
3978 k##name,
3979 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
3980#undef DECLARE_FUNCTION_ID
3981 // Fake id for a special case of Math.pow. Note, it continues the
3982 // list of math functions.
3983 kMathPowHalf,
3984 kFirstMathFunctionId = kMathFloor
3985};
3986
3987
Steve Blocka7e24c12009-10-30 11:49:00 +00003988// SharedFunctionInfo describes the JSFunction information that can be
3989// shared by multiple instances of the function.
3990class SharedFunctionInfo: public HeapObject {
3991 public:
3992 // [name]: Function name.
3993 DECL_ACCESSORS(name, Object)
3994
3995 // [code]: Function code.
3996 DECL_ACCESSORS(code, Code)
3997
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003998 // [scope_info]: Scope info.
3999 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
4000
Steve Blocka7e24c12009-10-30 11:49:00 +00004001 // [construct stub]: Code stub for constructing instances of this function.
4002 DECL_ACCESSORS(construct_stub, Code)
4003
Iain Merrick75681382010-08-19 15:07:18 +01004004 inline Code* unchecked_code();
4005
Steve Blocka7e24c12009-10-30 11:49:00 +00004006 // Returns if this function has been compiled to native code yet.
4007 inline bool is_compiled();
4008
4009 // [length]: The function length - usually the number of declared parameters.
4010 // Use up to 2^30 parameters.
4011 inline int length();
4012 inline void set_length(int value);
4013
4014 // [formal parameter count]: The declared number of parameters.
4015 inline int formal_parameter_count();
4016 inline void set_formal_parameter_count(int value);
4017
4018 // Set the formal parameter count so the function code will be
4019 // called without using argument adaptor frames.
4020 inline void DontAdaptArguments();
4021
4022 // [expected_nof_properties]: Expected number of properties for the function.
4023 inline int expected_nof_properties();
4024 inline void set_expected_nof_properties(int value);
4025
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004026 // Inobject slack tracking is the way to reclaim unused inobject space.
4027 //
4028 // The instance size is initially determined by adding some slack to
4029 // expected_nof_properties (to allow for a few extra properties added
4030 // after the constructor). There is no guarantee that the extra space
4031 // will not be wasted.
4032 //
4033 // Here is the algorithm to reclaim the unused inobject space:
4034 // - Detect the first constructor call for this SharedFunctionInfo.
4035 // When it happens enter the "in progress" state: remember the
4036 // constructor's initial_map and install a special construct stub that
4037 // counts constructor calls.
4038 // - While the tracking is in progress create objects filled with
4039 // one_pointer_filler_map instead of undefined_value. This way they can be
4040 // resized quickly and safely.
4041 // - Once enough (kGenerousAllocationCount) objects have been created
4042 // compute the 'slack' (traverse the map transition tree starting from the
4043 // initial_map and find the lowest value of unused_property_fields).
4044 // - Traverse the transition tree again and decrease the instance size
4045 // of every map. Existing objects will resize automatically (they are
4046 // filled with one_pointer_filler_map). All further allocations will
4047 // use the adjusted instance size.
4048 // - Decrease expected_nof_properties so that an allocations made from
4049 // another context will use the adjusted instance size too.
4050 // - Exit "in progress" state by clearing the reference to the initial_map
4051 // and setting the regular construct stub (generic or inline).
4052 //
4053 // The above is the main event sequence. Some special cases are possible
4054 // while the tracking is in progress:
4055 //
4056 // - GC occurs.
4057 // Check if the initial_map is referenced by any live objects (except this
4058 // SharedFunctionInfo). If it is, continue tracking as usual.
4059 // If it is not, clear the reference and reset the tracking state. The
4060 // tracking will be initiated again on the next constructor call.
4061 //
4062 // - The constructor is called from another context.
4063 // Immediately complete the tracking, perform all the necessary changes
4064 // to maps. This is necessary because there is no efficient way to track
4065 // multiple initial_maps.
4066 // Proceed to create an object in the current context (with the adjusted
4067 // size).
4068 //
4069 // - A different constructor function sharing the same SharedFunctionInfo is
4070 // called in the same context. This could be another closure in the same
4071 // context, or the first function could have been disposed.
4072 // This is handled the same way as the previous case.
4073 //
4074 // Important: inobject slack tracking is not attempted during the snapshot
4075 // creation.
4076
Ben Murdochf87a2032010-10-22 12:50:53 +01004077 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004078
4079 // [construction_count]: Counter for constructor calls made during
4080 // the tracking phase.
4081 inline int construction_count();
4082 inline void set_construction_count(int value);
4083
4084 // [initial_map]: initial map of the first function called as a constructor.
4085 // Saved for the duration of the tracking phase.
4086 // This is a weak link (GC resets it to undefined_value if no other live
4087 // object reference this map).
4088 DECL_ACCESSORS(initial_map, Object)
4089
4090 // True if the initial_map is not undefined and the countdown stub is
4091 // installed.
4092 inline bool IsInobjectSlackTrackingInProgress();
4093
4094 // Starts the tracking.
4095 // Stores the initial map and installs the countdown stub.
4096 // IsInobjectSlackTrackingInProgress is normally true after this call,
4097 // except when tracking have not been started (e.g. the map has no unused
4098 // properties or the snapshot is being built).
4099 void StartInobjectSlackTracking(Map* map);
4100
4101 // Completes the tracking.
4102 // IsInobjectSlackTrackingInProgress is false after this call.
4103 void CompleteInobjectSlackTracking();
4104
4105 // Clears the initial_map before the GC marking phase to ensure the reference
4106 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4107 void DetachInitialMap();
4108
4109 // Restores the link to the initial map after the GC marking phase.
4110 // IsInobjectSlackTrackingInProgress is true after this call.
4111 void AttachInitialMap(Map* map);
4112
4113 // False if there are definitely no live objects created from this function.
4114 // True if live objects _may_ exist (existence not guaranteed).
4115 // May go back from true to false after GC.
4116 inline bool live_objects_may_exist();
4117
4118 inline void set_live_objects_may_exist(bool value);
4119
Steve Blocka7e24c12009-10-30 11:49:00 +00004120 // [instance class name]: class name for instances.
4121 DECL_ACCESSORS(instance_class_name, Object)
4122
Steve Block6ded16b2010-05-10 14:33:55 +01004123 // [function data]: This field holds some additional data for function.
4124 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004125 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004126 // In the long run we don't want all functions to have this field but
4127 // we can fix that when we have a better model for storing hidden data
4128 // on objects.
4129 DECL_ACCESSORS(function_data, Object)
4130
Steve Block6ded16b2010-05-10 14:33:55 +01004131 inline bool IsApiFunction();
4132 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004133 inline bool HasBuiltinFunctionId();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004134 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004135
Steve Blocka7e24c12009-10-30 11:49:00 +00004136 // [script info]: Script from which the function originates.
4137 DECL_ACCESSORS(script, Object)
4138
Steve Block6ded16b2010-05-10 14:33:55 +01004139 // [num_literals]: Number of literals used by this function.
4140 inline int num_literals();
4141 inline void set_num_literals(int value);
4142
Steve Blocka7e24c12009-10-30 11:49:00 +00004143 // [start_position_and_type]: Field used to store both the source code
4144 // position, whether or not the function is a function expression,
4145 // and whether or not the function is a toplevel function. The two
4146 // least significants bit indicates whether the function is an
4147 // expression and the rest contains the source code position.
4148 inline int start_position_and_type();
4149 inline void set_start_position_and_type(int value);
4150
4151 // [debug info]: Debug information.
4152 DECL_ACCESSORS(debug_info, Object)
4153
4154 // [inferred name]: Name inferred from variable or property
4155 // assignment of this function. Used to facilitate debugging and
4156 // profiling of JavaScript code written in OO style, where almost
4157 // all functions are anonymous but are assigned to object
4158 // properties.
4159 DECL_ACCESSORS(inferred_name, String)
4160
Ben Murdochf87a2032010-10-22 12:50:53 +01004161 // The function's name if it is non-empty, otherwise the inferred name.
4162 String* DebugName();
4163
Steve Blocka7e24c12009-10-30 11:49:00 +00004164 // Position of the 'function' token in the script source.
4165 inline int function_token_position();
4166 inline void set_function_token_position(int function_token_position);
4167
4168 // Position of this function in the script source.
4169 inline int start_position();
4170 inline void set_start_position(int start_position);
4171
4172 // End position of this function in the script source.
4173 inline int end_position();
4174 inline void set_end_position(int end_position);
4175
4176 // Is this function a function expression in the source code.
4177 inline bool is_expression();
4178 inline void set_is_expression(bool value);
4179
4180 // Is this function a top-level function (scripts, evals).
4181 inline bool is_toplevel();
4182 inline void set_is_toplevel(bool value);
4183
4184 // Bit field containing various information collected by the compiler to
4185 // drive optimization.
4186 inline int compiler_hints();
4187 inline void set_compiler_hints(int value);
4188
Ben Murdochb0fe1622011-05-05 13:52:32 +01004189 // A counter used to determine when to stress the deoptimizer with a
4190 // deopt.
4191 inline Smi* deopt_counter();
4192 inline void set_deopt_counter(Smi* counter);
4193
Steve Blocka7e24c12009-10-30 11:49:00 +00004194 // Add information on assignments of the form this.x = ...;
4195 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004196 bool has_only_simple_this_property_assignments,
4197 FixedArray* this_property_assignments);
4198
4199 // Clear information on assignments of the form this.x = ...;
4200 void ClearThisPropertyAssignmentsInfo();
4201
4202 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004203 // this.x = y; where y is either a constant or refers to an argument.
4204 inline bool has_only_simple_this_property_assignments();
4205
Leon Clarked91b9f72010-01-27 17:25:45 +00004206 inline bool try_full_codegen();
4207 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00004208
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004209 // Indicates if this function can be lazy compiled.
4210 // This is used to determine if we can safely flush code from a function
4211 // when doing GC if we expect that the function will no longer be used.
4212 inline bool allows_lazy_compilation();
4213 inline void set_allows_lazy_compilation(bool flag);
4214
Iain Merrick75681382010-08-19 15:07:18 +01004215 // Indicates how many full GCs this function has survived with assigned
4216 // code object. Used to determine when it is relatively safe to flush
4217 // this code object and replace it with lazy compilation stub.
4218 // Age is reset when GC notices that the code object is referenced
4219 // from the stack or compilation cache.
4220 inline int code_age();
4221 inline void set_code_age(int age);
4222
Ben Murdochb0fe1622011-05-05 13:52:32 +01004223 // Indicates whether optimizations have been disabled for this
4224 // shared function info. If a function is repeatedly optimized or if
4225 // we cannot optimize the function we disable optimization to avoid
4226 // spending time attempting to optimize it again.
4227 inline bool optimization_disabled();
4228 inline void set_optimization_disabled(bool value);
4229
Steve Block1e0659c2011-05-24 12:43:12 +01004230 // Indicates whether the function is a strict mode function.
4231 inline bool strict_mode();
4232 inline void set_strict_mode(bool value);
4233
Ben Murdochb0fe1622011-05-05 13:52:32 +01004234 // Indicates whether or not the code in the shared function support
4235 // deoptimization.
4236 inline bool has_deoptimization_support();
4237
4238 // Enable deoptimization support through recompiled code.
4239 void EnableDeoptimizationSupport(Code* recompiled);
4240
4241 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4242 // code, returns whether it asserted (i.e., always true if assertions are
4243 // disabled).
4244 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004245
Andrei Popescu402d9372010-02-26 13:31:12 +00004246 // Check whether a inlined constructor can be generated with the given
4247 // prototype.
4248 bool CanGenerateInlineConstructor(Object* prototype);
4249
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004250 // Prevents further attempts to generate inline constructors.
4251 // To be called if generation failed for any reason.
4252 void ForbidInlineConstructor();
4253
Steve Blocka7e24c12009-10-30 11:49:00 +00004254 // For functions which only contains this property assignments this provides
4255 // access to the names for the properties assigned.
4256 DECL_ACCESSORS(this_property_assignments, Object)
4257 inline int this_property_assignments_count();
4258 inline void set_this_property_assignments_count(int value);
4259 String* GetThisPropertyAssignmentName(int index);
4260 bool IsThisPropertyAssignmentArgument(int index);
4261 int GetThisPropertyAssignmentArgument(int index);
4262 Object* GetThisPropertyAssignmentConstant(int index);
4263
4264 // [source code]: Source code for the function.
4265 bool HasSourceCode();
4266 Object* GetSourceCode();
4267
Ben Murdochb0fe1622011-05-05 13:52:32 +01004268 inline int opt_count();
4269 inline void set_opt_count(int opt_count);
4270
4271 // Source size of this function.
4272 int SourceSize();
4273
Steve Blocka7e24c12009-10-30 11:49:00 +00004274 // Calculate the instance size.
4275 int CalculateInstanceSize();
4276
4277 // Calculate the number of in-object properties.
4278 int CalculateInObjectProperties();
4279
4280 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004281 // Set max_length to -1 for unlimited length.
4282 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004283#ifdef OBJECT_PRINT
4284 inline void SharedFunctionInfoPrint() {
4285 SharedFunctionInfoPrint(stdout);
4286 }
4287 void SharedFunctionInfoPrint(FILE* out);
4288#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004289#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004290 void SharedFunctionInfoVerify();
4291#endif
4292
4293 // Casting.
4294 static inline SharedFunctionInfo* cast(Object* obj);
4295
4296 // Constants.
4297 static const int kDontAdaptArgumentsSentinel = -1;
4298
4299 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004300 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004301 static const int kNameOffset = HeapObject::kHeaderSize;
4302 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004303 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4304 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004305 static const int kInstanceClassNameOffset =
4306 kConstructStubOffset + kPointerSize;
4307 static const int kFunctionDataOffset =
4308 kInstanceClassNameOffset + kPointerSize;
4309 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4310 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4311 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004312 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004313 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004314 static const int kThisPropertyAssignmentsOffset =
4315 kInitialMapOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004316 static const int kDeoptCounterOffset =
4317 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004318#if V8_HOST_ARCH_32_BIT
4319 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004320 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004321 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004322 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4323 static const int kExpectedNofPropertiesOffset =
4324 kFormalParameterCountOffset + kPointerSize;
4325 static const int kNumLiteralsOffset =
4326 kExpectedNofPropertiesOffset + kPointerSize;
4327 static const int kStartPositionAndTypeOffset =
4328 kNumLiteralsOffset + kPointerSize;
4329 static const int kEndPositionOffset =
4330 kStartPositionAndTypeOffset + kPointerSize;
4331 static const int kFunctionTokenPositionOffset =
4332 kEndPositionOffset + kPointerSize;
4333 static const int kCompilerHintsOffset =
4334 kFunctionTokenPositionOffset + kPointerSize;
4335 static const int kThisPropertyAssignmentsCountOffset =
4336 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004337 static const int kOptCountOffset =
4338 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004339 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004340 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004341#else
4342 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004343 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004344 // garbage collections.
4345 // To avoid wasting space on 64-bit architectures we use
4346 // the following trick: we group integer fields into pairs
4347 // First integer in each pair is shifted left by 1.
4348 // By doing this we guarantee that LSB of each kPointerSize aligned
4349 // word is not set and thus this word cannot be treated as pointer
4350 // to HeapObject during old space traversal.
4351 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004352 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004353 static const int kFormalParameterCountOffset =
4354 kLengthOffset + kIntSize;
4355
Steve Blocka7e24c12009-10-30 11:49:00 +00004356 static const int kExpectedNofPropertiesOffset =
4357 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004358 static const int kNumLiteralsOffset =
4359 kExpectedNofPropertiesOffset + kIntSize;
4360
4361 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004362 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004363 static const int kStartPositionAndTypeOffset =
4364 kEndPositionOffset + kIntSize;
4365
4366 static const int kFunctionTokenPositionOffset =
4367 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004368 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004369 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004370
Steve Blocka7e24c12009-10-30 11:49:00 +00004371 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004372 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004373 static const int kOptCountOffset =
4374 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004375
Steve Block6ded16b2010-05-10 14:33:55 +01004376 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004377 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004378
4379#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004380
4381 // The construction counter for inobject slack tracking is stored in the
4382 // most significant byte of compiler_hints which is otherwise unused.
4383 // Its offset depends on the endian-ness of the architecture.
4384#if __BYTE_ORDER == __LITTLE_ENDIAN
4385 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4386#elif __BYTE_ORDER == __BIG_ENDIAN
4387 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4388#else
4389#error Unknown byte ordering
4390#endif
4391
Steve Block6ded16b2010-05-10 14:33:55 +01004392 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004393
Iain Merrick75681382010-08-19 15:07:18 +01004394 typedef FixedBodyDescriptor<kNameOffset,
4395 kThisPropertyAssignmentsOffset + kPointerSize,
4396 kSize> BodyDescriptor;
4397
Steve Blocka7e24c12009-10-30 11:49:00 +00004398 // Bit positions in start_position_and_type.
4399 // The source code start position is in the 30 most significant bits of
4400 // the start_position_and_type field.
4401 static const int kIsExpressionBit = 0;
4402 static const int kIsTopLevelBit = 1;
4403 static const int kStartPositionShift = 2;
4404 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4405
4406 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00004407 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00004408 static const int kTryFullCodegen = 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004409 static const int kAllowLazyCompilation = 2;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004410 static const int kLiveObjectsMayExist = 3;
4411 static const int kCodeAgeShift = 4;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004412 static const int kCodeAgeMask = 0x7;
4413 static const int kOptimizationDisabled = 7;
Steve Block1e0659c2011-05-24 12:43:12 +01004414 static const int kStrictModeFunction = 8;
Steve Blocka7e24c12009-10-30 11:49:00 +00004415
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004416 private:
4417#if V8_HOST_ARCH_32_BIT
4418 // On 32 bit platforms, compiler hints is a smi.
4419 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4420 static const int kCompilerHintsSize = kPointerSize;
4421#else
4422 // On 64 bit platforms, compiler hints is not a smi, see comment above.
4423 static const int kCompilerHintsSmiTagSize = 0;
4424 static const int kCompilerHintsSize = kIntSize;
4425#endif
4426
4427 public:
4428 // Constants for optimizing codegen for strict mode function tests.
4429 // Allows to use byte-widgh instructions.
4430 static const int kStrictModeBitWithinByte =
4431 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4432
4433#if __BYTE_ORDER == __LITTLE_ENDIAN
4434 static const int kStrictModeByteOffset = kCompilerHintsOffset +
4435 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
4436#elif __BYTE_ORDER == __BIG_ENDIAN
4437 static const int kStrictModeByteOffset = kCompilerHintsOffset +
4438 (kCompilerHintsSize - 1) -
4439 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
4440#else
4441#error Unknown byte ordering
4442#endif
4443
4444 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00004445 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4446};
4447
4448
4449// JSFunction describes JavaScript functions.
4450class JSFunction: public JSObject {
4451 public:
4452 // [prototype_or_initial_map]:
4453 DECL_ACCESSORS(prototype_or_initial_map, Object)
4454
4455 // [shared_function_info]: The information about the function that
4456 // can be shared by instances.
4457 DECL_ACCESSORS(shared, SharedFunctionInfo)
4458
Iain Merrick75681382010-08-19 15:07:18 +01004459 inline SharedFunctionInfo* unchecked_shared();
4460
Steve Blocka7e24c12009-10-30 11:49:00 +00004461 // [context]: The context for this function.
4462 inline Context* context();
4463 inline Object* unchecked_context();
4464 inline void set_context(Object* context);
4465
4466 // [code]: The generated code object for this function. Executed
4467 // when the function is invoked, e.g. foo() or new foo(). See
4468 // [[Call]] and [[Construct]] description in ECMA-262, section
4469 // 8.6.2, page 27.
4470 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004471 inline void set_code(Code* code);
4472 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004473
Iain Merrick75681382010-08-19 15:07:18 +01004474 inline Code* unchecked_code();
4475
Steve Blocka7e24c12009-10-30 11:49:00 +00004476 // Tells whether this function is builtin.
4477 inline bool IsBuiltin();
4478
Ben Murdochb0fe1622011-05-05 13:52:32 +01004479 // Tells whether or not the function needs arguments adaption.
4480 inline bool NeedsArgumentsAdaption();
4481
4482 // Tells whether or not this function has been optimized.
4483 inline bool IsOptimized();
4484
4485 // Mark this function for lazy recompilation. The function will be
4486 // recompiled the next time it is executed.
4487 void MarkForLazyRecompilation();
4488
4489 // Tells whether or not the function is already marked for lazy
4490 // recompilation.
4491 inline bool IsMarkedForLazyRecompilation();
4492
4493 // Compute a hash code for the source code of this function.
4494 uint32_t SourceHash();
4495
4496 // Check whether or not this function is inlineable.
4497 bool IsInlineable();
4498
Steve Blocka7e24c12009-10-30 11:49:00 +00004499 // [literals]: Fixed array holding the materialized literals.
4500 //
4501 // If the function contains object, regexp or array literals, the
4502 // literals array prefix contains the object, regexp, and array
4503 // function to be used when creating these literals. This is
4504 // necessary so that we do not dynamically lookup the object, regexp
4505 // or array functions. Performing a dynamic lookup, we might end up
4506 // using the functions from a new context that we should not have
4507 // access to.
4508 DECL_ACCESSORS(literals, FixedArray)
4509
4510 // The initial map for an object created by this constructor.
4511 inline Map* initial_map();
4512 inline void set_initial_map(Map* value);
4513 inline bool has_initial_map();
4514
4515 // Get and set the prototype property on a JSFunction. If the
4516 // function has an initial map the prototype is set on the initial
4517 // map. Otherwise, the prototype is put in the initial map field
4518 // until an initial map is needed.
4519 inline bool has_prototype();
4520 inline bool has_instance_prototype();
4521 inline Object* prototype();
4522 inline Object* instance_prototype();
4523 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07004524 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004525
Steve Block6ded16b2010-05-10 14:33:55 +01004526 // After prototype is removed, it will not be created when accessed, and
4527 // [[Construct]] from this function will not be allowed.
4528 Object* RemovePrototype();
4529 inline bool should_have_prototype();
4530
Steve Blocka7e24c12009-10-30 11:49:00 +00004531 // Accessor for this function's initial map's [[class]]
4532 // property. This is primarily used by ECMA native functions. This
4533 // method sets the class_name field of this function's initial map
4534 // to a given value. It creates an initial map if this function does
4535 // not have one. Note that this method does not copy the initial map
4536 // if it has one already, but simply replaces it with the new value.
4537 // Instances created afterwards will have a map whose [[class]] is
4538 // set to 'value', but there is no guarantees on instances created
4539 // before.
4540 Object* SetInstanceClassName(String* name);
4541
4542 // Returns if this function has been compiled to native code yet.
4543 inline bool is_compiled();
4544
Ben Murdochb0fe1622011-05-05 13:52:32 +01004545 // [next_function_link]: Field for linking functions. This list is treated as
4546 // a weak list by the GC.
4547 DECL_ACCESSORS(next_function_link, Object)
4548
4549 // Prints the name of the function using PrintF.
4550 inline void PrintName() {
4551 PrintName(stdout);
4552 }
4553 void PrintName(FILE* out);
4554
Steve Blocka7e24c12009-10-30 11:49:00 +00004555 // Casting.
4556 static inline JSFunction* cast(Object* obj);
4557
Steve Block791712a2010-08-27 10:21:07 +01004558 // Iterates the objects, including code objects indirectly referenced
4559 // through pointers to the first instruction in the code object.
4560 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
4561
Steve Blocka7e24c12009-10-30 11:49:00 +00004562 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004563#ifdef OBJECT_PRINT
4564 inline void JSFunctionPrint() {
4565 JSFunctionPrint(stdout);
4566 }
4567 void JSFunctionPrint(FILE* out);
4568#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004569#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004570 void JSFunctionVerify();
4571#endif
4572
4573 // Returns the number of allocated literals.
4574 inline int NumberOfLiterals();
4575
4576 // Retrieve the global context from a function's literal array.
4577 static Context* GlobalContextFromLiterals(FixedArray* literals);
4578
Ben Murdochb0fe1622011-05-05 13:52:32 +01004579 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
4580 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01004581 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01004582 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01004583 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004584 static const int kSharedFunctionInfoOffset =
4585 kPrototypeOrInitialMapOffset + kPointerSize;
4586 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
4587 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004588 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
4589 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
4590 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004591
4592 // Layout of the literals array.
4593 static const int kLiteralsPrefixSize = 1;
4594 static const int kLiteralGlobalContextIndex = 0;
4595 private:
4596 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
4597};
4598
4599
4600// JSGlobalProxy's prototype must be a JSGlobalObject or null,
4601// and the prototype is hidden. JSGlobalProxy always delegates
4602// property accesses to its prototype if the prototype is not null.
4603//
4604// A JSGlobalProxy can be reinitialized which will preserve its identity.
4605//
4606// Accessing a JSGlobalProxy requires security check.
4607
4608class JSGlobalProxy : public JSObject {
4609 public:
4610 // [context]: the owner global context of this proxy object.
4611 // It is null value if this object is not used by any context.
4612 DECL_ACCESSORS(context, Object)
4613
4614 // Casting.
4615 static inline JSGlobalProxy* cast(Object* obj);
4616
4617 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004618#ifdef OBJECT_PRINT
4619 inline void JSGlobalProxyPrint() {
4620 JSGlobalProxyPrint(stdout);
4621 }
4622 void JSGlobalProxyPrint(FILE* out);
4623#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004624#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004625 void JSGlobalProxyVerify();
4626#endif
4627
4628 // Layout description.
4629 static const int kContextOffset = JSObject::kHeaderSize;
4630 static const int kSize = kContextOffset + kPointerSize;
4631
4632 private:
4633
4634 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
4635};
4636
4637
4638// Forward declaration.
4639class JSBuiltinsObject;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004640class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00004641
4642// Common super class for JavaScript global objects and the special
4643// builtins global objects.
4644class GlobalObject: public JSObject {
4645 public:
4646 // [builtins]: the object holding the runtime routines written in JS.
4647 DECL_ACCESSORS(builtins, JSBuiltinsObject)
4648
4649 // [global context]: the global context corresponding to this global object.
4650 DECL_ACCESSORS(global_context, Context)
4651
4652 // [global receiver]: the global receiver object of the context
4653 DECL_ACCESSORS(global_receiver, JSObject)
4654
4655 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004656 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004657
John Reck59135872010-11-02 12:39:01 -07004658 // This is like GetProperty, but is used when you know the lookup won't fail
4659 // by throwing an exception. This is for the debug and builtins global
4660 // objects, where it is known which properties can be expected to be present
4661 // on the object.
4662 Object* GetPropertyNoExceptionThrown(String* key) {
4663 Object* answer = GetProperty(key)->ToObjectUnchecked();
4664 return answer;
4665 }
4666
Steve Blocka7e24c12009-10-30 11:49:00 +00004667 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07004668 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00004669
4670 // Casting.
4671 static inline GlobalObject* cast(Object* obj);
4672
4673 // Layout description.
4674 static const int kBuiltinsOffset = JSObject::kHeaderSize;
4675 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
4676 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
4677 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
4678
4679 private:
4680 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4681
4682 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
4683};
4684
4685
4686// JavaScript global object.
4687class JSGlobalObject: public GlobalObject {
4688 public:
4689
4690 // Casting.
4691 static inline JSGlobalObject* cast(Object* obj);
4692
4693 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004694#ifdef OBJECT_PRINT
4695 inline void JSGlobalObjectPrint() {
4696 JSGlobalObjectPrint(stdout);
4697 }
4698 void JSGlobalObjectPrint(FILE* out);
4699#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004700#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004701 void JSGlobalObjectVerify();
4702#endif
4703
4704 // Layout description.
4705 static const int kSize = GlobalObject::kHeaderSize;
4706
4707 private:
4708 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
4709};
4710
4711
4712// Builtins global object which holds the runtime routines written in
4713// JavaScript.
4714class JSBuiltinsObject: public GlobalObject {
4715 public:
4716 // Accessors for the runtime routines written in JavaScript.
4717 inline Object* javascript_builtin(Builtins::JavaScript id);
4718 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
4719
Steve Block6ded16b2010-05-10 14:33:55 +01004720 // Accessors for code of the runtime routines written in JavaScript.
4721 inline Code* javascript_builtin_code(Builtins::JavaScript id);
4722 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
4723
Steve Blocka7e24c12009-10-30 11:49:00 +00004724 // Casting.
4725 static inline JSBuiltinsObject* cast(Object* obj);
4726
4727 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004728#ifdef OBJECT_PRINT
4729 inline void JSBuiltinsObjectPrint() {
4730 JSBuiltinsObjectPrint(stdout);
4731 }
4732 void JSBuiltinsObjectPrint(FILE* out);
4733#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004734#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004735 void JSBuiltinsObjectVerify();
4736#endif
4737
4738 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01004739 // room for two pointers per runtime routine written in javascript
4740 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00004741 static const int kJSBuiltinsCount = Builtins::id_count;
4742 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004743 static const int kJSBuiltinsCodeOffset =
4744 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004745 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01004746 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
4747
4748 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
4749 return kJSBuiltinsOffset + id * kPointerSize;
4750 }
4751
4752 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
4753 return kJSBuiltinsCodeOffset + id * kPointerSize;
4754 }
4755
Steve Blocka7e24c12009-10-30 11:49:00 +00004756 private:
4757 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
4758};
4759
4760
4761// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
4762class JSValue: public JSObject {
4763 public:
4764 // [value]: the object being wrapped.
4765 DECL_ACCESSORS(value, Object)
4766
4767 // Casting.
4768 static inline JSValue* cast(Object* obj);
4769
4770 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004771#ifdef OBJECT_PRINT
4772 inline void JSValuePrint() {
4773 JSValuePrint(stdout);
4774 }
4775 void JSValuePrint(FILE* out);
4776#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004777#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004778 void JSValueVerify();
4779#endif
4780
4781 // Layout description.
4782 static const int kValueOffset = JSObject::kHeaderSize;
4783 static const int kSize = kValueOffset + kPointerSize;
4784
4785 private:
4786 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
4787};
4788
Steve Block1e0659c2011-05-24 12:43:12 +01004789
4790// Representation of message objects used for error reporting through
4791// the API. The messages are formatted in JavaScript so this object is
4792// a real JavaScript object. The information used for formatting the
4793// error messages are not directly accessible from JavaScript to
4794// prevent leaking information to user code called during error
4795// formatting.
4796class JSMessageObject: public JSObject {
4797 public:
4798 // [type]: the type of error message.
4799 DECL_ACCESSORS(type, String)
4800
4801 // [arguments]: the arguments for formatting the error message.
4802 DECL_ACCESSORS(arguments, JSArray)
4803
4804 // [script]: the script from which the error message originated.
4805 DECL_ACCESSORS(script, Object)
4806
4807 // [stack_trace]: the stack trace for this error message.
4808 DECL_ACCESSORS(stack_trace, Object)
4809
4810 // [stack_frames]: an array of stack frames for this error object.
4811 DECL_ACCESSORS(stack_frames, Object)
4812
4813 // [start_position]: the start position in the script for the error message.
4814 inline int start_position();
4815 inline void set_start_position(int value);
4816
4817 // [end_position]: the end position in the script for the error message.
4818 inline int end_position();
4819 inline void set_end_position(int value);
4820
4821 // Casting.
4822 static inline JSMessageObject* cast(Object* obj);
4823
4824 // Dispatched behavior.
4825#ifdef OBJECT_PRINT
4826 inline void JSMessageObjectPrint() {
4827 JSMessageObjectPrint(stdout);
4828 }
4829 void JSMessageObjectPrint(FILE* out);
4830#endif
4831#ifdef DEBUG
4832 void JSMessageObjectVerify();
4833#endif
4834
4835 // Layout description.
4836 static const int kTypeOffset = JSObject::kHeaderSize;
4837 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
4838 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
4839 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
4840 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
4841 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
4842 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
4843 static const int kSize = kEndPositionOffset + kPointerSize;
4844
4845 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
4846 kStackFramesOffset + kPointerSize,
4847 kSize> BodyDescriptor;
4848};
4849
4850
Steve Blocka7e24c12009-10-30 11:49:00 +00004851// Regular expressions
4852// The regular expression holds a single reference to a FixedArray in
4853// the kDataOffset field.
4854// The FixedArray contains the following data:
4855// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
4856// - reference to the original source string
4857// - reference to the original flag string
4858// If it is an atom regexp
4859// - a reference to a literal string to search for
4860// If it is an irregexp regexp:
4861// - a reference to code for ASCII inputs (bytecode or compiled).
4862// - a reference to code for UC16 inputs (bytecode or compiled).
4863// - max number of registers used by irregexp implementations.
4864// - number of capture registers (output values) of the regexp.
4865class JSRegExp: public JSObject {
4866 public:
4867 // Meaning of Type:
4868 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
4869 // ATOM: A simple string to match against using an indexOf operation.
4870 // IRREGEXP: Compiled with Irregexp.
4871 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
4872 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
4873 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
4874
4875 class Flags {
4876 public:
4877 explicit Flags(uint32_t value) : value_(value) { }
4878 bool is_global() { return (value_ & GLOBAL) != 0; }
4879 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
4880 bool is_multiline() { return (value_ & MULTILINE) != 0; }
4881 uint32_t value() { return value_; }
4882 private:
4883 uint32_t value_;
4884 };
4885
4886 DECL_ACCESSORS(data, Object)
4887
4888 inline Type TypeTag();
4889 inline int CaptureCount();
4890 inline Flags GetFlags();
4891 inline String* Pattern();
4892 inline Object* DataAt(int index);
4893 // Set implementation data after the object has been prepared.
4894 inline void SetDataAt(int index, Object* value);
4895 static int code_index(bool is_ascii) {
4896 if (is_ascii) {
4897 return kIrregexpASCIICodeIndex;
4898 } else {
4899 return kIrregexpUC16CodeIndex;
4900 }
4901 }
4902
4903 static inline JSRegExp* cast(Object* obj);
4904
4905 // Dispatched behavior.
4906#ifdef DEBUG
4907 void JSRegExpVerify();
4908#endif
4909
4910 static const int kDataOffset = JSObject::kHeaderSize;
4911 static const int kSize = kDataOffset + kPointerSize;
4912
4913 // Indices in the data array.
4914 static const int kTagIndex = 0;
4915 static const int kSourceIndex = kTagIndex + 1;
4916 static const int kFlagsIndex = kSourceIndex + 1;
4917 static const int kDataIndex = kFlagsIndex + 1;
4918 // The data fields are used in different ways depending on the
4919 // value of the tag.
4920 // Atom regexps (literal strings).
4921 static const int kAtomPatternIndex = kDataIndex;
4922
4923 static const int kAtomDataSize = kAtomPatternIndex + 1;
4924
4925 // Irregexp compiled code or bytecode for ASCII. If compilation
4926 // fails, this fields hold an exception object that should be
4927 // thrown if the regexp is used again.
4928 static const int kIrregexpASCIICodeIndex = kDataIndex;
4929 // Irregexp compiled code or bytecode for UC16. If compilation
4930 // fails, this fields hold an exception object that should be
4931 // thrown if the regexp is used again.
4932 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
4933 // Maximal number of registers used by either ASCII or UC16.
4934 // Only used to check that there is enough stack space
4935 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
4936 // Number of captures in the compiled regexp.
4937 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
4938
4939 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00004940
4941 // Offsets directly into the data fixed array.
4942 static const int kDataTagOffset =
4943 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
4944 static const int kDataAsciiCodeOffset =
4945 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00004946 static const int kDataUC16CodeOffset =
4947 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00004948 static const int kIrregexpCaptureCountOffset =
4949 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004950
4951 // In-object fields.
4952 static const int kSourceFieldIndex = 0;
4953 static const int kGlobalFieldIndex = 1;
4954 static const int kIgnoreCaseFieldIndex = 2;
4955 static const int kMultilineFieldIndex = 3;
4956 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01004957 static const int kInObjectFieldCount = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00004958};
4959
4960
4961class CompilationCacheShape {
4962 public:
4963 static inline bool IsMatch(HashTableKey* key, Object* value) {
4964 return key->IsMatch(value);
4965 }
4966
4967 static inline uint32_t Hash(HashTableKey* key) {
4968 return key->Hash();
4969 }
4970
4971 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4972 return key->HashForObject(object);
4973 }
4974
John Reck59135872010-11-02 12:39:01 -07004975 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004976 return key->AsObject();
4977 }
4978
4979 static const int kPrefixSize = 0;
4980 static const int kEntrySize = 2;
4981};
4982
Steve Block3ce2e202009-11-05 08:53:23 +00004983
Steve Blocka7e24c12009-10-30 11:49:00 +00004984class CompilationCacheTable: public HashTable<CompilationCacheShape,
4985 HashTableKey*> {
4986 public:
4987 // Find cached value for a string key, otherwise return null.
4988 Object* Lookup(String* src);
Steve Block1e0659c2011-05-24 12:43:12 +01004989 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00004990 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07004991 MaybeObject* Put(String* src, Object* value);
Steve Block1e0659c2011-05-24 12:43:12 +01004992 MaybeObject* PutEval(String* src,
4993 Context* context,
4994 SharedFunctionInfo* value);
John Reck59135872010-11-02 12:39:01 -07004995 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004996
Ben Murdochb0fe1622011-05-05 13:52:32 +01004997 // Remove given value from cache.
4998 void Remove(Object* value);
4999
Steve Blocka7e24c12009-10-30 11:49:00 +00005000 static inline CompilationCacheTable* cast(Object* obj);
5001
5002 private:
5003 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
5004};
5005
5006
Steve Block6ded16b2010-05-10 14:33:55 +01005007class CodeCache: public Struct {
5008 public:
5009 DECL_ACCESSORS(default_cache, FixedArray)
5010 DECL_ACCESSORS(normal_type_cache, Object)
5011
5012 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07005013 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005014
5015 // Lookup code object in the cache. Returns code object if found and undefined
5016 // if not.
5017 Object* Lookup(String* name, Code::Flags flags);
5018
5019 // Get the internal index of a code object in the cache. Returns -1 if the
5020 // code object is not in that cache. This index can be used to later call
5021 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5022 // RemoveByIndex.
5023 int GetIndex(Object* name, Code* code);
5024
5025 // Remove an object from the cache with the provided internal index.
5026 void RemoveByIndex(Object* name, Code* code, int index);
5027
5028 static inline CodeCache* cast(Object* obj);
5029
Ben Murdochb0fe1622011-05-05 13:52:32 +01005030#ifdef OBJECT_PRINT
5031 inline void CodeCachePrint() {
5032 CodeCachePrint(stdout);
5033 }
5034 void CodeCachePrint(FILE* out);
5035#endif
Steve Block6ded16b2010-05-10 14:33:55 +01005036#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01005037 void CodeCacheVerify();
5038#endif
5039
5040 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5041 static const int kNormalTypeCacheOffset =
5042 kDefaultCacheOffset + kPointerSize;
5043 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5044
5045 private:
John Reck59135872010-11-02 12:39:01 -07005046 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5047 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005048 Object* LookupDefaultCache(String* name, Code::Flags flags);
5049 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5050
5051 // Code cache layout of the default cache. Elements are alternating name and
5052 // code objects for non normal load/store/call IC's.
5053 static const int kCodeCacheEntrySize = 2;
5054 static const int kCodeCacheEntryNameOffset = 0;
5055 static const int kCodeCacheEntryCodeOffset = 1;
5056
5057 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5058};
5059
5060
5061class CodeCacheHashTableShape {
5062 public:
5063 static inline bool IsMatch(HashTableKey* key, Object* value) {
5064 return key->IsMatch(value);
5065 }
5066
5067 static inline uint32_t Hash(HashTableKey* key) {
5068 return key->Hash();
5069 }
5070
5071 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5072 return key->HashForObject(object);
5073 }
5074
John Reck59135872010-11-02 12:39:01 -07005075 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01005076 return key->AsObject();
5077 }
5078
5079 static const int kPrefixSize = 0;
5080 static const int kEntrySize = 2;
5081};
5082
5083
5084class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5085 HashTableKey*> {
5086 public:
5087 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005088 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005089
5090 int GetIndex(String* name, Code::Flags flags);
5091 void RemoveByIndex(int index);
5092
5093 static inline CodeCacheHashTable* cast(Object* obj);
5094
5095 // Initial size of the fixed array backing the hash table.
5096 static const int kInitialSize = 64;
5097
5098 private:
5099 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5100};
5101
5102
Steve Blocka7e24c12009-10-30 11:49:00 +00005103enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5104enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5105
5106
5107class StringHasher {
5108 public:
5109 inline StringHasher(int length);
5110
5111 // Returns true if the hash of this string can be computed without
5112 // looking at the contents.
5113 inline bool has_trivial_hash();
5114
5115 // Add a character to the hash and update the array index calculation.
5116 inline void AddCharacter(uc32 c);
5117
5118 // Adds a character to the hash but does not update the array index
5119 // calculation. This can only be called when it has been verified
5120 // that the input is not an array index.
5121 inline void AddCharacterNoIndex(uc32 c);
5122
5123 // Returns the value to store in the hash field of a string with
5124 // the given length and contents.
5125 uint32_t GetHashField();
5126
5127 // Returns true if the characters seen so far make up a legal array
5128 // index.
5129 bool is_array_index() { return is_array_index_; }
5130
5131 bool is_valid() { return is_valid_; }
5132
5133 void invalidate() { is_valid_ = false; }
5134
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005135 // Calculated hash value for a string consisting of 1 to
5136 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5137 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005138 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005139
Steve Blocka7e24c12009-10-30 11:49:00 +00005140 private:
5141
5142 uint32_t array_index() {
5143 ASSERT(is_array_index());
5144 return array_index_;
5145 }
5146
5147 inline uint32_t GetHash();
5148
5149 int length_;
5150 uint32_t raw_running_hash_;
5151 uint32_t array_index_;
5152 bool is_array_index_;
5153 bool is_first_char_;
5154 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00005155 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00005156};
5157
5158
5159// The characteristics of a string are stored in its map. Retrieving these
5160// few bits of information is moderately expensive, involving two memory
5161// loads where the second is dependent on the first. To improve efficiency
5162// the shape of the string is given its own class so that it can be retrieved
5163// once and used for several string operations. A StringShape is small enough
5164// to be passed by value and is immutable, but be aware that flattening a
5165// string can potentially alter its shape. Also be aware that a GC caused by
5166// something else can alter the shape of a string due to ConsString
5167// shortcutting. Keeping these restrictions in mind has proven to be error-
5168// prone and so we no longer put StringShapes in variables unless there is a
5169// concrete performance benefit at that particular point in the code.
5170class StringShape BASE_EMBEDDED {
5171 public:
5172 inline explicit StringShape(String* s);
5173 inline explicit StringShape(Map* s);
5174 inline explicit StringShape(InstanceType t);
5175 inline bool IsSequential();
5176 inline bool IsExternal();
5177 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00005178 inline bool IsExternalAscii();
5179 inline bool IsExternalTwoByte();
5180 inline bool IsSequentialAscii();
5181 inline bool IsSequentialTwoByte();
5182 inline bool IsSymbol();
5183 inline StringRepresentationTag representation_tag();
5184 inline uint32_t full_representation_tag();
5185 inline uint32_t size_tag();
5186#ifdef DEBUG
5187 inline uint32_t type() { return type_; }
5188 inline void invalidate() { valid_ = false; }
5189 inline bool valid() { return valid_; }
5190#else
5191 inline void invalidate() { }
5192#endif
5193 private:
5194 uint32_t type_;
5195#ifdef DEBUG
5196 inline void set_valid() { valid_ = true; }
5197 bool valid_;
5198#else
5199 inline void set_valid() { }
5200#endif
5201};
5202
5203
5204// The String abstract class captures JavaScript string values:
5205//
5206// Ecma-262:
5207// 4.3.16 String Value
5208// A string value is a member of the type String and is a finite
5209// ordered sequence of zero or more 16-bit unsigned integer values.
5210//
5211// All string values have a length field.
5212class String: public HeapObject {
5213 public:
5214 // Get and set the length of the string.
5215 inline int length();
5216 inline void set_length(int value);
5217
Steve Blockd0582a62009-12-15 09:54:21 +00005218 // Get and set the hash field of the string.
5219 inline uint32_t hash_field();
5220 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005221
5222 inline bool IsAsciiRepresentation();
5223 inline bool IsTwoByteRepresentation();
5224
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005225 // Returns whether this string has ascii chars, i.e. all of them can
5226 // be ascii encoded. This might be the case even if the string is
5227 // two-byte. Such strings may appear when the embedder prefers
5228 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01005229 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005230 // NOTE: this should be considered only a hint. False negatives are
5231 // possible.
5232 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005233
Steve Blocka7e24c12009-10-30 11:49:00 +00005234 // Get and set individual two byte chars in the string.
5235 inline void Set(int index, uint16_t value);
5236 // Get individual two byte char in the string. Repeated calls
5237 // to this method are not efficient unless the string is flat.
5238 inline uint16_t Get(int index);
5239
Leon Clarkef7060e22010-06-03 12:02:55 +01005240 // Try to flatten the string. Checks first inline to see if it is
5241 // necessary. Does nothing if the string is not a cons string.
5242 // Flattening allocates a sequential string with the same data as
5243 // the given string and mutates the cons string to a degenerate
5244 // form, where the first component is the new sequential string and
5245 // the second component is the empty string. If allocation fails,
5246 // this function returns a failure. If flattening succeeds, this
5247 // function returns the sequential string that is now the first
5248 // component of the cons string.
5249 //
5250 // Degenerate cons strings are handled specially by the garbage
5251 // collector (see IsShortcutCandidate).
5252 //
5253 // Use FlattenString from Handles.cc to flatten even in case an
5254 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07005255 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005256
Leon Clarkef7060e22010-06-03 12:02:55 +01005257 // Convenience function. Has exactly the same behavior as
5258 // TryFlatten(), except in the case of failure returns the original
5259 // string.
5260 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5261
Steve Blocka7e24c12009-10-30 11:49:00 +00005262 Vector<const char> ToAsciiVector();
5263 Vector<const uc16> ToUC16Vector();
5264
5265 // Mark the string as an undetectable object. It only applies to
5266 // ascii and two byte string types.
5267 bool MarkAsUndetectable();
5268
Steve Blockd0582a62009-12-15 09:54:21 +00005269 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07005270 MUST_USE_RESULT MaybeObject* SubString(int from,
5271 int to,
5272 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005273
5274 // String equality operations.
5275 inline bool Equals(String* other);
5276 bool IsEqualTo(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +01005277 bool IsAsciiEqualTo(Vector<const char> str);
5278 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00005279
5280 // Return a UTF8 representation of the string. The string is null
5281 // terminated but may optionally contain nulls. Length is returned
5282 // in length_output if length_output is not a null pointer The string
5283 // should be nearly flat, otherwise the performance of this method may
5284 // be very slow (quadratic in the length). Setting robustness_flag to
5285 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5286 // handles unexpected data without causing assert failures and it does not
5287 // do any heap allocations. This is useful when printing stack traces.
5288 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5289 RobustnessFlag robustness_flag,
5290 int offset,
5291 int length,
5292 int* length_output = 0);
5293 SmartPointer<char> ToCString(
5294 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5295 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5296 int* length_output = 0);
5297
5298 int Utf8Length();
5299
5300 // Return a 16 bit Unicode representation of the string.
5301 // The string should be nearly flat, otherwise the performance of
5302 // of this method may be very bad. Setting robustness_flag to
5303 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5304 // handles unexpected data without causing assert failures and it does not
5305 // do any heap allocations. This is useful when printing stack traces.
5306 SmartPointer<uc16> ToWideCString(
5307 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
5308
5309 // Tells whether the hash code has been computed.
5310 inline bool HasHashCode();
5311
5312 // Returns a hash value used for the property table
5313 inline uint32_t Hash();
5314
Steve Blockd0582a62009-12-15 09:54:21 +00005315 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5316 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005317
5318 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5319 uint32_t* index,
5320 int length);
5321
5322 // Externalization.
5323 bool MakeExternal(v8::String::ExternalStringResource* resource);
5324 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5325
5326 // Conversion.
5327 inline bool AsArrayIndex(uint32_t* index);
5328
5329 // Casting.
5330 static inline String* cast(Object* obj);
5331
5332 void PrintOn(FILE* out);
5333
5334 // For use during stack traces. Performs rudimentary sanity check.
5335 bool LooksValid();
5336
5337 // Dispatched behavior.
5338 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005339#ifdef OBJECT_PRINT
5340 inline void StringPrint() {
5341 StringPrint(stdout);
5342 }
5343 void StringPrint(FILE* out);
5344#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005345#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005346 void StringVerify();
5347#endif
5348 inline bool IsFlat();
5349
5350 // Layout description.
5351 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005352 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005353 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005354
Steve Blockd0582a62009-12-15 09:54:21 +00005355 // Maximum number of characters to consider when trying to convert a string
5356 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00005357 static const int kMaxArrayIndexSize = 10;
5358
5359 // Max ascii char code.
5360 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
5361 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
5362 static const int kMaxUC16CharCode = 0xffff;
5363
Steve Blockd0582a62009-12-15 09:54:21 +00005364 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00005365 static const int kMinNonFlatLength = 13;
5366
5367 // Mask constant for checking if a string has a computed hash code
5368 // and if it is an array index. The least significant bit indicates
5369 // whether a hash code has been computed. If the hash code has been
5370 // computed the 2nd bit tells whether the string can be used as an
5371 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005372 static const int kHashNotComputedMask = 1;
5373 static const int kIsNotArrayIndexMask = 1 << 1;
5374 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00005375
Steve Blockd0582a62009-12-15 09:54:21 +00005376 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005377 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00005378
Steve Blocka7e24c12009-10-30 11:49:00 +00005379 // Array index strings this short can keep their index in the hash
5380 // field.
5381 static const int kMaxCachedArrayIndexLength = 7;
5382
Steve Blockd0582a62009-12-15 09:54:21 +00005383 // For strings which are array indexes the hash value has the string length
5384 // mixed into the hash, mainly to avoid a hash value of zero which would be
5385 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005386 static const int kArrayIndexValueBits = 24;
5387 static const int kArrayIndexLengthBits =
5388 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
5389
5390 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01005391 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005392
5393 static const int kArrayIndexHashLengthShift =
5394 kArrayIndexValueBits + kNofHashBitFields;
5395
Steve Blockd0582a62009-12-15 09:54:21 +00005396 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005397
5398 static const int kArrayIndexValueMask =
5399 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
5400
5401 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
5402 // could use a mask to test if the length of string is less than or equal to
5403 // kMaxCachedArrayIndexLength.
5404 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
5405
5406 static const int kContainsCachedArrayIndexMask =
5407 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
5408 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005409
5410 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005411 static const int kEmptyHashField =
5412 kIsNotArrayIndexMask | kHashNotComputedMask;
5413
5414 // Value of hash field containing computed hash equal to zero.
5415 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005416
5417 // Maximal string length.
5418 static const int kMaxLength = (1 << (32 - 2)) - 1;
5419
5420 // Max length for computing hash. For strings longer than this limit the
5421 // string length is used as the hash value.
5422 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00005423
5424 // Limit for truncation in short printing.
5425 static const int kMaxShortPrintLength = 1024;
5426
5427 // Support for regular expressions.
5428 const uc16* GetTwoByteData();
5429 const uc16* GetTwoByteData(unsigned start);
5430
5431 // Support for StringInputBuffer
5432 static const unibrow::byte* ReadBlock(String* input,
5433 unibrow::byte* util_buffer,
5434 unsigned capacity,
5435 unsigned* remaining,
5436 unsigned* offset);
5437 static const unibrow::byte* ReadBlock(String** input,
5438 unibrow::byte* util_buffer,
5439 unsigned capacity,
5440 unsigned* remaining,
5441 unsigned* offset);
5442
5443 // Helper function for flattening strings.
5444 template <typename sinkchar>
5445 static void WriteToFlat(String* source,
5446 sinkchar* sink,
5447 int from,
5448 int to);
5449
Steve Block9fac8402011-05-12 15:51:54 +01005450 static inline bool IsAscii(const char* chars, int length) {
5451 const char* limit = chars + length;
5452#ifdef V8_HOST_CAN_READ_UNALIGNED
5453 ASSERT(kMaxAsciiCharCode == 0x7F);
5454 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
5455 while (chars <= limit - sizeof(uintptr_t)) {
5456 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
5457 return false;
5458 }
5459 chars += sizeof(uintptr_t);
5460 }
5461#endif
5462 while (chars < limit) {
5463 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
5464 ++chars;
5465 }
5466 return true;
5467 }
5468
5469 static inline bool IsAscii(const uc16* chars, int length) {
5470 const uc16* limit = chars + length;
5471 while (chars < limit) {
5472 if (*chars > kMaxAsciiCharCodeU) return false;
5473 ++chars;
5474 }
5475 return true;
5476 }
5477
Steve Blocka7e24c12009-10-30 11:49:00 +00005478 protected:
5479 class ReadBlockBuffer {
5480 public:
5481 ReadBlockBuffer(unibrow::byte* util_buffer_,
5482 unsigned cursor_,
5483 unsigned capacity_,
5484 unsigned remaining_) :
5485 util_buffer(util_buffer_),
5486 cursor(cursor_),
5487 capacity(capacity_),
5488 remaining(remaining_) {
5489 }
5490 unibrow::byte* util_buffer;
5491 unsigned cursor;
5492 unsigned capacity;
5493 unsigned remaining;
5494 };
5495
Steve Blocka7e24c12009-10-30 11:49:00 +00005496 static inline const unibrow::byte* ReadBlock(String* input,
5497 ReadBlockBuffer* buffer,
5498 unsigned* offset,
5499 unsigned max_chars);
5500 static void ReadBlockIntoBuffer(String* input,
5501 ReadBlockBuffer* buffer,
5502 unsigned* offset_ptr,
5503 unsigned max_chars);
5504
5505 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01005506 // Try to flatten the top level ConsString that is hiding behind this
5507 // string. This is a no-op unless the string is a ConsString. Flatten
5508 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07005509 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01005510
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005511 static inline bool IsHashFieldComputed(uint32_t field);
5512
Steve Blocka7e24c12009-10-30 11:49:00 +00005513 // Slow case of String::Equals. This implementation works on any strings
5514 // but it is most efficient on strings that are almost flat.
5515 bool SlowEquals(String* other);
5516
5517 // Slow case of AsArrayIndex.
5518 bool SlowAsArrayIndex(uint32_t* index);
5519
5520 // Compute and set the hash code.
5521 uint32_t ComputeAndSetHash();
5522
5523 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
5524};
5525
5526
5527// The SeqString abstract class captures sequential string values.
5528class SeqString: public String {
5529 public:
5530
5531 // Casting.
5532 static inline SeqString* cast(Object* obj);
5533
Steve Blocka7e24c12009-10-30 11:49:00 +00005534 private:
5535 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
5536};
5537
5538
5539// The AsciiString class captures sequential ascii string objects.
5540// Each character in the AsciiString is an ascii character.
5541class SeqAsciiString: public SeqString {
5542 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005543 static const bool kHasAsciiEncoding = true;
5544
Steve Blocka7e24c12009-10-30 11:49:00 +00005545 // Dispatched behavior.
5546 inline uint16_t SeqAsciiStringGet(int index);
5547 inline void SeqAsciiStringSet(int index, uint16_t value);
5548
5549 // Get the address of the characters in this string.
5550 inline Address GetCharsAddress();
5551
5552 inline char* GetChars();
5553
5554 // Casting
5555 static inline SeqAsciiString* cast(Object* obj);
5556
5557 // Garbage collection support. This method is called by the
5558 // garbage collector to compute the actual size of an AsciiString
5559 // instance.
5560 inline int SeqAsciiStringSize(InstanceType instance_type);
5561
5562 // Computes the size for an AsciiString instance of a given length.
5563 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005564 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005565 }
5566
5567 // Layout description.
5568 static const int kHeaderSize = String::kSize;
5569 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5570
Leon Clarkee46be812010-01-19 14:06:41 +00005571 // Maximal memory usage for a single sequential ASCII string.
5572 static const int kMaxSize = 512 * MB;
5573 // Maximal length of a single sequential ASCII string.
5574 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5575 static const int kMaxLength = (kMaxSize - kHeaderSize);
5576
Steve Blocka7e24c12009-10-30 11:49:00 +00005577 // Support for StringInputBuffer.
5578 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5579 unsigned* offset,
5580 unsigned chars);
5581 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
5582 unsigned* offset,
5583 unsigned chars);
5584
5585 private:
5586 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
5587};
5588
5589
5590// The TwoByteString class captures sequential unicode string objects.
5591// Each character in the TwoByteString is a two-byte uint16_t.
5592class SeqTwoByteString: public SeqString {
5593 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005594 static const bool kHasAsciiEncoding = false;
5595
Steve Blocka7e24c12009-10-30 11:49:00 +00005596 // Dispatched behavior.
5597 inline uint16_t SeqTwoByteStringGet(int index);
5598 inline void SeqTwoByteStringSet(int index, uint16_t value);
5599
5600 // Get the address of the characters in this string.
5601 inline Address GetCharsAddress();
5602
5603 inline uc16* GetChars();
5604
5605 // For regexp code.
5606 const uint16_t* SeqTwoByteStringGetData(unsigned start);
5607
5608 // Casting
5609 static inline SeqTwoByteString* cast(Object* obj);
5610
5611 // Garbage collection support. This method is called by the
5612 // garbage collector to compute the actual size of a TwoByteString
5613 // instance.
5614 inline int SeqTwoByteStringSize(InstanceType instance_type);
5615
5616 // Computes the size for a TwoByteString instance of a given length.
5617 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005618 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005619 }
5620
5621 // Layout description.
5622 static const int kHeaderSize = String::kSize;
5623 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5624
Leon Clarkee46be812010-01-19 14:06:41 +00005625 // Maximal memory usage for a single sequential two-byte string.
5626 static const int kMaxSize = 512 * MB;
5627 // Maximal length of a single sequential two-byte string.
5628 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5629 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
5630
Steve Blocka7e24c12009-10-30 11:49:00 +00005631 // Support for StringInputBuffer.
5632 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5633 unsigned* offset_ptr,
5634 unsigned chars);
5635
5636 private:
5637 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
5638};
5639
5640
5641// The ConsString class describes string values built by using the
5642// addition operator on strings. A ConsString is a pair where the
5643// first and second components are pointers to other string values.
5644// One or both components of a ConsString can be pointers to other
5645// ConsStrings, creating a binary tree of ConsStrings where the leaves
5646// are non-ConsString string values. The string value represented by
5647// a ConsString can be obtained by concatenating the leaf string
5648// values in a left-to-right depth-first traversal of the tree.
5649class ConsString: public String {
5650 public:
5651 // First string of the cons cell.
5652 inline String* first();
5653 // Doesn't check that the result is a string, even in debug mode. This is
5654 // useful during GC where the mark bits confuse the checks.
5655 inline Object* unchecked_first();
5656 inline void set_first(String* first,
5657 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5658
5659 // Second string of the cons cell.
5660 inline String* second();
5661 // Doesn't check that the result is a string, even in debug mode. This is
5662 // useful during GC where the mark bits confuse the checks.
5663 inline Object* unchecked_second();
5664 inline void set_second(String* second,
5665 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5666
5667 // Dispatched behavior.
5668 uint16_t ConsStringGet(int index);
5669
5670 // Casting.
5671 static inline ConsString* cast(Object* obj);
5672
Steve Blocka7e24c12009-10-30 11:49:00 +00005673 // Layout description.
5674 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
5675 static const int kSecondOffset = kFirstOffset + kPointerSize;
5676 static const int kSize = kSecondOffset + kPointerSize;
5677
5678 // Support for StringInputBuffer.
5679 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
5680 unsigned* offset_ptr,
5681 unsigned chars);
5682 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5683 unsigned* offset_ptr,
5684 unsigned chars);
5685
5686 // Minimum length for a cons string.
5687 static const int kMinLength = 13;
5688
Iain Merrick75681382010-08-19 15:07:18 +01005689 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
5690 BodyDescriptor;
5691
Steve Blocka7e24c12009-10-30 11:49:00 +00005692 private:
5693 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
5694};
5695
5696
Steve Blocka7e24c12009-10-30 11:49:00 +00005697// The ExternalString class describes string values that are backed by
5698// a string resource that lies outside the V8 heap. ExternalStrings
5699// consist of the length field common to all strings, a pointer to the
5700// external resource. It is important to ensure (externally) that the
5701// resource is not deallocated while the ExternalString is live in the
5702// V8 heap.
5703//
5704// The API expects that all ExternalStrings are created through the
5705// API. Therefore, ExternalStrings should not be used internally.
5706class ExternalString: public String {
5707 public:
5708 // Casting
5709 static inline ExternalString* cast(Object* obj);
5710
5711 // Layout description.
5712 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
5713 static const int kSize = kResourceOffset + kPointerSize;
5714
5715 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
5716
5717 private:
5718 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
5719};
5720
5721
5722// The ExternalAsciiString class is an external string backed by an
5723// ASCII string.
5724class ExternalAsciiString: public ExternalString {
5725 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005726 static const bool kHasAsciiEncoding = true;
5727
Steve Blocka7e24c12009-10-30 11:49:00 +00005728 typedef v8::String::ExternalAsciiStringResource Resource;
5729
5730 // The underlying resource.
5731 inline Resource* resource();
5732 inline void set_resource(Resource* buffer);
5733
5734 // Dispatched behavior.
5735 uint16_t ExternalAsciiStringGet(int index);
5736
5737 // Casting.
5738 static inline ExternalAsciiString* cast(Object* obj);
5739
Steve Blockd0582a62009-12-15 09:54:21 +00005740 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005741 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
5742
5743 template<typename StaticVisitor>
5744 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00005745
Steve Blocka7e24c12009-10-30 11:49:00 +00005746 // Support for StringInputBuffer.
5747 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
5748 unsigned* offset,
5749 unsigned chars);
5750 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5751 unsigned* offset,
5752 unsigned chars);
5753
Steve Blocka7e24c12009-10-30 11:49:00 +00005754 private:
5755 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
5756};
5757
5758
5759// The ExternalTwoByteString class is an external string backed by a UTF-16
5760// encoded string.
5761class ExternalTwoByteString: public ExternalString {
5762 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005763 static const bool kHasAsciiEncoding = false;
5764
Steve Blocka7e24c12009-10-30 11:49:00 +00005765 typedef v8::String::ExternalStringResource Resource;
5766
5767 // The underlying string resource.
5768 inline Resource* resource();
5769 inline void set_resource(Resource* buffer);
5770
5771 // Dispatched behavior.
5772 uint16_t ExternalTwoByteStringGet(int index);
5773
5774 // For regexp code.
5775 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
5776
5777 // Casting.
5778 static inline ExternalTwoByteString* cast(Object* obj);
5779
Steve Blockd0582a62009-12-15 09:54:21 +00005780 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005781 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
5782
5783 template<typename StaticVisitor>
5784 inline void ExternalTwoByteStringIterateBody();
5785
Steve Blockd0582a62009-12-15 09:54:21 +00005786
Steve Blocka7e24c12009-10-30 11:49:00 +00005787 // Support for StringInputBuffer.
5788 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5789 unsigned* offset_ptr,
5790 unsigned chars);
5791
Steve Blocka7e24c12009-10-30 11:49:00 +00005792 private:
5793 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
5794};
5795
5796
5797// Utility superclass for stack-allocated objects that must be updated
5798// on gc. It provides two ways for the gc to update instances, either
5799// iterating or updating after gc.
5800class Relocatable BASE_EMBEDDED {
5801 public:
5802 inline Relocatable() : prev_(top_) { top_ = this; }
5803 virtual ~Relocatable() {
5804 ASSERT_EQ(top_, this);
5805 top_ = prev_;
5806 }
5807 virtual void IterateInstance(ObjectVisitor* v) { }
5808 virtual void PostGarbageCollection() { }
5809
5810 static void PostGarbageCollectionProcessing();
5811 static int ArchiveSpacePerThread();
5812 static char* ArchiveState(char* to);
5813 static char* RestoreState(char* from);
5814 static void Iterate(ObjectVisitor* v);
5815 static void Iterate(ObjectVisitor* v, Relocatable* top);
5816 static char* Iterate(ObjectVisitor* v, char* t);
5817 private:
5818 static Relocatable* top_;
5819 Relocatable* prev_;
5820};
5821
5822
5823// A flat string reader provides random access to the contents of a
5824// string independent of the character width of the string. The handle
5825// must be valid as long as the reader is being used.
5826class FlatStringReader : public Relocatable {
5827 public:
5828 explicit FlatStringReader(Handle<String> str);
5829 explicit FlatStringReader(Vector<const char> input);
5830 void PostGarbageCollection();
5831 inline uc32 Get(int index);
5832 int length() { return length_; }
5833 private:
5834 String** str_;
5835 bool is_ascii_;
5836 int length_;
5837 const void* start_;
5838};
5839
5840
5841// Note that StringInputBuffers are not valid across a GC! To fix this
5842// it would have to store a String Handle instead of a String* and
5843// AsciiStringReadBlock would have to be modified to use memcpy.
5844//
5845// StringInputBuffer is able to traverse any string regardless of how
5846// deeply nested a sequence of ConsStrings it is made of. However,
5847// performance will be better if deep strings are flattened before they
5848// are traversed. Since flattening requires memory allocation this is
5849// not always desirable, however (esp. in debugging situations).
5850class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
5851 public:
5852 virtual void Seek(unsigned pos);
5853 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
5854 inline StringInputBuffer(String* backing):
5855 unibrow::InputBuffer<String, String*, 1024>(backing) {}
5856};
5857
5858
5859class SafeStringInputBuffer
5860 : public unibrow::InputBuffer<String, String**, 256> {
5861 public:
5862 virtual void Seek(unsigned pos);
5863 inline SafeStringInputBuffer()
5864 : unibrow::InputBuffer<String, String**, 256>() {}
5865 inline SafeStringInputBuffer(String** backing)
5866 : unibrow::InputBuffer<String, String**, 256>(backing) {}
5867};
5868
5869
5870template <typename T>
5871class VectorIterator {
5872 public:
5873 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
5874 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
5875 T GetNext() { return data_[index_++]; }
5876 bool has_more() { return index_ < data_.length(); }
5877 private:
5878 Vector<const T> data_;
5879 int index_;
5880};
5881
5882
5883// The Oddball describes objects null, undefined, true, and false.
5884class Oddball: public HeapObject {
5885 public:
5886 // [to_string]: Cached to_string computed at startup.
5887 DECL_ACCESSORS(to_string, String)
5888
5889 // [to_number]: Cached to_number computed at startup.
5890 DECL_ACCESSORS(to_number, Object)
5891
5892 // Casting.
5893 static inline Oddball* cast(Object* obj);
5894
5895 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00005896#ifdef DEBUG
5897 void OddballVerify();
5898#endif
5899
5900 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07005901 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
5902 Object* to_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00005903
5904 // Layout description.
5905 static const int kToStringOffset = HeapObject::kHeaderSize;
5906 static const int kToNumberOffset = kToStringOffset + kPointerSize;
5907 static const int kSize = kToNumberOffset + kPointerSize;
5908
Iain Merrick75681382010-08-19 15:07:18 +01005909 typedef FixedBodyDescriptor<kToStringOffset,
5910 kToNumberOffset + kPointerSize,
5911 kSize> BodyDescriptor;
5912
Steve Blocka7e24c12009-10-30 11:49:00 +00005913 private:
5914 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
5915};
5916
5917
5918class JSGlobalPropertyCell: public HeapObject {
5919 public:
5920 // [value]: value of the global property.
5921 DECL_ACCESSORS(value, Object)
5922
5923 // Casting.
5924 static inline JSGlobalPropertyCell* cast(Object* obj);
5925
Steve Blocka7e24c12009-10-30 11:49:00 +00005926#ifdef DEBUG
5927 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005928#endif
5929#ifdef OBJECT_PRINT
5930 inline void JSGlobalPropertyCellPrint() {
5931 JSGlobalPropertyCellPrint(stdout);
5932 }
5933 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00005934#endif
5935
5936 // Layout description.
5937 static const int kValueOffset = HeapObject::kHeaderSize;
5938 static const int kSize = kValueOffset + kPointerSize;
5939
Iain Merrick75681382010-08-19 15:07:18 +01005940 typedef FixedBodyDescriptor<kValueOffset,
5941 kValueOffset + kPointerSize,
5942 kSize> BodyDescriptor;
5943
Steve Blocka7e24c12009-10-30 11:49:00 +00005944 private:
5945 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
5946};
5947
5948
5949
5950// Proxy describes objects pointing from JavaScript to C structures.
5951// Since they cannot contain references to JS HeapObjects they can be
5952// placed in old_data_space.
5953class Proxy: public HeapObject {
5954 public:
5955 // [proxy]: field containing the address.
5956 inline Address proxy();
5957 inline void set_proxy(Address value);
5958
5959 // Casting.
5960 static inline Proxy* cast(Object* obj);
5961
5962 // Dispatched behavior.
5963 inline void ProxyIterateBody(ObjectVisitor* v);
Iain Merrick75681382010-08-19 15:07:18 +01005964
5965 template<typename StaticVisitor>
5966 inline void ProxyIterateBody();
5967
Ben Murdochb0fe1622011-05-05 13:52:32 +01005968#ifdef OBJECT_PRINT
5969 inline void ProxyPrint() {
5970 ProxyPrint(stdout);
5971 }
5972 void ProxyPrint(FILE* out);
5973#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005974#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005975 void ProxyVerify();
5976#endif
5977
5978 // Layout description.
5979
5980 static const int kProxyOffset = HeapObject::kHeaderSize;
5981 static const int kSize = kProxyOffset + kPointerSize;
5982
5983 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
5984
5985 private:
5986 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
5987};
5988
5989
5990// The JSArray describes JavaScript Arrays
5991// Such an array can be in one of two modes:
5992// - fast, backing storage is a FixedArray and length <= elements.length();
5993// Please note: push and pop can be used to grow and shrink the array.
5994// - slow, backing storage is a HashTable with numbers as keys.
5995class JSArray: public JSObject {
5996 public:
5997 // [length]: The length property.
5998 DECL_ACCESSORS(length, Object)
5999
Leon Clarke4515c472010-02-03 11:58:03 +00006000 // Overload the length setter to skip write barrier when the length
6001 // is set to a smi. This matches the set function on FixedArray.
6002 inline void set_length(Smi* length);
6003
John Reck59135872010-11-02 12:39:01 -07006004 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6005 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006006
6007 // Initialize the array with the given capacity. The function may
6008 // fail due to out-of-memory situations, but only if the requested
6009 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07006010 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00006011
6012 // Set the content of the array to the content of storage.
6013 inline void SetContent(FixedArray* storage);
6014
6015 // Casting.
6016 static inline JSArray* cast(Object* obj);
6017
6018 // Uses handles. Ensures that the fixed array backing the JSArray has at
6019 // least the stated size.
6020 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
6021
6022 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006023#ifdef OBJECT_PRINT
6024 inline void JSArrayPrint() {
6025 JSArrayPrint(stdout);
6026 }
6027 void JSArrayPrint(FILE* out);
6028#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006029#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006030 void JSArrayVerify();
6031#endif
6032
6033 // Number of element slots to pre-allocate for an empty array.
6034 static const int kPreallocatedArrayElements = 4;
6035
6036 // Layout description.
6037 static const int kLengthOffset = JSObject::kHeaderSize;
6038 static const int kSize = kLengthOffset + kPointerSize;
6039
6040 private:
6041 // Expand the fixed array backing of a fast-case JSArray to at least
6042 // the requested size.
6043 void Expand(int minimum_size_of_backing_fixed_array);
6044
6045 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6046};
6047
6048
Steve Block6ded16b2010-05-10 14:33:55 +01006049// JSRegExpResult is just a JSArray with a specific initial map.
6050// This initial map adds in-object properties for "index" and "input"
6051// properties, as assigned by RegExp.prototype.exec, which allows
6052// faster creation of RegExp exec results.
6053// This class just holds constants used when creating the result.
6054// After creation the result must be treated as a JSArray in all regards.
6055class JSRegExpResult: public JSArray {
6056 public:
6057 // Offsets of object fields.
6058 static const int kIndexOffset = JSArray::kSize;
6059 static const int kInputOffset = kIndexOffset + kPointerSize;
6060 static const int kSize = kInputOffset + kPointerSize;
6061 // Indices of in-object properties.
6062 static const int kIndexIndex = 0;
6063 static const int kInputIndex = 1;
6064 private:
6065 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6066};
6067
6068
Steve Blocka7e24c12009-10-30 11:49:00 +00006069// An accessor must have a getter, but can have no setter.
6070//
6071// When setting a property, V8 searches accessors in prototypes.
6072// If an accessor was found and it does not have a setter,
6073// the request is ignored.
6074//
6075// If the accessor in the prototype has the READ_ONLY property attribute, then
6076// a new value is added to the local object when the property is set.
6077// This shadows the accessor in the prototype.
6078class AccessorInfo: public Struct {
6079 public:
6080 DECL_ACCESSORS(getter, Object)
6081 DECL_ACCESSORS(setter, Object)
6082 DECL_ACCESSORS(data, Object)
6083 DECL_ACCESSORS(name, Object)
6084 DECL_ACCESSORS(flag, Smi)
6085
6086 inline bool all_can_read();
6087 inline void set_all_can_read(bool value);
6088
6089 inline bool all_can_write();
6090 inline void set_all_can_write(bool value);
6091
6092 inline bool prohibits_overwriting();
6093 inline void set_prohibits_overwriting(bool value);
6094
6095 inline PropertyAttributes property_attributes();
6096 inline void set_property_attributes(PropertyAttributes attributes);
6097
6098 static inline AccessorInfo* cast(Object* obj);
6099
Ben Murdochb0fe1622011-05-05 13:52:32 +01006100#ifdef OBJECT_PRINT
6101 inline void AccessorInfoPrint() {
6102 AccessorInfoPrint(stdout);
6103 }
6104 void AccessorInfoPrint(FILE* out);
6105#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006106#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006107 void AccessorInfoVerify();
6108#endif
6109
6110 static const int kGetterOffset = HeapObject::kHeaderSize;
6111 static const int kSetterOffset = kGetterOffset + kPointerSize;
6112 static const int kDataOffset = kSetterOffset + kPointerSize;
6113 static const int kNameOffset = kDataOffset + kPointerSize;
6114 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006115 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006116
6117 private:
6118 // Bit positions in flag.
6119 static const int kAllCanReadBit = 0;
6120 static const int kAllCanWriteBit = 1;
6121 static const int kProhibitsOverwritingBit = 2;
6122 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
6123
6124 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
6125};
6126
6127
6128class AccessCheckInfo: public Struct {
6129 public:
6130 DECL_ACCESSORS(named_callback, Object)
6131 DECL_ACCESSORS(indexed_callback, Object)
6132 DECL_ACCESSORS(data, Object)
6133
6134 static inline AccessCheckInfo* cast(Object* obj);
6135
Ben Murdochb0fe1622011-05-05 13:52:32 +01006136#ifdef OBJECT_PRINT
6137 inline void AccessCheckInfoPrint() {
6138 AccessCheckInfoPrint(stdout);
6139 }
6140 void AccessCheckInfoPrint(FILE* out);
6141#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006142#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006143 void AccessCheckInfoVerify();
6144#endif
6145
6146 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
6147 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
6148 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
6149 static const int kSize = kDataOffset + kPointerSize;
6150
6151 private:
6152 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
6153};
6154
6155
6156class InterceptorInfo: public Struct {
6157 public:
6158 DECL_ACCESSORS(getter, Object)
6159 DECL_ACCESSORS(setter, Object)
6160 DECL_ACCESSORS(query, Object)
6161 DECL_ACCESSORS(deleter, Object)
6162 DECL_ACCESSORS(enumerator, Object)
6163 DECL_ACCESSORS(data, Object)
6164
6165 static inline InterceptorInfo* cast(Object* obj);
6166
Ben Murdochb0fe1622011-05-05 13:52:32 +01006167#ifdef OBJECT_PRINT
6168 inline void InterceptorInfoPrint() {
6169 InterceptorInfoPrint(stdout);
6170 }
6171 void InterceptorInfoPrint(FILE* out);
6172#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006173#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006174 void InterceptorInfoVerify();
6175#endif
6176
6177 static const int kGetterOffset = HeapObject::kHeaderSize;
6178 static const int kSetterOffset = kGetterOffset + kPointerSize;
6179 static const int kQueryOffset = kSetterOffset + kPointerSize;
6180 static const int kDeleterOffset = kQueryOffset + kPointerSize;
6181 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
6182 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
6183 static const int kSize = kDataOffset + kPointerSize;
6184
6185 private:
6186 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
6187};
6188
6189
6190class CallHandlerInfo: public Struct {
6191 public:
6192 DECL_ACCESSORS(callback, Object)
6193 DECL_ACCESSORS(data, Object)
6194
6195 static inline CallHandlerInfo* cast(Object* obj);
6196
Ben Murdochb0fe1622011-05-05 13:52:32 +01006197#ifdef OBJECT_PRINT
6198 inline void CallHandlerInfoPrint() {
6199 CallHandlerInfoPrint(stdout);
6200 }
6201 void CallHandlerInfoPrint(FILE* out);
6202#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006203#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006204 void CallHandlerInfoVerify();
6205#endif
6206
6207 static const int kCallbackOffset = HeapObject::kHeaderSize;
6208 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006209 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006210
6211 private:
6212 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
6213};
6214
6215
6216class TemplateInfo: public Struct {
6217 public:
6218 DECL_ACCESSORS(tag, Object)
6219 DECL_ACCESSORS(property_list, Object)
6220
6221#ifdef DEBUG
6222 void TemplateInfoVerify();
6223#endif
6224
6225 static const int kTagOffset = HeapObject::kHeaderSize;
6226 static const int kPropertyListOffset = kTagOffset + kPointerSize;
6227 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
6228 protected:
6229 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6230 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6231};
6232
6233
6234class FunctionTemplateInfo: public TemplateInfo {
6235 public:
6236 DECL_ACCESSORS(serial_number, Object)
6237 DECL_ACCESSORS(call_code, Object)
6238 DECL_ACCESSORS(property_accessors, Object)
6239 DECL_ACCESSORS(prototype_template, Object)
6240 DECL_ACCESSORS(parent_template, Object)
6241 DECL_ACCESSORS(named_property_handler, Object)
6242 DECL_ACCESSORS(indexed_property_handler, Object)
6243 DECL_ACCESSORS(instance_template, Object)
6244 DECL_ACCESSORS(class_name, Object)
6245 DECL_ACCESSORS(signature, Object)
6246 DECL_ACCESSORS(instance_call_handler, Object)
6247 DECL_ACCESSORS(access_check_info, Object)
6248 DECL_ACCESSORS(flag, Smi)
6249
6250 // Following properties use flag bits.
6251 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6252 DECL_BOOLEAN_ACCESSORS(undetectable)
6253 // If the bit is set, object instances created by this function
6254 // requires access check.
6255 DECL_BOOLEAN_ACCESSORS(needs_access_check)
6256
6257 static inline FunctionTemplateInfo* cast(Object* obj);
6258
Ben Murdochb0fe1622011-05-05 13:52:32 +01006259#ifdef OBJECT_PRINT
6260 inline void FunctionTemplateInfoPrint() {
6261 FunctionTemplateInfoPrint(stdout);
6262 }
6263 void FunctionTemplateInfoPrint(FILE* out);
6264#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006265#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006266 void FunctionTemplateInfoVerify();
6267#endif
6268
6269 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
6270 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
6271 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
6272 static const int kPrototypeTemplateOffset =
6273 kPropertyAccessorsOffset + kPointerSize;
6274 static const int kParentTemplateOffset =
6275 kPrototypeTemplateOffset + kPointerSize;
6276 static const int kNamedPropertyHandlerOffset =
6277 kParentTemplateOffset + kPointerSize;
6278 static const int kIndexedPropertyHandlerOffset =
6279 kNamedPropertyHandlerOffset + kPointerSize;
6280 static const int kInstanceTemplateOffset =
6281 kIndexedPropertyHandlerOffset + kPointerSize;
6282 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
6283 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
6284 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
6285 static const int kAccessCheckInfoOffset =
6286 kInstanceCallHandlerOffset + kPointerSize;
6287 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
6288 static const int kSize = kFlagOffset + kPointerSize;
6289
6290 private:
6291 // Bit position in the flag, from least significant bit position.
6292 static const int kHiddenPrototypeBit = 0;
6293 static const int kUndetectableBit = 1;
6294 static const int kNeedsAccessCheckBit = 2;
6295
6296 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
6297};
6298
6299
6300class ObjectTemplateInfo: public TemplateInfo {
6301 public:
6302 DECL_ACCESSORS(constructor, Object)
6303 DECL_ACCESSORS(internal_field_count, Object)
6304
6305 static inline ObjectTemplateInfo* cast(Object* obj);
6306
Ben Murdochb0fe1622011-05-05 13:52:32 +01006307#ifdef OBJECT_PRINT
6308 inline void ObjectTemplateInfoPrint() {
6309 ObjectTemplateInfoPrint(stdout);
6310 }
6311 void ObjectTemplateInfoPrint(FILE* out);
6312#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006313#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006314 void ObjectTemplateInfoVerify();
6315#endif
6316
6317 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
6318 static const int kInternalFieldCountOffset =
6319 kConstructorOffset + kPointerSize;
6320 static const int kSize = kInternalFieldCountOffset + kPointerSize;
6321};
6322
6323
6324class SignatureInfo: public Struct {
6325 public:
6326 DECL_ACCESSORS(receiver, Object)
6327 DECL_ACCESSORS(args, Object)
6328
6329 static inline SignatureInfo* cast(Object* obj);
6330
Ben Murdochb0fe1622011-05-05 13:52:32 +01006331#ifdef OBJECT_PRINT
6332 inline void SignatureInfoPrint() {
6333 SignatureInfoPrint(stdout);
6334 }
6335 void SignatureInfoPrint(FILE* out);
6336#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006337#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006338 void SignatureInfoVerify();
6339#endif
6340
6341 static const int kReceiverOffset = Struct::kHeaderSize;
6342 static const int kArgsOffset = kReceiverOffset + kPointerSize;
6343 static const int kSize = kArgsOffset + kPointerSize;
6344
6345 private:
6346 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
6347};
6348
6349
6350class TypeSwitchInfo: public Struct {
6351 public:
6352 DECL_ACCESSORS(types, Object)
6353
6354 static inline TypeSwitchInfo* cast(Object* obj);
6355
Ben Murdochb0fe1622011-05-05 13:52:32 +01006356#ifdef OBJECT_PRINT
6357 inline void TypeSwitchInfoPrint() {
6358 TypeSwitchInfoPrint(stdout);
6359 }
6360 void TypeSwitchInfoPrint(FILE* out);
6361#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006362#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006363 void TypeSwitchInfoVerify();
6364#endif
6365
6366 static const int kTypesOffset = Struct::kHeaderSize;
6367 static const int kSize = kTypesOffset + kPointerSize;
6368};
6369
6370
6371#ifdef ENABLE_DEBUGGER_SUPPORT
6372// The DebugInfo class holds additional information for a function being
6373// debugged.
6374class DebugInfo: public Struct {
6375 public:
6376 // The shared function info for the source being debugged.
6377 DECL_ACCESSORS(shared, SharedFunctionInfo)
6378 // Code object for the original code.
6379 DECL_ACCESSORS(original_code, Code)
6380 // Code object for the patched code. This code object is the code object
6381 // currently active for the function.
6382 DECL_ACCESSORS(code, Code)
6383 // Fixed array holding status information for each active break point.
6384 DECL_ACCESSORS(break_points, FixedArray)
6385
6386 // Check if there is a break point at a code position.
6387 bool HasBreakPoint(int code_position);
6388 // Get the break point info object for a code position.
6389 Object* GetBreakPointInfo(int code_position);
6390 // Clear a break point.
6391 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
6392 int code_position,
6393 Handle<Object> break_point_object);
6394 // Set a break point.
6395 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
6396 int source_position, int statement_position,
6397 Handle<Object> break_point_object);
6398 // Get the break point objects for a code position.
6399 Object* GetBreakPointObjects(int code_position);
6400 // Find the break point info holding this break point object.
6401 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
6402 Handle<Object> break_point_object);
6403 // Get the number of break points for this function.
6404 int GetBreakPointCount();
6405
6406 static inline DebugInfo* cast(Object* obj);
6407
Ben Murdochb0fe1622011-05-05 13:52:32 +01006408#ifdef OBJECT_PRINT
6409 inline void DebugInfoPrint() {
6410 DebugInfoPrint(stdout);
6411 }
6412 void DebugInfoPrint(FILE* out);
6413#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006414#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006415 void DebugInfoVerify();
6416#endif
6417
6418 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
6419 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
6420 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
6421 static const int kActiveBreakPointsCountIndex =
6422 kPatchedCodeIndex + kPointerSize;
6423 static const int kBreakPointsStateIndex =
6424 kActiveBreakPointsCountIndex + kPointerSize;
6425 static const int kSize = kBreakPointsStateIndex + kPointerSize;
6426
6427 private:
6428 static const int kNoBreakPointInfo = -1;
6429
6430 // Lookup the index in the break_points array for a code position.
6431 int GetBreakPointInfoIndex(int code_position);
6432
6433 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
6434};
6435
6436
6437// The BreakPointInfo class holds information for break points set in a
6438// function. The DebugInfo object holds a BreakPointInfo object for each code
6439// position with one or more break points.
6440class BreakPointInfo: public Struct {
6441 public:
6442 // The position in the code for the break point.
6443 DECL_ACCESSORS(code_position, Smi)
6444 // The position in the source for the break position.
6445 DECL_ACCESSORS(source_position, Smi)
6446 // The position in the source for the last statement before this break
6447 // position.
6448 DECL_ACCESSORS(statement_position, Smi)
6449 // List of related JavaScript break points.
6450 DECL_ACCESSORS(break_point_objects, Object)
6451
6452 // Removes a break point.
6453 static void ClearBreakPoint(Handle<BreakPointInfo> info,
6454 Handle<Object> break_point_object);
6455 // Set a break point.
6456 static void SetBreakPoint(Handle<BreakPointInfo> info,
6457 Handle<Object> break_point_object);
6458 // Check if break point info has this break point object.
6459 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
6460 Handle<Object> break_point_object);
6461 // Get the number of break points for this code position.
6462 int GetBreakPointCount();
6463
6464 static inline BreakPointInfo* cast(Object* obj);
6465
Ben Murdochb0fe1622011-05-05 13:52:32 +01006466#ifdef OBJECT_PRINT
6467 inline void BreakPointInfoPrint() {
6468 BreakPointInfoPrint(stdout);
6469 }
6470 void BreakPointInfoPrint(FILE* out);
6471#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006472#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006473 void BreakPointInfoVerify();
6474#endif
6475
6476 static const int kCodePositionIndex = Struct::kHeaderSize;
6477 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
6478 static const int kStatementPositionIndex =
6479 kSourcePositionIndex + kPointerSize;
6480 static const int kBreakPointObjectsIndex =
6481 kStatementPositionIndex + kPointerSize;
6482 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
6483
6484 private:
6485 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
6486};
6487#endif // ENABLE_DEBUGGER_SUPPORT
6488
6489
6490#undef DECL_BOOLEAN_ACCESSORS
6491#undef DECL_ACCESSORS
6492
6493
6494// Abstract base class for visiting, and optionally modifying, the
6495// pointers contained in Objects. Used in GC and serialization/deserialization.
6496class ObjectVisitor BASE_EMBEDDED {
6497 public:
6498 virtual ~ObjectVisitor() {}
6499
6500 // Visits a contiguous arrays of pointers in the half-open range
6501 // [start, end). Any or all of the values may be modified on return.
6502 virtual void VisitPointers(Object** start, Object** end) = 0;
6503
6504 // To allow lazy clearing of inline caches the visitor has
6505 // a rich interface for iterating over Code objects..
6506
6507 // Visits a code target in the instruction stream.
6508 virtual void VisitCodeTarget(RelocInfo* rinfo);
6509
Steve Block791712a2010-08-27 10:21:07 +01006510 // Visits a code entry in a JS function.
6511 virtual void VisitCodeEntry(Address entry_address);
6512
Ben Murdochb0fe1622011-05-05 13:52:32 +01006513 // Visits a global property cell reference in the instruction stream.
6514 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
6515
Steve Blocka7e24c12009-10-30 11:49:00 +00006516 // Visits a runtime entry in the instruction stream.
6517 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
6518
Steve Blockd0582a62009-12-15 09:54:21 +00006519 // Visits the resource of an ASCII or two-byte string.
6520 virtual void VisitExternalAsciiString(
6521 v8::String::ExternalAsciiStringResource** resource) {}
6522 virtual void VisitExternalTwoByteString(
6523 v8::String::ExternalStringResource** resource) {}
6524
Steve Blocka7e24c12009-10-30 11:49:00 +00006525 // Visits a debug call target in the instruction stream.
6526 virtual void VisitDebugTarget(RelocInfo* rinfo);
6527
6528 // Handy shorthand for visiting a single pointer.
6529 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
6530
6531 // Visits a contiguous arrays of external references (references to the C++
6532 // heap) in the half-open range [start, end). Any or all of the values
6533 // may be modified on return.
6534 virtual void VisitExternalReferences(Address* start, Address* end) {}
6535
6536 inline void VisitExternalReference(Address* p) {
6537 VisitExternalReferences(p, p + 1);
6538 }
6539
6540#ifdef DEBUG
6541 // Intended for serialization/deserialization checking: insert, or
6542 // check for the presence of, a tag at this position in the stream.
6543 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00006544#else
6545 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00006546#endif
6547};
6548
6549
Iain Merrick75681382010-08-19 15:07:18 +01006550class StructBodyDescriptor : public
6551 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
6552 public:
6553 static inline int SizeOf(Map* map, HeapObject* object) {
6554 return map->instance_size();
6555 }
6556};
6557
6558
Steve Blocka7e24c12009-10-30 11:49:00 +00006559// BooleanBit is a helper class for setting and getting a bit in an
6560// integer or Smi.
6561class BooleanBit : public AllStatic {
6562 public:
6563 static inline bool get(Smi* smi, int bit_position) {
6564 return get(smi->value(), bit_position);
6565 }
6566
6567 static inline bool get(int value, int bit_position) {
6568 return (value & (1 << bit_position)) != 0;
6569 }
6570
6571 static inline Smi* set(Smi* smi, int bit_position, bool v) {
6572 return Smi::FromInt(set(smi->value(), bit_position, v));
6573 }
6574
6575 static inline int set(int value, int bit_position, bool v) {
6576 if (v) {
6577 value |= (1 << bit_position);
6578 } else {
6579 value &= ~(1 << bit_position);
6580 }
6581 return value;
6582 }
6583};
6584
6585} } // namespace v8::internal
6586
6587#endif // V8_OBJECTS_H_