blob: c5fda7d0385d10d299754204a1c79c9d8b3ef33a [file] [log] [blame]
Ben Murdochf87a2032010-10-22 12:50:53 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_OBJECTS_H_
29#define V8_OBJECTS_H_
30
31#include "builtins.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "smart-pointer.h"
33#include "unicode-inl.h"
Steve Block3ce2e202009-11-05 08:53:23 +000034#if V8_TARGET_ARCH_ARM
35#include "arm/constants-arm.h"
Andrei Popescu31002712010-02-23 13:46:05 +000036#elif V8_TARGET_ARCH_MIPS
37#include "mips/constants-mips.h"
Steve Block3ce2e202009-11-05 08:53:23 +000038#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000039
40//
Kristian Monsen50ef84f2010-07-29 15:18:00 +010041// Most object types in the V8 JavaScript are described in this file.
Steve Blocka7e24c12009-10-30 11:49:00 +000042//
43// Inheritance hierarchy:
John Reck59135872010-11-02 12:39:01 -070044// - MaybeObject (an object or a failure)
45// - Failure (immediate for marking failed operation)
Steve Blocka7e24c12009-10-30 11:49:00 +000046// - Object
47// - Smi (immediate small integer)
Steve Blocka7e24c12009-10-30 11:49:00 +000048// - HeapObject (superclass for everything allocated in the heap)
49// - JSObject
50// - JSArray
51// - JSRegExp
52// - JSFunction
53// - GlobalObject
54// - JSGlobalObject
55// - JSBuiltinsObject
56// - JSGlobalProxy
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010057// - JSValue
58// - ByteArray
59// - PixelArray
60// - ExternalArray
61// - ExternalByteArray
62// - ExternalUnsignedByteArray
63// - ExternalShortArray
64// - ExternalUnsignedShortArray
65// - ExternalIntArray
66// - ExternalUnsignedIntArray
67// - ExternalFloatArray
68// - FixedArray
69// - DescriptorArray
70// - HashTable
71// - Dictionary
72// - SymbolTable
73// - CompilationCacheTable
74// - CodeCacheHashTable
75// - MapCache
76// - Context
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010077// - JSFunctionResultCache
Kristian Monsen50ef84f2010-07-29 15:18:00 +010078// - SerializedScopeInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000079// - String
80// - SeqString
81// - SeqAsciiString
82// - SeqTwoByteString
83// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000084// - ExternalString
85// - ExternalAsciiString
86// - ExternalTwoByteString
87// - HeapNumber
88// - Code
89// - Map
90// - Oddball
91// - Proxy
92// - SharedFunctionInfo
93// - Struct
94// - AccessorInfo
95// - AccessCheckInfo
96// - InterceptorInfo
97// - CallHandlerInfo
98// - TemplateInfo
99// - FunctionTemplateInfo
100// - ObjectTemplateInfo
101// - Script
102// - SignatureInfo
103// - TypeSwitchInfo
104// - DebugInfo
105// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100106// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000107//
108// Formats of Object*:
109// Smi: [31 bit signed int] 0
110// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
111// Failure: [30 bit signed int] 11
112
113// Ecma-262 3rd 8.6.1
114enum PropertyAttributes {
115 NONE = v8::None,
116 READ_ONLY = v8::ReadOnly,
117 DONT_ENUM = v8::DontEnum,
118 DONT_DELETE = v8::DontDelete,
119 ABSENT = 16 // Used in runtime to indicate a property is absent.
120 // ABSENT can never be stored in or returned from a descriptor's attributes
121 // bitfield. It is only used as a return value meaning the attributes of
122 // a non-existent property.
123};
124
125namespace v8 {
126namespace internal {
127
128
129// PropertyDetails captures type and attributes for a property.
130// They are used both in property dictionaries and instance descriptors.
131class PropertyDetails BASE_EMBEDDED {
132 public:
133
134 PropertyDetails(PropertyAttributes attributes,
135 PropertyType type,
136 int index = 0) {
137 ASSERT(TypeField::is_valid(type));
138 ASSERT(AttributesField::is_valid(attributes));
139 ASSERT(IndexField::is_valid(index));
140
141 value_ = TypeField::encode(type)
142 | AttributesField::encode(attributes)
143 | IndexField::encode(index);
144
145 ASSERT(type == this->type());
146 ASSERT(attributes == this->attributes());
147 ASSERT(index == this->index());
148 }
149
150 // Conversion for storing details as Object*.
151 inline PropertyDetails(Smi* smi);
152 inline Smi* AsSmi();
153
154 PropertyType type() { return TypeField::decode(value_); }
155
156 bool IsTransition() {
157 PropertyType t = type();
158 ASSERT(t != INTERCEPTOR);
159 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION;
160 }
161
162 bool IsProperty() {
163 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
164 }
165
166 PropertyAttributes attributes() { return AttributesField::decode(value_); }
167
168 int index() { return IndexField::decode(value_); }
169
170 inline PropertyDetails AsDeleted();
171
172 static bool IsValidIndex(int index) { return IndexField::is_valid(index); }
173
174 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
175 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
176 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
177 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
178
179 // Bit fields in value_ (type, shift, size). Must be public so the
180 // constants can be embedded in generated code.
181 class TypeField: public BitField<PropertyType, 0, 3> {};
182 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
183 class DeletedField: public BitField<uint32_t, 6, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000184 class IndexField: public BitField<uint32_t, 7, 32-7> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
186 static const int kInitialIndex = 1;
187 private:
188 uint32_t value_;
189};
190
191
192// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
193enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
194
195
196// PropertyNormalizationMode is used to specify whether to keep
197// inobject properties when normalizing properties of a JSObject.
198enum PropertyNormalizationMode {
199 CLEAR_INOBJECT_PROPERTIES,
200 KEEP_INOBJECT_PROPERTIES
201};
202
203
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100204// NormalizedMapSharingMode is used to specify whether a map may be shared
205// by different objects with normalized properties.
206enum NormalizedMapSharingMode {
207 UNIQUE_NORMALIZED_MAP,
208 SHARED_NORMALIZED_MAP
209};
210
211
Steve Block791712a2010-08-27 10:21:07 +0100212// Instance size sentinel for objects of variable size.
213static const int kVariableSizeSentinel = 0;
214
215
Steve Blocka7e24c12009-10-30 11:49:00 +0000216// All Maps have a field instance_type containing a InstanceType.
217// It describes the type of the instances.
218//
219// As an example, a JavaScript object is a heap object and its map
220// instance_type is JS_OBJECT_TYPE.
221//
222// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000223// mirror their encoding in the instance_type field of the map. The default
224// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
225// encoding is mentioned explicitly in the name. Likewise, the default
226// representation is considered sequential. It is not mentioned in the
227// name. The other representations (eg, CONS, EXTERNAL) are explicitly
228// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
229// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000230//
231// NOTE: The following things are some that depend on the string types having
232// instance_types that are less than those of all other types:
233// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
234// Object::IsString.
235//
236// NOTE: Everything following JS_VALUE_TYPE is considered a
237// JSObject for GC purposes. The first four entries here have typeof
238// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000239#define INSTANCE_TYPE_LIST_ALL(V) \
240 V(SYMBOL_TYPE) \
241 V(ASCII_SYMBOL_TYPE) \
242 V(CONS_SYMBOL_TYPE) \
243 V(CONS_ASCII_SYMBOL_TYPE) \
244 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100245 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000246 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
247 V(STRING_TYPE) \
248 V(ASCII_STRING_TYPE) \
249 V(CONS_STRING_TYPE) \
250 V(CONS_ASCII_STRING_TYPE) \
251 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100252 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000253 V(EXTERNAL_ASCII_STRING_TYPE) \
254 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
255 \
256 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000257 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000258 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100259 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000260 \
261 V(HEAP_NUMBER_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000262 V(PROXY_TYPE) \
263 V(BYTE_ARRAY_TYPE) \
264 V(PIXEL_ARRAY_TYPE) \
265 /* Note: the order of these external array */ \
266 /* types is relied upon in */ \
267 /* Object::IsExternalArray(). */ \
268 V(EXTERNAL_BYTE_ARRAY_TYPE) \
269 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
270 V(EXTERNAL_SHORT_ARRAY_TYPE) \
271 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
272 V(EXTERNAL_INT_ARRAY_TYPE) \
273 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
274 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
275 V(FILLER_TYPE) \
276 \
277 V(ACCESSOR_INFO_TYPE) \
278 V(ACCESS_CHECK_INFO_TYPE) \
279 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000280 V(CALL_HANDLER_INFO_TYPE) \
281 V(FUNCTION_TEMPLATE_INFO_TYPE) \
282 V(OBJECT_TEMPLATE_INFO_TYPE) \
283 V(SIGNATURE_INFO_TYPE) \
284 V(TYPE_SWITCH_INFO_TYPE) \
285 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100286 V(CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000287 \
Iain Merrick75681382010-08-19 15:07:18 +0100288 V(FIXED_ARRAY_TYPE) \
289 V(SHARED_FUNCTION_INFO_TYPE) \
290 \
Steve Blockd0582a62009-12-15 09:54:21 +0000291 V(JS_VALUE_TYPE) \
292 V(JS_OBJECT_TYPE) \
293 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
294 V(JS_GLOBAL_OBJECT_TYPE) \
295 V(JS_BUILTINS_OBJECT_TYPE) \
296 V(JS_GLOBAL_PROXY_TYPE) \
297 V(JS_ARRAY_TYPE) \
298 V(JS_REGEXP_TYPE) \
299 \
300 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000301
302#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000303#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
304 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000305 V(BREAK_POINT_INFO_TYPE)
306#else
307#define INSTANCE_TYPE_LIST_DEBUGGER(V)
308#endif
309
Steve Blockd0582a62009-12-15 09:54:21 +0000310#define INSTANCE_TYPE_LIST(V) \
311 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000312 INSTANCE_TYPE_LIST_DEBUGGER(V)
313
314
315// Since string types are not consecutive, this macro is used to
316// iterate over them.
317#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000318 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100319 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000320 symbol, \
321 Symbol) \
322 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100323 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000324 ascii_symbol, \
325 AsciiSymbol) \
326 V(CONS_SYMBOL_TYPE, \
327 ConsString::kSize, \
328 cons_symbol, \
329 ConsSymbol) \
330 V(CONS_ASCII_SYMBOL_TYPE, \
331 ConsString::kSize, \
332 cons_ascii_symbol, \
333 ConsAsciiSymbol) \
334 V(EXTERNAL_SYMBOL_TYPE, \
335 ExternalTwoByteString::kSize, \
336 external_symbol, \
337 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100338 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
339 ExternalTwoByteString::kSize, \
340 external_symbol_with_ascii_data, \
341 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000342 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
343 ExternalAsciiString::kSize, \
344 external_ascii_symbol, \
345 ExternalAsciiSymbol) \
346 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100347 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000348 string, \
349 String) \
350 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100351 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000352 ascii_string, \
353 AsciiString) \
354 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000355 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000356 cons_string, \
357 ConsString) \
358 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000360 cons_ascii_string, \
361 ConsAsciiString) \
362 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000363 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000364 external_string, \
365 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100366 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
367 ExternalTwoByteString::kSize, \
368 external_string_with_ascii_data, \
369 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000370 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000372 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100373 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000374
375// A struct is a simple object a set of object-valued fields. Including an
376// object type in this causes the compiler to generate most of the boilerplate
377// code for the class including allocation and garbage collection routines,
378// casts and predicates. All you need to define is the class, methods and
379// object verification routines. Easy, no?
380//
381// Note that for subtle reasons related to the ordering or numerical values of
382// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
383// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000384#define STRUCT_LIST_ALL(V) \
385 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
386 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
387 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
388 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
389 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
390 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
391 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
392 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100393 V(SCRIPT, Script, script) \
394 V(CODE_CACHE, CodeCache, code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000395
396#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000397#define STRUCT_LIST_DEBUGGER(V) \
398 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
400#else
401#define STRUCT_LIST_DEBUGGER(V)
402#endif
403
Steve Blockd0582a62009-12-15 09:54:21 +0000404#define STRUCT_LIST(V) \
405 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000406 STRUCT_LIST_DEBUGGER(V)
407
408// We use the full 8 bits of the instance_type field to encode heap object
409// instance types. The high-order bit (bit 7) is set if the object is not a
410// string, and cleared if it is a string.
411const uint32_t kIsNotStringMask = 0x80;
412const uint32_t kStringTag = 0x0;
413const uint32_t kNotStringTag = 0x80;
414
Leon Clarkee46be812010-01-19 14:06:41 +0000415// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
416// There are not enough types that the non-string types (with bit 7 set) can
417// have bit 6 set too.
418const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000419const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000420const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000421
Steve Blocka7e24c12009-10-30 11:49:00 +0000422// If bit 7 is clear then bit 2 indicates whether the string consists of
423// two-byte characters or one-byte characters.
424const uint32_t kStringEncodingMask = 0x4;
425const uint32_t kTwoByteStringTag = 0x0;
426const uint32_t kAsciiStringTag = 0x4;
427
428// If bit 7 is clear, the low-order 2 bits indicate the representation
429// of the string.
430const uint32_t kStringRepresentationMask = 0x03;
431enum StringRepresentationTag {
432 kSeqStringTag = 0x0,
433 kConsStringTag = 0x1,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100434 kExternalStringTag = 0x2
Steve Blocka7e24c12009-10-30 11:49:00 +0000435};
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100436const uint32_t kIsConsStringMask = 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000437
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100438// If bit 7 is clear, then bit 3 indicates whether this two-byte
439// string actually contains ascii data.
440const uint32_t kAsciiDataHintMask = 0x08;
441const uint32_t kAsciiDataHintTag = 0x08;
442
Steve Blocka7e24c12009-10-30 11:49:00 +0000443
444// A ConsString with an empty string as the right side is a candidate
445// for being shortcut by the garbage collector unless it is a
446// symbol. It's not common to have non-flat symbols, so we do not
447// shortcut them thereby avoiding turning symbols into strings. See
448// heap.cc and mark-compact.cc.
449const uint32_t kShortcutTypeMask =
450 kIsNotStringMask |
451 kIsSymbolMask |
452 kStringRepresentationMask;
453const uint32_t kShortcutTypeTag = kConsStringTag;
454
455
456enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000457 // String types.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100458 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000459 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100460 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000461 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100462 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
463 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
464 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000465 EXTERNAL_ASCII_SYMBOL_TYPE =
466 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100467 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000468 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100469 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000470 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100471 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
472 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
473 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000474 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
475 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000476
Leon Clarkee46be812010-01-19 14:06:41 +0000477 // Objects allocated in their own spaces (never in new space).
478 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 CODE_TYPE,
480 ODDBALL_TYPE,
481 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000482
483 // "Data", objects that cannot contain non-map-word pointers to heap
484 // objects.
485 HEAP_NUMBER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000486 PROXY_TYPE,
487 BYTE_ARRAY_TYPE,
488 PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000489 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000490 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
491 EXTERNAL_SHORT_ARRAY_TYPE,
492 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
493 EXTERNAL_INT_ARRAY_TYPE,
494 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000495 EXTERNAL_FLOAT_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
496 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000497
Leon Clarkee46be812010-01-19 14:06:41 +0000498 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000499 ACCESSOR_INFO_TYPE,
500 ACCESS_CHECK_INFO_TYPE,
501 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 CALL_HANDLER_INFO_TYPE,
503 FUNCTION_TEMPLATE_INFO_TYPE,
504 OBJECT_TEMPLATE_INFO_TYPE,
505 SIGNATURE_INFO_TYPE,
506 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000507 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100508 CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100509 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
510 // is defined. However as include/v8.h contain some of the instance type
511 // constants always having them avoids them getting different numbers
512 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 DEBUG_INFO_TYPE,
514 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000515
Leon Clarkee46be812010-01-19 14:06:41 +0000516 FIXED_ARRAY_TYPE,
517 SHARED_FUNCTION_INFO_TYPE,
518
519 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 JS_OBJECT_TYPE,
521 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
522 JS_GLOBAL_OBJECT_TYPE,
523 JS_BUILTINS_OBJECT_TYPE,
524 JS_GLOBAL_PROXY_TYPE,
525 JS_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000526 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000527
528 JS_FUNCTION_TYPE,
529
530 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000531 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000533 INVALID_TYPE = FIRST_TYPE - 1,
534 FIRST_NONSTRING_TYPE = MAP_TYPE,
535 // Boundaries for testing for an external array.
536 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
537 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_FLOAT_ARRAY_TYPE,
538 // Boundary for promotion to old data space/old pointer space.
539 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 // Boundaries for testing the type is a JavaScript "object". Note that
541 // function objects are not counted as objects, even though they are
542 // implemented as such; only values whose typeof is "object" are included.
543 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
544 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE
545};
546
547
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100548STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
549STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
550STATIC_CHECK(PROXY_TYPE == Internals::kProxyType);
551
552
Steve Blocka7e24c12009-10-30 11:49:00 +0000553enum CompareResult {
554 LESS = -1,
555 EQUAL = 0,
556 GREATER = 1,
557
558 NOT_EQUAL = GREATER
559};
560
561
562#define DECL_BOOLEAN_ACCESSORS(name) \
563 inline bool name(); \
564 inline void set_##name(bool value); \
565
566
567#define DECL_ACCESSORS(name, type) \
568 inline type* name(); \
569 inline void set_##name(type* value, \
570 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
571
572
573class StringStream;
574class ObjectVisitor;
575
576struct ValueInfo : public Malloced {
577 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
578 InstanceType type;
579 Object* ptr;
580 const char* str;
581 double number;
582};
583
584
585// A template-ized version of the IsXXX functions.
586template <class C> static inline bool Is(Object* obj);
587
Ben Murdochb0fe1622011-05-05 13:52:32 +0100588
John Reck59135872010-11-02 12:39:01 -0700589class MaybeObject BASE_EMBEDDED {
590 public:
591 inline bool IsFailure();
592 inline bool IsRetryAfterGC();
593 inline bool IsOutOfMemory();
594 inline bool IsException();
595 INLINE(bool IsTheHole());
596 inline bool ToObject(Object** obj) {
597 if (IsFailure()) return false;
598 *obj = reinterpret_cast<Object*>(this);
599 return true;
600 }
601 inline Object* ToObjectUnchecked() {
602 ASSERT(!IsFailure());
603 return reinterpret_cast<Object*>(this);
604 }
605 inline Object* ToObjectChecked() {
606 CHECK(!IsFailure());
607 return reinterpret_cast<Object*>(this);
608 }
609
Ben Murdochb0fe1622011-05-05 13:52:32 +0100610#ifdef OBJECT_PRINT
John Reck59135872010-11-02 12:39:01 -0700611 // Prints this object with details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100612 inline void Print() {
613 Print(stdout);
614 };
615 inline void PrintLn() {
616 PrintLn(stdout);
617 }
618 void Print(FILE* out);
619 void PrintLn(FILE* out);
620#endif
621#ifdef DEBUG
John Reck59135872010-11-02 12:39:01 -0700622 // Verifies the object.
623 void Verify();
624#endif
625};
Steve Blocka7e24c12009-10-30 11:49:00 +0000626
627// Object is the abstract superclass for all classes in the
628// object hierarchy.
629// Object does not use any virtual functions to avoid the
630// allocation of the C++ vtable.
631// Since Smi and Failure are subclasses of Object no
632// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700633class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000634 public:
635 // Type testing.
636 inline bool IsSmi();
637 inline bool IsHeapObject();
638 inline bool IsHeapNumber();
639 inline bool IsString();
640 inline bool IsSymbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000641 // See objects-inl.h for more details
642 inline bool IsSeqString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000643 inline bool IsExternalString();
644 inline bool IsExternalTwoByteString();
645 inline bool IsExternalAsciiString();
646 inline bool IsSeqTwoByteString();
647 inline bool IsSeqAsciiString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000648 inline bool IsConsString();
649
650 inline bool IsNumber();
651 inline bool IsByteArray();
652 inline bool IsPixelArray();
Steve Block3ce2e202009-11-05 08:53:23 +0000653 inline bool IsExternalArray();
654 inline bool IsExternalByteArray();
655 inline bool IsExternalUnsignedByteArray();
656 inline bool IsExternalShortArray();
657 inline bool IsExternalUnsignedShortArray();
658 inline bool IsExternalIntArray();
659 inline bool IsExternalUnsignedIntArray();
660 inline bool IsExternalFloatArray();
Steve Blocka7e24c12009-10-30 11:49:00 +0000661 inline bool IsJSObject();
662 inline bool IsJSContextExtensionObject();
663 inline bool IsMap();
664 inline bool IsFixedArray();
665 inline bool IsDescriptorArray();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100666 inline bool IsDeoptimizationInputData();
667 inline bool IsDeoptimizationOutputData();
Steve Blocka7e24c12009-10-30 11:49:00 +0000668 inline bool IsContext();
669 inline bool IsCatchContext();
670 inline bool IsGlobalContext();
671 inline bool IsJSFunction();
672 inline bool IsCode();
673 inline bool IsOddball();
674 inline bool IsSharedFunctionInfo();
675 inline bool IsJSValue();
676 inline bool IsStringWrapper();
677 inline bool IsProxy();
678 inline bool IsBoolean();
679 inline bool IsJSArray();
680 inline bool IsJSRegExp();
681 inline bool IsHashTable();
682 inline bool IsDictionary();
683 inline bool IsSymbolTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100684 inline bool IsJSFunctionResultCache();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100685 inline bool IsNormalizedMapCache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000686 inline bool IsCompilationCacheTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100687 inline bool IsCodeCacheHashTable();
Steve Blocka7e24c12009-10-30 11:49:00 +0000688 inline bool IsMapCache();
689 inline bool IsPrimitive();
690 inline bool IsGlobalObject();
691 inline bool IsJSGlobalObject();
692 inline bool IsJSBuiltinsObject();
693 inline bool IsJSGlobalProxy();
694 inline bool IsUndetectableObject();
695 inline bool IsAccessCheckNeeded();
696 inline bool IsJSGlobalPropertyCell();
697
698 // Returns true if this object is an instance of the specified
699 // function template.
700 inline bool IsInstanceOf(FunctionTemplateInfo* type);
701
702 inline bool IsStruct();
703#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
704 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
705#undef DECLARE_STRUCT_PREDICATE
706
707 // Oddball testing.
708 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000709 INLINE(bool IsNull());
710 INLINE(bool IsTrue());
711 INLINE(bool IsFalse());
712
713 // Extract the number.
714 inline double Number();
715
716 inline bool HasSpecificClassOf(String* name);
717
John Reck59135872010-11-02 12:39:01 -0700718 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
719 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000720
721 // Convert to a JSObject if needed.
722 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700723 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000724
725 // Converts this to a Smi if possible.
726 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700727 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000728
729 void Lookup(String* name, LookupResult* result);
730
731 // Property access.
John Reck59135872010-11-02 12:39:01 -0700732 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
733 MUST_USE_RESULT inline MaybeObject* GetProperty(
734 String* key,
735 PropertyAttributes* attributes);
736 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
737 Object* receiver,
738 String* key,
739 PropertyAttributes* attributes);
740 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
741 LookupResult* result,
742 String* key,
743 PropertyAttributes* attributes);
744 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
745 Object* structure,
746 String* name,
747 Object* holder);
748 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
749 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000750
John Reck59135872010-11-02 12:39:01 -0700751 inline MaybeObject* GetElement(uint32_t index);
752 // For use when we know that no exception can be thrown.
753 inline Object* GetElementNoExceptionThrown(uint32_t index);
754 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000755
756 // Return the object's prototype (might be Heap::null_value()).
757 Object* GetPrototype();
758
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100759 // Tries to convert an object to an array index. Returns true and sets
760 // the output parameter if it succeeds.
761 inline bool ToArrayIndex(uint32_t* index);
762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 // Returns true if this is a JSValue containing a string and the index is
764 // < the length of the string. Used to implement [] on strings.
765 inline bool IsStringObjectWithCharacterAt(uint32_t index);
766
767#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000768 // Verify a pointer is a valid object pointer.
769 static void VerifyPointer(Object* p);
770#endif
771
772 // Prints this object without details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100773 inline void ShortPrint() {
774 ShortPrint(stdout);
775 }
776 void ShortPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000777
778 // Prints this object without details to a message accumulator.
779 void ShortPrint(StringStream* accumulator);
780
781 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
782 static Object* cast(Object* value) { return value; }
783
784 // Layout description.
785 static const int kHeaderSize = 0; // Object does not take up any space.
786
787 private:
788 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
789};
790
791
792// Smi represents integer Numbers that can be stored in 31 bits.
793// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000794// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000795// For long smis it has the following format:
796// [32 bit signed int] [31 bits zero padding] 0
797// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000798class Smi: public Object {
799 public:
800 // Returns the integer value.
801 inline int value();
802
803 // Convert a value to a Smi object.
804 static inline Smi* FromInt(int value);
805
806 static inline Smi* FromIntptr(intptr_t value);
807
808 // Returns whether value can be represented in a Smi.
809 static inline bool IsValid(intptr_t value);
810
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 // Casting.
812 static inline Smi* cast(Object* object);
813
814 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100815 inline void SmiPrint() {
816 SmiPrint(stdout);
817 }
818 void SmiPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 void SmiPrint(StringStream* accumulator);
820#ifdef DEBUG
821 void SmiVerify();
822#endif
823
Steve Block3ce2e202009-11-05 08:53:23 +0000824 static const int kMinValue = (-1 << (kSmiValueSize - 1));
825 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000826
827 private:
828 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
829};
830
831
832// Failure is used for reporting out of memory situations and
833// propagating exceptions through the runtime system. Failure objects
834// are transient and cannot occur as part of the object graph.
835//
836// Failures are a single word, encoded as follows:
837// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +0100838// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000839// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000840// 7 6 4 32 10
841//
Steve Blocka7e24c12009-10-30 11:49:00 +0000842//
843// The low two bits, 0-1, are the failure tag, 11. The next two bits,
844// 2-3, are a failure type tag 'tt' with possible values:
845// 00 RETRY_AFTER_GC
846// 01 EXCEPTION
847// 10 INTERNAL_ERROR
848// 11 OUT_OF_MEMORY_EXCEPTION
849//
850// The next three bits, 4-6, are an allocation space tag 'sss'. The
851// allocation space tag is 000 for all failure types except
852// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
853// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +0000854
855// Failure type tag info.
856const int kFailureTypeTagSize = 2;
857const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
858
John Reck59135872010-11-02 12:39:01 -0700859class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000860 public:
861 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
862 enum Type {
863 RETRY_AFTER_GC = 0,
864 EXCEPTION = 1, // Returning this marker tells the real exception
865 // is in Top::pending_exception.
866 INTERNAL_ERROR = 2,
867 OUT_OF_MEMORY_EXCEPTION = 3
868 };
869
870 inline Type type() const;
871
872 // Returns the space that needs to be collected for RetryAfterGC failures.
873 inline AllocationSpace allocation_space() const;
874
Steve Blocka7e24c12009-10-30 11:49:00 +0000875 inline bool IsInternalError() const;
876 inline bool IsOutOfMemoryException() const;
877
Ben Murdochf87a2032010-10-22 12:50:53 +0100878 static inline Failure* RetryAfterGC(AllocationSpace space);
879 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000880 static inline Failure* Exception();
881 static inline Failure* InternalError();
882 static inline Failure* OutOfMemoryException();
883 // Casting.
John Reck59135872010-11-02 12:39:01 -0700884 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000885
886 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100887 inline void FailurePrint() {
888 FailurePrint(stdout);
889 }
890 void FailurePrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 void FailurePrint(StringStream* accumulator);
892#ifdef DEBUG
893 void FailureVerify();
894#endif
895
896 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000897 inline intptr_t value() const;
898 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000899
900 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
901};
902
903
904// Heap objects typically have a map pointer in their first word. However,
905// during GC other data (eg, mark bits, forwarding addresses) is sometimes
906// encoded in the first word. The class MapWord is an abstraction of the
907// value in a heap object's first word.
908class MapWord BASE_EMBEDDED {
909 public:
910 // Normal state: the map word contains a map pointer.
911
912 // Create a map word from a map pointer.
913 static inline MapWord FromMap(Map* map);
914
915 // View this map word as a map pointer.
916 inline Map* ToMap();
917
918
919 // Scavenge collection: the map word of live objects in the from space
920 // contains a forwarding address (a heap object pointer in the to space).
921
922 // True if this map word is a forwarding address for a scavenge
923 // collection. Only valid during a scavenge collection (specifically,
924 // when all map words are heap object pointers, ie. not during a full GC).
925 inline bool IsForwardingAddress();
926
927 // Create a map word from a forwarding address.
928 static inline MapWord FromForwardingAddress(HeapObject* object);
929
930 // View this map word as a forwarding address.
931 inline HeapObject* ToForwardingAddress();
932
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 // Marking phase of full collection: the map word of live objects is
934 // marked, and may be marked as overflowed (eg, the object is live, its
935 // children have not been visited, and it does not fit in the marking
936 // stack).
937
938 // True if this map word's mark bit is set.
939 inline bool IsMarked();
940
941 // Return this map word but with its mark bit set.
942 inline void SetMark();
943
944 // Return this map word but with its mark bit cleared.
945 inline void ClearMark();
946
947 // True if this map word's overflow bit is set.
948 inline bool IsOverflowed();
949
950 // Return this map word but with its overflow bit set.
951 inline void SetOverflow();
952
953 // Return this map word but with its overflow bit cleared.
954 inline void ClearOverflow();
955
956
957 // Compacting phase of a full compacting collection: the map word of live
958 // objects contains an encoding of the original map address along with the
959 // forwarding address (represented as an offset from the first live object
960 // in the same page as the (old) object address).
961
962 // Create a map word from a map address and a forwarding address offset.
963 static inline MapWord EncodeAddress(Address map_address, int offset);
964
965 // Return the map address encoded in this map word.
966 inline Address DecodeMapAddress(MapSpace* map_space);
967
968 // Return the forwarding offset encoded in this map word.
969 inline int DecodeOffset();
970
971
972 // During serialization: the map word is used to hold an encoded
973 // address, and possibly a mark bit (set and cleared with SetMark
974 // and ClearMark).
975
976 // Create a map word from an encoded address.
977 static inline MapWord FromEncodedAddress(Address address);
978
979 inline Address ToEncodedAddress();
980
981 // Bits used by the marking phase of the garbage collector.
982 //
983 // The first word of a heap object is normally a map pointer. The last two
984 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
985 // mark an object as live and/or overflowed:
986 // last bit = 0, marked as alive
987 // second bit = 1, overflowed
988 // An object is only marked as overflowed when it is marked as live while
989 // the marking stack is overflowed.
990 static const int kMarkingBit = 0; // marking bit
991 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
992 static const int kOverflowBit = 1; // overflow bit
993 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
994
Leon Clarkee46be812010-01-19 14:06:41 +0000995 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
996 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +0000997 // +-----------------+------------------+-----------------+
998 // |forwarding offset|page offset of map|page index of map|
999 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +00001000 // ^ ^ ^
1001 // | | |
1002 // | | kMapPageIndexBits
1003 // | kMapPageOffsetBits
1004 // kForwardingOffsetBits
1005 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1006 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1007#ifdef V8_HOST_ARCH_64_BIT
1008 static const int kMapPageIndexBits = 16;
1009#else
1010 // Use all the 32-bits to encode on a 32-bit platform.
1011 static const int kMapPageIndexBits =
1012 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1013#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001014
1015 static const int kMapPageIndexShift = 0;
1016 static const int kMapPageOffsetShift =
1017 kMapPageIndexShift + kMapPageIndexBits;
1018 static const int kForwardingOffsetShift =
1019 kMapPageOffsetShift + kMapPageOffsetBits;
1020
Leon Clarkee46be812010-01-19 14:06:41 +00001021 // Bit masks covering the different parts the encoding.
1022 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001023 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001024 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001026 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 ~(kMapPageIndexMask | kMapPageOffsetMask);
1028
1029 private:
1030 // HeapObject calls the private constructor and directly reads the value.
1031 friend class HeapObject;
1032
1033 explicit MapWord(uintptr_t value) : value_(value) {}
1034
1035 uintptr_t value_;
1036};
1037
1038
1039// HeapObject is the superclass for all classes describing heap allocated
1040// objects.
1041class HeapObject: public Object {
1042 public:
1043 // [map]: Contains a map which contains the object's reflective
1044 // information.
1045 inline Map* map();
1046 inline void set_map(Map* value);
1047
1048 // During garbage collection, the map word of a heap object does not
1049 // necessarily contain a map pointer.
1050 inline MapWord map_word();
1051 inline void set_map_word(MapWord map_word);
1052
1053 // Converts an address to a HeapObject pointer.
1054 static inline HeapObject* FromAddress(Address address);
1055
1056 // Returns the address of this HeapObject.
1057 inline Address address();
1058
1059 // Iterates over pointers contained in the object (including the Map)
1060 void Iterate(ObjectVisitor* v);
1061
1062 // Iterates over all pointers contained in the object except the
1063 // first map pointer. The object type is given in the first
1064 // parameter. This function does not access the map pointer in the
1065 // object, and so is safe to call while the map pointer is modified.
1066 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1067
Steve Blocka7e24c12009-10-30 11:49:00 +00001068 // Returns the heap object's size in bytes
1069 inline int Size();
1070
1071 // Given a heap object's map pointer, returns the heap size in bytes
1072 // Useful when the map pointer field is used for other purposes.
1073 // GC internal.
1074 inline int SizeFromMap(Map* map);
1075
1076 // Support for the marking heap objects during the marking phase of GC.
1077 // True if the object is marked live.
1078 inline bool IsMarked();
1079
1080 // Mutate this object's map pointer to indicate that the object is live.
1081 inline void SetMark();
1082
1083 // Mutate this object's map pointer to remove the indication that the
1084 // object is live (ie, partially restore the map pointer).
1085 inline void ClearMark();
1086
1087 // True if this object is marked as overflowed. Overflowed objects have
1088 // been reached and marked during marking of the heap, but their children
1089 // have not necessarily been marked and they have not been pushed on the
1090 // marking stack.
1091 inline bool IsOverflowed();
1092
1093 // Mutate this object's map pointer to indicate that the object is
1094 // overflowed.
1095 inline void SetOverflow();
1096
1097 // Mutate this object's map pointer to remove the indication that the
1098 // object is overflowed (ie, partially restore the map pointer).
1099 inline void ClearOverflow();
1100
1101 // Returns the field at offset in obj, as a read/write Object* reference.
1102 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001103 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001104 // during marking GC.
1105 static inline Object** RawField(HeapObject* obj, int offset);
1106
1107 // Casting.
1108 static inline HeapObject* cast(Object* obj);
1109
Leon Clarke4515c472010-02-03 11:58:03 +00001110 // Return the write barrier mode for this. Callers of this function
1111 // must be able to present a reference to an AssertNoAllocation
1112 // object as a sign that they are not going to use this function
1113 // from code that allocates and thus invalidates the returned write
1114 // barrier mode.
1115 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001116
1117 // Dispatched behavior.
1118 void HeapObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001119#ifdef OBJECT_PRINT
1120 inline void HeapObjectPrint() {
1121 HeapObjectPrint(stdout);
1122 }
1123 void HeapObjectPrint(FILE* out);
1124#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001125#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001126 void HeapObjectVerify();
1127 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001128 inline void VerifySmiField(int offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001129#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001130
Ben Murdochb0fe1622011-05-05 13:52:32 +01001131#ifdef OBJECT_PRINT
1132 void PrintHeader(FILE* out, const char* id);
1133#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001134
Ben Murdochb0fe1622011-05-05 13:52:32 +01001135#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 // Verify a pointer is a valid HeapObject pointer that points to object
1137 // areas in the heap.
1138 static void VerifyHeapPointer(Object* p);
1139#endif
1140
1141 // Layout description.
1142 // First field in a heap object is map.
1143 static const int kMapOffset = Object::kHeaderSize;
1144 static const int kHeaderSize = kMapOffset + kPointerSize;
1145
1146 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1147
1148 protected:
1149 // helpers for calling an ObjectVisitor to iterate over pointers in the
1150 // half-open range [start, end) specified as integer offsets
1151 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1152 // as above, for the single element at "offset"
1153 inline void IteratePointer(ObjectVisitor* v, int offset);
1154
Steve Blocka7e24c12009-10-30 11:49:00 +00001155 private:
1156 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1157};
1158
1159
Iain Merrick75681382010-08-19 15:07:18 +01001160#define SLOT_ADDR(obj, offset) \
1161 reinterpret_cast<Object**>((obj)->address() + offset)
1162
1163// This class describes a body of an object of a fixed size
1164// in which all pointer fields are located in the [start_offset, end_offset)
1165// interval.
1166template<int start_offset, int end_offset, int size>
1167class FixedBodyDescriptor {
1168 public:
1169 static const int kStartOffset = start_offset;
1170 static const int kEndOffset = end_offset;
1171 static const int kSize = size;
1172
1173 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1174
1175 template<typename StaticVisitor>
1176 static inline void IterateBody(HeapObject* obj) {
1177 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1178 SLOT_ADDR(obj, end_offset));
1179 }
1180};
1181
1182
1183// This class describes a body of an object of a variable size
1184// in which all pointer fields are located in the [start_offset, object_size)
1185// interval.
1186template<int start_offset>
1187class FlexibleBodyDescriptor {
1188 public:
1189 static const int kStartOffset = start_offset;
1190
1191 static inline void IterateBody(HeapObject* obj,
1192 int object_size,
1193 ObjectVisitor* v);
1194
1195 template<typename StaticVisitor>
1196 static inline void IterateBody(HeapObject* obj, int object_size) {
1197 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1198 SLOT_ADDR(obj, object_size));
1199 }
1200};
1201
1202#undef SLOT_ADDR
1203
1204
Steve Blocka7e24c12009-10-30 11:49:00 +00001205// The HeapNumber class describes heap allocated numbers that cannot be
1206// represented in a Smi (small integer)
1207class HeapNumber: public HeapObject {
1208 public:
1209 // [value]: number value.
1210 inline double value();
1211 inline void set_value(double value);
1212
1213 // Casting.
1214 static inline HeapNumber* cast(Object* obj);
1215
1216 // Dispatched behavior.
1217 Object* HeapNumberToBoolean();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001218 inline void HeapNumberPrint() {
1219 HeapNumberPrint(stdout);
1220 }
1221 void HeapNumberPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001222 void HeapNumberPrint(StringStream* accumulator);
1223#ifdef DEBUG
1224 void HeapNumberVerify();
1225#endif
1226
Steve Block6ded16b2010-05-10 14:33:55 +01001227 inline int get_exponent();
1228 inline int get_sign();
1229
Steve Blocka7e24c12009-10-30 11:49:00 +00001230 // Layout description.
1231 static const int kValueOffset = HeapObject::kHeaderSize;
1232 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1233 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1234 // little endian apart from non-EABI arm which is little endian with big
1235 // endian floating point word ordering!
Steve Block3ce2e202009-11-05 08:53:23 +00001236#if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI)
Steve Blocka7e24c12009-10-30 11:49:00 +00001237 static const int kMantissaOffset = kValueOffset;
1238 static const int kExponentOffset = kValueOffset + 4;
1239#else
1240 static const int kMantissaOffset = kValueOffset + 4;
1241 static const int kExponentOffset = kValueOffset;
1242# define BIG_ENDIAN_FLOATING_POINT 1
1243#endif
1244 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001245 static const uint32_t kSignMask = 0x80000000u;
1246 static const uint32_t kExponentMask = 0x7ff00000u;
1247 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001248 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001249 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001250 static const int kExponentBias = 1023;
1251 static const int kExponentShift = 20;
1252 static const int kMantissaBitsInTopWord = 20;
1253 static const int kNonMantissaBitsInTopWord = 12;
1254
1255 private:
1256 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1257};
1258
1259
1260// The JSObject describes real heap allocated JavaScript objects with
1261// properties.
1262// Note that the map of JSObject changes during execution to enable inline
1263// caching.
1264class JSObject: public HeapObject {
1265 public:
1266 enum DeleteMode { NORMAL_DELETION, FORCE_DELETION };
1267 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001268 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001269 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001270 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001271 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001272 PIXEL_ELEMENTS,
1273 EXTERNAL_BYTE_ELEMENTS,
1274 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1275 EXTERNAL_SHORT_ELEMENTS,
1276 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1277 EXTERNAL_INT_ELEMENTS,
1278 EXTERNAL_UNSIGNED_INT_ELEMENTS,
1279 EXTERNAL_FLOAT_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001280 };
1281
1282 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001283 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001284 // slow case.
1285 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1286 inline void initialize_properties();
1287 inline bool HasFastProperties();
1288 inline StringDictionary* property_dictionary(); // Gets slow properties.
1289
1290 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001291 //
1292 // Elements can be in two general modes: fast and slow. Each mode
1293 // corrensponds to a set of object representations of elements that
1294 // have something in common.
1295 //
1296 // In the fast mode elements is a FixedArray and so each element can
1297 // be quickly accessed. This fact is used in the generated code. The
1298 // elements array can have one of the two maps in this mode:
1299 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1300 // arrays). In the latter case the elements array may be shared by a
1301 // few objects and so before writing to any element the array must
1302 // be copied. Use EnsureWritableFastElements in this case.
1303 //
1304 // In the slow mode elements is either a NumberDictionary or a
1305 // PixelArray or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001306 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001307 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001308 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 inline ElementsKind GetElementsKind();
1310 inline bool HasFastElements();
1311 inline bool HasDictionaryElements();
1312 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001313 inline bool HasExternalArrayElements();
1314 inline bool HasExternalByteElements();
1315 inline bool HasExternalUnsignedByteElements();
1316 inline bool HasExternalShortElements();
1317 inline bool HasExternalUnsignedShortElements();
1318 inline bool HasExternalIntElements();
1319 inline bool HasExternalUnsignedIntElements();
1320 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001321 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001322 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001323 // Requires: this->HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001324 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001325
1326 // Collects elements starting at index 0.
1327 // Undefined values are placed after non-undefined values.
1328 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001329 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001330 // As PrepareElementsForSort, but only on objects where elements is
1331 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001332 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001333
John Reck59135872010-11-02 12:39:01 -07001334 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1335 Object* value,
1336 PropertyAttributes attributes);
1337 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1338 String* key,
1339 Object* value,
1340 PropertyAttributes attributes);
1341 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1342 LookupResult* result,
1343 String* name,
1344 Object* value);
1345 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(Object* structure,
1346 String* name,
1347 Object* value,
1348 JSObject* holder);
1349 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1350 Object* value);
1351 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1352 String* name,
1353 Object* value,
1354 PropertyAttributes attributes);
1355 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1356 String* name,
1357 Object* value,
1358 PropertyAttributes attributes);
1359 MUST_USE_RESULT MaybeObject* IgnoreAttributesAndSetLocalProperty(
1360 String* key,
1361 Object* value,
1362 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001363
1364 // Retrieve a value in a normalized object given a lookup result.
1365 // Handles the special representation of JS global objects.
1366 Object* GetNormalizedProperty(LookupResult* result);
1367
1368 // Sets the property value in a normalized object given a lookup result.
1369 // Handles the special representation of JS global objects.
1370 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1371
1372 // Sets the property value in a normalized object given (key, value, details).
1373 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001374 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1375 Object* value,
1376 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001377
1378 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001379 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1380 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001381
Steve Blocka7e24c12009-10-30 11:49:00 +00001382 // Returns the class name ([[Class]] property in the specification).
1383 String* class_name();
1384
1385 // Returns the constructor name (the name (possibly, inferred name) of the
1386 // function that was used to instantiate the object).
1387 String* constructor_name();
1388
1389 // Retrieve interceptors.
1390 InterceptorInfo* GetNamedInterceptor();
1391 InterceptorInfo* GetIndexedInterceptor();
1392
1393 inline PropertyAttributes GetPropertyAttribute(String* name);
1394 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1395 String* name);
1396 PropertyAttributes GetLocalPropertyAttribute(String* name);
1397
John Reck59135872010-11-02 12:39:01 -07001398 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1399 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001400 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001401 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001402 Object* LookupAccessor(String* name, bool is_getter);
1403
John Reck59135872010-11-02 12:39:01 -07001404 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001405
Steve Blocka7e24c12009-10-30 11:49:00 +00001406 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001407 MaybeObject* GetPropertyWithFailedAccessCheck(
1408 Object* receiver,
1409 LookupResult* result,
1410 String* name,
1411 PropertyAttributes* attributes);
1412 MaybeObject* GetPropertyWithInterceptor(
1413 JSObject* receiver,
1414 String* name,
1415 PropertyAttributes* attributes);
1416 MaybeObject* GetPropertyPostInterceptor(
1417 JSObject* receiver,
1418 String* name,
1419 PropertyAttributes* attributes);
1420 MaybeObject* GetLocalPropertyPostInterceptor(JSObject* receiver,
1421 String* name,
1422 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001423
1424 // Returns true if this is an instance of an api function and has
1425 // been modified since it was created. May give false positives.
1426 bool IsDirty();
1427
1428 bool HasProperty(String* name) {
1429 return GetPropertyAttribute(name) != ABSENT;
1430 }
1431
1432 // Can cause a GC if it hits an interceptor.
1433 bool HasLocalProperty(String* name) {
1434 return GetLocalPropertyAttribute(name) != ABSENT;
1435 }
1436
Steve Blockd0582a62009-12-15 09:54:21 +00001437 // If the receiver is a JSGlobalProxy this method will return its prototype,
1438 // otherwise the result is the receiver itself.
1439 inline Object* BypassGlobalProxy();
1440
1441 // Accessors for hidden properties object.
1442 //
1443 // Hidden properties are not local properties of the object itself.
1444 // Instead they are stored on an auxiliary JSObject stored as a local
1445 // property with a special name Heap::hidden_symbol(). But if the
1446 // receiver is a JSGlobalProxy then the auxiliary object is a property
1447 // of its prototype.
1448 //
1449 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1450 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1451 // holder.
1452 //
1453 // These accessors do not touch interceptors or accessors.
1454 inline bool HasHiddenPropertiesObject();
1455 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001456 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1457 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001458
John Reck59135872010-11-02 12:39:01 -07001459 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1460 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001461
1462 // Tests for the fast common case for property enumeration.
1463 bool IsSimpleEnum();
1464
1465 // Do we want to keep the elements in fast case when increasing the
1466 // capacity?
1467 bool ShouldConvertToSlowElements(int new_capacity);
1468 // Returns true if the backing storage for the slow-case elements of
1469 // this object takes up nearly as much space as a fast-case backing
1470 // storage would. In that case the JSObject should have fast
1471 // elements.
1472 bool ShouldConvertToFastElements();
1473
1474 // Return the object's prototype (might be Heap::null_value()).
1475 inline Object* GetPrototype();
1476
Andrei Popescu402d9372010-02-26 13:31:12 +00001477 // Set the object's prototype (only JSObject and null are allowed).
John Reck59135872010-11-02 12:39:01 -07001478 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1479 bool skip_hidden_prototypes);
Andrei Popescu402d9372010-02-26 13:31:12 +00001480
Steve Blocka7e24c12009-10-30 11:49:00 +00001481 // Tells whether the index'th element is present.
1482 inline bool HasElement(uint32_t index);
1483 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001484
1485 // Tells whether the index'th element is present and how it is stored.
1486 enum LocalElementType {
1487 // There is no element with given index.
1488 UNDEFINED_ELEMENT,
1489
1490 // Element with given index is handled by interceptor.
1491 INTERCEPTED_ELEMENT,
1492
1493 // Element with given index is character in string.
1494 STRING_CHARACTER_ELEMENT,
1495
1496 // Element with given index is stored in fast backing store.
1497 FAST_ELEMENT,
1498
1499 // Element with given index is stored in slow backing store.
1500 DICTIONARY_ELEMENT
1501 };
1502
1503 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001504
1505 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1506 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1507
John Reck59135872010-11-02 12:39:01 -07001508 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001509
1510 // Set the index'th array element.
1511 // A Failure object is returned if GC is needed.
John Reck59135872010-11-02 12:39:01 -07001512 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001513
1514 // Returns the index'th element.
1515 // The undefined object if index is out of bounds.
John Reck59135872010-11-02 12:39:01 -07001516 MaybeObject* GetElementWithReceiver(JSObject* receiver, uint32_t index);
1517 MaybeObject* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001518
John Reck59135872010-11-02 12:39:01 -07001519 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1520 int length);
1521 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001522
1523 // Lookup interceptors are used for handling properties controlled by host
1524 // objects.
1525 inline bool HasNamedInterceptor();
1526 inline bool HasIndexedInterceptor();
1527
1528 // Support functions for v8 api (needed for correct interceptor behavior).
1529 bool HasRealNamedProperty(String* key);
1530 bool HasRealElementProperty(uint32_t index);
1531 bool HasRealNamedCallbackProperty(String* key);
1532
1533 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001534 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001535
1536 // Get the header size for a JSObject. Used to compute the index of
1537 // internal fields as well as the number of internal fields.
1538 inline int GetHeaderSize();
1539
1540 inline int GetInternalFieldCount();
1541 inline Object* GetInternalField(int index);
1542 inline void SetInternalField(int index, Object* value);
1543
1544 // Lookup a property. If found, the result is valid and has
1545 // detailed information.
1546 void LocalLookup(String* name, LookupResult* result);
1547 void Lookup(String* name, LookupResult* result);
1548
1549 // The following lookup functions skip interceptors.
1550 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1551 void LookupRealNamedProperty(String* name, LookupResult* result);
1552 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1553 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Leon Clarkef7060e22010-06-03 12:02:55 +01001554 bool SetElementWithCallbackSetterInPrototypes(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001555 void LookupCallback(String* name, LookupResult* result);
1556
1557 // Returns the number of properties on this object filtering out properties
1558 // with the specified attributes (ignoring interceptors).
1559 int NumberOfLocalProperties(PropertyAttributes filter);
1560 // Returns the number of enumerable properties (ignoring interceptors).
1561 int NumberOfEnumProperties();
1562 // Fill in details for properties into storage starting at the specified
1563 // index.
1564 void GetLocalPropertyNames(FixedArray* storage, int index);
1565
1566 // Returns the number of properties on this object filtering out properties
1567 // with the specified attributes (ignoring interceptors).
1568 int NumberOfLocalElements(PropertyAttributes filter);
1569 // Returns the number of enumerable elements (ignoring interceptors).
1570 int NumberOfEnumElements();
1571 // Returns the number of elements on this object filtering out elements
1572 // with the specified attributes (ignoring interceptors).
1573 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1574 // Count and fill in the enumerable elements into storage.
1575 // (storage->length() == NumberOfEnumElements()).
1576 // If storage is NULL, will count the elements without adding
1577 // them to any storage.
1578 // Returns the number of enumerable elements.
1579 int GetEnumElementKeys(FixedArray* storage);
1580
1581 // Add a property to a fast-case object using a map transition to
1582 // new_map.
John Reck59135872010-11-02 12:39:01 -07001583 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1584 String* name,
1585 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001586
1587 // Add a constant function property to a fast-case object.
1588 // This leaves a CONSTANT_TRANSITION in the old map, and
1589 // if it is called on a second object with this map, a
1590 // normal property is added instead, with a map transition.
1591 // This avoids the creation of many maps with the same constant
1592 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001593 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1594 String* name,
1595 JSFunction* function,
1596 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001597
John Reck59135872010-11-02 12:39:01 -07001598 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1599 String* name,
1600 Object* value,
1601 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001602
1603 // Converts a descriptor of any other type to a real field,
1604 // backed by the properties array. Descriptors of visible
1605 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1606 // Converts the descriptor on the original object's map to a
1607 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001608 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001609 String* name,
1610 Object* new_value,
1611 PropertyAttributes attributes);
1612
1613 // Converts a descriptor of any other type to a real field,
1614 // backed by the properties array. Descriptors of visible
1615 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001616 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1617 String* name,
1618 Object* new_value,
1619 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001620
1621 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001622 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1623 Object* value,
1624 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001625
1626 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001627 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1628 Object* value,
1629 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001630
1631 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001632 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1633 Object* value,
1634 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001635
1636 // Convert the object to use the canonical dictionary
1637 // representation. If the object is expected to have additional properties
1638 // added this number can be indicated to have the backing store allocated to
1639 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001640 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1641 PropertyNormalizationMode mode,
1642 int expected_additional_properties);
1643 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001644
John Reck59135872010-11-02 12:39:01 -07001645 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001646
Steve Blocka7e24c12009-10-30 11:49:00 +00001647 // Transform slow named properties to fast variants.
1648 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001649 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1650 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001651
1652 // Access fast-case object properties at index.
1653 inline Object* FastPropertyAt(int index);
1654 inline Object* FastPropertyAtPut(int index, Object* value);
1655
1656 // Access to in object properties.
1657 inline Object* InObjectPropertyAt(int index);
1658 inline Object* InObjectPropertyAtPut(int index,
1659 Object* value,
1660 WriteBarrierMode mode
1661 = UPDATE_WRITE_BARRIER);
1662
1663 // initializes the body after properties slot, properties slot is
1664 // initialized by set_properties
1665 // Note: this call does not update write barrier, it is caller's
1666 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001667 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001668
1669 // Check whether this object references another object
1670 bool ReferencesObject(Object* obj);
1671
1672 // Casting.
1673 static inline JSObject* cast(Object* obj);
1674
Steve Block8defd9f2010-07-08 12:39:36 +01001675 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001676 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001677
1678
Steve Blocka7e24c12009-10-30 11:49:00 +00001679 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001680 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001681#ifdef OBJECT_PRINT
1682 inline void JSObjectPrint() {
1683 JSObjectPrint(stdout);
1684 }
1685 void JSObjectPrint(FILE* out);
1686#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001687#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001688 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001689#endif
1690#ifdef OBJECT_PRINT
1691 inline void PrintProperties() {
1692 PrintProperties(stdout);
1693 }
1694 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001695
Ben Murdochb0fe1622011-05-05 13:52:32 +01001696 inline void PrintElements() {
1697 PrintElements(stdout);
1698 }
1699 void PrintElements(FILE* out);
1700#endif
1701
1702#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001703 // Structure for collecting spill information about JSObjects.
1704 class SpillInformation {
1705 public:
1706 void Clear();
1707 void Print();
1708 int number_of_objects_;
1709 int number_of_objects_with_fast_properties_;
1710 int number_of_objects_with_fast_elements_;
1711 int number_of_fast_used_fields_;
1712 int number_of_fast_unused_fields_;
1713 int number_of_slow_used_properties_;
1714 int number_of_slow_unused_properties_;
1715 int number_of_fast_used_elements_;
1716 int number_of_fast_unused_elements_;
1717 int number_of_slow_used_elements_;
1718 int number_of_slow_unused_elements_;
1719 };
1720
1721 void IncrementSpillStatistics(SpillInformation* info);
1722#endif
1723 Object* SlowReverseLookup(Object* value);
1724
Steve Block8defd9f2010-07-08 12:39:36 +01001725 // Maximal number of fast properties for the JSObject. Used to
1726 // restrict the number of map transitions to avoid an explosion in
1727 // the number of maps for objects used as dictionaries.
1728 inline int MaxFastProperties();
1729
Leon Clarkee46be812010-01-19 14:06:41 +00001730 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1731 // Also maximal value of JSArray's length property.
1732 static const uint32_t kMaxElementCount = 0xffffffffu;
1733
Steve Blocka7e24c12009-10-30 11:49:00 +00001734 static const uint32_t kMaxGap = 1024;
1735 static const int kMaxFastElementsLength = 5000;
1736 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001737 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00001738 static const int kMaxInstanceSize = 255 * kPointerSize;
1739 // When extending the backing storage for property values, we increase
1740 // its size by more than the 1 entry necessary, so sequentially adding fields
1741 // to the same object requires fewer allocations and copies.
1742 static const int kFieldsAdded = 3;
1743
1744 // Layout description.
1745 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1746 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1747 static const int kHeaderSize = kElementsOffset + kPointerSize;
1748
1749 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1750
Iain Merrick75681382010-08-19 15:07:18 +01001751 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1752 public:
1753 static inline int SizeOf(Map* map, HeapObject* object);
1754 };
1755
Steve Blocka7e24c12009-10-30 11:49:00 +00001756 private:
John Reck59135872010-11-02 12:39:01 -07001757 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1758 Object* structure,
1759 uint32_t index,
1760 Object* holder);
1761 MaybeObject* SetElementWithCallback(Object* structure,
1762 uint32_t index,
1763 Object* value,
1764 JSObject* holder);
1765 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(uint32_t index,
1766 Object* value);
1767 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(uint32_t index,
1768 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001769
John Reck59135872010-11-02 12:39:01 -07001770 MaybeObject* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001771
John Reck59135872010-11-02 12:39:01 -07001772 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1773 DeleteMode mode);
1774 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001775
John Reck59135872010-11-02 12:39:01 -07001776 MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1777 DeleteMode mode);
1778 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001779
1780 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1781 String* name,
1782 bool continue_search);
1783 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1784 String* name,
1785 bool continue_search);
1786 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1787 Object* receiver,
1788 LookupResult* result,
1789 String* name,
1790 bool continue_search);
1791 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1792 LookupResult* result,
1793 String* name,
1794 bool continue_search);
1795
1796 // Returns true if most of the elements backing storage is used.
1797 bool HasDenseElements();
1798
Leon Clarkef7060e22010-06-03 12:02:55 +01001799 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07001800 MUST_USE_RESULT MaybeObject* SetElementCallback(
1801 uint32_t index,
1802 Object* structure,
1803 PropertyAttributes attributes);
1804 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1805 String* name,
1806 Object* structure,
1807 PropertyAttributes attributes);
1808 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
1809 String* name,
1810 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001811
1812 void LookupInDescriptor(String* name, LookupResult* result);
1813
1814 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1815};
1816
1817
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001818// FixedArray describes fixed-sized arrays with element type Object*.
1819class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001820 public:
1821 // [length]: length of the array.
1822 inline int length();
1823 inline void set_length(int value);
1824
Steve Blocka7e24c12009-10-30 11:49:00 +00001825 // Setter and getter for elements.
1826 inline Object* get(int index);
1827 // Setter that uses write barrier.
1828 inline void set(int index, Object* value);
1829
1830 // Setter that doesn't need write barrier).
1831 inline void set(int index, Smi* value);
1832 // Setter with explicit barrier mode.
1833 inline void set(int index, Object* value, WriteBarrierMode mode);
1834
1835 // Setters for frequently used oddballs located in old space.
1836 inline void set_undefined(int index);
1837 inline void set_null(int index);
1838 inline void set_the_hole(int index);
1839
Iain Merrick75681382010-08-19 15:07:18 +01001840 // Setters with less debug checks for the GC to use.
1841 inline void set_unchecked(int index, Smi* value);
1842 inline void set_null_unchecked(int index);
Ben Murdochf87a2032010-10-22 12:50:53 +01001843 inline void set_unchecked(int index, Object* value, WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01001844
Steve Block6ded16b2010-05-10 14:33:55 +01001845 // Gives access to raw memory which stores the array's data.
1846 inline Object** data_start();
1847
Steve Blocka7e24c12009-10-30 11:49:00 +00001848 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07001849 MUST_USE_RESULT inline MaybeObject* Copy();
1850 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001851
1852 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07001853 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00001854
1855 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07001856 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00001857
1858 // Copy a sub array from the receiver to dest.
1859 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1860
1861 // Garbage collection support.
1862 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1863
1864 // Code Generation support.
1865 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1866
1867 // Casting.
1868 static inline FixedArray* cast(Object* obj);
1869
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001870 // Layout description.
1871 // Length is smi tagged when it is stored.
1872 static const int kLengthOffset = HeapObject::kHeaderSize;
1873 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001874
1875 // Maximal allowed size, in bytes, of a single FixedArray.
1876 // Prevents overflowing size computations, as well as extreme memory
1877 // consumption.
1878 static const int kMaxSize = 512 * MB;
1879 // Maximally allowed length of a FixedArray.
1880 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001881
1882 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001883#ifdef OBJECT_PRINT
1884 inline void FixedArrayPrint() {
1885 FixedArrayPrint(stdout);
1886 }
1887 void FixedArrayPrint(FILE* out);
1888#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001889#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001890 void FixedArrayVerify();
1891 // Checks if two FixedArrays have identical contents.
1892 bool IsEqualTo(FixedArray* other);
1893#endif
1894
1895 // Swap two elements in a pair of arrays. If this array and the
1896 // numbers array are the same object, the elements are only swapped
1897 // once.
1898 void SwapPairs(FixedArray* numbers, int i, int j);
1899
1900 // Sort prefix of this array and the numbers array as pairs wrt. the
1901 // numbers. If the numbers array and the this array are the same
1902 // object, the prefix of this array is sorted.
1903 void SortPairs(FixedArray* numbers, uint32_t len);
1904
Iain Merrick75681382010-08-19 15:07:18 +01001905 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
1906 public:
1907 static inline int SizeOf(Map* map, HeapObject* object) {
1908 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
1909 }
1910 };
1911
Steve Blocka7e24c12009-10-30 11:49:00 +00001912 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001913 // Set operation on FixedArray without using write barriers. Can
1914 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001915 static inline void fast_set(FixedArray* array, int index, Object* value);
1916
1917 private:
1918 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1919};
1920
1921
1922// DescriptorArrays are fixed arrays used to hold instance descriptors.
1923// The format of the these objects is:
1924// [0]: point to a fixed array with (value, detail) pairs.
1925// [1]: next enumeration index (Smi), or pointer to small fixed array:
1926// [0]: next enumeration index (Smi)
1927// [1]: pointer to fixed array with enum cache
1928// [2]: first key
1929// [length() - 1]: last key
1930//
1931class DescriptorArray: public FixedArray {
1932 public:
1933 // Is this the singleton empty_descriptor_array?
1934 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001935
Steve Blocka7e24c12009-10-30 11:49:00 +00001936 // Returns the number of descriptors in the array.
1937 int number_of_descriptors() {
1938 return IsEmpty() ? 0 : length() - kFirstIndex;
1939 }
1940
1941 int NextEnumerationIndex() {
1942 if (IsEmpty()) return PropertyDetails::kInitialIndex;
1943 Object* obj = get(kEnumerationIndexIndex);
1944 if (obj->IsSmi()) {
1945 return Smi::cast(obj)->value();
1946 } else {
1947 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1948 return Smi::cast(index)->value();
1949 }
1950 }
1951
1952 // Set next enumeration index and flush any enum cache.
1953 void SetNextEnumerationIndex(int value) {
1954 if (!IsEmpty()) {
1955 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1956 }
1957 }
1958 bool HasEnumCache() {
1959 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
1960 }
1961
1962 Object* GetEnumCache() {
1963 ASSERT(HasEnumCache());
1964 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1965 return bridge->get(kEnumCacheBridgeCacheIndex);
1966 }
1967
1968 // Initialize or change the enum cache,
1969 // using the supplied storage for the small "bridge".
1970 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1971
1972 // Accessors for fetching instance descriptor at descriptor number.
1973 inline String* GetKey(int descriptor_number);
1974 inline Object* GetValue(int descriptor_number);
1975 inline Smi* GetDetails(int descriptor_number);
1976 inline PropertyType GetType(int descriptor_number);
1977 inline int GetFieldIndex(int descriptor_number);
1978 inline JSFunction* GetConstantFunction(int descriptor_number);
1979 inline Object* GetCallbacksObject(int descriptor_number);
1980 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
1981 inline bool IsProperty(int descriptor_number);
1982 inline bool IsTransition(int descriptor_number);
1983 inline bool IsNullDescriptor(int descriptor_number);
1984 inline bool IsDontEnum(int descriptor_number);
1985
1986 // Accessor for complete descriptor.
1987 inline void Get(int descriptor_number, Descriptor* desc);
1988 inline void Set(int descriptor_number, Descriptor* desc);
1989
1990 // Transfer complete descriptor from another descriptor array to
1991 // this one.
1992 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
1993
1994 // Copy the descriptor array, insert a new descriptor and optionally
1995 // remove map transitions. If the descriptor is already present, it is
1996 // replaced. If a replaced descriptor is a real property (not a transition
1997 // or null), its enumeration index is kept as is.
1998 // If adding a real property, map transitions must be removed. If adding
1999 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002000 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2001 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002002
2003 // Remove all transitions. Return a copy of the array with all transitions
2004 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002005 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002006
2007 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002008 // Does not check for duplicates.
2009 void SortUnchecked();
2010
2011 // Sort the instance descriptors by the hash codes of their keys.
2012 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00002013 void Sort();
2014
2015 // Search the instance descriptors for given name.
2016 inline int Search(String* name);
2017
Iain Merrick75681382010-08-19 15:07:18 +01002018 // As the above, but uses DescriptorLookupCache and updates it when
2019 // necessary.
2020 inline int SearchWithCache(String* name);
2021
Steve Blocka7e24c12009-10-30 11:49:00 +00002022 // Tells whether the name is present int the array.
2023 bool Contains(String* name) { return kNotFound != Search(name); }
2024
2025 // Perform a binary search in the instance descriptors represented
2026 // by this fixed array. low and high are descriptor indices. If there
2027 // are three instance descriptors in this array it should be called
2028 // with low=0 and high=2.
2029 int BinarySearch(String* name, int low, int high);
2030
2031 // Perform a linear search in the instance descriptors represented
2032 // by this fixed array. len is the number of descriptor indices that are
2033 // valid. Does not require the descriptors to be sorted.
2034 int LinearSearch(String* name, int len);
2035
2036 // Allocates a DescriptorArray, but returns the singleton
2037 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002038 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002039
2040 // Casting.
2041 static inline DescriptorArray* cast(Object* obj);
2042
2043 // Constant for denoting key was not found.
2044 static const int kNotFound = -1;
2045
2046 static const int kContentArrayIndex = 0;
2047 static const int kEnumerationIndexIndex = 1;
2048 static const int kFirstIndex = 2;
2049
2050 // The length of the "bridge" to the enum cache.
2051 static const int kEnumCacheBridgeLength = 2;
2052 static const int kEnumCacheBridgeEnumIndex = 0;
2053 static const int kEnumCacheBridgeCacheIndex = 1;
2054
2055 // Layout description.
2056 static const int kContentArrayOffset = FixedArray::kHeaderSize;
2057 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2058 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2059
2060 // Layout description for the bridge array.
2061 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2062 static const int kEnumCacheBridgeCacheOffset =
2063 kEnumCacheBridgeEnumOffset + kPointerSize;
2064
Ben Murdochb0fe1622011-05-05 13:52:32 +01002065#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002066 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002067 inline void PrintDescriptors() {
2068 PrintDescriptors(stdout);
2069 }
2070 void PrintDescriptors(FILE* out);
2071#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002072
Ben Murdochb0fe1622011-05-05 13:52:32 +01002073#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002074 // Is the descriptor array sorted and without duplicates?
2075 bool IsSortedNoDuplicates();
2076
2077 // Are two DescriptorArrays equal?
2078 bool IsEqualTo(DescriptorArray* other);
2079#endif
2080
2081 // The maximum number of descriptors we want in a descriptor array (should
2082 // fit in a page).
2083 static const int kMaxNumberOfDescriptors = 1024 + 512;
2084
2085 private:
2086 // Conversion from descriptor number to array indices.
2087 static int ToKeyIndex(int descriptor_number) {
2088 return descriptor_number+kFirstIndex;
2089 }
Leon Clarkee46be812010-01-19 14:06:41 +00002090
2091 static int ToDetailsIndex(int descriptor_number) {
2092 return (descriptor_number << 1) + 1;
2093 }
2094
Steve Blocka7e24c12009-10-30 11:49:00 +00002095 static int ToValueIndex(int descriptor_number) {
2096 return descriptor_number << 1;
2097 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002098
2099 bool is_null_descriptor(int descriptor_number) {
2100 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2101 NULL_DESCRIPTOR;
2102 }
2103 // Swap operation on FixedArray without using write barriers.
2104 static inline void fast_swap(FixedArray* array, int first, int second);
2105
2106 // Swap descriptor first and second.
2107 inline void Swap(int first, int second);
2108
2109 FixedArray* GetContentArray() {
2110 return FixedArray::cast(get(kContentArrayIndex));
2111 }
2112 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2113};
2114
2115
2116// HashTable is a subclass of FixedArray that implements a hash table
2117// that uses open addressing and quadratic probing.
2118//
2119// In order for the quadratic probing to work, elements that have not
2120// yet been used and elements that have been deleted are
2121// distinguished. Probing continues when deleted elements are
2122// encountered and stops when unused elements are encountered.
2123//
2124// - Elements with key == undefined have not been used yet.
2125// - Elements with key == null have been deleted.
2126//
2127// The hash table class is parameterized with a Shape and a Key.
2128// Shape must be a class with the following interface:
2129// class ExampleShape {
2130// public:
2131// // Tells whether key matches other.
2132// static bool IsMatch(Key key, Object* other);
2133// // Returns the hash value for key.
2134// static uint32_t Hash(Key key);
2135// // Returns the hash value for object.
2136// static uint32_t HashForObject(Key key, Object* object);
2137// // Convert key to an object.
2138// static inline Object* AsObject(Key key);
2139// // The prefix size indicates number of elements in the beginning
2140// // of the backing storage.
2141// static const int kPrefixSize = ..;
2142// // The Element size indicates number of elements per entry.
2143// static const int kEntrySize = ..;
2144// };
Steve Block3ce2e202009-11-05 08:53:23 +00002145// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002146// beginning of the backing storage that can be used for non-element
2147// information by subclasses.
2148
2149template<typename Shape, typename Key>
2150class HashTable: public FixedArray {
2151 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002152 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002153 int NumberOfElements() {
2154 return Smi::cast(get(kNumberOfElementsIndex))->value();
2155 }
2156
Leon Clarkee46be812010-01-19 14:06:41 +00002157 // Returns the number of deleted elements in the hash table.
2158 int NumberOfDeletedElements() {
2159 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2160 }
2161
Steve Block3ce2e202009-11-05 08:53:23 +00002162 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002163 int Capacity() {
2164 return Smi::cast(get(kCapacityIndex))->value();
2165 }
2166
2167 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002168 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002169 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2170
2171 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002172 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002173 void ElementRemoved() {
2174 SetNumberOfElements(NumberOfElements() - 1);
2175 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2176 }
2177 void ElementsRemoved(int n) {
2178 SetNumberOfElements(NumberOfElements() - n);
2179 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2180 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002181
Steve Block3ce2e202009-11-05 08:53:23 +00002182 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002183 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002184 int at_least_space_for,
2185 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002186
2187 // Returns the key at entry.
2188 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2189
2190 // Tells whether k is a real key. Null and undefined are not allowed
2191 // as keys and can be used to indicate missing or deleted elements.
2192 bool IsKey(Object* k) {
2193 return !k->IsNull() && !k->IsUndefined();
2194 }
2195
2196 // Garbage collection support.
2197 void IteratePrefix(ObjectVisitor* visitor);
2198 void IterateElements(ObjectVisitor* visitor);
2199
2200 // Casting.
2201 static inline HashTable* cast(Object* obj);
2202
2203 // Compute the probe offset (quadratic probing).
2204 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2205 return (n + n * n) >> 1;
2206 }
2207
2208 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002209 static const int kNumberOfDeletedElementsIndex = 1;
2210 static const int kCapacityIndex = 2;
2211 static const int kPrefixStartIndex = 3;
2212 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002213 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002214 static const int kEntrySize = Shape::kEntrySize;
2215 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002216 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002217 static const int kCapacityOffset =
2218 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002219
2220 // Constant used for denoting a absent entry.
2221 static const int kNotFound = -1;
2222
Leon Clarkee46be812010-01-19 14:06:41 +00002223 // Maximal capacity of HashTable. Based on maximal length of underlying
2224 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2225 // cannot overflow.
2226 static const int kMaxCapacity =
2227 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2228
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002229 // Find entry for key otherwise return kNotFound.
Steve Blocka7e24c12009-10-30 11:49:00 +00002230 int FindEntry(Key key);
2231
2232 protected:
2233
2234 // Find the entry at which to insert element with the given key that
2235 // has the given hash value.
2236 uint32_t FindInsertionEntry(uint32_t hash);
2237
2238 // Returns the index for an entry (of the key)
2239 static inline int EntryToIndex(int entry) {
2240 return (entry * kEntrySize) + kElementsStartIndex;
2241 }
2242
Steve Block3ce2e202009-11-05 08:53:23 +00002243 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002244 void SetNumberOfElements(int nof) {
2245 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2246 }
2247
Leon Clarkee46be812010-01-19 14:06:41 +00002248 // Update the number of deleted elements in the hash table.
2249 void SetNumberOfDeletedElements(int nod) {
2250 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2251 }
2252
Steve Blocka7e24c12009-10-30 11:49:00 +00002253 // Sets the capacity of the hash table.
2254 void SetCapacity(int capacity) {
2255 // To scale a computed hash code to fit within the hash table, we
2256 // use bit-wise AND with a mask, so the capacity must be positive
2257 // and non-zero.
2258 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002259 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002260 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2261 }
2262
2263
2264 // Returns probe entry.
2265 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2266 ASSERT(IsPowerOf2(size));
2267 return (hash + GetProbeOffset(number)) & (size - 1);
2268 }
2269
Leon Clarkee46be812010-01-19 14:06:41 +00002270 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2271 return hash & (size - 1);
2272 }
2273
2274 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2275 return (last + number) & (size - 1);
2276 }
2277
Steve Blocka7e24c12009-10-30 11:49:00 +00002278 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002279 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002280};
2281
2282
2283
2284// HashTableKey is an abstract superclass for virtual key behavior.
2285class HashTableKey {
2286 public:
2287 // Returns whether the other object matches this key.
2288 virtual bool IsMatch(Object* other) = 0;
2289 // Returns the hash value for this key.
2290 virtual uint32_t Hash() = 0;
2291 // Returns the hash value for object.
2292 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002293 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002294 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002295 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002296 // Required.
2297 virtual ~HashTableKey() {}
2298};
2299
2300class SymbolTableShape {
2301 public:
2302 static bool IsMatch(HashTableKey* key, Object* value) {
2303 return key->IsMatch(value);
2304 }
2305 static uint32_t Hash(HashTableKey* key) {
2306 return key->Hash();
2307 }
2308 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2309 return key->HashForObject(object);
2310 }
John Reck59135872010-11-02 12:39:01 -07002311 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002312 return key->AsObject();
2313 }
2314
2315 static const int kPrefixSize = 0;
2316 static const int kEntrySize = 1;
2317};
2318
2319// SymbolTable.
2320//
2321// No special elements in the prefix and the element size is 1
2322// because only the symbol itself (the key) needs to be stored.
2323class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2324 public:
2325 // Find symbol in the symbol table. If it is not there yet, it is
2326 // added. The return value is the symbol table which might have
2327 // been enlarged. If the return value is not a failure, the symbol
2328 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002329 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
2330 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002331
2332 // Looks up a symbol that is equal to the given string and returns
2333 // true if it is found, assigning the symbol to the given output
2334 // parameter.
2335 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002336 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002337
2338 // Casting.
2339 static inline SymbolTable* cast(Object* obj);
2340
2341 private:
John Reck59135872010-11-02 12:39:01 -07002342 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002343
2344 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2345};
2346
2347
2348class MapCacheShape {
2349 public:
2350 static bool IsMatch(HashTableKey* key, Object* value) {
2351 return key->IsMatch(value);
2352 }
2353 static uint32_t Hash(HashTableKey* key) {
2354 return key->Hash();
2355 }
2356
2357 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2358 return key->HashForObject(object);
2359 }
2360
John Reck59135872010-11-02 12:39:01 -07002361 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002362 return key->AsObject();
2363 }
2364
2365 static const int kPrefixSize = 0;
2366 static const int kEntrySize = 2;
2367};
2368
2369
2370// MapCache.
2371//
2372// Maps keys that are a fixed array of symbols to a map.
2373// Used for canonicalize maps for object literals.
2374class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2375 public:
2376 // Find cached value for a string key, otherwise return null.
2377 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002378 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002379 static inline MapCache* cast(Object* obj);
2380
2381 private:
2382 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2383};
2384
2385
2386template <typename Shape, typename Key>
2387class Dictionary: public HashTable<Shape, Key> {
2388 public:
2389
2390 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2391 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2392 }
2393
2394 // Returns the value at entry.
2395 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002396 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002397 }
2398
2399 // Set the value for entry.
2400 void ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002401 // Check that this value can actually be written.
2402 PropertyDetails details = DetailsAt(entry);
2403 // If a value has not been initilized we allow writing to it even if
2404 // it is read only (a declared const that has not been initialized).
2405 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) return;
Steve Block6ded16b2010-05-10 14:33:55 +01002406 this->set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002407 }
2408
2409 // Returns the property details for the property at entry.
2410 PropertyDetails DetailsAt(int entry) {
2411 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2412 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002413 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002414 }
2415
2416 // Set the details for entry.
2417 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002418 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002419 }
2420
2421 // Sorting support
2422 void CopyValuesTo(FixedArray* elements);
2423
2424 // Delete a property from the dictionary.
2425 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2426
2427 // Returns the number of elements in the dictionary filtering out properties
2428 // with the specified attributes.
2429 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2430
2431 // Returns the number of enumerable elements in the dictionary.
2432 int NumberOfEnumElements();
2433
2434 // Copies keys to preallocated fixed array.
2435 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2436 // Fill in details for properties into storage.
2437 void CopyKeysTo(FixedArray* storage);
2438
2439 // Accessors for next enumeration index.
2440 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002441 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002442 }
2443
2444 int NextEnumerationIndex() {
2445 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2446 }
2447
2448 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002449 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002450
2451 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002452 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002453
Ben Murdochb0fe1622011-05-05 13:52:32 +01002454#ifdef OBJECT_PRINT
2455 inline void Print() {
2456 Print(stdout);
2457 }
2458 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002459#endif
2460 // Returns the key (slow).
2461 Object* SlowReverseLookup(Object* value);
2462
2463 // Sets the entry to (key, value) pair.
2464 inline void SetEntry(int entry,
2465 Object* key,
2466 Object* value,
2467 PropertyDetails details);
2468
John Reck59135872010-11-02 12:39:01 -07002469 MUST_USE_RESULT MaybeObject* Add(Key key,
2470 Object* value,
2471 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002472
2473 protected:
2474 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002475 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002476
2477 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002478 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2479 Object* value,
2480 PropertyDetails details,
2481 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002482
2483 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002484 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002485 static const int kMaxNumberKeyIndex =
2486 HashTable<Shape, Key>::kPrefixStartIndex;
2487 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2488};
2489
2490
2491class StringDictionaryShape {
2492 public:
2493 static inline bool IsMatch(String* key, Object* other);
2494 static inline uint32_t Hash(String* key);
2495 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002496 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002497 static const int kPrefixSize = 2;
2498 static const int kEntrySize = 3;
2499 static const bool kIsEnumerable = true;
2500};
2501
2502
2503class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2504 public:
2505 static inline StringDictionary* cast(Object* obj) {
2506 ASSERT(obj->IsDictionary());
2507 return reinterpret_cast<StringDictionary*>(obj);
2508 }
2509
2510 // Copies enumerable keys to preallocated fixed array.
2511 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2512
2513 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002514 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2515 JSObject* obj,
2516 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002517
2518 // Find entry for key otherwise return kNotFound. Optimzed version of
2519 // HashTable::FindEntry.
2520 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002521};
2522
2523
2524class NumberDictionaryShape {
2525 public:
2526 static inline bool IsMatch(uint32_t key, Object* other);
2527 static inline uint32_t Hash(uint32_t key);
2528 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002529 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002530 static const int kPrefixSize = 2;
2531 static const int kEntrySize = 3;
2532 static const bool kIsEnumerable = false;
2533};
2534
2535
2536class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2537 public:
2538 static NumberDictionary* cast(Object* obj) {
2539 ASSERT(obj->IsDictionary());
2540 return reinterpret_cast<NumberDictionary*>(obj);
2541 }
2542
2543 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002544 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2545 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2546 Object* value,
2547 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002548
2549 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002550 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2551 Object* value,
2552 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002553
2554 void UpdateMaxNumberKey(uint32_t key);
2555
2556 // If slow elements are required we will never go back to fast-case
2557 // for the elements kept in this dictionary. We require slow
2558 // elements if an element has been added at an index larger than
2559 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2560 // when defining a getter or setter with a number key.
2561 inline bool requires_slow_elements();
2562 inline void set_requires_slow_elements();
2563
2564 // Get the value of the max number key that has been added to this
2565 // dictionary. max_number_key can only be called if
2566 // requires_slow_elements returns false.
2567 inline uint32_t max_number_key();
2568
2569 // Remove all entries were key is a number and (from <= key && key < to).
2570 void RemoveNumberEntries(uint32_t from, uint32_t to);
2571
2572 // Bit masks.
2573 static const int kRequiresSlowElementsMask = 1;
2574 static const int kRequiresSlowElementsTagSize = 1;
2575 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2576};
2577
2578
Steve Block6ded16b2010-05-10 14:33:55 +01002579// JSFunctionResultCache caches results of some JSFunction invocation.
2580// It is a fixed array with fixed structure:
2581// [0]: factory function
2582// [1]: finger index
2583// [2]: current cache size
2584// [3]: dummy field.
2585// The rest of array are key/value pairs.
2586class JSFunctionResultCache: public FixedArray {
2587 public:
2588 static const int kFactoryIndex = 0;
2589 static const int kFingerIndex = kFactoryIndex + 1;
2590 static const int kCacheSizeIndex = kFingerIndex + 1;
2591 static const int kDummyIndex = kCacheSizeIndex + 1;
2592 static const int kEntriesIndex = kDummyIndex + 1;
2593
2594 static const int kEntrySize = 2; // key + value
2595
Kristian Monsen25f61362010-05-21 11:50:48 +01002596 static const int kFactoryOffset = kHeaderSize;
2597 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2598 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2599
Steve Block6ded16b2010-05-10 14:33:55 +01002600 inline void MakeZeroSize();
2601 inline void Clear();
2602
2603 // Casting
2604 static inline JSFunctionResultCache* cast(Object* obj);
2605
2606#ifdef DEBUG
2607 void JSFunctionResultCacheVerify();
2608#endif
2609};
2610
2611
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002612// The cache for maps used by normalized (dictionary mode) objects.
2613// Such maps do not have property descriptors, so a typical program
2614// needs very limited number of distinct normalized maps.
2615class NormalizedMapCache: public FixedArray {
2616 public:
2617 static const int kEntries = 64;
2618
John Reck59135872010-11-02 12:39:01 -07002619 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2620 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002621
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002622 void Clear();
2623
2624 // Casting
2625 static inline NormalizedMapCache* cast(Object* obj);
2626
2627#ifdef DEBUG
2628 void NormalizedMapCacheVerify();
2629#endif
2630
2631 private:
2632 static int Hash(Map* fast);
2633
2634 static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2635};
2636
2637
Steve Blocka7e24c12009-10-30 11:49:00 +00002638// ByteArray represents fixed sized byte arrays. Used by the outside world,
2639// such as PCRE, and also by the memory allocator and garbage collector to
2640// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002641class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002642 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002643 // [length]: length of the array.
2644 inline int length();
2645 inline void set_length(int value);
2646
Steve Blocka7e24c12009-10-30 11:49:00 +00002647 // Setter and getter.
2648 inline byte get(int index);
2649 inline void set(int index, byte value);
2650
2651 // Treat contents as an int array.
2652 inline int get_int(int index);
2653
2654 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002655 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002656 }
2657 // We use byte arrays for free blocks in the heap. Given a desired size in
2658 // bytes that is a multiple of the word size and big enough to hold a byte
2659 // array, this function returns the number of elements a byte array should
2660 // have.
2661 static int LengthFor(int size_in_bytes) {
2662 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2663 ASSERT(size_in_bytes >= kHeaderSize);
2664 return size_in_bytes - kHeaderSize;
2665 }
2666
2667 // Returns data start address.
2668 inline Address GetDataStartAddress();
2669
2670 // Returns a pointer to the ByteArray object for a given data start address.
2671 static inline ByteArray* FromDataStartAddress(Address address);
2672
2673 // Casting.
2674 static inline ByteArray* cast(Object* obj);
2675
2676 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002677 inline int ByteArraySize() {
2678 return SizeFor(this->length());
2679 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002680#ifdef OBJECT_PRINT
2681 inline void ByteArrayPrint() {
2682 ByteArrayPrint(stdout);
2683 }
2684 void ByteArrayPrint(FILE* out);
2685#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002686#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002687 void ByteArrayVerify();
2688#endif
2689
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002690 // Layout description.
2691 // Length is smi tagged when it is stored.
2692 static const int kLengthOffset = HeapObject::kHeaderSize;
2693 static const int kHeaderSize = kLengthOffset + kPointerSize;
2694
2695 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002696
Leon Clarkee46be812010-01-19 14:06:41 +00002697 // Maximal memory consumption for a single ByteArray.
2698 static const int kMaxSize = 512 * MB;
2699 // Maximal length of a single ByteArray.
2700 static const int kMaxLength = kMaxSize - kHeaderSize;
2701
Steve Blocka7e24c12009-10-30 11:49:00 +00002702 private:
2703 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2704};
2705
2706
2707// A PixelArray represents a fixed-size byte array with special semantics
2708// used for implementing the CanvasPixelArray object. Please see the
2709// specification at:
2710// http://www.whatwg.org/specs/web-apps/current-work/
2711// multipage/the-canvas-element.html#canvaspixelarray
2712// In particular, write access clamps the value written to 0 or 255 if the
2713// value written is outside this range.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002714class PixelArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002715 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002716 // [length]: length of the array.
2717 inline int length();
2718 inline void set_length(int value);
2719
Steve Blocka7e24c12009-10-30 11:49:00 +00002720 // [external_pointer]: The pointer to the external memory area backing this
2721 // pixel array.
2722 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2723
2724 // Setter and getter.
2725 inline uint8_t get(int index);
2726 inline void set(int index, uint8_t value);
2727
2728 // This accessor applies the correct conversion from Smi, HeapNumber and
2729 // undefined and clamps the converted value between 0 and 255.
2730 Object* SetValue(uint32_t index, Object* value);
2731
2732 // Casting.
2733 static inline PixelArray* cast(Object* obj);
2734
Ben Murdochb0fe1622011-05-05 13:52:32 +01002735#ifdef OBJECT_PRINT
2736 inline void PixelArrayPrint() {
2737 PixelArrayPrint(stdout);
2738 }
2739 void PixelArrayPrint(FILE* out);
2740#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002741#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002742 void PixelArrayVerify();
2743#endif // DEBUG
2744
Steve Block3ce2e202009-11-05 08:53:23 +00002745 // Maximal acceptable length for a pixel array.
2746 static const int kMaxLength = 0x3fffffff;
2747
Steve Blocka7e24c12009-10-30 11:49:00 +00002748 // PixelArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002749 static const int kLengthOffset = HeapObject::kHeaderSize;
2750 static const int kExternalPointerOffset =
2751 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002752 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002753 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002754
2755 private:
2756 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2757};
2758
2759
Steve Block3ce2e202009-11-05 08:53:23 +00002760// An ExternalArray represents a fixed-size array of primitive values
2761// which live outside the JavaScript heap. Its subclasses are used to
2762// implement the CanvasArray types being defined in the WebGL
2763// specification. As of this writing the first public draft is not yet
2764// available, but Khronos members can access the draft at:
2765// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2766//
2767// The semantics of these arrays differ from CanvasPixelArray.
2768// Out-of-range values passed to the setter are converted via a C
2769// cast, not clamping. Out-of-range indices cause exceptions to be
2770// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002771class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002772 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002773 // [length]: length of the array.
2774 inline int length();
2775 inline void set_length(int value);
2776
Steve Block3ce2e202009-11-05 08:53:23 +00002777 // [external_pointer]: The pointer to the external memory area backing this
2778 // external array.
2779 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2780
2781 // Casting.
2782 static inline ExternalArray* cast(Object* obj);
2783
2784 // Maximal acceptable length for an external array.
2785 static const int kMaxLength = 0x3fffffff;
2786
2787 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002788 static const int kLengthOffset = HeapObject::kHeaderSize;
2789 static const int kExternalPointerOffset =
2790 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002791 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002792 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002793
2794 private:
2795 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2796};
2797
2798
2799class ExternalByteArray: public ExternalArray {
2800 public:
2801 // Setter and getter.
2802 inline int8_t get(int index);
2803 inline void set(int index, int8_t value);
2804
2805 // This accessor applies the correct conversion from Smi, HeapNumber
2806 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002807 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002808
2809 // Casting.
2810 static inline ExternalByteArray* cast(Object* obj);
2811
Ben Murdochb0fe1622011-05-05 13:52:32 +01002812#ifdef OBJECT_PRINT
2813 inline void ExternalByteArrayPrint() {
2814 ExternalByteArrayPrint(stdout);
2815 }
2816 void ExternalByteArrayPrint(FILE* out);
2817#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002818#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002819 void ExternalByteArrayVerify();
2820#endif // DEBUG
2821
2822 private:
2823 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2824};
2825
2826
2827class ExternalUnsignedByteArray: public ExternalArray {
2828 public:
2829 // Setter and getter.
2830 inline uint8_t get(int index);
2831 inline void set(int index, uint8_t value);
2832
2833 // This accessor applies the correct conversion from Smi, HeapNumber
2834 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002835 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002836
2837 // Casting.
2838 static inline ExternalUnsignedByteArray* cast(Object* obj);
2839
Ben Murdochb0fe1622011-05-05 13:52:32 +01002840#ifdef OBJECT_PRINT
2841 inline void ExternalUnsignedByteArrayPrint() {
2842 ExternalUnsignedByteArrayPrint(stdout);
2843 }
2844 void ExternalUnsignedByteArrayPrint(FILE* out);
2845#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002846#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002847 void ExternalUnsignedByteArrayVerify();
2848#endif // DEBUG
2849
2850 private:
2851 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2852};
2853
2854
2855class ExternalShortArray: public ExternalArray {
2856 public:
2857 // Setter and getter.
2858 inline int16_t get(int index);
2859 inline void set(int index, int16_t value);
2860
2861 // This accessor applies the correct conversion from Smi, HeapNumber
2862 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002863 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002864
2865 // Casting.
2866 static inline ExternalShortArray* cast(Object* obj);
2867
Ben Murdochb0fe1622011-05-05 13:52:32 +01002868#ifdef OBJECT_PRINT
2869 inline void ExternalShortArrayPrint() {
2870 ExternalShortArrayPrint(stdout);
2871 }
2872 void ExternalShortArrayPrint(FILE* out);
2873#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002874#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002875 void ExternalShortArrayVerify();
2876#endif // DEBUG
2877
2878 private:
2879 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2880};
2881
2882
2883class ExternalUnsignedShortArray: public ExternalArray {
2884 public:
2885 // Setter and getter.
2886 inline uint16_t get(int index);
2887 inline void set(int index, uint16_t value);
2888
2889 // This accessor applies the correct conversion from Smi, HeapNumber
2890 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002891 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002892
2893 // Casting.
2894 static inline ExternalUnsignedShortArray* cast(Object* obj);
2895
Ben Murdochb0fe1622011-05-05 13:52:32 +01002896#ifdef OBJECT_PRINT
2897 inline void ExternalUnsignedShortArrayPrint() {
2898 ExternalUnsignedShortArrayPrint(stdout);
2899 }
2900 void ExternalUnsignedShortArrayPrint(FILE* out);
2901#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002902#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002903 void ExternalUnsignedShortArrayVerify();
2904#endif // DEBUG
2905
2906 private:
2907 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2908};
2909
2910
2911class ExternalIntArray: public ExternalArray {
2912 public:
2913 // Setter and getter.
2914 inline int32_t get(int index);
2915 inline void set(int index, int32_t value);
2916
2917 // This accessor applies the correct conversion from Smi, HeapNumber
2918 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002919 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002920
2921 // Casting.
2922 static inline ExternalIntArray* cast(Object* obj);
2923
Ben Murdochb0fe1622011-05-05 13:52:32 +01002924#ifdef OBJECT_PRINT
2925 inline void ExternalIntArrayPrint() {
2926 ExternalIntArrayPrint(stdout);
2927 }
2928 void ExternalIntArrayPrint(FILE* out);
2929#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002930#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002931 void ExternalIntArrayVerify();
2932#endif // DEBUG
2933
2934 private:
2935 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
2936};
2937
2938
2939class ExternalUnsignedIntArray: public ExternalArray {
2940 public:
2941 // Setter and getter.
2942 inline uint32_t get(int index);
2943 inline void set(int index, uint32_t value);
2944
2945 // This accessor applies the correct conversion from Smi, HeapNumber
2946 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002947 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002948
2949 // Casting.
2950 static inline ExternalUnsignedIntArray* cast(Object* obj);
2951
Ben Murdochb0fe1622011-05-05 13:52:32 +01002952#ifdef OBJECT_PRINT
2953 inline void ExternalUnsignedIntArrayPrint() {
2954 ExternalUnsignedIntArrayPrint(stdout);
2955 }
2956 void ExternalUnsignedIntArrayPrint(FILE* out);
2957#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002958#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002959 void ExternalUnsignedIntArrayVerify();
2960#endif // DEBUG
2961
2962 private:
2963 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
2964};
2965
2966
2967class ExternalFloatArray: public ExternalArray {
2968 public:
2969 // Setter and getter.
2970 inline float get(int index);
2971 inline void set(int index, float value);
2972
2973 // This accessor applies the correct conversion from Smi, HeapNumber
2974 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002975 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002976
2977 // Casting.
2978 static inline ExternalFloatArray* cast(Object* obj);
2979
Ben Murdochb0fe1622011-05-05 13:52:32 +01002980#ifdef OBJECT_PRINT
2981 inline void ExternalFloatArrayPrint() {
2982 ExternalFloatArrayPrint(stdout);
2983 }
2984 void ExternalFloatArrayPrint(FILE* out);
2985#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002986#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002987 void ExternalFloatArrayVerify();
2988#endif // DEBUG
2989
2990 private:
2991 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
2992};
2993
2994
Ben Murdochb0fe1622011-05-05 13:52:32 +01002995// DeoptimizationInputData is a fixed array used to hold the deoptimization
2996// data for code generated by the Hydrogen/Lithium compiler. It also
2997// contains information about functions that were inlined. If N different
2998// functions were inlined then first N elements of the literal array will
2999// contain these functions.
3000//
3001// It can be empty.
3002class DeoptimizationInputData: public FixedArray {
3003 public:
3004 // Layout description. Indices in the array.
3005 static const int kTranslationByteArrayIndex = 0;
3006 static const int kInlinedFunctionCountIndex = 1;
3007 static const int kLiteralArrayIndex = 2;
3008 static const int kOsrAstIdIndex = 3;
3009 static const int kOsrPcOffsetIndex = 4;
3010 static const int kFirstDeoptEntryIndex = 5;
3011
3012 // Offsets of deopt entry elements relative to the start of the entry.
3013 static const int kAstIdOffset = 0;
3014 static const int kTranslationIndexOffset = 1;
3015 static const int kArgumentsStackHeightOffset = 2;
3016 static const int kDeoptEntrySize = 3;
3017
3018 // Simple element accessors.
3019#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3020 type* name() { \
3021 return type::cast(get(k##name##Index)); \
3022 } \
3023 void Set##name(type* value) { \
3024 set(k##name##Index, value); \
3025 }
3026
3027 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3028 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3029 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3030 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3031 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3032
3033 // Unchecked accessor to be used during GC.
3034 FixedArray* UncheckedLiteralArray() {
3035 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3036 }
3037
3038#undef DEFINE_ELEMENT_ACCESSORS
3039
3040 // Accessors for elements of the ith deoptimization entry.
3041#define DEFINE_ENTRY_ACCESSORS(name, type) \
3042 type* name(int i) { \
3043 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3044 } \
3045 void Set##name(int i, type* value) { \
3046 set(IndexForEntry(i) + k##name##Offset, value); \
3047 }
3048
3049 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3050 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3051 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3052
3053#undef DEFINE_ENTRY_ACCESSORS
3054
3055 int DeoptCount() {
3056 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3057 }
3058
3059 // Allocates a DeoptimizationInputData.
3060 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3061 PretenureFlag pretenure);
3062
3063 // Casting.
3064 static inline DeoptimizationInputData* cast(Object* obj);
3065
3066#ifdef OBJECT_PRINT
3067 void DeoptimizationInputDataPrint(FILE* out);
3068#endif
3069
3070 private:
3071 static int IndexForEntry(int i) {
3072 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3073 }
3074
3075 static int LengthFor(int entry_count) {
3076 return IndexForEntry(entry_count);
3077 }
3078};
3079
3080
3081// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3082// data for code generated by the full compiler.
3083// The format of the these objects is
3084// [i * 2]: Ast ID for ith deoptimization.
3085// [i * 2 + 1]: PC and state of ith deoptimization
3086class DeoptimizationOutputData: public FixedArray {
3087 public:
3088 int DeoptPoints() { return length() / 2; }
3089 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3090 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3091 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3092 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3093
3094 static int LengthOfFixedArray(int deopt_points) {
3095 return deopt_points * 2;
3096 }
3097
3098 // Allocates a DeoptimizationOutputData.
3099 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3100 PretenureFlag pretenure);
3101
3102 // Casting.
3103 static inline DeoptimizationOutputData* cast(Object* obj);
3104
3105#ifdef OBJECT_PRINT
3106 void DeoptimizationOutputDataPrint(FILE* out);
3107#endif
3108};
3109
3110
Steve Blocka7e24c12009-10-30 11:49:00 +00003111// Code describes objects with on-the-fly generated machine code.
3112class Code: public HeapObject {
3113 public:
3114 // Opaque data type for encapsulating code flags like kind, inline
3115 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003116 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3117 // enumeration type has correct value range (see Issue 830 for more details).
3118 enum Flags {
3119 FLAGS_MIN_VALUE = kMinInt,
3120 FLAGS_MAX_VALUE = kMaxInt
3121 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003122
3123 enum Kind {
3124 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003125 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003126 STUB,
3127 BUILTIN,
3128 LOAD_IC,
3129 KEYED_LOAD_IC,
3130 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003131 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003132 STORE_IC,
3133 KEYED_STORE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003134 BINARY_OP_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003135 TYPE_RECORDING_BINARY_OP_IC,
3136 COMPARE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003137 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003138 // Flags.
3139
3140 // Pseudo-kinds.
3141 REGEXP = BUILTIN,
3142 FIRST_IC_KIND = LOAD_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003143 LAST_IC_KIND = COMPARE_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003144 };
3145
3146 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003147 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003148 };
3149
3150#ifdef ENABLE_DISASSEMBLER
3151 // Printing
3152 static const char* Kind2String(Kind kind);
3153 static const char* ICState2String(InlineCacheState state);
3154 static const char* PropertyType2String(PropertyType type);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003155 inline void Disassemble(const char* name) {
3156 Disassemble(name, stdout);
3157 }
3158 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003159#endif // ENABLE_DISASSEMBLER
3160
3161 // [instruction_size]: Size of the native instructions
3162 inline int instruction_size();
3163 inline void set_instruction_size(int value);
3164
Leon Clarkeac952652010-07-15 11:15:24 +01003165 // [relocation_info]: Code relocation information
3166 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003167 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003168
Ben Murdochb0fe1622011-05-05 13:52:32 +01003169 // [deoptimization_data]: Array containing data for deopt.
3170 DECL_ACCESSORS(deoptimization_data, FixedArray)
3171
3172 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003173 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003174 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003175
Steve Blocka7e24c12009-10-30 11:49:00 +00003176 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003177
Steve Blocka7e24c12009-10-30 11:49:00 +00003178 // [flags]: Various code flags.
3179 inline Flags flags();
3180 inline void set_flags(Flags flags);
3181
3182 // [flags]: Access to specific code flags.
3183 inline Kind kind();
3184 inline InlineCacheState ic_state(); // Only valid for IC stubs.
3185 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
3186 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3187 inline int arguments_count(); // Only valid for call IC stubs.
3188
3189 // Testers for IC stub kinds.
3190 inline bool is_inline_cache_stub();
3191 inline bool is_load_stub() { return kind() == LOAD_IC; }
3192 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3193 inline bool is_store_stub() { return kind() == STORE_IC; }
3194 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3195 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003196 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003197 inline bool is_binary_op_stub() { return kind() == BINARY_OP_IC; }
3198 inline bool is_type_recording_binary_op_stub() {
3199 return kind() == TYPE_RECORDING_BINARY_OP_IC;
3200 }
3201 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00003202
Steve Block6ded16b2010-05-10 14:33:55 +01003203 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003204 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003205 inline void set_major_key(int value);
3206
3207 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3208 inline bool optimizable();
3209 inline void set_optimizable(bool value);
3210
3211 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3212 // deoptimization support.
3213 inline bool has_deoptimization_support();
3214 inline void set_has_deoptimization_support(bool value);
3215
3216 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3217 // how long the function has been marked for OSR and therefore which
3218 // level of loop nesting we are willing to do on-stack replacement
3219 // for.
3220 inline void set_allow_osr_at_loop_nesting_level(int level);
3221 inline int allow_osr_at_loop_nesting_level();
3222
3223 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3224 // reserved in the code prologue.
3225 inline unsigned stack_slots();
3226 inline void set_stack_slots(unsigned slots);
3227
3228 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3229 // the instruction stream where the safepoint table starts.
3230 inline unsigned safepoint_table_start();
3231 inline void set_safepoint_table_start(unsigned offset);
3232
3233 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3234 // instruction stream where the stack check table starts.
3235 inline unsigned stack_check_table_start();
3236 inline void set_stack_check_table_start(unsigned offset);
3237
3238 // [check type]: For kind CALL_IC, tells how to check if the
3239 // receiver is valid for the given call.
3240 inline CheckType check_type();
3241 inline void set_check_type(CheckType value);
3242
3243 // [binary op type]: For all BINARY_OP_IC.
3244 inline byte binary_op_type();
3245 inline void set_binary_op_type(byte value);
3246
3247 // [type-recording binary op type]: For all TYPE_RECORDING_BINARY_OP_IC.
3248 inline byte type_recording_binary_op_type();
3249 inline void set_type_recording_binary_op_type(byte value);
3250 inline byte type_recording_binary_op_result_type();
3251 inline void set_type_recording_binary_op_result_type(byte value);
3252
3253 // [compare state]: For kind compare IC stubs, tells what state the
3254 // stub is in.
3255 inline byte compare_state();
3256 inline void set_compare_state(byte value);
3257
3258 // Get the safepoint entry for the given pc. Returns NULL for
3259 // non-safepoint pcs.
3260 uint8_t* GetSafepointEntry(Address pc);
3261
3262 // Mark this code object as not having a stack check table. Assumes kind
3263 // is FUNCTION.
3264 void SetNoStackCheckTable();
3265
3266 // Find the first map in an IC stub.
3267 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003268
3269 // Flags operations.
3270 static inline Flags ComputeFlags(Kind kind,
3271 InLoopFlag in_loop = NOT_IN_LOOP,
3272 InlineCacheState ic_state = UNINITIALIZED,
3273 PropertyType type = NORMAL,
Steve Block8defd9f2010-07-08 12:39:36 +01003274 int argc = -1,
3275 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003276
3277 static inline Flags ComputeMonomorphicFlags(
3278 Kind kind,
3279 PropertyType type,
Steve Block8defd9f2010-07-08 12:39:36 +01003280 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003281 InLoopFlag in_loop = NOT_IN_LOOP,
3282 int argc = -1);
3283
3284 static inline Kind ExtractKindFromFlags(Flags flags);
3285 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
3286 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
3287 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3288 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003289 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003290 static inline Flags RemoveTypeFromFlags(Flags flags);
3291
3292 // Convert a target address into a code object.
3293 static inline Code* GetCodeFromTargetAddress(Address address);
3294
Steve Block791712a2010-08-27 10:21:07 +01003295 // Convert an entry address into an object.
3296 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3297
Steve Blocka7e24c12009-10-30 11:49:00 +00003298 // Returns the address of the first instruction.
3299 inline byte* instruction_start();
3300
Leon Clarkeac952652010-07-15 11:15:24 +01003301 // Returns the address right after the last instruction.
3302 inline byte* instruction_end();
3303
Steve Blocka7e24c12009-10-30 11:49:00 +00003304 // Returns the size of the instructions, padding, and relocation information.
3305 inline int body_size();
3306
3307 // Returns the address of the first relocation info (read backwards!).
3308 inline byte* relocation_start();
3309
3310 // Code entry point.
3311 inline byte* entry();
3312
3313 // Returns true if pc is inside this object's instructions.
3314 inline bool contains(byte* pc);
3315
Steve Blocka7e24c12009-10-30 11:49:00 +00003316 // Relocate the code by delta bytes. Called to signal that this code
3317 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003318 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003319
3320 // Migrate code described by desc.
3321 void CopyFrom(const CodeDesc& desc);
3322
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003323 // Returns the object size for a given body (used for allocation).
3324 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003325 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003326 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003327 }
3328
3329 // Calculate the size of the code object to report for log events. This takes
3330 // the layout of the code object into account.
3331 int ExecutableSize() {
3332 // Check that the assumptions about the layout of the code object holds.
3333 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3334 Code::kHeaderSize);
3335 return instruction_size() + Code::kHeaderSize;
3336 }
3337
3338 // Locating source position.
3339 int SourcePosition(Address pc);
3340 int SourceStatementPosition(Address pc);
3341
3342 // Casting.
3343 static inline Code* cast(Object* obj);
3344
3345 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003346 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003347 inline void CodeIterateBody(ObjectVisitor* v);
3348
3349 template<typename StaticVisitor>
3350 inline void CodeIterateBody();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003351#ifdef OBJECT_PRINT
3352 inline void CodePrint() {
3353 CodePrint(stdout);
3354 }
3355 void CodePrint(FILE* out);
3356#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003357#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003358 void CodeVerify();
3359#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003360
3361 // Max loop nesting marker used to postpose OSR. We don't take loop
3362 // nesting that is deeper than 5 levels into account.
3363 static const int kMaxLoopNestingMarker = 6;
3364
Steve Blocka7e24c12009-10-30 11:49:00 +00003365 // Layout description.
3366 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003367 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003368 static const int kDeoptimizationDataOffset =
3369 kRelocationInfoOffset + kPointerSize;
3370 static const int kFlagsOffset = kDeoptimizationDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003371 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003372
3373 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3374
3375 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3376 kKindSpecificFlagsSize;
3377
Steve Blocka7e24c12009-10-30 11:49:00 +00003378 // Add padding to align the instruction start following right after
3379 // the Code object header.
3380 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003381 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003382
3383 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003384 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3385 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3386 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3387 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3388
3389 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3390 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
3391 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3392
3393 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3394 static const int kAllowOSRAtLoopNestingLevelOffset =
3395 kHasDeoptimizationSupportOffset + 1;
3396
3397 static const int kSafepointTableStartOffset = kStackSlotsOffset + kIntSize;
3398 static const int kStackCheckTableStartOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003399
3400 // Flags layout.
3401 static const int kFlagsICStateShift = 0;
3402 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003403 static const int kFlagsTypeShift = 4;
3404 static const int kFlagsKindShift = 7;
Steve Block8defd9f2010-07-08 12:39:36 +01003405 static const int kFlagsICHolderShift = 11;
3406 static const int kFlagsArgumentsCountShift = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00003407
Steve Block6ded16b2010-05-10 14:33:55 +01003408 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3409 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003410 static const int kFlagsTypeMask = 0x00000070; // 00001110000
3411 static const int kFlagsKindMask = 0x00000780; // 11110000000
Steve Block8defd9f2010-07-08 12:39:36 +01003412 static const int kFlagsCacheInPrototypeMapMask = 0x00000800;
3413 static const int kFlagsArgumentsCountMask = 0xFFFFF000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003414
3415 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003416 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003417
3418 private:
3419 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3420};
3421
3422
3423// All heap objects have a Map that describes their structure.
3424// A Map contains information about:
3425// - Size information about the object
3426// - How to iterate over an object (for garbage collection)
3427class Map: public HeapObject {
3428 public:
3429 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003430 // Size in bytes or kVariableSizeSentinel if instances do not have
3431 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003432 inline int instance_size();
3433 inline void set_instance_size(int value);
3434
3435 // Count of properties allocated in the object.
3436 inline int inobject_properties();
3437 inline void set_inobject_properties(int value);
3438
3439 // Count of property fields pre-allocated in the object when first allocated.
3440 inline int pre_allocated_property_fields();
3441 inline void set_pre_allocated_property_fields(int value);
3442
3443 // Instance type.
3444 inline InstanceType instance_type();
3445 inline void set_instance_type(InstanceType value);
3446
3447 // Tells how many unused property fields are available in the
3448 // instance (only used for JSObject in fast mode).
3449 inline int unused_property_fields();
3450 inline void set_unused_property_fields(int value);
3451
3452 // Bit field.
3453 inline byte bit_field();
3454 inline void set_bit_field(byte value);
3455
3456 // Bit field 2.
3457 inline byte bit_field2();
3458 inline void set_bit_field2(byte value);
3459
3460 // Tells whether the object in the prototype property will be used
3461 // for instances created from this function. If the prototype
3462 // property is set to a value that is not a JSObject, the prototype
3463 // property will not be used to create instances of the function.
3464 // See ECMA-262, 13.2.2.
3465 inline void set_non_instance_prototype(bool value);
3466 inline bool has_non_instance_prototype();
3467
Steve Block6ded16b2010-05-10 14:33:55 +01003468 // Tells whether function has special prototype property. If not, prototype
3469 // property will not be created when accessed (will return undefined),
3470 // and construction from this function will not be allowed.
3471 inline void set_function_with_prototype(bool value);
3472 inline bool function_with_prototype();
3473
Steve Blocka7e24c12009-10-30 11:49:00 +00003474 // Tells whether the instance with this map should be ignored by the
3475 // __proto__ accessor.
3476 inline void set_is_hidden_prototype() {
3477 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3478 }
3479
3480 inline bool is_hidden_prototype() {
3481 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3482 }
3483
3484 // Records and queries whether the instance has a named interceptor.
3485 inline void set_has_named_interceptor() {
3486 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3487 }
3488
3489 inline bool has_named_interceptor() {
3490 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3491 }
3492
3493 // Records and queries whether the instance has an indexed interceptor.
3494 inline void set_has_indexed_interceptor() {
3495 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3496 }
3497
3498 inline bool has_indexed_interceptor() {
3499 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3500 }
3501
3502 // Tells whether the instance is undetectable.
3503 // An undetectable object is a special class of JSObject: 'typeof' operator
3504 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3505 // a normal JS object. It is useful for implementing undetectable
3506 // document.all in Firefox & Safari.
3507 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3508 inline void set_is_undetectable() {
3509 set_bit_field(bit_field() | (1 << kIsUndetectable));
3510 }
3511
3512 inline bool is_undetectable() {
3513 return ((1 << kIsUndetectable) & bit_field()) != 0;
3514 }
3515
Steve Blocka7e24c12009-10-30 11:49:00 +00003516 // Tells whether the instance has a call-as-function handler.
3517 inline void set_has_instance_call_handler() {
3518 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3519 }
3520
3521 inline bool has_instance_call_handler() {
3522 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3523 }
3524
Steve Block8defd9f2010-07-08 12:39:36 +01003525 inline void set_is_extensible(bool value);
3526 inline bool is_extensible();
3527
3528 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003529 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3530 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003531 if (value) {
3532 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3533 } else {
3534 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3535 }
Leon Clarkee46be812010-01-19 14:06:41 +00003536 }
3537
Iain Merrick75681382010-08-19 15:07:18 +01003538 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003539 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003540 }
3541
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003542 // Tells whether the map is attached to SharedFunctionInfo
3543 // (for inobject slack tracking).
3544 inline void set_attached_to_shared_function_info(bool value);
3545
3546 inline bool attached_to_shared_function_info();
3547
3548 // Tells whether the map is shared between objects that may have different
3549 // behavior. If true, the map should never be modified, instead a clone
3550 // should be created and modified.
3551 inline void set_is_shared(bool value);
3552
3553 inline bool is_shared();
3554
Steve Blocka7e24c12009-10-30 11:49:00 +00003555 // Tells whether the instance needs security checks when accessing its
3556 // properties.
3557 inline void set_is_access_check_needed(bool access_check_needed);
3558 inline bool is_access_check_needed();
3559
3560 // [prototype]: implicit prototype object.
3561 DECL_ACCESSORS(prototype, Object)
3562
3563 // [constructor]: points back to the function responsible for this map.
3564 DECL_ACCESSORS(constructor, Object)
3565
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003566 inline JSFunction* unchecked_constructor();
3567
Steve Blocka7e24c12009-10-30 11:49:00 +00003568 // [instance descriptors]: describes the object.
3569 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3570
3571 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003572 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003573
Ben Murdochb0fe1622011-05-05 13:52:32 +01003574 // Lookup in the map's instance descriptors and fill out the result
3575 // with the given holder if the name is found. The holder may be
3576 // NULL when this function is used from the compiler.
3577 void LookupInDescriptors(JSObject* holder,
3578 String* name,
3579 LookupResult* result);
3580
John Reck59135872010-11-02 12:39:01 -07003581 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003582
John Reck59135872010-11-02 12:39:01 -07003583 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3584 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00003585
3586 // Returns a copy of the map, with all transitions dropped from the
3587 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07003588 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00003589
Steve Block8defd9f2010-07-08 12:39:36 +01003590 // Returns this map if it has the fast elements bit set, otherwise
3591 // returns a copy of the map, with all transitions dropped from the
3592 // descriptors and the fast elements bit set.
John Reck59135872010-11-02 12:39:01 -07003593 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003594
3595 // Returns this map if it has the fast elements bit cleared,
3596 // otherwise returns a copy of the map, with all transitions dropped
3597 // from the descriptors and the fast elements bit cleared.
John Reck59135872010-11-02 12:39:01 -07003598 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003599
Steve Blocka7e24c12009-10-30 11:49:00 +00003600 // Returns the property index for name (only valid for FAST MODE).
3601 int PropertyIndexFor(String* name);
3602
3603 // Returns the next free property index (only valid for FAST MODE).
3604 int NextFreePropertyIndex();
3605
3606 // Returns the number of properties described in instance_descriptors.
3607 int NumberOfDescribedProperties();
3608
3609 // Casting.
3610 static inline Map* cast(Object* obj);
3611
3612 // Locate an accessor in the instance descriptor.
3613 AccessorDescriptor* FindAccessor(String* name);
3614
3615 // Code cache operations.
3616
3617 // Clears the code cache.
3618 inline void ClearCodeCache();
3619
3620 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07003621 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003622
3623 // Returns the found code or undefined if absent.
3624 Object* FindInCodeCache(String* name, Code::Flags flags);
3625
3626 // Returns the non-negative index of the code object if it is in the
3627 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003628 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003629
3630 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003631 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003632
3633 // For every transition in this map, makes the transition's
3634 // target's prototype pointer point back to this map.
3635 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3636 void CreateBackPointers();
3637
3638 // Set all map transitions from this map to dead maps to null.
3639 // Also, restore the original prototype on the targets of these
3640 // transitions, so that we do not process this map again while
3641 // following back pointers.
3642 void ClearNonLiveTransitions(Object* real_prototype);
3643
3644 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003645#ifdef OBJECT_PRINT
3646 inline void MapPrint() {
3647 MapPrint(stdout);
3648 }
3649 void MapPrint(FILE* out);
3650#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003651#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003652 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003653 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00003654#endif
3655
Iain Merrick75681382010-08-19 15:07:18 +01003656 inline int visitor_id();
3657 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003658
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003659 typedef void (*TraverseCallback)(Map* map, void* data);
3660
3661 void TraverseTransitionTree(TraverseCallback callback, void* data);
3662
Steve Blocka7e24c12009-10-30 11:49:00 +00003663 static const int kMaxPreAllocatedPropertyFields = 255;
3664
3665 // Layout description.
3666 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3667 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3668 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3669 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3670 static const int kInstanceDescriptorsOffset =
3671 kConstructorOffset + kPointerSize;
3672 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003673 static const int kPadStart = kCodeCacheOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003674 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3675
3676 // Layout of pointer fields. Heap iteration code relies on them
3677 // being continiously allocated.
3678 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3679 static const int kPointerFieldsEndOffset =
3680 Map::kCodeCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003681
3682 // Byte offsets within kInstanceSizesOffset.
3683 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3684 static const int kInObjectPropertiesByte = 1;
3685 static const int kInObjectPropertiesOffset =
3686 kInstanceSizesOffset + kInObjectPropertiesByte;
3687 static const int kPreAllocatedPropertyFieldsByte = 2;
3688 static const int kPreAllocatedPropertyFieldsOffset =
3689 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003690 static const int kVisitorIdByte = 3;
3691 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00003692
3693 // Byte offsets within kInstanceAttributesOffset attributes.
3694 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3695 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3696 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3697 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3698
3699 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3700
3701 // Bit positions for bit field.
3702 static const int kUnused = 0; // To be used for marking recently used maps.
3703 static const int kHasNonInstancePrototype = 1;
3704 static const int kIsHiddenPrototype = 2;
3705 static const int kHasNamedInterceptor = 3;
3706 static const int kHasIndexedInterceptor = 4;
3707 static const int kIsUndetectable = 5;
3708 static const int kHasInstanceCallHandler = 6;
3709 static const int kIsAccessCheckNeeded = 7;
3710
3711 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003712 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003713 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003714 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003715 static const int kStringWrapperSafeForDefaultValueOf = 3;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003716 static const int kAttachedToSharedFunctionInfo = 4;
3717 static const int kIsShared = 5;
Steve Block6ded16b2010-05-10 14:33:55 +01003718
3719 // Layout of the default cache. It holds alternating name and code objects.
3720 static const int kCodeCacheEntrySize = 2;
3721 static const int kCodeCacheEntryNameOffset = 0;
3722 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003723
Iain Merrick75681382010-08-19 15:07:18 +01003724 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3725 kPointerFieldsEndOffset,
3726 kSize> BodyDescriptor;
3727
Steve Blocka7e24c12009-10-30 11:49:00 +00003728 private:
3729 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3730};
3731
3732
3733// An abstract superclass, a marker class really, for simple structure classes.
3734// It doesn't carry much functionality but allows struct classes to me
3735// identified in the type system.
3736class Struct: public HeapObject {
3737 public:
3738 inline void InitializeBody(int object_size);
3739 static inline Struct* cast(Object* that);
3740};
3741
3742
3743// Script describes a script which has been added to the VM.
3744class Script: public Struct {
3745 public:
3746 // Script types.
3747 enum Type {
3748 TYPE_NATIVE = 0,
3749 TYPE_EXTENSION = 1,
3750 TYPE_NORMAL = 2
3751 };
3752
3753 // Script compilation types.
3754 enum CompilationType {
3755 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003756 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003757 };
3758
3759 // [source]: the script source.
3760 DECL_ACCESSORS(source, Object)
3761
3762 // [name]: the script name.
3763 DECL_ACCESSORS(name, Object)
3764
3765 // [id]: the script id.
3766 DECL_ACCESSORS(id, Object)
3767
3768 // [line_offset]: script line offset in resource from where it was extracted.
3769 DECL_ACCESSORS(line_offset, Smi)
3770
3771 // [column_offset]: script column offset in resource from where it was
3772 // extracted.
3773 DECL_ACCESSORS(column_offset, Smi)
3774
3775 // [data]: additional data associated with this script.
3776 DECL_ACCESSORS(data, Object)
3777
3778 // [context_data]: context data for the context this script was compiled in.
3779 DECL_ACCESSORS(context_data, Object)
3780
3781 // [wrapper]: the wrapper cache.
3782 DECL_ACCESSORS(wrapper, Proxy)
3783
3784 // [type]: the script type.
3785 DECL_ACCESSORS(type, Smi)
3786
3787 // [compilation]: how the the script was compiled.
3788 DECL_ACCESSORS(compilation_type, Smi)
3789
Steve Blockd0582a62009-12-15 09:54:21 +00003790 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003791 DECL_ACCESSORS(line_ends, Object)
3792
Steve Blockd0582a62009-12-15 09:54:21 +00003793 // [eval_from_shared]: for eval scripts the shared funcion info for the
3794 // function from which eval was called.
3795 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003796
3797 // [eval_from_instructions_offset]: the instruction offset in the code for the
3798 // function from which eval was called where eval was called.
3799 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3800
3801 static inline Script* cast(Object* obj);
3802
Steve Block3ce2e202009-11-05 08:53:23 +00003803 // If script source is an external string, check that the underlying
3804 // resource is accessible. Otherwise, always return true.
3805 inline bool HasValidSource();
3806
Ben Murdochb0fe1622011-05-05 13:52:32 +01003807#ifdef OBJECT_PRINT
3808 inline void ScriptPrint() {
3809 ScriptPrint(stdout);
3810 }
3811 void ScriptPrint(FILE* out);
3812#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003813#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003814 void ScriptVerify();
3815#endif
3816
3817 static const int kSourceOffset = HeapObject::kHeaderSize;
3818 static const int kNameOffset = kSourceOffset + kPointerSize;
3819 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3820 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3821 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3822 static const int kContextOffset = kDataOffset + kPointerSize;
3823 static const int kWrapperOffset = kContextOffset + kPointerSize;
3824 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3825 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3826 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3827 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003828 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003829 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003830 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003831 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3832
3833 private:
3834 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3835};
3836
3837
Ben Murdochb0fe1622011-05-05 13:52:32 +01003838// List of builtin functions we want to identify to improve code
3839// generation.
3840//
3841// Each entry has a name of a global object property holding an object
3842// optionally followed by ".prototype", a name of a builtin function
3843// on the object (the one the id is set for), and a label.
3844//
3845// Installation of ids for the selected builtin functions is handled
3846// by the bootstrapper.
3847//
3848// NOTE: Order is important: math functions should be at the end of
3849// the list and MathFloor should be the first math function.
3850#define FUNCTIONS_WITH_ID_LIST(V) \
3851 V(Array.prototype, push, ArrayPush) \
3852 V(Array.prototype, pop, ArrayPop) \
3853 V(String.prototype, charCodeAt, StringCharCodeAt) \
3854 V(String.prototype, charAt, StringCharAt) \
3855 V(String, fromCharCode, StringFromCharCode) \
3856 V(Math, floor, MathFloor) \
3857 V(Math, round, MathRound) \
3858 V(Math, ceil, MathCeil) \
3859 V(Math, abs, MathAbs) \
3860 V(Math, log, MathLog) \
3861 V(Math, sin, MathSin) \
3862 V(Math, cos, MathCos) \
3863 V(Math, tan, MathTan) \
3864 V(Math, asin, MathASin) \
3865 V(Math, acos, MathACos) \
3866 V(Math, atan, MathATan) \
3867 V(Math, exp, MathExp) \
3868 V(Math, sqrt, MathSqrt) \
3869 V(Math, pow, MathPow)
3870
3871
3872enum BuiltinFunctionId {
3873#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
3874 k##name,
3875 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
3876#undef DECLARE_FUNCTION_ID
3877 // Fake id for a special case of Math.pow. Note, it continues the
3878 // list of math functions.
3879 kMathPowHalf,
3880 kFirstMathFunctionId = kMathFloor
3881};
3882
3883
Steve Blocka7e24c12009-10-30 11:49:00 +00003884// SharedFunctionInfo describes the JSFunction information that can be
3885// shared by multiple instances of the function.
3886class SharedFunctionInfo: public HeapObject {
3887 public:
3888 // [name]: Function name.
3889 DECL_ACCESSORS(name, Object)
3890
3891 // [code]: Function code.
3892 DECL_ACCESSORS(code, Code)
3893
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003894 // [scope_info]: Scope info.
3895 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
3896
Steve Blocka7e24c12009-10-30 11:49:00 +00003897 // [construct stub]: Code stub for constructing instances of this function.
3898 DECL_ACCESSORS(construct_stub, Code)
3899
Iain Merrick75681382010-08-19 15:07:18 +01003900 inline Code* unchecked_code();
3901
Steve Blocka7e24c12009-10-30 11:49:00 +00003902 // Returns if this function has been compiled to native code yet.
3903 inline bool is_compiled();
3904
3905 // [length]: The function length - usually the number of declared parameters.
3906 // Use up to 2^30 parameters.
3907 inline int length();
3908 inline void set_length(int value);
3909
3910 // [formal parameter count]: The declared number of parameters.
3911 inline int formal_parameter_count();
3912 inline void set_formal_parameter_count(int value);
3913
3914 // Set the formal parameter count so the function code will be
3915 // called without using argument adaptor frames.
3916 inline void DontAdaptArguments();
3917
3918 // [expected_nof_properties]: Expected number of properties for the function.
3919 inline int expected_nof_properties();
3920 inline void set_expected_nof_properties(int value);
3921
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003922 // Inobject slack tracking is the way to reclaim unused inobject space.
3923 //
3924 // The instance size is initially determined by adding some slack to
3925 // expected_nof_properties (to allow for a few extra properties added
3926 // after the constructor). There is no guarantee that the extra space
3927 // will not be wasted.
3928 //
3929 // Here is the algorithm to reclaim the unused inobject space:
3930 // - Detect the first constructor call for this SharedFunctionInfo.
3931 // When it happens enter the "in progress" state: remember the
3932 // constructor's initial_map and install a special construct stub that
3933 // counts constructor calls.
3934 // - While the tracking is in progress create objects filled with
3935 // one_pointer_filler_map instead of undefined_value. This way they can be
3936 // resized quickly and safely.
3937 // - Once enough (kGenerousAllocationCount) objects have been created
3938 // compute the 'slack' (traverse the map transition tree starting from the
3939 // initial_map and find the lowest value of unused_property_fields).
3940 // - Traverse the transition tree again and decrease the instance size
3941 // of every map. Existing objects will resize automatically (they are
3942 // filled with one_pointer_filler_map). All further allocations will
3943 // use the adjusted instance size.
3944 // - Decrease expected_nof_properties so that an allocations made from
3945 // another context will use the adjusted instance size too.
3946 // - Exit "in progress" state by clearing the reference to the initial_map
3947 // and setting the regular construct stub (generic or inline).
3948 //
3949 // The above is the main event sequence. Some special cases are possible
3950 // while the tracking is in progress:
3951 //
3952 // - GC occurs.
3953 // Check if the initial_map is referenced by any live objects (except this
3954 // SharedFunctionInfo). If it is, continue tracking as usual.
3955 // If it is not, clear the reference and reset the tracking state. The
3956 // tracking will be initiated again on the next constructor call.
3957 //
3958 // - The constructor is called from another context.
3959 // Immediately complete the tracking, perform all the necessary changes
3960 // to maps. This is necessary because there is no efficient way to track
3961 // multiple initial_maps.
3962 // Proceed to create an object in the current context (with the adjusted
3963 // size).
3964 //
3965 // - A different constructor function sharing the same SharedFunctionInfo is
3966 // called in the same context. This could be another closure in the same
3967 // context, or the first function could have been disposed.
3968 // This is handled the same way as the previous case.
3969 //
3970 // Important: inobject slack tracking is not attempted during the snapshot
3971 // creation.
3972
Ben Murdochf87a2032010-10-22 12:50:53 +01003973 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003974
3975 // [construction_count]: Counter for constructor calls made during
3976 // the tracking phase.
3977 inline int construction_count();
3978 inline void set_construction_count(int value);
3979
3980 // [initial_map]: initial map of the first function called as a constructor.
3981 // Saved for the duration of the tracking phase.
3982 // This is a weak link (GC resets it to undefined_value if no other live
3983 // object reference this map).
3984 DECL_ACCESSORS(initial_map, Object)
3985
3986 // True if the initial_map is not undefined and the countdown stub is
3987 // installed.
3988 inline bool IsInobjectSlackTrackingInProgress();
3989
3990 // Starts the tracking.
3991 // Stores the initial map and installs the countdown stub.
3992 // IsInobjectSlackTrackingInProgress is normally true after this call,
3993 // except when tracking have not been started (e.g. the map has no unused
3994 // properties or the snapshot is being built).
3995 void StartInobjectSlackTracking(Map* map);
3996
3997 // Completes the tracking.
3998 // IsInobjectSlackTrackingInProgress is false after this call.
3999 void CompleteInobjectSlackTracking();
4000
4001 // Clears the initial_map before the GC marking phase to ensure the reference
4002 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4003 void DetachInitialMap();
4004
4005 // Restores the link to the initial map after the GC marking phase.
4006 // IsInobjectSlackTrackingInProgress is true after this call.
4007 void AttachInitialMap(Map* map);
4008
4009 // False if there are definitely no live objects created from this function.
4010 // True if live objects _may_ exist (existence not guaranteed).
4011 // May go back from true to false after GC.
4012 inline bool live_objects_may_exist();
4013
4014 inline void set_live_objects_may_exist(bool value);
4015
Steve Blocka7e24c12009-10-30 11:49:00 +00004016 // [instance class name]: class name for instances.
4017 DECL_ACCESSORS(instance_class_name, Object)
4018
Steve Block6ded16b2010-05-10 14:33:55 +01004019 // [function data]: This field holds some additional data for function.
4020 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004021 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004022 // In the long run we don't want all functions to have this field but
4023 // we can fix that when we have a better model for storing hidden data
4024 // on objects.
4025 DECL_ACCESSORS(function_data, Object)
4026
Steve Block6ded16b2010-05-10 14:33:55 +01004027 inline bool IsApiFunction();
4028 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004029 inline bool HasBuiltinFunctionId();
4030 inline bool IsBuiltinMathFunction();
4031 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004032
Steve Blocka7e24c12009-10-30 11:49:00 +00004033 // [script info]: Script from which the function originates.
4034 DECL_ACCESSORS(script, Object)
4035
Steve Block6ded16b2010-05-10 14:33:55 +01004036 // [num_literals]: Number of literals used by this function.
4037 inline int num_literals();
4038 inline void set_num_literals(int value);
4039
Steve Blocka7e24c12009-10-30 11:49:00 +00004040 // [start_position_and_type]: Field used to store both the source code
4041 // position, whether or not the function is a function expression,
4042 // and whether or not the function is a toplevel function. The two
4043 // least significants bit indicates whether the function is an
4044 // expression and the rest contains the source code position.
4045 inline int start_position_and_type();
4046 inline void set_start_position_and_type(int value);
4047
4048 // [debug info]: Debug information.
4049 DECL_ACCESSORS(debug_info, Object)
4050
4051 // [inferred name]: Name inferred from variable or property
4052 // assignment of this function. Used to facilitate debugging and
4053 // profiling of JavaScript code written in OO style, where almost
4054 // all functions are anonymous but are assigned to object
4055 // properties.
4056 DECL_ACCESSORS(inferred_name, String)
4057
Ben Murdochf87a2032010-10-22 12:50:53 +01004058 // The function's name if it is non-empty, otherwise the inferred name.
4059 String* DebugName();
4060
Steve Blocka7e24c12009-10-30 11:49:00 +00004061 // Position of the 'function' token in the script source.
4062 inline int function_token_position();
4063 inline void set_function_token_position(int function_token_position);
4064
4065 // Position of this function in the script source.
4066 inline int start_position();
4067 inline void set_start_position(int start_position);
4068
4069 // End position of this function in the script source.
4070 inline int end_position();
4071 inline void set_end_position(int end_position);
4072
4073 // Is this function a function expression in the source code.
4074 inline bool is_expression();
4075 inline void set_is_expression(bool value);
4076
4077 // Is this function a top-level function (scripts, evals).
4078 inline bool is_toplevel();
4079 inline void set_is_toplevel(bool value);
4080
4081 // Bit field containing various information collected by the compiler to
4082 // drive optimization.
4083 inline int compiler_hints();
4084 inline void set_compiler_hints(int value);
4085
Ben Murdochb0fe1622011-05-05 13:52:32 +01004086 // A counter used to determine when to stress the deoptimizer with a
4087 // deopt.
4088 inline Smi* deopt_counter();
4089 inline void set_deopt_counter(Smi* counter);
4090
Steve Blocka7e24c12009-10-30 11:49:00 +00004091 // Add information on assignments of the form this.x = ...;
4092 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004093 bool has_only_simple_this_property_assignments,
4094 FixedArray* this_property_assignments);
4095
4096 // Clear information on assignments of the form this.x = ...;
4097 void ClearThisPropertyAssignmentsInfo();
4098
4099 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004100 // this.x = y; where y is either a constant or refers to an argument.
4101 inline bool has_only_simple_this_property_assignments();
4102
Leon Clarked91b9f72010-01-27 17:25:45 +00004103 inline bool try_full_codegen();
4104 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00004105
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004106 // Indicates if this function can be lazy compiled.
4107 // This is used to determine if we can safely flush code from a function
4108 // when doing GC if we expect that the function will no longer be used.
4109 inline bool allows_lazy_compilation();
4110 inline void set_allows_lazy_compilation(bool flag);
4111
Iain Merrick75681382010-08-19 15:07:18 +01004112 // Indicates how many full GCs this function has survived with assigned
4113 // code object. Used to determine when it is relatively safe to flush
4114 // this code object and replace it with lazy compilation stub.
4115 // Age is reset when GC notices that the code object is referenced
4116 // from the stack or compilation cache.
4117 inline int code_age();
4118 inline void set_code_age(int age);
4119
Ben Murdochb0fe1622011-05-05 13:52:32 +01004120 // Indicates whether optimizations have been disabled for this
4121 // shared function info. If a function is repeatedly optimized or if
4122 // we cannot optimize the function we disable optimization to avoid
4123 // spending time attempting to optimize it again.
4124 inline bool optimization_disabled();
4125 inline void set_optimization_disabled(bool value);
4126
4127 // Indicates whether or not the code in the shared function support
4128 // deoptimization.
4129 inline bool has_deoptimization_support();
4130
4131 // Enable deoptimization support through recompiled code.
4132 void EnableDeoptimizationSupport(Code* recompiled);
4133
4134 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4135 // code, returns whether it asserted (i.e., always true if assertions are
4136 // disabled).
4137 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004138
Andrei Popescu402d9372010-02-26 13:31:12 +00004139 // Check whether a inlined constructor can be generated with the given
4140 // prototype.
4141 bool CanGenerateInlineConstructor(Object* prototype);
4142
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004143 // Prevents further attempts to generate inline constructors.
4144 // To be called if generation failed for any reason.
4145 void ForbidInlineConstructor();
4146
Steve Blocka7e24c12009-10-30 11:49:00 +00004147 // For functions which only contains this property assignments this provides
4148 // access to the names for the properties assigned.
4149 DECL_ACCESSORS(this_property_assignments, Object)
4150 inline int this_property_assignments_count();
4151 inline void set_this_property_assignments_count(int value);
4152 String* GetThisPropertyAssignmentName(int index);
4153 bool IsThisPropertyAssignmentArgument(int index);
4154 int GetThisPropertyAssignmentArgument(int index);
4155 Object* GetThisPropertyAssignmentConstant(int index);
4156
4157 // [source code]: Source code for the function.
4158 bool HasSourceCode();
4159 Object* GetSourceCode();
4160
Ben Murdochb0fe1622011-05-05 13:52:32 +01004161 inline int opt_count();
4162 inline void set_opt_count(int opt_count);
4163
4164 // Source size of this function.
4165 int SourceSize();
4166
Steve Blocka7e24c12009-10-30 11:49:00 +00004167 // Calculate the instance size.
4168 int CalculateInstanceSize();
4169
4170 // Calculate the number of in-object properties.
4171 int CalculateInObjectProperties();
4172
4173 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004174 // Set max_length to -1 for unlimited length.
4175 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004176#ifdef OBJECT_PRINT
4177 inline void SharedFunctionInfoPrint() {
4178 SharedFunctionInfoPrint(stdout);
4179 }
4180 void SharedFunctionInfoPrint(FILE* out);
4181#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004182#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004183 void SharedFunctionInfoVerify();
4184#endif
4185
4186 // Casting.
4187 static inline SharedFunctionInfo* cast(Object* obj);
4188
4189 // Constants.
4190 static const int kDontAdaptArgumentsSentinel = -1;
4191
4192 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004193 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004194 static const int kNameOffset = HeapObject::kHeaderSize;
4195 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004196 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4197 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004198 static const int kInstanceClassNameOffset =
4199 kConstructStubOffset + kPointerSize;
4200 static const int kFunctionDataOffset =
4201 kInstanceClassNameOffset + kPointerSize;
4202 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4203 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4204 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004205 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004206 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004207 static const int kThisPropertyAssignmentsOffset =
4208 kInitialMapOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004209 static const int kDeoptCounterOffset =
4210 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004211#if V8_HOST_ARCH_32_BIT
4212 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004213 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004214 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004215 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4216 static const int kExpectedNofPropertiesOffset =
4217 kFormalParameterCountOffset + kPointerSize;
4218 static const int kNumLiteralsOffset =
4219 kExpectedNofPropertiesOffset + kPointerSize;
4220 static const int kStartPositionAndTypeOffset =
4221 kNumLiteralsOffset + kPointerSize;
4222 static const int kEndPositionOffset =
4223 kStartPositionAndTypeOffset + kPointerSize;
4224 static const int kFunctionTokenPositionOffset =
4225 kEndPositionOffset + kPointerSize;
4226 static const int kCompilerHintsOffset =
4227 kFunctionTokenPositionOffset + kPointerSize;
4228 static const int kThisPropertyAssignmentsCountOffset =
4229 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004230 static const int kOptCountOffset =
4231 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004232 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004233 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004234#else
4235 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004236 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004237 // garbage collections.
4238 // To avoid wasting space on 64-bit architectures we use
4239 // the following trick: we group integer fields into pairs
4240 // First integer in each pair is shifted left by 1.
4241 // By doing this we guarantee that LSB of each kPointerSize aligned
4242 // word is not set and thus this word cannot be treated as pointer
4243 // to HeapObject during old space traversal.
4244 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004245 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004246 static const int kFormalParameterCountOffset =
4247 kLengthOffset + kIntSize;
4248
Steve Blocka7e24c12009-10-30 11:49:00 +00004249 static const int kExpectedNofPropertiesOffset =
4250 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004251 static const int kNumLiteralsOffset =
4252 kExpectedNofPropertiesOffset + kIntSize;
4253
4254 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004255 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004256 static const int kStartPositionAndTypeOffset =
4257 kEndPositionOffset + kIntSize;
4258
4259 static const int kFunctionTokenPositionOffset =
4260 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004261 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004262 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004263
Steve Blocka7e24c12009-10-30 11:49:00 +00004264 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004265 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004266 static const int kOptCountOffset =
4267 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004268
Steve Block6ded16b2010-05-10 14:33:55 +01004269 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004270 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004271
4272#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004273
4274 // The construction counter for inobject slack tracking is stored in the
4275 // most significant byte of compiler_hints which is otherwise unused.
4276 // Its offset depends on the endian-ness of the architecture.
4277#if __BYTE_ORDER == __LITTLE_ENDIAN
4278 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4279#elif __BYTE_ORDER == __BIG_ENDIAN
4280 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4281#else
4282#error Unknown byte ordering
4283#endif
4284
Steve Block6ded16b2010-05-10 14:33:55 +01004285 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004286
Iain Merrick75681382010-08-19 15:07:18 +01004287 typedef FixedBodyDescriptor<kNameOffset,
4288 kThisPropertyAssignmentsOffset + kPointerSize,
4289 kSize> BodyDescriptor;
4290
Steve Blocka7e24c12009-10-30 11:49:00 +00004291 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00004292 // Bit positions in start_position_and_type.
4293 // The source code start position is in the 30 most significant bits of
4294 // the start_position_and_type field.
4295 static const int kIsExpressionBit = 0;
4296 static const int kIsTopLevelBit = 1;
4297 static const int kStartPositionShift = 2;
4298 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4299
4300 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00004301 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00004302 static const int kTryFullCodegen = 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004303 static const int kAllowLazyCompilation = 2;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004304 static const int kLiveObjectsMayExist = 3;
4305 static const int kCodeAgeShift = 4;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004306 static const int kCodeAgeMask = 0x7;
4307 static const int kOptimizationDisabled = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +00004308
4309 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4310};
4311
4312
4313// JSFunction describes JavaScript functions.
4314class JSFunction: public JSObject {
4315 public:
4316 // [prototype_or_initial_map]:
4317 DECL_ACCESSORS(prototype_or_initial_map, Object)
4318
4319 // [shared_function_info]: The information about the function that
4320 // can be shared by instances.
4321 DECL_ACCESSORS(shared, SharedFunctionInfo)
4322
Iain Merrick75681382010-08-19 15:07:18 +01004323 inline SharedFunctionInfo* unchecked_shared();
4324
Steve Blocka7e24c12009-10-30 11:49:00 +00004325 // [context]: The context for this function.
4326 inline Context* context();
4327 inline Object* unchecked_context();
4328 inline void set_context(Object* context);
4329
4330 // [code]: The generated code object for this function. Executed
4331 // when the function is invoked, e.g. foo() or new foo(). See
4332 // [[Call]] and [[Construct]] description in ECMA-262, section
4333 // 8.6.2, page 27.
4334 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004335 inline void set_code(Code* code);
4336 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004337
Iain Merrick75681382010-08-19 15:07:18 +01004338 inline Code* unchecked_code();
4339
Steve Blocka7e24c12009-10-30 11:49:00 +00004340 // Tells whether this function is builtin.
4341 inline bool IsBuiltin();
4342
Ben Murdochb0fe1622011-05-05 13:52:32 +01004343 // Tells whether or not the function needs arguments adaption.
4344 inline bool NeedsArgumentsAdaption();
4345
4346 // Tells whether or not this function has been optimized.
4347 inline bool IsOptimized();
4348
4349 // Mark this function for lazy recompilation. The function will be
4350 // recompiled the next time it is executed.
4351 void MarkForLazyRecompilation();
4352
4353 // Tells whether or not the function is already marked for lazy
4354 // recompilation.
4355 inline bool IsMarkedForLazyRecompilation();
4356
4357 // Compute a hash code for the source code of this function.
4358 uint32_t SourceHash();
4359
4360 // Check whether or not this function is inlineable.
4361 bool IsInlineable();
4362
Steve Blocka7e24c12009-10-30 11:49:00 +00004363 // [literals]: Fixed array holding the materialized literals.
4364 //
4365 // If the function contains object, regexp or array literals, the
4366 // literals array prefix contains the object, regexp, and array
4367 // function to be used when creating these literals. This is
4368 // necessary so that we do not dynamically lookup the object, regexp
4369 // or array functions. Performing a dynamic lookup, we might end up
4370 // using the functions from a new context that we should not have
4371 // access to.
4372 DECL_ACCESSORS(literals, FixedArray)
4373
4374 // The initial map for an object created by this constructor.
4375 inline Map* initial_map();
4376 inline void set_initial_map(Map* value);
4377 inline bool has_initial_map();
4378
4379 // Get and set the prototype property on a JSFunction. If the
4380 // function has an initial map the prototype is set on the initial
4381 // map. Otherwise, the prototype is put in the initial map field
4382 // until an initial map is needed.
4383 inline bool has_prototype();
4384 inline bool has_instance_prototype();
4385 inline Object* prototype();
4386 inline Object* instance_prototype();
4387 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07004388 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004389
Steve Block6ded16b2010-05-10 14:33:55 +01004390 // After prototype is removed, it will not be created when accessed, and
4391 // [[Construct]] from this function will not be allowed.
4392 Object* RemovePrototype();
4393 inline bool should_have_prototype();
4394
Steve Blocka7e24c12009-10-30 11:49:00 +00004395 // Accessor for this function's initial map's [[class]]
4396 // property. This is primarily used by ECMA native functions. This
4397 // method sets the class_name field of this function's initial map
4398 // to a given value. It creates an initial map if this function does
4399 // not have one. Note that this method does not copy the initial map
4400 // if it has one already, but simply replaces it with the new value.
4401 // Instances created afterwards will have a map whose [[class]] is
4402 // set to 'value', but there is no guarantees on instances created
4403 // before.
4404 Object* SetInstanceClassName(String* name);
4405
4406 // Returns if this function has been compiled to native code yet.
4407 inline bool is_compiled();
4408
Ben Murdochb0fe1622011-05-05 13:52:32 +01004409 // [next_function_link]: Field for linking functions. This list is treated as
4410 // a weak list by the GC.
4411 DECL_ACCESSORS(next_function_link, Object)
4412
4413 // Prints the name of the function using PrintF.
4414 inline void PrintName() {
4415 PrintName(stdout);
4416 }
4417 void PrintName(FILE* out);
4418
Steve Blocka7e24c12009-10-30 11:49:00 +00004419 // Casting.
4420 static inline JSFunction* cast(Object* obj);
4421
Steve Block791712a2010-08-27 10:21:07 +01004422 // Iterates the objects, including code objects indirectly referenced
4423 // through pointers to the first instruction in the code object.
4424 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
4425
Steve Blocka7e24c12009-10-30 11:49:00 +00004426 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004427#ifdef OBJECT_PRINT
4428 inline void JSFunctionPrint() {
4429 JSFunctionPrint(stdout);
4430 }
4431 void JSFunctionPrint(FILE* out);
4432#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004433#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004434 void JSFunctionVerify();
4435#endif
4436
4437 // Returns the number of allocated literals.
4438 inline int NumberOfLiterals();
4439
4440 // Retrieve the global context from a function's literal array.
4441 static Context* GlobalContextFromLiterals(FixedArray* literals);
4442
Ben Murdochb0fe1622011-05-05 13:52:32 +01004443 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
4444 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01004445 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01004446 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01004447 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004448 static const int kSharedFunctionInfoOffset =
4449 kPrototypeOrInitialMapOffset + kPointerSize;
4450 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
4451 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004452 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
4453 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
4454 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004455
4456 // Layout of the literals array.
4457 static const int kLiteralsPrefixSize = 1;
4458 static const int kLiteralGlobalContextIndex = 0;
4459 private:
4460 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
4461};
4462
4463
4464// JSGlobalProxy's prototype must be a JSGlobalObject or null,
4465// and the prototype is hidden. JSGlobalProxy always delegates
4466// property accesses to its prototype if the prototype is not null.
4467//
4468// A JSGlobalProxy can be reinitialized which will preserve its identity.
4469//
4470// Accessing a JSGlobalProxy requires security check.
4471
4472class JSGlobalProxy : public JSObject {
4473 public:
4474 // [context]: the owner global context of this proxy object.
4475 // It is null value if this object is not used by any context.
4476 DECL_ACCESSORS(context, Object)
4477
4478 // Casting.
4479 static inline JSGlobalProxy* cast(Object* obj);
4480
4481 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004482#ifdef OBJECT_PRINT
4483 inline void JSGlobalProxyPrint() {
4484 JSGlobalProxyPrint(stdout);
4485 }
4486 void JSGlobalProxyPrint(FILE* out);
4487#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004488#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004489 void JSGlobalProxyVerify();
4490#endif
4491
4492 // Layout description.
4493 static const int kContextOffset = JSObject::kHeaderSize;
4494 static const int kSize = kContextOffset + kPointerSize;
4495
4496 private:
4497
4498 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
4499};
4500
4501
4502// Forward declaration.
4503class JSBuiltinsObject;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004504class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00004505
4506// Common super class for JavaScript global objects and the special
4507// builtins global objects.
4508class GlobalObject: public JSObject {
4509 public:
4510 // [builtins]: the object holding the runtime routines written in JS.
4511 DECL_ACCESSORS(builtins, JSBuiltinsObject)
4512
4513 // [global context]: the global context corresponding to this global object.
4514 DECL_ACCESSORS(global_context, Context)
4515
4516 // [global receiver]: the global receiver object of the context
4517 DECL_ACCESSORS(global_receiver, JSObject)
4518
4519 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004520 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004521
John Reck59135872010-11-02 12:39:01 -07004522 // This is like GetProperty, but is used when you know the lookup won't fail
4523 // by throwing an exception. This is for the debug and builtins global
4524 // objects, where it is known which properties can be expected to be present
4525 // on the object.
4526 Object* GetPropertyNoExceptionThrown(String* key) {
4527 Object* answer = GetProperty(key)->ToObjectUnchecked();
4528 return answer;
4529 }
4530
Steve Blocka7e24c12009-10-30 11:49:00 +00004531 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07004532 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00004533
4534 // Casting.
4535 static inline GlobalObject* cast(Object* obj);
4536
4537 // Layout description.
4538 static const int kBuiltinsOffset = JSObject::kHeaderSize;
4539 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
4540 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
4541 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
4542
4543 private:
4544 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4545
4546 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
4547};
4548
4549
4550// JavaScript global object.
4551class JSGlobalObject: public GlobalObject {
4552 public:
4553
4554 // Casting.
4555 static inline JSGlobalObject* cast(Object* obj);
4556
4557 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004558#ifdef OBJECT_PRINT
4559 inline void JSGlobalObjectPrint() {
4560 JSGlobalObjectPrint(stdout);
4561 }
4562 void JSGlobalObjectPrint(FILE* out);
4563#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004564#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004565 void JSGlobalObjectVerify();
4566#endif
4567
4568 // Layout description.
4569 static const int kSize = GlobalObject::kHeaderSize;
4570
4571 private:
4572 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
4573};
4574
4575
4576// Builtins global object which holds the runtime routines written in
4577// JavaScript.
4578class JSBuiltinsObject: public GlobalObject {
4579 public:
4580 // Accessors for the runtime routines written in JavaScript.
4581 inline Object* javascript_builtin(Builtins::JavaScript id);
4582 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
4583
Steve Block6ded16b2010-05-10 14:33:55 +01004584 // Accessors for code of the runtime routines written in JavaScript.
4585 inline Code* javascript_builtin_code(Builtins::JavaScript id);
4586 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
4587
Steve Blocka7e24c12009-10-30 11:49:00 +00004588 // Casting.
4589 static inline JSBuiltinsObject* cast(Object* obj);
4590
4591 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004592#ifdef OBJECT_PRINT
4593 inline void JSBuiltinsObjectPrint() {
4594 JSBuiltinsObjectPrint(stdout);
4595 }
4596 void JSBuiltinsObjectPrint(FILE* out);
4597#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004598#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004599 void JSBuiltinsObjectVerify();
4600#endif
4601
4602 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01004603 // room for two pointers per runtime routine written in javascript
4604 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00004605 static const int kJSBuiltinsCount = Builtins::id_count;
4606 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004607 static const int kJSBuiltinsCodeOffset =
4608 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004609 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01004610 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
4611
4612 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
4613 return kJSBuiltinsOffset + id * kPointerSize;
4614 }
4615
4616 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
4617 return kJSBuiltinsCodeOffset + id * kPointerSize;
4618 }
4619
Steve Blocka7e24c12009-10-30 11:49:00 +00004620 private:
4621 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
4622};
4623
4624
4625// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
4626class JSValue: public JSObject {
4627 public:
4628 // [value]: the object being wrapped.
4629 DECL_ACCESSORS(value, Object)
4630
4631 // Casting.
4632 static inline JSValue* cast(Object* obj);
4633
4634 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004635#ifdef OBJECT_PRINT
4636 inline void JSValuePrint() {
4637 JSValuePrint(stdout);
4638 }
4639 void JSValuePrint(FILE* out);
4640#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004641#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004642 void JSValueVerify();
4643#endif
4644
4645 // Layout description.
4646 static const int kValueOffset = JSObject::kHeaderSize;
4647 static const int kSize = kValueOffset + kPointerSize;
4648
4649 private:
4650 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
4651};
4652
4653// Regular expressions
4654// The regular expression holds a single reference to a FixedArray in
4655// the kDataOffset field.
4656// The FixedArray contains the following data:
4657// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
4658// - reference to the original source string
4659// - reference to the original flag string
4660// If it is an atom regexp
4661// - a reference to a literal string to search for
4662// If it is an irregexp regexp:
4663// - a reference to code for ASCII inputs (bytecode or compiled).
4664// - a reference to code for UC16 inputs (bytecode or compiled).
4665// - max number of registers used by irregexp implementations.
4666// - number of capture registers (output values) of the regexp.
4667class JSRegExp: public JSObject {
4668 public:
4669 // Meaning of Type:
4670 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
4671 // ATOM: A simple string to match against using an indexOf operation.
4672 // IRREGEXP: Compiled with Irregexp.
4673 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
4674 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
4675 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
4676
4677 class Flags {
4678 public:
4679 explicit Flags(uint32_t value) : value_(value) { }
4680 bool is_global() { return (value_ & GLOBAL) != 0; }
4681 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
4682 bool is_multiline() { return (value_ & MULTILINE) != 0; }
4683 uint32_t value() { return value_; }
4684 private:
4685 uint32_t value_;
4686 };
4687
4688 DECL_ACCESSORS(data, Object)
4689
4690 inline Type TypeTag();
4691 inline int CaptureCount();
4692 inline Flags GetFlags();
4693 inline String* Pattern();
4694 inline Object* DataAt(int index);
4695 // Set implementation data after the object has been prepared.
4696 inline void SetDataAt(int index, Object* value);
4697 static int code_index(bool is_ascii) {
4698 if (is_ascii) {
4699 return kIrregexpASCIICodeIndex;
4700 } else {
4701 return kIrregexpUC16CodeIndex;
4702 }
4703 }
4704
4705 static inline JSRegExp* cast(Object* obj);
4706
4707 // Dispatched behavior.
4708#ifdef DEBUG
4709 void JSRegExpVerify();
4710#endif
4711
4712 static const int kDataOffset = JSObject::kHeaderSize;
4713 static const int kSize = kDataOffset + kPointerSize;
4714
4715 // Indices in the data array.
4716 static const int kTagIndex = 0;
4717 static const int kSourceIndex = kTagIndex + 1;
4718 static const int kFlagsIndex = kSourceIndex + 1;
4719 static const int kDataIndex = kFlagsIndex + 1;
4720 // The data fields are used in different ways depending on the
4721 // value of the tag.
4722 // Atom regexps (literal strings).
4723 static const int kAtomPatternIndex = kDataIndex;
4724
4725 static const int kAtomDataSize = kAtomPatternIndex + 1;
4726
4727 // Irregexp compiled code or bytecode for ASCII. If compilation
4728 // fails, this fields hold an exception object that should be
4729 // thrown if the regexp is used again.
4730 static const int kIrregexpASCIICodeIndex = kDataIndex;
4731 // Irregexp compiled code or bytecode for UC16. If compilation
4732 // fails, this fields hold an exception object that should be
4733 // thrown if the regexp is used again.
4734 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
4735 // Maximal number of registers used by either ASCII or UC16.
4736 // Only used to check that there is enough stack space
4737 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
4738 // Number of captures in the compiled regexp.
4739 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
4740
4741 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00004742
4743 // Offsets directly into the data fixed array.
4744 static const int kDataTagOffset =
4745 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
4746 static const int kDataAsciiCodeOffset =
4747 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00004748 static const int kDataUC16CodeOffset =
4749 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00004750 static const int kIrregexpCaptureCountOffset =
4751 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004752
4753 // In-object fields.
4754 static const int kSourceFieldIndex = 0;
4755 static const int kGlobalFieldIndex = 1;
4756 static const int kIgnoreCaseFieldIndex = 2;
4757 static const int kMultilineFieldIndex = 3;
4758 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01004759 static const int kInObjectFieldCount = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00004760};
4761
4762
4763class CompilationCacheShape {
4764 public:
4765 static inline bool IsMatch(HashTableKey* key, Object* value) {
4766 return key->IsMatch(value);
4767 }
4768
4769 static inline uint32_t Hash(HashTableKey* key) {
4770 return key->Hash();
4771 }
4772
4773 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4774 return key->HashForObject(object);
4775 }
4776
John Reck59135872010-11-02 12:39:01 -07004777 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004778 return key->AsObject();
4779 }
4780
4781 static const int kPrefixSize = 0;
4782 static const int kEntrySize = 2;
4783};
4784
Steve Block3ce2e202009-11-05 08:53:23 +00004785
Steve Blocka7e24c12009-10-30 11:49:00 +00004786class CompilationCacheTable: public HashTable<CompilationCacheShape,
4787 HashTableKey*> {
4788 public:
4789 // Find cached value for a string key, otherwise return null.
4790 Object* Lookup(String* src);
4791 Object* LookupEval(String* src, Context* context);
4792 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07004793 MaybeObject* Put(String* src, Object* value);
4794 MaybeObject* PutEval(String* src, Context* context, Object* value);
4795 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004796
Ben Murdochb0fe1622011-05-05 13:52:32 +01004797 // Remove given value from cache.
4798 void Remove(Object* value);
4799
Steve Blocka7e24c12009-10-30 11:49:00 +00004800 static inline CompilationCacheTable* cast(Object* obj);
4801
4802 private:
4803 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
4804};
4805
4806
Steve Block6ded16b2010-05-10 14:33:55 +01004807class CodeCache: public Struct {
4808 public:
4809 DECL_ACCESSORS(default_cache, FixedArray)
4810 DECL_ACCESSORS(normal_type_cache, Object)
4811
4812 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07004813 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004814
4815 // Lookup code object in the cache. Returns code object if found and undefined
4816 // if not.
4817 Object* Lookup(String* name, Code::Flags flags);
4818
4819 // Get the internal index of a code object in the cache. Returns -1 if the
4820 // code object is not in that cache. This index can be used to later call
4821 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
4822 // RemoveByIndex.
4823 int GetIndex(Object* name, Code* code);
4824
4825 // Remove an object from the cache with the provided internal index.
4826 void RemoveByIndex(Object* name, Code* code, int index);
4827
4828 static inline CodeCache* cast(Object* obj);
4829
Ben Murdochb0fe1622011-05-05 13:52:32 +01004830#ifdef OBJECT_PRINT
4831 inline void CodeCachePrint() {
4832 CodeCachePrint(stdout);
4833 }
4834 void CodeCachePrint(FILE* out);
4835#endif
Steve Block6ded16b2010-05-10 14:33:55 +01004836#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01004837 void CodeCacheVerify();
4838#endif
4839
4840 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
4841 static const int kNormalTypeCacheOffset =
4842 kDefaultCacheOffset + kPointerSize;
4843 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
4844
4845 private:
John Reck59135872010-11-02 12:39:01 -07004846 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
4847 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004848 Object* LookupDefaultCache(String* name, Code::Flags flags);
4849 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
4850
4851 // Code cache layout of the default cache. Elements are alternating name and
4852 // code objects for non normal load/store/call IC's.
4853 static const int kCodeCacheEntrySize = 2;
4854 static const int kCodeCacheEntryNameOffset = 0;
4855 static const int kCodeCacheEntryCodeOffset = 1;
4856
4857 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
4858};
4859
4860
4861class CodeCacheHashTableShape {
4862 public:
4863 static inline bool IsMatch(HashTableKey* key, Object* value) {
4864 return key->IsMatch(value);
4865 }
4866
4867 static inline uint32_t Hash(HashTableKey* key) {
4868 return key->Hash();
4869 }
4870
4871 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4872 return key->HashForObject(object);
4873 }
4874
John Reck59135872010-11-02 12:39:01 -07004875 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01004876 return key->AsObject();
4877 }
4878
4879 static const int kPrefixSize = 0;
4880 static const int kEntrySize = 2;
4881};
4882
4883
4884class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
4885 HashTableKey*> {
4886 public:
4887 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07004888 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004889
4890 int GetIndex(String* name, Code::Flags flags);
4891 void RemoveByIndex(int index);
4892
4893 static inline CodeCacheHashTable* cast(Object* obj);
4894
4895 // Initial size of the fixed array backing the hash table.
4896 static const int kInitialSize = 64;
4897
4898 private:
4899 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
4900};
4901
4902
Steve Blocka7e24c12009-10-30 11:49:00 +00004903enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
4904enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
4905
4906
4907class StringHasher {
4908 public:
4909 inline StringHasher(int length);
4910
4911 // Returns true if the hash of this string can be computed without
4912 // looking at the contents.
4913 inline bool has_trivial_hash();
4914
4915 // Add a character to the hash and update the array index calculation.
4916 inline void AddCharacter(uc32 c);
4917
4918 // Adds a character to the hash but does not update the array index
4919 // calculation. This can only be called when it has been verified
4920 // that the input is not an array index.
4921 inline void AddCharacterNoIndex(uc32 c);
4922
4923 // Returns the value to store in the hash field of a string with
4924 // the given length and contents.
4925 uint32_t GetHashField();
4926
4927 // Returns true if the characters seen so far make up a legal array
4928 // index.
4929 bool is_array_index() { return is_array_index_; }
4930
4931 bool is_valid() { return is_valid_; }
4932
4933 void invalidate() { is_valid_ = false; }
4934
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004935 // Calculated hash value for a string consisting of 1 to
4936 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
4937 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01004938 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004939
Steve Blocka7e24c12009-10-30 11:49:00 +00004940 private:
4941
4942 uint32_t array_index() {
4943 ASSERT(is_array_index());
4944 return array_index_;
4945 }
4946
4947 inline uint32_t GetHash();
4948
4949 int length_;
4950 uint32_t raw_running_hash_;
4951 uint32_t array_index_;
4952 bool is_array_index_;
4953 bool is_first_char_;
4954 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00004955 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00004956};
4957
4958
4959// The characteristics of a string are stored in its map. Retrieving these
4960// few bits of information is moderately expensive, involving two memory
4961// loads where the second is dependent on the first. To improve efficiency
4962// the shape of the string is given its own class so that it can be retrieved
4963// once and used for several string operations. A StringShape is small enough
4964// to be passed by value and is immutable, but be aware that flattening a
4965// string can potentially alter its shape. Also be aware that a GC caused by
4966// something else can alter the shape of a string due to ConsString
4967// shortcutting. Keeping these restrictions in mind has proven to be error-
4968// prone and so we no longer put StringShapes in variables unless there is a
4969// concrete performance benefit at that particular point in the code.
4970class StringShape BASE_EMBEDDED {
4971 public:
4972 inline explicit StringShape(String* s);
4973 inline explicit StringShape(Map* s);
4974 inline explicit StringShape(InstanceType t);
4975 inline bool IsSequential();
4976 inline bool IsExternal();
4977 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00004978 inline bool IsExternalAscii();
4979 inline bool IsExternalTwoByte();
4980 inline bool IsSequentialAscii();
4981 inline bool IsSequentialTwoByte();
4982 inline bool IsSymbol();
4983 inline StringRepresentationTag representation_tag();
4984 inline uint32_t full_representation_tag();
4985 inline uint32_t size_tag();
4986#ifdef DEBUG
4987 inline uint32_t type() { return type_; }
4988 inline void invalidate() { valid_ = false; }
4989 inline bool valid() { return valid_; }
4990#else
4991 inline void invalidate() { }
4992#endif
4993 private:
4994 uint32_t type_;
4995#ifdef DEBUG
4996 inline void set_valid() { valid_ = true; }
4997 bool valid_;
4998#else
4999 inline void set_valid() { }
5000#endif
5001};
5002
5003
5004// The String abstract class captures JavaScript string values:
5005//
5006// Ecma-262:
5007// 4.3.16 String Value
5008// A string value is a member of the type String and is a finite
5009// ordered sequence of zero or more 16-bit unsigned integer values.
5010//
5011// All string values have a length field.
5012class String: public HeapObject {
5013 public:
5014 // Get and set the length of the string.
5015 inline int length();
5016 inline void set_length(int value);
5017
Steve Blockd0582a62009-12-15 09:54:21 +00005018 // Get and set the hash field of the string.
5019 inline uint32_t hash_field();
5020 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005021
5022 inline bool IsAsciiRepresentation();
5023 inline bool IsTwoByteRepresentation();
5024
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005025 // Returns whether this string has ascii chars, i.e. all of them can
5026 // be ascii encoded. This might be the case even if the string is
5027 // two-byte. Such strings may appear when the embedder prefers
5028 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01005029 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005030 // NOTE: this should be considered only a hint. False negatives are
5031 // possible.
5032 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005033
Steve Blocka7e24c12009-10-30 11:49:00 +00005034 // Get and set individual two byte chars in the string.
5035 inline void Set(int index, uint16_t value);
5036 // Get individual two byte char in the string. Repeated calls
5037 // to this method are not efficient unless the string is flat.
5038 inline uint16_t Get(int index);
5039
Leon Clarkef7060e22010-06-03 12:02:55 +01005040 // Try to flatten the string. Checks first inline to see if it is
5041 // necessary. Does nothing if the string is not a cons string.
5042 // Flattening allocates a sequential string with the same data as
5043 // the given string and mutates the cons string to a degenerate
5044 // form, where the first component is the new sequential string and
5045 // the second component is the empty string. If allocation fails,
5046 // this function returns a failure. If flattening succeeds, this
5047 // function returns the sequential string that is now the first
5048 // component of the cons string.
5049 //
5050 // Degenerate cons strings are handled specially by the garbage
5051 // collector (see IsShortcutCandidate).
5052 //
5053 // Use FlattenString from Handles.cc to flatten even in case an
5054 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07005055 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005056
Leon Clarkef7060e22010-06-03 12:02:55 +01005057 // Convenience function. Has exactly the same behavior as
5058 // TryFlatten(), except in the case of failure returns the original
5059 // string.
5060 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5061
Steve Blocka7e24c12009-10-30 11:49:00 +00005062 Vector<const char> ToAsciiVector();
5063 Vector<const uc16> ToUC16Vector();
5064
5065 // Mark the string as an undetectable object. It only applies to
5066 // ascii and two byte string types.
5067 bool MarkAsUndetectable();
5068
Steve Blockd0582a62009-12-15 09:54:21 +00005069 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07005070 MUST_USE_RESULT MaybeObject* SubString(int from,
5071 int to,
5072 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005073
5074 // String equality operations.
5075 inline bool Equals(String* other);
5076 bool IsEqualTo(Vector<const char> str);
5077
5078 // Return a UTF8 representation of the string. The string is null
5079 // terminated but may optionally contain nulls. Length is returned
5080 // in length_output if length_output is not a null pointer The string
5081 // should be nearly flat, otherwise the performance of this method may
5082 // be very slow (quadratic in the length). Setting robustness_flag to
5083 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5084 // handles unexpected data without causing assert failures and it does not
5085 // do any heap allocations. This is useful when printing stack traces.
5086 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5087 RobustnessFlag robustness_flag,
5088 int offset,
5089 int length,
5090 int* length_output = 0);
5091 SmartPointer<char> ToCString(
5092 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5093 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5094 int* length_output = 0);
5095
5096 int Utf8Length();
5097
5098 // Return a 16 bit Unicode representation of the string.
5099 // The string should be nearly flat, otherwise the performance of
5100 // of this method may be very bad. Setting robustness_flag to
5101 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5102 // handles unexpected data without causing assert failures and it does not
5103 // do any heap allocations. This is useful when printing stack traces.
5104 SmartPointer<uc16> ToWideCString(
5105 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
5106
5107 // Tells whether the hash code has been computed.
5108 inline bool HasHashCode();
5109
5110 // Returns a hash value used for the property table
5111 inline uint32_t Hash();
5112
Steve Blockd0582a62009-12-15 09:54:21 +00005113 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5114 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005115
5116 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5117 uint32_t* index,
5118 int length);
5119
5120 // Externalization.
5121 bool MakeExternal(v8::String::ExternalStringResource* resource);
5122 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5123
5124 // Conversion.
5125 inline bool AsArrayIndex(uint32_t* index);
5126
5127 // Casting.
5128 static inline String* cast(Object* obj);
5129
5130 void PrintOn(FILE* out);
5131
5132 // For use during stack traces. Performs rudimentary sanity check.
5133 bool LooksValid();
5134
5135 // Dispatched behavior.
5136 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005137#ifdef OBJECT_PRINT
5138 inline void StringPrint() {
5139 StringPrint(stdout);
5140 }
5141 void StringPrint(FILE* out);
5142#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005143#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005144 void StringVerify();
5145#endif
5146 inline bool IsFlat();
5147
5148 // Layout description.
5149 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005150 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005151 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005152
Steve Blockd0582a62009-12-15 09:54:21 +00005153 // Maximum number of characters to consider when trying to convert a string
5154 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00005155 static const int kMaxArrayIndexSize = 10;
5156
5157 // Max ascii char code.
5158 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
5159 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
5160 static const int kMaxUC16CharCode = 0xffff;
5161
Steve Blockd0582a62009-12-15 09:54:21 +00005162 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00005163 static const int kMinNonFlatLength = 13;
5164
5165 // Mask constant for checking if a string has a computed hash code
5166 // and if it is an array index. The least significant bit indicates
5167 // whether a hash code has been computed. If the hash code has been
5168 // computed the 2nd bit tells whether the string can be used as an
5169 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005170 static const int kHashNotComputedMask = 1;
5171 static const int kIsNotArrayIndexMask = 1 << 1;
5172 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00005173
Steve Blockd0582a62009-12-15 09:54:21 +00005174 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005175 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00005176
Steve Blocka7e24c12009-10-30 11:49:00 +00005177 // Array index strings this short can keep their index in the hash
5178 // field.
5179 static const int kMaxCachedArrayIndexLength = 7;
5180
Steve Blockd0582a62009-12-15 09:54:21 +00005181 // For strings which are array indexes the hash value has the string length
5182 // mixed into the hash, mainly to avoid a hash value of zero which would be
5183 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005184 static const int kArrayIndexValueBits = 24;
5185 static const int kArrayIndexLengthBits =
5186 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
5187
5188 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01005189 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005190
5191 static const int kArrayIndexHashLengthShift =
5192 kArrayIndexValueBits + kNofHashBitFields;
5193
Steve Blockd0582a62009-12-15 09:54:21 +00005194 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005195
5196 static const int kArrayIndexValueMask =
5197 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
5198
5199 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
5200 // could use a mask to test if the length of string is less than or equal to
5201 // kMaxCachedArrayIndexLength.
5202 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
5203
5204 static const int kContainsCachedArrayIndexMask =
5205 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
5206 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005207
5208 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005209 static const int kEmptyHashField =
5210 kIsNotArrayIndexMask | kHashNotComputedMask;
5211
5212 // Value of hash field containing computed hash equal to zero.
5213 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005214
5215 // Maximal string length.
5216 static const int kMaxLength = (1 << (32 - 2)) - 1;
5217
5218 // Max length for computing hash. For strings longer than this limit the
5219 // string length is used as the hash value.
5220 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00005221
5222 // Limit for truncation in short printing.
5223 static const int kMaxShortPrintLength = 1024;
5224
5225 // Support for regular expressions.
5226 const uc16* GetTwoByteData();
5227 const uc16* GetTwoByteData(unsigned start);
5228
5229 // Support for StringInputBuffer
5230 static const unibrow::byte* ReadBlock(String* input,
5231 unibrow::byte* util_buffer,
5232 unsigned capacity,
5233 unsigned* remaining,
5234 unsigned* offset);
5235 static const unibrow::byte* ReadBlock(String** input,
5236 unibrow::byte* util_buffer,
5237 unsigned capacity,
5238 unsigned* remaining,
5239 unsigned* offset);
5240
5241 // Helper function for flattening strings.
5242 template <typename sinkchar>
5243 static void WriteToFlat(String* source,
5244 sinkchar* sink,
5245 int from,
5246 int to);
5247
5248 protected:
5249 class ReadBlockBuffer {
5250 public:
5251 ReadBlockBuffer(unibrow::byte* util_buffer_,
5252 unsigned cursor_,
5253 unsigned capacity_,
5254 unsigned remaining_) :
5255 util_buffer(util_buffer_),
5256 cursor(cursor_),
5257 capacity(capacity_),
5258 remaining(remaining_) {
5259 }
5260 unibrow::byte* util_buffer;
5261 unsigned cursor;
5262 unsigned capacity;
5263 unsigned remaining;
5264 };
5265
Steve Blocka7e24c12009-10-30 11:49:00 +00005266 static inline const unibrow::byte* ReadBlock(String* input,
5267 ReadBlockBuffer* buffer,
5268 unsigned* offset,
5269 unsigned max_chars);
5270 static void ReadBlockIntoBuffer(String* input,
5271 ReadBlockBuffer* buffer,
5272 unsigned* offset_ptr,
5273 unsigned max_chars);
5274
5275 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01005276 // Try to flatten the top level ConsString that is hiding behind this
5277 // string. This is a no-op unless the string is a ConsString. Flatten
5278 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07005279 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01005280
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005281 static inline bool IsHashFieldComputed(uint32_t field);
5282
Steve Blocka7e24c12009-10-30 11:49:00 +00005283 // Slow case of String::Equals. This implementation works on any strings
5284 // but it is most efficient on strings that are almost flat.
5285 bool SlowEquals(String* other);
5286
5287 // Slow case of AsArrayIndex.
5288 bool SlowAsArrayIndex(uint32_t* index);
5289
5290 // Compute and set the hash code.
5291 uint32_t ComputeAndSetHash();
5292
5293 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
5294};
5295
5296
5297// The SeqString abstract class captures sequential string values.
5298class SeqString: public String {
5299 public:
5300
5301 // Casting.
5302 static inline SeqString* cast(Object* obj);
5303
Steve Blocka7e24c12009-10-30 11:49:00 +00005304 private:
5305 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
5306};
5307
5308
5309// The AsciiString class captures sequential ascii string objects.
5310// Each character in the AsciiString is an ascii character.
5311class SeqAsciiString: public SeqString {
5312 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005313 static const bool kHasAsciiEncoding = true;
5314
Steve Blocka7e24c12009-10-30 11:49:00 +00005315 // Dispatched behavior.
5316 inline uint16_t SeqAsciiStringGet(int index);
5317 inline void SeqAsciiStringSet(int index, uint16_t value);
5318
5319 // Get the address of the characters in this string.
5320 inline Address GetCharsAddress();
5321
5322 inline char* GetChars();
5323
5324 // Casting
5325 static inline SeqAsciiString* cast(Object* obj);
5326
5327 // Garbage collection support. This method is called by the
5328 // garbage collector to compute the actual size of an AsciiString
5329 // instance.
5330 inline int SeqAsciiStringSize(InstanceType instance_type);
5331
5332 // Computes the size for an AsciiString instance of a given length.
5333 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005334 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005335 }
5336
5337 // Layout description.
5338 static const int kHeaderSize = String::kSize;
5339 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5340
Leon Clarkee46be812010-01-19 14:06:41 +00005341 // Maximal memory usage for a single sequential ASCII string.
5342 static const int kMaxSize = 512 * MB;
5343 // Maximal length of a single sequential ASCII string.
5344 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5345 static const int kMaxLength = (kMaxSize - kHeaderSize);
5346
Steve Blocka7e24c12009-10-30 11:49:00 +00005347 // Support for StringInputBuffer.
5348 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5349 unsigned* offset,
5350 unsigned chars);
5351 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
5352 unsigned* offset,
5353 unsigned chars);
5354
5355 private:
5356 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
5357};
5358
5359
5360// The TwoByteString class captures sequential unicode string objects.
5361// Each character in the TwoByteString is a two-byte uint16_t.
5362class SeqTwoByteString: public SeqString {
5363 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005364 static const bool kHasAsciiEncoding = false;
5365
Steve Blocka7e24c12009-10-30 11:49:00 +00005366 // Dispatched behavior.
5367 inline uint16_t SeqTwoByteStringGet(int index);
5368 inline void SeqTwoByteStringSet(int index, uint16_t value);
5369
5370 // Get the address of the characters in this string.
5371 inline Address GetCharsAddress();
5372
5373 inline uc16* GetChars();
5374
5375 // For regexp code.
5376 const uint16_t* SeqTwoByteStringGetData(unsigned start);
5377
5378 // Casting
5379 static inline SeqTwoByteString* cast(Object* obj);
5380
5381 // Garbage collection support. This method is called by the
5382 // garbage collector to compute the actual size of a TwoByteString
5383 // instance.
5384 inline int SeqTwoByteStringSize(InstanceType instance_type);
5385
5386 // Computes the size for a TwoByteString instance of a given length.
5387 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005388 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005389 }
5390
5391 // Layout description.
5392 static const int kHeaderSize = String::kSize;
5393 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5394
Leon Clarkee46be812010-01-19 14:06:41 +00005395 // Maximal memory usage for a single sequential two-byte string.
5396 static const int kMaxSize = 512 * MB;
5397 // Maximal length of a single sequential two-byte string.
5398 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5399 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
5400
Steve Blocka7e24c12009-10-30 11:49:00 +00005401 // Support for StringInputBuffer.
5402 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5403 unsigned* offset_ptr,
5404 unsigned chars);
5405
5406 private:
5407 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
5408};
5409
5410
5411// The ConsString class describes string values built by using the
5412// addition operator on strings. A ConsString is a pair where the
5413// first and second components are pointers to other string values.
5414// One or both components of a ConsString can be pointers to other
5415// ConsStrings, creating a binary tree of ConsStrings where the leaves
5416// are non-ConsString string values. The string value represented by
5417// a ConsString can be obtained by concatenating the leaf string
5418// values in a left-to-right depth-first traversal of the tree.
5419class ConsString: public String {
5420 public:
5421 // First string of the cons cell.
5422 inline String* first();
5423 // Doesn't check that the result is a string, even in debug mode. This is
5424 // useful during GC where the mark bits confuse the checks.
5425 inline Object* unchecked_first();
5426 inline void set_first(String* first,
5427 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5428
5429 // Second string of the cons cell.
5430 inline String* second();
5431 // Doesn't check that the result is a string, even in debug mode. This is
5432 // useful during GC where the mark bits confuse the checks.
5433 inline Object* unchecked_second();
5434 inline void set_second(String* second,
5435 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5436
5437 // Dispatched behavior.
5438 uint16_t ConsStringGet(int index);
5439
5440 // Casting.
5441 static inline ConsString* cast(Object* obj);
5442
Steve Blocka7e24c12009-10-30 11:49:00 +00005443 // Layout description.
5444 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
5445 static const int kSecondOffset = kFirstOffset + kPointerSize;
5446 static const int kSize = kSecondOffset + kPointerSize;
5447
5448 // Support for StringInputBuffer.
5449 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
5450 unsigned* offset_ptr,
5451 unsigned chars);
5452 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5453 unsigned* offset_ptr,
5454 unsigned chars);
5455
5456 // Minimum length for a cons string.
5457 static const int kMinLength = 13;
5458
Iain Merrick75681382010-08-19 15:07:18 +01005459 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
5460 BodyDescriptor;
5461
Steve Blocka7e24c12009-10-30 11:49:00 +00005462 private:
5463 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
5464};
5465
5466
Steve Blocka7e24c12009-10-30 11:49:00 +00005467// The ExternalString class describes string values that are backed by
5468// a string resource that lies outside the V8 heap. ExternalStrings
5469// consist of the length field common to all strings, a pointer to the
5470// external resource. It is important to ensure (externally) that the
5471// resource is not deallocated while the ExternalString is live in the
5472// V8 heap.
5473//
5474// The API expects that all ExternalStrings are created through the
5475// API. Therefore, ExternalStrings should not be used internally.
5476class ExternalString: public String {
5477 public:
5478 // Casting
5479 static inline ExternalString* cast(Object* obj);
5480
5481 // Layout description.
5482 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
5483 static const int kSize = kResourceOffset + kPointerSize;
5484
5485 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
5486
5487 private:
5488 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
5489};
5490
5491
5492// The ExternalAsciiString class is an external string backed by an
5493// ASCII string.
5494class ExternalAsciiString: public ExternalString {
5495 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005496 static const bool kHasAsciiEncoding = true;
5497
Steve Blocka7e24c12009-10-30 11:49:00 +00005498 typedef v8::String::ExternalAsciiStringResource Resource;
5499
5500 // The underlying resource.
5501 inline Resource* resource();
5502 inline void set_resource(Resource* buffer);
5503
5504 // Dispatched behavior.
5505 uint16_t ExternalAsciiStringGet(int index);
5506
5507 // Casting.
5508 static inline ExternalAsciiString* cast(Object* obj);
5509
Steve Blockd0582a62009-12-15 09:54:21 +00005510 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005511 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
5512
5513 template<typename StaticVisitor>
5514 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00005515
Steve Blocka7e24c12009-10-30 11:49:00 +00005516 // Support for StringInputBuffer.
5517 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
5518 unsigned* offset,
5519 unsigned chars);
5520 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5521 unsigned* offset,
5522 unsigned chars);
5523
Steve Blocka7e24c12009-10-30 11:49:00 +00005524 private:
5525 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
5526};
5527
5528
5529// The ExternalTwoByteString class is an external string backed by a UTF-16
5530// encoded string.
5531class ExternalTwoByteString: public ExternalString {
5532 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005533 static const bool kHasAsciiEncoding = false;
5534
Steve Blocka7e24c12009-10-30 11:49:00 +00005535 typedef v8::String::ExternalStringResource Resource;
5536
5537 // The underlying string resource.
5538 inline Resource* resource();
5539 inline void set_resource(Resource* buffer);
5540
5541 // Dispatched behavior.
5542 uint16_t ExternalTwoByteStringGet(int index);
5543
5544 // For regexp code.
5545 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
5546
5547 // Casting.
5548 static inline ExternalTwoByteString* cast(Object* obj);
5549
Steve Blockd0582a62009-12-15 09:54:21 +00005550 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005551 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
5552
5553 template<typename StaticVisitor>
5554 inline void ExternalTwoByteStringIterateBody();
5555
Steve Blockd0582a62009-12-15 09:54:21 +00005556
Steve Blocka7e24c12009-10-30 11:49:00 +00005557 // Support for StringInputBuffer.
5558 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5559 unsigned* offset_ptr,
5560 unsigned chars);
5561
Steve Blocka7e24c12009-10-30 11:49:00 +00005562 private:
5563 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
5564};
5565
5566
5567// Utility superclass for stack-allocated objects that must be updated
5568// on gc. It provides two ways for the gc to update instances, either
5569// iterating or updating after gc.
5570class Relocatable BASE_EMBEDDED {
5571 public:
5572 inline Relocatable() : prev_(top_) { top_ = this; }
5573 virtual ~Relocatable() {
5574 ASSERT_EQ(top_, this);
5575 top_ = prev_;
5576 }
5577 virtual void IterateInstance(ObjectVisitor* v) { }
5578 virtual void PostGarbageCollection() { }
5579
5580 static void PostGarbageCollectionProcessing();
5581 static int ArchiveSpacePerThread();
5582 static char* ArchiveState(char* to);
5583 static char* RestoreState(char* from);
5584 static void Iterate(ObjectVisitor* v);
5585 static void Iterate(ObjectVisitor* v, Relocatable* top);
5586 static char* Iterate(ObjectVisitor* v, char* t);
5587 private:
5588 static Relocatable* top_;
5589 Relocatable* prev_;
5590};
5591
5592
5593// A flat string reader provides random access to the contents of a
5594// string independent of the character width of the string. The handle
5595// must be valid as long as the reader is being used.
5596class FlatStringReader : public Relocatable {
5597 public:
5598 explicit FlatStringReader(Handle<String> str);
5599 explicit FlatStringReader(Vector<const char> input);
5600 void PostGarbageCollection();
5601 inline uc32 Get(int index);
5602 int length() { return length_; }
5603 private:
5604 String** str_;
5605 bool is_ascii_;
5606 int length_;
5607 const void* start_;
5608};
5609
5610
5611// Note that StringInputBuffers are not valid across a GC! To fix this
5612// it would have to store a String Handle instead of a String* and
5613// AsciiStringReadBlock would have to be modified to use memcpy.
5614//
5615// StringInputBuffer is able to traverse any string regardless of how
5616// deeply nested a sequence of ConsStrings it is made of. However,
5617// performance will be better if deep strings are flattened before they
5618// are traversed. Since flattening requires memory allocation this is
5619// not always desirable, however (esp. in debugging situations).
5620class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
5621 public:
5622 virtual void Seek(unsigned pos);
5623 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
5624 inline StringInputBuffer(String* backing):
5625 unibrow::InputBuffer<String, String*, 1024>(backing) {}
5626};
5627
5628
5629class SafeStringInputBuffer
5630 : public unibrow::InputBuffer<String, String**, 256> {
5631 public:
5632 virtual void Seek(unsigned pos);
5633 inline SafeStringInputBuffer()
5634 : unibrow::InputBuffer<String, String**, 256>() {}
5635 inline SafeStringInputBuffer(String** backing)
5636 : unibrow::InputBuffer<String, String**, 256>(backing) {}
5637};
5638
5639
5640template <typename T>
5641class VectorIterator {
5642 public:
5643 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
5644 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
5645 T GetNext() { return data_[index_++]; }
5646 bool has_more() { return index_ < data_.length(); }
5647 private:
5648 Vector<const T> data_;
5649 int index_;
5650};
5651
5652
5653// The Oddball describes objects null, undefined, true, and false.
5654class Oddball: public HeapObject {
5655 public:
5656 // [to_string]: Cached to_string computed at startup.
5657 DECL_ACCESSORS(to_string, String)
5658
5659 // [to_number]: Cached to_number computed at startup.
5660 DECL_ACCESSORS(to_number, Object)
5661
5662 // Casting.
5663 static inline Oddball* cast(Object* obj);
5664
5665 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00005666#ifdef DEBUG
5667 void OddballVerify();
5668#endif
5669
5670 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07005671 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
5672 Object* to_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00005673
5674 // Layout description.
5675 static const int kToStringOffset = HeapObject::kHeaderSize;
5676 static const int kToNumberOffset = kToStringOffset + kPointerSize;
5677 static const int kSize = kToNumberOffset + kPointerSize;
5678
Iain Merrick75681382010-08-19 15:07:18 +01005679 typedef FixedBodyDescriptor<kToStringOffset,
5680 kToNumberOffset + kPointerSize,
5681 kSize> BodyDescriptor;
5682
Steve Blocka7e24c12009-10-30 11:49:00 +00005683 private:
5684 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
5685};
5686
5687
5688class JSGlobalPropertyCell: public HeapObject {
5689 public:
5690 // [value]: value of the global property.
5691 DECL_ACCESSORS(value, Object)
5692
5693 // Casting.
5694 static inline JSGlobalPropertyCell* cast(Object* obj);
5695
Steve Blocka7e24c12009-10-30 11:49:00 +00005696#ifdef DEBUG
5697 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005698#endif
5699#ifdef OBJECT_PRINT
5700 inline void JSGlobalPropertyCellPrint() {
5701 JSGlobalPropertyCellPrint(stdout);
5702 }
5703 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00005704#endif
5705
5706 // Layout description.
5707 static const int kValueOffset = HeapObject::kHeaderSize;
5708 static const int kSize = kValueOffset + kPointerSize;
5709
Iain Merrick75681382010-08-19 15:07:18 +01005710 typedef FixedBodyDescriptor<kValueOffset,
5711 kValueOffset + kPointerSize,
5712 kSize> BodyDescriptor;
5713
Steve Blocka7e24c12009-10-30 11:49:00 +00005714 private:
5715 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
5716};
5717
5718
5719
5720// Proxy describes objects pointing from JavaScript to C structures.
5721// Since they cannot contain references to JS HeapObjects they can be
5722// placed in old_data_space.
5723class Proxy: public HeapObject {
5724 public:
5725 // [proxy]: field containing the address.
5726 inline Address proxy();
5727 inline void set_proxy(Address value);
5728
5729 // Casting.
5730 static inline Proxy* cast(Object* obj);
5731
5732 // Dispatched behavior.
5733 inline void ProxyIterateBody(ObjectVisitor* v);
Iain Merrick75681382010-08-19 15:07:18 +01005734
5735 template<typename StaticVisitor>
5736 inline void ProxyIterateBody();
5737
Ben Murdochb0fe1622011-05-05 13:52:32 +01005738#ifdef OBJECT_PRINT
5739 inline void ProxyPrint() {
5740 ProxyPrint(stdout);
5741 }
5742 void ProxyPrint(FILE* out);
5743#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005744#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005745 void ProxyVerify();
5746#endif
5747
5748 // Layout description.
5749
5750 static const int kProxyOffset = HeapObject::kHeaderSize;
5751 static const int kSize = kProxyOffset + kPointerSize;
5752
5753 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
5754
5755 private:
5756 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
5757};
5758
5759
5760// The JSArray describes JavaScript Arrays
5761// Such an array can be in one of two modes:
5762// - fast, backing storage is a FixedArray and length <= elements.length();
5763// Please note: push and pop can be used to grow and shrink the array.
5764// - slow, backing storage is a HashTable with numbers as keys.
5765class JSArray: public JSObject {
5766 public:
5767 // [length]: The length property.
5768 DECL_ACCESSORS(length, Object)
5769
Leon Clarke4515c472010-02-03 11:58:03 +00005770 // Overload the length setter to skip write barrier when the length
5771 // is set to a smi. This matches the set function on FixedArray.
5772 inline void set_length(Smi* length);
5773
John Reck59135872010-11-02 12:39:01 -07005774 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
5775 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005776
5777 // Initialize the array with the given capacity. The function may
5778 // fail due to out-of-memory situations, but only if the requested
5779 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07005780 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00005781
5782 // Set the content of the array to the content of storage.
5783 inline void SetContent(FixedArray* storage);
5784
5785 // Casting.
5786 static inline JSArray* cast(Object* obj);
5787
5788 // Uses handles. Ensures that the fixed array backing the JSArray has at
5789 // least the stated size.
5790 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
5791
5792 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005793#ifdef OBJECT_PRINT
5794 inline void JSArrayPrint() {
5795 JSArrayPrint(stdout);
5796 }
5797 void JSArrayPrint(FILE* out);
5798#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005799#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005800 void JSArrayVerify();
5801#endif
5802
5803 // Number of element slots to pre-allocate for an empty array.
5804 static const int kPreallocatedArrayElements = 4;
5805
5806 // Layout description.
5807 static const int kLengthOffset = JSObject::kHeaderSize;
5808 static const int kSize = kLengthOffset + kPointerSize;
5809
5810 private:
5811 // Expand the fixed array backing of a fast-case JSArray to at least
5812 // the requested size.
5813 void Expand(int minimum_size_of_backing_fixed_array);
5814
5815 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
5816};
5817
5818
Steve Block6ded16b2010-05-10 14:33:55 +01005819// JSRegExpResult is just a JSArray with a specific initial map.
5820// This initial map adds in-object properties for "index" and "input"
5821// properties, as assigned by RegExp.prototype.exec, which allows
5822// faster creation of RegExp exec results.
5823// This class just holds constants used when creating the result.
5824// After creation the result must be treated as a JSArray in all regards.
5825class JSRegExpResult: public JSArray {
5826 public:
5827 // Offsets of object fields.
5828 static const int kIndexOffset = JSArray::kSize;
5829 static const int kInputOffset = kIndexOffset + kPointerSize;
5830 static const int kSize = kInputOffset + kPointerSize;
5831 // Indices of in-object properties.
5832 static const int kIndexIndex = 0;
5833 static const int kInputIndex = 1;
5834 private:
5835 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
5836};
5837
5838
Steve Blocka7e24c12009-10-30 11:49:00 +00005839// An accessor must have a getter, but can have no setter.
5840//
5841// When setting a property, V8 searches accessors in prototypes.
5842// If an accessor was found and it does not have a setter,
5843// the request is ignored.
5844//
5845// If the accessor in the prototype has the READ_ONLY property attribute, then
5846// a new value is added to the local object when the property is set.
5847// This shadows the accessor in the prototype.
5848class AccessorInfo: public Struct {
5849 public:
5850 DECL_ACCESSORS(getter, Object)
5851 DECL_ACCESSORS(setter, Object)
5852 DECL_ACCESSORS(data, Object)
5853 DECL_ACCESSORS(name, Object)
5854 DECL_ACCESSORS(flag, Smi)
5855
5856 inline bool all_can_read();
5857 inline void set_all_can_read(bool value);
5858
5859 inline bool all_can_write();
5860 inline void set_all_can_write(bool value);
5861
5862 inline bool prohibits_overwriting();
5863 inline void set_prohibits_overwriting(bool value);
5864
5865 inline PropertyAttributes property_attributes();
5866 inline void set_property_attributes(PropertyAttributes attributes);
5867
5868 static inline AccessorInfo* cast(Object* obj);
5869
Ben Murdochb0fe1622011-05-05 13:52:32 +01005870#ifdef OBJECT_PRINT
5871 inline void AccessorInfoPrint() {
5872 AccessorInfoPrint(stdout);
5873 }
5874 void AccessorInfoPrint(FILE* out);
5875#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005876#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005877 void AccessorInfoVerify();
5878#endif
5879
5880 static const int kGetterOffset = HeapObject::kHeaderSize;
5881 static const int kSetterOffset = kGetterOffset + kPointerSize;
5882 static const int kDataOffset = kSetterOffset + kPointerSize;
5883 static const int kNameOffset = kDataOffset + kPointerSize;
5884 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005885 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005886
5887 private:
5888 // Bit positions in flag.
5889 static const int kAllCanReadBit = 0;
5890 static const int kAllCanWriteBit = 1;
5891 static const int kProhibitsOverwritingBit = 2;
5892 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
5893
5894 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
5895};
5896
5897
5898class AccessCheckInfo: public Struct {
5899 public:
5900 DECL_ACCESSORS(named_callback, Object)
5901 DECL_ACCESSORS(indexed_callback, Object)
5902 DECL_ACCESSORS(data, Object)
5903
5904 static inline AccessCheckInfo* cast(Object* obj);
5905
Ben Murdochb0fe1622011-05-05 13:52:32 +01005906#ifdef OBJECT_PRINT
5907 inline void AccessCheckInfoPrint() {
5908 AccessCheckInfoPrint(stdout);
5909 }
5910 void AccessCheckInfoPrint(FILE* out);
5911#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005912#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005913 void AccessCheckInfoVerify();
5914#endif
5915
5916 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
5917 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
5918 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
5919 static const int kSize = kDataOffset + kPointerSize;
5920
5921 private:
5922 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
5923};
5924
5925
5926class InterceptorInfo: public Struct {
5927 public:
5928 DECL_ACCESSORS(getter, Object)
5929 DECL_ACCESSORS(setter, Object)
5930 DECL_ACCESSORS(query, Object)
5931 DECL_ACCESSORS(deleter, Object)
5932 DECL_ACCESSORS(enumerator, Object)
5933 DECL_ACCESSORS(data, Object)
5934
5935 static inline InterceptorInfo* cast(Object* obj);
5936
Ben Murdochb0fe1622011-05-05 13:52:32 +01005937#ifdef OBJECT_PRINT
5938 inline void InterceptorInfoPrint() {
5939 InterceptorInfoPrint(stdout);
5940 }
5941 void InterceptorInfoPrint(FILE* out);
5942#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005943#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005944 void InterceptorInfoVerify();
5945#endif
5946
5947 static const int kGetterOffset = HeapObject::kHeaderSize;
5948 static const int kSetterOffset = kGetterOffset + kPointerSize;
5949 static const int kQueryOffset = kSetterOffset + kPointerSize;
5950 static const int kDeleterOffset = kQueryOffset + kPointerSize;
5951 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
5952 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
5953 static const int kSize = kDataOffset + kPointerSize;
5954
5955 private:
5956 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
5957};
5958
5959
5960class CallHandlerInfo: public Struct {
5961 public:
5962 DECL_ACCESSORS(callback, Object)
5963 DECL_ACCESSORS(data, Object)
5964
5965 static inline CallHandlerInfo* cast(Object* obj);
5966
Ben Murdochb0fe1622011-05-05 13:52:32 +01005967#ifdef OBJECT_PRINT
5968 inline void CallHandlerInfoPrint() {
5969 CallHandlerInfoPrint(stdout);
5970 }
5971 void CallHandlerInfoPrint(FILE* out);
5972#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005973#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005974 void CallHandlerInfoVerify();
5975#endif
5976
5977 static const int kCallbackOffset = HeapObject::kHeaderSize;
5978 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005979 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005980
5981 private:
5982 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
5983};
5984
5985
5986class TemplateInfo: public Struct {
5987 public:
5988 DECL_ACCESSORS(tag, Object)
5989 DECL_ACCESSORS(property_list, Object)
5990
5991#ifdef DEBUG
5992 void TemplateInfoVerify();
5993#endif
5994
5995 static const int kTagOffset = HeapObject::kHeaderSize;
5996 static const int kPropertyListOffset = kTagOffset + kPointerSize;
5997 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
5998 protected:
5999 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6000 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6001};
6002
6003
6004class FunctionTemplateInfo: public TemplateInfo {
6005 public:
6006 DECL_ACCESSORS(serial_number, Object)
6007 DECL_ACCESSORS(call_code, Object)
6008 DECL_ACCESSORS(property_accessors, Object)
6009 DECL_ACCESSORS(prototype_template, Object)
6010 DECL_ACCESSORS(parent_template, Object)
6011 DECL_ACCESSORS(named_property_handler, Object)
6012 DECL_ACCESSORS(indexed_property_handler, Object)
6013 DECL_ACCESSORS(instance_template, Object)
6014 DECL_ACCESSORS(class_name, Object)
6015 DECL_ACCESSORS(signature, Object)
6016 DECL_ACCESSORS(instance_call_handler, Object)
6017 DECL_ACCESSORS(access_check_info, Object)
6018 DECL_ACCESSORS(flag, Smi)
6019
6020 // Following properties use flag bits.
6021 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6022 DECL_BOOLEAN_ACCESSORS(undetectable)
6023 // If the bit is set, object instances created by this function
6024 // requires access check.
6025 DECL_BOOLEAN_ACCESSORS(needs_access_check)
6026
6027 static inline FunctionTemplateInfo* cast(Object* obj);
6028
Ben Murdochb0fe1622011-05-05 13:52:32 +01006029#ifdef OBJECT_PRINT
6030 inline void FunctionTemplateInfoPrint() {
6031 FunctionTemplateInfoPrint(stdout);
6032 }
6033 void FunctionTemplateInfoPrint(FILE* out);
6034#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006035#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006036 void FunctionTemplateInfoVerify();
6037#endif
6038
6039 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
6040 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
6041 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
6042 static const int kPrototypeTemplateOffset =
6043 kPropertyAccessorsOffset + kPointerSize;
6044 static const int kParentTemplateOffset =
6045 kPrototypeTemplateOffset + kPointerSize;
6046 static const int kNamedPropertyHandlerOffset =
6047 kParentTemplateOffset + kPointerSize;
6048 static const int kIndexedPropertyHandlerOffset =
6049 kNamedPropertyHandlerOffset + kPointerSize;
6050 static const int kInstanceTemplateOffset =
6051 kIndexedPropertyHandlerOffset + kPointerSize;
6052 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
6053 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
6054 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
6055 static const int kAccessCheckInfoOffset =
6056 kInstanceCallHandlerOffset + kPointerSize;
6057 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
6058 static const int kSize = kFlagOffset + kPointerSize;
6059
6060 private:
6061 // Bit position in the flag, from least significant bit position.
6062 static const int kHiddenPrototypeBit = 0;
6063 static const int kUndetectableBit = 1;
6064 static const int kNeedsAccessCheckBit = 2;
6065
6066 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
6067};
6068
6069
6070class ObjectTemplateInfo: public TemplateInfo {
6071 public:
6072 DECL_ACCESSORS(constructor, Object)
6073 DECL_ACCESSORS(internal_field_count, Object)
6074
6075 static inline ObjectTemplateInfo* cast(Object* obj);
6076
Ben Murdochb0fe1622011-05-05 13:52:32 +01006077#ifdef OBJECT_PRINT
6078 inline void ObjectTemplateInfoPrint() {
6079 ObjectTemplateInfoPrint(stdout);
6080 }
6081 void ObjectTemplateInfoPrint(FILE* out);
6082#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006083#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006084 void ObjectTemplateInfoVerify();
6085#endif
6086
6087 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
6088 static const int kInternalFieldCountOffset =
6089 kConstructorOffset + kPointerSize;
6090 static const int kSize = kInternalFieldCountOffset + kPointerSize;
6091};
6092
6093
6094class SignatureInfo: public Struct {
6095 public:
6096 DECL_ACCESSORS(receiver, Object)
6097 DECL_ACCESSORS(args, Object)
6098
6099 static inline SignatureInfo* cast(Object* obj);
6100
Ben Murdochb0fe1622011-05-05 13:52:32 +01006101#ifdef OBJECT_PRINT
6102 inline void SignatureInfoPrint() {
6103 SignatureInfoPrint(stdout);
6104 }
6105 void SignatureInfoPrint(FILE* out);
6106#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006107#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006108 void SignatureInfoVerify();
6109#endif
6110
6111 static const int kReceiverOffset = Struct::kHeaderSize;
6112 static const int kArgsOffset = kReceiverOffset + kPointerSize;
6113 static const int kSize = kArgsOffset + kPointerSize;
6114
6115 private:
6116 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
6117};
6118
6119
6120class TypeSwitchInfo: public Struct {
6121 public:
6122 DECL_ACCESSORS(types, Object)
6123
6124 static inline TypeSwitchInfo* cast(Object* obj);
6125
Ben Murdochb0fe1622011-05-05 13:52:32 +01006126#ifdef OBJECT_PRINT
6127 inline void TypeSwitchInfoPrint() {
6128 TypeSwitchInfoPrint(stdout);
6129 }
6130 void TypeSwitchInfoPrint(FILE* out);
6131#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006132#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006133 void TypeSwitchInfoVerify();
6134#endif
6135
6136 static const int kTypesOffset = Struct::kHeaderSize;
6137 static const int kSize = kTypesOffset + kPointerSize;
6138};
6139
6140
6141#ifdef ENABLE_DEBUGGER_SUPPORT
6142// The DebugInfo class holds additional information for a function being
6143// debugged.
6144class DebugInfo: public Struct {
6145 public:
6146 // The shared function info for the source being debugged.
6147 DECL_ACCESSORS(shared, SharedFunctionInfo)
6148 // Code object for the original code.
6149 DECL_ACCESSORS(original_code, Code)
6150 // Code object for the patched code. This code object is the code object
6151 // currently active for the function.
6152 DECL_ACCESSORS(code, Code)
6153 // Fixed array holding status information for each active break point.
6154 DECL_ACCESSORS(break_points, FixedArray)
6155
6156 // Check if there is a break point at a code position.
6157 bool HasBreakPoint(int code_position);
6158 // Get the break point info object for a code position.
6159 Object* GetBreakPointInfo(int code_position);
6160 // Clear a break point.
6161 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
6162 int code_position,
6163 Handle<Object> break_point_object);
6164 // Set a break point.
6165 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
6166 int source_position, int statement_position,
6167 Handle<Object> break_point_object);
6168 // Get the break point objects for a code position.
6169 Object* GetBreakPointObjects(int code_position);
6170 // Find the break point info holding this break point object.
6171 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
6172 Handle<Object> break_point_object);
6173 // Get the number of break points for this function.
6174 int GetBreakPointCount();
6175
6176 static inline DebugInfo* cast(Object* obj);
6177
Ben Murdochb0fe1622011-05-05 13:52:32 +01006178#ifdef OBJECT_PRINT
6179 inline void DebugInfoPrint() {
6180 DebugInfoPrint(stdout);
6181 }
6182 void DebugInfoPrint(FILE* out);
6183#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006184#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006185 void DebugInfoVerify();
6186#endif
6187
6188 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
6189 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
6190 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
6191 static const int kActiveBreakPointsCountIndex =
6192 kPatchedCodeIndex + kPointerSize;
6193 static const int kBreakPointsStateIndex =
6194 kActiveBreakPointsCountIndex + kPointerSize;
6195 static const int kSize = kBreakPointsStateIndex + kPointerSize;
6196
6197 private:
6198 static const int kNoBreakPointInfo = -1;
6199
6200 // Lookup the index in the break_points array for a code position.
6201 int GetBreakPointInfoIndex(int code_position);
6202
6203 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
6204};
6205
6206
6207// The BreakPointInfo class holds information for break points set in a
6208// function. The DebugInfo object holds a BreakPointInfo object for each code
6209// position with one or more break points.
6210class BreakPointInfo: public Struct {
6211 public:
6212 // The position in the code for the break point.
6213 DECL_ACCESSORS(code_position, Smi)
6214 // The position in the source for the break position.
6215 DECL_ACCESSORS(source_position, Smi)
6216 // The position in the source for the last statement before this break
6217 // position.
6218 DECL_ACCESSORS(statement_position, Smi)
6219 // List of related JavaScript break points.
6220 DECL_ACCESSORS(break_point_objects, Object)
6221
6222 // Removes a break point.
6223 static void ClearBreakPoint(Handle<BreakPointInfo> info,
6224 Handle<Object> break_point_object);
6225 // Set a break point.
6226 static void SetBreakPoint(Handle<BreakPointInfo> info,
6227 Handle<Object> break_point_object);
6228 // Check if break point info has this break point object.
6229 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
6230 Handle<Object> break_point_object);
6231 // Get the number of break points for this code position.
6232 int GetBreakPointCount();
6233
6234 static inline BreakPointInfo* cast(Object* obj);
6235
Ben Murdochb0fe1622011-05-05 13:52:32 +01006236#ifdef OBJECT_PRINT
6237 inline void BreakPointInfoPrint() {
6238 BreakPointInfoPrint(stdout);
6239 }
6240 void BreakPointInfoPrint(FILE* out);
6241#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006242#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006243 void BreakPointInfoVerify();
6244#endif
6245
6246 static const int kCodePositionIndex = Struct::kHeaderSize;
6247 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
6248 static const int kStatementPositionIndex =
6249 kSourcePositionIndex + kPointerSize;
6250 static const int kBreakPointObjectsIndex =
6251 kStatementPositionIndex + kPointerSize;
6252 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
6253
6254 private:
6255 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
6256};
6257#endif // ENABLE_DEBUGGER_SUPPORT
6258
6259
6260#undef DECL_BOOLEAN_ACCESSORS
6261#undef DECL_ACCESSORS
6262
6263
6264// Abstract base class for visiting, and optionally modifying, the
6265// pointers contained in Objects. Used in GC and serialization/deserialization.
6266class ObjectVisitor BASE_EMBEDDED {
6267 public:
6268 virtual ~ObjectVisitor() {}
6269
6270 // Visits a contiguous arrays of pointers in the half-open range
6271 // [start, end). Any or all of the values may be modified on return.
6272 virtual void VisitPointers(Object** start, Object** end) = 0;
6273
6274 // To allow lazy clearing of inline caches the visitor has
6275 // a rich interface for iterating over Code objects..
6276
6277 // Visits a code target in the instruction stream.
6278 virtual void VisitCodeTarget(RelocInfo* rinfo);
6279
Steve Block791712a2010-08-27 10:21:07 +01006280 // Visits a code entry in a JS function.
6281 virtual void VisitCodeEntry(Address entry_address);
6282
Ben Murdochb0fe1622011-05-05 13:52:32 +01006283 // Visits a global property cell reference in the instruction stream.
6284 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
6285
Steve Blocka7e24c12009-10-30 11:49:00 +00006286 // Visits a runtime entry in the instruction stream.
6287 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
6288
Steve Blockd0582a62009-12-15 09:54:21 +00006289 // Visits the resource of an ASCII or two-byte string.
6290 virtual void VisitExternalAsciiString(
6291 v8::String::ExternalAsciiStringResource** resource) {}
6292 virtual void VisitExternalTwoByteString(
6293 v8::String::ExternalStringResource** resource) {}
6294
Steve Blocka7e24c12009-10-30 11:49:00 +00006295 // Visits a debug call target in the instruction stream.
6296 virtual void VisitDebugTarget(RelocInfo* rinfo);
6297
6298 // Handy shorthand for visiting a single pointer.
6299 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
6300
6301 // Visits a contiguous arrays of external references (references to the C++
6302 // heap) in the half-open range [start, end). Any or all of the values
6303 // may be modified on return.
6304 virtual void VisitExternalReferences(Address* start, Address* end) {}
6305
6306 inline void VisitExternalReference(Address* p) {
6307 VisitExternalReferences(p, p + 1);
6308 }
6309
6310#ifdef DEBUG
6311 // Intended for serialization/deserialization checking: insert, or
6312 // check for the presence of, a tag at this position in the stream.
6313 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00006314#else
6315 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00006316#endif
6317};
6318
6319
Iain Merrick75681382010-08-19 15:07:18 +01006320class StructBodyDescriptor : public
6321 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
6322 public:
6323 static inline int SizeOf(Map* map, HeapObject* object) {
6324 return map->instance_size();
6325 }
6326};
6327
6328
Steve Blocka7e24c12009-10-30 11:49:00 +00006329// BooleanBit is a helper class for setting and getting a bit in an
6330// integer or Smi.
6331class BooleanBit : public AllStatic {
6332 public:
6333 static inline bool get(Smi* smi, int bit_position) {
6334 return get(smi->value(), bit_position);
6335 }
6336
6337 static inline bool get(int value, int bit_position) {
6338 return (value & (1 << bit_position)) != 0;
6339 }
6340
6341 static inline Smi* set(Smi* smi, int bit_position, bool v) {
6342 return Smi::FromInt(set(smi->value(), bit_position, v));
6343 }
6344
6345 static inline int set(int value, int bit_position, bool v) {
6346 if (v) {
6347 value |= (1 << bit_position);
6348 } else {
6349 value &= ~(1 << bit_position);
6350 }
6351 return value;
6352 }
6353};
6354
6355} } // namespace v8::internal
6356
6357#endif // V8_OBJECTS_H_