blob: 7f6538cf9c5344e104ad94979bc3602ef6c9b05b [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_OBJECTS_H_
29#define V8_OBJECTS_H_
30
31#include "builtins.h"
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:
44// - Object
45// - Smi (immediate small integer)
46// - Failure (immediate for marking failed operation)
47// - HeapObject (superclass for everything allocated in the heap)
48// - JSObject
49// - JSArray
50// - JSRegExp
51// - JSFunction
52// - GlobalObject
53// - JSGlobalObject
54// - JSBuiltinsObject
55// - JSGlobalProxy
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010056// - JSValue
57// - ByteArray
58// - PixelArray
59// - ExternalArray
60// - ExternalByteArray
61// - ExternalUnsignedByteArray
62// - ExternalShortArray
63// - ExternalUnsignedShortArray
64// - ExternalIntArray
65// - ExternalUnsignedIntArray
66// - ExternalFloatArray
67// - FixedArray
68// - DescriptorArray
69// - HashTable
70// - Dictionary
71// - SymbolTable
72// - CompilationCacheTable
73// - CodeCacheHashTable
74// - MapCache
75// - Context
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010076// - JSFunctionResultCache
Kristian Monsen50ef84f2010-07-29 15:18:00 +010077// - SerializedScopeInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000078// - String
79// - SeqString
80// - SeqAsciiString
81// - SeqTwoByteString
82// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000083// - ExternalString
84// - ExternalAsciiString
85// - ExternalTwoByteString
86// - HeapNumber
87// - Code
88// - Map
89// - Oddball
90// - Proxy
91// - SharedFunctionInfo
92// - Struct
93// - AccessorInfo
94// - AccessCheckInfo
95// - InterceptorInfo
96// - CallHandlerInfo
97// - TemplateInfo
98// - FunctionTemplateInfo
99// - ObjectTemplateInfo
100// - Script
101// - SignatureInfo
102// - TypeSwitchInfo
103// - DebugInfo
104// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100105// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000106//
107// Formats of Object*:
108// Smi: [31 bit signed int] 0
109// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
110// Failure: [30 bit signed int] 11
111
112// Ecma-262 3rd 8.6.1
113enum PropertyAttributes {
114 NONE = v8::None,
115 READ_ONLY = v8::ReadOnly,
116 DONT_ENUM = v8::DontEnum,
117 DONT_DELETE = v8::DontDelete,
118 ABSENT = 16 // Used in runtime to indicate a property is absent.
119 // ABSENT can never be stored in or returned from a descriptor's attributes
120 // bitfield. It is only used as a return value meaning the attributes of
121 // a non-existent property.
122};
123
124namespace v8 {
125namespace internal {
126
127
128// PropertyDetails captures type and attributes for a property.
129// They are used both in property dictionaries and instance descriptors.
130class PropertyDetails BASE_EMBEDDED {
131 public:
132
133 PropertyDetails(PropertyAttributes attributes,
134 PropertyType type,
135 int index = 0) {
136 ASSERT(TypeField::is_valid(type));
137 ASSERT(AttributesField::is_valid(attributes));
138 ASSERT(IndexField::is_valid(index));
139
140 value_ = TypeField::encode(type)
141 | AttributesField::encode(attributes)
142 | IndexField::encode(index);
143
144 ASSERT(type == this->type());
145 ASSERT(attributes == this->attributes());
146 ASSERT(index == this->index());
147 }
148
149 // Conversion for storing details as Object*.
150 inline PropertyDetails(Smi* smi);
151 inline Smi* AsSmi();
152
153 PropertyType type() { return TypeField::decode(value_); }
154
155 bool IsTransition() {
156 PropertyType t = type();
157 ASSERT(t != INTERCEPTOR);
158 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION;
159 }
160
161 bool IsProperty() {
162 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
163 }
164
165 PropertyAttributes attributes() { return AttributesField::decode(value_); }
166
167 int index() { return IndexField::decode(value_); }
168
169 inline PropertyDetails AsDeleted();
170
171 static bool IsValidIndex(int index) { return IndexField::is_valid(index); }
172
173 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
174 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
175 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
176 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
177
178 // Bit fields in value_ (type, shift, size). Must be public so the
179 // constants can be embedded in generated code.
180 class TypeField: public BitField<PropertyType, 0, 3> {};
181 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
182 class DeletedField: public BitField<uint32_t, 6, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000183 class IndexField: public BitField<uint32_t, 7, 32-7> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000184
185 static const int kInitialIndex = 1;
186 private:
187 uint32_t value_;
188};
189
190
191// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
192enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
193
194
195// PropertyNormalizationMode is used to specify whether to keep
196// inobject properties when normalizing properties of a JSObject.
197enum PropertyNormalizationMode {
198 CLEAR_INOBJECT_PROPERTIES,
199 KEEP_INOBJECT_PROPERTIES
200};
201
202
Steve Block791712a2010-08-27 10:21:07 +0100203// Instance size sentinel for objects of variable size.
204static const int kVariableSizeSentinel = 0;
205
206
Steve Blocka7e24c12009-10-30 11:49:00 +0000207// All Maps have a field instance_type containing a InstanceType.
208// It describes the type of the instances.
209//
210// As an example, a JavaScript object is a heap object and its map
211// instance_type is JS_OBJECT_TYPE.
212//
213// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000214// mirror their encoding in the instance_type field of the map. The default
215// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
216// encoding is mentioned explicitly in the name. Likewise, the default
217// representation is considered sequential. It is not mentioned in the
218// name. The other representations (eg, CONS, EXTERNAL) are explicitly
219// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
220// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000221//
222// NOTE: The following things are some that depend on the string types having
223// instance_types that are less than those of all other types:
224// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
225// Object::IsString.
226//
227// NOTE: Everything following JS_VALUE_TYPE is considered a
228// JSObject for GC purposes. The first four entries here have typeof
229// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000230#define INSTANCE_TYPE_LIST_ALL(V) \
231 V(SYMBOL_TYPE) \
232 V(ASCII_SYMBOL_TYPE) \
233 V(CONS_SYMBOL_TYPE) \
234 V(CONS_ASCII_SYMBOL_TYPE) \
235 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100236 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000237 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
238 V(STRING_TYPE) \
239 V(ASCII_STRING_TYPE) \
240 V(CONS_STRING_TYPE) \
241 V(CONS_ASCII_STRING_TYPE) \
242 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100243 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000244 V(EXTERNAL_ASCII_STRING_TYPE) \
245 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
246 \
247 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000248 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000249 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100250 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000251 \
252 V(HEAP_NUMBER_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000253 V(PROXY_TYPE) \
254 V(BYTE_ARRAY_TYPE) \
255 V(PIXEL_ARRAY_TYPE) \
256 /* Note: the order of these external array */ \
257 /* types is relied upon in */ \
258 /* Object::IsExternalArray(). */ \
259 V(EXTERNAL_BYTE_ARRAY_TYPE) \
260 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
261 V(EXTERNAL_SHORT_ARRAY_TYPE) \
262 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
263 V(EXTERNAL_INT_ARRAY_TYPE) \
264 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
265 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
266 V(FILLER_TYPE) \
267 \
268 V(ACCESSOR_INFO_TYPE) \
269 V(ACCESS_CHECK_INFO_TYPE) \
270 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000271 V(CALL_HANDLER_INFO_TYPE) \
272 V(FUNCTION_TEMPLATE_INFO_TYPE) \
273 V(OBJECT_TEMPLATE_INFO_TYPE) \
274 V(SIGNATURE_INFO_TYPE) \
275 V(TYPE_SWITCH_INFO_TYPE) \
276 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100277 V(CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000278 \
Iain Merrick75681382010-08-19 15:07:18 +0100279 V(FIXED_ARRAY_TYPE) \
280 V(SHARED_FUNCTION_INFO_TYPE) \
281 \
Steve Blockd0582a62009-12-15 09:54:21 +0000282 V(JS_VALUE_TYPE) \
283 V(JS_OBJECT_TYPE) \
284 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
285 V(JS_GLOBAL_OBJECT_TYPE) \
286 V(JS_BUILTINS_OBJECT_TYPE) \
287 V(JS_GLOBAL_PROXY_TYPE) \
288 V(JS_ARRAY_TYPE) \
289 V(JS_REGEXP_TYPE) \
290 \
291 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000292
293#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000294#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
295 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000296 V(BREAK_POINT_INFO_TYPE)
297#else
298#define INSTANCE_TYPE_LIST_DEBUGGER(V)
299#endif
300
Steve Blockd0582a62009-12-15 09:54:21 +0000301#define INSTANCE_TYPE_LIST(V) \
302 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000303 INSTANCE_TYPE_LIST_DEBUGGER(V)
304
305
306// Since string types are not consecutive, this macro is used to
307// iterate over them.
308#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000309 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100310 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000311 symbol, \
312 Symbol) \
313 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100314 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000315 ascii_symbol, \
316 AsciiSymbol) \
317 V(CONS_SYMBOL_TYPE, \
318 ConsString::kSize, \
319 cons_symbol, \
320 ConsSymbol) \
321 V(CONS_ASCII_SYMBOL_TYPE, \
322 ConsString::kSize, \
323 cons_ascii_symbol, \
324 ConsAsciiSymbol) \
325 V(EXTERNAL_SYMBOL_TYPE, \
326 ExternalTwoByteString::kSize, \
327 external_symbol, \
328 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100329 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
330 ExternalTwoByteString::kSize, \
331 external_symbol_with_ascii_data, \
332 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000333 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
334 ExternalAsciiString::kSize, \
335 external_ascii_symbol, \
336 ExternalAsciiSymbol) \
337 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100338 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000339 string, \
340 String) \
341 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100342 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000343 ascii_string, \
344 AsciiString) \
345 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000346 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000347 cons_string, \
348 ConsString) \
349 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000350 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000351 cons_ascii_string, \
352 ConsAsciiString) \
353 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000354 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000355 external_string, \
356 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100357 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
358 ExternalTwoByteString::kSize, \
359 external_string_with_ascii_data, \
360 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000361 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000363 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100364 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000365
366// A struct is a simple object a set of object-valued fields. Including an
367// object type in this causes the compiler to generate most of the boilerplate
368// code for the class including allocation and garbage collection routines,
369// casts and predicates. All you need to define is the class, methods and
370// object verification routines. Easy, no?
371//
372// Note that for subtle reasons related to the ordering or numerical values of
373// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
374// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000375#define STRUCT_LIST_ALL(V) \
376 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
377 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
378 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
379 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
380 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
381 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
382 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
383 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100384 V(SCRIPT, Script, script) \
385 V(CODE_CACHE, CodeCache, code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000386
387#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000388#define STRUCT_LIST_DEBUGGER(V) \
389 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000390 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
391#else
392#define STRUCT_LIST_DEBUGGER(V)
393#endif
394
Steve Blockd0582a62009-12-15 09:54:21 +0000395#define STRUCT_LIST(V) \
396 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000397 STRUCT_LIST_DEBUGGER(V)
398
399// We use the full 8 bits of the instance_type field to encode heap object
400// instance types. The high-order bit (bit 7) is set if the object is not a
401// string, and cleared if it is a string.
402const uint32_t kIsNotStringMask = 0x80;
403const uint32_t kStringTag = 0x0;
404const uint32_t kNotStringTag = 0x80;
405
Leon Clarkee46be812010-01-19 14:06:41 +0000406// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
407// There are not enough types that the non-string types (with bit 7 set) can
408// have bit 6 set too.
409const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000410const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000411const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000412
Steve Blocka7e24c12009-10-30 11:49:00 +0000413// If bit 7 is clear then bit 2 indicates whether the string consists of
414// two-byte characters or one-byte characters.
415const uint32_t kStringEncodingMask = 0x4;
416const uint32_t kTwoByteStringTag = 0x0;
417const uint32_t kAsciiStringTag = 0x4;
418
419// If bit 7 is clear, the low-order 2 bits indicate the representation
420// of the string.
421const uint32_t kStringRepresentationMask = 0x03;
422enum StringRepresentationTag {
423 kSeqStringTag = 0x0,
424 kConsStringTag = 0x1,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100425 kExternalStringTag = 0x2
Steve Blocka7e24c12009-10-30 11:49:00 +0000426};
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100427const uint32_t kIsConsStringMask = 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000428
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100429// If bit 7 is clear, then bit 3 indicates whether this two-byte
430// string actually contains ascii data.
431const uint32_t kAsciiDataHintMask = 0x08;
432const uint32_t kAsciiDataHintTag = 0x08;
433
Steve Blocka7e24c12009-10-30 11:49:00 +0000434
435// A ConsString with an empty string as the right side is a candidate
436// for being shortcut by the garbage collector unless it is a
437// symbol. It's not common to have non-flat symbols, so we do not
438// shortcut them thereby avoiding turning symbols into strings. See
439// heap.cc and mark-compact.cc.
440const uint32_t kShortcutTypeMask =
441 kIsNotStringMask |
442 kIsSymbolMask |
443 kStringRepresentationMask;
444const uint32_t kShortcutTypeTag = kConsStringTag;
445
446
447enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000448 // String types.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100449 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000450 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100451 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000452 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100453 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
454 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
455 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000456 EXTERNAL_ASCII_SYMBOL_TYPE =
457 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100458 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000459 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100460 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000461 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100462 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
463 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
464 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000465 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
466 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000467
Leon Clarkee46be812010-01-19 14:06:41 +0000468 // Objects allocated in their own spaces (never in new space).
469 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 CODE_TYPE,
471 ODDBALL_TYPE,
472 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000473
474 // "Data", objects that cannot contain non-map-word pointers to heap
475 // objects.
476 HEAP_NUMBER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000477 PROXY_TYPE,
478 BYTE_ARRAY_TYPE,
479 PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000480 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000481 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
482 EXTERNAL_SHORT_ARRAY_TYPE,
483 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
484 EXTERNAL_INT_ARRAY_TYPE,
485 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000486 EXTERNAL_FLOAT_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
487 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000488
Leon Clarkee46be812010-01-19 14:06:41 +0000489 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 ACCESSOR_INFO_TYPE,
491 ACCESS_CHECK_INFO_TYPE,
492 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000493 CALL_HANDLER_INFO_TYPE,
494 FUNCTION_TEMPLATE_INFO_TYPE,
495 OBJECT_TEMPLATE_INFO_TYPE,
496 SIGNATURE_INFO_TYPE,
497 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000498 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100499 CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100500 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
501 // is defined. However as include/v8.h contain some of the instance type
502 // constants always having them avoids them getting different numbers
503 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 DEBUG_INFO_TYPE,
505 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000506
Leon Clarkee46be812010-01-19 14:06:41 +0000507 FIXED_ARRAY_TYPE,
508 SHARED_FUNCTION_INFO_TYPE,
509
510 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000511 JS_OBJECT_TYPE,
512 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
513 JS_GLOBAL_OBJECT_TYPE,
514 JS_BUILTINS_OBJECT_TYPE,
515 JS_GLOBAL_PROXY_TYPE,
516 JS_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000517 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000518
519 JS_FUNCTION_TYPE,
520
521 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000524 INVALID_TYPE = FIRST_TYPE - 1,
525 FIRST_NONSTRING_TYPE = MAP_TYPE,
526 // Boundaries for testing for an external array.
527 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
528 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_FLOAT_ARRAY_TYPE,
529 // Boundary for promotion to old data space/old pointer space.
530 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 // Boundaries for testing the type is a JavaScript "object". Note that
532 // function objects are not counted as objects, even though they are
533 // implemented as such; only values whose typeof is "object" are included.
534 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
535 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE
536};
537
538
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100539STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
540STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
541STATIC_CHECK(PROXY_TYPE == Internals::kProxyType);
542
543
Steve Blocka7e24c12009-10-30 11:49:00 +0000544enum CompareResult {
545 LESS = -1,
546 EQUAL = 0,
547 GREATER = 1,
548
549 NOT_EQUAL = GREATER
550};
551
552
553#define DECL_BOOLEAN_ACCESSORS(name) \
554 inline bool name(); \
555 inline void set_##name(bool value); \
556
557
558#define DECL_ACCESSORS(name, type) \
559 inline type* name(); \
560 inline void set_##name(type* value, \
561 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
562
563
564class StringStream;
565class ObjectVisitor;
566
567struct ValueInfo : public Malloced {
568 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
569 InstanceType type;
570 Object* ptr;
571 const char* str;
572 double number;
573};
574
575
576// A template-ized version of the IsXXX functions.
577template <class C> static inline bool Is(Object* obj);
578
579
580// Object is the abstract superclass for all classes in the
581// object hierarchy.
582// Object does not use any virtual functions to avoid the
583// allocation of the C++ vtable.
584// Since Smi and Failure are subclasses of Object no
585// data members can be present in Object.
586class Object BASE_EMBEDDED {
587 public:
588 // Type testing.
589 inline bool IsSmi();
590 inline bool IsHeapObject();
591 inline bool IsHeapNumber();
592 inline bool IsString();
593 inline bool IsSymbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 // See objects-inl.h for more details
595 inline bool IsSeqString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000596 inline bool IsExternalString();
597 inline bool IsExternalTwoByteString();
598 inline bool IsExternalAsciiString();
599 inline bool IsSeqTwoByteString();
600 inline bool IsSeqAsciiString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 inline bool IsConsString();
602
603 inline bool IsNumber();
604 inline bool IsByteArray();
605 inline bool IsPixelArray();
Steve Block3ce2e202009-11-05 08:53:23 +0000606 inline bool IsExternalArray();
607 inline bool IsExternalByteArray();
608 inline bool IsExternalUnsignedByteArray();
609 inline bool IsExternalShortArray();
610 inline bool IsExternalUnsignedShortArray();
611 inline bool IsExternalIntArray();
612 inline bool IsExternalUnsignedIntArray();
613 inline bool IsExternalFloatArray();
Steve Blocka7e24c12009-10-30 11:49:00 +0000614 inline bool IsFailure();
615 inline bool IsRetryAfterGC();
616 inline bool IsOutOfMemoryFailure();
617 inline bool IsException();
618 inline bool IsJSObject();
619 inline bool IsJSContextExtensionObject();
620 inline bool IsMap();
621 inline bool IsFixedArray();
622 inline bool IsDescriptorArray();
623 inline bool IsContext();
624 inline bool IsCatchContext();
625 inline bool IsGlobalContext();
626 inline bool IsJSFunction();
627 inline bool IsCode();
628 inline bool IsOddball();
629 inline bool IsSharedFunctionInfo();
630 inline bool IsJSValue();
631 inline bool IsStringWrapper();
632 inline bool IsProxy();
633 inline bool IsBoolean();
634 inline bool IsJSArray();
635 inline bool IsJSRegExp();
636 inline bool IsHashTable();
637 inline bool IsDictionary();
638 inline bool IsSymbolTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100639 inline bool IsJSFunctionResultCache();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100640 inline bool IsNormalizedMapCache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000641 inline bool IsCompilationCacheTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100642 inline bool IsCodeCacheHashTable();
Steve Blocka7e24c12009-10-30 11:49:00 +0000643 inline bool IsMapCache();
644 inline bool IsPrimitive();
645 inline bool IsGlobalObject();
646 inline bool IsJSGlobalObject();
647 inline bool IsJSBuiltinsObject();
648 inline bool IsJSGlobalProxy();
649 inline bool IsUndetectableObject();
650 inline bool IsAccessCheckNeeded();
651 inline bool IsJSGlobalPropertyCell();
652
653 // Returns true if this object is an instance of the specified
654 // function template.
655 inline bool IsInstanceOf(FunctionTemplateInfo* type);
656
657 inline bool IsStruct();
658#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
659 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
660#undef DECLARE_STRUCT_PREDICATE
661
662 // Oddball testing.
663 INLINE(bool IsUndefined());
664 INLINE(bool IsTheHole());
665 INLINE(bool IsNull());
666 INLINE(bool IsTrue());
667 INLINE(bool IsFalse());
668
669 // Extract the number.
670 inline double Number();
671
672 inline bool HasSpecificClassOf(String* name);
673
674 Object* ToObject(); // ECMA-262 9.9.
675 Object* ToBoolean(); // ECMA-262 9.2.
676
677 // Convert to a JSObject if needed.
678 // global_context is used when creating wrapper object.
679 Object* ToObject(Context* global_context);
680
681 // Converts this to a Smi if possible.
682 // Failure is returned otherwise.
683 inline Object* ToSmi();
684
685 void Lookup(String* name, LookupResult* result);
686
687 // Property access.
688 inline Object* GetProperty(String* key);
689 inline Object* GetProperty(String* key, PropertyAttributes* attributes);
690 Object* GetPropertyWithReceiver(Object* receiver,
691 String* key,
692 PropertyAttributes* attributes);
693 Object* GetProperty(Object* receiver,
694 LookupResult* result,
695 String* key,
696 PropertyAttributes* attributes);
697 Object* GetPropertyWithCallback(Object* receiver,
698 Object* structure,
699 String* name,
700 Object* holder);
701 Object* GetPropertyWithDefinedGetter(Object* receiver,
702 JSFunction* getter);
703
704 inline Object* GetElement(uint32_t index);
705 Object* GetElementWithReceiver(Object* receiver, uint32_t index);
706
707 // Return the object's prototype (might be Heap::null_value()).
708 Object* GetPrototype();
709
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100710 // Tries to convert an object to an array index. Returns true and sets
711 // the output parameter if it succeeds.
712 inline bool ToArrayIndex(uint32_t* index);
713
Steve Blocka7e24c12009-10-30 11:49:00 +0000714 // Returns true if this is a JSValue containing a string and the index is
715 // < the length of the string. Used to implement [] on strings.
716 inline bool IsStringObjectWithCharacterAt(uint32_t index);
717
718#ifdef DEBUG
719 // Prints this object with details.
720 void Print();
721 void PrintLn();
722 // Verifies the object.
723 void Verify();
724
725 // Verify a pointer is a valid object pointer.
726 static void VerifyPointer(Object* p);
727#endif
728
729 // Prints this object without details.
730 void ShortPrint();
731
732 // Prints this object without details to a message accumulator.
733 void ShortPrint(StringStream* accumulator);
734
735 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
736 static Object* cast(Object* value) { return value; }
737
738 // Layout description.
739 static const int kHeaderSize = 0; // Object does not take up any space.
740
741 private:
742 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
743};
744
745
746// Smi represents integer Numbers that can be stored in 31 bits.
747// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000748// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000749// For long smis it has the following format:
750// [32 bit signed int] [31 bits zero padding] 0
751// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000752class Smi: public Object {
753 public:
754 // Returns the integer value.
755 inline int value();
756
757 // Convert a value to a Smi object.
758 static inline Smi* FromInt(int value);
759
760 static inline Smi* FromIntptr(intptr_t value);
761
762 // Returns whether value can be represented in a Smi.
763 static inline bool IsValid(intptr_t value);
764
Steve Blocka7e24c12009-10-30 11:49:00 +0000765 // Casting.
766 static inline Smi* cast(Object* object);
767
768 // Dispatched behavior.
769 void SmiPrint();
770 void SmiPrint(StringStream* accumulator);
771#ifdef DEBUG
772 void SmiVerify();
773#endif
774
Steve Block3ce2e202009-11-05 08:53:23 +0000775 static const int kMinValue = (-1 << (kSmiValueSize - 1));
776 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000777
778 private:
779 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
780};
781
782
783// Failure is used for reporting out of memory situations and
784// propagating exceptions through the runtime system. Failure objects
785// are transient and cannot occur as part of the object graph.
786//
787// Failures are a single word, encoded as follows:
788// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000789// |...rrrrrrrrrrrrrrrrrrrrrr|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000790// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000791// 7 6 4 32 10
792//
Steve Blocka7e24c12009-10-30 11:49:00 +0000793//
794// The low two bits, 0-1, are the failure tag, 11. The next two bits,
795// 2-3, are a failure type tag 'tt' with possible values:
796// 00 RETRY_AFTER_GC
797// 01 EXCEPTION
798// 10 INTERNAL_ERROR
799// 11 OUT_OF_MEMORY_EXCEPTION
800//
801// The next three bits, 4-6, are an allocation space tag 'sss'. The
802// allocation space tag is 000 for all failure types except
803// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
804// allocation spaces (the encoding is found in globals.h).
805//
806// The remaining bits is the size of the allocation request in units
807// of the pointer size, and is zeroed except for RETRY_AFTER_GC
808// failures. The 25 bits (on a 32 bit platform) gives a representable
809// range of 2^27 bytes (128MB).
810
811// Failure type tag info.
812const int kFailureTypeTagSize = 2;
813const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
814
815class Failure: public Object {
816 public:
817 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
818 enum Type {
819 RETRY_AFTER_GC = 0,
820 EXCEPTION = 1, // Returning this marker tells the real exception
821 // is in Top::pending_exception.
822 INTERNAL_ERROR = 2,
823 OUT_OF_MEMORY_EXCEPTION = 3
824 };
825
826 inline Type type() const;
827
828 // Returns the space that needs to be collected for RetryAfterGC failures.
829 inline AllocationSpace allocation_space() const;
830
831 // Returns the number of bytes requested (up to the representable maximum)
832 // for RetryAfterGC failures.
833 inline int requested() const;
834
835 inline bool IsInternalError() const;
836 inline bool IsOutOfMemoryException() const;
837
838 static Failure* RetryAfterGC(int requested_bytes, AllocationSpace space);
839 static inline Failure* RetryAfterGC(int requested_bytes); // NEW_SPACE
840 static inline Failure* Exception();
841 static inline Failure* InternalError();
842 static inline Failure* OutOfMemoryException();
843 // Casting.
844 static inline Failure* cast(Object* object);
845
846 // Dispatched behavior.
847 void FailurePrint();
848 void FailurePrint(StringStream* accumulator);
849#ifdef DEBUG
850 void FailureVerify();
851#endif
852
853 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000854 inline intptr_t value() const;
855 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000856
857 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
858};
859
860
861// Heap objects typically have a map pointer in their first word. However,
862// during GC other data (eg, mark bits, forwarding addresses) is sometimes
863// encoded in the first word. The class MapWord is an abstraction of the
864// value in a heap object's first word.
865class MapWord BASE_EMBEDDED {
866 public:
867 // Normal state: the map word contains a map pointer.
868
869 // Create a map word from a map pointer.
870 static inline MapWord FromMap(Map* map);
871
872 // View this map word as a map pointer.
873 inline Map* ToMap();
874
875
876 // Scavenge collection: the map word of live objects in the from space
877 // contains a forwarding address (a heap object pointer in the to space).
878
879 // True if this map word is a forwarding address for a scavenge
880 // collection. Only valid during a scavenge collection (specifically,
881 // when all map words are heap object pointers, ie. not during a full GC).
882 inline bool IsForwardingAddress();
883
884 // Create a map word from a forwarding address.
885 static inline MapWord FromForwardingAddress(HeapObject* object);
886
887 // View this map word as a forwarding address.
888 inline HeapObject* ToForwardingAddress();
889
Steve Blocka7e24c12009-10-30 11:49:00 +0000890 // Marking phase of full collection: the map word of live objects is
891 // marked, and may be marked as overflowed (eg, the object is live, its
892 // children have not been visited, and it does not fit in the marking
893 // stack).
894
895 // True if this map word's mark bit is set.
896 inline bool IsMarked();
897
898 // Return this map word but with its mark bit set.
899 inline void SetMark();
900
901 // Return this map word but with its mark bit cleared.
902 inline void ClearMark();
903
904 // True if this map word's overflow bit is set.
905 inline bool IsOverflowed();
906
907 // Return this map word but with its overflow bit set.
908 inline void SetOverflow();
909
910 // Return this map word but with its overflow bit cleared.
911 inline void ClearOverflow();
912
913
914 // Compacting phase of a full compacting collection: the map word of live
915 // objects contains an encoding of the original map address along with the
916 // forwarding address (represented as an offset from the first live object
917 // in the same page as the (old) object address).
918
919 // Create a map word from a map address and a forwarding address offset.
920 static inline MapWord EncodeAddress(Address map_address, int offset);
921
922 // Return the map address encoded in this map word.
923 inline Address DecodeMapAddress(MapSpace* map_space);
924
925 // Return the forwarding offset encoded in this map word.
926 inline int DecodeOffset();
927
928
929 // During serialization: the map word is used to hold an encoded
930 // address, and possibly a mark bit (set and cleared with SetMark
931 // and ClearMark).
932
933 // Create a map word from an encoded address.
934 static inline MapWord FromEncodedAddress(Address address);
935
936 inline Address ToEncodedAddress();
937
938 // Bits used by the marking phase of the garbage collector.
939 //
940 // The first word of a heap object is normally a map pointer. The last two
941 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
942 // mark an object as live and/or overflowed:
943 // last bit = 0, marked as alive
944 // second bit = 1, overflowed
945 // An object is only marked as overflowed when it is marked as live while
946 // the marking stack is overflowed.
947 static const int kMarkingBit = 0; // marking bit
948 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
949 static const int kOverflowBit = 1; // overflow bit
950 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
951
Leon Clarkee46be812010-01-19 14:06:41 +0000952 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
953 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +0000954 // +-----------------+------------------+-----------------+
955 // |forwarding offset|page offset of map|page index of map|
956 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +0000957 // ^ ^ ^
958 // | | |
959 // | | kMapPageIndexBits
960 // | kMapPageOffsetBits
961 // kForwardingOffsetBits
962 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
963 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
964#ifdef V8_HOST_ARCH_64_BIT
965 static const int kMapPageIndexBits = 16;
966#else
967 // Use all the 32-bits to encode on a 32-bit platform.
968 static const int kMapPageIndexBits =
969 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
970#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000971
972 static const int kMapPageIndexShift = 0;
973 static const int kMapPageOffsetShift =
974 kMapPageIndexShift + kMapPageIndexBits;
975 static const int kForwardingOffsetShift =
976 kMapPageOffsetShift + kMapPageOffsetBits;
977
Leon Clarkee46be812010-01-19 14:06:41 +0000978 // Bit masks covering the different parts the encoding.
979 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +0000981 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000982 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +0000983 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000984 ~(kMapPageIndexMask | kMapPageOffsetMask);
985
986 private:
987 // HeapObject calls the private constructor and directly reads the value.
988 friend class HeapObject;
989
990 explicit MapWord(uintptr_t value) : value_(value) {}
991
992 uintptr_t value_;
993};
994
995
996// HeapObject is the superclass for all classes describing heap allocated
997// objects.
998class HeapObject: public Object {
999 public:
1000 // [map]: Contains a map which contains the object's reflective
1001 // information.
1002 inline Map* map();
1003 inline void set_map(Map* value);
1004
1005 // During garbage collection, the map word of a heap object does not
1006 // necessarily contain a map pointer.
1007 inline MapWord map_word();
1008 inline void set_map_word(MapWord map_word);
1009
1010 // Converts an address to a HeapObject pointer.
1011 static inline HeapObject* FromAddress(Address address);
1012
1013 // Returns the address of this HeapObject.
1014 inline Address address();
1015
1016 // Iterates over pointers contained in the object (including the Map)
1017 void Iterate(ObjectVisitor* v);
1018
1019 // Iterates over all pointers contained in the object except the
1020 // first map pointer. The object type is given in the first
1021 // parameter. This function does not access the map pointer in the
1022 // object, and so is safe to call while the map pointer is modified.
1023 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1024
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 // Returns the heap object's size in bytes
1026 inline int Size();
1027
1028 // Given a heap object's map pointer, returns the heap size in bytes
1029 // Useful when the map pointer field is used for other purposes.
1030 // GC internal.
1031 inline int SizeFromMap(Map* map);
1032
1033 // Support for the marking heap objects during the marking phase of GC.
1034 // True if the object is marked live.
1035 inline bool IsMarked();
1036
1037 // Mutate this object's map pointer to indicate that the object is live.
1038 inline void SetMark();
1039
1040 // Mutate this object's map pointer to remove the indication that the
1041 // object is live (ie, partially restore the map pointer).
1042 inline void ClearMark();
1043
1044 // True if this object is marked as overflowed. Overflowed objects have
1045 // been reached and marked during marking of the heap, but their children
1046 // have not necessarily been marked and they have not been pushed on the
1047 // marking stack.
1048 inline bool IsOverflowed();
1049
1050 // Mutate this object's map pointer to indicate that the object is
1051 // overflowed.
1052 inline void SetOverflow();
1053
1054 // Mutate this object's map pointer to remove the indication that the
1055 // object is overflowed (ie, partially restore the map pointer).
1056 inline void ClearOverflow();
1057
1058 // Returns the field at offset in obj, as a read/write Object* reference.
1059 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001060 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 // during marking GC.
1062 static inline Object** RawField(HeapObject* obj, int offset);
1063
1064 // Casting.
1065 static inline HeapObject* cast(Object* obj);
1066
Leon Clarke4515c472010-02-03 11:58:03 +00001067 // Return the write barrier mode for this. Callers of this function
1068 // must be able to present a reference to an AssertNoAllocation
1069 // object as a sign that they are not going to use this function
1070 // from code that allocates and thus invalidates the returned write
1071 // barrier mode.
1072 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001073
1074 // Dispatched behavior.
1075 void HeapObjectShortPrint(StringStream* accumulator);
1076#ifdef DEBUG
1077 void HeapObjectPrint();
1078 void HeapObjectVerify();
1079 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001080 inline void VerifySmiField(int offset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001081
1082 void PrintHeader(const char* id);
1083
1084 // Verify a pointer is a valid HeapObject pointer that points to object
1085 // areas in the heap.
1086 static void VerifyHeapPointer(Object* p);
1087#endif
1088
1089 // Layout description.
1090 // First field in a heap object is map.
1091 static const int kMapOffset = Object::kHeaderSize;
1092 static const int kHeaderSize = kMapOffset + kPointerSize;
1093
1094 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1095
1096 protected:
1097 // helpers for calling an ObjectVisitor to iterate over pointers in the
1098 // half-open range [start, end) specified as integer offsets
1099 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1100 // as above, for the single element at "offset"
1101 inline void IteratePointer(ObjectVisitor* v, int offset);
1102
Steve Blocka7e24c12009-10-30 11:49:00 +00001103 private:
1104 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1105};
1106
1107
Iain Merrick75681382010-08-19 15:07:18 +01001108#define SLOT_ADDR(obj, offset) \
1109 reinterpret_cast<Object**>((obj)->address() + offset)
1110
1111// This class describes a body of an object of a fixed size
1112// in which all pointer fields are located in the [start_offset, end_offset)
1113// interval.
1114template<int start_offset, int end_offset, int size>
1115class FixedBodyDescriptor {
1116 public:
1117 static const int kStartOffset = start_offset;
1118 static const int kEndOffset = end_offset;
1119 static const int kSize = size;
1120
1121 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1122
1123 template<typename StaticVisitor>
1124 static inline void IterateBody(HeapObject* obj) {
1125 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1126 SLOT_ADDR(obj, end_offset));
1127 }
1128};
1129
1130
1131// This class describes a body of an object of a variable size
1132// in which all pointer fields are located in the [start_offset, object_size)
1133// interval.
1134template<int start_offset>
1135class FlexibleBodyDescriptor {
1136 public:
1137 static const int kStartOffset = start_offset;
1138
1139 static inline void IterateBody(HeapObject* obj,
1140 int object_size,
1141 ObjectVisitor* v);
1142
1143 template<typename StaticVisitor>
1144 static inline void IterateBody(HeapObject* obj, int object_size) {
1145 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1146 SLOT_ADDR(obj, object_size));
1147 }
1148};
1149
1150#undef SLOT_ADDR
1151
1152
Steve Blocka7e24c12009-10-30 11:49:00 +00001153// The HeapNumber class describes heap allocated numbers that cannot be
1154// represented in a Smi (small integer)
1155class HeapNumber: public HeapObject {
1156 public:
1157 // [value]: number value.
1158 inline double value();
1159 inline void set_value(double value);
1160
1161 // Casting.
1162 static inline HeapNumber* cast(Object* obj);
1163
1164 // Dispatched behavior.
1165 Object* HeapNumberToBoolean();
1166 void HeapNumberPrint();
1167 void HeapNumberPrint(StringStream* accumulator);
1168#ifdef DEBUG
1169 void HeapNumberVerify();
1170#endif
1171
Steve Block6ded16b2010-05-10 14:33:55 +01001172 inline int get_exponent();
1173 inline int get_sign();
1174
Steve Blocka7e24c12009-10-30 11:49:00 +00001175 // Layout description.
1176 static const int kValueOffset = HeapObject::kHeaderSize;
1177 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1178 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1179 // little endian apart from non-EABI arm which is little endian with big
1180 // endian floating point word ordering!
Steve Block3ce2e202009-11-05 08:53:23 +00001181#if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI)
Steve Blocka7e24c12009-10-30 11:49:00 +00001182 static const int kMantissaOffset = kValueOffset;
1183 static const int kExponentOffset = kValueOffset + 4;
1184#else
1185 static const int kMantissaOffset = kValueOffset + 4;
1186 static const int kExponentOffset = kValueOffset;
1187# define BIG_ENDIAN_FLOATING_POINT 1
1188#endif
1189 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001190 static const uint32_t kSignMask = 0x80000000u;
1191 static const uint32_t kExponentMask = 0x7ff00000u;
1192 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001193 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001194 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001195 static const int kExponentBias = 1023;
1196 static const int kExponentShift = 20;
1197 static const int kMantissaBitsInTopWord = 20;
1198 static const int kNonMantissaBitsInTopWord = 12;
1199
1200 private:
1201 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1202};
1203
1204
1205// The JSObject describes real heap allocated JavaScript objects with
1206// properties.
1207// Note that the map of JSObject changes during execution to enable inline
1208// caching.
1209class JSObject: public HeapObject {
1210 public:
1211 enum DeleteMode { NORMAL_DELETION, FORCE_DELETION };
1212 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001213 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001214 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001215 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001217 PIXEL_ELEMENTS,
1218 EXTERNAL_BYTE_ELEMENTS,
1219 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1220 EXTERNAL_SHORT_ELEMENTS,
1221 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1222 EXTERNAL_INT_ELEMENTS,
1223 EXTERNAL_UNSIGNED_INT_ELEMENTS,
1224 EXTERNAL_FLOAT_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001225 };
1226
1227 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001228 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001229 // slow case.
1230 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1231 inline void initialize_properties();
1232 inline bool HasFastProperties();
1233 inline StringDictionary* property_dictionary(); // Gets slow properties.
1234
1235 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001236 //
1237 // Elements can be in two general modes: fast and slow. Each mode
1238 // corrensponds to a set of object representations of elements that
1239 // have something in common.
1240 //
1241 // In the fast mode elements is a FixedArray and so each element can
1242 // be quickly accessed. This fact is used in the generated code. The
1243 // elements array can have one of the two maps in this mode:
1244 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1245 // arrays). In the latter case the elements array may be shared by a
1246 // few objects and so before writing to any element the array must
1247 // be copied. Use EnsureWritableFastElements in this case.
1248 //
1249 // In the slow mode elements is either a NumberDictionary or a
1250 // PixelArray or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001251 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001252 inline void initialize_elements();
Steve Block8defd9f2010-07-08 12:39:36 +01001253 inline Object* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001254 inline ElementsKind GetElementsKind();
1255 inline bool HasFastElements();
1256 inline bool HasDictionaryElements();
1257 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001258 inline bool HasExternalArrayElements();
1259 inline bool HasExternalByteElements();
1260 inline bool HasExternalUnsignedByteElements();
1261 inline bool HasExternalShortElements();
1262 inline bool HasExternalUnsignedShortElements();
1263 inline bool HasExternalIntElements();
1264 inline bool HasExternalUnsignedIntElements();
1265 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001266 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001267 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001268 // Requires: this->HasFastElements().
1269 inline Object* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001270
1271 // Collects elements starting at index 0.
1272 // Undefined values are placed after non-undefined values.
1273 // Returns the number of non-undefined values.
1274 Object* PrepareElementsForSort(uint32_t limit);
1275 // As PrepareElementsForSort, but only on objects where elements is
1276 // a dictionary, and it will stay a dictionary.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001277 MUST_USE_RESULT Object* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001278
1279 Object* SetProperty(String* key,
1280 Object* value,
1281 PropertyAttributes attributes);
1282 Object* SetProperty(LookupResult* result,
1283 String* key,
1284 Object* value,
1285 PropertyAttributes attributes);
1286 Object* SetPropertyWithFailedAccessCheck(LookupResult* result,
1287 String* name,
1288 Object* value);
1289 Object* SetPropertyWithCallback(Object* structure,
1290 String* name,
1291 Object* value,
1292 JSObject* holder);
1293 Object* SetPropertyWithDefinedSetter(JSFunction* setter,
1294 Object* value);
1295 Object* SetPropertyWithInterceptor(String* name,
1296 Object* value,
1297 PropertyAttributes attributes);
1298 Object* SetPropertyPostInterceptor(String* name,
1299 Object* value,
1300 PropertyAttributes attributes);
1301 Object* IgnoreAttributesAndSetLocalProperty(String* key,
1302 Object* value,
1303 PropertyAttributes attributes);
1304
1305 // Retrieve a value in a normalized object given a lookup result.
1306 // Handles the special representation of JS global objects.
1307 Object* GetNormalizedProperty(LookupResult* result);
1308
1309 // Sets the property value in a normalized object given a lookup result.
1310 // Handles the special representation of JS global objects.
1311 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1312
1313 // Sets the property value in a normalized object given (key, value, details).
1314 // Handles the special representation of JS global objects.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001315 MUST_USE_RESULT Object* SetNormalizedProperty(String* name,
1316 Object* value,
1317 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001318
1319 // Deletes the named property in a normalized object.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001320 MUST_USE_RESULT Object* DeleteNormalizedProperty(String* name,
1321 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001322
Steve Blocka7e24c12009-10-30 11:49:00 +00001323 // Returns the class name ([[Class]] property in the specification).
1324 String* class_name();
1325
1326 // Returns the constructor name (the name (possibly, inferred name) of the
1327 // function that was used to instantiate the object).
1328 String* constructor_name();
1329
1330 // Retrieve interceptors.
1331 InterceptorInfo* GetNamedInterceptor();
1332 InterceptorInfo* GetIndexedInterceptor();
1333
1334 inline PropertyAttributes GetPropertyAttribute(String* name);
1335 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1336 String* name);
1337 PropertyAttributes GetLocalPropertyAttribute(String* name);
1338
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001339 MUST_USE_RESULT Object* DefineAccessor(String* name,
1340 bool is_getter,
1341 JSFunction* fun,
1342 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001343 Object* LookupAccessor(String* name, bool is_getter);
1344
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001345 MUST_USE_RESULT Object* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001346
Steve Blocka7e24c12009-10-30 11:49:00 +00001347 // Used from Object::GetProperty().
1348 Object* GetPropertyWithFailedAccessCheck(Object* receiver,
1349 LookupResult* result,
1350 String* name,
1351 PropertyAttributes* attributes);
1352 Object* GetPropertyWithInterceptor(JSObject* receiver,
1353 String* name,
1354 PropertyAttributes* attributes);
1355 Object* GetPropertyPostInterceptor(JSObject* receiver,
1356 String* name,
1357 PropertyAttributes* attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001358 Object* GetLocalPropertyPostInterceptor(JSObject* receiver,
1359 String* name,
1360 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001361
1362 // Returns true if this is an instance of an api function and has
1363 // been modified since it was created. May give false positives.
1364 bool IsDirty();
1365
1366 bool HasProperty(String* name) {
1367 return GetPropertyAttribute(name) != ABSENT;
1368 }
1369
1370 // Can cause a GC if it hits an interceptor.
1371 bool HasLocalProperty(String* name) {
1372 return GetLocalPropertyAttribute(name) != ABSENT;
1373 }
1374
Steve Blockd0582a62009-12-15 09:54:21 +00001375 // If the receiver is a JSGlobalProxy this method will return its prototype,
1376 // otherwise the result is the receiver itself.
1377 inline Object* BypassGlobalProxy();
1378
1379 // Accessors for hidden properties object.
1380 //
1381 // Hidden properties are not local properties of the object itself.
1382 // Instead they are stored on an auxiliary JSObject stored as a local
1383 // property with a special name Heap::hidden_symbol(). But if the
1384 // receiver is a JSGlobalProxy then the auxiliary object is a property
1385 // of its prototype.
1386 //
1387 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1388 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1389 // holder.
1390 //
1391 // These accessors do not touch interceptors or accessors.
1392 inline bool HasHiddenPropertiesObject();
1393 inline Object* GetHiddenPropertiesObject();
1394 inline Object* SetHiddenPropertiesObject(Object* hidden_obj);
1395
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001396 MUST_USE_RESULT Object* DeleteProperty(String* name, DeleteMode mode);
1397 MUST_USE_RESULT Object* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001398
1399 // Tests for the fast common case for property enumeration.
1400 bool IsSimpleEnum();
1401
1402 // Do we want to keep the elements in fast case when increasing the
1403 // capacity?
1404 bool ShouldConvertToSlowElements(int new_capacity);
1405 // Returns true if the backing storage for the slow-case elements of
1406 // this object takes up nearly as much space as a fast-case backing
1407 // storage would. In that case the JSObject should have fast
1408 // elements.
1409 bool ShouldConvertToFastElements();
1410
1411 // Return the object's prototype (might be Heap::null_value()).
1412 inline Object* GetPrototype();
1413
Andrei Popescu402d9372010-02-26 13:31:12 +00001414 // Set the object's prototype (only JSObject and null are allowed).
1415 Object* SetPrototype(Object* value, bool skip_hidden_prototypes);
1416
Steve Blocka7e24c12009-10-30 11:49:00 +00001417 // Tells whether the index'th element is present.
1418 inline bool HasElement(uint32_t index);
1419 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
1420 bool HasLocalElement(uint32_t index);
1421
1422 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1423 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1424
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001425 MUST_USE_RESULT Object* SetFastElement(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001426
1427 // Set the index'th array element.
1428 // A Failure object is returned if GC is needed.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001429 MUST_USE_RESULT Object* SetElement(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001430
1431 // Returns the index'th element.
1432 // The undefined object if index is out of bounds.
1433 Object* GetElementWithReceiver(JSObject* receiver, uint32_t index);
Steve Block8defd9f2010-07-08 12:39:36 +01001434 Object* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001435
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001436 MUST_USE_RESULT Object* SetFastElementsCapacityAndLength(int capacity,
1437 int length);
1438 MUST_USE_RESULT Object* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001439
1440 // Lookup interceptors are used for handling properties controlled by host
1441 // objects.
1442 inline bool HasNamedInterceptor();
1443 inline bool HasIndexedInterceptor();
1444
1445 // Support functions for v8 api (needed for correct interceptor behavior).
1446 bool HasRealNamedProperty(String* key);
1447 bool HasRealElementProperty(uint32_t index);
1448 bool HasRealNamedCallbackProperty(String* key);
1449
1450 // Initializes the array to a certain length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001451 MUST_USE_RESULT Object* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001452
1453 // Get the header size for a JSObject. Used to compute the index of
1454 // internal fields as well as the number of internal fields.
1455 inline int GetHeaderSize();
1456
1457 inline int GetInternalFieldCount();
1458 inline Object* GetInternalField(int index);
1459 inline void SetInternalField(int index, Object* value);
1460
1461 // Lookup a property. If found, the result is valid and has
1462 // detailed information.
1463 void LocalLookup(String* name, LookupResult* result);
1464 void Lookup(String* name, LookupResult* result);
1465
1466 // The following lookup functions skip interceptors.
1467 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1468 void LookupRealNamedProperty(String* name, LookupResult* result);
1469 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1470 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Leon Clarkef7060e22010-06-03 12:02:55 +01001471 bool SetElementWithCallbackSetterInPrototypes(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001472 void LookupCallback(String* name, LookupResult* result);
1473
1474 // Returns the number of properties on this object filtering out properties
1475 // with the specified attributes (ignoring interceptors).
1476 int NumberOfLocalProperties(PropertyAttributes filter);
1477 // Returns the number of enumerable properties (ignoring interceptors).
1478 int NumberOfEnumProperties();
1479 // Fill in details for properties into storage starting at the specified
1480 // index.
1481 void GetLocalPropertyNames(FixedArray* storage, int index);
1482
1483 // Returns the number of properties on this object filtering out properties
1484 // with the specified attributes (ignoring interceptors).
1485 int NumberOfLocalElements(PropertyAttributes filter);
1486 // Returns the number of enumerable elements (ignoring interceptors).
1487 int NumberOfEnumElements();
1488 // Returns the number of elements on this object filtering out elements
1489 // with the specified attributes (ignoring interceptors).
1490 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1491 // Count and fill in the enumerable elements into storage.
1492 // (storage->length() == NumberOfEnumElements()).
1493 // If storage is NULL, will count the elements without adding
1494 // them to any storage.
1495 // Returns the number of enumerable elements.
1496 int GetEnumElementKeys(FixedArray* storage);
1497
1498 // Add a property to a fast-case object using a map transition to
1499 // new_map.
1500 Object* AddFastPropertyUsingMap(Map* new_map,
1501 String* name,
1502 Object* value);
1503
1504 // Add a constant function property to a fast-case object.
1505 // This leaves a CONSTANT_TRANSITION in the old map, and
1506 // if it is called on a second object with this map, a
1507 // normal property is added instead, with a map transition.
1508 // This avoids the creation of many maps with the same constant
1509 // function, all orphaned.
1510 Object* AddConstantFunctionProperty(String* name,
1511 JSFunction* function,
1512 PropertyAttributes attributes);
1513
1514 Object* ReplaceSlowProperty(String* name,
1515 Object* value,
1516 PropertyAttributes attributes);
1517
1518 // Converts a descriptor of any other type to a real field,
1519 // backed by the properties array. Descriptors of visible
1520 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1521 // Converts the descriptor on the original object's map to a
1522 // map transition, and the the new field is on the object's new map.
1523 Object* ConvertDescriptorToFieldAndMapTransition(
1524 String* name,
1525 Object* new_value,
1526 PropertyAttributes attributes);
1527
1528 // Converts a descriptor of any other type to a real field,
1529 // backed by the properties array. Descriptors of visible
1530 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1531 Object* ConvertDescriptorToField(String* name,
1532 Object* new_value,
1533 PropertyAttributes attributes);
1534
1535 // Add a property to a fast-case object.
1536 Object* AddFastProperty(String* name,
1537 Object* value,
1538 PropertyAttributes attributes);
1539
1540 // Add a property to a slow-case object.
1541 Object* AddSlowProperty(String* name,
1542 Object* value,
1543 PropertyAttributes attributes);
1544
1545 // Add a property to an object.
1546 Object* AddProperty(String* name,
1547 Object* value,
1548 PropertyAttributes attributes);
1549
1550 // Convert the object to use the canonical dictionary
1551 // representation. If the object is expected to have additional properties
1552 // added this number can be indicated to have the backing store allocated to
1553 // an initial capacity for holding these properties.
1554 Object* NormalizeProperties(PropertyNormalizationMode mode,
1555 int expected_additional_properties);
1556 Object* NormalizeElements();
1557
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001558 Object* UpdateMapCodeCache(String* name, Code* code);
1559
Steve Blocka7e24c12009-10-30 11:49:00 +00001560 // Transform slow named properties to fast variants.
1561 // Returns failure if allocation failed.
1562 Object* TransformToFastProperties(int unused_property_fields);
1563
1564 // Access fast-case object properties at index.
1565 inline Object* FastPropertyAt(int index);
1566 inline Object* FastPropertyAtPut(int index, Object* value);
1567
1568 // Access to in object properties.
1569 inline Object* InObjectPropertyAt(int index);
1570 inline Object* InObjectPropertyAtPut(int index,
1571 Object* value,
1572 WriteBarrierMode mode
1573 = UPDATE_WRITE_BARRIER);
1574
1575 // initializes the body after properties slot, properties slot is
1576 // initialized by set_properties
1577 // Note: this call does not update write barrier, it is caller's
1578 // reponsibility to ensure that *v* can be collected without WB here.
1579 inline void InitializeBody(int object_size);
1580
1581 // Check whether this object references another object
1582 bool ReferencesObject(Object* obj);
1583
1584 // Casting.
1585 static inline JSObject* cast(Object* obj);
1586
Steve Block8defd9f2010-07-08 12:39:36 +01001587 // Disalow further properties to be added to the object.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001588 MUST_USE_RESULT Object* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001589
1590
Steve Blocka7e24c12009-10-30 11:49:00 +00001591 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001592 void JSObjectShortPrint(StringStream* accumulator);
1593#ifdef DEBUG
1594 void JSObjectPrint();
1595 void JSObjectVerify();
1596 void PrintProperties();
1597 void PrintElements();
1598
1599 // Structure for collecting spill information about JSObjects.
1600 class SpillInformation {
1601 public:
1602 void Clear();
1603 void Print();
1604 int number_of_objects_;
1605 int number_of_objects_with_fast_properties_;
1606 int number_of_objects_with_fast_elements_;
1607 int number_of_fast_used_fields_;
1608 int number_of_fast_unused_fields_;
1609 int number_of_slow_used_properties_;
1610 int number_of_slow_unused_properties_;
1611 int number_of_fast_used_elements_;
1612 int number_of_fast_unused_elements_;
1613 int number_of_slow_used_elements_;
1614 int number_of_slow_unused_elements_;
1615 };
1616
1617 void IncrementSpillStatistics(SpillInformation* info);
1618#endif
1619 Object* SlowReverseLookup(Object* value);
1620
Steve Block8defd9f2010-07-08 12:39:36 +01001621 // Maximal number of fast properties for the JSObject. Used to
1622 // restrict the number of map transitions to avoid an explosion in
1623 // the number of maps for objects used as dictionaries.
1624 inline int MaxFastProperties();
1625
Leon Clarkee46be812010-01-19 14:06:41 +00001626 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1627 // Also maximal value of JSArray's length property.
1628 static const uint32_t kMaxElementCount = 0xffffffffu;
1629
Steve Blocka7e24c12009-10-30 11:49:00 +00001630 static const uint32_t kMaxGap = 1024;
1631 static const int kMaxFastElementsLength = 5000;
1632 static const int kInitialMaxFastElementArray = 100000;
1633 static const int kMaxFastProperties = 8;
1634 static const int kMaxInstanceSize = 255 * kPointerSize;
1635 // When extending the backing storage for property values, we increase
1636 // its size by more than the 1 entry necessary, so sequentially adding fields
1637 // to the same object requires fewer allocations and copies.
1638 static const int kFieldsAdded = 3;
1639
1640 // Layout description.
1641 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1642 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1643 static const int kHeaderSize = kElementsOffset + kPointerSize;
1644
1645 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1646
Iain Merrick75681382010-08-19 15:07:18 +01001647 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1648 public:
1649 static inline int SizeOf(Map* map, HeapObject* object);
1650 };
1651
Steve Blocka7e24c12009-10-30 11:49:00 +00001652 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01001653 Object* GetElementWithCallback(Object* receiver,
1654 Object* structure,
1655 uint32_t index,
1656 Object* holder);
1657 Object* SetElementWithCallback(Object* structure,
1658 uint32_t index,
1659 Object* value,
1660 JSObject* holder);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001661 MUST_USE_RESULT Object* SetElementWithInterceptor(uint32_t index,
1662 Object* value);
1663 MUST_USE_RESULT Object* SetElementWithoutInterceptor(uint32_t index,
1664 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001665
1666 Object* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
1667
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001668 MUST_USE_RESULT Object* DeletePropertyPostInterceptor(String* name,
1669 DeleteMode mode);
1670 MUST_USE_RESULT Object* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001671
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001672 MUST_USE_RESULT Object* DeleteElementPostInterceptor(uint32_t index,
1673 DeleteMode mode);
1674 MUST_USE_RESULT Object* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001675
1676 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1677 String* name,
1678 bool continue_search);
1679 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1680 String* name,
1681 bool continue_search);
1682 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1683 Object* receiver,
1684 LookupResult* result,
1685 String* name,
1686 bool continue_search);
1687 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1688 LookupResult* result,
1689 String* name,
1690 bool continue_search);
1691
1692 // Returns true if most of the elements backing storage is used.
1693 bool HasDenseElements();
1694
Leon Clarkef7060e22010-06-03 12:02:55 +01001695 bool CanSetCallback(String* name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001696 MUST_USE_RESULT Object* SetElementCallback(uint32_t index,
1697 Object* structure,
1698 PropertyAttributes attributes);
1699 MUST_USE_RESULT Object* SetPropertyCallback(String* name,
1700 Object* structure,
1701 PropertyAttributes attributes);
1702 MUST_USE_RESULT Object* DefineGetterSetter(String* name,
1703 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001704
1705 void LookupInDescriptor(String* name, LookupResult* result);
1706
1707 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1708};
1709
1710
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001711// FixedArray describes fixed-sized arrays with element type Object*.
1712class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001713 public:
1714 // [length]: length of the array.
1715 inline int length();
1716 inline void set_length(int value);
1717
Steve Blocka7e24c12009-10-30 11:49:00 +00001718 // Setter and getter for elements.
1719 inline Object* get(int index);
1720 // Setter that uses write barrier.
1721 inline void set(int index, Object* value);
1722
1723 // Setter that doesn't need write barrier).
1724 inline void set(int index, Smi* value);
1725 // Setter with explicit barrier mode.
1726 inline void set(int index, Object* value, WriteBarrierMode mode);
1727
1728 // Setters for frequently used oddballs located in old space.
1729 inline void set_undefined(int index);
1730 inline void set_null(int index);
1731 inline void set_the_hole(int index);
1732
Iain Merrick75681382010-08-19 15:07:18 +01001733 // Setters with less debug checks for the GC to use.
1734 inline void set_unchecked(int index, Smi* value);
1735 inline void set_null_unchecked(int index);
1736
Steve Block6ded16b2010-05-10 14:33:55 +01001737 // Gives access to raw memory which stores the array's data.
1738 inline Object** data_start();
1739
Steve Blocka7e24c12009-10-30 11:49:00 +00001740 // Copy operations.
1741 inline Object* Copy();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001742 MUST_USE_RESULT Object* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001743
1744 // Add the elements of a JSArray to this FixedArray.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001745 MUST_USE_RESULT Object* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00001746
1747 // Compute the union of this and other.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001748 MUST_USE_RESULT Object* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00001749
1750 // Copy a sub array from the receiver to dest.
1751 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1752
1753 // Garbage collection support.
1754 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1755
1756 // Code Generation support.
1757 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1758
1759 // Casting.
1760 static inline FixedArray* cast(Object* obj);
1761
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001762 // Layout description.
1763 // Length is smi tagged when it is stored.
1764 static const int kLengthOffset = HeapObject::kHeaderSize;
1765 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001766
1767 // Maximal allowed size, in bytes, of a single FixedArray.
1768 // Prevents overflowing size computations, as well as extreme memory
1769 // consumption.
1770 static const int kMaxSize = 512 * MB;
1771 // Maximally allowed length of a FixedArray.
1772 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001773
1774 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001775#ifdef DEBUG
1776 void FixedArrayPrint();
1777 void FixedArrayVerify();
1778 // Checks if two FixedArrays have identical contents.
1779 bool IsEqualTo(FixedArray* other);
1780#endif
1781
1782 // Swap two elements in a pair of arrays. If this array and the
1783 // numbers array are the same object, the elements are only swapped
1784 // once.
1785 void SwapPairs(FixedArray* numbers, int i, int j);
1786
1787 // Sort prefix of this array and the numbers array as pairs wrt. the
1788 // numbers. If the numbers array and the this array are the same
1789 // object, the prefix of this array is sorted.
1790 void SortPairs(FixedArray* numbers, uint32_t len);
1791
Iain Merrick75681382010-08-19 15:07:18 +01001792 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
1793 public:
1794 static inline int SizeOf(Map* map, HeapObject* object) {
1795 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
1796 }
1797 };
1798
Steve Blocka7e24c12009-10-30 11:49:00 +00001799 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001800 // Set operation on FixedArray without using write barriers. Can
1801 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001802 static inline void fast_set(FixedArray* array, int index, Object* value);
1803
1804 private:
1805 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1806};
1807
1808
1809// DescriptorArrays are fixed arrays used to hold instance descriptors.
1810// The format of the these objects is:
1811// [0]: point to a fixed array with (value, detail) pairs.
1812// [1]: next enumeration index (Smi), or pointer to small fixed array:
1813// [0]: next enumeration index (Smi)
1814// [1]: pointer to fixed array with enum cache
1815// [2]: first key
1816// [length() - 1]: last key
1817//
1818class DescriptorArray: public FixedArray {
1819 public:
1820 // Is this the singleton empty_descriptor_array?
1821 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001822
Steve Blocka7e24c12009-10-30 11:49:00 +00001823 // Returns the number of descriptors in the array.
1824 int number_of_descriptors() {
1825 return IsEmpty() ? 0 : length() - kFirstIndex;
1826 }
1827
1828 int NextEnumerationIndex() {
1829 if (IsEmpty()) return PropertyDetails::kInitialIndex;
1830 Object* obj = get(kEnumerationIndexIndex);
1831 if (obj->IsSmi()) {
1832 return Smi::cast(obj)->value();
1833 } else {
1834 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1835 return Smi::cast(index)->value();
1836 }
1837 }
1838
1839 // Set next enumeration index and flush any enum cache.
1840 void SetNextEnumerationIndex(int value) {
1841 if (!IsEmpty()) {
1842 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1843 }
1844 }
1845 bool HasEnumCache() {
1846 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
1847 }
1848
1849 Object* GetEnumCache() {
1850 ASSERT(HasEnumCache());
1851 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1852 return bridge->get(kEnumCacheBridgeCacheIndex);
1853 }
1854
1855 // Initialize or change the enum cache,
1856 // using the supplied storage for the small "bridge".
1857 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1858
1859 // Accessors for fetching instance descriptor at descriptor number.
1860 inline String* GetKey(int descriptor_number);
1861 inline Object* GetValue(int descriptor_number);
1862 inline Smi* GetDetails(int descriptor_number);
1863 inline PropertyType GetType(int descriptor_number);
1864 inline int GetFieldIndex(int descriptor_number);
1865 inline JSFunction* GetConstantFunction(int descriptor_number);
1866 inline Object* GetCallbacksObject(int descriptor_number);
1867 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
1868 inline bool IsProperty(int descriptor_number);
1869 inline bool IsTransition(int descriptor_number);
1870 inline bool IsNullDescriptor(int descriptor_number);
1871 inline bool IsDontEnum(int descriptor_number);
1872
1873 // Accessor for complete descriptor.
1874 inline void Get(int descriptor_number, Descriptor* desc);
1875 inline void Set(int descriptor_number, Descriptor* desc);
1876
1877 // Transfer complete descriptor from another descriptor array to
1878 // this one.
1879 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
1880
1881 // Copy the descriptor array, insert a new descriptor and optionally
1882 // remove map transitions. If the descriptor is already present, it is
1883 // replaced. If a replaced descriptor is a real property (not a transition
1884 // or null), its enumeration index is kept as is.
1885 // If adding a real property, map transitions must be removed. If adding
1886 // a transition, they must not be removed. All null descriptors are removed.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001887 MUST_USE_RESULT Object* CopyInsert(Descriptor* descriptor,
1888 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00001889
1890 // Remove all transitions. Return a copy of the array with all transitions
1891 // removed, or a Failure object if the new array could not be allocated.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001892 MUST_USE_RESULT Object* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00001893
1894 // Sort the instance descriptors by the hash codes of their keys.
1895 void Sort();
1896
1897 // Search the instance descriptors for given name.
1898 inline int Search(String* name);
1899
Iain Merrick75681382010-08-19 15:07:18 +01001900 // As the above, but uses DescriptorLookupCache and updates it when
1901 // necessary.
1902 inline int SearchWithCache(String* name);
1903
Steve Blocka7e24c12009-10-30 11:49:00 +00001904 // Tells whether the name is present int the array.
1905 bool Contains(String* name) { return kNotFound != Search(name); }
1906
1907 // Perform a binary search in the instance descriptors represented
1908 // by this fixed array. low and high are descriptor indices. If there
1909 // are three instance descriptors in this array it should be called
1910 // with low=0 and high=2.
1911 int BinarySearch(String* name, int low, int high);
1912
1913 // Perform a linear search in the instance descriptors represented
1914 // by this fixed array. len is the number of descriptor indices that are
1915 // valid. Does not require the descriptors to be sorted.
1916 int LinearSearch(String* name, int len);
1917
1918 // Allocates a DescriptorArray, but returns the singleton
1919 // empty descriptor array object if number_of_descriptors is 0.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001920 MUST_USE_RESULT static Object* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00001921
1922 // Casting.
1923 static inline DescriptorArray* cast(Object* obj);
1924
1925 // Constant for denoting key was not found.
1926 static const int kNotFound = -1;
1927
1928 static const int kContentArrayIndex = 0;
1929 static const int kEnumerationIndexIndex = 1;
1930 static const int kFirstIndex = 2;
1931
1932 // The length of the "bridge" to the enum cache.
1933 static const int kEnumCacheBridgeLength = 2;
1934 static const int kEnumCacheBridgeEnumIndex = 0;
1935 static const int kEnumCacheBridgeCacheIndex = 1;
1936
1937 // Layout description.
1938 static const int kContentArrayOffset = FixedArray::kHeaderSize;
1939 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
1940 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
1941
1942 // Layout description for the bridge array.
1943 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
1944 static const int kEnumCacheBridgeCacheOffset =
1945 kEnumCacheBridgeEnumOffset + kPointerSize;
1946
1947#ifdef DEBUG
1948 // Print all the descriptors.
1949 void PrintDescriptors();
1950
1951 // Is the descriptor array sorted and without duplicates?
1952 bool IsSortedNoDuplicates();
1953
1954 // Are two DescriptorArrays equal?
1955 bool IsEqualTo(DescriptorArray* other);
1956#endif
1957
1958 // The maximum number of descriptors we want in a descriptor array (should
1959 // fit in a page).
1960 static const int kMaxNumberOfDescriptors = 1024 + 512;
1961
1962 private:
1963 // Conversion from descriptor number to array indices.
1964 static int ToKeyIndex(int descriptor_number) {
1965 return descriptor_number+kFirstIndex;
1966 }
Leon Clarkee46be812010-01-19 14:06:41 +00001967
1968 static int ToDetailsIndex(int descriptor_number) {
1969 return (descriptor_number << 1) + 1;
1970 }
1971
Steve Blocka7e24c12009-10-30 11:49:00 +00001972 static int ToValueIndex(int descriptor_number) {
1973 return descriptor_number << 1;
1974 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001975
1976 bool is_null_descriptor(int descriptor_number) {
1977 return PropertyDetails(GetDetails(descriptor_number)).type() ==
1978 NULL_DESCRIPTOR;
1979 }
1980 // Swap operation on FixedArray without using write barriers.
1981 static inline void fast_swap(FixedArray* array, int first, int second);
1982
1983 // Swap descriptor first and second.
1984 inline void Swap(int first, int second);
1985
1986 FixedArray* GetContentArray() {
1987 return FixedArray::cast(get(kContentArrayIndex));
1988 }
1989 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
1990};
1991
1992
1993// HashTable is a subclass of FixedArray that implements a hash table
1994// that uses open addressing and quadratic probing.
1995//
1996// In order for the quadratic probing to work, elements that have not
1997// yet been used and elements that have been deleted are
1998// distinguished. Probing continues when deleted elements are
1999// encountered and stops when unused elements are encountered.
2000//
2001// - Elements with key == undefined have not been used yet.
2002// - Elements with key == null have been deleted.
2003//
2004// The hash table class is parameterized with a Shape and a Key.
2005// Shape must be a class with the following interface:
2006// class ExampleShape {
2007// public:
2008// // Tells whether key matches other.
2009// static bool IsMatch(Key key, Object* other);
2010// // Returns the hash value for key.
2011// static uint32_t Hash(Key key);
2012// // Returns the hash value for object.
2013// static uint32_t HashForObject(Key key, Object* object);
2014// // Convert key to an object.
2015// static inline Object* AsObject(Key key);
2016// // The prefix size indicates number of elements in the beginning
2017// // of the backing storage.
2018// static const int kPrefixSize = ..;
2019// // The Element size indicates number of elements per entry.
2020// static const int kEntrySize = ..;
2021// };
Steve Block3ce2e202009-11-05 08:53:23 +00002022// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002023// beginning of the backing storage that can be used for non-element
2024// information by subclasses.
2025
2026template<typename Shape, typename Key>
2027class HashTable: public FixedArray {
2028 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002029 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002030 int NumberOfElements() {
2031 return Smi::cast(get(kNumberOfElementsIndex))->value();
2032 }
2033
Leon Clarkee46be812010-01-19 14:06:41 +00002034 // Returns the number of deleted elements in the hash table.
2035 int NumberOfDeletedElements() {
2036 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2037 }
2038
Steve Block3ce2e202009-11-05 08:53:23 +00002039 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002040 int Capacity() {
2041 return Smi::cast(get(kCapacityIndex))->value();
2042 }
2043
2044 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002045 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002046 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2047
2048 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002049 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002050 void ElementRemoved() {
2051 SetNumberOfElements(NumberOfElements() - 1);
2052 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2053 }
2054 void ElementsRemoved(int n) {
2055 SetNumberOfElements(NumberOfElements() - n);
2056 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2057 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002058
Steve Block3ce2e202009-11-05 08:53:23 +00002059 // Returns a new HashTable object. Might return Failure.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002060 MUST_USE_RESULT static Object* Allocate(
2061 int at_least_space_for,
2062 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002063
2064 // Returns the key at entry.
2065 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2066
2067 // Tells whether k is a real key. Null and undefined are not allowed
2068 // as keys and can be used to indicate missing or deleted elements.
2069 bool IsKey(Object* k) {
2070 return !k->IsNull() && !k->IsUndefined();
2071 }
2072
2073 // Garbage collection support.
2074 void IteratePrefix(ObjectVisitor* visitor);
2075 void IterateElements(ObjectVisitor* visitor);
2076
2077 // Casting.
2078 static inline HashTable* cast(Object* obj);
2079
2080 // Compute the probe offset (quadratic probing).
2081 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2082 return (n + n * n) >> 1;
2083 }
2084
2085 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002086 static const int kNumberOfDeletedElementsIndex = 1;
2087 static const int kCapacityIndex = 2;
2088 static const int kPrefixStartIndex = 3;
2089 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002090 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002091 static const int kEntrySize = Shape::kEntrySize;
2092 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002093 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002094 static const int kCapacityOffset =
2095 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002096
2097 // Constant used for denoting a absent entry.
2098 static const int kNotFound = -1;
2099
Leon Clarkee46be812010-01-19 14:06:41 +00002100 // Maximal capacity of HashTable. Based on maximal length of underlying
2101 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2102 // cannot overflow.
2103 static const int kMaxCapacity =
2104 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2105
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002106 // Find entry for key otherwise return kNotFound.
Steve Blocka7e24c12009-10-30 11:49:00 +00002107 int FindEntry(Key key);
2108
2109 protected:
2110
2111 // Find the entry at which to insert element with the given key that
2112 // has the given hash value.
2113 uint32_t FindInsertionEntry(uint32_t hash);
2114
2115 // Returns the index for an entry (of the key)
2116 static inline int EntryToIndex(int entry) {
2117 return (entry * kEntrySize) + kElementsStartIndex;
2118 }
2119
Steve Block3ce2e202009-11-05 08:53:23 +00002120 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002121 void SetNumberOfElements(int nof) {
2122 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2123 }
2124
Leon Clarkee46be812010-01-19 14:06:41 +00002125 // Update the number of deleted elements in the hash table.
2126 void SetNumberOfDeletedElements(int nod) {
2127 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2128 }
2129
Steve Blocka7e24c12009-10-30 11:49:00 +00002130 // Sets the capacity of the hash table.
2131 void SetCapacity(int capacity) {
2132 // To scale a computed hash code to fit within the hash table, we
2133 // use bit-wise AND with a mask, so the capacity must be positive
2134 // and non-zero.
2135 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002136 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002137 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2138 }
2139
2140
2141 // Returns probe entry.
2142 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2143 ASSERT(IsPowerOf2(size));
2144 return (hash + GetProbeOffset(number)) & (size - 1);
2145 }
2146
Leon Clarkee46be812010-01-19 14:06:41 +00002147 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2148 return hash & (size - 1);
2149 }
2150
2151 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2152 return (last + number) & (size - 1);
2153 }
2154
Steve Blocka7e24c12009-10-30 11:49:00 +00002155 // Ensure enough space for n additional elements.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002156 MUST_USE_RESULT Object* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002157};
2158
2159
2160
2161// HashTableKey is an abstract superclass for virtual key behavior.
2162class HashTableKey {
2163 public:
2164 // Returns whether the other object matches this key.
2165 virtual bool IsMatch(Object* other) = 0;
2166 // Returns the hash value for this key.
2167 virtual uint32_t Hash() = 0;
2168 // Returns the hash value for object.
2169 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002170 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002171 // If allocations fails a failure object is returned.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002172 MUST_USE_RESULT virtual Object* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002173 // Required.
2174 virtual ~HashTableKey() {}
2175};
2176
2177class SymbolTableShape {
2178 public:
2179 static bool IsMatch(HashTableKey* key, Object* value) {
2180 return key->IsMatch(value);
2181 }
2182 static uint32_t Hash(HashTableKey* key) {
2183 return key->Hash();
2184 }
2185 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2186 return key->HashForObject(object);
2187 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002188 MUST_USE_RESULT static Object* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002189 return key->AsObject();
2190 }
2191
2192 static const int kPrefixSize = 0;
2193 static const int kEntrySize = 1;
2194};
2195
2196// SymbolTable.
2197//
2198// No special elements in the prefix and the element size is 1
2199// because only the symbol itself (the key) needs to be stored.
2200class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2201 public:
2202 // Find symbol in the symbol table. If it is not there yet, it is
2203 // added. The return value is the symbol table which might have
2204 // been enlarged. If the return value is not a failure, the symbol
2205 // pointer *s is set to the symbol found.
2206 Object* LookupSymbol(Vector<const char> str, Object** s);
2207 Object* LookupString(String* key, Object** s);
2208
2209 // Looks up a symbol that is equal to the given string and returns
2210 // true if it is found, assigning the symbol to the given output
2211 // parameter.
2212 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002213 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002214
2215 // Casting.
2216 static inline SymbolTable* cast(Object* obj);
2217
2218 private:
2219 Object* LookupKey(HashTableKey* key, Object** s);
2220
2221 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2222};
2223
2224
2225class MapCacheShape {
2226 public:
2227 static bool IsMatch(HashTableKey* key, Object* value) {
2228 return key->IsMatch(value);
2229 }
2230 static uint32_t Hash(HashTableKey* key) {
2231 return key->Hash();
2232 }
2233
2234 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2235 return key->HashForObject(object);
2236 }
2237
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002238 MUST_USE_RESULT static Object* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002239 return key->AsObject();
2240 }
2241
2242 static const int kPrefixSize = 0;
2243 static const int kEntrySize = 2;
2244};
2245
2246
2247// MapCache.
2248//
2249// Maps keys that are a fixed array of symbols to a map.
2250// Used for canonicalize maps for object literals.
2251class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2252 public:
2253 // Find cached value for a string key, otherwise return null.
2254 Object* Lookup(FixedArray* key);
2255 Object* Put(FixedArray* key, Map* value);
2256 static inline MapCache* cast(Object* obj);
2257
2258 private:
2259 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2260};
2261
2262
2263template <typename Shape, typename Key>
2264class Dictionary: public HashTable<Shape, Key> {
2265 public:
2266
2267 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2268 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2269 }
2270
2271 // Returns the value at entry.
2272 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002273 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002274 }
2275
2276 // Set the value for entry.
2277 void ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002278 // Check that this value can actually be written.
2279 PropertyDetails details = DetailsAt(entry);
2280 // If a value has not been initilized we allow writing to it even if
2281 // it is read only (a declared const that has not been initialized).
2282 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) return;
Steve Block6ded16b2010-05-10 14:33:55 +01002283 this->set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002284 }
2285
2286 // Returns the property details for the property at entry.
2287 PropertyDetails DetailsAt(int entry) {
2288 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2289 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002290 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002291 }
2292
2293 // Set the details for entry.
2294 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002295 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002296 }
2297
2298 // Sorting support
2299 void CopyValuesTo(FixedArray* elements);
2300
2301 // Delete a property from the dictionary.
2302 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2303
2304 // Returns the number of elements in the dictionary filtering out properties
2305 // with the specified attributes.
2306 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2307
2308 // Returns the number of enumerable elements in the dictionary.
2309 int NumberOfEnumElements();
2310
2311 // Copies keys to preallocated fixed array.
2312 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2313 // Fill in details for properties into storage.
2314 void CopyKeysTo(FixedArray* storage);
2315
2316 // Accessors for next enumeration index.
2317 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002318 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002319 }
2320
2321 int NextEnumerationIndex() {
2322 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2323 }
2324
2325 // Returns a new array for dictionary usage. Might return Failure.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002326 MUST_USE_RESULT static Object* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002327
2328 // Ensure enough space for n additional elements.
2329 Object* EnsureCapacity(int n, Key key);
2330
2331#ifdef DEBUG
2332 void Print();
2333#endif
2334 // Returns the key (slow).
2335 Object* SlowReverseLookup(Object* value);
2336
2337 // Sets the entry to (key, value) pair.
2338 inline void SetEntry(int entry,
2339 Object* key,
2340 Object* value,
2341 PropertyDetails details);
2342
2343 Object* Add(Key key, Object* value, PropertyDetails details);
2344
2345 protected:
2346 // Generic at put operation.
2347 Object* AtPut(Key key, Object* value);
2348
2349 // Add entry to dictionary.
2350 Object* AddEntry(Key key,
2351 Object* value,
2352 PropertyDetails details,
2353 uint32_t hash);
2354
2355 // Generate new enumeration indices to avoid enumeration index overflow.
2356 Object* GenerateNewEnumerationIndices();
2357 static const int kMaxNumberKeyIndex =
2358 HashTable<Shape, Key>::kPrefixStartIndex;
2359 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2360};
2361
2362
2363class StringDictionaryShape {
2364 public:
2365 static inline bool IsMatch(String* key, Object* other);
2366 static inline uint32_t Hash(String* key);
2367 static inline uint32_t HashForObject(String* key, Object* object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002368 MUST_USE_RESULT static inline Object* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002369 static const int kPrefixSize = 2;
2370 static const int kEntrySize = 3;
2371 static const bool kIsEnumerable = true;
2372};
2373
2374
2375class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2376 public:
2377 static inline StringDictionary* cast(Object* obj) {
2378 ASSERT(obj->IsDictionary());
2379 return reinterpret_cast<StringDictionary*>(obj);
2380 }
2381
2382 // Copies enumerable keys to preallocated fixed array.
2383 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2384
2385 // For transforming properties of a JSObject.
2386 Object* TransformPropertiesToFastFor(JSObject* obj,
2387 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002388
2389 // Find entry for key otherwise return kNotFound. Optimzed version of
2390 // HashTable::FindEntry.
2391 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002392};
2393
2394
2395class NumberDictionaryShape {
2396 public:
2397 static inline bool IsMatch(uint32_t key, Object* other);
2398 static inline uint32_t Hash(uint32_t key);
2399 static inline uint32_t HashForObject(uint32_t key, Object* object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002400 MUST_USE_RESULT static inline Object* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002401 static const int kPrefixSize = 2;
2402 static const int kEntrySize = 3;
2403 static const bool kIsEnumerable = false;
2404};
2405
2406
2407class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2408 public:
2409 static NumberDictionary* cast(Object* obj) {
2410 ASSERT(obj->IsDictionary());
2411 return reinterpret_cast<NumberDictionary*>(obj);
2412 }
2413
2414 // Type specific at put (default NONE attributes is used when adding).
2415 Object* AtNumberPut(uint32_t key, Object* value);
2416 Object* AddNumberEntry(uint32_t key,
2417 Object* value,
2418 PropertyDetails details);
2419
2420 // Set an existing entry or add a new one if needed.
2421 Object* Set(uint32_t key, Object* value, PropertyDetails details);
2422
2423 void UpdateMaxNumberKey(uint32_t key);
2424
2425 // If slow elements are required we will never go back to fast-case
2426 // for the elements kept in this dictionary. We require slow
2427 // elements if an element has been added at an index larger than
2428 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2429 // when defining a getter or setter with a number key.
2430 inline bool requires_slow_elements();
2431 inline void set_requires_slow_elements();
2432
2433 // Get the value of the max number key that has been added to this
2434 // dictionary. max_number_key can only be called if
2435 // requires_slow_elements returns false.
2436 inline uint32_t max_number_key();
2437
2438 // Remove all entries were key is a number and (from <= key && key < to).
2439 void RemoveNumberEntries(uint32_t from, uint32_t to);
2440
2441 // Bit masks.
2442 static const int kRequiresSlowElementsMask = 1;
2443 static const int kRequiresSlowElementsTagSize = 1;
2444 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2445};
2446
2447
Steve Block6ded16b2010-05-10 14:33:55 +01002448// JSFunctionResultCache caches results of some JSFunction invocation.
2449// It is a fixed array with fixed structure:
2450// [0]: factory function
2451// [1]: finger index
2452// [2]: current cache size
2453// [3]: dummy field.
2454// The rest of array are key/value pairs.
2455class JSFunctionResultCache: public FixedArray {
2456 public:
2457 static const int kFactoryIndex = 0;
2458 static const int kFingerIndex = kFactoryIndex + 1;
2459 static const int kCacheSizeIndex = kFingerIndex + 1;
2460 static const int kDummyIndex = kCacheSizeIndex + 1;
2461 static const int kEntriesIndex = kDummyIndex + 1;
2462
2463 static const int kEntrySize = 2; // key + value
2464
Kristian Monsen25f61362010-05-21 11:50:48 +01002465 static const int kFactoryOffset = kHeaderSize;
2466 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2467 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2468
Steve Block6ded16b2010-05-10 14:33:55 +01002469 inline void MakeZeroSize();
2470 inline void Clear();
2471
2472 // Casting
2473 static inline JSFunctionResultCache* cast(Object* obj);
2474
2475#ifdef DEBUG
2476 void JSFunctionResultCacheVerify();
2477#endif
2478};
2479
2480
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002481// The cache for maps used by normalized (dictionary mode) objects.
2482// Such maps do not have property descriptors, so a typical program
2483// needs very limited number of distinct normalized maps.
2484class NormalizedMapCache: public FixedArray {
2485 public:
2486 static const int kEntries = 64;
2487
2488 static bool IsCacheable(JSObject* object);
2489
2490 Object* Get(JSObject* object, PropertyNormalizationMode mode);
2491
2492 bool Contains(Map* map);
2493
2494 void Clear();
2495
2496 // Casting
2497 static inline NormalizedMapCache* cast(Object* obj);
2498
2499#ifdef DEBUG
2500 void NormalizedMapCacheVerify();
2501#endif
2502
2503 private:
2504 static int Hash(Map* fast);
2505
2506 static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2507};
2508
2509
Steve Blocka7e24c12009-10-30 11:49:00 +00002510// ByteArray represents fixed sized byte arrays. Used by the outside world,
2511// such as PCRE, and also by the memory allocator and garbage collector to
2512// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002513class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002514 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002515 // [length]: length of the array.
2516 inline int length();
2517 inline void set_length(int value);
2518
Steve Blocka7e24c12009-10-30 11:49:00 +00002519 // Setter and getter.
2520 inline byte get(int index);
2521 inline void set(int index, byte value);
2522
2523 // Treat contents as an int array.
2524 inline int get_int(int index);
2525
2526 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002527 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002528 }
2529 // We use byte arrays for free blocks in the heap. Given a desired size in
2530 // bytes that is a multiple of the word size and big enough to hold a byte
2531 // array, this function returns the number of elements a byte array should
2532 // have.
2533 static int LengthFor(int size_in_bytes) {
2534 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2535 ASSERT(size_in_bytes >= kHeaderSize);
2536 return size_in_bytes - kHeaderSize;
2537 }
2538
2539 // Returns data start address.
2540 inline Address GetDataStartAddress();
2541
2542 // Returns a pointer to the ByteArray object for a given data start address.
2543 static inline ByteArray* FromDataStartAddress(Address address);
2544
2545 // Casting.
2546 static inline ByteArray* cast(Object* obj);
2547
2548 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002549 inline int ByteArraySize() {
2550 return SizeFor(this->length());
2551 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002552#ifdef DEBUG
2553 void ByteArrayPrint();
2554 void ByteArrayVerify();
2555#endif
2556
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002557 // Layout description.
2558 // Length is smi tagged when it is stored.
2559 static const int kLengthOffset = HeapObject::kHeaderSize;
2560 static const int kHeaderSize = kLengthOffset + kPointerSize;
2561
2562 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002563
Leon Clarkee46be812010-01-19 14:06:41 +00002564 // Maximal memory consumption for a single ByteArray.
2565 static const int kMaxSize = 512 * MB;
2566 // Maximal length of a single ByteArray.
2567 static const int kMaxLength = kMaxSize - kHeaderSize;
2568
Steve Blocka7e24c12009-10-30 11:49:00 +00002569 private:
2570 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2571};
2572
2573
2574// A PixelArray represents a fixed-size byte array with special semantics
2575// used for implementing the CanvasPixelArray object. Please see the
2576// specification at:
2577// http://www.whatwg.org/specs/web-apps/current-work/
2578// multipage/the-canvas-element.html#canvaspixelarray
2579// In particular, write access clamps the value written to 0 or 255 if the
2580// value written is outside this range.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002581class PixelArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002582 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002583 // [length]: length of the array.
2584 inline int length();
2585 inline void set_length(int value);
2586
Steve Blocka7e24c12009-10-30 11:49:00 +00002587 // [external_pointer]: The pointer to the external memory area backing this
2588 // pixel array.
2589 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2590
2591 // Setter and getter.
2592 inline uint8_t get(int index);
2593 inline void set(int index, uint8_t value);
2594
2595 // This accessor applies the correct conversion from Smi, HeapNumber and
2596 // undefined and clamps the converted value between 0 and 255.
2597 Object* SetValue(uint32_t index, Object* value);
2598
2599 // Casting.
2600 static inline PixelArray* cast(Object* obj);
2601
2602#ifdef DEBUG
2603 void PixelArrayPrint();
2604 void PixelArrayVerify();
2605#endif // DEBUG
2606
Steve Block3ce2e202009-11-05 08:53:23 +00002607 // Maximal acceptable length for a pixel array.
2608 static const int kMaxLength = 0x3fffffff;
2609
Steve Blocka7e24c12009-10-30 11:49:00 +00002610 // PixelArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002611 static const int kLengthOffset = HeapObject::kHeaderSize;
2612 static const int kExternalPointerOffset =
2613 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002614 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002615 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002616
2617 private:
2618 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2619};
2620
2621
Steve Block3ce2e202009-11-05 08:53:23 +00002622// An ExternalArray represents a fixed-size array of primitive values
2623// which live outside the JavaScript heap. Its subclasses are used to
2624// implement the CanvasArray types being defined in the WebGL
2625// specification. As of this writing the first public draft is not yet
2626// available, but Khronos members can access the draft at:
2627// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2628//
2629// The semantics of these arrays differ from CanvasPixelArray.
2630// Out-of-range values passed to the setter are converted via a C
2631// cast, not clamping. Out-of-range indices cause exceptions to be
2632// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002633class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002634 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002635 // [length]: length of the array.
2636 inline int length();
2637 inline void set_length(int value);
2638
Steve Block3ce2e202009-11-05 08:53:23 +00002639 // [external_pointer]: The pointer to the external memory area backing this
2640 // external array.
2641 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2642
2643 // Casting.
2644 static inline ExternalArray* cast(Object* obj);
2645
2646 // Maximal acceptable length for an external array.
2647 static const int kMaxLength = 0x3fffffff;
2648
2649 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002650 static const int kLengthOffset = HeapObject::kHeaderSize;
2651 static const int kExternalPointerOffset =
2652 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002653 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002654 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002655
2656 private:
2657 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2658};
2659
2660
2661class ExternalByteArray: public ExternalArray {
2662 public:
2663 // Setter and getter.
2664 inline int8_t get(int index);
2665 inline void set(int index, int8_t value);
2666
2667 // This accessor applies the correct conversion from Smi, HeapNumber
2668 // and undefined.
2669 Object* SetValue(uint32_t index, Object* value);
2670
2671 // Casting.
2672 static inline ExternalByteArray* cast(Object* obj);
2673
2674#ifdef DEBUG
2675 void ExternalByteArrayPrint();
2676 void ExternalByteArrayVerify();
2677#endif // DEBUG
2678
2679 private:
2680 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2681};
2682
2683
2684class ExternalUnsignedByteArray: public ExternalArray {
2685 public:
2686 // Setter and getter.
2687 inline uint8_t get(int index);
2688 inline void set(int index, uint8_t value);
2689
2690 // This accessor applies the correct conversion from Smi, HeapNumber
2691 // and undefined.
2692 Object* SetValue(uint32_t index, Object* value);
2693
2694 // Casting.
2695 static inline ExternalUnsignedByteArray* cast(Object* obj);
2696
2697#ifdef DEBUG
2698 void ExternalUnsignedByteArrayPrint();
2699 void ExternalUnsignedByteArrayVerify();
2700#endif // DEBUG
2701
2702 private:
2703 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2704};
2705
2706
2707class ExternalShortArray: public ExternalArray {
2708 public:
2709 // Setter and getter.
2710 inline int16_t get(int index);
2711 inline void set(int index, int16_t value);
2712
2713 // This accessor applies the correct conversion from Smi, HeapNumber
2714 // and undefined.
2715 Object* SetValue(uint32_t index, Object* value);
2716
2717 // Casting.
2718 static inline ExternalShortArray* cast(Object* obj);
2719
2720#ifdef DEBUG
2721 void ExternalShortArrayPrint();
2722 void ExternalShortArrayVerify();
2723#endif // DEBUG
2724
2725 private:
2726 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2727};
2728
2729
2730class ExternalUnsignedShortArray: public ExternalArray {
2731 public:
2732 // Setter and getter.
2733 inline uint16_t get(int index);
2734 inline void set(int index, uint16_t value);
2735
2736 // This accessor applies the correct conversion from Smi, HeapNumber
2737 // and undefined.
2738 Object* SetValue(uint32_t index, Object* value);
2739
2740 // Casting.
2741 static inline ExternalUnsignedShortArray* cast(Object* obj);
2742
2743#ifdef DEBUG
2744 void ExternalUnsignedShortArrayPrint();
2745 void ExternalUnsignedShortArrayVerify();
2746#endif // DEBUG
2747
2748 private:
2749 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2750};
2751
2752
2753class ExternalIntArray: public ExternalArray {
2754 public:
2755 // Setter and getter.
2756 inline int32_t get(int index);
2757 inline void set(int index, int32_t value);
2758
2759 // This accessor applies the correct conversion from Smi, HeapNumber
2760 // and undefined.
2761 Object* SetValue(uint32_t index, Object* value);
2762
2763 // Casting.
2764 static inline ExternalIntArray* cast(Object* obj);
2765
2766#ifdef DEBUG
2767 void ExternalIntArrayPrint();
2768 void ExternalIntArrayVerify();
2769#endif // DEBUG
2770
2771 private:
2772 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
2773};
2774
2775
2776class ExternalUnsignedIntArray: public ExternalArray {
2777 public:
2778 // Setter and getter.
2779 inline uint32_t get(int index);
2780 inline void set(int index, uint32_t value);
2781
2782 // This accessor applies the correct conversion from Smi, HeapNumber
2783 // and undefined.
2784 Object* SetValue(uint32_t index, Object* value);
2785
2786 // Casting.
2787 static inline ExternalUnsignedIntArray* cast(Object* obj);
2788
2789#ifdef DEBUG
2790 void ExternalUnsignedIntArrayPrint();
2791 void ExternalUnsignedIntArrayVerify();
2792#endif // DEBUG
2793
2794 private:
2795 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
2796};
2797
2798
2799class ExternalFloatArray: public ExternalArray {
2800 public:
2801 // Setter and getter.
2802 inline float get(int index);
2803 inline void set(int index, float value);
2804
2805 // This accessor applies the correct conversion from Smi, HeapNumber
2806 // and undefined.
2807 Object* SetValue(uint32_t index, Object* value);
2808
2809 // Casting.
2810 static inline ExternalFloatArray* cast(Object* obj);
2811
2812#ifdef DEBUG
2813 void ExternalFloatArrayPrint();
2814 void ExternalFloatArrayVerify();
2815#endif // DEBUG
2816
2817 private:
2818 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
2819};
2820
2821
Steve Blocka7e24c12009-10-30 11:49:00 +00002822// Code describes objects with on-the-fly generated machine code.
2823class Code: public HeapObject {
2824 public:
2825 // Opaque data type for encapsulating code flags like kind, inline
2826 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01002827 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
2828 // enumeration type has correct value range (see Issue 830 for more details).
2829 enum Flags {
2830 FLAGS_MIN_VALUE = kMinInt,
2831 FLAGS_MAX_VALUE = kMaxInt
2832 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002833
2834 enum Kind {
2835 FUNCTION,
2836 STUB,
2837 BUILTIN,
2838 LOAD_IC,
2839 KEYED_LOAD_IC,
2840 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002841 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00002842 STORE_IC,
2843 KEYED_STORE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002844 BINARY_OP_IC,
2845 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00002846 // Flags.
2847
2848 // Pseudo-kinds.
2849 REGEXP = BUILTIN,
2850 FIRST_IC_KIND = LOAD_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002851 LAST_IC_KIND = BINARY_OP_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00002852 };
2853
2854 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002855 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00002856 };
2857
2858#ifdef ENABLE_DISASSEMBLER
2859 // Printing
2860 static const char* Kind2String(Kind kind);
2861 static const char* ICState2String(InlineCacheState state);
2862 static const char* PropertyType2String(PropertyType type);
2863 void Disassemble(const char* name);
2864#endif // ENABLE_DISASSEMBLER
2865
2866 // [instruction_size]: Size of the native instructions
2867 inline int instruction_size();
2868 inline void set_instruction_size(int value);
2869
Leon Clarkeac952652010-07-15 11:15:24 +01002870 // [relocation_info]: Code relocation information
2871 DECL_ACCESSORS(relocation_info, ByteArray)
2872
2873 // Unchecked accessor to be used during GC.
2874 inline ByteArray* unchecked_relocation_info();
2875
Steve Blocka7e24c12009-10-30 11:49:00 +00002876 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00002877
Steve Blocka7e24c12009-10-30 11:49:00 +00002878 // [flags]: Various code flags.
2879 inline Flags flags();
2880 inline void set_flags(Flags flags);
2881
2882 // [flags]: Access to specific code flags.
2883 inline Kind kind();
2884 inline InlineCacheState ic_state(); // Only valid for IC stubs.
2885 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
2886 inline PropertyType type(); // Only valid for monomorphic IC stubs.
2887 inline int arguments_count(); // Only valid for call IC stubs.
2888
2889 // Testers for IC stub kinds.
2890 inline bool is_inline_cache_stub();
2891 inline bool is_load_stub() { return kind() == LOAD_IC; }
2892 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
2893 inline bool is_store_stub() { return kind() == STORE_IC; }
2894 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
2895 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002896 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00002897
Steve Block6ded16b2010-05-10 14:33:55 +01002898 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002899 inline int major_key();
2900 inline void set_major_key(int major);
Steve Blocka7e24c12009-10-30 11:49:00 +00002901
2902 // Flags operations.
2903 static inline Flags ComputeFlags(Kind kind,
2904 InLoopFlag in_loop = NOT_IN_LOOP,
2905 InlineCacheState ic_state = UNINITIALIZED,
2906 PropertyType type = NORMAL,
Steve Block8defd9f2010-07-08 12:39:36 +01002907 int argc = -1,
2908 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00002909
2910 static inline Flags ComputeMonomorphicFlags(
2911 Kind kind,
2912 PropertyType type,
Steve Block8defd9f2010-07-08 12:39:36 +01002913 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00002914 InLoopFlag in_loop = NOT_IN_LOOP,
2915 int argc = -1);
2916
2917 static inline Kind ExtractKindFromFlags(Flags flags);
2918 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
2919 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
2920 static inline PropertyType ExtractTypeFromFlags(Flags flags);
2921 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01002922 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00002923 static inline Flags RemoveTypeFromFlags(Flags flags);
2924
2925 // Convert a target address into a code object.
2926 static inline Code* GetCodeFromTargetAddress(Address address);
2927
Steve Block791712a2010-08-27 10:21:07 +01002928 // Convert an entry address into an object.
2929 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
2930
Steve Blocka7e24c12009-10-30 11:49:00 +00002931 // Returns the address of the first instruction.
2932 inline byte* instruction_start();
2933
Leon Clarkeac952652010-07-15 11:15:24 +01002934 // Returns the address right after the last instruction.
2935 inline byte* instruction_end();
2936
Steve Blocka7e24c12009-10-30 11:49:00 +00002937 // Returns the size of the instructions, padding, and relocation information.
2938 inline int body_size();
2939
2940 // Returns the address of the first relocation info (read backwards!).
2941 inline byte* relocation_start();
2942
2943 // Code entry point.
2944 inline byte* entry();
2945
2946 // Returns true if pc is inside this object's instructions.
2947 inline bool contains(byte* pc);
2948
Steve Blocka7e24c12009-10-30 11:49:00 +00002949 // Relocate the code by delta bytes. Called to signal that this code
2950 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002951 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002952
2953 // Migrate code described by desc.
2954 void CopyFrom(const CodeDesc& desc);
2955
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002956 // Returns the object size for a given body (used for allocation).
2957 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002958 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002959 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00002960 }
2961
2962 // Calculate the size of the code object to report for log events. This takes
2963 // the layout of the code object into account.
2964 int ExecutableSize() {
2965 // Check that the assumptions about the layout of the code object holds.
2966 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
2967 Code::kHeaderSize);
2968 return instruction_size() + Code::kHeaderSize;
2969 }
2970
2971 // Locating source position.
2972 int SourcePosition(Address pc);
2973 int SourceStatementPosition(Address pc);
2974
2975 // Casting.
2976 static inline Code* cast(Object* obj);
2977
2978 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002979 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01002980 inline void CodeIterateBody(ObjectVisitor* v);
2981
2982 template<typename StaticVisitor>
2983 inline void CodeIterateBody();
Steve Blocka7e24c12009-10-30 11:49:00 +00002984#ifdef DEBUG
2985 void CodePrint();
2986 void CodeVerify();
2987#endif
2988 // Code entry points are aligned to 32 bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002989 static const int kCodeAlignmentBits = 5;
2990 static const int kCodeAlignment = 1 << kCodeAlignmentBits;
Steve Blocka7e24c12009-10-30 11:49:00 +00002991 static const int kCodeAlignmentMask = kCodeAlignment - 1;
2992
2993 // Layout description.
2994 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01002995 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002996 static const int kFlagsOffset = kRelocationInfoOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002997 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
2998 // Add padding to align the instruction start following right after
2999 // the Code object header.
3000 static const int kHeaderSize =
3001 (kKindSpecificFlagsOffset + kIntSize + kCodeAlignmentMask) &
3002 ~kCodeAlignmentMask;
3003
3004 // Byte offsets within kKindSpecificFlagsOffset.
3005 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset + 1;
3006
3007 // Flags layout.
3008 static const int kFlagsICStateShift = 0;
3009 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003010 static const int kFlagsTypeShift = 4;
3011 static const int kFlagsKindShift = 7;
Steve Block8defd9f2010-07-08 12:39:36 +01003012 static const int kFlagsICHolderShift = 11;
3013 static const int kFlagsArgumentsCountShift = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00003014
Steve Block6ded16b2010-05-10 14:33:55 +01003015 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3016 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003017 static const int kFlagsTypeMask = 0x00000070; // 00001110000
3018 static const int kFlagsKindMask = 0x00000780; // 11110000000
Steve Block8defd9f2010-07-08 12:39:36 +01003019 static const int kFlagsCacheInPrototypeMapMask = 0x00000800;
3020 static const int kFlagsArgumentsCountMask = 0xFFFFF000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003021
3022 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003023 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003024
3025 private:
3026 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3027};
3028
3029
3030// All heap objects have a Map that describes their structure.
3031// A Map contains information about:
3032// - Size information about the object
3033// - How to iterate over an object (for garbage collection)
3034class Map: public HeapObject {
3035 public:
3036 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003037 // Size in bytes or kVariableSizeSentinel if instances do not have
3038 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003039 inline int instance_size();
3040 inline void set_instance_size(int value);
3041
3042 // Count of properties allocated in the object.
3043 inline int inobject_properties();
3044 inline void set_inobject_properties(int value);
3045
3046 // Count of property fields pre-allocated in the object when first allocated.
3047 inline int pre_allocated_property_fields();
3048 inline void set_pre_allocated_property_fields(int value);
3049
3050 // Instance type.
3051 inline InstanceType instance_type();
3052 inline void set_instance_type(InstanceType value);
3053
3054 // Tells how many unused property fields are available in the
3055 // instance (only used for JSObject in fast mode).
3056 inline int unused_property_fields();
3057 inline void set_unused_property_fields(int value);
3058
3059 // Bit field.
3060 inline byte bit_field();
3061 inline void set_bit_field(byte value);
3062
3063 // Bit field 2.
3064 inline byte bit_field2();
3065 inline void set_bit_field2(byte value);
3066
3067 // Tells whether the object in the prototype property will be used
3068 // for instances created from this function. If the prototype
3069 // property is set to a value that is not a JSObject, the prototype
3070 // property will not be used to create instances of the function.
3071 // See ECMA-262, 13.2.2.
3072 inline void set_non_instance_prototype(bool value);
3073 inline bool has_non_instance_prototype();
3074
Steve Block6ded16b2010-05-10 14:33:55 +01003075 // Tells whether function has special prototype property. If not, prototype
3076 // property will not be created when accessed (will return undefined),
3077 // and construction from this function will not be allowed.
3078 inline void set_function_with_prototype(bool value);
3079 inline bool function_with_prototype();
3080
Steve Blocka7e24c12009-10-30 11:49:00 +00003081 // Tells whether the instance with this map should be ignored by the
3082 // __proto__ accessor.
3083 inline void set_is_hidden_prototype() {
3084 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3085 }
3086
3087 inline bool is_hidden_prototype() {
3088 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3089 }
3090
3091 // Records and queries whether the instance has a named interceptor.
3092 inline void set_has_named_interceptor() {
3093 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3094 }
3095
3096 inline bool has_named_interceptor() {
3097 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3098 }
3099
3100 // Records and queries whether the instance has an indexed interceptor.
3101 inline void set_has_indexed_interceptor() {
3102 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3103 }
3104
3105 inline bool has_indexed_interceptor() {
3106 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3107 }
3108
3109 // Tells whether the instance is undetectable.
3110 // An undetectable object is a special class of JSObject: 'typeof' operator
3111 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3112 // a normal JS object. It is useful for implementing undetectable
3113 // document.all in Firefox & Safari.
3114 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3115 inline void set_is_undetectable() {
3116 set_bit_field(bit_field() | (1 << kIsUndetectable));
3117 }
3118
3119 inline bool is_undetectable() {
3120 return ((1 << kIsUndetectable) & bit_field()) != 0;
3121 }
3122
Steve Blocka7e24c12009-10-30 11:49:00 +00003123 // Tells whether the instance has a call-as-function handler.
3124 inline void set_has_instance_call_handler() {
3125 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3126 }
3127
3128 inline bool has_instance_call_handler() {
3129 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3130 }
3131
Steve Block8defd9f2010-07-08 12:39:36 +01003132 inline void set_is_extensible(bool value);
3133 inline bool is_extensible();
3134
3135 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003136 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3137 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003138 if (value) {
3139 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3140 } else {
3141 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3142 }
Leon Clarkee46be812010-01-19 14:06:41 +00003143 }
3144
Iain Merrick75681382010-08-19 15:07:18 +01003145 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003146 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003147 }
3148
Steve Blocka7e24c12009-10-30 11:49:00 +00003149 // Tells whether the instance needs security checks when accessing its
3150 // properties.
3151 inline void set_is_access_check_needed(bool access_check_needed);
3152 inline bool is_access_check_needed();
3153
3154 // [prototype]: implicit prototype object.
3155 DECL_ACCESSORS(prototype, Object)
3156
3157 // [constructor]: points back to the function responsible for this map.
3158 DECL_ACCESSORS(constructor, Object)
3159
3160 // [instance descriptors]: describes the object.
3161 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3162
3163 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003164 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003165
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003166 MUST_USE_RESULT Object* CopyDropDescriptors();
3167
3168 MUST_USE_RESULT Object* CopyNormalized(PropertyNormalizationMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00003169
3170 // Returns a copy of the map, with all transitions dropped from the
3171 // instance descriptors.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003172 MUST_USE_RESULT Object* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00003173
Steve Block8defd9f2010-07-08 12:39:36 +01003174 // Returns this map if it has the fast elements bit set, otherwise
3175 // returns a copy of the map, with all transitions dropped from the
3176 // descriptors and the fast elements bit set.
3177 inline Object* GetFastElementsMap();
3178
3179 // Returns this map if it has the fast elements bit cleared,
3180 // otherwise returns a copy of the map, with all transitions dropped
3181 // from the descriptors and the fast elements bit cleared.
3182 inline Object* GetSlowElementsMap();
3183
Steve Blocka7e24c12009-10-30 11:49:00 +00003184 // Returns the property index for name (only valid for FAST MODE).
3185 int PropertyIndexFor(String* name);
3186
3187 // Returns the next free property index (only valid for FAST MODE).
3188 int NextFreePropertyIndex();
3189
3190 // Returns the number of properties described in instance_descriptors.
3191 int NumberOfDescribedProperties();
3192
3193 // Casting.
3194 static inline Map* cast(Object* obj);
3195
3196 // Locate an accessor in the instance descriptor.
3197 AccessorDescriptor* FindAccessor(String* name);
3198
3199 // Code cache operations.
3200
3201 // Clears the code cache.
3202 inline void ClearCodeCache();
3203
3204 // Update code cache.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003205 MUST_USE_RESULT Object* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003206
3207 // Returns the found code or undefined if absent.
3208 Object* FindInCodeCache(String* name, Code::Flags flags);
3209
3210 // Returns the non-negative index of the code object if it is in the
3211 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003212 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003213
3214 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003215 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003216
3217 // For every transition in this map, makes the transition's
3218 // target's prototype pointer point back to this map.
3219 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3220 void CreateBackPointers();
3221
3222 // Set all map transitions from this map to dead maps to null.
3223 // Also, restore the original prototype on the targets of these
3224 // transitions, so that we do not process this map again while
3225 // following back pointers.
3226 void ClearNonLiveTransitions(Object* real_prototype);
3227
3228 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00003229#ifdef DEBUG
3230 void MapPrint();
3231 void MapVerify();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003232 void NormalizedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00003233#endif
3234
Iain Merrick75681382010-08-19 15:07:18 +01003235 inline int visitor_id();
3236 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003237
Steve Blocka7e24c12009-10-30 11:49:00 +00003238 static const int kMaxPreAllocatedPropertyFields = 255;
3239
3240 // Layout description.
3241 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3242 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3243 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3244 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3245 static const int kInstanceDescriptorsOffset =
3246 kConstructorOffset + kPointerSize;
3247 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003248 static const int kPadStart = kCodeCacheOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003249 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3250
3251 // Layout of pointer fields. Heap iteration code relies on them
3252 // being continiously allocated.
3253 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3254 static const int kPointerFieldsEndOffset =
3255 Map::kCodeCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003256
3257 // Byte offsets within kInstanceSizesOffset.
3258 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3259 static const int kInObjectPropertiesByte = 1;
3260 static const int kInObjectPropertiesOffset =
3261 kInstanceSizesOffset + kInObjectPropertiesByte;
3262 static const int kPreAllocatedPropertyFieldsByte = 2;
3263 static const int kPreAllocatedPropertyFieldsOffset =
3264 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003265 static const int kVisitorIdByte = 3;
3266 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00003267
3268 // Byte offsets within kInstanceAttributesOffset attributes.
3269 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3270 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3271 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3272 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3273
3274 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3275
3276 // Bit positions for bit field.
3277 static const int kUnused = 0; // To be used for marking recently used maps.
3278 static const int kHasNonInstancePrototype = 1;
3279 static const int kIsHiddenPrototype = 2;
3280 static const int kHasNamedInterceptor = 3;
3281 static const int kHasIndexedInterceptor = 4;
3282 static const int kIsUndetectable = 5;
3283 static const int kHasInstanceCallHandler = 6;
3284 static const int kIsAccessCheckNeeded = 7;
3285
3286 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003287 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003288 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003289 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003290 static const int kStringWrapperSafeForDefaultValueOf = 3;
Steve Block6ded16b2010-05-10 14:33:55 +01003291
3292 // Layout of the default cache. It holds alternating name and code objects.
3293 static const int kCodeCacheEntrySize = 2;
3294 static const int kCodeCacheEntryNameOffset = 0;
3295 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003296
Iain Merrick75681382010-08-19 15:07:18 +01003297 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3298 kPointerFieldsEndOffset,
3299 kSize> BodyDescriptor;
3300
Steve Blocka7e24c12009-10-30 11:49:00 +00003301 private:
3302 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3303};
3304
3305
3306// An abstract superclass, a marker class really, for simple structure classes.
3307// It doesn't carry much functionality but allows struct classes to me
3308// identified in the type system.
3309class Struct: public HeapObject {
3310 public:
3311 inline void InitializeBody(int object_size);
3312 static inline Struct* cast(Object* that);
3313};
3314
3315
3316// Script describes a script which has been added to the VM.
3317class Script: public Struct {
3318 public:
3319 // Script types.
3320 enum Type {
3321 TYPE_NATIVE = 0,
3322 TYPE_EXTENSION = 1,
3323 TYPE_NORMAL = 2
3324 };
3325
3326 // Script compilation types.
3327 enum CompilationType {
3328 COMPILATION_TYPE_HOST = 0,
3329 COMPILATION_TYPE_EVAL = 1,
3330 COMPILATION_TYPE_JSON = 2
3331 };
3332
3333 // [source]: the script source.
3334 DECL_ACCESSORS(source, Object)
3335
3336 // [name]: the script name.
3337 DECL_ACCESSORS(name, Object)
3338
3339 // [id]: the script id.
3340 DECL_ACCESSORS(id, Object)
3341
3342 // [line_offset]: script line offset in resource from where it was extracted.
3343 DECL_ACCESSORS(line_offset, Smi)
3344
3345 // [column_offset]: script column offset in resource from where it was
3346 // extracted.
3347 DECL_ACCESSORS(column_offset, Smi)
3348
3349 // [data]: additional data associated with this script.
3350 DECL_ACCESSORS(data, Object)
3351
3352 // [context_data]: context data for the context this script was compiled in.
3353 DECL_ACCESSORS(context_data, Object)
3354
3355 // [wrapper]: the wrapper cache.
3356 DECL_ACCESSORS(wrapper, Proxy)
3357
3358 // [type]: the script type.
3359 DECL_ACCESSORS(type, Smi)
3360
3361 // [compilation]: how the the script was compiled.
3362 DECL_ACCESSORS(compilation_type, Smi)
3363
Steve Blockd0582a62009-12-15 09:54:21 +00003364 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003365 DECL_ACCESSORS(line_ends, Object)
3366
Steve Blockd0582a62009-12-15 09:54:21 +00003367 // [eval_from_shared]: for eval scripts the shared funcion info for the
3368 // function from which eval was called.
3369 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003370
3371 // [eval_from_instructions_offset]: the instruction offset in the code for the
3372 // function from which eval was called where eval was called.
3373 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3374
3375 static inline Script* cast(Object* obj);
3376
Steve Block3ce2e202009-11-05 08:53:23 +00003377 // If script source is an external string, check that the underlying
3378 // resource is accessible. Otherwise, always return true.
3379 inline bool HasValidSource();
3380
Steve Blocka7e24c12009-10-30 11:49:00 +00003381#ifdef DEBUG
3382 void ScriptPrint();
3383 void ScriptVerify();
3384#endif
3385
3386 static const int kSourceOffset = HeapObject::kHeaderSize;
3387 static const int kNameOffset = kSourceOffset + kPointerSize;
3388 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3389 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3390 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3391 static const int kContextOffset = kDataOffset + kPointerSize;
3392 static const int kWrapperOffset = kContextOffset + kPointerSize;
3393 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3394 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3395 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3396 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003397 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003398 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003399 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003400 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3401
3402 private:
3403 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3404};
3405
3406
3407// SharedFunctionInfo describes the JSFunction information that can be
3408// shared by multiple instances of the function.
3409class SharedFunctionInfo: public HeapObject {
3410 public:
3411 // [name]: Function name.
3412 DECL_ACCESSORS(name, Object)
3413
3414 // [code]: Function code.
3415 DECL_ACCESSORS(code, Code)
3416
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003417 // [scope_info]: Scope info.
3418 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
3419
Steve Blocka7e24c12009-10-30 11:49:00 +00003420 // [construct stub]: Code stub for constructing instances of this function.
3421 DECL_ACCESSORS(construct_stub, Code)
3422
Iain Merrick75681382010-08-19 15:07:18 +01003423 inline Code* unchecked_code();
3424
Steve Blocka7e24c12009-10-30 11:49:00 +00003425 // Returns if this function has been compiled to native code yet.
3426 inline bool is_compiled();
3427
3428 // [length]: The function length - usually the number of declared parameters.
3429 // Use up to 2^30 parameters.
3430 inline int length();
3431 inline void set_length(int value);
3432
3433 // [formal parameter count]: The declared number of parameters.
3434 inline int formal_parameter_count();
3435 inline void set_formal_parameter_count(int value);
3436
3437 // Set the formal parameter count so the function code will be
3438 // called without using argument adaptor frames.
3439 inline void DontAdaptArguments();
3440
3441 // [expected_nof_properties]: Expected number of properties for the function.
3442 inline int expected_nof_properties();
3443 inline void set_expected_nof_properties(int value);
3444
3445 // [instance class name]: class name for instances.
3446 DECL_ACCESSORS(instance_class_name, Object)
3447
Steve Block6ded16b2010-05-10 14:33:55 +01003448 // [function data]: This field holds some additional data for function.
3449 // Currently it either has FunctionTemplateInfo to make benefit the API
Kristian Monsen25f61362010-05-21 11:50:48 +01003450 // or Smi identifying a custom call generator.
Steve Blocka7e24c12009-10-30 11:49:00 +00003451 // In the long run we don't want all functions to have this field but
3452 // we can fix that when we have a better model for storing hidden data
3453 // on objects.
3454 DECL_ACCESSORS(function_data, Object)
3455
Steve Block6ded16b2010-05-10 14:33:55 +01003456 inline bool IsApiFunction();
3457 inline FunctionTemplateInfo* get_api_func_data();
3458 inline bool HasCustomCallGenerator();
Kristian Monsen25f61362010-05-21 11:50:48 +01003459 inline int custom_call_generator_id();
Steve Block6ded16b2010-05-10 14:33:55 +01003460
Steve Blocka7e24c12009-10-30 11:49:00 +00003461 // [script info]: Script from which the function originates.
3462 DECL_ACCESSORS(script, Object)
3463
Steve Block6ded16b2010-05-10 14:33:55 +01003464 // [num_literals]: Number of literals used by this function.
3465 inline int num_literals();
3466 inline void set_num_literals(int value);
3467
Steve Blocka7e24c12009-10-30 11:49:00 +00003468 // [start_position_and_type]: Field used to store both the source code
3469 // position, whether or not the function is a function expression,
3470 // and whether or not the function is a toplevel function. The two
3471 // least significants bit indicates whether the function is an
3472 // expression and the rest contains the source code position.
3473 inline int start_position_and_type();
3474 inline void set_start_position_and_type(int value);
3475
3476 // [debug info]: Debug information.
3477 DECL_ACCESSORS(debug_info, Object)
3478
3479 // [inferred name]: Name inferred from variable or property
3480 // assignment of this function. Used to facilitate debugging and
3481 // profiling of JavaScript code written in OO style, where almost
3482 // all functions are anonymous but are assigned to object
3483 // properties.
3484 DECL_ACCESSORS(inferred_name, String)
3485
3486 // Position of the 'function' token in the script source.
3487 inline int function_token_position();
3488 inline void set_function_token_position(int function_token_position);
3489
3490 // Position of this function in the script source.
3491 inline int start_position();
3492 inline void set_start_position(int start_position);
3493
3494 // End position of this function in the script source.
3495 inline int end_position();
3496 inline void set_end_position(int end_position);
3497
3498 // Is this function a function expression in the source code.
3499 inline bool is_expression();
3500 inline void set_is_expression(bool value);
3501
3502 // Is this function a top-level function (scripts, evals).
3503 inline bool is_toplevel();
3504 inline void set_is_toplevel(bool value);
3505
3506 // Bit field containing various information collected by the compiler to
3507 // drive optimization.
3508 inline int compiler_hints();
3509 inline void set_compiler_hints(int value);
3510
3511 // Add information on assignments of the form this.x = ...;
3512 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00003513 bool has_only_simple_this_property_assignments,
3514 FixedArray* this_property_assignments);
3515
3516 // Clear information on assignments of the form this.x = ...;
3517 void ClearThisPropertyAssignmentsInfo();
3518
3519 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00003520 // this.x = y; where y is either a constant or refers to an argument.
3521 inline bool has_only_simple_this_property_assignments();
3522
Leon Clarked91b9f72010-01-27 17:25:45 +00003523 inline bool try_full_codegen();
3524 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00003525
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003526 // Indicates if this function can be lazy compiled.
3527 // This is used to determine if we can safely flush code from a function
3528 // when doing GC if we expect that the function will no longer be used.
3529 inline bool allows_lazy_compilation();
3530 inline void set_allows_lazy_compilation(bool flag);
3531
Iain Merrick75681382010-08-19 15:07:18 +01003532 // Indicates how many full GCs this function has survived with assigned
3533 // code object. Used to determine when it is relatively safe to flush
3534 // this code object and replace it with lazy compilation stub.
3535 // Age is reset when GC notices that the code object is referenced
3536 // from the stack or compilation cache.
3537 inline int code_age();
3538 inline void set_code_age(int age);
3539
3540
Andrei Popescu402d9372010-02-26 13:31:12 +00003541 // Check whether a inlined constructor can be generated with the given
3542 // prototype.
3543 bool CanGenerateInlineConstructor(Object* prototype);
3544
Steve Blocka7e24c12009-10-30 11:49:00 +00003545 // For functions which only contains this property assignments this provides
3546 // access to the names for the properties assigned.
3547 DECL_ACCESSORS(this_property_assignments, Object)
3548 inline int this_property_assignments_count();
3549 inline void set_this_property_assignments_count(int value);
3550 String* GetThisPropertyAssignmentName(int index);
3551 bool IsThisPropertyAssignmentArgument(int index);
3552 int GetThisPropertyAssignmentArgument(int index);
3553 Object* GetThisPropertyAssignmentConstant(int index);
3554
3555 // [source code]: Source code for the function.
3556 bool HasSourceCode();
3557 Object* GetSourceCode();
3558
3559 // Calculate the instance size.
3560 int CalculateInstanceSize();
3561
3562 // Calculate the number of in-object properties.
3563 int CalculateInObjectProperties();
3564
3565 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00003566 // Set max_length to -1 for unlimited length.
3567 void SourceCodePrint(StringStream* accumulator, int max_length);
3568#ifdef DEBUG
3569 void SharedFunctionInfoPrint();
3570 void SharedFunctionInfoVerify();
3571#endif
3572
3573 // Casting.
3574 static inline SharedFunctionInfo* cast(Object* obj);
3575
3576 // Constants.
3577 static const int kDontAdaptArgumentsSentinel = -1;
3578
3579 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01003580 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00003581 static const int kNameOffset = HeapObject::kHeaderSize;
3582 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003583 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
3584 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003585 static const int kInstanceClassNameOffset =
3586 kConstructStubOffset + kPointerSize;
3587 static const int kFunctionDataOffset =
3588 kInstanceClassNameOffset + kPointerSize;
3589 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
3590 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
3591 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
3592 static const int kThisPropertyAssignmentsOffset =
3593 kInferredNameOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003594#if V8_HOST_ARCH_32_BIT
3595 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01003596 static const int kLengthOffset =
3597 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003598 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
3599 static const int kExpectedNofPropertiesOffset =
3600 kFormalParameterCountOffset + kPointerSize;
3601 static const int kNumLiteralsOffset =
3602 kExpectedNofPropertiesOffset + kPointerSize;
3603 static const int kStartPositionAndTypeOffset =
3604 kNumLiteralsOffset + kPointerSize;
3605 static const int kEndPositionOffset =
3606 kStartPositionAndTypeOffset + kPointerSize;
3607 static const int kFunctionTokenPositionOffset =
3608 kEndPositionOffset + kPointerSize;
3609 static const int kCompilerHintsOffset =
3610 kFunctionTokenPositionOffset + kPointerSize;
3611 static const int kThisPropertyAssignmentsCountOffset =
3612 kCompilerHintsOffset + kPointerSize;
3613 // Total size.
3614 static const int kSize = kThisPropertyAssignmentsCountOffset + kPointerSize;
3615#else
3616 // The only reason to use smi fields instead of int fields
3617 // is to allow interation without maps decoding during
3618 // garbage collections.
3619 // To avoid wasting space on 64-bit architectures we use
3620 // the following trick: we group integer fields into pairs
3621 // First integer in each pair is shifted left by 1.
3622 // By doing this we guarantee that LSB of each kPointerSize aligned
3623 // word is not set and thus this word cannot be treated as pointer
3624 // to HeapObject during old space traversal.
3625 static const int kLengthOffset =
3626 kThisPropertyAssignmentsOffset + kPointerSize;
3627 static const int kFormalParameterCountOffset =
3628 kLengthOffset + kIntSize;
3629
Steve Blocka7e24c12009-10-30 11:49:00 +00003630 static const int kExpectedNofPropertiesOffset =
3631 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003632 static const int kNumLiteralsOffset =
3633 kExpectedNofPropertiesOffset + kIntSize;
3634
3635 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003636 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003637 static const int kStartPositionAndTypeOffset =
3638 kEndPositionOffset + kIntSize;
3639
3640 static const int kFunctionTokenPositionOffset =
3641 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003642 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00003643 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003644
Steve Blocka7e24c12009-10-30 11:49:00 +00003645 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003646 kCompilerHintsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003647
Steve Block6ded16b2010-05-10 14:33:55 +01003648 // Total size.
3649 static const int kSize = kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003650
3651#endif
Steve Block6ded16b2010-05-10 14:33:55 +01003652 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003653
Iain Merrick75681382010-08-19 15:07:18 +01003654 typedef FixedBodyDescriptor<kNameOffset,
3655 kThisPropertyAssignmentsOffset + kPointerSize,
3656 kSize> BodyDescriptor;
3657
Steve Blocka7e24c12009-10-30 11:49:00 +00003658 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00003659 // Bit positions in start_position_and_type.
3660 // The source code start position is in the 30 most significant bits of
3661 // the start_position_and_type field.
3662 static const int kIsExpressionBit = 0;
3663 static const int kIsTopLevelBit = 1;
3664 static const int kStartPositionShift = 2;
3665 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
3666
3667 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00003668 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00003669 static const int kTryFullCodegen = 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003670 static const int kAllowLazyCompilation = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003671 static const int kCodeAgeShift = 3;
3672 static const int kCodeAgeMask = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +00003673
3674 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
3675};
3676
3677
3678// JSFunction describes JavaScript functions.
3679class JSFunction: public JSObject {
3680 public:
3681 // [prototype_or_initial_map]:
3682 DECL_ACCESSORS(prototype_or_initial_map, Object)
3683
3684 // [shared_function_info]: The information about the function that
3685 // can be shared by instances.
3686 DECL_ACCESSORS(shared, SharedFunctionInfo)
3687
Iain Merrick75681382010-08-19 15:07:18 +01003688 inline SharedFunctionInfo* unchecked_shared();
3689
Steve Blocka7e24c12009-10-30 11:49:00 +00003690 // [context]: The context for this function.
3691 inline Context* context();
3692 inline Object* unchecked_context();
3693 inline void set_context(Object* context);
3694
3695 // [code]: The generated code object for this function. Executed
3696 // when the function is invoked, e.g. foo() or new foo(). See
3697 // [[Call]] and [[Construct]] description in ECMA-262, section
3698 // 8.6.2, page 27.
3699 inline Code* code();
3700 inline void set_code(Code* value);
3701
Iain Merrick75681382010-08-19 15:07:18 +01003702 inline Code* unchecked_code();
3703
Steve Blocka7e24c12009-10-30 11:49:00 +00003704 // Tells whether this function is builtin.
3705 inline bool IsBuiltin();
3706
3707 // [literals]: Fixed array holding the materialized literals.
3708 //
3709 // If the function contains object, regexp or array literals, the
3710 // literals array prefix contains the object, regexp, and array
3711 // function to be used when creating these literals. This is
3712 // necessary so that we do not dynamically lookup the object, regexp
3713 // or array functions. Performing a dynamic lookup, we might end up
3714 // using the functions from a new context that we should not have
3715 // access to.
3716 DECL_ACCESSORS(literals, FixedArray)
3717
3718 // The initial map for an object created by this constructor.
3719 inline Map* initial_map();
3720 inline void set_initial_map(Map* value);
3721 inline bool has_initial_map();
3722
3723 // Get and set the prototype property on a JSFunction. If the
3724 // function has an initial map the prototype is set on the initial
3725 // map. Otherwise, the prototype is put in the initial map field
3726 // until an initial map is needed.
3727 inline bool has_prototype();
3728 inline bool has_instance_prototype();
3729 inline Object* prototype();
3730 inline Object* instance_prototype();
3731 Object* SetInstancePrototype(Object* value);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003732 MUST_USE_RESULT Object* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003733
Steve Block6ded16b2010-05-10 14:33:55 +01003734 // After prototype is removed, it will not be created when accessed, and
3735 // [[Construct]] from this function will not be allowed.
3736 Object* RemovePrototype();
3737 inline bool should_have_prototype();
3738
Steve Blocka7e24c12009-10-30 11:49:00 +00003739 // Accessor for this function's initial map's [[class]]
3740 // property. This is primarily used by ECMA native functions. This
3741 // method sets the class_name field of this function's initial map
3742 // to a given value. It creates an initial map if this function does
3743 // not have one. Note that this method does not copy the initial map
3744 // if it has one already, but simply replaces it with the new value.
3745 // Instances created afterwards will have a map whose [[class]] is
3746 // set to 'value', but there is no guarantees on instances created
3747 // before.
3748 Object* SetInstanceClassName(String* name);
3749
3750 // Returns if this function has been compiled to native code yet.
3751 inline bool is_compiled();
3752
3753 // Casting.
3754 static inline JSFunction* cast(Object* obj);
3755
Steve Block791712a2010-08-27 10:21:07 +01003756 // Iterates the objects, including code objects indirectly referenced
3757 // through pointers to the first instruction in the code object.
3758 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
3759
Steve Blocka7e24c12009-10-30 11:49:00 +00003760 // Dispatched behavior.
3761#ifdef DEBUG
3762 void JSFunctionPrint();
3763 void JSFunctionVerify();
3764#endif
3765
3766 // Returns the number of allocated literals.
3767 inline int NumberOfLiterals();
3768
3769 // Retrieve the global context from a function's literal array.
3770 static Context* GlobalContextFromLiterals(FixedArray* literals);
3771
3772 // Layout descriptors.
Steve Block791712a2010-08-27 10:21:07 +01003773 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01003774 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01003775 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003776 static const int kSharedFunctionInfoOffset =
3777 kPrototypeOrInitialMapOffset + kPointerSize;
3778 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
3779 static const int kLiteralsOffset = kContextOffset + kPointerSize;
3780 static const int kSize = kLiteralsOffset + kPointerSize;
3781
3782 // Layout of the literals array.
3783 static const int kLiteralsPrefixSize = 1;
3784 static const int kLiteralGlobalContextIndex = 0;
3785 private:
3786 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
3787};
3788
3789
3790// JSGlobalProxy's prototype must be a JSGlobalObject or null,
3791// and the prototype is hidden. JSGlobalProxy always delegates
3792// property accesses to its prototype if the prototype is not null.
3793//
3794// A JSGlobalProxy can be reinitialized which will preserve its identity.
3795//
3796// Accessing a JSGlobalProxy requires security check.
3797
3798class JSGlobalProxy : public JSObject {
3799 public:
3800 // [context]: the owner global context of this proxy object.
3801 // It is null value if this object is not used by any context.
3802 DECL_ACCESSORS(context, Object)
3803
3804 // Casting.
3805 static inline JSGlobalProxy* cast(Object* obj);
3806
3807 // Dispatched behavior.
3808#ifdef DEBUG
3809 void JSGlobalProxyPrint();
3810 void JSGlobalProxyVerify();
3811#endif
3812
3813 // Layout description.
3814 static const int kContextOffset = JSObject::kHeaderSize;
3815 static const int kSize = kContextOffset + kPointerSize;
3816
3817 private:
3818
3819 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
3820};
3821
3822
3823// Forward declaration.
3824class JSBuiltinsObject;
3825
3826// Common super class for JavaScript global objects and the special
3827// builtins global objects.
3828class GlobalObject: public JSObject {
3829 public:
3830 // [builtins]: the object holding the runtime routines written in JS.
3831 DECL_ACCESSORS(builtins, JSBuiltinsObject)
3832
3833 // [global context]: the global context corresponding to this global object.
3834 DECL_ACCESSORS(global_context, Context)
3835
3836 // [global receiver]: the global receiver object of the context
3837 DECL_ACCESSORS(global_receiver, JSObject)
3838
3839 // Retrieve the property cell used to store a property.
3840 Object* GetPropertyCell(LookupResult* result);
3841
3842 // Ensure that the global object has a cell for the given property name.
3843 Object* EnsurePropertyCell(String* name);
3844
3845 // Casting.
3846 static inline GlobalObject* cast(Object* obj);
3847
3848 // Layout description.
3849 static const int kBuiltinsOffset = JSObject::kHeaderSize;
3850 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
3851 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
3852 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
3853
3854 private:
3855 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
3856
3857 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
3858};
3859
3860
3861// JavaScript global object.
3862class JSGlobalObject: public GlobalObject {
3863 public:
3864
3865 // Casting.
3866 static inline JSGlobalObject* cast(Object* obj);
3867
3868 // Dispatched behavior.
3869#ifdef DEBUG
3870 void JSGlobalObjectPrint();
3871 void JSGlobalObjectVerify();
3872#endif
3873
3874 // Layout description.
3875 static const int kSize = GlobalObject::kHeaderSize;
3876
3877 private:
3878 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
3879};
3880
3881
3882// Builtins global object which holds the runtime routines written in
3883// JavaScript.
3884class JSBuiltinsObject: public GlobalObject {
3885 public:
3886 // Accessors for the runtime routines written in JavaScript.
3887 inline Object* javascript_builtin(Builtins::JavaScript id);
3888 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
3889
Steve Block6ded16b2010-05-10 14:33:55 +01003890 // Accessors for code of the runtime routines written in JavaScript.
3891 inline Code* javascript_builtin_code(Builtins::JavaScript id);
3892 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
3893
Steve Blocka7e24c12009-10-30 11:49:00 +00003894 // Casting.
3895 static inline JSBuiltinsObject* cast(Object* obj);
3896
3897 // Dispatched behavior.
3898#ifdef DEBUG
3899 void JSBuiltinsObjectPrint();
3900 void JSBuiltinsObjectVerify();
3901#endif
3902
3903 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01003904 // room for two pointers per runtime routine written in javascript
3905 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00003906 static const int kJSBuiltinsCount = Builtins::id_count;
3907 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003908 static const int kJSBuiltinsCodeOffset =
3909 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003910 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01003911 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
3912
3913 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
3914 return kJSBuiltinsOffset + id * kPointerSize;
3915 }
3916
3917 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
3918 return kJSBuiltinsCodeOffset + id * kPointerSize;
3919 }
3920
Steve Blocka7e24c12009-10-30 11:49:00 +00003921 private:
3922 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
3923};
3924
3925
3926// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
3927class JSValue: public JSObject {
3928 public:
3929 // [value]: the object being wrapped.
3930 DECL_ACCESSORS(value, Object)
3931
3932 // Casting.
3933 static inline JSValue* cast(Object* obj);
3934
3935 // Dispatched behavior.
3936#ifdef DEBUG
3937 void JSValuePrint();
3938 void JSValueVerify();
3939#endif
3940
3941 // Layout description.
3942 static const int kValueOffset = JSObject::kHeaderSize;
3943 static const int kSize = kValueOffset + kPointerSize;
3944
3945 private:
3946 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
3947};
3948
3949// Regular expressions
3950// The regular expression holds a single reference to a FixedArray in
3951// the kDataOffset field.
3952// The FixedArray contains the following data:
3953// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
3954// - reference to the original source string
3955// - reference to the original flag string
3956// If it is an atom regexp
3957// - a reference to a literal string to search for
3958// If it is an irregexp regexp:
3959// - a reference to code for ASCII inputs (bytecode or compiled).
3960// - a reference to code for UC16 inputs (bytecode or compiled).
3961// - max number of registers used by irregexp implementations.
3962// - number of capture registers (output values) of the regexp.
3963class JSRegExp: public JSObject {
3964 public:
3965 // Meaning of Type:
3966 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
3967 // ATOM: A simple string to match against using an indexOf operation.
3968 // IRREGEXP: Compiled with Irregexp.
3969 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
3970 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
3971 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
3972
3973 class Flags {
3974 public:
3975 explicit Flags(uint32_t value) : value_(value) { }
3976 bool is_global() { return (value_ & GLOBAL) != 0; }
3977 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
3978 bool is_multiline() { return (value_ & MULTILINE) != 0; }
3979 uint32_t value() { return value_; }
3980 private:
3981 uint32_t value_;
3982 };
3983
3984 DECL_ACCESSORS(data, Object)
3985
3986 inline Type TypeTag();
3987 inline int CaptureCount();
3988 inline Flags GetFlags();
3989 inline String* Pattern();
3990 inline Object* DataAt(int index);
3991 // Set implementation data after the object has been prepared.
3992 inline void SetDataAt(int index, Object* value);
3993 static int code_index(bool is_ascii) {
3994 if (is_ascii) {
3995 return kIrregexpASCIICodeIndex;
3996 } else {
3997 return kIrregexpUC16CodeIndex;
3998 }
3999 }
4000
4001 static inline JSRegExp* cast(Object* obj);
4002
4003 // Dispatched behavior.
4004#ifdef DEBUG
4005 void JSRegExpVerify();
4006#endif
4007
4008 static const int kDataOffset = JSObject::kHeaderSize;
4009 static const int kSize = kDataOffset + kPointerSize;
4010
4011 // Indices in the data array.
4012 static const int kTagIndex = 0;
4013 static const int kSourceIndex = kTagIndex + 1;
4014 static const int kFlagsIndex = kSourceIndex + 1;
4015 static const int kDataIndex = kFlagsIndex + 1;
4016 // The data fields are used in different ways depending on the
4017 // value of the tag.
4018 // Atom regexps (literal strings).
4019 static const int kAtomPatternIndex = kDataIndex;
4020
4021 static const int kAtomDataSize = kAtomPatternIndex + 1;
4022
4023 // Irregexp compiled code or bytecode for ASCII. If compilation
4024 // fails, this fields hold an exception object that should be
4025 // thrown if the regexp is used again.
4026 static const int kIrregexpASCIICodeIndex = kDataIndex;
4027 // Irregexp compiled code or bytecode for UC16. If compilation
4028 // fails, this fields hold an exception object that should be
4029 // thrown if the regexp is used again.
4030 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
4031 // Maximal number of registers used by either ASCII or UC16.
4032 // Only used to check that there is enough stack space
4033 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
4034 // Number of captures in the compiled regexp.
4035 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
4036
4037 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00004038
4039 // Offsets directly into the data fixed array.
4040 static const int kDataTagOffset =
4041 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
4042 static const int kDataAsciiCodeOffset =
4043 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00004044 static const int kDataUC16CodeOffset =
4045 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00004046 static const int kIrregexpCaptureCountOffset =
4047 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004048
4049 // In-object fields.
4050 static const int kSourceFieldIndex = 0;
4051 static const int kGlobalFieldIndex = 1;
4052 static const int kIgnoreCaseFieldIndex = 2;
4053 static const int kMultilineFieldIndex = 3;
4054 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01004055 static const int kInObjectFieldCount = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00004056};
4057
4058
4059class CompilationCacheShape {
4060 public:
4061 static inline bool IsMatch(HashTableKey* key, Object* value) {
4062 return key->IsMatch(value);
4063 }
4064
4065 static inline uint32_t Hash(HashTableKey* key) {
4066 return key->Hash();
4067 }
4068
4069 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4070 return key->HashForObject(object);
4071 }
4072
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004073 MUST_USE_RESULT static Object* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004074 return key->AsObject();
4075 }
4076
4077 static const int kPrefixSize = 0;
4078 static const int kEntrySize = 2;
4079};
4080
Steve Block3ce2e202009-11-05 08:53:23 +00004081
Steve Blocka7e24c12009-10-30 11:49:00 +00004082class CompilationCacheTable: public HashTable<CompilationCacheShape,
4083 HashTableKey*> {
4084 public:
4085 // Find cached value for a string key, otherwise return null.
4086 Object* Lookup(String* src);
4087 Object* LookupEval(String* src, Context* context);
4088 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
4089 Object* Put(String* src, Object* value);
4090 Object* PutEval(String* src, Context* context, Object* value);
4091 Object* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
4092
4093 static inline CompilationCacheTable* cast(Object* obj);
4094
4095 private:
4096 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
4097};
4098
4099
Steve Block6ded16b2010-05-10 14:33:55 +01004100class CodeCache: public Struct {
4101 public:
4102 DECL_ACCESSORS(default_cache, FixedArray)
4103 DECL_ACCESSORS(normal_type_cache, Object)
4104
4105 // Add the code object to the cache.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004106 MUST_USE_RESULT Object* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004107
4108 // Lookup code object in the cache. Returns code object if found and undefined
4109 // if not.
4110 Object* Lookup(String* name, Code::Flags flags);
4111
4112 // Get the internal index of a code object in the cache. Returns -1 if the
4113 // code object is not in that cache. This index can be used to later call
4114 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
4115 // RemoveByIndex.
4116 int GetIndex(Object* name, Code* code);
4117
4118 // Remove an object from the cache with the provided internal index.
4119 void RemoveByIndex(Object* name, Code* code, int index);
4120
4121 static inline CodeCache* cast(Object* obj);
4122
4123#ifdef DEBUG
4124 void CodeCachePrint();
4125 void CodeCacheVerify();
4126#endif
4127
4128 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
4129 static const int kNormalTypeCacheOffset =
4130 kDefaultCacheOffset + kPointerSize;
4131 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
4132
4133 private:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004134 MUST_USE_RESULT Object* UpdateDefaultCache(String* name, Code* code);
4135 MUST_USE_RESULT Object* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004136 Object* LookupDefaultCache(String* name, Code::Flags flags);
4137 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
4138
4139 // Code cache layout of the default cache. Elements are alternating name and
4140 // code objects for non normal load/store/call IC's.
4141 static const int kCodeCacheEntrySize = 2;
4142 static const int kCodeCacheEntryNameOffset = 0;
4143 static const int kCodeCacheEntryCodeOffset = 1;
4144
4145 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
4146};
4147
4148
4149class CodeCacheHashTableShape {
4150 public:
4151 static inline bool IsMatch(HashTableKey* key, Object* value) {
4152 return key->IsMatch(value);
4153 }
4154
4155 static inline uint32_t Hash(HashTableKey* key) {
4156 return key->Hash();
4157 }
4158
4159 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4160 return key->HashForObject(object);
4161 }
4162
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004163 MUST_USE_RESULT static Object* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01004164 return key->AsObject();
4165 }
4166
4167 static const int kPrefixSize = 0;
4168 static const int kEntrySize = 2;
4169};
4170
4171
4172class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
4173 HashTableKey*> {
4174 public:
4175 Object* Lookup(String* name, Code::Flags flags);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004176 MUST_USE_RESULT Object* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004177
4178 int GetIndex(String* name, Code::Flags flags);
4179 void RemoveByIndex(int index);
4180
4181 static inline CodeCacheHashTable* cast(Object* obj);
4182
4183 // Initial size of the fixed array backing the hash table.
4184 static const int kInitialSize = 64;
4185
4186 private:
4187 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
4188};
4189
4190
Steve Blocka7e24c12009-10-30 11:49:00 +00004191enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
4192enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
4193
4194
4195class StringHasher {
4196 public:
4197 inline StringHasher(int length);
4198
4199 // Returns true if the hash of this string can be computed without
4200 // looking at the contents.
4201 inline bool has_trivial_hash();
4202
4203 // Add a character to the hash and update the array index calculation.
4204 inline void AddCharacter(uc32 c);
4205
4206 // Adds a character to the hash but does not update the array index
4207 // calculation. This can only be called when it has been verified
4208 // that the input is not an array index.
4209 inline void AddCharacterNoIndex(uc32 c);
4210
4211 // Returns the value to store in the hash field of a string with
4212 // the given length and contents.
4213 uint32_t GetHashField();
4214
4215 // Returns true if the characters seen so far make up a legal array
4216 // index.
4217 bool is_array_index() { return is_array_index_; }
4218
4219 bool is_valid() { return is_valid_; }
4220
4221 void invalidate() { is_valid_ = false; }
4222
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004223 // Calculated hash value for a string consisting of 1 to
4224 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
4225 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01004226 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004227
Steve Blocka7e24c12009-10-30 11:49:00 +00004228 private:
4229
4230 uint32_t array_index() {
4231 ASSERT(is_array_index());
4232 return array_index_;
4233 }
4234
4235 inline uint32_t GetHash();
4236
4237 int length_;
4238 uint32_t raw_running_hash_;
4239 uint32_t array_index_;
4240 bool is_array_index_;
4241 bool is_first_char_;
4242 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00004243 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00004244};
4245
4246
4247// The characteristics of a string are stored in its map. Retrieving these
4248// few bits of information is moderately expensive, involving two memory
4249// loads where the second is dependent on the first. To improve efficiency
4250// the shape of the string is given its own class so that it can be retrieved
4251// once and used for several string operations. A StringShape is small enough
4252// to be passed by value and is immutable, but be aware that flattening a
4253// string can potentially alter its shape. Also be aware that a GC caused by
4254// something else can alter the shape of a string due to ConsString
4255// shortcutting. Keeping these restrictions in mind has proven to be error-
4256// prone and so we no longer put StringShapes in variables unless there is a
4257// concrete performance benefit at that particular point in the code.
4258class StringShape BASE_EMBEDDED {
4259 public:
4260 inline explicit StringShape(String* s);
4261 inline explicit StringShape(Map* s);
4262 inline explicit StringShape(InstanceType t);
4263 inline bool IsSequential();
4264 inline bool IsExternal();
4265 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00004266 inline bool IsExternalAscii();
4267 inline bool IsExternalTwoByte();
4268 inline bool IsSequentialAscii();
4269 inline bool IsSequentialTwoByte();
4270 inline bool IsSymbol();
4271 inline StringRepresentationTag representation_tag();
4272 inline uint32_t full_representation_tag();
4273 inline uint32_t size_tag();
4274#ifdef DEBUG
4275 inline uint32_t type() { return type_; }
4276 inline void invalidate() { valid_ = false; }
4277 inline bool valid() { return valid_; }
4278#else
4279 inline void invalidate() { }
4280#endif
4281 private:
4282 uint32_t type_;
4283#ifdef DEBUG
4284 inline void set_valid() { valid_ = true; }
4285 bool valid_;
4286#else
4287 inline void set_valid() { }
4288#endif
4289};
4290
4291
4292// The String abstract class captures JavaScript string values:
4293//
4294// Ecma-262:
4295// 4.3.16 String Value
4296// A string value is a member of the type String and is a finite
4297// ordered sequence of zero or more 16-bit unsigned integer values.
4298//
4299// All string values have a length field.
4300class String: public HeapObject {
4301 public:
4302 // Get and set the length of the string.
4303 inline int length();
4304 inline void set_length(int value);
4305
Steve Blockd0582a62009-12-15 09:54:21 +00004306 // Get and set the hash field of the string.
4307 inline uint32_t hash_field();
4308 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004309
4310 inline bool IsAsciiRepresentation();
4311 inline bool IsTwoByteRepresentation();
4312
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004313 // Returns whether this string has ascii chars, i.e. all of them can
4314 // be ascii encoded. This might be the case even if the string is
4315 // two-byte. Such strings may appear when the embedder prefers
4316 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01004317 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004318 // NOTE: this should be considered only a hint. False negatives are
4319 // possible.
4320 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01004321
Steve Blocka7e24c12009-10-30 11:49:00 +00004322 // Get and set individual two byte chars in the string.
4323 inline void Set(int index, uint16_t value);
4324 // Get individual two byte char in the string. Repeated calls
4325 // to this method are not efficient unless the string is flat.
4326 inline uint16_t Get(int index);
4327
Leon Clarkef7060e22010-06-03 12:02:55 +01004328 // Try to flatten the string. Checks first inline to see if it is
4329 // necessary. Does nothing if the string is not a cons string.
4330 // Flattening allocates a sequential string with the same data as
4331 // the given string and mutates the cons string to a degenerate
4332 // form, where the first component is the new sequential string and
4333 // the second component is the empty string. If allocation fails,
4334 // this function returns a failure. If flattening succeeds, this
4335 // function returns the sequential string that is now the first
4336 // component of the cons string.
4337 //
4338 // Degenerate cons strings are handled specially by the garbage
4339 // collector (see IsShortcutCandidate).
4340 //
4341 // Use FlattenString from Handles.cc to flatten even in case an
4342 // allocation failure happens.
Steve Block6ded16b2010-05-10 14:33:55 +01004343 inline Object* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004344
Leon Clarkef7060e22010-06-03 12:02:55 +01004345 // Convenience function. Has exactly the same behavior as
4346 // TryFlatten(), except in the case of failure returns the original
4347 // string.
4348 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
4349
Steve Blocka7e24c12009-10-30 11:49:00 +00004350 Vector<const char> ToAsciiVector();
4351 Vector<const uc16> ToUC16Vector();
4352
4353 // Mark the string as an undetectable object. It only applies to
4354 // ascii and two byte string types.
4355 bool MarkAsUndetectable();
4356
Steve Blockd0582a62009-12-15 09:54:21 +00004357 // Return a substring.
Steve Block6ded16b2010-05-10 14:33:55 +01004358 Object* SubString(int from, int to, PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004359
4360 // String equality operations.
4361 inline bool Equals(String* other);
4362 bool IsEqualTo(Vector<const char> str);
4363
4364 // Return a UTF8 representation of the string. The string is null
4365 // terminated but may optionally contain nulls. Length is returned
4366 // in length_output if length_output is not a null pointer The string
4367 // should be nearly flat, otherwise the performance of this method may
4368 // be very slow (quadratic in the length). Setting robustness_flag to
4369 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4370 // handles unexpected data without causing assert failures and it does not
4371 // do any heap allocations. This is useful when printing stack traces.
4372 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
4373 RobustnessFlag robustness_flag,
4374 int offset,
4375 int length,
4376 int* length_output = 0);
4377 SmartPointer<char> ToCString(
4378 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
4379 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
4380 int* length_output = 0);
4381
4382 int Utf8Length();
4383
4384 // Return a 16 bit Unicode representation of the string.
4385 // The string should be nearly flat, otherwise the performance of
4386 // of this method may be very bad. Setting robustness_flag to
4387 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4388 // handles unexpected data without causing assert failures and it does not
4389 // do any heap allocations. This is useful when printing stack traces.
4390 SmartPointer<uc16> ToWideCString(
4391 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
4392
4393 // Tells whether the hash code has been computed.
4394 inline bool HasHashCode();
4395
4396 // Returns a hash value used for the property table
4397 inline uint32_t Hash();
4398
Steve Blockd0582a62009-12-15 09:54:21 +00004399 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
4400 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00004401
4402 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
4403 uint32_t* index,
4404 int length);
4405
4406 // Externalization.
4407 bool MakeExternal(v8::String::ExternalStringResource* resource);
4408 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
4409
4410 // Conversion.
4411 inline bool AsArrayIndex(uint32_t* index);
4412
4413 // Casting.
4414 static inline String* cast(Object* obj);
4415
4416 void PrintOn(FILE* out);
4417
4418 // For use during stack traces. Performs rudimentary sanity check.
4419 bool LooksValid();
4420
4421 // Dispatched behavior.
4422 void StringShortPrint(StringStream* accumulator);
4423#ifdef DEBUG
4424 void StringPrint();
4425 void StringVerify();
4426#endif
4427 inline bool IsFlat();
4428
4429 // Layout description.
4430 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004431 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004432 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004433
Steve Blockd0582a62009-12-15 09:54:21 +00004434 // Maximum number of characters to consider when trying to convert a string
4435 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00004436 static const int kMaxArrayIndexSize = 10;
4437
4438 // Max ascii char code.
4439 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
4440 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
4441 static const int kMaxUC16CharCode = 0xffff;
4442
Steve Blockd0582a62009-12-15 09:54:21 +00004443 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00004444 static const int kMinNonFlatLength = 13;
4445
4446 // Mask constant for checking if a string has a computed hash code
4447 // and if it is an array index. The least significant bit indicates
4448 // whether a hash code has been computed. If the hash code has been
4449 // computed the 2nd bit tells whether the string can be used as an
4450 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004451 static const int kHashNotComputedMask = 1;
4452 static const int kIsNotArrayIndexMask = 1 << 1;
4453 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00004454
Steve Blockd0582a62009-12-15 09:54:21 +00004455 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004456 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00004457
Steve Blocka7e24c12009-10-30 11:49:00 +00004458 // Array index strings this short can keep their index in the hash
4459 // field.
4460 static const int kMaxCachedArrayIndexLength = 7;
4461
Steve Blockd0582a62009-12-15 09:54:21 +00004462 // For strings which are array indexes the hash value has the string length
4463 // mixed into the hash, mainly to avoid a hash value of zero which would be
4464 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004465 static const int kArrayIndexValueBits = 24;
4466 static const int kArrayIndexLengthBits =
4467 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
4468
4469 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01004470 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004471
4472 static const int kArrayIndexHashLengthShift =
4473 kArrayIndexValueBits + kNofHashBitFields;
4474
Steve Blockd0582a62009-12-15 09:54:21 +00004475 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004476
4477 static const int kArrayIndexValueMask =
4478 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
4479
4480 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
4481 // could use a mask to test if the length of string is less than or equal to
4482 // kMaxCachedArrayIndexLength.
4483 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
4484
4485 static const int kContainsCachedArrayIndexMask =
4486 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
4487 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004488
4489 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004490 static const int kEmptyHashField =
4491 kIsNotArrayIndexMask | kHashNotComputedMask;
4492
4493 // Value of hash field containing computed hash equal to zero.
4494 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004495
4496 // Maximal string length.
4497 static const int kMaxLength = (1 << (32 - 2)) - 1;
4498
4499 // Max length for computing hash. For strings longer than this limit the
4500 // string length is used as the hash value.
4501 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00004502
4503 // Limit for truncation in short printing.
4504 static const int kMaxShortPrintLength = 1024;
4505
4506 // Support for regular expressions.
4507 const uc16* GetTwoByteData();
4508 const uc16* GetTwoByteData(unsigned start);
4509
4510 // Support for StringInputBuffer
4511 static const unibrow::byte* ReadBlock(String* input,
4512 unibrow::byte* util_buffer,
4513 unsigned capacity,
4514 unsigned* remaining,
4515 unsigned* offset);
4516 static const unibrow::byte* ReadBlock(String** input,
4517 unibrow::byte* util_buffer,
4518 unsigned capacity,
4519 unsigned* remaining,
4520 unsigned* offset);
4521
4522 // Helper function for flattening strings.
4523 template <typename sinkchar>
4524 static void WriteToFlat(String* source,
4525 sinkchar* sink,
4526 int from,
4527 int to);
4528
4529 protected:
4530 class ReadBlockBuffer {
4531 public:
4532 ReadBlockBuffer(unibrow::byte* util_buffer_,
4533 unsigned cursor_,
4534 unsigned capacity_,
4535 unsigned remaining_) :
4536 util_buffer(util_buffer_),
4537 cursor(cursor_),
4538 capacity(capacity_),
4539 remaining(remaining_) {
4540 }
4541 unibrow::byte* util_buffer;
4542 unsigned cursor;
4543 unsigned capacity;
4544 unsigned remaining;
4545 };
4546
Steve Blocka7e24c12009-10-30 11:49:00 +00004547 static inline const unibrow::byte* ReadBlock(String* input,
4548 ReadBlockBuffer* buffer,
4549 unsigned* offset,
4550 unsigned max_chars);
4551 static void ReadBlockIntoBuffer(String* input,
4552 ReadBlockBuffer* buffer,
4553 unsigned* offset_ptr,
4554 unsigned max_chars);
4555
4556 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01004557 // Try to flatten the top level ConsString that is hiding behind this
4558 // string. This is a no-op unless the string is a ConsString. Flatten
4559 // mutates the ConsString and might return a failure.
4560 Object* SlowTryFlatten(PretenureFlag pretenure);
4561
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004562 static inline bool IsHashFieldComputed(uint32_t field);
4563
Steve Blocka7e24c12009-10-30 11:49:00 +00004564 // Slow case of String::Equals. This implementation works on any strings
4565 // but it is most efficient on strings that are almost flat.
4566 bool SlowEquals(String* other);
4567
4568 // Slow case of AsArrayIndex.
4569 bool SlowAsArrayIndex(uint32_t* index);
4570
4571 // Compute and set the hash code.
4572 uint32_t ComputeAndSetHash();
4573
4574 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
4575};
4576
4577
4578// The SeqString abstract class captures sequential string values.
4579class SeqString: public String {
4580 public:
4581
4582 // Casting.
4583 static inline SeqString* cast(Object* obj);
4584
Steve Blocka7e24c12009-10-30 11:49:00 +00004585 private:
4586 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
4587};
4588
4589
4590// The AsciiString class captures sequential ascii string objects.
4591// Each character in the AsciiString is an ascii character.
4592class SeqAsciiString: public SeqString {
4593 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004594 static const bool kHasAsciiEncoding = true;
4595
Steve Blocka7e24c12009-10-30 11:49:00 +00004596 // Dispatched behavior.
4597 inline uint16_t SeqAsciiStringGet(int index);
4598 inline void SeqAsciiStringSet(int index, uint16_t value);
4599
4600 // Get the address of the characters in this string.
4601 inline Address GetCharsAddress();
4602
4603 inline char* GetChars();
4604
4605 // Casting
4606 static inline SeqAsciiString* cast(Object* obj);
4607
4608 // Garbage collection support. This method is called by the
4609 // garbage collector to compute the actual size of an AsciiString
4610 // instance.
4611 inline int SeqAsciiStringSize(InstanceType instance_type);
4612
4613 // Computes the size for an AsciiString instance of a given length.
4614 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004615 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004616 }
4617
4618 // Layout description.
4619 static const int kHeaderSize = String::kSize;
4620 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4621
Leon Clarkee46be812010-01-19 14:06:41 +00004622 // Maximal memory usage for a single sequential ASCII string.
4623 static const int kMaxSize = 512 * MB;
4624 // Maximal length of a single sequential ASCII string.
4625 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4626 static const int kMaxLength = (kMaxSize - kHeaderSize);
4627
Steve Blocka7e24c12009-10-30 11:49:00 +00004628 // Support for StringInputBuffer.
4629 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4630 unsigned* offset,
4631 unsigned chars);
4632 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
4633 unsigned* offset,
4634 unsigned chars);
4635
4636 private:
4637 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
4638};
4639
4640
4641// The TwoByteString class captures sequential unicode string objects.
4642// Each character in the TwoByteString is a two-byte uint16_t.
4643class SeqTwoByteString: public SeqString {
4644 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004645 static const bool kHasAsciiEncoding = false;
4646
Steve Blocka7e24c12009-10-30 11:49:00 +00004647 // Dispatched behavior.
4648 inline uint16_t SeqTwoByteStringGet(int index);
4649 inline void SeqTwoByteStringSet(int index, uint16_t value);
4650
4651 // Get the address of the characters in this string.
4652 inline Address GetCharsAddress();
4653
4654 inline uc16* GetChars();
4655
4656 // For regexp code.
4657 const uint16_t* SeqTwoByteStringGetData(unsigned start);
4658
4659 // Casting
4660 static inline SeqTwoByteString* cast(Object* obj);
4661
4662 // Garbage collection support. This method is called by the
4663 // garbage collector to compute the actual size of a TwoByteString
4664 // instance.
4665 inline int SeqTwoByteStringSize(InstanceType instance_type);
4666
4667 // Computes the size for a TwoByteString instance of a given length.
4668 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004669 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004670 }
4671
4672 // Layout description.
4673 static const int kHeaderSize = String::kSize;
4674 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4675
Leon Clarkee46be812010-01-19 14:06:41 +00004676 // Maximal memory usage for a single sequential two-byte string.
4677 static const int kMaxSize = 512 * MB;
4678 // Maximal length of a single sequential two-byte string.
4679 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4680 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
4681
Steve Blocka7e24c12009-10-30 11:49:00 +00004682 // Support for StringInputBuffer.
4683 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4684 unsigned* offset_ptr,
4685 unsigned chars);
4686
4687 private:
4688 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
4689};
4690
4691
4692// The ConsString class describes string values built by using the
4693// addition operator on strings. A ConsString is a pair where the
4694// first and second components are pointers to other string values.
4695// One or both components of a ConsString can be pointers to other
4696// ConsStrings, creating a binary tree of ConsStrings where the leaves
4697// are non-ConsString string values. The string value represented by
4698// a ConsString can be obtained by concatenating the leaf string
4699// values in a left-to-right depth-first traversal of the tree.
4700class ConsString: public String {
4701 public:
4702 // First string of the cons cell.
4703 inline String* first();
4704 // Doesn't check that the result is a string, even in debug mode. This is
4705 // useful during GC where the mark bits confuse the checks.
4706 inline Object* unchecked_first();
4707 inline void set_first(String* first,
4708 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4709
4710 // Second string of the cons cell.
4711 inline String* second();
4712 // Doesn't check that the result is a string, even in debug mode. This is
4713 // useful during GC where the mark bits confuse the checks.
4714 inline Object* unchecked_second();
4715 inline void set_second(String* second,
4716 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4717
4718 // Dispatched behavior.
4719 uint16_t ConsStringGet(int index);
4720
4721 // Casting.
4722 static inline ConsString* cast(Object* obj);
4723
Steve Blocka7e24c12009-10-30 11:49:00 +00004724 // Layout description.
4725 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
4726 static const int kSecondOffset = kFirstOffset + kPointerSize;
4727 static const int kSize = kSecondOffset + kPointerSize;
4728
4729 // Support for StringInputBuffer.
4730 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
4731 unsigned* offset_ptr,
4732 unsigned chars);
4733 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4734 unsigned* offset_ptr,
4735 unsigned chars);
4736
4737 // Minimum length for a cons string.
4738 static const int kMinLength = 13;
4739
Iain Merrick75681382010-08-19 15:07:18 +01004740 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
4741 BodyDescriptor;
4742
Steve Blocka7e24c12009-10-30 11:49:00 +00004743 private:
4744 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
4745};
4746
4747
Steve Blocka7e24c12009-10-30 11:49:00 +00004748// The ExternalString class describes string values that are backed by
4749// a string resource that lies outside the V8 heap. ExternalStrings
4750// consist of the length field common to all strings, a pointer to the
4751// external resource. It is important to ensure (externally) that the
4752// resource is not deallocated while the ExternalString is live in the
4753// V8 heap.
4754//
4755// The API expects that all ExternalStrings are created through the
4756// API. Therefore, ExternalStrings should not be used internally.
4757class ExternalString: public String {
4758 public:
4759 // Casting
4760 static inline ExternalString* cast(Object* obj);
4761
4762 // Layout description.
4763 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
4764 static const int kSize = kResourceOffset + kPointerSize;
4765
4766 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
4767
4768 private:
4769 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
4770};
4771
4772
4773// The ExternalAsciiString class is an external string backed by an
4774// ASCII string.
4775class ExternalAsciiString: public ExternalString {
4776 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004777 static const bool kHasAsciiEncoding = true;
4778
Steve Blocka7e24c12009-10-30 11:49:00 +00004779 typedef v8::String::ExternalAsciiStringResource Resource;
4780
4781 // The underlying resource.
4782 inline Resource* resource();
4783 inline void set_resource(Resource* buffer);
4784
4785 // Dispatched behavior.
4786 uint16_t ExternalAsciiStringGet(int index);
4787
4788 // Casting.
4789 static inline ExternalAsciiString* cast(Object* obj);
4790
Steve Blockd0582a62009-12-15 09:54:21 +00004791 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01004792 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
4793
4794 template<typename StaticVisitor>
4795 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00004796
Steve Blocka7e24c12009-10-30 11:49:00 +00004797 // Support for StringInputBuffer.
4798 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
4799 unsigned* offset,
4800 unsigned chars);
4801 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4802 unsigned* offset,
4803 unsigned chars);
4804
Steve Blocka7e24c12009-10-30 11:49:00 +00004805 private:
4806 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
4807};
4808
4809
4810// The ExternalTwoByteString class is an external string backed by a UTF-16
4811// encoded string.
4812class ExternalTwoByteString: public ExternalString {
4813 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004814 static const bool kHasAsciiEncoding = false;
4815
Steve Blocka7e24c12009-10-30 11:49:00 +00004816 typedef v8::String::ExternalStringResource Resource;
4817
4818 // The underlying string resource.
4819 inline Resource* resource();
4820 inline void set_resource(Resource* buffer);
4821
4822 // Dispatched behavior.
4823 uint16_t ExternalTwoByteStringGet(int index);
4824
4825 // For regexp code.
4826 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
4827
4828 // Casting.
4829 static inline ExternalTwoByteString* cast(Object* obj);
4830
Steve Blockd0582a62009-12-15 09:54:21 +00004831 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01004832 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
4833
4834 template<typename StaticVisitor>
4835 inline void ExternalTwoByteStringIterateBody();
4836
Steve Blockd0582a62009-12-15 09:54:21 +00004837
Steve Blocka7e24c12009-10-30 11:49:00 +00004838 // Support for StringInputBuffer.
4839 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4840 unsigned* offset_ptr,
4841 unsigned chars);
4842
Steve Blocka7e24c12009-10-30 11:49:00 +00004843 private:
4844 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
4845};
4846
4847
4848// Utility superclass for stack-allocated objects that must be updated
4849// on gc. It provides two ways for the gc to update instances, either
4850// iterating or updating after gc.
4851class Relocatable BASE_EMBEDDED {
4852 public:
4853 inline Relocatable() : prev_(top_) { top_ = this; }
4854 virtual ~Relocatable() {
4855 ASSERT_EQ(top_, this);
4856 top_ = prev_;
4857 }
4858 virtual void IterateInstance(ObjectVisitor* v) { }
4859 virtual void PostGarbageCollection() { }
4860
4861 static void PostGarbageCollectionProcessing();
4862 static int ArchiveSpacePerThread();
4863 static char* ArchiveState(char* to);
4864 static char* RestoreState(char* from);
4865 static void Iterate(ObjectVisitor* v);
4866 static void Iterate(ObjectVisitor* v, Relocatable* top);
4867 static char* Iterate(ObjectVisitor* v, char* t);
4868 private:
4869 static Relocatable* top_;
4870 Relocatable* prev_;
4871};
4872
4873
4874// A flat string reader provides random access to the contents of a
4875// string independent of the character width of the string. The handle
4876// must be valid as long as the reader is being used.
4877class FlatStringReader : public Relocatable {
4878 public:
4879 explicit FlatStringReader(Handle<String> str);
4880 explicit FlatStringReader(Vector<const char> input);
4881 void PostGarbageCollection();
4882 inline uc32 Get(int index);
4883 int length() { return length_; }
4884 private:
4885 String** str_;
4886 bool is_ascii_;
4887 int length_;
4888 const void* start_;
4889};
4890
4891
4892// Note that StringInputBuffers are not valid across a GC! To fix this
4893// it would have to store a String Handle instead of a String* and
4894// AsciiStringReadBlock would have to be modified to use memcpy.
4895//
4896// StringInputBuffer is able to traverse any string regardless of how
4897// deeply nested a sequence of ConsStrings it is made of. However,
4898// performance will be better if deep strings are flattened before they
4899// are traversed. Since flattening requires memory allocation this is
4900// not always desirable, however (esp. in debugging situations).
4901class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
4902 public:
4903 virtual void Seek(unsigned pos);
4904 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
4905 inline StringInputBuffer(String* backing):
4906 unibrow::InputBuffer<String, String*, 1024>(backing) {}
4907};
4908
4909
4910class SafeStringInputBuffer
4911 : public unibrow::InputBuffer<String, String**, 256> {
4912 public:
4913 virtual void Seek(unsigned pos);
4914 inline SafeStringInputBuffer()
4915 : unibrow::InputBuffer<String, String**, 256>() {}
4916 inline SafeStringInputBuffer(String** backing)
4917 : unibrow::InputBuffer<String, String**, 256>(backing) {}
4918};
4919
4920
4921template <typename T>
4922class VectorIterator {
4923 public:
4924 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
4925 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
4926 T GetNext() { return data_[index_++]; }
4927 bool has_more() { return index_ < data_.length(); }
4928 private:
4929 Vector<const T> data_;
4930 int index_;
4931};
4932
4933
4934// The Oddball describes objects null, undefined, true, and false.
4935class Oddball: public HeapObject {
4936 public:
4937 // [to_string]: Cached to_string computed at startup.
4938 DECL_ACCESSORS(to_string, String)
4939
4940 // [to_number]: Cached to_number computed at startup.
4941 DECL_ACCESSORS(to_number, Object)
4942
4943 // Casting.
4944 static inline Oddball* cast(Object* obj);
4945
4946 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004947#ifdef DEBUG
4948 void OddballVerify();
4949#endif
4950
4951 // Initialize the fields.
4952 Object* Initialize(const char* to_string, Object* to_number);
4953
4954 // Layout description.
4955 static const int kToStringOffset = HeapObject::kHeaderSize;
4956 static const int kToNumberOffset = kToStringOffset + kPointerSize;
4957 static const int kSize = kToNumberOffset + kPointerSize;
4958
Iain Merrick75681382010-08-19 15:07:18 +01004959 typedef FixedBodyDescriptor<kToStringOffset,
4960 kToNumberOffset + kPointerSize,
4961 kSize> BodyDescriptor;
4962
Steve Blocka7e24c12009-10-30 11:49:00 +00004963 private:
4964 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
4965};
4966
4967
4968class JSGlobalPropertyCell: public HeapObject {
4969 public:
4970 // [value]: value of the global property.
4971 DECL_ACCESSORS(value, Object)
4972
4973 // Casting.
4974 static inline JSGlobalPropertyCell* cast(Object* obj);
4975
Steve Blocka7e24c12009-10-30 11:49:00 +00004976#ifdef DEBUG
4977 void JSGlobalPropertyCellVerify();
4978 void JSGlobalPropertyCellPrint();
4979#endif
4980
4981 // Layout description.
4982 static const int kValueOffset = HeapObject::kHeaderSize;
4983 static const int kSize = kValueOffset + kPointerSize;
4984
Iain Merrick75681382010-08-19 15:07:18 +01004985 typedef FixedBodyDescriptor<kValueOffset,
4986 kValueOffset + kPointerSize,
4987 kSize> BodyDescriptor;
4988
Steve Blocka7e24c12009-10-30 11:49:00 +00004989 private:
4990 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
4991};
4992
4993
4994
4995// Proxy describes objects pointing from JavaScript to C structures.
4996// Since they cannot contain references to JS HeapObjects they can be
4997// placed in old_data_space.
4998class Proxy: public HeapObject {
4999 public:
5000 // [proxy]: field containing the address.
5001 inline Address proxy();
5002 inline void set_proxy(Address value);
5003
5004 // Casting.
5005 static inline Proxy* cast(Object* obj);
5006
5007 // Dispatched behavior.
5008 inline void ProxyIterateBody(ObjectVisitor* v);
Iain Merrick75681382010-08-19 15:07:18 +01005009
5010 template<typename StaticVisitor>
5011 inline void ProxyIterateBody();
5012
Steve Blocka7e24c12009-10-30 11:49:00 +00005013#ifdef DEBUG
5014 void ProxyPrint();
5015 void ProxyVerify();
5016#endif
5017
5018 // Layout description.
5019
5020 static const int kProxyOffset = HeapObject::kHeaderSize;
5021 static const int kSize = kProxyOffset + kPointerSize;
5022
5023 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
5024
5025 private:
5026 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
5027};
5028
5029
5030// The JSArray describes JavaScript Arrays
5031// Such an array can be in one of two modes:
5032// - fast, backing storage is a FixedArray and length <= elements.length();
5033// Please note: push and pop can be used to grow and shrink the array.
5034// - slow, backing storage is a HashTable with numbers as keys.
5035class JSArray: public JSObject {
5036 public:
5037 // [length]: The length property.
5038 DECL_ACCESSORS(length, Object)
5039
Leon Clarke4515c472010-02-03 11:58:03 +00005040 // Overload the length setter to skip write barrier when the length
5041 // is set to a smi. This matches the set function on FixedArray.
5042 inline void set_length(Smi* length);
5043
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005044 MUST_USE_RESULT Object* JSArrayUpdateLengthFromIndex(uint32_t index,
5045 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005046
5047 // Initialize the array with the given capacity. The function may
5048 // fail due to out-of-memory situations, but only if the requested
5049 // capacity is non-zero.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005050 MUST_USE_RESULT Object* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00005051
5052 // Set the content of the array to the content of storage.
5053 inline void SetContent(FixedArray* storage);
5054
5055 // Casting.
5056 static inline JSArray* cast(Object* obj);
5057
5058 // Uses handles. Ensures that the fixed array backing the JSArray has at
5059 // least the stated size.
5060 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
5061
5062 // Dispatched behavior.
5063#ifdef DEBUG
5064 void JSArrayPrint();
5065 void JSArrayVerify();
5066#endif
5067
5068 // Number of element slots to pre-allocate for an empty array.
5069 static const int kPreallocatedArrayElements = 4;
5070
5071 // Layout description.
5072 static const int kLengthOffset = JSObject::kHeaderSize;
5073 static const int kSize = kLengthOffset + kPointerSize;
5074
5075 private:
5076 // Expand the fixed array backing of a fast-case JSArray to at least
5077 // the requested size.
5078 void Expand(int minimum_size_of_backing_fixed_array);
5079
5080 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
5081};
5082
5083
Steve Block6ded16b2010-05-10 14:33:55 +01005084// JSRegExpResult is just a JSArray with a specific initial map.
5085// This initial map adds in-object properties for "index" and "input"
5086// properties, as assigned by RegExp.prototype.exec, which allows
5087// faster creation of RegExp exec results.
5088// This class just holds constants used when creating the result.
5089// After creation the result must be treated as a JSArray in all regards.
5090class JSRegExpResult: public JSArray {
5091 public:
5092 // Offsets of object fields.
5093 static const int kIndexOffset = JSArray::kSize;
5094 static const int kInputOffset = kIndexOffset + kPointerSize;
5095 static const int kSize = kInputOffset + kPointerSize;
5096 // Indices of in-object properties.
5097 static const int kIndexIndex = 0;
5098 static const int kInputIndex = 1;
5099 private:
5100 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
5101};
5102
5103
Steve Blocka7e24c12009-10-30 11:49:00 +00005104// An accessor must have a getter, but can have no setter.
5105//
5106// When setting a property, V8 searches accessors in prototypes.
5107// If an accessor was found and it does not have a setter,
5108// the request is ignored.
5109//
5110// If the accessor in the prototype has the READ_ONLY property attribute, then
5111// a new value is added to the local object when the property is set.
5112// This shadows the accessor in the prototype.
5113class AccessorInfo: public Struct {
5114 public:
5115 DECL_ACCESSORS(getter, Object)
5116 DECL_ACCESSORS(setter, Object)
5117 DECL_ACCESSORS(data, Object)
5118 DECL_ACCESSORS(name, Object)
5119 DECL_ACCESSORS(flag, Smi)
Steve Blockd0582a62009-12-15 09:54:21 +00005120 DECL_ACCESSORS(load_stub_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00005121
5122 inline bool all_can_read();
5123 inline void set_all_can_read(bool value);
5124
5125 inline bool all_can_write();
5126 inline void set_all_can_write(bool value);
5127
5128 inline bool prohibits_overwriting();
5129 inline void set_prohibits_overwriting(bool value);
5130
5131 inline PropertyAttributes property_attributes();
5132 inline void set_property_attributes(PropertyAttributes attributes);
5133
5134 static inline AccessorInfo* cast(Object* obj);
5135
5136#ifdef DEBUG
5137 void AccessorInfoPrint();
5138 void AccessorInfoVerify();
5139#endif
5140
5141 static const int kGetterOffset = HeapObject::kHeaderSize;
5142 static const int kSetterOffset = kGetterOffset + kPointerSize;
5143 static const int kDataOffset = kSetterOffset + kPointerSize;
5144 static const int kNameOffset = kDataOffset + kPointerSize;
5145 static const int kFlagOffset = kNameOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00005146 static const int kLoadStubCacheOffset = kFlagOffset + kPointerSize;
5147 static const int kSize = kLoadStubCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005148
5149 private:
5150 // Bit positions in flag.
5151 static const int kAllCanReadBit = 0;
5152 static const int kAllCanWriteBit = 1;
5153 static const int kProhibitsOverwritingBit = 2;
5154 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
5155
5156 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
5157};
5158
5159
5160class AccessCheckInfo: public Struct {
5161 public:
5162 DECL_ACCESSORS(named_callback, Object)
5163 DECL_ACCESSORS(indexed_callback, Object)
5164 DECL_ACCESSORS(data, Object)
5165
5166 static inline AccessCheckInfo* cast(Object* obj);
5167
5168#ifdef DEBUG
5169 void AccessCheckInfoPrint();
5170 void AccessCheckInfoVerify();
5171#endif
5172
5173 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
5174 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
5175 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
5176 static const int kSize = kDataOffset + kPointerSize;
5177
5178 private:
5179 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
5180};
5181
5182
5183class InterceptorInfo: public Struct {
5184 public:
5185 DECL_ACCESSORS(getter, Object)
5186 DECL_ACCESSORS(setter, Object)
5187 DECL_ACCESSORS(query, Object)
5188 DECL_ACCESSORS(deleter, Object)
5189 DECL_ACCESSORS(enumerator, Object)
5190 DECL_ACCESSORS(data, Object)
5191
5192 static inline InterceptorInfo* cast(Object* obj);
5193
5194#ifdef DEBUG
5195 void InterceptorInfoPrint();
5196 void InterceptorInfoVerify();
5197#endif
5198
5199 static const int kGetterOffset = HeapObject::kHeaderSize;
5200 static const int kSetterOffset = kGetterOffset + kPointerSize;
5201 static const int kQueryOffset = kSetterOffset + kPointerSize;
5202 static const int kDeleterOffset = kQueryOffset + kPointerSize;
5203 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
5204 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
5205 static const int kSize = kDataOffset + kPointerSize;
5206
5207 private:
5208 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
5209};
5210
5211
5212class CallHandlerInfo: public Struct {
5213 public:
5214 DECL_ACCESSORS(callback, Object)
5215 DECL_ACCESSORS(data, Object)
5216
5217 static inline CallHandlerInfo* cast(Object* obj);
5218
5219#ifdef DEBUG
5220 void CallHandlerInfoPrint();
5221 void CallHandlerInfoVerify();
5222#endif
5223
5224 static const int kCallbackOffset = HeapObject::kHeaderSize;
5225 static const int kDataOffset = kCallbackOffset + kPointerSize;
5226 static const int kSize = kDataOffset + kPointerSize;
5227
5228 private:
5229 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
5230};
5231
5232
5233class TemplateInfo: public Struct {
5234 public:
5235 DECL_ACCESSORS(tag, Object)
5236 DECL_ACCESSORS(property_list, Object)
5237
5238#ifdef DEBUG
5239 void TemplateInfoVerify();
5240#endif
5241
5242 static const int kTagOffset = HeapObject::kHeaderSize;
5243 static const int kPropertyListOffset = kTagOffset + kPointerSize;
5244 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
5245 protected:
5246 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
5247 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
5248};
5249
5250
5251class FunctionTemplateInfo: public TemplateInfo {
5252 public:
5253 DECL_ACCESSORS(serial_number, Object)
5254 DECL_ACCESSORS(call_code, Object)
5255 DECL_ACCESSORS(property_accessors, Object)
5256 DECL_ACCESSORS(prototype_template, Object)
5257 DECL_ACCESSORS(parent_template, Object)
5258 DECL_ACCESSORS(named_property_handler, Object)
5259 DECL_ACCESSORS(indexed_property_handler, Object)
5260 DECL_ACCESSORS(instance_template, Object)
5261 DECL_ACCESSORS(class_name, Object)
5262 DECL_ACCESSORS(signature, Object)
5263 DECL_ACCESSORS(instance_call_handler, Object)
5264 DECL_ACCESSORS(access_check_info, Object)
5265 DECL_ACCESSORS(flag, Smi)
5266
5267 // Following properties use flag bits.
5268 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
5269 DECL_BOOLEAN_ACCESSORS(undetectable)
5270 // If the bit is set, object instances created by this function
5271 // requires access check.
5272 DECL_BOOLEAN_ACCESSORS(needs_access_check)
5273
5274 static inline FunctionTemplateInfo* cast(Object* obj);
5275
5276#ifdef DEBUG
5277 void FunctionTemplateInfoPrint();
5278 void FunctionTemplateInfoVerify();
5279#endif
5280
5281 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
5282 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
5283 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
5284 static const int kPrototypeTemplateOffset =
5285 kPropertyAccessorsOffset + kPointerSize;
5286 static const int kParentTemplateOffset =
5287 kPrototypeTemplateOffset + kPointerSize;
5288 static const int kNamedPropertyHandlerOffset =
5289 kParentTemplateOffset + kPointerSize;
5290 static const int kIndexedPropertyHandlerOffset =
5291 kNamedPropertyHandlerOffset + kPointerSize;
5292 static const int kInstanceTemplateOffset =
5293 kIndexedPropertyHandlerOffset + kPointerSize;
5294 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
5295 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
5296 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
5297 static const int kAccessCheckInfoOffset =
5298 kInstanceCallHandlerOffset + kPointerSize;
5299 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
5300 static const int kSize = kFlagOffset + kPointerSize;
5301
5302 private:
5303 // Bit position in the flag, from least significant bit position.
5304 static const int kHiddenPrototypeBit = 0;
5305 static const int kUndetectableBit = 1;
5306 static const int kNeedsAccessCheckBit = 2;
5307
5308 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
5309};
5310
5311
5312class ObjectTemplateInfo: public TemplateInfo {
5313 public:
5314 DECL_ACCESSORS(constructor, Object)
5315 DECL_ACCESSORS(internal_field_count, Object)
5316
5317 static inline ObjectTemplateInfo* cast(Object* obj);
5318
5319#ifdef DEBUG
5320 void ObjectTemplateInfoPrint();
5321 void ObjectTemplateInfoVerify();
5322#endif
5323
5324 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
5325 static const int kInternalFieldCountOffset =
5326 kConstructorOffset + kPointerSize;
5327 static const int kSize = kInternalFieldCountOffset + kPointerSize;
5328};
5329
5330
5331class SignatureInfo: public Struct {
5332 public:
5333 DECL_ACCESSORS(receiver, Object)
5334 DECL_ACCESSORS(args, Object)
5335
5336 static inline SignatureInfo* cast(Object* obj);
5337
5338#ifdef DEBUG
5339 void SignatureInfoPrint();
5340 void SignatureInfoVerify();
5341#endif
5342
5343 static const int kReceiverOffset = Struct::kHeaderSize;
5344 static const int kArgsOffset = kReceiverOffset + kPointerSize;
5345 static const int kSize = kArgsOffset + kPointerSize;
5346
5347 private:
5348 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
5349};
5350
5351
5352class TypeSwitchInfo: public Struct {
5353 public:
5354 DECL_ACCESSORS(types, Object)
5355
5356 static inline TypeSwitchInfo* cast(Object* obj);
5357
5358#ifdef DEBUG
5359 void TypeSwitchInfoPrint();
5360 void TypeSwitchInfoVerify();
5361#endif
5362
5363 static const int kTypesOffset = Struct::kHeaderSize;
5364 static const int kSize = kTypesOffset + kPointerSize;
5365};
5366
5367
5368#ifdef ENABLE_DEBUGGER_SUPPORT
5369// The DebugInfo class holds additional information for a function being
5370// debugged.
5371class DebugInfo: public Struct {
5372 public:
5373 // The shared function info for the source being debugged.
5374 DECL_ACCESSORS(shared, SharedFunctionInfo)
5375 // Code object for the original code.
5376 DECL_ACCESSORS(original_code, Code)
5377 // Code object for the patched code. This code object is the code object
5378 // currently active for the function.
5379 DECL_ACCESSORS(code, Code)
5380 // Fixed array holding status information for each active break point.
5381 DECL_ACCESSORS(break_points, FixedArray)
5382
5383 // Check if there is a break point at a code position.
5384 bool HasBreakPoint(int code_position);
5385 // Get the break point info object for a code position.
5386 Object* GetBreakPointInfo(int code_position);
5387 // Clear a break point.
5388 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
5389 int code_position,
5390 Handle<Object> break_point_object);
5391 // Set a break point.
5392 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
5393 int source_position, int statement_position,
5394 Handle<Object> break_point_object);
5395 // Get the break point objects for a code position.
5396 Object* GetBreakPointObjects(int code_position);
5397 // Find the break point info holding this break point object.
5398 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
5399 Handle<Object> break_point_object);
5400 // Get the number of break points for this function.
5401 int GetBreakPointCount();
5402
5403 static inline DebugInfo* cast(Object* obj);
5404
5405#ifdef DEBUG
5406 void DebugInfoPrint();
5407 void DebugInfoVerify();
5408#endif
5409
5410 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
5411 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
5412 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
5413 static const int kActiveBreakPointsCountIndex =
5414 kPatchedCodeIndex + kPointerSize;
5415 static const int kBreakPointsStateIndex =
5416 kActiveBreakPointsCountIndex + kPointerSize;
5417 static const int kSize = kBreakPointsStateIndex + kPointerSize;
5418
5419 private:
5420 static const int kNoBreakPointInfo = -1;
5421
5422 // Lookup the index in the break_points array for a code position.
5423 int GetBreakPointInfoIndex(int code_position);
5424
5425 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
5426};
5427
5428
5429// The BreakPointInfo class holds information for break points set in a
5430// function. The DebugInfo object holds a BreakPointInfo object for each code
5431// position with one or more break points.
5432class BreakPointInfo: public Struct {
5433 public:
5434 // The position in the code for the break point.
5435 DECL_ACCESSORS(code_position, Smi)
5436 // The position in the source for the break position.
5437 DECL_ACCESSORS(source_position, Smi)
5438 // The position in the source for the last statement before this break
5439 // position.
5440 DECL_ACCESSORS(statement_position, Smi)
5441 // List of related JavaScript break points.
5442 DECL_ACCESSORS(break_point_objects, Object)
5443
5444 // Removes a break point.
5445 static void ClearBreakPoint(Handle<BreakPointInfo> info,
5446 Handle<Object> break_point_object);
5447 // Set a break point.
5448 static void SetBreakPoint(Handle<BreakPointInfo> info,
5449 Handle<Object> break_point_object);
5450 // Check if break point info has this break point object.
5451 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
5452 Handle<Object> break_point_object);
5453 // Get the number of break points for this code position.
5454 int GetBreakPointCount();
5455
5456 static inline BreakPointInfo* cast(Object* obj);
5457
5458#ifdef DEBUG
5459 void BreakPointInfoPrint();
5460 void BreakPointInfoVerify();
5461#endif
5462
5463 static const int kCodePositionIndex = Struct::kHeaderSize;
5464 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
5465 static const int kStatementPositionIndex =
5466 kSourcePositionIndex + kPointerSize;
5467 static const int kBreakPointObjectsIndex =
5468 kStatementPositionIndex + kPointerSize;
5469 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
5470
5471 private:
5472 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
5473};
5474#endif // ENABLE_DEBUGGER_SUPPORT
5475
5476
5477#undef DECL_BOOLEAN_ACCESSORS
5478#undef DECL_ACCESSORS
5479
5480
5481// Abstract base class for visiting, and optionally modifying, the
5482// pointers contained in Objects. Used in GC and serialization/deserialization.
5483class ObjectVisitor BASE_EMBEDDED {
5484 public:
5485 virtual ~ObjectVisitor() {}
5486
5487 // Visits a contiguous arrays of pointers in the half-open range
5488 // [start, end). Any or all of the values may be modified on return.
5489 virtual void VisitPointers(Object** start, Object** end) = 0;
5490
5491 // To allow lazy clearing of inline caches the visitor has
5492 // a rich interface for iterating over Code objects..
5493
5494 // Visits a code target in the instruction stream.
5495 virtual void VisitCodeTarget(RelocInfo* rinfo);
5496
Steve Block791712a2010-08-27 10:21:07 +01005497 // Visits a code entry in a JS function.
5498 virtual void VisitCodeEntry(Address entry_address);
5499
Steve Blocka7e24c12009-10-30 11:49:00 +00005500 // Visits a runtime entry in the instruction stream.
5501 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
5502
Steve Blockd0582a62009-12-15 09:54:21 +00005503 // Visits the resource of an ASCII or two-byte string.
5504 virtual void VisitExternalAsciiString(
5505 v8::String::ExternalAsciiStringResource** resource) {}
5506 virtual void VisitExternalTwoByteString(
5507 v8::String::ExternalStringResource** resource) {}
5508
Steve Blocka7e24c12009-10-30 11:49:00 +00005509 // Visits a debug call target in the instruction stream.
5510 virtual void VisitDebugTarget(RelocInfo* rinfo);
5511
5512 // Handy shorthand for visiting a single pointer.
5513 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
5514
5515 // Visits a contiguous arrays of external references (references to the C++
5516 // heap) in the half-open range [start, end). Any or all of the values
5517 // may be modified on return.
5518 virtual void VisitExternalReferences(Address* start, Address* end) {}
5519
5520 inline void VisitExternalReference(Address* p) {
5521 VisitExternalReferences(p, p + 1);
5522 }
5523
5524#ifdef DEBUG
5525 // Intended for serialization/deserialization checking: insert, or
5526 // check for the presence of, a tag at this position in the stream.
5527 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00005528#else
5529 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00005530#endif
5531};
5532
5533
Iain Merrick75681382010-08-19 15:07:18 +01005534class StructBodyDescriptor : public
5535 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
5536 public:
5537 static inline int SizeOf(Map* map, HeapObject* object) {
5538 return map->instance_size();
5539 }
5540};
5541
5542
Steve Blocka7e24c12009-10-30 11:49:00 +00005543// BooleanBit is a helper class for setting and getting a bit in an
5544// integer or Smi.
5545class BooleanBit : public AllStatic {
5546 public:
5547 static inline bool get(Smi* smi, int bit_position) {
5548 return get(smi->value(), bit_position);
5549 }
5550
5551 static inline bool get(int value, int bit_position) {
5552 return (value & (1 << bit_position)) != 0;
5553 }
5554
5555 static inline Smi* set(Smi* smi, int bit_position, bool v) {
5556 return Smi::FromInt(set(smi->value(), bit_position, v));
5557 }
5558
5559 static inline int set(int value, int bit_position, bool v) {
5560 if (v) {
5561 value |= (1 << bit_position);
5562 } else {
5563 value &= ~(1 << bit_position);
5564 }
5565 return value;
5566 }
5567};
5568
5569} } // namespace v8::internal
5570
5571#endif // V8_OBJECTS_H_