blob: 332b2e47674e048e3b482ce9fd5da137f5a7557d [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_OBJECTS_H_
29#define V8_OBJECTS_H_
30
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "builtins.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000033#include "list.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "smart-pointer.h"
35#include "unicode-inl.h"
Steve Block3ce2e202009-11-05 08:53:23 +000036#if V8_TARGET_ARCH_ARM
37#include "arm/constants-arm.h"
Andrei Popescu31002712010-02-23 13:46:05 +000038#elif V8_TARGET_ARCH_MIPS
39#include "mips/constants-mips.h"
Steve Block3ce2e202009-11-05 08:53:23 +000040#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42//
Kristian Monsen50ef84f2010-07-29 15:18:00 +010043// Most object types in the V8 JavaScript are described in this file.
Steve Blocka7e24c12009-10-30 11:49:00 +000044//
45// Inheritance hierarchy:
John Reck59135872010-11-02 12:39:01 -070046// - MaybeObject (an object or a failure)
47// - Failure (immediate for marking failed operation)
Steve Blocka7e24c12009-10-30 11:49:00 +000048// - Object
49// - Smi (immediate small integer)
Steve Blocka7e24c12009-10-30 11:49:00 +000050// - HeapObject (superclass for everything allocated in the heap)
51// - JSObject
52// - JSArray
53// - JSRegExp
54// - JSFunction
55// - GlobalObject
56// - JSGlobalObject
57// - JSBuiltinsObject
58// - JSGlobalProxy
Steve Block1e0659c2011-05-24 12:43:12 +010059// - JSValue
60// - JSMessageObject
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010061// - ByteArray
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010062// - ExternalArray
Steve Block44f0eee2011-05-26 01:26:41 +010063// - ExternalPixelArray
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010064// - ExternalByteArray
65// - ExternalUnsignedByteArray
66// - ExternalShortArray
67// - ExternalUnsignedShortArray
68// - ExternalIntArray
69// - ExternalUnsignedIntArray
70// - ExternalFloatArray
71// - FixedArray
72// - DescriptorArray
73// - HashTable
74// - Dictionary
75// - SymbolTable
76// - CompilationCacheTable
77// - CodeCacheHashTable
78// - MapCache
79// - Context
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010080// - JSFunctionResultCache
Kristian Monsen50ef84f2010-07-29 15:18:00 +010081// - SerializedScopeInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000082// - String
83// - SeqString
84// - SeqAsciiString
85// - SeqTwoByteString
86// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000087// - ExternalString
88// - ExternalAsciiString
89// - ExternalTwoByteString
90// - HeapNumber
91// - Code
92// - Map
93// - Oddball
Ben Murdoch257744e2011-11-30 15:57:28 +000094// - JSProxy
95// - Foreign
Steve Blocka7e24c12009-10-30 11:49:00 +000096// - SharedFunctionInfo
97// - Struct
98// - AccessorInfo
99// - AccessCheckInfo
100// - InterceptorInfo
101// - CallHandlerInfo
102// - TemplateInfo
103// - FunctionTemplateInfo
104// - ObjectTemplateInfo
105// - Script
106// - SignatureInfo
107// - TypeSwitchInfo
108// - DebugInfo
109// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100110// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000111//
112// Formats of Object*:
113// Smi: [31 bit signed int] 0
114// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
115// Failure: [30 bit signed int] 11
116
117// Ecma-262 3rd 8.6.1
118enum PropertyAttributes {
119 NONE = v8::None,
120 READ_ONLY = v8::ReadOnly,
121 DONT_ENUM = v8::DontEnum,
122 DONT_DELETE = v8::DontDelete,
123 ABSENT = 16 // Used in runtime to indicate a property is absent.
124 // ABSENT can never be stored in or returned from a descriptor's attributes
125 // bitfield. It is only used as a return value meaning the attributes of
126 // a non-existent property.
127};
128
129namespace v8 {
130namespace internal {
131
132
133// PropertyDetails captures type and attributes for a property.
134// They are used both in property dictionaries and instance descriptors.
135class PropertyDetails BASE_EMBEDDED {
136 public:
137
138 PropertyDetails(PropertyAttributes attributes,
139 PropertyType type,
140 int index = 0) {
Steve Block44f0eee2011-05-26 01:26:41 +0100141 ASSERT(type != EXTERNAL_ARRAY_TRANSITION);
Steve Blocka7e24c12009-10-30 11:49:00 +0000142 ASSERT(TypeField::is_valid(type));
143 ASSERT(AttributesField::is_valid(attributes));
Steve Block44f0eee2011-05-26 01:26:41 +0100144 ASSERT(StorageField::is_valid(index));
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
146 value_ = TypeField::encode(type)
147 | AttributesField::encode(attributes)
Steve Block44f0eee2011-05-26 01:26:41 +0100148 | StorageField::encode(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
150 ASSERT(type == this->type());
151 ASSERT(attributes == this->attributes());
152 ASSERT(index == this->index());
153 }
154
Steve Block44f0eee2011-05-26 01:26:41 +0100155 PropertyDetails(PropertyAttributes attributes,
156 PropertyType type,
157 ExternalArrayType array_type) {
158 ASSERT(type == EXTERNAL_ARRAY_TRANSITION);
159 ASSERT(TypeField::is_valid(type));
160 ASSERT(AttributesField::is_valid(attributes));
161 ASSERT(StorageField::is_valid(static_cast<int>(array_type)));
162
163 value_ = TypeField::encode(type)
164 | AttributesField::encode(attributes)
165 | StorageField::encode(static_cast<int>(array_type));
166
167 ASSERT(type == this->type());
168 ASSERT(attributes == this->attributes());
169 ASSERT(array_type == this->array_type());
170 }
171
Steve Blocka7e24c12009-10-30 11:49:00 +0000172 // Conversion for storing details as Object*.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100173 explicit inline PropertyDetails(Smi* smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 inline Smi* AsSmi();
175
176 PropertyType type() { return TypeField::decode(value_); }
177
178 bool IsTransition() {
179 PropertyType t = type();
180 ASSERT(t != INTERCEPTOR);
Steve Block44f0eee2011-05-26 01:26:41 +0100181 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
182 t == EXTERNAL_ARRAY_TRANSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000183 }
184
185 bool IsProperty() {
186 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
187 }
188
189 PropertyAttributes attributes() { return AttributesField::decode(value_); }
190
Steve Block44f0eee2011-05-26 01:26:41 +0100191 int index() { return StorageField::decode(value_); }
192
193 ExternalArrayType array_type() {
194 ASSERT(type() == EXTERNAL_ARRAY_TRANSITION);
195 return static_cast<ExternalArrayType>(StorageField::decode(value_));
196 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000197
198 inline PropertyDetails AsDeleted();
199
Steve Block44f0eee2011-05-26 01:26:41 +0100200 static bool IsValidIndex(int index) {
201 return StorageField::is_valid(index);
202 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000203
204 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
205 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
206 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
207 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
208
209 // Bit fields in value_ (type, shift, size). Must be public so the
210 // constants can be embedded in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100211 class TypeField: public BitField<PropertyType, 0, 4> {};
212 class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
213 class DeletedField: public BitField<uint32_t, 7, 1> {};
214 class StorageField: public BitField<uint32_t, 8, 32-8> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000215
216 static const int kInitialIndex = 1;
217 private:
218 uint32_t value_;
219};
220
221
222// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
223enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
224
225
226// PropertyNormalizationMode is used to specify whether to keep
227// inobject properties when normalizing properties of a JSObject.
228enum PropertyNormalizationMode {
229 CLEAR_INOBJECT_PROPERTIES,
230 KEEP_INOBJECT_PROPERTIES
231};
232
233
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100234// NormalizedMapSharingMode is used to specify whether a map may be shared
235// by different objects with normalized properties.
236enum NormalizedMapSharingMode {
237 UNIQUE_NORMALIZED_MAP,
238 SHARED_NORMALIZED_MAP
239};
240
241
Steve Block791712a2010-08-27 10:21:07 +0100242// Instance size sentinel for objects of variable size.
243static const int kVariableSizeSentinel = 0;
244
245
Steve Blocka7e24c12009-10-30 11:49:00 +0000246// All Maps have a field instance_type containing a InstanceType.
247// It describes the type of the instances.
248//
249// As an example, a JavaScript object is a heap object and its map
250// instance_type is JS_OBJECT_TYPE.
251//
252// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000253// mirror their encoding in the instance_type field of the map. The default
254// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
255// encoding is mentioned explicitly in the name. Likewise, the default
256// representation is considered sequential. It is not mentioned in the
257// name. The other representations (eg, CONS, EXTERNAL) are explicitly
258// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
259// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000260//
261// NOTE: The following things are some that depend on the string types having
262// instance_types that are less than those of all other types:
263// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
264// Object::IsString.
265//
266// NOTE: Everything following JS_VALUE_TYPE is considered a
267// JSObject for GC purposes. The first four entries here have typeof
268// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000269#define INSTANCE_TYPE_LIST_ALL(V) \
270 V(SYMBOL_TYPE) \
271 V(ASCII_SYMBOL_TYPE) \
272 V(CONS_SYMBOL_TYPE) \
273 V(CONS_ASCII_SYMBOL_TYPE) \
274 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100275 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000276 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
277 V(STRING_TYPE) \
278 V(ASCII_STRING_TYPE) \
279 V(CONS_STRING_TYPE) \
280 V(CONS_ASCII_STRING_TYPE) \
281 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100282 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000283 V(EXTERNAL_ASCII_STRING_TYPE) \
284 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
285 \
286 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000287 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000288 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100289 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000290 \
291 V(HEAP_NUMBER_TYPE) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000292 V(JS_PROXY_TYPE) \
293 V(FOREIGN_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000294 V(BYTE_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000295 /* Note: the order of these external array */ \
296 /* types is relied upon in */ \
297 /* Object::IsExternalArray(). */ \
298 V(EXTERNAL_BYTE_ARRAY_TYPE) \
299 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
300 V(EXTERNAL_SHORT_ARRAY_TYPE) \
301 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
302 V(EXTERNAL_INT_ARRAY_TYPE) \
303 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
304 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
Steve Block44f0eee2011-05-26 01:26:41 +0100305 V(EXTERNAL_PIXEL_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000306 V(FILLER_TYPE) \
307 \
308 V(ACCESSOR_INFO_TYPE) \
309 V(ACCESS_CHECK_INFO_TYPE) \
310 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000311 V(CALL_HANDLER_INFO_TYPE) \
312 V(FUNCTION_TEMPLATE_INFO_TYPE) \
313 V(OBJECT_TEMPLATE_INFO_TYPE) \
314 V(SIGNATURE_INFO_TYPE) \
315 V(TYPE_SWITCH_INFO_TYPE) \
316 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100317 V(CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000318 \
Iain Merrick75681382010-08-19 15:07:18 +0100319 V(FIXED_ARRAY_TYPE) \
320 V(SHARED_FUNCTION_INFO_TYPE) \
321 \
Steve Block1e0659c2011-05-24 12:43:12 +0100322 V(JS_MESSAGE_OBJECT_TYPE) \
323 \
Steve Blockd0582a62009-12-15 09:54:21 +0000324 V(JS_VALUE_TYPE) \
325 V(JS_OBJECT_TYPE) \
326 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
327 V(JS_GLOBAL_OBJECT_TYPE) \
328 V(JS_BUILTINS_OBJECT_TYPE) \
329 V(JS_GLOBAL_PROXY_TYPE) \
330 V(JS_ARRAY_TYPE) \
331 V(JS_REGEXP_TYPE) \
332 \
333 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000334
335#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000336#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
337 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000338 V(BREAK_POINT_INFO_TYPE)
339#else
340#define INSTANCE_TYPE_LIST_DEBUGGER(V)
341#endif
342
Steve Blockd0582a62009-12-15 09:54:21 +0000343#define INSTANCE_TYPE_LIST(V) \
344 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000345 INSTANCE_TYPE_LIST_DEBUGGER(V)
346
347
348// Since string types are not consecutive, this macro is used to
349// iterate over them.
350#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000351 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100352 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000353 symbol, \
354 Symbol) \
355 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100356 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000357 ascii_symbol, \
358 AsciiSymbol) \
359 V(CONS_SYMBOL_TYPE, \
360 ConsString::kSize, \
361 cons_symbol, \
362 ConsSymbol) \
363 V(CONS_ASCII_SYMBOL_TYPE, \
364 ConsString::kSize, \
365 cons_ascii_symbol, \
366 ConsAsciiSymbol) \
367 V(EXTERNAL_SYMBOL_TYPE, \
368 ExternalTwoByteString::kSize, \
369 external_symbol, \
370 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100371 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
372 ExternalTwoByteString::kSize, \
373 external_symbol_with_ascii_data, \
374 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000375 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
376 ExternalAsciiString::kSize, \
377 external_ascii_symbol, \
378 ExternalAsciiSymbol) \
379 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100380 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000381 string, \
382 String) \
383 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100384 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000385 ascii_string, \
386 AsciiString) \
387 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000388 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000389 cons_string, \
390 ConsString) \
391 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000392 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000393 cons_ascii_string, \
394 ConsAsciiString) \
395 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000396 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000397 external_string, \
398 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100399 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
400 ExternalTwoByteString::kSize, \
401 external_string_with_ascii_data, \
402 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000403 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000404 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000405 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100406 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000407
408// A struct is a simple object a set of object-valued fields. Including an
409// object type in this causes the compiler to generate most of the boilerplate
410// code for the class including allocation and garbage collection routines,
411// casts and predicates. All you need to define is the class, methods and
412// object verification routines. Easy, no?
413//
414// Note that for subtle reasons related to the ordering or numerical values of
415// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
416// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000417#define STRUCT_LIST_ALL(V) \
418 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
419 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
420 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
421 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
422 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
423 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
424 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
425 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100426 V(SCRIPT, Script, script) \
427 V(CODE_CACHE, CodeCache, code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000428
429#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000430#define STRUCT_LIST_DEBUGGER(V) \
431 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000432 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
433#else
434#define STRUCT_LIST_DEBUGGER(V)
435#endif
436
Steve Blockd0582a62009-12-15 09:54:21 +0000437#define STRUCT_LIST(V) \
438 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000439 STRUCT_LIST_DEBUGGER(V)
440
441// We use the full 8 bits of the instance_type field to encode heap object
442// instance types. The high-order bit (bit 7) is set if the object is not a
443// string, and cleared if it is a string.
444const uint32_t kIsNotStringMask = 0x80;
445const uint32_t kStringTag = 0x0;
446const uint32_t kNotStringTag = 0x80;
447
Leon Clarkee46be812010-01-19 14:06:41 +0000448// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
449// There are not enough types that the non-string types (with bit 7 set) can
450// have bit 6 set too.
451const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000452const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000453const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000454
Steve Blocka7e24c12009-10-30 11:49:00 +0000455// If bit 7 is clear then bit 2 indicates whether the string consists of
456// two-byte characters or one-byte characters.
457const uint32_t kStringEncodingMask = 0x4;
458const uint32_t kTwoByteStringTag = 0x0;
459const uint32_t kAsciiStringTag = 0x4;
460
461// If bit 7 is clear, the low-order 2 bits indicate the representation
462// of the string.
463const uint32_t kStringRepresentationMask = 0x03;
464enum StringRepresentationTag {
465 kSeqStringTag = 0x0,
466 kConsStringTag = 0x1,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100467 kExternalStringTag = 0x2
Steve Blocka7e24c12009-10-30 11:49:00 +0000468};
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100469const uint32_t kIsConsStringMask = 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000470
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100471// If bit 7 is clear, then bit 3 indicates whether this two-byte
472// string actually contains ascii data.
473const uint32_t kAsciiDataHintMask = 0x08;
474const uint32_t kAsciiDataHintTag = 0x08;
475
Steve Blocka7e24c12009-10-30 11:49:00 +0000476
477// A ConsString with an empty string as the right side is a candidate
478// for being shortcut by the garbage collector unless it is a
479// symbol. It's not common to have non-flat symbols, so we do not
480// shortcut them thereby avoiding turning symbols into strings. See
481// heap.cc and mark-compact.cc.
482const uint32_t kShortcutTypeMask =
483 kIsNotStringMask |
484 kIsSymbolMask |
485 kStringRepresentationMask;
486const uint32_t kShortcutTypeTag = kConsStringTag;
487
488
489enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000490 // String types.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100491 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000492 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100493 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000494 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100495 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
496 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
497 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000498 EXTERNAL_ASCII_SYMBOL_TYPE =
499 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100500 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000501 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100502 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000503 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100504 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
505 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
506 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Block1e0659c2011-05-24 12:43:12 +0100507 // LAST_STRING_TYPE
Steve Blockd0582a62009-12-15 09:54:21 +0000508 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
509 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000510
Leon Clarkee46be812010-01-19 14:06:41 +0000511 // Objects allocated in their own spaces (never in new space).
512 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000513 CODE_TYPE,
514 ODDBALL_TYPE,
515 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000516
517 // "Data", objects that cannot contain non-map-word pointers to heap
518 // objects.
519 HEAP_NUMBER_TYPE,
Ben Murdoch257744e2011-11-30 15:57:28 +0000520 FOREIGN_TYPE,
521 JS_PROXY_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000522 BYTE_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000523 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000524 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
525 EXTERNAL_SHORT_ARRAY_TYPE,
526 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
527 EXTERNAL_INT_ARRAY_TYPE,
528 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100529 EXTERNAL_FLOAT_ARRAY_TYPE,
Ben Murdoch257744e2011-11-30 15:57:28 +0000530 EXTERNAL_DOUBLE_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100531 EXTERNAL_PIXEL_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
Leon Clarkee46be812010-01-19 14:06:41 +0000532 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000533
Leon Clarkee46be812010-01-19 14:06:41 +0000534 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000535 ACCESSOR_INFO_TYPE,
536 ACCESS_CHECK_INFO_TYPE,
537 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000538 CALL_HANDLER_INFO_TYPE,
539 FUNCTION_TEMPLATE_INFO_TYPE,
540 OBJECT_TEMPLATE_INFO_TYPE,
541 SIGNATURE_INFO_TYPE,
542 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000543 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100544 CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100545 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
546 // is defined. However as include/v8.h contain some of the instance type
547 // constants always having them avoids them getting different numbers
548 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000549 DEBUG_INFO_TYPE,
550 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000551
Leon Clarkee46be812010-01-19 14:06:41 +0000552 FIXED_ARRAY_TYPE,
553 SHARED_FUNCTION_INFO_TYPE,
554
Steve Block1e0659c2011-05-24 12:43:12 +0100555 JS_MESSAGE_OBJECT_TYPE,
556
Leon Clarkee46be812010-01-19 14:06:41 +0000557 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 JS_OBJECT_TYPE,
559 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
560 JS_GLOBAL_OBJECT_TYPE,
561 JS_BUILTINS_OBJECT_TYPE,
562 JS_GLOBAL_PROXY_TYPE,
563 JS_ARRAY_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100564
565 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE, FIRST_FUNCTION_CLASS_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000566
567 JS_FUNCTION_TYPE,
568
569 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000571 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000572 INVALID_TYPE = FIRST_TYPE - 1,
573 FIRST_NONSTRING_TYPE = MAP_TYPE,
574 // Boundaries for testing for an external array.
575 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100576 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000577 // Boundary for promotion to old data space/old pointer space.
578 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 // Boundaries for testing the type is a JavaScript "object". Note that
580 // function objects are not counted as objects, even though they are
581 // implemented as such; only values whose typeof is "object" are included.
582 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100583 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE,
584 // RegExp objects have [[Class]] "function" because they are callable.
585 // All types from this type and above are objects with [[Class]] "function".
586 FIRST_FUNCTION_CLASS_TYPE = JS_REGEXP_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000587};
588
Steve Block44f0eee2011-05-26 01:26:41 +0100589static const int kExternalArrayTypeCount = LAST_EXTERNAL_ARRAY_TYPE -
590 FIRST_EXTERNAL_ARRAY_TYPE + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000591
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100592STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
593STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
Ben Murdoch257744e2011-11-30 15:57:28 +0000594STATIC_CHECK(FOREIGN_TYPE == Internals::kForeignType);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100595
596
Steve Blocka7e24c12009-10-30 11:49:00 +0000597enum CompareResult {
598 LESS = -1,
599 EQUAL = 0,
600 GREATER = 1,
601
602 NOT_EQUAL = GREATER
603};
604
605
606#define DECL_BOOLEAN_ACCESSORS(name) \
607 inline bool name(); \
608 inline void set_##name(bool value); \
609
610
611#define DECL_ACCESSORS(name, type) \
612 inline type* name(); \
613 inline void set_##name(type* value, \
614 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
615
616
617class StringStream;
618class ObjectVisitor;
619
620struct ValueInfo : public Malloced {
621 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
622 InstanceType type;
623 Object* ptr;
624 const char* str;
625 double number;
626};
627
628
629// A template-ized version of the IsXXX functions.
630template <class C> static inline bool Is(Object* obj);
631
Ben Murdoch257744e2011-11-30 15:57:28 +0000632class Failure;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100633
John Reck59135872010-11-02 12:39:01 -0700634class MaybeObject BASE_EMBEDDED {
635 public:
636 inline bool IsFailure();
637 inline bool IsRetryAfterGC();
638 inline bool IsOutOfMemory();
639 inline bool IsException();
640 INLINE(bool IsTheHole());
641 inline bool ToObject(Object** obj) {
642 if (IsFailure()) return false;
643 *obj = reinterpret_cast<Object*>(this);
644 return true;
645 }
Steve Block053d10c2011-06-13 19:13:29 +0100646 inline Failure* ToFailureUnchecked() {
647 ASSERT(IsFailure());
648 return reinterpret_cast<Failure*>(this);
649 }
John Reck59135872010-11-02 12:39:01 -0700650 inline Object* ToObjectUnchecked() {
651 ASSERT(!IsFailure());
652 return reinterpret_cast<Object*>(this);
653 }
654 inline Object* ToObjectChecked() {
655 CHECK(!IsFailure());
656 return reinterpret_cast<Object*>(this);
657 }
658
Steve Block053d10c2011-06-13 19:13:29 +0100659 template<typename T>
660 inline bool To(T** obj) {
661 if (IsFailure()) return false;
662 *obj = T::cast(reinterpret_cast<Object*>(this));
663 return true;
664 }
665
Ben Murdochb0fe1622011-05-05 13:52:32 +0100666#ifdef OBJECT_PRINT
John Reck59135872010-11-02 12:39:01 -0700667 // Prints this object with details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100668 inline void Print() {
669 Print(stdout);
670 };
671 inline void PrintLn() {
672 PrintLn(stdout);
673 }
674 void Print(FILE* out);
675 void PrintLn(FILE* out);
676#endif
677#ifdef DEBUG
John Reck59135872010-11-02 12:39:01 -0700678 // Verifies the object.
679 void Verify();
680#endif
681};
Steve Blocka7e24c12009-10-30 11:49:00 +0000682
Ben Murdochb8e0da22011-05-16 14:20:40 +0100683
684#define OBJECT_TYPE_LIST(V) \
685 V(Smi) \
686 V(HeapObject) \
687 V(Number) \
688
689#define HEAP_OBJECT_TYPE_LIST(V) \
690 V(HeapNumber) \
691 V(String) \
692 V(Symbol) \
693 V(SeqString) \
694 V(ExternalString) \
695 V(ConsString) \
696 V(ExternalTwoByteString) \
697 V(ExternalAsciiString) \
698 V(SeqTwoByteString) \
699 V(SeqAsciiString) \
700 \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100701 V(ExternalArray) \
702 V(ExternalByteArray) \
703 V(ExternalUnsignedByteArray) \
704 V(ExternalShortArray) \
705 V(ExternalUnsignedShortArray) \
706 V(ExternalIntArray) \
707 V(ExternalUnsignedIntArray) \
708 V(ExternalFloatArray) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000709 V(ExternalDoubleArray) \
Steve Block44f0eee2011-05-26 01:26:41 +0100710 V(ExternalPixelArray) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100711 V(ByteArray) \
712 V(JSObject) \
713 V(JSContextExtensionObject) \
714 V(Map) \
715 V(DescriptorArray) \
716 V(DeoptimizationInputData) \
717 V(DeoptimizationOutputData) \
718 V(FixedArray) \
719 V(Context) \
720 V(CatchContext) \
721 V(GlobalContext) \
722 V(JSFunction) \
723 V(Code) \
724 V(Oddball) \
725 V(SharedFunctionInfo) \
726 V(JSValue) \
Steve Block1e0659c2011-05-24 12:43:12 +0100727 V(JSMessageObject) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100728 V(StringWrapper) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000729 V(Foreign) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100730 V(Boolean) \
731 V(JSArray) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000732 V(JSProxy) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100733 V(JSRegExp) \
734 V(HashTable) \
735 V(Dictionary) \
736 V(SymbolTable) \
737 V(JSFunctionResultCache) \
738 V(NormalizedMapCache) \
739 V(CompilationCacheTable) \
740 V(CodeCacheHashTable) \
741 V(MapCache) \
742 V(Primitive) \
743 V(GlobalObject) \
744 V(JSGlobalObject) \
745 V(JSBuiltinsObject) \
746 V(JSGlobalProxy) \
747 V(UndetectableObject) \
748 V(AccessCheckNeeded) \
749 V(JSGlobalPropertyCell) \
750
Steve Blocka7e24c12009-10-30 11:49:00 +0000751// Object is the abstract superclass for all classes in the
752// object hierarchy.
753// Object does not use any virtual functions to avoid the
754// allocation of the C++ vtable.
755// Since Smi and Failure are subclasses of Object no
756// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700757class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000758 public:
759 // Type testing.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100760#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
761 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
762 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
763#undef IS_TYPE_FUNCTION_DECL
Steve Blocka7e24c12009-10-30 11:49:00 +0000764
765 // Returns true if this object is an instance of the specified
766 // function template.
767 inline bool IsInstanceOf(FunctionTemplateInfo* type);
768
769 inline bool IsStruct();
770#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
771 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
772#undef DECLARE_STRUCT_PREDICATE
773
774 // Oddball testing.
775 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000776 INLINE(bool IsNull());
Steve Block44f0eee2011-05-26 01:26:41 +0100777 INLINE(bool IsTheHole()); // Shadows MaybeObject's implementation.
Steve Blocka7e24c12009-10-30 11:49:00 +0000778 INLINE(bool IsTrue());
779 INLINE(bool IsFalse());
Ben Murdoch086aeea2011-05-13 15:57:08 +0100780 inline bool IsArgumentsMarker();
Steve Blocka7e24c12009-10-30 11:49:00 +0000781
782 // Extract the number.
783 inline double Number();
784
785 inline bool HasSpecificClassOf(String* name);
786
John Reck59135872010-11-02 12:39:01 -0700787 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
788 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000789
790 // Convert to a JSObject if needed.
791 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700792 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000793
794 // Converts this to a Smi if possible.
795 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700796 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000797
798 void Lookup(String* name, LookupResult* result);
799
800 // Property access.
John Reck59135872010-11-02 12:39:01 -0700801 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
802 MUST_USE_RESULT inline MaybeObject* GetProperty(
803 String* key,
804 PropertyAttributes* attributes);
805 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
806 Object* receiver,
807 String* key,
808 PropertyAttributes* attributes);
809 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
810 LookupResult* result,
811 String* key,
812 PropertyAttributes* attributes);
813 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
814 Object* structure,
815 String* name,
816 Object* holder);
Ben Murdoch257744e2011-11-30 15:57:28 +0000817 MUST_USE_RESULT MaybeObject* GetPropertyWithHandler(Object* receiver,
818 String* name,
819 Object* handler);
John Reck59135872010-11-02 12:39:01 -0700820 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
821 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000822
John Reck59135872010-11-02 12:39:01 -0700823 inline MaybeObject* GetElement(uint32_t index);
824 // For use when we know that no exception can be thrown.
825 inline Object* GetElementNoExceptionThrown(uint32_t index);
826 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000827
828 // Return the object's prototype (might be Heap::null_value()).
829 Object* GetPrototype();
830
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100831 // Tries to convert an object to an array index. Returns true and sets
832 // the output parameter if it succeeds.
833 inline bool ToArrayIndex(uint32_t* index);
834
Steve Blocka7e24c12009-10-30 11:49:00 +0000835 // Returns true if this is a JSValue containing a string and the index is
836 // < the length of the string. Used to implement [] on strings.
837 inline bool IsStringObjectWithCharacterAt(uint32_t index);
838
839#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000840 // Verify a pointer is a valid object pointer.
841 static void VerifyPointer(Object* p);
842#endif
843
844 // Prints this object without details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100845 inline void ShortPrint() {
846 ShortPrint(stdout);
847 }
848 void ShortPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000849
850 // Prints this object without details to a message accumulator.
851 void ShortPrint(StringStream* accumulator);
852
853 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
854 static Object* cast(Object* value) { return value; }
855
856 // Layout description.
857 static const int kHeaderSize = 0; // Object does not take up any space.
858
859 private:
860 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
861};
862
863
864// Smi represents integer Numbers that can be stored in 31 bits.
865// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000866// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000867// For long smis it has the following format:
868// [32 bit signed int] [31 bits zero padding] 0
869// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000870class Smi: public Object {
871 public:
872 // Returns the integer value.
873 inline int value();
874
875 // Convert a value to a Smi object.
876 static inline Smi* FromInt(int value);
877
878 static inline Smi* FromIntptr(intptr_t value);
879
880 // Returns whether value can be represented in a Smi.
881 static inline bool IsValid(intptr_t value);
882
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 // Casting.
884 static inline Smi* cast(Object* object);
885
886 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100887 inline void SmiPrint() {
888 SmiPrint(stdout);
889 }
890 void SmiPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 void SmiPrint(StringStream* accumulator);
892#ifdef DEBUG
893 void SmiVerify();
894#endif
895
Steve Block3ce2e202009-11-05 08:53:23 +0000896 static const int kMinValue = (-1 << (kSmiValueSize - 1));
897 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000898
899 private:
900 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
901};
902
903
904// Failure is used for reporting out of memory situations and
905// propagating exceptions through the runtime system. Failure objects
906// are transient and cannot occur as part of the object graph.
907//
908// Failures are a single word, encoded as follows:
909// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +0100910// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000911// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000912// 7 6 4 32 10
913//
Steve Blocka7e24c12009-10-30 11:49:00 +0000914//
915// The low two bits, 0-1, are the failure tag, 11. The next two bits,
916// 2-3, are a failure type tag 'tt' with possible values:
917// 00 RETRY_AFTER_GC
918// 01 EXCEPTION
919// 10 INTERNAL_ERROR
920// 11 OUT_OF_MEMORY_EXCEPTION
921//
922// The next three bits, 4-6, are an allocation space tag 'sss'. The
923// allocation space tag is 000 for all failure types except
924// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
925// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +0000926
927// Failure type tag info.
928const int kFailureTypeTagSize = 2;
929const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
930
John Reck59135872010-11-02 12:39:01 -0700931class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000932 public:
933 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
934 enum Type {
935 RETRY_AFTER_GC = 0,
936 EXCEPTION = 1, // Returning this marker tells the real exception
Steve Block44f0eee2011-05-26 01:26:41 +0100937 // is in Isolate::pending_exception.
Steve Blocka7e24c12009-10-30 11:49:00 +0000938 INTERNAL_ERROR = 2,
939 OUT_OF_MEMORY_EXCEPTION = 3
940 };
941
942 inline Type type() const;
943
944 // Returns the space that needs to be collected for RetryAfterGC failures.
945 inline AllocationSpace allocation_space() const;
946
Steve Blocka7e24c12009-10-30 11:49:00 +0000947 inline bool IsInternalError() const;
948 inline bool IsOutOfMemoryException() const;
949
Ben Murdochf87a2032010-10-22 12:50:53 +0100950 static inline Failure* RetryAfterGC(AllocationSpace space);
951 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +0000952 static inline Failure* Exception();
953 static inline Failure* InternalError();
954 static inline Failure* OutOfMemoryException();
955 // Casting.
John Reck59135872010-11-02 12:39:01 -0700956 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000957
958 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100959 inline void FailurePrint() {
960 FailurePrint(stdout);
961 }
962 void FailurePrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000963 void FailurePrint(StringStream* accumulator);
964#ifdef DEBUG
965 void FailureVerify();
966#endif
967
968 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000969 inline intptr_t value() const;
970 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000971
972 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
973};
974
975
976// Heap objects typically have a map pointer in their first word. However,
977// during GC other data (eg, mark bits, forwarding addresses) is sometimes
978// encoded in the first word. The class MapWord is an abstraction of the
979// value in a heap object's first word.
980class MapWord BASE_EMBEDDED {
981 public:
982 // Normal state: the map word contains a map pointer.
983
984 // Create a map word from a map pointer.
985 static inline MapWord FromMap(Map* map);
986
987 // View this map word as a map pointer.
988 inline Map* ToMap();
989
990
991 // Scavenge collection: the map word of live objects in the from space
992 // contains a forwarding address (a heap object pointer in the to space).
993
994 // True if this map word is a forwarding address for a scavenge
995 // collection. Only valid during a scavenge collection (specifically,
996 // when all map words are heap object pointers, ie. not during a full GC).
997 inline bool IsForwardingAddress();
998
999 // Create a map word from a forwarding address.
1000 static inline MapWord FromForwardingAddress(HeapObject* object);
1001
1002 // View this map word as a forwarding address.
1003 inline HeapObject* ToForwardingAddress();
1004
Steve Blocka7e24c12009-10-30 11:49:00 +00001005 // Marking phase of full collection: the map word of live objects is
1006 // marked, and may be marked as overflowed (eg, the object is live, its
1007 // children have not been visited, and it does not fit in the marking
1008 // stack).
1009
1010 // True if this map word's mark bit is set.
1011 inline bool IsMarked();
1012
1013 // Return this map word but with its mark bit set.
1014 inline void SetMark();
1015
1016 // Return this map word but with its mark bit cleared.
1017 inline void ClearMark();
1018
1019 // True if this map word's overflow bit is set.
1020 inline bool IsOverflowed();
1021
1022 // Return this map word but with its overflow bit set.
1023 inline void SetOverflow();
1024
1025 // Return this map word but with its overflow bit cleared.
1026 inline void ClearOverflow();
1027
1028
1029 // Compacting phase of a full compacting collection: the map word of live
1030 // objects contains an encoding of the original map address along with the
1031 // forwarding address (represented as an offset from the first live object
1032 // in the same page as the (old) object address).
1033
1034 // Create a map word from a map address and a forwarding address offset.
1035 static inline MapWord EncodeAddress(Address map_address, int offset);
1036
1037 // Return the map address encoded in this map word.
1038 inline Address DecodeMapAddress(MapSpace* map_space);
1039
1040 // Return the forwarding offset encoded in this map word.
1041 inline int DecodeOffset();
1042
1043
1044 // During serialization: the map word is used to hold an encoded
1045 // address, and possibly a mark bit (set and cleared with SetMark
1046 // and ClearMark).
1047
1048 // Create a map word from an encoded address.
1049 static inline MapWord FromEncodedAddress(Address address);
1050
1051 inline Address ToEncodedAddress();
1052
1053 // Bits used by the marking phase of the garbage collector.
1054 //
1055 // The first word of a heap object is normally a map pointer. The last two
1056 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1057 // mark an object as live and/or overflowed:
1058 // last bit = 0, marked as alive
1059 // second bit = 1, overflowed
1060 // An object is only marked as overflowed when it is marked as live while
1061 // the marking stack is overflowed.
1062 static const int kMarkingBit = 0; // marking bit
1063 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1064 static const int kOverflowBit = 1; // overflow bit
1065 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1066
Leon Clarkee46be812010-01-19 14:06:41 +00001067 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1068 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 // +-----------------+------------------+-----------------+
1070 // |forwarding offset|page offset of map|page index of map|
1071 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +00001072 // ^ ^ ^
1073 // | | |
1074 // | | kMapPageIndexBits
1075 // | kMapPageOffsetBits
1076 // kForwardingOffsetBits
1077 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1078 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1079#ifdef V8_HOST_ARCH_64_BIT
1080 static const int kMapPageIndexBits = 16;
1081#else
1082 // Use all the 32-bits to encode on a 32-bit platform.
1083 static const int kMapPageIndexBits =
1084 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1085#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001086
1087 static const int kMapPageIndexShift = 0;
1088 static const int kMapPageOffsetShift =
1089 kMapPageIndexShift + kMapPageIndexBits;
1090 static const int kForwardingOffsetShift =
1091 kMapPageOffsetShift + kMapPageOffsetBits;
1092
Leon Clarkee46be812010-01-19 14:06:41 +00001093 // Bit masks covering the different parts the encoding.
1094 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001095 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001096 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001097 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001098 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001099 ~(kMapPageIndexMask | kMapPageOffsetMask);
1100
1101 private:
1102 // HeapObject calls the private constructor and directly reads the value.
1103 friend class HeapObject;
1104
1105 explicit MapWord(uintptr_t value) : value_(value) {}
1106
1107 uintptr_t value_;
1108};
1109
1110
1111// HeapObject is the superclass for all classes describing heap allocated
1112// objects.
1113class HeapObject: public Object {
1114 public:
1115 // [map]: Contains a map which contains the object's reflective
1116 // information.
1117 inline Map* map();
1118 inline void set_map(Map* value);
1119
1120 // During garbage collection, the map word of a heap object does not
1121 // necessarily contain a map pointer.
1122 inline MapWord map_word();
1123 inline void set_map_word(MapWord map_word);
1124
Steve Block44f0eee2011-05-26 01:26:41 +01001125 // The Heap the object was allocated in. Used also to access Isolate.
1126 // This method can not be used during GC, it ASSERTs this.
1127 inline Heap* GetHeap();
1128 // Convenience method to get current isolate. This method can be
1129 // accessed only when its result is the same as
1130 // Isolate::Current(), it ASSERTs this. See also comment for GetHeap.
1131 inline Isolate* GetIsolate();
1132
Steve Blocka7e24c12009-10-30 11:49:00 +00001133 // Converts an address to a HeapObject pointer.
1134 static inline HeapObject* FromAddress(Address address);
1135
1136 // Returns the address of this HeapObject.
1137 inline Address address();
1138
1139 // Iterates over pointers contained in the object (including the Map)
1140 void Iterate(ObjectVisitor* v);
1141
1142 // Iterates over all pointers contained in the object except the
1143 // first map pointer. The object type is given in the first
1144 // parameter. This function does not access the map pointer in the
1145 // object, and so is safe to call while the map pointer is modified.
1146 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1147
Steve Blocka7e24c12009-10-30 11:49:00 +00001148 // Returns the heap object's size in bytes
1149 inline int Size();
1150
1151 // Given a heap object's map pointer, returns the heap size in bytes
1152 // Useful when the map pointer field is used for other purposes.
1153 // GC internal.
1154 inline int SizeFromMap(Map* map);
1155
1156 // Support for the marking heap objects during the marking phase of GC.
1157 // True if the object is marked live.
1158 inline bool IsMarked();
1159
1160 // Mutate this object's map pointer to indicate that the object is live.
1161 inline void SetMark();
1162
1163 // Mutate this object's map pointer to remove the indication that the
1164 // object is live (ie, partially restore the map pointer).
1165 inline void ClearMark();
1166
1167 // True if this object is marked as overflowed. Overflowed objects have
1168 // been reached and marked during marking of the heap, but their children
1169 // have not necessarily been marked and they have not been pushed on the
1170 // marking stack.
1171 inline bool IsOverflowed();
1172
1173 // Mutate this object's map pointer to indicate that the object is
1174 // overflowed.
1175 inline void SetOverflow();
1176
1177 // Mutate this object's map pointer to remove the indication that the
1178 // object is overflowed (ie, partially restore the map pointer).
1179 inline void ClearOverflow();
1180
1181 // Returns the field at offset in obj, as a read/write Object* reference.
1182 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001183 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001184 // during marking GC.
1185 static inline Object** RawField(HeapObject* obj, int offset);
1186
1187 // Casting.
1188 static inline HeapObject* cast(Object* obj);
1189
Leon Clarke4515c472010-02-03 11:58:03 +00001190 // Return the write barrier mode for this. Callers of this function
1191 // must be able to present a reference to an AssertNoAllocation
1192 // object as a sign that they are not going to use this function
1193 // from code that allocates and thus invalidates the returned write
1194 // barrier mode.
1195 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001196
1197 // Dispatched behavior.
1198 void HeapObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001199#ifdef OBJECT_PRINT
1200 inline void HeapObjectPrint() {
1201 HeapObjectPrint(stdout);
1202 }
1203 void HeapObjectPrint(FILE* out);
1204#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001205#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001206 void HeapObjectVerify();
1207 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001208 inline void VerifySmiField(int offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001209#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001210
Ben Murdochb0fe1622011-05-05 13:52:32 +01001211#ifdef OBJECT_PRINT
1212 void PrintHeader(FILE* out, const char* id);
1213#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001214
Ben Murdochb0fe1622011-05-05 13:52:32 +01001215#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001216 // Verify a pointer is a valid HeapObject pointer that points to object
1217 // areas in the heap.
1218 static void VerifyHeapPointer(Object* p);
1219#endif
1220
1221 // Layout description.
1222 // First field in a heap object is map.
1223 static const int kMapOffset = Object::kHeaderSize;
1224 static const int kHeaderSize = kMapOffset + kPointerSize;
1225
1226 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1227
1228 protected:
1229 // helpers for calling an ObjectVisitor to iterate over pointers in the
1230 // half-open range [start, end) specified as integer offsets
1231 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1232 // as above, for the single element at "offset"
1233 inline void IteratePointer(ObjectVisitor* v, int offset);
1234
Steve Blocka7e24c12009-10-30 11:49:00 +00001235 private:
1236 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1237};
1238
1239
Iain Merrick75681382010-08-19 15:07:18 +01001240#define SLOT_ADDR(obj, offset) \
1241 reinterpret_cast<Object**>((obj)->address() + offset)
1242
1243// This class describes a body of an object of a fixed size
1244// in which all pointer fields are located in the [start_offset, end_offset)
1245// interval.
1246template<int start_offset, int end_offset, int size>
1247class FixedBodyDescriptor {
1248 public:
1249 static const int kStartOffset = start_offset;
1250 static const int kEndOffset = end_offset;
1251 static const int kSize = size;
1252
1253 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1254
1255 template<typename StaticVisitor>
1256 static inline void IterateBody(HeapObject* obj) {
1257 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1258 SLOT_ADDR(obj, end_offset));
1259 }
1260};
1261
1262
1263// This class describes a body of an object of a variable size
1264// in which all pointer fields are located in the [start_offset, object_size)
1265// interval.
1266template<int start_offset>
1267class FlexibleBodyDescriptor {
1268 public:
1269 static const int kStartOffset = start_offset;
1270
1271 static inline void IterateBody(HeapObject* obj,
1272 int object_size,
1273 ObjectVisitor* v);
1274
1275 template<typename StaticVisitor>
1276 static inline void IterateBody(HeapObject* obj, int object_size) {
1277 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1278 SLOT_ADDR(obj, object_size));
1279 }
1280};
1281
1282#undef SLOT_ADDR
1283
1284
Steve Blocka7e24c12009-10-30 11:49:00 +00001285// The HeapNumber class describes heap allocated numbers that cannot be
1286// represented in a Smi (small integer)
1287class HeapNumber: public HeapObject {
1288 public:
1289 // [value]: number value.
1290 inline double value();
1291 inline void set_value(double value);
1292
1293 // Casting.
1294 static inline HeapNumber* cast(Object* obj);
1295
1296 // Dispatched behavior.
1297 Object* HeapNumberToBoolean();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001298 inline void HeapNumberPrint() {
1299 HeapNumberPrint(stdout);
1300 }
1301 void HeapNumberPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001302 void HeapNumberPrint(StringStream* accumulator);
1303#ifdef DEBUG
1304 void HeapNumberVerify();
1305#endif
1306
Steve Block6ded16b2010-05-10 14:33:55 +01001307 inline int get_exponent();
1308 inline int get_sign();
1309
Steve Blocka7e24c12009-10-30 11:49:00 +00001310 // Layout description.
1311 static const int kValueOffset = HeapObject::kHeaderSize;
1312 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1313 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1314 // little endian apart from non-EABI arm which is little endian with big
1315 // endian floating point word ordering!
Steve Blocka7e24c12009-10-30 11:49:00 +00001316 static const int kMantissaOffset = kValueOffset;
1317 static const int kExponentOffset = kValueOffset + 4;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001318
Steve Blocka7e24c12009-10-30 11:49:00 +00001319 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001320 static const uint32_t kSignMask = 0x80000000u;
1321 static const uint32_t kExponentMask = 0x7ff00000u;
1322 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001323 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001324 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001325 static const int kExponentBias = 1023;
1326 static const int kExponentShift = 20;
1327 static const int kMantissaBitsInTopWord = 20;
1328 static const int kNonMantissaBitsInTopWord = 12;
1329
1330 private:
1331 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1332};
1333
1334
1335// The JSObject describes real heap allocated JavaScript objects with
1336// properties.
1337// Note that the map of JSObject changes during execution to enable inline
1338// caching.
1339class JSObject: public HeapObject {
1340 public:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001341 enum DeleteMode {
1342 NORMAL_DELETION,
1343 STRICT_DELETION,
1344 FORCE_DELETION
1345 };
1346
Steve Blocka7e24c12009-10-30 11:49:00 +00001347 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001348 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001349 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001350 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001351 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001352 EXTERNAL_BYTE_ELEMENTS,
1353 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1354 EXTERNAL_SHORT_ELEMENTS,
1355 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1356 EXTERNAL_INT_ELEMENTS,
1357 EXTERNAL_UNSIGNED_INT_ELEMENTS,
Steve Block44f0eee2011-05-26 01:26:41 +01001358 EXTERNAL_FLOAT_ELEMENTS,
Ben Murdoch257744e2011-11-30 15:57:28 +00001359 EXTERNAL_DOUBLE_ELEMENTS,
Steve Block44f0eee2011-05-26 01:26:41 +01001360 EXTERNAL_PIXEL_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001361 };
1362
1363 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001364 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001365 // slow case.
1366 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1367 inline void initialize_properties();
1368 inline bool HasFastProperties();
1369 inline StringDictionary* property_dictionary(); // Gets slow properties.
1370
1371 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001372 //
1373 // Elements can be in two general modes: fast and slow. Each mode
1374 // corrensponds to a set of object representations of elements that
1375 // have something in common.
1376 //
1377 // In the fast mode elements is a FixedArray and so each element can
1378 // be quickly accessed. This fact is used in the generated code. The
1379 // elements array can have one of the two maps in this mode:
1380 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1381 // arrays). In the latter case the elements array may be shared by a
1382 // few objects and so before writing to any element the array must
1383 // be copied. Use EnsureWritableFastElements in this case.
1384 //
Steve Block44f0eee2011-05-26 01:26:41 +01001385 // In the slow mode elements is either a NumberDictionary or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001386 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001387 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001388 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001389 inline ElementsKind GetElementsKind();
1390 inline bool HasFastElements();
1391 inline bool HasDictionaryElements();
Steve Block44f0eee2011-05-26 01:26:41 +01001392 inline bool HasExternalPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001393 inline bool HasExternalArrayElements();
1394 inline bool HasExternalByteElements();
1395 inline bool HasExternalUnsignedByteElements();
1396 inline bool HasExternalShortElements();
1397 inline bool HasExternalUnsignedShortElements();
1398 inline bool HasExternalIntElements();
1399 inline bool HasExternalUnsignedIntElements();
1400 inline bool HasExternalFloatElements();
Ben Murdoch257744e2011-11-30 15:57:28 +00001401 inline bool HasExternalDoubleElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001402 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001403 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001404 // Requires: this->HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001405 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001406
1407 // Collects elements starting at index 0.
1408 // Undefined values are placed after non-undefined values.
1409 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001410 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001411 // As PrepareElementsForSort, but only on objects where elements is
1412 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001413 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001414
John Reck59135872010-11-02 12:39:01 -07001415 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1416 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001417 PropertyAttributes attributes,
1418 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001419 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1420 String* key,
1421 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001422 PropertyAttributes attributes,
1423 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001424 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1425 LookupResult* result,
1426 String* name,
Ben Murdoch086aeea2011-05-13 15:57:08 +01001427 Object* value,
1428 bool check_prototype);
John Reck59135872010-11-02 12:39:01 -07001429 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(Object* structure,
1430 String* name,
1431 Object* value,
1432 JSObject* holder);
1433 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1434 Object* value);
1435 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1436 String* name,
1437 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001438 PropertyAttributes attributes,
1439 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001440 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1441 String* name,
1442 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001443 PropertyAttributes attributes,
1444 StrictModeFlag strict_mode);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001445 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
John Reck59135872010-11-02 12:39:01 -07001446 String* key,
1447 Object* value,
1448 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001449
1450 // Retrieve a value in a normalized object given a lookup result.
1451 // Handles the special representation of JS global objects.
1452 Object* GetNormalizedProperty(LookupResult* result);
1453
1454 // Sets the property value in a normalized object given a lookup result.
1455 // Handles the special representation of JS global objects.
1456 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1457
1458 // Sets the property value in a normalized object given (key, value, details).
1459 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001460 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1461 Object* value,
1462 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001463
1464 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001465 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1466 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001467
Steve Blocka7e24c12009-10-30 11:49:00 +00001468 // Returns the class name ([[Class]] property in the specification).
1469 String* class_name();
1470
1471 // Returns the constructor name (the name (possibly, inferred name) of the
1472 // function that was used to instantiate the object).
1473 String* constructor_name();
1474
1475 // Retrieve interceptors.
1476 InterceptorInfo* GetNamedInterceptor();
1477 InterceptorInfo* GetIndexedInterceptor();
1478
1479 inline PropertyAttributes GetPropertyAttribute(String* name);
1480 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1481 String* name);
1482 PropertyAttributes GetLocalPropertyAttribute(String* name);
1483
John Reck59135872010-11-02 12:39:01 -07001484 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1485 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001486 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001487 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001488 Object* LookupAccessor(String* name, bool is_getter);
1489
John Reck59135872010-11-02 12:39:01 -07001490 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001491
Steve Blocka7e24c12009-10-30 11:49:00 +00001492 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001493 MaybeObject* GetPropertyWithFailedAccessCheck(
1494 Object* receiver,
1495 LookupResult* result,
1496 String* name,
1497 PropertyAttributes* attributes);
1498 MaybeObject* GetPropertyWithInterceptor(
1499 JSObject* receiver,
1500 String* name,
1501 PropertyAttributes* attributes);
1502 MaybeObject* GetPropertyPostInterceptor(
1503 JSObject* receiver,
1504 String* name,
1505 PropertyAttributes* attributes);
1506 MaybeObject* GetLocalPropertyPostInterceptor(JSObject* receiver,
1507 String* name,
1508 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001509
1510 // Returns true if this is an instance of an api function and has
1511 // been modified since it was created. May give false positives.
1512 bool IsDirty();
1513
1514 bool HasProperty(String* name) {
1515 return GetPropertyAttribute(name) != ABSENT;
1516 }
1517
1518 // Can cause a GC if it hits an interceptor.
1519 bool HasLocalProperty(String* name) {
1520 return GetLocalPropertyAttribute(name) != ABSENT;
1521 }
1522
Steve Blockd0582a62009-12-15 09:54:21 +00001523 // If the receiver is a JSGlobalProxy this method will return its prototype,
1524 // otherwise the result is the receiver itself.
1525 inline Object* BypassGlobalProxy();
1526
1527 // Accessors for hidden properties object.
1528 //
1529 // Hidden properties are not local properties of the object itself.
1530 // Instead they are stored on an auxiliary JSObject stored as a local
1531 // property with a special name Heap::hidden_symbol(). But if the
1532 // receiver is a JSGlobalProxy then the auxiliary object is a property
1533 // of its prototype.
1534 //
1535 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1536 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1537 // holder.
1538 //
1539 // These accessors do not touch interceptors or accessors.
1540 inline bool HasHiddenPropertiesObject();
1541 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001542 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1543 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001544
John Reck59135872010-11-02 12:39:01 -07001545 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1546 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001547
1548 // Tests for the fast common case for property enumeration.
1549 bool IsSimpleEnum();
1550
1551 // Do we want to keep the elements in fast case when increasing the
1552 // capacity?
1553 bool ShouldConvertToSlowElements(int new_capacity);
1554 // Returns true if the backing storage for the slow-case elements of
1555 // this object takes up nearly as much space as a fast-case backing
1556 // storage would. In that case the JSObject should have fast
1557 // elements.
1558 bool ShouldConvertToFastElements();
1559
1560 // Return the object's prototype (might be Heap::null_value()).
1561 inline Object* GetPrototype();
1562
Andrei Popescu402d9372010-02-26 13:31:12 +00001563 // Set the object's prototype (only JSObject and null are allowed).
John Reck59135872010-11-02 12:39:01 -07001564 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1565 bool skip_hidden_prototypes);
Andrei Popescu402d9372010-02-26 13:31:12 +00001566
Steve Blocka7e24c12009-10-30 11:49:00 +00001567 // Tells whether the index'th element is present.
1568 inline bool HasElement(uint32_t index);
1569 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001570
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001571 // Computes the new capacity when expanding the elements of a JSObject.
1572 static int NewElementsCapacity(int old_capacity) {
1573 // (old_capacity + 50%) + 16
1574 return old_capacity + (old_capacity >> 1) + 16;
1575 }
1576
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001577 // Tells whether the index'th element is present and how it is stored.
1578 enum LocalElementType {
1579 // There is no element with given index.
1580 UNDEFINED_ELEMENT,
1581
1582 // Element with given index is handled by interceptor.
1583 INTERCEPTED_ELEMENT,
1584
1585 // Element with given index is character in string.
1586 STRING_CHARACTER_ELEMENT,
1587
1588 // Element with given index is stored in fast backing store.
1589 FAST_ELEMENT,
1590
1591 // Element with given index is stored in slow backing store.
1592 DICTIONARY_ELEMENT
1593 };
1594
1595 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001596
1597 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1598 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1599
Steve Block9fac8402011-05-12 15:51:54 +01001600 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1601 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001602 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001603 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001604
1605 // Set the index'th array element.
1606 // A Failure object is returned if GC is needed.
Steve Block9fac8402011-05-12 15:51:54 +01001607 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1608 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001609 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001610 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001611
1612 // Returns the index'th element.
1613 // The undefined object if index is out of bounds.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001614 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
1615 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
1616
1617 // Get external element value at index if there is one and undefined
1618 // otherwise. Can return a failure if allocation of a heap number
1619 // failed.
1620 MaybeObject* GetExternalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001621
John Reck59135872010-11-02 12:39:01 -07001622 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1623 int length);
1624 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001625
1626 // Lookup interceptors are used for handling properties controlled by host
1627 // objects.
1628 inline bool HasNamedInterceptor();
1629 inline bool HasIndexedInterceptor();
1630
1631 // Support functions for v8 api (needed for correct interceptor behavior).
1632 bool HasRealNamedProperty(String* key);
1633 bool HasRealElementProperty(uint32_t index);
1634 bool HasRealNamedCallbackProperty(String* key);
1635
1636 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001637 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001638
1639 // Get the header size for a JSObject. Used to compute the index of
1640 // internal fields as well as the number of internal fields.
1641 inline int GetHeaderSize();
1642
1643 inline int GetInternalFieldCount();
Steve Block44f0eee2011-05-26 01:26:41 +01001644 inline int GetInternalFieldOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001645 inline Object* GetInternalField(int index);
1646 inline void SetInternalField(int index, Object* value);
1647
1648 // Lookup a property. If found, the result is valid and has
1649 // detailed information.
1650 void LocalLookup(String* name, LookupResult* result);
1651 void Lookup(String* name, LookupResult* result);
1652
1653 // The following lookup functions skip interceptors.
1654 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1655 void LookupRealNamedProperty(String* name, LookupResult* result);
1656 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1657 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Steve Block1e0659c2011-05-24 12:43:12 +01001658 MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
1659 uint32_t index, Object* value, bool* found);
Steve Blocka7e24c12009-10-30 11:49:00 +00001660 void LookupCallback(String* name, LookupResult* result);
1661
1662 // Returns the number of properties on this object filtering out properties
1663 // with the specified attributes (ignoring interceptors).
1664 int NumberOfLocalProperties(PropertyAttributes filter);
1665 // Returns the number of enumerable properties (ignoring interceptors).
1666 int NumberOfEnumProperties();
1667 // Fill in details for properties into storage starting at the specified
1668 // index.
1669 void GetLocalPropertyNames(FixedArray* storage, int index);
1670
1671 // Returns the number of properties on this object filtering out properties
1672 // with the specified attributes (ignoring interceptors).
1673 int NumberOfLocalElements(PropertyAttributes filter);
1674 // Returns the number of enumerable elements (ignoring interceptors).
1675 int NumberOfEnumElements();
1676 // Returns the number of elements on this object filtering out elements
1677 // with the specified attributes (ignoring interceptors).
1678 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1679 // Count and fill in the enumerable elements into storage.
1680 // (storage->length() == NumberOfEnumElements()).
1681 // If storage is NULL, will count the elements without adding
1682 // them to any storage.
1683 // Returns the number of enumerable elements.
1684 int GetEnumElementKeys(FixedArray* storage);
1685
1686 // Add a property to a fast-case object using a map transition to
1687 // new_map.
John Reck59135872010-11-02 12:39:01 -07001688 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1689 String* name,
1690 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001691
1692 // Add a constant function property to a fast-case object.
1693 // This leaves a CONSTANT_TRANSITION in the old map, and
1694 // if it is called on a second object with this map, a
1695 // normal property is added instead, with a map transition.
1696 // This avoids the creation of many maps with the same constant
1697 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001698 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1699 String* name,
1700 JSFunction* function,
1701 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001702
John Reck59135872010-11-02 12:39:01 -07001703 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1704 String* name,
1705 Object* value,
1706 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001707
1708 // Converts a descriptor of any other type to a real field,
1709 // backed by the properties array. Descriptors of visible
1710 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1711 // Converts the descriptor on the original object's map to a
1712 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001713 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001714 String* name,
1715 Object* new_value,
1716 PropertyAttributes attributes);
1717
1718 // Converts a descriptor of any other type to a real field,
1719 // backed by the properties array. Descriptors of visible
1720 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001721 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1722 String* name,
1723 Object* new_value,
1724 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001725
1726 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001727 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1728 Object* value,
1729 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001730
1731 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001732 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1733 Object* value,
1734 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001735
1736 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001737 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1738 Object* value,
Steve Block44f0eee2011-05-26 01:26:41 +01001739 PropertyAttributes attributes,
1740 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001741
1742 // Convert the object to use the canonical dictionary
1743 // representation. If the object is expected to have additional properties
1744 // added this number can be indicated to have the backing store allocated to
1745 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001746 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1747 PropertyNormalizationMode mode,
1748 int expected_additional_properties);
1749 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001750
John Reck59135872010-11-02 12:39:01 -07001751 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001752
Steve Blocka7e24c12009-10-30 11:49:00 +00001753 // Transform slow named properties to fast variants.
1754 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001755 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1756 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001757
1758 // Access fast-case object properties at index.
1759 inline Object* FastPropertyAt(int index);
1760 inline Object* FastPropertyAtPut(int index, Object* value);
1761
1762 // Access to in object properties.
Steve Block44f0eee2011-05-26 01:26:41 +01001763 inline int GetInObjectPropertyOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001764 inline Object* InObjectPropertyAt(int index);
1765 inline Object* InObjectPropertyAtPut(int index,
1766 Object* value,
1767 WriteBarrierMode mode
1768 = UPDATE_WRITE_BARRIER);
1769
1770 // initializes the body after properties slot, properties slot is
1771 // initialized by set_properties
1772 // Note: this call does not update write barrier, it is caller's
1773 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001774 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001775
1776 // Check whether this object references another object
1777 bool ReferencesObject(Object* obj);
1778
1779 // Casting.
1780 static inline JSObject* cast(Object* obj);
1781
Steve Block8defd9f2010-07-08 12:39:36 +01001782 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001783 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001784
1785
Steve Blocka7e24c12009-10-30 11:49:00 +00001786 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001787 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001788#ifdef OBJECT_PRINT
1789 inline void JSObjectPrint() {
1790 JSObjectPrint(stdout);
1791 }
1792 void JSObjectPrint(FILE* out);
1793#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001794#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001795 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001796#endif
1797#ifdef OBJECT_PRINT
1798 inline void PrintProperties() {
1799 PrintProperties(stdout);
1800 }
1801 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001802
Ben Murdochb0fe1622011-05-05 13:52:32 +01001803 inline void PrintElements() {
1804 PrintElements(stdout);
1805 }
1806 void PrintElements(FILE* out);
1807#endif
1808
1809#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001810 // Structure for collecting spill information about JSObjects.
1811 class SpillInformation {
1812 public:
1813 void Clear();
1814 void Print();
1815 int number_of_objects_;
1816 int number_of_objects_with_fast_properties_;
1817 int number_of_objects_with_fast_elements_;
1818 int number_of_fast_used_fields_;
1819 int number_of_fast_unused_fields_;
1820 int number_of_slow_used_properties_;
1821 int number_of_slow_unused_properties_;
1822 int number_of_fast_used_elements_;
1823 int number_of_fast_unused_elements_;
1824 int number_of_slow_used_elements_;
1825 int number_of_slow_unused_elements_;
1826 };
1827
1828 void IncrementSpillStatistics(SpillInformation* info);
1829#endif
1830 Object* SlowReverseLookup(Object* value);
1831
Steve Block8defd9f2010-07-08 12:39:36 +01001832 // Maximal number of fast properties for the JSObject. Used to
1833 // restrict the number of map transitions to avoid an explosion in
1834 // the number of maps for objects used as dictionaries.
1835 inline int MaxFastProperties();
1836
Leon Clarkee46be812010-01-19 14:06:41 +00001837 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1838 // Also maximal value of JSArray's length property.
1839 static const uint32_t kMaxElementCount = 0xffffffffu;
1840
Steve Blocka7e24c12009-10-30 11:49:00 +00001841 static const uint32_t kMaxGap = 1024;
1842 static const int kMaxFastElementsLength = 5000;
1843 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001844 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00001845 static const int kMaxInstanceSize = 255 * kPointerSize;
1846 // When extending the backing storage for property values, we increase
1847 // its size by more than the 1 entry necessary, so sequentially adding fields
1848 // to the same object requires fewer allocations and copies.
1849 static const int kFieldsAdded = 3;
1850
1851 // Layout description.
1852 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1853 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1854 static const int kHeaderSize = kElementsOffset + kPointerSize;
1855
1856 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1857
Iain Merrick75681382010-08-19 15:07:18 +01001858 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1859 public:
1860 static inline int SizeOf(Map* map, HeapObject* object);
1861 };
1862
Steve Blocka7e24c12009-10-30 11:49:00 +00001863 private:
John Reck59135872010-11-02 12:39:01 -07001864 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1865 Object* structure,
1866 uint32_t index,
1867 Object* holder);
1868 MaybeObject* SetElementWithCallback(Object* structure,
1869 uint32_t index,
1870 Object* value,
1871 JSObject* holder);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001872 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
1873 uint32_t index,
1874 Object* value,
1875 StrictModeFlag strict_mode,
1876 bool check_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01001877 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
1878 uint32_t index,
1879 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001880 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01001881 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00001882
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001883 MaybeObject* GetElementPostInterceptor(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001884
John Reck59135872010-11-02 12:39:01 -07001885 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1886 DeleteMode mode);
1887 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00001888
John Reck59135872010-11-02 12:39:01 -07001889 MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1890 DeleteMode mode);
1891 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001892
1893 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1894 String* name,
1895 bool continue_search);
1896 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1897 String* name,
1898 bool continue_search);
1899 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1900 Object* receiver,
1901 LookupResult* result,
1902 String* name,
1903 bool continue_search);
1904 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1905 LookupResult* result,
1906 String* name,
1907 bool continue_search);
1908
1909 // Returns true if most of the elements backing storage is used.
1910 bool HasDenseElements();
1911
Leon Clarkef7060e22010-06-03 12:02:55 +01001912 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07001913 MUST_USE_RESULT MaybeObject* SetElementCallback(
1914 uint32_t index,
1915 Object* structure,
1916 PropertyAttributes attributes);
1917 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1918 String* name,
1919 Object* structure,
1920 PropertyAttributes attributes);
1921 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
1922 String* name,
1923 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001924
1925 void LookupInDescriptor(String* name, LookupResult* result);
1926
1927 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1928};
1929
1930
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001931// FixedArray describes fixed-sized arrays with element type Object*.
1932class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001933 public:
1934 // [length]: length of the array.
1935 inline int length();
1936 inline void set_length(int value);
1937
Steve Blocka7e24c12009-10-30 11:49:00 +00001938 // Setter and getter for elements.
1939 inline Object* get(int index);
1940 // Setter that uses write barrier.
1941 inline void set(int index, Object* value);
1942
1943 // Setter that doesn't need write barrier).
1944 inline void set(int index, Smi* value);
1945 // Setter with explicit barrier mode.
1946 inline void set(int index, Object* value, WriteBarrierMode mode);
1947
1948 // Setters for frequently used oddballs located in old space.
1949 inline void set_undefined(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01001950 // TODO(isolates): duplicate.
1951 inline void set_undefined(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001952 inline void set_null(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01001953 // TODO(isolates): duplicate.
1954 inline void set_null(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001955 inline void set_the_hole(int index);
1956
Iain Merrick75681382010-08-19 15:07:18 +01001957 // Setters with less debug checks for the GC to use.
1958 inline void set_unchecked(int index, Smi* value);
Steve Block44f0eee2011-05-26 01:26:41 +01001959 inline void set_null_unchecked(Heap* heap, int index);
1960 inline void set_unchecked(Heap* heap, int index, Object* value,
1961 WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01001962
Steve Block6ded16b2010-05-10 14:33:55 +01001963 // Gives access to raw memory which stores the array's data.
1964 inline Object** data_start();
1965
Steve Blocka7e24c12009-10-30 11:49:00 +00001966 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07001967 MUST_USE_RESULT inline MaybeObject* Copy();
1968 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001969
1970 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07001971 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00001972
1973 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07001974 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00001975
1976 // Copy a sub array from the receiver to dest.
1977 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1978
1979 // Garbage collection support.
1980 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1981
1982 // Code Generation support.
1983 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1984
1985 // Casting.
1986 static inline FixedArray* cast(Object* obj);
1987
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001988 // Layout description.
1989 // Length is smi tagged when it is stored.
1990 static const int kLengthOffset = HeapObject::kHeaderSize;
1991 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001992
1993 // Maximal allowed size, in bytes, of a single FixedArray.
1994 // Prevents overflowing size computations, as well as extreme memory
1995 // consumption.
1996 static const int kMaxSize = 512 * MB;
1997 // Maximally allowed length of a FixedArray.
1998 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001999
2000 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002001#ifdef OBJECT_PRINT
2002 inline void FixedArrayPrint() {
2003 FixedArrayPrint(stdout);
2004 }
2005 void FixedArrayPrint(FILE* out);
2006#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002007#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002008 void FixedArrayVerify();
2009 // Checks if two FixedArrays have identical contents.
2010 bool IsEqualTo(FixedArray* other);
2011#endif
2012
2013 // Swap two elements in a pair of arrays. If this array and the
2014 // numbers array are the same object, the elements are only swapped
2015 // once.
2016 void SwapPairs(FixedArray* numbers, int i, int j);
2017
2018 // Sort prefix of this array and the numbers array as pairs wrt. the
2019 // numbers. If the numbers array and the this array are the same
2020 // object, the prefix of this array is sorted.
2021 void SortPairs(FixedArray* numbers, uint32_t len);
2022
Iain Merrick75681382010-08-19 15:07:18 +01002023 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2024 public:
2025 static inline int SizeOf(Map* map, HeapObject* object) {
2026 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2027 }
2028 };
2029
Steve Blocka7e24c12009-10-30 11:49:00 +00002030 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00002031 // Set operation on FixedArray without using write barriers. Can
2032 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00002033 static inline void fast_set(FixedArray* array, int index, Object* value);
2034
2035 private:
2036 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2037};
2038
2039
2040// DescriptorArrays are fixed arrays used to hold instance descriptors.
2041// The format of the these objects is:
Ben Murdoch257744e2011-11-30 15:57:28 +00002042// TODO(1399): It should be possible to make room for bit_field3 in the map
2043// without overloading the instance descriptors field in the map
2044// (and storing it in the DescriptorArray when the map has one).
2045// [0]: storage for bit_field3 for Map owning this object (Smi)
2046// [1]: point to a fixed array with (value, detail) pairs.
2047// [2]: next enumeration index (Smi), or pointer to small fixed array:
Steve Blocka7e24c12009-10-30 11:49:00 +00002048// [0]: next enumeration index (Smi)
2049// [1]: pointer to fixed array with enum cache
Ben Murdoch257744e2011-11-30 15:57:28 +00002050// [3]: first key
Steve Blocka7e24c12009-10-30 11:49:00 +00002051// [length() - 1]: last key
2052//
2053class DescriptorArray: public FixedArray {
2054 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00002055 // Returns true for both shared empty_descriptor_array and for smis, which the
2056 // map uses to encode additional bit fields when the descriptor array is not
2057 // yet used.
Steve Blocka7e24c12009-10-30 11:49:00 +00002058 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00002059
Steve Blocka7e24c12009-10-30 11:49:00 +00002060 // Returns the number of descriptors in the array.
2061 int number_of_descriptors() {
Steve Block44f0eee2011-05-26 01:26:41 +01002062 ASSERT(length() > kFirstIndex || IsEmpty());
2063 int len = length();
2064 return len <= kFirstIndex ? 0 : len - kFirstIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00002065 }
2066
2067 int NextEnumerationIndex() {
2068 if (IsEmpty()) return PropertyDetails::kInitialIndex;
2069 Object* obj = get(kEnumerationIndexIndex);
2070 if (obj->IsSmi()) {
2071 return Smi::cast(obj)->value();
2072 } else {
2073 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2074 return Smi::cast(index)->value();
2075 }
2076 }
2077
2078 // Set next enumeration index and flush any enum cache.
2079 void SetNextEnumerationIndex(int value) {
2080 if (!IsEmpty()) {
2081 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2082 }
2083 }
2084 bool HasEnumCache() {
2085 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
2086 }
2087
2088 Object* GetEnumCache() {
2089 ASSERT(HasEnumCache());
2090 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2091 return bridge->get(kEnumCacheBridgeCacheIndex);
2092 }
2093
Ben Murdoch257744e2011-11-30 15:57:28 +00002094 // TODO(1399): It should be possible to make room for bit_field3 in the map
2095 // without overloading the instance descriptors field in the map
2096 // (and storing it in the DescriptorArray when the map has one).
2097 inline int bit_field3_storage();
2098 inline void set_bit_field3_storage(int value);
2099
Steve Blocka7e24c12009-10-30 11:49:00 +00002100 // Initialize or change the enum cache,
2101 // using the supplied storage for the small "bridge".
2102 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2103
2104 // Accessors for fetching instance descriptor at descriptor number.
2105 inline String* GetKey(int descriptor_number);
2106 inline Object* GetValue(int descriptor_number);
2107 inline Smi* GetDetails(int descriptor_number);
2108 inline PropertyType GetType(int descriptor_number);
2109 inline int GetFieldIndex(int descriptor_number);
2110 inline JSFunction* GetConstantFunction(int descriptor_number);
2111 inline Object* GetCallbacksObject(int descriptor_number);
2112 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2113 inline bool IsProperty(int descriptor_number);
2114 inline bool IsTransition(int descriptor_number);
2115 inline bool IsNullDescriptor(int descriptor_number);
2116 inline bool IsDontEnum(int descriptor_number);
2117
2118 // Accessor for complete descriptor.
2119 inline void Get(int descriptor_number, Descriptor* desc);
2120 inline void Set(int descriptor_number, Descriptor* desc);
2121
2122 // Transfer complete descriptor from another descriptor array to
2123 // this one.
2124 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2125
2126 // Copy the descriptor array, insert a new descriptor and optionally
2127 // remove map transitions. If the descriptor is already present, it is
2128 // replaced. If a replaced descriptor is a real property (not a transition
2129 // or null), its enumeration index is kept as is.
2130 // If adding a real property, map transitions must be removed. If adding
2131 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002132 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2133 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002134
2135 // Remove all transitions. Return a copy of the array with all transitions
2136 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002137 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002138
2139 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002140 // Does not check for duplicates.
2141 void SortUnchecked();
2142
2143 // Sort the instance descriptors by the hash codes of their keys.
2144 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00002145 void Sort();
2146
2147 // Search the instance descriptors for given name.
2148 inline int Search(String* name);
2149
Iain Merrick75681382010-08-19 15:07:18 +01002150 // As the above, but uses DescriptorLookupCache and updates it when
2151 // necessary.
2152 inline int SearchWithCache(String* name);
2153
Steve Blocka7e24c12009-10-30 11:49:00 +00002154 // Tells whether the name is present int the array.
2155 bool Contains(String* name) { return kNotFound != Search(name); }
2156
2157 // Perform a binary search in the instance descriptors represented
2158 // by this fixed array. low and high are descriptor indices. If there
2159 // are three instance descriptors in this array it should be called
2160 // with low=0 and high=2.
2161 int BinarySearch(String* name, int low, int high);
2162
2163 // Perform a linear search in the instance descriptors represented
2164 // by this fixed array. len is the number of descriptor indices that are
2165 // valid. Does not require the descriptors to be sorted.
2166 int LinearSearch(String* name, int len);
2167
2168 // Allocates a DescriptorArray, but returns the singleton
2169 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002170 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002171
2172 // Casting.
2173 static inline DescriptorArray* cast(Object* obj);
2174
2175 // Constant for denoting key was not found.
2176 static const int kNotFound = -1;
2177
Ben Murdoch257744e2011-11-30 15:57:28 +00002178 static const int kBitField3StorageIndex = 0;
2179 static const int kContentArrayIndex = 1;
2180 static const int kEnumerationIndexIndex = 2;
2181 static const int kFirstIndex = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00002182
2183 // The length of the "bridge" to the enum cache.
2184 static const int kEnumCacheBridgeLength = 2;
2185 static const int kEnumCacheBridgeEnumIndex = 0;
2186 static const int kEnumCacheBridgeCacheIndex = 1;
2187
2188 // Layout description.
Ben Murdoch257744e2011-11-30 15:57:28 +00002189 static const int kBitField3StorageOffset = FixedArray::kHeaderSize;
2190 static const int kContentArrayOffset = kBitField3StorageOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002191 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2192 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2193
2194 // Layout description for the bridge array.
2195 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2196 static const int kEnumCacheBridgeCacheOffset =
2197 kEnumCacheBridgeEnumOffset + kPointerSize;
2198
Ben Murdochb0fe1622011-05-05 13:52:32 +01002199#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002200 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002201 inline void PrintDescriptors() {
2202 PrintDescriptors(stdout);
2203 }
2204 void PrintDescriptors(FILE* out);
2205#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002206
Ben Murdochb0fe1622011-05-05 13:52:32 +01002207#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002208 // Is the descriptor array sorted and without duplicates?
2209 bool IsSortedNoDuplicates();
2210
2211 // Are two DescriptorArrays equal?
2212 bool IsEqualTo(DescriptorArray* other);
2213#endif
2214
2215 // The maximum number of descriptors we want in a descriptor array (should
2216 // fit in a page).
2217 static const int kMaxNumberOfDescriptors = 1024 + 512;
2218
2219 private:
2220 // Conversion from descriptor number to array indices.
2221 static int ToKeyIndex(int descriptor_number) {
2222 return descriptor_number+kFirstIndex;
2223 }
Leon Clarkee46be812010-01-19 14:06:41 +00002224
2225 static int ToDetailsIndex(int descriptor_number) {
2226 return (descriptor_number << 1) + 1;
2227 }
2228
Steve Blocka7e24c12009-10-30 11:49:00 +00002229 static int ToValueIndex(int descriptor_number) {
2230 return descriptor_number << 1;
2231 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002232
2233 bool is_null_descriptor(int descriptor_number) {
2234 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2235 NULL_DESCRIPTOR;
2236 }
2237 // Swap operation on FixedArray without using write barriers.
2238 static inline void fast_swap(FixedArray* array, int first, int second);
2239
2240 // Swap descriptor first and second.
2241 inline void Swap(int first, int second);
2242
2243 FixedArray* GetContentArray() {
2244 return FixedArray::cast(get(kContentArrayIndex));
2245 }
2246 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2247};
2248
2249
2250// HashTable is a subclass of FixedArray that implements a hash table
2251// that uses open addressing and quadratic probing.
2252//
2253// In order for the quadratic probing to work, elements that have not
2254// yet been used and elements that have been deleted are
2255// distinguished. Probing continues when deleted elements are
2256// encountered and stops when unused elements are encountered.
2257//
2258// - Elements with key == undefined have not been used yet.
2259// - Elements with key == null have been deleted.
2260//
2261// The hash table class is parameterized with a Shape and a Key.
2262// Shape must be a class with the following interface:
2263// class ExampleShape {
2264// public:
2265// // Tells whether key matches other.
2266// static bool IsMatch(Key key, Object* other);
2267// // Returns the hash value for key.
2268// static uint32_t Hash(Key key);
2269// // Returns the hash value for object.
2270// static uint32_t HashForObject(Key key, Object* object);
2271// // Convert key to an object.
2272// static inline Object* AsObject(Key key);
2273// // The prefix size indicates number of elements in the beginning
2274// // of the backing storage.
2275// static const int kPrefixSize = ..;
2276// // The Element size indicates number of elements per entry.
2277// static const int kEntrySize = ..;
2278// };
Steve Block3ce2e202009-11-05 08:53:23 +00002279// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002280// beginning of the backing storage that can be used for non-element
2281// information by subclasses.
2282
2283template<typename Shape, typename Key>
2284class HashTable: public FixedArray {
2285 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002286 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002287 int NumberOfElements() {
2288 return Smi::cast(get(kNumberOfElementsIndex))->value();
2289 }
2290
Leon Clarkee46be812010-01-19 14:06:41 +00002291 // Returns the number of deleted elements in the hash table.
2292 int NumberOfDeletedElements() {
2293 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2294 }
2295
Steve Block3ce2e202009-11-05 08:53:23 +00002296 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002297 int Capacity() {
2298 return Smi::cast(get(kCapacityIndex))->value();
2299 }
2300
2301 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002302 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002303 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2304
2305 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002306 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002307 void ElementRemoved() {
2308 SetNumberOfElements(NumberOfElements() - 1);
2309 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2310 }
2311 void ElementsRemoved(int n) {
2312 SetNumberOfElements(NumberOfElements() - n);
2313 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2314 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002315
Steve Block3ce2e202009-11-05 08:53:23 +00002316 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002317 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002318 int at_least_space_for,
2319 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002320
2321 // Returns the key at entry.
2322 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2323
2324 // Tells whether k is a real key. Null and undefined are not allowed
2325 // as keys and can be used to indicate missing or deleted elements.
2326 bool IsKey(Object* k) {
2327 return !k->IsNull() && !k->IsUndefined();
2328 }
2329
2330 // Garbage collection support.
2331 void IteratePrefix(ObjectVisitor* visitor);
2332 void IterateElements(ObjectVisitor* visitor);
2333
2334 // Casting.
2335 static inline HashTable* cast(Object* obj);
2336
2337 // Compute the probe offset (quadratic probing).
2338 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2339 return (n + n * n) >> 1;
2340 }
2341
2342 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002343 static const int kNumberOfDeletedElementsIndex = 1;
2344 static const int kCapacityIndex = 2;
2345 static const int kPrefixStartIndex = 3;
2346 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002347 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002348 static const int kEntrySize = Shape::kEntrySize;
2349 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002350 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002351 static const int kCapacityOffset =
2352 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002353
2354 // Constant used for denoting a absent entry.
2355 static const int kNotFound = -1;
2356
Leon Clarkee46be812010-01-19 14:06:41 +00002357 // Maximal capacity of HashTable. Based on maximal length of underlying
2358 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2359 // cannot overflow.
2360 static const int kMaxCapacity =
2361 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2362
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002363 // Find entry for key otherwise return kNotFound.
Steve Block44f0eee2011-05-26 01:26:41 +01002364 inline int FindEntry(Key key);
2365 int FindEntry(Isolate* isolate, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002366
2367 protected:
2368
2369 // Find the entry at which to insert element with the given key that
2370 // has the given hash value.
2371 uint32_t FindInsertionEntry(uint32_t hash);
2372
2373 // Returns the index for an entry (of the key)
2374 static inline int EntryToIndex(int entry) {
2375 return (entry * kEntrySize) + kElementsStartIndex;
2376 }
2377
Steve Block3ce2e202009-11-05 08:53:23 +00002378 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002379 void SetNumberOfElements(int nof) {
2380 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2381 }
2382
Leon Clarkee46be812010-01-19 14:06:41 +00002383 // Update the number of deleted elements in the hash table.
2384 void SetNumberOfDeletedElements(int nod) {
2385 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2386 }
2387
Steve Blocka7e24c12009-10-30 11:49:00 +00002388 // Sets the capacity of the hash table.
2389 void SetCapacity(int capacity) {
2390 // To scale a computed hash code to fit within the hash table, we
2391 // use bit-wise AND with a mask, so the capacity must be positive
2392 // and non-zero.
2393 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002394 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002395 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2396 }
2397
2398
2399 // Returns probe entry.
2400 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2401 ASSERT(IsPowerOf2(size));
2402 return (hash + GetProbeOffset(number)) & (size - 1);
2403 }
2404
Leon Clarkee46be812010-01-19 14:06:41 +00002405 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2406 return hash & (size - 1);
2407 }
2408
2409 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2410 return (last + number) & (size - 1);
2411 }
2412
Steve Blocka7e24c12009-10-30 11:49:00 +00002413 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002414 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002415};
2416
2417
2418
2419// HashTableKey is an abstract superclass for virtual key behavior.
2420class HashTableKey {
2421 public:
2422 // Returns whether the other object matches this key.
2423 virtual bool IsMatch(Object* other) = 0;
2424 // Returns the hash value for this key.
2425 virtual uint32_t Hash() = 0;
2426 // Returns the hash value for object.
2427 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002428 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002429 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002430 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002431 // Required.
2432 virtual ~HashTableKey() {}
2433};
2434
2435class SymbolTableShape {
2436 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002437 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002438 return key->IsMatch(value);
2439 }
Steve Block44f0eee2011-05-26 01:26:41 +01002440 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002441 return key->Hash();
2442 }
Steve Block44f0eee2011-05-26 01:26:41 +01002443 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002444 return key->HashForObject(object);
2445 }
Steve Block44f0eee2011-05-26 01:26:41 +01002446 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002447 return key->AsObject();
2448 }
2449
2450 static const int kPrefixSize = 0;
2451 static const int kEntrySize = 1;
2452};
2453
Ben Murdoch257744e2011-11-30 15:57:28 +00002454class SeqAsciiString;
2455
Steve Blocka7e24c12009-10-30 11:49:00 +00002456// SymbolTable.
2457//
2458// No special elements in the prefix and the element size is 1
2459// because only the symbol itself (the key) needs to be stored.
2460class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2461 public:
2462 // Find symbol in the symbol table. If it is not there yet, it is
2463 // added. The return value is the symbol table which might have
2464 // been enlarged. If the return value is not a failure, the symbol
2465 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002466 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002467 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2468 Object** s);
Ben Murdoch257744e2011-11-30 15:57:28 +00002469 MUST_USE_RESULT MaybeObject* LookupSubStringAsciiSymbol(
2470 Handle<SeqAsciiString> str,
2471 int from,
2472 int length,
2473 Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002474 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2475 Object** s);
John Reck59135872010-11-02 12:39:01 -07002476 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002477
2478 // Looks up a symbol that is equal to the given string and returns
2479 // true if it is found, assigning the symbol to the given output
2480 // parameter.
2481 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002482 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002483
2484 // Casting.
2485 static inline SymbolTable* cast(Object* obj);
2486
2487 private:
John Reck59135872010-11-02 12:39:01 -07002488 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002489
2490 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2491};
2492
2493
2494class MapCacheShape {
2495 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002496 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002497 return key->IsMatch(value);
2498 }
Steve Block44f0eee2011-05-26 01:26:41 +01002499 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002500 return key->Hash();
2501 }
2502
Steve Block44f0eee2011-05-26 01:26:41 +01002503 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002504 return key->HashForObject(object);
2505 }
2506
Steve Block44f0eee2011-05-26 01:26:41 +01002507 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002508 return key->AsObject();
2509 }
2510
2511 static const int kPrefixSize = 0;
2512 static const int kEntrySize = 2;
2513};
2514
2515
2516// MapCache.
2517//
2518// Maps keys that are a fixed array of symbols to a map.
2519// Used for canonicalize maps for object literals.
2520class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2521 public:
2522 // Find cached value for a string key, otherwise return null.
2523 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002524 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002525 static inline MapCache* cast(Object* obj);
2526
2527 private:
2528 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2529};
2530
2531
2532template <typename Shape, typename Key>
2533class Dictionary: public HashTable<Shape, Key> {
2534 public:
2535
2536 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2537 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2538 }
2539
2540 // Returns the value at entry.
2541 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002542 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002543 }
2544
2545 // Set the value for entry.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002546 // Returns false if the put wasn't performed due to property being read only.
2547 // Returns true on successful put.
2548 bool ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002549 // Check that this value can actually be written.
2550 PropertyDetails details = DetailsAt(entry);
2551 // If a value has not been initilized we allow writing to it even if
2552 // it is read only (a declared const that has not been initialized).
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002553 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2554 return false;
2555 }
2556 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2557 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002558 }
2559
2560 // Returns the property details for the property at entry.
2561 PropertyDetails DetailsAt(int entry) {
2562 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2563 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002564 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002565 }
2566
2567 // Set the details for entry.
2568 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002569 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002570 }
2571
2572 // Sorting support
2573 void CopyValuesTo(FixedArray* elements);
2574
2575 // Delete a property from the dictionary.
2576 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2577
2578 // Returns the number of elements in the dictionary filtering out properties
2579 // with the specified attributes.
2580 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2581
2582 // Returns the number of enumerable elements in the dictionary.
2583 int NumberOfEnumElements();
2584
2585 // Copies keys to preallocated fixed array.
2586 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2587 // Fill in details for properties into storage.
Ben Murdoch6d7cb002011-08-04 19:25:22 +01002588 void CopyKeysTo(FixedArray* storage, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002589
2590 // Accessors for next enumeration index.
2591 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002592 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002593 }
2594
2595 int NextEnumerationIndex() {
2596 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2597 }
2598
2599 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002600 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002601
2602 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002603 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002604
Ben Murdochb0fe1622011-05-05 13:52:32 +01002605#ifdef OBJECT_PRINT
2606 inline void Print() {
2607 Print(stdout);
2608 }
2609 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002610#endif
2611 // Returns the key (slow).
2612 Object* SlowReverseLookup(Object* value);
2613
2614 // Sets the entry to (key, value) pair.
2615 inline void SetEntry(int entry,
2616 Object* key,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002617 Object* value);
2618 inline void SetEntry(int entry,
2619 Object* key,
Steve Blocka7e24c12009-10-30 11:49:00 +00002620 Object* value,
2621 PropertyDetails details);
2622
John Reck59135872010-11-02 12:39:01 -07002623 MUST_USE_RESULT MaybeObject* Add(Key key,
2624 Object* value,
2625 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002626
2627 protected:
2628 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002629 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002630
2631 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002632 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2633 Object* value,
2634 PropertyDetails details,
2635 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002636
2637 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002638 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002639 static const int kMaxNumberKeyIndex =
2640 HashTable<Shape, Key>::kPrefixStartIndex;
2641 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2642};
2643
2644
2645class StringDictionaryShape {
2646 public:
2647 static inline bool IsMatch(String* key, Object* other);
2648 static inline uint32_t Hash(String* key);
2649 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002650 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002651 static const int kPrefixSize = 2;
2652 static const int kEntrySize = 3;
2653 static const bool kIsEnumerable = true;
2654};
2655
2656
2657class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2658 public:
2659 static inline StringDictionary* cast(Object* obj) {
2660 ASSERT(obj->IsDictionary());
2661 return reinterpret_cast<StringDictionary*>(obj);
2662 }
2663
2664 // Copies enumerable keys to preallocated fixed array.
2665 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2666
2667 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002668 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2669 JSObject* obj,
2670 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002671
2672 // Find entry for key otherwise return kNotFound. Optimzed version of
2673 // HashTable::FindEntry.
2674 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002675};
2676
2677
2678class NumberDictionaryShape {
2679 public:
2680 static inline bool IsMatch(uint32_t key, Object* other);
2681 static inline uint32_t Hash(uint32_t key);
2682 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002683 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002684 static const int kPrefixSize = 2;
2685 static const int kEntrySize = 3;
2686 static const bool kIsEnumerable = false;
2687};
2688
2689
2690class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2691 public:
2692 static NumberDictionary* cast(Object* obj) {
2693 ASSERT(obj->IsDictionary());
2694 return reinterpret_cast<NumberDictionary*>(obj);
2695 }
2696
2697 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002698 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2699 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2700 Object* value,
2701 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002702
2703 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002704 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2705 Object* value,
2706 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002707
2708 void UpdateMaxNumberKey(uint32_t key);
2709
2710 // If slow elements are required we will never go back to fast-case
2711 // for the elements kept in this dictionary. We require slow
2712 // elements if an element has been added at an index larger than
2713 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2714 // when defining a getter or setter with a number key.
2715 inline bool requires_slow_elements();
2716 inline void set_requires_slow_elements();
2717
2718 // Get the value of the max number key that has been added to this
2719 // dictionary. max_number_key can only be called if
2720 // requires_slow_elements returns false.
2721 inline uint32_t max_number_key();
2722
2723 // Remove all entries were key is a number and (from <= key && key < to).
2724 void RemoveNumberEntries(uint32_t from, uint32_t to);
2725
2726 // Bit masks.
2727 static const int kRequiresSlowElementsMask = 1;
2728 static const int kRequiresSlowElementsTagSize = 1;
2729 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2730};
2731
2732
Steve Block6ded16b2010-05-10 14:33:55 +01002733// JSFunctionResultCache caches results of some JSFunction invocation.
2734// It is a fixed array with fixed structure:
2735// [0]: factory function
2736// [1]: finger index
2737// [2]: current cache size
2738// [3]: dummy field.
2739// The rest of array are key/value pairs.
2740class JSFunctionResultCache: public FixedArray {
2741 public:
2742 static const int kFactoryIndex = 0;
2743 static const int kFingerIndex = kFactoryIndex + 1;
2744 static const int kCacheSizeIndex = kFingerIndex + 1;
2745 static const int kDummyIndex = kCacheSizeIndex + 1;
2746 static const int kEntriesIndex = kDummyIndex + 1;
2747
2748 static const int kEntrySize = 2; // key + value
2749
Kristian Monsen25f61362010-05-21 11:50:48 +01002750 static const int kFactoryOffset = kHeaderSize;
2751 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2752 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2753
Steve Block6ded16b2010-05-10 14:33:55 +01002754 inline void MakeZeroSize();
2755 inline void Clear();
2756
Ben Murdochb8e0da22011-05-16 14:20:40 +01002757 inline int size();
2758 inline void set_size(int size);
2759 inline int finger_index();
2760 inline void set_finger_index(int finger_index);
2761
Steve Block6ded16b2010-05-10 14:33:55 +01002762 // Casting
2763 static inline JSFunctionResultCache* cast(Object* obj);
2764
2765#ifdef DEBUG
2766 void JSFunctionResultCacheVerify();
2767#endif
2768};
2769
2770
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002771// The cache for maps used by normalized (dictionary mode) objects.
2772// Such maps do not have property descriptors, so a typical program
2773// needs very limited number of distinct normalized maps.
2774class NormalizedMapCache: public FixedArray {
2775 public:
2776 static const int kEntries = 64;
2777
John Reck59135872010-11-02 12:39:01 -07002778 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2779 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002780
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002781 void Clear();
2782
2783 // Casting
2784 static inline NormalizedMapCache* cast(Object* obj);
2785
2786#ifdef DEBUG
2787 void NormalizedMapCacheVerify();
2788#endif
2789
2790 private:
2791 static int Hash(Map* fast);
2792
2793 static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2794};
2795
2796
Steve Blocka7e24c12009-10-30 11:49:00 +00002797// ByteArray represents fixed sized byte arrays. Used by the outside world,
2798// such as PCRE, and also by the memory allocator and garbage collector to
2799// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002800class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002801 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002802 // [length]: length of the array.
2803 inline int length();
2804 inline void set_length(int value);
2805
Steve Blocka7e24c12009-10-30 11:49:00 +00002806 // Setter and getter.
2807 inline byte get(int index);
2808 inline void set(int index, byte value);
2809
2810 // Treat contents as an int array.
2811 inline int get_int(int index);
2812
2813 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002814 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002815 }
2816 // We use byte arrays for free blocks in the heap. Given a desired size in
2817 // bytes that is a multiple of the word size and big enough to hold a byte
2818 // array, this function returns the number of elements a byte array should
2819 // have.
2820 static int LengthFor(int size_in_bytes) {
2821 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2822 ASSERT(size_in_bytes >= kHeaderSize);
2823 return size_in_bytes - kHeaderSize;
2824 }
2825
2826 // Returns data start address.
2827 inline Address GetDataStartAddress();
2828
2829 // Returns a pointer to the ByteArray object for a given data start address.
2830 static inline ByteArray* FromDataStartAddress(Address address);
2831
2832 // Casting.
2833 static inline ByteArray* cast(Object* obj);
2834
2835 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002836 inline int ByteArraySize() {
2837 return SizeFor(this->length());
2838 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002839#ifdef OBJECT_PRINT
2840 inline void ByteArrayPrint() {
2841 ByteArrayPrint(stdout);
2842 }
2843 void ByteArrayPrint(FILE* out);
2844#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002845#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002846 void ByteArrayVerify();
2847#endif
2848
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002849 // Layout description.
2850 // Length is smi tagged when it is stored.
2851 static const int kLengthOffset = HeapObject::kHeaderSize;
2852 static const int kHeaderSize = kLengthOffset + kPointerSize;
2853
2854 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002855
Leon Clarkee46be812010-01-19 14:06:41 +00002856 // Maximal memory consumption for a single ByteArray.
2857 static const int kMaxSize = 512 * MB;
2858 // Maximal length of a single ByteArray.
2859 static const int kMaxLength = kMaxSize - kHeaderSize;
2860
Steve Blocka7e24c12009-10-30 11:49:00 +00002861 private:
2862 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2863};
2864
2865
Steve Block3ce2e202009-11-05 08:53:23 +00002866// An ExternalArray represents a fixed-size array of primitive values
2867// which live outside the JavaScript heap. Its subclasses are used to
2868// implement the CanvasArray types being defined in the WebGL
2869// specification. As of this writing the first public draft is not yet
2870// available, but Khronos members can access the draft at:
2871// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2872//
2873// The semantics of these arrays differ from CanvasPixelArray.
2874// Out-of-range values passed to the setter are converted via a C
2875// cast, not clamping. Out-of-range indices cause exceptions to be
2876// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002877class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002878 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002879 // [length]: length of the array.
2880 inline int length();
2881 inline void set_length(int value);
2882
Steve Block3ce2e202009-11-05 08:53:23 +00002883 // [external_pointer]: The pointer to the external memory area backing this
2884 // external array.
2885 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2886
2887 // Casting.
2888 static inline ExternalArray* cast(Object* obj);
2889
2890 // Maximal acceptable length for an external array.
2891 static const int kMaxLength = 0x3fffffff;
2892
2893 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002894 static const int kLengthOffset = HeapObject::kHeaderSize;
2895 static const int kExternalPointerOffset =
2896 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002897 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002898 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002899
2900 private:
2901 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2902};
2903
2904
Steve Block44f0eee2011-05-26 01:26:41 +01002905// A ExternalPixelArray represents a fixed-size byte array with special
2906// semantics used for implementing the CanvasPixelArray object. Please see the
2907// specification at:
2908
2909// http://www.whatwg.org/specs/web-apps/current-work/
2910// multipage/the-canvas-element.html#canvaspixelarray
2911// In particular, write access clamps the value written to 0 or 255 if the
2912// value written is outside this range.
2913class ExternalPixelArray: public ExternalArray {
2914 public:
2915 inline uint8_t* external_pixel_pointer();
2916
2917 // Setter and getter.
2918 inline uint8_t get(int index);
2919 inline void set(int index, uint8_t value);
2920
2921 // This accessor applies the correct conversion from Smi, HeapNumber and
2922 // undefined and clamps the converted value between 0 and 255.
2923 Object* SetValue(uint32_t index, Object* value);
2924
2925 // Casting.
2926 static inline ExternalPixelArray* cast(Object* obj);
2927
2928#ifdef OBJECT_PRINT
2929 inline void ExternalPixelArrayPrint() {
2930 ExternalPixelArrayPrint(stdout);
2931 }
2932 void ExternalPixelArrayPrint(FILE* out);
2933#endif
2934#ifdef DEBUG
2935 void ExternalPixelArrayVerify();
2936#endif // DEBUG
2937
2938 private:
2939 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
2940};
2941
2942
Steve Block3ce2e202009-11-05 08:53:23 +00002943class ExternalByteArray: public ExternalArray {
2944 public:
2945 // Setter and getter.
2946 inline int8_t get(int index);
2947 inline void set(int index, int8_t value);
2948
2949 // This accessor applies the correct conversion from Smi, HeapNumber
2950 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002951 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002952
2953 // Casting.
2954 static inline ExternalByteArray* cast(Object* obj);
2955
Ben Murdochb0fe1622011-05-05 13:52:32 +01002956#ifdef OBJECT_PRINT
2957 inline void ExternalByteArrayPrint() {
2958 ExternalByteArrayPrint(stdout);
2959 }
2960 void ExternalByteArrayPrint(FILE* out);
2961#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002962#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002963 void ExternalByteArrayVerify();
2964#endif // DEBUG
2965
2966 private:
2967 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2968};
2969
2970
2971class ExternalUnsignedByteArray: public ExternalArray {
2972 public:
2973 // Setter and getter.
2974 inline uint8_t get(int index);
2975 inline void set(int index, uint8_t value);
2976
2977 // This accessor applies the correct conversion from Smi, HeapNumber
2978 // and undefined.
John Reck59135872010-11-02 12:39:01 -07002979 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00002980
2981 // Casting.
2982 static inline ExternalUnsignedByteArray* cast(Object* obj);
2983
Ben Murdochb0fe1622011-05-05 13:52:32 +01002984#ifdef OBJECT_PRINT
2985 inline void ExternalUnsignedByteArrayPrint() {
2986 ExternalUnsignedByteArrayPrint(stdout);
2987 }
2988 void ExternalUnsignedByteArrayPrint(FILE* out);
2989#endif
Steve Block3ce2e202009-11-05 08:53:23 +00002990#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00002991 void ExternalUnsignedByteArrayVerify();
2992#endif // DEBUG
2993
2994 private:
2995 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2996};
2997
2998
2999class ExternalShortArray: public ExternalArray {
3000 public:
3001 // Setter and getter.
3002 inline int16_t get(int index);
3003 inline void set(int index, int16_t value);
3004
3005 // This accessor applies the correct conversion from Smi, HeapNumber
3006 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003007 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003008
3009 // Casting.
3010 static inline ExternalShortArray* cast(Object* obj);
3011
Ben Murdochb0fe1622011-05-05 13:52:32 +01003012#ifdef OBJECT_PRINT
3013 inline void ExternalShortArrayPrint() {
3014 ExternalShortArrayPrint(stdout);
3015 }
3016 void ExternalShortArrayPrint(FILE* out);
3017#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003018#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003019 void ExternalShortArrayVerify();
3020#endif // DEBUG
3021
3022 private:
3023 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
3024};
3025
3026
3027class ExternalUnsignedShortArray: public ExternalArray {
3028 public:
3029 // Setter and getter.
3030 inline uint16_t get(int index);
3031 inline void set(int index, uint16_t value);
3032
3033 // This accessor applies the correct conversion from Smi, HeapNumber
3034 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003035 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003036
3037 // Casting.
3038 static inline ExternalUnsignedShortArray* cast(Object* obj);
3039
Ben Murdochb0fe1622011-05-05 13:52:32 +01003040#ifdef OBJECT_PRINT
3041 inline void ExternalUnsignedShortArrayPrint() {
3042 ExternalUnsignedShortArrayPrint(stdout);
3043 }
3044 void ExternalUnsignedShortArrayPrint(FILE* out);
3045#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003046#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003047 void ExternalUnsignedShortArrayVerify();
3048#endif // DEBUG
3049
3050 private:
3051 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3052};
3053
3054
3055class ExternalIntArray: public ExternalArray {
3056 public:
3057 // Setter and getter.
3058 inline int32_t get(int index);
3059 inline void set(int index, int32_t value);
3060
3061 // This accessor applies the correct conversion from Smi, HeapNumber
3062 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003063 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003064
3065 // Casting.
3066 static inline ExternalIntArray* cast(Object* obj);
3067
Ben Murdochb0fe1622011-05-05 13:52:32 +01003068#ifdef OBJECT_PRINT
3069 inline void ExternalIntArrayPrint() {
3070 ExternalIntArrayPrint(stdout);
3071 }
3072 void ExternalIntArrayPrint(FILE* out);
3073#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003074#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003075 void ExternalIntArrayVerify();
3076#endif // DEBUG
3077
3078 private:
3079 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3080};
3081
3082
3083class ExternalUnsignedIntArray: public ExternalArray {
3084 public:
3085 // Setter and getter.
3086 inline uint32_t get(int index);
3087 inline void set(int index, uint32_t value);
3088
3089 // This accessor applies the correct conversion from Smi, HeapNumber
3090 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003091 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003092
3093 // Casting.
3094 static inline ExternalUnsignedIntArray* cast(Object* obj);
3095
Ben Murdochb0fe1622011-05-05 13:52:32 +01003096#ifdef OBJECT_PRINT
3097 inline void ExternalUnsignedIntArrayPrint() {
3098 ExternalUnsignedIntArrayPrint(stdout);
3099 }
3100 void ExternalUnsignedIntArrayPrint(FILE* out);
3101#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003102#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003103 void ExternalUnsignedIntArrayVerify();
3104#endif // DEBUG
3105
3106 private:
3107 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3108};
3109
3110
3111class ExternalFloatArray: public ExternalArray {
3112 public:
3113 // Setter and getter.
3114 inline float get(int index);
3115 inline void set(int index, float value);
3116
3117 // This accessor applies the correct conversion from Smi, HeapNumber
3118 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003119 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003120
3121 // Casting.
3122 static inline ExternalFloatArray* cast(Object* obj);
3123
Ben Murdochb0fe1622011-05-05 13:52:32 +01003124#ifdef OBJECT_PRINT
3125 inline void ExternalFloatArrayPrint() {
3126 ExternalFloatArrayPrint(stdout);
3127 }
3128 void ExternalFloatArrayPrint(FILE* out);
3129#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003130#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003131 void ExternalFloatArrayVerify();
3132#endif // DEBUG
3133
3134 private:
3135 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3136};
3137
3138
Ben Murdoch257744e2011-11-30 15:57:28 +00003139class ExternalDoubleArray: public ExternalArray {
3140 public:
3141 // Setter and getter.
3142 inline double get(int index);
3143 inline void set(int index, double value);
3144
3145 // This accessor applies the correct conversion from Smi, HeapNumber
3146 // and undefined.
3147 MaybeObject* SetValue(uint32_t index, Object* value);
3148
3149 // Casting.
3150 static inline ExternalDoubleArray* cast(Object* obj);
3151
3152#ifdef OBJECT_PRINT
3153 inline void ExternalDoubleArrayPrint() {
3154 ExternalDoubleArrayPrint(stdout);
3155 }
3156 void ExternalDoubleArrayPrint(FILE* out);
3157#endif // OBJECT_PRINT
3158#ifdef DEBUG
3159 void ExternalDoubleArrayVerify();
3160#endif // DEBUG
3161
3162 private:
3163 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalDoubleArray);
3164};
3165
3166
Ben Murdochb0fe1622011-05-05 13:52:32 +01003167// DeoptimizationInputData is a fixed array used to hold the deoptimization
3168// data for code generated by the Hydrogen/Lithium compiler. It also
3169// contains information about functions that were inlined. If N different
3170// functions were inlined then first N elements of the literal array will
3171// contain these functions.
3172//
3173// It can be empty.
3174class DeoptimizationInputData: public FixedArray {
3175 public:
3176 // Layout description. Indices in the array.
3177 static const int kTranslationByteArrayIndex = 0;
3178 static const int kInlinedFunctionCountIndex = 1;
3179 static const int kLiteralArrayIndex = 2;
3180 static const int kOsrAstIdIndex = 3;
3181 static const int kOsrPcOffsetIndex = 4;
3182 static const int kFirstDeoptEntryIndex = 5;
3183
3184 // Offsets of deopt entry elements relative to the start of the entry.
3185 static const int kAstIdOffset = 0;
3186 static const int kTranslationIndexOffset = 1;
3187 static const int kArgumentsStackHeightOffset = 2;
3188 static const int kDeoptEntrySize = 3;
3189
3190 // Simple element accessors.
3191#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3192 type* name() { \
3193 return type::cast(get(k##name##Index)); \
3194 } \
3195 void Set##name(type* value) { \
3196 set(k##name##Index, value); \
3197 }
3198
3199 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3200 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3201 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3202 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3203 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3204
3205 // Unchecked accessor to be used during GC.
3206 FixedArray* UncheckedLiteralArray() {
3207 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3208 }
3209
3210#undef DEFINE_ELEMENT_ACCESSORS
3211
3212 // Accessors for elements of the ith deoptimization entry.
3213#define DEFINE_ENTRY_ACCESSORS(name, type) \
3214 type* name(int i) { \
3215 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3216 } \
3217 void Set##name(int i, type* value) { \
3218 set(IndexForEntry(i) + k##name##Offset, value); \
3219 }
3220
3221 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3222 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3223 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3224
3225#undef DEFINE_ENTRY_ACCESSORS
3226
3227 int DeoptCount() {
3228 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3229 }
3230
3231 // Allocates a DeoptimizationInputData.
3232 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3233 PretenureFlag pretenure);
3234
3235 // Casting.
3236 static inline DeoptimizationInputData* cast(Object* obj);
3237
3238#ifdef OBJECT_PRINT
3239 void DeoptimizationInputDataPrint(FILE* out);
3240#endif
3241
3242 private:
3243 static int IndexForEntry(int i) {
3244 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3245 }
3246
3247 static int LengthFor(int entry_count) {
3248 return IndexForEntry(entry_count);
3249 }
3250};
3251
3252
3253// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3254// data for code generated by the full compiler.
3255// The format of the these objects is
3256// [i * 2]: Ast ID for ith deoptimization.
3257// [i * 2 + 1]: PC and state of ith deoptimization
3258class DeoptimizationOutputData: public FixedArray {
3259 public:
3260 int DeoptPoints() { return length() / 2; }
3261 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3262 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3263 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3264 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3265
3266 static int LengthOfFixedArray(int deopt_points) {
3267 return deopt_points * 2;
3268 }
3269
3270 // Allocates a DeoptimizationOutputData.
3271 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3272 PretenureFlag pretenure);
3273
3274 // Casting.
3275 static inline DeoptimizationOutputData* cast(Object* obj);
3276
3277#ifdef OBJECT_PRINT
3278 void DeoptimizationOutputDataPrint(FILE* out);
3279#endif
3280};
3281
3282
Ben Murdochb8e0da22011-05-16 14:20:40 +01003283class SafepointEntry;
3284
3285
Steve Blocka7e24c12009-10-30 11:49:00 +00003286// Code describes objects with on-the-fly generated machine code.
3287class Code: public HeapObject {
3288 public:
3289 // Opaque data type for encapsulating code flags like kind, inline
3290 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003291 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3292 // enumeration type has correct value range (see Issue 830 for more details).
3293 enum Flags {
3294 FLAGS_MIN_VALUE = kMinInt,
3295 FLAGS_MAX_VALUE = kMaxInt
3296 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003297
3298 enum Kind {
3299 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003300 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003301 STUB,
3302 BUILTIN,
3303 LOAD_IC,
3304 KEYED_LOAD_IC,
3305 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003306 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003307 STORE_IC,
3308 KEYED_STORE_IC,
Ben Murdoch257744e2011-11-30 15:57:28 +00003309 UNARY_OP_IC,
3310 BINARY_OP_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003311 COMPARE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003312 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003313 // Flags.
3314
3315 // Pseudo-kinds.
3316 REGEXP = BUILTIN,
3317 FIRST_IC_KIND = LOAD_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003318 LAST_IC_KIND = COMPARE_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003319 };
3320
3321 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003322 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003323 };
3324
Ben Murdochb8e0da22011-05-16 14:20:40 +01003325 typedef int ExtraICState;
3326
3327 static const ExtraICState kNoExtraICState = 0;
3328
Steve Blocka7e24c12009-10-30 11:49:00 +00003329#ifdef ENABLE_DISASSEMBLER
3330 // Printing
3331 static const char* Kind2String(Kind kind);
3332 static const char* ICState2String(InlineCacheState state);
3333 static const char* PropertyType2String(PropertyType type);
Steve Block1e0659c2011-05-24 12:43:12 +01003334 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003335 inline void Disassemble(const char* name) {
3336 Disassemble(name, stdout);
3337 }
3338 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003339#endif // ENABLE_DISASSEMBLER
3340
3341 // [instruction_size]: Size of the native instructions
3342 inline int instruction_size();
3343 inline void set_instruction_size(int value);
3344
Leon Clarkeac952652010-07-15 11:15:24 +01003345 // [relocation_info]: Code relocation information
3346 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003347 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003348
Ben Murdochb0fe1622011-05-05 13:52:32 +01003349 // [deoptimization_data]: Array containing data for deopt.
3350 DECL_ACCESSORS(deoptimization_data, FixedArray)
3351
Ben Murdoch257744e2011-11-30 15:57:28 +00003352 // [code_flushing_candidate]: Field only used during garbage
3353 // collection to hold code flushing candidates. The contents of this
3354 // field does not have to be traced during garbage collection since
3355 // it is only used by the garbage collector itself.
3356 DECL_ACCESSORS(next_code_flushing_candidate, Object)
3357
Ben Murdochb0fe1622011-05-05 13:52:32 +01003358 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003359 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003360 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003361
Steve Blocka7e24c12009-10-30 11:49:00 +00003362 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003363
Steve Blocka7e24c12009-10-30 11:49:00 +00003364 // [flags]: Various code flags.
3365 inline Flags flags();
3366 inline void set_flags(Flags flags);
3367
3368 // [flags]: Access to specific code flags.
3369 inline Kind kind();
3370 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003371 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00003372 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
3373 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3374 inline int arguments_count(); // Only valid for call IC stubs.
3375
3376 // Testers for IC stub kinds.
3377 inline bool is_inline_cache_stub();
3378 inline bool is_load_stub() { return kind() == LOAD_IC; }
3379 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3380 inline bool is_store_stub() { return kind() == STORE_IC; }
3381 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3382 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003383 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdoch257744e2011-11-30 15:57:28 +00003384 inline bool is_unary_op_stub() {
3385 return kind() == UNARY_OP_IC;
3386 }
3387 inline bool is_binary_op_stub() {
3388 return kind() == BINARY_OP_IC;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003389 }
3390 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00003391
Steve Block6ded16b2010-05-10 14:33:55 +01003392 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003393 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003394 inline void set_major_key(int value);
3395
3396 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3397 inline bool optimizable();
3398 inline void set_optimizable(bool value);
3399
3400 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3401 // deoptimization support.
3402 inline bool has_deoptimization_support();
3403 inline void set_has_deoptimization_support(bool value);
3404
3405 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3406 // how long the function has been marked for OSR and therefore which
3407 // level of loop nesting we are willing to do on-stack replacement
3408 // for.
3409 inline void set_allow_osr_at_loop_nesting_level(int level);
3410 inline int allow_osr_at_loop_nesting_level();
3411
3412 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3413 // reserved in the code prologue.
3414 inline unsigned stack_slots();
3415 inline void set_stack_slots(unsigned slots);
3416
3417 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3418 // the instruction stream where the safepoint table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003419 inline unsigned safepoint_table_offset();
3420 inline void set_safepoint_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003421
3422 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3423 // instruction stream where the stack check table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003424 inline unsigned stack_check_table_offset();
3425 inline void set_stack_check_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003426
3427 // [check type]: For kind CALL_IC, tells how to check if the
3428 // receiver is valid for the given call.
3429 inline CheckType check_type();
3430 inline void set_check_type(CheckType value);
3431
Steve Block44f0eee2011-05-26 01:26:41 +01003432 // [external array type]: For kind KEYED_EXTERNAL_ARRAY_LOAD_IC and
3433 // KEYED_EXTERNAL_ARRAY_STORE_IC, identifies the type of external
3434 // array that the code stub is specialized for.
3435 inline ExternalArrayType external_array_type();
3436 inline void set_external_array_type(ExternalArrayType value);
3437
Ben Murdoch257744e2011-11-30 15:57:28 +00003438 // [type-recording unary op type]: For all UNARY_OP_IC.
3439 inline byte unary_op_type();
3440 inline void set_unary_op_type(byte value);
3441
Ben Murdochb0fe1622011-05-05 13:52:32 +01003442 // [type-recording binary op type]: For all TYPE_RECORDING_BINARY_OP_IC.
Ben Murdoch257744e2011-11-30 15:57:28 +00003443 inline byte binary_op_type();
3444 inline void set_binary_op_type(byte value);
3445 inline byte binary_op_result_type();
3446 inline void set_binary_op_result_type(byte value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003447
3448 // [compare state]: For kind compare IC stubs, tells what state the
3449 // stub is in.
3450 inline byte compare_state();
3451 inline void set_compare_state(byte value);
3452
Ben Murdochb8e0da22011-05-16 14:20:40 +01003453 // Get the safepoint entry for the given pc.
3454 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003455
3456 // Mark this code object as not having a stack check table. Assumes kind
3457 // is FUNCTION.
3458 void SetNoStackCheckTable();
3459
3460 // Find the first map in an IC stub.
3461 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003462
3463 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003464 static inline Flags ComputeFlags(
3465 Kind kind,
3466 InLoopFlag in_loop = NOT_IN_LOOP,
3467 InlineCacheState ic_state = UNINITIALIZED,
3468 ExtraICState extra_ic_state = kNoExtraICState,
3469 PropertyType type = NORMAL,
3470 int argc = -1,
3471 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003472
3473 static inline Flags ComputeMonomorphicFlags(
3474 Kind kind,
3475 PropertyType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003476 ExtraICState extra_ic_state = kNoExtraICState,
Steve Block8defd9f2010-07-08 12:39:36 +01003477 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003478 InLoopFlag in_loop = NOT_IN_LOOP,
3479 int argc = -1);
3480
3481 static inline Kind ExtractKindFromFlags(Flags flags);
3482 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003483 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003484 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
3485 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3486 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003487 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003488 static inline Flags RemoveTypeFromFlags(Flags flags);
3489
3490 // Convert a target address into a code object.
3491 static inline Code* GetCodeFromTargetAddress(Address address);
3492
Steve Block791712a2010-08-27 10:21:07 +01003493 // Convert an entry address into an object.
3494 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3495
Steve Blocka7e24c12009-10-30 11:49:00 +00003496 // Returns the address of the first instruction.
3497 inline byte* instruction_start();
3498
Leon Clarkeac952652010-07-15 11:15:24 +01003499 // Returns the address right after the last instruction.
3500 inline byte* instruction_end();
3501
Steve Blocka7e24c12009-10-30 11:49:00 +00003502 // Returns the size of the instructions, padding, and relocation information.
3503 inline int body_size();
3504
3505 // Returns the address of the first relocation info (read backwards!).
3506 inline byte* relocation_start();
3507
3508 // Code entry point.
3509 inline byte* entry();
3510
3511 // Returns true if pc is inside this object's instructions.
3512 inline bool contains(byte* pc);
3513
Steve Blocka7e24c12009-10-30 11:49:00 +00003514 // Relocate the code by delta bytes. Called to signal that this code
3515 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003516 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003517
3518 // Migrate code described by desc.
3519 void CopyFrom(const CodeDesc& desc);
3520
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003521 // Returns the object size for a given body (used for allocation).
3522 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003523 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003524 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003525 }
3526
3527 // Calculate the size of the code object to report for log events. This takes
3528 // the layout of the code object into account.
3529 int ExecutableSize() {
3530 // Check that the assumptions about the layout of the code object holds.
3531 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3532 Code::kHeaderSize);
3533 return instruction_size() + Code::kHeaderSize;
3534 }
3535
3536 // Locating source position.
3537 int SourcePosition(Address pc);
3538 int SourceStatementPosition(Address pc);
3539
3540 // Casting.
3541 static inline Code* cast(Object* obj);
3542
3543 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003544 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003545 inline void CodeIterateBody(ObjectVisitor* v);
3546
3547 template<typename StaticVisitor>
Steve Block44f0eee2011-05-26 01:26:41 +01003548 inline void CodeIterateBody(Heap* heap);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003549#ifdef OBJECT_PRINT
3550 inline void CodePrint() {
3551 CodePrint(stdout);
3552 }
3553 void CodePrint(FILE* out);
3554#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003555#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003556 void CodeVerify();
3557#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003558
Ben Murdoch8b112d22011-06-08 16:22:53 +01003559 // Returns the isolate/heap this code object belongs to.
3560 inline Isolate* isolate();
3561 inline Heap* heap();
3562
Ben Murdochb0fe1622011-05-05 13:52:32 +01003563 // Max loop nesting marker used to postpose OSR. We don't take loop
3564 // nesting that is deeper than 5 levels into account.
3565 static const int kMaxLoopNestingMarker = 6;
3566
Steve Blocka7e24c12009-10-30 11:49:00 +00003567 // Layout description.
3568 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003569 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003570 static const int kDeoptimizationDataOffset =
3571 kRelocationInfoOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00003572 static const int kNextCodeFlushingCandidateOffset =
3573 kDeoptimizationDataOffset + kPointerSize;
3574 static const int kFlagsOffset =
3575 kNextCodeFlushingCandidateOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003576
Ben Murdoch257744e2011-11-30 15:57:28 +00003577 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003578 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3579
3580 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3581 kKindSpecificFlagsSize;
3582
Steve Blocka7e24c12009-10-30 11:49:00 +00003583 // Add padding to align the instruction start following right after
3584 // the Code object header.
3585 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003586 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003587
3588 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003589 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3590 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3591 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3592 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
Steve Block44f0eee2011-05-26 01:26:41 +01003593 static const int kExternalArrayTypeOffset = kKindSpecificFlagsOffset;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003594
3595 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00003596 static const int kUnaryOpTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003597 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
3598 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3599
3600 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3601 static const int kAllowOSRAtLoopNestingLevelOffset =
3602 kHasDeoptimizationSupportOffset + 1;
3603
Steve Block1e0659c2011-05-24 12:43:12 +01003604 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3605 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003606
3607 // Flags layout.
3608 static const int kFlagsICStateShift = 0;
3609 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003610 static const int kFlagsTypeShift = 4;
Steve Block44f0eee2011-05-26 01:26:41 +01003611 static const int kFlagsKindShift = 8;
3612 static const int kFlagsICHolderShift = 12;
3613 static const int kFlagsExtraICStateShift = 13;
3614 static const int kFlagsArgumentsCountShift = 15;
Steve Blocka7e24c12009-10-30 11:49:00 +00003615
Steve Block6ded16b2010-05-10 14:33:55 +01003616 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3617 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Steve Block44f0eee2011-05-26 01:26:41 +01003618 static const int kFlagsTypeMask = 0x000000F0; // 00001110000
3619 static const int kFlagsKindMask = 0x00000F00; // 11110000000
3620 static const int kFlagsCacheInPrototypeMapMask = 0x00001000;
3621 static const int kFlagsExtraICStateMask = 0x00006000;
3622 static const int kFlagsArgumentsCountMask = 0xFFFF8000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003623
3624 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003625 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003626
3627 private:
3628 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3629};
3630
3631
3632// All heap objects have a Map that describes their structure.
3633// A Map contains information about:
3634// - Size information about the object
3635// - How to iterate over an object (for garbage collection)
3636class Map: public HeapObject {
3637 public:
3638 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003639 // Size in bytes or kVariableSizeSentinel if instances do not have
3640 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003641 inline int instance_size();
3642 inline void set_instance_size(int value);
3643
3644 // Count of properties allocated in the object.
3645 inline int inobject_properties();
3646 inline void set_inobject_properties(int value);
3647
3648 // Count of property fields pre-allocated in the object when first allocated.
3649 inline int pre_allocated_property_fields();
3650 inline void set_pre_allocated_property_fields(int value);
3651
3652 // Instance type.
3653 inline InstanceType instance_type();
3654 inline void set_instance_type(InstanceType value);
3655
3656 // Tells how many unused property fields are available in the
3657 // instance (only used for JSObject in fast mode).
3658 inline int unused_property_fields();
3659 inline void set_unused_property_fields(int value);
3660
3661 // Bit field.
3662 inline byte bit_field();
3663 inline void set_bit_field(byte value);
3664
3665 // Bit field 2.
3666 inline byte bit_field2();
3667 inline void set_bit_field2(byte value);
3668
Ben Murdoch257744e2011-11-30 15:57:28 +00003669 // Bit field 3.
3670 // TODO(1399): It should be possible to make room for bit_field3 in the map
3671 // without overloading the instance descriptors field (and storing it in the
3672 // DescriptorArray when the map has one).
3673 inline int bit_field3();
3674 inline void set_bit_field3(int value);
3675
Steve Blocka7e24c12009-10-30 11:49:00 +00003676 // Tells whether the object in the prototype property will be used
3677 // for instances created from this function. If the prototype
3678 // property is set to a value that is not a JSObject, the prototype
3679 // property will not be used to create instances of the function.
3680 // See ECMA-262, 13.2.2.
3681 inline void set_non_instance_prototype(bool value);
3682 inline bool has_non_instance_prototype();
3683
Steve Block6ded16b2010-05-10 14:33:55 +01003684 // Tells whether function has special prototype property. If not, prototype
3685 // property will not be created when accessed (will return undefined),
3686 // and construction from this function will not be allowed.
3687 inline void set_function_with_prototype(bool value);
3688 inline bool function_with_prototype();
3689
Steve Blocka7e24c12009-10-30 11:49:00 +00003690 // Tells whether the instance with this map should be ignored by the
3691 // __proto__ accessor.
3692 inline void set_is_hidden_prototype() {
3693 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3694 }
3695
3696 inline bool is_hidden_prototype() {
3697 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3698 }
3699
3700 // Records and queries whether the instance has a named interceptor.
3701 inline void set_has_named_interceptor() {
3702 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3703 }
3704
3705 inline bool has_named_interceptor() {
3706 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3707 }
3708
3709 // Records and queries whether the instance has an indexed interceptor.
3710 inline void set_has_indexed_interceptor() {
3711 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3712 }
3713
3714 inline bool has_indexed_interceptor() {
3715 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3716 }
3717
3718 // Tells whether the instance is undetectable.
3719 // An undetectable object is a special class of JSObject: 'typeof' operator
3720 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3721 // a normal JS object. It is useful for implementing undetectable
3722 // document.all in Firefox & Safari.
3723 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3724 inline void set_is_undetectable() {
3725 set_bit_field(bit_field() | (1 << kIsUndetectable));
3726 }
3727
3728 inline bool is_undetectable() {
3729 return ((1 << kIsUndetectable) & bit_field()) != 0;
3730 }
3731
Steve Blocka7e24c12009-10-30 11:49:00 +00003732 // Tells whether the instance has a call-as-function handler.
3733 inline void set_has_instance_call_handler() {
3734 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3735 }
3736
3737 inline bool has_instance_call_handler() {
3738 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3739 }
3740
Steve Block8defd9f2010-07-08 12:39:36 +01003741 inline void set_is_extensible(bool value);
3742 inline bool is_extensible();
3743
3744 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003745 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3746 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003747 if (value) {
3748 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3749 } else {
3750 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3751 }
Leon Clarkee46be812010-01-19 14:06:41 +00003752 }
3753
Iain Merrick75681382010-08-19 15:07:18 +01003754 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003755 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003756 }
3757
Steve Block1e0659c2011-05-24 12:43:12 +01003758 // Tells whether an instance has pixel array elements.
Steve Block44f0eee2011-05-26 01:26:41 +01003759 inline void set_has_external_array_elements(bool value) {
Steve Block1e0659c2011-05-24 12:43:12 +01003760 if (value) {
Steve Block44f0eee2011-05-26 01:26:41 +01003761 set_bit_field2(bit_field2() | (1 << kHasExternalArrayElements));
Steve Block1e0659c2011-05-24 12:43:12 +01003762 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003763 set_bit_field2(bit_field2() & ~(1 << kHasExternalArrayElements));
Steve Block1e0659c2011-05-24 12:43:12 +01003764 }
3765 }
3766
Steve Block44f0eee2011-05-26 01:26:41 +01003767 inline bool has_external_array_elements() {
3768 return ((1 << kHasExternalArrayElements) & bit_field2()) != 0;
Steve Block1e0659c2011-05-24 12:43:12 +01003769 }
3770
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003771 // Tells whether the map is attached to SharedFunctionInfo
3772 // (for inobject slack tracking).
3773 inline void set_attached_to_shared_function_info(bool value);
3774
3775 inline bool attached_to_shared_function_info();
3776
3777 // Tells whether the map is shared between objects that may have different
3778 // behavior. If true, the map should never be modified, instead a clone
3779 // should be created and modified.
3780 inline void set_is_shared(bool value);
3781
3782 inline bool is_shared();
3783
Steve Blocka7e24c12009-10-30 11:49:00 +00003784 // Tells whether the instance needs security checks when accessing its
3785 // properties.
3786 inline void set_is_access_check_needed(bool access_check_needed);
3787 inline bool is_access_check_needed();
3788
3789 // [prototype]: implicit prototype object.
3790 DECL_ACCESSORS(prototype, Object)
3791
3792 // [constructor]: points back to the function responsible for this map.
3793 DECL_ACCESSORS(constructor, Object)
3794
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003795 inline JSFunction* unchecked_constructor();
3796
Ben Murdoch257744e2011-11-30 15:57:28 +00003797 // Should only be called by the code that initializes map to set initial valid
3798 // value of the instance descriptor member.
3799 inline void init_instance_descriptors();
3800
Steve Blocka7e24c12009-10-30 11:49:00 +00003801 // [instance descriptors]: describes the object.
3802 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3803
Ben Murdoch257744e2011-11-30 15:57:28 +00003804 // Sets the instance descriptor array for the map to be an empty descriptor
3805 // array.
3806 inline void clear_instance_descriptors();
3807
Steve Blocka7e24c12009-10-30 11:49:00 +00003808 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003809 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003810
Steve Block053d10c2011-06-13 19:13:29 +01003811 // [prototype transitions]: cache of prototype transitions.
3812 // Prototype transition is a transition that happens
3813 // when we change object's prototype to a new one.
3814 // Cache format:
3815 // 0: finger - index of the first free cell in the cache
3816 // 1 + 2 * i: prototype
3817 // 2 + 2 * i: target map
3818 DECL_ACCESSORS(prototype_transitions, FixedArray)
3819 inline FixedArray* unchecked_prototype_transitions();
3820
Ben Murdochb0fe1622011-05-05 13:52:32 +01003821 // Lookup in the map's instance descriptors and fill out the result
3822 // with the given holder if the name is found. The holder may be
3823 // NULL when this function is used from the compiler.
3824 void LookupInDescriptors(JSObject* holder,
3825 String* name,
3826 LookupResult* result);
3827
John Reck59135872010-11-02 12:39:01 -07003828 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003829
John Reck59135872010-11-02 12:39:01 -07003830 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3831 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00003832
3833 // Returns a copy of the map, with all transitions dropped from the
3834 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07003835 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00003836
Steve Block8defd9f2010-07-08 12:39:36 +01003837 // Returns this map if it has the fast elements bit set, otherwise
3838 // returns a copy of the map, with all transitions dropped from the
3839 // descriptors and the fast elements bit set.
John Reck59135872010-11-02 12:39:01 -07003840 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003841
3842 // Returns this map if it has the fast elements bit cleared,
3843 // otherwise returns a copy of the map, with all transitions dropped
3844 // from the descriptors and the fast elements bit cleared.
John Reck59135872010-11-02 12:39:01 -07003845 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01003846
Steve Block44f0eee2011-05-26 01:26:41 +01003847 // Returns a new map with all transitions dropped from the descriptors and the
3848 // external array elements bit set.
3849 MUST_USE_RESULT MaybeObject* GetExternalArrayElementsMap(
3850 ExternalArrayType array_type,
3851 bool safe_to_add_transition);
Steve Block1e0659c2011-05-24 12:43:12 +01003852
Steve Blocka7e24c12009-10-30 11:49:00 +00003853 // Returns the property index for name (only valid for FAST MODE).
3854 int PropertyIndexFor(String* name);
3855
3856 // Returns the next free property index (only valid for FAST MODE).
3857 int NextFreePropertyIndex();
3858
3859 // Returns the number of properties described in instance_descriptors.
3860 int NumberOfDescribedProperties();
3861
3862 // Casting.
3863 static inline Map* cast(Object* obj);
3864
3865 // Locate an accessor in the instance descriptor.
3866 AccessorDescriptor* FindAccessor(String* name);
3867
3868 // Code cache operations.
3869
3870 // Clears the code cache.
Steve Block44f0eee2011-05-26 01:26:41 +01003871 inline void ClearCodeCache(Heap* heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00003872
3873 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07003874 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003875
3876 // Returns the found code or undefined if absent.
3877 Object* FindInCodeCache(String* name, Code::Flags flags);
3878
3879 // Returns the non-negative index of the code object if it is in the
3880 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003881 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003882
3883 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003884 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003885
3886 // For every transition in this map, makes the transition's
3887 // target's prototype pointer point back to this map.
3888 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3889 void CreateBackPointers();
3890
3891 // Set all map transitions from this map to dead maps to null.
3892 // Also, restore the original prototype on the targets of these
3893 // transitions, so that we do not process this map again while
3894 // following back pointers.
Steve Block44f0eee2011-05-26 01:26:41 +01003895 void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00003896
3897 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003898#ifdef OBJECT_PRINT
3899 inline void MapPrint() {
3900 MapPrint(stdout);
3901 }
3902 void MapPrint(FILE* out);
3903#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003904#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003905 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003906 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00003907#endif
3908
Iain Merrick75681382010-08-19 15:07:18 +01003909 inline int visitor_id();
3910 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003911
Steve Block44f0eee2011-05-26 01:26:41 +01003912 // Returns the isolate/heap this map belongs to.
3913 inline Isolate* isolate();
3914 inline Heap* heap();
3915
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003916 typedef void (*TraverseCallback)(Map* map, void* data);
3917
3918 void TraverseTransitionTree(TraverseCallback callback, void* data);
3919
Steve Block053d10c2011-06-13 19:13:29 +01003920 static const int kMaxCachedPrototypeTransitions = 256;
3921
3922 Object* GetPrototypeTransition(Object* prototype);
3923
3924 MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
3925
Steve Blocka7e24c12009-10-30 11:49:00 +00003926 static const int kMaxPreAllocatedPropertyFields = 255;
3927
3928 // Layout description.
3929 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3930 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3931 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3932 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00003933 // Storage for instance descriptors is overloaded to also contain additional
3934 // map flags when unused (bit_field3). When the map has instance descriptors,
3935 // the flags are transferred to the instance descriptor array and accessed
3936 // through an extra indirection.
3937 // TODO(1399): It should be possible to make room for bit_field3 in the map
3938 // without overloading the instance descriptors field, but the map is
3939 // currently perfectly aligned to 32 bytes and extending it at all would
3940 // double its size. After the increment GC work lands, this size restriction
3941 // could be loosened and bit_field3 moved directly back in the map.
3942 static const int kInstanceDescriptorsOrBitField3Offset =
Steve Blocka7e24c12009-10-30 11:49:00 +00003943 kConstructorOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00003944 static const int kCodeCacheOffset =
3945 kInstanceDescriptorsOrBitField3Offset + kPointerSize;
Steve Block053d10c2011-06-13 19:13:29 +01003946 static const int kPrototypeTransitionsOffset =
3947 kCodeCacheOffset + kPointerSize;
3948 static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003949 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3950
3951 // Layout of pointer fields. Heap iteration code relies on them
3952 // being continiously allocated.
3953 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3954 static const int kPointerFieldsEndOffset =
Steve Block053d10c2011-06-13 19:13:29 +01003955 Map::kPrototypeTransitionsOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003956
3957 // Byte offsets within kInstanceSizesOffset.
3958 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3959 static const int kInObjectPropertiesByte = 1;
3960 static const int kInObjectPropertiesOffset =
3961 kInstanceSizesOffset + kInObjectPropertiesByte;
3962 static const int kPreAllocatedPropertyFieldsByte = 2;
3963 static const int kPreAllocatedPropertyFieldsOffset =
3964 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003965 static const int kVisitorIdByte = 3;
3966 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00003967
3968 // Byte offsets within kInstanceAttributesOffset attributes.
3969 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3970 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3971 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3972 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3973
3974 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3975
3976 // Bit positions for bit field.
3977 static const int kUnused = 0; // To be used for marking recently used maps.
3978 static const int kHasNonInstancePrototype = 1;
3979 static const int kIsHiddenPrototype = 2;
3980 static const int kHasNamedInterceptor = 3;
3981 static const int kHasIndexedInterceptor = 4;
3982 static const int kIsUndetectable = 5;
3983 static const int kHasInstanceCallHandler = 6;
3984 static const int kIsAccessCheckNeeded = 7;
3985
3986 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003987 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003988 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003989 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003990 static const int kStringWrapperSafeForDefaultValueOf = 3;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003991 static const int kAttachedToSharedFunctionInfo = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +00003992 static const int kHasExternalArrayElements = 5;
3993
3994 // Bit positions for bit field 3
3995 static const int kIsShared = 1;
Steve Block6ded16b2010-05-10 14:33:55 +01003996
3997 // Layout of the default cache. It holds alternating name and code objects.
3998 static const int kCodeCacheEntrySize = 2;
3999 static const int kCodeCacheEntryNameOffset = 0;
4000 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00004001
Iain Merrick75681382010-08-19 15:07:18 +01004002 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
4003 kPointerFieldsEndOffset,
4004 kSize> BodyDescriptor;
4005
Steve Blocka7e24c12009-10-30 11:49:00 +00004006 private:
4007 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
4008};
4009
4010
4011// An abstract superclass, a marker class really, for simple structure classes.
Ben Murdoch257744e2011-11-30 15:57:28 +00004012// It doesn't carry much functionality but allows struct classes to be
Steve Blocka7e24c12009-10-30 11:49:00 +00004013// identified in the type system.
4014class Struct: public HeapObject {
4015 public:
4016 inline void InitializeBody(int object_size);
4017 static inline Struct* cast(Object* that);
4018};
4019
4020
4021// Script describes a script which has been added to the VM.
4022class Script: public Struct {
4023 public:
4024 // Script types.
4025 enum Type {
4026 TYPE_NATIVE = 0,
4027 TYPE_EXTENSION = 1,
4028 TYPE_NORMAL = 2
4029 };
4030
4031 // Script compilation types.
4032 enum CompilationType {
4033 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004034 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00004035 };
4036
4037 // [source]: the script source.
4038 DECL_ACCESSORS(source, Object)
4039
4040 // [name]: the script name.
4041 DECL_ACCESSORS(name, Object)
4042
4043 // [id]: the script id.
4044 DECL_ACCESSORS(id, Object)
4045
4046 // [line_offset]: script line offset in resource from where it was extracted.
4047 DECL_ACCESSORS(line_offset, Smi)
4048
4049 // [column_offset]: script column offset in resource from where it was
4050 // extracted.
4051 DECL_ACCESSORS(column_offset, Smi)
4052
4053 // [data]: additional data associated with this script.
4054 DECL_ACCESSORS(data, Object)
4055
4056 // [context_data]: context data for the context this script was compiled in.
4057 DECL_ACCESSORS(context_data, Object)
4058
4059 // [wrapper]: the wrapper cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00004060 DECL_ACCESSORS(wrapper, Foreign)
Steve Blocka7e24c12009-10-30 11:49:00 +00004061
4062 // [type]: the script type.
4063 DECL_ACCESSORS(type, Smi)
4064
4065 // [compilation]: how the the script was compiled.
4066 DECL_ACCESSORS(compilation_type, Smi)
4067
Steve Blockd0582a62009-12-15 09:54:21 +00004068 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00004069 DECL_ACCESSORS(line_ends, Object)
4070
Steve Blockd0582a62009-12-15 09:54:21 +00004071 // [eval_from_shared]: for eval scripts the shared funcion info for the
4072 // function from which eval was called.
4073 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004074
4075 // [eval_from_instructions_offset]: the instruction offset in the code for the
4076 // function from which eval was called where eval was called.
4077 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
4078
4079 static inline Script* cast(Object* obj);
4080
Steve Block3ce2e202009-11-05 08:53:23 +00004081 // If script source is an external string, check that the underlying
4082 // resource is accessible. Otherwise, always return true.
4083 inline bool HasValidSource();
4084
Ben Murdochb0fe1622011-05-05 13:52:32 +01004085#ifdef OBJECT_PRINT
4086 inline void ScriptPrint() {
4087 ScriptPrint(stdout);
4088 }
4089 void ScriptPrint(FILE* out);
4090#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004091#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004092 void ScriptVerify();
4093#endif
4094
4095 static const int kSourceOffset = HeapObject::kHeaderSize;
4096 static const int kNameOffset = kSourceOffset + kPointerSize;
4097 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4098 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
4099 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
4100 static const int kContextOffset = kDataOffset + kPointerSize;
4101 static const int kWrapperOffset = kContextOffset + kPointerSize;
4102 static const int kTypeOffset = kWrapperOffset + kPointerSize;
4103 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
4104 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
4105 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00004106 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004107 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00004108 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004109 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
4110
4111 private:
4112 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4113};
4114
4115
Ben Murdochb0fe1622011-05-05 13:52:32 +01004116// List of builtin functions we want to identify to improve code
4117// generation.
4118//
4119// Each entry has a name of a global object property holding an object
4120// optionally followed by ".prototype", a name of a builtin function
4121// on the object (the one the id is set for), and a label.
4122//
4123// Installation of ids for the selected builtin functions is handled
4124// by the bootstrapper.
4125//
4126// NOTE: Order is important: math functions should be at the end of
4127// the list and MathFloor should be the first math function.
4128#define FUNCTIONS_WITH_ID_LIST(V) \
4129 V(Array.prototype, push, ArrayPush) \
4130 V(Array.prototype, pop, ArrayPop) \
Ben Murdoch42effa52011-08-19 16:40:31 +01004131 V(Function.prototype, apply, FunctionApply) \
Ben Murdochb0fe1622011-05-05 13:52:32 +01004132 V(String.prototype, charCodeAt, StringCharCodeAt) \
4133 V(String.prototype, charAt, StringCharAt) \
4134 V(String, fromCharCode, StringFromCharCode) \
4135 V(Math, floor, MathFloor) \
4136 V(Math, round, MathRound) \
4137 V(Math, ceil, MathCeil) \
4138 V(Math, abs, MathAbs) \
4139 V(Math, log, MathLog) \
4140 V(Math, sin, MathSin) \
4141 V(Math, cos, MathCos) \
4142 V(Math, tan, MathTan) \
4143 V(Math, asin, MathASin) \
4144 V(Math, acos, MathACos) \
4145 V(Math, atan, MathATan) \
4146 V(Math, exp, MathExp) \
4147 V(Math, sqrt, MathSqrt) \
4148 V(Math, pow, MathPow)
4149
4150
4151enum BuiltinFunctionId {
4152#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
4153 k##name,
4154 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4155#undef DECLARE_FUNCTION_ID
4156 // Fake id for a special case of Math.pow. Note, it continues the
4157 // list of math functions.
4158 kMathPowHalf,
4159 kFirstMathFunctionId = kMathFloor
4160};
4161
4162
Steve Blocka7e24c12009-10-30 11:49:00 +00004163// SharedFunctionInfo describes the JSFunction information that can be
4164// shared by multiple instances of the function.
4165class SharedFunctionInfo: public HeapObject {
4166 public:
4167 // [name]: Function name.
4168 DECL_ACCESSORS(name, Object)
4169
4170 // [code]: Function code.
4171 DECL_ACCESSORS(code, Code)
4172
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004173 // [scope_info]: Scope info.
4174 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
4175
Steve Blocka7e24c12009-10-30 11:49:00 +00004176 // [construct stub]: Code stub for constructing instances of this function.
4177 DECL_ACCESSORS(construct_stub, Code)
4178
Iain Merrick75681382010-08-19 15:07:18 +01004179 inline Code* unchecked_code();
4180
Steve Blocka7e24c12009-10-30 11:49:00 +00004181 // Returns if this function has been compiled to native code yet.
4182 inline bool is_compiled();
4183
4184 // [length]: The function length - usually the number of declared parameters.
4185 // Use up to 2^30 parameters.
4186 inline int length();
4187 inline void set_length(int value);
4188
4189 // [formal parameter count]: The declared number of parameters.
4190 inline int formal_parameter_count();
4191 inline void set_formal_parameter_count(int value);
4192
4193 // Set the formal parameter count so the function code will be
4194 // called without using argument adaptor frames.
4195 inline void DontAdaptArguments();
4196
4197 // [expected_nof_properties]: Expected number of properties for the function.
4198 inline int expected_nof_properties();
4199 inline void set_expected_nof_properties(int value);
4200
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004201 // Inobject slack tracking is the way to reclaim unused inobject space.
4202 //
4203 // The instance size is initially determined by adding some slack to
4204 // expected_nof_properties (to allow for a few extra properties added
4205 // after the constructor). There is no guarantee that the extra space
4206 // will not be wasted.
4207 //
4208 // Here is the algorithm to reclaim the unused inobject space:
4209 // - Detect the first constructor call for this SharedFunctionInfo.
4210 // When it happens enter the "in progress" state: remember the
4211 // constructor's initial_map and install a special construct stub that
4212 // counts constructor calls.
4213 // - While the tracking is in progress create objects filled with
4214 // one_pointer_filler_map instead of undefined_value. This way they can be
4215 // resized quickly and safely.
4216 // - Once enough (kGenerousAllocationCount) objects have been created
4217 // compute the 'slack' (traverse the map transition tree starting from the
4218 // initial_map and find the lowest value of unused_property_fields).
4219 // - Traverse the transition tree again and decrease the instance size
4220 // of every map. Existing objects will resize automatically (they are
4221 // filled with one_pointer_filler_map). All further allocations will
4222 // use the adjusted instance size.
4223 // - Decrease expected_nof_properties so that an allocations made from
4224 // another context will use the adjusted instance size too.
4225 // - Exit "in progress" state by clearing the reference to the initial_map
4226 // and setting the regular construct stub (generic or inline).
4227 //
4228 // The above is the main event sequence. Some special cases are possible
4229 // while the tracking is in progress:
4230 //
4231 // - GC occurs.
4232 // Check if the initial_map is referenced by any live objects (except this
4233 // SharedFunctionInfo). If it is, continue tracking as usual.
4234 // If it is not, clear the reference and reset the tracking state. The
4235 // tracking will be initiated again on the next constructor call.
4236 //
4237 // - The constructor is called from another context.
4238 // Immediately complete the tracking, perform all the necessary changes
4239 // to maps. This is necessary because there is no efficient way to track
4240 // multiple initial_maps.
4241 // Proceed to create an object in the current context (with the adjusted
4242 // size).
4243 //
4244 // - A different constructor function sharing the same SharedFunctionInfo is
4245 // called in the same context. This could be another closure in the same
4246 // context, or the first function could have been disposed.
4247 // This is handled the same way as the previous case.
4248 //
4249 // Important: inobject slack tracking is not attempted during the snapshot
4250 // creation.
4251
Ben Murdochf87a2032010-10-22 12:50:53 +01004252 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004253
4254 // [construction_count]: Counter for constructor calls made during
4255 // the tracking phase.
4256 inline int construction_count();
4257 inline void set_construction_count(int value);
4258
4259 // [initial_map]: initial map of the first function called as a constructor.
4260 // Saved for the duration of the tracking phase.
4261 // This is a weak link (GC resets it to undefined_value if no other live
4262 // object reference this map).
4263 DECL_ACCESSORS(initial_map, Object)
4264
4265 // True if the initial_map is not undefined and the countdown stub is
4266 // installed.
4267 inline bool IsInobjectSlackTrackingInProgress();
4268
4269 // Starts the tracking.
4270 // Stores the initial map and installs the countdown stub.
4271 // IsInobjectSlackTrackingInProgress is normally true after this call,
4272 // except when tracking have not been started (e.g. the map has no unused
4273 // properties or the snapshot is being built).
4274 void StartInobjectSlackTracking(Map* map);
4275
4276 // Completes the tracking.
4277 // IsInobjectSlackTrackingInProgress is false after this call.
4278 void CompleteInobjectSlackTracking();
4279
4280 // Clears the initial_map before the GC marking phase to ensure the reference
4281 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4282 void DetachInitialMap();
4283
4284 // Restores the link to the initial map after the GC marking phase.
4285 // IsInobjectSlackTrackingInProgress is true after this call.
4286 void AttachInitialMap(Map* map);
4287
4288 // False if there are definitely no live objects created from this function.
4289 // True if live objects _may_ exist (existence not guaranteed).
4290 // May go back from true to false after GC.
4291 inline bool live_objects_may_exist();
4292
4293 inline void set_live_objects_may_exist(bool value);
4294
Steve Blocka7e24c12009-10-30 11:49:00 +00004295 // [instance class name]: class name for instances.
4296 DECL_ACCESSORS(instance_class_name, Object)
4297
Steve Block6ded16b2010-05-10 14:33:55 +01004298 // [function data]: This field holds some additional data for function.
4299 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004300 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004301 // In the long run we don't want all functions to have this field but
4302 // we can fix that when we have a better model for storing hidden data
4303 // on objects.
4304 DECL_ACCESSORS(function_data, Object)
4305
Steve Block6ded16b2010-05-10 14:33:55 +01004306 inline bool IsApiFunction();
4307 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004308 inline bool HasBuiltinFunctionId();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004309 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004310
Steve Blocka7e24c12009-10-30 11:49:00 +00004311 // [script info]: Script from which the function originates.
4312 DECL_ACCESSORS(script, Object)
4313
Steve Block6ded16b2010-05-10 14:33:55 +01004314 // [num_literals]: Number of literals used by this function.
4315 inline int num_literals();
4316 inline void set_num_literals(int value);
4317
Steve Blocka7e24c12009-10-30 11:49:00 +00004318 // [start_position_and_type]: Field used to store both the source code
4319 // position, whether or not the function is a function expression,
4320 // and whether or not the function is a toplevel function. The two
4321 // least significants bit indicates whether the function is an
4322 // expression and the rest contains the source code position.
4323 inline int start_position_and_type();
4324 inline void set_start_position_and_type(int value);
4325
4326 // [debug info]: Debug information.
4327 DECL_ACCESSORS(debug_info, Object)
4328
4329 // [inferred name]: Name inferred from variable or property
4330 // assignment of this function. Used to facilitate debugging and
4331 // profiling of JavaScript code written in OO style, where almost
4332 // all functions are anonymous but are assigned to object
4333 // properties.
4334 DECL_ACCESSORS(inferred_name, String)
4335
Ben Murdochf87a2032010-10-22 12:50:53 +01004336 // The function's name if it is non-empty, otherwise the inferred name.
4337 String* DebugName();
4338
Steve Blocka7e24c12009-10-30 11:49:00 +00004339 // Position of the 'function' token in the script source.
4340 inline int function_token_position();
4341 inline void set_function_token_position(int function_token_position);
4342
4343 // Position of this function in the script source.
4344 inline int start_position();
4345 inline void set_start_position(int start_position);
4346
4347 // End position of this function in the script source.
4348 inline int end_position();
4349 inline void set_end_position(int end_position);
4350
4351 // Is this function a function expression in the source code.
4352 inline bool is_expression();
4353 inline void set_is_expression(bool value);
4354
4355 // Is this function a top-level function (scripts, evals).
4356 inline bool is_toplevel();
4357 inline void set_is_toplevel(bool value);
4358
4359 // Bit field containing various information collected by the compiler to
4360 // drive optimization.
4361 inline int compiler_hints();
4362 inline void set_compiler_hints(int value);
4363
Ben Murdochb0fe1622011-05-05 13:52:32 +01004364 // A counter used to determine when to stress the deoptimizer with a
4365 // deopt.
4366 inline Smi* deopt_counter();
4367 inline void set_deopt_counter(Smi* counter);
4368
Steve Blocka7e24c12009-10-30 11:49:00 +00004369 // Add information on assignments of the form this.x = ...;
4370 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004371 bool has_only_simple_this_property_assignments,
4372 FixedArray* this_property_assignments);
4373
4374 // Clear information on assignments of the form this.x = ...;
4375 void ClearThisPropertyAssignmentsInfo();
4376
4377 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004378 // this.x = y; where y is either a constant or refers to an argument.
4379 inline bool has_only_simple_this_property_assignments();
4380
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004381 // Indicates if this function can be lazy compiled.
4382 // This is used to determine if we can safely flush code from a function
4383 // when doing GC if we expect that the function will no longer be used.
4384 inline bool allows_lazy_compilation();
4385 inline void set_allows_lazy_compilation(bool flag);
4386
Iain Merrick75681382010-08-19 15:07:18 +01004387 // Indicates how many full GCs this function has survived with assigned
4388 // code object. Used to determine when it is relatively safe to flush
4389 // this code object and replace it with lazy compilation stub.
4390 // Age is reset when GC notices that the code object is referenced
4391 // from the stack or compilation cache.
4392 inline int code_age();
4393 inline void set_code_age(int age);
4394
Ben Murdochb0fe1622011-05-05 13:52:32 +01004395 // Indicates whether optimizations have been disabled for this
4396 // shared function info. If a function is repeatedly optimized or if
4397 // we cannot optimize the function we disable optimization to avoid
4398 // spending time attempting to optimize it again.
4399 inline bool optimization_disabled();
4400 inline void set_optimization_disabled(bool value);
4401
Steve Block1e0659c2011-05-24 12:43:12 +01004402 // Indicates whether the function is a strict mode function.
4403 inline bool strict_mode();
4404 inline void set_strict_mode(bool value);
4405
Ben Murdoch257744e2011-11-30 15:57:28 +00004406 // Indicates whether the function is a native ES5 function.
4407 // These needs special threatment in .call and .apply since
4408 // null passed as the receiver should not be translated to the
4409 // global object.
4410 inline bool es5_native();
4411 inline void set_es5_native(bool value);
4412
Ben Murdochb0fe1622011-05-05 13:52:32 +01004413 // Indicates whether or not the code in the shared function support
4414 // deoptimization.
4415 inline bool has_deoptimization_support();
4416
4417 // Enable deoptimization support through recompiled code.
4418 void EnableDeoptimizationSupport(Code* recompiled);
4419
Ben Murdoch257744e2011-11-30 15:57:28 +00004420 // Disable (further) attempted optimization of all functions sharing this
4421 // shared function info. The function is the one we actually tried to
4422 // optimize.
4423 void DisableOptimization(JSFunction* function);
4424
Ben Murdochb0fe1622011-05-05 13:52:32 +01004425 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4426 // code, returns whether it asserted (i.e., always true if assertions are
4427 // disabled).
4428 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004429
Andrei Popescu402d9372010-02-26 13:31:12 +00004430 // Check whether a inlined constructor can be generated with the given
4431 // prototype.
4432 bool CanGenerateInlineConstructor(Object* prototype);
4433
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004434 // Prevents further attempts to generate inline constructors.
4435 // To be called if generation failed for any reason.
4436 void ForbidInlineConstructor();
4437
Steve Blocka7e24c12009-10-30 11:49:00 +00004438 // For functions which only contains this property assignments this provides
4439 // access to the names for the properties assigned.
4440 DECL_ACCESSORS(this_property_assignments, Object)
4441 inline int this_property_assignments_count();
4442 inline void set_this_property_assignments_count(int value);
4443 String* GetThisPropertyAssignmentName(int index);
4444 bool IsThisPropertyAssignmentArgument(int index);
4445 int GetThisPropertyAssignmentArgument(int index);
4446 Object* GetThisPropertyAssignmentConstant(int index);
4447
4448 // [source code]: Source code for the function.
4449 bool HasSourceCode();
4450 Object* GetSourceCode();
4451
Ben Murdochb0fe1622011-05-05 13:52:32 +01004452 inline int opt_count();
4453 inline void set_opt_count(int opt_count);
4454
4455 // Source size of this function.
4456 int SourceSize();
4457
Steve Blocka7e24c12009-10-30 11:49:00 +00004458 // Calculate the instance size.
4459 int CalculateInstanceSize();
4460
4461 // Calculate the number of in-object properties.
4462 int CalculateInObjectProperties();
4463
4464 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004465 // Set max_length to -1 for unlimited length.
4466 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004467#ifdef OBJECT_PRINT
4468 inline void SharedFunctionInfoPrint() {
4469 SharedFunctionInfoPrint(stdout);
4470 }
4471 void SharedFunctionInfoPrint(FILE* out);
4472#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004473#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004474 void SharedFunctionInfoVerify();
4475#endif
4476
4477 // Casting.
4478 static inline SharedFunctionInfo* cast(Object* obj);
4479
4480 // Constants.
4481 static const int kDontAdaptArgumentsSentinel = -1;
4482
4483 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004484 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004485 static const int kNameOffset = HeapObject::kHeaderSize;
4486 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004487 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4488 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004489 static const int kInstanceClassNameOffset =
4490 kConstructStubOffset + kPointerSize;
4491 static const int kFunctionDataOffset =
4492 kInstanceClassNameOffset + kPointerSize;
4493 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4494 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4495 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004496 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004497 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004498 static const int kThisPropertyAssignmentsOffset =
4499 kInitialMapOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004500 static const int kDeoptCounterOffset =
4501 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004502#if V8_HOST_ARCH_32_BIT
4503 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004504 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004505 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004506 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4507 static const int kExpectedNofPropertiesOffset =
4508 kFormalParameterCountOffset + kPointerSize;
4509 static const int kNumLiteralsOffset =
4510 kExpectedNofPropertiesOffset + kPointerSize;
4511 static const int kStartPositionAndTypeOffset =
4512 kNumLiteralsOffset + kPointerSize;
4513 static const int kEndPositionOffset =
4514 kStartPositionAndTypeOffset + kPointerSize;
4515 static const int kFunctionTokenPositionOffset =
4516 kEndPositionOffset + kPointerSize;
4517 static const int kCompilerHintsOffset =
4518 kFunctionTokenPositionOffset + kPointerSize;
4519 static const int kThisPropertyAssignmentsCountOffset =
4520 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004521 static const int kOptCountOffset =
4522 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004523 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004524 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004525#else
4526 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004527 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004528 // garbage collections.
4529 // To avoid wasting space on 64-bit architectures we use
4530 // the following trick: we group integer fields into pairs
4531 // First integer in each pair is shifted left by 1.
4532 // By doing this we guarantee that LSB of each kPointerSize aligned
4533 // word is not set and thus this word cannot be treated as pointer
4534 // to HeapObject during old space traversal.
4535 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004536 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004537 static const int kFormalParameterCountOffset =
4538 kLengthOffset + kIntSize;
4539
Steve Blocka7e24c12009-10-30 11:49:00 +00004540 static const int kExpectedNofPropertiesOffset =
4541 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004542 static const int kNumLiteralsOffset =
4543 kExpectedNofPropertiesOffset + kIntSize;
4544
4545 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004546 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004547 static const int kStartPositionAndTypeOffset =
4548 kEndPositionOffset + kIntSize;
4549
4550 static const int kFunctionTokenPositionOffset =
4551 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004552 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004553 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004554
Steve Blocka7e24c12009-10-30 11:49:00 +00004555 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004556 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004557 static const int kOptCountOffset =
4558 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004559
Steve Block6ded16b2010-05-10 14:33:55 +01004560 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004561 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004562
4563#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004564
4565 // The construction counter for inobject slack tracking is stored in the
4566 // most significant byte of compiler_hints which is otherwise unused.
4567 // Its offset depends on the endian-ness of the architecture.
4568#if __BYTE_ORDER == __LITTLE_ENDIAN
4569 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4570#elif __BYTE_ORDER == __BIG_ENDIAN
4571 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4572#else
4573#error Unknown byte ordering
4574#endif
4575
Steve Block6ded16b2010-05-10 14:33:55 +01004576 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004577
Iain Merrick75681382010-08-19 15:07:18 +01004578 typedef FixedBodyDescriptor<kNameOffset,
4579 kThisPropertyAssignmentsOffset + kPointerSize,
4580 kSize> BodyDescriptor;
4581
Steve Blocka7e24c12009-10-30 11:49:00 +00004582 // Bit positions in start_position_and_type.
4583 // The source code start position is in the 30 most significant bits of
4584 // the start_position_and_type field.
4585 static const int kIsExpressionBit = 0;
4586 static const int kIsTopLevelBit = 1;
4587 static const int kStartPositionShift = 2;
4588 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4589
4590 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00004591 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004592 static const int kAllowLazyCompilation = 1;
4593 static const int kLiveObjectsMayExist = 2;
4594 static const int kCodeAgeShift = 3;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004595 static const int kCodeAgeMask = 0x7;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004596 static const int kOptimizationDisabled = 6;
4597 static const int kStrictModeFunction = 7;
Ben Murdoch257744e2011-11-30 15:57:28 +00004598 static const int kES5Native = 8;
Steve Blocka7e24c12009-10-30 11:49:00 +00004599
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004600 private:
4601#if V8_HOST_ARCH_32_BIT
4602 // On 32 bit platforms, compiler hints is a smi.
4603 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4604 static const int kCompilerHintsSize = kPointerSize;
4605#else
4606 // On 64 bit platforms, compiler hints is not a smi, see comment above.
4607 static const int kCompilerHintsSmiTagSize = 0;
4608 static const int kCompilerHintsSize = kIntSize;
4609#endif
4610
4611 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00004612 // Constants for optimizing codegen for strict mode function and
4613 // es5 native tests.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004614 // Allows to use byte-widgh instructions.
4615 static const int kStrictModeBitWithinByte =
4616 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4617
Ben Murdoch257744e2011-11-30 15:57:28 +00004618 static const int kES5NativeBitWithinByte =
4619 (kES5Native + kCompilerHintsSmiTagSize) % kBitsPerByte;
4620
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004621#if __BYTE_ORDER == __LITTLE_ENDIAN
4622 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004623 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
4624 static const int kES5NativeByteOffset = kCompilerHintsOffset +
4625 (kES5Native + kCompilerHintsSmiTagSize) / kBitsPerByte;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004626#elif __BYTE_ORDER == __BIG_ENDIAN
4627 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004628 (kCompilerHintsSize - 1) -
4629 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
4630 static const int kES5NativeByteOffset = kCompilerHintsOffset +
4631 (kCompilerHintsSize - 1) -
4632 ((kES5Native + kCompilerHintsSmiTagSize) / kBitsPerByte);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004633#else
4634#error Unknown byte ordering
4635#endif
4636
4637 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00004638 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4639};
4640
4641
4642// JSFunction describes JavaScript functions.
4643class JSFunction: public JSObject {
4644 public:
4645 // [prototype_or_initial_map]:
4646 DECL_ACCESSORS(prototype_or_initial_map, Object)
4647
4648 // [shared_function_info]: The information about the function that
4649 // can be shared by instances.
4650 DECL_ACCESSORS(shared, SharedFunctionInfo)
4651
Iain Merrick75681382010-08-19 15:07:18 +01004652 inline SharedFunctionInfo* unchecked_shared();
4653
Steve Blocka7e24c12009-10-30 11:49:00 +00004654 // [context]: The context for this function.
4655 inline Context* context();
4656 inline Object* unchecked_context();
4657 inline void set_context(Object* context);
4658
4659 // [code]: The generated code object for this function. Executed
4660 // when the function is invoked, e.g. foo() or new foo(). See
4661 // [[Call]] and [[Construct]] description in ECMA-262, section
4662 // 8.6.2, page 27.
4663 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004664 inline void set_code(Code* code);
4665 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004666
Iain Merrick75681382010-08-19 15:07:18 +01004667 inline Code* unchecked_code();
4668
Steve Blocka7e24c12009-10-30 11:49:00 +00004669 // Tells whether this function is builtin.
4670 inline bool IsBuiltin();
4671
Ben Murdochb0fe1622011-05-05 13:52:32 +01004672 // Tells whether or not the function needs arguments adaption.
4673 inline bool NeedsArgumentsAdaption();
4674
4675 // Tells whether or not this function has been optimized.
4676 inline bool IsOptimized();
4677
Ben Murdoch8b112d22011-06-08 16:22:53 +01004678 // Tells whether or not this function can be optimized.
4679 inline bool IsOptimizable();
4680
Ben Murdochb0fe1622011-05-05 13:52:32 +01004681 // Mark this function for lazy recompilation. The function will be
4682 // recompiled the next time it is executed.
4683 void MarkForLazyRecompilation();
4684
4685 // Tells whether or not the function is already marked for lazy
4686 // recompilation.
4687 inline bool IsMarkedForLazyRecompilation();
4688
4689 // Compute a hash code for the source code of this function.
4690 uint32_t SourceHash();
4691
4692 // Check whether or not this function is inlineable.
4693 bool IsInlineable();
4694
Steve Blocka7e24c12009-10-30 11:49:00 +00004695 // [literals]: Fixed array holding the materialized literals.
4696 //
4697 // If the function contains object, regexp or array literals, the
4698 // literals array prefix contains the object, regexp, and array
4699 // function to be used when creating these literals. This is
4700 // necessary so that we do not dynamically lookup the object, regexp
4701 // or array functions. Performing a dynamic lookup, we might end up
4702 // using the functions from a new context that we should not have
4703 // access to.
4704 DECL_ACCESSORS(literals, FixedArray)
4705
4706 // The initial map for an object created by this constructor.
4707 inline Map* initial_map();
4708 inline void set_initial_map(Map* value);
4709 inline bool has_initial_map();
4710
4711 // Get and set the prototype property on a JSFunction. If the
4712 // function has an initial map the prototype is set on the initial
4713 // map. Otherwise, the prototype is put in the initial map field
4714 // until an initial map is needed.
4715 inline bool has_prototype();
4716 inline bool has_instance_prototype();
4717 inline Object* prototype();
4718 inline Object* instance_prototype();
4719 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07004720 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004721
Steve Block6ded16b2010-05-10 14:33:55 +01004722 // After prototype is removed, it will not be created when accessed, and
4723 // [[Construct]] from this function will not be allowed.
4724 Object* RemovePrototype();
4725 inline bool should_have_prototype();
4726
Steve Blocka7e24c12009-10-30 11:49:00 +00004727 // Accessor for this function's initial map's [[class]]
4728 // property. This is primarily used by ECMA native functions. This
4729 // method sets the class_name field of this function's initial map
4730 // to a given value. It creates an initial map if this function does
4731 // not have one. Note that this method does not copy the initial map
4732 // if it has one already, but simply replaces it with the new value.
4733 // Instances created afterwards will have a map whose [[class]] is
4734 // set to 'value', but there is no guarantees on instances created
4735 // before.
4736 Object* SetInstanceClassName(String* name);
4737
4738 // Returns if this function has been compiled to native code yet.
4739 inline bool is_compiled();
4740
Ben Murdochb0fe1622011-05-05 13:52:32 +01004741 // [next_function_link]: Field for linking functions. This list is treated as
4742 // a weak list by the GC.
4743 DECL_ACCESSORS(next_function_link, Object)
4744
4745 // Prints the name of the function using PrintF.
4746 inline void PrintName() {
4747 PrintName(stdout);
4748 }
4749 void PrintName(FILE* out);
4750
Steve Blocka7e24c12009-10-30 11:49:00 +00004751 // Casting.
4752 static inline JSFunction* cast(Object* obj);
4753
Steve Block791712a2010-08-27 10:21:07 +01004754 // Iterates the objects, including code objects indirectly referenced
4755 // through pointers to the first instruction in the code object.
4756 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
4757
Steve Blocka7e24c12009-10-30 11:49:00 +00004758 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004759#ifdef OBJECT_PRINT
4760 inline void JSFunctionPrint() {
4761 JSFunctionPrint(stdout);
4762 }
4763 void JSFunctionPrint(FILE* out);
4764#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004765#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004766 void JSFunctionVerify();
4767#endif
4768
4769 // Returns the number of allocated literals.
4770 inline int NumberOfLiterals();
4771
4772 // Retrieve the global context from a function's literal array.
4773 static Context* GlobalContextFromLiterals(FixedArray* literals);
4774
Ben Murdochb0fe1622011-05-05 13:52:32 +01004775 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
4776 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01004777 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01004778 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01004779 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004780 static const int kSharedFunctionInfoOffset =
4781 kPrototypeOrInitialMapOffset + kPointerSize;
4782 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
4783 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004784 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
4785 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
4786 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004787
4788 // Layout of the literals array.
4789 static const int kLiteralsPrefixSize = 1;
4790 static const int kLiteralGlobalContextIndex = 0;
4791 private:
4792 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
4793};
4794
4795
4796// JSGlobalProxy's prototype must be a JSGlobalObject or null,
4797// and the prototype is hidden. JSGlobalProxy always delegates
4798// property accesses to its prototype if the prototype is not null.
4799//
4800// A JSGlobalProxy can be reinitialized which will preserve its identity.
4801//
4802// Accessing a JSGlobalProxy requires security check.
4803
4804class JSGlobalProxy : public JSObject {
4805 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00004806 // [context]: the owner global context of this global proxy object.
Steve Blocka7e24c12009-10-30 11:49:00 +00004807 // It is null value if this object is not used by any context.
4808 DECL_ACCESSORS(context, Object)
4809
4810 // Casting.
4811 static inline JSGlobalProxy* cast(Object* obj);
4812
4813 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004814#ifdef OBJECT_PRINT
4815 inline void JSGlobalProxyPrint() {
4816 JSGlobalProxyPrint(stdout);
4817 }
4818 void JSGlobalProxyPrint(FILE* out);
4819#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004820#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004821 void JSGlobalProxyVerify();
4822#endif
4823
4824 // Layout description.
4825 static const int kContextOffset = JSObject::kHeaderSize;
4826 static const int kSize = kContextOffset + kPointerSize;
4827
4828 private:
4829
4830 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
4831};
4832
4833
4834// Forward declaration.
4835class JSBuiltinsObject;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004836class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00004837
4838// Common super class for JavaScript global objects and the special
4839// builtins global objects.
4840class GlobalObject: public JSObject {
4841 public:
4842 // [builtins]: the object holding the runtime routines written in JS.
4843 DECL_ACCESSORS(builtins, JSBuiltinsObject)
4844
4845 // [global context]: the global context corresponding to this global object.
4846 DECL_ACCESSORS(global_context, Context)
4847
4848 // [global receiver]: the global receiver object of the context
4849 DECL_ACCESSORS(global_receiver, JSObject)
4850
4851 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004852 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00004853
John Reck59135872010-11-02 12:39:01 -07004854 // This is like GetProperty, but is used when you know the lookup won't fail
4855 // by throwing an exception. This is for the debug and builtins global
4856 // objects, where it is known which properties can be expected to be present
4857 // on the object.
4858 Object* GetPropertyNoExceptionThrown(String* key) {
4859 Object* answer = GetProperty(key)->ToObjectUnchecked();
4860 return answer;
4861 }
4862
Steve Blocka7e24c12009-10-30 11:49:00 +00004863 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07004864 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00004865
4866 // Casting.
4867 static inline GlobalObject* cast(Object* obj);
4868
4869 // Layout description.
4870 static const int kBuiltinsOffset = JSObject::kHeaderSize;
4871 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
4872 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
4873 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
4874
4875 private:
4876 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4877
4878 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
4879};
4880
4881
4882// JavaScript global object.
4883class JSGlobalObject: public GlobalObject {
4884 public:
4885
4886 // Casting.
4887 static inline JSGlobalObject* cast(Object* obj);
4888
4889 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004890#ifdef OBJECT_PRINT
4891 inline void JSGlobalObjectPrint() {
4892 JSGlobalObjectPrint(stdout);
4893 }
4894 void JSGlobalObjectPrint(FILE* out);
4895#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004896#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004897 void JSGlobalObjectVerify();
4898#endif
4899
4900 // Layout description.
4901 static const int kSize = GlobalObject::kHeaderSize;
4902
4903 private:
4904 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
4905};
4906
4907
4908// Builtins global object which holds the runtime routines written in
4909// JavaScript.
4910class JSBuiltinsObject: public GlobalObject {
4911 public:
4912 // Accessors for the runtime routines written in JavaScript.
4913 inline Object* javascript_builtin(Builtins::JavaScript id);
4914 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
4915
Steve Block6ded16b2010-05-10 14:33:55 +01004916 // Accessors for code of the runtime routines written in JavaScript.
4917 inline Code* javascript_builtin_code(Builtins::JavaScript id);
4918 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
4919
Steve Blocka7e24c12009-10-30 11:49:00 +00004920 // Casting.
4921 static inline JSBuiltinsObject* cast(Object* obj);
4922
4923 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004924#ifdef OBJECT_PRINT
4925 inline void JSBuiltinsObjectPrint() {
4926 JSBuiltinsObjectPrint(stdout);
4927 }
4928 void JSBuiltinsObjectPrint(FILE* out);
4929#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004930#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004931 void JSBuiltinsObjectVerify();
4932#endif
4933
4934 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01004935 // room for two pointers per runtime routine written in javascript
4936 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00004937 static const int kJSBuiltinsCount = Builtins::id_count;
4938 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004939 static const int kJSBuiltinsCodeOffset =
4940 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004941 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01004942 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
4943
4944 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
4945 return kJSBuiltinsOffset + id * kPointerSize;
4946 }
4947
4948 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
4949 return kJSBuiltinsCodeOffset + id * kPointerSize;
4950 }
4951
Steve Blocka7e24c12009-10-30 11:49:00 +00004952 private:
4953 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
4954};
4955
4956
4957// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
4958class JSValue: public JSObject {
4959 public:
4960 // [value]: the object being wrapped.
4961 DECL_ACCESSORS(value, Object)
4962
4963 // Casting.
4964 static inline JSValue* cast(Object* obj);
4965
4966 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004967#ifdef OBJECT_PRINT
4968 inline void JSValuePrint() {
4969 JSValuePrint(stdout);
4970 }
4971 void JSValuePrint(FILE* out);
4972#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004973#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004974 void JSValueVerify();
4975#endif
4976
4977 // Layout description.
4978 static const int kValueOffset = JSObject::kHeaderSize;
4979 static const int kSize = kValueOffset + kPointerSize;
4980
4981 private:
4982 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
4983};
4984
Steve Block1e0659c2011-05-24 12:43:12 +01004985
4986// Representation of message objects used for error reporting through
4987// the API. The messages are formatted in JavaScript so this object is
4988// a real JavaScript object. The information used for formatting the
4989// error messages are not directly accessible from JavaScript to
4990// prevent leaking information to user code called during error
4991// formatting.
4992class JSMessageObject: public JSObject {
4993 public:
4994 // [type]: the type of error message.
4995 DECL_ACCESSORS(type, String)
4996
4997 // [arguments]: the arguments for formatting the error message.
4998 DECL_ACCESSORS(arguments, JSArray)
4999
5000 // [script]: the script from which the error message originated.
5001 DECL_ACCESSORS(script, Object)
5002
5003 // [stack_trace]: the stack trace for this error message.
5004 DECL_ACCESSORS(stack_trace, Object)
5005
5006 // [stack_frames]: an array of stack frames for this error object.
5007 DECL_ACCESSORS(stack_frames, Object)
5008
5009 // [start_position]: the start position in the script for the error message.
5010 inline int start_position();
5011 inline void set_start_position(int value);
5012
5013 // [end_position]: the end position in the script for the error message.
5014 inline int end_position();
5015 inline void set_end_position(int value);
5016
5017 // Casting.
5018 static inline JSMessageObject* cast(Object* obj);
5019
5020 // Dispatched behavior.
5021#ifdef OBJECT_PRINT
5022 inline void JSMessageObjectPrint() {
5023 JSMessageObjectPrint(stdout);
5024 }
5025 void JSMessageObjectPrint(FILE* out);
5026#endif
5027#ifdef DEBUG
5028 void JSMessageObjectVerify();
5029#endif
5030
5031 // Layout description.
5032 static const int kTypeOffset = JSObject::kHeaderSize;
5033 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
5034 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
5035 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
5036 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
5037 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
5038 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
5039 static const int kSize = kEndPositionOffset + kPointerSize;
5040
5041 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
5042 kStackFramesOffset + kPointerSize,
5043 kSize> BodyDescriptor;
5044};
5045
5046
Steve Blocka7e24c12009-10-30 11:49:00 +00005047// Regular expressions
5048// The regular expression holds a single reference to a FixedArray in
5049// the kDataOffset field.
5050// The FixedArray contains the following data:
5051// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
5052// - reference to the original source string
5053// - reference to the original flag string
5054// If it is an atom regexp
5055// - a reference to a literal string to search for
5056// If it is an irregexp regexp:
Ben Murdoch257744e2011-11-30 15:57:28 +00005057// - a reference to code for ASCII inputs (bytecode or compiled), or a smi
5058// used for tracking the last usage (used for code flushing).
5059// - a reference to code for UC16 inputs (bytecode or compiled), or a smi
5060// used for tracking the last usage (used for code flushing)..
Steve Blocka7e24c12009-10-30 11:49:00 +00005061// - max number of registers used by irregexp implementations.
5062// - number of capture registers (output values) of the regexp.
5063class JSRegExp: public JSObject {
5064 public:
5065 // Meaning of Type:
5066 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
5067 // ATOM: A simple string to match against using an indexOf operation.
5068 // IRREGEXP: Compiled with Irregexp.
5069 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
5070 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
5071 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
5072
5073 class Flags {
5074 public:
5075 explicit Flags(uint32_t value) : value_(value) { }
5076 bool is_global() { return (value_ & GLOBAL) != 0; }
5077 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
5078 bool is_multiline() { return (value_ & MULTILINE) != 0; }
5079 uint32_t value() { return value_; }
5080 private:
5081 uint32_t value_;
5082 };
5083
5084 DECL_ACCESSORS(data, Object)
5085
5086 inline Type TypeTag();
5087 inline int CaptureCount();
5088 inline Flags GetFlags();
5089 inline String* Pattern();
5090 inline Object* DataAt(int index);
5091 // Set implementation data after the object has been prepared.
5092 inline void SetDataAt(int index, Object* value);
Ben Murdoch257744e2011-11-30 15:57:28 +00005093
5094 // Used during GC when flushing code or setting age.
5095 inline Object* DataAtUnchecked(int index);
5096 inline void SetDataAtUnchecked(int index, Object* value, Heap* heap);
5097 inline Type TypeTagUnchecked();
5098
Steve Blocka7e24c12009-10-30 11:49:00 +00005099 static int code_index(bool is_ascii) {
5100 if (is_ascii) {
5101 return kIrregexpASCIICodeIndex;
5102 } else {
5103 return kIrregexpUC16CodeIndex;
5104 }
5105 }
5106
Ben Murdoch257744e2011-11-30 15:57:28 +00005107 static int saved_code_index(bool is_ascii) {
5108 if (is_ascii) {
5109 return kIrregexpASCIICodeSavedIndex;
5110 } else {
5111 return kIrregexpUC16CodeSavedIndex;
5112 }
5113 }
5114
Steve Blocka7e24c12009-10-30 11:49:00 +00005115 static inline JSRegExp* cast(Object* obj);
5116
5117 // Dispatched behavior.
5118#ifdef DEBUG
5119 void JSRegExpVerify();
5120#endif
5121
5122 static const int kDataOffset = JSObject::kHeaderSize;
5123 static const int kSize = kDataOffset + kPointerSize;
5124
5125 // Indices in the data array.
5126 static const int kTagIndex = 0;
5127 static const int kSourceIndex = kTagIndex + 1;
5128 static const int kFlagsIndex = kSourceIndex + 1;
5129 static const int kDataIndex = kFlagsIndex + 1;
5130 // The data fields are used in different ways depending on the
5131 // value of the tag.
5132 // Atom regexps (literal strings).
5133 static const int kAtomPatternIndex = kDataIndex;
5134
5135 static const int kAtomDataSize = kAtomPatternIndex + 1;
5136
5137 // Irregexp compiled code or bytecode for ASCII. If compilation
5138 // fails, this fields hold an exception object that should be
5139 // thrown if the regexp is used again.
5140 static const int kIrregexpASCIICodeIndex = kDataIndex;
5141 // Irregexp compiled code or bytecode for UC16. If compilation
5142 // fails, this fields hold an exception object that should be
5143 // thrown if the regexp is used again.
5144 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00005145
5146 // Saved instance of Irregexp compiled code or bytecode for ASCII that
5147 // is a potential candidate for flushing.
5148 static const int kIrregexpASCIICodeSavedIndex = kDataIndex + 2;
5149 // Saved instance of Irregexp compiled code or bytecode for UC16 that is
5150 // a potential candidate for flushing.
5151 static const int kIrregexpUC16CodeSavedIndex = kDataIndex + 3;
5152
Steve Blocka7e24c12009-10-30 11:49:00 +00005153 // Maximal number of registers used by either ASCII or UC16.
5154 // Only used to check that there is enough stack space
Ben Murdoch257744e2011-11-30 15:57:28 +00005155 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +00005156 // Number of captures in the compiled regexp.
Ben Murdoch257744e2011-11-30 15:57:28 +00005157 static const int kIrregexpCaptureCountIndex = kDataIndex + 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00005158
5159 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00005160
5161 // Offsets directly into the data fixed array.
5162 static const int kDataTagOffset =
5163 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5164 static const int kDataAsciiCodeOffset =
5165 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00005166 static const int kDataUC16CodeOffset =
5167 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00005168 static const int kIrregexpCaptureCountOffset =
5169 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005170
5171 // In-object fields.
5172 static const int kSourceFieldIndex = 0;
5173 static const int kGlobalFieldIndex = 1;
5174 static const int kIgnoreCaseFieldIndex = 2;
5175 static const int kMultilineFieldIndex = 3;
5176 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01005177 static const int kInObjectFieldCount = 5;
Ben Murdoch257744e2011-11-30 15:57:28 +00005178
5179 // The uninitialized value for a regexp code object.
5180 static const int kUninitializedValue = -1;
5181
5182 // The compilation error value for the regexp code object. The real error
5183 // object is in the saved code field.
5184 static const int kCompilationErrorValue = -2;
5185
5186 // When we store the sweep generation at which we moved the code from the
5187 // code index to the saved code index we mask it of to be in the [0:255]
5188 // range.
5189 static const int kCodeAgeMask = 0xff;
Steve Blocka7e24c12009-10-30 11:49:00 +00005190};
5191
5192
5193class CompilationCacheShape {
5194 public:
5195 static inline bool IsMatch(HashTableKey* key, Object* value) {
5196 return key->IsMatch(value);
5197 }
5198
5199 static inline uint32_t Hash(HashTableKey* key) {
5200 return key->Hash();
5201 }
5202
5203 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5204 return key->HashForObject(object);
5205 }
5206
John Reck59135872010-11-02 12:39:01 -07005207 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005208 return key->AsObject();
5209 }
5210
5211 static const int kPrefixSize = 0;
5212 static const int kEntrySize = 2;
5213};
5214
Steve Block3ce2e202009-11-05 08:53:23 +00005215
Steve Blocka7e24c12009-10-30 11:49:00 +00005216class CompilationCacheTable: public HashTable<CompilationCacheShape,
5217 HashTableKey*> {
5218 public:
5219 // Find cached value for a string key, otherwise return null.
5220 Object* Lookup(String* src);
Steve Block1e0659c2011-05-24 12:43:12 +01005221 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00005222 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005223 MaybeObject* Put(String* src, Object* value);
Steve Block1e0659c2011-05-24 12:43:12 +01005224 MaybeObject* PutEval(String* src,
5225 Context* context,
5226 SharedFunctionInfo* value);
John Reck59135872010-11-02 12:39:01 -07005227 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005228
Ben Murdochb0fe1622011-05-05 13:52:32 +01005229 // Remove given value from cache.
5230 void Remove(Object* value);
5231
Steve Blocka7e24c12009-10-30 11:49:00 +00005232 static inline CompilationCacheTable* cast(Object* obj);
5233
5234 private:
5235 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
5236};
5237
5238
Steve Block6ded16b2010-05-10 14:33:55 +01005239class CodeCache: public Struct {
5240 public:
5241 DECL_ACCESSORS(default_cache, FixedArray)
5242 DECL_ACCESSORS(normal_type_cache, Object)
5243
5244 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07005245 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005246
5247 // Lookup code object in the cache. Returns code object if found and undefined
5248 // if not.
5249 Object* Lookup(String* name, Code::Flags flags);
5250
5251 // Get the internal index of a code object in the cache. Returns -1 if the
5252 // code object is not in that cache. This index can be used to later call
5253 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5254 // RemoveByIndex.
5255 int GetIndex(Object* name, Code* code);
5256
5257 // Remove an object from the cache with the provided internal index.
5258 void RemoveByIndex(Object* name, Code* code, int index);
5259
5260 static inline CodeCache* cast(Object* obj);
5261
Ben Murdochb0fe1622011-05-05 13:52:32 +01005262#ifdef OBJECT_PRINT
5263 inline void CodeCachePrint() {
5264 CodeCachePrint(stdout);
5265 }
5266 void CodeCachePrint(FILE* out);
5267#endif
Steve Block6ded16b2010-05-10 14:33:55 +01005268#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01005269 void CodeCacheVerify();
5270#endif
5271
5272 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5273 static const int kNormalTypeCacheOffset =
5274 kDefaultCacheOffset + kPointerSize;
5275 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5276
5277 private:
John Reck59135872010-11-02 12:39:01 -07005278 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5279 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005280 Object* LookupDefaultCache(String* name, Code::Flags flags);
5281 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5282
5283 // Code cache layout of the default cache. Elements are alternating name and
5284 // code objects for non normal load/store/call IC's.
5285 static const int kCodeCacheEntrySize = 2;
5286 static const int kCodeCacheEntryNameOffset = 0;
5287 static const int kCodeCacheEntryCodeOffset = 1;
5288
5289 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5290};
5291
5292
5293class CodeCacheHashTableShape {
5294 public:
5295 static inline bool IsMatch(HashTableKey* key, Object* value) {
5296 return key->IsMatch(value);
5297 }
5298
5299 static inline uint32_t Hash(HashTableKey* key) {
5300 return key->Hash();
5301 }
5302
5303 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5304 return key->HashForObject(object);
5305 }
5306
John Reck59135872010-11-02 12:39:01 -07005307 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01005308 return key->AsObject();
5309 }
5310
5311 static const int kPrefixSize = 0;
5312 static const int kEntrySize = 2;
5313};
5314
5315
5316class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5317 HashTableKey*> {
5318 public:
5319 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005320 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005321
5322 int GetIndex(String* name, Code::Flags flags);
5323 void RemoveByIndex(int index);
5324
5325 static inline CodeCacheHashTable* cast(Object* obj);
5326
5327 // Initial size of the fixed array backing the hash table.
5328 static const int kInitialSize = 64;
5329
5330 private:
5331 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5332};
5333
5334
Steve Blocka7e24c12009-10-30 11:49:00 +00005335enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5336enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5337
5338
5339class StringHasher {
5340 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +01005341 explicit inline StringHasher(int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005342
5343 // Returns true if the hash of this string can be computed without
5344 // looking at the contents.
5345 inline bool has_trivial_hash();
5346
5347 // Add a character to the hash and update the array index calculation.
5348 inline void AddCharacter(uc32 c);
5349
5350 // Adds a character to the hash but does not update the array index
5351 // calculation. This can only be called when it has been verified
5352 // that the input is not an array index.
5353 inline void AddCharacterNoIndex(uc32 c);
5354
5355 // Returns the value to store in the hash field of a string with
5356 // the given length and contents.
5357 uint32_t GetHashField();
5358
5359 // Returns true if the characters seen so far make up a legal array
5360 // index.
5361 bool is_array_index() { return is_array_index_; }
5362
5363 bool is_valid() { return is_valid_; }
5364
5365 void invalidate() { is_valid_ = false; }
5366
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005367 // Calculated hash value for a string consisting of 1 to
5368 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5369 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005370 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005371
Steve Blocka7e24c12009-10-30 11:49:00 +00005372 private:
5373
5374 uint32_t array_index() {
5375 ASSERT(is_array_index());
5376 return array_index_;
5377 }
5378
5379 inline uint32_t GetHash();
5380
5381 int length_;
5382 uint32_t raw_running_hash_;
5383 uint32_t array_index_;
5384 bool is_array_index_;
5385 bool is_first_char_;
5386 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00005387 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00005388};
5389
5390
Steve Block44f0eee2011-05-26 01:26:41 +01005391// Calculates string hash.
5392template <typename schar>
5393inline uint32_t HashSequentialString(const schar* chars, int length);
5394
5395
Steve Blocka7e24c12009-10-30 11:49:00 +00005396// The characteristics of a string are stored in its map. Retrieving these
5397// few bits of information is moderately expensive, involving two memory
5398// loads where the second is dependent on the first. To improve efficiency
5399// the shape of the string is given its own class so that it can be retrieved
5400// once and used for several string operations. A StringShape is small enough
5401// to be passed by value and is immutable, but be aware that flattening a
5402// string can potentially alter its shape. Also be aware that a GC caused by
5403// something else can alter the shape of a string due to ConsString
5404// shortcutting. Keeping these restrictions in mind has proven to be error-
5405// prone and so we no longer put StringShapes in variables unless there is a
5406// concrete performance benefit at that particular point in the code.
5407class StringShape BASE_EMBEDDED {
5408 public:
5409 inline explicit StringShape(String* s);
5410 inline explicit StringShape(Map* s);
5411 inline explicit StringShape(InstanceType t);
5412 inline bool IsSequential();
5413 inline bool IsExternal();
5414 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00005415 inline bool IsExternalAscii();
5416 inline bool IsExternalTwoByte();
5417 inline bool IsSequentialAscii();
5418 inline bool IsSequentialTwoByte();
5419 inline bool IsSymbol();
5420 inline StringRepresentationTag representation_tag();
5421 inline uint32_t full_representation_tag();
5422 inline uint32_t size_tag();
5423#ifdef DEBUG
5424 inline uint32_t type() { return type_; }
5425 inline void invalidate() { valid_ = false; }
5426 inline bool valid() { return valid_; }
5427#else
5428 inline void invalidate() { }
5429#endif
5430 private:
5431 uint32_t type_;
5432#ifdef DEBUG
5433 inline void set_valid() { valid_ = true; }
5434 bool valid_;
5435#else
5436 inline void set_valid() { }
5437#endif
5438};
5439
5440
5441// The String abstract class captures JavaScript string values:
5442//
5443// Ecma-262:
5444// 4.3.16 String Value
5445// A string value is a member of the type String and is a finite
5446// ordered sequence of zero or more 16-bit unsigned integer values.
5447//
5448// All string values have a length field.
5449class String: public HeapObject {
5450 public:
5451 // Get and set the length of the string.
5452 inline int length();
5453 inline void set_length(int value);
5454
Steve Blockd0582a62009-12-15 09:54:21 +00005455 // Get and set the hash field of the string.
5456 inline uint32_t hash_field();
5457 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005458
5459 inline bool IsAsciiRepresentation();
5460 inline bool IsTwoByteRepresentation();
5461
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005462 // Returns whether this string has ascii chars, i.e. all of them can
5463 // be ascii encoded. This might be the case even if the string is
5464 // two-byte. Such strings may appear when the embedder prefers
5465 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01005466 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005467 // NOTE: this should be considered only a hint. False negatives are
5468 // possible.
5469 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005470
Steve Blocka7e24c12009-10-30 11:49:00 +00005471 // Get and set individual two byte chars in the string.
5472 inline void Set(int index, uint16_t value);
5473 // Get individual two byte char in the string. Repeated calls
5474 // to this method are not efficient unless the string is flat.
5475 inline uint16_t Get(int index);
5476
Leon Clarkef7060e22010-06-03 12:02:55 +01005477 // Try to flatten the string. Checks first inline to see if it is
5478 // necessary. Does nothing if the string is not a cons string.
5479 // Flattening allocates a sequential string with the same data as
5480 // the given string and mutates the cons string to a degenerate
5481 // form, where the first component is the new sequential string and
5482 // the second component is the empty string. If allocation fails,
5483 // this function returns a failure. If flattening succeeds, this
5484 // function returns the sequential string that is now the first
5485 // component of the cons string.
5486 //
5487 // Degenerate cons strings are handled specially by the garbage
5488 // collector (see IsShortcutCandidate).
5489 //
5490 // Use FlattenString from Handles.cc to flatten even in case an
5491 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07005492 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005493
Leon Clarkef7060e22010-06-03 12:02:55 +01005494 // Convenience function. Has exactly the same behavior as
5495 // TryFlatten(), except in the case of failure returns the original
5496 // string.
5497 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5498
Steve Blocka7e24c12009-10-30 11:49:00 +00005499 Vector<const char> ToAsciiVector();
5500 Vector<const uc16> ToUC16Vector();
5501
5502 // Mark the string as an undetectable object. It only applies to
5503 // ascii and two byte string types.
5504 bool MarkAsUndetectable();
5505
Steve Blockd0582a62009-12-15 09:54:21 +00005506 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07005507 MUST_USE_RESULT MaybeObject* SubString(int from,
5508 int to,
5509 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005510
5511 // String equality operations.
5512 inline bool Equals(String* other);
5513 bool IsEqualTo(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +01005514 bool IsAsciiEqualTo(Vector<const char> str);
5515 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00005516
5517 // Return a UTF8 representation of the string. The string is null
5518 // terminated but may optionally contain nulls. Length is returned
5519 // in length_output if length_output is not a null pointer The string
5520 // should be nearly flat, otherwise the performance of this method may
5521 // be very slow (quadratic in the length). Setting robustness_flag to
5522 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5523 // handles unexpected data without causing assert failures and it does not
5524 // do any heap allocations. This is useful when printing stack traces.
5525 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5526 RobustnessFlag robustness_flag,
5527 int offset,
5528 int length,
5529 int* length_output = 0);
5530 SmartPointer<char> ToCString(
5531 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5532 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5533 int* length_output = 0);
5534
5535 int Utf8Length();
5536
5537 // Return a 16 bit Unicode representation of the string.
5538 // The string should be nearly flat, otherwise the performance of
5539 // of this method may be very bad. Setting robustness_flag to
5540 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5541 // handles unexpected data without causing assert failures and it does not
5542 // do any heap allocations. This is useful when printing stack traces.
5543 SmartPointer<uc16> ToWideCString(
5544 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
5545
5546 // Tells whether the hash code has been computed.
5547 inline bool HasHashCode();
5548
5549 // Returns a hash value used for the property table
5550 inline uint32_t Hash();
5551
Steve Blockd0582a62009-12-15 09:54:21 +00005552 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5553 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005554
5555 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5556 uint32_t* index,
5557 int length);
5558
5559 // Externalization.
5560 bool MakeExternal(v8::String::ExternalStringResource* resource);
5561 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5562
5563 // Conversion.
5564 inline bool AsArrayIndex(uint32_t* index);
5565
5566 // Casting.
5567 static inline String* cast(Object* obj);
5568
5569 void PrintOn(FILE* out);
5570
5571 // For use during stack traces. Performs rudimentary sanity check.
5572 bool LooksValid();
5573
5574 // Dispatched behavior.
5575 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005576#ifdef OBJECT_PRINT
5577 inline void StringPrint() {
5578 StringPrint(stdout);
5579 }
5580 void StringPrint(FILE* out);
5581#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005582#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005583 void StringVerify();
5584#endif
5585 inline bool IsFlat();
5586
5587 // Layout description.
5588 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005589 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005590 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005591
Steve Blockd0582a62009-12-15 09:54:21 +00005592 // Maximum number of characters to consider when trying to convert a string
5593 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00005594 static const int kMaxArrayIndexSize = 10;
5595
5596 // Max ascii char code.
5597 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
5598 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
5599 static const int kMaxUC16CharCode = 0xffff;
5600
Steve Blockd0582a62009-12-15 09:54:21 +00005601 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00005602 static const int kMinNonFlatLength = 13;
5603
5604 // Mask constant for checking if a string has a computed hash code
5605 // and if it is an array index. The least significant bit indicates
5606 // whether a hash code has been computed. If the hash code has been
5607 // computed the 2nd bit tells whether the string can be used as an
5608 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005609 static const int kHashNotComputedMask = 1;
5610 static const int kIsNotArrayIndexMask = 1 << 1;
5611 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00005612
Steve Blockd0582a62009-12-15 09:54:21 +00005613 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005614 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00005615
Steve Blocka7e24c12009-10-30 11:49:00 +00005616 // Array index strings this short can keep their index in the hash
5617 // field.
5618 static const int kMaxCachedArrayIndexLength = 7;
5619
Steve Blockd0582a62009-12-15 09:54:21 +00005620 // For strings which are array indexes the hash value has the string length
5621 // mixed into the hash, mainly to avoid a hash value of zero which would be
5622 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005623 static const int kArrayIndexValueBits = 24;
5624 static const int kArrayIndexLengthBits =
5625 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
5626
5627 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01005628 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005629
5630 static const int kArrayIndexHashLengthShift =
5631 kArrayIndexValueBits + kNofHashBitFields;
5632
Steve Blockd0582a62009-12-15 09:54:21 +00005633 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005634
5635 static const int kArrayIndexValueMask =
5636 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
5637
5638 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
5639 // could use a mask to test if the length of string is less than or equal to
5640 // kMaxCachedArrayIndexLength.
5641 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
5642
5643 static const int kContainsCachedArrayIndexMask =
5644 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
5645 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005646
5647 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005648 static const int kEmptyHashField =
5649 kIsNotArrayIndexMask | kHashNotComputedMask;
5650
5651 // Value of hash field containing computed hash equal to zero.
5652 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00005653
5654 // Maximal string length.
5655 static const int kMaxLength = (1 << (32 - 2)) - 1;
5656
5657 // Max length for computing hash. For strings longer than this limit the
5658 // string length is used as the hash value.
5659 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00005660
5661 // Limit for truncation in short printing.
5662 static const int kMaxShortPrintLength = 1024;
5663
5664 // Support for regular expressions.
5665 const uc16* GetTwoByteData();
5666 const uc16* GetTwoByteData(unsigned start);
5667
5668 // Support for StringInputBuffer
5669 static const unibrow::byte* ReadBlock(String* input,
5670 unibrow::byte* util_buffer,
5671 unsigned capacity,
5672 unsigned* remaining,
5673 unsigned* offset);
5674 static const unibrow::byte* ReadBlock(String** input,
5675 unibrow::byte* util_buffer,
5676 unsigned capacity,
5677 unsigned* remaining,
5678 unsigned* offset);
5679
5680 // Helper function for flattening strings.
5681 template <typename sinkchar>
5682 static void WriteToFlat(String* source,
5683 sinkchar* sink,
5684 int from,
5685 int to);
5686
Steve Block9fac8402011-05-12 15:51:54 +01005687 static inline bool IsAscii(const char* chars, int length) {
5688 const char* limit = chars + length;
5689#ifdef V8_HOST_CAN_READ_UNALIGNED
5690 ASSERT(kMaxAsciiCharCode == 0x7F);
5691 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
5692 while (chars <= limit - sizeof(uintptr_t)) {
5693 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
5694 return false;
5695 }
5696 chars += sizeof(uintptr_t);
5697 }
5698#endif
5699 while (chars < limit) {
5700 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
5701 ++chars;
5702 }
5703 return true;
5704 }
5705
5706 static inline bool IsAscii(const uc16* chars, int length) {
5707 const uc16* limit = chars + length;
5708 while (chars < limit) {
5709 if (*chars > kMaxAsciiCharCodeU) return false;
5710 ++chars;
5711 }
5712 return true;
5713 }
5714
Steve Blocka7e24c12009-10-30 11:49:00 +00005715 protected:
5716 class ReadBlockBuffer {
5717 public:
5718 ReadBlockBuffer(unibrow::byte* util_buffer_,
5719 unsigned cursor_,
5720 unsigned capacity_,
5721 unsigned remaining_) :
5722 util_buffer(util_buffer_),
5723 cursor(cursor_),
5724 capacity(capacity_),
5725 remaining(remaining_) {
5726 }
5727 unibrow::byte* util_buffer;
5728 unsigned cursor;
5729 unsigned capacity;
5730 unsigned remaining;
5731 };
5732
Steve Blocka7e24c12009-10-30 11:49:00 +00005733 static inline const unibrow::byte* ReadBlock(String* input,
5734 ReadBlockBuffer* buffer,
5735 unsigned* offset,
5736 unsigned max_chars);
5737 static void ReadBlockIntoBuffer(String* input,
5738 ReadBlockBuffer* buffer,
5739 unsigned* offset_ptr,
5740 unsigned max_chars);
5741
5742 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01005743 // Try to flatten the top level ConsString that is hiding behind this
5744 // string. This is a no-op unless the string is a ConsString. Flatten
5745 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07005746 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01005747
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005748 static inline bool IsHashFieldComputed(uint32_t field);
5749
Steve Blocka7e24c12009-10-30 11:49:00 +00005750 // Slow case of String::Equals. This implementation works on any strings
5751 // but it is most efficient on strings that are almost flat.
5752 bool SlowEquals(String* other);
5753
5754 // Slow case of AsArrayIndex.
5755 bool SlowAsArrayIndex(uint32_t* index);
5756
5757 // Compute and set the hash code.
5758 uint32_t ComputeAndSetHash();
5759
5760 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
5761};
5762
5763
5764// The SeqString abstract class captures sequential string values.
5765class SeqString: public String {
5766 public:
5767
5768 // Casting.
5769 static inline SeqString* cast(Object* obj);
5770
Steve Blocka7e24c12009-10-30 11:49:00 +00005771 private:
5772 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
5773};
5774
5775
5776// The AsciiString class captures sequential ascii string objects.
5777// Each character in the AsciiString is an ascii character.
5778class SeqAsciiString: public SeqString {
5779 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005780 static const bool kHasAsciiEncoding = true;
5781
Steve Blocka7e24c12009-10-30 11:49:00 +00005782 // Dispatched behavior.
5783 inline uint16_t SeqAsciiStringGet(int index);
5784 inline void SeqAsciiStringSet(int index, uint16_t value);
5785
5786 // Get the address of the characters in this string.
5787 inline Address GetCharsAddress();
5788
5789 inline char* GetChars();
5790
5791 // Casting
5792 static inline SeqAsciiString* cast(Object* obj);
5793
5794 // Garbage collection support. This method is called by the
5795 // garbage collector to compute the actual size of an AsciiString
5796 // instance.
5797 inline int SeqAsciiStringSize(InstanceType instance_type);
5798
5799 // Computes the size for an AsciiString instance of a given length.
5800 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005801 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005802 }
5803
5804 // Layout description.
5805 static const int kHeaderSize = String::kSize;
5806 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5807
Leon Clarkee46be812010-01-19 14:06:41 +00005808 // Maximal memory usage for a single sequential ASCII string.
5809 static const int kMaxSize = 512 * MB;
5810 // Maximal length of a single sequential ASCII string.
5811 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5812 static const int kMaxLength = (kMaxSize - kHeaderSize);
5813
Steve Blocka7e24c12009-10-30 11:49:00 +00005814 // Support for StringInputBuffer.
5815 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5816 unsigned* offset,
5817 unsigned chars);
5818 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
5819 unsigned* offset,
5820 unsigned chars);
5821
5822 private:
5823 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
5824};
5825
5826
5827// The TwoByteString class captures sequential unicode string objects.
5828// Each character in the TwoByteString is a two-byte uint16_t.
5829class SeqTwoByteString: public SeqString {
5830 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005831 static const bool kHasAsciiEncoding = false;
5832
Steve Blocka7e24c12009-10-30 11:49:00 +00005833 // Dispatched behavior.
5834 inline uint16_t SeqTwoByteStringGet(int index);
5835 inline void SeqTwoByteStringSet(int index, uint16_t value);
5836
5837 // Get the address of the characters in this string.
5838 inline Address GetCharsAddress();
5839
5840 inline uc16* GetChars();
5841
5842 // For regexp code.
5843 const uint16_t* SeqTwoByteStringGetData(unsigned start);
5844
5845 // Casting
5846 static inline SeqTwoByteString* cast(Object* obj);
5847
5848 // Garbage collection support. This method is called by the
5849 // garbage collector to compute the actual size of a TwoByteString
5850 // instance.
5851 inline int SeqTwoByteStringSize(InstanceType instance_type);
5852
5853 // Computes the size for a TwoByteString instance of a given length.
5854 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01005855 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005856 }
5857
5858 // Layout description.
5859 static const int kHeaderSize = String::kSize;
5860 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5861
Leon Clarkee46be812010-01-19 14:06:41 +00005862 // Maximal memory usage for a single sequential two-byte string.
5863 static const int kMaxSize = 512 * MB;
5864 // Maximal length of a single sequential two-byte string.
5865 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5866 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
5867
Steve Blocka7e24c12009-10-30 11:49:00 +00005868 // Support for StringInputBuffer.
5869 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5870 unsigned* offset_ptr,
5871 unsigned chars);
5872
5873 private:
5874 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
5875};
5876
5877
5878// The ConsString class describes string values built by using the
5879// addition operator on strings. A ConsString is a pair where the
5880// first and second components are pointers to other string values.
5881// One or both components of a ConsString can be pointers to other
5882// ConsStrings, creating a binary tree of ConsStrings where the leaves
5883// are non-ConsString string values. The string value represented by
5884// a ConsString can be obtained by concatenating the leaf string
5885// values in a left-to-right depth-first traversal of the tree.
5886class ConsString: public String {
5887 public:
5888 // First string of the cons cell.
5889 inline String* first();
5890 // Doesn't check that the result is a string, even in debug mode. This is
5891 // useful during GC where the mark bits confuse the checks.
5892 inline Object* unchecked_first();
5893 inline void set_first(String* first,
5894 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5895
5896 // Second string of the cons cell.
5897 inline String* second();
5898 // Doesn't check that the result is a string, even in debug mode. This is
5899 // useful during GC where the mark bits confuse the checks.
5900 inline Object* unchecked_second();
5901 inline void set_second(String* second,
5902 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5903
5904 // Dispatched behavior.
5905 uint16_t ConsStringGet(int index);
5906
5907 // Casting.
5908 static inline ConsString* cast(Object* obj);
5909
Steve Blocka7e24c12009-10-30 11:49:00 +00005910 // Layout description.
5911 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
5912 static const int kSecondOffset = kFirstOffset + kPointerSize;
5913 static const int kSize = kSecondOffset + kPointerSize;
5914
5915 // Support for StringInputBuffer.
5916 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
5917 unsigned* offset_ptr,
5918 unsigned chars);
5919 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5920 unsigned* offset_ptr,
5921 unsigned chars);
5922
5923 // Minimum length for a cons string.
5924 static const int kMinLength = 13;
5925
Iain Merrick75681382010-08-19 15:07:18 +01005926 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
5927 BodyDescriptor;
5928
Steve Blocka7e24c12009-10-30 11:49:00 +00005929 private:
5930 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
5931};
5932
5933
Steve Blocka7e24c12009-10-30 11:49:00 +00005934// The ExternalString class describes string values that are backed by
5935// a string resource that lies outside the V8 heap. ExternalStrings
5936// consist of the length field common to all strings, a pointer to the
5937// external resource. It is important to ensure (externally) that the
5938// resource is not deallocated while the ExternalString is live in the
5939// V8 heap.
5940//
5941// The API expects that all ExternalStrings are created through the
5942// API. Therefore, ExternalStrings should not be used internally.
5943class ExternalString: public String {
5944 public:
5945 // Casting
5946 static inline ExternalString* cast(Object* obj);
5947
5948 // Layout description.
5949 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
5950 static const int kSize = kResourceOffset + kPointerSize;
5951
5952 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
5953
5954 private:
5955 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
5956};
5957
5958
5959// The ExternalAsciiString class is an external string backed by an
5960// ASCII string.
5961class ExternalAsciiString: public ExternalString {
5962 public:
Leon Clarkeac952652010-07-15 11:15:24 +01005963 static const bool kHasAsciiEncoding = true;
5964
Steve Blocka7e24c12009-10-30 11:49:00 +00005965 typedef v8::String::ExternalAsciiStringResource Resource;
5966
5967 // The underlying resource.
5968 inline Resource* resource();
5969 inline void set_resource(Resource* buffer);
5970
5971 // Dispatched behavior.
5972 uint16_t ExternalAsciiStringGet(int index);
5973
5974 // Casting.
5975 static inline ExternalAsciiString* cast(Object* obj);
5976
Steve Blockd0582a62009-12-15 09:54:21 +00005977 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01005978 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
5979
5980 template<typename StaticVisitor>
5981 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00005982
Steve Blocka7e24c12009-10-30 11:49:00 +00005983 // Support for StringInputBuffer.
5984 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
5985 unsigned* offset,
5986 unsigned chars);
5987 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5988 unsigned* offset,
5989 unsigned chars);
5990
Steve Blocka7e24c12009-10-30 11:49:00 +00005991 private:
5992 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
5993};
5994
5995
5996// The ExternalTwoByteString class is an external string backed by a UTF-16
5997// encoded string.
5998class ExternalTwoByteString: public ExternalString {
5999 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006000 static const bool kHasAsciiEncoding = false;
6001
Steve Blocka7e24c12009-10-30 11:49:00 +00006002 typedef v8::String::ExternalStringResource Resource;
6003
6004 // The underlying string resource.
6005 inline Resource* resource();
6006 inline void set_resource(Resource* buffer);
6007
6008 // Dispatched behavior.
6009 uint16_t ExternalTwoByteStringGet(int index);
6010
6011 // For regexp code.
6012 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
6013
6014 // Casting.
6015 static inline ExternalTwoByteString* cast(Object* obj);
6016
Steve Blockd0582a62009-12-15 09:54:21 +00006017 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01006018 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
6019
6020 template<typename StaticVisitor>
6021 inline void ExternalTwoByteStringIterateBody();
6022
Steve Blockd0582a62009-12-15 09:54:21 +00006023
Steve Blocka7e24c12009-10-30 11:49:00 +00006024 // Support for StringInputBuffer.
6025 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6026 unsigned* offset_ptr,
6027 unsigned chars);
6028
Steve Blocka7e24c12009-10-30 11:49:00 +00006029 private:
6030 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
6031};
6032
6033
6034// Utility superclass for stack-allocated objects that must be updated
6035// on gc. It provides two ways for the gc to update instances, either
6036// iterating or updating after gc.
6037class Relocatable BASE_EMBEDDED {
6038 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006039 explicit inline Relocatable(Isolate* isolate);
6040 inline virtual ~Relocatable();
Steve Blocka7e24c12009-10-30 11:49:00 +00006041 virtual void IterateInstance(ObjectVisitor* v) { }
6042 virtual void PostGarbageCollection() { }
6043
6044 static void PostGarbageCollectionProcessing();
6045 static int ArchiveSpacePerThread();
Ben Murdoch257744e2011-11-30 15:57:28 +00006046 static char* ArchiveState(Isolate* isolate, char* to);
6047 static char* RestoreState(Isolate* isolate, char* from);
Steve Blocka7e24c12009-10-30 11:49:00 +00006048 static void Iterate(ObjectVisitor* v);
6049 static void Iterate(ObjectVisitor* v, Relocatable* top);
6050 static char* Iterate(ObjectVisitor* v, char* t);
6051 private:
Steve Block44f0eee2011-05-26 01:26:41 +01006052 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006053 Relocatable* prev_;
6054};
6055
6056
6057// A flat string reader provides random access to the contents of a
6058// string independent of the character width of the string. The handle
6059// must be valid as long as the reader is being used.
6060class FlatStringReader : public Relocatable {
6061 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006062 FlatStringReader(Isolate* isolate, Handle<String> str);
6063 FlatStringReader(Isolate* isolate, Vector<const char> input);
Steve Blocka7e24c12009-10-30 11:49:00 +00006064 void PostGarbageCollection();
6065 inline uc32 Get(int index);
6066 int length() { return length_; }
6067 private:
6068 String** str_;
6069 bool is_ascii_;
6070 int length_;
6071 const void* start_;
6072};
6073
6074
6075// Note that StringInputBuffers are not valid across a GC! To fix this
6076// it would have to store a String Handle instead of a String* and
6077// AsciiStringReadBlock would have to be modified to use memcpy.
6078//
6079// StringInputBuffer is able to traverse any string regardless of how
6080// deeply nested a sequence of ConsStrings it is made of. However,
6081// performance will be better if deep strings are flattened before they
6082// are traversed. Since flattening requires memory allocation this is
6083// not always desirable, however (esp. in debugging situations).
6084class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
6085 public:
6086 virtual void Seek(unsigned pos);
6087 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006088 explicit inline StringInputBuffer(String* backing):
Steve Blocka7e24c12009-10-30 11:49:00 +00006089 unibrow::InputBuffer<String, String*, 1024>(backing) {}
6090};
6091
6092
6093class SafeStringInputBuffer
6094 : public unibrow::InputBuffer<String, String**, 256> {
6095 public:
6096 virtual void Seek(unsigned pos);
6097 inline SafeStringInputBuffer()
6098 : unibrow::InputBuffer<String, String**, 256>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006099 explicit inline SafeStringInputBuffer(String** backing)
Steve Blocka7e24c12009-10-30 11:49:00 +00006100 : unibrow::InputBuffer<String, String**, 256>(backing) {}
6101};
6102
6103
6104template <typename T>
6105class VectorIterator {
6106 public:
6107 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
6108 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
6109 T GetNext() { return data_[index_++]; }
6110 bool has_more() { return index_ < data_.length(); }
6111 private:
6112 Vector<const T> data_;
6113 int index_;
6114};
6115
6116
6117// The Oddball describes objects null, undefined, true, and false.
6118class Oddball: public HeapObject {
6119 public:
6120 // [to_string]: Cached to_string computed at startup.
6121 DECL_ACCESSORS(to_string, String)
6122
6123 // [to_number]: Cached to_number computed at startup.
6124 DECL_ACCESSORS(to_number, Object)
6125
Steve Block44f0eee2011-05-26 01:26:41 +01006126 inline byte kind();
6127 inline void set_kind(byte kind);
6128
Steve Blocka7e24c12009-10-30 11:49:00 +00006129 // Casting.
6130 static inline Oddball* cast(Object* obj);
6131
6132 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00006133#ifdef DEBUG
6134 void OddballVerify();
6135#endif
6136
6137 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07006138 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
Steve Block44f0eee2011-05-26 01:26:41 +01006139 Object* to_number,
6140 byte kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00006141
6142 // Layout description.
6143 static const int kToStringOffset = HeapObject::kHeaderSize;
6144 static const int kToNumberOffset = kToStringOffset + kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +01006145 static const int kKindOffset = kToNumberOffset + kPointerSize;
6146 static const int kSize = kKindOffset + kPointerSize;
6147
6148 static const byte kFalse = 0;
6149 static const byte kTrue = 1;
6150 static const byte kNotBooleanMask = ~1;
6151 static const byte kTheHole = 2;
6152 static const byte kNull = 3;
6153 static const byte kArgumentMarker = 4;
6154 static const byte kUndefined = 5;
6155 static const byte kOther = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +00006156
Iain Merrick75681382010-08-19 15:07:18 +01006157 typedef FixedBodyDescriptor<kToStringOffset,
6158 kToNumberOffset + kPointerSize,
6159 kSize> BodyDescriptor;
6160
Steve Blocka7e24c12009-10-30 11:49:00 +00006161 private:
6162 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6163};
6164
6165
6166class JSGlobalPropertyCell: public HeapObject {
6167 public:
6168 // [value]: value of the global property.
6169 DECL_ACCESSORS(value, Object)
6170
6171 // Casting.
6172 static inline JSGlobalPropertyCell* cast(Object* obj);
6173
Steve Blocka7e24c12009-10-30 11:49:00 +00006174#ifdef DEBUG
6175 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006176#endif
6177#ifdef OBJECT_PRINT
6178 inline void JSGlobalPropertyCellPrint() {
6179 JSGlobalPropertyCellPrint(stdout);
6180 }
6181 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00006182#endif
6183
6184 // Layout description.
6185 static const int kValueOffset = HeapObject::kHeaderSize;
6186 static const int kSize = kValueOffset + kPointerSize;
6187
Iain Merrick75681382010-08-19 15:07:18 +01006188 typedef FixedBodyDescriptor<kValueOffset,
6189 kValueOffset + kPointerSize,
6190 kSize> BodyDescriptor;
6191
Ben Murdoch8b112d22011-06-08 16:22:53 +01006192 // Returns the isolate/heap this cell object belongs to.
6193 inline Isolate* isolate();
6194 inline Heap* heap();
6195
Steve Blocka7e24c12009-10-30 11:49:00 +00006196 private:
6197 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6198};
6199
6200
Ben Murdoch257744e2011-11-30 15:57:28 +00006201// The JSProxy describes EcmaScript Harmony proxies
6202class JSProxy: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00006203 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006204 // [handler]: The handler property.
6205 DECL_ACCESSORS(handler, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00006206
6207 // Casting.
Ben Murdoch257744e2011-11-30 15:57:28 +00006208 static inline JSProxy* cast(Object* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00006209
6210 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006211#ifdef OBJECT_PRINT
Ben Murdoch257744e2011-11-30 15:57:28 +00006212 inline void JSProxyPrint() {
6213 JSProxyPrint(stdout);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006214 }
Ben Murdoch257744e2011-11-30 15:57:28 +00006215 void JSProxyPrint(FILE* out);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006216#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006217#ifdef DEBUG
Ben Murdoch257744e2011-11-30 15:57:28 +00006218 void JSProxyVerify();
6219#endif
6220
6221 // Layout description.
6222 static const int kHandlerOffset = HeapObject::kHeaderSize;
6223 static const int kSize = kHandlerOffset + kPointerSize;
6224
6225 typedef FixedBodyDescriptor<kHandlerOffset,
6226 kHandlerOffset + kPointerSize,
6227 kSize> BodyDescriptor;
6228
6229 private:
6230 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxy);
6231};
6232
6233
6234
6235// Foreign describes objects pointing from JavaScript to C structures.
6236// Since they cannot contain references to JS HeapObjects they can be
6237// placed in old_data_space.
6238class Foreign: public HeapObject {
6239 public:
6240 // [address]: field containing the address.
6241 inline Address address();
6242 inline void set_address(Address value);
6243
6244 // Casting.
6245 static inline Foreign* cast(Object* obj);
6246
6247 // Dispatched behavior.
6248 inline void ForeignIterateBody(ObjectVisitor* v);
6249
6250 template<typename StaticVisitor>
6251 inline void ForeignIterateBody();
6252
6253#ifdef OBJECT_PRINT
6254 inline void ForeignPrint() {
6255 ForeignPrint(stdout);
6256 }
6257 void ForeignPrint(FILE* out);
6258#endif
6259#ifdef DEBUG
6260 void ForeignVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00006261#endif
6262
6263 // Layout description.
6264
Ben Murdoch257744e2011-11-30 15:57:28 +00006265 static const int kAddressOffset = HeapObject::kHeaderSize;
6266 static const int kSize = kAddressOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006267
Ben Murdoch257744e2011-11-30 15:57:28 +00006268 STATIC_CHECK(kAddressOffset == Internals::kForeignAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00006269
6270 private:
Ben Murdoch257744e2011-11-30 15:57:28 +00006271 DISALLOW_IMPLICIT_CONSTRUCTORS(Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +00006272};
6273
6274
6275// The JSArray describes JavaScript Arrays
6276// Such an array can be in one of two modes:
6277// - fast, backing storage is a FixedArray and length <= elements.length();
6278// Please note: push and pop can be used to grow and shrink the array.
6279// - slow, backing storage is a HashTable with numbers as keys.
6280class JSArray: public JSObject {
6281 public:
6282 // [length]: The length property.
6283 DECL_ACCESSORS(length, Object)
6284
Leon Clarke4515c472010-02-03 11:58:03 +00006285 // Overload the length setter to skip write barrier when the length
6286 // is set to a smi. This matches the set function on FixedArray.
6287 inline void set_length(Smi* length);
6288
John Reck59135872010-11-02 12:39:01 -07006289 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6290 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006291
6292 // Initialize the array with the given capacity. The function may
6293 // fail due to out-of-memory situations, but only if the requested
6294 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07006295 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00006296
6297 // Set the content of the array to the content of storage.
6298 inline void SetContent(FixedArray* storage);
6299
6300 // Casting.
6301 static inline JSArray* cast(Object* obj);
6302
6303 // Uses handles. Ensures that the fixed array backing the JSArray has at
6304 // least the stated size.
6305 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
6306
6307 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006308#ifdef OBJECT_PRINT
6309 inline void JSArrayPrint() {
6310 JSArrayPrint(stdout);
6311 }
6312 void JSArrayPrint(FILE* out);
6313#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006314#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006315 void JSArrayVerify();
6316#endif
6317
6318 // Number of element slots to pre-allocate for an empty array.
6319 static const int kPreallocatedArrayElements = 4;
6320
6321 // Layout description.
6322 static const int kLengthOffset = JSObject::kHeaderSize;
6323 static const int kSize = kLengthOffset + kPointerSize;
6324
6325 private:
6326 // Expand the fixed array backing of a fast-case JSArray to at least
6327 // the requested size.
6328 void Expand(int minimum_size_of_backing_fixed_array);
6329
6330 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6331};
6332
6333
Steve Block6ded16b2010-05-10 14:33:55 +01006334// JSRegExpResult is just a JSArray with a specific initial map.
6335// This initial map adds in-object properties for "index" and "input"
6336// properties, as assigned by RegExp.prototype.exec, which allows
6337// faster creation of RegExp exec results.
6338// This class just holds constants used when creating the result.
6339// After creation the result must be treated as a JSArray in all regards.
6340class JSRegExpResult: public JSArray {
6341 public:
6342 // Offsets of object fields.
6343 static const int kIndexOffset = JSArray::kSize;
6344 static const int kInputOffset = kIndexOffset + kPointerSize;
6345 static const int kSize = kInputOffset + kPointerSize;
6346 // Indices of in-object properties.
6347 static const int kIndexIndex = 0;
6348 static const int kInputIndex = 1;
6349 private:
6350 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6351};
6352
6353
Steve Blocka7e24c12009-10-30 11:49:00 +00006354// An accessor must have a getter, but can have no setter.
6355//
6356// When setting a property, V8 searches accessors in prototypes.
6357// If an accessor was found and it does not have a setter,
6358// the request is ignored.
6359//
6360// If the accessor in the prototype has the READ_ONLY property attribute, then
6361// a new value is added to the local object when the property is set.
6362// This shadows the accessor in the prototype.
6363class AccessorInfo: public Struct {
6364 public:
6365 DECL_ACCESSORS(getter, Object)
6366 DECL_ACCESSORS(setter, Object)
6367 DECL_ACCESSORS(data, Object)
6368 DECL_ACCESSORS(name, Object)
6369 DECL_ACCESSORS(flag, Smi)
6370
6371 inline bool all_can_read();
6372 inline void set_all_can_read(bool value);
6373
6374 inline bool all_can_write();
6375 inline void set_all_can_write(bool value);
6376
6377 inline bool prohibits_overwriting();
6378 inline void set_prohibits_overwriting(bool value);
6379
6380 inline PropertyAttributes property_attributes();
6381 inline void set_property_attributes(PropertyAttributes attributes);
6382
6383 static inline AccessorInfo* cast(Object* obj);
6384
Ben Murdochb0fe1622011-05-05 13:52:32 +01006385#ifdef OBJECT_PRINT
6386 inline void AccessorInfoPrint() {
6387 AccessorInfoPrint(stdout);
6388 }
6389 void AccessorInfoPrint(FILE* out);
6390#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006391#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006392 void AccessorInfoVerify();
6393#endif
6394
6395 static const int kGetterOffset = HeapObject::kHeaderSize;
6396 static const int kSetterOffset = kGetterOffset + kPointerSize;
6397 static const int kDataOffset = kSetterOffset + kPointerSize;
6398 static const int kNameOffset = kDataOffset + kPointerSize;
6399 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006400 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006401
6402 private:
6403 // Bit positions in flag.
6404 static const int kAllCanReadBit = 0;
6405 static const int kAllCanWriteBit = 1;
6406 static const int kProhibitsOverwritingBit = 2;
6407 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
6408
6409 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
6410};
6411
6412
6413class AccessCheckInfo: public Struct {
6414 public:
6415 DECL_ACCESSORS(named_callback, Object)
6416 DECL_ACCESSORS(indexed_callback, Object)
6417 DECL_ACCESSORS(data, Object)
6418
6419 static inline AccessCheckInfo* cast(Object* obj);
6420
Ben Murdochb0fe1622011-05-05 13:52:32 +01006421#ifdef OBJECT_PRINT
6422 inline void AccessCheckInfoPrint() {
6423 AccessCheckInfoPrint(stdout);
6424 }
6425 void AccessCheckInfoPrint(FILE* out);
6426#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006427#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006428 void AccessCheckInfoVerify();
6429#endif
6430
6431 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
6432 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
6433 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
6434 static const int kSize = kDataOffset + kPointerSize;
6435
6436 private:
6437 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
6438};
6439
6440
6441class InterceptorInfo: public Struct {
6442 public:
6443 DECL_ACCESSORS(getter, Object)
6444 DECL_ACCESSORS(setter, Object)
6445 DECL_ACCESSORS(query, Object)
6446 DECL_ACCESSORS(deleter, Object)
6447 DECL_ACCESSORS(enumerator, Object)
6448 DECL_ACCESSORS(data, Object)
6449
6450 static inline InterceptorInfo* cast(Object* obj);
6451
Ben Murdochb0fe1622011-05-05 13:52:32 +01006452#ifdef OBJECT_PRINT
6453 inline void InterceptorInfoPrint() {
6454 InterceptorInfoPrint(stdout);
6455 }
6456 void InterceptorInfoPrint(FILE* out);
6457#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006458#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006459 void InterceptorInfoVerify();
6460#endif
6461
6462 static const int kGetterOffset = HeapObject::kHeaderSize;
6463 static const int kSetterOffset = kGetterOffset + kPointerSize;
6464 static const int kQueryOffset = kSetterOffset + kPointerSize;
6465 static const int kDeleterOffset = kQueryOffset + kPointerSize;
6466 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
6467 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
6468 static const int kSize = kDataOffset + kPointerSize;
6469
6470 private:
6471 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
6472};
6473
6474
6475class CallHandlerInfo: public Struct {
6476 public:
6477 DECL_ACCESSORS(callback, Object)
6478 DECL_ACCESSORS(data, Object)
6479
6480 static inline CallHandlerInfo* cast(Object* obj);
6481
Ben Murdochb0fe1622011-05-05 13:52:32 +01006482#ifdef OBJECT_PRINT
6483 inline void CallHandlerInfoPrint() {
6484 CallHandlerInfoPrint(stdout);
6485 }
6486 void CallHandlerInfoPrint(FILE* out);
6487#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006488#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006489 void CallHandlerInfoVerify();
6490#endif
6491
6492 static const int kCallbackOffset = HeapObject::kHeaderSize;
6493 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006494 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006495
6496 private:
6497 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
6498};
6499
6500
6501class TemplateInfo: public Struct {
6502 public:
6503 DECL_ACCESSORS(tag, Object)
6504 DECL_ACCESSORS(property_list, Object)
6505
6506#ifdef DEBUG
6507 void TemplateInfoVerify();
6508#endif
6509
6510 static const int kTagOffset = HeapObject::kHeaderSize;
6511 static const int kPropertyListOffset = kTagOffset + kPointerSize;
6512 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
6513 protected:
6514 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6515 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6516};
6517
6518
6519class FunctionTemplateInfo: public TemplateInfo {
6520 public:
6521 DECL_ACCESSORS(serial_number, Object)
6522 DECL_ACCESSORS(call_code, Object)
6523 DECL_ACCESSORS(property_accessors, Object)
6524 DECL_ACCESSORS(prototype_template, Object)
6525 DECL_ACCESSORS(parent_template, Object)
6526 DECL_ACCESSORS(named_property_handler, Object)
6527 DECL_ACCESSORS(indexed_property_handler, Object)
6528 DECL_ACCESSORS(instance_template, Object)
6529 DECL_ACCESSORS(class_name, Object)
6530 DECL_ACCESSORS(signature, Object)
6531 DECL_ACCESSORS(instance_call_handler, Object)
6532 DECL_ACCESSORS(access_check_info, Object)
6533 DECL_ACCESSORS(flag, Smi)
6534
6535 // Following properties use flag bits.
6536 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6537 DECL_BOOLEAN_ACCESSORS(undetectable)
6538 // If the bit is set, object instances created by this function
6539 // requires access check.
6540 DECL_BOOLEAN_ACCESSORS(needs_access_check)
6541
6542 static inline FunctionTemplateInfo* cast(Object* obj);
6543
Ben Murdochb0fe1622011-05-05 13:52:32 +01006544#ifdef OBJECT_PRINT
6545 inline void FunctionTemplateInfoPrint() {
6546 FunctionTemplateInfoPrint(stdout);
6547 }
6548 void FunctionTemplateInfoPrint(FILE* out);
6549#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006550#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006551 void FunctionTemplateInfoVerify();
6552#endif
6553
6554 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
6555 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
6556 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
6557 static const int kPrototypeTemplateOffset =
6558 kPropertyAccessorsOffset + kPointerSize;
6559 static const int kParentTemplateOffset =
6560 kPrototypeTemplateOffset + kPointerSize;
6561 static const int kNamedPropertyHandlerOffset =
6562 kParentTemplateOffset + kPointerSize;
6563 static const int kIndexedPropertyHandlerOffset =
6564 kNamedPropertyHandlerOffset + kPointerSize;
6565 static const int kInstanceTemplateOffset =
6566 kIndexedPropertyHandlerOffset + kPointerSize;
6567 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
6568 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
6569 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
6570 static const int kAccessCheckInfoOffset =
6571 kInstanceCallHandlerOffset + kPointerSize;
6572 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
6573 static const int kSize = kFlagOffset + kPointerSize;
6574
6575 private:
6576 // Bit position in the flag, from least significant bit position.
6577 static const int kHiddenPrototypeBit = 0;
6578 static const int kUndetectableBit = 1;
6579 static const int kNeedsAccessCheckBit = 2;
6580
6581 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
6582};
6583
6584
6585class ObjectTemplateInfo: public TemplateInfo {
6586 public:
6587 DECL_ACCESSORS(constructor, Object)
6588 DECL_ACCESSORS(internal_field_count, Object)
6589
6590 static inline ObjectTemplateInfo* cast(Object* obj);
6591
Ben Murdochb0fe1622011-05-05 13:52:32 +01006592#ifdef OBJECT_PRINT
6593 inline void ObjectTemplateInfoPrint() {
6594 ObjectTemplateInfoPrint(stdout);
6595 }
6596 void ObjectTemplateInfoPrint(FILE* out);
6597#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006598#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006599 void ObjectTemplateInfoVerify();
6600#endif
6601
6602 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
6603 static const int kInternalFieldCountOffset =
6604 kConstructorOffset + kPointerSize;
6605 static const int kSize = kInternalFieldCountOffset + kPointerSize;
6606};
6607
6608
6609class SignatureInfo: public Struct {
6610 public:
6611 DECL_ACCESSORS(receiver, Object)
6612 DECL_ACCESSORS(args, Object)
6613
6614 static inline SignatureInfo* cast(Object* obj);
6615
Ben Murdochb0fe1622011-05-05 13:52:32 +01006616#ifdef OBJECT_PRINT
6617 inline void SignatureInfoPrint() {
6618 SignatureInfoPrint(stdout);
6619 }
6620 void SignatureInfoPrint(FILE* out);
6621#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006622#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006623 void SignatureInfoVerify();
6624#endif
6625
6626 static const int kReceiverOffset = Struct::kHeaderSize;
6627 static const int kArgsOffset = kReceiverOffset + kPointerSize;
6628 static const int kSize = kArgsOffset + kPointerSize;
6629
6630 private:
6631 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
6632};
6633
6634
6635class TypeSwitchInfo: public Struct {
6636 public:
6637 DECL_ACCESSORS(types, Object)
6638
6639 static inline TypeSwitchInfo* cast(Object* obj);
6640
Ben Murdochb0fe1622011-05-05 13:52:32 +01006641#ifdef OBJECT_PRINT
6642 inline void TypeSwitchInfoPrint() {
6643 TypeSwitchInfoPrint(stdout);
6644 }
6645 void TypeSwitchInfoPrint(FILE* out);
6646#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006647#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006648 void TypeSwitchInfoVerify();
6649#endif
6650
6651 static const int kTypesOffset = Struct::kHeaderSize;
6652 static const int kSize = kTypesOffset + kPointerSize;
6653};
6654
6655
6656#ifdef ENABLE_DEBUGGER_SUPPORT
6657// The DebugInfo class holds additional information for a function being
6658// debugged.
6659class DebugInfo: public Struct {
6660 public:
6661 // The shared function info for the source being debugged.
6662 DECL_ACCESSORS(shared, SharedFunctionInfo)
6663 // Code object for the original code.
6664 DECL_ACCESSORS(original_code, Code)
6665 // Code object for the patched code. This code object is the code object
6666 // currently active for the function.
6667 DECL_ACCESSORS(code, Code)
6668 // Fixed array holding status information for each active break point.
6669 DECL_ACCESSORS(break_points, FixedArray)
6670
6671 // Check if there is a break point at a code position.
6672 bool HasBreakPoint(int code_position);
6673 // Get the break point info object for a code position.
6674 Object* GetBreakPointInfo(int code_position);
6675 // Clear a break point.
6676 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
6677 int code_position,
6678 Handle<Object> break_point_object);
6679 // Set a break point.
6680 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
6681 int source_position, int statement_position,
6682 Handle<Object> break_point_object);
6683 // Get the break point objects for a code position.
6684 Object* GetBreakPointObjects(int code_position);
6685 // Find the break point info holding this break point object.
6686 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
6687 Handle<Object> break_point_object);
6688 // Get the number of break points for this function.
6689 int GetBreakPointCount();
6690
6691 static inline DebugInfo* cast(Object* obj);
6692
Ben Murdochb0fe1622011-05-05 13:52:32 +01006693#ifdef OBJECT_PRINT
6694 inline void DebugInfoPrint() {
6695 DebugInfoPrint(stdout);
6696 }
6697 void DebugInfoPrint(FILE* out);
6698#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006699#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006700 void DebugInfoVerify();
6701#endif
6702
6703 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
6704 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
6705 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
6706 static const int kActiveBreakPointsCountIndex =
6707 kPatchedCodeIndex + kPointerSize;
6708 static const int kBreakPointsStateIndex =
6709 kActiveBreakPointsCountIndex + kPointerSize;
6710 static const int kSize = kBreakPointsStateIndex + kPointerSize;
6711
6712 private:
6713 static const int kNoBreakPointInfo = -1;
6714
6715 // Lookup the index in the break_points array for a code position.
6716 int GetBreakPointInfoIndex(int code_position);
6717
6718 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
6719};
6720
6721
6722// The BreakPointInfo class holds information for break points set in a
6723// function. The DebugInfo object holds a BreakPointInfo object for each code
6724// position with one or more break points.
6725class BreakPointInfo: public Struct {
6726 public:
6727 // The position in the code for the break point.
6728 DECL_ACCESSORS(code_position, Smi)
6729 // The position in the source for the break position.
6730 DECL_ACCESSORS(source_position, Smi)
6731 // The position in the source for the last statement before this break
6732 // position.
6733 DECL_ACCESSORS(statement_position, Smi)
6734 // List of related JavaScript break points.
6735 DECL_ACCESSORS(break_point_objects, Object)
6736
6737 // Removes a break point.
6738 static void ClearBreakPoint(Handle<BreakPointInfo> info,
6739 Handle<Object> break_point_object);
6740 // Set a break point.
6741 static void SetBreakPoint(Handle<BreakPointInfo> info,
6742 Handle<Object> break_point_object);
6743 // Check if break point info has this break point object.
6744 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
6745 Handle<Object> break_point_object);
6746 // Get the number of break points for this code position.
6747 int GetBreakPointCount();
6748
6749 static inline BreakPointInfo* cast(Object* obj);
6750
Ben Murdochb0fe1622011-05-05 13:52:32 +01006751#ifdef OBJECT_PRINT
6752 inline void BreakPointInfoPrint() {
6753 BreakPointInfoPrint(stdout);
6754 }
6755 void BreakPointInfoPrint(FILE* out);
6756#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006757#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006758 void BreakPointInfoVerify();
6759#endif
6760
6761 static const int kCodePositionIndex = Struct::kHeaderSize;
6762 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
6763 static const int kStatementPositionIndex =
6764 kSourcePositionIndex + kPointerSize;
6765 static const int kBreakPointObjectsIndex =
6766 kStatementPositionIndex + kPointerSize;
6767 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
6768
6769 private:
6770 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
6771};
6772#endif // ENABLE_DEBUGGER_SUPPORT
6773
6774
6775#undef DECL_BOOLEAN_ACCESSORS
6776#undef DECL_ACCESSORS
6777
6778
6779// Abstract base class for visiting, and optionally modifying, the
6780// pointers contained in Objects. Used in GC and serialization/deserialization.
6781class ObjectVisitor BASE_EMBEDDED {
6782 public:
6783 virtual ~ObjectVisitor() {}
6784
6785 // Visits a contiguous arrays of pointers in the half-open range
6786 // [start, end). Any or all of the values may be modified on return.
6787 virtual void VisitPointers(Object** start, Object** end) = 0;
6788
6789 // To allow lazy clearing of inline caches the visitor has
6790 // a rich interface for iterating over Code objects..
6791
6792 // Visits a code target in the instruction stream.
6793 virtual void VisitCodeTarget(RelocInfo* rinfo);
6794
Steve Block791712a2010-08-27 10:21:07 +01006795 // Visits a code entry in a JS function.
6796 virtual void VisitCodeEntry(Address entry_address);
6797
Ben Murdochb0fe1622011-05-05 13:52:32 +01006798 // Visits a global property cell reference in the instruction stream.
6799 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
6800
Steve Blocka7e24c12009-10-30 11:49:00 +00006801 // Visits a runtime entry in the instruction stream.
6802 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
6803
Steve Blockd0582a62009-12-15 09:54:21 +00006804 // Visits the resource of an ASCII or two-byte string.
6805 virtual void VisitExternalAsciiString(
6806 v8::String::ExternalAsciiStringResource** resource) {}
6807 virtual void VisitExternalTwoByteString(
6808 v8::String::ExternalStringResource** resource) {}
6809
Steve Blocka7e24c12009-10-30 11:49:00 +00006810 // Visits a debug call target in the instruction stream.
6811 virtual void VisitDebugTarget(RelocInfo* rinfo);
6812
6813 // Handy shorthand for visiting a single pointer.
6814 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
6815
6816 // Visits a contiguous arrays of external references (references to the C++
6817 // heap) in the half-open range [start, end). Any or all of the values
6818 // may be modified on return.
6819 virtual void VisitExternalReferences(Address* start, Address* end) {}
6820
6821 inline void VisitExternalReference(Address* p) {
6822 VisitExternalReferences(p, p + 1);
6823 }
6824
Steve Block44f0eee2011-05-26 01:26:41 +01006825 // Visits a handle that has an embedder-assigned class ID.
6826 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
6827
Steve Blocka7e24c12009-10-30 11:49:00 +00006828#ifdef DEBUG
6829 // Intended for serialization/deserialization checking: insert, or
6830 // check for the presence of, a tag at this position in the stream.
6831 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00006832#else
6833 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00006834#endif
6835};
6836
6837
Iain Merrick75681382010-08-19 15:07:18 +01006838class StructBodyDescriptor : public
6839 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
6840 public:
6841 static inline int SizeOf(Map* map, HeapObject* object) {
6842 return map->instance_size();
6843 }
6844};
6845
6846
Steve Blocka7e24c12009-10-30 11:49:00 +00006847// BooleanBit is a helper class for setting and getting a bit in an
6848// integer or Smi.
6849class BooleanBit : public AllStatic {
6850 public:
6851 static inline bool get(Smi* smi, int bit_position) {
6852 return get(smi->value(), bit_position);
6853 }
6854
6855 static inline bool get(int value, int bit_position) {
6856 return (value & (1 << bit_position)) != 0;
6857 }
6858
6859 static inline Smi* set(Smi* smi, int bit_position, bool v) {
6860 return Smi::FromInt(set(smi->value(), bit_position, v));
6861 }
6862
6863 static inline int set(int value, int bit_position, bool v) {
6864 if (v) {
6865 value |= (1 << bit_position);
6866 } else {
6867 value &= ~(1 << bit_position);
6868 }
6869 return value;
6870 }
6871};
6872
6873} } // namespace v8::internal
6874
6875#endif // V8_OBJECTS_H_