blob: e966b3de98f6ff93489883912e0dbb5df5de3565 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 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
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010060// - ExternalArray
Steve Block44f0eee2011-05-26 01:26:41 +010061// - ExternalPixelArray
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010062// - 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) {
Steve Block44f0eee2011-05-26 01:26:41 +0100138 ASSERT(type != EXTERNAL_ARRAY_TRANSITION);
Steve Blocka7e24c12009-10-30 11:49:00 +0000139 ASSERT(TypeField::is_valid(type));
140 ASSERT(AttributesField::is_valid(attributes));
Steve Block44f0eee2011-05-26 01:26:41 +0100141 ASSERT(StorageField::is_valid(index));
Steve Blocka7e24c12009-10-30 11:49:00 +0000142
143 value_ = TypeField::encode(type)
144 | AttributesField::encode(attributes)
Steve Block44f0eee2011-05-26 01:26:41 +0100145 | StorageField::encode(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000146
147 ASSERT(type == this->type());
148 ASSERT(attributes == this->attributes());
149 ASSERT(index == this->index());
150 }
151
Steve Block44f0eee2011-05-26 01:26:41 +0100152 PropertyDetails(PropertyAttributes attributes,
153 PropertyType type,
154 ExternalArrayType array_type) {
155 ASSERT(type == EXTERNAL_ARRAY_TRANSITION);
156 ASSERT(TypeField::is_valid(type));
157 ASSERT(AttributesField::is_valid(attributes));
158 ASSERT(StorageField::is_valid(static_cast<int>(array_type)));
159
160 value_ = TypeField::encode(type)
161 | AttributesField::encode(attributes)
162 | StorageField::encode(static_cast<int>(array_type));
163
164 ASSERT(type == this->type());
165 ASSERT(attributes == this->attributes());
166 ASSERT(array_type == this->array_type());
167 }
168
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 // Conversion for storing details as Object*.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100170 explicit inline PropertyDetails(Smi* smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000171 inline Smi* AsSmi();
172
173 PropertyType type() { return TypeField::decode(value_); }
174
175 bool IsTransition() {
176 PropertyType t = type();
177 ASSERT(t != INTERCEPTOR);
Steve Block44f0eee2011-05-26 01:26:41 +0100178 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
179 t == EXTERNAL_ARRAY_TRANSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000180 }
181
182 bool IsProperty() {
183 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
184 }
185
186 PropertyAttributes attributes() { return AttributesField::decode(value_); }
187
Steve Block44f0eee2011-05-26 01:26:41 +0100188 int index() { return StorageField::decode(value_); }
189
190 ExternalArrayType array_type() {
191 ASSERT(type() == EXTERNAL_ARRAY_TRANSITION);
192 return static_cast<ExternalArrayType>(StorageField::decode(value_));
193 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
195 inline PropertyDetails AsDeleted();
196
Steve Block44f0eee2011-05-26 01:26:41 +0100197 static bool IsValidIndex(int index) {
198 return StorageField::is_valid(index);
199 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000200
201 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
202 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
203 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
204 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
205
206 // Bit fields in value_ (type, shift, size). Must be public so the
207 // constants can be embedded in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100208 class TypeField: public BitField<PropertyType, 0, 4> {};
209 class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
210 class DeletedField: public BitField<uint32_t, 7, 1> {};
211 class StorageField: public BitField<uint32_t, 8, 32-8> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000212
213 static const int kInitialIndex = 1;
214 private:
215 uint32_t value_;
216};
217
218
219// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
220enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
221
222
223// PropertyNormalizationMode is used to specify whether to keep
224// inobject properties when normalizing properties of a JSObject.
225enum PropertyNormalizationMode {
226 CLEAR_INOBJECT_PROPERTIES,
227 KEEP_INOBJECT_PROPERTIES
228};
229
230
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100231// NormalizedMapSharingMode is used to specify whether a map may be shared
232// by different objects with normalized properties.
233enum NormalizedMapSharingMode {
234 UNIQUE_NORMALIZED_MAP,
235 SHARED_NORMALIZED_MAP
236};
237
238
Steve Block791712a2010-08-27 10:21:07 +0100239// Instance size sentinel for objects of variable size.
240static const int kVariableSizeSentinel = 0;
241
242
Steve Blocka7e24c12009-10-30 11:49:00 +0000243// All Maps have a field instance_type containing a InstanceType.
244// It describes the type of the instances.
245//
246// As an example, a JavaScript object is a heap object and its map
247// instance_type is JS_OBJECT_TYPE.
248//
249// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000250// mirror their encoding in the instance_type field of the map. The default
251// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
252// encoding is mentioned explicitly in the name. Likewise, the default
253// representation is considered sequential. It is not mentioned in the
254// name. The other representations (eg, CONS, EXTERNAL) are explicitly
255// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
256// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000257//
258// NOTE: The following things are some that depend on the string types having
259// instance_types that are less than those of all other types:
260// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
261// Object::IsString.
262//
263// NOTE: Everything following JS_VALUE_TYPE is considered a
264// JSObject for GC purposes. The first four entries here have typeof
265// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000266#define INSTANCE_TYPE_LIST_ALL(V) \
267 V(SYMBOL_TYPE) \
268 V(ASCII_SYMBOL_TYPE) \
269 V(CONS_SYMBOL_TYPE) \
270 V(CONS_ASCII_SYMBOL_TYPE) \
271 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100272 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000273 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
274 V(STRING_TYPE) \
275 V(ASCII_STRING_TYPE) \
276 V(CONS_STRING_TYPE) \
277 V(CONS_ASCII_STRING_TYPE) \
278 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100279 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000280 V(EXTERNAL_ASCII_STRING_TYPE) \
281 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
282 \
283 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000284 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000285 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100286 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000287 \
288 V(HEAP_NUMBER_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000289 V(PROXY_TYPE) \
290 V(BYTE_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000291 /* Note: the order of these external array */ \
292 /* types is relied upon in */ \
293 /* Object::IsExternalArray(). */ \
294 V(EXTERNAL_BYTE_ARRAY_TYPE) \
295 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
296 V(EXTERNAL_SHORT_ARRAY_TYPE) \
297 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
298 V(EXTERNAL_INT_ARRAY_TYPE) \
299 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
300 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
Steve Block44f0eee2011-05-26 01:26:41 +0100301 V(EXTERNAL_PIXEL_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000302 V(FILLER_TYPE) \
303 \
304 V(ACCESSOR_INFO_TYPE) \
305 V(ACCESS_CHECK_INFO_TYPE) \
306 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000307 V(CALL_HANDLER_INFO_TYPE) \
308 V(FUNCTION_TEMPLATE_INFO_TYPE) \
309 V(OBJECT_TEMPLATE_INFO_TYPE) \
310 V(SIGNATURE_INFO_TYPE) \
311 V(TYPE_SWITCH_INFO_TYPE) \
312 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100313 V(CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000314 \
Iain Merrick75681382010-08-19 15:07:18 +0100315 V(FIXED_ARRAY_TYPE) \
316 V(SHARED_FUNCTION_INFO_TYPE) \
317 \
Steve Block1e0659c2011-05-24 12:43:12 +0100318 V(JS_MESSAGE_OBJECT_TYPE) \
319 \
Steve Blockd0582a62009-12-15 09:54:21 +0000320 V(JS_VALUE_TYPE) \
321 V(JS_OBJECT_TYPE) \
322 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
323 V(JS_GLOBAL_OBJECT_TYPE) \
324 V(JS_BUILTINS_OBJECT_TYPE) \
325 V(JS_GLOBAL_PROXY_TYPE) \
326 V(JS_ARRAY_TYPE) \
327 V(JS_REGEXP_TYPE) \
328 \
329 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000330
331#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000332#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
333 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000334 V(BREAK_POINT_INFO_TYPE)
335#else
336#define INSTANCE_TYPE_LIST_DEBUGGER(V)
337#endif
338
Steve Blockd0582a62009-12-15 09:54:21 +0000339#define INSTANCE_TYPE_LIST(V) \
340 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 INSTANCE_TYPE_LIST_DEBUGGER(V)
342
343
344// Since string types are not consecutive, this macro is used to
345// iterate over them.
346#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000347 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100348 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000349 symbol, \
350 Symbol) \
351 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100352 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000353 ascii_symbol, \
354 AsciiSymbol) \
355 V(CONS_SYMBOL_TYPE, \
356 ConsString::kSize, \
357 cons_symbol, \
358 ConsSymbol) \
359 V(CONS_ASCII_SYMBOL_TYPE, \
360 ConsString::kSize, \
361 cons_ascii_symbol, \
362 ConsAsciiSymbol) \
363 V(EXTERNAL_SYMBOL_TYPE, \
364 ExternalTwoByteString::kSize, \
365 external_symbol, \
366 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100367 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
368 ExternalTwoByteString::kSize, \
369 external_symbol_with_ascii_data, \
370 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000371 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
372 ExternalAsciiString::kSize, \
373 external_ascii_symbol, \
374 ExternalAsciiSymbol) \
375 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100376 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000377 string, \
378 String) \
379 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100380 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000381 ascii_string, \
382 AsciiString) \
383 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000385 cons_string, \
386 ConsString) \
387 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000389 cons_ascii_string, \
390 ConsAsciiString) \
391 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000393 external_string, \
394 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100395 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
396 ExternalTwoByteString::kSize, \
397 external_string_with_ascii_data, \
398 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000399 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000401 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100402 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000403
404// A struct is a simple object a set of object-valued fields. Including an
405// object type in this causes the compiler to generate most of the boilerplate
406// code for the class including allocation and garbage collection routines,
407// casts and predicates. All you need to define is the class, methods and
408// object verification routines. Easy, no?
409//
410// Note that for subtle reasons related to the ordering or numerical values of
411// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
412// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000413#define STRUCT_LIST_ALL(V) \
414 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
415 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
416 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
417 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
418 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
419 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
420 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
421 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100422 V(SCRIPT, Script, script) \
423 V(CODE_CACHE, CodeCache, code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000424
425#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000426#define STRUCT_LIST_DEBUGGER(V) \
427 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000428 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
429#else
430#define STRUCT_LIST_DEBUGGER(V)
431#endif
432
Steve Blockd0582a62009-12-15 09:54:21 +0000433#define STRUCT_LIST(V) \
434 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000435 STRUCT_LIST_DEBUGGER(V)
436
437// We use the full 8 bits of the instance_type field to encode heap object
438// instance types. The high-order bit (bit 7) is set if the object is not a
439// string, and cleared if it is a string.
440const uint32_t kIsNotStringMask = 0x80;
441const uint32_t kStringTag = 0x0;
442const uint32_t kNotStringTag = 0x80;
443
Leon Clarkee46be812010-01-19 14:06:41 +0000444// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
445// There are not enough types that the non-string types (with bit 7 set) can
446// have bit 6 set too.
447const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000448const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000449const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000450
Steve Blocka7e24c12009-10-30 11:49:00 +0000451// If bit 7 is clear then bit 2 indicates whether the string consists of
452// two-byte characters or one-byte characters.
453const uint32_t kStringEncodingMask = 0x4;
454const uint32_t kTwoByteStringTag = 0x0;
455const uint32_t kAsciiStringTag = 0x4;
456
457// If bit 7 is clear, the low-order 2 bits indicate the representation
458// of the string.
459const uint32_t kStringRepresentationMask = 0x03;
460enum StringRepresentationTag {
461 kSeqStringTag = 0x0,
462 kConsStringTag = 0x1,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100463 kExternalStringTag = 0x2
Steve Blocka7e24c12009-10-30 11:49:00 +0000464};
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100465const uint32_t kIsConsStringMask = 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000466
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100467// If bit 7 is clear, then bit 3 indicates whether this two-byte
468// string actually contains ascii data.
469const uint32_t kAsciiDataHintMask = 0x08;
470const uint32_t kAsciiDataHintTag = 0x08;
471
Steve Blocka7e24c12009-10-30 11:49:00 +0000472
473// A ConsString with an empty string as the right side is a candidate
474// for being shortcut by the garbage collector unless it is a
475// symbol. It's not common to have non-flat symbols, so we do not
476// shortcut them thereby avoiding turning symbols into strings. See
477// heap.cc and mark-compact.cc.
478const uint32_t kShortcutTypeMask =
479 kIsNotStringMask |
480 kIsSymbolMask |
481 kStringRepresentationMask;
482const uint32_t kShortcutTypeTag = kConsStringTag;
483
484
485enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000486 // String types.
Steve Block1e0659c2011-05-24 12:43:12 +0100487 // FIRST_STRING_TYPE
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100488 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000489 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100490 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000491 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100492 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
493 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
494 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000495 EXTERNAL_ASCII_SYMBOL_TYPE =
496 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100497 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000498 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100499 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000500 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100501 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
502 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
503 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Block1e0659c2011-05-24 12:43:12 +0100504 // LAST_STRING_TYPE
Steve Blockd0582a62009-12-15 09:54:21 +0000505 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
506 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000507
Leon Clarkee46be812010-01-19 14:06:41 +0000508 // Objects allocated in their own spaces (never in new space).
509 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000510 CODE_TYPE,
511 ODDBALL_TYPE,
512 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000513
514 // "Data", objects that cannot contain non-map-word pointers to heap
515 // objects.
516 HEAP_NUMBER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 PROXY_TYPE,
518 BYTE_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000519 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000520 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
521 EXTERNAL_SHORT_ARRAY_TYPE,
522 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
523 EXTERNAL_INT_ARRAY_TYPE,
524 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100525 EXTERNAL_FLOAT_ARRAY_TYPE,
526 EXTERNAL_PIXEL_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
Leon Clarkee46be812010-01-19 14:06:41 +0000527 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000528
Leon Clarkee46be812010-01-19 14:06:41 +0000529 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000530 ACCESSOR_INFO_TYPE,
531 ACCESS_CHECK_INFO_TYPE,
532 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000533 CALL_HANDLER_INFO_TYPE,
534 FUNCTION_TEMPLATE_INFO_TYPE,
535 OBJECT_TEMPLATE_INFO_TYPE,
536 SIGNATURE_INFO_TYPE,
537 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000538 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100539 CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100540 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
541 // is defined. However as include/v8.h contain some of the instance type
542 // constants always having them avoids them getting different numbers
543 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000544 DEBUG_INFO_TYPE,
545 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000546
Leon Clarkee46be812010-01-19 14:06:41 +0000547 FIXED_ARRAY_TYPE,
548 SHARED_FUNCTION_INFO_TYPE,
549
Steve Block1e0659c2011-05-24 12:43:12 +0100550 JS_MESSAGE_OBJECT_TYPE,
551
Leon Clarkee46be812010-01-19 14:06:41 +0000552 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 JS_OBJECT_TYPE,
554 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
555 JS_GLOBAL_OBJECT_TYPE,
556 JS_BUILTINS_OBJECT_TYPE,
557 JS_GLOBAL_PROXY_TYPE,
558 JS_ARRAY_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100559
560 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE, FIRST_FUNCTION_CLASS_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000561
562 JS_FUNCTION_TYPE,
563
564 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000566 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000567 INVALID_TYPE = FIRST_TYPE - 1,
568 FIRST_NONSTRING_TYPE = MAP_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100569 FIRST_STRING_TYPE = FIRST_TYPE,
570 LAST_STRING_TYPE = FIRST_NONSTRING_TYPE - 1,
Leon Clarkee46be812010-01-19 14:06:41 +0000571 // Boundaries for testing for an external array.
572 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100573 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000574 // Boundary for promotion to old data space/old pointer space.
575 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 // Boundaries for testing the type is a JavaScript "object". Note that
577 // function objects are not counted as objects, even though they are
578 // implemented as such; only values whose typeof is "object" are included.
579 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100580 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE,
581 // RegExp objects have [[Class]] "function" because they are callable.
582 // All types from this type and above are objects with [[Class]] "function".
583 FIRST_FUNCTION_CLASS_TYPE = JS_REGEXP_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000584};
585
Steve Block44f0eee2011-05-26 01:26:41 +0100586static const int kExternalArrayTypeCount = LAST_EXTERNAL_ARRAY_TYPE -
587 FIRST_EXTERNAL_ARRAY_TYPE + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000588
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100589STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
590STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
591STATIC_CHECK(PROXY_TYPE == Internals::kProxyType);
592
593
Steve Blocka7e24c12009-10-30 11:49:00 +0000594enum CompareResult {
595 LESS = -1,
596 EQUAL = 0,
597 GREATER = 1,
598
599 NOT_EQUAL = GREATER
600};
601
602
603#define DECL_BOOLEAN_ACCESSORS(name) \
604 inline bool name(); \
605 inline void set_##name(bool value); \
606
607
608#define DECL_ACCESSORS(name, type) \
609 inline type* name(); \
610 inline void set_##name(type* value, \
611 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
612
613
614class StringStream;
615class ObjectVisitor;
Steve Block053d10c2011-06-13 19:13:29 +0100616class Failure;
Steve Blocka7e24c12009-10-30 11:49:00 +0000617
618struct ValueInfo : public Malloced {
619 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
620 InstanceType type;
621 Object* ptr;
622 const char* str;
623 double number;
624};
625
626
627// A template-ized version of the IsXXX functions.
628template <class C> static inline bool Is(Object* obj);
629
Ben Murdochb0fe1622011-05-05 13:52:32 +0100630
John Reck59135872010-11-02 12:39:01 -0700631class MaybeObject BASE_EMBEDDED {
632 public:
633 inline bool IsFailure();
634 inline bool IsRetryAfterGC();
635 inline bool IsOutOfMemory();
636 inline bool IsException();
637 INLINE(bool IsTheHole());
638 inline bool ToObject(Object** obj) {
639 if (IsFailure()) return false;
640 *obj = reinterpret_cast<Object*>(this);
641 return true;
642 }
Steve Block053d10c2011-06-13 19:13:29 +0100643 inline Failure* ToFailureUnchecked() {
644 ASSERT(IsFailure());
645 return reinterpret_cast<Failure*>(this);
646 }
John Reck59135872010-11-02 12:39:01 -0700647 inline Object* ToObjectUnchecked() {
648 ASSERT(!IsFailure());
649 return reinterpret_cast<Object*>(this);
650 }
651 inline Object* ToObjectChecked() {
652 CHECK(!IsFailure());
653 return reinterpret_cast<Object*>(this);
654 }
655
Steve Block053d10c2011-06-13 19:13:29 +0100656 template<typename T>
657 inline bool To(T** obj) {
658 if (IsFailure()) return false;
659 *obj = T::cast(reinterpret_cast<Object*>(this));
660 return true;
661 }
662
Ben Murdochb0fe1622011-05-05 13:52:32 +0100663#ifdef OBJECT_PRINT
John Reck59135872010-11-02 12:39:01 -0700664 // Prints this object with details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100665 inline void Print() {
666 Print(stdout);
667 };
668 inline void PrintLn() {
669 PrintLn(stdout);
670 }
671 void Print(FILE* out);
672 void PrintLn(FILE* out);
673#endif
674#ifdef DEBUG
John Reck59135872010-11-02 12:39:01 -0700675 // Verifies the object.
676 void Verify();
677#endif
678};
Steve Blocka7e24c12009-10-30 11:49:00 +0000679
Ben Murdochb8e0da22011-05-16 14:20:40 +0100680
681#define OBJECT_TYPE_LIST(V) \
682 V(Smi) \
683 V(HeapObject) \
684 V(Number) \
685
686#define HEAP_OBJECT_TYPE_LIST(V) \
687 V(HeapNumber) \
688 V(String) \
689 V(Symbol) \
690 V(SeqString) \
691 V(ExternalString) \
692 V(ConsString) \
693 V(ExternalTwoByteString) \
694 V(ExternalAsciiString) \
695 V(SeqTwoByteString) \
696 V(SeqAsciiString) \
697 \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100698 V(ExternalArray) \
699 V(ExternalByteArray) \
700 V(ExternalUnsignedByteArray) \
701 V(ExternalShortArray) \
702 V(ExternalUnsignedShortArray) \
703 V(ExternalIntArray) \
704 V(ExternalUnsignedIntArray) \
705 V(ExternalFloatArray) \
Steve Block44f0eee2011-05-26 01:26:41 +0100706 V(ExternalPixelArray) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100707 V(ByteArray) \
708 V(JSObject) \
709 V(JSContextExtensionObject) \
710 V(Map) \
711 V(DescriptorArray) \
712 V(DeoptimizationInputData) \
713 V(DeoptimizationOutputData) \
714 V(FixedArray) \
715 V(Context) \
716 V(CatchContext) \
717 V(GlobalContext) \
718 V(JSFunction) \
719 V(Code) \
720 V(Oddball) \
721 V(SharedFunctionInfo) \
722 V(JSValue) \
Steve Block1e0659c2011-05-24 12:43:12 +0100723 V(JSMessageObject) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100724 V(StringWrapper) \
725 V(Proxy) \
726 V(Boolean) \
727 V(JSArray) \
728 V(JSRegExp) \
729 V(HashTable) \
730 V(Dictionary) \
731 V(SymbolTable) \
732 V(JSFunctionResultCache) \
733 V(NormalizedMapCache) \
734 V(CompilationCacheTable) \
735 V(CodeCacheHashTable) \
736 V(MapCache) \
737 V(Primitive) \
738 V(GlobalObject) \
739 V(JSGlobalObject) \
740 V(JSBuiltinsObject) \
741 V(JSGlobalProxy) \
742 V(UndetectableObject) \
743 V(AccessCheckNeeded) \
744 V(JSGlobalPropertyCell) \
745
Steve Blocka7e24c12009-10-30 11:49:00 +0000746// Object is the abstract superclass for all classes in the
747// object hierarchy.
748// Object does not use any virtual functions to avoid the
749// allocation of the C++ vtable.
750// Since Smi and Failure are subclasses of Object no
751// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700752class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000753 public:
754 // Type testing.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100755#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
756 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
757 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
758#undef IS_TYPE_FUNCTION_DECL
Steve Blocka7e24c12009-10-30 11:49:00 +0000759
760 // Returns true if this object is an instance of the specified
761 // function template.
762 inline bool IsInstanceOf(FunctionTemplateInfo* type);
763
764 inline bool IsStruct();
765#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
766 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
767#undef DECLARE_STRUCT_PREDICATE
768
769 // Oddball testing.
770 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000771 INLINE(bool IsNull());
Steve Block44f0eee2011-05-26 01:26:41 +0100772 INLINE(bool IsTheHole()); // Shadows MaybeObject's implementation.
Steve Blocka7e24c12009-10-30 11:49:00 +0000773 INLINE(bool IsTrue());
774 INLINE(bool IsFalse());
Ben Murdoch086aeea2011-05-13 15:57:08 +0100775 inline bool IsArgumentsMarker();
Steve Blocka7e24c12009-10-30 11:49:00 +0000776
777 // Extract the number.
778 inline double Number();
779
780 inline bool HasSpecificClassOf(String* name);
781
John Reck59135872010-11-02 12:39:01 -0700782 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
783 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000784
785 // Convert to a JSObject if needed.
786 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700787 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000788
789 // Converts this to a Smi if possible.
790 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700791 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000792
793 void Lookup(String* name, LookupResult* result);
794
795 // Property access.
John Reck59135872010-11-02 12:39:01 -0700796 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
797 MUST_USE_RESULT inline MaybeObject* GetProperty(
798 String* key,
799 PropertyAttributes* attributes);
800 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
801 Object* receiver,
802 String* key,
803 PropertyAttributes* attributes);
804 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
805 LookupResult* result,
806 String* key,
807 PropertyAttributes* attributes);
808 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
809 Object* structure,
810 String* name,
811 Object* holder);
812 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
813 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000814
John Reck59135872010-11-02 12:39:01 -0700815 inline MaybeObject* GetElement(uint32_t index);
816 // For use when we know that no exception can be thrown.
817 inline Object* GetElementNoExceptionThrown(uint32_t index);
818 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000819
820 // Return the object's prototype (might be Heap::null_value()).
821 Object* GetPrototype();
822
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100823 // Tries to convert an object to an array index. Returns true and sets
824 // the output parameter if it succeeds.
825 inline bool ToArrayIndex(uint32_t* index);
826
Steve Blocka7e24c12009-10-30 11:49:00 +0000827 // Returns true if this is a JSValue containing a string and the index is
828 // < the length of the string. Used to implement [] on strings.
829 inline bool IsStringObjectWithCharacterAt(uint32_t index);
830
831#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000832 // Verify a pointer is a valid object pointer.
833 static void VerifyPointer(Object* p);
834#endif
835
836 // Prints this object without details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100837 inline void ShortPrint() {
838 ShortPrint(stdout);
839 }
840 void ShortPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000841
842 // Prints this object without details to a message accumulator.
843 void ShortPrint(StringStream* accumulator);
844
845 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
846 static Object* cast(Object* value) { return value; }
847
848 // Layout description.
849 static const int kHeaderSize = 0; // Object does not take up any space.
850
851 private:
852 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
853};
854
855
856// Smi represents integer Numbers that can be stored in 31 bits.
857// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000858// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000859// For long smis it has the following format:
860// [32 bit signed int] [31 bits zero padding] 0
861// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000862class Smi: public Object {
863 public:
864 // Returns the integer value.
865 inline int value();
866
867 // Convert a value to a Smi object.
868 static inline Smi* FromInt(int value);
869
870 static inline Smi* FromIntptr(intptr_t value);
871
872 // Returns whether value can be represented in a Smi.
873 static inline bool IsValid(intptr_t value);
874
Steve Blocka7e24c12009-10-30 11:49:00 +0000875 // Casting.
876 static inline Smi* cast(Object* object);
877
878 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100879 inline void SmiPrint() {
880 SmiPrint(stdout);
881 }
882 void SmiPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 void SmiPrint(StringStream* accumulator);
884#ifdef DEBUG
885 void SmiVerify();
886#endif
887
Steve Block3ce2e202009-11-05 08:53:23 +0000888 static const int kMinValue = (-1 << (kSmiValueSize - 1));
889 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000890
891 private:
892 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
893};
894
895
896// Failure is used for reporting out of memory situations and
897// propagating exceptions through the runtime system. Failure objects
898// are transient and cannot occur as part of the object graph.
899//
900// Failures are a single word, encoded as follows:
901// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +0100902// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000903// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000904// 7 6 4 32 10
905//
Steve Blocka7e24c12009-10-30 11:49:00 +0000906//
907// The low two bits, 0-1, are the failure tag, 11. The next two bits,
908// 2-3, are a failure type tag 'tt' with possible values:
909// 00 RETRY_AFTER_GC
910// 01 EXCEPTION
911// 10 INTERNAL_ERROR
912// 11 OUT_OF_MEMORY_EXCEPTION
913//
914// The next three bits, 4-6, are an allocation space tag 'sss'. The
915// allocation space tag is 000 for all failure types except
916// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
917// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +0000918
919// Failure type tag info.
920const int kFailureTypeTagSize = 2;
921const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
922
John Reck59135872010-11-02 12:39:01 -0700923class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000924 public:
925 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
926 enum Type {
927 RETRY_AFTER_GC = 0,
928 EXCEPTION = 1, // Returning this marker tells the real exception
Steve Block44f0eee2011-05-26 01:26:41 +0100929 // is in Isolate::pending_exception.
Steve Blocka7e24c12009-10-30 11:49:00 +0000930 INTERNAL_ERROR = 2,
931 OUT_OF_MEMORY_EXCEPTION = 3
932 };
933
934 inline Type type() const;
935
936 // Returns the space that needs to be collected for RetryAfterGC failures.
937 inline AllocationSpace allocation_space() const;
938
Steve Blocka7e24c12009-10-30 11:49:00 +0000939 inline bool IsInternalError() const;
940 inline bool IsOutOfMemoryException() const;
941
Ben Murdochf87a2032010-10-22 12:50:53 +0100942 static inline Failure* RetryAfterGC(AllocationSpace space);
943 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000944 static inline Failure* Exception();
945 static inline Failure* InternalError();
946 static inline Failure* OutOfMemoryException();
947 // Casting.
John Reck59135872010-11-02 12:39:01 -0700948 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000949
950 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100951 inline void FailurePrint() {
952 FailurePrint(stdout);
953 }
954 void FailurePrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000955 void FailurePrint(StringStream* accumulator);
956#ifdef DEBUG
957 void FailureVerify();
958#endif
959
960 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000961 inline intptr_t value() const;
962 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000963
964 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
965};
966
967
968// Heap objects typically have a map pointer in their first word. However,
969// during GC other data (eg, mark bits, forwarding addresses) is sometimes
970// encoded in the first word. The class MapWord is an abstraction of the
971// value in a heap object's first word.
972class MapWord BASE_EMBEDDED {
973 public:
974 // Normal state: the map word contains a map pointer.
975
976 // Create a map word from a map pointer.
977 static inline MapWord FromMap(Map* map);
978
979 // View this map word as a map pointer.
980 inline Map* ToMap();
981
982
983 // Scavenge collection: the map word of live objects in the from space
984 // contains a forwarding address (a heap object pointer in the to space).
985
986 // True if this map word is a forwarding address for a scavenge
987 // collection. Only valid during a scavenge collection (specifically,
988 // when all map words are heap object pointers, ie. not during a full GC).
989 inline bool IsForwardingAddress();
990
991 // Create a map word from a forwarding address.
992 static inline MapWord FromForwardingAddress(HeapObject* object);
993
994 // View this map word as a forwarding address.
995 inline HeapObject* ToForwardingAddress();
996
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 // Marking phase of full collection: the map word of live objects is
998 // marked, and may be marked as overflowed (eg, the object is live, its
999 // children have not been visited, and it does not fit in the marking
1000 // stack).
1001
1002 // True if this map word's mark bit is set.
1003 inline bool IsMarked();
1004
1005 // Return this map word but with its mark bit set.
1006 inline void SetMark();
1007
1008 // Return this map word but with its mark bit cleared.
1009 inline void ClearMark();
1010
1011 // True if this map word's overflow bit is set.
1012 inline bool IsOverflowed();
1013
1014 // Return this map word but with its overflow bit set.
1015 inline void SetOverflow();
1016
1017 // Return this map word but with its overflow bit cleared.
1018 inline void ClearOverflow();
1019
1020
1021 // Compacting phase of a full compacting collection: the map word of live
1022 // objects contains an encoding of the original map address along with the
1023 // forwarding address (represented as an offset from the first live object
1024 // in the same page as the (old) object address).
1025
1026 // Create a map word from a map address and a forwarding address offset.
1027 static inline MapWord EncodeAddress(Address map_address, int offset);
1028
1029 // Return the map address encoded in this map word.
1030 inline Address DecodeMapAddress(MapSpace* map_space);
1031
1032 // Return the forwarding offset encoded in this map word.
1033 inline int DecodeOffset();
1034
1035
1036 // During serialization: the map word is used to hold an encoded
1037 // address, and possibly a mark bit (set and cleared with SetMark
1038 // and ClearMark).
1039
1040 // Create a map word from an encoded address.
1041 static inline MapWord FromEncodedAddress(Address address);
1042
1043 inline Address ToEncodedAddress();
1044
1045 // Bits used by the marking phase of the garbage collector.
1046 //
1047 // The first word of a heap object is normally a map pointer. The last two
1048 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1049 // mark an object as live and/or overflowed:
1050 // last bit = 0, marked as alive
1051 // second bit = 1, overflowed
1052 // An object is only marked as overflowed when it is marked as live while
1053 // the marking stack is overflowed.
1054 static const int kMarkingBit = 0; // marking bit
1055 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1056 static const int kOverflowBit = 1; // overflow bit
1057 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1058
Leon Clarkee46be812010-01-19 14:06:41 +00001059 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1060 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 // +-----------------+------------------+-----------------+
1062 // |forwarding offset|page offset of map|page index of map|
1063 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +00001064 // ^ ^ ^
1065 // | | |
1066 // | | kMapPageIndexBits
1067 // | kMapPageOffsetBits
1068 // kForwardingOffsetBits
1069 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1070 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1071#ifdef V8_HOST_ARCH_64_BIT
1072 static const int kMapPageIndexBits = 16;
1073#else
1074 // Use all the 32-bits to encode on a 32-bit platform.
1075 static const int kMapPageIndexBits =
1076 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1077#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001078
1079 static const int kMapPageIndexShift = 0;
1080 static const int kMapPageOffsetShift =
1081 kMapPageIndexShift + kMapPageIndexBits;
1082 static const int kForwardingOffsetShift =
1083 kMapPageOffsetShift + kMapPageOffsetBits;
1084
Leon Clarkee46be812010-01-19 14:06:41 +00001085 // Bit masks covering the different parts the encoding.
1086 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001087 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001088 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001089 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001090 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001091 ~(kMapPageIndexMask | kMapPageOffsetMask);
1092
1093 private:
1094 // HeapObject calls the private constructor and directly reads the value.
1095 friend class HeapObject;
1096
1097 explicit MapWord(uintptr_t value) : value_(value) {}
1098
1099 uintptr_t value_;
1100};
1101
1102
1103// HeapObject is the superclass for all classes describing heap allocated
1104// objects.
1105class HeapObject: public Object {
1106 public:
1107 // [map]: Contains a map which contains the object's reflective
1108 // information.
1109 inline Map* map();
1110 inline void set_map(Map* value);
1111
1112 // During garbage collection, the map word of a heap object does not
1113 // necessarily contain a map pointer.
1114 inline MapWord map_word();
1115 inline void set_map_word(MapWord map_word);
1116
Steve Block44f0eee2011-05-26 01:26:41 +01001117 // The Heap the object was allocated in. Used also to access Isolate.
1118 // This method can not be used during GC, it ASSERTs this.
1119 inline Heap* GetHeap();
1120 // Convenience method to get current isolate. This method can be
1121 // accessed only when its result is the same as
1122 // Isolate::Current(), it ASSERTs this. See also comment for GetHeap.
1123 inline Isolate* GetIsolate();
1124
Steve Blocka7e24c12009-10-30 11:49:00 +00001125 // Converts an address to a HeapObject pointer.
1126 static inline HeapObject* FromAddress(Address address);
1127
1128 // Returns the address of this HeapObject.
1129 inline Address address();
1130
1131 // Iterates over pointers contained in the object (including the Map)
1132 void Iterate(ObjectVisitor* v);
1133
1134 // Iterates over all pointers contained in the object except the
1135 // first map pointer. The object type is given in the first
1136 // parameter. This function does not access the map pointer in the
1137 // object, and so is safe to call while the map pointer is modified.
1138 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1139
Steve Blocka7e24c12009-10-30 11:49:00 +00001140 // Returns the heap object's size in bytes
1141 inline int Size();
1142
1143 // Given a heap object's map pointer, returns the heap size in bytes
1144 // Useful when the map pointer field is used for other purposes.
1145 // GC internal.
1146 inline int SizeFromMap(Map* map);
1147
1148 // Support for the marking heap objects during the marking phase of GC.
1149 // True if the object is marked live.
1150 inline bool IsMarked();
1151
1152 // Mutate this object's map pointer to indicate that the object is live.
1153 inline void SetMark();
1154
1155 // Mutate this object's map pointer to remove the indication that the
1156 // object is live (ie, partially restore the map pointer).
1157 inline void ClearMark();
1158
1159 // True if this object is marked as overflowed. Overflowed objects have
1160 // been reached and marked during marking of the heap, but their children
1161 // have not necessarily been marked and they have not been pushed on the
1162 // marking stack.
1163 inline bool IsOverflowed();
1164
1165 // Mutate this object's map pointer to indicate that the object is
1166 // overflowed.
1167 inline void SetOverflow();
1168
1169 // Mutate this object's map pointer to remove the indication that the
1170 // object is overflowed (ie, partially restore the map pointer).
1171 inline void ClearOverflow();
1172
1173 // Returns the field at offset in obj, as a read/write Object* reference.
1174 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001175 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001176 // during marking GC.
1177 static inline Object** RawField(HeapObject* obj, int offset);
1178
1179 // Casting.
1180 static inline HeapObject* cast(Object* obj);
1181
Leon Clarke4515c472010-02-03 11:58:03 +00001182 // Return the write barrier mode for this. Callers of this function
1183 // must be able to present a reference to an AssertNoAllocation
1184 // object as a sign that they are not going to use this function
1185 // from code that allocates and thus invalidates the returned write
1186 // barrier mode.
1187 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001188
1189 // Dispatched behavior.
1190 void HeapObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001191#ifdef OBJECT_PRINT
1192 inline void HeapObjectPrint() {
1193 HeapObjectPrint(stdout);
1194 }
1195 void HeapObjectPrint(FILE* out);
1196#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001197#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001198 void HeapObjectVerify();
1199 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001200 inline void VerifySmiField(int offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001201#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001202
Ben Murdochb0fe1622011-05-05 13:52:32 +01001203#ifdef OBJECT_PRINT
1204 void PrintHeader(FILE* out, const char* id);
1205#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001206
Ben Murdochb0fe1622011-05-05 13:52:32 +01001207#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001208 // Verify a pointer is a valid HeapObject pointer that points to object
1209 // areas in the heap.
1210 static void VerifyHeapPointer(Object* p);
1211#endif
1212
1213 // Layout description.
1214 // First field in a heap object is map.
1215 static const int kMapOffset = Object::kHeaderSize;
1216 static const int kHeaderSize = kMapOffset + kPointerSize;
1217
1218 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1219
1220 protected:
1221 // helpers for calling an ObjectVisitor to iterate over pointers in the
1222 // half-open range [start, end) specified as integer offsets
1223 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1224 // as above, for the single element at "offset"
1225 inline void IteratePointer(ObjectVisitor* v, int offset);
1226
Steve Blocka7e24c12009-10-30 11:49:00 +00001227 private:
1228 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1229};
1230
1231
Iain Merrick75681382010-08-19 15:07:18 +01001232#define SLOT_ADDR(obj, offset) \
1233 reinterpret_cast<Object**>((obj)->address() + offset)
1234
1235// This class describes a body of an object of a fixed size
1236// in which all pointer fields are located in the [start_offset, end_offset)
1237// interval.
1238template<int start_offset, int end_offset, int size>
1239class FixedBodyDescriptor {
1240 public:
1241 static const int kStartOffset = start_offset;
1242 static const int kEndOffset = end_offset;
1243 static const int kSize = size;
1244
1245 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1246
1247 template<typename StaticVisitor>
1248 static inline void IterateBody(HeapObject* obj) {
1249 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1250 SLOT_ADDR(obj, end_offset));
1251 }
1252};
1253
1254
1255// This class describes a body of an object of a variable size
1256// in which all pointer fields are located in the [start_offset, object_size)
1257// interval.
1258template<int start_offset>
1259class FlexibleBodyDescriptor {
1260 public:
1261 static const int kStartOffset = start_offset;
1262
1263 static inline void IterateBody(HeapObject* obj,
1264 int object_size,
1265 ObjectVisitor* v);
1266
1267 template<typename StaticVisitor>
1268 static inline void IterateBody(HeapObject* obj, int object_size) {
1269 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1270 SLOT_ADDR(obj, object_size));
1271 }
1272};
1273
1274#undef SLOT_ADDR
1275
1276
Steve Blocka7e24c12009-10-30 11:49:00 +00001277// The HeapNumber class describes heap allocated numbers that cannot be
1278// represented in a Smi (small integer)
1279class HeapNumber: public HeapObject {
1280 public:
1281 // [value]: number value.
1282 inline double value();
1283 inline void set_value(double value);
1284
1285 // Casting.
1286 static inline HeapNumber* cast(Object* obj);
1287
1288 // Dispatched behavior.
1289 Object* HeapNumberToBoolean();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001290 inline void HeapNumberPrint() {
1291 HeapNumberPrint(stdout);
1292 }
1293 void HeapNumberPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001294 void HeapNumberPrint(StringStream* accumulator);
1295#ifdef DEBUG
1296 void HeapNumberVerify();
1297#endif
1298
Steve Block6ded16b2010-05-10 14:33:55 +01001299 inline int get_exponent();
1300 inline int get_sign();
1301
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 // Layout description.
1303 static const int kValueOffset = HeapObject::kHeaderSize;
1304 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1305 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1306 // little endian apart from non-EABI arm which is little endian with big
1307 // endian floating point word ordering!
Steve Blocka7e24c12009-10-30 11:49:00 +00001308 static const int kMantissaOffset = kValueOffset;
1309 static const int kExponentOffset = kValueOffset + 4;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001310
Steve Blocka7e24c12009-10-30 11:49:00 +00001311 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001312 static const uint32_t kSignMask = 0x80000000u;
1313 static const uint32_t kExponentMask = 0x7ff00000u;
1314 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001315 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001316 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001317 static const int kExponentBias = 1023;
1318 static const int kExponentShift = 20;
1319 static const int kMantissaBitsInTopWord = 20;
1320 static const int kNonMantissaBitsInTopWord = 12;
1321
1322 private:
1323 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1324};
1325
1326
1327// The JSObject describes real heap allocated JavaScript objects with
1328// properties.
1329// Note that the map of JSObject changes during execution to enable inline
1330// caching.
1331class JSObject: public HeapObject {
1332 public:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001333 enum DeleteMode {
1334 NORMAL_DELETION,
1335 STRICT_DELETION,
1336 FORCE_DELETION
1337 };
1338
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001340 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001341 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001342 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001344 EXTERNAL_BYTE_ELEMENTS,
1345 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1346 EXTERNAL_SHORT_ELEMENTS,
1347 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1348 EXTERNAL_INT_ELEMENTS,
1349 EXTERNAL_UNSIGNED_INT_ELEMENTS,
Steve Block44f0eee2011-05-26 01:26:41 +01001350 EXTERNAL_FLOAT_ELEMENTS,
1351 EXTERNAL_PIXEL_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001352 };
1353
1354 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001355 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001356 // slow case.
1357 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1358 inline void initialize_properties();
1359 inline bool HasFastProperties();
1360 inline StringDictionary* property_dictionary(); // Gets slow properties.
1361
1362 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001363 //
1364 // Elements can be in two general modes: fast and slow. Each mode
1365 // corrensponds to a set of object representations of elements that
1366 // have something in common.
1367 //
1368 // In the fast mode elements is a FixedArray and so each element can
1369 // be quickly accessed. This fact is used in the generated code. The
1370 // elements array can have one of the two maps in this mode:
1371 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1372 // arrays). In the latter case the elements array may be shared by a
1373 // few objects and so before writing to any element the array must
1374 // be copied. Use EnsureWritableFastElements in this case.
1375 //
Steve Block44f0eee2011-05-26 01:26:41 +01001376 // In the slow mode elements is either a NumberDictionary or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001377 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001379 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001380 inline ElementsKind GetElementsKind();
1381 inline bool HasFastElements();
1382 inline bool HasDictionaryElements();
Steve Block44f0eee2011-05-26 01:26:41 +01001383 inline bool HasExternalPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001384 inline bool HasExternalArrayElements();
1385 inline bool HasExternalByteElements();
1386 inline bool HasExternalUnsignedByteElements();
1387 inline bool HasExternalShortElements();
1388 inline bool HasExternalUnsignedShortElements();
1389 inline bool HasExternalIntElements();
1390 inline bool HasExternalUnsignedIntElements();
1391 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001392 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001393 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001394 // Requires: this->HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001395 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001396
1397 // Collects elements starting at index 0.
1398 // Undefined values are placed after non-undefined values.
1399 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001400 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001401 // As PrepareElementsForSort, but only on objects where elements is
1402 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001403 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001404
John Reck59135872010-11-02 12:39:01 -07001405 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1406 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001407 PropertyAttributes attributes,
1408 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001409 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1410 String* key,
1411 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001412 PropertyAttributes attributes,
1413 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001414 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1415 LookupResult* result,
1416 String* name,
Ben Murdoch086aeea2011-05-13 15:57:08 +01001417 Object* value,
1418 bool check_prototype);
John Reck59135872010-11-02 12:39:01 -07001419 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(Object* structure,
1420 String* name,
1421 Object* value,
1422 JSObject* holder);
1423 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1424 Object* value);
1425 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1426 String* name,
1427 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001428 PropertyAttributes attributes,
1429 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001430 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1431 String* name,
1432 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001433 PropertyAttributes attributes,
1434 StrictModeFlag strict_mode);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001435 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
John Reck59135872010-11-02 12:39:01 -07001436 String* key,
1437 Object* value,
1438 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001439
1440 // Retrieve a value in a normalized object given a lookup result.
1441 // Handles the special representation of JS global objects.
1442 Object* GetNormalizedProperty(LookupResult* result);
1443
1444 // Sets the property value in a normalized object given a lookup result.
1445 // Handles the special representation of JS global objects.
1446 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1447
1448 // Sets the property value in a normalized object given (key, value, details).
1449 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001450 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1451 Object* value,
1452 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001453
1454 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001455 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1456 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001457
Steve Blocka7e24c12009-10-30 11:49:00 +00001458 // Returns the class name ([[Class]] property in the specification).
1459 String* class_name();
1460
1461 // Returns the constructor name (the name (possibly, inferred name) of the
1462 // function that was used to instantiate the object).
1463 String* constructor_name();
1464
1465 // Retrieve interceptors.
1466 InterceptorInfo* GetNamedInterceptor();
1467 InterceptorInfo* GetIndexedInterceptor();
1468
1469 inline PropertyAttributes GetPropertyAttribute(String* name);
1470 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1471 String* name);
1472 PropertyAttributes GetLocalPropertyAttribute(String* name);
1473
John Reck59135872010-11-02 12:39:01 -07001474 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1475 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001476 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001477 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001478 Object* LookupAccessor(String* name, bool is_getter);
1479
John Reck59135872010-11-02 12:39:01 -07001480 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001481
Steve Blocka7e24c12009-10-30 11:49:00 +00001482 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001483 MaybeObject* GetPropertyWithFailedAccessCheck(
1484 Object* receiver,
1485 LookupResult* result,
1486 String* name,
1487 PropertyAttributes* attributes);
1488 MaybeObject* GetPropertyWithInterceptor(
1489 JSObject* receiver,
1490 String* name,
1491 PropertyAttributes* attributes);
1492 MaybeObject* GetPropertyPostInterceptor(
1493 JSObject* receiver,
1494 String* name,
1495 PropertyAttributes* attributes);
1496 MaybeObject* GetLocalPropertyPostInterceptor(JSObject* receiver,
1497 String* name,
1498 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001499
1500 // Returns true if this is an instance of an api function and has
1501 // been modified since it was created. May give false positives.
1502 bool IsDirty();
1503
1504 bool HasProperty(String* name) {
1505 return GetPropertyAttribute(name) != ABSENT;
1506 }
1507
1508 // Can cause a GC if it hits an interceptor.
1509 bool HasLocalProperty(String* name) {
1510 return GetLocalPropertyAttribute(name) != ABSENT;
1511 }
1512
Steve Blockd0582a62009-12-15 09:54:21 +00001513 // If the receiver is a JSGlobalProxy this method will return its prototype,
1514 // otherwise the result is the receiver itself.
1515 inline Object* BypassGlobalProxy();
1516
1517 // Accessors for hidden properties object.
1518 //
1519 // Hidden properties are not local properties of the object itself.
1520 // Instead they are stored on an auxiliary JSObject stored as a local
1521 // property with a special name Heap::hidden_symbol(). But if the
1522 // receiver is a JSGlobalProxy then the auxiliary object is a property
1523 // of its prototype.
1524 //
1525 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1526 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1527 // holder.
1528 //
1529 // These accessors do not touch interceptors or accessors.
1530 inline bool HasHiddenPropertiesObject();
1531 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001532 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1533 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001534
John Reck59135872010-11-02 12:39:01 -07001535 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1536 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001537
1538 // Tests for the fast common case for property enumeration.
1539 bool IsSimpleEnum();
1540
1541 // Do we want to keep the elements in fast case when increasing the
1542 // capacity?
1543 bool ShouldConvertToSlowElements(int new_capacity);
1544 // Returns true if the backing storage for the slow-case elements of
1545 // this object takes up nearly as much space as a fast-case backing
1546 // storage would. In that case the JSObject should have fast
1547 // elements.
1548 bool ShouldConvertToFastElements();
1549
1550 // Return the object's prototype (might be Heap::null_value()).
1551 inline Object* GetPrototype();
1552
Andrei Popescu402d9372010-02-26 13:31:12 +00001553 // Set the object's prototype (only JSObject and null are allowed).
John Reck59135872010-11-02 12:39:01 -07001554 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1555 bool skip_hidden_prototypes);
Andrei Popescu402d9372010-02-26 13:31:12 +00001556
Steve Blocka7e24c12009-10-30 11:49:00 +00001557 // Tells whether the index'th element is present.
1558 inline bool HasElement(uint32_t index);
1559 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001560
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001561 // Computes the new capacity when expanding the elements of a JSObject.
1562 static int NewElementsCapacity(int old_capacity) {
1563 // (old_capacity + 50%) + 16
1564 return old_capacity + (old_capacity >> 1) + 16;
1565 }
1566
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001567 // Tells whether the index'th element is present and how it is stored.
1568 enum LocalElementType {
1569 // There is no element with given index.
1570 UNDEFINED_ELEMENT,
1571
1572 // Element with given index is handled by interceptor.
1573 INTERCEPTED_ELEMENT,
1574
1575 // Element with given index is character in string.
1576 STRING_CHARACTER_ELEMENT,
1577
1578 // Element with given index is stored in fast backing store.
1579 FAST_ELEMENT,
1580
1581 // Element with given index is stored in slow backing store.
1582 DICTIONARY_ELEMENT
1583 };
1584
1585 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001586
1587 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1588 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1589
Steve Block9fac8402011-05-12 15:51:54 +01001590 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1591 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001592 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001593 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001594
1595 // Set the index'th array element.
1596 // A Failure object is returned if GC is needed.
Steve Block9fac8402011-05-12 15:51:54 +01001597 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1598 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001599 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001600 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001601
1602 // Returns the index'th element.
1603 // The undefined object if index is out of bounds.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001604 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
1605 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
1606
1607 // Get external element value at index if there is one and undefined
1608 // otherwise. Can return a failure if allocation of a heap number
1609 // failed.
1610 MaybeObject* GetExternalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001611
John Reck59135872010-11-02 12:39:01 -07001612 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1613 int length);
1614 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001615
1616 // Lookup interceptors are used for handling properties controlled by host
1617 // objects.
1618 inline bool HasNamedInterceptor();
1619 inline bool HasIndexedInterceptor();
1620
1621 // Support functions for v8 api (needed for correct interceptor behavior).
1622 bool HasRealNamedProperty(String* key);
1623 bool HasRealElementProperty(uint32_t index);
1624 bool HasRealNamedCallbackProperty(String* key);
1625
1626 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001627 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001628
1629 // Get the header size for a JSObject. Used to compute the index of
1630 // internal fields as well as the number of internal fields.
1631 inline int GetHeaderSize();
1632
1633 inline int GetInternalFieldCount();
Steve Block44f0eee2011-05-26 01:26:41 +01001634 inline int GetInternalFieldOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001635 inline Object* GetInternalField(int index);
1636 inline void SetInternalField(int index, Object* value);
1637
1638 // Lookup a property. If found, the result is valid and has
1639 // detailed information.
1640 void LocalLookup(String* name, LookupResult* result);
1641 void Lookup(String* name, LookupResult* result);
1642
1643 // The following lookup functions skip interceptors.
1644 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1645 void LookupRealNamedProperty(String* name, LookupResult* result);
1646 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1647 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Steve Block1e0659c2011-05-24 12:43:12 +01001648 MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
1649 uint32_t index, Object* value, bool* found);
Steve Blocka7e24c12009-10-30 11:49:00 +00001650 void LookupCallback(String* name, LookupResult* result);
1651
1652 // Returns the number of properties on this object filtering out properties
1653 // with the specified attributes (ignoring interceptors).
1654 int NumberOfLocalProperties(PropertyAttributes filter);
1655 // Returns the number of enumerable properties (ignoring interceptors).
1656 int NumberOfEnumProperties();
1657 // Fill in details for properties into storage starting at the specified
1658 // index.
1659 void GetLocalPropertyNames(FixedArray* storage, int index);
1660
1661 // Returns the number of properties on this object filtering out properties
1662 // with the specified attributes (ignoring interceptors).
1663 int NumberOfLocalElements(PropertyAttributes filter);
1664 // Returns the number of enumerable elements (ignoring interceptors).
1665 int NumberOfEnumElements();
1666 // Returns the number of elements on this object filtering out elements
1667 // with the specified attributes (ignoring interceptors).
1668 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1669 // Count and fill in the enumerable elements into storage.
1670 // (storage->length() == NumberOfEnumElements()).
1671 // If storage is NULL, will count the elements without adding
1672 // them to any storage.
1673 // Returns the number of enumerable elements.
1674 int GetEnumElementKeys(FixedArray* storage);
1675
1676 // Add a property to a fast-case object using a map transition to
1677 // new_map.
John Reck59135872010-11-02 12:39:01 -07001678 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1679 String* name,
1680 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001681
1682 // Add a constant function property to a fast-case object.
1683 // This leaves a CONSTANT_TRANSITION in the old map, and
1684 // if it is called on a second object with this map, a
1685 // normal property is added instead, with a map transition.
1686 // This avoids the creation of many maps with the same constant
1687 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001688 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1689 String* name,
1690 JSFunction* function,
1691 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001692
John Reck59135872010-11-02 12:39:01 -07001693 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1694 String* name,
1695 Object* value,
1696 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001697
1698 // Converts a descriptor of any other type to a real field,
1699 // backed by the properties array. Descriptors of visible
1700 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1701 // Converts the descriptor on the original object's map to a
1702 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001703 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001704 String* name,
1705 Object* new_value,
1706 PropertyAttributes attributes);
1707
1708 // Converts a descriptor of any other type to a real field,
1709 // backed by the properties array. Descriptors of visible
1710 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001711 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1712 String* name,
1713 Object* new_value,
1714 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001715
1716 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001717 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1718 Object* value,
1719 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001720
1721 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001722 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1723 Object* value,
1724 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001725
1726 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001727 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1728 Object* value,
Steve Block44f0eee2011-05-26 01:26:41 +01001729 PropertyAttributes attributes,
1730 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001731
1732 // Convert the object to use the canonical dictionary
1733 // representation. If the object is expected to have additional properties
1734 // added this number can be indicated to have the backing store allocated to
1735 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001736 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1737 PropertyNormalizationMode mode,
1738 int expected_additional_properties);
1739 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001740
John Reck59135872010-11-02 12:39:01 -07001741 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001742
Steve Blocka7e24c12009-10-30 11:49:00 +00001743 // Transform slow named properties to fast variants.
1744 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001745 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1746 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001747
1748 // Access fast-case object properties at index.
1749 inline Object* FastPropertyAt(int index);
1750 inline Object* FastPropertyAtPut(int index, Object* value);
1751
1752 // Access to in object properties.
Steve Block44f0eee2011-05-26 01:26:41 +01001753 inline int GetInObjectPropertyOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001754 inline Object* InObjectPropertyAt(int index);
1755 inline Object* InObjectPropertyAtPut(int index,
1756 Object* value,
1757 WriteBarrierMode mode
1758 = UPDATE_WRITE_BARRIER);
1759
1760 // initializes the body after properties slot, properties slot is
1761 // initialized by set_properties
1762 // Note: this call does not update write barrier, it is caller's
1763 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001764 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001765
1766 // Check whether this object references another object
1767 bool ReferencesObject(Object* obj);
1768
1769 // Casting.
1770 static inline JSObject* cast(Object* obj);
1771
Steve Block8defd9f2010-07-08 12:39:36 +01001772 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001773 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001774
1775
Steve Blocka7e24c12009-10-30 11:49:00 +00001776 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001777 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001778#ifdef OBJECT_PRINT
1779 inline void JSObjectPrint() {
1780 JSObjectPrint(stdout);
1781 }
1782 void JSObjectPrint(FILE* out);
1783#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001784#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001785 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001786#endif
1787#ifdef OBJECT_PRINT
1788 inline void PrintProperties() {
1789 PrintProperties(stdout);
1790 }
1791 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001792
Ben Murdochb0fe1622011-05-05 13:52:32 +01001793 inline void PrintElements() {
1794 PrintElements(stdout);
1795 }
1796 void PrintElements(FILE* out);
1797#endif
1798
1799#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001800 // Structure for collecting spill information about JSObjects.
1801 class SpillInformation {
1802 public:
1803 void Clear();
1804 void Print();
1805 int number_of_objects_;
1806 int number_of_objects_with_fast_properties_;
1807 int number_of_objects_with_fast_elements_;
1808 int number_of_fast_used_fields_;
1809 int number_of_fast_unused_fields_;
1810 int number_of_slow_used_properties_;
1811 int number_of_slow_unused_properties_;
1812 int number_of_fast_used_elements_;
1813 int number_of_fast_unused_elements_;
1814 int number_of_slow_used_elements_;
1815 int number_of_slow_unused_elements_;
1816 };
1817
1818 void IncrementSpillStatistics(SpillInformation* info);
1819#endif
1820 Object* SlowReverseLookup(Object* value);
1821
Steve Block8defd9f2010-07-08 12:39:36 +01001822 // Maximal number of fast properties for the JSObject. Used to
1823 // restrict the number of map transitions to avoid an explosion in
1824 // the number of maps for objects used as dictionaries.
1825 inline int MaxFastProperties();
1826
Leon Clarkee46be812010-01-19 14:06:41 +00001827 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1828 // Also maximal value of JSArray's length property.
1829 static const uint32_t kMaxElementCount = 0xffffffffu;
1830
Steve Blocka7e24c12009-10-30 11:49:00 +00001831 static const uint32_t kMaxGap = 1024;
1832 static const int kMaxFastElementsLength = 5000;
1833 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001834 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00001835 static const int kMaxInstanceSize = 255 * kPointerSize;
1836 // When extending the backing storage for property values, we increase
1837 // its size by more than the 1 entry necessary, so sequentially adding fields
1838 // to the same object requires fewer allocations and copies.
1839 static const int kFieldsAdded = 3;
1840
1841 // Layout description.
1842 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1843 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1844 static const int kHeaderSize = kElementsOffset + kPointerSize;
1845
1846 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1847
Iain Merrick75681382010-08-19 15:07:18 +01001848 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1849 public:
1850 static inline int SizeOf(Map* map, HeapObject* object);
1851 };
1852
Steve Blocka7e24c12009-10-30 11:49:00 +00001853 private:
John Reck59135872010-11-02 12:39:01 -07001854 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1855 Object* structure,
1856 uint32_t index,
1857 Object* holder);
1858 MaybeObject* SetElementWithCallback(Object* structure,
1859 uint32_t index,
1860 Object* value,
1861 JSObject* holder);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001862 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
1863 uint32_t index,
1864 Object* value,
1865 StrictModeFlag strict_mode,
1866 bool check_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01001867 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
1868 uint32_t index,
1869 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001870 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001871 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00001872
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001873 MaybeObject* GetElementPostInterceptor(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001874
John Reck59135872010-11-02 12:39:01 -07001875 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1876 DeleteMode mode);
1877 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001878
John Reck59135872010-11-02 12:39:01 -07001879 MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1880 DeleteMode mode);
1881 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001882
1883 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1884 String* name,
1885 bool continue_search);
1886 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1887 String* name,
1888 bool continue_search);
1889 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1890 Object* receiver,
1891 LookupResult* result,
1892 String* name,
1893 bool continue_search);
1894 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1895 LookupResult* result,
1896 String* name,
1897 bool continue_search);
1898
1899 // Returns true if most of the elements backing storage is used.
1900 bool HasDenseElements();
1901
Leon Clarkef7060e22010-06-03 12:02:55 +01001902 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07001903 MUST_USE_RESULT MaybeObject* SetElementCallback(
1904 uint32_t index,
1905 Object* structure,
1906 PropertyAttributes attributes);
1907 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1908 String* name,
1909 Object* structure,
1910 PropertyAttributes attributes);
1911 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
1912 String* name,
1913 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001914
1915 void LookupInDescriptor(String* name, LookupResult* result);
1916
1917 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1918};
1919
1920
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001921// FixedArray describes fixed-sized arrays with element type Object*.
1922class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001923 public:
1924 // [length]: length of the array.
1925 inline int length();
1926 inline void set_length(int value);
1927
Steve Blocka7e24c12009-10-30 11:49:00 +00001928 // Setter and getter for elements.
1929 inline Object* get(int index);
1930 // Setter that uses write barrier.
1931 inline void set(int index, Object* value);
1932
1933 // Setter that doesn't need write barrier).
1934 inline void set(int index, Smi* value);
1935 // Setter with explicit barrier mode.
1936 inline void set(int index, Object* value, WriteBarrierMode mode);
1937
1938 // Setters for frequently used oddballs located in old space.
1939 inline void set_undefined(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01001940 // TODO(isolates): duplicate.
1941 inline void set_undefined(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001942 inline void set_null(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01001943 // TODO(isolates): duplicate.
1944 inline void set_null(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001945 inline void set_the_hole(int index);
1946
Iain Merrick75681382010-08-19 15:07:18 +01001947 // Setters with less debug checks for the GC to use.
1948 inline void set_unchecked(int index, Smi* value);
Steve Block44f0eee2011-05-26 01:26:41 +01001949 inline void set_null_unchecked(Heap* heap, int index);
1950 inline void set_unchecked(Heap* heap, int index, Object* value,
1951 WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01001952
Steve Block6ded16b2010-05-10 14:33:55 +01001953 // Gives access to raw memory which stores the array's data.
1954 inline Object** data_start();
1955
Steve Blocka7e24c12009-10-30 11:49:00 +00001956 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07001957 MUST_USE_RESULT inline MaybeObject* Copy();
1958 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001959
1960 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07001961 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00001962
1963 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07001964 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00001965
1966 // Copy a sub array from the receiver to dest.
1967 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1968
1969 // Garbage collection support.
1970 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1971
1972 // Code Generation support.
1973 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1974
1975 // Casting.
1976 static inline FixedArray* cast(Object* obj);
1977
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001978 // Layout description.
1979 // Length is smi tagged when it is stored.
1980 static const int kLengthOffset = HeapObject::kHeaderSize;
1981 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001982
1983 // Maximal allowed size, in bytes, of a single FixedArray.
1984 // Prevents overflowing size computations, as well as extreme memory
1985 // consumption.
1986 static const int kMaxSize = 512 * MB;
1987 // Maximally allowed length of a FixedArray.
1988 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001989
1990 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001991#ifdef OBJECT_PRINT
1992 inline void FixedArrayPrint() {
1993 FixedArrayPrint(stdout);
1994 }
1995 void FixedArrayPrint(FILE* out);
1996#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001997#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001998 void FixedArrayVerify();
1999 // Checks if two FixedArrays have identical contents.
2000 bool IsEqualTo(FixedArray* other);
2001#endif
2002
2003 // Swap two elements in a pair of arrays. If this array and the
2004 // numbers array are the same object, the elements are only swapped
2005 // once.
2006 void SwapPairs(FixedArray* numbers, int i, int j);
2007
2008 // Sort prefix of this array and the numbers array as pairs wrt. the
2009 // numbers. If the numbers array and the this array are the same
2010 // object, the prefix of this array is sorted.
2011 void SortPairs(FixedArray* numbers, uint32_t len);
2012
Iain Merrick75681382010-08-19 15:07:18 +01002013 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2014 public:
2015 static inline int SizeOf(Map* map, HeapObject* object) {
2016 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2017 }
2018 };
2019
Steve Blocka7e24c12009-10-30 11:49:00 +00002020 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00002021 // Set operation on FixedArray without using write barriers. Can
2022 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00002023 static inline void fast_set(FixedArray* array, int index, Object* value);
2024
2025 private:
2026 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2027};
2028
2029
2030// DescriptorArrays are fixed arrays used to hold instance descriptors.
2031// The format of the these objects is:
2032// [0]: point to a fixed array with (value, detail) pairs.
2033// [1]: next enumeration index (Smi), or pointer to small fixed array:
2034// [0]: next enumeration index (Smi)
2035// [1]: pointer to fixed array with enum cache
2036// [2]: first key
2037// [length() - 1]: last key
2038//
2039class DescriptorArray: public FixedArray {
2040 public:
2041 // Is this the singleton empty_descriptor_array?
2042 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00002043
Steve Blocka7e24c12009-10-30 11:49:00 +00002044 // Returns the number of descriptors in the array.
2045 int number_of_descriptors() {
Steve Block44f0eee2011-05-26 01:26:41 +01002046 ASSERT(length() > kFirstIndex || IsEmpty());
2047 int len = length();
2048 return len <= kFirstIndex ? 0 : len - kFirstIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00002049 }
2050
2051 int NextEnumerationIndex() {
2052 if (IsEmpty()) return PropertyDetails::kInitialIndex;
2053 Object* obj = get(kEnumerationIndexIndex);
2054 if (obj->IsSmi()) {
2055 return Smi::cast(obj)->value();
2056 } else {
2057 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2058 return Smi::cast(index)->value();
2059 }
2060 }
2061
2062 // Set next enumeration index and flush any enum cache.
2063 void SetNextEnumerationIndex(int value) {
2064 if (!IsEmpty()) {
2065 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2066 }
2067 }
2068 bool HasEnumCache() {
2069 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
2070 }
2071
2072 Object* GetEnumCache() {
2073 ASSERT(HasEnumCache());
2074 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2075 return bridge->get(kEnumCacheBridgeCacheIndex);
2076 }
2077
2078 // Initialize or change the enum cache,
2079 // using the supplied storage for the small "bridge".
2080 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2081
2082 // Accessors for fetching instance descriptor at descriptor number.
2083 inline String* GetKey(int descriptor_number);
2084 inline Object* GetValue(int descriptor_number);
2085 inline Smi* GetDetails(int descriptor_number);
2086 inline PropertyType GetType(int descriptor_number);
2087 inline int GetFieldIndex(int descriptor_number);
2088 inline JSFunction* GetConstantFunction(int descriptor_number);
2089 inline Object* GetCallbacksObject(int descriptor_number);
2090 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2091 inline bool IsProperty(int descriptor_number);
2092 inline bool IsTransition(int descriptor_number);
2093 inline bool IsNullDescriptor(int descriptor_number);
2094 inline bool IsDontEnum(int descriptor_number);
2095
2096 // Accessor for complete descriptor.
2097 inline void Get(int descriptor_number, Descriptor* desc);
2098 inline void Set(int descriptor_number, Descriptor* desc);
2099
2100 // Transfer complete descriptor from another descriptor array to
2101 // this one.
2102 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2103
2104 // Copy the descriptor array, insert a new descriptor and optionally
2105 // remove map transitions. If the descriptor is already present, it is
2106 // replaced. If a replaced descriptor is a real property (not a transition
2107 // or null), its enumeration index is kept as is.
2108 // If adding a real property, map transitions must be removed. If adding
2109 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002110 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2111 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002112
2113 // Remove all transitions. Return a copy of the array with all transitions
2114 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002115 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002116
2117 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002118 // Does not check for duplicates.
2119 void SortUnchecked();
2120
2121 // Sort the instance descriptors by the hash codes of their keys.
2122 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00002123 void Sort();
2124
2125 // Search the instance descriptors for given name.
2126 inline int Search(String* name);
2127
Iain Merrick75681382010-08-19 15:07:18 +01002128 // As the above, but uses DescriptorLookupCache and updates it when
2129 // necessary.
2130 inline int SearchWithCache(String* name);
2131
Steve Blocka7e24c12009-10-30 11:49:00 +00002132 // Tells whether the name is present int the array.
2133 bool Contains(String* name) { return kNotFound != Search(name); }
2134
2135 // Perform a binary search in the instance descriptors represented
2136 // by this fixed array. low and high are descriptor indices. If there
2137 // are three instance descriptors in this array it should be called
2138 // with low=0 and high=2.
2139 int BinarySearch(String* name, int low, int high);
2140
2141 // Perform a linear search in the instance descriptors represented
2142 // by this fixed array. len is the number of descriptor indices that are
2143 // valid. Does not require the descriptors to be sorted.
2144 int LinearSearch(String* name, int len);
2145
2146 // Allocates a DescriptorArray, but returns the singleton
2147 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002148 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002149
2150 // Casting.
2151 static inline DescriptorArray* cast(Object* obj);
2152
2153 // Constant for denoting key was not found.
2154 static const int kNotFound = -1;
2155
2156 static const int kContentArrayIndex = 0;
2157 static const int kEnumerationIndexIndex = 1;
2158 static const int kFirstIndex = 2;
2159
2160 // The length of the "bridge" to the enum cache.
2161 static const int kEnumCacheBridgeLength = 2;
2162 static const int kEnumCacheBridgeEnumIndex = 0;
2163 static const int kEnumCacheBridgeCacheIndex = 1;
2164
2165 // Layout description.
2166 static const int kContentArrayOffset = FixedArray::kHeaderSize;
2167 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2168 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2169
2170 // Layout description for the bridge array.
2171 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2172 static const int kEnumCacheBridgeCacheOffset =
2173 kEnumCacheBridgeEnumOffset + kPointerSize;
2174
Ben Murdochb0fe1622011-05-05 13:52:32 +01002175#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002176 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002177 inline void PrintDescriptors() {
2178 PrintDescriptors(stdout);
2179 }
2180 void PrintDescriptors(FILE* out);
2181#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002182
Ben Murdochb0fe1622011-05-05 13:52:32 +01002183#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002184 // Is the descriptor array sorted and without duplicates?
2185 bool IsSortedNoDuplicates();
2186
2187 // Are two DescriptorArrays equal?
2188 bool IsEqualTo(DescriptorArray* other);
2189#endif
2190
2191 // The maximum number of descriptors we want in a descriptor array (should
2192 // fit in a page).
2193 static const int kMaxNumberOfDescriptors = 1024 + 512;
2194
2195 private:
2196 // Conversion from descriptor number to array indices.
2197 static int ToKeyIndex(int descriptor_number) {
2198 return descriptor_number+kFirstIndex;
2199 }
Leon Clarkee46be812010-01-19 14:06:41 +00002200
2201 static int ToDetailsIndex(int descriptor_number) {
2202 return (descriptor_number << 1) + 1;
2203 }
2204
Steve Blocka7e24c12009-10-30 11:49:00 +00002205 static int ToValueIndex(int descriptor_number) {
2206 return descriptor_number << 1;
2207 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002208
2209 bool is_null_descriptor(int descriptor_number) {
2210 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2211 NULL_DESCRIPTOR;
2212 }
2213 // Swap operation on FixedArray without using write barriers.
2214 static inline void fast_swap(FixedArray* array, int first, int second);
2215
2216 // Swap descriptor first and second.
2217 inline void Swap(int first, int second);
2218
2219 FixedArray* GetContentArray() {
2220 return FixedArray::cast(get(kContentArrayIndex));
2221 }
2222 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2223};
2224
2225
2226// HashTable is a subclass of FixedArray that implements a hash table
2227// that uses open addressing and quadratic probing.
2228//
2229// In order for the quadratic probing to work, elements that have not
2230// yet been used and elements that have been deleted are
2231// distinguished. Probing continues when deleted elements are
2232// encountered and stops when unused elements are encountered.
2233//
2234// - Elements with key == undefined have not been used yet.
2235// - Elements with key == null have been deleted.
2236//
2237// The hash table class is parameterized with a Shape and a Key.
2238// Shape must be a class with the following interface:
2239// class ExampleShape {
2240// public:
2241// // Tells whether key matches other.
2242// static bool IsMatch(Key key, Object* other);
2243// // Returns the hash value for key.
2244// static uint32_t Hash(Key key);
2245// // Returns the hash value for object.
2246// static uint32_t HashForObject(Key key, Object* object);
2247// // Convert key to an object.
2248// static inline Object* AsObject(Key key);
2249// // The prefix size indicates number of elements in the beginning
2250// // of the backing storage.
2251// static const int kPrefixSize = ..;
2252// // The Element size indicates number of elements per entry.
2253// static const int kEntrySize = ..;
2254// };
Steve Block3ce2e202009-11-05 08:53:23 +00002255// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002256// beginning of the backing storage that can be used for non-element
2257// information by subclasses.
2258
2259template<typename Shape, typename Key>
2260class HashTable: public FixedArray {
2261 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002262 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002263 int NumberOfElements() {
2264 return Smi::cast(get(kNumberOfElementsIndex))->value();
2265 }
2266
Leon Clarkee46be812010-01-19 14:06:41 +00002267 // Returns the number of deleted elements in the hash table.
2268 int NumberOfDeletedElements() {
2269 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2270 }
2271
Steve Block3ce2e202009-11-05 08:53:23 +00002272 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002273 int Capacity() {
2274 return Smi::cast(get(kCapacityIndex))->value();
2275 }
2276
2277 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002278 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002279 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2280
2281 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002282 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002283 void ElementRemoved() {
2284 SetNumberOfElements(NumberOfElements() - 1);
2285 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2286 }
2287 void ElementsRemoved(int n) {
2288 SetNumberOfElements(NumberOfElements() - n);
2289 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2290 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002291
Steve Block3ce2e202009-11-05 08:53:23 +00002292 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002293 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002294 int at_least_space_for,
2295 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002296
2297 // Returns the key at entry.
2298 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2299
2300 // Tells whether k is a real key. Null and undefined are not allowed
2301 // as keys and can be used to indicate missing or deleted elements.
2302 bool IsKey(Object* k) {
2303 return !k->IsNull() && !k->IsUndefined();
2304 }
2305
2306 // Garbage collection support.
2307 void IteratePrefix(ObjectVisitor* visitor);
2308 void IterateElements(ObjectVisitor* visitor);
2309
2310 // Casting.
2311 static inline HashTable* cast(Object* obj);
2312
2313 // Compute the probe offset (quadratic probing).
2314 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2315 return (n + n * n) >> 1;
2316 }
2317
2318 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002319 static const int kNumberOfDeletedElementsIndex = 1;
2320 static const int kCapacityIndex = 2;
2321 static const int kPrefixStartIndex = 3;
2322 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002323 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002324 static const int kEntrySize = Shape::kEntrySize;
2325 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002326 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002327 static const int kCapacityOffset =
2328 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002329
2330 // Constant used for denoting a absent entry.
2331 static const int kNotFound = -1;
2332
Leon Clarkee46be812010-01-19 14:06:41 +00002333 // Maximal capacity of HashTable. Based on maximal length of underlying
2334 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2335 // cannot overflow.
2336 static const int kMaxCapacity =
2337 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2338
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002339 // Find entry for key otherwise return kNotFound.
Steve Block44f0eee2011-05-26 01:26:41 +01002340 inline int FindEntry(Key key);
2341 int FindEntry(Isolate* isolate, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002342
2343 protected:
2344
2345 // Find the entry at which to insert element with the given key that
2346 // has the given hash value.
2347 uint32_t FindInsertionEntry(uint32_t hash);
2348
2349 // Returns the index for an entry (of the key)
2350 static inline int EntryToIndex(int entry) {
2351 return (entry * kEntrySize) + kElementsStartIndex;
2352 }
2353
Steve Block3ce2e202009-11-05 08:53:23 +00002354 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002355 void SetNumberOfElements(int nof) {
2356 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2357 }
2358
Leon Clarkee46be812010-01-19 14:06:41 +00002359 // Update the number of deleted elements in the hash table.
2360 void SetNumberOfDeletedElements(int nod) {
2361 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2362 }
2363
Steve Blocka7e24c12009-10-30 11:49:00 +00002364 // Sets the capacity of the hash table.
2365 void SetCapacity(int capacity) {
2366 // To scale a computed hash code to fit within the hash table, we
2367 // use bit-wise AND with a mask, so the capacity must be positive
2368 // and non-zero.
2369 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002370 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002371 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2372 }
2373
2374
2375 // Returns probe entry.
2376 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2377 ASSERT(IsPowerOf2(size));
2378 return (hash + GetProbeOffset(number)) & (size - 1);
2379 }
2380
Leon Clarkee46be812010-01-19 14:06:41 +00002381 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2382 return hash & (size - 1);
2383 }
2384
2385 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2386 return (last + number) & (size - 1);
2387 }
2388
Steve Blocka7e24c12009-10-30 11:49:00 +00002389 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002390 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002391};
2392
2393
2394
2395// HashTableKey is an abstract superclass for virtual key behavior.
2396class HashTableKey {
2397 public:
2398 // Returns whether the other object matches this key.
2399 virtual bool IsMatch(Object* other) = 0;
2400 // Returns the hash value for this key.
2401 virtual uint32_t Hash() = 0;
2402 // Returns the hash value for object.
2403 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002404 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002405 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002406 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002407 // Required.
2408 virtual ~HashTableKey() {}
2409};
2410
2411class SymbolTableShape {
2412 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002413 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002414 return key->IsMatch(value);
2415 }
Steve Block44f0eee2011-05-26 01:26:41 +01002416 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002417 return key->Hash();
2418 }
Steve Block44f0eee2011-05-26 01:26:41 +01002419 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002420 return key->HashForObject(object);
2421 }
Steve Block44f0eee2011-05-26 01:26:41 +01002422 MUST_USE_RESULT static inline 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 = 1;
2428};
2429
2430// SymbolTable.
2431//
2432// No special elements in the prefix and the element size is 1
2433// because only the symbol itself (the key) needs to be stored.
2434class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2435 public:
2436 // Find symbol in the symbol table. If it is not there yet, it is
2437 // added. The return value is the symbol table which might have
2438 // been enlarged. If the return value is not a failure, the symbol
2439 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002440 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002441 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2442 Object** s);
2443 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2444 Object** s);
John Reck59135872010-11-02 12:39:01 -07002445 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002446
2447 // Looks up a symbol that is equal to the given string and returns
2448 // true if it is found, assigning the symbol to the given output
2449 // parameter.
2450 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002451 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002452
2453 // Casting.
2454 static inline SymbolTable* cast(Object* obj);
2455
2456 private:
John Reck59135872010-11-02 12:39:01 -07002457 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002458
2459 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2460};
2461
2462
2463class MapCacheShape {
2464 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002465 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002466 return key->IsMatch(value);
2467 }
Steve Block44f0eee2011-05-26 01:26:41 +01002468 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002469 return key->Hash();
2470 }
2471
Steve Block44f0eee2011-05-26 01:26:41 +01002472 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002473 return key->HashForObject(object);
2474 }
2475
Steve Block44f0eee2011-05-26 01:26:41 +01002476 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002477 return key->AsObject();
2478 }
2479
2480 static const int kPrefixSize = 0;
2481 static const int kEntrySize = 2;
2482};
2483
2484
2485// MapCache.
2486//
2487// Maps keys that are a fixed array of symbols to a map.
2488// Used for canonicalize maps for object literals.
2489class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2490 public:
2491 // Find cached value for a string key, otherwise return null.
2492 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002493 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002494 static inline MapCache* cast(Object* obj);
2495
2496 private:
2497 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2498};
2499
2500
2501template <typename Shape, typename Key>
2502class Dictionary: public HashTable<Shape, Key> {
2503 public:
2504
2505 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2506 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2507 }
2508
2509 // Returns the value at entry.
2510 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002511 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002512 }
2513
2514 // Set the value for entry.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002515 // Returns false if the put wasn't performed due to property being read only.
2516 // Returns true on successful put.
2517 bool ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002518 // Check that this value can actually be written.
2519 PropertyDetails details = DetailsAt(entry);
2520 // If a value has not been initilized we allow writing to it even if
2521 // it is read only (a declared const that has not been initialized).
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002522 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2523 return false;
2524 }
2525 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2526 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002527 }
2528
2529 // Returns the property details for the property at entry.
2530 PropertyDetails DetailsAt(int entry) {
2531 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2532 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002533 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002534 }
2535
2536 // Set the details for entry.
2537 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002538 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002539 }
2540
2541 // Sorting support
2542 void CopyValuesTo(FixedArray* elements);
2543
2544 // Delete a property from the dictionary.
2545 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2546
2547 // Returns the number of elements in the dictionary filtering out properties
2548 // with the specified attributes.
2549 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2550
2551 // Returns the number of enumerable elements in the dictionary.
2552 int NumberOfEnumElements();
2553
2554 // Copies keys to preallocated fixed array.
2555 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2556 // Fill in details for properties into storage.
2557 void CopyKeysTo(FixedArray* storage);
2558
2559 // Accessors for next enumeration index.
2560 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002561 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002562 }
2563
2564 int NextEnumerationIndex() {
2565 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2566 }
2567
2568 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002569 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002570
2571 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002572 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002573
Ben Murdochb0fe1622011-05-05 13:52:32 +01002574#ifdef OBJECT_PRINT
2575 inline void Print() {
2576 Print(stdout);
2577 }
2578 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002579#endif
2580 // Returns the key (slow).
2581 Object* SlowReverseLookup(Object* value);
2582
2583 // Sets the entry to (key, value) pair.
2584 inline void SetEntry(int entry,
2585 Object* key,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002586 Object* value);
2587 inline void SetEntry(int entry,
2588 Object* key,
Steve Blocka7e24c12009-10-30 11:49:00 +00002589 Object* value,
2590 PropertyDetails details);
2591
John Reck59135872010-11-02 12:39:01 -07002592 MUST_USE_RESULT MaybeObject* Add(Key key,
2593 Object* value,
2594 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002595
2596 protected:
2597 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002598 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002599
2600 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002601 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2602 Object* value,
2603 PropertyDetails details,
2604 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002605
2606 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002607 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002608 static const int kMaxNumberKeyIndex =
2609 HashTable<Shape, Key>::kPrefixStartIndex;
2610 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2611};
2612
2613
2614class StringDictionaryShape {
2615 public:
2616 static inline bool IsMatch(String* key, Object* other);
2617 static inline uint32_t Hash(String* key);
2618 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002619 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002620 static const int kPrefixSize = 2;
2621 static const int kEntrySize = 3;
2622 static const bool kIsEnumerable = true;
2623};
2624
2625
2626class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2627 public:
2628 static inline StringDictionary* cast(Object* obj) {
2629 ASSERT(obj->IsDictionary());
2630 return reinterpret_cast<StringDictionary*>(obj);
2631 }
2632
2633 // Copies enumerable keys to preallocated fixed array.
2634 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2635
2636 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002637 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2638 JSObject* obj,
2639 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002640
2641 // Find entry for key otherwise return kNotFound. Optimzed version of
2642 // HashTable::FindEntry.
2643 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002644};
2645
2646
2647class NumberDictionaryShape {
2648 public:
2649 static inline bool IsMatch(uint32_t key, Object* other);
2650 static inline uint32_t Hash(uint32_t key);
2651 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002652 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002653 static const int kPrefixSize = 2;
2654 static const int kEntrySize = 3;
2655 static const bool kIsEnumerable = false;
2656};
2657
2658
2659class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2660 public:
2661 static NumberDictionary* cast(Object* obj) {
2662 ASSERT(obj->IsDictionary());
2663 return reinterpret_cast<NumberDictionary*>(obj);
2664 }
2665
2666 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002667 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2668 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2669 Object* value,
2670 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002671
2672 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002673 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2674 Object* value,
2675 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002676
2677 void UpdateMaxNumberKey(uint32_t key);
2678
2679 // If slow elements are required we will never go back to fast-case
2680 // for the elements kept in this dictionary. We require slow
2681 // elements if an element has been added at an index larger than
2682 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2683 // when defining a getter or setter with a number key.
2684 inline bool requires_slow_elements();
2685 inline void set_requires_slow_elements();
2686
2687 // Get the value of the max number key that has been added to this
2688 // dictionary. max_number_key can only be called if
2689 // requires_slow_elements returns false.
2690 inline uint32_t max_number_key();
2691
2692 // Remove all entries were key is a number and (from <= key && key < to).
2693 void RemoveNumberEntries(uint32_t from, uint32_t to);
2694
2695 // Bit masks.
2696 static const int kRequiresSlowElementsMask = 1;
2697 static const int kRequiresSlowElementsTagSize = 1;
2698 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2699};
2700
2701
Steve Block6ded16b2010-05-10 14:33:55 +01002702// JSFunctionResultCache caches results of some JSFunction invocation.
2703// It is a fixed array with fixed structure:
2704// [0]: factory function
2705// [1]: finger index
2706// [2]: current cache size
2707// [3]: dummy field.
2708// The rest of array are key/value pairs.
2709class JSFunctionResultCache: public FixedArray {
2710 public:
2711 static const int kFactoryIndex = 0;
2712 static const int kFingerIndex = kFactoryIndex + 1;
2713 static const int kCacheSizeIndex = kFingerIndex + 1;
2714 static const int kDummyIndex = kCacheSizeIndex + 1;
2715 static const int kEntriesIndex = kDummyIndex + 1;
2716
2717 static const int kEntrySize = 2; // key + value
2718
Kristian Monsen25f61362010-05-21 11:50:48 +01002719 static const int kFactoryOffset = kHeaderSize;
2720 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2721 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2722
Steve Block6ded16b2010-05-10 14:33:55 +01002723 inline void MakeZeroSize();
2724 inline void Clear();
2725
Ben Murdochb8e0da22011-05-16 14:20:40 +01002726 inline int size();
2727 inline void set_size(int size);
2728 inline int finger_index();
2729 inline void set_finger_index(int finger_index);
2730
Steve Block6ded16b2010-05-10 14:33:55 +01002731 // Casting
2732 static inline JSFunctionResultCache* cast(Object* obj);
2733
2734#ifdef DEBUG
2735 void JSFunctionResultCacheVerify();
2736#endif
2737};
2738
2739
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002740// The cache for maps used by normalized (dictionary mode) objects.
2741// Such maps do not have property descriptors, so a typical program
2742// needs very limited number of distinct normalized maps.
2743class NormalizedMapCache: public FixedArray {
2744 public:
2745 static const int kEntries = 64;
2746
John Reck59135872010-11-02 12:39:01 -07002747 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2748 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002749
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002750 void Clear();
2751
2752 // Casting
2753 static inline NormalizedMapCache* cast(Object* obj);
2754
2755#ifdef DEBUG
2756 void NormalizedMapCacheVerify();
2757#endif
2758
2759 private:
2760 static int Hash(Map* fast);
2761
2762 static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2763};
2764
2765
Steve Blocka7e24c12009-10-30 11:49:00 +00002766// ByteArray represents fixed sized byte arrays. Used by the outside world,
2767// such as PCRE, and also by the memory allocator and garbage collector to
2768// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002769class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002770 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002771 // [length]: length of the array.
2772 inline int length();
2773 inline void set_length(int value);
2774
Steve Blocka7e24c12009-10-30 11:49:00 +00002775 // Setter and getter.
2776 inline byte get(int index);
2777 inline void set(int index, byte value);
2778
2779 // Treat contents as an int array.
2780 inline int get_int(int index);
2781
2782 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002783 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002784 }
2785 // We use byte arrays for free blocks in the heap. Given a desired size in
2786 // bytes that is a multiple of the word size and big enough to hold a byte
2787 // array, this function returns the number of elements a byte array should
2788 // have.
2789 static int LengthFor(int size_in_bytes) {
2790 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2791 ASSERT(size_in_bytes >= kHeaderSize);
2792 return size_in_bytes - kHeaderSize;
2793 }
2794
2795 // Returns data start address.
2796 inline Address GetDataStartAddress();
2797
2798 // Returns a pointer to the ByteArray object for a given data start address.
2799 static inline ByteArray* FromDataStartAddress(Address address);
2800
2801 // Casting.
2802 static inline ByteArray* cast(Object* obj);
2803
2804 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002805 inline int ByteArraySize() {
2806 return SizeFor(this->length());
2807 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002808#ifdef OBJECT_PRINT
2809 inline void ByteArrayPrint() {
2810 ByteArrayPrint(stdout);
2811 }
2812 void ByteArrayPrint(FILE* out);
2813#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002814#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002815 void ByteArrayVerify();
2816#endif
2817
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002818 // Layout description.
2819 // Length is smi tagged when it is stored.
2820 static const int kLengthOffset = HeapObject::kHeaderSize;
2821 static const int kHeaderSize = kLengthOffset + kPointerSize;
2822
2823 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002824
Leon Clarkee46be812010-01-19 14:06:41 +00002825 // Maximal memory consumption for a single ByteArray.
2826 static const int kMaxSize = 512 * MB;
2827 // Maximal length of a single ByteArray.
2828 static const int kMaxLength = kMaxSize - kHeaderSize;
2829
Steve Blocka7e24c12009-10-30 11:49:00 +00002830 private:
2831 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2832};
2833
2834
Steve Block3ce2e202009-11-05 08:53:23 +00002835// An ExternalArray represents a fixed-size array of primitive values
2836// which live outside the JavaScript heap. Its subclasses are used to
2837// implement the CanvasArray types being defined in the WebGL
2838// specification. As of this writing the first public draft is not yet
2839// available, but Khronos members can access the draft at:
2840// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2841//
2842// The semantics of these arrays differ from CanvasPixelArray.
2843// Out-of-range values passed to the setter are converted via a C
2844// cast, not clamping. Out-of-range indices cause exceptions to be
2845// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002846class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002847 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002848 // [length]: length of the array.
2849 inline int length();
2850 inline void set_length(int value);
2851
Steve Block3ce2e202009-11-05 08:53:23 +00002852 // [external_pointer]: The pointer to the external memory area backing this
2853 // external array.
2854 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2855
2856 // Casting.
2857 static inline ExternalArray* cast(Object* obj);
2858
2859 // Maximal acceptable length for an external array.
2860 static const int kMaxLength = 0x3fffffff;
2861
2862 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002863 static const int kLengthOffset = HeapObject::kHeaderSize;
2864 static const int kExternalPointerOffset =
2865 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002866 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002867 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002868
2869 private:
2870 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2871};
2872
2873
Steve Block44f0eee2011-05-26 01:26:41 +01002874// A ExternalPixelArray represents a fixed-size byte array with special
2875// semantics used for implementing the CanvasPixelArray object. Please see the
2876// specification at:
2877
2878// http://www.whatwg.org/specs/web-apps/current-work/
2879// multipage/the-canvas-element.html#canvaspixelarray
2880// In particular, write access clamps the value written to 0 or 255 if the
2881// value written is outside this range.
2882class ExternalPixelArray: public ExternalArray {
2883 public:
2884 inline uint8_t* external_pixel_pointer();
2885
2886 // Setter and getter.
2887 inline uint8_t get(int index);
2888 inline void set(int index, uint8_t value);
2889
2890 // This accessor applies the correct conversion from Smi, HeapNumber and
2891 // undefined and clamps the converted value between 0 and 255.
2892 Object* SetValue(uint32_t index, Object* value);
2893
2894 // Casting.
2895 static inline ExternalPixelArray* cast(Object* obj);
2896
2897#ifdef OBJECT_PRINT
2898 inline void ExternalPixelArrayPrint() {
2899 ExternalPixelArrayPrint(stdout);
2900 }
2901 void ExternalPixelArrayPrint(FILE* out);
2902#endif
2903#ifdef DEBUG
2904 void ExternalPixelArrayVerify();
2905#endif // DEBUG
2906
2907 private:
2908 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
2909};
2910
2911
Steve Block3ce2e202009-11-05 08:53:23 +00002912class ExternalByteArray: public ExternalArray {
2913 public:
2914 // Setter and getter.
2915 inline int8_t get(int index);
2916 inline void set(int index, int8_t value);
2917
2918 // This accessor applies the correct conversion from Smi, HeapNumber
2919 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002920 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002921
2922 // Casting.
2923 static inline ExternalByteArray* cast(Object* obj);
2924
Ben Murdochb0fe1622011-05-05 13:52:32 +01002925#ifdef OBJECT_PRINT
2926 inline void ExternalByteArrayPrint() {
2927 ExternalByteArrayPrint(stdout);
2928 }
2929 void ExternalByteArrayPrint(FILE* out);
2930#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002931#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002932 void ExternalByteArrayVerify();
2933#endif // DEBUG
2934
2935 private:
2936 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2937};
2938
2939
2940class ExternalUnsignedByteArray: public ExternalArray {
2941 public:
2942 // Setter and getter.
2943 inline uint8_t get(int index);
2944 inline void set(int index, uint8_t value);
2945
2946 // This accessor applies the correct conversion from Smi, HeapNumber
2947 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002948 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002949
2950 // Casting.
2951 static inline ExternalUnsignedByteArray* cast(Object* obj);
2952
Ben Murdochb0fe1622011-05-05 13:52:32 +01002953#ifdef OBJECT_PRINT
2954 inline void ExternalUnsignedByteArrayPrint() {
2955 ExternalUnsignedByteArrayPrint(stdout);
2956 }
2957 void ExternalUnsignedByteArrayPrint(FILE* out);
2958#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002959#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002960 void ExternalUnsignedByteArrayVerify();
2961#endif // DEBUG
2962
2963 private:
2964 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2965};
2966
2967
2968class ExternalShortArray: public ExternalArray {
2969 public:
2970 // Setter and getter.
2971 inline int16_t get(int index);
2972 inline void set(int index, int16_t value);
2973
2974 // This accessor applies the correct conversion from Smi, HeapNumber
2975 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002976 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002977
2978 // Casting.
2979 static inline ExternalShortArray* cast(Object* obj);
2980
Ben Murdochb0fe1622011-05-05 13:52:32 +01002981#ifdef OBJECT_PRINT
2982 inline void ExternalShortArrayPrint() {
2983 ExternalShortArrayPrint(stdout);
2984 }
2985 void ExternalShortArrayPrint(FILE* out);
2986#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002987#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002988 void ExternalShortArrayVerify();
2989#endif // DEBUG
2990
2991 private:
2992 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2993};
2994
2995
2996class ExternalUnsignedShortArray: public ExternalArray {
2997 public:
2998 // Setter and getter.
2999 inline uint16_t get(int index);
3000 inline void set(int index, uint16_t value);
3001
3002 // This accessor applies the correct conversion from Smi, HeapNumber
3003 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003004 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003005
3006 // Casting.
3007 static inline ExternalUnsignedShortArray* cast(Object* obj);
3008
Ben Murdochb0fe1622011-05-05 13:52:32 +01003009#ifdef OBJECT_PRINT
3010 inline void ExternalUnsignedShortArrayPrint() {
3011 ExternalUnsignedShortArrayPrint(stdout);
3012 }
3013 void ExternalUnsignedShortArrayPrint(FILE* out);
3014#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003015#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003016 void ExternalUnsignedShortArrayVerify();
3017#endif // DEBUG
3018
3019 private:
3020 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3021};
3022
3023
3024class ExternalIntArray: public ExternalArray {
3025 public:
3026 // Setter and getter.
3027 inline int32_t get(int index);
3028 inline void set(int index, int32_t value);
3029
3030 // This accessor applies the correct conversion from Smi, HeapNumber
3031 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003032 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003033
3034 // Casting.
3035 static inline ExternalIntArray* cast(Object* obj);
3036
Ben Murdochb0fe1622011-05-05 13:52:32 +01003037#ifdef OBJECT_PRINT
3038 inline void ExternalIntArrayPrint() {
3039 ExternalIntArrayPrint(stdout);
3040 }
3041 void ExternalIntArrayPrint(FILE* out);
3042#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003043#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003044 void ExternalIntArrayVerify();
3045#endif // DEBUG
3046
3047 private:
3048 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3049};
3050
3051
3052class ExternalUnsignedIntArray: public ExternalArray {
3053 public:
3054 // Setter and getter.
3055 inline uint32_t get(int index);
3056 inline void set(int index, uint32_t value);
3057
3058 // This accessor applies the correct conversion from Smi, HeapNumber
3059 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003060 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003061
3062 // Casting.
3063 static inline ExternalUnsignedIntArray* cast(Object* obj);
3064
Ben Murdochb0fe1622011-05-05 13:52:32 +01003065#ifdef OBJECT_PRINT
3066 inline void ExternalUnsignedIntArrayPrint() {
3067 ExternalUnsignedIntArrayPrint(stdout);
3068 }
3069 void ExternalUnsignedIntArrayPrint(FILE* out);
3070#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003071#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003072 void ExternalUnsignedIntArrayVerify();
3073#endif // DEBUG
3074
3075 private:
3076 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3077};
3078
3079
3080class ExternalFloatArray: public ExternalArray {
3081 public:
3082 // Setter and getter.
3083 inline float get(int index);
3084 inline void set(int index, float value);
3085
3086 // This accessor applies the correct conversion from Smi, HeapNumber
3087 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003088 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003089
3090 // Casting.
3091 static inline ExternalFloatArray* cast(Object* obj);
3092
Ben Murdochb0fe1622011-05-05 13:52:32 +01003093#ifdef OBJECT_PRINT
3094 inline void ExternalFloatArrayPrint() {
3095 ExternalFloatArrayPrint(stdout);
3096 }
3097 void ExternalFloatArrayPrint(FILE* out);
3098#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003099#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003100 void ExternalFloatArrayVerify();
3101#endif // DEBUG
3102
3103 private:
3104 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3105};
3106
3107
Ben Murdochb0fe1622011-05-05 13:52:32 +01003108// DeoptimizationInputData is a fixed array used to hold the deoptimization
3109// data for code generated by the Hydrogen/Lithium compiler. It also
3110// contains information about functions that were inlined. If N different
3111// functions were inlined then first N elements of the literal array will
3112// contain these functions.
3113//
3114// It can be empty.
3115class DeoptimizationInputData: public FixedArray {
3116 public:
3117 // Layout description. Indices in the array.
3118 static const int kTranslationByteArrayIndex = 0;
3119 static const int kInlinedFunctionCountIndex = 1;
3120 static const int kLiteralArrayIndex = 2;
3121 static const int kOsrAstIdIndex = 3;
3122 static const int kOsrPcOffsetIndex = 4;
3123 static const int kFirstDeoptEntryIndex = 5;
3124
3125 // Offsets of deopt entry elements relative to the start of the entry.
3126 static const int kAstIdOffset = 0;
3127 static const int kTranslationIndexOffset = 1;
3128 static const int kArgumentsStackHeightOffset = 2;
3129 static const int kDeoptEntrySize = 3;
3130
3131 // Simple element accessors.
3132#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3133 type* name() { \
3134 return type::cast(get(k##name##Index)); \
3135 } \
3136 void Set##name(type* value) { \
3137 set(k##name##Index, value); \
3138 }
3139
3140 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3141 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3142 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3143 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3144 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3145
3146 // Unchecked accessor to be used during GC.
3147 FixedArray* UncheckedLiteralArray() {
3148 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3149 }
3150
3151#undef DEFINE_ELEMENT_ACCESSORS
3152
3153 // Accessors for elements of the ith deoptimization entry.
3154#define DEFINE_ENTRY_ACCESSORS(name, type) \
3155 type* name(int i) { \
3156 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3157 } \
3158 void Set##name(int i, type* value) { \
3159 set(IndexForEntry(i) + k##name##Offset, value); \
3160 }
3161
3162 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3163 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3164 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3165
3166#undef DEFINE_ENTRY_ACCESSORS
3167
3168 int DeoptCount() {
3169 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3170 }
3171
3172 // Allocates a DeoptimizationInputData.
3173 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3174 PretenureFlag pretenure);
3175
3176 // Casting.
3177 static inline DeoptimizationInputData* cast(Object* obj);
3178
3179#ifdef OBJECT_PRINT
3180 void DeoptimizationInputDataPrint(FILE* out);
3181#endif
3182
3183 private:
3184 static int IndexForEntry(int i) {
3185 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3186 }
3187
3188 static int LengthFor(int entry_count) {
3189 return IndexForEntry(entry_count);
3190 }
3191};
3192
3193
3194// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3195// data for code generated by the full compiler.
3196// The format of the these objects is
3197// [i * 2]: Ast ID for ith deoptimization.
3198// [i * 2 + 1]: PC and state of ith deoptimization
3199class DeoptimizationOutputData: public FixedArray {
3200 public:
3201 int DeoptPoints() { return length() / 2; }
3202 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3203 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3204 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3205 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3206
3207 static int LengthOfFixedArray(int deopt_points) {
3208 return deopt_points * 2;
3209 }
3210
3211 // Allocates a DeoptimizationOutputData.
3212 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3213 PretenureFlag pretenure);
3214
3215 // Casting.
3216 static inline DeoptimizationOutputData* cast(Object* obj);
3217
3218#ifdef OBJECT_PRINT
3219 void DeoptimizationOutputDataPrint(FILE* out);
3220#endif
3221};
3222
3223
Ben Murdochb8e0da22011-05-16 14:20:40 +01003224class SafepointEntry;
3225
3226
Steve Blocka7e24c12009-10-30 11:49:00 +00003227// Code describes objects with on-the-fly generated machine code.
3228class Code: public HeapObject {
3229 public:
3230 // Opaque data type for encapsulating code flags like kind, inline
3231 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003232 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3233 // enumeration type has correct value range (see Issue 830 for more details).
3234 enum Flags {
3235 FLAGS_MIN_VALUE = kMinInt,
3236 FLAGS_MAX_VALUE = kMaxInt
3237 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003238
3239 enum Kind {
3240 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003241 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003242 STUB,
3243 BUILTIN,
3244 LOAD_IC,
3245 KEYED_LOAD_IC,
Steve Block44f0eee2011-05-26 01:26:41 +01003246 KEYED_EXTERNAL_ARRAY_LOAD_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003247 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003248 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003249 STORE_IC,
3250 KEYED_STORE_IC,
Steve Block44f0eee2011-05-26 01:26:41 +01003251 KEYED_EXTERNAL_ARRAY_STORE_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003252 TYPE_RECORDING_BINARY_OP_IC,
3253 COMPARE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003254 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003255 // Flags.
3256
3257 // Pseudo-kinds.
3258 REGEXP = BUILTIN,
3259 FIRST_IC_KIND = LOAD_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003260 LAST_IC_KIND = COMPARE_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003261 };
3262
3263 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003264 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003265 };
3266
Ben Murdochb8e0da22011-05-16 14:20:40 +01003267 typedef int ExtraICState;
3268
3269 static const ExtraICState kNoExtraICState = 0;
3270
Steve Blocka7e24c12009-10-30 11:49:00 +00003271#ifdef ENABLE_DISASSEMBLER
3272 // Printing
3273 static const char* Kind2String(Kind kind);
3274 static const char* ICState2String(InlineCacheState state);
3275 static const char* PropertyType2String(PropertyType type);
Steve Block1e0659c2011-05-24 12:43:12 +01003276 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003277 inline void Disassemble(const char* name) {
3278 Disassemble(name, stdout);
3279 }
3280 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003281#endif // ENABLE_DISASSEMBLER
3282
3283 // [instruction_size]: Size of the native instructions
3284 inline int instruction_size();
3285 inline void set_instruction_size(int value);
3286
Leon Clarkeac952652010-07-15 11:15:24 +01003287 // [relocation_info]: Code relocation information
3288 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003289 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003290
Ben Murdochb0fe1622011-05-05 13:52:32 +01003291 // [deoptimization_data]: Array containing data for deopt.
3292 DECL_ACCESSORS(deoptimization_data, FixedArray)
3293
3294 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003295 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003296 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003297
Steve Blocka7e24c12009-10-30 11:49:00 +00003298 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003299
Steve Blocka7e24c12009-10-30 11:49:00 +00003300 // [flags]: Various code flags.
3301 inline Flags flags();
3302 inline void set_flags(Flags flags);
3303
3304 // [flags]: Access to specific code flags.
3305 inline Kind kind();
3306 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003307 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00003308 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
3309 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3310 inline int arguments_count(); // Only valid for call IC stubs.
3311
3312 // Testers for IC stub kinds.
3313 inline bool is_inline_cache_stub();
3314 inline bool is_load_stub() { return kind() == LOAD_IC; }
3315 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3316 inline bool is_store_stub() { return kind() == STORE_IC; }
3317 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3318 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003319 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003320 inline bool is_type_recording_binary_op_stub() {
3321 return kind() == TYPE_RECORDING_BINARY_OP_IC;
3322 }
3323 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Steve Block44f0eee2011-05-26 01:26:41 +01003324 inline bool is_external_array_load_stub() {
3325 return kind() == KEYED_EXTERNAL_ARRAY_LOAD_IC;
3326 }
3327 inline bool is_external_array_store_stub() {
3328 return kind() == KEYED_EXTERNAL_ARRAY_STORE_IC;
3329 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003330
Steve Block6ded16b2010-05-10 14:33:55 +01003331 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003332 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003333 inline void set_major_key(int value);
3334
3335 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3336 inline bool optimizable();
3337 inline void set_optimizable(bool value);
3338
3339 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3340 // deoptimization support.
3341 inline bool has_deoptimization_support();
3342 inline void set_has_deoptimization_support(bool value);
3343
3344 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3345 // how long the function has been marked for OSR and therefore which
3346 // level of loop nesting we are willing to do on-stack replacement
3347 // for.
3348 inline void set_allow_osr_at_loop_nesting_level(int level);
3349 inline int allow_osr_at_loop_nesting_level();
3350
3351 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3352 // reserved in the code prologue.
3353 inline unsigned stack_slots();
3354 inline void set_stack_slots(unsigned slots);
3355
3356 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3357 // the instruction stream where the safepoint table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003358 inline unsigned safepoint_table_offset();
3359 inline void set_safepoint_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003360
3361 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3362 // instruction stream where the stack check table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003363 inline unsigned stack_check_table_offset();
3364 inline void set_stack_check_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003365
3366 // [check type]: For kind CALL_IC, tells how to check if the
3367 // receiver is valid for the given call.
3368 inline CheckType check_type();
3369 inline void set_check_type(CheckType value);
3370
Steve Block44f0eee2011-05-26 01:26:41 +01003371 // [external array type]: For kind KEYED_EXTERNAL_ARRAY_LOAD_IC and
3372 // KEYED_EXTERNAL_ARRAY_STORE_IC, identifies the type of external
3373 // array that the code stub is specialized for.
3374 inline ExternalArrayType external_array_type();
3375 inline void set_external_array_type(ExternalArrayType value);
3376
Ben Murdochb0fe1622011-05-05 13:52:32 +01003377 // [type-recording binary op type]: For all TYPE_RECORDING_BINARY_OP_IC.
3378 inline byte type_recording_binary_op_type();
3379 inline void set_type_recording_binary_op_type(byte value);
3380 inline byte type_recording_binary_op_result_type();
3381 inline void set_type_recording_binary_op_result_type(byte value);
3382
3383 // [compare state]: For kind compare IC stubs, tells what state the
3384 // stub is in.
3385 inline byte compare_state();
3386 inline void set_compare_state(byte value);
3387
Ben Murdochb8e0da22011-05-16 14:20:40 +01003388 // Get the safepoint entry for the given pc.
3389 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003390
3391 // Mark this code object as not having a stack check table. Assumes kind
3392 // is FUNCTION.
3393 void SetNoStackCheckTable();
3394
3395 // Find the first map in an IC stub.
3396 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003397
3398 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003399 static inline Flags ComputeFlags(
3400 Kind kind,
3401 InLoopFlag in_loop = NOT_IN_LOOP,
3402 InlineCacheState ic_state = UNINITIALIZED,
3403 ExtraICState extra_ic_state = kNoExtraICState,
3404 PropertyType type = NORMAL,
3405 int argc = -1,
3406 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003407
3408 static inline Flags ComputeMonomorphicFlags(
3409 Kind kind,
3410 PropertyType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003411 ExtraICState extra_ic_state = kNoExtraICState,
Steve Block8defd9f2010-07-08 12:39:36 +01003412 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003413 InLoopFlag in_loop = NOT_IN_LOOP,
3414 int argc = -1);
3415
3416 static inline Kind ExtractKindFromFlags(Flags flags);
3417 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003418 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003419 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
3420 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3421 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003422 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003423 static inline Flags RemoveTypeFromFlags(Flags flags);
3424
3425 // Convert a target address into a code object.
3426 static inline Code* GetCodeFromTargetAddress(Address address);
3427
Steve Block791712a2010-08-27 10:21:07 +01003428 // Convert an entry address into an object.
3429 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3430
Steve Blocka7e24c12009-10-30 11:49:00 +00003431 // Returns the address of the first instruction.
3432 inline byte* instruction_start();
3433
Leon Clarkeac952652010-07-15 11:15:24 +01003434 // Returns the address right after the last instruction.
3435 inline byte* instruction_end();
3436
Steve Blocka7e24c12009-10-30 11:49:00 +00003437 // Returns the size of the instructions, padding, and relocation information.
3438 inline int body_size();
3439
3440 // Returns the address of the first relocation info (read backwards!).
3441 inline byte* relocation_start();
3442
3443 // Code entry point.
3444 inline byte* entry();
3445
3446 // Returns true if pc is inside this object's instructions.
3447 inline bool contains(byte* pc);
3448
Steve Blocka7e24c12009-10-30 11:49:00 +00003449 // Relocate the code by delta bytes. Called to signal that this code
3450 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003451 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003452
3453 // Migrate code described by desc.
3454 void CopyFrom(const CodeDesc& desc);
3455
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003456 // Returns the object size for a given body (used for allocation).
3457 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003458 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003459 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003460 }
3461
3462 // Calculate the size of the code object to report for log events. This takes
3463 // the layout of the code object into account.
3464 int ExecutableSize() {
3465 // Check that the assumptions about the layout of the code object holds.
3466 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3467 Code::kHeaderSize);
3468 return instruction_size() + Code::kHeaderSize;
3469 }
3470
3471 // Locating source position.
3472 int SourcePosition(Address pc);
3473 int SourceStatementPosition(Address pc);
3474
3475 // Casting.
3476 static inline Code* cast(Object* obj);
3477
3478 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003479 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003480 inline void CodeIterateBody(ObjectVisitor* v);
3481
3482 template<typename StaticVisitor>
Steve Block44f0eee2011-05-26 01:26:41 +01003483 inline void CodeIterateBody(Heap* heap);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003484#ifdef OBJECT_PRINT
3485 inline void CodePrint() {
3486 CodePrint(stdout);
3487 }
3488 void CodePrint(FILE* out);
3489#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003490#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003491 void CodeVerify();
3492#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003493
Ben Murdoch8b112d22011-06-08 16:22:53 +01003494 // Returns the isolate/heap this code object belongs to.
3495 inline Isolate* isolate();
3496 inline Heap* heap();
3497
Ben Murdochb0fe1622011-05-05 13:52:32 +01003498 // Max loop nesting marker used to postpose OSR. We don't take loop
3499 // nesting that is deeper than 5 levels into account.
3500 static const int kMaxLoopNestingMarker = 6;
3501
Steve Blocka7e24c12009-10-30 11:49:00 +00003502 // Layout description.
3503 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003504 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003505 static const int kDeoptimizationDataOffset =
3506 kRelocationInfoOffset + kPointerSize;
3507 static const int kFlagsOffset = kDeoptimizationDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003508 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003509
3510 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3511
3512 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3513 kKindSpecificFlagsSize;
3514
Steve Blocka7e24c12009-10-30 11:49:00 +00003515 // Add padding to align the instruction start following right after
3516 // the Code object header.
3517 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003518 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003519
3520 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003521 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3522 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3523 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3524 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
Steve Block44f0eee2011-05-26 01:26:41 +01003525 static const int kExternalArrayTypeOffset = kKindSpecificFlagsOffset;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003526
3527 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3528 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
3529 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3530
3531 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3532 static const int kAllowOSRAtLoopNestingLevelOffset =
3533 kHasDeoptimizationSupportOffset + 1;
3534
Steve Block1e0659c2011-05-24 12:43:12 +01003535 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3536 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003537
3538 // Flags layout.
3539 static const int kFlagsICStateShift = 0;
3540 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003541 static const int kFlagsTypeShift = 4;
Steve Block44f0eee2011-05-26 01:26:41 +01003542 static const int kFlagsKindShift = 8;
3543 static const int kFlagsICHolderShift = 12;
3544 static const int kFlagsExtraICStateShift = 13;
3545 static const int kFlagsArgumentsCountShift = 15;
Steve Blocka7e24c12009-10-30 11:49:00 +00003546
Steve Block6ded16b2010-05-10 14:33:55 +01003547 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3548 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Steve Block44f0eee2011-05-26 01:26:41 +01003549 static const int kFlagsTypeMask = 0x000000F0; // 00001110000
3550 static const int kFlagsKindMask = 0x00000F00; // 11110000000
3551 static const int kFlagsCacheInPrototypeMapMask = 0x00001000;
3552 static const int kFlagsExtraICStateMask = 0x00006000;
3553 static const int kFlagsArgumentsCountMask = 0xFFFF8000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003554
3555 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003556 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003557
3558 private:
3559 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3560};
3561
3562
3563// All heap objects have a Map that describes their structure.
3564// A Map contains information about:
3565// - Size information about the object
3566// - How to iterate over an object (for garbage collection)
3567class Map: public HeapObject {
3568 public:
3569 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003570 // Size in bytes or kVariableSizeSentinel if instances do not have
3571 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003572 inline int instance_size();
3573 inline void set_instance_size(int value);
3574
3575 // Count of properties allocated in the object.
3576 inline int inobject_properties();
3577 inline void set_inobject_properties(int value);
3578
3579 // Count of property fields pre-allocated in the object when first allocated.
3580 inline int pre_allocated_property_fields();
3581 inline void set_pre_allocated_property_fields(int value);
3582
3583 // Instance type.
3584 inline InstanceType instance_type();
3585 inline void set_instance_type(InstanceType value);
3586
3587 // Tells how many unused property fields are available in the
3588 // instance (only used for JSObject in fast mode).
3589 inline int unused_property_fields();
3590 inline void set_unused_property_fields(int value);
3591
3592 // Bit field.
3593 inline byte bit_field();
3594 inline void set_bit_field(byte value);
3595
3596 // Bit field 2.
3597 inline byte bit_field2();
3598 inline void set_bit_field2(byte value);
3599
3600 // Tells whether the object in the prototype property will be used
3601 // for instances created from this function. If the prototype
3602 // property is set to a value that is not a JSObject, the prototype
3603 // property will not be used to create instances of the function.
3604 // See ECMA-262, 13.2.2.
3605 inline void set_non_instance_prototype(bool value);
3606 inline bool has_non_instance_prototype();
3607
Steve Block6ded16b2010-05-10 14:33:55 +01003608 // Tells whether function has special prototype property. If not, prototype
3609 // property will not be created when accessed (will return undefined),
3610 // and construction from this function will not be allowed.
3611 inline void set_function_with_prototype(bool value);
3612 inline bool function_with_prototype();
3613
Steve Blocka7e24c12009-10-30 11:49:00 +00003614 // Tells whether the instance with this map should be ignored by the
3615 // __proto__ accessor.
3616 inline void set_is_hidden_prototype() {
3617 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3618 }
3619
3620 inline bool is_hidden_prototype() {
3621 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3622 }
3623
3624 // Records and queries whether the instance has a named interceptor.
3625 inline void set_has_named_interceptor() {
3626 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3627 }
3628
3629 inline bool has_named_interceptor() {
3630 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3631 }
3632
3633 // Records and queries whether the instance has an indexed interceptor.
3634 inline void set_has_indexed_interceptor() {
3635 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3636 }
3637
3638 inline bool has_indexed_interceptor() {
3639 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3640 }
3641
3642 // Tells whether the instance is undetectable.
3643 // An undetectable object is a special class of JSObject: 'typeof' operator
3644 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3645 // a normal JS object. It is useful for implementing undetectable
3646 // document.all in Firefox & Safari.
3647 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3648 inline void set_is_undetectable() {
3649 set_bit_field(bit_field() | (1 << kIsUndetectable));
3650 }
3651
3652 inline bool is_undetectable() {
3653 return ((1 << kIsUndetectable) & bit_field()) != 0;
3654 }
3655
Steve Blocka7e24c12009-10-30 11:49:00 +00003656 // Tells whether the instance has a call-as-function handler.
3657 inline void set_has_instance_call_handler() {
3658 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3659 }
3660
3661 inline bool has_instance_call_handler() {
3662 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3663 }
3664
Steve Block8defd9f2010-07-08 12:39:36 +01003665 inline void set_is_extensible(bool value);
3666 inline bool is_extensible();
3667
3668 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003669 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3670 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003671 if (value) {
3672 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3673 } else {
3674 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3675 }
Leon Clarkee46be812010-01-19 14:06:41 +00003676 }
3677
Iain Merrick75681382010-08-19 15:07:18 +01003678 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003679 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003680 }
3681
Steve Block1e0659c2011-05-24 12:43:12 +01003682 // Tells whether an instance has pixel array elements.
Steve Block44f0eee2011-05-26 01:26:41 +01003683 inline void set_has_external_array_elements(bool value) {
Steve Block1e0659c2011-05-24 12:43:12 +01003684 if (value) {
Steve Block44f0eee2011-05-26 01:26:41 +01003685 set_bit_field2(bit_field2() | (1 << kHasExternalArrayElements));
Steve Block1e0659c2011-05-24 12:43:12 +01003686 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003687 set_bit_field2(bit_field2() & ~(1 << kHasExternalArrayElements));
Steve Block1e0659c2011-05-24 12:43:12 +01003688 }
3689 }
3690
Steve Block44f0eee2011-05-26 01:26:41 +01003691 inline bool has_external_array_elements() {
3692 return ((1 << kHasExternalArrayElements) & bit_field2()) != 0;
Steve Block1e0659c2011-05-24 12:43:12 +01003693 }
3694
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003695 // Tells whether the map is attached to SharedFunctionInfo
3696 // (for inobject slack tracking).
3697 inline void set_attached_to_shared_function_info(bool value);
3698
3699 inline bool attached_to_shared_function_info();
3700
3701 // Tells whether the map is shared between objects that may have different
3702 // behavior. If true, the map should never be modified, instead a clone
3703 // should be created and modified.
3704 inline void set_is_shared(bool value);
3705
3706 inline bool is_shared();
3707
Steve Blocka7e24c12009-10-30 11:49:00 +00003708 // Tells whether the instance needs security checks when accessing its
3709 // properties.
3710 inline void set_is_access_check_needed(bool access_check_needed);
3711 inline bool is_access_check_needed();
3712
3713 // [prototype]: implicit prototype object.
3714 DECL_ACCESSORS(prototype, Object)
3715
3716 // [constructor]: points back to the function responsible for this map.
3717 DECL_ACCESSORS(constructor, Object)
3718
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003719 inline JSFunction* unchecked_constructor();
3720
Steve Blocka7e24c12009-10-30 11:49:00 +00003721 // [instance descriptors]: describes the object.
3722 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3723
3724 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003725 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003726
Steve Block053d10c2011-06-13 19:13:29 +01003727 // [prototype transitions]: cache of prototype transitions.
3728 // Prototype transition is a transition that happens
3729 // when we change object's prototype to a new one.
3730 // Cache format:
3731 // 0: finger - index of the first free cell in the cache
3732 // 1 + 2 * i: prototype
3733 // 2 + 2 * i: target map
3734 DECL_ACCESSORS(prototype_transitions, FixedArray)
3735 inline FixedArray* unchecked_prototype_transitions();
3736
Ben Murdochb0fe1622011-05-05 13:52:32 +01003737 // Lookup in the map's instance descriptors and fill out the result
3738 // with the given holder if the name is found. The holder may be
3739 // NULL when this function is used from the compiler.
3740 void LookupInDescriptors(JSObject* holder,
3741 String* name,
3742 LookupResult* result);
3743
John Reck59135872010-11-02 12:39:01 -07003744 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003745
John Reck59135872010-11-02 12:39:01 -07003746 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3747 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00003748
3749 // Returns a copy of the map, with all transitions dropped from the
3750 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07003751 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00003752
Steve Block8defd9f2010-07-08 12:39:36 +01003753 // Returns this map if it has the fast elements bit set, otherwise
3754 // returns a copy of the map, with all transitions dropped from the
3755 // descriptors and the fast elements bit set.
John Reck59135872010-11-02 12:39:01 -07003756 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003757
3758 // Returns this map if it has the fast elements bit cleared,
3759 // otherwise returns a copy of the map, with all transitions dropped
3760 // from the descriptors and the fast elements bit cleared.
John Reck59135872010-11-02 12:39:01 -07003761 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003762
Steve Block44f0eee2011-05-26 01:26:41 +01003763 // Returns a new map with all transitions dropped from the descriptors and the
3764 // external array elements bit set.
3765 MUST_USE_RESULT MaybeObject* GetExternalArrayElementsMap(
3766 ExternalArrayType array_type,
3767 bool safe_to_add_transition);
Steve Block1e0659c2011-05-24 12:43:12 +01003768
Steve Blocka7e24c12009-10-30 11:49:00 +00003769 // Returns the property index for name (only valid for FAST MODE).
3770 int PropertyIndexFor(String* name);
3771
3772 // Returns the next free property index (only valid for FAST MODE).
3773 int NextFreePropertyIndex();
3774
3775 // Returns the number of properties described in instance_descriptors.
3776 int NumberOfDescribedProperties();
3777
3778 // Casting.
3779 static inline Map* cast(Object* obj);
3780
3781 // Locate an accessor in the instance descriptor.
3782 AccessorDescriptor* FindAccessor(String* name);
3783
3784 // Code cache operations.
3785
3786 // Clears the code cache.
Steve Block44f0eee2011-05-26 01:26:41 +01003787 inline void ClearCodeCache(Heap* heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00003788
3789 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07003790 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003791
3792 // Returns the found code or undefined if absent.
3793 Object* FindInCodeCache(String* name, Code::Flags flags);
3794
3795 // Returns the non-negative index of the code object if it is in the
3796 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003797 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003798
3799 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003800 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003801
3802 // For every transition in this map, makes the transition's
3803 // target's prototype pointer point back to this map.
3804 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3805 void CreateBackPointers();
3806
3807 // Set all map transitions from this map to dead maps to null.
3808 // Also, restore the original prototype on the targets of these
3809 // transitions, so that we do not process this map again while
3810 // following back pointers.
Steve Block44f0eee2011-05-26 01:26:41 +01003811 void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00003812
3813 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003814#ifdef OBJECT_PRINT
3815 inline void MapPrint() {
3816 MapPrint(stdout);
3817 }
3818 void MapPrint(FILE* out);
3819#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003820#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003821 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003822 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00003823#endif
3824
Iain Merrick75681382010-08-19 15:07:18 +01003825 inline int visitor_id();
3826 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003827
Steve Block44f0eee2011-05-26 01:26:41 +01003828 // Returns the isolate/heap this map belongs to.
3829 inline Isolate* isolate();
3830 inline Heap* heap();
3831
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003832 typedef void (*TraverseCallback)(Map* map, void* data);
3833
3834 void TraverseTransitionTree(TraverseCallback callback, void* data);
3835
Steve Block053d10c2011-06-13 19:13:29 +01003836 static const int kMaxCachedPrototypeTransitions = 256;
3837
3838 Object* GetPrototypeTransition(Object* prototype);
3839
3840 MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
3841
Steve Blocka7e24c12009-10-30 11:49:00 +00003842 static const int kMaxPreAllocatedPropertyFields = 255;
3843
3844 // Layout description.
3845 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3846 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3847 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3848 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3849 static const int kInstanceDescriptorsOffset =
3850 kConstructorOffset + kPointerSize;
3851 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Steve Block053d10c2011-06-13 19:13:29 +01003852 static const int kPrototypeTransitionsOffset =
3853 kCodeCacheOffset + kPointerSize;
3854 static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003855 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3856
3857 // Layout of pointer fields. Heap iteration code relies on them
3858 // being continiously allocated.
3859 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3860 static const int kPointerFieldsEndOffset =
Steve Block053d10c2011-06-13 19:13:29 +01003861 Map::kPrototypeTransitionsOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003862
3863 // Byte offsets within kInstanceSizesOffset.
3864 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3865 static const int kInObjectPropertiesByte = 1;
3866 static const int kInObjectPropertiesOffset =
3867 kInstanceSizesOffset + kInObjectPropertiesByte;
3868 static const int kPreAllocatedPropertyFieldsByte = 2;
3869 static const int kPreAllocatedPropertyFieldsOffset =
3870 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003871 static const int kVisitorIdByte = 3;
3872 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00003873
3874 // Byte offsets within kInstanceAttributesOffset attributes.
3875 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3876 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3877 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3878 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3879
3880 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3881
3882 // Bit positions for bit field.
3883 static const int kUnused = 0; // To be used for marking recently used maps.
3884 static const int kHasNonInstancePrototype = 1;
3885 static const int kIsHiddenPrototype = 2;
3886 static const int kHasNamedInterceptor = 3;
3887 static const int kHasIndexedInterceptor = 4;
3888 static const int kIsUndetectable = 5;
3889 static const int kHasInstanceCallHandler = 6;
3890 static const int kIsAccessCheckNeeded = 7;
3891
3892 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003893 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003894 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003895 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003896 static const int kStringWrapperSafeForDefaultValueOf = 3;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003897 static const int kAttachedToSharedFunctionInfo = 4;
3898 static const int kIsShared = 5;
Steve Block44f0eee2011-05-26 01:26:41 +01003899 static const int kHasExternalArrayElements = 6;
Steve Block6ded16b2010-05-10 14:33:55 +01003900
3901 // Layout of the default cache. It holds alternating name and code objects.
3902 static const int kCodeCacheEntrySize = 2;
3903 static const int kCodeCacheEntryNameOffset = 0;
3904 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003905
Iain Merrick75681382010-08-19 15:07:18 +01003906 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3907 kPointerFieldsEndOffset,
3908 kSize> BodyDescriptor;
3909
Steve Blocka7e24c12009-10-30 11:49:00 +00003910 private:
3911 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3912};
3913
3914
3915// An abstract superclass, a marker class really, for simple structure classes.
3916// It doesn't carry much functionality but allows struct classes to me
3917// identified in the type system.
3918class Struct: public HeapObject {
3919 public:
3920 inline void InitializeBody(int object_size);
3921 static inline Struct* cast(Object* that);
3922};
3923
3924
3925// Script describes a script which has been added to the VM.
3926class Script: public Struct {
3927 public:
3928 // Script types.
3929 enum Type {
3930 TYPE_NATIVE = 0,
3931 TYPE_EXTENSION = 1,
3932 TYPE_NORMAL = 2
3933 };
3934
3935 // Script compilation types.
3936 enum CompilationType {
3937 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003938 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003939 };
3940
3941 // [source]: the script source.
3942 DECL_ACCESSORS(source, Object)
3943
3944 // [name]: the script name.
3945 DECL_ACCESSORS(name, Object)
3946
3947 // [id]: the script id.
3948 DECL_ACCESSORS(id, Object)
3949
3950 // [line_offset]: script line offset in resource from where it was extracted.
3951 DECL_ACCESSORS(line_offset, Smi)
3952
3953 // [column_offset]: script column offset in resource from where it was
3954 // extracted.
3955 DECL_ACCESSORS(column_offset, Smi)
3956
3957 // [data]: additional data associated with this script.
3958 DECL_ACCESSORS(data, Object)
3959
3960 // [context_data]: context data for the context this script was compiled in.
3961 DECL_ACCESSORS(context_data, Object)
3962
3963 // [wrapper]: the wrapper cache.
3964 DECL_ACCESSORS(wrapper, Proxy)
3965
3966 // [type]: the script type.
3967 DECL_ACCESSORS(type, Smi)
3968
3969 // [compilation]: how the the script was compiled.
3970 DECL_ACCESSORS(compilation_type, Smi)
3971
Steve Blockd0582a62009-12-15 09:54:21 +00003972 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003973 DECL_ACCESSORS(line_ends, Object)
3974
Steve Blockd0582a62009-12-15 09:54:21 +00003975 // [eval_from_shared]: for eval scripts the shared funcion info for the
3976 // function from which eval was called.
3977 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003978
3979 // [eval_from_instructions_offset]: the instruction offset in the code for the
3980 // function from which eval was called where eval was called.
3981 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3982
3983 static inline Script* cast(Object* obj);
3984
Steve Block3ce2e202009-11-05 08:53:23 +00003985 // If script source is an external string, check that the underlying
3986 // resource is accessible. Otherwise, always return true.
3987 inline bool HasValidSource();
3988
Ben Murdochb0fe1622011-05-05 13:52:32 +01003989#ifdef OBJECT_PRINT
3990 inline void ScriptPrint() {
3991 ScriptPrint(stdout);
3992 }
3993 void ScriptPrint(FILE* out);
3994#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003995#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003996 void ScriptVerify();
3997#endif
3998
3999 static const int kSourceOffset = HeapObject::kHeaderSize;
4000 static const int kNameOffset = kSourceOffset + kPointerSize;
4001 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4002 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
4003 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
4004 static const int kContextOffset = kDataOffset + kPointerSize;
4005 static const int kWrapperOffset = kContextOffset + kPointerSize;
4006 static const int kTypeOffset = kWrapperOffset + kPointerSize;
4007 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
4008 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
4009 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00004010 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004011 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00004012 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004013 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
4014
4015 private:
4016 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4017};
4018
4019
Ben Murdochb0fe1622011-05-05 13:52:32 +01004020// List of builtin functions we want to identify to improve code
4021// generation.
4022//
4023// Each entry has a name of a global object property holding an object
4024// optionally followed by ".prototype", a name of a builtin function
4025// on the object (the one the id is set for), and a label.
4026//
4027// Installation of ids for the selected builtin functions is handled
4028// by the bootstrapper.
4029//
4030// NOTE: Order is important: math functions should be at the end of
4031// the list and MathFloor should be the first math function.
4032#define FUNCTIONS_WITH_ID_LIST(V) \
4033 V(Array.prototype, push, ArrayPush) \
4034 V(Array.prototype, pop, ArrayPop) \
4035 V(String.prototype, charCodeAt, StringCharCodeAt) \
4036 V(String.prototype, charAt, StringCharAt) \
4037 V(String, fromCharCode, StringFromCharCode) \
4038 V(Math, floor, MathFloor) \
4039 V(Math, round, MathRound) \
4040 V(Math, ceil, MathCeil) \
4041 V(Math, abs, MathAbs) \
4042 V(Math, log, MathLog) \
4043 V(Math, sin, MathSin) \
4044 V(Math, cos, MathCos) \
4045 V(Math, tan, MathTan) \
4046 V(Math, asin, MathASin) \
4047 V(Math, acos, MathACos) \
4048 V(Math, atan, MathATan) \
4049 V(Math, exp, MathExp) \
4050 V(Math, sqrt, MathSqrt) \
4051 V(Math, pow, MathPow)
4052
4053
4054enum BuiltinFunctionId {
4055#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
4056 k##name,
4057 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4058#undef DECLARE_FUNCTION_ID
4059 // Fake id for a special case of Math.pow. Note, it continues the
4060 // list of math functions.
4061 kMathPowHalf,
4062 kFirstMathFunctionId = kMathFloor
4063};
4064
4065
Steve Blocka7e24c12009-10-30 11:49:00 +00004066// SharedFunctionInfo describes the JSFunction information that can be
4067// shared by multiple instances of the function.
4068class SharedFunctionInfo: public HeapObject {
4069 public:
4070 // [name]: Function name.
4071 DECL_ACCESSORS(name, Object)
4072
4073 // [code]: Function code.
4074 DECL_ACCESSORS(code, Code)
4075
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004076 // [scope_info]: Scope info.
4077 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
4078
Steve Blocka7e24c12009-10-30 11:49:00 +00004079 // [construct stub]: Code stub for constructing instances of this function.
4080 DECL_ACCESSORS(construct_stub, Code)
4081
Iain Merrick75681382010-08-19 15:07:18 +01004082 inline Code* unchecked_code();
4083
Steve Blocka7e24c12009-10-30 11:49:00 +00004084 // Returns if this function has been compiled to native code yet.
4085 inline bool is_compiled();
4086
4087 // [length]: The function length - usually the number of declared parameters.
4088 // Use up to 2^30 parameters.
4089 inline int length();
4090 inline void set_length(int value);
4091
4092 // [formal parameter count]: The declared number of parameters.
4093 inline int formal_parameter_count();
4094 inline void set_formal_parameter_count(int value);
4095
4096 // Set the formal parameter count so the function code will be
4097 // called without using argument adaptor frames.
4098 inline void DontAdaptArguments();
4099
4100 // [expected_nof_properties]: Expected number of properties for the function.
4101 inline int expected_nof_properties();
4102 inline void set_expected_nof_properties(int value);
4103
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004104 // Inobject slack tracking is the way to reclaim unused inobject space.
4105 //
4106 // The instance size is initially determined by adding some slack to
4107 // expected_nof_properties (to allow for a few extra properties added
4108 // after the constructor). There is no guarantee that the extra space
4109 // will not be wasted.
4110 //
4111 // Here is the algorithm to reclaim the unused inobject space:
4112 // - Detect the first constructor call for this SharedFunctionInfo.
4113 // When it happens enter the "in progress" state: remember the
4114 // constructor's initial_map and install a special construct stub that
4115 // counts constructor calls.
4116 // - While the tracking is in progress create objects filled with
4117 // one_pointer_filler_map instead of undefined_value. This way they can be
4118 // resized quickly and safely.
4119 // - Once enough (kGenerousAllocationCount) objects have been created
4120 // compute the 'slack' (traverse the map transition tree starting from the
4121 // initial_map and find the lowest value of unused_property_fields).
4122 // - Traverse the transition tree again and decrease the instance size
4123 // of every map. Existing objects will resize automatically (they are
4124 // filled with one_pointer_filler_map). All further allocations will
4125 // use the adjusted instance size.
4126 // - Decrease expected_nof_properties so that an allocations made from
4127 // another context will use the adjusted instance size too.
4128 // - Exit "in progress" state by clearing the reference to the initial_map
4129 // and setting the regular construct stub (generic or inline).
4130 //
4131 // The above is the main event sequence. Some special cases are possible
4132 // while the tracking is in progress:
4133 //
4134 // - GC occurs.
4135 // Check if the initial_map is referenced by any live objects (except this
4136 // SharedFunctionInfo). If it is, continue tracking as usual.
4137 // If it is not, clear the reference and reset the tracking state. The
4138 // tracking will be initiated again on the next constructor call.
4139 //
4140 // - The constructor is called from another context.
4141 // Immediately complete the tracking, perform all the necessary changes
4142 // to maps. This is necessary because there is no efficient way to track
4143 // multiple initial_maps.
4144 // Proceed to create an object in the current context (with the adjusted
4145 // size).
4146 //
4147 // - A different constructor function sharing the same SharedFunctionInfo is
4148 // called in the same context. This could be another closure in the same
4149 // context, or the first function could have been disposed.
4150 // This is handled the same way as the previous case.
4151 //
4152 // Important: inobject slack tracking is not attempted during the snapshot
4153 // creation.
4154
Ben Murdochf87a2032010-10-22 12:50:53 +01004155 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004156
4157 // [construction_count]: Counter for constructor calls made during
4158 // the tracking phase.
4159 inline int construction_count();
4160 inline void set_construction_count(int value);
4161
4162 // [initial_map]: initial map of the first function called as a constructor.
4163 // Saved for the duration of the tracking phase.
4164 // This is a weak link (GC resets it to undefined_value if no other live
4165 // object reference this map).
4166 DECL_ACCESSORS(initial_map, Object)
4167
4168 // True if the initial_map is not undefined and the countdown stub is
4169 // installed.
4170 inline bool IsInobjectSlackTrackingInProgress();
4171
4172 // Starts the tracking.
4173 // Stores the initial map and installs the countdown stub.
4174 // IsInobjectSlackTrackingInProgress is normally true after this call,
4175 // except when tracking have not been started (e.g. the map has no unused
4176 // properties or the snapshot is being built).
4177 void StartInobjectSlackTracking(Map* map);
4178
4179 // Completes the tracking.
4180 // IsInobjectSlackTrackingInProgress is false after this call.
4181 void CompleteInobjectSlackTracking();
4182
4183 // Clears the initial_map before the GC marking phase to ensure the reference
4184 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4185 void DetachInitialMap();
4186
4187 // Restores the link to the initial map after the GC marking phase.
4188 // IsInobjectSlackTrackingInProgress is true after this call.
4189 void AttachInitialMap(Map* map);
4190
4191 // False if there are definitely no live objects created from this function.
4192 // True if live objects _may_ exist (existence not guaranteed).
4193 // May go back from true to false after GC.
4194 inline bool live_objects_may_exist();
4195
4196 inline void set_live_objects_may_exist(bool value);
4197
Steve Blocka7e24c12009-10-30 11:49:00 +00004198 // [instance class name]: class name for instances.
4199 DECL_ACCESSORS(instance_class_name, Object)
4200
Steve Block6ded16b2010-05-10 14:33:55 +01004201 // [function data]: This field holds some additional data for function.
4202 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004203 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004204 // In the long run we don't want all functions to have this field but
4205 // we can fix that when we have a better model for storing hidden data
4206 // on objects.
4207 DECL_ACCESSORS(function_data, Object)
4208
Steve Block6ded16b2010-05-10 14:33:55 +01004209 inline bool IsApiFunction();
4210 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004211 inline bool HasBuiltinFunctionId();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004212 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004213
Steve Blocka7e24c12009-10-30 11:49:00 +00004214 // [script info]: Script from which the function originates.
4215 DECL_ACCESSORS(script, Object)
4216
Steve Block6ded16b2010-05-10 14:33:55 +01004217 // [num_literals]: Number of literals used by this function.
4218 inline int num_literals();
4219 inline void set_num_literals(int value);
4220
Steve Blocka7e24c12009-10-30 11:49:00 +00004221 // [start_position_and_type]: Field used to store both the source code
4222 // position, whether or not the function is a function expression,
4223 // and whether or not the function is a toplevel function. The two
4224 // least significants bit indicates whether the function is an
4225 // expression and the rest contains the source code position.
4226 inline int start_position_and_type();
4227 inline void set_start_position_and_type(int value);
4228
4229 // [debug info]: Debug information.
4230 DECL_ACCESSORS(debug_info, Object)
4231
4232 // [inferred name]: Name inferred from variable or property
4233 // assignment of this function. Used to facilitate debugging and
4234 // profiling of JavaScript code written in OO style, where almost
4235 // all functions are anonymous but are assigned to object
4236 // properties.
4237 DECL_ACCESSORS(inferred_name, String)
4238
Ben Murdochf87a2032010-10-22 12:50:53 +01004239 // The function's name if it is non-empty, otherwise the inferred name.
4240 String* DebugName();
4241
Steve Blocka7e24c12009-10-30 11:49:00 +00004242 // Position of the 'function' token in the script source.
4243 inline int function_token_position();
4244 inline void set_function_token_position(int function_token_position);
4245
4246 // Position of this function in the script source.
4247 inline int start_position();
4248 inline void set_start_position(int start_position);
4249
4250 // End position of this function in the script source.
4251 inline int end_position();
4252 inline void set_end_position(int end_position);
4253
4254 // Is this function a function expression in the source code.
4255 inline bool is_expression();
4256 inline void set_is_expression(bool value);
4257
4258 // Is this function a top-level function (scripts, evals).
4259 inline bool is_toplevel();
4260 inline void set_is_toplevel(bool value);
4261
4262 // Bit field containing various information collected by the compiler to
4263 // drive optimization.
4264 inline int compiler_hints();
4265 inline void set_compiler_hints(int value);
4266
Ben Murdochb0fe1622011-05-05 13:52:32 +01004267 // A counter used to determine when to stress the deoptimizer with a
4268 // deopt.
4269 inline Smi* deopt_counter();
4270 inline void set_deopt_counter(Smi* counter);
4271
Steve Blocka7e24c12009-10-30 11:49:00 +00004272 // Add information on assignments of the form this.x = ...;
4273 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004274 bool has_only_simple_this_property_assignments,
4275 FixedArray* this_property_assignments);
4276
4277 // Clear information on assignments of the form this.x = ...;
4278 void ClearThisPropertyAssignmentsInfo();
4279
4280 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004281 // this.x = y; where y is either a constant or refers to an argument.
4282 inline bool has_only_simple_this_property_assignments();
4283
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004284 // Indicates if this function can be lazy compiled.
4285 // This is used to determine if we can safely flush code from a function
4286 // when doing GC if we expect that the function will no longer be used.
4287 inline bool allows_lazy_compilation();
4288 inline void set_allows_lazy_compilation(bool flag);
4289
Iain Merrick75681382010-08-19 15:07:18 +01004290 // Indicates how many full GCs this function has survived with assigned
4291 // code object. Used to determine when it is relatively safe to flush
4292 // this code object and replace it with lazy compilation stub.
4293 // Age is reset when GC notices that the code object is referenced
4294 // from the stack or compilation cache.
4295 inline int code_age();
4296 inline void set_code_age(int age);
4297
Ben Murdochb0fe1622011-05-05 13:52:32 +01004298 // Indicates whether optimizations have been disabled for this
4299 // shared function info. If a function is repeatedly optimized or if
4300 // we cannot optimize the function we disable optimization to avoid
4301 // spending time attempting to optimize it again.
4302 inline bool optimization_disabled();
4303 inline void set_optimization_disabled(bool value);
4304
Steve Block1e0659c2011-05-24 12:43:12 +01004305 // Indicates whether the function is a strict mode function.
4306 inline bool strict_mode();
4307 inline void set_strict_mode(bool value);
4308
Ben Murdochb0fe1622011-05-05 13:52:32 +01004309 // Indicates whether or not the code in the shared function support
4310 // deoptimization.
4311 inline bool has_deoptimization_support();
4312
4313 // Enable deoptimization support through recompiled code.
4314 void EnableDeoptimizationSupport(Code* recompiled);
4315
4316 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4317 // code, returns whether it asserted (i.e., always true if assertions are
4318 // disabled).
4319 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004320
Andrei Popescu402d9372010-02-26 13:31:12 +00004321 // Check whether a inlined constructor can be generated with the given
4322 // prototype.
4323 bool CanGenerateInlineConstructor(Object* prototype);
4324
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004325 // Prevents further attempts to generate inline constructors.
4326 // To be called if generation failed for any reason.
4327 void ForbidInlineConstructor();
4328
Steve Blocka7e24c12009-10-30 11:49:00 +00004329 // For functions which only contains this property assignments this provides
4330 // access to the names for the properties assigned.
4331 DECL_ACCESSORS(this_property_assignments, Object)
4332 inline int this_property_assignments_count();
4333 inline void set_this_property_assignments_count(int value);
4334 String* GetThisPropertyAssignmentName(int index);
4335 bool IsThisPropertyAssignmentArgument(int index);
4336 int GetThisPropertyAssignmentArgument(int index);
4337 Object* GetThisPropertyAssignmentConstant(int index);
4338
4339 // [source code]: Source code for the function.
4340 bool HasSourceCode();
4341 Object* GetSourceCode();
4342
Ben Murdochb0fe1622011-05-05 13:52:32 +01004343 inline int opt_count();
4344 inline void set_opt_count(int opt_count);
4345
4346 // Source size of this function.
4347 int SourceSize();
4348
Steve Blocka7e24c12009-10-30 11:49:00 +00004349 // Calculate the instance size.
4350 int CalculateInstanceSize();
4351
4352 // Calculate the number of in-object properties.
4353 int CalculateInObjectProperties();
4354
4355 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004356 // Set max_length to -1 for unlimited length.
4357 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004358#ifdef OBJECT_PRINT
4359 inline void SharedFunctionInfoPrint() {
4360 SharedFunctionInfoPrint(stdout);
4361 }
4362 void SharedFunctionInfoPrint(FILE* out);
4363#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004364#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004365 void SharedFunctionInfoVerify();
4366#endif
4367
4368 // Casting.
4369 static inline SharedFunctionInfo* cast(Object* obj);
4370
4371 // Constants.
4372 static const int kDontAdaptArgumentsSentinel = -1;
4373
4374 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004375 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004376 static const int kNameOffset = HeapObject::kHeaderSize;
4377 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004378 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4379 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004380 static const int kInstanceClassNameOffset =
4381 kConstructStubOffset + kPointerSize;
4382 static const int kFunctionDataOffset =
4383 kInstanceClassNameOffset + kPointerSize;
4384 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4385 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4386 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004387 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004388 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004389 static const int kThisPropertyAssignmentsOffset =
4390 kInitialMapOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004391 static const int kDeoptCounterOffset =
4392 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004393#if V8_HOST_ARCH_32_BIT
4394 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004395 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004396 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004397 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4398 static const int kExpectedNofPropertiesOffset =
4399 kFormalParameterCountOffset + kPointerSize;
4400 static const int kNumLiteralsOffset =
4401 kExpectedNofPropertiesOffset + kPointerSize;
4402 static const int kStartPositionAndTypeOffset =
4403 kNumLiteralsOffset + kPointerSize;
4404 static const int kEndPositionOffset =
4405 kStartPositionAndTypeOffset + kPointerSize;
4406 static const int kFunctionTokenPositionOffset =
4407 kEndPositionOffset + kPointerSize;
4408 static const int kCompilerHintsOffset =
4409 kFunctionTokenPositionOffset + kPointerSize;
4410 static const int kThisPropertyAssignmentsCountOffset =
4411 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004412 static const int kOptCountOffset =
4413 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004414 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004415 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004416#else
4417 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004418 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004419 // garbage collections.
4420 // To avoid wasting space on 64-bit architectures we use
4421 // the following trick: we group integer fields into pairs
4422 // First integer in each pair is shifted left by 1.
4423 // By doing this we guarantee that LSB of each kPointerSize aligned
4424 // word is not set and thus this word cannot be treated as pointer
4425 // to HeapObject during old space traversal.
4426 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004427 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004428 static const int kFormalParameterCountOffset =
4429 kLengthOffset + kIntSize;
4430
Steve Blocka7e24c12009-10-30 11:49:00 +00004431 static const int kExpectedNofPropertiesOffset =
4432 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004433 static const int kNumLiteralsOffset =
4434 kExpectedNofPropertiesOffset + kIntSize;
4435
4436 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004437 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004438 static const int kStartPositionAndTypeOffset =
4439 kEndPositionOffset + kIntSize;
4440
4441 static const int kFunctionTokenPositionOffset =
4442 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004443 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004444 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004445
Steve Blocka7e24c12009-10-30 11:49:00 +00004446 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004447 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004448 static const int kOptCountOffset =
4449 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004450
Steve Block6ded16b2010-05-10 14:33:55 +01004451 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004452 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004453
4454#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004455
4456 // The construction counter for inobject slack tracking is stored in the
4457 // most significant byte of compiler_hints which is otherwise unused.
4458 // Its offset depends on the endian-ness of the architecture.
4459#if __BYTE_ORDER == __LITTLE_ENDIAN
4460 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4461#elif __BYTE_ORDER == __BIG_ENDIAN
4462 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4463#else
4464#error Unknown byte ordering
4465#endif
4466
Steve Block6ded16b2010-05-10 14:33:55 +01004467 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004468
Iain Merrick75681382010-08-19 15:07:18 +01004469 typedef FixedBodyDescriptor<kNameOffset,
4470 kThisPropertyAssignmentsOffset + kPointerSize,
4471 kSize> BodyDescriptor;
4472
Steve Blocka7e24c12009-10-30 11:49:00 +00004473 // Bit positions in start_position_and_type.
4474 // The source code start position is in the 30 most significant bits of
4475 // the start_position_and_type field.
4476 static const int kIsExpressionBit = 0;
4477 static const int kIsTopLevelBit = 1;
4478 static const int kStartPositionShift = 2;
4479 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4480
4481 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00004482 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004483 static const int kAllowLazyCompilation = 1;
4484 static const int kLiveObjectsMayExist = 2;
4485 static const int kCodeAgeShift = 3;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004486 static const int kCodeAgeMask = 0x7;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004487 static const int kOptimizationDisabled = 6;
4488 static const int kStrictModeFunction = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +00004489
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004490 private:
4491#if V8_HOST_ARCH_32_BIT
4492 // On 32 bit platforms, compiler hints is a smi.
4493 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4494 static const int kCompilerHintsSize = kPointerSize;
4495#else
4496 // On 64 bit platforms, compiler hints is not a smi, see comment above.
4497 static const int kCompilerHintsSmiTagSize = 0;
4498 static const int kCompilerHintsSize = kIntSize;
4499#endif
4500
4501 public:
4502 // Constants for optimizing codegen for strict mode function tests.
4503 // Allows to use byte-widgh instructions.
4504 static const int kStrictModeBitWithinByte =
4505 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4506
4507#if __BYTE_ORDER == __LITTLE_ENDIAN
4508 static const int kStrictModeByteOffset = kCompilerHintsOffset +
4509 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
4510#elif __BYTE_ORDER == __BIG_ENDIAN
4511 static const int kStrictModeByteOffset = kCompilerHintsOffset +
4512 (kCompilerHintsSize - 1) -
4513 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
4514#else
4515#error Unknown byte ordering
4516#endif
4517
4518 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00004519 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4520};
4521
4522
4523// JSFunction describes JavaScript functions.
4524class JSFunction: public JSObject {
4525 public:
4526 // [prototype_or_initial_map]:
4527 DECL_ACCESSORS(prototype_or_initial_map, Object)
4528
4529 // [shared_function_info]: The information about the function that
4530 // can be shared by instances.
4531 DECL_ACCESSORS(shared, SharedFunctionInfo)
4532
Iain Merrick75681382010-08-19 15:07:18 +01004533 inline SharedFunctionInfo* unchecked_shared();
4534
Steve Blocka7e24c12009-10-30 11:49:00 +00004535 // [context]: The context for this function.
4536 inline Context* context();
4537 inline Object* unchecked_context();
4538 inline void set_context(Object* context);
4539
4540 // [code]: The generated code object for this function. Executed
4541 // when the function is invoked, e.g. foo() or new foo(). See
4542 // [[Call]] and [[Construct]] description in ECMA-262, section
4543 // 8.6.2, page 27.
4544 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004545 inline void set_code(Code* code);
4546 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004547
Iain Merrick75681382010-08-19 15:07:18 +01004548 inline Code* unchecked_code();
4549
Steve Blocka7e24c12009-10-30 11:49:00 +00004550 // Tells whether this function is builtin.
4551 inline bool IsBuiltin();
4552
Ben Murdochb0fe1622011-05-05 13:52:32 +01004553 // Tells whether or not the function needs arguments adaption.
4554 inline bool NeedsArgumentsAdaption();
4555
4556 // Tells whether or not this function has been optimized.
4557 inline bool IsOptimized();
4558
Ben Murdoch8b112d22011-06-08 16:22:53 +01004559 // Tells whether or not this function can be optimized.
4560 inline bool IsOptimizable();
4561
Ben Murdochb0fe1622011-05-05 13:52:32 +01004562 // Mark this function for lazy recompilation. The function will be
4563 // recompiled the next time it is executed.
4564 void MarkForLazyRecompilation();
4565
4566 // Tells whether or not the function is already marked for lazy
4567 // recompilation.
4568 inline bool IsMarkedForLazyRecompilation();
4569
4570 // Compute a hash code for the source code of this function.
4571 uint32_t SourceHash();
4572
4573 // Check whether or not this function is inlineable.
4574 bool IsInlineable();
4575
Steve Blocka7e24c12009-10-30 11:49:00 +00004576 // [literals]: Fixed array holding the materialized literals.
4577 //
4578 // If the function contains object, regexp or array literals, the
4579 // literals array prefix contains the object, regexp, and array
4580 // function to be used when creating these literals. This is
4581 // necessary so that we do not dynamically lookup the object, regexp
4582 // or array functions. Performing a dynamic lookup, we might end up
4583 // using the functions from a new context that we should not have
4584 // access to.
4585 DECL_ACCESSORS(literals, FixedArray)
4586
4587 // The initial map for an object created by this constructor.
4588 inline Map* initial_map();
4589 inline void set_initial_map(Map* value);
4590 inline bool has_initial_map();
4591
4592 // Get and set the prototype property on a JSFunction. If the
4593 // function has an initial map the prototype is set on the initial
4594 // map. Otherwise, the prototype is put in the initial map field
4595 // until an initial map is needed.
4596 inline bool has_prototype();
4597 inline bool has_instance_prototype();
4598 inline Object* prototype();
4599 inline Object* instance_prototype();
4600 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07004601 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004602
Steve Block6ded16b2010-05-10 14:33:55 +01004603 // After prototype is removed, it will not be created when accessed, and
4604 // [[Construct]] from this function will not be allowed.
4605 Object* RemovePrototype();
4606 inline bool should_have_prototype();
4607
Steve Blocka7e24c12009-10-30 11:49:00 +00004608 // Accessor for this function's initial map's [[class]]
4609 // property. This is primarily used by ECMA native functions. This
4610 // method sets the class_name field of this function's initial map
4611 // to a given value. It creates an initial map if this function does
4612 // not have one. Note that this method does not copy the initial map
4613 // if it has one already, but simply replaces it with the new value.
4614 // Instances created afterwards will have a map whose [[class]] is
4615 // set to 'value', but there is no guarantees on instances created
4616 // before.
4617 Object* SetInstanceClassName(String* name);
4618
4619 // Returns if this function has been compiled to native code yet.
4620 inline bool is_compiled();
4621
Ben Murdochb0fe1622011-05-05 13:52:32 +01004622 // [next_function_link]: Field for linking functions. This list is treated as
4623 // a weak list by the GC.
4624 DECL_ACCESSORS(next_function_link, Object)
4625
4626 // Prints the name of the function using PrintF.
4627 inline void PrintName() {
4628 PrintName(stdout);
4629 }
4630 void PrintName(FILE* out);
4631
Steve Blocka7e24c12009-10-30 11:49:00 +00004632 // Casting.
4633 static inline JSFunction* cast(Object* obj);
4634
Steve Block791712a2010-08-27 10:21:07 +01004635 // Iterates the objects, including code objects indirectly referenced
4636 // through pointers to the first instruction in the code object.
4637 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
4638
Steve Blocka7e24c12009-10-30 11:49:00 +00004639 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004640#ifdef OBJECT_PRINT
4641 inline void JSFunctionPrint() {
4642 JSFunctionPrint(stdout);
4643 }
4644 void JSFunctionPrint(FILE* out);
4645#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004646#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004647 void JSFunctionVerify();
4648#endif
4649
4650 // Returns the number of allocated literals.
4651 inline int NumberOfLiterals();
4652
4653 // Retrieve the global context from a function's literal array.
4654 static Context* GlobalContextFromLiterals(FixedArray* literals);
4655
Ben Murdochb0fe1622011-05-05 13:52:32 +01004656 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
4657 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01004658 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01004659 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01004660 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004661 static const int kSharedFunctionInfoOffset =
4662 kPrototypeOrInitialMapOffset + kPointerSize;
4663 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
4664 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004665 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
4666 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
4667 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004668
4669 // Layout of the literals array.
4670 static const int kLiteralsPrefixSize = 1;
4671 static const int kLiteralGlobalContextIndex = 0;
4672 private:
4673 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
4674};
4675
4676
4677// JSGlobalProxy's prototype must be a JSGlobalObject or null,
4678// and the prototype is hidden. JSGlobalProxy always delegates
4679// property accesses to its prototype if the prototype is not null.
4680//
4681// A JSGlobalProxy can be reinitialized which will preserve its identity.
4682//
4683// Accessing a JSGlobalProxy requires security check.
4684
4685class JSGlobalProxy : public JSObject {
4686 public:
4687 // [context]: the owner global context of this proxy object.
4688 // It is null value if this object is not used by any context.
4689 DECL_ACCESSORS(context, Object)
4690
4691 // Casting.
4692 static inline JSGlobalProxy* cast(Object* obj);
4693
4694 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004695#ifdef OBJECT_PRINT
4696 inline void JSGlobalProxyPrint() {
4697 JSGlobalProxyPrint(stdout);
4698 }
4699 void JSGlobalProxyPrint(FILE* out);
4700#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004701#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004702 void JSGlobalProxyVerify();
4703#endif
4704
4705 // Layout description.
4706 static const int kContextOffset = JSObject::kHeaderSize;
4707 static const int kSize = kContextOffset + kPointerSize;
4708
4709 private:
4710
4711 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
4712};
4713
4714
4715// Forward declaration.
4716class JSBuiltinsObject;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004717class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00004718
4719// Common super class for JavaScript global objects and the special
4720// builtins global objects.
4721class GlobalObject: public JSObject {
4722 public:
4723 // [builtins]: the object holding the runtime routines written in JS.
4724 DECL_ACCESSORS(builtins, JSBuiltinsObject)
4725
4726 // [global context]: the global context corresponding to this global object.
4727 DECL_ACCESSORS(global_context, Context)
4728
4729 // [global receiver]: the global receiver object of the context
4730 DECL_ACCESSORS(global_receiver, JSObject)
4731
4732 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004733 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004734
John Reck59135872010-11-02 12:39:01 -07004735 // This is like GetProperty, but is used when you know the lookup won't fail
4736 // by throwing an exception. This is for the debug and builtins global
4737 // objects, where it is known which properties can be expected to be present
4738 // on the object.
4739 Object* GetPropertyNoExceptionThrown(String* key) {
4740 Object* answer = GetProperty(key)->ToObjectUnchecked();
4741 return answer;
4742 }
4743
Steve Blocka7e24c12009-10-30 11:49:00 +00004744 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07004745 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00004746
4747 // Casting.
4748 static inline GlobalObject* cast(Object* obj);
4749
4750 // Layout description.
4751 static const int kBuiltinsOffset = JSObject::kHeaderSize;
4752 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
4753 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
4754 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
4755
4756 private:
4757 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4758
4759 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
4760};
4761
4762
4763// JavaScript global object.
4764class JSGlobalObject: public GlobalObject {
4765 public:
4766
4767 // Casting.
4768 static inline JSGlobalObject* cast(Object* obj);
4769
4770 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004771#ifdef OBJECT_PRINT
4772 inline void JSGlobalObjectPrint() {
4773 JSGlobalObjectPrint(stdout);
4774 }
4775 void JSGlobalObjectPrint(FILE* out);
4776#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004777#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004778 void JSGlobalObjectVerify();
4779#endif
4780
4781 // Layout description.
4782 static const int kSize = GlobalObject::kHeaderSize;
4783
4784 private:
4785 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
4786};
4787
4788
4789// Builtins global object which holds the runtime routines written in
4790// JavaScript.
4791class JSBuiltinsObject: public GlobalObject {
4792 public:
4793 // Accessors for the runtime routines written in JavaScript.
4794 inline Object* javascript_builtin(Builtins::JavaScript id);
4795 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
4796
Steve Block6ded16b2010-05-10 14:33:55 +01004797 // Accessors for code of the runtime routines written in JavaScript.
4798 inline Code* javascript_builtin_code(Builtins::JavaScript id);
4799 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
4800
Steve Blocka7e24c12009-10-30 11:49:00 +00004801 // Casting.
4802 static inline JSBuiltinsObject* cast(Object* obj);
4803
4804 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004805#ifdef OBJECT_PRINT
4806 inline void JSBuiltinsObjectPrint() {
4807 JSBuiltinsObjectPrint(stdout);
4808 }
4809 void JSBuiltinsObjectPrint(FILE* out);
4810#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004811#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004812 void JSBuiltinsObjectVerify();
4813#endif
4814
4815 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01004816 // room for two pointers per runtime routine written in javascript
4817 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00004818 static const int kJSBuiltinsCount = Builtins::id_count;
4819 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004820 static const int kJSBuiltinsCodeOffset =
4821 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004822 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01004823 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
4824
4825 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
4826 return kJSBuiltinsOffset + id * kPointerSize;
4827 }
4828
4829 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
4830 return kJSBuiltinsCodeOffset + id * kPointerSize;
4831 }
4832
Steve Blocka7e24c12009-10-30 11:49:00 +00004833 private:
4834 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
4835};
4836
4837
4838// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
4839class JSValue: public JSObject {
4840 public:
4841 // [value]: the object being wrapped.
4842 DECL_ACCESSORS(value, Object)
4843
4844 // Casting.
4845 static inline JSValue* cast(Object* obj);
4846
4847 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004848#ifdef OBJECT_PRINT
4849 inline void JSValuePrint() {
4850 JSValuePrint(stdout);
4851 }
4852 void JSValuePrint(FILE* out);
4853#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004854#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004855 void JSValueVerify();
4856#endif
4857
4858 // Layout description.
4859 static const int kValueOffset = JSObject::kHeaderSize;
4860 static const int kSize = kValueOffset + kPointerSize;
4861
4862 private:
4863 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
4864};
4865
Steve Block1e0659c2011-05-24 12:43:12 +01004866
4867// Representation of message objects used for error reporting through
4868// the API. The messages are formatted in JavaScript so this object is
4869// a real JavaScript object. The information used for formatting the
4870// error messages are not directly accessible from JavaScript to
4871// prevent leaking information to user code called during error
4872// formatting.
4873class JSMessageObject: public JSObject {
4874 public:
4875 // [type]: the type of error message.
4876 DECL_ACCESSORS(type, String)
4877
4878 // [arguments]: the arguments for formatting the error message.
4879 DECL_ACCESSORS(arguments, JSArray)
4880
4881 // [script]: the script from which the error message originated.
4882 DECL_ACCESSORS(script, Object)
4883
4884 // [stack_trace]: the stack trace for this error message.
4885 DECL_ACCESSORS(stack_trace, Object)
4886
4887 // [stack_frames]: an array of stack frames for this error object.
4888 DECL_ACCESSORS(stack_frames, Object)
4889
4890 // [start_position]: the start position in the script for the error message.
4891 inline int start_position();
4892 inline void set_start_position(int value);
4893
4894 // [end_position]: the end position in the script for the error message.
4895 inline int end_position();
4896 inline void set_end_position(int value);
4897
4898 // Casting.
4899 static inline JSMessageObject* cast(Object* obj);
4900
4901 // Dispatched behavior.
4902#ifdef OBJECT_PRINT
4903 inline void JSMessageObjectPrint() {
4904 JSMessageObjectPrint(stdout);
4905 }
4906 void JSMessageObjectPrint(FILE* out);
4907#endif
4908#ifdef DEBUG
4909 void JSMessageObjectVerify();
4910#endif
4911
4912 // Layout description.
4913 static const int kTypeOffset = JSObject::kHeaderSize;
4914 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
4915 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
4916 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
4917 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
4918 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
4919 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
4920 static const int kSize = kEndPositionOffset + kPointerSize;
4921
4922 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
4923 kStackFramesOffset + kPointerSize,
4924 kSize> BodyDescriptor;
4925};
4926
4927
Steve Blocka7e24c12009-10-30 11:49:00 +00004928// Regular expressions
4929// The regular expression holds a single reference to a FixedArray in
4930// the kDataOffset field.
4931// The FixedArray contains the following data:
4932// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
4933// - reference to the original source string
4934// - reference to the original flag string
4935// If it is an atom regexp
4936// - a reference to a literal string to search for
4937// If it is an irregexp regexp:
4938// - a reference to code for ASCII inputs (bytecode or compiled).
4939// - a reference to code for UC16 inputs (bytecode or compiled).
4940// - max number of registers used by irregexp implementations.
4941// - number of capture registers (output values) of the regexp.
4942class JSRegExp: public JSObject {
4943 public:
4944 // Meaning of Type:
4945 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
4946 // ATOM: A simple string to match against using an indexOf operation.
4947 // IRREGEXP: Compiled with Irregexp.
4948 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
4949 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
4950 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
4951
4952 class Flags {
4953 public:
4954 explicit Flags(uint32_t value) : value_(value) { }
4955 bool is_global() { return (value_ & GLOBAL) != 0; }
4956 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
4957 bool is_multiline() { return (value_ & MULTILINE) != 0; }
4958 uint32_t value() { return value_; }
4959 private:
4960 uint32_t value_;
4961 };
4962
4963 DECL_ACCESSORS(data, Object)
4964
4965 inline Type TypeTag();
4966 inline int CaptureCount();
4967 inline Flags GetFlags();
4968 inline String* Pattern();
4969 inline Object* DataAt(int index);
4970 // Set implementation data after the object has been prepared.
4971 inline void SetDataAt(int index, Object* value);
4972 static int code_index(bool is_ascii) {
4973 if (is_ascii) {
4974 return kIrregexpASCIICodeIndex;
4975 } else {
4976 return kIrregexpUC16CodeIndex;
4977 }
4978 }
4979
4980 static inline JSRegExp* cast(Object* obj);
4981
4982 // Dispatched behavior.
4983#ifdef DEBUG
4984 void JSRegExpVerify();
4985#endif
4986
4987 static const int kDataOffset = JSObject::kHeaderSize;
4988 static const int kSize = kDataOffset + kPointerSize;
4989
4990 // Indices in the data array.
4991 static const int kTagIndex = 0;
4992 static const int kSourceIndex = kTagIndex + 1;
4993 static const int kFlagsIndex = kSourceIndex + 1;
4994 static const int kDataIndex = kFlagsIndex + 1;
4995 // The data fields are used in different ways depending on the
4996 // value of the tag.
4997 // Atom regexps (literal strings).
4998 static const int kAtomPatternIndex = kDataIndex;
4999
5000 static const int kAtomDataSize = kAtomPatternIndex + 1;
5001
5002 // Irregexp compiled code or bytecode for ASCII. If compilation
5003 // fails, this fields hold an exception object that should be
5004 // thrown if the regexp is used again.
5005 static const int kIrregexpASCIICodeIndex = kDataIndex;
5006 // Irregexp compiled code or bytecode for UC16. If compilation
5007 // fails, this fields hold an exception object that should be
5008 // thrown if the regexp is used again.
5009 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
5010 // Maximal number of registers used by either ASCII or UC16.
5011 // Only used to check that there is enough stack space
5012 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
5013 // Number of captures in the compiled regexp.
5014 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
5015
5016 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00005017
5018 // Offsets directly into the data fixed array.
5019 static const int kDataTagOffset =
5020 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5021 static const int kDataAsciiCodeOffset =
5022 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00005023 static const int kDataUC16CodeOffset =
5024 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00005025 static const int kIrregexpCaptureCountOffset =
5026 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005027
5028 // In-object fields.
5029 static const int kSourceFieldIndex = 0;
5030 static const int kGlobalFieldIndex = 1;
5031 static const int kIgnoreCaseFieldIndex = 2;
5032 static const int kMultilineFieldIndex = 3;
5033 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01005034 static const int kInObjectFieldCount = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00005035};
5036
5037
5038class CompilationCacheShape {
5039 public:
5040 static inline bool IsMatch(HashTableKey* key, Object* value) {
5041 return key->IsMatch(value);
5042 }
5043
5044 static inline uint32_t Hash(HashTableKey* key) {
5045 return key->Hash();
5046 }
5047
5048 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5049 return key->HashForObject(object);
5050 }
5051
John Reck59135872010-11-02 12:39:01 -07005052 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005053 return key->AsObject();
5054 }
5055
5056 static const int kPrefixSize = 0;
5057 static const int kEntrySize = 2;
5058};
5059
Steve Block3ce2e202009-11-05 08:53:23 +00005060
Steve Blocka7e24c12009-10-30 11:49:00 +00005061class CompilationCacheTable: public HashTable<CompilationCacheShape,
5062 HashTableKey*> {
5063 public:
5064 // Find cached value for a string key, otherwise return null.
5065 Object* Lookup(String* src);
Steve Block1e0659c2011-05-24 12:43:12 +01005066 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00005067 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005068 MaybeObject* Put(String* src, Object* value);
Steve Block1e0659c2011-05-24 12:43:12 +01005069 MaybeObject* PutEval(String* src,
5070 Context* context,
5071 SharedFunctionInfo* value);
John Reck59135872010-11-02 12:39:01 -07005072 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005073
Ben Murdochb0fe1622011-05-05 13:52:32 +01005074 // Remove given value from cache.
5075 void Remove(Object* value);
5076
Steve Blocka7e24c12009-10-30 11:49:00 +00005077 static inline CompilationCacheTable* cast(Object* obj);
5078
5079 private:
5080 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
5081};
5082
5083
Steve Block6ded16b2010-05-10 14:33:55 +01005084class CodeCache: public Struct {
5085 public:
5086 DECL_ACCESSORS(default_cache, FixedArray)
5087 DECL_ACCESSORS(normal_type_cache, Object)
5088
5089 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07005090 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005091
5092 // Lookup code object in the cache. Returns code object if found and undefined
5093 // if not.
5094 Object* Lookup(String* name, Code::Flags flags);
5095
5096 // Get the internal index of a code object in the cache. Returns -1 if the
5097 // code object is not in that cache. This index can be used to later call
5098 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5099 // RemoveByIndex.
5100 int GetIndex(Object* name, Code* code);
5101
5102 // Remove an object from the cache with the provided internal index.
5103 void RemoveByIndex(Object* name, Code* code, int index);
5104
5105 static inline CodeCache* cast(Object* obj);
5106
Ben Murdochb0fe1622011-05-05 13:52:32 +01005107#ifdef OBJECT_PRINT
5108 inline void CodeCachePrint() {
5109 CodeCachePrint(stdout);
5110 }
5111 void CodeCachePrint(FILE* out);
5112#endif
Steve Block6ded16b2010-05-10 14:33:55 +01005113#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01005114 void CodeCacheVerify();
5115#endif
5116
5117 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5118 static const int kNormalTypeCacheOffset =
5119 kDefaultCacheOffset + kPointerSize;
5120 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5121
5122 private:
John Reck59135872010-11-02 12:39:01 -07005123 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5124 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005125 Object* LookupDefaultCache(String* name, Code::Flags flags);
5126 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5127
5128 // Code cache layout of the default cache. Elements are alternating name and
5129 // code objects for non normal load/store/call IC's.
5130 static const int kCodeCacheEntrySize = 2;
5131 static const int kCodeCacheEntryNameOffset = 0;
5132 static const int kCodeCacheEntryCodeOffset = 1;
5133
5134 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5135};
5136
5137
5138class CodeCacheHashTableShape {
5139 public:
5140 static inline bool IsMatch(HashTableKey* key, Object* value) {
5141 return key->IsMatch(value);
5142 }
5143
5144 static inline uint32_t Hash(HashTableKey* key) {
5145 return key->Hash();
5146 }
5147
5148 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5149 return key->HashForObject(object);
5150 }
5151
John Reck59135872010-11-02 12:39:01 -07005152 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01005153 return key->AsObject();
5154 }
5155
5156 static const int kPrefixSize = 0;
5157 static const int kEntrySize = 2;
5158};
5159
5160
5161class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5162 HashTableKey*> {
5163 public:
5164 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005165 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005166
5167 int GetIndex(String* name, Code::Flags flags);
5168 void RemoveByIndex(int index);
5169
5170 static inline CodeCacheHashTable* cast(Object* obj);
5171
5172 // Initial size of the fixed array backing the hash table.
5173 static const int kInitialSize = 64;
5174
5175 private:
5176 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5177};
5178
5179
Steve Blocka7e24c12009-10-30 11:49:00 +00005180enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5181enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5182
5183
5184class StringHasher {
5185 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +01005186 explicit inline StringHasher(int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005187
5188 // Returns true if the hash of this string can be computed without
5189 // looking at the contents.
5190 inline bool has_trivial_hash();
5191
5192 // Add a character to the hash and update the array index calculation.
5193 inline void AddCharacter(uc32 c);
5194
5195 // Adds a character to the hash but does not update the array index
5196 // calculation. This can only be called when it has been verified
5197 // that the input is not an array index.
5198 inline void AddCharacterNoIndex(uc32 c);
5199
5200 // Returns the value to store in the hash field of a string with
5201 // the given length and contents.
5202 uint32_t GetHashField();
5203
5204 // Returns true if the characters seen so far make up a legal array
5205 // index.
5206 bool is_array_index() { return is_array_index_; }
5207
5208 bool is_valid() { return is_valid_; }
5209
5210 void invalidate() { is_valid_ = false; }
5211
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005212 // Calculated hash value for a string consisting of 1 to
5213 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5214 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005215 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005216
Steve Blocka7e24c12009-10-30 11:49:00 +00005217 private:
5218
5219 uint32_t array_index() {
5220 ASSERT(is_array_index());
5221 return array_index_;
5222 }
5223
5224 inline uint32_t GetHash();
5225
5226 int length_;
5227 uint32_t raw_running_hash_;
5228 uint32_t array_index_;
5229 bool is_array_index_;
5230 bool is_first_char_;
5231 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00005232 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00005233};
5234
5235
Steve Block44f0eee2011-05-26 01:26:41 +01005236// Calculates string hash.
5237template <typename schar>
5238inline uint32_t HashSequentialString(const schar* chars, int length);
5239
5240
Steve Blocka7e24c12009-10-30 11:49:00 +00005241// The characteristics of a string are stored in its map. Retrieving these
5242// few bits of information is moderately expensive, involving two memory
5243// loads where the second is dependent on the first. To improve efficiency
5244// the shape of the string is given its own class so that it can be retrieved
5245// once and used for several string operations. A StringShape is small enough
5246// to be passed by value and is immutable, but be aware that flattening a
5247// string can potentially alter its shape. Also be aware that a GC caused by
5248// something else can alter the shape of a string due to ConsString
5249// shortcutting. Keeping these restrictions in mind has proven to be error-
5250// prone and so we no longer put StringShapes in variables unless there is a
5251// concrete performance benefit at that particular point in the code.
5252class StringShape BASE_EMBEDDED {
5253 public:
5254 inline explicit StringShape(String* s);
5255 inline explicit StringShape(Map* s);
5256 inline explicit StringShape(InstanceType t);
5257 inline bool IsSequential();
5258 inline bool IsExternal();
5259 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00005260 inline bool IsExternalAscii();
5261 inline bool IsExternalTwoByte();
5262 inline bool IsSequentialAscii();
5263 inline bool IsSequentialTwoByte();
5264 inline bool IsSymbol();
5265 inline StringRepresentationTag representation_tag();
5266 inline uint32_t full_representation_tag();
5267 inline uint32_t size_tag();
5268#ifdef DEBUG
5269 inline uint32_t type() { return type_; }
5270 inline void invalidate() { valid_ = false; }
5271 inline bool valid() { return valid_; }
5272#else
5273 inline void invalidate() { }
5274#endif
5275 private:
5276 uint32_t type_;
5277#ifdef DEBUG
5278 inline void set_valid() { valid_ = true; }
5279 bool valid_;
5280#else
5281 inline void set_valid() { }
5282#endif
5283};
5284
5285
5286// The String abstract class captures JavaScript string values:
5287//
5288// Ecma-262:
5289// 4.3.16 String Value
5290// A string value is a member of the type String and is a finite
5291// ordered sequence of zero or more 16-bit unsigned integer values.
5292//
5293// All string values have a length field.
5294class String: public HeapObject {
5295 public:
5296 // Get and set the length of the string.
5297 inline int length();
5298 inline void set_length(int value);
5299
Steve Blockd0582a62009-12-15 09:54:21 +00005300 // Get and set the hash field of the string.
5301 inline uint32_t hash_field();
5302 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005303
5304 inline bool IsAsciiRepresentation();
5305 inline bool IsTwoByteRepresentation();
5306
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005307 // Returns whether this string has ascii chars, i.e. all of them can
5308 // be ascii encoded. This might be the case even if the string is
5309 // two-byte. Such strings may appear when the embedder prefers
5310 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01005311 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005312 // NOTE: this should be considered only a hint. False negatives are
5313 // possible.
5314 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005315
Steve Blocka7e24c12009-10-30 11:49:00 +00005316 // Get and set individual two byte chars in the string.
5317 inline void Set(int index, uint16_t value);
5318 // Get individual two byte char in the string. Repeated calls
5319 // to this method are not efficient unless the string is flat.
5320 inline uint16_t Get(int index);
5321
Leon Clarkef7060e22010-06-03 12:02:55 +01005322 // Try to flatten the string. Checks first inline to see if it is
5323 // necessary. Does nothing if the string is not a cons string.
5324 // Flattening allocates a sequential string with the same data as
5325 // the given string and mutates the cons string to a degenerate
5326 // form, where the first component is the new sequential string and
5327 // the second component is the empty string. If allocation fails,
5328 // this function returns a failure. If flattening succeeds, this
5329 // function returns the sequential string that is now the first
5330 // component of the cons string.
5331 //
5332 // Degenerate cons strings are handled specially by the garbage
5333 // collector (see IsShortcutCandidate).
5334 //
5335 // Use FlattenString from Handles.cc to flatten even in case an
5336 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07005337 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005338
Leon Clarkef7060e22010-06-03 12:02:55 +01005339 // Convenience function. Has exactly the same behavior as
5340 // TryFlatten(), except in the case of failure returns the original
5341 // string.
5342 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5343
Steve Blocka7e24c12009-10-30 11:49:00 +00005344 Vector<const char> ToAsciiVector();
5345 Vector<const uc16> ToUC16Vector();
5346
5347 // Mark the string as an undetectable object. It only applies to
5348 // ascii and two byte string types.
5349 bool MarkAsUndetectable();
5350
Steve Blockd0582a62009-12-15 09:54:21 +00005351 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07005352 MUST_USE_RESULT MaybeObject* SubString(int from,
5353 int to,
5354 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005355
5356 // String equality operations.
5357 inline bool Equals(String* other);
5358 bool IsEqualTo(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +01005359 bool IsAsciiEqualTo(Vector<const char> str);
5360 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00005361
5362 // Return a UTF8 representation of the string. The string is null
5363 // terminated but may optionally contain nulls. Length is returned
5364 // in length_output if length_output is not a null pointer The string
5365 // should be nearly flat, otherwise the performance of this method may
5366 // be very slow (quadratic in the length). Setting robustness_flag to
5367 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5368 // handles unexpected data without causing assert failures and it does not
5369 // do any heap allocations. This is useful when printing stack traces.
5370 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5371 RobustnessFlag robustness_flag,
5372 int offset,
5373 int length,
5374 int* length_output = 0);
5375 SmartPointer<char> ToCString(
5376 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5377 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5378 int* length_output = 0);
5379
5380 int Utf8Length();
5381
5382 // Return a 16 bit Unicode representation of the string.
5383 // The string should be nearly flat, otherwise the performance of
5384 // of this method may be very bad. Setting robustness_flag to
5385 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5386 // handles unexpected data without causing assert failures and it does not
5387 // do any heap allocations. This is useful when printing stack traces.
5388 SmartPointer<uc16> ToWideCString(
5389 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
5390
5391 // Tells whether the hash code has been computed.
5392 inline bool HasHashCode();
5393
5394 // Returns a hash value used for the property table
5395 inline uint32_t Hash();
5396
Steve Blockd0582a62009-12-15 09:54:21 +00005397 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5398 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005399
5400 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5401 uint32_t* index,
5402 int length);
5403
5404 // Externalization.
5405 bool MakeExternal(v8::String::ExternalStringResource* resource);
5406 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5407
5408 // Conversion.
5409 inline bool AsArrayIndex(uint32_t* index);
5410
5411 // Casting.
5412 static inline String* cast(Object* obj);
5413
5414 void PrintOn(FILE* out);
5415
5416 // For use during stack traces. Performs rudimentary sanity check.
5417 bool LooksValid();
5418
5419 // Dispatched behavior.
5420 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005421#ifdef OBJECT_PRINT
5422 inline void StringPrint() {
5423 StringPrint(stdout);
5424 }
5425 void StringPrint(FILE* out);
5426#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005427#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005428 void StringVerify();
5429#endif
5430 inline bool IsFlat();
5431
5432 // Layout description.
5433 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005434 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005435 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005436
Steve Blockd0582a62009-12-15 09:54:21 +00005437 // Maximum number of characters to consider when trying to convert a string
5438 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00005439 static const int kMaxArrayIndexSize = 10;
5440
5441 // Max ascii char code.
5442 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
5443 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
5444 static const int kMaxUC16CharCode = 0xffff;
5445
Steve Blockd0582a62009-12-15 09:54:21 +00005446 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00005447 static const int kMinNonFlatLength = 13;
5448
5449 // Mask constant for checking if a string has a computed hash code
5450 // and if it is an array index. The least significant bit indicates
5451 // whether a hash code has been computed. If the hash code has been
5452 // computed the 2nd bit tells whether the string can be used as an
5453 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005454 static const int kHashNotComputedMask = 1;
5455 static const int kIsNotArrayIndexMask = 1 << 1;
5456 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00005457
Steve Blockd0582a62009-12-15 09:54:21 +00005458 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005459 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00005460
Steve Blocka7e24c12009-10-30 11:49:00 +00005461 // Array index strings this short can keep their index in the hash
5462 // field.
5463 static const int kMaxCachedArrayIndexLength = 7;
5464
Steve Blockd0582a62009-12-15 09:54:21 +00005465 // For strings which are array indexes the hash value has the string length
5466 // mixed into the hash, mainly to avoid a hash value of zero which would be
5467 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005468 static const int kArrayIndexValueBits = 24;
5469 static const int kArrayIndexLengthBits =
5470 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
5471
5472 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01005473 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005474
5475 static const int kArrayIndexHashLengthShift =
5476 kArrayIndexValueBits + kNofHashBitFields;
5477
Steve Blockd0582a62009-12-15 09:54:21 +00005478 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005479
5480 static const int kArrayIndexValueMask =
5481 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
5482
5483 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
5484 // could use a mask to test if the length of string is less than or equal to
5485 // kMaxCachedArrayIndexLength.
5486 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
5487
5488 static const int kContainsCachedArrayIndexMask =
5489 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
5490 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005491
5492 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005493 static const int kEmptyHashField =
5494 kIsNotArrayIndexMask | kHashNotComputedMask;
5495
5496 // Value of hash field containing computed hash equal to zero.
5497 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005498
5499 // Maximal string length.
5500 static const int kMaxLength = (1 << (32 - 2)) - 1;
5501
5502 // Max length for computing hash. For strings longer than this limit the
5503 // string length is used as the hash value.
5504 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00005505
5506 // Limit for truncation in short printing.
5507 static const int kMaxShortPrintLength = 1024;
5508
5509 // Support for regular expressions.
5510 const uc16* GetTwoByteData();
5511 const uc16* GetTwoByteData(unsigned start);
5512
5513 // Support for StringInputBuffer
5514 static const unibrow::byte* ReadBlock(String* input,
5515 unibrow::byte* util_buffer,
5516 unsigned capacity,
5517 unsigned* remaining,
5518 unsigned* offset);
5519 static const unibrow::byte* ReadBlock(String** input,
5520 unibrow::byte* util_buffer,
5521 unsigned capacity,
5522 unsigned* remaining,
5523 unsigned* offset);
5524
5525 // Helper function for flattening strings.
5526 template <typename sinkchar>
5527 static void WriteToFlat(String* source,
5528 sinkchar* sink,
5529 int from,
5530 int to);
5531
Steve Block9fac8402011-05-12 15:51:54 +01005532 static inline bool IsAscii(const char* chars, int length) {
5533 const char* limit = chars + length;
5534#ifdef V8_HOST_CAN_READ_UNALIGNED
5535 ASSERT(kMaxAsciiCharCode == 0x7F);
5536 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
5537 while (chars <= limit - sizeof(uintptr_t)) {
5538 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
5539 return false;
5540 }
5541 chars += sizeof(uintptr_t);
5542 }
5543#endif
5544 while (chars < limit) {
5545 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
5546 ++chars;
5547 }
5548 return true;
5549 }
5550
5551 static inline bool IsAscii(const uc16* chars, int length) {
5552 const uc16* limit = chars + length;
5553 while (chars < limit) {
5554 if (*chars > kMaxAsciiCharCodeU) return false;
5555 ++chars;
5556 }
5557 return true;
5558 }
5559
Steve Blocka7e24c12009-10-30 11:49:00 +00005560 protected:
5561 class ReadBlockBuffer {
5562 public:
5563 ReadBlockBuffer(unibrow::byte* util_buffer_,
5564 unsigned cursor_,
5565 unsigned capacity_,
5566 unsigned remaining_) :
5567 util_buffer(util_buffer_),
5568 cursor(cursor_),
5569 capacity(capacity_),
5570 remaining(remaining_) {
5571 }
5572 unibrow::byte* util_buffer;
5573 unsigned cursor;
5574 unsigned capacity;
5575 unsigned remaining;
5576 };
5577
Steve Blocka7e24c12009-10-30 11:49:00 +00005578 static inline const unibrow::byte* ReadBlock(String* input,
5579 ReadBlockBuffer* buffer,
5580 unsigned* offset,
5581 unsigned max_chars);
5582 static void ReadBlockIntoBuffer(String* input,
5583 ReadBlockBuffer* buffer,
5584 unsigned* offset_ptr,
5585 unsigned max_chars);
5586
5587 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01005588 // Try to flatten the top level ConsString that is hiding behind this
5589 // string. This is a no-op unless the string is a ConsString. Flatten
5590 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07005591 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01005592
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005593 static inline bool IsHashFieldComputed(uint32_t field);
5594
Steve Blocka7e24c12009-10-30 11:49:00 +00005595 // Slow case of String::Equals. This implementation works on any strings
5596 // but it is most efficient on strings that are almost flat.
5597 bool SlowEquals(String* other);
5598
5599 // Slow case of AsArrayIndex.
5600 bool SlowAsArrayIndex(uint32_t* index);
5601
5602 // Compute and set the hash code.
5603 uint32_t ComputeAndSetHash();
5604
5605 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
5606};
5607
5608
5609// The SeqString abstract class captures sequential string values.
5610class SeqString: public String {
5611 public:
5612
5613 // Casting.
5614 static inline SeqString* cast(Object* obj);
5615
Steve Blocka7e24c12009-10-30 11:49:00 +00005616 private:
5617 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
5618};
5619
5620
5621// The AsciiString class captures sequential ascii string objects.
5622// Each character in the AsciiString is an ascii character.
5623class SeqAsciiString: public SeqString {
5624 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005625 static const bool kHasAsciiEncoding = true;
5626
Steve Blocka7e24c12009-10-30 11:49:00 +00005627 // Dispatched behavior.
5628 inline uint16_t SeqAsciiStringGet(int index);
5629 inline void SeqAsciiStringSet(int index, uint16_t value);
5630
5631 // Get the address of the characters in this string.
5632 inline Address GetCharsAddress();
5633
5634 inline char* GetChars();
5635
5636 // Casting
5637 static inline SeqAsciiString* cast(Object* obj);
5638
5639 // Garbage collection support. This method is called by the
5640 // garbage collector to compute the actual size of an AsciiString
5641 // instance.
5642 inline int SeqAsciiStringSize(InstanceType instance_type);
5643
5644 // Computes the size for an AsciiString instance of a given length.
5645 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005646 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005647 }
5648
5649 // Layout description.
5650 static const int kHeaderSize = String::kSize;
5651 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5652
Leon Clarkee46be812010-01-19 14:06:41 +00005653 // Maximal memory usage for a single sequential ASCII string.
5654 static const int kMaxSize = 512 * MB;
5655 // Maximal length of a single sequential ASCII string.
5656 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5657 static const int kMaxLength = (kMaxSize - kHeaderSize);
5658
Steve Blocka7e24c12009-10-30 11:49:00 +00005659 // Support for StringInputBuffer.
5660 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5661 unsigned* offset,
5662 unsigned chars);
5663 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
5664 unsigned* offset,
5665 unsigned chars);
5666
5667 private:
5668 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
5669};
5670
5671
5672// The TwoByteString class captures sequential unicode string objects.
5673// Each character in the TwoByteString is a two-byte uint16_t.
5674class SeqTwoByteString: public SeqString {
5675 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005676 static const bool kHasAsciiEncoding = false;
5677
Steve Blocka7e24c12009-10-30 11:49:00 +00005678 // Dispatched behavior.
5679 inline uint16_t SeqTwoByteStringGet(int index);
5680 inline void SeqTwoByteStringSet(int index, uint16_t value);
5681
5682 // Get the address of the characters in this string.
5683 inline Address GetCharsAddress();
5684
5685 inline uc16* GetChars();
5686
5687 // For regexp code.
5688 const uint16_t* SeqTwoByteStringGetData(unsigned start);
5689
5690 // Casting
5691 static inline SeqTwoByteString* cast(Object* obj);
5692
5693 // Garbage collection support. This method is called by the
5694 // garbage collector to compute the actual size of a TwoByteString
5695 // instance.
5696 inline int SeqTwoByteStringSize(InstanceType instance_type);
5697
5698 // Computes the size for a TwoByteString instance of a given length.
5699 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005700 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005701 }
5702
5703 // Layout description.
5704 static const int kHeaderSize = String::kSize;
5705 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5706
Leon Clarkee46be812010-01-19 14:06:41 +00005707 // Maximal memory usage for a single sequential two-byte string.
5708 static const int kMaxSize = 512 * MB;
5709 // Maximal length of a single sequential two-byte string.
5710 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5711 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
5712
Steve Blocka7e24c12009-10-30 11:49:00 +00005713 // Support for StringInputBuffer.
5714 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5715 unsigned* offset_ptr,
5716 unsigned chars);
5717
5718 private:
5719 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
5720};
5721
5722
5723// The ConsString class describes string values built by using the
5724// addition operator on strings. A ConsString is a pair where the
5725// first and second components are pointers to other string values.
5726// One or both components of a ConsString can be pointers to other
5727// ConsStrings, creating a binary tree of ConsStrings where the leaves
5728// are non-ConsString string values. The string value represented by
5729// a ConsString can be obtained by concatenating the leaf string
5730// values in a left-to-right depth-first traversal of the tree.
5731class ConsString: public String {
5732 public:
5733 // First string of the cons cell.
5734 inline String* first();
5735 // Doesn't check that the result is a string, even in debug mode. This is
5736 // useful during GC where the mark bits confuse the checks.
5737 inline Object* unchecked_first();
5738 inline void set_first(String* first,
5739 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5740
5741 // Second string of the cons cell.
5742 inline String* second();
5743 // Doesn't check that the result is a string, even in debug mode. This is
5744 // useful during GC where the mark bits confuse the checks.
5745 inline Object* unchecked_second();
5746 inline void set_second(String* second,
5747 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5748
5749 // Dispatched behavior.
5750 uint16_t ConsStringGet(int index);
5751
5752 // Casting.
5753 static inline ConsString* cast(Object* obj);
5754
Steve Blocka7e24c12009-10-30 11:49:00 +00005755 // Layout description.
5756 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
5757 static const int kSecondOffset = kFirstOffset + kPointerSize;
5758 static const int kSize = kSecondOffset + kPointerSize;
5759
5760 // Support for StringInputBuffer.
5761 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
5762 unsigned* offset_ptr,
5763 unsigned chars);
5764 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5765 unsigned* offset_ptr,
5766 unsigned chars);
5767
5768 // Minimum length for a cons string.
5769 static const int kMinLength = 13;
5770
Iain Merrick75681382010-08-19 15:07:18 +01005771 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
5772 BodyDescriptor;
5773
Steve Blocka7e24c12009-10-30 11:49:00 +00005774 private:
5775 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
5776};
5777
5778
Steve Blocka7e24c12009-10-30 11:49:00 +00005779// The ExternalString class describes string values that are backed by
5780// a string resource that lies outside the V8 heap. ExternalStrings
5781// consist of the length field common to all strings, a pointer to the
5782// external resource. It is important to ensure (externally) that the
5783// resource is not deallocated while the ExternalString is live in the
5784// V8 heap.
5785//
5786// The API expects that all ExternalStrings are created through the
5787// API. Therefore, ExternalStrings should not be used internally.
5788class ExternalString: public String {
5789 public:
5790 // Casting
5791 static inline ExternalString* cast(Object* obj);
5792
5793 // Layout description.
5794 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
5795 static const int kSize = kResourceOffset + kPointerSize;
5796
5797 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
5798
5799 private:
5800 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
5801};
5802
5803
5804// The ExternalAsciiString class is an external string backed by an
5805// ASCII string.
5806class ExternalAsciiString: public ExternalString {
5807 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005808 static const bool kHasAsciiEncoding = true;
5809
Steve Blocka7e24c12009-10-30 11:49:00 +00005810 typedef v8::String::ExternalAsciiStringResource Resource;
5811
5812 // The underlying resource.
5813 inline Resource* resource();
5814 inline void set_resource(Resource* buffer);
5815
5816 // Dispatched behavior.
5817 uint16_t ExternalAsciiStringGet(int index);
5818
5819 // Casting.
5820 static inline ExternalAsciiString* cast(Object* obj);
5821
Steve Blockd0582a62009-12-15 09:54:21 +00005822 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005823 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
5824
5825 template<typename StaticVisitor>
5826 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00005827
Steve Blocka7e24c12009-10-30 11:49:00 +00005828 // Support for StringInputBuffer.
5829 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
5830 unsigned* offset,
5831 unsigned chars);
5832 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5833 unsigned* offset,
5834 unsigned chars);
5835
Steve Blocka7e24c12009-10-30 11:49:00 +00005836 private:
5837 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
5838};
5839
5840
5841// The ExternalTwoByteString class is an external string backed by a UTF-16
5842// encoded string.
5843class ExternalTwoByteString: public ExternalString {
5844 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005845 static const bool kHasAsciiEncoding = false;
5846
Steve Blocka7e24c12009-10-30 11:49:00 +00005847 typedef v8::String::ExternalStringResource Resource;
5848
5849 // The underlying string resource.
5850 inline Resource* resource();
5851 inline void set_resource(Resource* buffer);
5852
5853 // Dispatched behavior.
5854 uint16_t ExternalTwoByteStringGet(int index);
5855
5856 // For regexp code.
5857 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
5858
5859 // Casting.
5860 static inline ExternalTwoByteString* cast(Object* obj);
5861
Steve Blockd0582a62009-12-15 09:54:21 +00005862 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005863 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
5864
5865 template<typename StaticVisitor>
5866 inline void ExternalTwoByteStringIterateBody();
5867
Steve Blockd0582a62009-12-15 09:54:21 +00005868
Steve Blocka7e24c12009-10-30 11:49:00 +00005869 // Support for StringInputBuffer.
5870 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5871 unsigned* offset_ptr,
5872 unsigned chars);
5873
Steve Blocka7e24c12009-10-30 11:49:00 +00005874 private:
5875 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
5876};
5877
5878
5879// Utility superclass for stack-allocated objects that must be updated
5880// on gc. It provides two ways for the gc to update instances, either
5881// iterating or updating after gc.
5882class Relocatable BASE_EMBEDDED {
5883 public:
Steve Block44f0eee2011-05-26 01:26:41 +01005884 explicit inline Relocatable(Isolate* isolate);
5885 inline virtual ~Relocatable();
Steve Blocka7e24c12009-10-30 11:49:00 +00005886 virtual void IterateInstance(ObjectVisitor* v) { }
5887 virtual void PostGarbageCollection() { }
5888
5889 static void PostGarbageCollectionProcessing();
5890 static int ArchiveSpacePerThread();
5891 static char* ArchiveState(char* to);
5892 static char* RestoreState(char* from);
5893 static void Iterate(ObjectVisitor* v);
5894 static void Iterate(ObjectVisitor* v, Relocatable* top);
5895 static char* Iterate(ObjectVisitor* v, char* t);
5896 private:
Steve Block44f0eee2011-05-26 01:26:41 +01005897 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00005898 Relocatable* prev_;
5899};
5900
5901
5902// A flat string reader provides random access to the contents of a
5903// string independent of the character width of the string. The handle
5904// must be valid as long as the reader is being used.
5905class FlatStringReader : public Relocatable {
5906 public:
Steve Block44f0eee2011-05-26 01:26:41 +01005907 FlatStringReader(Isolate* isolate, Handle<String> str);
5908 FlatStringReader(Isolate* isolate, Vector<const char> input);
Steve Blocka7e24c12009-10-30 11:49:00 +00005909 void PostGarbageCollection();
5910 inline uc32 Get(int index);
5911 int length() { return length_; }
5912 private:
5913 String** str_;
5914 bool is_ascii_;
5915 int length_;
5916 const void* start_;
5917};
5918
5919
5920// Note that StringInputBuffers are not valid across a GC! To fix this
5921// it would have to store a String Handle instead of a String* and
5922// AsciiStringReadBlock would have to be modified to use memcpy.
5923//
5924// StringInputBuffer is able to traverse any string regardless of how
5925// deeply nested a sequence of ConsStrings it is made of. However,
5926// performance will be better if deep strings are flattened before they
5927// are traversed. Since flattening requires memory allocation this is
5928// not always desirable, however (esp. in debugging situations).
5929class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
5930 public:
5931 virtual void Seek(unsigned pos);
5932 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01005933 explicit inline StringInputBuffer(String* backing):
Steve Blocka7e24c12009-10-30 11:49:00 +00005934 unibrow::InputBuffer<String, String*, 1024>(backing) {}
5935};
5936
5937
5938class SafeStringInputBuffer
5939 : public unibrow::InputBuffer<String, String**, 256> {
5940 public:
5941 virtual void Seek(unsigned pos);
5942 inline SafeStringInputBuffer()
5943 : unibrow::InputBuffer<String, String**, 256>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01005944 explicit inline SafeStringInputBuffer(String** backing)
Steve Blocka7e24c12009-10-30 11:49:00 +00005945 : unibrow::InputBuffer<String, String**, 256>(backing) {}
5946};
5947
5948
5949template <typename T>
5950class VectorIterator {
5951 public:
5952 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
5953 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
5954 T GetNext() { return data_[index_++]; }
5955 bool has_more() { return index_ < data_.length(); }
5956 private:
5957 Vector<const T> data_;
5958 int index_;
5959};
5960
5961
5962// The Oddball describes objects null, undefined, true, and false.
5963class Oddball: public HeapObject {
5964 public:
5965 // [to_string]: Cached to_string computed at startup.
5966 DECL_ACCESSORS(to_string, String)
5967
5968 // [to_number]: Cached to_number computed at startup.
5969 DECL_ACCESSORS(to_number, Object)
5970
Steve Block44f0eee2011-05-26 01:26:41 +01005971 inline byte kind();
5972 inline void set_kind(byte kind);
5973
Steve Blocka7e24c12009-10-30 11:49:00 +00005974 // Casting.
5975 static inline Oddball* cast(Object* obj);
5976
5977 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00005978#ifdef DEBUG
5979 void OddballVerify();
5980#endif
5981
5982 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07005983 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
Steve Block44f0eee2011-05-26 01:26:41 +01005984 Object* to_number,
5985 byte kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00005986
5987 // Layout description.
5988 static const int kToStringOffset = HeapObject::kHeaderSize;
5989 static const int kToNumberOffset = kToStringOffset + kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +01005990 static const int kKindOffset = kToNumberOffset + kPointerSize;
5991 static const int kSize = kKindOffset + kPointerSize;
5992
5993 static const byte kFalse = 0;
5994 static const byte kTrue = 1;
5995 static const byte kNotBooleanMask = ~1;
5996 static const byte kTheHole = 2;
5997 static const byte kNull = 3;
5998 static const byte kArgumentMarker = 4;
5999 static const byte kUndefined = 5;
6000 static const byte kOther = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +00006001
Iain Merrick75681382010-08-19 15:07:18 +01006002 typedef FixedBodyDescriptor<kToStringOffset,
6003 kToNumberOffset + kPointerSize,
6004 kSize> BodyDescriptor;
6005
Steve Blocka7e24c12009-10-30 11:49:00 +00006006 private:
6007 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6008};
6009
6010
6011class JSGlobalPropertyCell: public HeapObject {
6012 public:
6013 // [value]: value of the global property.
6014 DECL_ACCESSORS(value, Object)
6015
6016 // Casting.
6017 static inline JSGlobalPropertyCell* cast(Object* obj);
6018
Steve Blocka7e24c12009-10-30 11:49:00 +00006019#ifdef DEBUG
6020 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006021#endif
6022#ifdef OBJECT_PRINT
6023 inline void JSGlobalPropertyCellPrint() {
6024 JSGlobalPropertyCellPrint(stdout);
6025 }
6026 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00006027#endif
6028
6029 // Layout description.
6030 static const int kValueOffset = HeapObject::kHeaderSize;
6031 static const int kSize = kValueOffset + kPointerSize;
6032
Iain Merrick75681382010-08-19 15:07:18 +01006033 typedef FixedBodyDescriptor<kValueOffset,
6034 kValueOffset + kPointerSize,
6035 kSize> BodyDescriptor;
6036
Ben Murdoch8b112d22011-06-08 16:22:53 +01006037 // Returns the isolate/heap this cell object belongs to.
6038 inline Isolate* isolate();
6039 inline Heap* heap();
6040
Steve Blocka7e24c12009-10-30 11:49:00 +00006041 private:
6042 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6043};
6044
6045
6046
6047// Proxy describes objects pointing from JavaScript to C structures.
6048// Since they cannot contain references to JS HeapObjects they can be
6049// placed in old_data_space.
6050class Proxy: public HeapObject {
6051 public:
6052 // [proxy]: field containing the address.
6053 inline Address proxy();
6054 inline void set_proxy(Address value);
6055
6056 // Casting.
6057 static inline Proxy* cast(Object* obj);
6058
6059 // Dispatched behavior.
6060 inline void ProxyIterateBody(ObjectVisitor* v);
Iain Merrick75681382010-08-19 15:07:18 +01006061
6062 template<typename StaticVisitor>
6063 inline void ProxyIterateBody();
6064
Ben Murdochb0fe1622011-05-05 13:52:32 +01006065#ifdef OBJECT_PRINT
6066 inline void ProxyPrint() {
6067 ProxyPrint(stdout);
6068 }
6069 void ProxyPrint(FILE* out);
6070#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006071#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006072 void ProxyVerify();
6073#endif
6074
6075 // Layout description.
6076
6077 static const int kProxyOffset = HeapObject::kHeaderSize;
6078 static const int kSize = kProxyOffset + kPointerSize;
6079
6080 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
6081
6082 private:
6083 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
6084};
6085
6086
6087// The JSArray describes JavaScript Arrays
6088// Such an array can be in one of two modes:
6089// - fast, backing storage is a FixedArray and length <= elements.length();
6090// Please note: push and pop can be used to grow and shrink the array.
6091// - slow, backing storage is a HashTable with numbers as keys.
6092class JSArray: public JSObject {
6093 public:
6094 // [length]: The length property.
6095 DECL_ACCESSORS(length, Object)
6096
Leon Clarke4515c472010-02-03 11:58:03 +00006097 // Overload the length setter to skip write barrier when the length
6098 // is set to a smi. This matches the set function on FixedArray.
6099 inline void set_length(Smi* length);
6100
John Reck59135872010-11-02 12:39:01 -07006101 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6102 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006103
6104 // Initialize the array with the given capacity. The function may
6105 // fail due to out-of-memory situations, but only if the requested
6106 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07006107 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00006108
6109 // Set the content of the array to the content of storage.
6110 inline void SetContent(FixedArray* storage);
6111
6112 // Casting.
6113 static inline JSArray* cast(Object* obj);
6114
6115 // Uses handles. Ensures that the fixed array backing the JSArray has at
6116 // least the stated size.
6117 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
6118
6119 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006120#ifdef OBJECT_PRINT
6121 inline void JSArrayPrint() {
6122 JSArrayPrint(stdout);
6123 }
6124 void JSArrayPrint(FILE* out);
6125#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006126#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006127 void JSArrayVerify();
6128#endif
6129
6130 // Number of element slots to pre-allocate for an empty array.
6131 static const int kPreallocatedArrayElements = 4;
6132
6133 // Layout description.
6134 static const int kLengthOffset = JSObject::kHeaderSize;
6135 static const int kSize = kLengthOffset + kPointerSize;
6136
6137 private:
6138 // Expand the fixed array backing of a fast-case JSArray to at least
6139 // the requested size.
6140 void Expand(int minimum_size_of_backing_fixed_array);
6141
6142 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6143};
6144
6145
Steve Block6ded16b2010-05-10 14:33:55 +01006146// JSRegExpResult is just a JSArray with a specific initial map.
6147// This initial map adds in-object properties for "index" and "input"
6148// properties, as assigned by RegExp.prototype.exec, which allows
6149// faster creation of RegExp exec results.
6150// This class just holds constants used when creating the result.
6151// After creation the result must be treated as a JSArray in all regards.
6152class JSRegExpResult: public JSArray {
6153 public:
6154 // Offsets of object fields.
6155 static const int kIndexOffset = JSArray::kSize;
6156 static const int kInputOffset = kIndexOffset + kPointerSize;
6157 static const int kSize = kInputOffset + kPointerSize;
6158 // Indices of in-object properties.
6159 static const int kIndexIndex = 0;
6160 static const int kInputIndex = 1;
6161 private:
6162 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6163};
6164
6165
Steve Blocka7e24c12009-10-30 11:49:00 +00006166// An accessor must have a getter, but can have no setter.
6167//
6168// When setting a property, V8 searches accessors in prototypes.
6169// If an accessor was found and it does not have a setter,
6170// the request is ignored.
6171//
6172// If the accessor in the prototype has the READ_ONLY property attribute, then
6173// a new value is added to the local object when the property is set.
6174// This shadows the accessor in the prototype.
6175class AccessorInfo: public Struct {
6176 public:
6177 DECL_ACCESSORS(getter, Object)
6178 DECL_ACCESSORS(setter, Object)
6179 DECL_ACCESSORS(data, Object)
6180 DECL_ACCESSORS(name, Object)
6181 DECL_ACCESSORS(flag, Smi)
6182
6183 inline bool all_can_read();
6184 inline void set_all_can_read(bool value);
6185
6186 inline bool all_can_write();
6187 inline void set_all_can_write(bool value);
6188
6189 inline bool prohibits_overwriting();
6190 inline void set_prohibits_overwriting(bool value);
6191
6192 inline PropertyAttributes property_attributes();
6193 inline void set_property_attributes(PropertyAttributes attributes);
6194
6195 static inline AccessorInfo* cast(Object* obj);
6196
Ben Murdochb0fe1622011-05-05 13:52:32 +01006197#ifdef OBJECT_PRINT
6198 inline void AccessorInfoPrint() {
6199 AccessorInfoPrint(stdout);
6200 }
6201 void AccessorInfoPrint(FILE* out);
6202#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006203#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006204 void AccessorInfoVerify();
6205#endif
6206
6207 static const int kGetterOffset = HeapObject::kHeaderSize;
6208 static const int kSetterOffset = kGetterOffset + kPointerSize;
6209 static const int kDataOffset = kSetterOffset + kPointerSize;
6210 static const int kNameOffset = kDataOffset + kPointerSize;
6211 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006212 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006213
6214 private:
6215 // Bit positions in flag.
6216 static const int kAllCanReadBit = 0;
6217 static const int kAllCanWriteBit = 1;
6218 static const int kProhibitsOverwritingBit = 2;
6219 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
6220
6221 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
6222};
6223
6224
6225class AccessCheckInfo: public Struct {
6226 public:
6227 DECL_ACCESSORS(named_callback, Object)
6228 DECL_ACCESSORS(indexed_callback, Object)
6229 DECL_ACCESSORS(data, Object)
6230
6231 static inline AccessCheckInfo* cast(Object* obj);
6232
Ben Murdochb0fe1622011-05-05 13:52:32 +01006233#ifdef OBJECT_PRINT
6234 inline void AccessCheckInfoPrint() {
6235 AccessCheckInfoPrint(stdout);
6236 }
6237 void AccessCheckInfoPrint(FILE* out);
6238#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006239#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006240 void AccessCheckInfoVerify();
6241#endif
6242
6243 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
6244 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
6245 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
6246 static const int kSize = kDataOffset + kPointerSize;
6247
6248 private:
6249 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
6250};
6251
6252
6253class InterceptorInfo: public Struct {
6254 public:
6255 DECL_ACCESSORS(getter, Object)
6256 DECL_ACCESSORS(setter, Object)
6257 DECL_ACCESSORS(query, Object)
6258 DECL_ACCESSORS(deleter, Object)
6259 DECL_ACCESSORS(enumerator, Object)
6260 DECL_ACCESSORS(data, Object)
6261
6262 static inline InterceptorInfo* cast(Object* obj);
6263
Ben Murdochb0fe1622011-05-05 13:52:32 +01006264#ifdef OBJECT_PRINT
6265 inline void InterceptorInfoPrint() {
6266 InterceptorInfoPrint(stdout);
6267 }
6268 void InterceptorInfoPrint(FILE* out);
6269#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006270#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006271 void InterceptorInfoVerify();
6272#endif
6273
6274 static const int kGetterOffset = HeapObject::kHeaderSize;
6275 static const int kSetterOffset = kGetterOffset + kPointerSize;
6276 static const int kQueryOffset = kSetterOffset + kPointerSize;
6277 static const int kDeleterOffset = kQueryOffset + kPointerSize;
6278 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
6279 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
6280 static const int kSize = kDataOffset + kPointerSize;
6281
6282 private:
6283 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
6284};
6285
6286
6287class CallHandlerInfo: public Struct {
6288 public:
6289 DECL_ACCESSORS(callback, Object)
6290 DECL_ACCESSORS(data, Object)
6291
6292 static inline CallHandlerInfo* cast(Object* obj);
6293
Ben Murdochb0fe1622011-05-05 13:52:32 +01006294#ifdef OBJECT_PRINT
6295 inline void CallHandlerInfoPrint() {
6296 CallHandlerInfoPrint(stdout);
6297 }
6298 void CallHandlerInfoPrint(FILE* out);
6299#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006300#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006301 void CallHandlerInfoVerify();
6302#endif
6303
6304 static const int kCallbackOffset = HeapObject::kHeaderSize;
6305 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006306 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006307
6308 private:
6309 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
6310};
6311
6312
6313class TemplateInfo: public Struct {
6314 public:
6315 DECL_ACCESSORS(tag, Object)
6316 DECL_ACCESSORS(property_list, Object)
6317
6318#ifdef DEBUG
6319 void TemplateInfoVerify();
6320#endif
6321
6322 static const int kTagOffset = HeapObject::kHeaderSize;
6323 static const int kPropertyListOffset = kTagOffset + kPointerSize;
6324 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
6325 protected:
6326 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6327 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6328};
6329
6330
6331class FunctionTemplateInfo: public TemplateInfo {
6332 public:
6333 DECL_ACCESSORS(serial_number, Object)
6334 DECL_ACCESSORS(call_code, Object)
6335 DECL_ACCESSORS(property_accessors, Object)
6336 DECL_ACCESSORS(prototype_template, Object)
6337 DECL_ACCESSORS(parent_template, Object)
6338 DECL_ACCESSORS(named_property_handler, Object)
6339 DECL_ACCESSORS(indexed_property_handler, Object)
6340 DECL_ACCESSORS(instance_template, Object)
6341 DECL_ACCESSORS(class_name, Object)
6342 DECL_ACCESSORS(signature, Object)
6343 DECL_ACCESSORS(instance_call_handler, Object)
6344 DECL_ACCESSORS(access_check_info, Object)
6345 DECL_ACCESSORS(flag, Smi)
6346
6347 // Following properties use flag bits.
6348 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6349 DECL_BOOLEAN_ACCESSORS(undetectable)
6350 // If the bit is set, object instances created by this function
6351 // requires access check.
6352 DECL_BOOLEAN_ACCESSORS(needs_access_check)
6353
6354 static inline FunctionTemplateInfo* cast(Object* obj);
6355
Ben Murdochb0fe1622011-05-05 13:52:32 +01006356#ifdef OBJECT_PRINT
6357 inline void FunctionTemplateInfoPrint() {
6358 FunctionTemplateInfoPrint(stdout);
6359 }
6360 void FunctionTemplateInfoPrint(FILE* out);
6361#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006362#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006363 void FunctionTemplateInfoVerify();
6364#endif
6365
6366 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
6367 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
6368 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
6369 static const int kPrototypeTemplateOffset =
6370 kPropertyAccessorsOffset + kPointerSize;
6371 static const int kParentTemplateOffset =
6372 kPrototypeTemplateOffset + kPointerSize;
6373 static const int kNamedPropertyHandlerOffset =
6374 kParentTemplateOffset + kPointerSize;
6375 static const int kIndexedPropertyHandlerOffset =
6376 kNamedPropertyHandlerOffset + kPointerSize;
6377 static const int kInstanceTemplateOffset =
6378 kIndexedPropertyHandlerOffset + kPointerSize;
6379 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
6380 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
6381 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
6382 static const int kAccessCheckInfoOffset =
6383 kInstanceCallHandlerOffset + kPointerSize;
6384 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
6385 static const int kSize = kFlagOffset + kPointerSize;
6386
6387 private:
6388 // Bit position in the flag, from least significant bit position.
6389 static const int kHiddenPrototypeBit = 0;
6390 static const int kUndetectableBit = 1;
6391 static const int kNeedsAccessCheckBit = 2;
6392
6393 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
6394};
6395
6396
6397class ObjectTemplateInfo: public TemplateInfo {
6398 public:
6399 DECL_ACCESSORS(constructor, Object)
6400 DECL_ACCESSORS(internal_field_count, Object)
6401
6402 static inline ObjectTemplateInfo* cast(Object* obj);
6403
Ben Murdochb0fe1622011-05-05 13:52:32 +01006404#ifdef OBJECT_PRINT
6405 inline void ObjectTemplateInfoPrint() {
6406 ObjectTemplateInfoPrint(stdout);
6407 }
6408 void ObjectTemplateInfoPrint(FILE* out);
6409#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006410#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006411 void ObjectTemplateInfoVerify();
6412#endif
6413
6414 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
6415 static const int kInternalFieldCountOffset =
6416 kConstructorOffset + kPointerSize;
6417 static const int kSize = kInternalFieldCountOffset + kPointerSize;
6418};
6419
6420
6421class SignatureInfo: public Struct {
6422 public:
6423 DECL_ACCESSORS(receiver, Object)
6424 DECL_ACCESSORS(args, Object)
6425
6426 static inline SignatureInfo* cast(Object* obj);
6427
Ben Murdochb0fe1622011-05-05 13:52:32 +01006428#ifdef OBJECT_PRINT
6429 inline void SignatureInfoPrint() {
6430 SignatureInfoPrint(stdout);
6431 }
6432 void SignatureInfoPrint(FILE* out);
6433#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006434#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006435 void SignatureInfoVerify();
6436#endif
6437
6438 static const int kReceiverOffset = Struct::kHeaderSize;
6439 static const int kArgsOffset = kReceiverOffset + kPointerSize;
6440 static const int kSize = kArgsOffset + kPointerSize;
6441
6442 private:
6443 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
6444};
6445
6446
6447class TypeSwitchInfo: public Struct {
6448 public:
6449 DECL_ACCESSORS(types, Object)
6450
6451 static inline TypeSwitchInfo* cast(Object* obj);
6452
Ben Murdochb0fe1622011-05-05 13:52:32 +01006453#ifdef OBJECT_PRINT
6454 inline void TypeSwitchInfoPrint() {
6455 TypeSwitchInfoPrint(stdout);
6456 }
6457 void TypeSwitchInfoPrint(FILE* out);
6458#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006459#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006460 void TypeSwitchInfoVerify();
6461#endif
6462
6463 static const int kTypesOffset = Struct::kHeaderSize;
6464 static const int kSize = kTypesOffset + kPointerSize;
6465};
6466
6467
6468#ifdef ENABLE_DEBUGGER_SUPPORT
6469// The DebugInfo class holds additional information for a function being
6470// debugged.
6471class DebugInfo: public Struct {
6472 public:
6473 // The shared function info for the source being debugged.
6474 DECL_ACCESSORS(shared, SharedFunctionInfo)
6475 // Code object for the original code.
6476 DECL_ACCESSORS(original_code, Code)
6477 // Code object for the patched code. This code object is the code object
6478 // currently active for the function.
6479 DECL_ACCESSORS(code, Code)
6480 // Fixed array holding status information for each active break point.
6481 DECL_ACCESSORS(break_points, FixedArray)
6482
6483 // Check if there is a break point at a code position.
6484 bool HasBreakPoint(int code_position);
6485 // Get the break point info object for a code position.
6486 Object* GetBreakPointInfo(int code_position);
6487 // Clear a break point.
6488 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
6489 int code_position,
6490 Handle<Object> break_point_object);
6491 // Set a break point.
6492 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
6493 int source_position, int statement_position,
6494 Handle<Object> break_point_object);
6495 // Get the break point objects for a code position.
6496 Object* GetBreakPointObjects(int code_position);
6497 // Find the break point info holding this break point object.
6498 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
6499 Handle<Object> break_point_object);
6500 // Get the number of break points for this function.
6501 int GetBreakPointCount();
6502
6503 static inline DebugInfo* cast(Object* obj);
6504
Ben Murdochb0fe1622011-05-05 13:52:32 +01006505#ifdef OBJECT_PRINT
6506 inline void DebugInfoPrint() {
6507 DebugInfoPrint(stdout);
6508 }
6509 void DebugInfoPrint(FILE* out);
6510#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006511#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006512 void DebugInfoVerify();
6513#endif
6514
6515 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
6516 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
6517 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
6518 static const int kActiveBreakPointsCountIndex =
6519 kPatchedCodeIndex + kPointerSize;
6520 static const int kBreakPointsStateIndex =
6521 kActiveBreakPointsCountIndex + kPointerSize;
6522 static const int kSize = kBreakPointsStateIndex + kPointerSize;
6523
6524 private:
6525 static const int kNoBreakPointInfo = -1;
6526
6527 // Lookup the index in the break_points array for a code position.
6528 int GetBreakPointInfoIndex(int code_position);
6529
6530 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
6531};
6532
6533
6534// The BreakPointInfo class holds information for break points set in a
6535// function. The DebugInfo object holds a BreakPointInfo object for each code
6536// position with one or more break points.
6537class BreakPointInfo: public Struct {
6538 public:
6539 // The position in the code for the break point.
6540 DECL_ACCESSORS(code_position, Smi)
6541 // The position in the source for the break position.
6542 DECL_ACCESSORS(source_position, Smi)
6543 // The position in the source for the last statement before this break
6544 // position.
6545 DECL_ACCESSORS(statement_position, Smi)
6546 // List of related JavaScript break points.
6547 DECL_ACCESSORS(break_point_objects, Object)
6548
6549 // Removes a break point.
6550 static void ClearBreakPoint(Handle<BreakPointInfo> info,
6551 Handle<Object> break_point_object);
6552 // Set a break point.
6553 static void SetBreakPoint(Handle<BreakPointInfo> info,
6554 Handle<Object> break_point_object);
6555 // Check if break point info has this break point object.
6556 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
6557 Handle<Object> break_point_object);
6558 // Get the number of break points for this code position.
6559 int GetBreakPointCount();
6560
6561 static inline BreakPointInfo* cast(Object* obj);
6562
Ben Murdochb0fe1622011-05-05 13:52:32 +01006563#ifdef OBJECT_PRINT
6564 inline void BreakPointInfoPrint() {
6565 BreakPointInfoPrint(stdout);
6566 }
6567 void BreakPointInfoPrint(FILE* out);
6568#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006569#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006570 void BreakPointInfoVerify();
6571#endif
6572
6573 static const int kCodePositionIndex = Struct::kHeaderSize;
6574 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
6575 static const int kStatementPositionIndex =
6576 kSourcePositionIndex + kPointerSize;
6577 static const int kBreakPointObjectsIndex =
6578 kStatementPositionIndex + kPointerSize;
6579 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
6580
6581 private:
6582 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
6583};
6584#endif // ENABLE_DEBUGGER_SUPPORT
6585
6586
6587#undef DECL_BOOLEAN_ACCESSORS
6588#undef DECL_ACCESSORS
6589
6590
6591// Abstract base class for visiting, and optionally modifying, the
6592// pointers contained in Objects. Used in GC and serialization/deserialization.
6593class ObjectVisitor BASE_EMBEDDED {
6594 public:
6595 virtual ~ObjectVisitor() {}
6596
6597 // Visits a contiguous arrays of pointers in the half-open range
6598 // [start, end). Any or all of the values may be modified on return.
6599 virtual void VisitPointers(Object** start, Object** end) = 0;
6600
6601 // To allow lazy clearing of inline caches the visitor has
6602 // a rich interface for iterating over Code objects..
6603
6604 // Visits a code target in the instruction stream.
6605 virtual void VisitCodeTarget(RelocInfo* rinfo);
6606
Steve Block791712a2010-08-27 10:21:07 +01006607 // Visits a code entry in a JS function.
6608 virtual void VisitCodeEntry(Address entry_address);
6609
Ben Murdochb0fe1622011-05-05 13:52:32 +01006610 // Visits a global property cell reference in the instruction stream.
6611 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
6612
Steve Blocka7e24c12009-10-30 11:49:00 +00006613 // Visits a runtime entry in the instruction stream.
6614 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
6615
Steve Blockd0582a62009-12-15 09:54:21 +00006616 // Visits the resource of an ASCII or two-byte string.
6617 virtual void VisitExternalAsciiString(
6618 v8::String::ExternalAsciiStringResource** resource) {}
6619 virtual void VisitExternalTwoByteString(
6620 v8::String::ExternalStringResource** resource) {}
6621
Steve Blocka7e24c12009-10-30 11:49:00 +00006622 // Visits a debug call target in the instruction stream.
6623 virtual void VisitDebugTarget(RelocInfo* rinfo);
6624
6625 // Handy shorthand for visiting a single pointer.
6626 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
6627
6628 // Visits a contiguous arrays of external references (references to the C++
6629 // heap) in the half-open range [start, end). Any or all of the values
6630 // may be modified on return.
6631 virtual void VisitExternalReferences(Address* start, Address* end) {}
6632
6633 inline void VisitExternalReference(Address* p) {
6634 VisitExternalReferences(p, p + 1);
6635 }
6636
Steve Block44f0eee2011-05-26 01:26:41 +01006637 // Visits a handle that has an embedder-assigned class ID.
6638 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
6639
Steve Blocka7e24c12009-10-30 11:49:00 +00006640#ifdef DEBUG
6641 // Intended for serialization/deserialization checking: insert, or
6642 // check for the presence of, a tag at this position in the stream.
6643 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00006644#else
6645 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00006646#endif
6647};
6648
6649
Iain Merrick75681382010-08-19 15:07:18 +01006650class StructBodyDescriptor : public
6651 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
6652 public:
6653 static inline int SizeOf(Map* map, HeapObject* object) {
6654 return map->instance_size();
6655 }
6656};
6657
6658
Steve Blocka7e24c12009-10-30 11:49:00 +00006659// BooleanBit is a helper class for setting and getting a bit in an
6660// integer or Smi.
6661class BooleanBit : public AllStatic {
6662 public:
6663 static inline bool get(Smi* smi, int bit_position) {
6664 return get(smi->value(), bit_position);
6665 }
6666
6667 static inline bool get(int value, int bit_position) {
6668 return (value & (1 << bit_position)) != 0;
6669 }
6670
6671 static inline Smi* set(Smi* smi, int bit_position, bool v) {
6672 return Smi::FromInt(set(smi->value(), bit_position, v));
6673 }
6674
6675 static inline int set(int value, int bit_position, bool v) {
6676 if (v) {
6677 value |= (1 << bit_position);
6678 } else {
6679 value &= ~(1 << bit_position);
6680 }
6681 return value;
6682 }
6683};
6684
6685} } // namespace v8::internal
6686
6687#endif // V8_OBJECTS_H_