blob: d9c7a82276cfed24c89d8474ed3e4bbbb03a1afa [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"
Ben Murdoch589d6972011-11-30 16:04:58 +000034#include "smart-array-pointer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000035#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)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000051// - JSReceiver (suitable for property access)
52// - JSObject
53// - JSArray
Ben Murdoch69a99ed2011-11-30 16:03:39 +000054// - JSWeakMap
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000055// - JSRegExp
56// - JSFunction
57// - GlobalObject
58// - JSGlobalObject
59// - JSBuiltinsObject
60// - JSGlobalProxy
61// - JSValue
62// - JSMessageObject
63// - JSProxy
64// - JSFunctionProxy
Ben Murdoch69a99ed2011-11-30 16:03:39 +000065// - FixedArrayBase
66// - ByteArray
67// - FixedArray
68// - DescriptorArray
69// - HashTable
70// - Dictionary
71// - SymbolTable
72// - CompilationCacheTable
73// - CodeCacheHashTable
74// - MapCache
75// - Context
76// - JSFunctionResultCache
77// - SerializedScopeInfo
78// - FixedDoubleArray
79// - ExternalArray
80// - ExternalPixelArray
81// - ExternalByteArray
82// - ExternalUnsignedByteArray
83// - ExternalShortArray
84// - ExternalUnsignedShortArray
85// - ExternalIntArray
86// - ExternalUnsignedIntArray
87// - ExternalFloatArray
Steve Blocka7e24c12009-10-30 11:49:00 +000088// - String
89// - SeqString
90// - SeqAsciiString
91// - SeqTwoByteString
Ben Murdoch69a99ed2011-11-30 16:03:39 +000092// - SlicedString
Steve Blocka7e24c12009-10-30 11:49:00 +000093// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000094// - ExternalString
95// - ExternalAsciiString
96// - ExternalTwoByteString
97// - HeapNumber
98// - Code
99// - Map
100// - Oddball
Ben Murdoch257744e2011-11-30 15:57:28 +0000101// - Foreign
Steve Blocka7e24c12009-10-30 11:49:00 +0000102// - SharedFunctionInfo
103// - Struct
104// - AccessorInfo
105// - AccessCheckInfo
106// - InterceptorInfo
107// - CallHandlerInfo
108// - TemplateInfo
109// - FunctionTemplateInfo
110// - ObjectTemplateInfo
111// - Script
112// - SignatureInfo
113// - TypeSwitchInfo
114// - DebugInfo
115// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100116// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000117//
118// Formats of Object*:
119// Smi: [31 bit signed int] 0
120// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
121// Failure: [30 bit signed int] 11
122
123// Ecma-262 3rd 8.6.1
124enum PropertyAttributes {
125 NONE = v8::None,
126 READ_ONLY = v8::ReadOnly,
127 DONT_ENUM = v8::DontEnum,
128 DONT_DELETE = v8::DontDelete,
129 ABSENT = 16 // Used in runtime to indicate a property is absent.
130 // ABSENT can never be stored in or returned from a descriptor's attributes
131 // bitfield. It is only used as a return value meaning the attributes of
132 // a non-existent property.
133};
134
135namespace v8 {
136namespace internal {
137
Ben Murdoch589d6972011-11-30 16:04:58 +0000138enum ElementsKind {
139 // The "fast" kind for tagged values. Must be first to make it possible
140 // to efficiently check maps if they have fast elements.
141 FAST_ELEMENTS,
142
143 // The "fast" kind for unwrapped, non-tagged double values.
144 FAST_DOUBLE_ELEMENTS,
145
146 // The "slow" kind.
147 DICTIONARY_ELEMENTS,
148 NON_STRICT_ARGUMENTS_ELEMENTS,
149 // The "fast" kind for external arrays
150 EXTERNAL_BYTE_ELEMENTS,
151 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
152 EXTERNAL_SHORT_ELEMENTS,
153 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
154 EXTERNAL_INT_ELEMENTS,
155 EXTERNAL_UNSIGNED_INT_ELEMENTS,
156 EXTERNAL_FLOAT_ELEMENTS,
157 EXTERNAL_DOUBLE_ELEMENTS,
158 EXTERNAL_PIXEL_ELEMENTS,
159
160 // Derived constants from ElementsKind
161 FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_BYTE_ELEMENTS,
162 LAST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS,
163 FIRST_ELEMENTS_KIND = FAST_ELEMENTS,
164 LAST_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS
165};
166
167static const int kElementsKindCount =
168 LAST_ELEMENTS_KIND - FIRST_ELEMENTS_KIND + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000169
170// PropertyDetails captures type and attributes for a property.
171// They are used both in property dictionaries and instance descriptors.
172class PropertyDetails BASE_EMBEDDED {
173 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000174 PropertyDetails(PropertyAttributes attributes,
175 PropertyType type,
176 int index = 0) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000177 ASSERT(type != ELEMENTS_TRANSITION);
Steve Blocka7e24c12009-10-30 11:49:00 +0000178 ASSERT(TypeField::is_valid(type));
179 ASSERT(AttributesField::is_valid(attributes));
Steve Block44f0eee2011-05-26 01:26:41 +0100180 ASSERT(StorageField::is_valid(index));
Steve Blocka7e24c12009-10-30 11:49:00 +0000181
182 value_ = TypeField::encode(type)
183 | AttributesField::encode(attributes)
Steve Block44f0eee2011-05-26 01:26:41 +0100184 | StorageField::encode(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
186 ASSERT(type == this->type());
187 ASSERT(attributes == this->attributes());
188 ASSERT(index == this->index());
189 }
190
Steve Block44f0eee2011-05-26 01:26:41 +0100191 PropertyDetails(PropertyAttributes attributes,
192 PropertyType type,
Ben Murdoch589d6972011-11-30 16:04:58 +0000193 ElementsKind elements_kind) {
194 ASSERT(type == ELEMENTS_TRANSITION);
Steve Block44f0eee2011-05-26 01:26:41 +0100195 ASSERT(TypeField::is_valid(type));
196 ASSERT(AttributesField::is_valid(attributes));
Ben Murdoch589d6972011-11-30 16:04:58 +0000197 ASSERT(StorageField::is_valid(static_cast<int>(elements_kind)));
Steve Block44f0eee2011-05-26 01:26:41 +0100198
199 value_ = TypeField::encode(type)
200 | AttributesField::encode(attributes)
Ben Murdoch589d6972011-11-30 16:04:58 +0000201 | StorageField::encode(static_cast<int>(elements_kind));
Steve Block44f0eee2011-05-26 01:26:41 +0100202
203 ASSERT(type == this->type());
204 ASSERT(attributes == this->attributes());
Ben Murdoch589d6972011-11-30 16:04:58 +0000205 ASSERT(elements_kind == this->elements_kind());
Steve Block44f0eee2011-05-26 01:26:41 +0100206 }
207
Steve Blocka7e24c12009-10-30 11:49:00 +0000208 // Conversion for storing details as Object*.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100209 explicit inline PropertyDetails(Smi* smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 inline Smi* AsSmi();
211
212 PropertyType type() { return TypeField::decode(value_); }
213
214 bool IsTransition() {
215 PropertyType t = type();
216 ASSERT(t != INTERCEPTOR);
Steve Block44f0eee2011-05-26 01:26:41 +0100217 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
Ben Murdoch589d6972011-11-30 16:04:58 +0000218 t == ELEMENTS_TRANSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000219 }
220
221 bool IsProperty() {
222 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
223 }
224
225 PropertyAttributes attributes() { return AttributesField::decode(value_); }
226
Steve Block44f0eee2011-05-26 01:26:41 +0100227 int index() { return StorageField::decode(value_); }
228
Ben Murdoch589d6972011-11-30 16:04:58 +0000229 ElementsKind elements_kind() {
230 ASSERT(type() == ELEMENTS_TRANSITION);
231 return static_cast<ElementsKind>(StorageField::decode(value_));
Steve Block44f0eee2011-05-26 01:26:41 +0100232 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000233
234 inline PropertyDetails AsDeleted();
235
Steve Block44f0eee2011-05-26 01:26:41 +0100236 static bool IsValidIndex(int index) {
237 return StorageField::is_valid(index);
238 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000239
240 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
241 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
242 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
243 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
244
245 // Bit fields in value_ (type, shift, size). Must be public so the
246 // constants can be embedded in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100247 class TypeField: public BitField<PropertyType, 0, 4> {};
248 class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
249 class DeletedField: public BitField<uint32_t, 7, 1> {};
250 class StorageField: public BitField<uint32_t, 8, 32-8> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000251
252 static const int kInitialIndex = 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000253
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 private:
255 uint32_t value_;
256};
257
258
259// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
260enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
261
262
263// PropertyNormalizationMode is used to specify whether to keep
264// inobject properties when normalizing properties of a JSObject.
265enum PropertyNormalizationMode {
266 CLEAR_INOBJECT_PROPERTIES,
267 KEEP_INOBJECT_PROPERTIES
268};
269
270
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100271// NormalizedMapSharingMode is used to specify whether a map may be shared
272// by different objects with normalized properties.
273enum NormalizedMapSharingMode {
274 UNIQUE_NORMALIZED_MAP,
275 SHARED_NORMALIZED_MAP
276};
277
278
Steve Block791712a2010-08-27 10:21:07 +0100279// Instance size sentinel for objects of variable size.
280static const int kVariableSizeSentinel = 0;
281
282
Steve Blocka7e24c12009-10-30 11:49:00 +0000283// All Maps have a field instance_type containing a InstanceType.
284// It describes the type of the instances.
285//
286// As an example, a JavaScript object is a heap object and its map
287// instance_type is JS_OBJECT_TYPE.
288//
289// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000290// mirror their encoding in the instance_type field of the map. The default
291// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
292// encoding is mentioned explicitly in the name. Likewise, the default
293// representation is considered sequential. It is not mentioned in the
294// name. The other representations (eg, CONS, EXTERNAL) are explicitly
295// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
296// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000297//
298// NOTE: The following things are some that depend on the string types having
299// instance_types that are less than those of all other types:
300// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
301// Object::IsString.
302//
303// NOTE: Everything following JS_VALUE_TYPE is considered a
304// JSObject for GC purposes. The first four entries here have typeof
305// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000306#define INSTANCE_TYPE_LIST_ALL(V) \
307 V(SYMBOL_TYPE) \
308 V(ASCII_SYMBOL_TYPE) \
309 V(CONS_SYMBOL_TYPE) \
310 V(CONS_ASCII_SYMBOL_TYPE) \
311 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100312 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000313 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
314 V(STRING_TYPE) \
315 V(ASCII_STRING_TYPE) \
316 V(CONS_STRING_TYPE) \
317 V(CONS_ASCII_STRING_TYPE) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000318 V(SLICED_STRING_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000319 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100320 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000321 V(EXTERNAL_ASCII_STRING_TYPE) \
322 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
323 \
324 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000325 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000326 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100327 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000328 \
329 V(HEAP_NUMBER_TYPE) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000330 V(FOREIGN_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000331 V(BYTE_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000332 /* Note: the order of these external array */ \
333 /* types is relied upon in */ \
334 /* Object::IsExternalArray(). */ \
335 V(EXTERNAL_BYTE_ARRAY_TYPE) \
336 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
337 V(EXTERNAL_SHORT_ARRAY_TYPE) \
338 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
339 V(EXTERNAL_INT_ARRAY_TYPE) \
340 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
341 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
Steve Block44f0eee2011-05-26 01:26:41 +0100342 V(EXTERNAL_PIXEL_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000343 V(FILLER_TYPE) \
344 \
345 V(ACCESSOR_INFO_TYPE) \
346 V(ACCESS_CHECK_INFO_TYPE) \
347 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000348 V(CALL_HANDLER_INFO_TYPE) \
349 V(FUNCTION_TEMPLATE_INFO_TYPE) \
350 V(OBJECT_TEMPLATE_INFO_TYPE) \
351 V(SIGNATURE_INFO_TYPE) \
352 V(TYPE_SWITCH_INFO_TYPE) \
353 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100354 V(CODE_CACHE_TYPE) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000355 V(POLYMORPHIC_CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000356 \
Iain Merrick75681382010-08-19 15:07:18 +0100357 V(FIXED_ARRAY_TYPE) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000358 V(FIXED_DOUBLE_ARRAY_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100359 V(SHARED_FUNCTION_INFO_TYPE) \
360 \
Steve Block1e0659c2011-05-24 12:43:12 +0100361 V(JS_MESSAGE_OBJECT_TYPE) \
362 \
Steve Blockd0582a62009-12-15 09:54:21 +0000363 V(JS_VALUE_TYPE) \
364 V(JS_OBJECT_TYPE) \
365 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
366 V(JS_GLOBAL_OBJECT_TYPE) \
367 V(JS_BUILTINS_OBJECT_TYPE) \
368 V(JS_GLOBAL_PROXY_TYPE) \
369 V(JS_ARRAY_TYPE) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000370 V(JS_PROXY_TYPE) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000371 V(JS_WEAK_MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000372 V(JS_REGEXP_TYPE) \
373 \
374 V(JS_FUNCTION_TYPE) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000375 V(JS_FUNCTION_PROXY_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000376
377#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000378#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
379 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 V(BREAK_POINT_INFO_TYPE)
381#else
382#define INSTANCE_TYPE_LIST_DEBUGGER(V)
383#endif
384
Steve Blockd0582a62009-12-15 09:54:21 +0000385#define INSTANCE_TYPE_LIST(V) \
386 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 INSTANCE_TYPE_LIST_DEBUGGER(V)
388
389
390// Since string types are not consecutive, this macro is used to
391// iterate over them.
392#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000393 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100394 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000395 symbol, \
396 Symbol) \
397 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100398 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000399 ascii_symbol, \
400 AsciiSymbol) \
401 V(CONS_SYMBOL_TYPE, \
402 ConsString::kSize, \
403 cons_symbol, \
404 ConsSymbol) \
405 V(CONS_ASCII_SYMBOL_TYPE, \
406 ConsString::kSize, \
407 cons_ascii_symbol, \
408 ConsAsciiSymbol) \
409 V(EXTERNAL_SYMBOL_TYPE, \
410 ExternalTwoByteString::kSize, \
411 external_symbol, \
412 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100413 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
414 ExternalTwoByteString::kSize, \
415 external_symbol_with_ascii_data, \
416 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000417 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
418 ExternalAsciiString::kSize, \
419 external_ascii_symbol, \
420 ExternalAsciiSymbol) \
421 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100422 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000423 string, \
424 String) \
425 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100426 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000427 ascii_string, \
428 AsciiString) \
429 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000430 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000431 cons_string, \
432 ConsString) \
433 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000434 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000435 cons_ascii_string, \
436 ConsAsciiString) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000437 V(SLICED_STRING_TYPE, \
438 SlicedString::kSize, \
439 sliced_string, \
440 SlicedString) \
441 V(SLICED_ASCII_STRING_TYPE, \
442 SlicedString::kSize, \
443 sliced_ascii_string, \
444 SlicedAsciiString) \
Steve Blockd0582a62009-12-15 09:54:21 +0000445 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000446 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000447 external_string, \
448 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100449 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
450 ExternalTwoByteString::kSize, \
451 external_string_with_ascii_data, \
452 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000453 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000454 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000455 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100456 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000457
458// A struct is a simple object a set of object-valued fields. Including an
459// object type in this causes the compiler to generate most of the boilerplate
460// code for the class including allocation and garbage collection routines,
461// casts and predicates. All you need to define is the class, methods and
462// object verification routines. Easy, no?
463//
464// Note that for subtle reasons related to the ordering or numerical values of
465// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
466// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000467#define STRUCT_LIST_ALL(V) \
468 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
469 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
470 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
471 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
472 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
473 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
474 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
475 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100476 V(SCRIPT, Script, script) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000477 V(CODE_CACHE, CodeCache, code_cache) \
478 V(POLYMORPHIC_CODE_CACHE, PolymorphicCodeCache, polymorphic_code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000479
480#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000481#define STRUCT_LIST_DEBUGGER(V) \
482 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
484#else
485#define STRUCT_LIST_DEBUGGER(V)
486#endif
487
Steve Blockd0582a62009-12-15 09:54:21 +0000488#define STRUCT_LIST(V) \
489 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 STRUCT_LIST_DEBUGGER(V)
491
492// We use the full 8 bits of the instance_type field to encode heap object
493// instance types. The high-order bit (bit 7) is set if the object is not a
494// string, and cleared if it is a string.
495const uint32_t kIsNotStringMask = 0x80;
496const uint32_t kStringTag = 0x0;
497const uint32_t kNotStringTag = 0x80;
498
Leon Clarkee46be812010-01-19 14:06:41 +0000499// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
500// There are not enough types that the non-string types (with bit 7 set) can
501// have bit 6 set too.
502const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000503const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000504const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000505
Steve Blocka7e24c12009-10-30 11:49:00 +0000506// If bit 7 is clear then bit 2 indicates whether the string consists of
507// two-byte characters or one-byte characters.
508const uint32_t kStringEncodingMask = 0x4;
509const uint32_t kTwoByteStringTag = 0x0;
510const uint32_t kAsciiStringTag = 0x4;
511
512// If bit 7 is clear, the low-order 2 bits indicate the representation
513// of the string.
514const uint32_t kStringRepresentationMask = 0x03;
515enum StringRepresentationTag {
516 kSeqStringTag = 0x0,
517 kConsStringTag = 0x1,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000518 kExternalStringTag = 0x2,
519 kSlicedStringTag = 0x3
Steve Blocka7e24c12009-10-30 11:49:00 +0000520};
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000521const uint32_t kIsIndirectStringMask = 0x1;
522const uint32_t kIsIndirectStringTag = 0x1;
523STATIC_ASSERT((kSeqStringTag & kIsIndirectStringMask) == 0);
524STATIC_ASSERT((kExternalStringTag & kIsIndirectStringMask) == 0);
525STATIC_ASSERT(
526 (kConsStringTag & kIsIndirectStringMask) == kIsIndirectStringTag);
527STATIC_ASSERT(
528 (kSlicedStringTag & kIsIndirectStringMask) == kIsIndirectStringTag);
529
530// Use this mask to distinguish between cons and slice only after making
531// sure that the string is one of the two (an indirect string).
532const uint32_t kSlicedNotConsMask = kSlicedStringTag & ~kConsStringTag;
533STATIC_ASSERT(IS_POWER_OF_TWO(kSlicedNotConsMask) && kSlicedNotConsMask != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000534
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100535// If bit 7 is clear, then bit 3 indicates whether this two-byte
536// string actually contains ascii data.
537const uint32_t kAsciiDataHintMask = 0x08;
538const uint32_t kAsciiDataHintTag = 0x08;
539
Steve Blocka7e24c12009-10-30 11:49:00 +0000540
541// A ConsString with an empty string as the right side is a candidate
542// for being shortcut by the garbage collector unless it is a
543// symbol. It's not common to have non-flat symbols, so we do not
544// shortcut them thereby avoiding turning symbols into strings. See
545// heap.cc and mark-compact.cc.
546const uint32_t kShortcutTypeMask =
547 kIsNotStringMask |
548 kIsSymbolMask |
549 kStringRepresentationMask;
550const uint32_t kShortcutTypeTag = kConsStringTag;
551
552
553enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000554 // String types.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100555 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000556 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100557 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000558 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100559 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
560 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
561 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000562 EXTERNAL_ASCII_SYMBOL_TYPE =
563 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100564 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000565 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100566 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000567 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000568 SLICED_STRING_TYPE = kTwoByteStringTag | kSlicedStringTag,
569 SLICED_ASCII_STRING_TYPE = kAsciiStringTag | kSlicedStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100570 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
571 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
572 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Block1e0659c2011-05-24 12:43:12 +0100573 // LAST_STRING_TYPE
Steve Blockd0582a62009-12-15 09:54:21 +0000574 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
575 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000576
Leon Clarkee46be812010-01-19 14:06:41 +0000577 // Objects allocated in their own spaces (never in new space).
578 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 CODE_TYPE,
580 ODDBALL_TYPE,
581 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000582
583 // "Data", objects that cannot contain non-map-word pointers to heap
584 // objects.
585 HEAP_NUMBER_TYPE,
Ben Murdoch257744e2011-11-30 15:57:28 +0000586 FOREIGN_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000587 BYTE_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000588 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000589 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
590 EXTERNAL_SHORT_ARRAY_TYPE,
591 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
592 EXTERNAL_INT_ARRAY_TYPE,
593 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100594 EXTERNAL_FLOAT_ARRAY_TYPE,
Ben Murdoch257744e2011-11-30 15:57:28 +0000595 EXTERNAL_DOUBLE_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100596 EXTERNAL_PIXEL_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000597 FIXED_DOUBLE_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000598 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000599
Leon Clarkee46be812010-01-19 14:06:41 +0000600 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 ACCESSOR_INFO_TYPE,
602 ACCESS_CHECK_INFO_TYPE,
603 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 CALL_HANDLER_INFO_TYPE,
605 FUNCTION_TEMPLATE_INFO_TYPE,
606 OBJECT_TEMPLATE_INFO_TYPE,
607 SIGNATURE_INFO_TYPE,
608 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000609 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100610 CODE_CACHE_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000611 POLYMORPHIC_CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100612 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
613 // is defined. However as include/v8.h contain some of the instance type
614 // constants always having them avoids them getting different numbers
615 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000616 DEBUG_INFO_TYPE,
617 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000618
Leon Clarkee46be812010-01-19 14:06:41 +0000619 FIXED_ARRAY_TYPE,
620 SHARED_FUNCTION_INFO_TYPE,
621
Steve Block1e0659c2011-05-24 12:43:12 +0100622 JS_MESSAGE_OBJECT_TYPE,
623
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000624 JS_VALUE_TYPE, // FIRST_NON_CALLABLE_OBJECT_TYPE, FIRST_JS_RECEIVER_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 JS_OBJECT_TYPE,
626 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
627 JS_GLOBAL_OBJECT_TYPE,
628 JS_BUILTINS_OBJECT_TYPE,
629 JS_GLOBAL_PROXY_TYPE,
630 JS_ARRAY_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000631 JS_PROXY_TYPE,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000632 JS_WEAK_MAP_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100633
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000634 JS_REGEXP_TYPE, // LAST_NONCALLABLE_SPEC_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000635
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000636 JS_FUNCTION_TYPE, // FIRST_CALLABLE_SPEC_OBJECT_TYPE
637 JS_FUNCTION_PROXY_TYPE, // LAST_CALLABLE_SPEC_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000638
639 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000640 FIRST_TYPE = 0x0,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000641 LAST_TYPE = JS_FUNCTION_PROXY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000642 INVALID_TYPE = FIRST_TYPE - 1,
643 FIRST_NONSTRING_TYPE = MAP_TYPE,
644 // Boundaries for testing for an external array.
645 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100646 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000647 // Boundary for promotion to old data space/old pointer space.
648 LAST_DATA_TYPE = FILLER_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000649 // Boundary for objects represented as JSReceiver (i.e. JSObject or JSProxy).
650 // Note that there is no range for JSObject or JSProxy, since their subtypes
651 // are not continuous in this enum! The enum ranges instead reflect the
652 // external class names, where proxies are treated as either ordinary objects,
653 // or functions.
654 FIRST_JS_RECEIVER_TYPE = JS_VALUE_TYPE,
655 LAST_JS_RECEIVER_TYPE = LAST_TYPE,
656 // Boundaries for testing the types for which typeof is "object".
657 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_VALUE_TYPE,
658 LAST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_REGEXP_TYPE,
659 // Boundaries for testing the types for which typeof is "function".
660 FIRST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_TYPE,
661 LAST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_PROXY_TYPE,
662 // Boundaries for testing whether the type is a JavaScript object.
663 FIRST_SPEC_OBJECT_TYPE = FIRST_NONCALLABLE_SPEC_OBJECT_TYPE,
664 LAST_SPEC_OBJECT_TYPE = LAST_CALLABLE_SPEC_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000665};
666
Steve Block44f0eee2011-05-26 01:26:41 +0100667static const int kExternalArrayTypeCount = LAST_EXTERNAL_ARRAY_TYPE -
668 FIRST_EXTERNAL_ARRAY_TYPE + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000669
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100670STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
671STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
Ben Murdoch257744e2011-11-30 15:57:28 +0000672STATIC_CHECK(FOREIGN_TYPE == Internals::kForeignType);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100673
674
Steve Blocka7e24c12009-10-30 11:49:00 +0000675enum CompareResult {
676 LESS = -1,
677 EQUAL = 0,
678 GREATER = 1,
679
680 NOT_EQUAL = GREATER
681};
682
683
684#define DECL_BOOLEAN_ACCESSORS(name) \
685 inline bool name(); \
686 inline void set_##name(bool value); \
687
688
689#define DECL_ACCESSORS(name, type) \
690 inline type* name(); \
691 inline void set_##name(type* value, \
692 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
693
694
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000695class DictionaryElementsAccessor;
696class ElementsAccessor;
697class FixedArrayBase;
Steve Blocka7e24c12009-10-30 11:49:00 +0000698class ObjectVisitor;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000699class StringStream;
Steve Blocka7e24c12009-10-30 11:49:00 +0000700
701struct ValueInfo : public Malloced {
702 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
703 InstanceType type;
704 Object* ptr;
705 const char* str;
706 double number;
707};
708
709
710// A template-ized version of the IsXXX functions.
711template <class C> static inline bool Is(Object* obj);
712
Ben Murdoch257744e2011-11-30 15:57:28 +0000713class Failure;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100714
John Reck59135872010-11-02 12:39:01 -0700715class MaybeObject BASE_EMBEDDED {
716 public:
717 inline bool IsFailure();
718 inline bool IsRetryAfterGC();
719 inline bool IsOutOfMemory();
720 inline bool IsException();
721 INLINE(bool IsTheHole());
722 inline bool ToObject(Object** obj) {
723 if (IsFailure()) return false;
724 *obj = reinterpret_cast<Object*>(this);
725 return true;
726 }
Steve Block053d10c2011-06-13 19:13:29 +0100727 inline Failure* ToFailureUnchecked() {
728 ASSERT(IsFailure());
729 return reinterpret_cast<Failure*>(this);
730 }
John Reck59135872010-11-02 12:39:01 -0700731 inline Object* ToObjectUnchecked() {
732 ASSERT(!IsFailure());
733 return reinterpret_cast<Object*>(this);
734 }
735 inline Object* ToObjectChecked() {
736 CHECK(!IsFailure());
737 return reinterpret_cast<Object*>(this);
738 }
739
Steve Block053d10c2011-06-13 19:13:29 +0100740 template<typename T>
741 inline bool To(T** obj) {
742 if (IsFailure()) return false;
743 *obj = T::cast(reinterpret_cast<Object*>(this));
744 return true;
745 }
746
Ben Murdochb0fe1622011-05-05 13:52:32 +0100747#ifdef OBJECT_PRINT
John Reck59135872010-11-02 12:39:01 -0700748 // Prints this object with details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100749 inline void Print() {
750 Print(stdout);
751 };
752 inline void PrintLn() {
753 PrintLn(stdout);
754 }
755 void Print(FILE* out);
756 void PrintLn(FILE* out);
757#endif
758#ifdef DEBUG
John Reck59135872010-11-02 12:39:01 -0700759 // Verifies the object.
760 void Verify();
761#endif
762};
Steve Blocka7e24c12009-10-30 11:49:00 +0000763
Ben Murdochb8e0da22011-05-16 14:20:40 +0100764
765#define OBJECT_TYPE_LIST(V) \
766 V(Smi) \
767 V(HeapObject) \
768 V(Number) \
769
770#define HEAP_OBJECT_TYPE_LIST(V) \
771 V(HeapNumber) \
772 V(String) \
773 V(Symbol) \
774 V(SeqString) \
775 V(ExternalString) \
776 V(ConsString) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000777 V(SlicedString) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100778 V(ExternalTwoByteString) \
779 V(ExternalAsciiString) \
780 V(SeqTwoByteString) \
781 V(SeqAsciiString) \
782 \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100783 V(ExternalArray) \
784 V(ExternalByteArray) \
785 V(ExternalUnsignedByteArray) \
786 V(ExternalShortArray) \
787 V(ExternalUnsignedShortArray) \
788 V(ExternalIntArray) \
789 V(ExternalUnsignedIntArray) \
790 V(ExternalFloatArray) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000791 V(ExternalDoubleArray) \
Steve Block44f0eee2011-05-26 01:26:41 +0100792 V(ExternalPixelArray) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100793 V(ByteArray) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000794 V(JSReceiver) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100795 V(JSObject) \
796 V(JSContextExtensionObject) \
797 V(Map) \
798 V(DescriptorArray) \
799 V(DeoptimizationInputData) \
800 V(DeoptimizationOutputData) \
801 V(FixedArray) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000802 V(FixedDoubleArray) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100803 V(Context) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100804 V(GlobalContext) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000805 V(SerializedScopeInfo) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100806 V(JSFunction) \
807 V(Code) \
808 V(Oddball) \
809 V(SharedFunctionInfo) \
810 V(JSValue) \
Steve Block1e0659c2011-05-24 12:43:12 +0100811 V(JSMessageObject) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100812 V(StringWrapper) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000813 V(Foreign) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100814 V(Boolean) \
815 V(JSArray) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000816 V(JSProxy) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000817 V(JSFunctionProxy) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000818 V(JSWeakMap) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100819 V(JSRegExp) \
820 V(HashTable) \
821 V(Dictionary) \
822 V(SymbolTable) \
823 V(JSFunctionResultCache) \
824 V(NormalizedMapCache) \
825 V(CompilationCacheTable) \
826 V(CodeCacheHashTable) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000827 V(PolymorphicCodeCacheHashTable) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100828 V(MapCache) \
829 V(Primitive) \
830 V(GlobalObject) \
831 V(JSGlobalObject) \
832 V(JSBuiltinsObject) \
833 V(JSGlobalProxy) \
834 V(UndetectableObject) \
835 V(AccessCheckNeeded) \
836 V(JSGlobalPropertyCell) \
837
Steve Blocka7e24c12009-10-30 11:49:00 +0000838// Object is the abstract superclass for all classes in the
839// object hierarchy.
840// Object does not use any virtual functions to avoid the
841// allocation of the C++ vtable.
842// Since Smi and Failure are subclasses of Object no
843// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700844class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000845 public:
846 // Type testing.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100847#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
848 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
849 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
850#undef IS_TYPE_FUNCTION_DECL
Steve Blocka7e24c12009-10-30 11:49:00 +0000851
852 // Returns true if this object is an instance of the specified
853 // function template.
854 inline bool IsInstanceOf(FunctionTemplateInfo* type);
855
856 inline bool IsStruct();
857#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
858 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
859#undef DECLARE_STRUCT_PREDICATE
860
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000861 INLINE(bool IsSpecObject());
862
Steve Blocka7e24c12009-10-30 11:49:00 +0000863 // Oddball testing.
864 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000865 INLINE(bool IsNull());
Steve Block44f0eee2011-05-26 01:26:41 +0100866 INLINE(bool IsTheHole()); // Shadows MaybeObject's implementation.
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 INLINE(bool IsTrue());
868 INLINE(bool IsFalse());
Ben Murdoch086aeea2011-05-13 15:57:08 +0100869 inline bool IsArgumentsMarker();
Steve Blocka7e24c12009-10-30 11:49:00 +0000870
871 // Extract the number.
872 inline double Number();
873
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000874 // Returns true if the object is of the correct type to be used as a
875 // implementation of a JSObject's elements.
876 inline bool HasValidElements();
877
Steve Blocka7e24c12009-10-30 11:49:00 +0000878 inline bool HasSpecificClassOf(String* name);
879
John Reck59135872010-11-02 12:39:01 -0700880 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
881 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000882
883 // Convert to a JSObject if needed.
884 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700885 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000886
887 // Converts this to a Smi if possible.
888 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700889 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000890
891 void Lookup(String* name, LookupResult* result);
892
893 // Property access.
John Reck59135872010-11-02 12:39:01 -0700894 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
895 MUST_USE_RESULT inline MaybeObject* GetProperty(
896 String* key,
897 PropertyAttributes* attributes);
898 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
899 Object* receiver,
900 String* key,
901 PropertyAttributes* attributes);
902 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
903 LookupResult* result,
904 String* key,
905 PropertyAttributes* attributes);
906 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
907 Object* structure,
908 String* name,
909 Object* holder);
Ben Murdoch257744e2011-11-30 15:57:28 +0000910 MUST_USE_RESULT MaybeObject* GetPropertyWithHandler(Object* receiver,
911 String* name,
912 Object* handler);
John Reck59135872010-11-02 12:39:01 -0700913 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
914 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000915
John Reck59135872010-11-02 12:39:01 -0700916 inline MaybeObject* GetElement(uint32_t index);
917 // For use when we know that no exception can be thrown.
918 inline Object* GetElementNoExceptionThrown(uint32_t index);
919 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000920
921 // Return the object's prototype (might be Heap::null_value()).
922 Object* GetPrototype();
923
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100924 // Tries to convert an object to an array index. Returns true and sets
925 // the output parameter if it succeeds.
926 inline bool ToArrayIndex(uint32_t* index);
927
Steve Blocka7e24c12009-10-30 11:49:00 +0000928 // Returns true if this is a JSValue containing a string and the index is
929 // < the length of the string. Used to implement [] on strings.
930 inline bool IsStringObjectWithCharacterAt(uint32_t index);
931
932#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000933 // Verify a pointer is a valid object pointer.
934 static void VerifyPointer(Object* p);
935#endif
936
937 // Prints this object without details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100938 inline void ShortPrint() {
939 ShortPrint(stdout);
940 }
941 void ShortPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000942
943 // Prints this object without details to a message accumulator.
944 void ShortPrint(StringStream* accumulator);
945
946 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
947 static Object* cast(Object* value) { return value; }
948
949 // Layout description.
950 static const int kHeaderSize = 0; // Object does not take up any space.
951
952 private:
953 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
954};
955
956
957// Smi represents integer Numbers that can be stored in 31 bits.
958// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000959// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000960// For long smis it has the following format:
961// [32 bit signed int] [31 bits zero padding] 0
962// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000963class Smi: public Object {
964 public:
965 // Returns the integer value.
966 inline int value();
967
968 // Convert a value to a Smi object.
969 static inline Smi* FromInt(int value);
970
971 static inline Smi* FromIntptr(intptr_t value);
972
973 // Returns whether value can be represented in a Smi.
974 static inline bool IsValid(intptr_t value);
975
Steve Blocka7e24c12009-10-30 11:49:00 +0000976 // Casting.
977 static inline Smi* cast(Object* object);
978
979 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100980 inline void SmiPrint() {
981 SmiPrint(stdout);
982 }
983 void SmiPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000984 void SmiPrint(StringStream* accumulator);
985#ifdef DEBUG
986 void SmiVerify();
987#endif
988
Steve Block3ce2e202009-11-05 08:53:23 +0000989 static const int kMinValue = (-1 << (kSmiValueSize - 1));
990 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000991
992 private:
993 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
994};
995
996
997// Failure is used for reporting out of memory situations and
998// propagating exceptions through the runtime system. Failure objects
999// are transient and cannot occur as part of the object graph.
1000//
1001// Failures are a single word, encoded as follows:
1002// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +01001003// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +00001004// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +00001005// 7 6 4 32 10
1006//
Steve Blocka7e24c12009-10-30 11:49:00 +00001007//
1008// The low two bits, 0-1, are the failure tag, 11. The next two bits,
1009// 2-3, are a failure type tag 'tt' with possible values:
1010// 00 RETRY_AFTER_GC
1011// 01 EXCEPTION
1012// 10 INTERNAL_ERROR
1013// 11 OUT_OF_MEMORY_EXCEPTION
1014//
1015// The next three bits, 4-6, are an allocation space tag 'sss'. The
1016// allocation space tag is 000 for all failure types except
1017// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
1018// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +00001019
1020// Failure type tag info.
1021const int kFailureTypeTagSize = 2;
1022const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
1023
John Reck59135872010-11-02 12:39:01 -07001024class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 public:
1026 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
1027 enum Type {
1028 RETRY_AFTER_GC = 0,
1029 EXCEPTION = 1, // Returning this marker tells the real exception
Steve Block44f0eee2011-05-26 01:26:41 +01001030 // is in Isolate::pending_exception.
Steve Blocka7e24c12009-10-30 11:49:00 +00001031 INTERNAL_ERROR = 2,
1032 OUT_OF_MEMORY_EXCEPTION = 3
1033 };
1034
1035 inline Type type() const;
1036
1037 // Returns the space that needs to be collected for RetryAfterGC failures.
1038 inline AllocationSpace allocation_space() const;
1039
Steve Blocka7e24c12009-10-30 11:49:00 +00001040 inline bool IsInternalError() const;
1041 inline bool IsOutOfMemoryException() const;
1042
Ben Murdochf87a2032010-10-22 12:50:53 +01001043 static inline Failure* RetryAfterGC(AllocationSpace space);
1044 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +00001045 static inline Failure* Exception();
1046 static inline Failure* InternalError();
1047 static inline Failure* OutOfMemoryException();
1048 // Casting.
John Reck59135872010-11-02 12:39:01 -07001049 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001050
1051 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001052 inline void FailurePrint() {
1053 FailurePrint(stdout);
1054 }
1055 void FailurePrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001056 void FailurePrint(StringStream* accumulator);
1057#ifdef DEBUG
1058 void FailureVerify();
1059#endif
1060
1061 private:
Steve Block3ce2e202009-11-05 08:53:23 +00001062 inline intptr_t value() const;
1063 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001064
1065 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
1066};
1067
1068
1069// Heap objects typically have a map pointer in their first word. However,
1070// during GC other data (eg, mark bits, forwarding addresses) is sometimes
1071// encoded in the first word. The class MapWord is an abstraction of the
1072// value in a heap object's first word.
1073class MapWord BASE_EMBEDDED {
1074 public:
1075 // Normal state: the map word contains a map pointer.
1076
1077 // Create a map word from a map pointer.
1078 static inline MapWord FromMap(Map* map);
1079
1080 // View this map word as a map pointer.
1081 inline Map* ToMap();
1082
1083
1084 // Scavenge collection: the map word of live objects in the from space
1085 // contains a forwarding address (a heap object pointer in the to space).
1086
1087 // True if this map word is a forwarding address for a scavenge
1088 // collection. Only valid during a scavenge collection (specifically,
1089 // when all map words are heap object pointers, ie. not during a full GC).
1090 inline bool IsForwardingAddress();
1091
1092 // Create a map word from a forwarding address.
1093 static inline MapWord FromForwardingAddress(HeapObject* object);
1094
1095 // View this map word as a forwarding address.
1096 inline HeapObject* ToForwardingAddress();
1097
Steve Blocka7e24c12009-10-30 11:49:00 +00001098 // Marking phase of full collection: the map word of live objects is
1099 // marked, and may be marked as overflowed (eg, the object is live, its
1100 // children have not been visited, and it does not fit in the marking
1101 // stack).
1102
1103 // True if this map word's mark bit is set.
1104 inline bool IsMarked();
1105
1106 // Return this map word but with its mark bit set.
1107 inline void SetMark();
1108
1109 // Return this map word but with its mark bit cleared.
1110 inline void ClearMark();
1111
1112 // True if this map word's overflow bit is set.
1113 inline bool IsOverflowed();
1114
1115 // Return this map word but with its overflow bit set.
1116 inline void SetOverflow();
1117
1118 // Return this map word but with its overflow bit cleared.
1119 inline void ClearOverflow();
1120
1121
1122 // Compacting phase of a full compacting collection: the map word of live
1123 // objects contains an encoding of the original map address along with the
1124 // forwarding address (represented as an offset from the first live object
1125 // in the same page as the (old) object address).
1126
1127 // Create a map word from a map address and a forwarding address offset.
1128 static inline MapWord EncodeAddress(Address map_address, int offset);
1129
1130 // Return the map address encoded in this map word.
1131 inline Address DecodeMapAddress(MapSpace* map_space);
1132
1133 // Return the forwarding offset encoded in this map word.
1134 inline int DecodeOffset();
1135
1136
1137 // During serialization: the map word is used to hold an encoded
1138 // address, and possibly a mark bit (set and cleared with SetMark
1139 // and ClearMark).
1140
1141 // Create a map word from an encoded address.
1142 static inline MapWord FromEncodedAddress(Address address);
1143
1144 inline Address ToEncodedAddress();
1145
1146 // Bits used by the marking phase of the garbage collector.
1147 //
1148 // The first word of a heap object is normally a map pointer. The last two
1149 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1150 // mark an object as live and/or overflowed:
1151 // last bit = 0, marked as alive
1152 // second bit = 1, overflowed
1153 // An object is only marked as overflowed when it is marked as live while
1154 // the marking stack is overflowed.
1155 static const int kMarkingBit = 0; // marking bit
1156 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1157 static const int kOverflowBit = 1; // overflow bit
1158 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1159
Leon Clarkee46be812010-01-19 14:06:41 +00001160 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1161 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +00001162 // +-----------------+------------------+-----------------+
1163 // |forwarding offset|page offset of map|page index of map|
1164 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +00001165 // ^ ^ ^
1166 // | | |
1167 // | | kMapPageIndexBits
1168 // | kMapPageOffsetBits
1169 // kForwardingOffsetBits
1170 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1171 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1172#ifdef V8_HOST_ARCH_64_BIT
1173 static const int kMapPageIndexBits = 16;
1174#else
1175 // Use all the 32-bits to encode on a 32-bit platform.
1176 static const int kMapPageIndexBits =
1177 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1178#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001179
1180 static const int kMapPageIndexShift = 0;
1181 static const int kMapPageOffsetShift =
1182 kMapPageIndexShift + kMapPageIndexBits;
1183 static const int kForwardingOffsetShift =
1184 kMapPageOffsetShift + kMapPageOffsetBits;
1185
Leon Clarkee46be812010-01-19 14:06:41 +00001186 // Bit masks covering the different parts the encoding.
1187 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001188 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001189 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001190 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001191 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001192 ~(kMapPageIndexMask | kMapPageOffsetMask);
1193
1194 private:
1195 // HeapObject calls the private constructor and directly reads the value.
1196 friend class HeapObject;
1197
1198 explicit MapWord(uintptr_t value) : value_(value) {}
1199
1200 uintptr_t value_;
1201};
1202
1203
1204// HeapObject is the superclass for all classes describing heap allocated
1205// objects.
1206class HeapObject: public Object {
1207 public:
1208 // [map]: Contains a map which contains the object's reflective
1209 // information.
1210 inline Map* map();
1211 inline void set_map(Map* value);
1212
1213 // During garbage collection, the map word of a heap object does not
1214 // necessarily contain a map pointer.
1215 inline MapWord map_word();
1216 inline void set_map_word(MapWord map_word);
1217
Steve Block44f0eee2011-05-26 01:26:41 +01001218 // The Heap the object was allocated in. Used also to access Isolate.
1219 // This method can not be used during GC, it ASSERTs this.
1220 inline Heap* GetHeap();
1221 // Convenience method to get current isolate. This method can be
1222 // accessed only when its result is the same as
1223 // Isolate::Current(), it ASSERTs this. See also comment for GetHeap.
1224 inline Isolate* GetIsolate();
1225
Steve Blocka7e24c12009-10-30 11:49:00 +00001226 // Converts an address to a HeapObject pointer.
1227 static inline HeapObject* FromAddress(Address address);
1228
1229 // Returns the address of this HeapObject.
1230 inline Address address();
1231
1232 // Iterates over pointers contained in the object (including the Map)
1233 void Iterate(ObjectVisitor* v);
1234
1235 // Iterates over all pointers contained in the object except the
1236 // first map pointer. The object type is given in the first
1237 // parameter. This function does not access the map pointer in the
1238 // object, and so is safe to call while the map pointer is modified.
1239 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1240
Steve Blocka7e24c12009-10-30 11:49:00 +00001241 // Returns the heap object's size in bytes
1242 inline int Size();
1243
1244 // Given a heap object's map pointer, returns the heap size in bytes
1245 // Useful when the map pointer field is used for other purposes.
1246 // GC internal.
1247 inline int SizeFromMap(Map* map);
1248
1249 // Support for the marking heap objects during the marking phase of GC.
1250 // True if the object is marked live.
1251 inline bool IsMarked();
1252
1253 // Mutate this object's map pointer to indicate that the object is live.
1254 inline void SetMark();
1255
1256 // Mutate this object's map pointer to remove the indication that the
1257 // object is live (ie, partially restore the map pointer).
1258 inline void ClearMark();
1259
1260 // True if this object is marked as overflowed. Overflowed objects have
1261 // been reached and marked during marking of the heap, but their children
1262 // have not necessarily been marked and they have not been pushed on the
1263 // marking stack.
1264 inline bool IsOverflowed();
1265
1266 // Mutate this object's map pointer to indicate that the object is
1267 // overflowed.
1268 inline void SetOverflow();
1269
1270 // Mutate this object's map pointer to remove the indication that the
1271 // object is overflowed (ie, partially restore the map pointer).
1272 inline void ClearOverflow();
1273
1274 // Returns the field at offset in obj, as a read/write Object* reference.
1275 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001276 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001277 // during marking GC.
1278 static inline Object** RawField(HeapObject* obj, int offset);
1279
1280 // Casting.
1281 static inline HeapObject* cast(Object* obj);
1282
Leon Clarke4515c472010-02-03 11:58:03 +00001283 // Return the write barrier mode for this. Callers of this function
1284 // must be able to present a reference to an AssertNoAllocation
1285 // object as a sign that they are not going to use this function
1286 // from code that allocates and thus invalidates the returned write
1287 // barrier mode.
1288 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001289
1290 // Dispatched behavior.
1291 void HeapObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001292#ifdef OBJECT_PRINT
1293 inline void HeapObjectPrint() {
1294 HeapObjectPrint(stdout);
1295 }
1296 void HeapObjectPrint(FILE* out);
1297#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001298#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001299 void HeapObjectVerify();
1300 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001301 inline void VerifySmiField(int offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001302#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001303
Ben Murdochb0fe1622011-05-05 13:52:32 +01001304#ifdef OBJECT_PRINT
1305 void PrintHeader(FILE* out, const char* id);
1306#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001307
Ben Murdochb0fe1622011-05-05 13:52:32 +01001308#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 // Verify a pointer is a valid HeapObject pointer that points to object
1310 // areas in the heap.
1311 static void VerifyHeapPointer(Object* p);
1312#endif
1313
1314 // Layout description.
1315 // First field in a heap object is map.
1316 static const int kMapOffset = Object::kHeaderSize;
1317 static const int kHeaderSize = kMapOffset + kPointerSize;
1318
1319 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1320
1321 protected:
1322 // helpers for calling an ObjectVisitor to iterate over pointers in the
1323 // half-open range [start, end) specified as integer offsets
1324 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1325 // as above, for the single element at "offset"
1326 inline void IteratePointer(ObjectVisitor* v, int offset);
1327
Steve Blocka7e24c12009-10-30 11:49:00 +00001328 private:
1329 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1330};
1331
1332
Iain Merrick75681382010-08-19 15:07:18 +01001333#define SLOT_ADDR(obj, offset) \
1334 reinterpret_cast<Object**>((obj)->address() + offset)
1335
1336// This class describes a body of an object of a fixed size
1337// in which all pointer fields are located in the [start_offset, end_offset)
1338// interval.
1339template<int start_offset, int end_offset, int size>
1340class FixedBodyDescriptor {
1341 public:
1342 static const int kStartOffset = start_offset;
1343 static const int kEndOffset = end_offset;
1344 static const int kSize = size;
1345
1346 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1347
1348 template<typename StaticVisitor>
1349 static inline void IterateBody(HeapObject* obj) {
1350 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1351 SLOT_ADDR(obj, end_offset));
1352 }
1353};
1354
1355
1356// This class describes a body of an object of a variable size
1357// in which all pointer fields are located in the [start_offset, object_size)
1358// interval.
1359template<int start_offset>
1360class FlexibleBodyDescriptor {
1361 public:
1362 static const int kStartOffset = start_offset;
1363
1364 static inline void IterateBody(HeapObject* obj,
1365 int object_size,
1366 ObjectVisitor* v);
1367
1368 template<typename StaticVisitor>
1369 static inline void IterateBody(HeapObject* obj, int object_size) {
1370 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1371 SLOT_ADDR(obj, object_size));
1372 }
1373};
1374
1375#undef SLOT_ADDR
1376
1377
Steve Blocka7e24c12009-10-30 11:49:00 +00001378// The HeapNumber class describes heap allocated numbers that cannot be
1379// represented in a Smi (small integer)
1380class HeapNumber: public HeapObject {
1381 public:
1382 // [value]: number value.
1383 inline double value();
1384 inline void set_value(double value);
1385
1386 // Casting.
1387 static inline HeapNumber* cast(Object* obj);
1388
1389 // Dispatched behavior.
1390 Object* HeapNumberToBoolean();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001391 inline void HeapNumberPrint() {
1392 HeapNumberPrint(stdout);
1393 }
1394 void HeapNumberPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001395 void HeapNumberPrint(StringStream* accumulator);
1396#ifdef DEBUG
1397 void HeapNumberVerify();
1398#endif
1399
Steve Block6ded16b2010-05-10 14:33:55 +01001400 inline int get_exponent();
1401 inline int get_sign();
1402
Steve Blocka7e24c12009-10-30 11:49:00 +00001403 // Layout description.
1404 static const int kValueOffset = HeapObject::kHeaderSize;
1405 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1406 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1407 // little endian apart from non-EABI arm which is little endian with big
1408 // endian floating point word ordering!
Steve Blocka7e24c12009-10-30 11:49:00 +00001409 static const int kMantissaOffset = kValueOffset;
1410 static const int kExponentOffset = kValueOffset + 4;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001411
Steve Blocka7e24c12009-10-30 11:49:00 +00001412 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001413 static const uint32_t kSignMask = 0x80000000u;
1414 static const uint32_t kExponentMask = 0x7ff00000u;
1415 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001416 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001417 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 static const int kExponentBias = 1023;
1419 static const int kExponentShift = 20;
1420 static const int kMantissaBitsInTopWord = 20;
1421 static const int kNonMantissaBitsInTopWord = 12;
1422
1423 private:
1424 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1425};
1426
1427
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001428// JSReceiver includes types on which properties can be defined, i.e.,
1429// JSObject and JSProxy.
1430class JSReceiver: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001431 public:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001432 enum DeleteMode {
1433 NORMAL_DELETION,
1434 STRICT_DELETION,
1435 FORCE_DELETION
1436 };
1437
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001438 // Casting.
1439 static inline JSReceiver* cast(Object* obj);
1440
1441 // Can cause GC.
1442 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1443 Object* value,
1444 PropertyAttributes attributes,
1445 StrictModeFlag strict_mode);
1446 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1447 String* key,
1448 Object* value,
1449 PropertyAttributes attributes,
1450 StrictModeFlag strict_mode);
1451
1452 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1453
1454 // Returns the class name ([[Class]] property in the specification).
1455 String* class_name();
1456
1457 // Returns the constructor name (the name (possibly, inferred name) of the
1458 // function that was used to instantiate the object).
1459 String* constructor_name();
1460
1461 inline PropertyAttributes GetPropertyAttribute(String* name);
1462 PropertyAttributes GetPropertyAttributeWithReceiver(JSReceiver* receiver,
1463 String* name);
1464 PropertyAttributes GetLocalPropertyAttribute(String* name);
1465
1466 // Can cause a GC.
1467 inline bool HasProperty(String* name);
1468 inline bool HasLocalProperty(String* name);
1469
1470 // Return the object's prototype (might be Heap::null_value()).
1471 inline Object* GetPrototype();
1472
1473 // Set the object's prototype (only JSReceiver and null are allowed).
1474 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1475 bool skip_hidden_prototypes);
1476
1477 // Lookup a property. If found, the result is valid and has
1478 // detailed information.
1479 void LocalLookup(String* name, LookupResult* result);
1480 void Lookup(String* name, LookupResult* result);
1481
1482 private:
1483 PropertyAttributes GetPropertyAttribute(JSReceiver* receiver,
1484 LookupResult* result,
1485 String* name,
1486 bool continue_search);
1487
1488 DISALLOW_IMPLICIT_CONSTRUCTORS(JSReceiver);
1489};
1490
1491// The JSObject describes real heap allocated JavaScript objects with
1492// properties.
1493// Note that the map of JSObject changes during execution to enable inline
1494// caching.
1495class JSObject: public JSReceiver {
1496 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001497 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001498 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001499 // slow case.
1500 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1501 inline void initialize_properties();
1502 inline bool HasFastProperties();
1503 inline StringDictionary* property_dictionary(); // Gets slow properties.
1504
1505 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001506 //
1507 // Elements can be in two general modes: fast and slow. Each mode
1508 // corrensponds to a set of object representations of elements that
1509 // have something in common.
1510 //
1511 // In the fast mode elements is a FixedArray and so each element can
1512 // be quickly accessed. This fact is used in the generated code. The
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001513 // elements array can have one of three maps in this mode:
1514 // fixed_array_map, non_strict_arguments_elements_map or
1515 // fixed_cow_array_map (for copy-on-write arrays). In the latter case
1516 // the elements array may be shared by a few objects and so before
1517 // writing to any element the array must be copied. Use
1518 // EnsureWritableFastElements in this case.
Iain Merrick75681382010-08-19 15:07:18 +01001519 //
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001520 // In the slow mode the elements is either a NumberDictionary, an
1521 // ExternalArray, or a FixedArray parameter map for a (non-strict)
1522 // arguments object.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001523 DECL_ACCESSORS(elements, FixedArrayBase)
Steve Blocka7e24c12009-10-30 11:49:00 +00001524 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001525 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001526 inline ElementsKind GetElementsKind();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001527 inline ElementsAccessor* GetElementsAccessor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001528 inline bool HasFastElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001529 inline bool HasFastDoubleElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001530 inline bool HasDictionaryElements();
Steve Block44f0eee2011-05-26 01:26:41 +01001531 inline bool HasExternalPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001532 inline bool HasExternalArrayElements();
1533 inline bool HasExternalByteElements();
1534 inline bool HasExternalUnsignedByteElements();
1535 inline bool HasExternalShortElements();
1536 inline bool HasExternalUnsignedShortElements();
1537 inline bool HasExternalIntElements();
1538 inline bool HasExternalUnsignedIntElements();
1539 inline bool HasExternalFloatElements();
Ben Murdoch257744e2011-11-30 15:57:28 +00001540 inline bool HasExternalDoubleElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001541 bool HasFastArgumentsElements();
1542 bool HasDictionaryArgumentsElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001543 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001544 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001545
1546 // Requires: HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001547 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001548
1549 // Collects elements starting at index 0.
1550 // Undefined values are placed after non-undefined values.
1551 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001552 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001553 // As PrepareElementsForSort, but only on objects where elements is
1554 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001555 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001556
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001557 MUST_USE_RESULT MaybeObject* SetPropertyForResult(LookupResult* result,
John Reck59135872010-11-02 12:39:01 -07001558 String* key,
1559 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001560 PropertyAttributes attributes,
1561 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001562 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1563 LookupResult* result,
1564 String* name,
Ben Murdoch086aeea2011-05-13 15:57:08 +01001565 Object* value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001566 bool check_prototype,
1567 StrictModeFlag strict_mode);
1568 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(
1569 Object* structure,
1570 String* name,
1571 Object* value,
1572 JSObject* holder,
1573 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001574 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1575 Object* value);
1576 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1577 String* name,
1578 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001579 PropertyAttributes attributes,
1580 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001581 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1582 String* name,
1583 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001584 PropertyAttributes attributes,
1585 StrictModeFlag strict_mode);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001586 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
John Reck59135872010-11-02 12:39:01 -07001587 String* key,
1588 Object* value,
1589 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001590
1591 // Retrieve a value in a normalized object given a lookup result.
1592 // Handles the special representation of JS global objects.
1593 Object* GetNormalizedProperty(LookupResult* result);
1594
1595 // Sets the property value in a normalized object given a lookup result.
1596 // Handles the special representation of JS global objects.
1597 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1598
1599 // Sets the property value in a normalized object given (key, value, details).
1600 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001601 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1602 Object* value,
1603 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001604
1605 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001606 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1607 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001608
Steve Blocka7e24c12009-10-30 11:49:00 +00001609 // Retrieve interceptors.
1610 InterceptorInfo* GetNamedInterceptor();
1611 InterceptorInfo* GetIndexedInterceptor();
1612
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001613 // Used from JSReceiver.
1614 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1615 String* name,
1616 bool continue_search);
1617 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1618 String* name,
1619 bool continue_search);
1620 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1621 Object* receiver,
1622 LookupResult* result,
1623 String* name,
1624 bool continue_search);
Steve Blocka7e24c12009-10-30 11:49:00 +00001625
John Reck59135872010-11-02 12:39:01 -07001626 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1627 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001628 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001629 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001630 Object* LookupAccessor(String* name, bool is_getter);
1631
John Reck59135872010-11-02 12:39:01 -07001632 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001633
Steve Blocka7e24c12009-10-30 11:49:00 +00001634 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001635 MaybeObject* GetPropertyWithFailedAccessCheck(
1636 Object* receiver,
1637 LookupResult* result,
1638 String* name,
1639 PropertyAttributes* attributes);
1640 MaybeObject* GetPropertyWithInterceptor(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001641 JSReceiver* receiver,
John Reck59135872010-11-02 12:39:01 -07001642 String* name,
1643 PropertyAttributes* attributes);
1644 MaybeObject* GetPropertyPostInterceptor(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001645 JSReceiver* receiver,
John Reck59135872010-11-02 12:39:01 -07001646 String* name,
1647 PropertyAttributes* attributes);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001648 MaybeObject* GetLocalPropertyPostInterceptor(JSReceiver* receiver,
John Reck59135872010-11-02 12:39:01 -07001649 String* name,
1650 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001651
1652 // Returns true if this is an instance of an api function and has
1653 // been modified since it was created. May give false positives.
1654 bool IsDirty();
1655
Steve Blockd0582a62009-12-15 09:54:21 +00001656 // If the receiver is a JSGlobalProxy this method will return its prototype,
1657 // otherwise the result is the receiver itself.
1658 inline Object* BypassGlobalProxy();
1659
1660 // Accessors for hidden properties object.
1661 //
1662 // Hidden properties are not local properties of the object itself.
1663 // Instead they are stored on an auxiliary JSObject stored as a local
1664 // property with a special name Heap::hidden_symbol(). But if the
1665 // receiver is a JSGlobalProxy then the auxiliary object is a property
1666 // of its prototype.
1667 //
1668 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1669 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1670 // holder.
1671 //
1672 // These accessors do not touch interceptors or accessors.
1673 inline bool HasHiddenPropertiesObject();
1674 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001675 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1676 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001677
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001678 // Indicates whether the hidden properties object should be created.
1679 enum HiddenPropertiesFlag { ALLOW_CREATION, OMIT_CREATION };
1680
1681 // Retrieves the hidden properties object.
1682 //
1683 // The undefined value might be returned in case no hidden properties object
1684 // is present and creation was omitted.
1685 inline bool HasHiddenProperties();
1686 MUST_USE_RESULT MaybeObject* GetHiddenProperties(HiddenPropertiesFlag flag);
1687
1688 // Retrieves a permanent object identity hash code.
1689 //
1690 // The identity hash is stored as a hidden property. The undefined value might
1691 // be returned in case no hidden properties object is present and creation was
1692 // omitted.
1693 MUST_USE_RESULT MaybeObject* GetIdentityHash(HiddenPropertiesFlag flag);
1694
John Reck59135872010-11-02 12:39:01 -07001695 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1696 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001697
1698 // Tests for the fast common case for property enumeration.
1699 bool IsSimpleEnum();
1700
1701 // Do we want to keep the elements in fast case when increasing the
1702 // capacity?
1703 bool ShouldConvertToSlowElements(int new_capacity);
1704 // Returns true if the backing storage for the slow-case elements of
1705 // this object takes up nearly as much space as a fast-case backing
1706 // storage would. In that case the JSObject should have fast
1707 // elements.
1708 bool ShouldConvertToFastElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001709 // Returns true if the elements of JSObject contains only values that can be
1710 // represented in a FixedDoubleArray.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001711 bool CanConvertToFastDoubleElements();
Andrei Popescu402d9372010-02-26 13:31:12 +00001712
Steve Blocka7e24c12009-10-30 11:49:00 +00001713 // Tells whether the index'th element is present.
1714 inline bool HasElement(uint32_t index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001715 bool HasElementWithReceiver(JSReceiver* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001716
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001717 // Computes the new capacity when expanding the elements of a JSObject.
1718 static int NewElementsCapacity(int old_capacity) {
1719 // (old_capacity + 50%) + 16
1720 return old_capacity + (old_capacity >> 1) + 16;
1721 }
1722
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001723 // Tells whether the index'th element is present and how it is stored.
1724 enum LocalElementType {
1725 // There is no element with given index.
1726 UNDEFINED_ELEMENT,
1727
1728 // Element with given index is handled by interceptor.
1729 INTERCEPTED_ELEMENT,
1730
1731 // Element with given index is character in string.
1732 STRING_CHARACTER_ELEMENT,
1733
1734 // Element with given index is stored in fast backing store.
1735 FAST_ELEMENT,
1736
1737 // Element with given index is stored in slow backing store.
1738 DICTIONARY_ELEMENT
1739 };
1740
1741 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001742
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001743 bool HasElementWithInterceptor(JSReceiver* receiver, uint32_t index);
1744 bool HasElementPostInterceptor(JSReceiver* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001745
Steve Block9fac8402011-05-12 15:51:54 +01001746 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1747 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001748 StrictModeFlag strict_mode,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001749 bool check_prototype);
1750 MUST_USE_RESULT MaybeObject* SetDictionaryElement(uint32_t index,
1751 Object* value,
1752 StrictModeFlag strict_mode,
1753 bool check_prototype);
1754
1755 MUST_USE_RESULT MaybeObject* SetFastDoubleElement(
1756 uint32_t index,
1757 Object* value,
1758 StrictModeFlag strict_mode,
1759 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001760
1761 // Set the index'th array element.
1762 // A Failure object is returned if GC is needed.
Steve Block9fac8402011-05-12 15:51:54 +01001763 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1764 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001765 StrictModeFlag strict_mode,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001766 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00001767
1768 // Returns the index'th element.
1769 // The undefined object if index is out of bounds.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001770 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
1771
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001772 // Replace the elements' backing store with fast elements of the given
1773 // capacity. Update the length for JSArrays. Returns the new backing
1774 // store.
John Reck59135872010-11-02 12:39:01 -07001775 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1776 int length);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001777 MUST_USE_RESULT MaybeObject* SetFastDoubleElementsCapacityAndLength(
1778 int capacity,
1779 int length);
John Reck59135872010-11-02 12:39:01 -07001780 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001781
1782 // Lookup interceptors are used for handling properties controlled by host
1783 // objects.
1784 inline bool HasNamedInterceptor();
1785 inline bool HasIndexedInterceptor();
1786
1787 // Support functions for v8 api (needed for correct interceptor behavior).
1788 bool HasRealNamedProperty(String* key);
1789 bool HasRealElementProperty(uint32_t index);
1790 bool HasRealNamedCallbackProperty(String* key);
1791
1792 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001793 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001794
1795 // Get the header size for a JSObject. Used to compute the index of
1796 // internal fields as well as the number of internal fields.
1797 inline int GetHeaderSize();
1798
1799 inline int GetInternalFieldCount();
Steve Block44f0eee2011-05-26 01:26:41 +01001800 inline int GetInternalFieldOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001801 inline Object* GetInternalField(int index);
1802 inline void SetInternalField(int index, Object* value);
1803
1804 // Lookup a property. If found, the result is valid and has
1805 // detailed information.
1806 void LocalLookup(String* name, LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001807
1808 // The following lookup functions skip interceptors.
1809 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1810 void LookupRealNamedProperty(String* name, LookupResult* result);
1811 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1812 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Steve Block1e0659c2011-05-24 12:43:12 +01001813 MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001814 uint32_t index, Object* value, bool* found, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001815 void LookupCallback(String* name, LookupResult* result);
1816
1817 // Returns the number of properties on this object filtering out properties
1818 // with the specified attributes (ignoring interceptors).
1819 int NumberOfLocalProperties(PropertyAttributes filter);
1820 // Returns the number of enumerable properties (ignoring interceptors).
1821 int NumberOfEnumProperties();
1822 // Fill in details for properties into storage starting at the specified
1823 // index.
1824 void GetLocalPropertyNames(FixedArray* storage, int index);
1825
1826 // Returns the number of properties on this object filtering out properties
1827 // with the specified attributes (ignoring interceptors).
1828 int NumberOfLocalElements(PropertyAttributes filter);
1829 // Returns the number of enumerable elements (ignoring interceptors).
1830 int NumberOfEnumElements();
1831 // Returns the number of elements on this object filtering out elements
1832 // with the specified attributes (ignoring interceptors).
1833 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1834 // Count and fill in the enumerable elements into storage.
1835 // (storage->length() == NumberOfEnumElements()).
1836 // If storage is NULL, will count the elements without adding
1837 // them to any storage.
1838 // Returns the number of enumerable elements.
1839 int GetEnumElementKeys(FixedArray* storage);
1840
1841 // Add a property to a fast-case object using a map transition to
1842 // new_map.
John Reck59135872010-11-02 12:39:01 -07001843 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1844 String* name,
1845 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001846
1847 // Add a constant function property to a fast-case object.
1848 // This leaves a CONSTANT_TRANSITION in the old map, and
1849 // if it is called on a second object with this map, a
1850 // normal property is added instead, with a map transition.
1851 // This avoids the creation of many maps with the same constant
1852 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001853 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1854 String* name,
1855 JSFunction* function,
1856 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001857
John Reck59135872010-11-02 12:39:01 -07001858 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1859 String* name,
1860 Object* value,
1861 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001862
1863 // Converts a descriptor of any other type to a real field,
1864 // backed by the properties array. Descriptors of visible
1865 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1866 // Converts the descriptor on the original object's map to a
1867 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001868 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001869 String* name,
1870 Object* new_value,
1871 PropertyAttributes attributes);
1872
1873 // Converts a descriptor of any other type to a real field,
1874 // backed by the properties array. Descriptors of visible
1875 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001876 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1877 String* name,
1878 Object* new_value,
1879 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001880
1881 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001882 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1883 Object* value,
1884 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001885
1886 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001887 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1888 Object* value,
1889 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001890
1891 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001892 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1893 Object* value,
Steve Block44f0eee2011-05-26 01:26:41 +01001894 PropertyAttributes attributes,
1895 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001896
1897 // Convert the object to use the canonical dictionary
1898 // representation. If the object is expected to have additional properties
1899 // added this number can be indicated to have the backing store allocated to
1900 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001901 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1902 PropertyNormalizationMode mode,
1903 int expected_additional_properties);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001904
1905 // Convert and update the elements backing store to be a NumberDictionary
1906 // dictionary. Returns the backing after conversion.
John Reck59135872010-11-02 12:39:01 -07001907 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001908
John Reck59135872010-11-02 12:39:01 -07001909 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001910
Steve Blocka7e24c12009-10-30 11:49:00 +00001911 // Transform slow named properties to fast variants.
1912 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001913 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1914 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001915
1916 // Access fast-case object properties at index.
1917 inline Object* FastPropertyAt(int index);
1918 inline Object* FastPropertyAtPut(int index, Object* value);
1919
1920 // Access to in object properties.
Steve Block44f0eee2011-05-26 01:26:41 +01001921 inline int GetInObjectPropertyOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001922 inline Object* InObjectPropertyAt(int index);
1923 inline Object* InObjectPropertyAtPut(int index,
1924 Object* value,
1925 WriteBarrierMode mode
1926 = UPDATE_WRITE_BARRIER);
1927
1928 // initializes the body after properties slot, properties slot is
1929 // initialized by set_properties
1930 // Note: this call does not update write barrier, it is caller's
1931 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001932 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001933
1934 // Check whether this object references another object
1935 bool ReferencesObject(Object* obj);
1936
1937 // Casting.
1938 static inline JSObject* cast(Object* obj);
1939
Steve Block8defd9f2010-07-08 12:39:36 +01001940 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001941 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001942
1943
Steve Blocka7e24c12009-10-30 11:49:00 +00001944 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001945 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001946#ifdef OBJECT_PRINT
1947 inline void JSObjectPrint() {
1948 JSObjectPrint(stdout);
1949 }
1950 void JSObjectPrint(FILE* out);
1951#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001952#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001953 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001954#endif
1955#ifdef OBJECT_PRINT
1956 inline void PrintProperties() {
1957 PrintProperties(stdout);
1958 }
1959 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001960
Ben Murdochb0fe1622011-05-05 13:52:32 +01001961 inline void PrintElements() {
1962 PrintElements(stdout);
1963 }
1964 void PrintElements(FILE* out);
1965#endif
1966
1967#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001968 // Structure for collecting spill information about JSObjects.
1969 class SpillInformation {
1970 public:
1971 void Clear();
1972 void Print();
1973 int number_of_objects_;
1974 int number_of_objects_with_fast_properties_;
1975 int number_of_objects_with_fast_elements_;
1976 int number_of_fast_used_fields_;
1977 int number_of_fast_unused_fields_;
1978 int number_of_slow_used_properties_;
1979 int number_of_slow_unused_properties_;
1980 int number_of_fast_used_elements_;
1981 int number_of_fast_unused_elements_;
1982 int number_of_slow_used_elements_;
1983 int number_of_slow_unused_elements_;
1984 };
1985
1986 void IncrementSpillStatistics(SpillInformation* info);
1987#endif
1988 Object* SlowReverseLookup(Object* value);
1989
Steve Block8defd9f2010-07-08 12:39:36 +01001990 // Maximal number of fast properties for the JSObject. Used to
1991 // restrict the number of map transitions to avoid an explosion in
1992 // the number of maps for objects used as dictionaries.
1993 inline int MaxFastProperties();
1994
Leon Clarkee46be812010-01-19 14:06:41 +00001995 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1996 // Also maximal value of JSArray's length property.
1997 static const uint32_t kMaxElementCount = 0xffffffffu;
1998
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001999 // Constants for heuristics controlling conversion of fast elements
2000 // to slow elements.
2001
2002 // Maximal gap that can be introduced by adding an element beyond
2003 // the current elements length.
Steve Blocka7e24c12009-10-30 11:49:00 +00002004 static const uint32_t kMaxGap = 1024;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002005
2006 // Maximal length of fast elements array that won't be checked for
2007 // being dense enough on expansion.
2008 static const int kMaxUncheckedFastElementsLength = 5000;
2009
2010 // Same as above but for old arrays. This limit is more strict. We
2011 // don't want to be wasteful with long lived objects.
2012 static const int kMaxUncheckedOldFastElementsLength = 500;
2013
Steve Blocka7e24c12009-10-30 11:49:00 +00002014 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002015 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00002016 static const int kMaxInstanceSize = 255 * kPointerSize;
2017 // When extending the backing storage for property values, we increase
2018 // its size by more than the 1 entry necessary, so sequentially adding fields
2019 // to the same object requires fewer allocations and copies.
2020 static const int kFieldsAdded = 3;
2021
2022 // Layout description.
2023 static const int kPropertiesOffset = HeapObject::kHeaderSize;
2024 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
2025 static const int kHeaderSize = kElementsOffset + kPointerSize;
2026
2027 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
2028
Iain Merrick75681382010-08-19 15:07:18 +01002029 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
2030 public:
2031 static inline int SizeOf(Map* map, HeapObject* object);
2032 };
2033
Steve Blocka7e24c12009-10-30 11:49:00 +00002034 private:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002035 friend class DictionaryElementsAccessor;
2036
John Reck59135872010-11-02 12:39:01 -07002037 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
2038 Object* structure,
2039 uint32_t index,
2040 Object* holder);
2041 MaybeObject* SetElementWithCallback(Object* structure,
2042 uint32_t index,
2043 Object* value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002044 JSObject* holder,
2045 StrictModeFlag strict_mode);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002046 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
2047 uint32_t index,
2048 Object* value,
2049 StrictModeFlag strict_mode,
2050 bool check_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01002051 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
2052 uint32_t index,
2053 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002054 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01002055 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00002056
John Reck59135872010-11-02 12:39:01 -07002057 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
2058 DeleteMode mode);
2059 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00002060
John Reck59135872010-11-02 12:39:01 -07002061 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002062
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002063 MUST_USE_RESULT MaybeObject* DeleteFastElement(uint32_t index);
2064 MUST_USE_RESULT MaybeObject* DeleteDictionaryElement(uint32_t index,
2065 DeleteMode mode);
2066
2067 bool ReferencesObjectFromElements(FixedArray* elements,
2068 ElementsKind kind,
2069 Object* object);
2070 bool HasElementInElements(FixedArray* elements,
2071 ElementsKind kind,
2072 uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002073
2074 // Returns true if most of the elements backing storage is used.
2075 bool HasDenseElements();
2076
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002077 // Gets the current elements capacity and the number of used elements.
2078 void GetElementsCapacityAndUsage(int* capacity, int* used);
2079
Leon Clarkef7060e22010-06-03 12:02:55 +01002080 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07002081 MUST_USE_RESULT MaybeObject* SetElementCallback(
2082 uint32_t index,
2083 Object* structure,
2084 PropertyAttributes attributes);
2085 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
2086 String* name,
2087 Object* structure,
2088 PropertyAttributes attributes);
2089 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
2090 String* name,
2091 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00002092
2093 void LookupInDescriptor(String* name, LookupResult* result);
2094
2095 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
2096};
2097
2098
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002099// Common superclass for FixedArrays that allow implementations to share
2100// common accessors and some code paths.
2101class FixedArrayBase: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002102 public:
2103 // [length]: length of the array.
2104 inline int length();
2105 inline void set_length(int value);
2106
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002107 inline static FixedArrayBase* cast(Object* object);
2108
2109 // Layout description.
2110 // Length is smi tagged when it is stored.
2111 static const int kLengthOffset = HeapObject::kHeaderSize;
2112 static const int kHeaderSize = kLengthOffset + kPointerSize;
2113};
2114
2115
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002116class FixedDoubleArray;
2117
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002118// FixedArray describes fixed-sized arrays with element type Object*.
2119class FixedArray: public FixedArrayBase {
2120 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00002121 // Setter and getter for elements.
2122 inline Object* get(int index);
2123 // Setter that uses write barrier.
2124 inline void set(int index, Object* value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002125 inline bool is_the_hole(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002126
2127 // Setter that doesn't need write barrier).
2128 inline void set(int index, Smi* value);
2129 // Setter with explicit barrier mode.
2130 inline void set(int index, Object* value, WriteBarrierMode mode);
2131
2132 // Setters for frequently used oddballs located in old space.
2133 inline void set_undefined(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01002134 // TODO(isolates): duplicate.
2135 inline void set_undefined(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002136 inline void set_null(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01002137 // TODO(isolates): duplicate.
2138 inline void set_null(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002139 inline void set_the_hole(int index);
2140
Iain Merrick75681382010-08-19 15:07:18 +01002141 // Setters with less debug checks for the GC to use.
2142 inline void set_unchecked(int index, Smi* value);
Steve Block44f0eee2011-05-26 01:26:41 +01002143 inline void set_null_unchecked(Heap* heap, int index);
2144 inline void set_unchecked(Heap* heap, int index, Object* value,
2145 WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01002146
Steve Block6ded16b2010-05-10 14:33:55 +01002147 // Gives access to raw memory which stores the array's data.
2148 inline Object** data_start();
2149
Steve Blocka7e24c12009-10-30 11:49:00 +00002150 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07002151 MUST_USE_RESULT inline MaybeObject* Copy();
2152 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002153
2154 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07002155 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00002156
2157 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07002158 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00002159
2160 // Copy a sub array from the receiver to dest.
2161 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
2162
2163 // Garbage collection support.
2164 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
2165
2166 // Code Generation support.
2167 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2168
2169 // Casting.
2170 static inline FixedArray* cast(Object* obj);
2171
Leon Clarkee46be812010-01-19 14:06:41 +00002172 // Maximal allowed size, in bytes, of a single FixedArray.
2173 // Prevents overflowing size computations, as well as extreme memory
2174 // consumption.
2175 static const int kMaxSize = 512 * MB;
2176 // Maximally allowed length of a FixedArray.
2177 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002178
2179 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002180#ifdef OBJECT_PRINT
2181 inline void FixedArrayPrint() {
2182 FixedArrayPrint(stdout);
2183 }
2184 void FixedArrayPrint(FILE* out);
2185#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002186#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002187 void FixedArrayVerify();
2188 // Checks if two FixedArrays have identical contents.
2189 bool IsEqualTo(FixedArray* other);
2190#endif
2191
2192 // Swap two elements in a pair of arrays. If this array and the
2193 // numbers array are the same object, the elements are only swapped
2194 // once.
2195 void SwapPairs(FixedArray* numbers, int i, int j);
2196
2197 // Sort prefix of this array and the numbers array as pairs wrt. the
2198 // numbers. If the numbers array and the this array are the same
2199 // object, the prefix of this array is sorted.
2200 void SortPairs(FixedArray* numbers, uint32_t len);
2201
Iain Merrick75681382010-08-19 15:07:18 +01002202 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2203 public:
2204 static inline int SizeOf(Map* map, HeapObject* object) {
2205 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2206 }
2207 };
2208
Steve Blocka7e24c12009-10-30 11:49:00 +00002209 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00002210 // Set operation on FixedArray without using write barriers. Can
2211 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00002212 static inline void fast_set(FixedArray* array, int index, Object* value);
2213
2214 private:
2215 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2216};
2217
2218
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002219// FixedDoubleArray describes fixed-sized arrays with element type double.
2220class FixedDoubleArray: public FixedArrayBase {
2221 public:
2222 inline void Initialize(FixedArray* from);
2223 inline void Initialize(FixedDoubleArray* from);
2224 inline void Initialize(NumberDictionary* from);
2225
2226 // Setter and getter for elements.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002227 inline double get_scalar(int index);
2228 inline MaybeObject* get(int index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002229 inline void set(int index, double value);
2230 inline void set_the_hole(int index);
2231
2232 // Checking for the hole.
2233 inline bool is_the_hole(int index);
2234
2235 // Garbage collection support.
2236 inline static int SizeFor(int length) {
2237 return kHeaderSize + length * kDoubleSize;
2238 }
2239
2240 // Code Generation support.
2241 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2242
2243 inline static bool is_the_hole_nan(double value);
2244 inline static double hole_nan_as_double();
2245 inline static double canonical_not_the_hole_nan_as_double();
2246
2247 // Casting.
2248 static inline FixedDoubleArray* cast(Object* obj);
2249
2250 // Maximal allowed size, in bytes, of a single FixedDoubleArray.
2251 // Prevents overflowing size computations, as well as extreme memory
2252 // consumption.
2253 static const int kMaxSize = 512 * MB;
2254 // Maximally allowed length of a FixedArray.
2255 static const int kMaxLength = (kMaxSize - kHeaderSize) / kDoubleSize;
2256
2257 // Dispatched behavior.
2258#ifdef OBJECT_PRINT
2259 inline void FixedDoubleArrayPrint() {
2260 FixedDoubleArrayPrint(stdout);
2261 }
2262 void FixedDoubleArrayPrint(FILE* out);
2263#endif
2264
2265#ifdef DEBUG
2266 void FixedDoubleArrayVerify();
2267#endif
2268
2269 private:
2270 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedDoubleArray);
2271};
2272
2273
Steve Blocka7e24c12009-10-30 11:49:00 +00002274// DescriptorArrays are fixed arrays used to hold instance descriptors.
2275// The format of the these objects is:
Ben Murdoch257744e2011-11-30 15:57:28 +00002276// TODO(1399): It should be possible to make room for bit_field3 in the map
2277// without overloading the instance descriptors field in the map
2278// (and storing it in the DescriptorArray when the map has one).
2279// [0]: storage for bit_field3 for Map owning this object (Smi)
2280// [1]: point to a fixed array with (value, detail) pairs.
2281// [2]: next enumeration index (Smi), or pointer to small fixed array:
Steve Blocka7e24c12009-10-30 11:49:00 +00002282// [0]: next enumeration index (Smi)
2283// [1]: pointer to fixed array with enum cache
Ben Murdoch257744e2011-11-30 15:57:28 +00002284// [3]: first key
Steve Blocka7e24c12009-10-30 11:49:00 +00002285// [length() - 1]: last key
2286//
2287class DescriptorArray: public FixedArray {
2288 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00002289 // Returns true for both shared empty_descriptor_array and for smis, which the
2290 // map uses to encode additional bit fields when the descriptor array is not
2291 // yet used.
Steve Blocka7e24c12009-10-30 11:49:00 +00002292 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00002293
Steve Blocka7e24c12009-10-30 11:49:00 +00002294 // Returns the number of descriptors in the array.
2295 int number_of_descriptors() {
Steve Block44f0eee2011-05-26 01:26:41 +01002296 ASSERT(length() > kFirstIndex || IsEmpty());
2297 int len = length();
2298 return len <= kFirstIndex ? 0 : len - kFirstIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00002299 }
2300
2301 int NextEnumerationIndex() {
2302 if (IsEmpty()) return PropertyDetails::kInitialIndex;
2303 Object* obj = get(kEnumerationIndexIndex);
2304 if (obj->IsSmi()) {
2305 return Smi::cast(obj)->value();
2306 } else {
2307 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2308 return Smi::cast(index)->value();
2309 }
2310 }
2311
2312 // Set next enumeration index and flush any enum cache.
2313 void SetNextEnumerationIndex(int value) {
2314 if (!IsEmpty()) {
2315 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2316 }
2317 }
2318 bool HasEnumCache() {
2319 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
2320 }
2321
2322 Object* GetEnumCache() {
2323 ASSERT(HasEnumCache());
2324 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2325 return bridge->get(kEnumCacheBridgeCacheIndex);
2326 }
2327
Ben Murdoch257744e2011-11-30 15:57:28 +00002328 // TODO(1399): It should be possible to make room for bit_field3 in the map
2329 // without overloading the instance descriptors field in the map
2330 // (and storing it in the DescriptorArray when the map has one).
2331 inline int bit_field3_storage();
2332 inline void set_bit_field3_storage(int value);
2333
Steve Blocka7e24c12009-10-30 11:49:00 +00002334 // Initialize or change the enum cache,
2335 // using the supplied storage for the small "bridge".
2336 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2337
2338 // Accessors for fetching instance descriptor at descriptor number.
2339 inline String* GetKey(int descriptor_number);
2340 inline Object* GetValue(int descriptor_number);
2341 inline Smi* GetDetails(int descriptor_number);
2342 inline PropertyType GetType(int descriptor_number);
2343 inline int GetFieldIndex(int descriptor_number);
2344 inline JSFunction* GetConstantFunction(int descriptor_number);
2345 inline Object* GetCallbacksObject(int descriptor_number);
2346 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2347 inline bool IsProperty(int descriptor_number);
2348 inline bool IsTransition(int descriptor_number);
2349 inline bool IsNullDescriptor(int descriptor_number);
2350 inline bool IsDontEnum(int descriptor_number);
2351
2352 // Accessor for complete descriptor.
2353 inline void Get(int descriptor_number, Descriptor* desc);
2354 inline void Set(int descriptor_number, Descriptor* desc);
2355
2356 // Transfer complete descriptor from another descriptor array to
2357 // this one.
2358 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2359
2360 // Copy the descriptor array, insert a new descriptor and optionally
2361 // remove map transitions. If the descriptor is already present, it is
2362 // replaced. If a replaced descriptor is a real property (not a transition
2363 // or null), its enumeration index is kept as is.
2364 // If adding a real property, map transitions must be removed. If adding
2365 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002366 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2367 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002368
2369 // Remove all transitions. Return a copy of the array with all transitions
2370 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002371 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002372
2373 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002374 // Does not check for duplicates.
2375 void SortUnchecked();
2376
2377 // Sort the instance descriptors by the hash codes of their keys.
2378 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00002379 void Sort();
2380
2381 // Search the instance descriptors for given name.
2382 inline int Search(String* name);
2383
Iain Merrick75681382010-08-19 15:07:18 +01002384 // As the above, but uses DescriptorLookupCache and updates it when
2385 // necessary.
2386 inline int SearchWithCache(String* name);
2387
Steve Blocka7e24c12009-10-30 11:49:00 +00002388 // Tells whether the name is present int the array.
2389 bool Contains(String* name) { return kNotFound != Search(name); }
2390
2391 // Perform a binary search in the instance descriptors represented
2392 // by this fixed array. low and high are descriptor indices. If there
2393 // are three instance descriptors in this array it should be called
2394 // with low=0 and high=2.
2395 int BinarySearch(String* name, int low, int high);
2396
2397 // Perform a linear search in the instance descriptors represented
2398 // by this fixed array. len is the number of descriptor indices that are
2399 // valid. Does not require the descriptors to be sorted.
2400 int LinearSearch(String* name, int len);
2401
2402 // Allocates a DescriptorArray, but returns the singleton
2403 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002404 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002405
2406 // Casting.
2407 static inline DescriptorArray* cast(Object* obj);
2408
2409 // Constant for denoting key was not found.
2410 static const int kNotFound = -1;
2411
Ben Murdoch257744e2011-11-30 15:57:28 +00002412 static const int kBitField3StorageIndex = 0;
2413 static const int kContentArrayIndex = 1;
2414 static const int kEnumerationIndexIndex = 2;
2415 static const int kFirstIndex = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00002416
2417 // The length of the "bridge" to the enum cache.
2418 static const int kEnumCacheBridgeLength = 2;
2419 static const int kEnumCacheBridgeEnumIndex = 0;
2420 static const int kEnumCacheBridgeCacheIndex = 1;
2421
2422 // Layout description.
Ben Murdoch257744e2011-11-30 15:57:28 +00002423 static const int kBitField3StorageOffset = FixedArray::kHeaderSize;
2424 static const int kContentArrayOffset = kBitField3StorageOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002425 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2426 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2427
2428 // Layout description for the bridge array.
2429 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2430 static const int kEnumCacheBridgeCacheOffset =
2431 kEnumCacheBridgeEnumOffset + kPointerSize;
2432
Ben Murdochb0fe1622011-05-05 13:52:32 +01002433#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002434 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002435 inline void PrintDescriptors() {
2436 PrintDescriptors(stdout);
2437 }
2438 void PrintDescriptors(FILE* out);
2439#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002440
Ben Murdochb0fe1622011-05-05 13:52:32 +01002441#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002442 // Is the descriptor array sorted and without duplicates?
2443 bool IsSortedNoDuplicates();
2444
2445 // Are two DescriptorArrays equal?
2446 bool IsEqualTo(DescriptorArray* other);
2447#endif
2448
2449 // The maximum number of descriptors we want in a descriptor array (should
2450 // fit in a page).
2451 static const int kMaxNumberOfDescriptors = 1024 + 512;
2452
2453 private:
2454 // Conversion from descriptor number to array indices.
2455 static int ToKeyIndex(int descriptor_number) {
2456 return descriptor_number+kFirstIndex;
2457 }
Leon Clarkee46be812010-01-19 14:06:41 +00002458
2459 static int ToDetailsIndex(int descriptor_number) {
2460 return (descriptor_number << 1) + 1;
2461 }
2462
Steve Blocka7e24c12009-10-30 11:49:00 +00002463 static int ToValueIndex(int descriptor_number) {
2464 return descriptor_number << 1;
2465 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002466
2467 bool is_null_descriptor(int descriptor_number) {
2468 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2469 NULL_DESCRIPTOR;
2470 }
2471 // Swap operation on FixedArray without using write barriers.
2472 static inline void fast_swap(FixedArray* array, int first, int second);
2473
2474 // Swap descriptor first and second.
2475 inline void Swap(int first, int second);
2476
2477 FixedArray* GetContentArray() {
2478 return FixedArray::cast(get(kContentArrayIndex));
2479 }
2480 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2481};
2482
2483
2484// HashTable is a subclass of FixedArray that implements a hash table
2485// that uses open addressing and quadratic probing.
2486//
2487// In order for the quadratic probing to work, elements that have not
2488// yet been used and elements that have been deleted are
2489// distinguished. Probing continues when deleted elements are
2490// encountered and stops when unused elements are encountered.
2491//
2492// - Elements with key == undefined have not been used yet.
2493// - Elements with key == null have been deleted.
2494//
2495// The hash table class is parameterized with a Shape and a Key.
2496// Shape must be a class with the following interface:
2497// class ExampleShape {
2498// public:
2499// // Tells whether key matches other.
2500// static bool IsMatch(Key key, Object* other);
2501// // Returns the hash value for key.
2502// static uint32_t Hash(Key key);
2503// // Returns the hash value for object.
2504// static uint32_t HashForObject(Key key, Object* object);
2505// // Convert key to an object.
2506// static inline Object* AsObject(Key key);
2507// // The prefix size indicates number of elements in the beginning
2508// // of the backing storage.
2509// static const int kPrefixSize = ..;
2510// // The Element size indicates number of elements per entry.
2511// static const int kEntrySize = ..;
2512// };
Steve Block3ce2e202009-11-05 08:53:23 +00002513// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002514// beginning of the backing storage that can be used for non-element
2515// information by subclasses.
2516
2517template<typename Shape, typename Key>
2518class HashTable: public FixedArray {
2519 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002520 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002521 int NumberOfElements() {
2522 return Smi::cast(get(kNumberOfElementsIndex))->value();
2523 }
2524
Leon Clarkee46be812010-01-19 14:06:41 +00002525 // Returns the number of deleted elements in the hash table.
2526 int NumberOfDeletedElements() {
2527 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2528 }
2529
Steve Block3ce2e202009-11-05 08:53:23 +00002530 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002531 int Capacity() {
2532 return Smi::cast(get(kCapacityIndex))->value();
2533 }
2534
2535 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002536 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002537 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2538
2539 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002540 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002541 void ElementRemoved() {
2542 SetNumberOfElements(NumberOfElements() - 1);
2543 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2544 }
2545 void ElementsRemoved(int n) {
2546 SetNumberOfElements(NumberOfElements() - n);
2547 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2548 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002549
Steve Block3ce2e202009-11-05 08:53:23 +00002550 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002551 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002552 int at_least_space_for,
2553 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002554
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002555 // Computes the required capacity for a table holding the given
2556 // number of elements. May be more than HashTable::kMaxCapacity.
2557 static int ComputeCapacity(int at_least_space_for);
2558
Steve Blocka7e24c12009-10-30 11:49:00 +00002559 // Returns the key at entry.
2560 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2561
2562 // Tells whether k is a real key. Null and undefined are not allowed
2563 // as keys and can be used to indicate missing or deleted elements.
2564 bool IsKey(Object* k) {
2565 return !k->IsNull() && !k->IsUndefined();
2566 }
2567
2568 // Garbage collection support.
2569 void IteratePrefix(ObjectVisitor* visitor);
2570 void IterateElements(ObjectVisitor* visitor);
2571
2572 // Casting.
2573 static inline HashTable* cast(Object* obj);
2574
2575 // Compute the probe offset (quadratic probing).
2576 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2577 return (n + n * n) >> 1;
2578 }
2579
2580 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002581 static const int kNumberOfDeletedElementsIndex = 1;
2582 static const int kCapacityIndex = 2;
2583 static const int kPrefixStartIndex = 3;
2584 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002585 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002586 static const int kEntrySize = Shape::kEntrySize;
2587 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002588 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002589 static const int kCapacityOffset =
2590 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002591
2592 // Constant used for denoting a absent entry.
2593 static const int kNotFound = -1;
2594
Leon Clarkee46be812010-01-19 14:06:41 +00002595 // Maximal capacity of HashTable. Based on maximal length of underlying
2596 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2597 // cannot overflow.
2598 static const int kMaxCapacity =
2599 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2600
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002601 // Find entry for key otherwise return kNotFound.
Steve Block44f0eee2011-05-26 01:26:41 +01002602 inline int FindEntry(Key key);
2603 int FindEntry(Isolate* isolate, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002604
2605 protected:
Steve Blocka7e24c12009-10-30 11:49:00 +00002606 // Find the entry at which to insert element with the given key that
2607 // has the given hash value.
2608 uint32_t FindInsertionEntry(uint32_t hash);
2609
2610 // Returns the index for an entry (of the key)
2611 static inline int EntryToIndex(int entry) {
2612 return (entry * kEntrySize) + kElementsStartIndex;
2613 }
2614
Steve Block3ce2e202009-11-05 08:53:23 +00002615 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002616 void SetNumberOfElements(int nof) {
2617 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2618 }
2619
Leon Clarkee46be812010-01-19 14:06:41 +00002620 // Update the number of deleted elements in the hash table.
2621 void SetNumberOfDeletedElements(int nod) {
2622 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2623 }
2624
Steve Blocka7e24c12009-10-30 11:49:00 +00002625 // Sets the capacity of the hash table.
2626 void SetCapacity(int capacity) {
2627 // To scale a computed hash code to fit within the hash table, we
2628 // use bit-wise AND with a mask, so the capacity must be positive
2629 // and non-zero.
2630 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002631 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002632 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2633 }
2634
2635
2636 // Returns probe entry.
2637 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2638 ASSERT(IsPowerOf2(size));
2639 return (hash + GetProbeOffset(number)) & (size - 1);
2640 }
2641
Leon Clarkee46be812010-01-19 14:06:41 +00002642 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2643 return hash & (size - 1);
2644 }
2645
2646 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2647 return (last + number) & (size - 1);
2648 }
2649
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002650 // Rehashes this hash-table into the new table.
2651 MUST_USE_RESULT MaybeObject* Rehash(HashTable* new_table, Key key);
2652
2653 // Attempt to shrink hash table after removal of key.
2654 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2655
Steve Blocka7e24c12009-10-30 11:49:00 +00002656 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002657 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002658};
2659
2660
2661
2662// HashTableKey is an abstract superclass for virtual key behavior.
2663class HashTableKey {
2664 public:
2665 // Returns whether the other object matches this key.
2666 virtual bool IsMatch(Object* other) = 0;
2667 // Returns the hash value for this key.
2668 virtual uint32_t Hash() = 0;
2669 // Returns the hash value for object.
2670 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002671 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002672 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002673 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002674 // Required.
2675 virtual ~HashTableKey() {}
2676};
2677
2678class SymbolTableShape {
2679 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002680 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002681 return key->IsMatch(value);
2682 }
Steve Block44f0eee2011-05-26 01:26:41 +01002683 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002684 return key->Hash();
2685 }
Steve Block44f0eee2011-05-26 01:26:41 +01002686 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002687 return key->HashForObject(object);
2688 }
Steve Block44f0eee2011-05-26 01:26:41 +01002689 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002690 return key->AsObject();
2691 }
2692
2693 static const int kPrefixSize = 0;
2694 static const int kEntrySize = 1;
2695};
2696
Ben Murdoch257744e2011-11-30 15:57:28 +00002697class SeqAsciiString;
2698
Steve Blocka7e24c12009-10-30 11:49:00 +00002699// SymbolTable.
2700//
2701// No special elements in the prefix and the element size is 1
2702// because only the symbol itself (the key) needs to be stored.
2703class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2704 public:
2705 // Find symbol in the symbol table. If it is not there yet, it is
2706 // added. The return value is the symbol table which might have
2707 // been enlarged. If the return value is not a failure, the symbol
2708 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002709 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002710 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2711 Object** s);
Ben Murdoch257744e2011-11-30 15:57:28 +00002712 MUST_USE_RESULT MaybeObject* LookupSubStringAsciiSymbol(
2713 Handle<SeqAsciiString> str,
2714 int from,
2715 int length,
2716 Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002717 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2718 Object** s);
John Reck59135872010-11-02 12:39:01 -07002719 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002720
2721 // Looks up a symbol that is equal to the given string and returns
2722 // true if it is found, assigning the symbol to the given output
2723 // parameter.
2724 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002725 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002726
2727 // Casting.
2728 static inline SymbolTable* cast(Object* obj);
2729
2730 private:
John Reck59135872010-11-02 12:39:01 -07002731 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002732
2733 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2734};
2735
2736
2737class MapCacheShape {
2738 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002739 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002740 return key->IsMatch(value);
2741 }
Steve Block44f0eee2011-05-26 01:26:41 +01002742 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002743 return key->Hash();
2744 }
2745
Steve Block44f0eee2011-05-26 01:26:41 +01002746 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002747 return key->HashForObject(object);
2748 }
2749
Steve Block44f0eee2011-05-26 01:26:41 +01002750 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002751 return key->AsObject();
2752 }
2753
2754 static const int kPrefixSize = 0;
2755 static const int kEntrySize = 2;
2756};
2757
2758
2759// MapCache.
2760//
2761// Maps keys that are a fixed array of symbols to a map.
2762// Used for canonicalize maps for object literals.
2763class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2764 public:
2765 // Find cached value for a string key, otherwise return null.
2766 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002767 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002768 static inline MapCache* cast(Object* obj);
2769
2770 private:
2771 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2772};
2773
2774
2775template <typename Shape, typename Key>
2776class Dictionary: public HashTable<Shape, Key> {
2777 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00002778 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2779 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2780 }
2781
2782 // Returns the value at entry.
2783 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002784 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002785 }
2786
2787 // Set the value for entry.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002788 // Returns false if the put wasn't performed due to property being read only.
2789 // Returns true on successful put.
2790 bool ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002791 // Check that this value can actually be written.
2792 PropertyDetails details = DetailsAt(entry);
2793 // If a value has not been initilized we allow writing to it even if
2794 // it is read only (a declared const that has not been initialized).
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002795 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2796 return false;
2797 }
2798 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2799 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002800 }
2801
2802 // Returns the property details for the property at entry.
2803 PropertyDetails DetailsAt(int entry) {
2804 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2805 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002806 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002807 }
2808
2809 // Set the details for entry.
2810 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002811 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002812 }
2813
2814 // Sorting support
2815 void CopyValuesTo(FixedArray* elements);
2816
2817 // Delete a property from the dictionary.
2818 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2819
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002820 // Attempt to shrink the dictionary after deletion of key.
2821 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2822
Steve Blocka7e24c12009-10-30 11:49:00 +00002823 // Returns the number of elements in the dictionary filtering out properties
2824 // with the specified attributes.
2825 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2826
2827 // Returns the number of enumerable elements in the dictionary.
2828 int NumberOfEnumElements();
2829
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002830 enum SortMode { UNSORTED, SORTED };
Steve Blocka7e24c12009-10-30 11:49:00 +00002831 // Copies keys to preallocated fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002832 void CopyKeysTo(FixedArray* storage,
2833 PropertyAttributes filter,
2834 SortMode sort_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002835 // Fill in details for properties into storage.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002836 void CopyKeysTo(FixedArray* storage, int index, SortMode sort_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002837
2838 // Accessors for next enumeration index.
2839 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002840 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002841 }
2842
2843 int NextEnumerationIndex() {
2844 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2845 }
2846
2847 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002848 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002849
2850 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002851 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002852
Ben Murdochb0fe1622011-05-05 13:52:32 +01002853#ifdef OBJECT_PRINT
2854 inline void Print() {
2855 Print(stdout);
2856 }
2857 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002858#endif
2859 // Returns the key (slow).
2860 Object* SlowReverseLookup(Object* value);
2861
2862 // Sets the entry to (key, value) pair.
2863 inline void SetEntry(int entry,
2864 Object* key,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002865 Object* value);
2866 inline void SetEntry(int entry,
2867 Object* key,
Steve Blocka7e24c12009-10-30 11:49:00 +00002868 Object* value,
2869 PropertyDetails details);
2870
John Reck59135872010-11-02 12:39:01 -07002871 MUST_USE_RESULT MaybeObject* Add(Key key,
2872 Object* value,
2873 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002874
2875 protected:
2876 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002877 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002878
2879 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002880 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2881 Object* value,
2882 PropertyDetails details,
2883 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002884
2885 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002886 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002887 static const int kMaxNumberKeyIndex =
2888 HashTable<Shape, Key>::kPrefixStartIndex;
2889 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2890};
2891
2892
2893class StringDictionaryShape {
2894 public:
2895 static inline bool IsMatch(String* key, Object* other);
2896 static inline uint32_t Hash(String* key);
2897 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002898 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002899 static const int kPrefixSize = 2;
2900 static const int kEntrySize = 3;
2901 static const bool kIsEnumerable = true;
2902};
2903
2904
2905class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2906 public:
2907 static inline StringDictionary* cast(Object* obj) {
2908 ASSERT(obj->IsDictionary());
2909 return reinterpret_cast<StringDictionary*>(obj);
2910 }
2911
2912 // Copies enumerable keys to preallocated fixed array.
2913 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2914
2915 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002916 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2917 JSObject* obj,
2918 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002919
2920 // Find entry for key otherwise return kNotFound. Optimzed version of
2921 // HashTable::FindEntry.
2922 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002923};
2924
2925
2926class NumberDictionaryShape {
2927 public:
2928 static inline bool IsMatch(uint32_t key, Object* other);
2929 static inline uint32_t Hash(uint32_t key);
2930 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002931 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002932 static const int kPrefixSize = 2;
2933 static const int kEntrySize = 3;
2934 static const bool kIsEnumerable = false;
2935};
2936
2937
2938class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2939 public:
2940 static NumberDictionary* cast(Object* obj) {
2941 ASSERT(obj->IsDictionary());
2942 return reinterpret_cast<NumberDictionary*>(obj);
2943 }
2944
2945 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002946 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2947 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2948 Object* value,
2949 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002950
2951 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002952 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2953 Object* value,
2954 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002955
2956 void UpdateMaxNumberKey(uint32_t key);
2957
2958 // If slow elements are required we will never go back to fast-case
2959 // for the elements kept in this dictionary. We require slow
2960 // elements if an element has been added at an index larger than
2961 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2962 // when defining a getter or setter with a number key.
2963 inline bool requires_slow_elements();
2964 inline void set_requires_slow_elements();
2965
2966 // Get the value of the max number key that has been added to this
2967 // dictionary. max_number_key can only be called if
2968 // requires_slow_elements returns false.
2969 inline uint32_t max_number_key();
2970
2971 // Remove all entries were key is a number and (from <= key && key < to).
2972 void RemoveNumberEntries(uint32_t from, uint32_t to);
2973
2974 // Bit masks.
2975 static const int kRequiresSlowElementsMask = 1;
2976 static const int kRequiresSlowElementsTagSize = 1;
2977 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2978};
2979
2980
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002981class ObjectHashTableShape {
2982 public:
2983 static inline bool IsMatch(JSObject* key, Object* other);
2984 static inline uint32_t Hash(JSObject* key);
2985 static inline uint32_t HashForObject(JSObject* key, Object* object);
2986 MUST_USE_RESULT static inline MaybeObject* AsObject(JSObject* key);
2987 static const int kPrefixSize = 0;
2988 static const int kEntrySize = 2;
2989};
2990
2991
2992// ObjectHashTable maps keys that are JavaScript objects to object values by
2993// using the identity hash of the key for hashing purposes.
2994class ObjectHashTable: public HashTable<ObjectHashTableShape, JSObject*> {
2995 public:
2996 static inline ObjectHashTable* cast(Object* obj) {
2997 ASSERT(obj->IsHashTable());
2998 return reinterpret_cast<ObjectHashTable*>(obj);
2999 }
3000
3001 // Looks up the value associated with the given key. The undefined value is
3002 // returned in case the key is not present.
3003 Object* Lookup(JSObject* key);
3004
3005 // Adds (or overwrites) the value associated with the given key. Mapping a
3006 // key to the undefined value causes removal of the whole entry.
3007 MUST_USE_RESULT MaybeObject* Put(JSObject* key, Object* value);
3008
3009 private:
3010 friend class MarkCompactCollector;
3011
3012 void AddEntry(int entry, JSObject* key, Object* value);
3013 void RemoveEntry(int entry, Heap* heap);
3014 inline void RemoveEntry(int entry);
3015
3016 // Returns the index to the value of an entry.
3017 static inline int EntryToValueIndex(int entry) {
3018 return EntryToIndex(entry) + 1;
3019 }
3020};
3021
3022
Steve Block6ded16b2010-05-10 14:33:55 +01003023// JSFunctionResultCache caches results of some JSFunction invocation.
3024// It is a fixed array with fixed structure:
3025// [0]: factory function
3026// [1]: finger index
3027// [2]: current cache size
3028// [3]: dummy field.
3029// The rest of array are key/value pairs.
3030class JSFunctionResultCache: public FixedArray {
3031 public:
3032 static const int kFactoryIndex = 0;
3033 static const int kFingerIndex = kFactoryIndex + 1;
3034 static const int kCacheSizeIndex = kFingerIndex + 1;
3035 static const int kDummyIndex = kCacheSizeIndex + 1;
3036 static const int kEntriesIndex = kDummyIndex + 1;
3037
3038 static const int kEntrySize = 2; // key + value
3039
Kristian Monsen25f61362010-05-21 11:50:48 +01003040 static const int kFactoryOffset = kHeaderSize;
3041 static const int kFingerOffset = kFactoryOffset + kPointerSize;
3042 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
3043
Steve Block6ded16b2010-05-10 14:33:55 +01003044 inline void MakeZeroSize();
3045 inline void Clear();
3046
Ben Murdochb8e0da22011-05-16 14:20:40 +01003047 inline int size();
3048 inline void set_size(int size);
3049 inline int finger_index();
3050 inline void set_finger_index(int finger_index);
3051
Steve Block6ded16b2010-05-10 14:33:55 +01003052 // Casting
3053 static inline JSFunctionResultCache* cast(Object* obj);
3054
3055#ifdef DEBUG
3056 void JSFunctionResultCacheVerify();
3057#endif
3058};
3059
3060
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003061// The cache for maps used by normalized (dictionary mode) objects.
3062// Such maps do not have property descriptors, so a typical program
3063// needs very limited number of distinct normalized maps.
3064class NormalizedMapCache: public FixedArray {
3065 public:
3066 static const int kEntries = 64;
3067
John Reck59135872010-11-02 12:39:01 -07003068 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
3069 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003070
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003071 void Clear();
3072
3073 // Casting
3074 static inline NormalizedMapCache* cast(Object* obj);
3075
3076#ifdef DEBUG
3077 void NormalizedMapCacheVerify();
3078#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003079};
3080
3081
Steve Blocka7e24c12009-10-30 11:49:00 +00003082// ByteArray represents fixed sized byte arrays. Used by the outside world,
3083// such as PCRE, and also by the memory allocator and garbage collector to
3084// fill in free blocks in the heap.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003085class ByteArray: public FixedArrayBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00003086 public:
3087 // Setter and getter.
3088 inline byte get(int index);
3089 inline void set(int index, byte value);
3090
3091 // Treat contents as an int array.
3092 inline int get_int(int index);
3093
3094 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003095 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00003096 }
3097 // We use byte arrays for free blocks in the heap. Given a desired size in
3098 // bytes that is a multiple of the word size and big enough to hold a byte
3099 // array, this function returns the number of elements a byte array should
3100 // have.
3101 static int LengthFor(int size_in_bytes) {
3102 ASSERT(IsAligned(size_in_bytes, kPointerSize));
3103 ASSERT(size_in_bytes >= kHeaderSize);
3104 return size_in_bytes - kHeaderSize;
3105 }
3106
3107 // Returns data start address.
3108 inline Address GetDataStartAddress();
3109
3110 // Returns a pointer to the ByteArray object for a given data start address.
3111 static inline ByteArray* FromDataStartAddress(Address address);
3112
3113 // Casting.
3114 static inline ByteArray* cast(Object* obj);
3115
3116 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01003117 inline int ByteArraySize() {
3118 return SizeFor(this->length());
3119 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003120#ifdef OBJECT_PRINT
3121 inline void ByteArrayPrint() {
3122 ByteArrayPrint(stdout);
3123 }
3124 void ByteArrayPrint(FILE* out);
3125#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003126#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003127 void ByteArrayVerify();
3128#endif
3129
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003130 // Layout description.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003131 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003132
Leon Clarkee46be812010-01-19 14:06:41 +00003133 // Maximal memory consumption for a single ByteArray.
3134 static const int kMaxSize = 512 * MB;
3135 // Maximal length of a single ByteArray.
3136 static const int kMaxLength = kMaxSize - kHeaderSize;
3137
Steve Blocka7e24c12009-10-30 11:49:00 +00003138 private:
3139 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
3140};
3141
3142
Steve Block3ce2e202009-11-05 08:53:23 +00003143// An ExternalArray represents a fixed-size array of primitive values
3144// which live outside the JavaScript heap. Its subclasses are used to
3145// implement the CanvasArray types being defined in the WebGL
3146// specification. As of this writing the first public draft is not yet
3147// available, but Khronos members can access the draft at:
3148// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
3149//
3150// The semantics of these arrays differ from CanvasPixelArray.
3151// Out-of-range values passed to the setter are converted via a C
3152// cast, not clamping. Out-of-range indices cause exceptions to be
3153// raised rather than being silently ignored.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003154class ExternalArray: public FixedArrayBase {
Steve Block3ce2e202009-11-05 08:53:23 +00003155 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003156 inline bool is_the_hole(int index) { return false; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003157
Steve Block3ce2e202009-11-05 08:53:23 +00003158 // [external_pointer]: The pointer to the external memory area backing this
3159 // external array.
3160 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
3161
3162 // Casting.
3163 static inline ExternalArray* cast(Object* obj);
3164
3165 // Maximal acceptable length for an external array.
3166 static const int kMaxLength = 0x3fffffff;
3167
3168 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003169 static const int kExternalPointerOffset =
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003170 POINTER_SIZE_ALIGN(FixedArrayBase::kLengthOffset + kPointerSize);
Steve Block3ce2e202009-11-05 08:53:23 +00003171 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003172 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00003173
3174 private:
3175 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
3176};
3177
3178
Steve Block44f0eee2011-05-26 01:26:41 +01003179// A ExternalPixelArray represents a fixed-size byte array with special
3180// semantics used for implementing the CanvasPixelArray object. Please see the
3181// specification at:
3182
3183// http://www.whatwg.org/specs/web-apps/current-work/
3184// multipage/the-canvas-element.html#canvaspixelarray
3185// In particular, write access clamps the value written to 0 or 255 if the
3186// value written is outside this range.
3187class ExternalPixelArray: public ExternalArray {
3188 public:
3189 inline uint8_t* external_pixel_pointer();
3190
3191 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003192 inline uint8_t get_scalar(int index);
3193 inline MaybeObject* get(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01003194 inline void set(int index, uint8_t value);
3195
3196 // This accessor applies the correct conversion from Smi, HeapNumber and
3197 // undefined and clamps the converted value between 0 and 255.
3198 Object* SetValue(uint32_t index, Object* value);
3199
3200 // Casting.
3201 static inline ExternalPixelArray* cast(Object* obj);
3202
3203#ifdef OBJECT_PRINT
3204 inline void ExternalPixelArrayPrint() {
3205 ExternalPixelArrayPrint(stdout);
3206 }
3207 void ExternalPixelArrayPrint(FILE* out);
3208#endif
3209#ifdef DEBUG
3210 void ExternalPixelArrayVerify();
3211#endif // DEBUG
3212
3213 private:
3214 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
3215};
3216
3217
Steve Block3ce2e202009-11-05 08:53:23 +00003218class ExternalByteArray: public ExternalArray {
3219 public:
3220 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003221 inline int8_t get_scalar(int index);
3222 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003223 inline void set(int index, int8_t value);
3224
3225 // This accessor applies the correct conversion from Smi, HeapNumber
3226 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003227 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003228
3229 // Casting.
3230 static inline ExternalByteArray* cast(Object* obj);
3231
Ben Murdochb0fe1622011-05-05 13:52:32 +01003232#ifdef OBJECT_PRINT
3233 inline void ExternalByteArrayPrint() {
3234 ExternalByteArrayPrint(stdout);
3235 }
3236 void ExternalByteArrayPrint(FILE* out);
3237#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003238#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003239 void ExternalByteArrayVerify();
3240#endif // DEBUG
3241
3242 private:
3243 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
3244};
3245
3246
3247class ExternalUnsignedByteArray: public ExternalArray {
3248 public:
3249 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003250 inline uint8_t get_scalar(int index);
3251 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003252 inline void set(int index, uint8_t value);
3253
3254 // This accessor applies the correct conversion from Smi, HeapNumber
3255 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003256 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003257
3258 // Casting.
3259 static inline ExternalUnsignedByteArray* cast(Object* obj);
3260
Ben Murdochb0fe1622011-05-05 13:52:32 +01003261#ifdef OBJECT_PRINT
3262 inline void ExternalUnsignedByteArrayPrint() {
3263 ExternalUnsignedByteArrayPrint(stdout);
3264 }
3265 void ExternalUnsignedByteArrayPrint(FILE* out);
3266#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003267#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003268 void ExternalUnsignedByteArrayVerify();
3269#endif // DEBUG
3270
3271 private:
3272 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
3273};
3274
3275
3276class ExternalShortArray: public ExternalArray {
3277 public:
3278 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003279 inline int16_t get_scalar(int index);
3280 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003281 inline void set(int index, int16_t value);
3282
3283 // This accessor applies the correct conversion from Smi, HeapNumber
3284 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003285 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003286
3287 // Casting.
3288 static inline ExternalShortArray* cast(Object* obj);
3289
Ben Murdochb0fe1622011-05-05 13:52:32 +01003290#ifdef OBJECT_PRINT
3291 inline void ExternalShortArrayPrint() {
3292 ExternalShortArrayPrint(stdout);
3293 }
3294 void ExternalShortArrayPrint(FILE* out);
3295#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003296#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003297 void ExternalShortArrayVerify();
3298#endif // DEBUG
3299
3300 private:
3301 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
3302};
3303
3304
3305class ExternalUnsignedShortArray: public ExternalArray {
3306 public:
3307 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003308 inline uint16_t get_scalar(int index);
3309 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003310 inline void set(int index, uint16_t value);
3311
3312 // This accessor applies the correct conversion from Smi, HeapNumber
3313 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003314 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003315
3316 // Casting.
3317 static inline ExternalUnsignedShortArray* cast(Object* obj);
3318
Ben Murdochb0fe1622011-05-05 13:52:32 +01003319#ifdef OBJECT_PRINT
3320 inline void ExternalUnsignedShortArrayPrint() {
3321 ExternalUnsignedShortArrayPrint(stdout);
3322 }
3323 void ExternalUnsignedShortArrayPrint(FILE* out);
3324#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003325#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003326 void ExternalUnsignedShortArrayVerify();
3327#endif // DEBUG
3328
3329 private:
3330 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3331};
3332
3333
3334class ExternalIntArray: public ExternalArray {
3335 public:
3336 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003337 inline int32_t get_scalar(int index);
3338 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003339 inline void set(int index, int32_t value);
3340
3341 // This accessor applies the correct conversion from Smi, HeapNumber
3342 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003343 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003344
3345 // Casting.
3346 static inline ExternalIntArray* cast(Object* obj);
3347
Ben Murdochb0fe1622011-05-05 13:52:32 +01003348#ifdef OBJECT_PRINT
3349 inline void ExternalIntArrayPrint() {
3350 ExternalIntArrayPrint(stdout);
3351 }
3352 void ExternalIntArrayPrint(FILE* out);
3353#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003354#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003355 void ExternalIntArrayVerify();
3356#endif // DEBUG
3357
3358 private:
3359 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3360};
3361
3362
3363class ExternalUnsignedIntArray: public ExternalArray {
3364 public:
3365 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003366 inline uint32_t get_scalar(int index);
3367 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003368 inline void set(int index, uint32_t value);
3369
3370 // This accessor applies the correct conversion from Smi, HeapNumber
3371 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003372 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003373
3374 // Casting.
3375 static inline ExternalUnsignedIntArray* cast(Object* obj);
3376
Ben Murdochb0fe1622011-05-05 13:52:32 +01003377#ifdef OBJECT_PRINT
3378 inline void ExternalUnsignedIntArrayPrint() {
3379 ExternalUnsignedIntArrayPrint(stdout);
3380 }
3381 void ExternalUnsignedIntArrayPrint(FILE* out);
3382#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003383#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003384 void ExternalUnsignedIntArrayVerify();
3385#endif // DEBUG
3386
3387 private:
3388 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3389};
3390
3391
3392class ExternalFloatArray: public ExternalArray {
3393 public:
3394 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003395 inline float get_scalar(int index);
3396 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003397 inline void set(int index, float value);
3398
3399 // This accessor applies the correct conversion from Smi, HeapNumber
3400 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003401 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003402
3403 // Casting.
3404 static inline ExternalFloatArray* cast(Object* obj);
3405
Ben Murdochb0fe1622011-05-05 13:52:32 +01003406#ifdef OBJECT_PRINT
3407 inline void ExternalFloatArrayPrint() {
3408 ExternalFloatArrayPrint(stdout);
3409 }
3410 void ExternalFloatArrayPrint(FILE* out);
3411#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003412#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003413 void ExternalFloatArrayVerify();
3414#endif // DEBUG
3415
3416 private:
3417 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3418};
3419
3420
Ben Murdoch257744e2011-11-30 15:57:28 +00003421class ExternalDoubleArray: public ExternalArray {
3422 public:
3423 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003424 inline double get_scalar(int index);
3425 inline MaybeObject* get(int index);
Ben Murdoch257744e2011-11-30 15:57:28 +00003426 inline void set(int index, double value);
3427
3428 // This accessor applies the correct conversion from Smi, HeapNumber
3429 // and undefined.
3430 MaybeObject* SetValue(uint32_t index, Object* value);
3431
3432 // Casting.
3433 static inline ExternalDoubleArray* cast(Object* obj);
3434
3435#ifdef OBJECT_PRINT
3436 inline void ExternalDoubleArrayPrint() {
3437 ExternalDoubleArrayPrint(stdout);
3438 }
3439 void ExternalDoubleArrayPrint(FILE* out);
3440#endif // OBJECT_PRINT
3441#ifdef DEBUG
3442 void ExternalDoubleArrayVerify();
3443#endif // DEBUG
3444
3445 private:
3446 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalDoubleArray);
3447};
3448
3449
Ben Murdochb0fe1622011-05-05 13:52:32 +01003450// DeoptimizationInputData is a fixed array used to hold the deoptimization
3451// data for code generated by the Hydrogen/Lithium compiler. It also
3452// contains information about functions that were inlined. If N different
3453// functions were inlined then first N elements of the literal array will
3454// contain these functions.
3455//
3456// It can be empty.
3457class DeoptimizationInputData: public FixedArray {
3458 public:
3459 // Layout description. Indices in the array.
3460 static const int kTranslationByteArrayIndex = 0;
3461 static const int kInlinedFunctionCountIndex = 1;
3462 static const int kLiteralArrayIndex = 2;
3463 static const int kOsrAstIdIndex = 3;
3464 static const int kOsrPcOffsetIndex = 4;
3465 static const int kFirstDeoptEntryIndex = 5;
3466
3467 // Offsets of deopt entry elements relative to the start of the entry.
3468 static const int kAstIdOffset = 0;
3469 static const int kTranslationIndexOffset = 1;
3470 static const int kArgumentsStackHeightOffset = 2;
3471 static const int kDeoptEntrySize = 3;
3472
3473 // Simple element accessors.
3474#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3475 type* name() { \
3476 return type::cast(get(k##name##Index)); \
3477 } \
3478 void Set##name(type* value) { \
3479 set(k##name##Index, value); \
3480 }
3481
3482 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3483 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3484 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3485 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3486 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3487
3488 // Unchecked accessor to be used during GC.
3489 FixedArray* UncheckedLiteralArray() {
3490 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3491 }
3492
3493#undef DEFINE_ELEMENT_ACCESSORS
3494
3495 // Accessors for elements of the ith deoptimization entry.
3496#define DEFINE_ENTRY_ACCESSORS(name, type) \
3497 type* name(int i) { \
3498 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3499 } \
3500 void Set##name(int i, type* value) { \
3501 set(IndexForEntry(i) + k##name##Offset, value); \
3502 }
3503
3504 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3505 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3506 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3507
3508#undef DEFINE_ENTRY_ACCESSORS
3509
3510 int DeoptCount() {
3511 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3512 }
3513
3514 // Allocates a DeoptimizationInputData.
3515 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3516 PretenureFlag pretenure);
3517
3518 // Casting.
3519 static inline DeoptimizationInputData* cast(Object* obj);
3520
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003521#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +01003522 void DeoptimizationInputDataPrint(FILE* out);
3523#endif
3524
3525 private:
3526 static int IndexForEntry(int i) {
3527 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3528 }
3529
3530 static int LengthFor(int entry_count) {
3531 return IndexForEntry(entry_count);
3532 }
3533};
3534
3535
3536// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3537// data for code generated by the full compiler.
3538// The format of the these objects is
3539// [i * 2]: Ast ID for ith deoptimization.
3540// [i * 2 + 1]: PC and state of ith deoptimization
3541class DeoptimizationOutputData: public FixedArray {
3542 public:
3543 int DeoptPoints() { return length() / 2; }
3544 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3545 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3546 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3547 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3548
3549 static int LengthOfFixedArray(int deopt_points) {
3550 return deopt_points * 2;
3551 }
3552
3553 // Allocates a DeoptimizationOutputData.
3554 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3555 PretenureFlag pretenure);
3556
3557 // Casting.
3558 static inline DeoptimizationOutputData* cast(Object* obj);
3559
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003560#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003561 void DeoptimizationOutputDataPrint(FILE* out);
3562#endif
3563};
3564
3565
Ben Murdochb8e0da22011-05-16 14:20:40 +01003566class SafepointEntry;
3567
3568
Steve Blocka7e24c12009-10-30 11:49:00 +00003569// Code describes objects with on-the-fly generated machine code.
3570class Code: public HeapObject {
3571 public:
3572 // Opaque data type for encapsulating code flags like kind, inline
3573 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003574 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3575 // enumeration type has correct value range (see Issue 830 for more details).
3576 enum Flags {
3577 FLAGS_MIN_VALUE = kMinInt,
3578 FLAGS_MAX_VALUE = kMaxInt
3579 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003580
3581 enum Kind {
3582 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003583 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003584 STUB,
3585 BUILTIN,
3586 LOAD_IC,
3587 KEYED_LOAD_IC,
3588 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003589 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003590 STORE_IC,
3591 KEYED_STORE_IC,
Ben Murdoch257744e2011-11-30 15:57:28 +00003592 UNARY_OP_IC,
3593 BINARY_OP_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003594 COMPARE_IC,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003595 TO_BOOLEAN_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003596 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003597 // Flags.
3598
3599 // Pseudo-kinds.
3600 REGEXP = BUILTIN,
3601 FIRST_IC_KIND = LOAD_IC,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003602 LAST_IC_KIND = TO_BOOLEAN_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003603 };
3604
3605 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003606 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003607 };
3608
Ben Murdochb8e0da22011-05-16 14:20:40 +01003609 typedef int ExtraICState;
3610
3611 static const ExtraICState kNoExtraICState = 0;
3612
Steve Blocka7e24c12009-10-30 11:49:00 +00003613#ifdef ENABLE_DISASSEMBLER
3614 // Printing
3615 static const char* Kind2String(Kind kind);
3616 static const char* ICState2String(InlineCacheState state);
3617 static const char* PropertyType2String(PropertyType type);
Steve Block1e0659c2011-05-24 12:43:12 +01003618 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003619 inline void Disassemble(const char* name) {
3620 Disassemble(name, stdout);
3621 }
3622 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003623#endif // ENABLE_DISASSEMBLER
3624
3625 // [instruction_size]: Size of the native instructions
3626 inline int instruction_size();
3627 inline void set_instruction_size(int value);
3628
Leon Clarkeac952652010-07-15 11:15:24 +01003629 // [relocation_info]: Code relocation information
3630 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003631 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003632
Ben Murdochb0fe1622011-05-05 13:52:32 +01003633 // [deoptimization_data]: Array containing data for deopt.
3634 DECL_ACCESSORS(deoptimization_data, FixedArray)
3635
Ben Murdoch257744e2011-11-30 15:57:28 +00003636 // [code_flushing_candidate]: Field only used during garbage
3637 // collection to hold code flushing candidates. The contents of this
3638 // field does not have to be traced during garbage collection since
3639 // it is only used by the garbage collector itself.
3640 DECL_ACCESSORS(next_code_flushing_candidate, Object)
3641
Ben Murdochb0fe1622011-05-05 13:52:32 +01003642 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003643 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003644 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003645
Steve Blocka7e24c12009-10-30 11:49:00 +00003646 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003647
Steve Blocka7e24c12009-10-30 11:49:00 +00003648 // [flags]: Various code flags.
3649 inline Flags flags();
3650 inline void set_flags(Flags flags);
3651
3652 // [flags]: Access to specific code flags.
3653 inline Kind kind();
3654 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003655 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00003656 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3657 inline int arguments_count(); // Only valid for call IC stubs.
3658
3659 // Testers for IC stub kinds.
3660 inline bool is_inline_cache_stub();
3661 inline bool is_load_stub() { return kind() == LOAD_IC; }
3662 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3663 inline bool is_store_stub() { return kind() == STORE_IC; }
3664 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3665 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003666 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003667 inline bool is_unary_op_stub() { return kind() == UNARY_OP_IC; }
3668 inline bool is_binary_op_stub() { return kind() == BINARY_OP_IC; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003669 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003670 inline bool is_to_boolean_ic_stub() { return kind() == TO_BOOLEAN_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00003671
Steve Block6ded16b2010-05-10 14:33:55 +01003672 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003673 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003674 inline void set_major_key(int value);
3675
3676 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3677 inline bool optimizable();
3678 inline void set_optimizable(bool value);
3679
3680 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3681 // deoptimization support.
3682 inline bool has_deoptimization_support();
3683 inline void set_has_deoptimization_support(bool value);
3684
Ben Murdoch589d6972011-11-30 16:04:58 +00003685 // [has_debug_break_slots]: For FUNCTION kind, tells if it has
3686 // been compiled with debug break slots.
3687 inline bool has_debug_break_slots();
3688 inline void set_has_debug_break_slots(bool value);
3689
Ben Murdochb0fe1622011-05-05 13:52:32 +01003690 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3691 // how long the function has been marked for OSR and therefore which
3692 // level of loop nesting we are willing to do on-stack replacement
3693 // for.
3694 inline void set_allow_osr_at_loop_nesting_level(int level);
3695 inline int allow_osr_at_loop_nesting_level();
3696
3697 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3698 // reserved in the code prologue.
3699 inline unsigned stack_slots();
3700 inline void set_stack_slots(unsigned slots);
3701
3702 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3703 // the instruction stream where the safepoint table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003704 inline unsigned safepoint_table_offset();
3705 inline void set_safepoint_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003706
3707 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3708 // instruction stream where the stack check table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003709 inline unsigned stack_check_table_offset();
3710 inline void set_stack_check_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003711
3712 // [check type]: For kind CALL_IC, tells how to check if the
3713 // receiver is valid for the given call.
3714 inline CheckType check_type();
3715 inline void set_check_type(CheckType value);
3716
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003717 // [type-recording unary op type]: For kind UNARY_OP_IC.
Ben Murdoch257744e2011-11-30 15:57:28 +00003718 inline byte unary_op_type();
3719 inline void set_unary_op_type(byte value);
3720
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003721 // [type-recording binary op type]: For kind BINARY_OP_IC.
Ben Murdoch257744e2011-11-30 15:57:28 +00003722 inline byte binary_op_type();
3723 inline void set_binary_op_type(byte value);
3724 inline byte binary_op_result_type();
3725 inline void set_binary_op_result_type(byte value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003726
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003727 // [compare state]: For kind COMPARE_IC, tells what state the stub is in.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003728 inline byte compare_state();
3729 inline void set_compare_state(byte value);
3730
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003731 // [to_boolean_foo]: For kind TO_BOOLEAN_IC tells what state the stub is in.
3732 inline byte to_boolean_state();
3733 inline void set_to_boolean_state(byte value);
3734
Ben Murdochb8e0da22011-05-16 14:20:40 +01003735 // Get the safepoint entry for the given pc.
3736 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003737
3738 // Mark this code object as not having a stack check table. Assumes kind
3739 // is FUNCTION.
3740 void SetNoStackCheckTable();
3741
3742 // Find the first map in an IC stub.
3743 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003744
3745 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003746 static inline Flags ComputeFlags(
3747 Kind kind,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003748 InlineCacheState ic_state = UNINITIALIZED,
3749 ExtraICState extra_ic_state = kNoExtraICState,
3750 PropertyType type = NORMAL,
3751 int argc = -1,
3752 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003753
3754 static inline Flags ComputeMonomorphicFlags(
3755 Kind kind,
3756 PropertyType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003757 ExtraICState extra_ic_state = kNoExtraICState,
Steve Block8defd9f2010-07-08 12:39:36 +01003758 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003759 int argc = -1);
3760
Steve Blocka7e24c12009-10-30 11:49:00 +00003761 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003762 static inline PropertyType ExtractTypeFromFlags(Flags flags);
Ben Murdoch589d6972011-11-30 16:04:58 +00003763 static inline Kind ExtractKindFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003764 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Ben Murdoch589d6972011-11-30 16:04:58 +00003765 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
3766 static inline int ExtractArgumentsCountFromFlags(Flags flags);
3767
Steve Blocka7e24c12009-10-30 11:49:00 +00003768 static inline Flags RemoveTypeFromFlags(Flags flags);
3769
3770 // Convert a target address into a code object.
3771 static inline Code* GetCodeFromTargetAddress(Address address);
3772
Steve Block791712a2010-08-27 10:21:07 +01003773 // Convert an entry address into an object.
3774 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3775
Steve Blocka7e24c12009-10-30 11:49:00 +00003776 // Returns the address of the first instruction.
3777 inline byte* instruction_start();
3778
Leon Clarkeac952652010-07-15 11:15:24 +01003779 // Returns the address right after the last instruction.
3780 inline byte* instruction_end();
3781
Steve Blocka7e24c12009-10-30 11:49:00 +00003782 // Returns the size of the instructions, padding, and relocation information.
3783 inline int body_size();
3784
3785 // Returns the address of the first relocation info (read backwards!).
3786 inline byte* relocation_start();
3787
3788 // Code entry point.
3789 inline byte* entry();
3790
3791 // Returns true if pc is inside this object's instructions.
3792 inline bool contains(byte* pc);
3793
Steve Blocka7e24c12009-10-30 11:49:00 +00003794 // Relocate the code by delta bytes. Called to signal that this code
3795 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003796 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003797
3798 // Migrate code described by desc.
3799 void CopyFrom(const CodeDesc& desc);
3800
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003801 // Returns the object size for a given body (used for allocation).
3802 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003803 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003804 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003805 }
3806
3807 // Calculate the size of the code object to report for log events. This takes
3808 // the layout of the code object into account.
3809 int ExecutableSize() {
3810 // Check that the assumptions about the layout of the code object holds.
3811 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3812 Code::kHeaderSize);
3813 return instruction_size() + Code::kHeaderSize;
3814 }
3815
3816 // Locating source position.
3817 int SourcePosition(Address pc);
3818 int SourceStatementPosition(Address pc);
3819
3820 // Casting.
3821 static inline Code* cast(Object* obj);
3822
3823 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003824 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003825 inline void CodeIterateBody(ObjectVisitor* v);
3826
3827 template<typename StaticVisitor>
Steve Block44f0eee2011-05-26 01:26:41 +01003828 inline void CodeIterateBody(Heap* heap);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003829#ifdef OBJECT_PRINT
3830 inline void CodePrint() {
3831 CodePrint(stdout);
3832 }
3833 void CodePrint(FILE* out);
3834#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003835#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003836 void CodeVerify();
3837#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003838
Ben Murdoch8b112d22011-06-08 16:22:53 +01003839 // Returns the isolate/heap this code object belongs to.
3840 inline Isolate* isolate();
3841 inline Heap* heap();
3842
Ben Murdochb0fe1622011-05-05 13:52:32 +01003843 // Max loop nesting marker used to postpose OSR. We don't take loop
3844 // nesting that is deeper than 5 levels into account.
3845 static const int kMaxLoopNestingMarker = 6;
3846
Steve Blocka7e24c12009-10-30 11:49:00 +00003847 // Layout description.
3848 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003849 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003850 static const int kDeoptimizationDataOffset =
3851 kRelocationInfoOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00003852 static const int kNextCodeFlushingCandidateOffset =
3853 kDeoptimizationDataOffset + kPointerSize;
3854 static const int kFlagsOffset =
3855 kNextCodeFlushingCandidateOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003856
Ben Murdoch257744e2011-11-30 15:57:28 +00003857 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003858 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3859
3860 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3861 kKindSpecificFlagsSize;
3862
Steve Blocka7e24c12009-10-30 11:49:00 +00003863 // Add padding to align the instruction start following right after
3864 // the Code object header.
3865 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003866 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003867
3868 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003869 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3870 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3871 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3872 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3873
Ben Murdoch257744e2011-11-30 15:57:28 +00003874 static const int kUnaryOpTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003875 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003876 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3877 static const int kToBooleanTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdoch589d6972011-11-30 16:04:58 +00003878
3879 static const int kFullCodeFlags = kOptimizableOffset + 1;
3880 class FullCodeFlagsHasDeoptimizationSupportField:
3881 public BitField<bool, 0, 1> {}; // NOLINT
3882 class FullCodeFlagsHasDebugBreakSlotsField: public BitField<bool, 1, 1> {};
Ben Murdochb0fe1622011-05-05 13:52:32 +01003883
3884 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
Ben Murdoch589d6972011-11-30 16:04:58 +00003885
3886 static const int kAllowOSRAtLoopNestingLevelOffset = kFullCodeFlags + 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003887
Steve Block1e0659c2011-05-24 12:43:12 +01003888 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3889 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003890
Ben Murdoch589d6972011-11-30 16:04:58 +00003891 // Flags layout. BitField<type, shift, size>.
3892 class ICStateField: public BitField<InlineCacheState, 0, 3> {};
3893 class TypeField: public BitField<PropertyType, 3, 4> {};
3894 class KindField: public BitField<Kind, 7, 4> {};
3895 class CacheHolderField: public BitField<InlineCacheHolderFlag, 11, 1> {};
3896 class ExtraICStateField: public BitField<ExtraICState, 12, 2> {};
Steve Blocka7e24c12009-10-30 11:49:00 +00003897
Ben Murdoch589d6972011-11-30 16:04:58 +00003898 // Signed field cannot be encoded using the BitField class.
3899 static const int kArgumentsCountShift = 14;
3900 static const int kArgumentsCountMask = ~((1 << kArgumentsCountShift) - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00003901
3902 static const int kFlagsNotUsedInLookup =
Ben Murdoch589d6972011-11-30 16:04:58 +00003903 TypeField::kMask | CacheHolderField::kMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003904
3905 private:
3906 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3907};
3908
3909
3910// All heap objects have a Map that describes their structure.
3911// A Map contains information about:
3912// - Size information about the object
3913// - How to iterate over an object (for garbage collection)
3914class Map: public HeapObject {
3915 public:
3916 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003917 // Size in bytes or kVariableSizeSentinel if instances do not have
3918 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003919 inline int instance_size();
3920 inline void set_instance_size(int value);
3921
3922 // Count of properties allocated in the object.
3923 inline int inobject_properties();
3924 inline void set_inobject_properties(int value);
3925
3926 // Count of property fields pre-allocated in the object when first allocated.
3927 inline int pre_allocated_property_fields();
3928 inline void set_pre_allocated_property_fields(int value);
3929
3930 // Instance type.
3931 inline InstanceType instance_type();
3932 inline void set_instance_type(InstanceType value);
3933
3934 // Tells how many unused property fields are available in the
3935 // instance (only used for JSObject in fast mode).
3936 inline int unused_property_fields();
3937 inline void set_unused_property_fields(int value);
3938
3939 // Bit field.
3940 inline byte bit_field();
3941 inline void set_bit_field(byte value);
3942
3943 // Bit field 2.
3944 inline byte bit_field2();
3945 inline void set_bit_field2(byte value);
3946
Ben Murdoch257744e2011-11-30 15:57:28 +00003947 // Bit field 3.
3948 // TODO(1399): It should be possible to make room for bit_field3 in the map
3949 // without overloading the instance descriptors field (and storing it in the
3950 // DescriptorArray when the map has one).
3951 inline int bit_field3();
3952 inline void set_bit_field3(int value);
3953
Steve Blocka7e24c12009-10-30 11:49:00 +00003954 // Tells whether the object in the prototype property will be used
3955 // for instances created from this function. If the prototype
3956 // property is set to a value that is not a JSObject, the prototype
3957 // property will not be used to create instances of the function.
3958 // See ECMA-262, 13.2.2.
3959 inline void set_non_instance_prototype(bool value);
3960 inline bool has_non_instance_prototype();
3961
Steve Block6ded16b2010-05-10 14:33:55 +01003962 // Tells whether function has special prototype property. If not, prototype
3963 // property will not be created when accessed (will return undefined),
3964 // and construction from this function will not be allowed.
3965 inline void set_function_with_prototype(bool value);
3966 inline bool function_with_prototype();
3967
Steve Blocka7e24c12009-10-30 11:49:00 +00003968 // Tells whether the instance with this map should be ignored by the
3969 // __proto__ accessor.
3970 inline void set_is_hidden_prototype() {
3971 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3972 }
3973
3974 inline bool is_hidden_prototype() {
3975 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3976 }
3977
3978 // Records and queries whether the instance has a named interceptor.
3979 inline void set_has_named_interceptor() {
3980 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3981 }
3982
3983 inline bool has_named_interceptor() {
3984 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3985 }
3986
3987 // Records and queries whether the instance has an indexed interceptor.
3988 inline void set_has_indexed_interceptor() {
3989 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3990 }
3991
3992 inline bool has_indexed_interceptor() {
3993 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3994 }
3995
3996 // Tells whether the instance is undetectable.
3997 // An undetectable object is a special class of JSObject: 'typeof' operator
3998 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3999 // a normal JS object. It is useful for implementing undetectable
4000 // document.all in Firefox & Safari.
4001 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
4002 inline void set_is_undetectable() {
4003 set_bit_field(bit_field() | (1 << kIsUndetectable));
4004 }
4005
4006 inline bool is_undetectable() {
4007 return ((1 << kIsUndetectable) & bit_field()) != 0;
4008 }
4009
Steve Blocka7e24c12009-10-30 11:49:00 +00004010 // Tells whether the instance has a call-as-function handler.
4011 inline void set_has_instance_call_handler() {
4012 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
4013 }
4014
4015 inline bool has_instance_call_handler() {
4016 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
4017 }
4018
Steve Block8defd9f2010-07-08 12:39:36 +01004019 inline void set_is_extensible(bool value);
4020 inline bool is_extensible();
4021
Ben Murdoch589d6972011-11-30 16:04:58 +00004022 inline void set_elements_kind(ElementsKind elements_kind) {
4023 ASSERT(elements_kind < kElementsKindCount);
4024 ASSERT(kElementsKindCount <= (1 << kElementsKindBitCount));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004025 set_bit_field2((bit_field2() & ~kElementsKindMask) |
4026 (elements_kind << kElementsKindShift));
4027 ASSERT(this->elements_kind() == elements_kind);
4028 }
4029
Ben Murdoch589d6972011-11-30 16:04:58 +00004030 inline ElementsKind elements_kind() {
4031 return static_cast<ElementsKind>(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004032 (bit_field2() & kElementsKindMask) >> kElementsKindShift);
4033 }
4034
Steve Block8defd9f2010-07-08 12:39:36 +01004035 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01004036 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
Iain Merrick75681382010-08-19 15:07:18 +01004037 inline bool has_fast_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004038 return elements_kind() == FAST_ELEMENTS;
Leon Clarkee46be812010-01-19 14:06:41 +00004039 }
4040
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004041 inline bool has_fast_double_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004042 return elements_kind() == FAST_DOUBLE_ELEMENTS;
Steve Block1e0659c2011-05-24 12:43:12 +01004043 }
4044
Steve Block44f0eee2011-05-26 01:26:41 +01004045 inline bool has_external_array_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004046 ElementsKind kind(elements_kind());
4047 return kind >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
4048 kind <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004049 }
4050
4051 inline bool has_dictionary_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004052 return elements_kind() == DICTIONARY_ELEMENTS;
Steve Block1e0659c2011-05-24 12:43:12 +01004053 }
4054
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004055 // Tells whether the map is attached to SharedFunctionInfo
4056 // (for inobject slack tracking).
4057 inline void set_attached_to_shared_function_info(bool value);
4058
4059 inline bool attached_to_shared_function_info();
4060
4061 // Tells whether the map is shared between objects that may have different
4062 // behavior. If true, the map should never be modified, instead a clone
4063 // should be created and modified.
4064 inline void set_is_shared(bool value);
4065
4066 inline bool is_shared();
4067
Steve Blocka7e24c12009-10-30 11:49:00 +00004068 // Tells whether the instance needs security checks when accessing its
4069 // properties.
4070 inline void set_is_access_check_needed(bool access_check_needed);
4071 inline bool is_access_check_needed();
4072
4073 // [prototype]: implicit prototype object.
4074 DECL_ACCESSORS(prototype, Object)
4075
4076 // [constructor]: points back to the function responsible for this map.
4077 DECL_ACCESSORS(constructor, Object)
4078
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004079 inline JSFunction* unchecked_constructor();
4080
Ben Murdoch257744e2011-11-30 15:57:28 +00004081 // Should only be called by the code that initializes map to set initial valid
4082 // value of the instance descriptor member.
4083 inline void init_instance_descriptors();
4084
Steve Blocka7e24c12009-10-30 11:49:00 +00004085 // [instance descriptors]: describes the object.
4086 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
4087
Ben Murdoch257744e2011-11-30 15:57:28 +00004088 // Sets the instance descriptor array for the map to be an empty descriptor
4089 // array.
4090 inline void clear_instance_descriptors();
4091
Steve Blocka7e24c12009-10-30 11:49:00 +00004092 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01004093 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004094
Steve Block053d10c2011-06-13 19:13:29 +01004095 // [prototype transitions]: cache of prototype transitions.
4096 // Prototype transition is a transition that happens
4097 // when we change object's prototype to a new one.
4098 // Cache format:
4099 // 0: finger - index of the first free cell in the cache
4100 // 1 + 2 * i: prototype
4101 // 2 + 2 * i: target map
4102 DECL_ACCESSORS(prototype_transitions, FixedArray)
4103 inline FixedArray* unchecked_prototype_transitions();
4104
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004105 static const int kProtoTransitionHeaderSize = 1;
4106 static const int kProtoTransitionNumberOfEntriesOffset = 0;
4107 static const int kProtoTransitionElementsPerEntry = 2;
4108 static const int kProtoTransitionPrototypeOffset = 0;
4109 static const int kProtoTransitionMapOffset = 1;
4110
4111 inline int NumberOfProtoTransitions() {
4112 FixedArray* cache = unchecked_prototype_transitions();
4113 if (cache->length() == 0) return 0;
4114 return
4115 Smi::cast(cache->get(kProtoTransitionNumberOfEntriesOffset))->value();
4116 }
4117
4118 inline void SetNumberOfProtoTransitions(int value) {
4119 FixedArray* cache = unchecked_prototype_transitions();
4120 ASSERT(cache->length() != 0);
4121 cache->set_unchecked(kProtoTransitionNumberOfEntriesOffset,
4122 Smi::FromInt(value));
4123 }
4124
Ben Murdochb0fe1622011-05-05 13:52:32 +01004125 // Lookup in the map's instance descriptors and fill out the result
4126 // with the given holder if the name is found. The holder may be
4127 // NULL when this function is used from the compiler.
4128 void LookupInDescriptors(JSObject* holder,
4129 String* name,
4130 LookupResult* result);
4131
John Reck59135872010-11-02 12:39:01 -07004132 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004133
John Reck59135872010-11-02 12:39:01 -07004134 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
4135 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00004136
4137 // Returns a copy of the map, with all transitions dropped from the
4138 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07004139 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00004140
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004141 // Returns this map if it already has elements that are fast, otherwise
Steve Block8defd9f2010-07-08 12:39:36 +01004142 // returns a copy of the map, with all transitions dropped from the
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004143 // descriptors and the ElementsKind set to FAST_ELEMENTS.
John Reck59135872010-11-02 12:39:01 -07004144 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01004145
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004146 // Returns this map if it already has fast elements that are doubles,
4147 // otherwise returns a copy of the map, with all transitions dropped from the
4148 // descriptors and the ElementsKind set to FAST_DOUBLE_ELEMENTS.
4149 MUST_USE_RESULT inline MaybeObject* GetFastDoubleElementsMap();
4150
4151 // Returns this map if already has dictionary elements, otherwise returns a
4152 // copy of the map, with all transitions dropped from the descriptors and the
4153 // ElementsKind set to DICTIONARY_ELEMENTS.
John Reck59135872010-11-02 12:39:01 -07004154 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01004155
Steve Block44f0eee2011-05-26 01:26:41 +01004156 // Returns a new map with all transitions dropped from the descriptors and the
Ben Murdoch589d6972011-11-30 16:04:58 +00004157 // ElementsKind set.
4158 MUST_USE_RESULT MaybeObject* GetElementsTransitionMap(
4159 ElementsKind elements_kind,
Steve Block44f0eee2011-05-26 01:26:41 +01004160 bool safe_to_add_transition);
Steve Block1e0659c2011-05-24 12:43:12 +01004161
Steve Blocka7e24c12009-10-30 11:49:00 +00004162 // Returns the property index for name (only valid for FAST MODE).
4163 int PropertyIndexFor(String* name);
4164
4165 // Returns the next free property index (only valid for FAST MODE).
4166 int NextFreePropertyIndex();
4167
4168 // Returns the number of properties described in instance_descriptors.
4169 int NumberOfDescribedProperties();
4170
4171 // Casting.
4172 static inline Map* cast(Object* obj);
4173
4174 // Locate an accessor in the instance descriptor.
4175 AccessorDescriptor* FindAccessor(String* name);
4176
4177 // Code cache operations.
4178
4179 // Clears the code cache.
Steve Block44f0eee2011-05-26 01:26:41 +01004180 inline void ClearCodeCache(Heap* heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00004181
4182 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07004183 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004184
4185 // Returns the found code or undefined if absent.
4186 Object* FindInCodeCache(String* name, Code::Flags flags);
4187
4188 // Returns the non-negative index of the code object if it is in the
4189 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01004190 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004191
4192 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01004193 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00004194
4195 // For every transition in this map, makes the transition's
4196 // target's prototype pointer point back to this map.
4197 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
4198 void CreateBackPointers();
4199
4200 // Set all map transitions from this map to dead maps to null.
4201 // Also, restore the original prototype on the targets of these
4202 // transitions, so that we do not process this map again while
4203 // following back pointers.
Steve Block44f0eee2011-05-26 01:26:41 +01004204 void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00004205
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004206 // Computes a hash value for this map, to be used in HashTables and such.
4207 int Hash();
4208
4209 // Compares this map to another to see if they describe equivalent objects.
4210 // If |mode| is set to CLEAR_INOBJECT_PROPERTIES, |other| is treated as if
4211 // it had exactly zero inobject properties.
4212 // The "shared" flags of both this map and |other| are ignored.
4213 bool EquivalentToForNormalization(Map* other, PropertyNormalizationMode mode);
4214
4215 // Returns true if this map and |other| describe equivalent objects.
4216 // The "shared" flags of both this map and |other| are ignored.
4217 bool EquivalentTo(Map* other) {
4218 return EquivalentToForNormalization(other, KEEP_INOBJECT_PROPERTIES);
4219 }
4220
Steve Blocka7e24c12009-10-30 11:49:00 +00004221 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004222#ifdef OBJECT_PRINT
4223 inline void MapPrint() {
4224 MapPrint(stdout);
4225 }
4226 void MapPrint(FILE* out);
4227#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004228#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004229 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004230 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00004231#endif
4232
Iain Merrick75681382010-08-19 15:07:18 +01004233 inline int visitor_id();
4234 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004235
Steve Block44f0eee2011-05-26 01:26:41 +01004236 // Returns the isolate/heap this map belongs to.
4237 inline Isolate* isolate();
4238 inline Heap* heap();
4239
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004240 typedef void (*TraverseCallback)(Map* map, void* data);
4241
4242 void TraverseTransitionTree(TraverseCallback callback, void* data);
4243
Steve Block053d10c2011-06-13 19:13:29 +01004244 static const int kMaxCachedPrototypeTransitions = 256;
4245
4246 Object* GetPrototypeTransition(Object* prototype);
4247
4248 MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
4249
Steve Blocka7e24c12009-10-30 11:49:00 +00004250 static const int kMaxPreAllocatedPropertyFields = 255;
4251
4252 // Layout description.
4253 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
4254 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
4255 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
4256 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00004257 // Storage for instance descriptors is overloaded to also contain additional
4258 // map flags when unused (bit_field3). When the map has instance descriptors,
4259 // the flags are transferred to the instance descriptor array and accessed
4260 // through an extra indirection.
4261 // TODO(1399): It should be possible to make room for bit_field3 in the map
4262 // without overloading the instance descriptors field, but the map is
4263 // currently perfectly aligned to 32 bytes and extending it at all would
4264 // double its size. After the increment GC work lands, this size restriction
4265 // could be loosened and bit_field3 moved directly back in the map.
4266 static const int kInstanceDescriptorsOrBitField3Offset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004267 kConstructorOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00004268 static const int kCodeCacheOffset =
4269 kInstanceDescriptorsOrBitField3Offset + kPointerSize;
Steve Block053d10c2011-06-13 19:13:29 +01004270 static const int kPrototypeTransitionsOffset =
4271 kCodeCacheOffset + kPointerSize;
4272 static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004273 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
4274
4275 // Layout of pointer fields. Heap iteration code relies on them
4276 // being continiously allocated.
4277 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
4278 static const int kPointerFieldsEndOffset =
Steve Block053d10c2011-06-13 19:13:29 +01004279 Map::kPrototypeTransitionsOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004280
4281 // Byte offsets within kInstanceSizesOffset.
4282 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
4283 static const int kInObjectPropertiesByte = 1;
4284 static const int kInObjectPropertiesOffset =
4285 kInstanceSizesOffset + kInObjectPropertiesByte;
4286 static const int kPreAllocatedPropertyFieldsByte = 2;
4287 static const int kPreAllocatedPropertyFieldsOffset =
4288 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01004289 static const int kVisitorIdByte = 3;
4290 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00004291
4292 // Byte offsets within kInstanceAttributesOffset attributes.
4293 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
4294 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
4295 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
4296 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
4297
4298 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
4299
4300 // Bit positions for bit field.
4301 static const int kUnused = 0; // To be used for marking recently used maps.
4302 static const int kHasNonInstancePrototype = 1;
4303 static const int kIsHiddenPrototype = 2;
4304 static const int kHasNamedInterceptor = 3;
4305 static const int kHasIndexedInterceptor = 4;
4306 static const int kIsUndetectable = 5;
4307 static const int kHasInstanceCallHandler = 6;
4308 static const int kIsAccessCheckNeeded = 7;
4309
4310 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00004311 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01004312 static const int kFunctionWithPrototype = 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004313 static const int kStringWrapperSafeForDefaultValueOf = 2;
4314 static const int kAttachedToSharedFunctionInfo = 3;
4315 // No bits can be used after kElementsKindFirstBit, they are all reserved for
4316 // storing ElementKind. for anything other than storing the ElementKind.
4317 static const int kElementsKindShift = 4;
4318 static const int kElementsKindBitCount = 4;
4319
4320 // Derived values from bit field 2
4321 static const int kElementsKindMask = (-1 << kElementsKindShift) &
4322 ((1 << (kElementsKindShift + kElementsKindBitCount)) - 1);
4323 static const int8_t kMaximumBitField2FastElementValue = static_cast<int8_t>(
Ben Murdoch589d6972011-11-30 16:04:58 +00004324 (FAST_ELEMENTS + 1) << Map::kElementsKindShift) - 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00004325
4326 // Bit positions for bit field 3
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004327 static const int kIsShared = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01004328
4329 // Layout of the default cache. It holds alternating name and code objects.
4330 static const int kCodeCacheEntrySize = 2;
4331 static const int kCodeCacheEntryNameOffset = 0;
4332 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00004333
Iain Merrick75681382010-08-19 15:07:18 +01004334 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
4335 kPointerFieldsEndOffset,
4336 kSize> BodyDescriptor;
4337
Steve Blocka7e24c12009-10-30 11:49:00 +00004338 private:
4339 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
4340};
4341
4342
4343// An abstract superclass, a marker class really, for simple structure classes.
Ben Murdoch257744e2011-11-30 15:57:28 +00004344// It doesn't carry much functionality but allows struct classes to be
Steve Blocka7e24c12009-10-30 11:49:00 +00004345// identified in the type system.
4346class Struct: public HeapObject {
4347 public:
4348 inline void InitializeBody(int object_size);
4349 static inline Struct* cast(Object* that);
4350};
4351
4352
4353// Script describes a script which has been added to the VM.
4354class Script: public Struct {
4355 public:
4356 // Script types.
4357 enum Type {
4358 TYPE_NATIVE = 0,
4359 TYPE_EXTENSION = 1,
4360 TYPE_NORMAL = 2
4361 };
4362
4363 // Script compilation types.
4364 enum CompilationType {
4365 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004366 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00004367 };
4368
4369 // [source]: the script source.
4370 DECL_ACCESSORS(source, Object)
4371
4372 // [name]: the script name.
4373 DECL_ACCESSORS(name, Object)
4374
4375 // [id]: the script id.
4376 DECL_ACCESSORS(id, Object)
4377
4378 // [line_offset]: script line offset in resource from where it was extracted.
4379 DECL_ACCESSORS(line_offset, Smi)
4380
4381 // [column_offset]: script column offset in resource from where it was
4382 // extracted.
4383 DECL_ACCESSORS(column_offset, Smi)
4384
4385 // [data]: additional data associated with this script.
4386 DECL_ACCESSORS(data, Object)
4387
4388 // [context_data]: context data for the context this script was compiled in.
4389 DECL_ACCESSORS(context_data, Object)
4390
4391 // [wrapper]: the wrapper cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00004392 DECL_ACCESSORS(wrapper, Foreign)
Steve Blocka7e24c12009-10-30 11:49:00 +00004393
4394 // [type]: the script type.
4395 DECL_ACCESSORS(type, Smi)
4396
4397 // [compilation]: how the the script was compiled.
4398 DECL_ACCESSORS(compilation_type, Smi)
4399
Steve Blockd0582a62009-12-15 09:54:21 +00004400 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00004401 DECL_ACCESSORS(line_ends, Object)
4402
Steve Blockd0582a62009-12-15 09:54:21 +00004403 // [eval_from_shared]: for eval scripts the shared funcion info for the
4404 // function from which eval was called.
4405 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004406
4407 // [eval_from_instructions_offset]: the instruction offset in the code for the
4408 // function from which eval was called where eval was called.
4409 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
4410
4411 static inline Script* cast(Object* obj);
4412
Steve Block3ce2e202009-11-05 08:53:23 +00004413 // If script source is an external string, check that the underlying
4414 // resource is accessible. Otherwise, always return true.
4415 inline bool HasValidSource();
4416
Ben Murdochb0fe1622011-05-05 13:52:32 +01004417#ifdef OBJECT_PRINT
4418 inline void ScriptPrint() {
4419 ScriptPrint(stdout);
4420 }
4421 void ScriptPrint(FILE* out);
4422#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004423#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004424 void ScriptVerify();
4425#endif
4426
4427 static const int kSourceOffset = HeapObject::kHeaderSize;
4428 static const int kNameOffset = kSourceOffset + kPointerSize;
4429 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4430 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
4431 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
4432 static const int kContextOffset = kDataOffset + kPointerSize;
4433 static const int kWrapperOffset = kContextOffset + kPointerSize;
4434 static const int kTypeOffset = kWrapperOffset + kPointerSize;
4435 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
4436 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
4437 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00004438 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004439 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00004440 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004441 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
4442
4443 private:
4444 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4445};
4446
4447
Ben Murdochb0fe1622011-05-05 13:52:32 +01004448// List of builtin functions we want to identify to improve code
4449// generation.
4450//
4451// Each entry has a name of a global object property holding an object
4452// optionally followed by ".prototype", a name of a builtin function
4453// on the object (the one the id is set for), and a label.
4454//
4455// Installation of ids for the selected builtin functions is handled
4456// by the bootstrapper.
4457//
4458// NOTE: Order is important: math functions should be at the end of
4459// the list and MathFloor should be the first math function.
4460#define FUNCTIONS_WITH_ID_LIST(V) \
4461 V(Array.prototype, push, ArrayPush) \
4462 V(Array.prototype, pop, ArrayPop) \
Ben Murdoch42effa52011-08-19 16:40:31 +01004463 V(Function.prototype, apply, FunctionApply) \
Ben Murdochb0fe1622011-05-05 13:52:32 +01004464 V(String.prototype, charCodeAt, StringCharCodeAt) \
4465 V(String.prototype, charAt, StringCharAt) \
4466 V(String, fromCharCode, StringFromCharCode) \
4467 V(Math, floor, MathFloor) \
4468 V(Math, round, MathRound) \
4469 V(Math, ceil, MathCeil) \
4470 V(Math, abs, MathAbs) \
4471 V(Math, log, MathLog) \
4472 V(Math, sin, MathSin) \
4473 V(Math, cos, MathCos) \
4474 V(Math, tan, MathTan) \
4475 V(Math, asin, MathASin) \
4476 V(Math, acos, MathACos) \
4477 V(Math, atan, MathATan) \
4478 V(Math, exp, MathExp) \
4479 V(Math, sqrt, MathSqrt) \
4480 V(Math, pow, MathPow)
4481
4482
4483enum BuiltinFunctionId {
4484#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
4485 k##name,
4486 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4487#undef DECLARE_FUNCTION_ID
4488 // Fake id for a special case of Math.pow. Note, it continues the
4489 // list of math functions.
4490 kMathPowHalf,
4491 kFirstMathFunctionId = kMathFloor
4492};
4493
4494
Steve Blocka7e24c12009-10-30 11:49:00 +00004495// SharedFunctionInfo describes the JSFunction information that can be
4496// shared by multiple instances of the function.
4497class SharedFunctionInfo: public HeapObject {
4498 public:
4499 // [name]: Function name.
4500 DECL_ACCESSORS(name, Object)
4501
4502 // [code]: Function code.
4503 DECL_ACCESSORS(code, Code)
4504
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004505 // [scope_info]: Scope info.
4506 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
4507
Steve Blocka7e24c12009-10-30 11:49:00 +00004508 // [construct stub]: Code stub for constructing instances of this function.
4509 DECL_ACCESSORS(construct_stub, Code)
4510
Iain Merrick75681382010-08-19 15:07:18 +01004511 inline Code* unchecked_code();
4512
Steve Blocka7e24c12009-10-30 11:49:00 +00004513 // Returns if this function has been compiled to native code yet.
4514 inline bool is_compiled();
4515
4516 // [length]: The function length - usually the number of declared parameters.
4517 // Use up to 2^30 parameters.
4518 inline int length();
4519 inline void set_length(int value);
4520
4521 // [formal parameter count]: The declared number of parameters.
4522 inline int formal_parameter_count();
4523 inline void set_formal_parameter_count(int value);
4524
4525 // Set the formal parameter count so the function code will be
4526 // called without using argument adaptor frames.
4527 inline void DontAdaptArguments();
4528
4529 // [expected_nof_properties]: Expected number of properties for the function.
4530 inline int expected_nof_properties();
4531 inline void set_expected_nof_properties(int value);
4532
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004533 // Inobject slack tracking is the way to reclaim unused inobject space.
4534 //
4535 // The instance size is initially determined by adding some slack to
4536 // expected_nof_properties (to allow for a few extra properties added
4537 // after the constructor). There is no guarantee that the extra space
4538 // will not be wasted.
4539 //
4540 // Here is the algorithm to reclaim the unused inobject space:
4541 // - Detect the first constructor call for this SharedFunctionInfo.
4542 // When it happens enter the "in progress" state: remember the
4543 // constructor's initial_map and install a special construct stub that
4544 // counts constructor calls.
4545 // - While the tracking is in progress create objects filled with
4546 // one_pointer_filler_map instead of undefined_value. This way they can be
4547 // resized quickly and safely.
4548 // - Once enough (kGenerousAllocationCount) objects have been created
4549 // compute the 'slack' (traverse the map transition tree starting from the
4550 // initial_map and find the lowest value of unused_property_fields).
4551 // - Traverse the transition tree again and decrease the instance size
4552 // of every map. Existing objects will resize automatically (they are
4553 // filled with one_pointer_filler_map). All further allocations will
4554 // use the adjusted instance size.
4555 // - Decrease expected_nof_properties so that an allocations made from
4556 // another context will use the adjusted instance size too.
4557 // - Exit "in progress" state by clearing the reference to the initial_map
4558 // and setting the regular construct stub (generic or inline).
4559 //
4560 // The above is the main event sequence. Some special cases are possible
4561 // while the tracking is in progress:
4562 //
4563 // - GC occurs.
4564 // Check if the initial_map is referenced by any live objects (except this
4565 // SharedFunctionInfo). If it is, continue tracking as usual.
4566 // If it is not, clear the reference and reset the tracking state. The
4567 // tracking will be initiated again on the next constructor call.
4568 //
4569 // - The constructor is called from another context.
4570 // Immediately complete the tracking, perform all the necessary changes
4571 // to maps. This is necessary because there is no efficient way to track
4572 // multiple initial_maps.
4573 // Proceed to create an object in the current context (with the adjusted
4574 // size).
4575 //
4576 // - A different constructor function sharing the same SharedFunctionInfo is
4577 // called in the same context. This could be another closure in the same
4578 // context, or the first function could have been disposed.
4579 // This is handled the same way as the previous case.
4580 //
4581 // Important: inobject slack tracking is not attempted during the snapshot
4582 // creation.
4583
Ben Murdochf87a2032010-10-22 12:50:53 +01004584 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004585
4586 // [construction_count]: Counter for constructor calls made during
4587 // the tracking phase.
4588 inline int construction_count();
4589 inline void set_construction_count(int value);
4590
4591 // [initial_map]: initial map of the first function called as a constructor.
4592 // Saved for the duration of the tracking phase.
4593 // This is a weak link (GC resets it to undefined_value if no other live
4594 // object reference this map).
4595 DECL_ACCESSORS(initial_map, Object)
4596
4597 // True if the initial_map is not undefined and the countdown stub is
4598 // installed.
4599 inline bool IsInobjectSlackTrackingInProgress();
4600
4601 // Starts the tracking.
4602 // Stores the initial map and installs the countdown stub.
4603 // IsInobjectSlackTrackingInProgress is normally true after this call,
4604 // except when tracking have not been started (e.g. the map has no unused
4605 // properties or the snapshot is being built).
4606 void StartInobjectSlackTracking(Map* map);
4607
4608 // Completes the tracking.
4609 // IsInobjectSlackTrackingInProgress is false after this call.
4610 void CompleteInobjectSlackTracking();
4611
4612 // Clears the initial_map before the GC marking phase to ensure the reference
4613 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4614 void DetachInitialMap();
4615
4616 // Restores the link to the initial map after the GC marking phase.
4617 // IsInobjectSlackTrackingInProgress is true after this call.
4618 void AttachInitialMap(Map* map);
4619
4620 // False if there are definitely no live objects created from this function.
4621 // True if live objects _may_ exist (existence not guaranteed).
4622 // May go back from true to false after GC.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004623 DECL_BOOLEAN_ACCESSORS(live_objects_may_exist)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004624
Steve Blocka7e24c12009-10-30 11:49:00 +00004625 // [instance class name]: class name for instances.
4626 DECL_ACCESSORS(instance_class_name, Object)
4627
Steve Block6ded16b2010-05-10 14:33:55 +01004628 // [function data]: This field holds some additional data for function.
4629 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004630 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004631 // In the long run we don't want all functions to have this field but
4632 // we can fix that when we have a better model for storing hidden data
4633 // on objects.
4634 DECL_ACCESSORS(function_data, Object)
4635
Steve Block6ded16b2010-05-10 14:33:55 +01004636 inline bool IsApiFunction();
4637 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004638 inline bool HasBuiltinFunctionId();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004639 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004640
Steve Blocka7e24c12009-10-30 11:49:00 +00004641 // [script info]: Script from which the function originates.
4642 DECL_ACCESSORS(script, Object)
4643
Steve Block6ded16b2010-05-10 14:33:55 +01004644 // [num_literals]: Number of literals used by this function.
4645 inline int num_literals();
4646 inline void set_num_literals(int value);
4647
Steve Blocka7e24c12009-10-30 11:49:00 +00004648 // [start_position_and_type]: Field used to store both the source code
4649 // position, whether or not the function is a function expression,
4650 // and whether or not the function is a toplevel function. The two
4651 // least significants bit indicates whether the function is an
4652 // expression and the rest contains the source code position.
4653 inline int start_position_and_type();
4654 inline void set_start_position_and_type(int value);
4655
4656 // [debug info]: Debug information.
4657 DECL_ACCESSORS(debug_info, Object)
4658
4659 // [inferred name]: Name inferred from variable or property
4660 // assignment of this function. Used to facilitate debugging and
4661 // profiling of JavaScript code written in OO style, where almost
4662 // all functions are anonymous but are assigned to object
4663 // properties.
4664 DECL_ACCESSORS(inferred_name, String)
4665
Ben Murdochf87a2032010-10-22 12:50:53 +01004666 // The function's name if it is non-empty, otherwise the inferred name.
4667 String* DebugName();
4668
Steve Blocka7e24c12009-10-30 11:49:00 +00004669 // Position of the 'function' token in the script source.
4670 inline int function_token_position();
4671 inline void set_function_token_position(int function_token_position);
4672
4673 // Position of this function in the script source.
4674 inline int start_position();
4675 inline void set_start_position(int start_position);
4676
4677 // End position of this function in the script source.
4678 inline int end_position();
4679 inline void set_end_position(int end_position);
4680
4681 // Is this function a function expression in the source code.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004682 DECL_BOOLEAN_ACCESSORS(is_expression)
Steve Blocka7e24c12009-10-30 11:49:00 +00004683
4684 // Is this function a top-level function (scripts, evals).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004685 DECL_BOOLEAN_ACCESSORS(is_toplevel)
Steve Blocka7e24c12009-10-30 11:49:00 +00004686
4687 // Bit field containing various information collected by the compiler to
4688 // drive optimization.
4689 inline int compiler_hints();
4690 inline void set_compiler_hints(int value);
4691
Ben Murdochb0fe1622011-05-05 13:52:32 +01004692 // A counter used to determine when to stress the deoptimizer with a
4693 // deopt.
4694 inline Smi* deopt_counter();
4695 inline void set_deopt_counter(Smi* counter);
4696
Steve Blocka7e24c12009-10-30 11:49:00 +00004697 // Add information on assignments of the form this.x = ...;
4698 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004699 bool has_only_simple_this_property_assignments,
4700 FixedArray* this_property_assignments);
4701
4702 // Clear information on assignments of the form this.x = ...;
4703 void ClearThisPropertyAssignmentsInfo();
4704
4705 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004706 // this.x = y; where y is either a constant or refers to an argument.
4707 inline bool has_only_simple_this_property_assignments();
4708
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004709 // Indicates if this function can be lazy compiled.
4710 // This is used to determine if we can safely flush code from a function
4711 // when doing GC if we expect that the function will no longer be used.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004712 DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004713
Iain Merrick75681382010-08-19 15:07:18 +01004714 // Indicates how many full GCs this function has survived with assigned
4715 // code object. Used to determine when it is relatively safe to flush
4716 // this code object and replace it with lazy compilation stub.
4717 // Age is reset when GC notices that the code object is referenced
4718 // from the stack or compilation cache.
4719 inline int code_age();
4720 inline void set_code_age(int age);
4721
Ben Murdochb0fe1622011-05-05 13:52:32 +01004722 // Indicates whether optimizations have been disabled for this
4723 // shared function info. If a function is repeatedly optimized or if
4724 // we cannot optimize the function we disable optimization to avoid
4725 // spending time attempting to optimize it again.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004726 DECL_BOOLEAN_ACCESSORS(optimization_disabled)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004727
Steve Block1e0659c2011-05-24 12:43:12 +01004728 // Indicates whether the function is a strict mode function.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004729 DECL_BOOLEAN_ACCESSORS(strict_mode)
Steve Block1e0659c2011-05-24 12:43:12 +01004730
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004731 // False if the function definitely does not allocate an arguments object.
4732 DECL_BOOLEAN_ACCESSORS(uses_arguments)
4733
4734 // True if the function has any duplicated parameter names.
4735 DECL_BOOLEAN_ACCESSORS(has_duplicate_parameters)
4736
4737 // Indicates whether the function is a native function.
Ben Murdoch589d6972011-11-30 16:04:58 +00004738 // These needs special treatment in .call and .apply since
Ben Murdoch257744e2011-11-30 15:57:28 +00004739 // null passed as the receiver should not be translated to the
4740 // global object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004741 DECL_BOOLEAN_ACCESSORS(native)
4742
4743 // Indicates that the function was created by the Function function.
4744 // Though it's anonymous, toString should treat it as if it had the name
4745 // "anonymous". We don't set the name itself so that the system does not
4746 // see a binding for it.
4747 DECL_BOOLEAN_ACCESSORS(name_should_print_as_anonymous)
4748
4749 // Indicates whether the function is a bound function created using
4750 // the bind function.
4751 DECL_BOOLEAN_ACCESSORS(bound)
4752
4753 // Indicates that the function is anonymous (the name field can be set
4754 // through the API, which does not change this flag).
4755 DECL_BOOLEAN_ACCESSORS(is_anonymous)
Ben Murdoch257744e2011-11-30 15:57:28 +00004756
Ben Murdochb0fe1622011-05-05 13:52:32 +01004757 // Indicates whether or not the code in the shared function support
4758 // deoptimization.
4759 inline bool has_deoptimization_support();
4760
4761 // Enable deoptimization support through recompiled code.
4762 void EnableDeoptimizationSupport(Code* recompiled);
4763
Ben Murdoch257744e2011-11-30 15:57:28 +00004764 // Disable (further) attempted optimization of all functions sharing this
4765 // shared function info. The function is the one we actually tried to
4766 // optimize.
4767 void DisableOptimization(JSFunction* function);
4768
Ben Murdochb0fe1622011-05-05 13:52:32 +01004769 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4770 // code, returns whether it asserted (i.e., always true if assertions are
4771 // disabled).
4772 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004773
Andrei Popescu402d9372010-02-26 13:31:12 +00004774 // Check whether a inlined constructor can be generated with the given
4775 // prototype.
4776 bool CanGenerateInlineConstructor(Object* prototype);
4777
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004778 // Prevents further attempts to generate inline constructors.
4779 // To be called if generation failed for any reason.
4780 void ForbidInlineConstructor();
4781
Steve Blocka7e24c12009-10-30 11:49:00 +00004782 // For functions which only contains this property assignments this provides
4783 // access to the names for the properties assigned.
4784 DECL_ACCESSORS(this_property_assignments, Object)
4785 inline int this_property_assignments_count();
4786 inline void set_this_property_assignments_count(int value);
4787 String* GetThisPropertyAssignmentName(int index);
4788 bool IsThisPropertyAssignmentArgument(int index);
4789 int GetThisPropertyAssignmentArgument(int index);
4790 Object* GetThisPropertyAssignmentConstant(int index);
4791
4792 // [source code]: Source code for the function.
4793 bool HasSourceCode();
4794 Object* GetSourceCode();
4795
Ben Murdochb0fe1622011-05-05 13:52:32 +01004796 inline int opt_count();
4797 inline void set_opt_count(int opt_count);
4798
4799 // Source size of this function.
4800 int SourceSize();
4801
Steve Blocka7e24c12009-10-30 11:49:00 +00004802 // Calculate the instance size.
4803 int CalculateInstanceSize();
4804
4805 // Calculate the number of in-object properties.
4806 int CalculateInObjectProperties();
4807
4808 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004809 // Set max_length to -1 for unlimited length.
4810 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004811#ifdef OBJECT_PRINT
4812 inline void SharedFunctionInfoPrint() {
4813 SharedFunctionInfoPrint(stdout);
4814 }
4815 void SharedFunctionInfoPrint(FILE* out);
4816#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004817#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004818 void SharedFunctionInfoVerify();
4819#endif
4820
4821 // Casting.
4822 static inline SharedFunctionInfo* cast(Object* obj);
4823
4824 // Constants.
4825 static const int kDontAdaptArgumentsSentinel = -1;
4826
4827 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004828 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004829 static const int kNameOffset = HeapObject::kHeaderSize;
4830 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004831 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4832 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004833 static const int kInstanceClassNameOffset =
4834 kConstructStubOffset + kPointerSize;
4835 static const int kFunctionDataOffset =
4836 kInstanceClassNameOffset + kPointerSize;
4837 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4838 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4839 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004840 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004841 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004842 static const int kThisPropertyAssignmentsOffset =
4843 kInitialMapOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004844 static const int kDeoptCounterOffset =
4845 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004846#if V8_HOST_ARCH_32_BIT
4847 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004848 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004849 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004850 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4851 static const int kExpectedNofPropertiesOffset =
4852 kFormalParameterCountOffset + kPointerSize;
4853 static const int kNumLiteralsOffset =
4854 kExpectedNofPropertiesOffset + kPointerSize;
4855 static const int kStartPositionAndTypeOffset =
4856 kNumLiteralsOffset + kPointerSize;
4857 static const int kEndPositionOffset =
4858 kStartPositionAndTypeOffset + kPointerSize;
4859 static const int kFunctionTokenPositionOffset =
4860 kEndPositionOffset + kPointerSize;
4861 static const int kCompilerHintsOffset =
4862 kFunctionTokenPositionOffset + kPointerSize;
4863 static const int kThisPropertyAssignmentsCountOffset =
4864 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004865 static const int kOptCountOffset =
4866 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004867 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004868 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004869#else
4870 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004871 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004872 // garbage collections.
4873 // To avoid wasting space on 64-bit architectures we use
4874 // the following trick: we group integer fields into pairs
4875 // First integer in each pair is shifted left by 1.
4876 // By doing this we guarantee that LSB of each kPointerSize aligned
4877 // word is not set and thus this word cannot be treated as pointer
4878 // to HeapObject during old space traversal.
4879 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004880 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004881 static const int kFormalParameterCountOffset =
4882 kLengthOffset + kIntSize;
4883
Steve Blocka7e24c12009-10-30 11:49:00 +00004884 static const int kExpectedNofPropertiesOffset =
4885 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004886 static const int kNumLiteralsOffset =
4887 kExpectedNofPropertiesOffset + kIntSize;
4888
4889 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004890 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004891 static const int kStartPositionAndTypeOffset =
4892 kEndPositionOffset + kIntSize;
4893
4894 static const int kFunctionTokenPositionOffset =
4895 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004896 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004897 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004898
Steve Blocka7e24c12009-10-30 11:49:00 +00004899 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004900 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004901 static const int kOptCountOffset =
4902 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004903
Steve Block6ded16b2010-05-10 14:33:55 +01004904 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004905 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004906
4907#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004908
4909 // The construction counter for inobject slack tracking is stored in the
4910 // most significant byte of compiler_hints which is otherwise unused.
4911 // Its offset depends on the endian-ness of the architecture.
4912#if __BYTE_ORDER == __LITTLE_ENDIAN
4913 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4914#elif __BYTE_ORDER == __BIG_ENDIAN
4915 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4916#else
4917#error Unknown byte ordering
4918#endif
4919
Steve Block6ded16b2010-05-10 14:33:55 +01004920 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004921
Iain Merrick75681382010-08-19 15:07:18 +01004922 typedef FixedBodyDescriptor<kNameOffset,
4923 kThisPropertyAssignmentsOffset + kPointerSize,
4924 kSize> BodyDescriptor;
4925
Steve Blocka7e24c12009-10-30 11:49:00 +00004926 // Bit positions in start_position_and_type.
4927 // The source code start position is in the 30 most significant bits of
4928 // the start_position_and_type field.
4929 static const int kIsExpressionBit = 0;
4930 static const int kIsTopLevelBit = 1;
4931 static const int kStartPositionShift = 2;
4932 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4933
4934 // Bit positions in compiler_hints.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004935 static const int kCodeAgeSize = 3;
4936 static const int kCodeAgeMask = (1 << kCodeAgeSize) - 1;
4937
4938 enum CompilerHints {
4939 kHasOnlySimpleThisPropertyAssignments,
4940 kAllowLazyCompilation,
4941 kLiveObjectsMayExist,
4942 kCodeAgeShift,
4943 kOptimizationDisabled = kCodeAgeShift + kCodeAgeSize,
4944 kStrictModeFunction,
4945 kUsesArguments,
4946 kHasDuplicateParameters,
4947 kNative,
4948 kBoundFunction,
4949 kIsAnonymous,
4950 kNameShouldPrintAsAnonymous,
4951 kCompilerHintsCount // Pseudo entry
4952 };
Steve Blocka7e24c12009-10-30 11:49:00 +00004953
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004954 private:
4955#if V8_HOST_ARCH_32_BIT
4956 // On 32 bit platforms, compiler hints is a smi.
4957 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4958 static const int kCompilerHintsSize = kPointerSize;
4959#else
4960 // On 64 bit platforms, compiler hints is not a smi, see comment above.
4961 static const int kCompilerHintsSmiTagSize = 0;
4962 static const int kCompilerHintsSize = kIntSize;
4963#endif
4964
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004965 STATIC_ASSERT(SharedFunctionInfo::kCompilerHintsCount <=
4966 SharedFunctionInfo::kCompilerHintsSize * kBitsPerByte);
4967
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004968 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00004969 // Constants for optimizing codegen for strict mode function and
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004970 // native tests.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004971 // Allows to use byte-widgh instructions.
4972 static const int kStrictModeBitWithinByte =
4973 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4974
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004975 static const int kNativeBitWithinByte =
4976 (kNative + kCompilerHintsSmiTagSize) % kBitsPerByte;
Ben Murdoch257744e2011-11-30 15:57:28 +00004977
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004978#if __BYTE_ORDER == __LITTLE_ENDIAN
4979 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004980 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004981 static const int kNativeByteOffset = kCompilerHintsOffset +
4982 (kNative + kCompilerHintsSmiTagSize) / kBitsPerByte;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004983#elif __BYTE_ORDER == __BIG_ENDIAN
4984 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004985 (kCompilerHintsSize - 1) -
4986 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004987 static const int kNativeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004988 (kCompilerHintsSize - 1) -
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004989 ((kNative + kCompilerHintsSmiTagSize) / kBitsPerByte);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004990#else
4991#error Unknown byte ordering
4992#endif
4993
4994 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00004995 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4996};
4997
4998
4999// JSFunction describes JavaScript functions.
5000class JSFunction: public JSObject {
5001 public:
5002 // [prototype_or_initial_map]:
5003 DECL_ACCESSORS(prototype_or_initial_map, Object)
5004
Ben Murdoch589d6972011-11-30 16:04:58 +00005005 // [shared]: The information about the function that
Steve Blocka7e24c12009-10-30 11:49:00 +00005006 // can be shared by instances.
5007 DECL_ACCESSORS(shared, SharedFunctionInfo)
5008
Iain Merrick75681382010-08-19 15:07:18 +01005009 inline SharedFunctionInfo* unchecked_shared();
5010
Steve Blocka7e24c12009-10-30 11:49:00 +00005011 // [context]: The context for this function.
5012 inline Context* context();
5013 inline Object* unchecked_context();
5014 inline void set_context(Object* context);
5015
5016 // [code]: The generated code object for this function. Executed
5017 // when the function is invoked, e.g. foo() or new foo(). See
5018 // [[Call]] and [[Construct]] description in ECMA-262, section
5019 // 8.6.2, page 27.
5020 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005021 inline void set_code(Code* code);
5022 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00005023
Iain Merrick75681382010-08-19 15:07:18 +01005024 inline Code* unchecked_code();
5025
Steve Blocka7e24c12009-10-30 11:49:00 +00005026 // Tells whether this function is builtin.
5027 inline bool IsBuiltin();
5028
Ben Murdochb0fe1622011-05-05 13:52:32 +01005029 // Tells whether or not the function needs arguments adaption.
5030 inline bool NeedsArgumentsAdaption();
5031
5032 // Tells whether or not this function has been optimized.
5033 inline bool IsOptimized();
5034
Ben Murdoch8b112d22011-06-08 16:22:53 +01005035 // Tells whether or not this function can be optimized.
5036 inline bool IsOptimizable();
5037
Ben Murdochb0fe1622011-05-05 13:52:32 +01005038 // Mark this function for lazy recompilation. The function will be
5039 // recompiled the next time it is executed.
5040 void MarkForLazyRecompilation();
5041
5042 // Tells whether or not the function is already marked for lazy
5043 // recompilation.
5044 inline bool IsMarkedForLazyRecompilation();
5045
Ben Murdochb0fe1622011-05-05 13:52:32 +01005046 // Check whether or not this function is inlineable.
5047 bool IsInlineable();
5048
Steve Blocka7e24c12009-10-30 11:49:00 +00005049 // [literals]: Fixed array holding the materialized literals.
5050 //
5051 // If the function contains object, regexp or array literals, the
5052 // literals array prefix contains the object, regexp, and array
5053 // function to be used when creating these literals. This is
5054 // necessary so that we do not dynamically lookup the object, regexp
5055 // or array functions. Performing a dynamic lookup, we might end up
5056 // using the functions from a new context that we should not have
5057 // access to.
5058 DECL_ACCESSORS(literals, FixedArray)
5059
5060 // The initial map for an object created by this constructor.
5061 inline Map* initial_map();
5062 inline void set_initial_map(Map* value);
5063 inline bool has_initial_map();
5064
5065 // Get and set the prototype property on a JSFunction. If the
5066 // function has an initial map the prototype is set on the initial
5067 // map. Otherwise, the prototype is put in the initial map field
5068 // until an initial map is needed.
5069 inline bool has_prototype();
5070 inline bool has_instance_prototype();
5071 inline Object* prototype();
5072 inline Object* instance_prototype();
5073 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07005074 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005075
Steve Block6ded16b2010-05-10 14:33:55 +01005076 // After prototype is removed, it will not be created when accessed, and
5077 // [[Construct]] from this function will not be allowed.
5078 Object* RemovePrototype();
5079 inline bool should_have_prototype();
5080
Steve Blocka7e24c12009-10-30 11:49:00 +00005081 // Accessor for this function's initial map's [[class]]
5082 // property. This is primarily used by ECMA native functions. This
5083 // method sets the class_name field of this function's initial map
5084 // to a given value. It creates an initial map if this function does
5085 // not have one. Note that this method does not copy the initial map
5086 // if it has one already, but simply replaces it with the new value.
5087 // Instances created afterwards will have a map whose [[class]] is
5088 // set to 'value', but there is no guarantees on instances created
5089 // before.
5090 Object* SetInstanceClassName(String* name);
5091
5092 // Returns if this function has been compiled to native code yet.
5093 inline bool is_compiled();
5094
Ben Murdochb0fe1622011-05-05 13:52:32 +01005095 // [next_function_link]: Field for linking functions. This list is treated as
5096 // a weak list by the GC.
5097 DECL_ACCESSORS(next_function_link, Object)
5098
5099 // Prints the name of the function using PrintF.
5100 inline void PrintName() {
5101 PrintName(stdout);
5102 }
5103 void PrintName(FILE* out);
5104
Steve Blocka7e24c12009-10-30 11:49:00 +00005105 // Casting.
5106 static inline JSFunction* cast(Object* obj);
5107
Steve Block791712a2010-08-27 10:21:07 +01005108 // Iterates the objects, including code objects indirectly referenced
5109 // through pointers to the first instruction in the code object.
5110 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
5111
Steve Blocka7e24c12009-10-30 11:49:00 +00005112 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005113#ifdef OBJECT_PRINT
5114 inline void JSFunctionPrint() {
5115 JSFunctionPrint(stdout);
5116 }
5117 void JSFunctionPrint(FILE* out);
5118#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005119#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005120 void JSFunctionVerify();
5121#endif
5122
5123 // Returns the number of allocated literals.
5124 inline int NumberOfLiterals();
5125
5126 // Retrieve the global context from a function's literal array.
5127 static Context* GlobalContextFromLiterals(FixedArray* literals);
5128
Ben Murdochb0fe1622011-05-05 13:52:32 +01005129 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
5130 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01005131 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01005132 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01005133 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005134 static const int kSharedFunctionInfoOffset =
5135 kPrototypeOrInitialMapOffset + kPointerSize;
5136 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
5137 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005138 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
5139 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
5140 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005141
5142 // Layout of the literals array.
5143 static const int kLiteralsPrefixSize = 1;
5144 static const int kLiteralGlobalContextIndex = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005145
Steve Blocka7e24c12009-10-30 11:49:00 +00005146 private:
5147 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
5148};
5149
5150
5151// JSGlobalProxy's prototype must be a JSGlobalObject or null,
5152// and the prototype is hidden. JSGlobalProxy always delegates
5153// property accesses to its prototype if the prototype is not null.
5154//
5155// A JSGlobalProxy can be reinitialized which will preserve its identity.
5156//
5157// Accessing a JSGlobalProxy requires security check.
5158
5159class JSGlobalProxy : public JSObject {
5160 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00005161 // [context]: the owner global context of this global proxy object.
Steve Blocka7e24c12009-10-30 11:49:00 +00005162 // It is null value if this object is not used by any context.
5163 DECL_ACCESSORS(context, Object)
5164
5165 // Casting.
5166 static inline JSGlobalProxy* cast(Object* obj);
5167
5168 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005169#ifdef OBJECT_PRINT
5170 inline void JSGlobalProxyPrint() {
5171 JSGlobalProxyPrint(stdout);
5172 }
5173 void JSGlobalProxyPrint(FILE* out);
5174#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005175#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005176 void JSGlobalProxyVerify();
5177#endif
5178
5179 // Layout description.
5180 static const int kContextOffset = JSObject::kHeaderSize;
5181 static const int kSize = kContextOffset + kPointerSize;
5182
5183 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00005184 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
5185};
5186
5187
5188// Forward declaration.
5189class JSBuiltinsObject;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005190class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00005191
5192// Common super class for JavaScript global objects and the special
5193// builtins global objects.
5194class GlobalObject: public JSObject {
5195 public:
5196 // [builtins]: the object holding the runtime routines written in JS.
5197 DECL_ACCESSORS(builtins, JSBuiltinsObject)
5198
5199 // [global context]: the global context corresponding to this global object.
5200 DECL_ACCESSORS(global_context, Context)
5201
5202 // [global receiver]: the global receiver object of the context
5203 DECL_ACCESSORS(global_receiver, JSObject)
5204
5205 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005206 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00005207
John Reck59135872010-11-02 12:39:01 -07005208 // This is like GetProperty, but is used when you know the lookup won't fail
5209 // by throwing an exception. This is for the debug and builtins global
5210 // objects, where it is known which properties can be expected to be present
5211 // on the object.
5212 Object* GetPropertyNoExceptionThrown(String* key) {
5213 Object* answer = GetProperty(key)->ToObjectUnchecked();
5214 return answer;
5215 }
5216
Steve Blocka7e24c12009-10-30 11:49:00 +00005217 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07005218 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00005219
5220 // Casting.
5221 static inline GlobalObject* cast(Object* obj);
5222
5223 // Layout description.
5224 static const int kBuiltinsOffset = JSObject::kHeaderSize;
5225 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
5226 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
5227 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
5228
5229 private:
5230 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
5231
5232 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
5233};
5234
5235
5236// JavaScript global object.
5237class JSGlobalObject: public GlobalObject {
5238 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00005239 // Casting.
5240 static inline JSGlobalObject* cast(Object* obj);
5241
5242 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005243#ifdef OBJECT_PRINT
5244 inline void JSGlobalObjectPrint() {
5245 JSGlobalObjectPrint(stdout);
5246 }
5247 void JSGlobalObjectPrint(FILE* out);
5248#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005249#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005250 void JSGlobalObjectVerify();
5251#endif
5252
5253 // Layout description.
5254 static const int kSize = GlobalObject::kHeaderSize;
5255
5256 private:
5257 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
5258};
5259
5260
5261// Builtins global object which holds the runtime routines written in
5262// JavaScript.
5263class JSBuiltinsObject: public GlobalObject {
5264 public:
5265 // Accessors for the runtime routines written in JavaScript.
5266 inline Object* javascript_builtin(Builtins::JavaScript id);
5267 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
5268
Steve Block6ded16b2010-05-10 14:33:55 +01005269 // Accessors for code of the runtime routines written in JavaScript.
5270 inline Code* javascript_builtin_code(Builtins::JavaScript id);
5271 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
5272
Steve Blocka7e24c12009-10-30 11:49:00 +00005273 // Casting.
5274 static inline JSBuiltinsObject* cast(Object* obj);
5275
5276 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005277#ifdef OBJECT_PRINT
5278 inline void JSBuiltinsObjectPrint() {
5279 JSBuiltinsObjectPrint(stdout);
5280 }
5281 void JSBuiltinsObjectPrint(FILE* out);
5282#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005283#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005284 void JSBuiltinsObjectVerify();
5285#endif
5286
5287 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01005288 // room for two pointers per runtime routine written in javascript
5289 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00005290 static const int kJSBuiltinsCount = Builtins::id_count;
5291 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005292 static const int kJSBuiltinsCodeOffset =
5293 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005294 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01005295 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
5296
5297 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
5298 return kJSBuiltinsOffset + id * kPointerSize;
5299 }
5300
5301 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
5302 return kJSBuiltinsCodeOffset + id * kPointerSize;
5303 }
5304
Steve Blocka7e24c12009-10-30 11:49:00 +00005305 private:
5306 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
5307};
5308
5309
5310// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
5311class JSValue: public JSObject {
5312 public:
5313 // [value]: the object being wrapped.
5314 DECL_ACCESSORS(value, Object)
5315
5316 // Casting.
5317 static inline JSValue* cast(Object* obj);
5318
5319 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005320#ifdef OBJECT_PRINT
5321 inline void JSValuePrint() {
5322 JSValuePrint(stdout);
5323 }
5324 void JSValuePrint(FILE* out);
5325#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005326#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005327 void JSValueVerify();
5328#endif
5329
5330 // Layout description.
5331 static const int kValueOffset = JSObject::kHeaderSize;
5332 static const int kSize = kValueOffset + kPointerSize;
5333
5334 private:
5335 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
5336};
5337
Steve Block1e0659c2011-05-24 12:43:12 +01005338
5339// Representation of message objects used for error reporting through
5340// the API. The messages are formatted in JavaScript so this object is
5341// a real JavaScript object. The information used for formatting the
5342// error messages are not directly accessible from JavaScript to
5343// prevent leaking information to user code called during error
5344// formatting.
5345class JSMessageObject: public JSObject {
5346 public:
5347 // [type]: the type of error message.
5348 DECL_ACCESSORS(type, String)
5349
5350 // [arguments]: the arguments for formatting the error message.
5351 DECL_ACCESSORS(arguments, JSArray)
5352
5353 // [script]: the script from which the error message originated.
5354 DECL_ACCESSORS(script, Object)
5355
5356 // [stack_trace]: the stack trace for this error message.
5357 DECL_ACCESSORS(stack_trace, Object)
5358
5359 // [stack_frames]: an array of stack frames for this error object.
5360 DECL_ACCESSORS(stack_frames, Object)
5361
5362 // [start_position]: the start position in the script for the error message.
5363 inline int start_position();
5364 inline void set_start_position(int value);
5365
5366 // [end_position]: the end position in the script for the error message.
5367 inline int end_position();
5368 inline void set_end_position(int value);
5369
5370 // Casting.
5371 static inline JSMessageObject* cast(Object* obj);
5372
5373 // Dispatched behavior.
5374#ifdef OBJECT_PRINT
5375 inline void JSMessageObjectPrint() {
5376 JSMessageObjectPrint(stdout);
5377 }
5378 void JSMessageObjectPrint(FILE* out);
5379#endif
5380#ifdef DEBUG
5381 void JSMessageObjectVerify();
5382#endif
5383
5384 // Layout description.
5385 static const int kTypeOffset = JSObject::kHeaderSize;
5386 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
5387 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
5388 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
5389 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
5390 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
5391 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
5392 static const int kSize = kEndPositionOffset + kPointerSize;
5393
5394 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
5395 kStackFramesOffset + kPointerSize,
5396 kSize> BodyDescriptor;
5397};
5398
5399
Steve Blocka7e24c12009-10-30 11:49:00 +00005400// Regular expressions
5401// The regular expression holds a single reference to a FixedArray in
5402// the kDataOffset field.
5403// The FixedArray contains the following data:
5404// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
5405// - reference to the original source string
5406// - reference to the original flag string
5407// If it is an atom regexp
5408// - a reference to a literal string to search for
5409// If it is an irregexp regexp:
Ben Murdoch257744e2011-11-30 15:57:28 +00005410// - a reference to code for ASCII inputs (bytecode or compiled), or a smi
5411// used for tracking the last usage (used for code flushing).
5412// - a reference to code for UC16 inputs (bytecode or compiled), or a smi
5413// used for tracking the last usage (used for code flushing)..
Steve Blocka7e24c12009-10-30 11:49:00 +00005414// - max number of registers used by irregexp implementations.
5415// - number of capture registers (output values) of the regexp.
5416class JSRegExp: public JSObject {
5417 public:
5418 // Meaning of Type:
5419 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
5420 // ATOM: A simple string to match against using an indexOf operation.
5421 // IRREGEXP: Compiled with Irregexp.
5422 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
5423 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
5424 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
5425
5426 class Flags {
5427 public:
5428 explicit Flags(uint32_t value) : value_(value) { }
5429 bool is_global() { return (value_ & GLOBAL) != 0; }
5430 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
5431 bool is_multiline() { return (value_ & MULTILINE) != 0; }
5432 uint32_t value() { return value_; }
5433 private:
5434 uint32_t value_;
5435 };
5436
5437 DECL_ACCESSORS(data, Object)
5438
5439 inline Type TypeTag();
5440 inline int CaptureCount();
5441 inline Flags GetFlags();
5442 inline String* Pattern();
5443 inline Object* DataAt(int index);
5444 // Set implementation data after the object has been prepared.
5445 inline void SetDataAt(int index, Object* value);
Ben Murdoch257744e2011-11-30 15:57:28 +00005446
5447 // Used during GC when flushing code or setting age.
5448 inline Object* DataAtUnchecked(int index);
5449 inline void SetDataAtUnchecked(int index, Object* value, Heap* heap);
5450 inline Type TypeTagUnchecked();
5451
Steve Blocka7e24c12009-10-30 11:49:00 +00005452 static int code_index(bool is_ascii) {
5453 if (is_ascii) {
5454 return kIrregexpASCIICodeIndex;
5455 } else {
5456 return kIrregexpUC16CodeIndex;
5457 }
5458 }
5459
Ben Murdoch257744e2011-11-30 15:57:28 +00005460 static int saved_code_index(bool is_ascii) {
5461 if (is_ascii) {
5462 return kIrregexpASCIICodeSavedIndex;
5463 } else {
5464 return kIrregexpUC16CodeSavedIndex;
5465 }
5466 }
5467
Steve Blocka7e24c12009-10-30 11:49:00 +00005468 static inline JSRegExp* cast(Object* obj);
5469
5470 // Dispatched behavior.
5471#ifdef DEBUG
5472 void JSRegExpVerify();
5473#endif
5474
5475 static const int kDataOffset = JSObject::kHeaderSize;
5476 static const int kSize = kDataOffset + kPointerSize;
5477
5478 // Indices in the data array.
5479 static const int kTagIndex = 0;
5480 static const int kSourceIndex = kTagIndex + 1;
5481 static const int kFlagsIndex = kSourceIndex + 1;
5482 static const int kDataIndex = kFlagsIndex + 1;
5483 // The data fields are used in different ways depending on the
5484 // value of the tag.
5485 // Atom regexps (literal strings).
5486 static const int kAtomPatternIndex = kDataIndex;
5487
5488 static const int kAtomDataSize = kAtomPatternIndex + 1;
5489
5490 // Irregexp compiled code or bytecode for ASCII. If compilation
5491 // fails, this fields hold an exception object that should be
5492 // thrown if the regexp is used again.
5493 static const int kIrregexpASCIICodeIndex = kDataIndex;
5494 // Irregexp compiled code or bytecode for UC16. If compilation
5495 // fails, this fields hold an exception object that should be
5496 // thrown if the regexp is used again.
5497 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00005498
5499 // Saved instance of Irregexp compiled code or bytecode for ASCII that
5500 // is a potential candidate for flushing.
5501 static const int kIrregexpASCIICodeSavedIndex = kDataIndex + 2;
5502 // Saved instance of Irregexp compiled code or bytecode for UC16 that is
5503 // a potential candidate for flushing.
5504 static const int kIrregexpUC16CodeSavedIndex = kDataIndex + 3;
5505
Steve Blocka7e24c12009-10-30 11:49:00 +00005506 // Maximal number of registers used by either ASCII or UC16.
5507 // Only used to check that there is enough stack space
Ben Murdoch257744e2011-11-30 15:57:28 +00005508 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +00005509 // Number of captures in the compiled regexp.
Ben Murdoch257744e2011-11-30 15:57:28 +00005510 static const int kIrregexpCaptureCountIndex = kDataIndex + 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00005511
5512 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00005513
5514 // Offsets directly into the data fixed array.
5515 static const int kDataTagOffset =
5516 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5517 static const int kDataAsciiCodeOffset =
5518 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00005519 static const int kDataUC16CodeOffset =
5520 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00005521 static const int kIrregexpCaptureCountOffset =
5522 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005523
5524 // In-object fields.
5525 static const int kSourceFieldIndex = 0;
5526 static const int kGlobalFieldIndex = 1;
5527 static const int kIgnoreCaseFieldIndex = 2;
5528 static const int kMultilineFieldIndex = 3;
5529 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01005530 static const int kInObjectFieldCount = 5;
Ben Murdoch257744e2011-11-30 15:57:28 +00005531
5532 // The uninitialized value for a regexp code object.
5533 static const int kUninitializedValue = -1;
5534
5535 // The compilation error value for the regexp code object. The real error
5536 // object is in the saved code field.
5537 static const int kCompilationErrorValue = -2;
5538
5539 // When we store the sweep generation at which we moved the code from the
5540 // code index to the saved code index we mask it of to be in the [0:255]
5541 // range.
5542 static const int kCodeAgeMask = 0xff;
Steve Blocka7e24c12009-10-30 11:49:00 +00005543};
5544
5545
5546class CompilationCacheShape {
5547 public:
5548 static inline bool IsMatch(HashTableKey* key, Object* value) {
5549 return key->IsMatch(value);
5550 }
5551
5552 static inline uint32_t Hash(HashTableKey* key) {
5553 return key->Hash();
5554 }
5555
5556 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5557 return key->HashForObject(object);
5558 }
5559
John Reck59135872010-11-02 12:39:01 -07005560 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005561 return key->AsObject();
5562 }
5563
5564 static const int kPrefixSize = 0;
5565 static const int kEntrySize = 2;
5566};
5567
Steve Block3ce2e202009-11-05 08:53:23 +00005568
Steve Blocka7e24c12009-10-30 11:49:00 +00005569class CompilationCacheTable: public HashTable<CompilationCacheShape,
5570 HashTableKey*> {
5571 public:
5572 // Find cached value for a string key, otherwise return null.
5573 Object* Lookup(String* src);
Steve Block1e0659c2011-05-24 12:43:12 +01005574 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00005575 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005576 MaybeObject* Put(String* src, Object* value);
Steve Block1e0659c2011-05-24 12:43:12 +01005577 MaybeObject* PutEval(String* src,
5578 Context* context,
5579 SharedFunctionInfo* value);
John Reck59135872010-11-02 12:39:01 -07005580 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005581
Ben Murdochb0fe1622011-05-05 13:52:32 +01005582 // Remove given value from cache.
5583 void Remove(Object* value);
5584
Steve Blocka7e24c12009-10-30 11:49:00 +00005585 static inline CompilationCacheTable* cast(Object* obj);
5586
5587 private:
5588 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
5589};
5590
5591
Steve Block6ded16b2010-05-10 14:33:55 +01005592class CodeCache: public Struct {
5593 public:
5594 DECL_ACCESSORS(default_cache, FixedArray)
5595 DECL_ACCESSORS(normal_type_cache, Object)
5596
5597 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07005598 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005599
5600 // Lookup code object in the cache. Returns code object if found and undefined
5601 // if not.
5602 Object* Lookup(String* name, Code::Flags flags);
5603
5604 // Get the internal index of a code object in the cache. Returns -1 if the
5605 // code object is not in that cache. This index can be used to later call
5606 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5607 // RemoveByIndex.
5608 int GetIndex(Object* name, Code* code);
5609
5610 // Remove an object from the cache with the provided internal index.
5611 void RemoveByIndex(Object* name, Code* code, int index);
5612
5613 static inline CodeCache* cast(Object* obj);
5614
Ben Murdochb0fe1622011-05-05 13:52:32 +01005615#ifdef OBJECT_PRINT
5616 inline void CodeCachePrint() {
5617 CodeCachePrint(stdout);
5618 }
5619 void CodeCachePrint(FILE* out);
5620#endif
Steve Block6ded16b2010-05-10 14:33:55 +01005621#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01005622 void CodeCacheVerify();
5623#endif
5624
5625 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5626 static const int kNormalTypeCacheOffset =
5627 kDefaultCacheOffset + kPointerSize;
5628 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5629
5630 private:
John Reck59135872010-11-02 12:39:01 -07005631 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5632 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005633 Object* LookupDefaultCache(String* name, Code::Flags flags);
5634 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5635
5636 // Code cache layout of the default cache. Elements are alternating name and
5637 // code objects for non normal load/store/call IC's.
5638 static const int kCodeCacheEntrySize = 2;
5639 static const int kCodeCacheEntryNameOffset = 0;
5640 static const int kCodeCacheEntryCodeOffset = 1;
5641
5642 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5643};
5644
5645
5646class CodeCacheHashTableShape {
5647 public:
5648 static inline bool IsMatch(HashTableKey* key, Object* value) {
5649 return key->IsMatch(value);
5650 }
5651
5652 static inline uint32_t Hash(HashTableKey* key) {
5653 return key->Hash();
5654 }
5655
5656 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5657 return key->HashForObject(object);
5658 }
5659
John Reck59135872010-11-02 12:39:01 -07005660 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01005661 return key->AsObject();
5662 }
5663
5664 static const int kPrefixSize = 0;
5665 static const int kEntrySize = 2;
5666};
5667
5668
5669class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5670 HashTableKey*> {
5671 public:
5672 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005673 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005674
5675 int GetIndex(String* name, Code::Flags flags);
5676 void RemoveByIndex(int index);
5677
5678 static inline CodeCacheHashTable* cast(Object* obj);
5679
5680 // Initial size of the fixed array backing the hash table.
5681 static const int kInitialSize = 64;
5682
5683 private:
5684 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5685};
5686
5687
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005688class PolymorphicCodeCache: public Struct {
5689 public:
5690 DECL_ACCESSORS(cache, Object)
5691
5692 MUST_USE_RESULT MaybeObject* Update(MapList* maps,
5693 Code::Flags flags,
5694 Code* code);
5695 Object* Lookup(MapList* maps, Code::Flags flags);
5696
5697 static inline PolymorphicCodeCache* cast(Object* obj);
5698
5699#ifdef OBJECT_PRINT
5700 inline void PolymorphicCodeCachePrint() {
5701 PolymorphicCodeCachePrint(stdout);
5702 }
5703 void PolymorphicCodeCachePrint(FILE* out);
5704#endif
5705#ifdef DEBUG
5706 void PolymorphicCodeCacheVerify();
5707#endif
5708
5709 static const int kCacheOffset = HeapObject::kHeaderSize;
5710 static const int kSize = kCacheOffset + kPointerSize;
5711
5712 private:
5713 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCache);
5714};
5715
5716
5717class PolymorphicCodeCacheHashTable
5718 : public HashTable<CodeCacheHashTableShape, HashTableKey*> {
5719 public:
5720 Object* Lookup(MapList* maps, int code_kind);
5721 MUST_USE_RESULT MaybeObject* Put(MapList* maps, int code_kind, Code* code);
5722
5723 static inline PolymorphicCodeCacheHashTable* cast(Object* obj);
5724
5725 static const int kInitialSize = 64;
5726 private:
5727 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCacheHashTable);
5728};
5729
5730
Steve Blocka7e24c12009-10-30 11:49:00 +00005731enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5732enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5733
5734
5735class StringHasher {
5736 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +01005737 explicit inline StringHasher(int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005738
5739 // Returns true if the hash of this string can be computed without
5740 // looking at the contents.
5741 inline bool has_trivial_hash();
5742
5743 // Add a character to the hash and update the array index calculation.
5744 inline void AddCharacter(uc32 c);
5745
5746 // Adds a character to the hash but does not update the array index
5747 // calculation. This can only be called when it has been verified
5748 // that the input is not an array index.
5749 inline void AddCharacterNoIndex(uc32 c);
5750
5751 // Returns the value to store in the hash field of a string with
5752 // the given length and contents.
5753 uint32_t GetHashField();
5754
5755 // Returns true if the characters seen so far make up a legal array
5756 // index.
5757 bool is_array_index() { return is_array_index_; }
5758
5759 bool is_valid() { return is_valid_; }
5760
5761 void invalidate() { is_valid_ = false; }
5762
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005763 // Calculated hash value for a string consisting of 1 to
5764 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5765 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005766 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005767
Steve Blocka7e24c12009-10-30 11:49:00 +00005768 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00005769 uint32_t array_index() {
5770 ASSERT(is_array_index());
5771 return array_index_;
5772 }
5773
5774 inline uint32_t GetHash();
5775
5776 int length_;
5777 uint32_t raw_running_hash_;
5778 uint32_t array_index_;
5779 bool is_array_index_;
5780 bool is_first_char_;
5781 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00005782 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00005783};
5784
5785
Steve Block44f0eee2011-05-26 01:26:41 +01005786// Calculates string hash.
5787template <typename schar>
5788inline uint32_t HashSequentialString(const schar* chars, int length);
5789
5790
Steve Blocka7e24c12009-10-30 11:49:00 +00005791// The characteristics of a string are stored in its map. Retrieving these
5792// few bits of information is moderately expensive, involving two memory
5793// loads where the second is dependent on the first. To improve efficiency
5794// the shape of the string is given its own class so that it can be retrieved
5795// once and used for several string operations. A StringShape is small enough
5796// to be passed by value and is immutable, but be aware that flattening a
5797// string can potentially alter its shape. Also be aware that a GC caused by
5798// something else can alter the shape of a string due to ConsString
5799// shortcutting. Keeping these restrictions in mind has proven to be error-
5800// prone and so we no longer put StringShapes in variables unless there is a
5801// concrete performance benefit at that particular point in the code.
5802class StringShape BASE_EMBEDDED {
5803 public:
5804 inline explicit StringShape(String* s);
5805 inline explicit StringShape(Map* s);
5806 inline explicit StringShape(InstanceType t);
5807 inline bool IsSequential();
5808 inline bool IsExternal();
5809 inline bool IsCons();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005810 inline bool IsSliced();
5811 inline bool IsIndirect();
Steve Blocka7e24c12009-10-30 11:49:00 +00005812 inline bool IsExternalAscii();
5813 inline bool IsExternalTwoByte();
5814 inline bool IsSequentialAscii();
5815 inline bool IsSequentialTwoByte();
5816 inline bool IsSymbol();
5817 inline StringRepresentationTag representation_tag();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005818 inline uint32_t encoding_tag();
Steve Blocka7e24c12009-10-30 11:49:00 +00005819 inline uint32_t full_representation_tag();
5820 inline uint32_t size_tag();
5821#ifdef DEBUG
5822 inline uint32_t type() { return type_; }
5823 inline void invalidate() { valid_ = false; }
5824 inline bool valid() { return valid_; }
5825#else
5826 inline void invalidate() { }
5827#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005828
Steve Blocka7e24c12009-10-30 11:49:00 +00005829 private:
5830 uint32_t type_;
5831#ifdef DEBUG
5832 inline void set_valid() { valid_ = true; }
5833 bool valid_;
5834#else
5835 inline void set_valid() { }
5836#endif
5837};
5838
5839
5840// The String abstract class captures JavaScript string values:
5841//
5842// Ecma-262:
5843// 4.3.16 String Value
5844// A string value is a member of the type String and is a finite
5845// ordered sequence of zero or more 16-bit unsigned integer values.
5846//
5847// All string values have a length field.
5848class String: public HeapObject {
5849 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005850 // Representation of the flat content of a String.
5851 // A non-flat string doesn't have flat content.
5852 // A flat string has content that's encoded as a sequence of either
5853 // ASCII chars or two-byte UC16.
5854 // Returned by String::GetFlatContent().
5855 class FlatContent {
5856 public:
5857 // Returns true if the string is flat and this structure contains content.
5858 bool IsFlat() { return state_ != NON_FLAT; }
5859 // Returns true if the structure contains ASCII content.
5860 bool IsAscii() { return state_ == ASCII; }
5861 // Returns true if the structure contains two-byte content.
5862 bool IsTwoByte() { return state_ == TWO_BYTE; }
5863
5864 // Return the ASCII content of the string. Only use if IsAscii() returns
5865 // true.
5866 Vector<const char> ToAsciiVector() {
5867 ASSERT_EQ(ASCII, state_);
5868 return Vector<const char>::cast(buffer_);
5869 }
5870 // Return the two-byte content of the string. Only use if IsTwoByte()
5871 // returns true.
5872 Vector<const uc16> ToUC16Vector() {
5873 ASSERT_EQ(TWO_BYTE, state_);
5874 return Vector<const uc16>::cast(buffer_);
5875 }
5876
5877 private:
5878 enum State { NON_FLAT, ASCII, TWO_BYTE };
5879
5880 // Constructors only used by String::GetFlatContent().
5881 explicit FlatContent(Vector<const char> chars)
5882 : buffer_(Vector<const byte>::cast(chars)),
5883 state_(ASCII) { }
5884 explicit FlatContent(Vector<const uc16> chars)
5885 : buffer_(Vector<const byte>::cast(chars)),
5886 state_(TWO_BYTE) { }
5887 FlatContent() : buffer_(), state_(NON_FLAT) { }
5888
5889 Vector<const byte> buffer_;
5890 State state_;
5891
5892 friend class String;
5893 };
5894
Steve Blocka7e24c12009-10-30 11:49:00 +00005895 // Get and set the length of the string.
5896 inline int length();
5897 inline void set_length(int value);
5898
Steve Blockd0582a62009-12-15 09:54:21 +00005899 // Get and set the hash field of the string.
5900 inline uint32_t hash_field();
5901 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005902
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005903 // Returns whether this string has only ASCII chars, i.e. all of them can
5904 // be ASCII encoded. This might be the case even if the string is
5905 // two-byte. Such strings may appear when the embedder prefers
5906 // two-byte external representations even for ASCII data.
Steve Blocka7e24c12009-10-30 11:49:00 +00005907 inline bool IsAsciiRepresentation();
5908 inline bool IsTwoByteRepresentation();
5909
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005910 // Cons and slices have an encoding flag that may not represent the actual
5911 // encoding of the underlying string. This is taken into account here.
5912 // Requires: this->IsFlat()
5913 inline bool IsAsciiRepresentationUnderneath();
5914 inline bool IsTwoByteRepresentationUnderneath();
5915
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005916 // NOTE: this should be considered only a hint. False negatives are
5917 // possible.
5918 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005919
Steve Blocka7e24c12009-10-30 11:49:00 +00005920 // Get and set individual two byte chars in the string.
5921 inline void Set(int index, uint16_t value);
5922 // Get individual two byte char in the string. Repeated calls
5923 // to this method are not efficient unless the string is flat.
5924 inline uint16_t Get(int index);
5925
Leon Clarkef7060e22010-06-03 12:02:55 +01005926 // Try to flatten the string. Checks first inline to see if it is
5927 // necessary. Does nothing if the string is not a cons string.
5928 // Flattening allocates a sequential string with the same data as
5929 // the given string and mutates the cons string to a degenerate
5930 // form, where the first component is the new sequential string and
5931 // the second component is the empty string. If allocation fails,
5932 // this function returns a failure. If flattening succeeds, this
5933 // function returns the sequential string that is now the first
5934 // component of the cons string.
5935 //
5936 // Degenerate cons strings are handled specially by the garbage
5937 // collector (see IsShortcutCandidate).
5938 //
5939 // Use FlattenString from Handles.cc to flatten even in case an
5940 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07005941 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005942
Leon Clarkef7060e22010-06-03 12:02:55 +01005943 // Convenience function. Has exactly the same behavior as
5944 // TryFlatten(), except in the case of failure returns the original
5945 // string.
5946 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5947
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005948 // Tries to return the content of a flat string as a structure holding either
5949 // a flat vector of char or of uc16.
5950 // If the string isn't flat, and therefore doesn't have flat content, the
5951 // returned structure will report so, and can't provide a vector of either
5952 // kind.
5953 FlatContent GetFlatContent();
5954
5955 // Returns the parent of a sliced string or first part of a flat cons string.
5956 // Requires: StringShape(this).IsIndirect() && this->IsFlat()
5957 inline String* GetUnderlying();
Steve Blocka7e24c12009-10-30 11:49:00 +00005958
5959 // Mark the string as an undetectable object. It only applies to
5960 // ascii and two byte string types.
5961 bool MarkAsUndetectable();
5962
Steve Blockd0582a62009-12-15 09:54:21 +00005963 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07005964 MUST_USE_RESULT MaybeObject* SubString(int from,
5965 int to,
5966 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005967
5968 // String equality operations.
5969 inline bool Equals(String* other);
5970 bool IsEqualTo(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +01005971 bool IsAsciiEqualTo(Vector<const char> str);
5972 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00005973
5974 // Return a UTF8 representation of the string. The string is null
5975 // terminated but may optionally contain nulls. Length is returned
5976 // in length_output if length_output is not a null pointer The string
5977 // should be nearly flat, otherwise the performance of this method may
5978 // be very slow (quadratic in the length). Setting robustness_flag to
5979 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5980 // handles unexpected data without causing assert failures and it does not
5981 // do any heap allocations. This is useful when printing stack traces.
Ben Murdoch589d6972011-11-30 16:04:58 +00005982 SmartArrayPointer<char> ToCString(AllowNullsFlag allow_nulls,
5983 RobustnessFlag robustness_flag,
5984 int offset,
5985 int length,
5986 int* length_output = 0);
5987 SmartArrayPointer<char> ToCString(
Steve Blocka7e24c12009-10-30 11:49:00 +00005988 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5989 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5990 int* length_output = 0);
5991
5992 int Utf8Length();
5993
5994 // Return a 16 bit Unicode representation of the string.
5995 // The string should be nearly flat, otherwise the performance of
5996 // of this method may be very bad. Setting robustness_flag to
5997 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5998 // handles unexpected data without causing assert failures and it does not
5999 // do any heap allocations. This is useful when printing stack traces.
Ben Murdoch589d6972011-11-30 16:04:58 +00006000 SmartArrayPointer<uc16> ToWideCString(
Steve Blocka7e24c12009-10-30 11:49:00 +00006001 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
6002
6003 // Tells whether the hash code has been computed.
6004 inline bool HasHashCode();
6005
6006 // Returns a hash value used for the property table
6007 inline uint32_t Hash();
6008
Steve Blockd0582a62009-12-15 09:54:21 +00006009 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
6010 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00006011
6012 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
6013 uint32_t* index,
6014 int length);
6015
6016 // Externalization.
6017 bool MakeExternal(v8::String::ExternalStringResource* resource);
6018 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
6019
6020 // Conversion.
6021 inline bool AsArrayIndex(uint32_t* index);
6022
6023 // Casting.
6024 static inline String* cast(Object* obj);
6025
6026 void PrintOn(FILE* out);
6027
6028 // For use during stack traces. Performs rudimentary sanity check.
6029 bool LooksValid();
6030
6031 // Dispatched behavior.
6032 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006033#ifdef OBJECT_PRINT
6034 inline void StringPrint() {
6035 StringPrint(stdout);
6036 }
6037 void StringPrint(FILE* out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006038
6039 char* ToAsciiArray();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006040#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006041#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006042 void StringVerify();
6043#endif
6044 inline bool IsFlat();
6045
6046 // Layout description.
6047 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01006048 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006049 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006050
Steve Blockd0582a62009-12-15 09:54:21 +00006051 // Maximum number of characters to consider when trying to convert a string
6052 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00006053 static const int kMaxArrayIndexSize = 10;
6054
6055 // Max ascii char code.
6056 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
6057 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
6058 static const int kMaxUC16CharCode = 0xffff;
6059
Steve Blockd0582a62009-12-15 09:54:21 +00006060 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00006061 static const int kMinNonFlatLength = 13;
6062
6063 // Mask constant for checking if a string has a computed hash code
6064 // and if it is an array index. The least significant bit indicates
6065 // whether a hash code has been computed. If the hash code has been
6066 // computed the 2nd bit tells whether the string can be used as an
6067 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006068 static const int kHashNotComputedMask = 1;
6069 static const int kIsNotArrayIndexMask = 1 << 1;
6070 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00006071
Steve Blockd0582a62009-12-15 09:54:21 +00006072 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006073 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00006074
Steve Blocka7e24c12009-10-30 11:49:00 +00006075 // Array index strings this short can keep their index in the hash
6076 // field.
6077 static const int kMaxCachedArrayIndexLength = 7;
6078
Steve Blockd0582a62009-12-15 09:54:21 +00006079 // For strings which are array indexes the hash value has the string length
6080 // mixed into the hash, mainly to avoid a hash value of zero which would be
6081 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006082 static const int kArrayIndexValueBits = 24;
6083 static const int kArrayIndexLengthBits =
6084 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
6085
6086 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01006087 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006088
6089 static const int kArrayIndexHashLengthShift =
6090 kArrayIndexValueBits + kNofHashBitFields;
6091
Steve Blockd0582a62009-12-15 09:54:21 +00006092 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006093
6094 static const int kArrayIndexValueMask =
6095 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
6096
6097 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
6098 // could use a mask to test if the length of string is less than or equal to
6099 // kMaxCachedArrayIndexLength.
6100 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
6101
6102 static const int kContainsCachedArrayIndexMask =
6103 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
6104 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00006105
6106 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006107 static const int kEmptyHashField =
6108 kIsNotArrayIndexMask | kHashNotComputedMask;
6109
6110 // Value of hash field containing computed hash equal to zero.
6111 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00006112
6113 // Maximal string length.
6114 static const int kMaxLength = (1 << (32 - 2)) - 1;
6115
6116 // Max length for computing hash. For strings longer than this limit the
6117 // string length is used as the hash value.
6118 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00006119
6120 // Limit for truncation in short printing.
6121 static const int kMaxShortPrintLength = 1024;
6122
6123 // Support for regular expressions.
6124 const uc16* GetTwoByteData();
6125 const uc16* GetTwoByteData(unsigned start);
6126
6127 // Support for StringInputBuffer
6128 static const unibrow::byte* ReadBlock(String* input,
6129 unibrow::byte* util_buffer,
6130 unsigned capacity,
6131 unsigned* remaining,
6132 unsigned* offset);
6133 static const unibrow::byte* ReadBlock(String** input,
6134 unibrow::byte* util_buffer,
6135 unsigned capacity,
6136 unsigned* remaining,
6137 unsigned* offset);
6138
6139 // Helper function for flattening strings.
6140 template <typename sinkchar>
6141 static void WriteToFlat(String* source,
6142 sinkchar* sink,
6143 int from,
6144 int to);
6145
Steve Block9fac8402011-05-12 15:51:54 +01006146 static inline bool IsAscii(const char* chars, int length) {
6147 const char* limit = chars + length;
6148#ifdef V8_HOST_CAN_READ_UNALIGNED
6149 ASSERT(kMaxAsciiCharCode == 0x7F);
6150 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
6151 while (chars <= limit - sizeof(uintptr_t)) {
6152 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
6153 return false;
6154 }
6155 chars += sizeof(uintptr_t);
6156 }
6157#endif
6158 while (chars < limit) {
6159 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
6160 ++chars;
6161 }
6162 return true;
6163 }
6164
6165 static inline bool IsAscii(const uc16* chars, int length) {
6166 const uc16* limit = chars + length;
6167 while (chars < limit) {
6168 if (*chars > kMaxAsciiCharCodeU) return false;
6169 ++chars;
6170 }
6171 return true;
6172 }
6173
Steve Blocka7e24c12009-10-30 11:49:00 +00006174 protected:
6175 class ReadBlockBuffer {
6176 public:
6177 ReadBlockBuffer(unibrow::byte* util_buffer_,
6178 unsigned cursor_,
6179 unsigned capacity_,
6180 unsigned remaining_) :
6181 util_buffer(util_buffer_),
6182 cursor(cursor_),
6183 capacity(capacity_),
6184 remaining(remaining_) {
6185 }
6186 unibrow::byte* util_buffer;
6187 unsigned cursor;
6188 unsigned capacity;
6189 unsigned remaining;
6190 };
6191
Steve Blocka7e24c12009-10-30 11:49:00 +00006192 static inline const unibrow::byte* ReadBlock(String* input,
6193 ReadBlockBuffer* buffer,
6194 unsigned* offset,
6195 unsigned max_chars);
6196 static void ReadBlockIntoBuffer(String* input,
6197 ReadBlockBuffer* buffer,
6198 unsigned* offset_ptr,
6199 unsigned max_chars);
6200
6201 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01006202 // Try to flatten the top level ConsString that is hiding behind this
6203 // string. This is a no-op unless the string is a ConsString. Flatten
6204 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07006205 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01006206
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006207 static inline bool IsHashFieldComputed(uint32_t field);
6208
Steve Blocka7e24c12009-10-30 11:49:00 +00006209 // Slow case of String::Equals. This implementation works on any strings
6210 // but it is most efficient on strings that are almost flat.
6211 bool SlowEquals(String* other);
6212
6213 // Slow case of AsArrayIndex.
6214 bool SlowAsArrayIndex(uint32_t* index);
6215
6216 // Compute and set the hash code.
6217 uint32_t ComputeAndSetHash();
6218
6219 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
6220};
6221
6222
6223// The SeqString abstract class captures sequential string values.
6224class SeqString: public String {
6225 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00006226 // Casting.
6227 static inline SeqString* cast(Object* obj);
6228
Steve Blocka7e24c12009-10-30 11:49:00 +00006229 private:
6230 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
6231};
6232
6233
6234// The AsciiString class captures sequential ascii string objects.
6235// Each character in the AsciiString is an ascii character.
6236class SeqAsciiString: public SeqString {
6237 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006238 static const bool kHasAsciiEncoding = true;
6239
Steve Blocka7e24c12009-10-30 11:49:00 +00006240 // Dispatched behavior.
6241 inline uint16_t SeqAsciiStringGet(int index);
6242 inline void SeqAsciiStringSet(int index, uint16_t value);
6243
6244 // Get the address of the characters in this string.
6245 inline Address GetCharsAddress();
6246
6247 inline char* GetChars();
6248
6249 // Casting
6250 static inline SeqAsciiString* cast(Object* obj);
6251
6252 // Garbage collection support. This method is called by the
6253 // garbage collector to compute the actual size of an AsciiString
6254 // instance.
6255 inline int SeqAsciiStringSize(InstanceType instance_type);
6256
6257 // Computes the size for an AsciiString instance of a given length.
6258 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006259 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00006260 }
6261
6262 // Layout description.
6263 static const int kHeaderSize = String::kSize;
6264 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
6265
Leon Clarkee46be812010-01-19 14:06:41 +00006266 // Maximal memory usage for a single sequential ASCII string.
6267 static const int kMaxSize = 512 * MB;
6268 // Maximal length of a single sequential ASCII string.
6269 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6270 static const int kMaxLength = (kMaxSize - kHeaderSize);
6271
Steve Blocka7e24c12009-10-30 11:49:00 +00006272 // Support for StringInputBuffer.
6273 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6274 unsigned* offset,
6275 unsigned chars);
6276 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
6277 unsigned* offset,
6278 unsigned chars);
6279
6280 private:
6281 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
6282};
6283
6284
6285// The TwoByteString class captures sequential unicode string objects.
6286// Each character in the TwoByteString is a two-byte uint16_t.
6287class SeqTwoByteString: public SeqString {
6288 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006289 static const bool kHasAsciiEncoding = false;
6290
Steve Blocka7e24c12009-10-30 11:49:00 +00006291 // Dispatched behavior.
6292 inline uint16_t SeqTwoByteStringGet(int index);
6293 inline void SeqTwoByteStringSet(int index, uint16_t value);
6294
6295 // Get the address of the characters in this string.
6296 inline Address GetCharsAddress();
6297
6298 inline uc16* GetChars();
6299
6300 // For regexp code.
6301 const uint16_t* SeqTwoByteStringGetData(unsigned start);
6302
6303 // Casting
6304 static inline SeqTwoByteString* cast(Object* obj);
6305
6306 // Garbage collection support. This method is called by the
6307 // garbage collector to compute the actual size of a TwoByteString
6308 // instance.
6309 inline int SeqTwoByteStringSize(InstanceType instance_type);
6310
6311 // Computes the size for a TwoByteString instance of a given length.
6312 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006313 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00006314 }
6315
6316 // Layout description.
6317 static const int kHeaderSize = String::kSize;
6318 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
6319
Leon Clarkee46be812010-01-19 14:06:41 +00006320 // Maximal memory usage for a single sequential two-byte string.
6321 static const int kMaxSize = 512 * MB;
6322 // Maximal length of a single sequential two-byte string.
6323 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6324 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
6325
Steve Blocka7e24c12009-10-30 11:49:00 +00006326 // Support for StringInputBuffer.
6327 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6328 unsigned* offset_ptr,
6329 unsigned chars);
6330
6331 private:
6332 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
6333};
6334
6335
6336// The ConsString class describes string values built by using the
6337// addition operator on strings. A ConsString is a pair where the
6338// first and second components are pointers to other string values.
6339// One or both components of a ConsString can be pointers to other
6340// ConsStrings, creating a binary tree of ConsStrings where the leaves
6341// are non-ConsString string values. The string value represented by
6342// a ConsString can be obtained by concatenating the leaf string
6343// values in a left-to-right depth-first traversal of the tree.
6344class ConsString: public String {
6345 public:
6346 // First string of the cons cell.
6347 inline String* first();
6348 // Doesn't check that the result is a string, even in debug mode. This is
6349 // useful during GC where the mark bits confuse the checks.
6350 inline Object* unchecked_first();
6351 inline void set_first(String* first,
6352 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
6353
6354 // Second string of the cons cell.
6355 inline String* second();
6356 // Doesn't check that the result is a string, even in debug mode. This is
6357 // useful during GC where the mark bits confuse the checks.
6358 inline Object* unchecked_second();
6359 inline void set_second(String* second,
6360 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
6361
6362 // Dispatched behavior.
6363 uint16_t ConsStringGet(int index);
6364
6365 // Casting.
6366 static inline ConsString* cast(Object* obj);
6367
Steve Blocka7e24c12009-10-30 11:49:00 +00006368 // Layout description.
6369 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
6370 static const int kSecondOffset = kFirstOffset + kPointerSize;
6371 static const int kSize = kSecondOffset + kPointerSize;
6372
6373 // Support for StringInputBuffer.
6374 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
6375 unsigned* offset_ptr,
6376 unsigned chars);
6377 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6378 unsigned* offset_ptr,
6379 unsigned chars);
6380
6381 // Minimum length for a cons string.
6382 static const int kMinLength = 13;
6383
Iain Merrick75681382010-08-19 15:07:18 +01006384 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
6385 BodyDescriptor;
6386
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006387#ifdef DEBUG
6388 void ConsStringVerify();
6389#endif
6390
Steve Blocka7e24c12009-10-30 11:49:00 +00006391 private:
6392 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
6393};
6394
6395
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006396// The Sliced String class describes strings that are substrings of another
6397// sequential string. The motivation is to save time and memory when creating
6398// a substring. A Sliced String is described as a pointer to the parent,
6399// the offset from the start of the parent string and the length. Using
6400// a Sliced String therefore requires unpacking of the parent string and
6401// adding the offset to the start address. A substring of a Sliced String
6402// are not nested since the double indirection is simplified when creating
6403// such a substring.
6404// Currently missing features are:
6405// - handling externalized parent strings
6406// - external strings as parent
6407// - truncating sliced string to enable otherwise unneeded parent to be GC'ed.
6408class SlicedString: public String {
6409 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006410 inline String* parent();
6411 inline void set_parent(String* parent);
6412 inline int offset();
6413 inline void set_offset(int offset);
6414
6415 // Dispatched behavior.
6416 uint16_t SlicedStringGet(int index);
6417
6418 // Casting.
6419 static inline SlicedString* cast(Object* obj);
6420
6421 // Layout description.
6422 static const int kParentOffset = POINTER_SIZE_ALIGN(String::kSize);
6423 static const int kOffsetOffset = kParentOffset + kPointerSize;
6424 static const int kSize = kOffsetOffset + kPointerSize;
6425
6426 // Support for StringInputBuffer
6427 inline const unibrow::byte* SlicedStringReadBlock(ReadBlockBuffer* buffer,
6428 unsigned* offset_ptr,
6429 unsigned chars);
6430 inline void SlicedStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6431 unsigned* offset_ptr,
6432 unsigned chars);
6433 // Minimum length for a sliced string.
6434 static const int kMinLength = 13;
6435
6436 typedef FixedBodyDescriptor<kParentOffset,
6437 kOffsetOffset + kPointerSize, kSize>
6438 BodyDescriptor;
6439
6440#ifdef DEBUG
6441 void SlicedStringVerify();
6442#endif
6443
6444 private:
6445 DISALLOW_IMPLICIT_CONSTRUCTORS(SlicedString);
6446};
6447
6448
Steve Blocka7e24c12009-10-30 11:49:00 +00006449// The ExternalString class describes string values that are backed by
6450// a string resource that lies outside the V8 heap. ExternalStrings
6451// consist of the length field common to all strings, a pointer to the
6452// external resource. It is important to ensure (externally) that the
6453// resource is not deallocated while the ExternalString is live in the
6454// V8 heap.
6455//
6456// The API expects that all ExternalStrings are created through the
6457// API. Therefore, ExternalStrings should not be used internally.
6458class ExternalString: public String {
6459 public:
6460 // Casting
6461 static inline ExternalString* cast(Object* obj);
6462
6463 // Layout description.
6464 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
6465 static const int kSize = kResourceOffset + kPointerSize;
6466
6467 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
6468
6469 private:
6470 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
6471};
6472
6473
6474// The ExternalAsciiString class is an external string backed by an
6475// ASCII string.
6476class ExternalAsciiString: public ExternalString {
6477 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006478 static const bool kHasAsciiEncoding = true;
6479
Steve Blocka7e24c12009-10-30 11:49:00 +00006480 typedef v8::String::ExternalAsciiStringResource Resource;
6481
6482 // The underlying resource.
6483 inline Resource* resource();
6484 inline void set_resource(Resource* buffer);
6485
6486 // Dispatched behavior.
6487 uint16_t ExternalAsciiStringGet(int index);
6488
6489 // Casting.
6490 static inline ExternalAsciiString* cast(Object* obj);
6491
Steve Blockd0582a62009-12-15 09:54:21 +00006492 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01006493 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
6494
6495 template<typename StaticVisitor>
6496 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00006497
Steve Blocka7e24c12009-10-30 11:49:00 +00006498 // Support for StringInputBuffer.
6499 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
6500 unsigned* offset,
6501 unsigned chars);
6502 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6503 unsigned* offset,
6504 unsigned chars);
6505
Steve Blocka7e24c12009-10-30 11:49:00 +00006506 private:
6507 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
6508};
6509
6510
6511// The ExternalTwoByteString class is an external string backed by a UTF-16
6512// encoded string.
6513class ExternalTwoByteString: public ExternalString {
6514 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006515 static const bool kHasAsciiEncoding = false;
6516
Steve Blocka7e24c12009-10-30 11:49:00 +00006517 typedef v8::String::ExternalStringResource Resource;
6518
6519 // The underlying string resource.
6520 inline Resource* resource();
6521 inline void set_resource(Resource* buffer);
6522
6523 // Dispatched behavior.
6524 uint16_t ExternalTwoByteStringGet(int index);
6525
6526 // For regexp code.
6527 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
6528
6529 // Casting.
6530 static inline ExternalTwoByteString* cast(Object* obj);
6531
Steve Blockd0582a62009-12-15 09:54:21 +00006532 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01006533 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
6534
6535 template<typename StaticVisitor>
6536 inline void ExternalTwoByteStringIterateBody();
6537
Steve Blockd0582a62009-12-15 09:54:21 +00006538
Steve Blocka7e24c12009-10-30 11:49:00 +00006539 // Support for StringInputBuffer.
6540 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6541 unsigned* offset_ptr,
6542 unsigned chars);
6543
Steve Blocka7e24c12009-10-30 11:49:00 +00006544 private:
6545 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
6546};
6547
6548
6549// Utility superclass for stack-allocated objects that must be updated
6550// on gc. It provides two ways for the gc to update instances, either
6551// iterating or updating after gc.
6552class Relocatable BASE_EMBEDDED {
6553 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006554 explicit inline Relocatable(Isolate* isolate);
6555 inline virtual ~Relocatable();
Steve Blocka7e24c12009-10-30 11:49:00 +00006556 virtual void IterateInstance(ObjectVisitor* v) { }
6557 virtual void PostGarbageCollection() { }
6558
6559 static void PostGarbageCollectionProcessing();
6560 static int ArchiveSpacePerThread();
Ben Murdoch257744e2011-11-30 15:57:28 +00006561 static char* ArchiveState(Isolate* isolate, char* to);
6562 static char* RestoreState(Isolate* isolate, char* from);
Steve Blocka7e24c12009-10-30 11:49:00 +00006563 static void Iterate(ObjectVisitor* v);
6564 static void Iterate(ObjectVisitor* v, Relocatable* top);
6565 static char* Iterate(ObjectVisitor* v, char* t);
6566 private:
Steve Block44f0eee2011-05-26 01:26:41 +01006567 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006568 Relocatable* prev_;
6569};
6570
6571
6572// A flat string reader provides random access to the contents of a
6573// string independent of the character width of the string. The handle
6574// must be valid as long as the reader is being used.
6575class FlatStringReader : public Relocatable {
6576 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006577 FlatStringReader(Isolate* isolate, Handle<String> str);
6578 FlatStringReader(Isolate* isolate, Vector<const char> input);
Steve Blocka7e24c12009-10-30 11:49:00 +00006579 void PostGarbageCollection();
6580 inline uc32 Get(int index);
6581 int length() { return length_; }
6582 private:
6583 String** str_;
6584 bool is_ascii_;
6585 int length_;
6586 const void* start_;
6587};
6588
6589
6590// Note that StringInputBuffers are not valid across a GC! To fix this
6591// it would have to store a String Handle instead of a String* and
6592// AsciiStringReadBlock would have to be modified to use memcpy.
6593//
6594// StringInputBuffer is able to traverse any string regardless of how
6595// deeply nested a sequence of ConsStrings it is made of. However,
6596// performance will be better if deep strings are flattened before they
6597// are traversed. Since flattening requires memory allocation this is
6598// not always desirable, however (esp. in debugging situations).
6599class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
6600 public:
6601 virtual void Seek(unsigned pos);
6602 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006603 explicit inline StringInputBuffer(String* backing):
Steve Blocka7e24c12009-10-30 11:49:00 +00006604 unibrow::InputBuffer<String, String*, 1024>(backing) {}
6605};
6606
6607
6608class SafeStringInputBuffer
6609 : public unibrow::InputBuffer<String, String**, 256> {
6610 public:
6611 virtual void Seek(unsigned pos);
6612 inline SafeStringInputBuffer()
6613 : unibrow::InputBuffer<String, String**, 256>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006614 explicit inline SafeStringInputBuffer(String** backing)
Steve Blocka7e24c12009-10-30 11:49:00 +00006615 : unibrow::InputBuffer<String, String**, 256>(backing) {}
6616};
6617
6618
6619template <typename T>
6620class VectorIterator {
6621 public:
6622 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
6623 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
6624 T GetNext() { return data_[index_++]; }
6625 bool has_more() { return index_ < data_.length(); }
6626 private:
6627 Vector<const T> data_;
6628 int index_;
6629};
6630
6631
6632// The Oddball describes objects null, undefined, true, and false.
6633class Oddball: public HeapObject {
6634 public:
6635 // [to_string]: Cached to_string computed at startup.
6636 DECL_ACCESSORS(to_string, String)
6637
6638 // [to_number]: Cached to_number computed at startup.
6639 DECL_ACCESSORS(to_number, Object)
6640
Steve Block44f0eee2011-05-26 01:26:41 +01006641 inline byte kind();
6642 inline void set_kind(byte kind);
6643
Steve Blocka7e24c12009-10-30 11:49:00 +00006644 // Casting.
6645 static inline Oddball* cast(Object* obj);
6646
6647 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00006648#ifdef DEBUG
6649 void OddballVerify();
6650#endif
6651
6652 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07006653 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
Steve Block44f0eee2011-05-26 01:26:41 +01006654 Object* to_number,
6655 byte kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00006656
6657 // Layout description.
6658 static const int kToStringOffset = HeapObject::kHeaderSize;
6659 static const int kToNumberOffset = kToStringOffset + kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +01006660 static const int kKindOffset = kToNumberOffset + kPointerSize;
6661 static const int kSize = kKindOffset + kPointerSize;
6662
6663 static const byte kFalse = 0;
6664 static const byte kTrue = 1;
6665 static const byte kNotBooleanMask = ~1;
6666 static const byte kTheHole = 2;
6667 static const byte kNull = 3;
6668 static const byte kArgumentMarker = 4;
6669 static const byte kUndefined = 5;
6670 static const byte kOther = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +00006671
Iain Merrick75681382010-08-19 15:07:18 +01006672 typedef FixedBodyDescriptor<kToStringOffset,
6673 kToNumberOffset + kPointerSize,
6674 kSize> BodyDescriptor;
6675
Steve Blocka7e24c12009-10-30 11:49:00 +00006676 private:
6677 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6678};
6679
6680
6681class JSGlobalPropertyCell: public HeapObject {
6682 public:
6683 // [value]: value of the global property.
6684 DECL_ACCESSORS(value, Object)
6685
6686 // Casting.
6687 static inline JSGlobalPropertyCell* cast(Object* obj);
6688
Steve Blocka7e24c12009-10-30 11:49:00 +00006689#ifdef DEBUG
6690 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006691#endif
6692#ifdef OBJECT_PRINT
6693 inline void JSGlobalPropertyCellPrint() {
6694 JSGlobalPropertyCellPrint(stdout);
6695 }
6696 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00006697#endif
6698
6699 // Layout description.
6700 static const int kValueOffset = HeapObject::kHeaderSize;
6701 static const int kSize = kValueOffset + kPointerSize;
6702
Iain Merrick75681382010-08-19 15:07:18 +01006703 typedef FixedBodyDescriptor<kValueOffset,
6704 kValueOffset + kPointerSize,
6705 kSize> BodyDescriptor;
6706
Ben Murdoch8b112d22011-06-08 16:22:53 +01006707 // Returns the isolate/heap this cell object belongs to.
6708 inline Isolate* isolate();
6709 inline Heap* heap();
6710
Steve Blocka7e24c12009-10-30 11:49:00 +00006711 private:
6712 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6713};
6714
6715
Ben Murdoch257744e2011-11-30 15:57:28 +00006716// The JSProxy describes EcmaScript Harmony proxies
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006717class JSProxy: public JSReceiver {
Steve Blocka7e24c12009-10-30 11:49:00 +00006718 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006719 // [handler]: The handler property.
6720 DECL_ACCESSORS(handler, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00006721
6722 // Casting.
Ben Murdoch257744e2011-11-30 15:57:28 +00006723 static inline JSProxy* cast(Object* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00006724
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006725 bool HasPropertyWithHandler(String* name);
6726
6727 MUST_USE_RESULT MaybeObject* SetPropertyWithHandler(
6728 String* name,
6729 Object* value,
6730 PropertyAttributes attributes,
6731 StrictModeFlag strict_mode);
6732
6733 MUST_USE_RESULT MaybeObject* DeletePropertyWithHandler(
6734 String* name,
6735 DeleteMode mode);
6736
6737 MUST_USE_RESULT PropertyAttributes GetPropertyAttributeWithHandler(
6738 JSReceiver* receiver,
6739 String* name,
6740 bool* has_exception);
6741
6742 // Turn this into an (empty) JSObject.
6743 void Fix();
6744
Ben Murdoch589d6972011-11-30 16:04:58 +00006745 // Initializes the body after the handler slot.
6746 inline void InitializeBody(int object_size, Object* value);
6747
Steve Blocka7e24c12009-10-30 11:49:00 +00006748 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006749#ifdef OBJECT_PRINT
Ben Murdoch257744e2011-11-30 15:57:28 +00006750 inline void JSProxyPrint() {
6751 JSProxyPrint(stdout);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006752 }
Ben Murdoch257744e2011-11-30 15:57:28 +00006753 void JSProxyPrint(FILE* out);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006754#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006755#ifdef DEBUG
Ben Murdoch257744e2011-11-30 15:57:28 +00006756 void JSProxyVerify();
6757#endif
6758
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006759 // Layout description. We add padding so that a proxy has the same
6760 // size as a virgin JSObject. This is essential for becoming a JSObject
6761 // upon freeze.
Ben Murdoch257744e2011-11-30 15:57:28 +00006762 static const int kHandlerOffset = HeapObject::kHeaderSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006763 static const int kPaddingOffset = kHandlerOffset + kPointerSize;
Ben Murdoch589d6972011-11-30 16:04:58 +00006764 static const int kSize = JSObject::kHeaderSize;
6765 static const int kHeaderSize = kPaddingOffset;
6766 static const int kPaddingSize = kSize - kPaddingOffset;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006767
Ben Murdoch589d6972011-11-30 16:04:58 +00006768 STATIC_CHECK(kPaddingSize >= 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00006769
6770 typedef FixedBodyDescriptor<kHandlerOffset,
6771 kHandlerOffset + kPointerSize,
6772 kSize> BodyDescriptor;
6773
6774 private:
6775 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxy);
6776};
6777
6778
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006779class JSFunctionProxy: public JSProxy {
6780 public:
Ben Murdoch589d6972011-11-30 16:04:58 +00006781 // [call_trap]: The call trap.
6782 DECL_ACCESSORS(call_trap, Object)
6783
6784 // [construct_trap]: The construct trap.
6785 DECL_ACCESSORS(construct_trap, Object)
6786
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006787 // Casting.
6788 static inline JSFunctionProxy* cast(Object* obj);
6789
Ben Murdoch589d6972011-11-30 16:04:58 +00006790 // Dispatched behavior.
6791#ifdef OBJECT_PRINT
6792 inline void JSFunctionProxyPrint() {
6793 JSFunctionProxyPrint(stdout);
6794 }
6795 void JSFunctionProxyPrint(FILE* out);
6796#endif
6797#ifdef DEBUG
6798 void JSFunctionProxyVerify();
6799#endif
6800
6801 // Layout description.
6802 static const int kCallTrapOffset = kHandlerOffset + kPointerSize;
6803 static const int kConstructTrapOffset = kCallTrapOffset + kPointerSize;
6804 static const int kPaddingOffset = kConstructTrapOffset + kPointerSize;
6805 static const int kSize = JSFunction::kSize;
6806 static const int kPaddingSize = kSize - kPaddingOffset;
6807
6808 STATIC_CHECK(kPaddingSize >= 0);
6809
6810 typedef FixedBodyDescriptor<kHandlerOffset,
6811 kConstructTrapOffset + kPointerSize,
6812 kSize> BodyDescriptor;
6813
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006814 private:
6815 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunctionProxy);
6816};
6817
Ben Murdoch257744e2011-11-30 15:57:28 +00006818
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006819// The JSWeakMap describes EcmaScript Harmony weak maps
6820class JSWeakMap: public JSObject {
6821 public:
6822 // [table]: the backing hash table mapping keys to values.
6823 DECL_ACCESSORS(table, ObjectHashTable)
6824
6825 // [next]: linked list of encountered weak maps during GC.
6826 DECL_ACCESSORS(next, Object)
6827
6828 // Unchecked accessors to be used during GC.
6829 inline ObjectHashTable* unchecked_table();
6830
6831 // Casting.
6832 static inline JSWeakMap* cast(Object* obj);
6833
6834#ifdef OBJECT_PRINT
6835 inline void JSWeakMapPrint() {
6836 JSWeakMapPrint(stdout);
6837 }
6838 void JSWeakMapPrint(FILE* out);
6839#endif
6840#ifdef DEBUG
6841 void JSWeakMapVerify();
6842#endif
6843
6844 static const int kTableOffset = JSObject::kHeaderSize;
6845 static const int kNextOffset = kTableOffset + kPointerSize;
6846 static const int kSize = kNextOffset + kPointerSize;
6847
6848 private:
6849 DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakMap);
6850};
6851
6852
Ben Murdoch257744e2011-11-30 15:57:28 +00006853// Foreign describes objects pointing from JavaScript to C structures.
6854// Since they cannot contain references to JS HeapObjects they can be
6855// placed in old_data_space.
6856class Foreign: public HeapObject {
6857 public:
6858 // [address]: field containing the address.
6859 inline Address address();
6860 inline void set_address(Address value);
6861
6862 // Casting.
6863 static inline Foreign* cast(Object* obj);
6864
6865 // Dispatched behavior.
6866 inline void ForeignIterateBody(ObjectVisitor* v);
6867
6868 template<typename StaticVisitor>
6869 inline void ForeignIterateBody();
6870
6871#ifdef OBJECT_PRINT
6872 inline void ForeignPrint() {
6873 ForeignPrint(stdout);
6874 }
6875 void ForeignPrint(FILE* out);
6876#endif
6877#ifdef DEBUG
6878 void ForeignVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00006879#endif
6880
6881 // Layout description.
6882
Ben Murdoch257744e2011-11-30 15:57:28 +00006883 static const int kAddressOffset = HeapObject::kHeaderSize;
6884 static const int kSize = kAddressOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006885
Ben Murdoch257744e2011-11-30 15:57:28 +00006886 STATIC_CHECK(kAddressOffset == Internals::kForeignAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00006887
6888 private:
Ben Murdoch257744e2011-11-30 15:57:28 +00006889 DISALLOW_IMPLICIT_CONSTRUCTORS(Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +00006890};
6891
6892
6893// The JSArray describes JavaScript Arrays
6894// Such an array can be in one of two modes:
6895// - fast, backing storage is a FixedArray and length <= elements.length();
6896// Please note: push and pop can be used to grow and shrink the array.
6897// - slow, backing storage is a HashTable with numbers as keys.
6898class JSArray: public JSObject {
6899 public:
6900 // [length]: The length property.
6901 DECL_ACCESSORS(length, Object)
6902
Leon Clarke4515c472010-02-03 11:58:03 +00006903 // Overload the length setter to skip write barrier when the length
6904 // is set to a smi. This matches the set function on FixedArray.
6905 inline void set_length(Smi* length);
6906
John Reck59135872010-11-02 12:39:01 -07006907 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6908 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006909
6910 // Initialize the array with the given capacity. The function may
6911 // fail due to out-of-memory situations, but only if the requested
6912 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07006913 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00006914
6915 // Set the content of the array to the content of storage.
6916 inline void SetContent(FixedArray* storage);
6917
6918 // Casting.
6919 static inline JSArray* cast(Object* obj);
6920
6921 // Uses handles. Ensures that the fixed array backing the JSArray has at
6922 // least the stated size.
6923 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
6924
6925 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006926#ifdef OBJECT_PRINT
6927 inline void JSArrayPrint() {
6928 JSArrayPrint(stdout);
6929 }
6930 void JSArrayPrint(FILE* out);
6931#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006932#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006933 void JSArrayVerify();
6934#endif
6935
6936 // Number of element slots to pre-allocate for an empty array.
6937 static const int kPreallocatedArrayElements = 4;
6938
6939 // Layout description.
6940 static const int kLengthOffset = JSObject::kHeaderSize;
6941 static const int kSize = kLengthOffset + kPointerSize;
6942
6943 private:
6944 // Expand the fixed array backing of a fast-case JSArray to at least
6945 // the requested size.
6946 void Expand(int minimum_size_of_backing_fixed_array);
6947
6948 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6949};
6950
6951
Steve Block6ded16b2010-05-10 14:33:55 +01006952// JSRegExpResult is just a JSArray with a specific initial map.
6953// This initial map adds in-object properties for "index" and "input"
6954// properties, as assigned by RegExp.prototype.exec, which allows
6955// faster creation of RegExp exec results.
6956// This class just holds constants used when creating the result.
6957// After creation the result must be treated as a JSArray in all regards.
6958class JSRegExpResult: public JSArray {
6959 public:
6960 // Offsets of object fields.
6961 static const int kIndexOffset = JSArray::kSize;
6962 static const int kInputOffset = kIndexOffset + kPointerSize;
6963 static const int kSize = kInputOffset + kPointerSize;
6964 // Indices of in-object properties.
6965 static const int kIndexIndex = 0;
6966 static const int kInputIndex = 1;
6967 private:
6968 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6969};
6970
6971
Steve Blocka7e24c12009-10-30 11:49:00 +00006972// An accessor must have a getter, but can have no setter.
6973//
6974// When setting a property, V8 searches accessors in prototypes.
6975// If an accessor was found and it does not have a setter,
6976// the request is ignored.
6977//
6978// If the accessor in the prototype has the READ_ONLY property attribute, then
6979// a new value is added to the local object when the property is set.
6980// This shadows the accessor in the prototype.
6981class AccessorInfo: public Struct {
6982 public:
6983 DECL_ACCESSORS(getter, Object)
6984 DECL_ACCESSORS(setter, Object)
6985 DECL_ACCESSORS(data, Object)
6986 DECL_ACCESSORS(name, Object)
6987 DECL_ACCESSORS(flag, Smi)
6988
6989 inline bool all_can_read();
6990 inline void set_all_can_read(bool value);
6991
6992 inline bool all_can_write();
6993 inline void set_all_can_write(bool value);
6994
6995 inline bool prohibits_overwriting();
6996 inline void set_prohibits_overwriting(bool value);
6997
6998 inline PropertyAttributes property_attributes();
6999 inline void set_property_attributes(PropertyAttributes attributes);
7000
7001 static inline AccessorInfo* cast(Object* obj);
7002
Ben Murdochb0fe1622011-05-05 13:52:32 +01007003#ifdef OBJECT_PRINT
7004 inline void AccessorInfoPrint() {
7005 AccessorInfoPrint(stdout);
7006 }
7007 void AccessorInfoPrint(FILE* out);
7008#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007009#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007010 void AccessorInfoVerify();
7011#endif
7012
7013 static const int kGetterOffset = HeapObject::kHeaderSize;
7014 static const int kSetterOffset = kGetterOffset + kPointerSize;
7015 static const int kDataOffset = kSetterOffset + kPointerSize;
7016 static const int kNameOffset = kDataOffset + kPointerSize;
7017 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007018 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007019
7020 private:
7021 // Bit positions in flag.
7022 static const int kAllCanReadBit = 0;
7023 static const int kAllCanWriteBit = 1;
7024 static const int kProhibitsOverwritingBit = 2;
7025 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
7026
7027 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
7028};
7029
7030
7031class AccessCheckInfo: public Struct {
7032 public:
7033 DECL_ACCESSORS(named_callback, Object)
7034 DECL_ACCESSORS(indexed_callback, Object)
7035 DECL_ACCESSORS(data, Object)
7036
7037 static inline AccessCheckInfo* cast(Object* obj);
7038
Ben Murdochb0fe1622011-05-05 13:52:32 +01007039#ifdef OBJECT_PRINT
7040 inline void AccessCheckInfoPrint() {
7041 AccessCheckInfoPrint(stdout);
7042 }
7043 void AccessCheckInfoPrint(FILE* out);
7044#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007045#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007046 void AccessCheckInfoVerify();
7047#endif
7048
7049 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
7050 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
7051 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
7052 static const int kSize = kDataOffset + kPointerSize;
7053
7054 private:
7055 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
7056};
7057
7058
7059class InterceptorInfo: public Struct {
7060 public:
7061 DECL_ACCESSORS(getter, Object)
7062 DECL_ACCESSORS(setter, Object)
7063 DECL_ACCESSORS(query, Object)
7064 DECL_ACCESSORS(deleter, Object)
7065 DECL_ACCESSORS(enumerator, Object)
7066 DECL_ACCESSORS(data, Object)
7067
7068 static inline InterceptorInfo* cast(Object* obj);
7069
Ben Murdochb0fe1622011-05-05 13:52:32 +01007070#ifdef OBJECT_PRINT
7071 inline void InterceptorInfoPrint() {
7072 InterceptorInfoPrint(stdout);
7073 }
7074 void InterceptorInfoPrint(FILE* out);
7075#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007076#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007077 void InterceptorInfoVerify();
7078#endif
7079
7080 static const int kGetterOffset = HeapObject::kHeaderSize;
7081 static const int kSetterOffset = kGetterOffset + kPointerSize;
7082 static const int kQueryOffset = kSetterOffset + kPointerSize;
7083 static const int kDeleterOffset = kQueryOffset + kPointerSize;
7084 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
7085 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
7086 static const int kSize = kDataOffset + kPointerSize;
7087
7088 private:
7089 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
7090};
7091
7092
7093class CallHandlerInfo: public Struct {
7094 public:
7095 DECL_ACCESSORS(callback, Object)
7096 DECL_ACCESSORS(data, Object)
7097
7098 static inline CallHandlerInfo* cast(Object* obj);
7099
Ben Murdochb0fe1622011-05-05 13:52:32 +01007100#ifdef OBJECT_PRINT
7101 inline void CallHandlerInfoPrint() {
7102 CallHandlerInfoPrint(stdout);
7103 }
7104 void CallHandlerInfoPrint(FILE* out);
7105#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007106#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007107 void CallHandlerInfoVerify();
7108#endif
7109
7110 static const int kCallbackOffset = HeapObject::kHeaderSize;
7111 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007112 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007113
7114 private:
7115 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
7116};
7117
7118
7119class TemplateInfo: public Struct {
7120 public:
7121 DECL_ACCESSORS(tag, Object)
7122 DECL_ACCESSORS(property_list, Object)
7123
7124#ifdef DEBUG
7125 void TemplateInfoVerify();
7126#endif
7127
7128 static const int kTagOffset = HeapObject::kHeaderSize;
7129 static const int kPropertyListOffset = kTagOffset + kPointerSize;
7130 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
7131 protected:
7132 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
7133 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
7134};
7135
7136
7137class FunctionTemplateInfo: public TemplateInfo {
7138 public:
7139 DECL_ACCESSORS(serial_number, Object)
7140 DECL_ACCESSORS(call_code, Object)
7141 DECL_ACCESSORS(property_accessors, Object)
7142 DECL_ACCESSORS(prototype_template, Object)
7143 DECL_ACCESSORS(parent_template, Object)
7144 DECL_ACCESSORS(named_property_handler, Object)
7145 DECL_ACCESSORS(indexed_property_handler, Object)
7146 DECL_ACCESSORS(instance_template, Object)
7147 DECL_ACCESSORS(class_name, Object)
7148 DECL_ACCESSORS(signature, Object)
7149 DECL_ACCESSORS(instance_call_handler, Object)
7150 DECL_ACCESSORS(access_check_info, Object)
7151 DECL_ACCESSORS(flag, Smi)
7152
7153 // Following properties use flag bits.
7154 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
7155 DECL_BOOLEAN_ACCESSORS(undetectable)
7156 // If the bit is set, object instances created by this function
7157 // requires access check.
7158 DECL_BOOLEAN_ACCESSORS(needs_access_check)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007159 DECL_BOOLEAN_ACCESSORS(read_only_prototype)
Steve Blocka7e24c12009-10-30 11:49:00 +00007160
7161 static inline FunctionTemplateInfo* cast(Object* obj);
7162
Ben Murdochb0fe1622011-05-05 13:52:32 +01007163#ifdef OBJECT_PRINT
7164 inline void FunctionTemplateInfoPrint() {
7165 FunctionTemplateInfoPrint(stdout);
7166 }
7167 void FunctionTemplateInfoPrint(FILE* out);
7168#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007169#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007170 void FunctionTemplateInfoVerify();
7171#endif
7172
7173 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
7174 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
7175 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
7176 static const int kPrototypeTemplateOffset =
7177 kPropertyAccessorsOffset + kPointerSize;
7178 static const int kParentTemplateOffset =
7179 kPrototypeTemplateOffset + kPointerSize;
7180 static const int kNamedPropertyHandlerOffset =
7181 kParentTemplateOffset + kPointerSize;
7182 static const int kIndexedPropertyHandlerOffset =
7183 kNamedPropertyHandlerOffset + kPointerSize;
7184 static const int kInstanceTemplateOffset =
7185 kIndexedPropertyHandlerOffset + kPointerSize;
7186 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
7187 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
7188 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
7189 static const int kAccessCheckInfoOffset =
7190 kInstanceCallHandlerOffset + kPointerSize;
7191 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007192 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007193
7194 private:
7195 // Bit position in the flag, from least significant bit position.
7196 static const int kHiddenPrototypeBit = 0;
7197 static const int kUndetectableBit = 1;
7198 static const int kNeedsAccessCheckBit = 2;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007199 static const int kReadOnlyPrototypeBit = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00007200
7201 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
7202};
7203
7204
7205class ObjectTemplateInfo: public TemplateInfo {
7206 public:
7207 DECL_ACCESSORS(constructor, Object)
7208 DECL_ACCESSORS(internal_field_count, Object)
7209
7210 static inline ObjectTemplateInfo* cast(Object* obj);
7211
Ben Murdochb0fe1622011-05-05 13:52:32 +01007212#ifdef OBJECT_PRINT
7213 inline void ObjectTemplateInfoPrint() {
7214 ObjectTemplateInfoPrint(stdout);
7215 }
7216 void ObjectTemplateInfoPrint(FILE* out);
7217#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007218#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007219 void ObjectTemplateInfoVerify();
7220#endif
7221
7222 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
7223 static const int kInternalFieldCountOffset =
7224 kConstructorOffset + kPointerSize;
7225 static const int kSize = kInternalFieldCountOffset + kPointerSize;
7226};
7227
7228
7229class SignatureInfo: public Struct {
7230 public:
7231 DECL_ACCESSORS(receiver, Object)
7232 DECL_ACCESSORS(args, Object)
7233
7234 static inline SignatureInfo* cast(Object* obj);
7235
Ben Murdochb0fe1622011-05-05 13:52:32 +01007236#ifdef OBJECT_PRINT
7237 inline void SignatureInfoPrint() {
7238 SignatureInfoPrint(stdout);
7239 }
7240 void SignatureInfoPrint(FILE* out);
7241#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007242#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007243 void SignatureInfoVerify();
7244#endif
7245
7246 static const int kReceiverOffset = Struct::kHeaderSize;
7247 static const int kArgsOffset = kReceiverOffset + kPointerSize;
7248 static const int kSize = kArgsOffset + kPointerSize;
7249
7250 private:
7251 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
7252};
7253
7254
7255class TypeSwitchInfo: public Struct {
7256 public:
7257 DECL_ACCESSORS(types, Object)
7258
7259 static inline TypeSwitchInfo* cast(Object* obj);
7260
Ben Murdochb0fe1622011-05-05 13:52:32 +01007261#ifdef OBJECT_PRINT
7262 inline void TypeSwitchInfoPrint() {
7263 TypeSwitchInfoPrint(stdout);
7264 }
7265 void TypeSwitchInfoPrint(FILE* out);
7266#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007267#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007268 void TypeSwitchInfoVerify();
7269#endif
7270
7271 static const int kTypesOffset = Struct::kHeaderSize;
7272 static const int kSize = kTypesOffset + kPointerSize;
7273};
7274
7275
7276#ifdef ENABLE_DEBUGGER_SUPPORT
7277// The DebugInfo class holds additional information for a function being
7278// debugged.
7279class DebugInfo: public Struct {
7280 public:
7281 // The shared function info for the source being debugged.
7282 DECL_ACCESSORS(shared, SharedFunctionInfo)
7283 // Code object for the original code.
7284 DECL_ACCESSORS(original_code, Code)
7285 // Code object for the patched code. This code object is the code object
7286 // currently active for the function.
7287 DECL_ACCESSORS(code, Code)
7288 // Fixed array holding status information for each active break point.
7289 DECL_ACCESSORS(break_points, FixedArray)
7290
7291 // Check if there is a break point at a code position.
7292 bool HasBreakPoint(int code_position);
7293 // Get the break point info object for a code position.
7294 Object* GetBreakPointInfo(int code_position);
7295 // Clear a break point.
7296 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
7297 int code_position,
7298 Handle<Object> break_point_object);
7299 // Set a break point.
7300 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
7301 int source_position, int statement_position,
7302 Handle<Object> break_point_object);
7303 // Get the break point objects for a code position.
7304 Object* GetBreakPointObjects(int code_position);
7305 // Find the break point info holding this break point object.
7306 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
7307 Handle<Object> break_point_object);
7308 // Get the number of break points for this function.
7309 int GetBreakPointCount();
7310
7311 static inline DebugInfo* cast(Object* obj);
7312
Ben Murdochb0fe1622011-05-05 13:52:32 +01007313#ifdef OBJECT_PRINT
7314 inline void DebugInfoPrint() {
7315 DebugInfoPrint(stdout);
7316 }
7317 void DebugInfoPrint(FILE* out);
7318#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007319#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007320 void DebugInfoVerify();
7321#endif
7322
7323 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
7324 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
7325 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
7326 static const int kActiveBreakPointsCountIndex =
7327 kPatchedCodeIndex + kPointerSize;
7328 static const int kBreakPointsStateIndex =
7329 kActiveBreakPointsCountIndex + kPointerSize;
7330 static const int kSize = kBreakPointsStateIndex + kPointerSize;
7331
7332 private:
7333 static const int kNoBreakPointInfo = -1;
7334
7335 // Lookup the index in the break_points array for a code position.
7336 int GetBreakPointInfoIndex(int code_position);
7337
7338 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
7339};
7340
7341
7342// The BreakPointInfo class holds information for break points set in a
7343// function. The DebugInfo object holds a BreakPointInfo object for each code
7344// position with one or more break points.
7345class BreakPointInfo: public Struct {
7346 public:
7347 // The position in the code for the break point.
7348 DECL_ACCESSORS(code_position, Smi)
7349 // The position in the source for the break position.
7350 DECL_ACCESSORS(source_position, Smi)
7351 // The position in the source for the last statement before this break
7352 // position.
7353 DECL_ACCESSORS(statement_position, Smi)
7354 // List of related JavaScript break points.
7355 DECL_ACCESSORS(break_point_objects, Object)
7356
7357 // Removes a break point.
7358 static void ClearBreakPoint(Handle<BreakPointInfo> info,
7359 Handle<Object> break_point_object);
7360 // Set a break point.
7361 static void SetBreakPoint(Handle<BreakPointInfo> info,
7362 Handle<Object> break_point_object);
7363 // Check if break point info has this break point object.
7364 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
7365 Handle<Object> break_point_object);
7366 // Get the number of break points for this code position.
7367 int GetBreakPointCount();
7368
7369 static inline BreakPointInfo* cast(Object* obj);
7370
Ben Murdochb0fe1622011-05-05 13:52:32 +01007371#ifdef OBJECT_PRINT
7372 inline void BreakPointInfoPrint() {
7373 BreakPointInfoPrint(stdout);
7374 }
7375 void BreakPointInfoPrint(FILE* out);
7376#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007377#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007378 void BreakPointInfoVerify();
7379#endif
7380
7381 static const int kCodePositionIndex = Struct::kHeaderSize;
7382 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
7383 static const int kStatementPositionIndex =
7384 kSourcePositionIndex + kPointerSize;
7385 static const int kBreakPointObjectsIndex =
7386 kStatementPositionIndex + kPointerSize;
7387 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
7388
7389 private:
7390 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
7391};
7392#endif // ENABLE_DEBUGGER_SUPPORT
7393
7394
7395#undef DECL_BOOLEAN_ACCESSORS
7396#undef DECL_ACCESSORS
7397
7398
7399// Abstract base class for visiting, and optionally modifying, the
7400// pointers contained in Objects. Used in GC and serialization/deserialization.
7401class ObjectVisitor BASE_EMBEDDED {
7402 public:
7403 virtual ~ObjectVisitor() {}
7404
7405 // Visits a contiguous arrays of pointers in the half-open range
7406 // [start, end). Any or all of the values may be modified on return.
7407 virtual void VisitPointers(Object** start, Object** end) = 0;
7408
7409 // To allow lazy clearing of inline caches the visitor has
7410 // a rich interface for iterating over Code objects..
7411
7412 // Visits a code target in the instruction stream.
7413 virtual void VisitCodeTarget(RelocInfo* rinfo);
7414
Steve Block791712a2010-08-27 10:21:07 +01007415 // Visits a code entry in a JS function.
7416 virtual void VisitCodeEntry(Address entry_address);
7417
Ben Murdochb0fe1622011-05-05 13:52:32 +01007418 // Visits a global property cell reference in the instruction stream.
7419 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
7420
Steve Blocka7e24c12009-10-30 11:49:00 +00007421 // Visits a runtime entry in the instruction stream.
7422 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
7423
Steve Blockd0582a62009-12-15 09:54:21 +00007424 // Visits the resource of an ASCII or two-byte string.
7425 virtual void VisitExternalAsciiString(
7426 v8::String::ExternalAsciiStringResource** resource) {}
7427 virtual void VisitExternalTwoByteString(
7428 v8::String::ExternalStringResource** resource) {}
7429
Steve Blocka7e24c12009-10-30 11:49:00 +00007430 // Visits a debug call target in the instruction stream.
7431 virtual void VisitDebugTarget(RelocInfo* rinfo);
7432
7433 // Handy shorthand for visiting a single pointer.
7434 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
7435
7436 // Visits a contiguous arrays of external references (references to the C++
7437 // heap) in the half-open range [start, end). Any or all of the values
7438 // may be modified on return.
7439 virtual void VisitExternalReferences(Address* start, Address* end) {}
7440
7441 inline void VisitExternalReference(Address* p) {
7442 VisitExternalReferences(p, p + 1);
7443 }
7444
Steve Block44f0eee2011-05-26 01:26:41 +01007445 // Visits a handle that has an embedder-assigned class ID.
7446 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
7447
Steve Blocka7e24c12009-10-30 11:49:00 +00007448#ifdef DEBUG
7449 // Intended for serialization/deserialization checking: insert, or
7450 // check for the presence of, a tag at this position in the stream.
7451 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00007452#else
7453 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00007454#endif
7455};
7456
7457
Iain Merrick75681382010-08-19 15:07:18 +01007458class StructBodyDescriptor : public
7459 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
7460 public:
7461 static inline int SizeOf(Map* map, HeapObject* object) {
7462 return map->instance_size();
7463 }
7464};
7465
7466
Steve Blocka7e24c12009-10-30 11:49:00 +00007467// BooleanBit is a helper class for setting and getting a bit in an
7468// integer or Smi.
7469class BooleanBit : public AllStatic {
7470 public:
7471 static inline bool get(Smi* smi, int bit_position) {
7472 return get(smi->value(), bit_position);
7473 }
7474
7475 static inline bool get(int value, int bit_position) {
7476 return (value & (1 << bit_position)) != 0;
7477 }
7478
7479 static inline Smi* set(Smi* smi, int bit_position, bool v) {
7480 return Smi::FromInt(set(smi->value(), bit_position, v));
7481 }
7482
7483 static inline int set(int value, int bit_position, bool v) {
7484 if (v) {
7485 value |= (1 << bit_position);
7486 } else {
7487 value &= ~(1 << bit_position);
7488 }
7489 return value;
7490 }
7491};
7492
7493} } // namespace v8::internal
7494
7495#endif // V8_OBJECTS_H_