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