The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / src / objects.h @ 40c0f755

History | View | Annotate | Download (154 KB)

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