blob: cbc9c04cddb75d9ce2c1eecc8bdd360b727f8446 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_OBJECTS_H_
6#define V8_OBJECTS_H_
7
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include <iosfwd>
9
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/assert-scope.h"
11#include "src/bailout-reason.h"
12#include "src/base/bits.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/base/flags.h"
14#include "src/base/smart-pointers.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/builtins.h"
16#include "src/checks.h"
17#include "src/elements-kind.h"
18#include "src/field-index.h"
19#include "src/flags.h"
20#include "src/list.h"
21#include "src/property-details.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#include "src/unicode.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040023#include "src/unicode-decoder.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024#include "src/zone.h"
25
Steve Block3ce2e202009-11-05 08:53:23 +000026#if V8_TARGET_ARCH_ARM
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027#include "src/arm/constants-arm.h" // NOLINT
28#elif V8_TARGET_ARCH_ARM64
29#include "src/arm64/constants-arm64.h" // NOLINT
Andrei Popescu31002712010-02-23 13:46:05 +000030#elif V8_TARGET_ARCH_MIPS
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031#include "src/mips/constants-mips.h" // NOLINT
32#elif V8_TARGET_ARCH_MIPS64
33#include "src/mips64/constants-mips64.h" // NOLINT
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034#elif V8_TARGET_ARCH_PPC
35#include "src/ppc/constants-ppc.h" // NOLINT
Ben Murdochda12d292016-06-02 14:46:10 +010036#elif V8_TARGET_ARCH_S390
37#include "src/s390/constants-s390.h" // NOLINT
Steve Block3ce2e202009-11-05 08:53:23 +000038#endif
Ben Murdoch3ef787d2012-04-12 10:51:47 +010039
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41//
Kristian Monsen50ef84f2010-07-29 15:18:00 +010042// Most object types in the V8 JavaScript are described in this file.
Steve Blocka7e24c12009-10-30 11:49:00 +000043//
44// Inheritance hierarchy:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045// - Object
46// - Smi (immediate small integer)
47// - HeapObject (superclass for everything allocated in the heap)
48// - JSReceiver (suitable for property access)
49// - JSObject
50// - JSArray
51// - JSArrayBuffer
52// - JSArrayBufferView
53// - JSTypedArray
54// - JSDataView
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055// - JSBoundFunction
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056// - JSCollection
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057// - JSSet
58// - JSMap
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059// - JSSetIterator
60// - JSMapIterator
61// - JSWeakCollection
Ben Murdoch69a99ed2011-11-30 16:03:39 +000062// - JSWeakMap
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063// - JSWeakSet
64// - JSRegExp
65// - JSFunction
66// - JSGeneratorObject
67// - JSModule
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068// - JSGlobalObject
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069// - JSGlobalProxy
70// - JSValue
71// - JSDate
72// - JSMessageObject
73// - JSProxy
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074// - FixedArrayBase
75// - ByteArray
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076// - BytecodeArray
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077// - FixedArray
78// - DescriptorArray
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000079// - LiteralsArray
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080// - HashTable
81// - Dictionary
82// - StringTable
Ben Murdochda12d292016-06-02 14:46:10 +010083// - StringSet
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084// - CompilationCacheTable
85// - CodeCacheHashTable
86// - MapCache
87// - OrderedHashTable
88// - OrderedHashSet
89// - OrderedHashMap
90// - Context
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091// - TypeFeedbackMetadata
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092// - TypeFeedbackVector
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093// - ScopeInfo
94// - TransitionArray
Emily Bernierd0a1eb72015-03-24 16:35:39 -040095// - ScriptContextTable
96// - WeakFixedArray
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097// - FixedDoubleArray
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098// - Name
Steve Blocka7e24c12009-10-30 11:49:00 +000099// - String
100// - SeqString
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101// - SeqOneByteString
Steve Blocka7e24c12009-10-30 11:49:00 +0000102// - SeqTwoByteString
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000103// - SlicedString
Steve Blocka7e24c12009-10-30 11:49:00 +0000104// - ConsString
Steve Blocka7e24c12009-10-30 11:49:00 +0000105// - ExternalString
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106// - ExternalOneByteString
Steve Blocka7e24c12009-10-30 11:49:00 +0000107// - ExternalTwoByteString
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108// - InternalizedString
109// - SeqInternalizedString
110// - SeqOneByteInternalizedString
111// - SeqTwoByteInternalizedString
112// - ConsInternalizedString
113// - ExternalInternalizedString
114// - ExternalOneByteInternalizedString
115// - ExternalTwoByteInternalizedString
116// - Symbol
117// - HeapNumber
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118// - Simd128Value
119// - Float32x4
120// - Int32x4
121// - Uint32x4
122// - Bool32x4
123// - Int16x8
124// - Uint16x8
125// - Bool16x8
126// - Int8x16
127// - Uint8x16
128// - Bool8x16
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129// - Cell
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000130// - PropertyCell
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131// - Code
Ben Murdoch097c5b22016-05-18 11:27:45 +0100132// - AbstractCode, a wrapper around Code or BytecodeArray
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133// - Map
134// - Oddball
135// - Foreign
136// - SharedFunctionInfo
137// - Struct
138// - Box
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139// - AccessorInfo
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140// - AccessorPair
141// - AccessCheckInfo
142// - InterceptorInfo
143// - CallHandlerInfo
144// - TemplateInfo
145// - FunctionTemplateInfo
146// - ObjectTemplateInfo
147// - Script
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148// - DebugInfo
149// - BreakPointInfo
150// - CodeCache
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151// - PrototypeInfo
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400152// - WeakCell
Steve Blocka7e24c12009-10-30 11:49:00 +0000153//
154// Formats of Object*:
155// Smi: [31 bit signed int] 0
156// HeapObject: [32 bit direct pointer] (4 byte aligned) | 01
Steve Blocka7e24c12009-10-30 11:49:00 +0000157
Steve Blocka7e24c12009-10-30 11:49:00 +0000158namespace v8 {
159namespace internal {
160
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161enum KeyedAccessStoreMode {
162 STANDARD_STORE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 STORE_TRANSITION_TO_OBJECT,
164 STORE_TRANSITION_TO_DOUBLE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 STORE_AND_GROW_NO_TRANSITION,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166 STORE_AND_GROW_TRANSITION_TO_OBJECT,
167 STORE_AND_GROW_TRANSITION_TO_DOUBLE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS,
169 STORE_NO_TRANSITION_HANDLE_COW
Ben Murdoch589d6972011-11-30 16:04:58 +0000170};
171
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173// Valid hints for the abstract operation ToPrimitive,
174// implemented according to ES6, section 7.1.1.
175enum class ToPrimitiveHint { kDefault, kNumber, kString };
176
177
178// Valid hints for the abstract operation OrdinaryToPrimitive,
179// implemented according to ES6, section 7.1.1.
180enum class OrdinaryToPrimitiveHint { kNumber, kString };
181
182
183enum TypeofMode : int { INSIDE_TYPEOF, NOT_INSIDE_TYPEOF };
Ben Murdochc7cc0282012-03-05 14:35:55 +0000184
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185
186enum MutableMode {
187 MUTABLE,
188 IMMUTABLE
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100189};
190
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100191
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000192enum ExternalArrayType {
193 kExternalInt8Array = 1,
194 kExternalUint8Array,
195 kExternalInt16Array,
196 kExternalUint16Array,
197 kExternalInt32Array,
198 kExternalUint32Array,
199 kExternalFloat32Array,
200 kExternalFloat64Array,
201 kExternalUint8ClampedArray,
202};
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203
204
205static inline bool IsTransitionStoreMode(KeyedAccessStoreMode store_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000206 return store_mode == STORE_TRANSITION_TO_OBJECT ||
207 store_mode == STORE_TRANSITION_TO_DOUBLE ||
208 store_mode == STORE_AND_GROW_TRANSITION_TO_OBJECT ||
209 store_mode == STORE_AND_GROW_TRANSITION_TO_DOUBLE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210}
211
212
213static inline KeyedAccessStoreMode GetNonTransitioningStoreMode(
214 KeyedAccessStoreMode store_mode) {
215 if (store_mode >= STORE_NO_TRANSITION_IGNORE_OUT_OF_BOUNDS) {
216 return store_mode;
217 }
218 if (store_mode >= STORE_AND_GROW_NO_TRANSITION) {
219 return STORE_AND_GROW_NO_TRANSITION;
220 }
221 return STANDARD_STORE;
222}
223
224
225static inline bool IsGrowStoreMode(KeyedAccessStoreMode store_mode) {
226 return store_mode >= STORE_AND_GROW_NO_TRANSITION &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000227 store_mode <= STORE_AND_GROW_TRANSITION_TO_DOUBLE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000228}
229
Steve Blocka7e24c12009-10-30 11:49:00 +0000230
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400231enum IcCheckType { ELEMENT, PROPERTY };
232
233
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000234// SKIP_WRITE_BARRIER skips the write barrier.
235// UPDATE_WEAK_WRITE_BARRIER skips the marking part of the write barrier and
236// only performs the generational part.
237// UPDATE_WRITE_BARRIER is doing the full barrier, marking and generational.
238enum WriteBarrierMode {
239 SKIP_WRITE_BARRIER,
240 UPDATE_WEAK_WRITE_BARRIER,
241 UPDATE_WRITE_BARRIER
242};
Steve Blocka7e24c12009-10-30 11:49:00 +0000243
244
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245// Indicates whether a value can be loaded as a constant.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000246enum StoreMode { ALLOW_IN_DESCRIPTOR, FORCE_FIELD };
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247
248
Steve Blocka7e24c12009-10-30 11:49:00 +0000249// PropertyNormalizationMode is used to specify whether to keep
250// inobject properties when normalizing properties of a JSObject.
251enum PropertyNormalizationMode {
252 CLEAR_INOBJECT_PROPERTIES,
253 KEEP_INOBJECT_PROPERTIES
254};
255
256
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257// Indicates how aggressively the prototype should be optimized. FAST_PROTOTYPE
258// will give the fastest result by tailoring the map to the prototype, but that
259// will cause polymorphism with other objects. REGULAR_PROTOTYPE is to be used
260// (at least for now) when dynamically modifying the prototype chain of an
261// object using __proto__ or Object.setPrototypeOf.
262enum PrototypeOptimizationMode { REGULAR_PROTOTYPE, FAST_PROTOTYPE };
263
264
265// Indicates whether transitions can be added to a source map or not.
266enum TransitionFlag {
267 INSERT_TRANSITION,
268 OMIT_TRANSITION
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100269};
270
271
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272// Indicates whether the transition is simple: the target map of the transition
273// either extends the current map with a new property, or it modifies the
274// property that was added last to the current map.
275enum SimpleTransitionFlag {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400276 SIMPLE_PROPERTY_TRANSITION,
277 PROPERTY_TRANSITION,
278 SPECIAL_TRANSITION
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279};
280
281
282// Indicates whether we are only interested in the descriptors of a particular
283// map, or in all descriptors in the descriptor array.
284enum DescriptorFlag {
285 ALL_DESCRIPTORS,
286 OWN_DESCRIPTORS
287};
288
289// The GC maintains a bit of information, the MarkingParity, which toggles
290// from odd to even and back every time marking is completed. Incremental
291// marking can visit an object twice during a marking phase, so algorithms that
292// that piggy-back on marking can use the parity to ensure that they only
293// perform an operation on an object once per marking phase: they record the
294// MarkingParity when they visit an object, and only re-visit the object when it
295// is marked again and the MarkingParity changes.
296enum MarkingParity {
297 NO_MARKING_PARITY,
298 ODD_MARKING_PARITY,
299 EVEN_MARKING_PARITY
300};
301
302// ICs store extra state in a Code object. The default extra state is
303// kNoExtraICState.
304typedef int ExtraICState;
305static const ExtraICState kNoExtraICState = 0;
306
Steve Block791712a2010-08-27 10:21:07 +0100307// Instance size sentinel for objects of variable size.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100308const int kVariableSizeSentinel = 0;
Steve Block791712a2010-08-27 10:21:07 +0100309
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310// We may store the unsigned bit field as signed Smi value and do not
311// use the sign bit.
312const int kStubMajorKeyBits = 7;
313const int kStubMinorKeyBits = kSmiValueSize - kStubMajorKeyBits - 1;
Steve Block791712a2010-08-27 10:21:07 +0100314
Steve Blocka7e24c12009-10-30 11:49:00 +0000315// All Maps have a field instance_type containing a InstanceType.
316// It describes the type of the instances.
317//
318// As an example, a JavaScript object is a heap object and its map
319// instance_type is JS_OBJECT_TYPE.
320//
321// The names of the string instance types are intended to systematically
Leon Clarkee46be812010-01-19 14:06:41 +0000322// mirror their encoding in the instance_type field of the map. The default
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323// encoding is considered TWO_BYTE. It is not mentioned in the name. ONE_BYTE
Leon Clarkee46be812010-01-19 14:06:41 +0000324// encoding is mentioned explicitly in the name. Likewise, the default
325// representation is considered sequential. It is not mentioned in the
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100326// name. The other representations (e.g. CONS, EXTERNAL) are explicitly
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327// mentioned. Finally, the string is either a STRING_TYPE (if it is a normal
328// string) or a INTERNALIZED_STRING_TYPE (if it is a internalized string).
Steve Blocka7e24c12009-10-30 11:49:00 +0000329//
330// NOTE: The following things are some that depend on the string types having
331// instance_types that are less than those of all other types:
332// HeapObject::Size, HeapObject::IterateBody, the typeof operator, and
333// Object::IsString.
334//
335// NOTE: Everything following JS_VALUE_TYPE is considered a
336// JSObject for GC purposes. The first four entries here have typeof
337// 'object', whereas JS_FUNCTION_TYPE has typeof 'function'.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338#define INSTANCE_TYPE_LIST(V) \
339 V(STRING_TYPE) \
340 V(ONE_BYTE_STRING_TYPE) \
341 V(CONS_STRING_TYPE) \
342 V(CONS_ONE_BYTE_STRING_TYPE) \
343 V(SLICED_STRING_TYPE) \
344 V(SLICED_ONE_BYTE_STRING_TYPE) \
345 V(EXTERNAL_STRING_TYPE) \
346 V(EXTERNAL_ONE_BYTE_STRING_TYPE) \
347 V(EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE) \
348 V(SHORT_EXTERNAL_STRING_TYPE) \
349 V(SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE) \
350 V(SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE) \
351 \
352 V(INTERNALIZED_STRING_TYPE) \
353 V(ONE_BYTE_INTERNALIZED_STRING_TYPE) \
354 V(EXTERNAL_INTERNALIZED_STRING_TYPE) \
355 V(EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE) \
356 V(EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE) \
357 V(SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE) \
358 V(SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE) \
359 V(SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE) \
360 \
361 V(SYMBOL_TYPE) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000362 V(SIMD128_VALUE_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363 \
364 V(MAP_TYPE) \
365 V(CODE_TYPE) \
366 V(ODDBALL_TYPE) \
367 V(CELL_TYPE) \
368 V(PROPERTY_CELL_TYPE) \
369 \
370 V(HEAP_NUMBER_TYPE) \
371 V(MUTABLE_HEAP_NUMBER_TYPE) \
372 V(FOREIGN_TYPE) \
373 V(BYTE_ARRAY_TYPE) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000374 V(BYTECODE_ARRAY_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375 V(FREE_SPACE_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 \
377 V(FIXED_INT8_ARRAY_TYPE) \
378 V(FIXED_UINT8_ARRAY_TYPE) \
379 V(FIXED_INT16_ARRAY_TYPE) \
380 V(FIXED_UINT16_ARRAY_TYPE) \
381 V(FIXED_INT32_ARRAY_TYPE) \
382 V(FIXED_UINT32_ARRAY_TYPE) \
383 V(FIXED_FLOAT32_ARRAY_TYPE) \
384 V(FIXED_FLOAT64_ARRAY_TYPE) \
385 V(FIXED_UINT8_CLAMPED_ARRAY_TYPE) \
386 \
387 V(FILLER_TYPE) \
388 \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100389 V(ACCESSOR_INFO_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 V(ACCESSOR_PAIR_TYPE) \
391 V(ACCESS_CHECK_INFO_TYPE) \
392 V(INTERCEPTOR_INFO_TYPE) \
393 V(CALL_HANDLER_INFO_TYPE) \
394 V(FUNCTION_TEMPLATE_INFO_TYPE) \
395 V(OBJECT_TEMPLATE_INFO_TYPE) \
396 V(SIGNATURE_INFO_TYPE) \
397 V(TYPE_SWITCH_INFO_TYPE) \
398 V(ALLOCATION_MEMENTO_TYPE) \
399 V(ALLOCATION_SITE_TYPE) \
400 V(SCRIPT_TYPE) \
401 V(CODE_CACHE_TYPE) \
402 V(POLYMORPHIC_CODE_CACHE_TYPE) \
403 V(TYPE_FEEDBACK_INFO_TYPE) \
404 V(ALIASED_ARGUMENTS_ENTRY_TYPE) \
405 V(BOX_TYPE) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000406 V(PROTOTYPE_INFO_TYPE) \
407 V(SLOPPY_BLOCK_WITH_EVAL_CONTEXT_EXTENSION_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000408 \
409 V(FIXED_ARRAY_TYPE) \
410 V(FIXED_DOUBLE_ARRAY_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411 V(SHARED_FUNCTION_INFO_TYPE) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400412 V(WEAK_CELL_TYPE) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413 V(TRANSITION_ARRAY_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000414 \
415 V(JS_MESSAGE_OBJECT_TYPE) \
416 \
417 V(JS_VALUE_TYPE) \
418 V(JS_DATE_TYPE) \
419 V(JS_OBJECT_TYPE) \
420 V(JS_CONTEXT_EXTENSION_OBJECT_TYPE) \
421 V(JS_GENERATOR_OBJECT_TYPE) \
422 V(JS_MODULE_TYPE) \
423 V(JS_GLOBAL_OBJECT_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000424 V(JS_GLOBAL_PROXY_TYPE) \
Ben Murdochda12d292016-06-02 14:46:10 +0100425 V(JS_SPECIAL_API_OBJECT_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000426 V(JS_ARRAY_TYPE) \
427 V(JS_ARRAY_BUFFER_TYPE) \
428 V(JS_TYPED_ARRAY_TYPE) \
429 V(JS_DATA_VIEW_TYPE) \
430 V(JS_PROXY_TYPE) \
431 V(JS_SET_TYPE) \
432 V(JS_MAP_TYPE) \
433 V(JS_SET_ITERATOR_TYPE) \
434 V(JS_MAP_ITERATOR_TYPE) \
435 V(JS_WEAK_MAP_TYPE) \
436 V(JS_WEAK_SET_TYPE) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437 V(JS_PROMISE_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 V(JS_REGEXP_TYPE) \
439 \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000440 V(JS_BOUND_FUNCTION_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441 V(JS_FUNCTION_TYPE) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 V(DEBUG_INFO_TYPE) \
Steve Blocka7e24c12009-10-30 11:49:00 +0000443 V(BREAK_POINT_INFO_TYPE)
Steve Blocka7e24c12009-10-30 11:49:00 +0000444
Steve Blocka7e24c12009-10-30 11:49:00 +0000445// Since string types are not consecutive, this macro is used to
446// iterate over them.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447#define STRING_TYPE_LIST(V) \
448 V(STRING_TYPE, kVariableSizeSentinel, string, String) \
449 V(ONE_BYTE_STRING_TYPE, kVariableSizeSentinel, one_byte_string, \
450 OneByteString) \
451 V(CONS_STRING_TYPE, ConsString::kSize, cons_string, ConsString) \
452 V(CONS_ONE_BYTE_STRING_TYPE, ConsString::kSize, cons_one_byte_string, \
453 ConsOneByteString) \
454 V(SLICED_STRING_TYPE, SlicedString::kSize, sliced_string, SlicedString) \
455 V(SLICED_ONE_BYTE_STRING_TYPE, SlicedString::kSize, sliced_one_byte_string, \
456 SlicedOneByteString) \
457 V(EXTERNAL_STRING_TYPE, ExternalTwoByteString::kSize, external_string, \
458 ExternalString) \
459 V(EXTERNAL_ONE_BYTE_STRING_TYPE, ExternalOneByteString::kSize, \
460 external_one_byte_string, ExternalOneByteString) \
461 V(EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE, ExternalTwoByteString::kSize, \
462 external_string_with_one_byte_data, ExternalStringWithOneByteData) \
463 V(SHORT_EXTERNAL_STRING_TYPE, ExternalTwoByteString::kShortSize, \
464 short_external_string, ShortExternalString) \
465 V(SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE, ExternalOneByteString::kShortSize, \
466 short_external_one_byte_string, ShortExternalOneByteString) \
467 V(SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE, \
468 ExternalTwoByteString::kShortSize, \
469 short_external_string_with_one_byte_data, \
470 ShortExternalStringWithOneByteData) \
471 \
472 V(INTERNALIZED_STRING_TYPE, kVariableSizeSentinel, internalized_string, \
473 InternalizedString) \
474 V(ONE_BYTE_INTERNALIZED_STRING_TYPE, kVariableSizeSentinel, \
475 one_byte_internalized_string, OneByteInternalizedString) \
476 V(EXTERNAL_INTERNALIZED_STRING_TYPE, ExternalTwoByteString::kSize, \
477 external_internalized_string, ExternalInternalizedString) \
478 V(EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE, ExternalOneByteString::kSize, \
479 external_one_byte_internalized_string, ExternalOneByteInternalizedString) \
480 V(EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE, \
481 ExternalTwoByteString::kSize, \
482 external_internalized_string_with_one_byte_data, \
483 ExternalInternalizedStringWithOneByteData) \
484 V(SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE, \
485 ExternalTwoByteString::kShortSize, short_external_internalized_string, \
486 ShortExternalInternalizedString) \
487 V(SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE, \
488 ExternalOneByteString::kShortSize, \
489 short_external_one_byte_internalized_string, \
490 ShortExternalOneByteInternalizedString) \
491 V(SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE, \
492 ExternalTwoByteString::kShortSize, \
493 short_external_internalized_string_with_one_byte_data, \
494 ShortExternalInternalizedStringWithOneByteData)
Steve Blocka7e24c12009-10-30 11:49:00 +0000495
496// A struct is a simple object a set of object-valued fields. Including an
497// object type in this causes the compiler to generate most of the boilerplate
498// code for the class including allocation and garbage collection routines,
499// casts and predicates. All you need to define is the class, methods and
500// object verification routines. Easy, no?
501//
502// Note that for subtle reasons related to the ordering or numerical values of
503// type tags, elements in this list have to be added to the INSTANCE_TYPE_LIST
504// manually.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000505#define STRUCT_LIST(V) \
506 V(BOX, Box, box) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100507 V(ACCESSOR_INFO, AccessorInfo, accessor_info) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000508 V(ACCESSOR_PAIR, AccessorPair, accessor_pair) \
509 V(ACCESS_CHECK_INFO, AccessCheckInfo, access_check_info) \
510 V(INTERCEPTOR_INFO, InterceptorInfo, interceptor_info) \
511 V(CALL_HANDLER_INFO, CallHandlerInfo, call_handler_info) \
512 V(FUNCTION_TEMPLATE_INFO, FunctionTemplateInfo, function_template_info) \
513 V(OBJECT_TEMPLATE_INFO, ObjectTemplateInfo, object_template_info) \
514 V(SCRIPT, Script, script) \
515 V(ALLOCATION_SITE, AllocationSite, allocation_site) \
516 V(ALLOCATION_MEMENTO, AllocationMemento, allocation_memento) \
517 V(CODE_CACHE, CodeCache, code_cache) \
518 V(POLYMORPHIC_CODE_CACHE, PolymorphicCodeCache, polymorphic_code_cache) \
519 V(TYPE_FEEDBACK_INFO, TypeFeedbackInfo, type_feedback_info) \
520 V(ALIASED_ARGUMENTS_ENTRY, AliasedArgumentsEntry, aliased_arguments_entry) \
521 V(DEBUG_INFO, DebugInfo, debug_info) \
522 V(BREAK_POINT_INFO, BreakPointInfo, break_point_info) \
523 V(PROTOTYPE_INFO, PrototypeInfo, prototype_info) \
524 V(SLOPPY_BLOCK_WITH_EVAL_CONTEXT_EXTENSION, \
525 SloppyBlockWithEvalContextExtension, \
526 sloppy_block_with_eval_context_extension)
Steve Blocka7e24c12009-10-30 11:49:00 +0000527
528// We use the full 8 bits of the instance_type field to encode heap object
529// instance types. The high-order bit (bit 7) is set if the object is not a
530// string, and cleared if it is a string.
531const uint32_t kIsNotStringMask = 0x80;
532const uint32_t kStringTag = 0x0;
533const uint32_t kNotStringTag = 0x80;
534
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535// Bit 6 indicates that the object is an internalized string (if set) or not.
536// Bit 7 has to be clear as well.
537const uint32_t kIsNotInternalizedMask = 0x40;
538const uint32_t kNotInternalizedTag = 0x40;
539const uint32_t kInternalizedTag = 0x0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000540
Steve Blocka7e24c12009-10-30 11:49:00 +0000541// If bit 7 is clear then bit 2 indicates whether the string consists of
542// two-byte characters or one-byte characters.
543const uint32_t kStringEncodingMask = 0x4;
544const uint32_t kTwoByteStringTag = 0x0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545const uint32_t kOneByteStringTag = 0x4;
Steve Blocka7e24c12009-10-30 11:49:00 +0000546
547// If bit 7 is clear, the low-order 2 bits indicate the representation
548// of the string.
549const uint32_t kStringRepresentationMask = 0x03;
550enum StringRepresentationTag {
551 kSeqStringTag = 0x0,
552 kConsStringTag = 0x1,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000553 kExternalStringTag = 0x2,
554 kSlicedStringTag = 0x3
Steve Blocka7e24c12009-10-30 11:49:00 +0000555};
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000556const uint32_t kIsIndirectStringMask = 0x1;
557const uint32_t kIsIndirectStringTag = 0x1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558STATIC_ASSERT((kSeqStringTag & kIsIndirectStringMask) == 0); // NOLINT
559STATIC_ASSERT((kExternalStringTag & kIsIndirectStringMask) == 0); // NOLINT
560STATIC_ASSERT((kConsStringTag &
561 kIsIndirectStringMask) == kIsIndirectStringTag); // NOLINT
562STATIC_ASSERT((kSlicedStringTag &
563 kIsIndirectStringMask) == kIsIndirectStringTag); // NOLINT
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000564
565// Use this mask to distinguish between cons and slice only after making
566// sure that the string is one of the two (an indirect string).
567const uint32_t kSlicedNotConsMask = kSlicedStringTag & ~kConsStringTag;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568STATIC_ASSERT(IS_POWER_OF_TWO(kSlicedNotConsMask));
Steve Blocka7e24c12009-10-30 11:49:00 +0000569
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100570// If bit 7 is clear, then bit 3 indicates whether this two-byte
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571// string actually contains one byte data.
572const uint32_t kOneByteDataHintMask = 0x08;
573const uint32_t kOneByteDataHintTag = 0x08;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100574
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100575// If bit 7 is clear and string representation indicates an external string,
576// then bit 4 indicates whether the data pointer is cached.
577const uint32_t kShortExternalStringMask = 0x10;
578const uint32_t kShortExternalStringTag = 0x10;
579
Steve Blocka7e24c12009-10-30 11:49:00 +0000580
581// A ConsString with an empty string as the right side is a candidate
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000582// for being shortcut by the garbage collector. We don't allocate any
583// non-flat internalized strings, so we do not shortcut them thereby
584// avoiding turning internalized strings into strings. The bit-masks
585// below contain the internalized bit as additional safety.
586// See heap.cc, mark-compact.cc and objects-visiting.cc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000587const uint32_t kShortcutTypeMask =
588 kIsNotStringMask |
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 kIsNotInternalizedMask |
Steve Blocka7e24c12009-10-30 11:49:00 +0000590 kStringRepresentationMask;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591const uint32_t kShortcutTypeTag = kConsStringTag | kNotInternalizedTag;
592
593static inline bool IsShortcutCandidate(int type) {
594 return ((type & kShortcutTypeMask) == kShortcutTypeTag);
595}
Steve Blocka7e24c12009-10-30 11:49:00 +0000596
Steve Blocka7e24c12009-10-30 11:49:00 +0000597enum InstanceType {
Leon Clarkee46be812010-01-19 14:06:41 +0000598 // String types.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000599 INTERNALIZED_STRING_TYPE = kTwoByteStringTag | kSeqStringTag |
600 kInternalizedTag, // FIRST_PRIMITIVE_TYPE
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 ONE_BYTE_INTERNALIZED_STRING_TYPE =
602 kOneByteStringTag | kSeqStringTag | kInternalizedTag,
603 EXTERNAL_INTERNALIZED_STRING_TYPE =
604 kTwoByteStringTag | kExternalStringTag | kInternalizedTag,
605 EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE =
606 kOneByteStringTag | kExternalStringTag | kInternalizedTag,
607 EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE =
608 EXTERNAL_INTERNALIZED_STRING_TYPE | kOneByteDataHintTag |
609 kInternalizedTag,
610 SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE = EXTERNAL_INTERNALIZED_STRING_TYPE |
611 kShortExternalStringTag |
612 kInternalizedTag,
613 SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE =
614 EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE | kShortExternalStringTag |
615 kInternalizedTag,
616 SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE =
617 EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE |
618 kShortExternalStringTag | kInternalizedTag,
619 STRING_TYPE = INTERNALIZED_STRING_TYPE | kNotInternalizedTag,
620 ONE_BYTE_STRING_TYPE =
621 ONE_BYTE_INTERNALIZED_STRING_TYPE | kNotInternalizedTag,
622 CONS_STRING_TYPE = kTwoByteStringTag | kConsStringTag | kNotInternalizedTag,
623 CONS_ONE_BYTE_STRING_TYPE =
624 kOneByteStringTag | kConsStringTag | kNotInternalizedTag,
625 SLICED_STRING_TYPE =
626 kTwoByteStringTag | kSlicedStringTag | kNotInternalizedTag,
627 SLICED_ONE_BYTE_STRING_TYPE =
628 kOneByteStringTag | kSlicedStringTag | kNotInternalizedTag,
629 EXTERNAL_STRING_TYPE =
630 EXTERNAL_INTERNALIZED_STRING_TYPE | kNotInternalizedTag,
631 EXTERNAL_ONE_BYTE_STRING_TYPE =
632 EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE | kNotInternalizedTag,
633 EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE =
634 EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE |
635 kNotInternalizedTag,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100636 SHORT_EXTERNAL_STRING_TYPE =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 SHORT_EXTERNAL_INTERNALIZED_STRING_TYPE | kNotInternalizedTag,
638 SHORT_EXTERNAL_ONE_BYTE_STRING_TYPE =
639 SHORT_EXTERNAL_ONE_BYTE_INTERNALIZED_STRING_TYPE | kNotInternalizedTag,
640 SHORT_EXTERNAL_STRING_WITH_ONE_BYTE_DATA_TYPE =
641 SHORT_EXTERNAL_INTERNALIZED_STRING_WITH_ONE_BYTE_DATA_TYPE |
642 kNotInternalizedTag,
643
644 // Non-string names
645 SYMBOL_TYPE = kNotStringTag, // FIRST_NONSTRING_TYPE, LAST_NAME_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000646
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000647 // Other primitives (cannot contain non-map-word pointers to heap objects).
648 HEAP_NUMBER_TYPE,
649 SIMD128_VALUE_TYPE,
650 ODDBALL_TYPE, // LAST_PRIMITIVE_TYPE
651
Leon Clarkee46be812010-01-19 14:06:41 +0000652 // Objects allocated in their own spaces (never in new space).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000653 MAP_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000654 CODE_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000655
656 // "Data", objects that cannot contain non-map-word pointers to heap
657 // objects.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000658 MUTABLE_HEAP_NUMBER_TYPE,
Ben Murdoch257744e2011-11-30 15:57:28 +0000659 FOREIGN_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000660 BYTE_ARRAY_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000661 BYTECODE_ARRAY_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100662 FREE_SPACE_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000663 FIXED_INT8_ARRAY_TYPE, // FIRST_FIXED_TYPED_ARRAY_TYPE
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000664 FIXED_UINT8_ARRAY_TYPE,
665 FIXED_INT16_ARRAY_TYPE,
666 FIXED_UINT16_ARRAY_TYPE,
667 FIXED_INT32_ARRAY_TYPE,
668 FIXED_UINT32_ARRAY_TYPE,
669 FIXED_FLOAT32_ARRAY_TYPE,
670 FIXED_FLOAT64_ARRAY_TYPE,
671 FIXED_UINT8_CLAMPED_ARRAY_TYPE, // LAST_FIXED_TYPED_ARRAY_TYPE
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000672 FIXED_DOUBLE_ARRAY_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000673 FILLER_TYPE, // LAST_DATA_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000674
Leon Clarkee46be812010-01-19 14:06:41 +0000675 // Structs.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100676 ACCESSOR_INFO_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100677 ACCESSOR_PAIR_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000678 ACCESS_CHECK_INFO_TYPE,
679 INTERCEPTOR_INFO_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000680 CALL_HANDLER_INFO_TYPE,
681 FUNCTION_TEMPLATE_INFO_TYPE,
682 OBJECT_TEMPLATE_INFO_TYPE,
683 SIGNATURE_INFO_TYPE,
684 TYPE_SWITCH_INFO_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685 ALLOCATION_SITE_TYPE,
686 ALLOCATION_MEMENTO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000687 SCRIPT_TYPE,
Steve Block6ded16b2010-05-10 14:33:55 +0100688 CODE_CACHE_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000689 POLYMORPHIC_CODE_CACHE_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100690 TYPE_FEEDBACK_INFO_TYPE,
691 ALIASED_ARGUMENTS_ENTRY_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000692 BOX_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000693 DEBUG_INFO_TYPE,
694 BREAK_POINT_INFO_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000695 FIXED_ARRAY_TYPE,
696 SHARED_FUNCTION_INFO_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000697 CELL_TYPE,
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400698 WEAK_CELL_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000699 TRANSITION_ARRAY_TYPE,
700 PROPERTY_CELL_TYPE,
701 PROTOTYPE_INFO_TYPE,
702 SLOPPY_BLOCK_WITH_EVAL_CONTEXT_EXTENSION_TYPE,
Leon Clarkee46be812010-01-19 14:06:41 +0000703
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100704 // All the following types are subtypes of JSReceiver, which corresponds to
705 // objects in the JS sense. The first and the last type in this range are
706 // the two forms of function. This organization enables using the same
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000707 // compares for checking the JS_RECEIVER and the NONCALLABLE_JS_OBJECT range.
Ben Murdochda12d292016-06-02 14:46:10 +0100708 JS_PROXY_TYPE, // FIRST_JS_RECEIVER_TYPE
709 JS_GLOBAL_OBJECT_TYPE, // FIRST_JS_OBJECT_TYPE
710 JS_GLOBAL_PROXY_TYPE,
711 // Like JS_OBJECT_TYPE, but requires access checks and/or has interceptors.
712 JS_SPECIAL_API_OBJECT_TYPE, // LAST_SPECIAL_RECEIVER_TYPE
713 JS_VALUE_TYPE, // LAST_CUSTOM_ELEMENTS_RECEIVER
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000714 JS_MESSAGE_OBJECT_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100715 JS_DATE_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000716 JS_OBJECT_TYPE,
717 JS_CONTEXT_EXTENSION_OBJECT_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000718 JS_GENERATOR_OBJECT_TYPE,
719 JS_MODULE_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 JS_ARRAY_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721 JS_ARRAY_BUFFER_TYPE,
722 JS_TYPED_ARRAY_TYPE,
723 JS_DATA_VIEW_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100724 JS_SET_TYPE,
725 JS_MAP_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000726 JS_SET_ITERATOR_TYPE,
727 JS_MAP_ITERATOR_TYPE,
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000728 JS_WEAK_MAP_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729 JS_WEAK_SET_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000730 JS_PROMISE_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100731 JS_REGEXP_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000732 JS_BOUND_FUNCTION_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100733 JS_FUNCTION_TYPE, // LAST_JS_OBJECT_TYPE, LAST_JS_RECEIVER_TYPE
Steve Blocka7e24c12009-10-30 11:49:00 +0000734
735 // Pseudo-types
Steve Blocka7e24c12009-10-30 11:49:00 +0000736 FIRST_TYPE = 0x0,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100737 LAST_TYPE = JS_FUNCTION_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 FIRST_NAME_TYPE = FIRST_TYPE,
739 LAST_NAME_TYPE = SYMBOL_TYPE,
740 FIRST_UNIQUE_NAME_TYPE = INTERNALIZED_STRING_TYPE,
741 LAST_UNIQUE_NAME_TYPE = SYMBOL_TYPE,
742 FIRST_NONSTRING_TYPE = SYMBOL_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000743 FIRST_PRIMITIVE_TYPE = FIRST_NAME_TYPE,
744 LAST_PRIMITIVE_TYPE = ODDBALL_TYPE,
745 FIRST_FUNCTION_TYPE = JS_BOUND_FUNCTION_TYPE,
746 LAST_FUNCTION_TYPE = JS_FUNCTION_TYPE,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000747 // Boundaries for testing for a fixed typed array.
748 FIRST_FIXED_TYPED_ARRAY_TYPE = FIXED_INT8_ARRAY_TYPE,
749 LAST_FIXED_TYPED_ARRAY_TYPE = FIXED_UINT8_CLAMPED_ARRAY_TYPE,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000750 // Boundary for promotion to old space.
Leon Clarkee46be812010-01-19 14:06:41 +0000751 LAST_DATA_TYPE = FILLER_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000752 // Boundary for objects represented as JSReceiver (i.e. JSObject or JSProxy).
753 // Note that there is no range for JSObject or JSProxy, since their subtypes
754 // are not continuous in this enum! The enum ranges instead reflect the
755 // external class names, where proxies are treated as either ordinary objects,
756 // or functions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000757 FIRST_JS_RECEIVER_TYPE = JS_PROXY_TYPE,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000758 LAST_JS_RECEIVER_TYPE = LAST_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100759 // Boundaries for testing the types represented as JSObject
Ben Murdochda12d292016-06-02 14:46:10 +0100760 FIRST_JS_OBJECT_TYPE = JS_GLOBAL_OBJECT_TYPE,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100761 LAST_JS_OBJECT_TYPE = LAST_TYPE,
Ben Murdochda12d292016-06-02 14:46:10 +0100762 // Boundary for testing JSReceivers that need special property lookup handling
763 LAST_SPECIAL_RECEIVER_TYPE = JS_SPECIAL_API_OBJECT_TYPE,
764 // Boundary case for testing JSReceivers that may have elements while having
765 // an empty fixed array as elements backing store. This is true for string
766 // wrappers.
767 LAST_CUSTOM_ELEMENTS_RECEIVER = JS_VALUE_TYPE,
Steve Blocka7e24c12009-10-30 11:49:00 +0000768};
769
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000770STATIC_ASSERT(JS_OBJECT_TYPE == Internals::kJSObjectType);
771STATIC_ASSERT(FIRST_NONSTRING_TYPE == Internals::kFirstNonstringType);
772STATIC_ASSERT(ODDBALL_TYPE == Internals::kOddballType);
773STATIC_ASSERT(FOREIGN_TYPE == Internals::kForeignType);
774
775
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000776std::ostream& operator<<(std::ostream& os, InstanceType instance_type);
777
778
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779#define FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(V) \
780 V(FAST_ELEMENTS_SUB_TYPE) \
781 V(DICTIONARY_ELEMENTS_SUB_TYPE) \
782 V(FAST_PROPERTIES_SUB_TYPE) \
783 V(DICTIONARY_PROPERTIES_SUB_TYPE) \
784 V(MAP_CODE_CACHE_SUB_TYPE) \
785 V(SCOPE_INFO_SUB_TYPE) \
786 V(STRING_TABLE_SUB_TYPE) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000787 V(DESCRIPTOR_ARRAY_SUB_TYPE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788
789enum FixedArraySubInstanceType {
790#define DEFINE_FIXED_ARRAY_SUB_INSTANCE_TYPE(name) name,
791 FIXED_ARRAY_SUB_INSTANCE_TYPE_LIST(DEFINE_FIXED_ARRAY_SUB_INSTANCE_TYPE)
792#undef DEFINE_FIXED_ARRAY_SUB_INSTANCE_TYPE
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000793 LAST_FIXED_ARRAY_SUB_TYPE = DESCRIPTOR_ARRAY_SUB_TYPE
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000794};
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100795
796
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000797// TODO(bmeurer): Remove this in favor of the ComparisonResult below.
Steve Blocka7e24c12009-10-30 11:49:00 +0000798enum CompareResult {
799 LESS = -1,
800 EQUAL = 0,
801 GREATER = 1,
802
803 NOT_EQUAL = GREATER
804};
805
806
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000807// Result of an abstract relational comparison of x and y, implemented according
808// to ES6 section 7.2.11 Abstract Relational Comparison.
809enum class ComparisonResult {
810 kLessThan, // x < y
811 kEqual, // x = y
812 kGreaterThan, // x > y
813 kUndefined // at least one of x or y was undefined or NaN
814};
815
816
817#define DECL_BOOLEAN_ACCESSORS(name) \
818 inline bool name() const; \
819 inline void set_##name(bool value);
820
821#define DECL_INT_ACCESSORS(name) \
822 inline int name() const; \
823 inline void set_##name(int value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000824
825
826#define DECL_ACCESSORS(name, type) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827 inline type* name() const; \
Steve Blocka7e24c12009-10-30 11:49:00 +0000828 inline void set_##name(type* value, \
829 WriteBarrierMode mode = UPDATE_WRITE_BARRIER); \
830
831
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000832#define DECLARE_CAST(type) \
833 INLINE(static type* cast(Object* object)); \
834 INLINE(static const type* cast(const Object* object));
835
836
837class AccessorPair;
838class AllocationSite;
839class AllocationSiteCreationContext;
840class AllocationSiteUsageContext;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000841class Cell;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400842class ConsString;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000843class ElementsAccessor;
844class FixedArrayBase;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000845class FunctionLiteral;
846class JSGlobalObject;
847class KeyAccumulator;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400848class LayoutDescriptor;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000849class LiteralsArray;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850class LookupIterator;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100851class FieldType;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000852class ObjectHashTable;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400853class ObjectVisitor;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000854class PropertyCell;
855class PropertyDescriptor;
856class SafepointEntry;
857class SharedFunctionInfo;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000858class StringStream;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000859class TypeFeedbackInfo;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000860class TypeFeedbackVector;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400861class WeakCell;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000862class TransitionArray;
863
Steve Blocka7e24c12009-10-30 11:49:00 +0000864
865// A template-ized version of the IsXXX functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000866template <class C> inline bool Is(Object* obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000867
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868#ifdef VERIFY_HEAP
869#define DECLARE_VERIFIER(Name) void Name##Verify();
870#else
871#define DECLARE_VERIFIER(Name)
872#endif
Steve Block053d10c2011-06-13 19:13:29 +0100873
Ben Murdochb0fe1622011-05-05 13:52:32 +0100874#ifdef OBJECT_PRINT
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400875#define DECLARE_PRINTER(Name) void Name##Print(std::ostream& os); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000876#else
877#define DECLARE_PRINTER(Name)
Ben Murdochb0fe1622011-05-05 13:52:32 +0100878#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000879
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880#define OBJECT_TYPE_LIST(V) \
881 V(Smi) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100882 V(LayoutDescriptor) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000883 V(HeapObject) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100884 V(Primitive) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 V(Number)
Ben Murdochb8e0da22011-05-16 14:20:40 +0100886
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000887#define HEAP_OBJECT_TYPE_LIST(V) \
888 V(HeapNumber) \
889 V(MutableHeapNumber) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000890 V(Simd128Value) \
891 V(Float32x4) \
892 V(Int32x4) \
893 V(Uint32x4) \
894 V(Bool32x4) \
895 V(Int16x8) \
896 V(Uint16x8) \
897 V(Bool16x8) \
898 V(Int8x16) \
899 V(Uint8x16) \
900 V(Bool8x16) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901 V(Name) \
902 V(UniqueName) \
903 V(String) \
904 V(SeqString) \
905 V(ExternalString) \
906 V(ConsString) \
907 V(SlicedString) \
908 V(ExternalTwoByteString) \
909 V(ExternalOneByteString) \
910 V(SeqTwoByteString) \
911 V(SeqOneByteString) \
912 V(InternalizedString) \
913 V(Symbol) \
914 \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 V(FixedTypedArrayBase) \
916 V(FixedUint8Array) \
917 V(FixedInt8Array) \
918 V(FixedUint16Array) \
919 V(FixedInt16Array) \
920 V(FixedUint32Array) \
921 V(FixedInt32Array) \
922 V(FixedFloat32Array) \
923 V(FixedFloat64Array) \
924 V(FixedUint8ClampedArray) \
925 V(ByteArray) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 V(BytecodeArray) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000927 V(FreeSpace) \
928 V(JSReceiver) \
929 V(JSObject) \
930 V(JSContextExtensionObject) \
931 V(JSGeneratorObject) \
932 V(JSModule) \
933 V(Map) \
934 V(DescriptorArray) \
935 V(TransitionArray) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000936 V(LiteralsArray) \
937 V(TypeFeedbackMetadata) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 V(TypeFeedbackVector) \
939 V(DeoptimizationInputData) \
940 V(DeoptimizationOutputData) \
941 V(DependentCode) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000942 V(HandlerTable) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943 V(FixedArray) \
944 V(FixedDoubleArray) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400945 V(WeakFixedArray) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000946 V(ArrayList) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000947 V(Context) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400948 V(ScriptContextTable) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000949 V(NativeContext) \
950 V(ScopeInfo) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000951 V(JSBoundFunction) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952 V(JSFunction) \
953 V(Code) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100954 V(AbstractCode) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955 V(Oddball) \
956 V(SharedFunctionInfo) \
957 V(JSValue) \
958 V(JSDate) \
959 V(JSMessageObject) \
960 V(StringWrapper) \
961 V(Foreign) \
962 V(Boolean) \
963 V(JSArray) \
964 V(JSArrayBuffer) \
965 V(JSArrayBufferView) \
966 V(JSTypedArray) \
967 V(JSDataView) \
968 V(JSProxy) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000969 V(JSSet) \
970 V(JSMap) \
971 V(JSSetIterator) \
972 V(JSMapIterator) \
973 V(JSWeakCollection) \
974 V(JSWeakMap) \
975 V(JSWeakSet) \
976 V(JSRegExp) \
977 V(HashTable) \
978 V(Dictionary) \
979 V(StringTable) \
Ben Murdochda12d292016-06-02 14:46:10 +0100980 V(StringSet) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000981 V(NormalizedMapCache) \
982 V(CompilationCacheTable) \
983 V(CodeCacheHashTable) \
984 V(PolymorphicCodeCacheHashTable) \
985 V(MapCache) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000986 V(JSGlobalObject) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000987 V(JSGlobalProxy) \
Ben Murdochda12d292016-06-02 14:46:10 +0100988 V(Undetectable) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000989 V(AccessCheckNeeded) \
Ben Murdoch097c5b22016-05-18 11:27:45 +0100990 V(Callable) \
991 V(Function) \
992 V(Constructor) \
993 V(TemplateInfo) \
994 V(Filler) \
995 V(FixedArrayBase) \
996 V(External) \
997 V(Struct) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000998 V(Cell) \
999 V(PropertyCell) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001000 V(WeakCell) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001001 V(ObjectHashTable) \
1002 V(WeakHashTable) \
1003 V(OrderedHashTable)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001004
Ben Murdoch097c5b22016-05-18 11:27:45 +01001005#define ODDBALL_LIST(V) \
1006 V(Undefined) \
1007 V(Null) \
1008 V(TheHole) \
1009 V(Exception) \
1010 V(Uninitialized) \
1011 V(True) \
1012 V(False) \
Ben Murdochda12d292016-06-02 14:46:10 +01001013 V(ArgumentsMarker) \
1014 V(OptimizedOut)
Ben Murdoch097c5b22016-05-18 11:27:45 +01001015
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001016// The element types selection for CreateListFromArrayLike.
1017enum class ElementTypes { kAll, kStringAndSymbol };
1018
Steve Blocka7e24c12009-10-30 11:49:00 +00001019// Object is the abstract superclass for all classes in the
1020// object hierarchy.
1021// Object does not use any virtual functions to avoid the
1022// allocation of the C++ vtable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001023// Since both Smi and HeapObject are subclasses of Object no
Steve Blocka7e24c12009-10-30 11:49:00 +00001024// data members can be present in Object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001025class Object {
Steve Blocka7e24c12009-10-30 11:49:00 +00001026 public:
1027 // Type testing.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001028 bool IsObject() const { return true; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001029
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001030#define IS_TYPE_FUNCTION_DECL(type_) INLINE(bool Is##type_() const);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001031 OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
1032 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
Ben Murdoch097c5b22016-05-18 11:27:45 +01001033 ODDBALL_LIST(IS_TYPE_FUNCTION_DECL)
Ben Murdochb8e0da22011-05-16 14:20:40 +01001034#undef IS_TYPE_FUNCTION_DECL
Steve Blocka7e24c12009-10-30 11:49:00 +00001035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001036 // A non-keyed store is of the form a.x = foo or a["x"] = foo whereas
1037 // a keyed store is of the form a[expression] = foo.
1038 enum StoreFromKeyed {
1039 MAY_BE_STORE_FROM_KEYED,
1040 CERTAINLY_NOT_STORE_FROM_KEYED
1041 };
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001042
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001043 enum ShouldThrow { THROW_ON_ERROR, DONT_THROW };
1044
1045#define RETURN_FAILURE(isolate, should_throw, call) \
1046 do { \
1047 if ((should_throw) == DONT_THROW) { \
1048 return Just(false); \
1049 } else { \
1050 isolate->Throw(*isolate->factory()->call); \
1051 return Nothing<bool>(); \
1052 } \
1053 } while (false)
1054
1055#define MAYBE_RETURN(call, value) \
1056 do { \
1057 if ((call).IsNothing()) return value; \
1058 } while (false)
1059
1060#define MAYBE_RETURN_NULL(call) MAYBE_RETURN(call, MaybeHandle<Object>())
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001061
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001062#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) \
1063 INLINE(bool Is##Name() const);
Steve Blocka7e24c12009-10-30 11:49:00 +00001064 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
1065#undef DECLARE_STRUCT_PREDICATE
1066
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001067 // ES6, section 7.2.2 IsArray. NOT to be confused with %_IsArray.
1068 MUST_USE_RESULT static Maybe<bool> IsArray(Handle<Object> object);
1069
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001070 INLINE(bool IsNameDictionary() const);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001071 INLINE(bool IsGlobalDictionary() const);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001072 INLINE(bool IsSeededNumberDictionary() const);
1073 INLINE(bool IsUnseededNumberDictionary() const);
1074 INLINE(bool IsOrderedHashSet() const);
1075 INLINE(bool IsOrderedHashMap() const);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001076 static bool IsPromise(Handle<Object> object);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001077
Steve Blocka7e24c12009-10-30 11:49:00 +00001078 // Extract the number.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001079 inline double Number() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001080 INLINE(bool IsNaN() const);
1081 INLINE(bool IsMinusZero() const);
1082 bool ToInt32(int32_t* value);
Ben Murdochda12d292016-06-02 14:46:10 +01001083 inline bool ToUint32(uint32_t* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001085 inline Representation OptimalRepresentation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001086
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001087 inline ElementsKind OptimalElementsKind();
1088
1089 inline bool FitsRepresentation(Representation representation);
1090
1091 // Checks whether two valid primitive encodings of a property name resolve to
1092 // the same logical property. E.g., the smi 1, the string "1" and the double
1093 // 1 all refer to the same property, so this helper will return true.
1094 inline bool KeyEquals(Object* other);
1095
1096 inline bool FilterKey(PropertyFilter filter);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001097
Ben Murdoch097c5b22016-05-18 11:27:45 +01001098 Handle<FieldType> OptimalType(Isolate* isolate,
1099 Representation representation);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001100
1101 inline static Handle<Object> NewStorageFor(Isolate* isolate,
1102 Handle<Object> object,
1103 Representation representation);
1104
1105 inline static Handle<Object> WrapForRead(Isolate* isolate,
1106 Handle<Object> object,
1107 Representation representation);
Steve Blocka7e24c12009-10-30 11:49:00 +00001108
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001109 // Returns true if the object is of the correct type to be used as a
1110 // implementation of a JSObject's elements.
1111 inline bool HasValidElements();
1112
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 inline bool HasSpecificClassOf(String* name);
1114
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001115 bool BooleanValue(); // ECMA-262 9.2.
Steve Blocka7e24c12009-10-30 11:49:00 +00001116
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001117 // ES6 section 7.2.11 Abstract Relational Comparison
Ben Murdoch097c5b22016-05-18 11:27:45 +01001118 MUST_USE_RESULT static Maybe<ComparisonResult> Compare(Handle<Object> x,
1119 Handle<Object> y);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001120
1121 // ES6 section 7.2.12 Abstract Equality Comparison
1122 MUST_USE_RESULT static Maybe<bool> Equals(Handle<Object> x, Handle<Object> y);
1123
1124 // ES6 section 7.2.13 Strict Equality Comparison
1125 bool StrictEquals(Object* that);
1126
Steve Blocka7e24c12009-10-30 11:49:00 +00001127 // Convert to a JSObject if needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128 // native_context is used when creating wrapper object.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001129 MUST_USE_RESULT static inline MaybeHandle<JSReceiver> ToObject(
1130 Isolate* isolate, Handle<Object> object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001131 MUST_USE_RESULT static MaybeHandle<JSReceiver> ToObject(
1132 Isolate* isolate, Handle<Object> object, Handle<Context> context);
Steve Blocka7e24c12009-10-30 11:49:00 +00001133
Ben Murdochda12d292016-06-02 14:46:10 +01001134 // ES6 section 9.2.1.2, OrdinaryCallBindThis for sloppy callee.
1135 MUST_USE_RESULT static MaybeHandle<JSReceiver> ConvertReceiver(
1136 Isolate* isolate, Handle<Object> object);
1137
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001138 // ES6 section 7.1.14 ToPropertyKey
Ben Murdochda12d292016-06-02 14:46:10 +01001139 MUST_USE_RESULT static inline MaybeHandle<Name> ToName(Isolate* isolate,
1140 Handle<Object> input);
Steve Blocka7e24c12009-10-30 11:49:00 +00001141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001142 // ES6 section 7.1.1 ToPrimitive
1143 MUST_USE_RESULT static inline MaybeHandle<Object> ToPrimitive(
1144 Handle<Object> input, ToPrimitiveHint hint = ToPrimitiveHint::kDefault);
Steve Blocka7e24c12009-10-30 11:49:00 +00001145
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001146 // ES6 section 7.1.3 ToNumber
1147 MUST_USE_RESULT static MaybeHandle<Object> ToNumber(Handle<Object> input);
1148
1149 // ES6 section 7.1.4 ToInteger
1150 MUST_USE_RESULT static MaybeHandle<Object> ToInteger(Isolate* isolate,
1151 Handle<Object> input);
1152
1153 // ES6 section 7.1.5 ToInt32
1154 MUST_USE_RESULT static MaybeHandle<Object> ToInt32(Isolate* isolate,
1155 Handle<Object> input);
1156
1157 // ES6 section 7.1.6 ToUint32
1158 MUST_USE_RESULT static MaybeHandle<Object> ToUint32(Isolate* isolate,
1159 Handle<Object> input);
1160
1161 // ES6 section 7.1.12 ToString
1162 MUST_USE_RESULT static MaybeHandle<String> ToString(Isolate* isolate,
1163 Handle<Object> input);
1164
1165 // ES6 section 7.1.15 ToLength
1166 MUST_USE_RESULT static MaybeHandle<Object> ToLength(Isolate* isolate,
1167 Handle<Object> input);
1168
1169 // ES6 section 7.3.9 GetMethod
1170 MUST_USE_RESULT static MaybeHandle<Object> GetMethod(
1171 Handle<JSReceiver> receiver, Handle<Name> name);
1172
1173 // ES6 section 7.3.17 CreateListFromArrayLike
1174 MUST_USE_RESULT static MaybeHandle<FixedArray> CreateListFromArrayLike(
1175 Isolate* isolate, Handle<Object> object, ElementTypes element_types);
1176
1177 // Check whether |object| is an instance of Error or NativeError.
1178 static bool IsErrorObject(Isolate* isolate, Handle<Object> object);
1179
1180 // ES6 section 12.5.6 The typeof Operator
1181 static Handle<String> TypeOf(Isolate* isolate, Handle<Object> object);
1182
1183 // ES6 section 12.6 Multiplicative Operators
Ben Murdoch097c5b22016-05-18 11:27:45 +01001184 MUST_USE_RESULT static MaybeHandle<Object> Multiply(Isolate* isolate,
1185 Handle<Object> lhs,
1186 Handle<Object> rhs);
1187 MUST_USE_RESULT static MaybeHandle<Object> Divide(Isolate* isolate,
1188 Handle<Object> lhs,
1189 Handle<Object> rhs);
1190 MUST_USE_RESULT static MaybeHandle<Object> Modulus(Isolate* isolate,
1191 Handle<Object> lhs,
1192 Handle<Object> rhs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001193
1194 // ES6 section 12.7 Additive Operators
Ben Murdoch097c5b22016-05-18 11:27:45 +01001195 MUST_USE_RESULT static MaybeHandle<Object> Add(Isolate* isolate,
1196 Handle<Object> lhs,
1197 Handle<Object> rhs);
1198 MUST_USE_RESULT static MaybeHandle<Object> Subtract(Isolate* isolate,
1199 Handle<Object> lhs,
1200 Handle<Object> rhs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001201
1202 // ES6 section 12.8 Bitwise Shift Operators
Ben Murdoch097c5b22016-05-18 11:27:45 +01001203 MUST_USE_RESULT static MaybeHandle<Object> ShiftLeft(Isolate* isolate,
1204 Handle<Object> lhs,
1205 Handle<Object> rhs);
1206 MUST_USE_RESULT static MaybeHandle<Object> ShiftRight(Isolate* isolate,
1207 Handle<Object> lhs,
1208 Handle<Object> rhs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001209 MUST_USE_RESULT static MaybeHandle<Object> ShiftRightLogical(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001210 Isolate* isolate, Handle<Object> lhs, Handle<Object> rhs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001211
1212 // ES6 section 12.9 Relational Operators
Ben Murdoch097c5b22016-05-18 11:27:45 +01001213 MUST_USE_RESULT static inline Maybe<bool> GreaterThan(Handle<Object> x,
1214 Handle<Object> y);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001215 MUST_USE_RESULT static inline Maybe<bool> GreaterThanOrEqual(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001216 Handle<Object> x, Handle<Object> y);
1217 MUST_USE_RESULT static inline Maybe<bool> LessThan(Handle<Object> x,
1218 Handle<Object> y);
1219 MUST_USE_RESULT static inline Maybe<bool> LessThanOrEqual(Handle<Object> x,
1220 Handle<Object> y);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001221
1222 // ES6 section 12.11 Binary Bitwise Operators
Ben Murdoch097c5b22016-05-18 11:27:45 +01001223 MUST_USE_RESULT static MaybeHandle<Object> BitwiseAnd(Isolate* isolate,
1224 Handle<Object> lhs,
1225 Handle<Object> rhs);
1226 MUST_USE_RESULT static MaybeHandle<Object> BitwiseOr(Isolate* isolate,
1227 Handle<Object> lhs,
1228 Handle<Object> rhs);
1229 MUST_USE_RESULT static MaybeHandle<Object> BitwiseXor(Isolate* isolate,
1230 Handle<Object> lhs,
1231 Handle<Object> rhs);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001232
Ben Murdoch097c5b22016-05-18 11:27:45 +01001233 MUST_USE_RESULT static MaybeHandle<Object> GetProperty(LookupIterator* it);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001234
1235 // ES6 [[Set]] (when passed DONT_THROW)
1236 // Invariants for this and related functions (unless stated otherwise):
1237 // 1) When the result is Nothing, an exception is pending.
1238 // 2) When passed THROW_ON_ERROR, the result is never Just(false).
1239 // In some cases, an exception is thrown regardless of the ShouldThrow
1240 // argument. These cases are either in accordance with the spec or not
1241 // covered by it (eg., concerning API callbacks).
1242 MUST_USE_RESULT static Maybe<bool> SetProperty(LookupIterator* it,
1243 Handle<Object> value,
1244 LanguageMode language_mode,
1245 StoreFromKeyed store_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246 MUST_USE_RESULT static MaybeHandle<Object> SetProperty(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001247 Handle<Object> object, Handle<Name> name, Handle<Object> value,
1248 LanguageMode language_mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 StoreFromKeyed store_mode = MAY_BE_STORE_FROM_KEYED);
Ben Murdochda12d292016-06-02 14:46:10 +01001250 MUST_USE_RESULT static inline MaybeHandle<Object> SetPropertyOrElement(
1251 Handle<Object> object, Handle<Name> name, Handle<Object> value,
1252 LanguageMode language_mode,
1253 StoreFromKeyed store_mode = MAY_BE_STORE_FROM_KEYED);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001254
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001255 MUST_USE_RESULT static Maybe<bool> SetSuperProperty(
1256 LookupIterator* it, Handle<Object> value, LanguageMode language_mode,
1257 StoreFromKeyed store_mode);
1258
1259 MUST_USE_RESULT static MaybeHandle<Object> ReadAbsentProperty(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001260 LookupIterator* it);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001261 MUST_USE_RESULT static MaybeHandle<Object> ReadAbsentProperty(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001262 Isolate* isolate, Handle<Object> receiver, Handle<Object> name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001263 MUST_USE_RESULT static Maybe<bool> CannotCreateProperty(
1264 Isolate* isolate, Handle<Object> receiver, Handle<Object> name,
1265 Handle<Object> value, ShouldThrow should_throw);
1266 MUST_USE_RESULT static Maybe<bool> WriteToReadOnlyProperty(
1267 LookupIterator* it, Handle<Object> value, ShouldThrow should_throw);
1268 MUST_USE_RESULT static Maybe<bool> WriteToReadOnlyProperty(
1269 Isolate* isolate, Handle<Object> receiver, Handle<Object> name,
1270 Handle<Object> value, ShouldThrow should_throw);
1271 MUST_USE_RESULT static Maybe<bool> RedefineIncompatibleProperty(
1272 Isolate* isolate, Handle<Object> name, Handle<Object> value,
1273 ShouldThrow should_throw);
1274 MUST_USE_RESULT static Maybe<bool> SetDataProperty(LookupIterator* it,
1275 Handle<Object> value);
1276 MUST_USE_RESULT static Maybe<bool> AddDataProperty(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001277 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001278 ShouldThrow should_throw, StoreFromKeyed store_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279 MUST_USE_RESULT static inline MaybeHandle<Object> GetPropertyOrElement(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001280 Handle<Object> object, Handle<Name> name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001281 MUST_USE_RESULT static inline MaybeHandle<Object> GetPropertyOrElement(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001282 Handle<Object> receiver, Handle<Name> name, Handle<JSReceiver> holder);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283 MUST_USE_RESULT static inline MaybeHandle<Object> GetProperty(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001284 Handle<Object> object, Handle<Name> name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001285
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001286 MUST_USE_RESULT static MaybeHandle<Object> GetPropertyWithAccessor(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001287 LookupIterator* it);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001288 MUST_USE_RESULT static Maybe<bool> SetPropertyWithAccessor(
1289 LookupIterator* it, Handle<Object> value, ShouldThrow should_throw);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001290
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001291 MUST_USE_RESULT static MaybeHandle<Object> GetPropertyWithDefinedGetter(
1292 Handle<Object> receiver,
1293 Handle<JSReceiver> getter);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001294 MUST_USE_RESULT static Maybe<bool> SetPropertyWithDefinedSetter(
1295 Handle<Object> receiver, Handle<JSReceiver> setter, Handle<Object> value,
1296 ShouldThrow should_throw);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001297
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001298 MUST_USE_RESULT static inline MaybeHandle<Object> GetElement(
Ben Murdoch097c5b22016-05-18 11:27:45 +01001299 Isolate* isolate, Handle<Object> object, uint32_t index);
Steve Blocka7e24c12009-10-30 11:49:00 +00001300
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001301 MUST_USE_RESULT static inline MaybeHandle<Object> SetElement(
1302 Isolate* isolate, Handle<Object> object, uint32_t index,
1303 Handle<Object> value, LanguageMode language_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001304
1305 // Returns the permanent hash code associated with this object. May return
1306 // undefined if not yet created.
1307 Object* GetHash();
Steve Blocka7e24c12009-10-30 11:49:00 +00001308
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001309 // Returns undefined for JSObjects, but returns the hash code for simple
1310 // objects. This avoids a double lookup in the cases where we know we will
1311 // add the hash to the JSObject if it does not already exist.
1312 Object* GetSimpleHash();
1313
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001314 // Returns the permanent hash code associated with this object depending on
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001315 // the actual object type. May create and store a hash code if needed and none
1316 // exists.
1317 static Handle<Smi> GetOrCreateHash(Isolate* isolate, Handle<Object> object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001318
1319 // Checks whether this object has the same value as the given one. This
1320 // function is implemented according to ES5, section 9.12 and can be used
1321 // to implement the Harmony "egal" function.
1322 bool SameValue(Object* other);
1323
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001324 // Checks whether this object has the same value as the given one.
1325 // +0 and -0 are treated equal. Everything else is the same as SameValue.
1326 // This function is implemented according to ES6, section 7.2.4 and is used
1327 // by ES6 Map and Set.
1328 bool SameValueZero(Object* other);
1329
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001330 // ES6 section 9.4.2.3 ArraySpeciesCreate (part of it)
1331 MUST_USE_RESULT static MaybeHandle<Object> ArraySpeciesConstructor(
1332 Isolate* isolate, Handle<Object> original_array);
1333
1334 // Tries to convert an object to an array length. Returns true and sets the
1335 // output parameter if it succeeds.
1336 inline bool ToArrayLength(uint32_t* index);
1337
1338 // Tries to convert an object to an array index. Returns true and sets the
1339 // output parameter if it succeeds. Equivalent to ToArrayLength, but does not
1340 // allow kMaxUInt32.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001341 inline bool ToArrayIndex(uint32_t* index);
1342
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001343 DECLARE_VERIFIER(Object)
1344#ifdef VERIFY_HEAP
Steve Blocka7e24c12009-10-30 11:49:00 +00001345 // Verify a pointer is a valid object pointer.
1346 static void VerifyPointer(Object* p);
1347#endif
1348
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001349 inline void VerifyApiCallResultType();
1350
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001351 // ES6 19.1.3.6 Object.prototype.toString
1352 MUST_USE_RESULT static MaybeHandle<String> ObjectProtoToString(
1353 Isolate* isolate, Handle<Object> object);
1354
Steve Blocka7e24c12009-10-30 11:49:00 +00001355 // Prints this object without details.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001356 void ShortPrint(FILE* out = stdout);
Steve Blocka7e24c12009-10-30 11:49:00 +00001357
1358 // Prints this object without details to a message accumulator.
1359 void ShortPrint(StringStream* accumulator);
1360
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001361 void ShortPrint(std::ostream& os); // NOLINT
1362
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001363 DECLARE_CAST(Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00001364
1365 // Layout description.
1366 static const int kHeaderSize = 0; // Object does not take up any space.
1367
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001368#ifdef OBJECT_PRINT
1369 // For our gdb macros, we should perhaps change these in the future.
1370 void Print();
1371
1372 // Prints this object with details.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001373 void Print(std::ostream& os); // NOLINT
1374#else
1375 void Print() { ShortPrint(); }
1376 void Print(std::ostream& os) { ShortPrint(os); } // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001377#endif
1378
Steve Blocka7e24c12009-10-30 11:49:00 +00001379 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001380 friend class LookupIterator;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001381 friend class StringStream;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001382
1383 // Return the map of the root of object's prototype chain.
1384 Map* GetRootMap(Isolate* isolate);
1385
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001386 // Helper for SetProperty and SetSuperProperty.
1387 // Return value is only meaningful if [found] is set to true on return.
1388 MUST_USE_RESULT static Maybe<bool> SetPropertyInternal(
1389 LookupIterator* it, Handle<Object> value, LanguageMode language_mode,
1390 StoreFromKeyed store_mode, bool* found);
1391
Ben Murdochda12d292016-06-02 14:46:10 +01001392 MUST_USE_RESULT static MaybeHandle<Name> ConvertToName(Isolate* isolate,
1393 Handle<Object> input);
1394
Steve Blocka7e24c12009-10-30 11:49:00 +00001395 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
1396};
1397
1398
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001399// In objects.h to be usable without objects-inl.h inclusion.
1400bool Object::IsSmi() const { return HAS_SMI_TAG(this); }
1401bool Object::IsHeapObject() const { return Internals::HasHeapObjectTag(this); }
1402
1403
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001404struct Brief {
1405 explicit Brief(const Object* const v) : value(v) {}
1406 const Object* value;
1407};
1408
1409
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001410std::ostream& operator<<(std::ostream& os, const Brief& v);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001411
1412
Steve Blocka7e24c12009-10-30 11:49:00 +00001413// Smi represents integer Numbers that can be stored in 31 bits.
1414// Smis are immediate which means they are NOT allocated in the heap.
Steve Blocka7e24c12009-10-30 11:49:00 +00001415// The this pointer has the following format: [31 bit signed int] 0
Steve Block3ce2e202009-11-05 08:53:23 +00001416// For long smis it has the following format:
1417// [32 bit signed int] [31 bits zero padding] 0
1418// Smi stands for small integer.
Steve Blocka7e24c12009-10-30 11:49:00 +00001419class Smi: public Object {
1420 public:
1421 // Returns the integer value.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001422 inline int value() const { return Internals::SmiValue(this); }
Steve Blocka7e24c12009-10-30 11:49:00 +00001423
1424 // Convert a value to a Smi object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001425 static inline Smi* FromInt(int value) {
1426 DCHECK(Smi::IsValid(value));
1427 return reinterpret_cast<Smi*>(Internals::IntToSmi(value));
1428 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001430 static inline Smi* FromIntptr(intptr_t value) {
1431 DCHECK(Smi::IsValid(value));
1432 int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
1433 return reinterpret_cast<Smi*>((value << smi_shift_bits) | kSmiTag);
1434 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001435
1436 // Returns whether value can be represented in a Smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001437 static inline bool IsValid(intptr_t value) {
1438 bool result = Internals::IsValidSmi(value);
1439 DCHECK_EQ(result, value >= kMinValue && value <= kMaxValue);
1440 return result;
1441 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001442
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001443 DECLARE_CAST(Smi)
Steve Blocka7e24c12009-10-30 11:49:00 +00001444
1445 // Dispatched behavior.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001446 void SmiPrint(std::ostream& os) const; // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001447 DECLARE_VERIFIER(Smi)
Steve Blocka7e24c12009-10-30 11:49:00 +00001448
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001449 static const int kMinValue =
1450 (static_cast<unsigned int>(-1)) << (kSmiValueSize - 1);
Steve Block3ce2e202009-11-05 08:53:23 +00001451 static const int kMaxValue = -(kMinValue + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001452
1453 private:
1454 DISALLOW_IMPLICIT_CONSTRUCTORS(Smi);
1455};
1456
1457
Steve Blocka7e24c12009-10-30 11:49:00 +00001458// Heap objects typically have a map pointer in their first word. However,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001459// during GC other data (e.g. mark bits, forwarding addresses) is sometimes
Steve Blocka7e24c12009-10-30 11:49:00 +00001460// encoded in the first word. The class MapWord is an abstraction of the
1461// value in a heap object's first word.
1462class MapWord BASE_EMBEDDED {
1463 public:
1464 // Normal state: the map word contains a map pointer.
1465
1466 // Create a map word from a map pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001467 static inline MapWord FromMap(const Map* map);
Steve Blocka7e24c12009-10-30 11:49:00 +00001468
1469 // View this map word as a map pointer.
1470 inline Map* ToMap();
1471
1472
1473 // Scavenge collection: the map word of live objects in the from space
1474 // contains a forwarding address (a heap object pointer in the to space).
1475
1476 // True if this map word is a forwarding address for a scavenge
1477 // collection. Only valid during a scavenge collection (specifically,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001478 // when all map words are heap object pointers, i.e. not during a full GC).
Steve Blocka7e24c12009-10-30 11:49:00 +00001479 inline bool IsForwardingAddress();
1480
1481 // Create a map word from a forwarding address.
1482 static inline MapWord FromForwardingAddress(HeapObject* object);
1483
1484 // View this map word as a forwarding address.
1485 inline HeapObject* ToForwardingAddress();
1486
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001487 static inline MapWord FromRawValue(uintptr_t value) {
1488 return MapWord(value);
1489 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001490
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001491 inline uintptr_t ToRawValue() {
1492 return value_;
1493 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001494
1495 private:
1496 // HeapObject calls the private constructor and directly reads the value.
1497 friend class HeapObject;
1498
1499 explicit MapWord(uintptr_t value) : value_(value) {}
1500
1501 uintptr_t value_;
1502};
1503
1504
1505// HeapObject is the superclass for all classes describing heap allocated
1506// objects.
1507class HeapObject: public Object {
1508 public:
1509 // [map]: Contains a map which contains the object's reflective
1510 // information.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001511 inline Map* map() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001512 inline void set_map(Map* value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001513 // The no-write-barrier version. This is OK if the object is white and in
1514 // new space, or if the value is an immortal immutable object, like the maps
1515 // of primitive (non-JS) objects like strings, heap numbers etc.
1516 inline void set_map_no_write_barrier(Map* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00001517
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001518 // Get the map using acquire load.
1519 inline Map* synchronized_map();
1520 inline MapWord synchronized_map_word() const;
1521
1522 // Set the map using release store
1523 inline void synchronized_set_map(Map* value);
1524 inline void synchronized_set_map_no_write_barrier(Map* value);
1525 inline void synchronized_set_map_word(MapWord map_word);
1526
Steve Blocka7e24c12009-10-30 11:49:00 +00001527 // During garbage collection, the map word of a heap object does not
1528 // necessarily contain a map pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001529 inline MapWord map_word() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001530 inline void set_map_word(MapWord map_word);
1531
Steve Block44f0eee2011-05-26 01:26:41 +01001532 // The Heap the object was allocated in. Used also to access Isolate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001533 inline Heap* GetHeap() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001534
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001535 // Convenience method to get current isolate.
1536 inline Isolate* GetIsolate() const;
Steve Block44f0eee2011-05-26 01:26:41 +01001537
Ben Murdoch097c5b22016-05-18 11:27:45 +01001538#define IS_TYPE_FUNCTION_DECL(type_) INLINE(bool Is##type_() const);
1539 HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
1540 ODDBALL_LIST(IS_TYPE_FUNCTION_DECL)
1541#undef IS_TYPE_FUNCTION_DECL
1542#define DECLARE_STRUCT_PREDICATE(NAME, Name, name) \
1543 INLINE(bool Is##Name() const);
1544 STRUCT_LIST(DECLARE_STRUCT_PREDICATE)
1545#undef DECLARE_STRUCT_PREDICATE
1546
Steve Blocka7e24c12009-10-30 11:49:00 +00001547 // Converts an address to a HeapObject pointer.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001548 static inline HeapObject* FromAddress(Address address) {
1549 DCHECK_TAG_ALIGNED(address);
1550 return reinterpret_cast<HeapObject*>(address + kHeapObjectTag);
1551 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001552
1553 // Returns the address of this HeapObject.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001554 inline Address address() {
1555 return reinterpret_cast<Address>(this) - kHeapObjectTag;
1556 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001557
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001558 // Iterates over pointers contained in the object (including the Map).
1559 // If it's not performance critical iteration use the non-templatized
1560 // version.
Steve Blocka7e24c12009-10-30 11:49:00 +00001561 void Iterate(ObjectVisitor* v);
1562
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001563 template <typename ObjectVisitor>
1564 inline void IterateFast(ObjectVisitor* v);
1565
Steve Blocka7e24c12009-10-30 11:49:00 +00001566 // Iterates over all pointers contained in the object except the
1567 // first map pointer. The object type is given in the first
1568 // parameter. This function does not access the map pointer in the
1569 // object, and so is safe to call while the map pointer is modified.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001570 // If it's not performance critical iteration use the non-templatized
1571 // version.
1572 void IterateBody(ObjectVisitor* v);
Steve Blocka7e24c12009-10-30 11:49:00 +00001573 void IterateBody(InstanceType type, int object_size, ObjectVisitor* v);
1574
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001575 template <typename ObjectVisitor>
1576 inline void IterateBodyFast(ObjectVisitor* v);
1577
1578 template <typename ObjectVisitor>
1579 inline void IterateBodyFast(InstanceType type, int object_size,
1580 ObjectVisitor* v);
1581
1582 // Returns true if the object contains a tagged value at given offset.
1583 // It is used for invalid slots filtering. If the offset points outside
1584 // of the object or to the map word, the result is UNDEFINED (!!!).
1585 bool IsValidSlot(int offset);
1586
Steve Blocka7e24c12009-10-30 11:49:00 +00001587 // Returns the heap object's size in bytes
1588 inline int Size();
1589
1590 // Given a heap object's map pointer, returns the heap size in bytes
1591 // Useful when the map pointer field is used for other purposes.
1592 // GC internal.
1593 inline int SizeFromMap(Map* map);
1594
Steve Blocka7e24c12009-10-30 11:49:00 +00001595 // Returns the field at offset in obj, as a read/write Object* reference.
1596 // Does no checking, and is safe to use during GC, while maps are invalid.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001597 // Does not invoke write barrier, so should only be assigned to
Steve Blocka7e24c12009-10-30 11:49:00 +00001598 // during marking GC.
1599 static inline Object** RawField(HeapObject* obj, int offset);
1600
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001601 // Adds the |code| object related to |name| to the code cache of this map. If
1602 // this map is a dictionary map that is shared, the map copied and installed
1603 // onto the object.
1604 static void UpdateMapCodeCache(Handle<HeapObject> object,
1605 Handle<Name> name,
1606 Handle<Code> code);
1607
1608 DECLARE_CAST(HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00001609
Leon Clarke4515c472010-02-03 11:58:03 +00001610 // Return the write barrier mode for this. Callers of this function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001611 // must be able to present a reference to an DisallowHeapAllocation
Leon Clarke4515c472010-02-03 11:58:03 +00001612 // object as a sign that they are not going to use this function
1613 // from code that allocates and thus invalidates the returned write
1614 // barrier mode.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001615 inline WriteBarrierMode GetWriteBarrierMode(
1616 const DisallowHeapAllocation& promise);
Steve Blocka7e24c12009-10-30 11:49:00 +00001617
1618 // Dispatched behavior.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001619 void HeapObjectShortPrint(std::ostream& os); // NOLINT
Ben Murdochb0fe1622011-05-05 13:52:32 +01001620#ifdef OBJECT_PRINT
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001621 void PrintHeader(std::ostream& os, const char* id); // NOLINT
Ben Murdoch85b71792012-04-11 18:30:58 +01001622#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001623 DECLARE_PRINTER(HeapObject)
1624 DECLARE_VERIFIER(HeapObject)
1625#ifdef VERIFY_HEAP
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001626 inline void VerifyObjectField(int offset);
1627 inline void VerifySmiField(int offset);
1628
Steve Blocka7e24c12009-10-30 11:49:00 +00001629 // Verify a pointer is a valid HeapObject pointer that points to object
1630 // areas in the heap.
1631 static void VerifyHeapPointer(Object* p);
1632#endif
1633
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001634 inline AllocationAlignment RequiredAlignment();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001635
Steve Blocka7e24c12009-10-30 11:49:00 +00001636 // Layout description.
1637 // First field in a heap object is map.
1638 static const int kMapOffset = Object::kHeaderSize;
1639 static const int kHeaderSize = kMapOffset + kPointerSize;
1640
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001641 STATIC_ASSERT(kMapOffset == Internals::kHeapObjectMapOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00001642
Steve Blocka7e24c12009-10-30 11:49:00 +00001643 private:
1644 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapObject);
1645};
1646
1647
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001648template <int start_offset, int end_offset, int size>
1649class FixedBodyDescriptor;
Iain Merrick75681382010-08-19 15:07:18 +01001650
1651
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001652template <int start_offset>
1653class FlexibleBodyDescriptor;
Iain Merrick75681382010-08-19 15:07:18 +01001654
Iain Merrick75681382010-08-19 15:07:18 +01001655
Steve Blocka7e24c12009-10-30 11:49:00 +00001656// The HeapNumber class describes heap allocated numbers that cannot be
1657// represented in a Smi (small integer)
1658class HeapNumber: public HeapObject {
1659 public:
1660 // [value]: number value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001661 inline double value() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00001662 inline void set_value(double value);
1663
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664 DECLARE_CAST(HeapNumber)
Steve Blocka7e24c12009-10-30 11:49:00 +00001665
1666 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001667 bool HeapNumberBooleanValue();
1668
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001669 void HeapNumberPrint(std::ostream& os); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001670 DECLARE_VERIFIER(HeapNumber)
Steve Blocka7e24c12009-10-30 11:49:00 +00001671
Steve Block6ded16b2010-05-10 14:33:55 +01001672 inline int get_exponent();
1673 inline int get_sign();
1674
Steve Blocka7e24c12009-10-30 11:49:00 +00001675 // Layout description.
1676 static const int kValueOffset = HeapObject::kHeaderSize;
1677 // IEEE doubles are two 32 bit words. The first is just mantissa, the second
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001678 // is a mixture of sign, exponent and mantissa. The offsets of two 32 bit
1679 // words within double numbers are endian dependent and they are set
1680 // accordingly.
1681#if defined(V8_TARGET_LITTLE_ENDIAN)
Steve Blocka7e24c12009-10-30 11:49:00 +00001682 static const int kMantissaOffset = kValueOffset;
1683 static const int kExponentOffset = kValueOffset + 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001684#elif defined(V8_TARGET_BIG_ENDIAN)
1685 static const int kMantissaOffset = kValueOffset + 4;
1686 static const int kExponentOffset = kValueOffset;
1687#else
1688#error Unknown byte ordering
1689#endif
Ben Murdoch8b112d22011-06-08 16:22:53 +01001690
Steve Blocka7e24c12009-10-30 11:49:00 +00001691 static const int kSize = kValueOffset + kDoubleSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001692 static const uint32_t kSignMask = 0x80000000u;
1693 static const uint32_t kExponentMask = 0x7ff00000u;
1694 static const uint32_t kMantissaMask = 0xfffffu;
Steve Block6ded16b2010-05-10 14:33:55 +01001695 static const int kMantissaBits = 52;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001696 static const int kExponentBits = 11;
Steve Blocka7e24c12009-10-30 11:49:00 +00001697 static const int kExponentBias = 1023;
1698 static const int kExponentShift = 20;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001699 static const int kInfinityOrNanExponent =
1700 (kExponentMask >> kExponentShift) - kExponentBias;
Steve Blocka7e24c12009-10-30 11:49:00 +00001701 static const int kMantissaBitsInTopWord = 20;
1702 static const int kNonMantissaBitsInTopWord = 12;
1703
1704 private:
1705 DISALLOW_IMPLICIT_CONSTRUCTORS(HeapNumber);
1706};
1707
1708
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001709// The Simd128Value class describes heap allocated 128 bit SIMD values.
1710class Simd128Value : public HeapObject {
1711 public:
1712 DECLARE_CAST(Simd128Value)
1713
1714 DECLARE_PRINTER(Simd128Value)
1715 DECLARE_VERIFIER(Simd128Value)
1716
1717 static Handle<String> ToString(Handle<Simd128Value> input);
1718
1719 // Equality operations.
1720 inline bool Equals(Simd128Value* that);
1721 static inline bool Equals(Handle<Simd128Value> one, Handle<Simd128Value> two);
1722
1723 // Checks that another instance is bit-wise equal.
1724 bool BitwiseEquals(const Simd128Value* other) const;
1725 // Computes a hash from the 128 bit value, viewed as 4 32-bit integers.
1726 uint32_t Hash() const;
1727 // Copies the 16 bytes of SIMD data to the destination address.
1728 void CopyBits(void* destination) const;
1729
1730 // Layout description.
1731 static const int kValueOffset = HeapObject::kHeaderSize;
1732 static const int kSize = kValueOffset + kSimd128Size;
1733
1734 private:
1735 DISALLOW_IMPLICIT_CONSTRUCTORS(Simd128Value);
1736};
1737
1738
1739// V has parameters (TYPE, Type, type, lane count, lane type)
1740#define SIMD128_TYPES(V) \
1741 V(FLOAT32X4, Float32x4, float32x4, 4, float) \
1742 V(INT32X4, Int32x4, int32x4, 4, int32_t) \
1743 V(UINT32X4, Uint32x4, uint32x4, 4, uint32_t) \
1744 V(BOOL32X4, Bool32x4, bool32x4, 4, bool) \
1745 V(INT16X8, Int16x8, int16x8, 8, int16_t) \
1746 V(UINT16X8, Uint16x8, uint16x8, 8, uint16_t) \
1747 V(BOOL16X8, Bool16x8, bool16x8, 8, bool) \
1748 V(INT8X16, Int8x16, int8x16, 16, int8_t) \
1749 V(UINT8X16, Uint8x16, uint8x16, 16, uint8_t) \
1750 V(BOOL8X16, Bool8x16, bool8x16, 16, bool)
1751
1752#define SIMD128_VALUE_CLASS(TYPE, Type, type, lane_count, lane_type) \
1753 class Type final : public Simd128Value { \
1754 public: \
1755 inline lane_type get_lane(int lane) const; \
1756 inline void set_lane(int lane, lane_type value); \
1757 \
1758 DECLARE_CAST(Type) \
1759 \
1760 DECLARE_PRINTER(Type) \
1761 \
1762 static Handle<String> ToString(Handle<Type> input); \
1763 \
1764 inline bool Equals(Type* that); \
1765 \
1766 private: \
1767 DISALLOW_IMPLICIT_CONSTRUCTORS(Type); \
1768 };
1769SIMD128_TYPES(SIMD128_VALUE_CLASS)
1770#undef SIMD128_VALUE_CLASS
1771
1772
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001773enum EnsureElementsMode {
1774 DONT_ALLOW_DOUBLE_ELEMENTS,
1775 ALLOW_COPIED_DOUBLE_ELEMENTS,
1776 ALLOW_CONVERTED_DOUBLE_ELEMENTS
1777};
1778
1779
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001780// Indicator for one component of an AccessorPair.
1781enum AccessorComponent {
1782 ACCESSOR_GETTER,
1783 ACCESSOR_SETTER
1784};
1785
1786
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001787enum GetKeysConversion { KEEP_NUMBERS, CONVERT_TO_STRING };
1788
Ben Murdoch097c5b22016-05-18 11:27:45 +01001789enum KeyCollectionType { OWN_ONLY, INCLUDE_PROTOS };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001790
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001791// JSReceiver includes types on which properties can be defined, i.e.,
1792// JSObject and JSProxy.
1793class JSReceiver: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00001794 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001795 // [properties]: Backing storage for properties.
1796 // properties is a FixedArray in the fast case and a Dictionary in the
1797 // slow case.
1798 DECL_ACCESSORS(properties, FixedArray) // Get and set fast properties.
1799 inline void initialize_properties();
1800 inline bool HasFastProperties();
1801 // Gets slow properties for non-global objects.
1802 inline NameDictionary* property_dictionary();
1803
1804 // Deletes an existing named property in a normalized object.
1805 static void DeleteNormalizedProperty(Handle<JSReceiver> object,
1806 Handle<Name> name, int entry);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001807
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001808 DECLARE_CAST(JSReceiver)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001809
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001810 // ES6 section 7.1.1 ToPrimitive
1811 MUST_USE_RESULT static MaybeHandle<Object> ToPrimitive(
1812 Handle<JSReceiver> receiver,
1813 ToPrimitiveHint hint = ToPrimitiveHint::kDefault);
1814 MUST_USE_RESULT static MaybeHandle<Object> OrdinaryToPrimitive(
1815 Handle<JSReceiver> receiver, OrdinaryToPrimitiveHint hint);
1816
1817 static MaybeHandle<Context> GetFunctionRealm(Handle<JSReceiver> receiver);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001818
Ben Murdoch097c5b22016-05-18 11:27:45 +01001819 // Get the first non-hidden prototype.
1820 static inline MaybeHandle<Object> GetPrototype(Isolate* isolate,
1821 Handle<JSReceiver> receiver);
1822
1823 MUST_USE_RESULT static Maybe<bool> HasInPrototypeChain(
1824 Isolate* isolate, Handle<JSReceiver> object, Handle<Object> proto);
1825
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001826 // Implementation of [[HasProperty]], ECMA-262 5th edition, section 8.12.6.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001827 MUST_USE_RESULT static Maybe<bool> HasProperty(LookupIterator* it);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001828 MUST_USE_RESULT static inline Maybe<bool> HasProperty(
1829 Handle<JSReceiver> object, Handle<Name> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001830 MUST_USE_RESULT static inline Maybe<bool> HasElement(
1831 Handle<JSReceiver> object, uint32_t index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001832
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001833 MUST_USE_RESULT static inline Maybe<bool> HasOwnProperty(
1834 Handle<JSReceiver> object, Handle<Name> name);
1835
Ben Murdochda12d292016-06-02 14:46:10 +01001836 MUST_USE_RESULT static inline MaybeHandle<Object> GetProperty(
1837 Isolate* isolate, Handle<JSReceiver> receiver, const char* key);
1838 MUST_USE_RESULT static inline MaybeHandle<Object> GetProperty(
1839 Handle<JSReceiver> receiver, Handle<Name> name);
1840 MUST_USE_RESULT static inline MaybeHandle<Object> GetElement(
1841 Isolate* isolate, Handle<JSReceiver> receiver, uint32_t index);
1842
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001843 // Implementation of ES6 [[Delete]]
1844 MUST_USE_RESULT static Maybe<bool> DeletePropertyOrElement(
1845 Handle<JSReceiver> object, Handle<Name> name,
1846 LanguageMode language_mode = SLOPPY);
1847 MUST_USE_RESULT static Maybe<bool> DeleteProperty(
1848 Handle<JSReceiver> object, Handle<Name> name,
1849 LanguageMode language_mode = SLOPPY);
1850 MUST_USE_RESULT static Maybe<bool> DeleteProperty(LookupIterator* it,
1851 LanguageMode language_mode);
1852 MUST_USE_RESULT static Maybe<bool> DeleteElement(
1853 Handle<JSReceiver> object, uint32_t index,
1854 LanguageMode language_mode = SLOPPY);
1855
1856 MUST_USE_RESULT static Object* DefineProperty(Isolate* isolate,
1857 Handle<Object> object,
1858 Handle<Object> name,
1859 Handle<Object> attributes);
1860 MUST_USE_RESULT static MaybeHandle<Object> DefineProperties(
1861 Isolate* isolate, Handle<Object> object, Handle<Object> properties);
1862
1863 // "virtual" dispatcher to the correct [[DefineOwnProperty]] implementation.
1864 MUST_USE_RESULT static Maybe<bool> DefineOwnProperty(
1865 Isolate* isolate, Handle<JSReceiver> object, Handle<Object> key,
1866 PropertyDescriptor* desc, ShouldThrow should_throw);
1867
1868 // ES6 7.3.4 (when passed DONT_THROW)
1869 MUST_USE_RESULT static Maybe<bool> CreateDataProperty(
1870 LookupIterator* it, Handle<Object> value, ShouldThrow should_throw);
1871
1872 // ES6 9.1.6.1
1873 MUST_USE_RESULT static Maybe<bool> OrdinaryDefineOwnProperty(
1874 Isolate* isolate, Handle<JSObject> object, Handle<Object> key,
1875 PropertyDescriptor* desc, ShouldThrow should_throw);
1876 MUST_USE_RESULT static Maybe<bool> OrdinaryDefineOwnProperty(
1877 LookupIterator* it, PropertyDescriptor* desc, ShouldThrow should_throw);
1878 // ES6 9.1.6.2
1879 MUST_USE_RESULT static Maybe<bool> IsCompatiblePropertyDescriptor(
1880 Isolate* isolate, bool extensible, PropertyDescriptor* desc,
1881 PropertyDescriptor* current, Handle<Name> property_name,
1882 ShouldThrow should_throw);
1883 // ES6 9.1.6.3
1884 // |it| can be NULL in cases where the ES spec passes |undefined| as the
1885 // receiver. Exactly one of |it| and |property_name| must be provided.
1886 MUST_USE_RESULT static Maybe<bool> ValidateAndApplyPropertyDescriptor(
1887 Isolate* isolate, LookupIterator* it, bool extensible,
1888 PropertyDescriptor* desc, PropertyDescriptor* current,
1889 ShouldThrow should_throw, Handle<Name> property_name = Handle<Name>());
1890
1891 MUST_USE_RESULT static Maybe<bool> GetOwnPropertyDescriptor(
1892 Isolate* isolate, Handle<JSReceiver> object, Handle<Object> key,
1893 PropertyDescriptor* desc);
1894 MUST_USE_RESULT static Maybe<bool> GetOwnPropertyDescriptor(
1895 LookupIterator* it, PropertyDescriptor* desc);
1896
1897 typedef PropertyAttributes IntegrityLevel;
1898
1899 // ES6 7.3.14 (when passed DONT_THROW)
1900 // 'level' must be SEALED or FROZEN.
1901 MUST_USE_RESULT static Maybe<bool> SetIntegrityLevel(
1902 Handle<JSReceiver> object, IntegrityLevel lvl, ShouldThrow should_throw);
1903
1904 // ES6 7.3.15
1905 // 'level' must be SEALED or FROZEN.
1906 MUST_USE_RESULT static Maybe<bool> TestIntegrityLevel(
1907 Handle<JSReceiver> object, IntegrityLevel lvl);
1908
1909 // ES6 [[PreventExtensions]] (when passed DONT_THROW)
1910 MUST_USE_RESULT static Maybe<bool> PreventExtensions(
1911 Handle<JSReceiver> object, ShouldThrow should_throw);
1912
1913 MUST_USE_RESULT static Maybe<bool> IsExtensible(Handle<JSReceiver> object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001914
1915 // Tests for the fast common case for property enumeration.
1916 bool IsSimpleEnum();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001917
1918 // Returns the class name ([[Class]] property in the specification).
1919 String* class_name();
1920
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001921 // Returns the builtin string tag used in Object.prototype.toString.
1922 MUST_USE_RESULT static MaybeHandle<String> BuiltinStringTag(
1923 Handle<JSReceiver> object);
1924
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001925 // Returns the constructor name (the name (possibly, inferred name) of the
1926 // function that was used to instantiate the object).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001927 static Handle<String> GetConstructorName(Handle<JSReceiver> receiver);
1928
1929 Context* GetCreationContext();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001930
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001931 MUST_USE_RESULT static inline Maybe<PropertyAttributes> GetPropertyAttributes(
1932 Handle<JSReceiver> object, Handle<Name> name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001933 MUST_USE_RESULT static inline Maybe<PropertyAttributes>
1934 GetOwnPropertyAttributes(Handle<JSReceiver> object, Handle<Name> name);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001935
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001936 MUST_USE_RESULT static inline Maybe<PropertyAttributes> GetElementAttributes(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001937 Handle<JSReceiver> object, uint32_t index);
1938 MUST_USE_RESULT static inline Maybe<PropertyAttributes>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001939 GetOwnElementAttributes(Handle<JSReceiver> object, uint32_t index);
1940
1941 MUST_USE_RESULT static Maybe<PropertyAttributes> GetPropertyAttributes(
1942 LookupIterator* it);
1943
1944 // Set the object's prototype (only JSReceiver and null are allowed values).
1945 MUST_USE_RESULT static Maybe<bool> SetPrototype(Handle<JSReceiver> object,
1946 Handle<Object> value,
1947 bool from_javascript,
1948 ShouldThrow should_throw);
1949
Ben Murdochda12d292016-06-02 14:46:10 +01001950 inline static Handle<Object> GetDataProperty(Handle<JSReceiver> object,
1951 Handle<Name> name);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001952 static Handle<Object> GetDataProperty(LookupIterator* it);
1953
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001954
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001955 // Retrieves a permanent object identity hash code. The undefined value might
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001956 // be returned in case no hash was created yet.
Ben Murdochda12d292016-06-02 14:46:10 +01001957 static inline Handle<Object> GetIdentityHash(Isolate* isolate,
1958 Handle<JSReceiver> object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001959
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001960 // Retrieves a permanent object identity hash code. May create and store a
1961 // hash code if needed and none exists.
1962 inline static Handle<Smi> GetOrCreateIdentityHash(
1963 Handle<JSReceiver> object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001964
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001965 // ES6 [[OwnPropertyKeys]] (modulo return type)
1966 MUST_USE_RESULT static MaybeHandle<FixedArray> OwnPropertyKeys(
1967 Handle<JSReceiver> object) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001968 return GetKeys(object, OWN_ONLY, ALL_PROPERTIES, CONVERT_TO_STRING);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001969 }
1970
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001971 // Computes the enumerable keys for a JSObject. Used for implementing
1972 // "for (n in object) { }".
1973 MUST_USE_RESULT static MaybeHandle<FixedArray> GetKeys(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001974 Handle<JSReceiver> object, KeyCollectionType type, PropertyFilter filter,
Ben Murdochda12d292016-06-02 14:46:10 +01001975 GetKeysConversion keys_conversion = KEEP_NUMBERS,
1976 bool filter_proxy_keys_ = true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001977
Ben Murdoch097c5b22016-05-18 11:27:45 +01001978 MUST_USE_RESULT static MaybeHandle<FixedArray> GetOwnValues(
1979 Handle<JSReceiver> object, PropertyFilter filter);
1980
1981 MUST_USE_RESULT static MaybeHandle<FixedArray> GetOwnEntries(
1982 Handle<JSReceiver> object, PropertyFilter filter);
1983
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001984 // Layout description.
1985 static const int kPropertiesOffset = HeapObject::kHeaderSize;
1986 static const int kHeaderSize = HeapObject::kHeaderSize + kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001987
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001988 private:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001989 DISALLOW_IMPLICIT_CONSTRUCTORS(JSReceiver);
1990};
1991
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001992
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001993// The JSObject describes real heap allocated JavaScript objects with
1994// properties.
1995// Note that the map of JSObject changes during execution to enable inline
1996// caching.
1997class JSObject: public JSReceiver {
1998 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001999 static MUST_USE_RESULT MaybeHandle<JSObject> New(
2000 Handle<JSFunction> constructor, Handle<JSReceiver> new_target,
2001 Handle<AllocationSite> site = Handle<AllocationSite>::null());
2002
2003 // Gets global object properties.
2004 inline GlobalDictionary* global_dictionary();
2005
2006 static MaybeHandle<Context> GetFunctionRealm(Handle<JSObject> object);
Steve Blocka7e24c12009-10-30 11:49:00 +00002007
2008 // [elements]: The elements (properties with names that are integers).
Iain Merrick75681382010-08-19 15:07:18 +01002009 //
2010 // Elements can be in two general modes: fast and slow. Each mode
2011 // corrensponds to a set of object representations of elements that
2012 // have something in common.
2013 //
2014 // In the fast mode elements is a FixedArray and so each element can
2015 // be quickly accessed. This fact is used in the generated code. The
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002016 // elements array can have one of three maps in this mode:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002017 // fixed_array_map, sloppy_arguments_elements_map or
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002018 // fixed_cow_array_map (for copy-on-write arrays). In the latter case
2019 // the elements array may be shared by a few objects and so before
2020 // writing to any element the array must be copied. Use
2021 // EnsureWritableFastElements in this case.
Iain Merrick75681382010-08-19 15:07:18 +01002022 //
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002023 // In the slow mode the elements is either a NumberDictionary, a
2024 // FixedArray parameter map for a (sloppy) arguments object.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002025 DECL_ACCESSORS(elements, FixedArrayBase)
Steve Blocka7e24c12009-10-30 11:49:00 +00002026 inline void initialize_elements();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002027 static void ResetElements(Handle<JSObject> object);
2028 static inline void SetMapAndElements(Handle<JSObject> object,
2029 Handle<Map> map,
2030 Handle<FixedArrayBase> elements);
Steve Blocka7e24c12009-10-30 11:49:00 +00002031 inline ElementsKind GetElementsKind();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002032 ElementsAccessor* GetElementsAccessor();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002033 // Returns true if an object has elements of FAST_SMI_ELEMENTS ElementsKind.
2034 inline bool HasFastSmiElements();
2035 // Returns true if an object has elements of FAST_ELEMENTS ElementsKind.
2036 inline bool HasFastObjectElements();
2037 // Returns true if an object has elements of FAST_ELEMENTS or
2038 // FAST_SMI_ONLY_ELEMENTS.
2039 inline bool HasFastSmiOrObjectElements();
2040 // Returns true if an object has any of the fast elements kinds.
Steve Blocka7e24c12009-10-30 11:49:00 +00002041 inline bool HasFastElements();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002042 // Returns true if an object has elements of FAST_DOUBLE_ELEMENTS
2043 // ElementsKind.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002044 inline bool HasFastDoubleElements();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002045 // Returns true if an object has elements of FAST_HOLEY_*_ELEMENTS
2046 // ElementsKind.
2047 inline bool HasFastHoleyElements();
2048 inline bool HasSloppyArgumentsElements();
Ben Murdoch097c5b22016-05-18 11:27:45 +01002049 inline bool HasStringWrapperElements();
Steve Blocka7e24c12009-10-30 11:49:00 +00002050 inline bool HasDictionaryElements();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002051
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002052 inline bool HasFixedTypedArrayElements();
2053
2054 inline bool HasFixedUint8ClampedElements();
2055 inline bool HasFixedArrayElements();
2056 inline bool HasFixedInt8Elements();
2057 inline bool HasFixedUint8Elements();
2058 inline bool HasFixedInt16Elements();
2059 inline bool HasFixedUint16Elements();
2060 inline bool HasFixedInt32Elements();
2061 inline bool HasFixedUint32Elements();
2062 inline bool HasFixedFloat32Elements();
2063 inline bool HasFixedFloat64Elements();
2064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002065 inline bool HasFastArgumentsElements();
2066 inline bool HasSlowArgumentsElements();
Ben Murdoch097c5b22016-05-18 11:27:45 +01002067 inline bool HasFastStringWrapperElements();
2068 inline bool HasSlowStringWrapperElements();
Ben Murdochda12d292016-06-02 14:46:10 +01002069 bool HasEnumerableElements();
2070
Ben Murdochc7cc0282012-03-05 14:35:55 +00002071 inline SeededNumberDictionary* element_dictionary(); // Gets slow elements.
2072
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002073 // Requires: HasFastElements().
Ben Murdochda12d292016-06-02 14:46:10 +01002074 static void EnsureWritableFastElements(Handle<JSObject> object);
Steve Blocka7e24c12009-10-30 11:49:00 +00002075
2076 // Collects elements starting at index 0.
2077 // Undefined values are placed after non-undefined values.
2078 // Returns the number of non-undefined values.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002079 static Handle<Object> PrepareElementsForSort(Handle<JSObject> object,
2080 uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00002081 // As PrepareElementsForSort, but only on objects where elements is
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002082 // a dictionary, and it will stay a dictionary. Collates undefined and
2083 // unexisting elements below limit from position zero of the elements.
2084 static Handle<Object> PrepareSlowElementsForSort(Handle<JSObject> object,
2085 uint32_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +00002086
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002087 MUST_USE_RESULT static Maybe<bool> SetPropertyWithInterceptor(
Ben Murdoch097c5b22016-05-18 11:27:45 +01002088 LookupIterator* it, ShouldThrow should_throw, Handle<Object> value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002089
Ben Murdoch097c5b22016-05-18 11:27:45 +01002090 // The API currently still wants DefineOwnPropertyIgnoreAttributes to convert
2091 // AccessorInfo objects to data fields. We allow FORCE_FIELD as an exception
2092 // to the default behavior that calls the setter.
2093 enum AccessorInfoHandling { FORCE_FIELD, DONT_FORCE_FIELD };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002094
2095 MUST_USE_RESULT static MaybeHandle<Object> DefineOwnPropertyIgnoreAttributes(
2096 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002097 AccessorInfoHandling handling = DONT_FORCE_FIELD);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002098
2099 MUST_USE_RESULT static Maybe<bool> DefineOwnPropertyIgnoreAttributes(
2100 LookupIterator* it, Handle<Object> value, PropertyAttributes attributes,
2101 ShouldThrow should_throw,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002102 AccessorInfoHandling handling = DONT_FORCE_FIELD);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002103
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002104 MUST_USE_RESULT static MaybeHandle<Object> SetOwnPropertyIgnoreAttributes(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002105 Handle<JSObject> object, Handle<Name> name, Handle<Object> value,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002106 PropertyAttributes attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002107
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002108 MUST_USE_RESULT static MaybeHandle<Object> SetOwnElementIgnoreAttributes(
2109 Handle<JSObject> object, uint32_t index, Handle<Object> value,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002110 PropertyAttributes attributes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002111
2112 // Equivalent to one of the above depending on whether |name| can be converted
2113 // to an array index.
2114 MUST_USE_RESULT static MaybeHandle<Object>
Ben Murdoch097c5b22016-05-18 11:27:45 +01002115 DefinePropertyOrElementIgnoreAttributes(Handle<JSObject> object,
2116 Handle<Name> name,
2117 Handle<Object> value,
2118 PropertyAttributes attributes = NONE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002119
2120 // Adds or reconfigures a property to attributes NONE. It will fail when it
2121 // cannot.
Ben Murdochda12d292016-06-02 14:46:10 +01002122 MUST_USE_RESULT static Maybe<bool> CreateDataProperty(
2123 LookupIterator* it, Handle<Object> value,
2124 ShouldThrow should_throw = DONT_THROW);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002125
2126 static void AddProperty(Handle<JSObject> object, Handle<Name> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002127 Handle<Object> value, PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00002128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002129 MUST_USE_RESULT static Maybe<bool> AddDataElement(
2130 Handle<JSObject> receiver, uint32_t index, Handle<Object> value,
2131 PropertyAttributes attributes, ShouldThrow should_throw);
2132 MUST_USE_RESULT static MaybeHandle<Object> AddDataElement(
2133 Handle<JSObject> receiver, uint32_t index, Handle<Object> value,
2134 PropertyAttributes attributes);
2135
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002136 // Extend the receiver with a single fast property appeared first in the
2137 // passed map. This also extends the property backing store if necessary.
2138 static void AllocateStorageForMap(Handle<JSObject> object, Handle<Map> map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002139
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002140 // Migrates the given object to a map whose field representations are the
2141 // lowest upper bound of all known representations for that field.
2142 static void MigrateInstance(Handle<JSObject> instance);
2143
2144 // Migrates the given object only if the target map is already available,
2145 // or returns false if such a map is not yet available.
2146 static bool TryMigrateInstance(Handle<JSObject> instance);
Steve Blocka7e24c12009-10-30 11:49:00 +00002147
2148 // Sets the property value in a normalized object given (key, value, details).
2149 // Handles the special representation of JS global objects.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002150 static void SetNormalizedProperty(Handle<JSObject> object, Handle<Name> name,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002151 Handle<Object> value,
2152 PropertyDetails details);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002153 static void SetDictionaryElement(Handle<JSObject> object, uint32_t index,
2154 Handle<Object> value,
2155 PropertyAttributes attributes);
2156 static void SetDictionaryArgumentsElement(Handle<JSObject> object,
2157 uint32_t index,
2158 Handle<Object> value,
2159 PropertyAttributes attributes);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002160
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002161 static void OptimizeAsPrototype(Handle<JSObject> object,
2162 PrototypeOptimizationMode mode);
2163 static void ReoptimizeIfPrototype(Handle<JSObject> object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002164 static void LazyRegisterPrototypeUser(Handle<Map> user, Isolate* isolate);
2165 static void UpdatePrototypeUserRegistration(Handle<Map> old_map,
2166 Handle<Map> new_map,
2167 Isolate* isolate);
2168 static bool UnregisterPrototypeUser(Handle<Map> user, Isolate* isolate);
2169 static void InvalidatePrototypeChains(Map* map);
2170
2171 // Alternative implementation of WeakFixedArray::NullCallback.
2172 class PrototypeRegistryCompactionCallback {
2173 public:
2174 static void Callback(Object* value, int old_index, int new_index);
2175 };
Steve Blocka7e24c12009-10-30 11:49:00 +00002176
Steve Blocka7e24c12009-10-30 11:49:00 +00002177 // Retrieve interceptors.
Ben Murdochda12d292016-06-02 14:46:10 +01002178 inline InterceptorInfo* GetNamedInterceptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002179 inline InterceptorInfo* GetIndexedInterceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00002180
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002181 // Used from JSReceiver.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002182 MUST_USE_RESULT static Maybe<PropertyAttributes>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002183 GetPropertyAttributesWithInterceptor(LookupIterator* it);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002184 MUST_USE_RESULT static Maybe<PropertyAttributes>
2185 GetPropertyAttributesWithFailedAccessCheck(LookupIterator* it);
Steve Blocka7e24c12009-10-30 11:49:00 +00002186
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002187 // Retrieves an AccessorPair property from the given object. Might return
2188 // undefined if the property doesn't exist or is of a different kind.
2189 MUST_USE_RESULT static MaybeHandle<Object> GetAccessor(
2190 Handle<JSObject> object,
2191 Handle<Name> name,
2192 AccessorComponent component);
Steve Blocka7e24c12009-10-30 11:49:00 +00002193
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002194 // Defines an AccessorPair property on the given object.
2195 // TODO(mstarzinger): Rename to SetAccessor().
2196 static MaybeHandle<Object> DefineAccessor(Handle<JSObject> object,
2197 Handle<Name> name,
2198 Handle<Object> getter,
2199 Handle<Object> setter,
2200 PropertyAttributes attributes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002201 static MaybeHandle<Object> DefineAccessor(LookupIterator* it,
2202 Handle<Object> getter,
2203 Handle<Object> setter,
2204 PropertyAttributes attributes);
Leon Clarkef7060e22010-06-03 12:02:55 +01002205
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002206 // Defines an AccessorInfo property on the given object.
2207 MUST_USE_RESULT static MaybeHandle<Object> SetAccessor(
2208 Handle<JSObject> object,
2209 Handle<AccessorInfo> info);
2210
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002211 // The result must be checked first for exceptions. If there's no exception,
2212 // the output parameter |done| indicates whether the interceptor has a result
2213 // or not.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002214 MUST_USE_RESULT static MaybeHandle<Object> GetPropertyWithInterceptor(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002215 LookupIterator* it, bool* done);
Steve Blocka7e24c12009-10-30 11:49:00 +00002216
Steve Blockd0582a62009-12-15 09:54:21 +00002217 // Accessors for hidden properties object.
2218 //
Ben Murdoch097c5b22016-05-18 11:27:45 +01002219 // Hidden properties are not own properties of the object itself. Instead
2220 // they are stored in an auxiliary structure kept as an own property with a
2221 // special name Heap::hidden_properties_symbol(). But if the receiver is a
2222 // JSGlobalProxy then the auxiliary object is a property of its prototype, and
2223 // if it's a detached proxy, then you can't have hidden properties.
Steve Blockd0582a62009-12-15 09:54:21 +00002224
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002225 // Sets a hidden property on this object. Returns this object if successful,
2226 // undefined if called on a detached proxy.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002227 static Handle<Object> SetHiddenProperty(Handle<JSObject> object,
2228 Handle<Name> key,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002229 Handle<Object> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002230 // Gets the value of a hidden property with the given key. Returns the hole
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002231 // if the property doesn't exist (or if called on a detached proxy),
2232 // otherwise returns the value set for the key.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002233 Object* GetHiddenProperty(Handle<Name> key);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002234 // Deletes a hidden property. Deleting a non-existing property is
2235 // considered successful.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002236 static void DeleteHiddenProperty(Handle<JSObject> object,
2237 Handle<Name> key);
2238 // Returns true if the object has a property with the hidden string as name.
2239 static bool HasHiddenProperties(Handle<JSObject> object);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002241 static void ValidateElements(Handle<JSObject> object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002242
2243 // Makes sure that this object can contain HeapObject as elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002244 static inline void EnsureCanContainHeapObjectElements(Handle<JSObject> obj);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002245
2246 // Makes sure that this object can contain the specified elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002247 static inline void EnsureCanContainElements(
2248 Handle<JSObject> object,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002249 Object** elements,
2250 uint32_t count,
2251 EnsureElementsMode mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002252 static inline void EnsureCanContainElements(
2253 Handle<JSObject> object,
2254 Handle<FixedArrayBase> elements,
2255 uint32_t length,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002256 EnsureElementsMode mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002257 static void EnsureCanContainElements(
2258 Handle<JSObject> object,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002259 Arguments* arguments,
2260 uint32_t first_arg,
2261 uint32_t arg_count,
2262 EnsureElementsMode mode);
Steve Blocka7e24c12009-10-30 11:49:00 +00002263
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002264 // Would we convert a fast elements array to dictionary mode given
2265 // an access at key?
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002266 bool WouldConvertToSlowElements(uint32_t index);
Andrei Popescu402d9372010-02-26 13:31:12 +00002267
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002268 // Computes the new capacity when expanding the elements of a JSObject.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002269 static uint32_t NewElementsCapacity(uint32_t old_capacity) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002270 // (old_capacity + 50%) + 16
2271 return old_capacity + (old_capacity >> 1) + 16;
2272 }
2273
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002274 // These methods do not perform access checks!
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002275 static void UpdateAllocationSite(Handle<JSObject> object,
2276 ElementsKind to_kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00002277
2278 // Lookup interceptors are used for handling properties controlled by host
2279 // objects.
2280 inline bool HasNamedInterceptor();
2281 inline bool HasIndexedInterceptor();
2282
2283 // Support functions for v8 api (needed for correct interceptor behavior).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002284 MUST_USE_RESULT static Maybe<bool> HasRealNamedProperty(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002285 Handle<JSObject> object, Handle<Name> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002286 MUST_USE_RESULT static Maybe<bool> HasRealElementProperty(
2287 Handle<JSObject> object, uint32_t index);
2288 MUST_USE_RESULT static Maybe<bool> HasRealNamedCallbackProperty(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002289 Handle<JSObject> object, Handle<Name> name);
Steve Blocka7e24c12009-10-30 11:49:00 +00002290
Steve Blocka7e24c12009-10-30 11:49:00 +00002291 // Get the header size for a JSObject. Used to compute the index of
2292 // internal fields as well as the number of internal fields.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002293 static inline int GetHeaderSize(InstanceType instance_type);
Steve Blocka7e24c12009-10-30 11:49:00 +00002294 inline int GetHeaderSize();
2295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002296 static inline int GetInternalFieldCount(Map* map);
Steve Blocka7e24c12009-10-30 11:49:00 +00002297 inline int GetInternalFieldCount();
Steve Block44f0eee2011-05-26 01:26:41 +01002298 inline int GetInternalFieldOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002299 inline Object* GetInternalField(int index);
2300 inline void SetInternalField(int index, Object* value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002301 inline void SetInternalField(int index, Smi* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002302
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002303 void CollectOwnPropertyNames(KeyAccumulator* keys,
2304 PropertyFilter filter = ALL_PROPERTIES);
Steve Blocka7e24c12009-10-30 11:49:00 +00002305
2306 // Returns the number of properties on this object filtering out properties
2307 // with the specified attributes (ignoring interceptors).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002308 // TODO(jkummerow): Deprecated, only used by Object.observe.
2309 int NumberOfOwnElements(PropertyFilter filter);
Steve Blocka7e24c12009-10-30 11:49:00 +00002310 // Returns the number of elements on this object filtering out elements
2311 // with the specified attributes (ignoring interceptors).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002312 // TODO(jkummerow): Deprecated, only used by Object.observe.
2313 int GetOwnElementKeys(FixedArray* storage, PropertyFilter filter);
2314
2315 static void CollectOwnElementKeys(Handle<JSObject> object,
2316 KeyAccumulator* keys,
2317 PropertyFilter filter);
2318
Ben Murdoch097c5b22016-05-18 11:27:45 +01002319 static Handle<FixedArray> GetEnumPropertyKeys(Handle<JSObject> object);
Steve Blocka7e24c12009-10-30 11:49:00 +00002320
Ben Murdochda12d292016-06-02 14:46:10 +01002321 static Handle<FixedArray> GetFastEnumPropertyKeys(Isolate* isolate,
2322 Handle<JSObject> object);
2323
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002324 // Returns a new map with all transitions dropped from the object's current
2325 // map and the ElementsKind set.
2326 static Handle<Map> GetElementsTransitionMap(Handle<JSObject> object,
2327 ElementsKind to_kind);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002328 static void TransitionElementsKind(Handle<JSObject> object,
2329 ElementsKind to_kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002330
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002331 // Always use this to migrate an object to a new map.
2332 // |expected_additional_properties| is only used for fast-to-slow transitions
2333 // and ignored otherwise.
2334 static void MigrateToMap(Handle<JSObject> object, Handle<Map> new_map,
2335 int expected_additional_properties = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00002336
2337 // Convert the object to use the canonical dictionary
2338 // representation. If the object is expected to have additional properties
2339 // added this number can be indicated to have the backing store allocated to
2340 // an initial capacity for holding these properties.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002341 static void NormalizeProperties(Handle<JSObject> object,
2342 PropertyNormalizationMode mode,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002343 int expected_additional_properties,
2344 const char* reason);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002345
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002346 // Convert and update the elements backing store to be a
2347 // SeededNumberDictionary dictionary. Returns the backing after conversion.
2348 static Handle<SeededNumberDictionary> NormalizeElements(
2349 Handle<JSObject> object);
2350
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002351 void RequireSlowElements(SeededNumberDictionary* dictionary);
2352
Steve Blocka7e24c12009-10-30 11:49:00 +00002353 // Transform slow named properties to fast variants.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002354 static void MigrateSlowToFast(Handle<JSObject> object,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002355 int unused_property_fields, const char* reason);
2356
2357 inline bool IsUnboxedDoubleField(FieldIndex index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002358
2359 // Access fast-case object properties at index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002360 static Handle<Object> FastPropertyAt(Handle<JSObject> object,
2361 Representation representation,
2362 FieldIndex index);
2363 inline Object* RawFastPropertyAt(FieldIndex index);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002364 inline double RawFastDoublePropertyAt(FieldIndex index);
2365
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002366 inline void FastPropertyAtPut(FieldIndex index, Object* value);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002367 inline void RawFastPropertyAtPut(FieldIndex index, Object* value);
2368 inline void RawFastDoublePropertyAtPut(FieldIndex index, double value);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002369 inline void WriteToField(int descriptor, PropertyDetails details,
2370 Object* value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002371 inline void WriteToField(int descriptor, Object* value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002372
2373 // Access to in object properties.
Steve Block44f0eee2011-05-26 01:26:41 +01002374 inline int GetInObjectPropertyOffset(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002375 inline Object* InObjectPropertyAt(int index);
2376 inline Object* InObjectPropertyAtPut(int index,
2377 Object* value,
2378 WriteBarrierMode mode
2379 = UPDATE_WRITE_BARRIER);
2380
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002381 // Set the object's prototype (only JSReceiver and null are allowed values).
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002382 MUST_USE_RESULT static Maybe<bool> SetPrototype(Handle<JSObject> object,
2383 Handle<Object> value,
2384 bool from_javascript,
2385 ShouldThrow should_throw);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002386
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002387 // Initializes the body starting at |start_offset|. It is responsibility of
2388 // the caller to initialize object header. Fill the pre-allocated fields with
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002389 // pre_allocated_value and the rest with filler_value.
2390 // Note: this call does not update write barrier, the caller is responsible
2391 // to ensure that |filler_value| can be collected without WB here.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002392 inline void InitializeBody(Map* map, int start_offset,
2393 Object* pre_allocated_value, Object* filler_value);
Steve Blocka7e24c12009-10-30 11:49:00 +00002394
2395 // Check whether this object references another object
2396 bool ReferencesObject(Object* obj);
2397
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002398 MUST_USE_RESULT static Maybe<bool> PreventExtensions(
2399 Handle<JSObject> object, ShouldThrow should_throw);
Steve Block8defd9f2010-07-08 12:39:36 +01002400
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002401 static bool IsExtensible(Handle<JSObject> object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002402
2403 // Called the first time an object is observed with ES7 Object.observe.
2404 static void SetObserved(Handle<JSObject> object);
2405
2406 // Copy object.
2407 enum DeepCopyHints { kNoHints = 0, kObjectIsShallow = 1 };
2408
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002409 MUST_USE_RESULT static MaybeHandle<JSObject> DeepCopy(
2410 Handle<JSObject> object,
2411 AllocationSiteUsageContext* site_context,
2412 DeepCopyHints hints = kNoHints);
2413 MUST_USE_RESULT static MaybeHandle<JSObject> DeepWalk(
2414 Handle<JSObject> object,
2415 AllocationSiteCreationContext* site_context);
2416
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002417 DECLARE_CAST(JSObject)
Steve Block8defd9f2010-07-08 12:39:36 +01002418
Steve Blocka7e24c12009-10-30 11:49:00 +00002419 // Dispatched behavior.
Steve Blocka7e24c12009-10-30 11:49:00 +00002420 void JSObjectShortPrint(StringStream* accumulator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002421 DECLARE_PRINTER(JSObject)
2422 DECLARE_VERIFIER(JSObject)
Ben Murdochb0fe1622011-05-05 13:52:32 +01002423#ifdef OBJECT_PRINT
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002424 void PrintProperties(std::ostream& os); // NOLINT
2425 void PrintElements(std::ostream& os); // NOLINT
2426#endif
2427#if defined(DEBUG) || defined(OBJECT_PRINT)
2428 void PrintTransitions(std::ostream& os); // NOLINT
Ben Murdochb0fe1622011-05-05 13:52:32 +01002429#endif
2430
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002431 static void PrintElementsTransition(
2432 FILE* file, Handle<JSObject> object,
2433 ElementsKind from_kind, Handle<FixedArrayBase> from_elements,
2434 ElementsKind to_kind, Handle<FixedArrayBase> to_elements);
2435
2436 void PrintInstanceMigration(FILE* file, Map* original_map, Map* new_map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002437
Ben Murdochb0fe1622011-05-05 13:52:32 +01002438#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002439 // Structure for collecting spill information about JSObjects.
2440 class SpillInformation {
2441 public:
2442 void Clear();
2443 void Print();
2444 int number_of_objects_;
2445 int number_of_objects_with_fast_properties_;
2446 int number_of_objects_with_fast_elements_;
2447 int number_of_fast_used_fields_;
2448 int number_of_fast_unused_fields_;
2449 int number_of_slow_used_properties_;
2450 int number_of_slow_unused_properties_;
2451 int number_of_fast_used_elements_;
2452 int number_of_fast_unused_elements_;
2453 int number_of_slow_used_elements_;
2454 int number_of_slow_unused_elements_;
2455 };
2456
2457 void IncrementSpillStatistics(SpillInformation* info);
2458#endif
Steve Blocka7e24c12009-10-30 11:49:00 +00002459
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002460#ifdef VERIFY_HEAP
2461 // If a GC was caused while constructing this object, the elements pointer
2462 // may point to a one pointer filler map. The object won't be rooted, but
2463 // our heap verification code could stumble across it.
2464 bool ElementsAreSafeToExamine();
2465#endif
2466
2467 Object* SlowReverseLookup(Object* value);
Steve Block8defd9f2010-07-08 12:39:36 +01002468
Leon Clarkee46be812010-01-19 14:06:41 +00002469 // Maximal number of elements (numbered 0 .. kMaxElementCount - 1).
2470 // Also maximal value of JSArray's length property.
2471 static const uint32_t kMaxElementCount = 0xffffffffu;
2472
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002473 // Constants for heuristics controlling conversion of fast elements
2474 // to slow elements.
2475
2476 // Maximal gap that can be introduced by adding an element beyond
2477 // the current elements length.
Steve Blocka7e24c12009-10-30 11:49:00 +00002478 static const uint32_t kMaxGap = 1024;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002479
2480 // Maximal length of fast elements array that won't be checked for
2481 // being dense enough on expansion.
2482 static const int kMaxUncheckedFastElementsLength = 5000;
2483
2484 // Same as above but for old arrays. This limit is more strict. We
2485 // don't want to be wasteful with long lived objects.
2486 static const int kMaxUncheckedOldFastElementsLength = 500;
2487
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002488 // This constant applies only to the initial map of "global.Object" and
2489 // not to arbitrary other JSObject maps.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002490 static const int kInitialGlobalObjectUnusedPropertiesCount = 4;
2491
Steve Blocka7e24c12009-10-30 11:49:00 +00002492 static const int kMaxInstanceSize = 255 * kPointerSize;
2493 // When extending the backing storage for property values, we increase
2494 // its size by more than the 1 entry necessary, so sequentially adding fields
2495 // to the same object requires fewer allocations and copies.
2496 static const int kFieldsAdded = 3;
2497
2498 // Layout description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002499 static const int kElementsOffset = JSReceiver::kHeaderSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002500 static const int kHeaderSize = kElementsOffset + kPointerSize;
2501
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002502 STATIC_ASSERT(kHeaderSize == Internals::kJSObjectHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00002503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002504 typedef FlexibleBodyDescriptor<JSReceiver::kPropertiesOffset> BodyDescriptor;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002505
2506 // Enqueue change record for Object.observe. May cause GC.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002507 MUST_USE_RESULT static MaybeHandle<Object> EnqueueChangeRecord(
2508 Handle<JSObject> object, const char* type, Handle<Name> name,
2509 Handle<Object> old_value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002510
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002511 // Gets the number of currently used elements.
2512 int GetFastElementsUsage();
2513
2514 static bool AllCanRead(LookupIterator* it);
2515 static bool AllCanWrite(LookupIterator* it);
2516
Steve Blocka7e24c12009-10-30 11:49:00 +00002517 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002518 friend class JSReceiver;
2519 friend class Object;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002521 // Used from Object::GetProperty().
2522 MUST_USE_RESULT static MaybeHandle<Object> GetPropertyWithFailedAccessCheck(
2523 LookupIterator* it);
2524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002525 MUST_USE_RESULT static Maybe<bool> SetPropertyWithFailedAccessCheck(
2526 LookupIterator* it, Handle<Object> value, ShouldThrow should_throw);
Steve Blocka7e24c12009-10-30 11:49:00 +00002527
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002528 // Add a property to a slow-case object.
2529 static void AddSlowProperty(Handle<JSObject> object,
2530 Handle<Name> name,
2531 Handle<Object> value,
2532 PropertyAttributes attributes);
Steve Blocka7e24c12009-10-30 11:49:00 +00002533
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002534 MUST_USE_RESULT static Maybe<bool> DeletePropertyWithInterceptor(
Ben Murdoch097c5b22016-05-18 11:27:45 +01002535 LookupIterator* it, ShouldThrow should_throw);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002536
2537 bool ReferencesObjectFromElements(FixedArray* elements,
2538 ElementsKind kind,
2539 Object* object);
Steve Blocka7e24c12009-10-30 11:49:00 +00002540
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002541 // Return the hash table backing store or the inline stored identity hash,
2542 // whatever is found.
2543 MUST_USE_RESULT Object* GetHiddenPropertiesHashTable();
2544
2545 // Return the hash table backing store for hidden properties. If there is no
2546 // backing store, allocate one.
2547 static Handle<ObjectHashTable> GetOrCreateHiddenPropertiesHashtable(
2548 Handle<JSObject> object);
2549
2550 // Set the hidden property backing store to either a hash table or
2551 // the inline-stored identity hash.
2552 static Handle<Object> SetHiddenPropertiesHashTable(
2553 Handle<JSObject> object,
2554 Handle<Object> value);
2555
Ben Murdochda12d292016-06-02 14:46:10 +01002556 static Handle<Object> GetIdentityHash(Isolate* isolate,
2557 Handle<JSObject> object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002558
2559 static Handle<Smi> GetOrCreateIdentityHash(Handle<JSObject> object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002560
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002561 // Helper for fast versions of preventExtensions, seal, and freeze.
2562 // attrs is one of NONE, SEALED, or FROZEN (depending on the operation).
2563 template <PropertyAttributes attrs>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002564 MUST_USE_RESULT static Maybe<bool> PreventExtensionsWithTransition(
2565 Handle<JSObject> object, ShouldThrow should_throw);
2566
2567 MUST_USE_RESULT static Maybe<bool> SetPrototypeUnobserved(
2568 Handle<JSObject> object, Handle<Object> value, bool from_javascript,
2569 ShouldThrow should_throw);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002570
Steve Blocka7e24c12009-10-30 11:49:00 +00002571 DISALLOW_IMPLICIT_CONSTRUCTORS(JSObject);
2572};
2573
2574
Ben Murdoch097c5b22016-05-18 11:27:45 +01002575// JSAccessorPropertyDescriptor is just a JSObject with a specific initial
2576// map. This initial map adds in-object properties for "get", "set",
2577// "enumerable" and "configurable" properties, as assigned by the
2578// FromPropertyDescriptor function for regular accessor properties.
2579class JSAccessorPropertyDescriptor: public JSObject {
2580 public:
2581 // Offsets of object fields.
2582 static const int kGetOffset = JSObject::kHeaderSize;
2583 static const int kSetOffset = kGetOffset + kPointerSize;
2584 static const int kEnumerableOffset = kSetOffset + kPointerSize;
2585 static const int kConfigurableOffset = kEnumerableOffset + kPointerSize;
2586 static const int kSize = kConfigurableOffset + kPointerSize;
2587 // Indices of in-object properties.
2588 static const int kGetIndex = 0;
2589 static const int kSetIndex = 1;
2590 static const int kEnumerableIndex = 2;
2591 static const int kConfigurableIndex = 3;
2592
2593 private:
2594 DISALLOW_IMPLICIT_CONSTRUCTORS(JSAccessorPropertyDescriptor);
2595};
2596
2597
2598// JSDataPropertyDescriptor is just a JSObject with a specific initial map.
2599// This initial map adds in-object properties for "value", "writable",
2600// "enumerable" and "configurable" properties, as assigned by the
2601// FromPropertyDescriptor function for regular data properties.
2602class JSDataPropertyDescriptor: public JSObject {
2603 public:
2604 // Offsets of object fields.
2605 static const int kValueOffset = JSObject::kHeaderSize;
2606 static const int kWritableOffset = kValueOffset + kPointerSize;
2607 static const int kEnumerableOffset = kWritableOffset + kPointerSize;
2608 static const int kConfigurableOffset = kEnumerableOffset + kPointerSize;
2609 static const int kSize = kConfigurableOffset + kPointerSize;
2610 // Indices of in-object properties.
2611 static const int kValueIndex = 0;
2612 static const int kWritableIndex = 1;
2613 static const int kEnumerableIndex = 2;
2614 static const int kConfigurableIndex = 3;
2615
2616 private:
2617 DISALLOW_IMPLICIT_CONSTRUCTORS(JSDataPropertyDescriptor);
2618};
2619
2620
2621// JSIteratorResult is just a JSObject with a specific initial map.
2622// This initial map adds in-object properties for "done" and "value",
2623// as specified by ES6 section 25.1.1.3 The IteratorResult Interface
2624class JSIteratorResult: public JSObject {
2625 public:
2626 // Offsets of object fields.
2627 static const int kValueOffset = JSObject::kHeaderSize;
2628 static const int kDoneOffset = kValueOffset + kPointerSize;
2629 static const int kSize = kDoneOffset + kPointerSize;
2630 // Indices of in-object properties.
2631 static const int kValueIndex = 0;
2632 static const int kDoneIndex = 1;
2633
2634 private:
2635 DISALLOW_IMPLICIT_CONSTRUCTORS(JSIteratorResult);
2636};
2637
2638
2639// Common superclass for JSSloppyArgumentsObject and JSStrictArgumentsObject.
2640class JSArgumentsObject: public JSObject {
2641 public:
2642 // Offsets of object fields.
2643 static const int kLengthOffset = JSObject::kHeaderSize;
2644 static const int kHeaderSize = kLengthOffset + kPointerSize;
2645 // Indices of in-object properties.
2646 static const int kLengthIndex = 0;
2647
2648 private:
2649 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArgumentsObject);
2650};
2651
2652
2653// JSSloppyArgumentsObject is just a JSObject with specific initial map.
2654// This initial map adds in-object properties for "length" and "callee".
2655class JSSloppyArgumentsObject: public JSArgumentsObject {
2656 public:
2657 // Offsets of object fields.
2658 static const int kCalleeOffset = JSArgumentsObject::kHeaderSize;
2659 static const int kSize = kCalleeOffset + kPointerSize;
2660 // Indices of in-object properties.
2661 static const int kCalleeIndex = 1;
2662
2663 private:
2664 DISALLOW_IMPLICIT_CONSTRUCTORS(JSSloppyArgumentsObject);
2665};
2666
2667
2668// JSStrictArgumentsObject is just a JSObject with specific initial map.
2669// This initial map adds an in-object property for "length".
2670class JSStrictArgumentsObject: public JSArgumentsObject {
2671 public:
2672 // Offsets of object fields.
2673 static const int kSize = JSArgumentsObject::kHeaderSize;
2674
2675 private:
2676 DISALLOW_IMPLICIT_CONSTRUCTORS(JSStrictArgumentsObject);
2677};
2678
2679
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002680// Common superclass for FixedArrays that allow implementations to share
2681// common accessors and some code paths.
2682class FixedArrayBase: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00002683 public:
2684 // [length]: length of the array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002685 inline int length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00002686 inline void set_length(int value);
2687
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002688 // Get and set the length using acquire loads and release stores.
2689 inline int synchronized_length() const;
2690 inline void synchronized_set_length(int value);
2691
2692 DECLARE_CAST(FixedArrayBase)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002693
2694 // Layout description.
2695 // Length is smi tagged when it is stored.
2696 static const int kLengthOffset = HeapObject::kHeaderSize;
2697 static const int kHeaderSize = kLengthOffset + kPointerSize;
2698};
2699
2700
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002701class FixedDoubleArray;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002702class IncrementalMarking;
2703
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002704
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002705// FixedArray describes fixed-sized arrays with element type Object*.
2706class FixedArray: public FixedArrayBase {
2707 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00002708 // Setter and getter for elements.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002709 inline Object* get(int index) const;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002710 static inline Handle<Object> get(FixedArray* array, int index,
2711 Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00002712 // Setter that uses write barrier.
2713 inline void set(int index, Object* value);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002714 inline bool is_the_hole(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00002715
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002716 // Setter that doesn't need write barrier.
Steve Blocka7e24c12009-10-30 11:49:00 +00002717 inline void set(int index, Smi* value);
2718 // Setter with explicit barrier mode.
2719 inline void set(int index, Object* value, WriteBarrierMode mode);
2720
2721 // Setters for frequently used oddballs located in old space.
2722 inline void set_undefined(int index);
2723 inline void set_null(int index);
2724 inline void set_the_hole(int index);
2725
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002726 inline Object** GetFirstElementAddress();
2727 inline bool ContainsOnlySmisOrHoles();
2728
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002729 // Gives access to raw memory which stores the array's data.
2730 inline Object** data_start();
2731
2732 inline void FillWithHoles(int from, int to);
2733
2734 // Shrink length and insert filler objects.
2735 void Shrink(int length);
2736
Steve Blocka7e24c12009-10-30 11:49:00 +00002737 // Copy a sub array from the receiver to dest.
2738 void CopyTo(int pos, FixedArray* dest, int dest_pos, int len);
2739
2740 // Garbage collection support.
2741 static int SizeFor(int length) { return kHeaderSize + length * kPointerSize; }
2742
2743 // Code Generation support.
2744 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2745
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002746 // Garbage collection support.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002747 inline Object** RawFieldOfElementAt(int index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002748
2749 DECLARE_CAST(FixedArray)
Steve Blocka7e24c12009-10-30 11:49:00 +00002750
Leon Clarkee46be812010-01-19 14:06:41 +00002751 // Maximal allowed size, in bytes, of a single FixedArray.
2752 // Prevents overflowing size computations, as well as extreme memory
2753 // consumption.
Ben Murdoch692be652012-01-10 18:47:50 +00002754 static const int kMaxSize = 128 * MB * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00002755 // Maximally allowed length of a FixedArray.
2756 static const int kMaxLength = (kMaxSize - kHeaderSize) / kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00002757
2758 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002759 DECLARE_PRINTER(FixedArray)
2760 DECLARE_VERIFIER(FixedArray)
Steve Blocka7e24c12009-10-30 11:49:00 +00002761#ifdef DEBUG
Steve Blocka7e24c12009-10-30 11:49:00 +00002762 // Checks if two FixedArrays have identical contents.
2763 bool IsEqualTo(FixedArray* other);
2764#endif
2765
2766 // Swap two elements in a pair of arrays. If this array and the
2767 // numbers array are the same object, the elements are only swapped
2768 // once.
2769 void SwapPairs(FixedArray* numbers, int i, int j);
2770
2771 // Sort prefix of this array and the numbers array as pairs wrt. the
2772 // numbers. If the numbers array and the this array are the same
2773 // object, the prefix of this array is sorted.
2774 void SortPairs(FixedArray* numbers, uint32_t len);
2775
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002776 typedef FlexibleBodyDescriptor<kHeaderSize> BodyDescriptor;
Iain Merrick75681382010-08-19 15:07:18 +01002777
Steve Blocka7e24c12009-10-30 11:49:00 +00002778 protected:
Leon Clarke4515c472010-02-03 11:58:03 +00002779 // Set operation on FixedArray without using write barriers. Can
2780 // only be used for storing old space objects or smis.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002781 static inline void NoWriteBarrierSet(FixedArray* array,
2782 int index,
2783 Object* value);
2784
Steve Blocka7e24c12009-10-30 11:49:00 +00002785 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002786 STATIC_ASSERT(kHeaderSize == Internals::kFixedArrayHeaderSize);
2787
Steve Blocka7e24c12009-10-30 11:49:00 +00002788 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedArray);
2789};
2790
2791
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002792// FixedDoubleArray describes fixed-sized arrays with element type double.
2793class FixedDoubleArray: public FixedArrayBase {
2794 public:
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002795 // Setter and getter for elements.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002796 inline double get_scalar(int index);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002797 inline uint64_t get_representation(int index);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002798 static inline Handle<Object> get(FixedDoubleArray* array, int index,
2799 Isolate* isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002800 inline void set(int index, double value);
2801 inline void set_the_hole(int index);
2802
2803 // Checking for the hole.
2804 inline bool is_the_hole(int index);
2805
2806 // Garbage collection support.
2807 inline static int SizeFor(int length) {
2808 return kHeaderSize + length * kDoubleSize;
2809 }
2810
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002811 // Gives access to raw memory which stores the array's data.
2812 inline double* data_start();
2813
2814 inline void FillWithHoles(int from, int to);
2815
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002816 // Code Generation support.
2817 static int OffsetOfElementAt(int index) { return SizeFor(index); }
2818
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002819 DECLARE_CAST(FixedDoubleArray)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002820
2821 // Maximal allowed size, in bytes, of a single FixedDoubleArray.
2822 // Prevents overflowing size computations, as well as extreme memory
2823 // consumption.
2824 static const int kMaxSize = 512 * MB;
2825 // Maximally allowed length of a FixedArray.
2826 static const int kMaxLength = (kMaxSize - kHeaderSize) / kDoubleSize;
2827
2828 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002829 DECLARE_PRINTER(FixedDoubleArray)
2830 DECLARE_VERIFIER(FixedDoubleArray)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002831
2832 private:
2833 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedDoubleArray);
2834};
2835
2836
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002837class WeakFixedArray : public FixedArray {
2838 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002839 // If |maybe_array| is not a WeakFixedArray, a fresh one will be allocated.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002840 // This function does not check if the value exists already, callers must
2841 // ensure this themselves if necessary.
2842 static Handle<WeakFixedArray> Add(Handle<Object> maybe_array,
2843 Handle<HeapObject> value,
2844 int* assigned_index = NULL);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002845
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002846 // Returns true if an entry was found and removed.
2847 bool Remove(Handle<HeapObject> value);
2848
2849 class NullCallback {
2850 public:
2851 static void Callback(Object* value, int old_index, int new_index) {}
2852 };
2853
2854 template <class CompactionCallback>
2855 void Compact();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002856
2857 inline Object* Get(int index) const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002858 inline void Clear(int index);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002859 inline int Length() const;
2860
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002861 inline bool IsEmptySlot(int index) const;
2862 static Object* Empty() { return Smi::FromInt(0); }
2863
2864 class Iterator {
2865 public:
2866 explicit Iterator(Object* maybe_array) : list_(NULL) { Reset(maybe_array); }
2867 void Reset(Object* maybe_array);
2868
2869 template <class T>
2870 inline T* Next();
2871
2872 private:
2873 int index_;
2874 WeakFixedArray* list_;
2875#ifdef DEBUG
2876 int last_used_index_;
2877 DisallowHeapAllocation no_gc_;
2878#endif // DEBUG
2879 DISALLOW_COPY_AND_ASSIGN(Iterator);
2880 };
2881
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002882 DECLARE_CAST(WeakFixedArray)
2883
2884 private:
2885 static const int kLastUsedIndexIndex = 0;
2886 static const int kFirstIndex = 1;
2887
2888 static Handle<WeakFixedArray> Allocate(
2889 Isolate* isolate, int size, Handle<WeakFixedArray> initialize_from);
2890
2891 static void Set(Handle<WeakFixedArray> array, int index,
2892 Handle<HeapObject> value);
2893 inline void clear(int index);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002894
2895 inline int last_used_index() const;
2896 inline void set_last_used_index(int index);
2897
2898 // Disallow inherited setters.
2899 void set(int index, Smi* value);
2900 void set(int index, Object* value);
2901 void set(int index, Object* value, WriteBarrierMode mode);
2902 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakFixedArray);
2903};
2904
2905
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002906// Generic array grows dynamically with O(1) amortized insertion.
2907class ArrayList : public FixedArray {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002908 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002909 enum AddMode {
2910 kNone,
2911 // Use this if GC can delete elements from the array.
2912 kReloadLengthAfterAllocation,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002913 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002914 static Handle<ArrayList> Add(Handle<ArrayList> array, Handle<Object> obj,
2915 AddMode mode = kNone);
2916 static Handle<ArrayList> Add(Handle<ArrayList> array, Handle<Object> obj1,
2917 Handle<Object> obj2, AddMode = kNone);
2918 inline int Length();
2919 inline void SetLength(int length);
2920 inline Object* Get(int index);
2921 inline Object** Slot(int index);
2922 inline void Set(int index, Object* obj);
2923 inline void Clear(int index, Object* undefined);
2924 bool IsFull();
2925 DECLARE_CAST(ArrayList)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002926
2927 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002928 static Handle<ArrayList> EnsureSpace(Handle<ArrayList> array, int length);
2929 static const int kLengthIndex = 0;
2930 static const int kFirstIndex = 1;
2931 DISALLOW_IMPLICIT_CONSTRUCTORS(ArrayList);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002932};
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002933
2934
Steve Blocka7e24c12009-10-30 11:49:00 +00002935// DescriptorArrays are fixed arrays used to hold instance descriptors.
2936// The format of the these objects is:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002937// [0]: Number of descriptors
2938// [1]: Either Smi(0) if uninitialized, or a pointer to small fixed array:
2939// [0]: pointer to fixed array with enum cache
2940// [1]: either Smi(0) or pointer to fixed array with indices
2941// [2]: first key
2942// [2 + number of descriptors * kDescriptorSize]: start of slack
Steve Blocka7e24c12009-10-30 11:49:00 +00002943class DescriptorArray: public FixedArray {
2944 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00002945 // Returns true for both shared empty_descriptor_array and for smis, which the
2946 // map uses to encode additional bit fields when the descriptor array is not
2947 // yet used.
Steve Blocka7e24c12009-10-30 11:49:00 +00002948 inline bool IsEmpty();
Leon Clarkee46be812010-01-19 14:06:41 +00002949
Steve Blocka7e24c12009-10-30 11:49:00 +00002950 // Returns the number of descriptors in the array.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002951 inline int number_of_descriptors();
Steve Blocka7e24c12009-10-30 11:49:00 +00002952
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002953 inline int number_of_descriptors_storage();
Steve Blocka7e24c12009-10-30 11:49:00 +00002954
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002955 inline int NumberOfSlackDescriptors();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002956
2957 inline void SetNumberOfDescriptors(int number_of_descriptors);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002958 inline int number_of_entries();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002959
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002960 inline bool HasEnumCache();
Steve Blocka7e24c12009-10-30 11:49:00 +00002961
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002962 inline void CopyEnumCacheFrom(DescriptorArray* array);
Steve Blocka7e24c12009-10-30 11:49:00 +00002963
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002964 inline FixedArray* GetEnumCache();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002965
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002966 inline bool HasEnumIndicesCache();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002967
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002968 inline FixedArray* GetEnumIndicesCache();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002969
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002970 inline Object** GetEnumCacheSlot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002971
2972 void ClearEnumCache();
Ben Murdoch257744e2011-11-30 15:57:28 +00002973
Steve Blocka7e24c12009-10-30 11:49:00 +00002974 // Initialize or change the enum cache,
2975 // using the supplied storage for the small "bridge".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002976 static void SetEnumCache(Handle<DescriptorArray> descriptors,
2977 Isolate* isolate, Handle<FixedArray> new_cache,
2978 Handle<FixedArray> new_index_cache);
Steve Blocka7e24c12009-10-30 11:49:00 +00002979
2980 // Accessors for fetching instance descriptor at descriptor number.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002981 inline Name* GetKey(int descriptor_number);
2982 inline Object** GetKeySlot(int descriptor_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00002983 inline Object* GetValue(int descriptor_number);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002984 inline void SetValue(int descriptor_number, Object* value);
2985 inline Object** GetValueSlot(int descriptor_number);
2986 static inline int GetValueOffset(int descriptor_number);
2987 inline Object** GetDescriptorStartSlot(int descriptor_number);
2988 inline Object** GetDescriptorEndSlot(int descriptor_number);
2989 inline PropertyDetails GetDetails(int descriptor_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00002990 inline PropertyType GetType(int descriptor_number);
2991 inline int GetFieldIndex(int descriptor_number);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002992 FieldType* GetFieldType(int descriptor_number);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002993 inline Object* GetConstant(int descriptor_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00002994 inline Object* GetCallbacksObject(int descriptor_number);
2995 inline AccessorDescriptor* GetCallbacks(int descriptor_number);
Steve Blocka7e24c12009-10-30 11:49:00 +00002996
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002997 inline Name* GetSortedKey(int descriptor_number);
2998 inline int GetSortedKeyIndex(int descriptor_number);
2999 inline void SetSortedKey(int pointer, int descriptor_number);
3000 inline void SetRepresentation(int descriptor_number,
3001 Representation representation);
3002
3003 // Accessor for complete descriptor.
3004 inline void Get(int descriptor_number, Descriptor* desc);
3005 inline void Set(int descriptor_number, Descriptor* desc);
3006 void Replace(int descriptor_number, Descriptor* descriptor);
3007
3008 // Append automatically sets the enumeration index. This should only be used
3009 // to add descriptors in bulk at the end, followed by sorting the descriptor
3010 // array.
3011 inline void Append(Descriptor* desc);
3012
3013 static Handle<DescriptorArray> CopyUpTo(Handle<DescriptorArray> desc,
3014 int enumeration_index,
3015 int slack = 0);
3016
3017 static Handle<DescriptorArray> CopyUpToAddAttributes(
3018 Handle<DescriptorArray> desc,
3019 int enumeration_index,
3020 PropertyAttributes attributes,
3021 int slack = 0);
3022
3023 // Sort the instance descriptors by the hash codes of their keys.
3024 void Sort();
3025
3026 // Search the instance descriptors for given name.
3027 INLINE(int Search(Name* name, int number_of_own_descriptors));
3028
3029 // As the above, but uses DescriptorLookupCache and updates it when
3030 // necessary.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003031 INLINE(int SearchWithCache(Isolate* isolate, Name* name, Map* map));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003032
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003033 bool IsEqualUpTo(DescriptorArray* desc, int nof_descriptors);
3034
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003035 // Allocates a DescriptorArray, but returns the singleton
3036 // empty descriptor array object if number_of_descriptors is 0.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003037 static Handle<DescriptorArray> Allocate(
3038 Isolate* isolate, int number_of_descriptors, int slack,
3039 PretenureFlag pretenure = NOT_TENURED);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003040
3041 DECLARE_CAST(DescriptorArray)
3042
3043 // Constant for denoting key was not found.
3044 static const int kNotFound = -1;
3045
3046 static const int kDescriptorLengthIndex = 0;
3047 static const int kEnumCacheIndex = 1;
3048 static const int kFirstIndex = 2;
3049
3050 // The length of the "bridge" to the enum cache.
3051 static const int kEnumCacheBridgeLength = 2;
3052 static const int kEnumCacheBridgeCacheIndex = 0;
3053 static const int kEnumCacheBridgeIndicesCacheIndex = 1;
3054
3055 // Layout description.
3056 static const int kDescriptorLengthOffset = FixedArray::kHeaderSize;
3057 static const int kEnumCacheOffset = kDescriptorLengthOffset + kPointerSize;
3058 static const int kFirstOffset = kEnumCacheOffset + kPointerSize;
3059
3060 // Layout description for the bridge array.
3061 static const int kEnumCacheBridgeCacheOffset = FixedArray::kHeaderSize;
3062
3063 // Layout of descriptor.
3064 static const int kDescriptorKey = 0;
3065 static const int kDescriptorDetails = 1;
3066 static const int kDescriptorValue = 2;
3067 static const int kDescriptorSize = 3;
3068
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003069#if defined(DEBUG) || defined(OBJECT_PRINT)
3070 // For our gdb macros, we should perhaps change these in the future.
3071 void Print();
3072
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003073 // Print all the descriptors.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003074 void PrintDescriptors(std::ostream& os); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003075#endif
3076
3077#ifdef DEBUG
3078 // Is the descriptor array sorted and without duplicates?
3079 bool IsSortedNoDuplicates(int valid_descriptors = -1);
3080
3081 // Is the descriptor array consistent with the back pointers in targets?
3082 bool IsConsistentWithBackPointers(Map* current_map);
3083
3084 // Are two DescriptorArrays equal?
3085 bool IsEqualTo(DescriptorArray* other);
3086#endif
3087
3088 // Returns the fixed array length required to hold number_of_descriptors
3089 // descriptors.
3090 static int LengthFor(int number_of_descriptors) {
3091 return ToKeyIndex(number_of_descriptors);
3092 }
3093
Ben Murdochda12d292016-06-02 14:46:10 +01003094 static int ToDetailsIndex(int descriptor_number) {
3095 return kFirstIndex + (descriptor_number * kDescriptorSize) +
3096 kDescriptorDetails;
3097 }
3098
3099 // Conversion from descriptor number to array indices.
3100 static int ToKeyIndex(int descriptor_number) {
3101 return kFirstIndex + (descriptor_number * kDescriptorSize) + kDescriptorKey;
3102 }
3103
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003104 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003105 // An entry in a DescriptorArray, represented as an (array, index) pair.
3106 class Entry {
3107 public:
3108 inline explicit Entry(DescriptorArray* descs, int index) :
3109 descs_(descs), index_(index) { }
3110
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003111 inline PropertyType type();
3112 inline Object* GetCallbackObject();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003113
3114 private:
3115 DescriptorArray* descs_;
3116 int index_;
3117 };
3118
Steve Blocka7e24c12009-10-30 11:49:00 +00003119 static int ToValueIndex(int descriptor_number) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003120 return kFirstIndex +
3121 (descriptor_number * kDescriptorSize) +
3122 kDescriptorValue;
Steve Blocka7e24c12009-10-30 11:49:00 +00003123 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003124
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003125 // Transfer a complete descriptor from the src descriptor array to this
3126 // descriptor array.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003127 void CopyFrom(int index, DescriptorArray* src);
Steve Blocka7e24c12009-10-30 11:49:00 +00003128
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003129 inline void SetDescriptor(int descriptor_number, Descriptor* desc);
Steve Blocka7e24c12009-10-30 11:49:00 +00003130
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003131 // Swap first and second descriptor.
3132 inline void SwapSortedKeys(int first, int second);
3133
Steve Blocka7e24c12009-10-30 11:49:00 +00003134 DISALLOW_IMPLICIT_CONSTRUCTORS(DescriptorArray);
3135};
3136
3137
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003138enum SearchMode { ALL_ENTRIES, VALID_ENTRIES };
3139
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003140template <SearchMode search_mode, typename T>
3141inline int Search(T* array, Name* name, int valid_entries = 0,
3142 int* out_insertion_index = NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003143
3144
Steve Blocka7e24c12009-10-30 11:49:00 +00003145// HashTable is a subclass of FixedArray that implements a hash table
3146// that uses open addressing and quadratic probing.
3147//
3148// In order for the quadratic probing to work, elements that have not
3149// yet been used and elements that have been deleted are
3150// distinguished. Probing continues when deleted elements are
3151// encountered and stops when unused elements are encountered.
3152//
3153// - Elements with key == undefined have not been used yet.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003154// - Elements with key == the_hole have been deleted.
Steve Blocka7e24c12009-10-30 11:49:00 +00003155//
3156// The hash table class is parameterized with a Shape and a Key.
3157// Shape must be a class with the following interface:
3158// class ExampleShape {
3159// public:
3160// // Tells whether key matches other.
3161// static bool IsMatch(Key key, Object* other);
3162// // Returns the hash value for key.
3163// static uint32_t Hash(Key key);
3164// // Returns the hash value for object.
3165// static uint32_t HashForObject(Key key, Object* object);
3166// // Convert key to an object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003167// static inline Handle<Object> AsHandle(Isolate* isolate, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003168// // The prefix size indicates number of elements in the beginning
3169// // of the backing storage.
3170// static const int kPrefixSize = ..;
3171// // The Element size indicates number of elements per entry.
3172// static const int kEntrySize = ..;
3173// };
Steve Block3ce2e202009-11-05 08:53:23 +00003174// The prefix size indicates an amount of memory in the
Steve Blocka7e24c12009-10-30 11:49:00 +00003175// beginning of the backing storage that can be used for non-element
3176// information by subclasses.
3177
Ben Murdochc7cc0282012-03-05 14:35:55 +00003178template<typename Key>
3179class BaseShape {
3180 public:
3181 static const bool UsesSeed = false;
3182 static uint32_t Hash(Key key) { return 0; }
3183 static uint32_t SeededHash(Key key, uint32_t seed) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003184 DCHECK(UsesSeed);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003185 return Hash(key);
3186 }
3187 static uint32_t HashForObject(Key key, Object* object) { return 0; }
3188 static uint32_t SeededHashForObject(Key key, uint32_t seed, Object* object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003189 DCHECK(UsesSeed);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003190 return HashForObject(key, object);
3191 }
3192};
3193
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003194
3195class HashTableBase : public FixedArray {
3196 public:
3197 // Returns the number of elements in the hash table.
3198 inline int NumberOfElements();
3199
3200 // Returns the number of deleted elements in the hash table.
3201 inline int NumberOfDeletedElements();
3202
3203 // Returns the capacity of the hash table.
3204 inline int Capacity();
3205
3206 // ElementAdded should be called whenever an element is added to a
3207 // hash table.
3208 inline void ElementAdded();
3209
3210 // ElementRemoved should be called whenever an element is removed from
3211 // a hash table.
3212 inline void ElementRemoved();
3213 inline void ElementsRemoved(int n);
3214
3215 // Computes the required capacity for a table holding the given
3216 // number of elements. May be more than HashTable::kMaxCapacity.
3217 static inline int ComputeCapacity(int at_least_space_for);
3218
3219 // Tells whether k is a real key. The hole and undefined are not allowed
3220 // as keys and can be used to indicate missing or deleted elements.
3221 inline bool IsKey(Object* k);
Ben Murdochda12d292016-06-02 14:46:10 +01003222 inline bool IsKey(Heap* heap, Object* k);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003223
3224 // Compute the probe offset (quadratic probing).
3225 INLINE(static uint32_t GetProbeOffset(uint32_t n)) {
3226 return (n + n * n) >> 1;
3227 }
3228
3229 static const int kNumberOfElementsIndex = 0;
3230 static const int kNumberOfDeletedElementsIndex = 1;
3231 static const int kCapacityIndex = 2;
3232 static const int kPrefixStartIndex = 3;
3233
3234 // Constant used for denoting a absent entry.
3235 static const int kNotFound = -1;
3236
3237 protected:
3238 // Update the number of elements in the hash table.
3239 inline void SetNumberOfElements(int nof);
3240
3241 // Update the number of deleted elements in the hash table.
3242 inline void SetNumberOfDeletedElements(int nod);
3243
3244 // Returns probe entry.
3245 static uint32_t GetProbe(uint32_t hash, uint32_t number, uint32_t size) {
3246 DCHECK(base::bits::IsPowerOfTwo32(size));
3247 return (hash + GetProbeOffset(number)) & (size - 1);
3248 }
3249
3250 inline static uint32_t FirstProbe(uint32_t hash, uint32_t size) {
3251 return hash & (size - 1);
3252 }
3253
3254 inline static uint32_t NextProbe(
3255 uint32_t last, uint32_t number, uint32_t size) {
3256 return (last + number) & (size - 1);
3257 }
3258};
3259
3260
3261template <typename Derived, typename Shape, typename Key>
3262class HashTable : public HashTableBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00003263 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +00003264 // Wrapper methods
3265 inline uint32_t Hash(Key key) {
3266 if (Shape::UsesSeed) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003267 return Shape::SeededHash(key, GetHeap()->HashSeed());
Ben Murdochc7cc0282012-03-05 14:35:55 +00003268 } else {
3269 return Shape::Hash(key);
3270 }
3271 }
3272
3273 inline uint32_t HashForObject(Key key, Object* object) {
3274 if (Shape::UsesSeed) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003275 return Shape::SeededHashForObject(key, GetHeap()->HashSeed(), object);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003276 } else {
3277 return Shape::HashForObject(key, object);
3278 }
3279 }
3280
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003281 // Returns a new HashTable object.
3282 MUST_USE_RESULT static Handle<Derived> New(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003283 Isolate* isolate, int at_least_space_for,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003284 MinimumCapacity capacity_option = USE_DEFAULT_MINIMUM_CAPACITY,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003285 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00003286
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003287 DECLARE_CAST(HashTable)
Steve Blocka7e24c12009-10-30 11:49:00 +00003288
3289 // Garbage collection support.
3290 void IteratePrefix(ObjectVisitor* visitor);
3291 void IterateElements(ObjectVisitor* visitor);
3292
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003293 // Find entry for key otherwise return kNotFound.
3294 inline int FindEntry(Key key);
3295 inline int FindEntry(Isolate* isolate, Key key, int32_t hash);
3296 int FindEntry(Isolate* isolate, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003297
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003298 // Rehashes the table in-place.
3299 void Rehash(Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003300
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003301 // Returns the key at entry.
3302 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
3303
3304 static const int kElementsStartIndex = kPrefixStartIndex + Shape::kPrefixSize;
Leon Clarkee46be812010-01-19 14:06:41 +00003305 static const int kEntrySize = Shape::kEntrySize;
3306 static const int kElementsStartOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00003307 kHeaderSize + kElementsStartIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01003308 static const int kCapacityOffset =
3309 kHeaderSize + kCapacityIndex * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00003310
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003311 // Returns the index for an entry (of the key)
3312 static inline int EntryToIndex(int entry) {
3313 return (entry * kEntrySize) + kElementsStartIndex;
3314 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003315
Steve Blocka7e24c12009-10-30 11:49:00 +00003316 protected:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003317 friend class ObjectHashTable;
3318
Steve Blocka7e24c12009-10-30 11:49:00 +00003319 // Find the entry at which to insert element with the given key that
3320 // has the given hash value.
3321 uint32_t FindInsertionEntry(uint32_t hash);
3322
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003323 // Attempt to shrink hash table after removal of key.
3324 MUST_USE_RESULT static Handle<Derived> Shrink(Handle<Derived> table, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003325
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003326 // Ensure enough space for n additional elements.
3327 MUST_USE_RESULT static Handle<Derived> EnsureCapacity(
3328 Handle<Derived> table,
3329 int n,
3330 Key key,
3331 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00003332
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003333 // Returns true if this table has sufficient capacity for adding n elements.
3334 bool HasSufficientCapacity(int n);
Leon Clarkee46be812010-01-19 14:06:41 +00003335
Steve Blocka7e24c12009-10-30 11:49:00 +00003336 // Sets the capacity of the hash table.
3337 void SetCapacity(int capacity) {
3338 // To scale a computed hash code to fit within the hash table, we
3339 // use bit-wise AND with a mask, so the capacity must be positive
3340 // and non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003341 DCHECK(capacity > 0);
3342 DCHECK(capacity <= kMaxCapacity);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003343 set(kCapacityIndex, Smi::FromInt(capacity));
Steve Blocka7e24c12009-10-30 11:49:00 +00003344 }
3345
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003346 // Maximal capacity of HashTable. Based on maximal length of underlying
3347 // FixedArray. Staying below kMaxCapacity also ensures that EntryToIndex
3348 // cannot overflow.
3349 static const int kMaxCapacity =
3350 (FixedArray::kMaxLength - kElementsStartOffset) / kEntrySize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003351
3352 private:
3353 // Returns _expected_ if one of entries given by the first _probe_ probes is
3354 // equal to _expected_. Otherwise, returns the entry given by the probe
3355 // number _probe_.
3356 uint32_t EntryForProbe(Key key, Object* k, int probe, uint32_t expected);
3357
3358 void Swap(uint32_t entry1, uint32_t entry2, WriteBarrierMode mode);
3359
3360 // Rehashes this hash-table into the new table.
3361 void Rehash(Handle<Derived> new_table, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003362};
3363
3364
Steve Blocka7e24c12009-10-30 11:49:00 +00003365// HashTableKey is an abstract superclass for virtual key behavior.
3366class HashTableKey {
3367 public:
3368 // Returns whether the other object matches this key.
3369 virtual bool IsMatch(Object* other) = 0;
3370 // Returns the hash value for this key.
3371 virtual uint32_t Hash() = 0;
3372 // Returns the hash value for object.
3373 virtual uint32_t HashForObject(Object* key) = 0;
Steve Block3ce2e202009-11-05 08:53:23 +00003374 // Returns the key object for storing into the hash table.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003375 MUST_USE_RESULT virtual Handle<Object> AsHandle(Isolate* isolate) = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +00003376 // Required.
3377 virtual ~HashTableKey() {}
3378};
3379
Ben Murdochc7cc0282012-03-05 14:35:55 +00003380
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003381class StringTableShape : public BaseShape<HashTableKey*> {
Steve Blocka7e24c12009-10-30 11:49:00 +00003382 public:
Steve Block44f0eee2011-05-26 01:26:41 +01003383 static inline bool IsMatch(HashTableKey* key, Object* value) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003384 return key->IsMatch(value);
3385 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003386
Steve Block44f0eee2011-05-26 01:26:41 +01003387 static inline uint32_t Hash(HashTableKey* key) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003388 return key->Hash();
3389 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003390
Steve Block44f0eee2011-05-26 01:26:41 +01003391 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
Steve Blocka7e24c12009-10-30 11:49:00 +00003392 return key->HashForObject(object);
3393 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003394
3395 static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003396
3397 static const int kPrefixSize = 0;
3398 static const int kEntrySize = 1;
3399};
3400
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003401class SeqOneByteString;
Ben Murdoch257744e2011-11-30 15:57:28 +00003402
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003403// StringTable.
Steve Blocka7e24c12009-10-30 11:49:00 +00003404//
3405// No special elements in the prefix and the element size is 1
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003406// because only the string itself (the key) needs to be stored.
3407class StringTable: public HashTable<StringTable,
3408 StringTableShape,
3409 HashTableKey*> {
Steve Blocka7e24c12009-10-30 11:49:00 +00003410 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003411 // Find string in the string table. If it is not there yet, it is
3412 // added. The return value is the string found.
3413 static Handle<String> LookupString(Isolate* isolate, Handle<String> key);
3414 static Handle<String> LookupKey(Isolate* isolate, HashTableKey* key);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003415 static String* LookupKeyIfExists(Isolate* isolate, HashTableKey* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003416
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003417 // Tries to internalize given string and returns string handle on success
3418 // or an empty handle otherwise.
3419 MUST_USE_RESULT static MaybeHandle<String> InternalizeStringIfExists(
3420 Isolate* isolate,
3421 Handle<String> string);
Steve Blocka7e24c12009-10-30 11:49:00 +00003422
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003423 // Looks up a string that is equal to the given string and returns
3424 // string handle if it is found, or an empty handle otherwise.
3425 MUST_USE_RESULT static MaybeHandle<String> LookupStringIfExists(
3426 Isolate* isolate,
3427 Handle<String> str);
3428 MUST_USE_RESULT static MaybeHandle<String> LookupTwoCharsStringIfExists(
3429 Isolate* isolate,
3430 uint16_t c1,
3431 uint16_t c2);
3432
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003433 static void EnsureCapacityForDeserialization(Isolate* isolate, int expected);
3434
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003435 DECLARE_CAST(StringTable)
Steve Blocka7e24c12009-10-30 11:49:00 +00003436
3437 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003438 template <bool seq_one_byte>
3439 friend class JsonParser;
Steve Blocka7e24c12009-10-30 11:49:00 +00003440
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003441 DISALLOW_IMPLICIT_CONSTRUCTORS(StringTable);
Steve Blocka7e24c12009-10-30 11:49:00 +00003442};
3443
Ben Murdochda12d292016-06-02 14:46:10 +01003444class StringSetShape : public BaseShape<String*> {
3445 public:
3446 static inline bool IsMatch(String* key, Object* value);
3447 static inline uint32_t Hash(String* key);
3448 static inline uint32_t HashForObject(String* key, Object* object);
3449
3450 static const int kPrefixSize = 0;
3451 static const int kEntrySize = 1;
3452};
3453
3454class StringSet : public HashTable<StringSet, StringSetShape, String*> {
3455 public:
3456 static Handle<StringSet> New(Isolate* isolate);
3457 static Handle<StringSet> Add(Handle<StringSet> blacklist,
3458 Handle<String> name);
3459 bool Has(Handle<String> name);
3460
3461 DECLARE_CAST(StringSet)
3462};
Steve Blocka7e24c12009-10-30 11:49:00 +00003463
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003464template <typename Derived, typename Shape, typename Key>
3465class Dictionary: public HashTable<Derived, Shape, Key> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003466 typedef HashTable<Derived, Shape, Key> DerivedHashTable;
Steve Blocka7e24c12009-10-30 11:49:00 +00003467
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003468 public:
Steve Blocka7e24c12009-10-30 11:49:00 +00003469 // Returns the value at entry.
3470 Object* ValueAt(int entry) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003471 return this->get(Derived::EntryToIndex(entry) + 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00003472 }
3473
3474 // Set the value for entry.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003475 void ValueAtPut(int entry, Object* value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003476 this->set(Derived::EntryToIndex(entry) + 1, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003477 }
3478
3479 // Returns the property details for the property at entry.
3480 PropertyDetails DetailsAt(int entry) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003481 return Shape::DetailsAt(static_cast<Derived*>(this), entry);
Steve Blocka7e24c12009-10-30 11:49:00 +00003482 }
3483
3484 // Set the details for entry.
3485 void DetailsAtPut(int entry, PropertyDetails value) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003486 Shape::DetailsAtPut(static_cast<Derived*>(this), entry, value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003487 }
3488
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003489 // Returns true if property at given entry is deleted.
3490 bool IsDeleted(int entry) {
3491 return Shape::IsDeleted(static_cast<Derived*>(this), entry);
3492 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003493
3494 // Delete a property from the dictionary.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003495 static Handle<Object> DeleteProperty(Handle<Derived> dictionary, int entry);
Steve Blocka7e24c12009-10-30 11:49:00 +00003496
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003497 // Attempt to shrink the dictionary after deletion of key.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003498 MUST_USE_RESULT static inline Handle<Derived> Shrink(
3499 Handle<Derived> dictionary,
3500 Key key) {
3501 return DerivedHashTable::Shrink(dictionary, key);
3502 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003504 // Sorting support
3505 // TODO(dcarney): templatize or move to SeededNumberDictionary
3506 void CopyValuesTo(FixedArray* elements);
3507
Steve Blocka7e24c12009-10-30 11:49:00 +00003508 // Returns the number of elements in the dictionary filtering out properties
3509 // with the specified attributes.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003510 // TODO(jkummerow): Deprecated, only used by Object.observe.
3511 int NumberOfElementsFilterAttributes(PropertyFilter filter);
Steve Blocka7e24c12009-10-30 11:49:00 +00003512
3513 // Returns the number of enumerable elements in the dictionary.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003514 // TODO(jkummerow): Deprecated, only used by Object.observe.
3515 int NumberOfEnumElements() {
3516 return NumberOfElementsFilterAttributes(ENUMERABLE_STRINGS);
3517 }
Steve Blocka7e24c12009-10-30 11:49:00 +00003518
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003519 enum SortMode { UNSORTED, SORTED };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003520
Steve Blocka7e24c12009-10-30 11:49:00 +00003521 // Fill in details for properties into storage.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003522 // Returns the number of properties added.
3523 // TODO(jkummerow): Deprecated, only used by Object.observe.
3524 int CopyKeysTo(FixedArray* storage, int index, PropertyFilter filter,
3525 SortMode sort_mode);
3526 // Collect the keys into the given KeyAccumulator, in ascending chronological
3527 // order of property creation.
3528 static void CollectKeysTo(Handle<Dictionary<Derived, Shape, Key> > dictionary,
3529 KeyAccumulator* keys, PropertyFilter filter);
3530
3531 // Copies enumerable keys to preallocated fixed array.
3532 void CopyEnumKeysTo(FixedArray* storage);
Steve Blocka7e24c12009-10-30 11:49:00 +00003533
3534 // Accessors for next enumeration index.
3535 void SetNextEnumerationIndex(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003536 DCHECK(index != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003537 this->set(kNextEnumerationIndexIndex, Smi::FromInt(index));
Steve Blocka7e24c12009-10-30 11:49:00 +00003538 }
3539
3540 int NextEnumerationIndex() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003541 return Smi::cast(this->get(kNextEnumerationIndexIndex))->value();
Steve Blocka7e24c12009-10-30 11:49:00 +00003542 }
3543
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003544 // Creates a new dictionary.
3545 MUST_USE_RESULT static Handle<Derived> New(
3546 Isolate* isolate,
3547 int at_least_space_for,
3548 PretenureFlag pretenure = NOT_TENURED);
Steve Blocka7e24c12009-10-30 11:49:00 +00003549
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003550 // Ensures that a new dictionary is created when the capacity is checked.
3551 void SetRequiresCopyOnCapacityChange();
3552
Steve Blocka7e24c12009-10-30 11:49:00 +00003553 // Ensure enough space for n additional elements.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003554 static Handle<Derived> EnsureCapacity(Handle<Derived> obj, int n, Key key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003555
Ben Murdochb0fe1622011-05-05 13:52:32 +01003556#ifdef OBJECT_PRINT
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003557 void Print(std::ostream& os); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00003558#endif
3559 // Returns the key (slow).
3560 Object* SlowReverseLookup(Object* value);
3561
3562 // Sets the entry to (key, value) pair.
3563 inline void SetEntry(int entry,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003564 Handle<Object> key,
3565 Handle<Object> value);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003566 inline void SetEntry(int entry,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003567 Handle<Object> key,
3568 Handle<Object> value,
Steve Blocka7e24c12009-10-30 11:49:00 +00003569 PropertyDetails details);
3570
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003571 MUST_USE_RESULT static Handle<Derived> Add(
3572 Handle<Derived> dictionary,
3573 Key key,
3574 Handle<Object> value,
3575 PropertyDetails details);
Steve Blocka7e24c12009-10-30 11:49:00 +00003576
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003577 // Returns iteration indices array for the |dictionary|.
3578 // Values are direct indices in the |HashTable| array.
3579 static Handle<FixedArray> BuildIterationIndicesArray(
3580 Handle<Derived> dictionary);
3581
Steve Blocka7e24c12009-10-30 11:49:00 +00003582 protected:
3583 // Generic at put operation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003584 MUST_USE_RESULT static Handle<Derived> AtPut(
3585 Handle<Derived> dictionary,
3586 Key key,
3587 Handle<Object> value);
Steve Blocka7e24c12009-10-30 11:49:00 +00003588
3589 // Add entry to dictionary.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003590 static void AddEntry(
3591 Handle<Derived> dictionary,
3592 Key key,
3593 Handle<Object> value,
3594 PropertyDetails details,
3595 uint32_t hash);
Steve Blocka7e24c12009-10-30 11:49:00 +00003596
3597 // Generate new enumeration indices to avoid enumeration index overflow.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003598 // Returns iteration indices array for the |dictionary|.
3599 static Handle<FixedArray> GenerateNewEnumerationIndices(
3600 Handle<Derived> dictionary);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003601 static const int kMaxNumberKeyIndex = DerivedHashTable::kPrefixStartIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00003602 static const int kNextEnumerationIndexIndex = kMaxNumberKeyIndex + 1;
3603};
3604
3605
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003606template <typename Derived, typename Shape>
3607class NameDictionaryBase : public Dictionary<Derived, Shape, Handle<Name> > {
3608 typedef Dictionary<Derived, Shape, Handle<Name> > DerivedDictionary;
3609
3610 public:
3611 // Find entry for key, otherwise return kNotFound. Optimized version of
3612 // HashTable::FindEntry.
3613 int FindEntry(Handle<Name> key);
3614};
3615
3616
3617template <typename Key>
3618class BaseDictionaryShape : public BaseShape<Key> {
3619 public:
3620 template <typename Dictionary>
3621 static inline PropertyDetails DetailsAt(Dictionary* dict, int entry) {
3622 STATIC_ASSERT(Dictionary::kEntrySize == 3);
3623 DCHECK(entry >= 0); // Not found is -1, which is not caught by get().
3624 return PropertyDetails(
3625 Smi::cast(dict->get(Dictionary::EntryToIndex(entry) + 2)));
3626 }
3627
3628 template <typename Dictionary>
3629 static inline void DetailsAtPut(Dictionary* dict, int entry,
3630 PropertyDetails value) {
3631 STATIC_ASSERT(Dictionary::kEntrySize == 3);
3632 dict->set(Dictionary::EntryToIndex(entry) + 2, value.AsSmi());
3633 }
3634
3635 template <typename Dictionary>
3636 static bool IsDeleted(Dictionary* dict, int entry) {
3637 return false;
3638 }
3639
3640 template <typename Dictionary>
3641 static inline void SetEntry(Dictionary* dict, int entry, Handle<Object> key,
3642 Handle<Object> value, PropertyDetails details);
3643};
3644
3645
3646class NameDictionaryShape : public BaseDictionaryShape<Handle<Name> > {
Steve Blocka7e24c12009-10-30 11:49:00 +00003647 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003648 static inline bool IsMatch(Handle<Name> key, Object* other);
3649 static inline uint32_t Hash(Handle<Name> key);
3650 static inline uint32_t HashForObject(Handle<Name> key, Object* object);
3651 static inline Handle<Object> AsHandle(Isolate* isolate, Handle<Name> key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003652 static const int kPrefixSize = 2;
3653 static const int kEntrySize = 3;
3654 static const bool kIsEnumerable = true;
3655};
3656
3657
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003658class NameDictionary
3659 : public NameDictionaryBase<NameDictionary, NameDictionaryShape> {
3660 typedef NameDictionaryBase<NameDictionary, NameDictionaryShape>
3661 DerivedDictionary;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003662
Steve Blocka7e24c12009-10-30 11:49:00 +00003663 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003664 DECLARE_CAST(NameDictionary)
Steve Blocka7e24c12009-10-30 11:49:00 +00003665
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003666 inline static Handle<FixedArray> DoGenerateNewEnumerationIndices(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003667 Handle<NameDictionary> dictionary);
Steve Blocka7e24c12009-10-30 11:49:00 +00003668};
3669
3670
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003671class GlobalDictionaryShape : public NameDictionaryShape {
3672 public:
3673 static const int kEntrySize = 2; // Overrides NameDictionaryShape::kEntrySize
3674
3675 template <typename Dictionary>
3676 static inline PropertyDetails DetailsAt(Dictionary* dict, int entry);
3677
3678 template <typename Dictionary>
3679 static inline void DetailsAtPut(Dictionary* dict, int entry,
3680 PropertyDetails value);
3681
3682 template <typename Dictionary>
3683 static bool IsDeleted(Dictionary* dict, int entry);
3684
3685 template <typename Dictionary>
3686 static inline void SetEntry(Dictionary* dict, int entry, Handle<Object> key,
3687 Handle<Object> value, PropertyDetails details);
3688};
3689
3690
3691class GlobalDictionary
3692 : public NameDictionaryBase<GlobalDictionary, GlobalDictionaryShape> {
3693 public:
3694 DECLARE_CAST(GlobalDictionary)
3695};
3696
3697
3698class NumberDictionaryShape : public BaseDictionaryShape<uint32_t> {
Steve Blocka7e24c12009-10-30 11:49:00 +00003699 public:
3700 static inline bool IsMatch(uint32_t key, Object* other);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003701 static inline Handle<Object> AsHandle(Isolate* isolate, uint32_t key);
Steve Blocka7e24c12009-10-30 11:49:00 +00003702 static const int kEntrySize = 3;
3703 static const bool kIsEnumerable = false;
3704};
3705
3706
Ben Murdochc7cc0282012-03-05 14:35:55 +00003707class SeededNumberDictionaryShape : public NumberDictionaryShape {
Steve Blocka7e24c12009-10-30 11:49:00 +00003708 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +00003709 static const bool UsesSeed = true;
3710 static const int kPrefixSize = 2;
3711
3712 static inline uint32_t SeededHash(uint32_t key, uint32_t seed);
3713 static inline uint32_t SeededHashForObject(uint32_t key,
3714 uint32_t seed,
3715 Object* object);
3716};
3717
3718
3719class UnseededNumberDictionaryShape : public NumberDictionaryShape {
3720 public:
3721 static const int kPrefixSize = 0;
3722
3723 static inline uint32_t Hash(uint32_t key);
3724 static inline uint32_t HashForObject(uint32_t key, Object* object);
3725};
3726
3727
3728class SeededNumberDictionary
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003729 : public Dictionary<SeededNumberDictionary,
3730 SeededNumberDictionaryShape,
3731 uint32_t> {
Ben Murdochc7cc0282012-03-05 14:35:55 +00003732 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003733 DECLARE_CAST(SeededNumberDictionary)
Steve Blocka7e24c12009-10-30 11:49:00 +00003734
3735 // Type specific at put (default NONE attributes is used when adding).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003736 MUST_USE_RESULT static Handle<SeededNumberDictionary> AtNumberPut(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003737 Handle<SeededNumberDictionary> dictionary, uint32_t key,
3738 Handle<Object> value, bool used_as_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003739 MUST_USE_RESULT static Handle<SeededNumberDictionary> AddNumberEntry(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003740 Handle<SeededNumberDictionary> dictionary, uint32_t key,
3741 Handle<Object> value, PropertyDetails details, bool used_as_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00003742
3743 // Set an existing entry or add a new one if needed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003744 // Return the updated dictionary.
3745 MUST_USE_RESULT static Handle<SeededNumberDictionary> Set(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003746 Handle<SeededNumberDictionary> dictionary, uint32_t key,
3747 Handle<Object> value, PropertyDetails details, bool used_as_prototype);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003748
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003749 void UpdateMaxNumberKey(uint32_t key, bool used_as_prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00003750
Ben Murdochda12d292016-06-02 14:46:10 +01003751 // Returns true if the dictionary contains any elements that are non-writable,
3752 // non-configurable, non-enumerable, or have getters/setters.
3753 bool HasComplexElements();
3754
Steve Blocka7e24c12009-10-30 11:49:00 +00003755 // If slow elements are required we will never go back to fast-case
3756 // for the elements kept in this dictionary. We require slow
3757 // elements if an element has been added at an index larger than
3758 // kRequiresSlowElementsLimit or set_requires_slow_elements() has been called
3759 // when defining a getter or setter with a number key.
3760 inline bool requires_slow_elements();
3761 inline void set_requires_slow_elements();
3762
3763 // Get the value of the max number key that has been added to this
3764 // dictionary. max_number_key can only be called if
3765 // requires_slow_elements returns false.
3766 inline uint32_t max_number_key();
3767
Steve Blocka7e24c12009-10-30 11:49:00 +00003768 // Bit masks.
3769 static const int kRequiresSlowElementsMask = 1;
3770 static const int kRequiresSlowElementsTagSize = 1;
3771 static const uint32_t kRequiresSlowElementsLimit = (1 << 29) - 1;
3772};
3773
3774
Ben Murdochc7cc0282012-03-05 14:35:55 +00003775class UnseededNumberDictionary
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003776 : public Dictionary<UnseededNumberDictionary,
3777 UnseededNumberDictionaryShape,
3778 uint32_t> {
Ben Murdochc7cc0282012-03-05 14:35:55 +00003779 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003780 DECLARE_CAST(UnseededNumberDictionary)
Ben Murdochc7cc0282012-03-05 14:35:55 +00003781
3782 // Type specific at put (default NONE attributes is used when adding).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003783 MUST_USE_RESULT static Handle<UnseededNumberDictionary> AtNumberPut(
3784 Handle<UnseededNumberDictionary> dictionary,
3785 uint32_t key,
3786 Handle<Object> value);
3787 MUST_USE_RESULT static Handle<UnseededNumberDictionary> AddNumberEntry(
3788 Handle<UnseededNumberDictionary> dictionary,
3789 uint32_t key,
3790 Handle<Object> value);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003791
3792 // Set an existing entry or add a new one if needed.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003793 // Return the updated dictionary.
3794 MUST_USE_RESULT static Handle<UnseededNumberDictionary> Set(
3795 Handle<UnseededNumberDictionary> dictionary,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003796 uint32_t key,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003797 Handle<Object> value);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003798};
3799
3800
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003801class ObjectHashTableShape : public BaseShape<Handle<Object> > {
Ben Murdoch2b4ba112012-01-20 14:57:15 +00003802 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003803 static inline bool IsMatch(Handle<Object> key, Object* other);
3804 static inline uint32_t Hash(Handle<Object> key);
3805 static inline uint32_t HashForObject(Handle<Object> key, Object* object);
3806 static inline Handle<Object> AsHandle(Isolate* isolate, Handle<Object> key);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003807 static const int kPrefixSize = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003808 static const int kEntrySize = 2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003809};
3810
3811
3812// ObjectHashTable maps keys that are arbitrary objects to object values by
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003813// using the identity hash of the key for hashing purposes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003814class ObjectHashTable: public HashTable<ObjectHashTable,
3815 ObjectHashTableShape,
3816 Handle<Object> > {
3817 typedef HashTable<
3818 ObjectHashTable, ObjectHashTableShape, Handle<Object> > DerivedHashTable;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003819 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003820 DECLARE_CAST(ObjectHashTable)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003821
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003822 // Attempt to shrink hash table after removal of key.
3823 MUST_USE_RESULT static inline Handle<ObjectHashTable> Shrink(
3824 Handle<ObjectHashTable> table,
3825 Handle<Object> key);
3826
3827 // Looks up the value associated with the given key. The hole value is
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003828 // returned in case the key is not present.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003829 Object* Lookup(Handle<Object> key);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003830 Object* Lookup(Handle<Object> key, int32_t hash);
3831 Object* Lookup(Isolate* isolate, Handle<Object> key, int32_t hash);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003832
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003833 // Adds (or overwrites) the value associated with the given key.
3834 static Handle<ObjectHashTable> Put(Handle<ObjectHashTable> table,
3835 Handle<Object> key,
3836 Handle<Object> value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003837 static Handle<ObjectHashTable> Put(Handle<ObjectHashTable> table,
3838 Handle<Object> key, Handle<Object> value,
3839 int32_t hash);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003840
3841 // Returns an ObjectHashTable (possibly |table|) where |key| has been removed.
3842 static Handle<ObjectHashTable> Remove(Handle<ObjectHashTable> table,
3843 Handle<Object> key,
3844 bool* was_present);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003845 static Handle<ObjectHashTable> Remove(Handle<ObjectHashTable> table,
3846 Handle<Object> key, bool* was_present,
3847 int32_t hash);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003848
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003849 protected:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003850 friend class MarkCompactCollector;
3851
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003852 void AddEntry(int entry, Object* key, Object* value);
3853 void RemoveEntry(int entry);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003854
3855 // Returns the index to the value of an entry.
3856 static inline int EntryToValueIndex(int entry) {
3857 return EntryToIndex(entry) + 1;
3858 }
3859};
3860
3861
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003862// OrderedHashTable is a HashTable with Object keys that preserves
3863// insertion order. There are Map and Set interfaces (OrderedHashMap
3864// and OrderedHashTable, below). It is meant to be used by JSMap/JSSet.
3865//
3866// Only Object* keys are supported, with Object::SameValueZero() used as the
3867// equality operator and Object::GetHash() for the hash function.
3868//
3869// Based on the "Deterministic Hash Table" as described by Jason Orendorff at
3870// https://wiki.mozilla.org/User:Jorend/Deterministic_hash_tables
3871// Originally attributed to Tyler Close.
3872//
3873// Memory layout:
3874// [0]: bucket count
3875// [1]: element count
3876// [2]: deleted element count
3877// [3..(3 + NumberOfBuckets() - 1)]: "hash table", where each item is an
3878// offset into the data table (see below) where the
3879// first item in this bucket is stored.
3880// [3 + NumberOfBuckets()..length]: "data table", an array of length
3881// Capacity() * kEntrySize, where the first entrysize
3882// items are handled by the derived class and the
3883// item at kChainOffset is another entry into the
3884// data table indicating the next entry in this hash
3885// bucket.
3886//
3887// When we transition the table to a new version we obsolete it and reuse parts
3888// of the memory to store information how to transition an iterator to the new
3889// table:
3890//
3891// Memory layout for obsolete table:
3892// [0]: bucket count
3893// [1]: Next newer table
3894// [2]: Number of removed holes or -1 when the table was cleared.
3895// [3..(3 + NumberOfRemovedHoles() - 1)]: The indexes of the removed holes.
3896// [3 + NumberOfRemovedHoles()..length]: Not used
3897//
3898template<class Derived, class Iterator, int entrysize>
3899class OrderedHashTable: public FixedArray {
3900 public:
3901 // Returns an OrderedHashTable with a capacity of at least |capacity|.
3902 static Handle<Derived> Allocate(
3903 Isolate* isolate, int capacity, PretenureFlag pretenure = NOT_TENURED);
3904
3905 // Returns an OrderedHashTable (possibly |table|) with enough space
3906 // to add at least one new element.
3907 static Handle<Derived> EnsureGrowable(Handle<Derived> table);
3908
3909 // Returns an OrderedHashTable (possibly |table|) that's shrunken
3910 // if possible.
3911 static Handle<Derived> Shrink(Handle<Derived> table);
3912
3913 // Returns a new empty OrderedHashTable and records the clearing so that
3914 // exisiting iterators can be updated.
3915 static Handle<Derived> Clear(Handle<Derived> table);
3916
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003917 // Returns a true if the OrderedHashTable contains the key
3918 static bool HasKey(Handle<Derived> table, Handle<Object> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003919
3920 int NumberOfElements() {
3921 return Smi::cast(get(kNumberOfElementsIndex))->value();
3922 }
3923
3924 int NumberOfDeletedElements() {
3925 return Smi::cast(get(kNumberOfDeletedElementsIndex))->value();
3926 }
3927
3928 int UsedCapacity() { return NumberOfElements() + NumberOfDeletedElements(); }
3929
3930 int NumberOfBuckets() {
3931 return Smi::cast(get(kNumberOfBucketsIndex))->value();
3932 }
3933
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003934 // Returns an index into |this| for the given entry.
3935 int EntryToIndex(int entry) {
3936 return kHashTableStartIndex + NumberOfBuckets() + (entry * kEntrySize);
3937 }
3938
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003939 int HashToBucket(int hash) { return hash & (NumberOfBuckets() - 1); }
3940
3941 int HashToEntry(int hash) {
3942 int bucket = HashToBucket(hash);
3943 Object* entry = this->get(kHashTableStartIndex + bucket);
3944 return Smi::cast(entry)->value();
3945 }
3946
3947 int KeyToFirstEntry(Object* key) {
3948 Object* hash = key->GetHash();
3949 // If the object does not have an identity hash, it was never used as a key
3950 if (hash->IsUndefined()) return kNotFound;
3951 return HashToEntry(Smi::cast(hash)->value());
3952 }
3953
3954 int NextChainEntry(int entry) {
3955 Object* next_entry = get(EntryToIndex(entry) + kChainOffset);
3956 return Smi::cast(next_entry)->value();
3957 }
3958
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003959 Object* KeyAt(int entry) { return get(EntryToIndex(entry)); }
3960
3961 bool IsObsolete() {
3962 return !get(kNextTableIndex)->IsSmi();
3963 }
3964
3965 // The next newer table. This is only valid if the table is obsolete.
3966 Derived* NextTable() {
3967 return Derived::cast(get(kNextTableIndex));
3968 }
3969
3970 // When the table is obsolete we store the indexes of the removed holes.
3971 int RemovedIndexAt(int index) {
3972 return Smi::cast(get(kRemovedHolesIndex + index))->value();
3973 }
3974
3975 static const int kNotFound = -1;
3976 static const int kMinCapacity = 4;
3977
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003978 static const int kNumberOfBucketsIndex = 0;
3979 static const int kNumberOfElementsIndex = kNumberOfBucketsIndex + 1;
3980 static const int kNumberOfDeletedElementsIndex = kNumberOfElementsIndex + 1;
3981 static const int kHashTableStartIndex = kNumberOfDeletedElementsIndex + 1;
3982 static const int kNextTableIndex = kNumberOfElementsIndex;
3983
3984 static const int kNumberOfBucketsOffset =
3985 kHeaderSize + kNumberOfBucketsIndex * kPointerSize;
3986 static const int kNumberOfElementsOffset =
3987 kHeaderSize + kNumberOfElementsIndex * kPointerSize;
3988 static const int kNumberOfDeletedElementsOffset =
3989 kHeaderSize + kNumberOfDeletedElementsIndex * kPointerSize;
3990 static const int kHashTableStartOffset =
3991 kHeaderSize + kHashTableStartIndex * kPointerSize;
3992 static const int kNextTableOffset =
3993 kHeaderSize + kNextTableIndex * kPointerSize;
3994
3995 static const int kEntrySize = entrysize + 1;
3996 static const int kChainOffset = entrysize;
3997
3998 static const int kLoadFactor = 2;
3999
4000 // NumberOfDeletedElements is set to kClearedTableSentinel when
4001 // the table is cleared, which allows iterator transitions to
4002 // optimize that case.
4003 static const int kClearedTableSentinel = -1;
4004
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004005 protected:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004006 static Handle<Derived> Rehash(Handle<Derived> table, int new_capacity);
4007
4008 void SetNumberOfBuckets(int num) {
4009 set(kNumberOfBucketsIndex, Smi::FromInt(num));
4010 }
4011
4012 void SetNumberOfElements(int num) {
4013 set(kNumberOfElementsIndex, Smi::FromInt(num));
4014 }
4015
4016 void SetNumberOfDeletedElements(int num) {
4017 set(kNumberOfDeletedElementsIndex, Smi::FromInt(num));
4018 }
4019
4020 int Capacity() {
4021 return NumberOfBuckets() * kLoadFactor;
4022 }
4023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004024 void SetNextTable(Derived* next_table) {
4025 set(kNextTableIndex, next_table);
4026 }
4027
4028 void SetRemovedIndexAt(int index, int removed_index) {
4029 return set(kRemovedHolesIndex + index, Smi::FromInt(removed_index));
4030 }
4031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004032 static const int kRemovedHolesIndex = kHashTableStartIndex;
4033
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004034 static const int kMaxCapacity =
4035 (FixedArray::kMaxLength - kHashTableStartIndex)
4036 / (1 + (kEntrySize * kLoadFactor));
4037};
4038
4039
4040class JSSetIterator;
4041
4042
4043class OrderedHashSet: public OrderedHashTable<
4044 OrderedHashSet, JSSetIterator, 1> {
4045 public:
4046 DECLARE_CAST(OrderedHashSet)
4047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004048 static Handle<OrderedHashSet> Add(Handle<OrderedHashSet> table,
4049 Handle<Object> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004050};
4051
4052
4053class JSMapIterator;
4054
4055
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004056class OrderedHashMap
4057 : public OrderedHashTable<OrderedHashMap, JSMapIterator, 2> {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004058 public:
4059 DECLARE_CAST(OrderedHashMap)
4060
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004061 inline Object* ValueAt(int entry);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004062
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004063 static const int kValueOffset = 1;
4064};
4065
4066
4067template <int entrysize>
4068class WeakHashTableShape : public BaseShape<Handle<Object> > {
4069 public:
4070 static inline bool IsMatch(Handle<Object> key, Object* other);
4071 static inline uint32_t Hash(Handle<Object> key);
4072 static inline uint32_t HashForObject(Handle<Object> key, Object* object);
4073 static inline Handle<Object> AsHandle(Isolate* isolate, Handle<Object> key);
4074 static const int kPrefixSize = 0;
4075 static const int kEntrySize = entrysize;
4076};
4077
4078
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004079// WeakHashTable maps keys that are arbitrary heap objects to heap object
4080// values. The table wraps the keys in weak cells and store values directly.
4081// Thus it references keys weakly and values strongly.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004082class WeakHashTable: public HashTable<WeakHashTable,
4083 WeakHashTableShape<2>,
4084 Handle<Object> > {
4085 typedef HashTable<
4086 WeakHashTable, WeakHashTableShape<2>, Handle<Object> > DerivedHashTable;
4087 public:
4088 DECLARE_CAST(WeakHashTable)
4089
4090 // Looks up the value associated with the given key. The hole value is
4091 // returned in case the key is not present.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004092 Object* Lookup(Handle<HeapObject> key);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004093
4094 // Adds (or overwrites) the value associated with the given key. Mapping a
4095 // key to the hole value causes removal of the whole entry.
4096 MUST_USE_RESULT static Handle<WeakHashTable> Put(Handle<WeakHashTable> table,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004097 Handle<HeapObject> key,
4098 Handle<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004099
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004100 static Handle<FixedArray> GetValues(Handle<WeakHashTable> table);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004101
4102 private:
4103 friend class MarkCompactCollector;
4104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004105 void AddEntry(int entry, Handle<WeakCell> key, Handle<HeapObject> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004106
4107 // Returns the index to the value of an entry.
4108 static inline int EntryToValueIndex(int entry) {
4109 return EntryToIndex(entry) + 1;
4110 }
4111};
4112
4113
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004114// ScopeInfo represents information about different scopes of a source
4115// program and the allocation of the scope's variables. Scope information
4116// is stored in a compressed form in ScopeInfo objects and is used
4117// at runtime (stack dumps, deoptimization, etc.).
4118
4119// This object provides quick access to scope info details for runtime
4120// routines.
4121class ScopeInfo : public FixedArray {
4122 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004123 DECLARE_CAST(ScopeInfo)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004124
4125 // Return the type of this scope.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004126 ScopeType scope_type();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004127
4128 // Does this scope call eval?
4129 bool CallsEval();
4130
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004131 // Return the language mode of this scope.
4132 LanguageMode language_mode();
4133
4134 // True if this scope is a (var) declaration scope.
4135 bool is_declaration_scope();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004136
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004137 // Does this scope make a sloppy eval call?
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004138 bool CallsSloppyEval() { return CallsEval() && is_sloppy(language_mode()); }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004139
4140 // Return the total number of locals allocated on the stack and in the
4141 // context. This includes the parameters that are allocated in the context.
4142 int LocalCount();
4143
4144 // Return the number of stack slots for code. This number consists of two
4145 // parts:
4146 // 1. One stack slot per stack allocated local.
4147 // 2. One stack slot for the function name if it is stack allocated.
4148 int StackSlotCount();
4149
4150 // Return the number of context slots for code if a context is allocated. This
4151 // number consists of three parts:
4152 // 1. Size of fixed header for every context: Context::MIN_CONTEXT_SLOTS
4153 // 2. One context slot per context allocated local.
4154 // 3. One context slot for the function name if it is context allocated.
4155 // Parameters allocated in the context count as context allocated locals. If
4156 // no contexts are allocated for this scope ContextLength returns 0.
4157 int ContextLength();
4158
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004159 // Does this scope declare a "this" binding?
4160 bool HasReceiver();
4161
4162 // Does this scope declare a "this" binding, and the "this" binding is stack-
4163 // or context-allocated?
4164 bool HasAllocatedReceiver();
4165
4166 // Does this scope declare a "new.target" binding?
4167 bool HasNewTarget();
4168
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004169 // Is this scope the scope of a named function expression?
4170 bool HasFunctionName();
4171
4172 // Return if this has context allocated locals.
4173 bool HasHeapAllocatedLocals();
4174
4175 // Return if contexts are allocated for this scope.
4176 bool HasContext();
4177
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004178 // Return if this is a function scope with "use asm".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004179 inline bool IsAsmModule();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004180
4181 // Return if this is a nested function within an asm module scope.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004182 inline bool IsAsmFunction();
4183
4184 inline bool HasSimpleParameters();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004185
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004186 // Return the function_name if present.
4187 String* FunctionName();
4188
4189 // Return the name of the given parameter.
4190 String* ParameterName(int var);
4191
4192 // Return the name of the given local.
4193 String* LocalName(int var);
4194
4195 // Return the name of the given stack local.
4196 String* StackLocalName(int var);
4197
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004198 // Return the name of the given stack local.
4199 int StackLocalIndex(int var);
4200
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004201 // Return the name of the given context local.
4202 String* ContextLocalName(int var);
4203
4204 // Return the mode of the given context local.
4205 VariableMode ContextLocalMode(int var);
4206
4207 // Return the initialization flag of the given context local.
4208 InitializationFlag ContextLocalInitFlag(int var);
4209
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004210 // Return the initialization flag of the given context local.
4211 MaybeAssignedFlag ContextLocalMaybeAssignedFlag(int var);
4212
4213 // Return true if this local was introduced by the compiler, and should not be
4214 // exposed to the user in a debugger.
4215 bool LocalIsSynthetic(int var);
4216
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004217 // Lookup support for serialized scope info. Returns the
4218 // the stack slot index for a given slot name if the slot is
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004219 // present; otherwise returns a value < 0. The name must be an internalized
4220 // string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004221 int StackSlotIndex(String* name);
4222
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004223 // Lookup support for serialized scope info. Returns the local context slot
4224 // index for a given slot name if the slot is present; otherwise
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004225 // returns a value < 0. The name must be an internalized string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004226 // If the slot is present and mode != NULL, sets *mode to the corresponding
4227 // mode for that variable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004228 static int ContextSlotIndex(Handle<ScopeInfo> scope_info, Handle<String> name,
4229 VariableMode* mode, InitializationFlag* init_flag,
4230 MaybeAssignedFlag* maybe_assigned_flag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004231
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004232 // Similar to ContextSlotIndex() but this method searches only among
4233 // global slots of the serialized scope info. Returns the context slot index
4234 // for a given slot name if the slot is present; otherwise returns a
4235 // value < 0. The name must be an internalized string. If the slot is present
4236 // and mode != NULL, sets *mode to the corresponding mode for that variable.
4237 static int ContextGlobalSlotIndex(Handle<ScopeInfo> scope_info,
4238 Handle<String> name, VariableMode* mode,
4239 InitializationFlag* init_flag,
4240 MaybeAssignedFlag* maybe_assigned_flag);
4241
4242 // Lookup the name of a certain context slot by its index.
4243 String* ContextSlotName(int slot_index);
4244
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004245 // Lookup support for serialized scope info. Returns the
4246 // parameter index for a given parameter name if the parameter is present;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004247 // otherwise returns a value < 0. The name must be an internalized string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004248 int ParameterIndex(String* name);
4249
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004250 // Lookup support for serialized scope info. Returns the function context
4251 // slot index if the function name is present and context-allocated (named
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004252 // function expressions, only), otherwise returns a value < 0. The name
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004253 // must be an internalized string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004254 int FunctionContextSlotIndex(String* name, VariableMode* mode);
4255
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004256 // Lookup support for serialized scope info. Returns the receiver context
4257 // slot index if scope has a "this" binding, and the binding is
4258 // context-allocated. Otherwise returns a value < 0.
4259 int ReceiverContextSlotIndex();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004260
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004261 FunctionKind function_kind();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004262
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004263 static Handle<ScopeInfo> Create(Isolate* isolate, Zone* zone, Scope* scope);
4264 static Handle<ScopeInfo> CreateGlobalThisBinding(Isolate* isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004265
4266 // Serializes empty scope info.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004267 static ScopeInfo* Empty(Isolate* isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004268
4269#ifdef DEBUG
4270 void Print();
4271#endif
4272
4273 // The layout of the static part of a ScopeInfo is as follows. Each entry is
4274 // numeric and occupies one array slot.
4275 // 1. A set of properties of the scope
4276 // 2. The number of parameters. This only applies to function scopes. For
4277 // non-function scopes this is 0.
4278 // 3. The number of non-parameter variables allocated on the stack.
4279 // 4. The number of non-parameter and parameter variables allocated in the
4280 // context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004281#define FOR_EACH_SCOPE_INFO_NUMERIC_FIELD(V) \
4282 V(Flags) \
4283 V(ParameterCount) \
4284 V(StackLocalCount) \
4285 V(ContextLocalCount) \
Ben Murdoch097c5b22016-05-18 11:27:45 +01004286 V(ContextGlobalCount)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004287
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004288#define FIELD_ACCESSORS(name) \
4289 inline void Set##name(int value); \
4290 inline int name();
4291 FOR_EACH_SCOPE_INFO_NUMERIC_FIELD(FIELD_ACCESSORS)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004292#undef FIELD_ACCESSORS
4293
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004294 enum {
4295#define DECL_INDEX(name) k##name,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004296 FOR_EACH_SCOPE_INFO_NUMERIC_FIELD(DECL_INDEX)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004297#undef DECL_INDEX
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004298 kVariablePartIndex
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004299 };
4300
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004301 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004302 // The layout of the variable part of a ScopeInfo is as follows:
4303 // 1. ParameterEntries:
4304 // This part stores the names of the parameters for function scopes. One
4305 // slot is used per parameter, so in total this part occupies
4306 // ParameterCount() slots in the array. For other scopes than function
4307 // scopes ParameterCount() is 0.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004308 // 2. StackLocalFirstSlot:
4309 // Index of a first stack slot for stack local. Stack locals belonging to
4310 // this scope are located on a stack at slots starting from this index.
4311 // 3. StackLocalEntries:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004312 // Contains the names of local variables that are allocated on the stack,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004313 // in increasing order of the stack slot index. First local variable has
4314 // a stack slot index defined in StackLocalFirstSlot (point 2 above).
4315 // One slot is used per stack local, so in total this part occupies
4316 // StackLocalCount() slots in the array.
4317 // 4. ContextLocalNameEntries:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004318 // Contains the names of local variables and parameters that are allocated
4319 // in the context. They are stored in increasing order of the context slot
4320 // index starting with Context::MIN_CONTEXT_SLOTS. One slot is used per
4321 // context local, so in total this part occupies ContextLocalCount() slots
4322 // in the array.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004323 // 5. ContextLocalInfoEntries:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004324 // Contains the variable modes and initialization flags corresponding to
4325 // the context locals in ContextLocalNameEntries. One slot is used per
4326 // context local, so in total this part occupies ContextLocalCount()
4327 // slots in the array.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004328 // 6. RecieverEntryIndex:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004329 // If the scope binds a "this" value, one slot is reserved to hold the
4330 // context or stack slot index for the variable.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004331 // 7. FunctionNameEntryIndex:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004332 // If the scope belongs to a named function expression this part contains
4333 // information about the function variable. It always occupies two array
4334 // slots: a. The name of the function variable.
4335 // b. The context or stack slot index for the variable.
4336 int ParameterEntriesIndex();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004337 int StackLocalFirstSlotIndex();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004338 int StackLocalEntriesIndex();
4339 int ContextLocalNameEntriesIndex();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004340 int ContextGlobalNameEntriesIndex();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004341 int ContextLocalInfoEntriesIndex();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004342 int ContextGlobalInfoEntriesIndex();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004343 int ReceiverEntryIndex();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004344 int FunctionNameEntryIndex();
4345
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004346 int Lookup(Handle<String> name, int start, int end, VariableMode* mode,
4347 VariableLocation* location, InitializationFlag* init_flag,
4348 MaybeAssignedFlag* maybe_assigned_flag);
4349
4350 // Used for the function name variable for named function expressions, and for
4351 // the receiver.
4352 enum VariableAllocationInfo { NONE, STACK, CONTEXT, UNUSED };
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004353
4354 // Properties of scopes.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004355 class ScopeTypeField : public BitField<ScopeType, 0, 4> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004356 class CallsEvalField : public BitField<bool, ScopeTypeField::kNext, 1> {};
4357 STATIC_ASSERT(LANGUAGE_END == 3);
4358 class LanguageModeField
4359 : public BitField<LanguageMode, CallsEvalField::kNext, 2> {};
4360 class DeclarationScopeField
4361 : public BitField<bool, LanguageModeField::kNext, 1> {};
4362 class ReceiverVariableField
4363 : public BitField<VariableAllocationInfo, DeclarationScopeField::kNext,
4364 2> {};
4365 class HasNewTargetField
4366 : public BitField<bool, ReceiverVariableField::kNext, 1> {};
4367 class FunctionVariableField
4368 : public BitField<VariableAllocationInfo, HasNewTargetField::kNext, 2> {};
4369 class FunctionVariableMode
4370 : public BitField<VariableMode, FunctionVariableField::kNext, 3> {};
4371 class AsmModuleField : public BitField<bool, FunctionVariableMode::kNext, 1> {
4372 };
4373 class AsmFunctionField : public BitField<bool, AsmModuleField::kNext, 1> {};
4374 class HasSimpleParametersField
4375 : public BitField<bool, AsmFunctionField::kNext, 1> {};
4376 class FunctionKindField
4377 : public BitField<FunctionKind, HasSimpleParametersField::kNext, 8> {};
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004378
4379 // BitFields representing the encoded information for context locals in the
4380 // ContextLocalInfoEntries part.
4381 class ContextLocalMode: public BitField<VariableMode, 0, 3> {};
4382 class ContextLocalInitFlag: public BitField<InitializationFlag, 3, 1> {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004383 class ContextLocalMaybeAssignedFlag
4384 : public BitField<MaybeAssignedFlag, 4, 1> {};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004385
4386 friend class ScopeIterator;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004387};
4388
4389
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004390// The cache for maps used by normalized (dictionary mode) objects.
4391// Such maps do not have property descriptors, so a typical program
4392// needs very limited number of distinct normalized maps.
4393class NormalizedMapCache: public FixedArray {
4394 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004395 static Handle<NormalizedMapCache> New(Isolate* isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004396
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004397 MUST_USE_RESULT MaybeHandle<Map> Get(Handle<Map> fast_map,
4398 PropertyNormalizationMode mode);
4399 void Set(Handle<Map> fast_map, Handle<Map> normalized_map);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004400
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004401 void Clear();
4402
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004403 DECLARE_CAST(NormalizedMapCache)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004404
Ben Murdoch097c5b22016-05-18 11:27:45 +01004405 static inline bool IsNormalizedMapCache(const HeapObject* obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004406
4407 DECLARE_VERIFIER(NormalizedMapCache)
4408 private:
4409 static const int kEntries = 64;
4410
4411 static inline int GetIndex(Handle<Map> map);
4412
4413 // The following declarations hide base class methods.
4414 Object* get(int index);
4415 void set(int index, Object* value);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004416};
4417
4418
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004419// ByteArray represents fixed sized byte arrays. Used for the relocation info
4420// that is attached to code objects.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004421class ByteArray: public FixedArrayBase {
Steve Blocka7e24c12009-10-30 11:49:00 +00004422 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004423 inline int Size();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004424
Steve Blocka7e24c12009-10-30 11:49:00 +00004425 // Setter and getter.
4426 inline byte get(int index);
4427 inline void set(int index, byte value);
4428
4429 // Treat contents as an int array.
4430 inline int get_int(int index);
4431
4432 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004433 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
Steve Blocka7e24c12009-10-30 11:49:00 +00004434 }
4435 // We use byte arrays for free blocks in the heap. Given a desired size in
4436 // bytes that is a multiple of the word size and big enough to hold a byte
4437 // array, this function returns the number of elements a byte array should
4438 // have.
4439 static int LengthFor(int size_in_bytes) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004440 DCHECK(IsAligned(size_in_bytes, kPointerSize));
4441 DCHECK(size_in_bytes >= kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004442 return size_in_bytes - kHeaderSize;
4443 }
4444
4445 // Returns data start address.
4446 inline Address GetDataStartAddress();
4447
4448 // Returns a pointer to the ByteArray object for a given data start address.
4449 static inline ByteArray* FromDataStartAddress(Address address);
4450
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004451 DECLARE_CAST(ByteArray)
Steve Blocka7e24c12009-10-30 11:49:00 +00004452
4453 // Dispatched behavior.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004454 inline int ByteArraySize();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004455 DECLARE_PRINTER(ByteArray)
4456 DECLARE_VERIFIER(ByteArray)
Steve Blocka7e24c12009-10-30 11:49:00 +00004457
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004458 // Layout description.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01004459 static const int kAlignedSize = OBJECT_POINTER_ALIGN(kHeaderSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00004460
Leon Clarkee46be812010-01-19 14:06:41 +00004461 // Maximal memory consumption for a single ByteArray.
4462 static const int kMaxSize = 512 * MB;
4463 // Maximal length of a single ByteArray.
4464 static const int kMaxLength = kMaxSize - kHeaderSize;
4465
Steve Blocka7e24c12009-10-30 11:49:00 +00004466 private:
4467 DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArray);
4468};
4469
4470
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004471// BytecodeArray represents a sequence of interpreter bytecodes.
4472class BytecodeArray : public FixedArrayBase {
4473 public:
4474 static int SizeFor(int length) {
4475 return OBJECT_POINTER_ALIGN(kHeaderSize + length);
4476 }
4477
4478 // Setter and getter
4479 inline byte get(int index);
4480 inline void set(int index, byte value);
4481
4482 // Returns data start address.
4483 inline Address GetFirstBytecodeAddress();
4484
4485 // Accessors for frame size.
4486 inline int frame_size() const;
4487 inline void set_frame_size(int frame_size);
4488
4489 // Accessor for register count (derived from frame_size).
4490 inline int register_count() const;
4491
4492 // Accessors for parameter count (including implicit 'this' receiver).
4493 inline int parameter_count() const;
4494 inline void set_parameter_count(int number_of_parameters);
4495
Ben Murdoch097c5b22016-05-18 11:27:45 +01004496 // Accessors for profiling count.
4497 inline int interrupt_budget() const;
4498 inline void set_interrupt_budget(int interrupt_budget);
4499
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004500 // Accessors for the constant pool.
4501 DECL_ACCESSORS(constant_pool, FixedArray)
4502
Ben Murdoch097c5b22016-05-18 11:27:45 +01004503 // Accessors for handler table containing offsets of exception handlers.
4504 DECL_ACCESSORS(handler_table, FixedArray)
4505
4506 // Accessors for source position table containing mappings between byte code
4507 // offset and source position.
Ben Murdochda12d292016-06-02 14:46:10 +01004508 DECL_ACCESSORS(source_position_table, ByteArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +01004509
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004510 DECLARE_CAST(BytecodeArray)
4511
4512 // Dispatched behavior.
4513 inline int BytecodeArraySize();
4514
Ben Murdoch097c5b22016-05-18 11:27:45 +01004515 inline int instruction_size();
4516
4517 int SourcePosition(int offset);
4518 int SourceStatementPosition(int offset);
4519
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004520 DECLARE_PRINTER(BytecodeArray)
4521 DECLARE_VERIFIER(BytecodeArray)
4522
4523 void Disassemble(std::ostream& os);
4524
Ben Murdoch097c5b22016-05-18 11:27:45 +01004525 void CopyBytecodesTo(BytecodeArray* to);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004526
Ben Murdoch097c5b22016-05-18 11:27:45 +01004527 // Layout description.
4528 static const int kConstantPoolOffset = FixedArrayBase::kHeaderSize;
4529 static const int kHandlerTableOffset = kConstantPoolOffset + kPointerSize;
4530 static const int kSourcePositionTableOffset =
4531 kHandlerTableOffset + kPointerSize;
4532 static const int kFrameSizeOffset = kSourcePositionTableOffset + kPointerSize;
4533 static const int kParameterSizeOffset = kFrameSizeOffset + kIntSize;
4534 static const int kInterruptBudgetOffset = kParameterSizeOffset + kIntSize;
4535 static const int kHeaderSize = kInterruptBudgetOffset + kIntSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004536
4537 // Maximal memory consumption for a single BytecodeArray.
4538 static const int kMaxSize = 512 * MB;
4539 // Maximal length of a single BytecodeArray.
4540 static const int kMaxLength = kMaxSize - kHeaderSize;
4541
4542 class BodyDescriptor;
4543
4544 private:
4545 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArray);
4546};
4547
4548
4549// FreeSpace are fixed-size free memory blocks used by the heap and GC.
4550// They look like heap objects (are heap object tagged and have a map) so that
4551// the heap remains iterable. They have a size and a next pointer.
4552// The next pointer is the raw address of the next FreeSpace object (or NULL)
4553// in the free list.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004554class FreeSpace: public HeapObject {
4555 public:
4556 // [size]: size of the free space including the header.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004557 inline int size() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004558 inline void set_size(int value);
4559
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004560 inline int nobarrier_size() const;
4561 inline void nobarrier_set_size(int value);
4562
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004563 inline int Size();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004564
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004565 // Accessors for the next field.
4566 inline FreeSpace* next();
4567 inline void set_next(FreeSpace* next);
4568
4569 inline static FreeSpace* cast(HeapObject* obj);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004570
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004571 // Dispatched behavior.
4572 DECLARE_PRINTER(FreeSpace)
4573 DECLARE_VERIFIER(FreeSpace)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004574
4575 // Layout description.
4576 // Size is smi tagged when it is stored.
4577 static const int kSizeOffset = HeapObject::kHeaderSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004578 static const int kNextOffset = POINTER_SIZE_ALIGN(kSizeOffset + kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004579
4580 private:
4581 DISALLOW_IMPLICIT_CONSTRUCTORS(FreeSpace);
4582};
4583
4584
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004585// V has parameters (Type, type, TYPE, C type, element_size)
4586#define TYPED_ARRAYS(V) \
4587 V(Uint8, uint8, UINT8, uint8_t, 1) \
4588 V(Int8, int8, INT8, int8_t, 1) \
4589 V(Uint16, uint16, UINT16, uint16_t, 2) \
4590 V(Int16, int16, INT16, int16_t, 2) \
4591 V(Uint32, uint32, UINT32, uint32_t, 4) \
4592 V(Int32, int32, INT32, int32_t, 4) \
4593 V(Float32, float32, FLOAT32, float, 4) \
4594 V(Float64, float64, FLOAT64, double, 8) \
4595 V(Uint8Clamped, uint8_clamped, UINT8_CLAMPED, uint8_t, 1)
4596
4597
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004598class FixedTypedArrayBase: public FixedArrayBase {
4599 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004600 // [base_pointer]: Either points to the FixedTypedArrayBase itself or nullptr.
4601 DECL_ACCESSORS(base_pointer, Object)
4602
4603 // [external_pointer]: Contains the offset between base_pointer and the start
4604 // of the data. If the base_pointer is a nullptr, the external_pointer
4605 // therefore points to the actual backing store.
4606 DECL_ACCESSORS(external_pointer, void)
4607
4608 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004609 DECLARE_CAST(FixedTypedArrayBase)
4610
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004611 static const int kBasePointerOffset = FixedArrayBase::kHeaderSize;
4612 static const int kExternalPointerOffset = kBasePointerOffset + kPointerSize;
4613 static const int kHeaderSize =
4614 DOUBLE_POINTER_ALIGN(kExternalPointerOffset + kPointerSize);
4615
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004616 static const int kDataOffset = kHeaderSize;
4617
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004618 class BodyDescriptor;
4619
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004620 inline int size();
4621
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004622 static inline int TypedArraySize(InstanceType type, int length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004623 inline int TypedArraySize(InstanceType type);
4624
4625 // Use with care: returns raw pointer into heap.
4626 inline void* DataPtr();
4627
4628 inline int DataSize();
4629
4630 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004631 static inline int ElementSize(InstanceType type);
4632
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004633 inline int DataSize(InstanceType type);
4634
4635 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedTypedArrayBase);
4636};
4637
4638
4639template <class Traits>
4640class FixedTypedArray: public FixedTypedArrayBase {
4641 public:
4642 typedef typename Traits::ElementType ElementType;
4643 static const InstanceType kInstanceType = Traits::kInstanceType;
4644
4645 DECLARE_CAST(FixedTypedArray<Traits>)
4646
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004647 inline ElementType get_scalar(int index);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004648 static inline Handle<Object> get(FixedTypedArray* array, int index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004649 inline void set(int index, ElementType value);
4650
4651 static inline ElementType from_int(int value);
4652 static inline ElementType from_double(double value);
4653
4654 // This accessor applies the correct conversion from Smi, HeapNumber
4655 // and undefined.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004656 inline void SetValue(uint32_t index, Object* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004657
4658 DECLARE_PRINTER(FixedTypedArray)
4659 DECLARE_VERIFIER(FixedTypedArray)
4660
4661 private:
4662 DISALLOW_IMPLICIT_CONSTRUCTORS(FixedTypedArray);
4663};
4664
4665#define FIXED_TYPED_ARRAY_TRAITS(Type, type, TYPE, elementType, size) \
4666 class Type##ArrayTraits { \
4667 public: /* NOLINT */ \
4668 typedef elementType ElementType; \
4669 static const InstanceType kInstanceType = FIXED_##TYPE##_ARRAY_TYPE; \
4670 static const char* Designator() { return #type " array"; } \
4671 static inline Handle<Object> ToHandle(Isolate* isolate, \
4672 elementType scalar); \
4673 static inline elementType defaultValue(); \
4674 }; \
4675 \
4676 typedef FixedTypedArray<Type##ArrayTraits> Fixed##Type##Array;
4677
4678TYPED_ARRAYS(FIXED_TYPED_ARRAY_TRAITS)
4679
4680#undef FIXED_TYPED_ARRAY_TRAITS
4681
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004682
Ben Murdochb0fe1622011-05-05 13:52:32 +01004683// DeoptimizationInputData is a fixed array used to hold the deoptimization
4684// data for code generated by the Hydrogen/Lithium compiler. It also
4685// contains information about functions that were inlined. If N different
4686// functions were inlined then first N elements of the literal array will
4687// contain these functions.
4688//
4689// It can be empty.
4690class DeoptimizationInputData: public FixedArray {
4691 public:
4692 // Layout description. Indices in the array.
4693 static const int kTranslationByteArrayIndex = 0;
4694 static const int kInlinedFunctionCountIndex = 1;
4695 static const int kLiteralArrayIndex = 2;
4696 static const int kOsrAstIdIndex = 3;
4697 static const int kOsrPcOffsetIndex = 4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004698 static const int kOptimizationIdIndex = 5;
4699 static const int kSharedFunctionInfoIndex = 6;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004700 static const int kWeakCellCacheIndex = 7;
4701 static const int kFirstDeoptEntryIndex = 8;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004702
4703 // Offsets of deopt entry elements relative to the start of the entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004704 static const int kAstIdRawOffset = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004705 static const int kTranslationIndexOffset = 1;
4706 static const int kArgumentsStackHeightOffset = 2;
Ben Murdoch2b4ba112012-01-20 14:57:15 +00004707 static const int kPcOffset = 3;
4708 static const int kDeoptEntrySize = 4;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004709
4710 // Simple element accessors.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004711#define DECLARE_ELEMENT_ACCESSORS(name, type) \
4712 inline type* name(); \
4713 inline void Set##name(type* value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004714
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004715 DECLARE_ELEMENT_ACCESSORS(TranslationByteArray, ByteArray)
4716 DECLARE_ELEMENT_ACCESSORS(InlinedFunctionCount, Smi)
4717 DECLARE_ELEMENT_ACCESSORS(LiteralArray, FixedArray)
4718 DECLARE_ELEMENT_ACCESSORS(OsrAstId, Smi)
4719 DECLARE_ELEMENT_ACCESSORS(OsrPcOffset, Smi)
4720 DECLARE_ELEMENT_ACCESSORS(OptimizationId, Smi)
4721 DECLARE_ELEMENT_ACCESSORS(SharedFunctionInfo, Object)
4722 DECLARE_ELEMENT_ACCESSORS(WeakCellCache, Object)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004723
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004724#undef DECLARE_ELEMENT_ACCESSORS
Ben Murdochb0fe1622011-05-05 13:52:32 +01004725
4726 // Accessors for elements of the ith deoptimization entry.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004727#define DECLARE_ENTRY_ACCESSORS(name, type) \
4728 inline type* name(int i); \
4729 inline void Set##name(int i, type* value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004730
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004731 DECLARE_ENTRY_ACCESSORS(AstIdRaw, Smi)
4732 DECLARE_ENTRY_ACCESSORS(TranslationIndex, Smi)
4733 DECLARE_ENTRY_ACCESSORS(ArgumentsStackHeight, Smi)
4734 DECLARE_ENTRY_ACCESSORS(Pc, Smi)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004735
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004736#undef DECLARE_ENTRY_ACCESSORS
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004737
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004738 inline BailoutId AstId(int i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004739
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004740 inline void SetAstId(int i, BailoutId value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004741
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004742 inline int DeoptCount();
Ben Murdochb0fe1622011-05-05 13:52:32 +01004743
4744 // Allocates a DeoptimizationInputData.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004745 static Handle<DeoptimizationInputData> New(Isolate* isolate,
4746 int deopt_entry_count,
4747 PretenureFlag pretenure);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004748
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004749 DECLARE_CAST(DeoptimizationInputData)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004750
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004751#ifdef ENABLE_DISASSEMBLER
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004752 void DeoptimizationInputDataPrint(std::ostream& os); // NOLINT
Ben Murdochb0fe1622011-05-05 13:52:32 +01004753#endif
4754
4755 private:
4756 static int IndexForEntry(int i) {
4757 return kFirstDeoptEntryIndex + (i * kDeoptEntrySize);
4758 }
4759
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004760
4761 static int LengthFor(int entry_count) { return IndexForEntry(entry_count); }
Ben Murdochb0fe1622011-05-05 13:52:32 +01004762};
4763
4764
4765// DeoptimizationOutputData is a fixed array used to hold the deoptimization
4766// data for code generated by the full compiler.
4767// The format of the these objects is
4768// [i * 2]: Ast ID for ith deoptimization.
4769// [i * 2 + 1]: PC and state of ith deoptimization
4770class DeoptimizationOutputData: public FixedArray {
4771 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004772 inline int DeoptPoints();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004773
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004774 inline BailoutId AstId(int index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004775
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004776 inline void SetAstId(int index, BailoutId id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004777
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004778 inline Smi* PcAndState(int index);
4779 inline void SetPcAndState(int index, Smi* offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004780
4781 static int LengthOfFixedArray(int deopt_points) {
4782 return deopt_points * 2;
4783 }
4784
4785 // Allocates a DeoptimizationOutputData.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004786 static Handle<DeoptimizationOutputData> New(Isolate* isolate,
4787 int number_of_deopt_points,
4788 PretenureFlag pretenure);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004789
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004790 DECLARE_CAST(DeoptimizationOutputData)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004791
Ben Murdoch097c5b22016-05-18 11:27:45 +01004792#ifdef ENABLE_DISASSEMBLER
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004793 void DeoptimizationOutputDataPrint(std::ostream& os); // NOLINT
Ben Murdochb0fe1622011-05-05 13:52:32 +01004794#endif
4795};
4796
4797
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004798// A literals array contains the literals for a JSFunction. It also holds
4799// the type feedback vector.
4800class LiteralsArray : public FixedArray {
4801 public:
4802 static const int kVectorIndex = 0;
4803 static const int kFirstLiteralIndex = 1;
4804 static const int kOffsetToFirstLiteral =
4805 FixedArray::kHeaderSize + kPointerSize;
4806
4807 static int OffsetOfLiteralAt(int index) {
4808 return SizeFor(index + kFirstLiteralIndex);
4809 }
4810
4811 inline TypeFeedbackVector* feedback_vector() const;
4812 inline void set_feedback_vector(TypeFeedbackVector* vector);
4813 inline Object* literal(int literal_index) const;
4814 inline void set_literal(int literal_index, Object* literal);
4815 inline int literals_count() const;
4816
4817 static Handle<LiteralsArray> New(Isolate* isolate,
4818 Handle<TypeFeedbackVector> vector,
4819 int number_of_literals,
4820 PretenureFlag pretenure);
4821
4822 DECLARE_CAST(LiteralsArray)
4823
4824 private:
4825 inline Object* get(int index) const;
4826 inline void set(int index, Object* value);
4827 inline void set(int index, Smi* value);
4828 inline void set(int index, Object* value, WriteBarrierMode mode);
4829};
4830
4831
4832// HandlerTable is a fixed array containing entries for exception handlers in
4833// the code object it is associated with. The tables comes in two flavors:
4834// 1) Based on ranges: Used for unoptimized code. Contains one entry per
4835// exception handler and a range representing the try-block covered by that
4836// handler. Layout looks as follows:
Ben Murdoch097c5b22016-05-18 11:27:45 +01004837// [ range-start , range-end , handler-offset , handler-data ]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004838// 2) Based on return addresses: Used for turbofanned code. Contains one entry
4839// per call-site that could throw an exception. Layout looks as follows:
4840// [ return-address-offset , handler-offset ]
4841class HandlerTable : public FixedArray {
4842 public:
4843 // Conservative prediction whether a given handler will locally catch an
4844 // exception or cause a re-throw to outside the code boundary. Since this is
4845 // undecidable it is merely an approximation (e.g. useful for debugger).
4846 enum CatchPrediction { UNCAUGHT, CAUGHT };
4847
Ben Murdoch097c5b22016-05-18 11:27:45 +01004848 // Getters for handler table based on ranges.
4849 inline int GetRangeStart(int index) const;
4850 inline int GetRangeEnd(int index) const;
4851 inline int GetRangeHandler(int index) const;
4852 inline int GetRangeData(int index) const;
4853
4854 // Setters for handler table based on ranges.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004855 inline void SetRangeStart(int index, int value);
4856 inline void SetRangeEnd(int index, int value);
4857 inline void SetRangeHandler(int index, int offset, CatchPrediction pred);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004858 inline void SetRangeData(int index, int value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004859
Ben Murdoch097c5b22016-05-18 11:27:45 +01004860 // Setters for handler table based on return addresses.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004861 inline void SetReturnOffset(int index, int value);
4862 inline void SetReturnHandler(int index, int offset, CatchPrediction pred);
4863
4864 // Lookup handler in a table based on ranges.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004865 int LookupRange(int pc_offset, int* data, CatchPrediction* prediction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004866
4867 // Lookup handler in a table based on return addresses.
4868 int LookupReturn(int pc_offset, CatchPrediction* prediction);
4869
Ben Murdoch097c5b22016-05-18 11:27:45 +01004870 // Returns the conservative catch predication.
4871 inline CatchPrediction GetRangePrediction(int index) const;
4872
4873 // Returns the number of entries in the table.
4874 inline int NumberOfRangeEntries() const;
4875
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004876 // Returns the required length of the underlying fixed array.
4877 static int LengthForRange(int entries) { return entries * kRangeEntrySize; }
4878 static int LengthForReturn(int entries) { return entries * kReturnEntrySize; }
4879
4880 DECLARE_CAST(HandlerTable)
4881
Ben Murdoch097c5b22016-05-18 11:27:45 +01004882#ifdef ENABLE_DISASSEMBLER
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004883 void HandlerTableRangePrint(std::ostream& os); // NOLINT
4884 void HandlerTableReturnPrint(std::ostream& os); // NOLINT
4885#endif
4886
4887 private:
4888 // Layout description for handler table based on ranges.
4889 static const int kRangeStartIndex = 0;
4890 static const int kRangeEndIndex = 1;
4891 static const int kRangeHandlerIndex = 2;
Ben Murdoch097c5b22016-05-18 11:27:45 +01004892 static const int kRangeDataIndex = 3;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004893 static const int kRangeEntrySize = 4;
4894
4895 // Layout description for handler table based on return addresses.
4896 static const int kReturnOffsetIndex = 0;
4897 static const int kReturnHandlerIndex = 1;
4898 static const int kReturnEntrySize = 2;
4899
4900 // Encoding of the {handler} field.
4901 class HandlerPredictionField : public BitField<CatchPrediction, 0, 1> {};
4902 class HandlerOffsetField : public BitField<int, 1, 30> {};
4903};
4904
Ben Murdochb8e0da22011-05-16 14:20:40 +01004905
Steve Blocka7e24c12009-10-30 11:49:00 +00004906// Code describes objects with on-the-fly generated machine code.
4907class Code: public HeapObject {
4908 public:
4909 // Opaque data type for encapsulating code flags like kind, inline
4910 // cache state, and arguments count.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004911 typedef uint32_t Flags;
4912
4913#define NON_IC_KIND_LIST(V) \
4914 V(FUNCTION) \
4915 V(OPTIMIZED_FUNCTION) \
Ben Murdochda12d292016-06-02 14:46:10 +01004916 V(BYTECODE_HANDLER) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004917 V(STUB) \
4918 V(HANDLER) \
4919 V(BUILTIN) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004920 V(REGEXP) \
Ben Murdochda12d292016-06-02 14:46:10 +01004921 V(WASM_FUNCTION) \
4922 V(WASM_TO_JS_FUNCTION) \
4923 V(JS_TO_WASM_FUNCTION)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004924
4925#define IC_KIND_LIST(V) \
4926 V(LOAD_IC) \
4927 V(KEYED_LOAD_IC) \
4928 V(CALL_IC) \
4929 V(STORE_IC) \
4930 V(KEYED_STORE_IC) \
4931 V(BINARY_OP_IC) \
4932 V(COMPARE_IC) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004933 V(TO_BOOLEAN_IC)
4934
4935#define CODE_KIND_LIST(V) \
4936 NON_IC_KIND_LIST(V) \
4937 IC_KIND_LIST(V)
Steve Blocka7e24c12009-10-30 11:49:00 +00004938
4939 enum Kind {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004940#define DEFINE_CODE_KIND_ENUM(name) name,
4941 CODE_KIND_LIST(DEFINE_CODE_KIND_ENUM)
4942#undef DEFINE_CODE_KIND_ENUM
4943 NUMBER_OF_KINDS
Steve Blocka7e24c12009-10-30 11:49:00 +00004944 };
4945
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004946 // No more than 32 kinds. The value is currently encoded in five bits in
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004947 // Flags.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004948 STATIC_ASSERT(NUMBER_OF_KINDS <= 32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004949
4950 static const char* Kind2String(Kind kind);
4951
4952 // Types of stubs.
4953 enum StubType {
4954 NORMAL,
4955 FAST
Steve Blocka7e24c12009-10-30 11:49:00 +00004956 };
4957
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004958 static const int kPrologueOffsetNotSet = -1;
Ben Murdochb8e0da22011-05-16 14:20:40 +01004959
Steve Blocka7e24c12009-10-30 11:49:00 +00004960#ifdef ENABLE_DISASSEMBLER
4961 // Printing
Steve Blocka7e24c12009-10-30 11:49:00 +00004962 static const char* ICState2String(InlineCacheState state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004963 static const char* StubType2String(StubType type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004964 static void PrintExtraICState(std::ostream& os, // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004965 Kind kind, ExtraICState extra);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04004966 void Disassemble(const char* name, std::ostream& os); // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00004967#endif // ENABLE_DISASSEMBLER
4968
4969 // [instruction_size]: Size of the native instructions
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004970 inline int instruction_size() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00004971 inline void set_instruction_size(int value);
4972
Leon Clarkeac952652010-07-15 11:15:24 +01004973 // [relocation_info]: Code relocation information
4974 DECL_ACCESSORS(relocation_info, ByteArray)
Ben Murdochb0fe1622011-05-05 13:52:32 +01004975 void InvalidateRelocation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004976 void InvalidateEmbeddedObjects();
Leon Clarkeac952652010-07-15 11:15:24 +01004977
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004978 // [handler_table]: Fixed array containing offsets of exception handlers.
4979 DECL_ACCESSORS(handler_table, FixedArray)
4980
Ben Murdochb0fe1622011-05-05 13:52:32 +01004981 // [deoptimization_data]: Array containing data for deopt.
4982 DECL_ACCESSORS(deoptimization_data, FixedArray)
4983
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004984 // [raw_type_feedback_info]: This field stores various things, depending on
4985 // the kind of the code object.
4986 // FUNCTION => type feedback information.
4987 // STUB and ICs => major/minor key as Smi.
4988 DECL_ACCESSORS(raw_type_feedback_info, Object)
4989 inline Object* type_feedback_info();
4990 inline void set_type_feedback_info(
4991 Object* value, WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
4992 inline uint32_t stub_key();
4993 inline void set_stub_key(uint32_t key);
4994
4995 // [next_code_link]: Link for lists of optimized or deoptimized code.
4996 // Note that storage for this field is overlapped with typefeedback_info.
4997 DECL_ACCESSORS(next_code_link, Object)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004998
4999 // [gc_metadata]: Field used to hold GC related metadata. The contents of this
Ben Murdoch257744e2011-11-30 15:57:28 +00005000 // field does not have to be traced during garbage collection since
5001 // it is only used by the garbage collector itself.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005002 DECL_ACCESSORS(gc_metadata, Object)
5003
5004 // [ic_age]: Inline caching age: the value of the Heap::global_ic_age
5005 // at the moment when this object was created.
5006 inline void set_ic_age(int count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005007 inline int ic_age() const;
5008
5009 // [prologue_offset]: Offset of the function prologue, used for aging
5010 // FUNCTIONs and OPTIMIZED_FUNCTIONs.
5011 inline int prologue_offset() const;
5012 inline void set_prologue_offset(int offset);
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01005013
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005014 // [constant_pool offset]: Offset of the constant pool.
5015 // Valid for FLAG_enable_embedded_constant_pool only
5016 inline int constant_pool_offset() const;
5017 inline void set_constant_pool_offset(int offset);
5018
Ben Murdochb0fe1622011-05-05 13:52:32 +01005019 // Unchecked accessors to be used during GC.
Leon Clarkeac952652010-07-15 11:15:24 +01005020 inline ByteArray* unchecked_relocation_info();
5021
Steve Blocka7e24c12009-10-30 11:49:00 +00005022 inline int relocation_size();
Steve Blocka7e24c12009-10-30 11:49:00 +00005023
Steve Blocka7e24c12009-10-30 11:49:00 +00005024 // [flags]: Various code flags.
5025 inline Flags flags();
5026 inline void set_flags(Flags flags);
5027
5028 // [flags]: Access to specific code flags.
5029 inline Kind kind();
5030 inline InlineCacheState ic_state(); // Only valid for IC stubs.
Ben Murdochb8e0da22011-05-16 14:20:40 +01005031 inline ExtraICState extra_ic_state(); // Only valid for IC stubs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005032
5033 inline StubType type(); // Only valid for monomorphic IC stubs.
Steve Blocka7e24c12009-10-30 11:49:00 +00005034
5035 // Testers for IC stub kinds.
5036 inline bool is_inline_cache_stub();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005037 inline bool is_debug_stub();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005038 inline bool is_handler();
5039 inline bool is_load_stub();
5040 inline bool is_keyed_load_stub();
5041 inline bool is_store_stub();
5042 inline bool is_keyed_store_stub();
5043 inline bool is_call_stub();
5044 inline bool is_binary_op_stub();
5045 inline bool is_compare_ic_stub();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005046 inline bool is_to_boolean_ic_stub();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005047 inline bool is_keyed_stub();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005048 inline bool is_optimized_code();
Ben Murdochda12d292016-06-02 14:46:10 +01005049 inline bool is_wasm_code();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005050 inline bool embeds_maps_weakly();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005051
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005052 inline bool IsCodeStubOrIC();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005053 inline bool IsJavaScriptCode();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005054
5055 inline void set_raw_kind_specific_flags1(int value);
5056 inline void set_raw_kind_specific_flags2(int value);
5057
Ben Murdoch097c5b22016-05-18 11:27:45 +01005058 // Testers for interpreter builtins.
5059 inline bool is_interpreter_entry_trampoline();
5060 inline bool is_interpreter_enter_bytecode_dispatch();
5061
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005062 // [is_crankshafted]: For kind STUB or ICs, tells whether or not a code
5063 // object was generated by either the hydrogen or the TurboFan optimizing
5064 // compiler (but it may not be an optimized function).
5065 inline bool is_crankshafted();
5066 inline bool is_hydrogen_stub(); // Crankshafted, but not a function.
5067 inline void set_is_crankshafted(bool value);
5068
5069 // [is_turbofanned]: For kind STUB or OPTIMIZED_FUNCTION, tells whether the
5070 // code object was generated by the TurboFan optimizing compiler.
5071 inline bool is_turbofanned();
5072 inline void set_is_turbofanned(bool value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005073
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005074 // [can_have_weak_objects]: For kind OPTIMIZED_FUNCTION, tells whether the
5075 // embedded objects in code should be treated weakly.
5076 inline bool can_have_weak_objects();
5077 inline void set_can_have_weak_objects(bool value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005078
5079 // [has_deoptimization_support]: For FUNCTION kind, tells if it has
5080 // deoptimization support.
5081 inline bool has_deoptimization_support();
5082 inline void set_has_deoptimization_support(bool value);
5083
Ben Murdoch589d6972011-11-30 16:04:58 +00005084 // [has_debug_break_slots]: For FUNCTION kind, tells if it has
5085 // been compiled with debug break slots.
5086 inline bool has_debug_break_slots();
5087 inline void set_has_debug_break_slots(bool value);
5088
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005089 // [has_reloc_info_for_serialization]: For FUNCTION kind, tells if its
5090 // reloc info includes runtime and external references to support
5091 // serialization/deserialization.
5092 inline bool has_reloc_info_for_serialization();
5093 inline void set_has_reloc_info_for_serialization(bool value);
5094
Ben Murdochb0fe1622011-05-05 13:52:32 +01005095 // [allow_osr_at_loop_nesting_level]: For FUNCTION kind, tells for
5096 // how long the function has been marked for OSR and therefore which
5097 // level of loop nesting we are willing to do on-stack replacement
5098 // for.
5099 inline void set_allow_osr_at_loop_nesting_level(int level);
5100 inline int allow_osr_at_loop_nesting_level();
5101
Ben Murdoch8f9999f2012-04-23 10:39:17 +01005102 // [profiler_ticks]: For FUNCTION kind, tells for how many profiler ticks
5103 // the code object was seen on the stack with no IC patching going on.
5104 inline int profiler_ticks();
5105 inline void set_profiler_ticks(int ticks);
5106
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005107 // [builtin_index]: For BUILTIN kind, tells which builtin index it has.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005108 // For builtins, tells which builtin index it has.
5109 // Note that builtins can have a code kind other than BUILTIN, which means
5110 // that for arbitrary code objects, this index value may be random garbage.
5111 // To verify in that case, compare the code object to the indexed builtin.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005112 inline int builtin_index();
5113 inline void set_builtin_index(int id);
5114
Ben Murdochb0fe1622011-05-05 13:52:32 +01005115 // [stack_slots]: For kind OPTIMIZED_FUNCTION, the number of stack slots
5116 // reserved in the code prologue.
5117 inline unsigned stack_slots();
5118 inline void set_stack_slots(unsigned slots);
5119
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005120 // [safepoint_table_start]: For kind OPTIMIZED_FUNCTION, the offset in
Ben Murdochb0fe1622011-05-05 13:52:32 +01005121 // the instruction stream where the safepoint table starts.
Steve Block1e0659c2011-05-24 12:43:12 +01005122 inline unsigned safepoint_table_offset();
5123 inline void set_safepoint_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005124
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005125 // [back_edge_table_start]: For kind FUNCTION, the offset in the
5126 // instruction stream where the back edge table starts.
5127 inline unsigned back_edge_table_offset();
5128 inline void set_back_edge_table_offset(unsigned offset);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005129
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005130 inline bool back_edges_patched_for_osr();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005131
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005132 // [to_boolean_foo]: For kind TO_BOOLEAN_IC tells what state the stub is in.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005133 inline uint16_t to_boolean_state();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005134
5135 // [marked_for_deoptimization]: For kind OPTIMIZED_FUNCTION tells whether
5136 // the code is going to be deoptimized because of dead embedded maps.
5137 inline bool marked_for_deoptimization();
5138 inline void set_marked_for_deoptimization(bool flag);
5139
5140 // [constant_pool]: The constant pool for this function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005141 inline Address constant_pool();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005142
Ben Murdochb8e0da22011-05-16 14:20:40 +01005143 // Get the safepoint entry for the given pc.
5144 SafepointEntry GetSafepointEntry(Address pc);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005145
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005146 // Find an object in a stub with a specified map
5147 Object* FindNthObject(int n, Map* match_map);
5148
5149 // Find the first allocation site in an IC stub.
5150 AllocationSite* FindFirstAllocationSite();
Ben Murdochb0fe1622011-05-05 13:52:32 +01005151
5152 // Find the first map in an IC stub.
5153 Map* FindFirstMap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005154 void FindAllMaps(MapHandleList* maps);
Steve Blocka7e24c12009-10-30 11:49:00 +00005155
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005156 // Find the first handler in an IC stub.
5157 Code* FindFirstHandler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005158
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005159 // Find |length| handlers and put them into |code_list|. Returns false if not
5160 // enough handlers can be found.
5161 bool FindHandlers(CodeHandleList* code_list, int length = -1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005163 // Find the handler for |map|.
5164 MaybeHandle<Code> FindHandlerForMap(Map* map);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005165
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005166 // Find the first name in an IC stub.
5167 Name* FindFirstName();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005168
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005169 class FindAndReplacePattern;
5170 // For each (map-to-find, object-to-replace) pair in the pattern, this
5171 // function replaces the corresponding placeholder in the code with the
5172 // object-to-replace. The function assumes that pairs in the pattern come in
5173 // the same order as the placeholders in the code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005174 // If the placeholder is a weak cell, then the value of weak cell is matched
5175 // against the map-to-find.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005176 void FindAndReplace(const FindAndReplacePattern& pattern);
5177
5178 // The entire code object including its header is copied verbatim to the
5179 // snapshot so that it can be written in one, fast, memcpy during
5180 // deserialization. The deserializer will overwrite some pointers, rather
5181 // like a runtime linker, but the random allocation addresses used in the
5182 // mksnapshot process would still be present in the unlinked snapshot data,
5183 // which would make snapshot production non-reproducible. This method wipes
5184 // out the to-be-overwritten header data for reproducible snapshots.
5185 inline void WipeOutHeader();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005186
Steve Blocka7e24c12009-10-30 11:49:00 +00005187 // Flags operations.
Ben Murdochb8e0da22011-05-16 14:20:40 +01005188 static inline Flags ComputeFlags(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005189 Kind kind, InlineCacheState ic_state = UNINITIALIZED,
5190 ExtraICState extra_ic_state = kNoExtraICState, StubType type = NORMAL,
5191 CacheHolderFlag holder = kCacheOnReceiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00005192
5193 static inline Flags ComputeMonomorphicFlags(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005194 Kind kind, ExtraICState extra_ic_state = kNoExtraICState,
5195 CacheHolderFlag holder = kCacheOnReceiver, StubType type = NORMAL);
5196
5197 static inline Flags ComputeHandlerFlags(
5198 Kind handler_kind, StubType type = NORMAL,
5199 CacheHolderFlag holder = kCacheOnReceiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00005200
Steve Blocka7e24c12009-10-30 11:49:00 +00005201 static inline InlineCacheState ExtractICStateFromFlags(Flags flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005202 static inline StubType ExtractTypeFromFlags(Flags flags);
5203 static inline CacheHolderFlag ExtractCacheHolderFromFlags(Flags flags);
Ben Murdoch589d6972011-11-30 16:04:58 +00005204 static inline Kind ExtractKindFromFlags(Flags flags);
Ben Murdoch589d6972011-11-30 16:04:58 +00005205 static inline ExtraICState ExtractExtraICStateFromFlags(Flags flags);
Ben Murdoch589d6972011-11-30 16:04:58 +00005206
Steve Blocka7e24c12009-10-30 11:49:00 +00005207 static inline Flags RemoveTypeFromFlags(Flags flags);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005208 static inline Flags RemoveTypeAndHolderFromFlags(Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00005209
5210 // Convert a target address into a code object.
5211 static inline Code* GetCodeFromTargetAddress(Address address);
5212
Steve Block791712a2010-08-27 10:21:07 +01005213 // Convert an entry address into an object.
5214 static inline Object* GetObjectFromEntryAddress(Address location_of_address);
5215
Steve Blocka7e24c12009-10-30 11:49:00 +00005216 // Returns the address of the first instruction.
5217 inline byte* instruction_start();
5218
Leon Clarkeac952652010-07-15 11:15:24 +01005219 // Returns the address right after the last instruction.
5220 inline byte* instruction_end();
5221
Steve Blocka7e24c12009-10-30 11:49:00 +00005222 // Returns the size of the instructions, padding, and relocation information.
5223 inline int body_size();
5224
5225 // Returns the address of the first relocation info (read backwards!).
5226 inline byte* relocation_start();
5227
5228 // Code entry point.
5229 inline byte* entry();
5230
5231 // Returns true if pc is inside this object's instructions.
5232 inline bool contains(byte* pc);
5233
Steve Blocka7e24c12009-10-30 11:49:00 +00005234 // Relocate the code by delta bytes. Called to signal that this code
5235 // object has been moved by delta bytes.
Steve Blockd0582a62009-12-15 09:54:21 +00005236 void Relocate(intptr_t delta);
Steve Blocka7e24c12009-10-30 11:49:00 +00005237
5238 // Migrate code described by desc.
5239 void CopyFrom(const CodeDesc& desc);
5240
Ben Murdoch3bec4d22010-07-22 14:51:16 +01005241 // Returns the object size for a given body (used for allocation).
5242 static int SizeFor(int body_size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005243 DCHECK_SIZE_TAG_ALIGNED(body_size);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01005244 return RoundUp(kHeaderSize + body_size, kCodeAlignment);
Steve Blocka7e24c12009-10-30 11:49:00 +00005245 }
5246
5247 // Calculate the size of the code object to report for log events. This takes
5248 // the layout of the code object into account.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005249 inline int ExecutableSize();
Steve Blocka7e24c12009-10-30 11:49:00 +00005250
5251 // Locating source position.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005252 int SourcePosition(int code_offset);
5253 int SourceStatementPosition(int code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +00005254
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005255 DECLARE_CAST(Code)
Steve Blocka7e24c12009-10-30 11:49:00 +00005256
5257 // Dispatched behavior.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005258 inline int CodeSize();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005259
5260 DECLARE_PRINTER(Code)
5261 DECLARE_VERIFIER(Code)
5262
Ben Murdoch8f9999f2012-04-23 10:39:17 +01005263 void ClearInlineCaches();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005264 void ClearInlineCaches(Kind kind);
5265
5266 BailoutId TranslatePcOffsetToAstId(uint32_t pc_offset);
5267 uint32_t TranslateAstIdToPcOffset(BailoutId ast_id);
5268
5269#define DECLARE_CODE_AGE_ENUM(X) k##X##CodeAge,
5270 enum Age {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005271 kToBeExecutedOnceCodeAge = -3,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005272 kNotExecutedCodeAge = -2,
5273 kExecutedOnceCodeAge = -1,
5274 kNoAgeCodeAge = 0,
5275 CODE_AGE_LIST(DECLARE_CODE_AGE_ENUM)
5276 kAfterLastCodeAge,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005277 kFirstCodeAge = kToBeExecutedOnceCodeAge,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005278 kLastCodeAge = kAfterLastCodeAge - 1,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005279 kCodeAgeCount = kAfterLastCodeAge - kFirstCodeAge - 1,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005280 kIsOldCodeAge = kSexagenarianCodeAge,
5281 kPreAgedCodeAge = kIsOldCodeAge - 1
5282 };
5283#undef DECLARE_CODE_AGE_ENUM
5284
5285 // Code aging. Indicates how many full GCs this code has survived without
5286 // being entered through the prologue. Used to determine when it is
5287 // relatively safe to flush this code object and replace it with the lazy
5288 // compilation stub.
5289 static void MakeCodeAgeSequenceYoung(byte* sequence, Isolate* isolate);
5290 static void MarkCodeAsExecuted(byte* sequence, Isolate* isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005291 void MakeYoung(Isolate* isolate);
Ben Murdochda12d292016-06-02 14:46:10 +01005292 void PreAge(Isolate* isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005293 void MarkToBeExecutedOnce(Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005294 void MakeOlder(MarkingParity);
5295 static bool IsYoungSequence(Isolate* isolate, byte* sequence);
5296 bool IsOld();
5297 Age GetAge();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005298 static inline Code* GetPreAgedCodeAgeStub(Isolate* isolate) {
5299 return GetCodeAgeStub(isolate, kNotExecutedCodeAge, NO_MARKING_PARITY);
5300 }
5301
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005302 void PrintDeoptLocation(FILE* out, Address pc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005303 bool CanDeoptAt(Address pc);
5304
5305#ifdef VERIFY_HEAP
5306 void VerifyEmbeddedObjectsDependency();
5307#endif
5308
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005309#ifdef DEBUG
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005310 enum VerifyMode { kNoContextSpecificPointers, kNoContextRetainingPointers };
5311 void VerifyEmbeddedObjects(VerifyMode mode = kNoContextRetainingPointers);
5312 static void VerifyRecompiledCode(Code* old_code, Code* new_code);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005313#endif // DEBUG
5314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005315 inline bool CanContainWeakObjects();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005316
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005317 inline bool IsWeakObject(Object* object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005318
5319 static inline bool IsWeakObjectInOptimizedCode(Object* object);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005321 static Handle<WeakCell> WeakCellFor(Handle<Code> code);
5322 WeakCell* CachedWeakCell();
5323
Ben Murdochb0fe1622011-05-05 13:52:32 +01005324 // Max loop nesting marker used to postpose OSR. We don't take loop
5325 // nesting that is deeper than 5 levels into account.
5326 static const int kMaxLoopNestingMarker = 6;
5327
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005328 static const int kConstantPoolSize =
5329 FLAG_enable_embedded_constant_pool ? kIntSize : 0;
5330
Steve Blocka7e24c12009-10-30 11:49:00 +00005331 // Layout description.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005332 static const int kRelocationInfoOffset = HeapObject::kHeaderSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005333 static const int kHandlerTableOffset = kRelocationInfoOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005334 static const int kDeoptimizationDataOffset =
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005335 kHandlerTableOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005336 // For FUNCTION kind, we store the type feedback info here.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005337 static const int kTypeFeedbackInfoOffset =
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01005338 kDeoptimizationDataOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005339 static const int kNextCodeLinkOffset = kTypeFeedbackInfoOffset + kPointerSize;
5340 static const int kGCMetadataOffset = kNextCodeLinkOffset + kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005341 static const int kInstructionSizeOffset = kGCMetadataOffset + kPointerSize;
5342 static const int kICAgeOffset = kInstructionSizeOffset + kIntSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005343 static const int kFlagsOffset = kICAgeOffset + kIntSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005344 static const int kKindSpecificFlags1Offset = kFlagsOffset + kIntSize;
5345 static const int kKindSpecificFlags2Offset =
5346 kKindSpecificFlags1Offset + kIntSize;
5347 // Note: We might be able to squeeze this into the flags above.
5348 static const int kPrologueOffset = kKindSpecificFlags2Offset + kIntSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005349 static const int kConstantPoolOffset = kPrologueOffset + kIntSize;
Ben Murdochda12d292016-06-02 14:46:10 +01005350 static const int kBuiltinIndexOffset =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005351 kConstantPoolOffset + kConstantPoolSize;
Ben Murdochda12d292016-06-02 14:46:10 +01005352 static const int kHeaderPaddingStart = kBuiltinIndexOffset + kIntSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01005353
Steve Blocka7e24c12009-10-30 11:49:00 +00005354 // Add padding to align the instruction start following right after
5355 // the Code object header.
5356 static const int kHeaderSize =
Ben Murdochb0fe1622011-05-05 13:52:32 +01005357 (kHeaderPaddingStart + kCodeAlignmentMask) & ~kCodeAlignmentMask;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005358
5359 class BodyDescriptor;
Steve Blocka7e24c12009-10-30 11:49:00 +00005360
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005361 // Byte offsets within kKindSpecificFlags1Offset.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005362 static const int kFullCodeFlags = kKindSpecificFlags1Offset;
Ben Murdoch589d6972011-11-30 16:04:58 +00005363 class FullCodeFlagsHasDeoptimizationSupportField:
5364 public BitField<bool, 0, 1> {}; // NOLINT
5365 class FullCodeFlagsHasDebugBreakSlotsField: public BitField<bool, 1, 1> {};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005366 class FullCodeFlagsHasRelocInfoForSerialization
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005367 : public BitField<bool, 2, 1> {};
5368 // Bit 3 in this bitfield is unused.
5369 class ProfilerTicksField : public BitField<int, 4, 28> {};
Steve Blocka7e24c12009-10-30 11:49:00 +00005370
Ben Murdoch589d6972011-11-30 16:04:58 +00005371 // Flags layout. BitField<type, shift, size>.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005372 class ICStateField : public BitField<InlineCacheState, 0, 3> {};
5373 class TypeField : public BitField<StubType, 3, 1> {};
5374 class CacheHolderField : public BitField<CacheHolderFlag, 4, 2> {};
5375 class KindField : public BitField<Kind, 6, 5> {};
Ben Murdochda12d292016-06-02 14:46:10 +01005376 class ExtraICStateField
5377 : public BitField<ExtraICState, 11, PlatformSmiTagging::kSmiValueSize -
5378 11 + 1> {}; // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +00005379
Ben Murdochda12d292016-06-02 14:46:10 +01005380 // KindSpecificFlags1 layout (STUB, BUILTIN and OPTIMIZED_FUNCTION)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005381 static const int kStackSlotsFirstBit = 0;
5382 static const int kStackSlotsBitCount = 24;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005383 static const int kMarkedForDeoptimizationBit =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005384 kStackSlotsFirstBit + kStackSlotsBitCount;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005385 static const int kIsTurbofannedBit = kMarkedForDeoptimizationBit + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005386 static const int kCanHaveWeakObjects = kIsTurbofannedBit + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005387
5388 STATIC_ASSERT(kStackSlotsFirstBit + kStackSlotsBitCount <= 32);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005389 STATIC_ASSERT(kCanHaveWeakObjects + 1 <= 32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005390
5391 class StackSlotsField: public BitField<int,
5392 kStackSlotsFirstBit, kStackSlotsBitCount> {}; // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005393 class MarkedForDeoptimizationField
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005394 : public BitField<bool, kMarkedForDeoptimizationBit, 1> {}; // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005395 class IsTurbofannedField : public BitField<bool, kIsTurbofannedBit, 1> {
5396 }; // NOLINT
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005397 class CanHaveWeakObjectsField
5398 : public BitField<bool, kCanHaveWeakObjects, 1> {}; // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005399
5400 // KindSpecificFlags2 layout (ALL)
5401 static const int kIsCrankshaftedBit = 0;
5402 class IsCrankshaftedField: public BitField<bool,
5403 kIsCrankshaftedBit, 1> {}; // NOLINT
5404
5405 // KindSpecificFlags2 layout (STUB and OPTIMIZED_FUNCTION)
5406 static const int kSafepointTableOffsetFirstBit = kIsCrankshaftedBit + 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005407 static const int kSafepointTableOffsetBitCount = 30;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005408
5409 STATIC_ASSERT(kSafepointTableOffsetFirstBit +
5410 kSafepointTableOffsetBitCount <= 32);
5411 STATIC_ASSERT(1 + kSafepointTableOffsetBitCount <= 32);
5412
5413 class SafepointTableOffsetField: public BitField<int,
5414 kSafepointTableOffsetFirstBit,
5415 kSafepointTableOffsetBitCount> {}; // NOLINT
5416
5417 // KindSpecificFlags2 layout (FUNCTION)
5418 class BackEdgeTableOffsetField: public BitField<int,
5419 kIsCrankshaftedBit + 1, 27> {}; // NOLINT
5420 class AllowOSRAtLoopNestingLevelField: public BitField<int,
5421 kIsCrankshaftedBit + 1 + 27, 4> {}; // NOLINT
5422 STATIC_ASSERT(AllowOSRAtLoopNestingLevelField::kMax >= kMaxLoopNestingMarker);
5423
5424 static const int kArgumentsBits = 16;
5425 static const int kMaxArguments = (1 << kArgumentsBits) - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00005426
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005427 // This constant should be encodable in an ARM instruction.
Steve Blocka7e24c12009-10-30 11:49:00 +00005428 static const int kFlagsNotUsedInLookup =
Ben Murdoch589d6972011-11-30 16:04:58 +00005429 TypeField::kMask | CacheHolderField::kMask;
Steve Blocka7e24c12009-10-30 11:49:00 +00005430
5431 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005432 friend class RelocIterator;
5433 friend class Deoptimizer; // For FindCodeAgeSequence.
5434
5435 void ClearInlineCaches(Kind* kind);
5436
5437 // Code aging
5438 byte* FindCodeAgeSequence();
5439 static void GetCodeAgeAndParity(Code* code, Age* age,
5440 MarkingParity* parity);
5441 static void GetCodeAgeAndParity(Isolate* isolate, byte* sequence, Age* age,
5442 MarkingParity* parity);
5443 static Code* GetCodeAgeStub(Isolate* isolate, Age age, MarkingParity parity);
5444
5445 // Code aging -- platform-specific
5446 static void PatchPlatformCodeAge(Isolate* isolate,
5447 byte* sequence, Age age,
5448 MarkingParity parity);
5449
Steve Blocka7e24c12009-10-30 11:49:00 +00005450 DISALLOW_IMPLICIT_CONSTRUCTORS(Code);
5451};
5452
Ben Murdoch097c5b22016-05-18 11:27:45 +01005453class AbstractCode : public HeapObject {
5454 public:
Ben Murdochda12d292016-06-02 14:46:10 +01005455 // All code kinds and INTERPRETED_FUNCTION.
5456 enum Kind {
5457#define DEFINE_CODE_KIND_ENUM(name) name,
5458 CODE_KIND_LIST(DEFINE_CODE_KIND_ENUM)
5459#undef DEFINE_CODE_KIND_ENUM
5460 INTERPRETED_FUNCTION,
5461 };
5462
Ben Murdoch097c5b22016-05-18 11:27:45 +01005463 int SourcePosition(int offset);
5464 int SourceStatementPosition(int offset);
5465
Ben Murdochda12d292016-06-02 14:46:10 +01005466 // Returns the address of the first instruction.
5467 inline Address instruction_start();
5468
5469 // Returns the address right after the last instruction.
5470 inline Address instruction_end();
5471
5472 // Returns the of the code instructions.
5473 inline int instruction_size();
5474
5475 // Returns true if pc is inside this object's instructions.
5476 inline bool contains(byte* pc);
5477
5478 // Returns the AbstractCode::Kind of the code.
5479 inline Kind kind();
5480
5481 // Calculate the size of the code object to report for log events. This takes
5482 // the layout of the code object into account.
5483 inline int ExecutableSize();
5484
Ben Murdoch097c5b22016-05-18 11:27:45 +01005485 DECLARE_CAST(AbstractCode)
Ben Murdoch097c5b22016-05-18 11:27:45 +01005486 inline Code* GetCode();
5487 inline BytecodeArray* GetBytecodeArray();
5488};
Steve Blocka7e24c12009-10-30 11:49:00 +00005489
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005490// Dependent code is a singly linked list of fixed arrays. Each array contains
5491// code objects in weak cells for one dependent group. The suffix of the array
5492// can be filled with the undefined value if the number of codes is less than
5493// the length of the array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005494//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005495// +------+-----------------+--------+--------+-----+--------+-----------+-----+
5496// | next | count & group 1 | code 1 | code 2 | ... | code n | undefined | ... |
5497// +------+-----------------+--------+--------+-----+--------+-----------+-----+
5498// |
5499// V
5500// +------+-----------------+--------+--------+-----+--------+-----------+-----+
5501// | next | count & group 2 | code 1 | code 2 | ... | code m | undefined | ... |
5502// +------+-----------------+--------+--------+-----+--------+-----------+-----+
5503// |
5504// V
5505// empty_fixed_array()
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005506//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005507// The list of fixed arrays is ordered by dependency groups.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005508
5509class DependentCode: public FixedArray {
5510 public:
5511 enum DependencyGroup {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005512 // Group of code that weakly embed this map and depend on being
5513 // deoptimized when the map is garbage collected.
5514 kWeakCodeGroup,
5515 // Group of code that embed a transition to this map, and depend on being
5516 // deoptimized when the transition is replaced by a new version.
5517 kTransitionGroup,
5518 // Group of code that omit run-time prototype checks for prototypes
5519 // described by this map. The group is deoptimized whenever an object
5520 // described by this map changes shape (and transitions to a new map),
5521 // possibly invalidating the assumptions embedded in the code.
5522 kPrototypeCheckGroup,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005523 // Group of code that depends on global property values in property cells
5524 // not being changed.
5525 kPropertyCellChangedGroup,
5526 // Group of code that omit run-time type checks for the field(s) introduced
5527 // by this map.
5528 kFieldTypeGroup,
5529 // Group of code that omit run-time type checks for initial maps of
5530 // constructors.
5531 kInitialMapChangedGroup,
5532 // Group of code that depends on tenuring information in AllocationSites
5533 // not being changed.
5534 kAllocationSiteTenuringChangedGroup,
5535 // Group of code that depends on element transition information in
5536 // AllocationSites not being changed.
5537 kAllocationSiteTransitionChangedGroup
5538 };
5539
5540 static const int kGroupCount = kAllocationSiteTransitionChangedGroup + 1;
5541
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005542 bool Contains(DependencyGroup group, WeakCell* code_cell);
5543 bool IsEmpty(DependencyGroup group);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005544
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005545 static Handle<DependentCode> InsertCompilationDependencies(
5546 Handle<DependentCode> entries, DependencyGroup group,
5547 Handle<Foreign> info);
5548
5549 static Handle<DependentCode> InsertWeakCode(Handle<DependentCode> entries,
5550 DependencyGroup group,
5551 Handle<WeakCell> code_cell);
5552
5553 void UpdateToFinishedCode(DependencyGroup group, Foreign* info,
5554 WeakCell* code_cell);
5555
5556 void RemoveCompilationDependencies(DependentCode::DependencyGroup group,
5557 Foreign* info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005558
5559 void DeoptimizeDependentCodeGroup(Isolate* isolate,
5560 DependentCode::DependencyGroup group);
5561
5562 bool MarkCodeForDeoptimization(Isolate* isolate,
5563 DependentCode::DependencyGroup group);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005564
5565 // The following low-level accessors should only be used by this class
5566 // and the mark compact collector.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005567 inline DependentCode* next_link();
5568 inline void set_next_link(DependentCode* next);
5569 inline int count();
5570 inline void set_count(int value);
5571 inline DependencyGroup group();
5572 inline void set_group(DependencyGroup group);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005573 inline Object* object_at(int i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005574 inline void set_object_at(int i, Object* object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005575 inline void clear_at(int i);
5576 inline void copy(int from, int to);
5577 DECLARE_CAST(DependentCode)
5578
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005579 static const char* DependencyGroupName(DependencyGroup group);
5580 static void SetMarkedForDeoptimization(Code* code, DependencyGroup group);
5581
5582 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005583 static Handle<DependentCode> Insert(Handle<DependentCode> entries,
5584 DependencyGroup group,
5585 Handle<Object> object);
5586 static Handle<DependentCode> New(DependencyGroup group, Handle<Object> object,
5587 Handle<DependentCode> next);
5588 static Handle<DependentCode> EnsureSpace(Handle<DependentCode> entries);
5589 // Compact by removing cleared weak cells and return true if there was
5590 // any cleared weak cell.
5591 bool Compact();
5592 static int Grow(int number_of_entries) {
5593 if (number_of_entries < 5) return number_of_entries + 1;
5594 return number_of_entries * 5 / 4;
5595 }
5596 inline int flags();
5597 inline void set_flags(int flags);
5598 class GroupField : public BitField<int, 0, 3> {};
5599 class CountField : public BitField<int, 3, 27> {};
5600 STATIC_ASSERT(kGroupCount <= GroupField::kMax + 1);
5601 static const int kNextLinkIndex = 0;
5602 static const int kFlagsIndex = 1;
5603 static const int kCodesStartIndex = 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005604};
5605
5606
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005607class PrototypeInfo;
5608
5609
Steve Blocka7e24c12009-10-30 11:49:00 +00005610// All heap objects have a Map that describes their structure.
5611// A Map contains information about:
5612// - Size information about the object
5613// - How to iterate over an object (for garbage collection)
5614class Map: public HeapObject {
5615 public:
5616 // Instance size.
Steve Block791712a2010-08-27 10:21:07 +01005617 // Size in bytes or kVariableSizeSentinel if instances do not have
5618 // a fixed size.
Steve Blocka7e24c12009-10-30 11:49:00 +00005619 inline int instance_size();
5620 inline void set_instance_size(int value);
5621
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005622 // Only to clear an unused byte, remove once byte is used.
5623 inline void clear_unused();
Steve Blocka7e24c12009-10-30 11:49:00 +00005624
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005625 // [inobject_properties_or_constructor_function_index]: Provides access
5626 // to the inobject properties in case of JSObject maps, or the constructor
5627 // function index in case of primitive maps.
5628 inline int inobject_properties_or_constructor_function_index();
5629 inline void set_inobject_properties_or_constructor_function_index(int value);
5630 // Count of properties allocated in the object (JSObject only).
5631 inline int GetInObjectProperties();
5632 inline void SetInObjectProperties(int value);
5633 // Index of the constructor function in the native context (primitives only),
5634 // or the special sentinel value to indicate that there is no object wrapper
5635 // for the primitive (i.e. in case of null or undefined).
5636 static const int kNoConstructorFunctionIndex = 0;
5637 inline int GetConstructorFunctionIndex();
5638 inline void SetConstructorFunctionIndex(int value);
5639 static MaybeHandle<JSFunction> GetConstructorFunction(
5640 Handle<Map> map, Handle<Context> native_context);
Steve Blocka7e24c12009-10-30 11:49:00 +00005641
Ben Murdochda12d292016-06-02 14:46:10 +01005642 // Retrieve interceptors.
5643 inline InterceptorInfo* GetNamedInterceptor();
5644 inline InterceptorInfo* GetIndexedInterceptor();
5645
Steve Blocka7e24c12009-10-30 11:49:00 +00005646 // Instance type.
5647 inline InstanceType instance_type();
5648 inline void set_instance_type(InstanceType value);
5649
5650 // Tells how many unused property fields are available in the
5651 // instance (only used for JSObject in fast mode).
5652 inline int unused_property_fields();
5653 inline void set_unused_property_fields(int value);
5654
5655 // Bit field.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005656 inline byte bit_field() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00005657 inline void set_bit_field(byte value);
5658
5659 // Bit field 2.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005660 inline byte bit_field2() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00005661 inline void set_bit_field2(byte value);
5662
Ben Murdoch257744e2011-11-30 15:57:28 +00005663 // Bit field 3.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005664 inline uint32_t bit_field3() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005665 inline void set_bit_field3(uint32_t bits);
5666
5667 class EnumLengthBits: public BitField<int,
5668 0, kDescriptorIndexBitCount> {}; // NOLINT
5669 class NumberOfOwnDescriptorsBits: public BitField<int,
5670 kDescriptorIndexBitCount, kDescriptorIndexBitCount> {}; // NOLINT
5671 STATIC_ASSERT(kDescriptorIndexBitCount + kDescriptorIndexBitCount == 20);
5672 class DictionaryMap : public BitField<bool, 20, 1> {};
5673 class OwnsDescriptors : public BitField<bool, 21, 1> {};
Ben Murdoch097c5b22016-05-18 11:27:45 +01005674 class HasHiddenPrototype : public BitField<bool, 22, 1> {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005675 class Deprecated : public BitField<bool, 23, 1> {};
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005676 class IsUnstable : public BitField<bool, 24, 1> {};
5677 class IsMigrationTarget : public BitField<bool, 25, 1> {};
Ben Murdochda12d292016-06-02 14:46:10 +01005678 // Bit 26 is free.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005679 class NewTargetIsBase : public BitField<bool, 27, 1> {};
5680 // Bit 28 is free.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005681
5682 // Keep this bit field at the very end for better code in
5683 // Builtins::kJSConstructStubGeneric stub.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005684 // This counter is used for in-object slack tracking.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005685 // The in-object slack tracking is considered enabled when the counter is
Ben Murdoch097c5b22016-05-18 11:27:45 +01005686 // non zero. The counter only has a valid count for initial maps. For
5687 // transitioned maps only kNoSlackTracking has a meaning, namely that inobject
5688 // slack tracking already finished for the transition tree. Any other value
5689 // indicates that either inobject slack tracking is still in progress, or that
5690 // the map isn't part of the transition tree anymore.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005691 class ConstructionCounter : public BitField<int, 29, 3> {};
5692 static const int kSlackTrackingCounterStart = 7;
5693 static const int kSlackTrackingCounterEnd = 1;
5694 static const int kNoSlackTracking = 0;
5695 STATIC_ASSERT(kSlackTrackingCounterStart <= ConstructionCounter::kMax);
5696
5697
5698 // Inobject slack tracking is the way to reclaim unused inobject space.
5699 //
5700 // The instance size is initially determined by adding some slack to
5701 // expected_nof_properties (to allow for a few extra properties added
5702 // after the constructor). There is no guarantee that the extra space
5703 // will not be wasted.
5704 //
5705 // Here is the algorithm to reclaim the unused inobject space:
5706 // - Detect the first constructor call for this JSFunction.
5707 // When it happens enter the "in progress" state: initialize construction
5708 // counter in the initial_map.
5709 // - While the tracking is in progress initialize unused properties of a new
5710 // object with one_pointer_filler_map instead of undefined_value (the "used"
5711 // part is initialized with undefined_value as usual). This way they can
5712 // be resized quickly and safely.
5713 // - Once enough objects have been created compute the 'slack'
5714 // (traverse the map transition tree starting from the
5715 // initial_map and find the lowest value of unused_property_fields).
5716 // - Traverse the transition tree again and decrease the instance size
5717 // of every map. Existing objects will resize automatically (they are
5718 // filled with one_pointer_filler_map). All further allocations will
5719 // use the adjusted instance size.
5720 // - SharedFunctionInfo's expected_nof_properties left unmodified since
5721 // allocations made using different closures could actually create different
5722 // kind of objects (see prototype inheritance pattern).
5723 //
5724 // Important: inobject slack tracking is not attempted during the snapshot
5725 // creation.
5726
5727 static const int kGenerousAllocationCount =
5728 kSlackTrackingCounterStart - kSlackTrackingCounterEnd + 1;
5729
5730 // Starts the tracking by initializing object constructions countdown counter.
5731 void StartInobjectSlackTracking();
5732
5733 // True if the object constructions countdown counter is a range
5734 // [kSlackTrackingCounterEnd, kSlackTrackingCounterStart].
5735 inline bool IsInobjectSlackTrackingInProgress();
5736
5737 // Does the tracking step.
5738 inline void InobjectSlackTrackingStep();
5739
5740 // Completes inobject slack tracking for the transition tree starting at this
5741 // initial map.
5742 void CompleteInobjectSlackTracking();
Ben Murdoch257744e2011-11-30 15:57:28 +00005743
Steve Blocka7e24c12009-10-30 11:49:00 +00005744 // Tells whether the object in the prototype property will be used
5745 // for instances created from this function. If the prototype
5746 // property is set to a value that is not a JSObject, the prototype
5747 // property will not be used to create instances of the function.
5748 // See ECMA-262, 13.2.2.
5749 inline void set_non_instance_prototype(bool value);
5750 inline bool has_non_instance_prototype();
5751
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005752 // Tells whether the instance has a [[Construct]] internal method.
5753 // This property is implemented according to ES6, section 7.2.4.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005754 inline void set_is_constructor(bool value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005755 inline bool is_constructor() const;
Steve Block6ded16b2010-05-10 14:33:55 +01005756
Ben Murdoch097c5b22016-05-18 11:27:45 +01005757 // Tells whether the instance with this map has a hidden prototype.
5758 inline void set_has_hidden_prototype(bool value);
5759 inline bool has_hidden_prototype() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00005760
5761 // Records and queries whether the instance has a named interceptor.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005762 inline void set_has_named_interceptor();
5763 inline bool has_named_interceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00005764
5765 // Records and queries whether the instance has an indexed interceptor.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005766 inline void set_has_indexed_interceptor();
5767 inline bool has_indexed_interceptor();
Steve Blocka7e24c12009-10-30 11:49:00 +00005768
5769 // Tells whether the instance is undetectable.
5770 // An undetectable object is a special class of JSObject: 'typeof' operator
5771 // returns undefined, ToBoolean returns false. Otherwise it behaves like
5772 // a normal JS object. It is useful for implementing undetectable
5773 // document.all in Firefox & Safari.
5774 // See https://bugzilla.mozilla.org/show_bug.cgi?id=248549.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005775 inline void set_is_undetectable();
5776 inline bool is_undetectable();
Steve Blocka7e24c12009-10-30 11:49:00 +00005777
Steve Blocka7e24c12009-10-30 11:49:00 +00005778 // Tells whether the instance has a call-as-function handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005779 inline void set_is_observed();
5780 inline bool is_observed();
Steve Blocka7e24c12009-10-30 11:49:00 +00005781
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005782 // Tells whether the instance has a [[Call]] internal method.
5783 // This property is implemented according to ES6, section 7.2.3.
5784 inline void set_is_callable();
5785 inline bool is_callable() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00005786
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005787 inline void set_new_target_is_base(bool value);
5788 inline bool new_target_is_base();
Steve Block8defd9f2010-07-08 12:39:36 +01005789 inline void set_is_extensible(bool value);
5790 inline bool is_extensible();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005791 inline void set_is_prototype_map(bool value);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005792 inline bool is_prototype_map() const;
Steve Block8defd9f2010-07-08 12:39:36 +01005793
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005794 inline void set_elements_kind(ElementsKind elements_kind);
5795 inline ElementsKind elements_kind();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005796
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005797 // Tells whether the instance has fast elements that are only Smis.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005798 inline bool has_fast_smi_elements();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005799
Steve Block8defd9f2010-07-08 12:39:36 +01005800 // Tells whether the instance has fast elements.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005801 inline bool has_fast_object_elements();
5802 inline bool has_fast_smi_or_object_elements();
5803 inline bool has_fast_double_elements();
5804 inline bool has_fast_elements();
5805 inline bool has_sloppy_arguments_elements();
Ben Murdoch097c5b22016-05-18 11:27:45 +01005806 inline bool has_fast_string_wrapper_elements();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005807 inline bool has_fixed_typed_array_elements();
5808 inline bool has_dictionary_elements();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01005809
5810 static bool IsValidElementsTransition(ElementsKind from_kind,
5811 ElementsKind to_kind);
5812
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005813 // Returns true if the current map doesn't have DICTIONARY_ELEMENTS but if a
5814 // map with DICTIONARY_ELEMENTS was found in the prototype chain.
5815 bool DictionaryElementsInPrototypeChainOnly();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005816
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005817 inline Map* ElementsTransitionMap();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005818
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005819 inline FixedArrayBase* GetInitialElements();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005820
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005821 // [raw_transitions]: Provides access to the transitions storage field.
5822 // Don't call set_raw_transitions() directly to overwrite transitions, use
5823 // the TransitionArray::ReplaceTransitions() wrapper instead!
5824 DECL_ACCESSORS(raw_transitions, Object)
5825 // [prototype_info]: Per-prototype metadata. Aliased with transitions
5826 // (which prototype maps don't have).
5827 DECL_ACCESSORS(prototype_info, Object)
5828 // PrototypeInfo is created lazily using this helper (which installs it on
5829 // the given prototype's map).
5830 static Handle<PrototypeInfo> GetOrCreatePrototypeInfo(
5831 Handle<JSObject> prototype, Isolate* isolate);
5832 static Handle<PrototypeInfo> GetOrCreatePrototypeInfo(
5833 Handle<Map> prototype_map, Isolate* isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005834
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005835 // [prototype chain validity cell]: Associated with a prototype object,
5836 // stored in that object's map's PrototypeInfo, indicates that prototype
5837 // chains through this object are currently valid. The cell will be
5838 // invalidated and replaced when the prototype chain changes.
5839 static Handle<Cell> GetOrCreatePrototypeChainValidityCell(Handle<Map> map,
5840 Isolate* isolate);
5841 static const int kPrototypeChainValid = 0;
5842 static const int kPrototypeChainInvalid = 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005843
5844 Map* FindRootMap();
5845 Map* FindFieldOwner(int descriptor);
5846
5847 inline int GetInObjectPropertyOffset(int index);
5848
5849 int NumberOfFields();
5850
5851 // TODO(ishell): candidate with JSObject::MigrateToMap().
5852 bool InstancesNeedRewriting(Map* target, int target_number_of_fields,
5853 int target_inobject, int target_unused,
5854 int* old_number_of_fields);
5855 // TODO(ishell): moveit!
5856 static Handle<Map> GeneralizeAllFieldRepresentations(Handle<Map> map);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005857 MUST_USE_RESULT static Handle<FieldType> GeneralizeFieldType(
5858 Representation rep1, Handle<FieldType> type1, Representation rep2,
5859 Handle<FieldType> type2, Isolate* isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005860 static void GeneralizeFieldType(Handle<Map> map, int modify_index,
5861 Representation new_representation,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005862 Handle<FieldType> new_field_type);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005863 static Handle<Map> ReconfigureProperty(Handle<Map> map, int modify_index,
5864 PropertyKind new_kind,
5865 PropertyAttributes new_attributes,
5866 Representation new_representation,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005867 Handle<FieldType> new_field_type,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005868 StoreMode store_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005869 static Handle<Map> CopyGeneralizeAllRepresentations(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005870 Handle<Map> map, int modify_index, StoreMode store_mode,
5871 PropertyKind kind, PropertyAttributes attributes, const char* reason);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005872
5873 static Handle<Map> PrepareForDataProperty(Handle<Map> old_map,
5874 int descriptor_number,
5875 Handle<Object> value);
5876
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005877 static Handle<Map> Normalize(Handle<Map> map, PropertyNormalizationMode mode,
5878 const char* reason);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005879
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005880 // Tells whether the map is used for JSObjects in dictionary mode (ie
5881 // normalized objects, ie objects for which HasFastProperties returns false).
5882 // A map can never be used for both dictionary mode and fast mode JSObjects.
5883 // False by default and for HeapObjects that are not JSObjects.
5884 inline void set_dictionary_map(bool value);
5885 inline bool is_dictionary_map();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005886
Steve Blocka7e24c12009-10-30 11:49:00 +00005887 // Tells whether the instance needs security checks when accessing its
5888 // properties.
5889 inline void set_is_access_check_needed(bool access_check_needed);
5890 inline bool is_access_check_needed();
5891
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005892 // Returns true if map has a non-empty stub code cache.
5893 inline bool has_code_cache();
5894
Steve Blocka7e24c12009-10-30 11:49:00 +00005895 // [prototype]: implicit prototype object.
5896 DECL_ACCESSORS(prototype, Object)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005897 // TODO(jkummerow): make set_prototype private.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005898 static void SetPrototype(
5899 Handle<Map> map, Handle<Object> prototype,
5900 PrototypeOptimizationMode proto_mode = FAST_PROTOTYPE);
Steve Blocka7e24c12009-10-30 11:49:00 +00005901
5902 // [constructor]: points back to the function responsible for this map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005903 // The field overlaps with the back pointer. All maps in a transition tree
5904 // have the same constructor, so maps with back pointers can walk the
5905 // back pointer chain until they find the map holding their constructor.
5906 DECL_ACCESSORS(constructor_or_backpointer, Object)
5907 inline Object* GetConstructor() const;
5908 inline void SetConstructor(Object* constructor,
5909 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
5910 // [back pointer]: points back to the parent map from which a transition
5911 // leads to this map. The field overlaps with the constructor (see above).
5912 inline Object* GetBackPointer();
5913 inline void SetBackPointer(Object* value,
5914 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
Steve Blocka7e24c12009-10-30 11:49:00 +00005915
5916 // [instance descriptors]: describes the object.
5917 DECL_ACCESSORS(instance_descriptors, DescriptorArray)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005918
5919 // [layout descriptor]: describes the object layout.
5920 DECL_ACCESSORS(layout_descriptor, LayoutDescriptor)
5921 // |layout descriptor| accessor which can be used from GC.
5922 inline LayoutDescriptor* layout_descriptor_gc_safe();
5923 inline bool HasFastPointerLayout() const;
5924
5925 // |layout descriptor| accessor that is safe to call even when
5926 // FLAG_unbox_double_fields is disabled (in this case Map does not contain
5927 // |layout_descriptor| field at all).
5928 inline LayoutDescriptor* GetLayoutDescriptor();
5929
5930 inline void UpdateDescriptors(DescriptorArray* descriptors,
5931 LayoutDescriptor* layout_descriptor);
5932 inline void InitializeDescriptors(DescriptorArray* descriptors,
5933 LayoutDescriptor* layout_descriptor);
Ben Murdoch257744e2011-11-30 15:57:28 +00005934
Steve Blocka7e24c12009-10-30 11:49:00 +00005935 // [stub cache]: contains stubs compiled for this map.
Steve Block6ded16b2010-05-10 14:33:55 +01005936 DECL_ACCESSORS(code_cache, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00005937
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005938 // [dependent code]: list of optimized codes that weakly embed this map.
5939 DECL_ACCESSORS(dependent_code, DependentCode)
5940
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005941 // [weak cell cache]: cache that stores a weak cell pointing to this map.
5942 DECL_ACCESSORS(weak_cell_cache, Object)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005943
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005944 inline PropertyDetails GetLastDescriptorDetails();
5945
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005946 inline int LastAdded();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005947
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005948 inline int NumberOfOwnDescriptors();
5949 inline void SetNumberOfOwnDescriptors(int number);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005950
5951 inline Cell* RetrieveDescriptorsPointer();
5952
Ben Murdoch097c5b22016-05-18 11:27:45 +01005953 // Checks whether all properties are stored either in the map or on the object
5954 // (inobject, properties, or elements backing store), requiring no special
5955 // checks.
5956 bool OnlyHasSimpleProperties();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005957 inline int EnumLength();
5958 inline void SetEnumLength(int length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005959
5960 inline bool owns_descriptors();
5961 inline void set_owns_descriptors(bool owns_descriptors);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005962 inline void mark_unstable();
5963 inline bool is_stable();
5964 inline void set_migration_target(bool value);
5965 inline bool is_migration_target();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005966 inline void set_construction_counter(int value);
5967 inline int construction_counter();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005968 inline void deprecate();
5969 inline bool is_deprecated();
5970 inline bool CanBeDeprecated();
5971 // Returns a non-deprecated version of the input. If the input was not
5972 // deprecated, it is directly returned. Otherwise, the non-deprecated version
5973 // is found by re-transitioning from the root of the transition tree using the
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005974 // descriptor array of the map. Returns MaybeHandle<Map>() if no updated map
5975 // is found.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005976 static MaybeHandle<Map> TryUpdate(Handle<Map> map) WARN_UNUSED_RESULT;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005977
5978 // Returns a non-deprecated version of the input. This method may deprecate
5979 // existing maps along the way if encodings conflict. Not for use while
5980 // gathering type feedback. Use TryUpdate in those cases instead.
5981 static Handle<Map> Update(Handle<Map> map);
5982
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005983 static inline Handle<Map> CopyInitialMap(Handle<Map> map);
5984 static Handle<Map> CopyInitialMap(Handle<Map> map, int instance_size,
5985 int in_object_properties,
5986 int unused_property_fields);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005987 static Handle<Map> CopyDropDescriptors(Handle<Map> map);
5988 static Handle<Map> CopyInsertDescriptor(Handle<Map> map,
5989 Descriptor* descriptor,
5990 TransitionFlag flag);
5991
5992 MUST_USE_RESULT static MaybeHandle<Map> CopyWithField(
Ben Murdoch097c5b22016-05-18 11:27:45 +01005993 Handle<Map> map, Handle<Name> name, Handle<FieldType> type,
5994 PropertyAttributes attributes, Representation representation,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005995 TransitionFlag flag);
5996
5997 MUST_USE_RESULT static MaybeHandle<Map> CopyWithConstant(
5998 Handle<Map> map,
5999 Handle<Name> name,
6000 Handle<Object> constant,
6001 PropertyAttributes attributes,
6002 TransitionFlag flag);
6003
6004 // Returns a new map with all transitions dropped from the given map and
6005 // the ElementsKind set.
6006 static Handle<Map> TransitionElementsTo(Handle<Map> map,
6007 ElementsKind to_kind);
6008
6009 static Handle<Map> AsElementsKind(Handle<Map> map, ElementsKind kind);
6010
6011 static Handle<Map> CopyAsElementsKind(Handle<Map> map,
6012 ElementsKind kind,
6013 TransitionFlag flag);
6014
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006015 static Handle<Map> AsLanguageMode(Handle<Map> initial_map,
6016 LanguageMode language_mode,
6017 FunctionKind kind);
6018
6019
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006020 static Handle<Map> CopyForObserved(Handle<Map> map);
6021
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006022 static Handle<Map> CopyForPreventExtensions(Handle<Map> map,
6023 PropertyAttributes attrs_to_add,
6024 Handle<Symbol> transition_marker,
6025 const char* reason);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006026
6027 static Handle<Map> FixProxy(Handle<Map> map, InstanceType type, int size);
6028
6029
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006030 // Maximal number of fast properties. Used to restrict the number of map
6031 // transitions to avoid an explosion in the number of maps for objects used as
6032 // dictionaries.
6033 inline bool TooManyFastProperties(StoreFromKeyed store_mode);
6034 static Handle<Map> TransitionToDataProperty(Handle<Map> map,
6035 Handle<Name> name,
6036 Handle<Object> value,
6037 PropertyAttributes attributes,
6038 StoreFromKeyed store_mode);
6039 static Handle<Map> TransitionToAccessorProperty(
Ben Murdochda12d292016-06-02 14:46:10 +01006040 Handle<Map> map, Handle<Name> name, int descriptor,
6041 AccessorComponent component, Handle<Object> accessor,
6042 PropertyAttributes attributes);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006043 static Handle<Map> ReconfigureExistingProperty(Handle<Map> map,
6044 int descriptor,
6045 PropertyKind kind,
6046 PropertyAttributes attributes);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006047
6048 inline void AppendDescriptor(Descriptor* desc);
Steve Blocka7e24c12009-10-30 11:49:00 +00006049
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006050 // Returns a copy of the map, prepared for inserting into the transition
6051 // tree (if the |map| owns descriptors then the new one will share
6052 // descriptors with |map|).
6053 static Handle<Map> CopyForTransition(Handle<Map> map, const char* reason);
6054
Steve Blocka7e24c12009-10-30 11:49:00 +00006055 // Returns a copy of the map, with all transitions dropped from the
6056 // instance descriptors.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006057 static Handle<Map> Copy(Handle<Map> map, const char* reason);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006058 static Handle<Map> Create(Isolate* isolate, int inobject_properties);
Steve Blocka7e24c12009-10-30 11:49:00 +00006059
6060 // Returns the next free property index (only valid for FAST MODE).
6061 int NextFreePropertyIndex();
6062
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006063 // Returns the number of properties described in instance_descriptors
6064 // filtering out properties with the specified attributes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006065 int NumberOfDescribedProperties(DescriptorFlag which = OWN_DESCRIPTORS,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006066 PropertyFilter filter = ALL_PROPERTIES);
Steve Blocka7e24c12009-10-30 11:49:00 +00006067
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006068 DECLARE_CAST(Map)
Steve Blocka7e24c12009-10-30 11:49:00 +00006069
6070 // Code cache operations.
6071
6072 // Clears the code cache.
Steve Block44f0eee2011-05-26 01:26:41 +01006073 inline void ClearCodeCache(Heap* heap);
Steve Blocka7e24c12009-10-30 11:49:00 +00006074
6075 // Update code cache.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006076 static void UpdateCodeCache(Handle<Map> map,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006077 Handle<Name> name,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006078 Handle<Code> code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006079
6080 // Extend the descriptor array of the map with the list of descriptors.
6081 // In case of duplicates, the latest descriptor is used.
6082 static void AppendCallbackDescriptors(Handle<Map> map,
6083 Handle<Object> descriptors);
6084
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006085 static inline int SlackForArraySize(int old_size, int size_limit);
6086
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006087 static void EnsureDescriptorSlack(Handle<Map> map, int slack);
Steve Blocka7e24c12009-10-30 11:49:00 +00006088
6089 // Returns the found code or undefined if absent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006090 Object* FindInCodeCache(Name* name, Code::Flags flags);
Steve Blocka7e24c12009-10-30 11:49:00 +00006091
6092 // Returns the non-negative index of the code object if it is in the
6093 // cache and -1 otherwise.
Steve Block6ded16b2010-05-10 14:33:55 +01006094 int IndexInCodeCache(Object* name, Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00006095
6096 // Removes a code object from the code cache at the given index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006097 void RemoveFromCodeCache(Name* name, Code* code, int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00006098
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006099 // Computes a hash value for this map, to be used in HashTables and such.
6100 int Hash();
6101
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006102 // Returns the map that this map transitions to if its elements_kind
6103 // is changed to |elements_kind|, or NULL if no such map is cached yet.
6104 // |safe_to_add_transitions| is set to false if adding transitions is not
6105 // allowed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006106 Map* LookupElementsTransitionMap(ElementsKind elements_kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006107
6108 // Returns the transitioned map for this map with the most generic
6109 // elements_kind that's found in |candidates|, or null handle if no match is
6110 // found at all.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006111 static Handle<Map> FindTransitionedMap(Handle<Map> map,
6112 MapHandleList* candidates);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006113
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006114 inline bool CanTransition();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006115
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006116 inline bool IsBooleanMap();
6117 inline bool IsPrimitiveMap();
6118 inline bool IsJSReceiverMap();
6119 inline bool IsJSObjectMap();
6120 inline bool IsJSArrayMap();
6121 inline bool IsJSFunctionMap();
6122 inline bool IsStringMap();
6123 inline bool IsJSProxyMap();
6124 inline bool IsJSGlobalProxyMap();
6125 inline bool IsJSGlobalObjectMap();
6126 inline bool IsJSTypedArrayMap();
6127 inline bool IsJSDataViewMap();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006128
6129 inline bool CanOmitMapChecks();
6130
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006131 static void AddDependentCode(Handle<Map> map,
6132 DependentCode::DependencyGroup group,
6133 Handle<Code> code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006134
6135 bool IsMapInArrayPrototypeChain();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006136
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006137 static Handle<WeakCell> WeakCellForMap(Handle<Map> map);
6138
Steve Blocka7e24c12009-10-30 11:49:00 +00006139 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006140 DECLARE_PRINTER(Map)
6141 DECLARE_VERIFIER(Map)
6142
6143#ifdef VERIFY_HEAP
6144 void DictionaryMapVerify();
6145 void VerifyOmittedMapChecks();
Steve Blocka7e24c12009-10-30 11:49:00 +00006146#endif
6147
Iain Merrick75681382010-08-19 15:07:18 +01006148 inline int visitor_id();
6149 inline void set_visitor_id(int visitor_id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006150
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006151 static Handle<Map> TransitionToPrototype(Handle<Map> map,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006152 Handle<Object> prototype,
6153 PrototypeOptimizationMode mode);
Steve Block053d10c2011-06-13 19:13:29 +01006154
Steve Blocka7e24c12009-10-30 11:49:00 +00006155 static const int kMaxPreAllocatedPropertyFields = 255;
6156
6157 // Layout description.
6158 static const int kInstanceSizesOffset = HeapObject::kHeaderSize;
6159 static const int kInstanceAttributesOffset = kInstanceSizesOffset + kIntSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006160 static const int kBitField3Offset = kInstanceAttributesOffset + kIntSize;
6161 static const int kPrototypeOffset = kBitField3Offset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006162 static const int kConstructorOrBackPointerOffset =
6163 kPrototypeOffset + kPointerSize;
6164 // When there is only one transition, it is stored directly in this field;
6165 // otherwise a transition array is used.
6166 // For prototype maps, this slot is used to store this map's PrototypeInfo
6167 // struct.
6168 static const int kTransitionsOrPrototypeInfoOffset =
6169 kConstructorOrBackPointerOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006170 static const int kDescriptorsOffset =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006171 kTransitionsOrPrototypeInfoOffset + kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006172#if V8_DOUBLE_FIELDS_UNBOXING
6173 static const int kLayoutDecriptorOffset = kDescriptorsOffset + kPointerSize;
6174 static const int kCodeCacheOffset = kLayoutDecriptorOffset + kPointerSize;
6175#else
6176 static const int kLayoutDecriptorOffset = 1; // Must not be ever accessed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006177 static const int kCodeCacheOffset = kDescriptorsOffset + kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006178#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006179 static const int kDependentCodeOffset = kCodeCacheOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006180 static const int kWeakCellCacheOffset = kDependentCodeOffset + kPointerSize;
6181 static const int kSize = kWeakCellCacheOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006182
6183 // Layout of pointer fields. Heap iteration code relies on them
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006184 // being continuously allocated.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006185 static const int kPointerFieldsBeginOffset = Map::kPrototypeOffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006186 static const int kPointerFieldsEndOffset = kSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006187
6188 // Byte offsets within kInstanceSizesOffset.
6189 static const int kInstanceSizeOffset = kInstanceSizesOffset + 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006190 static const int kInObjectPropertiesOrConstructorFunctionIndexByte = 1;
6191 static const int kInObjectPropertiesOrConstructorFunctionIndexOffset =
6192 kInstanceSizesOffset + kInObjectPropertiesOrConstructorFunctionIndexByte;
6193 // Note there is one byte available for use here.
6194 static const int kUnusedByte = 2;
6195 static const int kUnusedOffset = kInstanceSizesOffset + kUnusedByte;
Iain Merrick9ac36c92010-09-13 15:29:50 +01006196 static const int kVisitorIdByte = 3;
6197 static const int kVisitorIdOffset = kInstanceSizesOffset + kVisitorIdByte;
Steve Blocka7e24c12009-10-30 11:49:00 +00006198
6199 // Byte offsets within kInstanceAttributesOffset attributes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006200#if V8_TARGET_LITTLE_ENDIAN
6201 // Order instance type and bit field together such that they can be loaded
6202 // together as a 16-bit word with instance type in the lower 8 bits regardless
6203 // of endianess. Also provide endian-independent offset to that 16-bit word.
Steve Blocka7e24c12009-10-30 11:49:00 +00006204 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006205 static const int kBitFieldOffset = kInstanceAttributesOffset + 1;
6206#else
6207 static const int kBitFieldOffset = kInstanceAttributesOffset + 0;
6208 static const int kInstanceTypeOffset = kInstanceAttributesOffset + 1;
6209#endif
6210 static const int kInstanceTypeAndBitFieldOffset =
6211 kInstanceAttributesOffset + 0;
6212 static const int kBitField2Offset = kInstanceAttributesOffset + 2;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006213 static const int kUnusedPropertyFieldsByte = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006214 static const int kUnusedPropertyFieldsOffset = kInstanceAttributesOffset + 3;
Steve Blocka7e24c12009-10-30 11:49:00 +00006215
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006216 STATIC_ASSERT(kInstanceTypeAndBitFieldOffset ==
6217 Internals::kMapInstanceTypeAndBitFieldOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00006218
6219 // Bit positions for bit field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006220 static const int kHasNonInstancePrototype = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006221 static const int kIsCallable = 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006222 static const int kHasNamedInterceptor = 2;
6223 static const int kHasIndexedInterceptor = 3;
6224 static const int kIsUndetectable = 4;
6225 static const int kIsObserved = 5;
6226 static const int kIsAccessCheckNeeded = 6;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006227 static const int kIsConstructor = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +00006228
6229 // Bit positions for bit field 2
Andrei Popescu31002712010-02-23 13:46:05 +00006230 static const int kIsExtensible = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006231 // Bit 1 is free.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006232 class IsPrototypeMapBits : public BitField<bool, 2, 1> {};
6233 class ElementsKindBits: public BitField<ElementsKind, 3, 5> {};
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006234
6235 // Derived values from bit field 2
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006236 static const int8_t kMaximumBitField2FastElementValue = static_cast<int8_t>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006237 (FAST_ELEMENTS + 1) << Map::ElementsKindBits::kShift) - 1;
6238 static const int8_t kMaximumBitField2FastSmiElementValue =
6239 static_cast<int8_t>((FAST_SMI_ELEMENTS + 1) <<
6240 Map::ElementsKindBits::kShift) - 1;
6241 static const int8_t kMaximumBitField2FastHoleyElementValue =
6242 static_cast<int8_t>((FAST_HOLEY_ELEMENTS + 1) <<
6243 Map::ElementsKindBits::kShift) - 1;
6244 static const int8_t kMaximumBitField2FastHoleySmiElementValue =
6245 static_cast<int8_t>((FAST_HOLEY_SMI_ELEMENTS + 1) <<
6246 Map::ElementsKindBits::kShift) - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00006247
Iain Merrick75681382010-08-19 15:07:18 +01006248 typedef FixedBodyDescriptor<kPointerFieldsBeginOffset,
6249 kPointerFieldsEndOffset,
6250 kSize> BodyDescriptor;
6251
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006252 // Compares this map to another to see if they describe equivalent objects.
6253 // If |mode| is set to CLEAR_INOBJECT_PROPERTIES, |other| is treated as if
6254 // it had exactly zero inobject properties.
6255 // The "shared" flags of both this map and |other| are ignored.
6256 bool EquivalentToForNormalization(Map* other, PropertyNormalizationMode mode);
6257
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006258 // Returns true if given field is unboxed double.
6259 inline bool IsUnboxedDoubleField(FieldIndex index);
6260
6261#if TRACE_MAPS
6262 static void TraceTransition(const char* what, Map* from, Map* to, Name* name);
6263 static void TraceAllTransitions(Map* map);
6264#endif
6265
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006266 static inline Handle<Map> AddMissingTransitionsForTesting(
6267 Handle<Map> split_map, Handle<DescriptorArray> descriptors,
6268 Handle<LayoutDescriptor> full_layout_descriptor);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006269
Steve Blocka7e24c12009-10-30 11:49:00 +00006270 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006271 static void ConnectTransition(Handle<Map> parent, Handle<Map> child,
6272 Handle<Name> name, SimpleTransitionFlag flag);
6273
6274 bool EquivalentToForTransition(Map* other);
6275 static Handle<Map> RawCopy(Handle<Map> map, int instance_size);
6276 static Handle<Map> ShareDescriptor(Handle<Map> map,
6277 Handle<DescriptorArray> descriptors,
6278 Descriptor* descriptor);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006279 static Handle<Map> AddMissingTransitions(
6280 Handle<Map> map, Handle<DescriptorArray> descriptors,
6281 Handle<LayoutDescriptor> full_layout_descriptor);
6282 static void InstallDescriptors(
6283 Handle<Map> parent_map, Handle<Map> child_map, int new_descriptor,
6284 Handle<DescriptorArray> descriptors,
6285 Handle<LayoutDescriptor> full_layout_descriptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006286 static Handle<Map> CopyAddDescriptor(Handle<Map> map,
6287 Descriptor* descriptor,
6288 TransitionFlag flag);
6289 static Handle<Map> CopyReplaceDescriptors(
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006290 Handle<Map> map, Handle<DescriptorArray> descriptors,
6291 Handle<LayoutDescriptor> layout_descriptor, TransitionFlag flag,
6292 MaybeHandle<Name> maybe_name, const char* reason,
6293 SimpleTransitionFlag simple_flag);
6294
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006295 static Handle<Map> CopyReplaceDescriptor(Handle<Map> map,
6296 Handle<DescriptorArray> descriptors,
6297 Descriptor* descriptor,
6298 int index,
6299 TransitionFlag flag);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006300 static MUST_USE_RESULT MaybeHandle<Map> TryReconfigureExistingProperty(
6301 Handle<Map> map, int descriptor, PropertyKind kind,
6302 PropertyAttributes attributes, const char** reason);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006303
6304 static Handle<Map> CopyNormalized(Handle<Map> map,
6305 PropertyNormalizationMode mode);
6306
6307 // Fires when the layout of an object with a leaf map changes.
6308 // This includes adding transitions to the leaf map or changing
6309 // the descriptor array.
6310 inline void NotifyLeafMapLayoutChange();
6311
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006312 void DeprecateTransitionTree();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006313
6314 void ReplaceDescriptors(DescriptorArray* new_descriptors,
6315 LayoutDescriptor* new_layout_descriptor);
6316
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006317
6318 Map* FindLastMatchMap(int verbatim, int length, DescriptorArray* descriptors);
6319
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006320 // Update field type of the given descriptor to new representation and new
6321 // type. The type must be prepared for storing in descriptor array:
6322 // it must be either a simple type or a map wrapped in a weak cell.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006323 void UpdateFieldType(int descriptor_number, Handle<Name> name,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006324 Representation new_representation,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006325 Handle<Object> new_wrapped_type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006326
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006327 void PrintReconfiguration(FILE* file, int modify_index, PropertyKind kind,
6328 PropertyAttributes attributes);
Ben Murdoch097c5b22016-05-18 11:27:45 +01006329 void PrintGeneralization(FILE* file, const char* reason, int modify_index,
6330 int split, int descriptors, bool constant_to_field,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006331 Representation old_representation,
6332 Representation new_representation,
Ben Murdoch097c5b22016-05-18 11:27:45 +01006333 MaybeHandle<FieldType> old_field_type,
6334 MaybeHandle<Object> old_value,
6335 MaybeHandle<FieldType> new_field_type,
6336 MaybeHandle<Object> new_value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006337
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006338 static const int kFastPropertiesSoftLimit = 12;
6339 static const int kMaxFastProperties = 128;
6340
Steve Blocka7e24c12009-10-30 11:49:00 +00006341 DISALLOW_IMPLICIT_CONSTRUCTORS(Map);
6342};
6343
6344
6345// An abstract superclass, a marker class really, for simple structure classes.
Ben Murdoch257744e2011-11-30 15:57:28 +00006346// It doesn't carry much functionality but allows struct classes to be
Steve Blocka7e24c12009-10-30 11:49:00 +00006347// identified in the type system.
6348class Struct: public HeapObject {
6349 public:
6350 inline void InitializeBody(int object_size);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006351 DECLARE_CAST(Struct)
6352};
6353
6354
6355// A simple one-element struct, useful where smis need to be boxed.
6356class Box : public Struct {
6357 public:
6358 // [value]: the boxed contents.
6359 DECL_ACCESSORS(value, Object)
6360
6361 DECLARE_CAST(Box)
6362
6363 // Dispatched behavior.
6364 DECLARE_PRINTER(Box)
6365 DECLARE_VERIFIER(Box)
6366
6367 static const int kValueOffset = HeapObject::kHeaderSize;
6368 static const int kSize = kValueOffset + kPointerSize;
6369
6370 private:
6371 DISALLOW_IMPLICIT_CONSTRUCTORS(Box);
Steve Blocka7e24c12009-10-30 11:49:00 +00006372};
6373
6374
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006375// Container for metadata stored on each prototype map.
6376class PrototypeInfo : public Struct {
6377 public:
6378 static const int UNREGISTERED = -1;
6379
6380 // [prototype_users]: WeakFixedArray containing maps using this prototype,
6381 // or Smi(0) if uninitialized.
6382 DECL_ACCESSORS(prototype_users, Object)
6383 // [registry_slot]: Slot in prototype's user registry where this user
6384 // is stored. Returns UNREGISTERED if this prototype has not been registered.
6385 inline int registry_slot() const;
6386 inline void set_registry_slot(int slot);
6387 // [validity_cell]: Cell containing the validity bit for prototype chains
6388 // going through this object, or Smi(0) if uninitialized.
6389 // When a prototype object changes its map, then both its own validity cell
6390 // and those of all "downstream" prototypes are invalidated; handlers for a
6391 // given receiver embed the currently valid cell for that receiver's prototype
6392 // during their compilation and check it on execution.
6393 DECL_ACCESSORS(validity_cell, Object)
6394
6395 DECLARE_CAST(PrototypeInfo)
6396
6397 // Dispatched behavior.
6398 DECLARE_PRINTER(PrototypeInfo)
6399 DECLARE_VERIFIER(PrototypeInfo)
6400
6401 static const int kPrototypeUsersOffset = HeapObject::kHeaderSize;
6402 static const int kRegistrySlotOffset = kPrototypeUsersOffset + kPointerSize;
6403 static const int kValidityCellOffset = kRegistrySlotOffset + kPointerSize;
6404 static const int kConstructorNameOffset = kValidityCellOffset + kPointerSize;
6405 static const int kSize = kConstructorNameOffset + kPointerSize;
6406
6407 private:
6408 DISALLOW_IMPLICIT_CONSTRUCTORS(PrototypeInfo);
6409};
6410
6411
6412// Pair used to store both a ScopeInfo and an extension object in the extension
6413// slot of a block context. Needed in the rare case where a declaration block
6414// scope (a "varblock" as used to desugar parameter destructuring) also contains
6415// a sloppy direct eval. (In no other case both are needed at the same time.)
6416class SloppyBlockWithEvalContextExtension : public Struct {
6417 public:
6418 // [scope_info]: Scope info.
6419 DECL_ACCESSORS(scope_info, ScopeInfo)
6420 // [extension]: Extension object.
6421 DECL_ACCESSORS(extension, JSObject)
6422
6423 DECLARE_CAST(SloppyBlockWithEvalContextExtension)
6424
6425 // Dispatched behavior.
6426 DECLARE_PRINTER(SloppyBlockWithEvalContextExtension)
6427 DECLARE_VERIFIER(SloppyBlockWithEvalContextExtension)
6428
6429 static const int kScopeInfoOffset = HeapObject::kHeaderSize;
6430 static const int kExtensionOffset = kScopeInfoOffset + kPointerSize;
6431 static const int kSize = kExtensionOffset + kPointerSize;
6432
6433 private:
6434 DISALLOW_IMPLICIT_CONSTRUCTORS(SloppyBlockWithEvalContextExtension);
6435};
6436
6437
Steve Blocka7e24c12009-10-30 11:49:00 +00006438// Script describes a script which has been added to the VM.
6439class Script: public Struct {
6440 public:
6441 // Script types.
6442 enum Type {
6443 TYPE_NATIVE = 0,
6444 TYPE_EXTENSION = 1,
6445 TYPE_NORMAL = 2
6446 };
6447
6448 // Script compilation types.
6449 enum CompilationType {
6450 COMPILATION_TYPE_HOST = 0,
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08006451 COMPILATION_TYPE_EVAL = 1
Steve Blocka7e24c12009-10-30 11:49:00 +00006452 };
6453
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006454 // Script compilation state.
6455 enum CompilationState {
6456 COMPILATION_STATE_INITIAL = 0,
6457 COMPILATION_STATE_COMPILED = 1
6458 };
6459
Steve Blocka7e24c12009-10-30 11:49:00 +00006460 // [source]: the script source.
6461 DECL_ACCESSORS(source, Object)
6462
6463 // [name]: the script name.
6464 DECL_ACCESSORS(name, Object)
6465
6466 // [id]: the script id.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006467 DECL_INT_ACCESSORS(id)
Steve Blocka7e24c12009-10-30 11:49:00 +00006468
6469 // [line_offset]: script line offset in resource from where it was extracted.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006470 DECL_INT_ACCESSORS(line_offset)
Steve Blocka7e24c12009-10-30 11:49:00 +00006471
6472 // [column_offset]: script column offset in resource from where it was
6473 // extracted.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006474 DECL_INT_ACCESSORS(column_offset)
Steve Blocka7e24c12009-10-30 11:49:00 +00006475
Steve Blocka7e24c12009-10-30 11:49:00 +00006476 // [context_data]: context data for the context this script was compiled in.
6477 DECL_ACCESSORS(context_data, Object)
6478
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006479 // [wrapper]: the wrapper cache. This is either undefined or a WeakCell.
6480 DECL_ACCESSORS(wrapper, HeapObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00006481
6482 // [type]: the script type.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006483 DECL_INT_ACCESSORS(type)
Steve Blocka7e24c12009-10-30 11:49:00 +00006484
Steve Blockd0582a62009-12-15 09:54:21 +00006485 // [line_ends]: FixedArray of line ends positions.
Steve Blocka7e24c12009-10-30 11:49:00 +00006486 DECL_ACCESSORS(line_ends, Object)
6487
Ben Murdoch097c5b22016-05-18 11:27:45 +01006488 // [eval_from_shared]: for eval scripts the shared function info for the
Steve Blockd0582a62009-12-15 09:54:21 +00006489 // function from which eval was called.
6490 DECL_ACCESSORS(eval_from_shared, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00006491
6492 // [eval_from_instructions_offset]: the instruction offset in the code for the
6493 // function from which eval was called where eval was called.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006494 DECL_INT_ACCESSORS(eval_from_instructions_offset)
6495
6496 // [shared_function_infos]: weak fixed array containing all shared
6497 // function infos created from this script.
6498 DECL_ACCESSORS(shared_function_infos, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00006499
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006500 // [flags]: Holds an exciting bitfield.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006501 DECL_INT_ACCESSORS(flags)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006502
6503 // [source_url]: sourceURL from magic comment
6504 DECL_ACCESSORS(source_url, Object)
6505
6506 // [source_url]: sourceMappingURL magic comment
6507 DECL_ACCESSORS(source_mapping_url, Object)
6508
6509 // [compilation_type]: how the the script was compiled. Encoded in the
6510 // 'flags' field.
6511 inline CompilationType compilation_type();
6512 inline void set_compilation_type(CompilationType type);
6513
6514 // [compilation_state]: determines whether the script has already been
6515 // compiled. Encoded in the 'flags' field.
6516 inline CompilationState compilation_state();
6517 inline void set_compilation_state(CompilationState state);
6518
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006519 // [hide_source]: determines whether the script source can be exposed as
6520 // function source. Encoded in the 'flags' field.
6521 inline bool hide_source();
6522 inline void set_hide_source(bool value);
6523
6524 // [origin_options]: optional attributes set by the embedder via ScriptOrigin,
6525 // and used by the embedder to make decisions about the script. V8 just passes
6526 // this through. Encoded in the 'flags' field.
6527 inline v8::ScriptOriginOptions origin_options();
6528 inline void set_origin_options(ScriptOriginOptions origin_options);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006529
6530 DECLARE_CAST(Script)
Steve Blocka7e24c12009-10-30 11:49:00 +00006531
Steve Block3ce2e202009-11-05 08:53:23 +00006532 // If script source is an external string, check that the underlying
6533 // resource is accessible. Otherwise, always return true.
6534 inline bool HasValidSource();
6535
Ben Murdoch097c5b22016-05-18 11:27:45 +01006536 // Convert code offset into column number.
6537 static int GetColumnNumber(Handle<Script> script, int code_offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006538
Ben Murdoch097c5b22016-05-18 11:27:45 +01006539 // Convert code offset into (zero-based) line number.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006540 // The non-handlified version does not allocate, but may be much slower.
Ben Murdoch097c5b22016-05-18 11:27:45 +01006541 static int GetLineNumber(Handle<Script> script, int code_offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006542 int GetLineNumber(int code_pos);
6543
6544 static Handle<Object> GetNameOrSourceURL(Handle<Script> script);
6545
Ben Murdoch097c5b22016-05-18 11:27:45 +01006546 // Init line_ends array with source code positions of line ends.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006547 static void InitLineEnds(Handle<Script> script);
6548
6549 // Get the JS object wrapping the given script; create it if none exists.
6550 static Handle<JSObject> GetWrapper(Handle<Script> script);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006551
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006552 // Look through the list of existing shared function infos to find one
6553 // that matches the function literal. Return empty handle if not found.
6554 MaybeHandle<SharedFunctionInfo> FindSharedFunctionInfo(FunctionLiteral* fun);
6555
6556 // Iterate over all script objects on the heap.
6557 class Iterator {
6558 public:
6559 explicit Iterator(Isolate* isolate);
6560 Script* Next();
6561
6562 private:
6563 WeakFixedArray::Iterator iterator_;
6564 DISALLOW_COPY_AND_ASSIGN(Iterator);
6565 };
6566
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006567 // Dispatched behavior.
6568 DECLARE_PRINTER(Script)
6569 DECLARE_VERIFIER(Script)
Steve Blocka7e24c12009-10-30 11:49:00 +00006570
6571 static const int kSourceOffset = HeapObject::kHeaderSize;
6572 static const int kNameOffset = kSourceOffset + kPointerSize;
6573 static const int kLineOffsetOffset = kNameOffset + kPointerSize;
6574 static const int kColumnOffsetOffset = kLineOffsetOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006575 static const int kContextOffset = kColumnOffsetOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006576 static const int kWrapperOffset = kContextOffset + kPointerSize;
6577 static const int kTypeOffset = kWrapperOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006578 static const int kLineEndsOffset = kTypeOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006579 static const int kIdOffset = kLineEndsOffset + kPointerSize;
Steve Blockd0582a62009-12-15 09:54:21 +00006580 static const int kEvalFromSharedOffset = kIdOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006581 static const int kEvalFrominstructionsOffsetOffset =
Steve Blockd0582a62009-12-15 09:54:21 +00006582 kEvalFromSharedOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006583 static const int kSharedFunctionInfosOffset =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006584 kEvalFrominstructionsOffsetOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006585 static const int kFlagsOffset = kSharedFunctionInfosOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006586 static const int kSourceUrlOffset = kFlagsOffset + kPointerSize;
6587 static const int kSourceMappingUrlOffset = kSourceUrlOffset + kPointerSize;
6588 static const int kSize = kSourceMappingUrlOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00006589
6590 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006591 int GetLineNumberWithArray(int code_pos);
6592
6593 // Bit positions in the flags field.
6594 static const int kCompilationTypeBit = 0;
6595 static const int kCompilationStateBit = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006596 static const int kHideSourceBit = 2;
6597 static const int kOriginOptionsShift = 3;
6598 static const int kOriginOptionsSize = 3;
6599 static const int kOriginOptionsMask = ((1 << kOriginOptionsSize) - 1)
6600 << kOriginOptionsShift;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006601
Steve Blocka7e24c12009-10-30 11:49:00 +00006602 DISALLOW_IMPLICIT_CONSTRUCTORS(Script);
6603};
6604
6605
Ben Murdochb0fe1622011-05-05 13:52:32 +01006606// List of builtin functions we want to identify to improve code
6607// generation.
6608//
6609// Each entry has a name of a global object property holding an object
6610// optionally followed by ".prototype", a name of a builtin function
6611// on the object (the one the id is set for), and a label.
6612//
6613// Installation of ids for the selected builtin functions is handled
6614// by the bootstrapper.
Ben Murdochda12d292016-06-02 14:46:10 +01006615#define FUNCTIONS_WITH_ID_LIST(V) \
6616 V(Array.prototype, indexOf, ArrayIndexOf) \
6617 V(Array.prototype, lastIndexOf, ArrayLastIndexOf) \
6618 V(Array.prototype, push, ArrayPush) \
6619 V(Array.prototype, pop, ArrayPop) \
6620 V(Array.prototype, shift, ArrayShift) \
6621 V(Function.prototype, apply, FunctionApply) \
6622 V(Function.prototype, call, FunctionCall) \
6623 V(Object.prototype, hasOwnProperty, ObjectHasOwnProperty) \
6624 V(String.prototype, charCodeAt, StringCharCodeAt) \
6625 V(String.prototype, charAt, StringCharAt) \
6626 V(String.prototype, concat, StringConcat) \
6627 V(String.prototype, toLowerCase, StringToLowerCase) \
6628 V(String.prototype, toUpperCase, StringToUpperCase) \
6629 V(String, fromCharCode, StringFromCharCode) \
6630 V(Math, random, MathRandom) \
6631 V(Math, floor, MathFloor) \
6632 V(Math, round, MathRound) \
6633 V(Math, ceil, MathCeil) \
6634 V(Math, abs, MathAbs) \
6635 V(Math, log, MathLog) \
6636 V(Math, exp, MathExp) \
6637 V(Math, sqrt, MathSqrt) \
6638 V(Math, pow, MathPow) \
6639 V(Math, max, MathMax) \
6640 V(Math, min, MathMin) \
6641 V(Math, cos, MathCos) \
6642 V(Math, sin, MathSin) \
6643 V(Math, tan, MathTan) \
6644 V(Math, acos, MathAcos) \
6645 V(Math, asin, MathAsin) \
6646 V(Math, atan, MathAtan) \
6647 V(Math, atan2, MathAtan2) \
6648 V(Math, imul, MathImul) \
6649 V(Math, clz32, MathClz32) \
6650 V(Math, fround, MathFround) \
6651 V(Math, trunc, MathTrunc)
Ben Murdochb0fe1622011-05-05 13:52:32 +01006652
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006653#define ATOMIC_FUNCTIONS_WITH_ID_LIST(V) \
6654 V(Atomics, load, AtomicsLoad) \
6655 V(Atomics, store, AtomicsStore)
6656
Ben Murdochb0fe1622011-05-05 13:52:32 +01006657enum BuiltinFunctionId {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006658 kArrayCode,
Ben Murdochda12d292016-06-02 14:46:10 +01006659 kGeneratorObjectNext,
6660 kGeneratorObjectReturn,
6661 kGeneratorObjectThrow,
Ben Murdochb0fe1622011-05-05 13:52:32 +01006662#define DECLARE_FUNCTION_ID(ignored1, ignore2, name) \
6663 k##name,
6664 FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006665 ATOMIC_FUNCTIONS_WITH_ID_LIST(DECLARE_FUNCTION_ID)
Ben Murdochb0fe1622011-05-05 13:52:32 +01006666#undef DECLARE_FUNCTION_ID
6667 // Fake id for a special case of Math.pow. Note, it continues the
6668 // list of math functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006669 kMathPowHalf
Ben Murdochb0fe1622011-05-05 13:52:32 +01006670};
6671
6672
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006673// Result of searching in an optimized code map of a SharedFunctionInfo. Note
6674// that both {code} and {literals} can be NULL to pass search result status.
6675struct CodeAndLiterals {
6676 Code* code; // Cached optimized code.
6677 LiteralsArray* literals; // Cached literals array.
6678};
6679
6680
Steve Blocka7e24c12009-10-30 11:49:00 +00006681// SharedFunctionInfo describes the JSFunction information that can be
6682// shared by multiple instances of the function.
6683class SharedFunctionInfo: public HeapObject {
6684 public:
6685 // [name]: Function name.
6686 DECL_ACCESSORS(name, Object)
6687
6688 // [code]: Function code.
6689 DECL_ACCESSORS(code, Code)
Ben Murdoch097c5b22016-05-18 11:27:45 +01006690
Ben Murdochda12d292016-06-02 14:46:10 +01006691 // Get the abstract code associated with the function, which will either be
6692 // a Code object or a BytecodeArray.
6693 inline AbstractCode* abstract_code();
6694
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006695 inline void ReplaceCode(Code* code);
6696
6697 // [optimized_code_map]: Map from native context to optimized code
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006698 // and a shared literals array.
6699 DECL_ACCESSORS(optimized_code_map, FixedArray)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006700
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006701 // Returns entry from optimized code map for specified context and OSR entry.
6702 // Note that {code == nullptr, literals == nullptr} indicates no matching
6703 // entry has been found, whereas {code, literals == nullptr} indicates that
6704 // code is context-independent.
6705 CodeAndLiterals SearchOptimizedCodeMap(Context* native_context,
6706 BailoutId osr_ast_id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006707
6708 // Clear optimized code map.
6709 void ClearOptimizedCodeMap();
6710
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006711 // We have a special root FixedArray with the right shape and values
6712 // to represent the cleared optimized code map. This predicate checks
6713 // if that root is installed.
6714 inline bool OptimizedCodeMapIsCleared() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006715
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006716 // Removes a specific optimized code object from the optimized code map.
6717 // In case of non-OSR the code reference is cleared from the cache entry but
6718 // the entry itself is left in the map in order to proceed sharing literals.
6719 void EvictFromOptimizedCodeMap(Code* optimized_code, const char* reason);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006720
6721 // Trims the optimized code map after entries have been removed.
6722 void TrimOptimizedCodeMap(int shrink_by);
6723
Ben Murdochda12d292016-06-02 14:46:10 +01006724 // Add or update entry in the optimized code map for context-independent code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006725 static void AddSharedCodeToOptimizedCodeMap(Handle<SharedFunctionInfo> shared,
6726 Handle<Code> code);
6727
Ben Murdochda12d292016-06-02 14:46:10 +01006728 // Add or update entry in the optimized code map for context-dependent code.
6729 // If {code} is not given, then an existing entry's code won't be overwritten.
6730 static void AddToOptimizedCodeMap(Handle<SharedFunctionInfo> shared,
6731 Handle<Context> native_context,
6732 MaybeHandle<Code> code,
6733 Handle<LiteralsArray> literals,
6734 BailoutId osr_ast_id);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006735
6736 // Set up the link between shared function info and the script. The shared
6737 // function info is added to the list on the script.
6738 static void SetScript(Handle<SharedFunctionInfo> shared,
6739 Handle<Object> script_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006740
6741 // Layout description of the optimized code map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006742 static const int kSharedCodeIndex = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006743 static const int kEntriesStart = 1;
6744 static const int kContextOffset = 0;
6745 static const int kCachedCodeOffset = 1;
6746 static const int kLiteralsOffset = 2;
6747 static const int kOsrAstIdOffset = 3;
6748 static const int kEntryLength = 4;
6749 static const int kInitialLength = kEntriesStart + kEntryLength;
Steve Blocka7e24c12009-10-30 11:49:00 +00006750
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006751 static const int kNotFound = -1;
6752
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006753 // [scope_info]: Scope info.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006754 DECL_ACCESSORS(scope_info, ScopeInfo)
Ben Murdoch3bec4d22010-07-22 14:51:16 +01006755
Steve Blocka7e24c12009-10-30 11:49:00 +00006756 // [construct stub]: Code stub for constructing instances of this function.
6757 DECL_ACCESSORS(construct_stub, Code)
6758
6759 // Returns if this function has been compiled to native code yet.
6760 inline bool is_compiled();
6761
6762 // [length]: The function length - usually the number of declared parameters.
6763 // Use up to 2^30 parameters.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006764 inline int length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006765 inline void set_length(int value);
6766
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006767 // [internal formal parameter count]: The declared number of parameters.
6768 // For subclass constructors, also includes new.target.
6769 // The size of function's frame is internal_formal_parameter_count + 1.
6770 inline int internal_formal_parameter_count() const;
6771 inline void set_internal_formal_parameter_count(int value);
Steve Blocka7e24c12009-10-30 11:49:00 +00006772
6773 // Set the formal parameter count so the function code will be
6774 // called without using argument adaptor frames.
6775 inline void DontAdaptArguments();
6776
6777 // [expected_nof_properties]: Expected number of properties for the function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006778 inline int expected_nof_properties() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006779 inline void set_expected_nof_properties(int value);
6780
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006781 // [feedback_vector] - accumulates ast node feedback from full-codegen and
6782 // (increasingly) from crankshafted code where sufficient feedback isn't
6783 // available.
6784 DECL_ACCESSORS(feedback_vector, TypeFeedbackVector)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006785
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006786 // Unconditionally clear the type feedback vector (including vector ICs).
6787 void ClearTypeFeedbackInfo();
6788
6789 // Clear the type feedback vector with a more subtle policy at GC time.
6790 void ClearTypeFeedbackInfoAtGCTime();
6791
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006792#if TRACE_MAPS
6793 // [unique_id] - For --trace-maps purposes, an identifier that's persistent
6794 // even if the GC moves this SharedFunctionInfo.
6795 inline int unique_id() const;
6796 inline void set_unique_id(int value);
6797#endif
6798
Steve Blocka7e24c12009-10-30 11:49:00 +00006799 // [instance class name]: class name for instances.
6800 DECL_ACCESSORS(instance_class_name, Object)
6801
Steve Block6ded16b2010-05-10 14:33:55 +01006802 // [function data]: This field holds some additional data for function.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006803 // Currently it has one of:
6804 // - a FunctionTemplateInfo to make benefit the API [IsApiFunction()].
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006805 // - a BytecodeArray for the interpreter [HasBytecodeArray()].
Steve Blocka7e24c12009-10-30 11:49:00 +00006806 DECL_ACCESSORS(function_data, Object)
6807
Steve Block6ded16b2010-05-10 14:33:55 +01006808 inline bool IsApiFunction();
6809 inline FunctionTemplateInfo* get_api_func_data();
Ben Murdochda12d292016-06-02 14:46:10 +01006810 inline void set_api_func_data(FunctionTemplateInfo* data);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006811 inline bool HasBytecodeArray();
6812 inline BytecodeArray* bytecode_array();
Ben Murdochda12d292016-06-02 14:46:10 +01006813 inline void set_bytecode_array(BytecodeArray* bytecode);
6814 inline void ClearBytecodeArray();
6815
6816 // [function identifier]: This field holds an additional identifier for the
6817 // function.
6818 // - a Smi identifying a builtin function [HasBuiltinFunctionId()].
6819 // - a String identifying the function's inferred name [HasInferredName()].
6820 // The inferred_name is inferred from variable or property
6821 // assignment of this function. It is used to facilitate debugging and
6822 // profiling of JavaScript code written in OO style, where almost
6823 // all functions are anonymous but are assigned to object
6824 // properties.
6825 DECL_ACCESSORS(function_identifier, Object)
6826
6827 inline bool HasBuiltinFunctionId();
6828 inline BuiltinFunctionId builtin_function_id();
6829 inline void set_builtin_function_id(BuiltinFunctionId id);
6830 inline bool HasInferredName();
6831 inline String* inferred_name();
6832 inline void set_inferred_name(String* inferred_name);
Steve Block6ded16b2010-05-10 14:33:55 +01006833
Steve Blocka7e24c12009-10-30 11:49:00 +00006834 // [script info]: Script from which the function originates.
6835 DECL_ACCESSORS(script, Object)
6836
Steve Block6ded16b2010-05-10 14:33:55 +01006837 // [num_literals]: Number of literals used by this function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006838 inline int num_literals() const;
Steve Block6ded16b2010-05-10 14:33:55 +01006839 inline void set_num_literals(int value);
6840
Steve Blocka7e24c12009-10-30 11:49:00 +00006841 // [start_position_and_type]: Field used to store both the source code
6842 // position, whether or not the function is a function expression,
6843 // and whether or not the function is a toplevel function. The two
6844 // least significants bit indicates whether the function is an
6845 // expression and the rest contains the source code position.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006846 inline int start_position_and_type() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006847 inline void set_start_position_and_type(int value);
6848
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006849 // The function is subject to debugging if a debug info is attached.
6850 inline bool HasDebugInfo();
6851 inline DebugInfo* GetDebugInfo();
6852
6853 // A function has debug code if the compiled code has debug break slots.
6854 inline bool HasDebugCode();
6855
Steve Blocka7e24c12009-10-30 11:49:00 +00006856 // [debug info]: Debug information.
6857 DECL_ACCESSORS(debug_info, Object)
6858
Ben Murdochf87a2032010-10-22 12:50:53 +01006859 // The function's name if it is non-empty, otherwise the inferred name.
6860 String* DebugName();
6861
Ben Murdochda12d292016-06-02 14:46:10 +01006862 // Used for flags such as --hydrogen-filter.
6863 bool PassesFilter(const char* raw_filter);
6864
Steve Blocka7e24c12009-10-30 11:49:00 +00006865 // Position of the 'function' token in the script source.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006866 inline int function_token_position() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006867 inline void set_function_token_position(int function_token_position);
6868
6869 // Position of this function in the script source.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006870 inline int start_position() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006871 inline void set_start_position(int start_position);
6872
6873 // End position of this function in the script source.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006874 inline int end_position() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006875 inline void set_end_position(int end_position);
6876
Ben Murdoch097c5b22016-05-18 11:27:45 +01006877 // Is this function a named function expression in the source code.
6878 DECL_BOOLEAN_ACCESSORS(is_named_expression)
Steve Blocka7e24c12009-10-30 11:49:00 +00006879
6880 // Is this function a top-level function (scripts, evals).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006881 DECL_BOOLEAN_ACCESSORS(is_toplevel)
Steve Blocka7e24c12009-10-30 11:49:00 +00006882
6883 // Bit field containing various information collected by the compiler to
6884 // drive optimization.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006885 inline int compiler_hints() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00006886 inline void set_compiler_hints(int value);
6887
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006888 inline int ast_node_count() const;
Ben Murdoch8f9999f2012-04-23 10:39:17 +01006889 inline void set_ast_node_count(int count);
6890
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006891 inline int profiler_ticks() const;
6892 inline void set_profiler_ticks(int ticks);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006893
Ben Murdoch8f9999f2012-04-23 10:39:17 +01006894 // Inline cache age is used to infer whether the function survived a context
6895 // disposal or not. In the former case we reset the opt_count.
6896 inline int ic_age();
6897 inline void set_ic_age(int age);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006898
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006899 // Indicates if this function can be lazy compiled.
6900 // This is used to determine if we can safely flush code from a function
6901 // when doing GC if we expect that the function will no longer be used.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006902 DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01006903
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006904 // Indicates if this function can be lazy compiled without a context.
6905 // This is used to determine if we can force compilation without reaching
6906 // the function through program execution but through other means (e.g. heap
6907 // iteration by the debugger).
6908 DECL_BOOLEAN_ACCESSORS(allows_lazy_compilation_without_context)
Iain Merrick75681382010-08-19 15:07:18 +01006909
Ben Murdochb0fe1622011-05-05 13:52:32 +01006910 // Indicates whether optimizations have been disabled for this
6911 // shared function info. If a function is repeatedly optimized or if
6912 // we cannot optimize the function we disable optimization to avoid
6913 // spending time attempting to optimize it again.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006914 DECL_BOOLEAN_ACCESSORS(optimization_disabled)
Ben Murdochb0fe1622011-05-05 13:52:32 +01006915
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006916 // Indicates the language mode.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006917 inline LanguageMode language_mode();
6918 inline void set_language_mode(LanguageMode language_mode);
Steve Block1e0659c2011-05-24 12:43:12 +01006919
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006920 // False if the function definitely does not allocate an arguments object.
6921 DECL_BOOLEAN_ACCESSORS(uses_arguments)
6922
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006923 // Indicates that this function uses a super property (or an eval that may
6924 // use a super property).
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006925 // This is needed to set up the [[HomeObject]] on the function instance.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006926 DECL_BOOLEAN_ACCESSORS(needs_home_object)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006927
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006928 // True if the function has any duplicated parameter names.
6929 DECL_BOOLEAN_ACCESSORS(has_duplicate_parameters)
6930
6931 // Indicates whether the function is a native function.
Ben Murdoch589d6972011-11-30 16:04:58 +00006932 // These needs special treatment in .call and .apply since
Ben Murdoch257744e2011-11-30 15:57:28 +00006933 // null passed as the receiver should not be translated to the
6934 // global object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006935 DECL_BOOLEAN_ACCESSORS(native)
6936
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006937 // Indicate that this function should always be inlined in optimized code.
6938 DECL_BOOLEAN_ACCESSORS(force_inline)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006939
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006940 // Indicates that the function was created by the Function function.
6941 // Though it's anonymous, toString should treat it as if it had the name
6942 // "anonymous". We don't set the name itself so that the system does not
6943 // see a binding for it.
6944 DECL_BOOLEAN_ACCESSORS(name_should_print_as_anonymous)
6945
Ben Murdoch097c5b22016-05-18 11:27:45 +01006946 // Indicates that the function is either an anonymous expression
6947 // or an arrow function (the name field can be set through the API,
6948 // which does not change this flag).
6949 DECL_BOOLEAN_ACCESSORS(is_anonymous_expression)
Ben Murdoch257744e2011-11-30 15:57:28 +00006950
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006951 // Is this a function or top-level/eval code.
6952 DECL_BOOLEAN_ACCESSORS(is_function)
6953
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006954 // Indicates that code for this function cannot be compiled with Crankshaft.
6955 DECL_BOOLEAN_ACCESSORS(dont_crankshaft)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006956
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006957 // Indicates that code for this function cannot be flushed.
6958 DECL_BOOLEAN_ACCESSORS(dont_flush)
6959
6960 // Indicates that this function is a generator.
6961 DECL_BOOLEAN_ACCESSORS(is_generator)
6962
6963 // Indicates that this function is an arrow function.
6964 DECL_BOOLEAN_ACCESSORS(is_arrow)
6965
6966 // Indicates that this function is a concise method.
6967 DECL_BOOLEAN_ACCESSORS(is_concise_method)
6968
Ben Murdoch097c5b22016-05-18 11:27:45 +01006969 // Indicates that this function is a getter.
6970 DECL_BOOLEAN_ACCESSORS(is_getter_function)
6971
6972 // Indicates that this function is a setter.
6973 DECL_BOOLEAN_ACCESSORS(is_setter_function)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006974
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006975 // Indicates that this function is a default constructor.
6976 DECL_BOOLEAN_ACCESSORS(is_default_constructor)
6977
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006978 // Indicates that this function is an asm function.
6979 DECL_BOOLEAN_ACCESSORS(asm_function)
6980
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006981 // Indicates that the the shared function info is deserialized from cache.
6982 DECL_BOOLEAN_ACCESSORS(deserialized)
6983
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00006984 // Indicates that the the shared function info has never been compiled before.
6985 DECL_BOOLEAN_ACCESSORS(never_compiled)
6986
Ben Murdoch097c5b22016-05-18 11:27:45 +01006987 // Whether this function was created from a FunctionDeclaration.
6988 DECL_BOOLEAN_ACCESSORS(is_declaration)
6989
Ben Murdochb8a8cc12014-11-26 15:28:44 +00006990 inline FunctionKind kind();
6991 inline void set_kind(FunctionKind kind);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01006992
Ben Murdochb0fe1622011-05-05 13:52:32 +01006993 // Indicates whether or not the code in the shared function support
6994 // deoptimization.
6995 inline bool has_deoptimization_support();
6996
6997 // Enable deoptimization support through recompiled code.
6998 void EnableDeoptimizationSupport(Code* recompiled);
6999
Ben Murdoch257744e2011-11-30 15:57:28 +00007000 // Disable (further) attempted optimization of all functions sharing this
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007001 // shared function info.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007002 void DisableOptimization(BailoutReason reason);
Ben Murdoch257744e2011-11-30 15:57:28 +00007003
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007004 inline BailoutReason disable_optimization_reason();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007005
7006 // Lookup the bailout ID and DCHECK that it exists in the non-optimized
Ben Murdochb0fe1622011-05-05 13:52:32 +01007007 // code, returns whether it asserted (i.e., always true if assertions are
7008 // disabled).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007009 bool VerifyBailoutId(BailoutId id);
Steve Blocka7e24c12009-10-30 11:49:00 +00007010
7011 // [source code]: Source code for the function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007012 bool HasSourceCode() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007013 Handle<Object> GetSourceCode();
Steve Blocka7e24c12009-10-30 11:49:00 +00007014
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007015 // Number of times the function was optimized.
Ben Murdochb0fe1622011-05-05 13:52:32 +01007016 inline int opt_count();
7017 inline void set_opt_count(int opt_count);
7018
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007019 // Number of times the function was deoptimized.
7020 inline void set_deopt_count(int value);
7021 inline int deopt_count();
7022 inline void increment_deopt_count();
7023
7024 // Number of time we tried to re-enable optimization after it
7025 // was disabled due to high number of deoptimizations.
7026 inline void set_opt_reenable_tries(int value);
7027 inline int opt_reenable_tries();
7028
7029 inline void TryReenableOptimization();
7030
7031 // Stores deopt_count, opt_reenable_tries and ic_age as bit-fields.
7032 inline void set_counters(int value);
7033 inline int counters() const;
7034
7035 // Stores opt_count and bailout_reason as bit-fields.
7036 inline void set_opt_count_and_bailout_reason(int value);
7037 inline int opt_count_and_bailout_reason() const;
7038
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007039 inline void set_disable_optimization_reason(BailoutReason reason);
7040
7041 // Tells whether this function should be subject to debugging.
7042 inline bool IsSubjectToDebugging();
7043
7044 // Whether this function is defined in native code or extensions.
7045 inline bool IsBuiltin();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007046
7047 // Check whether or not this function is inlineable.
7048 bool IsInlineable();
7049
Ben Murdochb0fe1622011-05-05 13:52:32 +01007050 // Source size of this function.
7051 int SourceSize();
7052
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007053 // Returns `false` if formal parameters include rest parameters, optional
7054 // parameters, or destructuring parameters.
7055 // TODO(caitp): make this a flag set during parsing
7056 inline bool has_simple_parameters();
Steve Blocka7e24c12009-10-30 11:49:00 +00007057
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007058 // Initialize a SharedFunctionInfo from a parsed function literal.
7059 static void InitFromFunctionLiteral(Handle<SharedFunctionInfo> shared_info,
7060 FunctionLiteral* lit);
Steve Blocka7e24c12009-10-30 11:49:00 +00007061
7062 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007063 DECLARE_PRINTER(SharedFunctionInfo)
7064 DECLARE_VERIFIER(SharedFunctionInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +00007065
Ben Murdoch8f9999f2012-04-23 10:39:17 +01007066 void ResetForNewContext(int new_ic_age);
7067
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007068 // Iterate over all shared function infos.
7069 class Iterator {
7070 public:
7071 explicit Iterator(Isolate* isolate);
7072 SharedFunctionInfo* Next();
7073
7074 private:
7075 bool NextScript();
7076
7077 Script::Iterator script_iterator_;
7078 WeakFixedArray::Iterator sfi_iterator_;
7079 DisallowHeapAllocation no_gc_;
7080 DISALLOW_COPY_AND_ASSIGN(Iterator);
7081 };
7082
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007083 DECLARE_CAST(SharedFunctionInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +00007084
7085 // Constants.
7086 static const int kDontAdaptArgumentsSentinel = -1;
7087
7088 // Layout description.
Steve Block6ded16b2010-05-10 14:33:55 +01007089 // Pointer fields.
Steve Blocka7e24c12009-10-30 11:49:00 +00007090 static const int kNameOffset = HeapObject::kHeaderSize;
7091 static const int kCodeOffset = kNameOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007092 static const int kOptimizedCodeMapOffset = kCodeOffset + kPointerSize;
7093 static const int kScopeInfoOffset = kOptimizedCodeMapOffset + kPointerSize;
Ben Murdoch3bec4d22010-07-22 14:51:16 +01007094 static const int kConstructStubOffset = kScopeInfoOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01007095 static const int kInstanceClassNameOffset =
7096 kConstructStubOffset + kPointerSize;
7097 static const int kFunctionDataOffset =
7098 kInstanceClassNameOffset + kPointerSize;
7099 static const int kScriptOffset = kFunctionDataOffset + kPointerSize;
7100 static const int kDebugInfoOffset = kScriptOffset + kPointerSize;
Ben Murdochda12d292016-06-02 14:46:10 +01007101 static const int kFunctionIdentifierOffset = kDebugInfoOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007102 static const int kFeedbackVectorOffset =
Ben Murdochda12d292016-06-02 14:46:10 +01007103 kFunctionIdentifierOffset + kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007104#if TRACE_MAPS
7105 static const int kUniqueIdOffset = kFeedbackVectorOffset + kPointerSize;
7106 static const int kLastPointerFieldOffset = kUniqueIdOffset;
7107#else
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007108 // Just to not break the postmortrem support with conditional offsets
7109 static const int kUniqueIdOffset = kFeedbackVectorOffset;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007110 static const int kLastPointerFieldOffset = kFeedbackVectorOffset;
7111#endif
7112
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007113#if V8_HOST_ARCH_32_BIT
7114 // Smi fields.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007115 static const int kLengthOffset = kLastPointerFieldOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007116 static const int kFormalParameterCountOffset = kLengthOffset + kPointerSize;
7117 static const int kExpectedNofPropertiesOffset =
7118 kFormalParameterCountOffset + kPointerSize;
7119 static const int kNumLiteralsOffset =
7120 kExpectedNofPropertiesOffset + kPointerSize;
7121 static const int kStartPositionAndTypeOffset =
7122 kNumLiteralsOffset + kPointerSize;
7123 static const int kEndPositionOffset =
7124 kStartPositionAndTypeOffset + kPointerSize;
7125 static const int kFunctionTokenPositionOffset =
7126 kEndPositionOffset + kPointerSize;
7127 static const int kCompilerHintsOffset =
7128 kFunctionTokenPositionOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007129 static const int kOptCountAndBailoutReasonOffset =
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007130 kCompilerHintsOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007131 static const int kCountersOffset =
7132 kOptCountAndBailoutReasonOffset + kPointerSize;
7133 static const int kAstNodeCountOffset =
7134 kCountersOffset + kPointerSize;
7135 static const int kProfilerTicksOffset =
7136 kAstNodeCountOffset + kPointerSize;
Ben Murdoch8f9999f2012-04-23 10:39:17 +01007137
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007138 // Total size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007139 static const int kSize = kProfilerTicksOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007140#else
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007141// The only reason to use smi fields instead of int fields is to allow
7142// iteration without maps decoding during garbage collections.
7143// To avoid wasting space on 64-bit architectures we use the following trick:
7144// we group integer fields into pairs
7145// The least significant integer in each pair is shifted left by 1. By doing
7146// this we guarantee that LSB of each kPointerSize aligned word is not set and
7147// thus this word cannot be treated as pointer to HeapObject during old space
7148// traversal.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007149#if V8_TARGET_LITTLE_ENDIAN
7150 static const int kLengthOffset = kLastPointerFieldOffset + kPointerSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007151 static const int kFormalParameterCountOffset =
7152 kLengthOffset + kIntSize;
7153
Steve Blocka7e24c12009-10-30 11:49:00 +00007154 static const int kExpectedNofPropertiesOffset =
7155 kFormalParameterCountOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007156 static const int kNumLiteralsOffset =
7157 kExpectedNofPropertiesOffset + kIntSize;
7158
7159 static const int kEndPositionOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01007160 kNumLiteralsOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007161 static const int kStartPositionAndTypeOffset =
7162 kEndPositionOffset + kIntSize;
7163
7164 static const int kFunctionTokenPositionOffset =
7165 kStartPositionAndTypeOffset + kIntSize;
Steve Block6ded16b2010-05-10 14:33:55 +01007166 static const int kCompilerHintsOffset =
Steve Blocka7e24c12009-10-30 11:49:00 +00007167 kFunctionTokenPositionOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007168
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007169 static const int kOptCountAndBailoutReasonOffset =
Steve Block6ded16b2010-05-10 14:33:55 +01007170 kCompilerHintsOffset + kIntSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007171 static const int kCountersOffset =
7172 kOptCountAndBailoutReasonOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007173
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007174 static const int kAstNodeCountOffset =
7175 kCountersOffset + kIntSize;
7176 static const int kProfilerTicksOffset =
7177 kAstNodeCountOffset + kIntSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007178
Steve Block6ded16b2010-05-10 14:33:55 +01007179 // Total size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007180 static const int kSize = kProfilerTicksOffset + kIntSize;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01007181
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007182#elif V8_TARGET_BIG_ENDIAN
7183 static const int kFormalParameterCountOffset =
7184 kLastPointerFieldOffset + kPointerSize;
7185 static const int kLengthOffset = kFormalParameterCountOffset + kIntSize;
7186
7187 static const int kNumLiteralsOffset = kLengthOffset + kIntSize;
7188 static const int kExpectedNofPropertiesOffset = kNumLiteralsOffset + kIntSize;
7189
7190 static const int kStartPositionAndTypeOffset =
7191 kExpectedNofPropertiesOffset + kIntSize;
7192 static const int kEndPositionOffset = kStartPositionAndTypeOffset + kIntSize;
7193
7194 static const int kCompilerHintsOffset = kEndPositionOffset + kIntSize;
7195 static const int kFunctionTokenPositionOffset =
7196 kCompilerHintsOffset + kIntSize;
7197
7198 static const int kCountersOffset = kFunctionTokenPositionOffset + kIntSize;
7199 static const int kOptCountAndBailoutReasonOffset = kCountersOffset + kIntSize;
7200
7201 static const int kProfilerTicksOffset =
7202 kOptCountAndBailoutReasonOffset + kIntSize;
7203 static const int kAstNodeCountOffset = kProfilerTicksOffset + kIntSize;
7204
7205 // Total size.
7206 static const int kSize = kAstNodeCountOffset + kIntSize;
7207
7208#else
7209#error Unknown byte ordering
7210#endif // Big endian
7211#endif // 64-bit
7212
Kristian Monsen0d5e1162010-09-30 15:31:59 +01007213
Steve Block6ded16b2010-05-10 14:33:55 +01007214 static const int kAlignedSize = POINTER_SIZE_ALIGN(kSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00007215
Iain Merrick75681382010-08-19 15:07:18 +01007216 typedef FixedBodyDescriptor<kNameOffset,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007217 kLastPointerFieldOffset + kPointerSize,
Iain Merrick75681382010-08-19 15:07:18 +01007218 kSize> BodyDescriptor;
7219
Steve Blocka7e24c12009-10-30 11:49:00 +00007220 // Bit positions in start_position_and_type.
7221 // The source code start position is in the 30 most significant bits of
7222 // the start_position_and_type field.
Ben Murdoch097c5b22016-05-18 11:27:45 +01007223 static const int kIsNamedExpressionBit = 0;
7224 static const int kIsTopLevelBit = 1;
Steve Blocka7e24c12009-10-30 11:49:00 +00007225 static const int kStartPositionShift = 2;
Ben Murdoch097c5b22016-05-18 11:27:45 +01007226 static const int kStartPositionMask = ~((1 << kStartPositionShift) - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00007227
7228 // Bit positions in compiler_hints.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007229 enum CompilerHints {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007230 // byte 0
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007231 kAllowLazyCompilation,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007232 kAllowLazyCompilationWithoutContext,
7233 kOptimizationDisabled,
Ben Murdochda12d292016-06-02 14:46:10 +01007234 kNeverCompiled,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007235 kNative,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007236 kStrictModeFunction,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007237 kUsesArguments,
7238 kNeedsHomeObject,
7239 // byte 1
7240 kHasDuplicateParameters,
7241 kForceInline,
7242 kIsAsmFunction,
Ben Murdoch097c5b22016-05-18 11:27:45 +01007243 kIsAnonymousExpression,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007244 kNameShouldPrintAsAnonymous,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007245 kIsFunction,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007246 kDontCrankshaft,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007247 kDontFlush,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007248 // byte 2
7249 kFunctionKind,
7250 kIsArrow = kFunctionKind,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007251 kIsGenerator,
7252 kIsConciseMethod,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007253 kIsDefaultConstructor,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007254 kIsSubclassConstructor,
7255 kIsBaseConstructor,
Ben Murdoch097c5b22016-05-18 11:27:45 +01007256 kIsGetterFunction,
7257 kIsSetterFunction,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007258 // byte 3
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007259 kDeserialized,
Ben Murdoch097c5b22016-05-18 11:27:45 +01007260 kIsDeclaration,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007261 kCompilerHintsCount, // Pseudo entry
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007262 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007263 // Add hints for other modes when they're added.
7264 STATIC_ASSERT(LANGUAGE_END == 3);
7265 // kFunctionKind has to be byte-aligned
7266 STATIC_ASSERT((kFunctionKind % kBitsPerByte) == 0);
7267// Make sure that FunctionKind and byte 2 are in sync:
7268#define ASSERT_FUNCTION_KIND_ORDER(functionKind, compilerFunctionKind) \
7269 STATIC_ASSERT(FunctionKind::functionKind == \
7270 1 << (compilerFunctionKind - kFunctionKind))
7271 ASSERT_FUNCTION_KIND_ORDER(kArrowFunction, kIsArrow);
7272 ASSERT_FUNCTION_KIND_ORDER(kGeneratorFunction, kIsGenerator);
7273 ASSERT_FUNCTION_KIND_ORDER(kConciseMethod, kIsConciseMethod);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007274 ASSERT_FUNCTION_KIND_ORDER(kDefaultConstructor, kIsDefaultConstructor);
7275 ASSERT_FUNCTION_KIND_ORDER(kSubclassConstructor, kIsSubclassConstructor);
7276 ASSERT_FUNCTION_KIND_ORDER(kBaseConstructor, kIsBaseConstructor);
Ben Murdoch097c5b22016-05-18 11:27:45 +01007277 ASSERT_FUNCTION_KIND_ORDER(kGetterFunction, kIsGetterFunction);
7278 ASSERT_FUNCTION_KIND_ORDER(kSetterFunction, kIsSetterFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007279#undef ASSERT_FUNCTION_KIND_ORDER
Steve Blocka7e24c12009-10-30 11:49:00 +00007280
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007281 class FunctionKindBits : public BitField<FunctionKind, kIsArrow, 8> {};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007282
7283 class DeoptCountBits : public BitField<int, 0, 4> {};
7284 class OptReenableTriesBits : public BitField<int, 4, 18> {};
7285 class ICAgeBits : public BitField<int, 22, 8> {};
7286
7287 class OptCountBits : public BitField<int, 0, 22> {};
7288 class DisabledOptimizationReasonBits : public BitField<int, 22, 8> {};
7289
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007290 private:
7291#if V8_HOST_ARCH_32_BIT
7292 // On 32 bit platforms, compiler hints is a smi.
7293 static const int kCompilerHintsSmiTagSize = kSmiTagSize;
7294 static const int kCompilerHintsSize = kPointerSize;
7295#else
7296 // On 64 bit platforms, compiler hints is not a smi, see comment above.
7297 static const int kCompilerHintsSmiTagSize = 0;
7298 static const int kCompilerHintsSize = kIntSize;
7299#endif
7300
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007301 STATIC_ASSERT(SharedFunctionInfo::kCompilerHintsCount <=
7302 SharedFunctionInfo::kCompilerHintsSize * kBitsPerByte);
7303
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007304 public:
Ben Murdoch257744e2011-11-30 15:57:28 +00007305 // Constants for optimizing codegen for strict mode function and
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007306 // native tests when using integer-width instructions.
7307 static const int kStrictModeBit =
7308 kStrictModeFunction + kCompilerHintsSmiTagSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007309 static const int kNativeBit = kNative + kCompilerHintsSmiTagSize;
7310
7311 static const int kClassConstructorBits =
7312 FunctionKind::kClassConstructor
7313 << (kFunctionKind + kCompilerHintsSmiTagSize);
7314
7315 // Constants for optimizing codegen for strict mode function and
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00007316 // native tests.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007317 // Allows to use byte-width instructions.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007318 static const int kStrictModeBitWithinByte = kStrictModeBit % kBitsPerByte;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007319 static const int kNativeBitWithinByte = kNativeBit % kBitsPerByte;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007321 static const int kClassConstructorBitsWithinByte =
7322 FunctionKind::kClassConstructor << kCompilerHintsSmiTagSize;
7323 STATIC_ASSERT(kClassConstructorBitsWithinByte < (1 << kBitsPerByte));
Ben Murdoch257744e2011-11-30 15:57:28 +00007324
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007325#if defined(V8_TARGET_LITTLE_ENDIAN)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007326#define BYTE_OFFSET(compiler_hint) \
7327 kCompilerHintsOffset + \
7328 (compiler_hint + kCompilerHintsSmiTagSize) / kBitsPerByte
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007329#elif defined(V8_TARGET_BIG_ENDIAN)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007330#define BYTE_OFFSET(compiler_hint) \
7331 kCompilerHintsOffset + (kCompilerHintsSize - 1) - \
7332 ((compiler_hint + kCompilerHintsSmiTagSize) / kBitsPerByte)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007333#else
7334#error Unknown byte ordering
7335#endif
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007336 static const int kStrictModeByteOffset = BYTE_OFFSET(kStrictModeFunction);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007337 static const int kNativeByteOffset = BYTE_OFFSET(kNative);
7338 static const int kFunctionKindByteOffset = BYTE_OFFSET(kFunctionKind);
7339#undef BYTE_OFFSET
Ben Murdoche0cee9b2011-05-25 10:26:03 +01007340
7341 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007342 // Returns entry from optimized code map for specified context and OSR entry.
7343 // The result is either kNotFound, kSharedCodeIndex for context-independent
7344 // entry or a start index of the context-dependent entry.
7345 int SearchOptimizedCodeMapEntry(Context* native_context,
7346 BailoutId osr_ast_id);
7347
Steve Blocka7e24c12009-10-30 11:49:00 +00007348 DISALLOW_IMPLICIT_CONSTRUCTORS(SharedFunctionInfo);
7349};
7350
7351
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007352// Printing support.
7353struct SourceCodeOf {
7354 explicit SourceCodeOf(SharedFunctionInfo* v, int max = -1)
7355 : value(v), max_length(max) {}
7356 const SharedFunctionInfo* value;
7357 int max_length;
7358};
7359
7360
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007361std::ostream& operator<<(std::ostream& os, const SourceCodeOf& v);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007362
7363
7364class JSGeneratorObject: public JSObject {
7365 public:
7366 // [function]: The function corresponding to this generator object.
7367 DECL_ACCESSORS(function, JSFunction)
7368
7369 // [context]: The context of the suspended computation.
7370 DECL_ACCESSORS(context, Context)
7371
7372 // [receiver]: The receiver of the suspended computation.
7373 DECL_ACCESSORS(receiver, Object)
7374
Ben Murdoch097c5b22016-05-18 11:27:45 +01007375 // [input]: The most recent input value.
7376 DECL_ACCESSORS(input, Object)
7377
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007378 // [continuation]: Offset into code of continuation.
7379 //
7380 // A positive offset indicates a suspended generator. The special
7381 // kGeneratorExecuting and kGeneratorClosed values indicate that a generator
7382 // cannot be resumed.
7383 inline int continuation() const;
7384 inline void set_continuation(int continuation);
7385 inline bool is_closed();
7386 inline bool is_executing();
7387 inline bool is_suspended();
7388
7389 // [operand_stack]: Saved operand stack.
7390 DECL_ACCESSORS(operand_stack, FixedArray)
7391
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007392 DECLARE_CAST(JSGeneratorObject)
7393
7394 // Dispatched behavior.
7395 DECLARE_PRINTER(JSGeneratorObject)
7396 DECLARE_VERIFIER(JSGeneratorObject)
7397
7398 // Magic sentinel values for the continuation.
7399 static const int kGeneratorExecuting = -1;
7400 static const int kGeneratorClosed = 0;
7401
7402 // Layout description.
7403 static const int kFunctionOffset = JSObject::kHeaderSize;
7404 static const int kContextOffset = kFunctionOffset + kPointerSize;
7405 static const int kReceiverOffset = kContextOffset + kPointerSize;
Ben Murdoch097c5b22016-05-18 11:27:45 +01007406 static const int kInputOffset = kReceiverOffset + kPointerSize;
7407 static const int kContinuationOffset = kInputOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007408 static const int kOperandStackOffset = kContinuationOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007409 static const int kSize = kOperandStackOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007410
7411 // Resume mode, for use by runtime functions.
Ben Murdoch097c5b22016-05-18 11:27:45 +01007412 enum ResumeMode { NEXT, RETURN, THROW };
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007413
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007414 private:
7415 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGeneratorObject);
7416};
7417
7418
7419// Representation for module instance objects.
7420class JSModule: public JSObject {
7421 public:
7422 // [context]: the context holding the module's locals, or undefined if none.
7423 DECL_ACCESSORS(context, Object)
7424
7425 // [scope_info]: Scope info.
7426 DECL_ACCESSORS(scope_info, ScopeInfo)
7427
7428 DECLARE_CAST(JSModule)
7429
7430 // Dispatched behavior.
7431 DECLARE_PRINTER(JSModule)
7432 DECLARE_VERIFIER(JSModule)
7433
7434 // Layout description.
7435 static const int kContextOffset = JSObject::kHeaderSize;
7436 static const int kScopeInfoOffset = kContextOffset + kPointerSize;
7437 static const int kSize = kScopeInfoOffset + kPointerSize;
7438
7439 private:
7440 DISALLOW_IMPLICIT_CONSTRUCTORS(JSModule);
7441};
7442
7443
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007444// JSBoundFunction describes a bound function exotic object.
7445class JSBoundFunction : public JSObject {
7446 public:
7447 // [length]: The bound function "length" property.
7448 DECL_ACCESSORS(length, Object)
7449
7450 // [name]: The bound function "name" property.
7451 DECL_ACCESSORS(name, Object)
7452
7453 // [bound_target_function]: The wrapped function object.
7454 DECL_ACCESSORS(bound_target_function, JSReceiver)
7455
7456 // [bound_this]: The value that is always passed as the this value when
7457 // calling the wrapped function.
7458 DECL_ACCESSORS(bound_this, Object)
7459
7460 // [bound_arguments]: A list of values whose elements are used as the first
7461 // arguments to any call to the wrapped function.
7462 DECL_ACCESSORS(bound_arguments, FixedArray)
7463
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007464 static MaybeHandle<Context> GetFunctionRealm(
7465 Handle<JSBoundFunction> function);
7466
7467 DECLARE_CAST(JSBoundFunction)
7468
7469 // Dispatched behavior.
7470 DECLARE_PRINTER(JSBoundFunction)
7471 DECLARE_VERIFIER(JSBoundFunction)
7472
7473 // The bound function's string representation implemented according
7474 // to ES6 section 19.2.3.5 Function.prototype.toString ( ).
7475 static Handle<String> ToString(Handle<JSBoundFunction> function);
7476
Ben Murdochda12d292016-06-02 14:46:10 +01007477 static MaybeHandle<String> GetName(Isolate* isolate,
7478 Handle<JSBoundFunction> function);
7479
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007480 // Layout description.
7481 static const int kBoundTargetFunctionOffset = JSObject::kHeaderSize;
7482 static const int kBoundThisOffset = kBoundTargetFunctionOffset + kPointerSize;
7483 static const int kBoundArgumentsOffset = kBoundThisOffset + kPointerSize;
Ben Murdoch097c5b22016-05-18 11:27:45 +01007484 static const int kLengthOffset = kBoundArgumentsOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007485 static const int kNameOffset = kLengthOffset + kPointerSize;
7486 static const int kSize = kNameOffset + kPointerSize;
7487
7488 // Indices of in-object properties.
7489 static const int kLengthIndex = 0;
7490 static const int kNameIndex = 1;
7491
7492 private:
7493 DISALLOW_IMPLICIT_CONSTRUCTORS(JSBoundFunction);
7494};
7495
7496
Steve Blocka7e24c12009-10-30 11:49:00 +00007497// JSFunction describes JavaScript functions.
7498class JSFunction: public JSObject {
7499 public:
7500 // [prototype_or_initial_map]:
7501 DECL_ACCESSORS(prototype_or_initial_map, Object)
7502
Ben Murdoch589d6972011-11-30 16:04:58 +00007503 // [shared]: The information about the function that
Steve Blocka7e24c12009-10-30 11:49:00 +00007504 // can be shared by instances.
7505 DECL_ACCESSORS(shared, SharedFunctionInfo)
7506
7507 // [context]: The context for this function.
7508 inline Context* context();
Steve Blocka7e24c12009-10-30 11:49:00 +00007509 inline void set_context(Object* context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007510 inline JSObject* global_proxy();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007511 inline Context* native_context();
7512
7513 static Handle<Context> GetFunctionRealm(Handle<JSFunction> function);
Steve Blocka7e24c12009-10-30 11:49:00 +00007514
7515 // [code]: The generated code object for this function. Executed
7516 // when the function is invoked, e.g. foo() or new foo(). See
7517 // [[Call]] and [[Construct]] description in ECMA-262, section
7518 // 8.6.2, page 27.
7519 inline Code* code();
Ben Murdochb0fe1622011-05-05 13:52:32 +01007520 inline void set_code(Code* code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007521 inline void set_code_no_write_barrier(Code* code);
Ben Murdochb0fe1622011-05-05 13:52:32 +01007522 inline void ReplaceCode(Code* code);
Steve Blocka7e24c12009-10-30 11:49:00 +00007523
Ben Murdochda12d292016-06-02 14:46:10 +01007524 // Get the abstract code associated with the function, which will either be
7525 // a Code object or a BytecodeArray.
7526 inline AbstractCode* abstract_code();
7527
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007528 // Tells whether this function inlines the given shared function info.
7529 bool Inlines(SharedFunctionInfo* candidate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01007530
7531 // Tells whether or not this function has been optimized.
7532 inline bool IsOptimized();
7533
7534 // Mark this function for lazy recompilation. The function will be
7535 // recompiled the next time it is executed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007536 void MarkForOptimization();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007537 void AttemptConcurrentOptimization();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007538
Ben Murdochb0fe1622011-05-05 13:52:32 +01007539 // Tells whether or not the function is already marked for lazy
7540 // recompilation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007541 inline bool IsMarkedForOptimization();
7542 inline bool IsMarkedForConcurrentOptimization();
Ben Murdochb0fe1622011-05-05 13:52:32 +01007543
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007544 // Tells whether or not the function is on the concurrent recompilation queue.
7545 inline bool IsInOptimizationQueue();
7546
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007547 // Completes inobject slack tracking on initial map if it is active.
7548 inline void CompleteInobjectSlackTrackingIfActive();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007549
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007550 // [literals]: Fixed array holding the materialized literals.
Steve Blocka7e24c12009-10-30 11:49:00 +00007551 //
7552 // If the function contains object, regexp or array literals, the
7553 // literals array prefix contains the object, regexp, and array
7554 // function to be used when creating these literals. This is
7555 // necessary so that we do not dynamically lookup the object, regexp
7556 // or array functions. Performing a dynamic lookup, we might end up
7557 // using the functions from a new context that we should not have
7558 // access to.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007559 DECL_ACCESSORS(literals, LiteralsArray)
Steve Blocka7e24c12009-10-30 11:49:00 +00007560
7561 // The initial map for an object created by this constructor.
7562 inline Map* initial_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007563 static void SetInitialMap(Handle<JSFunction> function, Handle<Map> map,
7564 Handle<Object> prototype);
Steve Blocka7e24c12009-10-30 11:49:00 +00007565 inline bool has_initial_map();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007566 static void EnsureHasInitialMap(Handle<JSFunction> function);
Steve Blocka7e24c12009-10-30 11:49:00 +00007567
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007568 // Creates a map that matches the constructor's initial map, but with
7569 // [[prototype]] being new.target.prototype. Because new.target can be a
7570 // JSProxy, this can call back into JavaScript.
7571 static MUST_USE_RESULT MaybeHandle<Map> GetDerivedMap(
7572 Isolate* isolate, Handle<JSFunction> constructor,
7573 Handle<JSReceiver> new_target);
7574
Steve Blocka7e24c12009-10-30 11:49:00 +00007575 // Get and set the prototype property on a JSFunction. If the
7576 // function has an initial map the prototype is set on the initial
7577 // map. Otherwise, the prototype is put in the initial map field
7578 // until an initial map is needed.
7579 inline bool has_prototype();
7580 inline bool has_instance_prototype();
7581 inline Object* prototype();
7582 inline Object* instance_prototype();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007583 static void SetPrototype(Handle<JSFunction> function,
7584 Handle<Object> value);
7585 static void SetInstancePrototype(Handle<JSFunction> function,
7586 Handle<Object> value);
7587
Steve Block6ded16b2010-05-10 14:33:55 +01007588 // After prototype is removed, it will not be created when accessed, and
7589 // [[Construct]] from this function will not be allowed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007590 bool RemovePrototype();
Steve Blocka7e24c12009-10-30 11:49:00 +00007591
7592 // Returns if this function has been compiled to native code yet.
7593 inline bool is_compiled();
7594
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007595 // [next_function_link]: Links functions into various lists, e.g. the list
7596 // of optimized functions hanging off the native_context. The CodeFlusher
7597 // uses this link to chain together flushing candidates. Treated weakly
7598 // by the garbage collector.
Ben Murdochb0fe1622011-05-05 13:52:32 +01007599 DECL_ACCESSORS(next_function_link, Object)
7600
7601 // Prints the name of the function using PrintF.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007602 void PrintName(FILE* out = stdout);
Ben Murdochb0fe1622011-05-05 13:52:32 +01007603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007604 DECLARE_CAST(JSFunction)
Steve Blocka7e24c12009-10-30 11:49:00 +00007605
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007606 // Calculate the instance size and in-object properties count.
7607 void CalculateInstanceSize(InstanceType instance_type,
7608 int requested_internal_fields, int* instance_size,
7609 int* in_object_properties);
7610 void CalculateInstanceSizeForDerivedClass(InstanceType instance_type,
7611 int requested_internal_fields,
7612 int* instance_size,
7613 int* in_object_properties);
Ben Murdochda12d292016-06-02 14:46:10 +01007614 static void CalculateInstanceSizeHelper(InstanceType instance_type,
7615 int requested_internal_fields,
7616 int requested_in_object_properties,
7617 int* instance_size,
7618 int* in_object_properties);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007619 // Visiting policy flags define whether the code entry or next function
7620 // should be visited or not.
7621 enum BodyVisitingPolicy {
7622 kVisitCodeEntry = 1 << 0,
7623 kVisitNextFunction = 1 << 1,
7624
7625 kSkipCodeEntryAndNextFunction = 0,
7626 kVisitCodeEntryAndNextFunction = kVisitCodeEntry | kVisitNextFunction
7627 };
7628 // Iterates the function object according to the visiting policy.
7629 template <BodyVisitingPolicy>
7630 class BodyDescriptorImpl;
7631
7632 // Visit the whole object.
7633 typedef BodyDescriptorImpl<kVisitCodeEntryAndNextFunction> BodyDescriptor;
7634
7635 // Don't visit next function.
7636 typedef BodyDescriptorImpl<kVisitCodeEntry> BodyDescriptorStrongCode;
7637 typedef BodyDescriptorImpl<kSkipCodeEntryAndNextFunction>
7638 BodyDescriptorWeakCode;
Steve Block791712a2010-08-27 10:21:07 +01007639
Steve Blocka7e24c12009-10-30 11:49:00 +00007640 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007641 DECLARE_PRINTER(JSFunction)
7642 DECLARE_VERIFIER(JSFunction)
Steve Blocka7e24c12009-10-30 11:49:00 +00007643
7644 // Returns the number of allocated literals.
7645 inline int NumberOfLiterals();
7646
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007647 // The function's name if it is configured, otherwise shared function info
7648 // debug name.
7649 static Handle<String> GetName(Handle<JSFunction> function);
7650
Ben Murdoch097c5b22016-05-18 11:27:45 +01007651 // ES6 section 9.2.11 SetFunctionName
7652 // Because of the way this abstract operation is used in the spec,
7653 // it should never fail.
7654 static void SetName(Handle<JSFunction> function, Handle<Name> name,
7655 Handle<String> prefix);
7656
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007657 // The function's displayName if it is set, otherwise name if it is
7658 // configured, otherwise shared function info
7659 // debug name.
7660 static Handle<String> GetDebugName(Handle<JSFunction> function);
7661
7662 // The function's string representation implemented according to
7663 // ES6 section 19.2.3.5 Function.prototype.toString ( ).
7664 static Handle<String> ToString(Handle<JSFunction> function);
7665
Ben Murdochb0fe1622011-05-05 13:52:32 +01007666 // Layout descriptors. The last property (from kNonWeakFieldsEndOffset to
7667 // kSize) is weak and has special handling during garbage collection.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007668 static const int kPrototypeOrInitialMapOffset = JSObject::kHeaderSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007669 static const int kSharedFunctionInfoOffset =
7670 kPrototypeOrInitialMapOffset + kPointerSize;
7671 static const int kContextOffset = kSharedFunctionInfoOffset + kPointerSize;
7672 static const int kLiteralsOffset = kContextOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01007673 static const int kNonWeakFieldsEndOffset = kLiteralsOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007674 static const int kCodeEntryOffset = kNonWeakFieldsEndOffset;
7675 static const int kNextFunctionLinkOffset = kCodeEntryOffset + kPointerSize;
Ben Murdochb0fe1622011-05-05 13:52:32 +01007676 static const int kSize = kNextFunctionLinkOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007677
Steve Blocka7e24c12009-10-30 11:49:00 +00007678 private:
7679 DISALLOW_IMPLICIT_CONSTRUCTORS(JSFunction);
7680};
7681
7682
7683// JSGlobalProxy's prototype must be a JSGlobalObject or null,
7684// and the prototype is hidden. JSGlobalProxy always delegates
7685// property accesses to its prototype if the prototype is not null.
7686//
7687// A JSGlobalProxy can be reinitialized which will preserve its identity.
7688//
7689// Accessing a JSGlobalProxy requires security check.
7690
7691class JSGlobalProxy : public JSObject {
7692 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007693 // [native_context]: the owner native context of this global proxy object.
Steve Blocka7e24c12009-10-30 11:49:00 +00007694 // It is null value if this object is not used by any context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007695 DECL_ACCESSORS(native_context, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +00007696
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007697 // [hash]: The hash code property (undefined if not initialized yet).
7698 DECL_ACCESSORS(hash, Object)
7699
7700 DECLARE_CAST(JSGlobalProxy)
7701
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007702 inline bool IsDetachedFrom(JSGlobalObject* global) const;
Steve Blocka7e24c12009-10-30 11:49:00 +00007703
7704 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007705 DECLARE_PRINTER(JSGlobalProxy)
7706 DECLARE_VERIFIER(JSGlobalProxy)
Steve Blocka7e24c12009-10-30 11:49:00 +00007707
7708 // Layout description.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007709 static const int kNativeContextOffset = JSObject::kHeaderSize;
7710 static const int kHashOffset = kNativeContextOffset + kPointerSize;
7711 static const int kSize = kHashOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007712
7713 private:
Steve Blocka7e24c12009-10-30 11:49:00 +00007714 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalProxy);
7715};
7716
7717
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007718// JavaScript global object.
7719class JSGlobalObject : public JSObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00007720 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007721 // [native context]: the natives corresponding to this global object.
7722 DECL_ACCESSORS(native_context, Context)
7723
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007724 // [global proxy]: the global proxy object of the context
7725 DECL_ACCESSORS(global_proxy, JSObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00007726
Steve Blocka7e24c12009-10-30 11:49:00 +00007727
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007728 static void InvalidatePropertyCell(Handle<JSGlobalObject> object,
Emily Bernierd0a1eb72015-03-24 16:35:39 -04007729 Handle<Name> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007730 // Ensure that the global object has a cell for the given property name.
7731 static Handle<PropertyCell> EnsurePropertyCell(Handle<JSGlobalObject> global,
7732 Handle<Name> name);
7733
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007734 DECLARE_CAST(JSGlobalObject)
7735
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007736 inline bool IsDetached();
Steve Blocka7e24c12009-10-30 11:49:00 +00007737
7738 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007739 DECLARE_PRINTER(JSGlobalObject)
7740 DECLARE_VERIFIER(JSGlobalObject)
Steve Blocka7e24c12009-10-30 11:49:00 +00007741
7742 // Layout description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007743 static const int kNativeContextOffset = JSObject::kHeaderSize;
7744 static const int kGlobalProxyOffset = kNativeContextOffset + kPointerSize;
7745 static const int kHeaderSize = kGlobalProxyOffset + kPointerSize;
7746 static const int kSize = kHeaderSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00007747
7748 private:
7749 DISALLOW_IMPLICIT_CONSTRUCTORS(JSGlobalObject);
7750};
7751
7752
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007753// Representation for JS Wrapper objects, String, Number, Boolean, etc.
Steve Blocka7e24c12009-10-30 11:49:00 +00007754class JSValue: public JSObject {
7755 public:
7756 // [value]: the object being wrapped.
7757 DECL_ACCESSORS(value, Object)
7758
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007759 DECLARE_CAST(JSValue)
Steve Blocka7e24c12009-10-30 11:49:00 +00007760
7761 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007762 DECLARE_PRINTER(JSValue)
7763 DECLARE_VERIFIER(JSValue)
Steve Blocka7e24c12009-10-30 11:49:00 +00007764
7765 // Layout description.
7766 static const int kValueOffset = JSObject::kHeaderSize;
7767 static const int kSize = kValueOffset + kPointerSize;
7768
7769 private:
7770 DISALLOW_IMPLICIT_CONSTRUCTORS(JSValue);
7771};
7772
Steve Block1e0659c2011-05-24 12:43:12 +01007773
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007774class DateCache;
7775
7776// Representation for JS date objects.
7777class JSDate: public JSObject {
7778 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007779 static MUST_USE_RESULT MaybeHandle<JSDate> New(Handle<JSFunction> constructor,
7780 Handle<JSReceiver> new_target,
7781 double tv);
7782
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007783 // If one component is NaN, all of them are, indicating a NaN time value.
7784 // [value]: the time value.
7785 DECL_ACCESSORS(value, Object)
7786 // [year]: caches year. Either undefined, smi, or NaN.
7787 DECL_ACCESSORS(year, Object)
7788 // [month]: caches month. Either undefined, smi, or NaN.
7789 DECL_ACCESSORS(month, Object)
7790 // [day]: caches day. Either undefined, smi, or NaN.
7791 DECL_ACCESSORS(day, Object)
7792 // [weekday]: caches day of week. Either undefined, smi, or NaN.
7793 DECL_ACCESSORS(weekday, Object)
7794 // [hour]: caches hours. Either undefined, smi, or NaN.
7795 DECL_ACCESSORS(hour, Object)
7796 // [min]: caches minutes. Either undefined, smi, or NaN.
7797 DECL_ACCESSORS(min, Object)
7798 // [sec]: caches seconds. Either undefined, smi, or NaN.
7799 DECL_ACCESSORS(sec, Object)
7800 // [cache stamp]: sample of the date cache stamp at the
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007801 // moment when chached fields were cached.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007802 DECL_ACCESSORS(cache_stamp, Object)
7803
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007804 DECLARE_CAST(JSDate)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007805
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007806 // Returns the time value (UTC) identifying the current time.
7807 static double CurrentTimeValue(Isolate* isolate);
7808
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007809 // Returns the date field with the specified index.
7810 // See FieldIndex for the list of date fields.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007811 static Object* GetField(Object* date, Smi* index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007812
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007813 static Handle<Object> SetValue(Handle<JSDate> date, double v);
7814
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007815 void SetValue(Object* value, bool is_value_nan);
7816
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007817 // ES6 section 20.3.4.45 Date.prototype [ @@toPrimitive ]
7818 static MUST_USE_RESULT MaybeHandle<Object> ToPrimitive(
7819 Handle<JSReceiver> receiver, Handle<Object> hint);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007820
7821 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007822 DECLARE_PRINTER(JSDate)
7823 DECLARE_VERIFIER(JSDate)
7824
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007825 // The order is important. It must be kept in sync with date macros
7826 // in macros.py.
7827 enum FieldIndex {
7828 kDateValue,
7829 kYear,
7830 kMonth,
7831 kDay,
7832 kWeekday,
7833 kHour,
7834 kMinute,
7835 kSecond,
7836 kFirstUncachedField,
7837 kMillisecond = kFirstUncachedField,
7838 kDays,
7839 kTimeInDay,
7840 kFirstUTCField,
7841 kYearUTC = kFirstUTCField,
7842 kMonthUTC,
7843 kDayUTC,
7844 kWeekdayUTC,
7845 kHourUTC,
7846 kMinuteUTC,
7847 kSecondUTC,
7848 kMillisecondUTC,
7849 kDaysUTC,
7850 kTimeInDayUTC,
7851 kTimezoneOffset
7852 };
7853
7854 // Layout description.
7855 static const int kValueOffset = JSObject::kHeaderSize;
7856 static const int kYearOffset = kValueOffset + kPointerSize;
7857 static const int kMonthOffset = kYearOffset + kPointerSize;
7858 static const int kDayOffset = kMonthOffset + kPointerSize;
7859 static const int kWeekdayOffset = kDayOffset + kPointerSize;
7860 static const int kHourOffset = kWeekdayOffset + kPointerSize;
7861 static const int kMinOffset = kHourOffset + kPointerSize;
7862 static const int kSecOffset = kMinOffset + kPointerSize;
7863 static const int kCacheStampOffset = kSecOffset + kPointerSize;
7864 static const int kSize = kCacheStampOffset + kPointerSize;
7865
7866 private:
7867 inline Object* DoGetField(FieldIndex index);
7868
7869 Object* GetUTCField(FieldIndex index, double value, DateCache* date_cache);
7870
7871 // Computes and caches the cacheable fields of the date.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007872 inline void SetCachedFields(int64_t local_time_ms, DateCache* date_cache);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01007873
7874
7875 DISALLOW_IMPLICIT_CONSTRUCTORS(JSDate);
7876};
7877
7878
Steve Block1e0659c2011-05-24 12:43:12 +01007879// Representation of message objects used for error reporting through
7880// the API. The messages are formatted in JavaScript so this object is
7881// a real JavaScript object. The information used for formatting the
7882// error messages are not directly accessible from JavaScript to
7883// prevent leaking information to user code called during error
7884// formatting.
7885class JSMessageObject: public JSObject {
7886 public:
7887 // [type]: the type of error message.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007888 inline int type() const;
7889 inline void set_type(int value);
Steve Block1e0659c2011-05-24 12:43:12 +01007890
7891 // [arguments]: the arguments for formatting the error message.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007892 DECL_ACCESSORS(argument, Object)
Steve Block1e0659c2011-05-24 12:43:12 +01007893
7894 // [script]: the script from which the error message originated.
7895 DECL_ACCESSORS(script, Object)
7896
Steve Block1e0659c2011-05-24 12:43:12 +01007897 // [stack_frames]: an array of stack frames for this error object.
7898 DECL_ACCESSORS(stack_frames, Object)
7899
7900 // [start_position]: the start position in the script for the error message.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007901 inline int start_position() const;
Steve Block1e0659c2011-05-24 12:43:12 +01007902 inline void set_start_position(int value);
7903
7904 // [end_position]: the end position in the script for the error message.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007905 inline int end_position() const;
Steve Block1e0659c2011-05-24 12:43:12 +01007906 inline void set_end_position(int value);
7907
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007908 DECLARE_CAST(JSMessageObject)
Steve Block1e0659c2011-05-24 12:43:12 +01007909
7910 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007911 DECLARE_PRINTER(JSMessageObject)
7912 DECLARE_VERIFIER(JSMessageObject)
Steve Block1e0659c2011-05-24 12:43:12 +01007913
7914 // Layout description.
7915 static const int kTypeOffset = JSObject::kHeaderSize;
7916 static const int kArgumentsOffset = kTypeOffset + kPointerSize;
7917 static const int kScriptOffset = kArgumentsOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007918 static const int kStackFramesOffset = kScriptOffset + kPointerSize;
Steve Block1e0659c2011-05-24 12:43:12 +01007919 static const int kStartPositionOffset = kStackFramesOffset + kPointerSize;
7920 static const int kEndPositionOffset = kStartPositionOffset + kPointerSize;
7921 static const int kSize = kEndPositionOffset + kPointerSize;
7922
7923 typedef FixedBodyDescriptor<HeapObject::kMapOffset,
7924 kStackFramesOffset + kPointerSize,
7925 kSize> BodyDescriptor;
7926};
7927
7928
Steve Blocka7e24c12009-10-30 11:49:00 +00007929// Regular expressions
7930// The regular expression holds a single reference to a FixedArray in
7931// the kDataOffset field.
7932// The FixedArray contains the following data:
7933// - tag : type of regexp implementation (not compiled yet, atom or irregexp)
7934// - reference to the original source string
7935// - reference to the original flag string
7936// If it is an atom regexp
7937// - a reference to a literal string to search for
7938// If it is an irregexp regexp:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007939// - a reference to code for Latin1 inputs (bytecode or compiled), or a smi
Ben Murdoch257744e2011-11-30 15:57:28 +00007940// used for tracking the last usage (used for code flushing).
7941// - a reference to code for UC16 inputs (bytecode or compiled), or a smi
7942// used for tracking the last usage (used for code flushing)..
Steve Blocka7e24c12009-10-30 11:49:00 +00007943// - max number of registers used by irregexp implementations.
7944// - number of capture registers (output values) of the regexp.
7945class JSRegExp: public JSObject {
7946 public:
7947 // Meaning of Type:
7948 // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
7949 // ATOM: A simple string to match against using an indexOf operation.
7950 // IRREGEXP: Compiled with Irregexp.
7951 // IRREGEXP_NATIVE: Compiled to native code with Irregexp.
7952 enum Type { NOT_COMPILED, ATOM, IRREGEXP };
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007953 enum Flag {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007954 kNone = 0,
7955 kGlobal = 1 << 0,
7956 kIgnoreCase = 1 << 1,
7957 kMultiline = 1 << 2,
7958 kSticky = 1 << 3,
7959 kUnicode = 1 << 4,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007960 };
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007961 typedef base::Flags<Flag> Flags;
Steve Blocka7e24c12009-10-30 11:49:00 +00007962
7963 DECL_ACCESSORS(data, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00007964 DECL_ACCESSORS(flags, Object)
7965 DECL_ACCESSORS(source, Object)
7966
7967 static MaybeHandle<JSRegExp> New(Handle<String> source, Flags flags);
7968 static MaybeHandle<JSRegExp> New(Handle<String> source, Handle<String> flags);
7969 static Handle<JSRegExp> Copy(Handle<JSRegExp> regexp);
7970
7971 static MaybeHandle<JSRegExp> Initialize(Handle<JSRegExp> regexp,
7972 Handle<String> source, Flags flags);
7973 static MaybeHandle<JSRegExp> Initialize(Handle<JSRegExp> regexp,
7974 Handle<String> source,
7975 Handle<String> flags_string);
Steve Blocka7e24c12009-10-30 11:49:00 +00007976
7977 inline Type TypeTag();
7978 inline int CaptureCount();
7979 inline Flags GetFlags();
7980 inline String* Pattern();
7981 inline Object* DataAt(int index);
7982 // Set implementation data after the object has been prepared.
7983 inline void SetDataAt(int index, Object* value);
Ben Murdoch257744e2011-11-30 15:57:28 +00007984
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007985 static int code_index(bool is_latin1) {
7986 if (is_latin1) {
7987 return kIrregexpLatin1CodeIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00007988 } else {
7989 return kIrregexpUC16CodeIndex;
7990 }
7991 }
7992
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007993 static int saved_code_index(bool is_latin1) {
7994 if (is_latin1) {
7995 return kIrregexpLatin1CodeSavedIndex;
Ben Murdoch257744e2011-11-30 15:57:28 +00007996 } else {
7997 return kIrregexpUC16CodeSavedIndex;
7998 }
7999 }
8000
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008001 DECLARE_CAST(JSRegExp)
Steve Blocka7e24c12009-10-30 11:49:00 +00008002
8003 // Dispatched behavior.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008004 DECLARE_PRINTER(JSRegExp)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008005 DECLARE_VERIFIER(JSRegExp)
Steve Blocka7e24c12009-10-30 11:49:00 +00008006
8007 static const int kDataOffset = JSObject::kHeaderSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008008 static const int kSourceOffset = kDataOffset + kPointerSize;
8009 static const int kFlagsOffset = kSourceOffset + kPointerSize;
8010 static const int kSize = kFlagsOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00008011
8012 // Indices in the data array.
8013 static const int kTagIndex = 0;
8014 static const int kSourceIndex = kTagIndex + 1;
8015 static const int kFlagsIndex = kSourceIndex + 1;
8016 static const int kDataIndex = kFlagsIndex + 1;
8017 // The data fields are used in different ways depending on the
8018 // value of the tag.
8019 // Atom regexps (literal strings).
8020 static const int kAtomPatternIndex = kDataIndex;
8021
8022 static const int kAtomDataSize = kAtomPatternIndex + 1;
8023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008024 // Irregexp compiled code or bytecode for Latin1. If compilation
Steve Blocka7e24c12009-10-30 11:49:00 +00008025 // fails, this fields hold an exception object that should be
8026 // thrown if the regexp is used again.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008027 static const int kIrregexpLatin1CodeIndex = kDataIndex;
Steve Blocka7e24c12009-10-30 11:49:00 +00008028 // Irregexp compiled code or bytecode for UC16. If compilation
8029 // fails, this fields hold an exception object that should be
8030 // thrown if the regexp is used again.
8031 static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00008032
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008033 // Saved instance of Irregexp compiled code or bytecode for Latin1 that
Ben Murdoch257744e2011-11-30 15:57:28 +00008034 // is a potential candidate for flushing.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008035 static const int kIrregexpLatin1CodeSavedIndex = kDataIndex + 2;
Ben Murdoch257744e2011-11-30 15:57:28 +00008036 // Saved instance of Irregexp compiled code or bytecode for UC16 that is
8037 // a potential candidate for flushing.
8038 static const int kIrregexpUC16CodeSavedIndex = kDataIndex + 3;
8039
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008040 // Maximal number of registers used by either Latin1 or UC16.
Steve Blocka7e24c12009-10-30 11:49:00 +00008041 // Only used to check that there is enough stack space
Ben Murdoch257744e2011-11-30 15:57:28 +00008042 static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 4;
Steve Blocka7e24c12009-10-30 11:49:00 +00008043 // Number of captures in the compiled regexp.
Ben Murdoch257744e2011-11-30 15:57:28 +00008044 static const int kIrregexpCaptureCountIndex = kDataIndex + 5;
Steve Blocka7e24c12009-10-30 11:49:00 +00008045
8046 static const int kIrregexpDataSize = kIrregexpCaptureCountIndex + 1;
Leon Clarkee46be812010-01-19 14:06:41 +00008047
8048 // Offsets directly into the data fixed array.
8049 static const int kDataTagOffset =
8050 FixedArray::kHeaderSize + kTagIndex * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008051 static const int kDataOneByteCodeOffset =
8052 FixedArray::kHeaderSize + kIrregexpLatin1CodeIndex * kPointerSize;
Leon Clarked91b9f72010-01-27 17:25:45 +00008053 static const int kDataUC16CodeOffset =
8054 FixedArray::kHeaderSize + kIrregexpUC16CodeIndex * kPointerSize;
Leon Clarkee46be812010-01-19 14:06:41 +00008055 static const int kIrregexpCaptureCountOffset =
8056 FixedArray::kHeaderSize + kIrregexpCaptureCountIndex * kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01008057
8058 // In-object fields.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008059 static const int kLastIndexFieldIndex = 0;
8060 static const int kInObjectFieldCount = 1;
Ben Murdoch257744e2011-11-30 15:57:28 +00008061
8062 // The uninitialized value for a regexp code object.
8063 static const int kUninitializedValue = -1;
8064
8065 // The compilation error value for the regexp code object. The real error
8066 // object is in the saved code field.
8067 static const int kCompilationErrorValue = -2;
8068
8069 // When we store the sweep generation at which we moved the code from the
8070 // code index to the saved code index we mask it of to be in the [0:255]
8071 // range.
8072 static const int kCodeAgeMask = 0xff;
Steve Blocka7e24c12009-10-30 11:49:00 +00008073};
8074
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008075DEFINE_OPERATORS_FOR_FLAGS(JSRegExp::Flags)
8076
Steve Blocka7e24c12009-10-30 11:49:00 +00008077
Ben Murdochc7cc0282012-03-05 14:35:55 +00008078class CompilationCacheShape : public BaseShape<HashTableKey*> {
Steve Blocka7e24c12009-10-30 11:49:00 +00008079 public:
8080 static inline bool IsMatch(HashTableKey* key, Object* value) {
8081 return key->IsMatch(value);
8082 }
8083
8084 static inline uint32_t Hash(HashTableKey* key) {
8085 return key->Hash();
8086 }
8087
8088 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
8089 return key->HashForObject(object);
8090 }
8091
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008092 static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
Steve Blocka7e24c12009-10-30 11:49:00 +00008093
8094 static const int kPrefixSize = 0;
8095 static const int kEntrySize = 2;
8096};
8097
Steve Block3ce2e202009-11-05 08:53:23 +00008098
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008099// This cache is used in two different variants. For regexp caching, it simply
8100// maps identifying info of the regexp to the cached regexp object. Scripts and
8101// eval code only gets cached after a second probe for the code object. To do
8102// so, on first "put" only a hash identifying the source is entered into the
8103// cache, mapping it to a lifetime count of the hash. On each call to Age all
8104// such lifetimes get reduced, and removed once they reach zero. If a second put
8105// is called while such a hash is live in the cache, the hash gets replaced by
8106// an actual cache entry. Age also removes stale live entries from the cache.
8107// Such entries are identified by SharedFunctionInfos pointing to either the
8108// recompilation stub, or to "old" code. This avoids memory leaks due to
8109// premature caching of scripts and eval strings that are never needed later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008110class CompilationCacheTable: public HashTable<CompilationCacheTable,
8111 CompilationCacheShape,
Steve Blocka7e24c12009-10-30 11:49:00 +00008112 HashTableKey*> {
8113 public:
8114 // Find cached value for a string key, otherwise return null.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008115 Handle<Object> Lookup(
8116 Handle<String> src, Handle<Context> context, LanguageMode language_mode);
8117 Handle<Object> LookupEval(
8118 Handle<String> src, Handle<SharedFunctionInfo> shared,
8119 LanguageMode language_mode, int scope_position);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008120 Handle<Object> LookupRegExp(Handle<String> source, JSRegExp::Flags flags);
8121 static Handle<CompilationCacheTable> Put(
8122 Handle<CompilationCacheTable> cache, Handle<String> src,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008123 Handle<Context> context, LanguageMode language_mode,
8124 Handle<Object> value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008125 static Handle<CompilationCacheTable> PutEval(
8126 Handle<CompilationCacheTable> cache, Handle<String> src,
8127 Handle<SharedFunctionInfo> context, Handle<SharedFunctionInfo> value,
8128 int scope_position);
8129 static Handle<CompilationCacheTable> PutRegExp(
8130 Handle<CompilationCacheTable> cache, Handle<String> src,
8131 JSRegExp::Flags flags, Handle<FixedArray> value);
Ben Murdochb0fe1622011-05-05 13:52:32 +01008132 void Remove(Object* value);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008133 void Age();
8134 static const int kHashGenerations = 10;
Ben Murdochb0fe1622011-05-05 13:52:32 +01008135
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008136 DECLARE_CAST(CompilationCacheTable)
Steve Blocka7e24c12009-10-30 11:49:00 +00008137
8138 private:
8139 DISALLOW_IMPLICIT_CONSTRUCTORS(CompilationCacheTable);
8140};
8141
8142
Steve Block6ded16b2010-05-10 14:33:55 +01008143class CodeCache: public Struct {
8144 public:
8145 DECL_ACCESSORS(default_cache, FixedArray)
8146 DECL_ACCESSORS(normal_type_cache, Object)
8147
8148 // Add the code object to the cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008149 static void Update(
8150 Handle<CodeCache> cache, Handle<Name> name, Handle<Code> code);
Steve Block6ded16b2010-05-10 14:33:55 +01008151
8152 // Lookup code object in the cache. Returns code object if found and undefined
8153 // if not.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008154 Object* Lookup(Name* name, Code::Flags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01008155
8156 // Get the internal index of a code object in the cache. Returns -1 if the
8157 // code object is not in that cache. This index can be used to later call
8158 // RemoveByIndex. The cache cannot be modified between a call to GetIndex and
8159 // RemoveByIndex.
8160 int GetIndex(Object* name, Code* code);
8161
8162 // Remove an object from the cache with the provided internal index.
8163 void RemoveByIndex(Object* name, Code* code, int index);
8164
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008165 DECLARE_CAST(CodeCache)
Steve Block6ded16b2010-05-10 14:33:55 +01008166
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008167 // Dispatched behavior.
8168 DECLARE_PRINTER(CodeCache)
8169 DECLARE_VERIFIER(CodeCache)
Steve Block6ded16b2010-05-10 14:33:55 +01008170
8171 static const int kDefaultCacheOffset = HeapObject::kHeaderSize;
8172 static const int kNormalTypeCacheOffset =
8173 kDefaultCacheOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008174 static const int kSize = kNormalTypeCacheOffset + kPointerSize;
Steve Block6ded16b2010-05-10 14:33:55 +01008175
8176 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008177 static void UpdateDefaultCache(
8178 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code);
8179 static void UpdateNormalTypeCache(
8180 Handle<CodeCache> code_cache, Handle<Name> name, Handle<Code> code);
8181 Object* LookupDefaultCache(Name* name, Code::Flags flags);
8182 Object* LookupNormalTypeCache(Name* name, Code::Flags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01008183
8184 // Code cache layout of the default cache. Elements are alternating name and
8185 // code objects for non normal load/store/call IC's.
8186 static const int kCodeCacheEntrySize = 2;
8187 static const int kCodeCacheEntryNameOffset = 0;
8188 static const int kCodeCacheEntryCodeOffset = 1;
8189
8190 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCache);
8191};
8192
8193
Ben Murdochc7cc0282012-03-05 14:35:55 +00008194class CodeCacheHashTableShape : public BaseShape<HashTableKey*> {
Steve Block6ded16b2010-05-10 14:33:55 +01008195 public:
8196 static inline bool IsMatch(HashTableKey* key, Object* value) {
8197 return key->IsMatch(value);
8198 }
8199
8200 static inline uint32_t Hash(HashTableKey* key) {
8201 return key->Hash();
8202 }
8203
8204 static inline uint32_t HashForObject(HashTableKey* key, Object* object) {
8205 return key->HashForObject(object);
8206 }
8207
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008208 static inline Handle<Object> AsHandle(Isolate* isolate, HashTableKey* key);
Steve Block6ded16b2010-05-10 14:33:55 +01008209
8210 static const int kPrefixSize = 0;
8211 static const int kEntrySize = 2;
8212};
8213
8214
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008215class CodeCacheHashTable: public HashTable<CodeCacheHashTable,
8216 CodeCacheHashTableShape,
Steve Block6ded16b2010-05-10 14:33:55 +01008217 HashTableKey*> {
8218 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008219 Object* Lookup(Name* name, Code::Flags flags);
8220 static Handle<CodeCacheHashTable> Put(
8221 Handle<CodeCacheHashTable> table,
8222 Handle<Name> name,
8223 Handle<Code> code);
Steve Block6ded16b2010-05-10 14:33:55 +01008224
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008225 int GetIndex(Name* name, Code::Flags flags);
Steve Block6ded16b2010-05-10 14:33:55 +01008226 void RemoveByIndex(int index);
8227
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008228 DECLARE_CAST(CodeCacheHashTable)
Steve Block6ded16b2010-05-10 14:33:55 +01008229
8230 // Initial size of the fixed array backing the hash table.
8231 static const int kInitialSize = 64;
8232
8233 private:
8234 DISALLOW_IMPLICIT_CONSTRUCTORS(CodeCacheHashTable);
8235};
8236
8237
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008238class PolymorphicCodeCache: public Struct {
8239 public:
8240 DECL_ACCESSORS(cache, Object)
8241
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008242 static void Update(Handle<PolymorphicCodeCache> cache,
8243 MapHandleList* maps,
8244 Code::Flags flags,
8245 Handle<Code> code);
8246
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008247
8248 // Returns an undefined value if the entry is not found.
8249 Handle<Object> Lookup(MapHandleList* maps, Code::Flags flags);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008250
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008251 DECLARE_CAST(PolymorphicCodeCache)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008252
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008253 // Dispatched behavior.
8254 DECLARE_PRINTER(PolymorphicCodeCache)
8255 DECLARE_VERIFIER(PolymorphicCodeCache)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008256
8257 static const int kCacheOffset = HeapObject::kHeaderSize;
8258 static const int kSize = kCacheOffset + kPointerSize;
8259
8260 private:
8261 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCache);
8262};
8263
8264
8265class PolymorphicCodeCacheHashTable
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008266 : public HashTable<PolymorphicCodeCacheHashTable,
8267 CodeCacheHashTableShape,
8268 HashTableKey*> {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008269 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008270 Object* Lookup(MapHandleList* maps, int code_kind);
8271
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008272 static Handle<PolymorphicCodeCacheHashTable> Put(
8273 Handle<PolymorphicCodeCacheHashTable> hash_table,
8274 MapHandleList* maps,
8275 int code_kind,
8276 Handle<Code> code);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008277
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008278 DECLARE_CAST(PolymorphicCodeCacheHashTable)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008279
8280 static const int kInitialSize = 64;
8281 private:
8282 DISALLOW_IMPLICIT_CONSTRUCTORS(PolymorphicCodeCacheHashTable);
8283};
8284
8285
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008286class TypeFeedbackInfo: public Struct {
8287 public:
8288 inline int ic_total_count();
8289 inline void set_ic_total_count(int count);
8290
Ben Murdoch8f9999f2012-04-23 10:39:17 +01008291 inline int ic_with_type_info_count();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008292 inline void change_ic_with_type_info_count(int delta);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008293
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008294 inline int ic_generic_count();
8295 inline void change_ic_generic_count(int delta);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008296
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008297 inline void initialize_storage();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008298
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008299 inline void change_own_type_change_checksum();
8300 inline int own_type_change_checksum();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008301
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008302 inline void set_inlined_type_change_checksum(int checksum);
8303 inline bool matches_inlined_type_change_checksum(int checksum);
8304
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008305 DECLARE_CAST(TypeFeedbackInfo)
8306
8307 // Dispatched behavior.
8308 DECLARE_PRINTER(TypeFeedbackInfo)
8309 DECLARE_VERIFIER(TypeFeedbackInfo)
8310
8311 static const int kStorage1Offset = HeapObject::kHeaderSize;
8312 static const int kStorage2Offset = kStorage1Offset + kPointerSize;
8313 static const int kStorage3Offset = kStorage2Offset + kPointerSize;
8314 static const int kSize = kStorage3Offset + kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008315
8316 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008317 static const int kTypeChangeChecksumBits = 7;
8318
8319 class ICTotalCountField: public BitField<int, 0,
8320 kSmiValueSize - kTypeChangeChecksumBits> {}; // NOLINT
8321 class OwnTypeChangeChecksum: public BitField<int,
8322 kSmiValueSize - kTypeChangeChecksumBits,
8323 kTypeChangeChecksumBits> {}; // NOLINT
8324 class ICsWithTypeInfoCountField: public BitField<int, 0,
8325 kSmiValueSize - kTypeChangeChecksumBits> {}; // NOLINT
8326 class InlinedTypeChangeChecksum: public BitField<int,
8327 kSmiValueSize - kTypeChangeChecksumBits,
8328 kTypeChangeChecksumBits> {}; // NOLINT
8329
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008330 DISALLOW_IMPLICIT_CONSTRUCTORS(TypeFeedbackInfo);
8331};
8332
8333
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008334enum AllocationSiteMode {
8335 DONT_TRACK_ALLOCATION_SITE,
8336 TRACK_ALLOCATION_SITE,
8337 LAST_ALLOCATION_SITE_MODE = TRACK_ALLOCATION_SITE
8338};
8339
8340
8341class AllocationSite: public Struct {
8342 public:
8343 static const uint32_t kMaximumArrayBytesToPretransition = 8 * 1024;
8344 static const double kPretenureRatio;
8345 static const int kPretenureMinimumCreated = 100;
8346
8347 // Values for pretenure decision field.
8348 enum PretenureDecision {
8349 kUndecided = 0,
8350 kDontTenure = 1,
8351 kMaybeTenure = 2,
8352 kTenure = 3,
8353 kZombie = 4,
8354 kLastPretenureDecisionValue = kZombie
8355 };
8356
8357 const char* PretenureDecisionName(PretenureDecision decision);
8358
8359 DECL_ACCESSORS(transition_info, Object)
8360 // nested_site threads a list of sites that represent nested literals
8361 // walked in a particular order. So [[1, 2], 1, 2] will have one
8362 // nested_site, but [[1, 2], 3, [4]] will have a list of two.
8363 DECL_ACCESSORS(nested_site, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008364 DECL_INT_ACCESSORS(pretenure_data)
8365 DECL_INT_ACCESSORS(pretenure_create_count)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008366 DECL_ACCESSORS(dependent_code, DependentCode)
8367 DECL_ACCESSORS(weak_next, Object)
8368
8369 inline void Initialize();
8370
8371 // This method is expensive, it should only be called for reporting.
8372 bool IsNestedSite();
8373
8374 // transition_info bitfields, for constructed array transition info.
8375 class ElementsKindBits: public BitField<ElementsKind, 0, 15> {};
8376 class UnusedBits: public BitField<int, 15, 14> {};
8377 class DoNotInlineBit: public BitField<bool, 29, 1> {};
8378
8379 // Bitfields for pretenure_data
8380 class MementoFoundCountBits: public BitField<int, 0, 26> {};
8381 class PretenureDecisionBits: public BitField<PretenureDecision, 26, 3> {};
8382 class DeoptDependentCodeBit: public BitField<bool, 29, 1> {};
8383 STATIC_ASSERT(PretenureDecisionBits::kMax >= kLastPretenureDecisionValue);
8384
8385 // Increments the mementos found counter and returns true when the first
8386 // memento was found for a given allocation site.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008387 inline bool IncrementMementoFoundCount(int increment = 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008388
8389 inline void IncrementMementoCreateCount();
8390
8391 PretenureFlag GetPretenureMode();
8392
8393 void ResetPretenureDecision();
8394
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008395 inline PretenureDecision pretenure_decision();
8396 inline void set_pretenure_decision(PretenureDecision decision);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008397
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008398 inline bool deopt_dependent_code();
8399 inline void set_deopt_dependent_code(bool deopt);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008400
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008401 inline int memento_found_count();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008402 inline void set_memento_found_count(int count);
8403
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008404 inline int memento_create_count();
8405 inline void set_memento_create_count(int count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008406
8407 // The pretenuring decision is made during gc, and the zombie state allows
8408 // us to recognize when an allocation site is just being kept alive because
8409 // a later traversal of new space may discover AllocationMementos that point
8410 // to this AllocationSite.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008411 inline bool IsZombie();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008412
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008413 inline bool IsMaybeTenure();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008414
8415 inline void MarkZombie();
8416
8417 inline bool MakePretenureDecision(PretenureDecision current_decision,
8418 double ratio,
8419 bool maximum_size_scavenge);
8420
8421 inline bool DigestPretenuringFeedback(bool maximum_size_scavenge);
8422
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008423 inline ElementsKind GetElementsKind();
8424 inline void SetElementsKind(ElementsKind kind);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008425
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008426 inline bool CanInlineCall();
8427 inline void SetDoNotInlineCall();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008428
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008429 inline bool SitePointsToLiteral();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008430
8431 static void DigestTransitionFeedback(Handle<AllocationSite> site,
8432 ElementsKind to_kind);
8433
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008434 DECLARE_PRINTER(AllocationSite)
8435 DECLARE_VERIFIER(AllocationSite)
8436
8437 DECLARE_CAST(AllocationSite)
8438 static inline AllocationSiteMode GetMode(
8439 ElementsKind boilerplate_elements_kind);
8440 static inline AllocationSiteMode GetMode(ElementsKind from, ElementsKind to);
8441 static inline bool CanTrack(InstanceType type);
8442
8443 static const int kTransitionInfoOffset = HeapObject::kHeaderSize;
8444 static const int kNestedSiteOffset = kTransitionInfoOffset + kPointerSize;
8445 static const int kPretenureDataOffset = kNestedSiteOffset + kPointerSize;
8446 static const int kPretenureCreateCountOffset =
8447 kPretenureDataOffset + kPointerSize;
8448 static const int kDependentCodeOffset =
8449 kPretenureCreateCountOffset + kPointerSize;
8450 static const int kWeakNextOffset = kDependentCodeOffset + kPointerSize;
8451 static const int kSize = kWeakNextOffset + kPointerSize;
8452
8453 // During mark compact we need to take special care for the dependent code
8454 // field.
8455 static const int kPointerFieldsBeginOffset = kTransitionInfoOffset;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008456 static const int kPointerFieldsEndOffset = kWeakNextOffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008457
8458 // For other visitors, use the fixed body descriptor below.
Ben Murdoch097c5b22016-05-18 11:27:45 +01008459 typedef FixedBodyDescriptor<HeapObject::kHeaderSize, kSize, kSize>
8460 BodyDescriptor;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008461
8462 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008463 inline bool PretenuringDecisionMade();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008464
8465 DISALLOW_IMPLICIT_CONSTRUCTORS(AllocationSite);
8466};
8467
8468
8469class AllocationMemento: public Struct {
8470 public:
8471 static const int kAllocationSiteOffset = HeapObject::kHeaderSize;
8472 static const int kSize = kAllocationSiteOffset + kPointerSize;
8473
8474 DECL_ACCESSORS(allocation_site, Object)
8475
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008476 inline bool IsValid();
8477 inline AllocationSite* GetAllocationSite();
Ben Murdoch097c5b22016-05-18 11:27:45 +01008478 inline Address GetAllocationSiteUnchecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008479
8480 DECLARE_PRINTER(AllocationMemento)
8481 DECLARE_VERIFIER(AllocationMemento)
8482
8483 DECLARE_CAST(AllocationMemento)
8484
8485 private:
8486 DISALLOW_IMPLICIT_CONSTRUCTORS(AllocationMemento);
8487};
8488
8489
8490// Representation of a slow alias as part of a sloppy arguments objects.
8491// For fast aliases (if HasSloppyArgumentsElements()):
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008492// - the parameter map contains an index into the context
8493// - all attributes of the element have default values
8494// For slow aliases (if HasDictionaryArgumentsElements()):
8495// - the parameter map contains no fast alias mapping (i.e. the hole)
8496// - this struct (in the slow backing store) contains an index into the context
8497// - all attributes are available as part if the property details
8498class AliasedArgumentsEntry: public Struct {
8499 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008500 inline int aliased_context_slot() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008501 inline void set_aliased_context_slot(int count);
8502
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008503 DECLARE_CAST(AliasedArgumentsEntry)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008504
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008505 // Dispatched behavior.
8506 DECLARE_PRINTER(AliasedArgumentsEntry)
8507 DECLARE_VERIFIER(AliasedArgumentsEntry)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01008508
8509 static const int kAliasedContextSlot = HeapObject::kHeaderSize;
8510 static const int kSize = kAliasedContextSlot + kPointerSize;
8511
8512 private:
8513 DISALLOW_IMPLICIT_CONSTRUCTORS(AliasedArgumentsEntry);
8514};
8515
8516
Steve Blocka7e24c12009-10-30 11:49:00 +00008517enum AllowNullsFlag {ALLOW_NULLS, DISALLOW_NULLS};
8518enum RobustnessFlag {ROBUST_STRING_TRAVERSAL, FAST_STRING_TRAVERSAL};
8519
8520
8521class StringHasher {
8522 public:
Ben Murdochc7cc0282012-03-05 14:35:55 +00008523 explicit inline StringHasher(int length, uint32_t seed);
Steve Blocka7e24c12009-10-30 11:49:00 +00008524
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008525 template <typename schar>
8526 static inline uint32_t HashSequentialString(const schar* chars,
8527 int length,
8528 uint32_t seed);
Steve Blocka7e24c12009-10-30 11:49:00 +00008529
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008530 // Reads all the data, even for long strings and computes the utf16 length.
8531 static uint32_t ComputeUtf8Hash(Vector<const char> chars,
8532 uint32_t seed,
8533 int* utf16_length_out);
Steve Blocka7e24c12009-10-30 11:49:00 +00008534
Kristian Monsen80d68ea2010-09-08 11:05:35 +01008535 // Calculated hash value for a string consisting of 1 to
8536 // String::kMaxArrayIndexSize digits with no leading zeros (except "0").
8537 // value is represented decimal value.
Iain Merrick9ac36c92010-09-13 15:29:50 +01008538 static uint32_t MakeArrayIndexHash(uint32_t value, int length);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01008539
Ben Murdochc7cc0282012-03-05 14:35:55 +00008540 // No string is allowed to have a hash of zero. That value is reserved
8541 // for internal properties. If the hash calculation yields zero then we
8542 // use 27 instead.
8543 static const int kZeroHash = 27;
8544
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008545 // Reusable parts of the hashing algorithm.
8546 INLINE(static uint32_t AddCharacterCore(uint32_t running_hash, uint16_t c));
8547 INLINE(static uint32_t GetHashCore(uint32_t running_hash));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008548 INLINE(static uint32_t ComputeRunningHash(uint32_t running_hash,
8549 const uc16* chars, int length));
8550 INLINE(static uint32_t ComputeRunningHashOneByte(uint32_t running_hash,
8551 const char* chars,
8552 int length));
Steve Blocka7e24c12009-10-30 11:49:00 +00008553
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008554 protected:
8555 // Returns the value to store in the hash field of a string with
8556 // the given length and contents.
8557 uint32_t GetHashField();
8558 // Returns true if the hash of this string can be computed without
8559 // looking at the contents.
8560 inline bool has_trivial_hash();
8561 // Adds a block of characters to the hash.
8562 template<typename Char>
8563 inline void AddCharacters(const Char* chars, int len);
8564
8565 private:
8566 // Add a character to the hash.
8567 inline void AddCharacter(uint16_t c);
8568 // Update index. Returns true if string is still an index.
8569 inline bool UpdateIndex(uint16_t c);
Steve Blocka7e24c12009-10-30 11:49:00 +00008570
8571 int length_;
8572 uint32_t raw_running_hash_;
8573 uint32_t array_index_;
8574 bool is_array_index_;
8575 bool is_first_char_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008576 DISALLOW_COPY_AND_ASSIGN(StringHasher);
Steve Blocka7e24c12009-10-30 11:49:00 +00008577};
8578
8579
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008580class IteratingStringHasher : public StringHasher {
8581 public:
8582 static inline uint32_t Hash(String* string, uint32_t seed);
8583 inline void VisitOneByteString(const uint8_t* chars, int length);
8584 inline void VisitTwoByteString(const uint16_t* chars, int length);
8585
8586 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008587 inline IteratingStringHasher(int len, uint32_t seed);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008588 void VisitConsString(ConsString* cons_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008589 DISALLOW_COPY_AND_ASSIGN(IteratingStringHasher);
8590};
Steve Block44f0eee2011-05-26 01:26:41 +01008591
8592
Steve Blocka7e24c12009-10-30 11:49:00 +00008593// The characteristics of a string are stored in its map. Retrieving these
8594// few bits of information is moderately expensive, involving two memory
8595// loads where the second is dependent on the first. To improve efficiency
8596// the shape of the string is given its own class so that it can be retrieved
8597// once and used for several string operations. A StringShape is small enough
8598// to be passed by value and is immutable, but be aware that flattening a
8599// string can potentially alter its shape. Also be aware that a GC caused by
8600// something else can alter the shape of a string due to ConsString
8601// shortcutting. Keeping these restrictions in mind has proven to be error-
8602// prone and so we no longer put StringShapes in variables unless there is a
8603// concrete performance benefit at that particular point in the code.
8604class StringShape BASE_EMBEDDED {
8605 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008606 inline explicit StringShape(const String* s);
Steve Blocka7e24c12009-10-30 11:49:00 +00008607 inline explicit StringShape(Map* s);
8608 inline explicit StringShape(InstanceType t);
8609 inline bool IsSequential();
8610 inline bool IsExternal();
8611 inline bool IsCons();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008612 inline bool IsSliced();
8613 inline bool IsIndirect();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008614 inline bool IsExternalOneByte();
Steve Blocka7e24c12009-10-30 11:49:00 +00008615 inline bool IsExternalTwoByte();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008616 inline bool IsSequentialOneByte();
Steve Blocka7e24c12009-10-30 11:49:00 +00008617 inline bool IsSequentialTwoByte();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008618 inline bool IsInternalized();
Steve Blocka7e24c12009-10-30 11:49:00 +00008619 inline StringRepresentationTag representation_tag();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008620 inline uint32_t encoding_tag();
Steve Blocka7e24c12009-10-30 11:49:00 +00008621 inline uint32_t full_representation_tag();
8622 inline uint32_t size_tag();
8623#ifdef DEBUG
8624 inline uint32_t type() { return type_; }
8625 inline void invalidate() { valid_ = false; }
8626 inline bool valid() { return valid_; }
8627#else
8628 inline void invalidate() { }
8629#endif
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00008630
Steve Blocka7e24c12009-10-30 11:49:00 +00008631 private:
8632 uint32_t type_;
8633#ifdef DEBUG
8634 inline void set_valid() { valid_ = true; }
8635 bool valid_;
8636#else
8637 inline void set_valid() { }
8638#endif
8639};
8640
8641
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008642// The Name abstract class captures anything that can be used as a property
8643// name, i.e., strings and symbols. All names store a hash value.
8644class Name: public HeapObject {
8645 public:
8646 // Get and set the hash field of the name.
8647 inline uint32_t hash_field();
8648 inline void set_hash_field(uint32_t value);
8649
8650 // Tells whether the hash code has been computed.
8651 inline bool HasHashCode();
8652
8653 // Returns a hash value used for the property table
8654 inline uint32_t Hash();
8655
8656 // Equality operations.
8657 inline bool Equals(Name* other);
8658 inline static bool Equals(Handle<Name> one, Handle<Name> two);
8659
8660 // Conversion.
8661 inline bool AsArrayIndex(uint32_t* index);
8662
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008663 // If the name is private, it can only name own properties.
8664 inline bool IsPrivate();
8665
Ben Murdoch097c5b22016-05-18 11:27:45 +01008666 inline bool IsUniqueName() const;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008667
8668 // Return a string version of this name that is converted according to the
8669 // rules described in ES6 section 9.2.11.
8670 MUST_USE_RESULT static MaybeHandle<String> ToFunctionName(Handle<Name> name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008671
8672 DECLARE_CAST(Name)
8673
8674 DECLARE_PRINTER(Name)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008675#if TRACE_MAPS
8676 void NameShortPrint();
8677 int NameShortPrint(Vector<char> str);
8678#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008679
8680 // Layout description.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008681 static const int kHashFieldSlot = HeapObject::kHeaderSize;
8682#if V8_TARGET_LITTLE_ENDIAN || !V8_HOST_ARCH_64_BIT
8683 static const int kHashFieldOffset = kHashFieldSlot;
8684#else
8685 static const int kHashFieldOffset = kHashFieldSlot + kIntSize;
8686#endif
8687 static const int kSize = kHashFieldSlot + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008688
8689 // Mask constant for checking if a name has a computed hash code
8690 // and if it is a string that is an array index. The least significant bit
8691 // indicates whether a hash code has been computed. If the hash code has
8692 // been computed the 2nd bit tells whether the string can be used as an
8693 // array index.
8694 static const int kHashNotComputedMask = 1;
8695 static const int kIsNotArrayIndexMask = 1 << 1;
8696 static const int kNofHashBitFields = 2;
8697
8698 // Shift constant retrieving hash code from hash field.
8699 static const int kHashShift = kNofHashBitFields;
8700
8701 // Only these bits are relevant in the hash, since the top two are shifted
8702 // out.
8703 static const uint32_t kHashBitMask = 0xffffffffu >> kHashShift;
8704
8705 // Array index strings this short can keep their index in the hash field.
8706 static const int kMaxCachedArrayIndexLength = 7;
8707
8708 // For strings which are array indexes the hash value has the string length
8709 // mixed into the hash, mainly to avoid a hash value of zero which would be
8710 // the case for the string '0'. 24 bits are used for the array index value.
8711 static const int kArrayIndexValueBits = 24;
8712 static const int kArrayIndexLengthBits =
8713 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
8714
8715 STATIC_ASSERT((kArrayIndexLengthBits > 0));
8716
8717 class ArrayIndexValueBits : public BitField<unsigned int, kNofHashBitFields,
8718 kArrayIndexValueBits> {}; // NOLINT
8719 class ArrayIndexLengthBits : public BitField<unsigned int,
8720 kNofHashBitFields + kArrayIndexValueBits,
8721 kArrayIndexLengthBits> {}; // NOLINT
8722
8723 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
8724 // could use a mask to test if the length of string is less than or equal to
8725 // kMaxCachedArrayIndexLength.
8726 STATIC_ASSERT(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
8727
8728 static const unsigned int kContainsCachedArrayIndexMask =
8729 (~static_cast<unsigned>(kMaxCachedArrayIndexLength)
8730 << ArrayIndexLengthBits::kShift) |
8731 kIsNotArrayIndexMask;
8732
8733 // Value of empty hash field indicating that the hash is not computed.
8734 static const int kEmptyHashField =
8735 kIsNotArrayIndexMask | kHashNotComputedMask;
8736
8737 protected:
8738 static inline bool IsHashFieldComputed(uint32_t field);
8739
8740 private:
8741 DISALLOW_IMPLICIT_CONSTRUCTORS(Name);
8742};
8743
8744
8745// ES6 symbols.
8746class Symbol: public Name {
8747 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008748 // [name]: The print name of a symbol, or undefined if none.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008749 DECL_ACCESSORS(name, Object)
8750
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008751 DECL_INT_ACCESSORS(flags)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008752
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008753 // [is_private]: Whether this is a private symbol. Private symbols can only
8754 // be used to designate own properties of objects.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008755 DECL_BOOLEAN_ACCESSORS(is_private)
8756
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008757 // [is_well_known_symbol]: Whether this is a spec-defined well-known symbol,
8758 // or not. Well-known symbols do not throw when an access check fails during
8759 // a load.
8760 DECL_BOOLEAN_ACCESSORS(is_well_known_symbol)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008761
8762 DECLARE_CAST(Symbol)
8763
8764 // Dispatched behavior.
8765 DECLARE_PRINTER(Symbol)
8766 DECLARE_VERIFIER(Symbol)
8767
8768 // Layout description.
8769 static const int kNameOffset = Name::kSize;
8770 static const int kFlagsOffset = kNameOffset + kPointerSize;
8771 static const int kSize = kFlagsOffset + kPointerSize;
8772
8773 typedef FixedBodyDescriptor<kNameOffset, kFlagsOffset, kSize> BodyDescriptor;
8774
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008775 void SymbolShortPrint(std::ostream& os);
8776
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008777 private:
8778 static const int kPrivateBit = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008779 static const int kWellKnownSymbolBit = 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008780
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008781 const char* PrivateSymbolToName() const;
8782
8783#if TRACE_MAPS
8784 friend class Name; // For PrivateSymbolToName.
8785#endif
8786
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008787 DISALLOW_IMPLICIT_CONSTRUCTORS(Symbol);
8788};
8789
8790
8791class ConsString;
8792
Steve Blocka7e24c12009-10-30 11:49:00 +00008793// The String abstract class captures JavaScript string values:
8794//
8795// Ecma-262:
8796// 4.3.16 String Value
8797// A string value is a member of the type String and is a finite
8798// ordered sequence of zero or more 16-bit unsigned integer values.
8799//
8800// All string values have a length field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008801class String: public Name {
Steve Blocka7e24c12009-10-30 11:49:00 +00008802 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008803 enum Encoding { ONE_BYTE_ENCODING, TWO_BYTE_ENCODING };
8804
8805 // Array index strings this short can keep their index in the hash field.
8806 static const int kMaxCachedArrayIndexLength = 7;
8807
8808 // For strings which are array indexes the hash value has the string length
8809 // mixed into the hash, mainly to avoid a hash value of zero which would be
8810 // the case for the string '0'. 24 bits are used for the array index value.
8811 static const int kArrayIndexValueBits = 24;
8812 static const int kArrayIndexLengthBits =
8813 kBitsPerInt - kArrayIndexValueBits - kNofHashBitFields;
8814
8815 STATIC_ASSERT((kArrayIndexLengthBits > 0));
8816
8817 class ArrayIndexValueBits : public BitField<unsigned int, kNofHashBitFields,
8818 kArrayIndexValueBits> {}; // NOLINT
8819 class ArrayIndexLengthBits : public BitField<unsigned int,
8820 kNofHashBitFields + kArrayIndexValueBits,
8821 kArrayIndexLengthBits> {}; // NOLINT
8822
8823 // Check that kMaxCachedArrayIndexLength + 1 is a power of two so we
8824 // could use a mask to test if the length of string is less than or equal to
8825 // kMaxCachedArrayIndexLength.
8826 STATIC_ASSERT(IS_POWER_OF_TWO(kMaxCachedArrayIndexLength + 1));
8827
8828 static const unsigned int kContainsCachedArrayIndexMask =
8829 (~static_cast<unsigned>(kMaxCachedArrayIndexLength)
8830 << ArrayIndexLengthBits::kShift) |
8831 kIsNotArrayIndexMask;
8832
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008833 class SubStringRange {
8834 public:
8835 explicit inline SubStringRange(String* string, int first = 0,
8836 int length = -1);
8837 class iterator;
8838 inline iterator begin();
8839 inline iterator end();
8840
8841 private:
8842 String* string_;
8843 int first_;
8844 int length_;
8845 };
8846
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008847 // Representation of the flat content of a String.
8848 // A non-flat string doesn't have flat content.
8849 // A flat string has content that's encoded as a sequence of either
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008850 // one-byte chars or two-byte UC16.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008851 // Returned by String::GetFlatContent().
8852 class FlatContent {
8853 public:
8854 // Returns true if the string is flat and this structure contains content.
8855 bool IsFlat() { return state_ != NON_FLAT; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008856 // Returns true if the structure contains one-byte content.
8857 bool IsOneByte() { return state_ == ONE_BYTE; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008858 // Returns true if the structure contains two-byte content.
8859 bool IsTwoByte() { return state_ == TWO_BYTE; }
8860
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008861 // Return the one byte content of the string. Only use if IsOneByte()
8862 // returns true.
8863 Vector<const uint8_t> ToOneByteVector() {
8864 DCHECK_EQ(ONE_BYTE, state_);
8865 return Vector<const uint8_t>(onebyte_start, length_);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008866 }
8867 // Return the two-byte content of the string. Only use if IsTwoByte()
8868 // returns true.
8869 Vector<const uc16> ToUC16Vector() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008870 DCHECK_EQ(TWO_BYTE, state_);
8871 return Vector<const uc16>(twobyte_start, length_);
8872 }
8873
8874 uc16 Get(int i) {
8875 DCHECK(i < length_);
8876 DCHECK(state_ != NON_FLAT);
8877 if (state_ == ONE_BYTE) return onebyte_start[i];
8878 return twobyte_start[i];
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008879 }
8880
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008881 bool UsesSameString(const FlatContent& other) const {
8882 return onebyte_start == other.onebyte_start;
8883 }
8884
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008885 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008886 enum State { NON_FLAT, ONE_BYTE, TWO_BYTE };
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008887
8888 // Constructors only used by String::GetFlatContent().
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008889 explicit FlatContent(const uint8_t* start, int length)
8890 : onebyte_start(start), length_(length), state_(ONE_BYTE) {}
8891 explicit FlatContent(const uc16* start, int length)
8892 : twobyte_start(start), length_(length), state_(TWO_BYTE) { }
8893 FlatContent() : onebyte_start(NULL), length_(0), state_(NON_FLAT) { }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008894
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008895 union {
8896 const uint8_t* onebyte_start;
8897 const uc16* twobyte_start;
8898 };
8899 int length_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008900 State state_;
8901
8902 friend class String;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008903 friend class IterableSubString;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008904 };
8905
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008906 template <typename Char>
8907 INLINE(Vector<const Char> GetCharVector());
8908
Steve Blocka7e24c12009-10-30 11:49:00 +00008909 // Get and set the length of the string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008910 inline int length() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00008911 inline void set_length(int value);
8912
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008913 // Get and set the length of the string using acquire loads and release
8914 // stores.
8915 inline int synchronized_length() const;
8916 inline void synchronized_set_length(int value);
Steve Blocka7e24c12009-10-30 11:49:00 +00008917
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008918 // Returns whether this string has only one-byte chars, i.e. all of them can
8919 // be one-byte encoded. This might be the case even if the string is
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008920 // two-byte. Such strings may appear when the embedder prefers
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008921 // two-byte external representations even for one-byte data.
8922 inline bool IsOneByteRepresentation() const;
8923 inline bool IsTwoByteRepresentation() const;
Steve Blocka7e24c12009-10-30 11:49:00 +00008924
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008925 // Cons and slices have an encoding flag that may not represent the actual
8926 // encoding of the underlying string. This is taken into account here.
8927 // Requires: this->IsFlat()
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008928 inline bool IsOneByteRepresentationUnderneath();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008929 inline bool IsTwoByteRepresentationUnderneath();
8930
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01008931 // NOTE: this should be considered only a hint. False negatives are
8932 // possible.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008933 inline bool HasOnlyOneByteChars();
Steve Block6ded16b2010-05-10 14:33:55 +01008934
Steve Blocka7e24c12009-10-30 11:49:00 +00008935 // Get and set individual two byte chars in the string.
8936 inline void Set(int index, uint16_t value);
8937 // Get individual two byte char in the string. Repeated calls
8938 // to this method are not efficient unless the string is flat.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008939 INLINE(uint16_t Get(int index));
Steve Blocka7e24c12009-10-30 11:49:00 +00008940
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008941 // ES6 section 7.1.3.1 ToNumber Applied to the String Type
8942 static Handle<Object> ToNumber(Handle<String> subject);
8943
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008944 // Flattens the string. Checks first inline to see if it is
Leon Clarkef7060e22010-06-03 12:02:55 +01008945 // necessary. Does nothing if the string is not a cons string.
8946 // Flattening allocates a sequential string with the same data as
8947 // the given string and mutates the cons string to a degenerate
8948 // form, where the first component is the new sequential string and
8949 // the second component is the empty string. If allocation fails,
8950 // this function returns a failure. If flattening succeeds, this
8951 // function returns the sequential string that is now the first
8952 // component of the cons string.
8953 //
8954 // Degenerate cons strings are handled specially by the garbage
8955 // collector (see IsShortcutCandidate).
Steve Blocka7e24c12009-10-30 11:49:00 +00008956
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008957 static inline Handle<String> Flatten(Handle<String> string,
8958 PretenureFlag pretenure = NOT_TENURED);
Leon Clarkef7060e22010-06-03 12:02:55 +01008959
Ben Murdoch69a99ed2011-11-30 16:03:39 +00008960 // Tries to return the content of a flat string as a structure holding either
8961 // a flat vector of char or of uc16.
8962 // If the string isn't flat, and therefore doesn't have flat content, the
8963 // returned structure will report so, and can't provide a vector of either
8964 // kind.
8965 FlatContent GetFlatContent();
8966
8967 // Returns the parent of a sliced string or first part of a flat cons string.
8968 // Requires: StringShape(this).IsIndirect() && this->IsFlat()
8969 inline String* GetUnderlying();
Steve Blocka7e24c12009-10-30 11:49:00 +00008970
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008971 // String relational comparison, implemented according to ES6 section 7.2.11
8972 // Abstract Relational Comparison (step 5): The comparison of Strings uses a
8973 // simple lexicographic ordering on sequences of code unit values. There is no
8974 // attempt to use the more complex, semantically oriented definitions of
8975 // character or string equality and collating order defined in the Unicode
8976 // specification. Therefore String values that are canonically equal according
8977 // to the Unicode standard could test as unequal. In effect this algorithm
8978 // assumes that both Strings are already in normalized form. Also, note that
8979 // for strings containing supplementary characters, lexicographic ordering on
8980 // sequences of UTF-16 code unit values differs from that on sequences of code
8981 // point values.
8982 MUST_USE_RESULT static ComparisonResult Compare(Handle<String> x,
8983 Handle<String> y);
Steve Blocka7e24c12009-10-30 11:49:00 +00008984
Steve Blocka7e24c12009-10-30 11:49:00 +00008985 // String equality operations.
8986 inline bool Equals(String* other);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008987 inline static bool Equals(Handle<String> one, Handle<String> two);
8988 bool IsUtf8EqualTo(Vector<const char> str, bool allow_prefix_match = false);
8989 bool IsOneByteEqualTo(Vector<const uint8_t> str);
Steve Block9fac8402011-05-12 15:51:54 +01008990 bool IsTwoByteEqualTo(Vector<const uc16> str);
Steve Blocka7e24c12009-10-30 11:49:00 +00008991
8992 // Return a UTF8 representation of the string. The string is null
8993 // terminated but may optionally contain nulls. Length is returned
8994 // in length_output if length_output is not a null pointer The string
8995 // should be nearly flat, otherwise the performance of this method may
8996 // be very slow (quadratic in the length). Setting robustness_flag to
8997 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
8998 // handles unexpected data without causing assert failures and it does not
8999 // do any heap allocations. This is useful when printing stack traces.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009000 base::SmartArrayPointer<char> ToCString(AllowNullsFlag allow_nulls,
9001 RobustnessFlag robustness_flag,
9002 int offset, int length,
9003 int* length_output = 0);
9004 base::SmartArrayPointer<char> ToCString(
Steve Blocka7e24c12009-10-30 11:49:00 +00009005 AllowNullsFlag allow_nulls = DISALLOW_NULLS,
9006 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL,
9007 int* length_output = 0);
9008
Steve Blocka7e24c12009-10-30 11:49:00 +00009009 // Return a 16 bit Unicode representation of the string.
9010 // The string should be nearly flat, otherwise the performance of
9011 // of this method may be very bad. Setting robustness_flag to
9012 // ROBUST_STRING_TRAVERSAL invokes behaviour that is robust This means it
9013 // handles unexpected data without causing assert failures and it does not
9014 // do any heap allocations. This is useful when printing stack traces.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009015 base::SmartArrayPointer<uc16> ToWideCString(
Steve Blocka7e24c12009-10-30 11:49:00 +00009016 RobustnessFlag robustness_flag = FAST_STRING_TRAVERSAL);
9017
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009018 bool ComputeArrayIndex(uint32_t* index);
Steve Blocka7e24c12009-10-30 11:49:00 +00009019
9020 // Externalization.
9021 bool MakeExternal(v8::String::ExternalStringResource* resource);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009022 bool MakeExternal(v8::String::ExternalOneByteStringResource* resource);
Steve Blocka7e24c12009-10-30 11:49:00 +00009023
9024 // Conversion.
9025 inline bool AsArrayIndex(uint32_t* index);
9026
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009027 DECLARE_CAST(String)
Steve Blocka7e24c12009-10-30 11:49:00 +00009028
9029 void PrintOn(FILE* out);
9030
9031 // For use during stack traces. Performs rudimentary sanity check.
9032 bool LooksValid();
9033
9034 // Dispatched behavior.
9035 void StringShortPrint(StringStream* accumulator);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009036 void PrintUC16(std::ostream& os, int start = 0, int end = -1); // NOLINT
9037#if defined(DEBUG) || defined(OBJECT_PRINT)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009038 char* ToAsciiArray();
Ben Murdochb0fe1622011-05-05 13:52:32 +01009039#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009040 DECLARE_PRINTER(String)
9041 DECLARE_VERIFIER(String)
9042
Steve Blocka7e24c12009-10-30 11:49:00 +00009043 inline bool IsFlat();
9044
9045 // Layout description.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009046 static const int kLengthOffset = Name::kSize;
9047 static const int kSize = kLengthOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00009048
Steve Blockd0582a62009-12-15 09:54:21 +00009049 // Maximum number of characters to consider when trying to convert a string
9050 // value into an array index.
Steve Blocka7e24c12009-10-30 11:49:00 +00009051 static const int kMaxArrayIndexSize = 10;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009052 STATIC_ASSERT(kMaxArrayIndexSize < (1 << kArrayIndexLengthBits));
Steve Blocka7e24c12009-10-30 11:49:00 +00009053
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009054 // Max char codes.
9055 static const int32_t kMaxOneByteCharCode = unibrow::Latin1::kMaxChar;
9056 static const uint32_t kMaxOneByteCharCodeU = unibrow::Latin1::kMaxChar;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009057 static const int kMaxUtf16CodeUnit = 0xffff;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009058 static const uint32_t kMaxUtf16CodeUnitU = kMaxUtf16CodeUnit;
Ben Murdoch097c5b22016-05-18 11:27:45 +01009059 static const uc32 kMaxCodePoint = 0x10ffff;
Steve Blockd0582a62009-12-15 09:54:21 +00009060
9061 // Maximal string length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009062 static const int kMaxLength = (1 << 28) - 16;
Steve Blockd0582a62009-12-15 09:54:21 +00009063
9064 // Max length for computing hash. For strings longer than this limit the
9065 // string length is used as the hash value.
9066 static const int kMaxHashCalcLength = 16383;
Steve Blocka7e24c12009-10-30 11:49:00 +00009067
9068 // Limit for truncation in short printing.
9069 static const int kMaxShortPrintLength = 1024;
9070
9071 // Support for regular expressions.
Steve Blocka7e24c12009-10-30 11:49:00 +00009072 const uc16* GetTwoByteData(unsigned start);
9073
Steve Blocka7e24c12009-10-30 11:49:00 +00009074 // Helper function for flattening strings.
9075 template <typename sinkchar>
9076 static void WriteToFlat(String* source,
9077 sinkchar* sink,
9078 int from,
9079 int to);
9080
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009081 // The return value may point to the first aligned word containing the first
9082 // non-one-byte character, rather than directly to the non-one-byte character.
9083 // If the return value is >= the passed length, the entire string was
9084 // one-byte.
9085 static inline int NonAsciiStart(const char* chars, int length) {
9086 const char* start = chars;
Steve Block9fac8402011-05-12 15:51:54 +01009087 const char* limit = chars + length;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009088
9089 if (length >= kIntptrSize) {
9090 // Check unaligned bytes.
9091 while (!IsAligned(reinterpret_cast<intptr_t>(chars), sizeof(uintptr_t))) {
9092 if (static_cast<uint8_t>(*chars) > unibrow::Utf8::kMaxOneByteChar) {
9093 return static_cast<int>(chars - start);
9094 }
9095 ++chars;
Steve Block9fac8402011-05-12 15:51:54 +01009096 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009097 // Check aligned words.
9098 DCHECK(unibrow::Utf8::kMaxOneByteChar == 0x7F);
9099 const uintptr_t non_one_byte_mask = kUintptrAllBitsSet / 0xFF * 0x80;
9100 while (chars + sizeof(uintptr_t) <= limit) {
9101 if (*reinterpret_cast<const uintptr_t*>(chars) & non_one_byte_mask) {
9102 return static_cast<int>(chars - start);
9103 }
9104 chars += sizeof(uintptr_t);
9105 }
Steve Block9fac8402011-05-12 15:51:54 +01009106 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009107 // Check remaining unaligned bytes.
Steve Block9fac8402011-05-12 15:51:54 +01009108 while (chars < limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009109 if (static_cast<uint8_t>(*chars) > unibrow::Utf8::kMaxOneByteChar) {
9110 return static_cast<int>(chars - start);
9111 }
Steve Block9fac8402011-05-12 15:51:54 +01009112 ++chars;
9113 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009114
9115 return static_cast<int>(chars - start);
Steve Block9fac8402011-05-12 15:51:54 +01009116 }
9117
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009118 static inline bool IsAscii(const char* chars, int length) {
9119 return NonAsciiStart(chars, length) >= length;
9120 }
9121
9122 static inline bool IsAscii(const uint8_t* chars, int length) {
9123 return
9124 NonAsciiStart(reinterpret_cast<const char*>(chars), length) >= length;
9125 }
9126
9127 static inline int NonOneByteStart(const uc16* chars, int length) {
Steve Block9fac8402011-05-12 15:51:54 +01009128 const uc16* limit = chars + length;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009129 const uc16* start = chars;
Steve Block9fac8402011-05-12 15:51:54 +01009130 while (chars < limit) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009131 if (*chars > kMaxOneByteCharCodeU) return static_cast<int>(chars - start);
Steve Block9fac8402011-05-12 15:51:54 +01009132 ++chars;
9133 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009134 return static_cast<int>(chars - start);
Steve Block9fac8402011-05-12 15:51:54 +01009135 }
9136
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009137 static inline bool IsOneByte(const uc16* chars, int length) {
9138 return NonOneByteStart(chars, length) >= length;
9139 }
Steve Blocka7e24c12009-10-30 11:49:00 +00009140
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009141 template<class Visitor>
9142 static inline ConsString* VisitFlat(Visitor* visitor,
9143 String* string,
9144 int offset = 0);
9145
9146 static Handle<FixedArray> CalculateLineEnds(Handle<String> string,
9147 bool include_ending_line);
9148
9149 // Use the hash field to forward to the canonical internalized string
9150 // when deserializing an internalized string.
9151 inline void SetForwardedInternalizedString(String* string);
9152 inline String* GetForwardedInternalizedString();
Steve Blocka7e24c12009-10-30 11:49:00 +00009153
9154 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009155 friend class Name;
9156 friend class StringTableInsertionKey;
Leon Clarkef7060e22010-06-03 12:02:55 +01009157
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009158 static Handle<String> SlowFlatten(Handle<ConsString> cons,
9159 PretenureFlag tenure);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01009160
Steve Blocka7e24c12009-10-30 11:49:00 +00009161 // Slow case of String::Equals. This implementation works on any strings
9162 // but it is most efficient on strings that are almost flat.
9163 bool SlowEquals(String* other);
9164
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009165 static bool SlowEquals(Handle<String> one, Handle<String> two);
9166
Steve Blocka7e24c12009-10-30 11:49:00 +00009167 // Slow case of AsArrayIndex.
9168 bool SlowAsArrayIndex(uint32_t* index);
9169
9170 // Compute and set the hash code.
9171 uint32_t ComputeAndSetHash();
9172
9173 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
9174};
9175
9176
9177// The SeqString abstract class captures sequential string values.
9178class SeqString: public String {
9179 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009180 DECLARE_CAST(SeqString)
Steve Blocka7e24c12009-10-30 11:49:00 +00009181
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009182 // Layout description.
9183 static const int kHeaderSize = String::kSize;
9184
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009185 // Truncate the string in-place if possible and return the result.
9186 // In case of new_length == 0, the empty string is returned without
9187 // truncating the original string.
9188 MUST_USE_RESULT static Handle<String> Truncate(Handle<SeqString> string,
9189 int new_length);
Steve Blocka7e24c12009-10-30 11:49:00 +00009190 private:
9191 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqString);
9192};
9193
9194
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009195// The OneByteString class captures sequential one-byte string objects.
9196// Each character in the OneByteString is an one-byte character.
9197class SeqOneByteString: public SeqString {
Steve Blocka7e24c12009-10-30 11:49:00 +00009198 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009199 static const bool kHasOneByteEncoding = true;
Leon Clarkeac952652010-07-15 11:15:24 +01009200
Steve Blocka7e24c12009-10-30 11:49:00 +00009201 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009202 inline uint16_t SeqOneByteStringGet(int index);
9203 inline void SeqOneByteStringSet(int index, uint16_t value);
Steve Blocka7e24c12009-10-30 11:49:00 +00009204
9205 // Get the address of the characters in this string.
9206 inline Address GetCharsAddress();
9207
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009208 inline uint8_t* GetChars();
Steve Blocka7e24c12009-10-30 11:49:00 +00009209
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009210 DECLARE_CAST(SeqOneByteString)
Steve Blocka7e24c12009-10-30 11:49:00 +00009211
9212 // Garbage collection support. This method is called by the
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009213 // garbage collector to compute the actual size of an OneByteString
Steve Blocka7e24c12009-10-30 11:49:00 +00009214 // instance.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009215 inline int SeqOneByteStringSize(InstanceType instance_type);
Steve Blocka7e24c12009-10-30 11:49:00 +00009216
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009217 // Computes the size for an OneByteString instance of a given length.
Steve Blocka7e24c12009-10-30 11:49:00 +00009218 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01009219 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kCharSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00009220 }
9221
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009222 // Maximal memory usage for a single sequential one-byte string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009223 static const int kMaxSize = 512 * MB - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009224 STATIC_ASSERT((kMaxSize - kHeaderSize) >= String::kMaxLength);
Steve Blocka7e24c12009-10-30 11:49:00 +00009225
9226 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009227 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqOneByteString);
Steve Blocka7e24c12009-10-30 11:49:00 +00009228};
9229
9230
9231// The TwoByteString class captures sequential unicode string objects.
9232// Each character in the TwoByteString is a two-byte uint16_t.
9233class SeqTwoByteString: public SeqString {
9234 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009235 static const bool kHasOneByteEncoding = false;
Leon Clarkeac952652010-07-15 11:15:24 +01009236
Steve Blocka7e24c12009-10-30 11:49:00 +00009237 // Dispatched behavior.
9238 inline uint16_t SeqTwoByteStringGet(int index);
9239 inline void SeqTwoByteStringSet(int index, uint16_t value);
9240
9241 // Get the address of the characters in this string.
9242 inline Address GetCharsAddress();
9243
9244 inline uc16* GetChars();
9245
9246 // For regexp code.
9247 const uint16_t* SeqTwoByteStringGetData(unsigned start);
9248
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009249 DECLARE_CAST(SeqTwoByteString)
Steve Blocka7e24c12009-10-30 11:49:00 +00009250
9251 // Garbage collection support. This method is called by the
9252 // garbage collector to compute the actual size of a TwoByteString
9253 // instance.
9254 inline int SeqTwoByteStringSize(InstanceType instance_type);
9255
9256 // Computes the size for a TwoByteString instance of a given length.
9257 static int SizeFor(int length) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01009258 return OBJECT_POINTER_ALIGN(kHeaderSize + length * kShortSize);
Steve Blocka7e24c12009-10-30 11:49:00 +00009259 }
9260
Leon Clarkee46be812010-01-19 14:06:41 +00009261 // Maximal memory usage for a single sequential two-byte string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009262 static const int kMaxSize = 512 * MB - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009263 STATIC_ASSERT(static_cast<int>((kMaxSize - kHeaderSize)/sizeof(uint16_t)) >=
9264 String::kMaxLength);
Steve Blocka7e24c12009-10-30 11:49:00 +00009265
9266 private:
9267 DISALLOW_IMPLICIT_CONSTRUCTORS(SeqTwoByteString);
9268};
9269
9270
9271// The ConsString class describes string values built by using the
9272// addition operator on strings. A ConsString is a pair where the
9273// first and second components are pointers to other string values.
9274// One or both components of a ConsString can be pointers to other
9275// ConsStrings, creating a binary tree of ConsStrings where the leaves
9276// are non-ConsString string values. The string value represented by
9277// a ConsString can be obtained by concatenating the leaf string
9278// values in a left-to-right depth-first traversal of the tree.
9279class ConsString: public String {
9280 public:
9281 // First string of the cons cell.
9282 inline String* first();
9283 // Doesn't check that the result is a string, even in debug mode. This is
9284 // useful during GC where the mark bits confuse the checks.
9285 inline Object* unchecked_first();
9286 inline void set_first(String* first,
9287 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
9288
9289 // Second string of the cons cell.
9290 inline String* second();
9291 // Doesn't check that the result is a string, even in debug mode. This is
9292 // useful during GC where the mark bits confuse the checks.
9293 inline Object* unchecked_second();
9294 inline void set_second(String* second,
9295 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
9296
9297 // Dispatched behavior.
9298 uint16_t ConsStringGet(int index);
9299
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009300 DECLARE_CAST(ConsString)
Steve Blocka7e24c12009-10-30 11:49:00 +00009301
Steve Blocka7e24c12009-10-30 11:49:00 +00009302 // Layout description.
9303 static const int kFirstOffset = POINTER_SIZE_ALIGN(String::kSize);
9304 static const int kSecondOffset = kFirstOffset + kPointerSize;
9305 static const int kSize = kSecondOffset + kPointerSize;
9306
Steve Blocka7e24c12009-10-30 11:49:00 +00009307 // Minimum length for a cons string.
9308 static const int kMinLength = 13;
9309
Iain Merrick75681382010-08-19 15:07:18 +01009310 typedef FixedBodyDescriptor<kFirstOffset, kSecondOffset + kPointerSize, kSize>
9311 BodyDescriptor;
9312
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009313 DECLARE_VERIFIER(ConsString)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009314
Steve Blocka7e24c12009-10-30 11:49:00 +00009315 private:
9316 DISALLOW_IMPLICIT_CONSTRUCTORS(ConsString);
9317};
9318
9319
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009320// The Sliced String class describes strings that are substrings of another
9321// sequential string. The motivation is to save time and memory when creating
9322// a substring. A Sliced String is described as a pointer to the parent,
9323// the offset from the start of the parent string and the length. Using
9324// a Sliced String therefore requires unpacking of the parent string and
9325// adding the offset to the start address. A substring of a Sliced String
9326// are not nested since the double indirection is simplified when creating
9327// such a substring.
9328// Currently missing features are:
9329// - handling externalized parent strings
9330// - external strings as parent
9331// - truncating sliced string to enable otherwise unneeded parent to be GC'ed.
9332class SlicedString: public String {
9333 public:
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009334 inline String* parent();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009335 inline void set_parent(String* parent,
9336 WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
9337 inline int offset() const;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009338 inline void set_offset(int offset);
9339
9340 // Dispatched behavior.
9341 uint16_t SlicedStringGet(int index);
9342
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009343 DECLARE_CAST(SlicedString)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009344
9345 // Layout description.
9346 static const int kParentOffset = POINTER_SIZE_ALIGN(String::kSize);
9347 static const int kOffsetOffset = kParentOffset + kPointerSize;
9348 static const int kSize = kOffsetOffset + kPointerSize;
9349
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009350 // Minimum length for a sliced string.
9351 static const int kMinLength = 13;
9352
9353 typedef FixedBodyDescriptor<kParentOffset,
9354 kOffsetOffset + kPointerSize, kSize>
9355 BodyDescriptor;
9356
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009357 DECLARE_VERIFIER(SlicedString)
Ben Murdoch69a99ed2011-11-30 16:03:39 +00009358
9359 private:
9360 DISALLOW_IMPLICIT_CONSTRUCTORS(SlicedString);
9361};
9362
9363
Steve Blocka7e24c12009-10-30 11:49:00 +00009364// The ExternalString class describes string values that are backed by
9365// a string resource that lies outside the V8 heap. ExternalStrings
9366// consist of the length field common to all strings, a pointer to the
9367// external resource. It is important to ensure (externally) that the
9368// resource is not deallocated while the ExternalString is live in the
9369// V8 heap.
9370//
9371// The API expects that all ExternalStrings are created through the
9372// API. Therefore, ExternalStrings should not be used internally.
9373class ExternalString: public String {
9374 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009375 DECLARE_CAST(ExternalString)
Steve Blocka7e24c12009-10-30 11:49:00 +00009376
9377 // Layout description.
9378 static const int kResourceOffset = POINTER_SIZE_ALIGN(String::kSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009379 static const int kShortSize = kResourceOffset + kPointerSize;
9380 static const int kResourceDataOffset = kResourceOffset + kPointerSize;
9381 static const int kSize = kResourceDataOffset + kPointerSize;
9382
9383 // Return whether external string is short (data pointer is not cached).
9384 inline bool is_short();
Steve Blocka7e24c12009-10-30 11:49:00 +00009385
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009386 STATIC_ASSERT(kResourceOffset == Internals::kStringResourceOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +00009387
9388 private:
9389 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalString);
9390};
9391
9392
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009393// The ExternalOneByteString class is an external string backed by an
9394// one-byte string.
9395class ExternalOneByteString : public ExternalString {
Steve Blocka7e24c12009-10-30 11:49:00 +00009396 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009397 static const bool kHasOneByteEncoding = true;
Leon Clarkeac952652010-07-15 11:15:24 +01009398
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009399 typedef v8::String::ExternalOneByteStringResource Resource;
Steve Blocka7e24c12009-10-30 11:49:00 +00009400
9401 // The underlying resource.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009402 inline const Resource* resource();
9403 inline void set_resource(const Resource* buffer);
9404
9405 // Update the pointer cache to the external character array.
9406 // The cached pointer is always valid, as the external character array does =
9407 // not move during lifetime. Deserialization is the only exception, after
9408 // which the pointer cache has to be refreshed.
9409 inline void update_data_cache();
9410
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009411 inline const uint8_t* GetChars();
Steve Blocka7e24c12009-10-30 11:49:00 +00009412
9413 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009414 inline uint16_t ExternalOneByteStringGet(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00009415
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009416 DECLARE_CAST(ExternalOneByteString)
Steve Blocka7e24c12009-10-30 11:49:00 +00009417
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009418 class BodyDescriptor;
Steve Blocka7e24c12009-10-30 11:49:00 +00009419
Steve Blocka7e24c12009-10-30 11:49:00 +00009420 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009421 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalOneByteString);
Steve Blocka7e24c12009-10-30 11:49:00 +00009422};
9423
9424
9425// The ExternalTwoByteString class is an external string backed by a UTF-16
9426// encoded string.
9427class ExternalTwoByteString: public ExternalString {
9428 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009429 static const bool kHasOneByteEncoding = false;
Leon Clarkeac952652010-07-15 11:15:24 +01009430
Steve Blocka7e24c12009-10-30 11:49:00 +00009431 typedef v8::String::ExternalStringResource Resource;
9432
9433 // The underlying string resource.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009434 inline const Resource* resource();
9435 inline void set_resource(const Resource* buffer);
9436
9437 // Update the pointer cache to the external character array.
9438 // The cached pointer is always valid, as the external character array does =
9439 // not move during lifetime. Deserialization is the only exception, after
9440 // which the pointer cache has to be refreshed.
9441 inline void update_data_cache();
9442
9443 inline const uint16_t* GetChars();
Steve Blocka7e24c12009-10-30 11:49:00 +00009444
9445 // Dispatched behavior.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009446 inline uint16_t ExternalTwoByteStringGet(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00009447
9448 // For regexp code.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009449 inline const uint16_t* ExternalTwoByteStringGetData(unsigned start);
Steve Blocka7e24c12009-10-30 11:49:00 +00009450
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009451 DECLARE_CAST(ExternalTwoByteString)
Steve Blocka7e24c12009-10-30 11:49:00 +00009452
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009453 class BodyDescriptor;
Iain Merrick75681382010-08-19 15:07:18 +01009454
Steve Blocka7e24c12009-10-30 11:49:00 +00009455 private:
9456 DISALLOW_IMPLICIT_CONSTRUCTORS(ExternalTwoByteString);
9457};
9458
9459
9460// Utility superclass for stack-allocated objects that must be updated
9461// on gc. It provides two ways for the gc to update instances, either
9462// iterating or updating after gc.
9463class Relocatable BASE_EMBEDDED {
9464 public:
Steve Block44f0eee2011-05-26 01:26:41 +01009465 explicit inline Relocatable(Isolate* isolate);
9466 inline virtual ~Relocatable();
Steve Blocka7e24c12009-10-30 11:49:00 +00009467 virtual void IterateInstance(ObjectVisitor* v) { }
9468 virtual void PostGarbageCollection() { }
9469
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009470 static void PostGarbageCollectionProcessing(Isolate* isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +00009471 static int ArchiveSpacePerThread();
Ben Murdoch257744e2011-11-30 15:57:28 +00009472 static char* ArchiveState(Isolate* isolate, char* to);
9473 static char* RestoreState(Isolate* isolate, char* from);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009474 static void Iterate(Isolate* isolate, ObjectVisitor* v);
Steve Blocka7e24c12009-10-30 11:49:00 +00009475 static void Iterate(ObjectVisitor* v, Relocatable* top);
9476 static char* Iterate(ObjectVisitor* v, char* t);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009477
Steve Blocka7e24c12009-10-30 11:49:00 +00009478 private:
Steve Block44f0eee2011-05-26 01:26:41 +01009479 Isolate* isolate_;
Steve Blocka7e24c12009-10-30 11:49:00 +00009480 Relocatable* prev_;
9481};
9482
9483
9484// A flat string reader provides random access to the contents of a
9485// string independent of the character width of the string. The handle
9486// must be valid as long as the reader is being used.
9487class FlatStringReader : public Relocatable {
9488 public:
Steve Block44f0eee2011-05-26 01:26:41 +01009489 FlatStringReader(Isolate* isolate, Handle<String> str);
9490 FlatStringReader(Isolate* isolate, Vector<const char> input);
Steve Blocka7e24c12009-10-30 11:49:00 +00009491 void PostGarbageCollection();
9492 inline uc32 Get(int index);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009493 template <typename Char>
9494 inline Char Get(int index);
Steve Blocka7e24c12009-10-30 11:49:00 +00009495 int length() { return length_; }
9496 private:
9497 String** str_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009498 bool is_one_byte_;
Steve Blocka7e24c12009-10-30 11:49:00 +00009499 int length_;
9500 const void* start_;
9501};
9502
9503
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009504// This maintains an off-stack representation of the stack frames required
9505// to traverse a ConsString, allowing an entirely iterative and restartable
9506// traversal of the entire string
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009507class ConsStringIterator {
Steve Blocka7e24c12009-10-30 11:49:00 +00009508 public:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009509 inline ConsStringIterator() {}
9510 inline explicit ConsStringIterator(ConsString* cons_string, int offset = 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009511 Reset(cons_string, offset);
9512 }
9513 inline void Reset(ConsString* cons_string, int offset = 0) {
9514 depth_ = 0;
9515 // Next will always return NULL.
9516 if (cons_string == NULL) return;
9517 Initialize(cons_string, offset);
9518 }
9519 // Returns NULL when complete.
9520 inline String* Next(int* offset_out) {
9521 *offset_out = 0;
9522 if (depth_ == 0) return NULL;
9523 return Continue(offset_out);
9524 }
9525
9526 private:
9527 static const int kStackSize = 32;
9528 // Use a mask instead of doing modulo operations for stack wrapping.
9529 static const int kDepthMask = kStackSize-1;
9530 STATIC_ASSERT(IS_POWER_OF_TWO(kStackSize));
9531 static inline int OffsetForDepth(int depth);
9532
9533 inline void PushLeft(ConsString* string);
9534 inline void PushRight(ConsString* string);
9535 inline void AdjustMaximumDepth();
9536 inline void Pop();
9537 inline bool StackBlown() { return maximum_depth_ - depth_ == kStackSize; }
9538 void Initialize(ConsString* cons_string, int offset);
9539 String* Continue(int* offset_out);
9540 String* NextLeaf(bool* blew_stack);
9541 String* Search(int* offset_out);
9542
9543 // Stack must always contain only frames for which right traversal
9544 // has not yet been performed.
9545 ConsString* frames_[kStackSize];
9546 ConsString* root_;
9547 int depth_;
9548 int maximum_depth_;
9549 int consumed_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009550 DISALLOW_COPY_AND_ASSIGN(ConsStringIterator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009551};
9552
9553
9554class StringCharacterStream {
9555 public:
9556 inline StringCharacterStream(String* string,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009557 int offset = 0);
9558 inline uint16_t GetNext();
9559 inline bool HasMore();
9560 inline void Reset(String* string, int offset = 0);
9561 inline void VisitOneByteString(const uint8_t* chars, int length);
9562 inline void VisitTwoByteString(const uint16_t* chars, int length);
9563
9564 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009565 ConsStringIterator iter_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009566 bool is_one_byte_;
9567 union {
9568 const uint8_t* buffer8_;
9569 const uint16_t* buffer16_;
9570 };
9571 const uint8_t* end_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009572 DISALLOW_COPY_AND_ASSIGN(StringCharacterStream);
Steve Blocka7e24c12009-10-30 11:49:00 +00009573};
9574
9575
9576template <typename T>
9577class VectorIterator {
9578 public:
9579 VectorIterator(T* d, int l) : data_(Vector<const T>(d, l)), index_(0) { }
9580 explicit VectorIterator(Vector<const T> data) : data_(data), index_(0) { }
9581 T GetNext() { return data_[index_++]; }
9582 bool has_more() { return index_ < data_.length(); }
9583 private:
9584 Vector<const T> data_;
9585 int index_;
9586};
9587
9588
9589// The Oddball describes objects null, undefined, true, and false.
9590class Oddball: public HeapObject {
9591 public:
9592 // [to_string]: Cached to_string computed at startup.
9593 DECL_ACCESSORS(to_string, String)
9594
9595 // [to_number]: Cached to_number computed at startup.
9596 DECL_ACCESSORS(to_number, Object)
9597
Ben Murdochda12d292016-06-02 14:46:10 +01009598 // [to_number]: Cached to_boolean computed at startup.
9599 DECL_ACCESSORS(to_boolean, Oddball)
9600
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009601 // [typeof]: Cached type_of computed at startup.
9602 DECL_ACCESSORS(type_of, String)
9603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009604 inline byte kind() const;
Steve Block44f0eee2011-05-26 01:26:41 +01009605 inline void set_kind(byte kind);
9606
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009607 // ES6 section 7.1.3 ToNumber for Boolean, Null, Undefined.
9608 MUST_USE_RESULT static inline Handle<Object> ToNumber(Handle<Oddball> input);
9609
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009610 DECLARE_CAST(Oddball)
Steve Blocka7e24c12009-10-30 11:49:00 +00009611
9612 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009613 DECLARE_VERIFIER(Oddball)
Steve Blocka7e24c12009-10-30 11:49:00 +00009614
9615 // Initialize the fields.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009616 static void Initialize(Isolate* isolate, Handle<Oddball> oddball,
9617 const char* to_string, Handle<Object> to_number,
Ben Murdochda12d292016-06-02 14:46:10 +01009618 bool to_boolean, const char* type_of, byte kind);
Steve Blocka7e24c12009-10-30 11:49:00 +00009619
9620 // Layout description.
9621 static const int kToStringOffset = HeapObject::kHeaderSize;
9622 static const int kToNumberOffset = kToStringOffset + kPointerSize;
Ben Murdochda12d292016-06-02 14:46:10 +01009623 static const int kToBooleanOffset = kToNumberOffset + kPointerSize;
9624 static const int kTypeOfOffset = kToBooleanOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009625 static const int kKindOffset = kTypeOfOffset + kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +01009626 static const int kSize = kKindOffset + kPointerSize;
9627
9628 static const byte kFalse = 0;
9629 static const byte kTrue = 1;
9630 static const byte kNotBooleanMask = ~1;
9631 static const byte kTheHole = 2;
9632 static const byte kNull = 3;
Ben Murdoch097c5b22016-05-18 11:27:45 +01009633 static const byte kArgumentsMarker = 4;
Steve Block44f0eee2011-05-26 01:26:41 +01009634 static const byte kUndefined = 5;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009635 static const byte kUninitialized = 6;
9636 static const byte kOther = 7;
9637 static const byte kException = 8;
Ben Murdochda12d292016-06-02 14:46:10 +01009638 static const byte kOptimizedOut = 9;
Steve Blocka7e24c12009-10-30 11:49:00 +00009639
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009640 typedef FixedBodyDescriptor<kToStringOffset, kTypeOfOffset + kPointerSize,
Iain Merrick75681382010-08-19 15:07:18 +01009641 kSize> BodyDescriptor;
9642
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009643 STATIC_ASSERT(kKindOffset == Internals::kOddballKindOffset);
9644 STATIC_ASSERT(kNull == Internals::kNullOddballKind);
9645 STATIC_ASSERT(kUndefined == Internals::kUndefinedOddballKind);
9646
Steve Blocka7e24c12009-10-30 11:49:00 +00009647 private:
9648 DISALLOW_IMPLICIT_CONSTRUCTORS(Oddball);
9649};
9650
9651
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009652class Cell: public HeapObject {
Steve Blocka7e24c12009-10-30 11:49:00 +00009653 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009654 // [value]: value of the cell.
Steve Blocka7e24c12009-10-30 11:49:00 +00009655 DECL_ACCESSORS(value, Object)
9656
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009657 DECLARE_CAST(Cell)
Steve Blocka7e24c12009-10-30 11:49:00 +00009658
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009659 static inline Cell* FromValueAddress(Address value) {
9660 Object* result = FromAddress(value - kValueOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009661 return static_cast<Cell*>(result);
Ben Murdochb0fe1622011-05-05 13:52:32 +01009662 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009663
9664 inline Address ValueAddress() {
9665 return address() + kValueOffset;
9666 }
9667
9668 // Dispatched behavior.
9669 DECLARE_PRINTER(Cell)
9670 DECLARE_VERIFIER(Cell)
Steve Blocka7e24c12009-10-30 11:49:00 +00009671
9672 // Layout description.
9673 static const int kValueOffset = HeapObject::kHeaderSize;
9674 static const int kSize = kValueOffset + kPointerSize;
9675
Iain Merrick75681382010-08-19 15:07:18 +01009676 typedef FixedBodyDescriptor<kValueOffset,
9677 kValueOffset + kPointerSize,
9678 kSize> BodyDescriptor;
9679
Steve Blocka7e24c12009-10-30 11:49:00 +00009680 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009681 DISALLOW_IMPLICIT_CONSTRUCTORS(Cell);
9682};
9683
9684
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009685class PropertyCell : public HeapObject {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009686 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009687 // [property_details]: details of the global property.
9688 DECL_ACCESSORS(property_details_raw, Object)
9689 // [value]: value of the global property.
9690 DECL_ACCESSORS(value, Object)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009691 // [dependent_code]: dependent code that depends on the type of the global
9692 // property.
9693 DECL_ACCESSORS(dependent_code, DependentCode)
9694
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009695 inline PropertyDetails property_details();
9696 inline void set_property_details(PropertyDetails details);
9697
9698 PropertyCellConstantType GetConstantType();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009699
9700 // Computes the new type of the cell's contents for the given value, but
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009701 // without actually modifying the details.
9702 static PropertyCellType UpdatedType(Handle<PropertyCell> cell,
9703 Handle<Object> value,
9704 PropertyDetails details);
9705 static void UpdateCell(Handle<GlobalDictionary> dictionary, int entry,
9706 Handle<Object> value, PropertyDetails details);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009707
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009708 static Handle<PropertyCell> InvalidateEntry(
9709 Handle<GlobalDictionary> dictionary, int entry);
9710
9711 static void SetValueWithInvalidation(Handle<PropertyCell> cell,
9712 Handle<Object> new_value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009713
9714 DECLARE_CAST(PropertyCell)
9715
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009716 // Dispatched behavior.
9717 DECLARE_PRINTER(PropertyCell)
9718 DECLARE_VERIFIER(PropertyCell)
9719
9720 // Layout description.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009721 static const int kDetailsOffset = HeapObject::kHeaderSize;
9722 static const int kValueOffset = kDetailsOffset + kPointerSize;
9723 static const int kDependentCodeOffset = kValueOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009724 static const int kSize = kDependentCodeOffset + kPointerSize;
9725
9726 static const int kPointerFieldsBeginOffset = kValueOffset;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009727 static const int kPointerFieldsEndOffset = kSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009728
9729 typedef FixedBodyDescriptor<kValueOffset,
9730 kSize,
9731 kSize> BodyDescriptor;
9732
9733 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009734 DISALLOW_IMPLICIT_CONSTRUCTORS(PropertyCell);
Steve Blocka7e24c12009-10-30 11:49:00 +00009735};
9736
9737
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009738class WeakCell : public HeapObject {
9739 public:
9740 inline Object* value() const;
9741
9742 // This should not be called by anyone except GC.
9743 inline void clear();
9744
9745 // This should not be called by anyone except allocator.
9746 inline void initialize(HeapObject* value);
9747
9748 inline bool cleared() const;
9749
9750 DECL_ACCESSORS(next, Object)
9751
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009752 inline void clear_next(Object* the_hole_value);
9753
9754 inline bool next_cleared();
9755
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009756 DECLARE_CAST(WeakCell)
9757
9758 DECLARE_PRINTER(WeakCell)
9759 DECLARE_VERIFIER(WeakCell)
9760
9761 // Layout description.
9762 static const int kValueOffset = HeapObject::kHeaderSize;
9763 static const int kNextOffset = kValueOffset + kPointerSize;
9764 static const int kSize = kNextOffset + kPointerSize;
9765
9766 typedef FixedBodyDescriptor<kValueOffset, kSize, kSize> BodyDescriptor;
9767
9768 private:
9769 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakCell);
9770};
9771
9772
Ben Murdoch257744e2011-11-30 15:57:28 +00009773// The JSProxy describes EcmaScript Harmony proxies
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009774class JSProxy: public JSReceiver {
Steve Blocka7e24c12009-10-30 11:49:00 +00009775 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009776 MUST_USE_RESULT static MaybeHandle<JSProxy> New(Isolate* isolate,
9777 Handle<Object>,
9778 Handle<Object>);
9779
Ben Murdoch257744e2011-11-30 15:57:28 +00009780 // [handler]: The handler property.
9781 DECL_ACCESSORS(handler, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009782 // [target]: The target property.
9783 DECL_ACCESSORS(target, JSReceiver)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009784 // [hash]: The hash code property (undefined if not initialized yet).
9785 DECL_ACCESSORS(hash, Object)
9786
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009787 static MaybeHandle<Context> GetFunctionRealm(Handle<JSProxy> proxy);
9788
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009789 DECLARE_CAST(JSProxy)
Steve Blocka7e24c12009-10-30 11:49:00 +00009790
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009791 INLINE(bool IsRevoked() const);
9792 static void Revoke(Handle<JSProxy> proxy);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009793
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009794 // ES6 9.5.1
9795 static MaybeHandle<Object> GetPrototype(Handle<JSProxy> receiver);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009796
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009797 // ES6 9.5.2
9798 MUST_USE_RESULT static Maybe<bool> SetPrototype(Handle<JSProxy> proxy,
9799 Handle<Object> value,
9800 bool from_javascript,
9801 ShouldThrow should_throw);
9802 // ES6 9.5.3
9803 MUST_USE_RESULT static Maybe<bool> IsExtensible(Handle<JSProxy> proxy);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009804
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009805 // ES6 9.5.4 (when passed DONT_THROW)
9806 MUST_USE_RESULT static Maybe<bool> PreventExtensions(
9807 Handle<JSProxy> proxy, ShouldThrow should_throw);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009808
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009809 // ES6 9.5.5
9810 MUST_USE_RESULT static Maybe<bool> GetOwnPropertyDescriptor(
9811 Isolate* isolate, Handle<JSProxy> proxy, Handle<Name> name,
9812 PropertyDescriptor* desc);
Ben Murdoch589d6972011-11-30 16:04:58 +00009813
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009814 // ES6 9.5.6
9815 MUST_USE_RESULT static Maybe<bool> DefineOwnProperty(
9816 Isolate* isolate, Handle<JSProxy> object, Handle<Object> key,
9817 PropertyDescriptor* desc, ShouldThrow should_throw);
9818
9819 // ES6 9.5.7
9820 MUST_USE_RESULT static Maybe<bool> HasProperty(Isolate* isolate,
9821 Handle<JSProxy> proxy,
9822 Handle<Name> name);
9823
9824 // ES6 9.5.8
9825 MUST_USE_RESULT static MaybeHandle<Object> GetProperty(
9826 Isolate* isolate, Handle<JSProxy> proxy, Handle<Name> name,
Ben Murdochda12d292016-06-02 14:46:10 +01009827 Handle<Object> receiver, bool* was_found);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009828
9829 // ES6 9.5.9
9830 MUST_USE_RESULT static Maybe<bool> SetProperty(Handle<JSProxy> proxy,
9831 Handle<Name> name,
9832 Handle<Object> value,
9833 Handle<Object> receiver,
9834 LanguageMode language_mode);
9835
9836 // ES6 9.5.10 (when passed SLOPPY)
9837 MUST_USE_RESULT static Maybe<bool> DeletePropertyOrElement(
9838 Handle<JSProxy> proxy, Handle<Name> name, LanguageMode language_mode);
9839
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009840 // ES6 9.5.12
9841 MUST_USE_RESULT static Maybe<bool> OwnPropertyKeys(
9842 Isolate* isolate, Handle<JSReceiver> receiver, Handle<JSProxy> proxy,
9843 PropertyFilter filter, KeyAccumulator* accumulator);
9844
9845 MUST_USE_RESULT static Maybe<PropertyAttributes> GetPropertyAttributes(
9846 LookupIterator* it);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009847
Steve Blocka7e24c12009-10-30 11:49:00 +00009848 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009849 DECLARE_PRINTER(JSProxy)
9850 DECLARE_VERIFIER(JSProxy)
Ben Murdoch257744e2011-11-30 15:57:28 +00009851
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009852 // Layout description.
9853 static const int kTargetOffset = JSReceiver::kHeaderSize;
9854 static const int kHandlerOffset = kTargetOffset + kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009855 static const int kHashOffset = kHandlerOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009856 static const int kSize = kHashOffset + kPointerSize;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009857
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009858 typedef FixedBodyDescriptor<JSReceiver::kPropertiesOffset, kSize, kSize>
9859 BodyDescriptor;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009860
Ben Murdochda12d292016-06-02 14:46:10 +01009861 static Handle<Object> GetIdentityHash(Isolate* isolate,
9862 Handle<JSProxy> receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009863
9864 static Handle<Smi> GetOrCreateIdentityHash(Handle<JSProxy> proxy);
9865
Ben Murdoch097c5b22016-05-18 11:27:45 +01009866 static Maybe<bool> SetPrivateProperty(Isolate* isolate, Handle<JSProxy> proxy,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009867 Handle<Symbol> private_name,
9868 PropertyDescriptor* desc,
9869 ShouldThrow should_throw);
9870
Ben Murdoch097c5b22016-05-18 11:27:45 +01009871 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009872 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxy);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00009873};
9874
Ben Murdoch257744e2011-11-30 15:57:28 +00009875
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009876class JSCollection : public JSObject {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009877 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009878 // [table]: the backing hash table
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009879 DECL_ACCESSORS(table, Object)
9880
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009881 static const int kTableOffset = JSObject::kHeaderSize;
9882 static const int kSize = kTableOffset + kPointerSize;
9883
9884 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009885 DISALLOW_IMPLICIT_CONSTRUCTORS(JSCollection);
9886};
9887
9888
9889// The JSSet describes EcmaScript Harmony sets
9890class JSSet : public JSCollection {
9891 public:
9892 DECLARE_CAST(JSSet)
9893
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009894 static void Initialize(Handle<JSSet> set, Isolate* isolate);
9895 static void Clear(Handle<JSSet> set);
9896
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009897 // Dispatched behavior.
9898 DECLARE_PRINTER(JSSet)
9899 DECLARE_VERIFIER(JSSet)
9900
9901 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009902 DISALLOW_IMPLICIT_CONSTRUCTORS(JSSet);
9903};
9904
9905
9906// The JSMap describes EcmaScript Harmony maps
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009907class JSMap : public JSCollection {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009908 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009909 DECLARE_CAST(JSMap)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009910
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009911 static void Initialize(Handle<JSMap> map, Isolate* isolate);
9912 static void Clear(Handle<JSMap> map);
9913
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009914 // Dispatched behavior.
9915 DECLARE_PRINTER(JSMap)
9916 DECLARE_VERIFIER(JSMap)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01009917
9918 private:
9919 DISALLOW_IMPLICIT_CONSTRUCTORS(JSMap);
9920};
9921
9922
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009923// OrderedHashTableIterator is an iterator that iterates over the keys and
9924// values of an OrderedHashTable.
9925//
9926// The iterator has a reference to the underlying OrderedHashTable data,
9927// [table], as well as the current [index] the iterator is at.
9928//
9929// When the OrderedHashTable is rehashed it adds a reference from the old table
9930// to the new table as well as storing enough data about the changes so that the
9931// iterator [index] can be adjusted accordingly.
9932//
9933// When the [Next] result from the iterator is requested, the iterator checks if
9934// there is a newer table that it needs to transition to.
9935template<class Derived, class TableType>
9936class OrderedHashTableIterator: public JSObject {
9937 public:
9938 // [table]: the backing hash table mapping keys to values.
9939 DECL_ACCESSORS(table, Object)
9940
9941 // [index]: The index into the data table.
9942 DECL_ACCESSORS(index, Object)
9943
9944 // [kind]: The kind of iteration this is. One of the [Kind] enum values.
9945 DECL_ACCESSORS(kind, Object)
9946
9947#ifdef OBJECT_PRINT
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009948 void OrderedHashTableIteratorPrint(std::ostream& os); // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009949#endif
9950
9951 static const int kTableOffset = JSObject::kHeaderSize;
9952 static const int kIndexOffset = kTableOffset + kPointerSize;
9953 static const int kKindOffset = kIndexOffset + kPointerSize;
9954 static const int kSize = kKindOffset + kPointerSize;
9955
9956 enum Kind {
9957 kKindKeys = 1,
9958 kKindValues = 2,
9959 kKindEntries = 3
9960 };
9961
9962 // Whether the iterator has more elements. This needs to be called before
9963 // calling |CurrentKey| and/or |CurrentValue|.
9964 bool HasMore();
9965
9966 // Move the index forward one.
9967 void MoveNext() {
9968 set_index(Smi::FromInt(Smi::cast(index())->value() + 1));
9969 }
9970
9971 // Populates the array with the next key and value and then moves the iterator
9972 // forward.
9973 // This returns the |kind| or 0 if the iterator is already at the end.
9974 Smi* Next(JSArray* value_array);
9975
9976 // Returns the current key of the iterator. This should only be called when
9977 // |HasMore| returns true.
9978 inline Object* CurrentKey();
9979
9980 private:
9981 // Transitions the iterator to the non obsolete backing store. This is a NOP
9982 // if the [table] is not obsolete.
9983 void Transition();
9984
9985 DISALLOW_IMPLICIT_CONSTRUCTORS(OrderedHashTableIterator);
9986};
9987
9988
9989class JSSetIterator: public OrderedHashTableIterator<JSSetIterator,
9990 OrderedHashSet> {
9991 public:
9992 // Dispatched behavior.
9993 DECLARE_PRINTER(JSSetIterator)
9994 DECLARE_VERIFIER(JSSetIterator)
9995
9996 DECLARE_CAST(JSSetIterator)
9997
9998 // Called by |Next| to populate the array. This allows the subclasses to
9999 // populate the array differently.
10000 inline void PopulateValueArray(FixedArray* array);
10001
10002 private:
10003 DISALLOW_IMPLICIT_CONSTRUCTORS(JSSetIterator);
10004};
10005
10006
10007class JSMapIterator: public OrderedHashTableIterator<JSMapIterator,
10008 OrderedHashMap> {
10009 public:
10010 // Dispatched behavior.
10011 DECLARE_PRINTER(JSMapIterator)
10012 DECLARE_VERIFIER(JSMapIterator)
10013
10014 DECLARE_CAST(JSMapIterator)
10015
10016 // Called by |Next| to populate the array. This allows the subclasses to
10017 // populate the array differently.
10018 inline void PopulateValueArray(FixedArray* array);
10019
10020 private:
10021 // Returns the current value of the iterator. This should only be called when
10022 // |HasMore| returns true.
10023 inline Object* CurrentValue();
10024
10025 DISALLOW_IMPLICIT_CONSTRUCTORS(JSMapIterator);
10026};
10027
10028
10029// Base class for both JSWeakMap and JSWeakSet
10030class JSWeakCollection: public JSObject {
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010031 public:
10032 // [table]: the backing hash table mapping keys to values.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010033 DECL_ACCESSORS(table, Object)
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010034
10035 // [next]: linked list of encountered weak maps during GC.
10036 DECL_ACCESSORS(next, Object)
10037
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010038 static void Initialize(Handle<JSWeakCollection> collection, Isolate* isolate);
10039 static void Set(Handle<JSWeakCollection> collection, Handle<Object> key,
10040 Handle<Object> value, int32_t hash);
10041 static bool Delete(Handle<JSWeakCollection> collection, Handle<Object> key,
10042 int32_t hash);
10043
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010044 static const int kTableOffset = JSObject::kHeaderSize;
10045 static const int kNextOffset = kTableOffset + kPointerSize;
10046 static const int kSize = kNextOffset + kPointerSize;
10047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010048 // Visiting policy defines whether the table and next collection fields
10049 // should be visited or not.
10050 enum BodyVisitingPolicy { kVisitStrong, kVisitWeak };
10051
10052 // Iterates the function object according to the visiting policy.
10053 template <BodyVisitingPolicy>
10054 class BodyDescriptorImpl;
10055
10056 // Visit the whole object.
10057 typedef BodyDescriptorImpl<kVisitStrong> BodyDescriptor;
10058
10059 // Don't visit table and next collection fields.
10060 typedef BodyDescriptorImpl<kVisitWeak> BodyDescriptorWeak;
10061
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010062 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010063 DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakCollection);
10064};
10065
10066
10067// The JSWeakMap describes EcmaScript Harmony weak maps
10068class JSWeakMap: public JSWeakCollection {
10069 public:
10070 DECLARE_CAST(JSWeakMap)
10071
10072 // Dispatched behavior.
10073 DECLARE_PRINTER(JSWeakMap)
10074 DECLARE_VERIFIER(JSWeakMap)
10075
10076 private:
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010077 DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakMap);
10078};
10079
10080
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010081// The JSWeakSet describes EcmaScript Harmony weak sets
10082class JSWeakSet: public JSWeakCollection {
10083 public:
10084 DECLARE_CAST(JSWeakSet)
10085
10086 // Dispatched behavior.
10087 DECLARE_PRINTER(JSWeakSet)
10088 DECLARE_VERIFIER(JSWeakSet)
10089
10090 private:
10091 DISALLOW_IMPLICIT_CONSTRUCTORS(JSWeakSet);
10092};
10093
10094
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010095// Whether a JSArrayBuffer is a SharedArrayBuffer or not.
10096enum class SharedFlag { kNotShared, kShared };
10097
10098
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010099class JSArrayBuffer: public JSObject {
10100 public:
10101 // [backing_store]: backing memory for this array
10102 DECL_ACCESSORS(backing_store, void)
10103
10104 // [byte_length]: length in bytes
10105 DECL_ACCESSORS(byte_length, Object)
10106
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010107 inline uint32_t bit_field() const;
10108 inline void set_bit_field(uint32_t bits);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010109
10110 inline bool is_external();
10111 inline void set_is_external(bool value);
10112
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010113 inline bool is_neuterable();
10114 inline void set_is_neuterable(bool value);
10115
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010116 inline bool was_neutered();
10117 inline void set_was_neutered(bool value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010119 inline bool is_shared();
10120 inline void set_is_shared(bool value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010121
10122 DECLARE_CAST(JSArrayBuffer)
10123
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010124 void Neuter();
10125
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010126 static void Setup(Handle<JSArrayBuffer> array_buffer, Isolate* isolate,
10127 bool is_external, void* data, size_t allocated_length,
10128 SharedFlag shared = SharedFlag::kNotShared);
10129
10130 static bool SetupAllocatingData(Handle<JSArrayBuffer> array_buffer,
10131 Isolate* isolate, size_t allocated_length,
10132 bool initialize = true,
10133 SharedFlag shared = SharedFlag::kNotShared);
10134
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010135 // Dispatched behavior.
10136 DECLARE_PRINTER(JSArrayBuffer)
10137 DECLARE_VERIFIER(JSArrayBuffer)
10138
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010139 static const int kByteLengthOffset = JSObject::kHeaderSize;
10140 static const int kBackingStoreOffset = kByteLengthOffset + kPointerSize;
10141 static const int kBitFieldSlot = kBackingStoreOffset + kPointerSize;
10142#if V8_TARGET_LITTLE_ENDIAN || !V8_HOST_ARCH_64_BIT
10143 static const int kBitFieldOffset = kBitFieldSlot;
10144#else
10145 static const int kBitFieldOffset = kBitFieldSlot + kIntSize;
10146#endif
10147 static const int kSize = kBitFieldSlot + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010148
10149 static const int kSizeWithInternalFields =
10150 kSize + v8::ArrayBuffer::kInternalFieldCount * kPointerSize;
10151
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010152 // Iterates all fields in the object including internal ones except
10153 // kBackingStoreOffset and kBitFieldSlot.
10154 class BodyDescriptor;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010155
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010156 class IsExternal : public BitField<bool, 1, 1> {};
10157 class IsNeuterable : public BitField<bool, 2, 1> {};
10158 class WasNeutered : public BitField<bool, 3, 1> {};
10159 class IsShared : public BitField<bool, 4, 1> {};
10160
10161 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010162 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArrayBuffer);
10163};
10164
10165
10166class JSArrayBufferView: public JSObject {
10167 public:
10168 // [buffer]: ArrayBuffer that this typed array views.
10169 DECL_ACCESSORS(buffer, Object)
10170
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010171 // [byte_offset]: offset of typed array in bytes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010172 DECL_ACCESSORS(byte_offset, Object)
10173
10174 // [byte_length]: length of typed array in bytes.
10175 DECL_ACCESSORS(byte_length, Object)
10176
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010177 DECLARE_CAST(JSArrayBufferView)
10178
10179 DECLARE_VERIFIER(JSArrayBufferView)
10180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010181 inline bool WasNeutered() const;
10182
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010183 static const int kBufferOffset = JSObject::kHeaderSize;
10184 static const int kByteOffsetOffset = kBufferOffset + kPointerSize;
10185 static const int kByteLengthOffset = kByteOffsetOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010186 static const int kViewSize = kByteLengthOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010187
10188 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010189#ifdef VERIFY_HEAP
10190 DECL_ACCESSORS(raw_byte_offset, Object)
10191 DECL_ACCESSORS(raw_byte_length, Object)
10192#endif
10193
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010194 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArrayBufferView);
10195};
10196
10197
10198class JSTypedArray: public JSArrayBufferView {
10199 public:
10200 // [length]: length of typed array in elements.
10201 DECL_ACCESSORS(length, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010202 inline uint32_t length_value() const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010203
10204 DECLARE_CAST(JSTypedArray)
10205
10206 ExternalArrayType type();
10207 size_t element_size();
10208
10209 Handle<JSArrayBuffer> GetBuffer();
10210
10211 // Dispatched behavior.
10212 DECLARE_PRINTER(JSTypedArray)
10213 DECLARE_VERIFIER(JSTypedArray)
10214
10215 static const int kLengthOffset = kViewSize + kPointerSize;
10216 static const int kSize = kLengthOffset + kPointerSize;
10217
10218 static const int kSizeWithInternalFields =
10219 kSize + v8::ArrayBufferView::kInternalFieldCount * kPointerSize;
10220
10221 private:
10222 static Handle<JSArrayBuffer> MaterializeArrayBuffer(
10223 Handle<JSTypedArray> typed_array);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010224#ifdef VERIFY_HEAP
10225 DECL_ACCESSORS(raw_length, Object)
10226#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010227
10228 DISALLOW_IMPLICIT_CONSTRUCTORS(JSTypedArray);
10229};
10230
10231
10232class JSDataView: public JSArrayBufferView {
10233 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010234 DECLARE_CAST(JSDataView)
10235
10236 // Dispatched behavior.
10237 DECLARE_PRINTER(JSDataView)
10238 DECLARE_VERIFIER(JSDataView)
10239
10240 static const int kSize = kViewSize;
10241
10242 static const int kSizeWithInternalFields =
10243 kSize + v8::ArrayBufferView::kInternalFieldCount * kPointerSize;
10244
10245 private:
10246 DISALLOW_IMPLICIT_CONSTRUCTORS(JSDataView);
10247};
10248
10249
Ben Murdoch257744e2011-11-30 15:57:28 +000010250// Foreign describes objects pointing from JavaScript to C structures.
Ben Murdoch257744e2011-11-30 15:57:28 +000010251class Foreign: public HeapObject {
10252 public:
10253 // [address]: field containing the address.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010254 inline Address foreign_address();
10255 inline void set_foreign_address(Address value);
Ben Murdoch257744e2011-11-30 15:57:28 +000010256
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010257 DECLARE_CAST(Foreign)
Ben Murdoch257744e2011-11-30 15:57:28 +000010258
10259 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010260 DECLARE_PRINTER(Foreign)
10261 DECLARE_VERIFIER(Foreign)
Steve Blocka7e24c12009-10-30 11:49:00 +000010262
10263 // Layout description.
10264
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010265 static const int kForeignAddressOffset = HeapObject::kHeaderSize;
10266 static const int kSize = kForeignAddressOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010267
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010268 STATIC_ASSERT(kForeignAddressOffset == Internals::kForeignAddressOffset);
Steve Blocka7e24c12009-10-30 11:49:00 +000010269
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010270 class BodyDescriptor;
10271
Steve Blocka7e24c12009-10-30 11:49:00 +000010272 private:
Ben Murdoch257744e2011-11-30 15:57:28 +000010273 DISALLOW_IMPLICIT_CONSTRUCTORS(Foreign);
Steve Blocka7e24c12009-10-30 11:49:00 +000010274};
10275
10276
10277// The JSArray describes JavaScript Arrays
10278// Such an array can be in one of two modes:
10279// - fast, backing storage is a FixedArray and length <= elements.length();
10280// Please note: push and pop can be used to grow and shrink the array.
10281// - slow, backing storage is a HashTable with numbers as keys.
10282class JSArray: public JSObject {
10283 public:
10284 // [length]: The length property.
10285 DECL_ACCESSORS(length, Object)
10286
Leon Clarke4515c472010-02-03 11:58:03 +000010287 // Overload the length setter to skip write barrier when the length
10288 // is set to a smi. This matches the set function on FixedArray.
10289 inline void set_length(Smi* length);
10290
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010291 static bool HasReadOnlyLength(Handle<JSArray> array);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010292 static bool WouldChangeReadOnlyLength(Handle<JSArray> array, uint32_t index);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010293
Steve Blocka7e24c12009-10-30 11:49:00 +000010294 // Initialize the array with the given capacity. The function may
10295 // fail due to out-of-memory situations, but only if the requested
10296 // capacity is non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010297 static void Initialize(Handle<JSArray> array, int capacity, int length = 0);
Steve Blocka7e24c12009-10-30 11:49:00 +000010298
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010299 // If the JSArray has fast elements, and new_length would result in
10300 // normalization, returns true.
10301 bool SetLengthWouldNormalize(uint32_t new_length);
10302 static inline bool SetLengthWouldNormalize(Heap* heap, uint32_t new_length);
10303
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010304 // Initializes the array to a certain length.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010305 inline bool AllowsSetLength();
10306
10307 static void SetLength(Handle<JSArray> array, uint32_t length);
10308 // Same as above but will also queue splice records if |array| is observed.
10309 static MaybeHandle<Object> ObservableSetLength(Handle<JSArray> array,
10310 uint32_t length);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010311
Steve Blocka7e24c12009-10-30 11:49:00 +000010312 // Set the content of the array to the content of storage.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010313 static inline void SetContent(Handle<JSArray> array,
10314 Handle<FixedArrayBase> storage);
Steve Blocka7e24c12009-10-30 11:49:00 +000010315
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010316 // ES6 9.4.2.1
10317 MUST_USE_RESULT static Maybe<bool> DefineOwnProperty(
10318 Isolate* isolate, Handle<JSArray> o, Handle<Object> name,
10319 PropertyDescriptor* desc, ShouldThrow should_throw);
10320
10321 static bool AnythingToArrayLength(Isolate* isolate,
10322 Handle<Object> length_object,
10323 uint32_t* output);
10324 MUST_USE_RESULT static Maybe<bool> ArraySetLength(Isolate* isolate,
10325 Handle<JSArray> a,
10326 PropertyDescriptor* desc,
10327 ShouldThrow should_throw);
10328
Ben Murdochda12d292016-06-02 14:46:10 +010010329 // Checks whether the Array has the current realm's Array.prototype as its
10330 // prototype. This function is best-effort and only gives a conservative
10331 // approximation, erring on the side of false, in particular with respect
10332 // to Proxies and objects with a hidden prototype.
10333 inline bool HasArrayPrototype(Isolate* isolate);
10334
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010335 DECLARE_CAST(JSArray)
Steve Blocka7e24c12009-10-30 11:49:00 +000010336
Steve Blocka7e24c12009-10-30 11:49:00 +000010337 // Dispatched behavior.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010338 DECLARE_PRINTER(JSArray)
10339 DECLARE_VERIFIER(JSArray)
Steve Blocka7e24c12009-10-30 11:49:00 +000010340
10341 // Number of element slots to pre-allocate for an empty array.
10342 static const int kPreallocatedArrayElements = 4;
10343
10344 // Layout description.
10345 static const int kLengthOffset = JSObject::kHeaderSize;
10346 static const int kSize = kLengthOffset + kPointerSize;
10347
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010348 // 600 * KB is the Page::kMaxRegularHeapObjectSize defined in spaces.h which
10349 // we do not want to include in objects.h
10350 // Note that Page::kMaxRegularHeapObjectSize has to be in sync with
10351 // kInitialMaxFastElementArray which is checked in a DCHECK in heap.cc.
10352 static const int kInitialMaxFastElementArray =
10353 (600 * KB - FixedArray::kHeaderSize - kSize - AllocationMemento::kSize) /
10354 kPointerSize;
10355
Steve Blocka7e24c12009-10-30 11:49:00 +000010356 private:
Steve Blocka7e24c12009-10-30 11:49:00 +000010357 DISALLOW_IMPLICIT_CONSTRUCTORS(JSArray);
10358};
10359
10360
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010361Handle<Object> CacheInitialJSArrayMaps(Handle<Context> native_context,
10362 Handle<Map> initial_map);
10363
10364
Steve Block6ded16b2010-05-10 14:33:55 +010010365// JSRegExpResult is just a JSArray with a specific initial map.
10366// This initial map adds in-object properties for "index" and "input"
10367// properties, as assigned by RegExp.prototype.exec, which allows
10368// faster creation of RegExp exec results.
10369// This class just holds constants used when creating the result.
10370// After creation the result must be treated as a JSArray in all regards.
10371class JSRegExpResult: public JSArray {
10372 public:
10373 // Offsets of object fields.
10374 static const int kIndexOffset = JSArray::kSize;
10375 static const int kInputOffset = kIndexOffset + kPointerSize;
10376 static const int kSize = kInputOffset + kPointerSize;
10377 // Indices of in-object properties.
10378 static const int kIndexIndex = 0;
10379 static const int kInputIndex = 1;
10380 private:
10381 DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
10382};
10383
10384
Ben Murdoch097c5b22016-05-18 11:27:45 +010010385// An accessor must have a getter, but can have no setter.
10386//
10387// When setting a property, V8 searches accessors in prototypes.
10388// If an accessor was found and it does not have a setter,
10389// the request is ignored.
10390//
10391// If the accessor in the prototype has the READ_ONLY property attribute, then
10392// a new value is added to the derived object when the property is set.
10393// This shadows the accessor in the prototype.
Steve Blocka7e24c12009-10-30 11:49:00 +000010394class AccessorInfo: public Struct {
10395 public:
Steve Blocka7e24c12009-10-30 11:49:00 +000010396 DECL_ACCESSORS(name, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010397 DECL_INT_ACCESSORS(flag)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010398 DECL_ACCESSORS(expected_receiver_type, Object)
Ben Murdoch097c5b22016-05-18 11:27:45 +010010399 DECL_ACCESSORS(getter, Object)
10400 DECL_ACCESSORS(setter, Object)
10401 DECL_ACCESSORS(data, Object)
10402
10403 // Dispatched behavior.
10404 DECLARE_PRINTER(AccessorInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010405
10406 inline bool all_can_read();
10407 inline void set_all_can_read(bool value);
10408
10409 inline bool all_can_write();
10410 inline void set_all_can_write(bool value);
10411
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010412 inline bool is_special_data_property();
10413 inline void set_is_special_data_property(bool value);
10414
Ben Murdochda12d292016-06-02 14:46:10 +010010415 inline bool is_sloppy();
10416 inline void set_is_sloppy(bool value);
10417
Steve Blocka7e24c12009-10-30 11:49:00 +000010418 inline PropertyAttributes property_attributes();
10419 inline void set_property_attributes(PropertyAttributes attributes);
10420
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010421 // Checks whether the given receiver is compatible with this accessor.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010422 static bool IsCompatibleReceiverMap(Isolate* isolate,
10423 Handle<AccessorInfo> info,
10424 Handle<Map> map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010425 inline bool IsCompatibleReceiver(Object* receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +000010426
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010427 DECLARE_CAST(AccessorInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010428
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010429 // Dispatched behavior.
10430 DECLARE_VERIFIER(AccessorInfo)
10431
10432 // Append all descriptors to the array that are not already there.
10433 // Return number added.
10434 static int AppendUnique(Handle<Object> descriptors,
10435 Handle<FixedArray> array,
10436 int valid_descriptors);
10437
10438 static const int kNameOffset = HeapObject::kHeaderSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010439 static const int kFlagOffset = kNameOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010440 static const int kExpectedReceiverTypeOffset = kFlagOffset + kPointerSize;
Ben Murdoch097c5b22016-05-18 11:27:45 +010010441 static const int kGetterOffset = kExpectedReceiverTypeOffset + kPointerSize;
10442 static const int kSetterOffset = kGetterOffset + kPointerSize;
10443 static const int kDataOffset = kSetterOffset + kPointerSize;
10444 static const int kSize = kDataOffset + kPointerSize;
10445
Steve Blocka7e24c12009-10-30 11:49:00 +000010446
10447 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010448 inline bool HasExpectedReceiverType();
10449
Steve Blocka7e24c12009-10-30 11:49:00 +000010450 // Bit positions in flag.
10451 static const int kAllCanReadBit = 0;
10452 static const int kAllCanWriteBit = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010453 static const int kSpecialDataProperty = 2;
Ben Murdochda12d292016-06-02 14:46:10 +010010454 static const int kIsSloppy = 3;
10455 class AttributesField : public BitField<PropertyAttributes, 4, 3> {};
Steve Blocka7e24c12009-10-30 11:49:00 +000010456
10457 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorInfo);
10458};
10459
10460
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010461// Support for JavaScript accessors: A pair of a getter and a setter. Each
10462// accessor can either be
10463// * a pointer to a JavaScript function or proxy: a real accessor
10464// * undefined: considered an accessor by the spec, too, strangely enough
10465// * the hole: an accessor which has not been set
10466// * a pointer to a map: a transition used to ensure map sharing
10467class AccessorPair: public Struct {
10468 public:
10469 DECL_ACCESSORS(getter, Object)
10470 DECL_ACCESSORS(setter, Object)
10471
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010472 DECLARE_CAST(AccessorPair)
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010473
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010474 static Handle<AccessorPair> Copy(Handle<AccessorPair> pair);
10475
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010476 inline Object* get(AccessorComponent component);
10477 inline void set(AccessorComponent component, Object* value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010478
10479 // Note: Returns undefined instead in case of a hole.
Ben Murdoch097c5b22016-05-18 11:27:45 +010010480 static Handle<Object> GetComponent(Handle<AccessorPair> accessor_pair,
10481 AccessorComponent component);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010482
10483 // Set both components, skipping arguments which are a JavaScript null.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010484 inline void SetComponents(Object* getter, Object* setter);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010485
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010486 inline bool Equals(AccessorPair* pair);
10487 inline bool Equals(Object* getter_value, Object* setter_value);
10488
10489 inline bool ContainsAccessor();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010490
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010491 // Dispatched behavior.
10492 DECLARE_PRINTER(AccessorPair)
10493 DECLARE_VERIFIER(AccessorPair)
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010494
10495 static const int kGetterOffset = HeapObject::kHeaderSize;
10496 static const int kSetterOffset = kGetterOffset + kPointerSize;
10497 static const int kSize = kSetterOffset + kPointerSize;
10498
10499 private:
10500 // Strangely enough, in addition to functions and harmony proxies, the spec
10501 // requires us to consider undefined as a kind of accessor, too:
10502 // var obj = {};
10503 // Object.defineProperty(obj, "foo", {get: undefined});
10504 // assertTrue("foo" in obj);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010505 inline bool IsJSAccessor(Object* obj);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010506
10507 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessorPair);
10508};
10509
10510
Steve Blocka7e24c12009-10-30 11:49:00 +000010511class AccessCheckInfo: public Struct {
10512 public:
10513 DECL_ACCESSORS(named_callback, Object)
10514 DECL_ACCESSORS(indexed_callback, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010515 DECL_ACCESSORS(callback, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +000010516 DECL_ACCESSORS(data, Object)
10517
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010518 DECLARE_CAST(AccessCheckInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010519
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010520 // Dispatched behavior.
10521 DECLARE_PRINTER(AccessCheckInfo)
10522 DECLARE_VERIFIER(AccessCheckInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010523
10524 static const int kNamedCallbackOffset = HeapObject::kHeaderSize;
10525 static const int kIndexedCallbackOffset = kNamedCallbackOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010526 static const int kCallbackOffset = kIndexedCallbackOffset + kPointerSize;
10527 static const int kDataOffset = kCallbackOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010528 static const int kSize = kDataOffset + kPointerSize;
10529
10530 private:
10531 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessCheckInfo);
10532};
10533
10534
10535class InterceptorInfo: public Struct {
10536 public:
10537 DECL_ACCESSORS(getter, Object)
10538 DECL_ACCESSORS(setter, Object)
10539 DECL_ACCESSORS(query, Object)
10540 DECL_ACCESSORS(deleter, Object)
10541 DECL_ACCESSORS(enumerator, Object)
10542 DECL_ACCESSORS(data, Object)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010543 DECL_BOOLEAN_ACCESSORS(can_intercept_symbols)
10544 DECL_BOOLEAN_ACCESSORS(all_can_read)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010545 DECL_BOOLEAN_ACCESSORS(non_masking)
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010546
10547 inline int flags() const;
10548 inline void set_flags(int flags);
Steve Blocka7e24c12009-10-30 11:49:00 +000010549
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010550 DECLARE_CAST(InterceptorInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010551
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010552 // Dispatched behavior.
10553 DECLARE_PRINTER(InterceptorInfo)
10554 DECLARE_VERIFIER(InterceptorInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010555
10556 static const int kGetterOffset = HeapObject::kHeaderSize;
10557 static const int kSetterOffset = kGetterOffset + kPointerSize;
10558 static const int kQueryOffset = kSetterOffset + kPointerSize;
10559 static const int kDeleterOffset = kQueryOffset + kPointerSize;
10560 static const int kEnumeratorOffset = kDeleterOffset + kPointerSize;
10561 static const int kDataOffset = kEnumeratorOffset + kPointerSize;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010562 static const int kFlagsOffset = kDataOffset + kPointerSize;
10563 static const int kSize = kFlagsOffset + kPointerSize;
10564
10565 static const int kCanInterceptSymbolsBit = 0;
10566 static const int kAllCanReadBit = 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010567 static const int kNonMasking = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +000010568
10569 private:
10570 DISALLOW_IMPLICIT_CONSTRUCTORS(InterceptorInfo);
10571};
10572
10573
10574class CallHandlerInfo: public Struct {
10575 public:
10576 DECL_ACCESSORS(callback, Object)
10577 DECL_ACCESSORS(data, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010578 DECL_ACCESSORS(fast_handler, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +000010579
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010580 DECLARE_CAST(CallHandlerInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010581
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010582 // Dispatched behavior.
10583 DECLARE_PRINTER(CallHandlerInfo)
10584 DECLARE_VERIFIER(CallHandlerInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010585
10586 static const int kCallbackOffset = HeapObject::kHeaderSize;
10587 static const int kDataOffset = kCallbackOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010588 static const int kFastHandlerOffset = kDataOffset + kPointerSize;
10589 static const int kSize = kFastHandlerOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010590
10591 private:
10592 DISALLOW_IMPLICIT_CONSTRUCTORS(CallHandlerInfo);
10593};
10594
10595
10596class TemplateInfo: public Struct {
10597 public:
10598 DECL_ACCESSORS(tag, Object)
Ben Murdoch097c5b22016-05-18 11:27:45 +010010599 DECL_ACCESSORS(serial_number, Object)
10600 DECL_INT_ACCESSORS(number_of_properties)
Steve Blocka7e24c12009-10-30 11:49:00 +000010601 DECL_ACCESSORS(property_list, Object)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010602 DECL_ACCESSORS(property_accessors, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +000010603
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010604 DECLARE_VERIFIER(TemplateInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010605
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010606 static const int kTagOffset = HeapObject::kHeaderSize;
Ben Murdoch097c5b22016-05-18 11:27:45 +010010607 static const int kSerialNumberOffset = kTagOffset + kPointerSize;
10608 static const int kNumberOfProperties = kSerialNumberOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010609 static const int kPropertyListOffset = kNumberOfProperties + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010610 static const int kPropertyAccessorsOffset =
10611 kPropertyListOffset + kPointerSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010612 static const int kPropertyIntrinsicsOffset =
10613 kPropertyAccessorsOffset + kPointerSize;
10614 static const int kHeaderSize = kPropertyIntrinsicsOffset + kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010615
10616 private:
Steve Blocka7e24c12009-10-30 11:49:00 +000010617 DISALLOW_IMPLICIT_CONSTRUCTORS(TemplateInfo);
10618};
10619
10620
10621class FunctionTemplateInfo: public TemplateInfo {
10622 public:
Steve Blocka7e24c12009-10-30 11:49:00 +000010623 DECL_ACCESSORS(call_code, Object)
Steve Blocka7e24c12009-10-30 11:49:00 +000010624 DECL_ACCESSORS(prototype_template, Object)
10625 DECL_ACCESSORS(parent_template, Object)
10626 DECL_ACCESSORS(named_property_handler, Object)
10627 DECL_ACCESSORS(indexed_property_handler, Object)
10628 DECL_ACCESSORS(instance_template, Object)
10629 DECL_ACCESSORS(class_name, Object)
10630 DECL_ACCESSORS(signature, Object)
10631 DECL_ACCESSORS(instance_call_handler, Object)
10632 DECL_ACCESSORS(access_check_info, Object)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010633 DECL_INT_ACCESSORS(flag)
Steve Blocka7e24c12009-10-30 11:49:00 +000010634
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010635 inline int length() const;
10636 inline void set_length(int value);
10637
Steve Blocka7e24c12009-10-30 11:49:00 +000010638 // Following properties use flag bits.
10639 DECL_BOOLEAN_ACCESSORS(hidden_prototype)
10640 DECL_BOOLEAN_ACCESSORS(undetectable)
10641 // If the bit is set, object instances created by this function
10642 // requires access check.
10643 DECL_BOOLEAN_ACCESSORS(needs_access_check)
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010644 DECL_BOOLEAN_ACCESSORS(read_only_prototype)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010645 DECL_BOOLEAN_ACCESSORS(remove_prototype)
10646 DECL_BOOLEAN_ACCESSORS(do_not_cache)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010647 DECL_BOOLEAN_ACCESSORS(instantiated)
10648 DECL_BOOLEAN_ACCESSORS(accept_any_receiver)
Steve Blocka7e24c12009-10-30 11:49:00 +000010649
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010650 DECLARE_CAST(FunctionTemplateInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010651
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010652 // Dispatched behavior.
10653 DECLARE_PRINTER(FunctionTemplateInfo)
10654 DECLARE_VERIFIER(FunctionTemplateInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010655
Ben Murdoch097c5b22016-05-18 11:27:45 +010010656 static const int kCallCodeOffset = TemplateInfo::kHeaderSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010657 static const int kPrototypeTemplateOffset =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010658 kCallCodeOffset + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010659 static const int kParentTemplateOffset =
10660 kPrototypeTemplateOffset + kPointerSize;
10661 static const int kNamedPropertyHandlerOffset =
10662 kParentTemplateOffset + kPointerSize;
10663 static const int kIndexedPropertyHandlerOffset =
10664 kNamedPropertyHandlerOffset + kPointerSize;
10665 static const int kInstanceTemplateOffset =
10666 kIndexedPropertyHandlerOffset + kPointerSize;
10667 static const int kClassNameOffset = kInstanceTemplateOffset + kPointerSize;
10668 static const int kSignatureOffset = kClassNameOffset + kPointerSize;
10669 static const int kInstanceCallHandlerOffset = kSignatureOffset + kPointerSize;
10670 static const int kAccessCheckInfoOffset =
10671 kInstanceCallHandlerOffset + kPointerSize;
10672 static const int kFlagOffset = kAccessCheckInfoOffset + kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010673 static const int kLengthOffset = kFlagOffset + kPointerSize;
10674 static const int kSize = kLengthOffset + kPointerSize;
10675
10676 // Returns true if |object| is an instance of this function template.
10677 bool IsTemplateFor(Object* object);
10678 bool IsTemplateFor(Map* map);
Steve Blocka7e24c12009-10-30 11:49:00 +000010679
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010680 // Returns the holder JSObject if the function can legally be called with this
10681 // receiver. Returns Heap::null_value() if the call is illegal.
10682 Object* GetCompatibleReceiver(Isolate* isolate, Object* receiver);
10683
Steve Blocka7e24c12009-10-30 11:49:00 +000010684 private:
10685 // Bit position in the flag, from least significant bit position.
10686 static const int kHiddenPrototypeBit = 0;
10687 static const int kUndetectableBit = 1;
10688 static const int kNeedsAccessCheckBit = 2;
Ben Murdoch69a99ed2011-11-30 16:03:39 +000010689 static const int kReadOnlyPrototypeBit = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010690 static const int kRemovePrototypeBit = 4;
10691 static const int kDoNotCacheBit = 5;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010692 static const int kInstantiatedBit = 6;
10693 static const int kAcceptAnyReceiver = 7;
Steve Blocka7e24c12009-10-30 11:49:00 +000010694
10695 DISALLOW_IMPLICIT_CONSTRUCTORS(FunctionTemplateInfo);
10696};
10697
10698
10699class ObjectTemplateInfo: public TemplateInfo {
10700 public:
10701 DECL_ACCESSORS(constructor, Object)
10702 DECL_ACCESSORS(internal_field_count, Object)
10703
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010704 DECLARE_CAST(ObjectTemplateInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010705
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010706 // Dispatched behavior.
10707 DECLARE_PRINTER(ObjectTemplateInfo)
10708 DECLARE_VERIFIER(ObjectTemplateInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010709
10710 static const int kConstructorOffset = TemplateInfo::kHeaderSize;
10711 static const int kInternalFieldCountOffset =
10712 kConstructorOffset + kPointerSize;
10713 static const int kSize = kInternalFieldCountOffset + kPointerSize;
10714};
10715
10716
Steve Blocka7e24c12009-10-30 11:49:00 +000010717// The DebugInfo class holds additional information for a function being
10718// debugged.
10719class DebugInfo: public Struct {
10720 public:
10721 // The shared function info for the source being debugged.
10722 DECL_ACCESSORS(shared, SharedFunctionInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010723 // Code object for the patched code. This code object is the code object
10724 // currently active for the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +010010725 DECL_ACCESSORS(abstract_code, AbstractCode)
Steve Blocka7e24c12009-10-30 11:49:00 +000010726 // Fixed array holding status information for each active break point.
10727 DECL_ACCESSORS(break_points, FixedArray)
10728
Ben Murdoch097c5b22016-05-18 11:27:45 +010010729 // Check if there is a break point at a code offset.
10730 bool HasBreakPoint(int code_offset);
10731 // Get the break point info object for a code offset.
10732 Object* GetBreakPointInfo(int code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000010733 // Clear a break point.
Ben Murdoch097c5b22016-05-18 11:27:45 +010010734 static void ClearBreakPoint(Handle<DebugInfo> debug_info, int code_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +000010735 Handle<Object> break_point_object);
10736 // Set a break point.
Ben Murdoch097c5b22016-05-18 11:27:45 +010010737 static void SetBreakPoint(Handle<DebugInfo> debug_info, int code_offset,
Steve Blocka7e24c12009-10-30 11:49:00 +000010738 int source_position, int statement_position,
10739 Handle<Object> break_point_object);
Ben Murdoch097c5b22016-05-18 11:27:45 +010010740 // Get the break point objects for a code offset.
10741 Handle<Object> GetBreakPointObjects(int code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000010742 // Find the break point info holding this break point object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010743 static Handle<Object> FindBreakPointInfo(Handle<DebugInfo> debug_info,
10744 Handle<Object> break_point_object);
Steve Blocka7e24c12009-10-30 11:49:00 +000010745 // Get the number of break points for this function.
10746 int GetBreakPointCount();
10747
Ben Murdoch097c5b22016-05-18 11:27:45 +010010748 static Smi* uninitialized() { return Smi::FromInt(0); }
10749
10750 inline BytecodeArray* original_bytecode_array();
10751
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010752 DECLARE_CAST(DebugInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010753
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010754 // Dispatched behavior.
10755 DECLARE_PRINTER(DebugInfo)
10756 DECLARE_VERIFIER(DebugInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010757
10758 static const int kSharedFunctionInfoIndex = Struct::kHeaderSize;
Ben Murdoch097c5b22016-05-18 11:27:45 +010010759 static const int kAbstractCodeIndex = kSharedFunctionInfoIndex + kPointerSize;
10760 static const int kBreakPointsStateIndex = kAbstractCodeIndex + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010761 static const int kSize = kBreakPointsStateIndex + kPointerSize;
10762
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010763 static const int kEstimatedNofBreakPointsInFunction = 16;
10764
Steve Blocka7e24c12009-10-30 11:49:00 +000010765 private:
10766 static const int kNoBreakPointInfo = -1;
10767
Ben Murdoch097c5b22016-05-18 11:27:45 +010010768 // Lookup the index in the break_points array for a code offset.
10769 int GetBreakPointInfoIndex(int code_offset);
Steve Blocka7e24c12009-10-30 11:49:00 +000010770
10771 DISALLOW_IMPLICIT_CONSTRUCTORS(DebugInfo);
10772};
10773
10774
10775// The BreakPointInfo class holds information for break points set in a
10776// function. The DebugInfo object holds a BreakPointInfo object for each code
10777// position with one or more break points.
10778class BreakPointInfo: public Struct {
10779 public:
Ben Murdoch097c5b22016-05-18 11:27:45 +010010780 // The code offset for the break point.
10781 DECL_INT_ACCESSORS(code_offset)
Steve Blocka7e24c12009-10-30 11:49:00 +000010782 // The position in the source for the break position.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010783 DECL_INT_ACCESSORS(source_position)
Steve Blocka7e24c12009-10-30 11:49:00 +000010784 // The position in the source for the last statement before this break
10785 // position.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010786 DECL_INT_ACCESSORS(statement_position)
Steve Blocka7e24c12009-10-30 11:49:00 +000010787 // List of related JavaScript break points.
10788 DECL_ACCESSORS(break_point_objects, Object)
10789
10790 // Removes a break point.
10791 static void ClearBreakPoint(Handle<BreakPointInfo> info,
10792 Handle<Object> break_point_object);
10793 // Set a break point.
10794 static void SetBreakPoint(Handle<BreakPointInfo> info,
10795 Handle<Object> break_point_object);
10796 // Check if break point info has this break point object.
10797 static bool HasBreakPointObject(Handle<BreakPointInfo> info,
10798 Handle<Object> break_point_object);
Ben Murdoch097c5b22016-05-18 11:27:45 +010010799 // Get the number of break points for this code offset.
Steve Blocka7e24c12009-10-30 11:49:00 +000010800 int GetBreakPointCount();
10801
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010802 DECLARE_CAST(BreakPointInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010803
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010804 // Dispatched behavior.
10805 DECLARE_PRINTER(BreakPointInfo)
10806 DECLARE_VERIFIER(BreakPointInfo)
Steve Blocka7e24c12009-10-30 11:49:00 +000010807
Ben Murdoch097c5b22016-05-18 11:27:45 +010010808 static const int kCodeOffsetIndex = Struct::kHeaderSize;
10809 static const int kSourcePositionIndex = kCodeOffsetIndex + kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +000010810 static const int kStatementPositionIndex =
10811 kSourcePositionIndex + kPointerSize;
10812 static const int kBreakPointObjectsIndex =
10813 kStatementPositionIndex + kPointerSize;
10814 static const int kSize = kBreakPointObjectsIndex + kPointerSize;
10815
10816 private:
10817 DISALLOW_IMPLICIT_CONSTRUCTORS(BreakPointInfo);
10818};
Steve Blocka7e24c12009-10-30 11:49:00 +000010819
10820
10821#undef DECL_BOOLEAN_ACCESSORS
10822#undef DECL_ACCESSORS
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010823#undef DECLARE_CAST
10824#undef DECLARE_VERIFIER
Steve Blocka7e24c12009-10-30 11:49:00 +000010825
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010826#define VISITOR_SYNCHRONIZATION_TAGS_LIST(V) \
10827 V(kStringTable, "string_table", "(Internalized strings)") \
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010828 V(kExternalStringsTable, "external_strings_table", "(External strings)") \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010829 V(kStrongRootList, "strong_root_list", "(Strong roots)") \
10830 V(kSmiRootList, "smi_root_list", "(Smi roots)") \
10831 V(kBootstrapper, "bootstrapper", "(Bootstrapper)") \
10832 V(kTop, "top", "(Isolate)") \
10833 V(kRelocatable, "relocatable", "(Relocatable)") \
10834 V(kDebug, "debug", "(Debugger)") \
10835 V(kCompilationCache, "compilationcache", "(Compilation cache)") \
10836 V(kHandleScope, "handlescope", "(Handle scope)") \
Ben Murdoch097c5b22016-05-18 11:27:45 +010010837 V(kDispatchTable, "dispatchtable", "(Dispatch table)") \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010838 V(kBuiltins, "builtins", "(Builtins)") \
10839 V(kGlobalHandles, "globalhandles", "(Global handles)") \
10840 V(kEternalHandles, "eternalhandles", "(Eternal handles)") \
10841 V(kThreadManager, "threadmanager", "(Thread manager)") \
10842 V(kStrongRoots, "strong roots", "(Strong roots)") \
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010843 V(kExtensions, "Extensions", "(Extensions)")
10844
10845class VisitorSynchronization : public AllStatic {
10846 public:
10847#define DECLARE_ENUM(enum_item, ignore1, ignore2) enum_item,
10848 enum SyncTag {
10849 VISITOR_SYNCHRONIZATION_TAGS_LIST(DECLARE_ENUM)
10850 kNumberOfSyncTags
10851 };
10852#undef DECLARE_ENUM
10853
10854 static const char* const kTags[kNumberOfSyncTags];
10855 static const char* const kTagNames[kNumberOfSyncTags];
10856};
Steve Blocka7e24c12009-10-30 11:49:00 +000010857
10858// Abstract base class for visiting, and optionally modifying, the
10859// pointers contained in Objects. Used in GC and serialization/deserialization.
10860class ObjectVisitor BASE_EMBEDDED {
10861 public:
10862 virtual ~ObjectVisitor() {}
10863
10864 // Visits a contiguous arrays of pointers in the half-open range
10865 // [start, end). Any or all of the values may be modified on return.
10866 virtual void VisitPointers(Object** start, Object** end) = 0;
10867
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010868 // Handy shorthand for visiting a single pointer.
10869 virtual void VisitPointer(Object** p) { VisitPointers(p, p + 1); }
10870
10871 // Visit weak next_code_link in Code object.
10872 virtual void VisitNextCodeLink(Object** p) { VisitPointers(p, p + 1); }
10873
Steve Blocka7e24c12009-10-30 11:49:00 +000010874 // To allow lazy clearing of inline caches the visitor has
10875 // a rich interface for iterating over Code objects..
10876
10877 // Visits a code target in the instruction stream.
10878 virtual void VisitCodeTarget(RelocInfo* rinfo);
10879
Steve Block791712a2010-08-27 10:21:07 +010010880 // Visits a code entry in a JS function.
10881 virtual void VisitCodeEntry(Address entry_address);
10882
Ben Murdochb0fe1622011-05-05 13:52:32 +010010883 // Visits a global property cell reference in the instruction stream.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010884 virtual void VisitCell(RelocInfo* rinfo);
Ben Murdochb0fe1622011-05-05 13:52:32 +010010885
Steve Blocka7e24c12009-10-30 11:49:00 +000010886 // Visits a runtime entry in the instruction stream.
10887 virtual void VisitRuntimeEntry(RelocInfo* rinfo) {}
10888
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010889 // Visits the resource of an one-byte or two-byte string.
10890 virtual void VisitExternalOneByteString(
10891 v8::String::ExternalOneByteStringResource** resource) {}
Steve Blockd0582a62009-12-15 09:54:21 +000010892 virtual void VisitExternalTwoByteString(
10893 v8::String::ExternalStringResource** resource) {}
10894
Steve Blocka7e24c12009-10-30 11:49:00 +000010895 // Visits a debug call target in the instruction stream.
10896 virtual void VisitDebugTarget(RelocInfo* rinfo);
10897
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010898 // Visits the byte sequence in a function's prologue that contains information
10899 // about the code's age.
10900 virtual void VisitCodeAgeSequence(RelocInfo* rinfo);
Steve Blocka7e24c12009-10-30 11:49:00 +000010901
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010902 // Visit pointer embedded into a code object.
10903 virtual void VisitEmbeddedPointer(RelocInfo* rinfo);
10904
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010905 // Visits an external reference embedded into a code object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010906 virtual void VisitExternalReference(RelocInfo* rinfo);
10907
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010908 // Visits an external reference.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010909 virtual void VisitExternalReference(Address* p) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000010910
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010911 // Visits an (encoded) internal reference.
10912 virtual void VisitInternalReference(RelocInfo* rinfo) {}
10913
Steve Block44f0eee2011-05-26 01:26:41 +010010914 // Visits a handle that has an embedder-assigned class ID.
10915 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {}
10916
Steve Blocka7e24c12009-10-30 11:49:00 +000010917 // Intended for serialization/deserialization checking: insert, or
10918 // check for the presence of, a tag at this position in the stream.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010010919 // Also used for marking up GC roots in heap snapshots.
10920 virtual void Synchronize(VisitorSynchronization::SyncTag tag) {}
Steve Blocka7e24c12009-10-30 11:49:00 +000010921};
10922
10923
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010924// BooleanBit is a helper class for setting and getting a bit in an integer.
Steve Blocka7e24c12009-10-30 11:49:00 +000010925class BooleanBit : public AllStatic {
10926 public:
Steve Blocka7e24c12009-10-30 11:49:00 +000010927 static inline bool get(int value, int bit_position) {
10928 return (value & (1 << bit_position)) != 0;
10929 }
10930
Steve Blocka7e24c12009-10-30 11:49:00 +000010931 static inline int set(int value, int bit_position, bool v) {
10932 if (v) {
10933 value |= (1 << bit_position);
10934 } else {
10935 value &= ~(1 << bit_position);
10936 }
10937 return value;
10938 }
10939};
10940
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010941
10942} // NOLINT, false-positive due to second-order macros.
10943} // NOLINT, false-positive due to second-order macros.
Steve Blocka7e24c12009-10-30 11:49:00 +000010944
10945#endif // V8_OBJECTS_H_