objects.h revision 6d7cb000ed533f52d745e60663019ff891bb19a8
1// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_OBJECTS_H_
29#define V8_OBJECTS_H_
30
31#include "builtins.h"
32#include "smart-pointer.h"
33#include "unicode-inl.h"
34#if V8_TARGET_ARCH_ARM
35#include "arm/constants-arm.h"
36#elif V8_TARGET_ARCH_MIPS
37#include "mips/constants-mips.h"
38#endif
39
40//
41// Most object types in the V8 JavaScript are described in this file.
42//
43// Inheritance hierarchy:
44// - MaybeObject    (an object or a failure)
45//   - Failure      (immediate for marking failed operation)
46//   - Object
47//     - Smi          (immediate small integer)
48//     - HeapObject   (superclass for everything allocated in the heap)
49//       - JSObject
50//         - JSArray
51//         - JSRegExp
52//         - JSFunction
53//         - GlobalObject
54//           - JSGlobalObject
55//           - JSBuiltinsObject
56//         - JSGlobalProxy
57//         - JSValue
58//         - JSMessageObject
59//       - ByteArray
60//       - ExternalArray
61//         - ExternalPixelArray
62//         - ExternalByteArray
63//         - ExternalUnsignedByteArray
64//         - ExternalShortArray
65//         - ExternalUnsignedShortArray
66//         - ExternalIntArray
67//         - ExternalUnsignedIntArray
68//         - ExternalFloatArray
69//       - FixedArray
70//         - DescriptorArray
71//         - HashTable
72//           - Dictionary
73//           - SymbolTable
74//           - CompilationCacheTable
75//           - CodeCacheHashTable
76//           - MapCache
77//         - Context
78//         - JSFunctionResultCache
79//         - SerializedScopeInfo
80//       - String
81//         - SeqString
82//           - SeqAsciiString
83//           - SeqTwoByteString
84//         - ConsString
85//         - ExternalString
86//           - ExternalAsciiString
87//           - ExternalTwoByteString
88//       - HeapNumber
89//       - Code
90//       - Map
91//       - Oddball
92//       - Proxy
93//       - SharedFunctionInfo
94//       - Struct
95//         - AccessorInfo
96//         - AccessCheckInfo
97//         - InterceptorInfo
98//         - CallHandlerInfo
99//         - TemplateInfo
100//           - FunctionTemplateInfo
101//           - ObjectTemplateInfo
102//         - Script
103//         - SignatureInfo
104//         - TypeSwitchInfo
105//         - DebugInfo
106//         - BreakPointInfo
107//         - CodeCache
108//
109// Formats of Object*:
110//  Smi:        [31 bit signed int] 0
111//  HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
112//  Failure:    [30 bit signed int] 11
113
114// Ecma-262 3rd 8.6.1
115enum PropertyAttributes {
116  NONE              = v8::None,
117  READ_ONLY         = v8::ReadOnly,
118  DONT_ENUM         = v8::DontEnum,
119  DONT_DELETE       = v8::DontDelete,
120  ABSENT            = 16  // Used in runtime to indicate a property is absent.
121  // ABSENT can never be stored in or returned from a descriptor's attributes
122  // bitfield.  It is only used as a return value meaning the attributes of
123  // a non-existent property.
124};
125
126namespace v8 {
127namespace internal {
128
129
130// PropertyDetails captures type and attributes for a property.
131// They are used both in property dictionaries and instance descriptors.
132class PropertyDetails BASE_EMBEDDED {
133 public:
134
135  PropertyDetails(PropertyAttributes attributes,
136                  PropertyType type,
137                  int index = 0) {
138    ASSERT(type != EXTERNAL_ARRAY_TRANSITION);
139    ASSERT(TypeField::is_valid(type));
140    ASSERT(AttributesField::is_valid(attributes));
141    ASSERT(StorageField::is_valid(index));
142
143    value_ = TypeField::encode(type)
144        | AttributesField::encode(attributes)
145        | StorageField::encode(index);
146
147    ASSERT(type == this->type());
148    ASSERT(attributes == this->attributes());
149    ASSERT(index == this->index());
150  }
151
152  PropertyDetails(PropertyAttributes attributes,
153                  PropertyType type,
154                  ExternalArrayType array_type) {
155    ASSERT(type == EXTERNAL_ARRAY_TRANSITION);
156    ASSERT(TypeField::is_valid(type));
157    ASSERT(AttributesField::is_valid(attributes));
158    ASSERT(StorageField::is_valid(static_cast<int>(array_type)));
159
160    value_ = TypeField::encode(type)
161        | AttributesField::encode(attributes)
162        | StorageField::encode(static_cast<int>(array_type));
163
164    ASSERT(type == this->type());
165    ASSERT(attributes == this->attributes());
166    ASSERT(array_type == this->array_type());
167  }
168
169  // Conversion for storing details as Object*.
170  explicit inline PropertyDetails(Smi* smi);
171  inline Smi* AsSmi();
172
173  PropertyType type() { return TypeField::decode(value_); }
174
175  bool IsTransition() {
176    PropertyType t = type();
177    ASSERT(t != INTERCEPTOR);
178    return t == MAP_TRANSITION || t == CONSTANT_TRANSITION ||
179        t == EXTERNAL_ARRAY_TRANSITION;
180  }
181
182  bool IsProperty() {
183    return type() < FIRST_PHANTOM_PROPERTY_TYPE;
184  }
185
186  PropertyAttributes attributes() { return AttributesField::decode(value_); }
187
188  int index() { return StorageField::decode(value_); }
189
190  ExternalArrayType array_type() {
191    ASSERT(type() == EXTERNAL_ARRAY_TRANSITION);
192    return static_cast<ExternalArrayType>(StorageField::decode(value_));
193  }
194
195  inline PropertyDetails AsDeleted();
196
197  static bool IsValidIndex(int index) {
198    return StorageField::is_valid(index);
199  }
200
201  bool IsReadOnly() { return (attributes() & READ_ONLY) != 0; }
202  bool IsDontDelete() { return (attributes() & DONT_DELETE) != 0; }
203  bool IsDontEnum() { return (attributes() & DONT_ENUM) != 0; }
204  bool IsDeleted() { return DeletedField::decode(value_) != 0;}
205
206  // Bit fields in value_ (type, shift, size). Must be public so the
207  // constants can be embedded in generated code.
208  class TypeField:       public BitField<PropertyType,       0, 4> {};
209  class AttributesField: public BitField<PropertyAttributes, 4, 3> {};
210  class DeletedField:    public BitField<uint32_t,           7, 1> {};
211  class StorageField:    public BitField<uint32_t,           8, 32-8> {};
212
213  static const int kInitialIndex = 1;
214 private:
215  uint32_t value_;
216};
217
218
219// Setter that skips the write barrier if mode is SKIP_WRITE_BARRIER.
220enum WriteBarrierMode { SKIP_WRITE_BARRIER, UPDATE_WRITE_BARRIER };
221
222
223// PropertyNormalizationMode is used to specify whether to keep
224// inobject properties when normalizing properties of a JSObject.
225enum PropertyNormalizationMode {
226  CLEAR_INOBJECT_PROPERTIES,
227  KEEP_INOBJECT_PROPERTIES
228};
229
230
231// NormalizedMapSharingMode is used to specify whether a map may be shared
232// by different objects with normalized properties.
233enum NormalizedMapSharingMode {
234  UNIQUE_NORMALIZED_MAP,
235  SHARED_NORMALIZED_MAP
236};
237
238
239// Instance size sentinel for objects of variable size.
240static const int kVariableSizeSentinel = 0;
241
242
243// All Maps have a field instance_type containing a InstanceType.
244// It describes the type of the instances.
245//
246// As an example, a JavaScript object is a heap object and its map
247// instance_type is JS_OBJECT_TYPE.
248//
249// The names of the string instance types are intended to systematically
250// mirror their encoding in the instance_type field of the map.  The default
251// encoding is considered TWO_BYTE.  It is not mentioned in the name.  ASCII
252// encoding is mentioned explicitly in the name.  Likewise, the default
253// representation is considered sequential.  It is not mentioned in the
254// name.  The other representations (eg, CONS, EXTERNAL) are explicitly
255// mentioned.  Finally, the string is either a SYMBOL_TYPE (if it is a
256// symbol) or a STRING_TYPE (if it is not a symbol).
257//
258// NOTE: The following things are some that depend on the string types having
259// instance_types that are less than those of all other types:
260// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
261// Object::IsString.
262//
263// NOTE: Everything following JS_VALUE_TYPE is considered a
264// JSObject for GC purposes. The first four entries here have typeof
265// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
266#define INSTANCE_TYPE_LIST_ALL(V)                                              \
267  V(SYMBOL_TYPE)                                                               \
268  V(ASCII_SYMBOL_TYPE)                                                         \
269  V(CONS_SYMBOL_TYPE)                                                          \
270  V(CONS_ASCII_SYMBOL_TYPE)                                                    \
271  V(EXTERNAL_SYMBOL_TYPE)                                                      \
272  V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE)                                      \
273  V(EXTERNAL_ASCII_SYMBOL_TYPE)                                                \
274  V(STRING_TYPE)                                                               \
275  V(ASCII_STRING_TYPE)                                                         \
276  V(CONS_STRING_TYPE)                                                          \
277  V(CONS_ASCII_STRING_TYPE)                                                    \
278  V(EXTERNAL_STRING_TYPE)                                                      \
279  V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE)                                      \
280  V(EXTERNAL_ASCII_STRING_TYPE)                                                \
281  V(PRIVATE_EXTERNAL_ASCII_STRING_TYPE)                                        \
282                                                                               \
283  V(MAP_TYPE)                                                                  \
284  V(CODE_TYPE)                                                                 \
285  V(ODDBALL_TYPE)                                                              \
286  V(JS_GLOBAL_PROPERTY_CELL_TYPE)                                              \
287                                                                               \
288  V(HEAP_NUMBER_TYPE)                                                          \
289  V(PROXY_TYPE)                                                                \
290  V(BYTE_ARRAY_TYPE)                                                           \
291  /* Note: the order of these external array */                                \
292  /* types is relied upon in */                                                \
293  /* Object::IsExternalArray(). */                                             \
294  V(EXTERNAL_BYTE_ARRAY_TYPE)                                                  \
295  V(EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE)                                         \
296  V(EXTERNAL_SHORT_ARRAY_TYPE)                                                 \
297  V(EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE)                                        \
298  V(EXTERNAL_INT_ARRAY_TYPE)                                                   \
299  V(EXTERNAL_UNSIGNED_INT_ARRAY_TYPE)                                          \
300  V(EXTERNAL_FLOAT_ARRAY_TYPE)                                                 \
301  V(EXTERNAL_PIXEL_ARRAY_TYPE)                                                 \
302  V(FILLER_TYPE)                                                               \
303                                                                               \
304  V(ACCESSOR_INFO_TYPE)                                                        \
305  V(ACCESS_CHECK_INFO_TYPE)                                                    \
306  V(INTERCEPTOR_INFO_TYPE)                                                     \
307  V(CALL_HANDLER_INFO_TYPE)                                                    \
308  V(FUNCTION_TEMPLATE_INFO_TYPE)                                               \
309  V(OBJECT_TEMPLATE_INFO_TYPE)                                                 \
310  V(SIGNATURE_INFO_TYPE)                                                       \
311  V(TYPE_SWITCH_INFO_TYPE)                                                     \
312  V(SCRIPT_TYPE)                                                               \
313  V(CODE_CACHE_TYPE)                                                           \
314                                                                               \
315  V(FIXED_ARRAY_TYPE)                                                          \
316  V(SHARED_FUNCTION_INFO_TYPE)                                                 \
317                                                                               \
318  V(JS_MESSAGE_OBJECT_TYPE)                                                    \
319                                                                               \
320  V(JS_VALUE_TYPE)                                                             \
321  V(JS_OBJECT_TYPE)                                                            \
322  V(JS_CONTEXT_EXTENSION_OBJECT_TYPE)                                          \
323  V(JS_GLOBAL_OBJECT_TYPE)                                                     \
324  V(JS_BUILTINS_OBJECT_TYPE)                                                   \
325  V(JS_GLOBAL_PROXY_TYPE)                                                      \
326  V(JS_ARRAY_TYPE)                                                             \
327  V(JS_REGEXP_TYPE)                                                            \
328                                                                               \
329  V(JS_FUNCTION_TYPE)                                                          \
330
331#ifdef ENABLE_DEBUGGER_SUPPORT
332#define INSTANCE_TYPE_LIST_DEBUGGER(V)                                         \
333  V(DEBUG_INFO_TYPE)                                                           \
334  V(BREAK_POINT_INFO_TYPE)
335#else
336#define INSTANCE_TYPE_LIST_DEBUGGER(V)
337#endif
338
339#define INSTANCE_TYPE_LIST(V)                                                  \
340  INSTANCE_TYPE_LIST_ALL(V)                                                    \
341  INSTANCE_TYPE_LIST_DEBUGGER(V)
342
343
344// Since string types are not consecutive, this macro is used to
345// iterate over them.
346#define STRING_TYPE_LIST(V)                                                    \
347  V(SYMBOL_TYPE,                                                               \
348    kVariableSizeSentinel,                                                     \
349    symbol,                                                                    \
350    Symbol)                                                                    \
351  V(ASCII_SYMBOL_TYPE,                                                         \
352    kVariableSizeSentinel,                                                     \
353    ascii_symbol,                                                              \
354    AsciiSymbol)                                                               \
355  V(CONS_SYMBOL_TYPE,                                                          \
356    ConsString::kSize,                                                         \
357    cons_symbol,                                                               \
358    ConsSymbol)                                                                \
359  V(CONS_ASCII_SYMBOL_TYPE,                                                    \
360    ConsString::kSize,                                                         \
361    cons_ascii_symbol,                                                         \
362    ConsAsciiSymbol)                                                           \
363  V(EXTERNAL_SYMBOL_TYPE,                                                      \
364    ExternalTwoByteString::kSize,                                              \
365    external_symbol,                                                           \
366    ExternalSymbol)                                                            \
367  V(EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE,                                      \
368    ExternalTwoByteString::kSize,                                              \
369    external_symbol_with_ascii_data,                                           \
370    ExternalSymbolWithAsciiData)                                               \
371  V(EXTERNAL_ASCII_SYMBOL_TYPE,                                                \
372    ExternalAsciiString::kSize,                                                \
373    external_ascii_symbol,                                                     \
374    ExternalAsciiSymbol)                                                       \
375  V(STRING_TYPE,                                                               \
376    kVariableSizeSentinel,                                                     \
377    string,                                                                    \
378    String)                                                                    \
379  V(ASCII_STRING_TYPE,                                                         \
380    kVariableSizeSentinel,                                                     \
381    ascii_string,                                                              \
382    AsciiString)                                                               \
383  V(CONS_STRING_TYPE,                                                          \
384    ConsString::kSize,                                                         \
385    cons_string,                                                               \
386    ConsString)                                                                \
387  V(CONS_ASCII_STRING_TYPE,                                                    \
388    ConsString::kSize,                                                         \
389    cons_ascii_string,                                                         \
390    ConsAsciiString)                                                           \
391  V(EXTERNAL_STRING_TYPE,                                                      \
392    ExternalTwoByteString::kSize,                                              \
393    external_string,                                                           \
394    ExternalString)                                                            \
395  V(EXTERNAL_STRING_WITH_ASCII_DATA_TYPE,                                      \
396    ExternalTwoByteString::kSize,                                              \
397    external_string_with_ascii_data,                                           \
398    ExternalStringWithAsciiData)                                               \
399  V(EXTERNAL_ASCII_STRING_TYPE,                                                \
400    ExternalAsciiString::kSize,                                                \
401    external_ascii_string,                                                     \
402    ExternalAsciiString)
403
404// A struct is a simple object a set of object-valued fields.  Including an
405// object type in this causes the compiler to generate most of the boilerplate
406// code for the class including allocation and garbage collection routines,
407// casts and predicates.  All you need to define is the class, methods and
408// object verification routines.  Easy, no?
409//
410// Note that for subtle reasons related to the ordering or numerical values of
411// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
412// manually.
413#define STRUCT_LIST_ALL(V)                                                     \
414  V(ACCESSOR_INFO, AccessorInfo, accessor_info)                                \
415  V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info)                     \
416  V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info)                       \
417  V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info)                     \
418  V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info)      \
419  V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info)            \
420  V(SIGNATURE_INFO, SignatureInfo, signature_info)                             \
421  V(TYPE_SWITCH_INFO, TypeSwitchInfo, type_switch_info)                        \
422  V(SCRIPT, Script, script)                                                    \
423  V(CODE_CACHE, CodeCache, code_cache)
424
425#ifdef ENABLE_DEBUGGER_SUPPORT
426#define STRUCT_LIST_DEBUGGER(V)                                                \
427  V(DEBUG_INFO, DebugInfo, debug_info)                                         \
428  V(BREAK_POINT_INFO, BreakPointInfo, break_point_info)
429#else
430#define STRUCT_LIST_DEBUGGER(V)
431#endif
432
433#define STRUCT_LIST(V)                                                         \
434  STRUCT_LIST_ALL(V)                                                           \
435  STRUCT_LIST_DEBUGGER(V)
436
437// We use the full 8 bits of the instance_type field to encode heap object
438// instance types.  The high-order bit (bit 7) is set if the object is not a
439// string, and cleared if it is a string.
440const uint32_t kIsNotStringMask = 0x80;
441const uint32_t kStringTag = 0x0;
442const uint32_t kNotStringTag = 0x80;
443
444// Bit 6 indicates that the object is a symbol (if set) or not (if cleared).
445// There are not enough types that the non-string types (with bit 7 set) can
446// have bit 6 set too.
447const uint32_t kIsSymbolMask = 0x40;
448const uint32_t kNotSymbolTag = 0x0;
449const uint32_t kSymbolTag = 0x40;
450
451// If bit 7 is clear then bit 2 indicates whether the string consists of
452// two-byte characters or one-byte characters.
453const uint32_t kStringEncodingMask = 0x4;
454const uint32_t kTwoByteStringTag = 0x0;
455const uint32_t kAsciiStringTag = 0x4;
456
457// If bit 7 is clear, the low-order 2 bits indicate the representation
458// of the string.
459const uint32_t kStringRepresentationMask = 0x03;
460enum StringRepresentationTag {
461  kSeqStringTag = 0x0,
462  kConsStringTag = 0x1,
463  kExternalStringTag = 0x2
464};
465const uint32_t kIsConsStringMask = 0x1;
466
467// If bit 7 is clear, then bit 3 indicates whether this two-byte
468// string actually contains ascii data.
469const uint32_t kAsciiDataHintMask = 0x08;
470const uint32_t kAsciiDataHintTag = 0x08;
471
472
473// A ConsString with an empty string as the right side is a candidate
474// for being shortcut by the garbage collector unless it is a
475// symbol. It's not common to have non-flat symbols, so we do not
476// shortcut them thereby avoiding turning symbols into strings. See
477// heap.cc and mark-compact.cc.
478const uint32_t kShortcutTypeMask =
479    kIsNotStringMask |
480    kIsSymbolMask |
481    kStringRepresentationMask;
482const uint32_t kShortcutTypeTag = kConsStringTag;
483
484
485enum InstanceType {
486  // String types.
487  // FIRST_STRING_TYPE
488  SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kSeqStringTag,
489  ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kSeqStringTag,
490  CONS_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kConsStringTag,
491  CONS_ASCII_SYMBOL_TYPE = kAsciiStringTag | kSymbolTag | kConsStringTag,
492  EXTERNAL_SYMBOL_TYPE = kTwoByteStringTag | kSymbolTag | kExternalStringTag,
493  EXTERNAL_SYMBOL_WITH_ASCII_DATA_TYPE =
494      kTwoByteStringTag | kSymbolTag | kExternalStringTag | kAsciiDataHintTag,
495  EXTERNAL_ASCII_SYMBOL_TYPE =
496      kAsciiStringTag | kSymbolTag | kExternalStringTag,
497  STRING_TYPE = kTwoByteStringTag | kSeqStringTag,
498  ASCII_STRING_TYPE = kAsciiStringTag | kSeqStringTag,
499  CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag,
500  CONS_ASCII_STRING_TYPE = kAsciiStringTag | kConsStringTag,
501  EXTERNAL_STRING_TYPE = kTwoByteStringTag | kExternalStringTag,
502  EXTERNAL_STRING_WITH_ASCII_DATA_TYPE =
503      kTwoByteStringTag | kExternalStringTag | kAsciiDataHintTag,
504  // LAST_STRING_TYPE
505  EXTERNAL_ASCII_STRING_TYPE = kAsciiStringTag | kExternalStringTag,
506  PRIVATE_EXTERNAL_ASCII_STRING_TYPE = EXTERNAL_ASCII_STRING_TYPE,
507
508  // Objects allocated in their own spaces (never in new space).
509  MAP_TYPE = kNotStringTag,  // FIRST_NONSTRING_TYPE
510  CODE_TYPE,
511  ODDBALL_TYPE,
512  JS_GLOBAL_PROPERTY_CELL_TYPE,
513
514  // "Data", objects that cannot contain non-map-word pointers to heap
515  // objects.
516  HEAP_NUMBER_TYPE,
517  PROXY_TYPE,
518  BYTE_ARRAY_TYPE,
519  EXTERNAL_BYTE_ARRAY_TYPE,  // FIRST_EXTERNAL_ARRAY_TYPE
520  EXTERNAL_UNSIGNED_BYTE_ARRAY_TYPE,
521  EXTERNAL_SHORT_ARRAY_TYPE,
522  EXTERNAL_UNSIGNED_SHORT_ARRAY_TYPE,
523  EXTERNAL_INT_ARRAY_TYPE,
524  EXTERNAL_UNSIGNED_INT_ARRAY_TYPE,
525  EXTERNAL_FLOAT_ARRAY_TYPE,
526  EXTERNAL_PIXEL_ARRAY_TYPE,  // LAST_EXTERNAL_ARRAY_TYPE
527  FILLER_TYPE,  // LAST_DATA_TYPE
528
529  // Structs.
530  ACCESSOR_INFO_TYPE,
531  ACCESS_CHECK_INFO_TYPE,
532  INTERCEPTOR_INFO_TYPE,
533  CALL_HANDLER_INFO_TYPE,
534  FUNCTION_TEMPLATE_INFO_TYPE,
535  OBJECT_TEMPLATE_INFO_TYPE,
536  SIGNATURE_INFO_TYPE,
537  TYPE_SWITCH_INFO_TYPE,
538  SCRIPT_TYPE,
539  CODE_CACHE_TYPE,
540  // The following two instance types are only used when ENABLE_DEBUGGER_SUPPORT
541  // is defined. However as include/v8.h contain some of the instance type
542  // constants always having them avoids them getting different numbers
543  // depending on whether ENABLE_DEBUGGER_SUPPORT is defined or not.
544  DEBUG_INFO_TYPE,
545  BREAK_POINT_INFO_TYPE,
546
547  FIXED_ARRAY_TYPE,
548  SHARED_FUNCTION_INFO_TYPE,
549
550  JS_MESSAGE_OBJECT_TYPE,
551
552  JS_VALUE_TYPE,  // FIRST_JS_OBJECT_TYPE
553  JS_OBJECT_TYPE,
554  JS_CONTEXT_EXTENSION_OBJECT_TYPE,
555  JS_GLOBAL_OBJECT_TYPE,
556  JS_BUILTINS_OBJECT_TYPE,
557  JS_GLOBAL_PROXY_TYPE,
558  JS_ARRAY_TYPE,
559
560  JS_REGEXP_TYPE,  // LAST_JS_OBJECT_TYPE, FIRST_FUNCTION_CLASS_TYPE
561
562  JS_FUNCTION_TYPE,
563
564  // Pseudo-types
565  FIRST_TYPE = 0x0,
566  LAST_TYPE = JS_FUNCTION_TYPE,
567  INVALID_TYPE = FIRST_TYPE - 1,
568  FIRST_NONSTRING_TYPE = MAP_TYPE,
569  FIRST_STRING_TYPE = FIRST_TYPE,
570  LAST_STRING_TYPE = FIRST_NONSTRING_TYPE - 1,
571  // Boundaries for testing for an external array.
572  FIRST_EXTERNAL_ARRAY_TYPE = EXTERNAL_BYTE_ARRAY_TYPE,
573  LAST_EXTERNAL_ARRAY_TYPE = EXTERNAL_PIXEL_ARRAY_TYPE,
574  // Boundary for promotion to old data space/old pointer space.
575  LAST_DATA_TYPE = FILLER_TYPE,
576  // Boundaries for testing the type is a JavaScript "object".  Note that
577  // function objects are not counted as objects, even though they are
578  // implemented as such; only values whose typeof is "object" are included.
579  FIRST_JS_OBJECT_TYPE = JS_VALUE_TYPE,
580  LAST_JS_OBJECT_TYPE = JS_REGEXP_TYPE,
581  // RegExp objects have [[Class]] "function" because they are callable.
582  // All types from this type and above are objects with [[Class]] "function".
583  FIRST_FUNCTION_CLASS_TYPE = JS_REGEXP_TYPE
584};
585
586static const int kExternalArrayTypeCount = LAST_EXTERNAL_ARRAY_TYPE -
587    FIRST_EXTERNAL_ARRAY_TYPE + 1;
588
589STATIC_CHECK(JS_OBJECT_TYPE == Internals::kJSObjectType);
590STATIC_CHECK(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
591STATIC_CHECK(PROXY_TYPE == Internals::kProxyType);
592
593
594enum CompareResult {
595  LESS      = -1,
596  EQUAL     =  0,
597  GREATER   =  1,
598
599  NOT_EQUAL = GREATER
600};
601
602
603#define DECL_BOOLEAN_ACCESSORS(name)   \
604  inline bool name();                  \
605  inline void set_##name(bool value);  \
606
607
608#define DECL_ACCESSORS(name, type)                                      \
609  inline type* name();                                                  \
610  inline void set_##name(type* value,                                   \
611                         WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
612
613
614class StringStream;
615class ObjectVisitor;
616class Failure;
617
618struct ValueInfo : public Malloced {
619  ValueInfo() : type(FIRST_TYPE), ptr(NULL), str(NULL), number(0) { }
620  InstanceType type;
621  Object* ptr;
622  const char* str;
623  double number;
624};
625
626
627// A template-ized version of the IsXXX functions.
628template <class C> static inline bool Is(Object* obj);
629
630
631class MaybeObject BASE_EMBEDDED {
632 public:
633  inline bool IsFailure();
634  inline bool IsRetryAfterGC();
635  inline bool IsOutOfMemory();
636  inline bool IsException();
637  INLINE(bool IsTheHole());
638  inline bool ToObject(Object** obj) {
639    if (IsFailure()) return false;
640    *obj = reinterpret_cast<Object*>(this);
641    return true;
642  }
643  inline Failure* ToFailureUnchecked() {
644    ASSERT(IsFailure());
645    return reinterpret_cast<Failure*>(this);
646  }
647  inline Object* ToObjectUnchecked() {
648    ASSERT(!IsFailure());
649    return reinterpret_cast<Object*>(this);
650  }
651  inline Object* ToObjectChecked() {
652    CHECK(!IsFailure());
653    return reinterpret_cast<Object*>(this);
654  }
655
656  template<typename T>
657  inline bool To(T** obj) {
658    if (IsFailure()) return false;
659    *obj = T::cast(reinterpret_cast<Object*>(this));
660    return true;
661  }
662
663#ifdef OBJECT_PRINT
664  // Prints this object with details.
665  inline void Print() {
666    Print(stdout);
667  };
668  inline void PrintLn() {
669    PrintLn(stdout);
670  }
671  void Print(FILE* out);
672  void PrintLn(FILE* out);
673#endif
674#ifdef DEBUG
675  // Verifies the object.
676  void Verify();
677#endif
678};
679
680
681#define OBJECT_TYPE_LIST(V)                    \
682  V(Smi)                                       \
683  V(HeapObject)                                \
684  V(Number)                                    \
685
686#define HEAP_OBJECT_TYPE_LIST(V)               \
687  V(HeapNumber)                                \
688  V(String)                                    \
689  V(Symbol)                                    \
690  V(SeqString)                                 \
691  V(ExternalString)                            \
692  V(ConsString)                                \
693  V(ExternalTwoByteString)                     \
694  V(ExternalAsciiString)                       \
695  V(SeqTwoByteString)                          \
696  V(SeqAsciiString)                            \
697                                               \
698  V(ExternalArray)                             \
699  V(ExternalByteArray)                         \
700  V(ExternalUnsignedByteArray)                 \
701  V(ExternalShortArray)                        \
702  V(ExternalUnsignedShortArray)                \
703  V(ExternalIntArray)                          \
704  V(ExternalUnsignedIntArray)                  \
705  V(ExternalFloatArray)                        \
706  V(ExternalPixelArray)                        \
707  V(ByteArray)                                 \
708  V(JSObject)                                  \
709  V(JSContextExtensionObject)                  \
710  V(Map)                                       \
711  V(DescriptorArray)                           \
712  V(DeoptimizationInputData)                   \
713  V(DeoptimizationOutputData)                  \
714  V(FixedArray)                                \
715  V(Context)                                   \
716  V(CatchContext)                              \
717  V(GlobalContext)                             \
718  V(JSFunction)                                \
719  V(Code)                                      \
720  V(Oddball)                                   \
721  V(SharedFunctionInfo)                        \
722  V(JSValue)                                   \
723  V(JSMessageObject)                           \
724  V(StringWrapper)                             \
725  V(Proxy)                                     \
726  V(Boolean)                                   \
727  V(JSArray)                                   \
728  V(JSRegExp)                                  \
729  V(HashTable)                                 \
730  V(Dictionary)                                \
731  V(SymbolTable)                               \
732  V(JSFunctionResultCache)                     \
733  V(NormalizedMapCache)                        \
734  V(CompilationCacheTable)                     \
735  V(CodeCacheHashTable)                        \
736  V(MapCache)                                  \
737  V(Primitive)                                 \
738  V(GlobalObject)                              \
739  V(JSGlobalObject)                            \
740  V(JSBuiltinsObject)                          \
741  V(JSGlobalProxy)                             \
742  V(UndetectableObject)                        \
743  V(AccessCheckNeeded)                         \
744  V(JSGlobalPropertyCell)                      \
745
746// Object is the abstract superclass for all classes in the
747// object hierarchy.
748// Object does not use any virtual functions to avoid the
749// allocation of the C++ vtable.
750// Since Smi and Failure are subclasses of Object no
751// data members can be present in Object.
752class Object : public MaybeObject {
753 public:
754  // Type testing.
755#define IS_TYPE_FUNCTION_DECL(type_)  inline bool Is##type_();
756  OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
757  HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
758#undef IS_TYPE_FUNCTION_DECL
759
760  // Returns true if this object is an instance of the specified
761  // function template.
762  inline bool IsInstanceOf(FunctionTemplateInfo* type);
763
764  inline bool IsStruct();
765#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) inline bool Is##Name();
766  STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
767#undef DECLARE_STRUCT_PREDICATE
768
769  // Oddball testing.
770  INLINE(bool IsUndefined());
771  INLINE(bool IsNull());
772  INLINE(bool IsTheHole());  // Shadows MaybeObject's implementation.
773  INLINE(bool IsTrue());
774  INLINE(bool IsFalse());
775  inline bool IsArgumentsMarker();
776
777  // Extract the number.
778  inline double Number();
779
780  inline bool HasSpecificClassOf(String* name);
781
782  MUST_USE_RESULT MaybeObject* ToObject();             // ECMA-262 9.9.
783  Object* ToBoolean();                                 // ECMA-262 9.2.
784
785  // Convert to a JSObject if needed.
786  // global_context is used when creating wrapper object.
787  MUST_USE_RESULT MaybeObject* ToObject(Context* global_context);
788
789  // Converts this to a Smi if possible.
790  // Failure is returned otherwise.
791  MUST_USE_RESULT inline MaybeObject* ToSmi();
792
793  void Lookup(String* name, LookupResult* result);
794
795  // Property access.
796  MUST_USE_RESULT inline MaybeObject* GetProperty(String* key);
797  MUST_USE_RESULT inline MaybeObject* GetProperty(
798      String* key,
799      PropertyAttributes* attributes);
800  MUST_USE_RESULT MaybeObject* GetPropertyWithReceiver(
801      Object* receiver,
802      String* key,
803      PropertyAttributes* attributes);
804  MUST_USE_RESULT MaybeObject* GetProperty(Object* receiver,
805                                           LookupResult* result,
806                                           String* key,
807                                           PropertyAttributes* attributes);
808  MUST_USE_RESULT MaybeObject* GetPropertyWithCallback(Object* receiver,
809                                                       Object* structure,
810                                                       String* name,
811                                                       Object* holder);
812  MUST_USE_RESULT MaybeObject* GetPropertyWithDefinedGetter(Object* receiver,
813                                                            JSFunction* getter);
814
815  inline MaybeObject* GetElement(uint32_t index);
816  // For use when we know that no exception can be thrown.
817  inline Object* GetElementNoExceptionThrown(uint32_t index);
818  MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
819
820  // Return the object's prototype (might be Heap::null_value()).
821  Object* GetPrototype();
822
823  // Tries to convert an object to an array index.  Returns true and sets
824  // the output parameter if it succeeds.
825  inline bool ToArrayIndex(uint32_t* index);
826
827  // Returns true if this is a JSValue containing a string and the index is
828  // < the length of the string.  Used to implement [] on strings.
829  inline bool IsStringObjectWithCharacterAt(uint32_t index);
830
831#ifdef DEBUG
832  // Verify a pointer is a valid object pointer.
833  static void VerifyPointer(Object* p);
834#endif
835
836  // Prints this object without details.
837  inline void ShortPrint() {
838    ShortPrint(stdout);
839  }
840  void ShortPrint(FILE* out);
841
842  // Prints this object without details to a message accumulator.
843  void ShortPrint(StringStream* accumulator);
844
845  // Casting: This cast is only needed to satisfy macros in objects-inl.h.
846  static Object* cast(Object* value) { return value; }
847
848  // Layout description.
849  static const int kHeaderSize = 0;  // Object does not take up any space.
850
851 private:
852  DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
853};
854
855
856// Smi represents integer Numbers that can be stored in 31 bits.
857// Smis are immediate which means they are NOT allocated in the heap.
858// The this pointer has the following format: [31 bit signed int] 0
859// For long smis it has the following format:
860//     [32 bit signed int] [31 bits zero padding] 0
861// Smi stands for small integer.
862class Smi: public Object {
863 public:
864  // Returns the integer value.
865  inline int value();
866
867  // Convert a value to a Smi object.
868  static inline Smi* FromInt(int value);
869
870  static inline Smi* FromIntptr(intptr_t value);
871
872  // Returns whether value can be represented in a Smi.
873  static inline bool IsValid(intptr_t value);
874
875  // Casting.
876  static inline Smi* cast(Object* object);
877
878  // Dispatched behavior.
879  inline void SmiPrint() {
880    SmiPrint(stdout);
881  }
882  void SmiPrint(FILE* out);
883  void SmiPrint(StringStream* accumulator);
884#ifdef DEBUG
885  void SmiVerify();
886#endif
887
888  static const int kMinValue = (-1 << (kSmiValueSize - 1));
889  static const int kMaxValue = -(kMinValue + 1);
890
891 private:
892  DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
893};
894
895
896// Failure is used for reporting out of memory situations and
897// propagating exceptions through the runtime system.  Failure objects
898// are transient and cannot occur as part of the object graph.
899//
900// Failures are a single word, encoded as follows:
901// +-------------------------+---+--+--+
902// |.........unused..........|sss|tt|11|
903// +-------------------------+---+--+--+
904//                          7 6 4 32 10
905//
906//
907// The low two bits, 0-1, are the failure tag, 11.  The next two bits,
908// 2-3, are a failure type tag 'tt' with possible values:
909//   00 RETRY_AFTER_GC
910//   01 EXCEPTION
911//   10 INTERNAL_ERROR
912//   11 OUT_OF_MEMORY_EXCEPTION
913//
914// The next three bits, 4-6, are an allocation space tag 'sss'.  The
915// allocation space tag is 000 for all failure types except
916// RETRY_AFTER_GC.  For RETRY_AFTER_GC, the possible values are the
917// allocation spaces (the encoding is found in globals.h).
918
919// Failure type tag info.
920const int kFailureTypeTagSize = 2;
921const int kFailureTypeTagMask = (1 << kFailureTypeTagSize) - 1;
922
923class Failure: public MaybeObject {
924 public:
925  // RuntimeStubs assumes EXCEPTION = 1 in the compiler-generated code.
926  enum Type {
927    RETRY_AFTER_GC = 0,
928    EXCEPTION = 1,       // Returning this marker tells the real exception
929                         // is in Isolate::pending_exception.
930    INTERNAL_ERROR = 2,
931    OUT_OF_MEMORY_EXCEPTION = 3
932  };
933
934  inline Type type() const;
935
936  // Returns the space that needs to be collected for RetryAfterGC failures.
937  inline AllocationSpace allocation_space() const;
938
939  inline bool IsInternalError() const;
940  inline bool IsOutOfMemoryException() const;
941
942  static inline Failure* RetryAfterGC(AllocationSpace space);
943  static inline Failure* RetryAfterGC();  // NEW_SPACE
944  static inline Failure* Exception();
945  static inline Failure* InternalError();
946  static inline Failure* OutOfMemoryException();
947  // Casting.
948  static inline Failure* cast(MaybeObject* object);
949
950  // Dispatched behavior.
951  inline void FailurePrint() {
952    FailurePrint(stdout);
953  }
954  void FailurePrint(FILE* out);
955  void FailurePrint(StringStream* accumulator);
956#ifdef DEBUG
957  void FailureVerify();
958#endif
959
960 private:
961  inline intptr_t value() const;
962  static inline Failure* Construct(Type type, intptr_t value = 0);
963
964  DISALLOW_IMPLICIT_CONSTRUCTORS(Failure);
965};
966
967
968// Heap objects typically have a map pointer in their first word.  However,
969// during GC other data (eg, mark bits, forwarding addresses) is sometimes
970// encoded in the first word.  The class MapWord is an abstraction of the
971// value in a heap object's first word.
972class MapWord BASE_EMBEDDED {
973 public:
974  // Normal state: the map word contains a map pointer.
975
976  // Create a map word from a map pointer.
977  static inline MapWord FromMap(Map* map);
978
979  // View this map word as a map pointer.
980  inline Map* ToMap();
981
982
983  // Scavenge collection: the map word of live objects in the from space
984  // contains a forwarding address (a heap object pointer in the to space).
985
986  // True if this map word is a forwarding address for a scavenge
987  // collection.  Only valid during a scavenge collection (specifically,
988  // when all map words are heap object pointers, ie. not during a full GC).
989  inline bool IsForwardingAddress();
990
991  // Create a map word from a forwarding address.
992  static inline MapWord FromForwardingAddress(HeapObject* object);
993
994  // View this map word as a forwarding address.
995  inline HeapObject* ToForwardingAddress();
996
997  // Marking phase of full collection: the map word of live objects is
998  // marked, and may be marked as overflowed (eg, the object is live, its
999  // children have not been visited, and it does not fit in the marking
1000  // stack).
1001
1002  // True if this map word's mark bit is set.
1003  inline bool IsMarked();
1004
1005  // Return this map word but with its mark bit set.
1006  inline void SetMark();
1007
1008  // Return this map word but with its mark bit cleared.
1009  inline void ClearMark();
1010
1011  // True if this map word's overflow bit is set.
1012  inline bool IsOverflowed();
1013
1014  // Return this map word but with its overflow bit set.
1015  inline void SetOverflow();
1016
1017  // Return this map word but with its overflow bit cleared.
1018  inline void ClearOverflow();
1019
1020
1021  // Compacting phase of a full compacting collection: the map word of live
1022  // objects contains an encoding of the original map address along with the
1023  // forwarding address (represented as an offset from the first live object
1024  // in the same page as the (old) object address).
1025
1026  // Create a map word from a map address and a forwarding address offset.
1027  static inline MapWord EncodeAddress(Address map_address, int offset);
1028
1029  // Return the map address encoded in this map word.
1030  inline Address DecodeMapAddress(MapSpace* map_space);
1031
1032  // Return the forwarding offset encoded in this map word.
1033  inline int DecodeOffset();
1034
1035
1036  // During serialization: the map word is used to hold an encoded
1037  // address, and possibly a mark bit (set and cleared with SetMark
1038  // and ClearMark).
1039
1040  // Create a map word from an encoded address.
1041  static inline MapWord FromEncodedAddress(Address address);
1042
1043  inline Address ToEncodedAddress();
1044
1045  // Bits used by the marking phase of the garbage collector.
1046  //
1047  // The first word of a heap object is normally a map pointer. The last two
1048  // bits are tagged as '01' (kHeapObjectTag). We reuse the last two bits to
1049  // mark an object as live and/or overflowed:
1050  //   last bit = 0, marked as alive
1051  //   second bit = 1, overflowed
1052  // An object is only marked as overflowed when it is marked as live while
1053  // the marking stack is overflowed.
1054  static const int kMarkingBit = 0;  // marking bit
1055  static const int kMarkingMask = (1 << kMarkingBit);  // marking mask
1056  static const int kOverflowBit = 1;  // overflow bit
1057  static const int kOverflowMask = (1 << kOverflowBit);  // overflow mask
1058
1059  // Forwarding pointers and map pointer encoding. On 32 bit all the bits are
1060  // used.
1061  // +-----------------+------------------+-----------------+
1062  // |forwarding offset|page offset of map|page index of map|
1063  // +-----------------+------------------+-----------------+
1064  //          ^                 ^                  ^
1065  //          |                 |                  |
1066  //          |                 |          kMapPageIndexBits
1067  //          |         kMapPageOffsetBits
1068  // kForwardingOffsetBits
1069  static const int kMapPageOffsetBits = kPageSizeBits - kMapAlignmentBits;
1070  static const int kForwardingOffsetBits = kPageSizeBits - kObjectAlignmentBits;
1071#ifdef V8_HOST_ARCH_64_BIT
1072  static const int kMapPageIndexBits = 16;
1073#else
1074  // Use all the 32-bits to encode on a 32-bit platform.
1075  static const int kMapPageIndexBits =
1076      32 - (kMapPageOffsetBits + kForwardingOffsetBits);
1077#endif
1078
1079  static const int kMapPageIndexShift = 0;
1080  static const int kMapPageOffsetShift =
1081      kMapPageIndexShift + kMapPageIndexBits;
1082  static const int kForwardingOffsetShift =
1083      kMapPageOffsetShift + kMapPageOffsetBits;
1084
1085  // Bit masks covering the different parts the encoding.
1086  static const uintptr_t kMapPageIndexMask =
1087      (1 << kMapPageOffsetShift) - 1;
1088  static const uintptr_t kMapPageOffsetMask =
1089      ((1 << kForwardingOffsetShift) - 1) & ~kMapPageIndexMask;
1090  static const uintptr_t kForwardingOffsetMask =
1091      ~(kMapPageIndexMask | kMapPageOffsetMask);
1092
1093 private:
1094  // HeapObject calls the private constructor and directly reads the value.
1095  friend class HeapObject;
1096
1097  explicit MapWord(uintptr_t value) : value_(value) {}
1098
1099  uintptr_t value_;
1100};
1101
1102
1103// HeapObject is the superclass for all classes describing heap allocated
1104// objects.
1105class HeapObject: public Object {
1106 public:
1107  // [map]: Contains a map which contains the object's reflective
1108  // information.
1109  inline Map* map();
1110  inline void set_map(Map* value);
1111
1112  // During garbage collection, the map word of a heap object does not
1113  // necessarily contain a map pointer.
1114  inline MapWord map_word();
1115  inline void set_map_word(MapWord map_word);
1116
1117  // The Heap the object was allocated in. Used also to access Isolate.
1118  // This method can not be used during GC, it ASSERTs this.
1119  inline Heap* GetHeap();
1120  // Convenience method to get current isolate. This method can be
1121  // accessed only when its result is the same as
1122  // Isolate::Current(), it ASSERTs this. See also comment for GetHeap.
1123  inline Isolate* GetIsolate();
1124
1125  // Converts an address to a HeapObject pointer.
1126  static inline HeapObject* FromAddress(Address address);
1127
1128  // Returns the address of this HeapObject.
1129  inline Address address();
1130
1131  // Iterates over pointers contained in the object (including the Map)
1132  void Iterate(ObjectVisitor* v);
1133
1134  // Iterates over all pointers contained in the object except the
1135  // first map pointer.  The object type is given in the first
1136  // parameter. This function does not access the map pointer in the
1137  // object, and so is safe to call while the map pointer is modified.
1138  void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1139
1140  // Returns the heap object's size in bytes
1141  inline int Size();
1142
1143  // Given a heap object's map pointer, returns the heap size in bytes
1144  // Useful when the map pointer field is used for other purposes.
1145  // GC internal.
1146  inline int SizeFromMap(Map* map);
1147
1148  // Support for the marking heap objects during the marking phase of GC.
1149  // True if the object is marked live.
1150  inline bool IsMarked();
1151
1152  // Mutate this object's map pointer to indicate that the object is live.
1153  inline void SetMark();
1154
1155  // Mutate this object's map pointer to remove the indication that the
1156  // object is live (ie, partially restore the map pointer).
1157  inline void ClearMark();
1158
1159  // True if this object is marked as overflowed.  Overflowed objects have
1160  // been reached and marked during marking of the heap, but their children
1161  // have not necessarily been marked and they have not been pushed on the
1162  // marking stack.
1163  inline bool IsOverflowed();
1164
1165  // Mutate this object's map pointer to indicate that the object is
1166  // overflowed.
1167  inline void SetOverflow();
1168
1169  // Mutate this object's map pointer to remove the indication that the
1170  // object is overflowed (ie, partially restore the map pointer).
1171  inline void ClearOverflow();
1172
1173  // Returns the field at offset in obj, as a read/write Object* reference.
1174  // Does no checking, and is safe to use during GC, while maps are invalid.
1175  // Does not invoke write barrier, so should only be assigned to
1176  // during marking GC.
1177  static inline Object** RawField(HeapObject* obj, int offset);
1178
1179  // Casting.
1180  static inline HeapObject* cast(Object* obj);
1181
1182  // Return the write barrier mode for this. Callers of this function
1183  // must be able to present a reference to an AssertNoAllocation
1184  // object as a sign that they are not going to use this function
1185  // from code that allocates and thus invalidates the returned write
1186  // barrier mode.
1187  inline WriteBarrierMode GetWriteBarrierMode(const AssertNoAllocation&);
1188
1189  // Dispatched behavior.
1190  void HeapObjectShortPrint(StringStream* accumulator);
1191#ifdef OBJECT_PRINT
1192  inline void HeapObjectPrint() {
1193    HeapObjectPrint(stdout);
1194  }
1195  void HeapObjectPrint(FILE* out);
1196#endif
1197#ifdef DEBUG
1198  void HeapObjectVerify();
1199  inline void VerifyObjectField(int offset);
1200  inline void VerifySmiField(int offset);
1201#endif
1202
1203#ifdef OBJECT_PRINT
1204  void PrintHeader(FILE* out, const char* id);
1205#endif
1206
1207#ifdef DEBUG
1208  // Verify a pointer is a valid HeapObject pointer that points to object
1209  // areas in the heap.
1210  static void VerifyHeapPointer(Object* p);
1211#endif
1212
1213  // Layout description.
1214  // First field in a heap object is map.
1215  static const int kMapOffset = Object::kHeaderSize;
1216  static const int kHeaderSize = kMapOffset + kPointerSize;
1217
1218  STATIC_CHECK(kMapOffset == Internals::kHeapObjectMapOffset);
1219
1220 protected:
1221  // helpers for calling an ObjectVisitor to iterate over pointers in the
1222  // half-open range [start, end) specified as integer offsets
1223  inline void IteratePointers(ObjectVisitor* v, int start, int end);
1224  // as above, for the single element at "offset"
1225  inline void IteratePointer(ObjectVisitor* v, int offset);
1226
1227 private:
1228  DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1229};
1230
1231
1232#define SLOT_ADDR(obj, offset) \
1233  reinterpret_cast<Object**>((obj)->address() + offset)
1234
1235// This class describes a body of an object of a fixed size
1236// in which all pointer fields are located in the [start_offset, end_offset)
1237// interval.
1238template<int start_offset, int end_offset, int size>
1239class FixedBodyDescriptor {
1240 public:
1241  static const int kStartOffset = start_offset;
1242  static const int kEndOffset = end_offset;
1243  static const int kSize = size;
1244
1245  static inline void IterateBody(HeapObject* obj, ObjectVisitor* v);
1246
1247  template<typename StaticVisitor>
1248  static inline void IterateBody(HeapObject* obj) {
1249    StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1250                                 SLOT_ADDR(obj, end_offset));
1251  }
1252};
1253
1254
1255// This class describes a body of an object of a variable size
1256// in which all pointer fields are located in the [start_offset, object_size)
1257// interval.
1258template<int start_offset>
1259class FlexibleBodyDescriptor {
1260 public:
1261  static const int kStartOffset = start_offset;
1262
1263  static inline void IterateBody(HeapObject* obj,
1264                                 int object_size,
1265                                 ObjectVisitor* v);
1266
1267  template<typename StaticVisitor>
1268  static inline void IterateBody(HeapObject* obj, int object_size) {
1269    StaticVisitor::VisitPointers(SLOT_ADDR(obj, start_offset),
1270                                 SLOT_ADDR(obj, object_size));
1271  }
1272};
1273
1274#undef SLOT_ADDR
1275
1276
1277// The HeapNumber class describes heap allocated numbers that cannot be
1278// represented in a Smi (small integer)
1279class HeapNumber: public HeapObject {
1280 public:
1281  // [value]: number value.
1282  inline double value();
1283  inline void set_value(double value);
1284
1285  // Casting.
1286  static inline HeapNumber* cast(Object* obj);
1287
1288  // Dispatched behavior.
1289  Object* HeapNumberToBoolean();
1290  inline void HeapNumberPrint() {
1291    HeapNumberPrint(stdout);
1292  }
1293  void HeapNumberPrint(FILE* out);
1294  void HeapNumberPrint(StringStream* accumulator);
1295#ifdef DEBUG
1296  void HeapNumberVerify();
1297#endif
1298
1299  inline int get_exponent();
1300  inline int get_sign();
1301
1302  // Layout description.
1303  static const int kValueOffset = HeapObject::kHeaderSize;
1304  // IEEE doubles are two 32 bit words.  The first is just mantissa, the second
1305  // is a mixture of sign, exponent and mantissa.  Our current platforms are all
1306  // little endian apart from non-EABI arm which is little endian with big
1307  // endian floating point word ordering!
1308  static const int kMantissaOffset = kValueOffset;
1309  static const int kExponentOffset = kValueOffset + 4;
1310
1311  static const int kSize = kValueOffset + kDoubleSize;
1312  static const uint32_t kSignMask = 0x80000000u;
1313  static const uint32_t kExponentMask = 0x7ff00000u;
1314  static const uint32_t kMantissaMask = 0xfffffu;
1315  static const int kMantissaBits = 52;
1316  static const int kExponentBits = 11;
1317  static const int kExponentBias = 1023;
1318  static const int kExponentShift = 20;
1319  static const int kMantissaBitsInTopWord = 20;
1320  static const int kNonMantissaBitsInTopWord = 12;
1321
1322 private:
1323  DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1324};
1325
1326
1327// The JSObject describes real heap allocated JavaScript objects with
1328// properties.
1329// Note that the map of JSObject changes during execution to enable inline
1330// caching.
1331class JSObject: public HeapObject {
1332 public:
1333  enum DeleteMode {
1334    NORMAL_DELETION,
1335    STRICT_DELETION,
1336    FORCE_DELETION
1337  };
1338
1339  enum ElementsKind {
1340    // The only "fast" kind.
1341    FAST_ELEMENTS,
1342    // All the kinds below are "slow".
1343    DICTIONARY_ELEMENTS,
1344    EXTERNAL_BYTE_ELEMENTS,
1345    EXTERNAL_UNSIGNED_BYTE_ELEMENTS,
1346    EXTERNAL_SHORT_ELEMENTS,
1347    EXTERNAL_UNSIGNED_SHORT_ELEMENTS,
1348    EXTERNAL_INT_ELEMENTS,
1349    EXTERNAL_UNSIGNED_INT_ELEMENTS,
1350    EXTERNAL_FLOAT_ELEMENTS,
1351    EXTERNAL_PIXEL_ELEMENTS
1352  };
1353
1354  // [properties]: Backing storage for properties.
1355  // properties is a FixedArray in the fast case and a Dictionary in the
1356  // slow case.
1357  DECL_ACCESSORS(properties, FixedArray)  // Get and set fast properties.
1358  inline void initialize_properties();
1359  inline bool HasFastProperties();
1360  inline StringDictionary* property_dictionary();  // Gets slow properties.
1361
1362  // [elements]: The elements (properties with names that are integers).
1363  //
1364  // Elements can be in two general modes: fast and slow. Each mode
1365  // corrensponds to a set of object representations of elements that
1366  // have something in common.
1367  //
1368  // In the fast mode elements is a FixedArray and so each element can
1369  // be quickly accessed. This fact is used in the generated code. The
1370  // elements array can have one of the two maps in this mode:
1371  // fixed_array_map or fixed_cow_array_map (for copy-on-write
1372  // arrays). In the latter case the elements array may be shared by a
1373  // few objects and so before writing to any element the array must
1374  // be copied. Use EnsureWritableFastElements in this case.
1375  //
1376  // In the slow mode elements is either a NumberDictionary or an ExternalArray.
1377  DECL_ACCESSORS(elements, HeapObject)
1378  inline void initialize_elements();
1379  MUST_USE_RESULT inline MaybeObject* ResetElements();
1380  inline ElementsKind GetElementsKind();
1381  inline bool HasFastElements();
1382  inline bool HasDictionaryElements();
1383  inline bool HasExternalPixelElements();
1384  inline bool HasExternalArrayElements();
1385  inline bool HasExternalByteElements();
1386  inline bool HasExternalUnsignedByteElements();
1387  inline bool HasExternalShortElements();
1388  inline bool HasExternalUnsignedShortElements();
1389  inline bool HasExternalIntElements();
1390  inline bool HasExternalUnsignedIntElements();
1391  inline bool HasExternalFloatElements();
1392  inline bool AllowsSetElementsLength();
1393  inline NumberDictionary* element_dictionary();  // Gets slow elements.
1394  // Requires: this->HasFastElements().
1395  MUST_USE_RESULT inline MaybeObject* EnsureWritableFastElements();
1396
1397  // Collects elements starting at index 0.
1398  // Undefined values are placed after non-undefined values.
1399  // Returns the number of non-undefined values.
1400  MUST_USE_RESULT MaybeObject* PrepareElementsForSort(uint32_t limit);
1401  // As PrepareElementsForSort, but only on objects where elements is
1402  // a dictionary, and it will stay a dictionary.
1403  MUST_USE_RESULT MaybeObject* PrepareSlowElementsForSort(uint32_t limit);
1404
1405  MUST_USE_RESULT MaybeObject* SetProperty(String* key,
1406                                           Object* value,
1407                                           PropertyAttributes attributes,
1408                                           StrictModeFlag strict_mode);
1409  MUST_USE_RESULT MaybeObject* SetProperty(LookupResult* result,
1410                                           String* key,
1411                                           Object* value,
1412                                           PropertyAttributes attributes,
1413                                           StrictModeFlag strict_mode);
1414  MUST_USE_RESULT MaybeObject* SetPropertyWithFailedAccessCheck(
1415      LookupResult* result,
1416      String* name,
1417      Object* value,
1418      bool check_prototype);
1419  MUST_USE_RESULT MaybeObject* SetPropertyWithCallback(Object* structure,
1420                                                       String* name,
1421                                                       Object* value,
1422                                                       JSObject* holder);
1423  MUST_USE_RESULT MaybeObject* SetPropertyWithDefinedSetter(JSFunction* setter,
1424                                                            Object* value);
1425  MUST_USE_RESULT MaybeObject* SetPropertyWithInterceptor(
1426      String* name,
1427      Object* value,
1428      PropertyAttributes attributes,
1429      StrictModeFlag strict_mode);
1430  MUST_USE_RESULT MaybeObject* SetPropertyPostInterceptor(
1431      String* name,
1432      Object* value,
1433      PropertyAttributes attributes,
1434      StrictModeFlag strict_mode);
1435  MUST_USE_RESULT MaybeObject* SetLocalPropertyIgnoreAttributes(
1436      String* key,
1437      Object* value,
1438      PropertyAttributes attributes);
1439
1440  // Retrieve a value in a normalized object given a lookup result.
1441  // Handles the special representation of JS global objects.
1442  Object* GetNormalizedProperty(LookupResult* result);
1443
1444  // Sets the property value in a normalized object given a lookup result.
1445  // Handles the special representation of JS global objects.
1446  Object* SetNormalizedProperty(LookupResult* result, Object* value);
1447
1448  // Sets the property value in a normalized object given (key, value, details).
1449  // Handles the special representation of JS global objects.
1450  MUST_USE_RESULT MaybeObject* SetNormalizedProperty(String* name,
1451                                                     Object* value,
1452                                                     PropertyDetails details);
1453
1454  // Deletes the named property in a normalized object.
1455  MUST_USE_RESULT MaybeObject* DeleteNormalizedProperty(String* name,
1456                                                        DeleteMode mode);
1457
1458  // Returns the class name ([[Class]] property in the specification).
1459  String* class_name();
1460
1461  // Returns the constructor name (the name (possibly, inferred name) of the
1462  // function that was used to instantiate the object).
1463  String* constructor_name();
1464
1465  // Retrieve interceptors.
1466  InterceptorInfo* GetNamedInterceptor();
1467  InterceptorInfo* GetIndexedInterceptor();
1468
1469  inline PropertyAttributes GetPropertyAttribute(String* name);
1470  PropertyAttributes GetPropertyAttributeWithReceiver(JSObject* receiver,
1471                                                      String* name);
1472  PropertyAttributes GetLocalPropertyAttribute(String* name);
1473
1474  MUST_USE_RESULT MaybeObject* DefineAccessor(String* name,
1475                                              bool is_getter,
1476                                              Object* fun,
1477                                              PropertyAttributes attributes);
1478  Object* LookupAccessor(String* name, bool is_getter);
1479
1480  MUST_USE_RESULT MaybeObject* DefineAccessor(AccessorInfo* info);
1481
1482  // Used from Object::GetProperty().
1483  MaybeObject* GetPropertyWithFailedAccessCheck(
1484      Object* receiver,
1485      LookupResult* result,
1486      String* name,
1487      PropertyAttributes* attributes);
1488  MaybeObject* GetPropertyWithInterceptor(
1489      JSObject* receiver,
1490      String* name,
1491      PropertyAttributes* attributes);
1492  MaybeObject* GetPropertyPostInterceptor(
1493      JSObject* receiver,
1494      String* name,
1495      PropertyAttributes* attributes);
1496  MaybeObject* GetLocalPropertyPostInterceptor(JSObject* receiver,
1497                                               String* name,
1498                                               PropertyAttributes* attributes);
1499
1500  // Returns true if this is an instance of an api function and has
1501  // been modified since it was created.  May give false positives.
1502  bool IsDirty();
1503
1504  bool HasProperty(String* name) {
1505    return GetPropertyAttribute(name) != ABSENT;
1506  }
1507
1508  // Can cause a GC if it hits an interceptor.
1509  bool HasLocalProperty(String* name) {
1510    return GetLocalPropertyAttribute(name) != ABSENT;
1511  }
1512
1513  // If the receiver is a JSGlobalProxy this method will return its prototype,
1514  // otherwise the result is the receiver itself.
1515  inline Object* BypassGlobalProxy();
1516
1517  // Accessors for hidden properties object.
1518  //
1519  // Hidden properties are not local properties of the object itself.
1520  // Instead they are stored on an auxiliary JSObject stored as a local
1521  // property with a special name Heap::hidden_symbol(). But if the
1522  // receiver is a JSGlobalProxy then the auxiliary object is a property
1523  // of its prototype.
1524  //
1525  // Has/Get/SetHiddenPropertiesObject methods don't allow the holder to be
1526  // a JSGlobalProxy. Use BypassGlobalProxy method above to get to the real
1527  // holder.
1528  //
1529  // These accessors do not touch interceptors or accessors.
1530  inline bool HasHiddenPropertiesObject();
1531  inline Object* GetHiddenPropertiesObject();
1532  MUST_USE_RESULT inline MaybeObject* SetHiddenPropertiesObject(
1533      Object* hidden_obj);
1534
1535  MUST_USE_RESULT MaybeObject* DeleteProperty(String* name, DeleteMode mode);
1536  MUST_USE_RESULT MaybeObject* DeleteElement(uint32_t index, DeleteMode mode);
1537
1538  // Tests for the fast common case for property enumeration.
1539  bool IsSimpleEnum();
1540
1541  // Do we want to keep the elements in fast case when increasing the
1542  // capacity?
1543  bool ShouldConvertToSlowElements(int new_capacity);
1544  // Returns true if the backing storage for the slow-case elements of
1545  // this object takes up nearly as much space as a fast-case backing
1546  // storage would.  In that case the JSObject should have fast
1547  // elements.
1548  bool ShouldConvertToFastElements();
1549
1550  // Return the object's prototype (might be Heap::null_value()).
1551  inline Object* GetPrototype();
1552
1553  // Set the object's prototype (only JSObject and null are allowed).
1554  MUST_USE_RESULT MaybeObject* SetPrototype(Object* value,
1555                                            bool skip_hidden_prototypes);
1556
1557  // Tells whether the index'th element is present.
1558  inline bool HasElement(uint32_t index);
1559  bool HasElementWithReceiver(JSObject* receiver, uint32_t index);
1560
1561  // Computes the new capacity when expanding the elements of a JSObject.
1562  static int NewElementsCapacity(int old_capacity) {
1563    // (old_capacity + 50%) + 16
1564    return old_capacity + (old_capacity >> 1) + 16;
1565  }
1566
1567  // Tells whether the index'th element is present and how it is stored.
1568  enum LocalElementType {
1569    // There is no element with given index.
1570    UNDEFINED_ELEMENT,
1571
1572    // Element with given index is handled by interceptor.
1573    INTERCEPTED_ELEMENT,
1574
1575    // Element with given index is character in string.
1576    STRING_CHARACTER_ELEMENT,
1577
1578    // Element with given index is stored in fast backing store.
1579    FAST_ELEMENT,
1580
1581    // Element with given index is stored in slow backing store.
1582    DICTIONARY_ELEMENT
1583  };
1584
1585  LocalElementType HasLocalElement(uint32_t index);
1586
1587  bool HasElementWithInterceptor(JSObject* receiver, uint32_t index);
1588  bool HasElementPostInterceptor(JSObject* receiver, uint32_t index);
1589
1590  MUST_USE_RESULT MaybeObject* SetFastElement(uint32_t index,
1591                                              Object* value,
1592                                              StrictModeFlag strict_mode,
1593                                              bool check_prototype = true);
1594
1595  // Set the index'th array element.
1596  // A Failure object is returned if GC is needed.
1597  MUST_USE_RESULT MaybeObject* SetElement(uint32_t index,
1598                                          Object* value,
1599                                          StrictModeFlag strict_mode,
1600                                          bool check_prototype = true);
1601
1602  // Returns the index'th element.
1603  // The undefined object if index is out of bounds.
1604  MaybeObject* GetElementWithReceiver(Object* receiver, uint32_t index);
1605  MaybeObject* GetElementWithInterceptor(Object* receiver, uint32_t index);
1606
1607  // Get external element value at index if there is one and undefined
1608  // otherwise. Can return a failure if allocation of a heap number
1609  // failed.
1610  MaybeObject* GetExternalElement(uint32_t index);
1611
1612  MUST_USE_RESULT MaybeObject* SetFastElementsCapacityAndLength(int capacity,
1613                                                                int length);
1614  MUST_USE_RESULT MaybeObject* SetSlowElements(Object* length);
1615
1616  // Lookup interceptors are used for handling properties controlled by host
1617  // objects.
1618  inline bool HasNamedInterceptor();
1619  inline bool HasIndexedInterceptor();
1620
1621  // Support functions for v8 api (needed for correct interceptor behavior).
1622  bool HasRealNamedProperty(String* key);
1623  bool HasRealElementProperty(uint32_t index);
1624  bool HasRealNamedCallbackProperty(String* key);
1625
1626  // Initializes the array to a certain length
1627  MUST_USE_RESULT MaybeObject* SetElementsLength(Object* length);
1628
1629  // Get the header size for a JSObject.  Used to compute the index of
1630  // internal fields as well as the number of internal fields.
1631  inline int GetHeaderSize();
1632
1633  inline int GetInternalFieldCount();
1634  inline int GetInternalFieldOffset(int index);
1635  inline Object* GetInternalField(int index);
1636  inline void SetInternalField(int index, Object* value);
1637
1638  // Lookup a property.  If found, the result is valid and has
1639  // detailed information.
1640  void LocalLookup(String* name, LookupResult* result);
1641  void Lookup(String* name, LookupResult* result);
1642
1643  // The following lookup functions skip interceptors.
1644  void LocalLookupRealNamedProperty(String* name, LookupResult* result);
1645  void LookupRealNamedProperty(String* name, LookupResult* result);
1646  void LookupRealNamedPropertyInPrototypes(String* name, LookupResult* result);
1647  void LookupCallbackSetterInPrototypes(String* name, LookupResult* result);
1648  MUST_USE_RESULT MaybeObject* SetElementWithCallbackSetterInPrototypes(
1649      uint32_t index, Object* value, bool* found);
1650  void LookupCallback(String* name, LookupResult* result);
1651
1652  // Returns the number of properties on this object filtering out properties
1653  // with the specified attributes (ignoring interceptors).
1654  int NumberOfLocalProperties(PropertyAttributes filter);
1655  // Returns the number of enumerable properties (ignoring interceptors).
1656  int NumberOfEnumProperties();
1657  // Fill in details for properties into storage starting at the specified
1658  // index.
1659  void GetLocalPropertyNames(FixedArray* storage, int index);
1660
1661  // Returns the number of properties on this object filtering out properties
1662  // with the specified attributes (ignoring interceptors).
1663  int NumberOfLocalElements(PropertyAttributes filter);
1664  // Returns the number of enumerable elements (ignoring interceptors).
1665  int NumberOfEnumElements();
1666  // Returns the number of elements on this object filtering out elements
1667  // with the specified attributes (ignoring interceptors).
1668  int GetLocalElementKeys(FixedArray* storage, PropertyAttributes filter);
1669  // Count and fill in the enumerable elements into storage.
1670  // (storage->length() == NumberOfEnumElements()).
1671  // If storage is NULL, will count the elements without adding
1672  // them to any storage.
1673  // Returns the number of enumerable elements.
1674  int GetEnumElementKeys(FixedArray* storage);
1675
1676  // Add a property to a fast-case object using a map transition to
1677  // new_map.
1678  MUST_USE_RESULT MaybeObject* AddFastPropertyUsingMap(Map* new_map,
1679                                                       String* name,
1680                                                       Object* value);
1681
1682  // Add a constant function property to a fast-case object.
1683  // This leaves a CONSTANT_TRANSITION in the old map, and
1684  // if it is called on a second object with this map, a
1685  // normal property is added instead, with a map transition.
1686  // This avoids the creation of many maps with the same constant
1687  // function, all orphaned.
1688  MUST_USE_RESULT MaybeObject* AddConstantFunctionProperty(
1689      String* name,
1690      JSFunction* function,
1691      PropertyAttributes attributes);
1692
1693  MUST_USE_RESULT MaybeObject* ReplaceSlowProperty(
1694      String* name,
1695      Object* value,
1696      PropertyAttributes attributes);
1697
1698  // Converts a descriptor of any other type to a real field,
1699  // backed by the properties array.  Descriptors of visible
1700  // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1701  // Converts the descriptor on the original object's map to a
1702  // map transition, and the the new field is on the object's new map.
1703  MUST_USE_RESULT MaybeObject* ConvertDescriptorToFieldAndMapTransition(
1704      String* name,
1705      Object* new_value,
1706      PropertyAttributes attributes);
1707
1708  // Converts a descriptor of any other type to a real field,
1709  // backed by the properties array.  Descriptors of visible
1710  // types, such as CONSTANT_FUNCTION, keep their enumeration order.
1711  MUST_USE_RESULT MaybeObject* ConvertDescriptorToField(
1712      String* name,
1713      Object* new_value,
1714      PropertyAttributes attributes);
1715
1716  // Add a property to a fast-case object.
1717  MUST_USE_RESULT MaybeObject* AddFastProperty(String* name,
1718                                               Object* value,
1719                                               PropertyAttributes attributes);
1720
1721  // Add a property to a slow-case object.
1722  MUST_USE_RESULT MaybeObject* AddSlowProperty(String* name,
1723                                               Object* value,
1724                                               PropertyAttributes attributes);
1725
1726  // Add a property to an object.
1727  MUST_USE_RESULT MaybeObject* AddProperty(String* name,
1728                                           Object* value,
1729                                           PropertyAttributes attributes,
1730                                           StrictModeFlag strict_mode);
1731
1732  // Convert the object to use the canonical dictionary
1733  // representation. If the object is expected to have additional properties
1734  // added this number can be indicated to have the backing store allocated to
1735  // an initial capacity for holding these properties.
1736  MUST_USE_RESULT MaybeObject* NormalizeProperties(
1737      PropertyNormalizationMode mode,
1738      int expected_additional_properties);
1739  MUST_USE_RESULT MaybeObject* NormalizeElements();
1740
1741  MUST_USE_RESULT MaybeObject* UpdateMapCodeCache(String* name, Code* code);
1742
1743  // Transform slow named properties to fast variants.
1744  // Returns failure if allocation failed.
1745  MUST_USE_RESULT MaybeObject* TransformToFastProperties(
1746      int unused_property_fields);
1747
1748  // Access fast-case object properties at index.
1749  inline Object* FastPropertyAt(int index);
1750  inline Object* FastPropertyAtPut(int index, Object* value);
1751
1752  // Access to in object properties.
1753  inline int GetInObjectPropertyOffset(int index);
1754  inline Object* InObjectPropertyAt(int index);
1755  inline Object* InObjectPropertyAtPut(int index,
1756                                       Object* value,
1757                                       WriteBarrierMode mode
1758                                       = UPDATE_WRITE_BARRIER);
1759
1760  // initializes the body after properties slot, properties slot is
1761  // initialized by set_properties
1762  // Note: this call does not update write barrier, it is caller's
1763  // reponsibility to ensure that *v* can be collected without WB here.
1764  inline void InitializeBody(int object_size, Object* value);
1765
1766  // Check whether this object references another object
1767  bool ReferencesObject(Object* obj);
1768
1769  // Casting.
1770  static inline JSObject* cast(Object* obj);
1771
1772  // Disalow further properties to be added to the object.
1773  MUST_USE_RESULT MaybeObject* PreventExtensions();
1774
1775
1776  // Dispatched behavior.
1777  void JSObjectShortPrint(StringStream* accumulator);
1778#ifdef OBJECT_PRINT
1779  inline void JSObjectPrint() {
1780    JSObjectPrint(stdout);
1781  }
1782  void JSObjectPrint(FILE* out);
1783#endif
1784#ifdef DEBUG
1785  void JSObjectVerify();
1786#endif
1787#ifdef OBJECT_PRINT
1788  inline void PrintProperties() {
1789    PrintProperties(stdout);
1790  }
1791  void PrintProperties(FILE* out);
1792
1793  inline void PrintElements() {
1794    PrintElements(stdout);
1795  }
1796  void PrintElements(FILE* out);
1797#endif
1798
1799#ifdef DEBUG
1800  // Structure for collecting spill information about JSObjects.
1801  class SpillInformation {
1802   public:
1803    void Clear();
1804    void Print();
1805    int number_of_objects_;
1806    int number_of_objects_with_fast_properties_;
1807    int number_of_objects_with_fast_elements_;
1808    int number_of_fast_used_fields_;
1809    int number_of_fast_unused_fields_;
1810    int number_of_slow_used_properties_;
1811    int number_of_slow_unused_properties_;
1812    int number_of_fast_used_elements_;
1813    int number_of_fast_unused_elements_;
1814    int number_of_slow_used_elements_;
1815    int number_of_slow_unused_elements_;
1816  };
1817
1818  void IncrementSpillStatistics(SpillInformation* info);
1819#endif
1820  Object* SlowReverseLookup(Object* value);
1821
1822  // Maximal number of fast properties for the JSObject. Used to
1823  // restrict the number of map transitions to avoid an explosion in
1824  // the number of maps for objects used as dictionaries.
1825  inline int MaxFastProperties();
1826
1827  // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
1828  // Also maximal value of JSArray's length property.
1829  static const uint32_t kMaxElementCount = 0xffffffffu;
1830
1831  static const uint32_t kMaxGap = 1024;
1832  static const int kMaxFastElementsLength = 5000;
1833  static const int kInitialMaxFastElementArray = 100000;
1834  static const int kMaxFastProperties = 12;
1835  static const int kMaxInstanceSize = 255 * kPointerSize;
1836  // When extending the backing storage for property values, we increase
1837  // its size by more than the 1 entry necessary, so sequentially adding fields
1838  // to the same object requires fewer allocations and copies.
1839  static const int kFieldsAdded = 3;
1840
1841  // Layout description.
1842  static const int kPropertiesOffset = HeapObject::kHeaderSize;
1843  static const int kElementsOffset = kPropertiesOffset + kPointerSize;
1844  static const int kHeaderSize = kElementsOffset + kPointerSize;
1845
1846  STATIC_CHECK(kHeaderSize == Internals::kJSObjectHeaderSize);
1847
1848  class BodyDescriptor : public FlexibleBodyDescriptor<kPropertiesOffset> {
1849   public:
1850    static inline int SizeOf(Map* map, HeapObject* object);
1851  };
1852
1853 private:
1854  MUST_USE_RESULT MaybeObject* GetElementWithCallback(Object* receiver,
1855                                                      Object* structure,
1856                                                      uint32_t index,
1857                                                      Object* holder);
1858  MaybeObject* SetElementWithCallback(Object* structure,
1859                                      uint32_t index,
1860                                      Object* value,
1861                                      JSObject* holder);
1862  MUST_USE_RESULT MaybeObject* SetElementWithInterceptor(
1863      uint32_t index,
1864      Object* value,
1865      StrictModeFlag strict_mode,
1866      bool check_prototype);
1867  MUST_USE_RESULT MaybeObject* SetElementWithoutInterceptor(
1868      uint32_t index,
1869      Object* value,
1870      StrictModeFlag strict_mode,
1871      bool check_prototype);
1872
1873  MaybeObject* GetElementPostInterceptor(Object* receiver, uint32_t index);
1874
1875  MUST_USE_RESULT MaybeObject* DeletePropertyPostInterceptor(String* name,
1876                                                             DeleteMode mode);
1877  MUST_USE_RESULT MaybeObject* DeletePropertyWithInterceptor(String* name);
1878
1879  MUST_USE_RESULT MaybeObject* DeleteElementPostInterceptor(uint32_t index,
1880                                                            DeleteMode mode);
1881  MUST_USE_RESULT MaybeObject* DeleteElementWithInterceptor(uint32_t index);
1882
1883  PropertyAttributes GetPropertyAttributePostInterceptor(JSObject* receiver,
1884                                                         String* name,
1885                                                         bool continue_search);
1886  PropertyAttributes GetPropertyAttributeWithInterceptor(JSObject* receiver,
1887                                                         String* name,
1888                                                         bool continue_search);
1889  PropertyAttributes GetPropertyAttributeWithFailedAccessCheck(
1890      Object* receiver,
1891      LookupResult* result,
1892      String* name,
1893      bool continue_search);
1894  PropertyAttributes GetPropertyAttribute(JSObject* receiver,
1895                                          LookupResult* result,
1896                                          String* name,
1897                                          bool continue_search);
1898
1899  // Returns true if most of the elements backing storage is used.
1900  bool HasDenseElements();
1901
1902  bool CanSetCallback(String* name);
1903  MUST_USE_RESULT MaybeObject* SetElementCallback(
1904      uint32_t index,
1905      Object* structure,
1906      PropertyAttributes attributes);
1907  MUST_USE_RESULT MaybeObject* SetPropertyCallback(
1908      String* name,
1909      Object* structure,
1910      PropertyAttributes attributes);
1911  MUST_USE_RESULT MaybeObject* DefineGetterSetter(
1912      String* name,
1913      PropertyAttributes attributes);
1914
1915  void LookupInDescriptor(String* name, LookupResult* result);
1916
1917  DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
1918};
1919
1920
1921// FixedArray describes fixed-sized arrays with element type Object*.
1922class FixedArray: public HeapObject {
1923 public:
1924  // [length]: length of the array.
1925  inline int length();
1926  inline void set_length(int value);
1927
1928  // Setter and getter for elements.
1929  inline Object* get(int index);
1930  // Setter that uses write barrier.
1931  inline void set(int index, Object* value);
1932
1933  // Setter that doesn't need write barrier).
1934  inline void set(int index, Smi* value);
1935  // Setter with explicit barrier mode.
1936  inline void set(int index, Object* value, WriteBarrierMode mode);
1937
1938  // Setters for frequently used oddballs located in old space.
1939  inline void set_undefined(int index);
1940  // TODO(isolates): duplicate.
1941  inline void set_undefined(Heap* heap, int index);
1942  inline void set_null(int index);
1943  // TODO(isolates): duplicate.
1944  inline void set_null(Heap* heap, int index);
1945  inline void set_the_hole(int index);
1946
1947  // Setters with less debug checks for the GC to use.
1948  inline void set_unchecked(int index, Smi* value);
1949  inline void set_null_unchecked(Heap* heap, int index);
1950  inline void set_unchecked(Heap* heap, int index, Object* value,
1951                            WriteBarrierMode mode);
1952
1953  // Gives access to raw memory which stores the array's data.
1954  inline Object** data_start();
1955
1956  // Copy operations.
1957  MUST_USE_RESULT inline MaybeObject* Copy();
1958  MUST_USE_RESULT MaybeObject* CopySize(int new_length);
1959
1960  // Add the elements of a JSArray to this FixedArray.
1961  MUST_USE_RESULT MaybeObject* AddKeysFromJSArray(JSArray* array);
1962
1963  // Compute the union of this and other.
1964  MUST_USE_RESULT MaybeObject* UnionOfKeys(FixedArray* other);
1965
1966  // Copy a sub array from the receiver to dest.
1967  void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
1968
1969  // Garbage collection support.
1970  static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
1971
1972  // Code Generation support.
1973  static int OffsetOfElementAt(int index) { return SizeFor(index); }
1974
1975  // Casting.
1976  static inline FixedArray* cast(Object* obj);
1977
1978  // Layout description.
1979  // Length is smi tagged when it is stored.
1980  static const int kLengthOffset = HeapObject::kHeaderSize;
1981  static const int kHeaderSize = kLengthOffset + kPointerSize;
1982
1983  // Maximal allowed size, in bytes, of a single FixedArray.
1984  // Prevents overflowing size computations, as well as extreme memory
1985  // consumption.
1986  static const int kMaxSize = 512 * MB;
1987  // Maximally allowed length of a FixedArray.
1988  static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
1989
1990  // Dispatched behavior.
1991#ifdef OBJECT_PRINT
1992  inline void FixedArrayPrint() {
1993    FixedArrayPrint(stdout);
1994  }
1995  void FixedArrayPrint(FILE* out);
1996#endif
1997#ifdef DEBUG
1998  void FixedArrayVerify();
1999  // Checks if two FixedArrays have identical contents.
2000  bool IsEqualTo(FixedArray* other);
2001#endif
2002
2003  // Swap two elements in a pair of arrays.  If this array and the
2004  // numbers array are the same object, the elements are only swapped
2005  // once.
2006  void SwapPairs(FixedArray* numbers, int i, int j);
2007
2008  // Sort prefix of this array and the numbers array as pairs wrt. the
2009  // numbers.  If the numbers array and the this array are the same
2010  // object, the prefix of this array is sorted.
2011  void SortPairs(FixedArray* numbers, uint32_t len);
2012
2013  class BodyDescriptor : public FlexibleBodyDescriptor<kHeaderSize> {
2014   public:
2015    static inline int SizeOf(Map* map, HeapObject* object) {
2016      return SizeFor(reinterpret_cast<FixedArray*>(object)->length());
2017    }
2018  };
2019
2020 protected:
2021  // Set operation on FixedArray without using write barriers. Can
2022  // only be used for storing old space objects or smis.
2023  static inline void fast_set(FixedArray* array, int index, Object* value);
2024
2025 private:
2026  DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2027};
2028
2029
2030// DescriptorArrays are fixed arrays used to hold instance descriptors.
2031// The format of the these objects is:
2032//   [0]: point to a fixed array with (value, detail) pairs.
2033//   [1]: next enumeration index (Smi), or pointer to small fixed array:
2034//          [0]: next enumeration index (Smi)
2035//          [1]: pointer to fixed array with enum cache
2036//   [2]: first key
2037//   [length() - 1]: last key
2038//
2039class DescriptorArray: public FixedArray {
2040 public:
2041  // Is this the singleton empty_descriptor_array?
2042  inline bool IsEmpty();
2043
2044  // Returns the number of descriptors in the array.
2045  int number_of_descriptors() {
2046    ASSERT(length() > kFirstIndex || IsEmpty());
2047    int len = length();
2048    return len <= kFirstIndex ? 0 : len - kFirstIndex;
2049  }
2050
2051  int NextEnumerationIndex() {
2052    if (IsEmpty()) return PropertyDetails::kInitialIndex;
2053    Object* obj = get(kEnumerationIndexIndex);
2054    if (obj->IsSmi()) {
2055      return Smi::cast(obj)->value();
2056    } else {
2057      Object* index = FixedArray::cast(obj)->get(kEnumCacheBridgeEnumIndex);
2058      return Smi::cast(index)->value();
2059    }
2060  }
2061
2062  // Set next enumeration index and flush any enum cache.
2063  void SetNextEnumerationIndex(int value) {
2064    if (!IsEmpty()) {
2065      fast_set(this, kEnumerationIndexIndex, Smi::FromInt(value));
2066    }
2067  }
2068  bool HasEnumCache() {
2069    return !IsEmpty() && !get(kEnumerationIndexIndex)->IsSmi();
2070  }
2071
2072  Object* GetEnumCache() {
2073    ASSERT(HasEnumCache());
2074    FixedArray* bridge = FixedArray::cast(get(kEnumerationIndexIndex));
2075    return bridge->get(kEnumCacheBridgeCacheIndex);
2076  }
2077
2078  // Initialize or change the enum cache,
2079  // using the supplied storage for the small "bridge".
2080  void SetEnumCache(FixedArray* bridge_storage, FixedArray* new_cache);
2081
2082  // Accessors for fetching instance descriptor at descriptor number.
2083  inline String* GetKey(int descriptor_number);
2084  inline Object* GetValue(int descriptor_number);
2085  inline Smi* GetDetails(int descriptor_number);
2086  inline PropertyType GetType(int descriptor_number);
2087  inline int GetFieldIndex(int descriptor_number);
2088  inline JSFunction* GetConstantFunction(int descriptor_number);
2089  inline Object* GetCallbacksObject(int descriptor_number);
2090  inline AccessorDescriptor* GetCallbacks(int descriptor_number);
2091  inline bool IsProperty(int descriptor_number);
2092  inline bool IsTransition(int descriptor_number);
2093  inline bool IsNullDescriptor(int descriptor_number);
2094  inline bool IsDontEnum(int descriptor_number);
2095
2096  // Accessor for complete descriptor.
2097  inline void Get(int descriptor_number, Descriptor* desc);
2098  inline void Set(int descriptor_number, Descriptor* desc);
2099
2100  // Transfer complete descriptor from another descriptor array to
2101  // this one.
2102  inline void CopyFrom(int index, DescriptorArray* src, int src_index);
2103
2104  // Copy the descriptor array, insert a new descriptor and optionally
2105  // remove map transitions.  If the descriptor is already present, it is
2106  // replaced.  If a replaced descriptor is a real property (not a transition
2107  // or null), its enumeration index is kept as is.
2108  // If adding a real property, map transitions must be removed.  If adding
2109  // a transition, they must not be removed.  All null descriptors are removed.
2110  MUST_USE_RESULT MaybeObject* CopyInsert(Descriptor* descriptor,
2111                                          TransitionFlag transition_flag);
2112
2113  // Remove all transitions.  Return  a copy of the array with all transitions
2114  // removed, or a Failure object if the new array could not be allocated.
2115  MUST_USE_RESULT MaybeObject* RemoveTransitions();
2116
2117  // Sort the instance descriptors by the hash codes of their keys.
2118  // Does not check for duplicates.
2119  void SortUnchecked();
2120
2121  // Sort the instance descriptors by the hash codes of their keys.
2122  // Checks the result for duplicates.
2123  void Sort();
2124
2125  // Search the instance descriptors for given name.
2126  inline int Search(String* name);
2127
2128  // As the above, but uses DescriptorLookupCache and updates it when
2129  // necessary.
2130  inline int SearchWithCache(String* name);
2131
2132  // Tells whether the name is present int the array.
2133  bool Contains(String* name) { return kNotFound != Search(name); }
2134
2135  // Perform a binary search in the instance descriptors represented
2136  // by this fixed array.  low and high are descriptor indices.  If there
2137  // are three instance descriptors in this array it should be called
2138  // with low=0 and high=2.
2139  int BinarySearch(String* name, int low, int high);
2140
2141  // Perform a linear search in the instance descriptors represented
2142  // by this fixed array.  len is the number of descriptor indices that are
2143  // valid.  Does not require the descriptors to be sorted.
2144  int LinearSearch(String* name, int len);
2145
2146  // Allocates a DescriptorArray, but returns the singleton
2147  // empty descriptor array object if number_of_descriptors is 0.
2148  MUST_USE_RESULT static MaybeObject* Allocate(int number_of_descriptors);
2149
2150  // Casting.
2151  static inline DescriptorArray* cast(Object* obj);
2152
2153  // Constant for denoting key was not found.
2154  static const int kNotFound = -1;
2155
2156  static const int kContentArrayIndex = 0;
2157  static const int kEnumerationIndexIndex = 1;
2158  static const int kFirstIndex = 2;
2159
2160  // The length of the "bridge" to the enum cache.
2161  static const int kEnumCacheBridgeLength = 2;
2162  static const int kEnumCacheBridgeEnumIndex = 0;
2163  static const int kEnumCacheBridgeCacheIndex = 1;
2164
2165  // Layout description.
2166  static const int kContentArrayOffset = FixedArray::kHeaderSize;
2167  static const int kEnumerationIndexOffset = kContentArrayOffset + kPointerSize;
2168  static const int kFirstOffset = kEnumerationIndexOffset + kPointerSize;
2169
2170  // Layout description for the bridge array.
2171  static const int kEnumCacheBridgeEnumOffset = FixedArray::kHeaderSize;
2172  static const int kEnumCacheBridgeCacheOffset =
2173    kEnumCacheBridgeEnumOffset + kPointerSize;
2174
2175#ifdef OBJECT_PRINT
2176  // Print all the descriptors.
2177  inline void PrintDescriptors() {
2178    PrintDescriptors(stdout);
2179  }
2180  void PrintDescriptors(FILE* out);
2181#endif
2182
2183#ifdef DEBUG
2184  // Is the descriptor array sorted and without duplicates?
2185  bool IsSortedNoDuplicates();
2186
2187  // Are two DescriptorArrays equal?
2188  bool IsEqualTo(DescriptorArray* other);
2189#endif
2190
2191  // The maximum number of descriptors we want in a descriptor array (should
2192  // fit in a page).
2193  static const int kMaxNumberOfDescriptors = 1024 + 512;
2194
2195 private:
2196  // Conversion from descriptor number to array indices.
2197  static int ToKeyIndex(int descriptor_number) {
2198    return descriptor_number+kFirstIndex;
2199  }
2200
2201  static int ToDetailsIndex(int descriptor_number) {
2202    return (descriptor_number << 1) + 1;
2203  }
2204
2205  static int ToValueIndex(int descriptor_number) {
2206    return descriptor_number << 1;
2207  }
2208
2209  bool is_null_descriptor(int descriptor_number) {
2210    return PropertyDetails(GetDetails(descriptor_number)).type() ==
2211        NULL_DESCRIPTOR;
2212  }
2213  // Swap operation on FixedArray without using write barriers.
2214  static inline void fast_swap(FixedArray* array, int first, int second);
2215
2216  // Swap descriptor first and second.
2217  inline void Swap(int first, int second);
2218
2219  FixedArray* GetContentArray() {
2220    return FixedArray::cast(get(kContentArrayIndex));
2221  }
2222  DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
2223};
2224
2225
2226// HashTable is a subclass of FixedArray that implements a hash table
2227// that uses open addressing and quadratic probing.
2228//
2229// In order for the quadratic probing to work, elements that have not
2230// yet been used and elements that have been deleted are
2231// distinguished.  Probing continues when deleted elements are
2232// encountered and stops when unused elements are encountered.
2233//
2234// - Elements with key == undefined have not been used yet.
2235// - Elements with key == null have been deleted.
2236//
2237// The hash table class is parameterized with a Shape and a Key.
2238// Shape must be a class with the following interface:
2239//   class ExampleShape {
2240//    public:
2241//      // Tells whether key matches other.
2242//     static bool IsMatch(Key key, Object* other);
2243//     // Returns the hash value for key.
2244//     static uint32_t Hash(Key key);
2245//     // Returns the hash value for object.
2246//     static uint32_t HashForObject(Key key, Object* object);
2247//     // Convert key to an object.
2248//     static inline Object* AsObject(Key key);
2249//     // The prefix size indicates number of elements in the beginning
2250//     // of the backing storage.
2251//     static const int kPrefixSize = ..;
2252//     // The Element size indicates number of elements per entry.
2253//     static const int kEntrySize = ..;
2254//   };
2255// The prefix size indicates an amount of memory in the
2256// beginning of the backing storage that can be used for non-element
2257// information by subclasses.
2258
2259template<typename Shape, typename Key>
2260class HashTable: public FixedArray {
2261 public:
2262  // Returns the number of elements in the hash table.
2263  int NumberOfElements() {
2264    return Smi::cast(get(kNumberOfElementsIndex))->value();
2265  }
2266
2267  // Returns the number of deleted elements in the hash table.
2268  int NumberOfDeletedElements() {
2269    return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
2270  }
2271
2272  // Returns the capacity of the hash table.
2273  int Capacity() {
2274    return Smi::cast(get(kCapacityIndex))->value();
2275  }
2276
2277  // ElementAdded should be called whenever an element is added to a
2278  // hash table.
2279  void ElementAdded() { SetNumberOfElements(NumberOfElements() + 1); }
2280
2281  // ElementRemoved should be called whenever an element is removed from
2282  // a hash table.
2283  void ElementRemoved() {
2284    SetNumberOfElements(NumberOfElements() - 1);
2285    SetNumberOfDeletedElements(NumberOfDeletedElements() + 1);
2286  }
2287  void ElementsRemoved(int n) {
2288    SetNumberOfElements(NumberOfElements() - n);
2289    SetNumberOfDeletedElements(NumberOfDeletedElements() + n);
2290  }
2291
2292  // Returns a new HashTable object. Might return Failure.
2293  MUST_USE_RESULT static MaybeObject* Allocate(
2294      int at_least_space_for,
2295      PretenureFlag pretenure = NOT_TENURED);
2296
2297  // Returns the key at entry.
2298  Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
2299
2300  // Tells whether k is a real key.  Null and undefined are not allowed
2301  // as keys and can be used to indicate missing or deleted elements.
2302  bool IsKey(Object* k) {
2303    return !k->IsNull() && !k->IsUndefined();
2304  }
2305
2306  // Garbage collection support.
2307  void IteratePrefix(ObjectVisitor* visitor);
2308  void IterateElements(ObjectVisitor* visitor);
2309
2310  // Casting.
2311  static inline HashTable* cast(Object* obj);
2312
2313  // Compute the probe offset (quadratic probing).
2314  INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
2315    return (n + n * n) >> 1;
2316  }
2317
2318  static const int kNumberOfElementsIndex = 0;
2319  static const int kNumberOfDeletedElementsIndex = 1;
2320  static const int kCapacityIndex = 2;
2321  static const int kPrefixStartIndex = 3;
2322  static const int kElementsStartIndex =
2323      kPrefixStartIndex + Shape::kPrefixSize;
2324  static const int kEntrySize = Shape::kEntrySize;
2325  static const int kElementsStartOffset =
2326      kHeaderSize + kElementsStartIndex * kPointerSize;
2327  static const int kCapacityOffset =
2328      kHeaderSize + kCapacityIndex * kPointerSize;
2329
2330  // Constant used for denoting a absent entry.
2331  static const int kNotFound = -1;
2332
2333  // Maximal capacity of HashTable. Based on maximal length of underlying
2334  // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
2335  // cannot overflow.
2336  static const int kMaxCapacity =
2337      (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
2338
2339  // Find entry for key otherwise return kNotFound.
2340  inline int FindEntry(Key key);
2341  int FindEntry(Isolate* isolate, Key key);
2342
2343 protected:
2344
2345  // Find the entry at which to insert element with the given key that
2346  // has the given hash value.
2347  uint32_t FindInsertionEntry(uint32_t hash);
2348
2349  // Returns the index for an entry (of the key)
2350  static inline int EntryToIndex(int entry) {
2351    return (entry * kEntrySize) + kElementsStartIndex;
2352  }
2353
2354  // Update the number of elements in the hash table.
2355  void SetNumberOfElements(int nof) {
2356    fast_set(this, kNumberOfElementsIndex, Smi::FromInt(nof));
2357  }
2358
2359  // Update the number of deleted elements in the hash table.
2360  void SetNumberOfDeletedElements(int nod) {
2361    fast_set(this, kNumberOfDeletedElementsIndex, Smi::FromInt(nod));
2362  }
2363
2364  // Sets the capacity of the hash table.
2365  void SetCapacity(int capacity) {
2366    // To scale a computed hash code to fit within the hash table, we
2367    // use bit-wise AND with a mask, so the capacity must be positive
2368    // and non-zero.
2369    ASSERT(capacity > 0);
2370    ASSERT(capacity <= kMaxCapacity);
2371    fast_set(this, kCapacityIndex, Smi::FromInt(capacity));
2372  }
2373
2374
2375  // Returns probe entry.
2376  static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
2377    ASSERT(IsPowerOf2(size));
2378    return (hash + GetProbeOffset(number)) & (size - 1);
2379  }
2380
2381  static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
2382    return hash & (size - 1);
2383  }
2384
2385  static uint32_t NextProbe(uint32_t last, uint32_t number, uint32_t size) {
2386    return (last + number) & (size - 1);
2387  }
2388
2389  // Ensure enough space for n additional elements.
2390  MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
2391};
2392
2393
2394
2395// HashTableKey is an abstract superclass for virtual key behavior.
2396class HashTableKey {
2397 public:
2398  // Returns whether the other object matches this key.
2399  virtual bool IsMatch(Object* other) = 0;
2400  // Returns the hash value for this key.
2401  virtual uint32_t Hash() = 0;
2402  // Returns the hash value for object.
2403  virtual uint32_t HashForObject(Object* key) = 0;
2404  // Returns the key object for storing into the hash table.
2405  // If allocations fails a failure object is returned.
2406  MUST_USE_RESULT virtual MaybeObject* AsObject() = 0;
2407  // Required.
2408  virtual ~HashTableKey() {}
2409};
2410
2411class SymbolTableShape {
2412 public:
2413  static inline bool IsMatch(HashTableKey* key, Object* value) {
2414    return key->IsMatch(value);
2415  }
2416  static inline uint32_t Hash(HashTableKey* key) {
2417    return key->Hash();
2418  }
2419  static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
2420    return key->HashForObject(object);
2421  }
2422  MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
2423    return key->AsObject();
2424  }
2425
2426  static const int kPrefixSize = 0;
2427  static const int kEntrySize = 1;
2428};
2429
2430// SymbolTable.
2431//
2432// No special elements in the prefix and the element size is 1
2433// because only the symbol itself (the key) needs to be stored.
2434class SymbolTable: public HashTable<SymbolTableShape, HashTableKey*> {
2435 public:
2436  // Find symbol in the symbol table.  If it is not there yet, it is
2437  // added.  The return value is the symbol table which might have
2438  // been enlarged.  If the return value is not a failure, the symbol
2439  // pointer *s is set to the symbol found.
2440  MUST_USE_RESULT MaybeObject* LookupSymbol(Vector<const char> str, Object** s);
2441  MUST_USE_RESULT MaybeObject* LookupAsciiSymbol(Vector<const char> str,
2442                                                 Object** s);
2443  MUST_USE_RESULT MaybeObject* LookupTwoByteSymbol(Vector<const uc16> str,
2444                                                   Object** s);
2445  MUST_USE_RESULT MaybeObject* LookupString(String* key, Object** s);
2446
2447  // Looks up a symbol that is equal to the given string and returns
2448  // true if it is found, assigning the symbol to the given output
2449  // parameter.
2450  bool LookupSymbolIfExists(String* str, String** symbol);
2451  bool LookupTwoCharsSymbolIfExists(uint32_t c1, uint32_t c2, String** symbol);
2452
2453  // Casting.
2454  static inline SymbolTable* cast(Object* obj);
2455
2456 private:
2457  MUST_USE_RESULT MaybeObject* LookupKey(HashTableKey* key, Object** s);
2458
2459  DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolTable);
2460};
2461
2462
2463class MapCacheShape {
2464 public:
2465  static inline bool IsMatch(HashTableKey* key, Object* value) {
2466    return key->IsMatch(value);
2467  }
2468  static inline uint32_t Hash(HashTableKey* key) {
2469    return key->Hash();
2470  }
2471
2472  static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
2473    return key->HashForObject(object);
2474  }
2475
2476  MUST_USE_RESULT static inline MaybeObject* AsObject(HashTableKey* key) {
2477    return key->AsObject();
2478  }
2479
2480  static const int kPrefixSize = 0;
2481  static const int kEntrySize = 2;
2482};
2483
2484
2485// MapCache.
2486//
2487// Maps keys that are a fixed array of symbols to a map.
2488// Used for canonicalize maps for object literals.
2489class MapCache: public HashTable<MapCacheShape, HashTableKey*> {
2490 public:
2491  // Find cached value for a string key, otherwise return null.
2492  Object* Lookup(FixedArray* key);
2493  MUST_USE_RESULT MaybeObject* Put(FixedArray* key, Map* value);
2494  static inline MapCache* cast(Object* obj);
2495
2496 private:
2497  DISALLOW_IMPLICIT_CONSTRUCTORS(MapCache);
2498};
2499
2500
2501template <typename Shape, typename Key>
2502class Dictionary: public HashTable<Shape, Key> {
2503 public:
2504
2505  static inline Dictionary<Shape, Key>* cast(Object* obj) {
2506    return reinterpret_cast<Dictionary<Shape, Key>*>(obj);
2507  }
2508
2509  // Returns the value at entry.
2510  Object* ValueAt(int entry) {
2511    return this->get(HashTable<Shape, Key>::EntryToIndex(entry)+1);
2512  }
2513
2514  // Set the value for entry.
2515  // Returns false if the put wasn't performed due to property being read only.
2516  // Returns true on successful put.
2517  bool ValueAtPut(int entry, Object* value) {
2518    // Check that this value can actually be written.
2519    PropertyDetails details = DetailsAt(entry);
2520    // If a value has not been initilized we allow writing to it even if
2521    // it is read only (a declared const that has not been initialized).
2522    if (details.IsReadOnly() && !ValueAt(entry)->IsTheHole()) {
2523      return false;
2524    }
2525    this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 1, value);
2526    return true;
2527  }
2528
2529  // Returns the property details for the property at entry.
2530  PropertyDetails DetailsAt(int entry) {
2531    ASSERT(entry >= 0);  // Not found is -1, which is not caught by get().
2532    return PropertyDetails(
2533        Smi::cast(this->get(HashTable<Shape, Key>::EntryToIndex(entry) + 2)));
2534  }
2535
2536  // Set the details for entry.
2537  void DetailsAtPut(int entry, PropertyDetails value) {
2538    this->set(HashTable<Shape, Key>::EntryToIndex(entry) + 2, value.AsSmi());
2539  }
2540
2541  // Sorting support
2542  void CopyValuesTo(FixedArray* elements);
2543
2544  // Delete a property from the dictionary.
2545  Object* DeleteProperty(int entry, JSObject::DeleteMode mode);
2546
2547  // Returns the number of elements in the dictionary filtering out properties
2548  // with the specified attributes.
2549  int NumberOfElementsFilterAttributes(PropertyAttributes filter);
2550
2551  // Returns the number of enumerable elements in the dictionary.
2552  int NumberOfEnumElements();
2553
2554  // Copies keys to preallocated fixed array.
2555  void CopyKeysTo(FixedArray* storage, PropertyAttributes filter);
2556  // Fill in details for properties into storage.
2557  void CopyKeysTo(FixedArray* storage, int index);
2558
2559  // Accessors for next enumeration index.
2560  void SetNextEnumerationIndex(int index) {
2561    this->fast_set(this, kNextEnumerationIndexIndex, Smi::FromInt(index));
2562  }
2563
2564  int NextEnumerationIndex() {
2565    return Smi::cast(FixedArray::get(kNextEnumerationIndexIndex))->value();
2566  }
2567
2568  // Returns a new array for dictionary usage. Might return Failure.
2569  MUST_USE_RESULT static MaybeObject* Allocate(int at_least_space_for);
2570
2571  // Ensure enough space for n additional elements.
2572  MUST_USE_RESULT MaybeObject* EnsureCapacity(int n, Key key);
2573
2574#ifdef OBJECT_PRINT
2575  inline void Print() {
2576    Print(stdout);
2577  }
2578  void Print(FILE* out);
2579#endif
2580  // Returns the key (slow).
2581  Object* SlowReverseLookup(Object* value);
2582
2583  // Sets the entry to (key, value) pair.
2584  inline void SetEntry(int entry,
2585                       Object* key,
2586                       Object* value);
2587  inline void SetEntry(int entry,
2588                       Object* key,
2589                       Object* value,
2590                       PropertyDetails details);
2591
2592  MUST_USE_RESULT MaybeObject* Add(Key key,
2593                                   Object* value,
2594                                   PropertyDetails details);
2595
2596 protected:
2597  // Generic at put operation.
2598  MUST_USE_RESULT MaybeObject* AtPut(Key key, Object* value);
2599
2600  // Add entry to dictionary.
2601  MUST_USE_RESULT MaybeObject* AddEntry(Key key,
2602                                        Object* value,
2603                                        PropertyDetails details,
2604                                        uint32_t hash);
2605
2606  // Generate new enumeration indices to avoid enumeration index overflow.
2607  MUST_USE_RESULT MaybeObject* GenerateNewEnumerationIndices();
2608  static const int kMaxNumberKeyIndex =
2609      HashTable<Shape, Key>::kPrefixStartIndex;
2610  static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
2611};
2612
2613
2614class StringDictionaryShape {
2615 public:
2616  static inline bool IsMatch(String* key, Object* other);
2617  static inline uint32_t Hash(String* key);
2618  static inline uint32_t HashForObject(String* key, Object* object);
2619  MUST_USE_RESULT static inline MaybeObject* AsObject(String* key);
2620  static const int kPrefixSize = 2;
2621  static const int kEntrySize = 3;
2622  static const bool kIsEnumerable = true;
2623};
2624
2625
2626class StringDictionary: public Dictionary<StringDictionaryShape, String*> {
2627 public:
2628  static inline StringDictionary* cast(Object* obj) {
2629    ASSERT(obj->IsDictionary());
2630    return reinterpret_cast<StringDictionary*>(obj);
2631  }
2632
2633  // Copies enumerable keys to preallocated fixed array.
2634  void CopyEnumKeysTo(FixedArray* storage, FixedArray* sort_array);
2635
2636  // For transforming properties of a JSObject.
2637  MUST_USE_RESULT MaybeObject* TransformPropertiesToFastFor(
2638      JSObject* obj,
2639      int unused_property_fields);
2640
2641  // Find entry for key otherwise return kNotFound. Optimzed version of
2642  // HashTable::FindEntry.
2643  int FindEntry(String* key);
2644};
2645
2646
2647class NumberDictionaryShape {
2648 public:
2649  static inline bool IsMatch(uint32_t key, Object* other);
2650  static inline uint32_t Hash(uint32_t key);
2651  static inline uint32_t HashForObject(uint32_t key, Object* object);
2652  MUST_USE_RESULT static inline MaybeObject* AsObject(uint32_t key);
2653  static const int kPrefixSize = 2;
2654  static const int kEntrySize = 3;
2655  static const bool kIsEnumerable = false;
2656};
2657
2658
2659class NumberDictionary: public Dictionary<NumberDictionaryShape, uint32_t> {
2660 public:
2661  static NumberDictionary* cast(Object* obj) {
2662    ASSERT(obj->IsDictionary());
2663    return reinterpret_cast<NumberDictionary*>(obj);
2664  }
2665
2666  // Type specific at put (default NONE attributes is used when adding).
2667  MUST_USE_RESULT MaybeObject* AtNumberPut(uint32_t key, Object* value);
2668  MUST_USE_RESULT MaybeObject* AddNumberEntry(uint32_t key,
2669                                              Object* value,
2670                                              PropertyDetails details);
2671
2672  // Set an existing entry or add a new one if needed.
2673  MUST_USE_RESULT MaybeObject* Set(uint32_t key,
2674                                   Object* value,
2675                                   PropertyDetails details);
2676
2677  void UpdateMaxNumberKey(uint32_t key);
2678
2679  // If slow elements are required we will never go back to fast-case
2680  // for the elements kept in this dictionary.  We require slow
2681  // elements if an element has been added at an index larger than
2682  // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
2683  // when defining a getter or setter with a number key.
2684  inline bool requires_slow_elements();
2685  inline void set_requires_slow_elements();
2686
2687  // Get the value of the max number key that has been added to this
2688  // dictionary.  max_number_key can only be called if
2689  // requires_slow_elements returns false.
2690  inline uint32_t max_number_key();
2691
2692  // Remove all entries were key is a number and (from <= key && key < to).
2693  void RemoveNumberEntries(uint32_t from, uint32_t to);
2694
2695  // Bit masks.
2696  static const int kRequiresSlowElementsMask = 1;
2697  static const int kRequiresSlowElementsTagSize = 1;
2698  static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
2699};
2700
2701
2702// JSFunctionResultCache caches results of some JSFunction invocation.
2703// It is a fixed array with fixed structure:
2704//   [0]: factory function
2705//   [1]: finger index
2706//   [2]: current cache size
2707//   [3]: dummy field.
2708// The rest of array are key/value pairs.
2709class JSFunctionResultCache: public FixedArray {
2710 public:
2711  static const int kFactoryIndex = 0;
2712  static const int kFingerIndex = kFactoryIndex + 1;
2713  static const int kCacheSizeIndex = kFingerIndex + 1;
2714  static const int kDummyIndex = kCacheSizeIndex + 1;
2715  static const int kEntriesIndex = kDummyIndex + 1;
2716
2717  static const int kEntrySize = 2;  // key + value
2718
2719  static const int kFactoryOffset = kHeaderSize;
2720  static const int kFingerOffset = kFactoryOffset + kPointerSize;
2721  static const int kCacheSizeOffset = kFingerOffset + kPointerSize;
2722
2723  inline void MakeZeroSize();
2724  inline void Clear();
2725
2726  inline int size();
2727  inline void set_size(int size);
2728  inline int finger_index();
2729  inline void set_finger_index(int finger_index);
2730
2731  // Casting
2732  static inline JSFunctionResultCache* cast(Object* obj);
2733
2734#ifdef DEBUG
2735  void JSFunctionResultCacheVerify();
2736#endif
2737};
2738
2739
2740// The cache for maps used by normalized (dictionary mode) objects.
2741// Such maps do not have property descriptors, so a typical program
2742// needs very limited number of distinct normalized maps.
2743class NormalizedMapCache: public FixedArray {
2744 public:
2745  static const int kEntries = 64;
2746
2747  MUST_USE_RESULT MaybeObject* Get(JSObject* object,
2748                                   PropertyNormalizationMode mode);
2749
2750  void Clear();
2751
2752  // Casting
2753  static inline NormalizedMapCache* cast(Object* obj);
2754
2755#ifdef DEBUG
2756  void NormalizedMapCacheVerify();
2757#endif
2758
2759 private:
2760  static int Hash(Map* fast);
2761
2762  static bool CheckHit(Map* slow, Map* fast, PropertyNormalizationMode mode);
2763};
2764
2765
2766// ByteArray represents fixed sized byte arrays.  Used by the outside world,
2767// such as PCRE, and also by the memory allocator and garbage collector to
2768// fill in free blocks in the heap.
2769class ByteArray: public HeapObject {
2770 public:
2771  // [length]: length of the array.
2772  inline int length();
2773  inline void set_length(int value);
2774
2775  // Setter and getter.
2776  inline byte get(int index);
2777  inline void set(int index, byte value);
2778
2779  // Treat contents as an int array.
2780  inline int get_int(int index);
2781
2782  static int SizeFor(int length) {
2783    return OBJECT_POINTER_ALIGN(kHeaderSize + length);
2784  }
2785  // We use byte arrays for free blocks in the heap.  Given a desired size in
2786  // bytes that is a multiple of the word size and big enough to hold a byte
2787  // array, this function returns the number of elements a byte array should
2788  // have.
2789  static int LengthFor(int size_in_bytes) {
2790    ASSERT(IsAligned(size_in_bytes, kPointerSize));
2791    ASSERT(size_in_bytes >= kHeaderSize);
2792    return size_in_bytes - kHeaderSize;
2793  }
2794
2795  // Returns data start address.
2796  inline Address GetDataStartAddress();
2797
2798  // Returns a pointer to the ByteArray object for a given data start address.
2799  static inline ByteArray* FromDataStartAddress(Address address);
2800
2801  // Casting.
2802  static inline ByteArray* cast(Object* obj);
2803
2804  // Dispatched behavior.
2805  inline int ByteArraySize() {
2806    return SizeFor(this->length());
2807  }
2808#ifdef OBJECT_PRINT
2809  inline void ByteArrayPrint() {
2810    ByteArrayPrint(stdout);
2811  }
2812  void ByteArrayPrint(FILE* out);
2813#endif
2814#ifdef DEBUG
2815  void ByteArrayVerify();
2816#endif
2817
2818  // Layout description.
2819  // Length is smi tagged when it is stored.
2820  static const int kLengthOffset = HeapObject::kHeaderSize;
2821  static const int kHeaderSize = kLengthOffset + kPointerSize;
2822
2823  static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
2824
2825  // Maximal memory consumption for a single ByteArray.
2826  static const int kMaxSize = 512 * MB;
2827  // Maximal length of a single ByteArray.
2828  static const int kMaxLength = kMaxSize - kHeaderSize;
2829
2830 private:
2831  DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
2832};
2833
2834
2835// An ExternalArray represents a fixed-size array of primitive values
2836// which live outside the JavaScript heap. Its subclasses are used to
2837// implement the CanvasArray types being defined in the WebGL
2838// specification. As of this writing the first public draft is not yet
2839// available, but Khronos members can access the draft at:
2840//   https://cvs.khronos.org/svn/repos/3dweb/trunk/doc/spec/WebGL-spec.html
2841//
2842// The semantics of these arrays differ from CanvasPixelArray.
2843// Out-of-range values passed to the setter are converted via a C
2844// cast, not clamping. Out-of-range indices cause exceptions to be
2845// raised rather than being silently ignored.
2846class ExternalArray: public HeapObject {
2847 public:
2848  // [length]: length of the array.
2849  inline int length();
2850  inline void set_length(int value);
2851
2852  // [external_pointer]: The pointer to the external memory area backing this
2853  // external array.
2854  DECL_ACCESSORS(external_pointer, void)  // Pointer to the data store.
2855
2856  // Casting.
2857  static inline ExternalArray* cast(Object* obj);
2858
2859  // Maximal acceptable length for an external array.
2860  static const int kMaxLength = 0x3fffffff;
2861
2862  // ExternalArray headers are not quadword aligned.
2863  static const int kLengthOffset = HeapObject::kHeaderSize;
2864  static const int kExternalPointerOffset =
2865      POINTER_SIZE_ALIGN(kLengthOffset + kIntSize);
2866  static const int kHeaderSize = kExternalPointerOffset + kPointerSize;
2867  static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
2868
2869 private:
2870  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalArray);
2871};
2872
2873
2874// A ExternalPixelArray represents a fixed-size byte array with special
2875// semantics used for implementing the CanvasPixelArray object. Please see the
2876// specification at:
2877
2878// http://www.whatwg.org/specs/web-apps/current-work/
2879//                      multipage/the-canvas-element.html#canvaspixelarray
2880// In particular, write access clamps the value written to 0 or 255 if the
2881// value written is outside this range.
2882class ExternalPixelArray: public ExternalArray {
2883 public:
2884  inline uint8_t* external_pixel_pointer();
2885
2886  // Setter and getter.
2887  inline uint8_t get(int index);
2888  inline void set(int index, uint8_t value);
2889
2890  // This accessor applies the correct conversion from Smi, HeapNumber and
2891  // undefined and clamps the converted value between 0 and 255.
2892  Object* SetValue(uint32_t index, Object* value);
2893
2894  // Casting.
2895  static inline ExternalPixelArray* cast(Object* obj);
2896
2897#ifdef OBJECT_PRINT
2898  inline void ExternalPixelArrayPrint() {
2899    ExternalPixelArrayPrint(stdout);
2900  }
2901  void ExternalPixelArrayPrint(FILE* out);
2902#endif
2903#ifdef DEBUG
2904  void ExternalPixelArrayVerify();
2905#endif  // DEBUG
2906
2907 private:
2908  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalPixelArray);
2909};
2910
2911
2912class ExternalByteArray: public ExternalArray {
2913 public:
2914  // Setter and getter.
2915  inline int8_t get(int index);
2916  inline void set(int index, int8_t value);
2917
2918  // This accessor applies the correct conversion from Smi, HeapNumber
2919  // and undefined.
2920  MaybeObject* SetValue(uint32_t index, Object* value);
2921
2922  // Casting.
2923  static inline ExternalByteArray* cast(Object* obj);
2924
2925#ifdef OBJECT_PRINT
2926  inline void ExternalByteArrayPrint() {
2927    ExternalByteArrayPrint(stdout);
2928  }
2929  void ExternalByteArrayPrint(FILE* out);
2930#endif
2931#ifdef DEBUG
2932  void ExternalByteArrayVerify();
2933#endif  // DEBUG
2934
2935 private:
2936  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalByteArray);
2937};
2938
2939
2940class ExternalUnsignedByteArray: public ExternalArray {
2941 public:
2942  // Setter and getter.
2943  inline uint8_t get(int index);
2944  inline void set(int index, uint8_t value);
2945
2946  // This accessor applies the correct conversion from Smi, HeapNumber
2947  // and undefined.
2948  MaybeObject* SetValue(uint32_t index, Object* value);
2949
2950  // Casting.
2951  static inline ExternalUnsignedByteArray* cast(Object* obj);
2952
2953#ifdef OBJECT_PRINT
2954  inline void ExternalUnsignedByteArrayPrint() {
2955    ExternalUnsignedByteArrayPrint(stdout);
2956  }
2957  void ExternalUnsignedByteArrayPrint(FILE* out);
2958#endif
2959#ifdef DEBUG
2960  void ExternalUnsignedByteArrayVerify();
2961#endif  // DEBUG
2962
2963 private:
2964  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedByteArray);
2965};
2966
2967
2968class ExternalShortArray: public ExternalArray {
2969 public:
2970  // Setter and getter.
2971  inline int16_t get(int index);
2972  inline void set(int index, int16_t value);
2973
2974  // This accessor applies the correct conversion from Smi, HeapNumber
2975  // and undefined.
2976  MaybeObject* SetValue(uint32_t index, Object* value);
2977
2978  // Casting.
2979  static inline ExternalShortArray* cast(Object* obj);
2980
2981#ifdef OBJECT_PRINT
2982  inline void ExternalShortArrayPrint() {
2983    ExternalShortArrayPrint(stdout);
2984  }
2985  void ExternalShortArrayPrint(FILE* out);
2986#endif
2987#ifdef DEBUG
2988  void ExternalShortArrayVerify();
2989#endif  // DEBUG
2990
2991 private:
2992  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalShortArray);
2993};
2994
2995
2996class ExternalUnsignedShortArray: public ExternalArray {
2997 public:
2998  // Setter and getter.
2999  inline uint16_t get(int index);
3000  inline void set(int index, uint16_t value);
3001
3002  // This accessor applies the correct conversion from Smi, HeapNumber
3003  // and undefined.
3004  MaybeObject* SetValue(uint32_t index, Object* value);
3005
3006  // Casting.
3007  static inline ExternalUnsignedShortArray* cast(Object* obj);
3008
3009#ifdef OBJECT_PRINT
3010  inline void ExternalUnsignedShortArrayPrint() {
3011    ExternalUnsignedShortArrayPrint(stdout);
3012  }
3013  void ExternalUnsignedShortArrayPrint(FILE* out);
3014#endif
3015#ifdef DEBUG
3016  void ExternalUnsignedShortArrayVerify();
3017#endif  // DEBUG
3018
3019 private:
3020  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedShortArray);
3021};
3022
3023
3024class ExternalIntArray: public ExternalArray {
3025 public:
3026  // Setter and getter.
3027  inline int32_t get(int index);
3028  inline void set(int index, int32_t value);
3029
3030  // This accessor applies the correct conversion from Smi, HeapNumber
3031  // and undefined.
3032  MaybeObject* SetValue(uint32_t index, Object* value);
3033
3034  // Casting.
3035  static inline ExternalIntArray* cast(Object* obj);
3036
3037#ifdef OBJECT_PRINT
3038  inline void ExternalIntArrayPrint() {
3039    ExternalIntArrayPrint(stdout);
3040  }
3041  void ExternalIntArrayPrint(FILE* out);
3042#endif
3043#ifdef DEBUG
3044  void ExternalIntArrayVerify();
3045#endif  // DEBUG
3046
3047 private:
3048  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalIntArray);
3049};
3050
3051
3052class ExternalUnsignedIntArray: public ExternalArray {
3053 public:
3054  // Setter and getter.
3055  inline uint32_t get(int index);
3056  inline void set(int index, uint32_t value);
3057
3058  // This accessor applies the correct conversion from Smi, HeapNumber
3059  // and undefined.
3060  MaybeObject* SetValue(uint32_t index, Object* value);
3061
3062  // Casting.
3063  static inline ExternalUnsignedIntArray* cast(Object* obj);
3064
3065#ifdef OBJECT_PRINT
3066  inline void ExternalUnsignedIntArrayPrint() {
3067    ExternalUnsignedIntArrayPrint(stdout);
3068  }
3069  void ExternalUnsignedIntArrayPrint(FILE* out);
3070#endif
3071#ifdef DEBUG
3072  void ExternalUnsignedIntArrayVerify();
3073#endif  // DEBUG
3074
3075 private:
3076  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalUnsignedIntArray);
3077};
3078
3079
3080class ExternalFloatArray: public ExternalArray {
3081 public:
3082  // Setter and getter.
3083  inline float get(int index);
3084  inline void set(int index, float value);
3085
3086  // This accessor applies the correct conversion from Smi, HeapNumber
3087  // and undefined.
3088  MaybeObject* SetValue(uint32_t index, Object* value);
3089
3090  // Casting.
3091  static inline ExternalFloatArray* cast(Object* obj);
3092
3093#ifdef OBJECT_PRINT
3094  inline void ExternalFloatArrayPrint() {
3095    ExternalFloatArrayPrint(stdout);
3096  }
3097  void ExternalFloatArrayPrint(FILE* out);
3098#endif
3099#ifdef DEBUG
3100  void ExternalFloatArrayVerify();
3101#endif  // DEBUG
3102
3103 private:
3104  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalFloatArray);
3105};
3106
3107
3108// DeoptimizationInputData is a fixed array used to hold the deoptimization
3109// data for code generated by the Hydrogen/Lithium compiler.  It also
3110// contains information about functions that were inlined.  If N different
3111// functions were inlined then first N elements of the literal array will
3112// contain these functions.
3113//
3114// It can be empty.
3115class DeoptimizationInputData: public FixedArray {
3116 public:
3117  // Layout description.  Indices in the array.
3118  static const int kTranslationByteArrayIndex = 0;
3119  static const int kInlinedFunctionCountIndex = 1;
3120  static const int kLiteralArrayIndex = 2;
3121  static const int kOsrAstIdIndex = 3;
3122  static const int kOsrPcOffsetIndex = 4;
3123  static const int kFirstDeoptEntryIndex = 5;
3124
3125  // Offsets of deopt entry elements relative to the start of the entry.
3126  static const int kAstIdOffset = 0;
3127  static const int kTranslationIndexOffset = 1;
3128  static const int kArgumentsStackHeightOffset = 2;
3129  static const int kDeoptEntrySize = 3;
3130
3131  // Simple element accessors.
3132#define DEFINE_ELEMENT_ACCESSORS(name, type)      \
3133  type* name() {                                  \
3134    return type::cast(get(k##name##Index));       \
3135  }                                               \
3136  void Set##name(type* value) {                   \
3137    set(k##name##Index, value);                   \
3138  }
3139
3140  DEFINE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
3141  DEFINE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
3142  DEFINE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
3143  DEFINE_ELEMENT_ACCESSORS(OsrAstId, Smi)
3144  DEFINE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
3145
3146  // Unchecked accessor to be used during GC.
3147  FixedArray* UncheckedLiteralArray() {
3148    return reinterpret_cast<FixedArray*>(get(kLiteralArrayIndex));
3149  }
3150
3151#undef DEFINE_ELEMENT_ACCESSORS
3152
3153  // Accessors for elements of the ith deoptimization entry.
3154#define DEFINE_ENTRY_ACCESSORS(name, type)                       \
3155  type* name(int i) {                                            \
3156    return type::cast(get(IndexForEntry(i) + k##name##Offset));  \
3157  }                                                              \
3158  void Set##name(int i, type* value) {                           \
3159    set(IndexForEntry(i) + k##name##Offset, value);              \
3160  }
3161
3162  DEFINE_ENTRY_ACCESSORS(AstId, Smi)
3163  DEFINE_ENTRY_ACCESSORS(TranslationIndex, Smi)
3164  DEFINE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
3165
3166#undef DEFINE_ENTRY_ACCESSORS
3167
3168  int DeoptCount() {
3169    return (length() - kFirstDeoptEntryIndex) / kDeoptEntrySize;
3170  }
3171
3172  // Allocates a DeoptimizationInputData.
3173  MUST_USE_RESULT static MaybeObject* Allocate(int deopt_entry_count,
3174                                               PretenureFlag pretenure);
3175
3176  // Casting.
3177  static inline DeoptimizationInputData* cast(Object* obj);
3178
3179#ifdef OBJECT_PRINT
3180  void DeoptimizationInputDataPrint(FILE* out);
3181#endif
3182
3183 private:
3184  static int IndexForEntry(int i) {
3185    return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
3186  }
3187
3188  static int LengthFor(int entry_count) {
3189    return IndexForEntry(entry_count);
3190  }
3191};
3192
3193
3194// DeoptimizationOutputData is a fixed array used to hold the deoptimization
3195// data for code generated by the full compiler.
3196// The format of the these objects is
3197//   [i * 2]: Ast ID for ith deoptimization.
3198//   [i * 2 + 1]: PC and state of ith deoptimization
3199class DeoptimizationOutputData: public FixedArray {
3200 public:
3201  int DeoptPoints() { return length() / 2; }
3202  Smi* AstId(int index) { return Smi::cast(get(index * 2)); }
3203  void SetAstId(int index, Smi* id) { set(index * 2, id); }
3204  Smi* PcAndState(int index) { return Smi::cast(get(1 + index * 2)); }
3205  void SetPcAndState(int index, Smi* offset) { set(1 + index * 2, offset); }
3206
3207  static int LengthOfFixedArray(int deopt_points) {
3208    return deopt_points * 2;
3209  }
3210
3211  // Allocates a DeoptimizationOutputData.
3212  MUST_USE_RESULT static MaybeObject* Allocate(int number_of_deopt_points,
3213                                               PretenureFlag pretenure);
3214
3215  // Casting.
3216  static inline DeoptimizationOutputData* cast(Object* obj);
3217
3218#ifdef OBJECT_PRINT
3219  void DeoptimizationOutputDataPrint(FILE* out);
3220#endif
3221};
3222
3223
3224class SafepointEntry;
3225
3226
3227// Code describes objects with on-the-fly generated machine code.
3228class Code: public HeapObject {
3229 public:
3230  // Opaque data type for encapsulating code flags like kind, inline
3231  // cache state, and arguments count.
3232  // FLAGS_MIN_VALUE and FLAGS_MAX_VALUE are specified to ensure that
3233  // enumeration type has correct value range (see Issue 830 for more details).
3234  enum Flags {
3235    FLAGS_MIN_VALUE = kMinInt,
3236    FLAGS_MAX_VALUE = kMaxInt
3237  };
3238
3239  enum Kind {
3240    FUNCTION,
3241    OPTIMIZED_FUNCTION,
3242    STUB,
3243    BUILTIN,
3244    LOAD_IC,
3245    KEYED_LOAD_IC,
3246    KEYED_EXTERNAL_ARRAY_LOAD_IC,
3247    CALL_IC,
3248    KEYED_CALL_IC,
3249    STORE_IC,
3250    KEYED_STORE_IC,
3251    KEYED_EXTERNAL_ARRAY_STORE_IC,
3252    TYPE_RECORDING_BINARY_OP_IC,
3253    COMPARE_IC,
3254    // No more than 16 kinds. The value currently encoded in four bits in
3255    // Flags.
3256
3257    // Pseudo-kinds.
3258    REGEXP = BUILTIN,
3259    FIRST_IC_KIND = LOAD_IC,
3260    LAST_IC_KIND = COMPARE_IC
3261  };
3262
3263  enum {
3264    NUMBER_OF_KINDS = LAST_IC_KIND + 1
3265  };
3266
3267  typedef int ExtraICState;
3268
3269  static const ExtraICState kNoExtraICState = 0;
3270
3271#ifdef ENABLE_DISASSEMBLER
3272  // Printing
3273  static const char* Kind2String(Kind kind);
3274  static const char* ICState2String(InlineCacheState state);
3275  static const char* PropertyType2String(PropertyType type);
3276  static void PrintExtraICState(FILE* out, Kind kind, ExtraICState extra);
3277  inline void Disassemble(const char* name) {
3278    Disassemble(name, stdout);
3279  }
3280  void Disassemble(const char* name, FILE* out);
3281#endif  // ENABLE_DISASSEMBLER
3282
3283  // [instruction_size]: Size of the native instructions
3284  inline int instruction_size();
3285  inline void set_instruction_size(int value);
3286
3287  // [relocation_info]: Code relocation information
3288  DECL_ACCESSORS(relocation_info, ByteArray)
3289  void InvalidateRelocation();
3290
3291  // [deoptimization_data]: Array containing data for deopt.
3292  DECL_ACCESSORS(deoptimization_data, FixedArray)
3293
3294  // Unchecked accessors to be used during GC.
3295  inline ByteArray* unchecked_relocation_info();
3296  inline FixedArray* unchecked_deoptimization_data();
3297
3298  inline int relocation_size();
3299
3300  // [flags]: Various code flags.
3301  inline Flags flags();
3302  inline void set_flags(Flags flags);
3303
3304  // [flags]: Access to specific code flags.
3305  inline Kind kind();
3306  inline InlineCacheState ic_state();  // Only valid for IC stubs.
3307  inline ExtraICState extra_ic_state();  // Only valid for IC stubs.
3308  inline InLoopFlag ic_in_loop();  // Only valid for IC stubs.
3309  inline PropertyType type();  // Only valid for monomorphic IC stubs.
3310  inline int arguments_count();  // Only valid for call IC stubs.
3311
3312  // Testers for IC stub kinds.
3313  inline bool is_inline_cache_stub();
3314  inline bool is_load_stub() { return kind() == LOAD_IC; }
3315  inline bool is_keyed_load_stub() { return kind() == KEYED_LOAD_IC; }
3316  inline bool is_store_stub() { return kind() == STORE_IC; }
3317  inline bool is_keyed_store_stub() { return kind() == KEYED_STORE_IC; }
3318  inline bool is_call_stub() { return kind() == CALL_IC; }
3319  inline bool is_keyed_call_stub() { return kind() == KEYED_CALL_IC; }
3320  inline bool is_type_recording_binary_op_stub() {
3321    return kind() == TYPE_RECORDING_BINARY_OP_IC;
3322  }
3323  inline bool is_compare_ic_stub() { return kind() == COMPARE_IC; }
3324  inline bool is_external_array_load_stub() {
3325    return kind() == KEYED_EXTERNAL_ARRAY_LOAD_IC;
3326  }
3327  inline bool is_external_array_store_stub() {
3328    return kind() == KEYED_EXTERNAL_ARRAY_STORE_IC;
3329  }
3330
3331  // [major_key]: For kind STUB or BINARY_OP_IC, the major key.
3332  inline int major_key();
3333  inline void set_major_key(int value);
3334
3335  // [optimizable]: For FUNCTION kind, tells if it is optimizable.
3336  inline bool optimizable();
3337  inline void set_optimizable(bool value);
3338
3339  // [has_deoptimization_support]: For FUNCTION kind, tells if it has
3340  // deoptimization support.
3341  inline bool has_deoptimization_support();
3342  inline void set_has_deoptimization_support(bool value);
3343
3344  // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
3345  // how long the function has been marked for OSR and therefore which
3346  // level of loop nesting we are willing to do on-stack replacement
3347  // for.
3348  inline void set_allow_osr_at_loop_nesting_level(int level);
3349  inline int allow_osr_at_loop_nesting_level();
3350
3351  // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
3352  // reserved in the code prologue.
3353  inline unsigned stack_slots();
3354  inline void set_stack_slots(unsigned slots);
3355
3356  // [safepoint_table_start]: For kind OPTIMIZED_CODE, the offset in
3357  // the instruction stream where the safepoint table starts.
3358  inline unsigned safepoint_table_offset();
3359  inline void set_safepoint_table_offset(unsigned offset);
3360
3361  // [stack_check_table_start]: For kind FUNCTION, the offset in the
3362  // instruction stream where the stack check table starts.
3363  inline unsigned stack_check_table_offset();
3364  inline void set_stack_check_table_offset(unsigned offset);
3365
3366  // [check type]: For kind CALL_IC, tells how to check if the
3367  // receiver is valid for the given call.
3368  inline CheckType check_type();
3369  inline void set_check_type(CheckType value);
3370
3371  // [external array type]: For kind KEYED_EXTERNAL_ARRAY_LOAD_IC and
3372  // KEYED_EXTERNAL_ARRAY_STORE_IC, identifies the type of external
3373  // array that the code stub is specialized for.
3374  inline ExternalArrayType external_array_type();
3375  inline void set_external_array_type(ExternalArrayType value);
3376
3377  // [type-recording binary op type]: For all TYPE_RECORDING_BINARY_OP_IC.
3378  inline byte type_recording_binary_op_type();
3379  inline void set_type_recording_binary_op_type(byte value);
3380  inline byte type_recording_binary_op_result_type();
3381  inline void set_type_recording_binary_op_result_type(byte value);
3382
3383  // [compare state]: For kind compare IC stubs, tells what state the
3384  // stub is in.
3385  inline byte compare_state();
3386  inline void set_compare_state(byte value);
3387
3388  // Get the safepoint entry for the given pc.
3389  SafepointEntry GetSafepointEntry(Address pc);
3390
3391  // Mark this code object as not having a stack check table.  Assumes kind
3392  // is FUNCTION.
3393  void SetNoStackCheckTable();
3394
3395  // Find the first map in an IC stub.
3396  Map* FindFirstMap();
3397
3398  // Flags operations.
3399  static inline Flags ComputeFlags(
3400      Kind kind,
3401      InLoopFlag in_loop = NOT_IN_LOOP,
3402      InlineCacheState ic_state = UNINITIALIZED,
3403      ExtraICState extra_ic_state = kNoExtraICState,
3404      PropertyType type = NORMAL,
3405      int argc = -1,
3406      InlineCacheHolderFlag holder = OWN_MAP);
3407
3408  static inline Flags ComputeMonomorphicFlags(
3409      Kind kind,
3410      PropertyType type,
3411      ExtraICState extra_ic_state = kNoExtraICState,
3412      InlineCacheHolderFlag holder = OWN_MAP,
3413      InLoopFlag in_loop = NOT_IN_LOOP,
3414      int argc = -1);
3415
3416  static inline Kind ExtractKindFromFlags(Flags flags);
3417  static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
3418  static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
3419  static inline InLoopFlag ExtractICInLoopFromFlags(Flags flags);
3420  static inline PropertyType ExtractTypeFromFlags(Flags flags);
3421  static inline int ExtractArgumentsCountFromFlags(Flags flags);
3422  static inline InlineCacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
3423  static inline Flags RemoveTypeFromFlags(Flags flags);
3424
3425  // Convert a target address into a code object.
3426  static inline Code* GetCodeFromTargetAddress(Address address);
3427
3428  // Convert an entry address into an object.
3429  static inline Object* GetObjectFromEntryAddress(Address location_of_address);
3430
3431  // Returns the address of the first instruction.
3432  inline byte* instruction_start();
3433
3434  // Returns the address right after the last instruction.
3435  inline byte* instruction_end();
3436
3437  // Returns the size of the instructions, padding, and relocation information.
3438  inline int body_size();
3439
3440  // Returns the address of the first relocation info (read backwards!).
3441  inline byte* relocation_start();
3442
3443  // Code entry point.
3444  inline byte* entry();
3445
3446  // Returns true if pc is inside this object's instructions.
3447  inline bool contains(byte* pc);
3448
3449  // Relocate the code by delta bytes. Called to signal that this code
3450  // object has been moved by delta bytes.
3451  void Relocate(intptr_t delta);
3452
3453  // Migrate code described by desc.
3454  void CopyFrom(const CodeDesc& desc);
3455
3456  // Returns the object size for a given body (used for allocation).
3457  static int SizeFor(int body_size) {
3458    ASSERT_SIZE_TAG_ALIGNED(body_size);
3459    return RoundUp(kHeaderSize + body_size, kCodeAlignment);
3460  }
3461
3462  // Calculate the size of the code object to report for log events. This takes
3463  // the layout of the code object into account.
3464  int ExecutableSize() {
3465    // Check that the assumptions about the layout of the code object holds.
3466    ASSERT_EQ(static_cast<int>(instruction_start() - address()),
3467              Code::kHeaderSize);
3468    return instruction_size() + Code::kHeaderSize;
3469  }
3470
3471  // Locating source position.
3472  int SourcePosition(Address pc);
3473  int SourceStatementPosition(Address pc);
3474
3475  // Casting.
3476  static inline Code* cast(Object* obj);
3477
3478  // Dispatched behavior.
3479  int CodeSize() { return SizeFor(body_size()); }
3480  inline void CodeIterateBody(ObjectVisitor* v);
3481
3482  template<typename StaticVisitor>
3483  inline void CodeIterateBody(Heap* heap);
3484#ifdef OBJECT_PRINT
3485  inline void CodePrint() {
3486    CodePrint(stdout);
3487  }
3488  void CodePrint(FILE* out);
3489#endif
3490#ifdef DEBUG
3491  void CodeVerify();
3492#endif
3493
3494  // Returns the isolate/heap this code object belongs to.
3495  inline Isolate* isolate();
3496  inline Heap* heap();
3497
3498  // Max loop nesting marker used to postpose OSR. We don't take loop
3499  // nesting that is deeper than 5 levels into account.
3500  static const int kMaxLoopNestingMarker = 6;
3501
3502  // Layout description.
3503  static const int kInstructionSizeOffset = HeapObject::kHeaderSize;
3504  static const int kRelocationInfoOffset = kInstructionSizeOffset + kIntSize;
3505  static const int kDeoptimizationDataOffset =
3506      kRelocationInfoOffset + kPointerSize;
3507  static const int kFlagsOffset = kDeoptimizationDataOffset + kPointerSize;
3508  static const int kKindSpecificFlagsOffset  = kFlagsOffset + kIntSize;
3509
3510  static const int kKindSpecificFlagsSize = 2 * kIntSize;
3511
3512  static const int kHeaderPaddingStart = kKindSpecificFlagsOffset +
3513      kKindSpecificFlagsSize;
3514
3515  // Add padding to align the instruction start following right after
3516  // the Code object header.
3517  static const int kHeaderSize =
3518      (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
3519
3520  // Byte offsets within kKindSpecificFlagsOffset.
3521  static const int kStubMajorKeyOffset = kKindSpecificFlagsOffset;
3522  static const int kOptimizableOffset = kKindSpecificFlagsOffset;
3523  static const int kStackSlotsOffset = kKindSpecificFlagsOffset;
3524  static const int kCheckTypeOffset = kKindSpecificFlagsOffset;
3525  static const int kExternalArrayTypeOffset = kKindSpecificFlagsOffset;
3526
3527  static const int kCompareStateOffset = kStubMajorKeyOffset + 1;
3528  static const int kBinaryOpTypeOffset = kStubMajorKeyOffset + 1;
3529  static const int kHasDeoptimizationSupportOffset = kOptimizableOffset + 1;
3530
3531  static const int kBinaryOpReturnTypeOffset = kBinaryOpTypeOffset + 1;
3532  static const int kAllowOSRAtLoopNestingLevelOffset =
3533      kHasDeoptimizationSupportOffset + 1;
3534
3535  static const int kSafepointTableOffsetOffset = kStackSlotsOffset + kIntSize;
3536  static const int kStackCheckTableOffsetOffset = kStackSlotsOffset + kIntSize;
3537
3538  // Flags layout.
3539  static const int kFlagsICStateShift        = 0;
3540  static const int kFlagsICInLoopShift       = 3;
3541  static const int kFlagsTypeShift           = 4;
3542  static const int kFlagsKindShift           = 8;
3543  static const int kFlagsICHolderShift       = 12;
3544  static const int kFlagsExtraICStateShift   = 13;
3545  static const int kFlagsArgumentsCountShift = 15;
3546
3547  static const int kFlagsICStateMask        = 0x00000007;  // 00000000111
3548  static const int kFlagsICInLoopMask       = 0x00000008;  // 00000001000
3549  static const int kFlagsTypeMask           = 0x000000F0;  // 00001110000
3550  static const int kFlagsKindMask           = 0x00000F00;  // 11110000000
3551  static const int kFlagsCacheInPrototypeMapMask = 0x00001000;
3552  static const int kFlagsExtraICStateMask   = 0x00006000;
3553  static const int kFlagsArgumentsCountMask = 0xFFFF8000;
3554
3555  static const int kFlagsNotUsedInLookup =
3556      (kFlagsICInLoopMask | kFlagsTypeMask | kFlagsCacheInPrototypeMapMask);
3557
3558 private:
3559  DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
3560};
3561
3562
3563// All heap objects have a Map that describes their structure.
3564//  A Map contains information about:
3565//  - Size information about the object
3566//  - How to iterate over an object (for garbage collection)
3567class Map: public HeapObject {
3568 public:
3569  // Instance size.
3570  // Size in bytes or kVariableSizeSentinel if instances do not have
3571  // a fixed size.
3572  inline int instance_size();
3573  inline void set_instance_size(int value);
3574
3575  // Count of properties allocated in the object.
3576  inline int inobject_properties();
3577  inline void set_inobject_properties(int value);
3578
3579  // Count of property fields pre-allocated in the object when first allocated.
3580  inline int pre_allocated_property_fields();
3581  inline void set_pre_allocated_property_fields(int value);
3582
3583  // Instance type.
3584  inline InstanceType instance_type();
3585  inline void set_instance_type(InstanceType value);
3586
3587  // Tells how many unused property fields are available in the
3588  // instance (only used for JSObject in fast mode).
3589  inline int unused_property_fields();
3590  inline void set_unused_property_fields(int value);
3591
3592  // Bit field.
3593  inline byte bit_field();
3594  inline void set_bit_field(byte value);
3595
3596  // Bit field 2.
3597  inline byte bit_field2();
3598  inline void set_bit_field2(byte value);
3599
3600  // Tells whether the object in the prototype property will be used
3601  // for instances created from this function.  If the prototype
3602  // property is set to a value that is not a JSObject, the prototype
3603  // property will not be used to create instances of the function.
3604  // See ECMA-262, 13.2.2.
3605  inline void set_non_instance_prototype(bool value);
3606  inline bool has_non_instance_prototype();
3607
3608  // Tells whether function has special prototype property. If not, prototype
3609  // property will not be created when accessed (will return undefined),
3610  // and construction from this function will not be allowed.
3611  inline void set_function_with_prototype(bool value);
3612  inline bool function_with_prototype();
3613
3614  // Tells whether the instance with this map should be ignored by the
3615  // __proto__ accessor.
3616  inline void set_is_hidden_prototype() {
3617    set_bit_field(bit_field() | (1 << kIsHiddenPrototype));
3618  }
3619
3620  inline bool is_hidden_prototype() {
3621    return ((1 << kIsHiddenPrototype) & bit_field()) != 0;
3622  }
3623
3624  // Records and queries whether the instance has a named interceptor.
3625  inline void set_has_named_interceptor() {
3626    set_bit_field(bit_field() | (1 << kHasNamedInterceptor));
3627  }
3628
3629  inline bool has_named_interceptor() {
3630    return ((1 << kHasNamedInterceptor) & bit_field()) != 0;
3631  }
3632
3633  // Records and queries whether the instance has an indexed interceptor.
3634  inline void set_has_indexed_interceptor() {
3635    set_bit_field(bit_field() | (1 << kHasIndexedInterceptor));
3636  }
3637
3638  inline bool has_indexed_interceptor() {
3639    return ((1 << kHasIndexedInterceptor) & bit_field()) != 0;
3640  }
3641
3642  // Tells whether the instance is undetectable.
3643  // An undetectable object is a special class of JSObject: 'typeof' operator
3644  // returns undefined, ToBoolean returns false. Otherwise it behaves like
3645  // a normal JS object.  It is useful for implementing undetectable
3646  // document.all in Firefox & Safari.
3647  // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
3648  inline void set_is_undetectable() {
3649    set_bit_field(bit_field() | (1 << kIsUndetectable));
3650  }
3651
3652  inline bool is_undetectable() {
3653    return ((1 << kIsUndetectable) & bit_field()) != 0;
3654  }
3655
3656  // Tells whether the instance has a call-as-function handler.
3657  inline void set_has_instance_call_handler() {
3658    set_bit_field(bit_field() | (1 << kHasInstanceCallHandler));
3659  }
3660
3661  inline bool has_instance_call_handler() {
3662    return ((1 << kHasInstanceCallHandler) & bit_field()) != 0;
3663  }
3664
3665  inline void set_is_extensible(bool value);
3666  inline bool is_extensible();
3667
3668  // Tells whether the instance has fast elements.
3669  // Equivalent to instance->GetElementsKind() == FAST_ELEMENTS.
3670  inline void set_has_fast_elements(bool value) {
3671    if (value) {
3672      set_bit_field2(bit_field2() | (1 << kHasFastElements));
3673    } else {
3674      set_bit_field2(bit_field2() & ~(1 << kHasFastElements));
3675    }
3676  }
3677
3678  inline bool has_fast_elements() {
3679    return ((1 << kHasFastElements) & bit_field2()) != 0;
3680  }
3681
3682  // Tells whether an instance has pixel array elements.
3683  inline void set_has_external_array_elements(bool value) {
3684    if (value) {
3685      set_bit_field2(bit_field2() | (1 << kHasExternalArrayElements));
3686    } else {
3687      set_bit_field2(bit_field2() & ~(1 << kHasExternalArrayElements));
3688    }
3689  }
3690
3691  inline bool has_external_array_elements() {
3692    return ((1 << kHasExternalArrayElements) & bit_field2()) != 0;
3693  }
3694
3695  // Tells whether the map is attached to SharedFunctionInfo
3696  // (for inobject slack tracking).
3697  inline void set_attached_to_shared_function_info(bool value);
3698
3699  inline bool attached_to_shared_function_info();
3700
3701  // Tells whether the map is shared between objects that may have different
3702  // behavior. If true, the map should never be modified, instead a clone
3703  // should be created and modified.
3704  inline void set_is_shared(bool value);
3705
3706  inline bool is_shared();
3707
3708  // Tells whether the instance needs security checks when accessing its
3709  // properties.
3710  inline void set_is_access_check_needed(bool access_check_needed);
3711  inline bool is_access_check_needed();
3712
3713  // [prototype]: implicit prototype object.
3714  DECL_ACCESSORS(prototype, Object)
3715
3716  // [constructor]: points back to the function responsible for this map.
3717  DECL_ACCESSORS(constructor, Object)
3718
3719  inline JSFunction* unchecked_constructor();
3720
3721  // [instance descriptors]: describes the object.
3722  DECL_ACCESSORS(instance_descriptors, DescriptorArray)
3723
3724  // [stub cache]: contains stubs compiled for this map.
3725  DECL_ACCESSORS(code_cache, Object)
3726
3727  // [prototype transitions]: cache of prototype transitions.
3728  // Prototype transition is a transition that happens
3729  // when we change object's prototype to a new one.
3730  // Cache format:
3731  //    0: finger - index of the first free cell in the cache
3732  //    1 + 2 * i: prototype
3733  //    2 + 2 * i: target map
3734  DECL_ACCESSORS(prototype_transitions, FixedArray)
3735  inline FixedArray* unchecked_prototype_transitions();
3736
3737  // Lookup in the map's instance descriptors and fill out the result
3738  // with the given holder if the name is found. The holder may be
3739  // NULL when this function is used from the compiler.
3740  void LookupInDescriptors(JSObject* holder,
3741                           String* name,
3742                           LookupResult* result);
3743
3744  MUST_USE_RESULT MaybeObject* CopyDropDescriptors();
3745
3746  MUST_USE_RESULT MaybeObject* CopyNormalized(PropertyNormalizationMode mode,
3747                                              NormalizedMapSharingMode sharing);
3748
3749  // Returns a copy of the map, with all transitions dropped from the
3750  // instance descriptors.
3751  MUST_USE_RESULT MaybeObject* CopyDropTransitions();
3752
3753  // Returns this map if it has the fast elements bit set, otherwise
3754  // returns a copy of the map, with all transitions dropped from the
3755  // descriptors and the fast elements bit set.
3756  MUST_USE_RESULT inline MaybeObject* GetFastElementsMap();
3757
3758  // Returns this map if it has the fast elements bit cleared,
3759  // otherwise returns a copy of the map, with all transitions dropped
3760  // from the descriptors and the fast elements bit cleared.
3761  MUST_USE_RESULT inline MaybeObject* GetSlowElementsMap();
3762
3763  // Returns a new map with all transitions dropped from the descriptors and the
3764  // external array elements bit set.
3765  MUST_USE_RESULT MaybeObject* GetExternalArrayElementsMap(
3766      ExternalArrayType array_type,
3767      bool safe_to_add_transition);
3768
3769  // Returns the property index for name (only valid for FAST MODE).
3770  int PropertyIndexFor(String* name);
3771
3772  // Returns the next free property index (only valid for FAST MODE).
3773  int NextFreePropertyIndex();
3774
3775  // Returns the number of properties described in instance_descriptors.
3776  int NumberOfDescribedProperties();
3777
3778  // Casting.
3779  static inline Map* cast(Object* obj);
3780
3781  // Locate an accessor in the instance descriptor.
3782  AccessorDescriptor* FindAccessor(String* name);
3783
3784  // Code cache operations.
3785
3786  // Clears the code cache.
3787  inline void ClearCodeCache(Heap* heap);
3788
3789  // Update code cache.
3790  MUST_USE_RESULT MaybeObject* UpdateCodeCache(String* name, Code* code);
3791
3792  // Returns the found code or undefined if absent.
3793  Object* FindInCodeCache(String* name, Code::Flags flags);
3794
3795  // Returns the non-negative index of the code object if it is in the
3796  // cache and -1 otherwise.
3797  int IndexInCodeCache(Object* name, Code* code);
3798
3799  // Removes a code object from the code cache at the given index.
3800  void RemoveFromCodeCache(String* name, Code* code, int index);
3801
3802  // For every transition in this map, makes the transition's
3803  // target's prototype pointer point back to this map.
3804  // This is undone in MarkCompactCollector::ClearNonLiveTransitions().
3805  void CreateBackPointers();
3806
3807  // Set all map transitions from this map to dead maps to null.
3808  // Also, restore the original prototype on the targets of these
3809  // transitions, so that we do not process this map again while
3810  // following back pointers.
3811  void ClearNonLiveTransitions(Heap* heap, Object* real_prototype);
3812
3813  // Dispatched behavior.
3814#ifdef OBJECT_PRINT
3815  inline void MapPrint() {
3816    MapPrint(stdout);
3817  }
3818  void MapPrint(FILE* out);
3819#endif
3820#ifdef DEBUG
3821  void MapVerify();
3822  void SharedMapVerify();
3823#endif
3824
3825  inline int visitor_id();
3826  inline void set_visitor_id(int visitor_id);
3827
3828  // Returns the isolate/heap this map belongs to.
3829  inline Isolate* isolate();
3830  inline Heap* heap();
3831
3832  typedef void (*TraverseCallback)(Map* map, void* data);
3833
3834  void TraverseTransitionTree(TraverseCallback callback, void* data);
3835
3836  static const int kMaxCachedPrototypeTransitions = 256;
3837
3838  Object* GetPrototypeTransition(Object* prototype);
3839
3840  MaybeObject* PutPrototypeTransition(Object* prototype, Map* map);
3841
3842  static const int kMaxPreAllocatedPropertyFields = 255;
3843
3844  // Layout description.
3845  static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
3846  static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
3847  static const int kPrototypeOffset = kInstanceAttributesOffset + kIntSize;
3848  static const int kConstructorOffset = kPrototypeOffset + kPointerSize;
3849  static const int kInstanceDescriptorsOffset =
3850      kConstructorOffset + kPointerSize;
3851  static const int kCodeCacheOffset = kInstanceDescriptorsOffset + kPointerSize;
3852  static const int kPrototypeTransitionsOffset =
3853      kCodeCacheOffset + kPointerSize;
3854  static const int kPadStart = kPrototypeTransitionsOffset + kPointerSize;
3855  static const int kSize = MAP_POINTER_ALIGN(kPadStart);
3856
3857  // Layout of pointer fields. Heap iteration code relies on them
3858  // being continiously allocated.
3859  static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
3860  static const int kPointerFieldsEndOffset =
3861      Map::kPrototypeTransitionsOffset + kPointerSize;
3862
3863  // Byte offsets within kInstanceSizesOffset.
3864  static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
3865  static const int kInObjectPropertiesByte = 1;
3866  static const int kInObjectPropertiesOffset =
3867      kInstanceSizesOffset + kInObjectPropertiesByte;
3868  static const int kPreAllocatedPropertyFieldsByte = 2;
3869  static const int kPreAllocatedPropertyFieldsOffset =
3870      kInstanceSizesOffset + kPreAllocatedPropertyFieldsByte;
3871  static const int kVisitorIdByte = 3;
3872  static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
3873
3874  // Byte offsets within kInstanceAttributesOffset attributes.
3875  static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
3876  static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 1;
3877  static const int kBitFieldOffset = kInstanceAttributesOffset + 2;
3878  static const int kBitField2Offset = kInstanceAttributesOffset + 3;
3879
3880  STATIC_CHECK(kInstanceTypeOffset == Internals::kMapInstanceTypeOffset);
3881
3882  // Bit positions for bit field.
3883  static const int kUnused = 0;  // To be used for marking recently used maps.
3884  static const int kHasNonInstancePrototype = 1;
3885  static const int kIsHiddenPrototype = 2;
3886  static const int kHasNamedInterceptor = 3;
3887  static const int kHasIndexedInterceptor = 4;
3888  static const int kIsUndetectable = 5;
3889  static const int kHasInstanceCallHandler = 6;
3890  static const int kIsAccessCheckNeeded = 7;
3891
3892  // Bit positions for bit field 2
3893  static const int kIsExtensible = 0;
3894  static const int kFunctionWithPrototype = 1;
3895  static const int kHasFastElements = 2;
3896  static const int kStringWrapperSafeForDefaultValueOf = 3;
3897  static const int kAttachedToSharedFunctionInfo = 4;
3898  static const int kIsShared = 5;
3899  static const int kHasExternalArrayElements = 6;
3900
3901  // Layout of the default cache. It holds alternating name and code objects.
3902  static const int kCodeCacheEntrySize = 2;
3903  static const int kCodeCacheEntryNameOffset = 0;
3904  static const int kCodeCacheEntryCodeOffset = 1;
3905
3906  typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
3907                              kPointerFieldsEndOffset,
3908                              kSize> BodyDescriptor;
3909
3910 private:
3911  DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
3912};
3913
3914
3915// An abstract superclass, a marker class really, for simple structure classes.
3916// It doesn't carry much functionality but allows struct classes to me
3917// identified in the type system.
3918class Struct: public HeapObject {
3919 public:
3920  inline void InitializeBody(int object_size);
3921  static inline Struct* cast(Object* that);
3922};
3923
3924
3925// Script describes a script which has been added to the VM.
3926class Script: public Struct {
3927 public:
3928  // Script types.
3929  enum Type {
3930    TYPE_NATIVE = 0,
3931    TYPE_EXTENSION = 1,
3932    TYPE_NORMAL = 2
3933  };
3934
3935  // Script compilation types.
3936  enum CompilationType {
3937    COMPILATION_TYPE_HOST = 0,
3938    COMPILATION_TYPE_EVAL = 1
3939  };
3940
3941  // [source]: the script source.
3942  DECL_ACCESSORS(source, Object)
3943
3944  // [name]: the script name.
3945  DECL_ACCESSORS(name, Object)
3946
3947  // [id]: the script id.
3948  DECL_ACCESSORS(id, Object)
3949
3950  // [line_offset]: script line offset in resource from where it was extracted.
3951  DECL_ACCESSORS(line_offset, Smi)
3952
3953  // [column_offset]: script column offset in resource from where it was
3954  // extracted.
3955  DECL_ACCESSORS(column_offset, Smi)
3956
3957  // [data]: additional data associated with this script.
3958  DECL_ACCESSORS(data, Object)
3959
3960  // [context_data]: context data for the context this script was compiled in.
3961  DECL_ACCESSORS(context_data, Object)
3962
3963  // [wrapper]: the wrapper cache.
3964  DECL_ACCESSORS(wrapper, Proxy)
3965
3966  // [type]: the script type.
3967  DECL_ACCESSORS(type, Smi)
3968
3969  // [compilation]: how the the script was compiled.
3970  DECL_ACCESSORS(compilation_type, Smi)
3971
3972  // [line_ends]: FixedArray of line ends positions.
3973  DECL_ACCESSORS(line_ends, Object)
3974
3975  // [eval_from_shared]: for eval scripts the shared funcion info for the
3976  // function from which eval was called.
3977  DECL_ACCESSORS(eval_from_shared, Object)
3978
3979  // [eval_from_instructions_offset]: the instruction offset in the code for the
3980  // function from which eval was called where eval was called.
3981  DECL_ACCESSORS(eval_from_instructions_offset, Smi)
3982
3983  static inline Script* cast(Object* obj);
3984
3985  // If script source is an external string, check that the underlying
3986  // resource is accessible. Otherwise, always return true.
3987  inline bool HasValidSource();
3988
3989#ifdef OBJECT_PRINT
3990  inline void ScriptPrint() {
3991    ScriptPrint(stdout);
3992  }
3993  void ScriptPrint(FILE* out);
3994#endif
3995#ifdef DEBUG
3996  void ScriptVerify();
3997#endif
3998
3999  static const int kSourceOffset = HeapObject::kHeaderSize;
4000  static const int kNameOffset = kSourceOffset + kPointerSize;
4001  static const int kLineOffsetOffset = kNameOffset + kPointerSize;
4002  static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
4003  static const int kDataOffset = kColumnOffsetOffset + kPointerSize;
4004  static const int kContextOffset = kDataOffset + kPointerSize;
4005  static const int kWrapperOffset = kContextOffset + kPointerSize;
4006  static const int kTypeOffset = kWrapperOffset + kPointerSize;
4007  static const int kCompilationTypeOffset = kTypeOffset + kPointerSize;
4008  static const int kLineEndsOffset = kCompilationTypeOffset + kPointerSize;
4009  static const int kIdOffset = kLineEndsOffset + kPointerSize;
4010  static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
4011  static const int kEvalFrominstructionsOffsetOffset =
4012      kEvalFromSharedOffset + kPointerSize;
4013  static const int kSize = kEvalFrominstructionsOffsetOffset + kPointerSize;
4014
4015 private:
4016  DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
4017};
4018
4019
4020// List of builtin functions we want to identify to improve code
4021// generation.
4022//
4023// Each entry has a name of a global object property holding an object
4024// optionally followed by ".prototype", a name of a builtin function
4025// on the object (the one the id is set for), and a label.
4026//
4027// Installation of ids for the selected builtin functions is handled
4028// by the bootstrapper.
4029//
4030// NOTE: Order is important: math functions should be at the end of
4031// the list and MathFloor should be the first math function.
4032#define FUNCTIONS_WITH_ID_LIST(V)                   \
4033  V(Array.prototype, push, ArrayPush)               \
4034  V(Array.prototype, pop, ArrayPop)                 \
4035  V(String.prototype, charCodeAt, StringCharCodeAt) \
4036  V(String.prototype, charAt, StringCharAt)         \
4037  V(String, fromCharCode, StringFromCharCode)       \
4038  V(Math, floor, MathFloor)                         \
4039  V(Math, round, MathRound)                         \
4040  V(Math, ceil, MathCeil)                           \
4041  V(Math, abs, MathAbs)                             \
4042  V(Math, log, MathLog)                             \
4043  V(Math, sin, MathSin)                             \
4044  V(Math, cos, MathCos)                             \
4045  V(Math, tan, MathTan)                             \
4046  V(Math, asin, MathASin)                           \
4047  V(Math, acos, MathACos)                           \
4048  V(Math, atan, MathATan)                           \
4049  V(Math, exp, MathExp)                             \
4050  V(Math, sqrt, MathSqrt)                           \
4051  V(Math, pow, MathPow)
4052
4053
4054enum BuiltinFunctionId {
4055#define DECLARE_FUNCTION_ID(ignored1, ignore2, name)    \
4056  k##name,
4057  FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
4058#undef DECLARE_FUNCTION_ID
4059  // Fake id for a special case of Math.pow. Note, it continues the
4060  // list of math functions.
4061  kMathPowHalf,
4062  kFirstMathFunctionId = kMathFloor
4063};
4064
4065
4066// SharedFunctionInfo describes the JSFunction information that can be
4067// shared by multiple instances of the function.
4068class SharedFunctionInfo: public HeapObject {
4069 public:
4070  // [name]: Function name.
4071  DECL_ACCESSORS(name, Object)
4072
4073  // [code]: Function code.
4074  DECL_ACCESSORS(code, Code)
4075
4076  // [scope_info]: Scope info.
4077  DECL_ACCESSORS(scope_info, SerializedScopeInfo)
4078
4079  // [construct stub]: Code stub for constructing instances of this function.
4080  DECL_ACCESSORS(construct_stub, Code)
4081
4082  inline Code* unchecked_code();
4083
4084  // Returns if this function has been compiled to native code yet.
4085  inline bool is_compiled();
4086
4087  // [length]: The function length - usually the number of declared parameters.
4088  // Use up to 2^30 parameters.
4089  inline int length();
4090  inline void set_length(int value);
4091
4092  // [formal parameter count]: The declared number of parameters.
4093  inline int formal_parameter_count();
4094  inline void set_formal_parameter_count(int value);
4095
4096  // Set the formal parameter count so the function code will be
4097  // called without using argument adaptor frames.
4098  inline void DontAdaptArguments();
4099
4100  // [expected_nof_properties]: Expected number of properties for the function.
4101  inline int expected_nof_properties();
4102  inline void set_expected_nof_properties(int value);
4103
4104  // Inobject slack tracking is the way to reclaim unused inobject space.
4105  //
4106  // The instance size is initially determined by adding some slack to
4107  // expected_nof_properties (to allow for a few extra properties added
4108  // after the constructor). There is no guarantee that the extra space
4109  // will not be wasted.
4110  //
4111  // Here is the algorithm to reclaim the unused inobject space:
4112  // - Detect the first constructor call for this SharedFunctionInfo.
4113  //   When it happens enter the "in progress" state: remember the
4114  //   constructor's initial_map and install a special construct stub that
4115  //   counts constructor calls.
4116  // - While the tracking is in progress create objects filled with
4117  //   one_pointer_filler_map instead of undefined_value. This way they can be
4118  //   resized quickly and safely.
4119  // - Once enough (kGenerousAllocationCount) objects have been created
4120  //   compute the 'slack' (traverse the map transition tree starting from the
4121  //   initial_map and find the lowest value of unused_property_fields).
4122  // - Traverse the transition tree again and decrease the instance size
4123  //   of every map. Existing objects will resize automatically (they are
4124  //   filled with one_pointer_filler_map). All further allocations will
4125  //   use the adjusted instance size.
4126  // - Decrease expected_nof_properties so that an allocations made from
4127  //   another context will use the adjusted instance size too.
4128  // - Exit "in progress" state by clearing the reference to the initial_map
4129  //   and setting the regular construct stub (generic or inline).
4130  //
4131  //  The above is the main event sequence. Some special cases are possible
4132  //  while the tracking is in progress:
4133  //
4134  // - GC occurs.
4135  //   Check if the initial_map is referenced by any live objects (except this
4136  //   SharedFunctionInfo). If it is, continue tracking as usual.
4137  //   If it is not, clear the reference and reset the tracking state. The
4138  //   tracking will be initiated again on the next constructor call.
4139  //
4140  // - The constructor is called from another context.
4141  //   Immediately complete the tracking, perform all the necessary changes
4142  //   to maps. This is  necessary because there is no efficient way to track
4143  //   multiple initial_maps.
4144  //   Proceed to create an object in the current context (with the adjusted
4145  //   size).
4146  //
4147  // - A different constructor function sharing the same SharedFunctionInfo is
4148  //   called in the same context. This could be another closure in the same
4149  //   context, or the first function could have been disposed.
4150  //   This is handled the same way as the previous case.
4151  //
4152  //  Important: inobject slack tracking is not attempted during the snapshot
4153  //  creation.
4154
4155  static const int kGenerousAllocationCount = 8;
4156
4157  // [construction_count]: Counter for constructor calls made during
4158  // the tracking phase.
4159  inline int construction_count();
4160  inline void set_construction_count(int value);
4161
4162  // [initial_map]: initial map of the first function called as a constructor.
4163  // Saved for the duration of the tracking phase.
4164  // This is a weak link (GC resets it to undefined_value if no other live
4165  // object reference this map).
4166  DECL_ACCESSORS(initial_map, Object)
4167
4168  // True if the initial_map is not undefined and the countdown stub is
4169  // installed.
4170  inline bool IsInobjectSlackTrackingInProgress();
4171
4172  // Starts the tracking.
4173  // Stores the initial map and installs the countdown stub.
4174  // IsInobjectSlackTrackingInProgress is normally true after this call,
4175  // except when tracking have not been started (e.g. the map has no unused
4176  // properties or the snapshot is being built).
4177  void StartInobjectSlackTracking(Map* map);
4178
4179  // Completes the tracking.
4180  // IsInobjectSlackTrackingInProgress is false after this call.
4181  void CompleteInobjectSlackTracking();
4182
4183  // Clears the initial_map before the GC marking phase to ensure the reference
4184  // is weak. IsInobjectSlackTrackingInProgress is false after this call.
4185  void DetachInitialMap();
4186
4187  // Restores the link to the initial map after the GC marking phase.
4188  // IsInobjectSlackTrackingInProgress is true after this call.
4189  void AttachInitialMap(Map* map);
4190
4191  // False if there are definitely no live objects created from this function.
4192  // True if live objects _may_ exist (existence not guaranteed).
4193  // May go back from true to false after GC.
4194  inline bool live_objects_may_exist();
4195
4196  inline void set_live_objects_may_exist(bool value);
4197
4198  // [instance class name]: class name for instances.
4199  DECL_ACCESSORS(instance_class_name, Object)
4200
4201  // [function data]: This field holds some additional data for function.
4202  // Currently it either has FunctionTemplateInfo to make benefit the API
4203  // or Smi identifying a builtin function.
4204  // In the long run we don't want all functions to have this field but
4205  // we can fix that when we have a better model for storing hidden data
4206  // on objects.
4207  DECL_ACCESSORS(function_data, Object)
4208
4209  inline bool IsApiFunction();
4210  inline FunctionTemplateInfo* get_api_func_data();
4211  inline bool HasBuiltinFunctionId();
4212  inline BuiltinFunctionId builtin_function_id();
4213
4214  // [script info]: Script from which the function originates.
4215  DECL_ACCESSORS(script, Object)
4216
4217  // [num_literals]: Number of literals used by this function.
4218  inline int num_literals();
4219  inline void set_num_literals(int value);
4220
4221  // [start_position_and_type]: Field used to store both the source code
4222  // position, whether or not the function is a function expression,
4223  // and whether or not the function is a toplevel function. The two
4224  // least significants bit indicates whether the function is an
4225  // expression and the rest contains the source code position.
4226  inline int start_position_and_type();
4227  inline void set_start_position_and_type(int value);
4228
4229  // [debug info]: Debug information.
4230  DECL_ACCESSORS(debug_info, Object)
4231
4232  // [inferred name]: Name inferred from variable or property
4233  // assignment of this function. Used to facilitate debugging and
4234  // profiling of JavaScript code written in OO style, where almost
4235  // all functions are anonymous but are assigned to object
4236  // properties.
4237  DECL_ACCESSORS(inferred_name, String)
4238
4239  // The function's name if it is non-empty, otherwise the inferred name.
4240  String* DebugName();
4241
4242  // Position of the 'function' token in the script source.
4243  inline int function_token_position();
4244  inline void set_function_token_position(int function_token_position);
4245
4246  // Position of this function in the script source.
4247  inline int start_position();
4248  inline void set_start_position(int start_position);
4249
4250  // End position of this function in the script source.
4251  inline int end_position();
4252  inline void set_end_position(int end_position);
4253
4254  // Is this function a function expression in the source code.
4255  inline bool is_expression();
4256  inline void set_is_expression(bool value);
4257
4258  // Is this function a top-level function (scripts, evals).
4259  inline bool is_toplevel();
4260  inline void set_is_toplevel(bool value);
4261
4262  // Bit field containing various information collected by the compiler to
4263  // drive optimization.
4264  inline int compiler_hints();
4265  inline void set_compiler_hints(int value);
4266
4267  // A counter used to determine when to stress the deoptimizer with a
4268  // deopt.
4269  inline Smi* deopt_counter();
4270  inline void set_deopt_counter(Smi* counter);
4271
4272  // Add information on assignments of the form this.x = ...;
4273  void SetThisPropertyAssignmentsInfo(
4274      bool has_only_simple_this_property_assignments,
4275      FixedArray* this_property_assignments);
4276
4277  // Clear information on assignments of the form this.x = ...;
4278  void ClearThisPropertyAssignmentsInfo();
4279
4280  // Indicate that this function only consists of assignments of the form
4281  // this.x = y; where y is either a constant or refers to an argument.
4282  inline bool has_only_simple_this_property_assignments();
4283
4284  // Indicates if this function can be lazy compiled.
4285  // This is used to determine if we can safely flush code from a function
4286  // when doing GC if we expect that the function will no longer be used.
4287  inline bool allows_lazy_compilation();
4288  inline void set_allows_lazy_compilation(bool flag);
4289
4290  // Indicates how many full GCs this function has survived with assigned
4291  // code object. Used to determine when it is relatively safe to flush
4292  // this code object and replace it with lazy compilation stub.
4293  // Age is reset when GC notices that the code object is referenced
4294  // from the stack or compilation cache.
4295  inline int code_age();
4296  inline void set_code_age(int age);
4297
4298  // Indicates whether optimizations have been disabled for this
4299  // shared function info. If a function is repeatedly optimized or if
4300  // we cannot optimize the function we disable optimization to avoid
4301  // spending time attempting to optimize it again.
4302  inline bool optimization_disabled();
4303  inline void set_optimization_disabled(bool value);
4304
4305  // Indicates whether the function is a strict mode function.
4306  inline bool strict_mode();
4307  inline void set_strict_mode(bool value);
4308
4309  // Indicates whether or not the code in the shared function support
4310  // deoptimization.
4311  inline bool has_deoptimization_support();
4312
4313  // Enable deoptimization support through recompiled code.
4314  void EnableDeoptimizationSupport(Code* recompiled);
4315
4316  // Lookup the bailout ID and ASSERT that it exists in the non-optimized
4317  // code, returns whether it asserted (i.e., always true if assertions are
4318  // disabled).
4319  bool VerifyBailoutId(int id);
4320
4321  // Check whether a inlined constructor can be generated with the given
4322  // prototype.
4323  bool CanGenerateInlineConstructor(Object* prototype);
4324
4325  // Prevents further attempts to generate inline constructors.
4326  // To be called if generation failed for any reason.
4327  void ForbidInlineConstructor();
4328
4329  // For functions which only contains this property assignments this provides
4330  // access to the names for the properties assigned.
4331  DECL_ACCESSORS(this_property_assignments, Object)
4332  inline int this_property_assignments_count();
4333  inline void set_this_property_assignments_count(int value);
4334  String* GetThisPropertyAssignmentName(int index);
4335  bool IsThisPropertyAssignmentArgument(int index);
4336  int GetThisPropertyAssignmentArgument(int index);
4337  Object* GetThisPropertyAssignmentConstant(int index);
4338
4339  // [source code]: Source code for the function.
4340  bool HasSourceCode();
4341  Object* GetSourceCode();
4342
4343  inline int opt_count();
4344  inline void set_opt_count(int opt_count);
4345
4346  // Source size of this function.
4347  int SourceSize();
4348
4349  // Calculate the instance size.
4350  int CalculateInstanceSize();
4351
4352  // Calculate the number of in-object properties.
4353  int CalculateInObjectProperties();
4354
4355  // Dispatched behavior.
4356  // Set max_length to -1 for unlimited length.
4357  void SourceCodePrint(StringStream* accumulator, int max_length);
4358#ifdef OBJECT_PRINT
4359  inline void SharedFunctionInfoPrint() {
4360    SharedFunctionInfoPrint(stdout);
4361  }
4362  void SharedFunctionInfoPrint(FILE* out);
4363#endif
4364#ifdef DEBUG
4365  void SharedFunctionInfoVerify();
4366#endif
4367
4368  // Casting.
4369  static inline SharedFunctionInfo* cast(Object* obj);
4370
4371  // Constants.
4372  static const int kDontAdaptArgumentsSentinel = -1;
4373
4374  // Layout description.
4375  // Pointer fields.
4376  static const int kNameOffset = HeapObject::kHeaderSize;
4377  static const int kCodeOffset = kNameOffset + kPointerSize;
4378  static const int kScopeInfoOffset = kCodeOffset + kPointerSize;
4379  static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
4380  static const int kInstanceClassNameOffset =
4381      kConstructStubOffset + kPointerSize;
4382  static const int kFunctionDataOffset =
4383      kInstanceClassNameOffset + kPointerSize;
4384  static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
4385  static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
4386  static const int kInferredNameOffset = kDebugInfoOffset + kPointerSize;
4387  static const int kInitialMapOffset =
4388      kInferredNameOffset + kPointerSize;
4389  static const int kThisPropertyAssignmentsOffset =
4390      kInitialMapOffset + kPointerSize;
4391  static const int kDeoptCounterOffset =
4392      kThisPropertyAssignmentsOffset + kPointerSize;
4393#if V8_HOST_ARCH_32_BIT
4394  // Smi fields.
4395  static const int kLengthOffset =
4396      kDeoptCounterOffset + kPointerSize;
4397  static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
4398  static const int kExpectedNofPropertiesOffset =
4399      kFormalParameterCountOffset + kPointerSize;
4400  static const int kNumLiteralsOffset =
4401      kExpectedNofPropertiesOffset + kPointerSize;
4402  static const int kStartPositionAndTypeOffset =
4403      kNumLiteralsOffset + kPointerSize;
4404  static const int kEndPositionOffset =
4405      kStartPositionAndTypeOffset + kPointerSize;
4406  static const int kFunctionTokenPositionOffset =
4407      kEndPositionOffset + kPointerSize;
4408  static const int kCompilerHintsOffset =
4409      kFunctionTokenPositionOffset + kPointerSize;
4410  static const int kThisPropertyAssignmentsCountOffset =
4411      kCompilerHintsOffset + kPointerSize;
4412  static const int kOptCountOffset =
4413      kThisPropertyAssignmentsCountOffset + kPointerSize;
4414  // Total size.
4415  static const int kSize = kOptCountOffset + kPointerSize;
4416#else
4417  // The only reason to use smi fields instead of int fields
4418  // is to allow iteration without maps decoding during
4419  // garbage collections.
4420  // To avoid wasting space on 64-bit architectures we use
4421  // the following trick: we group integer fields into pairs
4422  // First integer in each pair is shifted left by 1.
4423  // By doing this we guarantee that LSB of each kPointerSize aligned
4424  // word is not set and thus this word cannot be treated as pointer
4425  // to HeapObject during old space traversal.
4426  static const int kLengthOffset =
4427      kDeoptCounterOffset + kPointerSize;
4428  static const int kFormalParameterCountOffset =
4429      kLengthOffset + kIntSize;
4430
4431  static const int kExpectedNofPropertiesOffset =
4432      kFormalParameterCountOffset + kIntSize;
4433  static const int kNumLiteralsOffset =
4434      kExpectedNofPropertiesOffset + kIntSize;
4435
4436  static const int kEndPositionOffset =
4437      kNumLiteralsOffset + kIntSize;
4438  static const int kStartPositionAndTypeOffset =
4439      kEndPositionOffset + kIntSize;
4440
4441  static const int kFunctionTokenPositionOffset =
4442      kStartPositionAndTypeOffset + kIntSize;
4443  static const int kCompilerHintsOffset =
4444      kFunctionTokenPositionOffset + kIntSize;
4445
4446  static const int kThisPropertyAssignmentsCountOffset =
4447      kCompilerHintsOffset + kIntSize;
4448  static const int kOptCountOffset =
4449      kThisPropertyAssignmentsCountOffset + kIntSize;
4450
4451  // Total size.
4452  static const int kSize = kOptCountOffset + kIntSize;
4453
4454#endif
4455
4456  // The construction counter for inobject slack tracking is stored in the
4457  // most significant byte of compiler_hints which is otherwise unused.
4458  // Its offset depends on the endian-ness of the architecture.
4459#if __BYTE_ORDER == __LITTLE_ENDIAN
4460  static const int kConstructionCountOffset = kCompilerHintsOffset + 3;
4461#elif __BYTE_ORDER == __BIG_ENDIAN
4462  static const int kConstructionCountOffset = kCompilerHintsOffset + 0;
4463#else
4464#error Unknown byte ordering
4465#endif
4466
4467  static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
4468
4469  typedef FixedBodyDescriptor<kNameOffset,
4470                              kThisPropertyAssignmentsOffset + kPointerSize,
4471                              kSize> BodyDescriptor;
4472
4473  // Bit positions in start_position_and_type.
4474  // The source code start position is in the 30 most significant bits of
4475  // the start_position_and_type field.
4476  static const int kIsExpressionBit = 0;
4477  static const int kIsTopLevelBit   = 1;
4478  static const int kStartPositionShift = 2;
4479  static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
4480
4481  // Bit positions in compiler_hints.
4482  static const int kHasOnlySimpleThisPropertyAssignments = 0;
4483  static const int kAllowLazyCompilation = 1;
4484  static const int kLiveObjectsMayExist = 2;
4485  static const int kCodeAgeShift = 3;
4486  static const int kCodeAgeMask = 0x7;
4487  static const int kOptimizationDisabled = 6;
4488  static const int kStrictModeFunction = 7;
4489
4490 private:
4491#if V8_HOST_ARCH_32_BIT
4492  // On 32 bit platforms, compiler hints is a smi.
4493  static const int kCompilerHintsSmiTagSize = kSmiTagSize;
4494  static const int kCompilerHintsSize = kPointerSize;
4495#else
4496  // On 64 bit platforms, compiler hints is not a smi, see comment above.
4497  static const int kCompilerHintsSmiTagSize = 0;
4498  static const int kCompilerHintsSize = kIntSize;
4499#endif
4500
4501 public:
4502  // Constants for optimizing codegen for strict mode function tests.
4503  // Allows to use byte-widgh instructions.
4504  static const int kStrictModeBitWithinByte =
4505      (kStrictModeFunction + kCompilerHintsSmiTagSize) % kBitsPerByte;
4506
4507#if __BYTE_ORDER == __LITTLE_ENDIAN
4508  static const int kStrictModeByteOffset = kCompilerHintsOffset +
4509    (kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte;
4510#elif __BYTE_ORDER == __BIG_ENDIAN
4511  static const int kStrictModeByteOffset = kCompilerHintsOffset +
4512    (kCompilerHintsSize - 1) -
4513    ((kStrictModeFunction + kCompilerHintsSmiTagSize) / kBitsPerByte);
4514#else
4515#error Unknown byte ordering
4516#endif
4517
4518 private:
4519  DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
4520};
4521
4522
4523// JSFunction describes JavaScript functions.
4524class JSFunction: public JSObject {
4525 public:
4526  // [prototype_or_initial_map]:
4527  DECL_ACCESSORS(prototype_or_initial_map, Object)
4528
4529  // [shared_function_info]: The information about the function that
4530  // can be shared by instances.
4531  DECL_ACCESSORS(shared, SharedFunctionInfo)
4532
4533  inline SharedFunctionInfo* unchecked_shared();
4534
4535  // [context]: The context for this function.
4536  inline Context* context();
4537  inline Object* unchecked_context();
4538  inline void set_context(Object* context);
4539
4540  // [code]: The generated code object for this function.  Executed
4541  // when the function is invoked, e.g. foo() or new foo(). See
4542  // [[Call]] and [[Construct]] description in ECMA-262, section
4543  // 8.6.2, page 27.
4544  inline Code* code();
4545  inline void set_code(Code* code);
4546  inline void ReplaceCode(Code* code);
4547
4548  inline Code* unchecked_code();
4549
4550  // Tells whether this function is builtin.
4551  inline bool IsBuiltin();
4552
4553  // Tells whether or not the function needs arguments adaption.
4554  inline bool NeedsArgumentsAdaption();
4555
4556  // Tells whether or not this function has been optimized.
4557  inline bool IsOptimized();
4558
4559  // Tells whether or not this function can be optimized.
4560  inline bool IsOptimizable();
4561
4562  // Mark this function for lazy recompilation. The function will be
4563  // recompiled the next time it is executed.
4564  void MarkForLazyRecompilation();
4565
4566  // Tells whether or not the function is already marked for lazy
4567  // recompilation.
4568  inline bool IsMarkedForLazyRecompilation();
4569
4570  // Compute a hash code for the source code of this function.
4571  uint32_t SourceHash();
4572
4573  // Check whether or not this function is inlineable.
4574  bool IsInlineable();
4575
4576  // [literals]: Fixed array holding the materialized literals.
4577  //
4578  // If the function contains object, regexp or array literals, the
4579  // literals array prefix contains the object, regexp, and array
4580  // function to be used when creating these literals.  This is
4581  // necessary so that we do not dynamically lookup the object, regexp
4582  // or array functions.  Performing a dynamic lookup, we might end up
4583  // using the functions from a new context that we should not have
4584  // access to.
4585  DECL_ACCESSORS(literals, FixedArray)
4586
4587  // The initial map for an object created by this constructor.
4588  inline Map* initial_map();
4589  inline void set_initial_map(Map* value);
4590  inline bool has_initial_map();
4591
4592  // Get and set the prototype property on a JSFunction. If the
4593  // function has an initial map the prototype is set on the initial
4594  // map. Otherwise, the prototype is put in the initial map field
4595  // until an initial map is needed.
4596  inline bool has_prototype();
4597  inline bool has_instance_prototype();
4598  inline Object* prototype();
4599  inline Object* instance_prototype();
4600  Object* SetInstancePrototype(Object* value);
4601  MUST_USE_RESULT MaybeObject* SetPrototype(Object* value);
4602
4603  // After prototype is removed, it will not be created when accessed, and
4604  // [[Construct]] from this function will not be allowed.
4605  Object* RemovePrototype();
4606  inline bool should_have_prototype();
4607
4608  // Accessor for this function's initial map's [[class]]
4609  // property. This is primarily used by ECMA native functions.  This
4610  // method sets the class_name field of this function's initial map
4611  // to a given value. It creates an initial map if this function does
4612  // not have one. Note that this method does not copy the initial map
4613  // if it has one already, but simply replaces it with the new value.
4614  // Instances created afterwards will have a map whose [[class]] is
4615  // set to 'value', but there is no guarantees on instances created
4616  // before.
4617  Object* SetInstanceClassName(String* name);
4618
4619  // Returns if this function has been compiled to native code yet.
4620  inline bool is_compiled();
4621
4622  // [next_function_link]: Field for linking functions. This list is treated as
4623  // a weak list by the GC.
4624  DECL_ACCESSORS(next_function_link, Object)
4625
4626  // Prints the name of the function using PrintF.
4627  inline void PrintName() {
4628    PrintName(stdout);
4629  }
4630  void PrintName(FILE* out);
4631
4632  // Casting.
4633  static inline JSFunction* cast(Object* obj);
4634
4635  // Iterates the objects, including code objects indirectly referenced
4636  // through pointers to the first instruction in the code object.
4637  void JSFunctionIterateBody(int object_size, ObjectVisitor* v);
4638
4639  // Dispatched behavior.
4640#ifdef OBJECT_PRINT
4641  inline void JSFunctionPrint() {
4642    JSFunctionPrint(stdout);
4643  }
4644  void JSFunctionPrint(FILE* out);
4645#endif
4646#ifdef DEBUG
4647  void JSFunctionVerify();
4648#endif
4649
4650  // Returns the number of allocated literals.
4651  inline int NumberOfLiterals();
4652
4653  // Retrieve the global context from a function's literal array.
4654  static Context* GlobalContextFromLiterals(FixedArray* literals);
4655
4656  // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
4657  // kSize) is weak and has special handling during garbage collection.
4658  static const int kCodeEntryOffset = JSObject::kHeaderSize;
4659  static const int kPrototypeOrInitialMapOffset =
4660      kCodeEntryOffset + kPointerSize;
4661  static const int kSharedFunctionInfoOffset =
4662      kPrototypeOrInitialMapOffset + kPointerSize;
4663  static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
4664  static const int kLiteralsOffset = kContextOffset + kPointerSize;
4665  static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
4666  static const int kNextFunctionLinkOffset = kNonWeakFieldsEndOffset;
4667  static const int kSize = kNextFunctionLinkOffset + kPointerSize;
4668
4669  // Layout of the literals array.
4670  static const int kLiteralsPrefixSize = 1;
4671  static const int kLiteralGlobalContextIndex = 0;
4672 private:
4673  DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
4674};
4675
4676
4677// JSGlobalProxy's prototype must be a JSGlobalObject or null,
4678// and the prototype is hidden. JSGlobalProxy always delegates
4679// property accesses to its prototype if the prototype is not null.
4680//
4681// A JSGlobalProxy can be reinitialized which will preserve its identity.
4682//
4683// Accessing a JSGlobalProxy requires security check.
4684
4685class JSGlobalProxy : public JSObject {
4686 public:
4687  // [context]: the owner global context of this proxy object.
4688  // It is null value if this object is not used by any context.
4689  DECL_ACCESSORS(context, Object)
4690
4691  // Casting.
4692  static inline JSGlobalProxy* cast(Object* obj);
4693
4694  // Dispatched behavior.
4695#ifdef OBJECT_PRINT
4696  inline void JSGlobalProxyPrint() {
4697    JSGlobalProxyPrint(stdout);
4698  }
4699  void JSGlobalProxyPrint(FILE* out);
4700#endif
4701#ifdef DEBUG
4702  void JSGlobalProxyVerify();
4703#endif
4704
4705  // Layout description.
4706  static const int kContextOffset = JSObject::kHeaderSize;
4707  static const int kSize = kContextOffset + kPointerSize;
4708
4709 private:
4710
4711  DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
4712};
4713
4714
4715// Forward declaration.
4716class JSBuiltinsObject;
4717class JSGlobalPropertyCell;
4718
4719// Common super class for JavaScript global objects and the special
4720// builtins global objects.
4721class GlobalObject: public JSObject {
4722 public:
4723  // [builtins]: the object holding the runtime routines written in JS.
4724  DECL_ACCESSORS(builtins, JSBuiltinsObject)
4725
4726  // [global context]: the global context corresponding to this global object.
4727  DECL_ACCESSORS(global_context, Context)
4728
4729  // [global receiver]: the global receiver object of the context
4730  DECL_ACCESSORS(global_receiver, JSObject)
4731
4732  // Retrieve the property cell used to store a property.
4733  JSGlobalPropertyCell* GetPropertyCell(LookupResult* result);
4734
4735  // This is like GetProperty, but is used when you know the lookup won't fail
4736  // by throwing an exception.  This is for the debug and builtins global
4737  // objects, where it is known which properties can be expected to be present
4738  // on the object.
4739  Object* GetPropertyNoExceptionThrown(String* key) {
4740    Object* answer = GetProperty(key)->ToObjectUnchecked();
4741    return answer;
4742  }
4743
4744  // Ensure that the global object has a cell for the given property name.
4745  MUST_USE_RESULT MaybeObject* EnsurePropertyCell(String* name);
4746
4747  // Casting.
4748  static inline GlobalObject* cast(Object* obj);
4749
4750  // Layout description.
4751  static const int kBuiltinsOffset = JSObject::kHeaderSize;
4752  static const int kGlobalContextOffset = kBuiltinsOffset + kPointerSize;
4753  static const int kGlobalReceiverOffset = kGlobalContextOffset + kPointerSize;
4754  static const int kHeaderSize = kGlobalReceiverOffset + kPointerSize;
4755
4756 private:
4757  friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
4758
4759  DISALLOW_IMPLICIT_CONSTRUCTORS(GlobalObject);
4760};
4761
4762
4763// JavaScript global object.
4764class JSGlobalObject: public GlobalObject {
4765 public:
4766
4767  // Casting.
4768  static inline JSGlobalObject* cast(Object* obj);
4769
4770  // Dispatched behavior.
4771#ifdef OBJECT_PRINT
4772  inline void JSGlobalObjectPrint() {
4773    JSGlobalObjectPrint(stdout);
4774  }
4775  void JSGlobalObjectPrint(FILE* out);
4776#endif
4777#ifdef DEBUG
4778  void JSGlobalObjectVerify();
4779#endif
4780
4781  // Layout description.
4782  static const int kSize = GlobalObject::kHeaderSize;
4783
4784 private:
4785  DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
4786};
4787
4788
4789// Builtins global object which holds the runtime routines written in
4790// JavaScript.
4791class JSBuiltinsObject: public GlobalObject {
4792 public:
4793  // Accessors for the runtime routines written in JavaScript.
4794  inline Object* javascript_builtin(Builtins::JavaScript id);
4795  inline void set_javascript_builtin(Builtins::JavaScript id, Object* value);
4796
4797  // Accessors for code of the runtime routines written in JavaScript.
4798  inline Code* javascript_builtin_code(Builtins::JavaScript id);
4799  inline void set_javascript_builtin_code(Builtins::JavaScript id, Code* value);
4800
4801  // Casting.
4802  static inline JSBuiltinsObject* cast(Object* obj);
4803
4804  // Dispatched behavior.
4805#ifdef OBJECT_PRINT
4806  inline void JSBuiltinsObjectPrint() {
4807    JSBuiltinsObjectPrint(stdout);
4808  }
4809  void JSBuiltinsObjectPrint(FILE* out);
4810#endif
4811#ifdef DEBUG
4812  void JSBuiltinsObjectVerify();
4813#endif
4814
4815  // Layout description.  The size of the builtins object includes
4816  // room for two pointers per runtime routine written in javascript
4817  // (function and code object).
4818  static const int kJSBuiltinsCount = Builtins::id_count;
4819  static const int kJSBuiltinsOffset = GlobalObject::kHeaderSize;
4820  static const int kJSBuiltinsCodeOffset =
4821      GlobalObject::kHeaderSize + (kJSBuiltinsCount * kPointerSize);
4822  static const int kSize =
4823      kJSBuiltinsCodeOffset + (kJSBuiltinsCount * kPointerSize);
4824
4825  static int OffsetOfFunctionWithId(Builtins::JavaScript id) {
4826    return kJSBuiltinsOffset + id * kPointerSize;
4827  }
4828
4829  static int OffsetOfCodeWithId(Builtins::JavaScript id) {
4830    return kJSBuiltinsCodeOffset + id * kPointerSize;
4831  }
4832
4833 private:
4834  DISALLOW_IMPLICIT_CONSTRUCTORS(JSBuiltinsObject);
4835};
4836
4837
4838// Representation for JS Wrapper objects, String, Number, Boolean, Date, etc.
4839class JSValue: public JSObject {
4840 public:
4841  // [value]: the object being wrapped.
4842  DECL_ACCESSORS(value, Object)
4843
4844  // Casting.
4845  static inline JSValue* cast(Object* obj);
4846
4847  // Dispatched behavior.
4848#ifdef OBJECT_PRINT
4849  inline void JSValuePrint() {
4850    JSValuePrint(stdout);
4851  }
4852  void JSValuePrint(FILE* out);
4853#endif
4854#ifdef DEBUG
4855  void JSValueVerify();
4856#endif
4857
4858  // Layout description.
4859  static const int kValueOffset = JSObject::kHeaderSize;
4860  static const int kSize = kValueOffset + kPointerSize;
4861
4862 private:
4863  DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
4864};
4865
4866
4867// Representation of message objects used for error reporting through
4868// the API. The messages are formatted in JavaScript so this object is
4869// a real JavaScript object. The information used for formatting the
4870// error messages are not directly accessible from JavaScript to
4871// prevent leaking information to user code called during error
4872// formatting.
4873class JSMessageObject: public JSObject {
4874 public:
4875  // [type]: the type of error message.
4876  DECL_ACCESSORS(type, String)
4877
4878  // [arguments]: the arguments for formatting the error message.
4879  DECL_ACCESSORS(arguments, JSArray)
4880
4881  // [script]: the script from which the error message originated.
4882  DECL_ACCESSORS(script, Object)
4883
4884  // [stack_trace]: the stack trace for this error message.
4885  DECL_ACCESSORS(stack_trace, Object)
4886
4887  // [stack_frames]: an array of stack frames for this error object.
4888  DECL_ACCESSORS(stack_frames, Object)
4889
4890  // [start_position]: the start position in the script for the error message.
4891  inline int start_position();
4892  inline void set_start_position(int value);
4893
4894  // [end_position]: the end position in the script for the error message.
4895  inline int end_position();
4896  inline void set_end_position(int value);
4897
4898  // Casting.
4899  static inline JSMessageObject* cast(Object* obj);
4900
4901  // Dispatched behavior.
4902#ifdef OBJECT_PRINT
4903  inline void JSMessageObjectPrint() {
4904    JSMessageObjectPrint(stdout);
4905  }
4906  void JSMessageObjectPrint(FILE* out);
4907#endif
4908#ifdef DEBUG
4909  void JSMessageObjectVerify();
4910#endif
4911
4912  // Layout description.
4913  static const int kTypeOffset = JSObject::kHeaderSize;
4914  static const int kArgumentsOffset = kTypeOffset + kPointerSize;
4915  static const int kScriptOffset = kArgumentsOffset + kPointerSize;
4916  static const int kStackTraceOffset = kScriptOffset + kPointerSize;
4917  static const int kStackFramesOffset = kStackTraceOffset + kPointerSize;
4918  static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
4919  static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
4920  static const int kSize = kEndPositionOffset + kPointerSize;
4921
4922  typedef FixedBodyDescriptor<HeapObject::kMapOffset,
4923                              kStackFramesOffset + kPointerSize,
4924                              kSize> BodyDescriptor;
4925};
4926
4927
4928// Regular expressions
4929// The regular expression holds a single reference to a FixedArray in
4930// the kDataOffset field.
4931// The FixedArray contains the following data:
4932// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
4933// - reference to the original source string
4934// - reference to the original flag string
4935// If it is an atom regexp
4936// - a reference to a literal string to search for
4937// If it is an irregexp regexp:
4938// - a reference to code for ASCII inputs (bytecode or compiled).
4939// - a reference to code for UC16 inputs (bytecode or compiled).
4940// - max number of registers used by irregexp implementations.
4941// - number of capture registers (output values) of the regexp.
4942class JSRegExp: public JSObject {
4943 public:
4944  // Meaning of Type:
4945  // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
4946  // ATOM: A simple string to match against using an indexOf operation.
4947  // IRREGEXP: Compiled with Irregexp.
4948  // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
4949  enum Type { NOT_COMPILED, ATOM, IRREGEXP };
4950  enum Flag { NONE = 0, GLOBAL = 1, IGNORE_CASE = 2, MULTILINE = 4 };
4951
4952  class Flags {
4953   public:
4954    explicit Flags(uint32_t value) : value_(value) { }
4955    bool is_global() { return (value_ & GLOBAL) != 0; }
4956    bool is_ignore_case() { return (value_ & IGNORE_CASE) != 0; }
4957    bool is_multiline() { return (value_ & MULTILINE) != 0; }
4958    uint32_t value() { return value_; }
4959   private:
4960    uint32_t value_;
4961  };
4962
4963  DECL_ACCESSORS(data, Object)
4964
4965  inline Type TypeTag();
4966  inline int CaptureCount();
4967  inline Flags GetFlags();
4968  inline String* Pattern();
4969  inline Object* DataAt(int index);
4970  // Set implementation data after the object has been prepared.
4971  inline void SetDataAt(int index, Object* value);
4972  static int code_index(bool is_ascii) {
4973    if (is_ascii) {
4974      return kIrregexpASCIICodeIndex;
4975    } else {
4976      return kIrregexpUC16CodeIndex;
4977    }
4978  }
4979
4980  static inline JSRegExp* cast(Object* obj);
4981
4982  // Dispatched behavior.
4983#ifdef DEBUG
4984  void JSRegExpVerify();
4985#endif
4986
4987  static const int kDataOffset = JSObject::kHeaderSize;
4988  static const int kSize = kDataOffset + kPointerSize;
4989
4990  // Indices in the data array.
4991  static const int kTagIndex = 0;
4992  static const int kSourceIndex = kTagIndex + 1;
4993  static const int kFlagsIndex = kSourceIndex + 1;
4994  static const int kDataIndex = kFlagsIndex + 1;
4995  // The data fields are used in different ways depending on the
4996  // value of the tag.
4997  // Atom regexps (literal strings).
4998  static const int kAtomPatternIndex = kDataIndex;
4999
5000  static const int kAtomDataSize = kAtomPatternIndex + 1;
5001
5002  // Irregexp compiled code or bytecode for ASCII. If compilation
5003  // fails, this fields hold an exception object that should be
5004  // thrown if the regexp is used again.
5005  static const int kIrregexpASCIICodeIndex = kDataIndex;
5006  // Irregexp compiled code or bytecode for UC16.  If compilation
5007  // fails, this fields hold an exception object that should be
5008  // thrown if the regexp is used again.
5009  static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
5010  // Maximal number of registers used by either ASCII or UC16.
5011  // Only used to check that there is enough stack space
5012  static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
5013  // Number of captures in the compiled regexp.
5014  static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
5015
5016  static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
5017
5018  // Offsets directly into the data fixed array.
5019  static const int kDataTagOffset =
5020      FixedArray::kHeaderSize + kTagIndex * kPointerSize;
5021  static const int kDataAsciiCodeOffset =
5022      FixedArray::kHeaderSize + kIrregexpASCIICodeIndex * kPointerSize;
5023  static const int kDataUC16CodeOffset =
5024      FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
5025  static const int kIrregexpCaptureCountOffset =
5026      FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
5027
5028  // In-object fields.
5029  static const int kSourceFieldIndex = 0;
5030  static const int kGlobalFieldIndex = 1;
5031  static const int kIgnoreCaseFieldIndex = 2;
5032  static const int kMultilineFieldIndex = 3;
5033  static const int kLastIndexFieldIndex = 4;
5034  static const int kInObjectFieldCount = 5;
5035};
5036
5037
5038class CompilationCacheShape {
5039 public:
5040  static inline bool IsMatch(HashTableKey* key, Object* value) {
5041    return key->IsMatch(value);
5042  }
5043
5044  static inline uint32_t Hash(HashTableKey* key) {
5045    return key->Hash();
5046  }
5047
5048  static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5049    return key->HashForObject(object);
5050  }
5051
5052  MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
5053    return key->AsObject();
5054  }
5055
5056  static const int kPrefixSize = 0;
5057  static const int kEntrySize = 2;
5058};
5059
5060
5061class CompilationCacheTable: public HashTable<CompilationCacheShape,
5062                                              HashTableKey*> {
5063 public:
5064  // Find cached value for a string key, otherwise return null.
5065  Object* Lookup(String* src);
5066  Object* LookupEval(String* src, Context* context, StrictModeFlag strict_mode);
5067  Object* LookupRegExp(String* source, JSRegExp::Flags flags);
5068  MaybeObject* Put(String* src, Object* value);
5069  MaybeObject* PutEval(String* src,
5070                       Context* context,
5071                       SharedFunctionInfo* value);
5072  MaybeObject* PutRegExp(String* src, JSRegExp::Flags flags, FixedArray* value);
5073
5074  // Remove given value from cache.
5075  void Remove(Object* value);
5076
5077  static inline CompilationCacheTable* cast(Object* obj);
5078
5079 private:
5080  DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
5081};
5082
5083
5084class CodeCache: public Struct {
5085 public:
5086  DECL_ACCESSORS(default_cache, FixedArray)
5087  DECL_ACCESSORS(normal_type_cache, Object)
5088
5089  // Add the code object to the cache.
5090  MUST_USE_RESULT MaybeObject* Update(String* name, Code* code);
5091
5092  // Lookup code object in the cache. Returns code object if found and undefined
5093  // if not.
5094  Object* Lookup(String* name, Code::Flags flags);
5095
5096  // Get the internal index of a code object in the cache. Returns -1 if the
5097  // code object is not in that cache. This index can be used to later call
5098  // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
5099  // RemoveByIndex.
5100  int GetIndex(Object* name, Code* code);
5101
5102  // Remove an object from the cache with the provided internal index.
5103  void RemoveByIndex(Object* name, Code* code, int index);
5104
5105  static inline CodeCache* cast(Object* obj);
5106
5107#ifdef OBJECT_PRINT
5108  inline void CodeCachePrint() {
5109    CodeCachePrint(stdout);
5110  }
5111  void CodeCachePrint(FILE* out);
5112#endif
5113#ifdef DEBUG
5114  void CodeCacheVerify();
5115#endif
5116
5117  static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
5118  static const int kNormalTypeCacheOffset =
5119      kDefaultCacheOffset + kPointerSize;
5120  static const int kSize = kNormalTypeCacheOffset + kPointerSize;
5121
5122 private:
5123  MUST_USE_RESULT MaybeObject* UpdateDefaultCache(String* name, Code* code);
5124  MUST_USE_RESULT MaybeObject* UpdateNormalTypeCache(String* name, Code* code);
5125  Object* LookupDefaultCache(String* name, Code::Flags flags);
5126  Object* LookupNormalTypeCache(String* name, Code::Flags flags);
5127
5128  // Code cache layout of the default cache. Elements are alternating name and
5129  // code objects for non normal load/store/call IC's.
5130  static const int kCodeCacheEntrySize = 2;
5131  static const int kCodeCacheEntryNameOffset = 0;
5132  static const int kCodeCacheEntryCodeOffset = 1;
5133
5134  DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
5135};
5136
5137
5138class CodeCacheHashTableShape {
5139 public:
5140  static inline bool IsMatch(HashTableKey* key, Object* value) {
5141    return key->IsMatch(value);
5142  }
5143
5144  static inline uint32_t Hash(HashTableKey* key) {
5145    return key->Hash();
5146  }
5147
5148  static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
5149    return key->HashForObject(object);
5150  }
5151
5152  MUST_USE_RESULT static MaybeObject* AsObject(HashTableKey* key) {
5153    return key->AsObject();
5154  }
5155
5156  static const int kPrefixSize = 0;
5157  static const int kEntrySize = 2;
5158};
5159
5160
5161class CodeCacheHashTable: public HashTable<CodeCacheHashTableShape,
5162                                           HashTableKey*> {
5163 public:
5164  Object* Lookup(String* name, Code::Flags flags);
5165  MUST_USE_RESULT MaybeObject* Put(String* name, Code* code);
5166
5167  int GetIndex(String* name, Code::Flags flags);
5168  void RemoveByIndex(int index);
5169
5170  static inline CodeCacheHashTable* cast(Object* obj);
5171
5172  // Initial size of the fixed array backing the hash table.
5173  static const int kInitialSize = 64;
5174
5175 private:
5176  DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
5177};
5178
5179
5180enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
5181enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
5182
5183
5184class StringHasher {
5185 public:
5186  explicit inline StringHasher(int length);
5187
5188  // Returns true if the hash of this string can be computed without
5189  // looking at the contents.
5190  inline bool has_trivial_hash();
5191
5192  // Add a character to the hash and update the array index calculation.
5193  inline void AddCharacter(uc32 c);
5194
5195  // Adds a character to the hash but does not update the array index
5196  // calculation.  This can only be called when it has been verified
5197  // that the input is not an array index.
5198  inline void AddCharacterNoIndex(uc32 c);
5199
5200  // Returns the value to store in the hash field of a string with
5201  // the given length and contents.
5202  uint32_t GetHashField();
5203
5204  // Returns true if the characters seen so far make up a legal array
5205  // index.
5206  bool is_array_index() { return is_array_index_; }
5207
5208  bool is_valid() { return is_valid_; }
5209
5210  void invalidate() { is_valid_ = false; }
5211
5212  // Calculated hash value for a string consisting of 1 to
5213  // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
5214  // value is represented decimal value.
5215  static uint32_t MakeArrayIndexHash(uint32_t value, int length);
5216
5217 private:
5218
5219  uint32_t array_index() {
5220    ASSERT(is_array_index());
5221    return array_index_;
5222  }
5223
5224  inline uint32_t GetHash();
5225
5226  int length_;
5227  uint32_t raw_running_hash_;
5228  uint32_t array_index_;
5229  bool is_array_index_;
5230  bool is_first_char_;
5231  bool is_valid_;
5232  friend class TwoCharHashTableKey;
5233};
5234
5235
5236// Calculates string hash.
5237template <typename schar>
5238inline uint32_t HashSequentialString(const schar* chars, int length);
5239
5240
5241// The characteristics of a string are stored in its map.  Retrieving these
5242// few bits of information is moderately expensive, involving two memory
5243// loads where the second is dependent on the first.  To improve efficiency
5244// the shape of the string is given its own class so that it can be retrieved
5245// once and used for several string operations.  A StringShape is small enough
5246// to be passed by value and is immutable, but be aware that flattening a
5247// string can potentially alter its shape.  Also be aware that a GC caused by
5248// something else can alter the shape of a string due to ConsString
5249// shortcutting.  Keeping these restrictions in mind has proven to be error-
5250// prone and so we no longer put StringShapes in variables unless there is a
5251// concrete performance benefit at that particular point in the code.
5252class StringShape BASE_EMBEDDED {
5253 public:
5254  inline explicit StringShape(String* s);
5255  inline explicit StringShape(Map* s);
5256  inline explicit StringShape(InstanceType t);
5257  inline bool IsSequential();
5258  inline bool IsExternal();
5259  inline bool IsCons();
5260  inline bool IsExternalAscii();
5261  inline bool IsExternalTwoByte();
5262  inline bool IsSequentialAscii();
5263  inline bool IsSequentialTwoByte();
5264  inline bool IsSymbol();
5265  inline StringRepresentationTag representation_tag();
5266  inline uint32_t full_representation_tag();
5267  inline uint32_t size_tag();
5268#ifdef DEBUG
5269  inline uint32_t type() { return type_; }
5270  inline void invalidate() { valid_ = false; }
5271  inline bool valid() { return valid_; }
5272#else
5273  inline void invalidate() { }
5274#endif
5275 private:
5276  uint32_t type_;
5277#ifdef DEBUG
5278  inline void set_valid() { valid_ = true; }
5279  bool valid_;
5280#else
5281  inline void set_valid() { }
5282#endif
5283};
5284
5285
5286// The String abstract class captures JavaScript string values:
5287//
5288// Ecma-262:
5289//  4.3.16 String Value
5290//    A string value is a member of the type String and is a finite
5291//    ordered sequence of zero or more 16-bit unsigned integer values.
5292//
5293// All string values have a length field.
5294class String: public HeapObject {
5295 public:
5296  // Get and set the length of the string.
5297  inline int length();
5298  inline void set_length(int value);
5299
5300  // Get and set the hash field of the string.
5301  inline uint32_t hash_field();
5302  inline void set_hash_field(uint32_t value);
5303
5304  inline bool IsAsciiRepresentation();
5305  inline bool IsTwoByteRepresentation();
5306
5307  // Returns whether this string has ascii chars, i.e. all of them can
5308  // be ascii encoded.  This might be the case even if the string is
5309  // two-byte.  Such strings may appear when the embedder prefers
5310  // two-byte external representations even for ascii data.
5311  //
5312  // NOTE: this should be considered only a hint.  False negatives are
5313  // possible.
5314  inline bool HasOnlyAsciiChars();
5315
5316  // Get and set individual two byte chars in the string.
5317  inline void Set(int index, uint16_t value);
5318  // Get individual two byte char in the string.  Repeated calls
5319  // to this method are not efficient unless the string is flat.
5320  inline uint16_t Get(int index);
5321
5322  // Try to flatten the string.  Checks first inline to see if it is
5323  // necessary.  Does nothing if the string is not a cons string.
5324  // Flattening allocates a sequential string with the same data as
5325  // the given string and mutates the cons string to a degenerate
5326  // form, where the first component is the new sequential string and
5327  // the second component is the empty string.  If allocation fails,
5328  // this function returns a failure.  If flattening succeeds, this
5329  // function returns the sequential string that is now the first
5330  // component of the cons string.
5331  //
5332  // Degenerate cons strings are handled specially by the garbage
5333  // collector (see IsShortcutCandidate).
5334  //
5335  // Use FlattenString from Handles.cc to flatten even in case an
5336  // allocation failure happens.
5337  inline MaybeObject* TryFlatten(PretenureFlag pretenure = NOT_TENURED);
5338
5339  // Convenience function.  Has exactly the same behavior as
5340  // TryFlatten(), except in the case of failure returns the original
5341  // string.
5342  inline String* TryFlattenGetString(PretenureFlag pretenure = NOT_TENURED);
5343
5344  Vector<const char> ToAsciiVector();
5345  Vector<const uc16> ToUC16Vector();
5346
5347  // Mark the string as an undetectable object. It only applies to
5348  // ascii and two byte string types.
5349  bool MarkAsUndetectable();
5350
5351  // Return a substring.
5352  MUST_USE_RESULT MaybeObject* SubString(int from,
5353                                         int to,
5354                                         PretenureFlag pretenure = NOT_TENURED);
5355
5356  // String equality operations.
5357  inline bool Equals(String* other);
5358  bool IsEqualTo(Vector<const char> str);
5359  bool IsAsciiEqualTo(Vector<const char> str);
5360  bool IsTwoByteEqualTo(Vector<const uc16> str);
5361
5362  // Return a UTF8 representation of the string.  The string is null
5363  // terminated but may optionally contain nulls.  Length is returned
5364  // in length_output if length_output is not a null pointer  The string
5365  // should be nearly flat, otherwise the performance of this method may
5366  // be very slow (quadratic in the length).  Setting robustness_flag to
5367  // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust  This means it
5368  // handles unexpected data without causing assert failures and it does not
5369  // do any heap allocations.  This is useful when printing stack traces.
5370  SmartPointer<char> ToCString(AllowNullsFlag allow_nulls,
5371                               RobustnessFlag robustness_flag,
5372                               int offset,
5373                               int length,
5374                               int* length_output = 0);
5375  SmartPointer<char> ToCString(
5376      AllowNullsFlag allow_nulls = DISALLOW_NULLS,
5377      RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
5378      int* length_output = 0);
5379
5380  int Utf8Length();
5381
5382  // Return a 16 bit Unicode representation of the string.
5383  // The string should be nearly flat, otherwise the performance of
5384  // of this method may be very bad.  Setting robustness_flag to
5385  // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust  This means it
5386  // handles unexpected data without causing assert failures and it does not
5387  // do any heap allocations.  This is useful when printing stack traces.
5388  SmartPointer<uc16> ToWideCString(
5389      RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
5390
5391  // Tells whether the hash code has been computed.
5392  inline bool HasHashCode();
5393
5394  // Returns a hash value used for the property table
5395  inline uint32_t Hash();
5396
5397  static uint32_t ComputeHashField(unibrow::CharacterStream* buffer,
5398                                   int length);
5399
5400  static bool ComputeArrayIndex(unibrow::CharacterStream* buffer,
5401                                uint32_t* index,
5402                                int length);
5403
5404  // Externalization.
5405  bool MakeExternal(v8::String::ExternalStringResource* resource);
5406  bool MakeExternal(v8::String::ExternalAsciiStringResource* resource);
5407
5408  // Conversion.
5409  inline bool AsArrayIndex(uint32_t* index);
5410
5411  // Casting.
5412  static inline String* cast(Object* obj);
5413
5414  void PrintOn(FILE* out);
5415
5416  // For use during stack traces.  Performs rudimentary sanity check.
5417  bool LooksValid();
5418
5419  // Dispatched behavior.
5420  void StringShortPrint(StringStream* accumulator);
5421#ifdef OBJECT_PRINT
5422  inline void StringPrint() {
5423    StringPrint(stdout);
5424  }
5425  void StringPrint(FILE* out);
5426#endif
5427#ifdef DEBUG
5428  void StringVerify();
5429#endif
5430  inline bool IsFlat();
5431
5432  // Layout description.
5433  static const int kLengthOffset = HeapObject::kHeaderSize;
5434  static const int kHashFieldOffset = kLengthOffset + kPointerSize;
5435  static const int kSize = kHashFieldOffset + kPointerSize;
5436
5437  // Maximum number of characters to consider when trying to convert a string
5438  // value into an array index.
5439  static const int kMaxArrayIndexSize = 10;
5440
5441  // Max ascii char code.
5442  static const int kMaxAsciiCharCode = unibrow::Utf8::kMaxOneByteChar;
5443  static const unsigned kMaxAsciiCharCodeU = unibrow::Utf8::kMaxOneByteChar;
5444  static const int kMaxUC16CharCode = 0xffff;
5445
5446  // Minimum length for a cons string.
5447  static const int kMinNonFlatLength = 13;
5448
5449  // Mask constant for checking if a string has a computed hash code
5450  // and if it is an array index.  The least significant bit indicates
5451  // whether a hash code has been computed.  If the hash code has been
5452  // computed the 2nd bit tells whether the string can be used as an
5453  // array index.
5454  static const int kHashNotComputedMask = 1;
5455  static const int kIsNotArrayIndexMask = 1 << 1;
5456  static const int kNofHashBitFields = 2;
5457
5458  // Shift constant retrieving hash code from hash field.
5459  static const int kHashShift = kNofHashBitFields;
5460
5461  // Array index strings this short can keep their index in the hash
5462  // field.
5463  static const int kMaxCachedArrayIndexLength = 7;
5464
5465  // For strings which are array indexes the hash value has the string length
5466  // mixed into the hash, mainly to avoid a hash value of zero which would be
5467  // the case for the string '0'. 24 bits are used for the array index value.
5468  static const int kArrayIndexValueBits = 24;
5469  static const int kArrayIndexLengthBits =
5470      kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
5471
5472  STATIC_CHECK((kArrayIndexLengthBits > 0));
5473  STATIC_CHECK(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
5474
5475  static const int kArrayIndexHashLengthShift =
5476      kArrayIndexValueBits + kNofHashBitFields;
5477
5478  static const int kArrayIndexHashMask = (1 << kArrayIndexHashLengthShift) - 1;
5479
5480  static const int kArrayIndexValueMask =
5481      ((1 << kArrayIndexValueBits) - 1) << kHashShift;
5482
5483  // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
5484  // could use a mask to test if the length of string is less than or equal to
5485  // kMaxCachedArrayIndexLength.
5486  STATIC_CHECK(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
5487
5488  static const int kContainsCachedArrayIndexMask =
5489      (~kMaxCachedArrayIndexLength << kArrayIndexHashLengthShift) |
5490      kIsNotArrayIndexMask;
5491
5492  // Value of empty hash field indicating that the hash is not computed.
5493  static const int kEmptyHashField =
5494      kIsNotArrayIndexMask | kHashNotComputedMask;
5495
5496  // Value of hash field containing computed hash equal to zero.
5497  static const int kZeroHash = kIsNotArrayIndexMask;
5498
5499  // Maximal string length.
5500  static const int kMaxLength = (1 << (32 - 2)) - 1;
5501
5502  // Max length for computing hash. For strings longer than this limit the
5503  // string length is used as the hash value.
5504  static const int kMaxHashCalcLength = 16383;
5505
5506  // Limit for truncation in short printing.
5507  static const int kMaxShortPrintLength = 1024;
5508
5509  // Support for regular expressions.
5510  const uc16* GetTwoByteData();
5511  const uc16* GetTwoByteData(unsigned start);
5512
5513  // Support for StringInputBuffer
5514  static const unibrow::byte* ReadBlock(String* input,
5515                                        unibrow::byte* util_buffer,
5516                                        unsigned capacity,
5517                                        unsigned* remaining,
5518                                        unsigned* offset);
5519  static const unibrow::byte* ReadBlock(String** input,
5520                                        unibrow::byte* util_buffer,
5521                                        unsigned capacity,
5522                                        unsigned* remaining,
5523                                        unsigned* offset);
5524
5525  // Helper function for flattening strings.
5526  template <typename sinkchar>
5527  static void WriteToFlat(String* source,
5528                          sinkchar* sink,
5529                          int from,
5530                          int to);
5531
5532  static inline bool IsAscii(const char* chars, int length) {
5533    const char* limit = chars + length;
5534#ifdef V8_HOST_CAN_READ_UNALIGNED
5535    ASSERT(kMaxAsciiCharCode == 0x7F);
5536    const uintptr_t non_ascii_mask = kUintptrAllBitsSet / 0xFF * 0x80;
5537    while (chars <= limit - sizeof(uintptr_t)) {
5538      if (*reinterpret_cast<const uintptr_t*>(chars) & non_ascii_mask) {
5539        return false;
5540      }
5541      chars += sizeof(uintptr_t);
5542    }
5543#endif
5544    while (chars < limit) {
5545      if (static_cast<uint8_t>(*chars) > kMaxAsciiCharCodeU) return false;
5546      ++chars;
5547    }
5548    return true;
5549  }
5550
5551  static inline bool IsAscii(const uc16* chars, int length) {
5552    const uc16* limit = chars + length;
5553    while (chars < limit) {
5554      if (*chars > kMaxAsciiCharCodeU) return false;
5555      ++chars;
5556    }
5557    return true;
5558  }
5559
5560 protected:
5561  class ReadBlockBuffer {
5562   public:
5563    ReadBlockBuffer(unibrow::byte* util_buffer_,
5564                    unsigned cursor_,
5565                    unsigned capacity_,
5566                    unsigned remaining_) :
5567      util_buffer(util_buffer_),
5568      cursor(cursor_),
5569      capacity(capacity_),
5570      remaining(remaining_) {
5571    }
5572    unibrow::byte* util_buffer;
5573    unsigned       cursor;
5574    unsigned       capacity;
5575    unsigned       remaining;
5576  };
5577
5578  static inline const unibrow::byte* ReadBlock(String* input,
5579                                               ReadBlockBuffer* buffer,
5580                                               unsigned* offset,
5581                                               unsigned max_chars);
5582  static void ReadBlockIntoBuffer(String* input,
5583                                  ReadBlockBuffer* buffer,
5584                                  unsigned* offset_ptr,
5585                                  unsigned max_chars);
5586
5587 private:
5588  // Try to flatten the top level ConsString that is hiding behind this
5589  // string.  This is a no-op unless the string is a ConsString.  Flatten
5590  // mutates the ConsString and might return a failure.
5591  MUST_USE_RESULT MaybeObject* SlowTryFlatten(PretenureFlag pretenure);
5592
5593  static inline bool IsHashFieldComputed(uint32_t field);
5594
5595  // Slow case of String::Equals.  This implementation works on any strings
5596  // but it is most efficient on strings that are almost flat.
5597  bool SlowEquals(String* other);
5598
5599  // Slow case of AsArrayIndex.
5600  bool SlowAsArrayIndex(uint32_t* index);
5601
5602  // Compute and set the hash code.
5603  uint32_t ComputeAndSetHash();
5604
5605  DISALLOW_IMPLICIT_CONSTRUCTORS(String);
5606};
5607
5608
5609// The SeqString abstract class captures sequential string values.
5610class SeqString: public String {
5611 public:
5612
5613  // Casting.
5614  static inline SeqString* cast(Object* obj);
5615
5616 private:
5617  DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
5618};
5619
5620
5621// The AsciiString class captures sequential ascii string objects.
5622// Each character in the AsciiString is an ascii character.
5623class SeqAsciiString: public SeqString {
5624 public:
5625  static const bool kHasAsciiEncoding = true;
5626
5627  // Dispatched behavior.
5628  inline uint16_t SeqAsciiStringGet(int index);
5629  inline void SeqAsciiStringSet(int index, uint16_t value);
5630
5631  // Get the address of the characters in this string.
5632  inline Address GetCharsAddress();
5633
5634  inline char* GetChars();
5635
5636  // Casting
5637  static inline SeqAsciiString* cast(Object* obj);
5638
5639  // Garbage collection support.  This method is called by the
5640  // garbage collector to compute the actual size of an AsciiString
5641  // instance.
5642  inline int SeqAsciiStringSize(InstanceType instance_type);
5643
5644  // Computes the size for an AsciiString instance of a given length.
5645  static int SizeFor(int length) {
5646    return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
5647  }
5648
5649  // Layout description.
5650  static const int kHeaderSize = String::kSize;
5651  static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5652
5653  // Maximal memory usage for a single sequential ASCII string.
5654  static const int kMaxSize = 512 * MB;
5655  // Maximal length of a single sequential ASCII string.
5656  // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5657  static const int kMaxLength = (kMaxSize - kHeaderSize);
5658
5659  // Support for StringInputBuffer.
5660  inline void SeqAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5661                                                unsigned* offset,
5662                                                unsigned chars);
5663  inline const unibrow::byte* SeqAsciiStringReadBlock(unsigned* remaining,
5664                                                      unsigned* offset,
5665                                                      unsigned chars);
5666
5667 private:
5668  DISALLOW_IMPLICIT_CONSTRUCTORS(SeqAsciiString);
5669};
5670
5671
5672// The TwoByteString class captures sequential unicode string objects.
5673// Each character in the TwoByteString is a two-byte uint16_t.
5674class SeqTwoByteString: public SeqString {
5675 public:
5676  static const bool kHasAsciiEncoding = false;
5677
5678  // Dispatched behavior.
5679  inline uint16_t SeqTwoByteStringGet(int index);
5680  inline void SeqTwoByteStringSet(int index, uint16_t value);
5681
5682  // Get the address of the characters in this string.
5683  inline Address GetCharsAddress();
5684
5685  inline uc16* GetChars();
5686
5687  // For regexp code.
5688  const uint16_t* SeqTwoByteStringGetData(unsigned start);
5689
5690  // Casting
5691  static inline SeqTwoByteString* cast(Object* obj);
5692
5693  // Garbage collection support.  This method is called by the
5694  // garbage collector to compute the actual size of a TwoByteString
5695  // instance.
5696  inline int SeqTwoByteStringSize(InstanceType instance_type);
5697
5698  // Computes the size for a TwoByteString instance of a given length.
5699  static int SizeFor(int length) {
5700    return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
5701  }
5702
5703  // Layout description.
5704  static const int kHeaderSize = String::kSize;
5705  static const int kAlignedSize = POINTER_SIZE_ALIGN(kHeaderSize);
5706
5707  // Maximal memory usage for a single sequential two-byte string.
5708  static const int kMaxSize = 512 * MB;
5709  // Maximal length of a single sequential two-byte string.
5710  // Q.v. String::kMaxLength which is the maximal size of concatenated strings.
5711  static const int kMaxLength = (kMaxSize - kHeaderSize) / sizeof(uint16_t);
5712
5713  // Support for StringInputBuffer.
5714  inline void SeqTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5715                                                  unsigned* offset_ptr,
5716                                                  unsigned chars);
5717
5718 private:
5719  DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
5720};
5721
5722
5723// The ConsString class describes string values built by using the
5724// addition operator on strings.  A ConsString is a pair where the
5725// first and second components are pointers to other string values.
5726// One or both components of a ConsString can be pointers to other
5727// ConsStrings, creating a binary tree of ConsStrings where the leaves
5728// are non-ConsString string values.  The string value represented by
5729// a ConsString can be obtained by concatenating the leaf string
5730// values in a left-to-right depth-first traversal of the tree.
5731class ConsString: public String {
5732 public:
5733  // First string of the cons cell.
5734  inline String* first();
5735  // Doesn't check that the result is a string, even in debug mode.  This is
5736  // useful during GC where the mark bits confuse the checks.
5737  inline Object* unchecked_first();
5738  inline void set_first(String* first,
5739                        WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5740
5741  // Second string of the cons cell.
5742  inline String* second();
5743  // Doesn't check that the result is a string, even in debug mode.  This is
5744  // useful during GC where the mark bits confuse the checks.
5745  inline Object* unchecked_second();
5746  inline void set_second(String* second,
5747                         WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5748
5749  // Dispatched behavior.
5750  uint16_t ConsStringGet(int index);
5751
5752  // Casting.
5753  static inline ConsString* cast(Object* obj);
5754
5755  // Layout description.
5756  static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
5757  static const int kSecondOffset = kFirstOffset + kPointerSize;
5758  static const int kSize = kSecondOffset + kPointerSize;
5759
5760  // Support for StringInputBuffer.
5761  inline const unibrow::byte* ConsStringReadBlock(ReadBlockBuffer* buffer,
5762                                                  unsigned* offset_ptr,
5763                                                  unsigned chars);
5764  inline void ConsStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5765                                            unsigned* offset_ptr,
5766                                            unsigned chars);
5767
5768  // Minimum length for a cons string.
5769  static const int kMinLength = 13;
5770
5771  typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
5772          BodyDescriptor;
5773
5774 private:
5775  DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
5776};
5777
5778
5779// The ExternalString class describes string values that are backed by
5780// a string resource that lies outside the V8 heap.  ExternalStrings
5781// consist of the length field common to all strings, a pointer to the
5782// external resource.  It is important to ensure (externally) that the
5783// resource is not deallocated while the ExternalString is live in the
5784// V8 heap.
5785//
5786// The API expects that all ExternalStrings are created through the
5787// API.  Therefore, ExternalStrings should not be used internally.
5788class ExternalString: public String {
5789 public:
5790  // Casting
5791  static inline ExternalString* cast(Object* obj);
5792
5793  // Layout description.
5794  static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
5795  static const int kSize = kResourceOffset + kPointerSize;
5796
5797  STATIC_CHECK(kResourceOffset == Internals::kStringResourceOffset);
5798
5799 private:
5800  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
5801};
5802
5803
5804// The ExternalAsciiString class is an external string backed by an
5805// ASCII string.
5806class ExternalAsciiString: public ExternalString {
5807 public:
5808  static const bool kHasAsciiEncoding = true;
5809
5810  typedef v8::String::ExternalAsciiStringResource Resource;
5811
5812  // The underlying resource.
5813  inline Resource* resource();
5814  inline void set_resource(Resource* buffer);
5815
5816  // Dispatched behavior.
5817  uint16_t ExternalAsciiStringGet(int index);
5818
5819  // Casting.
5820  static inline ExternalAsciiString* cast(Object* obj);
5821
5822  // Garbage collection support.
5823  inline void ExternalAsciiStringIterateBody(ObjectVisitor* v);
5824
5825  template<typename StaticVisitor>
5826  inline void ExternalAsciiStringIterateBody();
5827
5828  // Support for StringInputBuffer.
5829  const unibrow::byte* ExternalAsciiStringReadBlock(unsigned* remaining,
5830                                                    unsigned* offset,
5831                                                    unsigned chars);
5832  inline void ExternalAsciiStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5833                                                     unsigned* offset,
5834                                                     unsigned chars);
5835
5836 private:
5837  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalAsciiString);
5838};
5839
5840
5841// The ExternalTwoByteString class is an external string backed by a UTF-16
5842// encoded string.
5843class ExternalTwoByteString: public ExternalString {
5844 public:
5845  static const bool kHasAsciiEncoding = false;
5846
5847  typedef v8::String::ExternalStringResource Resource;
5848
5849  // The underlying string resource.
5850  inline Resource* resource();
5851  inline void set_resource(Resource* buffer);
5852
5853  // Dispatched behavior.
5854  uint16_t ExternalTwoByteStringGet(int index);
5855
5856  // For regexp code.
5857  const uint16_t* ExternalTwoByteStringGetData(unsigned start);
5858
5859  // Casting.
5860  static inline ExternalTwoByteString* cast(Object* obj);
5861
5862  // Garbage collection support.
5863  inline void ExternalTwoByteStringIterateBody(ObjectVisitor* v);
5864
5865  template<typename StaticVisitor>
5866  inline void ExternalTwoByteStringIterateBody();
5867
5868
5869  // Support for StringInputBuffer.
5870  void ExternalTwoByteStringReadBlockIntoBuffer(ReadBlockBuffer* buffer,
5871                                                unsigned* offset_ptr,
5872                                                unsigned chars);
5873
5874 private:
5875  DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
5876};
5877
5878
5879// Utility superclass for stack-allocated objects that must be updated
5880// on gc.  It provides two ways for the gc to update instances, either
5881// iterating or updating after gc.
5882class Relocatable BASE_EMBEDDED {
5883 public:
5884  explicit inline Relocatable(Isolate* isolate);
5885  inline virtual ~Relocatable();
5886  virtual void IterateInstance(ObjectVisitor* v) { }
5887  virtual void PostGarbageCollection() { }
5888
5889  static void PostGarbageCollectionProcessing();
5890  static int ArchiveSpacePerThread();
5891  static char* ArchiveState(char* to);
5892  static char* RestoreState(char* from);
5893  static void Iterate(ObjectVisitor* v);
5894  static void Iterate(ObjectVisitor* v, Relocatable* top);
5895  static char* Iterate(ObjectVisitor* v, char* t);
5896 private:
5897  Isolate* isolate_;
5898  Relocatable* prev_;
5899};
5900
5901
5902// A flat string reader provides random access to the contents of a
5903// string independent of the character width of the string.  The handle
5904// must be valid as long as the reader is being used.
5905class FlatStringReader : public Relocatable {
5906 public:
5907  FlatStringReader(Isolate* isolate, Handle<String> str);
5908  FlatStringReader(Isolate* isolate, Vector<const char> input);
5909  void PostGarbageCollection();
5910  inline uc32 Get(int index);
5911  int length() { return length_; }
5912 private:
5913  String** str_;
5914  bool is_ascii_;
5915  int length_;
5916  const void* start_;
5917};
5918
5919
5920// Note that StringInputBuffers are not valid across a GC!  To fix this
5921// it would have to store a String Handle instead of a String* and
5922// AsciiStringReadBlock would have to be modified to use memcpy.
5923//
5924// StringInputBuffer is able to traverse any string regardless of how
5925// deeply nested a sequence of ConsStrings it is made of.  However,
5926// performance will be better if deep strings are flattened before they
5927// are traversed.  Since flattening requires memory allocation this is
5928// not always desirable, however (esp. in debugging situations).
5929class StringInputBuffer: public unibrow::InputBuffer<String, String*, 1024> {
5930 public:
5931  virtual void Seek(unsigned pos);
5932  inline StringInputBuffer(): unibrow::InputBuffer<String, String*, 1024>() {}
5933  explicit inline StringInputBuffer(String* backing):
5934      unibrow::InputBuffer<String, String*, 1024>(backing) {}
5935};
5936
5937
5938class SafeStringInputBuffer
5939  : public unibrow::InputBuffer<String, String**, 256> {
5940 public:
5941  virtual void Seek(unsigned pos);
5942  inline SafeStringInputBuffer()
5943      : unibrow::InputBuffer<String, String**, 256>() {}
5944  explicit inline SafeStringInputBuffer(String** backing)
5945      : unibrow::InputBuffer<String, String**, 256>(backing) {}
5946};
5947
5948
5949template <typename T>
5950class VectorIterator {
5951 public:
5952  VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
5953  explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
5954  T GetNext() { return data_[index_++]; }
5955  bool has_more() { return index_ < data_.length(); }
5956 private:
5957  Vector<const T> data_;
5958  int index_;
5959};
5960
5961
5962// The Oddball describes objects null, undefined, true, and false.
5963class Oddball: public HeapObject {
5964 public:
5965  // [to_string]: Cached to_string computed at startup.
5966  DECL_ACCESSORS(to_string, String)
5967
5968  // [to_number]: Cached to_number computed at startup.
5969  DECL_ACCESSORS(to_number, Object)
5970
5971  inline byte kind();
5972  inline void set_kind(byte kind);
5973
5974  // Casting.
5975  static inline Oddball* cast(Object* obj);
5976
5977  // Dispatched behavior.
5978#ifdef DEBUG
5979  void OddballVerify();
5980#endif
5981
5982  // Initialize the fields.
5983  MUST_USE_RESULT MaybeObject* Initialize(const char* to_string,
5984                                          Object* to_number,
5985                                          byte kind);
5986
5987  // Layout description.
5988  static const int kToStringOffset = HeapObject::kHeaderSize;
5989  static const int kToNumberOffset = kToStringOffset + kPointerSize;
5990  static const int kKindOffset = kToNumberOffset + kPointerSize;
5991  static const int kSize = kKindOffset + kPointerSize;
5992
5993  static const byte kFalse = 0;
5994  static const byte kTrue = 1;
5995  static const byte kNotBooleanMask = ~1;
5996  static const byte kTheHole = 2;
5997  static const byte kNull = 3;
5998  static const byte kArgumentMarker = 4;
5999  static const byte kUndefined = 5;
6000  static const byte kOther = 6;
6001
6002  typedef FixedBodyDescriptor<kToStringOffset,
6003                              kToNumberOffset + kPointerSize,
6004                              kSize> BodyDescriptor;
6005
6006 private:
6007  DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
6008};
6009
6010
6011class JSGlobalPropertyCell: public HeapObject {
6012 public:
6013  // [value]: value of the global property.
6014  DECL_ACCESSORS(value, Object)
6015
6016  // Casting.
6017  static inline JSGlobalPropertyCell* cast(Object* obj);
6018
6019#ifdef DEBUG
6020  void JSGlobalPropertyCellVerify();
6021#endif
6022#ifdef OBJECT_PRINT
6023  inline void JSGlobalPropertyCellPrint() {
6024    JSGlobalPropertyCellPrint(stdout);
6025  }
6026  void JSGlobalPropertyCellPrint(FILE* out);
6027#endif
6028
6029  // Layout description.
6030  static const int kValueOffset = HeapObject::kHeaderSize;
6031  static const int kSize = kValueOffset + kPointerSize;
6032
6033  typedef FixedBodyDescriptor<kValueOffset,
6034                              kValueOffset + kPointerSize,
6035                              kSize> BodyDescriptor;
6036
6037  // Returns the isolate/heap this cell object belongs to.
6038  inline Isolate* isolate();
6039  inline Heap* heap();
6040
6041 private:
6042  DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalPropertyCell);
6043};
6044
6045
6046
6047// Proxy describes objects pointing from JavaScript to C structures.
6048// Since they cannot contain references to JS HeapObjects they can be
6049// placed in old_data_space.
6050class Proxy: public HeapObject {
6051 public:
6052  // [proxy]: field containing the address.
6053  inline Address proxy();
6054  inline void set_proxy(Address value);
6055
6056  // Casting.
6057  static inline Proxy* cast(Object* obj);
6058
6059  // Dispatched behavior.
6060  inline void ProxyIterateBody(ObjectVisitor* v);
6061
6062  template<typename StaticVisitor>
6063  inline void ProxyIterateBody();
6064
6065#ifdef OBJECT_PRINT
6066  inline void ProxyPrint() {
6067    ProxyPrint(stdout);
6068  }
6069  void ProxyPrint(FILE* out);
6070#endif
6071#ifdef DEBUG
6072  void ProxyVerify();
6073#endif
6074
6075  // Layout description.
6076
6077  static const int kProxyOffset = HeapObject::kHeaderSize;
6078  static const int kSize = kProxyOffset + kPointerSize;
6079
6080  STATIC_CHECK(kProxyOffset == Internals::kProxyProxyOffset);
6081
6082 private:
6083  DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
6084};
6085
6086
6087// The JSArray describes JavaScript Arrays
6088//  Such an array can be in one of two modes:
6089//    - fast, backing storage is a FixedArray and length <= elements.length();
6090//       Please note: push and pop can be used to grow and shrink the array.
6091//    - slow, backing storage is a HashTable with numbers as keys.
6092class JSArray: public JSObject {
6093 public:
6094  // [length]: The length property.
6095  DECL_ACCESSORS(length, Object)
6096
6097  // Overload the length setter to skip write barrier when the length
6098  // is set to a smi. This matches the set function on FixedArray.
6099  inline void set_length(Smi* length);
6100
6101  MUST_USE_RESULT MaybeObject* JSArrayUpdateLengthFromIndex(uint32_t index,
6102                                                            Object* value);
6103
6104  // Initialize the array with the given capacity. The function may
6105  // fail due to out-of-memory situations, but only if the requested
6106  // capacity is non-zero.
6107  MUST_USE_RESULT MaybeObject* Initialize(int capacity);
6108
6109  // Set the content of the array to the content of storage.
6110  inline void SetContent(FixedArray* storage);
6111
6112  // Casting.
6113  static inline JSArray* cast(Object* obj);
6114
6115  // Uses handles.  Ensures that the fixed array backing the JSArray has at
6116  // least the stated size.
6117  inline void EnsureSize(int minimum_size_of_backing_fixed_array);
6118
6119  // Dispatched behavior.
6120#ifdef OBJECT_PRINT
6121  inline void JSArrayPrint() {
6122    JSArrayPrint(stdout);
6123  }
6124  void JSArrayPrint(FILE* out);
6125#endif
6126#ifdef DEBUG
6127  void JSArrayVerify();
6128#endif
6129
6130  // Number of element slots to pre-allocate for an empty array.
6131  static const int kPreallocatedArrayElements = 4;
6132
6133  // Layout description.
6134  static const int kLengthOffset = JSObject::kHeaderSize;
6135  static const int kSize = kLengthOffset + kPointerSize;
6136
6137 private:
6138  // Expand the fixed array backing of a fast-case JSArray to at least
6139  // the requested size.
6140  void Expand(int minimum_size_of_backing_fixed_array);
6141
6142  DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
6143};
6144
6145
6146// JSRegExpResult is just a JSArray with a specific initial map.
6147// This initial map adds in-object properties for "index" and "input"
6148// properties, as assigned by RegExp.prototype.exec, which allows
6149// faster creation of RegExp exec results.
6150// This class just holds constants used when creating the result.
6151// After creation the result must be treated as a JSArray in all regards.
6152class JSRegExpResult: public JSArray {
6153 public:
6154  // Offsets of object fields.
6155  static const int kIndexOffset = JSArray::kSize;
6156  static const int kInputOffset = kIndexOffset + kPointerSize;
6157  static const int kSize = kInputOffset + kPointerSize;
6158  // Indices of in-object properties.
6159  static const int kIndexIndex = 0;
6160  static const int kInputIndex = 1;
6161 private:
6162  DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
6163};
6164
6165
6166// An accessor must have a getter, but can have no setter.
6167//
6168// When setting a property, V8 searches accessors in prototypes.
6169// If an accessor was found and it does not have a setter,
6170// the request is ignored.
6171//
6172// If the accessor in the prototype has the READ_ONLY property attribute, then
6173// a new value is added to the local object when the property is set.
6174// This shadows the accessor in the prototype.
6175class AccessorInfo: public Struct {
6176 public:
6177  DECL_ACCESSORS(getter, Object)
6178  DECL_ACCESSORS(setter, Object)
6179  DECL_ACCESSORS(data, Object)
6180  DECL_ACCESSORS(name, Object)
6181  DECL_ACCESSORS(flag, Smi)
6182
6183  inline bool all_can_read();
6184  inline void set_all_can_read(bool value);
6185
6186  inline bool all_can_write();
6187  inline void set_all_can_write(bool value);
6188
6189  inline bool prohibits_overwriting();
6190  inline void set_prohibits_overwriting(bool value);
6191
6192  inline PropertyAttributes property_attributes();
6193  inline void set_property_attributes(PropertyAttributes attributes);
6194
6195  static inline AccessorInfo* cast(Object* obj);
6196
6197#ifdef OBJECT_PRINT
6198  inline void AccessorInfoPrint() {
6199    AccessorInfoPrint(stdout);
6200  }
6201  void AccessorInfoPrint(FILE* out);
6202#endif
6203#ifdef DEBUG
6204  void AccessorInfoVerify();
6205#endif
6206
6207  static const int kGetterOffset = HeapObject::kHeaderSize;
6208  static const int kSetterOffset = kGetterOffset + kPointerSize;
6209  static const int kDataOffset = kSetterOffset + kPointerSize;
6210  static const int kNameOffset = kDataOffset + kPointerSize;
6211  static const int kFlagOffset = kNameOffset + kPointerSize;
6212  static const int kSize = kFlagOffset + kPointerSize;
6213
6214 private:
6215  // Bit positions in flag.
6216  static const int kAllCanReadBit = 0;
6217  static const int kAllCanWriteBit = 1;
6218  static const int kProhibitsOverwritingBit = 2;
6219  class AttributesField: public BitField<PropertyAttributes, 3, 3> {};
6220
6221  DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
6222};
6223
6224
6225class AccessCheckInfo: public Struct {
6226 public:
6227  DECL_ACCESSORS(named_callback, Object)
6228  DECL_ACCESSORS(indexed_callback, Object)
6229  DECL_ACCESSORS(data, Object)
6230
6231  static inline AccessCheckInfo* cast(Object* obj);
6232
6233#ifdef OBJECT_PRINT
6234  inline void AccessCheckInfoPrint() {
6235    AccessCheckInfoPrint(stdout);
6236  }
6237  void AccessCheckInfoPrint(FILE* out);
6238#endif
6239#ifdef DEBUG
6240  void AccessCheckInfoVerify();
6241#endif
6242
6243  static const int kNamedCallbackOffset   = HeapObject::kHeaderSize;
6244  static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
6245  static const int kDataOffset = kIndexedCallbackOffset + kPointerSize;
6246  static const int kSize = kDataOffset + kPointerSize;
6247
6248 private:
6249  DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
6250};
6251
6252
6253class InterceptorInfo: public Struct {
6254 public:
6255  DECL_ACCESSORS(getter, Object)
6256  DECL_ACCESSORS(setter, Object)
6257  DECL_ACCESSORS(query, Object)
6258  DECL_ACCESSORS(deleter, Object)
6259  DECL_ACCESSORS(enumerator, Object)
6260  DECL_ACCESSORS(data, Object)
6261
6262  static inline InterceptorInfo* cast(Object* obj);
6263
6264#ifdef OBJECT_PRINT
6265  inline void InterceptorInfoPrint() {
6266    InterceptorInfoPrint(stdout);
6267  }
6268  void InterceptorInfoPrint(FILE* out);
6269#endif
6270#ifdef DEBUG
6271  void InterceptorInfoVerify();
6272#endif
6273
6274  static const int kGetterOffset = HeapObject::kHeaderSize;
6275  static const int kSetterOffset = kGetterOffset + kPointerSize;
6276  static const int kQueryOffset = kSetterOffset + kPointerSize;
6277  static const int kDeleterOffset = kQueryOffset + kPointerSize;
6278  static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
6279  static const int kDataOffset = kEnumeratorOffset + kPointerSize;
6280  static const int kSize = kDataOffset + kPointerSize;
6281
6282 private:
6283  DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
6284};
6285
6286
6287class CallHandlerInfo: public Struct {
6288 public:
6289  DECL_ACCESSORS(callback, Object)
6290  DECL_ACCESSORS(data, Object)
6291
6292  static inline CallHandlerInfo* cast(Object* obj);
6293
6294#ifdef OBJECT_PRINT
6295  inline void CallHandlerInfoPrint() {
6296    CallHandlerInfoPrint(stdout);
6297  }
6298  void CallHandlerInfoPrint(FILE* out);
6299#endif
6300#ifdef DEBUG
6301  void CallHandlerInfoVerify();
6302#endif
6303
6304  static const int kCallbackOffset = HeapObject::kHeaderSize;
6305  static const int kDataOffset = kCallbackOffset + kPointerSize;
6306  static const int kSize = kDataOffset + kPointerSize;
6307
6308 private:
6309  DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
6310};
6311
6312
6313class TemplateInfo: public Struct {
6314 public:
6315  DECL_ACCESSORS(tag, Object)
6316  DECL_ACCESSORS(property_list, Object)
6317
6318#ifdef DEBUG
6319  void TemplateInfoVerify();
6320#endif
6321
6322  static const int kTagOffset          = HeapObject::kHeaderSize;
6323  static const int kPropertyListOffset = kTagOffset + kPointerSize;
6324  static const int kHeaderSize         = kPropertyListOffset + kPointerSize;
6325 protected:
6326  friend class AGCCVersionRequiresThisClassToHaveAFriendSoHereItIs;
6327  DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
6328};
6329
6330
6331class FunctionTemplateInfo: public TemplateInfo {
6332 public:
6333  DECL_ACCESSORS(serial_number, Object)
6334  DECL_ACCESSORS(call_code, Object)
6335  DECL_ACCESSORS(property_accessors, Object)
6336  DECL_ACCESSORS(prototype_template, Object)
6337  DECL_ACCESSORS(parent_template, Object)
6338  DECL_ACCESSORS(named_property_handler, Object)
6339  DECL_ACCESSORS(indexed_property_handler, Object)
6340  DECL_ACCESSORS(instance_template, Object)
6341  DECL_ACCESSORS(class_name, Object)
6342  DECL_ACCESSORS(signature, Object)
6343  DECL_ACCESSORS(instance_call_handler, Object)
6344  DECL_ACCESSORS(access_check_info, Object)
6345  DECL_ACCESSORS(flag, Smi)
6346
6347  // Following properties use flag bits.
6348  DECL_BOOLEAN_ACCESSORS(hidden_prototype)
6349  DECL_BOOLEAN_ACCESSORS(undetectable)
6350  // If the bit is set, object instances created by this function
6351  // requires access check.
6352  DECL_BOOLEAN_ACCESSORS(needs_access_check)
6353
6354  static inline FunctionTemplateInfo* cast(Object* obj);
6355
6356#ifdef OBJECT_PRINT
6357  inline void FunctionTemplateInfoPrint() {
6358    FunctionTemplateInfoPrint(stdout);
6359  }
6360  void FunctionTemplateInfoPrint(FILE* out);
6361#endif
6362#ifdef DEBUG
6363  void FunctionTemplateInfoVerify();
6364#endif
6365
6366  static const int kSerialNumberOffset = TemplateInfo::kHeaderSize;
6367  static const int kCallCodeOffset = kSerialNumberOffset + kPointerSize;
6368  static const int kPropertyAccessorsOffset = kCallCodeOffset + kPointerSize;
6369  static const int kPrototypeTemplateOffset =
6370      kPropertyAccessorsOffset + kPointerSize;
6371  static const int kParentTemplateOffset =
6372      kPrototypeTemplateOffset + kPointerSize;
6373  static const int kNamedPropertyHandlerOffset =
6374      kParentTemplateOffset + kPointerSize;
6375  static const int kIndexedPropertyHandlerOffset =
6376      kNamedPropertyHandlerOffset + kPointerSize;
6377  static const int kInstanceTemplateOffset =
6378      kIndexedPropertyHandlerOffset + kPointerSize;
6379  static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
6380  static const int kSignatureOffset = kClassNameOffset + kPointerSize;
6381  static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
6382  static const int kAccessCheckInfoOffset =
6383      kInstanceCallHandlerOffset + kPointerSize;
6384  static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
6385  static const int kSize = kFlagOffset + kPointerSize;
6386
6387 private:
6388  // Bit position in the flag, from least significant bit position.
6389  static const int kHiddenPrototypeBit   = 0;
6390  static const int kUndetectableBit      = 1;
6391  static const int kNeedsAccessCheckBit  = 2;
6392
6393  DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
6394};
6395
6396
6397class ObjectTemplateInfo: public TemplateInfo {
6398 public:
6399  DECL_ACCESSORS(constructor, Object)
6400  DECL_ACCESSORS(internal_field_count, Object)
6401
6402  static inline ObjectTemplateInfo* cast(Object* obj);
6403
6404#ifdef OBJECT_PRINT
6405  inline void ObjectTemplateInfoPrint() {
6406    ObjectTemplateInfoPrint(stdout);
6407  }
6408  void ObjectTemplateInfoPrint(FILE* out);
6409#endif
6410#ifdef DEBUG
6411  void ObjectTemplateInfoVerify();
6412#endif
6413
6414  static const int kConstructorOffset = TemplateInfo::kHeaderSize;
6415  static const int kInternalFieldCountOffset =
6416      kConstructorOffset + kPointerSize;
6417  static const int kSize = kInternalFieldCountOffset + kPointerSize;
6418};
6419
6420
6421class SignatureInfo: public Struct {
6422 public:
6423  DECL_ACCESSORS(receiver, Object)
6424  DECL_ACCESSORS(args, Object)
6425
6426  static inline SignatureInfo* cast(Object* obj);
6427
6428#ifdef OBJECT_PRINT
6429  inline void SignatureInfoPrint() {
6430    SignatureInfoPrint(stdout);
6431  }
6432  void SignatureInfoPrint(FILE* out);
6433#endif
6434#ifdef DEBUG
6435  void SignatureInfoVerify();
6436#endif
6437
6438  static const int kReceiverOffset = Struct::kHeaderSize;
6439  static const int kArgsOffset     = kReceiverOffset + kPointerSize;
6440  static const int kSize           = kArgsOffset + kPointerSize;
6441
6442 private:
6443  DISALLOW_IMPLICIT_CONSTRUCTORS(SignatureInfo);
6444};
6445
6446
6447class TypeSwitchInfo: public Struct {
6448 public:
6449  DECL_ACCESSORS(types, Object)
6450
6451  static inline TypeSwitchInfo* cast(Object* obj);
6452
6453#ifdef OBJECT_PRINT
6454  inline void TypeSwitchInfoPrint() {
6455    TypeSwitchInfoPrint(stdout);
6456  }
6457  void TypeSwitchInfoPrint(FILE* out);
6458#endif
6459#ifdef DEBUG
6460  void TypeSwitchInfoVerify();
6461#endif
6462
6463  static const int kTypesOffset = Struct::kHeaderSize;
6464  static const int kSize        = kTypesOffset + kPointerSize;
6465};
6466
6467
6468#ifdef ENABLE_DEBUGGER_SUPPORT
6469// The DebugInfo class holds additional information for a function being
6470// debugged.
6471class DebugInfo: public Struct {
6472 public:
6473  // The shared function info for the source being debugged.
6474  DECL_ACCESSORS(shared, SharedFunctionInfo)
6475  // Code object for the original code.
6476  DECL_ACCESSORS(original_code, Code)
6477  // Code object for the patched code. This code object is the code object
6478  // currently active for the function.
6479  DECL_ACCESSORS(code, Code)
6480  // Fixed array holding status information for each active break point.
6481  DECL_ACCESSORS(break_points, FixedArray)
6482
6483  // Check if there is a break point at a code position.
6484  bool HasBreakPoint(int code_position);
6485  // Get the break point info object for a code position.
6486  Object* GetBreakPointInfo(int code_position);
6487  // Clear a break point.
6488  static void ClearBreakPoint(Handle<DebugInfo> debug_info,
6489                              int code_position,
6490                              Handle<Object> break_point_object);
6491  // Set a break point.
6492  static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_position,
6493                            int source_position, int statement_position,
6494                            Handle<Object> break_point_object);
6495  // Get the break point objects for a code position.
6496  Object* GetBreakPointObjects(int code_position);
6497  // Find the break point info holding this break point object.
6498  static Object* FindBreakPointInfo(Handle<DebugInfo> debug_info,
6499                                    Handle<Object> break_point_object);
6500  // Get the number of break points for this function.
6501  int GetBreakPointCount();
6502
6503  static inline DebugInfo* cast(Object* obj);
6504
6505#ifdef OBJECT_PRINT
6506  inline void DebugInfoPrint() {
6507    DebugInfoPrint(stdout);
6508  }
6509  void DebugInfoPrint(FILE* out);
6510#endif
6511#ifdef DEBUG
6512  void DebugInfoVerify();
6513#endif
6514
6515  static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
6516  static const int kOriginalCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
6517  static const int kPatchedCodeIndex = kOriginalCodeIndex + kPointerSize;
6518  static const int kActiveBreakPointsCountIndex =
6519      kPatchedCodeIndex + kPointerSize;
6520  static const int kBreakPointsStateIndex =
6521      kActiveBreakPointsCountIndex + kPointerSize;
6522  static const int kSize = kBreakPointsStateIndex + kPointerSize;
6523
6524 private:
6525  static const int kNoBreakPointInfo = -1;
6526
6527  // Lookup the index in the break_points array for a code position.
6528  int GetBreakPointInfoIndex(int code_position);
6529
6530  DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
6531};
6532
6533
6534// The BreakPointInfo class holds information for break points set in a
6535// function. The DebugInfo object holds a BreakPointInfo object for each code
6536// position with one or more break points.
6537class BreakPointInfo: public Struct {
6538 public:
6539  // The position in the code for the break point.
6540  DECL_ACCESSORS(code_position, Smi)
6541  // The position in the source for the break position.
6542  DECL_ACCESSORS(source_position, Smi)
6543  // The position in the source for the last statement before this break
6544  // position.
6545  DECL_ACCESSORS(statement_position, Smi)
6546  // List of related JavaScript break points.
6547  DECL_ACCESSORS(break_point_objects, Object)
6548
6549  // Removes a break point.
6550  static void ClearBreakPoint(Handle<BreakPointInfo> info,
6551                              Handle<Object> break_point_object);
6552  // Set a break point.
6553  static void SetBreakPoint(Handle<BreakPointInfo> info,
6554                            Handle<Object> break_point_object);
6555  // Check if break point info has this break point object.
6556  static bool HasBreakPointObject(Handle<BreakPointInfo> info,
6557                                  Handle<Object> break_point_object);
6558  // Get the number of break points for this code position.
6559  int GetBreakPointCount();
6560
6561  static inline BreakPointInfo* cast(Object* obj);
6562
6563#ifdef OBJECT_PRINT
6564  inline void BreakPointInfoPrint() {
6565    BreakPointInfoPrint(stdout);
6566  }
6567  void BreakPointInfoPrint(FILE* out);
6568#endif
6569#ifdef DEBUG
6570  void BreakPointInfoVerify();
6571#endif
6572
6573  static const int kCodePositionIndex = Struct::kHeaderSize;
6574  static const int kSourcePositionIndex = kCodePositionIndex + kPointerSize;
6575  static const int kStatementPositionIndex =
6576      kSourcePositionIndex + kPointerSize;
6577  static const int kBreakPointObjectsIndex =
6578      kStatementPositionIndex + kPointerSize;
6579  static const int kSize = kBreakPointObjectsIndex + kPointerSize;
6580
6581 private:
6582  DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
6583};
6584#endif  // ENABLE_DEBUGGER_SUPPORT
6585
6586
6587#undef DECL_BOOLEAN_ACCESSORS
6588#undef DECL_ACCESSORS
6589
6590
6591// Abstract base class for visiting, and optionally modifying, the
6592// pointers contained in Objects. Used in GC and serialization/deserialization.
6593class ObjectVisitor BASE_EMBEDDED {
6594 public:
6595  virtual ~ObjectVisitor() {}
6596
6597  // Visits a contiguous arrays of pointers in the half-open range
6598  // [start, end). Any or all of the values may be modified on return.
6599  virtual void VisitPointers(Object** start, Object** end) = 0;
6600
6601  // To allow lazy clearing of inline caches the visitor has
6602  // a rich interface for iterating over Code objects..
6603
6604  // Visits a code target in the instruction stream.
6605  virtual void VisitCodeTarget(RelocInfo* rinfo);
6606
6607  // Visits a code entry in a JS function.
6608  virtual void VisitCodeEntry(Address entry_address);
6609
6610  // Visits a global property cell reference in the instruction stream.
6611  virtual void VisitGlobalPropertyCell(RelocInfo* rinfo);
6612
6613  // Visits a runtime entry in the instruction stream.
6614  virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
6615
6616  // Visits the resource of an ASCII or two-byte string.
6617  virtual void VisitExternalAsciiString(
6618      v8::String::ExternalAsciiStringResource** resource) {}
6619  virtual void VisitExternalTwoByteString(
6620      v8::String::ExternalStringResource** resource) {}
6621
6622  // Visits a debug call target in the instruction stream.
6623  virtual void VisitDebugTarget(RelocInfo* rinfo);
6624
6625  // Handy shorthand for visiting a single pointer.
6626  virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
6627
6628  // Visits a contiguous arrays of external references (references to the C++
6629  // heap) in the half-open range [start, end). Any or all of the values
6630  // may be modified on return.
6631  virtual void VisitExternalReferences(Address* start, Address* end) {}
6632
6633  inline void VisitExternalReference(Address* p) {
6634    VisitExternalReferences(p, p + 1);
6635  }
6636
6637  // Visits a handle that has an embedder-assigned class ID.
6638  virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
6639
6640#ifdef DEBUG
6641  // Intended for serialization/deserialization checking: insert, or
6642  // check for the presence of, a tag at this position in the stream.
6643  virtual void Synchronize(const char* tag) {}
6644#else
6645  inline void Synchronize(const char* tag) {}
6646#endif
6647};
6648
6649
6650class StructBodyDescriptor : public
6651  FlexibleBodyDescriptor<HeapObject::kHeaderSize> {
6652 public:
6653  static inline int SizeOf(Map* map, HeapObject* object) {
6654    return map->instance_size();
6655  }
6656};
6657
6658
6659// BooleanBit is a helper class for setting and getting a bit in an
6660// integer or Smi.
6661class BooleanBit : public AllStatic {
6662 public:
6663  static inline bool get(Smi* smi, int bit_position) {
6664    return get(smi->value(), bit_position);
6665  }
6666
6667  static inline bool get(int value, int bit_position) {
6668    return (value & (1 << bit_position)) != 0;
6669  }
6670
6671  static inline Smi* set(Smi* smi, int bit_position, bool v) {
6672    return Smi::FromInt(set(smi->value(), bit_position, v));
6673  }
6674
6675  static inline int set(int value, int bit_position, bool v) {
6676    if (v) {
6677      value |= (1 << bit_position);
6678    } else {
6679      value &= ~(1 << bit_position);
6680    }
6681    return value;
6682  }
6683};
6684
6685} }  // namespace v8::internal
6686
6687#endif  // V8_OBJECTS_H_
6688