blob: ff13aedcce6f9ba667cccbcdc20b8fd49f4c3f2e [file] [log] [blame]
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001// Copyright 2011 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "builtins.h"
ager@chromium.orgea91cc52011-05-23 06:06:11 +000033#include "list.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "smart-pointer.h"
35#include "unicode-inl.h"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000036#if V8_TARGET_ARCH_ARM
37#include "arm/constants-arm.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000038#elif V8_TARGET_ARCH_MIPS
39#include "mips/constants-mips.h"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000040#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42//
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +000043// Most object types in the V8 JavaScript are described in this file.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044//
45// Inheritance hierarchy:
lrn@chromium.org303ada72010-10-27 09:33:13 +000046// - MaybeObject (an object or a failure)
47// - Failure (immediate for marking failed operation)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048// - Object
49// - Smi (immediate small integer)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050// - HeapObject (superclass for everything allocated in the heap)
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000051// - JSReceiver (suitable for property access)
52// - JSObject
53// - JSArray
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000054// - JSWeakMap
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000055// - JSRegExp
56// - JSFunction
57// - GlobalObject
58// - JSGlobalObject
59// - JSBuiltinsObject
60// - JSGlobalProxy
61// - JSValue
62// - JSMessageObject
63// - JSProxy
64// - JSFunctionProxy
ricow@chromium.org30ce4112010-05-31 10:38:25 +000065// - ByteArray
ricow@chromium.org30ce4112010-05-31 10:38:25 +000066// - ExternalArray
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +000067// - ExternalPixelArray
ricow@chromium.org30ce4112010-05-31 10:38:25 +000068// - ExternalByteArray
69// - ExternalUnsignedByteArray
70// - ExternalShortArray
71// - ExternalUnsignedShortArray
72// - ExternalIntArray
73// - ExternalUnsignedIntArray
74// - ExternalFloatArray
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000075// - FixedArrayBase
76// - FixedArray
77// - DescriptorArray
78// - HashTable
79// - Dictionary
80// - SymbolTable
81// - CompilationCacheTable
82// - CodeCacheHashTable
83// - MapCache
84// - Context
85// - JSFunctionResultCache
86// - SerializedScopeInfo
87// - FixedDoubleArray
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088// - String
89// - SeqString
ager@chromium.org7c537e22008-10-16 08:43:32 +000090// - SeqAsciiString
91// - SeqTwoByteString
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092// - ConsString
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093// - ExternalString
94// - ExternalAsciiString
95// - ExternalTwoByteString
96// - HeapNumber
97// - Code
98// - Map
99// - Oddball
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000100// - Foreign
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101// - SharedFunctionInfo
102// - Struct
103// - AccessorInfo
104// - AccessCheckInfo
105// - InterceptorInfo
106// - CallHandlerInfo
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000107// - TemplateInfo
108// - FunctionTemplateInfo
109// - ObjectTemplateInfo
110// - Script
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000111// - SignatureInfo
112// - TypeSwitchInfo
113// - DebugInfo
114// - BreakPointInfo
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000115// - CodeCache
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116//
117// Formats of Object*:
118// Smi: [31 bit signed int] 0
119// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
120// Failure: [30 bit signed int] 11
121
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122// Ecma-262 3rd 8.6.1
123enum PropertyAttributes {
124 NONE = v8::None,
125 READ_ONLY = v8::ReadOnly,
126 DONT_ENUM = v8::DontEnum,
127 DONT_DELETE = v8::DontDelete,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 ABSENT = 16 // Used in runtime to indicate a property is absent.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000129 // ABSENT can never be stored in or returned from a descriptor's attributes
130 // bitfield. It is only used as a return value meaning the attributes of
131 // a non-existent property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132};
133
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000134namespace v8 {
135namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
137
138// PropertyDetails captures type and attributes for a property.
139// They are used both in property dictionaries and instance descriptors.
140class PropertyDetails BASE_EMBEDDED {
141 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 PropertyDetails(PropertyAttributes attributes,
143 PropertyType type,
144 int index = 0) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000145 ASSERT(type != EXTERNAL_ARRAY_TRANSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146 ASSERT(TypeField::is_valid(type));
147 ASSERT(AttributesField::is_valid(attributes));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000148 ASSERT(StorageField::is_valid(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000149
150 value_ = TypeField::encode(type)
151 | AttributesField::encode(attributes)
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000152 | StorageField::encode(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153
154 ASSERT(type == this->type());
155 ASSERT(attributes == this->attributes());
156 ASSERT(index == this->index());
157 }
158
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000159 PropertyDetails(PropertyAttributes attributes,
160 PropertyType type,
161 ExternalArrayType array_type) {
162 ASSERT(type == EXTERNAL_ARRAY_TRANSITION);
163 ASSERT(TypeField::is_valid(type));
164 ASSERT(AttributesField::is_valid(attributes));
165 ASSERT(StorageField::is_valid(static_cast<int>(array_type)));
166
167 value_ = TypeField::encode(type)
168 | AttributesField::encode(attributes)
169 | StorageField::encode(static_cast<int>(array_type));
170
171 ASSERT(type == this->type());
172 ASSERT(attributes == this->attributes());
173 ASSERT(array_type == this->array_type());
174 }
175
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 // Conversion for storing details as Object*.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000177 explicit inline PropertyDetails(Smi* smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 inline Smi* AsSmi();
179
180 PropertyType type() { return TypeField::decode(value_); }
181
182 bool IsTransition() {
183 PropertyType t = type();
184 ASSERT(t != INTERCEPTOR);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000185 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
186 t == EXTERNAL_ARRAY_TRANSITION;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187 }
188
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000189 bool IsProperty() {
190 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
191 }
192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 PropertyAttributes attributes() { return AttributesField::decode(value_); }
194
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000195 int index() { return StorageField::decode(value_); }
196
197 ExternalArrayType array_type() {
198 ASSERT(type() == EXTERNAL_ARRAY_TRANSITION);
199 return static_cast<ExternalArrayType>(StorageField::decode(value_));
200 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000202 inline PropertyDetails AsDeleted();
203
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000204 static bool IsValidIndex(int index) {
205 return StorageField::is_valid(index);
206 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000207
208 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
209 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
210 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000211 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212
213 // Bit fields in value_ (type, shift, size). Must be public so the
214 // constants can be embedded in generated code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000215 class TypeField: public BitField<PropertyType, 0, 4> {};
216 class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
217 class DeletedField: public BitField<uint32_t, 7, 1> {};
218 class StorageField: public BitField<uint32_t, 8, 32-8> {};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
220 static const int kInitialIndex = 1;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 private:
223 uint32_t value_;
224};
225
ager@chromium.org32912102009-01-16 10:38:43 +0000226
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000227// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
228enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
229
ager@chromium.org32912102009-01-16 10:38:43 +0000230
231// PropertyNormalizationMode is used to specify whether to keep
232// inobject properties when normalizing properties of a JSObject.
233enum PropertyNormalizationMode {
234 CLEAR_INOBJECT_PROPERTIES,
235 KEEP_INOBJECT_PROPERTIES
236};
237
238
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000239// NormalizedMapSharingMode is used to specify whether a map may be shared
240// by different objects with normalized properties.
241enum NormalizedMapSharingMode {
242 UNIQUE_NORMALIZED_MAP,
243 SHARED_NORMALIZED_MAP
244};
245
246
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000247// Instance size sentinel for objects of variable size.
248static const int kVariableSizeSentinel = 0;
249
250
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251// All Maps have a field instance_type containing a InstanceType.
252// It describes the type of the instances.
253//
254// As an example, a JavaScript object is a heap object and its map
255// instance_type is JS_OBJECT_TYPE.
256//
257// The names of the string instance types are intended to systematically
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000258// mirror their encoding in the instance_type field of the map. The default
259// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
260// encoding is mentioned explicitly in the name. Likewise, the default
261// representation is considered sequential. It is not mentioned in the
262// name. The other representations (eg, CONS, EXTERNAL) are explicitly
263// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
264// symbol) or a STRING_TYPE (if it is not a symbol).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265//
266// NOTE: The following things are some that depend on the string types having
267// instance_types that are less than those of all other types:
268// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
269// Object::IsString.
270//
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000271// NOTE: Everything following JS_VALUE_TYPE is considered a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000272// JSObject for GC purposes. The first four entries here have typeof
273// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000274#define INSTANCE_TYPE_LIST_ALL(V) \
275 V(SYMBOL_TYPE) \
276 V(ASCII_SYMBOL_TYPE) \
277 V(CONS_SYMBOL_TYPE) \
278 V(CONS_ASCII_SYMBOL_TYPE) \
279 V(EXTERNAL_SYMBOL_TYPE) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000280 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000281 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
282 V(STRING_TYPE) \
283 V(ASCII_STRING_TYPE) \
284 V(CONS_STRING_TYPE) \
285 V(CONS_ASCII_STRING_TYPE) \
286 V(EXTERNAL_STRING_TYPE) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000287 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000288 V(EXTERNAL_ASCII_STRING_TYPE) \
289 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
290 \
291 V(MAP_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000292 V(CODE_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000293 V(ODDBALL_TYPE) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000294 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000295 \
296 V(HEAP_NUMBER_TYPE) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000297 V(FOREIGN_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000298 V(BYTE_ARRAY_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000299 /* Note: the order of these external array */ \
300 /* types is relied upon in */ \
301 /* Object::IsExternalArray(). */ \
302 V(EXTERNAL_BYTE_ARRAY_TYPE) \
303 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
304 V(EXTERNAL_SHORT_ARRAY_TYPE) \
305 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
306 V(EXTERNAL_INT_ARRAY_TYPE) \
307 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
308 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000309 V(EXTERNAL_PIXEL_ARRAY_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000310 V(FILLER_TYPE) \
311 \
312 V(ACCESSOR_INFO_TYPE) \
313 V(ACCESS_CHECK_INFO_TYPE) \
314 V(INTERCEPTOR_INFO_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000315 V(CALL_HANDLER_INFO_TYPE) \
316 V(FUNCTION_TEMPLATE_INFO_TYPE) \
317 V(OBJECT_TEMPLATE_INFO_TYPE) \
318 V(SIGNATURE_INFO_TYPE) \
319 V(TYPE_SWITCH_INFO_TYPE) \
320 V(SCRIPT_TYPE) \
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000321 V(CODE_CACHE_TYPE) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000322 V(POLYMORPHIC_CODE_CACHE_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000323 \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000324 V(FIXED_ARRAY_TYPE) \
325 V(SHARED_FUNCTION_INFO_TYPE) \
326 \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000327 V(JS_MESSAGE_OBJECT_TYPE) \
328 \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000329 V(JS_VALUE_TYPE) \
330 V(JS_OBJECT_TYPE) \
331 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
332 V(JS_GLOBAL_OBJECT_TYPE) \
333 V(JS_BUILTINS_OBJECT_TYPE) \
334 V(JS_GLOBAL_PROXY_TYPE) \
335 V(JS_ARRAY_TYPE) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000336 V(JS_PROXY_TYPE) \
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000337 V(JS_WEAK_MAP_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000338 V(JS_REGEXP_TYPE) \
339 \
340 V(JS_FUNCTION_TYPE) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000341 V(JS_FUNCTION_PROXY_TYPE) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000343#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000344#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
345 V(DEBUG_INFO_TYPE) \
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000346 V(BREAK_POINT_INFO_TYPE)
347#else
348#define INSTANCE_TYPE_LIST_DEBUGGER(V)
349#endif
350
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000351#define INSTANCE_TYPE_LIST(V) \
352 INSTANCE_TYPE_LIST_ALL(V) \
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000353 INSTANCE_TYPE_LIST_DEBUGGER(V)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000355
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000356// Since string types are not consecutive, this macro is used to
357// iterate over them.
358#define STRING_TYPE_LIST(V) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000359 V(SYMBOL_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000360 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000361 symbol, \
362 Symbol) \
363 V(ASCII_SYMBOL_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000364 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000365 ascii_symbol, \
366 AsciiSymbol) \
367 V(CONS_SYMBOL_TYPE, \
368 ConsString::kSize, \
369 cons_symbol, \
370 ConsSymbol) \
371 V(CONS_ASCII_SYMBOL_TYPE, \
372 ConsString::kSize, \
373 cons_ascii_symbol, \
374 ConsAsciiSymbol) \
375 V(EXTERNAL_SYMBOL_TYPE, \
376 ExternalTwoByteString::kSize, \
377 external_symbol, \
378 ExternalSymbol) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000379 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
380 ExternalTwoByteString::kSize, \
381 external_symbol_with_ascii_data, \
382 ExternalSymbolWithAsciiData) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000383 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
384 ExternalAsciiString::kSize, \
385 external_ascii_symbol, \
386 ExternalAsciiSymbol) \
387 V(STRING_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000388 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000389 string, \
390 String) \
391 V(ASCII_STRING_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000392 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000393 ascii_string, \
394 AsciiString) \
395 V(CONS_STRING_TYPE, \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000396 ConsString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000397 cons_string, \
398 ConsString) \
399 V(CONS_ASCII_STRING_TYPE, \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000400 ConsString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000401 cons_ascii_string, \
402 ConsAsciiString) \
403 V(EXTERNAL_STRING_TYPE, \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000404 ExternalTwoByteString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000405 external_string, \
406 ExternalString) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000407 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
408 ExternalTwoByteString::kSize, \
409 external_string_with_ascii_data, \
410 ExternalStringWithAsciiData) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000411 V(EXTERNAL_ASCII_STRING_TYPE, \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412 ExternalAsciiString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000413 external_ascii_string, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000414 ExternalAsciiString)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000415
416// A struct is a simple object a set of object-valued fields. Including an
417// object type in this causes the compiler to generate most of the boilerplate
418// code for the class including allocation and garbage collection routines,
419// casts and predicates. All you need to define is the class, methods and
420// object verification routines. Easy, no?
421//
422// Note that for subtle reasons related to the ordering or numerical values of
423// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
424// manually.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000425#define STRUCT_LIST_ALL(V) \
426 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
427 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
428 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
429 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
430 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
431 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
432 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
433 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000434 V(SCRIPT, Script, script) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000435 V(CODE_CACHE, CodeCache, code_cache) \
436 V(POLYMORPHIC_CODE_CACHE, PolymorphicCodeCache, polymorphic_code_cache)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000438#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000439#define STRUCT_LIST_DEBUGGER(V) \
440 V(DEBUG_INFO, DebugInfo, debug_info) \
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000441 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
442#else
443#define STRUCT_LIST_DEBUGGER(V)
444#endif
445
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000446#define STRUCT_LIST(V) \
447 STRUCT_LIST_ALL(V) \
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000448 STRUCT_LIST_DEBUGGER(V)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000449
450// We use the full 8 bits of the instance_type field to encode heap object
451// instance types. The high-order bit (bit 7) is set if the object is not a
452// string, and cleared if it is a string.
453const uint32_t kIsNotStringMask = 0x80;
454const uint32_t kStringTag = 0x0;
455const uint32_t kNotStringTag = 0x80;
456
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000457// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
458// There are not enough types that the non-string types (with bit 7 set) can
459// have bit 6 set too.
460const uint32_t kIsSymbolMask = 0x40;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461const uint32_t kNotSymbolTag = 0x0;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000462const uint32_t kSymbolTag = 0x40;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000464// If bit 7 is clear then bit 2 indicates whether the string consists of
465// two-byte characters or one-byte characters.
466const uint32_t kStringEncodingMask = 0x4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467const uint32_t kTwoByteStringTag = 0x0;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000468const uint32_t kAsciiStringTag = 0x4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000470// If bit 7 is clear, the low-order 2 bits indicate the representation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000471// of the string.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000472const uint32_t kStringRepresentationMask = 0x03;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473enum StringRepresentationTag {
474 kSeqStringTag = 0x0,
475 kConsStringTag = 0x1,
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000476 kExternalStringTag = 0x2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477};
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000478const uint32_t kIsConsStringMask = 0x1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000480// If bit 7 is clear, then bit 3 indicates whether this two-byte
481// string actually contains ascii data.
482const uint32_t kAsciiDataHintMask = 0x08;
483const uint32_t kAsciiDataHintTag = 0x08;
484
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000485
486// A ConsString with an empty string as the right side is a candidate
487// for being shortcut by the garbage collector unless it is a
488// symbol. It's not common to have non-flat symbols, so we do not
489// shortcut them thereby avoiding turning symbols into strings. See
490// heap.cc and mark-compact.cc.
491const uint32_t kShortcutTypeMask =
492 kIsNotStringMask |
493 kIsSymbolMask |
494 kStringRepresentationMask;
495const uint32_t kShortcutTypeTag = kConsStringTag;
496
497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498enum InstanceType {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000499 // String types.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000500 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000501 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000502 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000503 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000504 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
505 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
506 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000507 EXTERNAL_ASCII_SYMBOL_TYPE =
508 kAsciiStringTag | kSymbolTag | kExternalStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000509 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000510 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000511 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000512 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000513 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
514 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
515 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000516 // LAST_STRING_TYPE
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000517 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
518 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000519
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000520 // Objects allocated in their own spaces (never in new space).
521 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522 CODE_TYPE,
523 ODDBALL_TYPE,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000524 JS_GLOBAL_PROPERTY_CELL_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000525
526 // "Data", objects that cannot contain non-map-word pointers to heap
527 // objects.
528 HEAP_NUMBER_TYPE,
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000529 FOREIGN_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 BYTE_ARRAY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000531 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
ager@chromium.org3811b432009-10-28 14:53:37 +0000532 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
533 EXTERNAL_SHORT_ARRAY_TYPE,
534 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
535 EXTERNAL_INT_ARRAY_TYPE,
536 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000537 EXTERNAL_FLOAT_ARRAY_TYPE,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000538 EXTERNAL_DOUBLE_ARRAY_TYPE,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000539 EXTERNAL_PIXEL_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000540 FIXED_DOUBLE_ARRAY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000541 FILLER_TYPE, // LAST_DATA_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000542
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000543 // Structs.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 ACCESSOR_INFO_TYPE,
545 ACCESS_CHECK_INFO_TYPE,
546 INTERCEPTOR_INFO_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 CALL_HANDLER_INFO_TYPE,
548 FUNCTION_TEMPLATE_INFO_TYPE,
549 OBJECT_TEMPLATE_INFO_TYPE,
550 SIGNATURE_INFO_TYPE,
551 TYPE_SWITCH_INFO_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000552 SCRIPT_TYPE,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000553 CODE_CACHE_TYPE,
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000554 POLYMORPHIC_CODE_CACHE_TYPE,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000555 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
556 // is defined. However as include/v8.h contain some of the instance type
557 // constants always having them avoids them getting different numbers
558 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 DEBUG_INFO_TYPE,
560 BREAK_POINT_INFO_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000561
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000562 FIXED_ARRAY_TYPE,
563 SHARED_FUNCTION_INFO_TYPE,
564
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000565 JS_MESSAGE_OBJECT_TYPE,
566
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000567 JS_VALUE_TYPE, // FIRST_NON_CALLABLE_OBJECT_TYPE, FIRST_JS_RECEIVER_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 JS_OBJECT_TYPE,
ager@chromium.org32912102009-01-16 10:38:43 +0000569 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 JS_GLOBAL_OBJECT_TYPE,
571 JS_BUILTINS_OBJECT_TYPE,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000572 JS_GLOBAL_PROXY_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 JS_ARRAY_TYPE,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000574 JS_PROXY_TYPE,
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000575 JS_WEAK_MAP_TYPE,
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000576
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000577 JS_REGEXP_TYPE, // LAST_NONCALLABLE_SPEC_OBJECT_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000579 JS_FUNCTION_TYPE, // FIRST_CALLABLE_SPEC_OBJECT_TYPE
580 JS_FUNCTION_PROXY_TYPE, // LAST_CALLABLE_SPEC_OBJECT_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581
582 // Pseudo-types
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 FIRST_TYPE = 0x0,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000584 LAST_TYPE = JS_FUNCTION_PROXY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000585 INVALID_TYPE = FIRST_TYPE - 1,
586 FIRST_NONSTRING_TYPE = MAP_TYPE,
587 // Boundaries for testing for an external array.
588 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000589 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_PIXEL_ARRAY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000590 // Boundary for promotion to old data space/old pointer space.
591 LAST_DATA_TYPE = FILLER_TYPE,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000592 // Boundary for objects represented as JSReceiver (i.e. JSObject or JSProxy).
593 // Note that there is no range for JSObject or JSProxy, since their subtypes
594 // are not continuous in this enum! The enum ranges instead reflect the
595 // external class names, where proxies are treated as either ordinary objects,
596 // or functions.
597 FIRST_JS_RECEIVER_TYPE = JS_VALUE_TYPE,
598 LAST_JS_RECEIVER_TYPE = LAST_TYPE,
599 // Boundaries for testing the types for which typeof is "object".
600 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_VALUE_TYPE,
601 LAST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_REGEXP_TYPE,
602 // Boundaries for testing the types for which typeof is "function".
603 FIRST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_TYPE,
604 LAST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_PROXY_TYPE,
605 // Boundaries for testing whether the type is a JavaScript object.
606 FIRST_SPEC_OBJECT_TYPE = FIRST_NONCALLABLE_SPEC_OBJECT_TYPE,
607 LAST_SPEC_OBJECT_TYPE = LAST_CALLABLE_SPEC_OBJECT_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608};
609
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000610static const int kExternalArrayTypeCount = LAST_EXTERNAL_ARRAY_TYPE -
611 FIRST_EXTERNAL_ARRAY_TYPE + 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000613STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
614STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000615STATIC_CHECK(FOREIGN_TYPE == Internals::kForeignType);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000616
617
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618enum CompareResult {
619 LESS = -1,
620 EQUAL = 0,
621 GREATER = 1,
622
623 NOT_EQUAL = GREATER
624};
625
626
627#define DECL_BOOLEAN_ACCESSORS(name) \
628 inline bool name(); \
629 inline void set_##name(bool value); \
630
631
ager@chromium.org32912102009-01-16 10:38:43 +0000632#define DECL_ACCESSORS(name, type) \
633 inline type* name(); \
634 inline void set_##name(type* value, \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000635 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636
637
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000638class ElementsAccessor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639class StringStream;
640class ObjectVisitor;
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000641class DictionaryElementsAccessor;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000642
643struct ValueInfo : public Malloced {
644 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
645 InstanceType type;
646 Object* ptr;
647 const char* str;
648 double number;
649};
650
651
652// A template-ized version of the IsXXX functions.
653template <class C> static inline bool Is(Object* obj);
654
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000655class Failure;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000656
lrn@chromium.org303ada72010-10-27 09:33:13 +0000657class MaybeObject BASE_EMBEDDED {
658 public:
659 inline bool IsFailure();
660 inline bool IsRetryAfterGC();
661 inline bool IsOutOfMemory();
662 inline bool IsException();
663 INLINE(bool IsTheHole());
664 inline bool ToObject(Object** obj) {
665 if (IsFailure()) return false;
666 *obj = reinterpret_cast<Object*>(this);
667 return true;
668 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000669 inline Failure* ToFailureUnchecked() {
670 ASSERT(IsFailure());
671 return reinterpret_cast<Failure*>(this);
672 }
lrn@chromium.org303ada72010-10-27 09:33:13 +0000673 inline Object* ToObjectUnchecked() {
674 ASSERT(!IsFailure());
675 return reinterpret_cast<Object*>(this);
676 }
677 inline Object* ToObjectChecked() {
678 CHECK(!IsFailure());
679 return reinterpret_cast<Object*>(this);
680 }
681
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000682 template<typename T>
683 inline bool To(T** obj) {
684 if (IsFailure()) return false;
685 *obj = T::cast(reinterpret_cast<Object*>(this));
686 return true;
687 }
688
whesse@chromium.org023421e2010-12-21 12:19:12 +0000689#ifdef OBJECT_PRINT
lrn@chromium.org303ada72010-10-27 09:33:13 +0000690 // Prints this object with details.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000691 inline void Print() {
692 Print(stdout);
693 };
694 inline void PrintLn() {
695 PrintLn(stdout);
696 }
697 void Print(FILE* out);
698 void PrintLn(FILE* out);
699#endif
700#ifdef DEBUG
lrn@chromium.org303ada72010-10-27 09:33:13 +0000701 // Verifies the object.
702 void Verify();
703#endif
704};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000706
707#define OBJECT_TYPE_LIST(V) \
708 V(Smi) \
709 V(HeapObject) \
710 V(Number) \
711
712#define HEAP_OBJECT_TYPE_LIST(V) \
713 V(HeapNumber) \
714 V(String) \
715 V(Symbol) \
716 V(SeqString) \
717 V(ExternalString) \
718 V(ConsString) \
719 V(ExternalTwoByteString) \
720 V(ExternalAsciiString) \
721 V(SeqTwoByteString) \
722 V(SeqAsciiString) \
723 \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000724 V(ExternalArray) \
725 V(ExternalByteArray) \
726 V(ExternalUnsignedByteArray) \
727 V(ExternalShortArray) \
728 V(ExternalUnsignedShortArray) \
729 V(ExternalIntArray) \
730 V(ExternalUnsignedIntArray) \
731 V(ExternalFloatArray) \
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000732 V(ExternalDoubleArray) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000733 V(ExternalPixelArray) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000734 V(ByteArray) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000735 V(JSReceiver) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000736 V(JSObject) \
737 V(JSContextExtensionObject) \
738 V(Map) \
739 V(DescriptorArray) \
740 V(DeoptimizationInputData) \
741 V(DeoptimizationOutputData) \
742 V(FixedArray) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000743 V(FixedDoubleArray) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000744 V(Context) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000745 V(GlobalContext) \
746 V(JSFunction) \
747 V(Code) \
748 V(Oddball) \
749 V(SharedFunctionInfo) \
750 V(JSValue) \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000751 V(JSMessageObject) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000752 V(StringWrapper) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000753 V(Foreign) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000754 V(Boolean) \
755 V(JSArray) \
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000756 V(JSProxy) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000757 V(JSFunctionProxy) \
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000758 V(JSWeakMap) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000759 V(JSRegExp) \
760 V(HashTable) \
761 V(Dictionary) \
762 V(SymbolTable) \
763 V(JSFunctionResultCache) \
764 V(NormalizedMapCache) \
765 V(CompilationCacheTable) \
766 V(CodeCacheHashTable) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000767 V(PolymorphicCodeCacheHashTable) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000768 V(MapCache) \
769 V(Primitive) \
770 V(GlobalObject) \
771 V(JSGlobalObject) \
772 V(JSBuiltinsObject) \
773 V(JSGlobalProxy) \
774 V(UndetectableObject) \
775 V(AccessCheckNeeded) \
776 V(JSGlobalPropertyCell) \
777
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000778// Object is the abstract superclass for all classes in the
779// object hierarchy.
780// Object does not use any virtual functions to avoid the
781// allocation of the C++ vtable.
782// Since Smi and Failure are subclasses of Object no
783// data members can be present in Object.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000784class Object : public MaybeObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000785 public:
786 // Type testing.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000787#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
788 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
789 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
790#undef IS_TYPE_FUNCTION_DECL
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791
792 // Returns true if this object is an instance of the specified
793 // function template.
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +0000794 inline bool IsInstanceOf(FunctionTemplateInfo* type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795
796 inline bool IsStruct();
797#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
798 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
799#undef DECLARE_STRUCT_PREDICATE
800
ricow@chromium.org9fa09672011-07-25 11:05:35 +0000801 INLINE(bool IsSpecObject());
802
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 // Oddball testing.
804 INLINE(bool IsUndefined());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805 INLINE(bool IsNull());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000806 INLINE(bool IsTheHole()); // Shadows MaybeObject's implementation.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000807 INLINE(bool IsTrue());
808 INLINE(bool IsFalse());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000809 inline bool IsArgumentsMarker();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000810
811 // Extract the number.
812 inline double Number();
813
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000814 // Returns true if the object is of the correct type to be used as a
815 // implementation of a JSObject's elements.
816 inline bool HasValidElements();
817
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000818 inline bool HasSpecificClassOf(String* name);
819
lrn@chromium.org303ada72010-10-27 09:33:13 +0000820 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
821 Object* ToBoolean(); // ECMA-262 9.2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822
823 // Convert to a JSObject if needed.
824 // global_context is used when creating wrapper object.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000825 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826
827 // Converts this to a Smi if possible.
828 // Failure is returned otherwise.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000829 MUST_USE_RESULT inline MaybeObject* ToSmi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830
831 void Lookup(String* name, LookupResult* result);
832
833 // Property access.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000834 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
835 MUST_USE_RESULT inline MaybeObject* GetProperty(
836 String* key,
837 PropertyAttributes* attributes);
838 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
839 Object* receiver,
840 String* key,
841 PropertyAttributes* attributes);
842 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
843 LookupResult* result,
844 String* key,
845 PropertyAttributes* attributes);
846 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
847 Object* structure,
848 String* name,
849 Object* holder);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000850 MUST_USE_RESULT MaybeObject* GetPropertyWithHandler(Object* receiver,
851 String* name,
852 Object* handler);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000853 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
854 JSFunction* getter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855
lrn@chromium.org303ada72010-10-27 09:33:13 +0000856 inline MaybeObject* GetElement(uint32_t index);
857 // For use when we know that no exception can be thrown.
858 inline Object* GetElementNoExceptionThrown(uint32_t index);
859 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860
861 // Return the object's prototype (might be Heap::null_value()).
862 Object* GetPrototype();
863
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000864 // Tries to convert an object to an array index. Returns true and sets
865 // the output parameter if it succeeds.
866 inline bool ToArrayIndex(uint32_t* index);
867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868 // Returns true if this is a JSValue containing a string and the index is
869 // < the length of the string. Used to implement [] on strings.
870 inline bool IsStringObjectWithCharacterAt(uint32_t index);
871
872#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 // Verify a pointer is a valid object pointer.
874 static void VerifyPointer(Object* p);
875#endif
876
877 // Prints this object without details.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000878 inline void ShortPrint() {
879 ShortPrint(stdout);
880 }
881 void ShortPrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882
883 // Prints this object without details to a message accumulator.
884 void ShortPrint(StringStream* accumulator);
885
886 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
887 static Object* cast(Object* value) { return value; }
888
889 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +0000890 static const int kHeaderSize = 0; // Object does not take up any space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891
892 private:
893 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
894};
895
896
897// Smi represents integer Numbers that can be stored in 31 bits.
898// Smis are immediate which means they are NOT allocated in the heap.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000899// The this pointer has the following format: [31 bit signed int] 0
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000900// For long smis it has the following format:
901// [32 bit signed int] [31 bits zero padding] 0
902// Smi stands for small integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903class Smi: public Object {
904 public:
905 // Returns the integer value.
906 inline int value();
907
908 // Convert a value to a Smi object.
909 static inline Smi* FromInt(int value);
910
ager@chromium.org9085a012009-05-11 19:22:57 +0000911 static inline Smi* FromIntptr(intptr_t value);
912
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 // Returns whether value can be represented in a Smi.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000914 static inline bool IsValid(intptr_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
916 // Casting.
917 static inline Smi* cast(Object* object);
918
919 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000920 inline void SmiPrint() {
921 SmiPrint(stdout);
922 }
923 void SmiPrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000924 void SmiPrint(StringStream* accumulator);
925#ifdef DEBUG
926 void SmiVerify();
927#endif
928
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000929 static const int kMinValue = (-1 << (kSmiValueSize - 1));
930 static const int kMaxValue = -(kMinValue + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931
932 private:
933 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
934};
935
936
ager@chromium.org6f10e412009-02-13 10:11:16 +0000937// Failure is used for reporting out of memory situations and
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938// propagating exceptions through the runtime system. Failure objects
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000939// are transient and cannot occur as part of the object graph.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000940//
941// Failures are a single word, encoded as follows:
942// +-------------------------+---+--+--+
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000943// |.........unused..........|sss|tt|11|
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944// +-------------------------+---+--+--+
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000945// 7 6 4 32 10
946//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947//
948// The low two bits, 0-1, are the failure tag, 11. The next two bits,
949// 2-3, are a failure type tag 'tt' with possible values:
950// 00 RETRY_AFTER_GC
951// 01 EXCEPTION
952// 10 INTERNAL_ERROR
953// 11 OUT_OF_MEMORY_EXCEPTION
954//
955// The next three bits, 4-6, are an allocation space tag 'sss'. The
956// allocation space tag is 000 for all failure types except
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000957// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
958// allocation spaces (the encoding is found in globals.h).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959
960// Failure type tag info.
961const int kFailureTypeTagSize = 2;
962const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
963
lrn@chromium.org303ada72010-10-27 09:33:13 +0000964class Failure: public MaybeObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 public:
966 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
967 enum Type {
968 RETRY_AFTER_GC = 0,
969 EXCEPTION = 1, // Returning this marker tells the real exception
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000970 // is in Isolate::pending_exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 INTERNAL_ERROR = 2,
972 OUT_OF_MEMORY_EXCEPTION = 3
973 };
974
975 inline Type type() const;
976
977 // Returns the space that needs to be collected for RetryAfterGC failures.
978 inline AllocationSpace allocation_space() const;
979
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 inline bool IsInternalError() const;
981 inline bool IsOutOfMemoryException() const;
982
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000983 static inline Failure* RetryAfterGC(AllocationSpace space);
984 static inline Failure* RetryAfterGC(); // NEW_SPACE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000985 static inline Failure* Exception();
986 static inline Failure* InternalError();
987 static inline Failure* OutOfMemoryException();
988 // Casting.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000989 static inline Failure* cast(MaybeObject* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000990
991 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000992 inline void FailurePrint() {
993 FailurePrint(stdout);
994 }
995 void FailurePrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 void FailurePrint(StringStream* accumulator);
997#ifdef DEBUG
998 void FailureVerify();
999#endif
1000
1001 private:
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001002 inline intptr_t value() const;
1003 static inline Failure* Construct(Type type, intptr_t value = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001004
1005 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
1006};
1007
1008
kasper.lund7276f142008-07-30 08:49:36 +00001009// Heap objects typically have a map pointer in their first word. However,
1010// during GC other data (eg, mark bits, forwarding addresses) is sometimes
1011// encoded in the first word. The class MapWord is an abstraction of the
1012// value in a heap object's first word.
1013class MapWord BASE_EMBEDDED {
1014 public:
1015 // Normal state: the map word contains a map pointer.
1016
1017 // Create a map word from a map pointer.
1018 static inline MapWord FromMap(Map* map);
1019
1020 // View this map word as a map pointer.
1021 inline Map* ToMap();
1022
1023
1024 // Scavenge collection: the map word of live objects in the from space
1025 // contains a forwarding address (a heap object pointer in the to space).
1026
1027 // True if this map word is a forwarding address for a scavenge
1028 // collection. Only valid during a scavenge collection (specifically,
1029 // when all map words are heap object pointers, ie. not during a full GC).
1030 inline bool IsForwardingAddress();
1031
1032 // Create a map word from a forwarding address.
1033 static inline MapWord FromForwardingAddress(HeapObject* object);
1034
1035 // View this map word as a forwarding address.
1036 inline HeapObject* ToForwardingAddress();
1037
kasper.lund7276f142008-07-30 08:49:36 +00001038 // Marking phase of full collection: the map word of live objects is
1039 // marked, and may be marked as overflowed (eg, the object is live, its
1040 // children have not been visited, and it does not fit in the marking
1041 // stack).
1042
1043 // True if this map word's mark bit is set.
1044 inline bool IsMarked();
1045
1046 // Return this map word but with its mark bit set.
1047 inline void SetMark();
1048
1049 // Return this map word but with its mark bit cleared.
1050 inline void ClearMark();
1051
1052 // True if this map word's overflow bit is set.
1053 inline bool IsOverflowed();
1054
1055 // Return this map word but with its overflow bit set.
1056 inline void SetOverflow();
1057
1058 // Return this map word but with its overflow bit cleared.
1059 inline void ClearOverflow();
1060
1061
1062 // Compacting phase of a full compacting collection: the map word of live
1063 // objects contains an encoding of the original map address along with the
1064 // forwarding address (represented as an offset from the first live object
1065 // in the same page as the (old) object address).
1066
1067 // Create a map word from a map address and a forwarding address offset.
1068 static inline MapWord EncodeAddress(Address map_address, int offset);
1069
1070 // Return the map address encoded in this map word.
1071 inline Address DecodeMapAddress(MapSpace* map_space);
1072
1073 // Return the forwarding offset encoded in this map word.
1074 inline int DecodeOffset();
1075
1076
1077 // During serialization: the map word is used to hold an encoded
1078 // address, and possibly a mark bit (set and cleared with SetMark
1079 // and ClearMark).
1080
1081 // Create a map word from an encoded address.
1082 static inline MapWord FromEncodedAddress(Address address);
1083
1084 inline Address ToEncodedAddress();
1085
kasper.lund7276f142008-07-30 08:49:36 +00001086 // Bits used by the marking phase of the garbage collector.
1087 //
ager@chromium.org6f10e412009-02-13 10:11:16 +00001088 // The first word of a heap object is normally a map pointer. The last two
kasper.lund7276f142008-07-30 08:49:36 +00001089 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1090 // mark an object as live and/or overflowed:
1091 // last bit = 0, marked as alive
1092 // second bit = 1, overflowed
1093 // An object is only marked as overflowed when it is marked as live while
1094 // the marking stack is overflowed.
1095 static const int kMarkingBit = 0; // marking bit
1096 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1097 static const int kOverflowBit = 1; // overflow bit
1098 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1099
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001100 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1101 // used.
kasper.lund7276f142008-07-30 08:49:36 +00001102 // +-----------------+------------------+-----------------+
1103 // |forwarding offset|page offset of map|page index of map|
1104 // +-----------------+------------------+-----------------+
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001105 // ^ ^ ^
1106 // | | |
1107 // | | kMapPageIndexBits
1108 // | kMapPageOffsetBits
1109 // kForwardingOffsetBits
1110 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1111 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1112#ifdef V8_HOST_ARCH_64_BIT
1113 static const int kMapPageIndexBits = 16;
1114#else
1115 // Use all the 32-bits to encode on a 32-bit platform.
1116 static const int kMapPageIndexBits =
1117 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1118#endif
kasper.lund7276f142008-07-30 08:49:36 +00001119
1120 static const int kMapPageIndexShift = 0;
1121 static const int kMapPageOffsetShift =
1122 kMapPageIndexShift + kMapPageIndexBits;
1123 static const int kForwardingOffsetShift =
1124 kMapPageOffsetShift + kMapPageOffsetBits;
1125
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001126 // Bit masks covering the different parts the encoding.
1127 static const uintptr_t kMapPageIndexMask =
kasper.lund7276f142008-07-30 08:49:36 +00001128 (1 << kMapPageOffsetShift) - 1;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001129 static const uintptr_t kMapPageOffsetMask =
kasper.lund7276f142008-07-30 08:49:36 +00001130 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001131 static const uintptr_t kForwardingOffsetMask =
kasper.lund7276f142008-07-30 08:49:36 +00001132 ~(kMapPageIndexMask | kMapPageOffsetMask);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001133
1134 private:
1135 // HeapObject calls the private constructor and directly reads the value.
1136 friend class HeapObject;
1137
1138 explicit MapWord(uintptr_t value) : value_(value) {}
1139
1140 uintptr_t value_;
kasper.lund7276f142008-07-30 08:49:36 +00001141};
1142
1143
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001144// HeapObject is the superclass for all classes describing heap allocated
1145// objects.
1146class HeapObject: public Object {
1147 public:
kasper.lund7276f142008-07-30 08:49:36 +00001148 // [map]: Contains a map which contains the object's reflective
1149 // information.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 inline Map* map();
1151 inline void set_map(Map* value);
1152
kasper.lund7276f142008-07-30 08:49:36 +00001153 // During garbage collection, the map word of a heap object does not
1154 // necessarily contain a map pointer.
1155 inline MapWord map_word();
1156 inline void set_map_word(MapWord map_word);
1157
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001158 // The Heap the object was allocated in. Used also to access Isolate.
1159 // This method can not be used during GC, it ASSERTs this.
1160 inline Heap* GetHeap();
1161 // Convenience method to get current isolate. This method can be
1162 // accessed only when its result is the same as
1163 // Isolate::Current(), it ASSERTs this. See also comment for GetHeap.
1164 inline Isolate* GetIsolate();
1165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166 // Converts an address to a HeapObject pointer.
1167 static inline HeapObject* FromAddress(Address address);
1168
1169 // Returns the address of this HeapObject.
1170 inline Address address();
1171
1172 // Iterates over pointers contained in the object (including the Map)
1173 void Iterate(ObjectVisitor* v);
1174
1175 // Iterates over all pointers contained in the object except the
1176 // first map pointer. The object type is given in the first
1177 // parameter. This function does not access the map pointer in the
1178 // object, and so is safe to call while the map pointer is modified.
1179 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 // Returns the heap object's size in bytes
1182 inline int Size();
1183
1184 // Given a heap object's map pointer, returns the heap size in bytes
1185 // Useful when the map pointer field is used for other purposes.
1186 // GC internal.
1187 inline int SizeFromMap(Map* map);
1188
kasper.lund7276f142008-07-30 08:49:36 +00001189 // Support for the marking heap objects during the marking phase of GC.
1190 // True if the object is marked live.
1191 inline bool IsMarked();
1192
1193 // Mutate this object's map pointer to indicate that the object is live.
1194 inline void SetMark();
1195
1196 // Mutate this object's map pointer to remove the indication that the
1197 // object is live (ie, partially restore the map pointer).
1198 inline void ClearMark();
1199
1200 // True if this object is marked as overflowed. Overflowed objects have
1201 // been reached and marked during marking of the heap, but their children
1202 // have not necessarily been marked and they have not been pushed on the
1203 // marking stack.
1204 inline bool IsOverflowed();
1205
1206 // Mutate this object's map pointer to indicate that the object is
1207 // overflowed.
1208 inline void SetOverflow();
1209
1210 // Mutate this object's map pointer to remove the indication that the
1211 // object is overflowed (ie, partially restore the map pointer).
1212 inline void ClearOverflow();
1213
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001214 // Returns the field at offset in obj, as a read/write Object* reference.
1215 // Does no checking, and is safe to use during GC, while maps are invalid.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001216 // Does not invoke write barrier, so should only be assigned to
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001217 // during marking GC.
1218 static inline Object** RawField(HeapObject* obj, int offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219
1220 // Casting.
1221 static inline HeapObject* cast(Object* obj);
1222
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001223 // Return the write barrier mode for this. Callers of this function
1224 // must be able to present a reference to an AssertNoAllocation
1225 // object as a sign that they are not going to use this function
1226 // from code that allocates and thus invalidates the returned write
1227 // barrier mode.
1228 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230 // Dispatched behavior.
1231 void HeapObjectShortPrint(StringStream* accumulator);
whesse@chromium.org023421e2010-12-21 12:19:12 +00001232#ifdef OBJECT_PRINT
1233 inline void HeapObjectPrint() {
1234 HeapObjectPrint(stdout);
1235 }
1236 void HeapObjectPrint(FILE* out);
1237#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 void HeapObjectVerify();
1240 inline void VerifyObjectField(int offset);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001241 inline void VerifySmiField(int offset);
whesse@chromium.org023421e2010-12-21 12:19:12 +00001242#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243
whesse@chromium.org023421e2010-12-21 12:19:12 +00001244#ifdef OBJECT_PRINT
1245 void PrintHeader(FILE* out, const char* id);
1246#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247
whesse@chromium.org023421e2010-12-21 12:19:12 +00001248#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 // Verify a pointer is a valid HeapObject pointer that points to object
1250 // areas in the heap.
1251 static void VerifyHeapPointer(Object* p);
1252#endif
1253
1254 // Layout description.
1255 // First field in a heap object is map.
ager@chromium.org236ad962008-09-25 09:45:57 +00001256 static const int kMapOffset = Object::kHeaderSize;
1257 static const int kHeaderSize = kMapOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001259 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 protected:
1262 // helpers for calling an ObjectVisitor to iterate over pointers in the
1263 // half-open range [start, end) specified as integer offsets
1264 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1265 // as above, for the single element at "offset"
1266 inline void IteratePointer(ObjectVisitor* v, int offset);
1267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 private:
1269 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1270};
1271
1272
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001273#define SLOT_ADDR(obj, offset) \
1274 reinterpret_cast<Object**>((obj)->address() + offset)
1275
1276// This class describes a body of an object of a fixed size
1277// in which all pointer fields are located in the [start_offset, end_offset)
1278// interval.
1279template<int start_offset, int end_offset, int size>
1280class FixedBodyDescriptor {
1281 public:
1282 static const int kStartOffset = start_offset;
1283 static const int kEndOffset = end_offset;
1284 static const int kSize = size;
1285
1286 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1287
1288 template<typename StaticVisitor>
1289 static inline void IterateBody(HeapObject* obj) {
1290 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1291 SLOT_ADDR(obj, end_offset));
1292 }
1293};
1294
1295
1296// This class describes a body of an object of a variable size
1297// in which all pointer fields are located in the [start_offset, object_size)
1298// interval.
1299template<int start_offset>
1300class FlexibleBodyDescriptor {
1301 public:
1302 static const int kStartOffset = start_offset;
1303
1304 static inline void IterateBody(HeapObject* obj,
1305 int object_size,
1306 ObjectVisitor* v);
1307
1308 template<typename StaticVisitor>
1309 static inline void IterateBody(HeapObject* obj, int object_size) {
1310 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1311 SLOT_ADDR(obj, object_size));
1312 }
1313};
1314
1315#undef SLOT_ADDR
1316
1317
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318// The HeapNumber class describes heap allocated numbers that cannot be
1319// represented in a Smi (small integer)
1320class HeapNumber: public HeapObject {
1321 public:
1322 // [value]: number value.
1323 inline double value();
1324 inline void set_value(double value);
1325
1326 // Casting.
1327 static inline HeapNumber* cast(Object* obj);
1328
1329 // Dispatched behavior.
1330 Object* HeapNumberToBoolean();
whesse@chromium.org023421e2010-12-21 12:19:12 +00001331 inline void HeapNumberPrint() {
1332 HeapNumberPrint(stdout);
1333 }
1334 void HeapNumberPrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001335 void HeapNumberPrint(StringStream* accumulator);
1336#ifdef DEBUG
1337 void HeapNumberVerify();
1338#endif
1339
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001340 inline int get_exponent();
1341 inline int get_sign();
1342
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00001344 static const int kValueOffset = HeapObject::kHeaderSize;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001345 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1346 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1347 // little endian apart from non-EABI arm which is little endian with big
1348 // endian floating point word ordering!
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001349 static const int kMantissaOffset = kValueOffset;
1350 static const int kExponentOffset = kValueOffset + 4;
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 static const int kSize = kValueOffset + kDoubleSize;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001353 static const uint32_t kSignMask = 0x80000000u;
1354 static const uint32_t kExponentMask = 0x7ff00000u;
1355 static const uint32_t kMantissaMask = 0xfffffu;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001356 static const int kMantissaBits = 52;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001357 static const int kExponentBits = 11;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001358 static const int kExponentBias = 1023;
1359 static const int kExponentShift = 20;
1360 static const int kMantissaBitsInTopWord = 20;
1361 static const int kNonMantissaBitsInTopWord = 12;
1362
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001363 private:
1364 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1365};
1366
1367
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001368// JSReceiver includes types on which properties can be defined, i.e.,
1369// JSObject and JSProxy.
1370class JSReceiver: public HeapObject {
1371 public:
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001372 enum DeleteMode {
1373 NORMAL_DELETION,
1374 STRICT_DELETION,
1375 FORCE_DELETION
1376 };
1377
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001378 // Casting.
1379 static inline JSReceiver* cast(Object* obj);
1380
1381 // Can cause GC.
1382 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1383 Object* value,
1384 PropertyAttributes attributes,
1385 StrictModeFlag strict_mode);
1386 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1387 String* key,
1388 Object* value,
1389 PropertyAttributes attributes,
1390 StrictModeFlag strict_mode);
1391
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00001392 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1393
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001394 // Returns the class name ([[Class]] property in the specification).
1395 String* class_name();
1396
1397 // Returns the constructor name (the name (possibly, inferred name) of the
1398 // function that was used to instantiate the object).
1399 String* constructor_name();
1400
1401 inline PropertyAttributes GetPropertyAttribute(String* name);
1402 PropertyAttributes GetPropertyAttributeWithReceiver(JSReceiver* receiver,
1403 String* name);
1404 PropertyAttributes GetLocalPropertyAttribute(String* name);
1405
1406 // Can cause a GC.
rossberg@chromium.org717967f2011-07-20 13:44:42 +00001407 inline bool HasProperty(String* name);
1408 inline bool HasLocalProperty(String* name);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001409
1410 // Return the object's prototype (might be Heap::null_value()).
1411 inline Object* GetPrototype();
1412
1413 // Set the object's prototype (only JSReceiver and null are allowed).
1414 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1415 bool skip_hidden_prototypes);
1416
1417 // Lookup a property. If found, the result is valid and has
1418 // detailed information.
1419 void LocalLookup(String* name, LookupResult* result);
1420 void Lookup(String* name, LookupResult* result);
1421
1422 private:
1423 PropertyAttributes GetPropertyAttribute(JSReceiver* receiver,
1424 LookupResult* result,
1425 String* name,
1426 bool continue_search);
1427
1428 DISALLOW_IMPLICIT_CONSTRUCTORS(JSReceiver);
1429};
1430
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431// The JSObject describes real heap allocated JavaScript objects with
1432// properties.
1433// Note that the map of JSObject changes during execution to enable inline
1434// caching.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001435class JSObject: public JSReceiver {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 public:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001437 enum ElementsKind {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001438 // The "fast" kind for tagged values. Must be first to make it possible
1439 // to efficiently check maps if they have fast elements.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001440 FAST_ELEMENTS,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001441
1442 // The "fast" kind for unwrapped, non-tagged double values.
1443 FAST_DOUBLE_ELEMENTS,
1444
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001445 // The "slow" kind.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001446 DICTIONARY_ELEMENTS,
whesse@chromium.org7b260152011-06-20 15:33:18 +00001447 NON_STRICT_ARGUMENTS_ELEMENTS,
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001448 // The "fast" kind for external arrays
ager@chromium.org3811b432009-10-28 14:53:37 +00001449 EXTERNAL_BYTE_ELEMENTS,
1450 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1451 EXTERNAL_SHORT_ELEMENTS,
1452 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1453 EXTERNAL_INT_ELEMENTS,
1454 EXTERNAL_UNSIGNED_INT_ELEMENTS,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001455 EXTERNAL_FLOAT_ELEMENTS,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001456 EXTERNAL_DOUBLE_ELEMENTS,
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001457 EXTERNAL_PIXEL_ELEMENTS,
1458
1459 // Derived constants from ElementsKind
1460 FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_BYTE_ELEMENTS,
1461 LAST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS,
1462 FIRST_ELEMENTS_KIND = FAST_ELEMENTS,
1463 LAST_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001464 };
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001465
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001466 static const int kElementsKindCount =
1467 LAST_ELEMENTS_KIND - FIRST_ELEMENTS_KIND + 1;
1468
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 // [properties]: Backing storage for properties.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001470 // properties is a FixedArray in the fast case and a Dictionary in the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001471 // slow case.
1472 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 inline void initialize_properties();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 inline bool HasFastProperties();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001475 inline StringDictionary* property_dictionary(); // Gets slow properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001477 // [elements]: The elements (properties with names that are integers).
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001478 //
1479 // Elements can be in two general modes: fast and slow. Each mode
1480 // corrensponds to a set of object representations of elements that
1481 // have something in common.
1482 //
1483 // In the fast mode elements is a FixedArray and so each element can
1484 // be quickly accessed. This fact is used in the generated code. The
whesse@chromium.org7b260152011-06-20 15:33:18 +00001485 // elements array can have one of three maps in this mode:
1486 // fixed_array_map, non_strict_arguments_elements_map or
1487 // fixed_cow_array_map (for copy-on-write arrays). In the latter case
1488 // the elements array may be shared by a few objects and so before
1489 // writing to any element the array must be copied. Use
1490 // EnsureWritableFastElements in this case.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001491 //
whesse@chromium.org7b260152011-06-20 15:33:18 +00001492 // In the slow mode the elements is either a NumberDictionary, an
1493 // ExternalArray, or a FixedArray parameter map for a (non-strict)
1494 // arguments object.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001495 DECL_ACCESSORS(elements, HeapObject)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001496 inline void initialize_elements();
lrn@chromium.org303ada72010-10-27 09:33:13 +00001497 MUST_USE_RESULT inline MaybeObject* ResetElements();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001498 inline ElementsKind GetElementsKind();
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00001499 inline ElementsAccessor* GetElementsAccessor();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001500 inline bool HasFastElements();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001501 inline bool HasFastDoubleElements();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001502 inline bool HasDictionaryElements();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001503 inline bool HasExternalPixelElements();
ager@chromium.org3811b432009-10-28 14:53:37 +00001504 inline bool HasExternalArrayElements();
1505 inline bool HasExternalByteElements();
1506 inline bool HasExternalUnsignedByteElements();
1507 inline bool HasExternalShortElements();
1508 inline bool HasExternalUnsignedShortElements();
1509 inline bool HasExternalIntElements();
1510 inline bool HasExternalUnsignedIntElements();
1511 inline bool HasExternalFloatElements();
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001512 inline bool HasExternalDoubleElements();
whesse@chromium.org7b260152011-06-20 15:33:18 +00001513 bool HasFastArgumentsElements();
1514 bool HasDictionaryArgumentsElements();
ager@chromium.org5c838252010-02-19 08:53:10 +00001515 inline bool AllowsSetElementsLength();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001516 inline NumberDictionary* element_dictionary(); // Gets slow elements.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001517
1518 // Requires: HasFastElements().
lrn@chromium.org303ada72010-10-27 09:33:13 +00001519 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520
ager@chromium.org5ec48922009-05-05 07:25:34 +00001521 // Collects elements starting at index 0.
1522 // Undefined values are placed after non-undefined values.
1523 // Returns the number of non-undefined values.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001524 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
ager@chromium.org5ec48922009-05-05 07:25:34 +00001525 // As PrepareElementsForSort, but only on objects where elements is
1526 // a dictionary, and it will stay a dictionary.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001527 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
ager@chromium.org5ec48922009-05-05 07:25:34 +00001528
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001529 MUST_USE_RESULT MaybeObject* SetPropertyForResult(LookupResult* result,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001530 String* key,
1531 Object* value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001532 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001533 StrictModeFlag strict_mode);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001534 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1535 LookupResult* result,
1536 String* name,
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001537 Object* value,
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001538 bool check_prototype,
1539 StrictModeFlag strict_mode);
1540 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(
1541 Object* structure,
1542 String* name,
1543 Object* value,
1544 JSObject* holder,
1545 StrictModeFlag strict_mode);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001546 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1547 Object* value);
1548 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1549 String* name,
1550 Object* value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001551 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001552 StrictModeFlag strict_mode);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001553 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1554 String* name,
1555 Object* value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001556 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001557 StrictModeFlag strict_mode);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001558 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
lrn@chromium.org303ada72010-10-27 09:33:13 +00001559 String* key,
1560 Object* value,
1561 PropertyAttributes attributes);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001562
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001563 // Retrieve a value in a normalized object given a lookup result.
1564 // Handles the special representation of JS global objects.
1565 Object* GetNormalizedProperty(LookupResult* result);
1566
1567 // Sets the property value in a normalized object given a lookup result.
1568 // Handles the special representation of JS global objects.
1569 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1570
1571 // Sets the property value in a normalized object given (key, value, details).
1572 // Handles the special representation of JS global objects.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001573 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1574 Object* value,
1575 PropertyDetails details);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001576
1577 // Deletes the named property in a normalized object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001578 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1579 DeleteMode mode);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001580
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 // Retrieve interceptors.
1582 InterceptorInfo* GetNamedInterceptor();
1583 InterceptorInfo* GetIndexedInterceptor();
1584
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001585 // Used from JSReceiver.
1586 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1587 String* name,
1588 bool continue_search);
1589 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1590 String* name,
1591 bool continue_search);
1592 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1593 Object* receiver,
1594 LookupResult* result,
1595 String* name,
1596 bool continue_search);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001597
lrn@chromium.org303ada72010-10-27 09:33:13 +00001598 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1599 bool is_getter,
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001600 Object* fun,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001601 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 Object* LookupAccessor(String* name, bool is_getter);
1603
lrn@chromium.org303ada72010-10-27 09:33:13 +00001604 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001605
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606 // Used from Object::GetProperty().
lrn@chromium.org303ada72010-10-27 09:33:13 +00001607 MaybeObject* GetPropertyWithFailedAccessCheck(
1608 Object* receiver,
1609 LookupResult* result,
1610 String* name,
1611 PropertyAttributes* attributes);
1612 MaybeObject* GetPropertyWithInterceptor(
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001613 JSReceiver* receiver,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001614 String* name,
1615 PropertyAttributes* attributes);
1616 MaybeObject* GetPropertyPostInterceptor(
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001617 JSReceiver* receiver,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001618 String* name,
1619 PropertyAttributes* attributes);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001620 MaybeObject* GetLocalPropertyPostInterceptor(JSReceiver* receiver,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001621 String* name,
1622 PropertyAttributes* attributes);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001623
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001624 // Returns true if this is an instance of an api function and has
1625 // been modified since it was created. May give false positives.
1626 bool IsDirty();
1627
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001628 // If the receiver is a JSGlobalProxy this method will return its prototype,
1629 // otherwise the result is the receiver itself.
1630 inline Object* BypassGlobalProxy();
1631
1632 // Accessors for hidden properties object.
1633 //
1634 // Hidden properties are not local properties of the object itself.
1635 // Instead they are stored on an auxiliary JSObject stored as a local
1636 // property with a special name Heap::hidden_symbol(). But if the
1637 // receiver is a JSGlobalProxy then the auxiliary object is a property
1638 // of its prototype.
1639 //
1640 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1641 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1642 // holder.
1643 //
1644 // These accessors do not touch interceptors or accessors.
1645 inline bool HasHiddenPropertiesObject();
1646 inline Object* GetHiddenPropertiesObject();
lrn@chromium.org303ada72010-10-27 09:33:13 +00001647 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1648 Object* hidden_obj);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001649
vegorov@chromium.org7943d462011-08-01 11:41:52 +00001650 // Indicates whether the hidden properties object should be created.
1651 enum HiddenPropertiesFlag { ALLOW_CREATION, OMIT_CREATION };
1652
1653 // Retrieves the hidden properties object.
1654 //
1655 // The undefined value might be returned in case no hidden properties object
1656 // is present and creation was omitted.
1657 inline bool HasHiddenProperties();
1658 MUST_USE_RESULT MaybeObject* GetHiddenProperties(HiddenPropertiesFlag flag);
1659
1660 // Retrieves a permanent object identity hash code.
1661 //
1662 // The identity hash is stored as a hidden property. The undefined value might
1663 // be returned in case no hidden properties object is present and creation was
1664 // omitted.
1665 MUST_USE_RESULT MaybeObject* GetIdentityHash(HiddenPropertiesFlag flag);
1666
lrn@chromium.org303ada72010-10-27 09:33:13 +00001667 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1668 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669
1670 // Tests for the fast common case for property enumeration.
1671 bool IsSimpleEnum();
1672
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001673 // Do we want to keep the elements in fast case when increasing the
1674 // capacity?
1675 bool ShouldConvertToSlowElements(int new_capacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 // Returns true if the backing storage for the slow-case elements of
1677 // this object takes up nearly as much space as a fast-case backing
1678 // storage would. In that case the JSObject should have fast
1679 // elements.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001680 bool ShouldConvertToFastElements();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001681 // Returns true if the elements of JSObject contains only values that can be
1682 // represented in a FixedDoubleArray.
ricow@chromium.org2c99e282011-07-28 09:15:17 +00001683 bool CanConvertToFastDoubleElements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 // Tells whether the index'th element is present.
1686 inline bool HasElement(uint32_t index);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001687 bool HasElementWithReceiver(JSReceiver* receiver, uint32_t index);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001688
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001689 // Computes the new capacity when expanding the elements of a JSObject.
1690 static int NewElementsCapacity(int old_capacity) {
1691 // (old_capacity + 50%) + 16
1692 return old_capacity + (old_capacity >> 1) + 16;
1693 }
1694
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001695 // Tells whether the index'th element is present and how it is stored.
1696 enum LocalElementType {
1697 // There is no element with given index.
1698 UNDEFINED_ELEMENT,
1699
1700 // Element with given index is handled by interceptor.
1701 INTERCEPTED_ELEMENT,
1702
1703 // Element with given index is character in string.
1704 STRING_CHARACTER_ELEMENT,
1705
1706 // Element with given index is stored in fast backing store.
1707 FAST_ELEMENT,
1708
1709 // Element with given index is stored in slow backing store.
1710 DICTIONARY_ELEMENT
1711 };
1712
1713 LocalElementType HasLocalElement(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001715 bool HasElementWithInterceptor(JSReceiver* receiver, uint32_t index);
1716 bool HasElementPostInterceptor(JSReceiver* receiver, uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001718 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1719 Object* value,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001720 StrictModeFlag strict_mode,
whesse@chromium.org7b260152011-06-20 15:33:18 +00001721 bool check_prototype);
1722 MUST_USE_RESULT MaybeObject* SetDictionaryElement(uint32_t index,
1723 Object* value,
1724 StrictModeFlag strict_mode,
1725 bool check_prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001726
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001727 MUST_USE_RESULT MaybeObject* SetFastDoubleElement(
1728 uint32_t index,
1729 Object* value,
1730 StrictModeFlag strict_mode,
1731 bool check_prototype = true);
1732
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 // Set the index'th array element.
1734 // A Failure object is returned if GC is needed.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001735 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1736 Object* value,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001737 StrictModeFlag strict_mode,
whesse@chromium.org7b260152011-06-20 15:33:18 +00001738 bool check_prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739
1740 // Returns the index'th element.
1741 // The undefined object if index is out of bounds.
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001742 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743
whesse@chromium.org7b260152011-06-20 15:33:18 +00001744 // Replace the elements' backing store with fast elements of the given
1745 // capacity. Update the length for JSArrays. Returns the new backing
1746 // store.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001747 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1748 int length);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001749 MUST_USE_RESULT MaybeObject* SetFastDoubleElementsCapacityAndLength(
1750 int capacity,
1751 int length);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001752 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753
1754 // Lookup interceptors are used for handling properties controlled by host
1755 // objects.
1756 inline bool HasNamedInterceptor();
1757 inline bool HasIndexedInterceptor();
1758
1759 // Support functions for v8 api (needed for correct interceptor behavior).
1760 bool HasRealNamedProperty(String* key);
1761 bool HasRealElementProperty(uint32_t index);
1762 bool HasRealNamedCallbackProperty(String* key);
1763
1764 // Initializes the array to a certain length
lrn@chromium.org303ada72010-10-27 09:33:13 +00001765 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766
1767 // Get the header size for a JSObject. Used to compute the index of
1768 // internal fields as well as the number of internal fields.
1769 inline int GetHeaderSize();
1770
1771 inline int GetInternalFieldCount();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001772 inline int GetInternalFieldOffset(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 inline Object* GetInternalField(int index);
1774 inline void SetInternalField(int index, Object* value);
1775
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776 // Lookup a property. If found, the result is valid and has
1777 // detailed information.
1778 void LocalLookup(String* name, LookupResult* result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779
1780 // The following lookup functions skip interceptors.
1781 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1782 void LookupRealNamedProperty(String* name, LookupResult* result);
1783 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1784 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001785 MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001786 uint32_t index, Object* value, bool* found, StrictModeFlag strict_mode);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001787 void LookupCallback(String* name, LookupResult* result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788
1789 // Returns the number of properties on this object filtering out properties
1790 // with the specified attributes (ignoring interceptors).
1791 int NumberOfLocalProperties(PropertyAttributes filter);
1792 // Returns the number of enumerable properties (ignoring interceptors).
1793 int NumberOfEnumProperties();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001794 // Fill in details for properties into storage starting at the specified
1795 // index.
1796 void GetLocalPropertyNames(FixedArray* storage, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797
1798 // Returns the number of properties on this object filtering out properties
1799 // with the specified attributes (ignoring interceptors).
1800 int NumberOfLocalElements(PropertyAttributes filter);
1801 // Returns the number of enumerable elements (ignoring interceptors).
1802 int NumberOfEnumElements();
1803 // Returns the number of elements on this object filtering out elements
1804 // with the specified attributes (ignoring interceptors).
1805 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1806 // Count and fill in the enumerable elements into storage.
1807 // (storage->length() == NumberOfEnumElements()).
1808 // If storage is NULL, will count the elements without adding
1809 // them to any storage.
1810 // Returns the number of enumerable elements.
1811 int GetEnumElementKeys(FixedArray* storage);
1812
1813 // Add a property to a fast-case object using a map transition to
1814 // new_map.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001815 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1816 String* name,
1817 Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818
1819 // Add a constant function property to a fast-case object.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001820 // This leaves a CONSTANT_TRANSITION in the old map, and
1821 // if it is called on a second object with this map, a
1822 // normal property is added instead, with a map transition.
1823 // This avoids the creation of many maps with the same constant
1824 // function, all orphaned.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001825 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1826 String* name,
1827 JSFunction* function,
1828 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829
lrn@chromium.org303ada72010-10-27 09:33:13 +00001830 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1831 String* name,
1832 Object* value,
1833 PropertyAttributes attributes);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001834
1835 // Converts a descriptor of any other type to a real field,
1836 // backed by the properties array. Descriptors of visible
1837 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1838 // Converts the descriptor on the original object's map to a
1839 // map transition, and the the new field is on the object's new map.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001840 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
ager@chromium.org7c537e22008-10-16 08:43:32 +00001841 String* name,
1842 Object* new_value,
1843 PropertyAttributes attributes);
1844
1845 // Converts a descriptor of any other type to a real field,
1846 // backed by the properties array. Descriptors of visible
1847 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001848 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1849 String* name,
1850 Object* new_value,
1851 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852
1853 // Add a property to a fast-case object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001854 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1855 Object* value,
1856 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857
1858 // Add a property to a slow-case object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001859 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1860 Object* value,
1861 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862
1863 // Add a property to an object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001864 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1865 Object* value,
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001866 PropertyAttributes attributes,
1867 StrictModeFlag strict_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868
1869 // Convert the object to use the canonical dictionary
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001870 // representation. If the object is expected to have additional properties
1871 // added this number can be indicated to have the backing store allocated to
1872 // an initial capacity for holding these properties.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001873 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1874 PropertyNormalizationMode mode,
1875 int expected_additional_properties);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001876
1877 // Convert and update the elements backing store to be a NumberDictionary
1878 // dictionary. Returns the backing after conversion.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001879 MUST_USE_RESULT MaybeObject* NormalizeElements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880
lrn@chromium.org303ada72010-10-27 09:33:13 +00001881 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001882
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883 // Transform slow named properties to fast variants.
1884 // Returns failure if allocation failed.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001885 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1886 int unused_property_fields);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887
ager@chromium.org7c537e22008-10-16 08:43:32 +00001888 // Access fast-case object properties at index.
1889 inline Object* FastPropertyAt(int index);
1890 inline Object* FastPropertyAtPut(int index, Object* value);
1891
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001892 // Access to in object properties.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001893 inline int GetInObjectPropertyOffset(int index);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001894 inline Object* InObjectPropertyAt(int index);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001895 inline Object* InObjectPropertyAtPut(int index,
1896 Object* value,
1897 WriteBarrierMode mode
1898 = UPDATE_WRITE_BARRIER);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001899
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 // initializes the body after properties slot, properties slot is
1901 // initialized by set_properties
1902 // Note: this call does not update write barrier, it is caller's
1903 // reponsibility to ensure that *v* can be collected without WB here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001904 inline void InitializeBody(int object_size, Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905
1906 // Check whether this object references another object
1907 bool ReferencesObject(Object* obj);
1908
1909 // Casting.
1910 static inline JSObject* cast(Object* obj);
1911
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001912 // Disalow further properties to be added to the object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001913 MUST_USE_RESULT MaybeObject* PreventExtensions();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001914
1915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 // Dispatched behavior.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 void JSObjectShortPrint(StringStream* accumulator);
whesse@chromium.org023421e2010-12-21 12:19:12 +00001918#ifdef OBJECT_PRINT
1919 inline void JSObjectPrint() {
1920 JSObjectPrint(stdout);
1921 }
1922 void JSObjectPrint(FILE* out);
1923#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 void JSObjectVerify();
whesse@chromium.org023421e2010-12-21 12:19:12 +00001926#endif
1927#ifdef OBJECT_PRINT
1928 inline void PrintProperties() {
1929 PrintProperties(stdout);
1930 }
1931 void PrintProperties(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932
whesse@chromium.org023421e2010-12-21 12:19:12 +00001933 inline void PrintElements() {
1934 PrintElements(stdout);
1935 }
1936 void PrintElements(FILE* out);
1937#endif
1938
1939#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 // Structure for collecting spill information about JSObjects.
1941 class SpillInformation {
1942 public:
1943 void Clear();
1944 void Print();
1945 int number_of_objects_;
1946 int number_of_objects_with_fast_properties_;
1947 int number_of_objects_with_fast_elements_;
1948 int number_of_fast_used_fields_;
1949 int number_of_fast_unused_fields_;
1950 int number_of_slow_used_properties_;
1951 int number_of_slow_unused_properties_;
1952 int number_of_fast_used_elements_;
1953 int number_of_fast_unused_elements_;
1954 int number_of_slow_used_elements_;
1955 int number_of_slow_unused_elements_;
1956 };
1957
1958 void IncrementSpillStatistics(SpillInformation* info);
1959#endif
1960 Object* SlowReverseLookup(Object* value);
1961
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001962 // Maximal number of fast properties for the JSObject. Used to
1963 // restrict the number of map transitions to avoid an explosion in
1964 // the number of maps for objects used as dictionaries.
1965 inline int MaxFastProperties();
1966
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001967 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1968 // Also maximal value of JSArray's length property.
1969 static const uint32_t kMaxElementCount = 0xffffffffu;
1970
ricow@chromium.org2c99e282011-07-28 09:15:17 +00001971 // Constants for heuristics controlling conversion of fast elements
1972 // to slow elements.
1973
1974 // Maximal gap that can be introduced by adding an element beyond
1975 // the current elements length.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 static const uint32_t kMaxGap = 1024;
ricow@chromium.org2c99e282011-07-28 09:15:17 +00001977
1978 // Maximal length of fast elements array that won't be checked for
1979 // being dense enough on expansion.
1980 static const int kMaxUncheckedFastElementsLength = 5000;
1981
1982 // Same as above but for old arrays. This limit is more strict. We
1983 // don't want to be wasteful with long lived objects.
1984 static const int kMaxUncheckedOldFastElementsLength = 500;
1985
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001986 static const int kInitialMaxFastElementArray = 100000;
whesse@chromium.org023421e2010-12-21 12:19:12 +00001987 static const int kMaxFastProperties = 12;
ager@chromium.org7c537e22008-10-16 08:43:32 +00001988 static const int kMaxInstanceSize = 255 * kPointerSize;
1989 // When extending the backing storage for property values, we increase
1990 // its size by more than the 1 entry necessary, so sequentially adding fields
1991 // to the same object requires fewer allocations and copies.
1992 static const int kFieldsAdded = 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993
1994 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00001995 static const int kPropertiesOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1997 static const int kHeaderSize = kElementsOffset + kPointerSize;
1998
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001999 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
2000
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002001 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
2002 public:
2003 static inline int SizeOf(Map* map, HeapObject* object);
2004 };
2005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 private:
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002007 friend class DictionaryElementsAccessor;
2008
lrn@chromium.org303ada72010-10-27 09:33:13 +00002009 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
2010 Object* structure,
2011 uint32_t index,
2012 Object* holder);
2013 MaybeObject* SetElementWithCallback(Object* structure,
2014 uint32_t index,
2015 Object* value,
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00002016 JSObject* holder,
2017 StrictModeFlag strict_mode);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00002018 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
2019 uint32_t index,
2020 Object* value,
2021 StrictModeFlag strict_mode,
2022 bool check_prototype);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00002023 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
2024 uint32_t index,
2025 Object* value,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00002026 StrictModeFlag strict_mode,
lrn@chromium.org5d00b602011-01-05 09:51:43 +00002027 bool check_prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028
lrn@chromium.org303ada72010-10-27 09:33:13 +00002029 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
2030 DeleteMode mode);
2031 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032
lrn@chromium.org303ada72010-10-27 09:33:13 +00002033 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034
whesse@chromium.org7b260152011-06-20 15:33:18 +00002035 MUST_USE_RESULT MaybeObject* DeleteFastElement(uint32_t index);
2036 MUST_USE_RESULT MaybeObject* DeleteDictionaryElement(uint32_t index,
2037 DeleteMode mode);
2038
2039 bool ReferencesObjectFromElements(FixedArray* elements,
2040 ElementsKind kind,
2041 Object* object);
2042 bool HasElementInElements(FixedArray* elements,
2043 ElementsKind kind,
2044 uint32_t index);
2045
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 // Returns true if most of the elements backing storage is used.
2047 bool HasDenseElements();
2048
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002049 // Gets the current elements capacity and the number of used elements.
2050 void GetElementsCapacityAndUsage(int* capacity, int* used);
2051
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00002052 bool CanSetCallback(String* name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002053 MUST_USE_RESULT MaybeObject* SetElementCallback(
2054 uint32_t index,
2055 Object* structure,
2056 PropertyAttributes attributes);
2057 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
2058 String* name,
2059 Object* structure,
2060 PropertyAttributes attributes);
2061 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
2062 String* name,
2063 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064
2065 void LookupInDescriptor(String* name, LookupResult* result);
2066
2067 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
2068};
2069
2070
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002071// Common superclass for FixedArrays that allow implementations to share
2072// common accessors and some code paths.
2073class FixedArrayBase: public HeapObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 public:
2075 // [length]: length of the array.
2076 inline int length();
2077 inline void set_length(int value);
2078
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002079 inline static FixedArrayBase* cast(Object* object);
2080
2081 // Layout description.
2082 // Length is smi tagged when it is stored.
2083 static const int kLengthOffset = HeapObject::kHeaderSize;
2084 static const int kHeaderSize = kLengthOffset + kPointerSize;
2085};
2086
ricow@chromium.org9fa09672011-07-25 11:05:35 +00002087class FixedDoubleArray;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002088
2089// FixedArray describes fixed-sized arrays with element type Object*.
2090class FixedArray: public FixedArrayBase {
2091 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 // Setter and getter for elements.
2093 inline Object* get(int index);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002094 // Setter that uses write barrier.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 inline void set(int index, Object* value);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002096 inline bool is_the_hole(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002098 // Setter that doesn't need write barrier).
2099 inline void set(int index, Smi* value);
2100 // Setter with explicit barrier mode.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002101 inline void set(int index, Object* value, WriteBarrierMode mode);
2102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 // Setters for frequently used oddballs located in old space.
2104 inline void set_undefined(int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002105 // TODO(isolates): duplicate.
2106 inline void set_undefined(Heap* heap, int index);
ager@chromium.org236ad962008-09-25 09:45:57 +00002107 inline void set_null(int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002108 // TODO(isolates): duplicate.
2109 inline void set_null(Heap* heap, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110 inline void set_the_hole(int index);
2111
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002112 // Setters with less debug checks for the GC to use.
2113 inline void set_unchecked(int index, Smi* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002114 inline void set_null_unchecked(Heap* heap, int index);
2115 inline void set_unchecked(Heap* heap, int index, Object* value,
2116 WriteBarrierMode mode);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002117
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002118 // Gives access to raw memory which stores the array's data.
2119 inline Object** data_start();
2120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 // Copy operations.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002122 MUST_USE_RESULT inline MaybeObject* Copy();
2123 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124
2125 // Add the elements of a JSArray to this FixedArray.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002126 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127
2128 // Compute the union of this and other.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002129 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
2131 // Copy a sub array from the receiver to dest.
2132 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
2133
2134 // Garbage collection support.
2135 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
2136
ager@chromium.org3e875802009-06-29 08:26:34 +00002137 // Code Generation support.
2138 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140 // Casting.
2141 static inline FixedArray* cast(Object* obj);
2142
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002143 // Maximal allowed size, in bytes, of a single FixedArray.
2144 // Prevents overflowing size computations, as well as extreme memory
2145 // consumption.
2146 static const int kMaxSize = 512 * MB;
2147 // Maximally allowed length of a FixedArray.
2148 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002149
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002151#ifdef OBJECT_PRINT
2152 inline void FixedArrayPrint() {
2153 FixedArrayPrint(stdout);
2154 }
2155 void FixedArrayPrint(FILE* out);
2156#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 void FixedArrayVerify();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002159 // Checks if two FixedArrays have identical contents.
2160 bool IsEqualTo(FixedArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161#endif
2162
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002163 // Swap two elements in a pair of arrays. If this array and the
2164 // numbers array are the same object, the elements are only swapped
2165 // once.
2166 void SwapPairs(FixedArray* numbers, int i, int j);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002168 // Sort prefix of this array and the numbers array as pairs wrt. the
2169 // numbers. If the numbers array and the this array are the same
2170 // object, the prefix of this array is sorted.
2171 void SortPairs(FixedArray* numbers, uint32_t len);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002173 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2174 public:
2175 static inline int SizeOf(Map* map, HeapObject* object) {
2176 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2177 }
2178 };
2179
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 protected:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002181 // Set operation on FixedArray without using write barriers. Can
2182 // only be used for storing old space objects or smis.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 static inline void fast_set(FixedArray* array, int index, Object* value);
2184
2185 private:
2186 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2187};
2188
2189
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002190// FixedDoubleArray describes fixed-sized arrays with element type double.
2191class FixedDoubleArray: public FixedArrayBase {
2192 public:
2193 inline void Initialize(FixedArray* from);
2194 inline void Initialize(FixedDoubleArray* from);
2195 inline void Initialize(NumberDictionary* from);
2196
2197 // Setter and getter for elements.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002198 inline double get_scalar(int index);
2199 inline MaybeObject* get(int index);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002200 inline void set(int index, double value);
2201 inline void set_the_hole(int index);
2202
2203 // Checking for the hole.
2204 inline bool is_the_hole(int index);
2205
2206 // Garbage collection support.
2207 inline static int SizeFor(int length) {
2208 return kHeaderSize + length * kDoubleSize;
2209 }
2210
rossberg@chromium.org717967f2011-07-20 13:44:42 +00002211 // Code Generation support.
2212 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2213
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00002214 inline static bool is_the_hole_nan(double value);
2215 inline static double hole_nan_as_double();
2216 inline static double canonical_not_the_hole_nan_as_double();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002217
2218 // Casting.
2219 static inline FixedDoubleArray* cast(Object* obj);
2220
2221 // Maximal allowed size, in bytes, of a single FixedDoubleArray.
2222 // Prevents overflowing size computations, as well as extreme memory
2223 // consumption.
2224 static const int kMaxSize = 512 * MB;
2225 // Maximally allowed length of a FixedArray.
2226 static const int kMaxLength = (kMaxSize - kHeaderSize) / kDoubleSize;
2227
2228 // Dispatched behavior.
2229#ifdef OBJECT_PRINT
2230 inline void FixedDoubleArrayPrint() {
2231 FixedDoubleArrayPrint(stdout);
2232 }
2233 void FixedDoubleArrayPrint(FILE* out);
2234#endif
2235
2236#ifdef DEBUG
2237 void FixedDoubleArrayVerify();
2238#endif
2239
2240 private:
2241 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedDoubleArray);
2242};
2243
2244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245// DescriptorArrays are fixed arrays used to hold instance descriptors.
2246// The format of the these objects is:
danno@chromium.org40cb8782011-05-25 07:58:50 +00002247// TODO(1399): It should be possible to make room for bit_field3 in the map
2248// without overloading the instance descriptors field in the map
2249// (and storing it in the DescriptorArray when the map has one).
2250// [0]: storage for bit_field3 for Map owning this object (Smi)
2251// [1]: point to a fixed array with (value, detail) pairs.
2252// [2]: next enumeration index (Smi), or pointer to small fixed array:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253// [0]: next enumeration index (Smi)
2254// [1]: pointer to fixed array with enum cache
danno@chromium.org40cb8782011-05-25 07:58:50 +00002255// [3]: first key
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256// [length() - 1]: last key
2257//
2258class DescriptorArray: public FixedArray {
2259 public:
danno@chromium.org40cb8782011-05-25 07:58:50 +00002260 // Returns true for both shared empty_descriptor_array and for smis, which the
2261 // map uses to encode additional bit fields when the descriptor array is not
2262 // yet used.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002263 inline bool IsEmpty();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00002264
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 // Returns the number of descriptors in the array.
2266 int number_of_descriptors() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002267 ASSERT(length() > kFirstIndex || IsEmpty());
2268 int len = length();
2269 return len <= kFirstIndex ? 0 : len - kFirstIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 }
2271
2272 int NextEnumerationIndex() {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002273 if (IsEmpty()) return PropertyDetails::kInitialIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 Object* obj = get(kEnumerationIndexIndex);
2275 if (obj->IsSmi()) {
2276 return Smi::cast(obj)->value();
2277 } else {
2278 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2279 return Smi::cast(index)->value();
2280 }
2281 }
2282
2283 // Set next enumeration index and flush any enum cache.
2284 void SetNextEnumerationIndex(int value) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002285 if (!IsEmpty()) {
2286 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2287 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 bool HasEnumCache() {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002290 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 }
2292
2293 Object* GetEnumCache() {
2294 ASSERT(HasEnumCache());
2295 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2296 return bridge->get(kEnumCacheBridgeCacheIndex);
2297 }
2298
danno@chromium.org40cb8782011-05-25 07:58:50 +00002299 // TODO(1399): It should be possible to make room for bit_field3 in the map
2300 // without overloading the instance descriptors field in the map
2301 // (and storing it in the DescriptorArray when the map has one).
2302 inline int bit_field3_storage();
2303 inline void set_bit_field3_storage(int value);
2304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 // Initialize or change the enum cache,
2306 // using the supplied storage for the small "bridge".
2307 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2308
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002309 // Accessors for fetching instance descriptor at descriptor number.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 inline String* GetKey(int descriptor_number);
2311 inline Object* GetValue(int descriptor_number);
2312 inline Smi* GetDetails(int descriptor_number);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002313 inline PropertyType GetType(int descriptor_number);
2314 inline int GetFieldIndex(int descriptor_number);
2315 inline JSFunction* GetConstantFunction(int descriptor_number);
2316 inline Object* GetCallbacksObject(int descriptor_number);
2317 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2318 inline bool IsProperty(int descriptor_number);
2319 inline bool IsTransition(int descriptor_number);
2320 inline bool IsNullDescriptor(int descriptor_number);
2321 inline bool IsDontEnum(int descriptor_number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322
2323 // Accessor for complete descriptor.
2324 inline void Get(int descriptor_number, Descriptor* desc);
2325 inline void Set(int descriptor_number, Descriptor* desc);
2326
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002327 // Transfer complete descriptor from another descriptor array to
2328 // this one.
2329 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2330
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002331 // Copy the descriptor array, insert a new descriptor and optionally
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002332 // remove map transitions. If the descriptor is already present, it is
2333 // replaced. If a replaced descriptor is a real property (not a transition
2334 // or null), its enumeration index is kept as is.
2335 // If adding a real property, map transitions must be removed. If adding
2336 // a transition, they must not be removed. All null descriptors are removed.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002337 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2338 TransitionFlag transition_flag);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002339
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002340 // Remove all transitions. Return a copy of the array with all transitions
2341 // removed, or a Failure object if the new array could not be allocated.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002342 MUST_USE_RESULT MaybeObject* RemoveTransitions();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002343
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 // Sort the instance descriptors by the hash codes of their keys.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002345 // Does not check for duplicates.
2346 void SortUnchecked();
2347
2348 // Sort the instance descriptors by the hash codes of their keys.
2349 // Checks the result for duplicates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 void Sort();
2351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 // Search the instance descriptors for given name.
2353 inline int Search(String* name);
2354
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002355 // As the above, but uses DescriptorLookupCache and updates it when
2356 // necessary.
2357 inline int SearchWithCache(String* name);
2358
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 // Tells whether the name is present int the array.
2360 bool Contains(String* name) { return kNotFound != Search(name); }
2361
2362 // Perform a binary search in the instance descriptors represented
2363 // by this fixed array. low and high are descriptor indices. If there
2364 // are three instance descriptors in this array it should be called
2365 // with low=0 and high=2.
2366 int BinarySearch(String* name, int low, int high);
2367
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002368 // Perform a linear search in the instance descriptors represented
ager@chromium.org32912102009-01-16 10:38:43 +00002369 // by this fixed array. len is the number of descriptor indices that are
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002370 // valid. Does not require the descriptors to be sorted.
2371 int LinearSearch(String* name, int len);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002372
2373 // Allocates a DescriptorArray, but returns the singleton
2374 // empty descriptor array object if number_of_descriptors is 0.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002375 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376
2377 // Casting.
2378 static inline DescriptorArray* cast(Object* obj);
2379
2380 // Constant for denoting key was not found.
2381 static const int kNotFound = -1;
2382
danno@chromium.org40cb8782011-05-25 07:58:50 +00002383 static const int kBitField3StorageIndex = 0;
2384 static const int kContentArrayIndex = 1;
2385 static const int kEnumerationIndexIndex = 2;
2386 static const int kFirstIndex = 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387
2388 // The length of the "bridge" to the enum cache.
2389 static const int kEnumCacheBridgeLength = 2;
2390 static const int kEnumCacheBridgeEnumIndex = 0;
2391 static const int kEnumCacheBridgeCacheIndex = 1;
2392
2393 // Layout description.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002394 static const int kBitField3StorageOffset = FixedArray::kHeaderSize;
2395 static const int kContentArrayOffset = kBitField3StorageOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002396 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2397 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2398
2399 // Layout description for the bridge array.
2400 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2401 static const int kEnumCacheBridgeCacheOffset =
2402 kEnumCacheBridgeEnumOffset + kPointerSize;
2403
whesse@chromium.org023421e2010-12-21 12:19:12 +00002404#ifdef OBJECT_PRINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 // Print all the descriptors.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002406 inline void PrintDescriptors() {
2407 PrintDescriptors(stdout);
2408 }
2409 void PrintDescriptors(FILE* out);
2410#endif
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002411
whesse@chromium.org023421e2010-12-21 12:19:12 +00002412#ifdef DEBUG
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002413 // Is the descriptor array sorted and without duplicates?
2414 bool IsSortedNoDuplicates();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002415
2416 // Are two DescriptorArrays equal?
2417 bool IsEqualTo(DescriptorArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418#endif
2419
2420 // The maximum number of descriptors we want in a descriptor array (should
2421 // fit in a page).
2422 static const int kMaxNumberOfDescriptors = 1024 + 512;
2423
2424 private:
2425 // Conversion from descriptor number to array indices.
2426 static int ToKeyIndex(int descriptor_number) {
2427 return descriptor_number+kFirstIndex;
2428 }
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00002429
2430 static int ToDetailsIndex(int descriptor_number) {
2431 return (descriptor_number << 1) + 1;
2432 }
2433
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434 static int ToValueIndex(int descriptor_number) {
2435 return descriptor_number << 1;
2436 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002438 bool is_null_descriptor(int descriptor_number) {
2439 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2440 NULL_DESCRIPTOR;
2441 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442 // Swap operation on FixedArray without using write barriers.
2443 static inline void fast_swap(FixedArray* array, int first, int second);
2444
2445 // Swap descriptor first and second.
2446 inline void Swap(int first, int second);
2447
2448 FixedArray* GetContentArray() {
2449 return FixedArray::cast(get(kContentArrayIndex));
2450 }
2451 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2452};
2453
2454
2455// HashTable is a subclass of FixedArray that implements a hash table
2456// that uses open addressing and quadratic probing.
2457//
2458// In order for the quadratic probing to work, elements that have not
2459// yet been used and elements that have been deleted are
2460// distinguished. Probing continues when deleted elements are
2461// encountered and stops when unused elements are encountered.
2462//
2463// - Elements with key == undefined have not been used yet.
2464// - Elements with key == null have been deleted.
2465//
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002466// The hash table class is parameterized with a Shape and a Key.
2467// Shape must be a class with the following interface:
2468// class ExampleShape {
2469// public:
2470// // Tells whether key matches other.
2471// static bool IsMatch(Key key, Object* other);
2472// // Returns the hash value for key.
2473// static uint32_t Hash(Key key);
2474// // Returns the hash value for object.
2475// static uint32_t HashForObject(Key key, Object* object);
2476// // Convert key to an object.
2477// static inline Object* AsObject(Key key);
2478// // The prefix size indicates number of elements in the beginning
2479// // of the backing storage.
2480// static const int kPrefixSize = ..;
2481// // The Element size indicates number of elements per entry.
2482// static const int kEntrySize = ..;
2483// };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002484// The prefix size indicates an amount of memory in the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002485// beginning of the backing storage that can be used for non-element
2486// information by subclasses.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002487
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002488template<typename Shape, typename Key>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489class HashTable: public FixedArray {
2490 public:
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002491 // Returns the number of elements in the hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492 int NumberOfElements() {
2493 return Smi::cast(get(kNumberOfElementsIndex))->value();
2494 }
2495
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002496 // Returns the number of deleted elements in the hash table.
2497 int NumberOfDeletedElements() {
2498 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2499 }
2500
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002501 // Returns the capacity of the hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002502 int Capacity() {
2503 return Smi::cast(get(kCapacityIndex))->value();
2504 }
2505
2506 // ElementAdded should be called whenever an element is added to a
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002507 // hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002508 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2509
2510 // ElementRemoved should be called whenever an element is removed from
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002511 // a hash table.
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002512 void ElementRemoved() {
2513 SetNumberOfElements(NumberOfElements() - 1);
2514 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2515 }
2516 void ElementsRemoved(int n) {
2517 SetNumberOfElements(NumberOfElements() - n);
2518 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2519 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002520
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002521 // Returns a new HashTable object. Might return Failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002522 MUST_USE_RESULT static MaybeObject* Allocate(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002523 int at_least_space_for,
2524 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525
ricow@chromium.org2c99e282011-07-28 09:15:17 +00002526 // Computes the required capacity for a table holding the given
2527 // number of elements. May be more than HashTable::kMaxCapacity.
2528 static int ComputeCapacity(int at_least_space_for);
2529
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002530 // Returns the key at entry.
2531 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2532
ager@chromium.org32912102009-01-16 10:38:43 +00002533 // Tells whether k is a real key. Null and undefined are not allowed
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 // as keys and can be used to indicate missing or deleted elements.
2535 bool IsKey(Object* k) {
2536 return !k->IsNull() && !k->IsUndefined();
2537 }
2538
2539 // Garbage collection support.
2540 void IteratePrefix(ObjectVisitor* visitor);
2541 void IterateElements(ObjectVisitor* visitor);
2542
2543 // Casting.
2544 static inline HashTable* cast(Object* obj);
2545
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 // Compute the probe offset (quadratic probing).
2547 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2548 return (n + n * n) >> 1;
2549 }
2550
2551 static const int kNumberOfElementsIndex = 0;
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002552 static const int kNumberOfDeletedElementsIndex = 1;
2553 static const int kCapacityIndex = 2;
2554 static const int kPrefixStartIndex = 3;
2555 static const int kElementsStartIndex =
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002556 kPrefixStartIndex + Shape::kPrefixSize;
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002557 static const int kEntrySize = Shape::kEntrySize;
2558 static const int kElementsStartOffset =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559 kHeaderSize + kElementsStartIndex * kPointerSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002560 static const int kCapacityOffset =
2561 kHeaderSize + kCapacityIndex * kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002563 // Constant used for denoting a absent entry.
2564 static const int kNotFound = -1;
2565
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002566 // Maximal capacity of HashTable. Based on maximal length of underlying
2567 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2568 // cannot overflow.
2569 static const int kMaxCapacity =
2570 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2571
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002572 // Find entry for key otherwise return kNotFound.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002573 inline int FindEntry(Key key);
2574 int FindEntry(Isolate* isolate, Key key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002575
2576 protected:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 // Find the entry at which to insert element with the given key that
2578 // has the given hash value.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002579 uint32_t FindInsertionEntry(uint32_t hash);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580
2581 // Returns the index for an entry (of the key)
2582 static inline int EntryToIndex(int entry) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002583 return (entry * kEntrySize) + kElementsStartIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002584 }
2585
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002586 // Update the number of elements in the hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 void SetNumberOfElements(int nof) {
2588 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2589 }
2590
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002591 // Update the number of deleted elements in the hash table.
2592 void SetNumberOfDeletedElements(int nod) {
2593 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2594 }
2595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 // Sets the capacity of the hash table.
2597 void SetCapacity(int capacity) {
2598 // To scale a computed hash code to fit within the hash table, we
2599 // use bit-wise AND with a mask, so the capacity must be positive
2600 // and non-zero.
2601 ASSERT(capacity > 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002602 ASSERT(capacity <= kMaxCapacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2604 }
2605
2606
2607 // Returns probe entry.
2608 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2609 ASSERT(IsPowerOf2(size));
2610 return (hash + GetProbeOffset(number)) & (size - 1);
2611 }
2612
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002613 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2614 return hash & (size - 1);
2615 }
2616
2617 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2618 return (last + number) & (size - 1);
2619 }
2620
ager@chromium.org04921a82011-06-27 13:21:41 +00002621 // Rehashes this hash-table into the new table.
2622 MUST_USE_RESULT MaybeObject* Rehash(HashTable* new_table, Key key);
2623
2624 // Attempt to shrink hash table after removal of key.
2625 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 // Ensure enough space for n additional elements.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002628 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629};
2630
2631
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002632
2633// HashTableKey is an abstract superclass for virtual key behavior.
2634class HashTableKey {
2635 public:
2636 // Returns whether the other object matches this key.
2637 virtual bool IsMatch(Object* other) = 0;
2638 // Returns the hash value for this key.
2639 virtual uint32_t Hash() = 0;
2640 // Returns the hash value for object.
2641 virtual uint32_t HashForObject(Object* key) = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002642 // Returns the key object for storing into the hash table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002643 // If allocations fails a failure object is returned.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002644 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002645 // Required.
2646 virtual ~HashTableKey() {}
2647};
2648
2649class SymbolTableShape {
2650 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002651 static inline bool IsMatch(HashTableKey* key, Object* value) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002652 return key->IsMatch(value);
2653 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002654 static inline uint32_t Hash(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002655 return key->Hash();
2656 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002657 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002658 return key->HashForObject(object);
2659 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002660 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002661 return key->AsObject();
2662 }
2663
2664 static const int kPrefixSize = 0;
2665 static const int kEntrySize = 1;
2666};
2667
danno@chromium.org40cb8782011-05-25 07:58:50 +00002668class SeqAsciiString;
2669
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670// SymbolTable.
2671//
2672// No special elements in the prefix and the element size is 1
2673// because only the symbol itself (the key) needs to be stored.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002674class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675 public:
2676 // Find symbol in the symbol table. If it is not there yet, it is
2677 // added. The return value is the symbol table which might have
2678 // been enlarged. If the return value is not a failure, the symbol
2679 // pointer *s is set to the symbol found.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002680 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00002681 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2682 Object** s);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002683 MUST_USE_RESULT MaybeObject* LookupSubStringAsciiSymbol(
2684 Handle<SeqAsciiString> str,
2685 int from,
2686 int length,
2687 Object** s);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00002688 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2689 Object** s);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002690 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691
ager@chromium.org7c537e22008-10-16 08:43:32 +00002692 // Looks up a symbol that is equal to the given string and returns
2693 // true if it is found, assigning the symbol to the given output
2694 // parameter.
2695 bool LookupSymbolIfExists(String* str, String** symbol);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00002696 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002697
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 // Casting.
2699 static inline SymbolTable* cast(Object* obj);
2700
2701 private:
lrn@chromium.org303ada72010-10-27 09:33:13 +00002702 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703
2704 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2705};
2706
2707
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002708class MapCacheShape {
2709 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002710 static inline bool IsMatch(HashTableKey* key, Object* value) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002711 return key->IsMatch(value);
2712 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002713 static inline uint32_t Hash(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002714 return key->Hash();
2715 }
2716
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002717 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002718 return key->HashForObject(object);
2719 }
2720
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002721 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002722 return key->AsObject();
2723 }
2724
2725 static const int kPrefixSize = 0;
2726 static const int kEntrySize = 2;
2727};
2728
2729
ager@chromium.org236ad962008-09-25 09:45:57 +00002730// MapCache.
2731//
2732// Maps keys that are a fixed array of symbols to a map.
2733// Used for canonicalize maps for object literals.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002734class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
ager@chromium.org236ad962008-09-25 09:45:57 +00002735 public:
2736 // Find cached value for a string key, otherwise return null.
2737 Object* Lookup(FixedArray* key);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002738 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
ager@chromium.org236ad962008-09-25 09:45:57 +00002739 static inline MapCache* cast(Object* obj);
2740
2741 private:
2742 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2743};
2744
2745
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002746template <typename Shape, typename Key>
2747class Dictionary: public HashTable<Shape, Key> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002748 public:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002749 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2750 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2751 }
2752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753 // Returns the value at entry.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002754 Object* ValueAt(int entry) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002755 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002756 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757
2758 // Set the value for entry.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00002759 // Returns false if the put wasn't performed due to property being read only.
2760 // Returns true on successful put.
2761 bool ValueAtPut(int entry, Object* value) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002762 // Check that this value can actually be written.
2763 PropertyDetails details = DetailsAt(entry);
2764 // If a value has not been initilized we allow writing to it even if
2765 // it is read only (a declared const that has not been initialized).
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00002766 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2767 return false;
2768 }
2769 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2770 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 }
2772
2773 // Returns the property details for the property at entry.
2774 PropertyDetails DetailsAt(int entry) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002775 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002776 return PropertyDetails(
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002777 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778 }
2779
2780 // Set the details for entry.
2781 void DetailsAtPut(int entry, PropertyDetails value) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002782 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783 }
2784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785 // Sorting support
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 void CopyValuesTo(FixedArray* elements);
2787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788 // Delete a property from the dictionary.
ager@chromium.orge2902be2009-06-08 12:21:35 +00002789 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790
ager@chromium.org04921a82011-06-27 13:21:41 +00002791 // Attempt to shrink the dictionary after deletion of key.
2792 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2793
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002794 // Returns the number of elements in the dictionary filtering out properties
2795 // with the specified attributes.
2796 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2797
2798 // Returns the number of enumerable elements in the dictionary.
2799 int NumberOfEnumElements();
2800
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002801 enum SortMode { UNSORTED, SORTED };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002802 // Copies keys to preallocated fixed array.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00002803 void CopyKeysTo(FixedArray* storage,
2804 PropertyAttributes filter,
2805 SortMode sort_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 // Fill in details for properties into storage.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002807 void CopyKeysTo(FixedArray* storage, int index, SortMode sort_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002808
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002809 // Accessors for next enumeration index.
2810 void SetNextEnumerationIndex(int index) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002811 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002812 }
2813
2814 int NextEnumerationIndex() {
2815 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2816 }
2817
2818 // Returns a new array for dictionary usage. Might return Failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002819 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002820
2821 // Ensure enough space for n additional elements.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002822 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002823
whesse@chromium.org023421e2010-12-21 12:19:12 +00002824#ifdef OBJECT_PRINT
2825 inline void Print() {
2826 Print(stdout);
2827 }
2828 void Print(FILE* out);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002829#endif
2830 // Returns the key (slow).
2831 Object* SlowReverseLookup(Object* value);
2832
2833 // Sets the entry to (key, value) pair.
2834 inline void SetEntry(int entry,
2835 Object* key,
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002836 Object* value);
2837 inline void SetEntry(int entry,
2838 Object* key,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002839 Object* value,
2840 PropertyDetails details);
2841
lrn@chromium.org303ada72010-10-27 09:33:13 +00002842 MUST_USE_RESULT MaybeObject* Add(Key key,
2843 Object* value,
2844 PropertyDetails details);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002845
2846 protected:
2847 // Generic at put operation.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002848 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002849
2850 // Add entry to dictionary.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002851 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2852 Object* value,
2853 PropertyDetails details,
2854 uint32_t hash);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002855
2856 // Generate new enumeration indices to avoid enumeration index overflow.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002857 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002858 static const int kMaxNumberKeyIndex =
2859 HashTable<Shape, Key>::kPrefixStartIndex;
2860 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2861};
2862
2863
2864class StringDictionaryShape {
2865 public:
2866 static inline bool IsMatch(String* key, Object* other);
2867 static inline uint32_t Hash(String* key);
2868 static inline uint32_t HashForObject(String* key, Object* object);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002869 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002870 static const int kPrefixSize = 2;
2871 static const int kEntrySize = 3;
2872 static const bool kIsEnumerable = true;
2873};
2874
2875
2876class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2877 public:
2878 static inline StringDictionary* cast(Object* obj) {
2879 ASSERT(obj->IsDictionary());
2880 return reinterpret_cast<StringDictionary*>(obj);
2881 }
2882
2883 // Copies enumerable keys to preallocated fixed array.
2884 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886 // For transforming properties of a JSObject.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002887 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2888 JSObject* obj,
2889 int unused_property_fields);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002890
2891 // Find entry for key otherwise return kNotFound. Optimzed version of
2892 // HashTable::FindEntry.
2893 int FindEntry(String* key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002894};
2895
2896
2897class NumberDictionaryShape {
2898 public:
2899 static inline bool IsMatch(uint32_t key, Object* other);
2900 static inline uint32_t Hash(uint32_t key);
2901 static inline uint32_t HashForObject(uint32_t key, Object* object);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002902 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002903 static const int kPrefixSize = 2;
2904 static const int kEntrySize = 3;
2905 static const bool kIsEnumerable = false;
2906};
2907
2908
2909class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2910 public:
2911 static NumberDictionary* cast(Object* obj) {
2912 ASSERT(obj->IsDictionary());
2913 return reinterpret_cast<NumberDictionary*>(obj);
2914 }
2915
2916 // Type specific at put (default NONE attributes is used when adding).
lrn@chromium.org303ada72010-10-27 09:33:13 +00002917 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2918 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2919 Object* value,
2920 PropertyDetails details);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002921
2922 // Set an existing entry or add a new one if needed.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002923 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2924 Object* value,
2925 PropertyDetails details);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002926
2927 void UpdateMaxNumberKey(uint32_t key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928
2929 // If slow elements are required we will never go back to fast-case
2930 // for the elements kept in this dictionary. We require slow
2931 // elements if an element has been added at an index larger than
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002932 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2933 // when defining a getter or setter with a number key.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002934 inline bool requires_slow_elements();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002935 inline void set_requires_slow_elements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936
2937 // Get the value of the max number key that has been added to this
2938 // dictionary. max_number_key can only be called if
2939 // requires_slow_elements returns false.
2940 inline uint32_t max_number_key();
2941
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002942 // Remove all entries were key is a number and (from <= key && key < to).
2943 void RemoveNumberEntries(uint32_t from, uint32_t to);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944
2945 // Bit masks.
2946 static const int kRequiresSlowElementsMask = 1;
2947 static const int kRequiresSlowElementsTagSize = 1;
2948 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002949};
2950
2951
vegorov@chromium.org7943d462011-08-01 11:41:52 +00002952class ObjectHashTableShape {
2953 public:
2954 static inline bool IsMatch(JSObject* key, Object* other);
2955 static inline uint32_t Hash(JSObject* key);
2956 static inline uint32_t HashForObject(JSObject* key, Object* object);
2957 MUST_USE_RESULT static inline MaybeObject* AsObject(JSObject* key);
2958 static const int kPrefixSize = 0;
2959 static const int kEntrySize = 2;
2960};
2961
2962
2963// ObjectHashTable maps keys that are JavaScript objects to object values by
2964// using the identity hash of the key for hashing purposes.
2965class ObjectHashTable: public HashTable<ObjectHashTableShape, JSObject*> {
2966 public:
2967 static inline ObjectHashTable* cast(Object* obj) {
2968 ASSERT(obj->IsHashTable());
2969 return reinterpret_cast<ObjectHashTable*>(obj);
2970 }
2971
2972 // Looks up the value associated with the given key. The undefined value is
2973 // returned in case the key is not present.
2974 Object* Lookup(JSObject* key);
2975
2976 // Adds (or overwrites) the value associated with the given key. Mapping a
2977 // key to the undefined value causes removal of the whole entry.
2978 MUST_USE_RESULT MaybeObject* Put(JSObject* key, Object* value);
2979
2980 private:
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002981 friend class MarkCompactCollector;
2982
vegorov@chromium.org7943d462011-08-01 11:41:52 +00002983 void AddEntry(int entry, JSObject* key, Object* value);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00002984 void RemoveEntry(int entry, Heap* heap);
2985 inline void RemoveEntry(int entry);
2986
2987 // Returns the index to the value of an entry.
2988 static inline int EntryToValueIndex(int entry) {
2989 return EntryToIndex(entry) + 1;
2990 }
vegorov@chromium.org7943d462011-08-01 11:41:52 +00002991};
2992
2993
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002994// JSFunctionResultCache caches results of some JSFunction invocation.
2995// It is a fixed array with fixed structure:
2996// [0]: factory function
2997// [1]: finger index
2998// [2]: current cache size
2999// [3]: dummy field.
3000// The rest of array are key/value pairs.
3001class JSFunctionResultCache: public FixedArray {
3002 public:
3003 static const int kFactoryIndex = 0;
3004 static const int kFingerIndex = kFactoryIndex + 1;
3005 static const int kCacheSizeIndex = kFingerIndex + 1;
3006 static const int kDummyIndex = kCacheSizeIndex + 1;
3007 static const int kEntriesIndex = kDummyIndex + 1;
antonm@chromium.org397e23c2010-04-21 12:00:05 +00003008
3009 static const int kEntrySize = 2; // key + value
ager@chromium.orgac091b72010-05-05 07:34:42 +00003010
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00003011 static const int kFactoryOffset = kHeaderSize;
3012 static const int kFingerOffset = kFactoryOffset + kPointerSize;
3013 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
3014
ager@chromium.orgac091b72010-05-05 07:34:42 +00003015 inline void MakeZeroSize();
3016 inline void Clear();
3017
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003018 inline int size();
3019 inline void set_size(int size);
3020 inline int finger_index();
3021 inline void set_finger_index(int finger_index);
3022
ager@chromium.orgac091b72010-05-05 07:34:42 +00003023 // Casting
3024 static inline JSFunctionResultCache* cast(Object* obj);
3025
3026#ifdef DEBUG
3027 void JSFunctionResultCacheVerify();
3028#endif
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00003029};
3030
3031
ricow@chromium.org65fae842010-08-25 15:26:24 +00003032// The cache for maps used by normalized (dictionary mode) objects.
3033// Such maps do not have property descriptors, so a typical program
3034// needs very limited number of distinct normalized maps.
3035class NormalizedMapCache: public FixedArray {
3036 public:
3037 static const int kEntries = 64;
3038
lrn@chromium.org303ada72010-10-27 09:33:13 +00003039 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
3040 PropertyNormalizationMode mode);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003041
ricow@chromium.org65fae842010-08-25 15:26:24 +00003042 void Clear();
3043
3044 // Casting
3045 static inline NormalizedMapCache* cast(Object* obj);
3046
3047#ifdef DEBUG
3048 void NormalizedMapCacheVerify();
3049#endif
ricow@chromium.org65fae842010-08-25 15:26:24 +00003050};
3051
3052
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003053// ByteArray represents fixed sized byte arrays. Used by the outside world,
3054// such as PCRE, and also by the memory allocator and garbage collector to
3055// fill in free blocks in the heap.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003056class ByteArray: public HeapObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003057 public:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003058 // [length]: length of the array.
3059 inline int length();
3060 inline void set_length(int value);
3061
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003062 // Setter and getter.
3063 inline byte get(int index);
3064 inline void set(int index, byte value);
3065
3066 // Treat contents as an int array.
3067 inline int get_int(int index);
3068
3069 static int SizeFor(int length) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003070 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071 }
3072 // We use byte arrays for free blocks in the heap. Given a desired size in
3073 // bytes that is a multiple of the word size and big enough to hold a byte
3074 // array, this function returns the number of elements a byte array should
3075 // have.
3076 static int LengthFor(int size_in_bytes) {
3077 ASSERT(IsAligned(size_in_bytes, kPointerSize));
3078 ASSERT(size_in_bytes >= kHeaderSize);
3079 return size_in_bytes - kHeaderSize;
3080 }
3081
3082 // Returns data start address.
3083 inline Address GetDataStartAddress();
3084
3085 // Returns a pointer to the ByteArray object for a given data start address.
3086 static inline ByteArray* FromDataStartAddress(Address address);
3087
3088 // Casting.
3089 static inline ByteArray* cast(Object* obj);
3090
3091 // Dispatched behavior.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003092 inline int ByteArraySize() {
3093 return SizeFor(this->length());
3094 }
whesse@chromium.org023421e2010-12-21 12:19:12 +00003095#ifdef OBJECT_PRINT
3096 inline void ByteArrayPrint() {
3097 ByteArrayPrint(stdout);
3098 }
3099 void ByteArrayPrint(FILE* out);
3100#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003102 void ByteArrayVerify();
3103#endif
3104
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003105 // Layout description.
3106 // Length is smi tagged when it is stored.
3107 static const int kLengthOffset = HeapObject::kHeaderSize;
3108 static const int kHeaderSize = kLengthOffset + kPointerSize;
3109
3110 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003111
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003112 // Maximal memory consumption for a single ByteArray.
3113 static const int kMaxSize = 512 * MB;
3114 // Maximal length of a single ByteArray.
3115 static const int kMaxLength = kMaxSize - kHeaderSize;
3116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117 private:
3118 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
3119};
3120
3121
ager@chromium.org3811b432009-10-28 14:53:37 +00003122// An ExternalArray represents a fixed-size array of primitive values
3123// which live outside the JavaScript heap. Its subclasses are used to
3124// implement the CanvasArray types being defined in the WebGL
3125// specification. As of this writing the first public draft is not yet
3126// available, but Khronos members can access the draft at:
3127// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
3128//
3129// The semantics of these arrays differ from CanvasPixelArray.
3130// Out-of-range values passed to the setter are converted via a C
3131// cast, not clamping. Out-of-range indices cause exceptions to be
3132// raised rather than being silently ignored.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003133class ExternalArray: public HeapObject {
ager@chromium.org3811b432009-10-28 14:53:37 +00003134 public:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003135 // [length]: length of the array.
3136 inline int length();
3137 inline void set_length(int value);
3138
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003139 inline bool is_the_hole(int index) { return false; }
3140
ager@chromium.org3811b432009-10-28 14:53:37 +00003141 // [external_pointer]: The pointer to the external memory area backing this
3142 // external array.
3143 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
3144
3145 // Casting.
3146 static inline ExternalArray* cast(Object* obj);
3147
3148 // Maximal acceptable length for an external array.
3149 static const int kMaxLength = 0x3fffffff;
3150
3151 // ExternalArray headers are not quadword aligned.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003152 static const int kLengthOffset = HeapObject::kHeaderSize;
3153 static const int kExternalPointerOffset =
3154 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
ager@chromium.org3811b432009-10-28 14:53:37 +00003155 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003156 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
ager@chromium.org3811b432009-10-28 14:53:37 +00003157
3158 private:
3159 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
3160};
3161
3162
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00003163// A ExternalPixelArray represents a fixed-size byte array with special
3164// semantics used for implementing the CanvasPixelArray object. Please see the
3165// specification at:
3166
3167// http://www.whatwg.org/specs/web-apps/current-work/
3168// multipage/the-canvas-element.html#canvaspixelarray
3169// In particular, write access clamps the value written to 0 or 255 if the
3170// value written is outside this range.
3171class ExternalPixelArray: public ExternalArray {
3172 public:
3173 inline uint8_t* external_pixel_pointer();
3174
3175 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003176 inline uint8_t get_scalar(int index);
3177 inline MaybeObject* get(int index);
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00003178 inline void set(int index, uint8_t value);
3179
3180 // This accessor applies the correct conversion from Smi, HeapNumber and
3181 // undefined and clamps the converted value between 0 and 255.
3182 Object* SetValue(uint32_t index, Object* value);
3183
3184 // Casting.
3185 static inline ExternalPixelArray* cast(Object* obj);
3186
3187#ifdef OBJECT_PRINT
3188 inline void ExternalPixelArrayPrint() {
3189 ExternalPixelArrayPrint(stdout);
3190 }
3191 void ExternalPixelArrayPrint(FILE* out);
3192#endif
3193#ifdef DEBUG
3194 void ExternalPixelArrayVerify();
3195#endif // DEBUG
3196
3197 private:
3198 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
3199};
3200
3201
ager@chromium.org3811b432009-10-28 14:53:37 +00003202class ExternalByteArray: public ExternalArray {
3203 public:
3204 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003205 inline int8_t get_scalar(int index);
3206 inline MaybeObject* get(int index);
ager@chromium.org3811b432009-10-28 14:53:37 +00003207 inline void set(int index, int8_t value);
3208
3209 // This accessor applies the correct conversion from Smi, HeapNumber
3210 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003211 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003212
3213 // Casting.
3214 static inline ExternalByteArray* cast(Object* obj);
3215
whesse@chromium.org023421e2010-12-21 12:19:12 +00003216#ifdef OBJECT_PRINT
3217 inline void ExternalByteArrayPrint() {
3218 ExternalByteArrayPrint(stdout);
3219 }
3220 void ExternalByteArrayPrint(FILE* out);
3221#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003222#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003223 void ExternalByteArrayVerify();
3224#endif // DEBUG
3225
3226 private:
3227 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
3228};
3229
3230
3231class ExternalUnsignedByteArray: public ExternalArray {
3232 public:
3233 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003234 inline uint8_t get_scalar(int index);
3235 inline MaybeObject* get(int index);
ager@chromium.org3811b432009-10-28 14:53:37 +00003236 inline void set(int index, uint8_t value);
3237
3238 // This accessor applies the correct conversion from Smi, HeapNumber
3239 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003240 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003241
3242 // Casting.
3243 static inline ExternalUnsignedByteArray* cast(Object* obj);
3244
whesse@chromium.org023421e2010-12-21 12:19:12 +00003245#ifdef OBJECT_PRINT
3246 inline void ExternalUnsignedByteArrayPrint() {
3247 ExternalUnsignedByteArrayPrint(stdout);
3248 }
3249 void ExternalUnsignedByteArrayPrint(FILE* out);
3250#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003251#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003252 void ExternalUnsignedByteArrayVerify();
3253#endif // DEBUG
3254
3255 private:
3256 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
3257};
3258
3259
3260class ExternalShortArray: public ExternalArray {
3261 public:
3262 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003263 inline int16_t get_scalar(int index);
3264 inline MaybeObject* get(int index);
ager@chromium.org3811b432009-10-28 14:53:37 +00003265 inline void set(int index, int16_t value);
3266
3267 // This accessor applies the correct conversion from Smi, HeapNumber
3268 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003269 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003270
3271 // Casting.
3272 static inline ExternalShortArray* cast(Object* obj);
3273
whesse@chromium.org023421e2010-12-21 12:19:12 +00003274#ifdef OBJECT_PRINT
3275 inline void ExternalShortArrayPrint() {
3276 ExternalShortArrayPrint(stdout);
3277 }
3278 void ExternalShortArrayPrint(FILE* out);
3279#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003280#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003281 void ExternalShortArrayVerify();
3282#endif // DEBUG
3283
3284 private:
3285 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
3286};
3287
3288
3289class ExternalUnsignedShortArray: public ExternalArray {
3290 public:
3291 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003292 inline uint16_t get_scalar(int index);
3293 inline MaybeObject* get(int index);
ager@chromium.org3811b432009-10-28 14:53:37 +00003294 inline void set(int index, uint16_t value);
3295
3296 // This accessor applies the correct conversion from Smi, HeapNumber
3297 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003298 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003299
3300 // Casting.
3301 static inline ExternalUnsignedShortArray* cast(Object* obj);
3302
whesse@chromium.org023421e2010-12-21 12:19:12 +00003303#ifdef OBJECT_PRINT
3304 inline void ExternalUnsignedShortArrayPrint() {
3305 ExternalUnsignedShortArrayPrint(stdout);
3306 }
3307 void ExternalUnsignedShortArrayPrint(FILE* out);
3308#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003309#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003310 void ExternalUnsignedShortArrayVerify();
3311#endif // DEBUG
3312
3313 private:
3314 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3315};
3316
3317
3318class ExternalIntArray: public ExternalArray {
3319 public:
3320 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003321 inline int32_t get_scalar(int index);
3322 inline MaybeObject* get(int index);
ager@chromium.org3811b432009-10-28 14:53:37 +00003323 inline void set(int index, int32_t value);
3324
3325 // This accessor applies the correct conversion from Smi, HeapNumber
3326 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003327 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003328
3329 // Casting.
3330 static inline ExternalIntArray* cast(Object* obj);
3331
whesse@chromium.org023421e2010-12-21 12:19:12 +00003332#ifdef OBJECT_PRINT
3333 inline void ExternalIntArrayPrint() {
3334 ExternalIntArrayPrint(stdout);
3335 }
3336 void ExternalIntArrayPrint(FILE* out);
3337#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003338#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003339 void ExternalIntArrayVerify();
3340#endif // DEBUG
3341
3342 private:
3343 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3344};
3345
3346
3347class ExternalUnsignedIntArray: public ExternalArray {
3348 public:
3349 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003350 inline uint32_t get_scalar(int index);
3351 inline MaybeObject* get(int index);
ager@chromium.org3811b432009-10-28 14:53:37 +00003352 inline void set(int index, uint32_t value);
3353
3354 // This accessor applies the correct conversion from Smi, HeapNumber
3355 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003356 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003357
3358 // Casting.
3359 static inline ExternalUnsignedIntArray* cast(Object* obj);
3360
whesse@chromium.org023421e2010-12-21 12:19:12 +00003361#ifdef OBJECT_PRINT
3362 inline void ExternalUnsignedIntArrayPrint() {
3363 ExternalUnsignedIntArrayPrint(stdout);
3364 }
3365 void ExternalUnsignedIntArrayPrint(FILE* out);
3366#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003367#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003368 void ExternalUnsignedIntArrayVerify();
3369#endif // DEBUG
3370
3371 private:
3372 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3373};
3374
3375
3376class ExternalFloatArray: public ExternalArray {
3377 public:
3378 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003379 inline float get_scalar(int index);
3380 inline MaybeObject* get(int index);
ager@chromium.org3811b432009-10-28 14:53:37 +00003381 inline void set(int index, float value);
3382
3383 // This accessor applies the correct conversion from Smi, HeapNumber
3384 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003385 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003386
3387 // Casting.
3388 static inline ExternalFloatArray* cast(Object* obj);
3389
whesse@chromium.org023421e2010-12-21 12:19:12 +00003390#ifdef OBJECT_PRINT
3391 inline void ExternalFloatArrayPrint() {
3392 ExternalFloatArrayPrint(stdout);
3393 }
3394 void ExternalFloatArrayPrint(FILE* out);
3395#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003396#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003397 void ExternalFloatArrayVerify();
3398#endif // DEBUG
3399
3400 private:
3401 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3402};
3403
3404
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00003405class ExternalDoubleArray: public ExternalArray {
3406 public:
3407 // Setter and getter.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00003408 inline double get_scalar(int index);
3409 inline MaybeObject* get(int index);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00003410 inline void set(int index, double value);
3411
3412 // This accessor applies the correct conversion from Smi, HeapNumber
3413 // and undefined.
3414 MaybeObject* SetValue(uint32_t index, Object* value);
3415
3416 // Casting.
3417 static inline ExternalDoubleArray* cast(Object* obj);
3418
3419#ifdef OBJECT_PRINT
3420 inline void ExternalDoubleArrayPrint() {
3421 ExternalDoubleArrayPrint(stdout);
3422 }
3423 void ExternalDoubleArrayPrint(FILE* out);
3424#endif // OBJECT_PRINT
3425#ifdef DEBUG
3426 void ExternalDoubleArrayVerify();
3427#endif // DEBUG
3428
3429 private:
3430 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalDoubleArray);
3431};
3432
3433
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003434// DeoptimizationInputData is a fixed array used to hold the deoptimization
3435// data for code generated by the Hydrogen/Lithium compiler. It also
3436// contains information about functions that were inlined. If N different
3437// functions were inlined then first N elements of the literal array will
3438// contain these functions.
3439//
3440// It can be empty.
3441class DeoptimizationInputData: public FixedArray {
3442 public:
3443 // Layout description. Indices in the array.
3444 static const int kTranslationByteArrayIndex = 0;
3445 static const int kInlinedFunctionCountIndex = 1;
3446 static const int kLiteralArrayIndex = 2;
3447 static const int kOsrAstIdIndex = 3;
3448 static const int kOsrPcOffsetIndex = 4;
3449 static const int kFirstDeoptEntryIndex = 5;
3450
3451 // Offsets of deopt entry elements relative to the start of the entry.
3452 static const int kAstIdOffset = 0;
3453 static const int kTranslationIndexOffset = 1;
3454 static const int kArgumentsStackHeightOffset = 2;
3455 static const int kDeoptEntrySize = 3;
3456
3457 // Simple element accessors.
3458#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3459 type* name() { \
3460 return type::cast(get(k##name##Index)); \
3461 } \
3462 void Set##name(type* value) { \
3463 set(k##name##Index, value); \
3464 }
3465
3466 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3467 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3468 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3469 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3470 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3471
3472 // Unchecked accessor to be used during GC.
3473 FixedArray* UncheckedLiteralArray() {
3474 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3475 }
3476
3477#undef DEFINE_ELEMENT_ACCESSORS
3478
3479 // Accessors for elements of the ith deoptimization entry.
3480#define DEFINE_ENTRY_ACCESSORS(name, type) \
3481 type* name(int i) { \
3482 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3483 } \
3484 void Set##name(int i, type* value) { \
3485 set(IndexForEntry(i) + k##name##Offset, value); \
3486 }
3487
3488 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3489 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3490 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3491
3492#undef DEFINE_ENTRY_ACCESSORS
3493
3494 int DeoptCount() {
3495 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3496 }
3497
3498 // Allocates a DeoptimizationInputData.
3499 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3500 PretenureFlag pretenure);
3501
3502 // Casting.
3503 static inline DeoptimizationInputData* cast(Object* obj);
3504
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003505#ifdef ENABLE_DISASSEMBLER
whesse@chromium.org023421e2010-12-21 12:19:12 +00003506 void DeoptimizationInputDataPrint(FILE* out);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003507#endif
3508
3509 private:
3510 static int IndexForEntry(int i) {
3511 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3512 }
3513
3514 static int LengthFor(int entry_count) {
3515 return IndexForEntry(entry_count);
3516 }
3517};
3518
3519
3520// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3521// data for code generated by the full compiler.
3522// The format of the these objects is
3523// [i * 2]: Ast ID for ith deoptimization.
3524// [i * 2 + 1]: PC and state of ith deoptimization
3525class DeoptimizationOutputData: public FixedArray {
3526 public:
3527 int DeoptPoints() { return length() / 2; }
3528 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3529 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3530 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3531 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3532
3533 static int LengthOfFixedArray(int deopt_points) {
3534 return deopt_points * 2;
3535 }
3536
3537 // Allocates a DeoptimizationOutputData.
3538 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3539 PretenureFlag pretenure);
3540
3541 // Casting.
3542 static inline DeoptimizationOutputData* cast(Object* obj);
3543
whesse@chromium.org7b260152011-06-20 15:33:18 +00003544#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
whesse@chromium.org023421e2010-12-21 12:19:12 +00003545 void DeoptimizationOutputDataPrint(FILE* out);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003546#endif
3547};
3548
3549
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003550class SafepointEntry;
3551
3552
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553// Code describes objects with on-the-fly generated machine code.
3554class Code: public HeapObject {
3555 public:
3556 // Opaque data type for encapsulating code flags like kind, inline
3557 // cache state, and arguments count.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003558 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3559 // enumeration type has correct value range (see Issue 830 for more details).
3560 enum Flags {
3561 FLAGS_MIN_VALUE = kMinInt,
3562 FLAGS_MAX_VALUE = kMaxInt
3563 };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003564
3565 enum Kind {
3566 FUNCTION,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003567 OPTIMIZED_FUNCTION,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003568 STUB,
3569 BUILTIN,
3570 LOAD_IC,
3571 KEYED_LOAD_IC,
3572 CALL_IC,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003573 KEYED_CALL_IC,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574 STORE_IC,
3575 KEYED_STORE_IC,
danno@chromium.org40cb8782011-05-25 07:58:50 +00003576 UNARY_OP_IC,
3577 BINARY_OP_IC,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003578 COMPARE_IC,
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003579 TO_BOOLEAN_IC,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003580 // No more than 16 kinds. The value currently encoded in four bits in
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003581 // Flags.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582
3583 // Pseudo-kinds.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003584 REGEXP = BUILTIN,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585 FIRST_IC_KIND = LOAD_IC,
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003586 LAST_IC_KIND = TO_BOOLEAN_IC
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003587 };
3588
3589 enum {
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00003590 NUMBER_OF_KINDS = LAST_IC_KIND + 1
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 };
3592
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003593 typedef int ExtraICState;
3594
3595 static const ExtraICState kNoExtraICState = 0;
3596
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00003597#ifdef ENABLE_DISASSEMBLER
3598 // Printing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599 static const char* Kind2String(Kind kind);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003600 static const char* ICState2String(InlineCacheState state);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003601 static const char* PropertyType2String(PropertyType type);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00003602 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
whesse@chromium.org023421e2010-12-21 12:19:12 +00003603 inline void Disassemble(const char* name) {
3604 Disassemble(name, stdout);
3605 }
3606 void Disassemble(const char* name, FILE* out);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00003607#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608
3609 // [instruction_size]: Size of the native instructions
3610 inline int instruction_size();
3611 inline void set_instruction_size(int value);
3612
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003613 // [relocation_info]: Code relocation information
3614 DECL_ACCESSORS(relocation_info, ByteArray)
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003615 void InvalidateRelocation();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003616
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003617 // [deoptimization_data]: Array containing data for deopt.
3618 DECL_ACCESSORS(deoptimization_data, FixedArray)
3619
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003620 // [code_flushing_candidate]: Field only used during garbage
3621 // collection to hold code flushing candidates. The contents of this
3622 // field does not have to be traced during garbage collection since
3623 // it is only used by the garbage collector itself.
3624 DECL_ACCESSORS(next_code_flushing_candidate, Object)
3625
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003626 // Unchecked accessors to be used during GC.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003627 inline ByteArray* unchecked_relocation_info();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003628 inline FixedArray* unchecked_deoptimization_data();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630 inline int relocation_size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003631
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003632 // [flags]: Various code flags.
3633 inline Flags flags();
3634 inline void set_flags(Flags flags);
3635
3636 // [flags]: Access to specific code flags.
3637 inline Kind kind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003638 inline InlineCacheState ic_state(); // Only valid for IC stubs.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003639 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003640 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003641 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3642 inline int arguments_count(); // Only valid for call IC stubs.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643
3644 // Testers for IC stub kinds.
3645 inline bool is_inline_cache_stub();
3646 inline bool is_load_stub() { return kind() == LOAD_IC; }
3647 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3648 inline bool is_store_stub() { return kind() == STORE_IC; }
3649 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3650 inline bool is_call_stub() { return kind() == CALL_IC; }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003651 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003652 inline bool is_unary_op_stub() { return kind() == UNARY_OP_IC; }
3653 inline bool is_binary_op_stub() { return kind() == BINARY_OP_IC; }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003654 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003655 inline bool is_to_boolean_ic_stub() { return kind() == TO_BOOLEAN_IC; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003656
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003657 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003658 inline int major_key();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003659 inline void set_major_key(int value);
3660
3661 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3662 inline bool optimizable();
3663 inline void set_optimizable(bool value);
3664
3665 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3666 // deoptimization support.
3667 inline bool has_deoptimization_support();
3668 inline void set_has_deoptimization_support(bool value);
3669
3670 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3671 // how long the function has been marked for OSR and therefore which
3672 // level of loop nesting we are willing to do on-stack replacement
3673 // for.
3674 inline void set_allow_osr_at_loop_nesting_level(int level);
3675 inline int allow_osr_at_loop_nesting_level();
3676
3677 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3678 // reserved in the code prologue.
3679 inline unsigned stack_slots();
3680 inline void set_stack_slots(unsigned slots);
3681
3682 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3683 // the instruction stream where the safepoint table starts.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00003684 inline unsigned safepoint_table_offset();
3685 inline void set_safepoint_table_offset(unsigned offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003686
3687 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3688 // instruction stream where the stack check table starts.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00003689 inline unsigned stack_check_table_offset();
3690 inline void set_stack_check_table_offset(unsigned offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003691
3692 // [check type]: For kind CALL_IC, tells how to check if the
3693 // receiver is valid for the given call.
3694 inline CheckType check_type();
3695 inline void set_check_type(CheckType value);
3696
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003697 // [type-recording unary op type]: For kind UNARY_OP_IC.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003698 inline byte unary_op_type();
3699 inline void set_unary_op_type(byte value);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003700
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003701 // [type-recording binary op type]: For kind BINARY_OP_IC.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003702 inline byte binary_op_type();
3703 inline void set_binary_op_type(byte value);
3704 inline byte binary_op_result_type();
3705 inline void set_binary_op_result_type(byte value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003706
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003707 // [compare state]: For kind COMPARE_IC, tells what state the stub is in.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003708 inline byte compare_state();
3709 inline void set_compare_state(byte value);
3710
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003711 // [to_boolean_foo]: For kind TO_BOOLEAN_IC tells what state the stub is in.
3712 inline byte to_boolean_state();
3713 inline void set_to_boolean_state(byte value);
3714
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003715 // Get the safepoint entry for the given pc.
3716 SafepointEntry GetSafepointEntry(Address pc);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003717
3718 // Mark this code object as not having a stack check table. Assumes kind
3719 // is FUNCTION.
3720 void SetNoStackCheckTable();
3721
3722 // Find the first map in an IC stub.
3723 Map* FindFirstMap();
kasper.lund7276f142008-07-30 08:49:36 +00003724
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003725 // Flags operations.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003726 static inline Flags ComputeFlags(
3727 Kind kind,
3728 InLoopFlag in_loop = NOT_IN_LOOP,
3729 InlineCacheState ic_state = UNINITIALIZED,
3730 ExtraICState extra_ic_state = kNoExtraICState,
3731 PropertyType type = NORMAL,
3732 int argc = -1,
3733 InlineCacheHolderFlag holder = OWN_MAP);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003734
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003735 static inline Flags ComputeMonomorphicFlags(
3736 Kind kind,
3737 PropertyType type,
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003738 ExtraICState extra_ic_state = kNoExtraICState,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003739 InlineCacheHolderFlag holder = OWN_MAP,
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003740 InLoopFlag in_loop = NOT_IN_LOOP,
3741 int argc = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003742
3743 static inline Kind ExtractKindFromFlags(Flags flags);
kasper.lund7276f142008-07-30 08:49:36 +00003744 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003745 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003746 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3748 static inline int ExtractArgumentsCountFromFlags(Flags flags);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003749 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750 static inline Flags RemoveTypeFromFlags(Flags flags);
3751
ager@chromium.org8bb60582008-12-11 12:02:20 +00003752 // Convert a target address into a code object.
3753 static inline Code* GetCodeFromTargetAddress(Address address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003754
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00003755 // Convert an entry address into an object.
3756 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758 // Returns the address of the first instruction.
3759 inline byte* instruction_start();
3760
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003761 // Returns the address right after the last instruction.
3762 inline byte* instruction_end();
3763
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764 // Returns the size of the instructions, padding, and relocation information.
3765 inline int body_size();
3766
3767 // Returns the address of the first relocation info (read backwards!).
3768 inline byte* relocation_start();
3769
3770 // Code entry point.
3771 inline byte* entry();
3772
3773 // Returns true if pc is inside this object's instructions.
3774 inline bool contains(byte* pc);
3775
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003776 // Relocate the code by delta bytes. Called to signal that this code
3777 // object has been moved by delta bytes.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003778 void Relocate(intptr_t delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003779
3780 // Migrate code described by desc.
3781 void CopyFrom(const CodeDesc& desc);
3782
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003783 // Returns the object size for a given body (used for allocation).
3784 static int SizeFor(int body_size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785 ASSERT_SIZE_TAG_ALIGNED(body_size);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003786 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003787 }
3788
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003789 // Calculate the size of the code object to report for log events. This takes
3790 // the layout of the code object into account.
3791 int ExecutableSize() {
3792 // Check that the assumptions about the layout of the code object holds.
ager@chromium.orga1645e22009-09-09 19:27:10 +00003793 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3794 Code::kHeaderSize);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003795 return instruction_size() + Code::kHeaderSize;
3796 }
3797
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798 // Locating source position.
3799 int SourcePosition(Address pc);
3800 int SourceStatementPosition(Address pc);
3801
3802 // Casting.
3803 static inline Code* cast(Object* obj);
3804
3805 // Dispatched behavior.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003806 int CodeSize() { return SizeFor(body_size()); }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003807 inline void CodeIterateBody(ObjectVisitor* v);
3808
3809 template<typename StaticVisitor>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003810 inline void CodeIterateBody(Heap* heap);
whesse@chromium.org023421e2010-12-21 12:19:12 +00003811#ifdef OBJECT_PRINT
3812 inline void CodePrint() {
3813 CodePrint(stdout);
3814 }
3815 void CodePrint(FILE* out);
3816#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003818 void CodeVerify();
3819#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003820
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003821 // Returns the isolate/heap this code object belongs to.
3822 inline Isolate* isolate();
3823 inline Heap* heap();
3824
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003825 // Max loop nesting marker used to postpose OSR. We don't take loop
3826 // nesting that is deeper than 5 levels into account.
3827 static const int kMaxLoopNestingMarker = 6;
3828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003829 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00003830 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003831 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003832 static const int kDeoptimizationDataOffset =
3833 kRelocationInfoOffset + kPointerSize;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003834 static const int kNextCodeFlushingCandidateOffset =
3835 kDeoptimizationDataOffset + kPointerSize;
3836 static const int kFlagsOffset =
3837 kNextCodeFlushingCandidateOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003838
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003839 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003840 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3841
3842 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3843 kKindSpecificFlagsSize;
3844
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003845 // Add padding to align the instruction start following right after
kasperl@chromium.org061ef742009-02-27 12:16:20 +00003846 // the Code object header.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003847 static const int kHeaderSize =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003848 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
kasper.lund7276f142008-07-30 08:49:36 +00003849
3850 // Byte offsets within kKindSpecificFlagsOffset.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003851 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3852 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3853 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3854 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3855
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003856 static const int kUnaryOpTypeOffset = kStubMajorKeyOffset + 1;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003857 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
ricow@chromium.org9fa09672011-07-25 11:05:35 +00003858 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3859 static const int kToBooleanTypeOffset = kStubMajorKeyOffset + 1;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003860 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3861
3862 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3863 static const int kAllowOSRAtLoopNestingLevelOffset =
3864 kHasDeoptimizationSupportOffset + 1;
3865
ricow@chromium.org83aa5492011-02-07 12:42:56 +00003866 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3867 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003868
3869 // Flags layout.
kasper.lund7276f142008-07-30 08:49:36 +00003870 static const int kFlagsICStateShift = 0;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003871 static const int kFlagsICInLoopShift = 3;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003872 static const int kFlagsTypeShift = 4;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003873 static const int kFlagsKindShift = 8;
3874 static const int kFlagsICHolderShift = 12;
3875 static const int kFlagsExtraICStateShift = 13;
3876 static const int kFlagsArgumentsCountShift = 15;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003877
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003878 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3879 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003880 static const int kFlagsTypeMask = 0x000000F0; // 00001110000
3881 static const int kFlagsKindMask = 0x00000F00; // 11110000000
3882 static const int kFlagsCacheInPrototypeMapMask = 0x00001000;
3883 static const int kFlagsExtraICStateMask = 0x00006000;
3884 static const int kFlagsArgumentsCountMask = 0xFFFF8000;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003885
3886 static const int kFlagsNotUsedInLookup =
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003887 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003889 private:
3890 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3891};
3892
3893
3894// All heap objects have a Map that describes their structure.
3895// A Map contains information about:
3896// - Size information about the object
3897// - How to iterate over an object (for garbage collection)
3898class Map: public HeapObject {
3899 public:
ager@chromium.org32912102009-01-16 10:38:43 +00003900 // Instance size.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00003901 // Size in bytes or kVariableSizeSentinel if instances do not have
3902 // a fixed size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 inline int instance_size();
3904 inline void set_instance_size(int value);
3905
ager@chromium.org7c537e22008-10-16 08:43:32 +00003906 // Count of properties allocated in the object.
3907 inline int inobject_properties();
3908 inline void set_inobject_properties(int value);
3909
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003910 // Count of property fields pre-allocated in the object when first allocated.
3911 inline int pre_allocated_property_fields();
3912 inline void set_pre_allocated_property_fields(int value);
3913
ager@chromium.org32912102009-01-16 10:38:43 +00003914 // Instance type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003915 inline InstanceType instance_type();
3916 inline void set_instance_type(InstanceType value);
3917
ager@chromium.org32912102009-01-16 10:38:43 +00003918 // Tells how many unused property fields are available in the
3919 // instance (only used for JSObject in fast mode).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920 inline int unused_property_fields();
3921 inline void set_unused_property_fields(int value);
3922
ager@chromium.org32912102009-01-16 10:38:43 +00003923 // Bit field.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 inline byte bit_field();
3925 inline void set_bit_field(byte value);
3926
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003927 // Bit field 2.
3928 inline byte bit_field2();
3929 inline void set_bit_field2(byte value);
3930
danno@chromium.org40cb8782011-05-25 07:58:50 +00003931 // Bit field 3.
3932 // TODO(1399): It should be possible to make room for bit_field3 in the map
3933 // without overloading the instance descriptors field (and storing it in the
3934 // DescriptorArray when the map has one).
3935 inline int bit_field3();
3936 inline void set_bit_field3(int value);
3937
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003938 // Tells whether the object in the prototype property will be used
3939 // for instances created from this function. If the prototype
3940 // property is set to a value that is not a JSObject, the prototype
3941 // property will not be used to create instances of the function.
3942 // See ECMA-262, 13.2.2.
3943 inline void set_non_instance_prototype(bool value);
3944 inline bool has_non_instance_prototype();
3945
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00003946 // Tells whether function has special prototype property. If not, prototype
3947 // property will not be created when accessed (will return undefined),
3948 // and construction from this function will not be allowed.
3949 inline void set_function_with_prototype(bool value);
3950 inline bool function_with_prototype();
3951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003952 // Tells whether the instance with this map should be ignored by the
3953 // __proto__ accessor.
3954 inline void set_is_hidden_prototype() {
3955 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3956 }
3957
3958 inline bool is_hidden_prototype() {
3959 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3960 }
3961
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003962 // Records and queries whether the instance has a named interceptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003963 inline void set_has_named_interceptor() {
3964 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3965 }
3966
3967 inline bool has_named_interceptor() {
3968 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3969 }
3970
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003971 // Records and queries whether the instance has an indexed interceptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003972 inline void set_has_indexed_interceptor() {
3973 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3974 }
3975
3976 inline bool has_indexed_interceptor() {
3977 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3978 }
3979
3980 // Tells whether the instance is undetectable.
3981 // An undetectable object is a special class of JSObject: 'typeof' operator
3982 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3983 // a normal JS object. It is useful for implementing undetectable
3984 // document.all in Firefox & Safari.
3985 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3986 inline void set_is_undetectable() {
3987 set_bit_field(bit_field() | (1 << kIsUndetectable));
3988 }
3989
3990 inline bool is_undetectable() {
3991 return ((1 << kIsUndetectable) & bit_field()) != 0;
3992 }
3993
3994 // Tells whether the instance has a call-as-function handler.
3995 inline void set_has_instance_call_handler() {
3996 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3997 }
3998
3999 inline bool has_instance_call_handler() {
4000 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
4001 }
4002
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00004003 inline void set_is_extensible(bool value);
4004 inline bool is_extensible();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004005
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004006 inline void set_elements_kind(JSObject::ElementsKind elements_kind) {
4007 ASSERT(elements_kind < JSObject::kElementsKindCount);
4008 ASSERT(JSObject::kElementsKindCount <= (1 << kElementsKindBitCount));
4009 set_bit_field2((bit_field2() & ~kElementsKindMask) |
4010 (elements_kind << kElementsKindShift));
4011 ASSERT(this->elements_kind() == elements_kind);
4012 }
4013
4014 inline JSObject::ElementsKind elements_kind() {
4015 return static_cast<JSObject::ElementsKind>(
4016 (bit_field2() & kElementsKindMask) >> kElementsKindShift);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004017 }
4018
whesse@chromium.org7b260152011-06-20 15:33:18 +00004019 // Tells whether the instance has fast elements.
4020 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004021 inline bool has_fast_elements() {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004022 return elements_kind() == JSObject::FAST_ELEMENTS;
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00004023 }
4024
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004025 inline bool has_fast_double_elements() {
4026 return elements_kind() == JSObject::FAST_DOUBLE_ELEMENTS;
4027 }
4028
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004029 inline bool has_external_array_elements() {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004030 JSObject::ElementsKind kind(elements_kind());
4031 return kind >= JSObject::FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
4032 kind <= JSObject::LAST_EXTERNAL_ARRAY_ELEMENTS_KIND;
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00004033 }
4034
ricow@chromium.org4f693d62011-07-04 14:01:31 +00004035 inline bool has_dictionary_elements() {
4036 return elements_kind() == JSObject::DICTIONARY_ELEMENTS;
4037 }
4038
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004039 // Tells whether the map is attached to SharedFunctionInfo
4040 // (for inobject slack tracking).
4041 inline void set_attached_to_shared_function_info(bool value);
4042
4043 inline bool attached_to_shared_function_info();
4044
4045 // Tells whether the map is shared between objects that may have different
4046 // behavior. If true, the map should never be modified, instead a clone
4047 // should be created and modified.
4048 inline void set_is_shared(bool value);
4049
4050 inline bool is_shared();
4051
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004052 // Tells whether the instance needs security checks when accessing its
4053 // properties.
ager@chromium.org870a0b62008-11-04 11:43:05 +00004054 inline void set_is_access_check_needed(bool access_check_needed);
4055 inline bool is_access_check_needed();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004056
4057 // [prototype]: implicit prototype object.
4058 DECL_ACCESSORS(prototype, Object)
4059
4060 // [constructor]: points back to the function responsible for this map.
4061 DECL_ACCESSORS(constructor, Object)
4062
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004063 inline JSFunction* unchecked_constructor();
4064
danno@chromium.org40cb8782011-05-25 07:58:50 +00004065 // Should only be called by the code that initializes map to set initial valid
4066 // value of the instance descriptor member.
4067 inline void init_instance_descriptors();
4068
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069 // [instance descriptors]: describes the object.
4070 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
4071
danno@chromium.org40cb8782011-05-25 07:58:50 +00004072 // Sets the instance descriptor array for the map to be an empty descriptor
4073 // array.
4074 inline void clear_instance_descriptors();
4075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004076 // [stub cache]: contains stubs compiled for this map.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004077 DECL_ACCESSORS(code_cache, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004078
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00004079 // [prototype transitions]: cache of prototype transitions.
4080 // Prototype transition is a transition that happens
4081 // when we change object's prototype to a new one.
4082 // Cache format:
4083 // 0: finger - index of the first free cell in the cache
4084 // 1 + 2 * i: prototype
4085 // 2 + 2 * i: target map
4086 DECL_ACCESSORS(prototype_transitions, FixedArray)
4087 inline FixedArray* unchecked_prototype_transitions();
4088
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004089 static const int kProtoTransitionHeaderSize = 1;
4090 static const int kProtoTransitionNumberOfEntriesOffset = 0;
4091 static const int kProtoTransitionElementsPerEntry = 2;
4092 static const int kProtoTransitionPrototypeOffset = 0;
4093 static const int kProtoTransitionMapOffset = 1;
4094
4095 inline int NumberOfProtoTransitions() {
4096 FixedArray* cache = unchecked_prototype_transitions();
4097 if (cache->length() == 0) return 0;
4098 return
4099 Smi::cast(cache->get(kProtoTransitionNumberOfEntriesOffset))->value();
4100 }
4101
4102 inline void SetNumberOfProtoTransitions(int value) {
4103 FixedArray* cache = unchecked_prototype_transitions();
4104 ASSERT(cache->length() != 0);
4105 cache->set_unchecked(kProtoTransitionNumberOfEntriesOffset,
4106 Smi::FromInt(value));
4107 }
4108
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004109 // Lookup in the map's instance descriptors and fill out the result
4110 // with the given holder if the name is found. The holder may be
4111 // NULL when this function is used from the compiler.
4112 void LookupInDescriptors(JSObject* holder,
4113 String* name,
4114 LookupResult* result);
4115
lrn@chromium.org303ada72010-10-27 09:33:13 +00004116 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004117
lrn@chromium.org303ada72010-10-27 09:33:13 +00004118 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
4119 NormalizedMapSharingMode sharing);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004120
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004121 // Returns a copy of the map, with all transitions dropped from the
4122 // instance descriptors.
lrn@chromium.org303ada72010-10-27 09:33:13 +00004123 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004124
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004125 // Returns this map if it already has elements that are fast, otherwise
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004126 // returns a copy of the map, with all transitions dropped from the
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004127 // descriptors and the ElementsKind set to FAST_ELEMENTS.
lrn@chromium.org303ada72010-10-27 09:33:13 +00004128 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004129
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004130 // Returns this map if it already has fast elements that are doubles,
4131 // otherwise returns a copy of the map, with all transitions dropped from the
4132 // descriptors and the ElementsKind set to FAST_DOUBLE_ELEMENTS.
4133 MUST_USE_RESULT inline MaybeObject* GetFastDoubleElementsMap();
4134
4135 // Returns this map if already has dictionary elements, otherwise returns a
4136 // copy of the map, with all transitions dropped from the descriptors and the
4137 // ElementsKind set to DICTIONARY_ELEMENTS.
lrn@chromium.org303ada72010-10-27 09:33:13 +00004138 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004139
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004140 // Returns a new map with all transitions dropped from the descriptors and the
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004141 // ElementsKind set to one of the value corresponding to array_type.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004142 MUST_USE_RESULT MaybeObject* GetExternalArrayElementsMap(
4143 ExternalArrayType array_type,
4144 bool safe_to_add_transition);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00004145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146 // Returns the property index for name (only valid for FAST MODE).
4147 int PropertyIndexFor(String* name);
4148
4149 // Returns the next free property index (only valid for FAST MODE).
4150 int NextFreePropertyIndex();
4151
4152 // Returns the number of properties described in instance_descriptors.
4153 int NumberOfDescribedProperties();
4154
4155 // Casting.
4156 static inline Map* cast(Object* obj);
4157
4158 // Locate an accessor in the instance descriptor.
4159 AccessorDescriptor* FindAccessor(String* name);
4160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004161 // Code cache operations.
4162
4163 // Clears the code cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004164 inline void ClearCodeCache(Heap* heap);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004165
4166 // Update code cache.
lrn@chromium.org303ada72010-10-27 09:33:13 +00004167 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004168
4169 // Returns the found code or undefined if absent.
4170 Object* FindInCodeCache(String* name, Code::Flags flags);
4171
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004172 // Returns the non-negative index of the code object if it is in the
4173 // cache and -1 otherwise.
sgjesse@chromium.org99a37fa2010-03-11 09:23:46 +00004174 int IndexInCodeCache(Object* name, Code* code);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004175
4176 // Removes a code object from the code cache at the given index.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004177 void RemoveFromCodeCache(String* name, Code* code, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004178
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004179 // For every transition in this map, makes the transition's
4180 // target's prototype pointer point back to this map.
4181 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
4182 void CreateBackPointers();
4183
4184 // Set all map transitions from this map to dead maps to null.
4185 // Also, restore the original prototype on the targets of these
4186 // transitions, so that we do not process this map again while
4187 // following back pointers.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004188 void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004189
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004190 // Computes a hash value for this map, to be used in HashTables and such.
4191 int Hash();
4192
4193 // Compares this map to another to see if they describe equivalent objects.
4194 // If |mode| is set to CLEAR_INOBJECT_PROPERTIES, |other| is treated as if
4195 // it had exactly zero inobject properties.
4196 // The "shared" flags of both this map and |other| are ignored.
4197 bool EquivalentToForNormalization(Map* other, PropertyNormalizationMode mode);
4198
4199 // Returns true if this map and |other| describe equivalent objects.
4200 // The "shared" flags of both this map and |other| are ignored.
4201 bool EquivalentTo(Map* other) {
4202 return EquivalentToForNormalization(other, KEEP_INOBJECT_PROPERTIES);
4203 }
4204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004205 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00004206#ifdef OBJECT_PRINT
4207 inline void MapPrint() {
4208 MapPrint(stdout);
4209 }
4210 void MapPrint(FILE* out);
4211#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004212#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004213 void MapVerify();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004214 void SharedMapVerify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004215#endif
4216
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004217 inline int visitor_id();
4218 inline void set_visitor_id(int visitor_id);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004219
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004220 // Returns the isolate/heap this map belongs to.
4221 inline Isolate* isolate();
4222 inline Heap* heap();
4223
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004224 typedef void (*TraverseCallback)(Map* map, void* data);
4225
4226 void TraverseTransitionTree(TraverseCallback callback, void* data);
4227
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00004228 static const int kMaxCachedPrototypeTransitions = 256;
4229
4230 Object* GetPrototypeTransition(Object* prototype);
4231
4232 MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
4233
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004234 static const int kMaxPreAllocatedPropertyFields = 255;
4235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004236 // Layout description.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004237 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
4238 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004239 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
4240 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004241 // Storage for instance descriptors is overloaded to also contain additional
4242 // map flags when unused (bit_field3). When the map has instance descriptors,
4243 // the flags are transferred to the instance descriptor array and accessed
4244 // through an extra indirection.
4245 // TODO(1399): It should be possible to make room for bit_field3 in the map
4246 // without overloading the instance descriptors field, but the map is
4247 // currently perfectly aligned to 32 bytes and extending it at all would
4248 // double its size. After the increment GC work lands, this size restriction
4249 // could be loosened and bit_field3 moved directly back in the map.
4250 static const int kInstanceDescriptorsOrBitField3Offset =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004251 kConstructorOffset + kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004252 static const int kCodeCacheOffset =
4253 kInstanceDescriptorsOrBitField3Offset + kPointerSize;
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00004254 static const int kPrototypeTransitionsOffset =
4255 kCodeCacheOffset + kPointerSize;
4256 static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004257 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
4258
4259 // Layout of pointer fields. Heap iteration code relies on them
4260 // being continiously allocated.
4261 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
4262 static const int kPointerFieldsEndOffset =
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00004263 Map::kPrototypeTransitionsOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004264
ager@chromium.org7c537e22008-10-16 08:43:32 +00004265 // Byte offsets within kInstanceSizesOffset.
4266 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +00004267 static const int kInObjectPropertiesByte = 1;
4268 static const int kInObjectPropertiesOffset =
4269 kInstanceSizesOffset + kInObjectPropertiesByte;
4270 static const int kPreAllocatedPropertyFieldsByte = 2;
4271 static const int kPreAllocatedPropertyFieldsOffset =
4272 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004273 static const int kVisitorIdByte = 3;
4274 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004275
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276 // Byte offsets within kInstanceAttributesOffset attributes.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004277 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
4278 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
4279 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004280 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004281
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004282 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
4283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004284 // Bit positions for bit field.
mads.s.ager31e71382008-08-13 09:32:07 +00004285 static const int kUnused = 0; // To be used for marking recently used maps.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004286 static const int kHasNonInstancePrototype = 1;
4287 static const int kIsHiddenPrototype = 2;
4288 static const int kHasNamedInterceptor = 3;
4289 static const int kHasIndexedInterceptor = 4;
4290 static const int kIsUndetectable = 5;
4291 static const int kHasInstanceCallHandler = 6;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004292 static const int kIsAccessCheckNeeded = 7;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004293
ager@chromium.orge2902be2009-06-08 12:21:35 +00004294 // Bit positions for bit field 2
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004295 static const int kIsExtensible = 0;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004296 static const int kFunctionWithPrototype = 1;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004297 static const int kStringWrapperSafeForDefaultValueOf = 2;
4298 static const int kAttachedToSharedFunctionInfo = 3;
4299 // No bits can be used after kElementsKindFirstBit, they are all reserved for
4300 // storing ElementKind. for anything other than storing the ElementKind.
4301 static const int kElementsKindShift = 4;
4302 static const int kElementsKindBitCount = 4;
4303
4304 // Derived values from bit field 2
4305 static const int kElementsKindMask = (-1 << kElementsKindShift) &
4306 ((1 << (kElementsKindShift + kElementsKindBitCount)) - 1);
4307 static const int8_t kMaximumBitField2FastElementValue = static_cast<int8_t>(
4308 (JSObject::FAST_ELEMENTS + 1) << Map::kElementsKindShift) - 1;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004309
4310 // Bit positions for bit field 3
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004311 static const int kIsShared = 0;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004312
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004313 // Layout of the default cache. It holds alternating name and code objects.
4314 static const int kCodeCacheEntrySize = 2;
4315 static const int kCodeCacheEntryNameOffset = 0;
4316 static const int kCodeCacheEntryCodeOffset = 1;
4317
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004318 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
4319 kPointerFieldsEndOffset,
4320 kSize> BodyDescriptor;
4321
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004322 private:
4323 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
4324};
4325
4326
4327// An abstract superclass, a marker class really, for simple structure classes.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004328// It doesn't carry much functionality but allows struct classes to be
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004329// identified in the type system.
4330class Struct: public HeapObject {
4331 public:
4332 inline void InitializeBody(int object_size);
4333 static inline Struct* cast(Object* that);
4334};
4335
4336
mads.s.ager31e71382008-08-13 09:32:07 +00004337// Script describes a script which has been added to the VM.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004338class Script: public Struct {
4339 public:
ager@chromium.orge2902be2009-06-08 12:21:35 +00004340 // Script types.
4341 enum Type {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004342 TYPE_NATIVE = 0,
4343 TYPE_EXTENSION = 1,
4344 TYPE_NORMAL = 2
ager@chromium.orge2902be2009-06-08 12:21:35 +00004345 };
4346
4347 // Script compilation types.
4348 enum CompilationType {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004349 COMPILATION_TYPE_HOST = 0,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004350 COMPILATION_TYPE_EVAL = 1
ager@chromium.orge2902be2009-06-08 12:21:35 +00004351 };
4352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004353 // [source]: the script source.
4354 DECL_ACCESSORS(source, Object)
4355
4356 // [name]: the script name.
4357 DECL_ACCESSORS(name, Object)
4358
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004359 // [id]: the script id.
4360 DECL_ACCESSORS(id, Object)
4361
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004362 // [line_offset]: script line offset in resource from where it was extracted.
4363 DECL_ACCESSORS(line_offset, Smi)
4364
4365 // [column_offset]: script column offset in resource from where it was
4366 // extracted.
4367 DECL_ACCESSORS(column_offset, Smi)
4368
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004369 // [data]: additional data associated with this script.
4370 DECL_ACCESSORS(data, Object)
4371
ager@chromium.org9085a012009-05-11 19:22:57 +00004372 // [context_data]: context data for the context this script was compiled in.
4373 DECL_ACCESSORS(context_data, Object)
4374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004375 // [wrapper]: the wrapper cache.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004376 DECL_ACCESSORS(wrapper, Foreign)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004377
4378 // [type]: the script type.
4379 DECL_ACCESSORS(type, Smi)
4380
ager@chromium.orge2902be2009-06-08 12:21:35 +00004381 // [compilation]: how the the script was compiled.
4382 DECL_ACCESSORS(compilation_type, Smi)
4383
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004384 // [line_ends]: FixedArray of line ends positions.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00004385 DECL_ACCESSORS(line_ends, Object)
iposva@chromium.org245aa852009-02-10 00:49:54 +00004386
sgjesse@chromium.org98180592009-12-02 08:17:28 +00004387 // [eval_from_shared]: for eval scripts the shared funcion info for the
4388 // function from which eval was called.
4389 DECL_ACCESSORS(eval_from_shared, Object)
ager@chromium.orge2902be2009-06-08 12:21:35 +00004390
4391 // [eval_from_instructions_offset]: the instruction offset in the code for the
4392 // function from which eval was called where eval was called.
4393 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
4394
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004395 static inline Script* cast(Object* obj);
4396
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00004397 // If script source is an external string, check that the underlying
4398 // resource is accessible. Otherwise, always return true.
4399 inline bool HasValidSource();
4400
whesse@chromium.org023421e2010-12-21 12:19:12 +00004401#ifdef OBJECT_PRINT
4402 inline void ScriptPrint() {
4403 ScriptPrint(stdout);
4404 }
4405 void ScriptPrint(FILE* out);
4406#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004407#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004408 void ScriptVerify();
4409#endif
4410
ager@chromium.org236ad962008-09-25 09:45:57 +00004411 static const int kSourceOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004412 static const int kNameOffset = kSourceOffset + kPointerSize;
4413 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4414 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004415 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
ager@chromium.org9085a012009-05-11 19:22:57 +00004416 static const int kContextOffset = kDataOffset + kPointerSize;
4417 static const int kWrapperOffset = kContextOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004418 static const int kTypeOffset = kWrapperOffset + kPointerSize;
ager@chromium.orge2902be2009-06-08 12:21:35 +00004419 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00004420 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
4421 static const int kIdOffset = kLineEndsOffset + kPointerSize;
sgjesse@chromium.org98180592009-12-02 08:17:28 +00004422 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
ager@chromium.orge2902be2009-06-08 12:21:35 +00004423 static const int kEvalFrominstructionsOffsetOffset =
sgjesse@chromium.org98180592009-12-02 08:17:28 +00004424 kEvalFromSharedOffset + kPointerSize;
ager@chromium.orge2902be2009-06-08 12:21:35 +00004425 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004426
4427 private:
4428 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4429};
4430
4431
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004432// List of builtin functions we want to identify to improve code
4433// generation.
4434//
4435// Each entry has a name of a global object property holding an object
4436// optionally followed by ".prototype", a name of a builtin function
4437// on the object (the one the id is set for), and a label.
4438//
4439// Installation of ids for the selected builtin functions is handled
4440// by the bootstrapper.
4441//
4442// NOTE: Order is important: math functions should be at the end of
4443// the list and MathFloor should be the first math function.
4444#define FUNCTIONS_WITH_ID_LIST(V) \
4445 V(Array.prototype, push, ArrayPush) \
4446 V(Array.prototype, pop, ArrayPop) \
4447 V(String.prototype, charCodeAt, StringCharCodeAt) \
4448 V(String.prototype, charAt, StringCharAt) \
4449 V(String, fromCharCode, StringFromCharCode) \
4450 V(Math, floor, MathFloor) \
4451 V(Math, round, MathRound) \
4452 V(Math, ceil, MathCeil) \
4453 V(Math, abs, MathAbs) \
4454 V(Math, log, MathLog) \
4455 V(Math, sin, MathSin) \
4456 V(Math, cos, MathCos) \
4457 V(Math, tan, MathTan) \
4458 V(Math, asin, MathASin) \
4459 V(Math, acos, MathACos) \
4460 V(Math, atan, MathATan) \
4461 V(Math, exp, MathExp) \
4462 V(Math, sqrt, MathSqrt) \
4463 V(Math, pow, MathPow)
4464
4465
4466enum BuiltinFunctionId {
4467#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
4468 k##name,
4469 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4470#undef DECLARE_FUNCTION_ID
4471 // Fake id for a special case of Math.pow. Note, it continues the
4472 // list of math functions.
4473 kMathPowHalf,
4474 kFirstMathFunctionId = kMathFloor
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004475};
4476
4477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004478// SharedFunctionInfo describes the JSFunction information that can be
4479// shared by multiple instances of the function.
4480class SharedFunctionInfo: public HeapObject {
4481 public:
4482 // [name]: Function name.
4483 DECL_ACCESSORS(name, Object)
4484
4485 // [code]: Function code.
4486 DECL_ACCESSORS(code, Code)
4487
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004488 // [scope_info]: Scope info.
ager@chromium.orgb5737492010-07-15 09:29:43 +00004489 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004490
ager@chromium.org5aa501c2009-06-23 07:57:28 +00004491 // [construct stub]: Code stub for constructing instances of this function.
4492 DECL_ACCESSORS(construct_stub, Code)
4493
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004494 inline Code* unchecked_code();
4495
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004496 // Returns if this function has been compiled to native code yet.
4497 inline bool is_compiled();
4498
4499 // [length]: The function length - usually the number of declared parameters.
4500 // Use up to 2^30 parameters.
4501 inline int length();
4502 inline void set_length(int value);
4503
4504 // [formal parameter count]: The declared number of parameters.
4505 inline int formal_parameter_count();
4506 inline void set_formal_parameter_count(int value);
4507
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004508 // Set the formal parameter count so the function code will be
4509 // called without using argument adaptor frames.
4510 inline void DontAdaptArguments();
4511
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004512 // [expected_nof_properties]: Expected number of properties for the function.
4513 inline int expected_nof_properties();
4514 inline void set_expected_nof_properties(int value);
4515
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004516 // Inobject slack tracking is the way to reclaim unused inobject space.
4517 //
4518 // The instance size is initially determined by adding some slack to
4519 // expected_nof_properties (to allow for a few extra properties added
4520 // after the constructor). There is no guarantee that the extra space
4521 // will not be wasted.
4522 //
4523 // Here is the algorithm to reclaim the unused inobject space:
4524 // - Detect the first constructor call for this SharedFunctionInfo.
4525 // When it happens enter the "in progress" state: remember the
4526 // constructor's initial_map and install a special construct stub that
4527 // counts constructor calls.
4528 // - While the tracking is in progress create objects filled with
4529 // one_pointer_filler_map instead of undefined_value. This way they can be
4530 // resized quickly and safely.
4531 // - Once enough (kGenerousAllocationCount) objects have been created
4532 // compute the 'slack' (traverse the map transition tree starting from the
4533 // initial_map and find the lowest value of unused_property_fields).
4534 // - Traverse the transition tree again and decrease the instance size
4535 // of every map. Existing objects will resize automatically (they are
4536 // filled with one_pointer_filler_map). All further allocations will
4537 // use the adjusted instance size.
4538 // - Decrease expected_nof_properties so that an allocations made from
4539 // another context will use the adjusted instance size too.
4540 // - Exit "in progress" state by clearing the reference to the initial_map
4541 // and setting the regular construct stub (generic or inline).
4542 //
4543 // The above is the main event sequence. Some special cases are possible
4544 // while the tracking is in progress:
4545 //
4546 // - GC occurs.
4547 // Check if the initial_map is referenced by any live objects (except this
4548 // SharedFunctionInfo). If it is, continue tracking as usual.
4549 // If it is not, clear the reference and reset the tracking state. The
4550 // tracking will be initiated again on the next constructor call.
4551 //
4552 // - The constructor is called from another context.
4553 // Immediately complete the tracking, perform all the necessary changes
4554 // to maps. This is necessary because there is no efficient way to track
4555 // multiple initial_maps.
4556 // Proceed to create an object in the current context (with the adjusted
4557 // size).
4558 //
4559 // - A different constructor function sharing the same SharedFunctionInfo is
4560 // called in the same context. This could be another closure in the same
4561 // context, or the first function could have been disposed.
4562 // This is handled the same way as the previous case.
4563 //
4564 // Important: inobject slack tracking is not attempted during the snapshot
4565 // creation.
4566
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00004567 static const int kGenerousAllocationCount = 8;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004568
4569 // [construction_count]: Counter for constructor calls made during
4570 // the tracking phase.
4571 inline int construction_count();
4572 inline void set_construction_count(int value);
4573
4574 // [initial_map]: initial map of the first function called as a constructor.
4575 // Saved for the duration of the tracking phase.
4576 // This is a weak link (GC resets it to undefined_value if no other live
4577 // object reference this map).
4578 DECL_ACCESSORS(initial_map, Object)
4579
4580 // True if the initial_map is not undefined and the countdown stub is
4581 // installed.
4582 inline bool IsInobjectSlackTrackingInProgress();
4583
4584 // Starts the tracking.
4585 // Stores the initial map and installs the countdown stub.
4586 // IsInobjectSlackTrackingInProgress is normally true after this call,
4587 // except when tracking have not been started (e.g. the map has no unused
4588 // properties or the snapshot is being built).
4589 void StartInobjectSlackTracking(Map* map);
4590
4591 // Completes the tracking.
4592 // IsInobjectSlackTrackingInProgress is false after this call.
4593 void CompleteInobjectSlackTracking();
4594
4595 // Clears the initial_map before the GC marking phase to ensure the reference
4596 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4597 void DetachInitialMap();
4598
4599 // Restores the link to the initial map after the GC marking phase.
4600 // IsInobjectSlackTrackingInProgress is true after this call.
4601 void AttachInitialMap(Map* map);
4602
4603 // False if there are definitely no live objects created from this function.
4604 // True if live objects _may_ exist (existence not guaranteed).
4605 // May go back from true to false after GC.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004606 DECL_BOOLEAN_ACCESSORS(live_objects_may_exist)
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004608 // [instance class name]: class name for instances.
4609 DECL_ACCESSORS(instance_class_name, Object)
4610
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004611 // [function data]: This field holds some additional data for function.
4612 // Currently it either has FunctionTemplateInfo to make benefit the API
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004613 // or Smi identifying a builtin function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004614 // In the long run we don't want all functions to have this field but
4615 // we can fix that when we have a better model for storing hidden data
4616 // on objects.
4617 DECL_ACCESSORS(function_data, Object)
4618
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004619 inline bool IsApiFunction();
4620 inline FunctionTemplateInfo* get_api_func_data();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004621 inline bool HasBuiltinFunctionId();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004622 inline BuiltinFunctionId builtin_function_id();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004623
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004624 // [script info]: Script from which the function originates.
4625 DECL_ACCESSORS(script, Object)
4626
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004627 // [num_literals]: Number of literals used by this function.
4628 inline int num_literals();
4629 inline void set_num_literals(int value);
4630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004631 // [start_position_and_type]: Field used to store both the source code
4632 // position, whether or not the function is a function expression,
4633 // and whether or not the function is a toplevel function. The two
4634 // least significants bit indicates whether the function is an
4635 // expression and the rest contains the source code position.
4636 inline int start_position_and_type();
4637 inline void set_start_position_and_type(int value);
4638
4639 // [debug info]: Debug information.
4640 DECL_ACCESSORS(debug_info, Object)
4641
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00004642 // [inferred name]: Name inferred from variable or property
4643 // assignment of this function. Used to facilitate debugging and
4644 // profiling of JavaScript code written in OO style, where almost
4645 // all functions are anonymous but are assigned to object
4646 // properties.
4647 DECL_ACCESSORS(inferred_name, String)
4648
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004649 // The function's name if it is non-empty, otherwise the inferred name.
4650 String* DebugName();
4651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004652 // Position of the 'function' token in the script source.
4653 inline int function_token_position();
4654 inline void set_function_token_position(int function_token_position);
4655
4656 // Position of this function in the script source.
4657 inline int start_position();
4658 inline void set_start_position(int start_position);
4659
4660 // End position of this function in the script source.
4661 inline int end_position();
4662 inline void set_end_position(int end_position);
4663
4664 // Is this function a function expression in the source code.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004665 DECL_BOOLEAN_ACCESSORS(is_expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004666
ager@chromium.orga1645e22009-09-09 19:27:10 +00004667 // Is this function a top-level function (scripts, evals).
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004668 DECL_BOOLEAN_ACCESSORS(is_toplevel)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004669
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004670 // Bit field containing various information collected by the compiler to
4671 // drive optimization.
4672 inline int compiler_hints();
4673 inline void set_compiler_hints(int value);
4674
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004675 // A counter used to determine when to stress the deoptimizer with a
4676 // deopt.
4677 inline Smi* deopt_counter();
4678 inline void set_deopt_counter(Smi* counter);
4679
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004680 // Add information on assignments of the form this.x = ...;
4681 void SetThisPropertyAssignmentsInfo(
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004682 bool has_only_simple_this_property_assignments,
4683 FixedArray* this_property_assignments);
4684
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004685 // Clear information on assignments of the form this.x = ...;
4686 void ClearThisPropertyAssignmentsInfo();
4687
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004688 // Indicate that this function only consists of assignments of the form
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004689 // this.x = y; where y is either a constant or refers to an argument.
4690 inline bool has_only_simple_this_property_assignments();
4691
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00004692 // Indicates if this function can be lazy compiled.
4693 // This is used to determine if we can safely flush code from a function
4694 // when doing GC if we expect that the function will no longer be used.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004695 DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation)
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00004696
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004697 // Indicates how many full GCs this function has survived with assigned
4698 // code object. Used to determine when it is relatively safe to flush
4699 // this code object and replace it with lazy compilation stub.
4700 // Age is reset when GC notices that the code object is referenced
4701 // from the stack or compilation cache.
4702 inline int code_age();
4703 inline void set_code_age(int age);
4704
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004705 // Indicates whether optimizations have been disabled for this
4706 // shared function info. If a function is repeatedly optimized or if
4707 // we cannot optimize the function we disable optimization to avoid
4708 // spending time attempting to optimize it again.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004709 DECL_BOOLEAN_ACCESSORS(optimization_disabled)
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004710
ricow@chromium.org83aa5492011-02-07 12:42:56 +00004711 // Indicates whether the function is a strict mode function.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004712 DECL_BOOLEAN_ACCESSORS(strict_mode)
4713
4714 // False if the function definitely does not allocate an arguments object.
4715 DECL_BOOLEAN_ACCESSORS(uses_arguments)
4716
4717 // True if the function has any duplicated parameter names.
4718 DECL_BOOLEAN_ACCESSORS(has_duplicate_parameters)
ricow@chromium.org83aa5492011-02-07 12:42:56 +00004719
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004720 // Indicates whether the function is a native function.
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004721 // These needs special threatment in .call and .apply since
4722 // null passed as the receiver should not be translated to the
4723 // global object.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004724 DECL_BOOLEAN_ACCESSORS(native)
4725
4726 // Indicates that the function was created by the Function function.
4727 // Though it's anonymous, toString should treat it as if it had the name
4728 // "anonymous". We don't set the name itself so that the system does not
4729 // see a binding for it.
4730 DECL_BOOLEAN_ACCESSORS(name_should_print_as_anonymous)
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004731
whesse@chromium.org7b260152011-06-20 15:33:18 +00004732 // Indicates whether the function is a bound function created using
4733 // the bind function.
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004734 DECL_BOOLEAN_ACCESSORS(bound)
4735
4736 // Indicates that the function is anonymous (the name field can be set
4737 // through the API, which does not change this flag).
4738 DECL_BOOLEAN_ACCESSORS(is_anonymous)
whesse@chromium.org7b260152011-06-20 15:33:18 +00004739
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004740 // Indicates whether or not the code in the shared function support
4741 // deoptimization.
4742 inline bool has_deoptimization_support();
4743
4744 // Enable deoptimization support through recompiled code.
4745 void EnableDeoptimizationSupport(Code* recompiled);
4746
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004747 // Disable (further) attempted optimization of all functions sharing this
4748 // shared function info. The function is the one we actually tried to
4749 // optimize.
4750 void DisableOptimization(JSFunction* function);
4751
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004752 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4753 // code, returns whether it asserted (i.e., always true if assertions are
4754 // disabled).
4755 bool VerifyBailoutId(int id);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004756
ager@chromium.org5c838252010-02-19 08:53:10 +00004757 // Check whether a inlined constructor can be generated with the given
4758 // prototype.
4759 bool CanGenerateInlineConstructor(Object* prototype);
4760
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004761 // Prevents further attempts to generate inline constructors.
4762 // To be called if generation failed for any reason.
4763 void ForbidInlineConstructor();
4764
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004765 // For functions which only contains this property assignments this provides
4766 // access to the names for the properties assigned.
4767 DECL_ACCESSORS(this_property_assignments, Object)
4768 inline int this_property_assignments_count();
4769 inline void set_this_property_assignments_count(int value);
4770 String* GetThisPropertyAssignmentName(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004771 bool IsThisPropertyAssignmentArgument(int index);
4772 int GetThisPropertyAssignmentArgument(int index);
4773 Object* GetThisPropertyAssignmentConstant(int index);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004774
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004775 // [source code]: Source code for the function.
4776 bool HasSourceCode();
4777 Object* GetSourceCode();
4778
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004779 inline int opt_count();
4780 inline void set_opt_count(int opt_count);
4781
4782 // Source size of this function.
4783 int SourceSize();
4784
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004785 // Calculate the instance size.
4786 int CalculateInstanceSize();
4787
4788 // Calculate the number of in-object properties.
4789 int CalculateInObjectProperties();
4790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004791 // Dispatched behavior.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004792 // Set max_length to -1 for unlimited length.
4793 void SourceCodePrint(StringStream* accumulator, int max_length);
whesse@chromium.org023421e2010-12-21 12:19:12 +00004794#ifdef OBJECT_PRINT
4795 inline void SharedFunctionInfoPrint() {
4796 SharedFunctionInfoPrint(stdout);
4797 }
4798 void SharedFunctionInfoPrint(FILE* out);
4799#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004800#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004801 void SharedFunctionInfoVerify();
4802#endif
4803
4804 // Casting.
4805 static inline SharedFunctionInfo* cast(Object* obj);
4806
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004807 // Constants.
4808 static const int kDontAdaptArgumentsSentinel = -1;
4809
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004810 // Layout description.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004811 // Pointer fields.
ager@chromium.org236ad962008-09-25 09:45:57 +00004812 static const int kNameOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004813 static const int kCodeOffset = kNameOffset + kPointerSize;
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004814 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4815 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004816 static const int kInstanceClassNameOffset =
4817 kConstructStubOffset + kPointerSize;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004818 static const int kFunctionDataOffset =
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004819 kInstanceClassNameOffset + kPointerSize;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004820 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004821 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4822 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004823 static const int kInitialMapOffset =
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004824 kInferredNameOffset + kPointerSize;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004825 static const int kThisPropertyAssignmentsOffset =
4826 kInitialMapOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004827 static const int kDeoptCounterOffset =
4828 kThisPropertyAssignmentsOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004829#if V8_HOST_ARCH_32_BIT
4830 // Smi fields.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004831 static const int kLengthOffset =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004832 kDeoptCounterOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004833 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4834 static const int kExpectedNofPropertiesOffset =
4835 kFormalParameterCountOffset + kPointerSize;
4836 static const int kNumLiteralsOffset =
4837 kExpectedNofPropertiesOffset + kPointerSize;
4838 static const int kStartPositionAndTypeOffset =
4839 kNumLiteralsOffset + kPointerSize;
4840 static const int kEndPositionOffset =
4841 kStartPositionAndTypeOffset + kPointerSize;
4842 static const int kFunctionTokenPositionOffset =
4843 kEndPositionOffset + kPointerSize;
4844 static const int kCompilerHintsOffset =
4845 kFunctionTokenPositionOffset + kPointerSize;
4846 static const int kThisPropertyAssignmentsCountOffset =
4847 kCompilerHintsOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004848 static const int kOptCountOffset =
4849 kThisPropertyAssignmentsCountOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004850 // Total size.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004851 static const int kSize = kOptCountOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004852#else
4853 // The only reason to use smi fields instead of int fields
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004854 // is to allow iteration without maps decoding during
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004855 // garbage collections.
4856 // To avoid wasting space on 64-bit architectures we use
4857 // the following trick: we group integer fields into pairs
4858 // First integer in each pair is shifted left by 1.
4859 // By doing this we guarantee that LSB of each kPointerSize aligned
4860 // word is not set and thus this word cannot be treated as pointer
4861 // to HeapObject during old space traversal.
4862 static const int kLengthOffset =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004863 kDeoptCounterOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004864 static const int kFormalParameterCountOffset =
4865 kLengthOffset + kIntSize;
4866
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004867 static const int kExpectedNofPropertiesOffset =
4868 kFormalParameterCountOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004869 static const int kNumLiteralsOffset =
4870 kExpectedNofPropertiesOffset + kIntSize;
4871
4872 static const int kEndPositionOffset =
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004873 kNumLiteralsOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004874 static const int kStartPositionAndTypeOffset =
4875 kEndPositionOffset + kIntSize;
4876
4877 static const int kFunctionTokenPositionOffset =
4878 kStartPositionAndTypeOffset + kIntSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004879 static const int kCompilerHintsOffset =
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004880 kFunctionTokenPositionOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004881
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004882 static const int kThisPropertyAssignmentsCountOffset =
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004883 kCompilerHintsOffset + kIntSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004884 static const int kOptCountOffset =
4885 kThisPropertyAssignmentsCountOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004886
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004887 // Total size.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004888 static const int kSize = kOptCountOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004889
4890#endif
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004891
4892 // The construction counter for inobject slack tracking is stored in the
4893 // most significant byte of compiler_hints which is otherwise unused.
4894 // Its offset depends on the endian-ness of the architecture.
4895#if __BYTE_ORDER == __LITTLE_ENDIAN
4896 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4897#elif __BYTE_ORDER == __BIG_ENDIAN
4898 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4899#else
4900#error Unknown byte ordering
4901#endif
4902
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004903 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004904
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004905 typedef FixedBodyDescriptor<kNameOffset,
4906 kThisPropertyAssignmentsOffset + kPointerSize,
4907 kSize> BodyDescriptor;
4908
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004909 // Bit positions in start_position_and_type.
4910 // The source code start position is in the 30 most significant bits of
4911 // the start_position_and_type field.
4912 static const int kIsExpressionBit = 0;
4913 static const int kIsTopLevelBit = 1;
4914 static const int kStartPositionShift = 2;
4915 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004916
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004917 // Bit positions in compiler_hints.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004918 static const int kCodeAgeSize = 3;
4919 static const int kCodeAgeMask = (1 << kCodeAgeSize) - 1;
whesse@chromium.org7b260152011-06-20 15:33:18 +00004920
4921 enum CompilerHints {
4922 kHasOnlySimpleThisPropertyAssignments,
4923 kAllowLazyCompilation,
4924 kLiveObjectsMayExist,
4925 kCodeAgeShift,
4926 kOptimizationDisabled = kCodeAgeShift + kCodeAgeSize,
4927 kStrictModeFunction,
4928 kUsesArguments,
4929 kHasDuplicateParameters,
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004930 kNative,
4931 kBoundFunction,
4932 kIsAnonymous,
4933 kNameShouldPrintAsAnonymous,
4934 kCompilerHintsCount // Pseudo entry
whesse@chromium.org7b260152011-06-20 15:33:18 +00004935 };
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004936
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004937 private:
4938#if V8_HOST_ARCH_32_BIT
4939 // On 32 bit platforms, compiler hints is a smi.
4940 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4941 static const int kCompilerHintsSize = kPointerSize;
4942#else
4943 // On 64 bit platforms, compiler hints is not a smi, see comment above.
4944 static const int kCompilerHintsSmiTagSize = 0;
4945 static const int kCompilerHintsSize = kIntSize;
4946#endif
4947
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00004948 STATIC_ASSERT(SharedFunctionInfo::kCompilerHintsCount <=
4949 SharedFunctionInfo::kCompilerHintsSize * kBitsPerByte);
4950
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004951 public:
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004952 // Constants for optimizing codegen for strict mode function and
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004953 // native tests.
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004954 // Allows to use byte-widgh instructions.
4955 static const int kStrictModeBitWithinByte =
4956 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4957
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004958 static const int kNativeBitWithinByte =
4959 (kNative + kCompilerHintsSmiTagSize) % kBitsPerByte;
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004960
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004961#if __BYTE_ORDER == __LITTLE_ENDIAN
4962 static const int kStrictModeByteOffset = kCompilerHintsOffset +
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004963 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004964 static const int kNativeByteOffset = kCompilerHintsOffset +
4965 (kNative + kCompilerHintsSmiTagSize) / kBitsPerByte;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004966#elif __BYTE_ORDER == __BIG_ENDIAN
4967 static const int kStrictModeByteOffset = kCompilerHintsOffset +
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004968 (kCompilerHintsSize - 1) -
4969 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004970 static const int kNativeByteOffset = kCompilerHintsOffset +
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004971 (kCompilerHintsSize - 1) -
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004972 ((kNative + kCompilerHintsSmiTagSize) / kBitsPerByte);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004973#else
4974#error Unknown byte ordering
4975#endif
4976
4977 private:
mads.s.ager31e71382008-08-13 09:32:07 +00004978 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004979};
4980
4981
4982// JSFunction describes JavaScript functions.
4983class JSFunction: public JSObject {
4984 public:
4985 // [prototype_or_initial_map]:
4986 DECL_ACCESSORS(prototype_or_initial_map, Object)
4987
4988 // [shared_function_info]: The information about the function that
4989 // can be shared by instances.
4990 DECL_ACCESSORS(shared, SharedFunctionInfo)
4991
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004992 inline SharedFunctionInfo* unchecked_shared();
4993
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004994 // [context]: The context for this function.
4995 inline Context* context();
4996 inline Object* unchecked_context();
4997 inline void set_context(Object* context);
4998
4999 // [code]: The generated code object for this function. Executed
5000 // when the function is invoked, e.g. foo() or new foo(). See
5001 // [[Call]] and [[Construct]] description in ECMA-262, section
5002 // 8.6.2, page 27.
5003 inline Code* code();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005004 inline void set_code(Code* code);
5005 inline void ReplaceCode(Code* code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005006
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00005007 inline Code* unchecked_code();
5008
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00005009 // Tells whether this function is builtin.
5010 inline bool IsBuiltin();
5011
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005012 // Tells whether or not the function needs arguments adaption.
5013 inline bool NeedsArgumentsAdaption();
5014
5015 // Tells whether or not this function has been optimized.
5016 inline bool IsOptimized();
5017
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00005018 // Tells whether or not this function can be optimized.
5019 inline bool IsOptimizable();
5020
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005021 // Mark this function for lazy recompilation. The function will be
5022 // recompiled the next time it is executed.
5023 void MarkForLazyRecompilation();
5024
5025 // Tells whether or not the function is already marked for lazy
5026 // recompilation.
5027 inline bool IsMarkedForLazyRecompilation();
5028
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005029 // Check whether or not this function is inlineable.
5030 bool IsInlineable();
5031
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005032 // [literals]: Fixed array holding the materialized literals.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005033 //
5034 // If the function contains object, regexp or array literals, the
5035 // literals array prefix contains the object, regexp, and array
5036 // function to be used when creating these literals. This is
5037 // necessary so that we do not dynamically lookup the object, regexp
5038 // or array functions. Performing a dynamic lookup, we might end up
5039 // using the functions from a new context that we should not have
5040 // access to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005041 DECL_ACCESSORS(literals, FixedArray)
5042
5043 // The initial map for an object created by this constructor.
5044 inline Map* initial_map();
5045 inline void set_initial_map(Map* value);
5046 inline bool has_initial_map();
5047
5048 // Get and set the prototype property on a JSFunction. If the
5049 // function has an initial map the prototype is set on the initial
5050 // map. Otherwise, the prototype is put in the initial map field
5051 // until an initial map is needed.
5052 inline bool has_prototype();
5053 inline bool has_instance_prototype();
5054 inline Object* prototype();
5055 inline Object* instance_prototype();
5056 Object* SetInstancePrototype(Object* value);
lrn@chromium.org303ada72010-10-27 09:33:13 +00005057 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005058
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00005059 // After prototype is removed, it will not be created when accessed, and
5060 // [[Construct]] from this function will not be allowed.
5061 Object* RemovePrototype();
5062 inline bool should_have_prototype();
5063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005064 // Accessor for this function's initial map's [[class]]
5065 // property. This is primarily used by ECMA native functions. This
5066 // method sets the class_name field of this function's initial map
5067 // to a given value. It creates an initial map if this function does
5068 // not have one. Note that this method does not copy the initial map
5069 // if it has one already, but simply replaces it with the new value.
5070 // Instances created afterwards will have a map whose [[class]] is
5071 // set to 'value', but there is no guarantees on instances created
5072 // before.
5073 Object* SetInstanceClassName(String* name);
5074
5075 // Returns if this function has been compiled to native code yet.
5076 inline bool is_compiled();
5077
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005078 // [next_function_link]: Field for linking functions. This list is treated as
5079 // a weak list by the GC.
5080 DECL_ACCESSORS(next_function_link, Object)
5081
5082 // Prints the name of the function using PrintF.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005083 inline void PrintName() {
5084 PrintName(stdout);
5085 }
5086 void PrintName(FILE* out);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005088 // Casting.
5089 static inline JSFunction* cast(Object* obj);
5090
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005091 // Iterates the objects, including code objects indirectly referenced
5092 // through pointers to the first instruction in the code object.
5093 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
5094
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005095 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005096#ifdef OBJECT_PRINT
5097 inline void JSFunctionPrint() {
5098 JSFunctionPrint(stdout);
5099 }
5100 void JSFunctionPrint(FILE* out);
5101#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005102#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005103 void JSFunctionVerify();
5104#endif
5105
5106 // Returns the number of allocated literals.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005107 inline int NumberOfLiterals();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005108
ager@chromium.org236ad962008-09-25 09:45:57 +00005109 // Retrieve the global context from a function's literal array.
5110 static Context* GlobalContextFromLiterals(FixedArray* literals);
5111
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005112 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
5113 // kSize) is weak and has special handling during garbage collection.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005114 static const int kCodeEntryOffset = JSObject::kHeaderSize;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00005115 static const int kPrototypeOrInitialMapOffset =
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00005116 kCodeEntryOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005117 static const int kSharedFunctionInfoOffset =
5118 kPrototypeOrInitialMapOffset + kPointerSize;
5119 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
5120 static const int kLiteralsOffset = kContextOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005121 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
5122 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
5123 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005124
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00005125 // Layout of the literals array.
ager@chromium.org236ad962008-09-25 09:45:57 +00005126 static const int kLiteralsPrefixSize = 1;
5127 static const int kLiteralGlobalContextIndex = 0;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00005128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005129 private:
5130 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
5131};
5132
5133
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005134// JSGlobalProxy's prototype must be a JSGlobalObject or null,
5135// and the prototype is hidden. JSGlobalProxy always delegates
5136// property accesses to its prototype if the prototype is not null.
5137//
5138// A JSGlobalProxy can be reinitialized which will preserve its identity.
5139//
5140// Accessing a JSGlobalProxy requires security check.
5141
5142class JSGlobalProxy : public JSObject {
5143 public:
ager@chromium.orgea91cc52011-05-23 06:06:11 +00005144 // [context]: the owner global context of this global proxy object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005145 // It is null value if this object is not used by any context.
5146 DECL_ACCESSORS(context, Object)
5147
5148 // Casting.
5149 static inline JSGlobalProxy* cast(Object* obj);
5150
5151 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005152#ifdef OBJECT_PRINT
5153 inline void JSGlobalProxyPrint() {
5154 JSGlobalProxyPrint(stdout);
5155 }
5156 void JSGlobalProxyPrint(FILE* out);
5157#endif
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005158#ifdef DEBUG
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005159 void JSGlobalProxyVerify();
5160#endif
5161
5162 // Layout description.
5163 static const int kContextOffset = JSObject::kHeaderSize;
5164 static const int kSize = kContextOffset + kPointerSize;
5165
5166 private:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005167 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
5168};
5169
5170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005171// Forward declaration.
5172class JSBuiltinsObject;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005173class JSGlobalPropertyCell;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005174
5175// Common super class for JavaScript global objects and the special
5176// builtins global objects.
5177class GlobalObject: public JSObject {
5178 public:
5179 // [builtins]: the object holding the runtime routines written in JS.
5180 DECL_ACCESSORS(builtins, JSBuiltinsObject)
5181
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005182 // [global context]: the global context corresponding to this global object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005183 DECL_ACCESSORS(global_context, Context)
5184
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005185 // [global receiver]: the global receiver object of the context
5186 DECL_ACCESSORS(global_receiver, JSObject)
5187
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005188 // Retrieve the property cell used to store a property.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005189 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005190
lrn@chromium.org303ada72010-10-27 09:33:13 +00005191 // This is like GetProperty, but is used when you know the lookup won't fail
5192 // by throwing an exception. This is for the debug and builtins global
5193 // objects, where it is known which properties can be expected to be present
5194 // on the object.
5195 Object* GetPropertyNoExceptionThrown(String* key) {
5196 Object* answer = GetProperty(key)->ToObjectUnchecked();
5197 return answer;
5198 }
5199
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00005200 // Ensure that the global object has a cell for the given property name.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005201 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00005202
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005203 // Casting.
5204 static inline GlobalObject* cast(Object* obj);
5205
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005206 // Layout description.
5207 static const int kBuiltinsOffset = JSObject::kHeaderSize;
5208 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005209 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
5210 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005211
5212 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005213 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
mads.s.ager31e71382008-08-13 09:32:07 +00005214
5215 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005216};
5217
5218
5219// JavaScript global object.
5220class JSGlobalObject: public GlobalObject {
5221 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005222 // Casting.
5223 static inline JSGlobalObject* cast(Object* obj);
5224
5225 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005226#ifdef OBJECT_PRINT
5227 inline void JSGlobalObjectPrint() {
5228 JSGlobalObjectPrint(stdout);
5229 }
5230 void JSGlobalObjectPrint(FILE* out);
5231#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005232#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005233 void JSGlobalObjectVerify();
5234#endif
5235
5236 // Layout description.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005237 static const int kSize = GlobalObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005238
5239 private:
5240 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
5241};
5242
5243
5244// Builtins global object which holds the runtime routines written in
5245// JavaScript.
5246class JSBuiltinsObject: public GlobalObject {
5247 public:
5248 // Accessors for the runtime routines written in JavaScript.
5249 inline Object* javascript_builtin(Builtins::JavaScript id);
5250 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
5251
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005252 // Accessors for code of the runtime routines written in JavaScript.
5253 inline Code* javascript_builtin_code(Builtins::JavaScript id);
5254 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
5255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005256 // Casting.
5257 static inline JSBuiltinsObject* cast(Object* obj);
5258
5259 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005260#ifdef OBJECT_PRINT
5261 inline void JSBuiltinsObjectPrint() {
5262 JSBuiltinsObjectPrint(stdout);
5263 }
5264 void JSBuiltinsObjectPrint(FILE* out);
5265#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005266#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005267 void JSBuiltinsObjectVerify();
5268#endif
5269
5270 // Layout description. The size of the builtins object includes
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005271 // room for two pointers per runtime routine written in javascript
5272 // (function and code object).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005273 static const int kJSBuiltinsCount = Builtins::id_count;
5274 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005275 static const int kJSBuiltinsCodeOffset =
5276 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005277 static const int kSize =
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005278 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
5279
5280 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
5281 return kJSBuiltinsOffset + id * kPointerSize;
5282 }
5283
5284 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
5285 return kJSBuiltinsCodeOffset + id * kPointerSize;
5286 }
5287
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005288 private:
5289 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
5290};
5291
5292
5293// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
5294class JSValue: public JSObject {
5295 public:
5296 // [value]: the object being wrapped.
5297 DECL_ACCESSORS(value, Object)
5298
5299 // Casting.
5300 static inline JSValue* cast(Object* obj);
5301
5302 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005303#ifdef OBJECT_PRINT
5304 inline void JSValuePrint() {
5305 JSValuePrint(stdout);
5306 }
5307 void JSValuePrint(FILE* out);
5308#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005309#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005310 void JSValueVerify();
5311#endif
5312
5313 // Layout description.
5314 static const int kValueOffset = JSObject::kHeaderSize;
5315 static const int kSize = kValueOffset + kPointerSize;
5316
5317 private:
5318 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
5319};
5320
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00005321
5322// Representation of message objects used for error reporting through
5323// the API. The messages are formatted in JavaScript so this object is
5324// a real JavaScript object. The information used for formatting the
5325// error messages are not directly accessible from JavaScript to
5326// prevent leaking information to user code called during error
5327// formatting.
5328class JSMessageObject: public JSObject {
5329 public:
5330 // [type]: the type of error message.
5331 DECL_ACCESSORS(type, String)
5332
5333 // [arguments]: the arguments for formatting the error message.
5334 DECL_ACCESSORS(arguments, JSArray)
5335
5336 // [script]: the script from which the error message originated.
5337 DECL_ACCESSORS(script, Object)
5338
5339 // [stack_trace]: the stack trace for this error message.
5340 DECL_ACCESSORS(stack_trace, Object)
5341
5342 // [stack_frames]: an array of stack frames for this error object.
5343 DECL_ACCESSORS(stack_frames, Object)
5344
5345 // [start_position]: the start position in the script for the error message.
5346 inline int start_position();
5347 inline void set_start_position(int value);
5348
5349 // [end_position]: the end position in the script for the error message.
5350 inline int end_position();
5351 inline void set_end_position(int value);
5352
5353 // Casting.
5354 static inline JSMessageObject* cast(Object* obj);
5355
5356 // Dispatched behavior.
5357#ifdef OBJECT_PRINT
5358 inline void JSMessageObjectPrint() {
5359 JSMessageObjectPrint(stdout);
5360 }
5361 void JSMessageObjectPrint(FILE* out);
5362#endif
5363#ifdef DEBUG
5364 void JSMessageObjectVerify();
5365#endif
5366
5367 // Layout description.
5368 static const int kTypeOffset = JSObject::kHeaderSize;
5369 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
5370 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
5371 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
5372 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
5373 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
5374 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
5375 static const int kSize = kEndPositionOffset + kPointerSize;
5376
5377 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
5378 kStackFramesOffset + kPointerSize,
5379 kSize> BodyDescriptor;
5380};
5381
5382
ager@chromium.org236ad962008-09-25 09:45:57 +00005383// Regular expressions
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005384// The regular expression holds a single reference to a FixedArray in
5385// the kDataOffset field.
5386// The FixedArray contains the following data:
5387// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
5388// - reference to the original source string
5389// - reference to the original flag string
5390// If it is an atom regexp
5391// - a reference to a literal string to search for
5392// If it is an irregexp regexp:
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00005393// - a reference to code for ASCII inputs (bytecode or compiled), or a smi
5394// used for tracking the last usage (used for code flushing).
5395// - a reference to code for UC16 inputs (bytecode or compiled), or a smi
5396// used for tracking the last usage (used for code flushing)..
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005397// - max number of registers used by irregexp implementations.
5398// - number of capture registers (output values) of the regexp.
ager@chromium.org236ad962008-09-25 09:45:57 +00005399class JSRegExp: public JSObject {
5400 public:
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005401 // Meaning of Type:
5402 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005403 // ATOM: A simple string to match against using an indexOf operation.
5404 // IRREGEXP: Compiled with Irregexp.
5405 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
ager@chromium.org381abbb2009-02-25 13:23:22 +00005406 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005407 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
ager@chromium.org236ad962008-09-25 09:45:57 +00005408
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005409 class Flags {
5410 public:
5411 explicit Flags(uint32_t value) : value_(value) { }
5412 bool is_global() { return (value_ & GLOBAL) != 0; }
5413 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
5414 bool is_multiline() { return (value_ & MULTILINE) != 0; }
5415 uint32_t value() { return value_; }
5416 private:
5417 uint32_t value_;
5418 };
ager@chromium.org236ad962008-09-25 09:45:57 +00005419
ager@chromium.org236ad962008-09-25 09:45:57 +00005420 DECL_ACCESSORS(data, Object)
5421
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005422 inline Type TypeTag();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005423 inline int CaptureCount();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005424 inline Flags GetFlags();
5425 inline String* Pattern();
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005426 inline Object* DataAt(int index);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005427 // Set implementation data after the object has been prepared.
5428 inline void SetDataAt(int index, Object* value);
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00005429
5430 // Used during GC when flushing code or setting age.
5431 inline Object* DataAtUnchecked(int index);
5432 inline void SetDataAtUnchecked(int index, Object* value, Heap* heap);
5433 inline Type TypeTagUnchecked();
5434
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005435 static int code_index(bool is_ascii) {
5436 if (is_ascii) {
5437 return kIrregexpASCIICodeIndex;
5438 } else {
5439 return kIrregexpUC16CodeIndex;
5440 }
5441 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005442
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00005443 static int saved_code_index(bool is_ascii) {
5444 if (is_ascii) {
5445 return kIrregexpASCIICodeSavedIndex;
5446 } else {
5447 return kIrregexpUC16CodeSavedIndex;
5448 }
5449 }
5450
ager@chromium.org236ad962008-09-25 09:45:57 +00005451 static inline JSRegExp* cast(Object* obj);
5452
5453 // Dispatched behavior.
5454#ifdef DEBUG
ager@chromium.org236ad962008-09-25 09:45:57 +00005455 void JSRegExpVerify();
5456#endif
5457
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005458 static const int kDataOffset = JSObject::kHeaderSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005459 static const int kSize = kDataOffset + kPointerSize;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005460
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005461 // Indices in the data array.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005462 static const int kTagIndex = 0;
5463 static const int kSourceIndex = kTagIndex + 1;
5464 static const int kFlagsIndex = kSourceIndex + 1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005465 static const int kDataIndex = kFlagsIndex + 1;
5466 // The data fields are used in different ways depending on the
5467 // value of the tag.
5468 // Atom regexps (literal strings).
5469 static const int kAtomPatternIndex = kDataIndex;
5470
5471 static const int kAtomDataSize = kAtomPatternIndex + 1;
5472
ager@chromium.orga1645e22009-09-09 19:27:10 +00005473 // Irregexp compiled code or bytecode for ASCII. If compilation
5474 // fails, this fields hold an exception object that should be
5475 // thrown if the regexp is used again.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005476 static const int kIrregexpASCIICodeIndex = kDataIndex;
ager@chromium.orga1645e22009-09-09 19:27:10 +00005477 // Irregexp compiled code or bytecode for UC16. If compilation
5478 // fails, this fields hold an exception object that should be
5479 // thrown if the regexp is used again.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005480 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00005481
5482 // Saved instance of Irregexp compiled code or bytecode for ASCII that
5483 // is a potential candidate for flushing.
5484 static const int kIrregexpASCIICodeSavedIndex = kDataIndex + 2;
5485 // Saved instance of Irregexp compiled code or bytecode for UC16 that is
5486 // a potential candidate for flushing.
5487 static const int kIrregexpUC16CodeSavedIndex = kDataIndex + 3;
5488
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005489 // Maximal number of registers used by either ASCII or UC16.
5490 // Only used to check that there is enough stack space
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00005491 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 4;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005492 // Number of captures in the compiled regexp.
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00005493 static const int kIrregexpCaptureCountIndex = kDataIndex + 5;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005494
5495 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005496
5497 // Offsets directly into the data fixed array.
5498 static const int kDataTagOffset =
5499 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5500 static const int kDataAsciiCodeOffset =
5501 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005502 static const int kDataUC16CodeOffset =
5503 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005504 static const int kIrregexpCaptureCountOffset =
5505 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
lrn@chromium.org25156de2010-04-06 13:10:27 +00005506
5507 // In-object fields.
5508 static const int kSourceFieldIndex = 0;
5509 static const int kGlobalFieldIndex = 1;
5510 static const int kIgnoreCaseFieldIndex = 2;
5511 static const int kMultilineFieldIndex = 3;
5512 static const int kLastIndexFieldIndex = 4;
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005513 static const int kInObjectFieldCount = 5;
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00005514
5515 // The uninitialized value for a regexp code object.
5516 static const int kUninitializedValue = -1;
5517
5518 // The compilation error value for the regexp code object. The real error
5519 // object is in the saved code field.
5520 static const int kCompilationErrorValue = -2;
5521
5522 // When we store the sweep generation at which we moved the code from the
5523 // code index to the saved code index we mask it of to be in the [0:255]
5524 // range.
5525 static const int kCodeAgeMask = 0xff;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005526};
5527
5528
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005529class CompilationCacheShape {
5530 public:
5531 static inline bool IsMatch(HashTableKey* key, Object* value) {
5532 return key->IsMatch(value);
5533 }
5534
5535 static inline uint32_t Hash(HashTableKey* key) {
5536 return key->Hash();
5537 }
5538
5539 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5540 return key->HashForObject(object);
5541 }
5542
lrn@chromium.org303ada72010-10-27 09:33:13 +00005543 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005544 return key->AsObject();
5545 }
5546
5547 static const int kPrefixSize = 0;
5548 static const int kEntrySize = 2;
5549};
5550
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005551
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005552class CompilationCacheTable: public HashTable<CompilationCacheShape,
5553 HashTableKey*> {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005554 public:
5555 // Find cached value for a string key, otherwise return null.
5556 Object* Lookup(String* src);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00005557 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005558 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
lrn@chromium.org303ada72010-10-27 09:33:13 +00005559 MaybeObject* Put(String* src, Object* value);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00005560 MaybeObject* PutEval(String* src,
5561 Context* context,
5562 SharedFunctionInfo* value);
lrn@chromium.org303ada72010-10-27 09:33:13 +00005563 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005564
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005565 // Remove given value from cache.
5566 void Remove(Object* value);
5567
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005568 static inline CompilationCacheTable* cast(Object* obj);
5569
5570 private:
5571 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
ager@chromium.org236ad962008-09-25 09:45:57 +00005572};
5573
5574
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005575class CodeCache: public Struct {
5576 public:
5577 DECL_ACCESSORS(default_cache, FixedArray)
5578 DECL_ACCESSORS(normal_type_cache, Object)
5579
5580 // Add the code object to the cache.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005581 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005582
5583 // Lookup code object in the cache. Returns code object if found and undefined
5584 // if not.
5585 Object* Lookup(String* name, Code::Flags flags);
5586
5587 // Get the internal index of a code object in the cache. Returns -1 if the
5588 // code object is not in that cache. This index can be used to later call
5589 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5590 // RemoveByIndex.
sgjesse@chromium.org99a37fa2010-03-11 09:23:46 +00005591 int GetIndex(Object* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005592
5593 // Remove an object from the cache with the provided internal index.
sgjesse@chromium.org99a37fa2010-03-11 09:23:46 +00005594 void RemoveByIndex(Object* name, Code* code, int index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005595
5596 static inline CodeCache* cast(Object* obj);
5597
whesse@chromium.org023421e2010-12-21 12:19:12 +00005598#ifdef OBJECT_PRINT
5599 inline void CodeCachePrint() {
5600 CodeCachePrint(stdout);
5601 }
5602 void CodeCachePrint(FILE* out);
5603#endif
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005604#ifdef DEBUG
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005605 void CodeCacheVerify();
5606#endif
5607
5608 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5609 static const int kNormalTypeCacheOffset =
5610 kDefaultCacheOffset + kPointerSize;
5611 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5612
5613 private:
lrn@chromium.org303ada72010-10-27 09:33:13 +00005614 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5615 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005616 Object* LookupDefaultCache(String* name, Code::Flags flags);
5617 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5618
5619 // Code cache layout of the default cache. Elements are alternating name and
5620 // code objects for non normal load/store/call IC's.
5621 static const int kCodeCacheEntrySize = 2;
5622 static const int kCodeCacheEntryNameOffset = 0;
5623 static const int kCodeCacheEntryCodeOffset = 1;
5624
5625 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5626};
5627
5628
5629class CodeCacheHashTableShape {
5630 public:
5631 static inline bool IsMatch(HashTableKey* key, Object* value) {
5632 return key->IsMatch(value);
5633 }
5634
5635 static inline uint32_t Hash(HashTableKey* key) {
5636 return key->Hash();
5637 }
5638
5639 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5640 return key->HashForObject(object);
5641 }
5642
lrn@chromium.org303ada72010-10-27 09:33:13 +00005643 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005644 return key->AsObject();
5645 }
5646
5647 static const int kPrefixSize = 0;
5648 static const int kEntrySize = 2;
5649};
5650
5651
5652class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5653 HashTableKey*> {
5654 public:
5655 Object* Lookup(String* name, Code::Flags flags);
lrn@chromium.org303ada72010-10-27 09:33:13 +00005656 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005657
5658 int GetIndex(String* name, Code::Flags flags);
5659 void RemoveByIndex(int index);
5660
5661 static inline CodeCacheHashTable* cast(Object* obj);
5662
5663 // Initial size of the fixed array backing the hash table.
5664 static const int kInitialSize = 64;
5665
5666 private:
5667 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5668};
5669
5670
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00005671class PolymorphicCodeCache: public Struct {
5672 public:
5673 DECL_ACCESSORS(cache, Object)
5674
5675 MUST_USE_RESULT MaybeObject* Update(MapList* maps,
5676 Code::Flags flags,
5677 Code* code);
5678 Object* Lookup(MapList* maps, Code::Flags flags);
5679
5680 static inline PolymorphicCodeCache* cast(Object* obj);
5681
5682#ifdef OBJECT_PRINT
5683 inline void PolymorphicCodeCachePrint() {
5684 PolymorphicCodeCachePrint(stdout);
5685 }
5686 void PolymorphicCodeCachePrint(FILE* out);
5687#endif
5688#ifdef DEBUG
5689 void PolymorphicCodeCacheVerify();
5690#endif
5691
5692 static const int kCacheOffset = HeapObject::kHeaderSize;
5693 static const int kSize = kCacheOffset + kPointerSize;
5694
5695 private:
5696 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCache);
5697};
5698
5699
5700class PolymorphicCodeCacheHashTable
5701 : public HashTable<CodeCacheHashTableShape, HashTableKey*> {
5702 public:
5703 Object* Lookup(MapList* maps, int code_kind);
5704 MUST_USE_RESULT MaybeObject* Put(MapList* maps, int code_kind, Code* code);
5705
5706 static inline PolymorphicCodeCacheHashTable* cast(Object* obj);
5707
5708 static const int kInitialSize = 64;
5709 private:
5710 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCacheHashTable);
5711};
5712
5713
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005714enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5715enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5716
5717
ager@chromium.org7c537e22008-10-16 08:43:32 +00005718class StringHasher {
5719 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00005720 explicit inline StringHasher(int length);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005721
5722 // Returns true if the hash of this string can be computed without
5723 // looking at the contents.
5724 inline bool has_trivial_hash();
5725
5726 // Add a character to the hash and update the array index calculation.
5727 inline void AddCharacter(uc32 c);
5728
5729 // Adds a character to the hash but does not update the array index
5730 // calculation. This can only be called when it has been verified
5731 // that the input is not an array index.
5732 inline void AddCharacterNoIndex(uc32 c);
5733
5734 // Returns the value to store in the hash field of a string with
5735 // the given length and contents.
5736 uint32_t GetHashField();
5737
5738 // Returns true if the characters seen so far make up a legal array
5739 // index.
5740 bool is_array_index() { return is_array_index_; }
5741
5742 bool is_valid() { return is_valid_; }
5743
5744 void invalidate() { is_valid_ = false; }
5745
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00005746 // Calculated hash value for a string consisting of 1 to
5747 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5748 // value is represented decimal value.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005749 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00005750
ager@chromium.org7c537e22008-10-16 08:43:32 +00005751 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00005752 uint32_t array_index() {
5753 ASSERT(is_array_index());
5754 return array_index_;
5755 }
5756
5757 inline uint32_t GetHash();
5758
5759 int length_;
5760 uint32_t raw_running_hash_;
5761 uint32_t array_index_;
5762 bool is_array_index_;
5763 bool is_first_char_;
5764 bool is_valid_;
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005765 friend class TwoCharHashTableKey;
ager@chromium.org7c537e22008-10-16 08:43:32 +00005766};
5767
5768
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005769// Calculates string hash.
5770template <typename schar>
5771inline uint32_t HashSequentialString(const schar* chars, int length);
5772
5773
ager@chromium.org870a0b62008-11-04 11:43:05 +00005774// The characteristics of a string are stored in its map. Retrieving these
5775// few bits of information is moderately expensive, involving two memory
5776// loads where the second is dependent on the first. To improve efficiency
5777// the shape of the string is given its own class so that it can be retrieved
5778// once and used for several string operations. A StringShape is small enough
5779// to be passed by value and is immutable, but be aware that flattening a
ager@chromium.orgc3e50d82008-11-05 11:53:10 +00005780// string can potentially alter its shape. Also be aware that a GC caused by
5781// something else can alter the shape of a string due to ConsString
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005782// shortcutting. Keeping these restrictions in mind has proven to be error-
5783// prone and so we no longer put StringShapes in variables unless there is a
5784// concrete performance benefit at that particular point in the code.
ager@chromium.org870a0b62008-11-04 11:43:05 +00005785class StringShape BASE_EMBEDDED {
5786 public:
5787 inline explicit StringShape(String* s);
5788 inline explicit StringShape(Map* s);
5789 inline explicit StringShape(InstanceType t);
ager@chromium.org870a0b62008-11-04 11:43:05 +00005790 inline bool IsSequential();
5791 inline bool IsExternal();
5792 inline bool IsCons();
ager@chromium.org870a0b62008-11-04 11:43:05 +00005793 inline bool IsExternalAscii();
5794 inline bool IsExternalTwoByte();
5795 inline bool IsSequentialAscii();
5796 inline bool IsSequentialTwoByte();
5797 inline bool IsSymbol();
5798 inline StringRepresentationTag representation_tag();
5799 inline uint32_t full_representation_tag();
5800 inline uint32_t size_tag();
5801#ifdef DEBUG
5802 inline uint32_t type() { return type_; }
5803 inline void invalidate() { valid_ = false; }
5804 inline bool valid() { return valid_; }
5805#else
5806 inline void invalidate() { }
5807#endif
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00005808
ager@chromium.org870a0b62008-11-04 11:43:05 +00005809 private:
5810 uint32_t type_;
5811#ifdef DEBUG
5812 inline void set_valid() { valid_ = true; }
5813 bool valid_;
5814#else
5815 inline void set_valid() { }
5816#endif
5817};
5818
5819
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005820// The String abstract class captures JavaScript string values:
5821//
5822// Ecma-262:
5823// 4.3.16 String Value
5824// A string value is a member of the type String and is a finite
5825// ordered sequence of zero or more 16-bit unsigned integer values.
5826//
5827// All string values have a length field.
5828class String: public HeapObject {
5829 public:
5830 // Get and set the length of the string.
5831 inline int length();
5832 inline void set_length(int value);
5833
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005834 // Get and set the hash field of the string.
5835 inline uint32_t hash_field();
5836 inline void set_hash_field(uint32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005837
ager@chromium.org5ec48922009-05-05 07:25:34 +00005838 inline bool IsAsciiRepresentation();
5839 inline bool IsTwoByteRepresentation();
5840
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005841 // Returns whether this string has ascii chars, i.e. all of them can
5842 // be ascii encoded. This might be the case even if the string is
5843 // two-byte. Such strings may appear when the embedder prefers
5844 // two-byte external representations even for ascii data.
ricow@chromium.orgaa1b6162010-03-29 07:44:58 +00005845 //
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005846 // NOTE: this should be considered only a hint. False negatives are
5847 // possible.
5848 inline bool HasOnlyAsciiChars();
ricow@chromium.orgaa1b6162010-03-29 07:44:58 +00005849
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005850 // Get and set individual two byte chars in the string.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005851 inline void Set(int index, uint16_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005852 // Get individual two byte char in the string. Repeated calls
5853 // to this method are not efficient unless the string is flat.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005854 inline uint16_t Get(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005855
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005856 // Try to flatten the string. Checks first inline to see if it is
5857 // necessary. Does nothing if the string is not a cons string.
5858 // Flattening allocates a sequential string with the same data as
5859 // the given string and mutates the cons string to a degenerate
5860 // form, where the first component is the new sequential string and
5861 // the second component is the empty string. If allocation fails,
5862 // this function returns a failure. If flattening succeeds, this
5863 // function returns the sequential string that is now the first
5864 // component of the cons string.
5865 //
5866 // Degenerate cons strings are handled specially by the garbage
5867 // collector (see IsShortcutCandidate).
5868 //
5869 // Use FlattenString from Handles.cc to flatten even in case an
5870 // allocation failure happens.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005871 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005872
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005873 // Convenience function. Has exactly the same behavior as
5874 // TryFlatten(), except in the case of failure returns the original
5875 // string.
5876 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5877
ager@chromium.org7c537e22008-10-16 08:43:32 +00005878 Vector<const char> ToAsciiVector();
5879 Vector<const uc16> ToUC16Vector();
5880
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005881 // Mark the string as an undetectable object. It only applies to
5882 // ascii and two byte string types.
5883 bool MarkAsUndetectable();
5884
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005885 // Return a substring.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005886 MUST_USE_RESULT MaybeObject* SubString(int from,
5887 int to,
5888 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005889
5890 // String equality operations.
5891 inline bool Equals(String* other);
5892 bool IsEqualTo(Vector<const char> str);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00005893 bool IsAsciiEqualTo(Vector<const char> str);
5894 bool IsTwoByteEqualTo(Vector<const uc16> str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005895
5896 // Return a UTF8 representation of the string. The string is null
5897 // terminated but may optionally contain nulls. Length is returned
5898 // in length_output if length_output is not a null pointer The string
5899 // should be nearly flat, otherwise the performance of this method may
5900 // be very slow (quadratic in the length). Setting robustness_flag to
5901 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5902 // handles unexpected data without causing assert failures and it does not
5903 // do any heap allocations. This is useful when printing stack traces.
5904 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5905 RobustnessFlag robustness_flag,
5906 int offset,
5907 int length,
5908 int* length_output = 0);
5909 SmartPointer<char> ToCString(
5910 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5911 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5912 int* length_output = 0);
5913
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005914 int Utf8Length();
5915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005916 // Return a 16 bit Unicode representation of the string.
5917 // The string should be nearly flat, otherwise the performance of
5918 // of this method may be very bad. Setting robustness_flag to
5919 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5920 // handles unexpected data without causing assert failures and it does not
5921 // do any heap allocations. This is useful when printing stack traces.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005922 SmartPointer<uc16> ToWideCString(
5923 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005924
5925 // Tells whether the hash code has been computed.
5926 inline bool HasHashCode();
5927
5928 // Returns a hash value used for the property table
5929 inline uint32_t Hash();
5930
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005931 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5932 int length);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005933
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005934 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5935 uint32_t* index,
5936 int length);
5937
ager@chromium.org6f10e412009-02-13 10:11:16 +00005938 // Externalization.
5939 bool MakeExternal(v8::String::ExternalStringResource* resource);
5940 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005942 // Conversion.
5943 inline bool AsArrayIndex(uint32_t* index);
5944
5945 // Casting.
5946 static inline String* cast(Object* obj);
5947
5948 void PrintOn(FILE* out);
5949
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005950 // For use during stack traces. Performs rudimentary sanity check.
5951 bool LooksValid();
5952
5953 // Dispatched behavior.
5954 void StringShortPrint(StringStream* accumulator);
whesse@chromium.org023421e2010-12-21 12:19:12 +00005955#ifdef OBJECT_PRINT
5956 inline void StringPrint() {
5957 StringPrint(stdout);
5958 }
5959 void StringPrint(FILE* out);
vegorov@chromium.org7943d462011-08-01 11:41:52 +00005960
5961 char* ToAsciiArray();
whesse@chromium.org023421e2010-12-21 12:19:12 +00005962#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005963#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005964 void StringVerify();
5965#endif
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005966 inline bool IsFlat();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005967
5968 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00005969 static const int kLengthOffset = HeapObject::kHeaderSize;
ager@chromium.orgac091b72010-05-05 07:34:42 +00005970 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005971 static const int kSize = kHashFieldOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005972
ager@chromium.org3811b432009-10-28 14:53:37 +00005973 // Maximum number of characters to consider when trying to convert a string
5974 // value into an array index.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005975 static const int kMaxArrayIndexSize = 10;
5976
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005977 // Max ascii char code.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005978 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
ager@chromium.org381abbb2009-02-25 13:23:22 +00005979 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
ager@chromium.org8bb60582008-12-11 12:02:20 +00005980 static const int kMaxUC16CharCode = 0xffff;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005981
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005982 // Minimum length for a cons string.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005983 static const int kMinNonFlatLength = 13;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005984
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005985 // Mask constant for checking if a string has a computed hash code
5986 // and if it is an array index. The least significant bit indicates
5987 // whether a hash code has been computed. If the hash code has been
5988 // computed the 2nd bit tells whether the string can be used as an
5989 // array index.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005990 static const int kHashNotComputedMask = 1;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005991 static const int kIsNotArrayIndexMask = 1 << 1;
5992 static const int kNofHashBitFields = 2;
ager@chromium.org7c537e22008-10-16 08:43:32 +00005993
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005994 // Shift constant retrieving hash code from hash field.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005995 static const int kHashShift = kNofHashBitFields;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005996
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005997 // Array index strings this short can keep their index in the hash
5998 // field.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005999 static const int kMaxCachedArrayIndexLength = 7;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006000
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006001 // For strings which are array indexes the hash value has the string length
6002 // mixed into the hash, mainly to avoid a hash value of zero which would be
6003 // the case for the string '0'. 24 bits are used for the array index value.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006004 static const int kArrayIndexValueBits = 24;
6005 static const int kArrayIndexLengthBits =
6006 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
6007
6008 STATIC_CHECK((kArrayIndexLengthBits > 0));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00006009 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006010
6011 static const int kArrayIndexHashLengthShift =
6012 kArrayIndexValueBits + kNofHashBitFields;
6013
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006014 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006015
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006016 static const int kArrayIndexValueMask =
6017 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
ager@chromium.org3811b432009-10-28 14:53:37 +00006018
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006019 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
6020 // could use a mask to test if the length of string is less than or equal to
6021 // kMaxCachedArrayIndexLength.
6022 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
6023
6024 static const int kContainsCachedArrayIndexMask =
6025 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
6026 kIsNotArrayIndexMask;
6027
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006028 // Value of empty hash field indicating that the hash is not computed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006029 static const int kEmptyHashField =
6030 kIsNotArrayIndexMask | kHashNotComputedMask;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006031
6032 // Value of hash field containing computed hash equal to zero.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00006033 static const int kZeroHash = kIsNotArrayIndexMask;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006034
6035 // Maximal string length.
6036 static const int kMaxLength = (1 << (32 - 2)) - 1;
6037
6038 // Max length for computing hash. For strings longer than this limit the
6039 // string length is used as the hash value.
6040 static const int kMaxHashCalcLength = 16383;
ager@chromium.org7c537e22008-10-16 08:43:32 +00006041
kasper.lund7276f142008-07-30 08:49:36 +00006042 // Limit for truncation in short printing.
6043 static const int kMaxShortPrintLength = 1024;
6044
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006045 // Support for regular expressions.
6046 const uc16* GetTwoByteData();
6047 const uc16* GetTwoByteData(unsigned start);
6048
6049 // Support for StringInputBuffer
6050 static const unibrow::byte* ReadBlock(String* input,
6051 unibrow::byte* util_buffer,
6052 unsigned capacity,
6053 unsigned* remaining,
6054 unsigned* offset);
6055 static const unibrow::byte* ReadBlock(String** input,
6056 unibrow::byte* util_buffer,
6057 unsigned capacity,
6058 unsigned* remaining,
6059 unsigned* offset);
6060
6061 // Helper function for flattening strings.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006062 template <typename sinkchar>
6063 static void WriteToFlat(String* source,
6064 sinkchar* sink,
6065 int from,
6066 int to);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006067
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00006068 static inline bool IsAscii(const char* chars, int length) {
6069 const char* limit = chars + length;
6070#ifdef V8_HOST_CAN_READ_UNALIGNED
6071 ASSERT(kMaxAsciiCharCode == 0x7F);
6072 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
6073 while (chars <= limit - sizeof(uintptr_t)) {
6074 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
6075 return false;
6076 }
6077 chars += sizeof(uintptr_t);
6078 }
6079#endif
6080 while (chars < limit) {
6081 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
6082 ++chars;
6083 }
6084 return true;
6085 }
6086
6087 static inline bool IsAscii(const uc16* chars, int length) {
6088 const uc16* limit = chars + length;
6089 while (chars < limit) {
6090 if (*chars > kMaxAsciiCharCodeU) return false;
6091 ++chars;
6092 }
6093 return true;
6094 }
6095
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006096 protected:
6097 class ReadBlockBuffer {
6098 public:
6099 ReadBlockBuffer(unibrow::byte* util_buffer_,
6100 unsigned cursor_,
6101 unsigned capacity_,
6102 unsigned remaining_) :
6103 util_buffer(util_buffer_),
6104 cursor(cursor_),
6105 capacity(capacity_),
6106 remaining(remaining_) {
6107 }
6108 unibrow::byte* util_buffer;
6109 unsigned cursor;
6110 unsigned capacity;
6111 unsigned remaining;
6112 };
6113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006114 static inline const unibrow::byte* ReadBlock(String* input,
6115 ReadBlockBuffer* buffer,
6116 unsigned* offset,
6117 unsigned max_chars);
6118 static void ReadBlockIntoBuffer(String* input,
6119 ReadBlockBuffer* buffer,
6120 unsigned* offset_ptr,
6121 unsigned max_chars);
6122
6123 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006124 // Try to flatten the top level ConsString that is hiding behind this
6125 // string. This is a no-op unless the string is a ConsString. Flatten
6126 // mutates the ConsString and might return a failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +00006127 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00006128
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006129 static inline bool IsHashFieldComputed(uint32_t field);
6130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006131 // Slow case of String::Equals. This implementation works on any strings
6132 // but it is most efficient on strings that are almost flat.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006133 bool SlowEquals(String* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006134
6135 // Slow case of AsArrayIndex.
6136 bool SlowAsArrayIndex(uint32_t* index);
6137
6138 // Compute and set the hash code.
6139 uint32_t ComputeAndSetHash();
6140
6141 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
6142};
6143
6144
6145// The SeqString abstract class captures sequential string values.
6146class SeqString: public String {
6147 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006148 // Casting.
6149 static inline SeqString* cast(Object* obj);
6150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006151 private:
6152 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
6153};
6154
6155
6156// The AsciiString class captures sequential ascii string objects.
6157// Each character in the AsciiString is an ascii character.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006158class SeqAsciiString: public SeqString {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006159 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00006160 static const bool kHasAsciiEncoding = true;
6161
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006162 // Dispatched behavior.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006163 inline uint16_t SeqAsciiStringGet(int index);
6164 inline void SeqAsciiStringSet(int index, uint16_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006165
6166 // Get the address of the characters in this string.
6167 inline Address GetCharsAddress();
6168
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006169 inline char* GetChars();
6170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006171 // Casting
ager@chromium.org7c537e22008-10-16 08:43:32 +00006172 static inline SeqAsciiString* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006173
6174 // Garbage collection support. This method is called by the
6175 // garbage collector to compute the actual size of an AsciiString
6176 // instance.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006177 inline int SeqAsciiStringSize(InstanceType instance_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006178
6179 // Computes the size for an AsciiString instance of a given length.
6180 static int SizeFor(int length) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006181 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006182 }
6183
6184 // Layout description.
6185 static const int kHeaderSize = String::kSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006186 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006187
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006188 // Maximal memory usage for a single sequential ASCII string.
6189 static const int kMaxSize = 512 * MB;
6190 // Maximal length of a single sequential ASCII string.
6191 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6192 static const int kMaxLength = (kMaxSize - kHeaderSize);
6193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006194 // Support for StringInputBuffer.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006195 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6196 unsigned* offset,
6197 unsigned chars);
6198 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
6199 unsigned* offset,
6200 unsigned chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006201
6202 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00006203 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006204};
6205
6206
6207// The TwoByteString class captures sequential unicode string objects.
6208// Each character in the TwoByteString is a two-byte uint16_t.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006209class SeqTwoByteString: public SeqString {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006210 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00006211 static const bool kHasAsciiEncoding = false;
6212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006213 // Dispatched behavior.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006214 inline uint16_t SeqTwoByteStringGet(int index);
6215 inline void SeqTwoByteStringSet(int index, uint16_t value);
6216
6217 // Get the address of the characters in this string.
6218 inline Address GetCharsAddress();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006219
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006220 inline uc16* GetChars();
6221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006222 // For regexp code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006223 const uint16_t* SeqTwoByteStringGetData(unsigned start);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006224
6225 // Casting
ager@chromium.org7c537e22008-10-16 08:43:32 +00006226 static inline SeqTwoByteString* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006227
6228 // Garbage collection support. This method is called by the
6229 // garbage collector to compute the actual size of a TwoByteString
6230 // instance.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006231 inline int SeqTwoByteStringSize(InstanceType instance_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006232
6233 // Computes the size for a TwoByteString instance of a given length.
6234 static int SizeFor(int length) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006235 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006236 }
6237
6238 // Layout description.
6239 static const int kHeaderSize = String::kSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006240 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006241
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006242 // Maximal memory usage for a single sequential two-byte string.
6243 static const int kMaxSize = 512 * MB;
6244 // Maximal length of a single sequential two-byte string.
6245 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6246 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
6247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006248 // Support for StringInputBuffer.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006249 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6250 unsigned* offset_ptr,
6251 unsigned chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006252
6253 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00006254 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006255};
6256
6257
6258// The ConsString class describes string values built by using the
6259// addition operator on strings. A ConsString is a pair where the
6260// first and second components are pointers to other string values.
6261// One or both components of a ConsString can be pointers to other
6262// ConsStrings, creating a binary tree of ConsStrings where the leaves
6263// are non-ConsString string values. The string value represented by
6264// a ConsString can be obtained by concatenating the leaf string
6265// values in a left-to-right depth-first traversal of the tree.
6266class ConsString: public String {
6267 public:
ager@chromium.org870a0b62008-11-04 11:43:05 +00006268 // First string of the cons cell.
6269 inline String* first();
6270 // Doesn't check that the result is a string, even in debug mode. This is
6271 // useful during GC where the mark bits confuse the checks.
6272 inline Object* unchecked_first();
6273 inline void set_first(String* first,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00006274 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006275
ager@chromium.org870a0b62008-11-04 11:43:05 +00006276 // Second string of the cons cell.
6277 inline String* second();
6278 // Doesn't check that the result is a string, even in debug mode. This is
6279 // useful during GC where the mark bits confuse the checks.
6280 inline Object* unchecked_second();
6281 inline void set_second(String* second,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00006282 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006283
6284 // Dispatched behavior.
6285 uint16_t ConsStringGet(int index);
6286
6287 // Casting.
6288 static inline ConsString* cast(Object* obj);
6289
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006290 // Layout description.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006291 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006292 static const int kSecondOffset = kFirstOffset + kPointerSize;
6293 static const int kSize = kSecondOffset + kPointerSize;
6294
6295 // Support for StringInputBuffer.
6296 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
6297 unsigned* offset_ptr,
6298 unsigned chars);
6299 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6300 unsigned* offset_ptr,
6301 unsigned chars);
6302
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006303 // Minimum length for a cons string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006304 static const int kMinLength = 13;
6305
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006306 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
6307 BodyDescriptor;
6308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006309 private:
6310 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
6311};
6312
6313
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006314// The ExternalString class describes string values that are backed by
6315// a string resource that lies outside the V8 heap. ExternalStrings
6316// consist of the length field common to all strings, a pointer to the
6317// external resource. It is important to ensure (externally) that the
6318// resource is not deallocated while the ExternalString is live in the
6319// V8 heap.
6320//
6321// The API expects that all ExternalStrings are created through the
6322// API. Therefore, ExternalStrings should not be used internally.
6323class ExternalString: public String {
6324 public:
6325 // Casting
6326 static inline ExternalString* cast(Object* obj);
6327
6328 // Layout description.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006329 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006330 static const int kSize = kResourceOffset + kPointerSize;
6331
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006332 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
6333
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006334 private:
6335 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
6336};
6337
6338
6339// The ExternalAsciiString class is an external string backed by an
6340// ASCII string.
6341class ExternalAsciiString: public ExternalString {
6342 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00006343 static const bool kHasAsciiEncoding = true;
6344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006345 typedef v8::String::ExternalAsciiStringResource Resource;
6346
6347 // The underlying resource.
6348 inline Resource* resource();
6349 inline void set_resource(Resource* buffer);
6350
6351 // Dispatched behavior.
6352 uint16_t ExternalAsciiStringGet(int index);
6353
6354 // Casting.
6355 static inline ExternalAsciiString* cast(Object* obj);
6356
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006357 // Garbage collection support.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006358 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
6359
6360 template<typename StaticVisitor>
6361 inline void ExternalAsciiStringIterateBody();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006362
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006363 // Support for StringInputBuffer.
6364 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
6365 unsigned* offset,
6366 unsigned chars);
6367 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6368 unsigned* offset,
6369 unsigned chars);
6370
6371 private:
6372 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
6373};
6374
6375
6376// The ExternalTwoByteString class is an external string backed by a UTF-16
6377// encoded string.
6378class ExternalTwoByteString: public ExternalString {
6379 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00006380 static const bool kHasAsciiEncoding = false;
6381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006382 typedef v8::String::ExternalStringResource Resource;
6383
6384 // The underlying string resource.
6385 inline Resource* resource();
6386 inline void set_resource(Resource* buffer);
6387
6388 // Dispatched behavior.
6389 uint16_t ExternalTwoByteStringGet(int index);
6390
6391 // For regexp code.
6392 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
6393
6394 // Casting.
6395 static inline ExternalTwoByteString* cast(Object* obj);
6396
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006397 // Garbage collection support.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006398 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
6399
6400 template<typename StaticVisitor>
6401 inline void ExternalTwoByteStringIterateBody();
6402
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006403
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006404 // Support for StringInputBuffer.
6405 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6406 unsigned* offset_ptr,
6407 unsigned chars);
6408
6409 private:
6410 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
6411};
6412
6413
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006414// Utility superclass for stack-allocated objects that must be updated
6415// on gc. It provides two ways for the gc to update instances, either
6416// iterating or updating after gc.
6417class Relocatable BASE_EMBEDDED {
6418 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006419 explicit inline Relocatable(Isolate* isolate);
6420 inline virtual ~Relocatable();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006421 virtual void IterateInstance(ObjectVisitor* v) { }
6422 virtual void PostGarbageCollection() { }
6423
6424 static void PostGarbageCollectionProcessing();
6425 static int ArchiveSpacePerThread();
lrn@chromium.org1c092762011-05-09 09:42:16 +00006426 static char* ArchiveState(Isolate* isolate, char* to);
6427 static char* RestoreState(Isolate* isolate, char* from);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006428 static void Iterate(ObjectVisitor* v);
6429 static void Iterate(ObjectVisitor* v, Relocatable* top);
6430 static char* Iterate(ObjectVisitor* v, char* t);
6431 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006432 Isolate* isolate_;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006433 Relocatable* prev_;
6434};
6435
6436
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006437// A flat string reader provides random access to the contents of a
6438// string independent of the character width of the string. The handle
6439// must be valid as long as the reader is being used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006440class FlatStringReader : public Relocatable {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006441 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006442 FlatStringReader(Isolate* isolate, Handle<String> str);
6443 FlatStringReader(Isolate* isolate, Vector<const char> input);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006444 void PostGarbageCollection();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006445 inline uc32 Get(int index);
6446 int length() { return length_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006447 private:
6448 String** str_;
6449 bool is_ascii_;
6450 int length_;
6451 const void* start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006452};
6453
6454
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006455// Note that StringInputBuffers are not valid across a GC! To fix this
6456// it would have to store a String Handle instead of a String* and
6457// AsciiStringReadBlock would have to be modified to use memcpy.
6458//
6459// StringInputBuffer is able to traverse any string regardless of how
6460// deeply nested a sequence of ConsStrings it is made of. However,
6461// performance will be better if deep strings are flattened before they
6462// are traversed. Since flattening requires memory allocation this is
6463// not always desirable, however (esp. in debugging situations).
6464class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
6465 public:
6466 virtual void Seek(unsigned pos);
6467 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00006468 explicit inline StringInputBuffer(String* backing):
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006469 unibrow::InputBuffer<String, String*, 1024>(backing) {}
6470};
6471
6472
6473class SafeStringInputBuffer
6474 : public unibrow::InputBuffer<String, String**, 256> {
6475 public:
6476 virtual void Seek(unsigned pos);
6477 inline SafeStringInputBuffer()
6478 : unibrow::InputBuffer<String, String**, 256>() {}
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00006479 explicit inline SafeStringInputBuffer(String** backing)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006480 : unibrow::InputBuffer<String, String**, 256>(backing) {}
6481};
6482
6483
ager@chromium.org7c537e22008-10-16 08:43:32 +00006484template <typename T>
6485class VectorIterator {
6486 public:
6487 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
6488 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
6489 T GetNext() { return data_[index_++]; }
6490 bool has_more() { return index_ < data_.length(); }
6491 private:
6492 Vector<const T> data_;
6493 int index_;
6494};
6495
6496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006497// The Oddball describes objects null, undefined, true, and false.
6498class Oddball: public HeapObject {
6499 public:
6500 // [to_string]: Cached to_string computed at startup.
6501 DECL_ACCESSORS(to_string, String)
6502
6503 // [to_number]: Cached to_number computed at startup.
6504 DECL_ACCESSORS(to_number, Object)
6505
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006506 inline byte kind();
6507 inline void set_kind(byte kind);
6508
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006509 // Casting.
6510 static inline Oddball* cast(Object* obj);
6511
6512 // Dispatched behavior.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006513#ifdef DEBUG
6514 void OddballVerify();
6515#endif
6516
6517 // Initialize the fields.
lrn@chromium.org303ada72010-10-27 09:33:13 +00006518 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006519 Object* to_number,
6520 byte kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006521
6522 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00006523 static const int kToStringOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006524 static const int kToNumberOffset = kToStringOffset + kPointerSize;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006525 static const int kKindOffset = kToNumberOffset + kPointerSize;
6526 static const int kSize = kKindOffset + kPointerSize;
6527
6528 static const byte kFalse = 0;
6529 static const byte kTrue = 1;
6530 static const byte kNotBooleanMask = ~1;
6531 static const byte kTheHole = 2;
6532 static const byte kNull = 3;
6533 static const byte kArgumentMarker = 4;
6534 static const byte kUndefined = 5;
6535 static const byte kOther = 6;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006536
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006537 typedef FixedBodyDescriptor<kToStringOffset,
6538 kToNumberOffset + kPointerSize,
6539 kSize> BodyDescriptor;
6540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006541 private:
6542 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6543};
6544
6545
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006546class JSGlobalPropertyCell: public HeapObject {
6547 public:
6548 // [value]: value of the global property.
6549 DECL_ACCESSORS(value, Object)
6550
6551 // Casting.
6552 static inline JSGlobalPropertyCell* cast(Object* obj);
6553
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006554#ifdef DEBUG
6555 void JSGlobalPropertyCellVerify();
whesse@chromium.org023421e2010-12-21 12:19:12 +00006556#endif
6557#ifdef OBJECT_PRINT
6558 inline void JSGlobalPropertyCellPrint() {
6559 JSGlobalPropertyCellPrint(stdout);
6560 }
6561 void JSGlobalPropertyCellPrint(FILE* out);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006562#endif
6563
6564 // Layout description.
6565 static const int kValueOffset = HeapObject::kHeaderSize;
6566 static const int kSize = kValueOffset + kPointerSize;
6567
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006568 typedef FixedBodyDescriptor<kValueOffset,
6569 kValueOffset + kPointerSize,
6570 kSize> BodyDescriptor;
6571
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00006572 // Returns the isolate/heap this cell object belongs to.
6573 inline Isolate* isolate();
6574 inline Heap* heap();
6575
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006576 private:
6577 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6578};
6579
6580
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006581// The JSProxy describes EcmaScript Harmony proxies
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006582class JSProxy: public JSReceiver {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006583 public:
6584 // [handler]: The handler property.
6585 DECL_ACCESSORS(handler, Object)
6586
rossberg@chromium.org717967f2011-07-20 13:44:42 +00006587 // [padding]: The padding slot (unused, see below).
6588 DECL_ACCESSORS(padding, Object)
6589
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006590 // Casting.
6591 static inline JSProxy* cast(Object* obj);
6592
rossberg@chromium.org717967f2011-07-20 13:44:42 +00006593 bool HasPropertyWithHandler(String* name);
6594
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006595 MUST_USE_RESULT MaybeObject* SetPropertyWithHandler(
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006596 String* name,
6597 Object* value,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006598 PropertyAttributes attributes,
6599 StrictModeFlag strict_mode);
6600
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006601 MUST_USE_RESULT MaybeObject* DeletePropertyWithHandler(
6602 String* name,
6603 DeleteMode mode);
6604
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006605 MUST_USE_RESULT PropertyAttributes GetPropertyAttributeWithHandler(
6606 JSReceiver* receiver,
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00006607 String* name,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006608 bool* has_exception);
6609
rossberg@chromium.org717967f2011-07-20 13:44:42 +00006610 // Turn this into an (empty) JSObject.
6611 void Fix();
6612
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006613 // Dispatched behavior.
6614#ifdef OBJECT_PRINT
6615 inline void JSProxyPrint() {
6616 JSProxyPrint(stdout);
6617 }
6618 void JSProxyPrint(FILE* out);
6619#endif
6620#ifdef DEBUG
6621 void JSProxyVerify();
6622#endif
6623
rossberg@chromium.org717967f2011-07-20 13:44:42 +00006624 // Layout description. We add padding so that a proxy has the same
6625 // size as a virgin JSObject. This is essential for becoming a JSObject
6626 // upon freeze.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006627 static const int kHandlerOffset = HeapObject::kHeaderSize;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00006628 static const int kPaddingOffset = kHandlerOffset + kPointerSize;
6629 static const int kSize = kPaddingOffset + kPointerSize;
6630
6631 STATIC_CHECK(kSize == JSObject::kHeaderSize);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006632
6633 typedef FixedBodyDescriptor<kHandlerOffset,
6634 kHandlerOffset + kPointerSize,
6635 kSize> BodyDescriptor;
6636
6637 private:
6638 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxy);
6639};
6640
6641
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006642// TODO(rossberg): Only a stub for now.
6643class JSFunctionProxy: public JSProxy {
6644 public:
6645 // Casting.
6646 static inline JSFunctionProxy* cast(Object* obj);
6647
6648 private:
6649 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunctionProxy);
6650};
6651
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006652
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +00006653// The JSWeakMap describes EcmaScript Harmony weak maps
6654class JSWeakMap: public JSObject {
6655 public:
6656 // [table]: the backing hash table mapping keys to values.
6657 DECL_ACCESSORS(table, ObjectHashTable)
6658
6659 // [next]: linked list of encountered weak maps during GC.
6660 DECL_ACCESSORS(next, Object)
6661
6662 // Unchecked accessors to be used during GC.
6663 inline ObjectHashTable* unchecked_table();
6664
6665 // Casting.
6666 static inline JSWeakMap* cast(Object* obj);
6667
6668#ifdef OBJECT_PRINT
6669 inline void JSWeakMapPrint() {
6670 JSWeakMapPrint(stdout);
6671 }
6672 void JSWeakMapPrint(FILE* out);
6673#endif
6674#ifdef DEBUG
6675 void JSWeakMapVerify();
6676#endif
6677
6678 static const int kTableOffset = JSObject::kHeaderSize;
6679 static const int kNextOffset = kTableOffset + kPointerSize;
6680 static const int kSize = kNextOffset + kPointerSize;
6681
6682 private:
6683 DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakMap);
6684};
6685
6686
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006687// Foreign describes objects pointing from JavaScript to C structures.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006688// Since they cannot contain references to JS HeapObjects they can be
6689// placed in old_data_space.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006690class Foreign: public HeapObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006691 public:
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006692 // [address]: field containing the address.
6693 inline Address address();
6694 inline void set_address(Address value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006695
6696 // Casting.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006697 static inline Foreign* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006698
6699 // Dispatched behavior.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006700 inline void ForeignIterateBody(ObjectVisitor* v);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006701
6702 template<typename StaticVisitor>
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006703 inline void ForeignIterateBody();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006704
whesse@chromium.org023421e2010-12-21 12:19:12 +00006705#ifdef OBJECT_PRINT
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006706 inline void ForeignPrint() {
6707 ForeignPrint(stdout);
whesse@chromium.org023421e2010-12-21 12:19:12 +00006708 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006709 void ForeignPrint(FILE* out);
whesse@chromium.org023421e2010-12-21 12:19:12 +00006710#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006711#ifdef DEBUG
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006712 void ForeignVerify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006713#endif
6714
6715 // Layout description.
6716
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006717 static const int kAddressOffset = HeapObject::kHeaderSize;
6718 static const int kSize = kAddressOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006719
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006720 STATIC_CHECK(kAddressOffset == Internals::kForeignAddressOffset);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006721
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006722 private:
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006723 DISALLOW_IMPLICIT_CONSTRUCTORS(Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006724};
6725
6726
6727// The JSArray describes JavaScript Arrays
6728// Such an array can be in one of two modes:
6729// - fast, backing storage is a FixedArray and length <= elements.length();
6730// Please note: push and pop can be used to grow and shrink the array.
6731// - slow, backing storage is a HashTable with numbers as keys.
6732class JSArray: public JSObject {
6733 public:
6734 // [length]: The length property.
6735 DECL_ACCESSORS(length, Object)
6736
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006737 // Overload the length setter to skip write barrier when the length
6738 // is set to a smi. This matches the set function on FixedArray.
6739 inline void set_length(Smi* length);
6740
lrn@chromium.org303ada72010-10-27 09:33:13 +00006741 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6742 Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006743
6744 // Initialize the array with the given capacity. The function may
6745 // fail due to out-of-memory situations, but only if the requested
6746 // capacity is non-zero.
lrn@chromium.org303ada72010-10-27 09:33:13 +00006747 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006748
6749 // Set the content of the array to the content of storage.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006750 inline void SetContent(FixedArray* storage);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006751
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006752 // Casting.
6753 static inline JSArray* cast(Object* obj);
6754
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006755 // Uses handles. Ensures that the fixed array backing the JSArray has at
6756 // least the stated size.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006757 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006759 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00006760#ifdef OBJECT_PRINT
6761 inline void JSArrayPrint() {
6762 JSArrayPrint(stdout);
6763 }
6764 void JSArrayPrint(FILE* out);
6765#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006766#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006767 void JSArrayVerify();
6768#endif
6769
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006770 // Number of element slots to pre-allocate for an empty array.
6771 static const int kPreallocatedArrayElements = 4;
6772
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006773 // Layout description.
6774 static const int kLengthOffset = JSObject::kHeaderSize;
6775 static const int kSize = kLengthOffset + kPointerSize;
6776
6777 private:
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006778 // Expand the fixed array backing of a fast-case JSArray to at least
6779 // the requested size.
6780 void Expand(int minimum_size_of_backing_fixed_array);
6781
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006782 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6783};
6784
6785
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00006786// JSRegExpResult is just a JSArray with a specific initial map.
6787// This initial map adds in-object properties for "index" and "input"
6788// properties, as assigned by RegExp.prototype.exec, which allows
6789// faster creation of RegExp exec results.
6790// This class just holds constants used when creating the result.
6791// After creation the result must be treated as a JSArray in all regards.
6792class JSRegExpResult: public JSArray {
6793 public:
6794 // Offsets of object fields.
6795 static const int kIndexOffset = JSArray::kSize;
6796 static const int kInputOffset = kIndexOffset + kPointerSize;
6797 static const int kSize = kInputOffset + kPointerSize;
6798 // Indices of in-object properties.
6799 static const int kIndexIndex = 0;
6800 static const int kInputIndex = 1;
6801 private:
6802 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6803};
6804
6805
ager@chromium.org32912102009-01-16 10:38:43 +00006806// An accessor must have a getter, but can have no setter.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006807//
6808// When setting a property, V8 searches accessors in prototypes.
6809// If an accessor was found and it does not have a setter,
6810// the request is ignored.
6811//
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006812// If the accessor in the prototype has the READ_ONLY property attribute, then
6813// a new value is added to the local object when the property is set.
6814// This shadows the accessor in the prototype.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006815class AccessorInfo: public Struct {
6816 public:
6817 DECL_ACCESSORS(getter, Object)
6818 DECL_ACCESSORS(setter, Object)
6819 DECL_ACCESSORS(data, Object)
6820 DECL_ACCESSORS(name, Object)
6821 DECL_ACCESSORS(flag, Smi)
6822
6823 inline bool all_can_read();
6824 inline void set_all_can_read(bool value);
6825
6826 inline bool all_can_write();
6827 inline void set_all_can_write(bool value);
6828
ager@chromium.org870a0b62008-11-04 11:43:05 +00006829 inline bool prohibits_overwriting();
6830 inline void set_prohibits_overwriting(bool value);
6831
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006832 inline PropertyAttributes property_attributes();
6833 inline void set_property_attributes(PropertyAttributes attributes);
6834
6835 static inline AccessorInfo* cast(Object* obj);
6836
whesse@chromium.org023421e2010-12-21 12:19:12 +00006837#ifdef OBJECT_PRINT
6838 inline void AccessorInfoPrint() {
6839 AccessorInfoPrint(stdout);
6840 }
6841 void AccessorInfoPrint(FILE* out);
6842#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006843#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006844 void AccessorInfoVerify();
6845#endif
6846
ager@chromium.org236ad962008-09-25 09:45:57 +00006847 static const int kGetterOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006848 static const int kSetterOffset = kGetterOffset + kPointerSize;
6849 static const int kDataOffset = kSetterOffset + kPointerSize;
6850 static const int kNameOffset = kDataOffset + kPointerSize;
6851 static const int kFlagOffset = kNameOffset + kPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00006852 static const int kSize = kFlagOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006853
6854 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006855 // Bit positions in flag.
ager@chromium.org870a0b62008-11-04 11:43:05 +00006856 static const int kAllCanReadBit = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006857 static const int kAllCanWriteBit = 1;
ager@chromium.org870a0b62008-11-04 11:43:05 +00006858 static const int kProhibitsOverwritingBit = 2;
6859 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
mads.s.ager31e71382008-08-13 09:32:07 +00006860
6861 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006862};
6863
6864
6865class AccessCheckInfo: public Struct {
6866 public:
6867 DECL_ACCESSORS(named_callback, Object)
6868 DECL_ACCESSORS(indexed_callback, Object)
6869 DECL_ACCESSORS(data, Object)
6870
6871 static inline AccessCheckInfo* cast(Object* obj);
6872
whesse@chromium.org023421e2010-12-21 12:19:12 +00006873#ifdef OBJECT_PRINT
6874 inline void AccessCheckInfoPrint() {
6875 AccessCheckInfoPrint(stdout);
6876 }
6877 void AccessCheckInfoPrint(FILE* out);
6878#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006879#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006880 void AccessCheckInfoVerify();
6881#endif
6882
ager@chromium.org236ad962008-09-25 09:45:57 +00006883 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006884 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
6885 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
6886 static const int kSize = kDataOffset + kPointerSize;
6887
6888 private:
6889 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
6890};
6891
6892
6893class InterceptorInfo: public Struct {
6894 public:
6895 DECL_ACCESSORS(getter, Object)
6896 DECL_ACCESSORS(setter, Object)
6897 DECL_ACCESSORS(query, Object)
6898 DECL_ACCESSORS(deleter, Object)
6899 DECL_ACCESSORS(enumerator, Object)
6900 DECL_ACCESSORS(data, Object)
6901
6902 static inline InterceptorInfo* cast(Object* obj);
6903
whesse@chromium.org023421e2010-12-21 12:19:12 +00006904#ifdef OBJECT_PRINT
6905 inline void InterceptorInfoPrint() {
6906 InterceptorInfoPrint(stdout);
6907 }
6908 void InterceptorInfoPrint(FILE* out);
6909#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006910#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006911 void InterceptorInfoVerify();
6912#endif
6913
ager@chromium.org236ad962008-09-25 09:45:57 +00006914 static const int kGetterOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006915 static const int kSetterOffset = kGetterOffset + kPointerSize;
6916 static const int kQueryOffset = kSetterOffset + kPointerSize;
6917 static const int kDeleterOffset = kQueryOffset + kPointerSize;
6918 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
6919 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
6920 static const int kSize = kDataOffset + kPointerSize;
6921
6922 private:
6923 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
6924};
6925
6926
6927class CallHandlerInfo: public Struct {
6928 public:
6929 DECL_ACCESSORS(callback, Object)
6930 DECL_ACCESSORS(data, Object)
6931
6932 static inline CallHandlerInfo* cast(Object* obj);
6933
whesse@chromium.org023421e2010-12-21 12:19:12 +00006934#ifdef OBJECT_PRINT
6935 inline void CallHandlerInfoPrint() {
6936 CallHandlerInfoPrint(stdout);
6937 }
6938 void CallHandlerInfoPrint(FILE* out);
6939#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006940#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006941 void CallHandlerInfoVerify();
6942#endif
6943
ager@chromium.org236ad962008-09-25 09:45:57 +00006944 static const int kCallbackOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006945 static const int kDataOffset = kCallbackOffset + kPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00006946 static const int kSize = kDataOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006947
6948 private:
6949 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
6950};
6951
6952
6953class TemplateInfo: public Struct {
6954 public:
6955 DECL_ACCESSORS(tag, Object)
6956 DECL_ACCESSORS(property_list, Object)
6957
6958#ifdef DEBUG
6959 void TemplateInfoVerify();
6960#endif
6961
ager@chromium.org236ad962008-09-25 09:45:57 +00006962 static const int kTagOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006963 static const int kPropertyListOffset = kTagOffset + kPointerSize;
6964 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
6965 protected:
6966 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6967 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6968};
6969
6970
6971class FunctionTemplateInfo: public TemplateInfo {
6972 public:
6973 DECL_ACCESSORS(serial_number, Object)
6974 DECL_ACCESSORS(call_code, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006975 DECL_ACCESSORS(property_accessors, Object)
6976 DECL_ACCESSORS(prototype_template, Object)
6977 DECL_ACCESSORS(parent_template, Object)
6978 DECL_ACCESSORS(named_property_handler, Object)
6979 DECL_ACCESSORS(indexed_property_handler, Object)
6980 DECL_ACCESSORS(instance_template, Object)
6981 DECL_ACCESSORS(class_name, Object)
6982 DECL_ACCESSORS(signature, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006983 DECL_ACCESSORS(instance_call_handler, Object)
6984 DECL_ACCESSORS(access_check_info, Object)
6985 DECL_ACCESSORS(flag, Smi)
6986
6987 // Following properties use flag bits.
6988 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6989 DECL_BOOLEAN_ACCESSORS(undetectable)
6990 // If the bit is set, object instances created by this function
6991 // requires access check.
6992 DECL_BOOLEAN_ACCESSORS(needs_access_check)
ricow@chromium.org2c99e282011-07-28 09:15:17 +00006993 DECL_BOOLEAN_ACCESSORS(read_only_prototype)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006994
6995 static inline FunctionTemplateInfo* cast(Object* obj);
6996
whesse@chromium.org023421e2010-12-21 12:19:12 +00006997#ifdef OBJECT_PRINT
6998 inline void FunctionTemplateInfoPrint() {
6999 FunctionTemplateInfoPrint(stdout);
7000 }
7001 void FunctionTemplateInfoPrint(FILE* out);
7002#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007003#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007004 void FunctionTemplateInfoVerify();
7005#endif
7006
7007 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
7008 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
kasper.lund212ac232008-07-16 07:07:30 +00007009 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007010 static const int kPrototypeTemplateOffset =
7011 kPropertyAccessorsOffset + kPointerSize;
7012 static const int kParentTemplateOffset =
7013 kPrototypeTemplateOffset + kPointerSize;
7014 static const int kNamedPropertyHandlerOffset =
7015 kParentTemplateOffset + kPointerSize;
7016 static const int kIndexedPropertyHandlerOffset =
7017 kNamedPropertyHandlerOffset + kPointerSize;
7018 static const int kInstanceTemplateOffset =
7019 kIndexedPropertyHandlerOffset + kPointerSize;
7020 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
7021 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
v8.team.kasperl727e9952008-09-02 14:56:44 +00007022 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007023 static const int kAccessCheckInfoOffset =
7024 kInstanceCallHandlerOffset + kPointerSize;
7025 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
ricow@chromium.org2c99e282011-07-28 09:15:17 +00007026 static const int kSize = kFlagOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007027
7028 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007029 // Bit position in the flag, from least significant bit position.
7030 static const int kHiddenPrototypeBit = 0;
7031 static const int kUndetectableBit = 1;
7032 static const int kNeedsAccessCheckBit = 2;
ricow@chromium.org2c99e282011-07-28 09:15:17 +00007033 static const int kReadOnlyPrototypeBit = 3;
mads.s.ager31e71382008-08-13 09:32:07 +00007034
7035 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007036};
7037
7038
7039class ObjectTemplateInfo: public TemplateInfo {
7040 public:
7041 DECL_ACCESSORS(constructor, Object)
kasper.lund212ac232008-07-16 07:07:30 +00007042 DECL_ACCESSORS(internal_field_count, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007043
7044 static inline ObjectTemplateInfo* cast(Object* obj);
7045
whesse@chromium.org023421e2010-12-21 12:19:12 +00007046#ifdef OBJECT_PRINT
7047 inline void ObjectTemplateInfoPrint() {
7048 ObjectTemplateInfoPrint(stdout);
7049 }
7050 void ObjectTemplateInfoPrint(FILE* out);
7051#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007052#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007053 void ObjectTemplateInfoVerify();
7054#endif
7055
7056 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
kasper.lund212ac232008-07-16 07:07:30 +00007057 static const int kInternalFieldCountOffset =
7058 kConstructorOffset + kPointerSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00007059 static const int kSize = kInternalFieldCountOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007060};
7061
7062
7063class SignatureInfo: public Struct {
7064 public:
7065 DECL_ACCESSORS(receiver, Object)
7066 DECL_ACCESSORS(args, Object)
7067
7068 static inline SignatureInfo* cast(Object* obj);
7069
whesse@chromium.org023421e2010-12-21 12:19:12 +00007070#ifdef OBJECT_PRINT
7071 inline void SignatureInfoPrint() {
7072 SignatureInfoPrint(stdout);
7073 }
7074 void SignatureInfoPrint(FILE* out);
7075#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007076#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007077 void SignatureInfoVerify();
7078#endif
7079
ager@chromium.org236ad962008-09-25 09:45:57 +00007080 static const int kReceiverOffset = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007081 static const int kArgsOffset = kReceiverOffset + kPointerSize;
7082 static const int kSize = kArgsOffset + kPointerSize;
7083
7084 private:
7085 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
7086};
7087
7088
7089class TypeSwitchInfo: public Struct {
7090 public:
7091 DECL_ACCESSORS(types, Object)
7092
7093 static inline TypeSwitchInfo* cast(Object* obj);
7094
whesse@chromium.org023421e2010-12-21 12:19:12 +00007095#ifdef OBJECT_PRINT
7096 inline void TypeSwitchInfoPrint() {
7097 TypeSwitchInfoPrint(stdout);
7098 }
7099 void TypeSwitchInfoPrint(FILE* out);
7100#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007101#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007102 void TypeSwitchInfoVerify();
7103#endif
7104
ager@chromium.org236ad962008-09-25 09:45:57 +00007105 static const int kTypesOffset = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007106 static const int kSize = kTypesOffset + kPointerSize;
7107};
7108
7109
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007110#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org32912102009-01-16 10:38:43 +00007111// The DebugInfo class holds additional information for a function being
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007112// debugged.
7113class DebugInfo: public Struct {
7114 public:
ager@chromium.org32912102009-01-16 10:38:43 +00007115 // The shared function info for the source being debugged.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007116 DECL_ACCESSORS(shared, SharedFunctionInfo)
7117 // Code object for the original code.
7118 DECL_ACCESSORS(original_code, Code)
7119 // Code object for the patched code. This code object is the code object
7120 // currently active for the function.
7121 DECL_ACCESSORS(code, Code)
7122 // Fixed array holding status information for each active break point.
7123 DECL_ACCESSORS(break_points, FixedArray)
7124
7125 // Check if there is a break point at a code position.
7126 bool HasBreakPoint(int code_position);
7127 // Get the break point info object for a code position.
7128 Object* GetBreakPointInfo(int code_position);
7129 // Clear a break point.
7130 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
7131 int code_position,
7132 Handle<Object> break_point_object);
7133 // Set a break point.
7134 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
7135 int source_position, int statement_position,
7136 Handle<Object> break_point_object);
7137 // Get the break point objects for a code position.
7138 Object* GetBreakPointObjects(int code_position);
7139 // Find the break point info holding this break point object.
7140 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
7141 Handle<Object> break_point_object);
7142 // Get the number of break points for this function.
7143 int GetBreakPointCount();
7144
7145 static inline DebugInfo* cast(Object* obj);
7146
whesse@chromium.org023421e2010-12-21 12:19:12 +00007147#ifdef OBJECT_PRINT
7148 inline void DebugInfoPrint() {
7149 DebugInfoPrint(stdout);
7150 }
7151 void DebugInfoPrint(FILE* out);
7152#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007153#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007154 void DebugInfoVerify();
7155#endif
7156
ager@chromium.org236ad962008-09-25 09:45:57 +00007157 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007158 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
7159 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
7160 static const int kActiveBreakPointsCountIndex =
7161 kPatchedCodeIndex + kPointerSize;
7162 static const int kBreakPointsStateIndex =
7163 kActiveBreakPointsCountIndex + kPointerSize;
7164 static const int kSize = kBreakPointsStateIndex + kPointerSize;
7165
7166 private:
7167 static const int kNoBreakPointInfo = -1;
7168
7169 // Lookup the index in the break_points array for a code position.
7170 int GetBreakPointInfoIndex(int code_position);
7171
7172 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
7173};
7174
7175
7176// The BreakPointInfo class holds information for break points set in a
7177// function. The DebugInfo object holds a BreakPointInfo object for each code
7178// position with one or more break points.
7179class BreakPointInfo: public Struct {
7180 public:
7181 // The position in the code for the break point.
7182 DECL_ACCESSORS(code_position, Smi)
7183 // The position in the source for the break position.
7184 DECL_ACCESSORS(source_position, Smi)
7185 // The position in the source for the last statement before this break
7186 // position.
7187 DECL_ACCESSORS(statement_position, Smi)
7188 // List of related JavaScript break points.
7189 DECL_ACCESSORS(break_point_objects, Object)
7190
7191 // Removes a break point.
7192 static void ClearBreakPoint(Handle<BreakPointInfo> info,
7193 Handle<Object> break_point_object);
7194 // Set a break point.
7195 static void SetBreakPoint(Handle<BreakPointInfo> info,
7196 Handle<Object> break_point_object);
7197 // Check if break point info has this break point object.
7198 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
7199 Handle<Object> break_point_object);
7200 // Get the number of break points for this code position.
7201 int GetBreakPointCount();
7202
7203 static inline BreakPointInfo* cast(Object* obj);
7204
whesse@chromium.org023421e2010-12-21 12:19:12 +00007205#ifdef OBJECT_PRINT
7206 inline void BreakPointInfoPrint() {
7207 BreakPointInfoPrint(stdout);
7208 }
7209 void BreakPointInfoPrint(FILE* out);
7210#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007211#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007212 void BreakPointInfoVerify();
7213#endif
7214
ager@chromium.org236ad962008-09-25 09:45:57 +00007215 static const int kCodePositionIndex = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007216 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
7217 static const int kStatementPositionIndex =
7218 kSourcePositionIndex + kPointerSize;
7219 static const int kBreakPointObjectsIndex =
7220 kStatementPositionIndex + kPointerSize;
7221 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
7222
7223 private:
7224 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
7225};
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007226#endif // ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007227
7228
7229#undef DECL_BOOLEAN_ACCESSORS
7230#undef DECL_ACCESSORS
7231
7232
7233// Abstract base class for visiting, and optionally modifying, the
7234// pointers contained in Objects. Used in GC and serialization/deserialization.
7235class ObjectVisitor BASE_EMBEDDED {
7236 public:
7237 virtual ~ObjectVisitor() {}
7238
7239 // Visits a contiguous arrays of pointers in the half-open range
7240 // [start, end). Any or all of the values may be modified on return.
7241 virtual void VisitPointers(Object** start, Object** end) = 0;
7242
7243 // To allow lazy clearing of inline caches the visitor has
7244 // a rich interface for iterating over Code objects..
7245
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007246 // Visits a code target in the instruction stream.
7247 virtual void VisitCodeTarget(RelocInfo* rinfo);
7248
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00007249 // Visits a code entry in a JS function.
7250 virtual void VisitCodeEntry(Address entry_address);
7251
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007252 // Visits a global property cell reference in the instruction stream.
7253 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
7254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007255 // Visits a runtime entry in the instruction stream.
7256 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
7257
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007258 // Visits the resource of an ASCII or two-byte string.
7259 virtual void VisitExternalAsciiString(
7260 v8::String::ExternalAsciiStringResource** resource) {}
7261 virtual void VisitExternalTwoByteString(
7262 v8::String::ExternalStringResource** resource) {}
7263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007264 // Visits a debug call target in the instruction stream.
7265 virtual void VisitDebugTarget(RelocInfo* rinfo);
7266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007267 // Handy shorthand for visiting a single pointer.
7268 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
7269
7270 // Visits a contiguous arrays of external references (references to the C++
7271 // heap) in the half-open range [start, end). Any or all of the values
7272 // may be modified on return.
7273 virtual void VisitExternalReferences(Address* start, Address* end) {}
7274
7275 inline void VisitExternalReference(Address* p) {
7276 VisitExternalReferences(p, p + 1);
7277 }
7278
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00007279 // Visits a handle that has an embedder-assigned class ID.
7280 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
7281
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007282#ifdef DEBUG
7283 // Intended for serialization/deserialization checking: insert, or
7284 // check for the presence of, a tag at this position in the stream.
7285 virtual void Synchronize(const char* tag) {}
ager@chromium.org3811b432009-10-28 14:53:37 +00007286#else
7287 inline void Synchronize(const char* tag) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007288#endif
7289};
7290
7291
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00007292class StructBodyDescriptor : public
7293 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
7294 public:
7295 static inline int SizeOf(Map* map, HeapObject* object) {
7296 return map->instance_size();
7297 }
7298};
7299
7300
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007301// BooleanBit is a helper class for setting and getting a bit in an
7302// integer or Smi.
7303class BooleanBit : public AllStatic {
7304 public:
7305 static inline bool get(Smi* smi, int bit_position) {
7306 return get(smi->value(), bit_position);
7307 }
7308
7309 static inline bool get(int value, int bit_position) {
7310 return (value & (1 << bit_position)) != 0;
7311 }
7312
7313 static inline Smi* set(Smi* smi, int bit_position, bool v) {
7314 return Smi::FromInt(set(smi->value(), bit_position, v));
7315 }
7316
7317 static inline int set(int value, int bit_position, bool v) {
7318 if (v) {
7319 value |= (1 << bit_position);
7320 } else {
7321 value &= ~(1 << bit_position);
7322 }
7323 return value;
7324 }
7325};
7326
7327} } // namespace v8::internal
7328
7329#endif // V8_OBJECTS_H_