blob: f9f66bf0cb195b3a00a4c23de6515c2b4bc29e2b [file] [log] [blame]
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001// Copyright 2011 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
lrn@chromium.org1c092762011-05-09 09:42:16 +000031#include "allocation.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "builtins.h"
ager@chromium.orgea91cc52011-05-23 06:06:11 +000033#include "list.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000034#include "smart-pointer.h"
35#include "unicode-inl.h"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000036#if V8_TARGET_ARCH_ARM
37#include "arm/constants-arm.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000038#elif V8_TARGET_ARCH_MIPS
39#include "mips/constants-mips.h"
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000040#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
42//
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +000043// Most object types in the V8 JavaScript are described in this file.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044//
45// Inheritance hierarchy:
lrn@chromium.org303ada72010-10-27 09:33:13 +000046// - MaybeObject (an object or a failure)
47// - Failure (immediate for marking failed operation)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048// - Object
49// - Smi (immediate small integer)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000050// - HeapObject (superclass for everything allocated in the heap)
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000051// - JSReceiver (suitable for property access)
52// - JSObject
53// - JSArray
54// - JSRegExp
55// - JSFunction
56// - GlobalObject
57// - JSGlobalObject
58// - JSBuiltinsObject
59// - JSGlobalProxy
60// - JSValue
61// - JSMessageObject
62// - JSProxy
63// - JSFunctionProxy
ricow@chromium.org30ce4112010-05-31 10:38:25 +000064// - ByteArray
ricow@chromium.org30ce4112010-05-31 10:38:25 +000065// - ExternalArray
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +000066// - ExternalPixelArray
ricow@chromium.org30ce4112010-05-31 10:38:25 +000067// - ExternalByteArray
68// - ExternalUnsignedByteArray
69// - ExternalShortArray
70// - ExternalUnsignedShortArray
71// - ExternalIntArray
72// - ExternalUnsignedIntArray
73// - ExternalFloatArray
74// - FixedArray
75// - DescriptorArray
76// - HashTable
77// - Dictionary
78// - SymbolTable
79// - CompilationCacheTable
80// - CodeCacheHashTable
81// - MapCache
82// - Context
ricow@chromium.org30ce4112010-05-31 10:38:25 +000083// - JSFunctionResultCache
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +000084// - SerializedScopeInfo
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085// - String
86// - SeqString
ager@chromium.org7c537e22008-10-16 08:43:32 +000087// - SeqAsciiString
88// - SeqTwoByteString
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089// - ConsString
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090// - ExternalString
91// - ExternalAsciiString
92// - ExternalTwoByteString
93// - HeapNumber
94// - Code
95// - Map
96// - Oddball
ager@chromium.orgea91cc52011-05-23 06:06:11 +000097// - Foreign
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098// - SharedFunctionInfo
99// - Struct
100// - AccessorInfo
101// - AccessCheckInfo
102// - InterceptorInfo
103// - CallHandlerInfo
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000104// - TemplateInfo
105// - FunctionTemplateInfo
106// - ObjectTemplateInfo
107// - Script
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108// - SignatureInfo
109// - TypeSwitchInfo
110// - DebugInfo
111// - BreakPointInfo
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000112// - CodeCache
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000113//
114// Formats of Object*:
115// Smi: [31 bit signed int] 0
116// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
117// Failure: [30 bit signed int] 11
118
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119// Ecma-262 3rd 8.6.1
120enum PropertyAttributes {
121 NONE = v8::None,
122 READ_ONLY = v8::ReadOnly,
123 DONT_ENUM = v8::DontEnum,
124 DONT_DELETE = v8::DontDelete,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125 ABSENT = 16 // Used in runtime to indicate a property is absent.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000126 // ABSENT can never be stored in or returned from a descriptor's attributes
127 // bitfield. It is only used as a return value meaning the attributes of
128 // a non-existent property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000129};
130
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000131namespace v8 {
132namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133
134
135// PropertyDetails captures type and attributes for a property.
136// They are used both in property dictionaries and instance descriptors.
137class PropertyDetails BASE_EMBEDDED {
138 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139 PropertyDetails(PropertyAttributes attributes,
140 PropertyType type,
141 int index = 0) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000142 ASSERT(type != EXTERNAL_ARRAY_TRANSITION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143 ASSERT(TypeField::is_valid(type));
144 ASSERT(AttributesField::is_valid(attributes));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000145 ASSERT(StorageField::is_valid(index));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000146
147 value_ = TypeField::encode(type)
148 | AttributesField::encode(attributes)
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000149 | StorageField::encode(index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150
151 ASSERT(type == this->type());
152 ASSERT(attributes == this->attributes());
153 ASSERT(index == this->index());
154 }
155
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000156 PropertyDetails(PropertyAttributes attributes,
157 PropertyType type,
158 ExternalArrayType array_type) {
159 ASSERT(type == EXTERNAL_ARRAY_TRANSITION);
160 ASSERT(TypeField::is_valid(type));
161 ASSERT(AttributesField::is_valid(attributes));
162 ASSERT(StorageField::is_valid(static_cast<int>(array_type)));
163
164 value_ = TypeField::encode(type)
165 | AttributesField::encode(attributes)
166 | StorageField::encode(static_cast<int>(array_type));
167
168 ASSERT(type == this->type());
169 ASSERT(attributes == this->attributes());
170 ASSERT(array_type == this->array_type());
171 }
172
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 // Conversion for storing details as Object*.
karlklose@chromium.org44bc7082011-04-11 12:33:05 +0000174 explicit inline PropertyDetails(Smi* smi);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000175 inline Smi* AsSmi();
176
177 PropertyType type() { return TypeField::decode(value_); }
178
179 bool IsTransition() {
180 PropertyType t = type();
181 ASSERT(t != INTERCEPTOR);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000182 return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
183 t == EXTERNAL_ARRAY_TRANSITION;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000184 }
185
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000186 bool IsProperty() {
187 return type() < FIRST_PHANTOM_PROPERTY_TYPE;
188 }
189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 PropertyAttributes attributes() { return AttributesField::decode(value_); }
191
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000192 int index() { return StorageField::decode(value_); }
193
194 ExternalArrayType array_type() {
195 ASSERT(type() == EXTERNAL_ARRAY_TRANSITION);
196 return static_cast<ExternalArrayType>(StorageField::decode(value_));
197 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000199 inline PropertyDetails AsDeleted();
200
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000201 static bool IsValidIndex(int index) {
202 return StorageField::is_valid(index);
203 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204
205 bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
206 bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
207 bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000208 bool IsDeleted() { return DeletedField::decode(value_) != 0;}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000209
210 // Bit fields in value_ (type, shift, size). Must be public so the
211 // constants can be embedded in generated code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000212 class TypeField: public BitField<PropertyType, 0, 4> {};
213 class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
214 class DeletedField: public BitField<uint32_t, 7, 1> {};
215 class StorageField: public BitField<uint32_t, 8, 32-8> {};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216
217 static const int kInitialIndex = 1;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000218
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 private:
220 uint32_t value_;
221};
222
ager@chromium.org32912102009-01-16 10:38:43 +0000223
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000224// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
225enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
226
ager@chromium.org32912102009-01-16 10:38:43 +0000227
228// PropertyNormalizationMode is used to specify whether to keep
229// inobject properties when normalizing properties of a JSObject.
230enum PropertyNormalizationMode {
231 CLEAR_INOBJECT_PROPERTIES,
232 KEEP_INOBJECT_PROPERTIES
233};
234
235
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000236// NormalizedMapSharingMode is used to specify whether a map may be shared
237// by different objects with normalized properties.
238enum NormalizedMapSharingMode {
239 UNIQUE_NORMALIZED_MAP,
240 SHARED_NORMALIZED_MAP
241};
242
243
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000244// Instance size sentinel for objects of variable size.
245static const int kVariableSizeSentinel = 0;
246
247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248// All Maps have a field instance_type containing a InstanceType.
249// It describes the type of the instances.
250//
251// As an example, a JavaScript object is a heap object and its map
252// instance_type is JS_OBJECT_TYPE.
253//
254// The names of the string instance types are intended to systematically
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000255// mirror their encoding in the instance_type field of the map. The default
256// encoding is considered TWO_BYTE. It is not mentioned in the name. ASCII
257// encoding is mentioned explicitly in the name. Likewise, the default
258// representation is considered sequential. It is not mentioned in the
259// name. The other representations (eg, CONS, EXTERNAL) are explicitly
260// mentioned. Finally, the string is either a SYMBOL_TYPE (if it is a
261// symbol) or a STRING_TYPE (if it is not a symbol).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262//
263// NOTE: The following things are some that depend on the string types having
264// instance_types that are less than those of all other types:
265// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
266// Object::IsString.
267//
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000268// NOTE: Everything following JS_VALUE_TYPE is considered a
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269// JSObject for GC purposes. The first four entries here have typeof
270// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000271#define INSTANCE_TYPE_LIST_ALL(V) \
272 V(SYMBOL_TYPE) \
273 V(ASCII_SYMBOL_TYPE) \
274 V(CONS_SYMBOL_TYPE) \
275 V(CONS_ASCII_SYMBOL_TYPE) \
276 V(EXTERNAL_SYMBOL_TYPE) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000277 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000278 V(EXTERNAL_ASCII_SYMBOL_TYPE) \
279 V(STRING_TYPE) \
280 V(ASCII_STRING_TYPE) \
281 V(CONS_STRING_TYPE) \
282 V(CONS_ASCII_STRING_TYPE) \
283 V(EXTERNAL_STRING_TYPE) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000284 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000285 V(EXTERNAL_ASCII_STRING_TYPE) \
286 V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE) \
287 \
288 V(MAP_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000289 V(CODE_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000290 V(ODDBALL_TYPE) \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000291 V(JS_GLOBAL_PROPERTY_CELL_TYPE) \
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000292 \
293 V(HEAP_NUMBER_TYPE) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000294 V(FOREIGN_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000295 V(BYTE_ARRAY_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000296 /* Note: the order of these external array */ \
297 /* types is relied upon in */ \
298 /* Object::IsExternalArray(). */ \
299 V(EXTERNAL_BYTE_ARRAY_TYPE) \
300 V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE) \
301 V(EXTERNAL_SHORT_ARRAY_TYPE) \
302 V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE) \
303 V(EXTERNAL_INT_ARRAY_TYPE) \
304 V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE) \
305 V(EXTERNAL_FLOAT_ARRAY_TYPE) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000306 V(EXTERNAL_PIXEL_ARRAY_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000307 V(FILLER_TYPE) \
308 \
309 V(ACCESSOR_INFO_TYPE) \
310 V(ACCESS_CHECK_INFO_TYPE) \
311 V(INTERCEPTOR_INFO_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000312 V(CALL_HANDLER_INFO_TYPE) \
313 V(FUNCTION_TEMPLATE_INFO_TYPE) \
314 V(OBJECT_TEMPLATE_INFO_TYPE) \
315 V(SIGNATURE_INFO_TYPE) \
316 V(TYPE_SWITCH_INFO_TYPE) \
317 V(SCRIPT_TYPE) \
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000318 V(CODE_CACHE_TYPE) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000319 V(POLYMORPHIC_CODE_CACHE_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000320 \
ricow@chromium.org0b9f8502010-08-18 07:45:01 +0000321 V(FIXED_ARRAY_TYPE) \
322 V(SHARED_FUNCTION_INFO_TYPE) \
323 \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000324 V(JS_MESSAGE_OBJECT_TYPE) \
325 \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000326 V(JS_VALUE_TYPE) \
327 V(JS_OBJECT_TYPE) \
328 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
329 V(JS_GLOBAL_OBJECT_TYPE) \
330 V(JS_BUILTINS_OBJECT_TYPE) \
331 V(JS_GLOBAL_PROXY_TYPE) \
332 V(JS_ARRAY_TYPE) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000333 V(JS_PROXY_TYPE) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000334 V(JS_REGEXP_TYPE) \
335 \
336 V(JS_FUNCTION_TYPE) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000337 V(JS_FUNCTION_PROXY_TYPE) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000338
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000339#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000340#define INSTANCE_TYPE_LIST_DEBUGGER(V) \
341 V(DEBUG_INFO_TYPE) \
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000342 V(BREAK_POINT_INFO_TYPE)
343#else
344#define INSTANCE_TYPE_LIST_DEBUGGER(V)
345#endif
346
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000347#define INSTANCE_TYPE_LIST(V) \
348 INSTANCE_TYPE_LIST_ALL(V) \
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000349 INSTANCE_TYPE_LIST_DEBUGGER(V)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352// Since string types are not consecutive, this macro is used to
353// iterate over them.
354#define STRING_TYPE_LIST(V) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000355 V(SYMBOL_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000356 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000357 symbol, \
358 Symbol) \
359 V(ASCII_SYMBOL_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000360 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000361 ascii_symbol, \
362 AsciiSymbol) \
363 V(CONS_SYMBOL_TYPE, \
364 ConsString::kSize, \
365 cons_symbol, \
366 ConsSymbol) \
367 V(CONS_ASCII_SYMBOL_TYPE, \
368 ConsString::kSize, \
369 cons_ascii_symbol, \
370 ConsAsciiSymbol) \
371 V(EXTERNAL_SYMBOL_TYPE, \
372 ExternalTwoByteString::kSize, \
373 external_symbol, \
374 ExternalSymbol) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000375 V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE, \
376 ExternalTwoByteString::kSize, \
377 external_symbol_with_ascii_data, \
378 ExternalSymbolWithAsciiData) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000379 V(EXTERNAL_ASCII_SYMBOL_TYPE, \
380 ExternalAsciiString::kSize, \
381 external_ascii_symbol, \
382 ExternalAsciiSymbol) \
383 V(STRING_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000384 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000385 string, \
386 String) \
387 V(ASCII_STRING_TYPE, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000388 kVariableSizeSentinel, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000389 ascii_string, \
390 AsciiString) \
391 V(CONS_STRING_TYPE, \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000392 ConsString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000393 cons_string, \
394 ConsString) \
395 V(CONS_ASCII_STRING_TYPE, \
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000396 ConsString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000397 cons_ascii_string, \
398 ConsAsciiString) \
399 V(EXTERNAL_STRING_TYPE, \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000400 ExternalTwoByteString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000401 external_string, \
402 ExternalString) \
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000403 V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE, \
404 ExternalTwoByteString::kSize, \
405 external_string_with_ascii_data, \
406 ExternalStringWithAsciiData) \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000407 V(EXTERNAL_ASCII_STRING_TYPE, \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000408 ExternalAsciiString::kSize, \
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000409 external_ascii_string, \
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000410 ExternalAsciiString)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000411
412// A struct is a simple object a set of object-valued fields. Including an
413// object type in this causes the compiler to generate most of the boilerplate
414// code for the class including allocation and garbage collection routines,
415// casts and predicates. All you need to define is the class, methods and
416// object verification routines. Easy, no?
417//
418// Note that for subtle reasons related to the ordering or numerical values of
419// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
420// manually.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000421#define STRUCT_LIST_ALL(V) \
422 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
423 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
424 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
425 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
426 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
427 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
428 V(SIGNATURE_INFO, SignatureInfo, signature_info) \
429 V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info) \
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000430 V(SCRIPT, Script, script) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000431 V(CODE_CACHE, CodeCache, code_cache) \
432 V(POLYMORPHIC_CODE_CACHE, PolymorphicCodeCache, polymorphic_code_cache)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000433
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000434#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000435#define STRUCT_LIST_DEBUGGER(V) \
436 V(DEBUG_INFO, DebugInfo, debug_info) \
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000437 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
438#else
439#define STRUCT_LIST_DEBUGGER(V)
440#endif
441
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000442#define STRUCT_LIST(V) \
443 STRUCT_LIST_ALL(V) \
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000444 STRUCT_LIST_DEBUGGER(V)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445
446// We use the full 8 bits of the instance_type field to encode heap object
447// instance types. The high-order bit (bit 7) is set if the object is not a
448// string, and cleared if it is a string.
449const uint32_t kIsNotStringMask = 0x80;
450const uint32_t kStringTag = 0x0;
451const uint32_t kNotStringTag = 0x80;
452
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000453// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
454// There are not enough types that the non-string types (with bit 7 set) can
455// have bit 6 set too.
456const uint32_t kIsSymbolMask = 0x40;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457const uint32_t kNotSymbolTag = 0x0;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000458const uint32_t kSymbolTag = 0x40;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000459
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000460// If bit 7 is clear then bit 2 indicates whether the string consists of
461// two-byte characters or one-byte characters.
462const uint32_t kStringEncodingMask = 0x4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463const uint32_t kTwoByteStringTag = 0x0;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000464const uint32_t kAsciiStringTag = 0x4;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000466// If bit 7 is clear, the low-order 2 bits indicate the representation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000467// of the string.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000468const uint32_t kStringRepresentationMask = 0x03;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000469enum StringRepresentationTag {
470 kSeqStringTag = 0x0,
471 kConsStringTag = 0x1,
sgjesse@chromium.org82dbbab2010-06-02 08:57:44 +0000472 kExternalStringTag = 0x2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000473};
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000474const uint32_t kIsConsStringMask = 0x1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000476// If bit 7 is clear, then bit 3 indicates whether this two-byte
477// string actually contains ascii data.
478const uint32_t kAsciiDataHintMask = 0x08;
479const uint32_t kAsciiDataHintTag = 0x08;
480
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +0000481
482// A ConsString with an empty string as the right side is a candidate
483// for being shortcut by the garbage collector unless it is a
484// symbol. It's not common to have non-flat symbols, so we do not
485// shortcut them thereby avoiding turning symbols into strings. See
486// heap.cc and mark-compact.cc.
487const uint32_t kShortcutTypeMask =
488 kIsNotStringMask |
489 kIsSymbolMask |
490 kStringRepresentationMask;
491const uint32_t kShortcutTypeTag = kConsStringTag;
492
493
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000494enum InstanceType {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000495 // String types.
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000496 SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000497 ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000498 CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000499 CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000500 EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
501 EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
502 kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000503 EXTERNAL_ASCII_SYMBOL_TYPE =
504 kAsciiStringTag | kSymbolTag | kExternalStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000505 STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000506 ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000507 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000508 CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000509 EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
510 EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
511 kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000512 // LAST_STRING_TYPE
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000513 EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
514 PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000516 // Objects allocated in their own spaces (never in new space).
517 MAP_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 CODE_TYPE,
519 ODDBALL_TYPE,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000520 JS_GLOBAL_PROPERTY_CELL_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000521
522 // "Data", objects that cannot contain non-map-word pointers to heap
523 // objects.
524 HEAP_NUMBER_TYPE,
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000525 FOREIGN_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 BYTE_ARRAY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000527 EXTERNAL_BYTE_ARRAY_TYPE, // FIRST_EXTERNAL_ARRAY_TYPE
ager@chromium.org3811b432009-10-28 14:53:37 +0000528 EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
529 EXTERNAL_SHORT_ARRAY_TYPE,
530 EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
531 EXTERNAL_INT_ARRAY_TYPE,
532 EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000533 EXTERNAL_FLOAT_ARRAY_TYPE,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000534 EXTERNAL_DOUBLE_ARRAY_TYPE,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000535 EXTERNAL_PIXEL_ARRAY_TYPE, // LAST_EXTERNAL_ARRAY_TYPE
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000536 FIXED_DOUBLE_ARRAY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000537 FILLER_TYPE, // LAST_DATA_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000538
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000539 // Structs.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 ACCESSOR_INFO_TYPE,
541 ACCESS_CHECK_INFO_TYPE,
542 INTERCEPTOR_INFO_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 CALL_HANDLER_INFO_TYPE,
544 FUNCTION_TEMPLATE_INFO_TYPE,
545 OBJECT_TEMPLATE_INFO_TYPE,
546 SIGNATURE_INFO_TYPE,
547 TYPE_SWITCH_INFO_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000548 SCRIPT_TYPE,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000549 CODE_CACHE_TYPE,
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000550 POLYMORPHIC_CODE_CACHE_TYPE,
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000551 // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
552 // is defined. However as include/v8.h contain some of the instance type
553 // constants always having them avoids them getting different numbers
554 // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 DEBUG_INFO_TYPE,
556 BREAK_POINT_INFO_TYPE,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000558 FIXED_ARRAY_TYPE,
559 SHARED_FUNCTION_INFO_TYPE,
560
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000561 JS_MESSAGE_OBJECT_TYPE,
562
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000563 JS_VALUE_TYPE, // FIRST_NON_CALLABLE_OBJECT_TYPE, FIRST_JS_RECEIVER_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,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000570 JS_PROXY_TYPE,
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +0000571
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000572 JS_REGEXP_TYPE, // LAST_NONCALLABLE_SPEC_OBJECT_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000574 JS_FUNCTION_TYPE, // FIRST_CALLABLE_SPEC_OBJECT_TYPE
575 JS_FUNCTION_PROXY_TYPE, // LAST_CALLABLE_SPEC_OBJECT_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576
577 // Pseudo-types
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 FIRST_TYPE = 0x0,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000579 LAST_TYPE = JS_FUNCTION_PROXY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000580 INVALID_TYPE = FIRST_TYPE - 1,
581 FIRST_NONSTRING_TYPE = MAP_TYPE,
582 // Boundaries for testing for an external array.
583 FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000584 LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_PIXEL_ARRAY_TYPE,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000585 // Boundary for promotion to old data space/old pointer space.
586 LAST_DATA_TYPE = FILLER_TYPE,
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000587 // Boundary for objects represented as JSReceiver (i.e. JSObject or JSProxy).
588 // Note that there is no range for JSObject or JSProxy, since their subtypes
589 // are not continuous in this enum! The enum ranges instead reflect the
590 // external class names, where proxies are treated as either ordinary objects,
591 // or functions.
592 FIRST_JS_RECEIVER_TYPE = JS_VALUE_TYPE,
593 LAST_JS_RECEIVER_TYPE = LAST_TYPE,
594 // Boundaries for testing the types for which typeof is "object".
595 FIRST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_VALUE_TYPE,
596 LAST_NONCALLABLE_SPEC_OBJECT_TYPE = JS_REGEXP_TYPE,
597 // Boundaries for testing the types for which typeof is "function".
598 FIRST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_TYPE,
599 LAST_CALLABLE_SPEC_OBJECT_TYPE = JS_FUNCTION_PROXY_TYPE,
600 // Boundaries for testing whether the type is a JavaScript object.
601 FIRST_SPEC_OBJECT_TYPE = FIRST_NONCALLABLE_SPEC_OBJECT_TYPE,
602 LAST_SPEC_OBJECT_TYPE = LAST_CALLABLE_SPEC_OBJECT_TYPE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603};
604
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000605static const int kExternalArrayTypeCount = LAST_EXTERNAL_ARRAY_TYPE -
606 FIRST_EXTERNAL_ARRAY_TYPE + 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000608STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
609STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000610STATIC_CHECK(FOREIGN_TYPE == Internals::kForeignType);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000611
612
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613enum CompareResult {
614 LESS = -1,
615 EQUAL = 0,
616 GREATER = 1,
617
618 NOT_EQUAL = GREATER
619};
620
621
622#define DECL_BOOLEAN_ACCESSORS(name) \
623 inline bool name(); \
624 inline void set_##name(bool value); \
625
626
ager@chromium.org32912102009-01-16 10:38:43 +0000627#define DECL_ACCESSORS(name, type) \
628 inline type* name(); \
629 inline void set_##name(type* value, \
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000630 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631
632
633class StringStream;
634class ObjectVisitor;
635
636struct ValueInfo : public Malloced {
637 ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
638 InstanceType type;
639 Object* ptr;
640 const char* str;
641 double number;
642};
643
644
645// A template-ized version of the IsXXX functions.
646template <class C> static inline bool Is(Object* obj);
647
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000648class Failure;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000649
lrn@chromium.org303ada72010-10-27 09:33:13 +0000650class MaybeObject BASE_EMBEDDED {
651 public:
652 inline bool IsFailure();
653 inline bool IsRetryAfterGC();
654 inline bool IsOutOfMemory();
655 inline bool IsException();
656 INLINE(bool IsTheHole());
657 inline bool ToObject(Object** obj) {
658 if (IsFailure()) return false;
659 *obj = reinterpret_cast<Object*>(this);
660 return true;
661 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000662 inline Failure* ToFailureUnchecked() {
663 ASSERT(IsFailure());
664 return reinterpret_cast<Failure*>(this);
665 }
lrn@chromium.org303ada72010-10-27 09:33:13 +0000666 inline Object* ToObjectUnchecked() {
667 ASSERT(!IsFailure());
668 return reinterpret_cast<Object*>(this);
669 }
670 inline Object* ToObjectChecked() {
671 CHECK(!IsFailure());
672 return reinterpret_cast<Object*>(this);
673 }
674
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000675 template<typename T>
676 inline bool To(T** obj) {
677 if (IsFailure()) return false;
678 *obj = T::cast(reinterpret_cast<Object*>(this));
679 return true;
680 }
681
whesse@chromium.org023421e2010-12-21 12:19:12 +0000682#ifdef OBJECT_PRINT
lrn@chromium.org303ada72010-10-27 09:33:13 +0000683 // Prints this object with details.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000684 inline void Print() {
685 Print(stdout);
686 };
687 inline void PrintLn() {
688 PrintLn(stdout);
689 }
690 void Print(FILE* out);
691 void PrintLn(FILE* out);
692#endif
693#ifdef DEBUG
lrn@chromium.org303ada72010-10-27 09:33:13 +0000694 // Verifies the object.
695 void Verify();
696#endif
697};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000699
700#define OBJECT_TYPE_LIST(V) \
701 V(Smi) \
702 V(HeapObject) \
703 V(Number) \
704
705#define HEAP_OBJECT_TYPE_LIST(V) \
706 V(HeapNumber) \
707 V(String) \
708 V(Symbol) \
709 V(SeqString) \
710 V(ExternalString) \
711 V(ConsString) \
712 V(ExternalTwoByteString) \
713 V(ExternalAsciiString) \
714 V(SeqTwoByteString) \
715 V(SeqAsciiString) \
716 \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000717 V(ExternalArray) \
718 V(ExternalByteArray) \
719 V(ExternalUnsignedByteArray) \
720 V(ExternalShortArray) \
721 V(ExternalUnsignedShortArray) \
722 V(ExternalIntArray) \
723 V(ExternalUnsignedIntArray) \
724 V(ExternalFloatArray) \
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000725 V(ExternalDoubleArray) \
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +0000726 V(ExternalPixelArray) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000727 V(ByteArray) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000728 V(JSReceiver) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000729 V(JSObject) \
730 V(JSContextExtensionObject) \
731 V(Map) \
732 V(DescriptorArray) \
733 V(DeoptimizationInputData) \
734 V(DeoptimizationOutputData) \
735 V(FixedArray) \
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000736 V(FixedDoubleArray) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000737 V(Context) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000738 V(GlobalContext) \
739 V(JSFunction) \
740 V(Code) \
741 V(Oddball) \
742 V(SharedFunctionInfo) \
743 V(JSValue) \
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000744 V(JSMessageObject) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000745 V(StringWrapper) \
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000746 V(Foreign) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000747 V(Boolean) \
748 V(JSArray) \
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000749 V(JSProxy) \
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000750 V(JSFunctionProxy) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000751 V(JSRegExp) \
752 V(HashTable) \
753 V(Dictionary) \
754 V(SymbolTable) \
755 V(JSFunctionResultCache) \
756 V(NormalizedMapCache) \
757 V(CompilationCacheTable) \
758 V(CodeCacheHashTable) \
jkummerow@chromium.orge297f592011-06-08 10:05:15 +0000759 V(PolymorphicCodeCacheHashTable) \
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000760 V(MapCache) \
761 V(Primitive) \
762 V(GlobalObject) \
763 V(JSGlobalObject) \
764 V(JSBuiltinsObject) \
765 V(JSGlobalProxy) \
766 V(UndetectableObject) \
767 V(AccessCheckNeeded) \
768 V(JSGlobalPropertyCell) \
769
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000770// Object is the abstract superclass for all classes in the
771// object hierarchy.
772// Object does not use any virtual functions to avoid the
773// allocation of the C++ vtable.
774// Since Smi and Failure are subclasses of Object no
775// data members can be present in Object.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000776class Object : public MaybeObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 public:
778 // Type testing.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000779#define IS_TYPE_FUNCTION_DECL(type_) inline bool Is##type_();
780 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
781 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
782#undef IS_TYPE_FUNCTION_DECL
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000783
784 // Returns true if this object is an instance of the specified
785 // function template.
sgjesse@chromium.org900d3b72009-08-07 11:24:25 +0000786 inline bool IsInstanceOf(FunctionTemplateInfo* type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000787
788 inline bool IsStruct();
789#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
790 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
791#undef DECLARE_STRUCT_PREDICATE
792
793 // Oddball testing.
794 INLINE(bool IsUndefined());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000795 INLINE(bool IsNull());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000796 INLINE(bool IsTheHole()); // Shadows MaybeObject's implementation.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797 INLINE(bool IsTrue());
798 INLINE(bool IsFalse());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +0000799 inline bool IsArgumentsMarker();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000800
801 // Extract the number.
802 inline double Number();
803
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000804 // Returns true if the object is of the correct type to be used as a
805 // implementation of a JSObject's elements.
806 inline bool HasValidElements();
807
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000808 inline bool HasSpecificClassOf(String* name);
809
lrn@chromium.org303ada72010-10-27 09:33:13 +0000810 MUST_USE_RESULT MaybeObject* ToObject(); // ECMA-262 9.9.
811 Object* ToBoolean(); // ECMA-262 9.2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000812
813 // Convert to a JSObject if needed.
814 // global_context is used when creating wrapper object.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000815 MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816
817 // Converts this to a Smi if possible.
818 // Failure is returned otherwise.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000819 MUST_USE_RESULT inline MaybeObject* ToSmi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820
821 void Lookup(String* name, LookupResult* result);
822
823 // Property access.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000824 MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
825 MUST_USE_RESULT inline MaybeObject* GetProperty(
826 String* key,
827 PropertyAttributes* attributes);
828 MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
829 Object* receiver,
830 String* key,
831 PropertyAttributes* attributes);
832 MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
833 LookupResult* result,
834 String* key,
835 PropertyAttributes* attributes);
836 MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
837 Object* structure,
838 String* name,
839 Object* holder);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000840 MUST_USE_RESULT MaybeObject* GetPropertyWithHandler(Object* receiver,
841 String* name,
842 Object* handler);
lrn@chromium.org303ada72010-10-27 09:33:13 +0000843 MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
844 JSFunction* getter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845
lrn@chromium.org303ada72010-10-27 09:33:13 +0000846 inline MaybeObject* GetElement(uint32_t index);
847 // For use when we know that no exception can be thrown.
848 inline Object* GetElementNoExceptionThrown(uint32_t index);
849 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850
851 // Return the object's prototype (might be Heap::null_value()).
852 Object* GetPrototype();
853
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000854 // Tries to convert an object to an array index. Returns true and sets
855 // the output parameter if it succeeds.
856 inline bool ToArrayIndex(uint32_t* index);
857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 // Returns true if this is a JSValue containing a string and the index is
859 // < the length of the string. Used to implement [] on strings.
860 inline bool IsStringObjectWithCharacterAt(uint32_t index);
861
862#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 // Verify a pointer is a valid object pointer.
864 static void VerifyPointer(Object* p);
865#endif
866
867 // Prints this object without details.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000868 inline void ShortPrint() {
869 ShortPrint(stdout);
870 }
871 void ShortPrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
873 // Prints this object without details to a message accumulator.
874 void ShortPrint(StringStream* accumulator);
875
876 // Casting: This cast is only needed to satisfy macros in objects-inl.h.
877 static Object* cast(Object* value) { return value; }
878
879 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +0000880 static const int kHeaderSize = 0; // Object does not take up any space.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
882 private:
883 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
884};
885
886
887// Smi represents integer Numbers that can be stored in 31 bits.
888// Smis are immediate which means they are NOT allocated in the heap.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000889// The this pointer has the following format: [31 bit signed int] 0
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000890// For long smis it has the following format:
891// [32 bit signed int] [31 bits zero padding] 0
892// Smi stands for small integer.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893class Smi: public Object {
894 public:
895 // Returns the integer value.
896 inline int value();
897
898 // Convert a value to a Smi object.
899 static inline Smi* FromInt(int value);
900
ager@chromium.org9085a012009-05-11 19:22:57 +0000901 static inline Smi* FromIntptr(intptr_t value);
902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 // Returns whether value can be represented in a Smi.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000904 static inline bool IsValid(intptr_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
906 // Casting.
907 static inline Smi* cast(Object* object);
908
909 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000910 inline void SmiPrint() {
911 SmiPrint(stdout);
912 }
913 void SmiPrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 void SmiPrint(StringStream* accumulator);
915#ifdef DEBUG
916 void SmiVerify();
917#endif
918
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000919 static const int kMinValue = (-1 << (kSmiValueSize - 1));
920 static const int kMaxValue = -(kMinValue + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921
922 private:
923 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
924};
925
926
ager@chromium.org6f10e412009-02-13 10:11:16 +0000927// Failure is used for reporting out of memory situations and
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928// propagating exceptions through the runtime system. Failure objects
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000929// are transient and cannot occur as part of the object graph.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930//
931// Failures are a single word, encoded as follows:
932// +-------------------------+---+--+--+
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000933// |.........unused..........|sss|tt|11|
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000934// +-------------------------+---+--+--+
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000935// 7 6 4 32 10
936//
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000937//
938// The low two bits, 0-1, are the failure tag, 11. The next two bits,
939// 2-3, are a failure type tag 'tt' with possible values:
940// 00 RETRY_AFTER_GC
941// 01 EXCEPTION
942// 10 INTERNAL_ERROR
943// 11 OUT_OF_MEMORY_EXCEPTION
944//
945// The next three bits, 4-6, are an allocation space tag 'sss'. The
946// allocation space tag is 000 for all failure types except
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000947// RETRY_AFTER_GC. For RETRY_AFTER_GC, the possible values are the
948// allocation spaces (the encoding is found in globals.h).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949
950// Failure type tag info.
951const int kFailureTypeTagSize = 2;
952const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
953
lrn@chromium.org303ada72010-10-27 09:33:13 +0000954class Failure: public MaybeObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 public:
956 // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
957 enum Type {
958 RETRY_AFTER_GC = 0,
959 EXCEPTION = 1, // Returning this marker tells the real exception
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000960 // is in Isolate::pending_exception.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 INTERNAL_ERROR = 2,
962 OUT_OF_MEMORY_EXCEPTION = 3
963 };
964
965 inline Type type() const;
966
967 // Returns the space that needs to be collected for RetryAfterGC failures.
968 inline AllocationSpace allocation_space() const;
969
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000970 inline bool IsInternalError() const;
971 inline bool IsOutOfMemoryException() const;
972
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000973 static inline Failure* RetryAfterGC(AllocationSpace space);
974 static inline Failure* RetryAfterGC(); // NEW_SPACE
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975 static inline Failure* Exception();
976 static inline Failure* InternalError();
977 static inline Failure* OutOfMemoryException();
978 // Casting.
lrn@chromium.org303ada72010-10-27 09:33:13 +0000979 static inline Failure* cast(MaybeObject* object);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980
981 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +0000982 inline void FailurePrint() {
983 FailurePrint(stdout);
984 }
985 void FailurePrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 void FailurePrint(StringStream* accumulator);
987#ifdef DEBUG
988 void FailureVerify();
989#endif
990
991 private:
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000992 inline intptr_t value() const;
993 static inline Failure* Construct(Type type, intptr_t value = 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000994
995 DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
996};
997
998
kasper.lund7276f142008-07-30 08:49:36 +0000999// Heap objects typically have a map pointer in their first word. However,
1000// during GC other data (eg, mark bits, forwarding addresses) is sometimes
1001// encoded in the first word. The class MapWord is an abstraction of the
1002// value in a heap object's first word.
1003class MapWord BASE_EMBEDDED {
1004 public:
1005 // Normal state: the map word contains a map pointer.
1006
1007 // Create a map word from a map pointer.
1008 static inline MapWord FromMap(Map* map);
1009
1010 // View this map word as a map pointer.
1011 inline Map* ToMap();
1012
1013
1014 // Scavenge collection: the map word of live objects in the from space
1015 // contains a forwarding address (a heap object pointer in the to space).
1016
1017 // True if this map word is a forwarding address for a scavenge
1018 // collection. Only valid during a scavenge collection (specifically,
1019 // when all map words are heap object pointers, ie. not during a full GC).
1020 inline bool IsForwardingAddress();
1021
1022 // Create a map word from a forwarding address.
1023 static inline MapWord FromForwardingAddress(HeapObject* object);
1024
1025 // View this map word as a forwarding address.
1026 inline HeapObject* ToForwardingAddress();
1027
kasper.lund7276f142008-07-30 08:49:36 +00001028 // Marking phase of full collection: the map word of live objects is
1029 // marked, and may be marked as overflowed (eg, the object is live, its
1030 // children have not been visited, and it does not fit in the marking
1031 // stack).
1032
1033 // True if this map word's mark bit is set.
1034 inline bool IsMarked();
1035
1036 // Return this map word but with its mark bit set.
1037 inline void SetMark();
1038
1039 // Return this map word but with its mark bit cleared.
1040 inline void ClearMark();
1041
1042 // True if this map word's overflow bit is set.
1043 inline bool IsOverflowed();
1044
1045 // Return this map word but with its overflow bit set.
1046 inline void SetOverflow();
1047
1048 // Return this map word but with its overflow bit cleared.
1049 inline void ClearOverflow();
1050
1051
1052 // Compacting phase of a full compacting collection: the map word of live
1053 // objects contains an encoding of the original map address along with the
1054 // forwarding address (represented as an offset from the first live object
1055 // in the same page as the (old) object address).
1056
1057 // Create a map word from a map address and a forwarding address offset.
1058 static inline MapWord EncodeAddress(Address map_address, int offset);
1059
1060 // Return the map address encoded in this map word.
1061 inline Address DecodeMapAddress(MapSpace* map_space);
1062
1063 // Return the forwarding offset encoded in this map word.
1064 inline int DecodeOffset();
1065
1066
1067 // During serialization: the map word is used to hold an encoded
1068 // address, and possibly a mark bit (set and cleared with SetMark
1069 // and ClearMark).
1070
1071 // Create a map word from an encoded address.
1072 static inline MapWord FromEncodedAddress(Address address);
1073
1074 inline Address ToEncodedAddress();
1075
kasper.lund7276f142008-07-30 08:49:36 +00001076 // Bits used by the marking phase of the garbage collector.
1077 //
ager@chromium.org6f10e412009-02-13 10:11:16 +00001078 // The first word of a heap object is normally a map pointer. The last two
kasper.lund7276f142008-07-30 08:49:36 +00001079 // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1080 // mark an object as live and/or overflowed:
1081 // last bit = 0, marked as alive
1082 // second bit = 1, overflowed
1083 // An object is only marked as overflowed when it is marked as live while
1084 // the marking stack is overflowed.
1085 static const int kMarkingBit = 0; // marking bit
1086 static const int kMarkingMask = (1 << kMarkingBit); // marking mask
1087 static const int kOverflowBit = 1; // overflow bit
1088 static const int kOverflowMask = (1 << kOverflowBit); // overflow mask
1089
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001090 // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1091 // used.
kasper.lund7276f142008-07-30 08:49:36 +00001092 // +-----------------+------------------+-----------------+
1093 // |forwarding offset|page offset of map|page index of map|
1094 // +-----------------+------------------+-----------------+
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001095 // ^ ^ ^
1096 // | | |
1097 // | | kMapPageIndexBits
1098 // | kMapPageOffsetBits
1099 // kForwardingOffsetBits
1100 static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1101 static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1102#ifdef V8_HOST_ARCH_64_BIT
1103 static const int kMapPageIndexBits = 16;
1104#else
1105 // Use all the 32-bits to encode on a 32-bit platform.
1106 static const int kMapPageIndexBits =
1107 32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1108#endif
kasper.lund7276f142008-07-30 08:49:36 +00001109
1110 static const int kMapPageIndexShift = 0;
1111 static const int kMapPageOffsetShift =
1112 kMapPageIndexShift + kMapPageIndexBits;
1113 static const int kForwardingOffsetShift =
1114 kMapPageOffsetShift + kMapPageOffsetBits;
1115
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001116 // Bit masks covering the different parts the encoding.
1117 static const uintptr_t kMapPageIndexMask =
kasper.lund7276f142008-07-30 08:49:36 +00001118 (1 << kMapPageOffsetShift) - 1;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001119 static const uintptr_t kMapPageOffsetMask =
kasper.lund7276f142008-07-30 08:49:36 +00001120 ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001121 static const uintptr_t kForwardingOffsetMask =
kasper.lund7276f142008-07-30 08:49:36 +00001122 ~(kMapPageIndexMask | kMapPageOffsetMask);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001123
1124 private:
1125 // HeapObject calls the private constructor and directly reads the value.
1126 friend class HeapObject;
1127
1128 explicit MapWord(uintptr_t value) : value_(value) {}
1129
1130 uintptr_t value_;
kasper.lund7276f142008-07-30 08:49:36 +00001131};
1132
1133
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001134// HeapObject is the superclass for all classes describing heap allocated
1135// objects.
1136class HeapObject: public Object {
1137 public:
kasper.lund7276f142008-07-30 08:49:36 +00001138 // [map]: Contains a map which contains the object's reflective
1139 // information.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 inline Map* map();
1141 inline void set_map(Map* value);
1142
kasper.lund7276f142008-07-30 08:49:36 +00001143 // During garbage collection, the map word of a heap object does not
1144 // necessarily contain a map pointer.
1145 inline MapWord map_word();
1146 inline void set_map_word(MapWord map_word);
1147
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001148 // The Heap the object was allocated in. Used also to access Isolate.
1149 // This method can not be used during GC, it ASSERTs this.
1150 inline Heap* GetHeap();
1151 // Convenience method to get current isolate. This method can be
1152 // accessed only when its result is the same as
1153 // Isolate::Current(), it ASSERTs this. See also comment for GetHeap.
1154 inline Isolate* GetIsolate();
1155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156 // Converts an address to a HeapObject pointer.
1157 static inline HeapObject* FromAddress(Address address);
1158
1159 // Returns the address of this HeapObject.
1160 inline Address address();
1161
1162 // Iterates over pointers contained in the object (including the Map)
1163 void Iterate(ObjectVisitor* v);
1164
1165 // Iterates over all pointers contained in the object except the
1166 // first map pointer. The object type is given in the first
1167 // parameter. This function does not access the map pointer in the
1168 // object, and so is safe to call while the map pointer is modified.
1169 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 // Returns the heap object's size in bytes
1172 inline int Size();
1173
1174 // Given a heap object's map pointer, returns the heap size in bytes
1175 // Useful when the map pointer field is used for other purposes.
1176 // GC internal.
1177 inline int SizeFromMap(Map* map);
1178
kasper.lund7276f142008-07-30 08:49:36 +00001179 // Support for the marking heap objects during the marking phase of GC.
1180 // True if the object is marked live.
1181 inline bool IsMarked();
1182
1183 // Mutate this object's map pointer to indicate that the object is live.
1184 inline void SetMark();
1185
1186 // Mutate this object's map pointer to remove the indication that the
1187 // object is live (ie, partially restore the map pointer).
1188 inline void ClearMark();
1189
1190 // True if this object is marked as overflowed. Overflowed objects have
1191 // been reached and marked during marking of the heap, but their children
1192 // have not necessarily been marked and they have not been pushed on the
1193 // marking stack.
1194 inline bool IsOverflowed();
1195
1196 // Mutate this object's map pointer to indicate that the object is
1197 // overflowed.
1198 inline void SetOverflow();
1199
1200 // Mutate this object's map pointer to remove the indication that the
1201 // object is overflowed (ie, partially restore the map pointer).
1202 inline void ClearOverflow();
1203
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001204 // Returns the field at offset in obj, as a read/write Object* reference.
1205 // Does no checking, and is safe to use during GC, while maps are invalid.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001206 // Does not invoke write barrier, so should only be assigned to
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00001207 // during marking GC.
1208 static inline Object** RawField(HeapObject* obj, int offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209
1210 // Casting.
1211 static inline HeapObject* cast(Object* obj);
1212
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001213 // Return the write barrier mode for this. Callers of this function
1214 // must be able to present a reference to an AssertNoAllocation
1215 // object as a sign that they are not going to use this function
1216 // from code that allocates and thus invalidates the returned write
1217 // barrier mode.
1218 inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001219
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 // Dispatched behavior.
1221 void HeapObjectShortPrint(StringStream* accumulator);
whesse@chromium.org023421e2010-12-21 12:19:12 +00001222#ifdef OBJECT_PRINT
1223 inline void HeapObjectPrint() {
1224 HeapObjectPrint(stdout);
1225 }
1226 void HeapObjectPrint(FILE* out);
1227#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 void HeapObjectVerify();
1230 inline void VerifyObjectField(int offset);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001231 inline void VerifySmiField(int offset);
whesse@chromium.org023421e2010-12-21 12:19:12 +00001232#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001233
whesse@chromium.org023421e2010-12-21 12:19:12 +00001234#ifdef OBJECT_PRINT
1235 void PrintHeader(FILE* out, const char* id);
1236#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237
whesse@chromium.org023421e2010-12-21 12:19:12 +00001238#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 // Verify a pointer is a valid HeapObject pointer that points to object
1240 // areas in the heap.
1241 static void VerifyHeapPointer(Object* p);
1242#endif
1243
1244 // Layout description.
1245 // First field in a heap object is map.
ager@chromium.org236ad962008-09-25 09:45:57 +00001246 static const int kMapOffset = Object::kHeaderSize;
1247 static const int kHeaderSize = kMapOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001249 STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1250
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 protected:
1252 // helpers for calling an ObjectVisitor to iterate over pointers in the
1253 // half-open range [start, end) specified as integer offsets
1254 inline void IteratePointers(ObjectVisitor* v, int start, int end);
1255 // as above, for the single element at "offset"
1256 inline void IteratePointer(ObjectVisitor* v, int offset);
1257
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 private:
1259 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1260};
1261
1262
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001263#define SLOT_ADDR(obj, offset) \
1264 reinterpret_cast<Object**>((obj)->address() + offset)
1265
1266// This class describes a body of an object of a fixed size
1267// in which all pointer fields are located in the [start_offset, end_offset)
1268// interval.
1269template<int start_offset, int end_offset, int size>
1270class FixedBodyDescriptor {
1271 public:
1272 static const int kStartOffset = start_offset;
1273 static const int kEndOffset = end_offset;
1274 static const int kSize = size;
1275
1276 static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1277
1278 template<typename StaticVisitor>
1279 static inline void IterateBody(HeapObject* obj) {
1280 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1281 SLOT_ADDR(obj, end_offset));
1282 }
1283};
1284
1285
1286// This class describes a body of an object of a variable size
1287// in which all pointer fields are located in the [start_offset, object_size)
1288// interval.
1289template<int start_offset>
1290class FlexibleBodyDescriptor {
1291 public:
1292 static const int kStartOffset = start_offset;
1293
1294 static inline void IterateBody(HeapObject* obj,
1295 int object_size,
1296 ObjectVisitor* v);
1297
1298 template<typename StaticVisitor>
1299 static inline void IterateBody(HeapObject* obj, int object_size) {
1300 StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1301 SLOT_ADDR(obj, object_size));
1302 }
1303};
1304
1305#undef SLOT_ADDR
1306
1307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308// The HeapNumber class describes heap allocated numbers that cannot be
1309// represented in a Smi (small integer)
1310class HeapNumber: public HeapObject {
1311 public:
1312 // [value]: number value.
1313 inline double value();
1314 inline void set_value(double value);
1315
1316 // Casting.
1317 static inline HeapNumber* cast(Object* obj);
1318
1319 // Dispatched behavior.
1320 Object* HeapNumberToBoolean();
whesse@chromium.org023421e2010-12-21 12:19:12 +00001321 inline void HeapNumberPrint() {
1322 HeapNumberPrint(stdout);
1323 }
1324 void HeapNumberPrint(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 void HeapNumberPrint(StringStream* accumulator);
1326#ifdef DEBUG
1327 void HeapNumberVerify();
1328#endif
1329
whesse@chromium.orgcec079d2010-03-22 14:44:04 +00001330 inline int get_exponent();
1331 inline int get_sign();
1332
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00001334 static const int kValueOffset = HeapObject::kHeaderSize;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001335 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
1336 // is a mixture of sign, exponent and mantissa. Our current platforms are all
1337 // little endian apart from non-EABI arm which is little endian with big
1338 // endian floating point word ordering!
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001339 static const int kMantissaOffset = kValueOffset;
1340 static const int kExponentOffset = kValueOffset + 4;
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00001341
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 static const int kSize = kValueOffset + kDoubleSize;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001343 static const uint32_t kSignMask = 0x80000000u;
1344 static const uint32_t kExponentMask = 0x7ff00000u;
1345 static const uint32_t kMantissaMask = 0xfffffu;
ager@chromium.orgac091b72010-05-05 07:34:42 +00001346 static const int kMantissaBits = 52;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00001347 static const int kExponentBits = 11;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001348 static const int kExponentBias = 1023;
1349 static const int kExponentShift = 20;
1350 static const int kMantissaBitsInTopWord = 20;
1351 static const int kNonMantissaBitsInTopWord = 12;
1352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 private:
1354 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1355};
1356
1357
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001358// JSReceiver includes types on which properties can be defined, i.e.,
1359// JSObject and JSProxy.
1360class JSReceiver: public HeapObject {
1361 public:
1362 // Casting.
1363 static inline JSReceiver* cast(Object* obj);
1364
1365 // Can cause GC.
1366 MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1367 Object* value,
1368 PropertyAttributes attributes,
1369 StrictModeFlag strict_mode);
1370 MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1371 String* key,
1372 Object* value,
1373 PropertyAttributes attributes,
1374 StrictModeFlag strict_mode);
1375
1376 // Returns the class name ([[Class]] property in the specification).
1377 String* class_name();
1378
1379 // Returns the constructor name (the name (possibly, inferred name) of the
1380 // function that was used to instantiate the object).
1381 String* constructor_name();
1382
1383 inline PropertyAttributes GetPropertyAttribute(String* name);
1384 PropertyAttributes GetPropertyAttributeWithReceiver(JSReceiver* receiver,
1385 String* name);
1386 PropertyAttributes GetLocalPropertyAttribute(String* name);
1387
1388 // Can cause a GC.
1389 bool HasProperty(String* name) {
1390 return GetPropertyAttribute(name) != ABSENT;
1391 }
1392
1393 // Can cause a GC.
1394 bool HasLocalProperty(String* name) {
1395 return GetLocalPropertyAttribute(name) != ABSENT;
1396 }
1397
1398 // Return the object's prototype (might be Heap::null_value()).
1399 inline Object* GetPrototype();
1400
1401 // Set the object's prototype (only JSReceiver and null are allowed).
1402 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1403 bool skip_hidden_prototypes);
1404
1405 // Lookup a property. If found, the result is valid and has
1406 // detailed information.
1407 void LocalLookup(String* name, LookupResult* result);
1408 void Lookup(String* name, LookupResult* result);
1409
1410 private:
1411 PropertyAttributes GetPropertyAttribute(JSReceiver* receiver,
1412 LookupResult* result,
1413 String* name,
1414 bool continue_search);
1415
1416 DISALLOW_IMPLICIT_CONSTRUCTORS(JSReceiver);
1417};
1418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419// The JSObject describes real heap allocated JavaScript objects with
1420// properties.
1421// Note that the map of JSObject changes during execution to enable inline
1422// caching.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001423class JSObject: public JSReceiver {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 public:
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001425 enum DeleteMode {
1426 NORMAL_DELETION,
1427 STRICT_DELETION,
1428 FORCE_DELETION
1429 };
1430
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001431 enum ElementsKind {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001432 // The "fast" kind for tagged values. Must be first to make it possible
1433 // to efficiently check maps if they have fast elements.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001434 FAST_ELEMENTS,
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001435
1436 // The "fast" kind for unwrapped, non-tagged double values.
1437 FAST_DOUBLE_ELEMENTS,
1438
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001439 // The "slow" kind.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001440 DICTIONARY_ELEMENTS,
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001441 // The "fast" kind for external arrays
ager@chromium.org3811b432009-10-28 14:53:37 +00001442 EXTERNAL_BYTE_ELEMENTS,
1443 EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1444 EXTERNAL_SHORT_ELEMENTS,
1445 EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1446 EXTERNAL_INT_ELEMENTS,
1447 EXTERNAL_UNSIGNED_INT_ELEMENTS,
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001448 EXTERNAL_FLOAT_ELEMENTS,
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001449 EXTERNAL_DOUBLE_ELEMENTS,
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001450 EXTERNAL_PIXEL_ELEMENTS,
1451
1452 // Derived constants from ElementsKind
1453 FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_BYTE_ELEMENTS,
1454 LAST_EXTERNAL_ARRAY_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS,
1455 FIRST_ELEMENTS_KIND = FAST_ELEMENTS,
1456 LAST_ELEMENTS_KIND = EXTERNAL_PIXEL_ELEMENTS
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001457 };
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001458
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001459 static const int kElementsKindCount =
1460 LAST_ELEMENTS_KIND - FIRST_ELEMENTS_KIND + 1;
1461
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 // [properties]: Backing storage for properties.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001463 // properties is a FixedArray in the fast case and a Dictionary in the
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001464 // slow case.
1465 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 inline void initialize_properties();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 inline bool HasFastProperties();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001468 inline StringDictionary* property_dictionary(); // Gets slow properties.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001470 // [elements]: The elements (properties with names that are integers).
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001471 //
1472 // Elements can be in two general modes: fast and slow. Each mode
1473 // corrensponds to a set of object representations of elements that
1474 // have something in common.
1475 //
1476 // In the fast mode elements is a FixedArray and so each element can
1477 // be quickly accessed. This fact is used in the generated code. The
1478 // elements array can have one of the two maps in this mode:
1479 // fixed_array_map or fixed_cow_array_map (for copy-on-write
1480 // arrays). In the latter case the elements array may be shared by a
1481 // few objects and so before writing to any element the array must
1482 // be copied. Use EnsureWritableFastElements in this case.
1483 //
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001484 // In the slow mode elements is either a NumberDictionary or an ExternalArray.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001485 DECL_ACCESSORS(elements, HeapObject)
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001486 inline void initialize_elements();
lrn@chromium.org303ada72010-10-27 09:33:13 +00001487 MUST_USE_RESULT inline MaybeObject* ResetElements();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001488 inline ElementsKind GetElementsKind();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001489 inline bool HasFastElements();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001490 inline bool HasFastDoubleElements();
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001491 inline bool HasDictionaryElements();
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00001492 inline bool HasExternalPixelElements();
ager@chromium.org3811b432009-10-28 14:53:37 +00001493 inline bool HasExternalArrayElements();
1494 inline bool HasExternalByteElements();
1495 inline bool HasExternalUnsignedByteElements();
1496 inline bool HasExternalShortElements();
1497 inline bool HasExternalUnsignedShortElements();
1498 inline bool HasExternalIntElements();
1499 inline bool HasExternalUnsignedIntElements();
1500 inline bool HasExternalFloatElements();
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00001501 inline bool HasExternalDoubleElements();
ager@chromium.org5c838252010-02-19 08:53:10 +00001502 inline bool AllowsSetElementsLength();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001503 inline NumberDictionary* element_dictionary(); // Gets slow elements.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001504 // Requires: this->HasFastElements().
lrn@chromium.org303ada72010-10-27 09:33:13 +00001505 MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506
ager@chromium.org5ec48922009-05-05 07:25:34 +00001507 // Collects elements starting at index 0.
1508 // Undefined values are placed after non-undefined values.
1509 // Returns the number of non-undefined values.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001510 MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
ager@chromium.org5ec48922009-05-05 07:25:34 +00001511 // As PrepareElementsForSort, but only on objects where elements is
1512 // a dictionary, and it will stay a dictionary.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001513 MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
ager@chromium.org5ec48922009-05-05 07:25:34 +00001514
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001515 MUST_USE_RESULT MaybeObject* SetPropertyForResult(LookupResult* result,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001516 String* key,
1517 Object* value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001518 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001519 StrictModeFlag strict_mode);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001520 MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1521 LookupResult* result,
1522 String* name,
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001523 Object* value,
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001524 bool check_prototype,
1525 StrictModeFlag strict_mode);
1526 MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(
1527 Object* structure,
1528 String* name,
1529 Object* value,
1530 JSObject* holder,
1531 StrictModeFlag strict_mode);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001532 MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1533 Object* value);
1534 MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1535 String* name,
1536 Object* value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001537 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001538 StrictModeFlag strict_mode);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001539 MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1540 String* name,
1541 Object* value,
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001542 PropertyAttributes attributes,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001543 StrictModeFlag strict_mode);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00001544 MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
lrn@chromium.org303ada72010-10-27 09:33:13 +00001545 String* key,
1546 Object* value,
1547 PropertyAttributes attributes);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001548
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001549 // Retrieve a value in a normalized object given a lookup result.
1550 // Handles the special representation of JS global objects.
1551 Object* GetNormalizedProperty(LookupResult* result);
1552
1553 // Sets the property value in a normalized object given a lookup result.
1554 // Handles the special representation of JS global objects.
1555 Object* SetNormalizedProperty(LookupResult* result, Object* value);
1556
1557 // Sets the property value in a normalized object given (key, value, details).
1558 // Handles the special representation of JS global objects.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001559 MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1560 Object* value,
1561 PropertyDetails details);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001562
1563 // Deletes the named property in a normalized object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001564 MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1565 DeleteMode mode);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001566
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 // Retrieve interceptors.
1568 InterceptorInfo* GetNamedInterceptor();
1569 InterceptorInfo* GetIndexedInterceptor();
1570
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001571 // Used from JSReceiver.
1572 PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1573 String* name,
1574 bool continue_search);
1575 PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1576 String* name,
1577 bool continue_search);
1578 PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1579 Object* receiver,
1580 LookupResult* result,
1581 String* name,
1582 bool continue_search);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583
lrn@chromium.org303ada72010-10-27 09:33:13 +00001584 MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1585 bool is_getter,
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001586 Object* fun,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001587 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588 Object* LookupAccessor(String* name, bool is_getter);
1589
lrn@chromium.org303ada72010-10-27 09:33:13 +00001590 MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001591
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 // Used from Object::GetProperty().
lrn@chromium.org303ada72010-10-27 09:33:13 +00001593 MaybeObject* GetPropertyWithFailedAccessCheck(
1594 Object* receiver,
1595 LookupResult* result,
1596 String* name,
1597 PropertyAttributes* attributes);
1598 MaybeObject* GetPropertyWithInterceptor(
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001599 JSReceiver* receiver,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001600 String* name,
1601 PropertyAttributes* attributes);
1602 MaybeObject* GetPropertyPostInterceptor(
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001603 JSReceiver* receiver,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001604 String* name,
1605 PropertyAttributes* attributes);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001606 MaybeObject* GetLocalPropertyPostInterceptor(JSReceiver* receiver,
lrn@chromium.org303ada72010-10-27 09:33:13 +00001607 String* name,
1608 PropertyAttributes* attributes);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00001609
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00001610 // Returns true if this is an instance of an api function and has
1611 // been modified since it was created. May give false positives.
1612 bool IsDirty();
1613
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001614 // If the receiver is a JSGlobalProxy this method will return its prototype,
1615 // otherwise the result is the receiver itself.
1616 inline Object* BypassGlobalProxy();
1617
1618 // Accessors for hidden properties object.
1619 //
1620 // Hidden properties are not local properties of the object itself.
1621 // Instead they are stored on an auxiliary JSObject stored as a local
1622 // property with a special name Heap::hidden_symbol(). But if the
1623 // receiver is a JSGlobalProxy then the auxiliary object is a property
1624 // of its prototype.
1625 //
1626 // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1627 // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1628 // holder.
1629 //
1630 // These accessors do not touch interceptors or accessors.
1631 inline bool HasHiddenPropertiesObject();
1632 inline Object* GetHiddenPropertiesObject();
lrn@chromium.org303ada72010-10-27 09:33:13 +00001633 MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1634 Object* hidden_obj);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001635
lrn@chromium.org303ada72010-10-27 09:33:13 +00001636 MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1637 MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638
1639 // Tests for the fast common case for property enumeration.
1640 bool IsSimpleEnum();
1641
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001642 // Do we want to keep the elements in fast case when increasing the
1643 // capacity?
1644 bool ShouldConvertToSlowElements(int new_capacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 // Returns true if the backing storage for the slow-case elements of
1646 // this object takes up nearly as much space as a fast-case backing
1647 // storage would. In that case the JSObject should have fast
1648 // elements.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001649 bool ShouldConvertToFastElements();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001650 // Returns true if the elements of JSObject contains only values that can be
1651 // represented in a FixedDoubleArray.
1652 bool ShouldConvertToFastDoubleElements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001653
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 // Tells whether the index'th element is present.
1655 inline bool HasElement(uint32_t index);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001656 bool HasElementWithReceiver(JSReceiver* receiver, uint32_t index);
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001657
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001658 // Computes the new capacity when expanding the elements of a JSObject.
1659 static int NewElementsCapacity(int old_capacity) {
1660 // (old_capacity + 50%) + 16
1661 return old_capacity + (old_capacity >> 1) + 16;
1662 }
1663
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001664 // Tells whether the index'th element is present and how it is stored.
1665 enum LocalElementType {
1666 // There is no element with given index.
1667 UNDEFINED_ELEMENT,
1668
1669 // Element with given index is handled by interceptor.
1670 INTERCEPTED_ELEMENT,
1671
1672 // Element with given index is character in string.
1673 STRING_CHARACTER_ELEMENT,
1674
1675 // Element with given index is stored in fast backing store.
1676 FAST_ELEMENT,
1677
1678 // Element with given index is stored in slow backing store.
1679 DICTIONARY_ELEMENT
1680 };
1681
1682 LocalElementType HasLocalElement(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001684 bool HasElementWithInterceptor(JSReceiver* receiver, uint32_t index);
1685 bool HasElementPostInterceptor(JSReceiver* receiver, uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001687 MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1688 Object* value,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001689 StrictModeFlag strict_mode,
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001690 bool check_prototype = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001692 MUST_USE_RESULT MaybeObject* SetFastDoubleElement(
1693 uint32_t index,
1694 Object* value,
1695 StrictModeFlag strict_mode,
1696 bool check_prototype = true);
1697
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 // Set the index'th array element.
1699 // A Failure object is returned if GC is needed.
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001700 MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1701 Object* value,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001702 StrictModeFlag strict_mode,
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001703 bool check_prototype = true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704
1705 // Returns the index'th element.
1706 // The undefined object if index is out of bounds.
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001707 MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
1708 MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00001710 // Get external element value at index if there is one and undefined
1711 // otherwise. Can return a failure if allocation of a heap number
1712 // failed.
1713 MaybeObject* GetExternalElement(uint32_t index);
1714
lrn@chromium.org303ada72010-10-27 09:33:13 +00001715 MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1716 int length);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001717 MUST_USE_RESULT MaybeObject* SetFastDoubleElementsCapacityAndLength(
1718 int capacity,
1719 int length);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001720 MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721
1722 // Lookup interceptors are used for handling properties controlled by host
1723 // objects.
1724 inline bool HasNamedInterceptor();
1725 inline bool HasIndexedInterceptor();
1726
1727 // Support functions for v8 api (needed for correct interceptor behavior).
1728 bool HasRealNamedProperty(String* key);
1729 bool HasRealElementProperty(uint32_t index);
1730 bool HasRealNamedCallbackProperty(String* key);
1731
1732 // Initializes the array to a certain length
lrn@chromium.org303ada72010-10-27 09:33:13 +00001733 MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734
1735 // Get the header size for a JSObject. Used to compute the index of
1736 // internal fields as well as the number of internal fields.
1737 inline int GetHeaderSize();
1738
1739 inline int GetInternalFieldCount();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001740 inline int GetInternalFieldOffset(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 inline Object* GetInternalField(int index);
1742 inline void SetInternalField(int index, Object* value);
1743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 // Lookup a property. If found, the result is valid and has
1745 // detailed information.
1746 void LocalLookup(String* name, LookupResult* result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747
1748 // The following lookup functions skip interceptors.
1749 void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1750 void LookupRealNamedProperty(String* name, LookupResult* result);
1751 void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1752 void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00001753 MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001754 uint32_t index, Object* value, bool* found, StrictModeFlag strict_mode);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001755 void LookupCallback(String* name, LookupResult* result);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756
1757 // Returns the number of properties on this object filtering out properties
1758 // with the specified attributes (ignoring interceptors).
1759 int NumberOfLocalProperties(PropertyAttributes filter);
1760 // Returns the number of enumerable properties (ignoring interceptors).
1761 int NumberOfEnumProperties();
ager@chromium.orgddb913d2009-01-27 10:01:48 +00001762 // Fill in details for properties into storage starting at the specified
1763 // index.
1764 void GetLocalPropertyNames(FixedArray* storage, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765
1766 // Returns the number of properties on this object filtering out properties
1767 // with the specified attributes (ignoring interceptors).
1768 int NumberOfLocalElements(PropertyAttributes filter);
1769 // Returns the number of enumerable elements (ignoring interceptors).
1770 int NumberOfEnumElements();
1771 // Returns the number of elements on this object filtering out elements
1772 // with the specified attributes (ignoring interceptors).
1773 int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1774 // Count and fill in the enumerable elements into storage.
1775 // (storage->length() == NumberOfEnumElements()).
1776 // If storage is NULL, will count the elements without adding
1777 // them to any storage.
1778 // Returns the number of enumerable elements.
1779 int GetEnumElementKeys(FixedArray* storage);
1780
1781 // Add a property to a fast-case object using a map transition to
1782 // new_map.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001783 MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1784 String* name,
1785 Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786
1787 // Add a constant function property to a fast-case object.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001788 // This leaves a CONSTANT_TRANSITION in the old map, and
1789 // if it is called on a second object with this map, a
1790 // normal property is added instead, with a map transition.
1791 // This avoids the creation of many maps with the same constant
1792 // function, all orphaned.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001793 MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1794 String* name,
1795 JSFunction* function,
1796 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797
lrn@chromium.org303ada72010-10-27 09:33:13 +00001798 MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1799 String* name,
1800 Object* value,
1801 PropertyAttributes attributes);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001802
1803 // Converts a descriptor of any other type to a real field,
1804 // backed by the properties array. Descriptors of visible
1805 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1806 // Converts the descriptor on the original object's map to a
1807 // map transition, and the the new field is on the object's new map.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001808 MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
ager@chromium.org7c537e22008-10-16 08:43:32 +00001809 String* name,
1810 Object* new_value,
1811 PropertyAttributes attributes);
1812
1813 // Converts a descriptor of any other type to a real field,
1814 // backed by the properties array. Descriptors of visible
1815 // types, such as CONSTANT_FUNCTION, keep their enumeration order.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001816 MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1817 String* name,
1818 Object* new_value,
1819 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820
1821 // Add a property to a fast-case object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001822 MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1823 Object* value,
1824 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825
1826 // Add a property to a slow-case object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001827 MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1828 Object* value,
1829 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830
1831 // Add a property to an object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001832 MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1833 Object* value,
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001834 PropertyAttributes attributes,
1835 StrictModeFlag strict_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836
1837 // Convert the object to use the canonical dictionary
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001838 // representation. If the object is expected to have additional properties
1839 // added this number can be indicated to have the backing store allocated to
1840 // an initial capacity for holding these properties.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001841 MUST_USE_RESULT MaybeObject* NormalizeProperties(
1842 PropertyNormalizationMode mode,
1843 int expected_additional_properties);
1844 MUST_USE_RESULT MaybeObject* NormalizeElements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845
lrn@chromium.org303ada72010-10-27 09:33:13 +00001846 MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001847
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 // Transform slow named properties to fast variants.
1849 // Returns failure if allocation failed.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001850 MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1851 int unused_property_fields);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852
ager@chromium.org7c537e22008-10-16 08:43:32 +00001853 // Access fast-case object properties at index.
1854 inline Object* FastPropertyAt(int index);
1855 inline Object* FastPropertyAtPut(int index, Object* value);
1856
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001857 // Access to in object properties.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001858 inline int GetInObjectPropertyOffset(int index);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001859 inline Object* InObjectPropertyAt(int index);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001860 inline Object* InObjectPropertyAtPut(int index,
1861 Object* value,
1862 WriteBarrierMode mode
1863 = UPDATE_WRITE_BARRIER);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001864
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 // initializes the body after properties slot, properties slot is
1866 // initialized by set_properties
1867 // Note: this call does not update write barrier, it is caller's
1868 // reponsibility to ensure that *v* can be collected without WB here.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001869 inline void InitializeBody(int object_size, Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870
1871 // Check whether this object references another object
1872 bool ReferencesObject(Object* obj);
1873
1874 // Casting.
1875 static inline JSObject* cast(Object* obj);
1876
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001877 // Disalow further properties to be added to the object.
lrn@chromium.org303ada72010-10-27 09:33:13 +00001878 MUST_USE_RESULT MaybeObject* PreventExtensions();
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001879
1880
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 // Dispatched behavior.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882 void JSObjectShortPrint(StringStream* accumulator);
whesse@chromium.org023421e2010-12-21 12:19:12 +00001883#ifdef OBJECT_PRINT
1884 inline void JSObjectPrint() {
1885 JSObjectPrint(stdout);
1886 }
1887 void JSObjectPrint(FILE* out);
1888#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 void JSObjectVerify();
whesse@chromium.org023421e2010-12-21 12:19:12 +00001891#endif
1892#ifdef OBJECT_PRINT
1893 inline void PrintProperties() {
1894 PrintProperties(stdout);
1895 }
1896 void PrintProperties(FILE* out);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897
whesse@chromium.org023421e2010-12-21 12:19:12 +00001898 inline void PrintElements() {
1899 PrintElements(stdout);
1900 }
1901 void PrintElements(FILE* out);
1902#endif
1903
1904#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 // Structure for collecting spill information about JSObjects.
1906 class SpillInformation {
1907 public:
1908 void Clear();
1909 void Print();
1910 int number_of_objects_;
1911 int number_of_objects_with_fast_properties_;
1912 int number_of_objects_with_fast_elements_;
1913 int number_of_fast_used_fields_;
1914 int number_of_fast_unused_fields_;
1915 int number_of_slow_used_properties_;
1916 int number_of_slow_unused_properties_;
1917 int number_of_fast_used_elements_;
1918 int number_of_fast_unused_elements_;
1919 int number_of_slow_used_elements_;
1920 int number_of_slow_unused_elements_;
1921 };
1922
1923 void IncrementSpillStatistics(SpillInformation* info);
1924#endif
1925 Object* SlowReverseLookup(Object* value);
1926
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00001927 // Maximal number of fast properties for the JSObject. Used to
1928 // restrict the number of map transitions to avoid an explosion in
1929 // the number of maps for objects used as dictionaries.
1930 inline int MaxFastProperties();
1931
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001932 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1933 // Also maximal value of JSArray's length property.
1934 static const uint32_t kMaxElementCount = 0xffffffffu;
1935
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 static const uint32_t kMaxGap = 1024;
1937 static const int kMaxFastElementsLength = 5000;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001938 static const int kInitialMaxFastElementArray = 100000;
whesse@chromium.org023421e2010-12-21 12:19:12 +00001939 static const int kMaxFastProperties = 12;
ager@chromium.org7c537e22008-10-16 08:43:32 +00001940 static const int kMaxInstanceSize = 255 * kPointerSize;
1941 // When extending the backing storage for property values, we increase
1942 // its size by more than the 1 entry necessary, so sequentially adding fields
1943 // to the same object requires fewer allocations and copies.
1944 static const int kFieldsAdded = 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945
1946 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00001947 static const int kPropertiesOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1949 static const int kHeaderSize = kElementsOffset + kPointerSize;
1950
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001951 STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1952
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001953 class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1954 public:
1955 static inline int SizeOf(Map* map, HeapObject* object);
1956 };
1957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 private:
lrn@chromium.org303ada72010-10-27 09:33:13 +00001959 MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1960 Object* structure,
1961 uint32_t index,
1962 Object* holder);
1963 MaybeObject* SetElementWithCallback(Object* structure,
1964 uint32_t index,
1965 Object* value,
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00001966 JSObject* holder,
1967 StrictModeFlag strict_mode);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001968 MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
1969 uint32_t index,
1970 Object* value,
1971 StrictModeFlag strict_mode,
1972 bool check_prototype);
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001973 MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
1974 uint32_t index,
1975 Object* value,
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00001976 StrictModeFlag strict_mode,
lrn@chromium.org5d00b602011-01-05 09:51:43 +00001977 bool check_prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001979 MaybeObject* GetElementPostInterceptor(Object* receiver, uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980
lrn@chromium.org303ada72010-10-27 09:33:13 +00001981 MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1982 DeleteMode mode);
1983 MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001984
lrn@chromium.org303ada72010-10-27 09:33:13 +00001985 MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1986 DeleteMode mode);
1987 MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001989 // Returns true if most of the elements backing storage is used.
1990 bool HasDenseElements();
1991
kmillikin@chromium.org9155e252010-05-26 13:27:57 +00001992 bool CanSetCallback(String* name);
lrn@chromium.org303ada72010-10-27 09:33:13 +00001993 MUST_USE_RESULT MaybeObject* SetElementCallback(
1994 uint32_t index,
1995 Object* structure,
1996 PropertyAttributes attributes);
1997 MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1998 String* name,
1999 Object* structure,
2000 PropertyAttributes attributes);
2001 MUST_USE_RESULT MaybeObject* DefineGetterSetter(
2002 String* name,
2003 PropertyAttributes attributes);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004
2005 void LookupInDescriptor(String* name, LookupResult* result);
2006
2007 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
2008};
2009
2010
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002011// Common superclass for FixedArrays that allow implementations to share
2012// common accessors and some code paths.
2013class FixedArrayBase: public HeapObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 public:
2015 // [length]: length of the array.
2016 inline int length();
2017 inline void set_length(int value);
2018
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002019 inline static FixedArrayBase* cast(Object* object);
2020
2021 // Layout description.
2022 // Length is smi tagged when it is stored.
2023 static const int kLengthOffset = HeapObject::kHeaderSize;
2024 static const int kHeaderSize = kLengthOffset + kPointerSize;
2025};
2026
2027
2028// FixedArray describes fixed-sized arrays with element type Object*.
2029class FixedArray: public FixedArrayBase {
2030 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 // Setter and getter for elements.
2032 inline Object* get(int index);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002033 // Setter that uses write barrier.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 inline void set(int index, Object* value);
2035
ager@chromium.orga74f0da2008-12-03 16:05:52 +00002036 // Setter that doesn't need write barrier).
2037 inline void set(int index, Smi* value);
2038 // Setter with explicit barrier mode.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002039 inline void set(int index, Object* value, WriteBarrierMode mode);
2040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 // Setters for frequently used oddballs located in old space.
2042 inline void set_undefined(int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002043 // TODO(isolates): duplicate.
2044 inline void set_undefined(Heap* heap, int index);
ager@chromium.org236ad962008-09-25 09:45:57 +00002045 inline void set_null(int index);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002046 // TODO(isolates): duplicate.
2047 inline void set_null(Heap* heap, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 inline void set_the_hole(int index);
2049
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002050 // Setters with less debug checks for the GC to use.
2051 inline void set_unchecked(int index, Smi* value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002052 inline void set_null_unchecked(Heap* heap, int index);
2053 inline void set_unchecked(Heap* heap, int index, Object* value,
2054 WriteBarrierMode mode);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00002055
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002056 // Gives access to raw memory which stores the array's data.
2057 inline Object** data_start();
2058
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002059 // Copy operations.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002060 MUST_USE_RESULT inline MaybeObject* Copy();
2061 MUST_USE_RESULT MaybeObject* CopySize(int new_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062
2063 // Add the elements of a JSArray to this FixedArray.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002064 MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065
2066 // Compute the union of this and other.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002067 MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068
2069 // Copy a sub array from the receiver to dest.
2070 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
2071
2072 // Garbage collection support.
2073 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
2074
ager@chromium.org3e875802009-06-29 08:26:34 +00002075 // Code Generation support.
2076 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2077
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 // Casting.
2079 static inline FixedArray* cast(Object* obj);
2080
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002081 // Maximal allowed size, in bytes, of a single FixedArray.
2082 // Prevents overflowing size computations, as well as extreme memory
2083 // consumption.
2084 static const int kMaxSize = 512 * MB;
2085 // Maximally allowed length of a FixedArray.
2086 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002089#ifdef OBJECT_PRINT
2090 inline void FixedArrayPrint() {
2091 FixedArrayPrint(stdout);
2092 }
2093 void FixedArrayPrint(FILE* out);
2094#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 void FixedArrayVerify();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002097 // Checks if two FixedArrays have identical contents.
2098 bool IsEqualTo(FixedArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099#endif
2100
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002101 // Swap two elements in a pair of arrays. If this array and the
2102 // numbers array are the same object, the elements are only swapped
2103 // once.
2104 void SwapPairs(FixedArray* numbers, int i, int j);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002106 // Sort prefix of this array and the numbers array as pairs wrt. the
2107 // numbers. If the numbers array and the this array are the same
2108 // object, the prefix of this array is sorted.
2109 void SortPairs(FixedArray* numbers, uint32_t len);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002110
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002111 class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2112 public:
2113 static inline int SizeOf(Map* map, HeapObject* object) {
2114 return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2115 }
2116 };
2117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118 protected:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002119 // Set operation on FixedArray without using write barriers. Can
2120 // only be used for storing old space objects or smis.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 static inline void fast_set(FixedArray* array, int index, Object* value);
2122
2123 private:
2124 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2125};
2126
2127
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00002128// FixedDoubleArray describes fixed-sized arrays with element type double.
2129class FixedDoubleArray: public FixedArrayBase {
2130 public:
2131 inline void Initialize(FixedArray* from);
2132 inline void Initialize(FixedDoubleArray* from);
2133 inline void Initialize(NumberDictionary* from);
2134
2135 // Setter and getter for elements.
2136 inline double get(int index);
2137 inline void set(int index, double value);
2138 inline void set_the_hole(int index);
2139
2140 // Checking for the hole.
2141 inline bool is_the_hole(int index);
2142
2143 // Garbage collection support.
2144 inline static int SizeFor(int length) {
2145 return kHeaderSize + length * kDoubleSize;
2146 }
2147
2148 // The following can't be declared inline as const static
2149 // because they're 64-bit.
2150 static uint64_t kCanonicalNonHoleNanLower32;
2151 static uint64_t kCanonicalNonHoleNanInt64;
2152 static uint64_t kHoleNanInt64;
2153
2154 inline static bool is_the_hole_nan(double value) {
2155 return BitCast<uint64_t, double>(value) == kHoleNanInt64;
2156 }
2157
2158 inline static double hole_nan_as_double() {
2159 return BitCast<double, uint64_t>(kHoleNanInt64);
2160 }
2161
2162 inline static double canonical_not_the_hole_nan_as_double() {
2163 return BitCast<double, uint64_t>(kCanonicalNonHoleNanInt64);
2164 }
2165
2166 // Casting.
2167 static inline FixedDoubleArray* cast(Object* obj);
2168
2169 // Maximal allowed size, in bytes, of a single FixedDoubleArray.
2170 // Prevents overflowing size computations, as well as extreme memory
2171 // consumption.
2172 static const int kMaxSize = 512 * MB;
2173 // Maximally allowed length of a FixedArray.
2174 static const int kMaxLength = (kMaxSize - kHeaderSize) / kDoubleSize;
2175
2176 // Dispatched behavior.
2177#ifdef OBJECT_PRINT
2178 inline void FixedDoubleArrayPrint() {
2179 FixedDoubleArrayPrint(stdout);
2180 }
2181 void FixedDoubleArrayPrint(FILE* out);
2182#endif
2183
2184#ifdef DEBUG
2185 void FixedDoubleArrayVerify();
2186#endif
2187
2188 private:
2189 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedDoubleArray);
2190};
2191
2192
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193// DescriptorArrays are fixed arrays used to hold instance descriptors.
2194// The format of the these objects is:
danno@chromium.org40cb8782011-05-25 07:58:50 +00002195// TODO(1399): It should be possible to make room for bit_field3 in the map
2196// without overloading the instance descriptors field in the map
2197// (and storing it in the DescriptorArray when the map has one).
2198// [0]: storage for bit_field3 for Map owning this object (Smi)
2199// [1]: point to a fixed array with (value, detail) pairs.
2200// [2]: next enumeration index (Smi), or pointer to small fixed array:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201// [0]: next enumeration index (Smi)
2202// [1]: pointer to fixed array with enum cache
danno@chromium.org40cb8782011-05-25 07:58:50 +00002203// [3]: first key
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204// [length() - 1]: last key
2205//
2206class DescriptorArray: public FixedArray {
2207 public:
danno@chromium.org40cb8782011-05-25 07:58:50 +00002208 // Returns true for both shared empty_descriptor_array and for smis, which the
2209 // map uses to encode additional bit fields when the descriptor array is not
2210 // yet used.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002211 inline bool IsEmpty();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00002212
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213 // Returns the number of descriptors in the array.
2214 int number_of_descriptors() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002215 ASSERT(length() > kFirstIndex || IsEmpty());
2216 int len = length();
2217 return len <= kFirstIndex ? 0 : len - kFirstIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 }
2219
2220 int NextEnumerationIndex() {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002221 if (IsEmpty()) return PropertyDetails::kInitialIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 Object* obj = get(kEnumerationIndexIndex);
2223 if (obj->IsSmi()) {
2224 return Smi::cast(obj)->value();
2225 } else {
2226 Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2227 return Smi::cast(index)->value();
2228 }
2229 }
2230
2231 // Set next enumeration index and flush any enum cache.
2232 void SetNextEnumerationIndex(int value) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002233 if (!IsEmpty()) {
2234 fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2235 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 bool HasEnumCache() {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002238 return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 }
2240
2241 Object* GetEnumCache() {
2242 ASSERT(HasEnumCache());
2243 FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2244 return bridge->get(kEnumCacheBridgeCacheIndex);
2245 }
2246
danno@chromium.org40cb8782011-05-25 07:58:50 +00002247 // TODO(1399): It should be possible to make room for bit_field3 in the map
2248 // without overloading the instance descriptors field in the map
2249 // (and storing it in the DescriptorArray when the map has one).
2250 inline int bit_field3_storage();
2251 inline void set_bit_field3_storage(int value);
2252
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 // Initialize or change the enum cache,
2254 // using the supplied storage for the small "bridge".
2255 void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2256
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002257 // Accessors for fetching instance descriptor at descriptor number.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 inline String* GetKey(int descriptor_number);
2259 inline Object* GetValue(int descriptor_number);
2260 inline Smi* GetDetails(int descriptor_number);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002261 inline PropertyType GetType(int descriptor_number);
2262 inline int GetFieldIndex(int descriptor_number);
2263 inline JSFunction* GetConstantFunction(int descriptor_number);
2264 inline Object* GetCallbacksObject(int descriptor_number);
2265 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2266 inline bool IsProperty(int descriptor_number);
2267 inline bool IsTransition(int descriptor_number);
2268 inline bool IsNullDescriptor(int descriptor_number);
2269 inline bool IsDontEnum(int descriptor_number);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270
2271 // Accessor for complete descriptor.
2272 inline void Get(int descriptor_number, Descriptor* desc);
2273 inline void Set(int descriptor_number, Descriptor* desc);
2274
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002275 // Transfer complete descriptor from another descriptor array to
2276 // this one.
2277 inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 // Copy the descriptor array, insert a new descriptor and optionally
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002280 // remove map transitions. If the descriptor is already present, it is
2281 // replaced. If a replaced descriptor is a real property (not a transition
2282 // or null), its enumeration index is kept as is.
2283 // If adding a real property, map transitions must be removed. If adding
2284 // a transition, they must not be removed. All null descriptors are removed.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002285 MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2286 TransitionFlag transition_flag);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002287
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002288 // Remove all transitions. Return a copy of the array with all transitions
2289 // removed, or a Failure object if the new array could not be allocated.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002290 MUST_USE_RESULT MaybeObject* RemoveTransitions();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002291
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292 // Sort the instance descriptors by the hash codes of their keys.
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00002293 // Does not check for duplicates.
2294 void SortUnchecked();
2295
2296 // Sort the instance descriptors by the hash codes of their keys.
2297 // Checks the result for duplicates.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 void Sort();
2299
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 // Search the instance descriptors for given name.
2301 inline int Search(String* name);
2302
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002303 // As the above, but uses DescriptorLookupCache and updates it when
2304 // necessary.
2305 inline int SearchWithCache(String* name);
2306
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 // Tells whether the name is present int the array.
2308 bool Contains(String* name) { return kNotFound != Search(name); }
2309
2310 // Perform a binary search in the instance descriptors represented
2311 // by this fixed array. low and high are descriptor indices. If there
2312 // are three instance descriptors in this array it should be called
2313 // with low=0 and high=2.
2314 int BinarySearch(String* name, int low, int high);
2315
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002316 // Perform a linear search in the instance descriptors represented
ager@chromium.org32912102009-01-16 10:38:43 +00002317 // by this fixed array. len is the number of descriptor indices that are
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002318 // valid. Does not require the descriptors to be sorted.
2319 int LinearSearch(String* name, int len);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002320
2321 // Allocates a DescriptorArray, but returns the singleton
2322 // empty descriptor array object if number_of_descriptors is 0.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002323 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324
2325 // Casting.
2326 static inline DescriptorArray* cast(Object* obj);
2327
2328 // Constant for denoting key was not found.
2329 static const int kNotFound = -1;
2330
danno@chromium.org40cb8782011-05-25 07:58:50 +00002331 static const int kBitField3StorageIndex = 0;
2332 static const int kContentArrayIndex = 1;
2333 static const int kEnumerationIndexIndex = 2;
2334 static const int kFirstIndex = 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335
2336 // The length of the "bridge" to the enum cache.
2337 static const int kEnumCacheBridgeLength = 2;
2338 static const int kEnumCacheBridgeEnumIndex = 0;
2339 static const int kEnumCacheBridgeCacheIndex = 1;
2340
2341 // Layout description.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002342 static const int kBitField3StorageOffset = FixedArray::kHeaderSize;
2343 static const int kContentArrayOffset = kBitField3StorageOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2345 static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2346
2347 // Layout description for the bridge array.
2348 static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2349 static const int kEnumCacheBridgeCacheOffset =
2350 kEnumCacheBridgeEnumOffset + kPointerSize;
2351
whesse@chromium.org023421e2010-12-21 12:19:12 +00002352#ifdef OBJECT_PRINT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 // Print all the descriptors.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002354 inline void PrintDescriptors() {
2355 PrintDescriptors(stdout);
2356 }
2357 void PrintDescriptors(FILE* out);
2358#endif
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002359
whesse@chromium.org023421e2010-12-21 12:19:12 +00002360#ifdef DEBUG
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00002361 // Is the descriptor array sorted and without duplicates?
2362 bool IsSortedNoDuplicates();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002363
2364 // Are two DescriptorArrays equal?
2365 bool IsEqualTo(DescriptorArray* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366#endif
2367
2368 // The maximum number of descriptors we want in a descriptor array (should
2369 // fit in a page).
2370 static const int kMaxNumberOfDescriptors = 1024 + 512;
2371
2372 private:
2373 // Conversion from descriptor number to array indices.
2374 static int ToKeyIndex(int descriptor_number) {
2375 return descriptor_number+kFirstIndex;
2376 }
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00002377
2378 static int ToDetailsIndex(int descriptor_number) {
2379 return (descriptor_number << 1) + 1;
2380 }
2381
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 static int ToValueIndex(int descriptor_number) {
2383 return descriptor_number << 1;
2384 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002385
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 bool is_null_descriptor(int descriptor_number) {
2387 return PropertyDetails(GetDetails(descriptor_number)).type() ==
2388 NULL_DESCRIPTOR;
2389 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390 // Swap operation on FixedArray without using write barriers.
2391 static inline void fast_swap(FixedArray* array, int first, int second);
2392
2393 // Swap descriptor first and second.
2394 inline void Swap(int first, int second);
2395
2396 FixedArray* GetContentArray() {
2397 return FixedArray::cast(get(kContentArrayIndex));
2398 }
2399 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2400};
2401
2402
2403// HashTable is a subclass of FixedArray that implements a hash table
2404// that uses open addressing and quadratic probing.
2405//
2406// In order for the quadratic probing to work, elements that have not
2407// yet been used and elements that have been deleted are
2408// distinguished. Probing continues when deleted elements are
2409// encountered and stops when unused elements are encountered.
2410//
2411// - Elements with key == undefined have not been used yet.
2412// - Elements with key == null have been deleted.
2413//
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002414// The hash table class is parameterized with a Shape and a Key.
2415// Shape must be a class with the following interface:
2416// class ExampleShape {
2417// public:
2418// // Tells whether key matches other.
2419// static bool IsMatch(Key key, Object* other);
2420// // Returns the hash value for key.
2421// static uint32_t Hash(Key key);
2422// // Returns the hash value for object.
2423// static uint32_t HashForObject(Key key, Object* object);
2424// // Convert key to an object.
2425// static inline Object* AsObject(Key key);
2426// // The prefix size indicates number of elements in the beginning
2427// // of the backing storage.
2428// static const int kPrefixSize = ..;
2429// // The Element size indicates number of elements per entry.
2430// static const int kEntrySize = ..;
2431// };
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002432// The prefix size indicates an amount of memory in the
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433// beginning of the backing storage that can be used for non-element
2434// information by subclasses.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00002435
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002436template<typename Shape, typename Key>
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437class HashTable: public FixedArray {
2438 public:
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002439 // Returns the number of elements in the hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002440 int NumberOfElements() {
2441 return Smi::cast(get(kNumberOfElementsIndex))->value();
2442 }
2443
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002444 // Returns the number of deleted elements in the hash table.
2445 int NumberOfDeletedElements() {
2446 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2447 }
2448
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002449 // Returns the capacity of the hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450 int Capacity() {
2451 return Smi::cast(get(kCapacityIndex))->value();
2452 }
2453
2454 // ElementAdded should be called whenever an element is added to a
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002455 // hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002456 void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2457
2458 // ElementRemoved should be called whenever an element is removed from
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002459 // a hash table.
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002460 void ElementRemoved() {
2461 SetNumberOfElements(NumberOfElements() - 1);
2462 SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2463 }
2464 void ElementsRemoved(int n) {
2465 SetNumberOfElements(NumberOfElements() - n);
2466 SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2467 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002469 // Returns a new HashTable object. Might return Failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002470 MUST_USE_RESULT static MaybeObject* Allocate(
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00002471 int at_least_space_for,
2472 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473
2474 // Returns the key at entry.
2475 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2476
ager@chromium.org32912102009-01-16 10:38:43 +00002477 // Tells whether k is a real key. Null and undefined are not allowed
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478 // as keys and can be used to indicate missing or deleted elements.
2479 bool IsKey(Object* k) {
2480 return !k->IsNull() && !k->IsUndefined();
2481 }
2482
2483 // Garbage collection support.
2484 void IteratePrefix(ObjectVisitor* visitor);
2485 void IterateElements(ObjectVisitor* visitor);
2486
2487 // Casting.
2488 static inline HashTable* cast(Object* obj);
2489
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002490 // Compute the probe offset (quadratic probing).
2491 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2492 return (n + n * n) >> 1;
2493 }
2494
2495 static const int kNumberOfElementsIndex = 0;
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002496 static const int kNumberOfDeletedElementsIndex = 1;
2497 static const int kCapacityIndex = 2;
2498 static const int kPrefixStartIndex = 3;
2499 static const int kElementsStartIndex =
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002500 kPrefixStartIndex + Shape::kPrefixSize;
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002501 static const int kEntrySize = Shape::kEntrySize;
2502 static const int kElementsStartOffset =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002503 kHeaderSize + kElementsStartIndex * kPointerSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002504 static const int kCapacityOffset =
2505 kHeaderSize + kCapacityIndex * kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002507 // Constant used for denoting a absent entry.
2508 static const int kNotFound = -1;
2509
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002510 // Maximal capacity of HashTable. Based on maximal length of underlying
2511 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2512 // cannot overflow.
2513 static const int kMaxCapacity =
2514 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2515
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002516 // Find entry for key otherwise return kNotFound.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002517 inline int FindEntry(Key key);
2518 int FindEntry(Isolate* isolate, Key key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002519
2520 protected:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002521 // Find the entry at which to insert element with the given key that
2522 // has the given hash value.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002523 uint32_t FindInsertionEntry(uint32_t hash);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524
2525 // Returns the index for an entry (of the key)
2526 static inline int EntryToIndex(int entry) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002527 return (entry * kEntrySize) + kElementsStartIndex;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528 }
2529
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002530 // Update the number of elements in the hash table.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002531 void SetNumberOfElements(int nof) {
2532 fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2533 }
2534
kasperl@chromium.orgedf0cd12010-01-05 13:29:12 +00002535 // Update the number of deleted elements in the hash table.
2536 void SetNumberOfDeletedElements(int nod) {
2537 fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2538 }
2539
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540 // Sets the capacity of the hash table.
2541 void SetCapacity(int capacity) {
2542 // To scale a computed hash code to fit within the hash table, we
2543 // use bit-wise AND with a mask, so the capacity must be positive
2544 // and non-zero.
2545 ASSERT(capacity > 0);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002546 ASSERT(capacity <= kMaxCapacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002547 fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2548 }
2549
2550
2551 // Returns probe entry.
2552 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2553 ASSERT(IsPowerOf2(size));
2554 return (hash + GetProbeOffset(number)) & (size - 1);
2555 }
2556
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002557 static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2558 return hash & (size - 1);
2559 }
2560
2561 static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2562 return (last + number) & (size - 1);
2563 }
2564
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 // Ensure enough space for n additional elements.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002566 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567};
2568
2569
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002570
2571// HashTableKey is an abstract superclass for virtual key behavior.
2572class HashTableKey {
2573 public:
2574 // Returns whether the other object matches this key.
2575 virtual bool IsMatch(Object* other) = 0;
2576 // Returns the hash value for this key.
2577 virtual uint32_t Hash() = 0;
2578 // Returns the hash value for object.
2579 virtual uint32_t HashForObject(Object* key) = 0;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002580 // Returns the key object for storing into the hash table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002581 // If allocations fails a failure object is returned.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002582 MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002583 // Required.
2584 virtual ~HashTableKey() {}
2585};
2586
2587class SymbolTableShape {
2588 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002589 static inline bool IsMatch(HashTableKey* key, Object* value) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002590 return key->IsMatch(value);
2591 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002592 static inline uint32_t Hash(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002593 return key->Hash();
2594 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002595 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002596 return key->HashForObject(object);
2597 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002598 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002599 return key->AsObject();
2600 }
2601
2602 static const int kPrefixSize = 0;
2603 static const int kEntrySize = 1;
2604};
2605
danno@chromium.org40cb8782011-05-25 07:58:50 +00002606class SeqAsciiString;
2607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608// SymbolTable.
2609//
2610// No special elements in the prefix and the element size is 1
2611// because only the symbol itself (the key) needs to be stored.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002612class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613 public:
2614 // Find symbol in the symbol table. If it is not there yet, it is
2615 // added. The return value is the symbol table which might have
2616 // been enlarged. If the return value is not a failure, the symbol
2617 // pointer *s is set to the symbol found.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002618 MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00002619 MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2620 Object** s);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002621 MUST_USE_RESULT MaybeObject* LookupSubStringAsciiSymbol(
2622 Handle<SeqAsciiString> str,
2623 int from,
2624 int length,
2625 Object** s);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00002626 MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2627 Object** s);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002628 MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629
ager@chromium.org7c537e22008-10-16 08:43:32 +00002630 // Looks up a symbol that is equal to the given string and returns
2631 // true if it is found, assigning the symbol to the given output
2632 // parameter.
2633 bool LookupSymbolIfExists(String* str, String** symbol);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00002634 bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 // Casting.
2637 static inline SymbolTable* cast(Object* obj);
2638
2639 private:
lrn@chromium.org303ada72010-10-27 09:33:13 +00002640 MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641
2642 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2643};
2644
2645
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002646class MapCacheShape {
2647 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002648 static inline bool IsMatch(HashTableKey* key, Object* value) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002649 return key->IsMatch(value);
2650 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002651 static inline uint32_t Hash(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002652 return key->Hash();
2653 }
2654
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002655 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002656 return key->HashForObject(object);
2657 }
2658
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002659 MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002660 return key->AsObject();
2661 }
2662
2663 static const int kPrefixSize = 0;
2664 static const int kEntrySize = 2;
2665};
2666
2667
ager@chromium.org236ad962008-09-25 09:45:57 +00002668// MapCache.
2669//
2670// Maps keys that are a fixed array of symbols to a map.
2671// Used for canonicalize maps for object literals.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002672class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
ager@chromium.org236ad962008-09-25 09:45:57 +00002673 public:
2674 // Find cached value for a string key, otherwise return null.
2675 Object* Lookup(FixedArray* key);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002676 MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
ager@chromium.org236ad962008-09-25 09:45:57 +00002677 static inline MapCache* cast(Object* obj);
2678
2679 private:
2680 DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2681};
2682
2683
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002684template <typename Shape, typename Key>
2685class Dictionary: public HashTable<Shape, Key> {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686 public:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002687 static inline Dictionary<Shape, Key>* cast(Object* obj) {
2688 return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2689 }
2690
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691 // Returns the value at entry.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002692 Object* ValueAt(int entry) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002693 return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002694 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695
2696 // Set the value for entry.
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00002697 // Returns false if the put wasn't performed due to property being read only.
2698 // Returns true on successful put.
2699 bool ValueAtPut(int entry, Object* value) {
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00002700 // Check that this value can actually be written.
2701 PropertyDetails details = DetailsAt(entry);
2702 // If a value has not been initilized we allow writing to it even if
2703 // it is read only (a declared const that has not been initialized).
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00002704 if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2705 return false;
2706 }
2707 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2708 return true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709 }
2710
2711 // Returns the property details for the property at entry.
2712 PropertyDetails DetailsAt(int entry) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00002713 ASSERT(entry >= 0); // Not found is -1, which is not caught by get().
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002714 return PropertyDetails(
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002715 Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002716 }
2717
2718 // Set the details for entry.
2719 void DetailsAtPut(int entry, PropertyDetails value) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002720 this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 }
2722
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723 // Sorting support
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 void CopyValuesTo(FixedArray* elements);
2725
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002726 // Delete a property from the dictionary.
ager@chromium.orge2902be2009-06-08 12:21:35 +00002727 Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002728
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729 // Returns the number of elements in the dictionary filtering out properties
2730 // with the specified attributes.
2731 int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2732
2733 // Returns the number of enumerable elements in the dictionary.
2734 int NumberOfEnumElements();
2735
2736 // Copies keys to preallocated fixed array.
2737 void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 // Fill in details for properties into storage.
2739 void CopyKeysTo(FixedArray* storage);
2740
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002741 // Accessors for next enumeration index.
2742 void SetNextEnumerationIndex(int index) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00002743 this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002744 }
2745
2746 int NextEnumerationIndex() {
2747 return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2748 }
2749
2750 // Returns a new array for dictionary usage. Might return Failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002751 MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002752
2753 // Ensure enough space for n additional elements.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002754 MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002755
whesse@chromium.org023421e2010-12-21 12:19:12 +00002756#ifdef OBJECT_PRINT
2757 inline void Print() {
2758 Print(stdout);
2759 }
2760 void Print(FILE* out);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002761#endif
2762 // Returns the key (slow).
2763 Object* SlowReverseLookup(Object* value);
2764
2765 // Sets the entry to (key, value) pair.
2766 inline void SetEntry(int entry,
2767 Object* key,
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002768 Object* value);
2769 inline void SetEntry(int entry,
2770 Object* key,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002771 Object* value,
2772 PropertyDetails details);
2773
lrn@chromium.org303ada72010-10-27 09:33:13 +00002774 MUST_USE_RESULT MaybeObject* Add(Key key,
2775 Object* value,
2776 PropertyDetails details);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002777
2778 protected:
2779 // Generic at put operation.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002780 MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002781
2782 // Add entry to dictionary.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002783 MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2784 Object* value,
2785 PropertyDetails details,
2786 uint32_t hash);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002787
2788 // Generate new enumeration indices to avoid enumeration index overflow.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002789 MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002790 static const int kMaxNumberKeyIndex =
2791 HashTable<Shape, Key>::kPrefixStartIndex;
2792 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2793};
2794
2795
2796class StringDictionaryShape {
2797 public:
2798 static inline bool IsMatch(String* key, Object* other);
2799 static inline uint32_t Hash(String* key);
2800 static inline uint32_t HashForObject(String* key, Object* object);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002801 MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002802 static const int kPrefixSize = 2;
2803 static const int kEntrySize = 3;
2804 static const bool kIsEnumerable = true;
2805};
2806
2807
2808class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2809 public:
2810 static inline StringDictionary* cast(Object* obj) {
2811 ASSERT(obj->IsDictionary());
2812 return reinterpret_cast<StringDictionary*>(obj);
2813 }
2814
2815 // Copies enumerable keys to preallocated fixed array.
2816 void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2817
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818 // For transforming properties of a JSObject.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002819 MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2820 JSObject* obj,
2821 int unused_property_fields);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002822
2823 // Find entry for key otherwise return kNotFound. Optimzed version of
2824 // HashTable::FindEntry.
2825 int FindEntry(String* key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002826};
2827
2828
2829class NumberDictionaryShape {
2830 public:
2831 static inline bool IsMatch(uint32_t key, Object* other);
2832 static inline uint32_t Hash(uint32_t key);
2833 static inline uint32_t HashForObject(uint32_t key, Object* object);
lrn@chromium.org303ada72010-10-27 09:33:13 +00002834 MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002835 static const int kPrefixSize = 2;
2836 static const int kEntrySize = 3;
2837 static const bool kIsEnumerable = false;
2838};
2839
2840
2841class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2842 public:
2843 static NumberDictionary* cast(Object* obj) {
2844 ASSERT(obj->IsDictionary());
2845 return reinterpret_cast<NumberDictionary*>(obj);
2846 }
2847
2848 // Type specific at put (default NONE attributes is used when adding).
lrn@chromium.org303ada72010-10-27 09:33:13 +00002849 MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2850 MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2851 Object* value,
2852 PropertyDetails details);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002853
2854 // Set an existing entry or add a new one if needed.
lrn@chromium.org303ada72010-10-27 09:33:13 +00002855 MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2856 Object* value,
2857 PropertyDetails details);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002858
2859 void UpdateMaxNumberKey(uint32_t key);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860
2861 // If slow elements are required we will never go back to fast-case
2862 // for the elements kept in this dictionary. We require slow
2863 // elements if an element has been added at an index larger than
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002864 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2865 // when defining a getter or setter with a number key.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002866 inline bool requires_slow_elements();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002867 inline void set_requires_slow_elements();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002868
2869 // Get the value of the max number key that has been added to this
2870 // dictionary. max_number_key can only be called if
2871 // requires_slow_elements returns false.
2872 inline uint32_t max_number_key();
2873
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00002874 // Remove all entries were key is a number and (from <= key && key < to).
2875 void RemoveNumberEntries(uint32_t from, uint32_t to);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002876
2877 // Bit masks.
2878 static const int kRequiresSlowElementsMask = 1;
2879 static const int kRequiresSlowElementsTagSize = 1;
2880 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881};
2882
2883
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002884// JSFunctionResultCache caches results of some JSFunction invocation.
2885// It is a fixed array with fixed structure:
2886// [0]: factory function
2887// [1]: finger index
2888// [2]: current cache size
2889// [3]: dummy field.
2890// The rest of array are key/value pairs.
2891class JSFunctionResultCache: public FixedArray {
2892 public:
2893 static const int kFactoryIndex = 0;
2894 static const int kFingerIndex = kFactoryIndex + 1;
2895 static const int kCacheSizeIndex = kFingerIndex + 1;
2896 static const int kDummyIndex = kCacheSizeIndex + 1;
2897 static const int kEntriesIndex = kDummyIndex + 1;
antonm@chromium.org397e23c2010-04-21 12:00:05 +00002898
2899 static const int kEntrySize = 2; // key + value
ager@chromium.orgac091b72010-05-05 07:34:42 +00002900
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00002901 static const int kFactoryOffset = kHeaderSize;
2902 static const int kFingerOffset = kFactoryOffset + kPointerSize;
2903 static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2904
ager@chromium.orgac091b72010-05-05 07:34:42 +00002905 inline void MakeZeroSize();
2906 inline void Clear();
2907
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00002908 inline int size();
2909 inline void set_size(int size);
2910 inline int finger_index();
2911 inline void set_finger_index(int finger_index);
2912
ager@chromium.orgac091b72010-05-05 07:34:42 +00002913 // Casting
2914 static inline JSFunctionResultCache* cast(Object* obj);
2915
2916#ifdef DEBUG
2917 void JSFunctionResultCacheVerify();
2918#endif
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002919};
2920
2921
ricow@chromium.org65fae842010-08-25 15:26:24 +00002922// The cache for maps used by normalized (dictionary mode) objects.
2923// Such maps do not have property descriptors, so a typical program
2924// needs very limited number of distinct normalized maps.
2925class NormalizedMapCache: public FixedArray {
2926 public:
2927 static const int kEntries = 64;
2928
lrn@chromium.org303ada72010-10-27 09:33:13 +00002929 MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2930 PropertyNormalizationMode mode);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002931
ricow@chromium.org65fae842010-08-25 15:26:24 +00002932 void Clear();
2933
2934 // Casting
2935 static inline NormalizedMapCache* cast(Object* obj);
2936
2937#ifdef DEBUG
2938 void NormalizedMapCacheVerify();
2939#endif
ricow@chromium.org65fae842010-08-25 15:26:24 +00002940};
2941
2942
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002943// ByteArray represents fixed sized byte arrays. Used by the outside world,
2944// such as PCRE, and also by the memory allocator and garbage collector to
2945// fill in free blocks in the heap.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002946class ByteArray: public HeapObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947 public:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002948 // [length]: length of the array.
2949 inline int length();
2950 inline void set_length(int value);
2951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 // Setter and getter.
2953 inline byte get(int index);
2954 inline void set(int index, byte value);
2955
2956 // Treat contents as an int array.
2957 inline int get_int(int index);
2958
2959 static int SizeFor(int length) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002960 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 }
2962 // We use byte arrays for free blocks in the heap. Given a desired size in
2963 // bytes that is a multiple of the word size and big enough to hold a byte
2964 // array, this function returns the number of elements a byte array should
2965 // have.
2966 static int LengthFor(int size_in_bytes) {
2967 ASSERT(IsAligned(size_in_bytes, kPointerSize));
2968 ASSERT(size_in_bytes >= kHeaderSize);
2969 return size_in_bytes - kHeaderSize;
2970 }
2971
2972 // Returns data start address.
2973 inline Address GetDataStartAddress();
2974
2975 // Returns a pointer to the ByteArray object for a given data start address.
2976 static inline ByteArray* FromDataStartAddress(Address address);
2977
2978 // Casting.
2979 static inline ByteArray* cast(Object* obj);
2980
2981 // Dispatched behavior.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00002982 inline int ByteArraySize() {
2983 return SizeFor(this->length());
2984 }
whesse@chromium.org023421e2010-12-21 12:19:12 +00002985#ifdef OBJECT_PRINT
2986 inline void ByteArrayPrint() {
2987 ByteArrayPrint(stdout);
2988 }
2989 void ByteArrayPrint(FILE* out);
2990#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002991#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002992 void ByteArrayVerify();
2993#endif
2994
ricow@chromium.org30ce4112010-05-31 10:38:25 +00002995 // Layout description.
2996 // Length is smi tagged when it is stored.
2997 static const int kLengthOffset = HeapObject::kHeaderSize;
2998 static const int kHeaderSize = kLengthOffset + kPointerSize;
2999
3000 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003001
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003002 // Maximal memory consumption for a single ByteArray.
3003 static const int kMaxSize = 512 * MB;
3004 // Maximal length of a single ByteArray.
3005 static const int kMaxLength = kMaxSize - kHeaderSize;
3006
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003007 private:
3008 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
3009};
3010
3011
ager@chromium.org3811b432009-10-28 14:53:37 +00003012// An ExternalArray represents a fixed-size array of primitive values
3013// which live outside the JavaScript heap. Its subclasses are used to
3014// implement the CanvasArray types being defined in the WebGL
3015// specification. As of this writing the first public draft is not yet
3016// available, but Khronos members can access the draft at:
3017// https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
3018//
3019// The semantics of these arrays differ from CanvasPixelArray.
3020// Out-of-range values passed to the setter are converted via a C
3021// cast, not clamping. Out-of-range indices cause exceptions to be
3022// raised rather than being silently ignored.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003023class ExternalArray: public HeapObject {
ager@chromium.org3811b432009-10-28 14:53:37 +00003024 public:
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003025 // [length]: length of the array.
3026 inline int length();
3027 inline void set_length(int value);
3028
ager@chromium.org3811b432009-10-28 14:53:37 +00003029 // [external_pointer]: The pointer to the external memory area backing this
3030 // external array.
3031 DECL_ACCESSORS(external_pointer, void) // Pointer to the data store.
3032
3033 // Casting.
3034 static inline ExternalArray* cast(Object* obj);
3035
3036 // Maximal acceptable length for an external array.
3037 static const int kMaxLength = 0x3fffffff;
3038
3039 // ExternalArray headers are not quadword aligned.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003040 static const int kLengthOffset = HeapObject::kHeaderSize;
3041 static const int kExternalPointerOffset =
3042 POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
ager@chromium.org3811b432009-10-28 14:53:37 +00003043 static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00003044 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
ager@chromium.org3811b432009-10-28 14:53:37 +00003045
3046 private:
3047 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
3048};
3049
3050
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00003051// A ExternalPixelArray represents a fixed-size byte array with special
3052// semantics used for implementing the CanvasPixelArray object. Please see the
3053// specification at:
3054
3055// http://www.whatwg.org/specs/web-apps/current-work/
3056// multipage/the-canvas-element.html#canvaspixelarray
3057// In particular, write access clamps the value written to 0 or 255 if the
3058// value written is outside this range.
3059class ExternalPixelArray: public ExternalArray {
3060 public:
3061 inline uint8_t* external_pixel_pointer();
3062
3063 // Setter and getter.
3064 inline uint8_t get(int index);
3065 inline void set(int index, uint8_t value);
3066
3067 // This accessor applies the correct conversion from Smi, HeapNumber and
3068 // undefined and clamps the converted value between 0 and 255.
3069 Object* SetValue(uint32_t index, Object* value);
3070
3071 // Casting.
3072 static inline ExternalPixelArray* cast(Object* obj);
3073
3074#ifdef OBJECT_PRINT
3075 inline void ExternalPixelArrayPrint() {
3076 ExternalPixelArrayPrint(stdout);
3077 }
3078 void ExternalPixelArrayPrint(FILE* out);
3079#endif
3080#ifdef DEBUG
3081 void ExternalPixelArrayVerify();
3082#endif // DEBUG
3083
3084 private:
3085 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
3086};
3087
3088
ager@chromium.org3811b432009-10-28 14:53:37 +00003089class ExternalByteArray: public ExternalArray {
3090 public:
3091 // Setter and getter.
3092 inline int8_t get(int index);
3093 inline void set(int index, int8_t value);
3094
3095 // This accessor applies the correct conversion from Smi, HeapNumber
3096 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003097 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003098
3099 // Casting.
3100 static inline ExternalByteArray* cast(Object* obj);
3101
whesse@chromium.org023421e2010-12-21 12:19:12 +00003102#ifdef OBJECT_PRINT
3103 inline void ExternalByteArrayPrint() {
3104 ExternalByteArrayPrint(stdout);
3105 }
3106 void ExternalByteArrayPrint(FILE* out);
3107#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003108#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003109 void ExternalByteArrayVerify();
3110#endif // DEBUG
3111
3112 private:
3113 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
3114};
3115
3116
3117class ExternalUnsignedByteArray: public ExternalArray {
3118 public:
3119 // Setter and getter.
3120 inline uint8_t get(int index);
3121 inline void set(int index, uint8_t value);
3122
3123 // This accessor applies the correct conversion from Smi, HeapNumber
3124 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003125 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003126
3127 // Casting.
3128 static inline ExternalUnsignedByteArray* cast(Object* obj);
3129
whesse@chromium.org023421e2010-12-21 12:19:12 +00003130#ifdef OBJECT_PRINT
3131 inline void ExternalUnsignedByteArrayPrint() {
3132 ExternalUnsignedByteArrayPrint(stdout);
3133 }
3134 void ExternalUnsignedByteArrayPrint(FILE* out);
3135#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003136#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003137 void ExternalUnsignedByteArrayVerify();
3138#endif // DEBUG
3139
3140 private:
3141 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
3142};
3143
3144
3145class ExternalShortArray: public ExternalArray {
3146 public:
3147 // Setter and getter.
3148 inline int16_t get(int index);
3149 inline void set(int index, int16_t value);
3150
3151 // This accessor applies the correct conversion from Smi, HeapNumber
3152 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003153 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003154
3155 // Casting.
3156 static inline ExternalShortArray* cast(Object* obj);
3157
whesse@chromium.org023421e2010-12-21 12:19:12 +00003158#ifdef OBJECT_PRINT
3159 inline void ExternalShortArrayPrint() {
3160 ExternalShortArrayPrint(stdout);
3161 }
3162 void ExternalShortArrayPrint(FILE* out);
3163#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003164#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003165 void ExternalShortArrayVerify();
3166#endif // DEBUG
3167
3168 private:
3169 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
3170};
3171
3172
3173class ExternalUnsignedShortArray: public ExternalArray {
3174 public:
3175 // Setter and getter.
3176 inline uint16_t get(int index);
3177 inline void set(int index, uint16_t value);
3178
3179 // This accessor applies the correct conversion from Smi, HeapNumber
3180 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003181 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003182
3183 // Casting.
3184 static inline ExternalUnsignedShortArray* cast(Object* obj);
3185
whesse@chromium.org023421e2010-12-21 12:19:12 +00003186#ifdef OBJECT_PRINT
3187 inline void ExternalUnsignedShortArrayPrint() {
3188 ExternalUnsignedShortArrayPrint(stdout);
3189 }
3190 void ExternalUnsignedShortArrayPrint(FILE* out);
3191#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003192#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003193 void ExternalUnsignedShortArrayVerify();
3194#endif // DEBUG
3195
3196 private:
3197 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3198};
3199
3200
3201class ExternalIntArray: public ExternalArray {
3202 public:
3203 // Setter and getter.
3204 inline int32_t get(int index);
3205 inline void set(int index, int32_t value);
3206
3207 // This accessor applies the correct conversion from Smi, HeapNumber
3208 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003209 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003210
3211 // Casting.
3212 static inline ExternalIntArray* cast(Object* obj);
3213
whesse@chromium.org023421e2010-12-21 12:19:12 +00003214#ifdef OBJECT_PRINT
3215 inline void ExternalIntArrayPrint() {
3216 ExternalIntArrayPrint(stdout);
3217 }
3218 void ExternalIntArrayPrint(FILE* out);
3219#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003220#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003221 void ExternalIntArrayVerify();
3222#endif // DEBUG
3223
3224 private:
3225 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3226};
3227
3228
3229class ExternalUnsignedIntArray: public ExternalArray {
3230 public:
3231 // Setter and getter.
3232 inline uint32_t get(int index);
3233 inline void set(int index, uint32_t value);
3234
3235 // This accessor applies the correct conversion from Smi, HeapNumber
3236 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003237 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003238
3239 // Casting.
3240 static inline ExternalUnsignedIntArray* cast(Object* obj);
3241
whesse@chromium.org023421e2010-12-21 12:19:12 +00003242#ifdef OBJECT_PRINT
3243 inline void ExternalUnsignedIntArrayPrint() {
3244 ExternalUnsignedIntArrayPrint(stdout);
3245 }
3246 void ExternalUnsignedIntArrayPrint(FILE* out);
3247#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003248#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003249 void ExternalUnsignedIntArrayVerify();
3250#endif // DEBUG
3251
3252 private:
3253 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3254};
3255
3256
3257class ExternalFloatArray: public ExternalArray {
3258 public:
3259 // Setter and getter.
3260 inline float get(int index);
3261 inline void set(int index, float value);
3262
3263 // This accessor applies the correct conversion from Smi, HeapNumber
3264 // and undefined.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003265 MaybeObject* SetValue(uint32_t index, Object* value);
ager@chromium.org3811b432009-10-28 14:53:37 +00003266
3267 // Casting.
3268 static inline ExternalFloatArray* cast(Object* obj);
3269
whesse@chromium.org023421e2010-12-21 12:19:12 +00003270#ifdef OBJECT_PRINT
3271 inline void ExternalFloatArrayPrint() {
3272 ExternalFloatArrayPrint(stdout);
3273 }
3274 void ExternalFloatArrayPrint(FILE* out);
3275#endif
ager@chromium.org3811b432009-10-28 14:53:37 +00003276#ifdef DEBUG
ager@chromium.org3811b432009-10-28 14:53:37 +00003277 void ExternalFloatArrayVerify();
3278#endif // DEBUG
3279
3280 private:
3281 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3282};
3283
3284
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00003285class ExternalDoubleArray: public ExternalArray {
3286 public:
3287 // Setter and getter.
3288 inline double get(int index);
3289 inline void set(int index, double value);
3290
3291 // This accessor applies the correct conversion from Smi, HeapNumber
3292 // and undefined.
3293 MaybeObject* SetValue(uint32_t index, Object* value);
3294
3295 // Casting.
3296 static inline ExternalDoubleArray* cast(Object* obj);
3297
3298#ifdef OBJECT_PRINT
3299 inline void ExternalDoubleArrayPrint() {
3300 ExternalDoubleArrayPrint(stdout);
3301 }
3302 void ExternalDoubleArrayPrint(FILE* out);
3303#endif // OBJECT_PRINT
3304#ifdef DEBUG
3305 void ExternalDoubleArrayVerify();
3306#endif // DEBUG
3307
3308 private:
3309 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalDoubleArray);
3310};
3311
3312
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003313// DeoptimizationInputData is a fixed array used to hold the deoptimization
3314// data for code generated by the Hydrogen/Lithium compiler. It also
3315// contains information about functions that were inlined. If N different
3316// functions were inlined then first N elements of the literal array will
3317// contain these functions.
3318//
3319// It can be empty.
3320class DeoptimizationInputData: public FixedArray {
3321 public:
3322 // Layout description. Indices in the array.
3323 static const int kTranslationByteArrayIndex = 0;
3324 static const int kInlinedFunctionCountIndex = 1;
3325 static const int kLiteralArrayIndex = 2;
3326 static const int kOsrAstIdIndex = 3;
3327 static const int kOsrPcOffsetIndex = 4;
3328 static const int kFirstDeoptEntryIndex = 5;
3329
3330 // Offsets of deopt entry elements relative to the start of the entry.
3331 static const int kAstIdOffset = 0;
3332 static const int kTranslationIndexOffset = 1;
3333 static const int kArgumentsStackHeightOffset = 2;
3334 static const int kDeoptEntrySize = 3;
3335
3336 // Simple element accessors.
3337#define DEFINE_ELEMENT_ACCESSORS(name, type) \
3338 type* name() { \
3339 return type::cast(get(k##name##Index)); \
3340 } \
3341 void Set##name(type* value) { \
3342 set(k##name##Index, value); \
3343 }
3344
3345 DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3346 DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3347 DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3348 DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3349 DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3350
3351 // Unchecked accessor to be used during GC.
3352 FixedArray* UncheckedLiteralArray() {
3353 return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3354 }
3355
3356#undef DEFINE_ELEMENT_ACCESSORS
3357
3358 // Accessors for elements of the ith deoptimization entry.
3359#define DEFINE_ENTRY_ACCESSORS(name, type) \
3360 type* name(int i) { \
3361 return type::cast(get(IndexForEntry(i) + k##name##Offset)); \
3362 } \
3363 void Set##name(int i, type* value) { \
3364 set(IndexForEntry(i) + k##name##Offset, value); \
3365 }
3366
3367 DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3368 DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3369 DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3370
3371#undef DEFINE_ENTRY_ACCESSORS
3372
3373 int DeoptCount() {
3374 return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3375 }
3376
3377 // Allocates a DeoptimizationInputData.
3378 MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3379 PretenureFlag pretenure);
3380
3381 // Casting.
3382 static inline DeoptimizationInputData* cast(Object* obj);
3383
whesse@chromium.org023421e2010-12-21 12:19:12 +00003384#ifdef OBJECT_PRINT
3385 void DeoptimizationInputDataPrint(FILE* out);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003386#endif
3387
3388 private:
3389 static int IndexForEntry(int i) {
3390 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3391 }
3392
3393 static int LengthFor(int entry_count) {
3394 return IndexForEntry(entry_count);
3395 }
3396};
3397
3398
3399// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3400// data for code generated by the full compiler.
3401// The format of the these objects is
3402// [i * 2]: Ast ID for ith deoptimization.
3403// [i * 2 + 1]: PC and state of ith deoptimization
3404class DeoptimizationOutputData: public FixedArray {
3405 public:
3406 int DeoptPoints() { return length() / 2; }
3407 Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3408 void SetAstId(int index, Smi* id) { set(index * 2, id); }
3409 Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3410 void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3411
3412 static int LengthOfFixedArray(int deopt_points) {
3413 return deopt_points * 2;
3414 }
3415
3416 // Allocates a DeoptimizationOutputData.
3417 MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3418 PretenureFlag pretenure);
3419
3420 // Casting.
3421 static inline DeoptimizationOutputData* cast(Object* obj);
3422
whesse@chromium.org023421e2010-12-21 12:19:12 +00003423#ifdef OBJECT_PRINT
3424 void DeoptimizationOutputDataPrint(FILE* out);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003425#endif
3426};
3427
3428
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003429class SafepointEntry;
3430
3431
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003432// Code describes objects with on-the-fly generated machine code.
3433class Code: public HeapObject {
3434 public:
3435 // Opaque data type for encapsulating code flags like kind, inline
3436 // cache state, and arguments count.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003437 // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3438 // enumeration type has correct value range (see Issue 830 for more details).
3439 enum Flags {
3440 FLAGS_MIN_VALUE = kMinInt,
3441 FLAGS_MAX_VALUE = kMaxInt
3442 };
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443
3444 enum Kind {
3445 FUNCTION,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003446 OPTIMIZED_FUNCTION,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003447 STUB,
3448 BUILTIN,
3449 LOAD_IC,
3450 KEYED_LOAD_IC,
3451 CALL_IC,
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003452 KEYED_CALL_IC,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003453 STORE_IC,
3454 KEYED_STORE_IC,
danno@chromium.org40cb8782011-05-25 07:58:50 +00003455 UNARY_OP_IC,
3456 BINARY_OP_IC,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003457 COMPARE_IC,
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003458 // No more than 16 kinds. The value currently encoded in four bits in
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003459 // Flags.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003460
3461 // Pseudo-kinds.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003462 REGEXP = BUILTIN,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463 FIRST_IC_KIND = LOAD_IC,
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003464 LAST_IC_KIND = COMPARE_IC
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003465 };
3466
3467 enum {
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +00003468 NUMBER_OF_KINDS = LAST_IC_KIND + 1
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003469 };
3470
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003471 typedef int ExtraICState;
3472
3473 static const ExtraICState kNoExtraICState = 0;
3474
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00003475#ifdef ENABLE_DISASSEMBLER
3476 // Printing
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003477 static const char* Kind2String(Kind kind);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003478 static const char* ICState2String(InlineCacheState state);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003479 static const char* PropertyType2String(PropertyType type);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00003480 static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
whesse@chromium.org023421e2010-12-21 12:19:12 +00003481 inline void Disassemble(const char* name) {
3482 Disassemble(name, stdout);
3483 }
3484 void Disassemble(const char* name, FILE* out);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00003485#endif // ENABLE_DISASSEMBLER
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003486
3487 // [instruction_size]: Size of the native instructions
3488 inline int instruction_size();
3489 inline void set_instruction_size(int value);
3490
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003491 // [relocation_info]: Code relocation information
3492 DECL_ACCESSORS(relocation_info, ByteArray)
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003493 void InvalidateRelocation();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003494
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003495 // [deoptimization_data]: Array containing data for deopt.
3496 DECL_ACCESSORS(deoptimization_data, FixedArray)
3497
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003498 // [code_flushing_candidate]: Field only used during garbage
3499 // collection to hold code flushing candidates. The contents of this
3500 // field does not have to be traced during garbage collection since
3501 // it is only used by the garbage collector itself.
3502 DECL_ACCESSORS(next_code_flushing_candidate, Object)
3503
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003504 // Unchecked accessors to be used during GC.
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003505 inline ByteArray* unchecked_relocation_info();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003506 inline FixedArray* unchecked_deoptimization_data();
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003507
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508 inline int relocation_size();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003510 // [flags]: Various code flags.
3511 inline Flags flags();
3512 inline void set_flags(Flags flags);
3513
3514 // [flags]: Access to specific code flags.
3515 inline Kind kind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003516 inline InlineCacheState ic_state(); // Only valid for IC stubs.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003517 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003518 inline InLoopFlag ic_in_loop(); // Only valid for IC stubs.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003519 inline PropertyType type(); // Only valid for monomorphic IC stubs.
3520 inline int arguments_count(); // Only valid for call IC stubs.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521
3522 // Testers for IC stub kinds.
3523 inline bool is_inline_cache_stub();
3524 inline bool is_load_stub() { return kind() == LOAD_IC; }
3525 inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3526 inline bool is_store_stub() { return kind() == STORE_IC; }
3527 inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3528 inline bool is_call_stub() { return kind() == CALL_IC; }
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00003529 inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
danno@chromium.org40cb8782011-05-25 07:58:50 +00003530 inline bool is_unary_op_stub() {
3531 return kind() == UNARY_OP_IC;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003532 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00003533 inline bool is_binary_op_stub() {
3534 return kind() == BINARY_OP_IC;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003535 }
3536 inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003537
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003538 // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003539 inline int major_key();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003540 inline void set_major_key(int value);
3541
3542 // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3543 inline bool optimizable();
3544 inline void set_optimizable(bool value);
3545
3546 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3547 // deoptimization support.
3548 inline bool has_deoptimization_support();
3549 inline void set_has_deoptimization_support(bool value);
3550
3551 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3552 // how long the function has been marked for OSR and therefore which
3553 // level of loop nesting we are willing to do on-stack replacement
3554 // for.
3555 inline void set_allow_osr_at_loop_nesting_level(int level);
3556 inline int allow_osr_at_loop_nesting_level();
3557
3558 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3559 // reserved in the code prologue.
3560 inline unsigned stack_slots();
3561 inline void set_stack_slots(unsigned slots);
3562
3563 // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3564 // the instruction stream where the safepoint table starts.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00003565 inline unsigned safepoint_table_offset();
3566 inline void set_safepoint_table_offset(unsigned offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003567
3568 // [stack_check_table_start]: For kind FUNCTION, the offset in the
3569 // instruction stream where the stack check table starts.
ricow@chromium.org83aa5492011-02-07 12:42:56 +00003570 inline unsigned stack_check_table_offset();
3571 inline void set_stack_check_table_offset(unsigned offset);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003572
3573 // [check type]: For kind CALL_IC, tells how to check if the
3574 // receiver is valid for the given call.
3575 inline CheckType check_type();
3576 inline void set_check_type(CheckType value);
3577
danno@chromium.org40cb8782011-05-25 07:58:50 +00003578 // [type-recording unary op type]: For all UNARY_OP_IC.
3579 inline byte unary_op_type();
3580 inline void set_unary_op_type(byte value);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003581
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003582 // [type-recording binary op type]: For all TYPE_RECORDING_BINARY_OP_IC.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003583 inline byte binary_op_type();
3584 inline void set_binary_op_type(byte value);
3585 inline byte binary_op_result_type();
3586 inline void set_binary_op_result_type(byte value);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003587
3588 // [compare state]: For kind compare IC stubs, tells what state the
3589 // stub is in.
3590 inline byte compare_state();
3591 inline void set_compare_state(byte value);
3592
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003593 // Get the safepoint entry for the given pc.
3594 SafepointEntry GetSafepointEntry(Address pc);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003595
3596 // Mark this code object as not having a stack check table. Assumes kind
3597 // is FUNCTION.
3598 void SetNoStackCheckTable();
3599
3600 // Find the first map in an IC stub.
3601 Map* FindFirstMap();
kasper.lund7276f142008-07-30 08:49:36 +00003602
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 // Flags operations.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003604 static inline Flags ComputeFlags(
3605 Kind kind,
3606 InLoopFlag in_loop = NOT_IN_LOOP,
3607 InlineCacheState ic_state = UNINITIALIZED,
3608 ExtraICState extra_ic_state = kNoExtraICState,
3609 PropertyType type = NORMAL,
3610 int argc = -1,
3611 InlineCacheHolderFlag holder = OWN_MAP);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003612
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003613 static inline Flags ComputeMonomorphicFlags(
3614 Kind kind,
3615 PropertyType type,
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003616 ExtraICState extra_ic_state = kNoExtraICState,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003617 InlineCacheHolderFlag holder = OWN_MAP,
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003618 InLoopFlag in_loop = NOT_IN_LOOP,
3619 int argc = -1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620
3621 static inline Kind ExtractKindFromFlags(Flags flags);
kasper.lund7276f142008-07-30 08:49:36 +00003622 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00003623 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003624 static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003625 static inline PropertyType ExtractTypeFromFlags(Flags flags);
3626 static inline int ExtractArgumentsCountFromFlags(Flags flags);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003627 static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 static inline Flags RemoveTypeFromFlags(Flags flags);
3629
ager@chromium.org8bb60582008-12-11 12:02:20 +00003630 // Convert a target address into a code object.
3631 static inline Code* GetCodeFromTargetAddress(Address address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003632
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00003633 // Convert an entry address into an object.
3634 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3635
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003636 // Returns the address of the first instruction.
3637 inline byte* instruction_start();
3638
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003639 // Returns the address right after the last instruction.
3640 inline byte* instruction_end();
3641
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003642 // Returns the size of the instructions, padding, and relocation information.
3643 inline int body_size();
3644
3645 // Returns the address of the first relocation info (read backwards!).
3646 inline byte* relocation_start();
3647
3648 // Code entry point.
3649 inline byte* entry();
3650
3651 // Returns true if pc is inside this object's instructions.
3652 inline bool contains(byte* pc);
3653
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654 // Relocate the code by delta bytes. Called to signal that this code
3655 // object has been moved by delta bytes.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003656 void Relocate(intptr_t delta);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003657
3658 // Migrate code described by desc.
3659 void CopyFrom(const CodeDesc& desc);
3660
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003661 // Returns the object size for a given body (used for allocation).
3662 static int SizeFor(int body_size) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663 ASSERT_SIZE_TAG_ALIGNED(body_size);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003664 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665 }
3666
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003667 // Calculate the size of the code object to report for log events. This takes
3668 // the layout of the code object into account.
3669 int ExecutableSize() {
3670 // Check that the assumptions about the layout of the code object holds.
ager@chromium.orga1645e22009-09-09 19:27:10 +00003671 ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3672 Code::kHeaderSize);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003673 return instruction_size() + Code::kHeaderSize;
3674 }
3675
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003676 // Locating source position.
3677 int SourcePosition(Address pc);
3678 int SourceStatementPosition(Address pc);
3679
3680 // Casting.
3681 static inline Code* cast(Object* obj);
3682
3683 // Dispatched behavior.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00003684 int CodeSize() { return SizeFor(body_size()); }
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00003685 inline void CodeIterateBody(ObjectVisitor* v);
3686
3687 template<typename StaticVisitor>
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003688 inline void CodeIterateBody(Heap* heap);
whesse@chromium.org023421e2010-12-21 12:19:12 +00003689#ifdef OBJECT_PRINT
3690 inline void CodePrint() {
3691 CodePrint(stdout);
3692 }
3693 void CodePrint(FILE* out);
3694#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696 void CodeVerify();
3697#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003698
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003699 // Returns the isolate/heap this code object belongs to.
3700 inline Isolate* isolate();
3701 inline Heap* heap();
3702
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003703 // Max loop nesting marker used to postpose OSR. We don't take loop
3704 // nesting that is deeper than 5 levels into account.
3705 static const int kMaxLoopNestingMarker = 6;
3706
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00003708 static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00003709 static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003710 static const int kDeoptimizationDataOffset =
3711 kRelocationInfoOffset + kPointerSize;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003712 static const int kNextCodeFlushingCandidateOffset =
3713 kDeoptimizationDataOffset + kPointerSize;
3714 static const int kFlagsOffset =
3715 kNextCodeFlushingCandidateOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003716
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003717 static const int kKindSpecificFlagsOffset = kFlagsOffset + kIntSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003718 static const int kKindSpecificFlagsSize = 2 * kIntSize;
3719
3720 static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3721 kKindSpecificFlagsSize;
3722
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003723 // Add padding to align the instruction start following right after
kasperl@chromium.org061ef742009-02-27 12:16:20 +00003724 // the Code object header.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003725 static const int kHeaderSize =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003726 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
kasper.lund7276f142008-07-30 08:49:36 +00003727
3728 // Byte offsets within kKindSpecificFlagsOffset.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003729 static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3730 static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3731 static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3732 static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00003733 static const int kExternalArrayTypeOffset = kKindSpecificFlagsOffset;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003734
3735 static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +00003736 static const int kUnaryOpTypeOffset = kStubMajorKeyOffset + 1;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003737 static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
3738 static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3739
3740 static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3741 static const int kAllowOSRAtLoopNestingLevelOffset =
3742 kHasDeoptimizationSupportOffset + 1;
3743
ricow@chromium.org83aa5492011-02-07 12:42:56 +00003744 static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3745 static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003746
3747 // Flags layout.
kasper.lund7276f142008-07-30 08:49:36 +00003748 static const int kFlagsICStateShift = 0;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003749 static const int kFlagsICInLoopShift = 3;
whesse@chromium.org2c186ca2010-06-16 11:32:39 +00003750 static const int kFlagsTypeShift = 4;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003751 static const int kFlagsKindShift = 8;
3752 static const int kFlagsICHolderShift = 12;
3753 static const int kFlagsExtraICStateShift = 13;
3754 static const int kFlagsArgumentsCountShift = 15;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003755
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003756 static const int kFlagsICStateMask = 0x00000007; // 00000000111
3757 static const int kFlagsICInLoopMask = 0x00000008; // 00000001000
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003758 static const int kFlagsTypeMask = 0x000000F0; // 00001110000
3759 static const int kFlagsKindMask = 0x00000F00; // 11110000000
3760 static const int kFlagsCacheInPrototypeMapMask = 0x00001000;
3761 static const int kFlagsExtraICStateMask = 0x00006000;
3762 static const int kFlagsArgumentsCountMask = 0xFFFF8000;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003763
3764 static const int kFlagsNotUsedInLookup =
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003765 (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767 private:
3768 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3769};
3770
3771
3772// All heap objects have a Map that describes their structure.
3773// A Map contains information about:
3774// - Size information about the object
3775// - How to iterate over an object (for garbage collection)
3776class Map: public HeapObject {
3777 public:
ager@chromium.org32912102009-01-16 10:38:43 +00003778 // Instance size.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00003779 // Size in bytes or kVariableSizeSentinel if instances do not have
3780 // a fixed size.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781 inline int instance_size();
3782 inline void set_instance_size(int value);
3783
ager@chromium.org7c537e22008-10-16 08:43:32 +00003784 // Count of properties allocated in the object.
3785 inline int inobject_properties();
3786 inline void set_inobject_properties(int value);
3787
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00003788 // Count of property fields pre-allocated in the object when first allocated.
3789 inline int pre_allocated_property_fields();
3790 inline void set_pre_allocated_property_fields(int value);
3791
ager@chromium.org32912102009-01-16 10:38:43 +00003792 // Instance type.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793 inline InstanceType instance_type();
3794 inline void set_instance_type(InstanceType value);
3795
ager@chromium.org32912102009-01-16 10:38:43 +00003796 // Tells how many unused property fields are available in the
3797 // instance (only used for JSObject in fast mode).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798 inline int unused_property_fields();
3799 inline void set_unused_property_fields(int value);
3800
ager@chromium.org32912102009-01-16 10:38:43 +00003801 // Bit field.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003802 inline byte bit_field();
3803 inline void set_bit_field(byte value);
3804
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00003805 // Bit field 2.
3806 inline byte bit_field2();
3807 inline void set_bit_field2(byte value);
3808
danno@chromium.org40cb8782011-05-25 07:58:50 +00003809 // Bit field 3.
3810 // TODO(1399): It should be possible to make room for bit_field3 in the map
3811 // without overloading the instance descriptors field (and storing it in the
3812 // DescriptorArray when the map has one).
3813 inline int bit_field3();
3814 inline void set_bit_field3(int value);
3815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816 // Tells whether the object in the prototype property will be used
3817 // for instances created from this function. If the prototype
3818 // property is set to a value that is not a JSObject, the prototype
3819 // property will not be used to create instances of the function.
3820 // See ECMA-262, 13.2.2.
3821 inline void set_non_instance_prototype(bool value);
3822 inline bool has_non_instance_prototype();
3823
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00003824 // Tells whether function has special prototype property. If not, prototype
3825 // property will not be created when accessed (will return undefined),
3826 // and construction from this function will not be allowed.
3827 inline void set_function_with_prototype(bool value);
3828 inline bool function_with_prototype();
3829
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830 // Tells whether the instance with this map should be ignored by the
3831 // __proto__ accessor.
3832 inline void set_is_hidden_prototype() {
3833 set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3834 }
3835
3836 inline bool is_hidden_prototype() {
3837 return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3838 }
3839
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003840 // Records and queries whether the instance has a named interceptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841 inline void set_has_named_interceptor() {
3842 set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3843 }
3844
3845 inline bool has_named_interceptor() {
3846 return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3847 }
3848
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003849 // Records and queries whether the instance has an indexed interceptor.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850 inline void set_has_indexed_interceptor() {
3851 set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3852 }
3853
3854 inline bool has_indexed_interceptor() {
3855 return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3856 }
3857
3858 // Tells whether the instance is undetectable.
3859 // An undetectable object is a special class of JSObject: 'typeof' operator
3860 // returns undefined, ToBoolean returns false. Otherwise it behaves like
3861 // a normal JS object. It is useful for implementing undetectable
3862 // document.all in Firefox & Safari.
3863 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3864 inline void set_is_undetectable() {
3865 set_bit_field(bit_field() | (1 << kIsUndetectable));
3866 }
3867
3868 inline bool is_undetectable() {
3869 return ((1 << kIsUndetectable) & bit_field()) != 0;
3870 }
3871
3872 // Tells whether the instance has a call-as-function handler.
3873 inline void set_has_instance_call_handler() {
3874 set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3875 }
3876
3877 inline bool has_instance_call_handler() {
3878 return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3879 }
3880
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00003881 inline void set_is_extensible(bool value);
3882 inline bool is_extensible();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003883
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003884 inline void set_elements_kind(JSObject::ElementsKind elements_kind) {
3885 ASSERT(elements_kind < JSObject::kElementsKindCount);
3886 ASSERT(JSObject::kElementsKindCount <= (1 << kElementsKindBitCount));
3887 set_bit_field2((bit_field2() & ~kElementsKindMask) |
3888 (elements_kind << kElementsKindShift));
3889 ASSERT(this->elements_kind() == elements_kind);
3890 }
3891
3892 inline JSObject::ElementsKind elements_kind() {
3893 return static_cast<JSObject::ElementsKind>(
3894 (bit_field2() & kElementsKindMask) >> kElementsKindShift);
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003895 }
3896
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00003897 inline bool has_fast_elements() {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003898 return elements_kind() == JSObject::FAST_ELEMENTS;
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00003899 }
3900
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003901 inline bool has_fast_double_elements() {
3902 return elements_kind() == JSObject::FAST_DOUBLE_ELEMENTS;
3903 }
3904
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00003905 inline bool has_external_array_elements() {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003906 JSObject::ElementsKind kind(elements_kind());
3907 return kind >= JSObject::FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
3908 kind <= JSObject::LAST_EXTERNAL_ARRAY_ELEMENTS_KIND;
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00003909 }
3910
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003911 // Tells whether the map is attached to SharedFunctionInfo
3912 // (for inobject slack tracking).
3913 inline void set_attached_to_shared_function_info(bool value);
3914
3915 inline bool attached_to_shared_function_info();
3916
3917 // Tells whether the map is shared between objects that may have different
3918 // behavior. If true, the map should never be modified, instead a clone
3919 // should be created and modified.
3920 inline void set_is_shared(bool value);
3921
3922 inline bool is_shared();
3923
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924 // Tells whether the instance needs security checks when accessing its
3925 // properties.
ager@chromium.org870a0b62008-11-04 11:43:05 +00003926 inline void set_is_access_check_needed(bool access_check_needed);
3927 inline bool is_access_check_needed();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928
3929 // [prototype]: implicit prototype object.
3930 DECL_ACCESSORS(prototype, Object)
3931
3932 // [constructor]: points back to the function responsible for this map.
3933 DECL_ACCESSORS(constructor, Object)
3934
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003935 inline JSFunction* unchecked_constructor();
3936
danno@chromium.org40cb8782011-05-25 07:58:50 +00003937 // Should only be called by the code that initializes map to set initial valid
3938 // value of the instance descriptor member.
3939 inline void init_instance_descriptors();
3940
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941 // [instance descriptors]: describes the object.
3942 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3943
danno@chromium.org40cb8782011-05-25 07:58:50 +00003944 // Sets the instance descriptor array for the map to be an empty descriptor
3945 // array.
3946 inline void clear_instance_descriptors();
3947
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003948 // [stub cache]: contains stubs compiled for this map.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003949 DECL_ACCESSORS(code_cache, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00003951 // [prototype transitions]: cache of prototype transitions.
3952 // Prototype transition is a transition that happens
3953 // when we change object's prototype to a new one.
3954 // Cache format:
3955 // 0: finger - index of the first free cell in the cache
3956 // 1 + 2 * i: prototype
3957 // 2 + 2 * i: target map
3958 DECL_ACCESSORS(prototype_transitions, FixedArray)
3959 inline FixedArray* unchecked_prototype_transitions();
3960
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00003961 static const int kProtoTransitionHeaderSize = 1;
3962 static const int kProtoTransitionNumberOfEntriesOffset = 0;
3963 static const int kProtoTransitionElementsPerEntry = 2;
3964 static const int kProtoTransitionPrototypeOffset = 0;
3965 static const int kProtoTransitionMapOffset = 1;
3966
3967 inline int NumberOfProtoTransitions() {
3968 FixedArray* cache = unchecked_prototype_transitions();
3969 if (cache->length() == 0) return 0;
3970 return
3971 Smi::cast(cache->get(kProtoTransitionNumberOfEntriesOffset))->value();
3972 }
3973
3974 inline void SetNumberOfProtoTransitions(int value) {
3975 FixedArray* cache = unchecked_prototype_transitions();
3976 ASSERT(cache->length() != 0);
3977 cache->set_unchecked(kProtoTransitionNumberOfEntriesOffset,
3978 Smi::FromInt(value));
3979 }
3980
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003981 // Lookup in the map's instance descriptors and fill out the result
3982 // with the given holder if the name is found. The holder may be
3983 // NULL when this function is used from the compiler.
3984 void LookupInDescriptors(JSObject* holder,
3985 String* name,
3986 LookupResult* result);
3987
lrn@chromium.org303ada72010-10-27 09:33:13 +00003988 MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003989
lrn@chromium.org303ada72010-10-27 09:33:13 +00003990 MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3991 NormalizedMapSharingMode sharing);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003992
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003993 // Returns a copy of the map, with all transitions dropped from the
3994 // instance descriptors.
lrn@chromium.org303ada72010-10-27 09:33:13 +00003995 MUST_USE_RESULT MaybeObject* CopyDropTransitions();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003996
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003997 // Returns this map if it already has elements that are fast, otherwise
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00003998 // returns a copy of the map, with all transitions dropped from the
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003999 // descriptors and the ElementsKind set to FAST_ELEMENTS.
lrn@chromium.org303ada72010-10-27 09:33:13 +00004000 MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004001
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004002 // Returns this map if it already has fast elements that are doubles,
4003 // otherwise returns a copy of the map, with all transitions dropped from the
4004 // descriptors and the ElementsKind set to FAST_DOUBLE_ELEMENTS.
4005 MUST_USE_RESULT inline MaybeObject* GetFastDoubleElementsMap();
4006
4007 // Returns this map if already has dictionary elements, otherwise returns a
4008 // copy of the map, with all transitions dropped from the descriptors and the
4009 // ElementsKind set to DICTIONARY_ELEMENTS.
lrn@chromium.org303ada72010-10-27 09:33:13 +00004010 MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
fschneider@chromium.org40b9da32010-06-28 11:29:21 +00004011
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004012 // Returns a new map with all transitions dropped from the descriptors and the
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004013 // ElementsKind set to one of the value corresponding to array_type.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004014 MUST_USE_RESULT MaybeObject* GetExternalArrayElementsMap(
4015 ExternalArrayType array_type,
4016 bool safe_to_add_transition);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00004017
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018 // Returns the property index for name (only valid for FAST MODE).
4019 int PropertyIndexFor(String* name);
4020
4021 // Returns the next free property index (only valid for FAST MODE).
4022 int NextFreePropertyIndex();
4023
4024 // Returns the number of properties described in instance_descriptors.
4025 int NumberOfDescribedProperties();
4026
4027 // Casting.
4028 static inline Map* cast(Object* obj);
4029
4030 // Locate an accessor in the instance descriptor.
4031 AccessorDescriptor* FindAccessor(String* name);
4032
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 // Code cache operations.
4034
4035 // Clears the code cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004036 inline void ClearCodeCache(Heap* heap);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037
4038 // Update code cache.
lrn@chromium.org303ada72010-10-27 09:33:13 +00004039 MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004040
4041 // Returns the found code or undefined if absent.
4042 Object* FindInCodeCache(String* name, Code::Flags flags);
4043
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004044 // Returns the non-negative index of the code object if it is in the
4045 // cache and -1 otherwise.
sgjesse@chromium.org99a37fa2010-03-11 09:23:46 +00004046 int IndexInCodeCache(Object* name, Code* code);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004047
4048 // Removes a code object from the code cache at the given index.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004049 void RemoveFromCodeCache(String* name, Code* code, int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004051 // For every transition in this map, makes the transition's
4052 // target's prototype pointer point back to this map.
4053 // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
4054 void CreateBackPointers();
4055
4056 // Set all map transitions from this map to dead maps to null.
4057 // Also, restore the original prototype on the targets of these
4058 // transitions, so that we do not process this map again while
4059 // following back pointers.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004060 void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004061
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004062 // Computes a hash value for this map, to be used in HashTables and such.
4063 int Hash();
4064
4065 // Compares this map to another to see if they describe equivalent objects.
4066 // If |mode| is set to CLEAR_INOBJECT_PROPERTIES, |other| is treated as if
4067 // it had exactly zero inobject properties.
4068 // The "shared" flags of both this map and |other| are ignored.
4069 bool EquivalentToForNormalization(Map* other, PropertyNormalizationMode mode);
4070
4071 // Returns true if this map and |other| describe equivalent objects.
4072 // The "shared" flags of both this map and |other| are ignored.
4073 bool EquivalentTo(Map* other) {
4074 return EquivalentToForNormalization(other, KEEP_INOBJECT_PROPERTIES);
4075 }
4076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00004078#ifdef OBJECT_PRINT
4079 inline void MapPrint() {
4080 MapPrint(stdout);
4081 }
4082 void MapPrint(FILE* out);
4083#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004085 void MapVerify();
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004086 void SharedMapVerify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004087#endif
4088
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004089 inline int visitor_id();
4090 inline void set_visitor_id(int visitor_id);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004091
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004092 // Returns the isolate/heap this map belongs to.
4093 inline Isolate* isolate();
4094 inline Heap* heap();
4095
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004096 typedef void (*TraverseCallback)(Map* map, void* data);
4097
4098 void TraverseTransitionTree(TraverseCallback callback, void* data);
4099
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00004100 static const int kMaxCachedPrototypeTransitions = 256;
4101
4102 Object* GetPrototypeTransition(Object* prototype);
4103
4104 MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
4105
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004106 static const int kMaxPreAllocatedPropertyFields = 255;
4107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108 // Layout description.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004109 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
4110 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004111 static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
4112 static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004113 // Storage for instance descriptors is overloaded to also contain additional
4114 // map flags when unused (bit_field3). When the map has instance descriptors,
4115 // the flags are transferred to the instance descriptor array and accessed
4116 // through an extra indirection.
4117 // TODO(1399): It should be possible to make room for bit_field3 in the map
4118 // without overloading the instance descriptors field, but the map is
4119 // currently perfectly aligned to 32 bytes and extending it at all would
4120 // double its size. After the increment GC work lands, this size restriction
4121 // could be loosened and bit_field3 moved directly back in the map.
4122 static const int kInstanceDescriptorsOrBitField3Offset =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004123 kConstructorOffset + kPointerSize;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004124 static const int kCodeCacheOffset =
4125 kInstanceDescriptorsOrBitField3Offset + kPointerSize;
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00004126 static const int kPrototypeTransitionsOffset =
4127 kCodeCacheOffset + kPointerSize;
4128 static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004129 static const int kSize = MAP_POINTER_ALIGN(kPadStart);
4130
4131 // Layout of pointer fields. Heap iteration code relies on them
4132 // being continiously allocated.
4133 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
4134 static const int kPointerFieldsEndOffset =
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00004135 Map::kPrototypeTransitionsOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004136
ager@chromium.org7c537e22008-10-16 08:43:32 +00004137 // Byte offsets within kInstanceSizesOffset.
4138 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
ager@chromium.orga1645e22009-09-09 19:27:10 +00004139 static const int kInObjectPropertiesByte = 1;
4140 static const int kInObjectPropertiesOffset =
4141 kInstanceSizesOffset + kInObjectPropertiesByte;
4142 static const int kPreAllocatedPropertyFieldsByte = 2;
4143 static const int kPreAllocatedPropertyFieldsOffset =
4144 kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00004145 static const int kVisitorIdByte = 3;
4146 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004148 // Byte offsets within kInstanceAttributesOffset attributes.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004149 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
4150 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
4151 static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004152 static const int kBitField2Offset = kInstanceAttributesOffset + 3;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004153
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004154 STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
4155
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004156 // Bit positions for bit field.
mads.s.ager31e71382008-08-13 09:32:07 +00004157 static const int kUnused = 0; // To be used for marking recently used maps.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004158 static const int kHasNonInstancePrototype = 1;
4159 static const int kIsHiddenPrototype = 2;
4160 static const int kHasNamedInterceptor = 3;
4161 static const int kHasIndexedInterceptor = 4;
4162 static const int kIsUndetectable = 5;
4163 static const int kHasInstanceCallHandler = 6;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004164 static const int kIsAccessCheckNeeded = 7;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004165
ager@chromium.orge2902be2009-06-08 12:21:35 +00004166 // Bit positions for bit field 2
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004167 static const int kIsExtensible = 0;
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004168 static const int kFunctionWithPrototype = 1;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004169 static const int kStringWrapperSafeForDefaultValueOf = 2;
4170 static const int kAttachedToSharedFunctionInfo = 3;
4171 // No bits can be used after kElementsKindFirstBit, they are all reserved for
4172 // storing ElementKind. for anything other than storing the ElementKind.
4173 static const int kElementsKindShift = 4;
4174 static const int kElementsKindBitCount = 4;
4175
4176 // Derived values from bit field 2
4177 static const int kElementsKindMask = (-1 << kElementsKindShift) &
4178 ((1 << (kElementsKindShift + kElementsKindBitCount)) - 1);
4179 static const int8_t kMaximumBitField2FastElementValue = static_cast<int8_t>(
4180 (JSObject::FAST_ELEMENTS + 1) << Map::kElementsKindShift) - 1;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004181
4182 // Bit positions for bit field 3
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004183 static const int kIsShared = 0;
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004184
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004185 // Layout of the default cache. It holds alternating name and code objects.
4186 static const int kCodeCacheEntrySize = 2;
4187 static const int kCodeCacheEntryNameOffset = 0;
4188 static const int kCodeCacheEntryCodeOffset = 1;
4189
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004190 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
4191 kPointerFieldsEndOffset,
4192 kSize> BodyDescriptor;
4193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004194 private:
4195 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
4196};
4197
4198
4199// An abstract superclass, a marker class really, for simple structure classes.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004200// It doesn't carry much functionality but allows struct classes to be
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004201// identified in the type system.
4202class Struct: public HeapObject {
4203 public:
4204 inline void InitializeBody(int object_size);
4205 static inline Struct* cast(Object* that);
4206};
4207
4208
mads.s.ager31e71382008-08-13 09:32:07 +00004209// Script describes a script which has been added to the VM.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004210class Script: public Struct {
4211 public:
ager@chromium.orge2902be2009-06-08 12:21:35 +00004212 // Script types.
4213 enum Type {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004214 TYPE_NATIVE = 0,
4215 TYPE_EXTENSION = 1,
4216 TYPE_NORMAL = 2
ager@chromium.orge2902be2009-06-08 12:21:35 +00004217 };
4218
4219 // Script compilation types.
4220 enum CompilationType {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004221 COMPILATION_TYPE_HOST = 0,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +00004222 COMPILATION_TYPE_EVAL = 1
ager@chromium.orge2902be2009-06-08 12:21:35 +00004223 };
4224
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004225 // [source]: the script source.
4226 DECL_ACCESSORS(source, Object)
4227
4228 // [name]: the script name.
4229 DECL_ACCESSORS(name, Object)
4230
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004231 // [id]: the script id.
4232 DECL_ACCESSORS(id, Object)
4233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004234 // [line_offset]: script line offset in resource from where it was extracted.
4235 DECL_ACCESSORS(line_offset, Smi)
4236
4237 // [column_offset]: script column offset in resource from where it was
4238 // extracted.
4239 DECL_ACCESSORS(column_offset, Smi)
4240
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004241 // [data]: additional data associated with this script.
4242 DECL_ACCESSORS(data, Object)
4243
ager@chromium.org9085a012009-05-11 19:22:57 +00004244 // [context_data]: context data for the context this script was compiled in.
4245 DECL_ACCESSORS(context_data, Object)
4246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004247 // [wrapper]: the wrapper cache.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004248 DECL_ACCESSORS(wrapper, Foreign)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004249
4250 // [type]: the script type.
4251 DECL_ACCESSORS(type, Smi)
4252
ager@chromium.orge2902be2009-06-08 12:21:35 +00004253 // [compilation]: how the the script was compiled.
4254 DECL_ACCESSORS(compilation_type, Smi)
4255
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004256 // [line_ends]: FixedArray of line ends positions.
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00004257 DECL_ACCESSORS(line_ends, Object)
iposva@chromium.org245aa852009-02-10 00:49:54 +00004258
sgjesse@chromium.org98180592009-12-02 08:17:28 +00004259 // [eval_from_shared]: for eval scripts the shared funcion info for the
4260 // function from which eval was called.
4261 DECL_ACCESSORS(eval_from_shared, Object)
ager@chromium.orge2902be2009-06-08 12:21:35 +00004262
4263 // [eval_from_instructions_offset]: the instruction offset in the code for the
4264 // function from which eval was called where eval was called.
4265 DECL_ACCESSORS(eval_from_instructions_offset, Smi)
4266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004267 static inline Script* cast(Object* obj);
4268
sgjesse@chromium.org152a0b02009-10-07 13:50:16 +00004269 // If script source is an external string, check that the underlying
4270 // resource is accessible. Otherwise, always return true.
4271 inline bool HasValidSource();
4272
whesse@chromium.org023421e2010-12-21 12:19:12 +00004273#ifdef OBJECT_PRINT
4274 inline void ScriptPrint() {
4275 ScriptPrint(stdout);
4276 }
4277 void ScriptPrint(FILE* out);
4278#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004279#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004280 void ScriptVerify();
4281#endif
4282
ager@chromium.org236ad962008-09-25 09:45:57 +00004283 static const int kSourceOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004284 static const int kNameOffset = kSourceOffset + kPointerSize;
4285 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4286 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004287 static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
ager@chromium.org9085a012009-05-11 19:22:57 +00004288 static const int kContextOffset = kDataOffset + kPointerSize;
4289 static const int kWrapperOffset = kContextOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004290 static const int kTypeOffset = kWrapperOffset + kPointerSize;
ager@chromium.orge2902be2009-06-08 12:21:35 +00004291 static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
sgjesse@chromium.org499aaa52009-11-30 08:07:20 +00004292 static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
4293 static const int kIdOffset = kLineEndsOffset + kPointerSize;
sgjesse@chromium.org98180592009-12-02 08:17:28 +00004294 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
ager@chromium.orge2902be2009-06-08 12:21:35 +00004295 static const int kEvalFrominstructionsOffsetOffset =
sgjesse@chromium.org98180592009-12-02 08:17:28 +00004296 kEvalFromSharedOffset + kPointerSize;
ager@chromium.orge2902be2009-06-08 12:21:35 +00004297 static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004298
4299 private:
4300 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4301};
4302
4303
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004304// List of builtin functions we want to identify to improve code
4305// generation.
4306//
4307// Each entry has a name of a global object property holding an object
4308// optionally followed by ".prototype", a name of a builtin function
4309// on the object (the one the id is set for), and a label.
4310//
4311// Installation of ids for the selected builtin functions is handled
4312// by the bootstrapper.
4313//
4314// NOTE: Order is important: math functions should be at the end of
4315// the list and MathFloor should be the first math function.
4316#define FUNCTIONS_WITH_ID_LIST(V) \
4317 V(Array.prototype, push, ArrayPush) \
4318 V(Array.prototype, pop, ArrayPop) \
4319 V(String.prototype, charCodeAt, StringCharCodeAt) \
4320 V(String.prototype, charAt, StringCharAt) \
4321 V(String, fromCharCode, StringFromCharCode) \
4322 V(Math, floor, MathFloor) \
4323 V(Math, round, MathRound) \
4324 V(Math, ceil, MathCeil) \
4325 V(Math, abs, MathAbs) \
4326 V(Math, log, MathLog) \
4327 V(Math, sin, MathSin) \
4328 V(Math, cos, MathCos) \
4329 V(Math, tan, MathTan) \
4330 V(Math, asin, MathASin) \
4331 V(Math, acos, MathACos) \
4332 V(Math, atan, MathATan) \
4333 V(Math, exp, MathExp) \
4334 V(Math, sqrt, MathSqrt) \
4335 V(Math, pow, MathPow)
4336
4337
4338enum BuiltinFunctionId {
4339#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
4340 k##name,
4341 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4342#undef DECLARE_FUNCTION_ID
4343 // Fake id for a special case of Math.pow. Note, it continues the
4344 // list of math functions.
4345 kMathPowHalf,
4346 kFirstMathFunctionId = kMathFloor
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004347};
4348
4349
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004350// SharedFunctionInfo describes the JSFunction information that can be
4351// shared by multiple instances of the function.
4352class SharedFunctionInfo: public HeapObject {
4353 public:
4354 // [name]: Function name.
4355 DECL_ACCESSORS(name, Object)
4356
4357 // [code]: Function code.
4358 DECL_ACCESSORS(code, Code)
4359
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004360 // [scope_info]: Scope info.
ager@chromium.orgb5737492010-07-15 09:29:43 +00004361 DECL_ACCESSORS(scope_info, SerializedScopeInfo)
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004362
ager@chromium.org5aa501c2009-06-23 07:57:28 +00004363 // [construct stub]: Code stub for constructing instances of this function.
4364 DECL_ACCESSORS(construct_stub, Code)
4365
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004366 inline Code* unchecked_code();
4367
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004368 // Returns if this function has been compiled to native code yet.
4369 inline bool is_compiled();
4370
4371 // [length]: The function length - usually the number of declared parameters.
4372 // Use up to 2^30 parameters.
4373 inline int length();
4374 inline void set_length(int value);
4375
4376 // [formal parameter count]: The declared number of parameters.
4377 inline int formal_parameter_count();
4378 inline void set_formal_parameter_count(int value);
4379
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004380 // Set the formal parameter count so the function code will be
4381 // called without using argument adaptor frames.
4382 inline void DontAdaptArguments();
4383
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004384 // [expected_nof_properties]: Expected number of properties for the function.
4385 inline int expected_nof_properties();
4386 inline void set_expected_nof_properties(int value);
4387
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004388 // Inobject slack tracking is the way to reclaim unused inobject space.
4389 //
4390 // The instance size is initially determined by adding some slack to
4391 // expected_nof_properties (to allow for a few extra properties added
4392 // after the constructor). There is no guarantee that the extra space
4393 // will not be wasted.
4394 //
4395 // Here is the algorithm to reclaim the unused inobject space:
4396 // - Detect the first constructor call for this SharedFunctionInfo.
4397 // When it happens enter the "in progress" state: remember the
4398 // constructor's initial_map and install a special construct stub that
4399 // counts constructor calls.
4400 // - While the tracking is in progress create objects filled with
4401 // one_pointer_filler_map instead of undefined_value. This way they can be
4402 // resized quickly and safely.
4403 // - Once enough (kGenerousAllocationCount) objects have been created
4404 // compute the 'slack' (traverse the map transition tree starting from the
4405 // initial_map and find the lowest value of unused_property_fields).
4406 // - Traverse the transition tree again and decrease the instance size
4407 // of every map. Existing objects will resize automatically (they are
4408 // filled with one_pointer_filler_map). All further allocations will
4409 // use the adjusted instance size.
4410 // - Decrease expected_nof_properties so that an allocations made from
4411 // another context will use the adjusted instance size too.
4412 // - Exit "in progress" state by clearing the reference to the initial_map
4413 // and setting the regular construct stub (generic or inline).
4414 //
4415 // The above is the main event sequence. Some special cases are possible
4416 // while the tracking is in progress:
4417 //
4418 // - GC occurs.
4419 // Check if the initial_map is referenced by any live objects (except this
4420 // SharedFunctionInfo). If it is, continue tracking as usual.
4421 // If it is not, clear the reference and reset the tracking state. The
4422 // tracking will be initiated again on the next constructor call.
4423 //
4424 // - The constructor is called from another context.
4425 // Immediately complete the tracking, perform all the necessary changes
4426 // to maps. This is necessary because there is no efficient way to track
4427 // multiple initial_maps.
4428 // Proceed to create an object in the current context (with the adjusted
4429 // size).
4430 //
4431 // - A different constructor function sharing the same SharedFunctionInfo is
4432 // called in the same context. This could be another closure in the same
4433 // context, or the first function could have been disposed.
4434 // This is handled the same way as the previous case.
4435 //
4436 // Important: inobject slack tracking is not attempted during the snapshot
4437 // creation.
4438
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00004439 static const int kGenerousAllocationCount = 8;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004440
4441 // [construction_count]: Counter for constructor calls made during
4442 // the tracking phase.
4443 inline int construction_count();
4444 inline void set_construction_count(int value);
4445
4446 // [initial_map]: initial map of the first function called as a constructor.
4447 // Saved for the duration of the tracking phase.
4448 // This is a weak link (GC resets it to undefined_value if no other live
4449 // object reference this map).
4450 DECL_ACCESSORS(initial_map, Object)
4451
4452 // True if the initial_map is not undefined and the countdown stub is
4453 // installed.
4454 inline bool IsInobjectSlackTrackingInProgress();
4455
4456 // Starts the tracking.
4457 // Stores the initial map and installs the countdown stub.
4458 // IsInobjectSlackTrackingInProgress is normally true after this call,
4459 // except when tracking have not been started (e.g. the map has no unused
4460 // properties or the snapshot is being built).
4461 void StartInobjectSlackTracking(Map* map);
4462
4463 // Completes the tracking.
4464 // IsInobjectSlackTrackingInProgress is false after this call.
4465 void CompleteInobjectSlackTracking();
4466
4467 // Clears the initial_map before the GC marking phase to ensure the reference
4468 // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4469 void DetachInitialMap();
4470
4471 // Restores the link to the initial map after the GC marking phase.
4472 // IsInobjectSlackTrackingInProgress is true after this call.
4473 void AttachInitialMap(Map* map);
4474
4475 // False if there are definitely no live objects created from this function.
4476 // True if live objects _may_ exist (existence not guaranteed).
4477 // May go back from true to false after GC.
4478 inline bool live_objects_may_exist();
4479
4480 inline void set_live_objects_may_exist(bool value);
4481
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004482 // [instance class name]: class name for instances.
4483 DECL_ACCESSORS(instance_class_name, Object)
4484
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004485 // [function data]: This field holds some additional data for function.
4486 // Currently it either has FunctionTemplateInfo to make benefit the API
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004487 // or Smi identifying a builtin function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004488 // In the long run we don't want all functions to have this field but
4489 // we can fix that when we have a better model for storing hidden data
4490 // on objects.
4491 DECL_ACCESSORS(function_data, Object)
4492
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004493 inline bool IsApiFunction();
4494 inline FunctionTemplateInfo* get_api_func_data();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004495 inline bool HasBuiltinFunctionId();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004496 inline BuiltinFunctionId builtin_function_id();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004498 // [script info]: Script from which the function originates.
4499 DECL_ACCESSORS(script, Object)
4500
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004501 // [num_literals]: Number of literals used by this function.
4502 inline int num_literals();
4503 inline void set_num_literals(int value);
4504
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004505 // [start_position_and_type]: Field used to store both the source code
4506 // position, whether or not the function is a function expression,
4507 // and whether or not the function is a toplevel function. The two
4508 // least significants bit indicates whether the function is an
4509 // expression and the rest contains the source code position.
4510 inline int start_position_and_type();
4511 inline void set_start_position_and_type(int value);
4512
4513 // [debug info]: Debug information.
4514 DECL_ACCESSORS(debug_info, Object)
4515
kasperl@chromium.orgd1e3e722009-04-14 13:38:25 +00004516 // [inferred name]: Name inferred from variable or property
4517 // assignment of this function. Used to facilitate debugging and
4518 // profiling of JavaScript code written in OO style, where almost
4519 // all functions are anonymous but are assigned to object
4520 // properties.
4521 DECL_ACCESSORS(inferred_name, String)
4522
ager@chromium.orgb61a0d12010-10-13 08:35:23 +00004523 // The function's name if it is non-empty, otherwise the inferred name.
4524 String* DebugName();
4525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004526 // Position of the 'function' token in the script source.
4527 inline int function_token_position();
4528 inline void set_function_token_position(int function_token_position);
4529
4530 // Position of this function in the script source.
4531 inline int start_position();
4532 inline void set_start_position(int start_position);
4533
4534 // End position of this function in the script source.
4535 inline int end_position();
4536 inline void set_end_position(int end_position);
4537
4538 // Is this function a function expression in the source code.
4539 inline bool is_expression();
4540 inline void set_is_expression(bool value);
4541
ager@chromium.orga1645e22009-09-09 19:27:10 +00004542 // Is this function a top-level function (scripts, evals).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004543 inline bool is_toplevel();
4544 inline void set_is_toplevel(bool value);
4545
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004546 // Bit field containing various information collected by the compiler to
4547 // drive optimization.
4548 inline int compiler_hints();
4549 inline void set_compiler_hints(int value);
4550
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004551 // A counter used to determine when to stress the deoptimizer with a
4552 // deopt.
4553 inline Smi* deopt_counter();
4554 inline void set_deopt_counter(Smi* counter);
4555
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004556 // Add information on assignments of the form this.x = ...;
4557 void SetThisPropertyAssignmentsInfo(
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004558 bool has_only_simple_this_property_assignments,
4559 FixedArray* this_property_assignments);
4560
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004561 // Clear information on assignments of the form this.x = ...;
4562 void ClearThisPropertyAssignmentsInfo();
4563
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004564 // Indicate that this function only consists of assignments of the form
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004565 // this.x = y; where y is either a constant or refers to an argument.
4566 inline bool has_only_simple_this_property_assignments();
4567
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +00004568 // Indicates if this function can be lazy compiled.
4569 // This is used to determine if we can safely flush code from a function
4570 // when doing GC if we expect that the function will no longer be used.
4571 inline bool allows_lazy_compilation();
4572 inline void set_allows_lazy_compilation(bool flag);
4573
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004574 // Indicates how many full GCs this function has survived with assigned
4575 // code object. Used to determine when it is relatively safe to flush
4576 // this code object and replace it with lazy compilation stub.
4577 // Age is reset when GC notices that the code object is referenced
4578 // from the stack or compilation cache.
4579 inline int code_age();
4580 inline void set_code_age(int age);
4581
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004582 // Indicates whether optimizations have been disabled for this
4583 // shared function info. If a function is repeatedly optimized or if
4584 // we cannot optimize the function we disable optimization to avoid
4585 // spending time attempting to optimize it again.
4586 inline bool optimization_disabled();
4587 inline void set_optimization_disabled(bool value);
4588
ricow@chromium.org83aa5492011-02-07 12:42:56 +00004589 // Indicates whether the function is a strict mode function.
4590 inline bool strict_mode();
4591 inline void set_strict_mode(bool value);
4592
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004593 // Indicates whether the function is a native function.
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004594 // These needs special threatment in .call and .apply since
4595 // null passed as the receiver should not be translated to the
4596 // global object.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004597 inline bool native();
4598 inline void set_native(bool value);
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004599
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004600 // Indicates whether or not the code in the shared function support
4601 // deoptimization.
4602 inline bool has_deoptimization_support();
4603
4604 // Enable deoptimization support through recompiled code.
4605 void EnableDeoptimizationSupport(Code* recompiled);
4606
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004607 // Disable (further) attempted optimization of all functions sharing this
4608 // shared function info. The function is the one we actually tried to
4609 // optimize.
4610 void DisableOptimization(JSFunction* function);
4611
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004612 // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4613 // code, returns whether it asserted (i.e., always true if assertions are
4614 // disabled).
4615 bool VerifyBailoutId(int id);
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004616
ager@chromium.org5c838252010-02-19 08:53:10 +00004617 // Check whether a inlined constructor can be generated with the given
4618 // prototype.
4619 bool CanGenerateInlineConstructor(Object* prototype);
4620
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +00004621 // Prevents further attempts to generate inline constructors.
4622 // To be called if generation failed for any reason.
4623 void ForbidInlineConstructor();
4624
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004625 // For functions which only contains this property assignments this provides
4626 // access to the names for the properties assigned.
4627 DECL_ACCESSORS(this_property_assignments, Object)
4628 inline int this_property_assignments_count();
4629 inline void set_this_property_assignments_count(int value);
4630 String* GetThisPropertyAssignmentName(int index);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00004631 bool IsThisPropertyAssignmentArgument(int index);
4632 int GetThisPropertyAssignmentArgument(int index);
4633 Object* GetThisPropertyAssignmentConstant(int index);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004634
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004635 // [source code]: Source code for the function.
4636 bool HasSourceCode();
4637 Object* GetSourceCode();
4638
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004639 inline int opt_count();
4640 inline void set_opt_count(int opt_count);
4641
4642 // Source size of this function.
4643 int SourceSize();
4644
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004645 // Calculate the instance size.
4646 int CalculateInstanceSize();
4647
4648 // Calculate the number of in-object properties.
4649 int CalculateInObjectProperties();
4650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004651 // Dispatched behavior.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004652 // Set max_length to -1 for unlimited length.
4653 void SourceCodePrint(StringStream* accumulator, int max_length);
whesse@chromium.org023421e2010-12-21 12:19:12 +00004654#ifdef OBJECT_PRINT
4655 inline void SharedFunctionInfoPrint() {
4656 SharedFunctionInfoPrint(stdout);
4657 }
4658 void SharedFunctionInfoPrint(FILE* out);
4659#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004660#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004661 void SharedFunctionInfoVerify();
4662#endif
4663
4664 // Casting.
4665 static inline SharedFunctionInfo* cast(Object* obj);
4666
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00004667 // Constants.
4668 static const int kDontAdaptArgumentsSentinel = -1;
4669
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004670 // Layout description.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004671 // Pointer fields.
ager@chromium.org236ad962008-09-25 09:45:57 +00004672 static const int kNameOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004673 static const int kCodeOffset = kNameOffset + kPointerSize;
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00004674 static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4675 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004676 static const int kInstanceClassNameOffset =
4677 kConstructStubOffset + kPointerSize;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004678 static const int kFunctionDataOffset =
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004679 kInstanceClassNameOffset + kPointerSize;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00004680 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004681 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4682 static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004683 static const int kInitialMapOffset =
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004684 kInferredNameOffset + kPointerSize;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004685 static const int kThisPropertyAssignmentsOffset =
4686 kInitialMapOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004687 static const int kDeoptCounterOffset =
4688 kThisPropertyAssignmentsOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004689#if V8_HOST_ARCH_32_BIT
4690 // Smi fields.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004691 static const int kLengthOffset =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004692 kDeoptCounterOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004693 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4694 static const int kExpectedNofPropertiesOffset =
4695 kFormalParameterCountOffset + kPointerSize;
4696 static const int kNumLiteralsOffset =
4697 kExpectedNofPropertiesOffset + kPointerSize;
4698 static const int kStartPositionAndTypeOffset =
4699 kNumLiteralsOffset + kPointerSize;
4700 static const int kEndPositionOffset =
4701 kStartPositionAndTypeOffset + kPointerSize;
4702 static const int kFunctionTokenPositionOffset =
4703 kEndPositionOffset + kPointerSize;
4704 static const int kCompilerHintsOffset =
4705 kFunctionTokenPositionOffset + kPointerSize;
4706 static const int kThisPropertyAssignmentsCountOffset =
4707 kCompilerHintsOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004708 static const int kOptCountOffset =
4709 kThisPropertyAssignmentsCountOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004710 // Total size.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004711 static const int kSize = kOptCountOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004712#else
4713 // The only reason to use smi fields instead of int fields
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004714 // is to allow iteration without maps decoding during
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004715 // garbage collections.
4716 // To avoid wasting space on 64-bit architectures we use
4717 // the following trick: we group integer fields into pairs
4718 // First integer in each pair is shifted left by 1.
4719 // By doing this we guarantee that LSB of each kPointerSize aligned
4720 // word is not set and thus this word cannot be treated as pointer
4721 // to HeapObject during old space traversal.
4722 static const int kLengthOffset =
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004723 kDeoptCounterOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004724 static const int kFormalParameterCountOffset =
4725 kLengthOffset + kIntSize;
4726
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004727 static const int kExpectedNofPropertiesOffset =
4728 kFormalParameterCountOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004729 static const int kNumLiteralsOffset =
4730 kExpectedNofPropertiesOffset + kIntSize;
4731
4732 static const int kEndPositionOffset =
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004733 kNumLiteralsOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004734 static const int kStartPositionAndTypeOffset =
4735 kEndPositionOffset + kIntSize;
4736
4737 static const int kFunctionTokenPositionOffset =
4738 kStartPositionAndTypeOffset + kIntSize;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004739 static const int kCompilerHintsOffset =
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004740 kFunctionTokenPositionOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004741
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004742 static const int kThisPropertyAssignmentsCountOffset =
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004743 kCompilerHintsOffset + kIntSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004744 static const int kOptCountOffset =
4745 kThisPropertyAssignmentsCountOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004746
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004747 // Total size.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004748 static const int kSize = kOptCountOffset + kIntSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00004749
4750#endif
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004751
4752 // The construction counter for inobject slack tracking is stored in the
4753 // most significant byte of compiler_hints which is otherwise unused.
4754 // Its offset depends on the endian-ness of the architecture.
4755#if __BYTE_ORDER == __LITTLE_ENDIAN
4756 static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4757#elif __BYTE_ORDER == __BIG_ENDIAN
4758 static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4759#else
4760#error Unknown byte ordering
4761#endif
4762
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00004763 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004764
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00004765 typedef FixedBodyDescriptor<kNameOffset,
4766 kThisPropertyAssignmentsOffset + kPointerSize,
4767 kSize> BodyDescriptor;
4768
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004769 // Bit positions in start_position_and_type.
4770 // The source code start position is in the 30 most significant bits of
4771 // the start_position_and_type field.
4772 static const int kIsExpressionBit = 0;
4773 static const int kIsTopLevelBit = 1;
4774 static const int kStartPositionShift = 2;
4775 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
mads.s.ager31e71382008-08-13 09:32:07 +00004776
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004777 // Bit positions in compiler_hints.
ager@chromium.orgb9a15452009-11-11 09:55:05 +00004778 static const int kHasOnlySimpleThisPropertyAssignments = 0;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004779 static const int kAllowLazyCompilation = 1;
4780 static const int kLiveObjectsMayExist = 2;
4781 static const int kCodeAgeShift = 3;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004782 static const int kCodeAgeMask = 0x7;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004783 static const int kOptimizationDisabled = 6;
4784 static const int kStrictModeFunction = 7;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004785 static const int kNative = 8;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00004786
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004787 private:
4788#if V8_HOST_ARCH_32_BIT
4789 // On 32 bit platforms, compiler hints is a smi.
4790 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4791 static const int kCompilerHintsSize = kPointerSize;
4792#else
4793 // On 64 bit platforms, compiler hints is not a smi, see comment above.
4794 static const int kCompilerHintsSmiTagSize = 0;
4795 static const int kCompilerHintsSize = kIntSize;
4796#endif
4797
4798 public:
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004799 // Constants for optimizing codegen for strict mode function and
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004800 // native tests.
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004801 // Allows to use byte-widgh instructions.
4802 static const int kStrictModeBitWithinByte =
4803 (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4804
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004805 static const int kNativeBitWithinByte =
4806 (kNative + kCompilerHintsSmiTagSize) % kBitsPerByte;
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004807
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004808#if __BYTE_ORDER == __LITTLE_ENDIAN
4809 static const int kStrictModeByteOffset = kCompilerHintsOffset +
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004810 (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004811 static const int kNativeByteOffset = kCompilerHintsOffset +
4812 (kNative + kCompilerHintsSmiTagSize) / kBitsPerByte;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004813#elif __BYTE_ORDER == __BIG_ENDIAN
4814 static const int kStrictModeByteOffset = kCompilerHintsOffset +
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004815 (kCompilerHintsSize - 1) -
4816 ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004817 static const int kNativeByteOffset = kCompilerHintsOffset +
ricow@chromium.org6fe7a8e2011-05-13 07:57:29 +00004818 (kCompilerHintsSize - 1) -
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004819 ((kNative + kCompilerHintsSmiTagSize) / kBitsPerByte);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004820#else
4821#error Unknown byte ordering
4822#endif
4823
4824 private:
mads.s.ager31e71382008-08-13 09:32:07 +00004825 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004826};
4827
4828
4829// JSFunction describes JavaScript functions.
4830class JSFunction: public JSObject {
4831 public:
4832 // [prototype_or_initial_map]:
4833 DECL_ACCESSORS(prototype_or_initial_map, Object)
4834
4835 // [shared_function_info]: The information about the function that
4836 // can be shared by instances.
4837 DECL_ACCESSORS(shared, SharedFunctionInfo)
4838
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004839 inline SharedFunctionInfo* unchecked_shared();
4840
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004841 // [context]: The context for this function.
4842 inline Context* context();
4843 inline Object* unchecked_context();
4844 inline void set_context(Object* context);
4845
4846 // [code]: The generated code object for this function. Executed
4847 // when the function is invoked, e.g. foo() or new foo(). See
4848 // [[Call]] and [[Construct]] description in ECMA-262, section
4849 // 8.6.2, page 27.
4850 inline Code* code();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004851 inline void set_code(Code* code);
4852 inline void ReplaceCode(Code* code);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004853
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00004854 inline Code* unchecked_code();
4855
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00004856 // Tells whether this function is builtin.
4857 inline bool IsBuiltin();
4858
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004859 // Tells whether or not the function needs arguments adaption.
4860 inline bool NeedsArgumentsAdaption();
4861
4862 // Tells whether or not this function has been optimized.
4863 inline bool IsOptimized();
4864
ager@chromium.orga9aa5fa2011-04-13 08:46:07 +00004865 // Tells whether or not this function can be optimized.
4866 inline bool IsOptimizable();
4867
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004868 // Mark this function for lazy recompilation. The function will be
4869 // recompiled the next time it is executed.
4870 void MarkForLazyRecompilation();
4871
4872 // Tells whether or not the function is already marked for lazy
4873 // recompilation.
4874 inline bool IsMarkedForLazyRecompilation();
4875
4876 // Compute a hash code for the source code of this function.
4877 uint32_t SourceHash();
4878
4879 // Check whether or not this function is inlineable.
4880 bool IsInlineable();
4881
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004882 // [literals]: Fixed array holding the materialized literals.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004883 //
4884 // If the function contains object, regexp or array literals, the
4885 // literals array prefix contains the object, regexp, and array
4886 // function to be used when creating these literals. This is
4887 // necessary so that we do not dynamically lookup the object, regexp
4888 // or array functions. Performing a dynamic lookup, we might end up
4889 // using the functions from a new context that we should not have
4890 // access to.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004891 DECL_ACCESSORS(literals, FixedArray)
4892
4893 // The initial map for an object created by this constructor.
4894 inline Map* initial_map();
4895 inline void set_initial_map(Map* value);
4896 inline bool has_initial_map();
4897
4898 // Get and set the prototype property on a JSFunction. If the
4899 // function has an initial map the prototype is set on the initial
4900 // map. Otherwise, the prototype is put in the initial map field
4901 // until an initial map is needed.
4902 inline bool has_prototype();
4903 inline bool has_instance_prototype();
4904 inline Object* prototype();
4905 inline Object* instance_prototype();
4906 Object* SetInstancePrototype(Object* value);
lrn@chromium.org303ada72010-10-27 09:33:13 +00004907 MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004908
kmillikin@chromium.org4111b802010-05-03 10:34:42 +00004909 // After prototype is removed, it will not be created when accessed, and
4910 // [[Construct]] from this function will not be allowed.
4911 Object* RemovePrototype();
4912 inline bool should_have_prototype();
4913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004914 // Accessor for this function's initial map's [[class]]
4915 // property. This is primarily used by ECMA native functions. This
4916 // method sets the class_name field of this function's initial map
4917 // to a given value. It creates an initial map if this function does
4918 // not have one. Note that this method does not copy the initial map
4919 // if it has one already, but simply replaces it with the new value.
4920 // Instances created afterwards will have a map whose [[class]] is
4921 // set to 'value', but there is no guarantees on instances created
4922 // before.
4923 Object* SetInstanceClassName(String* name);
4924
4925 // Returns if this function has been compiled to native code yet.
4926 inline bool is_compiled();
4927
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004928 // [next_function_link]: Field for linking functions. This list is treated as
4929 // a weak list by the GC.
4930 DECL_ACCESSORS(next_function_link, Object)
4931
4932 // Prints the name of the function using PrintF.
whesse@chromium.org023421e2010-12-21 12:19:12 +00004933 inline void PrintName() {
4934 PrintName(stdout);
4935 }
4936 void PrintName(FILE* out);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004937
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004938 // Casting.
4939 static inline JSFunction* cast(Object* obj);
4940
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00004941 // Iterates the objects, including code objects indirectly referenced
4942 // through pointers to the first instruction in the code object.
4943 void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
4944
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004945 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00004946#ifdef OBJECT_PRINT
4947 inline void JSFunctionPrint() {
4948 JSFunctionPrint(stdout);
4949 }
4950 void JSFunctionPrint(FILE* out);
4951#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004952#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004953 void JSFunctionVerify();
4954#endif
4955
4956 // Returns the number of allocated literals.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00004957 inline int NumberOfLiterals();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004958
ager@chromium.org236ad962008-09-25 09:45:57 +00004959 // Retrieve the global context from a function's literal array.
4960 static Context* GlobalContextFromLiterals(FixedArray* literals);
4961
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004962 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
4963 // kSize) is weak and has special handling during garbage collection.
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00004964 static const int kCodeEntryOffset = JSObject::kHeaderSize;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00004965 static const int kPrototypeOrInitialMapOffset =
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00004966 kCodeEntryOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004967 static const int kSharedFunctionInfoOffset =
4968 kPrototypeOrInitialMapOffset + kPointerSize;
4969 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
4970 static const int kLiteralsOffset = kContextOffset + kPointerSize;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004971 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
4972 static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
4973 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004974
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004975 // Layout of the literals array.
ager@chromium.org236ad962008-09-25 09:45:57 +00004976 static const int kLiteralsPrefixSize = 1;
4977 static const int kLiteralGlobalContextIndex = 0;
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00004978
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004979 private:
4980 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
4981};
4982
4983
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004984// JSGlobalProxy's prototype must be a JSGlobalObject or null,
4985// and the prototype is hidden. JSGlobalProxy always delegates
4986// property accesses to its prototype if the prototype is not null.
4987//
4988// A JSGlobalProxy can be reinitialized which will preserve its identity.
4989//
4990// Accessing a JSGlobalProxy requires security check.
4991
4992class JSGlobalProxy : public JSObject {
4993 public:
ager@chromium.orgea91cc52011-05-23 06:06:11 +00004994 // [context]: the owner global context of this global proxy object.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004995 // It is null value if this object is not used by any context.
4996 DECL_ACCESSORS(context, Object)
4997
4998 // Casting.
4999 static inline JSGlobalProxy* cast(Object* obj);
5000
5001 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005002#ifdef OBJECT_PRINT
5003 inline void JSGlobalProxyPrint() {
5004 JSGlobalProxyPrint(stdout);
5005 }
5006 void JSGlobalProxyPrint(FILE* out);
5007#endif
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005008#ifdef DEBUG
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005009 void JSGlobalProxyVerify();
5010#endif
5011
5012 // Layout description.
5013 static const int kContextOffset = JSObject::kHeaderSize;
5014 static const int kSize = kContextOffset + kPointerSize;
5015
5016 private:
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005017 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
5018};
5019
5020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005021// Forward declaration.
5022class JSBuiltinsObject;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005023class JSGlobalPropertyCell;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005024
5025// Common super class for JavaScript global objects and the special
5026// builtins global objects.
5027class GlobalObject: public JSObject {
5028 public:
5029 // [builtins]: the object holding the runtime routines written in JS.
5030 DECL_ACCESSORS(builtins, JSBuiltinsObject)
5031
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005032 // [global context]: the global context corresponding to this global object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005033 DECL_ACCESSORS(global_context, Context)
5034
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005035 // [global receiver]: the global receiver object of the context
5036 DECL_ACCESSORS(global_receiver, JSObject)
5037
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005038 // Retrieve the property cell used to store a property.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005039 JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005040
lrn@chromium.org303ada72010-10-27 09:33:13 +00005041 // This is like GetProperty, but is used when you know the lookup won't fail
5042 // by throwing an exception. This is for the debug and builtins global
5043 // objects, where it is known which properties can be expected to be present
5044 // on the object.
5045 Object* GetPropertyNoExceptionThrown(String* key) {
5046 Object* answer = GetProperty(key)->ToObjectUnchecked();
5047 return answer;
5048 }
5049
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00005050 // Ensure that the global object has a cell for the given property name.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005051 MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00005052
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005053 // Casting.
5054 static inline GlobalObject* cast(Object* obj);
5055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005056 // Layout description.
5057 static const int kBuiltinsOffset = JSObject::kHeaderSize;
5058 static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005059 static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
5060 static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005061
5062 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005063 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
mads.s.ager31e71382008-08-13 09:32:07 +00005064
5065 DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005066};
5067
5068
5069// JavaScript global object.
5070class JSGlobalObject: public GlobalObject {
5071 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005072 // Casting.
5073 static inline JSGlobalObject* cast(Object* obj);
5074
5075 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005076#ifdef OBJECT_PRINT
5077 inline void JSGlobalObjectPrint() {
5078 JSGlobalObjectPrint(stdout);
5079 }
5080 void JSGlobalObjectPrint(FILE* out);
5081#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005082#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005083 void JSGlobalObjectVerify();
5084#endif
5085
5086 // Layout description.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005087 static const int kSize = GlobalObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005088
5089 private:
5090 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
5091};
5092
5093
5094// Builtins global object which holds the runtime routines written in
5095// JavaScript.
5096class JSBuiltinsObject: public GlobalObject {
5097 public:
5098 // Accessors for the runtime routines written in JavaScript.
5099 inline Object* javascript_builtin(Builtins::JavaScript id);
5100 inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
5101
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005102 // Accessors for code of the runtime routines written in JavaScript.
5103 inline Code* javascript_builtin_code(Builtins::JavaScript id);
5104 inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
5105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005106 // Casting.
5107 static inline JSBuiltinsObject* cast(Object* obj);
5108
5109 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005110#ifdef OBJECT_PRINT
5111 inline void JSBuiltinsObjectPrint() {
5112 JSBuiltinsObjectPrint(stdout);
5113 }
5114 void JSBuiltinsObjectPrint(FILE* out);
5115#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005116#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005117 void JSBuiltinsObjectVerify();
5118#endif
5119
5120 // Layout description. The size of the builtins object includes
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005121 // room for two pointers per runtime routine written in javascript
5122 // (function and code object).
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005123 static const int kJSBuiltinsCount = Builtins::id_count;
5124 static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005125 static const int kJSBuiltinsCodeOffset =
5126 GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005127 static const int kSize =
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005128 kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
5129
5130 static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
5131 return kJSBuiltinsOffset + id * kPointerSize;
5132 }
5133
5134 static int OffsetOfCodeWithId(Builtins::JavaScript id) {
5135 return kJSBuiltinsCodeOffset + id * kPointerSize;
5136 }
5137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005138 private:
5139 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
5140};
5141
5142
5143// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
5144class JSValue: public JSObject {
5145 public:
5146 // [value]: the object being wrapped.
5147 DECL_ACCESSORS(value, Object)
5148
5149 // Casting.
5150 static inline JSValue* cast(Object* obj);
5151
5152 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00005153#ifdef OBJECT_PRINT
5154 inline void JSValuePrint() {
5155 JSValuePrint(stdout);
5156 }
5157 void JSValuePrint(FILE* out);
5158#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005159#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005160 void JSValueVerify();
5161#endif
5162
5163 // Layout description.
5164 static const int kValueOffset = JSObject::kHeaderSize;
5165 static const int kSize = kValueOffset + kPointerSize;
5166
5167 private:
5168 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
5169};
5170
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00005171
5172// Representation of message objects used for error reporting through
5173// the API. The messages are formatted in JavaScript so this object is
5174// a real JavaScript object. The information used for formatting the
5175// error messages are not directly accessible from JavaScript to
5176// prevent leaking information to user code called during error
5177// formatting.
5178class JSMessageObject: public JSObject {
5179 public:
5180 // [type]: the type of error message.
5181 DECL_ACCESSORS(type, String)
5182
5183 // [arguments]: the arguments for formatting the error message.
5184 DECL_ACCESSORS(arguments, JSArray)
5185
5186 // [script]: the script from which the error message originated.
5187 DECL_ACCESSORS(script, Object)
5188
5189 // [stack_trace]: the stack trace for this error message.
5190 DECL_ACCESSORS(stack_trace, Object)
5191
5192 // [stack_frames]: an array of stack frames for this error object.
5193 DECL_ACCESSORS(stack_frames, Object)
5194
5195 // [start_position]: the start position in the script for the error message.
5196 inline int start_position();
5197 inline void set_start_position(int value);
5198
5199 // [end_position]: the end position in the script for the error message.
5200 inline int end_position();
5201 inline void set_end_position(int value);
5202
5203 // Casting.
5204 static inline JSMessageObject* cast(Object* obj);
5205
5206 // Dispatched behavior.
5207#ifdef OBJECT_PRINT
5208 inline void JSMessageObjectPrint() {
5209 JSMessageObjectPrint(stdout);
5210 }
5211 void JSMessageObjectPrint(FILE* out);
5212#endif
5213#ifdef DEBUG
5214 void JSMessageObjectVerify();
5215#endif
5216
5217 // Layout description.
5218 static const int kTypeOffset = JSObject::kHeaderSize;
5219 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
5220 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
5221 static const int kStackTraceOffset = kScriptOffset + kPointerSize;
5222 static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
5223 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
5224 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
5225 static const int kSize = kEndPositionOffset + kPointerSize;
5226
5227 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
5228 kStackFramesOffset + kPointerSize,
5229 kSize> BodyDescriptor;
5230};
5231
5232
ager@chromium.org236ad962008-09-25 09:45:57 +00005233// Regular expressions
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005234// The regular expression holds a single reference to a FixedArray in
5235// the kDataOffset field.
5236// The FixedArray contains the following data:
5237// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
5238// - reference to the original source string
5239// - reference to the original flag string
5240// If it is an atom regexp
5241// - a reference to a literal string to search for
5242// If it is an irregexp regexp:
5243// - a reference to code for ASCII inputs (bytecode or compiled).
5244// - a reference to code for UC16 inputs (bytecode or compiled).
5245// - max number of registers used by irregexp implementations.
5246// - number of capture registers (output values) of the regexp.
ager@chromium.org236ad962008-09-25 09:45:57 +00005247class JSRegExp: public JSObject {
5248 public:
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005249 // Meaning of Type:
5250 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005251 // ATOM: A simple string to match against using an indexOf operation.
5252 // IRREGEXP: Compiled with Irregexp.
5253 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
ager@chromium.org381abbb2009-02-25 13:23:22 +00005254 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005255 enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
ager@chromium.org236ad962008-09-25 09:45:57 +00005256
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005257 class Flags {
5258 public:
5259 explicit Flags(uint32_t value) : value_(value) { }
5260 bool is_global() { return (value_ & GLOBAL) != 0; }
5261 bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
5262 bool is_multiline() { return (value_ & MULTILINE) != 0; }
5263 uint32_t value() { return value_; }
5264 private:
5265 uint32_t value_;
5266 };
ager@chromium.org236ad962008-09-25 09:45:57 +00005267
ager@chromium.org236ad962008-09-25 09:45:57 +00005268 DECL_ACCESSORS(data, Object)
5269
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005270 inline Type TypeTag();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005271 inline int CaptureCount();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00005272 inline Flags GetFlags();
5273 inline String* Pattern();
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005274 inline Object* DataAt(int index);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005275 // Set implementation data after the object has been prepared.
5276 inline void SetDataAt(int index, Object* value);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00005277 static int code_index(bool is_ascii) {
5278 if (is_ascii) {
5279 return kIrregexpASCIICodeIndex;
5280 } else {
5281 return kIrregexpUC16CodeIndex;
5282 }
5283 }
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005284
ager@chromium.org236ad962008-09-25 09:45:57 +00005285 static inline JSRegExp* cast(Object* obj);
5286
5287 // Dispatched behavior.
5288#ifdef DEBUG
ager@chromium.org236ad962008-09-25 09:45:57 +00005289 void JSRegExpVerify();
5290#endif
5291
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005292 static const int kDataOffset = JSObject::kHeaderSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005293 static const int kSize = kDataOffset + kPointerSize;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005294
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005295 // Indices in the data array.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005296 static const int kTagIndex = 0;
5297 static const int kSourceIndex = kTagIndex + 1;
5298 static const int kFlagsIndex = kSourceIndex + 1;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005299 static const int kDataIndex = kFlagsIndex + 1;
5300 // The data fields are used in different ways depending on the
5301 // value of the tag.
5302 // Atom regexps (literal strings).
5303 static const int kAtomPatternIndex = kDataIndex;
5304
5305 static const int kAtomDataSize = kAtomPatternIndex + 1;
5306
ager@chromium.orga1645e22009-09-09 19:27:10 +00005307 // Irregexp compiled code or bytecode for ASCII. If compilation
5308 // fails, this fields hold an exception object that should be
5309 // thrown if the regexp is used again.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005310 static const int kIrregexpASCIICodeIndex = kDataIndex;
ager@chromium.orga1645e22009-09-09 19:27:10 +00005311 // Irregexp compiled code or bytecode for UC16. If compilation
5312 // fails, this fields hold an exception object that should be
5313 // thrown if the regexp is used again.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005314 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
5315 // Maximal number of registers used by either ASCII or UC16.
5316 // Only used to check that there is enough stack space
5317 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
5318 // Number of captures in the compiled regexp.
5319 static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
5320
5321 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005322
5323 // Offsets directly into the data fixed array.
5324 static const int kDataTagOffset =
5325 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5326 static const int kDataAsciiCodeOffset =
5327 FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005328 static const int kDataUC16CodeOffset =
5329 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005330 static const int kIrregexpCaptureCountOffset =
5331 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
lrn@chromium.org25156de2010-04-06 13:10:27 +00005332
5333 // In-object fields.
5334 static const int kSourceFieldIndex = 0;
5335 static const int kGlobalFieldIndex = 1;
5336 static const int kIgnoreCaseFieldIndex = 2;
5337 static const int kMultilineFieldIndex = 3;
5338 static const int kLastIndexFieldIndex = 4;
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00005339 static const int kInObjectFieldCount = 5;
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005340};
5341
5342
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005343class CompilationCacheShape {
5344 public:
5345 static inline bool IsMatch(HashTableKey* key, Object* value) {
5346 return key->IsMatch(value);
5347 }
5348
5349 static inline uint32_t Hash(HashTableKey* key) {
5350 return key->Hash();
5351 }
5352
5353 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5354 return key->HashForObject(object);
5355 }
5356
lrn@chromium.org303ada72010-10-27 09:33:13 +00005357 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005358 return key->AsObject();
5359 }
5360
5361 static const int kPrefixSize = 0;
5362 static const int kEntrySize = 2;
5363};
5364
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00005365
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005366class CompilationCacheTable: public HashTable<CompilationCacheShape,
5367 HashTableKey*> {
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005368 public:
5369 // Find cached value for a string key, otherwise return null.
5370 Object* Lookup(String* src);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00005371 Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005372 Object* LookupRegExp(String* source, JSRegExp::Flags flags);
lrn@chromium.org303ada72010-10-27 09:33:13 +00005373 MaybeObject* Put(String* src, Object* value);
ricow@chromium.org83aa5492011-02-07 12:42:56 +00005374 MaybeObject* PutEval(String* src,
5375 Context* context,
5376 SharedFunctionInfo* value);
lrn@chromium.org303ada72010-10-27 09:33:13 +00005377 MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005378
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005379 // Remove given value from cache.
5380 void Remove(Object* value);
5381
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005382 static inline CompilationCacheTable* cast(Object* obj);
5383
5384 private:
5385 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
ager@chromium.org236ad962008-09-25 09:45:57 +00005386};
5387
5388
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005389class CodeCache: public Struct {
5390 public:
5391 DECL_ACCESSORS(default_cache, FixedArray)
5392 DECL_ACCESSORS(normal_type_cache, Object)
5393
5394 // Add the code object to the cache.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005395 MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005396
5397 // Lookup code object in the cache. Returns code object if found and undefined
5398 // if not.
5399 Object* Lookup(String* name, Code::Flags flags);
5400
5401 // Get the internal index of a code object in the cache. Returns -1 if the
5402 // code object is not in that cache. This index can be used to later call
5403 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5404 // RemoveByIndex.
sgjesse@chromium.org99a37fa2010-03-11 09:23:46 +00005405 int GetIndex(Object* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005406
5407 // Remove an object from the cache with the provided internal index.
sgjesse@chromium.org99a37fa2010-03-11 09:23:46 +00005408 void RemoveByIndex(Object* name, Code* code, int index);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005409
5410 static inline CodeCache* cast(Object* obj);
5411
whesse@chromium.org023421e2010-12-21 12:19:12 +00005412#ifdef OBJECT_PRINT
5413 inline void CodeCachePrint() {
5414 CodeCachePrint(stdout);
5415 }
5416 void CodeCachePrint(FILE* out);
5417#endif
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005418#ifdef DEBUG
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005419 void CodeCacheVerify();
5420#endif
5421
5422 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5423 static const int kNormalTypeCacheOffset =
5424 kDefaultCacheOffset + kPointerSize;
5425 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5426
5427 private:
lrn@chromium.org303ada72010-10-27 09:33:13 +00005428 MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5429 MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005430 Object* LookupDefaultCache(String* name, Code::Flags flags);
5431 Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5432
5433 // Code cache layout of the default cache. Elements are alternating name and
5434 // code objects for non normal load/store/call IC's.
5435 static const int kCodeCacheEntrySize = 2;
5436 static const int kCodeCacheEntryNameOffset = 0;
5437 static const int kCodeCacheEntryCodeOffset = 1;
5438
5439 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5440};
5441
5442
5443class CodeCacheHashTableShape {
5444 public:
5445 static inline bool IsMatch(HashTableKey* key, Object* value) {
5446 return key->IsMatch(value);
5447 }
5448
5449 static inline uint32_t Hash(HashTableKey* key) {
5450 return key->Hash();
5451 }
5452
5453 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5454 return key->HashForObject(object);
5455 }
5456
lrn@chromium.org303ada72010-10-27 09:33:13 +00005457 MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005458 return key->AsObject();
5459 }
5460
5461 static const int kPrefixSize = 0;
5462 static const int kEntrySize = 2;
5463};
5464
5465
5466class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5467 HashTableKey*> {
5468 public:
5469 Object* Lookup(String* name, Code::Flags flags);
lrn@chromium.org303ada72010-10-27 09:33:13 +00005470 MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005471
5472 int GetIndex(String* name, Code::Flags flags);
5473 void RemoveByIndex(int index);
5474
5475 static inline CodeCacheHashTable* cast(Object* obj);
5476
5477 // Initial size of the fixed array backing the hash table.
5478 static const int kInitialSize = 64;
5479
5480 private:
5481 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5482};
5483
5484
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00005485class PolymorphicCodeCache: public Struct {
5486 public:
5487 DECL_ACCESSORS(cache, Object)
5488
5489 MUST_USE_RESULT MaybeObject* Update(MapList* maps,
5490 Code::Flags flags,
5491 Code* code);
5492 Object* Lookup(MapList* maps, Code::Flags flags);
5493
5494 static inline PolymorphicCodeCache* cast(Object* obj);
5495
5496#ifdef OBJECT_PRINT
5497 inline void PolymorphicCodeCachePrint() {
5498 PolymorphicCodeCachePrint(stdout);
5499 }
5500 void PolymorphicCodeCachePrint(FILE* out);
5501#endif
5502#ifdef DEBUG
5503 void PolymorphicCodeCacheVerify();
5504#endif
5505
5506 static const int kCacheOffset = HeapObject::kHeaderSize;
5507 static const int kSize = kCacheOffset + kPointerSize;
5508
5509 private:
5510 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCache);
5511};
5512
5513
5514class PolymorphicCodeCacheHashTable
5515 : public HashTable<CodeCacheHashTableShape, HashTableKey*> {
5516 public:
5517 Object* Lookup(MapList* maps, int code_kind);
5518 MUST_USE_RESULT MaybeObject* Put(MapList* maps, int code_kind, Code* code);
5519
5520 static inline PolymorphicCodeCacheHashTable* cast(Object* obj);
5521
5522 static const int kInitialSize = 64;
5523 private:
5524 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCacheHashTable);
5525};
5526
5527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005528enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5529enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5530
5531
ager@chromium.org7c537e22008-10-16 08:43:32 +00005532class StringHasher {
5533 public:
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00005534 explicit inline StringHasher(int length);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005535
5536 // Returns true if the hash of this string can be computed without
5537 // looking at the contents.
5538 inline bool has_trivial_hash();
5539
5540 // Add a character to the hash and update the array index calculation.
5541 inline void AddCharacter(uc32 c);
5542
5543 // Adds a character to the hash but does not update the array index
5544 // calculation. This can only be called when it has been verified
5545 // that the input is not an array index.
5546 inline void AddCharacterNoIndex(uc32 c);
5547
5548 // Returns the value to store in the hash field of a string with
5549 // the given length and contents.
5550 uint32_t GetHashField();
5551
5552 // Returns true if the characters seen so far make up a legal array
5553 // index.
5554 bool is_array_index() { return is_array_index_; }
5555
5556 bool is_valid() { return is_valid_; }
5557
5558 void invalidate() { is_valid_ = false; }
5559
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00005560 // Calculated hash value for a string consisting of 1 to
5561 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5562 // value is represented decimal value.
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005563 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00005564
ager@chromium.org7c537e22008-10-16 08:43:32 +00005565 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00005566 uint32_t array_index() {
5567 ASSERT(is_array_index());
5568 return array_index_;
5569 }
5570
5571 inline uint32_t GetHash();
5572
5573 int length_;
5574 uint32_t raw_running_hash_;
5575 uint32_t array_index_;
5576 bool is_array_index_;
5577 bool is_first_char_;
5578 bool is_valid_;
ager@chromium.org6141cbe2009-11-20 12:14:52 +00005579 friend class TwoCharHashTableKey;
ager@chromium.org7c537e22008-10-16 08:43:32 +00005580};
5581
5582
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00005583// Calculates string hash.
5584template <typename schar>
5585inline uint32_t HashSequentialString(const schar* chars, int length);
5586
5587
ager@chromium.org870a0b62008-11-04 11:43:05 +00005588// The characteristics of a string are stored in its map. Retrieving these
5589// few bits of information is moderately expensive, involving two memory
5590// loads where the second is dependent on the first. To improve efficiency
5591// the shape of the string is given its own class so that it can be retrieved
5592// once and used for several string operations. A StringShape is small enough
5593// to be passed by value and is immutable, but be aware that flattening a
ager@chromium.orgc3e50d82008-11-05 11:53:10 +00005594// string can potentially alter its shape. Also be aware that a GC caused by
5595// something else can alter the shape of a string due to ConsString
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005596// shortcutting. Keeping these restrictions in mind has proven to be error-
5597// prone and so we no longer put StringShapes in variables unless there is a
5598// concrete performance benefit at that particular point in the code.
ager@chromium.org870a0b62008-11-04 11:43:05 +00005599class StringShape BASE_EMBEDDED {
5600 public:
5601 inline explicit StringShape(String* s);
5602 inline explicit StringShape(Map* s);
5603 inline explicit StringShape(InstanceType t);
ager@chromium.org870a0b62008-11-04 11:43:05 +00005604 inline bool IsSequential();
5605 inline bool IsExternal();
5606 inline bool IsCons();
ager@chromium.org870a0b62008-11-04 11:43:05 +00005607 inline bool IsExternalAscii();
5608 inline bool IsExternalTwoByte();
5609 inline bool IsSequentialAscii();
5610 inline bool IsSequentialTwoByte();
5611 inline bool IsSymbol();
5612 inline StringRepresentationTag representation_tag();
5613 inline uint32_t full_representation_tag();
5614 inline uint32_t size_tag();
5615#ifdef DEBUG
5616 inline uint32_t type() { return type_; }
5617 inline void invalidate() { valid_ = false; }
5618 inline bool valid() { return valid_; }
5619#else
5620 inline void invalidate() { }
5621#endif
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00005622
ager@chromium.org870a0b62008-11-04 11:43:05 +00005623 private:
5624 uint32_t type_;
5625#ifdef DEBUG
5626 inline void set_valid() { valid_ = true; }
5627 bool valid_;
5628#else
5629 inline void set_valid() { }
5630#endif
5631};
5632
5633
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005634// The String abstract class captures JavaScript string values:
5635//
5636// Ecma-262:
5637// 4.3.16 String Value
5638// A string value is a member of the type String and is a finite
5639// ordered sequence of zero or more 16-bit unsigned integer values.
5640//
5641// All string values have a length field.
5642class String: public HeapObject {
5643 public:
5644 // Get and set the length of the string.
5645 inline int length();
5646 inline void set_length(int value);
5647
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005648 // Get and set the hash field of the string.
5649 inline uint32_t hash_field();
5650 inline void set_hash_field(uint32_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005651
ager@chromium.org5ec48922009-05-05 07:25:34 +00005652 inline bool IsAsciiRepresentation();
5653 inline bool IsTwoByteRepresentation();
5654
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005655 // Returns whether this string has ascii chars, i.e. all of them can
5656 // be ascii encoded. This might be the case even if the string is
5657 // two-byte. Such strings may appear when the embedder prefers
5658 // two-byte external representations even for ascii data.
ricow@chromium.orgaa1b6162010-03-29 07:44:58 +00005659 //
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00005660 // NOTE: this should be considered only a hint. False negatives are
5661 // possible.
5662 inline bool HasOnlyAsciiChars();
ricow@chromium.orgaa1b6162010-03-29 07:44:58 +00005663
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005664 // Get and set individual two byte chars in the string.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005665 inline void Set(int index, uint16_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005666 // Get individual two byte char in the string. Repeated calls
5667 // to this method are not efficient unless the string is flat.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005668 inline uint16_t Get(int index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005669
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005670 // Try to flatten the string. Checks first inline to see if it is
5671 // necessary. Does nothing if the string is not a cons string.
5672 // Flattening allocates a sequential string with the same data as
5673 // the given string and mutates the cons string to a degenerate
5674 // form, where the first component is the new sequential string and
5675 // the second component is the empty string. If allocation fails,
5676 // this function returns a failure. If flattening succeeds, this
5677 // function returns the sequential string that is now the first
5678 // component of the cons string.
5679 //
5680 // Degenerate cons strings are handled specially by the garbage
5681 // collector (see IsShortcutCandidate).
5682 //
5683 // Use FlattenString from Handles.cc to flatten even in case an
5684 // allocation failure happens.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005685 inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005686
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005687 // Convenience function. Has exactly the same behavior as
5688 // TryFlatten(), except in the case of failure returns the original
5689 // string.
5690 inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5691
ager@chromium.org7c537e22008-10-16 08:43:32 +00005692 Vector<const char> ToAsciiVector();
5693 Vector<const uc16> ToUC16Vector();
5694
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005695 // Mark the string as an undetectable object. It only applies to
5696 // ascii and two byte string types.
5697 bool MarkAsUndetectable();
5698
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005699 // Return a substring.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005700 MUST_USE_RESULT MaybeObject* SubString(int from,
5701 int to,
5702 PretenureFlag pretenure = NOT_TENURED);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005703
5704 // String equality operations.
5705 inline bool Equals(String* other);
5706 bool IsEqualTo(Vector<const char> str);
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00005707 bool IsAsciiEqualTo(Vector<const char> str);
5708 bool IsTwoByteEqualTo(Vector<const uc16> str);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005709
5710 // Return a UTF8 representation of the string. The string is null
5711 // terminated but may optionally contain nulls. Length is returned
5712 // in length_output if length_output is not a null pointer The string
5713 // should be nearly flat, otherwise the performance of this method may
5714 // be very slow (quadratic in the length). Setting robustness_flag to
5715 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5716 // handles unexpected data without causing assert failures and it does not
5717 // do any heap allocations. This is useful when printing stack traces.
5718 SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5719 RobustnessFlag robustness_flag,
5720 int offset,
5721 int length,
5722 int* length_output = 0);
5723 SmartPointer<char> ToCString(
5724 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5725 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5726 int* length_output = 0);
5727
ager@chromium.org9258b6b2008-09-11 09:11:10 +00005728 int Utf8Length();
5729
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005730 // Return a 16 bit Unicode representation of the string.
5731 // The string should be nearly flat, otherwise the performance of
5732 // of this method may be very bad. Setting robustness_flag to
5733 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
5734 // handles unexpected data without causing assert failures and it does not
5735 // do any heap allocations. This is useful when printing stack traces.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00005736 SmartPointer<uc16> ToWideCString(
5737 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005738
5739 // Tells whether the hash code has been computed.
5740 inline bool HasHashCode();
5741
5742 // Returns a hash value used for the property table
5743 inline uint32_t Hash();
5744
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005745 static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5746 int length);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005747
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005748 static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5749 uint32_t* index,
5750 int length);
5751
ager@chromium.org6f10e412009-02-13 10:11:16 +00005752 // Externalization.
5753 bool MakeExternal(v8::String::ExternalStringResource* resource);
5754 bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5755
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005756 // Conversion.
5757 inline bool AsArrayIndex(uint32_t* index);
5758
5759 // Casting.
5760 static inline String* cast(Object* obj);
5761
5762 void PrintOn(FILE* out);
5763
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005764 // For use during stack traces. Performs rudimentary sanity check.
5765 bool LooksValid();
5766
5767 // Dispatched behavior.
5768 void StringShortPrint(StringStream* accumulator);
whesse@chromium.org023421e2010-12-21 12:19:12 +00005769#ifdef OBJECT_PRINT
5770 inline void StringPrint() {
5771 StringPrint(stdout);
5772 }
5773 void StringPrint(FILE* out);
5774#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005775#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005776 void StringVerify();
5777#endif
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005778 inline bool IsFlat();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005779
5780 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00005781 static const int kLengthOffset = HeapObject::kHeaderSize;
ager@chromium.orgac091b72010-05-05 07:34:42 +00005782 static const int kHashFieldOffset = kLengthOffset + kPointerSize;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005783 static const int kSize = kHashFieldOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005784
ager@chromium.org3811b432009-10-28 14:53:37 +00005785 // Maximum number of characters to consider when trying to convert a string
5786 // value into an array index.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005787 static const int kMaxArrayIndexSize = 10;
5788
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005789 // Max ascii char code.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005790 static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
ager@chromium.org381abbb2009-02-25 13:23:22 +00005791 static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
ager@chromium.org8bb60582008-12-11 12:02:20 +00005792 static const int kMaxUC16CharCode = 0xffff;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005793
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005794 // Minimum length for a cons string.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005795 static const int kMinNonFlatLength = 13;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005797 // Mask constant for checking if a string has a computed hash code
5798 // and if it is an array index. The least significant bit indicates
5799 // whether a hash code has been computed. If the hash code has been
5800 // computed the 2nd bit tells whether the string can be used as an
5801 // array index.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005802 static const int kHashNotComputedMask = 1;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005803 static const int kIsNotArrayIndexMask = 1 << 1;
5804 static const int kNofHashBitFields = 2;
ager@chromium.org7c537e22008-10-16 08:43:32 +00005805
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005806 // Shift constant retrieving hash code from hash field.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005807 static const int kHashShift = kNofHashBitFields;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005808
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005809 // Array index strings this short can keep their index in the hash
5810 // field.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00005811 static const int kMaxCachedArrayIndexLength = 7;
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005812
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005813 // For strings which are array indexes the hash value has the string length
5814 // mixed into the hash, mainly to avoid a hash value of zero which would be
5815 // the case for the string '0'. 24 bits are used for the array index value.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005816 static const int kArrayIndexValueBits = 24;
5817 static const int kArrayIndexLengthBits =
5818 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
5819
5820 STATIC_CHECK((kArrayIndexLengthBits > 0));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00005821 STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005822
5823 static const int kArrayIndexHashLengthShift =
5824 kArrayIndexValueBits + kNofHashBitFields;
5825
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005826 static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005827
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005828 static const int kArrayIndexValueMask =
5829 ((1 << kArrayIndexValueBits) - 1) << kHashShift;
ager@chromium.org3811b432009-10-28 14:53:37 +00005830
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005831 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
5832 // could use a mask to test if the length of string is less than or equal to
5833 // kMaxCachedArrayIndexLength.
5834 STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
5835
5836 static const int kContainsCachedArrayIndexMask =
5837 (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
5838 kIsNotArrayIndexMask;
5839
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005840 // Value of empty hash field indicating that the hash is not computed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005841 static const int kEmptyHashField =
5842 kIsNotArrayIndexMask | kHashNotComputedMask;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005843
5844 // Value of hash field containing computed hash equal to zero.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00005845 static const int kZeroHash = kIsNotArrayIndexMask;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005846
5847 // Maximal string length.
5848 static const int kMaxLength = (1 << (32 - 2)) - 1;
5849
5850 // Max length for computing hash. For strings longer than this limit the
5851 // string length is used as the hash value.
5852 static const int kMaxHashCalcLength = 16383;
ager@chromium.org7c537e22008-10-16 08:43:32 +00005853
kasper.lund7276f142008-07-30 08:49:36 +00005854 // Limit for truncation in short printing.
5855 static const int kMaxShortPrintLength = 1024;
5856
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005857 // Support for regular expressions.
5858 const uc16* GetTwoByteData();
5859 const uc16* GetTwoByteData(unsigned start);
5860
5861 // Support for StringInputBuffer
5862 static const unibrow::byte* ReadBlock(String* input,
5863 unibrow::byte* util_buffer,
5864 unsigned capacity,
5865 unsigned* remaining,
5866 unsigned* offset);
5867 static const unibrow::byte* ReadBlock(String** input,
5868 unibrow::byte* util_buffer,
5869 unsigned capacity,
5870 unsigned* remaining,
5871 unsigned* offset);
5872
5873 // Helper function for flattening strings.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005874 template <typename sinkchar>
5875 static void WriteToFlat(String* source,
5876 sinkchar* sink,
5877 int from,
5878 int to);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005879
fschneider@chromium.org9e3e0b62011-01-03 10:16:46 +00005880 static inline bool IsAscii(const char* chars, int length) {
5881 const char* limit = chars + length;
5882#ifdef V8_HOST_CAN_READ_UNALIGNED
5883 ASSERT(kMaxAsciiCharCode == 0x7F);
5884 const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
5885 while (chars <= limit - sizeof(uintptr_t)) {
5886 if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
5887 return false;
5888 }
5889 chars += sizeof(uintptr_t);
5890 }
5891#endif
5892 while (chars < limit) {
5893 if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
5894 ++chars;
5895 }
5896 return true;
5897 }
5898
5899 static inline bool IsAscii(const uc16* chars, int length) {
5900 const uc16* limit = chars + length;
5901 while (chars < limit) {
5902 if (*chars > kMaxAsciiCharCodeU) return false;
5903 ++chars;
5904 }
5905 return true;
5906 }
5907
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005908 protected:
5909 class ReadBlockBuffer {
5910 public:
5911 ReadBlockBuffer(unibrow::byte* util_buffer_,
5912 unsigned cursor_,
5913 unsigned capacity_,
5914 unsigned remaining_) :
5915 util_buffer(util_buffer_),
5916 cursor(cursor_),
5917 capacity(capacity_),
5918 remaining(remaining_) {
5919 }
5920 unibrow::byte* util_buffer;
5921 unsigned cursor;
5922 unsigned capacity;
5923 unsigned remaining;
5924 };
5925
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005926 static inline const unibrow::byte* ReadBlock(String* input,
5927 ReadBlockBuffer* buffer,
5928 unsigned* offset,
5929 unsigned max_chars);
5930 static void ReadBlockIntoBuffer(String* input,
5931 ReadBlockBuffer* buffer,
5932 unsigned* offset_ptr,
5933 unsigned max_chars);
5934
5935 private:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005936 // Try to flatten the top level ConsString that is hiding behind this
5937 // string. This is a no-op unless the string is a ConsString. Flatten
5938 // mutates the ConsString and might return a failure.
lrn@chromium.org303ada72010-10-27 09:33:13 +00005939 MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00005940
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005941 static inline bool IsHashFieldComputed(uint32_t field);
5942
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005943 // Slow case of String::Equals. This implementation works on any strings
5944 // but it is most efficient on strings that are almost flat.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005945 bool SlowEquals(String* other);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005946
5947 // Slow case of AsArrayIndex.
5948 bool SlowAsArrayIndex(uint32_t* index);
5949
5950 // Compute and set the hash code.
5951 uint32_t ComputeAndSetHash();
5952
5953 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
5954};
5955
5956
5957// The SeqString abstract class captures sequential string values.
5958class SeqString: public String {
5959 public:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005960 // Casting.
5961 static inline SeqString* cast(Object* obj);
5962
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005963 private:
5964 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
5965};
5966
5967
5968// The AsciiString class captures sequential ascii string objects.
5969// Each character in the AsciiString is an ascii character.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005970class SeqAsciiString: public SeqString {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005971 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00005972 static const bool kHasAsciiEncoding = true;
5973
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005974 // Dispatched behavior.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005975 inline uint16_t SeqAsciiStringGet(int index);
5976 inline void SeqAsciiStringSet(int index, uint16_t value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005977
5978 // Get the address of the characters in this string.
5979 inline Address GetCharsAddress();
5980
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00005981 inline char* GetChars();
5982
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005983 // Casting
ager@chromium.org7c537e22008-10-16 08:43:32 +00005984 static inline SeqAsciiString* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005985
5986 // Garbage collection support. This method is called by the
5987 // garbage collector to compute the actual size of an AsciiString
5988 // instance.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00005989 inline int SeqAsciiStringSize(InstanceType instance_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005990
5991 // Computes the size for an AsciiString instance of a given length.
5992 static int SizeFor(int length) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00005993 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005994 }
5995
5996 // Layout description.
5997 static const int kHeaderSize = String::kSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00005998 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005999
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006000 // Maximal memory usage for a single sequential ASCII string.
6001 static const int kMaxSize = 512 * MB;
6002 // Maximal length of a single sequential ASCII string.
6003 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6004 static const int kMaxLength = (kMaxSize - kHeaderSize);
6005
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006006 // Support for StringInputBuffer.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006007 inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6008 unsigned* offset,
6009 unsigned chars);
6010 inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
6011 unsigned* offset,
6012 unsigned chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006013
6014 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00006015 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006016};
6017
6018
6019// The TwoByteString class captures sequential unicode string objects.
6020// Each character in the TwoByteString is a two-byte uint16_t.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006021class SeqTwoByteString: public SeqString {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006022 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00006023 static const bool kHasAsciiEncoding = false;
6024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006025 // Dispatched behavior.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006026 inline uint16_t SeqTwoByteStringGet(int index);
6027 inline void SeqTwoByteStringSet(int index, uint16_t value);
6028
6029 // Get the address of the characters in this string.
6030 inline Address GetCharsAddress();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006031
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006032 inline uc16* GetChars();
6033
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006034 // For regexp code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006035 const uint16_t* SeqTwoByteStringGetData(unsigned start);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006036
6037 // Casting
ager@chromium.org7c537e22008-10-16 08:43:32 +00006038 static inline SeqTwoByteString* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006039
6040 // Garbage collection support. This method is called by the
6041 // garbage collector to compute the actual size of a TwoByteString
6042 // instance.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00006043 inline int SeqTwoByteStringSize(InstanceType instance_type);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006044
6045 // Computes the size for a TwoByteString instance of a given length.
6046 static int SizeFor(int length) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00006047 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006048 }
6049
6050 // Layout description.
6051 static const int kHeaderSize = String::kSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006052 static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006053
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006054 // Maximal memory usage for a single sequential two-byte string.
6055 static const int kMaxSize = 512 * MB;
6056 // Maximal length of a single sequential two-byte string.
6057 // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
6058 static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
6059
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006060 // Support for StringInputBuffer.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006061 inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6062 unsigned* offset_ptr,
6063 unsigned chars);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006064
6065 private:
ager@chromium.org7c537e22008-10-16 08:43:32 +00006066 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006067};
6068
6069
6070// The ConsString class describes string values built by using the
6071// addition operator on strings. A ConsString is a pair where the
6072// first and second components are pointers to other string values.
6073// One or both components of a ConsString can be pointers to other
6074// ConsStrings, creating a binary tree of ConsStrings where the leaves
6075// are non-ConsString string values. The string value represented by
6076// a ConsString can be obtained by concatenating the leaf string
6077// values in a left-to-right depth-first traversal of the tree.
6078class ConsString: public String {
6079 public:
ager@chromium.org870a0b62008-11-04 11:43:05 +00006080 // First string of the cons cell.
6081 inline String* first();
6082 // Doesn't check that the result is a string, even in debug mode. This is
6083 // useful during GC where the mark bits confuse the checks.
6084 inline Object* unchecked_first();
6085 inline void set_first(String* first,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00006086 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006087
ager@chromium.org870a0b62008-11-04 11:43:05 +00006088 // Second string of the cons cell.
6089 inline String* second();
6090 // Doesn't check that the result is a string, even in debug mode. This is
6091 // useful during GC where the mark bits confuse the checks.
6092 inline Object* unchecked_second();
6093 inline void set_second(String* second,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00006094 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006095
6096 // Dispatched behavior.
6097 uint16_t ConsStringGet(int index);
6098
6099 // Casting.
6100 static inline ConsString* cast(Object* obj);
6101
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006102 // Layout description.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006103 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006104 static const int kSecondOffset = kFirstOffset + kPointerSize;
6105 static const int kSize = kSecondOffset + kPointerSize;
6106
6107 // Support for StringInputBuffer.
6108 inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
6109 unsigned* offset_ptr,
6110 unsigned chars);
6111 inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6112 unsigned* offset_ptr,
6113 unsigned chars);
6114
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006115 // Minimum length for a cons string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006116 static const int kMinLength = 13;
6117
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006118 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
6119 BodyDescriptor;
6120
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006121 private:
6122 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
6123};
6124
6125
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006126// The ExternalString class describes string values that are backed by
6127// a string resource that lies outside the V8 heap. ExternalStrings
6128// consist of the length field common to all strings, a pointer to the
6129// external resource. It is important to ensure (externally) that the
6130// resource is not deallocated while the ExternalString is live in the
6131// V8 heap.
6132//
6133// The API expects that all ExternalStrings are created through the
6134// API. Therefore, ExternalStrings should not be used internally.
6135class ExternalString: public String {
6136 public:
6137 // Casting
6138 static inline ExternalString* cast(Object* obj);
6139
6140 // Layout description.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006141 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006142 static const int kSize = kResourceOffset + kPointerSize;
6143
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006144 STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
6145
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006146 private:
6147 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
6148};
6149
6150
6151// The ExternalAsciiString class is an external string backed by an
6152// ASCII string.
6153class ExternalAsciiString: public ExternalString {
6154 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00006155 static const bool kHasAsciiEncoding = true;
6156
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006157 typedef v8::String::ExternalAsciiStringResource Resource;
6158
6159 // The underlying resource.
6160 inline Resource* resource();
6161 inline void set_resource(Resource* buffer);
6162
6163 // Dispatched behavior.
6164 uint16_t ExternalAsciiStringGet(int index);
6165
6166 // Casting.
6167 static inline ExternalAsciiString* cast(Object* obj);
6168
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006169 // Garbage collection support.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006170 inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
6171
6172 template<typename StaticVisitor>
6173 inline void ExternalAsciiStringIterateBody();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006175 // Support for StringInputBuffer.
6176 const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
6177 unsigned* offset,
6178 unsigned chars);
6179 inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6180 unsigned* offset,
6181 unsigned chars);
6182
6183 private:
6184 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
6185};
6186
6187
6188// The ExternalTwoByteString class is an external string backed by a UTF-16
6189// encoded string.
6190class ExternalTwoByteString: public ExternalString {
6191 public:
erik.corry@gmail.com4a2e25e2010-07-07 12:22:46 +00006192 static const bool kHasAsciiEncoding = false;
6193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006194 typedef v8::String::ExternalStringResource Resource;
6195
6196 // The underlying string resource.
6197 inline Resource* resource();
6198 inline void set_resource(Resource* buffer);
6199
6200 // Dispatched behavior.
6201 uint16_t ExternalTwoByteStringGet(int index);
6202
6203 // For regexp code.
6204 const uint16_t* ExternalTwoByteStringGetData(unsigned start);
6205
6206 // Casting.
6207 static inline ExternalTwoByteString* cast(Object* obj);
6208
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006209 // Garbage collection support.
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006210 inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
6211
6212 template<typename StaticVisitor>
6213 inline void ExternalTwoByteStringIterateBody();
6214
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006215
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006216 // Support for StringInputBuffer.
6217 void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
6218 unsigned* offset_ptr,
6219 unsigned chars);
6220
6221 private:
6222 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
6223};
6224
6225
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006226// Utility superclass for stack-allocated objects that must be updated
6227// on gc. It provides two ways for the gc to update instances, either
6228// iterating or updating after gc.
6229class Relocatable BASE_EMBEDDED {
6230 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006231 explicit inline Relocatable(Isolate* isolate);
6232 inline virtual ~Relocatable();
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006233 virtual void IterateInstance(ObjectVisitor* v) { }
6234 virtual void PostGarbageCollection() { }
6235
6236 static void PostGarbageCollectionProcessing();
6237 static int ArchiveSpacePerThread();
lrn@chromium.org1c092762011-05-09 09:42:16 +00006238 static char* ArchiveState(Isolate* isolate, char* to);
6239 static char* RestoreState(Isolate* isolate, char* from);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006240 static void Iterate(ObjectVisitor* v);
6241 static void Iterate(ObjectVisitor* v, Relocatable* top);
6242 static char* Iterate(ObjectVisitor* v, char* t);
6243 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006244 Isolate* isolate_;
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006245 Relocatable* prev_;
6246};
6247
6248
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006249// A flat string reader provides random access to the contents of a
6250// string independent of the character width of the string. The handle
6251// must be valid as long as the reader is being used.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006252class FlatStringReader : public Relocatable {
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006253 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006254 FlatStringReader(Isolate* isolate, Handle<String> str);
6255 FlatStringReader(Isolate* isolate, Vector<const char> input);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006256 void PostGarbageCollection();
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006257 inline uc32 Get(int index);
6258 int length() { return length_; }
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006259 private:
6260 String** str_;
6261 bool is_ascii_;
6262 int length_;
6263 const void* start_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00006264};
6265
6266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006267// Note that StringInputBuffers are not valid across a GC! To fix this
6268// it would have to store a String Handle instead of a String* and
6269// AsciiStringReadBlock would have to be modified to use memcpy.
6270//
6271// StringInputBuffer is able to traverse any string regardless of how
6272// deeply nested a sequence of ConsStrings it is made of. However,
6273// performance will be better if deep strings are flattened before they
6274// are traversed. Since flattening requires memory allocation this is
6275// not always desirable, however (esp. in debugging situations).
6276class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
6277 public:
6278 virtual void Seek(unsigned pos);
6279 inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00006280 explicit inline StringInputBuffer(String* backing):
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006281 unibrow::InputBuffer<String, String*, 1024>(backing) {}
6282};
6283
6284
6285class SafeStringInputBuffer
6286 : public unibrow::InputBuffer<String, String**, 256> {
6287 public:
6288 virtual void Seek(unsigned pos);
6289 inline SafeStringInputBuffer()
6290 : unibrow::InputBuffer<String, String**, 256>() {}
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00006291 explicit inline SafeStringInputBuffer(String** backing)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006292 : unibrow::InputBuffer<String, String**, 256>(backing) {}
6293};
6294
6295
ager@chromium.org7c537e22008-10-16 08:43:32 +00006296template <typename T>
6297class VectorIterator {
6298 public:
6299 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
6300 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
6301 T GetNext() { return data_[index_++]; }
6302 bool has_more() { return index_ < data_.length(); }
6303 private:
6304 Vector<const T> data_;
6305 int index_;
6306};
6307
6308
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006309// The Oddball describes objects null, undefined, true, and false.
6310class Oddball: public HeapObject {
6311 public:
6312 // [to_string]: Cached to_string computed at startup.
6313 DECL_ACCESSORS(to_string, String)
6314
6315 // [to_number]: Cached to_number computed at startup.
6316 DECL_ACCESSORS(to_number, Object)
6317
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006318 inline byte kind();
6319 inline void set_kind(byte kind);
6320
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006321 // Casting.
6322 static inline Oddball* cast(Object* obj);
6323
6324 // Dispatched behavior.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006325#ifdef DEBUG
6326 void OddballVerify();
6327#endif
6328
6329 // Initialize the fields.
lrn@chromium.org303ada72010-10-27 09:33:13 +00006330 MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006331 Object* to_number,
6332 byte kind);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006333
6334 // Layout description.
ager@chromium.org236ad962008-09-25 09:45:57 +00006335 static const int kToStringOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006336 static const int kToNumberOffset = kToStringOffset + kPointerSize;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006337 static const int kKindOffset = kToNumberOffset + kPointerSize;
6338 static const int kSize = kKindOffset + kPointerSize;
6339
6340 static const byte kFalse = 0;
6341 static const byte kTrue = 1;
6342 static const byte kNotBooleanMask = ~1;
6343 static const byte kTheHole = 2;
6344 static const byte kNull = 3;
6345 static const byte kArgumentMarker = 4;
6346 static const byte kUndefined = 5;
6347 static const byte kOther = 6;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006348
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006349 typedef FixedBodyDescriptor<kToStringOffset,
6350 kToNumberOffset + kPointerSize,
6351 kSize> BodyDescriptor;
6352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006353 private:
6354 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6355};
6356
6357
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006358class JSGlobalPropertyCell: public HeapObject {
6359 public:
6360 // [value]: value of the global property.
6361 DECL_ACCESSORS(value, Object)
6362
6363 // Casting.
6364 static inline JSGlobalPropertyCell* cast(Object* obj);
6365
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006366#ifdef DEBUG
6367 void JSGlobalPropertyCellVerify();
whesse@chromium.org023421e2010-12-21 12:19:12 +00006368#endif
6369#ifdef OBJECT_PRINT
6370 inline void JSGlobalPropertyCellPrint() {
6371 JSGlobalPropertyCellPrint(stdout);
6372 }
6373 void JSGlobalPropertyCellPrint(FILE* out);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006374#endif
6375
6376 // Layout description.
6377 static const int kValueOffset = HeapObject::kHeaderSize;
6378 static const int kSize = kValueOffset + kPointerSize;
6379
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006380 typedef FixedBodyDescriptor<kValueOffset,
6381 kValueOffset + kPointerSize,
6382 kSize> BodyDescriptor;
6383
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00006384 // Returns the isolate/heap this cell object belongs to.
6385 inline Isolate* isolate();
6386 inline Heap* heap();
6387
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006388 private:
6389 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6390};
6391
6392
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006393// The JSProxy describes EcmaScript Harmony proxies
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006394class JSProxy: public JSReceiver {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006395 public:
6396 // [handler]: The handler property.
6397 DECL_ACCESSORS(handler, Object)
6398
6399 // Casting.
6400 static inline JSProxy* cast(Object* obj);
6401
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006402 MUST_USE_RESULT MaybeObject* SetPropertyWithHandler(
6403 String* name_raw,
6404 Object* value_raw,
6405 PropertyAttributes attributes,
6406 StrictModeFlag strict_mode);
6407
6408 MUST_USE_RESULT PropertyAttributes GetPropertyAttributeWithHandler(
6409 JSReceiver* receiver,
6410 String* name_raw,
6411 bool* has_exception);
6412
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006413 // Dispatched behavior.
6414#ifdef OBJECT_PRINT
6415 inline void JSProxyPrint() {
6416 JSProxyPrint(stdout);
6417 }
6418 void JSProxyPrint(FILE* out);
6419#endif
6420#ifdef DEBUG
6421 void JSProxyVerify();
6422#endif
6423
6424 // Layout description.
6425 static const int kHandlerOffset = HeapObject::kHeaderSize;
6426 static const int kSize = kHandlerOffset + kPointerSize;
6427
6428 typedef FixedBodyDescriptor<kHandlerOffset,
6429 kHandlerOffset + kPointerSize,
6430 kSize> BodyDescriptor;
6431
6432 private:
6433 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxy);
6434};
6435
6436
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00006437// TODO(rossberg): Only a stub for now.
6438class JSFunctionProxy: public JSProxy {
6439 public:
6440 // Casting.
6441 static inline JSFunctionProxy* cast(Object* obj);
6442
6443 private:
6444 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunctionProxy);
6445};
6446
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006447
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006448// Foreign describes objects pointing from JavaScript to C structures.
ager@chromium.org9258b6b2008-09-11 09:11:10 +00006449// Since they cannot contain references to JS HeapObjects they can be
6450// placed in old_data_space.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006451class Foreign: public HeapObject {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006452 public:
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006453 // [address]: field containing the address.
6454 inline Address address();
6455 inline void set_address(Address value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006456
6457 // Casting.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006458 static inline Foreign* cast(Object* obj);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006459
6460 // Dispatched behavior.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006461 inline void ForeignIterateBody(ObjectVisitor* v);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006462
6463 template<typename StaticVisitor>
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006464 inline void ForeignIterateBody();
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00006465
whesse@chromium.org023421e2010-12-21 12:19:12 +00006466#ifdef OBJECT_PRINT
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006467 inline void ForeignPrint() {
6468 ForeignPrint(stdout);
whesse@chromium.org023421e2010-12-21 12:19:12 +00006469 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006470 void ForeignPrint(FILE* out);
whesse@chromium.org023421e2010-12-21 12:19:12 +00006471#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006472#ifdef DEBUG
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006473 void ForeignVerify();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006474#endif
6475
6476 // Layout description.
6477
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006478 static const int kAddressOffset = HeapObject::kHeaderSize;
6479 static const int kSize = kAddressOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006480
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006481 STATIC_CHECK(kAddressOffset == Internals::kForeignAddressOffset);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006482
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006483 private:
ager@chromium.orgea91cc52011-05-23 06:06:11 +00006484 DISALLOW_IMPLICIT_CONSTRUCTORS(Foreign);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006485};
6486
6487
6488// The JSArray describes JavaScript Arrays
6489// Such an array can be in one of two modes:
6490// - fast, backing storage is a FixedArray and length <= elements.length();
6491// Please note: push and pop can be used to grow and shrink the array.
6492// - slow, backing storage is a HashTable with numbers as keys.
6493class JSArray: public JSObject {
6494 public:
6495 // [length]: The length property.
6496 DECL_ACCESSORS(length, Object)
6497
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006498 // Overload the length setter to skip write barrier when the length
6499 // is set to a smi. This matches the set function on FixedArray.
6500 inline void set_length(Smi* length);
6501
lrn@chromium.org303ada72010-10-27 09:33:13 +00006502 MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6503 Object* value);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006504
6505 // Initialize the array with the given capacity. The function may
6506 // fail due to out-of-memory situations, but only if the requested
6507 // capacity is non-zero.
lrn@chromium.org303ada72010-10-27 09:33:13 +00006508 MUST_USE_RESULT MaybeObject* Initialize(int capacity);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006509
6510 // Set the content of the array to the content of storage.
ager@chromium.org7c537e22008-10-16 08:43:32 +00006511 inline void SetContent(FixedArray* storage);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006512
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006513 // Casting.
6514 static inline JSArray* cast(Object* obj);
6515
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006516 // Uses handles. Ensures that the fixed array backing the JSArray has at
6517 // least the stated size.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006518 inline void EnsureSize(int minimum_size_of_backing_fixed_array);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00006519
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006520 // Dispatched behavior.
whesse@chromium.org023421e2010-12-21 12:19:12 +00006521#ifdef OBJECT_PRINT
6522 inline void JSArrayPrint() {
6523 JSArrayPrint(stdout);
6524 }
6525 void JSArrayPrint(FILE* out);
6526#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006527#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006528 void JSArrayVerify();
6529#endif
6530
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00006531 // Number of element slots to pre-allocate for an empty array.
6532 static const int kPreallocatedArrayElements = 4;
6533
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006534 // Layout description.
6535 static const int kLengthOffset = JSObject::kHeaderSize;
6536 static const int kSize = kLengthOffset + kPointerSize;
6537
6538 private:
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006539 // Expand the fixed array backing of a fast-case JSArray to at least
6540 // the requested size.
6541 void Expand(int minimum_size_of_backing_fixed_array);
6542
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006543 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6544};
6545
6546
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00006547// JSRegExpResult is just a JSArray with a specific initial map.
6548// This initial map adds in-object properties for "index" and "input"
6549// properties, as assigned by RegExp.prototype.exec, which allows
6550// faster creation of RegExp exec results.
6551// This class just holds constants used when creating the result.
6552// After creation the result must be treated as a JSArray in all regards.
6553class JSRegExpResult: public JSArray {
6554 public:
6555 // Offsets of object fields.
6556 static const int kIndexOffset = JSArray::kSize;
6557 static const int kInputOffset = kIndexOffset + kPointerSize;
6558 static const int kSize = kInputOffset + kPointerSize;
6559 // Indices of in-object properties.
6560 static const int kIndexIndex = 0;
6561 static const int kInputIndex = 1;
6562 private:
6563 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6564};
6565
6566
ager@chromium.org32912102009-01-16 10:38:43 +00006567// An accessor must have a getter, but can have no setter.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006568//
6569// When setting a property, V8 searches accessors in prototypes.
6570// If an accessor was found and it does not have a setter,
6571// the request is ignored.
6572//
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006573// If the accessor in the prototype has the READ_ONLY property attribute, then
6574// a new value is added to the local object when the property is set.
6575// This shadows the accessor in the prototype.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006576class AccessorInfo: public Struct {
6577 public:
6578 DECL_ACCESSORS(getter, Object)
6579 DECL_ACCESSORS(setter, Object)
6580 DECL_ACCESSORS(data, Object)
6581 DECL_ACCESSORS(name, Object)
6582 DECL_ACCESSORS(flag, Smi)
6583
6584 inline bool all_can_read();
6585 inline void set_all_can_read(bool value);
6586
6587 inline bool all_can_write();
6588 inline void set_all_can_write(bool value);
6589
ager@chromium.org870a0b62008-11-04 11:43:05 +00006590 inline bool prohibits_overwriting();
6591 inline void set_prohibits_overwriting(bool value);
6592
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006593 inline PropertyAttributes property_attributes();
6594 inline void set_property_attributes(PropertyAttributes attributes);
6595
6596 static inline AccessorInfo* cast(Object* obj);
6597
whesse@chromium.org023421e2010-12-21 12:19:12 +00006598#ifdef OBJECT_PRINT
6599 inline void AccessorInfoPrint() {
6600 AccessorInfoPrint(stdout);
6601 }
6602 void AccessorInfoPrint(FILE* out);
6603#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006604#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006605 void AccessorInfoVerify();
6606#endif
6607
ager@chromium.org236ad962008-09-25 09:45:57 +00006608 static const int kGetterOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006609 static const int kSetterOffset = kGetterOffset + kPointerSize;
6610 static const int kDataOffset = kSetterOffset + kPointerSize;
6611 static const int kNameOffset = kDataOffset + kPointerSize;
6612 static const int kFlagOffset = kNameOffset + kPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00006613 static const int kSize = kFlagOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006614
6615 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006616 // Bit positions in flag.
ager@chromium.org870a0b62008-11-04 11:43:05 +00006617 static const int kAllCanReadBit = 0;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006618 static const int kAllCanWriteBit = 1;
ager@chromium.org870a0b62008-11-04 11:43:05 +00006619 static const int kProhibitsOverwritingBit = 2;
6620 class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
mads.s.ager31e71382008-08-13 09:32:07 +00006621
6622 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006623};
6624
6625
6626class AccessCheckInfo: public Struct {
6627 public:
6628 DECL_ACCESSORS(named_callback, Object)
6629 DECL_ACCESSORS(indexed_callback, Object)
6630 DECL_ACCESSORS(data, Object)
6631
6632 static inline AccessCheckInfo* cast(Object* obj);
6633
whesse@chromium.org023421e2010-12-21 12:19:12 +00006634#ifdef OBJECT_PRINT
6635 inline void AccessCheckInfoPrint() {
6636 AccessCheckInfoPrint(stdout);
6637 }
6638 void AccessCheckInfoPrint(FILE* out);
6639#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006640#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006641 void AccessCheckInfoVerify();
6642#endif
6643
ager@chromium.org236ad962008-09-25 09:45:57 +00006644 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006645 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
6646 static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
6647 static const int kSize = kDataOffset + kPointerSize;
6648
6649 private:
6650 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
6651};
6652
6653
6654class InterceptorInfo: public Struct {
6655 public:
6656 DECL_ACCESSORS(getter, Object)
6657 DECL_ACCESSORS(setter, Object)
6658 DECL_ACCESSORS(query, Object)
6659 DECL_ACCESSORS(deleter, Object)
6660 DECL_ACCESSORS(enumerator, Object)
6661 DECL_ACCESSORS(data, Object)
6662
6663 static inline InterceptorInfo* cast(Object* obj);
6664
whesse@chromium.org023421e2010-12-21 12:19:12 +00006665#ifdef OBJECT_PRINT
6666 inline void InterceptorInfoPrint() {
6667 InterceptorInfoPrint(stdout);
6668 }
6669 void InterceptorInfoPrint(FILE* out);
6670#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006671#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006672 void InterceptorInfoVerify();
6673#endif
6674
ager@chromium.org236ad962008-09-25 09:45:57 +00006675 static const int kGetterOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006676 static const int kSetterOffset = kGetterOffset + kPointerSize;
6677 static const int kQueryOffset = kSetterOffset + kPointerSize;
6678 static const int kDeleterOffset = kQueryOffset + kPointerSize;
6679 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
6680 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
6681 static const int kSize = kDataOffset + kPointerSize;
6682
6683 private:
6684 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
6685};
6686
6687
6688class CallHandlerInfo: public Struct {
6689 public:
6690 DECL_ACCESSORS(callback, Object)
6691 DECL_ACCESSORS(data, Object)
6692
6693 static inline CallHandlerInfo* cast(Object* obj);
6694
whesse@chromium.org023421e2010-12-21 12:19:12 +00006695#ifdef OBJECT_PRINT
6696 inline void CallHandlerInfoPrint() {
6697 CallHandlerInfoPrint(stdout);
6698 }
6699 void CallHandlerInfoPrint(FILE* out);
6700#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006701#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006702 void CallHandlerInfoVerify();
6703#endif
6704
ager@chromium.org236ad962008-09-25 09:45:57 +00006705 static const int kCallbackOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006706 static const int kDataOffset = kCallbackOffset + kPointerSize;
erik.corry@gmail.com4a6c3272010-11-18 12:04:40 +00006707 static const int kSize = kDataOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006708
6709 private:
6710 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
6711};
6712
6713
6714class TemplateInfo: public Struct {
6715 public:
6716 DECL_ACCESSORS(tag, Object)
6717 DECL_ACCESSORS(property_list, Object)
6718
6719#ifdef DEBUG
6720 void TemplateInfoVerify();
6721#endif
6722
ager@chromium.org236ad962008-09-25 09:45:57 +00006723 static const int kTagOffset = HeapObject::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006724 static const int kPropertyListOffset = kTagOffset + kPointerSize;
6725 static const int kHeaderSize = kPropertyListOffset + kPointerSize;
6726 protected:
6727 friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6728 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6729};
6730
6731
6732class FunctionTemplateInfo: public TemplateInfo {
6733 public:
6734 DECL_ACCESSORS(serial_number, Object)
6735 DECL_ACCESSORS(call_code, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006736 DECL_ACCESSORS(property_accessors, Object)
6737 DECL_ACCESSORS(prototype_template, Object)
6738 DECL_ACCESSORS(parent_template, Object)
6739 DECL_ACCESSORS(named_property_handler, Object)
6740 DECL_ACCESSORS(indexed_property_handler, Object)
6741 DECL_ACCESSORS(instance_template, Object)
6742 DECL_ACCESSORS(class_name, Object)
6743 DECL_ACCESSORS(signature, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006744 DECL_ACCESSORS(instance_call_handler, Object)
6745 DECL_ACCESSORS(access_check_info, Object)
6746 DECL_ACCESSORS(flag, Smi)
6747
6748 // Following properties use flag bits.
6749 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6750 DECL_BOOLEAN_ACCESSORS(undetectable)
6751 // If the bit is set, object instances created by this function
6752 // requires access check.
6753 DECL_BOOLEAN_ACCESSORS(needs_access_check)
6754
6755 static inline FunctionTemplateInfo* cast(Object* obj);
6756
whesse@chromium.org023421e2010-12-21 12:19:12 +00006757#ifdef OBJECT_PRINT
6758 inline void FunctionTemplateInfoPrint() {
6759 FunctionTemplateInfoPrint(stdout);
6760 }
6761 void FunctionTemplateInfoPrint(FILE* out);
6762#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006763#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006764 void FunctionTemplateInfoVerify();
6765#endif
6766
6767 static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
6768 static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
kasper.lund212ac232008-07-16 07:07:30 +00006769 static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006770 static const int kPrototypeTemplateOffset =
6771 kPropertyAccessorsOffset + kPointerSize;
6772 static const int kParentTemplateOffset =
6773 kPrototypeTemplateOffset + kPointerSize;
6774 static const int kNamedPropertyHandlerOffset =
6775 kParentTemplateOffset + kPointerSize;
6776 static const int kIndexedPropertyHandlerOffset =
6777 kNamedPropertyHandlerOffset + kPointerSize;
6778 static const int kInstanceTemplateOffset =
6779 kIndexedPropertyHandlerOffset + kPointerSize;
6780 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
6781 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
v8.team.kasperl727e9952008-09-02 14:56:44 +00006782 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006783 static const int kAccessCheckInfoOffset =
6784 kInstanceCallHandlerOffset + kPointerSize;
6785 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
6786 static const int kSize = kFlagOffset + kPointerSize;
6787
6788 private:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006789 // Bit position in the flag, from least significant bit position.
6790 static const int kHiddenPrototypeBit = 0;
6791 static const int kUndetectableBit = 1;
6792 static const int kNeedsAccessCheckBit = 2;
mads.s.ager31e71382008-08-13 09:32:07 +00006793
6794 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006795};
6796
6797
6798class ObjectTemplateInfo: public TemplateInfo {
6799 public:
6800 DECL_ACCESSORS(constructor, Object)
kasper.lund212ac232008-07-16 07:07:30 +00006801 DECL_ACCESSORS(internal_field_count, Object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006802
6803 static inline ObjectTemplateInfo* cast(Object* obj);
6804
whesse@chromium.org023421e2010-12-21 12:19:12 +00006805#ifdef OBJECT_PRINT
6806 inline void ObjectTemplateInfoPrint() {
6807 ObjectTemplateInfoPrint(stdout);
6808 }
6809 void ObjectTemplateInfoPrint(FILE* out);
6810#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006811#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006812 void ObjectTemplateInfoVerify();
6813#endif
6814
6815 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
kasper.lund212ac232008-07-16 07:07:30 +00006816 static const int kInternalFieldCountOffset =
6817 kConstructorOffset + kPointerSize;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00006818 static const int kSize = kInternalFieldCountOffset + kPointerSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006819};
6820
6821
6822class SignatureInfo: public Struct {
6823 public:
6824 DECL_ACCESSORS(receiver, Object)
6825 DECL_ACCESSORS(args, Object)
6826
6827 static inline SignatureInfo* cast(Object* obj);
6828
whesse@chromium.org023421e2010-12-21 12:19:12 +00006829#ifdef OBJECT_PRINT
6830 inline void SignatureInfoPrint() {
6831 SignatureInfoPrint(stdout);
6832 }
6833 void SignatureInfoPrint(FILE* out);
6834#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006835#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006836 void SignatureInfoVerify();
6837#endif
6838
ager@chromium.org236ad962008-09-25 09:45:57 +00006839 static const int kReceiverOffset = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006840 static const int kArgsOffset = kReceiverOffset + kPointerSize;
6841 static const int kSize = kArgsOffset + kPointerSize;
6842
6843 private:
6844 DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
6845};
6846
6847
6848class TypeSwitchInfo: public Struct {
6849 public:
6850 DECL_ACCESSORS(types, Object)
6851
6852 static inline TypeSwitchInfo* cast(Object* obj);
6853
whesse@chromium.org023421e2010-12-21 12:19:12 +00006854#ifdef OBJECT_PRINT
6855 inline void TypeSwitchInfoPrint() {
6856 TypeSwitchInfoPrint(stdout);
6857 }
6858 void TypeSwitchInfoPrint(FILE* out);
6859#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006860#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006861 void TypeSwitchInfoVerify();
6862#endif
6863
ager@chromium.org236ad962008-09-25 09:45:57 +00006864 static const int kTypesOffset = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006865 static const int kSize = kTypesOffset + kPointerSize;
6866};
6867
6868
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006869#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org32912102009-01-16 10:38:43 +00006870// The DebugInfo class holds additional information for a function being
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006871// debugged.
6872class DebugInfo: public Struct {
6873 public:
ager@chromium.org32912102009-01-16 10:38:43 +00006874 // The shared function info for the source being debugged.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006875 DECL_ACCESSORS(shared, SharedFunctionInfo)
6876 // Code object for the original code.
6877 DECL_ACCESSORS(original_code, Code)
6878 // Code object for the patched code. This code object is the code object
6879 // currently active for the function.
6880 DECL_ACCESSORS(code, Code)
6881 // Fixed array holding status information for each active break point.
6882 DECL_ACCESSORS(break_points, FixedArray)
6883
6884 // Check if there is a break point at a code position.
6885 bool HasBreakPoint(int code_position);
6886 // Get the break point info object for a code position.
6887 Object* GetBreakPointInfo(int code_position);
6888 // Clear a break point.
6889 static void ClearBreakPoint(Handle<DebugInfo> debug_info,
6890 int code_position,
6891 Handle<Object> break_point_object);
6892 // Set a break point.
6893 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
6894 int source_position, int statement_position,
6895 Handle<Object> break_point_object);
6896 // Get the break point objects for a code position.
6897 Object* GetBreakPointObjects(int code_position);
6898 // Find the break point info holding this break point object.
6899 static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
6900 Handle<Object> break_point_object);
6901 // Get the number of break points for this function.
6902 int GetBreakPointCount();
6903
6904 static inline DebugInfo* cast(Object* obj);
6905
whesse@chromium.org023421e2010-12-21 12:19:12 +00006906#ifdef OBJECT_PRINT
6907 inline void DebugInfoPrint() {
6908 DebugInfoPrint(stdout);
6909 }
6910 void DebugInfoPrint(FILE* out);
6911#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006912#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006913 void DebugInfoVerify();
6914#endif
6915
ager@chromium.org236ad962008-09-25 09:45:57 +00006916 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006917 static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
6918 static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
6919 static const int kActiveBreakPointsCountIndex =
6920 kPatchedCodeIndex + kPointerSize;
6921 static const int kBreakPointsStateIndex =
6922 kActiveBreakPointsCountIndex + kPointerSize;
6923 static const int kSize = kBreakPointsStateIndex + kPointerSize;
6924
6925 private:
6926 static const int kNoBreakPointInfo = -1;
6927
6928 // Lookup the index in the break_points array for a code position.
6929 int GetBreakPointInfoIndex(int code_position);
6930
6931 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
6932};
6933
6934
6935// The BreakPointInfo class holds information for break points set in a
6936// function. The DebugInfo object holds a BreakPointInfo object for each code
6937// position with one or more break points.
6938class BreakPointInfo: public Struct {
6939 public:
6940 // The position in the code for the break point.
6941 DECL_ACCESSORS(code_position, Smi)
6942 // The position in the source for the break position.
6943 DECL_ACCESSORS(source_position, Smi)
6944 // The position in the source for the last statement before this break
6945 // position.
6946 DECL_ACCESSORS(statement_position, Smi)
6947 // List of related JavaScript break points.
6948 DECL_ACCESSORS(break_point_objects, Object)
6949
6950 // Removes a break point.
6951 static void ClearBreakPoint(Handle<BreakPointInfo> info,
6952 Handle<Object> break_point_object);
6953 // Set a break point.
6954 static void SetBreakPoint(Handle<BreakPointInfo> info,
6955 Handle<Object> break_point_object);
6956 // Check if break point info has this break point object.
6957 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
6958 Handle<Object> break_point_object);
6959 // Get the number of break points for this code position.
6960 int GetBreakPointCount();
6961
6962 static inline BreakPointInfo* cast(Object* obj);
6963
whesse@chromium.org023421e2010-12-21 12:19:12 +00006964#ifdef OBJECT_PRINT
6965 inline void BreakPointInfoPrint() {
6966 BreakPointInfoPrint(stdout);
6967 }
6968 void BreakPointInfoPrint(FILE* out);
6969#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006970#ifdef DEBUG
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006971 void BreakPointInfoVerify();
6972#endif
6973
ager@chromium.org236ad962008-09-25 09:45:57 +00006974 static const int kCodePositionIndex = Struct::kHeaderSize;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006975 static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
6976 static const int kStatementPositionIndex =
6977 kSourcePositionIndex + kPointerSize;
6978 static const int kBreakPointObjectsIndex =
6979 kStatementPositionIndex + kPointerSize;
6980 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
6981
6982 private:
6983 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
6984};
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006985#endif // ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006986
6987
6988#undef DECL_BOOLEAN_ACCESSORS
6989#undef DECL_ACCESSORS
6990
6991
6992// Abstract base class for visiting, and optionally modifying, the
6993// pointers contained in Objects. Used in GC and serialization/deserialization.
6994class ObjectVisitor BASE_EMBEDDED {
6995 public:
6996 virtual ~ObjectVisitor() {}
6997
6998 // Visits a contiguous arrays of pointers in the half-open range
6999 // [start, end). Any or all of the values may be modified on return.
7000 virtual void VisitPointers(Object** start, Object** end) = 0;
7001
7002 // To allow lazy clearing of inline caches the visitor has
7003 // a rich interface for iterating over Code objects..
7004
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007005 // Visits a code target in the instruction stream.
7006 virtual void VisitCodeTarget(RelocInfo* rinfo);
7007
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00007008 // Visits a code entry in a JS function.
7009 virtual void VisitCodeEntry(Address entry_address);
7010
kasperl@chromium.orga5551262010-12-07 12:49:48 +00007011 // Visits a global property cell reference in the instruction stream.
7012 virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
7013
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007014 // Visits a runtime entry in the instruction stream.
7015 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
7016
ager@chromium.orgc4c92722009-11-18 14:12:51 +00007017 // Visits the resource of an ASCII or two-byte string.
7018 virtual void VisitExternalAsciiString(
7019 v8::String::ExternalAsciiStringResource** resource) {}
7020 virtual void VisitExternalTwoByteString(
7021 v8::String::ExternalStringResource** resource) {}
7022
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007023 // Visits a debug call target in the instruction stream.
7024 virtual void VisitDebugTarget(RelocInfo* rinfo);
7025
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007026 // Handy shorthand for visiting a single pointer.
7027 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
7028
7029 // Visits a contiguous arrays of external references (references to the C++
7030 // heap) in the half-open range [start, end). Any or all of the values
7031 // may be modified on return.
7032 virtual void VisitExternalReferences(Address* start, Address* end) {}
7033
7034 inline void VisitExternalReference(Address* p) {
7035 VisitExternalReferences(p, p + 1);
7036 }
7037
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00007038 // Visits a handle that has an embedder-assigned class ID.
7039 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
7040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007041#ifdef DEBUG
7042 // Intended for serialization/deserialization checking: insert, or
7043 // check for the presence of, a tag at this position in the stream.
7044 virtual void Synchronize(const char* tag) {}
ager@chromium.org3811b432009-10-28 14:53:37 +00007045#else
7046 inline void Synchronize(const char* tag) {}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007047#endif
7048};
7049
7050
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00007051class StructBodyDescriptor : public
7052 FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
7053 public:
7054 static inline int SizeOf(Map* map, HeapObject* object) {
7055 return map->instance_size();
7056 }
7057};
7058
7059
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007060// BooleanBit is a helper class for setting and getting a bit in an
7061// integer or Smi.
7062class BooleanBit : public AllStatic {
7063 public:
7064 static inline bool get(Smi* smi, int bit_position) {
7065 return get(smi->value(), bit_position);
7066 }
7067
7068 static inline bool get(int value, int bit_position) {
7069 return (value & (1 << bit_position)) != 0;
7070 }
7071
7072 static inline Smi* set(Smi* smi, int bit_position, bool v) {
7073 return Smi::FromInt(set(smi->value(), bit_position, v));
7074 }
7075
7076 static inline int set(int value, int bit_position, bool v) {
7077 if (v) {
7078 value |= (1 << bit_position);
7079 } else {
7080 value &= ~(1 << bit_position);
7081 }
7082 return value;
7083 }
7084};
7085
7086} } // namespace v8::internal
7087
7088#endif // V8_OBJECTS_H_