blob: 65e0f36cad52148f1d840933c498442dfa6134bb [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//
Kristian Monsen50ef84f2010-07-29 15:18:00 +010042// Most object types in the V8 JavaScript are described in this file.
Steve Blocka7e24c12009-10-30 11:49:00 +000043//
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
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010057// - JSValue
58// - ByteArray
59// - PixelArray
60// - ExternalArray
61// - ExternalByteArray
62// - ExternalUnsignedByteArray
63// - ExternalShortArray
64// - ExternalUnsignedShortArray
65// - ExternalIntArray
66// - ExternalUnsignedIntArray
67// - ExternalFloatArray
68// - FixedArray
69// - DescriptorArray
70// - HashTable
71// - Dictionary
72// - SymbolTable
73// - CompilationCacheTable
74// - CodeCacheHashTable
75// - MapCache
76// - Context
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010077// - JSFunctionResultCache
Kristian Monsen50ef84f2010-07-29 15:18:00 +010078// - SerializedScopeInfo
Steve Blocka7e24c12009-10-30 11:49:00 +000079// - String
80// - SeqString
81// - SeqAsciiString
82// - SeqTwoByteString
83// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +000084// - ExternalString
85// - ExternalAsciiString
86// - ExternalTwoByteString
87// - HeapNumber
88// - Code
89// - Map
90// - Oddball
91// - Proxy
92// - SharedFunctionInfo
93// - Struct
94// - AccessorInfo
95// - AccessCheckInfo
96// - InterceptorInfo
97// - CallHandlerInfo
98// - TemplateInfo
99// - FunctionTemplateInfo
100// - ObjectTemplateInfo
101// - Script
102// - SignatureInfo
103// - TypeSwitchInfo
104// - DebugInfo
105// - BreakPointInfo
Steve Block6ded16b2010-05-10 14:33:55 +0100106// - CodeCache
Steve Blocka7e24c12009-10-30 11:49:00 +0000107//
108// Formats of Object*:
109// Smi: [31 bit signed int] 0
110// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
111// Failure: [30 bit signed int] 11
112
113// Ecma-262 3rd 8.6.1
114enum PropertyAttributes {
115 NONE = v8::None,
116 READ_ONLY = v8::ReadOnly,
117 DONT_ENUM = v8::DontEnum,
118 DONT_DELETE = v8::DontDelete,
119 ABSENT = 16 // Used in runtime to indicate a property is absent.
120 // ABSENT can never be stored in or returned from a descriptor's attributes
121 // bitfield. It is only used as a return value meaning the attributes of
122 // a non-existent property.
123};
124
125namespace v8 {
126namespace internal {
127
128
129// PropertyDetails captures type and attributes for a property.
130// They are used both in property dictionaries and instance descriptors.
131class PropertyDetails BASE_EMBEDDED {
132 public:
133
134 PropertyDetails(PropertyAttributes attributes,
135 PropertyType type,
136 int index = 0) {
137 ASSERT(TypeField::is_valid(type));
138 ASSERT(AttributesField::is_valid(attributes));
139 ASSERT(IndexField::is_valid(index));
140
141 value_ = TypeField::encode(type)
142 | AttributesField::encode(attributes)
143 | IndexField::encode(index);
144
145 ASSERT(type == this->type());
146 ASSERT(attributes == this->attributes());
147 ASSERT(index == this->index());
148 }
149
150 // Conversion for storing details as Object*.
151 inline PropertyDetails(Smi* smi);
152 inline Smi* AsSmi();
153
154 PropertyType type() { return TypeField::decode(value_); }
155
156 bool IsTransition() {
157 PropertyType t = type();
158 ASSERT(t != INTERCEPTOR);
159 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION;
160 }
161
162 bool IsProperty() {
163 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
164 }
165
166 PropertyAttributes attributes() { return AttributesField::decode(value_); }
167
168 int index() { return IndexField::decode(value_); }
169
170 inline PropertyDetails AsDeleted();
171
172 static bool IsValidIndex(int index) { return IndexField::is_valid(index); }
173
174 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
175 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
176 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
177 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
178
179 // Bit fields in value_ (type, shift, size). Must be public so the
180 // constants can be embedded in generated code.
181 class TypeField: public BitField<PropertyType, 0, 3> {};
182 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
183 class DeletedField: public BitField<uint32_t, 6, 1> {};
Andrei Popescu402d9372010-02-26 13:31:12 +0000184 class IndexField: public BitField<uint32_t, 7, 32-7> {};
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
186 static const int kInitialIndex = 1;
187 private:
188 uint32_t value_;
189};
190
191
192// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
193enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
194
195
196// PropertyNormalizationMode is used to specify whether to keep
197// inobject properties when normalizing properties of a JSObject.
198enum PropertyNormalizationMode {
199 CLEAR_INOBJECT_PROPERTIES,
200 KEEP_INOBJECT_PROPERTIES
201};
202
203
204// All Maps have a field instance_type containing a InstanceType.
205// It describes the type of the instances.
206//
207// As an example, a JavaScript object is a heap object and its map
208// instance_type is JS_OBJECT_TYPE.
209//
210// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000211// mirror their encoding in the instance_type field of the map. The default
212// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
213// encoding is mentioned explicitly in the name. Likewise, the default
214// representation is considered sequential. It is not mentioned in the
215// name. The other representations (eg, CONS, EXTERNAL) are explicitly
216// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
217// symbol) or a STRING_TYPE (if it is not a symbol).
Steve Blocka7e24c12009-10-30 11:49:00 +0000218//
219// NOTE: The following things are some that depend on the string types having
220// instance_types that are less than those of all other types:
221// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
222// Object::IsString.
223//
224// NOTE: Everything following JS_VALUE_TYPE is considered a
225// JSObject for GC purposes. The first four entries here have typeof
226// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Steve Blockd0582a62009-12-15 09:54:21 +0000227#define INSTANCE_TYPE_LIST_ALL(V) \
228 V(SYMBOL_TYPE) \
229 V(ASCII_SYMBOL_TYPE) \
230 V(CONS_SYMBOL_TYPE) \
231 V(CONS_ASCII_SYMBOL_TYPE) \
232 V(EXTERNAL_SYMBOL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100233 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000234 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
235 V(STRING_TYPE) \
236 V(ASCII_STRING_TYPE) \
237 V(CONS_STRING_TYPE) \
238 V(CONS_ASCII_STRING_TYPE) \
239 V(EXTERNAL_STRING_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100240 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000241 V(EXTERNAL_ASCII_STRING_TYPE) \
242 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
243 \
244 V(MAP_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000245 V(CODE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000246 V(ODDBALL_TYPE) \
Iain Merrick75681382010-08-19 15:07:18 +0100247 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
Leon Clarkee46be812010-01-19 14:06:41 +0000248 \
249 V(HEAP_NUMBER_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000250 V(PROXY_TYPE) \
251 V(BYTE_ARRAY_TYPE) \
252 V(PIXEL_ARRAY_TYPE) \
253 /* Note: the order of these external array */ \
254 /* types is relied upon in */ \
255 /* Object::IsExternalArray(). */ \
256 V(EXTERNAL_BYTE_ARRAY_TYPE) \
257 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
258 V(EXTERNAL_SHORT_ARRAY_TYPE) \
259 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
260 V(EXTERNAL_INT_ARRAY_TYPE) \
261 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
262 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
263 V(FILLER_TYPE) \
264 \
265 V(ACCESSOR_INFO_TYPE) \
266 V(ACCESS_CHECK_INFO_TYPE) \
267 V(INTERCEPTOR_INFO_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000268 V(CALL_HANDLER_INFO_TYPE) \
269 V(FUNCTION_TEMPLATE_INFO_TYPE) \
270 V(OBJECT_TEMPLATE_INFO_TYPE) \
271 V(SIGNATURE_INFO_TYPE) \
272 V(TYPE_SWITCH_INFO_TYPE) \
273 V(SCRIPT_TYPE) \
Steve Block6ded16b2010-05-10 14:33:55 +0100274 V(CODE_CACHE_TYPE) \
Steve Blockd0582a62009-12-15 09:54:21 +0000275 \
Iain Merrick75681382010-08-19 15:07:18 +0100276 V(FIXED_ARRAY_TYPE) \
277 V(SHARED_FUNCTION_INFO_TYPE) \
278 \
Steve Blockd0582a62009-12-15 09:54:21 +0000279 V(JS_VALUE_TYPE) \
280 V(JS_OBJECT_TYPE) \
281 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
282 V(JS_GLOBAL_OBJECT_TYPE) \
283 V(JS_BUILTINS_OBJECT_TYPE) \
284 V(JS_GLOBAL_PROXY_TYPE) \
285 V(JS_ARRAY_TYPE) \
286 V(JS_REGEXP_TYPE) \
287 \
288 V(JS_FUNCTION_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000289
290#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000291#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
292 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000293 V(BREAK_POINT_INFO_TYPE)
294#else
295#define INSTANCE_TYPE_LIST_DEBUGGER(V)
296#endif
297
Steve Blockd0582a62009-12-15 09:54:21 +0000298#define INSTANCE_TYPE_LIST(V) \
299 INSTANCE_TYPE_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000300 INSTANCE_TYPE_LIST_DEBUGGER(V)
301
302
303// Since string types are not consecutive, this macro is used to
304// iterate over them.
305#define STRING_TYPE_LIST(V) \
Steve Blockd0582a62009-12-15 09:54:21 +0000306 V(SYMBOL_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 SeqTwoByteString::kAlignedSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000308 symbol, \
309 Symbol) \
310 V(ASCII_SYMBOL_TYPE, \
311 SeqAsciiString::kAlignedSize, \
312 ascii_symbol, \
313 AsciiSymbol) \
314 V(CONS_SYMBOL_TYPE, \
315 ConsString::kSize, \
316 cons_symbol, \
317 ConsSymbol) \
318 V(CONS_ASCII_SYMBOL_TYPE, \
319 ConsString::kSize, \
320 cons_ascii_symbol, \
321 ConsAsciiSymbol) \
322 V(EXTERNAL_SYMBOL_TYPE, \
323 ExternalTwoByteString::kSize, \
324 external_symbol, \
325 ExternalSymbol) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100326 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
327 ExternalTwoByteString::kSize, \
328 external_symbol_with_ascii_data, \
329 ExternalSymbolWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000330 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
331 ExternalAsciiString::kSize, \
332 external_ascii_symbol, \
333 ExternalAsciiSymbol) \
334 V(STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000335 SeqTwoByteString::kAlignedSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000336 string, \
337 String) \
338 V(ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000339 SeqAsciiString::kAlignedSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000340 ascii_string, \
341 AsciiString) \
342 V(CONS_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000343 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000344 cons_string, \
345 ConsString) \
346 V(CONS_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000347 ConsString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000348 cons_ascii_string, \
349 ConsAsciiString) \
350 V(EXTERNAL_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000351 ExternalTwoByteString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000352 external_string, \
353 ExternalString) \
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100354 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
355 ExternalTwoByteString::kSize, \
356 external_string_with_ascii_data, \
357 ExternalStringWithAsciiData) \
Steve Blockd0582a62009-12-15 09:54:21 +0000358 V(EXTERNAL_ASCII_STRING_TYPE, \
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 ExternalAsciiString::kSize, \
Steve Blockd0582a62009-12-15 09:54:21 +0000360 external_ascii_string, \
361 ExternalAsciiString) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000362
363// A struct is a simple object a set of object-valued fields. Including an
364// object type in this causes the compiler to generate most of the boilerplate
365// code for the class including allocation and garbage collection routines,
366// casts and predicates. All you need to define is the class, methods and
367// object verification routines. Easy, no?
368//
369// Note that for subtle reasons related to the ordering or numerical values of
370// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
371// manually.
Steve Blockd0582a62009-12-15 09:54:21 +0000372#define STRUCT_LIST_ALL(V) \
373 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
374 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
375 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
376 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
377 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
378 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
379 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
380 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
Steve Block6ded16b2010-05-10 14:33:55 +0100381 V(SCRIPT, Script, script) \
382 V(CODE_CACHE, CodeCache, code_cache)
Steve Blocka7e24c12009-10-30 11:49:00 +0000383
384#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blockd0582a62009-12-15 09:54:21 +0000385#define STRUCT_LIST_DEBUGGER(V) \
386 V(DEBUG_INFO, DebugInfo, debug_info) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000387 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
388#else
389#define STRUCT_LIST_DEBUGGER(V)
390#endif
391
Steve Blockd0582a62009-12-15 09:54:21 +0000392#define STRUCT_LIST(V) \
393 STRUCT_LIST_ALL(V) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000394 STRUCT_LIST_DEBUGGER(V)
395
396// We use the full 8 bits of the instance_type field to encode heap object
397// instance types. The high-order bit (bit 7) is set if the object is not a
398// string, and cleared if it is a string.
399const uint32_t kIsNotStringMask = 0x80;
400const uint32_t kStringTag = 0x0;
401const uint32_t kNotStringTag = 0x80;
402
Leon Clarkee46be812010-01-19 14:06:41 +0000403// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
404// There are not enough types that the non-string types (with bit 7 set) can
405// have bit 6 set too.
406const uint32_t kIsSymbolMask = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000407const uint32_t kNotSymbolTag = 0x0;
Leon Clarkee46be812010-01-19 14:06:41 +0000408const uint32_t kSymbolTag = 0x40;
Steve Blocka7e24c12009-10-30 11:49:00 +0000409
Steve Blocka7e24c12009-10-30 11:49:00 +0000410// If bit 7 is clear then bit 2 indicates whether the string consists of
411// two-byte characters or one-byte characters.
412const uint32_t kStringEncodingMask = 0x4;
413const uint32_t kTwoByteStringTag = 0x0;
414const uint32_t kAsciiStringTag = 0x4;
415
416// If bit 7 is clear, the low-order 2 bits indicate the representation
417// of the string.
418const uint32_t kStringRepresentationMask = 0x03;
419enum StringRepresentationTag {
420 kSeqStringTag = 0x0,
421 kConsStringTag = 0x1,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100422 kExternalStringTag = 0x2
Steve Blocka7e24c12009-10-30 11:49:00 +0000423};
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100424const uint32_t kIsConsStringMask = 0x1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000425
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100426// If bit 7 is clear, then bit 3 indicates whether this two-byte
427// string actually contains ascii data.
428const uint32_t kAsciiDataHintMask = 0x08;
429const uint32_t kAsciiDataHintTag = 0x08;
430
Steve Blocka7e24c12009-10-30 11:49:00 +0000431
432// A ConsString with an empty string as the right side is a candidate
433// for being shortcut by the garbage collector unless it is a
434// symbol. It's not common to have non-flat symbols, so we do not
435// shortcut them thereby avoiding turning symbols into strings. See
436// heap.cc and mark-compact.cc.
437const uint32_t kShortcutTypeMask =
438 kIsNotStringMask |
439 kIsSymbolMask |
440 kStringRepresentationMask;
441const uint32_t kShortcutTypeTag = kConsStringTag;
442
443
444enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000445 // String types.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100446 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000447 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100448 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000449 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100450 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
451 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
452 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000453 EXTERNAL_ASCII_SYMBOL_TYPE =
454 kAsciiStringTag | kSymbolTag | kExternalStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100455 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000456 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100457 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000458 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100459 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
460 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
461 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
Steve Blockd0582a62009-12-15 09:54:21 +0000462 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
463 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000464
Leon Clarkee46be812010-01-19 14:06:41 +0000465 // Objects allocated in their own spaces (never in new space).
466 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 CODE_TYPE,
468 ODDBALL_TYPE,
469 JS_GLOBAL_PROPERTY_CELL_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000470
471 // "Data", objects that cannot contain non-map-word pointers to heap
472 // objects.
473 HEAP_NUMBER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000474 PROXY_TYPE,
475 BYTE_ARRAY_TYPE,
476 PIXEL_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000477 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
Steve Block3ce2e202009-11-05 08:53:23 +0000478 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
479 EXTERNAL_SHORT_ARRAY_TYPE,
480 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
481 EXTERNAL_INT_ARRAY_TYPE,
482 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000483 EXTERNAL_FLOAT_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
484 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000485
Leon Clarkee46be812010-01-19 14:06:41 +0000486 // Structs.
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 ACCESSOR_INFO_TYPE,
488 ACCESS_CHECK_INFO_TYPE,
489 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000490 CALL_HANDLER_INFO_TYPE,
491 FUNCTION_TEMPLATE_INFO_TYPE,
492 OBJECT_TEMPLATE_INFO_TYPE,
493 SIGNATURE_INFO_TYPE,
494 TYPE_SWITCH_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000495 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100496 CODE_CACHE_TYPE,
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100497 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
498 // is defined. However as include/v8.h contain some of the instance type
499 // constants always having them avoids them getting different numbers
500 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
Steve Blocka7e24c12009-10-30 11:49:00 +0000501 DEBUG_INFO_TYPE,
502 BREAK_POINT_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000503
Leon Clarkee46be812010-01-19 14:06:41 +0000504 FIXED_ARRAY_TYPE,
505 SHARED_FUNCTION_INFO_TYPE,
506
507 JS_VALUE_TYPE, // FIRST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000508 JS_OBJECT_TYPE,
509 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
510 JS_GLOBAL_OBJECT_TYPE,
511 JS_BUILTINS_OBJECT_TYPE,
512 JS_GLOBAL_PROXY_TYPE,
513 JS_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000514 JS_REGEXP_TYPE, // LAST_JS_OBJECT_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000515
516 JS_FUNCTION_TYPE,
517
518 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000519 FIRST_TYPE = 0x0,
Steve Blocka7e24c12009-10-30 11:49:00 +0000520 LAST_TYPE = JS_FUNCTION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000521 INVALID_TYPE = FIRST_TYPE - 1,
522 FIRST_NONSTRING_TYPE = MAP_TYPE,
523 // Boundaries for testing for an external array.
524 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
525 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_FLOAT_ARRAY_TYPE,
526 // Boundary for promotion to old data space/old pointer space.
527 LAST_DATA_TYPE = FILLER_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000528 // Boundaries for testing the type is a JavaScript "object". Note that
529 // function objects are not counted as objects, even though they are
530 // implemented as such; only values whose typeof is "object" are included.
531 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
532 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE
533};
534
535
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100536STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
537STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
538STATIC_CHECK(PROXY_TYPE == Internals::kProxyType);
539
540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541enum CompareResult {
542 LESS = -1,
543 EQUAL = 0,
544 GREATER = 1,
545
546 NOT_EQUAL = GREATER
547};
548
549
550#define DECL_BOOLEAN_ACCESSORS(name) \
551 inline bool name(); \
552 inline void set_##name(bool value); \
553
554
555#define DECL_ACCESSORS(name, type) \
556 inline type* name(); \
557 inline void set_##name(type* value, \
558 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
559
560
561class StringStream;
562class ObjectVisitor;
563
564struct ValueInfo : public Malloced {
565 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
566 InstanceType type;
567 Object* ptr;
568 const char* str;
569 double number;
570};
571
572
573// A template-ized version of the IsXXX functions.
574template <class C> static inline bool Is(Object* obj);
575
576
577// Object is the abstract superclass for all classes in the
578// object hierarchy.
579// Object does not use any virtual functions to avoid the
580// allocation of the C++ vtable.
581// Since Smi and Failure are subclasses of Object no
582// data members can be present in Object.
583class Object BASE_EMBEDDED {
584 public:
585 // Type testing.
586 inline bool IsSmi();
587 inline bool IsHeapObject();
588 inline bool IsHeapNumber();
589 inline bool IsString();
590 inline bool IsSymbol();
Steve Blocka7e24c12009-10-30 11:49:00 +0000591 // See objects-inl.h for more details
592 inline bool IsSeqString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000593 inline bool IsExternalString();
594 inline bool IsExternalTwoByteString();
595 inline bool IsExternalAsciiString();
596 inline bool IsSeqTwoByteString();
597 inline bool IsSeqAsciiString();
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 inline bool IsConsString();
599
600 inline bool IsNumber();
601 inline bool IsByteArray();
602 inline bool IsPixelArray();
Steve Block3ce2e202009-11-05 08:53:23 +0000603 inline bool IsExternalArray();
604 inline bool IsExternalByteArray();
605 inline bool IsExternalUnsignedByteArray();
606 inline bool IsExternalShortArray();
607 inline bool IsExternalUnsignedShortArray();
608 inline bool IsExternalIntArray();
609 inline bool IsExternalUnsignedIntArray();
610 inline bool IsExternalFloatArray();
Steve Blocka7e24c12009-10-30 11:49:00 +0000611 inline bool IsFailure();
612 inline bool IsRetryAfterGC();
613 inline bool IsOutOfMemoryFailure();
614 inline bool IsException();
615 inline bool IsJSObject();
616 inline bool IsJSContextExtensionObject();
617 inline bool IsMap();
618 inline bool IsFixedArray();
619 inline bool IsDescriptorArray();
620 inline bool IsContext();
621 inline bool IsCatchContext();
622 inline bool IsGlobalContext();
623 inline bool IsJSFunction();
624 inline bool IsCode();
625 inline bool IsOddball();
626 inline bool IsSharedFunctionInfo();
627 inline bool IsJSValue();
628 inline bool IsStringWrapper();
629 inline bool IsProxy();
630 inline bool IsBoolean();
631 inline bool IsJSArray();
632 inline bool IsJSRegExp();
633 inline bool IsHashTable();
634 inline bool IsDictionary();
635 inline bool IsSymbolTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100636 inline bool IsJSFunctionResultCache();
Steve Blocka7e24c12009-10-30 11:49:00 +0000637 inline bool IsCompilationCacheTable();
Steve Block6ded16b2010-05-10 14:33:55 +0100638 inline bool IsCodeCacheHashTable();
Steve Blocka7e24c12009-10-30 11:49:00 +0000639 inline bool IsMapCache();
640 inline bool IsPrimitive();
641 inline bool IsGlobalObject();
642 inline bool IsJSGlobalObject();
643 inline bool IsJSBuiltinsObject();
644 inline bool IsJSGlobalProxy();
645 inline bool IsUndetectableObject();
646 inline bool IsAccessCheckNeeded();
647 inline bool IsJSGlobalPropertyCell();
648
649 // Returns true if this object is an instance of the specified
650 // function template.
651 inline bool IsInstanceOf(FunctionTemplateInfo* type);
652
653 inline bool IsStruct();
654#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
655 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
656#undef DECLARE_STRUCT_PREDICATE
657
658 // Oddball testing.
659 INLINE(bool IsUndefined());
660 INLINE(bool IsTheHole());
661 INLINE(bool IsNull());
662 INLINE(bool IsTrue());
663 INLINE(bool IsFalse());
664
665 // Extract the number.
666 inline double Number();
667
668 inline bool HasSpecificClassOf(String* name);
669
670 Object* ToObject(); // ECMA-262 9.9.
671 Object* ToBoolean(); // ECMA-262 9.2.
672
673 // Convert to a JSObject if needed.
674 // global_context is used when creating wrapper object.
675 Object* ToObject(Context* global_context);
676
677 // Converts this to a Smi if possible.
678 // Failure is returned otherwise.
679 inline Object* ToSmi();
680
681 void Lookup(String* name, LookupResult* result);
682
683 // Property access.
684 inline Object* GetProperty(String* key);
685 inline Object* GetProperty(String* key, PropertyAttributes* attributes);
686 Object* GetPropertyWithReceiver(Object* receiver,
687 String* key,
688 PropertyAttributes* attributes);
689 Object* GetProperty(Object* receiver,
690 LookupResult* result,
691 String* key,
692 PropertyAttributes* attributes);
693 Object* GetPropertyWithCallback(Object* receiver,
694 Object* structure,
695 String* name,
696 Object* holder);
697 Object* GetPropertyWithDefinedGetter(Object* receiver,
698 JSFunction* getter);
699
700 inline Object* GetElement(uint32_t index);
701 Object* GetElementWithReceiver(Object* receiver, uint32_t index);
702
703 // Return the object's prototype (might be Heap::null_value()).
704 Object* GetPrototype();
705
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100706 // Tries to convert an object to an array index. Returns true and sets
707 // the output parameter if it succeeds.
708 inline bool ToArrayIndex(uint32_t* index);
709
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 // Returns true if this is a JSValue containing a string and the index is
711 // < the length of the string. Used to implement [] on strings.
712 inline bool IsStringObjectWithCharacterAt(uint32_t index);
713
714#ifdef DEBUG
715 // Prints this object with details.
716 void Print();
717 void PrintLn();
718 // Verifies the object.
719 void Verify();
720
721 // Verify a pointer is a valid object pointer.
722 static void VerifyPointer(Object* p);
723#endif
724
725 // Prints this object without details.
726 void ShortPrint();
727
728 // Prints this object without details to a message accumulator.
729 void ShortPrint(StringStream* accumulator);
730
731 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
732 static Object* cast(Object* value) { return value; }
733
734 // Layout description.
735 static const int kHeaderSize = 0; // Object does not take up any space.
736
737 private:
738 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
739};
740
741
742// Smi represents integer Numbers that can be stored in 31 bits.
743// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +0000744// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +0000745// For long smis it has the following format:
746// [32 bit signed int] [31 bits zero padding] 0
747// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +0000748class Smi: public Object {
749 public:
750 // Returns the integer value.
751 inline int value();
752
753 // Convert a value to a Smi object.
754 static inline Smi* FromInt(int value);
755
756 static inline Smi* FromIntptr(intptr_t value);
757
758 // Returns whether value can be represented in a Smi.
759 static inline bool IsValid(intptr_t value);
760
Steve Blocka7e24c12009-10-30 11:49:00 +0000761 // Casting.
762 static inline Smi* cast(Object* object);
763
764 // Dispatched behavior.
765 void SmiPrint();
766 void SmiPrint(StringStream* accumulator);
767#ifdef DEBUG
768 void SmiVerify();
769#endif
770
Steve Block3ce2e202009-11-05 08:53:23 +0000771 static const int kMinValue = (-1 << (kSmiValueSize - 1));
772 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000773
774 private:
775 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
776};
777
778
779// Failure is used for reporting out of memory situations and
780// propagating exceptions through the runtime system. Failure objects
781// are transient and cannot occur as part of the object graph.
782//
783// Failures are a single word, encoded as follows:
784// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000785// |...rrrrrrrrrrrrrrrrrrrrrr|sss|tt|11|
Steve Blocka7e24c12009-10-30 11:49:00 +0000786// +-------------------------+---+--+--+
Steve Block3ce2e202009-11-05 08:53:23 +0000787// 7 6 4 32 10
788//
Steve Blocka7e24c12009-10-30 11:49:00 +0000789//
790// The low two bits, 0-1, are the failure tag, 11. The next two bits,
791// 2-3, are a failure type tag 'tt' with possible values:
792// 00 RETRY_AFTER_GC
793// 01 EXCEPTION
794// 10 INTERNAL_ERROR
795// 11 OUT_OF_MEMORY_EXCEPTION
796//
797// The next three bits, 4-6, are an allocation space tag 'sss'. The
798// allocation space tag is 000 for all failure types except
799// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
800// allocation spaces (the encoding is found in globals.h).
801//
802// The remaining bits is the size of the allocation request in units
803// of the pointer size, and is zeroed except for RETRY_AFTER_GC
804// failures. The 25 bits (on a 32 bit platform) gives a representable
805// range of 2^27 bytes (128MB).
806
807// Failure type tag info.
808const int kFailureTypeTagSize = 2;
809const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
810
811class Failure: public Object {
812 public:
813 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
814 enum Type {
815 RETRY_AFTER_GC = 0,
816 EXCEPTION = 1, // Returning this marker tells the real exception
817 // is in Top::pending_exception.
818 INTERNAL_ERROR = 2,
819 OUT_OF_MEMORY_EXCEPTION = 3
820 };
821
822 inline Type type() const;
823
824 // Returns the space that needs to be collected for RetryAfterGC failures.
825 inline AllocationSpace allocation_space() const;
826
827 // Returns the number of bytes requested (up to the representable maximum)
828 // for RetryAfterGC failures.
829 inline int requested() const;
830
831 inline bool IsInternalError() const;
832 inline bool IsOutOfMemoryException() const;
833
834 static Failure* RetryAfterGC(int requested_bytes, AllocationSpace space);
835 static inline Failure* RetryAfterGC(int requested_bytes); // NEW_SPACE
836 static inline Failure* Exception();
837 static inline Failure* InternalError();
838 static inline Failure* OutOfMemoryException();
839 // Casting.
840 static inline Failure* cast(Object* object);
841
842 // Dispatched behavior.
843 void FailurePrint();
844 void FailurePrint(StringStream* accumulator);
845#ifdef DEBUG
846 void FailureVerify();
847#endif
848
849 private:
Steve Block3ce2e202009-11-05 08:53:23 +0000850 inline intptr_t value() const;
851 static inline Failure* Construct(Type type, intptr_t value = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000852
853 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
854};
855
856
857// Heap objects typically have a map pointer in their first word. However,
858// during GC other data (eg, mark bits, forwarding addresses) is sometimes
859// encoded in the first word. The class MapWord is an abstraction of the
860// value in a heap object's first word.
861class MapWord BASE_EMBEDDED {
862 public:
863 // Normal state: the map word contains a map pointer.
864
865 // Create a map word from a map pointer.
866 static inline MapWord FromMap(Map* map);
867
868 // View this map word as a map pointer.
869 inline Map* ToMap();
870
871
872 // Scavenge collection: the map word of live objects in the from space
873 // contains a forwarding address (a heap object pointer in the to space).
874
875 // True if this map word is a forwarding address for a scavenge
876 // collection. Only valid during a scavenge collection (specifically,
877 // when all map words are heap object pointers, ie. not during a full GC).
878 inline bool IsForwardingAddress();
879
880 // Create a map word from a forwarding address.
881 static inline MapWord FromForwardingAddress(HeapObject* object);
882
883 // View this map word as a forwarding address.
884 inline HeapObject* ToForwardingAddress();
885
Steve Blocka7e24c12009-10-30 11:49:00 +0000886 // Marking phase of full collection: the map word of live objects is
887 // marked, and may be marked as overflowed (eg, the object is live, its
888 // children have not been visited, and it does not fit in the marking
889 // stack).
890
891 // True if this map word's mark bit is set.
892 inline bool IsMarked();
893
894 // Return this map word but with its mark bit set.
895 inline void SetMark();
896
897 // Return this map word but with its mark bit cleared.
898 inline void ClearMark();
899
900 // True if this map word's overflow bit is set.
901 inline bool IsOverflowed();
902
903 // Return this map word but with its overflow bit set.
904 inline void SetOverflow();
905
906 // Return this map word but with its overflow bit cleared.
907 inline void ClearOverflow();
908
909
910 // Compacting phase of a full compacting collection: the map word of live
911 // objects contains an encoding of the original map address along with the
912 // forwarding address (represented as an offset from the first live object
913 // in the same page as the (old) object address).
914
915 // Create a map word from a map address and a forwarding address offset.
916 static inline MapWord EncodeAddress(Address map_address, int offset);
917
918 // Return the map address encoded in this map word.
919 inline Address DecodeMapAddress(MapSpace* map_space);
920
921 // Return the forwarding offset encoded in this map word.
922 inline int DecodeOffset();
923
924
925 // During serialization: the map word is used to hold an encoded
926 // address, and possibly a mark bit (set and cleared with SetMark
927 // and ClearMark).
928
929 // Create a map word from an encoded address.
930 static inline MapWord FromEncodedAddress(Address address);
931
932 inline Address ToEncodedAddress();
933
934 // Bits used by the marking phase of the garbage collector.
935 //
936 // The first word of a heap object is normally a map pointer. The last two
937 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
938 // mark an object as live and/or overflowed:
939 // last bit = 0, marked as alive
940 // second bit = 1, overflowed
941 // An object is only marked as overflowed when it is marked as live while
942 // the marking stack is overflowed.
943 static const int kMarkingBit = 0; // marking bit
944 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
945 static const int kOverflowBit = 1; // overflow bit
946 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
947
Leon Clarkee46be812010-01-19 14:06:41 +0000948 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
949 // used.
Steve Blocka7e24c12009-10-30 11:49:00 +0000950 // +-----------------+------------------+-----------------+
951 // |forwarding offset|page offset of map|page index of map|
952 // +-----------------+------------------+-----------------+
Leon Clarkee46be812010-01-19 14:06:41 +0000953 // ^ ^ ^
954 // | | |
955 // | | kMapPageIndexBits
956 // | kMapPageOffsetBits
957 // kForwardingOffsetBits
958 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
959 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
960#ifdef V8_HOST_ARCH_64_BIT
961 static const int kMapPageIndexBits = 16;
962#else
963 // Use all the 32-bits to encode on a 32-bit platform.
964 static const int kMapPageIndexBits =
965 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
966#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000967
968 static const int kMapPageIndexShift = 0;
969 static const int kMapPageOffsetShift =
970 kMapPageIndexShift + kMapPageIndexBits;
971 static const int kForwardingOffsetShift =
972 kMapPageOffsetShift + kMapPageOffsetBits;
973
Leon Clarkee46be812010-01-19 14:06:41 +0000974 // Bit masks covering the different parts the encoding.
975 static const uintptr_t kMapPageIndexMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000976 (1 << kMapPageOffsetShift) - 1;
Leon Clarkee46be812010-01-19 14:06:41 +0000977 static const uintptr_t kMapPageOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000978 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
Leon Clarkee46be812010-01-19 14:06:41 +0000979 static const uintptr_t kForwardingOffsetMask =
Steve Blocka7e24c12009-10-30 11:49:00 +0000980 ~(kMapPageIndexMask | kMapPageOffsetMask);
981
982 private:
983 // HeapObject calls the private constructor and directly reads the value.
984 friend class HeapObject;
985
986 explicit MapWord(uintptr_t value) : value_(value) {}
987
988 uintptr_t value_;
989};
990
991
992// HeapObject is the superclass for all classes describing heap allocated
993// objects.
994class HeapObject: public Object {
995 public:
996 // [map]: Contains a map which contains the object's reflective
997 // information.
998 inline Map* map();
999 inline void set_map(Map* value);
1000
1001 // During garbage collection, the map word of a heap object does not
1002 // necessarily contain a map pointer.
1003 inline MapWord map_word();
1004 inline void set_map_word(MapWord map_word);
1005
1006 // Converts an address to a HeapObject pointer.
1007 static inline HeapObject* FromAddress(Address address);
1008
1009 // Returns the address of this HeapObject.
1010 inline Address address();
1011
1012 // Iterates over pointers contained in the object (including the Map)
1013 void Iterate(ObjectVisitor* v);
1014
1015 // Iterates over all pointers contained in the object except the
1016 // first map pointer. The object type is given in the first
1017 // parameter. This function does not access the map pointer in the
1018 // object, and so is safe to call while the map pointer is modified.
1019 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1020
1021 // This method only applies to struct objects. Iterates over all the fields
1022 // of this struct.
1023 void IterateStructBody(int object_size, ObjectVisitor* v);
1024
1025 // Returns the heap object's size in bytes
1026 inline int Size();
1027
1028 // Given a heap object's map pointer, returns the heap size in bytes
1029 // Useful when the map pointer field is used for other purposes.
1030 // GC internal.
1031 inline int SizeFromMap(Map* map);
1032
1033 // Support for the marking heap objects during the marking phase of GC.
1034 // True if the object is marked live.
1035 inline bool IsMarked();
1036
1037 // Mutate this object's map pointer to indicate that the object is live.
1038 inline void SetMark();
1039
1040 // Mutate this object's map pointer to remove the indication that the
1041 // object is live (ie, partially restore the map pointer).
1042 inline void ClearMark();
1043
1044 // True if this object is marked as overflowed. Overflowed objects have
1045 // been reached and marked during marking of the heap, but their children
1046 // have not necessarily been marked and they have not been pushed on the
1047 // marking stack.
1048 inline bool IsOverflowed();
1049
1050 // Mutate this object's map pointer to indicate that the object is
1051 // overflowed.
1052 inline void SetOverflow();
1053
1054 // Mutate this object's map pointer to remove the indication that the
1055 // object is overflowed (ie, partially restore the map pointer).
1056 inline void ClearOverflow();
1057
1058 // Returns the field at offset in obj, as a read/write Object* reference.
1059 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001060 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 // during marking GC.
1062 static inline Object** RawField(HeapObject* obj, int offset);
1063
1064 // Casting.
1065 static inline HeapObject* cast(Object* obj);
1066
Leon Clarke4515c472010-02-03 11:58:03 +00001067 // Return the write barrier mode for this. Callers of this function
1068 // must be able to present a reference to an AssertNoAllocation
1069 // object as a sign that they are not going to use this function
1070 // from code that allocates and thus invalidates the returned write
1071 // barrier mode.
1072 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
Steve Blocka7e24c12009-10-30 11:49:00 +00001073
1074 // Dispatched behavior.
1075 void HeapObjectShortPrint(StringStream* accumulator);
1076#ifdef DEBUG
1077 void HeapObjectPrint();
1078 void HeapObjectVerify();
1079 inline void VerifyObjectField(int offset);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001080 inline void VerifySmiField(int offset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001081
1082 void PrintHeader(const char* id);
1083
1084 // Verify a pointer is a valid HeapObject pointer that points to object
1085 // areas in the heap.
1086 static void VerifyHeapPointer(Object* p);
1087#endif
1088
1089 // Layout description.
1090 // First field in a heap object is map.
1091 static const int kMapOffset = Object::kHeaderSize;
1092 static const int kHeaderSize = kMapOffset + kPointerSize;
1093
1094 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1095
1096 protected:
1097 // helpers for calling an ObjectVisitor to iterate over pointers in the
1098 // half-open range [start, end) specified as integer offsets
1099 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1100 // as above, for the single element at "offset"
1101 inline void IteratePointer(ObjectVisitor* v, int offset);
1102
1103 // Computes the object size from the map.
1104 // Should only be used from SizeFromMap.
1105 int SlowSizeFromMap(Map* map);
1106
1107 private:
1108 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1109};
1110
1111
Iain Merrick75681382010-08-19 15:07:18 +01001112#define SLOT_ADDR(obj, offset) \
1113 reinterpret_cast<Object**>((obj)->address() + offset)
1114
1115// This class describes a body of an object of a fixed size
1116// in which all pointer fields are located in the [start_offset, end_offset)
1117// interval.
1118template<int start_offset, int end_offset, int size>
1119class FixedBodyDescriptor {
1120 public:
1121 static const int kStartOffset = start_offset;
1122 static const int kEndOffset = end_offset;
1123 static const int kSize = size;
1124
1125 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1126
1127 template<typename StaticVisitor>
1128 static inline void IterateBody(HeapObject* obj) {
1129 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1130 SLOT_ADDR(obj, end_offset));
1131 }
1132};
1133
1134
1135// This class describes a body of an object of a variable size
1136// in which all pointer fields are located in the [start_offset, object_size)
1137// interval.
1138template<int start_offset>
1139class FlexibleBodyDescriptor {
1140 public:
1141 static const int kStartOffset = start_offset;
1142
1143 static inline void IterateBody(HeapObject* obj,
1144 int object_size,
1145 ObjectVisitor* v);
1146
1147 template<typename StaticVisitor>
1148 static inline void IterateBody(HeapObject* obj, int object_size) {
1149 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1150 SLOT_ADDR(obj, object_size));
1151 }
1152};
1153
1154#undef SLOT_ADDR
1155
1156
Steve Blocka7e24c12009-10-30 11:49:00 +00001157// The HeapNumber class describes heap allocated numbers that cannot be
1158// represented in a Smi (small integer)
1159class HeapNumber: public HeapObject {
1160 public:
1161 // [value]: number value.
1162 inline double value();
1163 inline void set_value(double value);
1164
1165 // Casting.
1166 static inline HeapNumber* cast(Object* obj);
1167
1168 // Dispatched behavior.
1169 Object* HeapNumberToBoolean();
1170 void HeapNumberPrint();
1171 void HeapNumberPrint(StringStream* accumulator);
1172#ifdef DEBUG
1173 void HeapNumberVerify();
1174#endif
1175
Steve Block6ded16b2010-05-10 14:33:55 +01001176 inline int get_exponent();
1177 inline int get_sign();
1178
Steve Blocka7e24c12009-10-30 11:49:00 +00001179 // Layout description.
1180 static const int kValueOffset = HeapObject::kHeaderSize;
1181 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1182 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1183 // little endian apart from non-EABI arm which is little endian with big
1184 // endian floating point word ordering!
Steve Block3ce2e202009-11-05 08:53:23 +00001185#if !defined(V8_HOST_ARCH_ARM) || defined(USE_ARM_EABI)
Steve Blocka7e24c12009-10-30 11:49:00 +00001186 static const int kMantissaOffset = kValueOffset;
1187 static const int kExponentOffset = kValueOffset + 4;
1188#else
1189 static const int kMantissaOffset = kValueOffset + 4;
1190 static const int kExponentOffset = kValueOffset;
1191# define BIG_ENDIAN_FLOATING_POINT 1
1192#endif
1193 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001194 static const uint32_t kSignMask = 0x80000000u;
1195 static const uint32_t kExponentMask = 0x7ff00000u;
1196 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001197 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001198 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001199 static const int kExponentBias = 1023;
1200 static const int kExponentShift = 20;
1201 static const int kMantissaBitsInTopWord = 20;
1202 static const int kNonMantissaBitsInTopWord = 12;
1203
1204 private:
1205 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1206};
1207
1208
1209// The JSObject describes real heap allocated JavaScript objects with
1210// properties.
1211// Note that the map of JSObject changes during execution to enable inline
1212// caching.
1213class JSObject: public HeapObject {
1214 public:
1215 enum DeleteMode { NORMAL_DELETION, FORCE_DELETION };
1216 enum ElementsKind {
Iain Merrick75681382010-08-19 15:07:18 +01001217 // The only "fast" kind.
Steve Blocka7e24c12009-10-30 11:49:00 +00001218 FAST_ELEMENTS,
Iain Merrick75681382010-08-19 15:07:18 +01001219 // All the kinds below are "slow".
Steve Blocka7e24c12009-10-30 11:49:00 +00001220 DICTIONARY_ELEMENTS,
Steve Block3ce2e202009-11-05 08:53:23 +00001221 PIXEL_ELEMENTS,
1222 EXTERNAL_BYTE_ELEMENTS,
1223 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1224 EXTERNAL_SHORT_ELEMENTS,
1225 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1226 EXTERNAL_INT_ELEMENTS,
1227 EXTERNAL_UNSIGNED_INT_ELEMENTS,
1228 EXTERNAL_FLOAT_ELEMENTS
Steve Blocka7e24c12009-10-30 11:49:00 +00001229 };
1230
1231 // [properties]: Backing storage for properties.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001232 // properties is a FixedArray in the fast case and a Dictionary in the
Steve Blocka7e24c12009-10-30 11:49:00 +00001233 // slow case.
1234 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1235 inline void initialize_properties();
1236 inline bool HasFastProperties();
1237 inline StringDictionary* property_dictionary(); // Gets slow properties.
1238
1239 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01001240 //
1241 // Elements can be in two general modes: fast and slow. Each mode
1242 // corrensponds to a set of object representations of elements that
1243 // have something in common.
1244 //
1245 // In the fast mode elements is a FixedArray and so each element can
1246 // be quickly accessed. This fact is used in the generated code. The
1247 // elements array can have one of the two maps in this mode:
1248 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1249 // arrays). In the latter case the elements array may be shared by a
1250 // few objects and so before writing to any element the array must
1251 // be copied. Use EnsureWritableFastElements in this case.
1252 //
1253 // In the slow mode elements is either a NumberDictionary or a
1254 // PixelArray or an ExternalArray.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001255 DECL_ACCESSORS(elements, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001256 inline void initialize_elements();
Steve Block8defd9f2010-07-08 12:39:36 +01001257 inline Object* ResetElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001258 inline ElementsKind GetElementsKind();
1259 inline bool HasFastElements();
1260 inline bool HasDictionaryElements();
1261 inline bool HasPixelElements();
Steve Block3ce2e202009-11-05 08:53:23 +00001262 inline bool HasExternalArrayElements();
1263 inline bool HasExternalByteElements();
1264 inline bool HasExternalUnsignedByteElements();
1265 inline bool HasExternalShortElements();
1266 inline bool HasExternalUnsignedShortElements();
1267 inline bool HasExternalIntElements();
1268 inline bool HasExternalUnsignedIntElements();
1269 inline bool HasExternalFloatElements();
Steve Block6ded16b2010-05-10 14:33:55 +01001270 inline bool AllowsSetElementsLength();
Steve Blocka7e24c12009-10-30 11:49:00 +00001271 inline NumberDictionary* element_dictionary(); // Gets slow elements.
Iain Merrick75681382010-08-19 15:07:18 +01001272 // Requires: this->HasFastElements().
1273 inline Object* EnsureWritableFastElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00001274
1275 // Collects elements starting at index 0.
1276 // Undefined values are placed after non-undefined values.
1277 // Returns the number of non-undefined values.
1278 Object* PrepareElementsForSort(uint32_t limit);
1279 // As PrepareElementsForSort, but only on objects where elements is
1280 // a dictionary, and it will stay a dictionary.
1281 Object* PrepareSlowElementsForSort(uint32_t limit);
1282
1283 Object* SetProperty(String* key,
1284 Object* value,
1285 PropertyAttributes attributes);
1286 Object* SetProperty(LookupResult* result,
1287 String* key,
1288 Object* value,
1289 PropertyAttributes attributes);
1290 Object* SetPropertyWithFailedAccessCheck(LookupResult* result,
1291 String* name,
1292 Object* value);
1293 Object* SetPropertyWithCallback(Object* structure,
1294 String* name,
1295 Object* value,
1296 JSObject* holder);
1297 Object* SetPropertyWithDefinedSetter(JSFunction* setter,
1298 Object* value);
1299 Object* SetPropertyWithInterceptor(String* name,
1300 Object* value,
1301 PropertyAttributes attributes);
1302 Object* SetPropertyPostInterceptor(String* name,
1303 Object* value,
1304 PropertyAttributes attributes);
1305 Object* IgnoreAttributesAndSetLocalProperty(String* key,
1306 Object* value,
1307 PropertyAttributes attributes);
1308
1309 // Retrieve a value in a normalized object given a lookup result.
1310 // Handles the special representation of JS global objects.
1311 Object* GetNormalizedProperty(LookupResult* result);
1312
1313 // Sets the property value in a normalized object given a lookup result.
1314 // Handles the special representation of JS global objects.
1315 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1316
1317 // Sets the property value in a normalized object given (key, value, details).
1318 // Handles the special representation of JS global objects.
1319 Object* SetNormalizedProperty(String* name,
1320 Object* value,
1321 PropertyDetails details);
1322
1323 // Deletes the named property in a normalized object.
1324 Object* DeleteNormalizedProperty(String* name, DeleteMode mode);
1325
Steve Blocka7e24c12009-10-30 11:49:00 +00001326 // Returns the class name ([[Class]] property in the specification).
1327 String* class_name();
1328
1329 // Returns the constructor name (the name (possibly, inferred name) of the
1330 // function that was used to instantiate the object).
1331 String* constructor_name();
1332
1333 // Retrieve interceptors.
1334 InterceptorInfo* GetNamedInterceptor();
1335 InterceptorInfo* GetIndexedInterceptor();
1336
1337 inline PropertyAttributes GetPropertyAttribute(String* name);
1338 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1339 String* name);
1340 PropertyAttributes GetLocalPropertyAttribute(String* name);
1341
1342 Object* DefineAccessor(String* name, bool is_getter, JSFunction* fun,
1343 PropertyAttributes attributes);
1344 Object* LookupAccessor(String* name, bool is_getter);
1345
Leon Clarkef7060e22010-06-03 12:02:55 +01001346 Object* DefineAccessor(AccessorInfo* info);
1347
Steve Blocka7e24c12009-10-30 11:49:00 +00001348 // Used from Object::GetProperty().
1349 Object* GetPropertyWithFailedAccessCheck(Object* receiver,
1350 LookupResult* result,
1351 String* name,
1352 PropertyAttributes* attributes);
1353 Object* GetPropertyWithInterceptor(JSObject* receiver,
1354 String* name,
1355 PropertyAttributes* attributes);
1356 Object* GetPropertyPostInterceptor(JSObject* receiver,
1357 String* name,
1358 PropertyAttributes* attributes);
Steve Blockd0582a62009-12-15 09:54:21 +00001359 Object* GetLocalPropertyPostInterceptor(JSObject* receiver,
1360 String* name,
1361 PropertyAttributes* attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001362
1363 // Returns true if this is an instance of an api function and has
1364 // been modified since it was created. May give false positives.
1365 bool IsDirty();
1366
1367 bool HasProperty(String* name) {
1368 return GetPropertyAttribute(name) != ABSENT;
1369 }
1370
1371 // Can cause a GC if it hits an interceptor.
1372 bool HasLocalProperty(String* name) {
1373 return GetLocalPropertyAttribute(name) != ABSENT;
1374 }
1375
Steve Blockd0582a62009-12-15 09:54:21 +00001376 // If the receiver is a JSGlobalProxy this method will return its prototype,
1377 // otherwise the result is the receiver itself.
1378 inline Object* BypassGlobalProxy();
1379
1380 // Accessors for hidden properties object.
1381 //
1382 // Hidden properties are not local properties of the object itself.
1383 // Instead they are stored on an auxiliary JSObject stored as a local
1384 // property with a special name Heap::hidden_symbol(). But if the
1385 // receiver is a JSGlobalProxy then the auxiliary object is a property
1386 // of its prototype.
1387 //
1388 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1389 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1390 // holder.
1391 //
1392 // These accessors do not touch interceptors or accessors.
1393 inline bool HasHiddenPropertiesObject();
1394 inline Object* GetHiddenPropertiesObject();
1395 inline Object* SetHiddenPropertiesObject(Object* hidden_obj);
1396
Steve Blocka7e24c12009-10-30 11:49:00 +00001397 Object* DeleteProperty(String* name, DeleteMode mode);
1398 Object* DeleteElement(uint32_t index, DeleteMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00001399
1400 // Tests for the fast common case for property enumeration.
1401 bool IsSimpleEnum();
1402
1403 // Do we want to keep the elements in fast case when increasing the
1404 // capacity?
1405 bool ShouldConvertToSlowElements(int new_capacity);
1406 // Returns true if the backing storage for the slow-case elements of
1407 // this object takes up nearly as much space as a fast-case backing
1408 // storage would. In that case the JSObject should have fast
1409 // elements.
1410 bool ShouldConvertToFastElements();
1411
1412 // Return the object's prototype (might be Heap::null_value()).
1413 inline Object* GetPrototype();
1414
Andrei Popescu402d9372010-02-26 13:31:12 +00001415 // Set the object's prototype (only JSObject and null are allowed).
1416 Object* SetPrototype(Object* value, bool skip_hidden_prototypes);
1417
Steve Blocka7e24c12009-10-30 11:49:00 +00001418 // Tells whether the index'th element is present.
1419 inline bool HasElement(uint32_t index);
1420 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
1421 bool HasLocalElement(uint32_t index);
1422
1423 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1424 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1425
1426 Object* SetFastElement(uint32_t index, Object* value);
1427
1428 // Set the index'th array element.
1429 // A Failure object is returned if GC is needed.
1430 Object* SetElement(uint32_t index, Object* value);
1431
1432 // Returns the index'th element.
1433 // The undefined object if index is out of bounds.
1434 Object* GetElementWithReceiver(JSObject* receiver, uint32_t index);
Steve Block8defd9f2010-07-08 12:39:36 +01001435 Object* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001436
Steve Block8defd9f2010-07-08 12:39:36 +01001437 Object* SetFastElementsCapacityAndLength(int capacity, int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00001438 Object* SetSlowElements(Object* length);
1439
1440 // Lookup interceptors are used for handling properties controlled by host
1441 // objects.
1442 inline bool HasNamedInterceptor();
1443 inline bool HasIndexedInterceptor();
1444
1445 // Support functions for v8 api (needed for correct interceptor behavior).
1446 bool HasRealNamedProperty(String* key);
1447 bool HasRealElementProperty(uint32_t index);
1448 bool HasRealNamedCallbackProperty(String* key);
1449
1450 // Initializes the array to a certain length
1451 Object* SetElementsLength(Object* length);
1452
1453 // Get the header size for a JSObject. Used to compute the index of
1454 // internal fields as well as the number of internal fields.
1455 inline int GetHeaderSize();
1456
1457 inline int GetInternalFieldCount();
1458 inline Object* GetInternalField(int index);
1459 inline void SetInternalField(int index, Object* value);
1460
1461 // Lookup a property. If found, the result is valid and has
1462 // detailed information.
1463 void LocalLookup(String* name, LookupResult* result);
1464 void Lookup(String* name, LookupResult* result);
1465
1466 // The following lookup functions skip interceptors.
1467 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1468 void LookupRealNamedProperty(String* name, LookupResult* result);
1469 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1470 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
Leon Clarkef7060e22010-06-03 12:02:55 +01001471 bool SetElementWithCallbackSetterInPrototypes(uint32_t index, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001472 void LookupCallback(String* name, LookupResult* result);
1473
1474 // Returns the number of properties on this object filtering out properties
1475 // with the specified attributes (ignoring interceptors).
1476 int NumberOfLocalProperties(PropertyAttributes filter);
1477 // Returns the number of enumerable properties (ignoring interceptors).
1478 int NumberOfEnumProperties();
1479 // Fill in details for properties into storage starting at the specified
1480 // index.
1481 void GetLocalPropertyNames(FixedArray* storage, int index);
1482
1483 // Returns the number of properties on this object filtering out properties
1484 // with the specified attributes (ignoring interceptors).
1485 int NumberOfLocalElements(PropertyAttributes filter);
1486 // Returns the number of enumerable elements (ignoring interceptors).
1487 int NumberOfEnumElements();
1488 // Returns the number of elements on this object filtering out elements
1489 // with the specified attributes (ignoring interceptors).
1490 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1491 // Count and fill in the enumerable elements into storage.
1492 // (storage->length() == NumberOfEnumElements()).
1493 // If storage is NULL, will count the elements without adding
1494 // them to any storage.
1495 // Returns the number of enumerable elements.
1496 int GetEnumElementKeys(FixedArray* storage);
1497
1498 // Add a property to a fast-case object using a map transition to
1499 // new_map.
1500 Object* AddFastPropertyUsingMap(Map* new_map,
1501 String* name,
1502 Object* value);
1503
1504 // Add a constant function property to a fast-case object.
1505 // This leaves a CONSTANT_TRANSITION in the old map, and
1506 // if it is called on a second object with this map, a
1507 // normal property is added instead, with a map transition.
1508 // This avoids the creation of many maps with the same constant
1509 // function, all orphaned.
1510 Object* AddConstantFunctionProperty(String* name,
1511 JSFunction* function,
1512 PropertyAttributes attributes);
1513
1514 Object* ReplaceSlowProperty(String* name,
1515 Object* value,
1516 PropertyAttributes attributes);
1517
1518 // Converts a descriptor of any other type to a real field,
1519 // backed by the properties array. Descriptors of visible
1520 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1521 // Converts the descriptor on the original object's map to a
1522 // map transition, and the the new field is on the object's new map.
1523 Object* ConvertDescriptorToFieldAndMapTransition(
1524 String* name,
1525 Object* new_value,
1526 PropertyAttributes attributes);
1527
1528 // Converts a descriptor of any other type to a real field,
1529 // backed by the properties array. Descriptors of visible
1530 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1531 Object* ConvertDescriptorToField(String* name,
1532 Object* new_value,
1533 PropertyAttributes attributes);
1534
1535 // Add a property to a fast-case object.
1536 Object* AddFastProperty(String* name,
1537 Object* value,
1538 PropertyAttributes attributes);
1539
1540 // Add a property to a slow-case object.
1541 Object* AddSlowProperty(String* name,
1542 Object* value,
1543 PropertyAttributes attributes);
1544
1545 // Add a property to an object.
1546 Object* AddProperty(String* name,
1547 Object* value,
1548 PropertyAttributes attributes);
1549
1550 // Convert the object to use the canonical dictionary
1551 // representation. If the object is expected to have additional properties
1552 // added this number can be indicated to have the backing store allocated to
1553 // an initial capacity for holding these properties.
1554 Object* NormalizeProperties(PropertyNormalizationMode mode,
1555 int expected_additional_properties);
1556 Object* NormalizeElements();
1557
1558 // Transform slow named properties to fast variants.
1559 // Returns failure if allocation failed.
1560 Object* TransformToFastProperties(int unused_property_fields);
1561
1562 // Access fast-case object properties at index.
1563 inline Object* FastPropertyAt(int index);
1564 inline Object* FastPropertyAtPut(int index, Object* value);
1565
1566 // Access to in object properties.
1567 inline Object* InObjectPropertyAt(int index);
1568 inline Object* InObjectPropertyAtPut(int index,
1569 Object* value,
1570 WriteBarrierMode mode
1571 = UPDATE_WRITE_BARRIER);
1572
1573 // initializes the body after properties slot, properties slot is
1574 // initialized by set_properties
1575 // Note: this call does not update write barrier, it is caller's
1576 // reponsibility to ensure that *v* can be collected without WB here.
1577 inline void InitializeBody(int object_size);
1578
1579 // Check whether this object references another object
1580 bool ReferencesObject(Object* obj);
1581
1582 // Casting.
1583 static inline JSObject* cast(Object* obj);
1584
Steve Block8defd9f2010-07-08 12:39:36 +01001585 // Disalow further properties to be added to the object.
1586 Object* PreventExtensions();
1587
1588
Steve Blocka7e24c12009-10-30 11:49:00 +00001589 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001590 void JSObjectShortPrint(StringStream* accumulator);
1591#ifdef DEBUG
1592 void JSObjectPrint();
1593 void JSObjectVerify();
1594 void PrintProperties();
1595 void PrintElements();
1596
1597 // Structure for collecting spill information about JSObjects.
1598 class SpillInformation {
1599 public:
1600 void Clear();
1601 void Print();
1602 int number_of_objects_;
1603 int number_of_objects_with_fast_properties_;
1604 int number_of_objects_with_fast_elements_;
1605 int number_of_fast_used_fields_;
1606 int number_of_fast_unused_fields_;
1607 int number_of_slow_used_properties_;
1608 int number_of_slow_unused_properties_;
1609 int number_of_fast_used_elements_;
1610 int number_of_fast_unused_elements_;
1611 int number_of_slow_used_elements_;
1612 int number_of_slow_unused_elements_;
1613 };
1614
1615 void IncrementSpillStatistics(SpillInformation* info);
1616#endif
1617 Object* SlowReverseLookup(Object* value);
1618
Steve Block8defd9f2010-07-08 12:39:36 +01001619 // Maximal number of fast properties for the JSObject. Used to
1620 // restrict the number of map transitions to avoid an explosion in
1621 // the number of maps for objects used as dictionaries.
1622 inline int MaxFastProperties();
1623
Leon Clarkee46be812010-01-19 14:06:41 +00001624 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1625 // Also maximal value of JSArray's length property.
1626 static const uint32_t kMaxElementCount = 0xffffffffu;
1627
Steve Blocka7e24c12009-10-30 11:49:00 +00001628 static const uint32_t kMaxGap = 1024;
1629 static const int kMaxFastElementsLength = 5000;
1630 static const int kInitialMaxFastElementArray = 100000;
1631 static const int kMaxFastProperties = 8;
1632 static const int kMaxInstanceSize = 255 * kPointerSize;
1633 // When extending the backing storage for property values, we increase
1634 // its size by more than the 1 entry necessary, so sequentially adding fields
1635 // to the same object requires fewer allocations and copies.
1636 static const int kFieldsAdded = 3;
1637
1638 // Layout description.
1639 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1640 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1641 static const int kHeaderSize = kElementsOffset + kPointerSize;
1642
1643 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1644
Iain Merrick75681382010-08-19 15:07:18 +01001645 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1646 public:
1647 static inline int SizeOf(Map* map, HeapObject* object);
1648 };
1649
Steve Blocka7e24c12009-10-30 11:49:00 +00001650 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01001651 Object* GetElementWithCallback(Object* receiver,
1652 Object* structure,
1653 uint32_t index,
1654 Object* holder);
1655 Object* SetElementWithCallback(Object* structure,
1656 uint32_t index,
1657 Object* value,
1658 JSObject* holder);
Steve Blocka7e24c12009-10-30 11:49:00 +00001659 Object* SetElementWithInterceptor(uint32_t index, Object* value);
1660 Object* SetElementWithoutInterceptor(uint32_t index, Object* value);
1661
1662 Object* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
1663
1664 Object* DeletePropertyPostInterceptor(String* name, DeleteMode mode);
1665 Object* DeletePropertyWithInterceptor(String* name);
1666
1667 Object* DeleteElementPostInterceptor(uint32_t index, DeleteMode mode);
1668 Object* DeleteElementWithInterceptor(uint32_t index);
1669
1670 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1671 String* name,
1672 bool continue_search);
1673 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1674 String* name,
1675 bool continue_search);
1676 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1677 Object* receiver,
1678 LookupResult* result,
1679 String* name,
1680 bool continue_search);
1681 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1682 LookupResult* result,
1683 String* name,
1684 bool continue_search);
1685
1686 // Returns true if most of the elements backing storage is used.
1687 bool HasDenseElements();
1688
Leon Clarkef7060e22010-06-03 12:02:55 +01001689 bool CanSetCallback(String* name);
1690 Object* SetElementCallback(uint32_t index,
1691 Object* structure,
1692 PropertyAttributes attributes);
1693 Object* SetPropertyCallback(String* name,
1694 Object* structure,
1695 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00001696 Object* DefineGetterSetter(String* name, PropertyAttributes attributes);
1697
1698 void LookupInDescriptor(String* name, LookupResult* result);
1699
1700 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1701};
1702
1703
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001704// FixedArray describes fixed-sized arrays with element type Object*.
1705class FixedArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001706 public:
1707 // [length]: length of the array.
1708 inline int length();
1709 inline void set_length(int value);
1710
Steve Blocka7e24c12009-10-30 11:49:00 +00001711 // Setter and getter for elements.
1712 inline Object* get(int index);
1713 // Setter that uses write barrier.
1714 inline void set(int index, Object* value);
1715
1716 // Setter that doesn't need write barrier).
1717 inline void set(int index, Smi* value);
1718 // Setter with explicit barrier mode.
1719 inline void set(int index, Object* value, WriteBarrierMode mode);
1720
1721 // Setters for frequently used oddballs located in old space.
1722 inline void set_undefined(int index);
1723 inline void set_null(int index);
1724 inline void set_the_hole(int index);
1725
Iain Merrick75681382010-08-19 15:07:18 +01001726 // Setters with less debug checks for the GC to use.
1727 inline void set_unchecked(int index, Smi* value);
1728 inline void set_null_unchecked(int index);
1729
Steve Block6ded16b2010-05-10 14:33:55 +01001730 // Gives access to raw memory which stores the array's data.
1731 inline Object** data_start();
1732
Steve Blocka7e24c12009-10-30 11:49:00 +00001733 // Copy operations.
1734 inline Object* Copy();
1735 Object* CopySize(int new_length);
1736
1737 // Add the elements of a JSArray to this FixedArray.
1738 Object* AddKeysFromJSArray(JSArray* array);
1739
1740 // Compute the union of this and other.
1741 Object* UnionOfKeys(FixedArray* other);
1742
1743 // Copy a sub array from the receiver to dest.
1744 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1745
1746 // Garbage collection support.
1747 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1748
1749 // Code Generation support.
1750 static int OffsetOfElementAt(int index) { return SizeFor(index); }
1751
1752 // Casting.
1753 static inline FixedArray* cast(Object* obj);
1754
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001755 // Layout description.
1756 // Length is smi tagged when it is stored.
1757 static const int kLengthOffset = HeapObject::kHeaderSize;
1758 static const int kHeaderSize = kLengthOffset + kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00001759
1760 // Maximal allowed size, in bytes, of a single FixedArray.
1761 // Prevents overflowing size computations, as well as extreme memory
1762 // consumption.
1763 static const int kMaxSize = 512 * MB;
1764 // Maximally allowed length of a FixedArray.
1765 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001766
1767 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00001768#ifdef DEBUG
1769 void FixedArrayPrint();
1770 void FixedArrayVerify();
1771 // Checks if two FixedArrays have identical contents.
1772 bool IsEqualTo(FixedArray* other);
1773#endif
1774
1775 // Swap two elements in a pair of arrays. If this array and the
1776 // numbers array are the same object, the elements are only swapped
1777 // once.
1778 void SwapPairs(FixedArray* numbers, int i, int j);
1779
1780 // Sort prefix of this array and the numbers array as pairs wrt. the
1781 // numbers. If the numbers array and the this array are the same
1782 // object, the prefix of this array is sorted.
1783 void SortPairs(FixedArray* numbers, uint32_t len);
1784
Iain Merrick75681382010-08-19 15:07:18 +01001785 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
1786 public:
1787 static inline int SizeOf(Map* map, HeapObject* object) {
1788 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
1789 }
1790 };
1791
Steve Blocka7e24c12009-10-30 11:49:00 +00001792 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00001793 // Set operation on FixedArray without using write barriers. Can
1794 // only be used for storing old space objects or smis.
Steve Blocka7e24c12009-10-30 11:49:00 +00001795 static inline void fast_set(FixedArray* array, int index, Object* value);
1796
1797 private:
1798 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1799};
1800
1801
1802// DescriptorArrays are fixed arrays used to hold instance descriptors.
1803// The format of the these objects is:
1804// [0]: point to a fixed array with (value, detail) pairs.
1805// [1]: next enumeration index (Smi), or pointer to small fixed array:
1806// [0]: next enumeration index (Smi)
1807// [1]: pointer to fixed array with enum cache
1808// [2]: first key
1809// [length() - 1]: last key
1810//
1811class DescriptorArray: public FixedArray {
1812 public:
1813 // Is this the singleton empty_descriptor_array?
1814 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00001815
Steve Blocka7e24c12009-10-30 11:49:00 +00001816 // Returns the number of descriptors in the array.
1817 int number_of_descriptors() {
1818 return IsEmpty() ? 0 : length() - kFirstIndex;
1819 }
1820
1821 int NextEnumerationIndex() {
1822 if (IsEmpty()) return PropertyDetails::kInitialIndex;
1823 Object* obj = get(kEnumerationIndexIndex);
1824 if (obj->IsSmi()) {
1825 return Smi::cast(obj)->value();
1826 } else {
1827 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1828 return Smi::cast(index)->value();
1829 }
1830 }
1831
1832 // Set next enumeration index and flush any enum cache.
1833 void SetNextEnumerationIndex(int value) {
1834 if (!IsEmpty()) {
1835 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1836 }
1837 }
1838 bool HasEnumCache() {
1839 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
1840 }
1841
1842 Object* GetEnumCache() {
1843 ASSERT(HasEnumCache());
1844 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1845 return bridge->get(kEnumCacheBridgeCacheIndex);
1846 }
1847
1848 // Initialize or change the enum cache,
1849 // using the supplied storage for the small "bridge".
1850 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1851
1852 // Accessors for fetching instance descriptor at descriptor number.
1853 inline String* GetKey(int descriptor_number);
1854 inline Object* GetValue(int descriptor_number);
1855 inline Smi* GetDetails(int descriptor_number);
1856 inline PropertyType GetType(int descriptor_number);
1857 inline int GetFieldIndex(int descriptor_number);
1858 inline JSFunction* GetConstantFunction(int descriptor_number);
1859 inline Object* GetCallbacksObject(int descriptor_number);
1860 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
1861 inline bool IsProperty(int descriptor_number);
1862 inline bool IsTransition(int descriptor_number);
1863 inline bool IsNullDescriptor(int descriptor_number);
1864 inline bool IsDontEnum(int descriptor_number);
1865
1866 // Accessor for complete descriptor.
1867 inline void Get(int descriptor_number, Descriptor* desc);
1868 inline void Set(int descriptor_number, Descriptor* desc);
1869
1870 // Transfer complete descriptor from another descriptor array to
1871 // this one.
1872 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
1873
1874 // Copy the descriptor array, insert a new descriptor and optionally
1875 // remove map transitions. If the descriptor is already present, it is
1876 // replaced. If a replaced descriptor is a real property (not a transition
1877 // or null), its enumeration index is kept as is.
1878 // If adding a real property, map transitions must be removed. If adding
1879 // a transition, they must not be removed. All null descriptors are removed.
1880 Object* CopyInsert(Descriptor* descriptor, TransitionFlag transition_flag);
1881
1882 // Remove all transitions. Return a copy of the array with all transitions
1883 // removed, or a Failure object if the new array could not be allocated.
1884 Object* RemoveTransitions();
1885
1886 // Sort the instance descriptors by the hash codes of their keys.
1887 void Sort();
1888
1889 // Search the instance descriptors for given name.
1890 inline int Search(String* name);
1891
Iain Merrick75681382010-08-19 15:07:18 +01001892 // As the above, but uses DescriptorLookupCache and updates it when
1893 // necessary.
1894 inline int SearchWithCache(String* name);
1895
Steve Blocka7e24c12009-10-30 11:49:00 +00001896 // Tells whether the name is present int the array.
1897 bool Contains(String* name) { return kNotFound != Search(name); }
1898
1899 // Perform a binary search in the instance descriptors represented
1900 // by this fixed array. low and high are descriptor indices. If there
1901 // are three instance descriptors in this array it should be called
1902 // with low=0 and high=2.
1903 int BinarySearch(String* name, int low, int high);
1904
1905 // Perform a linear search in the instance descriptors represented
1906 // by this fixed array. len is the number of descriptor indices that are
1907 // valid. Does not require the descriptors to be sorted.
1908 int LinearSearch(String* name, int len);
1909
1910 // Allocates a DescriptorArray, but returns the singleton
1911 // empty descriptor array object if number_of_descriptors is 0.
1912 static Object* Allocate(int number_of_descriptors);
1913
1914 // Casting.
1915 static inline DescriptorArray* cast(Object* obj);
1916
1917 // Constant for denoting key was not found.
1918 static const int kNotFound = -1;
1919
1920 static const int kContentArrayIndex = 0;
1921 static const int kEnumerationIndexIndex = 1;
1922 static const int kFirstIndex = 2;
1923
1924 // The length of the "bridge" to the enum cache.
1925 static const int kEnumCacheBridgeLength = 2;
1926 static const int kEnumCacheBridgeEnumIndex = 0;
1927 static const int kEnumCacheBridgeCacheIndex = 1;
1928
1929 // Layout description.
1930 static const int kContentArrayOffset = FixedArray::kHeaderSize;
1931 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
1932 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
1933
1934 // Layout description for the bridge array.
1935 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
1936 static const int kEnumCacheBridgeCacheOffset =
1937 kEnumCacheBridgeEnumOffset + kPointerSize;
1938
1939#ifdef DEBUG
1940 // Print all the descriptors.
1941 void PrintDescriptors();
1942
1943 // Is the descriptor array sorted and without duplicates?
1944 bool IsSortedNoDuplicates();
1945
1946 // Are two DescriptorArrays equal?
1947 bool IsEqualTo(DescriptorArray* other);
1948#endif
1949
1950 // The maximum number of descriptors we want in a descriptor array (should
1951 // fit in a page).
1952 static const int kMaxNumberOfDescriptors = 1024 + 512;
1953
1954 private:
1955 // Conversion from descriptor number to array indices.
1956 static int ToKeyIndex(int descriptor_number) {
1957 return descriptor_number+kFirstIndex;
1958 }
Leon Clarkee46be812010-01-19 14:06:41 +00001959
1960 static int ToDetailsIndex(int descriptor_number) {
1961 return (descriptor_number << 1) + 1;
1962 }
1963
Steve Blocka7e24c12009-10-30 11:49:00 +00001964 static int ToValueIndex(int descriptor_number) {
1965 return descriptor_number << 1;
1966 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001967
1968 bool is_null_descriptor(int descriptor_number) {
1969 return PropertyDetails(GetDetails(descriptor_number)).type() ==
1970 NULL_DESCRIPTOR;
1971 }
1972 // Swap operation on FixedArray without using write barriers.
1973 static inline void fast_swap(FixedArray* array, int first, int second);
1974
1975 // Swap descriptor first and second.
1976 inline void Swap(int first, int second);
1977
1978 FixedArray* GetContentArray() {
1979 return FixedArray::cast(get(kContentArrayIndex));
1980 }
1981 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
1982};
1983
1984
1985// HashTable is a subclass of FixedArray that implements a hash table
1986// that uses open addressing and quadratic probing.
1987//
1988// In order for the quadratic probing to work, elements that have not
1989// yet been used and elements that have been deleted are
1990// distinguished. Probing continues when deleted elements are
1991// encountered and stops when unused elements are encountered.
1992//
1993// - Elements with key == undefined have not been used yet.
1994// - Elements with key == null have been deleted.
1995//
1996// The hash table class is parameterized with a Shape and a Key.
1997// Shape must be a class with the following interface:
1998// class ExampleShape {
1999// public:
2000// // Tells whether key matches other.
2001// static bool IsMatch(Key key, Object* other);
2002// // Returns the hash value for key.
2003// static uint32_t Hash(Key key);
2004// // Returns the hash value for object.
2005// static uint32_t HashForObject(Key key, Object* object);
2006// // Convert key to an object.
2007// static inline Object* AsObject(Key key);
2008// // The prefix size indicates number of elements in the beginning
2009// // of the backing storage.
2010// static const int kPrefixSize = ..;
2011// // The Element size indicates number of elements per entry.
2012// static const int kEntrySize = ..;
2013// };
Steve Block3ce2e202009-11-05 08:53:23 +00002014// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00002015// beginning of the backing storage that can be used for non-element
2016// information by subclasses.
2017
2018template<typename Shape, typename Key>
2019class HashTable: public FixedArray {
2020 public:
Steve Block3ce2e202009-11-05 08:53:23 +00002021 // Returns the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002022 int NumberOfElements() {
2023 return Smi::cast(get(kNumberOfElementsIndex))->value();
2024 }
2025
Leon Clarkee46be812010-01-19 14:06:41 +00002026 // Returns the number of deleted elements in the hash table.
2027 int NumberOfDeletedElements() {
2028 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2029 }
2030
Steve Block3ce2e202009-11-05 08:53:23 +00002031 // Returns the capacity of the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002032 int Capacity() {
2033 return Smi::cast(get(kCapacityIndex))->value();
2034 }
2035
2036 // ElementAdded should be called whenever an element is added to a
Steve Block3ce2e202009-11-05 08:53:23 +00002037 // hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002038 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2039
2040 // ElementRemoved should be called whenever an element is removed from
Steve Block3ce2e202009-11-05 08:53:23 +00002041 // a hash table.
Leon Clarkee46be812010-01-19 14:06:41 +00002042 void ElementRemoved() {
2043 SetNumberOfElements(NumberOfElements() - 1);
2044 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2045 }
2046 void ElementsRemoved(int n) {
2047 SetNumberOfElements(NumberOfElements() - n);
2048 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2049 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002050
Steve Block3ce2e202009-11-05 08:53:23 +00002051 // Returns a new HashTable object. Might return Failure.
Steve Block6ded16b2010-05-10 14:33:55 +01002052 static Object* Allocate(int at_least_space_for,
2053 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00002054
2055 // Returns the key at entry.
2056 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2057
2058 // Tells whether k is a real key. Null and undefined are not allowed
2059 // as keys and can be used to indicate missing or deleted elements.
2060 bool IsKey(Object* k) {
2061 return !k->IsNull() && !k->IsUndefined();
2062 }
2063
2064 // Garbage collection support.
2065 void IteratePrefix(ObjectVisitor* visitor);
2066 void IterateElements(ObjectVisitor* visitor);
2067
2068 // Casting.
2069 static inline HashTable* cast(Object* obj);
2070
2071 // Compute the probe offset (quadratic probing).
2072 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2073 return (n + n * n) >> 1;
2074 }
2075
2076 static const int kNumberOfElementsIndex = 0;
Leon Clarkee46be812010-01-19 14:06:41 +00002077 static const int kNumberOfDeletedElementsIndex = 1;
2078 static const int kCapacityIndex = 2;
2079 static const int kPrefixStartIndex = 3;
2080 static const int kElementsStartIndex =
Steve Blocka7e24c12009-10-30 11:49:00 +00002081 kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002082 static const int kEntrySize = Shape::kEntrySize;
2083 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00002084 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01002085 static const int kCapacityOffset =
2086 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002087
2088 // Constant used for denoting a absent entry.
2089 static const int kNotFound = -1;
2090
Leon Clarkee46be812010-01-19 14:06:41 +00002091 // Maximal capacity of HashTable. Based on maximal length of underlying
2092 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2093 // cannot overflow.
2094 static const int kMaxCapacity =
2095 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2096
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002097 // Find entry for key otherwise return kNotFound.
Steve Blocka7e24c12009-10-30 11:49:00 +00002098 int FindEntry(Key key);
2099
2100 protected:
2101
2102 // Find the entry at which to insert element with the given key that
2103 // has the given hash value.
2104 uint32_t FindInsertionEntry(uint32_t hash);
2105
2106 // Returns the index for an entry (of the key)
2107 static inline int EntryToIndex(int entry) {
2108 return (entry * kEntrySize) + kElementsStartIndex;
2109 }
2110
Steve Block3ce2e202009-11-05 08:53:23 +00002111 // Update the number of elements in the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002112 void SetNumberOfElements(int nof) {
2113 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2114 }
2115
Leon Clarkee46be812010-01-19 14:06:41 +00002116 // Update the number of deleted elements in the hash table.
2117 void SetNumberOfDeletedElements(int nod) {
2118 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2119 }
2120
Steve Blocka7e24c12009-10-30 11:49:00 +00002121 // Sets the capacity of the hash table.
2122 void SetCapacity(int capacity) {
2123 // To scale a computed hash code to fit within the hash table, we
2124 // use bit-wise AND with a mask, so the capacity must be positive
2125 // and non-zero.
2126 ASSERT(capacity > 0);
Leon Clarkee46be812010-01-19 14:06:41 +00002127 ASSERT(capacity <= kMaxCapacity);
Steve Blocka7e24c12009-10-30 11:49:00 +00002128 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2129 }
2130
2131
2132 // Returns probe entry.
2133 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2134 ASSERT(IsPowerOf2(size));
2135 return (hash + GetProbeOffset(number)) & (size - 1);
2136 }
2137
Leon Clarkee46be812010-01-19 14:06:41 +00002138 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2139 return hash & (size - 1);
2140 }
2141
2142 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2143 return (last + number) & (size - 1);
2144 }
2145
Steve Blocka7e24c12009-10-30 11:49:00 +00002146 // Ensure enough space for n additional elements.
2147 Object* EnsureCapacity(int n, Key key);
2148};
2149
2150
2151
2152// HashTableKey is an abstract superclass for virtual key behavior.
2153class HashTableKey {
2154 public:
2155 // Returns whether the other object matches this key.
2156 virtual bool IsMatch(Object* other) = 0;
2157 // Returns the hash value for this key.
2158 virtual uint32_t Hash() = 0;
2159 // Returns the hash value for object.
2160 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00002161 // Returns the key object for storing into the hash table.
Steve Blocka7e24c12009-10-30 11:49:00 +00002162 // If allocations fails a failure object is returned.
2163 virtual Object* AsObject() = 0;
2164 // Required.
2165 virtual ~HashTableKey() {}
2166};
2167
2168class SymbolTableShape {
2169 public:
2170 static bool IsMatch(HashTableKey* key, Object* value) {
2171 return key->IsMatch(value);
2172 }
2173 static uint32_t Hash(HashTableKey* key) {
2174 return key->Hash();
2175 }
2176 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2177 return key->HashForObject(object);
2178 }
2179 static Object* AsObject(HashTableKey* key) {
2180 return key->AsObject();
2181 }
2182
2183 static const int kPrefixSize = 0;
2184 static const int kEntrySize = 1;
2185};
2186
2187// SymbolTable.
2188//
2189// No special elements in the prefix and the element size is 1
2190// because only the symbol itself (the key) needs to be stored.
2191class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2192 public:
2193 // Find symbol in the symbol table. If it is not there yet, it is
2194 // added. The return value is the symbol table which might have
2195 // been enlarged. If the return value is not a failure, the symbol
2196 // pointer *s is set to the symbol found.
2197 Object* LookupSymbol(Vector<const char> str, Object** s);
2198 Object* LookupString(String* key, Object** s);
2199
2200 // Looks up a symbol that is equal to the given string and returns
2201 // true if it is found, assigning the symbol to the given output
2202 // parameter.
2203 bool LookupSymbolIfExists(String* str, String** symbol);
Steve Blockd0582a62009-12-15 09:54:21 +00002204 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
Steve Blocka7e24c12009-10-30 11:49:00 +00002205
2206 // Casting.
2207 static inline SymbolTable* cast(Object* obj);
2208
2209 private:
2210 Object* LookupKey(HashTableKey* key, Object** s);
2211
2212 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2213};
2214
2215
2216class MapCacheShape {
2217 public:
2218 static bool IsMatch(HashTableKey* key, Object* value) {
2219 return key->IsMatch(value);
2220 }
2221 static uint32_t Hash(HashTableKey* key) {
2222 return key->Hash();
2223 }
2224
2225 static uint32_t HashForObject(HashTableKey* key, Object* object) {
2226 return key->HashForObject(object);
2227 }
2228
2229 static Object* AsObject(HashTableKey* key) {
2230 return key->AsObject();
2231 }
2232
2233 static const int kPrefixSize = 0;
2234 static const int kEntrySize = 2;
2235};
2236
2237
2238// MapCache.
2239//
2240// Maps keys that are a fixed array of symbols to a map.
2241// Used for canonicalize maps for object literals.
2242class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2243 public:
2244 // Find cached value for a string key, otherwise return null.
2245 Object* Lookup(FixedArray* key);
2246 Object* Put(FixedArray* key, Map* value);
2247 static inline MapCache* cast(Object* obj);
2248
2249 private:
2250 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2251};
2252
2253
2254template <typename Shape, typename Key>
2255class Dictionary: public HashTable<Shape, Key> {
2256 public:
2257
2258 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2259 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2260 }
2261
2262 // Returns the value at entry.
2263 Object* ValueAt(int entry) {
Steve Block6ded16b2010-05-10 14:33:55 +01002264 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
Steve Blocka7e24c12009-10-30 11:49:00 +00002265 }
2266
2267 // Set the value for entry.
2268 void ValueAtPut(int entry, Object* value) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002269 // Check that this value can actually be written.
2270 PropertyDetails details = DetailsAt(entry);
2271 // If a value has not been initilized we allow writing to it even if
2272 // it is read only (a declared const that has not been initialized).
2273 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) return;
Steve Block6ded16b2010-05-10 14:33:55 +01002274 this->set(HashTable<Shape, Key>::EntryToIndex(entry)+1, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002275 }
2276
2277 // Returns the property details for the property at entry.
2278 PropertyDetails DetailsAt(int entry) {
2279 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
2280 return PropertyDetails(
Steve Block6ded16b2010-05-10 14:33:55 +01002281 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
Steve Blocka7e24c12009-10-30 11:49:00 +00002282 }
2283
2284 // Set the details for entry.
2285 void DetailsAtPut(int entry, PropertyDetails value) {
Steve Block6ded16b2010-05-10 14:33:55 +01002286 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
Steve Blocka7e24c12009-10-30 11:49:00 +00002287 }
2288
2289 // Sorting support
2290 void CopyValuesTo(FixedArray* elements);
2291
2292 // Delete a property from the dictionary.
2293 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2294
2295 // Returns the number of elements in the dictionary filtering out properties
2296 // with the specified attributes.
2297 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2298
2299 // Returns the number of enumerable elements in the dictionary.
2300 int NumberOfEnumElements();
2301
2302 // Copies keys to preallocated fixed array.
2303 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2304 // Fill in details for properties into storage.
2305 void CopyKeysTo(FixedArray* storage);
2306
2307 // Accessors for next enumeration index.
2308 void SetNextEnumerationIndex(int index) {
Steve Block6ded16b2010-05-10 14:33:55 +01002309 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00002310 }
2311
2312 int NextEnumerationIndex() {
2313 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2314 }
2315
2316 // Returns a new array for dictionary usage. Might return Failure.
2317 static Object* Allocate(int at_least_space_for);
2318
2319 // Ensure enough space for n additional elements.
2320 Object* EnsureCapacity(int n, Key key);
2321
2322#ifdef DEBUG
2323 void Print();
2324#endif
2325 // Returns the key (slow).
2326 Object* SlowReverseLookup(Object* value);
2327
2328 // Sets the entry to (key, value) pair.
2329 inline void SetEntry(int entry,
2330 Object* key,
2331 Object* value,
2332 PropertyDetails details);
2333
2334 Object* Add(Key key, Object* value, PropertyDetails details);
2335
2336 protected:
2337 // Generic at put operation.
2338 Object* AtPut(Key key, Object* value);
2339
2340 // Add entry to dictionary.
2341 Object* AddEntry(Key key,
2342 Object* value,
2343 PropertyDetails details,
2344 uint32_t hash);
2345
2346 // Generate new enumeration indices to avoid enumeration index overflow.
2347 Object* GenerateNewEnumerationIndices();
2348 static const int kMaxNumberKeyIndex =
2349 HashTable<Shape, Key>::kPrefixStartIndex;
2350 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2351};
2352
2353
2354class StringDictionaryShape {
2355 public:
2356 static inline bool IsMatch(String* key, Object* other);
2357 static inline uint32_t Hash(String* key);
2358 static inline uint32_t HashForObject(String* key, Object* object);
2359 static inline Object* AsObject(String* key);
2360 static const int kPrefixSize = 2;
2361 static const int kEntrySize = 3;
2362 static const bool kIsEnumerable = true;
2363};
2364
2365
2366class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2367 public:
2368 static inline StringDictionary* cast(Object* obj) {
2369 ASSERT(obj->IsDictionary());
2370 return reinterpret_cast<StringDictionary*>(obj);
2371 }
2372
2373 // Copies enumerable keys to preallocated fixed array.
2374 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2375
2376 // For transforming properties of a JSObject.
2377 Object* TransformPropertiesToFastFor(JSObject* obj,
2378 int unused_property_fields);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002379
2380 // Find entry for key otherwise return kNotFound. Optimzed version of
2381 // HashTable::FindEntry.
2382 int FindEntry(String* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00002383};
2384
2385
2386class NumberDictionaryShape {
2387 public:
2388 static inline bool IsMatch(uint32_t key, Object* other);
2389 static inline uint32_t Hash(uint32_t key);
2390 static inline uint32_t HashForObject(uint32_t key, Object* object);
2391 static inline Object* AsObject(uint32_t key);
2392 static const int kPrefixSize = 2;
2393 static const int kEntrySize = 3;
2394 static const bool kIsEnumerable = false;
2395};
2396
2397
2398class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2399 public:
2400 static NumberDictionary* cast(Object* obj) {
2401 ASSERT(obj->IsDictionary());
2402 return reinterpret_cast<NumberDictionary*>(obj);
2403 }
2404
2405 // Type specific at put (default NONE attributes is used when adding).
2406 Object* AtNumberPut(uint32_t key, Object* value);
2407 Object* AddNumberEntry(uint32_t key,
2408 Object* value,
2409 PropertyDetails details);
2410
2411 // Set an existing entry or add a new one if needed.
2412 Object* Set(uint32_t key, Object* value, PropertyDetails details);
2413
2414 void UpdateMaxNumberKey(uint32_t key);
2415
2416 // If slow elements are required we will never go back to fast-case
2417 // for the elements kept in this dictionary. We require slow
2418 // elements if an element has been added at an index larger than
2419 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2420 // when defining a getter or setter with a number key.
2421 inline bool requires_slow_elements();
2422 inline void set_requires_slow_elements();
2423
2424 // Get the value of the max number key that has been added to this
2425 // dictionary. max_number_key can only be called if
2426 // requires_slow_elements returns false.
2427 inline uint32_t max_number_key();
2428
2429 // Remove all entries were key is a number and (from <= key && key < to).
2430 void RemoveNumberEntries(uint32_t from, uint32_t to);
2431
2432 // Bit masks.
2433 static const int kRequiresSlowElementsMask = 1;
2434 static const int kRequiresSlowElementsTagSize = 1;
2435 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2436};
2437
2438
Steve Block6ded16b2010-05-10 14:33:55 +01002439// JSFunctionResultCache caches results of some JSFunction invocation.
2440// It is a fixed array with fixed structure:
2441// [0]: factory function
2442// [1]: finger index
2443// [2]: current cache size
2444// [3]: dummy field.
2445// The rest of array are key/value pairs.
2446class JSFunctionResultCache: public FixedArray {
2447 public:
2448 static const int kFactoryIndex = 0;
2449 static const int kFingerIndex = kFactoryIndex + 1;
2450 static const int kCacheSizeIndex = kFingerIndex + 1;
2451 static const int kDummyIndex = kCacheSizeIndex + 1;
2452 static const int kEntriesIndex = kDummyIndex + 1;
2453
2454 static const int kEntrySize = 2; // key + value
2455
Kristian Monsen25f61362010-05-21 11:50:48 +01002456 static const int kFactoryOffset = kHeaderSize;
2457 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2458 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2459
Steve Block6ded16b2010-05-10 14:33:55 +01002460 inline void MakeZeroSize();
2461 inline void Clear();
2462
2463 // Casting
2464 static inline JSFunctionResultCache* cast(Object* obj);
2465
2466#ifdef DEBUG
2467 void JSFunctionResultCacheVerify();
2468#endif
2469};
2470
2471
Steve Blocka7e24c12009-10-30 11:49:00 +00002472// ByteArray represents fixed sized byte arrays. Used by the outside world,
2473// such as PCRE, and also by the memory allocator and garbage collector to
2474// fill in free blocks in the heap.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002475class ByteArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002476 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002477 // [length]: length of the array.
2478 inline int length();
2479 inline void set_length(int value);
2480
Steve Blocka7e24c12009-10-30 11:49:00 +00002481 // Setter and getter.
2482 inline byte get(int index);
2483 inline void set(int index, byte value);
2484
2485 // Treat contents as an int array.
2486 inline int get_int(int index);
2487
2488 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002489 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00002490 }
2491 // We use byte arrays for free blocks in the heap. Given a desired size in
2492 // bytes that is a multiple of the word size and big enough to hold a byte
2493 // array, this function returns the number of elements a byte array should
2494 // have.
2495 static int LengthFor(int size_in_bytes) {
2496 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2497 ASSERT(size_in_bytes >= kHeaderSize);
2498 return size_in_bytes - kHeaderSize;
2499 }
2500
2501 // Returns data start address.
2502 inline Address GetDataStartAddress();
2503
2504 // Returns a pointer to the ByteArray object for a given data start address.
2505 static inline ByteArray* FromDataStartAddress(Address address);
2506
2507 // Casting.
2508 static inline ByteArray* cast(Object* obj);
2509
2510 // Dispatched behavior.
Iain Merrick75681382010-08-19 15:07:18 +01002511 inline int ByteArraySize() {
2512 return SizeFor(this->length());
2513 }
Steve Blocka7e24c12009-10-30 11:49:00 +00002514#ifdef DEBUG
2515 void ByteArrayPrint();
2516 void ByteArrayVerify();
2517#endif
2518
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002519 // Layout description.
2520 // Length is smi tagged when it is stored.
2521 static const int kLengthOffset = HeapObject::kHeaderSize;
2522 static const int kHeaderSize = kLengthOffset + kPointerSize;
2523
2524 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002525
Leon Clarkee46be812010-01-19 14:06:41 +00002526 // Maximal memory consumption for a single ByteArray.
2527 static const int kMaxSize = 512 * MB;
2528 // Maximal length of a single ByteArray.
2529 static const int kMaxLength = kMaxSize - kHeaderSize;
2530
Steve Blocka7e24c12009-10-30 11:49:00 +00002531 private:
2532 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2533};
2534
2535
2536// A PixelArray represents a fixed-size byte array with special semantics
2537// used for implementing the CanvasPixelArray object. Please see the
2538// specification at:
2539// http://www.whatwg.org/specs/web-apps/current-work/
2540// multipage/the-canvas-element.html#canvaspixelarray
2541// In particular, write access clamps the value written to 0 or 255 if the
2542// value written is outside this range.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002543class PixelArray: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002544 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002545 // [length]: length of the array.
2546 inline int length();
2547 inline void set_length(int value);
2548
Steve Blocka7e24c12009-10-30 11:49:00 +00002549 // [external_pointer]: The pointer to the external memory area backing this
2550 // pixel array.
2551 DECL_ACCESSORS(external_pointer, uint8_t) // Pointer to the data store.
2552
2553 // Setter and getter.
2554 inline uint8_t get(int index);
2555 inline void set(int index, uint8_t value);
2556
2557 // This accessor applies the correct conversion from Smi, HeapNumber and
2558 // undefined and clamps the converted value between 0 and 255.
2559 Object* SetValue(uint32_t index, Object* value);
2560
2561 // Casting.
2562 static inline PixelArray* cast(Object* obj);
2563
2564#ifdef DEBUG
2565 void PixelArrayPrint();
2566 void PixelArrayVerify();
2567#endif // DEBUG
2568
Steve Block3ce2e202009-11-05 08:53:23 +00002569 // Maximal acceptable length for a pixel array.
2570 static const int kMaxLength = 0x3fffffff;
2571
Steve Blocka7e24c12009-10-30 11:49:00 +00002572 // PixelArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002573 static const int kLengthOffset = HeapObject::kHeaderSize;
2574 static const int kExternalPointerOffset =
2575 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002576 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002577 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002578
2579 private:
2580 DISALLOW_IMPLICIT_CONSTRUCTORS(PixelArray);
2581};
2582
2583
Steve Block3ce2e202009-11-05 08:53:23 +00002584// An ExternalArray represents a fixed-size array of primitive values
2585// which live outside the JavaScript heap. Its subclasses are used to
2586// implement the CanvasArray types being defined in the WebGL
2587// specification. As of this writing the first public draft is not yet
2588// available, but Khronos members can access the draft at:
2589// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2590//
2591// The semantics of these arrays differ from CanvasPixelArray.
2592// Out-of-range values passed to the setter are converted via a C
2593// cast, not clamping. Out-of-range indices cause exceptions to be
2594// raised rather than being silently ignored.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002595class ExternalArray: public HeapObject {
Steve Block3ce2e202009-11-05 08:53:23 +00002596 public:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002597 // [length]: length of the array.
2598 inline int length();
2599 inline void set_length(int value);
2600
Steve Block3ce2e202009-11-05 08:53:23 +00002601 // [external_pointer]: The pointer to the external memory area backing this
2602 // external array.
2603 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
2604
2605 // Casting.
2606 static inline ExternalArray* cast(Object* obj);
2607
2608 // Maximal acceptable length for an external array.
2609 static const int kMaxLength = 0x3fffffff;
2610
2611 // ExternalArray headers are not quadword aligned.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002612 static const int kLengthOffset = HeapObject::kHeaderSize;
2613 static const int kExternalPointerOffset =
2614 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002615 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002616 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Block3ce2e202009-11-05 08:53:23 +00002617
2618 private:
2619 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2620};
2621
2622
2623class ExternalByteArray: public ExternalArray {
2624 public:
2625 // Setter and getter.
2626 inline int8_t get(int index);
2627 inline void set(int index, int8_t value);
2628
2629 // This accessor applies the correct conversion from Smi, HeapNumber
2630 // and undefined.
2631 Object* SetValue(uint32_t index, Object* value);
2632
2633 // Casting.
2634 static inline ExternalByteArray* cast(Object* obj);
2635
2636#ifdef DEBUG
2637 void ExternalByteArrayPrint();
2638 void ExternalByteArrayVerify();
2639#endif // DEBUG
2640
2641 private:
2642 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2643};
2644
2645
2646class ExternalUnsignedByteArray: public ExternalArray {
2647 public:
2648 // Setter and getter.
2649 inline uint8_t get(int index);
2650 inline void set(int index, uint8_t value);
2651
2652 // This accessor applies the correct conversion from Smi, HeapNumber
2653 // and undefined.
2654 Object* SetValue(uint32_t index, Object* value);
2655
2656 // Casting.
2657 static inline ExternalUnsignedByteArray* cast(Object* obj);
2658
2659#ifdef DEBUG
2660 void ExternalUnsignedByteArrayPrint();
2661 void ExternalUnsignedByteArrayVerify();
2662#endif // DEBUG
2663
2664 private:
2665 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2666};
2667
2668
2669class ExternalShortArray: public ExternalArray {
2670 public:
2671 // Setter and getter.
2672 inline int16_t get(int index);
2673 inline void set(int index, int16_t value);
2674
2675 // This accessor applies the correct conversion from Smi, HeapNumber
2676 // and undefined.
2677 Object* SetValue(uint32_t index, Object* value);
2678
2679 // Casting.
2680 static inline ExternalShortArray* cast(Object* obj);
2681
2682#ifdef DEBUG
2683 void ExternalShortArrayPrint();
2684 void ExternalShortArrayVerify();
2685#endif // DEBUG
2686
2687 private:
2688 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2689};
2690
2691
2692class ExternalUnsignedShortArray: public ExternalArray {
2693 public:
2694 // Setter and getter.
2695 inline uint16_t get(int index);
2696 inline void set(int index, uint16_t value);
2697
2698 // This accessor applies the correct conversion from Smi, HeapNumber
2699 // and undefined.
2700 Object* SetValue(uint32_t index, Object* value);
2701
2702 // Casting.
2703 static inline ExternalUnsignedShortArray* cast(Object* obj);
2704
2705#ifdef DEBUG
2706 void ExternalUnsignedShortArrayPrint();
2707 void ExternalUnsignedShortArrayVerify();
2708#endif // DEBUG
2709
2710 private:
2711 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
2712};
2713
2714
2715class ExternalIntArray: public ExternalArray {
2716 public:
2717 // Setter and getter.
2718 inline int32_t get(int index);
2719 inline void set(int index, int32_t value);
2720
2721 // This accessor applies the correct conversion from Smi, HeapNumber
2722 // and undefined.
2723 Object* SetValue(uint32_t index, Object* value);
2724
2725 // Casting.
2726 static inline ExternalIntArray* cast(Object* obj);
2727
2728#ifdef DEBUG
2729 void ExternalIntArrayPrint();
2730 void ExternalIntArrayVerify();
2731#endif // DEBUG
2732
2733 private:
2734 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
2735};
2736
2737
2738class ExternalUnsignedIntArray: public ExternalArray {
2739 public:
2740 // Setter and getter.
2741 inline uint32_t get(int index);
2742 inline void set(int index, uint32_t value);
2743
2744 // This accessor applies the correct conversion from Smi, HeapNumber
2745 // and undefined.
2746 Object* SetValue(uint32_t index, Object* value);
2747
2748 // Casting.
2749 static inline ExternalUnsignedIntArray* cast(Object* obj);
2750
2751#ifdef DEBUG
2752 void ExternalUnsignedIntArrayPrint();
2753 void ExternalUnsignedIntArrayVerify();
2754#endif // DEBUG
2755
2756 private:
2757 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
2758};
2759
2760
2761class ExternalFloatArray: public ExternalArray {
2762 public:
2763 // Setter and getter.
2764 inline float get(int index);
2765 inline void set(int index, float value);
2766
2767 // This accessor applies the correct conversion from Smi, HeapNumber
2768 // and undefined.
2769 Object* SetValue(uint32_t index, Object* value);
2770
2771 // Casting.
2772 static inline ExternalFloatArray* cast(Object* obj);
2773
2774#ifdef DEBUG
2775 void ExternalFloatArrayPrint();
2776 void ExternalFloatArrayVerify();
2777#endif // DEBUG
2778
2779 private:
2780 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
2781};
2782
2783
Steve Blocka7e24c12009-10-30 11:49:00 +00002784// Code describes objects with on-the-fly generated machine code.
2785class Code: public HeapObject {
2786 public:
2787 // Opaque data type for encapsulating code flags like kind, inline
2788 // cache state, and arguments count.
Iain Merrick75681382010-08-19 15:07:18 +01002789 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
2790 // enumeration type has correct value range (see Issue 830 for more details).
2791 enum Flags {
2792 FLAGS_MIN_VALUE = kMinInt,
2793 FLAGS_MAX_VALUE = kMaxInt
2794 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002795
2796 enum Kind {
2797 FUNCTION,
2798 STUB,
2799 BUILTIN,
2800 LOAD_IC,
2801 KEYED_LOAD_IC,
2802 CALL_IC,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002803 KEYED_CALL_IC,
Steve Blocka7e24c12009-10-30 11:49:00 +00002804 STORE_IC,
2805 KEYED_STORE_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002806 BINARY_OP_IC,
2807 // No more than 16 kinds. The value currently encoded in four bits in
Steve Blocka7e24c12009-10-30 11:49:00 +00002808 // Flags.
2809
2810 // Pseudo-kinds.
2811 REGEXP = BUILTIN,
2812 FIRST_IC_KIND = LOAD_IC,
Steve Block6ded16b2010-05-10 14:33:55 +01002813 LAST_IC_KIND = BINARY_OP_IC
Steve Blocka7e24c12009-10-30 11:49:00 +00002814 };
2815
2816 enum {
Kristian Monsen50ef84f2010-07-29 15:18:00 +01002817 NUMBER_OF_KINDS = LAST_IC_KIND + 1
Steve Blocka7e24c12009-10-30 11:49:00 +00002818 };
2819
2820#ifdef ENABLE_DISASSEMBLER
2821 // Printing
2822 static const char* Kind2String(Kind kind);
2823 static const char* ICState2String(InlineCacheState state);
2824 static const char* PropertyType2String(PropertyType type);
2825 void Disassemble(const char* name);
2826#endif // ENABLE_DISASSEMBLER
2827
2828 // [instruction_size]: Size of the native instructions
2829 inline int instruction_size();
2830 inline void set_instruction_size(int value);
2831
Leon Clarkeac952652010-07-15 11:15:24 +01002832 // [relocation_info]: Code relocation information
2833 DECL_ACCESSORS(relocation_info, ByteArray)
2834
2835 // Unchecked accessor to be used during GC.
2836 inline ByteArray* unchecked_relocation_info();
2837
Steve Blocka7e24c12009-10-30 11:49:00 +00002838 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00002839
Steve Blocka7e24c12009-10-30 11:49:00 +00002840 // [flags]: Various code flags.
2841 inline Flags flags();
2842 inline void set_flags(Flags flags);
2843
2844 // [flags]: Access to specific code flags.
2845 inline Kind kind();
2846 inline InlineCacheState ic_state(); // Only valid for IC stubs.
2847 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
2848 inline PropertyType type(); // Only valid for monomorphic IC stubs.
2849 inline int arguments_count(); // Only valid for call IC stubs.
2850
2851 // Testers for IC stub kinds.
2852 inline bool is_inline_cache_stub();
2853 inline bool is_load_stub() { return kind() == LOAD_IC; }
2854 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
2855 inline bool is_store_stub() { return kind() == STORE_IC; }
2856 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
2857 inline bool is_call_stub() { return kind() == CALL_IC; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01002858 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
Steve Blocka7e24c12009-10-30 11:49:00 +00002859
Steve Block6ded16b2010-05-10 14:33:55 +01002860 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
Steve Blocka7e24c12009-10-30 11:49:00 +00002861 inline CodeStub::Major major_key();
2862 inline void set_major_key(CodeStub::Major major);
2863
2864 // Flags operations.
2865 static inline Flags ComputeFlags(Kind kind,
2866 InLoopFlag in_loop = NOT_IN_LOOP,
2867 InlineCacheState ic_state = UNINITIALIZED,
2868 PropertyType type = NORMAL,
Steve Block8defd9f2010-07-08 12:39:36 +01002869 int argc = -1,
2870 InlineCacheHolderFlag holder = OWN_MAP);
Steve Blocka7e24c12009-10-30 11:49:00 +00002871
2872 static inline Flags ComputeMonomorphicFlags(
2873 Kind kind,
2874 PropertyType type,
Steve Block8defd9f2010-07-08 12:39:36 +01002875 InlineCacheHolderFlag holder = OWN_MAP,
Steve Blocka7e24c12009-10-30 11:49:00 +00002876 InLoopFlag in_loop = NOT_IN_LOOP,
2877 int argc = -1);
2878
2879 static inline Kind ExtractKindFromFlags(Flags flags);
2880 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
2881 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
2882 static inline PropertyType ExtractTypeFromFlags(Flags flags);
2883 static inline int ExtractArgumentsCountFromFlags(Flags flags);
Steve Block8defd9f2010-07-08 12:39:36 +01002884 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00002885 static inline Flags RemoveTypeFromFlags(Flags flags);
2886
2887 // Convert a target address into a code object.
2888 static inline Code* GetCodeFromTargetAddress(Address address);
2889
2890 // Returns the address of the first instruction.
2891 inline byte* instruction_start();
2892
Leon Clarkeac952652010-07-15 11:15:24 +01002893 // Returns the address right after the last instruction.
2894 inline byte* instruction_end();
2895
Steve Blocka7e24c12009-10-30 11:49:00 +00002896 // Returns the size of the instructions, padding, and relocation information.
2897 inline int body_size();
2898
2899 // Returns the address of the first relocation info (read backwards!).
2900 inline byte* relocation_start();
2901
2902 // Code entry point.
2903 inline byte* entry();
2904
2905 // Returns true if pc is inside this object's instructions.
2906 inline bool contains(byte* pc);
2907
Steve Blocka7e24c12009-10-30 11:49:00 +00002908 // Relocate the code by delta bytes. Called to signal that this code
2909 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002910 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00002911
2912 // Migrate code described by desc.
2913 void CopyFrom(const CodeDesc& desc);
2914
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002915 // Returns the object size for a given body (used for allocation).
2916 static int SizeFor(int body_size) {
Steve Blocka7e24c12009-10-30 11:49:00 +00002917 ASSERT_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002918 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00002919 }
2920
2921 // Calculate the size of the code object to report for log events. This takes
2922 // the layout of the code object into account.
2923 int ExecutableSize() {
2924 // Check that the assumptions about the layout of the code object holds.
2925 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
2926 Code::kHeaderSize);
2927 return instruction_size() + Code::kHeaderSize;
2928 }
2929
2930 // Locating source position.
2931 int SourcePosition(Address pc);
2932 int SourceStatementPosition(Address pc);
2933
2934 // Casting.
2935 static inline Code* cast(Object* obj);
2936
2937 // Dispatched behavior.
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002938 int CodeSize() { return SizeFor(body_size()); }
Iain Merrick75681382010-08-19 15:07:18 +01002939 inline void CodeIterateBody(ObjectVisitor* v);
2940
2941 template<typename StaticVisitor>
2942 inline void CodeIterateBody();
Steve Blocka7e24c12009-10-30 11:49:00 +00002943#ifdef DEBUG
2944 void CodePrint();
2945 void CodeVerify();
2946#endif
2947 // Code entry points are aligned to 32 bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00002948 static const int kCodeAlignmentBits = 5;
2949 static const int kCodeAlignment = 1 << kCodeAlignmentBits;
Steve Blocka7e24c12009-10-30 11:49:00 +00002950 static const int kCodeAlignmentMask = kCodeAlignment - 1;
2951
2952 // Layout description.
2953 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
Leon Clarkeac952652010-07-15 11:15:24 +01002954 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01002955 static const int kFlagsOffset = kRelocationInfoOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002956 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
2957 // Add padding to align the instruction start following right after
2958 // the Code object header.
2959 static const int kHeaderSize =
2960 (kKindSpecificFlagsOffset + kIntSize + kCodeAlignmentMask) &
2961 ~kCodeAlignmentMask;
2962
2963 // Byte offsets within kKindSpecificFlagsOffset.
2964 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset + 1;
2965
2966 // Flags layout.
2967 static const int kFlagsICStateShift = 0;
2968 static const int kFlagsICInLoopShift = 3;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002969 static const int kFlagsTypeShift = 4;
2970 static const int kFlagsKindShift = 7;
Steve Block8defd9f2010-07-08 12:39:36 +01002971 static const int kFlagsICHolderShift = 11;
2972 static const int kFlagsArgumentsCountShift = 12;
Steve Blocka7e24c12009-10-30 11:49:00 +00002973
Steve Block6ded16b2010-05-10 14:33:55 +01002974 static const int kFlagsICStateMask = 0x00000007; // 00000000111
2975 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01002976 static const int kFlagsTypeMask = 0x00000070; // 00001110000
2977 static const int kFlagsKindMask = 0x00000780; // 11110000000
Steve Block8defd9f2010-07-08 12:39:36 +01002978 static const int kFlagsCacheInPrototypeMapMask = 0x00000800;
2979 static const int kFlagsArgumentsCountMask = 0xFFFFF000;
Steve Blocka7e24c12009-10-30 11:49:00 +00002980
2981 static const int kFlagsNotUsedInLookup =
Steve Block8defd9f2010-07-08 12:39:36 +01002982 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
Steve Blocka7e24c12009-10-30 11:49:00 +00002983
2984 private:
2985 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
2986};
2987
2988
2989// All heap objects have a Map that describes their structure.
2990// A Map contains information about:
2991// - Size information about the object
2992// - How to iterate over an object (for garbage collection)
2993class Map: public HeapObject {
2994 public:
2995 // Instance size.
2996 inline int instance_size();
2997 inline void set_instance_size(int value);
2998
2999 // Count of properties allocated in the object.
3000 inline int inobject_properties();
3001 inline void set_inobject_properties(int value);
3002
3003 // Count of property fields pre-allocated in the object when first allocated.
3004 inline int pre_allocated_property_fields();
3005 inline void set_pre_allocated_property_fields(int value);
3006
3007 // Instance type.
3008 inline InstanceType instance_type();
3009 inline void set_instance_type(InstanceType value);
3010
3011 // Tells how many unused property fields are available in the
3012 // instance (only used for JSObject in fast mode).
3013 inline int unused_property_fields();
3014 inline void set_unused_property_fields(int value);
3015
3016 // Bit field.
3017 inline byte bit_field();
3018 inline void set_bit_field(byte value);
3019
3020 // Bit field 2.
3021 inline byte bit_field2();
3022 inline void set_bit_field2(byte value);
3023
3024 // Tells whether the object in the prototype property will be used
3025 // for instances created from this function. If the prototype
3026 // property is set to a value that is not a JSObject, the prototype
3027 // property will not be used to create instances of the function.
3028 // See ECMA-262, 13.2.2.
3029 inline void set_non_instance_prototype(bool value);
3030 inline bool has_non_instance_prototype();
3031
Steve Block6ded16b2010-05-10 14:33:55 +01003032 // Tells whether function has special prototype property. If not, prototype
3033 // property will not be created when accessed (will return undefined),
3034 // and construction from this function will not be allowed.
3035 inline void set_function_with_prototype(bool value);
3036 inline bool function_with_prototype();
3037
Steve Blocka7e24c12009-10-30 11:49:00 +00003038 // Tells whether the instance with this map should be ignored by the
3039 // __proto__ accessor.
3040 inline void set_is_hidden_prototype() {
3041 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3042 }
3043
3044 inline bool is_hidden_prototype() {
3045 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3046 }
3047
3048 // Records and queries whether the instance has a named interceptor.
3049 inline void set_has_named_interceptor() {
3050 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3051 }
3052
3053 inline bool has_named_interceptor() {
3054 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3055 }
3056
3057 // Records and queries whether the instance has an indexed interceptor.
3058 inline void set_has_indexed_interceptor() {
3059 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3060 }
3061
3062 inline bool has_indexed_interceptor() {
3063 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3064 }
3065
3066 // Tells whether the instance is undetectable.
3067 // An undetectable object is a special class of JSObject: 'typeof' operator
3068 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3069 // a normal JS object. It is useful for implementing undetectable
3070 // document.all in Firefox & Safari.
3071 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3072 inline void set_is_undetectable() {
3073 set_bit_field(bit_field() | (1 << kIsUndetectable));
3074 }
3075
3076 inline bool is_undetectable() {
3077 return ((1 << kIsUndetectable) & bit_field()) != 0;
3078 }
3079
Steve Blocka7e24c12009-10-30 11:49:00 +00003080 // Tells whether the instance has a call-as-function handler.
3081 inline void set_has_instance_call_handler() {
3082 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3083 }
3084
3085 inline bool has_instance_call_handler() {
3086 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3087 }
3088
Steve Block8defd9f2010-07-08 12:39:36 +01003089 inline void set_is_extensible(bool value);
3090 inline bool is_extensible();
3091
3092 // Tells whether the instance has fast elements.
Iain Merrick75681382010-08-19 15:07:18 +01003093 // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3094 inline void set_has_fast_elements(bool value) {
Steve Block8defd9f2010-07-08 12:39:36 +01003095 if (value) {
3096 set_bit_field2(bit_field2() | (1 << kHasFastElements));
3097 } else {
3098 set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3099 }
Leon Clarkee46be812010-01-19 14:06:41 +00003100 }
3101
Iain Merrick75681382010-08-19 15:07:18 +01003102 inline bool has_fast_elements() {
Steve Block8defd9f2010-07-08 12:39:36 +01003103 return ((1 << kHasFastElements) & bit_field2()) != 0;
Leon Clarkee46be812010-01-19 14:06:41 +00003104 }
3105
Steve Blocka7e24c12009-10-30 11:49:00 +00003106 // Tells whether the instance needs security checks when accessing its
3107 // properties.
3108 inline void set_is_access_check_needed(bool access_check_needed);
3109 inline bool is_access_check_needed();
3110
3111 // [prototype]: implicit prototype object.
3112 DECL_ACCESSORS(prototype, Object)
3113
3114 // [constructor]: points back to the function responsible for this map.
3115 DECL_ACCESSORS(constructor, Object)
3116
3117 // [instance descriptors]: describes the object.
3118 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3119
3120 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01003121 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003122
Steve Blocka7e24c12009-10-30 11:49:00 +00003123 Object* CopyDropDescriptors();
3124
3125 // Returns a copy of the map, with all transitions dropped from the
3126 // instance descriptors.
3127 Object* CopyDropTransitions();
3128
Steve Block8defd9f2010-07-08 12:39:36 +01003129 // Returns this map if it has the fast elements bit set, otherwise
3130 // returns a copy of the map, with all transitions dropped from the
3131 // descriptors and the fast elements bit set.
3132 inline Object* GetFastElementsMap();
3133
3134 // Returns this map if it has the fast elements bit cleared,
3135 // otherwise returns a copy of the map, with all transitions dropped
3136 // from the descriptors and the fast elements bit cleared.
3137 inline Object* GetSlowElementsMap();
3138
Steve Blocka7e24c12009-10-30 11:49:00 +00003139 // Returns the property index for name (only valid for FAST MODE).
3140 int PropertyIndexFor(String* name);
3141
3142 // Returns the next free property index (only valid for FAST MODE).
3143 int NextFreePropertyIndex();
3144
3145 // Returns the number of properties described in instance_descriptors.
3146 int NumberOfDescribedProperties();
3147
3148 // Casting.
3149 static inline Map* cast(Object* obj);
3150
3151 // Locate an accessor in the instance descriptor.
3152 AccessorDescriptor* FindAccessor(String* name);
3153
3154 // Code cache operations.
3155
3156 // Clears the code cache.
3157 inline void ClearCodeCache();
3158
3159 // Update code cache.
3160 Object* UpdateCodeCache(String* name, Code* code);
3161
3162 // Returns the found code or undefined if absent.
3163 Object* FindInCodeCache(String* name, Code::Flags flags);
3164
3165 // Returns the non-negative index of the code object if it is in the
3166 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01003167 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00003168
3169 // Removes a code object from the code cache at the given index.
Steve Block6ded16b2010-05-10 14:33:55 +01003170 void RemoveFromCodeCache(String* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00003171
3172 // For every transition in this map, makes the transition's
3173 // target's prototype pointer point back to this map.
3174 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3175 void CreateBackPointers();
3176
3177 // Set all map transitions from this map to dead maps to null.
3178 // Also, restore the original prototype on the targets of these
3179 // transitions, so that we do not process this map again while
3180 // following back pointers.
3181 void ClearNonLiveTransitions(Object* real_prototype);
3182
3183 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00003184#ifdef DEBUG
3185 void MapPrint();
3186 void MapVerify();
3187#endif
3188
Iain Merrick75681382010-08-19 15:07:18 +01003189 inline int visitor_id();
3190 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003191
Steve Blocka7e24c12009-10-30 11:49:00 +00003192 static const int kMaxPreAllocatedPropertyFields = 255;
3193
3194 // Layout description.
3195 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3196 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3197 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3198 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3199 static const int kInstanceDescriptorsOffset =
3200 kConstructorOffset + kPointerSize;
3201 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003202 static const int kScavengerCallbackOffset = kCodeCacheOffset + kPointerSize;
3203 static const int kPadStart = kScavengerCallbackOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003204 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3205
3206 // Layout of pointer fields. Heap iteration code relies on them
3207 // being continiously allocated.
3208 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3209 static const int kPointerFieldsEndOffset =
3210 Map::kCodeCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003211
3212 // Byte offsets within kInstanceSizesOffset.
3213 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3214 static const int kInObjectPropertiesByte = 1;
3215 static const int kInObjectPropertiesOffset =
3216 kInstanceSizesOffset + kInObjectPropertiesByte;
3217 static const int kPreAllocatedPropertyFieldsByte = 2;
3218 static const int kPreAllocatedPropertyFieldsOffset =
3219 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
3220 // The byte at position 3 is not in use at the moment.
3221
3222 // Byte offsets within kInstanceAttributesOffset attributes.
3223 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3224 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3225 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3226 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3227
3228 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3229
3230 // Bit positions for bit field.
3231 static const int kUnused = 0; // To be used for marking recently used maps.
3232 static const int kHasNonInstancePrototype = 1;
3233 static const int kIsHiddenPrototype = 2;
3234 static const int kHasNamedInterceptor = 3;
3235 static const int kHasIndexedInterceptor = 4;
3236 static const int kIsUndetectable = 5;
3237 static const int kHasInstanceCallHandler = 6;
3238 static const int kIsAccessCheckNeeded = 7;
3239
3240 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00003241 static const int kIsExtensible = 0;
Steve Block6ded16b2010-05-10 14:33:55 +01003242 static const int kFunctionWithPrototype = 1;
Steve Block8defd9f2010-07-08 12:39:36 +01003243 static const int kHasFastElements = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003244 static const int kStringWrapperSafeForDefaultValueOf = 3;
Steve Block6ded16b2010-05-10 14:33:55 +01003245
3246 // Layout of the default cache. It holds alternating name and code objects.
3247 static const int kCodeCacheEntrySize = 2;
3248 static const int kCodeCacheEntryNameOffset = 0;
3249 static const int kCodeCacheEntryCodeOffset = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00003250
Iain Merrick75681382010-08-19 15:07:18 +01003251 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3252 kPointerFieldsEndOffset,
3253 kSize> BodyDescriptor;
3254
Steve Blocka7e24c12009-10-30 11:49:00 +00003255 private:
3256 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3257};
3258
3259
3260// An abstract superclass, a marker class really, for simple structure classes.
3261// It doesn't carry much functionality but allows struct classes to me
3262// identified in the type system.
3263class Struct: public HeapObject {
3264 public:
3265 inline void InitializeBody(int object_size);
3266 static inline Struct* cast(Object* that);
3267};
3268
3269
3270// Script describes a script which has been added to the VM.
3271class Script: public Struct {
3272 public:
3273 // Script types.
3274 enum Type {
3275 TYPE_NATIVE = 0,
3276 TYPE_EXTENSION = 1,
3277 TYPE_NORMAL = 2
3278 };
3279
3280 // Script compilation types.
3281 enum CompilationType {
3282 COMPILATION_TYPE_HOST = 0,
3283 COMPILATION_TYPE_EVAL = 1,
3284 COMPILATION_TYPE_JSON = 2
3285 };
3286
3287 // [source]: the script source.
3288 DECL_ACCESSORS(source, Object)
3289
3290 // [name]: the script name.
3291 DECL_ACCESSORS(name, Object)
3292
3293 // [id]: the script id.
3294 DECL_ACCESSORS(id, Object)
3295
3296 // [line_offset]: script line offset in resource from where it was extracted.
3297 DECL_ACCESSORS(line_offset, Smi)
3298
3299 // [column_offset]: script column offset in resource from where it was
3300 // extracted.
3301 DECL_ACCESSORS(column_offset, Smi)
3302
3303 // [data]: additional data associated with this script.
3304 DECL_ACCESSORS(data, Object)
3305
3306 // [context_data]: context data for the context this script was compiled in.
3307 DECL_ACCESSORS(context_data, Object)
3308
3309 // [wrapper]: the wrapper cache.
3310 DECL_ACCESSORS(wrapper, Proxy)
3311
3312 // [type]: the script type.
3313 DECL_ACCESSORS(type, Smi)
3314
3315 // [compilation]: how the the script was compiled.
3316 DECL_ACCESSORS(compilation_type, Smi)
3317
Steve Blockd0582a62009-12-15 09:54:21 +00003318 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00003319 DECL_ACCESSORS(line_ends, Object)
3320
Steve Blockd0582a62009-12-15 09:54:21 +00003321 // [eval_from_shared]: for eval scripts the shared funcion info for the
3322 // function from which eval was called.
3323 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00003324
3325 // [eval_from_instructions_offset]: the instruction offset in the code for the
3326 // function from which eval was called where eval was called.
3327 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3328
3329 static inline Script* cast(Object* obj);
3330
Steve Block3ce2e202009-11-05 08:53:23 +00003331 // If script source is an external string, check that the underlying
3332 // resource is accessible. Otherwise, always return true.
3333 inline bool HasValidSource();
3334
Steve Blocka7e24c12009-10-30 11:49:00 +00003335#ifdef DEBUG
3336 void ScriptPrint();
3337 void ScriptVerify();
3338#endif
3339
3340 static const int kSourceOffset = HeapObject::kHeaderSize;
3341 static const int kNameOffset = kSourceOffset + kPointerSize;
3342 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
3343 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
3344 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
3345 static const int kContextOffset = kDataOffset + kPointerSize;
3346 static const int kWrapperOffset = kContextOffset + kPointerSize;
3347 static const int kTypeOffset = kWrapperOffset + kPointerSize;
3348 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
3349 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
3350 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00003351 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003352 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00003353 kEvalFromSharedOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003354 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
3355
3356 private:
3357 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
3358};
3359
3360
3361// SharedFunctionInfo describes the JSFunction information that can be
3362// shared by multiple instances of the function.
3363class SharedFunctionInfo: public HeapObject {
3364 public:
3365 // [name]: Function name.
3366 DECL_ACCESSORS(name, Object)
3367
3368 // [code]: Function code.
3369 DECL_ACCESSORS(code, Code)
3370
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003371 // [scope_info]: Scope info.
3372 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
3373
Steve Blocka7e24c12009-10-30 11:49:00 +00003374 // [construct stub]: Code stub for constructing instances of this function.
3375 DECL_ACCESSORS(construct_stub, Code)
3376
Iain Merrick75681382010-08-19 15:07:18 +01003377 inline Code* unchecked_code();
3378
Steve Blocka7e24c12009-10-30 11:49:00 +00003379 // Returns if this function has been compiled to native code yet.
3380 inline bool is_compiled();
3381
3382 // [length]: The function length - usually the number of declared parameters.
3383 // Use up to 2^30 parameters.
3384 inline int length();
3385 inline void set_length(int value);
3386
3387 // [formal parameter count]: The declared number of parameters.
3388 inline int formal_parameter_count();
3389 inline void set_formal_parameter_count(int value);
3390
3391 // Set the formal parameter count so the function code will be
3392 // called without using argument adaptor frames.
3393 inline void DontAdaptArguments();
3394
3395 // [expected_nof_properties]: Expected number of properties for the function.
3396 inline int expected_nof_properties();
3397 inline void set_expected_nof_properties(int value);
3398
3399 // [instance class name]: class name for instances.
3400 DECL_ACCESSORS(instance_class_name, Object)
3401
Steve Block6ded16b2010-05-10 14:33:55 +01003402 // [function data]: This field holds some additional data for function.
3403 // Currently it either has FunctionTemplateInfo to make benefit the API
Kristian Monsen25f61362010-05-21 11:50:48 +01003404 // or Smi identifying a custom call generator.
Steve Blocka7e24c12009-10-30 11:49:00 +00003405 // In the long run we don't want all functions to have this field but
3406 // we can fix that when we have a better model for storing hidden data
3407 // on objects.
3408 DECL_ACCESSORS(function_data, Object)
3409
Steve Block6ded16b2010-05-10 14:33:55 +01003410 inline bool IsApiFunction();
3411 inline FunctionTemplateInfo* get_api_func_data();
3412 inline bool HasCustomCallGenerator();
Kristian Monsen25f61362010-05-21 11:50:48 +01003413 inline int custom_call_generator_id();
Steve Block6ded16b2010-05-10 14:33:55 +01003414
Steve Blocka7e24c12009-10-30 11:49:00 +00003415 // [script info]: Script from which the function originates.
3416 DECL_ACCESSORS(script, Object)
3417
Steve Block6ded16b2010-05-10 14:33:55 +01003418 // [num_literals]: Number of literals used by this function.
3419 inline int num_literals();
3420 inline void set_num_literals(int value);
3421
Steve Blocka7e24c12009-10-30 11:49:00 +00003422 // [start_position_and_type]: Field used to store both the source code
3423 // position, whether or not the function is a function expression,
3424 // and whether or not the function is a toplevel function. The two
3425 // least significants bit indicates whether the function is an
3426 // expression and the rest contains the source code position.
3427 inline int start_position_and_type();
3428 inline void set_start_position_and_type(int value);
3429
3430 // [debug info]: Debug information.
3431 DECL_ACCESSORS(debug_info, Object)
3432
3433 // [inferred name]: Name inferred from variable or property
3434 // assignment of this function. Used to facilitate debugging and
3435 // profiling of JavaScript code written in OO style, where almost
3436 // all functions are anonymous but are assigned to object
3437 // properties.
3438 DECL_ACCESSORS(inferred_name, String)
3439
3440 // Position of the 'function' token in the script source.
3441 inline int function_token_position();
3442 inline void set_function_token_position(int function_token_position);
3443
3444 // Position of this function in the script source.
3445 inline int start_position();
3446 inline void set_start_position(int start_position);
3447
3448 // End position of this function in the script source.
3449 inline int end_position();
3450 inline void set_end_position(int end_position);
3451
3452 // Is this function a function expression in the source code.
3453 inline bool is_expression();
3454 inline void set_is_expression(bool value);
3455
3456 // Is this function a top-level function (scripts, evals).
3457 inline bool is_toplevel();
3458 inline void set_is_toplevel(bool value);
3459
3460 // Bit field containing various information collected by the compiler to
3461 // drive optimization.
3462 inline int compiler_hints();
3463 inline void set_compiler_hints(int value);
3464
3465 // Add information on assignments of the form this.x = ...;
3466 void SetThisPropertyAssignmentsInfo(
Steve Blocka7e24c12009-10-30 11:49:00 +00003467 bool has_only_simple_this_property_assignments,
3468 FixedArray* this_property_assignments);
3469
3470 // Clear information on assignments of the form this.x = ...;
3471 void ClearThisPropertyAssignmentsInfo();
3472
3473 // Indicate that this function only consists of assignments of the form
Steve Blocka7e24c12009-10-30 11:49:00 +00003474 // this.x = y; where y is either a constant or refers to an argument.
3475 inline bool has_only_simple_this_property_assignments();
3476
Leon Clarked91b9f72010-01-27 17:25:45 +00003477 inline bool try_full_codegen();
3478 inline void set_try_full_codegen(bool flag);
Steve Blockd0582a62009-12-15 09:54:21 +00003479
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003480 // Indicates if this function can be lazy compiled.
3481 // This is used to determine if we can safely flush code from a function
3482 // when doing GC if we expect that the function will no longer be used.
3483 inline bool allows_lazy_compilation();
3484 inline void set_allows_lazy_compilation(bool flag);
3485
Iain Merrick75681382010-08-19 15:07:18 +01003486 // Indicates how many full GCs this function has survived with assigned
3487 // code object. Used to determine when it is relatively safe to flush
3488 // this code object and replace it with lazy compilation stub.
3489 // Age is reset when GC notices that the code object is referenced
3490 // from the stack or compilation cache.
3491 inline int code_age();
3492 inline void set_code_age(int age);
3493
3494
Andrei Popescu402d9372010-02-26 13:31:12 +00003495 // Check whether a inlined constructor can be generated with the given
3496 // prototype.
3497 bool CanGenerateInlineConstructor(Object* prototype);
3498
Steve Blocka7e24c12009-10-30 11:49:00 +00003499 // For functions which only contains this property assignments this provides
3500 // access to the names for the properties assigned.
3501 DECL_ACCESSORS(this_property_assignments, Object)
3502 inline int this_property_assignments_count();
3503 inline void set_this_property_assignments_count(int value);
3504 String* GetThisPropertyAssignmentName(int index);
3505 bool IsThisPropertyAssignmentArgument(int index);
3506 int GetThisPropertyAssignmentArgument(int index);
3507 Object* GetThisPropertyAssignmentConstant(int index);
3508
3509 // [source code]: Source code for the function.
3510 bool HasSourceCode();
3511 Object* GetSourceCode();
3512
3513 // Calculate the instance size.
3514 int CalculateInstanceSize();
3515
3516 // Calculate the number of in-object properties.
3517 int CalculateInObjectProperties();
3518
3519 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00003520 // Set max_length to -1 for unlimited length.
3521 void SourceCodePrint(StringStream* accumulator, int max_length);
3522#ifdef DEBUG
3523 void SharedFunctionInfoPrint();
3524 void SharedFunctionInfoVerify();
3525#endif
3526
3527 // Casting.
3528 static inline SharedFunctionInfo* cast(Object* obj);
3529
3530 // Constants.
3531 static const int kDontAdaptArgumentsSentinel = -1;
3532
3533 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01003534 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00003535 static const int kNameOffset = HeapObject::kHeaderSize;
3536 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01003537 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
3538 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003539 static const int kInstanceClassNameOffset =
3540 kConstructStubOffset + kPointerSize;
3541 static const int kFunctionDataOffset =
3542 kInstanceClassNameOffset + kPointerSize;
3543 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
3544 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
3545 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
3546 static const int kThisPropertyAssignmentsOffset =
3547 kInferredNameOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003548#if V8_HOST_ARCH_32_BIT
3549 // Smi fields.
Steve Block6ded16b2010-05-10 14:33:55 +01003550 static const int kLengthOffset =
3551 kThisPropertyAssignmentsOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003552 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
3553 static const int kExpectedNofPropertiesOffset =
3554 kFormalParameterCountOffset + kPointerSize;
3555 static const int kNumLiteralsOffset =
3556 kExpectedNofPropertiesOffset + kPointerSize;
3557 static const int kStartPositionAndTypeOffset =
3558 kNumLiteralsOffset + kPointerSize;
3559 static const int kEndPositionOffset =
3560 kStartPositionAndTypeOffset + kPointerSize;
3561 static const int kFunctionTokenPositionOffset =
3562 kEndPositionOffset + kPointerSize;
3563 static const int kCompilerHintsOffset =
3564 kFunctionTokenPositionOffset + kPointerSize;
3565 static const int kThisPropertyAssignmentsCountOffset =
3566 kCompilerHintsOffset + kPointerSize;
3567 // Total size.
3568 static const int kSize = kThisPropertyAssignmentsCountOffset + kPointerSize;
3569#else
3570 // The only reason to use smi fields instead of int fields
3571 // is to allow interation without maps decoding during
3572 // garbage collections.
3573 // To avoid wasting space on 64-bit architectures we use
3574 // the following trick: we group integer fields into pairs
3575 // First integer in each pair is shifted left by 1.
3576 // By doing this we guarantee that LSB of each kPointerSize aligned
3577 // word is not set and thus this word cannot be treated as pointer
3578 // to HeapObject during old space traversal.
3579 static const int kLengthOffset =
3580 kThisPropertyAssignmentsOffset + kPointerSize;
3581 static const int kFormalParameterCountOffset =
3582 kLengthOffset + kIntSize;
3583
Steve Blocka7e24c12009-10-30 11:49:00 +00003584 static const int kExpectedNofPropertiesOffset =
3585 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003586 static const int kNumLiteralsOffset =
3587 kExpectedNofPropertiesOffset + kIntSize;
3588
3589 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003590 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003591 static const int kStartPositionAndTypeOffset =
3592 kEndPositionOffset + kIntSize;
3593
3594 static const int kFunctionTokenPositionOffset =
3595 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003596 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00003597 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003598
Steve Blocka7e24c12009-10-30 11:49:00 +00003599 static const int kThisPropertyAssignmentsCountOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01003600 kCompilerHintsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003601
Steve Block6ded16b2010-05-10 14:33:55 +01003602 // Total size.
3603 static const int kSize = kThisPropertyAssignmentsCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003604
3605#endif
Steve Block6ded16b2010-05-10 14:33:55 +01003606 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003607
Iain Merrick75681382010-08-19 15:07:18 +01003608 typedef FixedBodyDescriptor<kNameOffset,
3609 kThisPropertyAssignmentsOffset + kPointerSize,
3610 kSize> BodyDescriptor;
3611
Steve Blocka7e24c12009-10-30 11:49:00 +00003612 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00003613 // Bit positions in start_position_and_type.
3614 // The source code start position is in the 30 most significant bits of
3615 // the start_position_and_type field.
3616 static const int kIsExpressionBit = 0;
3617 static const int kIsTopLevelBit = 1;
3618 static const int kStartPositionShift = 2;
3619 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
3620
3621 // Bit positions in compiler_hints.
Steve Blockd0582a62009-12-15 09:54:21 +00003622 static const int kHasOnlySimpleThisPropertyAssignments = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +00003623 static const int kTryFullCodegen = 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01003624 static const int kAllowLazyCompilation = 2;
Iain Merrick75681382010-08-19 15:07:18 +01003625 static const int kCodeAgeShift = 3;
3626 static const int kCodeAgeMask = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +00003627
3628 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
3629};
3630
3631
3632// JSFunction describes JavaScript functions.
3633class JSFunction: public JSObject {
3634 public:
3635 // [prototype_or_initial_map]:
3636 DECL_ACCESSORS(prototype_or_initial_map, Object)
3637
3638 // [shared_function_info]: The information about the function that
3639 // can be shared by instances.
3640 DECL_ACCESSORS(shared, SharedFunctionInfo)
3641
Iain Merrick75681382010-08-19 15:07:18 +01003642 inline SharedFunctionInfo* unchecked_shared();
3643
Steve Blocka7e24c12009-10-30 11:49:00 +00003644 // [context]: The context for this function.
3645 inline Context* context();
3646 inline Object* unchecked_context();
3647 inline void set_context(Object* context);
3648
3649 // [code]: The generated code object for this function. Executed
3650 // when the function is invoked, e.g. foo() or new foo(). See
3651 // [[Call]] and [[Construct]] description in ECMA-262, section
3652 // 8.6.2, page 27.
3653 inline Code* code();
3654 inline void set_code(Code* value);
3655
Iain Merrick75681382010-08-19 15:07:18 +01003656 inline Code* unchecked_code();
3657
Steve Blocka7e24c12009-10-30 11:49:00 +00003658 // Tells whether this function is builtin.
3659 inline bool IsBuiltin();
3660
3661 // [literals]: Fixed array holding the materialized literals.
3662 //
3663 // If the function contains object, regexp or array literals, the
3664 // literals array prefix contains the object, regexp, and array
3665 // function to be used when creating these literals. This is
3666 // necessary so that we do not dynamically lookup the object, regexp
3667 // or array functions. Performing a dynamic lookup, we might end up
3668 // using the functions from a new context that we should not have
3669 // access to.
3670 DECL_ACCESSORS(literals, FixedArray)
3671
3672 // The initial map for an object created by this constructor.
3673 inline Map* initial_map();
3674 inline void set_initial_map(Map* value);
3675 inline bool has_initial_map();
3676
3677 // Get and set the prototype property on a JSFunction. If the
3678 // function has an initial map the prototype is set on the initial
3679 // map. Otherwise, the prototype is put in the initial map field
3680 // until an initial map is needed.
3681 inline bool has_prototype();
3682 inline bool has_instance_prototype();
3683 inline Object* prototype();
3684 inline Object* instance_prototype();
3685 Object* SetInstancePrototype(Object* value);
3686 Object* SetPrototype(Object* value);
3687
Steve Block6ded16b2010-05-10 14:33:55 +01003688 // After prototype is removed, it will not be created when accessed, and
3689 // [[Construct]] from this function will not be allowed.
3690 Object* RemovePrototype();
3691 inline bool should_have_prototype();
3692
Steve Blocka7e24c12009-10-30 11:49:00 +00003693 // Accessor for this function's initial map's [[class]]
3694 // property. This is primarily used by ECMA native functions. This
3695 // method sets the class_name field of this function's initial map
3696 // to a given value. It creates an initial map if this function does
3697 // not have one. Note that this method does not copy the initial map
3698 // if it has one already, but simply replaces it with the new value.
3699 // Instances created afterwards will have a map whose [[class]] is
3700 // set to 'value', but there is no guarantees on instances created
3701 // before.
3702 Object* SetInstanceClassName(String* name);
3703
3704 // Returns if this function has been compiled to native code yet.
3705 inline bool is_compiled();
3706
3707 // Casting.
3708 static inline JSFunction* cast(Object* obj);
3709
3710 // Dispatched behavior.
3711#ifdef DEBUG
3712 void JSFunctionPrint();
3713 void JSFunctionVerify();
3714#endif
3715
3716 // Returns the number of allocated literals.
3717 inline int NumberOfLiterals();
3718
3719 // Retrieve the global context from a function's literal array.
3720 static Context* GlobalContextFromLiterals(FixedArray* literals);
3721
3722 // Layout descriptors.
Iain Merrick75681382010-08-19 15:07:18 +01003723 static const int kCodeOffset = JSObject::kHeaderSize;
3724 static const int kPrototypeOrInitialMapOffset =
3725 kCodeOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003726 static const int kSharedFunctionInfoOffset =
3727 kPrototypeOrInitialMapOffset + kPointerSize;
3728 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
3729 static const int kLiteralsOffset = kContextOffset + kPointerSize;
3730 static const int kSize = kLiteralsOffset + kPointerSize;
3731
3732 // Layout of the literals array.
3733 static const int kLiteralsPrefixSize = 1;
3734 static const int kLiteralGlobalContextIndex = 0;
3735 private:
3736 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
3737};
3738
3739
3740// JSGlobalProxy's prototype must be a JSGlobalObject or null,
3741// and the prototype is hidden. JSGlobalProxy always delegates
3742// property accesses to its prototype if the prototype is not null.
3743//
3744// A JSGlobalProxy can be reinitialized which will preserve its identity.
3745//
3746// Accessing a JSGlobalProxy requires security check.
3747
3748class JSGlobalProxy : public JSObject {
3749 public:
3750 // [context]: the owner global context of this proxy object.
3751 // It is null value if this object is not used by any context.
3752 DECL_ACCESSORS(context, Object)
3753
3754 // Casting.
3755 static inline JSGlobalProxy* cast(Object* obj);
3756
3757 // Dispatched behavior.
3758#ifdef DEBUG
3759 void JSGlobalProxyPrint();
3760 void JSGlobalProxyVerify();
3761#endif
3762
3763 // Layout description.
3764 static const int kContextOffset = JSObject::kHeaderSize;
3765 static const int kSize = kContextOffset + kPointerSize;
3766
3767 private:
3768
3769 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
3770};
3771
3772
3773// Forward declaration.
3774class JSBuiltinsObject;
3775
3776// Common super class for JavaScript global objects and the special
3777// builtins global objects.
3778class GlobalObject: public JSObject {
3779 public:
3780 // [builtins]: the object holding the runtime routines written in JS.
3781 DECL_ACCESSORS(builtins, JSBuiltinsObject)
3782
3783 // [global context]: the global context corresponding to this global object.
3784 DECL_ACCESSORS(global_context, Context)
3785
3786 // [global receiver]: the global receiver object of the context
3787 DECL_ACCESSORS(global_receiver, JSObject)
3788
3789 // Retrieve the property cell used to store a property.
3790 Object* GetPropertyCell(LookupResult* result);
3791
3792 // Ensure that the global object has a cell for the given property name.
3793 Object* EnsurePropertyCell(String* name);
3794
3795 // Casting.
3796 static inline GlobalObject* cast(Object* obj);
3797
3798 // Layout description.
3799 static const int kBuiltinsOffset = JSObject::kHeaderSize;
3800 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
3801 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
3802 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
3803
3804 private:
3805 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
3806
3807 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
3808};
3809
3810
3811// JavaScript global object.
3812class JSGlobalObject: public GlobalObject {
3813 public:
3814
3815 // Casting.
3816 static inline JSGlobalObject* cast(Object* obj);
3817
3818 // Dispatched behavior.
3819#ifdef DEBUG
3820 void JSGlobalObjectPrint();
3821 void JSGlobalObjectVerify();
3822#endif
3823
3824 // Layout description.
3825 static const int kSize = GlobalObject::kHeaderSize;
3826
3827 private:
3828 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
3829};
3830
3831
3832// Builtins global object which holds the runtime routines written in
3833// JavaScript.
3834class JSBuiltinsObject: public GlobalObject {
3835 public:
3836 // Accessors for the runtime routines written in JavaScript.
3837 inline Object* javascript_builtin(Builtins::JavaScript id);
3838 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
3839
Steve Block6ded16b2010-05-10 14:33:55 +01003840 // Accessors for code of the runtime routines written in JavaScript.
3841 inline Code* javascript_builtin_code(Builtins::JavaScript id);
3842 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
3843
Steve Blocka7e24c12009-10-30 11:49:00 +00003844 // Casting.
3845 static inline JSBuiltinsObject* cast(Object* obj);
3846
3847 // Dispatched behavior.
3848#ifdef DEBUG
3849 void JSBuiltinsObjectPrint();
3850 void JSBuiltinsObjectVerify();
3851#endif
3852
3853 // Layout description. The size of the builtins object includes
Steve Block6ded16b2010-05-10 14:33:55 +01003854 // room for two pointers per runtime routine written in javascript
3855 // (function and code object).
Steve Blocka7e24c12009-10-30 11:49:00 +00003856 static const int kJSBuiltinsCount = Builtins::id_count;
3857 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003858 static const int kJSBuiltinsCodeOffset =
3859 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00003860 static const int kSize =
Steve Block6ded16b2010-05-10 14:33:55 +01003861 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
3862
3863 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
3864 return kJSBuiltinsOffset + id * kPointerSize;
3865 }
3866
3867 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
3868 return kJSBuiltinsCodeOffset + id * kPointerSize;
3869 }
3870
Steve Blocka7e24c12009-10-30 11:49:00 +00003871 private:
3872 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
3873};
3874
3875
3876// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
3877class JSValue: public JSObject {
3878 public:
3879 // [value]: the object being wrapped.
3880 DECL_ACCESSORS(value, Object)
3881
3882 // Casting.
3883 static inline JSValue* cast(Object* obj);
3884
3885 // Dispatched behavior.
3886#ifdef DEBUG
3887 void JSValuePrint();
3888 void JSValueVerify();
3889#endif
3890
3891 // Layout description.
3892 static const int kValueOffset = JSObject::kHeaderSize;
3893 static const int kSize = kValueOffset + kPointerSize;
3894
3895 private:
3896 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
3897};
3898
3899// Regular expressions
3900// The regular expression holds a single reference to a FixedArray in
3901// the kDataOffset field.
3902// The FixedArray contains the following data:
3903// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
3904// - reference to the original source string
3905// - reference to the original flag string
3906// If it is an atom regexp
3907// - a reference to a literal string to search for
3908// If it is an irregexp regexp:
3909// - a reference to code for ASCII inputs (bytecode or compiled).
3910// - a reference to code for UC16 inputs (bytecode or compiled).
3911// - max number of registers used by irregexp implementations.
3912// - number of capture registers (output values) of the regexp.
3913class JSRegExp: public JSObject {
3914 public:
3915 // Meaning of Type:
3916 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
3917 // ATOM: A simple string to match against using an indexOf operation.
3918 // IRREGEXP: Compiled with Irregexp.
3919 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
3920 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
3921 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
3922
3923 class Flags {
3924 public:
3925 explicit Flags(uint32_t value) : value_(value) { }
3926 bool is_global() { return (value_ & GLOBAL) != 0; }
3927 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
3928 bool is_multiline() { return (value_ & MULTILINE) != 0; }
3929 uint32_t value() { return value_; }
3930 private:
3931 uint32_t value_;
3932 };
3933
3934 DECL_ACCESSORS(data, Object)
3935
3936 inline Type TypeTag();
3937 inline int CaptureCount();
3938 inline Flags GetFlags();
3939 inline String* Pattern();
3940 inline Object* DataAt(int index);
3941 // Set implementation data after the object has been prepared.
3942 inline void SetDataAt(int index, Object* value);
3943 static int code_index(bool is_ascii) {
3944 if (is_ascii) {
3945 return kIrregexpASCIICodeIndex;
3946 } else {
3947 return kIrregexpUC16CodeIndex;
3948 }
3949 }
3950
3951 static inline JSRegExp* cast(Object* obj);
3952
3953 // Dispatched behavior.
3954#ifdef DEBUG
3955 void JSRegExpVerify();
3956#endif
3957
3958 static const int kDataOffset = JSObject::kHeaderSize;
3959 static const int kSize = kDataOffset + kPointerSize;
3960
3961 // Indices in the data array.
3962 static const int kTagIndex = 0;
3963 static const int kSourceIndex = kTagIndex + 1;
3964 static const int kFlagsIndex = kSourceIndex + 1;
3965 static const int kDataIndex = kFlagsIndex + 1;
3966 // The data fields are used in different ways depending on the
3967 // value of the tag.
3968 // Atom regexps (literal strings).
3969 static const int kAtomPatternIndex = kDataIndex;
3970
3971 static const int kAtomDataSize = kAtomPatternIndex + 1;
3972
3973 // Irregexp compiled code or bytecode for ASCII. If compilation
3974 // fails, this fields hold an exception object that should be
3975 // thrown if the regexp is used again.
3976 static const int kIrregexpASCIICodeIndex = kDataIndex;
3977 // Irregexp compiled code or bytecode for UC16. If compilation
3978 // fails, this fields hold an exception object that should be
3979 // thrown if the regexp is used again.
3980 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
3981 // Maximal number of registers used by either ASCII or UC16.
3982 // Only used to check that there is enough stack space
3983 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
3984 // Number of captures in the compiled regexp.
3985 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
3986
3987 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00003988
3989 // Offsets directly into the data fixed array.
3990 static const int kDataTagOffset =
3991 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
3992 static const int kDataAsciiCodeOffset =
3993 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00003994 static const int kDataUC16CodeOffset =
3995 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00003996 static const int kIrregexpCaptureCountOffset =
3997 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003998
3999 // In-object fields.
4000 static const int kSourceFieldIndex = 0;
4001 static const int kGlobalFieldIndex = 1;
4002 static const int kIgnoreCaseFieldIndex = 2;
4003 static const int kMultilineFieldIndex = 3;
4004 static const int kLastIndexFieldIndex = 4;
Ben Murdochbb769b22010-08-11 14:56:33 +01004005 static const int kInObjectFieldCount = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00004006};
4007
4008
4009class CompilationCacheShape {
4010 public:
4011 static inline bool IsMatch(HashTableKey* key, Object* value) {
4012 return key->IsMatch(value);
4013 }
4014
4015 static inline uint32_t Hash(HashTableKey* key) {
4016 return key->Hash();
4017 }
4018
4019 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4020 return key->HashForObject(object);
4021 }
4022
4023 static Object* AsObject(HashTableKey* key) {
4024 return key->AsObject();
4025 }
4026
4027 static const int kPrefixSize = 0;
4028 static const int kEntrySize = 2;
4029};
4030
Steve Block3ce2e202009-11-05 08:53:23 +00004031
Steve Blocka7e24c12009-10-30 11:49:00 +00004032class CompilationCacheTable: public HashTable<CompilationCacheShape,
4033 HashTableKey*> {
4034 public:
4035 // Find cached value for a string key, otherwise return null.
4036 Object* Lookup(String* src);
4037 Object* LookupEval(String* src, Context* context);
4038 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
4039 Object* Put(String* src, Object* value);
4040 Object* PutEval(String* src, Context* context, Object* value);
4041 Object* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
4042
4043 static inline CompilationCacheTable* cast(Object* obj);
4044
4045 private:
4046 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
4047};
4048
4049
Steve Block6ded16b2010-05-10 14:33:55 +01004050class CodeCache: public Struct {
4051 public:
4052 DECL_ACCESSORS(default_cache, FixedArray)
4053 DECL_ACCESSORS(normal_type_cache, Object)
4054
4055 // Add the code object to the cache.
4056 Object* Update(String* name, Code* code);
4057
4058 // Lookup code object in the cache. Returns code object if found and undefined
4059 // if not.
4060 Object* Lookup(String* name, Code::Flags flags);
4061
4062 // Get the internal index of a code object in the cache. Returns -1 if the
4063 // code object is not in that cache. This index can be used to later call
4064 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
4065 // RemoveByIndex.
4066 int GetIndex(Object* name, Code* code);
4067
4068 // Remove an object from the cache with the provided internal index.
4069 void RemoveByIndex(Object* name, Code* code, int index);
4070
4071 static inline CodeCache* cast(Object* obj);
4072
4073#ifdef DEBUG
4074 void CodeCachePrint();
4075 void CodeCacheVerify();
4076#endif
4077
4078 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
4079 static const int kNormalTypeCacheOffset =
4080 kDefaultCacheOffset + kPointerSize;
4081 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
4082
4083 private:
4084 Object* UpdateDefaultCache(String* name, Code* code);
4085 Object* UpdateNormalTypeCache(String* name, Code* code);
4086 Object* LookupDefaultCache(String* name, Code::Flags flags);
4087 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
4088
4089 // Code cache layout of the default cache. Elements are alternating name and
4090 // code objects for non normal load/store/call IC's.
4091 static const int kCodeCacheEntrySize = 2;
4092 static const int kCodeCacheEntryNameOffset = 0;
4093 static const int kCodeCacheEntryCodeOffset = 1;
4094
4095 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
4096};
4097
4098
4099class CodeCacheHashTableShape {
4100 public:
4101 static inline bool IsMatch(HashTableKey* key, Object* value) {
4102 return key->IsMatch(value);
4103 }
4104
4105 static inline uint32_t Hash(HashTableKey* key) {
4106 return key->Hash();
4107 }
4108
4109 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
4110 return key->HashForObject(object);
4111 }
4112
4113 static Object* AsObject(HashTableKey* key) {
4114 return key->AsObject();
4115 }
4116
4117 static const int kPrefixSize = 0;
4118 static const int kEntrySize = 2;
4119};
4120
4121
4122class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
4123 HashTableKey*> {
4124 public:
4125 Object* Lookup(String* name, Code::Flags flags);
4126 Object* Put(String* name, Code* code);
4127
4128 int GetIndex(String* name, Code::Flags flags);
4129 void RemoveByIndex(int index);
4130
4131 static inline CodeCacheHashTable* cast(Object* obj);
4132
4133 // Initial size of the fixed array backing the hash table.
4134 static const int kInitialSize = 64;
4135
4136 private:
4137 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
4138};
4139
4140
Steve Blocka7e24c12009-10-30 11:49:00 +00004141enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
4142enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
4143
4144
4145class StringHasher {
4146 public:
4147 inline StringHasher(int length);
4148
4149 // Returns true if the hash of this string can be computed without
4150 // looking at the contents.
4151 inline bool has_trivial_hash();
4152
4153 // Add a character to the hash and update the array index calculation.
4154 inline void AddCharacter(uc32 c);
4155
4156 // Adds a character to the hash but does not update the array index
4157 // calculation. This can only be called when it has been verified
4158 // that the input is not an array index.
4159 inline void AddCharacterNoIndex(uc32 c);
4160
4161 // Returns the value to store in the hash field of a string with
4162 // the given length and contents.
4163 uint32_t GetHashField();
4164
4165 // Returns true if the characters seen so far make up a legal array
4166 // index.
4167 bool is_array_index() { return is_array_index_; }
4168
4169 bool is_valid() { return is_valid_; }
4170
4171 void invalidate() { is_valid_ = false; }
4172
4173 private:
4174
4175 uint32_t array_index() {
4176 ASSERT(is_array_index());
4177 return array_index_;
4178 }
4179
4180 inline uint32_t GetHash();
4181
4182 int length_;
4183 uint32_t raw_running_hash_;
4184 uint32_t array_index_;
4185 bool is_array_index_;
4186 bool is_first_char_;
4187 bool is_valid_;
Steve Blockd0582a62009-12-15 09:54:21 +00004188 friend class TwoCharHashTableKey;
Steve Blocka7e24c12009-10-30 11:49:00 +00004189};
4190
4191
4192// The characteristics of a string are stored in its map. Retrieving these
4193// few bits of information is moderately expensive, involving two memory
4194// loads where the second is dependent on the first. To improve efficiency
4195// the shape of the string is given its own class so that it can be retrieved
4196// once and used for several string operations. A StringShape is small enough
4197// to be passed by value and is immutable, but be aware that flattening a
4198// string can potentially alter its shape. Also be aware that a GC caused by
4199// something else can alter the shape of a string due to ConsString
4200// shortcutting. Keeping these restrictions in mind has proven to be error-
4201// prone and so we no longer put StringShapes in variables unless there is a
4202// concrete performance benefit at that particular point in the code.
4203class StringShape BASE_EMBEDDED {
4204 public:
4205 inline explicit StringShape(String* s);
4206 inline explicit StringShape(Map* s);
4207 inline explicit StringShape(InstanceType t);
4208 inline bool IsSequential();
4209 inline bool IsExternal();
4210 inline bool IsCons();
Steve Blocka7e24c12009-10-30 11:49:00 +00004211 inline bool IsExternalAscii();
4212 inline bool IsExternalTwoByte();
4213 inline bool IsSequentialAscii();
4214 inline bool IsSequentialTwoByte();
4215 inline bool IsSymbol();
4216 inline StringRepresentationTag representation_tag();
4217 inline uint32_t full_representation_tag();
4218 inline uint32_t size_tag();
4219#ifdef DEBUG
4220 inline uint32_t type() { return type_; }
4221 inline void invalidate() { valid_ = false; }
4222 inline bool valid() { return valid_; }
4223#else
4224 inline void invalidate() { }
4225#endif
4226 private:
4227 uint32_t type_;
4228#ifdef DEBUG
4229 inline void set_valid() { valid_ = true; }
4230 bool valid_;
4231#else
4232 inline void set_valid() { }
4233#endif
4234};
4235
4236
4237// The String abstract class captures JavaScript string values:
4238//
4239// Ecma-262:
4240// 4.3.16 String Value
4241// A string value is a member of the type String and is a finite
4242// ordered sequence of zero or more 16-bit unsigned integer values.
4243//
4244// All string values have a length field.
4245class String: public HeapObject {
4246 public:
4247 // Get and set the length of the string.
4248 inline int length();
4249 inline void set_length(int value);
4250
Steve Blockd0582a62009-12-15 09:54:21 +00004251 // Get and set the hash field of the string.
4252 inline uint32_t hash_field();
4253 inline void set_hash_field(uint32_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00004254
4255 inline bool IsAsciiRepresentation();
4256 inline bool IsTwoByteRepresentation();
4257
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004258 // Returns whether this string has ascii chars, i.e. all of them can
4259 // be ascii encoded. This might be the case even if the string is
4260 // two-byte. Such strings may appear when the embedder prefers
4261 // two-byte external representations even for ascii data.
Steve Block6ded16b2010-05-10 14:33:55 +01004262 //
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01004263 // NOTE: this should be considered only a hint. False negatives are
4264 // possible.
4265 inline bool HasOnlyAsciiChars();
Steve Block6ded16b2010-05-10 14:33:55 +01004266
Steve Blocka7e24c12009-10-30 11:49:00 +00004267 // Get and set individual two byte chars in the string.
4268 inline void Set(int index, uint16_t value);
4269 // Get individual two byte char in the string. Repeated calls
4270 // to this method are not efficient unless the string is flat.
4271 inline uint16_t Get(int index);
4272
Leon Clarkef7060e22010-06-03 12:02:55 +01004273 // Try to flatten the string. Checks first inline to see if it is
4274 // necessary. Does nothing if the string is not a cons string.
4275 // Flattening allocates a sequential string with the same data as
4276 // the given string and mutates the cons string to a degenerate
4277 // form, where the first component is the new sequential string and
4278 // the second component is the empty string. If allocation fails,
4279 // this function returns a failure. If flattening succeeds, this
4280 // function returns the sequential string that is now the first
4281 // component of the cons string.
4282 //
4283 // Degenerate cons strings are handled specially by the garbage
4284 // collector (see IsShortcutCandidate).
4285 //
4286 // Use FlattenString from Handles.cc to flatten even in case an
4287 // allocation failure happens.
Steve Block6ded16b2010-05-10 14:33:55 +01004288 inline Object* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004289
Leon Clarkef7060e22010-06-03 12:02:55 +01004290 // Convenience function. Has exactly the same behavior as
4291 // TryFlatten(), except in the case of failure returns the original
4292 // string.
4293 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
4294
Steve Blocka7e24c12009-10-30 11:49:00 +00004295 Vector<const char> ToAsciiVector();
4296 Vector<const uc16> ToUC16Vector();
4297
4298 // Mark the string as an undetectable object. It only applies to
4299 // ascii and two byte string types.
4300 bool MarkAsUndetectable();
4301
Steve Blockd0582a62009-12-15 09:54:21 +00004302 // Return a substring.
Steve Block6ded16b2010-05-10 14:33:55 +01004303 Object* SubString(int from, int to, PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00004304
4305 // String equality operations.
4306 inline bool Equals(String* other);
4307 bool IsEqualTo(Vector<const char> str);
4308
4309 // Return a UTF8 representation of the string. The string is null
4310 // terminated but may optionally contain nulls. Length is returned
4311 // in length_output if length_output is not a null pointer The string
4312 // should be nearly flat, otherwise the performance of this method may
4313 // be very slow (quadratic in the length). Setting robustness_flag to
4314 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4315 // handles unexpected data without causing assert failures and it does not
4316 // do any heap allocations. This is useful when printing stack traces.
4317 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
4318 RobustnessFlag robustness_flag,
4319 int offset,
4320 int length,
4321 int* length_output = 0);
4322 SmartPointer<char> ToCString(
4323 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
4324 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
4325 int* length_output = 0);
4326
4327 int Utf8Length();
4328
4329 // Return a 16 bit Unicode representation of the string.
4330 // The string should be nearly flat, otherwise the performance of
4331 // of this method may be very bad. Setting robustness_flag to
4332 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
4333 // handles unexpected data without causing assert failures and it does not
4334 // do any heap allocations. This is useful when printing stack traces.
4335 SmartPointer<uc16> ToWideCString(
4336 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
4337
4338 // Tells whether the hash code has been computed.
4339 inline bool HasHashCode();
4340
4341 // Returns a hash value used for the property table
4342 inline uint32_t Hash();
4343
Steve Blockd0582a62009-12-15 09:54:21 +00004344 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
4345 int length);
Steve Blocka7e24c12009-10-30 11:49:00 +00004346
4347 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
4348 uint32_t* index,
4349 int length);
4350
4351 // Externalization.
4352 bool MakeExternal(v8::String::ExternalStringResource* resource);
4353 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
4354
4355 // Conversion.
4356 inline bool AsArrayIndex(uint32_t* index);
4357
4358 // Casting.
4359 static inline String* cast(Object* obj);
4360
4361 void PrintOn(FILE* out);
4362
4363 // For use during stack traces. Performs rudimentary sanity check.
4364 bool LooksValid();
4365
4366 // Dispatched behavior.
4367 void StringShortPrint(StringStream* accumulator);
4368#ifdef DEBUG
4369 void StringPrint();
4370 void StringVerify();
4371#endif
4372 inline bool IsFlat();
4373
4374 // Layout description.
4375 static const int kLengthOffset = HeapObject::kHeaderSize;
Steve Block6ded16b2010-05-10 14:33:55 +01004376 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004377 static const int kSize = kHashFieldOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00004378
Steve Blockd0582a62009-12-15 09:54:21 +00004379 // Maximum number of characters to consider when trying to convert a string
4380 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00004381 static const int kMaxArrayIndexSize = 10;
4382
4383 // Max ascii char code.
4384 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
4385 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
4386 static const int kMaxUC16CharCode = 0xffff;
4387
Steve Blockd0582a62009-12-15 09:54:21 +00004388 // Minimum length for a cons string.
Steve Blocka7e24c12009-10-30 11:49:00 +00004389 static const int kMinNonFlatLength = 13;
4390
4391 // Mask constant for checking if a string has a computed hash code
4392 // and if it is an array index. The least significant bit indicates
4393 // whether a hash code has been computed. If the hash code has been
4394 // computed the 2nd bit tells whether the string can be used as an
4395 // array index.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004396 static const int kHashNotComputedMask = 1;
4397 static const int kIsNotArrayIndexMask = 1 << 1;
4398 static const int kNofHashBitFields = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +00004399
Steve Blockd0582a62009-12-15 09:54:21 +00004400 // Shift constant retrieving hash code from hash field.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004401 static const int kHashShift = kNofHashBitFields;
Steve Blockd0582a62009-12-15 09:54:21 +00004402
Steve Blocka7e24c12009-10-30 11:49:00 +00004403 // Array index strings this short can keep their index in the hash
4404 // field.
4405 static const int kMaxCachedArrayIndexLength = 7;
4406
Steve Blockd0582a62009-12-15 09:54:21 +00004407 // For strings which are array indexes the hash value has the string length
4408 // mixed into the hash, mainly to avoid a hash value of zero which would be
4409 // the case for the string '0'. 24 bits are used for the array index value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004410 static const int kArrayIndexValueBits = 24;
4411 static const int kArrayIndexLengthBits =
4412 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
4413
4414 STATIC_CHECK((kArrayIndexLengthBits > 0));
4415
4416 static const int kArrayIndexHashLengthShift =
4417 kArrayIndexValueBits + kNofHashBitFields;
4418
Steve Blockd0582a62009-12-15 09:54:21 +00004419 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004420
4421 static const int kArrayIndexValueMask =
4422 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
4423
4424 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
4425 // could use a mask to test if the length of string is less than or equal to
4426 // kMaxCachedArrayIndexLength.
4427 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
4428
4429 static const int kContainsCachedArrayIndexMask =
4430 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
4431 kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004432
4433 // Value of empty hash field indicating that the hash is not computed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004434 static const int kEmptyHashField =
4435 kIsNotArrayIndexMask | kHashNotComputedMask;
4436
4437 // Value of hash field containing computed hash equal to zero.
4438 static const int kZeroHash = kIsNotArrayIndexMask;
Steve Blockd0582a62009-12-15 09:54:21 +00004439
4440 // Maximal string length.
4441 static const int kMaxLength = (1 << (32 - 2)) - 1;
4442
4443 // Max length for computing hash. For strings longer than this limit the
4444 // string length is used as the hash value.
4445 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00004446
4447 // Limit for truncation in short printing.
4448 static const int kMaxShortPrintLength = 1024;
4449
4450 // Support for regular expressions.
4451 const uc16* GetTwoByteData();
4452 const uc16* GetTwoByteData(unsigned start);
4453
4454 // Support for StringInputBuffer
4455 static const unibrow::byte* ReadBlock(String* input,
4456 unibrow::byte* util_buffer,
4457 unsigned capacity,
4458 unsigned* remaining,
4459 unsigned* offset);
4460 static const unibrow::byte* ReadBlock(String** input,
4461 unibrow::byte* util_buffer,
4462 unsigned capacity,
4463 unsigned* remaining,
4464 unsigned* offset);
4465
4466 // Helper function for flattening strings.
4467 template <typename sinkchar>
4468 static void WriteToFlat(String* source,
4469 sinkchar* sink,
4470 int from,
4471 int to);
4472
4473 protected:
4474 class ReadBlockBuffer {
4475 public:
4476 ReadBlockBuffer(unibrow::byte* util_buffer_,
4477 unsigned cursor_,
4478 unsigned capacity_,
4479 unsigned remaining_) :
4480 util_buffer(util_buffer_),
4481 cursor(cursor_),
4482 capacity(capacity_),
4483 remaining(remaining_) {
4484 }
4485 unibrow::byte* util_buffer;
4486 unsigned cursor;
4487 unsigned capacity;
4488 unsigned remaining;
4489 };
4490
Steve Blocka7e24c12009-10-30 11:49:00 +00004491 static inline const unibrow::byte* ReadBlock(String* input,
4492 ReadBlockBuffer* buffer,
4493 unsigned* offset,
4494 unsigned max_chars);
4495 static void ReadBlockIntoBuffer(String* input,
4496 ReadBlockBuffer* buffer,
4497 unsigned* offset_ptr,
4498 unsigned max_chars);
4499
4500 private:
Leon Clarkef7060e22010-06-03 12:02:55 +01004501 // Try to flatten the top level ConsString that is hiding behind this
4502 // string. This is a no-op unless the string is a ConsString. Flatten
4503 // mutates the ConsString and might return a failure.
4504 Object* SlowTryFlatten(PretenureFlag pretenure);
4505
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004506 static inline bool IsHashFieldComputed(uint32_t field);
4507
Steve Blocka7e24c12009-10-30 11:49:00 +00004508 // Slow case of String::Equals. This implementation works on any strings
4509 // but it is most efficient on strings that are almost flat.
4510 bool SlowEquals(String* other);
4511
4512 // Slow case of AsArrayIndex.
4513 bool SlowAsArrayIndex(uint32_t* index);
4514
4515 // Compute and set the hash code.
4516 uint32_t ComputeAndSetHash();
4517
4518 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
4519};
4520
4521
4522// The SeqString abstract class captures sequential string values.
4523class SeqString: public String {
4524 public:
4525
4526 // Casting.
4527 static inline SeqString* cast(Object* obj);
4528
Steve Blocka7e24c12009-10-30 11:49:00 +00004529 private:
4530 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
4531};
4532
4533
4534// The AsciiString class captures sequential ascii string objects.
4535// Each character in the AsciiString is an ascii character.
4536class SeqAsciiString: public SeqString {
4537 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004538 static const bool kHasAsciiEncoding = true;
4539
Steve Blocka7e24c12009-10-30 11:49:00 +00004540 // Dispatched behavior.
4541 inline uint16_t SeqAsciiStringGet(int index);
4542 inline void SeqAsciiStringSet(int index, uint16_t value);
4543
4544 // Get the address of the characters in this string.
4545 inline Address GetCharsAddress();
4546
4547 inline char* GetChars();
4548
4549 // Casting
4550 static inline SeqAsciiString* cast(Object* obj);
4551
4552 // Garbage collection support. This method is called by the
4553 // garbage collector to compute the actual size of an AsciiString
4554 // instance.
4555 inline int SeqAsciiStringSize(InstanceType instance_type);
4556
4557 // Computes the size for an AsciiString instance of a given length.
4558 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004559 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004560 }
4561
4562 // Layout description.
4563 static const int kHeaderSize = String::kSize;
4564 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4565
Leon Clarkee46be812010-01-19 14:06:41 +00004566 // Maximal memory usage for a single sequential ASCII string.
4567 static const int kMaxSize = 512 * MB;
4568 // Maximal length of a single sequential ASCII string.
4569 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4570 static const int kMaxLength = (kMaxSize - kHeaderSize);
4571
Steve Blocka7e24c12009-10-30 11:49:00 +00004572 // Support for StringInputBuffer.
4573 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4574 unsigned* offset,
4575 unsigned chars);
4576 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
4577 unsigned* offset,
4578 unsigned chars);
4579
4580 private:
4581 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
4582};
4583
4584
4585// The TwoByteString class captures sequential unicode string objects.
4586// Each character in the TwoByteString is a two-byte uint16_t.
4587class SeqTwoByteString: public SeqString {
4588 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004589 static const bool kHasAsciiEncoding = false;
4590
Steve Blocka7e24c12009-10-30 11:49:00 +00004591 // Dispatched behavior.
4592 inline uint16_t SeqTwoByteStringGet(int index);
4593 inline void SeqTwoByteStringSet(int index, uint16_t value);
4594
4595 // Get the address of the characters in this string.
4596 inline Address GetCharsAddress();
4597
4598 inline uc16* GetChars();
4599
4600 // For regexp code.
4601 const uint16_t* SeqTwoByteStringGetData(unsigned start);
4602
4603 // Casting
4604 static inline SeqTwoByteString* cast(Object* obj);
4605
4606 // Garbage collection support. This method is called by the
4607 // garbage collector to compute the actual size of a TwoByteString
4608 // instance.
4609 inline int SeqTwoByteStringSize(InstanceType instance_type);
4610
4611 // Computes the size for a TwoByteString instance of a given length.
4612 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004613 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004614 }
4615
4616 // Layout description.
4617 static const int kHeaderSize = String::kSize;
4618 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
4619
Leon Clarkee46be812010-01-19 14:06:41 +00004620 // Maximal memory usage for a single sequential two-byte string.
4621 static const int kMaxSize = 512 * MB;
4622 // Maximal length of a single sequential two-byte string.
4623 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
4624 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
4625
Steve Blocka7e24c12009-10-30 11:49:00 +00004626 // Support for StringInputBuffer.
4627 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4628 unsigned* offset_ptr,
4629 unsigned chars);
4630
4631 private:
4632 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
4633};
4634
4635
4636// The ConsString class describes string values built by using the
4637// addition operator on strings. A ConsString is a pair where the
4638// first and second components are pointers to other string values.
4639// One or both components of a ConsString can be pointers to other
4640// ConsStrings, creating a binary tree of ConsStrings where the leaves
4641// are non-ConsString string values. The string value represented by
4642// a ConsString can be obtained by concatenating the leaf string
4643// values in a left-to-right depth-first traversal of the tree.
4644class ConsString: public String {
4645 public:
4646 // First string of the cons cell.
4647 inline String* first();
4648 // Doesn't check that the result is a string, even in debug mode. This is
4649 // useful during GC where the mark bits confuse the checks.
4650 inline Object* unchecked_first();
4651 inline void set_first(String* first,
4652 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4653
4654 // Second string of the cons cell.
4655 inline String* second();
4656 // Doesn't check that the result is a string, even in debug mode. This is
4657 // useful during GC where the mark bits confuse the checks.
4658 inline Object* unchecked_second();
4659 inline void set_second(String* second,
4660 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4661
4662 // Dispatched behavior.
4663 uint16_t ConsStringGet(int index);
4664
4665 // Casting.
4666 static inline ConsString* cast(Object* obj);
4667
Steve Blocka7e24c12009-10-30 11:49:00 +00004668 // Layout description.
4669 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
4670 static const int kSecondOffset = kFirstOffset + kPointerSize;
4671 static const int kSize = kSecondOffset + kPointerSize;
4672
4673 // Support for StringInputBuffer.
4674 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
4675 unsigned* offset_ptr,
4676 unsigned chars);
4677 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4678 unsigned* offset_ptr,
4679 unsigned chars);
4680
4681 // Minimum length for a cons string.
4682 static const int kMinLength = 13;
4683
Iain Merrick75681382010-08-19 15:07:18 +01004684 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
4685 BodyDescriptor;
4686
Steve Blocka7e24c12009-10-30 11:49:00 +00004687 private:
4688 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
4689};
4690
4691
Steve Blocka7e24c12009-10-30 11:49:00 +00004692// The ExternalString class describes string values that are backed by
4693// a string resource that lies outside the V8 heap. ExternalStrings
4694// consist of the length field common to all strings, a pointer to the
4695// external resource. It is important to ensure (externally) that the
4696// resource is not deallocated while the ExternalString is live in the
4697// V8 heap.
4698//
4699// The API expects that all ExternalStrings are created through the
4700// API. Therefore, ExternalStrings should not be used internally.
4701class ExternalString: public String {
4702 public:
4703 // Casting
4704 static inline ExternalString* cast(Object* obj);
4705
4706 // Layout description.
4707 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
4708 static const int kSize = kResourceOffset + kPointerSize;
4709
4710 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
4711
4712 private:
4713 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
4714};
4715
4716
4717// The ExternalAsciiString class is an external string backed by an
4718// ASCII string.
4719class ExternalAsciiString: public ExternalString {
4720 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004721 static const bool kHasAsciiEncoding = true;
4722
Steve Blocka7e24c12009-10-30 11:49:00 +00004723 typedef v8::String::ExternalAsciiStringResource Resource;
4724
4725 // The underlying resource.
4726 inline Resource* resource();
4727 inline void set_resource(Resource* buffer);
4728
4729 // Dispatched behavior.
4730 uint16_t ExternalAsciiStringGet(int index);
4731
4732 // Casting.
4733 static inline ExternalAsciiString* cast(Object* obj);
4734
Steve Blockd0582a62009-12-15 09:54:21 +00004735 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01004736 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
4737
4738 template<typename StaticVisitor>
4739 inline void ExternalAsciiStringIterateBody();
Steve Blockd0582a62009-12-15 09:54:21 +00004740
Steve Blocka7e24c12009-10-30 11:49:00 +00004741 // Support for StringInputBuffer.
4742 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
4743 unsigned* offset,
4744 unsigned chars);
4745 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4746 unsigned* offset,
4747 unsigned chars);
4748
Steve Blocka7e24c12009-10-30 11:49:00 +00004749 private:
4750 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
4751};
4752
4753
4754// The ExternalTwoByteString class is an external string backed by a UTF-16
4755// encoded string.
4756class ExternalTwoByteString: public ExternalString {
4757 public:
Leon Clarkeac952652010-07-15 11:15:24 +01004758 static const bool kHasAsciiEncoding = false;
4759
Steve Blocka7e24c12009-10-30 11:49:00 +00004760 typedef v8::String::ExternalStringResource Resource;
4761
4762 // The underlying string resource.
4763 inline Resource* resource();
4764 inline void set_resource(Resource* buffer);
4765
4766 // Dispatched behavior.
4767 uint16_t ExternalTwoByteStringGet(int index);
4768
4769 // For regexp code.
4770 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
4771
4772 // Casting.
4773 static inline ExternalTwoByteString* cast(Object* obj);
4774
Steve Blockd0582a62009-12-15 09:54:21 +00004775 // Garbage collection support.
Iain Merrick75681382010-08-19 15:07:18 +01004776 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
4777
4778 template<typename StaticVisitor>
4779 inline void ExternalTwoByteStringIterateBody();
4780
Steve Blockd0582a62009-12-15 09:54:21 +00004781
Steve Blocka7e24c12009-10-30 11:49:00 +00004782 // Support for StringInputBuffer.
4783 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
4784 unsigned* offset_ptr,
4785 unsigned chars);
4786
Steve Blocka7e24c12009-10-30 11:49:00 +00004787 private:
4788 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
4789};
4790
4791
4792// Utility superclass for stack-allocated objects that must be updated
4793// on gc. It provides two ways for the gc to update instances, either
4794// iterating or updating after gc.
4795class Relocatable BASE_EMBEDDED {
4796 public:
4797 inline Relocatable() : prev_(top_) { top_ = this; }
4798 virtual ~Relocatable() {
4799 ASSERT_EQ(top_, this);
4800 top_ = prev_;
4801 }
4802 virtual void IterateInstance(ObjectVisitor* v) { }
4803 virtual void PostGarbageCollection() { }
4804
4805 static void PostGarbageCollectionProcessing();
4806 static int ArchiveSpacePerThread();
4807 static char* ArchiveState(char* to);
4808 static char* RestoreState(char* from);
4809 static void Iterate(ObjectVisitor* v);
4810 static void Iterate(ObjectVisitor* v, Relocatable* top);
4811 static char* Iterate(ObjectVisitor* v, char* t);
4812 private:
4813 static Relocatable* top_;
4814 Relocatable* prev_;
4815};
4816
4817
4818// A flat string reader provides random access to the contents of a
4819// string independent of the character width of the string. The handle
4820// must be valid as long as the reader is being used.
4821class FlatStringReader : public Relocatable {
4822 public:
4823 explicit FlatStringReader(Handle<String> str);
4824 explicit FlatStringReader(Vector<const char> input);
4825 void PostGarbageCollection();
4826 inline uc32 Get(int index);
4827 int length() { return length_; }
4828 private:
4829 String** str_;
4830 bool is_ascii_;
4831 int length_;
4832 const void* start_;
4833};
4834
4835
4836// Note that StringInputBuffers are not valid across a GC! To fix this
4837// it would have to store a String Handle instead of a String* and
4838// AsciiStringReadBlock would have to be modified to use memcpy.
4839//
4840// StringInputBuffer is able to traverse any string regardless of how
4841// deeply nested a sequence of ConsStrings it is made of. However,
4842// performance will be better if deep strings are flattened before they
4843// are traversed. Since flattening requires memory allocation this is
4844// not always desirable, however (esp. in debugging situations).
4845class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
4846 public:
4847 virtual void Seek(unsigned pos);
4848 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
4849 inline StringInputBuffer(String* backing):
4850 unibrow::InputBuffer<String, String*, 1024>(backing) {}
4851};
4852
4853
4854class SafeStringInputBuffer
4855 : public unibrow::InputBuffer<String, String**, 256> {
4856 public:
4857 virtual void Seek(unsigned pos);
4858 inline SafeStringInputBuffer()
4859 : unibrow::InputBuffer<String, String**, 256>() {}
4860 inline SafeStringInputBuffer(String** backing)
4861 : unibrow::InputBuffer<String, String**, 256>(backing) {}
4862};
4863
4864
4865template <typename T>
4866class VectorIterator {
4867 public:
4868 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
4869 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
4870 T GetNext() { return data_[index_++]; }
4871 bool has_more() { return index_ < data_.length(); }
4872 private:
4873 Vector<const T> data_;
4874 int index_;
4875};
4876
4877
4878// The Oddball describes objects null, undefined, true, and false.
4879class Oddball: public HeapObject {
4880 public:
4881 // [to_string]: Cached to_string computed at startup.
4882 DECL_ACCESSORS(to_string, String)
4883
4884 // [to_number]: Cached to_number computed at startup.
4885 DECL_ACCESSORS(to_number, Object)
4886
4887 // Casting.
4888 static inline Oddball* cast(Object* obj);
4889
4890 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00004891#ifdef DEBUG
4892 void OddballVerify();
4893#endif
4894
4895 // Initialize the fields.
4896 Object* Initialize(const char* to_string, Object* to_number);
4897
4898 // Layout description.
4899 static const int kToStringOffset = HeapObject::kHeaderSize;
4900 static const int kToNumberOffset = kToStringOffset + kPointerSize;
4901 static const int kSize = kToNumberOffset + kPointerSize;
4902
Iain Merrick75681382010-08-19 15:07:18 +01004903 typedef FixedBodyDescriptor<kToStringOffset,
4904 kToNumberOffset + kPointerSize,
4905 kSize> BodyDescriptor;
4906
Steve Blocka7e24c12009-10-30 11:49:00 +00004907 private:
4908 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
4909};
4910
4911
4912class JSGlobalPropertyCell: public HeapObject {
4913 public:
4914 // [value]: value of the global property.
4915 DECL_ACCESSORS(value, Object)
4916
4917 // Casting.
4918 static inline JSGlobalPropertyCell* cast(Object* obj);
4919
Steve Blocka7e24c12009-10-30 11:49:00 +00004920#ifdef DEBUG
4921 void JSGlobalPropertyCellVerify();
4922 void JSGlobalPropertyCellPrint();
4923#endif
4924
4925 // Layout description.
4926 static const int kValueOffset = HeapObject::kHeaderSize;
4927 static const int kSize = kValueOffset + kPointerSize;
4928
Iain Merrick75681382010-08-19 15:07:18 +01004929 typedef FixedBodyDescriptor<kValueOffset,
4930 kValueOffset + kPointerSize,
4931 kSize> BodyDescriptor;
4932
Steve Blocka7e24c12009-10-30 11:49:00 +00004933 private:
4934 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
4935};
4936
4937
4938
4939// Proxy describes objects pointing from JavaScript to C structures.
4940// Since they cannot contain references to JS HeapObjects they can be
4941// placed in old_data_space.
4942class Proxy: public HeapObject {
4943 public:
4944 // [proxy]: field containing the address.
4945 inline Address proxy();
4946 inline void set_proxy(Address value);
4947
4948 // Casting.
4949 static inline Proxy* cast(Object* obj);
4950
4951 // Dispatched behavior.
4952 inline void ProxyIterateBody(ObjectVisitor* v);
Iain Merrick75681382010-08-19 15:07:18 +01004953
4954 template<typename StaticVisitor>
4955 inline void ProxyIterateBody();
4956
Steve Blocka7e24c12009-10-30 11:49:00 +00004957#ifdef DEBUG
4958 void ProxyPrint();
4959 void ProxyVerify();
4960#endif
4961
4962 // Layout description.
4963
4964 static const int kProxyOffset = HeapObject::kHeaderSize;
4965 static const int kSize = kProxyOffset + kPointerSize;
4966
4967 STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
4968
4969 private:
4970 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
4971};
4972
4973
4974// The JSArray describes JavaScript Arrays
4975// Such an array can be in one of two modes:
4976// - fast, backing storage is a FixedArray and length <= elements.length();
4977// Please note: push and pop can be used to grow and shrink the array.
4978// - slow, backing storage is a HashTable with numbers as keys.
4979class JSArray: public JSObject {
4980 public:
4981 // [length]: The length property.
4982 DECL_ACCESSORS(length, Object)
4983
Leon Clarke4515c472010-02-03 11:58:03 +00004984 // Overload the length setter to skip write barrier when the length
4985 // is set to a smi. This matches the set function on FixedArray.
4986 inline void set_length(Smi* length);
4987
Steve Blocka7e24c12009-10-30 11:49:00 +00004988 Object* JSArrayUpdateLengthFromIndex(uint32_t index, Object* value);
4989
4990 // Initialize the array with the given capacity. The function may
4991 // fail due to out-of-memory situations, but only if the requested
4992 // capacity is non-zero.
4993 Object* Initialize(int capacity);
4994
4995 // Set the content of the array to the content of storage.
4996 inline void SetContent(FixedArray* storage);
4997
4998 // Casting.
4999 static inline JSArray* cast(Object* obj);
5000
5001 // Uses handles. Ensures that the fixed array backing the JSArray has at
5002 // least the stated size.
5003 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
5004
5005 // Dispatched behavior.
5006#ifdef DEBUG
5007 void JSArrayPrint();
5008 void JSArrayVerify();
5009#endif
5010
5011 // Number of element slots to pre-allocate for an empty array.
5012 static const int kPreallocatedArrayElements = 4;
5013
5014 // Layout description.
5015 static const int kLengthOffset = JSObject::kHeaderSize;
5016 static const int kSize = kLengthOffset + kPointerSize;
5017
5018 private:
5019 // Expand the fixed array backing of a fast-case JSArray to at least
5020 // the requested size.
5021 void Expand(int minimum_size_of_backing_fixed_array);
5022
5023 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
5024};
5025
5026
Steve Block6ded16b2010-05-10 14:33:55 +01005027// JSRegExpResult is just a JSArray with a specific initial map.
5028// This initial map adds in-object properties for "index" and "input"
5029// properties, as assigned by RegExp.prototype.exec, which allows
5030// faster creation of RegExp exec results.
5031// This class just holds constants used when creating the result.
5032// After creation the result must be treated as a JSArray in all regards.
5033class JSRegExpResult: public JSArray {
5034 public:
5035 // Offsets of object fields.
5036 static const int kIndexOffset = JSArray::kSize;
5037 static const int kInputOffset = kIndexOffset + kPointerSize;
5038 static const int kSize = kInputOffset + kPointerSize;
5039 // Indices of in-object properties.
5040 static const int kIndexIndex = 0;
5041 static const int kInputIndex = 1;
5042 private:
5043 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
5044};
5045
5046
Steve Blocka7e24c12009-10-30 11:49:00 +00005047// An accessor must have a getter, but can have no setter.
5048//
5049// When setting a property, V8 searches accessors in prototypes.
5050// If an accessor was found and it does not have a setter,
5051// the request is ignored.
5052//
5053// If the accessor in the prototype has the READ_ONLY property attribute, then
5054// a new value is added to the local object when the property is set.
5055// This shadows the accessor in the prototype.
5056class AccessorInfo: public Struct {
5057 public:
5058 DECL_ACCESSORS(getter, Object)
5059 DECL_ACCESSORS(setter, Object)
5060 DECL_ACCESSORS(data, Object)
5061 DECL_ACCESSORS(name, Object)
5062 DECL_ACCESSORS(flag, Smi)
Steve Blockd0582a62009-12-15 09:54:21 +00005063 DECL_ACCESSORS(load_stub_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00005064
5065 inline bool all_can_read();
5066 inline void set_all_can_read(bool value);
5067
5068 inline bool all_can_write();
5069 inline void set_all_can_write(bool value);
5070
5071 inline bool prohibits_overwriting();
5072 inline void set_prohibits_overwriting(bool value);
5073
5074 inline PropertyAttributes property_attributes();
5075 inline void set_property_attributes(PropertyAttributes attributes);
5076
5077 static inline AccessorInfo* cast(Object* obj);
5078
5079#ifdef DEBUG
5080 void AccessorInfoPrint();
5081 void AccessorInfoVerify();
5082#endif
5083
5084 static const int kGetterOffset = HeapObject::kHeaderSize;
5085 static const int kSetterOffset = kGetterOffset + kPointerSize;
5086 static const int kDataOffset = kSetterOffset + kPointerSize;
5087 static const int kNameOffset = kDataOffset + kPointerSize;
5088 static const int kFlagOffset = kNameOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00005089 static const int kLoadStubCacheOffset = kFlagOffset + kPointerSize;
5090 static const int kSize = kLoadStubCacheOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00005091
5092 private:
5093 // Bit positions in flag.
5094 static const int kAllCanReadBit = 0;
5095 static const int kAllCanWriteBit = 1;
5096 static const int kProhibitsOverwritingBit = 2;
5097 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
5098
5099 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
5100};
5101
5102
5103class AccessCheckInfo: public Struct {
5104 public:
5105 DECL_ACCESSORS(named_callback, Object)
5106 DECL_ACCESSORS(indexed_callback, Object)
5107 DECL_ACCESSORS(data, Object)
5108
5109 static inline AccessCheckInfo* cast(Object* obj);
5110
5111#ifdef DEBUG
5112 void AccessCheckInfoPrint();
5113 void AccessCheckInfoVerify();
5114#endif
5115
5116 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
5117 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
5118 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
5119 static const int kSize = kDataOffset + kPointerSize;
5120
5121 private:
5122 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
5123};
5124
5125
5126class InterceptorInfo: public Struct {
5127 public:
5128 DECL_ACCESSORS(getter, Object)
5129 DECL_ACCESSORS(setter, Object)
5130 DECL_ACCESSORS(query, Object)
5131 DECL_ACCESSORS(deleter, Object)
5132 DECL_ACCESSORS(enumerator, Object)
5133 DECL_ACCESSORS(data, Object)
5134
5135 static inline InterceptorInfo* cast(Object* obj);
5136
5137#ifdef DEBUG
5138 void InterceptorInfoPrint();
5139 void InterceptorInfoVerify();
5140#endif
5141
5142 static const int kGetterOffset = HeapObject::kHeaderSize;
5143 static const int kSetterOffset = kGetterOffset + kPointerSize;
5144 static const int kQueryOffset = kSetterOffset + kPointerSize;
5145 static const int kDeleterOffset = kQueryOffset + kPointerSize;
5146 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
5147 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
5148 static const int kSize = kDataOffset + kPointerSize;
5149
5150 private:
5151 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
5152};
5153
5154
5155class CallHandlerInfo: public Struct {
5156 public:
5157 DECL_ACCESSORS(callback, Object)
5158 DECL_ACCESSORS(data, Object)
5159
5160 static inline CallHandlerInfo* cast(Object* obj);
5161
5162#ifdef DEBUG
5163 void CallHandlerInfoPrint();
5164 void CallHandlerInfoVerify();
5165#endif
5166
5167 static const int kCallbackOffset = HeapObject::kHeaderSize;
5168 static const int kDataOffset = kCallbackOffset + kPointerSize;
5169 static const int kSize = kDataOffset + kPointerSize;
5170
5171 private:
5172 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
5173};
5174
5175
5176class TemplateInfo: public Struct {
5177 public:
5178 DECL_ACCESSORS(tag, Object)
5179 DECL_ACCESSORS(property_list, Object)
5180
5181#ifdef DEBUG
5182 void TemplateInfoVerify();
5183#endif
5184
5185 static const int kTagOffset = HeapObject::kHeaderSize;
5186 static const int kPropertyListOffset = kTagOffset + kPointerSize;
5187 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
5188 protected:
5189 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
5190 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
5191};
5192
5193
5194class FunctionTemplateInfo: public TemplateInfo {
5195 public:
5196 DECL_ACCESSORS(serial_number, Object)
5197 DECL_ACCESSORS(call_code, Object)
5198 DECL_ACCESSORS(property_accessors, Object)
5199 DECL_ACCESSORS(prototype_template, Object)
5200 DECL_ACCESSORS(parent_template, Object)
5201 DECL_ACCESSORS(named_property_handler, Object)
5202 DECL_ACCESSORS(indexed_property_handler, Object)
5203 DECL_ACCESSORS(instance_template, Object)
5204 DECL_ACCESSORS(class_name, Object)
5205 DECL_ACCESSORS(signature, Object)
5206 DECL_ACCESSORS(instance_call_handler, Object)
5207 DECL_ACCESSORS(access_check_info, Object)
5208 DECL_ACCESSORS(flag, Smi)
5209
5210 // Following properties use flag bits.
5211 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
5212 DECL_BOOLEAN_ACCESSORS(undetectable)
5213 // If the bit is set, object instances created by this function
5214 // requires access check.
5215 DECL_BOOLEAN_ACCESSORS(needs_access_check)
5216
5217 static inline FunctionTemplateInfo* cast(Object* obj);
5218
5219#ifdef DEBUG
5220 void FunctionTemplateInfoPrint();
5221 void FunctionTemplateInfoVerify();
5222#endif
5223
5224 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
5225 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
5226 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
5227 static const int kPrototypeTemplateOffset =
5228 kPropertyAccessorsOffset + kPointerSize;
5229 static const int kParentTemplateOffset =
5230 kPrototypeTemplateOffset + kPointerSize;
5231 static const int kNamedPropertyHandlerOffset =
5232 kParentTemplateOffset + kPointerSize;
5233 static const int kIndexedPropertyHandlerOffset =
5234 kNamedPropertyHandlerOffset + kPointerSize;
5235 static const int kInstanceTemplateOffset =
5236 kIndexedPropertyHandlerOffset + kPointerSize;
5237 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
5238 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
5239 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
5240 static const int kAccessCheckInfoOffset =
5241 kInstanceCallHandlerOffset + kPointerSize;
5242 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
5243 static const int kSize = kFlagOffset + kPointerSize;
5244
5245 private:
5246 // Bit position in the flag, from least significant bit position.
5247 static const int kHiddenPrototypeBit = 0;
5248 static const int kUndetectableBit = 1;
5249 static const int kNeedsAccessCheckBit = 2;
5250
5251 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
5252};
5253
5254
5255class ObjectTemplateInfo: public TemplateInfo {
5256 public:
5257 DECL_ACCESSORS(constructor, Object)
5258 DECL_ACCESSORS(internal_field_count, Object)
5259
5260 static inline ObjectTemplateInfo* cast(Object* obj);
5261
5262#ifdef DEBUG
5263 void ObjectTemplateInfoPrint();
5264 void ObjectTemplateInfoVerify();
5265#endif
5266
5267 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
5268 static const int kInternalFieldCountOffset =
5269 kConstructorOffset + kPointerSize;
5270 static const int kSize = kInternalFieldCountOffset + kPointerSize;
5271};
5272
5273
5274class SignatureInfo: public Struct {
5275 public:
5276 DECL_ACCESSORS(receiver, Object)
5277 DECL_ACCESSORS(args, Object)
5278
5279 static inline SignatureInfo* cast(Object* obj);
5280
5281#ifdef DEBUG
5282 void SignatureInfoPrint();
5283 void SignatureInfoVerify();
5284#endif
5285
5286 static const int kReceiverOffset = Struct::kHeaderSize;
5287 static const int kArgsOffset = kReceiverOffset + kPointerSize;
5288 static const int kSize = kArgsOffset + kPointerSize;
5289
5290 private:
5291 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
5292};
5293
5294
5295class TypeSwitchInfo: public Struct {
5296 public:
5297 DECL_ACCESSORS(types, Object)
5298
5299 static inline TypeSwitchInfo* cast(Object* obj);
5300
5301#ifdef DEBUG
5302 void TypeSwitchInfoPrint();
5303 void TypeSwitchInfoVerify();
5304#endif
5305
5306 static const int kTypesOffset = Struct::kHeaderSize;
5307 static const int kSize = kTypesOffset + kPointerSize;
5308};
5309
5310
5311#ifdef ENABLE_DEBUGGER_SUPPORT
5312// The DebugInfo class holds additional information for a function being
5313// debugged.
5314class DebugInfo: public Struct {
5315 public:
5316 // The shared function info for the source being debugged.
5317 DECL_ACCESSORS(shared, SharedFunctionInfo)
5318 // Code object for the original code.
5319 DECL_ACCESSORS(original_code, Code)
5320 // Code object for the patched code. This code object is the code object
5321 // currently active for the function.
5322 DECL_ACCESSORS(code, Code)
5323 // Fixed array holding status information for each active break point.
5324 DECL_ACCESSORS(break_points, FixedArray)
5325
5326 // Check if there is a break point at a code position.
5327 bool HasBreakPoint(int code_position);
5328 // Get the break point info object for a code position.
5329 Object* GetBreakPointInfo(int code_position);
5330 // Clear a break point.
5331 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
5332 int code_position,
5333 Handle<Object> break_point_object);
5334 // Set a break point.
5335 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
5336 int source_position, int statement_position,
5337 Handle<Object> break_point_object);
5338 // Get the break point objects for a code position.
5339 Object* GetBreakPointObjects(int code_position);
5340 // Find the break point info holding this break point object.
5341 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
5342 Handle<Object> break_point_object);
5343 // Get the number of break points for this function.
5344 int GetBreakPointCount();
5345
5346 static inline DebugInfo* cast(Object* obj);
5347
5348#ifdef DEBUG
5349 void DebugInfoPrint();
5350 void DebugInfoVerify();
5351#endif
5352
5353 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
5354 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
5355 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
5356 static const int kActiveBreakPointsCountIndex =
5357 kPatchedCodeIndex + kPointerSize;
5358 static const int kBreakPointsStateIndex =
5359 kActiveBreakPointsCountIndex + kPointerSize;
5360 static const int kSize = kBreakPointsStateIndex + kPointerSize;
5361
5362 private:
5363 static const int kNoBreakPointInfo = -1;
5364
5365 // Lookup the index in the break_points array for a code position.
5366 int GetBreakPointInfoIndex(int code_position);
5367
5368 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
5369};
5370
5371
5372// The BreakPointInfo class holds information for break points set in a
5373// function. The DebugInfo object holds a BreakPointInfo object for each code
5374// position with one or more break points.
5375class BreakPointInfo: public Struct {
5376 public:
5377 // The position in the code for the break point.
5378 DECL_ACCESSORS(code_position, Smi)
5379 // The position in the source for the break position.
5380 DECL_ACCESSORS(source_position, Smi)
5381 // The position in the source for the last statement before this break
5382 // position.
5383 DECL_ACCESSORS(statement_position, Smi)
5384 // List of related JavaScript break points.
5385 DECL_ACCESSORS(break_point_objects, Object)
5386
5387 // Removes a break point.
5388 static void ClearBreakPoint(Handle<BreakPointInfo> info,
5389 Handle<Object> break_point_object);
5390 // Set a break point.
5391 static void SetBreakPoint(Handle<BreakPointInfo> info,
5392 Handle<Object> break_point_object);
5393 // Check if break point info has this break point object.
5394 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
5395 Handle<Object> break_point_object);
5396 // Get the number of break points for this code position.
5397 int GetBreakPointCount();
5398
5399 static inline BreakPointInfo* cast(Object* obj);
5400
5401#ifdef DEBUG
5402 void BreakPointInfoPrint();
5403 void BreakPointInfoVerify();
5404#endif
5405
5406 static const int kCodePositionIndex = Struct::kHeaderSize;
5407 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
5408 static const int kStatementPositionIndex =
5409 kSourcePositionIndex + kPointerSize;
5410 static const int kBreakPointObjectsIndex =
5411 kStatementPositionIndex + kPointerSize;
5412 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
5413
5414 private:
5415 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
5416};
5417#endif // ENABLE_DEBUGGER_SUPPORT
5418
5419
5420#undef DECL_BOOLEAN_ACCESSORS
5421#undef DECL_ACCESSORS
5422
5423
5424// Abstract base class for visiting, and optionally modifying, the
5425// pointers contained in Objects. Used in GC and serialization/deserialization.
5426class ObjectVisitor BASE_EMBEDDED {
5427 public:
5428 virtual ~ObjectVisitor() {}
5429
5430 // Visits a contiguous arrays of pointers in the half-open range
5431 // [start, end). Any or all of the values may be modified on return.
5432 virtual void VisitPointers(Object** start, Object** end) = 0;
5433
5434 // To allow lazy clearing of inline caches the visitor has
5435 // a rich interface for iterating over Code objects..
5436
5437 // Visits a code target in the instruction stream.
5438 virtual void VisitCodeTarget(RelocInfo* rinfo);
5439
5440 // Visits a runtime entry in the instruction stream.
5441 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
5442
Steve Blockd0582a62009-12-15 09:54:21 +00005443 // Visits the resource of an ASCII or two-byte string.
5444 virtual void VisitExternalAsciiString(
5445 v8::String::ExternalAsciiStringResource** resource) {}
5446 virtual void VisitExternalTwoByteString(
5447 v8::String::ExternalStringResource** resource) {}
5448
Steve Blocka7e24c12009-10-30 11:49:00 +00005449 // Visits a debug call target in the instruction stream.
5450 virtual void VisitDebugTarget(RelocInfo* rinfo);
5451
5452 // Handy shorthand for visiting a single pointer.
5453 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
5454
5455 // Visits a contiguous arrays of external references (references to the C++
5456 // heap) in the half-open range [start, end). Any or all of the values
5457 // may be modified on return.
5458 virtual void VisitExternalReferences(Address* start, Address* end) {}
5459
5460 inline void VisitExternalReference(Address* p) {
5461 VisitExternalReferences(p, p + 1);
5462 }
5463
5464#ifdef DEBUG
5465 // Intended for serialization/deserialization checking: insert, or
5466 // check for the presence of, a tag at this position in the stream.
5467 virtual void Synchronize(const char* tag) {}
Steve Blockd0582a62009-12-15 09:54:21 +00005468#else
5469 inline void Synchronize(const char* tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00005470#endif
5471};
5472
5473
Iain Merrick75681382010-08-19 15:07:18 +01005474class StructBodyDescriptor : public
5475 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
5476 public:
5477 static inline int SizeOf(Map* map, HeapObject* object) {
5478 return map->instance_size();
5479 }
5480};
5481
5482
Steve Blocka7e24c12009-10-30 11:49:00 +00005483// BooleanBit is a helper class for setting and getting a bit in an
5484// integer or Smi.
5485class BooleanBit : public AllStatic {
5486 public:
5487 static inline bool get(Smi* smi, int bit_position) {
5488 return get(smi->value(), bit_position);
5489 }
5490
5491 static inline bool get(int value, int bit_position) {
5492 return (value & (1 << bit_position)) != 0;
5493 }
5494
5495 static inline Smi* set(Smi* smi, int bit_position, bool v) {
5496 return Smi::FromInt(set(smi->value(), bit_position, v));
5497 }
5498
5499 static inline int set(int value, int bit_position, bool v) {
5500 if (v) {
5501 value |= (1 << bit_position);
5502 } else {
5503 value &= ~(1 << bit_position);
5504 }
5505 return value;
5506 }
5507};
5508
5509} } // namespace v8::internal
5510
5511#endif // V8_OBJECTS_H_