blob: 1245ed0c127efecc00e7a040e9517812cd67c4ba [file] [log] [blame]
Ben Murdoch85b71792012-04-11 18:30:58 +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
Ben Murdoch85b71792012-04-11 18:30:58 +010077// - SerializedScopeInfo
Ben Murdoch69a99ed2011-11-30 16:03:39 +000078// - 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
Ben Murdoch85b71792012-04-11 18:30:58 +0100123// 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
Steve Blocka7e24c12009-10-30 11:49:00 +0000135namespace v8 {
136namespace internal {
137
Ben Murdoch589d6972011-11-30 16:04:58 +0000138enum ElementsKind {
Ben Murdoch85b71792012-04-11 18:30:58 +0100139 // The "fast" kind for tagged values. Must be first to make it possible
140 // to efficiently check maps if they have fast elements.
Ben Murdoch589d6972011-11-30 16:04:58 +0000141 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,
Ben Murdoch85b71792012-04-11 18:30:58 +0100163 FIRST_ELEMENTS_KIND = FAST_ELEMENTS,
Ben Murdoch589d6972011-11-30 16:04:58 +0000164 LAST_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS
165};
166
Ben Murdoch85b71792012-04-11 18:30:58 +0100167static const int kElementsKindCount =
168 LAST_ELEMENTS_KIND - FIRST_ELEMENTS_KIND + 1;
169
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:
174 PropertyDetails(PropertyAttributes attributes,
175 PropertyType type,
176 int index = 0) {
177 ASSERT(type != ELEMENTS_TRANSITION);
178 ASSERT(TypeField::is_valid(type));
179 ASSERT(AttributesField::is_valid(attributes));
180 ASSERT(StorageField::is_valid(index));
181
182 value_ = TypeField::encode(type)
183 | AttributesField::encode(attributes)
184 | StorageField::encode(index);
185
186 ASSERT(type == this->type());
187 ASSERT(attributes == this->attributes());
188 ASSERT(index == this->index());
189 }
190
191 PropertyDetails(PropertyAttributes attributes,
192 PropertyType type,
193 ElementsKind elements_kind) {
194 ASSERT(type == ELEMENTS_TRANSITION);
195 ASSERT(TypeField::is_valid(type));
196 ASSERT(AttributesField::is_valid(attributes));
197 ASSERT(StorageField::is_valid(static_cast<int>(elements_kind)));
198
199 value_ = TypeField::encode(type)
200 | AttributesField::encode(attributes)
201 | StorageField::encode(static_cast<int>(elements_kind));
202
203 ASSERT(type == this->type());
204 ASSERT(attributes == this->attributes());
205 ASSERT(elements_kind == this->elements_kind());
206 }
207
208 // Conversion for storing details as Object*.
209 explicit inline PropertyDetails(Smi* smi);
210 inline Smi* AsSmi();
211
212 PropertyType type() { return TypeField::decode(value_); }
213
214 bool IsTransition() {
215 PropertyType t = type();
216 ASSERT(t != INTERCEPTOR);
217 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
218 t == ELEMENTS_TRANSITION;
219 }
220
221 bool IsProperty() {
222 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
223 }
224
225 PropertyAttributes attributes() { return AttributesField::decode(value_); }
226
227 int index() { return StorageField::decode(value_); }
228
229 ElementsKind elements_kind() {
230 ASSERT(type() == ELEMENTS_TRANSITION);
231 return static_cast<ElementsKind>(StorageField::decode(value_));
232 }
233
234 inline PropertyDetails AsDeleted();
235
236 static bool IsValidIndex(int index) {
237 return StorageField::is_valid(index);
238 }
239
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.
247 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> {};
251
252 static const int kInitialIndex = 1;
253
254 private:
255 uint32_t value_;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000256};
257
Steve Blocka7e24c12009-10-30 11:49:00 +0000258
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.
Ben Murdoch85b71792012-04-11 18:30:58 +0100280static const int kVariableSizeSentinel = 0;
Steve Block791712a2010-08-27 10:21:07 +0100281
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
Ben Murdoch85b71792012-04-11 18:30:58 +0100294// name. The other representations (eg, CONS, EXTERNAL) are explicitly
Leon Clarkee46be812010-01-19 14:06:41 +0000295// 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, \
Ben Murdoch85b71792012-04-11 18:30:58 +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) \
Ben Murdoch85b71792012-04-11 18:30:58 +0100478 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
Ben Murdoch85b71792012-04-11 18:30:58 +0100536// string actually contains ascii data.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100537const 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 Murdoch85b71792012-04-11 18:30:58 +0100624 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 Murdoch85b71792012-04-11 18:30:58 +0100631 JS_PROXY_TYPE,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000632 JS_WEAK_MAP_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100633
Ben Murdoch85b71792012-04-11 18:30:58 +0100634 JS_REGEXP_TYPE, // LAST_NONCALLABLE_SPEC_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000635
Ben Murdoch85b71792012-04-11 18:30:58 +0100636 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 Murdoch85b71792012-04-11 18:30:58 +0100641 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.
Ben Murdoch85b71792012-04-11 18:30:58 +0100654 FIRST_JS_RECEIVER_TYPE = JS_VALUE_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000655 LAST_JS_RECEIVER_TYPE = LAST_TYPE,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000656 // Boundaries for testing the types for which typeof is "object".
Ben Murdoch85b71792012-04-11 18:30:58 +0100657 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_VALUE_TYPE,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000658 LAST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_REGEXP_TYPE,
Ben Murdoch85b71792012-04-11 18:30:58 +0100659 // 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
Ben Murdoch85b71792012-04-11 18:30:58 +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 Murdoch85b71792012-04-11 18:30:58 +0100713class 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);
Ben Murdoch85b71792012-04-11 18:30:58 +0100751 };
Ben Murdochb0fe1622011-05-05 13:52:32 +0100752 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 Murdoch85b71792012-04-11 18:30:58 +0100805 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);
Ben Murdoch85b71792012-04-11 18:30:58 +0100906 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
907 Object* structure,
908 String* name,
909 Object* holder);
910 MUST_USE_RESULT MaybeObject* GetPropertyWithHandler(Object* receiver,
911 String* name,
912 Object* handler);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000913 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
Ben Murdoch85b71792012-04-11 18:30:58 +0100914 JSFunction* getter);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000915
Ben Murdoch85b71792012-04-11 18:30:58 +0100916 inline MaybeObject* GetElement(uint32_t index);
John Reck59135872010-11-02 12:39:01 -0700917 // For use when we know that no exception can be thrown.
918 inline Object* GetElementNoExceptionThrown(uint32_t index);
Ben Murdoch85b71792012-04-11 18:30:58 +0100919 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
Ben Murdoch85b71792012-04-11 18:30:58 +0100989 static const int kMinValue = (-1 << (kSmiValueSize - 1));
Steve Block3ce2e202009-11-05 08:53:23 +0000990 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,
Ben Murdoch85b71792012-04-11 18:30:58 +01001070// during GC other data (eg, mark bits, forwarding addresses) is sometimes
Steve Blocka7e24c12009-10-30 11:49:00 +00001071// 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,
Ben Murdoch85b71792012-04-11 18:30:58 +01001089 // when all map words are heap object pointers, ie. not during a full GC).
Steve Blocka7e24c12009-10-30 11:49:00 +00001090 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
Ben Murdoch85b71792012-04-11 18:30:58 +01001098 // 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).
Steve Blocka7e24c12009-10-30 11:49:00 +00001102
Ben Murdoch85b71792012-04-11 18:30:58 +01001103 // 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
1160 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1161 // used.
1162 // +-----------------+------------------+-----------------+
1163 // |forwarding offset|page offset of map|page index of map|
1164 // +-----------------+------------------+-----------------+
1165 // ^ ^ ^
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
1179
1180 static const int kMapPageIndexShift = 0;
1181 static const int kMapPageOffsetShift =
1182 kMapPageIndexShift + kMapPageIndexBits;
1183 static const int kForwardingOffsetShift =
1184 kMapPageOffsetShift + kMapPageOffsetBits;
1185
1186 // Bit masks covering the different parts the encoding.
1187 static const uintptr_t kMapPageIndexMask =
1188 (1 << kMapPageOffsetShift) - 1;
1189 static const uintptr_t kMapPageOffsetMask =
1190 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
1191 static const uintptr_t kForwardingOffsetMask =
1192 ~(kMapPageIndexMask | kMapPageOffsetMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00001193
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.
Ben Murdoch85b71792012-04-11 18:30:58 +01001219 // This method can not be used during GC, it ASSERTs this.
Steve Block44f0eee2011-05-26 01:26:41 +01001220 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
Ben Murdoch85b71792012-04-11 18:30:58 +01001249 // 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
Steve Blocka7e24c12009-10-30 11:49:00 +00001274 // 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);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001297#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01001298#ifdef DEBUG
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001299 void HeapObjectVerify();
1300 inline void VerifyObjectField(int offset);
1301 inline void VerifySmiField(int offset);
Ben Murdoch85b71792012-04-11 18:30:58 +01001302#endif
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001303
Ben Murdoch85b71792012-04-11 18:30:58 +01001304#ifdef OBJECT_PRINT
1305 void PrintHeader(FILE* out, const char* id);
1306#endif
1307
1308#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();
Ben Murdoch85b71792012-04-11 18:30:58 +01001543 inline bool AllowsSetElementsLength();
Ben Murdochc7cc0282012-03-05 14:35:55 +00001544 inline SeededNumberDictionary* element_dictionary(); // Gets slow elements.
1545
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001546 // 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);
Ben Murdoch85b71792012-04-11 18:30:58 +01001574 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1575 Object* value);
John Reck59135872010-11-02 12:39:01 -07001576 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,
Ben Murdoch85b71792012-04-11 18:30:58 +01001627 bool is_getter,
1628 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001629 PropertyAttributes attributes);
Ben Murdoch85b71792012-04-11 18:30:58 +01001630 Object* LookupAccessor(String* name, bool is_getter);
Steve Blocka7e24c12009-10-30 11:49:00 +00001631
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().
Ben Murdoch85b71792012-04-11 18:30:58 +01001635 MaybeObject* GetPropertyWithFailedAccessCheck(
John Reck59135872010-11-02 12:39:01 -07001636 Object* receiver,
1637 LookupResult* result,
1638 String* name,
1639 PropertyAttributes* attributes);
Ben Murdoch85b71792012-04-11 18:30:58 +01001640 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);
Ben Murdoch85b71792012-04-11 18:30:58 +01001644 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 Murdoch85b71792012-04-11 18:30:58 +01001648 MaybeObject* GetLocalPropertyPostInterceptor(JSReceiver* receiver,
1649 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.
Ben Murdoch85b71792012-04-11 18:30:58 +01001663 // Instead they are stored on an auxiliary JSObject stored as a local
Steve Blockd0582a62009-12-15 09:54:21 +00001664 // property with a special name Heap::hidden_symbol(). But if the
1665 // receiver is a JSGlobalProxy then the auxiliary object is a property
Ben Murdoch85b71792012-04-11 18:30:58 +01001666 // 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();
1675 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1676 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001677
Ben Murdoch85b71792012-04-11 18:30:58 +01001678 // Indicates whether the hidden properties object should be created.
1679 enum HiddenPropertiesFlag { ALLOW_CREATION, OMIT_CREATION };
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001680
Ben Murdoch85b71792012-04-11 18:30:58 +01001681 // 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);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001687
Ben Murdoch85b71792012-04-11 18:30:58 +01001688 // 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
Ben Murdoch85b71792012-04-11 18:30:58 +01001698 // Tests for the fast common case for property enumeration.
1699 bool IsSimpleEnum();
Steve Blocka7e24c12009-10-30 11:49:00 +00001700
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
Ben Murdoch85b71792012-04-11 18:30:58 +01001710 // represented in a FixedDoubleArray.
1711 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.
Ben Murdoch85b71792012-04-11 18:30:58 +01001714 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);
Ben Murdoch85b71792012-04-11 18:30:58 +01001744 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);
Ben Murdoch85b71792012-04-11 18:30:58 +01001750 MUST_USE_RESULT MaybeObject* SetDictionaryElement(uint32_t index,
1751 Object* value,
1752 StrictModeFlag strict_mode,
1753 bool check_prototype);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001754
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
Ben Murdoch85b71792012-04-11 18:30:58 +01001761 // Set the index'th array element.
Steve Blocka7e24c12009-10-30 11:49:00 +00001762 // A Failure object is returned if GC is needed.
Ben Murdoch85b71792012-04-11 18:30:58 +01001763 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1764 Object* value,
1765 StrictModeFlag strict_mode,
1766 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 Murdoch85b71792012-04-11 18:30:58 +01001770 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001771
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.
Ben Murdoch85b71792012-04-11 18:30:58 +01001775 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);
Ben Murdoch85b71792012-04-11 18:30:58 +01001780 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
Ben Murdoch85b71792012-04-11 18:30:58 +01001792 // Initializes the array to a certain length
1793 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
1794
Steve Blocka7e24c12009-10-30 11:49:00 +00001795 // 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);
Ben Murdoch85b71792012-04-11 18:30:58 +01001803
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).
Ben Murdoch85b71792012-04-11 18:30:58 +01001819 int NumberOfLocalProperties(PropertyAttributes filter);
1820 // Returns the number of enumerable properties (ignoring interceptors).
1821 int NumberOfEnumProperties();
Steve Blocka7e24c12009-10-30 11:49:00 +00001822 // 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
John Reck59135872010-11-02 12:39:01 -07001905 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001906
John Reck59135872010-11-02 12:39:01 -07001907 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001908
Steve Blocka7e24c12009-10-30 11:49:00 +00001909 // Transform slow named properties to fast variants.
1910 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001911 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1912 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001913
1914 // Access fast-case object properties at index.
1915 inline Object* FastPropertyAt(int index);
1916 inline Object* FastPropertyAtPut(int index, Object* value);
1917
1918 // Access to in object properties.
Steve Block44f0eee2011-05-26 01:26:41 +01001919 inline int GetInObjectPropertyOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001920 inline Object* InObjectPropertyAt(int index);
1921 inline Object* InObjectPropertyAtPut(int index,
1922 Object* value,
1923 WriteBarrierMode mode
1924 = UPDATE_WRITE_BARRIER);
1925
Ben Murdoch85b71792012-04-11 18:30:58 +01001926 // initializes the body after properties slot, properties slot is
1927 // initialized by set_properties
1928 // Note: this call does not update write barrier, it is caller's
1929 // reponsibility to ensure that *v* can be collected without WB here.
1930 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001931
1932 // Check whether this object references another object
1933 bool ReferencesObject(Object* obj);
1934
1935 // Casting.
1936 static inline JSObject* cast(Object* obj);
1937
Steve Block8defd9f2010-07-08 12:39:36 +01001938 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001939 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001940
1941
Steve Blocka7e24c12009-10-30 11:49:00 +00001942 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001943 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001944#ifdef OBJECT_PRINT
1945 inline void JSObjectPrint() {
1946 JSObjectPrint(stdout);
1947 }
1948 void JSObjectPrint(FILE* out);
1949#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001950#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001951 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001952#endif
1953#ifdef OBJECT_PRINT
1954 inline void PrintProperties() {
1955 PrintProperties(stdout);
1956 }
1957 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001958
Ben Murdochb0fe1622011-05-05 13:52:32 +01001959 inline void PrintElements() {
1960 PrintElements(stdout);
1961 }
1962 void PrintElements(FILE* out);
1963#endif
1964
1965#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001966 // Structure for collecting spill information about JSObjects.
1967 class SpillInformation {
1968 public:
1969 void Clear();
1970 void Print();
1971 int number_of_objects_;
1972 int number_of_objects_with_fast_properties_;
1973 int number_of_objects_with_fast_elements_;
1974 int number_of_fast_used_fields_;
1975 int number_of_fast_unused_fields_;
1976 int number_of_slow_used_properties_;
1977 int number_of_slow_unused_properties_;
1978 int number_of_fast_used_elements_;
1979 int number_of_fast_unused_elements_;
1980 int number_of_slow_used_elements_;
1981 int number_of_slow_unused_elements_;
1982 };
1983
1984 void IncrementSpillStatistics(SpillInformation* info);
1985#endif
1986 Object* SlowReverseLookup(Object* value);
1987
Steve Block8defd9f2010-07-08 12:39:36 +01001988 // Maximal number of fast properties for the JSObject. Used to
1989 // restrict the number of map transitions to avoid an explosion in
1990 // the number of maps for objects used as dictionaries.
1991 inline int MaxFastProperties();
1992
Leon Clarkee46be812010-01-19 14:06:41 +00001993 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1994 // Also maximal value of JSArray's length property.
1995 static const uint32_t kMaxElementCount = 0xffffffffu;
1996
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001997 // Constants for heuristics controlling conversion of fast elements
1998 // to slow elements.
1999
2000 // Maximal gap that can be introduced by adding an element beyond
2001 // the current elements length.
Steve Blocka7e24c12009-10-30 11:49:00 +00002002 static const uint32_t kMaxGap = 1024;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002003
2004 // Maximal length of fast elements array that won't be checked for
2005 // being dense enough on expansion.
2006 static const int kMaxUncheckedFastElementsLength = 5000;
2007
2008 // Same as above but for old arrays. This limit is more strict. We
2009 // don't want to be wasteful with long lived objects.
2010 static const int kMaxUncheckedOldFastElementsLength = 500;
2011
Steve Blocka7e24c12009-10-30 11:49:00 +00002012 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002013 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00002014 static const int kMaxInstanceSize = 255 * kPointerSize;
2015 // When extending the backing storage for property values, we increase
2016 // its size by more than the 1 entry necessary, so sequentially adding fields
2017 // to the same object requires fewer allocations and copies.
2018 static const int kFieldsAdded = 3;
2019
2020 // Layout description.
2021 static const int kPropertiesOffset = HeapObject::kHeaderSize;
2022 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
2023 static const int kHeaderSize = kElementsOffset + kPointerSize;
2024
2025 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
2026
Iain Merrick75681382010-08-19 15:07:18 +01002027 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
2028 public:
2029 static inline int SizeOf(Map* map, HeapObject* object);
2030 };
2031
Steve Blocka7e24c12009-10-30 11:49:00 +00002032 private:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002033 friend class DictionaryElementsAccessor;
2034
John Reck59135872010-11-02 12:39:01 -07002035 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
2036 Object* structure,
2037 uint32_t index,
2038 Object* holder);
Ben Murdoch85b71792012-04-11 18:30:58 +01002039 MaybeObject* SetElementWithCallback(Object* structure,
2040 uint32_t index,
2041 Object* value,
2042 JSObject* holder,
2043 StrictModeFlag strict_mode);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002044 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
2045 uint32_t index,
2046 Object* value,
2047 StrictModeFlag strict_mode,
Ben Murdoch85b71792012-04-11 18:30:58 +01002048 bool check_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01002049 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
2050 uint32_t index,
2051 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002052 StrictModeFlag strict_mode,
Ben Murdoch85b71792012-04-11 18:30:58 +01002053 bool check_prototype);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002054
John Reck59135872010-11-02 12:39:01 -07002055 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
2056 DeleteMode mode);
2057 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00002058
John Reck59135872010-11-02 12:39:01 -07002059 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002060
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002061 MUST_USE_RESULT MaybeObject* DeleteFastElement(uint32_t index);
2062 MUST_USE_RESULT MaybeObject* DeleteDictionaryElement(uint32_t index,
2063 DeleteMode mode);
2064
2065 bool ReferencesObjectFromElements(FixedArray* elements,
2066 ElementsKind kind,
2067 Object* object);
Ben Murdoch85b71792012-04-11 18:30:58 +01002068 bool HasElementInElements(FixedArray* elements,
2069 ElementsKind kind,
2070 uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002071
2072 // Returns true if most of the elements backing storage is used.
2073 bool HasDenseElements();
2074
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002075 // Gets the current elements capacity and the number of used elements.
2076 void GetElementsCapacityAndUsage(int* capacity, int* used);
2077
Leon Clarkef7060e22010-06-03 12:02:55 +01002078 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07002079 MUST_USE_RESULT MaybeObject* SetElementCallback(
2080 uint32_t index,
2081 Object* structure,
2082 PropertyAttributes attributes);
2083 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
2084 String* name,
2085 Object* structure,
2086 PropertyAttributes attributes);
Ben Murdoch85b71792012-04-11 18:30:58 +01002087 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002088 String* name,
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002089 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00002090
Ben Murdoch85b71792012-04-11 18:30:58 +01002091 void LookupInDescriptor(String* name, LookupResult* result);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002092
Steve Blocka7e24c12009-10-30 11:49:00 +00002093 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
2094};
2095
2096
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002097// Common superclass for FixedArrays that allow implementations to share
2098// common accessors and some code paths.
2099class FixedArrayBase: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002100 public:
2101 // [length]: length of the array.
2102 inline int length();
2103 inline void set_length(int value);
2104
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002105 inline static FixedArrayBase* cast(Object* object);
2106
2107 // Layout description.
2108 // Length is smi tagged when it is stored.
2109 static const int kLengthOffset = HeapObject::kHeaderSize;
2110 static const int kHeaderSize = kLengthOffset + kPointerSize;
2111};
2112
2113
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002114class FixedDoubleArray;
2115
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002116// FixedArray describes fixed-sized arrays with element type Object*.
2117class FixedArray: public FixedArrayBase {
2118 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00002119 // Setter and getter for elements.
2120 inline Object* get(int index);
2121 // Setter that uses write barrier.
2122 inline void set(int index, Object* value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002123 inline bool is_the_hole(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002124
2125 // Setter that doesn't need write barrier).
2126 inline void set(int index, Smi* value);
2127 // Setter with explicit barrier mode.
2128 inline void set(int index, Object* value, WriteBarrierMode mode);
2129
2130 // Setters for frequently used oddballs located in old space.
2131 inline void set_undefined(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01002132 // TODO(isolates): duplicate.
2133 inline void set_undefined(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002134 inline void set_null(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01002135 // TODO(isolates): duplicate.
2136 inline void set_null(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002137 inline void set_the_hole(int index);
2138
Iain Merrick75681382010-08-19 15:07:18 +01002139 // Setters with less debug checks for the GC to use.
2140 inline void set_unchecked(int index, Smi* value);
Steve Block44f0eee2011-05-26 01:26:41 +01002141 inline void set_null_unchecked(Heap* heap, int index);
2142 inline void set_unchecked(Heap* heap, int index, Object* value,
2143 WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01002144
Steve Block6ded16b2010-05-10 14:33:55 +01002145 // Gives access to raw memory which stores the array's data.
2146 inline Object** data_start();
2147
Steve Blocka7e24c12009-10-30 11:49:00 +00002148 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07002149 MUST_USE_RESULT inline MaybeObject* Copy();
2150 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002151
2152 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07002153 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00002154
2155 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07002156 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00002157
2158 // Copy a sub array from the receiver to dest.
2159 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
2160
2161 // Garbage collection support.
2162 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
2163
2164 // Code Generation support.
2165 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2166
2167 // Casting.
2168 static inline FixedArray* cast(Object* obj);
2169
Leon Clarkee46be812010-01-19 14:06:41 +00002170 // Maximal allowed size, in bytes, of a single FixedArray.
2171 // Prevents overflowing size computations, as well as extreme memory
2172 // consumption.
Ben Murdoch692be652012-01-10 18:47:50 +00002173 static const int kMaxSize = 128 * MB * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002174 // Maximally allowed length of a FixedArray.
2175 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002176
2177 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002178#ifdef OBJECT_PRINT
2179 inline void FixedArrayPrint() {
2180 FixedArrayPrint(stdout);
2181 }
2182 void FixedArrayPrint(FILE* out);
2183#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002184#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002185 void FixedArrayVerify();
2186 // Checks if two FixedArrays have identical contents.
2187 bool IsEqualTo(FixedArray* other);
2188#endif
2189
2190 // Swap two elements in a pair of arrays. If this array and the
2191 // numbers array are the same object, the elements are only swapped
2192 // once.
2193 void SwapPairs(FixedArray* numbers, int i, int j);
2194
2195 // Sort prefix of this array and the numbers array as pairs wrt. the
2196 // numbers. If the numbers array and the this array are the same
2197 // object, the prefix of this array is sorted.
2198 void SortPairs(FixedArray* numbers, uint32_t len);
2199
Iain Merrick75681382010-08-19 15:07:18 +01002200 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2201 public:
2202 static inline int SizeOf(Map* map, HeapObject* object) {
2203 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2204 }
2205 };
2206
Steve Blocka7e24c12009-10-30 11:49:00 +00002207 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00002208 // Set operation on FixedArray without using write barriers. Can
2209 // only be used for storing old space objects or smis.
Ben Murdoch85b71792012-04-11 18:30:58 +01002210 static inline void fast_set(FixedArray* array, int index, Object* value);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002211
Steve Blocka7e24c12009-10-30 11:49:00 +00002212 private:
2213 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2214};
2215
2216
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002217// FixedDoubleArray describes fixed-sized arrays with element type double.
2218class FixedDoubleArray: public FixedArrayBase {
2219 public:
Ben Murdoch85b71792012-04-11 18:30:58 +01002220 inline void Initialize(FixedArray* from);
2221 inline void Initialize(FixedDoubleArray* from);
2222 inline void Initialize(SeededNumberDictionary* from);
2223
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002224 // Setter and getter for elements.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002225 inline double get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01002226 inline MaybeObject* get(int index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002227 inline void set(int index, double value);
2228 inline void set_the_hole(int index);
2229
2230 // Checking for the hole.
2231 inline bool is_the_hole(int index);
2232
2233 // Garbage collection support.
2234 inline static int SizeFor(int length) {
2235 return kHeaderSize + length * kDoubleSize;
2236 }
2237
2238 // Code Generation support.
2239 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2240
2241 inline static bool is_the_hole_nan(double value);
2242 inline static double hole_nan_as_double();
2243 inline static double canonical_not_the_hole_nan_as_double();
2244
2245 // Casting.
2246 static inline FixedDoubleArray* cast(Object* obj);
2247
2248 // Maximal allowed size, in bytes, of a single FixedDoubleArray.
2249 // Prevents overflowing size computations, as well as extreme memory
2250 // consumption.
2251 static const int kMaxSize = 512 * MB;
2252 // Maximally allowed length of a FixedArray.
2253 static const int kMaxLength = (kMaxSize - kHeaderSize) / kDoubleSize;
2254
2255 // Dispatched behavior.
2256#ifdef OBJECT_PRINT
2257 inline void FixedDoubleArrayPrint() {
2258 FixedDoubleArrayPrint(stdout);
2259 }
2260 void FixedDoubleArrayPrint(FILE* out);
2261#endif
2262
2263#ifdef DEBUG
2264 void FixedDoubleArrayVerify();
2265#endif
2266
2267 private:
2268 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedDoubleArray);
2269};
2270
2271
Steve Blocka7e24c12009-10-30 11:49:00 +00002272// DescriptorArrays are fixed arrays used to hold instance descriptors.
2273// The format of the these objects is:
Ben Murdoch257744e2011-11-30 15:57:28 +00002274// TODO(1399): It should be possible to make room for bit_field3 in the map
2275// without overloading the instance descriptors field in the map
2276// (and storing it in the DescriptorArray when the map has one).
2277// [0]: storage for bit_field3 for Map owning this object (Smi)
2278// [1]: point to a fixed array with (value, detail) pairs.
2279// [2]: next enumeration index (Smi), or pointer to small fixed array:
Steve Blocka7e24c12009-10-30 11:49:00 +00002280// [0]: next enumeration index (Smi)
2281// [1]: pointer to fixed array with enum cache
Ben Murdoch257744e2011-11-30 15:57:28 +00002282// [3]: first key
Steve Blocka7e24c12009-10-30 11:49:00 +00002283// [length() - 1]: last key
2284//
2285class DescriptorArray: public FixedArray {
2286 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00002287 // Returns true for both shared empty_descriptor_array and for smis, which the
2288 // map uses to encode additional bit fields when the descriptor array is not
2289 // yet used.
Steve Blocka7e24c12009-10-30 11:49:00 +00002290 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00002291
Steve Blocka7e24c12009-10-30 11:49:00 +00002292 // Returns the number of descriptors in the array.
2293 int number_of_descriptors() {
Steve Block44f0eee2011-05-26 01:26:41 +01002294 ASSERT(length() > kFirstIndex || IsEmpty());
2295 int len = length();
2296 return len <= kFirstIndex ? 0 : len - kFirstIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00002297 }
2298
2299 int NextEnumerationIndex() {
2300 if (IsEmpty()) return PropertyDetails::kInitialIndex;
2301 Object* obj = get(kEnumerationIndexIndex);
2302 if (obj->IsSmi()) {
2303 return Smi::cast(obj)->value();
2304 } else {
2305 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2306 return Smi::cast(index)->value();
2307 }
2308 }
2309
2310 // Set next enumeration index and flush any enum cache.
2311 void SetNextEnumerationIndex(int value) {
2312 if (!IsEmpty()) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002313 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
Steve Blocka7e24c12009-10-30 11:49:00 +00002314 }
2315 }
2316 bool HasEnumCache() {
2317 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
2318 }
2319
2320 Object* GetEnumCache() {
2321 ASSERT(HasEnumCache());
2322 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2323 return bridge->get(kEnumCacheBridgeCacheIndex);
2324 }
2325
Ben Murdoch257744e2011-11-30 15:57:28 +00002326 // TODO(1399): It should be possible to make room for bit_field3 in the map
2327 // without overloading the instance descriptors field in the map
2328 // (and storing it in the DescriptorArray when the map has one).
2329 inline int bit_field3_storage();
2330 inline void set_bit_field3_storage(int value);
2331
Steve Blocka7e24c12009-10-30 11:49:00 +00002332 // Initialize or change the enum cache,
2333 // using the supplied storage for the small "bridge".
Ben Murdoch85b71792012-04-11 18:30:58 +01002334 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
Steve Blocka7e24c12009-10-30 11:49:00 +00002335
2336 // Accessors for fetching instance descriptor at descriptor number.
2337 inline String* GetKey(int descriptor_number);
2338 inline Object* GetValue(int descriptor_number);
2339 inline Smi* GetDetails(int descriptor_number);
2340 inline PropertyType GetType(int descriptor_number);
2341 inline int GetFieldIndex(int descriptor_number);
2342 inline JSFunction* GetConstantFunction(int descriptor_number);
2343 inline Object* GetCallbacksObject(int descriptor_number);
2344 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2345 inline bool IsProperty(int descriptor_number);
Ben Murdoch85b71792012-04-11 18:30:58 +01002346 inline bool IsTransition(int descriptor_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00002347 inline bool IsNullDescriptor(int descriptor_number);
2348 inline bool IsDontEnum(int descriptor_number);
2349
2350 // Accessor for complete descriptor.
2351 inline void Get(int descriptor_number, Descriptor* desc);
Ben Murdoch85b71792012-04-11 18:30:58 +01002352 inline void Set(int descriptor_number, Descriptor* desc);
Steve Blocka7e24c12009-10-30 11:49:00 +00002353
Ben Murdoch85b71792012-04-11 18:30:58 +01002354 // Transfer complete descriptor from another descriptor array to
2355 // this one.
2356 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002357
2358 // Copy the descriptor array, insert a new descriptor and optionally
2359 // remove map transitions. If the descriptor is already present, it is
2360 // replaced. If a replaced descriptor is a real property (not a transition
2361 // or null), its enumeration index is kept as is.
2362 // If adding a real property, map transitions must be removed. If adding
2363 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002364 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2365 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002366
Ben Murdoch85b71792012-04-11 18:30:58 +01002367 // Remove all transitions. Return a copy of the array with all transitions
2368 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002369 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002370
2371 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002372 // Does not check for duplicates.
Ben Murdoch85b71792012-04-11 18:30:58 +01002373 void SortUnchecked();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002374
2375 // Sort the instance descriptors by the hash codes of their keys.
2376 // Checks the result for duplicates.
Ben Murdoch85b71792012-04-11 18:30:58 +01002377 void Sort();
Steve Blocka7e24c12009-10-30 11:49:00 +00002378
2379 // Search the instance descriptors for given name.
2380 inline int Search(String* name);
2381
Iain Merrick75681382010-08-19 15:07:18 +01002382 // As the above, but uses DescriptorLookupCache and updates it when
2383 // necessary.
2384 inline int SearchWithCache(String* name);
2385
Steve Blocka7e24c12009-10-30 11:49:00 +00002386 // Tells whether the name is present int the array.
2387 bool Contains(String* name) { return kNotFound != Search(name); }
2388
2389 // Perform a binary search in the instance descriptors represented
2390 // by this fixed array. low and high are descriptor indices. If there
2391 // are three instance descriptors in this array it should be called
2392 // with low=0 and high=2.
2393 int BinarySearch(String* name, int low, int high);
2394
2395 // Perform a linear search in the instance descriptors represented
2396 // by this fixed array. len is the number of descriptor indices that are
2397 // valid. Does not require the descriptors to be sorted.
2398 int LinearSearch(String* name, int len);
2399
2400 // Allocates a DescriptorArray, but returns the singleton
2401 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002402 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002403
2404 // Casting.
2405 static inline DescriptorArray* cast(Object* obj);
2406
2407 // Constant for denoting key was not found.
2408 static const int kNotFound = -1;
2409
Ben Murdoch257744e2011-11-30 15:57:28 +00002410 static const int kBitField3StorageIndex = 0;
2411 static const int kContentArrayIndex = 1;
2412 static const int kEnumerationIndexIndex = 2;
2413 static const int kFirstIndex = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00002414
2415 // The length of the "bridge" to the enum cache.
Ben Murdoch85b71792012-04-11 18:30:58 +01002416 static const int kEnumCacheBridgeLength = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00002417 static const int kEnumCacheBridgeEnumIndex = 0;
2418 static const int kEnumCacheBridgeCacheIndex = 1;
2419
2420 // Layout description.
Ben Murdoch257744e2011-11-30 15:57:28 +00002421 static const int kBitField3StorageOffset = FixedArray::kHeaderSize;
2422 static const int kContentArrayOffset = kBitField3StorageOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002423 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2424 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2425
2426 // Layout description for the bridge array.
2427 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2428 static const int kEnumCacheBridgeCacheOffset =
2429 kEnumCacheBridgeEnumOffset + kPointerSize;
2430
Ben Murdochb0fe1622011-05-05 13:52:32 +01002431#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002432 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002433 inline void PrintDescriptors() {
2434 PrintDescriptors(stdout);
2435 }
2436 void PrintDescriptors(FILE* out);
2437#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002438
Ben Murdochb0fe1622011-05-05 13:52:32 +01002439#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002440 // Is the descriptor array sorted and without duplicates?
2441 bool IsSortedNoDuplicates();
2442
2443 // Are two DescriptorArrays equal?
2444 bool IsEqualTo(DescriptorArray* other);
2445#endif
2446
2447 // The maximum number of descriptors we want in a descriptor array (should
2448 // fit in a page).
2449 static const int kMaxNumberOfDescriptors = 1024 + 512;
2450
2451 private:
2452 // Conversion from descriptor number to array indices.
2453 static int ToKeyIndex(int descriptor_number) {
2454 return descriptor_number+kFirstIndex;
2455 }
Leon Clarkee46be812010-01-19 14:06:41 +00002456
2457 static int ToDetailsIndex(int descriptor_number) {
2458 return (descriptor_number << 1) + 1;
2459 }
2460
Steve Blocka7e24c12009-10-30 11:49:00 +00002461 static int ToValueIndex(int descriptor_number) {
2462 return descriptor_number << 1;
2463 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002464
2465 bool is_null_descriptor(int descriptor_number) {
2466 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2467 NULL_DESCRIPTOR;
2468 }
2469 // Swap operation on FixedArray without using write barriers.
Ben Murdoch85b71792012-04-11 18:30:58 +01002470 static inline void fast_swap(FixedArray* array, int first, int second);
Steve Blocka7e24c12009-10-30 11:49:00 +00002471
2472 // Swap descriptor first and second.
Ben Murdoch85b71792012-04-11 18:30:58 +01002473 inline void Swap(int first, int second);
Steve Blocka7e24c12009-10-30 11:49:00 +00002474
2475 FixedArray* GetContentArray() {
2476 return FixedArray::cast(get(kContentArrayIndex));
2477 }
2478 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2479};
2480
2481
2482// HashTable is a subclass of FixedArray that implements a hash table
2483// that uses open addressing and quadratic probing.
2484//
2485// In order for the quadratic probing to work, elements that have not
2486// yet been used and elements that have been deleted are
2487// distinguished. Probing continues when deleted elements are
2488// encountered and stops when unused elements are encountered.
2489//
2490// - Elements with key == undefined have not been used yet.
Ben Murdoch85b71792012-04-11 18:30:58 +01002491// - Elements with key == null have been deleted.
Steve Blocka7e24c12009-10-30 11:49:00 +00002492//
2493// The hash table class is parameterized with a Shape and a Key.
2494// Shape must be a class with the following interface:
2495// class ExampleShape {
2496// public:
2497// // Tells whether key matches other.
2498// static bool IsMatch(Key key, Object* other);
2499// // Returns the hash value for key.
2500// static uint32_t Hash(Key key);
2501// // Returns the hash value for object.
2502// static uint32_t HashForObject(Key key, Object* object);
2503// // Convert key to an object.
2504// static inline Object* AsObject(Key key);
2505// // The prefix size indicates number of elements in the beginning
2506// // of the backing storage.
2507// static const int kPrefixSize = ..;
2508// // The Element size indicates number of elements per entry.
2509// static const int kEntrySize = ..;
2510// };
Steve Block3ce2e202009-11-05 08:53:23 +00002511// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002512// beginning of the backing storage that can be used for non-element
2513// information by subclasses.
2514
Ben Murdochc7cc0282012-03-05 14:35:55 +00002515template<typename Key>
2516class BaseShape {
2517 public:
2518 static const bool UsesSeed = false;
2519 static uint32_t Hash(Key key) { return 0; }
2520 static uint32_t SeededHash(Key key, uint32_t seed) {
2521 ASSERT(UsesSeed);
2522 return Hash(key);
2523 }
2524 static uint32_t HashForObject(Key key, Object* object) { return 0; }
2525 static uint32_t SeededHashForObject(Key key, uint32_t seed, Object* object) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002526 // Won't be called if UsesSeed isn't overridden by child class.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002527 return HashForObject(key, object);
2528 }
2529};
2530
Steve Blocka7e24c12009-10-30 11:49:00 +00002531template<typename Shape, typename Key>
2532class HashTable: public FixedArray {
2533 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +00002534 // Wrapper methods
2535 inline uint32_t Hash(Key key) {
2536 if (Shape::UsesSeed) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002537 return Shape::SeededHash(key, GetHeap()->HashSeed());
Ben Murdochc7cc0282012-03-05 14:35:55 +00002538 } else {
2539 return Shape::Hash(key);
2540 }
2541 }
2542
2543 inline uint32_t HashForObject(Key key, Object* object) {
2544 if (Shape::UsesSeed) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002545 return Shape::SeededHashForObject(key, GetHeap()->HashSeed(), object);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002546 } else {
2547 return Shape::HashForObject(key, object);
2548 }
2549 }
2550
Steve Block3ce2e202009-11-05 08:53:23 +00002551 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002552 int NumberOfElements() {
2553 return Smi::cast(get(kNumberOfElementsIndex))->value();
2554 }
2555
Leon Clarkee46be812010-01-19 14:06:41 +00002556 // Returns the number of deleted elements in the hash table.
2557 int NumberOfDeletedElements() {
2558 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2559 }
2560
Steve Block3ce2e202009-11-05 08:53:23 +00002561 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002562 int Capacity() {
2563 return Smi::cast(get(kCapacityIndex))->value();
2564 }
2565
2566 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002567 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002568 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2569
2570 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002571 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002572 void ElementRemoved() {
2573 SetNumberOfElements(NumberOfElements() - 1);
2574 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2575 }
2576 void ElementsRemoved(int n) {
2577 SetNumberOfElements(NumberOfElements() - n);
2578 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2579 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002580
Steve Block3ce2e202009-11-05 08:53:23 +00002581 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002582 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002583 int at_least_space_for,
2584 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002585
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002586 // Computes the required capacity for a table holding the given
2587 // number of elements. May be more than HashTable::kMaxCapacity.
2588 static int ComputeCapacity(int at_least_space_for);
2589
Steve Blocka7e24c12009-10-30 11:49:00 +00002590 // Returns the key at entry.
2591 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2592
Ben Murdoch85b71792012-04-11 18:30:58 +01002593 // Tells whether k is a real key. Null and undefined are not allowed
Steve Blocka7e24c12009-10-30 11:49:00 +00002594 // as keys and can be used to indicate missing or deleted elements.
2595 bool IsKey(Object* k) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002596 return !k->IsNull() && !k->IsUndefined();
Steve Blocka7e24c12009-10-30 11:49:00 +00002597 }
2598
2599 // Garbage collection support.
2600 void IteratePrefix(ObjectVisitor* visitor);
2601 void IterateElements(ObjectVisitor* visitor);
2602
2603 // Casting.
2604 static inline HashTable* cast(Object* obj);
2605
2606 // Compute the probe offset (quadratic probing).
2607 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2608 return (n + n * n) >> 1;
2609 }
2610
2611 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002612 static const int kNumberOfDeletedElementsIndex = 1;
2613 static const int kCapacityIndex = 2;
2614 static const int kPrefixStartIndex = 3;
2615 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002616 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002617 static const int kEntrySize = Shape::kEntrySize;
2618 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002619 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002620 static const int kCapacityOffset =
2621 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002622
2623 // Constant used for denoting a absent entry.
2624 static const int kNotFound = -1;
2625
Leon Clarkee46be812010-01-19 14:06:41 +00002626 // Maximal capacity of HashTable. Based on maximal length of underlying
2627 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2628 // cannot overflow.
2629 static const int kMaxCapacity =
2630 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2631
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002632 // Find entry for key otherwise return kNotFound.
Steve Block44f0eee2011-05-26 01:26:41 +01002633 inline int FindEntry(Key key);
2634 int FindEntry(Isolate* isolate, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002635
2636 protected:
Steve Blocka7e24c12009-10-30 11:49:00 +00002637 // Find the entry at which to insert element with the given key that
2638 // has the given hash value.
2639 uint32_t FindInsertionEntry(uint32_t hash);
2640
2641 // Returns the index for an entry (of the key)
2642 static inline int EntryToIndex(int entry) {
2643 return (entry * kEntrySize) + kElementsStartIndex;
2644 }
2645
Steve Block3ce2e202009-11-05 08:53:23 +00002646 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002647 void SetNumberOfElements(int nof) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002648 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
Steve Blocka7e24c12009-10-30 11:49:00 +00002649 }
2650
Leon Clarkee46be812010-01-19 14:06:41 +00002651 // Update the number of deleted elements in the hash table.
2652 void SetNumberOfDeletedElements(int nod) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002653 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
Leon Clarkee46be812010-01-19 14:06:41 +00002654 }
2655
Steve Blocka7e24c12009-10-30 11:49:00 +00002656 // Sets the capacity of the hash table.
2657 void SetCapacity(int capacity) {
2658 // To scale a computed hash code to fit within the hash table, we
2659 // use bit-wise AND with a mask, so the capacity must be positive
2660 // and non-zero.
2661 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002662 ASSERT(capacity <= kMaxCapacity);
Ben Murdoch85b71792012-04-11 18:30:58 +01002663 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
Steve Blocka7e24c12009-10-30 11:49:00 +00002664 }
2665
2666
2667 // Returns probe entry.
2668 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2669 ASSERT(IsPowerOf2(size));
2670 return (hash + GetProbeOffset(number)) & (size - 1);
2671 }
2672
Leon Clarkee46be812010-01-19 14:06:41 +00002673 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2674 return hash & (size - 1);
2675 }
2676
2677 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2678 return (last + number) & (size - 1);
2679 }
2680
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002681 // Rehashes this hash-table into the new table.
2682 MUST_USE_RESULT MaybeObject* Rehash(HashTable* new_table, Key key);
2683
2684 // Attempt to shrink hash table after removal of key.
2685 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2686
Steve Blocka7e24c12009-10-30 11:49:00 +00002687 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002688 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002689};
2690
2691
Steve Blocka7e24c12009-10-30 11:49:00 +00002692// HashTableKey is an abstract superclass for virtual key behavior.
2693class HashTableKey {
2694 public:
2695 // Returns whether the other object matches this key.
2696 virtual bool IsMatch(Object* other) = 0;
2697 // Returns the hash value for this key.
2698 virtual uint32_t Hash() = 0;
2699 // Returns the hash value for object.
2700 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002701 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002702 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002703 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002704 // Required.
2705 virtual ~HashTableKey() {}
2706};
2707
Ben Murdochc7cc0282012-03-05 14:35:55 +00002708
2709class SymbolTableShape : public BaseShape<HashTableKey*> {
Steve Blocka7e24c12009-10-30 11:49:00 +00002710 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002711 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002712 return key->IsMatch(value);
2713 }
Steve Block44f0eee2011-05-26 01:26:41 +01002714 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002715 return key->Hash();
2716 }
Steve Block44f0eee2011-05-26 01:26:41 +01002717 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002718 return key->HashForObject(object);
2719 }
Steve Block44f0eee2011-05-26 01:26:41 +01002720 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002721 return key->AsObject();
2722 }
2723
2724 static const int kPrefixSize = 0;
2725 static const int kEntrySize = 1;
2726};
2727
Ben Murdoch257744e2011-11-30 15:57:28 +00002728class SeqAsciiString;
2729
Steve Blocka7e24c12009-10-30 11:49:00 +00002730// SymbolTable.
2731//
2732// No special elements in the prefix and the element size is 1
2733// because only the symbol itself (the key) needs to be stored.
2734class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2735 public:
2736 // Find symbol in the symbol table. If it is not there yet, it is
2737 // added. The return value is the symbol table which might have
2738 // been enlarged. If the return value is not a failure, the symbol
2739 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002740 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002741 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2742 Object** s);
Ben Murdoch257744e2011-11-30 15:57:28 +00002743 MUST_USE_RESULT MaybeObject* LookupSubStringAsciiSymbol(
2744 Handle<SeqAsciiString> str,
2745 int from,
2746 int length,
2747 Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002748 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2749 Object** s);
John Reck59135872010-11-02 12:39:01 -07002750 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002751
2752 // Looks up a symbol that is equal to the given string and returns
2753 // true if it is found, assigning the symbol to the given output
2754 // parameter.
2755 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002756 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002757
2758 // Casting.
2759 static inline SymbolTable* cast(Object* obj);
2760
2761 private:
John Reck59135872010-11-02 12:39:01 -07002762 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002763
2764 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2765};
2766
2767
Ben Murdochc7cc0282012-03-05 14:35:55 +00002768class MapCacheShape : public BaseShape<HashTableKey*> {
Steve Blocka7e24c12009-10-30 11:49:00 +00002769 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002770 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002771 return key->IsMatch(value);
2772 }
Steve Block44f0eee2011-05-26 01:26:41 +01002773 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002774 return key->Hash();
2775 }
2776
Steve Block44f0eee2011-05-26 01:26:41 +01002777 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002778 return key->HashForObject(object);
2779 }
2780
Steve Block44f0eee2011-05-26 01:26:41 +01002781 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002782 return key->AsObject();
2783 }
2784
2785 static const int kPrefixSize = 0;
2786 static const int kEntrySize = 2;
2787};
2788
2789
2790// MapCache.
2791//
2792// Maps keys that are a fixed array of symbols to a map.
2793// Used for canonicalize maps for object literals.
2794class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2795 public:
2796 // Find cached value for a string key, otherwise return null.
2797 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002798 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002799 static inline MapCache* cast(Object* obj);
2800
2801 private:
2802 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2803};
2804
2805
2806template <typename Shape, typename Key>
2807class Dictionary: public HashTable<Shape, Key> {
2808 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00002809 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2810 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2811 }
2812
2813 // Returns the value at entry.
2814 Object* ValueAt(int entry) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002815 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002816 }
2817
2818 // Set the value for entry.
Ben Murdoch85b71792012-04-11 18:30:58 +01002819 // Returns false if the put wasn't performed due to property being read only.
2820 // Returns true on successful put.
2821 bool ValueAtPut(int entry, Object* value) {
2822 // Check that this value can actually be written.
2823 PropertyDetails details = DetailsAt(entry);
2824 // If a value has not been initilized we allow writing to it even if
2825 // it is read only (a declared const that has not been initialized).
2826 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2827 return false;
2828 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002829 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
Ben Murdoch85b71792012-04-11 18:30:58 +01002830 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002831 }
2832
2833 // Returns the property details for the property at entry.
2834 PropertyDetails DetailsAt(int entry) {
2835 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2836 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002837 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002838 }
2839
2840 // Set the details for entry.
2841 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002842 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002843 }
2844
2845 // Sorting support
2846 void CopyValuesTo(FixedArray* elements);
2847
2848 // Delete a property from the dictionary.
2849 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2850
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002851 // Attempt to shrink the dictionary after deletion of key.
2852 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2853
Steve Blocka7e24c12009-10-30 11:49:00 +00002854 // Returns the number of elements in the dictionary filtering out properties
2855 // with the specified attributes.
2856 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2857
2858 // Returns the number of enumerable elements in the dictionary.
2859 int NumberOfEnumElements();
2860
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002861 enum SortMode { UNSORTED, SORTED };
Steve Blocka7e24c12009-10-30 11:49:00 +00002862 // Copies keys to preallocated fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002863 void CopyKeysTo(FixedArray* storage,
2864 PropertyAttributes filter,
2865 SortMode sort_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002866 // Fill in details for properties into storage.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002867 void CopyKeysTo(FixedArray* storage, int index, SortMode sort_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002868
2869 // Accessors for next enumeration index.
2870 void SetNextEnumerationIndex(int index) {
Ben Murdoch85b71792012-04-11 18:30:58 +01002871 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002872 }
2873
2874 int NextEnumerationIndex() {
2875 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2876 }
2877
2878 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002879 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002880
2881 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002882 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002883
Ben Murdochb0fe1622011-05-05 13:52:32 +01002884#ifdef OBJECT_PRINT
2885 inline void Print() {
2886 Print(stdout);
2887 }
2888 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002889#endif
2890 // Returns the key (slow).
2891 Object* SlowReverseLookup(Object* value);
2892
2893 // Sets the entry to (key, value) pair.
2894 inline void SetEntry(int entry,
2895 Object* key,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002896 Object* value);
2897 inline void SetEntry(int entry,
2898 Object* key,
Steve Blocka7e24c12009-10-30 11:49:00 +00002899 Object* value,
2900 PropertyDetails details);
2901
John Reck59135872010-11-02 12:39:01 -07002902 MUST_USE_RESULT MaybeObject* Add(Key key,
2903 Object* value,
2904 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002905
2906 protected:
2907 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002908 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002909
2910 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002911 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2912 Object* value,
2913 PropertyDetails details,
2914 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002915
2916 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002917 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002918 static const int kMaxNumberKeyIndex =
2919 HashTable<Shape, Key>::kPrefixStartIndex;
2920 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2921};
2922
2923
Ben Murdochc7cc0282012-03-05 14:35:55 +00002924class StringDictionaryShape : public BaseShape<String*> {
Steve Blocka7e24c12009-10-30 11:49:00 +00002925 public:
2926 static inline bool IsMatch(String* key, Object* other);
2927 static inline uint32_t Hash(String* key);
2928 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002929 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002930 static const int kPrefixSize = 2;
2931 static const int kEntrySize = 3;
2932 static const bool kIsEnumerable = true;
2933};
2934
2935
2936class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2937 public:
2938 static inline StringDictionary* cast(Object* obj) {
2939 ASSERT(obj->IsDictionary());
2940 return reinterpret_cast<StringDictionary*>(obj);
2941 }
2942
2943 // Copies enumerable keys to preallocated fixed array.
2944 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2945
2946 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002947 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2948 JSObject* obj,
2949 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002950
Ben Murdoch85b71792012-04-11 18:30:58 +01002951 // Find entry for key otherwise return kNotFound. Optimzed version of
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002952 // HashTable::FindEntry.
2953 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002954};
2955
2956
Ben Murdochc7cc0282012-03-05 14:35:55 +00002957class NumberDictionaryShape : public BaseShape<uint32_t> {
Steve Blocka7e24c12009-10-30 11:49:00 +00002958 public:
2959 static inline bool IsMatch(uint32_t key, Object* other);
John Reck59135872010-11-02 12:39:01 -07002960 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002961 static const int kEntrySize = 3;
2962 static const bool kIsEnumerable = false;
2963};
2964
2965
Ben Murdochc7cc0282012-03-05 14:35:55 +00002966class SeededNumberDictionaryShape : public NumberDictionaryShape {
Steve Blocka7e24c12009-10-30 11:49:00 +00002967 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +00002968 static const bool UsesSeed = true;
2969 static const int kPrefixSize = 2;
2970
2971 static inline uint32_t SeededHash(uint32_t key, uint32_t seed);
2972 static inline uint32_t SeededHashForObject(uint32_t key,
2973 uint32_t seed,
2974 Object* object);
2975};
2976
2977
2978class UnseededNumberDictionaryShape : public NumberDictionaryShape {
2979 public:
2980 static const int kPrefixSize = 0;
2981
2982 static inline uint32_t Hash(uint32_t key);
2983 static inline uint32_t HashForObject(uint32_t key, Object* object);
2984};
2985
2986
2987class SeededNumberDictionary
2988 : public Dictionary<SeededNumberDictionaryShape, uint32_t> {
2989 public:
2990 static SeededNumberDictionary* cast(Object* obj) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002991 ASSERT(obj->IsDictionary());
Ben Murdochc7cc0282012-03-05 14:35:55 +00002992 return reinterpret_cast<SeededNumberDictionary*>(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00002993 }
2994
2995 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002996 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2997 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2998 Object* value,
2999 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00003000
3001 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07003002 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
3003 Object* value,
3004 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00003005
3006 void UpdateMaxNumberKey(uint32_t key);
3007
3008 // If slow elements are required we will never go back to fast-case
3009 // for the elements kept in this dictionary. We require slow
3010 // elements if an element has been added at an index larger than
3011 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
3012 // when defining a getter or setter with a number key.
3013 inline bool requires_slow_elements();
3014 inline void set_requires_slow_elements();
3015
3016 // Get the value of the max number key that has been added to this
3017 // dictionary. max_number_key can only be called if
3018 // requires_slow_elements returns false.
3019 inline uint32_t max_number_key();
3020
Ben Murdoch85b71792012-04-11 18:30:58 +01003021 // Remove all entries were key is a number and (from <= key && key < to).
3022 void RemoveNumberEntries(uint32_t from, uint32_t to);
3023
Steve Blocka7e24c12009-10-30 11:49:00 +00003024 // Bit masks.
3025 static const int kRequiresSlowElementsMask = 1;
3026 static const int kRequiresSlowElementsTagSize = 1;
3027 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
3028};
3029
3030
Ben Murdochc7cc0282012-03-05 14:35:55 +00003031class UnseededNumberDictionary
3032 : public Dictionary<UnseededNumberDictionaryShape, uint32_t> {
3033 public:
3034 static UnseededNumberDictionary* cast(Object* obj) {
3035 ASSERT(obj->IsDictionary());
3036 return reinterpret_cast<UnseededNumberDictionary*>(obj);
3037 }
3038
3039 // Type specific at put (default NONE attributes is used when adding).
3040 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
3041 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key, Object* value);
3042
3043 // Set an existing entry or add a new one if needed.
Ben Murdochc7cc0282012-03-05 14:35:55 +00003044 MUST_USE_RESULT MaybeObject* Set(uint32_t key, Object* value);
3045};
3046
3047
Ben Murdochc7cc0282012-03-05 14:35:55 +00003048class ObjectHashTableShape : public BaseShape<Object*> {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00003049 public:
Ben Murdoch85b71792012-04-11 18:30:58 +01003050 static inline bool IsMatch(JSObject* key, Object* other);
3051 static inline uint32_t Hash(JSObject* key);
3052 static inline uint32_t HashForObject(JSObject* key, Object* object);
3053 MUST_USE_RESULT static inline MaybeObject* AsObject(JSObject* key);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003054 static const int kPrefixSize = 0;
Ben Murdoch85b71792012-04-11 18:30:58 +01003055 static const int kEntrySize = 2;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003056};
3057
3058
Ben Murdoch85b71792012-04-11 18:30:58 +01003059// ObjectHashTable maps keys that are JavaScript objects to object values by
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003060// using the identity hash of the key for hashing purposes.
Ben Murdoch85b71792012-04-11 18:30:58 +01003061class ObjectHashTable: public HashTable<ObjectHashTableShape, JSObject*> {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003062 public:
3063 static inline ObjectHashTable* cast(Object* obj) {
3064 ASSERT(obj->IsHashTable());
3065 return reinterpret_cast<ObjectHashTable*>(obj);
3066 }
3067
3068 // Looks up the value associated with the given key. The undefined value is
3069 // returned in case the key is not present.
Ben Murdoch85b71792012-04-11 18:30:58 +01003070 Object* Lookup(JSObject* key);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003071
3072 // Adds (or overwrites) the value associated with the given key. Mapping a
3073 // key to the undefined value causes removal of the whole entry.
Ben Murdoch85b71792012-04-11 18:30:58 +01003074 MUST_USE_RESULT MaybeObject* Put(JSObject* key, Object* value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003075
3076 private:
3077 friend class MarkCompactCollector;
3078
Ben Murdoch85b71792012-04-11 18:30:58 +01003079 void AddEntry(int entry, JSObject* key, Object* value);
3080 void RemoveEntry(int entry, Heap* heap);
3081 inline void RemoveEntry(int entry);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003082
3083 // Returns the index to the value of an entry.
3084 static inline int EntryToValueIndex(int entry) {
3085 return EntryToIndex(entry) + 1;
3086 }
3087};
3088
3089
Steve Block6ded16b2010-05-10 14:33:55 +01003090// JSFunctionResultCache caches results of some JSFunction invocation.
3091// It is a fixed array with fixed structure:
3092// [0]: factory function
3093// [1]: finger index
3094// [2]: current cache size
3095// [3]: dummy field.
3096// The rest of array are key/value pairs.
3097class JSFunctionResultCache: public FixedArray {
3098 public:
3099 static const int kFactoryIndex = 0;
3100 static const int kFingerIndex = kFactoryIndex + 1;
3101 static const int kCacheSizeIndex = kFingerIndex + 1;
3102 static const int kDummyIndex = kCacheSizeIndex + 1;
3103 static const int kEntriesIndex = kDummyIndex + 1;
3104
3105 static const int kEntrySize = 2; // key + value
3106
Kristian Monsen25f61362010-05-21 11:50:48 +01003107 static const int kFactoryOffset = kHeaderSize;
3108 static const int kFingerOffset = kFactoryOffset + kPointerSize;
3109 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
3110
Steve Block6ded16b2010-05-10 14:33:55 +01003111 inline void MakeZeroSize();
3112 inline void Clear();
3113
Ben Murdochb8e0da22011-05-16 14:20:40 +01003114 inline int size();
3115 inline void set_size(int size);
3116 inline int finger_index();
3117 inline void set_finger_index(int finger_index);
3118
Steve Block6ded16b2010-05-10 14:33:55 +01003119 // Casting
3120 static inline JSFunctionResultCache* cast(Object* obj);
3121
3122#ifdef DEBUG
3123 void JSFunctionResultCacheVerify();
3124#endif
3125};
3126
3127
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003128// The cache for maps used by normalized (dictionary mode) objects.
3129// Such maps do not have property descriptors, so a typical program
3130// needs very limited number of distinct normalized maps.
3131class NormalizedMapCache: public FixedArray {
3132 public:
3133 static const int kEntries = 64;
3134
John Reck59135872010-11-02 12:39:01 -07003135 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
3136 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003137
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003138 void Clear();
3139
3140 // Casting
3141 static inline NormalizedMapCache* cast(Object* obj);
3142
3143#ifdef DEBUG
3144 void NormalizedMapCacheVerify();
3145#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003146};
3147
3148
Ben Murdoch85b71792012-04-11 18:30:58 +01003149// ByteArray represents fixed sized byte arrays. Used by the outside world,
3150// such as PCRE, and also by the memory allocator and garbage collector to
3151// fill in free blocks in the heap.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003152class ByteArray: public FixedArrayBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00003153 public:
3154 // Setter and getter.
3155 inline byte get(int index);
3156 inline void set(int index, byte value);
3157
3158 // Treat contents as an int array.
3159 inline int get_int(int index);
3160
3161 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003162 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00003163 }
3164 // We use byte arrays for free blocks in the heap. Given a desired size in
3165 // bytes that is a multiple of the word size and big enough to hold a byte
3166 // array, this function returns the number of elements a byte array should
3167 // have.
3168 static int LengthFor(int size_in_bytes) {
3169 ASSERT(IsAligned(size_in_bytes, kPointerSize));
3170 ASSERT(size_in_bytes >= kHeaderSize);
3171 return size_in_bytes - kHeaderSize;
3172 }
3173
3174 // Returns data start address.
3175 inline Address GetDataStartAddress();
3176
3177 // Returns a pointer to the ByteArray object for a given data start address.
3178 static inline ByteArray* FromDataStartAddress(Address address);
3179
3180 // Casting.
3181 static inline ByteArray* cast(Object* obj);
3182
3183 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01003184 inline int ByteArraySize() {
3185 return SizeFor(this->length());
3186 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003187#ifdef OBJECT_PRINT
3188 inline void ByteArrayPrint() {
3189 ByteArrayPrint(stdout);
3190 }
3191 void ByteArrayPrint(FILE* out);
3192#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003193#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003194 void ByteArrayVerify();
3195#endif
3196
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003197 // Layout description.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003198 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003199
Leon Clarkee46be812010-01-19 14:06:41 +00003200 // Maximal memory consumption for a single ByteArray.
3201 static const int kMaxSize = 512 * MB;
3202 // Maximal length of a single ByteArray.
3203 static const int kMaxLength = kMaxSize - kHeaderSize;
3204
Steve Blocka7e24c12009-10-30 11:49:00 +00003205 private:
3206 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
3207};
3208
3209
Steve Block3ce2e202009-11-05 08:53:23 +00003210// An ExternalArray represents a fixed-size array of primitive values
3211// which live outside the JavaScript heap. Its subclasses are used to
3212// implement the CanvasArray types being defined in the WebGL
3213// specification. As of this writing the first public draft is not yet
3214// available, but Khronos members can access the draft at:
3215// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
3216//
3217// The semantics of these arrays differ from CanvasPixelArray.
3218// Out-of-range values passed to the setter are converted via a C
3219// cast, not clamping. Out-of-range indices cause exceptions to be
3220// raised rather than being silently ignored.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003221class ExternalArray: public FixedArrayBase {
Steve Block3ce2e202009-11-05 08:53:23 +00003222 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003223 inline bool is_the_hole(int index) { return false; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003224
Steve Block3ce2e202009-11-05 08:53:23 +00003225 // [external_pointer]: The pointer to the external memory area backing this
3226 // external array.
3227 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
3228
3229 // Casting.
3230 static inline ExternalArray* cast(Object* obj);
3231
3232 // Maximal acceptable length for an external array.
3233 static const int kMaxLength = 0x3fffffff;
3234
3235 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003236 static const int kExternalPointerOffset =
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003237 POINTER_SIZE_ALIGN(FixedArrayBase::kLengthOffset + kPointerSize);
Steve Block3ce2e202009-11-05 08:53:23 +00003238 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003239 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00003240
3241 private:
3242 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
3243};
3244
3245
Steve Block44f0eee2011-05-26 01:26:41 +01003246// A ExternalPixelArray represents a fixed-size byte array with special
3247// semantics used for implementing the CanvasPixelArray object. Please see the
3248// specification at:
3249
3250// http://www.whatwg.org/specs/web-apps/current-work/
3251// multipage/the-canvas-element.html#canvaspixelarray
3252// In particular, write access clamps the value written to 0 or 255 if the
3253// value written is outside this range.
3254class ExternalPixelArray: public ExternalArray {
3255 public:
3256 inline uint8_t* external_pixel_pointer();
3257
3258 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003259 inline uint8_t get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003260 inline MaybeObject* get(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01003261 inline void set(int index, uint8_t value);
3262
3263 // This accessor applies the correct conversion from Smi, HeapNumber and
3264 // undefined and clamps the converted value between 0 and 255.
3265 Object* SetValue(uint32_t index, Object* value);
3266
3267 // Casting.
3268 static inline ExternalPixelArray* cast(Object* obj);
3269
3270#ifdef OBJECT_PRINT
3271 inline void ExternalPixelArrayPrint() {
3272 ExternalPixelArrayPrint(stdout);
3273 }
3274 void ExternalPixelArrayPrint(FILE* out);
3275#endif
3276#ifdef DEBUG
3277 void ExternalPixelArrayVerify();
3278#endif // DEBUG
3279
3280 private:
3281 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
3282};
3283
3284
Steve Block3ce2e202009-11-05 08:53:23 +00003285class ExternalByteArray: public ExternalArray {
3286 public:
3287 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003288 inline int8_t get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003289 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003290 inline void set(int index, int8_t value);
3291
3292 // This accessor applies the correct conversion from Smi, HeapNumber
3293 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003294 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003295
3296 // Casting.
3297 static inline ExternalByteArray* cast(Object* obj);
3298
Ben Murdochb0fe1622011-05-05 13:52:32 +01003299#ifdef OBJECT_PRINT
3300 inline void ExternalByteArrayPrint() {
3301 ExternalByteArrayPrint(stdout);
3302 }
3303 void ExternalByteArrayPrint(FILE* out);
3304#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003305#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003306 void ExternalByteArrayVerify();
3307#endif // DEBUG
3308
3309 private:
3310 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
3311};
3312
3313
3314class ExternalUnsignedByteArray: public ExternalArray {
3315 public:
3316 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003317 inline uint8_t get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003318 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003319 inline void set(int index, uint8_t value);
3320
3321 // This accessor applies the correct conversion from Smi, HeapNumber
3322 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003323 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003324
3325 // Casting.
3326 static inline ExternalUnsignedByteArray* cast(Object* obj);
3327
Ben Murdochb0fe1622011-05-05 13:52:32 +01003328#ifdef OBJECT_PRINT
3329 inline void ExternalUnsignedByteArrayPrint() {
3330 ExternalUnsignedByteArrayPrint(stdout);
3331 }
3332 void ExternalUnsignedByteArrayPrint(FILE* out);
3333#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003334#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003335 void ExternalUnsignedByteArrayVerify();
3336#endif // DEBUG
3337
3338 private:
3339 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
3340};
3341
3342
3343class ExternalShortArray: public ExternalArray {
3344 public:
3345 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003346 inline int16_t get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003347 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003348 inline void set(int index, int16_t value);
3349
3350 // This accessor applies the correct conversion from Smi, HeapNumber
3351 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003352 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003353
3354 // Casting.
3355 static inline ExternalShortArray* cast(Object* obj);
3356
Ben Murdochb0fe1622011-05-05 13:52:32 +01003357#ifdef OBJECT_PRINT
3358 inline void ExternalShortArrayPrint() {
3359 ExternalShortArrayPrint(stdout);
3360 }
3361 void ExternalShortArrayPrint(FILE* out);
3362#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003363#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003364 void ExternalShortArrayVerify();
3365#endif // DEBUG
3366
3367 private:
3368 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
3369};
3370
3371
3372class ExternalUnsignedShortArray: public ExternalArray {
3373 public:
3374 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003375 inline uint16_t get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003376 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003377 inline void set(int index, uint16_t value);
3378
3379 // This accessor applies the correct conversion from Smi, HeapNumber
3380 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003381 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003382
3383 // Casting.
3384 static inline ExternalUnsignedShortArray* cast(Object* obj);
3385
Ben Murdochb0fe1622011-05-05 13:52:32 +01003386#ifdef OBJECT_PRINT
3387 inline void ExternalUnsignedShortArrayPrint() {
3388 ExternalUnsignedShortArrayPrint(stdout);
3389 }
3390 void ExternalUnsignedShortArrayPrint(FILE* out);
3391#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003392#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003393 void ExternalUnsignedShortArrayVerify();
3394#endif // DEBUG
3395
3396 private:
3397 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3398};
3399
3400
3401class ExternalIntArray: public ExternalArray {
3402 public:
3403 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003404 inline int32_t get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003405 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003406 inline void set(int index, int32_t value);
3407
3408 // This accessor applies the correct conversion from Smi, HeapNumber
3409 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003410 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003411
3412 // Casting.
3413 static inline ExternalIntArray* cast(Object* obj);
3414
Ben Murdochb0fe1622011-05-05 13:52:32 +01003415#ifdef OBJECT_PRINT
3416 inline void ExternalIntArrayPrint() {
3417 ExternalIntArrayPrint(stdout);
3418 }
3419 void ExternalIntArrayPrint(FILE* out);
3420#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003421#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003422 void ExternalIntArrayVerify();
3423#endif // DEBUG
3424
3425 private:
3426 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3427};
3428
3429
3430class ExternalUnsignedIntArray: public ExternalArray {
3431 public:
3432 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003433 inline uint32_t get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003434 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003435 inline void set(int index, uint32_t value);
3436
3437 // This accessor applies the correct conversion from Smi, HeapNumber
3438 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003439 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003440
3441 // Casting.
3442 static inline ExternalUnsignedIntArray* cast(Object* obj);
3443
Ben Murdochb0fe1622011-05-05 13:52:32 +01003444#ifdef OBJECT_PRINT
3445 inline void ExternalUnsignedIntArrayPrint() {
3446 ExternalUnsignedIntArrayPrint(stdout);
3447 }
3448 void ExternalUnsignedIntArrayPrint(FILE* out);
3449#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003450#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003451 void ExternalUnsignedIntArrayVerify();
3452#endif // DEBUG
3453
3454 private:
3455 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3456};
3457
3458
3459class ExternalFloatArray: public ExternalArray {
3460 public:
3461 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003462 inline float get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003463 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003464 inline void set(int index, float value);
3465
3466 // This accessor applies the correct conversion from Smi, HeapNumber
3467 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003468 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003469
3470 // Casting.
3471 static inline ExternalFloatArray* cast(Object* obj);
3472
Ben Murdochb0fe1622011-05-05 13:52:32 +01003473#ifdef OBJECT_PRINT
3474 inline void ExternalFloatArrayPrint() {
3475 ExternalFloatArrayPrint(stdout);
3476 }
3477 void ExternalFloatArrayPrint(FILE* out);
3478#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003479#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003480 void ExternalFloatArrayVerify();
3481#endif // DEBUG
3482
3483 private:
3484 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3485};
3486
3487
Ben Murdoch257744e2011-11-30 15:57:28 +00003488class ExternalDoubleArray: public ExternalArray {
3489 public:
3490 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003491 inline double get_scalar(int index);
Ben Murdoch85b71792012-04-11 18:30:58 +01003492 inline MaybeObject* get(int index);
Ben Murdoch257744e2011-11-30 15:57:28 +00003493 inline void set(int index, double value);
3494
3495 // This accessor applies the correct conversion from Smi, HeapNumber
3496 // and undefined.
Ben Murdoch85b71792012-04-11 18:30:58 +01003497 MaybeObject* SetValue(uint32_t index, Object* value);
Ben Murdoch257744e2011-11-30 15:57:28 +00003498
3499 // Casting.
3500 static inline ExternalDoubleArray* cast(Object* obj);
3501
3502#ifdef OBJECT_PRINT
3503 inline void ExternalDoubleArrayPrint() {
3504 ExternalDoubleArrayPrint(stdout);
3505 }
3506 void ExternalDoubleArrayPrint(FILE* out);
3507#endif // OBJECT_PRINT
3508#ifdef DEBUG
3509 void ExternalDoubleArrayVerify();
3510#endif // DEBUG
3511
3512 private:
3513 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalDoubleArray);
3514};
3515
3516
Ben Murdochb0fe1622011-05-05 13:52:32 +01003517// DeoptimizationInputData is a fixed array used to hold the deoptimization
3518// data for code generated by the Hydrogen/Lithium compiler. It also
3519// contains information about functions that were inlined. If N different
3520// functions were inlined then first N elements of the literal array will
3521// contain these functions.
3522//
3523// It can be empty.
3524class DeoptimizationInputData: public FixedArray {
3525 public:
3526 // Layout description. Indices in the array.
3527 static const int kTranslationByteArrayIndex = 0;
3528 static const int kInlinedFunctionCountIndex = 1;
3529 static const int kLiteralArrayIndex = 2;
3530 static const int kOsrAstIdIndex = 3;
3531 static const int kOsrPcOffsetIndex = 4;
3532 static const int kFirstDeoptEntryIndex = 5;
3533
3534 // Offsets of deopt entry elements relative to the start of the entry.
3535 static const int kAstIdOffset = 0;
3536 static const int kTranslationIndexOffset = 1;
3537 static const int kArgumentsStackHeightOffset = 2;
Ben Murdoch2b4ba112012-01-20 14:57:15 +00003538 static const int kPcOffset = 3;
3539 static const int kDeoptEntrySize = 4;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003540
3541 // Simple element accessors.
3542#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3543 type* name() { \
3544 return type::cast(get(k##name##Index)); \
3545 } \
3546 void Set##name(type* value) { \
3547 set(k##name##Index, value); \
3548 }
3549
3550 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3551 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3552 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3553 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3554 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3555
Ben Murdoch85b71792012-04-11 18:30:58 +01003556 // Unchecked accessor to be used during GC.
3557 FixedArray* UncheckedLiteralArray() {
3558 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3559 }
3560
Ben Murdochb0fe1622011-05-05 13:52:32 +01003561#undef DEFINE_ELEMENT_ACCESSORS
3562
3563 // Accessors for elements of the ith deoptimization entry.
3564#define DEFINE_ENTRY_ACCESSORS(name, type) \
3565 type* name(int i) { \
3566 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3567 } \
3568 void Set##name(int i, type* value) { \
3569 set(IndexForEntry(i) + k##name##Offset, value); \
3570 }
3571
3572 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3573 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3574 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
Ben Murdoch2b4ba112012-01-20 14:57:15 +00003575 DEFINE_ENTRY_ACCESSORS(Pc, Smi)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003576
3577#undef DEFINE_ENTRY_ACCESSORS
3578
3579 int DeoptCount() {
3580 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3581 }
3582
3583 // Allocates a DeoptimizationInputData.
3584 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3585 PretenureFlag pretenure);
3586
3587 // Casting.
3588 static inline DeoptimizationInputData* cast(Object* obj);
3589
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003590#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +01003591 void DeoptimizationInputDataPrint(FILE* out);
3592#endif
3593
3594 private:
3595 static int IndexForEntry(int i) {
3596 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3597 }
3598
3599 static int LengthFor(int entry_count) {
3600 return IndexForEntry(entry_count);
3601 }
3602};
3603
3604
3605// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3606// data for code generated by the full compiler.
3607// The format of the these objects is
3608// [i * 2]: Ast ID for ith deoptimization.
3609// [i * 2 + 1]: PC and state of ith deoptimization
3610class DeoptimizationOutputData: public FixedArray {
3611 public:
3612 int DeoptPoints() { return length() / 2; }
3613 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3614 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3615 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3616 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3617
3618 static int LengthOfFixedArray(int deopt_points) {
3619 return deopt_points * 2;
3620 }
3621
3622 // Allocates a DeoptimizationOutputData.
3623 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3624 PretenureFlag pretenure);
3625
3626 // Casting.
3627 static inline DeoptimizationOutputData* cast(Object* obj);
3628
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003629#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003630 void DeoptimizationOutputDataPrint(FILE* out);
3631#endif
3632};
3633
3634
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003635class SafepointEntry;
Ben Murdoch85b71792012-04-11 18:30:58 +01003636
Ben Murdochb8e0da22011-05-16 14:20:40 +01003637
Steve Blocka7e24c12009-10-30 11:49:00 +00003638// Code describes objects with on-the-fly generated machine code.
3639class Code: public HeapObject {
3640 public:
3641 // Opaque data type for encapsulating code flags like kind, inline
3642 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003643 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3644 // enumeration type has correct value range (see Issue 830 for more details).
3645 enum Flags {
3646 FLAGS_MIN_VALUE = kMinInt,
3647 FLAGS_MAX_VALUE = kMaxInt
3648 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003649
3650 enum Kind {
3651 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003652 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003653 STUB,
3654 BUILTIN,
3655 LOAD_IC,
3656 KEYED_LOAD_IC,
3657 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003658 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003659 STORE_IC,
3660 KEYED_STORE_IC,
Ben Murdoch257744e2011-11-30 15:57:28 +00003661 UNARY_OP_IC,
3662 BINARY_OP_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003663 COMPARE_IC,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003664 TO_BOOLEAN_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003665 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003666 // Flags.
3667
3668 // Pseudo-kinds.
3669 REGEXP = BUILTIN,
3670 FIRST_IC_KIND = LOAD_IC,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003671 LAST_IC_KIND = TO_BOOLEAN_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003672 };
3673
3674 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003675 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003676 };
3677
Ben Murdochb8e0da22011-05-16 14:20:40 +01003678 typedef int ExtraICState;
3679
3680 static const ExtraICState kNoExtraICState = 0;
3681
Steve Blocka7e24c12009-10-30 11:49:00 +00003682#ifdef ENABLE_DISASSEMBLER
3683 // Printing
3684 static const char* Kind2String(Kind kind);
3685 static const char* ICState2String(InlineCacheState state);
3686 static const char* PropertyType2String(PropertyType type);
Steve Block1e0659c2011-05-24 12:43:12 +01003687 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003688 inline void Disassemble(const char* name) {
3689 Disassemble(name, stdout);
3690 }
3691 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003692#endif // ENABLE_DISASSEMBLER
3693
3694 // [instruction_size]: Size of the native instructions
3695 inline int instruction_size();
3696 inline void set_instruction_size(int value);
3697
Leon Clarkeac952652010-07-15 11:15:24 +01003698 // [relocation_info]: Code relocation information
3699 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003700 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003701
Ben Murdochb0fe1622011-05-05 13:52:32 +01003702 // [deoptimization_data]: Array containing data for deopt.
3703 DECL_ACCESSORS(deoptimization_data, FixedArray)
3704
Ben Murdoch85b71792012-04-11 18:30:58 +01003705 // [code_flushing_candidate]: Field only used during garbage
3706 // collection to hold code flushing candidates. The contents of this
Ben Murdoch257744e2011-11-30 15:57:28 +00003707 // field does not have to be traced during garbage collection since
3708 // it is only used by the garbage collector itself.
Ben Murdoch85b71792012-04-11 18:30:58 +01003709 DECL_ACCESSORS(next_code_flushing_candidate, Object)
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003710
Ben Murdochb0fe1622011-05-05 13:52:32 +01003711 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003712 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003713 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003714
Steve Blocka7e24c12009-10-30 11:49:00 +00003715 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003716
Steve Blocka7e24c12009-10-30 11:49:00 +00003717 // [flags]: Various code flags.
3718 inline Flags flags();
3719 inline void set_flags(Flags flags);
3720
3721 // [flags]: Access to specific code flags.
3722 inline Kind kind();
3723 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003724 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00003725 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3726 inline int arguments_count(); // Only valid for call IC stubs.
3727
3728 // Testers for IC stub kinds.
3729 inline bool is_inline_cache_stub();
3730 inline bool is_load_stub() { return kind() == LOAD_IC; }
3731 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3732 inline bool is_store_stub() { return kind() == STORE_IC; }
3733 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3734 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003735 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003736 inline bool is_unary_op_stub() { return kind() == UNARY_OP_IC; }
3737 inline bool is_binary_op_stub() { return kind() == BINARY_OP_IC; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003738 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003739 inline bool is_to_boolean_ic_stub() { return kind() == TO_BOOLEAN_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00003740
Steve Block6ded16b2010-05-10 14:33:55 +01003741 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003742 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003743 inline void set_major_key(int value);
3744
3745 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3746 inline bool optimizable();
3747 inline void set_optimizable(bool value);
3748
3749 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3750 // deoptimization support.
3751 inline bool has_deoptimization_support();
3752 inline void set_has_deoptimization_support(bool value);
3753
Ben Murdoch589d6972011-11-30 16:04:58 +00003754 // [has_debug_break_slots]: For FUNCTION kind, tells if it has
3755 // been compiled with debug break slots.
3756 inline bool has_debug_break_slots();
3757 inline void set_has_debug_break_slots(bool value);
3758
Ben Murdochb0fe1622011-05-05 13:52:32 +01003759 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3760 // how long the function has been marked for OSR and therefore which
3761 // level of loop nesting we are willing to do on-stack replacement
3762 // for.
3763 inline void set_allow_osr_at_loop_nesting_level(int level);
3764 inline int allow_osr_at_loop_nesting_level();
3765
3766 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3767 // reserved in the code prologue.
3768 inline unsigned stack_slots();
3769 inline void set_stack_slots(unsigned slots);
3770
3771 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3772 // the instruction stream where the safepoint table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003773 inline unsigned safepoint_table_offset();
3774 inline void set_safepoint_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003775
3776 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3777 // instruction stream where the stack check table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003778 inline unsigned stack_check_table_offset();
3779 inline void set_stack_check_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003780
3781 // [check type]: For kind CALL_IC, tells how to check if the
3782 // receiver is valid for the given call.
3783 inline CheckType check_type();
3784 inline void set_check_type(CheckType value);
3785
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003786 // [type-recording unary op type]: For kind UNARY_OP_IC.
Ben Murdoch257744e2011-11-30 15:57:28 +00003787 inline byte unary_op_type();
3788 inline void set_unary_op_type(byte value);
3789
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003790 // [type-recording binary op type]: For kind BINARY_OP_IC.
Ben Murdoch257744e2011-11-30 15:57:28 +00003791 inline byte binary_op_type();
3792 inline void set_binary_op_type(byte value);
3793 inline byte binary_op_result_type();
3794 inline void set_binary_op_result_type(byte value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003795
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003796 // [compare state]: For kind COMPARE_IC, tells what state the stub is in.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003797 inline byte compare_state();
3798 inline void set_compare_state(byte value);
3799
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003800 // [to_boolean_foo]: For kind TO_BOOLEAN_IC tells what state the stub is in.
3801 inline byte to_boolean_state();
3802 inline void set_to_boolean_state(byte value);
3803
Ben Murdochb8e0da22011-05-16 14:20:40 +01003804 // Get the safepoint entry for the given pc.
3805 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003806
3807 // Mark this code object as not having a stack check table. Assumes kind
3808 // is FUNCTION.
3809 void SetNoStackCheckTable();
3810
3811 // Find the first map in an IC stub.
3812 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003813
3814 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003815 static inline Flags ComputeFlags(
3816 Kind kind,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003817 InlineCacheState ic_state = UNINITIALIZED,
3818 ExtraICState extra_ic_state = kNoExtraICState,
3819 PropertyType type = NORMAL,
3820 int argc = -1,
3821 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003822
3823 static inline Flags ComputeMonomorphicFlags(
3824 Kind kind,
3825 PropertyType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003826 ExtraICState extra_ic_state = kNoExtraICState,
Steve Block8defd9f2010-07-08 12:39:36 +01003827 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003828 int argc = -1);
3829
Steve Blocka7e24c12009-10-30 11:49:00 +00003830 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003831 static inline PropertyType ExtractTypeFromFlags(Flags flags);
Ben Murdoch589d6972011-11-30 16:04:58 +00003832 static inline Kind ExtractKindFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003833 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Ben Murdoch589d6972011-11-30 16:04:58 +00003834 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
3835 static inline int ExtractArgumentsCountFromFlags(Flags flags);
3836
Steve Blocka7e24c12009-10-30 11:49:00 +00003837 static inline Flags RemoveTypeFromFlags(Flags flags);
3838
3839 // Convert a target address into a code object.
3840 static inline Code* GetCodeFromTargetAddress(Address address);
3841
Steve Block791712a2010-08-27 10:21:07 +01003842 // Convert an entry address into an object.
3843 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3844
Steve Blocka7e24c12009-10-30 11:49:00 +00003845 // Returns the address of the first instruction.
3846 inline byte* instruction_start();
3847
Leon Clarkeac952652010-07-15 11:15:24 +01003848 // Returns the address right after the last instruction.
3849 inline byte* instruction_end();
3850
Steve Blocka7e24c12009-10-30 11:49:00 +00003851 // Returns the size of the instructions, padding, and relocation information.
3852 inline int body_size();
3853
3854 // Returns the address of the first relocation info (read backwards!).
3855 inline byte* relocation_start();
3856
3857 // Code entry point.
3858 inline byte* entry();
3859
3860 // Returns true if pc is inside this object's instructions.
3861 inline bool contains(byte* pc);
3862
Steve Blocka7e24c12009-10-30 11:49:00 +00003863 // Relocate the code by delta bytes. Called to signal that this code
3864 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003865 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003866
3867 // Migrate code described by desc.
3868 void CopyFrom(const CodeDesc& desc);
3869
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003870 // Returns the object size for a given body (used for allocation).
3871 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003872 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003873 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003874 }
3875
3876 // Calculate the size of the code object to report for log events. This takes
3877 // the layout of the code object into account.
3878 int ExecutableSize() {
3879 // Check that the assumptions about the layout of the code object holds.
3880 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3881 Code::kHeaderSize);
3882 return instruction_size() + Code::kHeaderSize;
3883 }
3884
3885 // Locating source position.
3886 int SourcePosition(Address pc);
3887 int SourceStatementPosition(Address pc);
3888
3889 // Casting.
3890 static inline Code* cast(Object* obj);
3891
3892 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003893 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003894 inline void CodeIterateBody(ObjectVisitor* v);
3895
3896 template<typename StaticVisitor>
Steve Block44f0eee2011-05-26 01:26:41 +01003897 inline void CodeIterateBody(Heap* heap);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003898#ifdef OBJECT_PRINT
3899 inline void CodePrint() {
3900 CodePrint(stdout);
3901 }
3902 void CodePrint(FILE* out);
3903#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003904#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003905 void CodeVerify();
3906#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003907
Ben Murdoch85b71792012-04-11 18:30:58 +01003908 // Returns the isolate/heap this code object belongs to.
3909 inline Isolate* isolate();
3910 inline Heap* heap();
3911
Ben Murdochb0fe1622011-05-05 13:52:32 +01003912 // Max loop nesting marker used to postpose OSR. We don't take loop
3913 // nesting that is deeper than 5 levels into account.
3914 static const int kMaxLoopNestingMarker = 6;
3915
Steve Blocka7e24c12009-10-30 11:49:00 +00003916 // Layout description.
3917 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003918 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003919 static const int kDeoptimizationDataOffset =
Ben Murdoch85b71792012-04-11 18:30:58 +01003920 kRelocationInfoOffset + kPointerSize;
3921 static const int kNextCodeFlushingCandidateOffset =
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003922 kDeoptimizationDataOffset + kPointerSize;
Ben Murdoch85b71792012-04-11 18:30:58 +01003923 static const int kFlagsOffset =
3924 kNextCodeFlushingCandidateOffset + kPointerSize;
3925
Ben Murdoch257744e2011-11-30 15:57:28 +00003926 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003927 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3928
3929 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3930 kKindSpecificFlagsSize;
3931
Steve Blocka7e24c12009-10-30 11:49:00 +00003932 // Add padding to align the instruction start following right after
3933 // the Code object header.
3934 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003935 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003936
3937 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003938 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3939 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3940 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3941 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3942
Ben Murdoch257744e2011-11-30 15:57:28 +00003943 static const int kUnaryOpTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003944 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003945 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3946 static const int kToBooleanTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdoch589d6972011-11-30 16:04:58 +00003947
3948 static const int kFullCodeFlags = kOptimizableOffset + 1;
3949 class FullCodeFlagsHasDeoptimizationSupportField:
3950 public BitField<bool, 0, 1> {}; // NOLINT
3951 class FullCodeFlagsHasDebugBreakSlotsField: public BitField<bool, 1, 1> {};
Ben Murdochb0fe1622011-05-05 13:52:32 +01003952
3953 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
Ben Murdoch589d6972011-11-30 16:04:58 +00003954
3955 static const int kAllowOSRAtLoopNestingLevelOffset = kFullCodeFlags + 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003956
Steve Block1e0659c2011-05-24 12:43:12 +01003957 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3958 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003959
Ben Murdoch589d6972011-11-30 16:04:58 +00003960 // Flags layout. BitField<type, shift, size>.
3961 class ICStateField: public BitField<InlineCacheState, 0, 3> {};
3962 class TypeField: public BitField<PropertyType, 3, 4> {};
Ben Murdoch85b71792012-04-11 18:30:58 +01003963 class KindField: public BitField<Kind, 7, 4> {};
3964 class CacheHolderField: public BitField<InlineCacheHolderFlag, 11, 1> {};
Ben Murdoch589d6972011-11-30 16:04:58 +00003965 class ExtraICStateField: public BitField<ExtraICState, 12, 2> {};
Steve Blocka7e24c12009-10-30 11:49:00 +00003966
Ben Murdoch589d6972011-11-30 16:04:58 +00003967 // Signed field cannot be encoded using the BitField class.
Ben Murdoch85b71792012-04-11 18:30:58 +01003968 static const int kArgumentsCountShift = 14;
Ben Murdoch589d6972011-11-30 16:04:58 +00003969 static const int kArgumentsCountMask = ~((1 << kArgumentsCountShift) - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00003970
3971 static const int kFlagsNotUsedInLookup =
Ben Murdoch589d6972011-11-30 16:04:58 +00003972 TypeField::kMask | CacheHolderField::kMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003973
3974 private:
3975 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3976};
3977
3978
3979// All heap objects have a Map that describes their structure.
3980// A Map contains information about:
3981// - Size information about the object
3982// - How to iterate over an object (for garbage collection)
3983class Map: public HeapObject {
3984 public:
3985 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003986 // Size in bytes or kVariableSizeSentinel if instances do not have
3987 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003988 inline int instance_size();
3989 inline void set_instance_size(int value);
3990
3991 // Count of properties allocated in the object.
3992 inline int inobject_properties();
3993 inline void set_inobject_properties(int value);
3994
3995 // Count of property fields pre-allocated in the object when first allocated.
3996 inline int pre_allocated_property_fields();
3997 inline void set_pre_allocated_property_fields(int value);
3998
3999 // Instance type.
4000 inline InstanceType instance_type();
4001 inline void set_instance_type(InstanceType value);
4002
4003 // Tells how many unused property fields are available in the
4004 // instance (only used for JSObject in fast mode).
4005 inline int unused_property_fields();
4006 inline void set_unused_property_fields(int value);
4007
4008 // Bit field.
4009 inline byte bit_field();
4010 inline void set_bit_field(byte value);
4011
4012 // Bit field 2.
4013 inline byte bit_field2();
4014 inline void set_bit_field2(byte value);
4015
Ben Murdoch257744e2011-11-30 15:57:28 +00004016 // Bit field 3.
4017 // TODO(1399): It should be possible to make room for bit_field3 in the map
4018 // without overloading the instance descriptors field (and storing it in the
4019 // DescriptorArray when the map has one).
4020 inline int bit_field3();
4021 inline void set_bit_field3(int value);
4022
Steve Blocka7e24c12009-10-30 11:49:00 +00004023 // Tells whether the object in the prototype property will be used
4024 // for instances created from this function. If the prototype
4025 // property is set to a value that is not a JSObject, the prototype
4026 // property will not be used to create instances of the function.
4027 // See ECMA-262, 13.2.2.
4028 inline void set_non_instance_prototype(bool value);
4029 inline bool has_non_instance_prototype();
4030
Steve Block6ded16b2010-05-10 14:33:55 +01004031 // Tells whether function has special prototype property. If not, prototype
4032 // property will not be created when accessed (will return undefined),
4033 // and construction from this function will not be allowed.
4034 inline void set_function_with_prototype(bool value);
4035 inline bool function_with_prototype();
4036
Steve Blocka7e24c12009-10-30 11:49:00 +00004037 // Tells whether the instance with this map should be ignored by the
4038 // __proto__ accessor.
4039 inline void set_is_hidden_prototype() {
4040 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
4041 }
4042
4043 inline bool is_hidden_prototype() {
4044 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
4045 }
4046
4047 // Records and queries whether the instance has a named interceptor.
4048 inline void set_has_named_interceptor() {
4049 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
4050 }
4051
4052 inline bool has_named_interceptor() {
4053 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
4054 }
4055
4056 // Records and queries whether the instance has an indexed interceptor.
4057 inline void set_has_indexed_interceptor() {
4058 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
4059 }
4060
4061 inline bool has_indexed_interceptor() {
4062 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
4063 }
4064
4065 // Tells whether the instance is undetectable.
4066 // An undetectable object is a special class of JSObject: 'typeof' operator
4067 // returns undefined, ToBoolean returns false. Otherwise it behaves like
4068 // a normal JS object. It is useful for implementing undetectable
4069 // document.all in Firefox & Safari.
4070 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
4071 inline void set_is_undetectable() {
4072 set_bit_field(bit_field() | (1 << kIsUndetectable));
4073 }
4074
4075 inline bool is_undetectable() {
4076 return ((1 << kIsUndetectable) & bit_field()) != 0;
4077 }
4078
Steve Blocka7e24c12009-10-30 11:49:00 +00004079 // Tells whether the instance has a call-as-function handler.
4080 inline void set_has_instance_call_handler() {
4081 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
4082 }
4083
4084 inline bool has_instance_call_handler() {
4085 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
4086 }
4087
Steve Block8defd9f2010-07-08 12:39:36 +01004088 inline void set_is_extensible(bool value);
4089 inline bool is_extensible();
4090
Ben Murdoch589d6972011-11-30 16:04:58 +00004091 inline void set_elements_kind(ElementsKind elements_kind) {
4092 ASSERT(elements_kind < kElementsKindCount);
4093 ASSERT(kElementsKindCount <= (1 << kElementsKindBitCount));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004094 set_bit_field2((bit_field2() & ~kElementsKindMask) |
4095 (elements_kind << kElementsKindShift));
4096 ASSERT(this->elements_kind() == elements_kind);
4097 }
4098
Ben Murdoch589d6972011-11-30 16:04:58 +00004099 inline ElementsKind elements_kind() {
4100 return static_cast<ElementsKind>(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004101 (bit_field2() & kElementsKindMask) >> kElementsKindShift);
4102 }
4103
Steve Block8defd9f2010-07-08 12:39:36 +01004104 // Tells whether the instance has fast elements.
Ben Murdoch85b71792012-04-11 18:30:58 +01004105 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
Iain Merrick75681382010-08-19 15:07:18 +01004106 inline bool has_fast_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004107 return elements_kind() == FAST_ELEMENTS;
Leon Clarkee46be812010-01-19 14:06:41 +00004108 }
4109
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004110 inline bool has_fast_double_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004111 return elements_kind() == FAST_DOUBLE_ELEMENTS;
Steve Block1e0659c2011-05-24 12:43:12 +01004112 }
4113
Steve Block44f0eee2011-05-26 01:26:41 +01004114 inline bool has_external_array_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004115 ElementsKind kind(elements_kind());
4116 return kind >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
4117 kind <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004118 }
4119
4120 inline bool has_dictionary_elements() {
Ben Murdoch589d6972011-11-30 16:04:58 +00004121 return elements_kind() == DICTIONARY_ELEMENTS;
Steve Block1e0659c2011-05-24 12:43:12 +01004122 }
4123
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004124 // Tells whether the map is attached to SharedFunctionInfo
4125 // (for inobject slack tracking).
4126 inline void set_attached_to_shared_function_info(bool value);
4127
4128 inline bool attached_to_shared_function_info();
4129
4130 // Tells whether the map is shared between objects that may have different
4131 // behavior. If true, the map should never be modified, instead a clone
4132 // should be created and modified.
4133 inline void set_is_shared(bool value);
4134
4135 inline bool is_shared();
4136
Steve Blocka7e24c12009-10-30 11:49:00 +00004137 // Tells whether the instance needs security checks when accessing its
4138 // properties.
4139 inline void set_is_access_check_needed(bool access_check_needed);
4140 inline bool is_access_check_needed();
4141
4142 // [prototype]: implicit prototype object.
4143 DECL_ACCESSORS(prototype, Object)
4144
4145 // [constructor]: points back to the function responsible for this map.
4146 DECL_ACCESSORS(constructor, Object)
4147
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004148 inline JSFunction* unchecked_constructor();
4149
Ben Murdoch257744e2011-11-30 15:57:28 +00004150 // Should only be called by the code that initializes map to set initial valid
4151 // value of the instance descriptor member.
4152 inline void init_instance_descriptors();
4153
Steve Blocka7e24c12009-10-30 11:49:00 +00004154 // [instance descriptors]: describes the object.
4155 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
4156
Ben Murdoch257744e2011-11-30 15:57:28 +00004157 // Sets the instance descriptor array for the map to be an empty descriptor
4158 // array.
4159 inline void clear_instance_descriptors();
4160
Steve Blocka7e24c12009-10-30 11:49:00 +00004161 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01004162 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004163
Steve Block053d10c2011-06-13 19:13:29 +01004164 // [prototype transitions]: cache of prototype transitions.
4165 // Prototype transition is a transition that happens
4166 // when we change object's prototype to a new one.
4167 // Cache format:
4168 // 0: finger - index of the first free cell in the cache
4169 // 1 + 2 * i: prototype
4170 // 2 + 2 * i: target map
4171 DECL_ACCESSORS(prototype_transitions, FixedArray)
4172 inline FixedArray* unchecked_prototype_transitions();
4173
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004174 static const int kProtoTransitionHeaderSize = 1;
4175 static const int kProtoTransitionNumberOfEntriesOffset = 0;
4176 static const int kProtoTransitionElementsPerEntry = 2;
4177 static const int kProtoTransitionPrototypeOffset = 0;
4178 static const int kProtoTransitionMapOffset = 1;
4179
4180 inline int NumberOfProtoTransitions() {
Ben Murdoch85b71792012-04-11 18:30:58 +01004181 FixedArray* cache = unchecked_prototype_transitions();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004182 if (cache->length() == 0) return 0;
4183 return
4184 Smi::cast(cache->get(kProtoTransitionNumberOfEntriesOffset))->value();
4185 }
4186
4187 inline void SetNumberOfProtoTransitions(int value) {
Ben Murdoch85b71792012-04-11 18:30:58 +01004188 FixedArray* cache = unchecked_prototype_transitions();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004189 ASSERT(cache->length() != 0);
4190 cache->set_unchecked(kProtoTransitionNumberOfEntriesOffset,
4191 Smi::FromInt(value));
4192 }
4193
Ben Murdochb0fe1622011-05-05 13:52:32 +01004194 // Lookup in the map's instance descriptors and fill out the result
4195 // with the given holder if the name is found. The holder may be
4196 // NULL when this function is used from the compiler.
4197 void LookupInDescriptors(JSObject* holder,
4198 String* name,
4199 LookupResult* result);
4200
John Reck59135872010-11-02 12:39:01 -07004201 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004202
John Reck59135872010-11-02 12:39:01 -07004203 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
4204 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00004205
4206 // Returns a copy of the map, with all transitions dropped from the
4207 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07004208 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00004209
Ben Murdoch85b71792012-04-11 18:30:58 +01004210 // Returns this map if it already has elements that are fast, otherwise
4211 // returns a copy of the map, with all transitions dropped from the
4212 // descriptors and the ElementsKind set to FAST_ELEMENTS.
4213 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
4214
4215 // Returns this map if it already has fast elements that are doubles,
4216 // otherwise returns a copy of the map, with all transitions dropped from the
4217 // descriptors and the ElementsKind set to FAST_DOUBLE_ELEMENTS.
4218 MUST_USE_RESULT inline MaybeObject* GetFastDoubleElementsMap();
4219
4220 // Returns this map if already has dictionary elements, otherwise returns a
4221 // copy of the map, with all transitions dropped from the descriptors and the
4222 // ElementsKind set to DICTIONARY_ELEMENTS.
4223 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
4224
4225 // Returns a new map with all transitions dropped from the descriptors and the
4226 // ElementsKind set.
4227 MUST_USE_RESULT MaybeObject* GetElementsTransitionMap(
4228 ElementsKind elements_kind,
4229 bool safe_to_add_transition);
4230
Steve Blocka7e24c12009-10-30 11:49:00 +00004231 // Returns the property index for name (only valid for FAST MODE).
4232 int PropertyIndexFor(String* name);
4233
4234 // Returns the next free property index (only valid for FAST MODE).
4235 int NextFreePropertyIndex();
4236
Ben Murdoch85b71792012-04-11 18:30:58 +01004237 // Returns the number of properties described in instance_descriptors.
4238 int NumberOfDescribedProperties();
Steve Blocka7e24c12009-10-30 11:49:00 +00004239
4240 // Casting.
4241 static inline Map* cast(Object* obj);
4242
4243 // Locate an accessor in the instance descriptor.
4244 AccessorDescriptor* FindAccessor(String* name);
4245
4246 // Code cache operations.
4247
4248 // Clears the code cache.
Steve Block44f0eee2011-05-26 01:26:41 +01004249 inline void ClearCodeCache(Heap* heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00004250
4251 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07004252 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004253
4254 // Returns the found code or undefined if absent.
4255 Object* FindInCodeCache(String* name, Code::Flags flags);
4256
4257 // Returns the non-negative index of the code object if it is in the
4258 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01004259 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004260
4261 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01004262 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00004263
4264 // For every transition in this map, makes the transition's
4265 // target's prototype pointer point back to this map.
4266 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
4267 void CreateBackPointers();
4268
4269 // Set all map transitions from this map to dead maps to null.
4270 // Also, restore the original prototype on the targets of these
4271 // transitions, so that we do not process this map again while
4272 // following back pointers.
Steve Block44f0eee2011-05-26 01:26:41 +01004273 void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00004274
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004275 // Computes a hash value for this map, to be used in HashTables and such.
4276 int Hash();
4277
4278 // Compares this map to another to see if they describe equivalent objects.
4279 // If |mode| is set to CLEAR_INOBJECT_PROPERTIES, |other| is treated as if
4280 // it had exactly zero inobject properties.
4281 // The "shared" flags of both this map and |other| are ignored.
4282 bool EquivalentToForNormalization(Map* other, PropertyNormalizationMode mode);
4283
Ben Murdoch85b71792012-04-11 18:30:58 +01004284 // Returns true if this map and |other| describe equivalent objects.
4285 // The "shared" flags of both this map and |other| are ignored.
4286 bool EquivalentTo(Map* other) {
4287 return EquivalentToForNormalization(other, KEEP_INOBJECT_PROPERTIES);
4288 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004289
Steve Blocka7e24c12009-10-30 11:49:00 +00004290 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004291#ifdef OBJECT_PRINT
4292 inline void MapPrint() {
4293 MapPrint(stdout);
4294 }
4295 void MapPrint(FILE* out);
4296#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004297#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004298 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004299 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00004300#endif
4301
Iain Merrick75681382010-08-19 15:07:18 +01004302 inline int visitor_id();
4303 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004304
Ben Murdoch85b71792012-04-11 18:30:58 +01004305 // Returns the isolate/heap this map belongs to.
4306 inline Isolate* isolate();
4307 inline Heap* heap();
4308
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004309 typedef void (*TraverseCallback)(Map* map, void* data);
4310
4311 void TraverseTransitionTree(TraverseCallback callback, void* data);
4312
Steve Block053d10c2011-06-13 19:13:29 +01004313 static const int kMaxCachedPrototypeTransitions = 256;
4314
4315 Object* GetPrototypeTransition(Object* prototype);
4316
Ben Murdoch85b71792012-04-11 18:30:58 +01004317 MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
Steve Block053d10c2011-06-13 19:13:29 +01004318
Steve Blocka7e24c12009-10-30 11:49:00 +00004319 static const int kMaxPreAllocatedPropertyFields = 255;
4320
4321 // Layout description.
4322 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
4323 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
4324 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
4325 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00004326 // Storage for instance descriptors is overloaded to also contain additional
4327 // map flags when unused (bit_field3). When the map has instance descriptors,
4328 // the flags are transferred to the instance descriptor array and accessed
4329 // through an extra indirection.
4330 // TODO(1399): It should be possible to make room for bit_field3 in the map
4331 // without overloading the instance descriptors field, but the map is
4332 // currently perfectly aligned to 32 bytes and extending it at all would
4333 // double its size. After the increment GC work lands, this size restriction
4334 // could be loosened and bit_field3 moved directly back in the map.
4335 static const int kInstanceDescriptorsOrBitField3Offset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004336 kConstructorOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00004337 static const int kCodeCacheOffset =
4338 kInstanceDescriptorsOrBitField3Offset + kPointerSize;
Steve Block053d10c2011-06-13 19:13:29 +01004339 static const int kPrototypeTransitionsOffset =
4340 kCodeCacheOffset + kPointerSize;
4341 static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004342 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
4343
4344 // Layout of pointer fields. Heap iteration code relies on them
Ben Murdoch85b71792012-04-11 18:30:58 +01004345 // being continiously allocated.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004346 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
4347 static const int kPointerFieldsEndOffset =
Steve Block053d10c2011-06-13 19:13:29 +01004348 Map::kPrototypeTransitionsOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004349
4350 // Byte offsets within kInstanceSizesOffset.
4351 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
4352 static const int kInObjectPropertiesByte = 1;
4353 static const int kInObjectPropertiesOffset =
4354 kInstanceSizesOffset + kInObjectPropertiesByte;
4355 static const int kPreAllocatedPropertyFieldsByte = 2;
4356 static const int kPreAllocatedPropertyFieldsOffset =
4357 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01004358 static const int kVisitorIdByte = 3;
4359 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00004360
4361 // Byte offsets within kInstanceAttributesOffset attributes.
4362 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
4363 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
4364 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
4365 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
4366
4367 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
4368
4369 // Bit positions for bit field.
4370 static const int kUnused = 0; // To be used for marking recently used maps.
4371 static const int kHasNonInstancePrototype = 1;
4372 static const int kIsHiddenPrototype = 2;
4373 static const int kHasNamedInterceptor = 3;
4374 static const int kHasIndexedInterceptor = 4;
4375 static const int kIsUndetectable = 5;
4376 static const int kHasInstanceCallHandler = 6;
4377 static const int kIsAccessCheckNeeded = 7;
4378
4379 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00004380 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01004381 static const int kFunctionWithPrototype = 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004382 static const int kStringWrapperSafeForDefaultValueOf = 2;
4383 static const int kAttachedToSharedFunctionInfo = 3;
4384 // No bits can be used after kElementsKindFirstBit, they are all reserved for
Ben Murdoch85b71792012-04-11 18:30:58 +01004385 // storing ElementKind. for anything other than storing the ElementKind.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004386 static const int kElementsKindShift = 4;
4387 static const int kElementsKindBitCount = 4;
4388
4389 // Derived values from bit field 2
4390 static const int kElementsKindMask = (-1 << kElementsKindShift) &
4391 ((1 << (kElementsKindShift + kElementsKindBitCount)) - 1);
4392 static const int8_t kMaximumBitField2FastElementValue = static_cast<int8_t>(
Ben Murdoch589d6972011-11-30 16:04:58 +00004393 (FAST_ELEMENTS + 1) << Map::kElementsKindShift) - 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00004394
4395 // Bit positions for bit field 3
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004396 static const int kIsShared = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01004397
4398 // Layout of the default cache. It holds alternating name and code objects.
4399 static const int kCodeCacheEntrySize = 2;
4400 static const int kCodeCacheEntryNameOffset = 0;
4401 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00004402
Iain Merrick75681382010-08-19 15:07:18 +01004403 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
4404 kPointerFieldsEndOffset,
4405 kSize> BodyDescriptor;
4406
Steve Blocka7e24c12009-10-30 11:49:00 +00004407 private:
4408 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
4409};
4410
4411
4412// An abstract superclass, a marker class really, for simple structure classes.
Ben Murdoch257744e2011-11-30 15:57:28 +00004413// It doesn't carry much functionality but allows struct classes to be
Steve Blocka7e24c12009-10-30 11:49:00 +00004414// identified in the type system.
4415class Struct: public HeapObject {
4416 public:
4417 inline void InitializeBody(int object_size);
4418 static inline Struct* cast(Object* that);
4419};
4420
4421
4422// Script describes a script which has been added to the VM.
4423class Script: public Struct {
4424 public:
4425 // Script types.
4426 enum Type {
4427 TYPE_NATIVE = 0,
4428 TYPE_EXTENSION = 1,
4429 TYPE_NORMAL = 2
4430 };
4431
4432 // Script compilation types.
4433 enum CompilationType {
4434 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004435 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00004436 };
4437
4438 // [source]: the script source.
4439 DECL_ACCESSORS(source, Object)
4440
4441 // [name]: the script name.
4442 DECL_ACCESSORS(name, Object)
4443
4444 // [id]: the script id.
4445 DECL_ACCESSORS(id, Object)
4446
4447 // [line_offset]: script line offset in resource from where it was extracted.
4448 DECL_ACCESSORS(line_offset, Smi)
4449
4450 // [column_offset]: script column offset in resource from where it was
4451 // extracted.
4452 DECL_ACCESSORS(column_offset, Smi)
4453
4454 // [data]: additional data associated with this script.
4455 DECL_ACCESSORS(data, Object)
4456
4457 // [context_data]: context data for the context this script was compiled in.
4458 DECL_ACCESSORS(context_data, Object)
4459
4460 // [wrapper]: the wrapper cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00004461 DECL_ACCESSORS(wrapper, Foreign)
Steve Blocka7e24c12009-10-30 11:49:00 +00004462
4463 // [type]: the script type.
4464 DECL_ACCESSORS(type, Smi)
4465
4466 // [compilation]: how the the script was compiled.
4467 DECL_ACCESSORS(compilation_type, Smi)
4468
Steve Blockd0582a62009-12-15 09:54:21 +00004469 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00004470 DECL_ACCESSORS(line_ends, Object)
4471
Steve Blockd0582a62009-12-15 09:54:21 +00004472 // [eval_from_shared]: for eval scripts the shared funcion info for the
4473 // function from which eval was called.
4474 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004475
4476 // [eval_from_instructions_offset]: the instruction offset in the code for the
4477 // function from which eval was called where eval was called.
4478 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
4479
4480 static inline Script* cast(Object* obj);
4481
Steve Block3ce2e202009-11-05 08:53:23 +00004482 // If script source is an external string, check that the underlying
4483 // resource is accessible. Otherwise, always return true.
4484 inline bool HasValidSource();
4485
Ben Murdochb0fe1622011-05-05 13:52:32 +01004486#ifdef OBJECT_PRINT
4487 inline void ScriptPrint() {
4488 ScriptPrint(stdout);
4489 }
4490 void ScriptPrint(FILE* out);
4491#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004492#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004493 void ScriptVerify();
4494#endif
4495
4496 static const int kSourceOffset = HeapObject::kHeaderSize;
4497 static const int kNameOffset = kSourceOffset + kPointerSize;
4498 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4499 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
4500 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
4501 static const int kContextOffset = kDataOffset + kPointerSize;
4502 static const int kWrapperOffset = kContextOffset + kPointerSize;
4503 static const int kTypeOffset = kWrapperOffset + kPointerSize;
4504 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
Ben Murdoch85b71792012-04-11 18:30:58 +01004505 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004506 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00004507 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004508 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00004509 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004510 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
4511
4512 private:
4513 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4514};
4515
4516
Ben Murdochb0fe1622011-05-05 13:52:32 +01004517// List of builtin functions we want to identify to improve code
4518// generation.
4519//
4520// Each entry has a name of a global object property holding an object
4521// optionally followed by ".prototype", a name of a builtin function
4522// on the object (the one the id is set for), and a label.
4523//
4524// Installation of ids for the selected builtin functions is handled
4525// by the bootstrapper.
4526//
4527// NOTE: Order is important: math functions should be at the end of
4528// the list and MathFloor should be the first math function.
4529#define FUNCTIONS_WITH_ID_LIST(V) \
4530 V(Array.prototype, push, ArrayPush) \
4531 V(Array.prototype, pop, ArrayPop) \
Ben Murdoch42effa52011-08-19 16:40:31 +01004532 V(Function.prototype, apply, FunctionApply) \
Ben Murdochb0fe1622011-05-05 13:52:32 +01004533 V(String.prototype, charCodeAt, StringCharCodeAt) \
4534 V(String.prototype, charAt, StringCharAt) \
4535 V(String, fromCharCode, StringFromCharCode) \
4536 V(Math, floor, MathFloor) \
4537 V(Math, round, MathRound) \
4538 V(Math, ceil, MathCeil) \
4539 V(Math, abs, MathAbs) \
4540 V(Math, log, MathLog) \
4541 V(Math, sin, MathSin) \
4542 V(Math, cos, MathCos) \
4543 V(Math, tan, MathTan) \
4544 V(Math, asin, MathASin) \
4545 V(Math, acos, MathACos) \
4546 V(Math, atan, MathATan) \
4547 V(Math, exp, MathExp) \
4548 V(Math, sqrt, MathSqrt) \
Ben Murdoch85b71792012-04-11 18:30:58 +01004549 V(Math, pow, MathPow)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004550
4551
4552enum BuiltinFunctionId {
4553#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
4554 k##name,
4555 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4556#undef DECLARE_FUNCTION_ID
4557 // Fake id for a special case of Math.pow. Note, it continues the
4558 // list of math functions.
4559 kMathPowHalf,
4560 kFirstMathFunctionId = kMathFloor
4561};
4562
4563
Steve Blocka7e24c12009-10-30 11:49:00 +00004564// SharedFunctionInfo describes the JSFunction information that can be
4565// shared by multiple instances of the function.
4566class SharedFunctionInfo: public HeapObject {
4567 public:
4568 // [name]: Function name.
4569 DECL_ACCESSORS(name, Object)
4570
4571 // [code]: Function code.
4572 DECL_ACCESSORS(code, Code)
4573
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004574 // [scope_info]: Scope info.
Ben Murdoch85b71792012-04-11 18:30:58 +01004575 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004576
Steve Blocka7e24c12009-10-30 11:49:00 +00004577 // [construct stub]: Code stub for constructing instances of this function.
4578 DECL_ACCESSORS(construct_stub, Code)
4579
Iain Merrick75681382010-08-19 15:07:18 +01004580 inline Code* unchecked_code();
4581
Steve Blocka7e24c12009-10-30 11:49:00 +00004582 // Returns if this function has been compiled to native code yet.
4583 inline bool is_compiled();
4584
4585 // [length]: The function length - usually the number of declared parameters.
4586 // Use up to 2^30 parameters.
4587 inline int length();
4588 inline void set_length(int value);
4589
4590 // [formal parameter count]: The declared number of parameters.
4591 inline int formal_parameter_count();
4592 inline void set_formal_parameter_count(int value);
4593
4594 // Set the formal parameter count so the function code will be
4595 // called without using argument adaptor frames.
4596 inline void DontAdaptArguments();
4597
4598 // [expected_nof_properties]: Expected number of properties for the function.
4599 inline int expected_nof_properties();
4600 inline void set_expected_nof_properties(int value);
4601
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004602 // Inobject slack tracking is the way to reclaim unused inobject space.
4603 //
4604 // The instance size is initially determined by adding some slack to
4605 // expected_nof_properties (to allow for a few extra properties added
4606 // after the constructor). There is no guarantee that the extra space
4607 // will not be wasted.
4608 //
4609 // Here is the algorithm to reclaim the unused inobject space:
4610 // - Detect the first constructor call for this SharedFunctionInfo.
4611 // When it happens enter the "in progress" state: remember the
4612 // constructor's initial_map and install a special construct stub that
4613 // counts constructor calls.
4614 // - While the tracking is in progress create objects filled with
4615 // one_pointer_filler_map instead of undefined_value. This way they can be
4616 // resized quickly and safely.
4617 // - Once enough (kGenerousAllocationCount) objects have been created
4618 // compute the 'slack' (traverse the map transition tree starting from the
4619 // initial_map and find the lowest value of unused_property_fields).
4620 // - Traverse the transition tree again and decrease the instance size
4621 // of every map. Existing objects will resize automatically (they are
4622 // filled with one_pointer_filler_map). All further allocations will
4623 // use the adjusted instance size.
4624 // - Decrease expected_nof_properties so that an allocations made from
4625 // another context will use the adjusted instance size too.
4626 // - Exit "in progress" state by clearing the reference to the initial_map
4627 // and setting the regular construct stub (generic or inline).
4628 //
4629 // The above is the main event sequence. Some special cases are possible
4630 // while the tracking is in progress:
4631 //
4632 // - GC occurs.
4633 // Check if the initial_map is referenced by any live objects (except this
4634 // SharedFunctionInfo). If it is, continue tracking as usual.
4635 // If it is not, clear the reference and reset the tracking state. The
4636 // tracking will be initiated again on the next constructor call.
4637 //
4638 // - The constructor is called from another context.
4639 // Immediately complete the tracking, perform all the necessary changes
4640 // to maps. This is necessary because there is no efficient way to track
4641 // multiple initial_maps.
4642 // Proceed to create an object in the current context (with the adjusted
4643 // size).
4644 //
4645 // - A different constructor function sharing the same SharedFunctionInfo is
4646 // called in the same context. This could be another closure in the same
4647 // context, or the first function could have been disposed.
4648 // This is handled the same way as the previous case.
4649 //
4650 // Important: inobject slack tracking is not attempted during the snapshot
4651 // creation.
4652
Ben Murdochf87a2032010-10-22 12:50:53 +01004653 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004654
4655 // [construction_count]: Counter for constructor calls made during
4656 // the tracking phase.
4657 inline int construction_count();
4658 inline void set_construction_count(int value);
4659
4660 // [initial_map]: initial map of the first function called as a constructor.
4661 // Saved for the duration of the tracking phase.
4662 // This is a weak link (GC resets it to undefined_value if no other live
4663 // object reference this map).
4664 DECL_ACCESSORS(initial_map, Object)
4665
4666 // True if the initial_map is not undefined and the countdown stub is
4667 // installed.
4668 inline bool IsInobjectSlackTrackingInProgress();
4669
4670 // Starts the tracking.
4671 // Stores the initial map and installs the countdown stub.
4672 // IsInobjectSlackTrackingInProgress is normally true after this call,
4673 // except when tracking have not been started (e.g. the map has no unused
4674 // properties or the snapshot is being built).
4675 void StartInobjectSlackTracking(Map* map);
4676
4677 // Completes the tracking.
4678 // IsInobjectSlackTrackingInProgress is false after this call.
4679 void CompleteInobjectSlackTracking();
4680
4681 // Clears the initial_map before the GC marking phase to ensure the reference
4682 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4683 void DetachInitialMap();
4684
4685 // Restores the link to the initial map after the GC marking phase.
4686 // IsInobjectSlackTrackingInProgress is true after this call.
4687 void AttachInitialMap(Map* map);
4688
4689 // False if there are definitely no live objects created from this function.
4690 // True if live objects _may_ exist (existence not guaranteed).
4691 // May go back from true to false after GC.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004692 DECL_BOOLEAN_ACCESSORS(live_objects_may_exist)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004693
Steve Blocka7e24c12009-10-30 11:49:00 +00004694 // [instance class name]: class name for instances.
4695 DECL_ACCESSORS(instance_class_name, Object)
4696
Steve Block6ded16b2010-05-10 14:33:55 +01004697 // [function data]: This field holds some additional data for function.
4698 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004699 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004700 // In the long run we don't want all functions to have this field but
4701 // we can fix that when we have a better model for storing hidden data
4702 // on objects.
4703 DECL_ACCESSORS(function_data, Object)
4704
Steve Block6ded16b2010-05-10 14:33:55 +01004705 inline bool IsApiFunction();
4706 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004707 inline bool HasBuiltinFunctionId();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004708 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004709
Steve Blocka7e24c12009-10-30 11:49:00 +00004710 // [script info]: Script from which the function originates.
4711 DECL_ACCESSORS(script, Object)
4712
Steve Block6ded16b2010-05-10 14:33:55 +01004713 // [num_literals]: Number of literals used by this function.
4714 inline int num_literals();
4715 inline void set_num_literals(int value);
4716
Steve Blocka7e24c12009-10-30 11:49:00 +00004717 // [start_position_and_type]: Field used to store both the source code
4718 // position, whether or not the function is a function expression,
4719 // and whether or not the function is a toplevel function. The two
4720 // least significants bit indicates whether the function is an
4721 // expression and the rest contains the source code position.
4722 inline int start_position_and_type();
4723 inline void set_start_position_and_type(int value);
4724
4725 // [debug info]: Debug information.
4726 DECL_ACCESSORS(debug_info, Object)
4727
4728 // [inferred name]: Name inferred from variable or property
4729 // assignment of this function. Used to facilitate debugging and
4730 // profiling of JavaScript code written in OO style, where almost
4731 // all functions are anonymous but are assigned to object
4732 // properties.
4733 DECL_ACCESSORS(inferred_name, String)
4734
Ben Murdochf87a2032010-10-22 12:50:53 +01004735 // The function's name if it is non-empty, otherwise the inferred name.
4736 String* DebugName();
4737
Steve Blocka7e24c12009-10-30 11:49:00 +00004738 // Position of the 'function' token in the script source.
4739 inline int function_token_position();
4740 inline void set_function_token_position(int function_token_position);
4741
4742 // Position of this function in the script source.
4743 inline int start_position();
4744 inline void set_start_position(int start_position);
4745
4746 // End position of this function in the script source.
4747 inline int end_position();
4748 inline void set_end_position(int end_position);
4749
4750 // Is this function a function expression in the source code.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004751 DECL_BOOLEAN_ACCESSORS(is_expression)
Steve Blocka7e24c12009-10-30 11:49:00 +00004752
4753 // Is this function a top-level function (scripts, evals).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004754 DECL_BOOLEAN_ACCESSORS(is_toplevel)
Steve Blocka7e24c12009-10-30 11:49:00 +00004755
4756 // Bit field containing various information collected by the compiler to
4757 // drive optimization.
4758 inline int compiler_hints();
4759 inline void set_compiler_hints(int value);
4760
Ben Murdochb0fe1622011-05-05 13:52:32 +01004761 // A counter used to determine when to stress the deoptimizer with a
4762 // deopt.
Ben Murdoch85b71792012-04-11 18:30:58 +01004763 inline Smi* deopt_counter();
4764 inline void set_deopt_counter(Smi* counter);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004765
Steve Blocka7e24c12009-10-30 11:49:00 +00004766 // Add information on assignments of the form this.x = ...;
4767 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004768 bool has_only_simple_this_property_assignments,
4769 FixedArray* this_property_assignments);
4770
4771 // Clear information on assignments of the form this.x = ...;
4772 void ClearThisPropertyAssignmentsInfo();
4773
4774 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004775 // this.x = y; where y is either a constant or refers to an argument.
4776 inline bool has_only_simple_this_property_assignments();
4777
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004778 // Indicates if this function can be lazy compiled.
4779 // This is used to determine if we can safely flush code from a function
4780 // when doing GC if we expect that the function will no longer be used.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004781 DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004782
Iain Merrick75681382010-08-19 15:07:18 +01004783 // Indicates how many full GCs this function has survived with assigned
4784 // code object. Used to determine when it is relatively safe to flush
4785 // this code object and replace it with lazy compilation stub.
4786 // Age is reset when GC notices that the code object is referenced
4787 // from the stack or compilation cache.
4788 inline int code_age();
4789 inline void set_code_age(int age);
4790
Ben Murdochb0fe1622011-05-05 13:52:32 +01004791 // Indicates whether optimizations have been disabled for this
4792 // shared function info. If a function is repeatedly optimized or if
4793 // we cannot optimize the function we disable optimization to avoid
4794 // spending time attempting to optimize it again.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004795 DECL_BOOLEAN_ACCESSORS(optimization_disabled)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004796
Ben Murdoch85b71792012-04-11 18:30:58 +01004797 // Indicates whether the function is a strict mode function.
4798 DECL_BOOLEAN_ACCESSORS(strict_mode)
Steve Block1e0659c2011-05-24 12:43:12 +01004799
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004800 // False if the function definitely does not allocate an arguments object.
4801 DECL_BOOLEAN_ACCESSORS(uses_arguments)
4802
4803 // True if the function has any duplicated parameter names.
4804 DECL_BOOLEAN_ACCESSORS(has_duplicate_parameters)
4805
4806 // Indicates whether the function is a native function.
Ben Murdoch589d6972011-11-30 16:04:58 +00004807 // These needs special treatment in .call and .apply since
Ben Murdoch257744e2011-11-30 15:57:28 +00004808 // null passed as the receiver should not be translated to the
4809 // global object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004810 DECL_BOOLEAN_ACCESSORS(native)
4811
4812 // Indicates that the function was created by the Function function.
4813 // Though it's anonymous, toString should treat it as if it had the name
4814 // "anonymous". We don't set the name itself so that the system does not
4815 // see a binding for it.
4816 DECL_BOOLEAN_ACCESSORS(name_should_print_as_anonymous)
4817
4818 // Indicates whether the function is a bound function created using
4819 // the bind function.
4820 DECL_BOOLEAN_ACCESSORS(bound)
4821
4822 // Indicates that the function is anonymous (the name field can be set
4823 // through the API, which does not change this flag).
4824 DECL_BOOLEAN_ACCESSORS(is_anonymous)
Ben Murdoch257744e2011-11-30 15:57:28 +00004825
Ben Murdochb0fe1622011-05-05 13:52:32 +01004826 // Indicates whether or not the code in the shared function support
4827 // deoptimization.
4828 inline bool has_deoptimization_support();
4829
4830 // Enable deoptimization support through recompiled code.
4831 void EnableDeoptimizationSupport(Code* recompiled);
4832
Ben Murdoch257744e2011-11-30 15:57:28 +00004833 // Disable (further) attempted optimization of all functions sharing this
Ben Murdoch85b71792012-04-11 18:30:58 +01004834 // shared function info. The function is the one we actually tried to
4835 // optimize.
4836 void DisableOptimization(JSFunction* function);
Ben Murdoch257744e2011-11-30 15:57:28 +00004837
Ben Murdochb0fe1622011-05-05 13:52:32 +01004838 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4839 // code, returns whether it asserted (i.e., always true if assertions are
4840 // disabled).
4841 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004842
Andrei Popescu402d9372010-02-26 13:31:12 +00004843 // Check whether a inlined constructor can be generated with the given
4844 // prototype.
4845 bool CanGenerateInlineConstructor(Object* prototype);
4846
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004847 // Prevents further attempts to generate inline constructors.
4848 // To be called if generation failed for any reason.
4849 void ForbidInlineConstructor();
4850
Steve Blocka7e24c12009-10-30 11:49:00 +00004851 // For functions which only contains this property assignments this provides
4852 // access to the names for the properties assigned.
4853 DECL_ACCESSORS(this_property_assignments, Object)
4854 inline int this_property_assignments_count();
4855 inline void set_this_property_assignments_count(int value);
4856 String* GetThisPropertyAssignmentName(int index);
4857 bool IsThisPropertyAssignmentArgument(int index);
4858 int GetThisPropertyAssignmentArgument(int index);
4859 Object* GetThisPropertyAssignmentConstant(int index);
4860
4861 // [source code]: Source code for the function.
4862 bool HasSourceCode();
Ben Murdoch85b71792012-04-11 18:30:58 +01004863 Object* GetSourceCode();
Steve Blocka7e24c12009-10-30 11:49:00 +00004864
Ben Murdochb0fe1622011-05-05 13:52:32 +01004865 inline int opt_count();
4866 inline void set_opt_count(int opt_count);
4867
4868 // Source size of this function.
4869 int SourceSize();
4870
Steve Blocka7e24c12009-10-30 11:49:00 +00004871 // Calculate the instance size.
4872 int CalculateInstanceSize();
4873
4874 // Calculate the number of in-object properties.
4875 int CalculateInObjectProperties();
4876
4877 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004878 // Set max_length to -1 for unlimited length.
4879 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004880#ifdef OBJECT_PRINT
4881 inline void SharedFunctionInfoPrint() {
4882 SharedFunctionInfoPrint(stdout);
4883 }
4884 void SharedFunctionInfoPrint(FILE* out);
4885#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004886#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004887 void SharedFunctionInfoVerify();
4888#endif
4889
4890 // Casting.
4891 static inline SharedFunctionInfo* cast(Object* obj);
4892
4893 // Constants.
4894 static const int kDontAdaptArgumentsSentinel = -1;
4895
4896 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004897 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004898 static const int kNameOffset = HeapObject::kHeaderSize;
4899 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004900 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4901 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004902 static const int kInstanceClassNameOffset =
4903 kConstructStubOffset + kPointerSize;
4904 static const int kFunctionDataOffset =
4905 kInstanceClassNameOffset + kPointerSize;
4906 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4907 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4908 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004909 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004910 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004911 static const int kThisPropertyAssignmentsOffset =
4912 kInitialMapOffset + kPointerSize;
Ben Murdoch85b71792012-04-11 18:30:58 +01004913 static const int kDeoptCounterOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004914 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004915#if V8_HOST_ARCH_32_BIT
4916 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004917 static const int kLengthOffset =
Ben Murdoch85b71792012-04-11 18:30:58 +01004918 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004919 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4920 static const int kExpectedNofPropertiesOffset =
4921 kFormalParameterCountOffset + kPointerSize;
4922 static const int kNumLiteralsOffset =
4923 kExpectedNofPropertiesOffset + kPointerSize;
4924 static const int kStartPositionAndTypeOffset =
4925 kNumLiteralsOffset + kPointerSize;
4926 static const int kEndPositionOffset =
4927 kStartPositionAndTypeOffset + kPointerSize;
4928 static const int kFunctionTokenPositionOffset =
4929 kEndPositionOffset + kPointerSize;
4930 static const int kCompilerHintsOffset =
4931 kFunctionTokenPositionOffset + kPointerSize;
4932 static const int kThisPropertyAssignmentsCountOffset =
4933 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004934 static const int kOptCountOffset =
4935 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004936 // Total size.
Ben Murdoch85b71792012-04-11 18:30:58 +01004937 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004938#else
4939 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004940 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004941 // garbage collections.
4942 // To avoid wasting space on 64-bit architectures we use
4943 // the following trick: we group integer fields into pairs
4944 // First integer in each pair is shifted left by 1.
4945 // By doing this we guarantee that LSB of each kPointerSize aligned
4946 // word is not set and thus this word cannot be treated as pointer
4947 // to HeapObject during old space traversal.
4948 static const int kLengthOffset =
Ben Murdoch85b71792012-04-11 18:30:58 +01004949 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004950 static const int kFormalParameterCountOffset =
4951 kLengthOffset + kIntSize;
4952
Steve Blocka7e24c12009-10-30 11:49:00 +00004953 static const int kExpectedNofPropertiesOffset =
4954 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004955 static const int kNumLiteralsOffset =
4956 kExpectedNofPropertiesOffset + kIntSize;
4957
4958 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004959 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004960 static const int kStartPositionAndTypeOffset =
4961 kEndPositionOffset + kIntSize;
4962
4963 static const int kFunctionTokenPositionOffset =
4964 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004965 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004966 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004967
Steve Blocka7e24c12009-10-30 11:49:00 +00004968 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004969 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004970 static const int kOptCountOffset =
4971 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004972
Steve Block6ded16b2010-05-10 14:33:55 +01004973 // Total size.
Ben Murdoch85b71792012-04-11 18:30:58 +01004974 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004975
4976#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004977
4978 // The construction counter for inobject slack tracking is stored in the
4979 // most significant byte of compiler_hints which is otherwise unused.
4980 // Its offset depends on the endian-ness of the architecture.
4981#if __BYTE_ORDER == __LITTLE_ENDIAN
4982 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4983#elif __BYTE_ORDER == __BIG_ENDIAN
4984 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4985#else
4986#error Unknown byte ordering
4987#endif
4988
Steve Block6ded16b2010-05-10 14:33:55 +01004989 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004990
Iain Merrick75681382010-08-19 15:07:18 +01004991 typedef FixedBodyDescriptor<kNameOffset,
4992 kThisPropertyAssignmentsOffset + kPointerSize,
4993 kSize> BodyDescriptor;
4994
Steve Blocka7e24c12009-10-30 11:49:00 +00004995 // Bit positions in start_position_and_type.
4996 // The source code start position is in the 30 most significant bits of
4997 // the start_position_and_type field.
4998 static const int kIsExpressionBit = 0;
4999 static const int kIsTopLevelBit = 1;
5000 static const int kStartPositionShift = 2;
5001 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
5002
5003 // Bit positions in compiler_hints.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005004 static const int kCodeAgeSize = 3;
5005 static const int kCodeAgeMask = (1 << kCodeAgeSize) - 1;
5006
5007 enum CompilerHints {
5008 kHasOnlySimpleThisPropertyAssignments,
5009 kAllowLazyCompilation,
5010 kLiveObjectsMayExist,
5011 kCodeAgeShift,
5012 kOptimizationDisabled = kCodeAgeShift + kCodeAgeSize,
5013 kStrictModeFunction,
5014 kUsesArguments,
5015 kHasDuplicateParameters,
5016 kNative,
5017 kBoundFunction,
5018 kIsAnonymous,
5019 kNameShouldPrintAsAnonymous,
5020 kCompilerHintsCount // Pseudo entry
5021 };
Steve Blocka7e24c12009-10-30 11:49:00 +00005022
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005023 private:
5024#if V8_HOST_ARCH_32_BIT
5025 // On 32 bit platforms, compiler hints is a smi.
5026 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
5027 static const int kCompilerHintsSize = kPointerSize;
5028#else
5029 // On 64 bit platforms, compiler hints is not a smi, see comment above.
5030 static const int kCompilerHintsSmiTagSize = 0;
5031 static const int kCompilerHintsSize = kIntSize;
5032#endif
5033
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005034 STATIC_ASSERT(SharedFunctionInfo::kCompilerHintsCount <=
5035 SharedFunctionInfo::kCompilerHintsSize * kBitsPerByte);
5036
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005037 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00005038 // Constants for optimizing codegen for strict mode function and
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005039 // native tests.
Ben Murdoch85b71792012-04-11 18:30:58 +01005040 // Allows to use byte-widgh instructions.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005041 static const int kStrictModeBitWithinByte =
5042 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
5043
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005044 static const int kNativeBitWithinByte =
5045 (kNative + kCompilerHintsSmiTagSize) % kBitsPerByte;
Ben Murdoch257744e2011-11-30 15:57:28 +00005046
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005047#if __BYTE_ORDER == __LITTLE_ENDIAN
5048 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00005049 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005050 static const int kNativeByteOffset = kCompilerHintsOffset +
5051 (kNative + kCompilerHintsSmiTagSize) / kBitsPerByte;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005052#elif __BYTE_ORDER == __BIG_ENDIAN
5053 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00005054 (kCompilerHintsSize - 1) -
5055 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005056 static const int kNativeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00005057 (kCompilerHintsSize - 1) -
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005058 ((kNative + kCompilerHintsSmiTagSize) / kBitsPerByte);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005059#else
5060#error Unknown byte ordering
5061#endif
5062
5063 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00005064 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
5065};
5066
5067
5068// JSFunction describes JavaScript functions.
5069class JSFunction: public JSObject {
5070 public:
5071 // [prototype_or_initial_map]:
5072 DECL_ACCESSORS(prototype_or_initial_map, Object)
5073
Ben Murdoch589d6972011-11-30 16:04:58 +00005074 // [shared]: The information about the function that
Steve Blocka7e24c12009-10-30 11:49:00 +00005075 // can be shared by instances.
5076 DECL_ACCESSORS(shared, SharedFunctionInfo)
5077
Iain Merrick75681382010-08-19 15:07:18 +01005078 inline SharedFunctionInfo* unchecked_shared();
5079
Steve Blocka7e24c12009-10-30 11:49:00 +00005080 // [context]: The context for this function.
5081 inline Context* context();
5082 inline Object* unchecked_context();
5083 inline void set_context(Object* context);
5084
5085 // [code]: The generated code object for this function. Executed
5086 // when the function is invoked, e.g. foo() or new foo(). See
5087 // [[Call]] and [[Construct]] description in ECMA-262, section
5088 // 8.6.2, page 27.
5089 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005090 inline void set_code(Code* code);
5091 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00005092
Iain Merrick75681382010-08-19 15:07:18 +01005093 inline Code* unchecked_code();
5094
Steve Blocka7e24c12009-10-30 11:49:00 +00005095 // Tells whether this function is builtin.
5096 inline bool IsBuiltin();
5097
Ben Murdochb0fe1622011-05-05 13:52:32 +01005098 // Tells whether or not the function needs arguments adaption.
5099 inline bool NeedsArgumentsAdaption();
5100
5101 // Tells whether or not this function has been optimized.
5102 inline bool IsOptimized();
5103
Ben Murdoch8b112d22011-06-08 16:22:53 +01005104 // Tells whether or not this function can be optimized.
5105 inline bool IsOptimizable();
5106
Ben Murdochb0fe1622011-05-05 13:52:32 +01005107 // Mark this function for lazy recompilation. The function will be
5108 // recompiled the next time it is executed.
5109 void MarkForLazyRecompilation();
5110
5111 // Tells whether or not the function is already marked for lazy
5112 // recompilation.
5113 inline bool IsMarkedForLazyRecompilation();
5114
Ben Murdochb0fe1622011-05-05 13:52:32 +01005115 // Check whether or not this function is inlineable.
5116 bool IsInlineable();
5117
Ben Murdoch85b71792012-04-11 18:30:58 +01005118 // [literals]: Fixed array holding the materialized literals.
Steve Blocka7e24c12009-10-30 11:49:00 +00005119 //
5120 // If the function contains object, regexp or array literals, the
5121 // literals array prefix contains the object, regexp, and array
5122 // function to be used when creating these literals. This is
5123 // necessary so that we do not dynamically lookup the object, regexp
5124 // or array functions. Performing a dynamic lookup, we might end up
5125 // using the functions from a new context that we should not have
5126 // access to.
Ben Murdoch85b71792012-04-11 18:30:58 +01005127 DECL_ACCESSORS(literals, FixedArray)
Steve Blocka7e24c12009-10-30 11:49:00 +00005128
5129 // The initial map for an object created by this constructor.
5130 inline Map* initial_map();
5131 inline void set_initial_map(Map* value);
5132 inline bool has_initial_map();
5133
5134 // Get and set the prototype property on a JSFunction. If the
5135 // function has an initial map the prototype is set on the initial
5136 // map. Otherwise, the prototype is put in the initial map field
5137 // until an initial map is needed.
5138 inline bool has_prototype();
5139 inline bool has_instance_prototype();
5140 inline Object* prototype();
5141 inline Object* instance_prototype();
Ben Murdoch85b71792012-04-11 18:30:58 +01005142 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07005143 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005144
Steve Block6ded16b2010-05-10 14:33:55 +01005145 // After prototype is removed, it will not be created when accessed, and
5146 // [[Construct]] from this function will not be allowed.
5147 Object* RemovePrototype();
5148 inline bool should_have_prototype();
5149
Steve Blocka7e24c12009-10-30 11:49:00 +00005150 // Accessor for this function's initial map's [[class]]
5151 // property. This is primarily used by ECMA native functions. This
5152 // method sets the class_name field of this function's initial map
5153 // to a given value. It creates an initial map if this function does
5154 // not have one. Note that this method does not copy the initial map
5155 // if it has one already, but simply replaces it with the new value.
5156 // Instances created afterwards will have a map whose [[class]] is
5157 // set to 'value', but there is no guarantees on instances created
5158 // before.
5159 Object* SetInstanceClassName(String* name);
5160
5161 // Returns if this function has been compiled to native code yet.
5162 inline bool is_compiled();
5163
Ben Murdochb0fe1622011-05-05 13:52:32 +01005164 // [next_function_link]: Field for linking functions. This list is treated as
5165 // a weak list by the GC.
5166 DECL_ACCESSORS(next_function_link, Object)
5167
5168 // Prints the name of the function using PrintF.
5169 inline void PrintName() {
5170 PrintName(stdout);
5171 }
5172 void PrintName(FILE* out);
5173
Steve Blocka7e24c12009-10-30 11:49:00 +00005174 // Casting.
5175 static inline JSFunction* cast(Object* obj);
5176
Steve Block791712a2010-08-27 10:21:07 +01005177 // Iterates the objects, including code objects indirectly referenced
5178 // through pointers to the first instruction in the code object.
5179 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
5180
Steve Blocka7e24c12009-10-30 11:49:00 +00005181 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005182#ifdef OBJECT_PRINT
5183 inline void JSFunctionPrint() {
5184 JSFunctionPrint(stdout);
5185 }
5186 void JSFunctionPrint(FILE* out);
5187#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005188#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005189 void JSFunctionVerify();
5190#endif
5191
5192 // Returns the number of allocated literals.
5193 inline int NumberOfLiterals();
5194
5195 // Retrieve the global context from a function's literal array.
5196 static Context* GlobalContextFromLiterals(FixedArray* literals);
5197
Ben Murdochb0fe1622011-05-05 13:52:32 +01005198 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
5199 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01005200 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01005201 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01005202 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005203 static const int kSharedFunctionInfoOffset =
5204 kPrototypeOrInitialMapOffset + kPointerSize;
5205 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
5206 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005207 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
5208 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
5209 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005210
5211 // Layout of the literals array.
5212 static const int kLiteralsPrefixSize = 1;
5213 static const int kLiteralGlobalContextIndex = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005214
Steve Blocka7e24c12009-10-30 11:49:00 +00005215 private:
5216 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
5217};
5218
5219
5220// JSGlobalProxy's prototype must be a JSGlobalObject or null,
5221// and the prototype is hidden. JSGlobalProxy always delegates
5222// property accesses to its prototype if the prototype is not null.
5223//
5224// A JSGlobalProxy can be reinitialized which will preserve its identity.
5225//
5226// Accessing a JSGlobalProxy requires security check.
5227
5228class JSGlobalProxy : public JSObject {
5229 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00005230 // [context]: the owner global context of this global proxy object.
Steve Blocka7e24c12009-10-30 11:49:00 +00005231 // It is null value if this object is not used by any context.
5232 DECL_ACCESSORS(context, Object)
5233
5234 // Casting.
5235 static inline JSGlobalProxy* cast(Object* obj);
5236
5237 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005238#ifdef OBJECT_PRINT
5239 inline void JSGlobalProxyPrint() {
5240 JSGlobalProxyPrint(stdout);
5241 }
5242 void JSGlobalProxyPrint(FILE* out);
5243#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005244#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005245 void JSGlobalProxyVerify();
5246#endif
5247
5248 // Layout description.
5249 static const int kContextOffset = JSObject::kHeaderSize;
5250 static const int kSize = kContextOffset + kPointerSize;
5251
5252 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00005253 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
5254};
5255
5256
5257// Forward declaration.
5258class JSBuiltinsObject;
Ben Murdoch85b71792012-04-11 18:30:58 +01005259class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00005260
5261// Common super class for JavaScript global objects and the special
5262// builtins global objects.
5263class GlobalObject: public JSObject {
5264 public:
5265 // [builtins]: the object holding the runtime routines written in JS.
5266 DECL_ACCESSORS(builtins, JSBuiltinsObject)
5267
5268 // [global context]: the global context corresponding to this global object.
5269 DECL_ACCESSORS(global_context, Context)
5270
5271 // [global receiver]: the global receiver object of the context
5272 DECL_ACCESSORS(global_receiver, JSObject)
5273
5274 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005275 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00005276
John Reck59135872010-11-02 12:39:01 -07005277 // This is like GetProperty, but is used when you know the lookup won't fail
5278 // by throwing an exception. This is for the debug and builtins global
5279 // objects, where it is known which properties can be expected to be present
5280 // on the object.
5281 Object* GetPropertyNoExceptionThrown(String* key) {
5282 Object* answer = GetProperty(key)->ToObjectUnchecked();
5283 return answer;
5284 }
5285
Steve Blocka7e24c12009-10-30 11:49:00 +00005286 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07005287 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00005288
5289 // Casting.
5290 static inline GlobalObject* cast(Object* obj);
5291
5292 // Layout description.
5293 static const int kBuiltinsOffset = JSObject::kHeaderSize;
5294 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
5295 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
5296 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
5297
5298 private:
Ben Murdoch85b71792012-04-11 18:30:58 +01005299 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
5300
Steve Blocka7e24c12009-10-30 11:49:00 +00005301 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
5302};
5303
5304
5305// JavaScript global object.
5306class JSGlobalObject: public GlobalObject {
5307 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00005308 // Casting.
5309 static inline JSGlobalObject* cast(Object* obj);
5310
5311 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005312#ifdef OBJECT_PRINT
5313 inline void JSGlobalObjectPrint() {
5314 JSGlobalObjectPrint(stdout);
5315 }
5316 void JSGlobalObjectPrint(FILE* out);
5317#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005318#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005319 void JSGlobalObjectVerify();
5320#endif
5321
5322 // Layout description.
5323 static const int kSize = GlobalObject::kHeaderSize;
5324
5325 private:
5326 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
5327};
5328
5329
5330// Builtins global object which holds the runtime routines written in
5331// JavaScript.
5332class JSBuiltinsObject: public GlobalObject {
5333 public:
5334 // Accessors for the runtime routines written in JavaScript.
5335 inline Object* javascript_builtin(Builtins::JavaScript id);
5336 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
5337
Steve Block6ded16b2010-05-10 14:33:55 +01005338 // Accessors for code of the runtime routines written in JavaScript.
5339 inline Code* javascript_builtin_code(Builtins::JavaScript id);
5340 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
5341
Steve Blocka7e24c12009-10-30 11:49:00 +00005342 // Casting.
5343 static inline JSBuiltinsObject* cast(Object* obj);
5344
5345 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005346#ifdef OBJECT_PRINT
5347 inline void JSBuiltinsObjectPrint() {
5348 JSBuiltinsObjectPrint(stdout);
5349 }
5350 void JSBuiltinsObjectPrint(FILE* out);
5351#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005352#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005353 void JSBuiltinsObjectVerify();
5354#endif
5355
5356 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01005357 // room for two pointers per runtime routine written in javascript
5358 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00005359 static const int kJSBuiltinsCount = Builtins::id_count;
5360 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005361 static const int kJSBuiltinsCodeOffset =
5362 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005363 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01005364 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
5365
5366 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
5367 return kJSBuiltinsOffset + id * kPointerSize;
5368 }
5369
5370 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
5371 return kJSBuiltinsCodeOffset + id * kPointerSize;
5372 }
5373
Steve Blocka7e24c12009-10-30 11:49:00 +00005374 private:
5375 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
5376};
5377
5378
Ben Murdoch85b71792012-04-11 18:30:58 +01005379// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
Steve Blocka7e24c12009-10-30 11:49:00 +00005380class JSValue: public JSObject {
5381 public:
5382 // [value]: the object being wrapped.
5383 DECL_ACCESSORS(value, Object)
5384
5385 // Casting.
5386 static inline JSValue* cast(Object* obj);
5387
5388 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005389#ifdef OBJECT_PRINT
5390 inline void JSValuePrint() {
5391 JSValuePrint(stdout);
5392 }
5393 void JSValuePrint(FILE* out);
5394#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005395#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005396 void JSValueVerify();
5397#endif
5398
5399 // Layout description.
5400 static const int kValueOffset = JSObject::kHeaderSize;
5401 static const int kSize = kValueOffset + kPointerSize;
5402
5403 private:
5404 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
5405};
5406
Steve Block1e0659c2011-05-24 12:43:12 +01005407
5408// Representation of message objects used for error reporting through
5409// the API. The messages are formatted in JavaScript so this object is
5410// a real JavaScript object. The information used for formatting the
5411// error messages are not directly accessible from JavaScript to
5412// prevent leaking information to user code called during error
5413// formatting.
5414class JSMessageObject: public JSObject {
5415 public:
5416 // [type]: the type of error message.
5417 DECL_ACCESSORS(type, String)
5418
5419 // [arguments]: the arguments for formatting the error message.
5420 DECL_ACCESSORS(arguments, JSArray)
5421
5422 // [script]: the script from which the error message originated.
5423 DECL_ACCESSORS(script, Object)
5424
5425 // [stack_trace]: the stack trace for this error message.
5426 DECL_ACCESSORS(stack_trace, Object)
5427
5428 // [stack_frames]: an array of stack frames for this error object.
5429 DECL_ACCESSORS(stack_frames, Object)
5430
5431 // [start_position]: the start position in the script for the error message.
5432 inline int start_position();
5433 inline void set_start_position(int value);
5434
5435 // [end_position]: the end position in the script for the error message.
5436 inline int end_position();
5437 inline void set_end_position(int value);
5438
5439 // Casting.
5440 static inline JSMessageObject* cast(Object* obj);
5441
5442 // Dispatched behavior.
5443#ifdef OBJECT_PRINT
5444 inline void JSMessageObjectPrint() {
5445 JSMessageObjectPrint(stdout);
5446 }
5447 void JSMessageObjectPrint(FILE* out);
5448#endif
5449#ifdef DEBUG
5450 void JSMessageObjectVerify();
5451#endif
5452
5453 // Layout description.
5454 static const int kTypeOffset = JSObject::kHeaderSize;
5455 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
5456 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
5457 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
5458 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
5459 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
5460 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
5461 static const int kSize = kEndPositionOffset + kPointerSize;
5462
5463 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
5464 kStackFramesOffset + kPointerSize,
5465 kSize> BodyDescriptor;
5466};
5467
5468
Steve Blocka7e24c12009-10-30 11:49:00 +00005469// Regular expressions
5470// The regular expression holds a single reference to a FixedArray in
5471// the kDataOffset field.
5472// The FixedArray contains the following data:
5473// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
5474// - reference to the original source string
5475// - reference to the original flag string
5476// If it is an atom regexp
5477// - a reference to a literal string to search for
5478// If it is an irregexp regexp:
Ben Murdoch257744e2011-11-30 15:57:28 +00005479// - a reference to code for ASCII inputs (bytecode or compiled), or a smi
5480// used for tracking the last usage (used for code flushing).
5481// - a reference to code for UC16 inputs (bytecode or compiled), or a smi
5482// used for tracking the last usage (used for code flushing)..
Steve Blocka7e24c12009-10-30 11:49:00 +00005483// - max number of registers used by irregexp implementations.
5484// - number of capture registers (output values) of the regexp.
5485class JSRegExp: public JSObject {
5486 public:
5487 // Meaning of Type:
5488 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
5489 // ATOM: A simple string to match against using an indexOf operation.
5490 // IRREGEXP: Compiled with Irregexp.
5491 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
5492 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
5493 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
5494
5495 class Flags {
5496 public:
5497 explicit Flags(uint32_t value) : value_(value) { }
5498 bool is_global() { return (value_ & GLOBAL) != 0; }
5499 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
5500 bool is_multiline() { return (value_ & MULTILINE) != 0; }
5501 uint32_t value() { return value_; }
5502 private:
5503 uint32_t value_;
5504 };
5505
5506 DECL_ACCESSORS(data, Object)
5507
5508 inline Type TypeTag();
5509 inline int CaptureCount();
5510 inline Flags GetFlags();
5511 inline String* Pattern();
5512 inline Object* DataAt(int index);
5513 // Set implementation data after the object has been prepared.
5514 inline void SetDataAt(int index, Object* value);
Ben Murdoch257744e2011-11-30 15:57:28 +00005515
5516 // Used during GC when flushing code or setting age.
5517 inline Object* DataAtUnchecked(int index);
5518 inline void SetDataAtUnchecked(int index, Object* value, Heap* heap);
5519 inline Type TypeTagUnchecked();
5520
Steve Blocka7e24c12009-10-30 11:49:00 +00005521 static int code_index(bool is_ascii) {
5522 if (is_ascii) {
5523 return kIrregexpASCIICodeIndex;
5524 } else {
5525 return kIrregexpUC16CodeIndex;
5526 }
5527 }
5528
Ben Murdoch257744e2011-11-30 15:57:28 +00005529 static int saved_code_index(bool is_ascii) {
5530 if (is_ascii) {
5531 return kIrregexpASCIICodeSavedIndex;
5532 } else {
5533 return kIrregexpUC16CodeSavedIndex;
5534 }
5535 }
5536
Steve Blocka7e24c12009-10-30 11:49:00 +00005537 static inline JSRegExp* cast(Object* obj);
5538
5539 // Dispatched behavior.
5540#ifdef DEBUG
5541 void JSRegExpVerify();
5542#endif
5543
5544 static const int kDataOffset = JSObject::kHeaderSize;
5545 static const int kSize = kDataOffset + kPointerSize;
5546
5547 // Indices in the data array.
5548 static const int kTagIndex = 0;
5549 static const int kSourceIndex = kTagIndex + 1;
5550 static const int kFlagsIndex = kSourceIndex + 1;
5551 static const int kDataIndex = kFlagsIndex + 1;
5552 // The data fields are used in different ways depending on the
5553 // value of the tag.
5554 // Atom regexps (literal strings).
5555 static const int kAtomPatternIndex = kDataIndex;
5556
5557 static const int kAtomDataSize = kAtomPatternIndex + 1;
5558
5559 // Irregexp compiled code or bytecode for ASCII. If compilation
5560 // fails, this fields hold an exception object that should be
5561 // thrown if the regexp is used again.
5562 static const int kIrregexpASCIICodeIndex = kDataIndex;
5563 // Irregexp compiled code or bytecode for UC16. If compilation
5564 // fails, this fields hold an exception object that should be
5565 // thrown if the regexp is used again.
5566 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00005567
5568 // Saved instance of Irregexp compiled code or bytecode for ASCII that
5569 // is a potential candidate for flushing.
5570 static const int kIrregexpASCIICodeSavedIndex = kDataIndex + 2;
5571 // Saved instance of Irregexp compiled code or bytecode for UC16 that is
5572 // a potential candidate for flushing.
5573 static const int kIrregexpUC16CodeSavedIndex = kDataIndex + 3;
5574
Steve Blocka7e24c12009-10-30 11:49:00 +00005575 // Maximal number of registers used by either ASCII or UC16.
5576 // Only used to check that there is enough stack space
Ben Murdoch257744e2011-11-30 15:57:28 +00005577 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +00005578 // Number of captures in the compiled regexp.
Ben Murdoch257744e2011-11-30 15:57:28 +00005579 static const int kIrregexpCaptureCountIndex = kDataIndex + 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00005580
5581 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00005582
5583 // Offsets directly into the data fixed array.
5584 static const int kDataTagOffset =
5585 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5586 static const int kDataAsciiCodeOffset =
5587 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00005588 static const int kDataUC16CodeOffset =
5589 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00005590 static const int kIrregexpCaptureCountOffset =
5591 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005592
5593 // In-object fields.
5594 static const int kSourceFieldIndex = 0;
5595 static const int kGlobalFieldIndex = 1;
5596 static const int kIgnoreCaseFieldIndex = 2;
5597 static const int kMultilineFieldIndex = 3;
5598 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01005599 static const int kInObjectFieldCount = 5;
Ben Murdoch257744e2011-11-30 15:57:28 +00005600
5601 // The uninitialized value for a regexp code object.
5602 static const int kUninitializedValue = -1;
5603
5604 // The compilation error value for the regexp code object. The real error
5605 // object is in the saved code field.
5606 static const int kCompilationErrorValue = -2;
5607
5608 // When we store the sweep generation at which we moved the code from the
5609 // code index to the saved code index we mask it of to be in the [0:255]
5610 // range.
5611 static const int kCodeAgeMask = 0xff;
Steve Blocka7e24c12009-10-30 11:49:00 +00005612};
5613
5614
Ben Murdochc7cc0282012-03-05 14:35:55 +00005615class CompilationCacheShape : public BaseShape<HashTableKey*> {
Steve Blocka7e24c12009-10-30 11:49:00 +00005616 public:
5617 static inline bool IsMatch(HashTableKey* key, Object* value) {
5618 return key->IsMatch(value);
5619 }
5620
5621 static inline uint32_t Hash(HashTableKey* key) {
5622 return key->Hash();
5623 }
5624
5625 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5626 return key->HashForObject(object);
5627 }
5628
John Reck59135872010-11-02 12:39:01 -07005629 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005630 return key->AsObject();
5631 }
5632
5633 static const int kPrefixSize = 0;
5634 static const int kEntrySize = 2;
5635};
5636
Steve Block3ce2e202009-11-05 08:53:23 +00005637
Steve Blocka7e24c12009-10-30 11:49:00 +00005638class CompilationCacheTable: public HashTable<CompilationCacheShape,
5639 HashTableKey*> {
5640 public:
5641 // Find cached value for a string key, otherwise return null.
5642 Object* Lookup(String* src);
Ben Murdoch85b71792012-04-11 18:30:58 +01005643 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00005644 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
Ben Murdoch85b71792012-04-11 18:30:58 +01005645 MaybeObject* Put(String* src, Object* value);
5646 MaybeObject* PutEval(String* src,
5647 Context* context,
5648 SharedFunctionInfo* value);
5649 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005650
Ben Murdochb0fe1622011-05-05 13:52:32 +01005651 // Remove given value from cache.
5652 void Remove(Object* value);
5653
Steve Blocka7e24c12009-10-30 11:49:00 +00005654 static inline CompilationCacheTable* cast(Object* obj);
5655
5656 private:
5657 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
5658};
5659
5660
Steve Block6ded16b2010-05-10 14:33:55 +01005661class CodeCache: public Struct {
5662 public:
5663 DECL_ACCESSORS(default_cache, FixedArray)
5664 DECL_ACCESSORS(normal_type_cache, Object)
5665
5666 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07005667 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005668
5669 // Lookup code object in the cache. Returns code object if found and undefined
5670 // if not.
5671 Object* Lookup(String* name, Code::Flags flags);
5672
5673 // Get the internal index of a code object in the cache. Returns -1 if the
5674 // code object is not in that cache. This index can be used to later call
5675 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5676 // RemoveByIndex.
5677 int GetIndex(Object* name, Code* code);
5678
5679 // Remove an object from the cache with the provided internal index.
5680 void RemoveByIndex(Object* name, Code* code, int index);
5681
5682 static inline CodeCache* cast(Object* obj);
5683
Ben Murdochb0fe1622011-05-05 13:52:32 +01005684#ifdef OBJECT_PRINT
5685 inline void CodeCachePrint() {
5686 CodeCachePrint(stdout);
5687 }
5688 void CodeCachePrint(FILE* out);
5689#endif
Steve Block6ded16b2010-05-10 14:33:55 +01005690#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01005691 void CodeCacheVerify();
5692#endif
5693
5694 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5695 static const int kNormalTypeCacheOffset =
5696 kDefaultCacheOffset + kPointerSize;
5697 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5698
5699 private:
John Reck59135872010-11-02 12:39:01 -07005700 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5701 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005702 Object* LookupDefaultCache(String* name, Code::Flags flags);
5703 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5704
5705 // Code cache layout of the default cache. Elements are alternating name and
5706 // code objects for non normal load/store/call IC's.
5707 static const int kCodeCacheEntrySize = 2;
5708 static const int kCodeCacheEntryNameOffset = 0;
5709 static const int kCodeCacheEntryCodeOffset = 1;
5710
5711 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5712};
5713
5714
Ben Murdochc7cc0282012-03-05 14:35:55 +00005715class CodeCacheHashTableShape : public BaseShape<HashTableKey*> {
Steve Block6ded16b2010-05-10 14:33:55 +01005716 public:
5717 static inline bool IsMatch(HashTableKey* key, Object* value) {
5718 return key->IsMatch(value);
5719 }
5720
5721 static inline uint32_t Hash(HashTableKey* key) {
5722 return key->Hash();
5723 }
5724
5725 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5726 return key->HashForObject(object);
5727 }
5728
John Reck59135872010-11-02 12:39:01 -07005729 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01005730 return key->AsObject();
5731 }
5732
5733 static const int kPrefixSize = 0;
5734 static const int kEntrySize = 2;
5735};
5736
5737
5738class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5739 HashTableKey*> {
5740 public:
5741 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005742 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005743
5744 int GetIndex(String* name, Code::Flags flags);
5745 void RemoveByIndex(int index);
5746
5747 static inline CodeCacheHashTable* cast(Object* obj);
5748
5749 // Initial size of the fixed array backing the hash table.
5750 static const int kInitialSize = 64;
5751
5752 private:
5753 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5754};
5755
5756
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005757class PolymorphicCodeCache: public Struct {
5758 public:
5759 DECL_ACCESSORS(cache, Object)
5760
Ben Murdoch85b71792012-04-11 18:30:58 +01005761 MUST_USE_RESULT MaybeObject* Update(MapList* maps,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005762 Code::Flags flags,
5763 Code* code);
Ben Murdoch85b71792012-04-11 18:30:58 +01005764 Object* Lookup(MapList* maps, Code::Flags flags);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005765
5766 static inline PolymorphicCodeCache* cast(Object* obj);
5767
5768#ifdef OBJECT_PRINT
5769 inline void PolymorphicCodeCachePrint() {
5770 PolymorphicCodeCachePrint(stdout);
5771 }
5772 void PolymorphicCodeCachePrint(FILE* out);
5773#endif
5774#ifdef DEBUG
5775 void PolymorphicCodeCacheVerify();
5776#endif
5777
5778 static const int kCacheOffset = HeapObject::kHeaderSize;
5779 static const int kSize = kCacheOffset + kPointerSize;
5780
5781 private:
5782 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCache);
5783};
5784
5785
5786class PolymorphicCodeCacheHashTable
5787 : public HashTable<CodeCacheHashTableShape, HashTableKey*> {
5788 public:
Ben Murdoch85b71792012-04-11 18:30:58 +01005789 Object* Lookup(MapList* maps, int code_kind);
5790 MUST_USE_RESULT MaybeObject* Put(MapList* maps, int code_kind, Code* code);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005791
5792 static inline PolymorphicCodeCacheHashTable* cast(Object* obj);
5793
5794 static const int kInitialSize = 64;
5795 private:
5796 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCacheHashTable);
5797};
5798
5799
Steve Blocka7e24c12009-10-30 11:49:00 +00005800enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5801enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5802
5803
5804class StringHasher {
5805 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +00005806 explicit inline StringHasher(int length, uint32_t seed);
Steve Blocka7e24c12009-10-30 11:49:00 +00005807
5808 // Returns true if the hash of this string can be computed without
5809 // looking at the contents.
5810 inline bool has_trivial_hash();
5811
5812 // Add a character to the hash and update the array index calculation.
Ben Murdoch85b71792012-04-11 18:30:58 +01005813 inline void AddCharacter(uc32 c);
Steve Blocka7e24c12009-10-30 11:49:00 +00005814
5815 // Adds a character to the hash but does not update the array index
5816 // calculation. This can only be called when it has been verified
5817 // that the input is not an array index.
Ben Murdoch85b71792012-04-11 18:30:58 +01005818 inline void AddCharacterNoIndex(uc32 c);
Steve Blocka7e24c12009-10-30 11:49:00 +00005819
5820 // Returns the value to store in the hash field of a string with
5821 // the given length and contents.
5822 uint32_t GetHashField();
5823
5824 // Returns true if the characters seen so far make up a legal array
5825 // index.
5826 bool is_array_index() { return is_array_index_; }
5827
5828 bool is_valid() { return is_valid_; }
5829
5830 void invalidate() { is_valid_ = false; }
5831
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005832 // Calculated hash value for a string consisting of 1 to
5833 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5834 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005835 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005836
Ben Murdochc7cc0282012-03-05 14:35:55 +00005837 // No string is allowed to have a hash of zero. That value is reserved
5838 // for internal properties. If the hash calculation yields zero then we
5839 // use 27 instead.
5840 static const int kZeroHash = 27;
5841
Steve Blocka7e24c12009-10-30 11:49:00 +00005842 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00005843 uint32_t array_index() {
5844 ASSERT(is_array_index());
5845 return array_index_;
5846 }
5847
5848 inline uint32_t GetHash();
5849
5850 int length_;
5851 uint32_t raw_running_hash_;
5852 uint32_t array_index_;
5853 bool is_array_index_;
5854 bool is_first_char_;
5855 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00005856 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00005857};
5858
5859
Steve Block44f0eee2011-05-26 01:26:41 +01005860// Calculates string hash.
5861template <typename schar>
Ben Murdochc7cc0282012-03-05 14:35:55 +00005862inline uint32_t HashSequentialString(const schar* chars,
5863 int length,
5864 uint32_t seed);
Steve Block44f0eee2011-05-26 01:26:41 +01005865
5866
Steve Blocka7e24c12009-10-30 11:49:00 +00005867// The characteristics of a string are stored in its map. Retrieving these
5868// few bits of information is moderately expensive, involving two memory
5869// loads where the second is dependent on the first. To improve efficiency
5870// the shape of the string is given its own class so that it can be retrieved
5871// once and used for several string operations. A StringShape is small enough
5872// to be passed by value and is immutable, but be aware that flattening a
5873// string can potentially alter its shape. Also be aware that a GC caused by
5874// something else can alter the shape of a string due to ConsString
5875// shortcutting. Keeping these restrictions in mind has proven to be error-
5876// prone and so we no longer put StringShapes in variables unless there is a
5877// concrete performance benefit at that particular point in the code.
5878class StringShape BASE_EMBEDDED {
5879 public:
5880 inline explicit StringShape(String* s);
5881 inline explicit StringShape(Map* s);
5882 inline explicit StringShape(InstanceType t);
5883 inline bool IsSequential();
5884 inline bool IsExternal();
5885 inline bool IsCons();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005886 inline bool IsSliced();
5887 inline bool IsIndirect();
Steve Blocka7e24c12009-10-30 11:49:00 +00005888 inline bool IsExternalAscii();
5889 inline bool IsExternalTwoByte();
5890 inline bool IsSequentialAscii();
5891 inline bool IsSequentialTwoByte();
5892 inline bool IsSymbol();
5893 inline StringRepresentationTag representation_tag();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005894 inline uint32_t encoding_tag();
Steve Blocka7e24c12009-10-30 11:49:00 +00005895 inline uint32_t full_representation_tag();
5896 inline uint32_t size_tag();
5897#ifdef DEBUG
5898 inline uint32_t type() { return type_; }
5899 inline void invalidate() { valid_ = false; }
5900 inline bool valid() { return valid_; }
5901#else
5902 inline void invalidate() { }
5903#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005904
Steve Blocka7e24c12009-10-30 11:49:00 +00005905 private:
5906 uint32_t type_;
5907#ifdef DEBUG
5908 inline void set_valid() { valid_ = true; }
5909 bool valid_;
5910#else
5911 inline void set_valid() { }
5912#endif
5913};
5914
5915
5916// The String abstract class captures JavaScript string values:
5917//
5918// Ecma-262:
5919// 4.3.16 String Value
5920// A string value is a member of the type String and is a finite
5921// ordered sequence of zero or more 16-bit unsigned integer values.
5922//
5923// All string values have a length field.
5924class String: public HeapObject {
5925 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005926 // Representation of the flat content of a String.
5927 // A non-flat string doesn't have flat content.
5928 // A flat string has content that's encoded as a sequence of either
5929 // ASCII chars or two-byte UC16.
5930 // Returned by String::GetFlatContent().
5931 class FlatContent {
5932 public:
5933 // Returns true if the string is flat and this structure contains content.
5934 bool IsFlat() { return state_ != NON_FLAT; }
5935 // Returns true if the structure contains ASCII content.
5936 bool IsAscii() { return state_ == ASCII; }
5937 // Returns true if the structure contains two-byte content.
5938 bool IsTwoByte() { return state_ == TWO_BYTE; }
5939
5940 // Return the ASCII content of the string. Only use if IsAscii() returns
5941 // true.
5942 Vector<const char> ToAsciiVector() {
5943 ASSERT_EQ(ASCII, state_);
5944 return Vector<const char>::cast(buffer_);
5945 }
5946 // Return the two-byte content of the string. Only use if IsTwoByte()
5947 // returns true.
5948 Vector<const uc16> ToUC16Vector() {
5949 ASSERT_EQ(TWO_BYTE, state_);
5950 return Vector<const uc16>::cast(buffer_);
5951 }
5952
5953 private:
5954 enum State { NON_FLAT, ASCII, TWO_BYTE };
5955
5956 // Constructors only used by String::GetFlatContent().
5957 explicit FlatContent(Vector<const char> chars)
5958 : buffer_(Vector<const byte>::cast(chars)),
5959 state_(ASCII) { }
5960 explicit FlatContent(Vector<const uc16> chars)
5961 : buffer_(Vector<const byte>::cast(chars)),
5962 state_(TWO_BYTE) { }
5963 FlatContent() : buffer_(), state_(NON_FLAT) { }
5964
5965 Vector<const byte> buffer_;
5966 State state_;
5967
5968 friend class String;
5969 };
5970
Steve Blocka7e24c12009-10-30 11:49:00 +00005971 // Get and set the length of the string.
5972 inline int length();
5973 inline void set_length(int value);
5974
Steve Blockd0582a62009-12-15 09:54:21 +00005975 // Get and set the hash field of the string.
5976 inline uint32_t hash_field();
5977 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005978
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005979 // Returns whether this string has only ASCII chars, i.e. all of them can
5980 // be ASCII encoded. This might be the case even if the string is
5981 // two-byte. Such strings may appear when the embedder prefers
5982 // two-byte external representations even for ASCII data.
Steve Blocka7e24c12009-10-30 11:49:00 +00005983 inline bool IsAsciiRepresentation();
5984 inline bool IsTwoByteRepresentation();
5985
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005986 // Cons and slices have an encoding flag that may not represent the actual
5987 // encoding of the underlying string. This is taken into account here.
5988 // Requires: this->IsFlat()
5989 inline bool IsAsciiRepresentationUnderneath();
5990 inline bool IsTwoByteRepresentationUnderneath();
5991
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005992 // NOTE: this should be considered only a hint. False negatives are
5993 // possible.
5994 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005995
Steve Blocka7e24c12009-10-30 11:49:00 +00005996 // Get and set individual two byte chars in the string.
5997 inline void Set(int index, uint16_t value);
5998 // Get individual two byte char in the string. Repeated calls
5999 // to this method are not efficient unless the string is flat.
6000 inline uint16_t Get(int index);
6001
Leon Clarkef7060e22010-06-03 12:02:55 +01006002 // Try to flatten the string. Checks first inline to see if it is
6003 // necessary. Does nothing if the string is not a cons string.
6004 // Flattening allocates a sequential string with the same data as
6005 // the given string and mutates the cons string to a degenerate
6006 // form, where the first component is the new sequential string and
6007 // the second component is the empty string. If allocation fails,
6008 // this function returns a failure. If flattening succeeds, this
6009 // function returns the sequential string that is now the first
6010 // component of the cons string.
6011 //
6012 // Degenerate cons strings are handled specially by the garbage
6013 // collector (see IsShortcutCandidate).
6014 //
6015 // Use FlattenString from Handles.cc to flatten even in case an
6016 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07006017 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00006018
Leon Clarkef7060e22010-06-03 12:02:55 +01006019 // Convenience function. Has exactly the same behavior as
6020 // TryFlatten(), except in the case of failure returns the original
6021 // string.
6022 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
6023
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006024 // Tries to return the content of a flat string as a structure holding either
6025 // a flat vector of char or of uc16.
6026 // If the string isn't flat, and therefore doesn't have flat content, the
6027 // returned structure will report so, and can't provide a vector of either
6028 // kind.
6029 FlatContent GetFlatContent();
6030
6031 // Returns the parent of a sliced string or first part of a flat cons string.
6032 // Requires: StringShape(this).IsIndirect() && this->IsFlat()
6033 inline String* GetUnderlying();
Steve Blocka7e24c12009-10-30 11:49:00 +00006034
6035 // Mark the string as an undetectable object. It only applies to
Ben Murdoch85b71792012-04-11 18:30:58 +01006036 // ascii and two byte string types.
Steve Blocka7e24c12009-10-30 11:49:00 +00006037 bool MarkAsUndetectable();
6038
Steve Blockd0582a62009-12-15 09:54:21 +00006039 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07006040 MUST_USE_RESULT MaybeObject* SubString(int from,
6041 int to,
6042 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00006043
6044 // String equality operations.
6045 inline bool Equals(String* other);
6046 bool IsEqualTo(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +01006047 bool IsAsciiEqualTo(Vector<const char> str);
6048 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00006049
6050 // Return a UTF8 representation of the string. The string is null
6051 // terminated but may optionally contain nulls. Length is returned
6052 // in length_output if length_output is not a null pointer The string
6053 // should be nearly flat, otherwise the performance of this method may
6054 // be very slow (quadratic in the length). Setting robustness_flag to
6055 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
6056 // handles unexpected data without causing assert failures and it does not
6057 // do any heap allocations. This is useful when printing stack traces.
Ben Murdoch589d6972011-11-30 16:04:58 +00006058 SmartArrayPointer<char> ToCString(AllowNullsFlag allow_nulls,
6059 RobustnessFlag robustness_flag,
6060 int offset,
6061 int length,
6062 int* length_output = 0);
6063 SmartArrayPointer<char> ToCString(
Steve Blocka7e24c12009-10-30 11:49:00 +00006064 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
6065 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
6066 int* length_output = 0);
6067
Ben Murdoch85b71792012-04-11 18:30:58 +01006068 int Utf8Length();
6069
Steve Blocka7e24c12009-10-30 11:49:00 +00006070 // Return a 16 bit Unicode representation of the string.
6071 // The string should be nearly flat, otherwise the performance of
6072 // of this method may be very bad. Setting robustness_flag to
6073 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
6074 // handles unexpected data without causing assert failures and it does not
6075 // do any heap allocations. This is useful when printing stack traces.
Ben Murdoch589d6972011-11-30 16:04:58 +00006076 SmartArrayPointer<uc16> ToWideCString(
Steve Blocka7e24c12009-10-30 11:49:00 +00006077 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
6078
6079 // Tells whether the hash code has been computed.
6080 inline bool HasHashCode();
6081
6082 // Returns a hash value used for the property table
6083 inline uint32_t Hash();
6084
Steve Blockd0582a62009-12-15 09:54:21 +00006085 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
Ben Murdochc7cc0282012-03-05 14:35:55 +00006086 int length,
6087 uint32_t seed);
Steve Blocka7e24c12009-10-30 11:49:00 +00006088
6089 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
6090 uint32_t* index,
6091 int length);
6092
6093 // Externalization.
6094 bool MakeExternal(v8::String::ExternalStringResource* resource);
6095 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
6096
6097 // Conversion.
6098 inline bool AsArrayIndex(uint32_t* index);
6099
6100 // Casting.
6101 static inline String* cast(Object* obj);
6102
6103 void PrintOn(FILE* out);
6104
6105 // For use during stack traces. Performs rudimentary sanity check.
6106 bool LooksValid();
6107
6108 // Dispatched behavior.
6109 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006110#ifdef OBJECT_PRINT
6111 inline void StringPrint() {
6112 StringPrint(stdout);
6113 }
6114 void StringPrint(FILE* out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006115
6116 char* ToAsciiArray();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006117#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006118#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006119 void StringVerify();
6120#endif
6121 inline bool IsFlat();
6122
6123 // Layout description.
6124 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01006125 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006126 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006127
Steve Blockd0582a62009-12-15 09:54:21 +00006128 // Maximum number of characters to consider when trying to convert a string
6129 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00006130 static const int kMaxArrayIndexSize = 10;
6131
Ben Murdoch85b71792012-04-11 18:30:58 +01006132 // Max ascii char code.
Steve Blocka7e24c12009-10-30 11:49:00 +00006133 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
6134 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
Ben Murdoch85b71792012-04-11 18:30:58 +01006135 static const int kMaxUC16CharCode = 0xffff;
6136
6137 // Minimum length for a cons string.
6138 static const int kMinNonFlatLength = 13;
Steve Blocka7e24c12009-10-30 11:49:00 +00006139
Steve Blocka7e24c12009-10-30 11:49:00 +00006140 // Mask constant for checking if a string has a computed hash code
6141 // and if it is an array index. The least significant bit indicates
6142 // whether a hash code has been computed. If the hash code has been
6143 // computed the 2nd bit tells whether the string can be used as an
6144 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006145 static const int kHashNotComputedMask = 1;
6146 static const int kIsNotArrayIndexMask = 1 << 1;
6147 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00006148
Steve Blockd0582a62009-12-15 09:54:21 +00006149 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006150 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00006151
Ben Murdochc7cc0282012-03-05 14:35:55 +00006152 // Only these bits are relevant in the hash, since the top two are shifted
6153 // out.
6154 static const uint32_t kHashBitMask = 0xffffffffu >> kHashShift;
6155
Steve Blocka7e24c12009-10-30 11:49:00 +00006156 // Array index strings this short can keep their index in the hash
6157 // field.
6158 static const int kMaxCachedArrayIndexLength = 7;
6159
Steve Blockd0582a62009-12-15 09:54:21 +00006160 // For strings which are array indexes the hash value has the string length
6161 // mixed into the hash, mainly to avoid a hash value of zero which would be
6162 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006163 static const int kArrayIndexValueBits = 24;
6164 static const int kArrayIndexLengthBits =
6165 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
6166
6167 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01006168 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006169
6170 static const int kArrayIndexHashLengthShift =
6171 kArrayIndexValueBits + kNofHashBitFields;
6172
Steve Blockd0582a62009-12-15 09:54:21 +00006173 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006174
6175 static const int kArrayIndexValueMask =
6176 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
6177
6178 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
6179 // could use a mask to test if the length of string is less than or equal to
6180 // kMaxCachedArrayIndexLength.
6181 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
6182
6183 static const int kContainsCachedArrayIndexMask =
6184 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
6185 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00006186
6187 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006188 static const int kEmptyHashField =
6189 kIsNotArrayIndexMask | kHashNotComputedMask;
6190
6191 // Value of hash field containing computed hash equal to zero.
6192 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00006193
6194 // Maximal string length.
6195 static const int kMaxLength = (1 << (32 - 2)) - 1;
6196
6197 // Max length for computing hash. For strings longer than this limit the
6198 // string length is used as the hash value.
6199 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00006200
6201 // Limit for truncation in short printing.
6202 static const int kMaxShortPrintLength = 1024;
6203
6204 // Support for regular expressions.
6205 const uc16* GetTwoByteData();
6206 const uc16* GetTwoByteData(unsigned start);
6207
6208 // Support for StringInputBuffer
6209 static const unibrow::byte* ReadBlock(String* input,
6210 unibrow::byte* util_buffer,
6211 unsigned capacity,
6212 unsigned* remaining,
6213 unsigned* offset);
6214 static const unibrow::byte* ReadBlock(String** input,
6215 unibrow::byte* util_buffer,
6216 unsigned capacity,
6217 unsigned* remaining,
6218 unsigned* offset);
6219
6220 // Helper function for flattening strings.
6221 template <typename sinkchar>
6222 static void WriteToFlat(String* source,
6223 sinkchar* sink,
6224 int from,
6225 int to);
6226
Steve Block9fac8402011-05-12 15:51:54 +01006227 static inline bool IsAscii(const char* chars, int length) {
6228 const char* limit = chars + length;
6229#ifdef V8_HOST_CAN_READ_UNALIGNED
6230 ASSERT(kMaxAsciiCharCode == 0x7F);
6231 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
6232 while (chars <= limit - sizeof(uintptr_t)) {
6233 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
6234 return false;
6235 }
6236 chars += sizeof(uintptr_t);
6237 }
6238#endif
6239 while (chars < limit) {
6240 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
6241 ++chars;
6242 }
6243 return true;
6244 }
6245
6246 static inline bool IsAscii(const uc16* chars, int length) {
6247 const uc16* limit = chars + length;
6248 while (chars < limit) {
6249 if (*chars > kMaxAsciiCharCodeU) return false;
6250 ++chars;
6251 }
6252 return true;
6253 }
6254
Steve Blocka7e24c12009-10-30 11:49:00 +00006255 protected:
6256 class ReadBlockBuffer {
6257 public:
6258 ReadBlockBuffer(unibrow::byte* util_buffer_,
6259 unsigned cursor_,
6260 unsigned capacity_,
6261 unsigned remaining_) :
6262 util_buffer(util_buffer_),
6263 cursor(cursor_),
6264 capacity(capacity_),
6265 remaining(remaining_) {
6266 }
6267 unibrow::byte* util_buffer;
6268 unsigned cursor;
6269 unsigned capacity;
6270 unsigned remaining;
6271 };
6272
Steve Blocka7e24c12009-10-30 11:49:00 +00006273 static inline const unibrow::byte* ReadBlock(String* input,
6274 ReadBlockBuffer* buffer,
6275 unsigned* offset,
6276 unsigned max_chars);
6277 static void ReadBlockIntoBuffer(String* input,
6278 ReadBlockBuffer* buffer,
6279 unsigned* offset_ptr,
6280 unsigned max_chars);
6281
6282 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01006283 // Try to flatten the top level ConsString that is hiding behind this
6284 // string. This is a no-op unless the string is a ConsString. Flatten
6285 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07006286 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01006287
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006288 static inline bool IsHashFieldComputed(uint32_t field);
6289
Steve Blocka7e24c12009-10-30 11:49:00 +00006290 // Slow case of String::Equals. This implementation works on any strings
6291 // but it is most efficient on strings that are almost flat.
6292 bool SlowEquals(String* other);
6293
6294 // Slow case of AsArrayIndex.
6295 bool SlowAsArrayIndex(uint32_t* index);
6296
6297 // Compute and set the hash code.
6298 uint32_t ComputeAndSetHash();
6299
6300 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
6301};
6302
6303
6304// The SeqString abstract class captures sequential string values.
6305class SeqString: public String {
6306 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00006307 // Casting.
6308 static inline SeqString* cast(Object* obj);
6309
Steve Blocka7e24c12009-10-30 11:49:00 +00006310 private:
6311 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
6312};
6313
6314
Ben Murdoch85b71792012-04-11 18:30:58 +01006315// The AsciiString class captures sequential ascii string objects.
6316// Each character in the AsciiString is an ascii character.
Steve Blocka7e24c12009-10-30 11:49:00 +00006317class SeqAsciiString: public SeqString {
6318 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006319 static const bool kHasAsciiEncoding = true;
6320
Steve Blocka7e24c12009-10-30 11:49:00 +00006321 // Dispatched behavior.
6322 inline uint16_t SeqAsciiStringGet(int index);
6323 inline void SeqAsciiStringSet(int index, uint16_t value);
6324
6325 // Get the address of the characters in this string.
6326 inline Address GetCharsAddress();
6327
6328 inline char* GetChars();
6329
6330 // Casting
6331 static inline SeqAsciiString* cast(Object* obj);
6332
6333 // Garbage collection support. This method is called by the
6334 // garbage collector to compute the actual size of an AsciiString
6335 // instance.
6336 inline int SeqAsciiStringSize(InstanceType instance_type);
6337
6338 // Computes the size for an AsciiString instance of a given length.
6339 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006340 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00006341 }
6342
Ben Murdoch85b71792012-04-11 18:30:58 +01006343 // Layout description.
6344 static const int kHeaderSize = String::kSize;
6345 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
6346
Leon Clarkee46be812010-01-19 14:06:41 +00006347 // Maximal memory usage for a single sequential ASCII string.
Ben Murdoch85b71792012-04-11 18:30:58 +01006348 static const int kMaxSize = 512 * MB;
Leon Clarkee46be812010-01-19 14:06:41 +00006349 // Maximal length of a single sequential ASCII string.
6350 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6351 static const int kMaxLength = (kMaxSize - kHeaderSize);
6352
Steve Blocka7e24c12009-10-30 11:49:00 +00006353 // Support for StringInputBuffer.
6354 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6355 unsigned* offset,
6356 unsigned chars);
6357 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
6358 unsigned* offset,
6359 unsigned chars);
6360
6361 private:
6362 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
6363};
6364
6365
6366// The TwoByteString class captures sequential unicode string objects.
6367// Each character in the TwoByteString is a two-byte uint16_t.
6368class SeqTwoByteString: public SeqString {
6369 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006370 static const bool kHasAsciiEncoding = false;
6371
Steve Blocka7e24c12009-10-30 11:49:00 +00006372 // Dispatched behavior.
6373 inline uint16_t SeqTwoByteStringGet(int index);
6374 inline void SeqTwoByteStringSet(int index, uint16_t value);
6375
6376 // Get the address of the characters in this string.
6377 inline Address GetCharsAddress();
6378
6379 inline uc16* GetChars();
6380
6381 // For regexp code.
6382 const uint16_t* SeqTwoByteStringGetData(unsigned start);
6383
6384 // Casting
6385 static inline SeqTwoByteString* cast(Object* obj);
6386
6387 // Garbage collection support. This method is called by the
6388 // garbage collector to compute the actual size of a TwoByteString
6389 // instance.
6390 inline int SeqTwoByteStringSize(InstanceType instance_type);
6391
6392 // Computes the size for a TwoByteString instance of a given length.
6393 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006394 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00006395 }
6396
Ben Murdoch85b71792012-04-11 18:30:58 +01006397 // Layout description.
6398 static const int kHeaderSize = String::kSize;
6399 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
6400
Leon Clarkee46be812010-01-19 14:06:41 +00006401 // Maximal memory usage for a single sequential two-byte string.
Ben Murdoch85b71792012-04-11 18:30:58 +01006402 static const int kMaxSize = 512 * MB;
Leon Clarkee46be812010-01-19 14:06:41 +00006403 // Maximal length of a single sequential two-byte string.
6404 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6405 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
6406
Steve Blocka7e24c12009-10-30 11:49:00 +00006407 // Support for StringInputBuffer.
6408 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6409 unsigned* offset_ptr,
6410 unsigned chars);
6411
6412 private:
6413 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
6414};
6415
6416
6417// The ConsString class describes string values built by using the
6418// addition operator on strings. A ConsString is a pair where the
6419// first and second components are pointers to other string values.
6420// One or both components of a ConsString can be pointers to other
6421// ConsStrings, creating a binary tree of ConsStrings where the leaves
6422// are non-ConsString string values. The string value represented by
6423// a ConsString can be obtained by concatenating the leaf string
6424// values in a left-to-right depth-first traversal of the tree.
6425class ConsString: public String {
6426 public:
6427 // First string of the cons cell.
6428 inline String* first();
6429 // Doesn't check that the result is a string, even in debug mode. This is
6430 // useful during GC where the mark bits confuse the checks.
6431 inline Object* unchecked_first();
6432 inline void set_first(String* first,
6433 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
6434
6435 // Second string of the cons cell.
6436 inline String* second();
6437 // Doesn't check that the result is a string, even in debug mode. This is
6438 // useful during GC where the mark bits confuse the checks.
6439 inline Object* unchecked_second();
6440 inline void set_second(String* second,
6441 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
6442
6443 // Dispatched behavior.
6444 uint16_t ConsStringGet(int index);
6445
6446 // Casting.
6447 static inline ConsString* cast(Object* obj);
6448
Steve Blocka7e24c12009-10-30 11:49:00 +00006449 // Layout description.
6450 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
6451 static const int kSecondOffset = kFirstOffset + kPointerSize;
6452 static const int kSize = kSecondOffset + kPointerSize;
6453
6454 // Support for StringInputBuffer.
6455 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
6456 unsigned* offset_ptr,
6457 unsigned chars);
6458 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6459 unsigned* offset_ptr,
6460 unsigned chars);
6461
6462 // Minimum length for a cons string.
6463 static const int kMinLength = 13;
6464
Iain Merrick75681382010-08-19 15:07:18 +01006465 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
6466 BodyDescriptor;
6467
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006468#ifdef DEBUG
6469 void ConsStringVerify();
6470#endif
6471
Steve Blocka7e24c12009-10-30 11:49:00 +00006472 private:
6473 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
6474};
6475
6476
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006477// The Sliced String class describes strings that are substrings of another
6478// sequential string. The motivation is to save time and memory when creating
6479// a substring. A Sliced String is described as a pointer to the parent,
6480// the offset from the start of the parent string and the length. Using
6481// a Sliced String therefore requires unpacking of the parent string and
6482// adding the offset to the start address. A substring of a Sliced String
6483// are not nested since the double indirection is simplified when creating
6484// such a substring.
6485// Currently missing features are:
6486// - handling externalized parent strings
6487// - external strings as parent
6488// - truncating sliced string to enable otherwise unneeded parent to be GC'ed.
6489class SlicedString: public String {
6490 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006491 inline String* parent();
6492 inline void set_parent(String* parent);
6493 inline int offset();
6494 inline void set_offset(int offset);
6495
6496 // Dispatched behavior.
6497 uint16_t SlicedStringGet(int index);
6498
6499 // Casting.
6500 static inline SlicedString* cast(Object* obj);
6501
6502 // Layout description.
6503 static const int kParentOffset = POINTER_SIZE_ALIGN(String::kSize);
6504 static const int kOffsetOffset = kParentOffset + kPointerSize;
6505 static const int kSize = kOffsetOffset + kPointerSize;
6506
6507 // Support for StringInputBuffer
6508 inline const unibrow::byte* SlicedStringReadBlock(ReadBlockBuffer* buffer,
6509 unsigned* offset_ptr,
6510 unsigned chars);
6511 inline void SlicedStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6512 unsigned* offset_ptr,
6513 unsigned chars);
6514 // Minimum length for a sliced string.
6515 static const int kMinLength = 13;
6516
6517 typedef FixedBodyDescriptor<kParentOffset,
6518 kOffsetOffset + kPointerSize, kSize>
6519 BodyDescriptor;
6520
6521#ifdef DEBUG
6522 void SlicedStringVerify();
6523#endif
6524
6525 private:
6526 DISALLOW_IMPLICIT_CONSTRUCTORS(SlicedString);
6527};
6528
6529
Steve Blocka7e24c12009-10-30 11:49:00 +00006530// The ExternalString class describes string values that are backed by
6531// a string resource that lies outside the V8 heap. ExternalStrings
6532// consist of the length field common to all strings, a pointer to the
6533// external resource. It is important to ensure (externally) that the
6534// resource is not deallocated while the ExternalString is live in the
6535// V8 heap.
6536//
6537// The API expects that all ExternalStrings are created through the
6538// API. Therefore, ExternalStrings should not be used internally.
6539class ExternalString: public String {
6540 public:
6541 // Casting
6542 static inline ExternalString* cast(Object* obj);
6543
6544 // Layout description.
6545 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
Ben Murdoch85b71792012-04-11 18:30:58 +01006546 static const int kSize = kResourceOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006547
6548 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
6549
6550 private:
6551 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
6552};
6553
6554
6555// The ExternalAsciiString class is an external string backed by an
6556// ASCII string.
6557class ExternalAsciiString: public ExternalString {
6558 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006559 static const bool kHasAsciiEncoding = true;
6560
Steve Blocka7e24c12009-10-30 11:49:00 +00006561 typedef v8::String::ExternalAsciiStringResource Resource;
6562
6563 // The underlying resource.
Ben Murdoch85b71792012-04-11 18:30:58 +01006564 inline Resource* resource();
6565 inline void set_resource(Resource* buffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00006566
6567 // Dispatched behavior.
Ben Murdoch85b71792012-04-11 18:30:58 +01006568 uint16_t ExternalAsciiStringGet(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006569
6570 // Casting.
6571 static inline ExternalAsciiString* cast(Object* obj);
6572
Steve Blockd0582a62009-12-15 09:54:21 +00006573 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01006574 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
6575
6576 template<typename StaticVisitor>
6577 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00006578
Steve Blocka7e24c12009-10-30 11:49:00 +00006579 // Support for StringInputBuffer.
6580 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
6581 unsigned* offset,
6582 unsigned chars);
6583 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6584 unsigned* offset,
6585 unsigned chars);
6586
Steve Blocka7e24c12009-10-30 11:49:00 +00006587 private:
6588 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
6589};
6590
6591
6592// The ExternalTwoByteString class is an external string backed by a UTF-16
6593// encoded string.
6594class ExternalTwoByteString: public ExternalString {
6595 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006596 static const bool kHasAsciiEncoding = false;
6597
Steve Blocka7e24c12009-10-30 11:49:00 +00006598 typedef v8::String::ExternalStringResource Resource;
6599
6600 // The underlying string resource.
Ben Murdoch85b71792012-04-11 18:30:58 +01006601 inline Resource* resource();
6602 inline void set_resource(Resource* buffer);
Steve Blocka7e24c12009-10-30 11:49:00 +00006603
6604 // Dispatched behavior.
Ben Murdoch85b71792012-04-11 18:30:58 +01006605 uint16_t ExternalTwoByteStringGet(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006606
6607 // For regexp code.
Ben Murdoch85b71792012-04-11 18:30:58 +01006608 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
Steve Blocka7e24c12009-10-30 11:49:00 +00006609
6610 // Casting.
6611 static inline ExternalTwoByteString* cast(Object* obj);
6612
Steve Blockd0582a62009-12-15 09:54:21 +00006613 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01006614 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
6615
6616 template<typename StaticVisitor>
6617 inline void ExternalTwoByteStringIterateBody();
6618
Steve Blockd0582a62009-12-15 09:54:21 +00006619
Steve Blocka7e24c12009-10-30 11:49:00 +00006620 // Support for StringInputBuffer.
6621 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6622 unsigned* offset_ptr,
6623 unsigned chars);
6624
Steve Blocka7e24c12009-10-30 11:49:00 +00006625 private:
6626 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
6627};
6628
6629
6630// Utility superclass for stack-allocated objects that must be updated
6631// on gc. It provides two ways for the gc to update instances, either
6632// iterating or updating after gc.
6633class Relocatable BASE_EMBEDDED {
6634 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006635 explicit inline Relocatable(Isolate* isolate);
6636 inline virtual ~Relocatable();
Steve Blocka7e24c12009-10-30 11:49:00 +00006637 virtual void IterateInstance(ObjectVisitor* v) { }
6638 virtual void PostGarbageCollection() { }
6639
6640 static void PostGarbageCollectionProcessing();
6641 static int ArchiveSpacePerThread();
Ben Murdoch257744e2011-11-30 15:57:28 +00006642 static char* ArchiveState(Isolate* isolate, char* to);
6643 static char* RestoreState(Isolate* isolate, char* from);
Steve Blocka7e24c12009-10-30 11:49:00 +00006644 static void Iterate(ObjectVisitor* v);
6645 static void Iterate(ObjectVisitor* v, Relocatable* top);
6646 static char* Iterate(ObjectVisitor* v, char* t);
6647 private:
Steve Block44f0eee2011-05-26 01:26:41 +01006648 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006649 Relocatable* prev_;
6650};
6651
6652
6653// A flat string reader provides random access to the contents of a
6654// string independent of the character width of the string. The handle
6655// must be valid as long as the reader is being used.
6656class FlatStringReader : public Relocatable {
6657 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006658 FlatStringReader(Isolate* isolate, Handle<String> str);
6659 FlatStringReader(Isolate* isolate, Vector<const char> input);
Steve Blocka7e24c12009-10-30 11:49:00 +00006660 void PostGarbageCollection();
6661 inline uc32 Get(int index);
6662 int length() { return length_; }
6663 private:
6664 String** str_;
6665 bool is_ascii_;
6666 int length_;
6667 const void* start_;
6668};
6669
6670
6671// Note that StringInputBuffers are not valid across a GC! To fix this
6672// it would have to store a String Handle instead of a String* and
6673// AsciiStringReadBlock would have to be modified to use memcpy.
6674//
6675// StringInputBuffer is able to traverse any string regardless of how
6676// deeply nested a sequence of ConsStrings it is made of. However,
6677// performance will be better if deep strings are flattened before they
6678// are traversed. Since flattening requires memory allocation this is
6679// not always desirable, however (esp. in debugging situations).
6680class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
6681 public:
6682 virtual void Seek(unsigned pos);
6683 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006684 explicit inline StringInputBuffer(String* backing):
Steve Blocka7e24c12009-10-30 11:49:00 +00006685 unibrow::InputBuffer<String, String*, 1024>(backing) {}
6686};
6687
6688
6689class SafeStringInputBuffer
6690 : public unibrow::InputBuffer<String, String**, 256> {
6691 public:
6692 virtual void Seek(unsigned pos);
6693 inline SafeStringInputBuffer()
6694 : unibrow::InputBuffer<String, String**, 256>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006695 explicit inline SafeStringInputBuffer(String** backing)
Steve Blocka7e24c12009-10-30 11:49:00 +00006696 : unibrow::InputBuffer<String, String**, 256>(backing) {}
6697};
6698
6699
6700template <typename T>
6701class VectorIterator {
6702 public:
6703 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
6704 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
6705 T GetNext() { return data_[index_++]; }
6706 bool has_more() { return index_ < data_.length(); }
6707 private:
6708 Vector<const T> data_;
6709 int index_;
6710};
6711
6712
6713// The Oddball describes objects null, undefined, true, and false.
6714class Oddball: public HeapObject {
6715 public:
6716 // [to_string]: Cached to_string computed at startup.
6717 DECL_ACCESSORS(to_string, String)
6718
6719 // [to_number]: Cached to_number computed at startup.
6720 DECL_ACCESSORS(to_number, Object)
6721
Steve Block44f0eee2011-05-26 01:26:41 +01006722 inline byte kind();
6723 inline void set_kind(byte kind);
6724
Steve Blocka7e24c12009-10-30 11:49:00 +00006725 // Casting.
6726 static inline Oddball* cast(Object* obj);
6727
6728 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00006729#ifdef DEBUG
6730 void OddballVerify();
6731#endif
6732
6733 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07006734 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
Steve Block44f0eee2011-05-26 01:26:41 +01006735 Object* to_number,
6736 byte kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00006737
6738 // Layout description.
6739 static const int kToStringOffset = HeapObject::kHeaderSize;
6740 static const int kToNumberOffset = kToStringOffset + kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +01006741 static const int kKindOffset = kToNumberOffset + kPointerSize;
6742 static const int kSize = kKindOffset + kPointerSize;
6743
6744 static const byte kFalse = 0;
6745 static const byte kTrue = 1;
6746 static const byte kNotBooleanMask = ~1;
6747 static const byte kTheHole = 2;
6748 static const byte kNull = 3;
6749 static const byte kArgumentMarker = 4;
6750 static const byte kUndefined = 5;
6751 static const byte kOther = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +00006752
Iain Merrick75681382010-08-19 15:07:18 +01006753 typedef FixedBodyDescriptor<kToStringOffset,
6754 kToNumberOffset + kPointerSize,
6755 kSize> BodyDescriptor;
6756
Steve Blocka7e24c12009-10-30 11:49:00 +00006757 private:
6758 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6759};
6760
6761
6762class JSGlobalPropertyCell: public HeapObject {
6763 public:
6764 // [value]: value of the global property.
6765 DECL_ACCESSORS(value, Object)
6766
6767 // Casting.
6768 static inline JSGlobalPropertyCell* cast(Object* obj);
6769
Steve Blocka7e24c12009-10-30 11:49:00 +00006770#ifdef DEBUG
6771 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006772#endif
6773#ifdef OBJECT_PRINT
6774 inline void JSGlobalPropertyCellPrint() {
6775 JSGlobalPropertyCellPrint(stdout);
6776 }
6777 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00006778#endif
6779
6780 // Layout description.
6781 static const int kValueOffset = HeapObject::kHeaderSize;
6782 static const int kSize = kValueOffset + kPointerSize;
6783
Iain Merrick75681382010-08-19 15:07:18 +01006784 typedef FixedBodyDescriptor<kValueOffset,
6785 kValueOffset + kPointerSize,
6786 kSize> BodyDescriptor;
6787
Ben Murdoch85b71792012-04-11 18:30:58 +01006788 // Returns the isolate/heap this cell object belongs to.
6789 inline Isolate* isolate();
6790 inline Heap* heap();
6791
Steve Blocka7e24c12009-10-30 11:49:00 +00006792 private:
6793 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6794};
6795
6796
Ben Murdoch257744e2011-11-30 15:57:28 +00006797// The JSProxy describes EcmaScript Harmony proxies
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006798class JSProxy: public JSReceiver {
Steve Blocka7e24c12009-10-30 11:49:00 +00006799 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006800 // [handler]: The handler property.
6801 DECL_ACCESSORS(handler, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00006802
6803 // Casting.
Ben Murdoch257744e2011-11-30 15:57:28 +00006804 static inline JSProxy* cast(Object* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00006805
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006806 bool HasPropertyWithHandler(String* name);
6807
6808 MUST_USE_RESULT MaybeObject* SetPropertyWithHandler(
6809 String* name,
6810 Object* value,
6811 PropertyAttributes attributes,
6812 StrictModeFlag strict_mode);
6813
6814 MUST_USE_RESULT MaybeObject* DeletePropertyWithHandler(
6815 String* name,
6816 DeleteMode mode);
6817
6818 MUST_USE_RESULT PropertyAttributes GetPropertyAttributeWithHandler(
6819 JSReceiver* receiver,
Ben Murdoch85b71792012-04-11 18:30:58 +01006820 String* name,
6821 bool* has_exception);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006822
6823 // Turn this into an (empty) JSObject.
6824 void Fix();
6825
Ben Murdoch589d6972011-11-30 16:04:58 +00006826 // Initializes the body after the handler slot.
6827 inline void InitializeBody(int object_size, Object* value);
6828
Steve Blocka7e24c12009-10-30 11:49:00 +00006829 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006830#ifdef OBJECT_PRINT
Ben Murdoch257744e2011-11-30 15:57:28 +00006831 inline void JSProxyPrint() {
6832 JSProxyPrint(stdout);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006833 }
Ben Murdoch257744e2011-11-30 15:57:28 +00006834 void JSProxyPrint(FILE* out);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006835#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006836#ifdef DEBUG
Ben Murdoch257744e2011-11-30 15:57:28 +00006837 void JSProxyVerify();
6838#endif
6839
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006840 // Layout description. We add padding so that a proxy has the same
6841 // size as a virgin JSObject. This is essential for becoming a JSObject
6842 // upon freeze.
Ben Murdoch257744e2011-11-30 15:57:28 +00006843 static const int kHandlerOffset = HeapObject::kHeaderSize;
Ben Murdoch85b71792012-04-11 18:30:58 +01006844 static const int kPaddingOffset = kHandlerOffset + kPointerSize;
Ben Murdoch589d6972011-11-30 16:04:58 +00006845 static const int kSize = JSObject::kHeaderSize;
6846 static const int kHeaderSize = kPaddingOffset;
6847 static const int kPaddingSize = kSize - kPaddingOffset;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006848
Ben Murdoch589d6972011-11-30 16:04:58 +00006849 STATIC_CHECK(kPaddingSize >= 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00006850
6851 typedef FixedBodyDescriptor<kHandlerOffset,
Ben Murdoch85b71792012-04-11 18:30:58 +01006852 kHandlerOffset + kPointerSize,
Ben Murdoch257744e2011-11-30 15:57:28 +00006853 kSize> BodyDescriptor;
6854
6855 private:
6856 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxy);
6857};
6858
6859
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006860class JSFunctionProxy: public JSProxy {
6861 public:
Ben Murdoch589d6972011-11-30 16:04:58 +00006862 // [call_trap]: The call trap.
6863 DECL_ACCESSORS(call_trap, Object)
6864
6865 // [construct_trap]: The construct trap.
6866 DECL_ACCESSORS(construct_trap, Object)
6867
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006868 // Casting.
6869 static inline JSFunctionProxy* cast(Object* obj);
6870
Ben Murdoch589d6972011-11-30 16:04:58 +00006871 // Dispatched behavior.
6872#ifdef OBJECT_PRINT
6873 inline void JSFunctionProxyPrint() {
6874 JSFunctionProxyPrint(stdout);
6875 }
6876 void JSFunctionProxyPrint(FILE* out);
6877#endif
6878#ifdef DEBUG
6879 void JSFunctionProxyVerify();
6880#endif
6881
6882 // Layout description.
Ben Murdoch85b71792012-04-11 18:30:58 +01006883 static const int kCallTrapOffset = kHandlerOffset + kPointerSize;
Ben Murdoch589d6972011-11-30 16:04:58 +00006884 static const int kConstructTrapOffset = kCallTrapOffset + kPointerSize;
6885 static const int kPaddingOffset = kConstructTrapOffset + kPointerSize;
6886 static const int kSize = JSFunction::kSize;
6887 static const int kPaddingSize = kSize - kPaddingOffset;
6888
6889 STATIC_CHECK(kPaddingSize >= 0);
6890
6891 typedef FixedBodyDescriptor<kHandlerOffset,
6892 kConstructTrapOffset + kPointerSize,
6893 kSize> BodyDescriptor;
6894
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006895 private:
6896 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunctionProxy);
6897};
6898
Ben Murdoch257744e2011-11-30 15:57:28 +00006899
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006900// The JSWeakMap describes EcmaScript Harmony weak maps
6901class JSWeakMap: public JSObject {
6902 public:
6903 // [table]: the backing hash table mapping keys to values.
Ben Murdoch85b71792012-04-11 18:30:58 +01006904 DECL_ACCESSORS(table, ObjectHashTable)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006905
6906 // [next]: linked list of encountered weak maps during GC.
6907 DECL_ACCESSORS(next, Object)
6908
Ben Murdoch85b71792012-04-11 18:30:58 +01006909 // Unchecked accessors to be used during GC.
6910 inline ObjectHashTable* unchecked_table();
6911
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006912 // Casting.
6913 static inline JSWeakMap* cast(Object* obj);
6914
6915#ifdef OBJECT_PRINT
6916 inline void JSWeakMapPrint() {
6917 JSWeakMapPrint(stdout);
6918 }
6919 void JSWeakMapPrint(FILE* out);
6920#endif
6921#ifdef DEBUG
6922 void JSWeakMapVerify();
6923#endif
6924
6925 static const int kTableOffset = JSObject::kHeaderSize;
6926 static const int kNextOffset = kTableOffset + kPointerSize;
6927 static const int kSize = kNextOffset + kPointerSize;
6928
6929 private:
6930 DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakMap);
6931};
6932
6933
Ben Murdoch257744e2011-11-30 15:57:28 +00006934// Foreign describes objects pointing from JavaScript to C structures.
6935// Since they cannot contain references to JS HeapObjects they can be
6936// placed in old_data_space.
6937class Foreign: public HeapObject {
6938 public:
6939 // [address]: field containing the address.
Ben Murdoch85b71792012-04-11 18:30:58 +01006940 inline Address address();
6941 inline void set_address(Address value);
Ben Murdoch257744e2011-11-30 15:57:28 +00006942
6943 // Casting.
6944 static inline Foreign* cast(Object* obj);
6945
6946 // Dispatched behavior.
6947 inline void ForeignIterateBody(ObjectVisitor* v);
6948
6949 template<typename StaticVisitor>
6950 inline void ForeignIterateBody();
6951
6952#ifdef OBJECT_PRINT
6953 inline void ForeignPrint() {
6954 ForeignPrint(stdout);
6955 }
6956 void ForeignPrint(FILE* out);
6957#endif
6958#ifdef DEBUG
6959 void ForeignVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00006960#endif
6961
6962 // Layout description.
6963
Ben Murdoch85b71792012-04-11 18:30:58 +01006964 static const int kAddressOffset = HeapObject::kHeaderSize;
6965 static const int kSize = kAddressOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006966
Ben Murdoch85b71792012-04-11 18:30:58 +01006967 STATIC_CHECK(kAddressOffset == Internals::kForeignAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00006968
6969 private:
Ben Murdoch257744e2011-11-30 15:57:28 +00006970 DISALLOW_IMPLICIT_CONSTRUCTORS(Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +00006971};
6972
6973
6974// The JSArray describes JavaScript Arrays
6975// Such an array can be in one of two modes:
6976// - fast, backing storage is a FixedArray and length <= elements.length();
6977// Please note: push and pop can be used to grow and shrink the array.
6978// - slow, backing storage is a HashTable with numbers as keys.
6979class JSArray: public JSObject {
6980 public:
6981 // [length]: The length property.
6982 DECL_ACCESSORS(length, Object)
6983
Leon Clarke4515c472010-02-03 11:58:03 +00006984 // Overload the length setter to skip write barrier when the length
6985 // is set to a smi. This matches the set function on FixedArray.
6986 inline void set_length(Smi* length);
6987
John Reck59135872010-11-02 12:39:01 -07006988 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6989 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006990
6991 // Initialize the array with the given capacity. The function may
6992 // fail due to out-of-memory situations, but only if the requested
6993 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07006994 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00006995
6996 // Set the content of the array to the content of storage.
Ben Murdoch85b71792012-04-11 18:30:58 +01006997 inline void SetContent(FixedArray* storage);
Steve Blocka7e24c12009-10-30 11:49:00 +00006998
6999 // Casting.
7000 static inline JSArray* cast(Object* obj);
7001
7002 // Uses handles. Ensures that the fixed array backing the JSArray has at
7003 // least the stated size.
7004 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
7005
7006 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01007007#ifdef OBJECT_PRINT
7008 inline void JSArrayPrint() {
7009 JSArrayPrint(stdout);
7010 }
7011 void JSArrayPrint(FILE* out);
7012#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007013#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007014 void JSArrayVerify();
7015#endif
7016
7017 // Number of element slots to pre-allocate for an empty array.
7018 static const int kPreallocatedArrayElements = 4;
7019
7020 // Layout description.
7021 static const int kLengthOffset = JSObject::kHeaderSize;
7022 static const int kSize = kLengthOffset + kPointerSize;
7023
7024 private:
7025 // Expand the fixed array backing of a fast-case JSArray to at least
7026 // the requested size.
7027 void Expand(int minimum_size_of_backing_fixed_array);
7028
7029 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
7030};
7031
7032
Steve Block6ded16b2010-05-10 14:33:55 +01007033// JSRegExpResult is just a JSArray with a specific initial map.
7034// This initial map adds in-object properties for "index" and "input"
7035// properties, as assigned by RegExp.prototype.exec, which allows
7036// faster creation of RegExp exec results.
7037// This class just holds constants used when creating the result.
7038// After creation the result must be treated as a JSArray in all regards.
7039class JSRegExpResult: public JSArray {
7040 public:
7041 // Offsets of object fields.
7042 static const int kIndexOffset = JSArray::kSize;
7043 static const int kInputOffset = kIndexOffset + kPointerSize;
7044 static const int kSize = kInputOffset + kPointerSize;
7045 // Indices of in-object properties.
7046 static const int kIndexIndex = 0;
7047 static const int kInputIndex = 1;
7048 private:
7049 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
7050};
7051
7052
Steve Blocka7e24c12009-10-30 11:49:00 +00007053// An accessor must have a getter, but can have no setter.
7054//
7055// When setting a property, V8 searches accessors in prototypes.
7056// If an accessor was found and it does not have a setter,
7057// the request is ignored.
7058//
7059// If the accessor in the prototype has the READ_ONLY property attribute, then
7060// a new value is added to the local object when the property is set.
7061// This shadows the accessor in the prototype.
7062class AccessorInfo: public Struct {
7063 public:
7064 DECL_ACCESSORS(getter, Object)
7065 DECL_ACCESSORS(setter, Object)
7066 DECL_ACCESSORS(data, Object)
7067 DECL_ACCESSORS(name, Object)
7068 DECL_ACCESSORS(flag, Smi)
7069
7070 inline bool all_can_read();
7071 inline void set_all_can_read(bool value);
7072
7073 inline bool all_can_write();
7074 inline void set_all_can_write(bool value);
7075
7076 inline bool prohibits_overwriting();
7077 inline void set_prohibits_overwriting(bool value);
7078
7079 inline PropertyAttributes property_attributes();
7080 inline void set_property_attributes(PropertyAttributes attributes);
7081
7082 static inline AccessorInfo* cast(Object* obj);
7083
Ben Murdochb0fe1622011-05-05 13:52:32 +01007084#ifdef OBJECT_PRINT
7085 inline void AccessorInfoPrint() {
7086 AccessorInfoPrint(stdout);
7087 }
7088 void AccessorInfoPrint(FILE* out);
7089#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007090#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007091 void AccessorInfoVerify();
7092#endif
7093
7094 static const int kGetterOffset = HeapObject::kHeaderSize;
7095 static const int kSetterOffset = kGetterOffset + kPointerSize;
7096 static const int kDataOffset = kSetterOffset + kPointerSize;
7097 static const int kNameOffset = kDataOffset + kPointerSize;
7098 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007099 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007100
7101 private:
7102 // Bit positions in flag.
7103 static const int kAllCanReadBit = 0;
7104 static const int kAllCanWriteBit = 1;
7105 static const int kProhibitsOverwritingBit = 2;
7106 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
7107
7108 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
7109};
7110
7111
7112class AccessCheckInfo: public Struct {
7113 public:
7114 DECL_ACCESSORS(named_callback, Object)
7115 DECL_ACCESSORS(indexed_callback, Object)
7116 DECL_ACCESSORS(data, Object)
7117
7118 static inline AccessCheckInfo* cast(Object* obj);
7119
Ben Murdochb0fe1622011-05-05 13:52:32 +01007120#ifdef OBJECT_PRINT
7121 inline void AccessCheckInfoPrint() {
7122 AccessCheckInfoPrint(stdout);
7123 }
7124 void AccessCheckInfoPrint(FILE* out);
7125#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007126#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007127 void AccessCheckInfoVerify();
7128#endif
7129
7130 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
7131 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
7132 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
7133 static const int kSize = kDataOffset + kPointerSize;
7134
7135 private:
7136 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
7137};
7138
7139
7140class InterceptorInfo: public Struct {
7141 public:
7142 DECL_ACCESSORS(getter, Object)
7143 DECL_ACCESSORS(setter, Object)
7144 DECL_ACCESSORS(query, Object)
7145 DECL_ACCESSORS(deleter, Object)
7146 DECL_ACCESSORS(enumerator, Object)
7147 DECL_ACCESSORS(data, Object)
7148
7149 static inline InterceptorInfo* cast(Object* obj);
7150
Ben Murdochb0fe1622011-05-05 13:52:32 +01007151#ifdef OBJECT_PRINT
7152 inline void InterceptorInfoPrint() {
7153 InterceptorInfoPrint(stdout);
7154 }
7155 void InterceptorInfoPrint(FILE* out);
7156#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007157#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007158 void InterceptorInfoVerify();
7159#endif
7160
7161 static const int kGetterOffset = HeapObject::kHeaderSize;
7162 static const int kSetterOffset = kGetterOffset + kPointerSize;
7163 static const int kQueryOffset = kSetterOffset + kPointerSize;
7164 static const int kDeleterOffset = kQueryOffset + kPointerSize;
7165 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
7166 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
7167 static const int kSize = kDataOffset + kPointerSize;
7168
7169 private:
7170 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
7171};
7172
7173
7174class CallHandlerInfo: public Struct {
7175 public:
7176 DECL_ACCESSORS(callback, Object)
7177 DECL_ACCESSORS(data, Object)
7178
7179 static inline CallHandlerInfo* cast(Object* obj);
7180
Ben Murdochb0fe1622011-05-05 13:52:32 +01007181#ifdef OBJECT_PRINT
7182 inline void CallHandlerInfoPrint() {
7183 CallHandlerInfoPrint(stdout);
7184 }
7185 void CallHandlerInfoPrint(FILE* out);
7186#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007187#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007188 void CallHandlerInfoVerify();
7189#endif
7190
7191 static const int kCallbackOffset = HeapObject::kHeaderSize;
7192 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007193 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007194
7195 private:
7196 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
7197};
7198
7199
7200class TemplateInfo: public Struct {
7201 public:
7202 DECL_ACCESSORS(tag, Object)
7203 DECL_ACCESSORS(property_list, Object)
7204
7205#ifdef DEBUG
7206 void TemplateInfoVerify();
7207#endif
7208
7209 static const int kTagOffset = HeapObject::kHeaderSize;
7210 static const int kPropertyListOffset = kTagOffset + kPointerSize;
7211 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
Ben Murdoch85b71792012-04-11 18:30:58 +01007212 protected:
7213 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
Steve Blocka7e24c12009-10-30 11:49:00 +00007214 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
7215};
7216
7217
7218class FunctionTemplateInfo: public TemplateInfo {
7219 public:
7220 DECL_ACCESSORS(serial_number, Object)
7221 DECL_ACCESSORS(call_code, Object)
7222 DECL_ACCESSORS(property_accessors, Object)
7223 DECL_ACCESSORS(prototype_template, Object)
7224 DECL_ACCESSORS(parent_template, Object)
7225 DECL_ACCESSORS(named_property_handler, Object)
7226 DECL_ACCESSORS(indexed_property_handler, Object)
7227 DECL_ACCESSORS(instance_template, Object)
7228 DECL_ACCESSORS(class_name, Object)
7229 DECL_ACCESSORS(signature, Object)
7230 DECL_ACCESSORS(instance_call_handler, Object)
7231 DECL_ACCESSORS(access_check_info, Object)
7232 DECL_ACCESSORS(flag, Smi)
7233
7234 // Following properties use flag bits.
7235 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
7236 DECL_BOOLEAN_ACCESSORS(undetectable)
7237 // If the bit is set, object instances created by this function
7238 // requires access check.
7239 DECL_BOOLEAN_ACCESSORS(needs_access_check)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007240 DECL_BOOLEAN_ACCESSORS(read_only_prototype)
Steve Blocka7e24c12009-10-30 11:49:00 +00007241
7242 static inline FunctionTemplateInfo* cast(Object* obj);
7243
Ben Murdochb0fe1622011-05-05 13:52:32 +01007244#ifdef OBJECT_PRINT
7245 inline void FunctionTemplateInfoPrint() {
7246 FunctionTemplateInfoPrint(stdout);
7247 }
7248 void FunctionTemplateInfoPrint(FILE* out);
7249#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007250#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007251 void FunctionTemplateInfoVerify();
7252#endif
7253
7254 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
7255 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
7256 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
7257 static const int kPrototypeTemplateOffset =
7258 kPropertyAccessorsOffset + kPointerSize;
7259 static const int kParentTemplateOffset =
7260 kPrototypeTemplateOffset + kPointerSize;
7261 static const int kNamedPropertyHandlerOffset =
7262 kParentTemplateOffset + kPointerSize;
7263 static const int kIndexedPropertyHandlerOffset =
7264 kNamedPropertyHandlerOffset + kPointerSize;
7265 static const int kInstanceTemplateOffset =
7266 kIndexedPropertyHandlerOffset + kPointerSize;
7267 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
7268 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
7269 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
7270 static const int kAccessCheckInfoOffset =
7271 kInstanceCallHandlerOffset + kPointerSize;
7272 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007273 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007274
7275 private:
7276 // Bit position in the flag, from least significant bit position.
7277 static const int kHiddenPrototypeBit = 0;
7278 static const int kUndetectableBit = 1;
7279 static const int kNeedsAccessCheckBit = 2;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007280 static const int kReadOnlyPrototypeBit = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00007281
7282 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
7283};
7284
7285
7286class ObjectTemplateInfo: public TemplateInfo {
7287 public:
7288 DECL_ACCESSORS(constructor, Object)
7289 DECL_ACCESSORS(internal_field_count, Object)
7290
7291 static inline ObjectTemplateInfo* cast(Object* obj);
7292
Ben Murdochb0fe1622011-05-05 13:52:32 +01007293#ifdef OBJECT_PRINT
7294 inline void ObjectTemplateInfoPrint() {
7295 ObjectTemplateInfoPrint(stdout);
7296 }
7297 void ObjectTemplateInfoPrint(FILE* out);
7298#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007299#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007300 void ObjectTemplateInfoVerify();
7301#endif
7302
7303 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
7304 static const int kInternalFieldCountOffset =
7305 kConstructorOffset + kPointerSize;
7306 static const int kSize = kInternalFieldCountOffset + kPointerSize;
7307};
7308
7309
7310class SignatureInfo: public Struct {
7311 public:
7312 DECL_ACCESSORS(receiver, Object)
7313 DECL_ACCESSORS(args, Object)
7314
7315 static inline SignatureInfo* cast(Object* obj);
7316
Ben Murdochb0fe1622011-05-05 13:52:32 +01007317#ifdef OBJECT_PRINT
7318 inline void SignatureInfoPrint() {
7319 SignatureInfoPrint(stdout);
7320 }
7321 void SignatureInfoPrint(FILE* out);
7322#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007323#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007324 void SignatureInfoVerify();
7325#endif
7326
7327 static const int kReceiverOffset = Struct::kHeaderSize;
7328 static const int kArgsOffset = kReceiverOffset + kPointerSize;
7329 static const int kSize = kArgsOffset + kPointerSize;
7330
7331 private:
7332 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
7333};
7334
7335
7336class TypeSwitchInfo: public Struct {
7337 public:
7338 DECL_ACCESSORS(types, Object)
7339
7340 static inline TypeSwitchInfo* cast(Object* obj);
7341
Ben Murdochb0fe1622011-05-05 13:52:32 +01007342#ifdef OBJECT_PRINT
7343 inline void TypeSwitchInfoPrint() {
7344 TypeSwitchInfoPrint(stdout);
7345 }
7346 void TypeSwitchInfoPrint(FILE* out);
7347#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007348#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007349 void TypeSwitchInfoVerify();
7350#endif
7351
7352 static const int kTypesOffset = Struct::kHeaderSize;
7353 static const int kSize = kTypesOffset + kPointerSize;
7354};
7355
7356
7357#ifdef ENABLE_DEBUGGER_SUPPORT
7358// The DebugInfo class holds additional information for a function being
7359// debugged.
7360class DebugInfo: public Struct {
7361 public:
7362 // The shared function info for the source being debugged.
7363 DECL_ACCESSORS(shared, SharedFunctionInfo)
7364 // Code object for the original code.
7365 DECL_ACCESSORS(original_code, Code)
7366 // Code object for the patched code. This code object is the code object
7367 // currently active for the function.
7368 DECL_ACCESSORS(code, Code)
7369 // Fixed array holding status information for each active break point.
7370 DECL_ACCESSORS(break_points, FixedArray)
7371
7372 // Check if there is a break point at a code position.
7373 bool HasBreakPoint(int code_position);
7374 // Get the break point info object for a code position.
7375 Object* GetBreakPointInfo(int code_position);
7376 // Clear a break point.
7377 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
7378 int code_position,
7379 Handle<Object> break_point_object);
7380 // Set a break point.
7381 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
7382 int source_position, int statement_position,
7383 Handle<Object> break_point_object);
7384 // Get the break point objects for a code position.
7385 Object* GetBreakPointObjects(int code_position);
7386 // Find the break point info holding this break point object.
7387 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
7388 Handle<Object> break_point_object);
7389 // Get the number of break points for this function.
7390 int GetBreakPointCount();
7391
7392 static inline DebugInfo* cast(Object* obj);
7393
Ben Murdochb0fe1622011-05-05 13:52:32 +01007394#ifdef OBJECT_PRINT
7395 inline void DebugInfoPrint() {
7396 DebugInfoPrint(stdout);
7397 }
7398 void DebugInfoPrint(FILE* out);
7399#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007400#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007401 void DebugInfoVerify();
7402#endif
7403
7404 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
7405 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
7406 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
7407 static const int kActiveBreakPointsCountIndex =
7408 kPatchedCodeIndex + kPointerSize;
7409 static const int kBreakPointsStateIndex =
7410 kActiveBreakPointsCountIndex + kPointerSize;
7411 static const int kSize = kBreakPointsStateIndex + kPointerSize;
7412
7413 private:
7414 static const int kNoBreakPointInfo = -1;
7415
7416 // Lookup the index in the break_points array for a code position.
7417 int GetBreakPointInfoIndex(int code_position);
7418
7419 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
7420};
7421
7422
7423// The BreakPointInfo class holds information for break points set in a
7424// function. The DebugInfo object holds a BreakPointInfo object for each code
7425// position with one or more break points.
7426class BreakPointInfo: public Struct {
7427 public:
7428 // The position in the code for the break point.
7429 DECL_ACCESSORS(code_position, Smi)
7430 // The position in the source for the break position.
7431 DECL_ACCESSORS(source_position, Smi)
7432 // The position in the source for the last statement before this break
7433 // position.
7434 DECL_ACCESSORS(statement_position, Smi)
7435 // List of related JavaScript break points.
7436 DECL_ACCESSORS(break_point_objects, Object)
7437
7438 // Removes a break point.
7439 static void ClearBreakPoint(Handle<BreakPointInfo> info,
7440 Handle<Object> break_point_object);
7441 // Set a break point.
7442 static void SetBreakPoint(Handle<BreakPointInfo> info,
7443 Handle<Object> break_point_object);
7444 // Check if break point info has this break point object.
7445 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
7446 Handle<Object> break_point_object);
7447 // Get the number of break points for this code position.
7448 int GetBreakPointCount();
7449
7450 static inline BreakPointInfo* cast(Object* obj);
7451
Ben Murdochb0fe1622011-05-05 13:52:32 +01007452#ifdef OBJECT_PRINT
7453 inline void BreakPointInfoPrint() {
7454 BreakPointInfoPrint(stdout);
7455 }
7456 void BreakPointInfoPrint(FILE* out);
7457#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007458#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007459 void BreakPointInfoVerify();
7460#endif
7461
7462 static const int kCodePositionIndex = Struct::kHeaderSize;
7463 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
7464 static const int kStatementPositionIndex =
7465 kSourcePositionIndex + kPointerSize;
7466 static const int kBreakPointObjectsIndex =
7467 kStatementPositionIndex + kPointerSize;
7468 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
7469
7470 private:
7471 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
7472};
7473#endif // ENABLE_DEBUGGER_SUPPORT
7474
7475
7476#undef DECL_BOOLEAN_ACCESSORS
7477#undef DECL_ACCESSORS
7478
7479
7480// Abstract base class for visiting, and optionally modifying, the
7481// pointers contained in Objects. Used in GC and serialization/deserialization.
7482class ObjectVisitor BASE_EMBEDDED {
7483 public:
7484 virtual ~ObjectVisitor() {}
7485
7486 // Visits a contiguous arrays of pointers in the half-open range
7487 // [start, end). Any or all of the values may be modified on return.
7488 virtual void VisitPointers(Object** start, Object** end) = 0;
7489
7490 // To allow lazy clearing of inline caches the visitor has
7491 // a rich interface for iterating over Code objects..
7492
7493 // Visits a code target in the instruction stream.
7494 virtual void VisitCodeTarget(RelocInfo* rinfo);
7495
Steve Block791712a2010-08-27 10:21:07 +01007496 // Visits a code entry in a JS function.
7497 virtual void VisitCodeEntry(Address entry_address);
7498
Ben Murdochb0fe1622011-05-05 13:52:32 +01007499 // Visits a global property cell reference in the instruction stream.
7500 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
7501
Steve Blocka7e24c12009-10-30 11:49:00 +00007502 // Visits a runtime entry in the instruction stream.
7503 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
7504
Steve Blockd0582a62009-12-15 09:54:21 +00007505 // Visits the resource of an ASCII or two-byte string.
7506 virtual void VisitExternalAsciiString(
7507 v8::String::ExternalAsciiStringResource** resource) {}
7508 virtual void VisitExternalTwoByteString(
7509 v8::String::ExternalStringResource** resource) {}
7510
Steve Blocka7e24c12009-10-30 11:49:00 +00007511 // Visits a debug call target in the instruction stream.
7512 virtual void VisitDebugTarget(RelocInfo* rinfo);
7513
7514 // Handy shorthand for visiting a single pointer.
7515 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
7516
7517 // Visits a contiguous arrays of external references (references to the C++
7518 // heap) in the half-open range [start, end). Any or all of the values
7519 // may be modified on return.
7520 virtual void VisitExternalReferences(Address* start, Address* end) {}
7521
7522 inline void VisitExternalReference(Address* p) {
7523 VisitExternalReferences(p, p + 1);
7524 }
7525
Steve Block44f0eee2011-05-26 01:26:41 +01007526 // Visits a handle that has an embedder-assigned class ID.
7527 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
7528
Ben Murdoch85b71792012-04-11 18:30:58 +01007529#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007530 // Intended for serialization/deserialization checking: insert, or
7531 // check for the presence of, a tag at this position in the stream.
Ben Murdoch85b71792012-04-11 18:30:58 +01007532 virtual void Synchronize(const char* tag) {}
7533#else
7534 inline void Synchronize(const char* tag) {}
7535#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007536};
7537
7538
Iain Merrick75681382010-08-19 15:07:18 +01007539class StructBodyDescriptor : public
7540 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
7541 public:
7542 static inline int SizeOf(Map* map, HeapObject* object) {
7543 return map->instance_size();
7544 }
7545};
7546
7547
Steve Blocka7e24c12009-10-30 11:49:00 +00007548// BooleanBit is a helper class for setting and getting a bit in an
7549// integer or Smi.
7550class BooleanBit : public AllStatic {
7551 public:
7552 static inline bool get(Smi* smi, int bit_position) {
7553 return get(smi->value(), bit_position);
7554 }
7555
7556 static inline bool get(int value, int bit_position) {
7557 return (value & (1 << bit_position)) != 0;
7558 }
7559
7560 static inline Smi* set(Smi* smi, int bit_position, bool v) {
7561 return Smi::FromInt(set(smi->value(), bit_position, v));
7562 }
7563
7564 static inline int set(int value, int bit_position, bool v) {
7565 if (v) {
7566 value |= (1 << bit_position);
7567 } else {
7568 value &= ~(1 << bit_position);
7569 }
7570 return value;
7571 }
7572};
7573
7574} } // namespace v8::internal
7575
7576#endif // V8_OBJECTS_H_