blob: f9cab45fb1058608f2ae02aa26dcccc0c165be14 [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
Ben Murdochb8e0da22011-05-16 14:20:40 +0100627
628#define OBJECT_TYPE_LIST(V) \
629 V(Smi) \
630 V(HeapObject) \
631 V(Number) \
632
633#define HEAP_OBJECT_TYPE_LIST(V) \
634 V(HeapNumber) \
635 V(String) \
636 V(Symbol) \
637 V(SeqString) \
638 V(ExternalString) \
639 V(ConsString) \
640 V(ExternalTwoByteString) \
641 V(ExternalAsciiString) \
642 V(SeqTwoByteString) \
643 V(SeqAsciiString) \
644 \
645 V(PixelArray) \
646 V(ExternalArray) \
647 V(ExternalByteArray) \
648 V(ExternalUnsignedByteArray) \
649 V(ExternalShortArray) \
650 V(ExternalUnsignedShortArray) \
651 V(ExternalIntArray) \
652 V(ExternalUnsignedIntArray) \
653 V(ExternalFloatArray) \
654 V(ByteArray) \
655 V(JSObject) \
656 V(JSContextExtensionObject) \
657 V(Map) \
658 V(DescriptorArray) \
659 V(DeoptimizationInputData) \
660 V(DeoptimizationOutputData) \
661 V(FixedArray) \
662 V(Context) \
663 V(CatchContext) \
664 V(GlobalContext) \
665 V(JSFunction) \
666 V(Code) \
667 V(Oddball) \
668 V(SharedFunctionInfo) \
669 V(JSValue) \
670 V(StringWrapper) \
671 V(Proxy) \
672 V(Boolean) \
673 V(JSArray) \
674 V(JSRegExp) \
675 V(HashTable) \
676 V(Dictionary) \
677 V(SymbolTable) \
678 V(JSFunctionResultCache) \
679 V(NormalizedMapCache) \
680 V(CompilationCacheTable) \
681 V(CodeCacheHashTable) \
682 V(MapCache) \
683 V(Primitive) \
684 V(GlobalObject) \
685 V(JSGlobalObject) \
686 V(JSBuiltinsObject) \
687 V(JSGlobalProxy) \
688 V(UndetectableObject) \
689 V(AccessCheckNeeded) \
690 V(JSGlobalPropertyCell) \
691
Steve Blocka7e24c12009-10-30 11:49:00 +0000692// Object is the abstract superclass for all classes in the
693// object hierarchy.
694// Object does not use any virtual functions to avoid the
695// allocation of the C++ vtable.
696// Since Smi and Failure are subclasses of Object no
697// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700698class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000699 public:
700 // Type testing.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100701#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
702 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
703 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
704#undef IS_TYPE_FUNCTION_DECL
Steve Blocka7e24c12009-10-30 11:49:00 +0000705
706 // Returns true if this object is an instance of the specified
707 // function template.
708 inline bool IsInstanceOf(FunctionTemplateInfo* type);
709
710 inline bool IsStruct();
711#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
712 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
713#undef DECLARE_STRUCT_PREDICATE
714
715 // Oddball testing.
716 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000717 INLINE(bool IsNull());
718 INLINE(bool IsTrue());
719 INLINE(bool IsFalse());
Ben Murdoch086aeea2011-05-13 15:57:08 +0100720 inline bool IsArgumentsMarker();
Steve Blocka7e24c12009-10-30 11:49:00 +0000721
722 // Extract the number.
723 inline double Number();
724
725 inline bool HasSpecificClassOf(String* name);
726
John Reck59135872010-11-02 12:39:01 -0700727 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
728 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000729
730 // Convert to a JSObject if needed.
731 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700732 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000733
734 // Converts this to a Smi if possible.
735 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700736 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000737
738 void Lookup(String* name, LookupResult* result);
739
740 // Property access.
John Reck59135872010-11-02 12:39:01 -0700741 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
742 MUST_USE_RESULT inline MaybeObject* GetProperty(
743 String* key,
744 PropertyAttributes* attributes);
745 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
746 Object* receiver,
747 String* key,
748 PropertyAttributes* attributes);
749 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
750 LookupResult* result,
751 String* key,
752 PropertyAttributes* attributes);
753 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
754 Object* structure,
755 String* name,
756 Object* holder);
757 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
758 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000759
John Reck59135872010-11-02 12:39:01 -0700760 inline MaybeObject* GetElement(uint32_t index);
761 // For use when we know that no exception can be thrown.
762 inline Object* GetElementNoExceptionThrown(uint32_t index);
763 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000764
765 // Return the object's prototype (might be Heap::null_value()).
766 Object* GetPrototype();
767
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100768 // Tries to convert an object to an array index. Returns true and sets
769 // the output parameter if it succeeds.
770 inline bool ToArrayIndex(uint32_t* index);
771
Steve Blocka7e24c12009-10-30 11:49:00 +0000772 // Returns true if this is a JSValue containing a string and the index is
773 // < the length of the string. Used to implement [] on strings.
774 inline bool IsStringObjectWithCharacterAt(uint32_t index);
775
776#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000777 // Verify a pointer is a valid object pointer.
778 static void VerifyPointer(Object* p);
779#endif
780
781 // Prints this object without details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100782 inline void ShortPrint() {
783 ShortPrint(stdout);
784 }
785 void ShortPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000786
787 // Prints this object without details to a message accumulator.
788 void ShortPrint(StringStream* accumulator);
789
790 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
791 static Object* cast(Object* value) { return value; }
792
793 // Layout description.
794 static const int kHeaderSize = 0; // Object does not take up any space.
795
796 private:
797 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
798};
799
800
801// Smi represents integer Numbers that can be stored in 31 bits.
802// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000803// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000804// For long smis it has the following format:
805// [32 bit signed int] [31 bits zero padding] 0
806// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000807class Smi: public Object {
808 public:
809 // Returns the integer value.
810 inline int value();
811
812 // Convert a value to a Smi object.
813 static inline Smi* FromInt(int value);
814
815 static inline Smi* FromIntptr(intptr_t value);
816
817 // Returns whether value can be represented in a Smi.
818 static inline bool IsValid(intptr_t value);
819
Steve Blocka7e24c12009-10-30 11:49:00 +0000820 // Casting.
821 static inline Smi* cast(Object* object);
822
823 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100824 inline void SmiPrint() {
825 SmiPrint(stdout);
826 }
827 void SmiPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 void SmiPrint(StringStream* accumulator);
829#ifdef DEBUG
830 void SmiVerify();
831#endif
832
Steve Block3ce2e202009-11-05 08:53:23 +0000833 static const int kMinValue = (-1 << (kSmiValueSize - 1));
834 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000835
836 private:
837 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
838};
839
840
841// Failure is used for reporting out of memory situations and
842// propagating exceptions through the runtime system. Failure objects
843// are transient and cannot occur as part of the object graph.
844//
845// Failures are a single word, encoded as follows:
846// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +0100847// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000848// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000849// 7 6 4 32 10
850//
Steve Blocka7e24c12009-10-30 11:49:00 +0000851//
852// The low two bits, 0-1, are the failure tag, 11. The next two bits,
853// 2-3, are a failure type tag 'tt' with possible values:
854// 00 RETRY_AFTER_GC
855// 01 EXCEPTION
856// 10 INTERNAL_ERROR
857// 11 OUT_OF_MEMORY_EXCEPTION
858//
859// The next three bits, 4-6, are an allocation space tag 'sss'. The
860// allocation space tag is 000 for all failure types except
861// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
862// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +0000863
864// Failure type tag info.
865const int kFailureTypeTagSize = 2;
866const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
867
John Reck59135872010-11-02 12:39:01 -0700868class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000869 public:
870 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
871 enum Type {
872 RETRY_AFTER_GC = 0,
873 EXCEPTION = 1, // Returning this marker tells the real exception
874 // is in Top::pending_exception.
875 INTERNAL_ERROR = 2,
876 OUT_OF_MEMORY_EXCEPTION = 3
877 };
878
879 inline Type type() const;
880
881 // Returns the space that needs to be collected for RetryAfterGC failures.
882 inline AllocationSpace allocation_space() const;
883
Steve Blocka7e24c12009-10-30 11:49:00 +0000884 inline bool IsInternalError() const;
885 inline bool IsOutOfMemoryException() const;
886
Ben Murdochf87a2032010-10-22 12:50:53 +0100887 static inline Failure* RetryAfterGC(AllocationSpace space);
888 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000889 static inline Failure* Exception();
890 static inline Failure* InternalError();
891 static inline Failure* OutOfMemoryException();
892 // Casting.
John Reck59135872010-11-02 12:39:01 -0700893 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000894
895 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100896 inline void FailurePrint() {
897 FailurePrint(stdout);
898 }
899 void FailurePrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000900 void FailurePrint(StringStream* accumulator);
901#ifdef DEBUG
902 void FailureVerify();
903#endif
904
905 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000906 inline intptr_t value() const;
907 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000908
909 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
910};
911
912
913// Heap objects typically have a map pointer in their first word. However,
914// during GC other data (eg, mark bits, forwarding addresses) is sometimes
915// encoded in the first word. The class MapWord is an abstraction of the
916// value in a heap object's first word.
917class MapWord BASE_EMBEDDED {
918 public:
919 // Normal state: the map word contains a map pointer.
920
921 // Create a map word from a map pointer.
922 static inline MapWord FromMap(Map* map);
923
924 // View this map word as a map pointer.
925 inline Map* ToMap();
926
927
928 // Scavenge collection: the map word of live objects in the from space
929 // contains a forwarding address (a heap object pointer in the to space).
930
931 // True if this map word is a forwarding address for a scavenge
932 // collection. Only valid during a scavenge collection (specifically,
933 // when all map words are heap object pointers, ie. not during a full GC).
934 inline bool IsForwardingAddress();
935
936 // Create a map word from a forwarding address.
937 static inline MapWord FromForwardingAddress(HeapObject* object);
938
939 // View this map word as a forwarding address.
940 inline HeapObject* ToForwardingAddress();
941
Steve Blocka7e24c12009-10-30 11:49:00 +0000942 // Marking phase of full collection: the map word of live objects is
943 // marked, and may be marked as overflowed (eg, the object is live, its
944 // children have not been visited, and it does not fit in the marking
945 // stack).
946
947 // True if this map word's mark bit is set.
948 inline bool IsMarked();
949
950 // Return this map word but with its mark bit set.
951 inline void SetMark();
952
953 // Return this map word but with its mark bit cleared.
954 inline void ClearMark();
955
956 // True if this map word's overflow bit is set.
957 inline bool IsOverflowed();
958
959 // Return this map word but with its overflow bit set.
960 inline void SetOverflow();
961
962 // Return this map word but with its overflow bit cleared.
963 inline void ClearOverflow();
964
965
966 // Compacting phase of a full compacting collection: the map word of live
967 // objects contains an encoding of the original map address along with the
968 // forwarding address (represented as an offset from the first live object
969 // in the same page as the (old) object address).
970
971 // Create a map word from a map address and a forwarding address offset.
972 static inline MapWord EncodeAddress(Address map_address, int offset);
973
974 // Return the map address encoded in this map word.
975 inline Address DecodeMapAddress(MapSpace* map_space);
976
977 // Return the forwarding offset encoded in this map word.
978 inline int DecodeOffset();
979
980
981 // During serialization: the map word is used to hold an encoded
982 // address, and possibly a mark bit (set and cleared with SetMark
983 // and ClearMark).
984
985 // Create a map word from an encoded address.
986 static inline MapWord FromEncodedAddress(Address address);
987
988 inline Address ToEncodedAddress();
989
990 // Bits used by the marking phase of the garbage collector.
991 //
992 // The first word of a heap object is normally a map pointer. The last two
993 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
994 // mark an object as live and/or overflowed:
995 // last bit = 0, marked as alive
996 // second bit = 1, overflowed
997 // An object is only marked as overflowed when it is marked as live while
998 // the marking stack is overflowed.
999 static const int kMarkingBit = 0; // marking bit
1000 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1001 static const int kOverflowBit = 1; // overflow bit
1002 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1003
Leon Clarkee46be812010-01-19 14:06:41 +00001004 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1005 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +00001006 // +-----------------+------------------+-----------------+
1007 // |forwarding offset|page offset of map|page index of map|
1008 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +00001009 // ^ ^ ^
1010 // | | |
1011 // | | kMapPageIndexBits
1012 // | kMapPageOffsetBits
1013 // kForwardingOffsetBits
1014 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1015 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1016#ifdef V8_HOST_ARCH_64_BIT
1017 static const int kMapPageIndexBits = 16;
1018#else
1019 // Use all the 32-bits to encode on a 32-bit platform.
1020 static const int kMapPageIndexBits =
1021 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1022#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001023
1024 static const int kMapPageIndexShift = 0;
1025 static const int kMapPageOffsetShift =
1026 kMapPageIndexShift + kMapPageIndexBits;
1027 static const int kForwardingOffsetShift =
1028 kMapPageOffsetShift + kMapPageOffsetBits;
1029
Leon Clarkee46be812010-01-19 14:06:41 +00001030 // Bit masks covering the different parts the encoding.
1031 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001032 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001033 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001034 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001035 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001036 ~(kMapPageIndexMask | kMapPageOffsetMask);
1037
1038 private:
1039 // HeapObject calls the private constructor and directly reads the value.
1040 friend class HeapObject;
1041
1042 explicit MapWord(uintptr_t value) : value_(value) {}
1043
1044 uintptr_t value_;
1045};
1046
1047
1048// HeapObject is the superclass for all classes describing heap allocated
1049// objects.
1050class HeapObject: public Object {
1051 public:
1052 // [map]: Contains a map which contains the object's reflective
1053 // information.
1054 inline Map* map();
1055 inline void set_map(Map* value);
1056
1057 // During garbage collection, the map word of a heap object does not
1058 // necessarily contain a map pointer.
1059 inline MapWord map_word();
1060 inline void set_map_word(MapWord map_word);
1061
1062 // Converts an address to a HeapObject pointer.
1063 static inline HeapObject* FromAddress(Address address);
1064
1065 // Returns the address of this HeapObject.
1066 inline Address address();
1067
1068 // Iterates over pointers contained in the object (including the Map)
1069 void Iterate(ObjectVisitor* v);
1070
1071 // Iterates over all pointers contained in the object except the
1072 // first map pointer. The object type is given in the first
1073 // parameter. This function does not access the map pointer in the
1074 // object, and so is safe to call while the map pointer is modified.
1075 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1076
Steve Blocka7e24c12009-10-30 11:49:00 +00001077 // Returns the heap object's size in bytes
1078 inline int Size();
1079
1080 // Given a heap object's map pointer, returns the heap size in bytes
1081 // Useful when the map pointer field is used for other purposes.
1082 // GC internal.
1083 inline int SizeFromMap(Map* map);
1084
1085 // Support for the marking heap objects during the marking phase of GC.
1086 // True if the object is marked live.
1087 inline bool IsMarked();
1088
1089 // Mutate this object's map pointer to indicate that the object is live.
1090 inline void SetMark();
1091
1092 // Mutate this object's map pointer to remove the indication that the
1093 // object is live (ie, partially restore the map pointer).
1094 inline void ClearMark();
1095
1096 // True if this object is marked as overflowed. Overflowed objects have
1097 // been reached and marked during marking of the heap, but their children
1098 // have not necessarily been marked and they have not been pushed on the
1099 // marking stack.
1100 inline bool IsOverflowed();
1101
1102 // Mutate this object's map pointer to indicate that the object is
1103 // overflowed.
1104 inline void SetOverflow();
1105
1106 // Mutate this object's map pointer to remove the indication that the
1107 // object is overflowed (ie, partially restore the map pointer).
1108 inline void ClearOverflow();
1109
1110 // Returns the field at offset in obj, as a read/write Object* reference.
1111 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001112 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 // during marking GC.
1114 static inline Object** RawField(HeapObject* obj, int offset);
1115
1116 // Casting.
1117 static inline HeapObject* cast(Object* obj);
1118
Leon Clarke4515c472010-02-03 11:58:03 +00001119 // Return the write barrier mode for this. Callers of this function
1120 // must be able to present a reference to an AssertNoAllocation
1121 // object as a sign that they are not going to use this function
1122 // from code that allocates and thus invalidates the returned write
1123 // barrier mode.
1124 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001125
1126 // Dispatched behavior.
1127 void HeapObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001128#ifdef OBJECT_PRINT
1129 inline void HeapObjectPrint() {
1130 HeapObjectPrint(stdout);
1131 }
1132 void HeapObjectPrint(FILE* out);
1133#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001134#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001135 void HeapObjectVerify();
1136 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001137 inline void VerifySmiField(int offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001138#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001139
Ben Murdochb0fe1622011-05-05 13:52:32 +01001140#ifdef OBJECT_PRINT
1141 void PrintHeader(FILE* out, const char* id);
1142#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001143
Ben Murdochb0fe1622011-05-05 13:52:32 +01001144#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001145 // Verify a pointer is a valid HeapObject pointer that points to object
1146 // areas in the heap.
1147 static void VerifyHeapPointer(Object* p);
1148#endif
1149
1150 // Layout description.
1151 // First field in a heap object is map.
1152 static const int kMapOffset = Object::kHeaderSize;
1153 static const int kHeaderSize = kMapOffset + kPointerSize;
1154
1155 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1156
1157 protected:
1158 // helpers for calling an ObjectVisitor to iterate over pointers in the
1159 // half-open range [start, end) specified as integer offsets
1160 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1161 // as above, for the single element at "offset"
1162 inline void IteratePointer(ObjectVisitor* v, int offset);
1163
Steve Blocka7e24c12009-10-30 11:49:00 +00001164 private:
1165 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1166};
1167
1168
Iain Merrick75681382010-08-19 15:07:18 +01001169#define SLOT_ADDR(obj, offset) \
1170 reinterpret_cast<Object**>((obj)->address() + offset)
1171
1172// This class describes a body of an object of a fixed size
1173// in which all pointer fields are located in the [start_offset, end_offset)
1174// interval.
1175template<int start_offset, int end_offset, int size>
1176class FixedBodyDescriptor {
1177 public:
1178 static const int kStartOffset = start_offset;
1179 static const int kEndOffset = end_offset;
1180 static const int kSize = size;
1181
1182 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1183
1184 template<typename StaticVisitor>
1185 static inline void IterateBody(HeapObject* obj) {
1186 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1187 SLOT_ADDR(obj, end_offset));
1188 }
1189};
1190
1191
1192// This class describes a body of an object of a variable size
1193// in which all pointer fields are located in the [start_offset, object_size)
1194// interval.
1195template<int start_offset>
1196class FlexibleBodyDescriptor {
1197 public:
1198 static const int kStartOffset = start_offset;
1199
1200 static inline void IterateBody(HeapObject* obj,
1201 int object_size,
1202 ObjectVisitor* v);
1203
1204 template<typename StaticVisitor>
1205 static inline void IterateBody(HeapObject* obj, int object_size) {
1206 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1207 SLOT_ADDR(obj, object_size));
1208 }
1209};
1210
1211#undef SLOT_ADDR
1212
1213
Steve Blocka7e24c12009-10-30 11:49:00 +00001214// The HeapNumber class describes heap allocated numbers that cannot be
1215// represented in a Smi (small integer)
1216class HeapNumber: public HeapObject {
1217 public:
1218 // [value]: number value.
1219 inline double value();
1220 inline void set_value(double value);
1221
1222 // Casting.
1223 static inline HeapNumber* cast(Object* obj);
1224
1225 // Dispatched behavior.
1226 Object* HeapNumberToBoolean();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001227 inline void HeapNumberPrint() {
1228 HeapNumberPrint(stdout);
1229 }
1230 void HeapNumberPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001231 void HeapNumberPrint(StringStream* accumulator);
1232#ifdef DEBUG
1233 void HeapNumberVerify();
1234#endif
1235
Steve Block6ded16b2010-05-10 14:33:55 +01001236 inline int get_exponent();
1237 inline int get_sign();
1238
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 // Layout description.
1240 static const int kValueOffset = HeapObject::kHeaderSize;
1241 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1242 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1243 // little endian apart from non-EABI arm which is little endian with big
1244 // endian floating point word ordering!
Steve Block3ce2e202009-11-05 08:53:23 +00001245#if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI)
Steve Blocka7e24c12009-10-30 11:49:00 +00001246 static const int kMantissaOffset = kValueOffset;
1247 static const int kExponentOffset = kValueOffset + 4;
1248#else
1249 static const int kMantissaOffset = kValueOffset + 4;
1250 static const int kExponentOffset = kValueOffset;
1251# define BIG_ENDIAN_FLOATING_POINT 1
1252#endif
1253 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001254 static const uint32_t kSignMask = 0x80000000u;
1255 static const uint32_t kExponentMask = 0x7ff00000u;
1256 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001257 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001258 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001259 static const int kExponentBias = 1023;
1260 static const int kExponentShift = 20;
1261 static const int kMantissaBitsInTopWord = 20;
1262 static const int kNonMantissaBitsInTopWord = 12;
1263
1264 private:
1265 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1266};
1267
1268
1269// The JSObject describes real heap allocated JavaScript objects with
1270// properties.
1271// Note that the map of JSObject changes during execution to enable inline
1272// caching.
1273class JSObject: public HeapObject {
1274 public:
1275 enum DeleteMode { NORMAL_DELETION, FORCE_DELETION };
1276 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001277 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001278 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001279 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001280 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001281 PIXEL_ELEMENTS,
1282 EXTERNAL_BYTE_ELEMENTS,
1283 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1284 EXTERNAL_SHORT_ELEMENTS,
1285 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1286 EXTERNAL_INT_ELEMENTS,
1287 EXTERNAL_UNSIGNED_INT_ELEMENTS,
1288 EXTERNAL_FLOAT_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001289 };
1290
1291 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001292 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001293 // slow case.
1294 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1295 inline void initialize_properties();
1296 inline bool HasFastProperties();
1297 inline StringDictionary* property_dictionary(); // Gets slow properties.
1298
1299 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001300 //
1301 // Elements can be in two general modes: fast and slow. Each mode
1302 // corrensponds to a set of object representations of elements that
1303 // have something in common.
1304 //
1305 // In the fast mode elements is a FixedArray and so each element can
1306 // be quickly accessed. This fact is used in the generated code. The
1307 // elements array can have one of the two maps in this mode:
1308 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1309 // arrays). In the latter case the elements array may be shared by a
1310 // few objects and so before writing to any element the array must
1311 // be copied. Use EnsureWritableFastElements in this case.
1312 //
1313 // In the slow mode elements is either a NumberDictionary or a
1314 // PixelArray or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001315 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001316 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001317 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001318 inline ElementsKind GetElementsKind();
1319 inline bool HasFastElements();
1320 inline bool HasDictionaryElements();
1321 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001322 inline bool HasExternalArrayElements();
1323 inline bool HasExternalByteElements();
1324 inline bool HasExternalUnsignedByteElements();
1325 inline bool HasExternalShortElements();
1326 inline bool HasExternalUnsignedShortElements();
1327 inline bool HasExternalIntElements();
1328 inline bool HasExternalUnsignedIntElements();
1329 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001330 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001331 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001332 // Requires: this->HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001333 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001334
1335 // Collects elements starting at index 0.
1336 // Undefined values are placed after non-undefined values.
1337 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001338 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 // As PrepareElementsForSort, but only on objects where elements is
1340 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001341 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001342
John Reck59135872010-11-02 12:39:01 -07001343 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1344 Object* value,
1345 PropertyAttributes attributes);
1346 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1347 String* key,
1348 Object* value,
1349 PropertyAttributes attributes);
1350 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1351 LookupResult* result,
1352 String* name,
Ben Murdoch086aeea2011-05-13 15:57:08 +01001353 Object* value,
1354 bool check_prototype);
John Reck59135872010-11-02 12:39:01 -07001355 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(Object* structure,
1356 String* name,
1357 Object* value,
1358 JSObject* holder);
1359 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1360 Object* value);
1361 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1362 String* name,
1363 Object* value,
1364 PropertyAttributes attributes);
1365 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1366 String* name,
1367 Object* value,
1368 PropertyAttributes attributes);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001369 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
John Reck59135872010-11-02 12:39:01 -07001370 String* key,
1371 Object* value,
1372 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001373
1374 // Retrieve a value in a normalized object given a lookup result.
1375 // Handles the special representation of JS global objects.
1376 Object* GetNormalizedProperty(LookupResult* result);
1377
1378 // Sets the property value in a normalized object given a lookup result.
1379 // Handles the special representation of JS global objects.
1380 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1381
1382 // Sets the property value in a normalized object given (key, value, details).
1383 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001384 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1385 Object* value,
1386 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001387
1388 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001389 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1390 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001391
Steve Blocka7e24c12009-10-30 11:49:00 +00001392 // Returns the class name ([[Class]] property in the specification).
1393 String* class_name();
1394
1395 // Returns the constructor name (the name (possibly, inferred name) of the
1396 // function that was used to instantiate the object).
1397 String* constructor_name();
1398
1399 // Retrieve interceptors.
1400 InterceptorInfo* GetNamedInterceptor();
1401 InterceptorInfo* GetIndexedInterceptor();
1402
1403 inline PropertyAttributes GetPropertyAttribute(String* name);
1404 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1405 String* name);
1406 PropertyAttributes GetLocalPropertyAttribute(String* name);
1407
John Reck59135872010-11-02 12:39:01 -07001408 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1409 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001410 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001411 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001412 Object* LookupAccessor(String* name, bool is_getter);
1413
John Reck59135872010-11-02 12:39:01 -07001414 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001415
Steve Blocka7e24c12009-10-30 11:49:00 +00001416 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001417 MaybeObject* GetPropertyWithFailedAccessCheck(
1418 Object* receiver,
1419 LookupResult* result,
1420 String* name,
1421 PropertyAttributes* attributes);
1422 MaybeObject* GetPropertyWithInterceptor(
1423 JSObject* receiver,
1424 String* name,
1425 PropertyAttributes* attributes);
1426 MaybeObject* GetPropertyPostInterceptor(
1427 JSObject* receiver,
1428 String* name,
1429 PropertyAttributes* attributes);
1430 MaybeObject* GetLocalPropertyPostInterceptor(JSObject* receiver,
1431 String* name,
1432 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001433
1434 // Returns true if this is an instance of an api function and has
1435 // been modified since it was created. May give false positives.
1436 bool IsDirty();
1437
1438 bool HasProperty(String* name) {
1439 return GetPropertyAttribute(name) != ABSENT;
1440 }
1441
1442 // Can cause a GC if it hits an interceptor.
1443 bool HasLocalProperty(String* name) {
1444 return GetLocalPropertyAttribute(name) != ABSENT;
1445 }
1446
Steve Blockd0582a62009-12-15 09:54:21 +00001447 // If the receiver is a JSGlobalProxy this method will return its prototype,
1448 // otherwise the result is the receiver itself.
1449 inline Object* BypassGlobalProxy();
1450
1451 // Accessors for hidden properties object.
1452 //
1453 // Hidden properties are not local properties of the object itself.
1454 // Instead they are stored on an auxiliary JSObject stored as a local
1455 // property with a special name Heap::hidden_symbol(). But if the
1456 // receiver is a JSGlobalProxy then the auxiliary object is a property
1457 // of its prototype.
1458 //
1459 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1460 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1461 // holder.
1462 //
1463 // These accessors do not touch interceptors or accessors.
1464 inline bool HasHiddenPropertiesObject();
1465 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001466 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1467 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001468
John Reck59135872010-11-02 12:39:01 -07001469 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1470 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001471
1472 // Tests for the fast common case for property enumeration.
1473 bool IsSimpleEnum();
1474
1475 // Do we want to keep the elements in fast case when increasing the
1476 // capacity?
1477 bool ShouldConvertToSlowElements(int new_capacity);
1478 // Returns true if the backing storage for the slow-case elements of
1479 // this object takes up nearly as much space as a fast-case backing
1480 // storage would. In that case the JSObject should have fast
1481 // elements.
1482 bool ShouldConvertToFastElements();
1483
1484 // Return the object's prototype (might be Heap::null_value()).
1485 inline Object* GetPrototype();
1486
Andrei Popescu402d9372010-02-26 13:31:12 +00001487 // Set the object's prototype (only JSObject and null are allowed).
John Reck59135872010-11-02 12:39:01 -07001488 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1489 bool skip_hidden_prototypes);
Andrei Popescu402d9372010-02-26 13:31:12 +00001490
Steve Blocka7e24c12009-10-30 11:49:00 +00001491 // Tells whether the index'th element is present.
1492 inline bool HasElement(uint32_t index);
1493 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001494
1495 // Tells whether the index'th element is present and how it is stored.
1496 enum LocalElementType {
1497 // There is no element with given index.
1498 UNDEFINED_ELEMENT,
1499
1500 // Element with given index is handled by interceptor.
1501 INTERCEPTED_ELEMENT,
1502
1503 // Element with given index is character in string.
1504 STRING_CHARACTER_ELEMENT,
1505
1506 // Element with given index is stored in fast backing store.
1507 FAST_ELEMENT,
1508
1509 // Element with given index is stored in slow backing store.
1510 DICTIONARY_ELEMENT
1511 };
1512
1513 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001514
1515 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1516 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1517
Steve Block9fac8402011-05-12 15:51:54 +01001518 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1519 Object* value,
1520 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001521
1522 // Set the index'th array element.
1523 // A Failure object is returned if GC is needed.
Steve Block9fac8402011-05-12 15:51:54 +01001524 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1525 Object* value,
1526 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001527
1528 // Returns the index'th element.
1529 // The undefined object if index is out of bounds.
John Reck59135872010-11-02 12:39:01 -07001530 MaybeObject* GetElementWithReceiver(JSObject* receiver, uint32_t index);
1531 MaybeObject* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001532
John Reck59135872010-11-02 12:39:01 -07001533 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1534 int length);
1535 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001536
1537 // Lookup interceptors are used for handling properties controlled by host
1538 // objects.
1539 inline bool HasNamedInterceptor();
1540 inline bool HasIndexedInterceptor();
1541
1542 // Support functions for v8 api (needed for correct interceptor behavior).
1543 bool HasRealNamedProperty(String* key);
1544 bool HasRealElementProperty(uint32_t index);
1545 bool HasRealNamedCallbackProperty(String* key);
1546
1547 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001548 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001549
1550 // Get the header size for a JSObject. Used to compute the index of
1551 // internal fields as well as the number of internal fields.
1552 inline int GetHeaderSize();
1553
1554 inline int GetInternalFieldCount();
1555 inline Object* GetInternalField(int index);
1556 inline void SetInternalField(int index, Object* value);
1557
1558 // Lookup a property. If found, the result is valid and has
1559 // detailed information.
1560 void LocalLookup(String* name, LookupResult* result);
1561 void Lookup(String* name, LookupResult* result);
1562
1563 // The following lookup functions skip interceptors.
1564 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1565 void LookupRealNamedProperty(String* name, LookupResult* result);
1566 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1567 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Leon Clarkef7060e22010-06-03 12:02:55 +01001568 bool SetElementWithCallbackSetterInPrototypes(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001569 void LookupCallback(String* name, LookupResult* result);
1570
1571 // Returns the number of properties on this object filtering out properties
1572 // with the specified attributes (ignoring interceptors).
1573 int NumberOfLocalProperties(PropertyAttributes filter);
1574 // Returns the number of enumerable properties (ignoring interceptors).
1575 int NumberOfEnumProperties();
1576 // Fill in details for properties into storage starting at the specified
1577 // index.
1578 void GetLocalPropertyNames(FixedArray* storage, int index);
1579
1580 // Returns the number of properties on this object filtering out properties
1581 // with the specified attributes (ignoring interceptors).
1582 int NumberOfLocalElements(PropertyAttributes filter);
1583 // Returns the number of enumerable elements (ignoring interceptors).
1584 int NumberOfEnumElements();
1585 // Returns the number of elements on this object filtering out elements
1586 // with the specified attributes (ignoring interceptors).
1587 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1588 // Count and fill in the enumerable elements into storage.
1589 // (storage->length() == NumberOfEnumElements()).
1590 // If storage is NULL, will count the elements without adding
1591 // them to any storage.
1592 // Returns the number of enumerable elements.
1593 int GetEnumElementKeys(FixedArray* storage);
1594
1595 // Add a property to a fast-case object using a map transition to
1596 // new_map.
John Reck59135872010-11-02 12:39:01 -07001597 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1598 String* name,
1599 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001600
1601 // Add a constant function property to a fast-case object.
1602 // This leaves a CONSTANT_TRANSITION in the old map, and
1603 // if it is called on a second object with this map, a
1604 // normal property is added instead, with a map transition.
1605 // This avoids the creation of many maps with the same constant
1606 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001607 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1608 String* name,
1609 JSFunction* function,
1610 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001611
John Reck59135872010-11-02 12:39:01 -07001612 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1613 String* name,
1614 Object* value,
1615 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001616
1617 // Converts a descriptor of any other type to a real field,
1618 // backed by the properties array. Descriptors of visible
1619 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1620 // Converts the descriptor on the original object's map to a
1621 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001622 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001623 String* name,
1624 Object* new_value,
1625 PropertyAttributes attributes);
1626
1627 // Converts a descriptor of any other type to a real field,
1628 // backed by the properties array. Descriptors of visible
1629 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001630 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1631 String* name,
1632 Object* new_value,
1633 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001634
1635 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001636 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1637 Object* value,
1638 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001639
1640 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001641 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1642 Object* value,
1643 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001644
1645 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001646 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1647 Object* value,
1648 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001649
1650 // Convert the object to use the canonical dictionary
1651 // representation. If the object is expected to have additional properties
1652 // added this number can be indicated to have the backing store allocated to
1653 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001654 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1655 PropertyNormalizationMode mode,
1656 int expected_additional_properties);
1657 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001658
John Reck59135872010-11-02 12:39:01 -07001659 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001660
Steve Blocka7e24c12009-10-30 11:49:00 +00001661 // Transform slow named properties to fast variants.
1662 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001663 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1664 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001665
1666 // Access fast-case object properties at index.
1667 inline Object* FastPropertyAt(int index);
1668 inline Object* FastPropertyAtPut(int index, Object* value);
1669
1670 // Access to in object properties.
1671 inline Object* InObjectPropertyAt(int index);
1672 inline Object* InObjectPropertyAtPut(int index,
1673 Object* value,
1674 WriteBarrierMode mode
1675 = UPDATE_WRITE_BARRIER);
1676
1677 // initializes the body after properties slot, properties slot is
1678 // initialized by set_properties
1679 // Note: this call does not update write barrier, it is caller's
1680 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001681 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001682
1683 // Check whether this object references another object
1684 bool ReferencesObject(Object* obj);
1685
1686 // Casting.
1687 static inline JSObject* cast(Object* obj);
1688
Steve Block8defd9f2010-07-08 12:39:36 +01001689 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001690 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001691
1692
Steve Blocka7e24c12009-10-30 11:49:00 +00001693 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001694 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001695#ifdef OBJECT_PRINT
1696 inline void JSObjectPrint() {
1697 JSObjectPrint(stdout);
1698 }
1699 void JSObjectPrint(FILE* out);
1700#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001701#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001702 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001703#endif
1704#ifdef OBJECT_PRINT
1705 inline void PrintProperties() {
1706 PrintProperties(stdout);
1707 }
1708 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001709
Ben Murdochb0fe1622011-05-05 13:52:32 +01001710 inline void PrintElements() {
1711 PrintElements(stdout);
1712 }
1713 void PrintElements(FILE* out);
1714#endif
1715
1716#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001717 // Structure for collecting spill information about JSObjects.
1718 class SpillInformation {
1719 public:
1720 void Clear();
1721 void Print();
1722 int number_of_objects_;
1723 int number_of_objects_with_fast_properties_;
1724 int number_of_objects_with_fast_elements_;
1725 int number_of_fast_used_fields_;
1726 int number_of_fast_unused_fields_;
1727 int number_of_slow_used_properties_;
1728 int number_of_slow_unused_properties_;
1729 int number_of_fast_used_elements_;
1730 int number_of_fast_unused_elements_;
1731 int number_of_slow_used_elements_;
1732 int number_of_slow_unused_elements_;
1733 };
1734
1735 void IncrementSpillStatistics(SpillInformation* info);
1736#endif
1737 Object* SlowReverseLookup(Object* value);
1738
Steve Block8defd9f2010-07-08 12:39:36 +01001739 // Maximal number of fast properties for the JSObject. Used to
1740 // restrict the number of map transitions to avoid an explosion in
1741 // the number of maps for objects used as dictionaries.
1742 inline int MaxFastProperties();
1743
Leon Clarkee46be812010-01-19 14:06:41 +00001744 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1745 // Also maximal value of JSArray's length property.
1746 static const uint32_t kMaxElementCount = 0xffffffffu;
1747
Steve Blocka7e24c12009-10-30 11:49:00 +00001748 static const uint32_t kMaxGap = 1024;
1749 static const int kMaxFastElementsLength = 5000;
1750 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001751 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00001752 static const int kMaxInstanceSize = 255 * kPointerSize;
1753 // When extending the backing storage for property values, we increase
1754 // its size by more than the 1 entry necessary, so sequentially adding fields
1755 // to the same object requires fewer allocations and copies.
1756 static const int kFieldsAdded = 3;
1757
1758 // Layout description.
1759 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1760 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1761 static const int kHeaderSize = kElementsOffset + kPointerSize;
1762
1763 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1764
Iain Merrick75681382010-08-19 15:07:18 +01001765 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1766 public:
1767 static inline int SizeOf(Map* map, HeapObject* object);
1768 };
1769
Steve Blocka7e24c12009-10-30 11:49:00 +00001770 private:
John Reck59135872010-11-02 12:39:01 -07001771 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1772 Object* structure,
1773 uint32_t index,
1774 Object* holder);
1775 MaybeObject* SetElementWithCallback(Object* structure,
1776 uint32_t index,
1777 Object* value,
1778 JSObject* holder);
1779 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(uint32_t index,
Steve Block9fac8402011-05-12 15:51:54 +01001780 Object* value,
1781 bool check_prototype);
1782 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
1783 uint32_t index,
1784 Object* value,
1785 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00001786
John Reck59135872010-11-02 12:39:01 -07001787 MaybeObject* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001788
John Reck59135872010-11-02 12:39:01 -07001789 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1790 DeleteMode mode);
1791 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001792
John Reck59135872010-11-02 12:39:01 -07001793 MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1794 DeleteMode mode);
1795 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001796
1797 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1798 String* name,
1799 bool continue_search);
1800 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1801 String* name,
1802 bool continue_search);
1803 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1804 Object* receiver,
1805 LookupResult* result,
1806 String* name,
1807 bool continue_search);
1808 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1809 LookupResult* result,
1810 String* name,
1811 bool continue_search);
1812
1813 // Returns true if most of the elements backing storage is used.
1814 bool HasDenseElements();
1815
Leon Clarkef7060e22010-06-03 12:02:55 +01001816 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07001817 MUST_USE_RESULT MaybeObject* SetElementCallback(
1818 uint32_t index,
1819 Object* structure,
1820 PropertyAttributes attributes);
1821 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1822 String* name,
1823 Object* structure,
1824 PropertyAttributes attributes);
1825 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
1826 String* name,
1827 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001828
1829 void LookupInDescriptor(String* name, LookupResult* result);
1830
1831 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1832};
1833
1834
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001835// FixedArray describes fixed-sized arrays with element type Object*.
1836class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001837 public:
1838 // [length]: length of the array.
1839 inline int length();
1840 inline void set_length(int value);
1841
Steve Blocka7e24c12009-10-30 11:49:00 +00001842 // Setter and getter for elements.
1843 inline Object* get(int index);
1844 // Setter that uses write barrier.
1845 inline void set(int index, Object* value);
1846
1847 // Setter that doesn't need write barrier).
1848 inline void set(int index, Smi* value);
1849 // Setter with explicit barrier mode.
1850 inline void set(int index, Object* value, WriteBarrierMode mode);
1851
1852 // Setters for frequently used oddballs located in old space.
1853 inline void set_undefined(int index);
1854 inline void set_null(int index);
1855 inline void set_the_hole(int index);
1856
Iain Merrick75681382010-08-19 15:07:18 +01001857 // Setters with less debug checks for the GC to use.
1858 inline void set_unchecked(int index, Smi* value);
1859 inline void set_null_unchecked(int index);
Ben Murdochf87a2032010-10-22 12:50:53 +01001860 inline void set_unchecked(int index, Object* value, WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01001861
Steve Block6ded16b2010-05-10 14:33:55 +01001862 // Gives access to raw memory which stores the array's data.
1863 inline Object** data_start();
1864
Steve Blocka7e24c12009-10-30 11:49:00 +00001865 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07001866 MUST_USE_RESULT inline MaybeObject* Copy();
1867 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001868
1869 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07001870 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00001871
1872 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07001873 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00001874
1875 // Copy a sub array from the receiver to dest.
1876 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1877
1878 // Garbage collection support.
1879 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1880
1881 // Code Generation support.
1882 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1883
1884 // Casting.
1885 static inline FixedArray* cast(Object* obj);
1886
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001887 // Layout description.
1888 // Length is smi tagged when it is stored.
1889 static const int kLengthOffset = HeapObject::kHeaderSize;
1890 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001891
1892 // Maximal allowed size, in bytes, of a single FixedArray.
1893 // Prevents overflowing size computations, as well as extreme memory
1894 // consumption.
1895 static const int kMaxSize = 512 * MB;
1896 // Maximally allowed length of a FixedArray.
1897 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001898
1899 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001900#ifdef OBJECT_PRINT
1901 inline void FixedArrayPrint() {
1902 FixedArrayPrint(stdout);
1903 }
1904 void FixedArrayPrint(FILE* out);
1905#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001906#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001907 void FixedArrayVerify();
1908 // Checks if two FixedArrays have identical contents.
1909 bool IsEqualTo(FixedArray* other);
1910#endif
1911
1912 // Swap two elements in a pair of arrays. If this array and the
1913 // numbers array are the same object, the elements are only swapped
1914 // once.
1915 void SwapPairs(FixedArray* numbers, int i, int j);
1916
1917 // Sort prefix of this array and the numbers array as pairs wrt. the
1918 // numbers. If the numbers array and the this array are the same
1919 // object, the prefix of this array is sorted.
1920 void SortPairs(FixedArray* numbers, uint32_t len);
1921
Iain Merrick75681382010-08-19 15:07:18 +01001922 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
1923 public:
1924 static inline int SizeOf(Map* map, HeapObject* object) {
1925 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
1926 }
1927 };
1928
Steve Blocka7e24c12009-10-30 11:49:00 +00001929 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001930 // Set operation on FixedArray without using write barriers. Can
1931 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001932 static inline void fast_set(FixedArray* array, int index, Object* value);
1933
1934 private:
1935 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1936};
1937
1938
1939// DescriptorArrays are fixed arrays used to hold instance descriptors.
1940// The format of the these objects is:
1941// [0]: point to a fixed array with (value, detail) pairs.
1942// [1]: next enumeration index (Smi), or pointer to small fixed array:
1943// [0]: next enumeration index (Smi)
1944// [1]: pointer to fixed array with enum cache
1945// [2]: first key
1946// [length() - 1]: last key
1947//
1948class DescriptorArray: public FixedArray {
1949 public:
1950 // Is this the singleton empty_descriptor_array?
1951 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001952
Steve Blocka7e24c12009-10-30 11:49:00 +00001953 // Returns the number of descriptors in the array.
1954 int number_of_descriptors() {
1955 return IsEmpty() ? 0 : length() - kFirstIndex;
1956 }
1957
1958 int NextEnumerationIndex() {
1959 if (IsEmpty()) return PropertyDetails::kInitialIndex;
1960 Object* obj = get(kEnumerationIndexIndex);
1961 if (obj->IsSmi()) {
1962 return Smi::cast(obj)->value();
1963 } else {
1964 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1965 return Smi::cast(index)->value();
1966 }
1967 }
1968
1969 // Set next enumeration index and flush any enum cache.
1970 void SetNextEnumerationIndex(int value) {
1971 if (!IsEmpty()) {
1972 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1973 }
1974 }
1975 bool HasEnumCache() {
1976 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
1977 }
1978
1979 Object* GetEnumCache() {
1980 ASSERT(HasEnumCache());
1981 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1982 return bridge->get(kEnumCacheBridgeCacheIndex);
1983 }
1984
1985 // Initialize or change the enum cache,
1986 // using the supplied storage for the small "bridge".
1987 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1988
1989 // Accessors for fetching instance descriptor at descriptor number.
1990 inline String* GetKey(int descriptor_number);
1991 inline Object* GetValue(int descriptor_number);
1992 inline Smi* GetDetails(int descriptor_number);
1993 inline PropertyType GetType(int descriptor_number);
1994 inline int GetFieldIndex(int descriptor_number);
1995 inline JSFunction* GetConstantFunction(int descriptor_number);
1996 inline Object* GetCallbacksObject(int descriptor_number);
1997 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
1998 inline bool IsProperty(int descriptor_number);
1999 inline bool IsTransition(int descriptor_number);
2000 inline bool IsNullDescriptor(int descriptor_number);
2001 inline bool IsDontEnum(int descriptor_number);
2002
2003 // Accessor for complete descriptor.
2004 inline void Get(int descriptor_number, Descriptor* desc);
2005 inline void Set(int descriptor_number, Descriptor* desc);
2006
2007 // Transfer complete descriptor from another descriptor array to
2008 // this one.
2009 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2010
2011 // Copy the descriptor array, insert a new descriptor and optionally
2012 // remove map transitions. If the descriptor is already present, it is
2013 // replaced. If a replaced descriptor is a real property (not a transition
2014 // or null), its enumeration index is kept as is.
2015 // If adding a real property, map transitions must be removed. If adding
2016 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002017 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2018 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002019
2020 // Remove all transitions. Return a copy of the array with all transitions
2021 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002022 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002023
2024 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002025 // Does not check for duplicates.
2026 void SortUnchecked();
2027
2028 // Sort the instance descriptors by the hash codes of their keys.
2029 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00002030 void Sort();
2031
2032 // Search the instance descriptors for given name.
2033 inline int Search(String* name);
2034
Iain Merrick75681382010-08-19 15:07:18 +01002035 // As the above, but uses DescriptorLookupCache and updates it when
2036 // necessary.
2037 inline int SearchWithCache(String* name);
2038
Steve Blocka7e24c12009-10-30 11:49:00 +00002039 // Tells whether the name is present int the array.
2040 bool Contains(String* name) { return kNotFound != Search(name); }
2041
2042 // Perform a binary search in the instance descriptors represented
2043 // by this fixed array. low and high are descriptor indices. If there
2044 // are three instance descriptors in this array it should be called
2045 // with low=0 and high=2.
2046 int BinarySearch(String* name, int low, int high);
2047
2048 // Perform a linear search in the instance descriptors represented
2049 // by this fixed array. len is the number of descriptor indices that are
2050 // valid. Does not require the descriptors to be sorted.
2051 int LinearSearch(String* name, int len);
2052
2053 // Allocates a DescriptorArray, but returns the singleton
2054 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002055 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002056
2057 // Casting.
2058 static inline DescriptorArray* cast(Object* obj);
2059
2060 // Constant for denoting key was not found.
2061 static const int kNotFound = -1;
2062
2063 static const int kContentArrayIndex = 0;
2064 static const int kEnumerationIndexIndex = 1;
2065 static const int kFirstIndex = 2;
2066
2067 // The length of the "bridge" to the enum cache.
2068 static const int kEnumCacheBridgeLength = 2;
2069 static const int kEnumCacheBridgeEnumIndex = 0;
2070 static const int kEnumCacheBridgeCacheIndex = 1;
2071
2072 // Layout description.
2073 static const int kContentArrayOffset = FixedArray::kHeaderSize;
2074 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2075 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2076
2077 // Layout description for the bridge array.
2078 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2079 static const int kEnumCacheBridgeCacheOffset =
2080 kEnumCacheBridgeEnumOffset + kPointerSize;
2081
Ben Murdochb0fe1622011-05-05 13:52:32 +01002082#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002083 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002084 inline void PrintDescriptors() {
2085 PrintDescriptors(stdout);
2086 }
2087 void PrintDescriptors(FILE* out);
2088#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002089
Ben Murdochb0fe1622011-05-05 13:52:32 +01002090#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002091 // Is the descriptor array sorted and without duplicates?
2092 bool IsSortedNoDuplicates();
2093
2094 // Are two DescriptorArrays equal?
2095 bool IsEqualTo(DescriptorArray* other);
2096#endif
2097
2098 // The maximum number of descriptors we want in a descriptor array (should
2099 // fit in a page).
2100 static const int kMaxNumberOfDescriptors = 1024 + 512;
2101
2102 private:
2103 // Conversion from descriptor number to array indices.
2104 static int ToKeyIndex(int descriptor_number) {
2105 return descriptor_number+kFirstIndex;
2106 }
Leon Clarkee46be812010-01-19 14:06:41 +00002107
2108 static int ToDetailsIndex(int descriptor_number) {
2109 return (descriptor_number << 1) + 1;
2110 }
2111
Steve Blocka7e24c12009-10-30 11:49:00 +00002112 static int ToValueIndex(int descriptor_number) {
2113 return descriptor_number << 1;
2114 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002115
2116 bool is_null_descriptor(int descriptor_number) {
2117 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2118 NULL_DESCRIPTOR;
2119 }
2120 // Swap operation on FixedArray without using write barriers.
2121 static inline void fast_swap(FixedArray* array, int first, int second);
2122
2123 // Swap descriptor first and second.
2124 inline void Swap(int first, int second);
2125
2126 FixedArray* GetContentArray() {
2127 return FixedArray::cast(get(kContentArrayIndex));
2128 }
2129 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2130};
2131
2132
2133// HashTable is a subclass of FixedArray that implements a hash table
2134// that uses open addressing and quadratic probing.
2135//
2136// In order for the quadratic probing to work, elements that have not
2137// yet been used and elements that have been deleted are
2138// distinguished. Probing continues when deleted elements are
2139// encountered and stops when unused elements are encountered.
2140//
2141// - Elements with key == undefined have not been used yet.
2142// - Elements with key == null have been deleted.
2143//
2144// The hash table class is parameterized with a Shape and a Key.
2145// Shape must be a class with the following interface:
2146// class ExampleShape {
2147// public:
2148// // Tells whether key matches other.
2149// static bool IsMatch(Key key, Object* other);
2150// // Returns the hash value for key.
2151// static uint32_t Hash(Key key);
2152// // Returns the hash value for object.
2153// static uint32_t HashForObject(Key key, Object* object);
2154// // Convert key to an object.
2155// static inline Object* AsObject(Key key);
2156// // The prefix size indicates number of elements in the beginning
2157// // of the backing storage.
2158// static const int kPrefixSize = ..;
2159// // The Element size indicates number of elements per entry.
2160// static const int kEntrySize = ..;
2161// };
Steve Block3ce2e202009-11-05 08:53:23 +00002162// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002163// beginning of the backing storage that can be used for non-element
2164// information by subclasses.
2165
2166template<typename Shape, typename Key>
2167class HashTable: public FixedArray {
2168 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002169 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002170 int NumberOfElements() {
2171 return Smi::cast(get(kNumberOfElementsIndex))->value();
2172 }
2173
Leon Clarkee46be812010-01-19 14:06:41 +00002174 // Returns the number of deleted elements in the hash table.
2175 int NumberOfDeletedElements() {
2176 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2177 }
2178
Steve Block3ce2e202009-11-05 08:53:23 +00002179 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002180 int Capacity() {
2181 return Smi::cast(get(kCapacityIndex))->value();
2182 }
2183
2184 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002185 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002186 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2187
2188 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002189 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002190 void ElementRemoved() {
2191 SetNumberOfElements(NumberOfElements() - 1);
2192 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2193 }
2194 void ElementsRemoved(int n) {
2195 SetNumberOfElements(NumberOfElements() - n);
2196 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2197 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002198
Steve Block3ce2e202009-11-05 08:53:23 +00002199 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002200 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002201 int at_least_space_for,
2202 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002203
2204 // Returns the key at entry.
2205 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2206
2207 // Tells whether k is a real key. Null and undefined are not allowed
2208 // as keys and can be used to indicate missing or deleted elements.
2209 bool IsKey(Object* k) {
2210 return !k->IsNull() && !k->IsUndefined();
2211 }
2212
2213 // Garbage collection support.
2214 void IteratePrefix(ObjectVisitor* visitor);
2215 void IterateElements(ObjectVisitor* visitor);
2216
2217 // Casting.
2218 static inline HashTable* cast(Object* obj);
2219
2220 // Compute the probe offset (quadratic probing).
2221 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2222 return (n + n * n) >> 1;
2223 }
2224
2225 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002226 static const int kNumberOfDeletedElementsIndex = 1;
2227 static const int kCapacityIndex = 2;
2228 static const int kPrefixStartIndex = 3;
2229 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002230 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002231 static const int kEntrySize = Shape::kEntrySize;
2232 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002233 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002234 static const int kCapacityOffset =
2235 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002236
2237 // Constant used for denoting a absent entry.
2238 static const int kNotFound = -1;
2239
Leon Clarkee46be812010-01-19 14:06:41 +00002240 // Maximal capacity of HashTable. Based on maximal length of underlying
2241 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2242 // cannot overflow.
2243 static const int kMaxCapacity =
2244 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2245
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002246 // Find entry for key otherwise return kNotFound.
Steve Blocka7e24c12009-10-30 11:49:00 +00002247 int FindEntry(Key key);
2248
2249 protected:
2250
2251 // Find the entry at which to insert element with the given key that
2252 // has the given hash value.
2253 uint32_t FindInsertionEntry(uint32_t hash);
2254
2255 // Returns the index for an entry (of the key)
2256 static inline int EntryToIndex(int entry) {
2257 return (entry * kEntrySize) + kElementsStartIndex;
2258 }
2259
Steve Block3ce2e202009-11-05 08:53:23 +00002260 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002261 void SetNumberOfElements(int nof) {
2262 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2263 }
2264
Leon Clarkee46be812010-01-19 14:06:41 +00002265 // Update the number of deleted elements in the hash table.
2266 void SetNumberOfDeletedElements(int nod) {
2267 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2268 }
2269
Steve Blocka7e24c12009-10-30 11:49:00 +00002270 // Sets the capacity of the hash table.
2271 void SetCapacity(int capacity) {
2272 // To scale a computed hash code to fit within the hash table, we
2273 // use bit-wise AND with a mask, so the capacity must be positive
2274 // and non-zero.
2275 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002276 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002277 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2278 }
2279
2280
2281 // Returns probe entry.
2282 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2283 ASSERT(IsPowerOf2(size));
2284 return (hash + GetProbeOffset(number)) & (size - 1);
2285 }
2286
Leon Clarkee46be812010-01-19 14:06:41 +00002287 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2288 return hash & (size - 1);
2289 }
2290
2291 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2292 return (last + number) & (size - 1);
2293 }
2294
Steve Blocka7e24c12009-10-30 11:49:00 +00002295 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002296 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002297};
2298
2299
2300
2301// HashTableKey is an abstract superclass for virtual key behavior.
2302class HashTableKey {
2303 public:
2304 // Returns whether the other object matches this key.
2305 virtual bool IsMatch(Object* other) = 0;
2306 // Returns the hash value for this key.
2307 virtual uint32_t Hash() = 0;
2308 // Returns the hash value for object.
2309 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002310 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002311 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002312 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002313 // Required.
2314 virtual ~HashTableKey() {}
2315};
2316
2317class SymbolTableShape {
2318 public:
2319 static bool IsMatch(HashTableKey* key, Object* value) {
2320 return key->IsMatch(value);
2321 }
2322 static uint32_t Hash(HashTableKey* key) {
2323 return key->Hash();
2324 }
2325 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2326 return key->HashForObject(object);
2327 }
John Reck59135872010-11-02 12:39:01 -07002328 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002329 return key->AsObject();
2330 }
2331
2332 static const int kPrefixSize = 0;
2333 static const int kEntrySize = 1;
2334};
2335
2336// SymbolTable.
2337//
2338// No special elements in the prefix and the element size is 1
2339// because only the symbol itself (the key) needs to be stored.
2340class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2341 public:
2342 // Find symbol in the symbol table. If it is not there yet, it is
2343 // added. The return value is the symbol table which might have
2344 // been enlarged. If the return value is not a failure, the symbol
2345 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002346 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002347 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2348 Object** s);
2349 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2350 Object** s);
John Reck59135872010-11-02 12:39:01 -07002351 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002352
2353 // Looks up a symbol that is equal to the given string and returns
2354 // true if it is found, assigning the symbol to the given output
2355 // parameter.
2356 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002357 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002358
2359 // Casting.
2360 static inline SymbolTable* cast(Object* obj);
2361
2362 private:
John Reck59135872010-11-02 12:39:01 -07002363 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002364
2365 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2366};
2367
2368
2369class MapCacheShape {
2370 public:
2371 static bool IsMatch(HashTableKey* key, Object* value) {
2372 return key->IsMatch(value);
2373 }
2374 static uint32_t Hash(HashTableKey* key) {
2375 return key->Hash();
2376 }
2377
2378 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2379 return key->HashForObject(object);
2380 }
2381
John Reck59135872010-11-02 12:39:01 -07002382 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002383 return key->AsObject();
2384 }
2385
2386 static const int kPrefixSize = 0;
2387 static const int kEntrySize = 2;
2388};
2389
2390
2391// MapCache.
2392//
2393// Maps keys that are a fixed array of symbols to a map.
2394// Used for canonicalize maps for object literals.
2395class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2396 public:
2397 // Find cached value for a string key, otherwise return null.
2398 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002399 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002400 static inline MapCache* cast(Object* obj);
2401
2402 private:
2403 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2404};
2405
2406
2407template <typename Shape, typename Key>
2408class Dictionary: public HashTable<Shape, Key> {
2409 public:
2410
2411 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2412 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2413 }
2414
2415 // Returns the value at entry.
2416 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002417 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002418 }
2419
2420 // Set the value for entry.
2421 void ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002422 // Check that this value can actually be written.
2423 PropertyDetails details = DetailsAt(entry);
2424 // If a value has not been initilized we allow writing to it even if
2425 // it is read only (a declared const that has not been initialized).
2426 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) return;
Steve Block6ded16b2010-05-10 14:33:55 +01002427 this->set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002428 }
2429
2430 // Returns the property details for the property at entry.
2431 PropertyDetails DetailsAt(int entry) {
2432 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2433 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002434 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002435 }
2436
2437 // Set the details for entry.
2438 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002439 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002440 }
2441
2442 // Sorting support
2443 void CopyValuesTo(FixedArray* elements);
2444
2445 // Delete a property from the dictionary.
2446 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2447
2448 // Returns the number of elements in the dictionary filtering out properties
2449 // with the specified attributes.
2450 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2451
2452 // Returns the number of enumerable elements in the dictionary.
2453 int NumberOfEnumElements();
2454
2455 // Copies keys to preallocated fixed array.
2456 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2457 // Fill in details for properties into storage.
2458 void CopyKeysTo(FixedArray* storage);
2459
2460 // Accessors for next enumeration index.
2461 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002462 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002463 }
2464
2465 int NextEnumerationIndex() {
2466 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2467 }
2468
2469 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002470 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002471
2472 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002473 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002474
Ben Murdochb0fe1622011-05-05 13:52:32 +01002475#ifdef OBJECT_PRINT
2476 inline void Print() {
2477 Print(stdout);
2478 }
2479 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002480#endif
2481 // Returns the key (slow).
2482 Object* SlowReverseLookup(Object* value);
2483
2484 // Sets the entry to (key, value) pair.
2485 inline void SetEntry(int entry,
2486 Object* key,
2487 Object* value,
2488 PropertyDetails details);
2489
John Reck59135872010-11-02 12:39:01 -07002490 MUST_USE_RESULT MaybeObject* Add(Key key,
2491 Object* value,
2492 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002493
2494 protected:
2495 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002496 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002497
2498 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002499 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2500 Object* value,
2501 PropertyDetails details,
2502 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002503
2504 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002505 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002506 static const int kMaxNumberKeyIndex =
2507 HashTable<Shape, Key>::kPrefixStartIndex;
2508 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2509};
2510
2511
2512class StringDictionaryShape {
2513 public:
2514 static inline bool IsMatch(String* key, Object* other);
2515 static inline uint32_t Hash(String* key);
2516 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002517 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002518 static const int kPrefixSize = 2;
2519 static const int kEntrySize = 3;
2520 static const bool kIsEnumerable = true;
2521};
2522
2523
2524class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2525 public:
2526 static inline StringDictionary* cast(Object* obj) {
2527 ASSERT(obj->IsDictionary());
2528 return reinterpret_cast<StringDictionary*>(obj);
2529 }
2530
2531 // Copies enumerable keys to preallocated fixed array.
2532 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2533
2534 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002535 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2536 JSObject* obj,
2537 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002538
2539 // Find entry for key otherwise return kNotFound. Optimzed version of
2540 // HashTable::FindEntry.
2541 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002542};
2543
2544
2545class NumberDictionaryShape {
2546 public:
2547 static inline bool IsMatch(uint32_t key, Object* other);
2548 static inline uint32_t Hash(uint32_t key);
2549 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002550 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002551 static const int kPrefixSize = 2;
2552 static const int kEntrySize = 3;
2553 static const bool kIsEnumerable = false;
2554};
2555
2556
2557class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2558 public:
2559 static NumberDictionary* cast(Object* obj) {
2560 ASSERT(obj->IsDictionary());
2561 return reinterpret_cast<NumberDictionary*>(obj);
2562 }
2563
2564 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002565 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2566 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2567 Object* value,
2568 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002569
2570 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002571 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2572 Object* value,
2573 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002574
2575 void UpdateMaxNumberKey(uint32_t key);
2576
2577 // If slow elements are required we will never go back to fast-case
2578 // for the elements kept in this dictionary. We require slow
2579 // elements if an element has been added at an index larger than
2580 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2581 // when defining a getter or setter with a number key.
2582 inline bool requires_slow_elements();
2583 inline void set_requires_slow_elements();
2584
2585 // Get the value of the max number key that has been added to this
2586 // dictionary. max_number_key can only be called if
2587 // requires_slow_elements returns false.
2588 inline uint32_t max_number_key();
2589
2590 // Remove all entries were key is a number and (from <= key && key < to).
2591 void RemoveNumberEntries(uint32_t from, uint32_t to);
2592
2593 // Bit masks.
2594 static const int kRequiresSlowElementsMask = 1;
2595 static const int kRequiresSlowElementsTagSize = 1;
2596 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2597};
2598
2599
Steve Block6ded16b2010-05-10 14:33:55 +01002600// JSFunctionResultCache caches results of some JSFunction invocation.
2601// It is a fixed array with fixed structure:
2602// [0]: factory function
2603// [1]: finger index
2604// [2]: current cache size
2605// [3]: dummy field.
2606// The rest of array are key/value pairs.
2607class JSFunctionResultCache: public FixedArray {
2608 public:
2609 static const int kFactoryIndex = 0;
2610 static const int kFingerIndex = kFactoryIndex + 1;
2611 static const int kCacheSizeIndex = kFingerIndex + 1;
2612 static const int kDummyIndex = kCacheSizeIndex + 1;
2613 static const int kEntriesIndex = kDummyIndex + 1;
2614
2615 static const int kEntrySize = 2; // key + value
2616
Kristian Monsen25f61362010-05-21 11:50:48 +01002617 static const int kFactoryOffset = kHeaderSize;
2618 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2619 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2620
Steve Block6ded16b2010-05-10 14:33:55 +01002621 inline void MakeZeroSize();
2622 inline void Clear();
2623
Ben Murdochb8e0da22011-05-16 14:20:40 +01002624 inline int size();
2625 inline void set_size(int size);
2626 inline int finger_index();
2627 inline void set_finger_index(int finger_index);
2628
Steve Block6ded16b2010-05-10 14:33:55 +01002629 // Casting
2630 static inline JSFunctionResultCache* cast(Object* obj);
2631
2632#ifdef DEBUG
2633 void JSFunctionResultCacheVerify();
2634#endif
2635};
2636
2637
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002638// The cache for maps used by normalized (dictionary mode) objects.
2639// Such maps do not have property descriptors, so a typical program
2640// needs very limited number of distinct normalized maps.
2641class NormalizedMapCache: public FixedArray {
2642 public:
2643 static const int kEntries = 64;
2644
John Reck59135872010-11-02 12:39:01 -07002645 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2646 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002647
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002648 void Clear();
2649
2650 // Casting
2651 static inline NormalizedMapCache* cast(Object* obj);
2652
2653#ifdef DEBUG
2654 void NormalizedMapCacheVerify();
2655#endif
2656
2657 private:
2658 static int Hash(Map* fast);
2659
2660 static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2661};
2662
2663
Steve Blocka7e24c12009-10-30 11:49:00 +00002664// ByteArray represents fixed sized byte arrays. Used by the outside world,
2665// such as PCRE, and also by the memory allocator and garbage collector to
2666// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002667class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002668 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002669 // [length]: length of the array.
2670 inline int length();
2671 inline void set_length(int value);
2672
Steve Blocka7e24c12009-10-30 11:49:00 +00002673 // Setter and getter.
2674 inline byte get(int index);
2675 inline void set(int index, byte value);
2676
2677 // Treat contents as an int array.
2678 inline int get_int(int index);
2679
2680 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002681 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002682 }
2683 // We use byte arrays for free blocks in the heap. Given a desired size in
2684 // bytes that is a multiple of the word size and big enough to hold a byte
2685 // array, this function returns the number of elements a byte array should
2686 // have.
2687 static int LengthFor(int size_in_bytes) {
2688 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2689 ASSERT(size_in_bytes >= kHeaderSize);
2690 return size_in_bytes - kHeaderSize;
2691 }
2692
2693 // Returns data start address.
2694 inline Address GetDataStartAddress();
2695
2696 // Returns a pointer to the ByteArray object for a given data start address.
2697 static inline ByteArray* FromDataStartAddress(Address address);
2698
2699 // Casting.
2700 static inline ByteArray* cast(Object* obj);
2701
2702 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002703 inline int ByteArraySize() {
2704 return SizeFor(this->length());
2705 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002706#ifdef OBJECT_PRINT
2707 inline void ByteArrayPrint() {
2708 ByteArrayPrint(stdout);
2709 }
2710 void ByteArrayPrint(FILE* out);
2711#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002712#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002713 void ByteArrayVerify();
2714#endif
2715
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002716 // Layout description.
2717 // Length is smi tagged when it is stored.
2718 static const int kLengthOffset = HeapObject::kHeaderSize;
2719 static const int kHeaderSize = kLengthOffset + kPointerSize;
2720
2721 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002722
Leon Clarkee46be812010-01-19 14:06:41 +00002723 // Maximal memory consumption for a single ByteArray.
2724 static const int kMaxSize = 512 * MB;
2725 // Maximal length of a single ByteArray.
2726 static const int kMaxLength = kMaxSize - kHeaderSize;
2727
Steve Blocka7e24c12009-10-30 11:49:00 +00002728 private:
2729 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2730};
2731
2732
2733// A PixelArray represents a fixed-size byte array with special semantics
2734// used for implementing the CanvasPixelArray object. Please see the
2735// specification at:
2736// http://www.whatwg.org/specs/web-apps/current-work/
2737// multipage/the-canvas-element.html#canvaspixelarray
2738// In particular, write access clamps the value written to 0 or 255 if the
2739// value written is outside this range.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002740class PixelArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002741 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002742 // [length]: length of the array.
2743 inline int length();
2744 inline void set_length(int value);
2745
Steve Blocka7e24c12009-10-30 11:49:00 +00002746 // [external_pointer]: The pointer to the external memory area backing this
2747 // pixel array.
2748 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2749
2750 // Setter and getter.
2751 inline uint8_t get(int index);
2752 inline void set(int index, uint8_t value);
2753
2754 // This accessor applies the correct conversion from Smi, HeapNumber and
2755 // undefined and clamps the converted value between 0 and 255.
2756 Object* SetValue(uint32_t index, Object* value);
2757
2758 // Casting.
2759 static inline PixelArray* cast(Object* obj);
2760
Ben Murdochb0fe1622011-05-05 13:52:32 +01002761#ifdef OBJECT_PRINT
2762 inline void PixelArrayPrint() {
2763 PixelArrayPrint(stdout);
2764 }
2765 void PixelArrayPrint(FILE* out);
2766#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002767#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002768 void PixelArrayVerify();
2769#endif // DEBUG
2770
Steve Block3ce2e202009-11-05 08:53:23 +00002771 // Maximal acceptable length for a pixel array.
2772 static const int kMaxLength = 0x3fffffff;
2773
Steve Blocka7e24c12009-10-30 11:49:00 +00002774 // PixelArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002775 static const int kLengthOffset = HeapObject::kHeaderSize;
2776 static const int kExternalPointerOffset =
2777 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002778 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002779 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002780
2781 private:
2782 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2783};
2784
2785
Steve Block3ce2e202009-11-05 08:53:23 +00002786// An ExternalArray represents a fixed-size array of primitive values
2787// which live outside the JavaScript heap. Its subclasses are used to
2788// implement the CanvasArray types being defined in the WebGL
2789// specification. As of this writing the first public draft is not yet
2790// available, but Khronos members can access the draft at:
2791// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2792//
2793// The semantics of these arrays differ from CanvasPixelArray.
2794// Out-of-range values passed to the setter are converted via a C
2795// cast, not clamping. Out-of-range indices cause exceptions to be
2796// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002797class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002798 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002799 // [length]: length of the array.
2800 inline int length();
2801 inline void set_length(int value);
2802
Steve Block3ce2e202009-11-05 08:53:23 +00002803 // [external_pointer]: The pointer to the external memory area backing this
2804 // external array.
2805 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2806
2807 // Casting.
2808 static inline ExternalArray* cast(Object* obj);
2809
2810 // Maximal acceptable length for an external array.
2811 static const int kMaxLength = 0x3fffffff;
2812
2813 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002814 static const int kLengthOffset = HeapObject::kHeaderSize;
2815 static const int kExternalPointerOffset =
2816 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002817 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002818 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002819
2820 private:
2821 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2822};
2823
2824
2825class ExternalByteArray: public ExternalArray {
2826 public:
2827 // Setter and getter.
2828 inline int8_t get(int index);
2829 inline void set(int index, int8_t value);
2830
2831 // This accessor applies the correct conversion from Smi, HeapNumber
2832 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002833 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002834
2835 // Casting.
2836 static inline ExternalByteArray* cast(Object* obj);
2837
Ben Murdochb0fe1622011-05-05 13:52:32 +01002838#ifdef OBJECT_PRINT
2839 inline void ExternalByteArrayPrint() {
2840 ExternalByteArrayPrint(stdout);
2841 }
2842 void ExternalByteArrayPrint(FILE* out);
2843#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002844#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002845 void ExternalByteArrayVerify();
2846#endif // DEBUG
2847
2848 private:
2849 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2850};
2851
2852
2853class ExternalUnsignedByteArray: public ExternalArray {
2854 public:
2855 // Setter and getter.
2856 inline uint8_t get(int index);
2857 inline void set(int index, uint8_t value);
2858
2859 // This accessor applies the correct conversion from Smi, HeapNumber
2860 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002861 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002862
2863 // Casting.
2864 static inline ExternalUnsignedByteArray* cast(Object* obj);
2865
Ben Murdochb0fe1622011-05-05 13:52:32 +01002866#ifdef OBJECT_PRINT
2867 inline void ExternalUnsignedByteArrayPrint() {
2868 ExternalUnsignedByteArrayPrint(stdout);
2869 }
2870 void ExternalUnsignedByteArrayPrint(FILE* out);
2871#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002872#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002873 void ExternalUnsignedByteArrayVerify();
2874#endif // DEBUG
2875
2876 private:
2877 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2878};
2879
2880
2881class ExternalShortArray: public ExternalArray {
2882 public:
2883 // Setter and getter.
2884 inline int16_t get(int index);
2885 inline void set(int index, int16_t value);
2886
2887 // This accessor applies the correct conversion from Smi, HeapNumber
2888 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002889 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002890
2891 // Casting.
2892 static inline ExternalShortArray* cast(Object* obj);
2893
Ben Murdochb0fe1622011-05-05 13:52:32 +01002894#ifdef OBJECT_PRINT
2895 inline void ExternalShortArrayPrint() {
2896 ExternalShortArrayPrint(stdout);
2897 }
2898 void ExternalShortArrayPrint(FILE* out);
2899#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002900#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002901 void ExternalShortArrayVerify();
2902#endif // DEBUG
2903
2904 private:
2905 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2906};
2907
2908
2909class ExternalUnsignedShortArray: public ExternalArray {
2910 public:
2911 // Setter and getter.
2912 inline uint16_t get(int index);
2913 inline void set(int index, uint16_t value);
2914
2915 // This accessor applies the correct conversion from Smi, HeapNumber
2916 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002917 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002918
2919 // Casting.
2920 static inline ExternalUnsignedShortArray* cast(Object* obj);
2921
Ben Murdochb0fe1622011-05-05 13:52:32 +01002922#ifdef OBJECT_PRINT
2923 inline void ExternalUnsignedShortArrayPrint() {
2924 ExternalUnsignedShortArrayPrint(stdout);
2925 }
2926 void ExternalUnsignedShortArrayPrint(FILE* out);
2927#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002928#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002929 void ExternalUnsignedShortArrayVerify();
2930#endif // DEBUG
2931
2932 private:
2933 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2934};
2935
2936
2937class ExternalIntArray: public ExternalArray {
2938 public:
2939 // Setter and getter.
2940 inline int32_t get(int index);
2941 inline void set(int index, int32_t value);
2942
2943 // This accessor applies the correct conversion from Smi, HeapNumber
2944 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002945 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002946
2947 // Casting.
2948 static inline ExternalIntArray* cast(Object* obj);
2949
Ben Murdochb0fe1622011-05-05 13:52:32 +01002950#ifdef OBJECT_PRINT
2951 inline void ExternalIntArrayPrint() {
2952 ExternalIntArrayPrint(stdout);
2953 }
2954 void ExternalIntArrayPrint(FILE* out);
2955#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002956#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002957 void ExternalIntArrayVerify();
2958#endif // DEBUG
2959
2960 private:
2961 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
2962};
2963
2964
2965class ExternalUnsignedIntArray: public ExternalArray {
2966 public:
2967 // Setter and getter.
2968 inline uint32_t get(int index);
2969 inline void set(int index, uint32_t value);
2970
2971 // This accessor applies the correct conversion from Smi, HeapNumber
2972 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002973 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002974
2975 // Casting.
2976 static inline ExternalUnsignedIntArray* cast(Object* obj);
2977
Ben Murdochb0fe1622011-05-05 13:52:32 +01002978#ifdef OBJECT_PRINT
2979 inline void ExternalUnsignedIntArrayPrint() {
2980 ExternalUnsignedIntArrayPrint(stdout);
2981 }
2982 void ExternalUnsignedIntArrayPrint(FILE* out);
2983#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002984#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002985 void ExternalUnsignedIntArrayVerify();
2986#endif // DEBUG
2987
2988 private:
2989 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
2990};
2991
2992
2993class ExternalFloatArray: public ExternalArray {
2994 public:
2995 // Setter and getter.
2996 inline float get(int index);
2997 inline void set(int index, float value);
2998
2999 // This accessor applies the correct conversion from Smi, HeapNumber
3000 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003001 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003002
3003 // Casting.
3004 static inline ExternalFloatArray* cast(Object* obj);
3005
Ben Murdochb0fe1622011-05-05 13:52:32 +01003006#ifdef OBJECT_PRINT
3007 inline void ExternalFloatArrayPrint() {
3008 ExternalFloatArrayPrint(stdout);
3009 }
3010 void ExternalFloatArrayPrint(FILE* out);
3011#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003012#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003013 void ExternalFloatArrayVerify();
3014#endif // DEBUG
3015
3016 private:
3017 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3018};
3019
3020
Ben Murdochb0fe1622011-05-05 13:52:32 +01003021// DeoptimizationInputData is a fixed array used to hold the deoptimization
3022// data for code generated by the Hydrogen/Lithium compiler. It also
3023// contains information about functions that were inlined. If N different
3024// functions were inlined then first N elements of the literal array will
3025// contain these functions.
3026//
3027// It can be empty.
3028class DeoptimizationInputData: public FixedArray {
3029 public:
3030 // Layout description. Indices in the array.
3031 static const int kTranslationByteArrayIndex = 0;
3032 static const int kInlinedFunctionCountIndex = 1;
3033 static const int kLiteralArrayIndex = 2;
3034 static const int kOsrAstIdIndex = 3;
3035 static const int kOsrPcOffsetIndex = 4;
3036 static const int kFirstDeoptEntryIndex = 5;
3037
3038 // Offsets of deopt entry elements relative to the start of the entry.
3039 static const int kAstIdOffset = 0;
3040 static const int kTranslationIndexOffset = 1;
3041 static const int kArgumentsStackHeightOffset = 2;
3042 static const int kDeoptEntrySize = 3;
3043
3044 // Simple element accessors.
3045#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3046 type* name() { \
3047 return type::cast(get(k##name##Index)); \
3048 } \
3049 void Set##name(type* value) { \
3050 set(k##name##Index, value); \
3051 }
3052
3053 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3054 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3055 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3056 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3057 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3058
3059 // Unchecked accessor to be used during GC.
3060 FixedArray* UncheckedLiteralArray() {
3061 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3062 }
3063
3064#undef DEFINE_ELEMENT_ACCESSORS
3065
3066 // Accessors for elements of the ith deoptimization entry.
3067#define DEFINE_ENTRY_ACCESSORS(name, type) \
3068 type* name(int i) { \
3069 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3070 } \
3071 void Set##name(int i, type* value) { \
3072 set(IndexForEntry(i) + k##name##Offset, value); \
3073 }
3074
3075 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3076 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3077 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3078
3079#undef DEFINE_ENTRY_ACCESSORS
3080
3081 int DeoptCount() {
3082 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3083 }
3084
3085 // Allocates a DeoptimizationInputData.
3086 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3087 PretenureFlag pretenure);
3088
3089 // Casting.
3090 static inline DeoptimizationInputData* cast(Object* obj);
3091
3092#ifdef OBJECT_PRINT
3093 void DeoptimizationInputDataPrint(FILE* out);
3094#endif
3095
3096 private:
3097 static int IndexForEntry(int i) {
3098 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3099 }
3100
3101 static int LengthFor(int entry_count) {
3102 return IndexForEntry(entry_count);
3103 }
3104};
3105
3106
3107// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3108// data for code generated by the full compiler.
3109// The format of the these objects is
3110// [i * 2]: Ast ID for ith deoptimization.
3111// [i * 2 + 1]: PC and state of ith deoptimization
3112class DeoptimizationOutputData: public FixedArray {
3113 public:
3114 int DeoptPoints() { return length() / 2; }
3115 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3116 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3117 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3118 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3119
3120 static int LengthOfFixedArray(int deopt_points) {
3121 return deopt_points * 2;
3122 }
3123
3124 // Allocates a DeoptimizationOutputData.
3125 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3126 PretenureFlag pretenure);
3127
3128 // Casting.
3129 static inline DeoptimizationOutputData* cast(Object* obj);
3130
3131#ifdef OBJECT_PRINT
3132 void DeoptimizationOutputDataPrint(FILE* out);
3133#endif
3134};
3135
3136
Ben Murdochb8e0da22011-05-16 14:20:40 +01003137class SafepointEntry;
3138
3139
Steve Blocka7e24c12009-10-30 11:49:00 +00003140// Code describes objects with on-the-fly generated machine code.
3141class Code: public HeapObject {
3142 public:
3143 // Opaque data type for encapsulating code flags like kind, inline
3144 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003145 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3146 // enumeration type has correct value range (see Issue 830 for more details).
3147 enum Flags {
3148 FLAGS_MIN_VALUE = kMinInt,
3149 FLAGS_MAX_VALUE = kMaxInt
3150 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003151
3152 enum Kind {
3153 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003154 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003155 STUB,
3156 BUILTIN,
3157 LOAD_IC,
3158 KEYED_LOAD_IC,
3159 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003160 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003161 STORE_IC,
3162 KEYED_STORE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003163 BINARY_OP_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003164 TYPE_RECORDING_BINARY_OP_IC,
3165 COMPARE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003166 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003167 // Flags.
3168
3169 // Pseudo-kinds.
3170 REGEXP = BUILTIN,
3171 FIRST_IC_KIND = LOAD_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003172 LAST_IC_KIND = COMPARE_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003173 };
3174
3175 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003176 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003177 };
3178
Ben Murdochb8e0da22011-05-16 14:20:40 +01003179 typedef int ExtraICState;
3180
3181 static const ExtraICState kNoExtraICState = 0;
3182
Steve Blocka7e24c12009-10-30 11:49:00 +00003183#ifdef ENABLE_DISASSEMBLER
3184 // Printing
3185 static const char* Kind2String(Kind kind);
3186 static const char* ICState2String(InlineCacheState state);
3187 static const char* PropertyType2String(PropertyType type);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003188 inline void Disassemble(const char* name) {
3189 Disassemble(name, stdout);
3190 }
3191 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003192#endif // ENABLE_DISASSEMBLER
3193
3194 // [instruction_size]: Size of the native instructions
3195 inline int instruction_size();
3196 inline void set_instruction_size(int value);
3197
Leon Clarkeac952652010-07-15 11:15:24 +01003198 // [relocation_info]: Code relocation information
3199 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003200 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003201
Ben Murdochb0fe1622011-05-05 13:52:32 +01003202 // [deoptimization_data]: Array containing data for deopt.
3203 DECL_ACCESSORS(deoptimization_data, FixedArray)
3204
3205 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003206 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003207 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003208
Steve Blocka7e24c12009-10-30 11:49:00 +00003209 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003210
Steve Blocka7e24c12009-10-30 11:49:00 +00003211 // [flags]: Various code flags.
3212 inline Flags flags();
3213 inline void set_flags(Flags flags);
3214
3215 // [flags]: Access to specific code flags.
3216 inline Kind kind();
3217 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003218 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00003219 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
3220 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3221 inline int arguments_count(); // Only valid for call IC stubs.
3222
3223 // Testers for IC stub kinds.
3224 inline bool is_inline_cache_stub();
3225 inline bool is_load_stub() { return kind() == LOAD_IC; }
3226 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3227 inline bool is_store_stub() { return kind() == STORE_IC; }
3228 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3229 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003230 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003231 inline bool is_binary_op_stub() { return kind() == BINARY_OP_IC; }
3232 inline bool is_type_recording_binary_op_stub() {
3233 return kind() == TYPE_RECORDING_BINARY_OP_IC;
3234 }
3235 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00003236
Steve Block6ded16b2010-05-10 14:33:55 +01003237 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003238 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003239 inline void set_major_key(int value);
3240
3241 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3242 inline bool optimizable();
3243 inline void set_optimizable(bool value);
3244
3245 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3246 // deoptimization support.
3247 inline bool has_deoptimization_support();
3248 inline void set_has_deoptimization_support(bool value);
3249
3250 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3251 // how long the function has been marked for OSR and therefore which
3252 // level of loop nesting we are willing to do on-stack replacement
3253 // for.
3254 inline void set_allow_osr_at_loop_nesting_level(int level);
3255 inline int allow_osr_at_loop_nesting_level();
3256
3257 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3258 // reserved in the code prologue.
3259 inline unsigned stack_slots();
3260 inline void set_stack_slots(unsigned slots);
3261
3262 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3263 // the instruction stream where the safepoint table starts.
3264 inline unsigned safepoint_table_start();
3265 inline void set_safepoint_table_start(unsigned offset);
3266
3267 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3268 // instruction stream where the stack check table starts.
3269 inline unsigned stack_check_table_start();
3270 inline void set_stack_check_table_start(unsigned offset);
3271
3272 // [check type]: For kind CALL_IC, tells how to check if the
3273 // receiver is valid for the given call.
3274 inline CheckType check_type();
3275 inline void set_check_type(CheckType value);
3276
3277 // [binary op type]: For all BINARY_OP_IC.
3278 inline byte binary_op_type();
3279 inline void set_binary_op_type(byte value);
3280
3281 // [type-recording binary op type]: For all TYPE_RECORDING_BINARY_OP_IC.
3282 inline byte type_recording_binary_op_type();
3283 inline void set_type_recording_binary_op_type(byte value);
3284 inline byte type_recording_binary_op_result_type();
3285 inline void set_type_recording_binary_op_result_type(byte value);
3286
3287 // [compare state]: For kind compare IC stubs, tells what state the
3288 // stub is in.
3289 inline byte compare_state();
3290 inline void set_compare_state(byte value);
3291
Ben Murdochb8e0da22011-05-16 14:20:40 +01003292 // Get the safepoint entry for the given pc.
3293 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003294
3295 // Mark this code object as not having a stack check table. Assumes kind
3296 // is FUNCTION.
3297 void SetNoStackCheckTable();
3298
3299 // Find the first map in an IC stub.
3300 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003301
3302 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003303 static inline Flags ComputeFlags(
3304 Kind kind,
3305 InLoopFlag in_loop = NOT_IN_LOOP,
3306 InlineCacheState ic_state = UNINITIALIZED,
3307 ExtraICState extra_ic_state = kNoExtraICState,
3308 PropertyType type = NORMAL,
3309 int argc = -1,
3310 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003311
3312 static inline Flags ComputeMonomorphicFlags(
3313 Kind kind,
3314 PropertyType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003315 ExtraICState extra_ic_state = kNoExtraICState,
Steve Block8defd9f2010-07-08 12:39:36 +01003316 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003317 InLoopFlag in_loop = NOT_IN_LOOP,
3318 int argc = -1);
3319
3320 static inline Kind ExtractKindFromFlags(Flags flags);
3321 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003322 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003323 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
3324 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3325 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003326 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003327 static inline Flags RemoveTypeFromFlags(Flags flags);
3328
3329 // Convert a target address into a code object.
3330 static inline Code* GetCodeFromTargetAddress(Address address);
3331
Steve Block791712a2010-08-27 10:21:07 +01003332 // Convert an entry address into an object.
3333 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3334
Steve Blocka7e24c12009-10-30 11:49:00 +00003335 // Returns the address of the first instruction.
3336 inline byte* instruction_start();
3337
Leon Clarkeac952652010-07-15 11:15:24 +01003338 // Returns the address right after the last instruction.
3339 inline byte* instruction_end();
3340
Steve Blocka7e24c12009-10-30 11:49:00 +00003341 // Returns the size of the instructions, padding, and relocation information.
3342 inline int body_size();
3343
3344 // Returns the address of the first relocation info (read backwards!).
3345 inline byte* relocation_start();
3346
3347 // Code entry point.
3348 inline byte* entry();
3349
3350 // Returns true if pc is inside this object's instructions.
3351 inline bool contains(byte* pc);
3352
Steve Blocka7e24c12009-10-30 11:49:00 +00003353 // Relocate the code by delta bytes. Called to signal that this code
3354 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003355 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003356
3357 // Migrate code described by desc.
3358 void CopyFrom(const CodeDesc& desc);
3359
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003360 // Returns the object size for a given body (used for allocation).
3361 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003362 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003363 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003364 }
3365
3366 // Calculate the size of the code object to report for log events. This takes
3367 // the layout of the code object into account.
3368 int ExecutableSize() {
3369 // Check that the assumptions about the layout of the code object holds.
3370 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3371 Code::kHeaderSize);
3372 return instruction_size() + Code::kHeaderSize;
3373 }
3374
3375 // Locating source position.
3376 int SourcePosition(Address pc);
3377 int SourceStatementPosition(Address pc);
3378
3379 // Casting.
3380 static inline Code* cast(Object* obj);
3381
3382 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003383 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003384 inline void CodeIterateBody(ObjectVisitor* v);
3385
3386 template<typename StaticVisitor>
3387 inline void CodeIterateBody();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003388#ifdef OBJECT_PRINT
3389 inline void CodePrint() {
3390 CodePrint(stdout);
3391 }
3392 void CodePrint(FILE* out);
3393#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003394#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003395 void CodeVerify();
3396#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003397
3398 // Max loop nesting marker used to postpose OSR. We don't take loop
3399 // nesting that is deeper than 5 levels into account.
3400 static const int kMaxLoopNestingMarker = 6;
3401
Steve Blocka7e24c12009-10-30 11:49:00 +00003402 // Layout description.
3403 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003404 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003405 static const int kDeoptimizationDataOffset =
3406 kRelocationInfoOffset + kPointerSize;
3407 static const int kFlagsOffset = kDeoptimizationDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003408 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003409
3410 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3411
3412 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3413 kKindSpecificFlagsSize;
3414
Steve Blocka7e24c12009-10-30 11:49:00 +00003415 // Add padding to align the instruction start following right after
3416 // the Code object header.
3417 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003418 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003419
3420 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003421 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3422 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3423 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3424 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3425
3426 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3427 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
3428 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3429
3430 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3431 static const int kAllowOSRAtLoopNestingLevelOffset =
3432 kHasDeoptimizationSupportOffset + 1;
3433
3434 static const int kSafepointTableStartOffset = kStackSlotsOffset + kIntSize;
3435 static const int kStackCheckTableStartOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003436
3437 // Flags layout.
3438 static const int kFlagsICStateShift = 0;
3439 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003440 static const int kFlagsTypeShift = 4;
3441 static const int kFlagsKindShift = 7;
Steve Block8defd9f2010-07-08 12:39:36 +01003442 static const int kFlagsICHolderShift = 11;
Ben Murdochb8e0da22011-05-16 14:20:40 +01003443 static const int kFlagsExtraICStateShift = 12;
3444 static const int kFlagsArgumentsCountShift = 14;
Steve Blocka7e24c12009-10-30 11:49:00 +00003445
Steve Block6ded16b2010-05-10 14:33:55 +01003446 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3447 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003448 static const int kFlagsTypeMask = 0x00000070; // 00001110000
3449 static const int kFlagsKindMask = 0x00000780; // 11110000000
Steve Block8defd9f2010-07-08 12:39:36 +01003450 static const int kFlagsCacheInPrototypeMapMask = 0x00000800;
Ben Murdochb8e0da22011-05-16 14:20:40 +01003451 static const int kFlagsExtraICStateMask = 0x00003000;
3452 static const int kFlagsArgumentsCountMask = 0xFFFFC000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003453
3454 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003455 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003456
3457 private:
3458 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3459};
3460
3461
3462// All heap objects have a Map that describes their structure.
3463// A Map contains information about:
3464// - Size information about the object
3465// - How to iterate over an object (for garbage collection)
3466class Map: public HeapObject {
3467 public:
3468 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003469 // Size in bytes or kVariableSizeSentinel if instances do not have
3470 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003471 inline int instance_size();
3472 inline void set_instance_size(int value);
3473
3474 // Count of properties allocated in the object.
3475 inline int inobject_properties();
3476 inline void set_inobject_properties(int value);
3477
3478 // Count of property fields pre-allocated in the object when first allocated.
3479 inline int pre_allocated_property_fields();
3480 inline void set_pre_allocated_property_fields(int value);
3481
3482 // Instance type.
3483 inline InstanceType instance_type();
3484 inline void set_instance_type(InstanceType value);
3485
3486 // Tells how many unused property fields are available in the
3487 // instance (only used for JSObject in fast mode).
3488 inline int unused_property_fields();
3489 inline void set_unused_property_fields(int value);
3490
3491 // Bit field.
3492 inline byte bit_field();
3493 inline void set_bit_field(byte value);
3494
3495 // Bit field 2.
3496 inline byte bit_field2();
3497 inline void set_bit_field2(byte value);
3498
3499 // Tells whether the object in the prototype property will be used
3500 // for instances created from this function. If the prototype
3501 // property is set to a value that is not a JSObject, the prototype
3502 // property will not be used to create instances of the function.
3503 // See ECMA-262, 13.2.2.
3504 inline void set_non_instance_prototype(bool value);
3505 inline bool has_non_instance_prototype();
3506
Steve Block6ded16b2010-05-10 14:33:55 +01003507 // Tells whether function has special prototype property. If not, prototype
3508 // property will not be created when accessed (will return undefined),
3509 // and construction from this function will not be allowed.
3510 inline void set_function_with_prototype(bool value);
3511 inline bool function_with_prototype();
3512
Steve Blocka7e24c12009-10-30 11:49:00 +00003513 // Tells whether the instance with this map should be ignored by the
3514 // __proto__ accessor.
3515 inline void set_is_hidden_prototype() {
3516 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3517 }
3518
3519 inline bool is_hidden_prototype() {
3520 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3521 }
3522
3523 // Records and queries whether the instance has a named interceptor.
3524 inline void set_has_named_interceptor() {
3525 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3526 }
3527
3528 inline bool has_named_interceptor() {
3529 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3530 }
3531
3532 // Records and queries whether the instance has an indexed interceptor.
3533 inline void set_has_indexed_interceptor() {
3534 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3535 }
3536
3537 inline bool has_indexed_interceptor() {
3538 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3539 }
3540
3541 // Tells whether the instance is undetectable.
3542 // An undetectable object is a special class of JSObject: 'typeof' operator
3543 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3544 // a normal JS object. It is useful for implementing undetectable
3545 // document.all in Firefox & Safari.
3546 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3547 inline void set_is_undetectable() {
3548 set_bit_field(bit_field() | (1 << kIsUndetectable));
3549 }
3550
3551 inline bool is_undetectable() {
3552 return ((1 << kIsUndetectable) & bit_field()) != 0;
3553 }
3554
Steve Blocka7e24c12009-10-30 11:49:00 +00003555 // Tells whether the instance has a call-as-function handler.
3556 inline void set_has_instance_call_handler() {
3557 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3558 }
3559
3560 inline bool has_instance_call_handler() {
3561 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3562 }
3563
Steve Block8defd9f2010-07-08 12:39:36 +01003564 inline void set_is_extensible(bool value);
3565 inline bool is_extensible();
3566
3567 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003568 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3569 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003570 if (value) {
3571 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3572 } else {
3573 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3574 }
Leon Clarkee46be812010-01-19 14:06:41 +00003575 }
3576
Iain Merrick75681382010-08-19 15:07:18 +01003577 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003578 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003579 }
3580
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003581 // Tells whether the map is attached to SharedFunctionInfo
3582 // (for inobject slack tracking).
3583 inline void set_attached_to_shared_function_info(bool value);
3584
3585 inline bool attached_to_shared_function_info();
3586
3587 // Tells whether the map is shared between objects that may have different
3588 // behavior. If true, the map should never be modified, instead a clone
3589 // should be created and modified.
3590 inline void set_is_shared(bool value);
3591
3592 inline bool is_shared();
3593
Steve Blocka7e24c12009-10-30 11:49:00 +00003594 // Tells whether the instance needs security checks when accessing its
3595 // properties.
3596 inline void set_is_access_check_needed(bool access_check_needed);
3597 inline bool is_access_check_needed();
3598
3599 // [prototype]: implicit prototype object.
3600 DECL_ACCESSORS(prototype, Object)
3601
3602 // [constructor]: points back to the function responsible for this map.
3603 DECL_ACCESSORS(constructor, Object)
3604
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003605 inline JSFunction* unchecked_constructor();
3606
Steve Blocka7e24c12009-10-30 11:49:00 +00003607 // [instance descriptors]: describes the object.
3608 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3609
3610 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003611 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003612
Ben Murdochb0fe1622011-05-05 13:52:32 +01003613 // Lookup in the map's instance descriptors and fill out the result
3614 // with the given holder if the name is found. The holder may be
3615 // NULL when this function is used from the compiler.
3616 void LookupInDescriptors(JSObject* holder,
3617 String* name,
3618 LookupResult* result);
3619
John Reck59135872010-11-02 12:39:01 -07003620 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003621
John Reck59135872010-11-02 12:39:01 -07003622 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3623 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00003624
3625 // Returns a copy of the map, with all transitions dropped from the
3626 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07003627 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00003628
Steve Block8defd9f2010-07-08 12:39:36 +01003629 // Returns this map if it has the fast elements bit set, otherwise
3630 // returns a copy of the map, with all transitions dropped from the
3631 // descriptors and the fast elements bit set.
John Reck59135872010-11-02 12:39:01 -07003632 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003633
3634 // Returns this map if it has the fast elements bit cleared,
3635 // otherwise returns a copy of the map, with all transitions dropped
3636 // from the descriptors and the fast elements bit cleared.
John Reck59135872010-11-02 12:39:01 -07003637 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003638
Steve Blocka7e24c12009-10-30 11:49:00 +00003639 // Returns the property index for name (only valid for FAST MODE).
3640 int PropertyIndexFor(String* name);
3641
3642 // Returns the next free property index (only valid for FAST MODE).
3643 int NextFreePropertyIndex();
3644
3645 // Returns the number of properties described in instance_descriptors.
3646 int NumberOfDescribedProperties();
3647
3648 // Casting.
3649 static inline Map* cast(Object* obj);
3650
3651 // Locate an accessor in the instance descriptor.
3652 AccessorDescriptor* FindAccessor(String* name);
3653
3654 // Code cache operations.
3655
3656 // Clears the code cache.
3657 inline void ClearCodeCache();
3658
3659 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07003660 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003661
3662 // Returns the found code or undefined if absent.
3663 Object* FindInCodeCache(String* name, Code::Flags flags);
3664
3665 // Returns the non-negative index of the code object if it is in the
3666 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003667 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003668
3669 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003670 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003671
3672 // For every transition in this map, makes the transition's
3673 // target's prototype pointer point back to this map.
3674 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3675 void CreateBackPointers();
3676
3677 // Set all map transitions from this map to dead maps to null.
3678 // Also, restore the original prototype on the targets of these
3679 // transitions, so that we do not process this map again while
3680 // following back pointers.
3681 void ClearNonLiveTransitions(Object* real_prototype);
3682
3683 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003684#ifdef OBJECT_PRINT
3685 inline void MapPrint() {
3686 MapPrint(stdout);
3687 }
3688 void MapPrint(FILE* out);
3689#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003690#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003691 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003692 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00003693#endif
3694
Iain Merrick75681382010-08-19 15:07:18 +01003695 inline int visitor_id();
3696 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003697
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003698 typedef void (*TraverseCallback)(Map* map, void* data);
3699
3700 void TraverseTransitionTree(TraverseCallback callback, void* data);
3701
Steve Blocka7e24c12009-10-30 11:49:00 +00003702 static const int kMaxPreAllocatedPropertyFields = 255;
3703
3704 // Layout description.
3705 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3706 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3707 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3708 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3709 static const int kInstanceDescriptorsOffset =
3710 kConstructorOffset + kPointerSize;
3711 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003712 static const int kPadStart = kCodeCacheOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003713 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3714
3715 // Layout of pointer fields. Heap iteration code relies on them
3716 // being continiously allocated.
3717 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3718 static const int kPointerFieldsEndOffset =
3719 Map::kCodeCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003720
3721 // Byte offsets within kInstanceSizesOffset.
3722 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3723 static const int kInObjectPropertiesByte = 1;
3724 static const int kInObjectPropertiesOffset =
3725 kInstanceSizesOffset + kInObjectPropertiesByte;
3726 static const int kPreAllocatedPropertyFieldsByte = 2;
3727 static const int kPreAllocatedPropertyFieldsOffset =
3728 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003729 static const int kVisitorIdByte = 3;
3730 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00003731
3732 // Byte offsets within kInstanceAttributesOffset attributes.
3733 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3734 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3735 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3736 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3737
3738 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3739
3740 // Bit positions for bit field.
3741 static const int kUnused = 0; // To be used for marking recently used maps.
3742 static const int kHasNonInstancePrototype = 1;
3743 static const int kIsHiddenPrototype = 2;
3744 static const int kHasNamedInterceptor = 3;
3745 static const int kHasIndexedInterceptor = 4;
3746 static const int kIsUndetectable = 5;
3747 static const int kHasInstanceCallHandler = 6;
3748 static const int kIsAccessCheckNeeded = 7;
3749
3750 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003751 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003752 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003753 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003754 static const int kStringWrapperSafeForDefaultValueOf = 3;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003755 static const int kAttachedToSharedFunctionInfo = 4;
3756 static const int kIsShared = 5;
Steve Block6ded16b2010-05-10 14:33:55 +01003757
3758 // Layout of the default cache. It holds alternating name and code objects.
3759 static const int kCodeCacheEntrySize = 2;
3760 static const int kCodeCacheEntryNameOffset = 0;
3761 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003762
Iain Merrick75681382010-08-19 15:07:18 +01003763 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3764 kPointerFieldsEndOffset,
3765 kSize> BodyDescriptor;
3766
Steve Blocka7e24c12009-10-30 11:49:00 +00003767 private:
3768 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3769};
3770
3771
3772// An abstract superclass, a marker class really, for simple structure classes.
3773// It doesn't carry much functionality but allows struct classes to me
3774// identified in the type system.
3775class Struct: public HeapObject {
3776 public:
3777 inline void InitializeBody(int object_size);
3778 static inline Struct* cast(Object* that);
3779};
3780
3781
3782// Script describes a script which has been added to the VM.
3783class Script: public Struct {
3784 public:
3785 // Script types.
3786 enum Type {
3787 TYPE_NATIVE = 0,
3788 TYPE_EXTENSION = 1,
3789 TYPE_NORMAL = 2
3790 };
3791
3792 // Script compilation types.
3793 enum CompilationType {
3794 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08003795 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003796 };
3797
3798 // [source]: the script source.
3799 DECL_ACCESSORS(source, Object)
3800
3801 // [name]: the script name.
3802 DECL_ACCESSORS(name, Object)
3803
3804 // [id]: the script id.
3805 DECL_ACCESSORS(id, Object)
3806
3807 // [line_offset]: script line offset in resource from where it was extracted.
3808 DECL_ACCESSORS(line_offset, Smi)
3809
3810 // [column_offset]: script column offset in resource from where it was
3811 // extracted.
3812 DECL_ACCESSORS(column_offset, Smi)
3813
3814 // [data]: additional data associated with this script.
3815 DECL_ACCESSORS(data, Object)
3816
3817 // [context_data]: context data for the context this script was compiled in.
3818 DECL_ACCESSORS(context_data, Object)
3819
3820 // [wrapper]: the wrapper cache.
3821 DECL_ACCESSORS(wrapper, Proxy)
3822
3823 // [type]: the script type.
3824 DECL_ACCESSORS(type, Smi)
3825
3826 // [compilation]: how the the script was compiled.
3827 DECL_ACCESSORS(compilation_type, Smi)
3828
Steve Blockd0582a62009-12-15 09:54:21 +00003829 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003830 DECL_ACCESSORS(line_ends, Object)
3831
Steve Blockd0582a62009-12-15 09:54:21 +00003832 // [eval_from_shared]: for eval scripts the shared funcion info for the
3833 // function from which eval was called.
3834 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003835
3836 // [eval_from_instructions_offset]: the instruction offset in the code for the
3837 // function from which eval was called where eval was called.
3838 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3839
3840 static inline Script* cast(Object* obj);
3841
Steve Block3ce2e202009-11-05 08:53:23 +00003842 // If script source is an external string, check that the underlying
3843 // resource is accessible. Otherwise, always return true.
3844 inline bool HasValidSource();
3845
Ben Murdochb0fe1622011-05-05 13:52:32 +01003846#ifdef OBJECT_PRINT
3847 inline void ScriptPrint() {
3848 ScriptPrint(stdout);
3849 }
3850 void ScriptPrint(FILE* out);
3851#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003852#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003853 void ScriptVerify();
3854#endif
3855
3856 static const int kSourceOffset = HeapObject::kHeaderSize;
3857 static const int kNameOffset = kSourceOffset + kPointerSize;
3858 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3859 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3860 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3861 static const int kContextOffset = kDataOffset + kPointerSize;
3862 static const int kWrapperOffset = kContextOffset + kPointerSize;
3863 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3864 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3865 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3866 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003867 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003868 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003869 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003870 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3871
3872 private:
3873 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3874};
3875
3876
Ben Murdochb0fe1622011-05-05 13:52:32 +01003877// List of builtin functions we want to identify to improve code
3878// generation.
3879//
3880// Each entry has a name of a global object property holding an object
3881// optionally followed by ".prototype", a name of a builtin function
3882// on the object (the one the id is set for), and a label.
3883//
3884// Installation of ids for the selected builtin functions is handled
3885// by the bootstrapper.
3886//
3887// NOTE: Order is important: math functions should be at the end of
3888// the list and MathFloor should be the first math function.
3889#define FUNCTIONS_WITH_ID_LIST(V) \
3890 V(Array.prototype, push, ArrayPush) \
3891 V(Array.prototype, pop, ArrayPop) \
3892 V(String.prototype, charCodeAt, StringCharCodeAt) \
3893 V(String.prototype, charAt, StringCharAt) \
3894 V(String, fromCharCode, StringFromCharCode) \
3895 V(Math, floor, MathFloor) \
3896 V(Math, round, MathRound) \
3897 V(Math, ceil, MathCeil) \
3898 V(Math, abs, MathAbs) \
3899 V(Math, log, MathLog) \
3900 V(Math, sin, MathSin) \
3901 V(Math, cos, MathCos) \
3902 V(Math, tan, MathTan) \
3903 V(Math, asin, MathASin) \
3904 V(Math, acos, MathACos) \
3905 V(Math, atan, MathATan) \
3906 V(Math, exp, MathExp) \
3907 V(Math, sqrt, MathSqrt) \
3908 V(Math, pow, MathPow)
3909
3910
3911enum BuiltinFunctionId {
3912#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
3913 k##name,
3914 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
3915#undef DECLARE_FUNCTION_ID
3916 // Fake id for a special case of Math.pow. Note, it continues the
3917 // list of math functions.
3918 kMathPowHalf,
3919 kFirstMathFunctionId = kMathFloor
3920};
3921
3922
Steve Blocka7e24c12009-10-30 11:49:00 +00003923// SharedFunctionInfo describes the JSFunction information that can be
3924// shared by multiple instances of the function.
3925class SharedFunctionInfo: public HeapObject {
3926 public:
3927 // [name]: Function name.
3928 DECL_ACCESSORS(name, Object)
3929
3930 // [code]: Function code.
3931 DECL_ACCESSORS(code, Code)
3932
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003933 // [scope_info]: Scope info.
3934 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
3935
Steve Blocka7e24c12009-10-30 11:49:00 +00003936 // [construct stub]: Code stub for constructing instances of this function.
3937 DECL_ACCESSORS(construct_stub, Code)
3938
Iain Merrick75681382010-08-19 15:07:18 +01003939 inline Code* unchecked_code();
3940
Steve Blocka7e24c12009-10-30 11:49:00 +00003941 // Returns if this function has been compiled to native code yet.
3942 inline bool is_compiled();
3943
3944 // [length]: The function length - usually the number of declared parameters.
3945 // Use up to 2^30 parameters.
3946 inline int length();
3947 inline void set_length(int value);
3948
3949 // [formal parameter count]: The declared number of parameters.
3950 inline int formal_parameter_count();
3951 inline void set_formal_parameter_count(int value);
3952
3953 // Set the formal parameter count so the function code will be
3954 // called without using argument adaptor frames.
3955 inline void DontAdaptArguments();
3956
3957 // [expected_nof_properties]: Expected number of properties for the function.
3958 inline int expected_nof_properties();
3959 inline void set_expected_nof_properties(int value);
3960
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003961 // Inobject slack tracking is the way to reclaim unused inobject space.
3962 //
3963 // The instance size is initially determined by adding some slack to
3964 // expected_nof_properties (to allow for a few extra properties added
3965 // after the constructor). There is no guarantee that the extra space
3966 // will not be wasted.
3967 //
3968 // Here is the algorithm to reclaim the unused inobject space:
3969 // - Detect the first constructor call for this SharedFunctionInfo.
3970 // When it happens enter the "in progress" state: remember the
3971 // constructor's initial_map and install a special construct stub that
3972 // counts constructor calls.
3973 // - While the tracking is in progress create objects filled with
3974 // one_pointer_filler_map instead of undefined_value. This way they can be
3975 // resized quickly and safely.
3976 // - Once enough (kGenerousAllocationCount) objects have been created
3977 // compute the 'slack' (traverse the map transition tree starting from the
3978 // initial_map and find the lowest value of unused_property_fields).
3979 // - Traverse the transition tree again and decrease the instance size
3980 // of every map. Existing objects will resize automatically (they are
3981 // filled with one_pointer_filler_map). All further allocations will
3982 // use the adjusted instance size.
3983 // - Decrease expected_nof_properties so that an allocations made from
3984 // another context will use the adjusted instance size too.
3985 // - Exit "in progress" state by clearing the reference to the initial_map
3986 // and setting the regular construct stub (generic or inline).
3987 //
3988 // The above is the main event sequence. Some special cases are possible
3989 // while the tracking is in progress:
3990 //
3991 // - GC occurs.
3992 // Check if the initial_map is referenced by any live objects (except this
3993 // SharedFunctionInfo). If it is, continue tracking as usual.
3994 // If it is not, clear the reference and reset the tracking state. The
3995 // tracking will be initiated again on the next constructor call.
3996 //
3997 // - The constructor is called from another context.
3998 // Immediately complete the tracking, perform all the necessary changes
3999 // to maps. This is necessary because there is no efficient way to track
4000 // multiple initial_maps.
4001 // Proceed to create an object in the current context (with the adjusted
4002 // size).
4003 //
4004 // - A different constructor function sharing the same SharedFunctionInfo is
4005 // called in the same context. This could be another closure in the same
4006 // context, or the first function could have been disposed.
4007 // This is handled the same way as the previous case.
4008 //
4009 // Important: inobject slack tracking is not attempted during the snapshot
4010 // creation.
4011
Ben Murdochf87a2032010-10-22 12:50:53 +01004012 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004013
4014 // [construction_count]: Counter for constructor calls made during
4015 // the tracking phase.
4016 inline int construction_count();
4017 inline void set_construction_count(int value);
4018
4019 // [initial_map]: initial map of the first function called as a constructor.
4020 // Saved for the duration of the tracking phase.
4021 // This is a weak link (GC resets it to undefined_value if no other live
4022 // object reference this map).
4023 DECL_ACCESSORS(initial_map, Object)
4024
4025 // True if the initial_map is not undefined and the countdown stub is
4026 // installed.
4027 inline bool IsInobjectSlackTrackingInProgress();
4028
4029 // Starts the tracking.
4030 // Stores the initial map and installs the countdown stub.
4031 // IsInobjectSlackTrackingInProgress is normally true after this call,
4032 // except when tracking have not been started (e.g. the map has no unused
4033 // properties or the snapshot is being built).
4034 void StartInobjectSlackTracking(Map* map);
4035
4036 // Completes the tracking.
4037 // IsInobjectSlackTrackingInProgress is false after this call.
4038 void CompleteInobjectSlackTracking();
4039
4040 // Clears the initial_map before the GC marking phase to ensure the reference
4041 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4042 void DetachInitialMap();
4043
4044 // Restores the link to the initial map after the GC marking phase.
4045 // IsInobjectSlackTrackingInProgress is true after this call.
4046 void AttachInitialMap(Map* map);
4047
4048 // False if there are definitely no live objects created from this function.
4049 // True if live objects _may_ exist (existence not guaranteed).
4050 // May go back from true to false after GC.
4051 inline bool live_objects_may_exist();
4052
4053 inline void set_live_objects_may_exist(bool value);
4054
Steve Blocka7e24c12009-10-30 11:49:00 +00004055 // [instance class name]: class name for instances.
4056 DECL_ACCESSORS(instance_class_name, Object)
4057
Steve Block6ded16b2010-05-10 14:33:55 +01004058 // [function data]: This field holds some additional data for function.
4059 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004060 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004061 // In the long run we don't want all functions to have this field but
4062 // we can fix that when we have a better model for storing hidden data
4063 // on objects.
4064 DECL_ACCESSORS(function_data, Object)
4065
Steve Block6ded16b2010-05-10 14:33:55 +01004066 inline bool IsApiFunction();
4067 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004068 inline bool HasBuiltinFunctionId();
4069 inline bool IsBuiltinMathFunction();
4070 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004071
Steve Blocka7e24c12009-10-30 11:49:00 +00004072 // [script info]: Script from which the function originates.
4073 DECL_ACCESSORS(script, Object)
4074
Steve Block6ded16b2010-05-10 14:33:55 +01004075 // [num_literals]: Number of literals used by this function.
4076 inline int num_literals();
4077 inline void set_num_literals(int value);
4078
Steve Blocka7e24c12009-10-30 11:49:00 +00004079 // [start_position_and_type]: Field used to store both the source code
4080 // position, whether or not the function is a function expression,
4081 // and whether or not the function is a toplevel function. The two
4082 // least significants bit indicates whether the function is an
4083 // expression and the rest contains the source code position.
4084 inline int start_position_and_type();
4085 inline void set_start_position_and_type(int value);
4086
4087 // [debug info]: Debug information.
4088 DECL_ACCESSORS(debug_info, Object)
4089
4090 // [inferred name]: Name inferred from variable or property
4091 // assignment of this function. Used to facilitate debugging and
4092 // profiling of JavaScript code written in OO style, where almost
4093 // all functions are anonymous but are assigned to object
4094 // properties.
4095 DECL_ACCESSORS(inferred_name, String)
4096
Ben Murdochf87a2032010-10-22 12:50:53 +01004097 // The function's name if it is non-empty, otherwise the inferred name.
4098 String* DebugName();
4099
Steve Blocka7e24c12009-10-30 11:49:00 +00004100 // Position of the 'function' token in the script source.
4101 inline int function_token_position();
4102 inline void set_function_token_position(int function_token_position);
4103
4104 // Position of this function in the script source.
4105 inline int start_position();
4106 inline void set_start_position(int start_position);
4107
4108 // End position of this function in the script source.
4109 inline int end_position();
4110 inline void set_end_position(int end_position);
4111
4112 // Is this function a function expression in the source code.
4113 inline bool is_expression();
4114 inline void set_is_expression(bool value);
4115
4116 // Is this function a top-level function (scripts, evals).
4117 inline bool is_toplevel();
4118 inline void set_is_toplevel(bool value);
4119
4120 // Bit field containing various information collected by the compiler to
4121 // drive optimization.
4122 inline int compiler_hints();
4123 inline void set_compiler_hints(int value);
4124
Ben Murdochb0fe1622011-05-05 13:52:32 +01004125 // A counter used to determine when to stress the deoptimizer with a
4126 // deopt.
4127 inline Smi* deopt_counter();
4128 inline void set_deopt_counter(Smi* counter);
4129
Steve Blocka7e24c12009-10-30 11:49:00 +00004130 // Add information on assignments of the form this.x = ...;
4131 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004132 bool has_only_simple_this_property_assignments,
4133 FixedArray* this_property_assignments);
4134
4135 // Clear information on assignments of the form this.x = ...;
4136 void ClearThisPropertyAssignmentsInfo();
4137
4138 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004139 // this.x = y; where y is either a constant or refers to an argument.
4140 inline bool has_only_simple_this_property_assignments();
4141
Leon Clarked91b9f72010-01-27 17:25:45 +00004142 inline bool try_full_codegen();
4143 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00004144
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004145 // Indicates if this function can be lazy compiled.
4146 // This is used to determine if we can safely flush code from a function
4147 // when doing GC if we expect that the function will no longer be used.
4148 inline bool allows_lazy_compilation();
4149 inline void set_allows_lazy_compilation(bool flag);
4150
Iain Merrick75681382010-08-19 15:07:18 +01004151 // Indicates how many full GCs this function has survived with assigned
4152 // code object. Used to determine when it is relatively safe to flush
4153 // this code object and replace it with lazy compilation stub.
4154 // Age is reset when GC notices that the code object is referenced
4155 // from the stack or compilation cache.
4156 inline int code_age();
4157 inline void set_code_age(int age);
4158
Ben Murdochb0fe1622011-05-05 13:52:32 +01004159 // Indicates whether optimizations have been disabled for this
4160 // shared function info. If a function is repeatedly optimized or if
4161 // we cannot optimize the function we disable optimization to avoid
4162 // spending time attempting to optimize it again.
4163 inline bool optimization_disabled();
4164 inline void set_optimization_disabled(bool value);
4165
4166 // Indicates whether or not the code in the shared function support
4167 // deoptimization.
4168 inline bool has_deoptimization_support();
4169
4170 // Enable deoptimization support through recompiled code.
4171 void EnableDeoptimizationSupport(Code* recompiled);
4172
4173 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4174 // code, returns whether it asserted (i.e., always true if assertions are
4175 // disabled).
4176 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004177
Andrei Popescu402d9372010-02-26 13:31:12 +00004178 // Check whether a inlined constructor can be generated with the given
4179 // prototype.
4180 bool CanGenerateInlineConstructor(Object* prototype);
4181
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004182 // Prevents further attempts to generate inline constructors.
4183 // To be called if generation failed for any reason.
4184 void ForbidInlineConstructor();
4185
Steve Blocka7e24c12009-10-30 11:49:00 +00004186 // For functions which only contains this property assignments this provides
4187 // access to the names for the properties assigned.
4188 DECL_ACCESSORS(this_property_assignments, Object)
4189 inline int this_property_assignments_count();
4190 inline void set_this_property_assignments_count(int value);
4191 String* GetThisPropertyAssignmentName(int index);
4192 bool IsThisPropertyAssignmentArgument(int index);
4193 int GetThisPropertyAssignmentArgument(int index);
4194 Object* GetThisPropertyAssignmentConstant(int index);
4195
4196 // [source code]: Source code for the function.
4197 bool HasSourceCode();
4198 Object* GetSourceCode();
4199
Ben Murdochb0fe1622011-05-05 13:52:32 +01004200 inline int opt_count();
4201 inline void set_opt_count(int opt_count);
4202
4203 // Source size of this function.
4204 int SourceSize();
4205
Steve Blocka7e24c12009-10-30 11:49:00 +00004206 // Calculate the instance size.
4207 int CalculateInstanceSize();
4208
4209 // Calculate the number of in-object properties.
4210 int CalculateInObjectProperties();
4211
4212 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004213 // Set max_length to -1 for unlimited length.
4214 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004215#ifdef OBJECT_PRINT
4216 inline void SharedFunctionInfoPrint() {
4217 SharedFunctionInfoPrint(stdout);
4218 }
4219 void SharedFunctionInfoPrint(FILE* out);
4220#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004221#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004222 void SharedFunctionInfoVerify();
4223#endif
4224
4225 // Casting.
4226 static inline SharedFunctionInfo* cast(Object* obj);
4227
4228 // Constants.
4229 static const int kDontAdaptArgumentsSentinel = -1;
4230
4231 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004232 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004233 static const int kNameOffset = HeapObject::kHeaderSize;
4234 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004235 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4236 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004237 static const int kInstanceClassNameOffset =
4238 kConstructStubOffset + kPointerSize;
4239 static const int kFunctionDataOffset =
4240 kInstanceClassNameOffset + kPointerSize;
4241 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4242 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4243 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004244 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004245 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004246 static const int kThisPropertyAssignmentsOffset =
4247 kInitialMapOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004248 static const int kDeoptCounterOffset =
4249 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004250#if V8_HOST_ARCH_32_BIT
4251 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004252 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004253 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004254 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4255 static const int kExpectedNofPropertiesOffset =
4256 kFormalParameterCountOffset + kPointerSize;
4257 static const int kNumLiteralsOffset =
4258 kExpectedNofPropertiesOffset + kPointerSize;
4259 static const int kStartPositionAndTypeOffset =
4260 kNumLiteralsOffset + kPointerSize;
4261 static const int kEndPositionOffset =
4262 kStartPositionAndTypeOffset + kPointerSize;
4263 static const int kFunctionTokenPositionOffset =
4264 kEndPositionOffset + kPointerSize;
4265 static const int kCompilerHintsOffset =
4266 kFunctionTokenPositionOffset + kPointerSize;
4267 static const int kThisPropertyAssignmentsCountOffset =
4268 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004269 static const int kOptCountOffset =
4270 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004271 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004272 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004273#else
4274 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004275 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004276 // garbage collections.
4277 // To avoid wasting space on 64-bit architectures we use
4278 // the following trick: we group integer fields into pairs
4279 // First integer in each pair is shifted left by 1.
4280 // By doing this we guarantee that LSB of each kPointerSize aligned
4281 // word is not set and thus this word cannot be treated as pointer
4282 // to HeapObject during old space traversal.
4283 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004284 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004285 static const int kFormalParameterCountOffset =
4286 kLengthOffset + kIntSize;
4287
Steve Blocka7e24c12009-10-30 11:49:00 +00004288 static const int kExpectedNofPropertiesOffset =
4289 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004290 static const int kNumLiteralsOffset =
4291 kExpectedNofPropertiesOffset + kIntSize;
4292
4293 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004294 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004295 static const int kStartPositionAndTypeOffset =
4296 kEndPositionOffset + kIntSize;
4297
4298 static const int kFunctionTokenPositionOffset =
4299 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004300 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004301 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004302
Steve Blocka7e24c12009-10-30 11:49:00 +00004303 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004304 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004305 static const int kOptCountOffset =
4306 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004307
Steve Block6ded16b2010-05-10 14:33:55 +01004308 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004309 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004310
4311#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004312
4313 // The construction counter for inobject slack tracking is stored in the
4314 // most significant byte of compiler_hints which is otherwise unused.
4315 // Its offset depends on the endian-ness of the architecture.
4316#if __BYTE_ORDER == __LITTLE_ENDIAN
4317 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4318#elif __BYTE_ORDER == __BIG_ENDIAN
4319 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4320#else
4321#error Unknown byte ordering
4322#endif
4323
Steve Block6ded16b2010-05-10 14:33:55 +01004324 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004325
Iain Merrick75681382010-08-19 15:07:18 +01004326 typedef FixedBodyDescriptor<kNameOffset,
4327 kThisPropertyAssignmentsOffset + kPointerSize,
4328 kSize> BodyDescriptor;
4329
Steve Blocka7e24c12009-10-30 11:49:00 +00004330 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00004331 // Bit positions in start_position_and_type.
4332 // The source code start position is in the 30 most significant bits of
4333 // the start_position_and_type field.
4334 static const int kIsExpressionBit = 0;
4335 static const int kIsTopLevelBit = 1;
4336 static const int kStartPositionShift = 2;
4337 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4338
4339 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00004340 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00004341 static const int kTryFullCodegen = 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004342 static const int kAllowLazyCompilation = 2;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004343 static const int kLiveObjectsMayExist = 3;
4344 static const int kCodeAgeShift = 4;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004345 static const int kCodeAgeMask = 0x7;
4346 static const int kOptimizationDisabled = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +00004347
4348 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4349};
4350
4351
4352// JSFunction describes JavaScript functions.
4353class JSFunction: public JSObject {
4354 public:
4355 // [prototype_or_initial_map]:
4356 DECL_ACCESSORS(prototype_or_initial_map, Object)
4357
4358 // [shared_function_info]: The information about the function that
4359 // can be shared by instances.
4360 DECL_ACCESSORS(shared, SharedFunctionInfo)
4361
Iain Merrick75681382010-08-19 15:07:18 +01004362 inline SharedFunctionInfo* unchecked_shared();
4363
Steve Blocka7e24c12009-10-30 11:49:00 +00004364 // [context]: The context for this function.
4365 inline Context* context();
4366 inline Object* unchecked_context();
4367 inline void set_context(Object* context);
4368
4369 // [code]: The generated code object for this function. Executed
4370 // when the function is invoked, e.g. foo() or new foo(). See
4371 // [[Call]] and [[Construct]] description in ECMA-262, section
4372 // 8.6.2, page 27.
4373 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004374 inline void set_code(Code* code);
4375 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004376
Iain Merrick75681382010-08-19 15:07:18 +01004377 inline Code* unchecked_code();
4378
Steve Blocka7e24c12009-10-30 11:49:00 +00004379 // Tells whether this function is builtin.
4380 inline bool IsBuiltin();
4381
Ben Murdochb0fe1622011-05-05 13:52:32 +01004382 // Tells whether or not the function needs arguments adaption.
4383 inline bool NeedsArgumentsAdaption();
4384
4385 // Tells whether or not this function has been optimized.
4386 inline bool IsOptimized();
4387
4388 // Mark this function for lazy recompilation. The function will be
4389 // recompiled the next time it is executed.
4390 void MarkForLazyRecompilation();
4391
4392 // Tells whether or not the function is already marked for lazy
4393 // recompilation.
4394 inline bool IsMarkedForLazyRecompilation();
4395
4396 // Compute a hash code for the source code of this function.
4397 uint32_t SourceHash();
4398
4399 // Check whether or not this function is inlineable.
4400 bool IsInlineable();
4401
Steve Blocka7e24c12009-10-30 11:49:00 +00004402 // [literals]: Fixed array holding the materialized literals.
4403 //
4404 // If the function contains object, regexp or array literals, the
4405 // literals array prefix contains the object, regexp, and array
4406 // function to be used when creating these literals. This is
4407 // necessary so that we do not dynamically lookup the object, regexp
4408 // or array functions. Performing a dynamic lookup, we might end up
4409 // using the functions from a new context that we should not have
4410 // access to.
4411 DECL_ACCESSORS(literals, FixedArray)
4412
4413 // The initial map for an object created by this constructor.
4414 inline Map* initial_map();
4415 inline void set_initial_map(Map* value);
4416 inline bool has_initial_map();
4417
4418 // Get and set the prototype property on a JSFunction. If the
4419 // function has an initial map the prototype is set on the initial
4420 // map. Otherwise, the prototype is put in the initial map field
4421 // until an initial map is needed.
4422 inline bool has_prototype();
4423 inline bool has_instance_prototype();
4424 inline Object* prototype();
4425 inline Object* instance_prototype();
4426 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07004427 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004428
Steve Block6ded16b2010-05-10 14:33:55 +01004429 // After prototype is removed, it will not be created when accessed, and
4430 // [[Construct]] from this function will not be allowed.
4431 Object* RemovePrototype();
4432 inline bool should_have_prototype();
4433
Steve Blocka7e24c12009-10-30 11:49:00 +00004434 // Accessor for this function's initial map's [[class]]
4435 // property. This is primarily used by ECMA native functions. This
4436 // method sets the class_name field of this function's initial map
4437 // to a given value. It creates an initial map if this function does
4438 // not have one. Note that this method does not copy the initial map
4439 // if it has one already, but simply replaces it with the new value.
4440 // Instances created afterwards will have a map whose [[class]] is
4441 // set to 'value', but there is no guarantees on instances created
4442 // before.
4443 Object* SetInstanceClassName(String* name);
4444
4445 // Returns if this function has been compiled to native code yet.
4446 inline bool is_compiled();
4447
Ben Murdochb0fe1622011-05-05 13:52:32 +01004448 // [next_function_link]: Field for linking functions. This list is treated as
4449 // a weak list by the GC.
4450 DECL_ACCESSORS(next_function_link, Object)
4451
4452 // Prints the name of the function using PrintF.
4453 inline void PrintName() {
4454 PrintName(stdout);
4455 }
4456 void PrintName(FILE* out);
4457
Steve Blocka7e24c12009-10-30 11:49:00 +00004458 // Casting.
4459 static inline JSFunction* cast(Object* obj);
4460
Steve Block791712a2010-08-27 10:21:07 +01004461 // Iterates the objects, including code objects indirectly referenced
4462 // through pointers to the first instruction in the code object.
4463 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
4464
Steve Blocka7e24c12009-10-30 11:49:00 +00004465 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004466#ifdef OBJECT_PRINT
4467 inline void JSFunctionPrint() {
4468 JSFunctionPrint(stdout);
4469 }
4470 void JSFunctionPrint(FILE* out);
4471#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004472#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004473 void JSFunctionVerify();
4474#endif
4475
4476 // Returns the number of allocated literals.
4477 inline int NumberOfLiterals();
4478
4479 // Retrieve the global context from a function's literal array.
4480 static Context* GlobalContextFromLiterals(FixedArray* literals);
4481
Ben Murdochb0fe1622011-05-05 13:52:32 +01004482 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
4483 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01004484 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01004485 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01004486 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004487 static const int kSharedFunctionInfoOffset =
4488 kPrototypeOrInitialMapOffset + kPointerSize;
4489 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
4490 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004491 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
4492 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
4493 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004494
4495 // Layout of the literals array.
4496 static const int kLiteralsPrefixSize = 1;
4497 static const int kLiteralGlobalContextIndex = 0;
4498 private:
4499 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
4500};
4501
4502
4503// JSGlobalProxy's prototype must be a JSGlobalObject or null,
4504// and the prototype is hidden. JSGlobalProxy always delegates
4505// property accesses to its prototype if the prototype is not null.
4506//
4507// A JSGlobalProxy can be reinitialized which will preserve its identity.
4508//
4509// Accessing a JSGlobalProxy requires security check.
4510
4511class JSGlobalProxy : public JSObject {
4512 public:
4513 // [context]: the owner global context of this proxy object.
4514 // It is null value if this object is not used by any context.
4515 DECL_ACCESSORS(context, Object)
4516
4517 // Casting.
4518 static inline JSGlobalProxy* cast(Object* obj);
4519
4520 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004521#ifdef OBJECT_PRINT
4522 inline void JSGlobalProxyPrint() {
4523 JSGlobalProxyPrint(stdout);
4524 }
4525 void JSGlobalProxyPrint(FILE* out);
4526#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004527#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004528 void JSGlobalProxyVerify();
4529#endif
4530
4531 // Layout description.
4532 static const int kContextOffset = JSObject::kHeaderSize;
4533 static const int kSize = kContextOffset + kPointerSize;
4534
4535 private:
4536
4537 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
4538};
4539
4540
4541// Forward declaration.
4542class JSBuiltinsObject;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004543class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00004544
4545// Common super class for JavaScript global objects and the special
4546// builtins global objects.
4547class GlobalObject: public JSObject {
4548 public:
4549 // [builtins]: the object holding the runtime routines written in JS.
4550 DECL_ACCESSORS(builtins, JSBuiltinsObject)
4551
4552 // [global context]: the global context corresponding to this global object.
4553 DECL_ACCESSORS(global_context, Context)
4554
4555 // [global receiver]: the global receiver object of the context
4556 DECL_ACCESSORS(global_receiver, JSObject)
4557
4558 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004559 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004560
John Reck59135872010-11-02 12:39:01 -07004561 // This is like GetProperty, but is used when you know the lookup won't fail
4562 // by throwing an exception. This is for the debug and builtins global
4563 // objects, where it is known which properties can be expected to be present
4564 // on the object.
4565 Object* GetPropertyNoExceptionThrown(String* key) {
4566 Object* answer = GetProperty(key)->ToObjectUnchecked();
4567 return answer;
4568 }
4569
Steve Blocka7e24c12009-10-30 11:49:00 +00004570 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07004571 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00004572
4573 // Casting.
4574 static inline GlobalObject* cast(Object* obj);
4575
4576 // Layout description.
4577 static const int kBuiltinsOffset = JSObject::kHeaderSize;
4578 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
4579 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
4580 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
4581
4582 private:
4583 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4584
4585 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
4586};
4587
4588
4589// JavaScript global object.
4590class JSGlobalObject: public GlobalObject {
4591 public:
4592
4593 // Casting.
4594 static inline JSGlobalObject* cast(Object* obj);
4595
4596 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004597#ifdef OBJECT_PRINT
4598 inline void JSGlobalObjectPrint() {
4599 JSGlobalObjectPrint(stdout);
4600 }
4601 void JSGlobalObjectPrint(FILE* out);
4602#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004603#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004604 void JSGlobalObjectVerify();
4605#endif
4606
4607 // Layout description.
4608 static const int kSize = GlobalObject::kHeaderSize;
4609
4610 private:
4611 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
4612};
4613
4614
4615// Builtins global object which holds the runtime routines written in
4616// JavaScript.
4617class JSBuiltinsObject: public GlobalObject {
4618 public:
4619 // Accessors for the runtime routines written in JavaScript.
4620 inline Object* javascript_builtin(Builtins::JavaScript id);
4621 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
4622
Steve Block6ded16b2010-05-10 14:33:55 +01004623 // Accessors for code of the runtime routines written in JavaScript.
4624 inline Code* javascript_builtin_code(Builtins::JavaScript id);
4625 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
4626
Steve Blocka7e24c12009-10-30 11:49:00 +00004627 // Casting.
4628 static inline JSBuiltinsObject* cast(Object* obj);
4629
4630 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004631#ifdef OBJECT_PRINT
4632 inline void JSBuiltinsObjectPrint() {
4633 JSBuiltinsObjectPrint(stdout);
4634 }
4635 void JSBuiltinsObjectPrint(FILE* out);
4636#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004637#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004638 void JSBuiltinsObjectVerify();
4639#endif
4640
4641 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01004642 // room for two pointers per runtime routine written in javascript
4643 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00004644 static const int kJSBuiltinsCount = Builtins::id_count;
4645 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004646 static const int kJSBuiltinsCodeOffset =
4647 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004648 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01004649 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
4650
4651 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
4652 return kJSBuiltinsOffset + id * kPointerSize;
4653 }
4654
4655 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
4656 return kJSBuiltinsCodeOffset + id * kPointerSize;
4657 }
4658
Steve Blocka7e24c12009-10-30 11:49:00 +00004659 private:
4660 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
4661};
4662
4663
4664// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
4665class JSValue: public JSObject {
4666 public:
4667 // [value]: the object being wrapped.
4668 DECL_ACCESSORS(value, Object)
4669
4670 // Casting.
4671 static inline JSValue* cast(Object* obj);
4672
4673 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004674#ifdef OBJECT_PRINT
4675 inline void JSValuePrint() {
4676 JSValuePrint(stdout);
4677 }
4678 void JSValuePrint(FILE* out);
4679#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004680#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004681 void JSValueVerify();
4682#endif
4683
4684 // Layout description.
4685 static const int kValueOffset = JSObject::kHeaderSize;
4686 static const int kSize = kValueOffset + kPointerSize;
4687
4688 private:
4689 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
4690};
4691
4692// Regular expressions
4693// The regular expression holds a single reference to a FixedArray in
4694// the kDataOffset field.
4695// The FixedArray contains the following data:
4696// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
4697// - reference to the original source string
4698// - reference to the original flag string
4699// If it is an atom regexp
4700// - a reference to a literal string to search for
4701// If it is an irregexp regexp:
4702// - a reference to code for ASCII inputs (bytecode or compiled).
4703// - a reference to code for UC16 inputs (bytecode or compiled).
4704// - max number of registers used by irregexp implementations.
4705// - number of capture registers (output values) of the regexp.
4706class JSRegExp: public JSObject {
4707 public:
4708 // Meaning of Type:
4709 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
4710 // ATOM: A simple string to match against using an indexOf operation.
4711 // IRREGEXP: Compiled with Irregexp.
4712 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
4713 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
4714 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
4715
4716 class Flags {
4717 public:
4718 explicit Flags(uint32_t value) : value_(value) { }
4719 bool is_global() { return (value_ & GLOBAL) != 0; }
4720 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
4721 bool is_multiline() { return (value_ & MULTILINE) != 0; }
4722 uint32_t value() { return value_; }
4723 private:
4724 uint32_t value_;
4725 };
4726
4727 DECL_ACCESSORS(data, Object)
4728
4729 inline Type TypeTag();
4730 inline int CaptureCount();
4731 inline Flags GetFlags();
4732 inline String* Pattern();
4733 inline Object* DataAt(int index);
4734 // Set implementation data after the object has been prepared.
4735 inline void SetDataAt(int index, Object* value);
4736 static int code_index(bool is_ascii) {
4737 if (is_ascii) {
4738 return kIrregexpASCIICodeIndex;
4739 } else {
4740 return kIrregexpUC16CodeIndex;
4741 }
4742 }
4743
4744 static inline JSRegExp* cast(Object* obj);
4745
4746 // Dispatched behavior.
4747#ifdef DEBUG
4748 void JSRegExpVerify();
4749#endif
4750
4751 static const int kDataOffset = JSObject::kHeaderSize;
4752 static const int kSize = kDataOffset + kPointerSize;
4753
4754 // Indices in the data array.
4755 static const int kTagIndex = 0;
4756 static const int kSourceIndex = kTagIndex + 1;
4757 static const int kFlagsIndex = kSourceIndex + 1;
4758 static const int kDataIndex = kFlagsIndex + 1;
4759 // The data fields are used in different ways depending on the
4760 // value of the tag.
4761 // Atom regexps (literal strings).
4762 static const int kAtomPatternIndex = kDataIndex;
4763
4764 static const int kAtomDataSize = kAtomPatternIndex + 1;
4765
4766 // Irregexp compiled code or bytecode for ASCII. If compilation
4767 // fails, this fields hold an exception object that should be
4768 // thrown if the regexp is used again.
4769 static const int kIrregexpASCIICodeIndex = kDataIndex;
4770 // Irregexp compiled code or bytecode for UC16. If compilation
4771 // fails, this fields hold an exception object that should be
4772 // thrown if the regexp is used again.
4773 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
4774 // Maximal number of registers used by either ASCII or UC16.
4775 // Only used to check that there is enough stack space
4776 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
4777 // Number of captures in the compiled regexp.
4778 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
4779
4780 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00004781
4782 // Offsets directly into the data fixed array.
4783 static const int kDataTagOffset =
4784 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
4785 static const int kDataAsciiCodeOffset =
4786 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00004787 static const int kDataUC16CodeOffset =
4788 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00004789 static const int kIrregexpCaptureCountOffset =
4790 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004791
4792 // In-object fields.
4793 static const int kSourceFieldIndex = 0;
4794 static const int kGlobalFieldIndex = 1;
4795 static const int kIgnoreCaseFieldIndex = 2;
4796 static const int kMultilineFieldIndex = 3;
4797 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01004798 static const int kInObjectFieldCount = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00004799};
4800
4801
4802class CompilationCacheShape {
4803 public:
4804 static inline bool IsMatch(HashTableKey* key, Object* value) {
4805 return key->IsMatch(value);
4806 }
4807
4808 static inline uint32_t Hash(HashTableKey* key) {
4809 return key->Hash();
4810 }
4811
4812 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4813 return key->HashForObject(object);
4814 }
4815
John Reck59135872010-11-02 12:39:01 -07004816 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00004817 return key->AsObject();
4818 }
4819
4820 static const int kPrefixSize = 0;
4821 static const int kEntrySize = 2;
4822};
4823
Steve Block3ce2e202009-11-05 08:53:23 +00004824
Steve Blocka7e24c12009-10-30 11:49:00 +00004825class CompilationCacheTable: public HashTable<CompilationCacheShape,
4826 HashTableKey*> {
4827 public:
4828 // Find cached value for a string key, otherwise return null.
4829 Object* Lookup(String* src);
4830 Object* LookupEval(String* src, Context* context);
4831 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07004832 MaybeObject* Put(String* src, Object* value);
4833 MaybeObject* PutEval(String* src, Context* context, Object* value);
4834 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004835
Ben Murdochb0fe1622011-05-05 13:52:32 +01004836 // Remove given value from cache.
4837 void Remove(Object* value);
4838
Steve Blocka7e24c12009-10-30 11:49:00 +00004839 static inline CompilationCacheTable* cast(Object* obj);
4840
4841 private:
4842 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
4843};
4844
4845
Steve Block6ded16b2010-05-10 14:33:55 +01004846class CodeCache: public Struct {
4847 public:
4848 DECL_ACCESSORS(default_cache, FixedArray)
4849 DECL_ACCESSORS(normal_type_cache, Object)
4850
4851 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07004852 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004853
4854 // Lookup code object in the cache. Returns code object if found and undefined
4855 // if not.
4856 Object* Lookup(String* name, Code::Flags flags);
4857
4858 // Get the internal index of a code object in the cache. Returns -1 if the
4859 // code object is not in that cache. This index can be used to later call
4860 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
4861 // RemoveByIndex.
4862 int GetIndex(Object* name, Code* code);
4863
4864 // Remove an object from the cache with the provided internal index.
4865 void RemoveByIndex(Object* name, Code* code, int index);
4866
4867 static inline CodeCache* cast(Object* obj);
4868
Ben Murdochb0fe1622011-05-05 13:52:32 +01004869#ifdef OBJECT_PRINT
4870 inline void CodeCachePrint() {
4871 CodeCachePrint(stdout);
4872 }
4873 void CodeCachePrint(FILE* out);
4874#endif
Steve Block6ded16b2010-05-10 14:33:55 +01004875#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01004876 void CodeCacheVerify();
4877#endif
4878
4879 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
4880 static const int kNormalTypeCacheOffset =
4881 kDefaultCacheOffset + kPointerSize;
4882 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
4883
4884 private:
John Reck59135872010-11-02 12:39:01 -07004885 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
4886 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004887 Object* LookupDefaultCache(String* name, Code::Flags flags);
4888 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
4889
4890 // Code cache layout of the default cache. Elements are alternating name and
4891 // code objects for non normal load/store/call IC's.
4892 static const int kCodeCacheEntrySize = 2;
4893 static const int kCodeCacheEntryNameOffset = 0;
4894 static const int kCodeCacheEntryCodeOffset = 1;
4895
4896 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
4897};
4898
4899
4900class CodeCacheHashTableShape {
4901 public:
4902 static inline bool IsMatch(HashTableKey* key, Object* value) {
4903 return key->IsMatch(value);
4904 }
4905
4906 static inline uint32_t Hash(HashTableKey* key) {
4907 return key->Hash();
4908 }
4909
4910 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4911 return key->HashForObject(object);
4912 }
4913
John Reck59135872010-11-02 12:39:01 -07004914 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01004915 return key->AsObject();
4916 }
4917
4918 static const int kPrefixSize = 0;
4919 static const int kEntrySize = 2;
4920};
4921
4922
4923class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
4924 HashTableKey*> {
4925 public:
4926 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07004927 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01004928
4929 int GetIndex(String* name, Code::Flags flags);
4930 void RemoveByIndex(int index);
4931
4932 static inline CodeCacheHashTable* cast(Object* obj);
4933
4934 // Initial size of the fixed array backing the hash table.
4935 static const int kInitialSize = 64;
4936
4937 private:
4938 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
4939};
4940
4941
Steve Blocka7e24c12009-10-30 11:49:00 +00004942enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
4943enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
4944
4945
4946class StringHasher {
4947 public:
4948 inline StringHasher(int length);
4949
4950 // Returns true if the hash of this string can be computed without
4951 // looking at the contents.
4952 inline bool has_trivial_hash();
4953
4954 // Add a character to the hash and update the array index calculation.
4955 inline void AddCharacter(uc32 c);
4956
4957 // Adds a character to the hash but does not update the array index
4958 // calculation. This can only be called when it has been verified
4959 // that the input is not an array index.
4960 inline void AddCharacterNoIndex(uc32 c);
4961
4962 // Returns the value to store in the hash field of a string with
4963 // the given length and contents.
4964 uint32_t GetHashField();
4965
4966 // Returns true if the characters seen so far make up a legal array
4967 // index.
4968 bool is_array_index() { return is_array_index_; }
4969
4970 bool is_valid() { return is_valid_; }
4971
4972 void invalidate() { is_valid_ = false; }
4973
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004974 // Calculated hash value for a string consisting of 1 to
4975 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
4976 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01004977 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004978
Steve Blocka7e24c12009-10-30 11:49:00 +00004979 private:
4980
4981 uint32_t array_index() {
4982 ASSERT(is_array_index());
4983 return array_index_;
4984 }
4985
4986 inline uint32_t GetHash();
4987
4988 int length_;
4989 uint32_t raw_running_hash_;
4990 uint32_t array_index_;
4991 bool is_array_index_;
4992 bool is_first_char_;
4993 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00004994 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00004995};
4996
4997
4998// The characteristics of a string are stored in its map. Retrieving these
4999// few bits of information is moderately expensive, involving two memory
5000// loads where the second is dependent on the first. To improve efficiency
5001// the shape of the string is given its own class so that it can be retrieved
5002// once and used for several string operations. A StringShape is small enough
5003// to be passed by value and is immutable, but be aware that flattening a
5004// string can potentially alter its shape. Also be aware that a GC caused by
5005// something else can alter the shape of a string due to ConsString
5006// shortcutting. Keeping these restrictions in mind has proven to be error-
5007// prone and so we no longer put StringShapes in variables unless there is a
5008// concrete performance benefit at that particular point in the code.
5009class StringShape BASE_EMBEDDED {
5010 public:
5011 inline explicit StringShape(String* s);
5012 inline explicit StringShape(Map* s);
5013 inline explicit StringShape(InstanceType t);
5014 inline bool IsSequential();
5015 inline bool IsExternal();
5016 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00005017 inline bool IsExternalAscii();
5018 inline bool IsExternalTwoByte();
5019 inline bool IsSequentialAscii();
5020 inline bool IsSequentialTwoByte();
5021 inline bool IsSymbol();
5022 inline StringRepresentationTag representation_tag();
5023 inline uint32_t full_representation_tag();
5024 inline uint32_t size_tag();
5025#ifdef DEBUG
5026 inline uint32_t type() { return type_; }
5027 inline void invalidate() { valid_ = false; }
5028 inline bool valid() { return valid_; }
5029#else
5030 inline void invalidate() { }
5031#endif
5032 private:
5033 uint32_t type_;
5034#ifdef DEBUG
5035 inline void set_valid() { valid_ = true; }
5036 bool valid_;
5037#else
5038 inline void set_valid() { }
5039#endif
5040};
5041
5042
5043// The String abstract class captures JavaScript string values:
5044//
5045// Ecma-262:
5046// 4.3.16 String Value
5047// A string value is a member of the type String and is a finite
5048// ordered sequence of zero or more 16-bit unsigned integer values.
5049//
5050// All string values have a length field.
5051class String: public HeapObject {
5052 public:
5053 // Get and set the length of the string.
5054 inline int length();
5055 inline void set_length(int value);
5056
Steve Blockd0582a62009-12-15 09:54:21 +00005057 // Get and set the hash field of the string.
5058 inline uint32_t hash_field();
5059 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005060
5061 inline bool IsAsciiRepresentation();
5062 inline bool IsTwoByteRepresentation();
5063
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005064 // Returns whether this string has ascii chars, i.e. all of them can
5065 // be ascii encoded. This might be the case even if the string is
5066 // two-byte. Such strings may appear when the embedder prefers
5067 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01005068 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005069 // NOTE: this should be considered only a hint. False negatives are
5070 // possible.
5071 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005072
Steve Blocka7e24c12009-10-30 11:49:00 +00005073 // Get and set individual two byte chars in the string.
5074 inline void Set(int index, uint16_t value);
5075 // Get individual two byte char in the string. Repeated calls
5076 // to this method are not efficient unless the string is flat.
5077 inline uint16_t Get(int index);
5078
Leon Clarkef7060e22010-06-03 12:02:55 +01005079 // Try to flatten the string. Checks first inline to see if it is
5080 // necessary. Does nothing if the string is not a cons string.
5081 // Flattening allocates a sequential string with the same data as
5082 // the given string and mutates the cons string to a degenerate
5083 // form, where the first component is the new sequential string and
5084 // the second component is the empty string. If allocation fails,
5085 // this function returns a failure. If flattening succeeds, this
5086 // function returns the sequential string that is now the first
5087 // component of the cons string.
5088 //
5089 // Degenerate cons strings are handled specially by the garbage
5090 // collector (see IsShortcutCandidate).
5091 //
5092 // Use FlattenString from Handles.cc to flatten even in case an
5093 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07005094 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005095
Leon Clarkef7060e22010-06-03 12:02:55 +01005096 // Convenience function. Has exactly the same behavior as
5097 // TryFlatten(), except in the case of failure returns the original
5098 // string.
5099 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5100
Steve Blocka7e24c12009-10-30 11:49:00 +00005101 Vector<const char> ToAsciiVector();
5102 Vector<const uc16> ToUC16Vector();
5103
5104 // Mark the string as an undetectable object. It only applies to
5105 // ascii and two byte string types.
5106 bool MarkAsUndetectable();
5107
Steve Blockd0582a62009-12-15 09:54:21 +00005108 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07005109 MUST_USE_RESULT MaybeObject* SubString(int from,
5110 int to,
5111 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005112
5113 // String equality operations.
5114 inline bool Equals(String* other);
5115 bool IsEqualTo(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +01005116 bool IsAsciiEqualTo(Vector<const char> str);
5117 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00005118
5119 // Return a UTF8 representation of the string. The string is null
5120 // terminated but may optionally contain nulls. Length is returned
5121 // in length_output if length_output is not a null pointer The string
5122 // should be nearly flat, otherwise the performance of this method may
5123 // be very slow (quadratic in the length). Setting robustness_flag to
5124 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5125 // handles unexpected data without causing assert failures and it does not
5126 // do any heap allocations. This is useful when printing stack traces.
5127 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5128 RobustnessFlag robustness_flag,
5129 int offset,
5130 int length,
5131 int* length_output = 0);
5132 SmartPointer<char> ToCString(
5133 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5134 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5135 int* length_output = 0);
5136
5137 int Utf8Length();
5138
5139 // Return a 16 bit Unicode representation of the string.
5140 // The string should be nearly flat, otherwise the performance of
5141 // of this method may be very bad. Setting robustness_flag to
5142 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5143 // handles unexpected data without causing assert failures and it does not
5144 // do any heap allocations. This is useful when printing stack traces.
5145 SmartPointer<uc16> ToWideCString(
5146 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
5147
5148 // Tells whether the hash code has been computed.
5149 inline bool HasHashCode();
5150
5151 // Returns a hash value used for the property table
5152 inline uint32_t Hash();
5153
Steve Blockd0582a62009-12-15 09:54:21 +00005154 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5155 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005156
5157 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5158 uint32_t* index,
5159 int length);
5160
5161 // Externalization.
5162 bool MakeExternal(v8::String::ExternalStringResource* resource);
5163 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5164
5165 // Conversion.
5166 inline bool AsArrayIndex(uint32_t* index);
5167
5168 // Casting.
5169 static inline String* cast(Object* obj);
5170
5171 void PrintOn(FILE* out);
5172
5173 // For use during stack traces. Performs rudimentary sanity check.
5174 bool LooksValid();
5175
5176 // Dispatched behavior.
5177 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005178#ifdef OBJECT_PRINT
5179 inline void StringPrint() {
5180 StringPrint(stdout);
5181 }
5182 void StringPrint(FILE* out);
5183#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005184#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005185 void StringVerify();
5186#endif
5187 inline bool IsFlat();
5188
5189 // Layout description.
5190 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005191 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005192 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005193
Steve Blockd0582a62009-12-15 09:54:21 +00005194 // Maximum number of characters to consider when trying to convert a string
5195 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00005196 static const int kMaxArrayIndexSize = 10;
5197
5198 // Max ascii char code.
5199 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
5200 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
5201 static const int kMaxUC16CharCode = 0xffff;
5202
Steve Blockd0582a62009-12-15 09:54:21 +00005203 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00005204 static const int kMinNonFlatLength = 13;
5205
5206 // Mask constant for checking if a string has a computed hash code
5207 // and if it is an array index. The least significant bit indicates
5208 // whether a hash code has been computed. If the hash code has been
5209 // computed the 2nd bit tells whether the string can be used as an
5210 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005211 static const int kHashNotComputedMask = 1;
5212 static const int kIsNotArrayIndexMask = 1 << 1;
5213 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00005214
Steve Blockd0582a62009-12-15 09:54:21 +00005215 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005216 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00005217
Steve Blocka7e24c12009-10-30 11:49:00 +00005218 // Array index strings this short can keep their index in the hash
5219 // field.
5220 static const int kMaxCachedArrayIndexLength = 7;
5221
Steve Blockd0582a62009-12-15 09:54:21 +00005222 // For strings which are array indexes the hash value has the string length
5223 // mixed into the hash, mainly to avoid a hash value of zero which would be
5224 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005225 static const int kArrayIndexValueBits = 24;
5226 static const int kArrayIndexLengthBits =
5227 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
5228
5229 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01005230 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005231
5232 static const int kArrayIndexHashLengthShift =
5233 kArrayIndexValueBits + kNofHashBitFields;
5234
Steve Blockd0582a62009-12-15 09:54:21 +00005235 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005236
5237 static const int kArrayIndexValueMask =
5238 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
5239
5240 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
5241 // could use a mask to test if the length of string is less than or equal to
5242 // kMaxCachedArrayIndexLength.
5243 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
5244
5245 static const int kContainsCachedArrayIndexMask =
5246 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
5247 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005248
5249 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005250 static const int kEmptyHashField =
5251 kIsNotArrayIndexMask | kHashNotComputedMask;
5252
5253 // Value of hash field containing computed hash equal to zero.
5254 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005255
5256 // Maximal string length.
5257 static const int kMaxLength = (1 << (32 - 2)) - 1;
5258
5259 // Max length for computing hash. For strings longer than this limit the
5260 // string length is used as the hash value.
5261 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00005262
5263 // Limit for truncation in short printing.
5264 static const int kMaxShortPrintLength = 1024;
5265
5266 // Support for regular expressions.
5267 const uc16* GetTwoByteData();
5268 const uc16* GetTwoByteData(unsigned start);
5269
5270 // Support for StringInputBuffer
5271 static const unibrow::byte* ReadBlock(String* input,
5272 unibrow::byte* util_buffer,
5273 unsigned capacity,
5274 unsigned* remaining,
5275 unsigned* offset);
5276 static const unibrow::byte* ReadBlock(String** input,
5277 unibrow::byte* util_buffer,
5278 unsigned capacity,
5279 unsigned* remaining,
5280 unsigned* offset);
5281
5282 // Helper function for flattening strings.
5283 template <typename sinkchar>
5284 static void WriteToFlat(String* source,
5285 sinkchar* sink,
5286 int from,
5287 int to);
5288
Steve Block9fac8402011-05-12 15:51:54 +01005289 static inline bool IsAscii(const char* chars, int length) {
5290 const char* limit = chars + length;
5291#ifdef V8_HOST_CAN_READ_UNALIGNED
5292 ASSERT(kMaxAsciiCharCode == 0x7F);
5293 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
5294 while (chars <= limit - sizeof(uintptr_t)) {
5295 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
5296 return false;
5297 }
5298 chars += sizeof(uintptr_t);
5299 }
5300#endif
5301 while (chars < limit) {
5302 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
5303 ++chars;
5304 }
5305 return true;
5306 }
5307
5308 static inline bool IsAscii(const uc16* chars, int length) {
5309 const uc16* limit = chars + length;
5310 while (chars < limit) {
5311 if (*chars > kMaxAsciiCharCodeU) return false;
5312 ++chars;
5313 }
5314 return true;
5315 }
5316
Steve Blocka7e24c12009-10-30 11:49:00 +00005317 protected:
5318 class ReadBlockBuffer {
5319 public:
5320 ReadBlockBuffer(unibrow::byte* util_buffer_,
5321 unsigned cursor_,
5322 unsigned capacity_,
5323 unsigned remaining_) :
5324 util_buffer(util_buffer_),
5325 cursor(cursor_),
5326 capacity(capacity_),
5327 remaining(remaining_) {
5328 }
5329 unibrow::byte* util_buffer;
5330 unsigned cursor;
5331 unsigned capacity;
5332 unsigned remaining;
5333 };
5334
Steve Blocka7e24c12009-10-30 11:49:00 +00005335 static inline const unibrow::byte* ReadBlock(String* input,
5336 ReadBlockBuffer* buffer,
5337 unsigned* offset,
5338 unsigned max_chars);
5339 static void ReadBlockIntoBuffer(String* input,
5340 ReadBlockBuffer* buffer,
5341 unsigned* offset_ptr,
5342 unsigned max_chars);
5343
5344 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01005345 // Try to flatten the top level ConsString that is hiding behind this
5346 // string. This is a no-op unless the string is a ConsString. Flatten
5347 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07005348 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01005349
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005350 static inline bool IsHashFieldComputed(uint32_t field);
5351
Steve Blocka7e24c12009-10-30 11:49:00 +00005352 // Slow case of String::Equals. This implementation works on any strings
5353 // but it is most efficient on strings that are almost flat.
5354 bool SlowEquals(String* other);
5355
5356 // Slow case of AsArrayIndex.
5357 bool SlowAsArrayIndex(uint32_t* index);
5358
5359 // Compute and set the hash code.
5360 uint32_t ComputeAndSetHash();
5361
5362 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
5363};
5364
5365
5366// The SeqString abstract class captures sequential string values.
5367class SeqString: public String {
5368 public:
5369
5370 // Casting.
5371 static inline SeqString* cast(Object* obj);
5372
Steve Blocka7e24c12009-10-30 11:49:00 +00005373 private:
5374 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
5375};
5376
5377
5378// The AsciiString class captures sequential ascii string objects.
5379// Each character in the AsciiString is an ascii character.
5380class SeqAsciiString: public SeqString {
5381 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005382 static const bool kHasAsciiEncoding = true;
5383
Steve Blocka7e24c12009-10-30 11:49:00 +00005384 // Dispatched behavior.
5385 inline uint16_t SeqAsciiStringGet(int index);
5386 inline void SeqAsciiStringSet(int index, uint16_t value);
5387
5388 // Get the address of the characters in this string.
5389 inline Address GetCharsAddress();
5390
5391 inline char* GetChars();
5392
5393 // Casting
5394 static inline SeqAsciiString* cast(Object* obj);
5395
5396 // Garbage collection support. This method is called by the
5397 // garbage collector to compute the actual size of an AsciiString
5398 // instance.
5399 inline int SeqAsciiStringSize(InstanceType instance_type);
5400
5401 // Computes the size for an AsciiString instance of a given length.
5402 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005403 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005404 }
5405
5406 // Layout description.
5407 static const int kHeaderSize = String::kSize;
5408 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5409
Leon Clarkee46be812010-01-19 14:06:41 +00005410 // Maximal memory usage for a single sequential ASCII string.
5411 static const int kMaxSize = 512 * MB;
5412 // Maximal length of a single sequential ASCII string.
5413 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5414 static const int kMaxLength = (kMaxSize - kHeaderSize);
5415
Steve Blocka7e24c12009-10-30 11:49:00 +00005416 // Support for StringInputBuffer.
5417 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5418 unsigned* offset,
5419 unsigned chars);
5420 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
5421 unsigned* offset,
5422 unsigned chars);
5423
5424 private:
5425 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
5426};
5427
5428
5429// The TwoByteString class captures sequential unicode string objects.
5430// Each character in the TwoByteString is a two-byte uint16_t.
5431class SeqTwoByteString: public SeqString {
5432 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005433 static const bool kHasAsciiEncoding = false;
5434
Steve Blocka7e24c12009-10-30 11:49:00 +00005435 // Dispatched behavior.
5436 inline uint16_t SeqTwoByteStringGet(int index);
5437 inline void SeqTwoByteStringSet(int index, uint16_t value);
5438
5439 // Get the address of the characters in this string.
5440 inline Address GetCharsAddress();
5441
5442 inline uc16* GetChars();
5443
5444 // For regexp code.
5445 const uint16_t* SeqTwoByteStringGetData(unsigned start);
5446
5447 // Casting
5448 static inline SeqTwoByteString* cast(Object* obj);
5449
5450 // Garbage collection support. This method is called by the
5451 // garbage collector to compute the actual size of a TwoByteString
5452 // instance.
5453 inline int SeqTwoByteStringSize(InstanceType instance_type);
5454
5455 // Computes the size for a TwoByteString instance of a given length.
5456 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005457 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005458 }
5459
5460 // Layout description.
5461 static const int kHeaderSize = String::kSize;
5462 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5463
Leon Clarkee46be812010-01-19 14:06:41 +00005464 // Maximal memory usage for a single sequential two-byte string.
5465 static const int kMaxSize = 512 * MB;
5466 // Maximal length of a single sequential two-byte string.
5467 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5468 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
5469
Steve Blocka7e24c12009-10-30 11:49:00 +00005470 // Support for StringInputBuffer.
5471 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5472 unsigned* offset_ptr,
5473 unsigned chars);
5474
5475 private:
5476 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
5477};
5478
5479
5480// The ConsString class describes string values built by using the
5481// addition operator on strings. A ConsString is a pair where the
5482// first and second components are pointers to other string values.
5483// One or both components of a ConsString can be pointers to other
5484// ConsStrings, creating a binary tree of ConsStrings where the leaves
5485// are non-ConsString string values. The string value represented by
5486// a ConsString can be obtained by concatenating the leaf string
5487// values in a left-to-right depth-first traversal of the tree.
5488class ConsString: public String {
5489 public:
5490 // First string of the cons cell.
5491 inline String* first();
5492 // Doesn't check that the result is a string, even in debug mode. This is
5493 // useful during GC where the mark bits confuse the checks.
5494 inline Object* unchecked_first();
5495 inline void set_first(String* first,
5496 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5497
5498 // Second string of the cons cell.
5499 inline String* second();
5500 // Doesn't check that the result is a string, even in debug mode. This is
5501 // useful during GC where the mark bits confuse the checks.
5502 inline Object* unchecked_second();
5503 inline void set_second(String* second,
5504 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5505
5506 // Dispatched behavior.
5507 uint16_t ConsStringGet(int index);
5508
5509 // Casting.
5510 static inline ConsString* cast(Object* obj);
5511
Steve Blocka7e24c12009-10-30 11:49:00 +00005512 // Layout description.
5513 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
5514 static const int kSecondOffset = kFirstOffset + kPointerSize;
5515 static const int kSize = kSecondOffset + kPointerSize;
5516
5517 // Support for StringInputBuffer.
5518 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
5519 unsigned* offset_ptr,
5520 unsigned chars);
5521 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5522 unsigned* offset_ptr,
5523 unsigned chars);
5524
5525 // Minimum length for a cons string.
5526 static const int kMinLength = 13;
5527
Iain Merrick75681382010-08-19 15:07:18 +01005528 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
5529 BodyDescriptor;
5530
Steve Blocka7e24c12009-10-30 11:49:00 +00005531 private:
5532 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
5533};
5534
5535
Steve Blocka7e24c12009-10-30 11:49:00 +00005536// The ExternalString class describes string values that are backed by
5537// a string resource that lies outside the V8 heap. ExternalStrings
5538// consist of the length field common to all strings, a pointer to the
5539// external resource. It is important to ensure (externally) that the
5540// resource is not deallocated while the ExternalString is live in the
5541// V8 heap.
5542//
5543// The API expects that all ExternalStrings are created through the
5544// API. Therefore, ExternalStrings should not be used internally.
5545class ExternalString: public String {
5546 public:
5547 // Casting
5548 static inline ExternalString* cast(Object* obj);
5549
5550 // Layout description.
5551 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
5552 static const int kSize = kResourceOffset + kPointerSize;
5553
5554 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
5555
5556 private:
5557 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
5558};
5559
5560
5561// The ExternalAsciiString class is an external string backed by an
5562// ASCII string.
5563class ExternalAsciiString: public ExternalString {
5564 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005565 static const bool kHasAsciiEncoding = true;
5566
Steve Blocka7e24c12009-10-30 11:49:00 +00005567 typedef v8::String::ExternalAsciiStringResource Resource;
5568
5569 // The underlying resource.
5570 inline Resource* resource();
5571 inline void set_resource(Resource* buffer);
5572
5573 // Dispatched behavior.
5574 uint16_t ExternalAsciiStringGet(int index);
5575
5576 // Casting.
5577 static inline ExternalAsciiString* cast(Object* obj);
5578
Steve Blockd0582a62009-12-15 09:54:21 +00005579 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005580 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
5581
5582 template<typename StaticVisitor>
5583 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00005584
Steve Blocka7e24c12009-10-30 11:49:00 +00005585 // Support for StringInputBuffer.
5586 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
5587 unsigned* offset,
5588 unsigned chars);
5589 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5590 unsigned* offset,
5591 unsigned chars);
5592
Steve Blocka7e24c12009-10-30 11:49:00 +00005593 private:
5594 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
5595};
5596
5597
5598// The ExternalTwoByteString class is an external string backed by a UTF-16
5599// encoded string.
5600class ExternalTwoByteString: public ExternalString {
5601 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005602 static const bool kHasAsciiEncoding = false;
5603
Steve Blocka7e24c12009-10-30 11:49:00 +00005604 typedef v8::String::ExternalStringResource Resource;
5605
5606 // The underlying string resource.
5607 inline Resource* resource();
5608 inline void set_resource(Resource* buffer);
5609
5610 // Dispatched behavior.
5611 uint16_t ExternalTwoByteStringGet(int index);
5612
5613 // For regexp code.
5614 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
5615
5616 // Casting.
5617 static inline ExternalTwoByteString* cast(Object* obj);
5618
Steve Blockd0582a62009-12-15 09:54:21 +00005619 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005620 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
5621
5622 template<typename StaticVisitor>
5623 inline void ExternalTwoByteStringIterateBody();
5624
Steve Blockd0582a62009-12-15 09:54:21 +00005625
Steve Blocka7e24c12009-10-30 11:49:00 +00005626 // Support for StringInputBuffer.
5627 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5628 unsigned* offset_ptr,
5629 unsigned chars);
5630
Steve Blocka7e24c12009-10-30 11:49:00 +00005631 private:
5632 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
5633};
5634
5635
5636// Utility superclass for stack-allocated objects that must be updated
5637// on gc. It provides two ways for the gc to update instances, either
5638// iterating or updating after gc.
5639class Relocatable BASE_EMBEDDED {
5640 public:
5641 inline Relocatable() : prev_(top_) { top_ = this; }
5642 virtual ~Relocatable() {
5643 ASSERT_EQ(top_, this);
5644 top_ = prev_;
5645 }
5646 virtual void IterateInstance(ObjectVisitor* v) { }
5647 virtual void PostGarbageCollection() { }
5648
5649 static void PostGarbageCollectionProcessing();
5650 static int ArchiveSpacePerThread();
5651 static char* ArchiveState(char* to);
5652 static char* RestoreState(char* from);
5653 static void Iterate(ObjectVisitor* v);
5654 static void Iterate(ObjectVisitor* v, Relocatable* top);
5655 static char* Iterate(ObjectVisitor* v, char* t);
5656 private:
5657 static Relocatable* top_;
5658 Relocatable* prev_;
5659};
5660
5661
5662// A flat string reader provides random access to the contents of a
5663// string independent of the character width of the string. The handle
5664// must be valid as long as the reader is being used.
5665class FlatStringReader : public Relocatable {
5666 public:
5667 explicit FlatStringReader(Handle<String> str);
5668 explicit FlatStringReader(Vector<const char> input);
5669 void PostGarbageCollection();
5670 inline uc32 Get(int index);
5671 int length() { return length_; }
5672 private:
5673 String** str_;
5674 bool is_ascii_;
5675 int length_;
5676 const void* start_;
5677};
5678
5679
5680// Note that StringInputBuffers are not valid across a GC! To fix this
5681// it would have to store a String Handle instead of a String* and
5682// AsciiStringReadBlock would have to be modified to use memcpy.
5683//
5684// StringInputBuffer is able to traverse any string regardless of how
5685// deeply nested a sequence of ConsStrings it is made of. However,
5686// performance will be better if deep strings are flattened before they
5687// are traversed. Since flattening requires memory allocation this is
5688// not always desirable, however (esp. in debugging situations).
5689class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
5690 public:
5691 virtual void Seek(unsigned pos);
5692 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
5693 inline StringInputBuffer(String* backing):
5694 unibrow::InputBuffer<String, String*, 1024>(backing) {}
5695};
5696
5697
5698class SafeStringInputBuffer
5699 : public unibrow::InputBuffer<String, String**, 256> {
5700 public:
5701 virtual void Seek(unsigned pos);
5702 inline SafeStringInputBuffer()
5703 : unibrow::InputBuffer<String, String**, 256>() {}
5704 inline SafeStringInputBuffer(String** backing)
5705 : unibrow::InputBuffer<String, String**, 256>(backing) {}
5706};
5707
5708
5709template <typename T>
5710class VectorIterator {
5711 public:
5712 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
5713 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
5714 T GetNext() { return data_[index_++]; }
5715 bool has_more() { return index_ < data_.length(); }
5716 private:
5717 Vector<const T> data_;
5718 int index_;
5719};
5720
5721
5722// The Oddball describes objects null, undefined, true, and false.
5723class Oddball: public HeapObject {
5724 public:
5725 // [to_string]: Cached to_string computed at startup.
5726 DECL_ACCESSORS(to_string, String)
5727
5728 // [to_number]: Cached to_number computed at startup.
5729 DECL_ACCESSORS(to_number, Object)
5730
5731 // Casting.
5732 static inline Oddball* cast(Object* obj);
5733
5734 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00005735#ifdef DEBUG
5736 void OddballVerify();
5737#endif
5738
5739 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07005740 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
5741 Object* to_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00005742
5743 // Layout description.
5744 static const int kToStringOffset = HeapObject::kHeaderSize;
5745 static const int kToNumberOffset = kToStringOffset + kPointerSize;
5746 static const int kSize = kToNumberOffset + kPointerSize;
5747
Iain Merrick75681382010-08-19 15:07:18 +01005748 typedef FixedBodyDescriptor<kToStringOffset,
5749 kToNumberOffset + kPointerSize,
5750 kSize> BodyDescriptor;
5751
Steve Blocka7e24c12009-10-30 11:49:00 +00005752 private:
5753 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
5754};
5755
5756
5757class JSGlobalPropertyCell: public HeapObject {
5758 public:
5759 // [value]: value of the global property.
5760 DECL_ACCESSORS(value, Object)
5761
5762 // Casting.
5763 static inline JSGlobalPropertyCell* cast(Object* obj);
5764
Steve Blocka7e24c12009-10-30 11:49:00 +00005765#ifdef DEBUG
5766 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005767#endif
5768#ifdef OBJECT_PRINT
5769 inline void JSGlobalPropertyCellPrint() {
5770 JSGlobalPropertyCellPrint(stdout);
5771 }
5772 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00005773#endif
5774
5775 // Layout description.
5776 static const int kValueOffset = HeapObject::kHeaderSize;
5777 static const int kSize = kValueOffset + kPointerSize;
5778
Iain Merrick75681382010-08-19 15:07:18 +01005779 typedef FixedBodyDescriptor<kValueOffset,
5780 kValueOffset + kPointerSize,
5781 kSize> BodyDescriptor;
5782
Steve Blocka7e24c12009-10-30 11:49:00 +00005783 private:
5784 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
5785};
5786
5787
5788
5789// Proxy describes objects pointing from JavaScript to C structures.
5790// Since they cannot contain references to JS HeapObjects they can be
5791// placed in old_data_space.
5792class Proxy: public HeapObject {
5793 public:
5794 // [proxy]: field containing the address.
5795 inline Address proxy();
5796 inline void set_proxy(Address value);
5797
5798 // Casting.
5799 static inline Proxy* cast(Object* obj);
5800
5801 // Dispatched behavior.
5802 inline void ProxyIterateBody(ObjectVisitor* v);
Iain Merrick75681382010-08-19 15:07:18 +01005803
5804 template<typename StaticVisitor>
5805 inline void ProxyIterateBody();
5806
Ben Murdochb0fe1622011-05-05 13:52:32 +01005807#ifdef OBJECT_PRINT
5808 inline void ProxyPrint() {
5809 ProxyPrint(stdout);
5810 }
5811 void ProxyPrint(FILE* out);
5812#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005813#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005814 void ProxyVerify();
5815#endif
5816
5817 // Layout description.
5818
5819 static const int kProxyOffset = HeapObject::kHeaderSize;
5820 static const int kSize = kProxyOffset + kPointerSize;
5821
5822 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
5823
5824 private:
5825 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
5826};
5827
5828
5829// The JSArray describes JavaScript Arrays
5830// Such an array can be in one of two modes:
5831// - fast, backing storage is a FixedArray and length <= elements.length();
5832// Please note: push and pop can be used to grow and shrink the array.
5833// - slow, backing storage is a HashTable with numbers as keys.
5834class JSArray: public JSObject {
5835 public:
5836 // [length]: The length property.
5837 DECL_ACCESSORS(length, Object)
5838
Leon Clarke4515c472010-02-03 11:58:03 +00005839 // Overload the length setter to skip write barrier when the length
5840 // is set to a smi. This matches the set function on FixedArray.
5841 inline void set_length(Smi* length);
5842
John Reck59135872010-11-02 12:39:01 -07005843 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
5844 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005845
5846 // Initialize the array with the given capacity. The function may
5847 // fail due to out-of-memory situations, but only if the requested
5848 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07005849 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00005850
5851 // Set the content of the array to the content of storage.
5852 inline void SetContent(FixedArray* storage);
5853
5854 // Casting.
5855 static inline JSArray* cast(Object* obj);
5856
5857 // Uses handles. Ensures that the fixed array backing the JSArray has at
5858 // least the stated size.
5859 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
5860
5861 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005862#ifdef OBJECT_PRINT
5863 inline void JSArrayPrint() {
5864 JSArrayPrint(stdout);
5865 }
5866 void JSArrayPrint(FILE* out);
5867#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005868#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005869 void JSArrayVerify();
5870#endif
5871
5872 // Number of element slots to pre-allocate for an empty array.
5873 static const int kPreallocatedArrayElements = 4;
5874
5875 // Layout description.
5876 static const int kLengthOffset = JSObject::kHeaderSize;
5877 static const int kSize = kLengthOffset + kPointerSize;
5878
5879 private:
5880 // Expand the fixed array backing of a fast-case JSArray to at least
5881 // the requested size.
5882 void Expand(int minimum_size_of_backing_fixed_array);
5883
5884 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
5885};
5886
5887
Steve Block6ded16b2010-05-10 14:33:55 +01005888// JSRegExpResult is just a JSArray with a specific initial map.
5889// This initial map adds in-object properties for "index" and "input"
5890// properties, as assigned by RegExp.prototype.exec, which allows
5891// faster creation of RegExp exec results.
5892// This class just holds constants used when creating the result.
5893// After creation the result must be treated as a JSArray in all regards.
5894class JSRegExpResult: public JSArray {
5895 public:
5896 // Offsets of object fields.
5897 static const int kIndexOffset = JSArray::kSize;
5898 static const int kInputOffset = kIndexOffset + kPointerSize;
5899 static const int kSize = kInputOffset + kPointerSize;
5900 // Indices of in-object properties.
5901 static const int kIndexIndex = 0;
5902 static const int kInputIndex = 1;
5903 private:
5904 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
5905};
5906
5907
Steve Blocka7e24c12009-10-30 11:49:00 +00005908// An accessor must have a getter, but can have no setter.
5909//
5910// When setting a property, V8 searches accessors in prototypes.
5911// If an accessor was found and it does not have a setter,
5912// the request is ignored.
5913//
5914// If the accessor in the prototype has the READ_ONLY property attribute, then
5915// a new value is added to the local object when the property is set.
5916// This shadows the accessor in the prototype.
5917class AccessorInfo: public Struct {
5918 public:
5919 DECL_ACCESSORS(getter, Object)
5920 DECL_ACCESSORS(setter, Object)
5921 DECL_ACCESSORS(data, Object)
5922 DECL_ACCESSORS(name, Object)
5923 DECL_ACCESSORS(flag, Smi)
5924
5925 inline bool all_can_read();
5926 inline void set_all_can_read(bool value);
5927
5928 inline bool all_can_write();
5929 inline void set_all_can_write(bool value);
5930
5931 inline bool prohibits_overwriting();
5932 inline void set_prohibits_overwriting(bool value);
5933
5934 inline PropertyAttributes property_attributes();
5935 inline void set_property_attributes(PropertyAttributes attributes);
5936
5937 static inline AccessorInfo* cast(Object* obj);
5938
Ben Murdochb0fe1622011-05-05 13:52:32 +01005939#ifdef OBJECT_PRINT
5940 inline void AccessorInfoPrint() {
5941 AccessorInfoPrint(stdout);
5942 }
5943 void AccessorInfoPrint(FILE* out);
5944#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005945#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005946 void AccessorInfoVerify();
5947#endif
5948
5949 static const int kGetterOffset = HeapObject::kHeaderSize;
5950 static const int kSetterOffset = kGetterOffset + kPointerSize;
5951 static const int kDataOffset = kSetterOffset + kPointerSize;
5952 static const int kNameOffset = kDataOffset + kPointerSize;
5953 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08005954 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005955
5956 private:
5957 // Bit positions in flag.
5958 static const int kAllCanReadBit = 0;
5959 static const int kAllCanWriteBit = 1;
5960 static const int kProhibitsOverwritingBit = 2;
5961 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
5962
5963 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
5964};
5965
5966
5967class AccessCheckInfo: public Struct {
5968 public:
5969 DECL_ACCESSORS(named_callback, Object)
5970 DECL_ACCESSORS(indexed_callback, Object)
5971 DECL_ACCESSORS(data, Object)
5972
5973 static inline AccessCheckInfo* cast(Object* obj);
5974
Ben Murdochb0fe1622011-05-05 13:52:32 +01005975#ifdef OBJECT_PRINT
5976 inline void AccessCheckInfoPrint() {
5977 AccessCheckInfoPrint(stdout);
5978 }
5979 void AccessCheckInfoPrint(FILE* out);
5980#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005981#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005982 void AccessCheckInfoVerify();
5983#endif
5984
5985 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
5986 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
5987 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
5988 static const int kSize = kDataOffset + kPointerSize;
5989
5990 private:
5991 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
5992};
5993
5994
5995class InterceptorInfo: public Struct {
5996 public:
5997 DECL_ACCESSORS(getter, Object)
5998 DECL_ACCESSORS(setter, Object)
5999 DECL_ACCESSORS(query, Object)
6000 DECL_ACCESSORS(deleter, Object)
6001 DECL_ACCESSORS(enumerator, Object)
6002 DECL_ACCESSORS(data, Object)
6003
6004 static inline InterceptorInfo* cast(Object* obj);
6005
Ben Murdochb0fe1622011-05-05 13:52:32 +01006006#ifdef OBJECT_PRINT
6007 inline void InterceptorInfoPrint() {
6008 InterceptorInfoPrint(stdout);
6009 }
6010 void InterceptorInfoPrint(FILE* out);
6011#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006012#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006013 void InterceptorInfoVerify();
6014#endif
6015
6016 static const int kGetterOffset = HeapObject::kHeaderSize;
6017 static const int kSetterOffset = kGetterOffset + kPointerSize;
6018 static const int kQueryOffset = kSetterOffset + kPointerSize;
6019 static const int kDeleterOffset = kQueryOffset + kPointerSize;
6020 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
6021 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
6022 static const int kSize = kDataOffset + kPointerSize;
6023
6024 private:
6025 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
6026};
6027
6028
6029class CallHandlerInfo: public Struct {
6030 public:
6031 DECL_ACCESSORS(callback, Object)
6032 DECL_ACCESSORS(data, Object)
6033
6034 static inline CallHandlerInfo* cast(Object* obj);
6035
Ben Murdochb0fe1622011-05-05 13:52:32 +01006036#ifdef OBJECT_PRINT
6037 inline void CallHandlerInfoPrint() {
6038 CallHandlerInfoPrint(stdout);
6039 }
6040 void CallHandlerInfoPrint(FILE* out);
6041#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006042#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006043 void CallHandlerInfoVerify();
6044#endif
6045
6046 static const int kCallbackOffset = HeapObject::kHeaderSize;
6047 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006048 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006049
6050 private:
6051 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
6052};
6053
6054
6055class TemplateInfo: public Struct {
6056 public:
6057 DECL_ACCESSORS(tag, Object)
6058 DECL_ACCESSORS(property_list, Object)
6059
6060#ifdef DEBUG
6061 void TemplateInfoVerify();
6062#endif
6063
6064 static const int kTagOffset = HeapObject::kHeaderSize;
6065 static const int kPropertyListOffset = kTagOffset + kPointerSize;
6066 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
6067 protected:
6068 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6069 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6070};
6071
6072
6073class FunctionTemplateInfo: public TemplateInfo {
6074 public:
6075 DECL_ACCESSORS(serial_number, Object)
6076 DECL_ACCESSORS(call_code, Object)
6077 DECL_ACCESSORS(property_accessors, Object)
6078 DECL_ACCESSORS(prototype_template, Object)
6079 DECL_ACCESSORS(parent_template, Object)
6080 DECL_ACCESSORS(named_property_handler, Object)
6081 DECL_ACCESSORS(indexed_property_handler, Object)
6082 DECL_ACCESSORS(instance_template, Object)
6083 DECL_ACCESSORS(class_name, Object)
6084 DECL_ACCESSORS(signature, Object)
6085 DECL_ACCESSORS(instance_call_handler, Object)
6086 DECL_ACCESSORS(access_check_info, Object)
6087 DECL_ACCESSORS(flag, Smi)
6088
6089 // Following properties use flag bits.
6090 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6091 DECL_BOOLEAN_ACCESSORS(undetectable)
6092 // If the bit is set, object instances created by this function
6093 // requires access check.
6094 DECL_BOOLEAN_ACCESSORS(needs_access_check)
6095
6096 static inline FunctionTemplateInfo* cast(Object* obj);
6097
Ben Murdochb0fe1622011-05-05 13:52:32 +01006098#ifdef OBJECT_PRINT
6099 inline void FunctionTemplateInfoPrint() {
6100 FunctionTemplateInfoPrint(stdout);
6101 }
6102 void FunctionTemplateInfoPrint(FILE* out);
6103#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006104#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006105 void FunctionTemplateInfoVerify();
6106#endif
6107
6108 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
6109 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
6110 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
6111 static const int kPrototypeTemplateOffset =
6112 kPropertyAccessorsOffset + kPointerSize;
6113 static const int kParentTemplateOffset =
6114 kPrototypeTemplateOffset + kPointerSize;
6115 static const int kNamedPropertyHandlerOffset =
6116 kParentTemplateOffset + kPointerSize;
6117 static const int kIndexedPropertyHandlerOffset =
6118 kNamedPropertyHandlerOffset + kPointerSize;
6119 static const int kInstanceTemplateOffset =
6120 kIndexedPropertyHandlerOffset + kPointerSize;
6121 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
6122 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
6123 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
6124 static const int kAccessCheckInfoOffset =
6125 kInstanceCallHandlerOffset + kPointerSize;
6126 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
6127 static const int kSize = kFlagOffset + kPointerSize;
6128
6129 private:
6130 // Bit position in the flag, from least significant bit position.
6131 static const int kHiddenPrototypeBit = 0;
6132 static const int kUndetectableBit = 1;
6133 static const int kNeedsAccessCheckBit = 2;
6134
6135 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
6136};
6137
6138
6139class ObjectTemplateInfo: public TemplateInfo {
6140 public:
6141 DECL_ACCESSORS(constructor, Object)
6142 DECL_ACCESSORS(internal_field_count, Object)
6143
6144 static inline ObjectTemplateInfo* cast(Object* obj);
6145
Ben Murdochb0fe1622011-05-05 13:52:32 +01006146#ifdef OBJECT_PRINT
6147 inline void ObjectTemplateInfoPrint() {
6148 ObjectTemplateInfoPrint(stdout);
6149 }
6150 void ObjectTemplateInfoPrint(FILE* out);
6151#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006152#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006153 void ObjectTemplateInfoVerify();
6154#endif
6155
6156 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
6157 static const int kInternalFieldCountOffset =
6158 kConstructorOffset + kPointerSize;
6159 static const int kSize = kInternalFieldCountOffset + kPointerSize;
6160};
6161
6162
6163class SignatureInfo: public Struct {
6164 public:
6165 DECL_ACCESSORS(receiver, Object)
6166 DECL_ACCESSORS(args, Object)
6167
6168 static inline SignatureInfo* cast(Object* obj);
6169
Ben Murdochb0fe1622011-05-05 13:52:32 +01006170#ifdef OBJECT_PRINT
6171 inline void SignatureInfoPrint() {
6172 SignatureInfoPrint(stdout);
6173 }
6174 void SignatureInfoPrint(FILE* out);
6175#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006176#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006177 void SignatureInfoVerify();
6178#endif
6179
6180 static const int kReceiverOffset = Struct::kHeaderSize;
6181 static const int kArgsOffset = kReceiverOffset + kPointerSize;
6182 static const int kSize = kArgsOffset + kPointerSize;
6183
6184 private:
6185 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
6186};
6187
6188
6189class TypeSwitchInfo: public Struct {
6190 public:
6191 DECL_ACCESSORS(types, Object)
6192
6193 static inline TypeSwitchInfo* cast(Object* obj);
6194
Ben Murdochb0fe1622011-05-05 13:52:32 +01006195#ifdef OBJECT_PRINT
6196 inline void TypeSwitchInfoPrint() {
6197 TypeSwitchInfoPrint(stdout);
6198 }
6199 void TypeSwitchInfoPrint(FILE* out);
6200#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006201#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006202 void TypeSwitchInfoVerify();
6203#endif
6204
6205 static const int kTypesOffset = Struct::kHeaderSize;
6206 static const int kSize = kTypesOffset + kPointerSize;
6207};
6208
6209
6210#ifdef ENABLE_DEBUGGER_SUPPORT
6211// The DebugInfo class holds additional information for a function being
6212// debugged.
6213class DebugInfo: public Struct {
6214 public:
6215 // The shared function info for the source being debugged.
6216 DECL_ACCESSORS(shared, SharedFunctionInfo)
6217 // Code object for the original code.
6218 DECL_ACCESSORS(original_code, Code)
6219 // Code object for the patched code. This code object is the code object
6220 // currently active for the function.
6221 DECL_ACCESSORS(code, Code)
6222 // Fixed array holding status information for each active break point.
6223 DECL_ACCESSORS(break_points, FixedArray)
6224
6225 // Check if there is a break point at a code position.
6226 bool HasBreakPoint(int code_position);
6227 // Get the break point info object for a code position.
6228 Object* GetBreakPointInfo(int code_position);
6229 // Clear a break point.
6230 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
6231 int code_position,
6232 Handle<Object> break_point_object);
6233 // Set a break point.
6234 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
6235 int source_position, int statement_position,
6236 Handle<Object> break_point_object);
6237 // Get the break point objects for a code position.
6238 Object* GetBreakPointObjects(int code_position);
6239 // Find the break point info holding this break point object.
6240 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
6241 Handle<Object> break_point_object);
6242 // Get the number of break points for this function.
6243 int GetBreakPointCount();
6244
6245 static inline DebugInfo* cast(Object* obj);
6246
Ben Murdochb0fe1622011-05-05 13:52:32 +01006247#ifdef OBJECT_PRINT
6248 inline void DebugInfoPrint() {
6249 DebugInfoPrint(stdout);
6250 }
6251 void DebugInfoPrint(FILE* out);
6252#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006253#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006254 void DebugInfoVerify();
6255#endif
6256
6257 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
6258 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
6259 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
6260 static const int kActiveBreakPointsCountIndex =
6261 kPatchedCodeIndex + kPointerSize;
6262 static const int kBreakPointsStateIndex =
6263 kActiveBreakPointsCountIndex + kPointerSize;
6264 static const int kSize = kBreakPointsStateIndex + kPointerSize;
6265
6266 private:
6267 static const int kNoBreakPointInfo = -1;
6268
6269 // Lookup the index in the break_points array for a code position.
6270 int GetBreakPointInfoIndex(int code_position);
6271
6272 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
6273};
6274
6275
6276// The BreakPointInfo class holds information for break points set in a
6277// function. The DebugInfo object holds a BreakPointInfo object for each code
6278// position with one or more break points.
6279class BreakPointInfo: public Struct {
6280 public:
6281 // The position in the code for the break point.
6282 DECL_ACCESSORS(code_position, Smi)
6283 // The position in the source for the break position.
6284 DECL_ACCESSORS(source_position, Smi)
6285 // The position in the source for the last statement before this break
6286 // position.
6287 DECL_ACCESSORS(statement_position, Smi)
6288 // List of related JavaScript break points.
6289 DECL_ACCESSORS(break_point_objects, Object)
6290
6291 // Removes a break point.
6292 static void ClearBreakPoint(Handle<BreakPointInfo> info,
6293 Handle<Object> break_point_object);
6294 // Set a break point.
6295 static void SetBreakPoint(Handle<BreakPointInfo> info,
6296 Handle<Object> break_point_object);
6297 // Check if break point info has this break point object.
6298 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
6299 Handle<Object> break_point_object);
6300 // Get the number of break points for this code position.
6301 int GetBreakPointCount();
6302
6303 static inline BreakPointInfo* cast(Object* obj);
6304
Ben Murdochb0fe1622011-05-05 13:52:32 +01006305#ifdef OBJECT_PRINT
6306 inline void BreakPointInfoPrint() {
6307 BreakPointInfoPrint(stdout);
6308 }
6309 void BreakPointInfoPrint(FILE* out);
6310#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006311#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006312 void BreakPointInfoVerify();
6313#endif
6314
6315 static const int kCodePositionIndex = Struct::kHeaderSize;
6316 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
6317 static const int kStatementPositionIndex =
6318 kSourcePositionIndex + kPointerSize;
6319 static const int kBreakPointObjectsIndex =
6320 kStatementPositionIndex + kPointerSize;
6321 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
6322
6323 private:
6324 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
6325};
6326#endif // ENABLE_DEBUGGER_SUPPORT
6327
6328
6329#undef DECL_BOOLEAN_ACCESSORS
6330#undef DECL_ACCESSORS
6331
6332
6333// Abstract base class for visiting, and optionally modifying, the
6334// pointers contained in Objects. Used in GC and serialization/deserialization.
6335class ObjectVisitor BASE_EMBEDDED {
6336 public:
6337 virtual ~ObjectVisitor() {}
6338
6339 // Visits a contiguous arrays of pointers in the half-open range
6340 // [start, end). Any or all of the values may be modified on return.
6341 virtual void VisitPointers(Object** start, Object** end) = 0;
6342
6343 // To allow lazy clearing of inline caches the visitor has
6344 // a rich interface for iterating over Code objects..
6345
6346 // Visits a code target in the instruction stream.
6347 virtual void VisitCodeTarget(RelocInfo* rinfo);
6348
Steve Block791712a2010-08-27 10:21:07 +01006349 // Visits a code entry in a JS function.
6350 virtual void VisitCodeEntry(Address entry_address);
6351
Ben Murdochb0fe1622011-05-05 13:52:32 +01006352 // Visits a global property cell reference in the instruction stream.
6353 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
6354
Steve Blocka7e24c12009-10-30 11:49:00 +00006355 // Visits a runtime entry in the instruction stream.
6356 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
6357
Steve Blockd0582a62009-12-15 09:54:21 +00006358 // Visits the resource of an ASCII or two-byte string.
6359 virtual void VisitExternalAsciiString(
6360 v8::String::ExternalAsciiStringResource** resource) {}
6361 virtual void VisitExternalTwoByteString(
6362 v8::String::ExternalStringResource** resource) {}
6363
Steve Blocka7e24c12009-10-30 11:49:00 +00006364 // Visits a debug call target in the instruction stream.
6365 virtual void VisitDebugTarget(RelocInfo* rinfo);
6366
6367 // Handy shorthand for visiting a single pointer.
6368 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
6369
6370 // Visits a contiguous arrays of external references (references to the C++
6371 // heap) in the half-open range [start, end). Any or all of the values
6372 // may be modified on return.
6373 virtual void VisitExternalReferences(Address* start, Address* end) {}
6374
6375 inline void VisitExternalReference(Address* p) {
6376 VisitExternalReferences(p, p + 1);
6377 }
6378
6379#ifdef DEBUG
6380 // Intended for serialization/deserialization checking: insert, or
6381 // check for the presence of, a tag at this position in the stream.
6382 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00006383#else
6384 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00006385#endif
6386};
6387
6388
Iain Merrick75681382010-08-19 15:07:18 +01006389class StructBodyDescriptor : public
6390 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
6391 public:
6392 static inline int SizeOf(Map* map, HeapObject* object) {
6393 return map->instance_size();
6394 }
6395};
6396
6397
Steve Blocka7e24c12009-10-30 11:49:00 +00006398// BooleanBit is a helper class for setting and getting a bit in an
6399// integer or Smi.
6400class BooleanBit : public AllStatic {
6401 public:
6402 static inline bool get(Smi* smi, int bit_position) {
6403 return get(smi->value(), bit_position);
6404 }
6405
6406 static inline bool get(int value, int bit_position) {
6407 return (value & (1 << bit_position)) != 0;
6408 }
6409
6410 static inline Smi* set(Smi* smi, int bit_position, bool v) {
6411 return Smi::FromInt(set(smi->value(), bit_position, v));
6412 }
6413
6414 static inline int set(int value, int bit_position, bool v) {
6415 if (v) {
6416 value |= (1 << bit_position);
6417 } else {
6418 value &= ~(1 << bit_position);
6419 }
6420 return value;
6421 }
6422};
6423
6424} } // namespace v8::internal
6425
6426#endif // V8_OBJECTS_H_