blob: f6411965a5aa3b6f9b3320bb749083661b3179a3 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// 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
31#include "builtins.h"
32#include "code-stubs.h"
33#include "smart-pointer.h"
34#include "unicode-inl.h"
Steve Block3ce2e202009-11-05 08:53:23 +000035#if V8_TARGET_ARCH_ARM
36#include "arm/constants-arm.h"
37#endif
Steve Blocka7e24c12009-10-30 11:49:00 +000038
39//
40// All object types in the V8 JavaScript are described in this file.
41//
42// Inheritance hierarchy:
43// - Object
44// - Smi (immediate small integer)
45// - Failure (immediate for marking failed operation)
46// - HeapObject (superclass for everything allocated in the heap)
47// - JSObject
48// - JSArray
49// - JSRegExp
50// - JSFunction
51// - GlobalObject
52// - JSGlobalObject
53// - JSBuiltinsObject
54// - JSGlobalProxy
55// - JSValue
56// - Array
57// - ByteArray
58// - PixelArray
Steve Block3ce2e202009-11-05 08:53:23 +000059// - ExternalArray
60// - ExternalByteArray
61// - ExternalUnsignedByteArray
62// - ExternalShortArray
63// - ExternalUnsignedShortArray
64// - ExternalIntArray
65// - ExternalUnsignedIntArray
66// - ExternalFloatArray
Steve Blocka7e24c12009-10-30 11:49:00 +000067// - FixedArray
68// - DescriptorArray
69// - HashTable
70// - Dictionary
71// - SymbolTable
72// - CompilationCacheTable
73// - MapCache
74// - Context
75// - GlobalContext
76// - String
77// - SeqString
78// - SeqAsciiString
79// - SeqTwoByteString
80// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000081// - ExternalString
82// - ExternalAsciiString
83// - ExternalTwoByteString
84// - HeapNumber
85// - Code
86// - Map
87// - Oddball
88// - Proxy
89// - SharedFunctionInfo
90// - Struct
91// - AccessorInfo
92// - AccessCheckInfo
93// - InterceptorInfo
94// - CallHandlerInfo
95// - TemplateInfo
96// - FunctionTemplateInfo
97// - ObjectTemplateInfo
98// - Script
99// - SignatureInfo
100// - TypeSwitchInfo
101// - DebugInfo
102// - BreakPointInfo
103//
104// Formats of Object*:
105// Smi: [31 bit signed int] 0
106// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
107// Failure: [30 bit signed int] 11
108
109// Ecma-262 3rd 8.6.1
110enum PropertyAttributes {
111 NONE = v8::None,
112 READ_ONLY = v8::ReadOnly,
113 DONT_ENUM = v8::DontEnum,
114 DONT_DELETE = v8::DontDelete,
115 ABSENT = 16 // Used in runtime to indicate a property is absent.
116 // ABSENT can never be stored in or returned from a descriptor's attributes
117 // bitfield. It is only used as a return value meaning the attributes of
118 // a non-existent property.
119};
120
121namespace v8 {
122namespace internal {
123
124
125// PropertyDetails captures type and attributes for a property.
126// They are used both in property dictionaries and instance descriptors.
127class PropertyDetails BASE_EMBEDDED {
128 public:
129
130 PropertyDetails(PropertyAttributes attributes,
131 PropertyType type,
132 int index = 0) {
133 ASSERT(TypeField::is_valid(type));
134 ASSERT(AttributesField::is_valid(attributes));
135 ASSERT(IndexField::is_valid(index));
136
137 value_ = TypeField::encode(type)
138 | AttributesField::encode(attributes)
139 | IndexField::encode(index);
140
141 ASSERT(type == this->type());
142 ASSERT(attributes == this->attributes());
143 ASSERT(index == this->index());
144 }
145
146 // Conversion for storing details as Object*.
147 inline PropertyDetails(Smi* smi);
148 inline Smi* AsSmi();
149
150 PropertyType type() { return TypeField::decode(value_); }
151
152 bool IsTransition() {
153 PropertyType t = type();
154 ASSERT(t != INTERCEPTOR);
155 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION;
156 }
157
158 bool IsProperty() {
159 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
160 }
161
162 PropertyAttributes attributes() { return AttributesField::decode(value_); }
163
164 int index() { return IndexField::decode(value_); }
165
166 inline PropertyDetails AsDeleted();
167
168 static bool IsValidIndex(int index) { return IndexField::is_valid(index); }
169
170 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
171 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
172 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
173 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
174
175 // Bit fields in value_ (type, shift, size). Must be public so the
176 // constants can be embedded in generated code.
177 class TypeField: public BitField<PropertyType, 0, 3> {};
178 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
179 class DeletedField: public BitField<uint32_t, 6, 1> {};
180 class IndexField: public BitField<uint32_t, 7, 31-7> {};
181
182 static const int kInitialIndex = 1;
183 private:
184 uint32_t value_;
185};
186
187
188// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
189enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
190
191
192// PropertyNormalizationMode is used to specify whether to keep
193// inobject properties when normalizing properties of a JSObject.
194enum PropertyNormalizationMode {
195 CLEAR_INOBJECT_PROPERTIES,
196 KEEP_INOBJECT_PROPERTIES
197};
198
199
200// All Maps have a field instance_type containing a InstanceType.
201// It describes the type of the instances.
202//
203// As an example, a JavaScript object is a heap object and its map
204// instance_type is JS_OBJECT_TYPE.
205//
206// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000207// mirror their encoding in the instance_type field of the map. The default
208// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
209// encoding is mentioned explicitly in the name. Likewise, the default
210// representation is considered sequential. It is not mentioned in the
211// name. The other representations (eg, CONS, EXTERNAL) are explicitly
212// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
213// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000214//
215// NOTE: The following things are some that depend on the string types having
216// instance_types that are less than those of all other types:
217// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
218// Object::IsString.
219//
220// NOTE: Everything following JS_VALUE_TYPE is considered a
221// JSObject for GC purposes. The first four entries here have typeof
222// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000223#define INSTANCE_TYPE_LIST_ALL(V) \
224 V(SYMBOL_TYPE) \
225 V(ASCII_SYMBOL_TYPE) \
226 V(CONS_SYMBOL_TYPE) \
227 V(CONS_ASCII_SYMBOL_TYPE) \
228 V(EXTERNAL_SYMBOL_TYPE) \
229 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
230 V(STRING_TYPE) \
231 V(ASCII_STRING_TYPE) \
232 V(CONS_STRING_TYPE) \
233 V(CONS_ASCII_STRING_TYPE) \
234 V(EXTERNAL_STRING_TYPE) \
235 V(EXTERNAL_ASCII_STRING_TYPE) \
236 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
237 \
238 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000239 V(CODE_TYPE) \
240 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
241 V(ODDBALL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000242 \
243 V(HEAP_NUMBER_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000244 V(PROXY_TYPE) \
245 V(BYTE_ARRAY_TYPE) \
246 V(PIXEL_ARRAY_TYPE) \
247 /* Note: the order of these external array */ \
248 /* types is relied upon in */ \
249 /* Object::IsExternalArray(). */ \
250 V(EXTERNAL_BYTE_ARRAY_TYPE) \
251 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
252 V(EXTERNAL_SHORT_ARRAY_TYPE) \
253 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
254 V(EXTERNAL_INT_ARRAY_TYPE) \
255 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
256 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
257 V(FILLER_TYPE) \
258 \
Leon Clarkee46be812010-01-19 14:06:41 +0000259 V(FIXED_ARRAY_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000260 V(ACCESSOR_INFO_TYPE) \
261 V(ACCESS_CHECK_INFO_TYPE) \
262 V(INTERCEPTOR_INFO_TYPE) \
263 V(SHARED_FUNCTION_INFO_TYPE) \
264 V(CALL_HANDLER_INFO_TYPE) \
265 V(FUNCTION_TEMPLATE_INFO_TYPE) \
266 V(OBJECT_TEMPLATE_INFO_TYPE) \
267 V(SIGNATURE_INFO_TYPE) \
268 V(TYPE_SWITCH_INFO_TYPE) \
269 V(SCRIPT_TYPE) \
270 \
271 V(JS_VALUE_TYPE) \
272 V(JS_OBJECT_TYPE) \
273 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
274 V(JS_GLOBAL_OBJECT_TYPE) \
275 V(JS_BUILTINS_OBJECT_TYPE) \
276 V(JS_GLOBAL_PROXY_TYPE) \
277 V(JS_ARRAY_TYPE) \
278 V(JS_REGEXP_TYPE) \
279 \
280 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000281
282#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000283#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
284 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000285 V(BREAK_POINT_INFO_TYPE)
286#else
287#define INSTANCE_TYPE_LIST_DEBUGGER(V)
288#endif
289
Steve Blockd0582a62009-12-15 09:54:21 +0000290#define INSTANCE_TYPE_LIST(V) \
291 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000292 INSTANCE_TYPE_LIST_DEBUGGER(V)
293
294
295// Since string types are not consecutive, this macro is used to
296// iterate over them.
297#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000298 V(SYMBOL_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 SeqTwoByteString::kAlignedSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000300 symbol, \
301 Symbol) \
302 V(ASCII_SYMBOL_TYPE, \
303 SeqAsciiString::kAlignedSize, \
304 ascii_symbol, \
305 AsciiSymbol) \
306 V(CONS_SYMBOL_TYPE, \
307 ConsString::kSize, \
308 cons_symbol, \
309 ConsSymbol) \
310 V(CONS_ASCII_SYMBOL_TYPE, \
311 ConsString::kSize, \
312 cons_ascii_symbol, \
313 ConsAsciiSymbol) \
314 V(EXTERNAL_SYMBOL_TYPE, \
315 ExternalTwoByteString::kSize, \
316 external_symbol, \
317 ExternalSymbol) \
318 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
319 ExternalAsciiString::kSize, \
320 external_ascii_symbol, \
321 ExternalAsciiSymbol) \
322 V(STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000323 SeqTwoByteString::kAlignedSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000324 string, \
325 String) \
326 V(ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000327 SeqAsciiString::kAlignedSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000328 ascii_string, \
329 AsciiString) \
330 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000331 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000332 cons_string, \
333 ConsString) \
334 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000336 cons_ascii_string, \
337 ConsAsciiString) \
338 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000340 external_string, \
341 ExternalString) \
342 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000343 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000344 external_ascii_string, \
345 ExternalAsciiString) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000346
347// A struct is a simple object a set of object-valued fields. Including an
348// object type in this causes the compiler to generate most of the boilerplate
349// code for the class including allocation and garbage collection routines,
350// casts and predicates. All you need to define is the class, methods and
351// object verification routines. Easy, no?
352//
353// Note that for subtle reasons related to the ordering or numerical values of
354// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
355// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000356#define STRUCT_LIST_ALL(V) \
357 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
358 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
359 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
360 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
361 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
362 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
363 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
364 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000365 V(SCRIPT, Script, script)
366
367#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000368#define STRUCT_LIST_DEBUGGER(V) \
369 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000370 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
371#else
372#define STRUCT_LIST_DEBUGGER(V)
373#endif
374
Steve Blockd0582a62009-12-15 09:54:21 +0000375#define STRUCT_LIST(V) \
376 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000377 STRUCT_LIST_DEBUGGER(V)
378
379// We use the full 8 bits of the instance_type field to encode heap object
380// instance types. The high-order bit (bit 7) is set if the object is not a
381// string, and cleared if it is a string.
382const uint32_t kIsNotStringMask = 0x80;
383const uint32_t kStringTag = 0x0;
384const uint32_t kNotStringTag = 0x80;
385
Leon Clarkee46be812010-01-19 14:06:41 +0000386// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
387// There are not enough types that the non-string types (with bit 7 set) can
388// have bit 6 set too.
389const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000390const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000391const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000392
Steve Blocka7e24c12009-10-30 11:49:00 +0000393// If bit 7 is clear then bit 2 indicates whether the string consists of
394// two-byte characters or one-byte characters.
395const uint32_t kStringEncodingMask = 0x4;
396const uint32_t kTwoByteStringTag = 0x0;
397const uint32_t kAsciiStringTag = 0x4;
398
399// If bit 7 is clear, the low-order 2 bits indicate the representation
400// of the string.
401const uint32_t kStringRepresentationMask = 0x03;
402enum StringRepresentationTag {
403 kSeqStringTag = 0x0,
404 kConsStringTag = 0x1,
Steve Blocka7e24c12009-10-30 11:49:00 +0000405 kExternalStringTag = 0x3
406};
407
408
409// A ConsString with an empty string as the right side is a candidate
410// for being shortcut by the garbage collector unless it is a
411// symbol. It's not common to have non-flat symbols, so we do not
412// shortcut them thereby avoiding turning symbols into strings. See
413// heap.cc and mark-compact.cc.
414const uint32_t kShortcutTypeMask =
415 kIsNotStringMask |
416 kIsSymbolMask |
417 kStringRepresentationMask;
418const uint32_t kShortcutTypeTag = kConsStringTag;
419
420
421enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000422 // String types.
Steve Blockd0582a62009-12-15 09:54:21 +0000423 SYMBOL_TYPE = kSymbolTag | kSeqStringTag,
424 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
425 CONS_SYMBOL_TYPE = kSymbolTag | kConsStringTag,
426 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
427 EXTERNAL_SYMBOL_TYPE = kSymbolTag | kExternalStringTag,
428 EXTERNAL_ASCII_SYMBOL_TYPE =
429 kAsciiStringTag | kSymbolTag | kExternalStringTag,
430 STRING_TYPE = kSeqStringTag,
431 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
432 CONS_STRING_TYPE = kConsStringTag,
433 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
434 EXTERNAL_STRING_TYPE = kExternalStringTag,
435 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
436 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000437
Leon Clarkee46be812010-01-19 14:06:41 +0000438 // Objects allocated in their own spaces (never in new space).
439 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000440 CODE_TYPE,
441 ODDBALL_TYPE,
442 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000443
444 // "Data", objects that cannot contain non-map-word pointers to heap
445 // objects.
446 HEAP_NUMBER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000447 PROXY_TYPE,
448 BYTE_ARRAY_TYPE,
449 PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000450 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000451 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
452 EXTERNAL_SHORT_ARRAY_TYPE,
453 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
454 EXTERNAL_INT_ARRAY_TYPE,
455 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000456 EXTERNAL_FLOAT_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
457 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000458
Leon Clarkee46be812010-01-19 14:06:41 +0000459 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 ACCESSOR_INFO_TYPE,
461 ACCESS_CHECK_INFO_TYPE,
462 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 CALL_HANDLER_INFO_TYPE,
464 FUNCTION_TEMPLATE_INFO_TYPE,
465 OBJECT_TEMPLATE_INFO_TYPE,
466 SIGNATURE_INFO_TYPE,
467 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000468 SCRIPT_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000469#ifdef ENABLE_DEBUGGER_SUPPORT
470 DEBUG_INFO_TYPE,
471 BREAK_POINT_INFO_TYPE,
472#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000473
Leon Clarkee46be812010-01-19 14:06:41 +0000474 FIXED_ARRAY_TYPE,
475 SHARED_FUNCTION_INFO_TYPE,
476
477 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000478 JS_OBJECT_TYPE,
479 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
480 JS_GLOBAL_OBJECT_TYPE,
481 JS_BUILTINS_OBJECT_TYPE,
482 JS_GLOBAL_PROXY_TYPE,
483 JS_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000484 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000485
486 JS_FUNCTION_TYPE,
487
488 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000489 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000491 INVALID_TYPE = FIRST_TYPE - 1,
492 FIRST_NONSTRING_TYPE = MAP_TYPE,
493 // Boundaries for testing for an external array.
494 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
495 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_FLOAT_ARRAY_TYPE,
496 // Boundary for promotion to old data space/old pointer space.
497 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 // Boundaries for testing the type is a JavaScript "object". Note that
499 // function objects are not counted as objects, even though they are
500 // implemented as such; only values whose typeof is "object" are included.
501 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
502 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE
503};
504
505
506enum CompareResult {
507 LESS = -1,
508 EQUAL = 0,
509 GREATER = 1,
510
511 NOT_EQUAL = GREATER
512};
513
514
515#define DECL_BOOLEAN_ACCESSORS(name) \
516 inline bool name(); \
517 inline void set_##name(bool value); \
518
519
520#define DECL_ACCESSORS(name, type) \
521 inline type* name(); \
522 inline void set_##name(type* value, \
523 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
524
525
526class StringStream;
527class ObjectVisitor;
528
529struct ValueInfo : public Malloced {
530 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
531 InstanceType type;
532 Object* ptr;
533 const char* str;
534 double number;
535};
536
537
538// A template-ized version of the IsXXX functions.
539template <class C> static inline bool Is(Object* obj);
540
541
542// Object is the abstract superclass for all classes in the
543// object hierarchy.
544// Object does not use any virtual functions to avoid the
545// allocation of the C++ vtable.
546// Since Smi and Failure are subclasses of Object no
547// data members can be present in Object.
548class Object BASE_EMBEDDED {
549 public:
550 // Type testing.
551 inline bool IsSmi();
552 inline bool IsHeapObject();
553 inline bool IsHeapNumber();
554 inline bool IsString();
555 inline bool IsSymbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 // See objects-inl.h for more details
557 inline bool IsSeqString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 inline bool IsExternalString();
559 inline bool IsExternalTwoByteString();
560 inline bool IsExternalAsciiString();
561 inline bool IsSeqTwoByteString();
562 inline bool IsSeqAsciiString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000563 inline bool IsConsString();
564
565 inline bool IsNumber();
566 inline bool IsByteArray();
567 inline bool IsPixelArray();
Steve Block3ce2e202009-11-05 08:53:23 +0000568 inline bool IsExternalArray();
569 inline bool IsExternalByteArray();
570 inline bool IsExternalUnsignedByteArray();
571 inline bool IsExternalShortArray();
572 inline bool IsExternalUnsignedShortArray();
573 inline bool IsExternalIntArray();
574 inline bool IsExternalUnsignedIntArray();
575 inline bool IsExternalFloatArray();
Steve Blocka7e24c12009-10-30 11:49:00 +0000576 inline bool IsFailure();
577 inline bool IsRetryAfterGC();
578 inline bool IsOutOfMemoryFailure();
579 inline bool IsException();
580 inline bool IsJSObject();
581 inline bool IsJSContextExtensionObject();
582 inline bool IsMap();
583 inline bool IsFixedArray();
584 inline bool IsDescriptorArray();
585 inline bool IsContext();
586 inline bool IsCatchContext();
587 inline bool IsGlobalContext();
588 inline bool IsJSFunction();
589 inline bool IsCode();
590 inline bool IsOddball();
591 inline bool IsSharedFunctionInfo();
592 inline bool IsJSValue();
593 inline bool IsStringWrapper();
594 inline bool IsProxy();
595 inline bool IsBoolean();
596 inline bool IsJSArray();
597 inline bool IsJSRegExp();
598 inline bool IsHashTable();
599 inline bool IsDictionary();
600 inline bool IsSymbolTable();
601 inline bool IsCompilationCacheTable();
602 inline bool IsMapCache();
603 inline bool IsPrimitive();
604 inline bool IsGlobalObject();
605 inline bool IsJSGlobalObject();
606 inline bool IsJSBuiltinsObject();
607 inline bool IsJSGlobalProxy();
608 inline bool IsUndetectableObject();
609 inline bool IsAccessCheckNeeded();
610 inline bool IsJSGlobalPropertyCell();
611
612 // Returns true if this object is an instance of the specified
613 // function template.
614 inline bool IsInstanceOf(FunctionTemplateInfo* type);
615
616 inline bool IsStruct();
617#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
618 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
619#undef DECLARE_STRUCT_PREDICATE
620
621 // Oddball testing.
622 INLINE(bool IsUndefined());
623 INLINE(bool IsTheHole());
624 INLINE(bool IsNull());
625 INLINE(bool IsTrue());
626 INLINE(bool IsFalse());
627
628 // Extract the number.
629 inline double Number();
630
631 inline bool HasSpecificClassOf(String* name);
632
633 Object* ToObject(); // ECMA-262 9.9.
634 Object* ToBoolean(); // ECMA-262 9.2.
635
636 // Convert to a JSObject if needed.
637 // global_context is used when creating wrapper object.
638 Object* ToObject(Context* global_context);
639
640 // Converts this to a Smi if possible.
641 // Failure is returned otherwise.
642 inline Object* ToSmi();
643
644 void Lookup(String* name, LookupResult* result);
645
646 // Property access.
647 inline Object* GetProperty(String* key);
648 inline Object* GetProperty(String* key, PropertyAttributes* attributes);
649 Object* GetPropertyWithReceiver(Object* receiver,
650 String* key,
651 PropertyAttributes* attributes);
652 Object* GetProperty(Object* receiver,
653 LookupResult* result,
654 String* key,
655 PropertyAttributes* attributes);
656 Object* GetPropertyWithCallback(Object* receiver,
657 Object* structure,
658 String* name,
659 Object* holder);
660 Object* GetPropertyWithDefinedGetter(Object* receiver,
661 JSFunction* getter);
662
663 inline Object* GetElement(uint32_t index);
664 Object* GetElementWithReceiver(Object* receiver, uint32_t index);
665
666 // Return the object's prototype (might be Heap::null_value()).
667 Object* GetPrototype();
668
669 // Returns true if this is a JSValue containing a string and the index is
670 // < the length of the string. Used to implement [] on strings.
671 inline bool IsStringObjectWithCharacterAt(uint32_t index);
672
673#ifdef DEBUG
674 // Prints this object with details.
675 void Print();
676 void PrintLn();
677 // Verifies the object.
678 void Verify();
679
680 // Verify a pointer is a valid object pointer.
681 static void VerifyPointer(Object* p);
682#endif
683
684 // Prints this object without details.
685 void ShortPrint();
686
687 // Prints this object without details to a message accumulator.
688 void ShortPrint(StringStream* accumulator);
689
690 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
691 static Object* cast(Object* value) { return value; }
692
693 // Layout description.
694 static const int kHeaderSize = 0; // Object does not take up any space.
695
696 private:
697 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
698};
699
700
701// Smi represents integer Numbers that can be stored in 31 bits.
702// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000703// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000704// For long smis it has the following format:
705// [32 bit signed int] [31 bits zero padding] 0
706// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000707class Smi: public Object {
708 public:
709 // Returns the integer value.
710 inline int value();
711
712 // Convert a value to a Smi object.
713 static inline Smi* FromInt(int value);
714
715 static inline Smi* FromIntptr(intptr_t value);
716
717 // Returns whether value can be represented in a Smi.
718 static inline bool IsValid(intptr_t value);
719
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 // Casting.
721 static inline Smi* cast(Object* object);
722
723 // Dispatched behavior.
724 void SmiPrint();
725 void SmiPrint(StringStream* accumulator);
726#ifdef DEBUG
727 void SmiVerify();
728#endif
729
Steve Block3ce2e202009-11-05 08:53:23 +0000730 static const int kMinValue = (-1 << (kSmiValueSize - 1));
731 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000732
733 private:
734 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
735};
736
737
738// Failure is used for reporting out of memory situations and
739// propagating exceptions through the runtime system. Failure objects
740// are transient and cannot occur as part of the object graph.
741//
742// Failures are a single word, encoded as follows:
743// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000744// |...rrrrrrrrrrrrrrrrrrrrrr|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000745// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000746// 7 6 4 32 10
747//
Steve Blocka7e24c12009-10-30 11:49:00 +0000748//
749// The low two bits, 0-1, are the failure tag, 11. The next two bits,
750// 2-3, are a failure type tag 'tt' with possible values:
751// 00 RETRY_AFTER_GC
752// 01 EXCEPTION
753// 10 INTERNAL_ERROR
754// 11 OUT_OF_MEMORY_EXCEPTION
755//
756// The next three bits, 4-6, are an allocation space tag 'sss'. The
757// allocation space tag is 000 for all failure types except
758// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
759// allocation spaces (the encoding is found in globals.h).
760//
761// The remaining bits is the size of the allocation request in units
762// of the pointer size, and is zeroed except for RETRY_AFTER_GC
763// failures. The 25 bits (on a 32 bit platform) gives a representable
764// range of 2^27 bytes (128MB).
765
766// Failure type tag info.
767const int kFailureTypeTagSize = 2;
768const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
769
770class Failure: public Object {
771 public:
772 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
773 enum Type {
774 RETRY_AFTER_GC = 0,
775 EXCEPTION = 1, // Returning this marker tells the real exception
776 // is in Top::pending_exception.
777 INTERNAL_ERROR = 2,
778 OUT_OF_MEMORY_EXCEPTION = 3
779 };
780
781 inline Type type() const;
782
783 // Returns the space that needs to be collected for RetryAfterGC failures.
784 inline AllocationSpace allocation_space() const;
785
786 // Returns the number of bytes requested (up to the representable maximum)
787 // for RetryAfterGC failures.
788 inline int requested() const;
789
790 inline bool IsInternalError() const;
791 inline bool IsOutOfMemoryException() const;
792
793 static Failure* RetryAfterGC(int requested_bytes, AllocationSpace space);
794 static inline Failure* RetryAfterGC(int requested_bytes); // NEW_SPACE
795 static inline Failure* Exception();
796 static inline Failure* InternalError();
797 static inline Failure* OutOfMemoryException();
798 // Casting.
799 static inline Failure* cast(Object* object);
800
801 // Dispatched behavior.
802 void FailurePrint();
803 void FailurePrint(StringStream* accumulator);
804#ifdef DEBUG
805 void FailureVerify();
806#endif
807
808 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000809 inline intptr_t value() const;
810 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000811
812 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
813};
814
815
816// Heap objects typically have a map pointer in their first word. However,
817// during GC other data (eg, mark bits, forwarding addresses) is sometimes
818// encoded in the first word. The class MapWord is an abstraction of the
819// value in a heap object's first word.
820class MapWord BASE_EMBEDDED {
821 public:
822 // Normal state: the map word contains a map pointer.
823
824 // Create a map word from a map pointer.
825 static inline MapWord FromMap(Map* map);
826
827 // View this map word as a map pointer.
828 inline Map* ToMap();
829
830
831 // Scavenge collection: the map word of live objects in the from space
832 // contains a forwarding address (a heap object pointer in the to space).
833
834 // True if this map word is a forwarding address for a scavenge
835 // collection. Only valid during a scavenge collection (specifically,
836 // when all map words are heap object pointers, ie. not during a full GC).
837 inline bool IsForwardingAddress();
838
839 // Create a map word from a forwarding address.
840 static inline MapWord FromForwardingAddress(HeapObject* object);
841
842 // View this map word as a forwarding address.
843 inline HeapObject* ToForwardingAddress();
844
Steve Blocka7e24c12009-10-30 11:49:00 +0000845 // Marking phase of full collection: the map word of live objects is
846 // marked, and may be marked as overflowed (eg, the object is live, its
847 // children have not been visited, and it does not fit in the marking
848 // stack).
849
850 // True if this map word's mark bit is set.
851 inline bool IsMarked();
852
853 // Return this map word but with its mark bit set.
854 inline void SetMark();
855
856 // Return this map word but with its mark bit cleared.
857 inline void ClearMark();
858
859 // True if this map word's overflow bit is set.
860 inline bool IsOverflowed();
861
862 // Return this map word but with its overflow bit set.
863 inline void SetOverflow();
864
865 // Return this map word but with its overflow bit cleared.
866 inline void ClearOverflow();
867
868
869 // Compacting phase of a full compacting collection: the map word of live
870 // objects contains an encoding of the original map address along with the
871 // forwarding address (represented as an offset from the first live object
872 // in the same page as the (old) object address).
873
874 // Create a map word from a map address and a forwarding address offset.
875 static inline MapWord EncodeAddress(Address map_address, int offset);
876
877 // Return the map address encoded in this map word.
878 inline Address DecodeMapAddress(MapSpace* map_space);
879
880 // Return the forwarding offset encoded in this map word.
881 inline int DecodeOffset();
882
883
884 // During serialization: the map word is used to hold an encoded
885 // address, and possibly a mark bit (set and cleared with SetMark
886 // and ClearMark).
887
888 // Create a map word from an encoded address.
889 static inline MapWord FromEncodedAddress(Address address);
890
891 inline Address ToEncodedAddress();
892
893 // Bits used by the marking phase of the garbage collector.
894 //
895 // The first word of a heap object is normally a map pointer. The last two
896 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
897 // mark an object as live and/or overflowed:
898 // last bit = 0, marked as alive
899 // second bit = 1, overflowed
900 // An object is only marked as overflowed when it is marked as live while
901 // the marking stack is overflowed.
902 static const int kMarkingBit = 0; // marking bit
903 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
904 static const int kOverflowBit = 1; // overflow bit
905 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
906
Leon Clarkee46be812010-01-19 14:06:41 +0000907 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
908 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +0000909 // +-----------------+------------------+-----------------+
910 // |forwarding offset|page offset of map|page index of map|
911 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +0000912 // ^ ^ ^
913 // | | |
914 // | | kMapPageIndexBits
915 // | kMapPageOffsetBits
916 // kForwardingOffsetBits
917 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
918 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
919#ifdef V8_HOST_ARCH_64_BIT
920 static const int kMapPageIndexBits = 16;
921#else
922 // Use all the 32-bits to encode on a 32-bit platform.
923 static const int kMapPageIndexBits =
924 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
925#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000926
927 static const int kMapPageIndexShift = 0;
928 static const int kMapPageOffsetShift =
929 kMapPageIndexShift + kMapPageIndexBits;
930 static const int kForwardingOffsetShift =
931 kMapPageOffsetShift + kMapPageOffsetBits;
932
Leon Clarkee46be812010-01-19 14:06:41 +0000933 // Bit masks covering the different parts the encoding.
934 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000935 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +0000936 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000937 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +0000938 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000939 ~(kMapPageIndexMask | kMapPageOffsetMask);
940
941 private:
942 // HeapObject calls the private constructor and directly reads the value.
943 friend class HeapObject;
944
945 explicit MapWord(uintptr_t value) : value_(value) {}
946
947 uintptr_t value_;
948};
949
950
951// HeapObject is the superclass for all classes describing heap allocated
952// objects.
953class HeapObject: public Object {
954 public:
955 // [map]: Contains a map which contains the object's reflective
956 // information.
957 inline Map* map();
958 inline void set_map(Map* value);
959
960 // During garbage collection, the map word of a heap object does not
961 // necessarily contain a map pointer.
962 inline MapWord map_word();
963 inline void set_map_word(MapWord map_word);
964
965 // Converts an address to a HeapObject pointer.
966 static inline HeapObject* FromAddress(Address address);
967
968 // Returns the address of this HeapObject.
969 inline Address address();
970
971 // Iterates over pointers contained in the object (including the Map)
972 void Iterate(ObjectVisitor* v);
973
974 // Iterates over all pointers contained in the object except the
975 // first map pointer. The object type is given in the first
976 // parameter. This function does not access the map pointer in the
977 // object, and so is safe to call while the map pointer is modified.
978 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
979
980 // This method only applies to struct objects. Iterates over all the fields
981 // of this struct.
982 void IterateStructBody(int object_size, ObjectVisitor* v);
983
984 // Returns the heap object's size in bytes
985 inline int Size();
986
987 // Given a heap object's map pointer, returns the heap size in bytes
988 // Useful when the map pointer field is used for other purposes.
989 // GC internal.
990 inline int SizeFromMap(Map* map);
991
992 // Support for the marking heap objects during the marking phase of GC.
993 // True if the object is marked live.
994 inline bool IsMarked();
995
996 // Mutate this object's map pointer to indicate that the object is live.
997 inline void SetMark();
998
999 // Mutate this object's map pointer to remove the indication that the
1000 // object is live (ie, partially restore the map pointer).
1001 inline void ClearMark();
1002
1003 // True if this object is marked as overflowed. Overflowed objects have
1004 // been reached and marked during marking of the heap, but their children
1005 // have not necessarily been marked and they have not been pushed on the
1006 // marking stack.
1007 inline bool IsOverflowed();
1008
1009 // Mutate this object's map pointer to indicate that the object is
1010 // overflowed.
1011 inline void SetOverflow();
1012
1013 // Mutate this object's map pointer to remove the indication that the
1014 // object is overflowed (ie, partially restore the map pointer).
1015 inline void ClearOverflow();
1016
1017 // Returns the field at offset in obj, as a read/write Object* reference.
1018 // Does no checking, and is safe to use during GC, while maps are invalid.
1019 // Does not update remembered sets, so should only be assigned to
1020 // during marking GC.
1021 static inline Object** RawField(HeapObject* obj, int offset);
1022
1023 // Casting.
1024 static inline HeapObject* cast(Object* obj);
1025
Leon Clarke4515c472010-02-03 11:58:03 +00001026 // Return the write barrier mode for this. Callers of this function
1027 // must be able to present a reference to an AssertNoAllocation
1028 // object as a sign that they are not going to use this function
1029 // from code that allocates and thus invalidates the returned write
1030 // barrier mode.
1031 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001032
1033 // Dispatched behavior.
1034 void HeapObjectShortPrint(StringStream* accumulator);
1035#ifdef DEBUG
1036 void HeapObjectPrint();
1037 void HeapObjectVerify();
1038 inline void VerifyObjectField(int offset);
1039
1040 void PrintHeader(const char* id);
1041
1042 // Verify a pointer is a valid HeapObject pointer that points to object
1043 // areas in the heap.
1044 static void VerifyHeapPointer(Object* p);
1045#endif
1046
1047 // Layout description.
1048 // First field in a heap object is map.
1049 static const int kMapOffset = Object::kHeaderSize;
1050 static const int kHeaderSize = kMapOffset + kPointerSize;
1051
1052 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1053
1054 protected:
1055 // helpers for calling an ObjectVisitor to iterate over pointers in the
1056 // half-open range [start, end) specified as integer offsets
1057 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1058 // as above, for the single element at "offset"
1059 inline void IteratePointer(ObjectVisitor* v, int offset);
1060
1061 // Computes the object size from the map.
1062 // Should only be used from SizeFromMap.
1063 int SlowSizeFromMap(Map* map);
1064
1065 private:
1066 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1067};
1068
1069
1070// The HeapNumber class describes heap allocated numbers that cannot be
1071// represented in a Smi (small integer)
1072class HeapNumber: public HeapObject {
1073 public:
1074 // [value]: number value.
1075 inline double value();
1076 inline void set_value(double value);
1077
1078 // Casting.
1079 static inline HeapNumber* cast(Object* obj);
1080
1081 // Dispatched behavior.
1082 Object* HeapNumberToBoolean();
1083 void HeapNumberPrint();
1084 void HeapNumberPrint(StringStream* accumulator);
1085#ifdef DEBUG
1086 void HeapNumberVerify();
1087#endif
1088
1089 // Layout description.
1090 static const int kValueOffset = HeapObject::kHeaderSize;
1091 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1092 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1093 // little endian apart from non-EABI arm which is little endian with big
1094 // endian floating point word ordering!
Steve Block3ce2e202009-11-05 08:53:23 +00001095#if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI)
Steve Blocka7e24c12009-10-30 11:49:00 +00001096 static const int kMantissaOffset = kValueOffset;
1097 static const int kExponentOffset = kValueOffset + 4;
1098#else
1099 static const int kMantissaOffset = kValueOffset + 4;
1100 static const int kExponentOffset = kValueOffset;
1101# define BIG_ENDIAN_FLOATING_POINT 1
1102#endif
1103 static const int kSize = kValueOffset + kDoubleSize;
1104
1105 static const uint32_t kSignMask = 0x80000000u;
1106 static const uint32_t kExponentMask = 0x7ff00000u;
1107 static const uint32_t kMantissaMask = 0xfffffu;
1108 static const int kExponentBias = 1023;
1109 static const int kExponentShift = 20;
1110 static const int kMantissaBitsInTopWord = 20;
1111 static const int kNonMantissaBitsInTopWord = 12;
1112
1113 private:
1114 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1115};
1116
1117
1118// The JSObject describes real heap allocated JavaScript objects with
1119// properties.
1120// Note that the map of JSObject changes during execution to enable inline
1121// caching.
1122class JSObject: public HeapObject {
1123 public:
1124 enum DeleteMode { NORMAL_DELETION, FORCE_DELETION };
1125 enum ElementsKind {
1126 FAST_ELEMENTS,
1127 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001128 PIXEL_ELEMENTS,
1129 EXTERNAL_BYTE_ELEMENTS,
1130 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1131 EXTERNAL_SHORT_ELEMENTS,
1132 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1133 EXTERNAL_INT_ELEMENTS,
1134 EXTERNAL_UNSIGNED_INT_ELEMENTS,
1135 EXTERNAL_FLOAT_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001136 };
1137
1138 // [properties]: Backing storage for properties.
1139 // properties is a FixedArray in the fast case, and a Dictionary in the
1140 // slow case.
1141 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1142 inline void initialize_properties();
1143 inline bool HasFastProperties();
1144 inline StringDictionary* property_dictionary(); // Gets slow properties.
1145
1146 // [elements]: The elements (properties with names that are integers).
1147 // elements is a FixedArray in the fast case, and a Dictionary in the slow
1148 // case or a PixelArray in a special case.
1149 DECL_ACCESSORS(elements, Array) // Get and set fast elements.
1150 inline void initialize_elements();
1151 inline ElementsKind GetElementsKind();
1152 inline bool HasFastElements();
1153 inline bool HasDictionaryElements();
1154 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001155 inline bool HasExternalArrayElements();
1156 inline bool HasExternalByteElements();
1157 inline bool HasExternalUnsignedByteElements();
1158 inline bool HasExternalShortElements();
1159 inline bool HasExternalUnsignedShortElements();
1160 inline bool HasExternalIntElements();
1161 inline bool HasExternalUnsignedIntElements();
1162 inline bool HasExternalFloatElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001163 inline NumberDictionary* element_dictionary(); // Gets slow elements.
1164
1165 // Collects elements starting at index 0.
1166 // Undefined values are placed after non-undefined values.
1167 // Returns the number of non-undefined values.
1168 Object* PrepareElementsForSort(uint32_t limit);
1169 // As PrepareElementsForSort, but only on objects where elements is
1170 // a dictionary, and it will stay a dictionary.
1171 Object* PrepareSlowElementsForSort(uint32_t limit);
1172
1173 Object* SetProperty(String* key,
1174 Object* value,
1175 PropertyAttributes attributes);
1176 Object* SetProperty(LookupResult* result,
1177 String* key,
1178 Object* value,
1179 PropertyAttributes attributes);
1180 Object* SetPropertyWithFailedAccessCheck(LookupResult* result,
1181 String* name,
1182 Object* value);
1183 Object* SetPropertyWithCallback(Object* structure,
1184 String* name,
1185 Object* value,
1186 JSObject* holder);
1187 Object* SetPropertyWithDefinedSetter(JSFunction* setter,
1188 Object* value);
1189 Object* SetPropertyWithInterceptor(String* name,
1190 Object* value,
1191 PropertyAttributes attributes);
1192 Object* SetPropertyPostInterceptor(String* name,
1193 Object* value,
1194 PropertyAttributes attributes);
1195 Object* IgnoreAttributesAndSetLocalProperty(String* key,
1196 Object* value,
1197 PropertyAttributes attributes);
1198
1199 // Retrieve a value in a normalized object given a lookup result.
1200 // Handles the special representation of JS global objects.
1201 Object* GetNormalizedProperty(LookupResult* result);
1202
1203 // Sets the property value in a normalized object given a lookup result.
1204 // Handles the special representation of JS global objects.
1205 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1206
1207 // Sets the property value in a normalized object given (key, value, details).
1208 // Handles the special representation of JS global objects.
1209 Object* SetNormalizedProperty(String* name,
1210 Object* value,
1211 PropertyDetails details);
1212
1213 // Deletes the named property in a normalized object.
1214 Object* DeleteNormalizedProperty(String* name, DeleteMode mode);
1215
1216 // Sets a property that currently has lazy loading.
1217 Object* SetLazyProperty(LookupResult* result,
1218 String* name,
1219 Object* value,
1220 PropertyAttributes attributes);
1221
1222 // Returns the class name ([[Class]] property in the specification).
1223 String* class_name();
1224
1225 // Returns the constructor name (the name (possibly, inferred name) of the
1226 // function that was used to instantiate the object).
1227 String* constructor_name();
1228
1229 // Retrieve interceptors.
1230 InterceptorInfo* GetNamedInterceptor();
1231 InterceptorInfo* GetIndexedInterceptor();
1232
1233 inline PropertyAttributes GetPropertyAttribute(String* name);
1234 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1235 String* name);
1236 PropertyAttributes GetLocalPropertyAttribute(String* name);
1237
1238 Object* DefineAccessor(String* name, bool is_getter, JSFunction* fun,
1239 PropertyAttributes attributes);
1240 Object* LookupAccessor(String* name, bool is_getter);
1241
1242 // Used from Object::GetProperty().
1243 Object* GetPropertyWithFailedAccessCheck(Object* receiver,
1244 LookupResult* result,
1245 String* name,
1246 PropertyAttributes* attributes);
1247 Object* GetPropertyWithInterceptor(JSObject* receiver,
1248 String* name,
1249 PropertyAttributes* attributes);
1250 Object* GetPropertyPostInterceptor(JSObject* receiver,
1251 String* name,
1252 PropertyAttributes* attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001253 Object* GetLocalPropertyPostInterceptor(JSObject* receiver,
1254 String* name,
1255 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001256 Object* GetLazyProperty(Object* receiver,
1257 LookupResult* result,
1258 String* name,
1259 PropertyAttributes* attributes);
1260
1261 // Tells whether this object needs to be loaded.
1262 inline bool IsLoaded();
1263
1264 // Returns true if this is an instance of an api function and has
1265 // been modified since it was created. May give false positives.
1266 bool IsDirty();
1267
1268 bool HasProperty(String* name) {
1269 return GetPropertyAttribute(name) != ABSENT;
1270 }
1271
1272 // Can cause a GC if it hits an interceptor.
1273 bool HasLocalProperty(String* name) {
1274 return GetLocalPropertyAttribute(name) != ABSENT;
1275 }
1276
Steve Blockd0582a62009-12-15 09:54:21 +00001277 // If the receiver is a JSGlobalProxy this method will return its prototype,
1278 // otherwise the result is the receiver itself.
1279 inline Object* BypassGlobalProxy();
1280
1281 // Accessors for hidden properties object.
1282 //
1283 // Hidden properties are not local properties of the object itself.
1284 // Instead they are stored on an auxiliary JSObject stored as a local
1285 // property with a special name Heap::hidden_symbol(). But if the
1286 // receiver is a JSGlobalProxy then the auxiliary object is a property
1287 // of its prototype.
1288 //
1289 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1290 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1291 // holder.
1292 //
1293 // These accessors do not touch interceptors or accessors.
1294 inline bool HasHiddenPropertiesObject();
1295 inline Object* GetHiddenPropertiesObject();
1296 inline Object* SetHiddenPropertiesObject(Object* hidden_obj);
1297
Steve Blocka7e24c12009-10-30 11:49:00 +00001298 Object* DeleteProperty(String* name, DeleteMode mode);
1299 Object* DeleteElement(uint32_t index, DeleteMode mode);
1300 Object* DeleteLazyProperty(LookupResult* result,
1301 String* name,
1302 DeleteMode mode);
1303
1304 // Tests for the fast common case for property enumeration.
1305 bool IsSimpleEnum();
1306
1307 // Do we want to keep the elements in fast case when increasing the
1308 // capacity?
1309 bool ShouldConvertToSlowElements(int new_capacity);
1310 // Returns true if the backing storage for the slow-case elements of
1311 // this object takes up nearly as much space as a fast-case backing
1312 // storage would. In that case the JSObject should have fast
1313 // elements.
1314 bool ShouldConvertToFastElements();
1315
1316 // Return the object's prototype (might be Heap::null_value()).
1317 inline Object* GetPrototype();
1318
1319 // Tells whether the index'th element is present.
1320 inline bool HasElement(uint32_t index);
1321 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
1322 bool HasLocalElement(uint32_t index);
1323
1324 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1325 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1326
1327 Object* SetFastElement(uint32_t index, Object* value);
1328
1329 // Set the index'th array element.
1330 // A Failure object is returned if GC is needed.
1331 Object* SetElement(uint32_t index, Object* value);
1332
1333 // Returns the index'th element.
1334 // The undefined object if index is out of bounds.
1335 Object* GetElementWithReceiver(JSObject* receiver, uint32_t index);
1336
1337 void SetFastElements(FixedArray* elements);
1338 Object* SetSlowElements(Object* length);
1339
1340 // Lookup interceptors are used for handling properties controlled by host
1341 // objects.
1342 inline bool HasNamedInterceptor();
1343 inline bool HasIndexedInterceptor();
1344
1345 // Support functions for v8 api (needed for correct interceptor behavior).
1346 bool HasRealNamedProperty(String* key);
1347 bool HasRealElementProperty(uint32_t index);
1348 bool HasRealNamedCallbackProperty(String* key);
1349
1350 // Initializes the array to a certain length
1351 Object* SetElementsLength(Object* length);
1352
1353 // Get the header size for a JSObject. Used to compute the index of
1354 // internal fields as well as the number of internal fields.
1355 inline int GetHeaderSize();
1356
1357 inline int GetInternalFieldCount();
1358 inline Object* GetInternalField(int index);
1359 inline void SetInternalField(int index, Object* value);
1360
1361 // Lookup a property. If found, the result is valid and has
1362 // detailed information.
1363 void LocalLookup(String* name, LookupResult* result);
1364 void Lookup(String* name, LookupResult* result);
1365
1366 // The following lookup functions skip interceptors.
1367 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1368 void LookupRealNamedProperty(String* name, LookupResult* result);
1369 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1370 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
1371 Object* LookupCallbackSetterInPrototypes(uint32_t index);
1372 void LookupCallback(String* name, LookupResult* result);
1373
1374 // Returns the number of properties on this object filtering out properties
1375 // with the specified attributes (ignoring interceptors).
1376 int NumberOfLocalProperties(PropertyAttributes filter);
1377 // Returns the number of enumerable properties (ignoring interceptors).
1378 int NumberOfEnumProperties();
1379 // Fill in details for properties into storage starting at the specified
1380 // index.
1381 void GetLocalPropertyNames(FixedArray* storage, int index);
1382
1383 // Returns the number of properties on this object filtering out properties
1384 // with the specified attributes (ignoring interceptors).
1385 int NumberOfLocalElements(PropertyAttributes filter);
1386 // Returns the number of enumerable elements (ignoring interceptors).
1387 int NumberOfEnumElements();
1388 // Returns the number of elements on this object filtering out elements
1389 // with the specified attributes (ignoring interceptors).
1390 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1391 // Count and fill in the enumerable elements into storage.
1392 // (storage->length() == NumberOfEnumElements()).
1393 // If storage is NULL, will count the elements without adding
1394 // them to any storage.
1395 // Returns the number of enumerable elements.
1396 int GetEnumElementKeys(FixedArray* storage);
1397
1398 // Add a property to a fast-case object using a map transition to
1399 // new_map.
1400 Object* AddFastPropertyUsingMap(Map* new_map,
1401 String* name,
1402 Object* value);
1403
1404 // Add a constant function property to a fast-case object.
1405 // This leaves a CONSTANT_TRANSITION in the old map, and
1406 // if it is called on a second object with this map, a
1407 // normal property is added instead, with a map transition.
1408 // This avoids the creation of many maps with the same constant
1409 // function, all orphaned.
1410 Object* AddConstantFunctionProperty(String* name,
1411 JSFunction* function,
1412 PropertyAttributes attributes);
1413
1414 Object* ReplaceSlowProperty(String* name,
1415 Object* value,
1416 PropertyAttributes attributes);
1417
1418 // Converts a descriptor of any other type to a real field,
1419 // backed by the properties array. Descriptors of visible
1420 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1421 // Converts the descriptor on the original object's map to a
1422 // map transition, and the the new field is on the object's new map.
1423 Object* ConvertDescriptorToFieldAndMapTransition(
1424 String* name,
1425 Object* new_value,
1426 PropertyAttributes attributes);
1427
1428 // Converts a descriptor of any other type to a real field,
1429 // backed by the properties array. Descriptors of visible
1430 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1431 Object* ConvertDescriptorToField(String* name,
1432 Object* new_value,
1433 PropertyAttributes attributes);
1434
1435 // Add a property to a fast-case object.
1436 Object* AddFastProperty(String* name,
1437 Object* value,
1438 PropertyAttributes attributes);
1439
1440 // Add a property to a slow-case object.
1441 Object* AddSlowProperty(String* name,
1442 Object* value,
1443 PropertyAttributes attributes);
1444
1445 // Add a property to an object.
1446 Object* AddProperty(String* name,
1447 Object* value,
1448 PropertyAttributes attributes);
1449
1450 // Convert the object to use the canonical dictionary
1451 // representation. If the object is expected to have additional properties
1452 // added this number can be indicated to have the backing store allocated to
1453 // an initial capacity for holding these properties.
1454 Object* NormalizeProperties(PropertyNormalizationMode mode,
1455 int expected_additional_properties);
1456 Object* NormalizeElements();
1457
1458 // Transform slow named properties to fast variants.
1459 // Returns failure if allocation failed.
1460 Object* TransformToFastProperties(int unused_property_fields);
1461
1462 // Access fast-case object properties at index.
1463 inline Object* FastPropertyAt(int index);
1464 inline Object* FastPropertyAtPut(int index, Object* value);
1465
1466 // Access to in object properties.
1467 inline Object* InObjectPropertyAt(int index);
1468 inline Object* InObjectPropertyAtPut(int index,
1469 Object* value,
1470 WriteBarrierMode mode
1471 = UPDATE_WRITE_BARRIER);
1472
1473 // initializes the body after properties slot, properties slot is
1474 // initialized by set_properties
1475 // Note: this call does not update write barrier, it is caller's
1476 // reponsibility to ensure that *v* can be collected without WB here.
1477 inline void InitializeBody(int object_size);
1478
1479 // Check whether this object references another object
1480 bool ReferencesObject(Object* obj);
1481
1482 // Casting.
1483 static inline JSObject* cast(Object* obj);
1484
1485 // Dispatched behavior.
1486 void JSObjectIterateBody(int object_size, ObjectVisitor* v);
1487 void JSObjectShortPrint(StringStream* accumulator);
1488#ifdef DEBUG
1489 void JSObjectPrint();
1490 void JSObjectVerify();
1491 void PrintProperties();
1492 void PrintElements();
1493
1494 // Structure for collecting spill information about JSObjects.
1495 class SpillInformation {
1496 public:
1497 void Clear();
1498 void Print();
1499 int number_of_objects_;
1500 int number_of_objects_with_fast_properties_;
1501 int number_of_objects_with_fast_elements_;
1502 int number_of_fast_used_fields_;
1503 int number_of_fast_unused_fields_;
1504 int number_of_slow_used_properties_;
1505 int number_of_slow_unused_properties_;
1506 int number_of_fast_used_elements_;
1507 int number_of_fast_unused_elements_;
1508 int number_of_slow_used_elements_;
1509 int number_of_slow_unused_elements_;
1510 };
1511
1512 void IncrementSpillStatistics(SpillInformation* info);
1513#endif
1514 Object* SlowReverseLookup(Object* value);
1515
Leon Clarkee46be812010-01-19 14:06:41 +00001516 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1517 // Also maximal value of JSArray's length property.
1518 static const uint32_t kMaxElementCount = 0xffffffffu;
1519
Steve Blocka7e24c12009-10-30 11:49:00 +00001520 static const uint32_t kMaxGap = 1024;
1521 static const int kMaxFastElementsLength = 5000;
1522 static const int kInitialMaxFastElementArray = 100000;
1523 static const int kMaxFastProperties = 8;
1524 static const int kMaxInstanceSize = 255 * kPointerSize;
1525 // When extending the backing storage for property values, we increase
1526 // its size by more than the 1 entry necessary, so sequentially adding fields
1527 // to the same object requires fewer allocations and copies.
1528 static const int kFieldsAdded = 3;
1529
1530 // Layout description.
1531 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1532 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1533 static const int kHeaderSize = kElementsOffset + kPointerSize;
1534
1535 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1536
1537 Object* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
1538
1539 private:
1540 Object* SetElementWithInterceptor(uint32_t index, Object* value);
1541 Object* SetElementWithoutInterceptor(uint32_t index, Object* value);
1542
1543 Object* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
1544
1545 Object* DeletePropertyPostInterceptor(String* name, DeleteMode mode);
1546 Object* DeletePropertyWithInterceptor(String* name);
1547
1548 Object* DeleteElementPostInterceptor(uint32_t index, DeleteMode mode);
1549 Object* DeleteElementWithInterceptor(uint32_t index);
1550
1551 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1552 String* name,
1553 bool continue_search);
1554 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1555 String* name,
1556 bool continue_search);
1557 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1558 Object* receiver,
1559 LookupResult* result,
1560 String* name,
1561 bool continue_search);
1562 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1563 LookupResult* result,
1564 String* name,
1565 bool continue_search);
1566
1567 // Returns true if most of the elements backing storage is used.
1568 bool HasDenseElements();
1569
1570 Object* DefineGetterSetter(String* name, PropertyAttributes attributes);
1571
1572 void LookupInDescriptor(String* name, LookupResult* result);
1573
1574 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1575};
1576
1577
1578// Abstract super class arrays. It provides length behavior.
1579class Array: public HeapObject {
1580 public:
1581 // [length]: length of the array.
1582 inline int length();
1583 inline void set_length(int value);
1584
1585 // Convert an object to an array index.
1586 // Returns true if the conversion succeeded.
1587 static inline bool IndexFromObject(Object* object, uint32_t* index);
1588
1589 // Layout descriptor.
1590 static const int kLengthOffset = HeapObject::kHeaderSize;
1591
1592 protected:
1593 // No code should use the Array class directly, only its subclasses.
1594 // Use the kHeaderSize of the appropriate subclass, which may be aligned.
1595 static const int kHeaderSize = kLengthOffset + kIntSize;
1596 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
1597
1598 private:
1599 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
1600};
1601
1602
1603// FixedArray describes fixed sized arrays where element
1604// type is Object*.
1605
1606class FixedArray: public Array {
1607 public:
1608
1609 // Setter and getter for elements.
1610 inline Object* get(int index);
1611 // Setter that uses write barrier.
1612 inline void set(int index, Object* value);
1613
1614 // Setter that doesn't need write barrier).
1615 inline void set(int index, Smi* value);
1616 // Setter with explicit barrier mode.
1617 inline void set(int index, Object* value, WriteBarrierMode mode);
1618
1619 // Setters for frequently used oddballs located in old space.
1620 inline void set_undefined(int index);
1621 inline void set_null(int index);
1622 inline void set_the_hole(int index);
1623
1624 // Copy operations.
1625 inline Object* Copy();
1626 Object* CopySize(int new_length);
1627
1628 // Add the elements of a JSArray to this FixedArray.
1629 Object* AddKeysFromJSArray(JSArray* array);
1630
1631 // Compute the union of this and other.
1632 Object* UnionOfKeys(FixedArray* other);
1633
1634 // Copy a sub array from the receiver to dest.
1635 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1636
1637 // Garbage collection support.
1638 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1639
1640 // Code Generation support.
1641 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1642
1643 // Casting.
1644 static inline FixedArray* cast(Object* obj);
1645
Leon Clarkee46be812010-01-19 14:06:41 +00001646 static const int kHeaderSize = Array::kAlignedSize;
1647
1648 // Maximal allowed size, in bytes, of a single FixedArray.
1649 // Prevents overflowing size computations, as well as extreme memory
1650 // consumption.
1651 static const int kMaxSize = 512 * MB;
1652 // Maximally allowed length of a FixedArray.
1653 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001654
1655 // Dispatched behavior.
1656 int FixedArraySize() { return SizeFor(length()); }
1657 void FixedArrayIterateBody(ObjectVisitor* v);
1658#ifdef DEBUG
1659 void FixedArrayPrint();
1660 void FixedArrayVerify();
1661 // Checks if two FixedArrays have identical contents.
1662 bool IsEqualTo(FixedArray* other);
1663#endif
1664
1665 // Swap two elements in a pair of arrays. If this array and the
1666 // numbers array are the same object, the elements are only swapped
1667 // once.
1668 void SwapPairs(FixedArray* numbers, int i, int j);
1669
1670 // Sort prefix of this array and the numbers array as pairs wrt. the
1671 // numbers. If the numbers array and the this array are the same
1672 // object, the prefix of this array is sorted.
1673 void SortPairs(FixedArray* numbers, uint32_t len);
1674
1675 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001676 // Set operation on FixedArray without using write barriers. Can
1677 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001678 static inline void fast_set(FixedArray* array, int index, Object* value);
1679
1680 private:
1681 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1682};
1683
1684
1685// DescriptorArrays are fixed arrays used to hold instance descriptors.
1686// The format of the these objects is:
1687// [0]: point to a fixed array with (value, detail) pairs.
1688// [1]: next enumeration index (Smi), or pointer to small fixed array:
1689// [0]: next enumeration index (Smi)
1690// [1]: pointer to fixed array with enum cache
1691// [2]: first key
1692// [length() - 1]: last key
1693//
1694class DescriptorArray: public FixedArray {
1695 public:
1696 // Is this the singleton empty_descriptor_array?
1697 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001698
Steve Blocka7e24c12009-10-30 11:49:00 +00001699 // Returns the number of descriptors in the array.
1700 int number_of_descriptors() {
1701 return IsEmpty() ? 0 : length() - kFirstIndex;
1702 }
1703
1704 int NextEnumerationIndex() {
1705 if (IsEmpty()) return PropertyDetails::kInitialIndex;
1706 Object* obj = get(kEnumerationIndexIndex);
1707 if (obj->IsSmi()) {
1708 return Smi::cast(obj)->value();
1709 } else {
1710 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1711 return Smi::cast(index)->value();
1712 }
1713 }
1714
1715 // Set next enumeration index and flush any enum cache.
1716 void SetNextEnumerationIndex(int value) {
1717 if (!IsEmpty()) {
1718 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1719 }
1720 }
1721 bool HasEnumCache() {
1722 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
1723 }
1724
1725 Object* GetEnumCache() {
1726 ASSERT(HasEnumCache());
1727 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1728 return bridge->get(kEnumCacheBridgeCacheIndex);
1729 }
1730
1731 // Initialize or change the enum cache,
1732 // using the supplied storage for the small "bridge".
1733 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1734
1735 // Accessors for fetching instance descriptor at descriptor number.
1736 inline String* GetKey(int descriptor_number);
1737 inline Object* GetValue(int descriptor_number);
1738 inline Smi* GetDetails(int descriptor_number);
1739 inline PropertyType GetType(int descriptor_number);
1740 inline int GetFieldIndex(int descriptor_number);
1741 inline JSFunction* GetConstantFunction(int descriptor_number);
1742 inline Object* GetCallbacksObject(int descriptor_number);
1743 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
1744 inline bool IsProperty(int descriptor_number);
1745 inline bool IsTransition(int descriptor_number);
1746 inline bool IsNullDescriptor(int descriptor_number);
1747 inline bool IsDontEnum(int descriptor_number);
1748
1749 // Accessor for complete descriptor.
1750 inline void Get(int descriptor_number, Descriptor* desc);
1751 inline void Set(int descriptor_number, Descriptor* desc);
1752
1753 // Transfer complete descriptor from another descriptor array to
1754 // this one.
1755 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
1756
1757 // Copy the descriptor array, insert a new descriptor and optionally
1758 // remove map transitions. If the descriptor is already present, it is
1759 // replaced. If a replaced descriptor is a real property (not a transition
1760 // or null), its enumeration index is kept as is.
1761 // If adding a real property, map transitions must be removed. If adding
1762 // a transition, they must not be removed. All null descriptors are removed.
1763 Object* CopyInsert(Descriptor* descriptor, TransitionFlag transition_flag);
1764
1765 // Remove all transitions. Return a copy of the array with all transitions
1766 // removed, or a Failure object if the new array could not be allocated.
1767 Object* RemoveTransitions();
1768
1769 // Sort the instance descriptors by the hash codes of their keys.
1770 void Sort();
1771
1772 // Search the instance descriptors for given name.
1773 inline int Search(String* name);
1774
1775 // Tells whether the name is present int the array.
1776 bool Contains(String* name) { return kNotFound != Search(name); }
1777
1778 // Perform a binary search in the instance descriptors represented
1779 // by this fixed array. low and high are descriptor indices. If there
1780 // are three instance descriptors in this array it should be called
1781 // with low=0 and high=2.
1782 int BinarySearch(String* name, int low, int high);
1783
1784 // Perform a linear search in the instance descriptors represented
1785 // by this fixed array. len is the number of descriptor indices that are
1786 // valid. Does not require the descriptors to be sorted.
1787 int LinearSearch(String* name, int len);
1788
1789 // Allocates a DescriptorArray, but returns the singleton
1790 // empty descriptor array object if number_of_descriptors is 0.
1791 static Object* Allocate(int number_of_descriptors);
1792
1793 // Casting.
1794 static inline DescriptorArray* cast(Object* obj);
1795
1796 // Constant for denoting key was not found.
1797 static const int kNotFound = -1;
1798
1799 static const int kContentArrayIndex = 0;
1800 static const int kEnumerationIndexIndex = 1;
1801 static const int kFirstIndex = 2;
1802
1803 // The length of the "bridge" to the enum cache.
1804 static const int kEnumCacheBridgeLength = 2;
1805 static const int kEnumCacheBridgeEnumIndex = 0;
1806 static const int kEnumCacheBridgeCacheIndex = 1;
1807
1808 // Layout description.
1809 static const int kContentArrayOffset = FixedArray::kHeaderSize;
1810 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
1811 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
1812
1813 // Layout description for the bridge array.
1814 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
1815 static const int kEnumCacheBridgeCacheOffset =
1816 kEnumCacheBridgeEnumOffset + kPointerSize;
1817
1818#ifdef DEBUG
1819 // Print all the descriptors.
1820 void PrintDescriptors();
1821
1822 // Is the descriptor array sorted and without duplicates?
1823 bool IsSortedNoDuplicates();
1824
1825 // Are two DescriptorArrays equal?
1826 bool IsEqualTo(DescriptorArray* other);
1827#endif
1828
1829 // The maximum number of descriptors we want in a descriptor array (should
1830 // fit in a page).
1831 static const int kMaxNumberOfDescriptors = 1024 + 512;
1832
1833 private:
1834 // Conversion from descriptor number to array indices.
1835 static int ToKeyIndex(int descriptor_number) {
1836 return descriptor_number+kFirstIndex;
1837 }
Leon Clarkee46be812010-01-19 14:06:41 +00001838
1839 static int ToDetailsIndex(int descriptor_number) {
1840 return (descriptor_number << 1) + 1;
1841 }
1842
Steve Blocka7e24c12009-10-30 11:49:00 +00001843 static int ToValueIndex(int descriptor_number) {
1844 return descriptor_number << 1;
1845 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001846
1847 bool is_null_descriptor(int descriptor_number) {
1848 return PropertyDetails(GetDetails(descriptor_number)).type() ==
1849 NULL_DESCRIPTOR;
1850 }
1851 // Swap operation on FixedArray without using write barriers.
1852 static inline void fast_swap(FixedArray* array, int first, int second);
1853
1854 // Swap descriptor first and second.
1855 inline void Swap(int first, int second);
1856
1857 FixedArray* GetContentArray() {
1858 return FixedArray::cast(get(kContentArrayIndex));
1859 }
1860 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
1861};
1862
1863
1864// HashTable is a subclass of FixedArray that implements a hash table
1865// that uses open addressing and quadratic probing.
1866//
1867// In order for the quadratic probing to work, elements that have not
1868// yet been used and elements that have been deleted are
1869// distinguished. Probing continues when deleted elements are
1870// encountered and stops when unused elements are encountered.
1871//
1872// - Elements with key == undefined have not been used yet.
1873// - Elements with key == null have been deleted.
1874//
1875// The hash table class is parameterized with a Shape and a Key.
1876// Shape must be a class with the following interface:
1877// class ExampleShape {
1878// public:
1879// // Tells whether key matches other.
1880// static bool IsMatch(Key key, Object* other);
1881// // Returns the hash value for key.
1882// static uint32_t Hash(Key key);
1883// // Returns the hash value for object.
1884// static uint32_t HashForObject(Key key, Object* object);
1885// // Convert key to an object.
1886// static inline Object* AsObject(Key key);
1887// // The prefix size indicates number of elements in the beginning
1888// // of the backing storage.
1889// static const int kPrefixSize = ..;
1890// // The Element size indicates number of elements per entry.
1891// static const int kEntrySize = ..;
1892// };
Steve Block3ce2e202009-11-05 08:53:23 +00001893// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001894// beginning of the backing storage that can be used for non-element
1895// information by subclasses.
1896
1897template<typename Shape, typename Key>
1898class HashTable: public FixedArray {
1899 public:
Steve Block3ce2e202009-11-05 08:53:23 +00001900 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00001901 int NumberOfElements() {
1902 return Smi::cast(get(kNumberOfElementsIndex))->value();
1903 }
1904
Leon Clarkee46be812010-01-19 14:06:41 +00001905 // Returns the number of deleted elements in the hash table.
1906 int NumberOfDeletedElements() {
1907 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
1908 }
1909
Steve Block3ce2e202009-11-05 08:53:23 +00001910 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00001911 int Capacity() {
1912 return Smi::cast(get(kCapacityIndex))->value();
1913 }
1914
1915 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00001916 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00001917 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
1918
1919 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00001920 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00001921 void ElementRemoved() {
1922 SetNumberOfElements(NumberOfElements() - 1);
1923 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
1924 }
1925 void ElementsRemoved(int n) {
1926 SetNumberOfElements(NumberOfElements() - n);
1927 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
1928 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001929
Steve Block3ce2e202009-11-05 08:53:23 +00001930 // Returns a new HashTable object. Might return Failure.
Steve Blocka7e24c12009-10-30 11:49:00 +00001931 static Object* Allocate(int at_least_space_for);
1932
1933 // Returns the key at entry.
1934 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
1935
1936 // Tells whether k is a real key. Null and undefined are not allowed
1937 // as keys and can be used to indicate missing or deleted elements.
1938 bool IsKey(Object* k) {
1939 return !k->IsNull() && !k->IsUndefined();
1940 }
1941
1942 // Garbage collection support.
1943 void IteratePrefix(ObjectVisitor* visitor);
1944 void IterateElements(ObjectVisitor* visitor);
1945
1946 // Casting.
1947 static inline HashTable* cast(Object* obj);
1948
1949 // Compute the probe offset (quadratic probing).
1950 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
1951 return (n + n * n) >> 1;
1952 }
1953
1954 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00001955 static const int kNumberOfDeletedElementsIndex = 1;
1956 static const int kCapacityIndex = 2;
1957 static const int kPrefixStartIndex = 3;
1958 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00001959 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001960 static const int kEntrySize = Shape::kEntrySize;
1961 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00001962 kHeaderSize + kElementsStartIndex * kPointerSize;
1963
1964 // Constant used for denoting a absent entry.
1965 static const int kNotFound = -1;
1966
Leon Clarkee46be812010-01-19 14:06:41 +00001967 // Maximal capacity of HashTable. Based on maximal length of underlying
1968 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
1969 // cannot overflow.
1970 static const int kMaxCapacity =
1971 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
1972
Steve Blocka7e24c12009-10-30 11:49:00 +00001973 // Find entry for key otherwise return -1.
1974 int FindEntry(Key key);
1975
1976 protected:
1977
1978 // Find the entry at which to insert element with the given key that
1979 // has the given hash value.
1980 uint32_t FindInsertionEntry(uint32_t hash);
1981
1982 // Returns the index for an entry (of the key)
1983 static inline int EntryToIndex(int entry) {
1984 return (entry * kEntrySize) + kElementsStartIndex;
1985 }
1986
Steve Block3ce2e202009-11-05 08:53:23 +00001987 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00001988 void SetNumberOfElements(int nof) {
1989 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
1990 }
1991
Leon Clarkee46be812010-01-19 14:06:41 +00001992 // Update the number of deleted elements in the hash table.
1993 void SetNumberOfDeletedElements(int nod) {
1994 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
1995 }
1996
Steve Blocka7e24c12009-10-30 11:49:00 +00001997 // Sets the capacity of the hash table.
1998 void SetCapacity(int capacity) {
1999 // To scale a computed hash code to fit within the hash table, we
2000 // use bit-wise AND with a mask, so the capacity must be positive
2001 // and non-zero.
2002 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002003 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002004 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2005 }
2006
2007
2008 // Returns probe entry.
2009 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2010 ASSERT(IsPowerOf2(size));
2011 return (hash + GetProbeOffset(number)) & (size - 1);
2012 }
2013
Leon Clarkee46be812010-01-19 14:06:41 +00002014 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2015 return hash & (size - 1);
2016 }
2017
2018 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2019 return (last + number) & (size - 1);
2020 }
2021
Steve Blocka7e24c12009-10-30 11:49:00 +00002022 // Ensure enough space for n additional elements.
2023 Object* EnsureCapacity(int n, Key key);
2024};
2025
2026
2027
2028// HashTableKey is an abstract superclass for virtual key behavior.
2029class HashTableKey {
2030 public:
2031 // Returns whether the other object matches this key.
2032 virtual bool IsMatch(Object* other) = 0;
2033 // Returns the hash value for this key.
2034 virtual uint32_t Hash() = 0;
2035 // Returns the hash value for object.
2036 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002037 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002038 // If allocations fails a failure object is returned.
2039 virtual Object* AsObject() = 0;
2040 // Required.
2041 virtual ~HashTableKey() {}
2042};
2043
2044class SymbolTableShape {
2045 public:
2046 static bool IsMatch(HashTableKey* key, Object* value) {
2047 return key->IsMatch(value);
2048 }
2049 static uint32_t Hash(HashTableKey* key) {
2050 return key->Hash();
2051 }
2052 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2053 return key->HashForObject(object);
2054 }
2055 static Object* AsObject(HashTableKey* key) {
2056 return key->AsObject();
2057 }
2058
2059 static const int kPrefixSize = 0;
2060 static const int kEntrySize = 1;
2061};
2062
2063// SymbolTable.
2064//
2065// No special elements in the prefix and the element size is 1
2066// because only the symbol itself (the key) needs to be stored.
2067class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2068 public:
2069 // Find symbol in the symbol table. If it is not there yet, it is
2070 // added. The return value is the symbol table which might have
2071 // been enlarged. If the return value is not a failure, the symbol
2072 // pointer *s is set to the symbol found.
2073 Object* LookupSymbol(Vector<const char> str, Object** s);
2074 Object* LookupString(String* key, Object** s);
2075
2076 // Looks up a symbol that is equal to the given string and returns
2077 // true if it is found, assigning the symbol to the given output
2078 // parameter.
2079 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002080 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002081
2082 // Casting.
2083 static inline SymbolTable* cast(Object* obj);
2084
2085 private:
2086 Object* LookupKey(HashTableKey* key, Object** s);
2087
2088 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2089};
2090
2091
2092class MapCacheShape {
2093 public:
2094 static bool IsMatch(HashTableKey* key, Object* value) {
2095 return key->IsMatch(value);
2096 }
2097 static uint32_t Hash(HashTableKey* key) {
2098 return key->Hash();
2099 }
2100
2101 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2102 return key->HashForObject(object);
2103 }
2104
2105 static Object* AsObject(HashTableKey* key) {
2106 return key->AsObject();
2107 }
2108
2109 static const int kPrefixSize = 0;
2110 static const int kEntrySize = 2;
2111};
2112
2113
2114// MapCache.
2115//
2116// Maps keys that are a fixed array of symbols to a map.
2117// Used for canonicalize maps for object literals.
2118class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2119 public:
2120 // Find cached value for a string key, otherwise return null.
2121 Object* Lookup(FixedArray* key);
2122 Object* Put(FixedArray* key, Map* value);
2123 static inline MapCache* cast(Object* obj);
2124
2125 private:
2126 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2127};
2128
2129
2130template <typename Shape, typename Key>
2131class Dictionary: public HashTable<Shape, Key> {
2132 public:
2133
2134 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2135 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2136 }
2137
2138 // Returns the value at entry.
2139 Object* ValueAt(int entry) {
2140 return get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
2141 }
2142
2143 // Set the value for entry.
2144 void ValueAtPut(int entry, Object* value) {
2145 set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value);
2146 }
2147
2148 // Returns the property details for the property at entry.
2149 PropertyDetails DetailsAt(int entry) {
2150 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2151 return PropertyDetails(
2152 Smi::cast(get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
2153 }
2154
2155 // Set the details for entry.
2156 void DetailsAtPut(int entry, PropertyDetails value) {
2157 set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
2158 }
2159
2160 // Sorting support
2161 void CopyValuesTo(FixedArray* elements);
2162
2163 // Delete a property from the dictionary.
2164 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2165
2166 // Returns the number of elements in the dictionary filtering out properties
2167 // with the specified attributes.
2168 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2169
2170 // Returns the number of enumerable elements in the dictionary.
2171 int NumberOfEnumElements();
2172
2173 // Copies keys to preallocated fixed array.
2174 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2175 // Fill in details for properties into storage.
2176 void CopyKeysTo(FixedArray* storage);
2177
2178 // Accessors for next enumeration index.
2179 void SetNextEnumerationIndex(int index) {
2180 fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
2181 }
2182
2183 int NextEnumerationIndex() {
2184 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2185 }
2186
2187 // Returns a new array for dictionary usage. Might return Failure.
2188 static Object* Allocate(int at_least_space_for);
2189
2190 // Ensure enough space for n additional elements.
2191 Object* EnsureCapacity(int n, Key key);
2192
2193#ifdef DEBUG
2194 void Print();
2195#endif
2196 // Returns the key (slow).
2197 Object* SlowReverseLookup(Object* value);
2198
2199 // Sets the entry to (key, value) pair.
2200 inline void SetEntry(int entry,
2201 Object* key,
2202 Object* value,
2203 PropertyDetails details);
2204
2205 Object* Add(Key key, Object* value, PropertyDetails details);
2206
2207 protected:
2208 // Generic at put operation.
2209 Object* AtPut(Key key, Object* value);
2210
2211 // Add entry to dictionary.
2212 Object* AddEntry(Key key,
2213 Object* value,
2214 PropertyDetails details,
2215 uint32_t hash);
2216
2217 // Generate new enumeration indices to avoid enumeration index overflow.
2218 Object* GenerateNewEnumerationIndices();
2219 static const int kMaxNumberKeyIndex =
2220 HashTable<Shape, Key>::kPrefixStartIndex;
2221 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2222};
2223
2224
2225class StringDictionaryShape {
2226 public:
2227 static inline bool IsMatch(String* key, Object* other);
2228 static inline uint32_t Hash(String* key);
2229 static inline uint32_t HashForObject(String* key, Object* object);
2230 static inline Object* AsObject(String* key);
2231 static const int kPrefixSize = 2;
2232 static const int kEntrySize = 3;
2233 static const bool kIsEnumerable = true;
2234};
2235
2236
2237class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2238 public:
2239 static inline StringDictionary* cast(Object* obj) {
2240 ASSERT(obj->IsDictionary());
2241 return reinterpret_cast<StringDictionary*>(obj);
2242 }
2243
2244 // Copies enumerable keys to preallocated fixed array.
2245 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2246
2247 // For transforming properties of a JSObject.
2248 Object* TransformPropertiesToFastFor(JSObject* obj,
2249 int unused_property_fields);
2250};
2251
2252
2253class NumberDictionaryShape {
2254 public:
2255 static inline bool IsMatch(uint32_t key, Object* other);
2256 static inline uint32_t Hash(uint32_t key);
2257 static inline uint32_t HashForObject(uint32_t key, Object* object);
2258 static inline Object* AsObject(uint32_t key);
2259 static const int kPrefixSize = 2;
2260 static const int kEntrySize = 3;
2261 static const bool kIsEnumerable = false;
2262};
2263
2264
2265class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2266 public:
2267 static NumberDictionary* cast(Object* obj) {
2268 ASSERT(obj->IsDictionary());
2269 return reinterpret_cast<NumberDictionary*>(obj);
2270 }
2271
2272 // Type specific at put (default NONE attributes is used when adding).
2273 Object* AtNumberPut(uint32_t key, Object* value);
2274 Object* AddNumberEntry(uint32_t key,
2275 Object* value,
2276 PropertyDetails details);
2277
2278 // Set an existing entry or add a new one if needed.
2279 Object* Set(uint32_t key, Object* value, PropertyDetails details);
2280
2281 void UpdateMaxNumberKey(uint32_t key);
2282
2283 // If slow elements are required we will never go back to fast-case
2284 // for the elements kept in this dictionary. We require slow
2285 // elements if an element has been added at an index larger than
2286 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2287 // when defining a getter or setter with a number key.
2288 inline bool requires_slow_elements();
2289 inline void set_requires_slow_elements();
2290
2291 // Get the value of the max number key that has been added to this
2292 // dictionary. max_number_key can only be called if
2293 // requires_slow_elements returns false.
2294 inline uint32_t max_number_key();
2295
2296 // Remove all entries were key is a number and (from <= key && key < to).
2297 void RemoveNumberEntries(uint32_t from, uint32_t to);
2298
2299 // Bit masks.
2300 static const int kRequiresSlowElementsMask = 1;
2301 static const int kRequiresSlowElementsTagSize = 1;
2302 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2303};
2304
2305
2306// ByteArray represents fixed sized byte arrays. Used by the outside world,
2307// such as PCRE, and also by the memory allocator and garbage collector to
2308// fill in free blocks in the heap.
2309class ByteArray: public Array {
2310 public:
2311 // Setter and getter.
2312 inline byte get(int index);
2313 inline void set(int index, byte value);
2314
2315 // Treat contents as an int array.
2316 inline int get_int(int index);
2317
2318 static int SizeFor(int length) {
2319 return OBJECT_SIZE_ALIGN(kHeaderSize + length);
2320 }
2321 // We use byte arrays for free blocks in the heap. Given a desired size in
2322 // bytes that is a multiple of the word size and big enough to hold a byte
2323 // array, this function returns the number of elements a byte array should
2324 // have.
2325 static int LengthFor(int size_in_bytes) {
2326 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2327 ASSERT(size_in_bytes >= kHeaderSize);
2328 return size_in_bytes - kHeaderSize;
2329 }
2330
2331 // Returns data start address.
2332 inline Address GetDataStartAddress();
2333
2334 // Returns a pointer to the ByteArray object for a given data start address.
2335 static inline ByteArray* FromDataStartAddress(Address address);
2336
2337 // Casting.
2338 static inline ByteArray* cast(Object* obj);
2339
2340 // Dispatched behavior.
2341 int ByteArraySize() { return SizeFor(length()); }
2342#ifdef DEBUG
2343 void ByteArrayPrint();
2344 void ByteArrayVerify();
2345#endif
2346
2347 // ByteArray headers are not quadword aligned.
2348 static const int kHeaderSize = Array::kHeaderSize;
2349 static const int kAlignedSize = Array::kAlignedSize;
2350
Leon Clarkee46be812010-01-19 14:06:41 +00002351 // Maximal memory consumption for a single ByteArray.
2352 static const int kMaxSize = 512 * MB;
2353 // Maximal length of a single ByteArray.
2354 static const int kMaxLength = kMaxSize - kHeaderSize;
2355
Steve Blocka7e24c12009-10-30 11:49:00 +00002356 private:
2357 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2358};
2359
2360
2361// A PixelArray represents a fixed-size byte array with special semantics
2362// used for implementing the CanvasPixelArray object. Please see the
2363// specification at:
2364// http://www.whatwg.org/specs/web-apps/current-work/
2365// multipage/the-canvas-element.html#canvaspixelarray
2366// In particular, write access clamps the value written to 0 or 255 if the
2367// value written is outside this range.
2368class PixelArray: public Array {
2369 public:
2370 // [external_pointer]: The pointer to the external memory area backing this
2371 // pixel array.
2372 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2373
2374 // Setter and getter.
2375 inline uint8_t get(int index);
2376 inline void set(int index, uint8_t value);
2377
2378 // This accessor applies the correct conversion from Smi, HeapNumber and
2379 // undefined and clamps the converted value between 0 and 255.
2380 Object* SetValue(uint32_t index, Object* value);
2381
2382 // Casting.
2383 static inline PixelArray* cast(Object* obj);
2384
2385#ifdef DEBUG
2386 void PixelArrayPrint();
2387 void PixelArrayVerify();
2388#endif // DEBUG
2389
Steve Block3ce2e202009-11-05 08:53:23 +00002390 // Maximal acceptable length for a pixel array.
2391 static const int kMaxLength = 0x3fffffff;
2392
Steve Blocka7e24c12009-10-30 11:49:00 +00002393 // PixelArray headers are not quadword aligned.
2394 static const int kExternalPointerOffset = Array::kAlignedSize;
2395 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
2396 static const int kAlignedSize = OBJECT_SIZE_ALIGN(kHeaderSize);
2397
2398 private:
2399 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2400};
2401
2402
Steve Block3ce2e202009-11-05 08:53:23 +00002403// An ExternalArray represents a fixed-size array of primitive values
2404// which live outside the JavaScript heap. Its subclasses are used to
2405// implement the CanvasArray types being defined in the WebGL
2406// specification. As of this writing the first public draft is not yet
2407// available, but Khronos members can access the draft at:
2408// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2409//
2410// The semantics of these arrays differ from CanvasPixelArray.
2411// Out-of-range values passed to the setter are converted via a C
2412// cast, not clamping. Out-of-range indices cause exceptions to be
2413// raised rather than being silently ignored.
2414class ExternalArray: public Array {
2415 public:
2416 // [external_pointer]: The pointer to the external memory area backing this
2417 // external array.
2418 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2419
2420 // Casting.
2421 static inline ExternalArray* cast(Object* obj);
2422
2423 // Maximal acceptable length for an external array.
2424 static const int kMaxLength = 0x3fffffff;
2425
2426 // ExternalArray headers are not quadword aligned.
2427 static const int kExternalPointerOffset = Array::kAlignedSize;
2428 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
2429 static const int kAlignedSize = OBJECT_SIZE_ALIGN(kHeaderSize);
2430
2431 private:
2432 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2433};
2434
2435
2436class ExternalByteArray: public ExternalArray {
2437 public:
2438 // Setter and getter.
2439 inline int8_t get(int index);
2440 inline void set(int index, int8_t value);
2441
2442 // This accessor applies the correct conversion from Smi, HeapNumber
2443 // and undefined.
2444 Object* SetValue(uint32_t index, Object* value);
2445
2446 // Casting.
2447 static inline ExternalByteArray* cast(Object* obj);
2448
2449#ifdef DEBUG
2450 void ExternalByteArrayPrint();
2451 void ExternalByteArrayVerify();
2452#endif // DEBUG
2453
2454 private:
2455 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2456};
2457
2458
2459class ExternalUnsignedByteArray: public ExternalArray {
2460 public:
2461 // Setter and getter.
2462 inline uint8_t get(int index);
2463 inline void set(int index, uint8_t value);
2464
2465 // This accessor applies the correct conversion from Smi, HeapNumber
2466 // and undefined.
2467 Object* SetValue(uint32_t index, Object* value);
2468
2469 // Casting.
2470 static inline ExternalUnsignedByteArray* cast(Object* obj);
2471
2472#ifdef DEBUG
2473 void ExternalUnsignedByteArrayPrint();
2474 void ExternalUnsignedByteArrayVerify();
2475#endif // DEBUG
2476
2477 private:
2478 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2479};
2480
2481
2482class ExternalShortArray: public ExternalArray {
2483 public:
2484 // Setter and getter.
2485 inline int16_t get(int index);
2486 inline void set(int index, int16_t value);
2487
2488 // This accessor applies the correct conversion from Smi, HeapNumber
2489 // and undefined.
2490 Object* SetValue(uint32_t index, Object* value);
2491
2492 // Casting.
2493 static inline ExternalShortArray* cast(Object* obj);
2494
2495#ifdef DEBUG
2496 void ExternalShortArrayPrint();
2497 void ExternalShortArrayVerify();
2498#endif // DEBUG
2499
2500 private:
2501 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2502};
2503
2504
2505class ExternalUnsignedShortArray: public ExternalArray {
2506 public:
2507 // Setter and getter.
2508 inline uint16_t get(int index);
2509 inline void set(int index, uint16_t value);
2510
2511 // This accessor applies the correct conversion from Smi, HeapNumber
2512 // and undefined.
2513 Object* SetValue(uint32_t index, Object* value);
2514
2515 // Casting.
2516 static inline ExternalUnsignedShortArray* cast(Object* obj);
2517
2518#ifdef DEBUG
2519 void ExternalUnsignedShortArrayPrint();
2520 void ExternalUnsignedShortArrayVerify();
2521#endif // DEBUG
2522
2523 private:
2524 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2525};
2526
2527
2528class ExternalIntArray: public ExternalArray {
2529 public:
2530 // Setter and getter.
2531 inline int32_t get(int index);
2532 inline void set(int index, int32_t value);
2533
2534 // This accessor applies the correct conversion from Smi, HeapNumber
2535 // and undefined.
2536 Object* SetValue(uint32_t index, Object* value);
2537
2538 // Casting.
2539 static inline ExternalIntArray* cast(Object* obj);
2540
2541#ifdef DEBUG
2542 void ExternalIntArrayPrint();
2543 void ExternalIntArrayVerify();
2544#endif // DEBUG
2545
2546 private:
2547 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
2548};
2549
2550
2551class ExternalUnsignedIntArray: public ExternalArray {
2552 public:
2553 // Setter and getter.
2554 inline uint32_t get(int index);
2555 inline void set(int index, uint32_t value);
2556
2557 // This accessor applies the correct conversion from Smi, HeapNumber
2558 // and undefined.
2559 Object* SetValue(uint32_t index, Object* value);
2560
2561 // Casting.
2562 static inline ExternalUnsignedIntArray* cast(Object* obj);
2563
2564#ifdef DEBUG
2565 void ExternalUnsignedIntArrayPrint();
2566 void ExternalUnsignedIntArrayVerify();
2567#endif // DEBUG
2568
2569 private:
2570 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
2571};
2572
2573
2574class ExternalFloatArray: public ExternalArray {
2575 public:
2576 // Setter and getter.
2577 inline float get(int index);
2578 inline void set(int index, float value);
2579
2580 // This accessor applies the correct conversion from Smi, HeapNumber
2581 // and undefined.
2582 Object* SetValue(uint32_t index, Object* value);
2583
2584 // Casting.
2585 static inline ExternalFloatArray* cast(Object* obj);
2586
2587#ifdef DEBUG
2588 void ExternalFloatArrayPrint();
2589 void ExternalFloatArrayVerify();
2590#endif // DEBUG
2591
2592 private:
2593 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
2594};
2595
2596
Steve Blocka7e24c12009-10-30 11:49:00 +00002597// Code describes objects with on-the-fly generated machine code.
2598class Code: public HeapObject {
2599 public:
2600 // Opaque data type for encapsulating code flags like kind, inline
2601 // cache state, and arguments count.
2602 enum Flags { };
2603
2604 enum Kind {
2605 FUNCTION,
2606 STUB,
2607 BUILTIN,
2608 LOAD_IC,
2609 KEYED_LOAD_IC,
2610 CALL_IC,
2611 STORE_IC,
2612 KEYED_STORE_IC,
2613 // No more than eight kinds. The value currently encoded in three bits in
2614 // Flags.
2615
2616 // Pseudo-kinds.
2617 REGEXP = BUILTIN,
2618 FIRST_IC_KIND = LOAD_IC,
2619 LAST_IC_KIND = KEYED_STORE_IC
2620 };
2621
2622 enum {
2623 NUMBER_OF_KINDS = KEYED_STORE_IC + 1
2624 };
2625
2626#ifdef ENABLE_DISASSEMBLER
2627 // Printing
2628 static const char* Kind2String(Kind kind);
2629 static const char* ICState2String(InlineCacheState state);
2630 static const char* PropertyType2String(PropertyType type);
2631 void Disassemble(const char* name);
2632#endif // ENABLE_DISASSEMBLER
2633
2634 // [instruction_size]: Size of the native instructions
2635 inline int instruction_size();
2636 inline void set_instruction_size(int value);
2637
2638 // [relocation_size]: Size of relocation information.
2639 inline int relocation_size();
2640 inline void set_relocation_size(int value);
2641
2642 // [sinfo_size]: Size of scope information.
2643 inline int sinfo_size();
2644 inline void set_sinfo_size(int value);
2645
2646 // [flags]: Various code flags.
2647 inline Flags flags();
2648 inline void set_flags(Flags flags);
2649
2650 // [flags]: Access to specific code flags.
2651 inline Kind kind();
2652 inline InlineCacheState ic_state(); // Only valid for IC stubs.
2653 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
2654 inline PropertyType type(); // Only valid for monomorphic IC stubs.
2655 inline int arguments_count(); // Only valid for call IC stubs.
2656
2657 // Testers for IC stub kinds.
2658 inline bool is_inline_cache_stub();
2659 inline bool is_load_stub() { return kind() == LOAD_IC; }
2660 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
2661 inline bool is_store_stub() { return kind() == STORE_IC; }
2662 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
2663 inline bool is_call_stub() { return kind() == CALL_IC; }
2664
2665 // [major_key]: For kind STUB, the major key.
2666 inline CodeStub::Major major_key();
2667 inline void set_major_key(CodeStub::Major major);
2668
2669 // Flags operations.
2670 static inline Flags ComputeFlags(Kind kind,
2671 InLoopFlag in_loop = NOT_IN_LOOP,
2672 InlineCacheState ic_state = UNINITIALIZED,
2673 PropertyType type = NORMAL,
2674 int argc = -1);
2675
2676 static inline Flags ComputeMonomorphicFlags(
2677 Kind kind,
2678 PropertyType type,
2679 InLoopFlag in_loop = NOT_IN_LOOP,
2680 int argc = -1);
2681
2682 static inline Kind ExtractKindFromFlags(Flags flags);
2683 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
2684 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
2685 static inline PropertyType ExtractTypeFromFlags(Flags flags);
2686 static inline int ExtractArgumentsCountFromFlags(Flags flags);
2687 static inline Flags RemoveTypeFromFlags(Flags flags);
2688
2689 // Convert a target address into a code object.
2690 static inline Code* GetCodeFromTargetAddress(Address address);
2691
2692 // Returns the address of the first instruction.
2693 inline byte* instruction_start();
2694
2695 // Returns the size of the instructions, padding, and relocation information.
2696 inline int body_size();
2697
2698 // Returns the address of the first relocation info (read backwards!).
2699 inline byte* relocation_start();
2700
2701 // Code entry point.
2702 inline byte* entry();
2703
2704 // Returns true if pc is inside this object's instructions.
2705 inline bool contains(byte* pc);
2706
2707 // Returns the address of the scope information.
2708 inline byte* sinfo_start();
2709
2710 // Relocate the code by delta bytes. Called to signal that this code
2711 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002712 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002713
2714 // Migrate code described by desc.
2715 void CopyFrom(const CodeDesc& desc);
2716
2717 // Returns the object size for a given body and sinfo size (Used for
2718 // allocation).
2719 static int SizeFor(int body_size, int sinfo_size) {
2720 ASSERT_SIZE_TAG_ALIGNED(body_size);
2721 ASSERT_SIZE_TAG_ALIGNED(sinfo_size);
2722 return RoundUp(kHeaderSize + body_size + sinfo_size, kCodeAlignment);
2723 }
2724
2725 // Calculate the size of the code object to report for log events. This takes
2726 // the layout of the code object into account.
2727 int ExecutableSize() {
2728 // Check that the assumptions about the layout of the code object holds.
2729 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
2730 Code::kHeaderSize);
2731 return instruction_size() + Code::kHeaderSize;
2732 }
2733
2734 // Locating source position.
2735 int SourcePosition(Address pc);
2736 int SourceStatementPosition(Address pc);
2737
2738 // Casting.
2739 static inline Code* cast(Object* obj);
2740
2741 // Dispatched behavior.
2742 int CodeSize() { return SizeFor(body_size(), sinfo_size()); }
2743 void CodeIterateBody(ObjectVisitor* v);
2744#ifdef DEBUG
2745 void CodePrint();
2746 void CodeVerify();
2747#endif
2748 // Code entry points are aligned to 32 bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002749 static const int kCodeAlignmentBits = 5;
2750 static const int kCodeAlignment = 1 << kCodeAlignmentBits;
Steve Blocka7e24c12009-10-30 11:49:00 +00002751 static const int kCodeAlignmentMask = kCodeAlignment - 1;
2752
2753 // Layout description.
2754 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
2755 static const int kRelocationSizeOffset = kInstructionSizeOffset + kIntSize;
2756 static const int kSInfoSizeOffset = kRelocationSizeOffset + kIntSize;
2757 static const int kFlagsOffset = kSInfoSizeOffset + kIntSize;
2758 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
2759 // Add padding to align the instruction start following right after
2760 // the Code object header.
2761 static const int kHeaderSize =
2762 (kKindSpecificFlagsOffset + kIntSize + kCodeAlignmentMask) &
2763 ~kCodeAlignmentMask;
2764
2765 // Byte offsets within kKindSpecificFlagsOffset.
2766 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset + 1;
2767
2768 // Flags layout.
2769 static const int kFlagsICStateShift = 0;
2770 static const int kFlagsICInLoopShift = 3;
2771 static const int kFlagsKindShift = 4;
2772 static const int kFlagsTypeShift = 7;
2773 static const int kFlagsArgumentsCountShift = 10;
2774
2775 static const int kFlagsICStateMask = 0x00000007; // 0000000111
2776 static const int kFlagsICInLoopMask = 0x00000008; // 0000001000
2777 static const int kFlagsKindMask = 0x00000070; // 0001110000
2778 static const int kFlagsTypeMask = 0x00000380; // 1110000000
2779 static const int kFlagsArgumentsCountMask = 0xFFFFFC00;
2780
2781 static const int kFlagsNotUsedInLookup =
2782 (kFlagsICInLoopMask | kFlagsTypeMask);
2783
2784 private:
2785 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
2786};
2787
2788
2789// All heap objects have a Map that describes their structure.
2790// A Map contains information about:
2791// - Size information about the object
2792// - How to iterate over an object (for garbage collection)
2793class Map: public HeapObject {
2794 public:
2795 // Instance size.
2796 inline int instance_size();
2797 inline void set_instance_size(int value);
2798
2799 // Count of properties allocated in the object.
2800 inline int inobject_properties();
2801 inline void set_inobject_properties(int value);
2802
2803 // Count of property fields pre-allocated in the object when first allocated.
2804 inline int pre_allocated_property_fields();
2805 inline void set_pre_allocated_property_fields(int value);
2806
2807 // Instance type.
2808 inline InstanceType instance_type();
2809 inline void set_instance_type(InstanceType value);
2810
2811 // Tells how many unused property fields are available in the
2812 // instance (only used for JSObject in fast mode).
2813 inline int unused_property_fields();
2814 inline void set_unused_property_fields(int value);
2815
2816 // Bit field.
2817 inline byte bit_field();
2818 inline void set_bit_field(byte value);
2819
2820 // Bit field 2.
2821 inline byte bit_field2();
2822 inline void set_bit_field2(byte value);
2823
2824 // Tells whether the object in the prototype property will be used
2825 // for instances created from this function. If the prototype
2826 // property is set to a value that is not a JSObject, the prototype
2827 // property will not be used to create instances of the function.
2828 // See ECMA-262, 13.2.2.
2829 inline void set_non_instance_prototype(bool value);
2830 inline bool has_non_instance_prototype();
2831
2832 // Tells whether the instance with this map should be ignored by the
2833 // __proto__ accessor.
2834 inline void set_is_hidden_prototype() {
2835 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
2836 }
2837
2838 inline bool is_hidden_prototype() {
2839 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
2840 }
2841
2842 // Records and queries whether the instance has a named interceptor.
2843 inline void set_has_named_interceptor() {
2844 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
2845 }
2846
2847 inline bool has_named_interceptor() {
2848 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
2849 }
2850
2851 // Records and queries whether the instance has an indexed interceptor.
2852 inline void set_has_indexed_interceptor() {
2853 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
2854 }
2855
2856 inline bool has_indexed_interceptor() {
2857 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
2858 }
2859
2860 // Tells whether the instance is undetectable.
2861 // An undetectable object is a special class of JSObject: 'typeof' operator
2862 // returns undefined, ToBoolean returns false. Otherwise it behaves like
2863 // a normal JS object. It is useful for implementing undetectable
2864 // document.all in Firefox & Safari.
2865 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
2866 inline void set_is_undetectable() {
2867 set_bit_field(bit_field() | (1 << kIsUndetectable));
2868 }
2869
2870 inline bool is_undetectable() {
2871 return ((1 << kIsUndetectable) & bit_field()) != 0;
2872 }
2873
2874 inline void set_needs_loading(bool value) {
2875 if (value) {
2876 set_bit_field2(bit_field2() | (1 << kNeedsLoading));
2877 } else {
2878 set_bit_field2(bit_field2() & ~(1 << kNeedsLoading));
2879 }
2880 }
2881
2882 // Does this object or function require a lazily loaded script to be
2883 // run before being used?
2884 inline bool needs_loading() {
2885 return ((1 << kNeedsLoading) & bit_field2()) != 0;
2886 }
2887
2888 // Tells whether the instance has a call-as-function handler.
2889 inline void set_has_instance_call_handler() {
2890 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
2891 }
2892
2893 inline bool has_instance_call_handler() {
2894 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
2895 }
2896
Leon Clarkee46be812010-01-19 14:06:41 +00002897 inline void set_is_extensible() {
2898 set_bit_field2(bit_field2() | (1 << kIsExtensible));
2899 }
2900
2901 inline bool is_extensible() {
2902 return ((1 << kIsExtensible) & bit_field2()) != 0;
2903 }
2904
Steve Blocka7e24c12009-10-30 11:49:00 +00002905 // Tells whether the instance needs security checks when accessing its
2906 // properties.
2907 inline void set_is_access_check_needed(bool access_check_needed);
2908 inline bool is_access_check_needed();
2909
2910 // [prototype]: implicit prototype object.
2911 DECL_ACCESSORS(prototype, Object)
2912
2913 // [constructor]: points back to the function responsible for this map.
2914 DECL_ACCESSORS(constructor, Object)
2915
2916 // [instance descriptors]: describes the object.
2917 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
2918
2919 // [stub cache]: contains stubs compiled for this map.
2920 DECL_ACCESSORS(code_cache, FixedArray)
2921
Steve Blocka7e24c12009-10-30 11:49:00 +00002922 Object* CopyDropDescriptors();
2923
2924 // Returns a copy of the map, with all transitions dropped from the
2925 // instance descriptors.
2926 Object* CopyDropTransitions();
2927
2928 // Returns the property index for name (only valid for FAST MODE).
2929 int PropertyIndexFor(String* name);
2930
2931 // Returns the next free property index (only valid for FAST MODE).
2932 int NextFreePropertyIndex();
2933
2934 // Returns the number of properties described in instance_descriptors.
2935 int NumberOfDescribedProperties();
2936
2937 // Casting.
2938 static inline Map* cast(Object* obj);
2939
2940 // Locate an accessor in the instance descriptor.
2941 AccessorDescriptor* FindAccessor(String* name);
2942
2943 // Code cache operations.
2944
2945 // Clears the code cache.
2946 inline void ClearCodeCache();
2947
2948 // Update code cache.
2949 Object* UpdateCodeCache(String* name, Code* code);
2950
2951 // Returns the found code or undefined if absent.
2952 Object* FindInCodeCache(String* name, Code::Flags flags);
2953
2954 // Returns the non-negative index of the code object if it is in the
2955 // cache and -1 otherwise.
2956 int IndexInCodeCache(Code* code);
2957
2958 // Removes a code object from the code cache at the given index.
2959 void RemoveFromCodeCache(int index);
2960
2961 // For every transition in this map, makes the transition's
2962 // target's prototype pointer point back to this map.
2963 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
2964 void CreateBackPointers();
2965
2966 // Set all map transitions from this map to dead maps to null.
2967 // Also, restore the original prototype on the targets of these
2968 // transitions, so that we do not process this map again while
2969 // following back pointers.
2970 void ClearNonLiveTransitions(Object* real_prototype);
2971
2972 // Dispatched behavior.
2973 void MapIterateBody(ObjectVisitor* v);
2974#ifdef DEBUG
2975 void MapPrint();
2976 void MapVerify();
2977#endif
2978
2979 static const int kMaxPreAllocatedPropertyFields = 255;
2980
2981 // Layout description.
2982 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
2983 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
2984 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
2985 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
2986 static const int kInstanceDescriptorsOffset =
2987 kConstructorOffset + kPointerSize;
2988 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002989 static const int kPadStart = kCodeCacheOffset + kPointerSize;
2990 static const int kSize = MAP_SIZE_ALIGN(kPadStart);
Steve Blocka7e24c12009-10-30 11:49:00 +00002991
2992 // Byte offsets within kInstanceSizesOffset.
2993 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
2994 static const int kInObjectPropertiesByte = 1;
2995 static const int kInObjectPropertiesOffset =
2996 kInstanceSizesOffset + kInObjectPropertiesByte;
2997 static const int kPreAllocatedPropertyFieldsByte = 2;
2998 static const int kPreAllocatedPropertyFieldsOffset =
2999 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
3000 // The byte at position 3 is not in use at the moment.
3001
3002 // Byte offsets within kInstanceAttributesOffset attributes.
3003 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3004 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3005 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3006 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3007
3008 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3009
3010 // Bit positions for bit field.
3011 static const int kUnused = 0; // To be used for marking recently used maps.
3012 static const int kHasNonInstancePrototype = 1;
3013 static const int kIsHiddenPrototype = 2;
3014 static const int kHasNamedInterceptor = 3;
3015 static const int kHasIndexedInterceptor = 4;
3016 static const int kIsUndetectable = 5;
3017 static const int kHasInstanceCallHandler = 6;
3018 static const int kIsAccessCheckNeeded = 7;
3019
3020 // Bit positions for bit field 2
3021 static const int kNeedsLoading = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003022 static const int kIsExtensible = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003023
3024 private:
3025 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3026};
3027
3028
3029// An abstract superclass, a marker class really, for simple structure classes.
3030// It doesn't carry much functionality but allows struct classes to me
3031// identified in the type system.
3032class Struct: public HeapObject {
3033 public:
3034 inline void InitializeBody(int object_size);
3035 static inline Struct* cast(Object* that);
3036};
3037
3038
3039// Script describes a script which has been added to the VM.
3040class Script: public Struct {
3041 public:
3042 // Script types.
3043 enum Type {
3044 TYPE_NATIVE = 0,
3045 TYPE_EXTENSION = 1,
3046 TYPE_NORMAL = 2
3047 };
3048
3049 // Script compilation types.
3050 enum CompilationType {
3051 COMPILATION_TYPE_HOST = 0,
3052 COMPILATION_TYPE_EVAL = 1,
3053 COMPILATION_TYPE_JSON = 2
3054 };
3055
3056 // [source]: the script source.
3057 DECL_ACCESSORS(source, Object)
3058
3059 // [name]: the script name.
3060 DECL_ACCESSORS(name, Object)
3061
3062 // [id]: the script id.
3063 DECL_ACCESSORS(id, Object)
3064
3065 // [line_offset]: script line offset in resource from where it was extracted.
3066 DECL_ACCESSORS(line_offset, Smi)
3067
3068 // [column_offset]: script column offset in resource from where it was
3069 // extracted.
3070 DECL_ACCESSORS(column_offset, Smi)
3071
3072 // [data]: additional data associated with this script.
3073 DECL_ACCESSORS(data, Object)
3074
3075 // [context_data]: context data for the context this script was compiled in.
3076 DECL_ACCESSORS(context_data, Object)
3077
3078 // [wrapper]: the wrapper cache.
3079 DECL_ACCESSORS(wrapper, Proxy)
3080
3081 // [type]: the script type.
3082 DECL_ACCESSORS(type, Smi)
3083
3084 // [compilation]: how the the script was compiled.
3085 DECL_ACCESSORS(compilation_type, Smi)
3086
Steve Blockd0582a62009-12-15 09:54:21 +00003087 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003088 DECL_ACCESSORS(line_ends, Object)
3089
Steve Blockd0582a62009-12-15 09:54:21 +00003090 // [eval_from_shared]: for eval scripts the shared funcion info for the
3091 // function from which eval was called.
3092 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003093
3094 // [eval_from_instructions_offset]: the instruction offset in the code for the
3095 // function from which eval was called where eval was called.
3096 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3097
3098 static inline Script* cast(Object* obj);
3099
Steve Block3ce2e202009-11-05 08:53:23 +00003100 // If script source is an external string, check that the underlying
3101 // resource is accessible. Otherwise, always return true.
3102 inline bool HasValidSource();
3103
Steve Blocka7e24c12009-10-30 11:49:00 +00003104#ifdef DEBUG
3105 void ScriptPrint();
3106 void ScriptVerify();
3107#endif
3108
3109 static const int kSourceOffset = HeapObject::kHeaderSize;
3110 static const int kNameOffset = kSourceOffset + kPointerSize;
3111 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3112 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3113 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3114 static const int kContextOffset = kDataOffset + kPointerSize;
3115 static const int kWrapperOffset = kContextOffset + kPointerSize;
3116 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3117 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3118 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3119 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003120 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003121 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003122 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003123 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3124
3125 private:
3126 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3127};
3128
3129
3130// SharedFunctionInfo describes the JSFunction information that can be
3131// shared by multiple instances of the function.
3132class SharedFunctionInfo: public HeapObject {
3133 public:
3134 // [name]: Function name.
3135 DECL_ACCESSORS(name, Object)
3136
3137 // [code]: Function code.
3138 DECL_ACCESSORS(code, Code)
3139
3140 // [construct stub]: Code stub for constructing instances of this function.
3141 DECL_ACCESSORS(construct_stub, Code)
3142
3143 // Returns if this function has been compiled to native code yet.
3144 inline bool is_compiled();
3145
3146 // [length]: The function length - usually the number of declared parameters.
3147 // Use up to 2^30 parameters.
3148 inline int length();
3149 inline void set_length(int value);
3150
3151 // [formal parameter count]: The declared number of parameters.
3152 inline int formal_parameter_count();
3153 inline void set_formal_parameter_count(int value);
3154
3155 // Set the formal parameter count so the function code will be
3156 // called without using argument adaptor frames.
3157 inline void DontAdaptArguments();
3158
3159 // [expected_nof_properties]: Expected number of properties for the function.
3160 inline int expected_nof_properties();
3161 inline void set_expected_nof_properties(int value);
3162
3163 // [instance class name]: class name for instances.
3164 DECL_ACCESSORS(instance_class_name, Object)
3165
3166 // [function data]: This field has been added for make benefit the API.
3167 // In the long run we don't want all functions to have this field but
3168 // we can fix that when we have a better model for storing hidden data
3169 // on objects.
3170 DECL_ACCESSORS(function_data, Object)
3171
3172 // [script info]: Script from which the function originates.
3173 DECL_ACCESSORS(script, Object)
3174
3175 // [start_position_and_type]: Field used to store both the source code
3176 // position, whether or not the function is a function expression,
3177 // and whether or not the function is a toplevel function. The two
3178 // least significants bit indicates whether the function is an
3179 // expression and the rest contains the source code position.
3180 inline int start_position_and_type();
3181 inline void set_start_position_and_type(int value);
3182
3183 // [debug info]: Debug information.
3184 DECL_ACCESSORS(debug_info, Object)
3185
3186 // [inferred name]: Name inferred from variable or property
3187 // assignment of this function. Used to facilitate debugging and
3188 // profiling of JavaScript code written in OO style, where almost
3189 // all functions are anonymous but are assigned to object
3190 // properties.
3191 DECL_ACCESSORS(inferred_name, String)
3192
3193 // Position of the 'function' token in the script source.
3194 inline int function_token_position();
3195 inline void set_function_token_position(int function_token_position);
3196
3197 // Position of this function in the script source.
3198 inline int start_position();
3199 inline void set_start_position(int start_position);
3200
3201 // End position of this function in the script source.
3202 inline int end_position();
3203 inline void set_end_position(int end_position);
3204
3205 // Is this function a function expression in the source code.
3206 inline bool is_expression();
3207 inline void set_is_expression(bool value);
3208
3209 // Is this function a top-level function (scripts, evals).
3210 inline bool is_toplevel();
3211 inline void set_is_toplevel(bool value);
3212
3213 // Bit field containing various information collected by the compiler to
3214 // drive optimization.
3215 inline int compiler_hints();
3216 inline void set_compiler_hints(int value);
3217
3218 // Add information on assignments of the form this.x = ...;
3219 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00003220 bool has_only_simple_this_property_assignments,
3221 FixedArray* this_property_assignments);
3222
3223 // Clear information on assignments of the form this.x = ...;
3224 void ClearThisPropertyAssignmentsInfo();
3225
3226 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00003227 // this.x = y; where y is either a constant or refers to an argument.
3228 inline bool has_only_simple_this_property_assignments();
3229
Leon Clarked91b9f72010-01-27 17:25:45 +00003230 inline bool try_full_codegen();
3231 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00003232
Steve Blocka7e24c12009-10-30 11:49:00 +00003233 // For functions which only contains this property assignments this provides
3234 // access to the names for the properties assigned.
3235 DECL_ACCESSORS(this_property_assignments, Object)
3236 inline int this_property_assignments_count();
3237 inline void set_this_property_assignments_count(int value);
3238 String* GetThisPropertyAssignmentName(int index);
3239 bool IsThisPropertyAssignmentArgument(int index);
3240 int GetThisPropertyAssignmentArgument(int index);
3241 Object* GetThisPropertyAssignmentConstant(int index);
3242
3243 // [source code]: Source code for the function.
3244 bool HasSourceCode();
3245 Object* GetSourceCode();
3246
3247 // Calculate the instance size.
3248 int CalculateInstanceSize();
3249
3250 // Calculate the number of in-object properties.
3251 int CalculateInObjectProperties();
3252
3253 // Dispatched behavior.
3254 void SharedFunctionInfoIterateBody(ObjectVisitor* v);
3255 // Set max_length to -1 for unlimited length.
3256 void SourceCodePrint(StringStream* accumulator, int max_length);
3257#ifdef DEBUG
3258 void SharedFunctionInfoPrint();
3259 void SharedFunctionInfoVerify();
3260#endif
3261
3262 // Casting.
3263 static inline SharedFunctionInfo* cast(Object* obj);
3264
3265 // Constants.
3266 static const int kDontAdaptArgumentsSentinel = -1;
3267
3268 // Layout description.
3269 // (An even number of integers has a size that is a multiple of a pointer.)
3270 static const int kNameOffset = HeapObject::kHeaderSize;
3271 static const int kCodeOffset = kNameOffset + kPointerSize;
3272 static const int kConstructStubOffset = kCodeOffset + kPointerSize;
3273 static const int kLengthOffset = kConstructStubOffset + kPointerSize;
3274 static const int kFormalParameterCountOffset = kLengthOffset + kIntSize;
3275 static const int kExpectedNofPropertiesOffset =
3276 kFormalParameterCountOffset + kIntSize;
3277 static const int kStartPositionAndTypeOffset =
3278 kExpectedNofPropertiesOffset + kIntSize;
3279 static const int kEndPositionOffset = kStartPositionAndTypeOffset + kIntSize;
3280 static const int kFunctionTokenPositionOffset = kEndPositionOffset + kIntSize;
3281 static const int kInstanceClassNameOffset =
3282 kFunctionTokenPositionOffset + kIntSize;
3283 static const int kExternalReferenceDataOffset =
3284 kInstanceClassNameOffset + kPointerSize;
3285 static const int kScriptOffset = kExternalReferenceDataOffset + kPointerSize;
3286 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
3287 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
3288 static const int kCompilerHintsOffset = kInferredNameOffset + kPointerSize;
3289 static const int kThisPropertyAssignmentsOffset =
3290 kCompilerHintsOffset + kPointerSize;
3291 static const int kThisPropertyAssignmentsCountOffset =
3292 kThisPropertyAssignmentsOffset + kPointerSize;
3293 static const int kSize = kThisPropertyAssignmentsCountOffset + kPointerSize;
3294
3295 private:
3296 // Bit positions in length_and_flg.
3297 // The least significant bit is used as the flag.
3298 static const int kFlagBit = 0;
3299 static const int kLengthShift = 1;
3300 static const int kLengthMask = ~((1 << kLengthShift) - 1);
3301
3302 // Bit positions in start_position_and_type.
3303 // The source code start position is in the 30 most significant bits of
3304 // the start_position_and_type field.
3305 static const int kIsExpressionBit = 0;
3306 static const int kIsTopLevelBit = 1;
3307 static const int kStartPositionShift = 2;
3308 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
3309
3310 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00003311 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00003312 static const int kTryFullCodegen = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003313
3314 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
3315};
3316
3317
3318// JSFunction describes JavaScript functions.
3319class JSFunction: public JSObject {
3320 public:
3321 // [prototype_or_initial_map]:
3322 DECL_ACCESSORS(prototype_or_initial_map, Object)
3323
3324 // [shared_function_info]: The information about the function that
3325 // can be shared by instances.
3326 DECL_ACCESSORS(shared, SharedFunctionInfo)
3327
3328 // [context]: The context for this function.
3329 inline Context* context();
3330 inline Object* unchecked_context();
3331 inline void set_context(Object* context);
3332
3333 // [code]: The generated code object for this function. Executed
3334 // when the function is invoked, e.g. foo() or new foo(). See
3335 // [[Call]] and [[Construct]] description in ECMA-262, section
3336 // 8.6.2, page 27.
3337 inline Code* code();
3338 inline void set_code(Code* value);
3339
3340 // Tells whether this function is a context-independent boilerplate
3341 // function.
3342 inline bool IsBoilerplate();
3343
3344 // Tells whether this function is builtin.
3345 inline bool IsBuiltin();
3346
3347 // [literals]: Fixed array holding the materialized literals.
3348 //
3349 // If the function contains object, regexp or array literals, the
3350 // literals array prefix contains the object, regexp, and array
3351 // function to be used when creating these literals. This is
3352 // necessary so that we do not dynamically lookup the object, regexp
3353 // or array functions. Performing a dynamic lookup, we might end up
3354 // using the functions from a new context that we should not have
3355 // access to.
3356 DECL_ACCESSORS(literals, FixedArray)
3357
3358 // The initial map for an object created by this constructor.
3359 inline Map* initial_map();
3360 inline void set_initial_map(Map* value);
3361 inline bool has_initial_map();
3362
3363 // Get and set the prototype property on a JSFunction. If the
3364 // function has an initial map the prototype is set on the initial
3365 // map. Otherwise, the prototype is put in the initial map field
3366 // until an initial map is needed.
3367 inline bool has_prototype();
3368 inline bool has_instance_prototype();
3369 inline Object* prototype();
3370 inline Object* instance_prototype();
3371 Object* SetInstancePrototype(Object* value);
3372 Object* SetPrototype(Object* value);
3373
3374 // Accessor for this function's initial map's [[class]]
3375 // property. This is primarily used by ECMA native functions. This
3376 // method sets the class_name field of this function's initial map
3377 // to a given value. It creates an initial map if this function does
3378 // not have one. Note that this method does not copy the initial map
3379 // if it has one already, but simply replaces it with the new value.
3380 // Instances created afterwards will have a map whose [[class]] is
3381 // set to 'value', but there is no guarantees on instances created
3382 // before.
3383 Object* SetInstanceClassName(String* name);
3384
3385 // Returns if this function has been compiled to native code yet.
3386 inline bool is_compiled();
3387
3388 // Casting.
3389 static inline JSFunction* cast(Object* obj);
3390
3391 // Dispatched behavior.
3392#ifdef DEBUG
3393 void JSFunctionPrint();
3394 void JSFunctionVerify();
3395#endif
3396
3397 // Returns the number of allocated literals.
3398 inline int NumberOfLiterals();
3399
3400 // Retrieve the global context from a function's literal array.
3401 static Context* GlobalContextFromLiterals(FixedArray* literals);
3402
3403 // Layout descriptors.
3404 static const int kPrototypeOrInitialMapOffset = JSObject::kHeaderSize;
3405 static const int kSharedFunctionInfoOffset =
3406 kPrototypeOrInitialMapOffset + kPointerSize;
3407 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
3408 static const int kLiteralsOffset = kContextOffset + kPointerSize;
3409 static const int kSize = kLiteralsOffset + kPointerSize;
3410
3411 // Layout of the literals array.
3412 static const int kLiteralsPrefixSize = 1;
3413 static const int kLiteralGlobalContextIndex = 0;
3414 private:
3415 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
3416};
3417
3418
3419// JSGlobalProxy's prototype must be a JSGlobalObject or null,
3420// and the prototype is hidden. JSGlobalProxy always delegates
3421// property accesses to its prototype if the prototype is not null.
3422//
3423// A JSGlobalProxy can be reinitialized which will preserve its identity.
3424//
3425// Accessing a JSGlobalProxy requires security check.
3426
3427class JSGlobalProxy : public JSObject {
3428 public:
3429 // [context]: the owner global context of this proxy object.
3430 // It is null value if this object is not used by any context.
3431 DECL_ACCESSORS(context, Object)
3432
3433 // Casting.
3434 static inline JSGlobalProxy* cast(Object* obj);
3435
3436 // Dispatched behavior.
3437#ifdef DEBUG
3438 void JSGlobalProxyPrint();
3439 void JSGlobalProxyVerify();
3440#endif
3441
3442 // Layout description.
3443 static const int kContextOffset = JSObject::kHeaderSize;
3444 static const int kSize = kContextOffset + kPointerSize;
3445
3446 private:
3447
3448 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
3449};
3450
3451
3452// Forward declaration.
3453class JSBuiltinsObject;
3454
3455// Common super class for JavaScript global objects and the special
3456// builtins global objects.
3457class GlobalObject: public JSObject {
3458 public:
3459 // [builtins]: the object holding the runtime routines written in JS.
3460 DECL_ACCESSORS(builtins, JSBuiltinsObject)
3461
3462 // [global context]: the global context corresponding to this global object.
3463 DECL_ACCESSORS(global_context, Context)
3464
3465 // [global receiver]: the global receiver object of the context
3466 DECL_ACCESSORS(global_receiver, JSObject)
3467
3468 // Retrieve the property cell used to store a property.
3469 Object* GetPropertyCell(LookupResult* result);
3470
3471 // Ensure that the global object has a cell for the given property name.
3472 Object* EnsurePropertyCell(String* name);
3473
3474 // Casting.
3475 static inline GlobalObject* cast(Object* obj);
3476
3477 // Layout description.
3478 static const int kBuiltinsOffset = JSObject::kHeaderSize;
3479 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
3480 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
3481 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
3482
3483 private:
3484 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
3485
3486 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
3487};
3488
3489
3490// JavaScript global object.
3491class JSGlobalObject: public GlobalObject {
3492 public:
3493
3494 // Casting.
3495 static inline JSGlobalObject* cast(Object* obj);
3496
3497 // Dispatched behavior.
3498#ifdef DEBUG
3499 void JSGlobalObjectPrint();
3500 void JSGlobalObjectVerify();
3501#endif
3502
3503 // Layout description.
3504 static const int kSize = GlobalObject::kHeaderSize;
3505
3506 private:
3507 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
3508};
3509
3510
3511// Builtins global object which holds the runtime routines written in
3512// JavaScript.
3513class JSBuiltinsObject: public GlobalObject {
3514 public:
3515 // Accessors for the runtime routines written in JavaScript.
3516 inline Object* javascript_builtin(Builtins::JavaScript id);
3517 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
3518
3519 // Casting.
3520 static inline JSBuiltinsObject* cast(Object* obj);
3521
3522 // Dispatched behavior.
3523#ifdef DEBUG
3524 void JSBuiltinsObjectPrint();
3525 void JSBuiltinsObjectVerify();
3526#endif
3527
3528 // Layout description. The size of the builtins object includes
3529 // room for one pointer per runtime routine written in javascript.
3530 static const int kJSBuiltinsCount = Builtins::id_count;
3531 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
3532 static const int kSize =
3533 kJSBuiltinsOffset + (kJSBuiltinsCount * kPointerSize);
3534 private:
3535 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
3536};
3537
3538
3539// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
3540class JSValue: public JSObject {
3541 public:
3542 // [value]: the object being wrapped.
3543 DECL_ACCESSORS(value, Object)
3544
3545 // Casting.
3546 static inline JSValue* cast(Object* obj);
3547
3548 // Dispatched behavior.
3549#ifdef DEBUG
3550 void JSValuePrint();
3551 void JSValueVerify();
3552#endif
3553
3554 // Layout description.
3555 static const int kValueOffset = JSObject::kHeaderSize;
3556 static const int kSize = kValueOffset + kPointerSize;
3557
3558 private:
3559 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
3560};
3561
3562// Regular expressions
3563// The regular expression holds a single reference to a FixedArray in
3564// the kDataOffset field.
3565// The FixedArray contains the following data:
3566// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
3567// - reference to the original source string
3568// - reference to the original flag string
3569// If it is an atom regexp
3570// - a reference to a literal string to search for
3571// If it is an irregexp regexp:
3572// - a reference to code for ASCII inputs (bytecode or compiled).
3573// - a reference to code for UC16 inputs (bytecode or compiled).
3574// - max number of registers used by irregexp implementations.
3575// - number of capture registers (output values) of the regexp.
3576class JSRegExp: public JSObject {
3577 public:
3578 // Meaning of Type:
3579 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
3580 // ATOM: A simple string to match against using an indexOf operation.
3581 // IRREGEXP: Compiled with Irregexp.
3582 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
3583 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
3584 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
3585
3586 class Flags {
3587 public:
3588 explicit Flags(uint32_t value) : value_(value) { }
3589 bool is_global() { return (value_ & GLOBAL) != 0; }
3590 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
3591 bool is_multiline() { return (value_ & MULTILINE) != 0; }
3592 uint32_t value() { return value_; }
3593 private:
3594 uint32_t value_;
3595 };
3596
3597 DECL_ACCESSORS(data, Object)
3598
3599 inline Type TypeTag();
3600 inline int CaptureCount();
3601 inline Flags GetFlags();
3602 inline String* Pattern();
3603 inline Object* DataAt(int index);
3604 // Set implementation data after the object has been prepared.
3605 inline void SetDataAt(int index, Object* value);
3606 static int code_index(bool is_ascii) {
3607 if (is_ascii) {
3608 return kIrregexpASCIICodeIndex;
3609 } else {
3610 return kIrregexpUC16CodeIndex;
3611 }
3612 }
3613
3614 static inline JSRegExp* cast(Object* obj);
3615
3616 // Dispatched behavior.
3617#ifdef DEBUG
3618 void JSRegExpVerify();
3619#endif
3620
3621 static const int kDataOffset = JSObject::kHeaderSize;
3622 static const int kSize = kDataOffset + kPointerSize;
3623
3624 // Indices in the data array.
3625 static const int kTagIndex = 0;
3626 static const int kSourceIndex = kTagIndex + 1;
3627 static const int kFlagsIndex = kSourceIndex + 1;
3628 static const int kDataIndex = kFlagsIndex + 1;
3629 // The data fields are used in different ways depending on the
3630 // value of the tag.
3631 // Atom regexps (literal strings).
3632 static const int kAtomPatternIndex = kDataIndex;
3633
3634 static const int kAtomDataSize = kAtomPatternIndex + 1;
3635
3636 // Irregexp compiled code or bytecode for ASCII. If compilation
3637 // fails, this fields hold an exception object that should be
3638 // thrown if the regexp is used again.
3639 static const int kIrregexpASCIICodeIndex = kDataIndex;
3640 // Irregexp compiled code or bytecode for UC16. If compilation
3641 // fails, this fields hold an exception object that should be
3642 // thrown if the regexp is used again.
3643 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
3644 // Maximal number of registers used by either ASCII or UC16.
3645 // Only used to check that there is enough stack space
3646 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
3647 // Number of captures in the compiled regexp.
3648 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
3649
3650 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00003651
3652 // Offsets directly into the data fixed array.
3653 static const int kDataTagOffset =
3654 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
3655 static const int kDataAsciiCodeOffset =
3656 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00003657 static const int kDataUC16CodeOffset =
3658 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00003659 static const int kIrregexpCaptureCountOffset =
3660 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003661};
3662
3663
3664class CompilationCacheShape {
3665 public:
3666 static inline bool IsMatch(HashTableKey* key, Object* value) {
3667 return key->IsMatch(value);
3668 }
3669
3670 static inline uint32_t Hash(HashTableKey* key) {
3671 return key->Hash();
3672 }
3673
3674 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
3675 return key->HashForObject(object);
3676 }
3677
3678 static Object* AsObject(HashTableKey* key) {
3679 return key->AsObject();
3680 }
3681
3682 static const int kPrefixSize = 0;
3683 static const int kEntrySize = 2;
3684};
3685
Steve Block3ce2e202009-11-05 08:53:23 +00003686
Steve Blocka7e24c12009-10-30 11:49:00 +00003687class CompilationCacheTable: public HashTable<CompilationCacheShape,
3688 HashTableKey*> {
3689 public:
3690 // Find cached value for a string key, otherwise return null.
3691 Object* Lookup(String* src);
3692 Object* LookupEval(String* src, Context* context);
3693 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
3694 Object* Put(String* src, Object* value);
3695 Object* PutEval(String* src, Context* context, Object* value);
3696 Object* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
3697
3698 static inline CompilationCacheTable* cast(Object* obj);
3699
3700 private:
3701 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
3702};
3703
3704
3705enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
3706enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
3707
3708
3709class StringHasher {
3710 public:
3711 inline StringHasher(int length);
3712
3713 // Returns true if the hash of this string can be computed without
3714 // looking at the contents.
3715 inline bool has_trivial_hash();
3716
3717 // Add a character to the hash and update the array index calculation.
3718 inline void AddCharacter(uc32 c);
3719
3720 // Adds a character to the hash but does not update the array index
3721 // calculation. This can only be called when it has been verified
3722 // that the input is not an array index.
3723 inline void AddCharacterNoIndex(uc32 c);
3724
3725 // Returns the value to store in the hash field of a string with
3726 // the given length and contents.
3727 uint32_t GetHashField();
3728
3729 // Returns true if the characters seen so far make up a legal array
3730 // index.
3731 bool is_array_index() { return is_array_index_; }
3732
3733 bool is_valid() { return is_valid_; }
3734
3735 void invalidate() { is_valid_ = false; }
3736
3737 private:
3738
3739 uint32_t array_index() {
3740 ASSERT(is_array_index());
3741 return array_index_;
3742 }
3743
3744 inline uint32_t GetHash();
3745
3746 int length_;
3747 uint32_t raw_running_hash_;
3748 uint32_t array_index_;
3749 bool is_array_index_;
3750 bool is_first_char_;
3751 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00003752 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00003753};
3754
3755
3756// The characteristics of a string are stored in its map. Retrieving these
3757// few bits of information is moderately expensive, involving two memory
3758// loads where the second is dependent on the first. To improve efficiency
3759// the shape of the string is given its own class so that it can be retrieved
3760// once and used for several string operations. A StringShape is small enough
3761// to be passed by value and is immutable, but be aware that flattening a
3762// string can potentially alter its shape. Also be aware that a GC caused by
3763// something else can alter the shape of a string due to ConsString
3764// shortcutting. Keeping these restrictions in mind has proven to be error-
3765// prone and so we no longer put StringShapes in variables unless there is a
3766// concrete performance benefit at that particular point in the code.
3767class StringShape BASE_EMBEDDED {
3768 public:
3769 inline explicit StringShape(String* s);
3770 inline explicit StringShape(Map* s);
3771 inline explicit StringShape(InstanceType t);
3772 inline bool IsSequential();
3773 inline bool IsExternal();
3774 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00003775 inline bool IsExternalAscii();
3776 inline bool IsExternalTwoByte();
3777 inline bool IsSequentialAscii();
3778 inline bool IsSequentialTwoByte();
3779 inline bool IsSymbol();
3780 inline StringRepresentationTag representation_tag();
3781 inline uint32_t full_representation_tag();
3782 inline uint32_t size_tag();
3783#ifdef DEBUG
3784 inline uint32_t type() { return type_; }
3785 inline void invalidate() { valid_ = false; }
3786 inline bool valid() { return valid_; }
3787#else
3788 inline void invalidate() { }
3789#endif
3790 private:
3791 uint32_t type_;
3792#ifdef DEBUG
3793 inline void set_valid() { valid_ = true; }
3794 bool valid_;
3795#else
3796 inline void set_valid() { }
3797#endif
3798};
3799
3800
3801// The String abstract class captures JavaScript string values:
3802//
3803// Ecma-262:
3804// 4.3.16 String Value
3805// A string value is a member of the type String and is a finite
3806// ordered sequence of zero or more 16-bit unsigned integer values.
3807//
3808// All string values have a length field.
3809class String: public HeapObject {
3810 public:
3811 // Get and set the length of the string.
3812 inline int length();
3813 inline void set_length(int value);
3814
Steve Blockd0582a62009-12-15 09:54:21 +00003815 // Get and set the hash field of the string.
3816 inline uint32_t hash_field();
3817 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003818
3819 inline bool IsAsciiRepresentation();
3820 inline bool IsTwoByteRepresentation();
3821
3822 // Get and set individual two byte chars in the string.
3823 inline void Set(int index, uint16_t value);
3824 // Get individual two byte char in the string. Repeated calls
3825 // to this method are not efficient unless the string is flat.
3826 inline uint16_t Get(int index);
3827
3828 // Try to flatten the top level ConsString that is hiding behind this
Steve Blockd0582a62009-12-15 09:54:21 +00003829 // string. This is a no-op unless the string is a ConsString. Flatten
3830 // mutates the ConsString and might return a failure.
Steve Blocka7e24c12009-10-30 11:49:00 +00003831 Object* TryFlatten();
3832
3833 // Try to flatten the string. Checks first inline to see if it is necessary.
3834 // Do not handle allocation failures. After calling TryFlattenIfNotFlat, the
3835 // string could still be a ConsString, in which case a failure is returned.
3836 // Use FlattenString from Handles.cc to be sure to flatten.
3837 inline Object* TryFlattenIfNotFlat();
3838
3839 Vector<const char> ToAsciiVector();
3840 Vector<const uc16> ToUC16Vector();
3841
3842 // Mark the string as an undetectable object. It only applies to
3843 // ascii and two byte string types.
3844 bool MarkAsUndetectable();
3845
Steve Blockd0582a62009-12-15 09:54:21 +00003846 // Return a substring.
3847 Object* SubString(int from, int to);
Steve Blocka7e24c12009-10-30 11:49:00 +00003848
3849 // String equality operations.
3850 inline bool Equals(String* other);
3851 bool IsEqualTo(Vector<const char> str);
3852
3853 // Return a UTF8 representation of the string. The string is null
3854 // terminated but may optionally contain nulls. Length is returned
3855 // in length_output if length_output is not a null pointer The string
3856 // should be nearly flat, otherwise the performance of this method may
3857 // be very slow (quadratic in the length). Setting robustness_flag to
3858 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
3859 // handles unexpected data without causing assert failures and it does not
3860 // do any heap allocations. This is useful when printing stack traces.
3861 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
3862 RobustnessFlag robustness_flag,
3863 int offset,
3864 int length,
3865 int* length_output = 0);
3866 SmartPointer<char> ToCString(
3867 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
3868 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
3869 int* length_output = 0);
3870
3871 int Utf8Length();
3872
3873 // Return a 16 bit Unicode representation of the string.
3874 // The string should be nearly flat, otherwise the performance of
3875 // of this method may be very bad. Setting robustness_flag to
3876 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
3877 // handles unexpected data without causing assert failures and it does not
3878 // do any heap allocations. This is useful when printing stack traces.
3879 SmartPointer<uc16> ToWideCString(
3880 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
3881
3882 // Tells whether the hash code has been computed.
3883 inline bool HasHashCode();
3884
3885 // Returns a hash value used for the property table
3886 inline uint32_t Hash();
3887
Steve Blockd0582a62009-12-15 09:54:21 +00003888 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
3889 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00003890
3891 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
3892 uint32_t* index,
3893 int length);
3894
3895 // Externalization.
3896 bool MakeExternal(v8::String::ExternalStringResource* resource);
3897 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
3898
3899 // Conversion.
3900 inline bool AsArrayIndex(uint32_t* index);
3901
3902 // Casting.
3903 static inline String* cast(Object* obj);
3904
3905 void PrintOn(FILE* out);
3906
3907 // For use during stack traces. Performs rudimentary sanity check.
3908 bool LooksValid();
3909
3910 // Dispatched behavior.
3911 void StringShortPrint(StringStream* accumulator);
3912#ifdef DEBUG
3913 void StringPrint();
3914 void StringVerify();
3915#endif
3916 inline bool IsFlat();
3917
3918 // Layout description.
3919 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003920 static const int kHashFieldOffset = kLengthOffset + kIntSize;
3921 static const int kSize = kHashFieldOffset + kIntSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003922 // Notice: kSize is not pointer-size aligned if pointers are 64-bit.
3923
Steve Blockd0582a62009-12-15 09:54:21 +00003924 // Maximum number of characters to consider when trying to convert a string
3925 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00003926 static const int kMaxArrayIndexSize = 10;
3927
3928 // Max ascii char code.
3929 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
3930 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
3931 static const int kMaxUC16CharCode = 0xffff;
3932
Steve Blockd0582a62009-12-15 09:54:21 +00003933 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00003934 static const int kMinNonFlatLength = 13;
3935
3936 // Mask constant for checking if a string has a computed hash code
3937 // and if it is an array index. The least significant bit indicates
3938 // whether a hash code has been computed. If the hash code has been
3939 // computed the 2nd bit tells whether the string can be used as an
3940 // array index.
3941 static const int kHashComputedMask = 1;
3942 static const int kIsArrayIndexMask = 1 << 1;
3943 static const int kNofLengthBitFields = 2;
3944
Steve Blockd0582a62009-12-15 09:54:21 +00003945 // Shift constant retrieving hash code from hash field.
3946 static const int kHashShift = kNofLengthBitFields;
3947
Steve Blocka7e24c12009-10-30 11:49:00 +00003948 // Array index strings this short can keep their index in the hash
3949 // field.
3950 static const int kMaxCachedArrayIndexLength = 7;
3951
Steve Blockd0582a62009-12-15 09:54:21 +00003952 // For strings which are array indexes the hash value has the string length
3953 // mixed into the hash, mainly to avoid a hash value of zero which would be
3954 // the case for the string '0'. 24 bits are used for the array index value.
3955 static const int kArrayIndexHashLengthShift = 24 + kNofLengthBitFields;
3956 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
3957 static const int kArrayIndexValueBits =
3958 kArrayIndexHashLengthShift - kHashShift;
3959
3960 // Value of empty hash field indicating that the hash is not computed.
3961 static const int kEmptyHashField = 0;
3962
3963 // Maximal string length.
3964 static const int kMaxLength = (1 << (32 - 2)) - 1;
3965
3966 // Max length for computing hash. For strings longer than this limit the
3967 // string length is used as the hash value.
3968 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00003969
3970 // Limit for truncation in short printing.
3971 static const int kMaxShortPrintLength = 1024;
3972
3973 // Support for regular expressions.
3974 const uc16* GetTwoByteData();
3975 const uc16* GetTwoByteData(unsigned start);
3976
3977 // Support for StringInputBuffer
3978 static const unibrow::byte* ReadBlock(String* input,
3979 unibrow::byte* util_buffer,
3980 unsigned capacity,
3981 unsigned* remaining,
3982 unsigned* offset);
3983 static const unibrow::byte* ReadBlock(String** input,
3984 unibrow::byte* util_buffer,
3985 unsigned capacity,
3986 unsigned* remaining,
3987 unsigned* offset);
3988
3989 // Helper function for flattening strings.
3990 template <typename sinkchar>
3991 static void WriteToFlat(String* source,
3992 sinkchar* sink,
3993 int from,
3994 int to);
3995
3996 protected:
3997 class ReadBlockBuffer {
3998 public:
3999 ReadBlockBuffer(unibrow::byte* util_buffer_,
4000 unsigned cursor_,
4001 unsigned capacity_,
4002 unsigned remaining_) :
4003 util_buffer(util_buffer_),
4004 cursor(cursor_),
4005 capacity(capacity_),
4006 remaining(remaining_) {
4007 }
4008 unibrow::byte* util_buffer;
4009 unsigned cursor;
4010 unsigned capacity;
4011 unsigned remaining;
4012 };
4013
Steve Blocka7e24c12009-10-30 11:49:00 +00004014 static inline const unibrow::byte* ReadBlock(String* input,
4015 ReadBlockBuffer* buffer,
4016 unsigned* offset,
4017 unsigned max_chars);
4018 static void ReadBlockIntoBuffer(String* input,
4019 ReadBlockBuffer* buffer,
4020 unsigned* offset_ptr,
4021 unsigned max_chars);
4022
4023 private:
4024 // Slow case of String::Equals. This implementation works on any strings
4025 // but it is most efficient on strings that are almost flat.
4026 bool SlowEquals(String* other);
4027
4028 // Slow case of AsArrayIndex.
4029 bool SlowAsArrayIndex(uint32_t* index);
4030
4031 // Compute and set the hash code.
4032 uint32_t ComputeAndSetHash();
4033
4034 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
4035};
4036
4037
4038// The SeqString abstract class captures sequential string values.
4039class SeqString: public String {
4040 public:
4041
4042 // Casting.
4043 static inline SeqString* cast(Object* obj);
4044
4045 // Dispatched behaviour.
4046 // For regexp code.
4047 uint16_t* SeqStringGetTwoByteAddress();
4048
4049 private:
4050 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
4051};
4052
4053
4054// The AsciiString class captures sequential ascii string objects.
4055// Each character in the AsciiString is an ascii character.
4056class SeqAsciiString: public SeqString {
4057 public:
4058 // Dispatched behavior.
4059 inline uint16_t SeqAsciiStringGet(int index);
4060 inline void SeqAsciiStringSet(int index, uint16_t value);
4061
4062 // Get the address of the characters in this string.
4063 inline Address GetCharsAddress();
4064
4065 inline char* GetChars();
4066
4067 // Casting
4068 static inline SeqAsciiString* cast(Object* obj);
4069
4070 // Garbage collection support. This method is called by the
4071 // garbage collector to compute the actual size of an AsciiString
4072 // instance.
4073 inline int SeqAsciiStringSize(InstanceType instance_type);
4074
4075 // Computes the size for an AsciiString instance of a given length.
4076 static int SizeFor(int length) {
4077 return OBJECT_SIZE_ALIGN(kHeaderSize + length * kCharSize);
4078 }
4079
4080 // Layout description.
4081 static const int kHeaderSize = String::kSize;
4082 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4083
Leon Clarkee46be812010-01-19 14:06:41 +00004084 // Maximal memory usage for a single sequential ASCII string.
4085 static const int kMaxSize = 512 * MB;
4086 // Maximal length of a single sequential ASCII string.
4087 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4088 static const int kMaxLength = (kMaxSize - kHeaderSize);
4089
Steve Blocka7e24c12009-10-30 11:49:00 +00004090 // Support for StringInputBuffer.
4091 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4092 unsigned* offset,
4093 unsigned chars);
4094 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
4095 unsigned* offset,
4096 unsigned chars);
4097
4098 private:
4099 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
4100};
4101
4102
4103// The TwoByteString class captures sequential unicode string objects.
4104// Each character in the TwoByteString is a two-byte uint16_t.
4105class SeqTwoByteString: public SeqString {
4106 public:
4107 // Dispatched behavior.
4108 inline uint16_t SeqTwoByteStringGet(int index);
4109 inline void SeqTwoByteStringSet(int index, uint16_t value);
4110
4111 // Get the address of the characters in this string.
4112 inline Address GetCharsAddress();
4113
4114 inline uc16* GetChars();
4115
4116 // For regexp code.
4117 const uint16_t* SeqTwoByteStringGetData(unsigned start);
4118
4119 // Casting
4120 static inline SeqTwoByteString* cast(Object* obj);
4121
4122 // Garbage collection support. This method is called by the
4123 // garbage collector to compute the actual size of a TwoByteString
4124 // instance.
4125 inline int SeqTwoByteStringSize(InstanceType instance_type);
4126
4127 // Computes the size for a TwoByteString instance of a given length.
4128 static int SizeFor(int length) {
4129 return OBJECT_SIZE_ALIGN(kHeaderSize + length * kShortSize);
4130 }
4131
4132 // Layout description.
4133 static const int kHeaderSize = String::kSize;
4134 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4135
Leon Clarkee46be812010-01-19 14:06:41 +00004136 // Maximal memory usage for a single sequential two-byte string.
4137 static const int kMaxSize = 512 * MB;
4138 // Maximal length of a single sequential two-byte string.
4139 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4140 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
4141
Steve Blocka7e24c12009-10-30 11:49:00 +00004142 // Support for StringInputBuffer.
4143 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4144 unsigned* offset_ptr,
4145 unsigned chars);
4146
4147 private:
4148 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
4149};
4150
4151
4152// The ConsString class describes string values built by using the
4153// addition operator on strings. A ConsString is a pair where the
4154// first and second components are pointers to other string values.
4155// One or both components of a ConsString can be pointers to other
4156// ConsStrings, creating a binary tree of ConsStrings where the leaves
4157// are non-ConsString string values. The string value represented by
4158// a ConsString can be obtained by concatenating the leaf string
4159// values in a left-to-right depth-first traversal of the tree.
4160class ConsString: public String {
4161 public:
4162 // First string of the cons cell.
4163 inline String* first();
4164 // Doesn't check that the result is a string, even in debug mode. This is
4165 // useful during GC where the mark bits confuse the checks.
4166 inline Object* unchecked_first();
4167 inline void set_first(String* first,
4168 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4169
4170 // Second string of the cons cell.
4171 inline String* second();
4172 // Doesn't check that the result is a string, even in debug mode. This is
4173 // useful during GC where the mark bits confuse the checks.
4174 inline Object* unchecked_second();
4175 inline void set_second(String* second,
4176 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4177
4178 // Dispatched behavior.
4179 uint16_t ConsStringGet(int index);
4180
4181 // Casting.
4182 static inline ConsString* cast(Object* obj);
4183
4184 // Garbage collection support. This method is called during garbage
4185 // collection to iterate through the heap pointers in the body of
4186 // the ConsString.
4187 void ConsStringIterateBody(ObjectVisitor* v);
4188
4189 // Layout description.
4190 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
4191 static const int kSecondOffset = kFirstOffset + kPointerSize;
4192 static const int kSize = kSecondOffset + kPointerSize;
4193
4194 // Support for StringInputBuffer.
4195 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
4196 unsigned* offset_ptr,
4197 unsigned chars);
4198 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4199 unsigned* offset_ptr,
4200 unsigned chars);
4201
4202 // Minimum length for a cons string.
4203 static const int kMinLength = 13;
4204
4205 private:
4206 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
4207};
4208
4209
Steve Blocka7e24c12009-10-30 11:49:00 +00004210// The ExternalString class describes string values that are backed by
4211// a string resource that lies outside the V8 heap. ExternalStrings
4212// consist of the length field common to all strings, a pointer to the
4213// external resource. It is important to ensure (externally) that the
4214// resource is not deallocated while the ExternalString is live in the
4215// V8 heap.
4216//
4217// The API expects that all ExternalStrings are created through the
4218// API. Therefore, ExternalStrings should not be used internally.
4219class ExternalString: public String {
4220 public:
4221 // Casting
4222 static inline ExternalString* cast(Object* obj);
4223
4224 // Layout description.
4225 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
4226 static const int kSize = kResourceOffset + kPointerSize;
4227
4228 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
4229
4230 private:
4231 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
4232};
4233
4234
4235// The ExternalAsciiString class is an external string backed by an
4236// ASCII string.
4237class ExternalAsciiString: public ExternalString {
4238 public:
4239 typedef v8::String::ExternalAsciiStringResource Resource;
4240
4241 // The underlying resource.
4242 inline Resource* resource();
4243 inline void set_resource(Resource* buffer);
4244
4245 // Dispatched behavior.
4246 uint16_t ExternalAsciiStringGet(int index);
4247
4248 // Casting.
4249 static inline ExternalAsciiString* cast(Object* obj);
4250
Steve Blockd0582a62009-12-15 09:54:21 +00004251 // Garbage collection support.
4252 void ExternalAsciiStringIterateBody(ObjectVisitor* v);
4253
Steve Blocka7e24c12009-10-30 11:49:00 +00004254 // Support for StringInputBuffer.
4255 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
4256 unsigned* offset,
4257 unsigned chars);
4258 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4259 unsigned* offset,
4260 unsigned chars);
4261
Steve Blocka7e24c12009-10-30 11:49:00 +00004262 private:
4263 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
4264};
4265
4266
4267// The ExternalTwoByteString class is an external string backed by a UTF-16
4268// encoded string.
4269class ExternalTwoByteString: public ExternalString {
4270 public:
4271 typedef v8::String::ExternalStringResource Resource;
4272
4273 // The underlying string resource.
4274 inline Resource* resource();
4275 inline void set_resource(Resource* buffer);
4276
4277 // Dispatched behavior.
4278 uint16_t ExternalTwoByteStringGet(int index);
4279
4280 // For regexp code.
4281 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
4282
4283 // Casting.
4284 static inline ExternalTwoByteString* cast(Object* obj);
4285
Steve Blockd0582a62009-12-15 09:54:21 +00004286 // Garbage collection support.
4287 void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
4288
Steve Blocka7e24c12009-10-30 11:49:00 +00004289 // Support for StringInputBuffer.
4290 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4291 unsigned* offset_ptr,
4292 unsigned chars);
4293
Steve Blocka7e24c12009-10-30 11:49:00 +00004294 private:
4295 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
4296};
4297
4298
4299// Utility superclass for stack-allocated objects that must be updated
4300// on gc. It provides two ways for the gc to update instances, either
4301// iterating or updating after gc.
4302class Relocatable BASE_EMBEDDED {
4303 public:
4304 inline Relocatable() : prev_(top_) { top_ = this; }
4305 virtual ~Relocatable() {
4306 ASSERT_EQ(top_, this);
4307 top_ = prev_;
4308 }
4309 virtual void IterateInstance(ObjectVisitor* v) { }
4310 virtual void PostGarbageCollection() { }
4311
4312 static void PostGarbageCollectionProcessing();
4313 static int ArchiveSpacePerThread();
4314 static char* ArchiveState(char* to);
4315 static char* RestoreState(char* from);
4316 static void Iterate(ObjectVisitor* v);
4317 static void Iterate(ObjectVisitor* v, Relocatable* top);
4318 static char* Iterate(ObjectVisitor* v, char* t);
4319 private:
4320 static Relocatable* top_;
4321 Relocatable* prev_;
4322};
4323
4324
4325// A flat string reader provides random access to the contents of a
4326// string independent of the character width of the string. The handle
4327// must be valid as long as the reader is being used.
4328class FlatStringReader : public Relocatable {
4329 public:
4330 explicit FlatStringReader(Handle<String> str);
4331 explicit FlatStringReader(Vector<const char> input);
4332 void PostGarbageCollection();
4333 inline uc32 Get(int index);
4334 int length() { return length_; }
4335 private:
4336 String** str_;
4337 bool is_ascii_;
4338 int length_;
4339 const void* start_;
4340};
4341
4342
4343// Note that StringInputBuffers are not valid across a GC! To fix this
4344// it would have to store a String Handle instead of a String* and
4345// AsciiStringReadBlock would have to be modified to use memcpy.
4346//
4347// StringInputBuffer is able to traverse any string regardless of how
4348// deeply nested a sequence of ConsStrings it is made of. However,
4349// performance will be better if deep strings are flattened before they
4350// are traversed. Since flattening requires memory allocation this is
4351// not always desirable, however (esp. in debugging situations).
4352class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
4353 public:
4354 virtual void Seek(unsigned pos);
4355 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
4356 inline StringInputBuffer(String* backing):
4357 unibrow::InputBuffer<String, String*, 1024>(backing) {}
4358};
4359
4360
4361class SafeStringInputBuffer
4362 : public unibrow::InputBuffer<String, String**, 256> {
4363 public:
4364 virtual void Seek(unsigned pos);
4365 inline SafeStringInputBuffer()
4366 : unibrow::InputBuffer<String, String**, 256>() {}
4367 inline SafeStringInputBuffer(String** backing)
4368 : unibrow::InputBuffer<String, String**, 256>(backing) {}
4369};
4370
4371
4372template <typename T>
4373class VectorIterator {
4374 public:
4375 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
4376 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
4377 T GetNext() { return data_[index_++]; }
4378 bool has_more() { return index_ < data_.length(); }
4379 private:
4380 Vector<const T> data_;
4381 int index_;
4382};
4383
4384
4385// The Oddball describes objects null, undefined, true, and false.
4386class Oddball: public HeapObject {
4387 public:
4388 // [to_string]: Cached to_string computed at startup.
4389 DECL_ACCESSORS(to_string, String)
4390
4391 // [to_number]: Cached to_number computed at startup.
4392 DECL_ACCESSORS(to_number, Object)
4393
4394 // Casting.
4395 static inline Oddball* cast(Object* obj);
4396
4397 // Dispatched behavior.
4398 void OddballIterateBody(ObjectVisitor* v);
4399#ifdef DEBUG
4400 void OddballVerify();
4401#endif
4402
4403 // Initialize the fields.
4404 Object* Initialize(const char* to_string, Object* to_number);
4405
4406 // Layout description.
4407 static const int kToStringOffset = HeapObject::kHeaderSize;
4408 static const int kToNumberOffset = kToStringOffset + kPointerSize;
4409 static const int kSize = kToNumberOffset + kPointerSize;
4410
4411 private:
4412 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
4413};
4414
4415
4416class JSGlobalPropertyCell: public HeapObject {
4417 public:
4418 // [value]: value of the global property.
4419 DECL_ACCESSORS(value, Object)
4420
4421 // Casting.
4422 static inline JSGlobalPropertyCell* cast(Object* obj);
4423
4424 // Dispatched behavior.
4425 void JSGlobalPropertyCellIterateBody(ObjectVisitor* v);
4426#ifdef DEBUG
4427 void JSGlobalPropertyCellVerify();
4428 void JSGlobalPropertyCellPrint();
4429#endif
4430
4431 // Layout description.
4432 static const int kValueOffset = HeapObject::kHeaderSize;
4433 static const int kSize = kValueOffset + kPointerSize;
4434
4435 private:
4436 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
4437};
4438
4439
4440
4441// Proxy describes objects pointing from JavaScript to C structures.
4442// Since they cannot contain references to JS HeapObjects they can be
4443// placed in old_data_space.
4444class Proxy: public HeapObject {
4445 public:
4446 // [proxy]: field containing the address.
4447 inline Address proxy();
4448 inline void set_proxy(Address value);
4449
4450 // Casting.
4451 static inline Proxy* cast(Object* obj);
4452
4453 // Dispatched behavior.
4454 inline void ProxyIterateBody(ObjectVisitor* v);
4455#ifdef DEBUG
4456 void ProxyPrint();
4457 void ProxyVerify();
4458#endif
4459
4460 // Layout description.
4461
4462 static const int kProxyOffset = HeapObject::kHeaderSize;
4463 static const int kSize = kProxyOffset + kPointerSize;
4464
4465 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
4466
4467 private:
4468 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
4469};
4470
4471
4472// The JSArray describes JavaScript Arrays
4473// Such an array can be in one of two modes:
4474// - fast, backing storage is a FixedArray and length <= elements.length();
4475// Please note: push and pop can be used to grow and shrink the array.
4476// - slow, backing storage is a HashTable with numbers as keys.
4477class JSArray: public JSObject {
4478 public:
4479 // [length]: The length property.
4480 DECL_ACCESSORS(length, Object)
4481
Leon Clarke4515c472010-02-03 11:58:03 +00004482 // Overload the length setter to skip write barrier when the length
4483 // is set to a smi. This matches the set function on FixedArray.
4484 inline void set_length(Smi* length);
4485
Steve Blocka7e24c12009-10-30 11:49:00 +00004486 Object* JSArrayUpdateLengthFromIndex(uint32_t index, Object* value);
4487
4488 // Initialize the array with the given capacity. The function may
4489 // fail due to out-of-memory situations, but only if the requested
4490 // capacity is non-zero.
4491 Object* Initialize(int capacity);
4492
4493 // Set the content of the array to the content of storage.
4494 inline void SetContent(FixedArray* storage);
4495
4496 // Casting.
4497 static inline JSArray* cast(Object* obj);
4498
4499 // Uses handles. Ensures that the fixed array backing the JSArray has at
4500 // least the stated size.
4501 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
4502
4503 // Dispatched behavior.
4504#ifdef DEBUG
4505 void JSArrayPrint();
4506 void JSArrayVerify();
4507#endif
4508
4509 // Number of element slots to pre-allocate for an empty array.
4510 static const int kPreallocatedArrayElements = 4;
4511
4512 // Layout description.
4513 static const int kLengthOffset = JSObject::kHeaderSize;
4514 static const int kSize = kLengthOffset + kPointerSize;
4515
4516 private:
4517 // Expand the fixed array backing of a fast-case JSArray to at least
4518 // the requested size.
4519 void Expand(int minimum_size_of_backing_fixed_array);
4520
4521 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
4522};
4523
4524
4525// An accessor must have a getter, but can have no setter.
4526//
4527// When setting a property, V8 searches accessors in prototypes.
4528// If an accessor was found and it does not have a setter,
4529// the request is ignored.
4530//
4531// If the accessor in the prototype has the READ_ONLY property attribute, then
4532// a new value is added to the local object when the property is set.
4533// This shadows the accessor in the prototype.
4534class AccessorInfo: public Struct {
4535 public:
4536 DECL_ACCESSORS(getter, Object)
4537 DECL_ACCESSORS(setter, Object)
4538 DECL_ACCESSORS(data, Object)
4539 DECL_ACCESSORS(name, Object)
4540 DECL_ACCESSORS(flag, Smi)
Steve Blockd0582a62009-12-15 09:54:21 +00004541 DECL_ACCESSORS(load_stub_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00004542
4543 inline bool all_can_read();
4544 inline void set_all_can_read(bool value);
4545
4546 inline bool all_can_write();
4547 inline void set_all_can_write(bool value);
4548
4549 inline bool prohibits_overwriting();
4550 inline void set_prohibits_overwriting(bool value);
4551
4552 inline PropertyAttributes property_attributes();
4553 inline void set_property_attributes(PropertyAttributes attributes);
4554
4555 static inline AccessorInfo* cast(Object* obj);
4556
4557#ifdef DEBUG
4558 void AccessorInfoPrint();
4559 void AccessorInfoVerify();
4560#endif
4561
4562 static const int kGetterOffset = HeapObject::kHeaderSize;
4563 static const int kSetterOffset = kGetterOffset + kPointerSize;
4564 static const int kDataOffset = kSetterOffset + kPointerSize;
4565 static const int kNameOffset = kDataOffset + kPointerSize;
4566 static const int kFlagOffset = kNameOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00004567 static const int kLoadStubCacheOffset = kFlagOffset + kPointerSize;
4568 static const int kSize = kLoadStubCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004569
4570 private:
4571 // Bit positions in flag.
4572 static const int kAllCanReadBit = 0;
4573 static const int kAllCanWriteBit = 1;
4574 static const int kProhibitsOverwritingBit = 2;
4575 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
4576
4577 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
4578};
4579
4580
4581class AccessCheckInfo: public Struct {
4582 public:
4583 DECL_ACCESSORS(named_callback, Object)
4584 DECL_ACCESSORS(indexed_callback, Object)
4585 DECL_ACCESSORS(data, Object)
4586
4587 static inline AccessCheckInfo* cast(Object* obj);
4588
4589#ifdef DEBUG
4590 void AccessCheckInfoPrint();
4591 void AccessCheckInfoVerify();
4592#endif
4593
4594 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
4595 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
4596 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
4597 static const int kSize = kDataOffset + kPointerSize;
4598
4599 private:
4600 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
4601};
4602
4603
4604class InterceptorInfo: public Struct {
4605 public:
4606 DECL_ACCESSORS(getter, Object)
4607 DECL_ACCESSORS(setter, Object)
4608 DECL_ACCESSORS(query, Object)
4609 DECL_ACCESSORS(deleter, Object)
4610 DECL_ACCESSORS(enumerator, Object)
4611 DECL_ACCESSORS(data, Object)
4612
4613 static inline InterceptorInfo* cast(Object* obj);
4614
4615#ifdef DEBUG
4616 void InterceptorInfoPrint();
4617 void InterceptorInfoVerify();
4618#endif
4619
4620 static const int kGetterOffset = HeapObject::kHeaderSize;
4621 static const int kSetterOffset = kGetterOffset + kPointerSize;
4622 static const int kQueryOffset = kSetterOffset + kPointerSize;
4623 static const int kDeleterOffset = kQueryOffset + kPointerSize;
4624 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
4625 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
4626 static const int kSize = kDataOffset + kPointerSize;
4627
4628 private:
4629 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
4630};
4631
4632
4633class CallHandlerInfo: public Struct {
4634 public:
4635 DECL_ACCESSORS(callback, Object)
4636 DECL_ACCESSORS(data, Object)
4637
4638 static inline CallHandlerInfo* cast(Object* obj);
4639
4640#ifdef DEBUG
4641 void CallHandlerInfoPrint();
4642 void CallHandlerInfoVerify();
4643#endif
4644
4645 static const int kCallbackOffset = HeapObject::kHeaderSize;
4646 static const int kDataOffset = kCallbackOffset + kPointerSize;
4647 static const int kSize = kDataOffset + kPointerSize;
4648
4649 private:
4650 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
4651};
4652
4653
4654class TemplateInfo: public Struct {
4655 public:
4656 DECL_ACCESSORS(tag, Object)
4657 DECL_ACCESSORS(property_list, Object)
4658
4659#ifdef DEBUG
4660 void TemplateInfoVerify();
4661#endif
4662
4663 static const int kTagOffset = HeapObject::kHeaderSize;
4664 static const int kPropertyListOffset = kTagOffset + kPointerSize;
4665 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
4666 protected:
4667 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4668 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
4669};
4670
4671
4672class FunctionTemplateInfo: public TemplateInfo {
4673 public:
4674 DECL_ACCESSORS(serial_number, Object)
4675 DECL_ACCESSORS(call_code, Object)
4676 DECL_ACCESSORS(property_accessors, Object)
4677 DECL_ACCESSORS(prototype_template, Object)
4678 DECL_ACCESSORS(parent_template, Object)
4679 DECL_ACCESSORS(named_property_handler, Object)
4680 DECL_ACCESSORS(indexed_property_handler, Object)
4681 DECL_ACCESSORS(instance_template, Object)
4682 DECL_ACCESSORS(class_name, Object)
4683 DECL_ACCESSORS(signature, Object)
4684 DECL_ACCESSORS(instance_call_handler, Object)
4685 DECL_ACCESSORS(access_check_info, Object)
4686 DECL_ACCESSORS(flag, Smi)
4687
4688 // Following properties use flag bits.
4689 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
4690 DECL_BOOLEAN_ACCESSORS(undetectable)
4691 // If the bit is set, object instances created by this function
4692 // requires access check.
4693 DECL_BOOLEAN_ACCESSORS(needs_access_check)
4694
4695 static inline FunctionTemplateInfo* cast(Object* obj);
4696
4697#ifdef DEBUG
4698 void FunctionTemplateInfoPrint();
4699 void FunctionTemplateInfoVerify();
4700#endif
4701
4702 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
4703 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
4704 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
4705 static const int kPrototypeTemplateOffset =
4706 kPropertyAccessorsOffset + kPointerSize;
4707 static const int kParentTemplateOffset =
4708 kPrototypeTemplateOffset + kPointerSize;
4709 static const int kNamedPropertyHandlerOffset =
4710 kParentTemplateOffset + kPointerSize;
4711 static const int kIndexedPropertyHandlerOffset =
4712 kNamedPropertyHandlerOffset + kPointerSize;
4713 static const int kInstanceTemplateOffset =
4714 kIndexedPropertyHandlerOffset + kPointerSize;
4715 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
4716 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
4717 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
4718 static const int kAccessCheckInfoOffset =
4719 kInstanceCallHandlerOffset + kPointerSize;
4720 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
4721 static const int kSize = kFlagOffset + kPointerSize;
4722
4723 private:
4724 // Bit position in the flag, from least significant bit position.
4725 static const int kHiddenPrototypeBit = 0;
4726 static const int kUndetectableBit = 1;
4727 static const int kNeedsAccessCheckBit = 2;
4728
4729 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
4730};
4731
4732
4733class ObjectTemplateInfo: public TemplateInfo {
4734 public:
4735 DECL_ACCESSORS(constructor, Object)
4736 DECL_ACCESSORS(internal_field_count, Object)
4737
4738 static inline ObjectTemplateInfo* cast(Object* obj);
4739
4740#ifdef DEBUG
4741 void ObjectTemplateInfoPrint();
4742 void ObjectTemplateInfoVerify();
4743#endif
4744
4745 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
4746 static const int kInternalFieldCountOffset =
4747 kConstructorOffset + kPointerSize;
4748 static const int kSize = kInternalFieldCountOffset + kPointerSize;
4749};
4750
4751
4752class SignatureInfo: public Struct {
4753 public:
4754 DECL_ACCESSORS(receiver, Object)
4755 DECL_ACCESSORS(args, Object)
4756
4757 static inline SignatureInfo* cast(Object* obj);
4758
4759#ifdef DEBUG
4760 void SignatureInfoPrint();
4761 void SignatureInfoVerify();
4762#endif
4763
4764 static const int kReceiverOffset = Struct::kHeaderSize;
4765 static const int kArgsOffset = kReceiverOffset + kPointerSize;
4766 static const int kSize = kArgsOffset + kPointerSize;
4767
4768 private:
4769 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
4770};
4771
4772
4773class TypeSwitchInfo: public Struct {
4774 public:
4775 DECL_ACCESSORS(types, Object)
4776
4777 static inline TypeSwitchInfo* cast(Object* obj);
4778
4779#ifdef DEBUG
4780 void TypeSwitchInfoPrint();
4781 void TypeSwitchInfoVerify();
4782#endif
4783
4784 static const int kTypesOffset = Struct::kHeaderSize;
4785 static const int kSize = kTypesOffset + kPointerSize;
4786};
4787
4788
4789#ifdef ENABLE_DEBUGGER_SUPPORT
4790// The DebugInfo class holds additional information for a function being
4791// debugged.
4792class DebugInfo: public Struct {
4793 public:
4794 // The shared function info for the source being debugged.
4795 DECL_ACCESSORS(shared, SharedFunctionInfo)
4796 // Code object for the original code.
4797 DECL_ACCESSORS(original_code, Code)
4798 // Code object for the patched code. This code object is the code object
4799 // currently active for the function.
4800 DECL_ACCESSORS(code, Code)
4801 // Fixed array holding status information for each active break point.
4802 DECL_ACCESSORS(break_points, FixedArray)
4803
4804 // Check if there is a break point at a code position.
4805 bool HasBreakPoint(int code_position);
4806 // Get the break point info object for a code position.
4807 Object* GetBreakPointInfo(int code_position);
4808 // Clear a break point.
4809 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
4810 int code_position,
4811 Handle<Object> break_point_object);
4812 // Set a break point.
4813 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
4814 int source_position, int statement_position,
4815 Handle<Object> break_point_object);
4816 // Get the break point objects for a code position.
4817 Object* GetBreakPointObjects(int code_position);
4818 // Find the break point info holding this break point object.
4819 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
4820 Handle<Object> break_point_object);
4821 // Get the number of break points for this function.
4822 int GetBreakPointCount();
4823
4824 static inline DebugInfo* cast(Object* obj);
4825
4826#ifdef DEBUG
4827 void DebugInfoPrint();
4828 void DebugInfoVerify();
4829#endif
4830
4831 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
4832 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
4833 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
4834 static const int kActiveBreakPointsCountIndex =
4835 kPatchedCodeIndex + kPointerSize;
4836 static const int kBreakPointsStateIndex =
4837 kActiveBreakPointsCountIndex + kPointerSize;
4838 static const int kSize = kBreakPointsStateIndex + kPointerSize;
4839
4840 private:
4841 static const int kNoBreakPointInfo = -1;
4842
4843 // Lookup the index in the break_points array for a code position.
4844 int GetBreakPointInfoIndex(int code_position);
4845
4846 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
4847};
4848
4849
4850// The BreakPointInfo class holds information for break points set in a
4851// function. The DebugInfo object holds a BreakPointInfo object for each code
4852// position with one or more break points.
4853class BreakPointInfo: public Struct {
4854 public:
4855 // The position in the code for the break point.
4856 DECL_ACCESSORS(code_position, Smi)
4857 // The position in the source for the break position.
4858 DECL_ACCESSORS(source_position, Smi)
4859 // The position in the source for the last statement before this break
4860 // position.
4861 DECL_ACCESSORS(statement_position, Smi)
4862 // List of related JavaScript break points.
4863 DECL_ACCESSORS(break_point_objects, Object)
4864
4865 // Removes a break point.
4866 static void ClearBreakPoint(Handle<BreakPointInfo> info,
4867 Handle<Object> break_point_object);
4868 // Set a break point.
4869 static void SetBreakPoint(Handle<BreakPointInfo> info,
4870 Handle<Object> break_point_object);
4871 // Check if break point info has this break point object.
4872 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
4873 Handle<Object> break_point_object);
4874 // Get the number of break points for this code position.
4875 int GetBreakPointCount();
4876
4877 static inline BreakPointInfo* cast(Object* obj);
4878
4879#ifdef DEBUG
4880 void BreakPointInfoPrint();
4881 void BreakPointInfoVerify();
4882#endif
4883
4884 static const int kCodePositionIndex = Struct::kHeaderSize;
4885 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
4886 static const int kStatementPositionIndex =
4887 kSourcePositionIndex + kPointerSize;
4888 static const int kBreakPointObjectsIndex =
4889 kStatementPositionIndex + kPointerSize;
4890 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
4891
4892 private:
4893 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
4894};
4895#endif // ENABLE_DEBUGGER_SUPPORT
4896
4897
4898#undef DECL_BOOLEAN_ACCESSORS
4899#undef DECL_ACCESSORS
4900
4901
4902// Abstract base class for visiting, and optionally modifying, the
4903// pointers contained in Objects. Used in GC and serialization/deserialization.
4904class ObjectVisitor BASE_EMBEDDED {
4905 public:
4906 virtual ~ObjectVisitor() {}
4907
4908 // Visits a contiguous arrays of pointers in the half-open range
4909 // [start, end). Any or all of the values may be modified on return.
4910 virtual void VisitPointers(Object** start, Object** end) = 0;
4911
4912 // To allow lazy clearing of inline caches the visitor has
4913 // a rich interface for iterating over Code objects..
4914
4915 // Visits a code target in the instruction stream.
4916 virtual void VisitCodeTarget(RelocInfo* rinfo);
4917
4918 // Visits a runtime entry in the instruction stream.
4919 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
4920
Steve Blockd0582a62009-12-15 09:54:21 +00004921 // Visits the resource of an ASCII or two-byte string.
4922 virtual void VisitExternalAsciiString(
4923 v8::String::ExternalAsciiStringResource** resource) {}
4924 virtual void VisitExternalTwoByteString(
4925 v8::String::ExternalStringResource** resource) {}
4926
Steve Blocka7e24c12009-10-30 11:49:00 +00004927 // Visits a debug call target in the instruction stream.
4928 virtual void VisitDebugTarget(RelocInfo* rinfo);
4929
4930 // Handy shorthand for visiting a single pointer.
4931 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
4932
4933 // Visits a contiguous arrays of external references (references to the C++
4934 // heap) in the half-open range [start, end). Any or all of the values
4935 // may be modified on return.
4936 virtual void VisitExternalReferences(Address* start, Address* end) {}
4937
4938 inline void VisitExternalReference(Address* p) {
4939 VisitExternalReferences(p, p + 1);
4940 }
4941
4942#ifdef DEBUG
4943 // Intended for serialization/deserialization checking: insert, or
4944 // check for the presence of, a tag at this position in the stream.
4945 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00004946#else
4947 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00004948#endif
4949};
4950
4951
4952// BooleanBit is a helper class for setting and getting a bit in an
4953// integer or Smi.
4954class BooleanBit : public AllStatic {
4955 public:
4956 static inline bool get(Smi* smi, int bit_position) {
4957 return get(smi->value(), bit_position);
4958 }
4959
4960 static inline bool get(int value, int bit_position) {
4961 return (value & (1 << bit_position)) != 0;
4962 }
4963
4964 static inline Smi* set(Smi* smi, int bit_position, bool v) {
4965 return Smi::FromInt(set(smi->value(), bit_position, v));
4966 }
4967
4968 static inline int set(int value, int bit_position, bool v) {
4969 if (v) {
4970 value |= (1 << bit_position);
4971 } else {
4972 value &= ~(1 << bit_position);
4973 }
4974 return value;
4975 }
4976};
4977
4978} } // namespace v8::internal
4979
4980#endif // V8_OBJECTS_H_