blob: 53ba981c85b2f794dae8c0187be0a8aae3270964 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_OBJECTS_H_
29#define V8_OBJECTS_H_
30
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "builtins.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000033#include "list.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000034#include "smart-pointer.h"
35#include "unicode-inl.h"
Steve Block3ce2e202009-11-05 08:53:23 +000036#if V8_TARGET_ARCH_ARM
37#include "arm/constants-arm.h"
Andrei Popescu31002712010-02-23 13:46:05 +000038#elif V8_TARGET_ARCH_MIPS
39#include "mips/constants-mips.h"
Steve Block3ce2e202009-11-05 08:53:23 +000040#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42//
Kristian Monsen50ef84f2010-07-29 15:18:00 +010043// Most object types in the V8 JavaScript are described in this file.
Steve Blocka7e24c12009-10-30 11:49:00 +000044//
45// Inheritance hierarchy:
John Reck59135872010-11-02 12:39:01 -070046// - MaybeObject (an object or a failure)
47// - Failure (immediate for marking failed operation)
Steve Blocka7e24c12009-10-30 11:49:00 +000048// - Object
49// - Smi (immediate small integer)
Steve Blocka7e24c12009-10-30 11:49:00 +000050// - HeapObject (superclass for everything allocated in the heap)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000051// - JSReceiver (suitable for property access)
52// - JSObject
53// - JSArray
Ben Murdoch69a99ed2011-11-30 16:03:39 +000054// - JSWeakMap
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000055// - JSRegExp
56// - JSFunction
57// - GlobalObject
58// - JSGlobalObject
59// - JSBuiltinsObject
60// - JSGlobalProxy
61// - JSValue
62// - JSMessageObject
63// - JSProxy
64// - JSFunctionProxy
Ben Murdoch69a99ed2011-11-30 16:03:39 +000065// - FixedArrayBase
66// - ByteArray
67// - FixedArray
68// - DescriptorArray
69// - HashTable
70// - Dictionary
71// - SymbolTable
72// - CompilationCacheTable
73// - CodeCacheHashTable
74// - MapCache
75// - Context
76// - JSFunctionResultCache
77// - SerializedScopeInfo
78// - FixedDoubleArray
79// - ExternalArray
80// - ExternalPixelArray
81// - ExternalByteArray
82// - ExternalUnsignedByteArray
83// - ExternalShortArray
84// - ExternalUnsignedShortArray
85// - ExternalIntArray
86// - ExternalUnsignedIntArray
87// - ExternalFloatArray
Steve Blocka7e24c12009-10-30 11:49:00 +000088// - String
89// - SeqString
90// - SeqAsciiString
91// - SeqTwoByteString
Ben Murdoch69a99ed2011-11-30 16:03:39 +000092// - SlicedString
Steve Blocka7e24c12009-10-30 11:49:00 +000093// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000094// - ExternalString
95// - ExternalAsciiString
96// - ExternalTwoByteString
97// - HeapNumber
98// - Code
99// - Map
100// - Oddball
Ben Murdoch257744e2011-11-30 15:57:28 +0000101// - Foreign
Steve Blocka7e24c12009-10-30 11:49:00 +0000102// - SharedFunctionInfo
103// - Struct
104// - AccessorInfo
105// - AccessCheckInfo
106// - InterceptorInfo
107// - CallHandlerInfo
108// - TemplateInfo
109// - FunctionTemplateInfo
110// - ObjectTemplateInfo
111// - Script
112// - SignatureInfo
113// - TypeSwitchInfo
114// - DebugInfo
115// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100116// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000117//
118// Formats of Object*:
119// Smi: [31 bit signed int] 0
120// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
121// Failure: [30 bit signed int] 11
122
123// Ecma-262 3rd 8.6.1
124enum PropertyAttributes {
125 NONE = v8::None,
126 READ_ONLY = v8::ReadOnly,
127 DONT_ENUM = v8::DontEnum,
128 DONT_DELETE = v8::DontDelete,
129 ABSENT = 16 // Used in runtime to indicate a property is absent.
130 // ABSENT can never be stored in or returned from a descriptor's attributes
131 // bitfield. It is only used as a return value meaning the attributes of
132 // a non-existent property.
133};
134
135namespace v8 {
136namespace internal {
137
138
139// PropertyDetails captures type and attributes for a property.
140// They are used both in property dictionaries and instance descriptors.
141class PropertyDetails BASE_EMBEDDED {
142 public:
Steve Blocka7e24c12009-10-30 11:49:00 +0000143 PropertyDetails(PropertyAttributes attributes,
144 PropertyType type,
145 int index = 0) {
Steve Block44f0eee2011-05-26 01:26:41 +0100146 ASSERT(type != EXTERNAL_ARRAY_TRANSITION);
Steve Blocka7e24c12009-10-30 11:49:00 +0000147 ASSERT(TypeField::is_valid(type));
148 ASSERT(AttributesField::is_valid(attributes));
Steve Block44f0eee2011-05-26 01:26:41 +0100149 ASSERT(StorageField::is_valid(index));
Steve Blocka7e24c12009-10-30 11:49:00 +0000150
151 value_ = TypeField::encode(type)
152 | AttributesField::encode(attributes)
Steve Block44f0eee2011-05-26 01:26:41 +0100153 | StorageField::encode(index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000154
155 ASSERT(type == this->type());
156 ASSERT(attributes == this->attributes());
157 ASSERT(index == this->index());
158 }
159
Steve Block44f0eee2011-05-26 01:26:41 +0100160 PropertyDetails(PropertyAttributes attributes,
161 PropertyType type,
162 ExternalArrayType array_type) {
163 ASSERT(type == EXTERNAL_ARRAY_TRANSITION);
164 ASSERT(TypeField::is_valid(type));
165 ASSERT(AttributesField::is_valid(attributes));
166 ASSERT(StorageField::is_valid(static_cast<int>(array_type)));
167
168 value_ = TypeField::encode(type)
169 | AttributesField::encode(attributes)
170 | StorageField::encode(static_cast<int>(array_type));
171
172 ASSERT(type == this->type());
173 ASSERT(attributes == this->attributes());
174 ASSERT(array_type == this->array_type());
175 }
176
Steve Blocka7e24c12009-10-30 11:49:00 +0000177 // Conversion for storing details as Object*.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100178 explicit inline PropertyDetails(Smi* smi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 inline Smi* AsSmi();
180
181 PropertyType type() { return TypeField::decode(value_); }
182
183 bool IsTransition() {
184 PropertyType t = type();
185 ASSERT(t != INTERCEPTOR);
Steve Block44f0eee2011-05-26 01:26:41 +0100186 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
187 t == EXTERNAL_ARRAY_TRANSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000188 }
189
190 bool IsProperty() {
191 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
192 }
193
194 PropertyAttributes attributes() { return AttributesField::decode(value_); }
195
Steve Block44f0eee2011-05-26 01:26:41 +0100196 int index() { return StorageField::decode(value_); }
197
198 ExternalArrayType array_type() {
199 ASSERT(type() == EXTERNAL_ARRAY_TRANSITION);
200 return static_cast<ExternalArrayType>(StorageField::decode(value_));
201 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203 inline PropertyDetails AsDeleted();
204
Steve Block44f0eee2011-05-26 01:26:41 +0100205 static bool IsValidIndex(int index) {
206 return StorageField::is_valid(index);
207 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000208
209 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
210 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
211 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
212 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
213
214 // Bit fields in value_ (type, shift, size). Must be public so the
215 // constants can be embedded in generated code.
Steve Block44f0eee2011-05-26 01:26:41 +0100216 class TypeField: public BitField<PropertyType, 0, 4> {};
217 class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
218 class DeletedField: public BitField<uint32_t, 7, 1> {};
219 class StorageField: public BitField<uint32_t, 8, 32-8> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000220
221 static const int kInitialIndex = 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000222
Steve Blocka7e24c12009-10-30 11:49:00 +0000223 private:
224 uint32_t value_;
225};
226
227
228// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
229enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
230
231
232// PropertyNormalizationMode is used to specify whether to keep
233// inobject properties when normalizing properties of a JSObject.
234enum PropertyNormalizationMode {
235 CLEAR_INOBJECT_PROPERTIES,
236 KEEP_INOBJECT_PROPERTIES
237};
238
239
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100240// NormalizedMapSharingMode is used to specify whether a map may be shared
241// by different objects with normalized properties.
242enum NormalizedMapSharingMode {
243 UNIQUE_NORMALIZED_MAP,
244 SHARED_NORMALIZED_MAP
245};
246
247
Steve Block791712a2010-08-27 10:21:07 +0100248// Instance size sentinel for objects of variable size.
249static const int kVariableSizeSentinel = 0;
250
251
Steve Blocka7e24c12009-10-30 11:49:00 +0000252// All Maps have a field instance_type containing a InstanceType.
253// It describes the type of the instances.
254//
255// As an example, a JavaScript object is a heap object and its map
256// instance_type is JS_OBJECT_TYPE.
257//
258// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000259// mirror their encoding in the instance_type field of the map. The default
260// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
261// encoding is mentioned explicitly in the name. Likewise, the default
262// representation is considered sequential. It is not mentioned in the
263// name. The other representations (eg, CONS, EXTERNAL) are explicitly
264// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
265// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000266//
267// NOTE: The following things are some that depend on the string types having
268// instance_types that are less than those of all other types:
269// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
270// Object::IsString.
271//
272// NOTE: Everything following JS_VALUE_TYPE is considered a
273// JSObject for GC purposes. The first four entries here have typeof
274// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000275#define INSTANCE_TYPE_LIST_ALL(V) \
276 V(SYMBOL_TYPE) \
277 V(ASCII_SYMBOL_TYPE) \
278 V(CONS_SYMBOL_TYPE) \
279 V(CONS_ASCII_SYMBOL_TYPE) \
280 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100281 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000282 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
283 V(STRING_TYPE) \
284 V(ASCII_STRING_TYPE) \
285 V(CONS_STRING_TYPE) \
286 V(CONS_ASCII_STRING_TYPE) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000287 V(SLICED_STRING_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000288 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100289 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000290 V(EXTERNAL_ASCII_STRING_TYPE) \
291 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
292 \
293 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000294 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000295 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100296 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000297 \
298 V(HEAP_NUMBER_TYPE) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000299 V(FOREIGN_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000300 V(BYTE_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000301 /* Note: the order of these external array */ \
302 /* types is relied upon in */ \
303 /* Object::IsExternalArray(). */ \
304 V(EXTERNAL_BYTE_ARRAY_TYPE) \
305 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
306 V(EXTERNAL_SHORT_ARRAY_TYPE) \
307 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
308 V(EXTERNAL_INT_ARRAY_TYPE) \
309 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
310 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
Steve Block44f0eee2011-05-26 01:26:41 +0100311 V(EXTERNAL_PIXEL_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000312 V(FILLER_TYPE) \
313 \
314 V(ACCESSOR_INFO_TYPE) \
315 V(ACCESS_CHECK_INFO_TYPE) \
316 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000317 V(CALL_HANDLER_INFO_TYPE) \
318 V(FUNCTION_TEMPLATE_INFO_TYPE) \
319 V(OBJECT_TEMPLATE_INFO_TYPE) \
320 V(SIGNATURE_INFO_TYPE) \
321 V(TYPE_SWITCH_INFO_TYPE) \
322 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100323 V(CODE_CACHE_TYPE) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000324 V(POLYMORPHIC_CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000325 \
Iain Merrick75681382010-08-19 15:07:18 +0100326 V(FIXED_ARRAY_TYPE) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000327 V(FIXED_DOUBLE_ARRAY_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100328 V(SHARED_FUNCTION_INFO_TYPE) \
329 \
Steve Block1e0659c2011-05-24 12:43:12 +0100330 V(JS_MESSAGE_OBJECT_TYPE) \
331 \
Steve Blockd0582a62009-12-15 09:54:21 +0000332 V(JS_VALUE_TYPE) \
333 V(JS_OBJECT_TYPE) \
334 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
335 V(JS_GLOBAL_OBJECT_TYPE) \
336 V(JS_BUILTINS_OBJECT_TYPE) \
337 V(JS_GLOBAL_PROXY_TYPE) \
338 V(JS_ARRAY_TYPE) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000339 V(JS_PROXY_TYPE) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000340 V(JS_WEAK_MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000341 V(JS_REGEXP_TYPE) \
342 \
343 V(JS_FUNCTION_TYPE) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000344 V(JS_FUNCTION_PROXY_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000345
346#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000347#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
348 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000349 V(BREAK_POINT_INFO_TYPE)
350#else
351#define INSTANCE_TYPE_LIST_DEBUGGER(V)
352#endif
353
Steve Blockd0582a62009-12-15 09:54:21 +0000354#define INSTANCE_TYPE_LIST(V) \
355 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000356 INSTANCE_TYPE_LIST_DEBUGGER(V)
357
358
359// Since string types are not consecutive, this macro is used to
360// iterate over them.
361#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000362 V(SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100363 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000364 symbol, \
365 Symbol) \
366 V(ASCII_SYMBOL_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100367 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000368 ascii_symbol, \
369 AsciiSymbol) \
370 V(CONS_SYMBOL_TYPE, \
371 ConsString::kSize, \
372 cons_symbol, \
373 ConsSymbol) \
374 V(CONS_ASCII_SYMBOL_TYPE, \
375 ConsString::kSize, \
376 cons_ascii_symbol, \
377 ConsAsciiSymbol) \
378 V(EXTERNAL_SYMBOL_TYPE, \
379 ExternalTwoByteString::kSize, \
380 external_symbol, \
381 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100382 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
383 ExternalTwoByteString::kSize, \
384 external_symbol_with_ascii_data, \
385 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000386 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
387 ExternalAsciiString::kSize, \
388 external_ascii_symbol, \
389 ExternalAsciiSymbol) \
390 V(STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100391 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000392 string, \
393 String) \
394 V(ASCII_STRING_TYPE, \
Steve Block791712a2010-08-27 10:21:07 +0100395 kVariableSizeSentinel, \
Steve Blockd0582a62009-12-15 09:54:21 +0000396 ascii_string, \
397 AsciiString) \
398 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000400 cons_string, \
401 ConsString) \
402 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000404 cons_ascii_string, \
405 ConsAsciiString) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000406 V(SLICED_STRING_TYPE, \
407 SlicedString::kSize, \
408 sliced_string, \
409 SlicedString) \
410 V(SLICED_ASCII_STRING_TYPE, \
411 SlicedString::kSize, \
412 sliced_ascii_string, \
413 SlicedAsciiString) \
Steve Blockd0582a62009-12-15 09:54:21 +0000414 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000415 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000416 external_string, \
417 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100418 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
419 ExternalTwoByteString::kSize, \
420 external_string_with_ascii_data, \
421 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000422 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000423 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000424 external_ascii_string, \
Steve Block791712a2010-08-27 10:21:07 +0100425 ExternalAsciiString)
Steve Blocka7e24c12009-10-30 11:49:00 +0000426
427// A struct is a simple object a set of object-valued fields. Including an
428// object type in this causes the compiler to generate most of the boilerplate
429// code for the class including allocation and garbage collection routines,
430// casts and predicates. All you need to define is the class, methods and
431// object verification routines. Easy, no?
432//
433// Note that for subtle reasons related to the ordering or numerical values of
434// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
435// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000436#define STRUCT_LIST_ALL(V) \
437 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
438 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
439 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
440 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
441 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
442 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
443 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
444 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100445 V(SCRIPT, Script, script) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000446 V(CODE_CACHE, CodeCache, code_cache) \
447 V(POLYMORPHIC_CODE_CACHE, PolymorphicCodeCache, polymorphic_code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000448
449#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000450#define STRUCT_LIST_DEBUGGER(V) \
451 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000452 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
453#else
454#define STRUCT_LIST_DEBUGGER(V)
455#endif
456
Steve Blockd0582a62009-12-15 09:54:21 +0000457#define STRUCT_LIST(V) \
458 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 STRUCT_LIST_DEBUGGER(V)
460
461// We use the full 8 bits of the instance_type field to encode heap object
462// instance types. The high-order bit (bit 7) is set if the object is not a
463// string, and cleared if it is a string.
464const uint32_t kIsNotStringMask = 0x80;
465const uint32_t kStringTag = 0x0;
466const uint32_t kNotStringTag = 0x80;
467
Leon Clarkee46be812010-01-19 14:06:41 +0000468// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
469// There are not enough types that the non-string types (with bit 7 set) can
470// have bit 6 set too.
471const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000472const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000473const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475// If bit 7 is clear then bit 2 indicates whether the string consists of
476// two-byte characters or one-byte characters.
477const uint32_t kStringEncodingMask = 0x4;
478const uint32_t kTwoByteStringTag = 0x0;
479const uint32_t kAsciiStringTag = 0x4;
480
481// If bit 7 is clear, the low-order 2 bits indicate the representation
482// of the string.
483const uint32_t kStringRepresentationMask = 0x03;
484enum StringRepresentationTag {
485 kSeqStringTag = 0x0,
486 kConsStringTag = 0x1,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000487 kExternalStringTag = 0x2,
488 kSlicedStringTag = 0x3
Steve Blocka7e24c12009-10-30 11:49:00 +0000489};
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000490const uint32_t kIsIndirectStringMask = 0x1;
491const uint32_t kIsIndirectStringTag = 0x1;
492STATIC_ASSERT((kSeqStringTag & kIsIndirectStringMask) == 0);
493STATIC_ASSERT((kExternalStringTag & kIsIndirectStringMask) == 0);
494STATIC_ASSERT(
495 (kConsStringTag & kIsIndirectStringMask) == kIsIndirectStringTag);
496STATIC_ASSERT(
497 (kSlicedStringTag & kIsIndirectStringMask) == kIsIndirectStringTag);
498
499// Use this mask to distinguish between cons and slice only after making
500// sure that the string is one of the two (an indirect string).
501const uint32_t kSlicedNotConsMask = kSlicedStringTag & ~kConsStringTag;
502STATIC_ASSERT(IS_POWER_OF_TWO(kSlicedNotConsMask) && kSlicedNotConsMask != 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000503
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100504// If bit 7 is clear, then bit 3 indicates whether this two-byte
505// string actually contains ascii data.
506const uint32_t kAsciiDataHintMask = 0x08;
507const uint32_t kAsciiDataHintTag = 0x08;
508
Steve Blocka7e24c12009-10-30 11:49:00 +0000509
510// A ConsString with an empty string as the right side is a candidate
511// for being shortcut by the garbage collector unless it is a
512// symbol. It's not common to have non-flat symbols, so we do not
513// shortcut them thereby avoiding turning symbols into strings. See
514// heap.cc and mark-compact.cc.
515const uint32_t kShortcutTypeMask =
516 kIsNotStringMask |
517 kIsSymbolMask |
518 kStringRepresentationMask;
519const uint32_t kShortcutTypeTag = kConsStringTag;
520
521
522enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000523 // String types.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100524 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000525 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100526 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000527 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100528 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
529 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
530 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000531 EXTERNAL_ASCII_SYMBOL_TYPE =
532 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100533 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000534 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100535 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000536 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000537 SLICED_STRING_TYPE = kTwoByteStringTag | kSlicedStringTag,
538 SLICED_ASCII_STRING_TYPE = kAsciiStringTag | kSlicedStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100539 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
540 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
541 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Block1e0659c2011-05-24 12:43:12 +0100542 // LAST_STRING_TYPE
Steve Blockd0582a62009-12-15 09:54:21 +0000543 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
544 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000545
Leon Clarkee46be812010-01-19 14:06:41 +0000546 // Objects allocated in their own spaces (never in new space).
547 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000548 CODE_TYPE,
549 ODDBALL_TYPE,
550 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000551
552 // "Data", objects that cannot contain non-map-word pointers to heap
553 // objects.
554 HEAP_NUMBER_TYPE,
Ben Murdoch257744e2011-11-30 15:57:28 +0000555 FOREIGN_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 BYTE_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000557 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000558 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
559 EXTERNAL_SHORT_ARRAY_TYPE,
560 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
561 EXTERNAL_INT_ARRAY_TYPE,
562 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100563 EXTERNAL_FLOAT_ARRAY_TYPE,
Ben Murdoch257744e2011-11-30 15:57:28 +0000564 EXTERNAL_DOUBLE_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100565 EXTERNAL_PIXEL_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000566 FIXED_DOUBLE_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000567 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000568
Leon Clarkee46be812010-01-19 14:06:41 +0000569 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 ACCESSOR_INFO_TYPE,
571 ACCESS_CHECK_INFO_TYPE,
572 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000573 CALL_HANDLER_INFO_TYPE,
574 FUNCTION_TEMPLATE_INFO_TYPE,
575 OBJECT_TEMPLATE_INFO_TYPE,
576 SIGNATURE_INFO_TYPE,
577 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000578 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100579 CODE_CACHE_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000580 POLYMORPHIC_CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100581 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
582 // is defined. However as include/v8.h contain some of the instance type
583 // constants always having them avoids them getting different numbers
584 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 DEBUG_INFO_TYPE,
586 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000587
Leon Clarkee46be812010-01-19 14:06:41 +0000588 FIXED_ARRAY_TYPE,
589 SHARED_FUNCTION_INFO_TYPE,
590
Steve Block1e0659c2011-05-24 12:43:12 +0100591 JS_MESSAGE_OBJECT_TYPE,
592
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000593 JS_VALUE_TYPE, // FIRST_NON_CALLABLE_OBJECT_TYPE, FIRST_JS_RECEIVER_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000594 JS_OBJECT_TYPE,
595 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
596 JS_GLOBAL_OBJECT_TYPE,
597 JS_BUILTINS_OBJECT_TYPE,
598 JS_GLOBAL_PROXY_TYPE,
599 JS_ARRAY_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000600 JS_PROXY_TYPE,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000601 JS_WEAK_MAP_TYPE,
Steve Block1e0659c2011-05-24 12:43:12 +0100602
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000603 JS_REGEXP_TYPE, // LAST_NONCALLABLE_SPEC_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000604
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000605 JS_FUNCTION_TYPE, // FIRST_CALLABLE_SPEC_OBJECT_TYPE
606 JS_FUNCTION_PROXY_TYPE, // LAST_CALLABLE_SPEC_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000607
608 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000609 FIRST_TYPE = 0x0,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000610 LAST_TYPE = JS_FUNCTION_PROXY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000611 INVALID_TYPE = FIRST_TYPE - 1,
612 FIRST_NONSTRING_TYPE = MAP_TYPE,
613 // Boundaries for testing for an external array.
614 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
Steve Block44f0eee2011-05-26 01:26:41 +0100615 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000616 // Boundary for promotion to old data space/old pointer space.
617 LAST_DATA_TYPE = FILLER_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000618 // Boundary for objects represented as JSReceiver (i.e. JSObject or JSProxy).
619 // Note that there is no range for JSObject or JSProxy, since their subtypes
620 // are not continuous in this enum! The enum ranges instead reflect the
621 // external class names, where proxies are treated as either ordinary objects,
622 // or functions.
623 FIRST_JS_RECEIVER_TYPE = JS_VALUE_TYPE,
624 LAST_JS_RECEIVER_TYPE = LAST_TYPE,
625 // Boundaries for testing the types for which typeof is "object".
626 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_VALUE_TYPE,
627 LAST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_REGEXP_TYPE,
628 // Boundaries for testing the types for which typeof is "function".
629 FIRST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_TYPE,
630 LAST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_PROXY_TYPE,
631 // Boundaries for testing whether the type is a JavaScript object.
632 FIRST_SPEC_OBJECT_TYPE = FIRST_NONCALLABLE_SPEC_OBJECT_TYPE,
633 LAST_SPEC_OBJECT_TYPE = LAST_CALLABLE_SPEC_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000634};
635
Steve Block44f0eee2011-05-26 01:26:41 +0100636static const int kExternalArrayTypeCount = LAST_EXTERNAL_ARRAY_TYPE -
637 FIRST_EXTERNAL_ARRAY_TYPE + 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000638
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100639STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
640STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
Ben Murdoch257744e2011-11-30 15:57:28 +0000641STATIC_CHECK(FOREIGN_TYPE == Internals::kForeignType);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100642
643
Steve Blocka7e24c12009-10-30 11:49:00 +0000644enum CompareResult {
645 LESS = -1,
646 EQUAL = 0,
647 GREATER = 1,
648
649 NOT_EQUAL = GREATER
650};
651
652
653#define DECL_BOOLEAN_ACCESSORS(name) \
654 inline bool name(); \
655 inline void set_##name(bool value); \
656
657
658#define DECL_ACCESSORS(name, type) \
659 inline type* name(); \
660 inline void set_##name(type* value, \
661 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
662
663
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000664class DictionaryElementsAccessor;
665class ElementsAccessor;
666class FixedArrayBase;
Steve Blocka7e24c12009-10-30 11:49:00 +0000667class ObjectVisitor;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000668class StringStream;
Steve Blocka7e24c12009-10-30 11:49:00 +0000669
670struct ValueInfo : public Malloced {
671 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
672 InstanceType type;
673 Object* ptr;
674 const char* str;
675 double number;
676};
677
678
679// A template-ized version of the IsXXX functions.
680template <class C> static inline bool Is(Object* obj);
681
Ben Murdoch257744e2011-11-30 15:57:28 +0000682class Failure;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100683
John Reck59135872010-11-02 12:39:01 -0700684class MaybeObject BASE_EMBEDDED {
685 public:
686 inline bool IsFailure();
687 inline bool IsRetryAfterGC();
688 inline bool IsOutOfMemory();
689 inline bool IsException();
690 INLINE(bool IsTheHole());
691 inline bool ToObject(Object** obj) {
692 if (IsFailure()) return false;
693 *obj = reinterpret_cast<Object*>(this);
694 return true;
695 }
Steve Block053d10c2011-06-13 19:13:29 +0100696 inline Failure* ToFailureUnchecked() {
697 ASSERT(IsFailure());
698 return reinterpret_cast<Failure*>(this);
699 }
John Reck59135872010-11-02 12:39:01 -0700700 inline Object* ToObjectUnchecked() {
701 ASSERT(!IsFailure());
702 return reinterpret_cast<Object*>(this);
703 }
704 inline Object* ToObjectChecked() {
705 CHECK(!IsFailure());
706 return reinterpret_cast<Object*>(this);
707 }
708
Steve Block053d10c2011-06-13 19:13:29 +0100709 template<typename T>
710 inline bool To(T** obj) {
711 if (IsFailure()) return false;
712 *obj = T::cast(reinterpret_cast<Object*>(this));
713 return true;
714 }
715
Ben Murdochb0fe1622011-05-05 13:52:32 +0100716#ifdef OBJECT_PRINT
John Reck59135872010-11-02 12:39:01 -0700717 // Prints this object with details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100718 inline void Print() {
719 Print(stdout);
720 };
721 inline void PrintLn() {
722 PrintLn(stdout);
723 }
724 void Print(FILE* out);
725 void PrintLn(FILE* out);
726#endif
727#ifdef DEBUG
John Reck59135872010-11-02 12:39:01 -0700728 // Verifies the object.
729 void Verify();
730#endif
731};
Steve Blocka7e24c12009-10-30 11:49:00 +0000732
Ben Murdochb8e0da22011-05-16 14:20:40 +0100733
734#define OBJECT_TYPE_LIST(V) \
735 V(Smi) \
736 V(HeapObject) \
737 V(Number) \
738
739#define HEAP_OBJECT_TYPE_LIST(V) \
740 V(HeapNumber) \
741 V(String) \
742 V(Symbol) \
743 V(SeqString) \
744 V(ExternalString) \
745 V(ConsString) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000746 V(SlicedString) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100747 V(ExternalTwoByteString) \
748 V(ExternalAsciiString) \
749 V(SeqTwoByteString) \
750 V(SeqAsciiString) \
751 \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100752 V(ExternalArray) \
753 V(ExternalByteArray) \
754 V(ExternalUnsignedByteArray) \
755 V(ExternalShortArray) \
756 V(ExternalUnsignedShortArray) \
757 V(ExternalIntArray) \
758 V(ExternalUnsignedIntArray) \
759 V(ExternalFloatArray) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000760 V(ExternalDoubleArray) \
Steve Block44f0eee2011-05-26 01:26:41 +0100761 V(ExternalPixelArray) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100762 V(ByteArray) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000763 V(JSReceiver) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100764 V(JSObject) \
765 V(JSContextExtensionObject) \
766 V(Map) \
767 V(DescriptorArray) \
768 V(DeoptimizationInputData) \
769 V(DeoptimizationOutputData) \
770 V(FixedArray) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000771 V(FixedDoubleArray) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100772 V(Context) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100773 V(GlobalContext) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000774 V(SerializedScopeInfo) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100775 V(JSFunction) \
776 V(Code) \
777 V(Oddball) \
778 V(SharedFunctionInfo) \
779 V(JSValue) \
Steve Block1e0659c2011-05-24 12:43:12 +0100780 V(JSMessageObject) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100781 V(StringWrapper) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000782 V(Foreign) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100783 V(Boolean) \
784 V(JSArray) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000785 V(JSProxy) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000786 V(JSFunctionProxy) \
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000787 V(JSWeakMap) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100788 V(JSRegExp) \
789 V(HashTable) \
790 V(Dictionary) \
791 V(SymbolTable) \
792 V(JSFunctionResultCache) \
793 V(NormalizedMapCache) \
794 V(CompilationCacheTable) \
795 V(CodeCacheHashTable) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000796 V(PolymorphicCodeCacheHashTable) \
Ben Murdochb8e0da22011-05-16 14:20:40 +0100797 V(MapCache) \
798 V(Primitive) \
799 V(GlobalObject) \
800 V(JSGlobalObject) \
801 V(JSBuiltinsObject) \
802 V(JSGlobalProxy) \
803 V(UndetectableObject) \
804 V(AccessCheckNeeded) \
805 V(JSGlobalPropertyCell) \
806
Steve Blocka7e24c12009-10-30 11:49:00 +0000807// Object is the abstract superclass for all classes in the
808// object hierarchy.
809// Object does not use any virtual functions to avoid the
810// allocation of the C++ vtable.
811// Since Smi and Failure are subclasses of Object no
812// data members can be present in Object.
John Reck59135872010-11-02 12:39:01 -0700813class Object : public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 public:
815 // Type testing.
Ben Murdochb8e0da22011-05-16 14:20:40 +0100816#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
817 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
818 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
819#undef IS_TYPE_FUNCTION_DECL
Steve Blocka7e24c12009-10-30 11:49:00 +0000820
821 // Returns true if this object is an instance of the specified
822 // function template.
823 inline bool IsInstanceOf(FunctionTemplateInfo* type);
824
825 inline bool IsStruct();
826#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
827 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
828#undef DECLARE_STRUCT_PREDICATE
829
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000830 INLINE(bool IsSpecObject());
831
Steve Blocka7e24c12009-10-30 11:49:00 +0000832 // Oddball testing.
833 INLINE(bool IsUndefined());
Steve Blocka7e24c12009-10-30 11:49:00 +0000834 INLINE(bool IsNull());
Steve Block44f0eee2011-05-26 01:26:41 +0100835 INLINE(bool IsTheHole()); // Shadows MaybeObject's implementation.
Steve Blocka7e24c12009-10-30 11:49:00 +0000836 INLINE(bool IsTrue());
837 INLINE(bool IsFalse());
Ben Murdoch086aeea2011-05-13 15:57:08 +0100838 inline bool IsArgumentsMarker();
Steve Blocka7e24c12009-10-30 11:49:00 +0000839
840 // Extract the number.
841 inline double Number();
842
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000843 // Returns true if the object is of the correct type to be used as a
844 // implementation of a JSObject's elements.
845 inline bool HasValidElements();
846
Steve Blocka7e24c12009-10-30 11:49:00 +0000847 inline bool HasSpecificClassOf(String* name);
848
John Reck59135872010-11-02 12:39:01 -0700849 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
850 Object* ToBoolean(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +0000851
852 // Convert to a JSObject if needed.
853 // global_context is used when creating wrapper object.
John Reck59135872010-11-02 12:39:01 -0700854 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
Steve Blocka7e24c12009-10-30 11:49:00 +0000855
856 // Converts this to a Smi if possible.
857 // Failure is returned otherwise.
John Reck59135872010-11-02 12:39:01 -0700858 MUST_USE_RESULT inline MaybeObject* ToSmi();
Steve Blocka7e24c12009-10-30 11:49:00 +0000859
860 void Lookup(String* name, LookupResult* result);
861
862 // Property access.
John Reck59135872010-11-02 12:39:01 -0700863 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
864 MUST_USE_RESULT inline MaybeObject* GetProperty(
865 String* key,
866 PropertyAttributes* attributes);
867 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
868 Object* receiver,
869 String* key,
870 PropertyAttributes* attributes);
871 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
872 LookupResult* result,
873 String* key,
874 PropertyAttributes* attributes);
875 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
876 Object* structure,
877 String* name,
878 Object* holder);
Ben Murdoch257744e2011-11-30 15:57:28 +0000879 MUST_USE_RESULT MaybeObject* GetPropertyWithHandler(Object* receiver,
880 String* name,
881 Object* handler);
John Reck59135872010-11-02 12:39:01 -0700882 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
883 JSFunction* getter);
Steve Blocka7e24c12009-10-30 11:49:00 +0000884
John Reck59135872010-11-02 12:39:01 -0700885 inline MaybeObject* GetElement(uint32_t index);
886 // For use when we know that no exception can be thrown.
887 inline Object* GetElementNoExceptionThrown(uint32_t index);
888 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +0000889
890 // Return the object's prototype (might be Heap::null_value()).
891 Object* GetPrototype();
892
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100893 // Tries to convert an object to an array index. Returns true and sets
894 // the output parameter if it succeeds.
895 inline bool ToArrayIndex(uint32_t* index);
896
Steve Blocka7e24c12009-10-30 11:49:00 +0000897 // Returns true if this is a JSValue containing a string and the index is
898 // < the length of the string. Used to implement [] on strings.
899 inline bool IsStringObjectWithCharacterAt(uint32_t index);
900
901#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +0000902 // Verify a pointer is a valid object pointer.
903 static void VerifyPointer(Object* p);
904#endif
905
906 // Prints this object without details.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100907 inline void ShortPrint() {
908 ShortPrint(stdout);
909 }
910 void ShortPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000911
912 // Prints this object without details to a message accumulator.
913 void ShortPrint(StringStream* accumulator);
914
915 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
916 static Object* cast(Object* value) { return value; }
917
918 // Layout description.
919 static const int kHeaderSize = 0; // Object does not take up any space.
920
921 private:
922 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
923};
924
925
926// Smi represents integer Numbers that can be stored in 31 bits.
927// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000928// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000929// For long smis it has the following format:
930// [32 bit signed int] [31 bits zero padding] 0
931// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000932class Smi: public Object {
933 public:
934 // Returns the integer value.
935 inline int value();
936
937 // Convert a value to a Smi object.
938 static inline Smi* FromInt(int value);
939
940 static inline Smi* FromIntptr(intptr_t value);
941
942 // Returns whether value can be represented in a Smi.
943 static inline bool IsValid(intptr_t value);
944
Steve Blocka7e24c12009-10-30 11:49:00 +0000945 // Casting.
946 static inline Smi* cast(Object* object);
947
948 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100949 inline void SmiPrint() {
950 SmiPrint(stdout);
951 }
952 void SmiPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +0000953 void SmiPrint(StringStream* accumulator);
954#ifdef DEBUG
955 void SmiVerify();
956#endif
957
Steve Block3ce2e202009-11-05 08:53:23 +0000958 static const int kMinValue = (-1 << (kSmiValueSize - 1));
959 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000960
961 private:
962 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
963};
964
965
966// Failure is used for reporting out of memory situations and
967// propagating exceptions through the runtime system. Failure objects
968// are transient and cannot occur as part of the object graph.
969//
970// Failures are a single word, encoded as follows:
971// +-------------------------+---+--+--+
Ben Murdochf87a2032010-10-22 12:50:53 +0100972// |.........unused..........|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000973// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000974// 7 6 4 32 10
975//
Steve Blocka7e24c12009-10-30 11:49:00 +0000976//
977// The low two bits, 0-1, are the failure tag, 11. The next two bits,
978// 2-3, are a failure type tag 'tt' with possible values:
979// 00 RETRY_AFTER_GC
980// 01 EXCEPTION
981// 10 INTERNAL_ERROR
982// 11 OUT_OF_MEMORY_EXCEPTION
983//
984// The next three bits, 4-6, are an allocation space tag 'sss'. The
985// allocation space tag is 000 for all failure types except
986// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
987// allocation spaces (the encoding is found in globals.h).
Steve Blocka7e24c12009-10-30 11:49:00 +0000988
989// Failure type tag info.
990const int kFailureTypeTagSize = 2;
991const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
992
John Reck59135872010-11-02 12:39:01 -0700993class Failure: public MaybeObject {
Steve Blocka7e24c12009-10-30 11:49:00 +0000994 public:
995 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
996 enum Type {
997 RETRY_AFTER_GC = 0,
998 EXCEPTION = 1, // Returning this marker tells the real exception
Steve Block44f0eee2011-05-26 01:26:41 +0100999 // is in Isolate::pending_exception.
Steve Blocka7e24c12009-10-30 11:49:00 +00001000 INTERNAL_ERROR = 2,
1001 OUT_OF_MEMORY_EXCEPTION = 3
1002 };
1003
1004 inline Type type() const;
1005
1006 // Returns the space that needs to be collected for RetryAfterGC failures.
1007 inline AllocationSpace allocation_space() const;
1008
Steve Blocka7e24c12009-10-30 11:49:00 +00001009 inline bool IsInternalError() const;
1010 inline bool IsOutOfMemoryException() const;
1011
Ben Murdochf87a2032010-10-22 12:50:53 +01001012 static inline Failure* RetryAfterGC(AllocationSpace space);
1013 static inline Failure* RetryAfterGC(); // NEW_SPACE
Steve Blocka7e24c12009-10-30 11:49:00 +00001014 static inline Failure* Exception();
1015 static inline Failure* InternalError();
1016 static inline Failure* OutOfMemoryException();
1017 // Casting.
John Reck59135872010-11-02 12:39:01 -07001018 static inline Failure* cast(MaybeObject* object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001019
1020 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001021 inline void FailurePrint() {
1022 FailurePrint(stdout);
1023 }
1024 void FailurePrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001025 void FailurePrint(StringStream* accumulator);
1026#ifdef DEBUG
1027 void FailureVerify();
1028#endif
1029
1030 private:
Steve Block3ce2e202009-11-05 08:53:23 +00001031 inline intptr_t value() const;
1032 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001033
1034 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
1035};
1036
1037
1038// Heap objects typically have a map pointer in their first word. However,
1039// during GC other data (eg, mark bits, forwarding addresses) is sometimes
1040// encoded in the first word. The class MapWord is an abstraction of the
1041// value in a heap object's first word.
1042class MapWord BASE_EMBEDDED {
1043 public:
1044 // Normal state: the map word contains a map pointer.
1045
1046 // Create a map word from a map pointer.
1047 static inline MapWord FromMap(Map* map);
1048
1049 // View this map word as a map pointer.
1050 inline Map* ToMap();
1051
1052
1053 // Scavenge collection: the map word of live objects in the from space
1054 // contains a forwarding address (a heap object pointer in the to space).
1055
1056 // True if this map word is a forwarding address for a scavenge
1057 // collection. Only valid during a scavenge collection (specifically,
1058 // when all map words are heap object pointers, ie. not during a full GC).
1059 inline bool IsForwardingAddress();
1060
1061 // Create a map word from a forwarding address.
1062 static inline MapWord FromForwardingAddress(HeapObject* object);
1063
1064 // View this map word as a forwarding address.
1065 inline HeapObject* ToForwardingAddress();
1066
Steve Blocka7e24c12009-10-30 11:49:00 +00001067 // Marking phase of full collection: the map word of live objects is
1068 // marked, and may be marked as overflowed (eg, the object is live, its
1069 // children have not been visited, and it does not fit in the marking
1070 // stack).
1071
1072 // True if this map word's mark bit is set.
1073 inline bool IsMarked();
1074
1075 // Return this map word but with its mark bit set.
1076 inline void SetMark();
1077
1078 // Return this map word but with its mark bit cleared.
1079 inline void ClearMark();
1080
1081 // True if this map word's overflow bit is set.
1082 inline bool IsOverflowed();
1083
1084 // Return this map word but with its overflow bit set.
1085 inline void SetOverflow();
1086
1087 // Return this map word but with its overflow bit cleared.
1088 inline void ClearOverflow();
1089
1090
1091 // Compacting phase of a full compacting collection: the map word of live
1092 // objects contains an encoding of the original map address along with the
1093 // forwarding address (represented as an offset from the first live object
1094 // in the same page as the (old) object address).
1095
1096 // Create a map word from a map address and a forwarding address offset.
1097 static inline MapWord EncodeAddress(Address map_address, int offset);
1098
1099 // Return the map address encoded in this map word.
1100 inline Address DecodeMapAddress(MapSpace* map_space);
1101
1102 // Return the forwarding offset encoded in this map word.
1103 inline int DecodeOffset();
1104
1105
1106 // During serialization: the map word is used to hold an encoded
1107 // address, and possibly a mark bit (set and cleared with SetMark
1108 // and ClearMark).
1109
1110 // Create a map word from an encoded address.
1111 static inline MapWord FromEncodedAddress(Address address);
1112
1113 inline Address ToEncodedAddress();
1114
1115 // Bits used by the marking phase of the garbage collector.
1116 //
1117 // The first word of a heap object is normally a map pointer. The last two
1118 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1119 // mark an object as live and/or overflowed:
1120 // last bit = 0, marked as alive
1121 // second bit = 1, overflowed
1122 // An object is only marked as overflowed when it is marked as live while
1123 // the marking stack is overflowed.
1124 static const int kMarkingBit = 0; // marking bit
1125 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1126 static const int kOverflowBit = 1; // overflow bit
1127 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1128
Leon Clarkee46be812010-01-19 14:06:41 +00001129 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1130 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +00001131 // +-----------------+------------------+-----------------+
1132 // |forwarding offset|page offset of map|page index of map|
1133 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +00001134 // ^ ^ ^
1135 // | | |
1136 // | | kMapPageIndexBits
1137 // | kMapPageOffsetBits
1138 // kForwardingOffsetBits
1139 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1140 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1141#ifdef V8_HOST_ARCH_64_BIT
1142 static const int kMapPageIndexBits = 16;
1143#else
1144 // Use all the 32-bits to encode on a 32-bit platform.
1145 static const int kMapPageIndexBits =
1146 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1147#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001148
1149 static const int kMapPageIndexShift = 0;
1150 static const int kMapPageOffsetShift =
1151 kMapPageIndexShift + kMapPageIndexBits;
1152 static const int kForwardingOffsetShift =
1153 kMapPageOffsetShift + kMapPageOffsetBits;
1154
Leon Clarkee46be812010-01-19 14:06:41 +00001155 // Bit masks covering the different parts the encoding.
1156 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001157 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +00001158 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001159 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +00001160 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +00001161 ~(kMapPageIndexMask | kMapPageOffsetMask);
1162
1163 private:
1164 // HeapObject calls the private constructor and directly reads the value.
1165 friend class HeapObject;
1166
1167 explicit MapWord(uintptr_t value) : value_(value) {}
1168
1169 uintptr_t value_;
1170};
1171
1172
1173// HeapObject is the superclass for all classes describing heap allocated
1174// objects.
1175class HeapObject: public Object {
1176 public:
1177 // [map]: Contains a map which contains the object's reflective
1178 // information.
1179 inline Map* map();
1180 inline void set_map(Map* value);
1181
1182 // During garbage collection, the map word of a heap object does not
1183 // necessarily contain a map pointer.
1184 inline MapWord map_word();
1185 inline void set_map_word(MapWord map_word);
1186
Steve Block44f0eee2011-05-26 01:26:41 +01001187 // The Heap the object was allocated in. Used also to access Isolate.
1188 // This method can not be used during GC, it ASSERTs this.
1189 inline Heap* GetHeap();
1190 // Convenience method to get current isolate. This method can be
1191 // accessed only when its result is the same as
1192 // Isolate::Current(), it ASSERTs this. See also comment for GetHeap.
1193 inline Isolate* GetIsolate();
1194
Steve Blocka7e24c12009-10-30 11:49:00 +00001195 // Converts an address to a HeapObject pointer.
1196 static inline HeapObject* FromAddress(Address address);
1197
1198 // Returns the address of this HeapObject.
1199 inline Address address();
1200
1201 // Iterates over pointers contained in the object (including the Map)
1202 void Iterate(ObjectVisitor* v);
1203
1204 // Iterates over all pointers contained in the object except the
1205 // first map pointer. The object type is given in the first
1206 // parameter. This function does not access the map pointer in the
1207 // object, and so is safe to call while the map pointer is modified.
1208 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1209
Steve Blocka7e24c12009-10-30 11:49:00 +00001210 // Returns the heap object's size in bytes
1211 inline int Size();
1212
1213 // Given a heap object's map pointer, returns the heap size in bytes
1214 // Useful when the map pointer field is used for other purposes.
1215 // GC internal.
1216 inline int SizeFromMap(Map* map);
1217
1218 // Support for the marking heap objects during the marking phase of GC.
1219 // True if the object is marked live.
1220 inline bool IsMarked();
1221
1222 // Mutate this object's map pointer to indicate that the object is live.
1223 inline void SetMark();
1224
1225 // Mutate this object's map pointer to remove the indication that the
1226 // object is live (ie, partially restore the map pointer).
1227 inline void ClearMark();
1228
1229 // True if this object is marked as overflowed. Overflowed objects have
1230 // been reached and marked during marking of the heap, but their children
1231 // have not necessarily been marked and they have not been pushed on the
1232 // marking stack.
1233 inline bool IsOverflowed();
1234
1235 // Mutate this object's map pointer to indicate that the object is
1236 // overflowed.
1237 inline void SetOverflow();
1238
1239 // Mutate this object's map pointer to remove the indication that the
1240 // object is overflowed (ie, partially restore the map pointer).
1241 inline void ClearOverflow();
1242
1243 // Returns the field at offset in obj, as a read/write Object* reference.
1244 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001245 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001246 // during marking GC.
1247 static inline Object** RawField(HeapObject* obj, int offset);
1248
1249 // Casting.
1250 static inline HeapObject* cast(Object* obj);
1251
Leon Clarke4515c472010-02-03 11:58:03 +00001252 // Return the write barrier mode for this. Callers of this function
1253 // must be able to present a reference to an AssertNoAllocation
1254 // object as a sign that they are not going to use this function
1255 // from code that allocates and thus invalidates the returned write
1256 // barrier mode.
1257 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001258
1259 // Dispatched behavior.
1260 void HeapObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001261#ifdef OBJECT_PRINT
1262 inline void HeapObjectPrint() {
1263 HeapObjectPrint(stdout);
1264 }
1265 void HeapObjectPrint(FILE* out);
1266#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001267#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001268 void HeapObjectVerify();
1269 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001270 inline void VerifySmiField(int offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001271#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001272
Ben Murdochb0fe1622011-05-05 13:52:32 +01001273#ifdef OBJECT_PRINT
1274 void PrintHeader(FILE* out, const char* id);
1275#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001276
Ben Murdochb0fe1622011-05-05 13:52:32 +01001277#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001278 // Verify a pointer is a valid HeapObject pointer that points to object
1279 // areas in the heap.
1280 static void VerifyHeapPointer(Object* p);
1281#endif
1282
1283 // Layout description.
1284 // First field in a heap object is map.
1285 static const int kMapOffset = Object::kHeaderSize;
1286 static const int kHeaderSize = kMapOffset + kPointerSize;
1287
1288 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1289
1290 protected:
1291 // helpers for calling an ObjectVisitor to iterate over pointers in the
1292 // half-open range [start, end) specified as integer offsets
1293 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1294 // as above, for the single element at "offset"
1295 inline void IteratePointer(ObjectVisitor* v, int offset);
1296
Steve Blocka7e24c12009-10-30 11:49:00 +00001297 private:
1298 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1299};
1300
1301
Iain Merrick75681382010-08-19 15:07:18 +01001302#define SLOT_ADDR(obj, offset) \
1303 reinterpret_cast<Object**>((obj)->address() + offset)
1304
1305// This class describes a body of an object of a fixed size
1306// in which all pointer fields are located in the [start_offset, end_offset)
1307// interval.
1308template<int start_offset, int end_offset, int size>
1309class FixedBodyDescriptor {
1310 public:
1311 static const int kStartOffset = start_offset;
1312 static const int kEndOffset = end_offset;
1313 static const int kSize = size;
1314
1315 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1316
1317 template<typename StaticVisitor>
1318 static inline void IterateBody(HeapObject* obj) {
1319 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1320 SLOT_ADDR(obj, end_offset));
1321 }
1322};
1323
1324
1325// This class describes a body of an object of a variable size
1326// in which all pointer fields are located in the [start_offset, object_size)
1327// interval.
1328template<int start_offset>
1329class FlexibleBodyDescriptor {
1330 public:
1331 static const int kStartOffset = start_offset;
1332
1333 static inline void IterateBody(HeapObject* obj,
1334 int object_size,
1335 ObjectVisitor* v);
1336
1337 template<typename StaticVisitor>
1338 static inline void IterateBody(HeapObject* obj, int object_size) {
1339 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1340 SLOT_ADDR(obj, object_size));
1341 }
1342};
1343
1344#undef SLOT_ADDR
1345
1346
Steve Blocka7e24c12009-10-30 11:49:00 +00001347// The HeapNumber class describes heap allocated numbers that cannot be
1348// represented in a Smi (small integer)
1349class HeapNumber: public HeapObject {
1350 public:
1351 // [value]: number value.
1352 inline double value();
1353 inline void set_value(double value);
1354
1355 // Casting.
1356 static inline HeapNumber* cast(Object* obj);
1357
1358 // Dispatched behavior.
1359 Object* HeapNumberToBoolean();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001360 inline void HeapNumberPrint() {
1361 HeapNumberPrint(stdout);
1362 }
1363 void HeapNumberPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001364 void HeapNumberPrint(StringStream* accumulator);
1365#ifdef DEBUG
1366 void HeapNumberVerify();
1367#endif
1368
Steve Block6ded16b2010-05-10 14:33:55 +01001369 inline int get_exponent();
1370 inline int get_sign();
1371
Steve Blocka7e24c12009-10-30 11:49:00 +00001372 // Layout description.
1373 static const int kValueOffset = HeapObject::kHeaderSize;
1374 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1375 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1376 // little endian apart from non-EABI arm which is little endian with big
1377 // endian floating point word ordering!
Steve Blocka7e24c12009-10-30 11:49:00 +00001378 static const int kMantissaOffset = kValueOffset;
1379 static const int kExponentOffset = kValueOffset + 4;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001380
Steve Blocka7e24c12009-10-30 11:49:00 +00001381 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001382 static const uint32_t kSignMask = 0x80000000u;
1383 static const uint32_t kExponentMask = 0x7ff00000u;
1384 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001385 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001386 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001387 static const int kExponentBias = 1023;
1388 static const int kExponentShift = 20;
1389 static const int kMantissaBitsInTopWord = 20;
1390 static const int kNonMantissaBitsInTopWord = 12;
1391
1392 private:
1393 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1394};
1395
1396
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001397// JSReceiver includes types on which properties can be defined, i.e.,
1398// JSObject and JSProxy.
1399class JSReceiver: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001400 public:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001401 enum DeleteMode {
1402 NORMAL_DELETION,
1403 STRICT_DELETION,
1404 FORCE_DELETION
1405 };
1406
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001407 // Casting.
1408 static inline JSReceiver* cast(Object* obj);
1409
1410 // Can cause GC.
1411 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1412 Object* value,
1413 PropertyAttributes attributes,
1414 StrictModeFlag strict_mode);
1415 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1416 String* key,
1417 Object* value,
1418 PropertyAttributes attributes,
1419 StrictModeFlag strict_mode);
1420
1421 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1422
1423 // Returns the class name ([[Class]] property in the specification).
1424 String* class_name();
1425
1426 // Returns the constructor name (the name (possibly, inferred name) of the
1427 // function that was used to instantiate the object).
1428 String* constructor_name();
1429
1430 inline PropertyAttributes GetPropertyAttribute(String* name);
1431 PropertyAttributes GetPropertyAttributeWithReceiver(JSReceiver* receiver,
1432 String* name);
1433 PropertyAttributes GetLocalPropertyAttribute(String* name);
1434
1435 // Can cause a GC.
1436 inline bool HasProperty(String* name);
1437 inline bool HasLocalProperty(String* name);
1438
1439 // Return the object's prototype (might be Heap::null_value()).
1440 inline Object* GetPrototype();
1441
1442 // Set the object's prototype (only JSReceiver and null are allowed).
1443 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1444 bool skip_hidden_prototypes);
1445
1446 // Lookup a property. If found, the result is valid and has
1447 // detailed information.
1448 void LocalLookup(String* name, LookupResult* result);
1449 void Lookup(String* name, LookupResult* result);
1450
1451 private:
1452 PropertyAttributes GetPropertyAttribute(JSReceiver* receiver,
1453 LookupResult* result,
1454 String* name,
1455 bool continue_search);
1456
1457 DISALLOW_IMPLICIT_CONSTRUCTORS(JSReceiver);
1458};
1459
1460// The JSObject describes real heap allocated JavaScript objects with
1461// properties.
1462// Note that the map of JSObject changes during execution to enable inline
1463// caching.
1464class JSObject: public JSReceiver {
1465 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00001466 enum ElementsKind {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001467 // The "fast" kind for tagged values. Must be first to make it possible
1468 // to efficiently check maps if they have fast elements.
Steve Blocka7e24c12009-10-30 11:49:00 +00001469 FAST_ELEMENTS,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001470
1471 // The "fast" kind for unwrapped, non-tagged double values.
1472 FAST_DOUBLE_ELEMENTS,
1473
1474 // The "slow" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001475 DICTIONARY_ELEMENTS,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001476 NON_STRICT_ARGUMENTS_ELEMENTS,
1477 // The "fast" kind for external arrays
Steve Block3ce2e202009-11-05 08:53:23 +00001478 EXTERNAL_BYTE_ELEMENTS,
1479 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1480 EXTERNAL_SHORT_ELEMENTS,
1481 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1482 EXTERNAL_INT_ELEMENTS,
1483 EXTERNAL_UNSIGNED_INT_ELEMENTS,
Steve Block44f0eee2011-05-26 01:26:41 +01001484 EXTERNAL_FLOAT_ELEMENTS,
Ben Murdoch257744e2011-11-30 15:57:28 +00001485 EXTERNAL_DOUBLE_ELEMENTS,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001486 EXTERNAL_PIXEL_ELEMENTS,
1487
1488 // Derived constants from ElementsKind
1489 FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_BYTE_ELEMENTS,
1490 LAST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS,
1491 FIRST_ELEMENTS_KIND = FAST_ELEMENTS,
1492 LAST_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001493 };
1494
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001495 static const int kElementsKindCount =
1496 LAST_ELEMENTS_KIND - FIRST_ELEMENTS_KIND + 1;
1497
Steve Blocka7e24c12009-10-30 11:49:00 +00001498 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001499 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001500 // slow case.
1501 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1502 inline void initialize_properties();
1503 inline bool HasFastProperties();
1504 inline StringDictionary* property_dictionary(); // Gets slow properties.
1505
1506 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001507 //
1508 // Elements can be in two general modes: fast and slow. Each mode
1509 // corrensponds to a set of object representations of elements that
1510 // have something in common.
1511 //
1512 // In the fast mode elements is a FixedArray and so each element can
1513 // be quickly accessed. This fact is used in the generated code. The
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001514 // elements array can have one of three maps in this mode:
1515 // fixed_array_map, non_strict_arguments_elements_map or
1516 // fixed_cow_array_map (for copy-on-write arrays). In the latter case
1517 // the elements array may be shared by a few objects and so before
1518 // writing to any element the array must be copied. Use
1519 // EnsureWritableFastElements in this case.
Iain Merrick75681382010-08-19 15:07:18 +01001520 //
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001521 // In the slow mode the elements is either a NumberDictionary, an
1522 // ExternalArray, or a FixedArray parameter map for a (non-strict)
1523 // arguments object.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001524 DECL_ACCESSORS(elements, FixedArrayBase)
Steve Blocka7e24c12009-10-30 11:49:00 +00001525 inline void initialize_elements();
John Reck59135872010-11-02 12:39:01 -07001526 MUST_USE_RESULT inline MaybeObject* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001527 inline ElementsKind GetElementsKind();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001528 inline ElementsAccessor* GetElementsAccessor();
Steve Blocka7e24c12009-10-30 11:49:00 +00001529 inline bool HasFastElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001530 inline bool HasFastDoubleElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001531 inline bool HasDictionaryElements();
Steve Block44f0eee2011-05-26 01:26:41 +01001532 inline bool HasExternalPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001533 inline bool HasExternalArrayElements();
1534 inline bool HasExternalByteElements();
1535 inline bool HasExternalUnsignedByteElements();
1536 inline bool HasExternalShortElements();
1537 inline bool HasExternalUnsignedShortElements();
1538 inline bool HasExternalIntElements();
1539 inline bool HasExternalUnsignedIntElements();
1540 inline bool HasExternalFloatElements();
Ben Murdoch257744e2011-11-30 15:57:28 +00001541 inline bool HasExternalDoubleElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001542 bool HasFastArgumentsElements();
1543 bool HasDictionaryArgumentsElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001544 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001545 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001546
1547 // Requires: HasFastElements().
John Reck59135872010-11-02 12:39:01 -07001548 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001549
1550 // Collects elements starting at index 0.
1551 // Undefined values are placed after non-undefined values.
1552 // Returns the number of non-undefined values.
John Reck59135872010-11-02 12:39:01 -07001553 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001554 // As PrepareElementsForSort, but only on objects where elements is
1555 // a dictionary, and it will stay a dictionary.
John Reck59135872010-11-02 12:39:01 -07001556 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00001557
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001558 MUST_USE_RESULT MaybeObject* SetPropertyForResult(LookupResult* result,
John Reck59135872010-11-02 12:39:01 -07001559 String* key,
1560 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001561 PropertyAttributes attributes,
1562 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001563 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1564 LookupResult* result,
1565 String* name,
Ben Murdoch086aeea2011-05-13 15:57:08 +01001566 Object* value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001567 bool check_prototype,
1568 StrictModeFlag strict_mode);
1569 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(
1570 Object* structure,
1571 String* name,
1572 Object* value,
1573 JSObject* holder,
1574 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001575 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1576 Object* value);
1577 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1578 String* name,
1579 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001580 PropertyAttributes attributes,
1581 StrictModeFlag strict_mode);
John Reck59135872010-11-02 12:39:01 -07001582 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1583 String* name,
1584 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001585 PropertyAttributes attributes,
1586 StrictModeFlag strict_mode);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001587 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
John Reck59135872010-11-02 12:39:01 -07001588 String* key,
1589 Object* value,
1590 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001591
1592 // Retrieve a value in a normalized object given a lookup result.
1593 // Handles the special representation of JS global objects.
1594 Object* GetNormalizedProperty(LookupResult* result);
1595
1596 // Sets the property value in a normalized object given a lookup result.
1597 // Handles the special representation of JS global objects.
1598 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1599
1600 // Sets the property value in a normalized object given (key, value, details).
1601 // Handles the special representation of JS global objects.
John Reck59135872010-11-02 12:39:01 -07001602 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1603 Object* value,
1604 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00001605
1606 // Deletes the named property in a normalized object.
John Reck59135872010-11-02 12:39:01 -07001607 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1608 DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001609
Steve Blocka7e24c12009-10-30 11:49:00 +00001610 // Retrieve interceptors.
1611 InterceptorInfo* GetNamedInterceptor();
1612 InterceptorInfo* GetIndexedInterceptor();
1613
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001614 // Used from JSReceiver.
1615 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1616 String* name,
1617 bool continue_search);
1618 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1619 String* name,
1620 bool continue_search);
1621 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1622 Object* receiver,
1623 LookupResult* result,
1624 String* name,
1625 bool continue_search);
Steve Blocka7e24c12009-10-30 11:49:00 +00001626
John Reck59135872010-11-02 12:39:01 -07001627 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1628 bool is_getter,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001629 Object* fun,
John Reck59135872010-11-02 12:39:01 -07001630 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001631 Object* LookupAccessor(String* name, bool is_getter);
1632
John Reck59135872010-11-02 12:39:01 -07001633 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
Leon Clarkef7060e22010-06-03 12:02:55 +01001634
Steve Blocka7e24c12009-10-30 11:49:00 +00001635 // Used from Object::GetProperty().
John Reck59135872010-11-02 12:39:01 -07001636 MaybeObject* GetPropertyWithFailedAccessCheck(
1637 Object* receiver,
1638 LookupResult* result,
1639 String* name,
1640 PropertyAttributes* attributes);
1641 MaybeObject* GetPropertyWithInterceptor(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001642 JSReceiver* receiver,
John Reck59135872010-11-02 12:39:01 -07001643 String* name,
1644 PropertyAttributes* attributes);
1645 MaybeObject* GetPropertyPostInterceptor(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001646 JSReceiver* receiver,
John Reck59135872010-11-02 12:39:01 -07001647 String* name,
1648 PropertyAttributes* attributes);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001649 MaybeObject* GetLocalPropertyPostInterceptor(JSReceiver* receiver,
John Reck59135872010-11-02 12:39:01 -07001650 String* name,
1651 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001652
1653 // Returns true if this is an instance of an api function and has
1654 // been modified since it was created. May give false positives.
1655 bool IsDirty();
1656
Steve Blockd0582a62009-12-15 09:54:21 +00001657 // If the receiver is a JSGlobalProxy this method will return its prototype,
1658 // otherwise the result is the receiver itself.
1659 inline Object* BypassGlobalProxy();
1660
1661 // Accessors for hidden properties object.
1662 //
1663 // Hidden properties are not local properties of the object itself.
1664 // Instead they are stored on an auxiliary JSObject stored as a local
1665 // property with a special name Heap::hidden_symbol(). But if the
1666 // receiver is a JSGlobalProxy then the auxiliary object is a property
1667 // of its prototype.
1668 //
1669 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1670 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1671 // holder.
1672 //
1673 // These accessors do not touch interceptors or accessors.
1674 inline bool HasHiddenPropertiesObject();
1675 inline Object* GetHiddenPropertiesObject();
John Reck59135872010-11-02 12:39:01 -07001676 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1677 Object* hidden_obj);
Steve Blockd0582a62009-12-15 09:54:21 +00001678
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001679 // Indicates whether the hidden properties object should be created.
1680 enum HiddenPropertiesFlag { ALLOW_CREATION, OMIT_CREATION };
1681
1682 // Retrieves the hidden properties object.
1683 //
1684 // The undefined value might be returned in case no hidden properties object
1685 // is present and creation was omitted.
1686 inline bool HasHiddenProperties();
1687 MUST_USE_RESULT MaybeObject* GetHiddenProperties(HiddenPropertiesFlag flag);
1688
1689 // Retrieves a permanent object identity hash code.
1690 //
1691 // The identity hash is stored as a hidden property. The undefined value might
1692 // be returned in case no hidden properties object is present and creation was
1693 // omitted.
1694 MUST_USE_RESULT MaybeObject* GetIdentityHash(HiddenPropertiesFlag flag);
1695
John Reck59135872010-11-02 12:39:01 -07001696 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1697 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001698
1699 // Tests for the fast common case for property enumeration.
1700 bool IsSimpleEnum();
1701
1702 // Do we want to keep the elements in fast case when increasing the
1703 // capacity?
1704 bool ShouldConvertToSlowElements(int new_capacity);
1705 // Returns true if the backing storage for the slow-case elements of
1706 // this object takes up nearly as much space as a fast-case backing
1707 // storage would. In that case the JSObject should have fast
1708 // elements.
1709 bool ShouldConvertToFastElements();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001710 // Returns true if the elements of JSObject contains only values that can be
1711 // represented in a FixedDoubleArray.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001712 bool CanConvertToFastDoubleElements();
Andrei Popescu402d9372010-02-26 13:31:12 +00001713
Steve Blocka7e24c12009-10-30 11:49:00 +00001714 // Tells whether the index'th element is present.
1715 inline bool HasElement(uint32_t index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001716 bool HasElementWithReceiver(JSReceiver* receiver, uint32_t index);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001717
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001718 // Computes the new capacity when expanding the elements of a JSObject.
1719 static int NewElementsCapacity(int old_capacity) {
1720 // (old_capacity + 50%) + 16
1721 return old_capacity + (old_capacity >> 1) + 16;
1722 }
1723
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001724 // Tells whether the index'th element is present and how it is stored.
1725 enum LocalElementType {
1726 // There is no element with given index.
1727 UNDEFINED_ELEMENT,
1728
1729 // Element with given index is handled by interceptor.
1730 INTERCEPTED_ELEMENT,
1731
1732 // Element with given index is character in string.
1733 STRING_CHARACTER_ELEMENT,
1734
1735 // Element with given index is stored in fast backing store.
1736 FAST_ELEMENT,
1737
1738 // Element with given index is stored in slow backing store.
1739 DICTIONARY_ELEMENT
1740 };
1741
1742 LocalElementType HasLocalElement(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001743
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001744 bool HasElementWithInterceptor(JSReceiver* receiver, uint32_t index);
1745 bool HasElementPostInterceptor(JSReceiver* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001746
Steve Block9fac8402011-05-12 15:51:54 +01001747 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1748 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001749 StrictModeFlag strict_mode,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001750 bool check_prototype);
1751 MUST_USE_RESULT MaybeObject* SetDictionaryElement(uint32_t index,
1752 Object* value,
1753 StrictModeFlag strict_mode,
1754 bool check_prototype);
1755
1756 MUST_USE_RESULT MaybeObject* SetFastDoubleElement(
1757 uint32_t index,
1758 Object* value,
1759 StrictModeFlag strict_mode,
1760 bool check_prototype = true);
Steve Blocka7e24c12009-10-30 11:49:00 +00001761
1762 // Set the index'th array element.
1763 // A Failure object is returned if GC is needed.
Steve Block9fac8402011-05-12 15:51:54 +01001764 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1765 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001766 StrictModeFlag strict_mode,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001767 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00001768
1769 // Returns the index'th element.
1770 // The undefined object if index is out of bounds.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001771 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
1772
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001773 // Replace the elements' backing store with fast elements of the given
1774 // capacity. Update the length for JSArrays. Returns the new backing
1775 // store.
John Reck59135872010-11-02 12:39:01 -07001776 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1777 int length);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001778 MUST_USE_RESULT MaybeObject* SetFastDoubleElementsCapacityAndLength(
1779 int capacity,
1780 int length);
John Reck59135872010-11-02 12:39:01 -07001781 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001782
1783 // Lookup interceptors are used for handling properties controlled by host
1784 // objects.
1785 inline bool HasNamedInterceptor();
1786 inline bool HasIndexedInterceptor();
1787
1788 // Support functions for v8 api (needed for correct interceptor behavior).
1789 bool HasRealNamedProperty(String* key);
1790 bool HasRealElementProperty(uint32_t index);
1791 bool HasRealNamedCallbackProperty(String* key);
1792
1793 // Initializes the array to a certain length
John Reck59135872010-11-02 12:39:01 -07001794 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001795
1796 // Get the header size for a JSObject. Used to compute the index of
1797 // internal fields as well as the number of internal fields.
1798 inline int GetHeaderSize();
1799
1800 inline int GetInternalFieldCount();
Steve Block44f0eee2011-05-26 01:26:41 +01001801 inline int GetInternalFieldOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001802 inline Object* GetInternalField(int index);
1803 inline void SetInternalField(int index, Object* value);
1804
1805 // Lookup a property. If found, the result is valid and has
1806 // detailed information.
1807 void LocalLookup(String* name, LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00001808
1809 // The following lookup functions skip interceptors.
1810 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1811 void LookupRealNamedProperty(String* name, LookupResult* result);
1812 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1813 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Steve Block1e0659c2011-05-24 12:43:12 +01001814 MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001815 uint32_t index, Object* value, bool* found, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001816 void LookupCallback(String* name, LookupResult* result);
1817
1818 // Returns the number of properties on this object filtering out properties
1819 // with the specified attributes (ignoring interceptors).
1820 int NumberOfLocalProperties(PropertyAttributes filter);
1821 // Returns the number of enumerable properties (ignoring interceptors).
1822 int NumberOfEnumProperties();
1823 // Fill in details for properties into storage starting at the specified
1824 // index.
1825 void GetLocalPropertyNames(FixedArray* storage, int index);
1826
1827 // Returns the number of properties on this object filtering out properties
1828 // with the specified attributes (ignoring interceptors).
1829 int NumberOfLocalElements(PropertyAttributes filter);
1830 // Returns the number of enumerable elements (ignoring interceptors).
1831 int NumberOfEnumElements();
1832 // Returns the number of elements on this object filtering out elements
1833 // with the specified attributes (ignoring interceptors).
1834 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1835 // Count and fill in the enumerable elements into storage.
1836 // (storage->length() == NumberOfEnumElements()).
1837 // If storage is NULL, will count the elements without adding
1838 // them to any storage.
1839 // Returns the number of enumerable elements.
1840 int GetEnumElementKeys(FixedArray* storage);
1841
1842 // Add a property to a fast-case object using a map transition to
1843 // new_map.
John Reck59135872010-11-02 12:39:01 -07001844 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1845 String* name,
1846 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001847
1848 // Add a constant function property to a fast-case object.
1849 // This leaves a CONSTANT_TRANSITION in the old map, and
1850 // if it is called on a second object with this map, a
1851 // normal property is added instead, with a map transition.
1852 // This avoids the creation of many maps with the same constant
1853 // function, all orphaned.
John Reck59135872010-11-02 12:39:01 -07001854 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1855 String* name,
1856 JSFunction* function,
1857 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001858
John Reck59135872010-11-02 12:39:01 -07001859 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1860 String* name,
1861 Object* value,
1862 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001863
1864 // Converts a descriptor of any other type to a real field,
1865 // backed by the properties array. Descriptors of visible
1866 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1867 // Converts the descriptor on the original object's map to a
1868 // map transition, and the the new field is on the object's new map.
John Reck59135872010-11-02 12:39:01 -07001869 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
Steve Blocka7e24c12009-10-30 11:49:00 +00001870 String* name,
1871 Object* new_value,
1872 PropertyAttributes attributes);
1873
1874 // Converts a descriptor of any other type to a real field,
1875 // backed by the properties array. Descriptors of visible
1876 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
John Reck59135872010-11-02 12:39:01 -07001877 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1878 String* name,
1879 Object* new_value,
1880 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001881
1882 // Add a property to a fast-case object.
John Reck59135872010-11-02 12:39:01 -07001883 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1884 Object* value,
1885 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001886
1887 // Add a property to a slow-case object.
John Reck59135872010-11-02 12:39:01 -07001888 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1889 Object* value,
1890 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001891
1892 // Add a property to an object.
John Reck59135872010-11-02 12:39:01 -07001893 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1894 Object* value,
Steve Block44f0eee2011-05-26 01:26:41 +01001895 PropertyAttributes attributes,
1896 StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001897
1898 // Convert the object to use the canonical dictionary
1899 // representation. If the object is expected to have additional properties
1900 // added this number can be indicated to have the backing store allocated to
1901 // an initial capacity for holding these properties.
John Reck59135872010-11-02 12:39:01 -07001902 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1903 PropertyNormalizationMode mode,
1904 int expected_additional_properties);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001905
1906 // Convert and update the elements backing store to be a NumberDictionary
1907 // dictionary. Returns the backing after conversion.
John Reck59135872010-11-02 12:39:01 -07001908 MUST_USE_RESULT MaybeObject* NormalizeElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001909
John Reck59135872010-11-02 12:39:01 -07001910 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001911
Steve Blocka7e24c12009-10-30 11:49:00 +00001912 // Transform slow named properties to fast variants.
1913 // Returns failure if allocation failed.
John Reck59135872010-11-02 12:39:01 -07001914 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1915 int unused_property_fields);
Steve Blocka7e24c12009-10-30 11:49:00 +00001916
1917 // Access fast-case object properties at index.
1918 inline Object* FastPropertyAt(int index);
1919 inline Object* FastPropertyAtPut(int index, Object* value);
1920
1921 // Access to in object properties.
Steve Block44f0eee2011-05-26 01:26:41 +01001922 inline int GetInObjectPropertyOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001923 inline Object* InObjectPropertyAt(int index);
1924 inline Object* InObjectPropertyAtPut(int index,
1925 Object* value,
1926 WriteBarrierMode mode
1927 = UPDATE_WRITE_BARRIER);
1928
1929 // initializes the body after properties slot, properties slot is
1930 // initialized by set_properties
1931 // Note: this call does not update write barrier, it is caller's
1932 // reponsibility to ensure that *v* can be collected without WB here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001933 inline void InitializeBody(int object_size, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001934
1935 // Check whether this object references another object
1936 bool ReferencesObject(Object* obj);
1937
1938 // Casting.
1939 static inline JSObject* cast(Object* obj);
1940
Steve Block8defd9f2010-07-08 12:39:36 +01001941 // Disalow further properties to be added to the object.
John Reck59135872010-11-02 12:39:01 -07001942 MUST_USE_RESULT MaybeObject* PreventExtensions();
Steve Block8defd9f2010-07-08 12:39:36 +01001943
1944
Steve Blocka7e24c12009-10-30 11:49:00 +00001945 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001946 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001947#ifdef OBJECT_PRINT
1948 inline void JSObjectPrint() {
1949 JSObjectPrint(stdout);
1950 }
1951 void JSObjectPrint(FILE* out);
1952#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00001953#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001954 void JSObjectVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001955#endif
1956#ifdef OBJECT_PRINT
1957 inline void PrintProperties() {
1958 PrintProperties(stdout);
1959 }
1960 void PrintProperties(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00001961
Ben Murdochb0fe1622011-05-05 13:52:32 +01001962 inline void PrintElements() {
1963 PrintElements(stdout);
1964 }
1965 void PrintElements(FILE* out);
1966#endif
1967
1968#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00001969 // Structure for collecting spill information about JSObjects.
1970 class SpillInformation {
1971 public:
1972 void Clear();
1973 void Print();
1974 int number_of_objects_;
1975 int number_of_objects_with_fast_properties_;
1976 int number_of_objects_with_fast_elements_;
1977 int number_of_fast_used_fields_;
1978 int number_of_fast_unused_fields_;
1979 int number_of_slow_used_properties_;
1980 int number_of_slow_unused_properties_;
1981 int number_of_fast_used_elements_;
1982 int number_of_fast_unused_elements_;
1983 int number_of_slow_used_elements_;
1984 int number_of_slow_unused_elements_;
1985 };
1986
1987 void IncrementSpillStatistics(SpillInformation* info);
1988#endif
1989 Object* SlowReverseLookup(Object* value);
1990
Steve Block8defd9f2010-07-08 12:39:36 +01001991 // Maximal number of fast properties for the JSObject. Used to
1992 // restrict the number of map transitions to avoid an explosion in
1993 // the number of maps for objects used as dictionaries.
1994 inline int MaxFastProperties();
1995
Leon Clarkee46be812010-01-19 14:06:41 +00001996 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1997 // Also maximal value of JSArray's length property.
1998 static const uint32_t kMaxElementCount = 0xffffffffu;
1999
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002000 // Constants for heuristics controlling conversion of fast elements
2001 // to slow elements.
2002
2003 // Maximal gap that can be introduced by adding an element beyond
2004 // the current elements length.
Steve Blocka7e24c12009-10-30 11:49:00 +00002005 static const uint32_t kMaxGap = 1024;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002006
2007 // Maximal length of fast elements array that won't be checked for
2008 // being dense enough on expansion.
2009 static const int kMaxUncheckedFastElementsLength = 5000;
2010
2011 // Same as above but for old arrays. This limit is more strict. We
2012 // don't want to be wasteful with long lived objects.
2013 static const int kMaxUncheckedOldFastElementsLength = 500;
2014
Steve Blocka7e24c12009-10-30 11:49:00 +00002015 static const int kInitialMaxFastElementArray = 100000;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002016 static const int kMaxFastProperties = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00002017 static const int kMaxInstanceSize = 255 * kPointerSize;
2018 // When extending the backing storage for property values, we increase
2019 // its size by more than the 1 entry necessary, so sequentially adding fields
2020 // to the same object requires fewer allocations and copies.
2021 static const int kFieldsAdded = 3;
2022
2023 // Layout description.
2024 static const int kPropertiesOffset = HeapObject::kHeaderSize;
2025 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
2026 static const int kHeaderSize = kElementsOffset + kPointerSize;
2027
2028 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
2029
Iain Merrick75681382010-08-19 15:07:18 +01002030 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
2031 public:
2032 static inline int SizeOf(Map* map, HeapObject* object);
2033 };
2034
Steve Blocka7e24c12009-10-30 11:49:00 +00002035 private:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002036 friend class DictionaryElementsAccessor;
2037
John Reck59135872010-11-02 12:39:01 -07002038 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
2039 Object* structure,
2040 uint32_t index,
2041 Object* holder);
2042 MaybeObject* SetElementWithCallback(Object* structure,
2043 uint32_t index,
2044 Object* value,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002045 JSObject* holder,
2046 StrictModeFlag strict_mode);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002047 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
2048 uint32_t index,
2049 Object* value,
2050 StrictModeFlag strict_mode,
2051 bool check_prototype);
Steve Block9fac8402011-05-12 15:51:54 +01002052 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
2053 uint32_t index,
2054 Object* value,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002055 StrictModeFlag strict_mode,
Steve Block9fac8402011-05-12 15:51:54 +01002056 bool check_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00002057
John Reck59135872010-11-02 12:39:01 -07002058 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
2059 DeleteMode mode);
2060 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00002061
John Reck59135872010-11-02 12:39:01 -07002062 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002063
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002064 MUST_USE_RESULT MaybeObject* DeleteFastElement(uint32_t index);
2065 MUST_USE_RESULT MaybeObject* DeleteDictionaryElement(uint32_t index,
2066 DeleteMode mode);
2067
2068 bool ReferencesObjectFromElements(FixedArray* elements,
2069 ElementsKind kind,
2070 Object* object);
2071 bool HasElementInElements(FixedArray* elements,
2072 ElementsKind kind,
2073 uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002074
2075 // Returns true if most of the elements backing storage is used.
2076 bool HasDenseElements();
2077
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002078 // Gets the current elements capacity and the number of used elements.
2079 void GetElementsCapacityAndUsage(int* capacity, int* used);
2080
Leon Clarkef7060e22010-06-03 12:02:55 +01002081 bool CanSetCallback(String* name);
John Reck59135872010-11-02 12:39:01 -07002082 MUST_USE_RESULT MaybeObject* SetElementCallback(
2083 uint32_t index,
2084 Object* structure,
2085 PropertyAttributes attributes);
2086 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
2087 String* name,
2088 Object* structure,
2089 PropertyAttributes attributes);
2090 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
2091 String* name,
2092 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00002093
2094 void LookupInDescriptor(String* name, LookupResult* result);
2095
2096 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
2097};
2098
2099
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002100// Common superclass for FixedArrays that allow implementations to share
2101// common accessors and some code paths.
2102class FixedArrayBase: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002103 public:
2104 // [length]: length of the array.
2105 inline int length();
2106 inline void set_length(int value);
2107
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002108 inline static FixedArrayBase* cast(Object* object);
2109
2110 // Layout description.
2111 // Length is smi tagged when it is stored.
2112 static const int kLengthOffset = HeapObject::kHeaderSize;
2113 static const int kHeaderSize = kLengthOffset + kPointerSize;
2114};
2115
2116
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002117class FixedDoubleArray;
2118
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002119// FixedArray describes fixed-sized arrays with element type Object*.
2120class FixedArray: public FixedArrayBase {
2121 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00002122 // Setter and getter for elements.
2123 inline Object* get(int index);
2124 // Setter that uses write barrier.
2125 inline void set(int index, Object* value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002126 inline bool is_the_hole(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002127
2128 // Setter that doesn't need write barrier).
2129 inline void set(int index, Smi* value);
2130 // Setter with explicit barrier mode.
2131 inline void set(int index, Object* value, WriteBarrierMode mode);
2132
2133 // Setters for frequently used oddballs located in old space.
2134 inline void set_undefined(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01002135 // TODO(isolates): duplicate.
2136 inline void set_undefined(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002137 inline void set_null(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01002138 // TODO(isolates): duplicate.
2139 inline void set_null(Heap* heap, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002140 inline void set_the_hole(int index);
2141
Iain Merrick75681382010-08-19 15:07:18 +01002142 // Setters with less debug checks for the GC to use.
2143 inline void set_unchecked(int index, Smi* value);
Steve Block44f0eee2011-05-26 01:26:41 +01002144 inline void set_null_unchecked(Heap* heap, int index);
2145 inline void set_unchecked(Heap* heap, int index, Object* value,
2146 WriteBarrierMode mode);
Iain Merrick75681382010-08-19 15:07:18 +01002147
Steve Block6ded16b2010-05-10 14:33:55 +01002148 // Gives access to raw memory which stores the array's data.
2149 inline Object** data_start();
2150
Steve Blocka7e24c12009-10-30 11:49:00 +00002151 // Copy operations.
John Reck59135872010-11-02 12:39:01 -07002152 MUST_USE_RESULT inline MaybeObject* Copy();
2153 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002154
2155 // Add the elements of a JSArray to this FixedArray.
John Reck59135872010-11-02 12:39:01 -07002156 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00002157
2158 // Compute the union of this and other.
John Reck59135872010-11-02 12:39:01 -07002159 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
Steve Blocka7e24c12009-10-30 11:49:00 +00002160
2161 // Copy a sub array from the receiver to dest.
2162 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
2163
2164 // Garbage collection support.
2165 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
2166
2167 // Code Generation support.
2168 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2169
2170 // Casting.
2171 static inline FixedArray* cast(Object* obj);
2172
Leon Clarkee46be812010-01-19 14:06:41 +00002173 // Maximal allowed size, in bytes, of a single FixedArray.
2174 // Prevents overflowing size computations, as well as extreme memory
2175 // consumption.
2176 static const int kMaxSize = 512 * MB;
2177 // Maximally allowed length of a FixedArray.
2178 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002179
2180 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002181#ifdef OBJECT_PRINT
2182 inline void FixedArrayPrint() {
2183 FixedArrayPrint(stdout);
2184 }
2185 void FixedArrayPrint(FILE* out);
2186#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002187#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002188 void FixedArrayVerify();
2189 // Checks if two FixedArrays have identical contents.
2190 bool IsEqualTo(FixedArray* other);
2191#endif
2192
2193 // Swap two elements in a pair of arrays. If this array and the
2194 // numbers array are the same object, the elements are only swapped
2195 // once.
2196 void SwapPairs(FixedArray* numbers, int i, int j);
2197
2198 // Sort prefix of this array and the numbers array as pairs wrt. the
2199 // numbers. If the numbers array and the this array are the same
2200 // object, the prefix of this array is sorted.
2201 void SortPairs(FixedArray* numbers, uint32_t len);
2202
Iain Merrick75681382010-08-19 15:07:18 +01002203 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2204 public:
2205 static inline int SizeOf(Map* map, HeapObject* object) {
2206 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2207 }
2208 };
2209
Steve Blocka7e24c12009-10-30 11:49:00 +00002210 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00002211 // Set operation on FixedArray without using write barriers. Can
2212 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00002213 static inline void fast_set(FixedArray* array, int index, Object* value);
2214
2215 private:
2216 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2217};
2218
2219
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002220// FixedDoubleArray describes fixed-sized arrays with element type double.
2221class FixedDoubleArray: public FixedArrayBase {
2222 public:
2223 inline void Initialize(FixedArray* from);
2224 inline void Initialize(FixedDoubleArray* from);
2225 inline void Initialize(NumberDictionary* from);
2226
2227 // Setter and getter for elements.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002228 inline double get_scalar(int index);
2229 inline MaybeObject* get(int index);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002230 inline void set(int index, double value);
2231 inline void set_the_hole(int index);
2232
2233 // Checking for the hole.
2234 inline bool is_the_hole(int index);
2235
2236 // Garbage collection support.
2237 inline static int SizeFor(int length) {
2238 return kHeaderSize + length * kDoubleSize;
2239 }
2240
2241 // Code Generation support.
2242 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2243
2244 inline static bool is_the_hole_nan(double value);
2245 inline static double hole_nan_as_double();
2246 inline static double canonical_not_the_hole_nan_as_double();
2247
2248 // Casting.
2249 static inline FixedDoubleArray* cast(Object* obj);
2250
2251 // Maximal allowed size, in bytes, of a single FixedDoubleArray.
2252 // Prevents overflowing size computations, as well as extreme memory
2253 // consumption.
2254 static const int kMaxSize = 512 * MB;
2255 // Maximally allowed length of a FixedArray.
2256 static const int kMaxLength = (kMaxSize - kHeaderSize) / kDoubleSize;
2257
2258 // Dispatched behavior.
2259#ifdef OBJECT_PRINT
2260 inline void FixedDoubleArrayPrint() {
2261 FixedDoubleArrayPrint(stdout);
2262 }
2263 void FixedDoubleArrayPrint(FILE* out);
2264#endif
2265
2266#ifdef DEBUG
2267 void FixedDoubleArrayVerify();
2268#endif
2269
2270 private:
2271 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedDoubleArray);
2272};
2273
2274
Steve Blocka7e24c12009-10-30 11:49:00 +00002275// DescriptorArrays are fixed arrays used to hold instance descriptors.
2276// The format of the these objects is:
Ben Murdoch257744e2011-11-30 15:57:28 +00002277// TODO(1399): It should be possible to make room for bit_field3 in the map
2278// without overloading the instance descriptors field in the map
2279// (and storing it in the DescriptorArray when the map has one).
2280// [0]: storage for bit_field3 for Map owning this object (Smi)
2281// [1]: point to a fixed array with (value, detail) pairs.
2282// [2]: next enumeration index (Smi), or pointer to small fixed array:
Steve Blocka7e24c12009-10-30 11:49:00 +00002283// [0]: next enumeration index (Smi)
2284// [1]: pointer to fixed array with enum cache
Ben Murdoch257744e2011-11-30 15:57:28 +00002285// [3]: first key
Steve Blocka7e24c12009-10-30 11:49:00 +00002286// [length() - 1]: last key
2287//
2288class DescriptorArray: public FixedArray {
2289 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00002290 // Returns true for both shared empty_descriptor_array and for smis, which the
2291 // map uses to encode additional bit fields when the descriptor array is not
2292 // yet used.
Steve Blocka7e24c12009-10-30 11:49:00 +00002293 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00002294
Steve Blocka7e24c12009-10-30 11:49:00 +00002295 // Returns the number of descriptors in the array.
2296 int number_of_descriptors() {
Steve Block44f0eee2011-05-26 01:26:41 +01002297 ASSERT(length() > kFirstIndex || IsEmpty());
2298 int len = length();
2299 return len <= kFirstIndex ? 0 : len - kFirstIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00002300 }
2301
2302 int NextEnumerationIndex() {
2303 if (IsEmpty()) return PropertyDetails::kInitialIndex;
2304 Object* obj = get(kEnumerationIndexIndex);
2305 if (obj->IsSmi()) {
2306 return Smi::cast(obj)->value();
2307 } else {
2308 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2309 return Smi::cast(index)->value();
2310 }
2311 }
2312
2313 // Set next enumeration index and flush any enum cache.
2314 void SetNextEnumerationIndex(int value) {
2315 if (!IsEmpty()) {
2316 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2317 }
2318 }
2319 bool HasEnumCache() {
2320 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
2321 }
2322
2323 Object* GetEnumCache() {
2324 ASSERT(HasEnumCache());
2325 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2326 return bridge->get(kEnumCacheBridgeCacheIndex);
2327 }
2328
Ben Murdoch257744e2011-11-30 15:57:28 +00002329 // TODO(1399): It should be possible to make room for bit_field3 in the map
2330 // without overloading the instance descriptors field in the map
2331 // (and storing it in the DescriptorArray when the map has one).
2332 inline int bit_field3_storage();
2333 inline void set_bit_field3_storage(int value);
2334
Steve Blocka7e24c12009-10-30 11:49:00 +00002335 // Initialize or change the enum cache,
2336 // using the supplied storage for the small "bridge".
2337 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2338
2339 // Accessors for fetching instance descriptor at descriptor number.
2340 inline String* GetKey(int descriptor_number);
2341 inline Object* GetValue(int descriptor_number);
2342 inline Smi* GetDetails(int descriptor_number);
2343 inline PropertyType GetType(int descriptor_number);
2344 inline int GetFieldIndex(int descriptor_number);
2345 inline JSFunction* GetConstantFunction(int descriptor_number);
2346 inline Object* GetCallbacksObject(int descriptor_number);
2347 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2348 inline bool IsProperty(int descriptor_number);
2349 inline bool IsTransition(int descriptor_number);
2350 inline bool IsNullDescriptor(int descriptor_number);
2351 inline bool IsDontEnum(int descriptor_number);
2352
2353 // Accessor for complete descriptor.
2354 inline void Get(int descriptor_number, Descriptor* desc);
2355 inline void Set(int descriptor_number, Descriptor* desc);
2356
2357 // Transfer complete descriptor from another descriptor array to
2358 // this one.
2359 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2360
2361 // Copy the descriptor array, insert a new descriptor and optionally
2362 // remove map transitions. If the descriptor is already present, it is
2363 // replaced. If a replaced descriptor is a real property (not a transition
2364 // or null), its enumeration index is kept as is.
2365 // If adding a real property, map transitions must be removed. If adding
2366 // a transition, they must not be removed. All null descriptors are removed.
John Reck59135872010-11-02 12:39:01 -07002367 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2368 TransitionFlag transition_flag);
Steve Blocka7e24c12009-10-30 11:49:00 +00002369
2370 // Remove all transitions. Return a copy of the array with all transitions
2371 // removed, or a Failure object if the new array could not be allocated.
John Reck59135872010-11-02 12:39:01 -07002372 MUST_USE_RESULT MaybeObject* RemoveTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00002373
2374 // Sort the instance descriptors by the hash codes of their keys.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002375 // Does not check for duplicates.
2376 void SortUnchecked();
2377
2378 // Sort the instance descriptors by the hash codes of their keys.
2379 // Checks the result for duplicates.
Steve Blocka7e24c12009-10-30 11:49:00 +00002380 void Sort();
2381
2382 // Search the instance descriptors for given name.
2383 inline int Search(String* name);
2384
Iain Merrick75681382010-08-19 15:07:18 +01002385 // As the above, but uses DescriptorLookupCache and updates it when
2386 // necessary.
2387 inline int SearchWithCache(String* name);
2388
Steve Blocka7e24c12009-10-30 11:49:00 +00002389 // Tells whether the name is present int the array.
2390 bool Contains(String* name) { return kNotFound != Search(name); }
2391
2392 // Perform a binary search in the instance descriptors represented
2393 // by this fixed array. low and high are descriptor indices. If there
2394 // are three instance descriptors in this array it should be called
2395 // with low=0 and high=2.
2396 int BinarySearch(String* name, int low, int high);
2397
2398 // Perform a linear search in the instance descriptors represented
2399 // by this fixed array. len is the number of descriptor indices that are
2400 // valid. Does not require the descriptors to be sorted.
2401 int LinearSearch(String* name, int len);
2402
2403 // Allocates a DescriptorArray, but returns the singleton
2404 // empty descriptor array object if number_of_descriptors is 0.
John Reck59135872010-11-02 12:39:01 -07002405 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
Steve Blocka7e24c12009-10-30 11:49:00 +00002406
2407 // Casting.
2408 static inline DescriptorArray* cast(Object* obj);
2409
2410 // Constant for denoting key was not found.
2411 static const int kNotFound = -1;
2412
Ben Murdoch257744e2011-11-30 15:57:28 +00002413 static const int kBitField3StorageIndex = 0;
2414 static const int kContentArrayIndex = 1;
2415 static const int kEnumerationIndexIndex = 2;
2416 static const int kFirstIndex = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00002417
2418 // The length of the "bridge" to the enum cache.
2419 static const int kEnumCacheBridgeLength = 2;
2420 static const int kEnumCacheBridgeEnumIndex = 0;
2421 static const int kEnumCacheBridgeCacheIndex = 1;
2422
2423 // Layout description.
Ben Murdoch257744e2011-11-30 15:57:28 +00002424 static const int kBitField3StorageOffset = FixedArray::kHeaderSize;
2425 static const int kContentArrayOffset = kBitField3StorageOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002426 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2427 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2428
2429 // Layout description for the bridge array.
2430 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2431 static const int kEnumCacheBridgeCacheOffset =
2432 kEnumCacheBridgeEnumOffset + kPointerSize;
2433
Ben Murdochb0fe1622011-05-05 13:52:32 +01002434#ifdef OBJECT_PRINT
Steve Blocka7e24c12009-10-30 11:49:00 +00002435 // Print all the descriptors.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002436 inline void PrintDescriptors() {
2437 PrintDescriptors(stdout);
2438 }
2439 void PrintDescriptors(FILE* out);
2440#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002441
Ben Murdochb0fe1622011-05-05 13:52:32 +01002442#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002443 // Is the descriptor array sorted and without duplicates?
2444 bool IsSortedNoDuplicates();
2445
2446 // Are two DescriptorArrays equal?
2447 bool IsEqualTo(DescriptorArray* other);
2448#endif
2449
2450 // The maximum number of descriptors we want in a descriptor array (should
2451 // fit in a page).
2452 static const int kMaxNumberOfDescriptors = 1024 + 512;
2453
2454 private:
2455 // Conversion from descriptor number to array indices.
2456 static int ToKeyIndex(int descriptor_number) {
2457 return descriptor_number+kFirstIndex;
2458 }
Leon Clarkee46be812010-01-19 14:06:41 +00002459
2460 static int ToDetailsIndex(int descriptor_number) {
2461 return (descriptor_number << 1) + 1;
2462 }
2463
Steve Blocka7e24c12009-10-30 11:49:00 +00002464 static int ToValueIndex(int descriptor_number) {
2465 return descriptor_number << 1;
2466 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002467
2468 bool is_null_descriptor(int descriptor_number) {
2469 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2470 NULL_DESCRIPTOR;
2471 }
2472 // Swap operation on FixedArray without using write barriers.
2473 static inline void fast_swap(FixedArray* array, int first, int second);
2474
2475 // Swap descriptor first and second.
2476 inline void Swap(int first, int second);
2477
2478 FixedArray* GetContentArray() {
2479 return FixedArray::cast(get(kContentArrayIndex));
2480 }
2481 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2482};
2483
2484
2485// HashTable is a subclass of FixedArray that implements a hash table
2486// that uses open addressing and quadratic probing.
2487//
2488// In order for the quadratic probing to work, elements that have not
2489// yet been used and elements that have been deleted are
2490// distinguished. Probing continues when deleted elements are
2491// encountered and stops when unused elements are encountered.
2492//
2493// - Elements with key == undefined have not been used yet.
2494// - Elements with key == null have been deleted.
2495//
2496// The hash table class is parameterized with a Shape and a Key.
2497// Shape must be a class with the following interface:
2498// class ExampleShape {
2499// public:
2500// // Tells whether key matches other.
2501// static bool IsMatch(Key key, Object* other);
2502// // Returns the hash value for key.
2503// static uint32_t Hash(Key key);
2504// // Returns the hash value for object.
2505// static uint32_t HashForObject(Key key, Object* object);
2506// // Convert key to an object.
2507// static inline Object* AsObject(Key key);
2508// // The prefix size indicates number of elements in the beginning
2509// // of the backing storage.
2510// static const int kPrefixSize = ..;
2511// // The Element size indicates number of elements per entry.
2512// static const int kEntrySize = ..;
2513// };
Steve Block3ce2e202009-11-05 08:53:23 +00002514// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002515// beginning of the backing storage that can be used for non-element
2516// information by subclasses.
2517
2518template<typename Shape, typename Key>
2519class HashTable: public FixedArray {
2520 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002521 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002522 int NumberOfElements() {
2523 return Smi::cast(get(kNumberOfElementsIndex))->value();
2524 }
2525
Leon Clarkee46be812010-01-19 14:06:41 +00002526 // Returns the number of deleted elements in the hash table.
2527 int NumberOfDeletedElements() {
2528 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2529 }
2530
Steve Block3ce2e202009-11-05 08:53:23 +00002531 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002532 int Capacity() {
2533 return Smi::cast(get(kCapacityIndex))->value();
2534 }
2535
2536 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002537 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002538 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2539
2540 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002541 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002542 void ElementRemoved() {
2543 SetNumberOfElements(NumberOfElements() - 1);
2544 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2545 }
2546 void ElementsRemoved(int n) {
2547 SetNumberOfElements(NumberOfElements() - n);
2548 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2549 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002550
Steve Block3ce2e202009-11-05 08:53:23 +00002551 // Returns a new HashTable object. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002552 MUST_USE_RESULT static MaybeObject* Allocate(
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002553 int at_least_space_for,
2554 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002555
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002556 // Computes the required capacity for a table holding the given
2557 // number of elements. May be more than HashTable::kMaxCapacity.
2558 static int ComputeCapacity(int at_least_space_for);
2559
Steve Blocka7e24c12009-10-30 11:49:00 +00002560 // Returns the key at entry.
2561 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2562
2563 // Tells whether k is a real key. Null and undefined are not allowed
2564 // as keys and can be used to indicate missing or deleted elements.
2565 bool IsKey(Object* k) {
2566 return !k->IsNull() && !k->IsUndefined();
2567 }
2568
2569 // Garbage collection support.
2570 void IteratePrefix(ObjectVisitor* visitor);
2571 void IterateElements(ObjectVisitor* visitor);
2572
2573 // Casting.
2574 static inline HashTable* cast(Object* obj);
2575
2576 // Compute the probe offset (quadratic probing).
2577 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2578 return (n + n * n) >> 1;
2579 }
2580
2581 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002582 static const int kNumberOfDeletedElementsIndex = 1;
2583 static const int kCapacityIndex = 2;
2584 static const int kPrefixStartIndex = 3;
2585 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002586 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002587 static const int kEntrySize = Shape::kEntrySize;
2588 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002589 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002590 static const int kCapacityOffset =
2591 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002592
2593 // Constant used for denoting a absent entry.
2594 static const int kNotFound = -1;
2595
Leon Clarkee46be812010-01-19 14:06:41 +00002596 // Maximal capacity of HashTable. Based on maximal length of underlying
2597 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2598 // cannot overflow.
2599 static const int kMaxCapacity =
2600 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2601
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002602 // Find entry for key otherwise return kNotFound.
Steve Block44f0eee2011-05-26 01:26:41 +01002603 inline int FindEntry(Key key);
2604 int FindEntry(Isolate* isolate, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002605
2606 protected:
Steve Blocka7e24c12009-10-30 11:49:00 +00002607 // Find the entry at which to insert element with the given key that
2608 // has the given hash value.
2609 uint32_t FindInsertionEntry(uint32_t hash);
2610
2611 // Returns the index for an entry (of the key)
2612 static inline int EntryToIndex(int entry) {
2613 return (entry * kEntrySize) + kElementsStartIndex;
2614 }
2615
Steve Block3ce2e202009-11-05 08:53:23 +00002616 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002617 void SetNumberOfElements(int nof) {
2618 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2619 }
2620
Leon Clarkee46be812010-01-19 14:06:41 +00002621 // Update the number of deleted elements in the hash table.
2622 void SetNumberOfDeletedElements(int nod) {
2623 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2624 }
2625
Steve Blocka7e24c12009-10-30 11:49:00 +00002626 // Sets the capacity of the hash table.
2627 void SetCapacity(int capacity) {
2628 // To scale a computed hash code to fit within the hash table, we
2629 // use bit-wise AND with a mask, so the capacity must be positive
2630 // and non-zero.
2631 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002632 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002633 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2634 }
2635
2636
2637 // Returns probe entry.
2638 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2639 ASSERT(IsPowerOf2(size));
2640 return (hash + GetProbeOffset(number)) & (size - 1);
2641 }
2642
Leon Clarkee46be812010-01-19 14:06:41 +00002643 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2644 return hash & (size - 1);
2645 }
2646
2647 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2648 return (last + number) & (size - 1);
2649 }
2650
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002651 // Rehashes this hash-table into the new table.
2652 MUST_USE_RESULT MaybeObject* Rehash(HashTable* new_table, Key key);
2653
2654 // Attempt to shrink hash table after removal of key.
2655 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2656
Steve Blocka7e24c12009-10-30 11:49:00 +00002657 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002658 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002659};
2660
2661
2662
2663// HashTableKey is an abstract superclass for virtual key behavior.
2664class HashTableKey {
2665 public:
2666 // Returns whether the other object matches this key.
2667 virtual bool IsMatch(Object* other) = 0;
2668 // Returns the hash value for this key.
2669 virtual uint32_t Hash() = 0;
2670 // Returns the hash value for object.
2671 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002672 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002673 // If allocations fails a failure object is returned.
John Reck59135872010-11-02 12:39:01 -07002674 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00002675 // Required.
2676 virtual ~HashTableKey() {}
2677};
2678
2679class SymbolTableShape {
2680 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002681 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002682 return key->IsMatch(value);
2683 }
Steve Block44f0eee2011-05-26 01:26:41 +01002684 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002685 return key->Hash();
2686 }
Steve Block44f0eee2011-05-26 01:26:41 +01002687 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002688 return key->HashForObject(object);
2689 }
Steve Block44f0eee2011-05-26 01:26:41 +01002690 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002691 return key->AsObject();
2692 }
2693
2694 static const int kPrefixSize = 0;
2695 static const int kEntrySize = 1;
2696};
2697
Ben Murdoch257744e2011-11-30 15:57:28 +00002698class SeqAsciiString;
2699
Steve Blocka7e24c12009-10-30 11:49:00 +00002700// SymbolTable.
2701//
2702// No special elements in the prefix and the element size is 1
2703// because only the symbol itself (the key) needs to be stored.
2704class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2705 public:
2706 // Find symbol in the symbol table. If it is not there yet, it is
2707 // added. The return value is the symbol table which might have
2708 // been enlarged. If the return value is not a failure, the symbol
2709 // pointer *s is set to the symbol found.
John Reck59135872010-11-02 12:39:01 -07002710 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002711 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2712 Object** s);
Ben Murdoch257744e2011-11-30 15:57:28 +00002713 MUST_USE_RESULT MaybeObject* LookupSubStringAsciiSymbol(
2714 Handle<SeqAsciiString> str,
2715 int from,
2716 int length,
2717 Object** s);
Steve Block9fac8402011-05-12 15:51:54 +01002718 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2719 Object** s);
John Reck59135872010-11-02 12:39:01 -07002720 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002721
2722 // Looks up a symbol that is equal to the given string and returns
2723 // true if it is found, assigning the symbol to the given output
2724 // parameter.
2725 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002726 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002727
2728 // Casting.
2729 static inline SymbolTable* cast(Object* obj);
2730
2731 private:
John Reck59135872010-11-02 12:39:01 -07002732 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
Steve Blocka7e24c12009-10-30 11:49:00 +00002733
2734 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2735};
2736
2737
2738class MapCacheShape {
2739 public:
Steve Block44f0eee2011-05-26 01:26:41 +01002740 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002741 return key->IsMatch(value);
2742 }
Steve Block44f0eee2011-05-26 01:26:41 +01002743 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002744 return key->Hash();
2745 }
2746
Steve Block44f0eee2011-05-26 01:26:41 +01002747 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002748 return key->HashForObject(object);
2749 }
2750
Steve Block44f0eee2011-05-26 01:26:41 +01002751 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002752 return key->AsObject();
2753 }
2754
2755 static const int kPrefixSize = 0;
2756 static const int kEntrySize = 2;
2757};
2758
2759
2760// MapCache.
2761//
2762// Maps keys that are a fixed array of symbols to a map.
2763// Used for canonicalize maps for object literals.
2764class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2765 public:
2766 // Find cached value for a string key, otherwise return null.
2767 Object* Lookup(FixedArray* key);
John Reck59135872010-11-02 12:39:01 -07002768 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002769 static inline MapCache* cast(Object* obj);
2770
2771 private:
2772 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2773};
2774
2775
2776template <typename Shape, typename Key>
2777class Dictionary: public HashTable<Shape, Key> {
2778 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00002779 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2780 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2781 }
2782
2783 // Returns the value at entry.
2784 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002785 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002786 }
2787
2788 // Set the value for entry.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002789 // Returns false if the put wasn't performed due to property being read only.
2790 // Returns true on successful put.
2791 bool ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002792 // Check that this value can actually be written.
2793 PropertyDetails details = DetailsAt(entry);
2794 // If a value has not been initilized we allow writing to it even if
2795 // it is read only (a declared const that has not been initialized).
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002796 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2797 return false;
2798 }
2799 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2800 return true;
Steve Blocka7e24c12009-10-30 11:49:00 +00002801 }
2802
2803 // Returns the property details for the property at entry.
2804 PropertyDetails DetailsAt(int entry) {
2805 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2806 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002807 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002808 }
2809
2810 // Set the details for entry.
2811 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002812 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002813 }
2814
2815 // Sorting support
2816 void CopyValuesTo(FixedArray* elements);
2817
2818 // Delete a property from the dictionary.
2819 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2820
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002821 // Attempt to shrink the dictionary after deletion of key.
2822 MUST_USE_RESULT MaybeObject* Shrink(Key key);
2823
Steve Blocka7e24c12009-10-30 11:49:00 +00002824 // Returns the number of elements in the dictionary filtering out properties
2825 // with the specified attributes.
2826 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2827
2828 // Returns the number of enumerable elements in the dictionary.
2829 int NumberOfEnumElements();
2830
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002831 enum SortMode { UNSORTED, SORTED };
Steve Blocka7e24c12009-10-30 11:49:00 +00002832 // Copies keys to preallocated fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002833 void CopyKeysTo(FixedArray* storage,
2834 PropertyAttributes filter,
2835 SortMode sort_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002836 // Fill in details for properties into storage.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002837 void CopyKeysTo(FixedArray* storage, int index, SortMode sort_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002838
2839 // Accessors for next enumeration index.
2840 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002841 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002842 }
2843
2844 int NextEnumerationIndex() {
2845 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2846 }
2847
2848 // Returns a new array for dictionary usage. Might return Failure.
John Reck59135872010-11-02 12:39:01 -07002849 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
Steve Blocka7e24c12009-10-30 11:49:00 +00002850
2851 // Ensure enough space for n additional elements.
John Reck59135872010-11-02 12:39:01 -07002852 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002853
Ben Murdochb0fe1622011-05-05 13:52:32 +01002854#ifdef OBJECT_PRINT
2855 inline void Print() {
2856 Print(stdout);
2857 }
2858 void Print(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00002859#endif
2860 // Returns the key (slow).
2861 Object* SlowReverseLookup(Object* value);
2862
2863 // Sets the entry to (key, value) pair.
2864 inline void SetEntry(int entry,
2865 Object* key,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002866 Object* value);
2867 inline void SetEntry(int entry,
2868 Object* key,
Steve Blocka7e24c12009-10-30 11:49:00 +00002869 Object* value,
2870 PropertyDetails details);
2871
John Reck59135872010-11-02 12:39:01 -07002872 MUST_USE_RESULT MaybeObject* Add(Key key,
2873 Object* value,
2874 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002875
2876 protected:
2877 // Generic at put operation.
John Reck59135872010-11-02 12:39:01 -07002878 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002879
2880 // Add entry to dictionary.
John Reck59135872010-11-02 12:39:01 -07002881 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2882 Object* value,
2883 PropertyDetails details,
2884 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00002885
2886 // Generate new enumeration indices to avoid enumeration index overflow.
John Reck59135872010-11-02 12:39:01 -07002887 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
Steve Blocka7e24c12009-10-30 11:49:00 +00002888 static const int kMaxNumberKeyIndex =
2889 HashTable<Shape, Key>::kPrefixStartIndex;
2890 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2891};
2892
2893
2894class StringDictionaryShape {
2895 public:
2896 static inline bool IsMatch(String* key, Object* other);
2897 static inline uint32_t Hash(String* key);
2898 static inline uint32_t HashForObject(String* key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002899 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002900 static const int kPrefixSize = 2;
2901 static const int kEntrySize = 3;
2902 static const bool kIsEnumerable = true;
2903};
2904
2905
2906class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2907 public:
2908 static inline StringDictionary* cast(Object* obj) {
2909 ASSERT(obj->IsDictionary());
2910 return reinterpret_cast<StringDictionary*>(obj);
2911 }
2912
2913 // Copies enumerable keys to preallocated fixed array.
2914 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2915
2916 // For transforming properties of a JSObject.
John Reck59135872010-11-02 12:39:01 -07002917 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2918 JSObject* obj,
2919 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002920
2921 // Find entry for key otherwise return kNotFound. Optimzed version of
2922 // HashTable::FindEntry.
2923 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002924};
2925
2926
2927class NumberDictionaryShape {
2928 public:
2929 static inline bool IsMatch(uint32_t key, Object* other);
2930 static inline uint32_t Hash(uint32_t key);
2931 static inline uint32_t HashForObject(uint32_t key, Object* object);
John Reck59135872010-11-02 12:39:01 -07002932 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002933 static const int kPrefixSize = 2;
2934 static const int kEntrySize = 3;
2935 static const bool kIsEnumerable = false;
2936};
2937
2938
2939class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2940 public:
2941 static NumberDictionary* cast(Object* obj) {
2942 ASSERT(obj->IsDictionary());
2943 return reinterpret_cast<NumberDictionary*>(obj);
2944 }
2945
2946 // Type specific at put (default NONE attributes is used when adding).
John Reck59135872010-11-02 12:39:01 -07002947 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2948 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2949 Object* value,
2950 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002951
2952 // Set an existing entry or add a new one if needed.
John Reck59135872010-11-02 12:39:01 -07002953 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2954 Object* value,
2955 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00002956
2957 void UpdateMaxNumberKey(uint32_t key);
2958
2959 // If slow elements are required we will never go back to fast-case
2960 // for the elements kept in this dictionary. We require slow
2961 // elements if an element has been added at an index larger than
2962 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2963 // when defining a getter or setter with a number key.
2964 inline bool requires_slow_elements();
2965 inline void set_requires_slow_elements();
2966
2967 // Get the value of the max number key that has been added to this
2968 // dictionary. max_number_key can only be called if
2969 // requires_slow_elements returns false.
2970 inline uint32_t max_number_key();
2971
2972 // Remove all entries were key is a number and (from <= key && key < to).
2973 void RemoveNumberEntries(uint32_t from, uint32_t to);
2974
2975 // Bit masks.
2976 static const int kRequiresSlowElementsMask = 1;
2977 static const int kRequiresSlowElementsTagSize = 1;
2978 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2979};
2980
2981
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002982class ObjectHashTableShape {
2983 public:
2984 static inline bool IsMatch(JSObject* key, Object* other);
2985 static inline uint32_t Hash(JSObject* key);
2986 static inline uint32_t HashForObject(JSObject* key, Object* object);
2987 MUST_USE_RESULT static inline MaybeObject* AsObject(JSObject* key);
2988 static const int kPrefixSize = 0;
2989 static const int kEntrySize = 2;
2990};
2991
2992
2993// ObjectHashTable maps keys that are JavaScript objects to object values by
2994// using the identity hash of the key for hashing purposes.
2995class ObjectHashTable: public HashTable<ObjectHashTableShape, JSObject*> {
2996 public:
2997 static inline ObjectHashTable* cast(Object* obj) {
2998 ASSERT(obj->IsHashTable());
2999 return reinterpret_cast<ObjectHashTable*>(obj);
3000 }
3001
3002 // Looks up the value associated with the given key. The undefined value is
3003 // returned in case the key is not present.
3004 Object* Lookup(JSObject* key);
3005
3006 // Adds (or overwrites) the value associated with the given key. Mapping a
3007 // key to the undefined value causes removal of the whole entry.
3008 MUST_USE_RESULT MaybeObject* Put(JSObject* key, Object* value);
3009
3010 private:
3011 friend class MarkCompactCollector;
3012
3013 void AddEntry(int entry, JSObject* key, Object* value);
3014 void RemoveEntry(int entry, Heap* heap);
3015 inline void RemoveEntry(int entry);
3016
3017 // Returns the index to the value of an entry.
3018 static inline int EntryToValueIndex(int entry) {
3019 return EntryToIndex(entry) + 1;
3020 }
3021};
3022
3023
Steve Block6ded16b2010-05-10 14:33:55 +01003024// JSFunctionResultCache caches results of some JSFunction invocation.
3025// It is a fixed array with fixed structure:
3026// [0]: factory function
3027// [1]: finger index
3028// [2]: current cache size
3029// [3]: dummy field.
3030// The rest of array are key/value pairs.
3031class JSFunctionResultCache: public FixedArray {
3032 public:
3033 static const int kFactoryIndex = 0;
3034 static const int kFingerIndex = kFactoryIndex + 1;
3035 static const int kCacheSizeIndex = kFingerIndex + 1;
3036 static const int kDummyIndex = kCacheSizeIndex + 1;
3037 static const int kEntriesIndex = kDummyIndex + 1;
3038
3039 static const int kEntrySize = 2; // key + value
3040
Kristian Monsen25f61362010-05-21 11:50:48 +01003041 static const int kFactoryOffset = kHeaderSize;
3042 static const int kFingerOffset = kFactoryOffset + kPointerSize;
3043 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
3044
Steve Block6ded16b2010-05-10 14:33:55 +01003045 inline void MakeZeroSize();
3046 inline void Clear();
3047
Ben Murdochb8e0da22011-05-16 14:20:40 +01003048 inline int size();
3049 inline void set_size(int size);
3050 inline int finger_index();
3051 inline void set_finger_index(int finger_index);
3052
Steve Block6ded16b2010-05-10 14:33:55 +01003053 // Casting
3054 static inline JSFunctionResultCache* cast(Object* obj);
3055
3056#ifdef DEBUG
3057 void JSFunctionResultCacheVerify();
3058#endif
3059};
3060
3061
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003062// The cache for maps used by normalized (dictionary mode) objects.
3063// Such maps do not have property descriptors, so a typical program
3064// needs very limited number of distinct normalized maps.
3065class NormalizedMapCache: public FixedArray {
3066 public:
3067 static const int kEntries = 64;
3068
John Reck59135872010-11-02 12:39:01 -07003069 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
3070 PropertyNormalizationMode mode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003071
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003072 void Clear();
3073
3074 // Casting
3075 static inline NormalizedMapCache* cast(Object* obj);
3076
3077#ifdef DEBUG
3078 void NormalizedMapCacheVerify();
3079#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003080};
3081
3082
Steve Blocka7e24c12009-10-30 11:49:00 +00003083// ByteArray represents fixed sized byte arrays. Used by the outside world,
3084// such as PCRE, and also by the memory allocator and garbage collector to
3085// fill in free blocks in the heap.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003086class ByteArray: public FixedArrayBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00003087 public:
3088 // Setter and getter.
3089 inline byte get(int index);
3090 inline void set(int index, byte value);
3091
3092 // Treat contents as an int array.
3093 inline int get_int(int index);
3094
3095 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003096 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00003097 }
3098 // We use byte arrays for free blocks in the heap. Given a desired size in
3099 // bytes that is a multiple of the word size and big enough to hold a byte
3100 // array, this function returns the number of elements a byte array should
3101 // have.
3102 static int LengthFor(int size_in_bytes) {
3103 ASSERT(IsAligned(size_in_bytes, kPointerSize));
3104 ASSERT(size_in_bytes >= kHeaderSize);
3105 return size_in_bytes - kHeaderSize;
3106 }
3107
3108 // Returns data start address.
3109 inline Address GetDataStartAddress();
3110
3111 // Returns a pointer to the ByteArray object for a given data start address.
3112 static inline ByteArray* FromDataStartAddress(Address address);
3113
3114 // Casting.
3115 static inline ByteArray* cast(Object* obj);
3116
3117 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01003118 inline int ByteArraySize() {
3119 return SizeFor(this->length());
3120 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003121#ifdef OBJECT_PRINT
3122 inline void ByteArrayPrint() {
3123 ByteArrayPrint(stdout);
3124 }
3125 void ByteArrayPrint(FILE* out);
3126#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003127#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003128 void ByteArrayVerify();
3129#endif
3130
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003131 // Layout description.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003132 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003133
Leon Clarkee46be812010-01-19 14:06:41 +00003134 // Maximal memory consumption for a single ByteArray.
3135 static const int kMaxSize = 512 * MB;
3136 // Maximal length of a single ByteArray.
3137 static const int kMaxLength = kMaxSize - kHeaderSize;
3138
Steve Blocka7e24c12009-10-30 11:49:00 +00003139 private:
3140 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
3141};
3142
3143
Steve Block3ce2e202009-11-05 08:53:23 +00003144// An ExternalArray represents a fixed-size array of primitive values
3145// which live outside the JavaScript heap. Its subclasses are used to
3146// implement the CanvasArray types being defined in the WebGL
3147// specification. As of this writing the first public draft is not yet
3148// available, but Khronos members can access the draft at:
3149// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
3150//
3151// The semantics of these arrays differ from CanvasPixelArray.
3152// Out-of-range values passed to the setter are converted via a C
3153// cast, not clamping. Out-of-range indices cause exceptions to be
3154// raised rather than being silently ignored.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003155class ExternalArray: public FixedArrayBase {
Steve Block3ce2e202009-11-05 08:53:23 +00003156 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003157
3158 inline bool is_the_hole(int index) { return false; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003159
Steve Block3ce2e202009-11-05 08:53:23 +00003160 // [external_pointer]: The pointer to the external memory area backing this
3161 // external array.
3162 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
3163
3164 // Casting.
3165 static inline ExternalArray* cast(Object* obj);
3166
3167 // Maximal acceptable length for an external array.
3168 static const int kMaxLength = 0x3fffffff;
3169
3170 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003171 static const int kExternalPointerOffset =
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003172 POINTER_SIZE_ALIGN(FixedArrayBase::kLengthOffset + kPointerSize);
Steve Block3ce2e202009-11-05 08:53:23 +00003173 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003174 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00003175
3176 private:
3177 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
3178};
3179
3180
Steve Block44f0eee2011-05-26 01:26:41 +01003181// A ExternalPixelArray represents a fixed-size byte array with special
3182// semantics used for implementing the CanvasPixelArray object. Please see the
3183// specification at:
3184
3185// http://www.whatwg.org/specs/web-apps/current-work/
3186// multipage/the-canvas-element.html#canvaspixelarray
3187// In particular, write access clamps the value written to 0 or 255 if the
3188// value written is outside this range.
3189class ExternalPixelArray: public ExternalArray {
3190 public:
3191 inline uint8_t* external_pixel_pointer();
3192
3193 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003194 inline uint8_t get_scalar(int index);
3195 inline MaybeObject* get(int index);
Steve Block44f0eee2011-05-26 01:26:41 +01003196 inline void set(int index, uint8_t value);
3197
3198 // This accessor applies the correct conversion from Smi, HeapNumber and
3199 // undefined and clamps the converted value between 0 and 255.
3200 Object* SetValue(uint32_t index, Object* value);
3201
3202 // Casting.
3203 static inline ExternalPixelArray* cast(Object* obj);
3204
3205#ifdef OBJECT_PRINT
3206 inline void ExternalPixelArrayPrint() {
3207 ExternalPixelArrayPrint(stdout);
3208 }
3209 void ExternalPixelArrayPrint(FILE* out);
3210#endif
3211#ifdef DEBUG
3212 void ExternalPixelArrayVerify();
3213#endif // DEBUG
3214
3215 private:
3216 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
3217};
3218
3219
Steve Block3ce2e202009-11-05 08:53:23 +00003220class ExternalByteArray: public ExternalArray {
3221 public:
3222 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003223 inline int8_t get_scalar(int index);
3224 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003225 inline void set(int index, int8_t value);
3226
3227 // This accessor applies the correct conversion from Smi, HeapNumber
3228 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003229 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003230
3231 // Casting.
3232 static inline ExternalByteArray* cast(Object* obj);
3233
Ben Murdochb0fe1622011-05-05 13:52:32 +01003234#ifdef OBJECT_PRINT
3235 inline void ExternalByteArrayPrint() {
3236 ExternalByteArrayPrint(stdout);
3237 }
3238 void ExternalByteArrayPrint(FILE* out);
3239#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003240#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003241 void ExternalByteArrayVerify();
3242#endif // DEBUG
3243
3244 private:
3245 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
3246};
3247
3248
3249class ExternalUnsignedByteArray: public ExternalArray {
3250 public:
3251 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003252 inline uint8_t get_scalar(int index);
3253 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003254 inline void set(int index, uint8_t value);
3255
3256 // This accessor applies the correct conversion from Smi, HeapNumber
3257 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003258 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003259
3260 // Casting.
3261 static inline ExternalUnsignedByteArray* cast(Object* obj);
3262
Ben Murdochb0fe1622011-05-05 13:52:32 +01003263#ifdef OBJECT_PRINT
3264 inline void ExternalUnsignedByteArrayPrint() {
3265 ExternalUnsignedByteArrayPrint(stdout);
3266 }
3267 void ExternalUnsignedByteArrayPrint(FILE* out);
3268#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003269#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003270 void ExternalUnsignedByteArrayVerify();
3271#endif // DEBUG
3272
3273 private:
3274 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
3275};
3276
3277
3278class ExternalShortArray: public ExternalArray {
3279 public:
3280 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003281 inline int16_t get_scalar(int index);
3282 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003283 inline void set(int index, int16_t value);
3284
3285 // This accessor applies the correct conversion from Smi, HeapNumber
3286 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003287 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003288
3289 // Casting.
3290 static inline ExternalShortArray* cast(Object* obj);
3291
Ben Murdochb0fe1622011-05-05 13:52:32 +01003292#ifdef OBJECT_PRINT
3293 inline void ExternalShortArrayPrint() {
3294 ExternalShortArrayPrint(stdout);
3295 }
3296 void ExternalShortArrayPrint(FILE* out);
3297#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003298#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003299 void ExternalShortArrayVerify();
3300#endif // DEBUG
3301
3302 private:
3303 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
3304};
3305
3306
3307class ExternalUnsignedShortArray: public ExternalArray {
3308 public:
3309 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003310 inline uint16_t get_scalar(int index);
3311 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003312 inline void set(int index, uint16_t value);
3313
3314 // This accessor applies the correct conversion from Smi, HeapNumber
3315 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003316 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003317
3318 // Casting.
3319 static inline ExternalUnsignedShortArray* cast(Object* obj);
3320
Ben Murdochb0fe1622011-05-05 13:52:32 +01003321#ifdef OBJECT_PRINT
3322 inline void ExternalUnsignedShortArrayPrint() {
3323 ExternalUnsignedShortArrayPrint(stdout);
3324 }
3325 void ExternalUnsignedShortArrayPrint(FILE* out);
3326#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003327#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003328 void ExternalUnsignedShortArrayVerify();
3329#endif // DEBUG
3330
3331 private:
3332 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3333};
3334
3335
3336class ExternalIntArray: public ExternalArray {
3337 public:
3338 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003339 inline int32_t get_scalar(int index);
3340 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003341 inline void set(int index, int32_t value);
3342
3343 // This accessor applies the correct conversion from Smi, HeapNumber
3344 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003345 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003346
3347 // Casting.
3348 static inline ExternalIntArray* cast(Object* obj);
3349
Ben Murdochb0fe1622011-05-05 13:52:32 +01003350#ifdef OBJECT_PRINT
3351 inline void ExternalIntArrayPrint() {
3352 ExternalIntArrayPrint(stdout);
3353 }
3354 void ExternalIntArrayPrint(FILE* out);
3355#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003356#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003357 void ExternalIntArrayVerify();
3358#endif // DEBUG
3359
3360 private:
3361 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3362};
3363
3364
3365class ExternalUnsignedIntArray: public ExternalArray {
3366 public:
3367 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003368 inline uint32_t get_scalar(int index);
3369 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003370 inline void set(int index, uint32_t value);
3371
3372 // This accessor applies the correct conversion from Smi, HeapNumber
3373 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003374 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003375
3376 // Casting.
3377 static inline ExternalUnsignedIntArray* cast(Object* obj);
3378
Ben Murdochb0fe1622011-05-05 13:52:32 +01003379#ifdef OBJECT_PRINT
3380 inline void ExternalUnsignedIntArrayPrint() {
3381 ExternalUnsignedIntArrayPrint(stdout);
3382 }
3383 void ExternalUnsignedIntArrayPrint(FILE* out);
3384#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003385#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003386 void ExternalUnsignedIntArrayVerify();
3387#endif // DEBUG
3388
3389 private:
3390 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3391};
3392
3393
3394class ExternalFloatArray: public ExternalArray {
3395 public:
3396 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003397 inline float get_scalar(int index);
3398 inline MaybeObject* get(int index);
Steve Block3ce2e202009-11-05 08:53:23 +00003399 inline void set(int index, float value);
3400
3401 // This accessor applies the correct conversion from Smi, HeapNumber
3402 // and undefined.
John Reck59135872010-11-02 12:39:01 -07003403 MaybeObject* SetValue(uint32_t index, Object* value);
Steve Block3ce2e202009-11-05 08:53:23 +00003404
3405 // Casting.
3406 static inline ExternalFloatArray* cast(Object* obj);
3407
Ben Murdochb0fe1622011-05-05 13:52:32 +01003408#ifdef OBJECT_PRINT
3409 inline void ExternalFloatArrayPrint() {
3410 ExternalFloatArrayPrint(stdout);
3411 }
3412 void ExternalFloatArrayPrint(FILE* out);
3413#endif
Steve Block3ce2e202009-11-05 08:53:23 +00003414#ifdef DEBUG
Steve Block3ce2e202009-11-05 08:53:23 +00003415 void ExternalFloatArrayVerify();
3416#endif // DEBUG
3417
3418 private:
3419 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3420};
3421
3422
Ben Murdoch257744e2011-11-30 15:57:28 +00003423class ExternalDoubleArray: public ExternalArray {
3424 public:
3425 // Setter and getter.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003426 inline double get_scalar(int index);
3427 inline MaybeObject* get(int index);
Ben Murdoch257744e2011-11-30 15:57:28 +00003428 inline void set(int index, double value);
3429
3430 // This accessor applies the correct conversion from Smi, HeapNumber
3431 // and undefined.
3432 MaybeObject* SetValue(uint32_t index, Object* value);
3433
3434 // Casting.
3435 static inline ExternalDoubleArray* cast(Object* obj);
3436
3437#ifdef OBJECT_PRINT
3438 inline void ExternalDoubleArrayPrint() {
3439 ExternalDoubleArrayPrint(stdout);
3440 }
3441 void ExternalDoubleArrayPrint(FILE* out);
3442#endif // OBJECT_PRINT
3443#ifdef DEBUG
3444 void ExternalDoubleArrayVerify();
3445#endif // DEBUG
3446
3447 private:
3448 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalDoubleArray);
3449};
3450
3451
Ben Murdochb0fe1622011-05-05 13:52:32 +01003452// DeoptimizationInputData is a fixed array used to hold the deoptimization
3453// data for code generated by the Hydrogen/Lithium compiler. It also
3454// contains information about functions that were inlined. If N different
3455// functions were inlined then first N elements of the literal array will
3456// contain these functions.
3457//
3458// It can be empty.
3459class DeoptimizationInputData: public FixedArray {
3460 public:
3461 // Layout description. Indices in the array.
3462 static const int kTranslationByteArrayIndex = 0;
3463 static const int kInlinedFunctionCountIndex = 1;
3464 static const int kLiteralArrayIndex = 2;
3465 static const int kOsrAstIdIndex = 3;
3466 static const int kOsrPcOffsetIndex = 4;
3467 static const int kFirstDeoptEntryIndex = 5;
3468
3469 // Offsets of deopt entry elements relative to the start of the entry.
3470 static const int kAstIdOffset = 0;
3471 static const int kTranslationIndexOffset = 1;
3472 static const int kArgumentsStackHeightOffset = 2;
3473 static const int kDeoptEntrySize = 3;
3474
3475 // Simple element accessors.
3476#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3477 type* name() { \
3478 return type::cast(get(k##name##Index)); \
3479 } \
3480 void Set##name(type* value) { \
3481 set(k##name##Index, value); \
3482 }
3483
3484 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3485 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3486 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3487 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3488 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3489
3490 // Unchecked accessor to be used during GC.
3491 FixedArray* UncheckedLiteralArray() {
3492 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3493 }
3494
3495#undef DEFINE_ELEMENT_ACCESSORS
3496
3497 // Accessors for elements of the ith deoptimization entry.
3498#define DEFINE_ENTRY_ACCESSORS(name, type) \
3499 type* name(int i) { \
3500 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3501 } \
3502 void Set##name(int i, type* value) { \
3503 set(IndexForEntry(i) + k##name##Offset, value); \
3504 }
3505
3506 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3507 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3508 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3509
3510#undef DEFINE_ENTRY_ACCESSORS
3511
3512 int DeoptCount() {
3513 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3514 }
3515
3516 // Allocates a DeoptimizationInputData.
3517 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3518 PretenureFlag pretenure);
3519
3520 // Casting.
3521 static inline DeoptimizationInputData* cast(Object* obj);
3522
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003523#ifdef ENABLE_DISASSEMBLER
Ben Murdochb0fe1622011-05-05 13:52:32 +01003524 void DeoptimizationInputDataPrint(FILE* out);
3525#endif
3526
3527 private:
3528 static int IndexForEntry(int i) {
3529 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3530 }
3531
3532 static int LengthFor(int entry_count) {
3533 return IndexForEntry(entry_count);
3534 }
3535};
3536
3537
3538// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3539// data for code generated by the full compiler.
3540// The format of the these objects is
3541// [i * 2]: Ast ID for ith deoptimization.
3542// [i * 2 + 1]: PC and state of ith deoptimization
3543class DeoptimizationOutputData: public FixedArray {
3544 public:
3545 int DeoptPoints() { return length() / 2; }
3546 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3547 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3548 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3549 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3550
3551 static int LengthOfFixedArray(int deopt_points) {
3552 return deopt_points * 2;
3553 }
3554
3555 // Allocates a DeoptimizationOutputData.
3556 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3557 PretenureFlag pretenure);
3558
3559 // Casting.
3560 static inline DeoptimizationOutputData* cast(Object* obj);
3561
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003562#if defined(OBJECT_PRINT) || defined(ENABLE_DISASSEMBLER)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003563 void DeoptimizationOutputDataPrint(FILE* out);
3564#endif
3565};
3566
3567
Ben Murdochb8e0da22011-05-16 14:20:40 +01003568class SafepointEntry;
3569
3570
Steve Blocka7e24c12009-10-30 11:49:00 +00003571// Code describes objects with on-the-fly generated machine code.
3572class Code: public HeapObject {
3573 public:
3574 // Opaque data type for encapsulating code flags like kind, inline
3575 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01003576 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3577 // enumeration type has correct value range (see Issue 830 for more details).
3578 enum Flags {
3579 FLAGS_MIN_VALUE = kMinInt,
3580 FLAGS_MAX_VALUE = kMaxInt
3581 };
Steve Blocka7e24c12009-10-30 11:49:00 +00003582
3583 enum Kind {
3584 FUNCTION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003585 OPTIMIZED_FUNCTION,
Steve Blocka7e24c12009-10-30 11:49:00 +00003586 STUB,
3587 BUILTIN,
3588 LOAD_IC,
3589 KEYED_LOAD_IC,
3590 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003591 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00003592 STORE_IC,
3593 KEYED_STORE_IC,
Ben Murdoch257744e2011-11-30 15:57:28 +00003594 UNARY_OP_IC,
3595 BINARY_OP_IC,
Ben Murdochb0fe1622011-05-05 13:52:32 +01003596 COMPARE_IC,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003597 TO_BOOLEAN_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01003598 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00003599 // Flags.
3600
3601 // Pseudo-kinds.
3602 REGEXP = BUILTIN,
3603 FIRST_IC_KIND = LOAD_IC,
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003604 LAST_IC_KIND = TO_BOOLEAN_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00003605 };
3606
3607 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01003608 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00003609 };
3610
Ben Murdochb8e0da22011-05-16 14:20:40 +01003611 typedef int ExtraICState;
3612
3613 static const ExtraICState kNoExtraICState = 0;
3614
Steve Blocka7e24c12009-10-30 11:49:00 +00003615#ifdef ENABLE_DISASSEMBLER
3616 // Printing
3617 static const char* Kind2String(Kind kind);
3618 static const char* ICState2String(InlineCacheState state);
3619 static const char* PropertyType2String(PropertyType type);
Steve Block1e0659c2011-05-24 12:43:12 +01003620 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003621 inline void Disassemble(const char* name) {
3622 Disassemble(name, stdout);
3623 }
3624 void Disassemble(const char* name, FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00003625#endif // ENABLE_DISASSEMBLER
3626
3627 // [instruction_size]: Size of the native instructions
3628 inline int instruction_size();
3629 inline void set_instruction_size(int value);
3630
Leon Clarkeac952652010-07-15 11:15:24 +01003631 // [relocation_info]: Code relocation information
3632 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01003633 void InvalidateRelocation();
Leon Clarkeac952652010-07-15 11:15:24 +01003634
Ben Murdochb0fe1622011-05-05 13:52:32 +01003635 // [deoptimization_data]: Array containing data for deopt.
3636 DECL_ACCESSORS(deoptimization_data, FixedArray)
3637
Ben Murdoch257744e2011-11-30 15:57:28 +00003638 // [code_flushing_candidate]: Field only used during garbage
3639 // collection to hold code flushing candidates. The contents of this
3640 // field does not have to be traced during garbage collection since
3641 // it is only used by the garbage collector itself.
3642 DECL_ACCESSORS(next_code_flushing_candidate, Object)
3643
Ben Murdochb0fe1622011-05-05 13:52:32 +01003644 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01003645 inline ByteArray* unchecked_relocation_info();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003646 inline FixedArray* unchecked_deoptimization_data();
Leon Clarkeac952652010-07-15 11:15:24 +01003647
Steve Blocka7e24c12009-10-30 11:49:00 +00003648 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00003649
Steve Blocka7e24c12009-10-30 11:49:00 +00003650 // [flags]: Various code flags.
3651 inline Flags flags();
3652 inline void set_flags(Flags flags);
3653
3654 // [flags]: Access to specific code flags.
3655 inline Kind kind();
3656 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003657 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00003658 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
3659 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3660 inline int arguments_count(); // Only valid for call IC stubs.
3661
3662 // Testers for IC stub kinds.
3663 inline bool is_inline_cache_stub();
3664 inline bool is_load_stub() { return kind() == LOAD_IC; }
3665 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3666 inline bool is_store_stub() { return kind() == STORE_IC; }
3667 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3668 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003669 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003670 inline bool is_unary_op_stub() { return kind() == UNARY_OP_IC; }
3671 inline bool is_binary_op_stub() { return kind() == BINARY_OP_IC; }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003672 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003673 inline bool is_to_boolean_ic_stub() { return kind() == TO_BOOLEAN_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00003674
Steve Block6ded16b2010-05-10 14:33:55 +01003675 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003676 inline int major_key();
Ben Murdochb0fe1622011-05-05 13:52:32 +01003677 inline void set_major_key(int value);
3678
3679 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3680 inline bool optimizable();
3681 inline void set_optimizable(bool value);
3682
3683 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3684 // deoptimization support.
3685 inline bool has_deoptimization_support();
3686 inline void set_has_deoptimization_support(bool value);
3687
3688 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3689 // how long the function has been marked for OSR and therefore which
3690 // level of loop nesting we are willing to do on-stack replacement
3691 // for.
3692 inline void set_allow_osr_at_loop_nesting_level(int level);
3693 inline int allow_osr_at_loop_nesting_level();
3694
3695 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3696 // reserved in the code prologue.
3697 inline unsigned stack_slots();
3698 inline void set_stack_slots(unsigned slots);
3699
3700 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3701 // the instruction stream where the safepoint table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003702 inline unsigned safepoint_table_offset();
3703 inline void set_safepoint_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003704
3705 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3706 // instruction stream where the stack check table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01003707 inline unsigned stack_check_table_offset();
3708 inline void set_stack_check_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003709
3710 // [check type]: For kind CALL_IC, tells how to check if the
3711 // receiver is valid for the given call.
3712 inline CheckType check_type();
3713 inline void set_check_type(CheckType value);
3714
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003715 // [type-recording unary op type]: For kind UNARY_OP_IC.
Ben Murdoch257744e2011-11-30 15:57:28 +00003716 inline byte unary_op_type();
3717 inline void set_unary_op_type(byte value);
3718
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003719 // [type-recording binary op type]: For kind BINARY_OP_IC.
Ben Murdoch257744e2011-11-30 15:57:28 +00003720 inline byte binary_op_type();
3721 inline void set_binary_op_type(byte value);
3722 inline byte binary_op_result_type();
3723 inline void set_binary_op_result_type(byte value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003724
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003725 // [compare state]: For kind COMPARE_IC, tells what state the stub is in.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003726 inline byte compare_state();
3727 inline void set_compare_state(byte value);
3728
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003729 // [to_boolean_foo]: For kind TO_BOOLEAN_IC tells what state the stub is in.
3730 inline byte to_boolean_state();
3731 inline void set_to_boolean_state(byte value);
3732
Ben Murdochb8e0da22011-05-16 14:20:40 +01003733 // Get the safepoint entry for the given pc.
3734 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003735
3736 // Mark this code object as not having a stack check table. Assumes kind
3737 // is FUNCTION.
3738 void SetNoStackCheckTable();
3739
3740 // Find the first map in an IC stub.
3741 Map* FindFirstMap();
Steve Blocka7e24c12009-10-30 11:49:00 +00003742
3743 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01003744 static inline Flags ComputeFlags(
3745 Kind kind,
3746 InLoopFlag in_loop = NOT_IN_LOOP,
3747 InlineCacheState ic_state = UNINITIALIZED,
3748 ExtraICState extra_ic_state = kNoExtraICState,
3749 PropertyType type = NORMAL,
3750 int argc = -1,
3751 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00003752
3753 static inline Flags ComputeMonomorphicFlags(
3754 Kind kind,
3755 PropertyType type,
Ben Murdochb8e0da22011-05-16 14:20:40 +01003756 ExtraICState extra_ic_state = kNoExtraICState,
Steve Block8defd9f2010-07-08 12:39:36 +01003757 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00003758 InLoopFlag in_loop = NOT_IN_LOOP,
3759 int argc = -1);
3760
3761 static inline Kind ExtractKindFromFlags(Flags flags);
3762 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Ben Murdochb8e0da22011-05-16 14:20:40 +01003763 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003764 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
3765 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3766 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01003767 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00003768 static inline Flags RemoveTypeFromFlags(Flags flags);
3769
3770 // Convert a target address into a code object.
3771 static inline Code* GetCodeFromTargetAddress(Address address);
3772
Steve Block791712a2010-08-27 10:21:07 +01003773 // Convert an entry address into an object.
3774 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3775
Steve Blocka7e24c12009-10-30 11:49:00 +00003776 // Returns the address of the first instruction.
3777 inline byte* instruction_start();
3778
Leon Clarkeac952652010-07-15 11:15:24 +01003779 // Returns the address right after the last instruction.
3780 inline byte* instruction_end();
3781
Steve Blocka7e24c12009-10-30 11:49:00 +00003782 // Returns the size of the instructions, padding, and relocation information.
3783 inline int body_size();
3784
3785 // Returns the address of the first relocation info (read backwards!).
3786 inline byte* relocation_start();
3787
3788 // Code entry point.
3789 inline byte* entry();
3790
3791 // Returns true if pc is inside this object's instructions.
3792 inline bool contains(byte* pc);
3793
Steve Blocka7e24c12009-10-30 11:49:00 +00003794 // Relocate the code by delta bytes. Called to signal that this code
3795 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00003796 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00003797
3798 // Migrate code described by desc.
3799 void CopyFrom(const CodeDesc& desc);
3800
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003801 // Returns the object size for a given body (used for allocation).
3802 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003803 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003804 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00003805 }
3806
3807 // Calculate the size of the code object to report for log events. This takes
3808 // the layout of the code object into account.
3809 int ExecutableSize() {
3810 // Check that the assumptions about the layout of the code object holds.
3811 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3812 Code::kHeaderSize);
3813 return instruction_size() + Code::kHeaderSize;
3814 }
3815
3816 // Locating source position.
3817 int SourcePosition(Address pc);
3818 int SourceStatementPosition(Address pc);
3819
3820 // Casting.
3821 static inline Code* cast(Object* obj);
3822
3823 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003824 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01003825 inline void CodeIterateBody(ObjectVisitor* v);
3826
3827 template<typename StaticVisitor>
Steve Block44f0eee2011-05-26 01:26:41 +01003828 inline void CodeIterateBody(Heap* heap);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003829#ifdef OBJECT_PRINT
3830 inline void CodePrint() {
3831 CodePrint(stdout);
3832 }
3833 void CodePrint(FILE* out);
3834#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00003835#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00003836 void CodeVerify();
3837#endif
Ben Murdochb0fe1622011-05-05 13:52:32 +01003838
Ben Murdoch8b112d22011-06-08 16:22:53 +01003839 // Returns the isolate/heap this code object belongs to.
3840 inline Isolate* isolate();
3841 inline Heap* heap();
3842
Ben Murdochb0fe1622011-05-05 13:52:32 +01003843 // Max loop nesting marker used to postpose OSR. We don't take loop
3844 // nesting that is deeper than 5 levels into account.
3845 static const int kMaxLoopNestingMarker = 6;
3846
Steve Blocka7e24c12009-10-30 11:49:00 +00003847 // Layout description.
3848 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01003849 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003850 static const int kDeoptimizationDataOffset =
3851 kRelocationInfoOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00003852 static const int kNextCodeFlushingCandidateOffset =
3853 kDeoptimizationDataOffset + kPointerSize;
3854 static const int kFlagsOffset =
3855 kNextCodeFlushingCandidateOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003856
Ben Murdoch257744e2011-11-30 15:57:28 +00003857 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003858 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3859
3860 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3861 kKindSpecificFlagsSize;
3862
Steve Blocka7e24c12009-10-30 11:49:00 +00003863 // Add padding to align the instruction start following right after
3864 // the Code object header.
3865 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01003866 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00003867
3868 // Byte offsets within kKindSpecificFlagsOffset.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003869 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3870 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3871 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3872 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3873
Ben Murdoch257744e2011-11-30 15:57:28 +00003874 static const int kUnaryOpTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003875 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003876 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3877 static const int kToBooleanTypeOffset = kStubMajorKeyOffset + 1;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003878 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3879
3880 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3881 static const int kAllowOSRAtLoopNestingLevelOffset =
3882 kHasDeoptimizationSupportOffset + 1;
3883
Steve Block1e0659c2011-05-24 12:43:12 +01003884 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3885 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003886
3887 // Flags layout.
3888 static const int kFlagsICStateShift = 0;
3889 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01003890 static const int kFlagsTypeShift = 4;
Steve Block44f0eee2011-05-26 01:26:41 +01003891 static const int kFlagsKindShift = 8;
3892 static const int kFlagsICHolderShift = 12;
3893 static const int kFlagsExtraICStateShift = 13;
3894 static const int kFlagsArgumentsCountShift = 15;
Steve Blocka7e24c12009-10-30 11:49:00 +00003895
Steve Block6ded16b2010-05-10 14:33:55 +01003896 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3897 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Steve Block44f0eee2011-05-26 01:26:41 +01003898 static const int kFlagsTypeMask = 0x000000F0; // 00001110000
3899 static const int kFlagsKindMask = 0x00000F00; // 11110000000
3900 static const int kFlagsCacheInPrototypeMapMask = 0x00001000;
3901 static const int kFlagsExtraICStateMask = 0x00006000;
3902 static const int kFlagsArgumentsCountMask = 0xFFFF8000;
Steve Blocka7e24c12009-10-30 11:49:00 +00003903
3904 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01003905 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00003906
3907 private:
3908 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3909};
3910
3911
3912// All heap objects have a Map that describes their structure.
3913// A Map contains information about:
3914// - Size information about the object
3915// - How to iterate over an object (for garbage collection)
3916class Map: public HeapObject {
3917 public:
3918 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01003919 // Size in bytes or kVariableSizeSentinel if instances do not have
3920 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00003921 inline int instance_size();
3922 inline void set_instance_size(int value);
3923
3924 // Count of properties allocated in the object.
3925 inline int inobject_properties();
3926 inline void set_inobject_properties(int value);
3927
3928 // Count of property fields pre-allocated in the object when first allocated.
3929 inline int pre_allocated_property_fields();
3930 inline void set_pre_allocated_property_fields(int value);
3931
3932 // Instance type.
3933 inline InstanceType instance_type();
3934 inline void set_instance_type(InstanceType value);
3935
3936 // Tells how many unused property fields are available in the
3937 // instance (only used for JSObject in fast mode).
3938 inline int unused_property_fields();
3939 inline void set_unused_property_fields(int value);
3940
3941 // Bit field.
3942 inline byte bit_field();
3943 inline void set_bit_field(byte value);
3944
3945 // Bit field 2.
3946 inline byte bit_field2();
3947 inline void set_bit_field2(byte value);
3948
Ben Murdoch257744e2011-11-30 15:57:28 +00003949 // Bit field 3.
3950 // TODO(1399): It should be possible to make room for bit_field3 in the map
3951 // without overloading the instance descriptors field (and storing it in the
3952 // DescriptorArray when the map has one).
3953 inline int bit_field3();
3954 inline void set_bit_field3(int value);
3955
Steve Blocka7e24c12009-10-30 11:49:00 +00003956 // Tells whether the object in the prototype property will be used
3957 // for instances created from this function. If the prototype
3958 // property is set to a value that is not a JSObject, the prototype
3959 // property will not be used to create instances of the function.
3960 // See ECMA-262, 13.2.2.
3961 inline void set_non_instance_prototype(bool value);
3962 inline bool has_non_instance_prototype();
3963
Steve Block6ded16b2010-05-10 14:33:55 +01003964 // Tells whether function has special prototype property. If not, prototype
3965 // property will not be created when accessed (will return undefined),
3966 // and construction from this function will not be allowed.
3967 inline void set_function_with_prototype(bool value);
3968 inline bool function_with_prototype();
3969
Steve Blocka7e24c12009-10-30 11:49:00 +00003970 // Tells whether the instance with this map should be ignored by the
3971 // __proto__ accessor.
3972 inline void set_is_hidden_prototype() {
3973 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3974 }
3975
3976 inline bool is_hidden_prototype() {
3977 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3978 }
3979
3980 // Records and queries whether the instance has a named interceptor.
3981 inline void set_has_named_interceptor() {
3982 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3983 }
3984
3985 inline bool has_named_interceptor() {
3986 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3987 }
3988
3989 // Records and queries whether the instance has an indexed interceptor.
3990 inline void set_has_indexed_interceptor() {
3991 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3992 }
3993
3994 inline bool has_indexed_interceptor() {
3995 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3996 }
3997
3998 // Tells whether the instance is undetectable.
3999 // An undetectable object is a special class of JSObject: 'typeof' operator
4000 // returns undefined, ToBoolean returns false. Otherwise it behaves like
4001 // a normal JS object. It is useful for implementing undetectable
4002 // document.all in Firefox & Safari.
4003 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
4004 inline void set_is_undetectable() {
4005 set_bit_field(bit_field() | (1 << kIsUndetectable));
4006 }
4007
4008 inline bool is_undetectable() {
4009 return ((1 << kIsUndetectable) & bit_field()) != 0;
4010 }
4011
Steve Blocka7e24c12009-10-30 11:49:00 +00004012 // Tells whether the instance has a call-as-function handler.
4013 inline void set_has_instance_call_handler() {
4014 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
4015 }
4016
4017 inline bool has_instance_call_handler() {
4018 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
4019 }
4020
Steve Block8defd9f2010-07-08 12:39:36 +01004021 inline void set_is_extensible(bool value);
4022 inline bool is_extensible();
4023
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004024 inline void set_elements_kind(JSObject::ElementsKind elements_kind) {
4025 ASSERT(elements_kind < JSObject::kElementsKindCount);
4026 ASSERT(JSObject::kElementsKindCount <= (1 << kElementsKindBitCount));
4027 set_bit_field2((bit_field2() & ~kElementsKindMask) |
4028 (elements_kind << kElementsKindShift));
4029 ASSERT(this->elements_kind() == elements_kind);
4030 }
4031
4032 inline JSObject::ElementsKind elements_kind() {
4033 return static_cast<JSObject::ElementsKind>(
4034 (bit_field2() & kElementsKindMask) >> kElementsKindShift);
4035 }
4036
Steve Block8defd9f2010-07-08 12:39:36 +01004037 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01004038 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
Iain Merrick75681382010-08-19 15:07:18 +01004039 inline bool has_fast_elements() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004040 return elements_kind() == JSObject::FAST_ELEMENTS;
Leon Clarkee46be812010-01-19 14:06:41 +00004041 }
4042
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004043 inline bool has_fast_double_elements() {
4044 return elements_kind() == JSObject::FAST_DOUBLE_ELEMENTS;
Steve Block1e0659c2011-05-24 12:43:12 +01004045 }
4046
Steve Block44f0eee2011-05-26 01:26:41 +01004047 inline bool has_external_array_elements() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004048 JSObject::ElementsKind kind(elements_kind());
4049 return kind >= JSObject::FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
4050 kind <= JSObject::LAST_EXTERNAL_ARRAY_ELEMENTS_KIND;
4051 }
4052
4053 inline bool has_dictionary_elements() {
4054 return elements_kind() == JSObject::DICTIONARY_ELEMENTS;
Steve Block1e0659c2011-05-24 12:43:12 +01004055 }
4056
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004057 // Tells whether the map is attached to SharedFunctionInfo
4058 // (for inobject slack tracking).
4059 inline void set_attached_to_shared_function_info(bool value);
4060
4061 inline bool attached_to_shared_function_info();
4062
4063 // Tells whether the map is shared between objects that may have different
4064 // behavior. If true, the map should never be modified, instead a clone
4065 // should be created and modified.
4066 inline void set_is_shared(bool value);
4067
4068 inline bool is_shared();
4069
Steve Blocka7e24c12009-10-30 11:49:00 +00004070 // Tells whether the instance needs security checks when accessing its
4071 // properties.
4072 inline void set_is_access_check_needed(bool access_check_needed);
4073 inline bool is_access_check_needed();
4074
4075 // [prototype]: implicit prototype object.
4076 DECL_ACCESSORS(prototype, Object)
4077
4078 // [constructor]: points back to the function responsible for this map.
4079 DECL_ACCESSORS(constructor, Object)
4080
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004081 inline JSFunction* unchecked_constructor();
4082
Ben Murdoch257744e2011-11-30 15:57:28 +00004083 // Should only be called by the code that initializes map to set initial valid
4084 // value of the instance descriptor member.
4085 inline void init_instance_descriptors();
4086
Steve Blocka7e24c12009-10-30 11:49:00 +00004087 // [instance descriptors]: describes the object.
4088 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
4089
Ben Murdoch257744e2011-11-30 15:57:28 +00004090 // Sets the instance descriptor array for the map to be an empty descriptor
4091 // array.
4092 inline void clear_instance_descriptors();
4093
Steve Blocka7e24c12009-10-30 11:49:00 +00004094 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01004095 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004096
Steve Block053d10c2011-06-13 19:13:29 +01004097 // [prototype transitions]: cache of prototype transitions.
4098 // Prototype transition is a transition that happens
4099 // when we change object's prototype to a new one.
4100 // Cache format:
4101 // 0: finger - index of the first free cell in the cache
4102 // 1 + 2 * i: prototype
4103 // 2 + 2 * i: target map
4104 DECL_ACCESSORS(prototype_transitions, FixedArray)
4105 inline FixedArray* unchecked_prototype_transitions();
4106
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004107 static const int kProtoTransitionHeaderSize = 1;
4108 static const int kProtoTransitionNumberOfEntriesOffset = 0;
4109 static const int kProtoTransitionElementsPerEntry = 2;
4110 static const int kProtoTransitionPrototypeOffset = 0;
4111 static const int kProtoTransitionMapOffset = 1;
4112
4113 inline int NumberOfProtoTransitions() {
4114 FixedArray* cache = unchecked_prototype_transitions();
4115 if (cache->length() == 0) return 0;
4116 return
4117 Smi::cast(cache->get(kProtoTransitionNumberOfEntriesOffset))->value();
4118 }
4119
4120 inline void SetNumberOfProtoTransitions(int value) {
4121 FixedArray* cache = unchecked_prototype_transitions();
4122 ASSERT(cache->length() != 0);
4123 cache->set_unchecked(kProtoTransitionNumberOfEntriesOffset,
4124 Smi::FromInt(value));
4125 }
4126
Ben Murdochb0fe1622011-05-05 13:52:32 +01004127 // Lookup in the map's instance descriptors and fill out the result
4128 // with the given holder if the name is found. The holder may be
4129 // NULL when this function is used from the compiler.
4130 void LookupInDescriptors(JSObject* holder,
4131 String* name,
4132 LookupResult* result);
4133
John Reck59135872010-11-02 12:39:01 -07004134 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004135
John Reck59135872010-11-02 12:39:01 -07004136 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
4137 NormalizedMapSharingMode sharing);
Steve Blocka7e24c12009-10-30 11:49:00 +00004138
4139 // Returns a copy of the map, with all transitions dropped from the
4140 // instance descriptors.
John Reck59135872010-11-02 12:39:01 -07004141 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
Steve Blocka7e24c12009-10-30 11:49:00 +00004142
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004143 // Returns this map if it already has elements that are fast, otherwise
Steve Block8defd9f2010-07-08 12:39:36 +01004144 // returns a copy of the map, with all transitions dropped from the
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004145 // descriptors and the ElementsKind set to FAST_ELEMENTS.
John Reck59135872010-11-02 12:39:01 -07004146 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01004147
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004148 // Returns this map if it already has fast elements that are doubles,
4149 // otherwise returns a copy of the map, with all transitions dropped from the
4150 // descriptors and the ElementsKind set to FAST_DOUBLE_ELEMENTS.
4151 MUST_USE_RESULT inline MaybeObject* GetFastDoubleElementsMap();
4152
4153 // Returns this map if already has dictionary elements, otherwise returns a
4154 // copy of the map, with all transitions dropped from the descriptors and the
4155 // ElementsKind set to DICTIONARY_ELEMENTS.
John Reck59135872010-11-02 12:39:01 -07004156 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
Steve Block8defd9f2010-07-08 12:39:36 +01004157
Steve Block44f0eee2011-05-26 01:26:41 +01004158 // Returns a new map with all transitions dropped from the descriptors and the
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004159 // ElementsKind set to one of the value corresponding to array_type.
Steve Block44f0eee2011-05-26 01:26:41 +01004160 MUST_USE_RESULT MaybeObject* GetExternalArrayElementsMap(
4161 ExternalArrayType array_type,
4162 bool safe_to_add_transition);
Steve Block1e0659c2011-05-24 12:43:12 +01004163
Steve Blocka7e24c12009-10-30 11:49:00 +00004164 // Returns the property index for name (only valid for FAST MODE).
4165 int PropertyIndexFor(String* name);
4166
4167 // Returns the next free property index (only valid for FAST MODE).
4168 int NextFreePropertyIndex();
4169
4170 // Returns the number of properties described in instance_descriptors.
4171 int NumberOfDescribedProperties();
4172
4173 // Casting.
4174 static inline Map* cast(Object* obj);
4175
4176 // Locate an accessor in the instance descriptor.
4177 AccessorDescriptor* FindAccessor(String* name);
4178
4179 // Code cache operations.
4180
4181 // Clears the code cache.
Steve Block44f0eee2011-05-26 01:26:41 +01004182 inline void ClearCodeCache(Heap* heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00004183
4184 // Update code cache.
John Reck59135872010-11-02 12:39:01 -07004185 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004186
4187 // Returns the found code or undefined if absent.
4188 Object* FindInCodeCache(String* name, Code::Flags flags);
4189
4190 // Returns the non-negative index of the code object if it is in the
4191 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01004192 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00004193
4194 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01004195 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00004196
4197 // For every transition in this map, makes the transition's
4198 // target's prototype pointer point back to this map.
4199 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
4200 void CreateBackPointers();
4201
4202 // Set all map transitions from this map to dead maps to null.
4203 // Also, restore the original prototype on the targets of these
4204 // transitions, so that we do not process this map again while
4205 // following back pointers.
Steve Block44f0eee2011-05-26 01:26:41 +01004206 void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00004207
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004208 // Computes a hash value for this map, to be used in HashTables and such.
4209 int Hash();
4210
4211 // Compares this map to another to see if they describe equivalent objects.
4212 // If |mode| is set to CLEAR_INOBJECT_PROPERTIES, |other| is treated as if
4213 // it had exactly zero inobject properties.
4214 // The "shared" flags of both this map and |other| are ignored.
4215 bool EquivalentToForNormalization(Map* other, PropertyNormalizationMode mode);
4216
4217 // Returns true if this map and |other| describe equivalent objects.
4218 // The "shared" flags of both this map and |other| are ignored.
4219 bool EquivalentTo(Map* other) {
4220 return EquivalentToForNormalization(other, KEEP_INOBJECT_PROPERTIES);
4221 }
4222
Steve Blocka7e24c12009-10-30 11:49:00 +00004223 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004224#ifdef OBJECT_PRINT
4225 inline void MapPrint() {
4226 MapPrint(stdout);
4227 }
4228 void MapPrint(FILE* out);
4229#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004230#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004231 void MapVerify();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004232 void SharedMapVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00004233#endif
4234
Iain Merrick75681382010-08-19 15:07:18 +01004235 inline int visitor_id();
4236 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004237
Steve Block44f0eee2011-05-26 01:26:41 +01004238 // Returns the isolate/heap this map belongs to.
4239 inline Isolate* isolate();
4240 inline Heap* heap();
4241
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004242 typedef void (*TraverseCallback)(Map* map, void* data);
4243
4244 void TraverseTransitionTree(TraverseCallback callback, void* data);
4245
Steve Block053d10c2011-06-13 19:13:29 +01004246 static const int kMaxCachedPrototypeTransitions = 256;
4247
4248 Object* GetPrototypeTransition(Object* prototype);
4249
4250 MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
4251
Steve Blocka7e24c12009-10-30 11:49:00 +00004252 static const int kMaxPreAllocatedPropertyFields = 255;
4253
4254 // Layout description.
4255 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
4256 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
4257 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
4258 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00004259 // Storage for instance descriptors is overloaded to also contain additional
4260 // map flags when unused (bit_field3). When the map has instance descriptors,
4261 // the flags are transferred to the instance descriptor array and accessed
4262 // through an extra indirection.
4263 // TODO(1399): It should be possible to make room for bit_field3 in the map
4264 // without overloading the instance descriptors field, but the map is
4265 // currently perfectly aligned to 32 bytes and extending it at all would
4266 // double its size. After the increment GC work lands, this size restriction
4267 // could be loosened and bit_field3 moved directly back in the map.
4268 static const int kInstanceDescriptorsOrBitField3Offset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004269 kConstructorOffset + kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00004270 static const int kCodeCacheOffset =
4271 kInstanceDescriptorsOrBitField3Offset + kPointerSize;
Steve Block053d10c2011-06-13 19:13:29 +01004272 static const int kPrototypeTransitionsOffset =
4273 kCodeCacheOffset + kPointerSize;
4274 static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004275 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
4276
4277 // Layout of pointer fields. Heap iteration code relies on them
4278 // being continiously allocated.
4279 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
4280 static const int kPointerFieldsEndOffset =
Steve Block053d10c2011-06-13 19:13:29 +01004281 Map::kPrototypeTransitionsOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004282
4283 // Byte offsets within kInstanceSizesOffset.
4284 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
4285 static const int kInObjectPropertiesByte = 1;
4286 static const int kInObjectPropertiesOffset =
4287 kInstanceSizesOffset + kInObjectPropertiesByte;
4288 static const int kPreAllocatedPropertyFieldsByte = 2;
4289 static const int kPreAllocatedPropertyFieldsOffset =
4290 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01004291 static const int kVisitorIdByte = 3;
4292 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00004293
4294 // Byte offsets within kInstanceAttributesOffset attributes.
4295 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
4296 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
4297 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
4298 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
4299
4300 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
4301
4302 // Bit positions for bit field.
4303 static const int kUnused = 0; // To be used for marking recently used maps.
4304 static const int kHasNonInstancePrototype = 1;
4305 static const int kIsHiddenPrototype = 2;
4306 static const int kHasNamedInterceptor = 3;
4307 static const int kHasIndexedInterceptor = 4;
4308 static const int kIsUndetectable = 5;
4309 static const int kHasInstanceCallHandler = 6;
4310 static const int kIsAccessCheckNeeded = 7;
4311
4312 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00004313 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01004314 static const int kFunctionWithPrototype = 1;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004315 static const int kStringWrapperSafeForDefaultValueOf = 2;
4316 static const int kAttachedToSharedFunctionInfo = 3;
4317 // No bits can be used after kElementsKindFirstBit, they are all reserved for
4318 // storing ElementKind. for anything other than storing the ElementKind.
4319 static const int kElementsKindShift = 4;
4320 static const int kElementsKindBitCount = 4;
4321
4322 // Derived values from bit field 2
4323 static const int kElementsKindMask = (-1 << kElementsKindShift) &
4324 ((1 << (kElementsKindShift + kElementsKindBitCount)) - 1);
4325 static const int8_t kMaximumBitField2FastElementValue = static_cast<int8_t>(
4326 (JSObject::FAST_ELEMENTS + 1) << Map::kElementsKindShift) - 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00004327
4328 // Bit positions for bit field 3
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004329 static const int kIsShared = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01004330
4331 // Layout of the default cache. It holds alternating name and code objects.
4332 static const int kCodeCacheEntrySize = 2;
4333 static const int kCodeCacheEntryNameOffset = 0;
4334 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00004335
Iain Merrick75681382010-08-19 15:07:18 +01004336 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
4337 kPointerFieldsEndOffset,
4338 kSize> BodyDescriptor;
4339
Steve Blocka7e24c12009-10-30 11:49:00 +00004340 private:
4341 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
4342};
4343
4344
4345// An abstract superclass, a marker class really, for simple structure classes.
Ben Murdoch257744e2011-11-30 15:57:28 +00004346// It doesn't carry much functionality but allows struct classes to be
Steve Blocka7e24c12009-10-30 11:49:00 +00004347// identified in the type system.
4348class Struct: public HeapObject {
4349 public:
4350 inline void InitializeBody(int object_size);
4351 static inline Struct* cast(Object* that);
4352};
4353
4354
4355// Script describes a script which has been added to the VM.
4356class Script: public Struct {
4357 public:
4358 // Script types.
4359 enum Type {
4360 TYPE_NATIVE = 0,
4361 TYPE_EXTENSION = 1,
4362 TYPE_NORMAL = 2
4363 };
4364
4365 // Script compilation types.
4366 enum CompilationType {
4367 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08004368 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00004369 };
4370
4371 // [source]: the script source.
4372 DECL_ACCESSORS(source, Object)
4373
4374 // [name]: the script name.
4375 DECL_ACCESSORS(name, Object)
4376
4377 // [id]: the script id.
4378 DECL_ACCESSORS(id, Object)
4379
4380 // [line_offset]: script line offset in resource from where it was extracted.
4381 DECL_ACCESSORS(line_offset, Smi)
4382
4383 // [column_offset]: script column offset in resource from where it was
4384 // extracted.
4385 DECL_ACCESSORS(column_offset, Smi)
4386
4387 // [data]: additional data associated with this script.
4388 DECL_ACCESSORS(data, Object)
4389
4390 // [context_data]: context data for the context this script was compiled in.
4391 DECL_ACCESSORS(context_data, Object)
4392
4393 // [wrapper]: the wrapper cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00004394 DECL_ACCESSORS(wrapper, Foreign)
Steve Blocka7e24c12009-10-30 11:49:00 +00004395
4396 // [type]: the script type.
4397 DECL_ACCESSORS(type, Smi)
4398
4399 // [compilation]: how the the script was compiled.
4400 DECL_ACCESSORS(compilation_type, Smi)
4401
Steve Blockd0582a62009-12-15 09:54:21 +00004402 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00004403 DECL_ACCESSORS(line_ends, Object)
4404
Steve Blockd0582a62009-12-15 09:54:21 +00004405 // [eval_from_shared]: for eval scripts the shared funcion info for the
4406 // function from which eval was called.
4407 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004408
4409 // [eval_from_instructions_offset]: the instruction offset in the code for the
4410 // function from which eval was called where eval was called.
4411 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
4412
4413 static inline Script* cast(Object* obj);
4414
Steve Block3ce2e202009-11-05 08:53:23 +00004415 // If script source is an external string, check that the underlying
4416 // resource is accessible. Otherwise, always return true.
4417 inline bool HasValidSource();
4418
Ben Murdochb0fe1622011-05-05 13:52:32 +01004419#ifdef OBJECT_PRINT
4420 inline void ScriptPrint() {
4421 ScriptPrint(stdout);
4422 }
4423 void ScriptPrint(FILE* out);
4424#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004425#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004426 void ScriptVerify();
4427#endif
4428
4429 static const int kSourceOffset = HeapObject::kHeaderSize;
4430 static const int kNameOffset = kSourceOffset + kPointerSize;
4431 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4432 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
4433 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
4434 static const int kContextOffset = kDataOffset + kPointerSize;
4435 static const int kWrapperOffset = kContextOffset + kPointerSize;
4436 static const int kTypeOffset = kWrapperOffset + kPointerSize;
4437 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
4438 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
4439 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00004440 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004441 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00004442 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004443 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
4444
4445 private:
4446 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4447};
4448
4449
Ben Murdochb0fe1622011-05-05 13:52:32 +01004450// List of builtin functions we want to identify to improve code
4451// generation.
4452//
4453// Each entry has a name of a global object property holding an object
4454// optionally followed by ".prototype", a name of a builtin function
4455// on the object (the one the id is set for), and a label.
4456//
4457// Installation of ids for the selected builtin functions is handled
4458// by the bootstrapper.
4459//
4460// NOTE: Order is important: math functions should be at the end of
4461// the list and MathFloor should be the first math function.
4462#define FUNCTIONS_WITH_ID_LIST(V) \
4463 V(Array.prototype, push, ArrayPush) \
4464 V(Array.prototype, pop, ArrayPop) \
Ben Murdoch42effa52011-08-19 16:40:31 +01004465 V(Function.prototype, apply, FunctionApply) \
Ben Murdochb0fe1622011-05-05 13:52:32 +01004466 V(String.prototype, charCodeAt, StringCharCodeAt) \
4467 V(String.prototype, charAt, StringCharAt) \
4468 V(String, fromCharCode, StringFromCharCode) \
4469 V(Math, floor, MathFloor) \
4470 V(Math, round, MathRound) \
4471 V(Math, ceil, MathCeil) \
4472 V(Math, abs, MathAbs) \
4473 V(Math, log, MathLog) \
4474 V(Math, sin, MathSin) \
4475 V(Math, cos, MathCos) \
4476 V(Math, tan, MathTan) \
4477 V(Math, asin, MathASin) \
4478 V(Math, acos, MathACos) \
4479 V(Math, atan, MathATan) \
4480 V(Math, exp, MathExp) \
4481 V(Math, sqrt, MathSqrt) \
4482 V(Math, pow, MathPow)
4483
4484
4485enum BuiltinFunctionId {
4486#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
4487 k##name,
4488 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4489#undef DECLARE_FUNCTION_ID
4490 // Fake id for a special case of Math.pow. Note, it continues the
4491 // list of math functions.
4492 kMathPowHalf,
4493 kFirstMathFunctionId = kMathFloor
4494};
4495
4496
Steve Blocka7e24c12009-10-30 11:49:00 +00004497// SharedFunctionInfo describes the JSFunction information that can be
4498// shared by multiple instances of the function.
4499class SharedFunctionInfo: public HeapObject {
4500 public:
4501 // [name]: Function name.
4502 DECL_ACCESSORS(name, Object)
4503
4504 // [code]: Function code.
4505 DECL_ACCESSORS(code, Code)
4506
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004507 // [scope_info]: Scope info.
4508 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
4509
Steve Blocka7e24c12009-10-30 11:49:00 +00004510 // [construct stub]: Code stub for constructing instances of this function.
4511 DECL_ACCESSORS(construct_stub, Code)
4512
Iain Merrick75681382010-08-19 15:07:18 +01004513 inline Code* unchecked_code();
4514
Steve Blocka7e24c12009-10-30 11:49:00 +00004515 // Returns if this function has been compiled to native code yet.
4516 inline bool is_compiled();
4517
4518 // [length]: The function length - usually the number of declared parameters.
4519 // Use up to 2^30 parameters.
4520 inline int length();
4521 inline void set_length(int value);
4522
4523 // [formal parameter count]: The declared number of parameters.
4524 inline int formal_parameter_count();
4525 inline void set_formal_parameter_count(int value);
4526
4527 // Set the formal parameter count so the function code will be
4528 // called without using argument adaptor frames.
4529 inline void DontAdaptArguments();
4530
4531 // [expected_nof_properties]: Expected number of properties for the function.
4532 inline int expected_nof_properties();
4533 inline void set_expected_nof_properties(int value);
4534
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004535 // Inobject slack tracking is the way to reclaim unused inobject space.
4536 //
4537 // The instance size is initially determined by adding some slack to
4538 // expected_nof_properties (to allow for a few extra properties added
4539 // after the constructor). There is no guarantee that the extra space
4540 // will not be wasted.
4541 //
4542 // Here is the algorithm to reclaim the unused inobject space:
4543 // - Detect the first constructor call for this SharedFunctionInfo.
4544 // When it happens enter the "in progress" state: remember the
4545 // constructor's initial_map and install a special construct stub that
4546 // counts constructor calls.
4547 // - While the tracking is in progress create objects filled with
4548 // one_pointer_filler_map instead of undefined_value. This way they can be
4549 // resized quickly and safely.
4550 // - Once enough (kGenerousAllocationCount) objects have been created
4551 // compute the 'slack' (traverse the map transition tree starting from the
4552 // initial_map and find the lowest value of unused_property_fields).
4553 // - Traverse the transition tree again and decrease the instance size
4554 // of every map. Existing objects will resize automatically (they are
4555 // filled with one_pointer_filler_map). All further allocations will
4556 // use the adjusted instance size.
4557 // - Decrease expected_nof_properties so that an allocations made from
4558 // another context will use the adjusted instance size too.
4559 // - Exit "in progress" state by clearing the reference to the initial_map
4560 // and setting the regular construct stub (generic or inline).
4561 //
4562 // The above is the main event sequence. Some special cases are possible
4563 // while the tracking is in progress:
4564 //
4565 // - GC occurs.
4566 // Check if the initial_map is referenced by any live objects (except this
4567 // SharedFunctionInfo). If it is, continue tracking as usual.
4568 // If it is not, clear the reference and reset the tracking state. The
4569 // tracking will be initiated again on the next constructor call.
4570 //
4571 // - The constructor is called from another context.
4572 // Immediately complete the tracking, perform all the necessary changes
4573 // to maps. This is necessary because there is no efficient way to track
4574 // multiple initial_maps.
4575 // Proceed to create an object in the current context (with the adjusted
4576 // size).
4577 //
4578 // - A different constructor function sharing the same SharedFunctionInfo is
4579 // called in the same context. This could be another closure in the same
4580 // context, or the first function could have been disposed.
4581 // This is handled the same way as the previous case.
4582 //
4583 // Important: inobject slack tracking is not attempted during the snapshot
4584 // creation.
4585
Ben Murdochf87a2032010-10-22 12:50:53 +01004586 static const int kGenerousAllocationCount = 8;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004587
4588 // [construction_count]: Counter for constructor calls made during
4589 // the tracking phase.
4590 inline int construction_count();
4591 inline void set_construction_count(int value);
4592
4593 // [initial_map]: initial map of the first function called as a constructor.
4594 // Saved for the duration of the tracking phase.
4595 // This is a weak link (GC resets it to undefined_value if no other live
4596 // object reference this map).
4597 DECL_ACCESSORS(initial_map, Object)
4598
4599 // True if the initial_map is not undefined and the countdown stub is
4600 // installed.
4601 inline bool IsInobjectSlackTrackingInProgress();
4602
4603 // Starts the tracking.
4604 // Stores the initial map and installs the countdown stub.
4605 // IsInobjectSlackTrackingInProgress is normally true after this call,
4606 // except when tracking have not been started (e.g. the map has no unused
4607 // properties or the snapshot is being built).
4608 void StartInobjectSlackTracking(Map* map);
4609
4610 // Completes the tracking.
4611 // IsInobjectSlackTrackingInProgress is false after this call.
4612 void CompleteInobjectSlackTracking();
4613
4614 // Clears the initial_map before the GC marking phase to ensure the reference
4615 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4616 void DetachInitialMap();
4617
4618 // Restores the link to the initial map after the GC marking phase.
4619 // IsInobjectSlackTrackingInProgress is true after this call.
4620 void AttachInitialMap(Map* map);
4621
4622 // False if there are definitely no live objects created from this function.
4623 // True if live objects _may_ exist (existence not guaranteed).
4624 // May go back from true to false after GC.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004625 DECL_BOOLEAN_ACCESSORS(live_objects_may_exist)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004626
Steve Blocka7e24c12009-10-30 11:49:00 +00004627 // [instance class name]: class name for instances.
4628 DECL_ACCESSORS(instance_class_name, Object)
4629
Steve Block6ded16b2010-05-10 14:33:55 +01004630 // [function data]: This field holds some additional data for function.
4631 // Currently it either has FunctionTemplateInfo to make benefit the API
Ben Murdochb0fe1622011-05-05 13:52:32 +01004632 // or Smi identifying a builtin function.
Steve Blocka7e24c12009-10-30 11:49:00 +00004633 // In the long run we don't want all functions to have this field but
4634 // we can fix that when we have a better model for storing hidden data
4635 // on objects.
4636 DECL_ACCESSORS(function_data, Object)
4637
Steve Block6ded16b2010-05-10 14:33:55 +01004638 inline bool IsApiFunction();
4639 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004640 inline bool HasBuiltinFunctionId();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004641 inline BuiltinFunctionId builtin_function_id();
Steve Block6ded16b2010-05-10 14:33:55 +01004642
Steve Blocka7e24c12009-10-30 11:49:00 +00004643 // [script info]: Script from which the function originates.
4644 DECL_ACCESSORS(script, Object)
4645
Steve Block6ded16b2010-05-10 14:33:55 +01004646 // [num_literals]: Number of literals used by this function.
4647 inline int num_literals();
4648 inline void set_num_literals(int value);
4649
Steve Blocka7e24c12009-10-30 11:49:00 +00004650 // [start_position_and_type]: Field used to store both the source code
4651 // position, whether or not the function is a function expression,
4652 // and whether or not the function is a toplevel function. The two
4653 // least significants bit indicates whether the function is an
4654 // expression and the rest contains the source code position.
4655 inline int start_position_and_type();
4656 inline void set_start_position_and_type(int value);
4657
4658 // [debug info]: Debug information.
4659 DECL_ACCESSORS(debug_info, Object)
4660
4661 // [inferred name]: Name inferred from variable or property
4662 // assignment of this function. Used to facilitate debugging and
4663 // profiling of JavaScript code written in OO style, where almost
4664 // all functions are anonymous but are assigned to object
4665 // properties.
4666 DECL_ACCESSORS(inferred_name, String)
4667
Ben Murdochf87a2032010-10-22 12:50:53 +01004668 // The function's name if it is non-empty, otherwise the inferred name.
4669 String* DebugName();
4670
Steve Blocka7e24c12009-10-30 11:49:00 +00004671 // Position of the 'function' token in the script source.
4672 inline int function_token_position();
4673 inline void set_function_token_position(int function_token_position);
4674
4675 // Position of this function in the script source.
4676 inline int start_position();
4677 inline void set_start_position(int start_position);
4678
4679 // End position of this function in the script source.
4680 inline int end_position();
4681 inline void set_end_position(int end_position);
4682
4683 // Is this function a function expression in the source code.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004684 DECL_BOOLEAN_ACCESSORS(is_expression)
Steve Blocka7e24c12009-10-30 11:49:00 +00004685
4686 // Is this function a top-level function (scripts, evals).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004687 DECL_BOOLEAN_ACCESSORS(is_toplevel)
Steve Blocka7e24c12009-10-30 11:49:00 +00004688
4689 // Bit field containing various information collected by the compiler to
4690 // drive optimization.
4691 inline int compiler_hints();
4692 inline void set_compiler_hints(int value);
4693
Ben Murdochb0fe1622011-05-05 13:52:32 +01004694 // A counter used to determine when to stress the deoptimizer with a
4695 // deopt.
4696 inline Smi* deopt_counter();
4697 inline void set_deopt_counter(Smi* counter);
4698
Steve Blocka7e24c12009-10-30 11:49:00 +00004699 // Add information on assignments of the form this.x = ...;
4700 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00004701 bool has_only_simple_this_property_assignments,
4702 FixedArray* this_property_assignments);
4703
4704 // Clear information on assignments of the form this.x = ...;
4705 void ClearThisPropertyAssignmentsInfo();
4706
4707 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00004708 // this.x = y; where y is either a constant or refers to an argument.
4709 inline bool has_only_simple_this_property_assignments();
4710
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004711 // Indicates if this function can be lazy compiled.
4712 // This is used to determine if we can safely flush code from a function
4713 // when doing GC if we expect that the function will no longer be used.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004714 DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004715
Iain Merrick75681382010-08-19 15:07:18 +01004716 // Indicates how many full GCs this function has survived with assigned
4717 // code object. Used to determine when it is relatively safe to flush
4718 // this code object and replace it with lazy compilation stub.
4719 // Age is reset when GC notices that the code object is referenced
4720 // from the stack or compilation cache.
4721 inline int code_age();
4722 inline void set_code_age(int age);
4723
Ben Murdochb0fe1622011-05-05 13:52:32 +01004724 // Indicates whether optimizations have been disabled for this
4725 // shared function info. If a function is repeatedly optimized or if
4726 // we cannot optimize the function we disable optimization to avoid
4727 // spending time attempting to optimize it again.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004728 DECL_BOOLEAN_ACCESSORS(optimization_disabled)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004729
Steve Block1e0659c2011-05-24 12:43:12 +01004730 // Indicates whether the function is a strict mode function.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004731 DECL_BOOLEAN_ACCESSORS(strict_mode)
Steve Block1e0659c2011-05-24 12:43:12 +01004732
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004733 // False if the function definitely does not allocate an arguments object.
4734 DECL_BOOLEAN_ACCESSORS(uses_arguments)
4735
4736 // True if the function has any duplicated parameter names.
4737 DECL_BOOLEAN_ACCESSORS(has_duplicate_parameters)
4738
4739 // Indicates whether the function is a native function.
Ben Murdoch257744e2011-11-30 15:57:28 +00004740 // These needs special threatment in .call and .apply since
4741 // null passed as the receiver should not be translated to the
4742 // global object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004743 DECL_BOOLEAN_ACCESSORS(native)
4744
4745 // Indicates that the function was created by the Function function.
4746 // Though it's anonymous, toString should treat it as if it had the name
4747 // "anonymous". We don't set the name itself so that the system does not
4748 // see a binding for it.
4749 DECL_BOOLEAN_ACCESSORS(name_should_print_as_anonymous)
4750
4751 // Indicates whether the function is a bound function created using
4752 // the bind function.
4753 DECL_BOOLEAN_ACCESSORS(bound)
4754
4755 // Indicates that the function is anonymous (the name field can be set
4756 // through the API, which does not change this flag).
4757 DECL_BOOLEAN_ACCESSORS(is_anonymous)
Ben Murdoch257744e2011-11-30 15:57:28 +00004758
Ben Murdochb0fe1622011-05-05 13:52:32 +01004759 // Indicates whether or not the code in the shared function support
4760 // deoptimization.
4761 inline bool has_deoptimization_support();
4762
4763 // Enable deoptimization support through recompiled code.
4764 void EnableDeoptimizationSupport(Code* recompiled);
4765
Ben Murdoch257744e2011-11-30 15:57:28 +00004766 // Disable (further) attempted optimization of all functions sharing this
4767 // shared function info. The function is the one we actually tried to
4768 // optimize.
4769 void DisableOptimization(JSFunction* function);
4770
Ben Murdochb0fe1622011-05-05 13:52:32 +01004771 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4772 // code, returns whether it asserted (i.e., always true if assertions are
4773 // disabled).
4774 bool VerifyBailoutId(int id);
Iain Merrick75681382010-08-19 15:07:18 +01004775
Andrei Popescu402d9372010-02-26 13:31:12 +00004776 // Check whether a inlined constructor can be generated with the given
4777 // prototype.
4778 bool CanGenerateInlineConstructor(Object* prototype);
4779
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004780 // Prevents further attempts to generate inline constructors.
4781 // To be called if generation failed for any reason.
4782 void ForbidInlineConstructor();
4783
Steve Blocka7e24c12009-10-30 11:49:00 +00004784 // For functions which only contains this property assignments this provides
4785 // access to the names for the properties assigned.
4786 DECL_ACCESSORS(this_property_assignments, Object)
4787 inline int this_property_assignments_count();
4788 inline void set_this_property_assignments_count(int value);
4789 String* GetThisPropertyAssignmentName(int index);
4790 bool IsThisPropertyAssignmentArgument(int index);
4791 int GetThisPropertyAssignmentArgument(int index);
4792 Object* GetThisPropertyAssignmentConstant(int index);
4793
4794 // [source code]: Source code for the function.
4795 bool HasSourceCode();
4796 Object* GetSourceCode();
4797
Ben Murdochb0fe1622011-05-05 13:52:32 +01004798 inline int opt_count();
4799 inline void set_opt_count(int opt_count);
4800
4801 // Source size of this function.
4802 int SourceSize();
4803
Steve Blocka7e24c12009-10-30 11:49:00 +00004804 // Calculate the instance size.
4805 int CalculateInstanceSize();
4806
4807 // Calculate the number of in-object properties.
4808 int CalculateInObjectProperties();
4809
4810 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004811 // Set max_length to -1 for unlimited length.
4812 void SourceCodePrint(StringStream* accumulator, int max_length);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004813#ifdef OBJECT_PRINT
4814 inline void SharedFunctionInfoPrint() {
4815 SharedFunctionInfoPrint(stdout);
4816 }
4817 void SharedFunctionInfoPrint(FILE* out);
4818#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00004819#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00004820 void SharedFunctionInfoVerify();
4821#endif
4822
4823 // Casting.
4824 static inline SharedFunctionInfo* cast(Object* obj);
4825
4826 // Constants.
4827 static const int kDontAdaptArgumentsSentinel = -1;
4828
4829 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01004830 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00004831 static const int kNameOffset = HeapObject::kHeaderSize;
4832 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01004833 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4834 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004835 static const int kInstanceClassNameOffset =
4836 kConstructStubOffset + kPointerSize;
4837 static const int kFunctionDataOffset =
4838 kInstanceClassNameOffset + kPointerSize;
4839 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4840 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4841 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004842 static const int kInitialMapOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004843 kInferredNameOffset + kPointerSize;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004844 static const int kThisPropertyAssignmentsOffset =
4845 kInitialMapOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004846 static const int kDeoptCounterOffset =
4847 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004848#if V8_HOST_ARCH_32_BIT
4849 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01004850 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004851 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004852 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4853 static const int kExpectedNofPropertiesOffset =
4854 kFormalParameterCountOffset + kPointerSize;
4855 static const int kNumLiteralsOffset =
4856 kExpectedNofPropertiesOffset + kPointerSize;
4857 static const int kStartPositionAndTypeOffset =
4858 kNumLiteralsOffset + kPointerSize;
4859 static const int kEndPositionOffset =
4860 kStartPositionAndTypeOffset + kPointerSize;
4861 static const int kFunctionTokenPositionOffset =
4862 kEndPositionOffset + kPointerSize;
4863 static const int kCompilerHintsOffset =
4864 kFunctionTokenPositionOffset + kPointerSize;
4865 static const int kThisPropertyAssignmentsCountOffset =
4866 kCompilerHintsOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004867 static const int kOptCountOffset =
4868 kThisPropertyAssignmentsCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004869 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004870 static const int kSize = kOptCountOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004871#else
4872 // The only reason to use smi fields instead of int fields
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004873 // is to allow iteration without maps decoding during
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004874 // garbage collections.
4875 // To avoid wasting space on 64-bit architectures we use
4876 // the following trick: we group integer fields into pairs
4877 // First integer in each pair is shifted left by 1.
4878 // By doing this we guarantee that LSB of each kPointerSize aligned
4879 // word is not set and thus this word cannot be treated as pointer
4880 // to HeapObject during old space traversal.
4881 static const int kLengthOffset =
Ben Murdochb0fe1622011-05-05 13:52:32 +01004882 kDeoptCounterOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004883 static const int kFormalParameterCountOffset =
4884 kLengthOffset + kIntSize;
4885
Steve Blocka7e24c12009-10-30 11:49:00 +00004886 static const int kExpectedNofPropertiesOffset =
4887 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004888 static const int kNumLiteralsOffset =
4889 kExpectedNofPropertiesOffset + kIntSize;
4890
4891 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004892 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004893 static const int kStartPositionAndTypeOffset =
4894 kEndPositionOffset + kIntSize;
4895
4896 static const int kFunctionTokenPositionOffset =
4897 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004898 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00004899 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004900
Steve Blocka7e24c12009-10-30 11:49:00 +00004901 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01004902 kCompilerHintsOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004903 static const int kOptCountOffset =
4904 kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004905
Steve Block6ded16b2010-05-10 14:33:55 +01004906 // Total size.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004907 static const int kSize = kOptCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004908
4909#endif
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004910
4911 // The construction counter for inobject slack tracking is stored in the
4912 // most significant byte of compiler_hints which is otherwise unused.
4913 // Its offset depends on the endian-ness of the architecture.
4914#if __BYTE_ORDER == __LITTLE_ENDIAN
4915 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4916#elif __BYTE_ORDER == __BIG_ENDIAN
4917 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4918#else
4919#error Unknown byte ordering
4920#endif
4921
Steve Block6ded16b2010-05-10 14:33:55 +01004922 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004923
Iain Merrick75681382010-08-19 15:07:18 +01004924 typedef FixedBodyDescriptor<kNameOffset,
4925 kThisPropertyAssignmentsOffset + kPointerSize,
4926 kSize> BodyDescriptor;
4927
Steve Blocka7e24c12009-10-30 11:49:00 +00004928 // Bit positions in start_position_and_type.
4929 // The source code start position is in the 30 most significant bits of
4930 // the start_position_and_type field.
4931 static const int kIsExpressionBit = 0;
4932 static const int kIsTopLevelBit = 1;
4933 static const int kStartPositionShift = 2;
4934 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4935
4936 // Bit positions in compiler_hints.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004937 static const int kCodeAgeSize = 3;
4938 static const int kCodeAgeMask = (1 << kCodeAgeSize) - 1;
4939
4940 enum CompilerHints {
4941 kHasOnlySimpleThisPropertyAssignments,
4942 kAllowLazyCompilation,
4943 kLiveObjectsMayExist,
4944 kCodeAgeShift,
4945 kOptimizationDisabled = kCodeAgeShift + kCodeAgeSize,
4946 kStrictModeFunction,
4947 kUsesArguments,
4948 kHasDuplicateParameters,
4949 kNative,
4950 kBoundFunction,
4951 kIsAnonymous,
4952 kNameShouldPrintAsAnonymous,
4953 kCompilerHintsCount // Pseudo entry
4954 };
Steve Blocka7e24c12009-10-30 11:49:00 +00004955
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004956 private:
4957#if V8_HOST_ARCH_32_BIT
4958 // On 32 bit platforms, compiler hints is a smi.
4959 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4960 static const int kCompilerHintsSize = kPointerSize;
4961#else
4962 // On 64 bit platforms, compiler hints is not a smi, see comment above.
4963 static const int kCompilerHintsSmiTagSize = 0;
4964 static const int kCompilerHintsSize = kIntSize;
4965#endif
4966
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004967 STATIC_ASSERT(SharedFunctionInfo::kCompilerHintsCount <=
4968 SharedFunctionInfo::kCompilerHintsSize * kBitsPerByte);
4969
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004970 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00004971 // Constants for optimizing codegen for strict mode function and
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004972 // native tests.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004973 // Allows to use byte-widgh instructions.
4974 static const int kStrictModeBitWithinByte =
4975 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4976
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004977 static const int kNativeBitWithinByte =
4978 (kNative + kCompilerHintsSmiTagSize) % kBitsPerByte;
Ben Murdoch257744e2011-11-30 15:57:28 +00004979
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004980#if __BYTE_ORDER == __LITTLE_ENDIAN
4981 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004982 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004983 static const int kNativeByteOffset = kCompilerHintsOffset +
4984 (kNative + kCompilerHintsSmiTagSize) / kBitsPerByte;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004985#elif __BYTE_ORDER == __BIG_ENDIAN
4986 static const int kStrictModeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004987 (kCompilerHintsSize - 1) -
4988 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004989 static const int kNativeByteOffset = kCompilerHintsOffset +
Ben Murdoch257744e2011-11-30 15:57:28 +00004990 (kCompilerHintsSize - 1) -
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004991 ((kNative + kCompilerHintsSmiTagSize) / kBitsPerByte);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004992#else
4993#error Unknown byte ordering
4994#endif
4995
4996 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00004997 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4998};
4999
5000
5001// JSFunction describes JavaScript functions.
5002class JSFunction: public JSObject {
5003 public:
5004 // [prototype_or_initial_map]:
5005 DECL_ACCESSORS(prototype_or_initial_map, Object)
5006
5007 // [shared_function_info]: The information about the function that
5008 // can be shared by instances.
5009 DECL_ACCESSORS(shared, SharedFunctionInfo)
5010
Iain Merrick75681382010-08-19 15:07:18 +01005011 inline SharedFunctionInfo* unchecked_shared();
5012
Steve Blocka7e24c12009-10-30 11:49:00 +00005013 // [context]: The context for this function.
5014 inline Context* context();
5015 inline Object* unchecked_context();
5016 inline void set_context(Object* context);
5017
5018 // [code]: The generated code object for this function. Executed
5019 // when the function is invoked, e.g. foo() or new foo(). See
5020 // [[Call]] and [[Construct]] description in ECMA-262, section
5021 // 8.6.2, page 27.
5022 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005023 inline void set_code(Code* code);
5024 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00005025
Iain Merrick75681382010-08-19 15:07:18 +01005026 inline Code* unchecked_code();
5027
Steve Blocka7e24c12009-10-30 11:49:00 +00005028 // Tells whether this function is builtin.
5029 inline bool IsBuiltin();
5030
Ben Murdochb0fe1622011-05-05 13:52:32 +01005031 // Tells whether or not the function needs arguments adaption.
5032 inline bool NeedsArgumentsAdaption();
5033
5034 // Tells whether or not this function has been optimized.
5035 inline bool IsOptimized();
5036
Ben Murdoch8b112d22011-06-08 16:22:53 +01005037 // Tells whether or not this function can be optimized.
5038 inline bool IsOptimizable();
5039
Ben Murdochb0fe1622011-05-05 13:52:32 +01005040 // Mark this function for lazy recompilation. The function will be
5041 // recompiled the next time it is executed.
5042 void MarkForLazyRecompilation();
5043
5044 // Tells whether or not the function is already marked for lazy
5045 // recompilation.
5046 inline bool IsMarkedForLazyRecompilation();
5047
Ben Murdochb0fe1622011-05-05 13:52:32 +01005048 // Check whether or not this function is inlineable.
5049 bool IsInlineable();
5050
Steve Blocka7e24c12009-10-30 11:49:00 +00005051 // [literals]: Fixed array holding the materialized literals.
5052 //
5053 // If the function contains object, regexp or array literals, the
5054 // literals array prefix contains the object, regexp, and array
5055 // function to be used when creating these literals. This is
5056 // necessary so that we do not dynamically lookup the object, regexp
5057 // or array functions. Performing a dynamic lookup, we might end up
5058 // using the functions from a new context that we should not have
5059 // access to.
5060 DECL_ACCESSORS(literals, FixedArray)
5061
5062 // The initial map for an object created by this constructor.
5063 inline Map* initial_map();
5064 inline void set_initial_map(Map* value);
5065 inline bool has_initial_map();
5066
5067 // Get and set the prototype property on a JSFunction. If the
5068 // function has an initial map the prototype is set on the initial
5069 // map. Otherwise, the prototype is put in the initial map field
5070 // until an initial map is needed.
5071 inline bool has_prototype();
5072 inline bool has_instance_prototype();
5073 inline Object* prototype();
5074 inline Object* instance_prototype();
5075 Object* SetInstancePrototype(Object* value);
John Reck59135872010-11-02 12:39:01 -07005076 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005077
Steve Block6ded16b2010-05-10 14:33:55 +01005078 // After prototype is removed, it will not be created when accessed, and
5079 // [[Construct]] from this function will not be allowed.
5080 Object* RemovePrototype();
5081 inline bool should_have_prototype();
5082
Steve Blocka7e24c12009-10-30 11:49:00 +00005083 // Accessor for this function's initial map's [[class]]
5084 // property. This is primarily used by ECMA native functions. This
5085 // method sets the class_name field of this function's initial map
5086 // to a given value. It creates an initial map if this function does
5087 // not have one. Note that this method does not copy the initial map
5088 // if it has one already, but simply replaces it with the new value.
5089 // Instances created afterwards will have a map whose [[class]] is
5090 // set to 'value', but there is no guarantees on instances created
5091 // before.
5092 Object* SetInstanceClassName(String* name);
5093
5094 // Returns if this function has been compiled to native code yet.
5095 inline bool is_compiled();
5096
Ben Murdochb0fe1622011-05-05 13:52:32 +01005097 // [next_function_link]: Field for linking functions. This list is treated as
5098 // a weak list by the GC.
5099 DECL_ACCESSORS(next_function_link, Object)
5100
5101 // Prints the name of the function using PrintF.
5102 inline void PrintName() {
5103 PrintName(stdout);
5104 }
5105 void PrintName(FILE* out);
5106
Steve Blocka7e24c12009-10-30 11:49:00 +00005107 // Casting.
5108 static inline JSFunction* cast(Object* obj);
5109
Steve Block791712a2010-08-27 10:21:07 +01005110 // Iterates the objects, including code objects indirectly referenced
5111 // through pointers to the first instruction in the code object.
5112 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
5113
Steve Blocka7e24c12009-10-30 11:49:00 +00005114 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005115#ifdef OBJECT_PRINT
5116 inline void JSFunctionPrint() {
5117 JSFunctionPrint(stdout);
5118 }
5119 void JSFunctionPrint(FILE* out);
5120#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005121#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005122 void JSFunctionVerify();
5123#endif
5124
5125 // Returns the number of allocated literals.
5126 inline int NumberOfLiterals();
5127
5128 // Retrieve the global context from a function's literal array.
5129 static Context* GlobalContextFromLiterals(FixedArray* literals);
5130
Ben Murdochb0fe1622011-05-05 13:52:32 +01005131 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
5132 // kSize) is weak and has special handling during garbage collection.
Steve Block791712a2010-08-27 10:21:07 +01005133 static const int kCodeEntryOffset = JSObject::kHeaderSize;
Iain Merrick75681382010-08-19 15:07:18 +01005134 static const int kPrototypeOrInitialMapOffset =
Steve Block791712a2010-08-27 10:21:07 +01005135 kCodeEntryOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005136 static const int kSharedFunctionInfoOffset =
5137 kPrototypeOrInitialMapOffset + kPointerSize;
5138 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
5139 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005140 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
5141 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
5142 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005143
5144 // Layout of the literals array.
5145 static const int kLiteralsPrefixSize = 1;
5146 static const int kLiteralGlobalContextIndex = 0;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005147
Steve Blocka7e24c12009-10-30 11:49:00 +00005148 private:
5149 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
5150};
5151
5152
5153// JSGlobalProxy's prototype must be a JSGlobalObject or null,
5154// and the prototype is hidden. JSGlobalProxy always delegates
5155// property accesses to its prototype if the prototype is not null.
5156//
5157// A JSGlobalProxy can be reinitialized which will preserve its identity.
5158//
5159// Accessing a JSGlobalProxy requires security check.
5160
5161class JSGlobalProxy : public JSObject {
5162 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00005163 // [context]: the owner global context of this global proxy object.
Steve Blocka7e24c12009-10-30 11:49:00 +00005164 // It is null value if this object is not used by any context.
5165 DECL_ACCESSORS(context, Object)
5166
5167 // Casting.
5168 static inline JSGlobalProxy* cast(Object* obj);
5169
5170 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005171#ifdef OBJECT_PRINT
5172 inline void JSGlobalProxyPrint() {
5173 JSGlobalProxyPrint(stdout);
5174 }
5175 void JSGlobalProxyPrint(FILE* out);
5176#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005177#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005178 void JSGlobalProxyVerify();
5179#endif
5180
5181 // Layout description.
5182 static const int kContextOffset = JSObject::kHeaderSize;
5183 static const int kSize = kContextOffset + kPointerSize;
5184
5185 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00005186 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
5187};
5188
5189
5190// Forward declaration.
5191class JSBuiltinsObject;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005192class JSGlobalPropertyCell;
Steve Blocka7e24c12009-10-30 11:49:00 +00005193
5194// Common super class for JavaScript global objects and the special
5195// builtins global objects.
5196class GlobalObject: public JSObject {
5197 public:
5198 // [builtins]: the object holding the runtime routines written in JS.
5199 DECL_ACCESSORS(builtins, JSBuiltinsObject)
5200
5201 // [global context]: the global context corresponding to this global object.
5202 DECL_ACCESSORS(global_context, Context)
5203
5204 // [global receiver]: the global receiver object of the context
5205 DECL_ACCESSORS(global_receiver, JSObject)
5206
5207 // Retrieve the property cell used to store a property.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005208 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
Steve Blocka7e24c12009-10-30 11:49:00 +00005209
John Reck59135872010-11-02 12:39:01 -07005210 // This is like GetProperty, but is used when you know the lookup won't fail
5211 // by throwing an exception. This is for the debug and builtins global
5212 // objects, where it is known which properties can be expected to be present
5213 // on the object.
5214 Object* GetPropertyNoExceptionThrown(String* key) {
5215 Object* answer = GetProperty(key)->ToObjectUnchecked();
5216 return answer;
5217 }
5218
Steve Blocka7e24c12009-10-30 11:49:00 +00005219 // Ensure that the global object has a cell for the given property name.
John Reck59135872010-11-02 12:39:01 -07005220 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
Steve Blocka7e24c12009-10-30 11:49:00 +00005221
5222 // Casting.
5223 static inline GlobalObject* cast(Object* obj);
5224
5225 // Layout description.
5226 static const int kBuiltinsOffset = JSObject::kHeaderSize;
5227 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
5228 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
5229 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
5230
5231 private:
5232 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
5233
5234 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
5235};
5236
5237
5238// JavaScript global object.
5239class JSGlobalObject: public GlobalObject {
5240 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00005241 // Casting.
5242 static inline JSGlobalObject* cast(Object* obj);
5243
5244 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005245#ifdef OBJECT_PRINT
5246 inline void JSGlobalObjectPrint() {
5247 JSGlobalObjectPrint(stdout);
5248 }
5249 void JSGlobalObjectPrint(FILE* out);
5250#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005251#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005252 void JSGlobalObjectVerify();
5253#endif
5254
5255 // Layout description.
5256 static const int kSize = GlobalObject::kHeaderSize;
5257
5258 private:
5259 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
5260};
5261
5262
5263// Builtins global object which holds the runtime routines written in
5264// JavaScript.
5265class JSBuiltinsObject: public GlobalObject {
5266 public:
5267 // Accessors for the runtime routines written in JavaScript.
5268 inline Object* javascript_builtin(Builtins::JavaScript id);
5269 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
5270
Steve Block6ded16b2010-05-10 14:33:55 +01005271 // Accessors for code of the runtime routines written in JavaScript.
5272 inline Code* javascript_builtin_code(Builtins::JavaScript id);
5273 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
5274
Steve Blocka7e24c12009-10-30 11:49:00 +00005275 // Casting.
5276 static inline JSBuiltinsObject* cast(Object* obj);
5277
5278 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005279#ifdef OBJECT_PRINT
5280 inline void JSBuiltinsObjectPrint() {
5281 JSBuiltinsObjectPrint(stdout);
5282 }
5283 void JSBuiltinsObjectPrint(FILE* out);
5284#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005285#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005286 void JSBuiltinsObjectVerify();
5287#endif
5288
5289 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01005290 // room for two pointers per runtime routine written in javascript
5291 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00005292 static const int kJSBuiltinsCount = Builtins::id_count;
5293 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005294 static const int kJSBuiltinsCodeOffset =
5295 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00005296 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01005297 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
5298
5299 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
5300 return kJSBuiltinsOffset + id * kPointerSize;
5301 }
5302
5303 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
5304 return kJSBuiltinsCodeOffset + id * kPointerSize;
5305 }
5306
Steve Blocka7e24c12009-10-30 11:49:00 +00005307 private:
5308 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
5309};
5310
5311
5312// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
5313class JSValue: public JSObject {
5314 public:
5315 // [value]: the object being wrapped.
5316 DECL_ACCESSORS(value, Object)
5317
5318 // Casting.
5319 static inline JSValue* cast(Object* obj);
5320
5321 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01005322#ifdef OBJECT_PRINT
5323 inline void JSValuePrint() {
5324 JSValuePrint(stdout);
5325 }
5326 void JSValuePrint(FILE* out);
5327#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00005328#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00005329 void JSValueVerify();
5330#endif
5331
5332 // Layout description.
5333 static const int kValueOffset = JSObject::kHeaderSize;
5334 static const int kSize = kValueOffset + kPointerSize;
5335
5336 private:
5337 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
5338};
5339
Steve Block1e0659c2011-05-24 12:43:12 +01005340
5341// Representation of message objects used for error reporting through
5342// the API. The messages are formatted in JavaScript so this object is
5343// a real JavaScript object. The information used for formatting the
5344// error messages are not directly accessible from JavaScript to
5345// prevent leaking information to user code called during error
5346// formatting.
5347class JSMessageObject: public JSObject {
5348 public:
5349 // [type]: the type of error message.
5350 DECL_ACCESSORS(type, String)
5351
5352 // [arguments]: the arguments for formatting the error message.
5353 DECL_ACCESSORS(arguments, JSArray)
5354
5355 // [script]: the script from which the error message originated.
5356 DECL_ACCESSORS(script, Object)
5357
5358 // [stack_trace]: the stack trace for this error message.
5359 DECL_ACCESSORS(stack_trace, Object)
5360
5361 // [stack_frames]: an array of stack frames for this error object.
5362 DECL_ACCESSORS(stack_frames, Object)
5363
5364 // [start_position]: the start position in the script for the error message.
5365 inline int start_position();
5366 inline void set_start_position(int value);
5367
5368 // [end_position]: the end position in the script for the error message.
5369 inline int end_position();
5370 inline void set_end_position(int value);
5371
5372 // Casting.
5373 static inline JSMessageObject* cast(Object* obj);
5374
5375 // Dispatched behavior.
5376#ifdef OBJECT_PRINT
5377 inline void JSMessageObjectPrint() {
5378 JSMessageObjectPrint(stdout);
5379 }
5380 void JSMessageObjectPrint(FILE* out);
5381#endif
5382#ifdef DEBUG
5383 void JSMessageObjectVerify();
5384#endif
5385
5386 // Layout description.
5387 static const int kTypeOffset = JSObject::kHeaderSize;
5388 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
5389 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
5390 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
5391 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
5392 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
5393 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
5394 static const int kSize = kEndPositionOffset + kPointerSize;
5395
5396 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
5397 kStackFramesOffset + kPointerSize,
5398 kSize> BodyDescriptor;
5399};
5400
5401
Steve Blocka7e24c12009-10-30 11:49:00 +00005402// Regular expressions
5403// The regular expression holds a single reference to a FixedArray in
5404// the kDataOffset field.
5405// The FixedArray contains the following data:
5406// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
5407// - reference to the original source string
5408// - reference to the original flag string
5409// If it is an atom regexp
5410// - a reference to a literal string to search for
5411// If it is an irregexp regexp:
Ben Murdoch257744e2011-11-30 15:57:28 +00005412// - a reference to code for ASCII inputs (bytecode or compiled), or a smi
5413// used for tracking the last usage (used for code flushing).
5414// - a reference to code for UC16 inputs (bytecode or compiled), or a smi
5415// used for tracking the last usage (used for code flushing)..
Steve Blocka7e24c12009-10-30 11:49:00 +00005416// - max number of registers used by irregexp implementations.
5417// - number of capture registers (output values) of the regexp.
5418class JSRegExp: public JSObject {
5419 public:
5420 // Meaning of Type:
5421 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
5422 // ATOM: A simple string to match against using an indexOf operation.
5423 // IRREGEXP: Compiled with Irregexp.
5424 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
5425 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
5426 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
5427
5428 class Flags {
5429 public:
5430 explicit Flags(uint32_t value) : value_(value) { }
5431 bool is_global() { return (value_ & GLOBAL) != 0; }
5432 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
5433 bool is_multiline() { return (value_ & MULTILINE) != 0; }
5434 uint32_t value() { return value_; }
5435 private:
5436 uint32_t value_;
5437 };
5438
5439 DECL_ACCESSORS(data, Object)
5440
5441 inline Type TypeTag();
5442 inline int CaptureCount();
5443 inline Flags GetFlags();
5444 inline String* Pattern();
5445 inline Object* DataAt(int index);
5446 // Set implementation data after the object has been prepared.
5447 inline void SetDataAt(int index, Object* value);
Ben Murdoch257744e2011-11-30 15:57:28 +00005448
5449 // Used during GC when flushing code or setting age.
5450 inline Object* DataAtUnchecked(int index);
5451 inline void SetDataAtUnchecked(int index, Object* value, Heap* heap);
5452 inline Type TypeTagUnchecked();
5453
Steve Blocka7e24c12009-10-30 11:49:00 +00005454 static int code_index(bool is_ascii) {
5455 if (is_ascii) {
5456 return kIrregexpASCIICodeIndex;
5457 } else {
5458 return kIrregexpUC16CodeIndex;
5459 }
5460 }
5461
Ben Murdoch257744e2011-11-30 15:57:28 +00005462 static int saved_code_index(bool is_ascii) {
5463 if (is_ascii) {
5464 return kIrregexpASCIICodeSavedIndex;
5465 } else {
5466 return kIrregexpUC16CodeSavedIndex;
5467 }
5468 }
5469
Steve Blocka7e24c12009-10-30 11:49:00 +00005470 static inline JSRegExp* cast(Object* obj);
5471
5472 // Dispatched behavior.
5473#ifdef DEBUG
5474 void JSRegExpVerify();
5475#endif
5476
5477 static const int kDataOffset = JSObject::kHeaderSize;
5478 static const int kSize = kDataOffset + kPointerSize;
5479
5480 // Indices in the data array.
5481 static const int kTagIndex = 0;
5482 static const int kSourceIndex = kTagIndex + 1;
5483 static const int kFlagsIndex = kSourceIndex + 1;
5484 static const int kDataIndex = kFlagsIndex + 1;
5485 // The data fields are used in different ways depending on the
5486 // value of the tag.
5487 // Atom regexps (literal strings).
5488 static const int kAtomPatternIndex = kDataIndex;
5489
5490 static const int kAtomDataSize = kAtomPatternIndex + 1;
5491
5492 // Irregexp compiled code or bytecode for ASCII. If compilation
5493 // fails, this fields hold an exception object that should be
5494 // thrown if the regexp is used again.
5495 static const int kIrregexpASCIICodeIndex = kDataIndex;
5496 // Irregexp compiled code or bytecode for UC16. If compilation
5497 // fails, this fields hold an exception object that should be
5498 // thrown if the regexp is used again.
5499 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00005500
5501 // Saved instance of Irregexp compiled code or bytecode for ASCII that
5502 // is a potential candidate for flushing.
5503 static const int kIrregexpASCIICodeSavedIndex = kDataIndex + 2;
5504 // Saved instance of Irregexp compiled code or bytecode for UC16 that is
5505 // a potential candidate for flushing.
5506 static const int kIrregexpUC16CodeSavedIndex = kDataIndex + 3;
5507
Steve Blocka7e24c12009-10-30 11:49:00 +00005508 // Maximal number of registers used by either ASCII or UC16.
5509 // Only used to check that there is enough stack space
Ben Murdoch257744e2011-11-30 15:57:28 +00005510 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +00005511 // Number of captures in the compiled regexp.
Ben Murdoch257744e2011-11-30 15:57:28 +00005512 static const int kIrregexpCaptureCountIndex = kDataIndex + 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00005513
5514 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00005515
5516 // Offsets directly into the data fixed array.
5517 static const int kDataTagOffset =
5518 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5519 static const int kDataAsciiCodeOffset =
5520 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00005521 static const int kDataUC16CodeOffset =
5522 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00005523 static const int kIrregexpCaptureCountOffset =
5524 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01005525
5526 // In-object fields.
5527 static const int kSourceFieldIndex = 0;
5528 static const int kGlobalFieldIndex = 1;
5529 static const int kIgnoreCaseFieldIndex = 2;
5530 static const int kMultilineFieldIndex = 3;
5531 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01005532 static const int kInObjectFieldCount = 5;
Ben Murdoch257744e2011-11-30 15:57:28 +00005533
5534 // The uninitialized value for a regexp code object.
5535 static const int kUninitializedValue = -1;
5536
5537 // The compilation error value for the regexp code object. The real error
5538 // object is in the saved code field.
5539 static const int kCompilationErrorValue = -2;
5540
5541 // When we store the sweep generation at which we moved the code from the
5542 // code index to the saved code index we mask it of to be in the [0:255]
5543 // range.
5544 static const int kCodeAgeMask = 0xff;
Steve Blocka7e24c12009-10-30 11:49:00 +00005545};
5546
5547
5548class CompilationCacheShape {
5549 public:
5550 static inline bool IsMatch(HashTableKey* key, Object* value) {
5551 return key->IsMatch(value);
5552 }
5553
5554 static inline uint32_t Hash(HashTableKey* key) {
5555 return key->Hash();
5556 }
5557
5558 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5559 return key->HashForObject(object);
5560 }
5561
John Reck59135872010-11-02 12:39:01 -07005562 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00005563 return key->AsObject();
5564 }
5565
5566 static const int kPrefixSize = 0;
5567 static const int kEntrySize = 2;
5568};
5569
Steve Block3ce2e202009-11-05 08:53:23 +00005570
Steve Blocka7e24c12009-10-30 11:49:00 +00005571class CompilationCacheTable: public HashTable<CompilationCacheShape,
5572 HashTableKey*> {
5573 public:
5574 // Find cached value for a string key, otherwise return null.
5575 Object* Lookup(String* src);
Steve Block1e0659c2011-05-24 12:43:12 +01005576 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00005577 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005578 MaybeObject* Put(String* src, Object* value);
Steve Block1e0659c2011-05-24 12:43:12 +01005579 MaybeObject* PutEval(String* src,
5580 Context* context,
5581 SharedFunctionInfo* value);
John Reck59135872010-11-02 12:39:01 -07005582 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005583
Ben Murdochb0fe1622011-05-05 13:52:32 +01005584 // Remove given value from cache.
5585 void Remove(Object* value);
5586
Steve Blocka7e24c12009-10-30 11:49:00 +00005587 static inline CompilationCacheTable* cast(Object* obj);
5588
5589 private:
5590 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
5591};
5592
5593
Steve Block6ded16b2010-05-10 14:33:55 +01005594class CodeCache: public Struct {
5595 public:
5596 DECL_ACCESSORS(default_cache, FixedArray)
5597 DECL_ACCESSORS(normal_type_cache, Object)
5598
5599 // Add the code object to the cache.
John Reck59135872010-11-02 12:39:01 -07005600 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005601
5602 // Lookup code object in the cache. Returns code object if found and undefined
5603 // if not.
5604 Object* Lookup(String* name, Code::Flags flags);
5605
5606 // Get the internal index of a code object in the cache. Returns -1 if the
5607 // code object is not in that cache. This index can be used to later call
5608 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5609 // RemoveByIndex.
5610 int GetIndex(Object* name, Code* code);
5611
5612 // Remove an object from the cache with the provided internal index.
5613 void RemoveByIndex(Object* name, Code* code, int index);
5614
5615 static inline CodeCache* cast(Object* obj);
5616
Ben Murdochb0fe1622011-05-05 13:52:32 +01005617#ifdef OBJECT_PRINT
5618 inline void CodeCachePrint() {
5619 CodeCachePrint(stdout);
5620 }
5621 void CodeCachePrint(FILE* out);
5622#endif
Steve Block6ded16b2010-05-10 14:33:55 +01005623#ifdef DEBUG
Steve Block6ded16b2010-05-10 14:33:55 +01005624 void CodeCacheVerify();
5625#endif
5626
5627 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5628 static const int kNormalTypeCacheOffset =
5629 kDefaultCacheOffset + kPointerSize;
5630 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5631
5632 private:
John Reck59135872010-11-02 12:39:01 -07005633 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5634 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005635 Object* LookupDefaultCache(String* name, Code::Flags flags);
5636 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5637
5638 // Code cache layout of the default cache. Elements are alternating name and
5639 // code objects for non normal load/store/call IC's.
5640 static const int kCodeCacheEntrySize = 2;
5641 static const int kCodeCacheEntryNameOffset = 0;
5642 static const int kCodeCacheEntryCodeOffset = 1;
5643
5644 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5645};
5646
5647
5648class CodeCacheHashTableShape {
5649 public:
5650 static inline bool IsMatch(HashTableKey* key, Object* value) {
5651 return key->IsMatch(value);
5652 }
5653
5654 static inline uint32_t Hash(HashTableKey* key) {
5655 return key->Hash();
5656 }
5657
5658 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5659 return key->HashForObject(object);
5660 }
5661
John Reck59135872010-11-02 12:39:01 -07005662 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
Steve Block6ded16b2010-05-10 14:33:55 +01005663 return key->AsObject();
5664 }
5665
5666 static const int kPrefixSize = 0;
5667 static const int kEntrySize = 2;
5668};
5669
5670
5671class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5672 HashTableKey*> {
5673 public:
5674 Object* Lookup(String* name, Code::Flags flags);
John Reck59135872010-11-02 12:39:01 -07005675 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
Steve Block6ded16b2010-05-10 14:33:55 +01005676
5677 int GetIndex(String* name, Code::Flags flags);
5678 void RemoveByIndex(int index);
5679
5680 static inline CodeCacheHashTable* cast(Object* obj);
5681
5682 // Initial size of the fixed array backing the hash table.
5683 static const int kInitialSize = 64;
5684
5685 private:
5686 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5687};
5688
5689
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005690class PolymorphicCodeCache: public Struct {
5691 public:
5692 DECL_ACCESSORS(cache, Object)
5693
5694 MUST_USE_RESULT MaybeObject* Update(MapList* maps,
5695 Code::Flags flags,
5696 Code* code);
5697 Object* Lookup(MapList* maps, Code::Flags flags);
5698
5699 static inline PolymorphicCodeCache* cast(Object* obj);
5700
5701#ifdef OBJECT_PRINT
5702 inline void PolymorphicCodeCachePrint() {
5703 PolymorphicCodeCachePrint(stdout);
5704 }
5705 void PolymorphicCodeCachePrint(FILE* out);
5706#endif
5707#ifdef DEBUG
5708 void PolymorphicCodeCacheVerify();
5709#endif
5710
5711 static const int kCacheOffset = HeapObject::kHeaderSize;
5712 static const int kSize = kCacheOffset + kPointerSize;
5713
5714 private:
5715 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCache);
5716};
5717
5718
5719class PolymorphicCodeCacheHashTable
5720 : public HashTable<CodeCacheHashTableShape, HashTableKey*> {
5721 public:
5722 Object* Lookup(MapList* maps, int code_kind);
5723 MUST_USE_RESULT MaybeObject* Put(MapList* maps, int code_kind, Code* code);
5724
5725 static inline PolymorphicCodeCacheHashTable* cast(Object* obj);
5726
5727 static const int kInitialSize = 64;
5728 private:
5729 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCacheHashTable);
5730};
5731
5732
Steve Blocka7e24c12009-10-30 11:49:00 +00005733enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5734enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5735
5736
5737class StringHasher {
5738 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +01005739 explicit inline StringHasher(int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00005740
5741 // Returns true if the hash of this string can be computed without
5742 // looking at the contents.
5743 inline bool has_trivial_hash();
5744
5745 // Add a character to the hash and update the array index calculation.
5746 inline void AddCharacter(uc32 c);
5747
5748 // Adds a character to the hash but does not update the array index
5749 // calculation. This can only be called when it has been verified
5750 // that the input is not an array index.
5751 inline void AddCharacterNoIndex(uc32 c);
5752
5753 // Returns the value to store in the hash field of a string with
5754 // the given length and contents.
5755 uint32_t GetHashField();
5756
5757 // Returns true if the characters seen so far make up a legal array
5758 // index.
5759 bool is_array_index() { return is_array_index_; }
5760
5761 bool is_valid() { return is_valid_; }
5762
5763 void invalidate() { is_valid_ = false; }
5764
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005765 // Calculated hash value for a string consisting of 1 to
5766 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5767 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005768 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005769
Steve Blocka7e24c12009-10-30 11:49:00 +00005770 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00005771 uint32_t array_index() {
5772 ASSERT(is_array_index());
5773 return array_index_;
5774 }
5775
5776 inline uint32_t GetHash();
5777
5778 int length_;
5779 uint32_t raw_running_hash_;
5780 uint32_t array_index_;
5781 bool is_array_index_;
5782 bool is_first_char_;
5783 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00005784 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00005785};
5786
5787
Steve Block44f0eee2011-05-26 01:26:41 +01005788// Calculates string hash.
5789template <typename schar>
5790inline uint32_t HashSequentialString(const schar* chars, int length);
5791
5792
Steve Blocka7e24c12009-10-30 11:49:00 +00005793// The characteristics of a string are stored in its map. Retrieving these
5794// few bits of information is moderately expensive, involving two memory
5795// loads where the second is dependent on the first. To improve efficiency
5796// the shape of the string is given its own class so that it can be retrieved
5797// once and used for several string operations. A StringShape is small enough
5798// to be passed by value and is immutable, but be aware that flattening a
5799// string can potentially alter its shape. Also be aware that a GC caused by
5800// something else can alter the shape of a string due to ConsString
5801// shortcutting. Keeping these restrictions in mind has proven to be error-
5802// prone and so we no longer put StringShapes in variables unless there is a
5803// concrete performance benefit at that particular point in the code.
5804class StringShape BASE_EMBEDDED {
5805 public:
5806 inline explicit StringShape(String* s);
5807 inline explicit StringShape(Map* s);
5808 inline explicit StringShape(InstanceType t);
5809 inline bool IsSequential();
5810 inline bool IsExternal();
5811 inline bool IsCons();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005812 inline bool IsSliced();
5813 inline bool IsIndirect();
Steve Blocka7e24c12009-10-30 11:49:00 +00005814 inline bool IsExternalAscii();
5815 inline bool IsExternalTwoByte();
5816 inline bool IsSequentialAscii();
5817 inline bool IsSequentialTwoByte();
5818 inline bool IsSymbol();
5819 inline StringRepresentationTag representation_tag();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005820 inline uint32_t encoding_tag();
Steve Blocka7e24c12009-10-30 11:49:00 +00005821 inline uint32_t full_representation_tag();
5822 inline uint32_t size_tag();
5823#ifdef DEBUG
5824 inline uint32_t type() { return type_; }
5825 inline void invalidate() { valid_ = false; }
5826 inline bool valid() { return valid_; }
5827#else
5828 inline void invalidate() { }
5829#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005830
Steve Blocka7e24c12009-10-30 11:49:00 +00005831 private:
5832 uint32_t type_;
5833#ifdef DEBUG
5834 inline void set_valid() { valid_ = true; }
5835 bool valid_;
5836#else
5837 inline void set_valid() { }
5838#endif
5839};
5840
5841
5842// The String abstract class captures JavaScript string values:
5843//
5844// Ecma-262:
5845// 4.3.16 String Value
5846// A string value is a member of the type String and is a finite
5847// ordered sequence of zero or more 16-bit unsigned integer values.
5848//
5849// All string values have a length field.
5850class String: public HeapObject {
5851 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005852 // Representation of the flat content of a String.
5853 // A non-flat string doesn't have flat content.
5854 // A flat string has content that's encoded as a sequence of either
5855 // ASCII chars or two-byte UC16.
5856 // Returned by String::GetFlatContent().
5857 class FlatContent {
5858 public:
5859 // Returns true if the string is flat and this structure contains content.
5860 bool IsFlat() { return state_ != NON_FLAT; }
5861 // Returns true if the structure contains ASCII content.
5862 bool IsAscii() { return state_ == ASCII; }
5863 // Returns true if the structure contains two-byte content.
5864 bool IsTwoByte() { return state_ == TWO_BYTE; }
5865
5866 // Return the ASCII content of the string. Only use if IsAscii() returns
5867 // true.
5868 Vector<const char> ToAsciiVector() {
5869 ASSERT_EQ(ASCII, state_);
5870 return Vector<const char>::cast(buffer_);
5871 }
5872 // Return the two-byte content of the string. Only use if IsTwoByte()
5873 // returns true.
5874 Vector<const uc16> ToUC16Vector() {
5875 ASSERT_EQ(TWO_BYTE, state_);
5876 return Vector<const uc16>::cast(buffer_);
5877 }
5878
5879 private:
5880 enum State { NON_FLAT, ASCII, TWO_BYTE };
5881
5882 // Constructors only used by String::GetFlatContent().
5883 explicit FlatContent(Vector<const char> chars)
5884 : buffer_(Vector<const byte>::cast(chars)),
5885 state_(ASCII) { }
5886 explicit FlatContent(Vector<const uc16> chars)
5887 : buffer_(Vector<const byte>::cast(chars)),
5888 state_(TWO_BYTE) { }
5889 FlatContent() : buffer_(), state_(NON_FLAT) { }
5890
5891 Vector<const byte> buffer_;
5892 State state_;
5893
5894 friend class String;
5895 };
5896
Steve Blocka7e24c12009-10-30 11:49:00 +00005897 // Get and set the length of the string.
5898 inline int length();
5899 inline void set_length(int value);
5900
Steve Blockd0582a62009-12-15 09:54:21 +00005901 // Get and set the hash field of the string.
5902 inline uint32_t hash_field();
5903 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00005904
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005905 // Returns whether this string has only ASCII chars, i.e. all of them can
5906 // be ASCII encoded. This might be the case even if the string is
5907 // two-byte. Such strings may appear when the embedder prefers
5908 // two-byte external representations even for ASCII data.
Steve Blocka7e24c12009-10-30 11:49:00 +00005909 inline bool IsAsciiRepresentation();
5910 inline bool IsTwoByteRepresentation();
5911
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005912 // Cons and slices have an encoding flag that may not represent the actual
5913 // encoding of the underlying string. This is taken into account here.
5914 // Requires: this->IsFlat()
5915 inline bool IsAsciiRepresentationUnderneath();
5916 inline bool IsTwoByteRepresentationUnderneath();
5917
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01005918 // NOTE: this should be considered only a hint. False negatives are
5919 // possible.
5920 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01005921
Steve Blocka7e24c12009-10-30 11:49:00 +00005922 // Get and set individual two byte chars in the string.
5923 inline void Set(int index, uint16_t value);
5924 // Get individual two byte char in the string. Repeated calls
5925 // to this method are not efficient unless the string is flat.
5926 inline uint16_t Get(int index);
5927
Leon Clarkef7060e22010-06-03 12:02:55 +01005928 // Try to flatten the string. Checks first inline to see if it is
5929 // necessary. Does nothing if the string is not a cons string.
5930 // Flattening allocates a sequential string with the same data as
5931 // the given string and mutates the cons string to a degenerate
5932 // form, where the first component is the new sequential string and
5933 // the second component is the empty string. If allocation fails,
5934 // this function returns a failure. If flattening succeeds, this
5935 // function returns the sequential string that is now the first
5936 // component of the cons string.
5937 //
5938 // Degenerate cons strings are handled specially by the garbage
5939 // collector (see IsShortcutCandidate).
5940 //
5941 // Use FlattenString from Handles.cc to flatten even in case an
5942 // allocation failure happens.
John Reck59135872010-11-02 12:39:01 -07005943 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005944
Leon Clarkef7060e22010-06-03 12:02:55 +01005945 // Convenience function. Has exactly the same behavior as
5946 // TryFlatten(), except in the case of failure returns the original
5947 // string.
5948 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5949
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005950 // Tries to return the content of a flat string as a structure holding either
5951 // a flat vector of char or of uc16.
5952 // If the string isn't flat, and therefore doesn't have flat content, the
5953 // returned structure will report so, and can't provide a vector of either
5954 // kind.
5955 FlatContent GetFlatContent();
5956
5957 // Returns the parent of a sliced string or first part of a flat cons string.
5958 // Requires: StringShape(this).IsIndirect() && this->IsFlat()
5959 inline String* GetUnderlying();
Steve Blocka7e24c12009-10-30 11:49:00 +00005960
5961 // Mark the string as an undetectable object. It only applies to
5962 // ascii and two byte string types.
5963 bool MarkAsUndetectable();
5964
Steve Blockd0582a62009-12-15 09:54:21 +00005965 // Return a substring.
John Reck59135872010-11-02 12:39:01 -07005966 MUST_USE_RESULT MaybeObject* SubString(int from,
5967 int to,
5968 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00005969
5970 // String equality operations.
5971 inline bool Equals(String* other);
5972 bool IsEqualTo(Vector<const char> str);
Steve Block9fac8402011-05-12 15:51:54 +01005973 bool IsAsciiEqualTo(Vector<const char> str);
5974 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00005975
5976 // Return a UTF8 representation of the string. The string is null
5977 // terminated but may optionally contain nulls. Length is returned
5978 // in length_output if length_output is not a null pointer The string
5979 // should be nearly flat, otherwise the performance of this method may
5980 // be very slow (quadratic in the length). Setting robustness_flag to
5981 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5982 // handles unexpected data without causing assert failures and it does not
5983 // do any heap allocations. This is useful when printing stack traces.
5984 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5985 RobustnessFlag robustness_flag,
5986 int offset,
5987 int length,
5988 int* length_output = 0);
5989 SmartPointer<char> ToCString(
5990 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5991 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5992 int* length_output = 0);
5993
5994 int Utf8Length();
5995
5996 // Return a 16 bit Unicode representation of the string.
5997 // The string should be nearly flat, otherwise the performance of
5998 // of this method may be very bad. Setting robustness_flag to
5999 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
6000 // handles unexpected data without causing assert failures and it does not
6001 // do any heap allocations. This is useful when printing stack traces.
6002 SmartPointer<uc16> ToWideCString(
6003 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
6004
6005 // Tells whether the hash code has been computed.
6006 inline bool HasHashCode();
6007
6008 // Returns a hash value used for the property table
6009 inline uint32_t Hash();
6010
Steve Blockd0582a62009-12-15 09:54:21 +00006011 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
6012 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00006013
6014 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
6015 uint32_t* index,
6016 int length);
6017
6018 // Externalization.
6019 bool MakeExternal(v8::String::ExternalStringResource* resource);
6020 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
6021
6022 // Conversion.
6023 inline bool AsArrayIndex(uint32_t* index);
6024
6025 // Casting.
6026 static inline String* cast(Object* obj);
6027
6028 void PrintOn(FILE* out);
6029
6030 // For use during stack traces. Performs rudimentary sanity check.
6031 bool LooksValid();
6032
6033 // Dispatched behavior.
6034 void StringShortPrint(StringStream* accumulator);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006035#ifdef OBJECT_PRINT
6036 inline void StringPrint() {
6037 StringPrint(stdout);
6038 }
6039 void StringPrint(FILE* out);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006040
6041 char* ToAsciiArray();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006042#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006043#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006044 void StringVerify();
6045#endif
6046 inline bool IsFlat();
6047
6048 // Layout description.
6049 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01006050 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006051 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006052
Steve Blockd0582a62009-12-15 09:54:21 +00006053 // Maximum number of characters to consider when trying to convert a string
6054 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00006055 static const int kMaxArrayIndexSize = 10;
6056
6057 // Max ascii char code.
6058 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
6059 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
6060 static const int kMaxUC16CharCode = 0xffff;
6061
Steve Blockd0582a62009-12-15 09:54:21 +00006062 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00006063 static const int kMinNonFlatLength = 13;
6064
6065 // Mask constant for checking if a string has a computed hash code
6066 // and if it is an array index. The least significant bit indicates
6067 // whether a hash code has been computed. If the hash code has been
6068 // computed the 2nd bit tells whether the string can be used as an
6069 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006070 static const int kHashNotComputedMask = 1;
6071 static const int kIsNotArrayIndexMask = 1 << 1;
6072 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00006073
Steve Blockd0582a62009-12-15 09:54:21 +00006074 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006075 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00006076
Steve Blocka7e24c12009-10-30 11:49:00 +00006077 // Array index strings this short can keep their index in the hash
6078 // field.
6079 static const int kMaxCachedArrayIndexLength = 7;
6080
Steve Blockd0582a62009-12-15 09:54:21 +00006081 // For strings which are array indexes the hash value has the string length
6082 // mixed into the hash, mainly to avoid a hash value of zero which would be
6083 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006084 static const int kArrayIndexValueBits = 24;
6085 static const int kArrayIndexLengthBits =
6086 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
6087
6088 STATIC_CHECK((kArrayIndexLengthBits > 0));
Iain Merrick9ac36c92010-09-13 15:29:50 +01006089 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006090
6091 static const int kArrayIndexHashLengthShift =
6092 kArrayIndexValueBits + kNofHashBitFields;
6093
Steve Blockd0582a62009-12-15 09:54:21 +00006094 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006095
6096 static const int kArrayIndexValueMask =
6097 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
6098
6099 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
6100 // could use a mask to test if the length of string is less than or equal to
6101 // kMaxCachedArrayIndexLength.
6102 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
6103
6104 static const int kContainsCachedArrayIndexMask =
6105 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
6106 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00006107
6108 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006109 static const int kEmptyHashField =
6110 kIsNotArrayIndexMask | kHashNotComputedMask;
6111
6112 // Value of hash field containing computed hash equal to zero.
6113 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00006114
6115 // Maximal string length.
6116 static const int kMaxLength = (1 << (32 - 2)) - 1;
6117
6118 // Max length for computing hash. For strings longer than this limit the
6119 // string length is used as the hash value.
6120 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00006121
6122 // Limit for truncation in short printing.
6123 static const int kMaxShortPrintLength = 1024;
6124
6125 // Support for regular expressions.
6126 const uc16* GetTwoByteData();
6127 const uc16* GetTwoByteData(unsigned start);
6128
6129 // Support for StringInputBuffer
6130 static const unibrow::byte* ReadBlock(String* input,
6131 unibrow::byte* util_buffer,
6132 unsigned capacity,
6133 unsigned* remaining,
6134 unsigned* offset);
6135 static const unibrow::byte* ReadBlock(String** input,
6136 unibrow::byte* util_buffer,
6137 unsigned capacity,
6138 unsigned* remaining,
6139 unsigned* offset);
6140
6141 // Helper function for flattening strings.
6142 template <typename sinkchar>
6143 static void WriteToFlat(String* source,
6144 sinkchar* sink,
6145 int from,
6146 int to);
6147
Steve Block9fac8402011-05-12 15:51:54 +01006148 static inline bool IsAscii(const char* chars, int length) {
6149 const char* limit = chars + length;
6150#ifdef V8_HOST_CAN_READ_UNALIGNED
6151 ASSERT(kMaxAsciiCharCode == 0x7F);
6152 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
6153 while (chars <= limit - sizeof(uintptr_t)) {
6154 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
6155 return false;
6156 }
6157 chars += sizeof(uintptr_t);
6158 }
6159#endif
6160 while (chars < limit) {
6161 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
6162 ++chars;
6163 }
6164 return true;
6165 }
6166
6167 static inline bool IsAscii(const uc16* chars, int length) {
6168 const uc16* limit = chars + length;
6169 while (chars < limit) {
6170 if (*chars > kMaxAsciiCharCodeU) return false;
6171 ++chars;
6172 }
6173 return true;
6174 }
6175
Steve Blocka7e24c12009-10-30 11:49:00 +00006176 protected:
6177 class ReadBlockBuffer {
6178 public:
6179 ReadBlockBuffer(unibrow::byte* util_buffer_,
6180 unsigned cursor_,
6181 unsigned capacity_,
6182 unsigned remaining_) :
6183 util_buffer(util_buffer_),
6184 cursor(cursor_),
6185 capacity(capacity_),
6186 remaining(remaining_) {
6187 }
6188 unibrow::byte* util_buffer;
6189 unsigned cursor;
6190 unsigned capacity;
6191 unsigned remaining;
6192 };
6193
Steve Blocka7e24c12009-10-30 11:49:00 +00006194 static inline const unibrow::byte* ReadBlock(String* input,
6195 ReadBlockBuffer* buffer,
6196 unsigned* offset,
6197 unsigned max_chars);
6198 static void ReadBlockIntoBuffer(String* input,
6199 ReadBlockBuffer* buffer,
6200 unsigned* offset_ptr,
6201 unsigned max_chars);
6202
6203 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01006204 // Try to flatten the top level ConsString that is hiding behind this
6205 // string. This is a no-op unless the string is a ConsString. Flatten
6206 // mutates the ConsString and might return a failure.
John Reck59135872010-11-02 12:39:01 -07006207 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
Leon Clarkef7060e22010-06-03 12:02:55 +01006208
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006209 static inline bool IsHashFieldComputed(uint32_t field);
6210
Steve Blocka7e24c12009-10-30 11:49:00 +00006211 // Slow case of String::Equals. This implementation works on any strings
6212 // but it is most efficient on strings that are almost flat.
6213 bool SlowEquals(String* other);
6214
6215 // Slow case of AsArrayIndex.
6216 bool SlowAsArrayIndex(uint32_t* index);
6217
6218 // Compute and set the hash code.
6219 uint32_t ComputeAndSetHash();
6220
6221 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
6222};
6223
6224
6225// The SeqString abstract class captures sequential string values.
6226class SeqString: public String {
6227 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00006228 // Casting.
6229 static inline SeqString* cast(Object* obj);
6230
Steve Blocka7e24c12009-10-30 11:49:00 +00006231 private:
6232 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
6233};
6234
6235
6236// The AsciiString class captures sequential ascii string objects.
6237// Each character in the AsciiString is an ascii character.
6238class SeqAsciiString: public SeqString {
6239 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006240 static const bool kHasAsciiEncoding = true;
6241
Steve Blocka7e24c12009-10-30 11:49:00 +00006242 // Dispatched behavior.
6243 inline uint16_t SeqAsciiStringGet(int index);
6244 inline void SeqAsciiStringSet(int index, uint16_t value);
6245
6246 // Get the address of the characters in this string.
6247 inline Address GetCharsAddress();
6248
6249 inline char* GetChars();
6250
6251 // Casting
6252 static inline SeqAsciiString* cast(Object* obj);
6253
6254 // Garbage collection support. This method is called by the
6255 // garbage collector to compute the actual size of an AsciiString
6256 // instance.
6257 inline int SeqAsciiStringSize(InstanceType instance_type);
6258
6259 // Computes the size for an AsciiString instance of a given length.
6260 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006261 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00006262 }
6263
6264 // Layout description.
6265 static const int kHeaderSize = String::kSize;
6266 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
6267
Leon Clarkee46be812010-01-19 14:06:41 +00006268 // Maximal memory usage for a single sequential ASCII string.
6269 static const int kMaxSize = 512 * MB;
6270 // Maximal length of a single sequential ASCII string.
6271 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6272 static const int kMaxLength = (kMaxSize - kHeaderSize);
6273
Steve Blocka7e24c12009-10-30 11:49:00 +00006274 // Support for StringInputBuffer.
6275 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6276 unsigned* offset,
6277 unsigned chars);
6278 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
6279 unsigned* offset,
6280 unsigned chars);
6281
6282 private:
6283 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
6284};
6285
6286
6287// The TwoByteString class captures sequential unicode string objects.
6288// Each character in the TwoByteString is a two-byte uint16_t.
6289class SeqTwoByteString: public SeqString {
6290 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006291 static const bool kHasAsciiEncoding = false;
6292
Steve Blocka7e24c12009-10-30 11:49:00 +00006293 // Dispatched behavior.
6294 inline uint16_t SeqTwoByteStringGet(int index);
6295 inline void SeqTwoByteStringSet(int index, uint16_t value);
6296
6297 // Get the address of the characters in this string.
6298 inline Address GetCharsAddress();
6299
6300 inline uc16* GetChars();
6301
6302 // For regexp code.
6303 const uint16_t* SeqTwoByteStringGetData(unsigned start);
6304
6305 // Casting
6306 static inline SeqTwoByteString* cast(Object* obj);
6307
6308 // Garbage collection support. This method is called by the
6309 // garbage collector to compute the actual size of a TwoByteString
6310 // instance.
6311 inline int SeqTwoByteStringSize(InstanceType instance_type);
6312
6313 // Computes the size for a TwoByteString instance of a given length.
6314 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006315 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00006316 }
6317
6318 // Layout description.
6319 static const int kHeaderSize = String::kSize;
6320 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
6321
Leon Clarkee46be812010-01-19 14:06:41 +00006322 // Maximal memory usage for a single sequential two-byte string.
6323 static const int kMaxSize = 512 * MB;
6324 // Maximal length of a single sequential two-byte string.
6325 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6326 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
6327
Steve Blocka7e24c12009-10-30 11:49:00 +00006328 // Support for StringInputBuffer.
6329 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6330 unsigned* offset_ptr,
6331 unsigned chars);
6332
6333 private:
6334 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
6335};
6336
6337
6338// The ConsString class describes string values built by using the
6339// addition operator on strings. A ConsString is a pair where the
6340// first and second components are pointers to other string values.
6341// One or both components of a ConsString can be pointers to other
6342// ConsStrings, creating a binary tree of ConsStrings where the leaves
6343// are non-ConsString string values. The string value represented by
6344// a ConsString can be obtained by concatenating the leaf string
6345// values in a left-to-right depth-first traversal of the tree.
6346class ConsString: public String {
6347 public:
6348 // First string of the cons cell.
6349 inline String* first();
6350 // Doesn't check that the result is a string, even in debug mode. This is
6351 // useful during GC where the mark bits confuse the checks.
6352 inline Object* unchecked_first();
6353 inline void set_first(String* first,
6354 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
6355
6356 // Second string of the cons cell.
6357 inline String* second();
6358 // Doesn't check that the result is a string, even in debug mode. This is
6359 // useful during GC where the mark bits confuse the checks.
6360 inline Object* unchecked_second();
6361 inline void set_second(String* second,
6362 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
6363
6364 // Dispatched behavior.
6365 uint16_t ConsStringGet(int index);
6366
6367 // Casting.
6368 static inline ConsString* cast(Object* obj);
6369
Steve Blocka7e24c12009-10-30 11:49:00 +00006370 // Layout description.
6371 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
6372 static const int kSecondOffset = kFirstOffset + kPointerSize;
6373 static const int kSize = kSecondOffset + kPointerSize;
6374
6375 // Support for StringInputBuffer.
6376 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
6377 unsigned* offset_ptr,
6378 unsigned chars);
6379 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6380 unsigned* offset_ptr,
6381 unsigned chars);
6382
6383 // Minimum length for a cons string.
6384 static const int kMinLength = 13;
6385
Iain Merrick75681382010-08-19 15:07:18 +01006386 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
6387 BodyDescriptor;
6388
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006389#ifdef DEBUG
6390 void ConsStringVerify();
6391#endif
6392
Steve Blocka7e24c12009-10-30 11:49:00 +00006393 private:
6394 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
6395};
6396
6397
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006398// The Sliced String class describes strings that are substrings of another
6399// sequential string. The motivation is to save time and memory when creating
6400// a substring. A Sliced String is described as a pointer to the parent,
6401// the offset from the start of the parent string and the length. Using
6402// a Sliced String therefore requires unpacking of the parent string and
6403// adding the offset to the start address. A substring of a Sliced String
6404// are not nested since the double indirection is simplified when creating
6405// such a substring.
6406// Currently missing features are:
6407// - handling externalized parent strings
6408// - external strings as parent
6409// - truncating sliced string to enable otherwise unneeded parent to be GC'ed.
6410class SlicedString: public String {
6411 public:
6412
6413 inline String* parent();
6414 inline void set_parent(String* parent);
6415 inline int offset();
6416 inline void set_offset(int offset);
6417
6418 // Dispatched behavior.
6419 uint16_t SlicedStringGet(int index);
6420
6421 // Casting.
6422 static inline SlicedString* cast(Object* obj);
6423
6424 // Layout description.
6425 static const int kParentOffset = POINTER_SIZE_ALIGN(String::kSize);
6426 static const int kOffsetOffset = kParentOffset + kPointerSize;
6427 static const int kSize = kOffsetOffset + kPointerSize;
6428
6429 // Support for StringInputBuffer
6430 inline const unibrow::byte* SlicedStringReadBlock(ReadBlockBuffer* buffer,
6431 unsigned* offset_ptr,
6432 unsigned chars);
6433 inline void SlicedStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6434 unsigned* offset_ptr,
6435 unsigned chars);
6436 // Minimum length for a sliced string.
6437 static const int kMinLength = 13;
6438
6439 typedef FixedBodyDescriptor<kParentOffset,
6440 kOffsetOffset + kPointerSize, kSize>
6441 BodyDescriptor;
6442
6443#ifdef DEBUG
6444 void SlicedStringVerify();
6445#endif
6446
6447 private:
6448 DISALLOW_IMPLICIT_CONSTRUCTORS(SlicedString);
6449};
6450
6451
Steve Blocka7e24c12009-10-30 11:49:00 +00006452// The ExternalString class describes string values that are backed by
6453// a string resource that lies outside the V8 heap. ExternalStrings
6454// consist of the length field common to all strings, a pointer to the
6455// external resource. It is important to ensure (externally) that the
6456// resource is not deallocated while the ExternalString is live in the
6457// V8 heap.
6458//
6459// The API expects that all ExternalStrings are created through the
6460// API. Therefore, ExternalStrings should not be used internally.
6461class ExternalString: public String {
6462 public:
6463 // Casting
6464 static inline ExternalString* cast(Object* obj);
6465
6466 // Layout description.
6467 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
6468 static const int kSize = kResourceOffset + kPointerSize;
6469
6470 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
6471
6472 private:
6473 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
6474};
6475
6476
6477// The ExternalAsciiString class is an external string backed by an
6478// ASCII string.
6479class ExternalAsciiString: public ExternalString {
6480 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006481 static const bool kHasAsciiEncoding = true;
6482
Steve Blocka7e24c12009-10-30 11:49:00 +00006483 typedef v8::String::ExternalAsciiStringResource Resource;
6484
6485 // The underlying resource.
6486 inline Resource* resource();
6487 inline void set_resource(Resource* buffer);
6488
6489 // Dispatched behavior.
6490 uint16_t ExternalAsciiStringGet(int index);
6491
6492 // Casting.
6493 static inline ExternalAsciiString* cast(Object* obj);
6494
Steve Blockd0582a62009-12-15 09:54:21 +00006495 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01006496 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
6497
6498 template<typename StaticVisitor>
6499 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00006500
Steve Blocka7e24c12009-10-30 11:49:00 +00006501 // Support for StringInputBuffer.
6502 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
6503 unsigned* offset,
6504 unsigned chars);
6505 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6506 unsigned* offset,
6507 unsigned chars);
6508
Steve Blocka7e24c12009-10-30 11:49:00 +00006509 private:
6510 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
6511};
6512
6513
6514// The ExternalTwoByteString class is an external string backed by a UTF-16
6515// encoded string.
6516class ExternalTwoByteString: public ExternalString {
6517 public:
Leon Clarkeac952652010-07-15 11:15:24 +01006518 static const bool kHasAsciiEncoding = false;
6519
Steve Blocka7e24c12009-10-30 11:49:00 +00006520 typedef v8::String::ExternalStringResource Resource;
6521
6522 // The underlying string resource.
6523 inline Resource* resource();
6524 inline void set_resource(Resource* buffer);
6525
6526 // Dispatched behavior.
6527 uint16_t ExternalTwoByteStringGet(int index);
6528
6529 // For regexp code.
6530 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
6531
6532 // Casting.
6533 static inline ExternalTwoByteString* cast(Object* obj);
6534
Steve Blockd0582a62009-12-15 09:54:21 +00006535 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01006536 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
6537
6538 template<typename StaticVisitor>
6539 inline void ExternalTwoByteStringIterateBody();
6540
Steve Blockd0582a62009-12-15 09:54:21 +00006541
Steve Blocka7e24c12009-10-30 11:49:00 +00006542 // Support for StringInputBuffer.
6543 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6544 unsigned* offset_ptr,
6545 unsigned chars);
6546
Steve Blocka7e24c12009-10-30 11:49:00 +00006547 private:
6548 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
6549};
6550
6551
6552// Utility superclass for stack-allocated objects that must be updated
6553// on gc. It provides two ways for the gc to update instances, either
6554// iterating or updating after gc.
6555class Relocatable BASE_EMBEDDED {
6556 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006557 explicit inline Relocatable(Isolate* isolate);
6558 inline virtual ~Relocatable();
Steve Blocka7e24c12009-10-30 11:49:00 +00006559 virtual void IterateInstance(ObjectVisitor* v) { }
6560 virtual void PostGarbageCollection() { }
6561
6562 static void PostGarbageCollectionProcessing();
6563 static int ArchiveSpacePerThread();
Ben Murdoch257744e2011-11-30 15:57:28 +00006564 static char* ArchiveState(Isolate* isolate, char* to);
6565 static char* RestoreState(Isolate* isolate, char* from);
Steve Blocka7e24c12009-10-30 11:49:00 +00006566 static void Iterate(ObjectVisitor* v);
6567 static void Iterate(ObjectVisitor* v, Relocatable* top);
6568 static char* Iterate(ObjectVisitor* v, char* t);
6569 private:
Steve Block44f0eee2011-05-26 01:26:41 +01006570 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00006571 Relocatable* prev_;
6572};
6573
6574
6575// A flat string reader provides random access to the contents of a
6576// string independent of the character width of the string. The handle
6577// must be valid as long as the reader is being used.
6578class FlatStringReader : public Relocatable {
6579 public:
Steve Block44f0eee2011-05-26 01:26:41 +01006580 FlatStringReader(Isolate* isolate, Handle<String> str);
6581 FlatStringReader(Isolate* isolate, Vector<const char> input);
Steve Blocka7e24c12009-10-30 11:49:00 +00006582 void PostGarbageCollection();
6583 inline uc32 Get(int index);
6584 int length() { return length_; }
6585 private:
6586 String** str_;
6587 bool is_ascii_;
6588 int length_;
6589 const void* start_;
6590};
6591
6592
6593// Note that StringInputBuffers are not valid across a GC! To fix this
6594// it would have to store a String Handle instead of a String* and
6595// AsciiStringReadBlock would have to be modified to use memcpy.
6596//
6597// StringInputBuffer is able to traverse any string regardless of how
6598// deeply nested a sequence of ConsStrings it is made of. However,
6599// performance will be better if deep strings are flattened before they
6600// are traversed. Since flattening requires memory allocation this is
6601// not always desirable, however (esp. in debugging situations).
6602class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
6603 public:
6604 virtual void Seek(unsigned pos);
6605 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006606 explicit inline StringInputBuffer(String* backing):
Steve Blocka7e24c12009-10-30 11:49:00 +00006607 unibrow::InputBuffer<String, String*, 1024>(backing) {}
6608};
6609
6610
6611class SafeStringInputBuffer
6612 : public unibrow::InputBuffer<String, String**, 256> {
6613 public:
6614 virtual void Seek(unsigned pos);
6615 inline SafeStringInputBuffer()
6616 : unibrow::InputBuffer<String, String**, 256>() {}
Ben Murdoch8b112d22011-06-08 16:22:53 +01006617 explicit inline SafeStringInputBuffer(String** backing)
Steve Blocka7e24c12009-10-30 11:49:00 +00006618 : unibrow::InputBuffer<String, String**, 256>(backing) {}
6619};
6620
6621
6622template <typename T>
6623class VectorIterator {
6624 public:
6625 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
6626 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
6627 T GetNext() { return data_[index_++]; }
6628 bool has_more() { return index_ < data_.length(); }
6629 private:
6630 Vector<const T> data_;
6631 int index_;
6632};
6633
6634
6635// The Oddball describes objects null, undefined, true, and false.
6636class Oddball: public HeapObject {
6637 public:
6638 // [to_string]: Cached to_string computed at startup.
6639 DECL_ACCESSORS(to_string, String)
6640
6641 // [to_number]: Cached to_number computed at startup.
6642 DECL_ACCESSORS(to_number, Object)
6643
Steve Block44f0eee2011-05-26 01:26:41 +01006644 inline byte kind();
6645 inline void set_kind(byte kind);
6646
Steve Blocka7e24c12009-10-30 11:49:00 +00006647 // Casting.
6648 static inline Oddball* cast(Object* obj);
6649
6650 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00006651#ifdef DEBUG
6652 void OddballVerify();
6653#endif
6654
6655 // Initialize the fields.
John Reck59135872010-11-02 12:39:01 -07006656 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
Steve Block44f0eee2011-05-26 01:26:41 +01006657 Object* to_number,
6658 byte kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00006659
6660 // Layout description.
6661 static const int kToStringOffset = HeapObject::kHeaderSize;
6662 static const int kToNumberOffset = kToStringOffset + kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +01006663 static const int kKindOffset = kToNumberOffset + kPointerSize;
6664 static const int kSize = kKindOffset + kPointerSize;
6665
6666 static const byte kFalse = 0;
6667 static const byte kTrue = 1;
6668 static const byte kNotBooleanMask = ~1;
6669 static const byte kTheHole = 2;
6670 static const byte kNull = 3;
6671 static const byte kArgumentMarker = 4;
6672 static const byte kUndefined = 5;
6673 static const byte kOther = 6;
Steve Blocka7e24c12009-10-30 11:49:00 +00006674
Iain Merrick75681382010-08-19 15:07:18 +01006675 typedef FixedBodyDescriptor<kToStringOffset,
6676 kToNumberOffset + kPointerSize,
6677 kSize> BodyDescriptor;
6678
Steve Blocka7e24c12009-10-30 11:49:00 +00006679 private:
6680 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6681};
6682
6683
6684class JSGlobalPropertyCell: public HeapObject {
6685 public:
6686 // [value]: value of the global property.
6687 DECL_ACCESSORS(value, Object)
6688
6689 // Casting.
6690 static inline JSGlobalPropertyCell* cast(Object* obj);
6691
Steve Blocka7e24c12009-10-30 11:49:00 +00006692#ifdef DEBUG
6693 void JSGlobalPropertyCellVerify();
Ben Murdochb0fe1622011-05-05 13:52:32 +01006694#endif
6695#ifdef OBJECT_PRINT
6696 inline void JSGlobalPropertyCellPrint() {
6697 JSGlobalPropertyCellPrint(stdout);
6698 }
6699 void JSGlobalPropertyCellPrint(FILE* out);
Steve Blocka7e24c12009-10-30 11:49:00 +00006700#endif
6701
6702 // Layout description.
6703 static const int kValueOffset = HeapObject::kHeaderSize;
6704 static const int kSize = kValueOffset + kPointerSize;
6705
Iain Merrick75681382010-08-19 15:07:18 +01006706 typedef FixedBodyDescriptor<kValueOffset,
6707 kValueOffset + kPointerSize,
6708 kSize> BodyDescriptor;
6709
Ben Murdoch8b112d22011-06-08 16:22:53 +01006710 // Returns the isolate/heap this cell object belongs to.
6711 inline Isolate* isolate();
6712 inline Heap* heap();
6713
Steve Blocka7e24c12009-10-30 11:49:00 +00006714 private:
6715 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6716};
6717
6718
Ben Murdoch257744e2011-11-30 15:57:28 +00006719// The JSProxy describes EcmaScript Harmony proxies
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006720class JSProxy: public JSReceiver {
Steve Blocka7e24c12009-10-30 11:49:00 +00006721 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00006722 // [handler]: The handler property.
6723 DECL_ACCESSORS(handler, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00006724
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006725 // [padding]: The padding slot (unused, see below).
6726 DECL_ACCESSORS(padding, Object)
6727
Steve Blocka7e24c12009-10-30 11:49:00 +00006728 // Casting.
Ben Murdoch257744e2011-11-30 15:57:28 +00006729 static inline JSProxy* cast(Object* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +00006730
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006731 bool HasPropertyWithHandler(String* name);
6732
6733 MUST_USE_RESULT MaybeObject* SetPropertyWithHandler(
6734 String* name,
6735 Object* value,
6736 PropertyAttributes attributes,
6737 StrictModeFlag strict_mode);
6738
6739 MUST_USE_RESULT MaybeObject* DeletePropertyWithHandler(
6740 String* name,
6741 DeleteMode mode);
6742
6743 MUST_USE_RESULT PropertyAttributes GetPropertyAttributeWithHandler(
6744 JSReceiver* receiver,
6745 String* name,
6746 bool* has_exception);
6747
6748 // Turn this into an (empty) JSObject.
6749 void Fix();
6750
Steve Blocka7e24c12009-10-30 11:49:00 +00006751 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006752#ifdef OBJECT_PRINT
Ben Murdoch257744e2011-11-30 15:57:28 +00006753 inline void JSProxyPrint() {
6754 JSProxyPrint(stdout);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006755 }
Ben Murdoch257744e2011-11-30 15:57:28 +00006756 void JSProxyPrint(FILE* out);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006757#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006758#ifdef DEBUG
Ben Murdoch257744e2011-11-30 15:57:28 +00006759 void JSProxyVerify();
6760#endif
6761
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006762 // Layout description. We add padding so that a proxy has the same
6763 // size as a virgin JSObject. This is essential for becoming a JSObject
6764 // upon freeze.
Ben Murdoch257744e2011-11-30 15:57:28 +00006765 static const int kHandlerOffset = HeapObject::kHeaderSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006766 static const int kPaddingOffset = kHandlerOffset + kPointerSize;
6767 static const int kSize = kPaddingOffset + kPointerSize;
6768
6769 STATIC_CHECK(kSize == JSObject::kHeaderSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00006770
6771 typedef FixedBodyDescriptor<kHandlerOffset,
6772 kHandlerOffset + kPointerSize,
6773 kSize> BodyDescriptor;
6774
6775 private:
6776 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxy);
6777};
6778
6779
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006780// TODO(rossberg): Only a stub for now.
6781class JSFunctionProxy: public JSProxy {
6782 public:
6783 // Casting.
6784 static inline JSFunctionProxy* cast(Object* obj);
6785
6786 private:
6787 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunctionProxy);
6788};
6789
Ben Murdoch257744e2011-11-30 15:57:28 +00006790
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006791// The JSWeakMap describes EcmaScript Harmony weak maps
6792class JSWeakMap: public JSObject {
6793 public:
6794 // [table]: the backing hash table mapping keys to values.
6795 DECL_ACCESSORS(table, ObjectHashTable)
6796
6797 // [next]: linked list of encountered weak maps during GC.
6798 DECL_ACCESSORS(next, Object)
6799
6800 // Unchecked accessors to be used during GC.
6801 inline ObjectHashTable* unchecked_table();
6802
6803 // Casting.
6804 static inline JSWeakMap* cast(Object* obj);
6805
6806#ifdef OBJECT_PRINT
6807 inline void JSWeakMapPrint() {
6808 JSWeakMapPrint(stdout);
6809 }
6810 void JSWeakMapPrint(FILE* out);
6811#endif
6812#ifdef DEBUG
6813 void JSWeakMapVerify();
6814#endif
6815
6816 static const int kTableOffset = JSObject::kHeaderSize;
6817 static const int kNextOffset = kTableOffset + kPointerSize;
6818 static const int kSize = kNextOffset + kPointerSize;
6819
6820 private:
6821 DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakMap);
6822};
6823
6824
Ben Murdoch257744e2011-11-30 15:57:28 +00006825// Foreign describes objects pointing from JavaScript to C structures.
6826// Since they cannot contain references to JS HeapObjects they can be
6827// placed in old_data_space.
6828class Foreign: public HeapObject {
6829 public:
6830 // [address]: field containing the address.
6831 inline Address address();
6832 inline void set_address(Address value);
6833
6834 // Casting.
6835 static inline Foreign* cast(Object* obj);
6836
6837 // Dispatched behavior.
6838 inline void ForeignIterateBody(ObjectVisitor* v);
6839
6840 template<typename StaticVisitor>
6841 inline void ForeignIterateBody();
6842
6843#ifdef OBJECT_PRINT
6844 inline void ForeignPrint() {
6845 ForeignPrint(stdout);
6846 }
6847 void ForeignPrint(FILE* out);
6848#endif
6849#ifdef DEBUG
6850 void ForeignVerify();
Steve Blocka7e24c12009-10-30 11:49:00 +00006851#endif
6852
6853 // Layout description.
6854
Ben Murdoch257744e2011-11-30 15:57:28 +00006855 static const int kAddressOffset = HeapObject::kHeaderSize;
6856 static const int kSize = kAddressOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006857
Ben Murdoch257744e2011-11-30 15:57:28 +00006858 STATIC_CHECK(kAddressOffset == Internals::kForeignAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00006859
6860 private:
Ben Murdoch257744e2011-11-30 15:57:28 +00006861 DISALLOW_IMPLICIT_CONSTRUCTORS(Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +00006862};
6863
6864
6865// The JSArray describes JavaScript Arrays
6866// Such an array can be in one of two modes:
6867// - fast, backing storage is a FixedArray and length <= elements.length();
6868// Please note: push and pop can be used to grow and shrink the array.
6869// - slow, backing storage is a HashTable with numbers as keys.
6870class JSArray: public JSObject {
6871 public:
6872 // [length]: The length property.
6873 DECL_ACCESSORS(length, Object)
6874
Leon Clarke4515c472010-02-03 11:58:03 +00006875 // Overload the length setter to skip write barrier when the length
6876 // is set to a smi. This matches the set function on FixedArray.
6877 inline void set_length(Smi* length);
6878
John Reck59135872010-11-02 12:39:01 -07006879 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6880 Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006881
6882 // Initialize the array with the given capacity. The function may
6883 // fail due to out-of-memory situations, but only if the requested
6884 // capacity is non-zero.
John Reck59135872010-11-02 12:39:01 -07006885 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00006886
6887 // Set the content of the array to the content of storage.
6888 inline void SetContent(FixedArray* storage);
6889
6890 // Casting.
6891 static inline JSArray* cast(Object* obj);
6892
6893 // Uses handles. Ensures that the fixed array backing the JSArray has at
6894 // least the stated size.
6895 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
6896
6897 // Dispatched behavior.
Ben Murdochb0fe1622011-05-05 13:52:32 +01006898#ifdef OBJECT_PRINT
6899 inline void JSArrayPrint() {
6900 JSArrayPrint(stdout);
6901 }
6902 void JSArrayPrint(FILE* out);
6903#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006904#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006905 void JSArrayVerify();
6906#endif
6907
6908 // Number of element slots to pre-allocate for an empty array.
6909 static const int kPreallocatedArrayElements = 4;
6910
6911 // Layout description.
6912 static const int kLengthOffset = JSObject::kHeaderSize;
6913 static const int kSize = kLengthOffset + kPointerSize;
6914
6915 private:
6916 // Expand the fixed array backing of a fast-case JSArray to at least
6917 // the requested size.
6918 void Expand(int minimum_size_of_backing_fixed_array);
6919
6920 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6921};
6922
6923
Steve Block6ded16b2010-05-10 14:33:55 +01006924// JSRegExpResult is just a JSArray with a specific initial map.
6925// This initial map adds in-object properties for "index" and "input"
6926// properties, as assigned by RegExp.prototype.exec, which allows
6927// faster creation of RegExp exec results.
6928// This class just holds constants used when creating the result.
6929// After creation the result must be treated as a JSArray in all regards.
6930class JSRegExpResult: public JSArray {
6931 public:
6932 // Offsets of object fields.
6933 static const int kIndexOffset = JSArray::kSize;
6934 static const int kInputOffset = kIndexOffset + kPointerSize;
6935 static const int kSize = kInputOffset + kPointerSize;
6936 // Indices of in-object properties.
6937 static const int kIndexIndex = 0;
6938 static const int kInputIndex = 1;
6939 private:
6940 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6941};
6942
6943
Steve Blocka7e24c12009-10-30 11:49:00 +00006944// An accessor must have a getter, but can have no setter.
6945//
6946// When setting a property, V8 searches accessors in prototypes.
6947// If an accessor was found and it does not have a setter,
6948// the request is ignored.
6949//
6950// If the accessor in the prototype has the READ_ONLY property attribute, then
6951// a new value is added to the local object when the property is set.
6952// This shadows the accessor in the prototype.
6953class AccessorInfo: public Struct {
6954 public:
6955 DECL_ACCESSORS(getter, Object)
6956 DECL_ACCESSORS(setter, Object)
6957 DECL_ACCESSORS(data, Object)
6958 DECL_ACCESSORS(name, Object)
6959 DECL_ACCESSORS(flag, Smi)
6960
6961 inline bool all_can_read();
6962 inline void set_all_can_read(bool value);
6963
6964 inline bool all_can_write();
6965 inline void set_all_can_write(bool value);
6966
6967 inline bool prohibits_overwriting();
6968 inline void set_prohibits_overwriting(bool value);
6969
6970 inline PropertyAttributes property_attributes();
6971 inline void set_property_attributes(PropertyAttributes attributes);
6972
6973 static inline AccessorInfo* cast(Object* obj);
6974
Ben Murdochb0fe1622011-05-05 13:52:32 +01006975#ifdef OBJECT_PRINT
6976 inline void AccessorInfoPrint() {
6977 AccessorInfoPrint(stdout);
6978 }
6979 void AccessorInfoPrint(FILE* out);
6980#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00006981#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00006982 void AccessorInfoVerify();
6983#endif
6984
6985 static const int kGetterOffset = HeapObject::kHeaderSize;
6986 static const int kSetterOffset = kGetterOffset + kPointerSize;
6987 static const int kDataOffset = kSetterOffset + kPointerSize;
6988 static const int kNameOffset = kDataOffset + kPointerSize;
6989 static const int kFlagOffset = kNameOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08006990 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006991
6992 private:
6993 // Bit positions in flag.
6994 static const int kAllCanReadBit = 0;
6995 static const int kAllCanWriteBit = 1;
6996 static const int kProhibitsOverwritingBit = 2;
6997 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
6998
6999 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
7000};
7001
7002
7003class AccessCheckInfo: public Struct {
7004 public:
7005 DECL_ACCESSORS(named_callback, Object)
7006 DECL_ACCESSORS(indexed_callback, Object)
7007 DECL_ACCESSORS(data, Object)
7008
7009 static inline AccessCheckInfo* cast(Object* obj);
7010
Ben Murdochb0fe1622011-05-05 13:52:32 +01007011#ifdef OBJECT_PRINT
7012 inline void AccessCheckInfoPrint() {
7013 AccessCheckInfoPrint(stdout);
7014 }
7015 void AccessCheckInfoPrint(FILE* out);
7016#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007017#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007018 void AccessCheckInfoVerify();
7019#endif
7020
7021 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
7022 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
7023 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
7024 static const int kSize = kDataOffset + kPointerSize;
7025
7026 private:
7027 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
7028};
7029
7030
7031class InterceptorInfo: public Struct {
7032 public:
7033 DECL_ACCESSORS(getter, Object)
7034 DECL_ACCESSORS(setter, Object)
7035 DECL_ACCESSORS(query, Object)
7036 DECL_ACCESSORS(deleter, Object)
7037 DECL_ACCESSORS(enumerator, Object)
7038 DECL_ACCESSORS(data, Object)
7039
7040 static inline InterceptorInfo* cast(Object* obj);
7041
Ben Murdochb0fe1622011-05-05 13:52:32 +01007042#ifdef OBJECT_PRINT
7043 inline void InterceptorInfoPrint() {
7044 InterceptorInfoPrint(stdout);
7045 }
7046 void InterceptorInfoPrint(FILE* out);
7047#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007048#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007049 void InterceptorInfoVerify();
7050#endif
7051
7052 static const int kGetterOffset = HeapObject::kHeaderSize;
7053 static const int kSetterOffset = kGetterOffset + kPointerSize;
7054 static const int kQueryOffset = kSetterOffset + kPointerSize;
7055 static const int kDeleterOffset = kQueryOffset + kPointerSize;
7056 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
7057 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
7058 static const int kSize = kDataOffset + kPointerSize;
7059
7060 private:
7061 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
7062};
7063
7064
7065class CallHandlerInfo: public Struct {
7066 public:
7067 DECL_ACCESSORS(callback, Object)
7068 DECL_ACCESSORS(data, Object)
7069
7070 static inline CallHandlerInfo* cast(Object* obj);
7071
Ben Murdochb0fe1622011-05-05 13:52:32 +01007072#ifdef OBJECT_PRINT
7073 inline void CallHandlerInfoPrint() {
7074 CallHandlerInfoPrint(stdout);
7075 }
7076 void CallHandlerInfoPrint(FILE* out);
7077#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007078#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007079 void CallHandlerInfoVerify();
7080#endif
7081
7082 static const int kCallbackOffset = HeapObject::kHeaderSize;
7083 static const int kDataOffset = kCallbackOffset + kPointerSize;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08007084 static const int kSize = kDataOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007085
7086 private:
7087 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
7088};
7089
7090
7091class TemplateInfo: public Struct {
7092 public:
7093 DECL_ACCESSORS(tag, Object)
7094 DECL_ACCESSORS(property_list, Object)
7095
7096#ifdef DEBUG
7097 void TemplateInfoVerify();
7098#endif
7099
7100 static const int kTagOffset = HeapObject::kHeaderSize;
7101 static const int kPropertyListOffset = kTagOffset + kPointerSize;
7102 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
7103 protected:
7104 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
7105 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
7106};
7107
7108
7109class FunctionTemplateInfo: public TemplateInfo {
7110 public:
7111 DECL_ACCESSORS(serial_number, Object)
7112 DECL_ACCESSORS(call_code, Object)
7113 DECL_ACCESSORS(property_accessors, Object)
7114 DECL_ACCESSORS(prototype_template, Object)
7115 DECL_ACCESSORS(parent_template, Object)
7116 DECL_ACCESSORS(named_property_handler, Object)
7117 DECL_ACCESSORS(indexed_property_handler, Object)
7118 DECL_ACCESSORS(instance_template, Object)
7119 DECL_ACCESSORS(class_name, Object)
7120 DECL_ACCESSORS(signature, Object)
7121 DECL_ACCESSORS(instance_call_handler, Object)
7122 DECL_ACCESSORS(access_check_info, Object)
7123 DECL_ACCESSORS(flag, Smi)
7124
7125 // Following properties use flag bits.
7126 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
7127 DECL_BOOLEAN_ACCESSORS(undetectable)
7128 // If the bit is set, object instances created by this function
7129 // requires access check.
7130 DECL_BOOLEAN_ACCESSORS(needs_access_check)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007131 DECL_BOOLEAN_ACCESSORS(read_only_prototype)
Steve Blocka7e24c12009-10-30 11:49:00 +00007132
7133 static inline FunctionTemplateInfo* cast(Object* obj);
7134
Ben Murdochb0fe1622011-05-05 13:52:32 +01007135#ifdef OBJECT_PRINT
7136 inline void FunctionTemplateInfoPrint() {
7137 FunctionTemplateInfoPrint(stdout);
7138 }
7139 void FunctionTemplateInfoPrint(FILE* out);
7140#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007141#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007142 void FunctionTemplateInfoVerify();
7143#endif
7144
7145 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
7146 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
7147 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
7148 static const int kPrototypeTemplateOffset =
7149 kPropertyAccessorsOffset + kPointerSize;
7150 static const int kParentTemplateOffset =
7151 kPrototypeTemplateOffset + kPointerSize;
7152 static const int kNamedPropertyHandlerOffset =
7153 kParentTemplateOffset + kPointerSize;
7154 static const int kIndexedPropertyHandlerOffset =
7155 kNamedPropertyHandlerOffset + kPointerSize;
7156 static const int kInstanceTemplateOffset =
7157 kIndexedPropertyHandlerOffset + kPointerSize;
7158 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
7159 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
7160 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
7161 static const int kAccessCheckInfoOffset =
7162 kInstanceCallHandlerOffset + kPointerSize;
7163 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007164 static const int kSize = kFlagOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007165
7166 private:
7167 // Bit position in the flag, from least significant bit position.
7168 static const int kHiddenPrototypeBit = 0;
7169 static const int kUndetectableBit = 1;
7170 static const int kNeedsAccessCheckBit = 2;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00007171 static const int kReadOnlyPrototypeBit = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00007172
7173 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
7174};
7175
7176
7177class ObjectTemplateInfo: public TemplateInfo {
7178 public:
7179 DECL_ACCESSORS(constructor, Object)
7180 DECL_ACCESSORS(internal_field_count, Object)
7181
7182 static inline ObjectTemplateInfo* cast(Object* obj);
7183
Ben Murdochb0fe1622011-05-05 13:52:32 +01007184#ifdef OBJECT_PRINT
7185 inline void ObjectTemplateInfoPrint() {
7186 ObjectTemplateInfoPrint(stdout);
7187 }
7188 void ObjectTemplateInfoPrint(FILE* out);
7189#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007190#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007191 void ObjectTemplateInfoVerify();
7192#endif
7193
7194 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
7195 static const int kInternalFieldCountOffset =
7196 kConstructorOffset + kPointerSize;
7197 static const int kSize = kInternalFieldCountOffset + kPointerSize;
7198};
7199
7200
7201class SignatureInfo: public Struct {
7202 public:
7203 DECL_ACCESSORS(receiver, Object)
7204 DECL_ACCESSORS(args, Object)
7205
7206 static inline SignatureInfo* cast(Object* obj);
7207
Ben Murdochb0fe1622011-05-05 13:52:32 +01007208#ifdef OBJECT_PRINT
7209 inline void SignatureInfoPrint() {
7210 SignatureInfoPrint(stdout);
7211 }
7212 void SignatureInfoPrint(FILE* out);
7213#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007214#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007215 void SignatureInfoVerify();
7216#endif
7217
7218 static const int kReceiverOffset = Struct::kHeaderSize;
7219 static const int kArgsOffset = kReceiverOffset + kPointerSize;
7220 static const int kSize = kArgsOffset + kPointerSize;
7221
7222 private:
7223 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
7224};
7225
7226
7227class TypeSwitchInfo: public Struct {
7228 public:
7229 DECL_ACCESSORS(types, Object)
7230
7231 static inline TypeSwitchInfo* cast(Object* obj);
7232
Ben Murdochb0fe1622011-05-05 13:52:32 +01007233#ifdef OBJECT_PRINT
7234 inline void TypeSwitchInfoPrint() {
7235 TypeSwitchInfoPrint(stdout);
7236 }
7237 void TypeSwitchInfoPrint(FILE* out);
7238#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007239#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007240 void TypeSwitchInfoVerify();
7241#endif
7242
7243 static const int kTypesOffset = Struct::kHeaderSize;
7244 static const int kSize = kTypesOffset + kPointerSize;
7245};
7246
7247
7248#ifdef ENABLE_DEBUGGER_SUPPORT
7249// The DebugInfo class holds additional information for a function being
7250// debugged.
7251class DebugInfo: public Struct {
7252 public:
7253 // The shared function info for the source being debugged.
7254 DECL_ACCESSORS(shared, SharedFunctionInfo)
7255 // Code object for the original code.
7256 DECL_ACCESSORS(original_code, Code)
7257 // Code object for the patched code. This code object is the code object
7258 // currently active for the function.
7259 DECL_ACCESSORS(code, Code)
7260 // Fixed array holding status information for each active break point.
7261 DECL_ACCESSORS(break_points, FixedArray)
7262
7263 // Check if there is a break point at a code position.
7264 bool HasBreakPoint(int code_position);
7265 // Get the break point info object for a code position.
7266 Object* GetBreakPointInfo(int code_position);
7267 // Clear a break point.
7268 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
7269 int code_position,
7270 Handle<Object> break_point_object);
7271 // Set a break point.
7272 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
7273 int source_position, int statement_position,
7274 Handle<Object> break_point_object);
7275 // Get the break point objects for a code position.
7276 Object* GetBreakPointObjects(int code_position);
7277 // Find the break point info holding this break point object.
7278 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
7279 Handle<Object> break_point_object);
7280 // Get the number of break points for this function.
7281 int GetBreakPointCount();
7282
7283 static inline DebugInfo* cast(Object* obj);
7284
Ben Murdochb0fe1622011-05-05 13:52:32 +01007285#ifdef OBJECT_PRINT
7286 inline void DebugInfoPrint() {
7287 DebugInfoPrint(stdout);
7288 }
7289 void DebugInfoPrint(FILE* out);
7290#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007291#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007292 void DebugInfoVerify();
7293#endif
7294
7295 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
7296 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
7297 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
7298 static const int kActiveBreakPointsCountIndex =
7299 kPatchedCodeIndex + kPointerSize;
7300 static const int kBreakPointsStateIndex =
7301 kActiveBreakPointsCountIndex + kPointerSize;
7302 static const int kSize = kBreakPointsStateIndex + kPointerSize;
7303
7304 private:
7305 static const int kNoBreakPointInfo = -1;
7306
7307 // Lookup the index in the break_points array for a code position.
7308 int GetBreakPointInfoIndex(int code_position);
7309
7310 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
7311};
7312
7313
7314// The BreakPointInfo class holds information for break points set in a
7315// function. The DebugInfo object holds a BreakPointInfo object for each code
7316// position with one or more break points.
7317class BreakPointInfo: public Struct {
7318 public:
7319 // The position in the code for the break point.
7320 DECL_ACCESSORS(code_position, Smi)
7321 // The position in the source for the break position.
7322 DECL_ACCESSORS(source_position, Smi)
7323 // The position in the source for the last statement before this break
7324 // position.
7325 DECL_ACCESSORS(statement_position, Smi)
7326 // List of related JavaScript break points.
7327 DECL_ACCESSORS(break_point_objects, Object)
7328
7329 // Removes a break point.
7330 static void ClearBreakPoint(Handle<BreakPointInfo> info,
7331 Handle<Object> break_point_object);
7332 // Set a break point.
7333 static void SetBreakPoint(Handle<BreakPointInfo> info,
7334 Handle<Object> break_point_object);
7335 // Check if break point info has this break point object.
7336 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
7337 Handle<Object> break_point_object);
7338 // Get the number of break points for this code position.
7339 int GetBreakPointCount();
7340
7341 static inline BreakPointInfo* cast(Object* obj);
7342
Ben Murdochb0fe1622011-05-05 13:52:32 +01007343#ifdef OBJECT_PRINT
7344 inline void BreakPointInfoPrint() {
7345 BreakPointInfoPrint(stdout);
7346 }
7347 void BreakPointInfoPrint(FILE* out);
7348#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00007349#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00007350 void BreakPointInfoVerify();
7351#endif
7352
7353 static const int kCodePositionIndex = Struct::kHeaderSize;
7354 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
7355 static const int kStatementPositionIndex =
7356 kSourcePositionIndex + kPointerSize;
7357 static const int kBreakPointObjectsIndex =
7358 kStatementPositionIndex + kPointerSize;
7359 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
7360
7361 private:
7362 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
7363};
7364#endif // ENABLE_DEBUGGER_SUPPORT
7365
7366
7367#undef DECL_BOOLEAN_ACCESSORS
7368#undef DECL_ACCESSORS
7369
7370
7371// Abstract base class for visiting, and optionally modifying, the
7372// pointers contained in Objects. Used in GC and serialization/deserialization.
7373class ObjectVisitor BASE_EMBEDDED {
7374 public:
7375 virtual ~ObjectVisitor() {}
7376
7377 // Visits a contiguous arrays of pointers in the half-open range
7378 // [start, end). Any or all of the values may be modified on return.
7379 virtual void VisitPointers(Object** start, Object** end) = 0;
7380
7381 // To allow lazy clearing of inline caches the visitor has
7382 // a rich interface for iterating over Code objects..
7383
7384 // Visits a code target in the instruction stream.
7385 virtual void VisitCodeTarget(RelocInfo* rinfo);
7386
Steve Block791712a2010-08-27 10:21:07 +01007387 // Visits a code entry in a JS function.
7388 virtual void VisitCodeEntry(Address entry_address);
7389
Ben Murdochb0fe1622011-05-05 13:52:32 +01007390 // Visits a global property cell reference in the instruction stream.
7391 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
7392
Steve Blocka7e24c12009-10-30 11:49:00 +00007393 // Visits a runtime entry in the instruction stream.
7394 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
7395
Steve Blockd0582a62009-12-15 09:54:21 +00007396 // Visits the resource of an ASCII or two-byte string.
7397 virtual void VisitExternalAsciiString(
7398 v8::String::ExternalAsciiStringResource** resource) {}
7399 virtual void VisitExternalTwoByteString(
7400 v8::String::ExternalStringResource** resource) {}
7401
Steve Blocka7e24c12009-10-30 11:49:00 +00007402 // Visits a debug call target in the instruction stream.
7403 virtual void VisitDebugTarget(RelocInfo* rinfo);
7404
7405 // Handy shorthand for visiting a single pointer.
7406 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
7407
7408 // Visits a contiguous arrays of external references (references to the C++
7409 // heap) in the half-open range [start, end). Any or all of the values
7410 // may be modified on return.
7411 virtual void VisitExternalReferences(Address* start, Address* end) {}
7412
7413 inline void VisitExternalReference(Address* p) {
7414 VisitExternalReferences(p, p + 1);
7415 }
7416
Steve Block44f0eee2011-05-26 01:26:41 +01007417 // Visits a handle that has an embedder-assigned class ID.
7418 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
7419
Steve Blocka7e24c12009-10-30 11:49:00 +00007420#ifdef DEBUG
7421 // Intended for serialization/deserialization checking: insert, or
7422 // check for the presence of, a tag at this position in the stream.
7423 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00007424#else
7425 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00007426#endif
7427};
7428
7429
Iain Merrick75681382010-08-19 15:07:18 +01007430class StructBodyDescriptor : public
7431 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
7432 public:
7433 static inline int SizeOf(Map* map, HeapObject* object) {
7434 return map->instance_size();
7435 }
7436};
7437
7438
Steve Blocka7e24c12009-10-30 11:49:00 +00007439// BooleanBit is a helper class for setting and getting a bit in an
7440// integer or Smi.
7441class BooleanBit : public AllStatic {
7442 public:
7443 static inline bool get(Smi* smi, int bit_position) {
7444 return get(smi->value(), bit_position);
7445 }
7446
7447 static inline bool get(int value, int bit_position) {
7448 return (value & (1 << bit_position)) != 0;
7449 }
7450
7451 static inline Smi* set(Smi* smi, int bit_position, bool v) {
7452 return Smi::FromInt(set(smi->value(), bit_position, v));
7453 }
7454
7455 static inline int set(int value, int bit_position, bool v) {
7456 if (v) {
7457 value |= (1 << bit_position);
7458 } else {
7459 value &= ~(1 << bit_position);
7460 }
7461 return value;
7462 }
7463};
7464
7465} } // namespace v8::internal
7466
7467#endif // V8_OBJECTS_H_