blob: 2619c891bd5c5cf7f64769b5a65a9ad90afb2d88 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_OBJECTS_H_
29#define V8_OBJECTS_H_
30
31#include "builtins.h"
32#include "code-stubs.h"
33#include "smart-pointer.h"
34#include "unicode-inl.h"
35
36//
37// All object types in the V8 JavaScript are described in this file.
38//
39// Inheritance hierarchy:
40// - Object
41// - Smi (immediate small integer)
42// - Failure (immediate for marking failed operation)
43// - HeapObject (superclass for everything allocated in the heap)
44// - JSObject
45// - JSArray
ager@chromium.org236ad962008-09-25 09:45:57 +000046// - JSRegExp
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047// - JSFunction
48// - GlobalObject
49// - JSGlobalObject
50// - JSBuiltinsObject
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000051// - JSGlobalProxy
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052// - JSValue
53// - Script
54// - Array
55// - ByteArray
56// - FixedArray
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000057// - DescriptorArray
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058// - HashTable
59// - Dictionary
60// - SymbolTable
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +000061// - CompilationCacheTable
62// - MapCache
63// - LookupCache
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064// - Context
65// - GlobalContext
66// - String
67// - SeqString
ager@chromium.org7c537e22008-10-16 08:43:32 +000068// - SeqAsciiString
69// - SeqTwoByteString
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070// - ConsString
71// - SlicedString
72// - ExternalString
73// - ExternalAsciiString
74// - ExternalTwoByteString
75// - HeapNumber
76// - Code
77// - Map
78// - Oddball
79// - Proxy
80// - SharedFunctionInfo
81// - Struct
82// - AccessorInfo
83// - AccessCheckInfo
84// - InterceptorInfo
85// - CallHandlerInfo
86// - FunctionTemplateInfo
87// - ObjectTemplateInfo
88// - SignatureInfo
89// - TypeSwitchInfo
90// - DebugInfo
91// - BreakPointInfo
92//
93// Formats of Object*:
94// Smi: [31 bit signed int] 0
95// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
96// Failure: [30 bit signed int] 11
97
98
99// Ecma-262 3rd 8.6.1
100enum PropertyAttributes {
101 NONE = v8::None,
102 READ_ONLY = v8::ReadOnly,
103 DONT_ENUM = v8::DontEnum,
104 DONT_DELETE = v8::DontDelete,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 ABSENT = 16 // Used in runtime to indicate a property is absent.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000106 // ABSENT can never be stored in or returned from a descriptor's attributes
107 // bitfield. It is only used as a return value meaning the attributes of
108 // a non-existent property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000109};
110
111namespace v8 { namespace internal {
112
113
114// PropertyDetails captures type and attributes for a property.
115// They are used both in property dictionaries and instance descriptors.
116class PropertyDetails BASE_EMBEDDED {
117 public:
118
119 PropertyDetails(PropertyAttributes attributes,
120 PropertyType type,
121 int index = 0) {
122 ASSERT(TypeField::is_valid(type));
123 ASSERT(AttributesField::is_valid(attributes));
124 ASSERT(IndexField::is_valid(index));
125
126 value_ = TypeField::encode(type)
127 | AttributesField::encode(attributes)
128 | IndexField::encode(index);
129
130 ASSERT(type == this->type());
131 ASSERT(attributes == this->attributes());
132 ASSERT(index == this->index());
133 }
134
135 // Conversion for storing details as Object*.
136 inline PropertyDetails(Smi* smi);
137 inline Smi* AsSmi();
138
139 PropertyType type() { return TypeField::decode(value_); }
140
141 bool IsTransition() {
142 PropertyType t = type();
143 ASSERT(t != INTERCEPTOR);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000144 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 }
146
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000147 bool IsProperty() {
148 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
149 }
150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151 PropertyAttributes attributes() { return AttributesField::decode(value_); }
152
153 int index() { return IndexField::decode(value_); }
154
155 static bool IsValidIndex(int index) { return IndexField::is_valid(index); }
156
157 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
158 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
159 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
160
161 // Bit fields in value_ (type, shift, size). Must be public so the
162 // constants can be embedded in generated code.
163 class TypeField: public BitField<PropertyType, 0, 3> {};
164 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
165 class IndexField: public BitField<uint32_t, 6, 32-6> {};
166
167 static const int kInitialIndex = 1;
168
169 private:
170 uint32_t value_;
171};
172
ager@chromium.org32912102009-01-16 10:38:43 +0000173
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000174// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
175enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
176
ager@chromium.org32912102009-01-16 10:38:43 +0000177
178// PropertyNormalizationMode is used to specify whether to keep
179// inobject properties when normalizing properties of a JSObject.
180enum PropertyNormalizationMode {
181 CLEAR_INOBJECT_PROPERTIES,
182 KEEP_INOBJECT_PROPERTIES
183};
184
185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186// All Maps have a field instance_type containing a InstanceType.
187// It describes the type of the instances.
188//
189// As an example, a JavaScript object is a heap object and its map
190// instance_type is JS_OBJECT_TYPE.
191//
192// The names of the string instance types are intended to systematically
193// mirror their encoding in the instance_type field of the map. The length
194// (SHORT, MEDIUM, or LONG) is always mentioned. The default encoding is
195// considered TWO_BYTE. It is not mentioned in the name. ASCII encoding is
196// mentioned explicitly in the name. Likewise, the default representation is
197// considered sequential. It is not mentioned in the name. The other
198// representations (eg, CONS, SLICED, EXTERNAL) are explicitly mentioned.
199// Finally, the string is either a SYMBOL_TYPE (if it is a symbol) or a
200// STRING_TYPE (if it is not a symbol).
201//
202// NOTE: The following things are some that depend on the string types having
203// instance_types that are less than those of all other types:
204// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
205// Object::IsString.
206//
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000207// NOTE: Everything following JS_VALUE_TYPE is considered a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208// JSObject for GC purposes. The first four entries here have typeof
209// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
210#define INSTANCE_TYPE_LIST(V) \
211 V(SHORT_SYMBOL_TYPE) \
212 V(MEDIUM_SYMBOL_TYPE) \
213 V(LONG_SYMBOL_TYPE) \
214 V(SHORT_ASCII_SYMBOL_TYPE) \
215 V(MEDIUM_ASCII_SYMBOL_TYPE) \
216 V(LONG_ASCII_SYMBOL_TYPE) \
217 V(SHORT_CONS_SYMBOL_TYPE) \
218 V(MEDIUM_CONS_SYMBOL_TYPE) \
219 V(LONG_CONS_SYMBOL_TYPE) \
220 V(SHORT_CONS_ASCII_SYMBOL_TYPE) \
221 V(MEDIUM_CONS_ASCII_SYMBOL_TYPE) \
222 V(LONG_CONS_ASCII_SYMBOL_TYPE) \
223 V(SHORT_SLICED_SYMBOL_TYPE) \
224 V(MEDIUM_SLICED_SYMBOL_TYPE) \
225 V(LONG_SLICED_SYMBOL_TYPE) \
226 V(SHORT_SLICED_ASCII_SYMBOL_TYPE) \
227 V(MEDIUM_SLICED_ASCII_SYMBOL_TYPE) \
228 V(LONG_SLICED_ASCII_SYMBOL_TYPE) \
229 V(SHORT_EXTERNAL_SYMBOL_TYPE) \
230 V(MEDIUM_EXTERNAL_SYMBOL_TYPE) \
231 V(LONG_EXTERNAL_SYMBOL_TYPE) \
232 V(SHORT_EXTERNAL_ASCII_SYMBOL_TYPE) \
233 V(MEDIUM_EXTERNAL_ASCII_SYMBOL_TYPE) \
234 V(LONG_EXTERNAL_ASCII_SYMBOL_TYPE) \
235 V(SHORT_STRING_TYPE) \
236 V(MEDIUM_STRING_TYPE) \
237 V(LONG_STRING_TYPE) \
238 V(SHORT_ASCII_STRING_TYPE) \
239 V(MEDIUM_ASCII_STRING_TYPE) \
240 V(LONG_ASCII_STRING_TYPE) \
241 V(SHORT_CONS_STRING_TYPE) \
242 V(MEDIUM_CONS_STRING_TYPE) \
243 V(LONG_CONS_STRING_TYPE) \
244 V(SHORT_CONS_ASCII_STRING_TYPE) \
245 V(MEDIUM_CONS_ASCII_STRING_TYPE) \
246 V(LONG_CONS_ASCII_STRING_TYPE) \
247 V(SHORT_SLICED_STRING_TYPE) \
248 V(MEDIUM_SLICED_STRING_TYPE) \
249 V(LONG_SLICED_STRING_TYPE) \
250 V(SHORT_SLICED_ASCII_STRING_TYPE) \
251 V(MEDIUM_SLICED_ASCII_STRING_TYPE) \
252 V(LONG_SLICED_ASCII_STRING_TYPE) \
253 V(SHORT_EXTERNAL_STRING_TYPE) \
254 V(MEDIUM_EXTERNAL_STRING_TYPE) \
255 V(LONG_EXTERNAL_STRING_TYPE) \
256 V(SHORT_EXTERNAL_ASCII_STRING_TYPE) \
257 V(MEDIUM_EXTERNAL_ASCII_STRING_TYPE) \
258 V(LONG_EXTERNAL_ASCII_STRING_TYPE) \
259 V(LONG_PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
260 \
261 V(MAP_TYPE) \
262 V(HEAP_NUMBER_TYPE) \
263 V(FIXED_ARRAY_TYPE) \
264 V(CODE_TYPE) \
265 V(ODDBALL_TYPE) \
266 V(PROXY_TYPE) \
267 V(BYTE_ARRAY_TYPE) \
268 V(FILLER_TYPE) \
269 \
270 V(ACCESSOR_INFO_TYPE) \
271 V(ACCESS_CHECK_INFO_TYPE) \
272 V(INTERCEPTOR_INFO_TYPE) \
273 V(SHARED_FUNCTION_INFO_TYPE) \
274 V(CALL_HANDLER_INFO_TYPE) \
275 V(FUNCTION_TEMPLATE_INFO_TYPE) \
276 V(OBJECT_TEMPLATE_INFO_TYPE) \
277 V(SIGNATURE_INFO_TYPE) \
278 V(TYPE_SWITCH_INFO_TYPE) \
279 V(DEBUG_INFO_TYPE) \
280 V(BREAK_POINT_INFO_TYPE) \
281 V(SCRIPT_TYPE) \
282 \
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000283 V(JS_VALUE_TYPE) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 V(JS_OBJECT_TYPE) \
ager@chromium.org32912102009-01-16 10:38:43 +0000285 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 V(JS_GLOBAL_OBJECT_TYPE) \
287 V(JS_BUILTINS_OBJECT_TYPE) \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000288 V(JS_GLOBAL_PROXY_TYPE) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 V(JS_ARRAY_TYPE) \
ager@chromium.org236ad962008-09-25 09:45:57 +0000290 V(JS_REGEXP_TYPE) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 \
292 V(JS_FUNCTION_TYPE) \
293
294
295// Since string types are not consecutive, this macro is used to
296// iterate over them.
297#define STRING_TYPE_LIST(V) \
ager@chromium.org7c537e22008-10-16 08:43:32 +0000298 V(SHORT_SYMBOL_TYPE, SeqTwoByteString::kHeaderSize, short_symbol) \
299 V(MEDIUM_SYMBOL_TYPE, SeqTwoByteString::kHeaderSize, medium_symbol) \
300 V(LONG_SYMBOL_TYPE, SeqTwoByteString::kHeaderSize, long_symbol) \
301 V(SHORT_ASCII_SYMBOL_TYPE, SeqAsciiString::kHeaderSize, short_ascii_symbol) \
302 V(MEDIUM_ASCII_SYMBOL_TYPE, SeqAsciiString::kHeaderSize, medium_ascii_symbol)\
303 V(LONG_ASCII_SYMBOL_TYPE, SeqAsciiString::kHeaderSize, long_ascii_symbol) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 V(SHORT_CONS_SYMBOL_TYPE, ConsString::kSize, short_cons_symbol) \
305 V(MEDIUM_CONS_SYMBOL_TYPE, ConsString::kSize, medium_cons_symbol) \
306 V(LONG_CONS_SYMBOL_TYPE, ConsString::kSize, long_cons_symbol) \
307 V(SHORT_CONS_ASCII_SYMBOL_TYPE, ConsString::kSize, short_cons_ascii_symbol) \
308 V(MEDIUM_CONS_ASCII_SYMBOL_TYPE, ConsString::kSize, medium_cons_ascii_symbol)\
309 V(LONG_CONS_ASCII_SYMBOL_TYPE, ConsString::kSize, long_cons_ascii_symbol) \
310 V(SHORT_SLICED_SYMBOL_TYPE, SlicedString::kSize, short_sliced_symbol) \
311 V(MEDIUM_SLICED_SYMBOL_TYPE, SlicedString::kSize, medium_sliced_symbol) \
312 V(LONG_SLICED_SYMBOL_TYPE, SlicedString::kSize, long_sliced_symbol) \
313 V(SHORT_SLICED_ASCII_SYMBOL_TYPE, \
314 SlicedString::kSize, \
315 short_sliced_ascii_symbol) \
316 V(MEDIUM_SLICED_ASCII_SYMBOL_TYPE, \
317 SlicedString::kSize, \
318 medium_sliced_ascii_symbol) \
319 V(LONG_SLICED_ASCII_SYMBOL_TYPE, \
320 SlicedString::kSize, \
321 long_sliced_ascii_symbol) \
322 V(SHORT_EXTERNAL_SYMBOL_TYPE, \
323 ExternalTwoByteString::kSize, \
324 short_external_symbol) \
325 V(MEDIUM_EXTERNAL_SYMBOL_TYPE, \
326 ExternalTwoByteString::kSize, \
327 medium_external_symbol) \
328 V(LONG_EXTERNAL_SYMBOL_TYPE, \
329 ExternalTwoByteString::kSize, \
330 long_external_symbol) \
331 V(SHORT_EXTERNAL_ASCII_SYMBOL_TYPE, \
332 ExternalAsciiString::kSize, \
333 short_external_ascii_symbol) \
334 V(MEDIUM_EXTERNAL_ASCII_SYMBOL_TYPE, \
335 ExternalAsciiString::kSize, \
336 medium_external_ascii_symbol) \
337 V(LONG_EXTERNAL_ASCII_SYMBOL_TYPE, \
338 ExternalAsciiString::kSize, \
339 long_external_ascii_symbol) \
ager@chromium.org7c537e22008-10-16 08:43:32 +0000340 V(SHORT_STRING_TYPE, SeqTwoByteString::kHeaderSize, short_string) \
341 V(MEDIUM_STRING_TYPE, SeqTwoByteString::kHeaderSize, medium_string) \
342 V(LONG_STRING_TYPE, SeqTwoByteString::kHeaderSize, long_string) \
343 V(SHORT_ASCII_STRING_TYPE, SeqAsciiString::kHeaderSize, short_ascii_string) \
344 V(MEDIUM_ASCII_STRING_TYPE, SeqAsciiString::kHeaderSize, medium_ascii_string)\
345 V(LONG_ASCII_STRING_TYPE, SeqAsciiString::kHeaderSize, long_ascii_string) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000346 V(SHORT_CONS_STRING_TYPE, ConsString::kSize, short_cons_string) \
347 V(MEDIUM_CONS_STRING_TYPE, ConsString::kSize, medium_cons_string) \
348 V(LONG_CONS_STRING_TYPE, ConsString::kSize, long_cons_string) \
349 V(SHORT_CONS_ASCII_STRING_TYPE, ConsString::kSize, short_cons_ascii_string) \
350 V(MEDIUM_CONS_ASCII_STRING_TYPE, ConsString::kSize, medium_cons_ascii_string)\
351 V(LONG_CONS_ASCII_STRING_TYPE, ConsString::kSize, long_cons_ascii_string) \
352 V(SHORT_SLICED_STRING_TYPE, SlicedString::kSize, short_sliced_string) \
353 V(MEDIUM_SLICED_STRING_TYPE, SlicedString::kSize, medium_sliced_string) \
354 V(LONG_SLICED_STRING_TYPE, SlicedString::kSize, long_sliced_string) \
355 V(SHORT_SLICED_ASCII_STRING_TYPE, \
356 SlicedString::kSize, \
357 short_sliced_ascii_string) \
358 V(MEDIUM_SLICED_ASCII_STRING_TYPE, \
359 SlicedString::kSize, \
360 medium_sliced_ascii_string) \
361 V(LONG_SLICED_ASCII_STRING_TYPE, \
362 SlicedString::kSize, \
363 long_sliced_ascii_string) \
364 V(SHORT_EXTERNAL_STRING_TYPE, \
365 ExternalTwoByteString::kSize, \
366 short_external_string) \
367 V(MEDIUM_EXTERNAL_STRING_TYPE, \
368 ExternalTwoByteString::kSize, \
369 medium_external_string) \
370 V(LONG_EXTERNAL_STRING_TYPE, \
371 ExternalTwoByteString::kSize, \
372 long_external_string) \
373 V(SHORT_EXTERNAL_ASCII_STRING_TYPE, \
374 ExternalAsciiString::kSize, \
375 short_external_ascii_string) \
376 V(MEDIUM_EXTERNAL_ASCII_STRING_TYPE, \
377 ExternalAsciiString::kSize, \
378 medium_external_ascii_string) \
379 V(LONG_EXTERNAL_ASCII_STRING_TYPE, \
380 ExternalAsciiString::kSize, \
381 long_external_ascii_string)
382
383// A struct is a simple object a set of object-valued fields. Including an
384// object type in this causes the compiler to generate most of the boilerplate
385// code for the class including allocation and garbage collection routines,
386// casts and predicates. All you need to define is the class, methods and
387// object verification routines. Easy, no?
388//
389// Note that for subtle reasons related to the ordering or numerical values of
390// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
391// manually.
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000392#define STRUCT_LIST_ALL(V) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
394 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
395 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
396 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
397 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
398 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
399 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
400 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000401 V(SCRIPT, Script, script)
402
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000403#ifdef ENABLE_DEBUGGER_SUPPORT
404#define STRUCT_LIST_DEBUGGER(V) \
405 V(DEBUG_INFO, DebugInfo, debug_info) \
406 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
407#else
408#define STRUCT_LIST_DEBUGGER(V)
409#endif
410
411#define STRUCT_LIST(V) \
412 STRUCT_LIST_ALL(V) \
413 STRUCT_LIST_DEBUGGER(V)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414
415// We use the full 8 bits of the instance_type field to encode heap object
416// instance types. The high-order bit (bit 7) is set if the object is not a
417// string, and cleared if it is a string.
418const uint32_t kIsNotStringMask = 0x80;
419const uint32_t kStringTag = 0x0;
420const uint32_t kNotStringTag = 0x80;
421
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000422// If bit 7 is clear, bit 5 indicates that the string is a symbol (if set) or
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000423// not (if cleared).
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000424const uint32_t kIsSymbolMask = 0x20;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000425const uint32_t kNotSymbolTag = 0x0;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000426const uint32_t kSymbolTag = 0x20;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000427
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000428// If bit 7 is clear, bits 3 and 4 are the string's size (short, medium or
429// long). These values are very special in that they are also used to shift
430// the length field to get the length, removing the hash value. This avoids
431// using if or switch when getting the length of a string.
432const uint32_t kStringSizeMask = 0x18;
433const uint32_t kShortStringTag = 0x18;
434const uint32_t kMediumStringTag = 0x10;
435const uint32_t kLongStringTag = 0x00;
436
437// If bit 7 is clear then bit 2 indicates whether the string consists of
438// two-byte characters or one-byte characters.
439const uint32_t kStringEncodingMask = 0x4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440const uint32_t kTwoByteStringTag = 0x0;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000441const uint32_t kAsciiStringTag = 0x4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000443// If bit 7 is clear, the low-order 2 bits indicate the representation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000444// of the string.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000445const uint32_t kStringRepresentationMask = 0x03;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000446enum StringRepresentationTag {
447 kSeqStringTag = 0x0,
448 kConsStringTag = 0x1,
449 kSlicedStringTag = 0x2,
450 kExternalStringTag = 0x3
451};
452
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000453
454// A ConsString with an empty string as the right side is a candidate
455// for being shortcut by the garbage collector unless it is a
456// symbol. It's not common to have non-flat symbols, so we do not
457// shortcut them thereby avoiding turning symbols into strings. See
458// heap.cc and mark-compact.cc.
459const uint32_t kShortcutTypeMask =
460 kIsNotStringMask |
461 kIsSymbolMask |
462 kStringRepresentationMask;
463const uint32_t kShortcutTypeTag = kConsStringTag;
464
465
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000466enum InstanceType {
467 SHORT_SYMBOL_TYPE = kShortStringTag | kSymbolTag | kSeqStringTag,
468 MEDIUM_SYMBOL_TYPE = kMediumStringTag | kSymbolTag | kSeqStringTag,
469 LONG_SYMBOL_TYPE = kLongStringTag | kSymbolTag | kSeqStringTag,
470 SHORT_ASCII_SYMBOL_TYPE =
471 kShortStringTag | kAsciiStringTag | kSymbolTag | kSeqStringTag,
472 MEDIUM_ASCII_SYMBOL_TYPE =
473 kMediumStringTag | kAsciiStringTag | kSymbolTag | kSeqStringTag,
474 LONG_ASCII_SYMBOL_TYPE =
475 kLongStringTag | kAsciiStringTag | kSymbolTag | kSeqStringTag,
476 SHORT_CONS_SYMBOL_TYPE = kShortStringTag | kSymbolTag | kConsStringTag,
477 MEDIUM_CONS_SYMBOL_TYPE = kMediumStringTag | kSymbolTag | kConsStringTag,
478 LONG_CONS_SYMBOL_TYPE = kLongStringTag | kSymbolTag | kConsStringTag,
479 SHORT_CONS_ASCII_SYMBOL_TYPE =
480 kShortStringTag | kAsciiStringTag | kSymbolTag | kConsStringTag,
481 MEDIUM_CONS_ASCII_SYMBOL_TYPE =
482 kMediumStringTag | kAsciiStringTag | kSymbolTag | kConsStringTag,
483 LONG_CONS_ASCII_SYMBOL_TYPE =
484 kLongStringTag | kAsciiStringTag | kSymbolTag | kConsStringTag,
485 SHORT_SLICED_SYMBOL_TYPE = kShortStringTag | kSymbolTag | kSlicedStringTag,
486 MEDIUM_SLICED_SYMBOL_TYPE = kMediumStringTag | kSymbolTag | kSlicedStringTag,
487 LONG_SLICED_SYMBOL_TYPE = kLongStringTag | kSymbolTag | kSlicedStringTag,
488 SHORT_SLICED_ASCII_SYMBOL_TYPE =
489 kShortStringTag | kAsciiStringTag | kSymbolTag | kSlicedStringTag,
490 MEDIUM_SLICED_ASCII_SYMBOL_TYPE =
491 kMediumStringTag | kAsciiStringTag | kSymbolTag | kSlicedStringTag,
492 LONG_SLICED_ASCII_SYMBOL_TYPE =
493 kLongStringTag | kAsciiStringTag | kSymbolTag | kSlicedStringTag,
494 SHORT_EXTERNAL_SYMBOL_TYPE =
495 kShortStringTag | kSymbolTag | kExternalStringTag,
496 MEDIUM_EXTERNAL_SYMBOL_TYPE =
497 kMediumStringTag | kSymbolTag | kExternalStringTag,
498 LONG_EXTERNAL_SYMBOL_TYPE = kLongStringTag | kSymbolTag | kExternalStringTag,
499 SHORT_EXTERNAL_ASCII_SYMBOL_TYPE =
500 kShortStringTag | kAsciiStringTag | kSymbolTag | kExternalStringTag,
501 MEDIUM_EXTERNAL_ASCII_SYMBOL_TYPE =
502 kMediumStringTag | kAsciiStringTag | kSymbolTag | kExternalStringTag,
503 LONG_EXTERNAL_ASCII_SYMBOL_TYPE =
504 kLongStringTag | kAsciiStringTag | kSymbolTag | kExternalStringTag,
505 SHORT_STRING_TYPE = kShortStringTag | kSeqStringTag,
506 MEDIUM_STRING_TYPE = kMediumStringTag | kSeqStringTag,
507 LONG_STRING_TYPE = kLongStringTag | kSeqStringTag,
508 SHORT_ASCII_STRING_TYPE = kShortStringTag | kAsciiStringTag | kSeqStringTag,
509 MEDIUM_ASCII_STRING_TYPE = kMediumStringTag | kAsciiStringTag | kSeqStringTag,
510 LONG_ASCII_STRING_TYPE = kLongStringTag | kAsciiStringTag | kSeqStringTag,
511 SHORT_CONS_STRING_TYPE = kShortStringTag | kConsStringTag,
512 MEDIUM_CONS_STRING_TYPE = kMediumStringTag | kConsStringTag,
513 LONG_CONS_STRING_TYPE = kLongStringTag | kConsStringTag,
514 SHORT_CONS_ASCII_STRING_TYPE =
515 kShortStringTag | kAsciiStringTag | kConsStringTag,
516 MEDIUM_CONS_ASCII_STRING_TYPE =
517 kMediumStringTag | kAsciiStringTag | kConsStringTag,
518 LONG_CONS_ASCII_STRING_TYPE =
519 kLongStringTag | kAsciiStringTag | kConsStringTag,
520 SHORT_SLICED_STRING_TYPE = kShortStringTag | kSlicedStringTag,
521 MEDIUM_SLICED_STRING_TYPE = kMediumStringTag | kSlicedStringTag,
522 LONG_SLICED_STRING_TYPE = kLongStringTag | kSlicedStringTag,
523 SHORT_SLICED_ASCII_STRING_TYPE =
524 kShortStringTag | kAsciiStringTag | kSlicedStringTag,
525 MEDIUM_SLICED_ASCII_STRING_TYPE =
526 kMediumStringTag | kAsciiStringTag | kSlicedStringTag,
527 LONG_SLICED_ASCII_STRING_TYPE =
528 kLongStringTag | kAsciiStringTag | kSlicedStringTag,
529 SHORT_EXTERNAL_STRING_TYPE = kShortStringTag | kExternalStringTag,
530 MEDIUM_EXTERNAL_STRING_TYPE = kMediumStringTag | kExternalStringTag,
531 LONG_EXTERNAL_STRING_TYPE = kLongStringTag | kExternalStringTag,
532 SHORT_EXTERNAL_ASCII_STRING_TYPE =
533 kShortStringTag | kAsciiStringTag | kExternalStringTag,
534 MEDIUM_EXTERNAL_ASCII_STRING_TYPE =
535 kMediumStringTag | kAsciiStringTag | kExternalStringTag,
536 LONG_EXTERNAL_ASCII_STRING_TYPE =
537 kLongStringTag | kAsciiStringTag | kExternalStringTag,
538 LONG_PRIVATE_EXTERNAL_ASCII_STRING_TYPE = LONG_EXTERNAL_ASCII_STRING_TYPE,
539
540 MAP_TYPE = kNotStringTag,
541 HEAP_NUMBER_TYPE,
542 FIXED_ARRAY_TYPE,
543 CODE_TYPE,
544 ODDBALL_TYPE,
545 PROXY_TYPE,
546 BYTE_ARRAY_TYPE,
547 FILLER_TYPE,
548 SMI_TYPE,
549
550 ACCESSOR_INFO_TYPE,
551 ACCESS_CHECK_INFO_TYPE,
552 INTERCEPTOR_INFO_TYPE,
553 SHARED_FUNCTION_INFO_TYPE,
554 CALL_HANDLER_INFO_TYPE,
555 FUNCTION_TEMPLATE_INFO_TYPE,
556 OBJECT_TEMPLATE_INFO_TYPE,
557 SIGNATURE_INFO_TYPE,
558 TYPE_SWITCH_INFO_TYPE,
559 DEBUG_INFO_TYPE,
560 BREAK_POINT_INFO_TYPE,
561 SCRIPT_TYPE,
562
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000563 JS_VALUE_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 JS_OBJECT_TYPE,
ager@chromium.org32912102009-01-16 10:38:43 +0000565 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 JS_GLOBAL_OBJECT_TYPE,
567 JS_BUILTINS_OBJECT_TYPE,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000568 JS_GLOBAL_PROXY_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 JS_ARRAY_TYPE,
ager@chromium.org236ad962008-09-25 09:45:57 +0000570 JS_REGEXP_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571
572 JS_FUNCTION_TYPE,
573
574 // Pseudo-types
575 FIRST_NONSTRING_TYPE = MAP_TYPE,
576 FIRST_TYPE = 0x0,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000577 INVALID_TYPE = FIRST_TYPE - 1,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 LAST_TYPE = JS_FUNCTION_TYPE,
579 // Boundaries for testing the type is a JavaScript "object". Note that
580 // function objects are not counted as objects, even though they are
581 // implemented as such; only values whose typeof is "object" are included.
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000582 FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
ager@chromium.org236ad962008-09-25 09:45:57 +0000583 LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584};
585
586
587enum CompareResult {
588 LESS = -1,
589 EQUAL = 0,
590 GREATER = 1,
591
592 NOT_EQUAL = GREATER
593};
594
595
596#define DECL_BOOLEAN_ACCESSORS(name) \
597 inline bool name(); \
598 inline void set_##name(bool value); \
599
600
ager@chromium.org32912102009-01-16 10:38:43 +0000601#define DECL_ACCESSORS(name, type) \
602 inline type* name(); \
603 inline void set_##name(type* value, \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000604 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605
606
607class StringStream;
608class ObjectVisitor;
609
610struct ValueInfo : public Malloced {
611 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
612 InstanceType type;
613 Object* ptr;
614 const char* str;
615 double number;
616};
617
618
619// A template-ized version of the IsXXX functions.
620template <class C> static inline bool Is(Object* obj);
621
622
623// Object is the abstract superclass for all classes in the
624// object hierarchy.
625// Object does not use any virtual functions to avoid the
626// allocation of the C++ vtable.
627// Since Smi and Failure are subclasses of Object no
628// data members can be present in Object.
629class Object BASE_EMBEDDED {
630 public:
631 // Type testing.
632 inline bool IsSmi();
633 inline bool IsHeapObject();
634 inline bool IsHeapNumber();
635 inline bool IsString();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000636 inline bool IsSymbol();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 inline bool IsSeqString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 inline bool IsSlicedString();
639 inline bool IsExternalString();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000640 inline bool IsConsString();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 inline bool IsExternalTwoByteString();
ager@chromium.org870a0b62008-11-04 11:43:05 +0000642 inline bool IsExternalAsciiString();
643 inline bool IsSeqTwoByteString();
644 inline bool IsSeqAsciiString();
645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 inline bool IsNumber();
647 inline bool IsByteArray();
648 inline bool IsFailure();
649 inline bool IsRetryAfterGC();
ager@chromium.org7c537e22008-10-16 08:43:32 +0000650 inline bool IsOutOfMemoryFailure();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 inline bool IsException();
652 inline bool IsJSObject();
ager@chromium.org32912102009-01-16 10:38:43 +0000653 inline bool IsJSContextExtensionObject();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 inline bool IsMap();
655 inline bool IsFixedArray();
656 inline bool IsDescriptorArray();
657 inline bool IsContext();
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000658 inline bool IsCatchContext();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 inline bool IsGlobalContext();
660 inline bool IsJSFunction();
661 inline bool IsCode();
662 inline bool IsOddball();
663 inline bool IsSharedFunctionInfo();
664 inline bool IsJSValue();
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000665 inline bool IsStringWrapper();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000666 inline bool IsProxy();
667 inline bool IsBoolean();
668 inline bool IsJSArray();
ager@chromium.org236ad962008-09-25 09:45:57 +0000669 inline bool IsJSRegExp();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 inline bool IsHashTable();
671 inline bool IsDictionary();
672 inline bool IsSymbolTable();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000673 inline bool IsCompilationCacheTable();
ager@chromium.org236ad962008-09-25 09:45:57 +0000674 inline bool IsMapCache();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000675 inline bool IsLookupCache();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 inline bool IsPrimitive();
677 inline bool IsGlobalObject();
678 inline bool IsJSGlobalObject();
679 inline bool IsJSBuiltinsObject();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000680 inline bool IsJSGlobalProxy();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681 inline bool IsUndetectableObject();
682 inline bool IsAccessCheckNeeded();
683
684 // Returns true if this object is an instance of the specified
685 // function template.
686 bool IsInstanceOf(FunctionTemplateInfo* type);
687
688 inline bool IsStruct();
689#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
690 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
691#undef DECLARE_STRUCT_PREDICATE
692
693 // Oddball testing.
694 INLINE(bool IsUndefined());
695 INLINE(bool IsTheHole());
696 INLINE(bool IsNull());
697 INLINE(bool IsTrue());
698 INLINE(bool IsFalse());
699
700 // Extract the number.
701 inline double Number();
702
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000703 inline bool HasSpecificClassOf(String* name);
704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000705 Object* ToObject(); // ECMA-262 9.9.
706 Object* ToBoolean(); // ECMA-262 9.2.
707
708 // Convert to a JSObject if needed.
709 // global_context is used when creating wrapper object.
710 Object* ToObject(Context* global_context);
711
712 // Converts this to a Smi if possible.
713 // Failure is returned otherwise.
714 inline Object* ToSmi();
715
716 void Lookup(String* name, LookupResult* result);
717
718 // Property access.
719 inline Object* GetProperty(String* key);
720 inline Object* GetProperty(String* key, PropertyAttributes* attributes);
721 Object* GetPropertyWithReceiver(Object* receiver,
722 String* key,
723 PropertyAttributes* attributes);
724 Object* GetProperty(Object* receiver,
725 LookupResult* result,
726 String* key,
727 PropertyAttributes* attributes);
728 Object* GetPropertyWithCallback(Object* receiver,
729 Object* structure,
730 String* name,
731 Object* holder);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000732 Object* GetPropertyWithDefinedGetter(Object* receiver,
733 JSFunction* getter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734
735 inline Object* GetElement(uint32_t index);
736 Object* GetElementWithReceiver(Object* receiver, uint32_t index);
737
738 // Return the object's prototype (might be Heap::null_value()).
739 Object* GetPrototype();
740
741 // Returns true if this is a JSValue containing a string and the index is
742 // < the length of the string. Used to implement [] on strings.
743 inline bool IsStringObjectWithCharacterAt(uint32_t index);
744
745#ifdef DEBUG
746 // Prints this object with details.
747 void Print();
748 void PrintLn();
749 // Verifies the object.
750 void Verify();
751
752 // Verify a pointer is a valid object pointer.
753 static void VerifyPointer(Object* p);
754#endif
755
756 // Prints this object without details.
757 void ShortPrint();
758
759 // Prints this object without details to a message accumulator.
760 void ShortPrint(StringStream* accumulator);
761
762 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
763 static Object* cast(Object* value) { return value; }
764
765 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +0000766 static const int kHeaderSize = 0; // Object does not take up any space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000767
768 private:
769 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
770};
771
772
773// Smi represents integer Numbers that can be stored in 31 bits.
774// Smis are immediate which means they are NOT allocated in the heap.
775// The this pointer has the following format: [31 bit signed int] 0
776// Smi stands for small integer.
777class Smi: public Object {
778 public:
779 // Returns the integer value.
780 inline int value();
781
782 // Convert a value to a Smi object.
783 static inline Smi* FromInt(int value);
784
ager@chromium.org9085a012009-05-11 19:22:57 +0000785 static inline Smi* FromIntptr(intptr_t value);
786
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787 // Returns whether value can be represented in a Smi.
788 static inline bool IsValid(int value);
789
ager@chromium.org9085a012009-05-11 19:22:57 +0000790 static inline bool IsIntptrValid(intptr_t);
791
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000792 // Casting.
793 static inline Smi* cast(Object* object);
794
795 // Dispatched behavior.
796 void SmiPrint();
797 void SmiPrint(StringStream* accumulator);
798#ifdef DEBUG
799 void SmiVerify();
800#endif
801
ager@chromium.org5ec48922009-05-05 07:25:34 +0000802 static const int kSmiNumBits = 31;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000803 // Min and max limits for Smi values.
ager@chromium.org5ec48922009-05-05 07:25:34 +0000804 static const int kMinValue = -(1 << (kSmiNumBits - 1));
805 static const int kMaxValue = (1 << (kSmiNumBits - 1)) - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806
807 private:
808 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
809};
810
811
ager@chromium.org6f10e412009-02-13 10:11:16 +0000812// Failure is used for reporting out of memory situations and
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813// propagating exceptions through the runtime system. Failure objects
814// are transient and cannot occur as part of the objects graph.
815//
816// Failures are a single word, encoded as follows:
817// +-------------------------+---+--+--+
818// |rrrrrrrrrrrrrrrrrrrrrrrrr|sss|tt|11|
819// +-------------------------+---+--+--+
820//
821// The low two bits, 0-1, are the failure tag, 11. The next two bits,
822// 2-3, are a failure type tag 'tt' with possible values:
823// 00 RETRY_AFTER_GC
824// 01 EXCEPTION
825// 10 INTERNAL_ERROR
826// 11 OUT_OF_MEMORY_EXCEPTION
827//
828// The next three bits, 4-6, are an allocation space tag 'sss'. The
829// allocation space tag is 000 for all failure types except
830// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are
831// (the encoding is found in globals.h):
832// 000 NEW_SPACE
833// 001 OLD_SPACE
834// 010 CODE_SPACE
835// 011 MAP_SPACE
836// 100 LO_SPACE
837//
838// The remaining bits is the number of words requested by the
839// allocation request that failed, and is zeroed except for
840// RETRY_AFTER_GC failures. The 25 bits (on a 32 bit platform) gives
841// a representable range of 2^27 bytes (128MB).
842
843// Failure type tag info.
844const int kFailureTypeTagSize = 2;
845const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
846
847class Failure: public Object {
848 public:
849 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
850 enum Type {
851 RETRY_AFTER_GC = 0,
852 EXCEPTION = 1, // Returning this marker tells the real exception
853 // is in Top::pending_exception.
854 INTERNAL_ERROR = 2,
855 OUT_OF_MEMORY_EXCEPTION = 3
856 };
857
858 inline Type type() const;
859
860 // Returns the space that needs to be collected for RetryAfterGC failures.
861 inline AllocationSpace allocation_space() const;
862
863 // Returns the number of bytes requested (up to the representable maximum)
864 // for RetryAfterGC failures.
865 inline int requested() const;
866
867 inline bool IsInternalError() const;
868 inline bool IsOutOfMemoryException() const;
869
870 static Failure* RetryAfterGC(int requested_bytes, AllocationSpace space);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000871 static inline Failure* RetryAfterGC(int requested_bytes); // NEW_SPACE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872 static inline Failure* Exception();
873 static inline Failure* InternalError();
874 static inline Failure* OutOfMemoryException();
875 // Casting.
876 static inline Failure* cast(Object* object);
877
878 // Dispatched behavior.
879 void FailurePrint();
880 void FailurePrint(StringStream* accumulator);
881#ifdef DEBUG
882 void FailureVerify();
883#endif
884
885 private:
886 inline int value() const;
887 static inline Failure* Construct(Type type, int value = 0);
888
889 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
890};
891
892
kasper.lund7276f142008-07-30 08:49:36 +0000893// Heap objects typically have a map pointer in their first word. However,
894// during GC other data (eg, mark bits, forwarding addresses) is sometimes
895// encoded in the first word. The class MapWord is an abstraction of the
896// value in a heap object's first word.
897class MapWord BASE_EMBEDDED {
898 public:
899 // Normal state: the map word contains a map pointer.
900
901 // Create a map word from a map pointer.
902 static inline MapWord FromMap(Map* map);
903
904 // View this map word as a map pointer.
905 inline Map* ToMap();
906
907
908 // Scavenge collection: the map word of live objects in the from space
909 // contains a forwarding address (a heap object pointer in the to space).
910
911 // True if this map word is a forwarding address for a scavenge
912 // collection. Only valid during a scavenge collection (specifically,
913 // when all map words are heap object pointers, ie. not during a full GC).
914 inline bool IsForwardingAddress();
915
916 // Create a map word from a forwarding address.
917 static inline MapWord FromForwardingAddress(HeapObject* object);
918
919 // View this map word as a forwarding address.
920 inline HeapObject* ToForwardingAddress();
921
922
923 // Marking phase of full collection: the map word of live objects is
924 // marked, and may be marked as overflowed (eg, the object is live, its
925 // children have not been visited, and it does not fit in the marking
926 // stack).
927
928 // True if this map word's mark bit is set.
929 inline bool IsMarked();
930
931 // Return this map word but with its mark bit set.
932 inline void SetMark();
933
934 // Return this map word but with its mark bit cleared.
935 inline void ClearMark();
936
937 // True if this map word's overflow bit is set.
938 inline bool IsOverflowed();
939
940 // Return this map word but with its overflow bit set.
941 inline void SetOverflow();
942
943 // Return this map word but with its overflow bit cleared.
944 inline void ClearOverflow();
945
946
947 // Compacting phase of a full compacting collection: the map word of live
948 // objects contains an encoding of the original map address along with the
949 // forwarding address (represented as an offset from the first live object
950 // in the same page as the (old) object address).
951
952 // Create a map word from a map address and a forwarding address offset.
953 static inline MapWord EncodeAddress(Address map_address, int offset);
954
955 // Return the map address encoded in this map word.
956 inline Address DecodeMapAddress(MapSpace* map_space);
957
958 // Return the forwarding offset encoded in this map word.
959 inline int DecodeOffset();
960
961
962 // During serialization: the map word is used to hold an encoded
963 // address, and possibly a mark bit (set and cleared with SetMark
964 // and ClearMark).
965
966 // Create a map word from an encoded address.
967 static inline MapWord FromEncodedAddress(Address address);
968
969 inline Address ToEncodedAddress();
970
971 private:
972 // HeapObject calls the private constructor and directly reads the value.
973 friend class HeapObject;
974
975 explicit MapWord(uintptr_t value) : value_(value) {}
976
977 uintptr_t value_;
978
979 // Bits used by the marking phase of the garbage collector.
980 //
ager@chromium.org6f10e412009-02-13 10:11:16 +0000981 // The first word of a heap object is normally a map pointer. The last two
kasper.lund7276f142008-07-30 08:49:36 +0000982 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
983 // mark an object as live and/or overflowed:
984 // last bit = 0, marked as alive
985 // second bit = 1, overflowed
986 // An object is only marked as overflowed when it is marked as live while
987 // the marking stack is overflowed.
988 static const int kMarkingBit = 0; // marking bit
989 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
990 static const int kOverflowBit = 1; // overflow bit
991 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
992
993 // Forwarding pointers and map pointer encoding
994 // 31 21 20 10 9 0
995 // +-----------------+------------------+-----------------+
996 // |forwarding offset|page offset of map|page index of map|
997 // +-----------------+------------------+-----------------+
998 // 11 bits 11 bits 10 bits
999 static const int kMapPageIndexBits = 10;
1000 static const int kMapPageOffsetBits = 11;
1001 static const int kForwardingOffsetBits = 11;
1002
1003 static const int kMapPageIndexShift = 0;
1004 static const int kMapPageOffsetShift =
1005 kMapPageIndexShift + kMapPageIndexBits;
1006 static const int kForwardingOffsetShift =
1007 kMapPageOffsetShift + kMapPageOffsetBits;
1008
1009 // 0x000003FF
1010 static const uint32_t kMapPageIndexMask =
1011 (1 << kMapPageOffsetShift) - 1;
1012
1013 // 0x001FFC00
1014 static const uint32_t kMapPageOffsetMask =
1015 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
1016
1017 // 0xFFE00000
1018 static const uint32_t kForwardingOffsetMask =
1019 ~(kMapPageIndexMask | kMapPageOffsetMask);
1020};
1021
1022
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023// HeapObject is the superclass for all classes describing heap allocated
1024// objects.
1025class HeapObject: public Object {
1026 public:
kasper.lund7276f142008-07-30 08:49:36 +00001027 // [map]: Contains a map which contains the object's reflective
1028 // information.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 inline Map* map();
1030 inline void set_map(Map* value);
1031
kasper.lund7276f142008-07-30 08:49:36 +00001032 // During garbage collection, the map word of a heap object does not
1033 // necessarily contain a map pointer.
1034 inline MapWord map_word();
1035 inline void set_map_word(MapWord map_word);
1036
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 // Converts an address to a HeapObject pointer.
1038 static inline HeapObject* FromAddress(Address address);
1039
1040 // Returns the address of this HeapObject.
1041 inline Address address();
1042
1043 // Iterates over pointers contained in the object (including the Map)
1044 void Iterate(ObjectVisitor* v);
1045
1046 // Iterates over all pointers contained in the object except the
1047 // first map pointer. The object type is given in the first
1048 // parameter. This function does not access the map pointer in the
1049 // object, and so is safe to call while the map pointer is modified.
1050 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1051
1052 // This method only applies to struct objects. Iterates over all the fields
1053 // of this struct.
1054 void IterateStructBody(int object_size, ObjectVisitor* v);
1055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 // Returns the heap object's size in bytes
1057 inline int Size();
1058
1059 // Given a heap object's map pointer, returns the heap size in bytes
1060 // Useful when the map pointer field is used for other purposes.
1061 // GC internal.
1062 inline int SizeFromMap(Map* map);
1063
kasper.lund7276f142008-07-30 08:49:36 +00001064 // Support for the marking heap objects during the marking phase of GC.
1065 // True if the object is marked live.
1066 inline bool IsMarked();
1067
1068 // Mutate this object's map pointer to indicate that the object is live.
1069 inline void SetMark();
1070
1071 // Mutate this object's map pointer to remove the indication that the
1072 // object is live (ie, partially restore the map pointer).
1073 inline void ClearMark();
1074
1075 // True if this object is marked as overflowed. Overflowed objects have
1076 // been reached and marked during marking of the heap, but their children
1077 // have not necessarily been marked and they have not been pushed on the
1078 // marking stack.
1079 inline bool IsOverflowed();
1080
1081 // Mutate this object's map pointer to indicate that the object is
1082 // overflowed.
1083 inline void SetOverflow();
1084
1085 // Mutate this object's map pointer to remove the indication that the
1086 // object is overflowed (ie, partially restore the map pointer).
1087 inline void ClearOverflow();
1088
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001089 // Returns the field at offset in obj, as a read/write Object* reference.
1090 // Does no checking, and is safe to use during GC, while maps are invalid.
1091 // Does not update remembered sets, so should only be assigned to
1092 // during marking GC.
1093 static inline Object** RawField(HeapObject* obj, int offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094
1095 // Casting.
1096 static inline HeapObject* cast(Object* obj);
1097
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001098 // Return the write barrier mode for this.
1099 inline WriteBarrierMode GetWriteBarrierMode();
1100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 // Dispatched behavior.
1102 void HeapObjectShortPrint(StringStream* accumulator);
1103#ifdef DEBUG
1104 void HeapObjectPrint();
1105 void HeapObjectVerify();
1106 inline void VerifyObjectField(int offset);
1107
1108 void PrintHeader(const char* id);
1109
1110 // Verify a pointer is a valid HeapObject pointer that points to object
1111 // areas in the heap.
1112 static void VerifyHeapPointer(Object* p);
1113#endif
1114
1115 // Layout description.
1116 // First field in a heap object is map.
ager@chromium.org236ad962008-09-25 09:45:57 +00001117 static const int kMapOffset = Object::kHeaderSize;
1118 static const int kHeaderSize = kMapOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119
1120 protected:
1121 // helpers for calling an ObjectVisitor to iterate over pointers in the
1122 // half-open range [start, end) specified as integer offsets
1123 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1124 // as above, for the single element at "offset"
1125 inline void IteratePointer(ObjectVisitor* v, int offset);
1126
1127 // Computes the object size from the map.
1128 // Should only be used from SizeFromMap.
1129 int SlowSizeFromMap(Map* map);
1130
1131 private:
1132 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1133};
1134
1135
1136// The HeapNumber class describes heap allocated numbers that cannot be
1137// represented in a Smi (small integer)
1138class HeapNumber: public HeapObject {
1139 public:
1140 // [value]: number value.
1141 inline double value();
1142 inline void set_value(double value);
1143
1144 // Casting.
1145 static inline HeapNumber* cast(Object* obj);
1146
1147 // Dispatched behavior.
1148 Object* HeapNumberToBoolean();
1149 void HeapNumberPrint();
1150 void HeapNumberPrint(StringStream* accumulator);
1151#ifdef DEBUG
1152 void HeapNumberVerify();
1153#endif
1154
1155 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00001156 static const int kValueOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 static const int kSize = kValueOffset + kDoubleSize;
1158
1159 private:
1160 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1161};
1162
1163
1164// The JSObject describes real heap allocated JavaScript objects with
1165// properties.
1166// Note that the map of JSObject changes during execution to enable inline
1167// caching.
1168class JSObject: public HeapObject {
1169 public:
1170 // [properties]: Backing storage for properties.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001171 // properties is a FixedArray in the fast case, and a Dictionary in the
1172 // slow case.
1173 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001174 inline void initialize_properties();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 inline bool HasFastProperties();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001176 inline Dictionary* property_dictionary(); // Gets slow properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001178 // [elements]: The elements (properties with names that are integers).
1179 // elements is a FixedArray in the fast case, and a Dictionary in the slow
1180 // case.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001181 DECL_ACCESSORS(elements, FixedArray) // Get and set fast elements.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001182 inline void initialize_elements();
1183 inline bool HasFastElements();
1184 inline Dictionary* element_dictionary(); // Gets slow elements.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185
ager@chromium.org5ec48922009-05-05 07:25:34 +00001186 // Collects elements starting at index 0.
1187 // Undefined values are placed after non-undefined values.
1188 // Returns the number of non-undefined values.
1189 Object* PrepareElementsForSort(uint32_t limit);
1190 // As PrepareElementsForSort, but only on objects where elements is
1191 // a dictionary, and it will stay a dictionary.
1192 Object* PrepareSlowElementsForSort(uint32_t limit);
1193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194 Object* SetProperty(String* key,
1195 Object* value,
1196 PropertyAttributes attributes);
1197 Object* SetProperty(LookupResult* result,
1198 String* key,
1199 Object* value,
1200 PropertyAttributes attributes);
1201 Object* SetPropertyWithFailedAccessCheck(LookupResult* result,
1202 String* name,
1203 Object* value);
1204 Object* SetPropertyWithCallback(Object* structure,
1205 String* name,
1206 Object* value,
1207 JSObject* holder);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001208 Object* SetPropertyWithDefinedSetter(JSFunction* setter,
1209 Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 Object* SetPropertyWithInterceptor(String* name,
1211 Object* value,
1212 PropertyAttributes attributes);
1213 Object* SetPropertyPostInterceptor(String* name,
1214 Object* value,
1215 PropertyAttributes attributes);
1216 Object* IgnoreAttributesAndSetLocalProperty(String* key,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001217 Object* value,
1218 PropertyAttributes attributes);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001219
1220 // Sets a property that currently has lazy loading.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001221 Object* SetLazyProperty(LookupResult* result,
1222 String* name,
1223 Object* value,
1224 PropertyAttributes attributes);
1225
1226 // Returns the class name ([[Class]] property in the specification).
1227 String* class_name();
1228
1229 // Retrieve interceptors.
1230 InterceptorInfo* GetNamedInterceptor();
1231 InterceptorInfo* GetIndexedInterceptor();
1232
1233 inline PropertyAttributes GetPropertyAttribute(String* name);
1234 PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1235 String* name);
1236 PropertyAttributes GetLocalPropertyAttribute(String* name);
1237
1238 Object* DefineAccessor(String* name, bool is_getter, JSFunction* fun,
1239 PropertyAttributes attributes);
1240 Object* LookupAccessor(String* name, bool is_getter);
1241
1242 // Used from Object::GetProperty().
1243 Object* GetPropertyWithFailedAccessCheck(Object* receiver,
1244 LookupResult* result,
ager@chromium.org8bb60582008-12-11 12:02:20 +00001245 String* name,
1246 PropertyAttributes* attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 Object* GetPropertyWithInterceptor(JSObject* receiver,
1248 String* name,
1249 PropertyAttributes* attributes);
1250 Object* GetPropertyPostInterceptor(JSObject* receiver,
1251 String* name,
1252 PropertyAttributes* attributes);
1253 Object* GetLazyProperty(Object* receiver,
1254 LookupResult* result,
1255 String* name,
1256 PropertyAttributes* attributes);
1257
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001258 // Tells whether this object needs to be loaded.
1259 inline bool IsLoaded();
1260
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 bool HasProperty(String* name) {
1262 return GetPropertyAttribute(name) != ABSENT;
1263 }
1264
ager@chromium.org9085a012009-05-11 19:22:57 +00001265 // Can cause a GC if it hits an interceptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 bool HasLocalProperty(String* name) {
1267 return GetLocalPropertyAttribute(name) != ABSENT;
1268 }
1269
1270 Object* DeleteProperty(String* name);
1271 Object* DeleteElement(uint32_t index);
1272 Object* DeleteLazyProperty(LookupResult* result, String* name);
1273
1274 // Tests for the fast common case for property enumeration.
1275 bool IsSimpleEnum();
1276
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001277 // Do we want to keep the elements in fast case when increasing the
1278 // capacity?
1279 bool ShouldConvertToSlowElements(int new_capacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 // Returns true if the backing storage for the slow-case elements of
1281 // this object takes up nearly as much space as a fast-case backing
1282 // storage would. In that case the JSObject should have fast
1283 // elements.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001284 bool ShouldConvertToFastElements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285
1286 // Return the object's prototype (might be Heap::null_value()).
1287 inline Object* GetPrototype();
1288
1289 // Tells whether the index'th element is present.
1290 inline bool HasElement(uint32_t index);
1291 bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
1292 bool HasLocalElement(uint32_t index);
1293
1294 bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1295 bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1296
1297 Object* SetFastElement(uint32_t index, Object* value);
1298
1299 // Set the index'th array element.
1300 // A Failure object is returned if GC is needed.
1301 Object* SetElement(uint32_t index, Object* value);
1302
1303 // Returns the index'th element.
1304 // The undefined object if index is out of bounds.
1305 Object* GetElementWithReceiver(JSObject* receiver, uint32_t index);
1306
1307 void SetFastElements(FixedArray* elements);
1308 Object* SetSlowElements(Object* length);
1309
1310 // Lookup interceptors are used for handling properties controlled by host
1311 // objects.
1312 inline bool HasNamedInterceptor();
1313 inline bool HasIndexedInterceptor();
1314
1315 // Support functions for v8 api (needed for correct interceptor behavior).
1316 bool HasRealNamedProperty(String* key);
1317 bool HasRealElementProperty(uint32_t index);
1318 bool HasRealNamedCallbackProperty(String* key);
1319
1320 // Initializes the array to a certain length
1321 Object* SetElementsLength(Object* length);
1322
1323 // Get the header size for a JSObject. Used to compute the index of
1324 // internal fields as well as the number of internal fields.
1325 inline int GetHeaderSize();
1326
1327 inline int GetInternalFieldCount();
1328 inline Object* GetInternalField(int index);
1329 inline void SetInternalField(int index, Object* value);
1330
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 // Lookup a property. If found, the result is valid and has
1332 // detailed information.
1333 void LocalLookup(String* name, LookupResult* result);
1334 void Lookup(String* name, LookupResult* result);
1335
1336 // The following lookup functions skip interceptors.
1337 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1338 void LookupRealNamedProperty(String* name, LookupResult* result);
1339 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1340 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001341 Object* LookupCallbackSetterInPrototypes(uint32_t index);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001342 void LookupCallback(String* name, LookupResult* result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343
1344 // Returns the number of properties on this object filtering out properties
1345 // with the specified attributes (ignoring interceptors).
1346 int NumberOfLocalProperties(PropertyAttributes filter);
1347 // Returns the number of enumerable properties (ignoring interceptors).
1348 int NumberOfEnumProperties();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001349 // Fill in details for properties into storage starting at the specified
1350 // index.
1351 void GetLocalPropertyNames(FixedArray* storage, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352
1353 // Returns the number of properties on this object filtering out properties
1354 // with the specified attributes (ignoring interceptors).
1355 int NumberOfLocalElements(PropertyAttributes filter);
1356 // Returns the number of enumerable elements (ignoring interceptors).
1357 int NumberOfEnumElements();
1358 // Returns the number of elements on this object filtering out elements
1359 // with the specified attributes (ignoring interceptors).
1360 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1361 // Count and fill in the enumerable elements into storage.
1362 // (storage->length() == NumberOfEnumElements()).
1363 // If storage is NULL, will count the elements without adding
1364 // them to any storage.
1365 // Returns the number of enumerable elements.
1366 int GetEnumElementKeys(FixedArray* storage);
1367
1368 // Add a property to a fast-case object using a map transition to
1369 // new_map.
1370 Object* AddFastPropertyUsingMap(Map* new_map,
1371 String* name,
1372 Object* value);
1373
1374 // Add a constant function property to a fast-case object.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001375 // This leaves a CONSTANT_TRANSITION in the old map, and
1376 // if it is called on a second object with this map, a
1377 // normal property is added instead, with a map transition.
1378 // This avoids the creation of many maps with the same constant
1379 // function, all orphaned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 Object* AddConstantFunctionProperty(String* name,
1381 JSFunction* function,
1382 PropertyAttributes attributes);
1383
ager@chromium.org7c537e22008-10-16 08:43:32 +00001384 Object* ReplaceSlowProperty(String* name,
1385 Object* value,
1386 PropertyAttributes attributes);
1387
1388 // Converts a descriptor of any other type to a real field,
1389 // backed by the properties array. Descriptors of visible
1390 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1391 // Converts the descriptor on the original object's map to a
1392 // map transition, and the the new field is on the object's new map.
1393 Object* ConvertDescriptorToFieldAndMapTransition(
1394 String* name,
1395 Object* new_value,
1396 PropertyAttributes attributes);
1397
1398 // Converts a descriptor of any other type to a real field,
1399 // backed by the properties array. Descriptors of visible
1400 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1401 Object* ConvertDescriptorToField(String* name,
1402 Object* new_value,
1403 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404
1405 // Add a property to a fast-case object.
1406 Object* AddFastProperty(String* name,
1407 Object* value,
1408 PropertyAttributes attributes);
1409
1410 // Add a property to a slow-case object.
1411 Object* AddSlowProperty(String* name,
1412 Object* value,
1413 PropertyAttributes attributes);
1414
1415 // Add a property to an object.
1416 Object* AddProperty(String* name,
1417 Object* value,
1418 PropertyAttributes attributes);
1419
1420 // Convert the object to use the canonical dictionary
1421 // representation.
ager@chromium.org32912102009-01-16 10:38:43 +00001422 Object* NormalizeProperties(PropertyNormalizationMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 Object* NormalizeElements();
1424
1425 // Transform slow named properties to fast variants.
1426 // Returns failure if allocation failed.
1427 Object* TransformToFastProperties(int unused_property_fields);
1428
ager@chromium.org7c537e22008-10-16 08:43:32 +00001429 // Access fast-case object properties at index.
1430 inline Object* FastPropertyAt(int index);
1431 inline Object* FastPropertyAtPut(int index, Object* value);
1432
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001433 // Access to in object properties.
1434 inline Object* InObjectPropertyAt(int index);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001435 inline Object* InObjectPropertyAtPut(int index,
1436 Object* value,
1437 WriteBarrierMode mode
1438 = UPDATE_WRITE_BARRIER);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001439
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 // initializes the body after properties slot, properties slot is
1441 // initialized by set_properties
1442 // Note: this call does not update write barrier, it is caller's
1443 // reponsibility to ensure that *v* can be collected without WB here.
1444 inline void InitializeBody(int object_size);
1445
1446 // Check whether this object references another object
1447 bool ReferencesObject(Object* obj);
1448
1449 // Casting.
1450 static inline JSObject* cast(Object* obj);
1451
1452 // Dispatched behavior.
1453 void JSObjectIterateBody(int object_size, ObjectVisitor* v);
1454 void JSObjectShortPrint(StringStream* accumulator);
1455#ifdef DEBUG
1456 void JSObjectPrint();
1457 void JSObjectVerify();
1458 void PrintProperties();
1459 void PrintElements();
1460
1461 // Structure for collecting spill information about JSObjects.
1462 class SpillInformation {
1463 public:
1464 void Clear();
1465 void Print();
1466 int number_of_objects_;
1467 int number_of_objects_with_fast_properties_;
1468 int number_of_objects_with_fast_elements_;
1469 int number_of_fast_used_fields_;
1470 int number_of_fast_unused_fields_;
1471 int number_of_slow_used_properties_;
1472 int number_of_slow_unused_properties_;
1473 int number_of_fast_used_elements_;
1474 int number_of_fast_unused_elements_;
1475 int number_of_slow_used_elements_;
1476 int number_of_slow_unused_elements_;
1477 };
1478
1479 void IncrementSpillStatistics(SpillInformation* info);
1480#endif
1481 Object* SlowReverseLookup(Object* value);
1482
1483 static const uint32_t kMaxGap = 1024;
1484 static const int kMaxFastElementsLength = 5000;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001485 static const int kInitialMaxFastElementArray = 100000;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001486 static const int kMaxFastProperties = 8;
ager@chromium.org7c537e22008-10-16 08:43:32 +00001487 static const int kMaxInstanceSize = 255 * kPointerSize;
1488 // When extending the backing storage for property values, we increase
1489 // its size by more than the 1 entry necessary, so sequentially adding fields
1490 // to the same object requires fewer allocations and copies.
1491 static const int kFieldsAdded = 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492
1493 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00001494 static const int kPropertiesOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1496 static const int kHeaderSize = kElementsOffset + kPointerSize;
1497
1498 Object* GetElementWithInterceptor(JSObject* receiver, uint32_t index);
1499
1500 private:
1501 Object* SetElementWithInterceptor(uint32_t index, Object* value);
1502 Object* SetElementPostInterceptor(uint32_t index, Object* value);
1503
1504 Object* GetElementPostInterceptor(JSObject* receiver, uint32_t index);
1505
1506 Object* DeletePropertyPostInterceptor(String* name);
1507 Object* DeletePropertyWithInterceptor(String* name);
1508
1509 Object* DeleteElementPostInterceptor(uint32_t index);
1510 Object* DeleteElementWithInterceptor(uint32_t index);
1511
1512 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1513 String* name,
1514 bool continue_search);
1515 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1516 String* name,
1517 bool continue_search);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001518 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1519 Object* receiver,
1520 LookupResult* result,
1521 String* name,
1522 bool continue_search);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1524 LookupResult* result,
1525 String* name,
1526 bool continue_search);
1527
1528 // Returns true if most of the elements backing storage is used.
1529 bool HasDenseElements();
1530
1531 Object* DefineGetterSetter(String* name, PropertyAttributes attributes);
1532
1533 void LookupInDescriptor(String* name, LookupResult* result);
1534
1535 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1536};
1537
1538
1539// Abstract super class arrays. It provides length behavior.
1540class Array: public HeapObject {
1541 public:
1542 // [length]: length of the array.
1543 inline int length();
1544 inline void set_length(int value);
1545
1546 // Convert an object to an array index.
1547 // Returns true if the conversion succeeded.
1548 static inline bool IndexFromObject(Object* object, uint32_t* index);
1549
1550 // Layout descriptor.
ager@chromium.org236ad962008-09-25 09:45:57 +00001551 static const int kLengthOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552 static const int kHeaderSize = kLengthOffset + kIntSize;
1553
1554 private:
1555 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
1556};
1557
1558
1559// FixedArray describes fixed sized arrays where element
1560// type is Object*.
1561
1562class FixedArray: public Array {
1563 public:
1564
1565 // Setter and getter for elements.
1566 inline Object* get(int index);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001567 // Setter that uses write barrier.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 inline void set(int index, Object* value);
1569
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001570 // Setter that doesn't need write barrier).
1571 inline void set(int index, Smi* value);
1572 // Setter with explicit barrier mode.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001573 inline void set(int index, Object* value, WriteBarrierMode mode);
1574
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001575 // Setters for frequently used oddballs located in old space.
1576 inline void set_undefined(int index);
ager@chromium.org236ad962008-09-25 09:45:57 +00001577 inline void set_null(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 inline void set_the_hole(int index);
1579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 // Copy operations.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001581 inline Object* Copy();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582 Object* CopySize(int new_length);
1583
1584 // Add the elements of a JSArray to this FixedArray.
1585 Object* AddKeysFromJSArray(JSArray* array);
1586
1587 // Compute the union of this and other.
1588 Object* UnionOfKeys(FixedArray* other);
1589
1590 // Copy a sub array from the receiver to dest.
1591 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1592
1593 // Garbage collection support.
1594 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1595
1596 // Casting.
1597 static inline FixedArray* cast(Object* obj);
1598
1599 // Dispatched behavior.
1600 int FixedArraySize() { return SizeFor(length()); }
1601 void FixedArrayIterateBody(ObjectVisitor* v);
1602#ifdef DEBUG
1603 void FixedArrayPrint();
1604 void FixedArrayVerify();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001605 // Checks if two FixedArrays have identical contents.
1606 bool IsEqualTo(FixedArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607#endif
1608
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001609 // Swap two elements in a pair of arrays. If this array and the
1610 // numbers array are the same object, the elements are only swapped
1611 // once.
1612 void SwapPairs(FixedArray* numbers, int i, int j);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001614 // Sort prefix of this array and the numbers array as pairs wrt. the
1615 // numbers. If the numbers array and the this array are the same
1616 // object, the prefix of this array is sorted.
1617 void SortPairs(FixedArray* numbers, uint32_t len);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618
1619 protected:
1620 // Set operation on FixedArray without using write barriers.
1621 static inline void fast_set(FixedArray* array, int index, Object* value);
1622
1623 private:
1624 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
1625};
1626
1627
1628// DescriptorArrays are fixed arrays used to hold instance descriptors.
1629// The format of the these objects is:
1630// [0]: point to a fixed array with (value, detail) pairs.
1631// [1]: next enumeration index (Smi), or pointer to small fixed array:
1632// [0]: next enumeration index (Smi)
1633// [1]: pointer to fixed array with enum cache
1634// [2]: first key
1635// [length() - 1]: last key
1636//
1637class DescriptorArray: public FixedArray {
1638 public:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001639 // Is this the singleton empty_descriptor_array?
1640 inline bool IsEmpty();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 // Returns the number of descriptors in the array.
1642 int number_of_descriptors() {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001643 return IsEmpty() ? 0 : length() - kFirstIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 }
1645
1646 int NextEnumerationIndex() {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001647 if (IsEmpty()) return PropertyDetails::kInitialIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 Object* obj = get(kEnumerationIndexIndex);
1649 if (obj->IsSmi()) {
1650 return Smi::cast(obj)->value();
1651 } else {
1652 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
1653 return Smi::cast(index)->value();
1654 }
1655 }
1656
1657 // Set next enumeration index and flush any enum cache.
1658 void SetNextEnumerationIndex(int value) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001659 if (!IsEmpty()) {
1660 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
1661 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001662 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663 bool HasEnumCache() {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001664 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 }
1666
1667 Object* GetEnumCache() {
1668 ASSERT(HasEnumCache());
1669 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
1670 return bridge->get(kEnumCacheBridgeCacheIndex);
1671 }
1672
1673 // Initialize or change the enum cache,
1674 // using the supplied storage for the small "bridge".
1675 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
1676
1677 // Accessors for fetching instance descriptor at descriptor number..
1678 inline String* GetKey(int descriptor_number);
1679 inline Object* GetValue(int descriptor_number);
1680 inline Smi* GetDetails(int descriptor_number);
1681
1682 // Accessor for complete descriptor.
1683 inline void Get(int descriptor_number, Descriptor* desc);
1684 inline void Set(int descriptor_number, Descriptor* desc);
1685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 // Copy the descriptor array, insert a new descriptor and optionally
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001687 // remove map transitions. If the descriptor is already present, it is
1688 // replaced. If a replaced descriptor is a real property (not a transition
1689 // or null), its enumeration index is kept as is.
1690 // If adding a real property, map transitions must be removed. If adding
1691 // a transition, they must not be removed. All null descriptors are removed.
1692 Object* CopyInsert(Descriptor* descriptor, TransitionFlag transition_flag);
1693
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001694 // Remove all transitions. Return a copy of the array with all transitions
1695 // removed, or a Failure object if the new array could not be allocated.
1696 Object* RemoveTransitions();
1697
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 // Sort the instance descriptors by the hash codes of their keys.
1699 void Sort();
1700
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 // Search the instance descriptors for given name.
1702 inline int Search(String* name);
1703
1704 // Tells whether the name is present int the array.
1705 bool Contains(String* name) { return kNotFound != Search(name); }
1706
1707 // Perform a binary search in the instance descriptors represented
1708 // by this fixed array. low and high are descriptor indices. If there
1709 // are three instance descriptors in this array it should be called
1710 // with low=0 and high=2.
1711 int BinarySearch(String* name, int low, int high);
1712
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001713 // Perform a linear search in the instance descriptors represented
ager@chromium.org32912102009-01-16 10:38:43 +00001714 // by this fixed array. len is the number of descriptor indices that are
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001715 // valid. Does not require the descriptors to be sorted.
1716 int LinearSearch(String* name, int len);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001717
1718 // Allocates a DescriptorArray, but returns the singleton
1719 // empty descriptor array object if number_of_descriptors is 0.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 static Object* Allocate(int number_of_descriptors);
1721
1722 // Casting.
1723 static inline DescriptorArray* cast(Object* obj);
1724
1725 // Constant for denoting key was not found.
1726 static const int kNotFound = -1;
1727
1728 static const int kContentArrayIndex = 0;
1729 static const int kEnumerationIndexIndex = 1;
1730 static const int kFirstIndex = 2;
1731
1732 // The length of the "bridge" to the enum cache.
1733 static const int kEnumCacheBridgeLength = 2;
1734 static const int kEnumCacheBridgeEnumIndex = 0;
1735 static const int kEnumCacheBridgeCacheIndex = 1;
1736
1737 // Layout description.
1738 static const int kContentArrayOffset = FixedArray::kHeaderSize;
1739 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
1740 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
1741
1742 // Layout description for the bridge array.
1743 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
1744 static const int kEnumCacheBridgeCacheOffset =
1745 kEnumCacheBridgeEnumOffset + kPointerSize;
1746
1747#ifdef DEBUG
1748 // Print all the descriptors.
1749 void PrintDescriptors();
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001750
1751 // Is the descriptor array sorted and without duplicates?
1752 bool IsSortedNoDuplicates();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001753
1754 // Are two DescriptorArrays equal?
1755 bool IsEqualTo(DescriptorArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756#endif
1757
1758 // The maximum number of descriptors we want in a descriptor array (should
1759 // fit in a page).
1760 static const int kMaxNumberOfDescriptors = 1024 + 512;
1761
1762 private:
1763 // Conversion from descriptor number to array indices.
1764 static int ToKeyIndex(int descriptor_number) {
1765 return descriptor_number+kFirstIndex;
1766 }
1767 static int ToValueIndex(int descriptor_number) {
1768 return descriptor_number << 1;
1769 }
1770 static int ToDetailsIndex(int descriptor_number) {
1771 return( descriptor_number << 1) + 1;
1772 }
1773
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001774 bool is_null_descriptor(int descriptor_number) {
1775 return PropertyDetails(GetDetails(descriptor_number)).type() ==
1776 NULL_DESCRIPTOR;
1777 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778 // Swap operation on FixedArray without using write barriers.
1779 static inline void fast_swap(FixedArray* array, int first, int second);
1780
1781 // Swap descriptor first and second.
1782 inline void Swap(int first, int second);
1783
1784 FixedArray* GetContentArray() {
1785 return FixedArray::cast(get(kContentArrayIndex));
1786 }
1787 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
1788};
1789
1790
1791// HashTable is a subclass of FixedArray that implements a hash table
1792// that uses open addressing and quadratic probing.
1793//
1794// In order for the quadratic probing to work, elements that have not
1795// yet been used and elements that have been deleted are
1796// distinguished. Probing continues when deleted elements are
1797// encountered and stops when unused elements are encountered.
1798//
1799// - Elements with key == undefined have not been used yet.
1800// - Elements with key == null have been deleted.
1801//
1802// The hash table class is parameterized with a prefix size and with
1803// the size, including the key size, of the elements held in the hash
1804// table. The prefix size indicates an amount of memory in the
1805// beginning of the backing storage that can be used for non-element
1806// information by subclasses.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001807
1808// HashTableKey is an abstract superclass keys.
1809class HashTableKey {
1810 public:
1811 // Returns whether the other object matches this key.
1812 virtual bool IsMatch(Object* other) = 0;
1813 typedef uint32_t (*HashFunction)(Object* obj);
1814 // Returns the hash function used for this key.
1815 virtual HashFunction GetHashFunction() = 0;
1816 // Returns the hash value for this key.
1817 virtual uint32_t Hash() = 0;
1818 // Returns the key object for storing into the dictionary.
1819 // If allocations fails a failure object is returned.
1820 virtual Object* GetObject() = 0;
1821 virtual bool IsStringKey() = 0;
1822 // Required.
1823 virtual ~HashTableKey() {}
1824};
1825
1826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827template<int prefix_size, int element_size>
1828class HashTable: public FixedArray {
1829 public:
1830 // Returns the number of elements in the dictionary.
1831 int NumberOfElements() {
1832 return Smi::cast(get(kNumberOfElementsIndex))->value();
1833 }
1834
1835 // Returns the capacity of the dictionary.
1836 int Capacity() {
1837 return Smi::cast(get(kCapacityIndex))->value();
1838 }
1839
1840 // ElementAdded should be called whenever an element is added to a
1841 // dictionary.
1842 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
1843
1844 // ElementRemoved should be called whenever an element is removed from
1845 // a dictionary.
1846 void ElementRemoved() { SetNumberOfElements(NumberOfElements() - 1); }
1847 void ElementsRemoved(int n) { SetNumberOfElements(NumberOfElements() - n); }
1848
1849 // Returns a new array for dictionary usage. Might return Failure.
1850 static Object* Allocate(int at_least_space_for);
1851
1852 // Returns the key at entry.
1853 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
1854
ager@chromium.org32912102009-01-16 10:38:43 +00001855 // Tells whether k is a real key. Null and undefined are not allowed
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 // as keys and can be used to indicate missing or deleted elements.
1857 bool IsKey(Object* k) {
1858 return !k->IsNull() && !k->IsUndefined();
1859 }
1860
1861 // Garbage collection support.
1862 void IteratePrefix(ObjectVisitor* visitor);
1863 void IterateElements(ObjectVisitor* visitor);
1864
1865 // Casting.
1866 static inline HashTable* cast(Object* obj);
1867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 // Compute the probe offset (quadratic probing).
1869 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
1870 return (n + n * n) >> 1;
1871 }
1872
1873 static const int kNumberOfElementsIndex = 0;
1874 static const int kCapacityIndex = 1;
1875 static const int kPrefixStartIndex = 2;
1876 static const int kElementsStartIndex = kPrefixStartIndex + prefix_size;
1877 static const int kElementSize = element_size;
1878 static const int kElementsStartOffset =
1879 kHeaderSize + kElementsStartIndex * kPointerSize;
1880
1881 protected:
1882 // Find entry for key otherwise return -1.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001883 int FindEntry(HashTableKey* key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
1885 // Find the entry at which to insert element with the given key that
1886 // has the given hash value.
1887 uint32_t FindInsertionEntry(Object* key, uint32_t hash);
1888
1889 // Returns the index for an entry (of the key)
1890 static inline int EntryToIndex(int entry) {
1891 return (entry * kElementSize) + kElementsStartIndex;
1892 }
1893
1894 // Update the number of elements in the dictionary.
1895 void SetNumberOfElements(int nof) {
1896 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
1897 }
1898
1899 // Sets the capacity of the hash table.
1900 void SetCapacity(int capacity) {
1901 // To scale a computed hash code to fit within the hash table, we
1902 // use bit-wise AND with a mask, so the capacity must be positive
1903 // and non-zero.
1904 ASSERT(capacity > 0);
1905 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
1906 }
1907
1908
1909 // Returns probe entry.
1910 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
1911 ASSERT(IsPowerOf2(size));
1912 return (hash + GetProbeOffset(number)) & (size - 1);
1913 }
1914
1915 // Ensure enough space for n additional elements.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001916 Object* EnsureCapacity(int n, HashTableKey* key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917};
1918
1919
1920// SymbolTable.
1921//
1922// No special elements in the prefix and the element size is 1
1923// because only the symbol itself (the key) needs to be stored.
1924class SymbolTable: public HashTable<0, 1> {
1925 public:
1926 // Find symbol in the symbol table. If it is not there yet, it is
1927 // added. The return value is the symbol table which might have
1928 // been enlarged. If the return value is not a failure, the symbol
1929 // pointer *s is set to the symbol found.
1930 Object* LookupSymbol(Vector<const char> str, Object** s);
1931 Object* LookupString(String* key, Object** s);
1932
ager@chromium.org7c537e22008-10-16 08:43:32 +00001933 // Looks up a symbol that is equal to the given string and returns
1934 // true if it is found, assigning the symbol to the given output
1935 // parameter.
1936 bool LookupSymbolIfExists(String* str, String** symbol);
1937
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 // Casting.
1939 static inline SymbolTable* cast(Object* obj);
1940
1941 private:
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001942 Object* LookupKey(HashTableKey* key, Object** s);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943
1944 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
1945};
1946
1947
ager@chromium.org236ad962008-09-25 09:45:57 +00001948// MapCache.
1949//
1950// Maps keys that are a fixed array of symbols to a map.
1951// Used for canonicalize maps for object literals.
1952class MapCache: public HashTable<0, 2> {
1953 public:
1954 // Find cached value for a string key, otherwise return null.
1955 Object* Lookup(FixedArray* key);
1956 Object* Put(FixedArray* key, Map* value);
1957 static inline MapCache* cast(Object* obj);
1958
1959 private:
1960 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
1961};
1962
1963
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001964// LookupCache.
1965//
1966// Maps a key consisting of a map and a name to an index within a
1967// fast-case properties array.
1968//
1969// LookupCaches are used to avoid repeatedly searching instance
1970// descriptors.
1971class LookupCache: public HashTable<0, 2> {
1972 public:
1973 int Lookup(Map* map, String* name);
1974 Object* Put(Map* map, String* name, int offset);
1975 static inline LookupCache* cast(Object* obj);
1976
1977 // Constant returned by Lookup when the key was not found.
1978 static const int kNotFound = -1;
1979
1980 private:
1981 DISALLOW_IMPLICIT_CONSTRUCTORS(LookupCache);
1982};
1983
1984
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985// Dictionary for keeping properties and elements in slow case.
1986//
1987// One element in the prefix is used for storing non-element
1988// information about the dictionary.
1989//
1990// The rest of the array embeds triples of (key, value, details).
1991// if key == undefined the triple is empty.
1992// if key == null the triple has been deleted.
1993// otherwise key contains the name of a property.
1994class DictionaryBase: public HashTable<2, 3> {};
1995
1996class Dictionary: public DictionaryBase {
1997 public:
1998 // Returns the value at entry.
1999 Object* ValueAt(int entry) { return get(EntryToIndex(entry)+1); }
2000
2001 // Set the value for entry.
2002 void ValueAtPut(int entry, Object* value) {
2003 set(EntryToIndex(entry)+1, value);
2004 }
2005
2006 // Returns the property details for the property at entry.
2007 PropertyDetails DetailsAt(int entry) {
2008 return PropertyDetails(Smi::cast(get(EntryToIndex(entry) + 2)));
2009 }
2010
2011 // Set the details for entry.
2012 void DetailsAtPut(int entry, PropertyDetails value) {
2013 set(EntryToIndex(entry) + 2, value.AsSmi());
2014 }
2015
2016 // Remove all entries were key is a number and (from <= key && key < to).
2017 void RemoveNumberEntries(uint32_t from, uint32_t to);
2018
2019 // Sorting support
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 void CopyValuesTo(FixedArray* elements);
2021
2022 // Casting.
2023 static inline Dictionary* cast(Object* obj);
2024
2025 // Find entry for string key otherwise return -1.
2026 int FindStringEntry(String* key);
2027
2028 // Find entry for number key otherwise return -1.
2029 int FindNumberEntry(uint32_t index);
2030
2031 // Delete a property from the dictionary.
2032 Object* DeleteProperty(int entry);
2033
2034 // Type specific at put (default NONE attributes is used when adding).
2035 Object* AtStringPut(String* key, Object* value);
2036 Object* AtNumberPut(uint32_t key, Object* value);
2037
2038 Object* AddStringEntry(String* key, Object* value, PropertyDetails details);
2039 Object* AddNumberEntry(uint32_t key, Object* value, PropertyDetails details);
2040
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002041 // Set an existing entry or add a new one if needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 Object* SetOrAddStringEntry(String* key,
2043 Object* value,
2044 PropertyDetails details);
2045
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002046 Object* SetOrAddNumberEntry(uint32_t key,
2047 Object* value,
2048 PropertyDetails details);
2049
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 // Returns the number of elements in the dictionary filtering out properties
2051 // with the specified attributes.
2052 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2053
2054 // Returns the number of enumerable elements in the dictionary.
2055 int NumberOfEnumElements();
2056
2057 // Copies keys to preallocated fixed array.
2058 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2059 // Copies enumerable keys to preallocated fixed array.
2060 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2061 // Fill in details for properties into storage.
2062 void CopyKeysTo(FixedArray* storage);
2063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 // For transforming properties of a JSObject.
2065 Object* TransformPropertiesToFastFor(JSObject* obj,
2066 int unused_property_fields);
2067
2068 // If slow elements are required we will never go back to fast-case
2069 // for the elements kept in this dictionary. We require slow
2070 // elements if an element has been added at an index larger than
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002071 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2072 // when defining a getter or setter with a number key.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 inline bool requires_slow_elements();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002074 inline void set_requires_slow_elements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075
2076 // Get the value of the max number key that has been added to this
2077 // dictionary. max_number_key can only be called if
2078 // requires_slow_elements returns false.
2079 inline uint32_t max_number_key();
2080
2081 // Accessors for next enumeration index.
2082 void SetNextEnumerationIndex(int index) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002083 fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 }
2085
2086 int NextEnumerationIndex() {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002087 return Smi::cast(get(kNextEnumerationIndexIndex))->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 }
2089
2090 // Returns a new array for dictionary usage. Might return Failure.
2091 static Object* Allocate(int at_least_space_for);
2092
2093 // Ensure enough space for n additional elements.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002094 Object* EnsureCapacity(int n, HashTableKey* key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095
2096#ifdef DEBUG
2097 void Print();
2098#endif
2099 // Returns the key (slow).
2100 Object* SlowReverseLookup(Object* value);
2101
2102 // Bit masks.
2103 static const int kRequiresSlowElementsMask = 1;
2104 static const int kRequiresSlowElementsTagSize = 1;
2105 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2106
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002107 void UpdateMaxNumberKey(uint32_t key);
2108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 private:
2110 // Generic at put operation.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002111 Object* AtPut(HashTableKey* key, Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002112
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002113 Object* Add(HashTableKey* key, Object* value, PropertyDetails details);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114
2115 // Add entry to dictionary.
2116 void AddEntry(Object* key,
2117 Object* value,
2118 PropertyDetails details,
2119 uint32_t hash);
2120
2121 // Sets the entry to (key, value) pair.
2122 inline void SetEntry(int entry,
2123 Object* key,
2124 Object* value,
2125 PropertyDetails details);
2126
ager@chromium.org32912102009-01-16 10:38:43 +00002127 // Generate new enumeration indices to avoid enumeration index overflow.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002128 Object* GenerateNewEnumerationIndices();
2129
2130 static const int kMaxNumberKeyIndex = kPrefixStartIndex;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002131 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 DISALLOW_IMPLICIT_CONSTRUCTORS(Dictionary);
2134};
2135
2136
2137// ByteArray represents fixed sized byte arrays. Used by the outside world,
2138// such as PCRE, and also by the memory allocator and garbage collector to
2139// fill in free blocks in the heap.
2140class ByteArray: public Array {
2141 public:
2142 // Setter and getter.
2143 inline byte get(int index);
2144 inline void set(int index, byte value);
2145
2146 // Treat contents as an int array.
2147 inline int get_int(int index);
2148
2149 static int SizeFor(int length) {
2150 return kHeaderSize + OBJECT_SIZE_ALIGN(length);
2151 }
2152 // We use byte arrays for free blocks in the heap. Given a desired size in
2153 // bytes that is a multiple of the word size and big enough to hold a byte
2154 // array, this function returns the number of elements a byte array should
2155 // have.
2156 static int LengthFor(int size_in_bytes) {
2157 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2158 ASSERT(size_in_bytes >= kHeaderSize);
2159 return size_in_bytes - kHeaderSize;
2160 }
2161
2162 // Returns data start address.
2163 inline Address GetDataStartAddress();
2164
2165 // Returns a pointer to the ByteArray object for a given data start address.
2166 static inline ByteArray* FromDataStartAddress(Address address);
2167
2168 // Casting.
2169 static inline ByteArray* cast(Object* obj);
2170
2171 // Dispatched behavior.
2172 int ByteArraySize() { return SizeFor(length()); }
2173#ifdef DEBUG
2174 void ByteArrayPrint();
2175 void ByteArrayVerify();
2176#endif
2177
2178 private:
2179 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2180};
2181
2182
2183// Code describes objects with on-the-fly generated machine code.
2184class Code: public HeapObject {
2185 public:
2186 // Opaque data type for encapsulating code flags like kind, inline
2187 // cache state, and arguments count.
2188 enum Flags { };
2189
2190 enum Kind {
2191 FUNCTION,
2192 STUB,
2193 BUILTIN,
2194 LOAD_IC,
2195 KEYED_LOAD_IC,
2196 CALL_IC,
2197 STORE_IC,
2198 KEYED_STORE_IC,
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002199 // No more than eight kinds. The value currently encoded in three bits in
2200 // Flags.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201
2202 // Pseudo-kinds.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002203 REGEXP = BUILTIN,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204 FIRST_IC_KIND = LOAD_IC,
2205 LAST_IC_KIND = KEYED_STORE_IC
2206 };
2207
2208 enum {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002209 NUMBER_OF_KINDS = KEYED_STORE_IC + 1
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 };
2211
2212 // A state indicates that inline cache in this Code object contains
2213 // objects or relative instruction addresses.
2214 enum ICTargetState {
2215 IC_TARGET_IS_ADDRESS,
2216 IC_TARGET_IS_OBJECT
2217 };
2218
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002219#ifdef ENABLE_DISASSEMBLER
2220 // Printing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 static const char* Kind2String(Kind kind);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002222 static const char* ICState2String(InlineCacheState state);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002223 void Disassemble(const char* name);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002224#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225
2226 // [instruction_size]: Size of the native instructions
2227 inline int instruction_size();
2228 inline void set_instruction_size(int value);
2229
2230 // [relocation_size]: Size of relocation information.
2231 inline int relocation_size();
2232 inline void set_relocation_size(int value);
2233
2234 // [sinfo_size]: Size of scope information.
2235 inline int sinfo_size();
2236 inline void set_sinfo_size(int value);
2237
2238 // [flags]: Various code flags.
2239 inline Flags flags();
2240 inline void set_flags(Flags flags);
2241
2242 // [flags]: Access to specific code flags.
2243 inline Kind kind();
kasper.lund7276f142008-07-30 08:49:36 +00002244 inline InlineCacheState ic_state(); // only valid for IC stubs
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 inline PropertyType type(); // only valid for monomorphic IC stubs
2246 inline int arguments_count(); // only valid for call IC stubs
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247
2248 // Testers for IC stub kinds.
2249 inline bool is_inline_cache_stub();
2250 inline bool is_load_stub() { return kind() == LOAD_IC; }
2251 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
2252 inline bool is_store_stub() { return kind() == STORE_IC; }
2253 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
2254 inline bool is_call_stub() { return kind() == CALL_IC; }
2255
2256 // [ic_flag]: State of inline cache targets. The flag is set to the
2257 // object variant in ConvertICTargetsFromAddressToObject, and set to
2258 // the address variant in ConvertICTargetsFromObjectToAddress.
2259 inline ICTargetState ic_flag();
2260 inline void set_ic_flag(ICTargetState value);
2261
kasper.lund7276f142008-07-30 08:49:36 +00002262 // [major_key]: For kind STUB, the major key.
2263 inline CodeStub::Major major_key();
2264 inline void set_major_key(CodeStub::Major major);
2265
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 // Flags operations.
2267 static inline Flags ComputeFlags(Kind kind,
kasper.lund7276f142008-07-30 08:49:36 +00002268 InlineCacheState ic_state = UNINITIALIZED,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 PropertyType type = NORMAL,
2270 int argc = -1);
2271
2272 static inline Flags ComputeMonomorphicFlags(Kind kind,
2273 PropertyType type,
2274 int argc = -1);
2275
2276 static inline Kind ExtractKindFromFlags(Flags flags);
kasper.lund7276f142008-07-30 08:49:36 +00002277 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002278 static inline PropertyType ExtractTypeFromFlags(Flags flags);
2279 static inline int ExtractArgumentsCountFromFlags(Flags flags);
2280 static inline Flags RemoveTypeFromFlags(Flags flags);
2281
ager@chromium.org8bb60582008-12-11 12:02:20 +00002282 // Convert a target address into a code object.
2283 static inline Code* GetCodeFromTargetAddress(Address address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284
2285 // Returns the address of the first instruction.
2286 inline byte* instruction_start();
2287
2288 // Returns the size of the instructions, padding, and relocation information.
2289 inline int body_size();
2290
2291 // Returns the address of the first relocation info (read backwards!).
2292 inline byte* relocation_start();
2293
2294 // Code entry point.
2295 inline byte* entry();
2296
2297 // Returns true if pc is inside this object's instructions.
2298 inline bool contains(byte* pc);
2299
ager@chromium.org32912102009-01-16 10:38:43 +00002300 // Returns the address of the scope information.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 inline byte* sinfo_start();
2302
2303 // Convert inline cache target from address to code object before GC.
2304 void ConvertICTargetsFromAddressToObject();
2305
2306 // Convert inline cache target from code object to address after GC
2307 void ConvertICTargetsFromObjectToAddress();
2308
2309 // Relocate the code by delta bytes. Called to signal that this code
2310 // object has been moved by delta bytes.
2311 void Relocate(int delta);
2312
2313 // Migrate code described by desc.
2314 void CopyFrom(const CodeDesc& desc);
2315
2316 // Returns the object size for a given body and sinfo size (Used for
2317 // allocation).
2318 static int SizeFor(int body_size, int sinfo_size) {
2319 ASSERT_SIZE_TAG_ALIGNED(body_size);
2320 ASSERT_SIZE_TAG_ALIGNED(sinfo_size);
kasperl@chromium.org061ef742009-02-27 12:16:20 +00002321 return RoundUp(kHeaderSize + body_size + sinfo_size, kCodeAlignment);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 }
2323
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002324 // Calculate the size of the code object to report for log events. This takes
2325 // the layout of the code object into account.
2326 int ExecutableSize() {
2327 // Check that the assumptions about the layout of the code object holds.
ager@chromium.org5ec48922009-05-05 07:25:34 +00002328 ASSERT_EQ(instruction_start() - address(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002329 Code::kHeaderSize);
2330 return instruction_size() + Code::kHeaderSize;
2331 }
2332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333 // Locating source position.
2334 int SourcePosition(Address pc);
2335 int SourceStatementPosition(Address pc);
2336
2337 // Casting.
2338 static inline Code* cast(Object* obj);
2339
2340 // Dispatched behavior.
2341 int CodeSize() { return SizeFor(body_size(), sinfo_size()); }
2342 void CodeIterateBody(ObjectVisitor* v);
2343#ifdef DEBUG
2344 void CodePrint();
2345 void CodeVerify();
2346#endif
2347
2348 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00002349 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 static const int kRelocationSizeOffset = kInstructionSizeOffset + kIntSize;
2351 static const int kSInfoSizeOffset = kRelocationSizeOffset + kIntSize;
2352 static const int kFlagsOffset = kSInfoSizeOffset + kIntSize;
kasper.lund7276f142008-07-30 08:49:36 +00002353 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
kasperl@chromium.org061ef742009-02-27 12:16:20 +00002354 // Add filler objects to align the instruction start following right after
2355 // the Code object header.
2356 static const int kFiller6Offset = kKindSpecificFlagsOffset + kIntSize;
2357 static const int kFiller7Offset = kFiller6Offset + kIntSize;
2358 static const int kHeaderSize = kFiller7Offset + kIntSize;
2359
2360 // Code entry points are aligned to 32 bytes.
2361 static const int kCodeAlignment = 32;
kasper.lund7276f142008-07-30 08:49:36 +00002362
2363 // Byte offsets within kKindSpecificFlagsOffset.
2364 static const int kICFlagOffset = kKindSpecificFlagsOffset + 0;
2365 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset + 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366
2367 // Flags layout.
kasper.lund7276f142008-07-30 08:49:36 +00002368 static const int kFlagsICStateShift = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002369 static const int kFlagsKindShift = 3;
2370 static const int kFlagsTypeShift = 6;
2371 static const int kFlagsArgumentsCountShift = 9;
2372
kasper.lund7276f142008-07-30 08:49:36 +00002373 static const int kFlagsICStateMask = 0x00000007; // 000000111
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 static const int kFlagsKindMask = 0x00000038; // 000111000
2375 static const int kFlagsTypeMask = 0x000001C0; // 111000000
2376 static const int kFlagsArgumentsCountMask = 0xFFFFFE00;
2377
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378 private:
2379 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
2380};
2381
2382
2383// All heap objects have a Map that describes their structure.
2384// A Map contains information about:
2385// - Size information about the object
2386// - How to iterate over an object (for garbage collection)
2387class Map: public HeapObject {
2388 public:
ager@chromium.org32912102009-01-16 10:38:43 +00002389 // Instance size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390 inline int instance_size();
2391 inline void set_instance_size(int value);
2392
ager@chromium.org7c537e22008-10-16 08:43:32 +00002393 // Count of properties allocated in the object.
2394 inline int inobject_properties();
2395 inline void set_inobject_properties(int value);
2396
ager@chromium.org32912102009-01-16 10:38:43 +00002397 // Instance type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398 inline InstanceType instance_type();
2399 inline void set_instance_type(InstanceType value);
2400
ager@chromium.org32912102009-01-16 10:38:43 +00002401 // Tells how many unused property fields are available in the
2402 // instance (only used for JSObject in fast mode).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 inline int unused_property_fields();
2404 inline void set_unused_property_fields(int value);
2405
ager@chromium.org32912102009-01-16 10:38:43 +00002406 // Bit field.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002407 inline byte bit_field();
2408 inline void set_bit_field(byte value);
2409
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002410 // Bit field 2.
2411 inline byte bit_field2();
2412 inline void set_bit_field2(byte value);
2413
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 // Tells whether the object in the prototype property will be used
2415 // for instances created from this function. If the prototype
2416 // property is set to a value that is not a JSObject, the prototype
2417 // property will not be used to create instances of the function.
2418 // See ECMA-262, 13.2.2.
2419 inline void set_non_instance_prototype(bool value);
2420 inline bool has_non_instance_prototype();
2421
2422 // Tells whether the instance with this map should be ignored by the
2423 // __proto__ accessor.
2424 inline void set_is_hidden_prototype() {
2425 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
2426 }
2427
2428 inline bool is_hidden_prototype() {
2429 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
2430 }
2431
2432 // Tells whether the instance has a named interceptor.
2433 inline void set_has_named_interceptor() {
2434 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
2435 }
2436
2437 inline bool has_named_interceptor() {
2438 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
2439 }
2440
2441 // Tells whether the instance has a named interceptor.
2442 inline void set_has_indexed_interceptor() {
2443 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
2444 }
2445
2446 inline bool has_indexed_interceptor() {
2447 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
2448 }
2449
2450 // Tells whether the instance is undetectable.
2451 // An undetectable object is a special class of JSObject: 'typeof' operator
2452 // returns undefined, ToBoolean returns false. Otherwise it behaves like
2453 // a normal JS object. It is useful for implementing undetectable
2454 // document.all in Firefox & Safari.
2455 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
2456 inline void set_is_undetectable() {
2457 set_bit_field(bit_field() | (1 << kIsUndetectable));
2458 }
2459
2460 inline bool is_undetectable() {
2461 return ((1 << kIsUndetectable) & bit_field()) != 0;
2462 }
2463
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002464 inline void set_needs_loading(bool value) {
2465 if (value) {
2466 set_bit_field2(bit_field2() | (1 << kNeedsLoading));
2467 } else {
2468 set_bit_field2(bit_field2() & ~(1 << kNeedsLoading));
2469 }
2470 }
2471
2472 // Does this object or function require a lazily loaded script to be
2473 // run before being used?
2474 inline bool needs_loading() {
2475 return ((1 << kNeedsLoading) & bit_field2()) != 0;
2476 }
2477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 // Tells whether the instance has a call-as-function handler.
2479 inline void set_has_instance_call_handler() {
2480 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
2481 }
2482
2483 inline bool has_instance_call_handler() {
2484 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
2485 }
2486
2487 // Tells whether the instance needs security checks when accessing its
2488 // properties.
ager@chromium.org870a0b62008-11-04 11:43:05 +00002489 inline void set_is_access_check_needed(bool access_check_needed);
2490 inline bool is_access_check_needed();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491
2492 // [prototype]: implicit prototype object.
2493 DECL_ACCESSORS(prototype, Object)
2494
2495 // [constructor]: points back to the function responsible for this map.
2496 DECL_ACCESSORS(constructor, Object)
2497
2498 // [instance descriptors]: describes the object.
2499 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
2500
2501 // [stub cache]: contains stubs compiled for this map.
2502 DECL_ACCESSORS(code_cache, FixedArray)
2503
2504 // Returns a copy of the map.
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002505 Object* CopyDropDescriptors();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002507 // Returns a copy of the map, with all transitions dropped from the
2508 // instance descriptors.
2509 Object* CopyDropTransitions();
2510
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511 // Returns the property index for name (only valid for FAST MODE).
2512 int PropertyIndexFor(String* name);
2513
2514 // Returns the next free property index (only valid for FAST MODE).
2515 int NextFreePropertyIndex();
2516
2517 // Returns the number of properties described in instance_descriptors.
2518 int NumberOfDescribedProperties();
2519
2520 // Casting.
2521 static inline Map* cast(Object* obj);
2522
2523 // Locate an accessor in the instance descriptor.
2524 AccessorDescriptor* FindAccessor(String* name);
2525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002526 // Code cache operations.
2527
2528 // Clears the code cache.
2529 inline void ClearCodeCache();
2530
2531 // Update code cache.
2532 Object* UpdateCodeCache(String* name, Code* code);
2533
2534 // Returns the found code or undefined if absent.
2535 Object* FindInCodeCache(String* name, Code::Flags flags);
2536
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002537 // Returns the non-negative index of the code object if it is in the
2538 // cache and -1 otherwise.
2539 int IndexInCodeCache(Code* code);
2540
2541 // Removes a code object from the code cache at the given index.
2542 void RemoveFromCodeCache(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002544 // For every transition in this map, makes the transition's
2545 // target's prototype pointer point back to this map.
2546 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
2547 void CreateBackPointers();
2548
2549 // Set all map transitions from this map to dead maps to null.
2550 // Also, restore the original prototype on the targets of these
2551 // transitions, so that we do not process this map again while
2552 // following back pointers.
2553 void ClearNonLiveTransitions(Object* real_prototype);
2554
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002555 // Dispatched behavior.
2556 void MapIterateBody(ObjectVisitor* v);
2557#ifdef DEBUG
2558 void MapPrint();
2559 void MapVerify();
2560#endif
2561
2562 // Layout description.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002563 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
2564 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
2566 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
2567 static const int kInstanceDescriptorsOffset =
2568 kConstructorOffset + kPointerSize;
2569 static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
2570 static const int kSize = kCodeCacheOffset + kIntSize;
2571
ager@chromium.org7c537e22008-10-16 08:43:32 +00002572 // Byte offsets within kInstanceSizesOffset.
2573 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
2574 static const int kInObjectPropertiesOffset = kInstanceSizesOffset + 1;
2575 // The bytes at positions 2 and 3 are not in use at the moment.
2576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 // Byte offsets within kInstanceAttributesOffset attributes.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002578 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
2579 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
2580 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002581 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582
2583 // Bit positions for bit field.
mads.s.ager31e71382008-08-13 09:32:07 +00002584 static const int kUnused = 0; // To be used for marking recently used maps.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002585 static const int kHasNonInstancePrototype = 1;
2586 static const int kIsHiddenPrototype = 2;
2587 static const int kHasNamedInterceptor = 3;
2588 static const int kHasIndexedInterceptor = 4;
2589 static const int kIsUndetectable = 5;
2590 static const int kHasInstanceCallHandler = 6;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002591 static const int kIsAccessCheckNeeded = 7;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002592
2593 // Bit positions for but field 2
2594 static const int kNeedsLoading = 0;
2595
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 private:
2597 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
2598};
2599
2600
2601// An abstract superclass, a marker class really, for simple structure classes.
2602// It doesn't carry much functionality but allows struct classes to me
2603// identified in the type system.
2604class Struct: public HeapObject {
2605 public:
2606 inline void InitializeBody(int object_size);
2607 static inline Struct* cast(Object* that);
2608};
2609
2610
2611// Script types.
2612enum ScriptType {
2613 SCRIPT_TYPE_NATIVE,
2614 SCRIPT_TYPE_EXTENSION,
2615 SCRIPT_TYPE_NORMAL
2616};
2617
2618
mads.s.ager31e71382008-08-13 09:32:07 +00002619// Script describes a script which has been added to the VM.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620class Script: public Struct {
2621 public:
2622 // [source]: the script source.
2623 DECL_ACCESSORS(source, Object)
2624
2625 // [name]: the script name.
2626 DECL_ACCESSORS(name, Object)
2627
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002628 // [id]: the script id.
2629 DECL_ACCESSORS(id, Object)
2630
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631 // [line_offset]: script line offset in resource from where it was extracted.
2632 DECL_ACCESSORS(line_offset, Smi)
2633
2634 // [column_offset]: script column offset in resource from where it was
2635 // extracted.
2636 DECL_ACCESSORS(column_offset, Smi)
2637
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002638 // [data]: additional data associated with this script.
2639 DECL_ACCESSORS(data, Object)
2640
ager@chromium.org9085a012009-05-11 19:22:57 +00002641 // [context_data]: context data for the context this script was compiled in.
2642 DECL_ACCESSORS(context_data, Object)
2643
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 // [wrapper]: the wrapper cache.
2645 DECL_ACCESSORS(wrapper, Proxy)
2646
2647 // [type]: the script type.
2648 DECL_ACCESSORS(type, Smi)
2649
iposva@chromium.org245aa852009-02-10 00:49:54 +00002650 // [line_ends]: array of line ends positions
2651 DECL_ACCESSORS(line_ends, Object)
2652
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 static inline Script* cast(Object* obj);
2654
2655#ifdef DEBUG
2656 void ScriptPrint();
2657 void ScriptVerify();
2658#endif
2659
ager@chromium.org236ad962008-09-25 09:45:57 +00002660 static const int kSourceOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 static const int kNameOffset = kSourceOffset + kPointerSize;
2662 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
2663 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002664 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
ager@chromium.org9085a012009-05-11 19:22:57 +00002665 static const int kContextOffset = kDataOffset + kPointerSize;
2666 static const int kWrapperOffset = kContextOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002667 static const int kTypeOffset = kWrapperOffset + kPointerSize;
iposva@chromium.org245aa852009-02-10 00:49:54 +00002668 static const int kLineEndsOffset = kTypeOffset + kPointerSize;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002669 static const int kIdOffset = kLineEndsOffset + kPointerSize;
2670 static const int kSize = kIdOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671
2672 private:
2673 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
2674};
2675
2676
2677// SharedFunctionInfo describes the JSFunction information that can be
2678// shared by multiple instances of the function.
2679class SharedFunctionInfo: public HeapObject {
2680 public:
2681 // [name]: Function name.
2682 DECL_ACCESSORS(name, Object)
2683
2684 // [code]: Function code.
2685 DECL_ACCESSORS(code, Code)
2686
2687 // Returns if this function has been compiled to native code yet.
2688 inline bool is_compiled();
2689
2690 // [length]: The function length - usually the number of declared parameters.
2691 // Use up to 2^30 parameters.
2692 inline int length();
2693 inline void set_length(int value);
2694
2695 // [formal parameter count]: The declared number of parameters.
2696 inline int formal_parameter_count();
2697 inline void set_formal_parameter_count(int value);
2698
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002699 // Set the formal parameter count so the function code will be
2700 // called without using argument adaptor frames.
2701 inline void DontAdaptArguments();
2702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 // [expected_nof_properties]: Expected number of properties for the function.
2704 inline int expected_nof_properties();
2705 inline void set_expected_nof_properties(int value);
2706
2707 // [instance class name]: class name for instances.
2708 DECL_ACCESSORS(instance_class_name, Object)
2709
2710 // [function data]: This field has been added for make benefit the API.
2711 // In the long run we don't want all functions to have this field but
2712 // we can fix that when we have a better model for storing hidden data
2713 // on objects.
2714 DECL_ACCESSORS(function_data, Object)
2715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 // [script info]: Script from which the function originates.
2717 DECL_ACCESSORS(script, Object)
2718
2719 // [start_position_and_type]: Field used to store both the source code
2720 // position, whether or not the function is a function expression,
2721 // and whether or not the function is a toplevel function. The two
2722 // least significants bit indicates whether the function is an
2723 // expression and the rest contains the source code position.
2724 inline int start_position_and_type();
2725 inline void set_start_position_and_type(int value);
2726
2727 // [debug info]: Debug information.
2728 DECL_ACCESSORS(debug_info, Object)
2729
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00002730 // [inferred name]: Name inferred from variable or property
2731 // assignment of this function. Used to facilitate debugging and
2732 // profiling of JavaScript code written in OO style, where almost
2733 // all functions are anonymous but are assigned to object
2734 // properties.
2735 DECL_ACCESSORS(inferred_name, String)
2736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737 // Position of the 'function' token in the script source.
2738 inline int function_token_position();
2739 inline void set_function_token_position(int function_token_position);
2740
2741 // Position of this function in the script source.
2742 inline int start_position();
2743 inline void set_start_position(int start_position);
2744
2745 // End position of this function in the script source.
2746 inline int end_position();
2747 inline void set_end_position(int end_position);
2748
2749 // Is this function a function expression in the source code.
2750 inline bool is_expression();
2751 inline void set_is_expression(bool value);
2752
2753 // Is this function a top-level function. Used for accessing the
2754 // caller of functions. Top-level functions (scripts, evals) are
2755 // returned as null; see JSFunction::GetCallerAccessor(...).
2756 inline bool is_toplevel();
2757 inline void set_is_toplevel(bool value);
2758
2759 // [source code]: Source code for the function.
2760 bool HasSourceCode();
2761 Object* GetSourceCode();
2762
2763 // Dispatched behavior.
2764 void SharedFunctionInfoIterateBody(ObjectVisitor* v);
2765 // Set max_length to -1 for unlimited length.
2766 void SourceCodePrint(StringStream* accumulator, int max_length);
2767#ifdef DEBUG
2768 void SharedFunctionInfoPrint();
2769 void SharedFunctionInfoVerify();
2770#endif
2771
2772 // Casting.
2773 static inline SharedFunctionInfo* cast(Object* obj);
2774
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002775 // Constants.
2776 static const int kDontAdaptArgumentsSentinel = -1;
2777
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00002779 static const int kNameOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002780 static const int kCodeOffset = kNameOffset + kPointerSize;
2781 static const int kLengthOffset = kCodeOffset + kPointerSize;
2782 static const int kFormalParameterCountOffset = kLengthOffset + kIntSize;
2783 static const int kExpectedNofPropertiesOffset =
2784 kFormalParameterCountOffset + kIntSize;
2785 static const int kInstanceClassNameOffset =
2786 kExpectedNofPropertiesOffset + kIntSize;
2787 static const int kExternalReferenceDataOffset =
2788 kInstanceClassNameOffset + kPointerSize;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00002789 static const int kScriptOffset = kExternalReferenceDataOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002790 static const int kStartPositionAndTypeOffset = kScriptOffset + kPointerSize;
2791 static const int kEndPositionOffset = kStartPositionAndTypeOffset + kIntSize;
2792 static const int kFunctionTokenPositionOffset = kEndPositionOffset + kIntSize;
2793 static const int kDebugInfoOffset = kFunctionTokenPositionOffset + kIntSize;
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00002794 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
2795 static const int kSize = kInferredNameOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002796
2797 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002798 // Bit positions in length_and_flg.
2799 // The least significant bit is used as the flag.
2800 static const int kFlagBit = 0;
2801 static const int kLengthShift = 1;
2802 static const int kLengthMask = ~((1 << kLengthShift) - 1);
2803
2804 // Bit positions in start_position_and_type.
2805 // The source code start position is in the 30 most significant bits of
2806 // the start_position_and_type field.
2807 static const int kIsExpressionBit = 0;
2808 static const int kIsTopLevelBit = 1;
2809 static const int kStartPositionShift = 2;
2810 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
mads.s.ager31e71382008-08-13 09:32:07 +00002811
2812 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002813};
2814
2815
2816// JSFunction describes JavaScript functions.
2817class JSFunction: public JSObject {
2818 public:
2819 // [prototype_or_initial_map]:
2820 DECL_ACCESSORS(prototype_or_initial_map, Object)
2821
2822 // [shared_function_info]: The information about the function that
2823 // can be shared by instances.
2824 DECL_ACCESSORS(shared, SharedFunctionInfo)
2825
2826 // [context]: The context for this function.
2827 inline Context* context();
2828 inline Object* unchecked_context();
2829 inline void set_context(Object* context);
2830
2831 // [code]: The generated code object for this function. Executed
2832 // when the function is invoked, e.g. foo() or new foo(). See
2833 // [[Call]] and [[Construct]] description in ECMA-262, section
2834 // 8.6.2, page 27.
2835 inline Code* code();
2836 inline void set_code(Code* value);
2837
2838 // Tells whether this function is a context-independent boilerplate
2839 // function.
2840 inline bool IsBoilerplate();
2841
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002842 // [literals]: Fixed array holding the materialized literals.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002843 //
2844 // If the function contains object, regexp or array literals, the
2845 // literals array prefix contains the object, regexp, and array
2846 // function to be used when creating these literals. This is
2847 // necessary so that we do not dynamically lookup the object, regexp
2848 // or array functions. Performing a dynamic lookup, we might end up
2849 // using the functions from a new context that we should not have
2850 // access to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002851 DECL_ACCESSORS(literals, FixedArray)
2852
2853 // The initial map for an object created by this constructor.
2854 inline Map* initial_map();
2855 inline void set_initial_map(Map* value);
2856 inline bool has_initial_map();
2857
2858 // Get and set the prototype property on a JSFunction. If the
2859 // function has an initial map the prototype is set on the initial
2860 // map. Otherwise, the prototype is put in the initial map field
2861 // until an initial map is needed.
2862 inline bool has_prototype();
2863 inline bool has_instance_prototype();
2864 inline Object* prototype();
2865 inline Object* instance_prototype();
2866 Object* SetInstancePrototype(Object* value);
2867 Object* SetPrototype(Object* value);
2868
2869 // Accessor for this function's initial map's [[class]]
2870 // property. This is primarily used by ECMA native functions. This
2871 // method sets the class_name field of this function's initial map
2872 // to a given value. It creates an initial map if this function does
2873 // not have one. Note that this method does not copy the initial map
2874 // if it has one already, but simply replaces it with the new value.
2875 // Instances created afterwards will have a map whose [[class]] is
2876 // set to 'value', but there is no guarantees on instances created
2877 // before.
2878 Object* SetInstanceClassName(String* name);
2879
2880 // Returns if this function has been compiled to native code yet.
2881 inline bool is_compiled();
2882
2883 // Casting.
2884 static inline JSFunction* cast(Object* obj);
2885
2886 // Dispatched behavior.
2887#ifdef DEBUG
2888 void JSFunctionPrint();
2889 void JSFunctionVerify();
2890#endif
2891
2892 // Returns the number of allocated literals.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002893 inline int NumberOfLiterals();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894
ager@chromium.org236ad962008-09-25 09:45:57 +00002895 // Retrieve the global context from a function's literal array.
2896 static Context* GlobalContextFromLiterals(FixedArray* literals);
2897
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898 // Layout descriptors.
2899 static const int kPrototypeOrInitialMapOffset = JSObject::kHeaderSize;
2900 static const int kSharedFunctionInfoOffset =
2901 kPrototypeOrInitialMapOffset + kPointerSize;
2902 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
2903 static const int kLiteralsOffset = kContextOffset + kPointerSize;
2904 static const int kSize = kLiteralsOffset + kPointerSize;
2905
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002906 // Layout of the literals array.
ager@chromium.org236ad962008-09-25 09:45:57 +00002907 static const int kLiteralsPrefixSize = 1;
2908 static const int kLiteralGlobalContextIndex = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909 private:
2910 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
2911};
2912
2913
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002914// JSGlobalProxy's prototype must be a JSGlobalObject or null,
2915// and the prototype is hidden. JSGlobalProxy always delegates
2916// property accesses to its prototype if the prototype is not null.
2917//
2918// A JSGlobalProxy can be reinitialized which will preserve its identity.
2919//
2920// Accessing a JSGlobalProxy requires security check.
2921
2922class JSGlobalProxy : public JSObject {
2923 public:
2924 // [context]: the owner global context of this proxy object.
2925 // It is null value if this object is not used by any context.
2926 DECL_ACCESSORS(context, Object)
2927
2928 // Casting.
2929 static inline JSGlobalProxy* cast(Object* obj);
2930
2931 // Dispatched behavior.
2932#ifdef DEBUG
2933 void JSGlobalProxyPrint();
2934 void JSGlobalProxyVerify();
2935#endif
2936
2937 // Layout description.
2938 static const int kContextOffset = JSObject::kHeaderSize;
2939 static const int kSize = kContextOffset + kPointerSize;
2940
2941 private:
2942
2943 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
2944};
2945
2946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947// Forward declaration.
2948class JSBuiltinsObject;
2949
2950// Common super class for JavaScript global objects and the special
2951// builtins global objects.
2952class GlobalObject: public JSObject {
2953 public:
2954 // [builtins]: the object holding the runtime routines written in JS.
2955 DECL_ACCESSORS(builtins, JSBuiltinsObject)
2956
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00002957 // [global context]: the global context corresponding to this global object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 DECL_ACCESSORS(global_context, Context)
2959
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002960 // [global receiver]: the global receiver object of the context
2961 DECL_ACCESSORS(global_receiver, JSObject)
2962
2963 // Casting.
2964 static inline GlobalObject* cast(Object* obj);
2965
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002966 // Layout description.
2967 static const int kBuiltinsOffset = JSObject::kHeaderSize;
2968 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002969 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
2970 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002971
2972 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
mads.s.ager31e71382008-08-13 09:32:07 +00002974
2975 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002976};
2977
2978
2979// JavaScript global object.
2980class JSGlobalObject: public GlobalObject {
2981 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 // Casting.
2983 static inline JSGlobalObject* cast(Object* obj);
2984
2985 // Dispatched behavior.
2986#ifdef DEBUG
2987 void JSGlobalObjectPrint();
2988 void JSGlobalObjectVerify();
2989#endif
2990
2991 // Layout description.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002992 static const int kSize = GlobalObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993
2994 private:
2995 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
2996};
2997
2998
2999// Builtins global object which holds the runtime routines written in
3000// JavaScript.
3001class JSBuiltinsObject: public GlobalObject {
3002 public:
3003 // Accessors for the runtime routines written in JavaScript.
3004 inline Object* javascript_builtin(Builtins::JavaScript id);
3005 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
3006
3007 // Casting.
3008 static inline JSBuiltinsObject* cast(Object* obj);
3009
3010 // Dispatched behavior.
3011#ifdef DEBUG
3012 void JSBuiltinsObjectPrint();
3013 void JSBuiltinsObjectVerify();
3014#endif
3015
3016 // Layout description. The size of the builtins object includes
3017 // room for one pointer per runtime routine written in javascript.
3018 static const int kJSBuiltinsCount = Builtins::id_count;
3019 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
3020 static const int kSize =
3021 kJSBuiltinsOffset + (kJSBuiltinsCount * kPointerSize);
3022 private:
3023 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
3024};
3025
3026
3027// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
3028class JSValue: public JSObject {
3029 public:
3030 // [value]: the object being wrapped.
3031 DECL_ACCESSORS(value, Object)
3032
3033 // Casting.
3034 static inline JSValue* cast(Object* obj);
3035
3036 // Dispatched behavior.
3037#ifdef DEBUG
3038 void JSValuePrint();
3039 void JSValueVerify();
3040#endif
3041
3042 // Layout description.
3043 static const int kValueOffset = JSObject::kHeaderSize;
3044 static const int kSize = kValueOffset + kPointerSize;
3045
3046 private:
3047 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
3048};
3049
ager@chromium.org236ad962008-09-25 09:45:57 +00003050// Regular expressions
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003051// The regular expression holds a single reference to a FixedArray in
3052// the kDataOffset field.
3053// The FixedArray contains the following data:
3054// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
3055// - reference to the original source string
3056// - reference to the original flag string
3057// If it is an atom regexp
3058// - a reference to a literal string to search for
3059// If it is an irregexp regexp:
3060// - a reference to code for ASCII inputs (bytecode or compiled).
3061// - a reference to code for UC16 inputs (bytecode or compiled).
3062// - max number of registers used by irregexp implementations.
3063// - number of capture registers (output values) of the regexp.
ager@chromium.org236ad962008-09-25 09:45:57 +00003064class JSRegExp: public JSObject {
3065 public:
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003066 // Meaning of Type:
3067 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003068 // ATOM: A simple string to match against using an indexOf operation.
3069 // IRREGEXP: Compiled with Irregexp.
3070 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
ager@chromium.org381abbb2009-02-25 13:23:22 +00003071 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003072 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
ager@chromium.org236ad962008-09-25 09:45:57 +00003073
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003074 class Flags {
3075 public:
3076 explicit Flags(uint32_t value) : value_(value) { }
3077 bool is_global() { return (value_ & GLOBAL) != 0; }
3078 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
3079 bool is_multiline() { return (value_ & MULTILINE) != 0; }
3080 uint32_t value() { return value_; }
3081 private:
3082 uint32_t value_;
3083 };
ager@chromium.org236ad962008-09-25 09:45:57 +00003084
ager@chromium.org236ad962008-09-25 09:45:57 +00003085 DECL_ACCESSORS(data, Object)
3086
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003087 inline Type TypeTag();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003088 inline int CaptureCount();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003089 inline Flags GetFlags();
3090 inline String* Pattern();
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003091 inline Object* DataAt(int index);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092 // Set implementation data after the object has been prepared.
3093 inline void SetDataAt(int index, Object* value);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003094
ager@chromium.org236ad962008-09-25 09:45:57 +00003095 static inline JSRegExp* cast(Object* obj);
3096
3097 // Dispatched behavior.
3098#ifdef DEBUG
ager@chromium.org236ad962008-09-25 09:45:57 +00003099 void JSRegExpVerify();
3100#endif
3101
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003102 static const int kDataOffset = JSObject::kHeaderSize;
ager@chromium.org236ad962008-09-25 09:45:57 +00003103 static const int kSize = kDataOffset + kIntSize;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003104
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003105 // Indices in the data array.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003106 static const int kTagIndex = 0;
3107 static const int kSourceIndex = kTagIndex + 1;
3108 static const int kFlagsIndex = kSourceIndex + 1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003109 static const int kDataIndex = kFlagsIndex + 1;
3110 // The data fields are used in different ways depending on the
3111 // value of the tag.
3112 // Atom regexps (literal strings).
3113 static const int kAtomPatternIndex = kDataIndex;
3114
3115 static const int kAtomDataSize = kAtomPatternIndex + 1;
3116
3117 // Irregexp compiled code or bytecode for ASCII.
3118 static const int kIrregexpASCIICodeIndex = kDataIndex;
3119 // Irregexp compiled code or bytecode for UC16.
3120 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
3121 // Maximal number of registers used by either ASCII or UC16.
3122 // Only used to check that there is enough stack space
3123 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
3124 // Number of captures in the compiled regexp.
3125 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
3126
3127 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003128};
3129
3130
3131class CompilationCacheTable: public HashTable<0, 2> {
3132 public:
3133 // Find cached value for a string key, otherwise return null.
3134 Object* Lookup(String* src);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003135 Object* LookupEval(String* src, Context* context);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003136 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
3137 Object* Put(String* src, Object* value);
ager@chromium.org381abbb2009-02-25 13:23:22 +00003138 Object* PutEval(String* src, Context* context, Object* value);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003139 Object* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
3140
3141 static inline CompilationCacheTable* cast(Object* obj);
3142
3143 private:
3144 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
ager@chromium.org236ad962008-09-25 09:45:57 +00003145};
3146
3147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003148enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
3149enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
3150
3151
ager@chromium.org7c537e22008-10-16 08:43:32 +00003152class StringHasher {
3153 public:
3154 inline StringHasher(int length);
3155
3156 // Returns true if the hash of this string can be computed without
3157 // looking at the contents.
3158 inline bool has_trivial_hash();
3159
3160 // Add a character to the hash and update the array index calculation.
3161 inline void AddCharacter(uc32 c);
3162
3163 // Adds a character to the hash but does not update the array index
3164 // calculation. This can only be called when it has been verified
3165 // that the input is not an array index.
3166 inline void AddCharacterNoIndex(uc32 c);
3167
3168 // Returns the value to store in the hash field of a string with
3169 // the given length and contents.
3170 uint32_t GetHashField();
3171
3172 // Returns true if the characters seen so far make up a legal array
3173 // index.
3174 bool is_array_index() { return is_array_index_; }
3175
3176 bool is_valid() { return is_valid_; }
3177
3178 void invalidate() { is_valid_ = false; }
3179
3180 private:
3181
3182 uint32_t array_index() {
3183 ASSERT(is_array_index());
3184 return array_index_;
3185 }
3186
3187 inline uint32_t GetHash();
3188
3189 int length_;
3190 uint32_t raw_running_hash_;
3191 uint32_t array_index_;
3192 bool is_array_index_;
3193 bool is_first_char_;
3194 bool is_valid_;
3195};
3196
3197
ager@chromium.org870a0b62008-11-04 11:43:05 +00003198// The characteristics of a string are stored in its map. Retrieving these
3199// few bits of information is moderately expensive, involving two memory
3200// loads where the second is dependent on the first. To improve efficiency
3201// the shape of the string is given its own class so that it can be retrieved
3202// once and used for several string operations. A StringShape is small enough
3203// to be passed by value and is immutable, but be aware that flattening a
ager@chromium.orgc3e50d82008-11-05 11:53:10 +00003204// string can potentially alter its shape. Also be aware that a GC caused by
3205// something else can alter the shape of a string due to ConsString
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003206// shortcutting. Keeping these restrictions in mind has proven to be error-
3207// prone and so we no longer put StringShapes in variables unless there is a
3208// concrete performance benefit at that particular point in the code.
ager@chromium.org870a0b62008-11-04 11:43:05 +00003209class StringShape BASE_EMBEDDED {
3210 public:
3211 inline explicit StringShape(String* s);
3212 inline explicit StringShape(Map* s);
3213 inline explicit StringShape(InstanceType t);
ager@chromium.org870a0b62008-11-04 11:43:05 +00003214 inline bool IsSequential();
3215 inline bool IsExternal();
3216 inline bool IsCons();
3217 inline bool IsSliced();
3218 inline bool IsExternalAscii();
3219 inline bool IsExternalTwoByte();
3220 inline bool IsSequentialAscii();
3221 inline bool IsSequentialTwoByte();
3222 inline bool IsSymbol();
3223 inline StringRepresentationTag representation_tag();
3224 inline uint32_t full_representation_tag();
3225 inline uint32_t size_tag();
3226#ifdef DEBUG
3227 inline uint32_t type() { return type_; }
3228 inline void invalidate() { valid_ = false; }
3229 inline bool valid() { return valid_; }
3230#else
3231 inline void invalidate() { }
3232#endif
3233 private:
3234 uint32_t type_;
3235#ifdef DEBUG
3236 inline void set_valid() { valid_ = true; }
3237 bool valid_;
3238#else
3239 inline void set_valid() { }
3240#endif
3241};
3242
3243
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003244// The String abstract class captures JavaScript string values:
3245//
3246// Ecma-262:
3247// 4.3.16 String Value
3248// A string value is a member of the type String and is a finite
3249// ordered sequence of zero or more 16-bit unsigned integer values.
3250//
3251// All string values have a length field.
3252class String: public HeapObject {
3253 public:
3254 // Get and set the length of the string.
3255 inline int length();
3256 inline void set_length(int value);
3257
3258 // Get and set the uninterpreted length field of the string. Notice
3259 // that the length field is also used to cache the hash value of
3260 // strings. In order to get or set the actual length of the string
3261 // use the length() and set_length methods.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003262 inline uint32_t length_field();
3263 inline void set_length_field(uint32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003264
ager@chromium.org5ec48922009-05-05 07:25:34 +00003265 inline bool IsAsciiRepresentation();
3266 inline bool IsTwoByteRepresentation();
3267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003268 // Get and set individual two byte chars in the string.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003269 inline void Set(int index, uint16_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003270 // Get individual two byte char in the string. Repeated calls
3271 // to this method are not efficient unless the string is flat.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003272 inline uint16_t Get(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003274 // Try to flatten the top level ConsString that is hiding behind this
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003275 // string. This is a no-op unless the string is a ConsString or a
3276 // SlicedString. Flatten mutates the ConsString and might return a
3277 // failure.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003278 Object* TryFlatten();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00003279
3280 // Try to flatten the string. Checks first inline to see if it is necessary.
3281 // Do not handle allocation failures. After calling TryFlattenIfNotFlat, the
3282 // string could still be a ConsString, in which case a failure is returned.
3283 // Use FlattenString from Handles.cc to be sure to flatten.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003284 inline Object* TryFlattenIfNotFlat();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285
ager@chromium.org7c537e22008-10-16 08:43:32 +00003286 Vector<const char> ToAsciiVector();
3287 Vector<const uc16> ToUC16Vector();
3288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003289 // Mark the string as an undetectable object. It only applies to
3290 // ascii and two byte string types.
3291 bool MarkAsUndetectable();
3292
3293 // Slice the string and return a substring.
ager@chromium.orgc3e50d82008-11-05 11:53:10 +00003294 Object* Slice(int from, int to);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003295
3296 // String equality operations.
3297 inline bool Equals(String* other);
3298 bool IsEqualTo(Vector<const char> str);
3299
3300 // Return a UTF8 representation of the string. The string is null
3301 // terminated but may optionally contain nulls. Length is returned
3302 // in length_output if length_output is not a null pointer The string
3303 // should be nearly flat, otherwise the performance of this method may
3304 // be very slow (quadratic in the length). Setting robustness_flag to
3305 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
3306 // handles unexpected data without causing assert failures and it does not
3307 // do any heap allocations. This is useful when printing stack traces.
3308 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
3309 RobustnessFlag robustness_flag,
3310 int offset,
3311 int length,
3312 int* length_output = 0);
3313 SmartPointer<char> ToCString(
3314 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
3315 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
3316 int* length_output = 0);
3317
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003318 int Utf8Length();
3319
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003320 // Return a 16 bit Unicode representation of the string.
3321 // The string should be nearly flat, otherwise the performance of
3322 // of this method may be very bad. Setting robustness_flag to
3323 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
3324 // handles unexpected data without causing assert failures and it does not
3325 // do any heap allocations. This is useful when printing stack traces.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003326 SmartPointer<uc16> ToWideCString(
3327 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003328
3329 // Tells whether the hash code has been computed.
3330 inline bool HasHashCode();
3331
3332 // Returns a hash value used for the property table
3333 inline uint32_t Hash();
3334
ager@chromium.org7c537e22008-10-16 08:43:32 +00003335 static uint32_t ComputeLengthAndHashField(unibrow::CharacterStream* buffer,
3336 int length);
3337
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003338 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
3339 uint32_t* index,
3340 int length);
3341
ager@chromium.org6f10e412009-02-13 10:11:16 +00003342 // Externalization.
3343 bool MakeExternal(v8::String::ExternalStringResource* resource);
3344 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
3345
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003346 // Conversion.
3347 inline bool AsArrayIndex(uint32_t* index);
3348
3349 // Casting.
3350 static inline String* cast(Object* obj);
3351
3352 void PrintOn(FILE* out);
3353
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003354 // For use during stack traces. Performs rudimentary sanity check.
3355 bool LooksValid();
3356
3357 // Dispatched behavior.
3358 void StringShortPrint(StringStream* accumulator);
3359#ifdef DEBUG
3360 void StringPrint();
3361 void StringVerify();
3362#endif
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003363 inline bool IsFlat();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364
3365 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00003366 static const int kLengthOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003367 static const int kSize = kLengthOffset + kIntSize;
3368
3369 // Limits on sizes of different types of strings.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003370 static const int kMaxShortStringSize = 63;
3371 static const int kMaxMediumStringSize = 16383;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003372
ager@chromium.org7c537e22008-10-16 08:43:32 +00003373 static const int kMaxArrayIndexSize = 10;
3374
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003375 // Max ascii char code.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003376 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
ager@chromium.org381abbb2009-02-25 13:23:22 +00003377 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
ager@chromium.org8bb60582008-12-11 12:02:20 +00003378 static const int kMaxUC16CharCode = 0xffff;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003379
3380 // Minimum length for a cons or sliced string.
3381 static const int kMinNonFlatLength = 13;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003383 // Mask constant for checking if a string has a computed hash code
3384 // and if it is an array index. The least significant bit indicates
3385 // whether a hash code has been computed. If the hash code has been
3386 // computed the 2nd bit tells whether the string can be used as an
3387 // array index.
3388 static const int kHashComputedMask = 1;
3389 static const int kIsArrayIndexMask = 1 << 1;
ager@chromium.org7c537e22008-10-16 08:43:32 +00003390 static const int kNofLengthBitFields = 2;
3391
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003392 // Array index strings this short can keep their index in the hash
3393 // field.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003394 static const int kMaxCachedArrayIndexLength = 7;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003395
ager@chromium.org7c537e22008-10-16 08:43:32 +00003396 // Shift constants for retriving length and hash code from
3397 // length/hash field.
3398 static const int kHashShift = kNofLengthBitFields;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003399 static const int kShortLengthShift = kHashShift + kShortStringTag;
3400 static const int kMediumLengthShift = kHashShift + kMediumStringTag;
3401 static const int kLongLengthShift = kHashShift + kLongStringTag;
ager@chromium.org7c537e22008-10-16 08:43:32 +00003402
kasper.lund7276f142008-07-30 08:49:36 +00003403 // Limit for truncation in short printing.
3404 static const int kMaxShortPrintLength = 1024;
3405
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 // Support for regular expressions.
3407 const uc16* GetTwoByteData();
3408 const uc16* GetTwoByteData(unsigned start);
3409
3410 // Support for StringInputBuffer
3411 static const unibrow::byte* ReadBlock(String* input,
3412 unibrow::byte* util_buffer,
3413 unsigned capacity,
3414 unsigned* remaining,
3415 unsigned* offset);
3416 static const unibrow::byte* ReadBlock(String** input,
3417 unibrow::byte* util_buffer,
3418 unsigned capacity,
3419 unsigned* remaining,
3420 unsigned* offset);
3421
3422 // Helper function for flattening strings.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003423 template <typename sinkchar>
3424 static void WriteToFlat(String* source,
3425 sinkchar* sink,
3426 int from,
3427 int to);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003428
3429 protected:
3430 class ReadBlockBuffer {
3431 public:
3432 ReadBlockBuffer(unibrow::byte* util_buffer_,
3433 unsigned cursor_,
3434 unsigned capacity_,
3435 unsigned remaining_) :
3436 util_buffer(util_buffer_),
3437 cursor(cursor_),
3438 capacity(capacity_),
3439 remaining(remaining_) {
3440 }
3441 unibrow::byte* util_buffer;
3442 unsigned cursor;
3443 unsigned capacity;
3444 unsigned remaining;
3445 };
3446
3447 // NOTE: If you call StringInputBuffer routines on strings that are
3448 // too deeply nested trees of cons and slice strings, then this
3449 // routine will overflow the stack. Strings that are merely deeply
3450 // nested trees of cons strings do not have a problem apart from
3451 // performance.
3452
3453 static inline const unibrow::byte* ReadBlock(String* input,
3454 ReadBlockBuffer* buffer,
3455 unsigned* offset,
3456 unsigned max_chars);
3457 static void ReadBlockIntoBuffer(String* input,
3458 ReadBlockBuffer* buffer,
3459 unsigned* offset_ptr,
3460 unsigned max_chars);
3461
3462 private:
3463 // Slow case of String::Equals. This implementation works on any strings
3464 // but it is most efficient on strings that are almost flat.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003465 bool SlowEquals(String* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003466
3467 // Slow case of AsArrayIndex.
3468 bool SlowAsArrayIndex(uint32_t* index);
3469
3470 // Compute and set the hash code.
3471 uint32_t ComputeAndSetHash();
3472
3473 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
3474};
3475
3476
3477// The SeqString abstract class captures sequential string values.
3478class SeqString: public String {
3479 public:
3480
3481 // Casting.
3482 static inline SeqString* cast(Object* obj);
3483
3484 // Dispatched behaviour.
3485 // For regexp code.
3486 uint16_t* SeqStringGetTwoByteAddress();
3487
3488 private:
3489 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
3490};
3491
3492
3493// The AsciiString class captures sequential ascii string objects.
3494// Each character in the AsciiString is an ascii character.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003495class SeqAsciiString: public SeqString {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003496 public:
3497 // Dispatched behavior.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003498 inline uint16_t SeqAsciiStringGet(int index);
3499 inline void SeqAsciiStringSet(int index, uint16_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500
3501 // Get the address of the characters in this string.
3502 inline Address GetCharsAddress();
3503
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003504 inline char* GetChars();
3505
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003506 // Casting
ager@chromium.org7c537e22008-10-16 08:43:32 +00003507 static inline SeqAsciiString* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508
3509 // Garbage collection support. This method is called by the
3510 // garbage collector to compute the actual size of an AsciiString
3511 // instance.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003512 inline int SeqAsciiStringSize(InstanceType instance_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003513
3514 // Computes the size for an AsciiString instance of a given length.
3515 static int SizeFor(int length) {
3516 return kHeaderSize + OBJECT_SIZE_ALIGN(length * kCharSize);
3517 }
3518
3519 // Layout description.
3520 static const int kHeaderSize = String::kSize;
3521
3522 // Support for StringInputBuffer.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003523 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
3524 unsigned* offset,
3525 unsigned chars);
3526 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
3527 unsigned* offset,
3528 unsigned chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003529
3530 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00003531 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003532};
3533
3534
3535// The TwoByteString class captures sequential unicode string objects.
3536// Each character in the TwoByteString is a two-byte uint16_t.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003537class SeqTwoByteString: public SeqString {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538 public:
3539 // Dispatched behavior.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003540 inline uint16_t SeqTwoByteStringGet(int index);
3541 inline void SeqTwoByteStringSet(int index, uint16_t value);
3542
3543 // Get the address of the characters in this string.
3544 inline Address GetCharsAddress();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003545
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003546 inline uc16* GetChars();
3547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003548 // For regexp code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003549 const uint16_t* SeqTwoByteStringGetData(unsigned start);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003550
3551 // Casting
ager@chromium.org7c537e22008-10-16 08:43:32 +00003552 static inline SeqTwoByteString* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553
3554 // Garbage collection support. This method is called by the
3555 // garbage collector to compute the actual size of a TwoByteString
3556 // instance.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003557 inline int SeqTwoByteStringSize(InstanceType instance_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003558
3559 // Computes the size for a TwoByteString instance of a given length.
3560 static int SizeFor(int length) {
3561 return kHeaderSize + OBJECT_SIZE_ALIGN(length * kShortSize);
3562 }
3563
3564 // Layout description.
3565 static const int kHeaderSize = String::kSize;
3566
3567 // Support for StringInputBuffer.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003568 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
3569 unsigned* offset_ptr,
3570 unsigned chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003571
3572 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00003573 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574};
3575
3576
3577// The ConsString class describes string values built by using the
3578// addition operator on strings. A ConsString is a pair where the
3579// first and second components are pointers to other string values.
3580// One or both components of a ConsString can be pointers to other
3581// ConsStrings, creating a binary tree of ConsStrings where the leaves
3582// are non-ConsString string values. The string value represented by
3583// a ConsString can be obtained by concatenating the leaf string
3584// values in a left-to-right depth-first traversal of the tree.
3585class ConsString: public String {
3586 public:
ager@chromium.org870a0b62008-11-04 11:43:05 +00003587 // First string of the cons cell.
3588 inline String* first();
3589 // Doesn't check that the result is a string, even in debug mode. This is
3590 // useful during GC where the mark bits confuse the checks.
3591 inline Object* unchecked_first();
3592 inline void set_first(String* first,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003593 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594
ager@chromium.org870a0b62008-11-04 11:43:05 +00003595 // Second string of the cons cell.
3596 inline String* second();
3597 // Doesn't check that the result is a string, even in debug mode. This is
3598 // useful during GC where the mark bits confuse the checks.
3599 inline Object* unchecked_second();
3600 inline void set_second(String* second,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003601 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003602
3603 // Dispatched behavior.
3604 uint16_t ConsStringGet(int index);
3605
3606 // Casting.
3607 static inline ConsString* cast(Object* obj);
3608
3609 // Garbage collection support. This method is called during garbage
3610 // collection to iterate through the heap pointers in the body of
3611 // the ConsString.
3612 void ConsStringIterateBody(ObjectVisitor* v);
3613
3614 // Layout description.
3615 static const int kFirstOffset = String::kSize;
3616 static const int kSecondOffset = kFirstOffset + kPointerSize;
3617 static const int kSize = kSecondOffset + kPointerSize;
3618
3619 // Support for StringInputBuffer.
3620 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
3621 unsigned* offset_ptr,
3622 unsigned chars);
3623 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
3624 unsigned* offset_ptr,
3625 unsigned chars);
3626
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003627 // Minimum length for a cons string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 static const int kMinLength = 13;
3629
3630 private:
3631 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
3632};
3633
3634
3635// The SlicedString class describes string values that are slices of
3636// some other string. SlicedStrings consist of a reference to an
3637// underlying heap-allocated string value, a start index, and the
3638// length field common to all strings.
3639class SlicedString: public String {
3640 public:
3641 // The underlying string buffer.
ager@chromium.org870a0b62008-11-04 11:43:05 +00003642 inline String* buffer();
3643 inline void set_buffer(String* buffer);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644
3645 // The start index of the slice.
3646 inline int start();
3647 inline void set_start(int start);
3648
3649 // Dispatched behavior.
3650 uint16_t SlicedStringGet(int index);
3651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003652 // Casting.
3653 static inline SlicedString* cast(Object* obj);
3654
3655 // Garbage collection support.
3656 void SlicedStringIterateBody(ObjectVisitor* v);
3657
3658 // Layout description
3659 static const int kBufferOffset = String::kSize;
3660 static const int kStartOffset = kBufferOffset + kPointerSize;
3661 static const int kSize = kStartOffset + kIntSize;
3662
3663 // Support for StringInputBuffer.
3664 inline const unibrow::byte* SlicedStringReadBlock(ReadBlockBuffer* buffer,
3665 unsigned* offset_ptr,
3666 unsigned chars);
3667 inline void SlicedStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
3668 unsigned* offset_ptr,
3669 unsigned chars);
3670
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003671 private:
3672 DISALLOW_IMPLICIT_CONSTRUCTORS(SlicedString);
3673};
3674
3675
3676// The ExternalString class describes string values that are backed by
3677// a string resource that lies outside the V8 heap. ExternalStrings
3678// consist of the length field common to all strings, a pointer to the
3679// external resource. It is important to ensure (externally) that the
3680// resource is not deallocated while the ExternalString is live in the
3681// V8 heap.
3682//
3683// The API expects that all ExternalStrings are created through the
3684// API. Therefore, ExternalStrings should not be used internally.
3685class ExternalString: public String {
3686 public:
3687 // Casting
3688 static inline ExternalString* cast(Object* obj);
3689
3690 // Layout description.
3691 static const int kResourceOffset = String::kSize;
3692 static const int kSize = kResourceOffset + kPointerSize;
3693
3694 private:
3695 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
3696};
3697
3698
3699// The ExternalAsciiString class is an external string backed by an
3700// ASCII string.
3701class ExternalAsciiString: public ExternalString {
3702 public:
3703 typedef v8::String::ExternalAsciiStringResource Resource;
3704
3705 // The underlying resource.
3706 inline Resource* resource();
3707 inline void set_resource(Resource* buffer);
3708
3709 // Dispatched behavior.
3710 uint16_t ExternalAsciiStringGet(int index);
3711
3712 // Casting.
3713 static inline ExternalAsciiString* cast(Object* obj);
3714
3715 // Support for StringInputBuffer.
3716 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
3717 unsigned* offset,
3718 unsigned chars);
3719 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
3720 unsigned* offset,
3721 unsigned chars);
3722
ager@chromium.org6f10e412009-02-13 10:11:16 +00003723 // Identify the map for the external string/symbol with a particular length.
3724 static inline Map* StringMap(int length);
3725 static inline Map* SymbolMap(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003726 private:
3727 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
3728};
3729
3730
3731// The ExternalTwoByteString class is an external string backed by a UTF-16
3732// encoded string.
3733class ExternalTwoByteString: public ExternalString {
3734 public:
3735 typedef v8::String::ExternalStringResource Resource;
3736
3737 // The underlying string resource.
3738 inline Resource* resource();
3739 inline void set_resource(Resource* buffer);
3740
3741 // Dispatched behavior.
3742 uint16_t ExternalTwoByteStringGet(int index);
3743
3744 // For regexp code.
3745 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
3746
3747 // Casting.
3748 static inline ExternalTwoByteString* cast(Object* obj);
3749
3750 // Support for StringInputBuffer.
3751 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
3752 unsigned* offset_ptr,
3753 unsigned chars);
3754
ager@chromium.org6f10e412009-02-13 10:11:16 +00003755 // Identify the map for the external string/symbol with a particular length.
3756 static inline Map* StringMap(int length);
3757 static inline Map* SymbolMap(int length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758 private:
3759 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
3760};
3761
3762
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003763// A flat string reader provides random access to the contents of a
3764// string independent of the character width of the string. The handle
3765// must be valid as long as the reader is being used.
3766class FlatStringReader BASE_EMBEDDED {
3767 public:
3768 explicit FlatStringReader(Handle<String> str);
3769 explicit FlatStringReader(Vector<const char> input);
3770 ~FlatStringReader();
3771 void RefreshState();
3772 inline uc32 Get(int index);
3773 int length() { return length_; }
3774 static void PostGarbageCollectionProcessing();
3775 private:
3776 String** str_;
3777 bool is_ascii_;
3778 int length_;
3779 const void* start_;
3780 FlatStringReader* prev_;
3781 static FlatStringReader* top_;
3782};
3783
3784
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785// Note that StringInputBuffers are not valid across a GC! To fix this
3786// it would have to store a String Handle instead of a String* and
3787// AsciiStringReadBlock would have to be modified to use memcpy.
3788//
3789// StringInputBuffer is able to traverse any string regardless of how
3790// deeply nested a sequence of ConsStrings it is made of. However,
3791// performance will be better if deep strings are flattened before they
3792// are traversed. Since flattening requires memory allocation this is
3793// not always desirable, however (esp. in debugging situations).
3794class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
3795 public:
3796 virtual void Seek(unsigned pos);
3797 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
3798 inline StringInputBuffer(String* backing):
3799 unibrow::InputBuffer<String, String*, 1024>(backing) {}
3800};
3801
3802
3803class SafeStringInputBuffer
3804 : public unibrow::InputBuffer<String, String**, 256> {
3805 public:
3806 virtual void Seek(unsigned pos);
3807 inline SafeStringInputBuffer()
3808 : unibrow::InputBuffer<String, String**, 256>() {}
3809 inline SafeStringInputBuffer(String** backing)
3810 : unibrow::InputBuffer<String, String**, 256>(backing) {}
3811};
3812
3813
ager@chromium.org7c537e22008-10-16 08:43:32 +00003814template <typename T>
3815class VectorIterator {
3816 public:
3817 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
3818 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
3819 T GetNext() { return data_[index_++]; }
3820 bool has_more() { return index_ < data_.length(); }
3821 private:
3822 Vector<const T> data_;
3823 int index_;
3824};
3825
3826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827// The Oddball describes objects null, undefined, true, and false.
3828class Oddball: public HeapObject {
3829 public:
3830 // [to_string]: Cached to_string computed at startup.
3831 DECL_ACCESSORS(to_string, String)
3832
3833 // [to_number]: Cached to_number computed at startup.
3834 DECL_ACCESSORS(to_number, Object)
3835
3836 // Casting.
3837 static inline Oddball* cast(Object* obj);
3838
3839 // Dispatched behavior.
3840 void OddballIterateBody(ObjectVisitor* v);
3841#ifdef DEBUG
3842 void OddballVerify();
3843#endif
3844
3845 // Initialize the fields.
3846 Object* Initialize(const char* to_string, Object* to_number);
3847
3848 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00003849 static const int kToStringOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850 static const int kToNumberOffset = kToStringOffset + kPointerSize;
3851 static const int kSize = kToNumberOffset + kPointerSize;
3852
3853 private:
3854 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
3855};
3856
3857
3858// Proxy describes objects pointing from JavaScript to C structures.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003859// Since they cannot contain references to JS HeapObjects they can be
3860// placed in old_data_space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003861class Proxy: public HeapObject {
3862 public:
3863 // [proxy]: field containing the address.
3864 inline Address proxy();
3865 inline void set_proxy(Address value);
3866
3867 // Casting.
3868 static inline Proxy* cast(Object* obj);
3869
3870 // Dispatched behavior.
3871 inline void ProxyIterateBody(ObjectVisitor* v);
3872#ifdef DEBUG
3873 void ProxyPrint();
3874 void ProxyVerify();
3875#endif
3876
3877 // Layout description.
3878
ager@chromium.org236ad962008-09-25 09:45:57 +00003879 static const int kProxyOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003880 static const int kSize = kProxyOffset + kPointerSize;
3881
3882 private:
3883 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
3884};
3885
3886
3887// The JSArray describes JavaScript Arrays
3888// Such an array can be in one of two modes:
3889// - fast, backing storage is a FixedArray and length <= elements.length();
3890// Please note: push and pop can be used to grow and shrink the array.
3891// - slow, backing storage is a HashTable with numbers as keys.
3892class JSArray: public JSObject {
3893 public:
3894 // [length]: The length property.
3895 DECL_ACCESSORS(length, Object)
3896
3897 Object* JSArrayUpdateLengthFromIndex(uint32_t index, Object* value);
3898
3899 // Initialize the array with the given capacity. The function may
3900 // fail due to out-of-memory situations, but only if the requested
3901 // capacity is non-zero.
3902 Object* Initialize(int capacity);
3903
3904 // Set the content of the array to the content of storage.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003905 inline void SetContent(FixedArray* storage);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907 // Casting.
3908 static inline JSArray* cast(Object* obj);
3909
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003910 // Uses handles. Ensures that the fixed array backing the JSArray has at
3911 // least the stated size.
3912 void EnsureSize(int minimum_size_of_backing_fixed_array);
3913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003914 // Dispatched behavior.
3915#ifdef DEBUG
3916 void JSArrayPrint();
3917 void JSArrayVerify();
3918#endif
3919
3920 // Layout description.
3921 static const int kLengthOffset = JSObject::kHeaderSize;
3922 static const int kSize = kLengthOffset + kPointerSize;
3923
3924 private:
3925 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
3926};
3927
3928
ager@chromium.org32912102009-01-16 10:38:43 +00003929// An accessor must have a getter, but can have no setter.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930//
3931// When setting a property, V8 searches accessors in prototypes.
3932// If an accessor was found and it does not have a setter,
3933// the request is ignored.
3934//
3935// To allow shadow an accessor property, the accessor can
3936// have READ_ONLY property attribute so that a new value
3937// is added to the local object to shadow the accessor
3938// in prototypes.
3939class AccessorInfo: public Struct {
3940 public:
3941 DECL_ACCESSORS(getter, Object)
3942 DECL_ACCESSORS(setter, Object)
3943 DECL_ACCESSORS(data, Object)
3944 DECL_ACCESSORS(name, Object)
3945 DECL_ACCESSORS(flag, Smi)
3946
3947 inline bool all_can_read();
3948 inline void set_all_can_read(bool value);
3949
3950 inline bool all_can_write();
3951 inline void set_all_can_write(bool value);
3952
ager@chromium.org870a0b62008-11-04 11:43:05 +00003953 inline bool prohibits_overwriting();
3954 inline void set_prohibits_overwriting(bool value);
3955
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003956 inline PropertyAttributes property_attributes();
3957 inline void set_property_attributes(PropertyAttributes attributes);
3958
3959 static inline AccessorInfo* cast(Object* obj);
3960
3961#ifdef DEBUG
3962 void AccessorInfoPrint();
3963 void AccessorInfoVerify();
3964#endif
3965
ager@chromium.org236ad962008-09-25 09:45:57 +00003966 static const int kGetterOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003967 static const int kSetterOffset = kGetterOffset + kPointerSize;
3968 static const int kDataOffset = kSetterOffset + kPointerSize;
3969 static const int kNameOffset = kDataOffset + kPointerSize;
3970 static const int kFlagOffset = kNameOffset + kPointerSize;
3971 static const int kSize = kFlagOffset + kPointerSize;
3972
3973 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003974 // Bit positions in flag.
ager@chromium.org870a0b62008-11-04 11:43:05 +00003975 static const int kAllCanReadBit = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003976 static const int kAllCanWriteBit = 1;
ager@chromium.org870a0b62008-11-04 11:43:05 +00003977 static const int kProhibitsOverwritingBit = 2;
3978 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
mads.s.ager31e71382008-08-13 09:32:07 +00003979
3980 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003981};
3982
3983
3984class AccessCheckInfo: public Struct {
3985 public:
3986 DECL_ACCESSORS(named_callback, Object)
3987 DECL_ACCESSORS(indexed_callback, Object)
3988 DECL_ACCESSORS(data, Object)
3989
3990 static inline AccessCheckInfo* cast(Object* obj);
3991
3992#ifdef DEBUG
3993 void AccessCheckInfoPrint();
3994 void AccessCheckInfoVerify();
3995#endif
3996
ager@chromium.org236ad962008-09-25 09:45:57 +00003997 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003998 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
3999 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
4000 static const int kSize = kDataOffset + kPointerSize;
4001
4002 private:
4003 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
4004};
4005
4006
4007class InterceptorInfo: public Struct {
4008 public:
4009 DECL_ACCESSORS(getter, Object)
4010 DECL_ACCESSORS(setter, Object)
4011 DECL_ACCESSORS(query, Object)
4012 DECL_ACCESSORS(deleter, Object)
4013 DECL_ACCESSORS(enumerator, Object)
4014 DECL_ACCESSORS(data, Object)
4015
4016 static inline InterceptorInfo* cast(Object* obj);
4017
4018#ifdef DEBUG
4019 void InterceptorInfoPrint();
4020 void InterceptorInfoVerify();
4021#endif
4022
ager@chromium.org236ad962008-09-25 09:45:57 +00004023 static const int kGetterOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 static const int kSetterOffset = kGetterOffset + kPointerSize;
4025 static const int kQueryOffset = kSetterOffset + kPointerSize;
4026 static const int kDeleterOffset = kQueryOffset + kPointerSize;
4027 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
4028 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
4029 static const int kSize = kDataOffset + kPointerSize;
4030
4031 private:
4032 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
4033};
4034
4035
4036class CallHandlerInfo: public Struct {
4037 public:
4038 DECL_ACCESSORS(callback, Object)
4039 DECL_ACCESSORS(data, Object)
4040
4041 static inline CallHandlerInfo* cast(Object* obj);
4042
4043#ifdef DEBUG
4044 void CallHandlerInfoPrint();
4045 void CallHandlerInfoVerify();
4046#endif
4047
ager@chromium.org236ad962008-09-25 09:45:57 +00004048 static const int kCallbackOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004049 static const int kDataOffset = kCallbackOffset + kPointerSize;
4050 static const int kSize = kDataOffset + kPointerSize;
4051
4052 private:
4053 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
4054};
4055
4056
4057class TemplateInfo: public Struct {
4058 public:
4059 DECL_ACCESSORS(tag, Object)
4060 DECL_ACCESSORS(property_list, Object)
4061
4062#ifdef DEBUG
4063 void TemplateInfoVerify();
4064#endif
4065
ager@chromium.org236ad962008-09-25 09:45:57 +00004066 static const int kTagOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004067 static const int kPropertyListOffset = kTagOffset + kPointerSize;
4068 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
4069 protected:
4070 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4071 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
4072};
4073
4074
4075class FunctionTemplateInfo: public TemplateInfo {
4076 public:
4077 DECL_ACCESSORS(serial_number, Object)
4078 DECL_ACCESSORS(call_code, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004079 DECL_ACCESSORS(property_accessors, Object)
4080 DECL_ACCESSORS(prototype_template, Object)
4081 DECL_ACCESSORS(parent_template, Object)
4082 DECL_ACCESSORS(named_property_handler, Object)
4083 DECL_ACCESSORS(indexed_property_handler, Object)
4084 DECL_ACCESSORS(instance_template, Object)
4085 DECL_ACCESSORS(class_name, Object)
4086 DECL_ACCESSORS(signature, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004087 DECL_ACCESSORS(instance_call_handler, Object)
4088 DECL_ACCESSORS(access_check_info, Object)
4089 DECL_ACCESSORS(flag, Smi)
4090
4091 // Following properties use flag bits.
4092 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
4093 DECL_BOOLEAN_ACCESSORS(undetectable)
4094 // If the bit is set, object instances created by this function
4095 // requires access check.
4096 DECL_BOOLEAN_ACCESSORS(needs_access_check)
4097
4098 static inline FunctionTemplateInfo* cast(Object* obj);
4099
4100#ifdef DEBUG
4101 void FunctionTemplateInfoPrint();
4102 void FunctionTemplateInfoVerify();
4103#endif
4104
4105 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
4106 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
kasper.lund212ac232008-07-16 07:07:30 +00004107 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108 static const int kPrototypeTemplateOffset =
4109 kPropertyAccessorsOffset + kPointerSize;
4110 static const int kParentTemplateOffset =
4111 kPrototypeTemplateOffset + kPointerSize;
4112 static const int kNamedPropertyHandlerOffset =
4113 kParentTemplateOffset + kPointerSize;
4114 static const int kIndexedPropertyHandlerOffset =
4115 kNamedPropertyHandlerOffset + kPointerSize;
4116 static const int kInstanceTemplateOffset =
4117 kIndexedPropertyHandlerOffset + kPointerSize;
4118 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
4119 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
v8.team.kasperl727e9952008-09-02 14:56:44 +00004120 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004121 static const int kAccessCheckInfoOffset =
4122 kInstanceCallHandlerOffset + kPointerSize;
4123 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
4124 static const int kSize = kFlagOffset + kPointerSize;
4125
4126 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004127 // Bit position in the flag, from least significant bit position.
4128 static const int kHiddenPrototypeBit = 0;
4129 static const int kUndetectableBit = 1;
4130 static const int kNeedsAccessCheckBit = 2;
mads.s.ager31e71382008-08-13 09:32:07 +00004131
4132 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004133};
4134
4135
4136class ObjectTemplateInfo: public TemplateInfo {
4137 public:
4138 DECL_ACCESSORS(constructor, Object)
kasper.lund212ac232008-07-16 07:07:30 +00004139 DECL_ACCESSORS(internal_field_count, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004140
4141 static inline ObjectTemplateInfo* cast(Object* obj);
4142
4143#ifdef DEBUG
4144 void ObjectTemplateInfoPrint();
4145 void ObjectTemplateInfoVerify();
4146#endif
4147
4148 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
kasper.lund212ac232008-07-16 07:07:30 +00004149 static const int kInternalFieldCountOffset =
4150 kConstructorOffset + kPointerSize;
4151 static const int kSize = kInternalFieldCountOffset + kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004152};
4153
4154
4155class SignatureInfo: public Struct {
4156 public:
4157 DECL_ACCESSORS(receiver, Object)
4158 DECL_ACCESSORS(args, Object)
4159
4160 static inline SignatureInfo* cast(Object* obj);
4161
4162#ifdef DEBUG
4163 void SignatureInfoPrint();
4164 void SignatureInfoVerify();
4165#endif
4166
ager@chromium.org236ad962008-09-25 09:45:57 +00004167 static const int kReceiverOffset = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004168 static const int kArgsOffset = kReceiverOffset + kPointerSize;
4169 static const int kSize = kArgsOffset + kPointerSize;
4170
4171 private:
4172 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
4173};
4174
4175
4176class TypeSwitchInfo: public Struct {
4177 public:
4178 DECL_ACCESSORS(types, Object)
4179
4180 static inline TypeSwitchInfo* cast(Object* obj);
4181
4182#ifdef DEBUG
4183 void TypeSwitchInfoPrint();
4184 void TypeSwitchInfoVerify();
4185#endif
4186
ager@chromium.org236ad962008-09-25 09:45:57 +00004187 static const int kTypesOffset = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004188 static const int kSize = kTypesOffset + kPointerSize;
4189};
4190
4191
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004192#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org32912102009-01-16 10:38:43 +00004193// The DebugInfo class holds additional information for a function being
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004194// debugged.
4195class DebugInfo: public Struct {
4196 public:
ager@chromium.org32912102009-01-16 10:38:43 +00004197 // The shared function info for the source being debugged.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004198 DECL_ACCESSORS(shared, SharedFunctionInfo)
4199 // Code object for the original code.
4200 DECL_ACCESSORS(original_code, Code)
4201 // Code object for the patched code. This code object is the code object
4202 // currently active for the function.
4203 DECL_ACCESSORS(code, Code)
4204 // Fixed array holding status information for each active break point.
4205 DECL_ACCESSORS(break_points, FixedArray)
4206
4207 // Check if there is a break point at a code position.
4208 bool HasBreakPoint(int code_position);
4209 // Get the break point info object for a code position.
4210 Object* GetBreakPointInfo(int code_position);
4211 // Clear a break point.
4212 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
4213 int code_position,
4214 Handle<Object> break_point_object);
4215 // Set a break point.
4216 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
4217 int source_position, int statement_position,
4218 Handle<Object> break_point_object);
4219 // Get the break point objects for a code position.
4220 Object* GetBreakPointObjects(int code_position);
4221 // Find the break point info holding this break point object.
4222 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
4223 Handle<Object> break_point_object);
4224 // Get the number of break points for this function.
4225 int GetBreakPointCount();
4226
4227 static inline DebugInfo* cast(Object* obj);
4228
4229#ifdef DEBUG
4230 void DebugInfoPrint();
4231 void DebugInfoVerify();
4232#endif
4233
ager@chromium.org236ad962008-09-25 09:45:57 +00004234 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004235 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
4236 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
4237 static const int kActiveBreakPointsCountIndex =
4238 kPatchedCodeIndex + kPointerSize;
4239 static const int kBreakPointsStateIndex =
4240 kActiveBreakPointsCountIndex + kPointerSize;
4241 static const int kSize = kBreakPointsStateIndex + kPointerSize;
4242
4243 private:
4244 static const int kNoBreakPointInfo = -1;
4245
4246 // Lookup the index in the break_points array for a code position.
4247 int GetBreakPointInfoIndex(int code_position);
4248
4249 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
4250};
4251
4252
4253// The BreakPointInfo class holds information for break points set in a
4254// function. The DebugInfo object holds a BreakPointInfo object for each code
4255// position with one or more break points.
4256class BreakPointInfo: public Struct {
4257 public:
4258 // The position in the code for the break point.
4259 DECL_ACCESSORS(code_position, Smi)
4260 // The position in the source for the break position.
4261 DECL_ACCESSORS(source_position, Smi)
4262 // The position in the source for the last statement before this break
4263 // position.
4264 DECL_ACCESSORS(statement_position, Smi)
4265 // List of related JavaScript break points.
4266 DECL_ACCESSORS(break_point_objects, Object)
4267
4268 // Removes a break point.
4269 static void ClearBreakPoint(Handle<BreakPointInfo> info,
4270 Handle<Object> break_point_object);
4271 // Set a break point.
4272 static void SetBreakPoint(Handle<BreakPointInfo> info,
4273 Handle<Object> break_point_object);
4274 // Check if break point info has this break point object.
4275 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
4276 Handle<Object> break_point_object);
4277 // Get the number of break points for this code position.
4278 int GetBreakPointCount();
4279
4280 static inline BreakPointInfo* cast(Object* obj);
4281
4282#ifdef DEBUG
4283 void BreakPointInfoPrint();
4284 void BreakPointInfoVerify();
4285#endif
4286
ager@chromium.org236ad962008-09-25 09:45:57 +00004287 static const int kCodePositionIndex = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004288 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
4289 static const int kStatementPositionIndex =
4290 kSourcePositionIndex + kPointerSize;
4291 static const int kBreakPointObjectsIndex =
4292 kStatementPositionIndex + kPointerSize;
4293 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
4294
4295 private:
4296 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
4297};
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004298#endif // ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004299
4300
4301#undef DECL_BOOLEAN_ACCESSORS
4302#undef DECL_ACCESSORS
4303
4304
4305// Abstract base class for visiting, and optionally modifying, the
4306// pointers contained in Objects. Used in GC and serialization/deserialization.
4307class ObjectVisitor BASE_EMBEDDED {
4308 public:
4309 virtual ~ObjectVisitor() {}
4310
4311 // Visits a contiguous arrays of pointers in the half-open range
4312 // [start, end). Any or all of the values may be modified on return.
4313 virtual void VisitPointers(Object** start, Object** end) = 0;
4314
4315 // To allow lazy clearing of inline caches the visitor has
4316 // a rich interface for iterating over Code objects..
4317
4318 // Called prior to visiting the body of a Code object.
4319 virtual void BeginCodeIteration(Code* code);
4320
4321 // Visits a code target in the instruction stream.
4322 virtual void VisitCodeTarget(RelocInfo* rinfo);
4323
4324 // Visits a runtime entry in the instruction stream.
4325 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
4326
4327 // Visits a debug call target in the instruction stream.
4328 virtual void VisitDebugTarget(RelocInfo* rinfo);
4329
4330 // Called after completing visiting the body of a Code object.
4331 virtual void EndCodeIteration(Code* code) {}
4332
4333 // Handy shorthand for visiting a single pointer.
4334 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
4335
4336 // Visits a contiguous arrays of external references (references to the C++
4337 // heap) in the half-open range [start, end). Any or all of the values
4338 // may be modified on return.
4339 virtual void VisitExternalReferences(Address* start, Address* end) {}
4340
4341 inline void VisitExternalReference(Address* p) {
4342 VisitExternalReferences(p, p + 1);
4343 }
4344
4345#ifdef DEBUG
4346 // Intended for serialization/deserialization checking: insert, or
4347 // check for the presence of, a tag at this position in the stream.
4348 virtual void Synchronize(const char* tag) {}
4349#endif
4350};
4351
4352
4353// BooleanBit is a helper class for setting and getting a bit in an
4354// integer or Smi.
4355class BooleanBit : public AllStatic {
4356 public:
4357 static inline bool get(Smi* smi, int bit_position) {
4358 return get(smi->value(), bit_position);
4359 }
4360
4361 static inline bool get(int value, int bit_position) {
4362 return (value & (1 << bit_position)) != 0;
4363 }
4364
4365 static inline Smi* set(Smi* smi, int bit_position, bool v) {
4366 return Smi::FromInt(set(smi->value(), bit_position, v));
4367 }
4368
4369 static inline int set(int value, int bit_position, bool v) {
4370 if (v) {
4371 value |= (1 << bit_position);
4372 } else {
4373 value &= ~(1 << bit_position);
4374 }
4375 return value;
4376 }
4377};
4378
4379} } // namespace v8::internal
4380
4381#endif // V8_OBJECTS_H_