Line data Source code
1 : // Copyright 2012 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 : //
5 : // Review notes:
6 : //
7 : // - The use of macros in these inline functions may seem superfluous
8 : // but it is absolutely needed to make sure gcc generates optimal
9 : // code. gcc is not happy when attempting to inline too deep.
10 : //
11 :
12 : #ifndef V8_OBJECTS_INL_H_
13 : #define V8_OBJECTS_INL_H_
14 :
15 : #include "src/objects.h"
16 :
17 : #include "src/base/bits.h"
18 : #include "src/base/tsan.h"
19 : #include "src/builtins/builtins.h"
20 : #include "src/conversions.h"
21 : #include "src/double.h"
22 : #include "src/handles-inl.h"
23 : #include "src/heap/factory.h"
24 : #include "src/heap/heap-write-barrier-inl.h"
25 : #include "src/keys.h"
26 : #include "src/lookup-inl.h" // TODO(jkummerow): Drop.
27 : #include "src/objects/bigint.h"
28 : #include "src/objects/heap-number-inl.h"
29 : #include "src/objects/heap-object.h"
30 : #include "src/objects/js-proxy-inl.h" // TODO(jkummerow): Drop.
31 : #include "src/objects/literal-objects.h"
32 : #include "src/objects/oddball.h"
33 : #include "src/objects/regexp-match-info.h"
34 : #include "src/objects/scope-info.h"
35 : #include "src/objects/shared-function-info.h"
36 : #include "src/objects/slots-inl.h"
37 : #include "src/objects/smi-inl.h"
38 : #include "src/objects/templates.h"
39 : #include "src/property-details.h"
40 : #include "src/property.h"
41 : #include "src/v8memory.h"
42 :
43 : // Has to be the last include (doesn't have include guards):
44 : #include "src/objects/object-macros.h"
45 :
46 : namespace v8 {
47 : namespace internal {
48 :
49 4835515317 : PropertyDetails::PropertyDetails(Smi smi) { value_ = smi->value(); }
50 :
51 : Smi PropertyDetails::AsSmi() const {
52 : // Ensure the upper 2 bits have the same value by sign extending it. This is
53 : // necessary to be able to use the 31st bit of the property details.
54 1073099964 : int value = value_ << 1;
55 1073099964 : return Smi::FromInt(value >> 1);
56 : }
57 :
58 :
59 : int PropertyDetails::field_width_in_words() const {
60 : DCHECK_EQ(location(), kField);
61 : if (!FLAG_unbox_double_fields) return 1;
62 : if (kDoubleSize == kTaggedSize) return 1;
63 : return representation().IsDouble() ? kDoubleSize / kTaggedSize : 1;
64 : }
65 :
66 : bool HeapObject::IsSloppyArgumentsElements() const {
67 : return IsFixedArrayExact();
68 : }
69 :
70 : bool HeapObject::IsJSSloppyArgumentsObject() const {
71 : return IsJSArgumentsObject();
72 : }
73 :
74 : bool HeapObject::IsJSGeneratorObject() const {
75 12672778 : return map()->instance_type() == JS_GENERATOR_OBJECT_TYPE ||
76 21082051 : IsJSAsyncFunctionObject() || IsJSAsyncGeneratorObject();
77 : }
78 :
79 : bool HeapObject::IsDataHandler() const {
80 : return IsLoadHandler() || IsStoreHandler();
81 : }
82 :
83 : bool HeapObject::IsClassBoilerplate() const { return IsFixedArrayExact(); }
84 :
85 : #define IS_TYPE_FUNCTION_DEF(type_) \
86 : bool Object::Is##type_() const { \
87 : return IsHeapObject() && HeapObject::cast(*this)->Is##type_(); \
88 : }
89 10623658453 : HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DEF)
90 : #undef IS_TYPE_FUNCTION_DEF
91 :
92 : #define IS_TYPE_FUNCTION_DEF(Type, Value) \
93 : bool Object::Is##Type(Isolate* isolate) const { \
94 : return Is##Type(ReadOnlyRoots(isolate->heap())); \
95 : } \
96 : bool Object::Is##Type(ReadOnlyRoots roots) const { \
97 : return *this == roots.Value(); \
98 : } \
99 : bool Object::Is##Type() const { \
100 : return IsHeapObject() && HeapObject::cast(*this)->Is##Type(); \
101 : } \
102 : bool HeapObject::Is##Type(Isolate* isolate) const { \
103 : return Object::Is##Type(isolate); \
104 : } \
105 : bool HeapObject::Is##Type(ReadOnlyRoots roots) const { \
106 : return Object::Is##Type(roots); \
107 : } \
108 : bool HeapObject::Is##Type() const { return Is##Type(GetReadOnlyRoots()); }
109 101289345 : ODDBALL_LIST(IS_TYPE_FUNCTION_DEF)
110 : #undef IS_TYPE_FUNCTION_DEF
111 :
112 : bool Object::IsNullOrUndefined(Isolate* isolate) const {
113 : return IsNullOrUndefined(ReadOnlyRoots(isolate));
114 : }
115 :
116 : bool Object::IsNullOrUndefined(ReadOnlyRoots roots) const {
117 267015226 : return IsNull(roots) || IsUndefined(roots);
118 : }
119 :
120 : bool Object::IsNullOrUndefined() const {
121 : return IsHeapObject() && HeapObject::cast(*this)->IsNullOrUndefined();
122 : }
123 :
124 : bool HeapObject::IsNullOrUndefined(Isolate* isolate) const {
125 : return Object::IsNullOrUndefined(isolate);
126 : }
127 :
128 : bool HeapObject::IsNullOrUndefined(ReadOnlyRoots roots) const {
129 : return Object::IsNullOrUndefined(roots);
130 : }
131 :
132 : bool HeapObject::IsNullOrUndefined() const {
133 : return IsNullOrUndefined(GetReadOnlyRoots());
134 : }
135 :
136 : bool HeapObject::IsUniqueName() const {
137 9298975 : return IsInternalizedString() || IsSymbol();
138 : }
139 :
140 : bool HeapObject::IsFunction() const {
141 : STATIC_ASSERT(LAST_FUNCTION_TYPE == LAST_TYPE);
142 3706 : return map()->instance_type() >= FIRST_FUNCTION_TYPE;
143 : }
144 :
145 7868228 : bool HeapObject::IsCallable() const { return map()->is_callable(); }
146 :
147 12 : bool HeapObject::IsConstructor() const { return map()->is_constructor(); }
148 :
149 : bool HeapObject::IsModuleInfo() const {
150 : return map() == GetReadOnlyRoots().module_info_map();
151 : }
152 :
153 : bool HeapObject::IsTemplateInfo() const {
154 1087281 : return IsObjectTemplateInfo() || IsFunctionTemplateInfo();
155 : }
156 :
157 : bool HeapObject::IsConsString() const {
158 99839659 : if (!IsString()) return false;
159 13318428 : return StringShape(String::cast(*this)).IsCons();
160 : }
161 :
162 : bool HeapObject::IsThinString() const {
163 173382188 : if (!IsString()) return false;
164 234826 : return StringShape(String::cast(*this)).IsThin();
165 : }
166 :
167 : bool HeapObject::IsSlicedString() const {
168 5978765 : if (!IsString()) return false;
169 223148 : return StringShape(String::cast(*this)).IsSliced();
170 : }
171 :
172 : bool HeapObject::IsSeqString() const {
173 7621256 : if (!IsString()) return false;
174 0 : return StringShape(String::cast(*this)).IsSequential();
175 : }
176 :
177 : bool HeapObject::IsSeqOneByteString() const {
178 72749165 : if (!IsString()) return false;
179 130535295 : return StringShape(String::cast(*this)).IsSequential() &&
180 111505 : String::cast(*this)->IsOneByteRepresentation();
181 : }
182 :
183 : bool HeapObject::IsSeqTwoByteString() const {
184 1985300 : if (!IsString()) return false;
185 229519 : return StringShape(String::cast(*this)).IsSequential() &&
186 70621 : String::cast(*this)->IsTwoByteRepresentation();
187 : }
188 :
189 : bool HeapObject::IsExternalString() const {
190 373298214 : if (!IsString()) return false;
191 0 : return StringShape(String::cast(*this)).IsExternal();
192 : }
193 :
194 : bool HeapObject::IsExternalOneByteString() const {
195 7735104 : if (!IsString()) return false;
196 7746660 : return StringShape(String::cast(*this)).IsExternal() &&
197 3868 : String::cast(*this)->IsOneByteRepresentation();
198 : }
199 :
200 : bool HeapObject::IsExternalTwoByteString() const {
201 7727472 : if (!IsString()) return false;
202 7727548 : return StringShape(String::cast(*this)).IsExternal() &&
203 : String::cast(*this)->IsTwoByteRepresentation();
204 : }
205 :
206 200085373 : bool Object::IsNumber() const { return IsSmi() || IsHeapNumber(); }
207 :
208 1566 : bool Object::IsNumeric() const { return IsNumber() || IsBigInt(); }
209 :
210 : bool HeapObject::IsFiller() const {
211 0 : InstanceType instance_type = map()->instance_type();
212 374678852 : return instance_type == FREE_SPACE_TYPE || instance_type == FILLER_TYPE;
213 : }
214 :
215 : bool HeapObject::IsJSWeakCollection() const {
216 180344 : return IsJSWeakMap() || IsJSWeakSet();
217 : }
218 :
219 0 : bool HeapObject::IsJSCollection() const { return IsJSMap() || IsJSSet(); }
220 :
221 : bool HeapObject::IsPromiseReactionJobTask() const {
222 2497641 : return IsPromiseFulfillReactionJobTask() || IsPromiseRejectReactionJobTask();
223 : }
224 :
225 : bool HeapObject::IsFrameArray() const { return IsFixedArrayExact(); }
226 :
227 : bool HeapObject::IsArrayList() const {
228 1366 : return map() == GetReadOnlyRoots().array_list_map() ||
229 : *this == GetReadOnlyRoots().empty_fixed_array();
230 : }
231 :
232 : bool HeapObject::IsRegExpMatchInfo() const { return IsFixedArrayExact(); }
233 :
234 0 : bool Object::IsLayoutDescriptor() const { return IsSmi() || IsByteArray(); }
235 :
236 : bool HeapObject::IsDeoptimizationData() const {
237 : // Must be a fixed array.
238 : if (!IsFixedArrayExact()) return false;
239 :
240 : // There's no sure way to detect the difference between a fixed array and
241 : // a deoptimization data array. Since this is used for asserts we can
242 : // check that the length is zero or else the fixed size plus a multiple of
243 : // the entry size.
244 : int length = FixedArray::cast(*this)->length();
245 : if (length == 0) return true;
246 :
247 : length -= DeoptimizationData::kFirstDeoptEntryIndex;
248 : return length >= 0 && length % DeoptimizationData::kDeoptEntrySize == 0;
249 : }
250 :
251 : bool HeapObject::IsHandlerTable() const {
252 : if (!IsFixedArrayExact()) return false;
253 : // There's actually no way to see the difference between a fixed array and
254 : // a handler table array.
255 : return true;
256 : }
257 :
258 : bool HeapObject::IsTemplateList() const {
259 : if (!IsFixedArrayExact()) return false;
260 : // There's actually no way to see the difference between a fixed array and
261 : // a template list.
262 : if (FixedArray::cast(*this)->length() < 1) return false;
263 : return true;
264 : }
265 :
266 : bool HeapObject::IsDependentCode() const {
267 : if (!IsWeakFixedArray()) return false;
268 : // There's actually no way to see the difference between a weak fixed array
269 : // and a dependent codes array.
270 : return true;
271 : }
272 :
273 : bool HeapObject::IsAbstractCode() const {
274 6695463 : return IsBytecodeArray() || IsCode();
275 : }
276 :
277 : bool HeapObject::IsStringWrapper() const {
278 1586276 : return IsJSValue() && JSValue::cast(*this)->value()->IsString();
279 : }
280 :
281 : bool HeapObject::IsBooleanWrapper() const {
282 21278 : return IsJSValue() && JSValue::cast(*this)->value()->IsBoolean();
283 : }
284 :
285 : bool HeapObject::IsScriptWrapper() const {
286 : return IsJSValue() && JSValue::cast(*this)->value()->IsScript();
287 : }
288 :
289 : bool HeapObject::IsNumberWrapper() const {
290 21200 : return IsJSValue() && JSValue::cast(*this)->value()->IsNumber();
291 : }
292 :
293 : bool HeapObject::IsBigIntWrapper() const {
294 21169 : return IsJSValue() && JSValue::cast(*this)->value()->IsBigInt();
295 : }
296 :
297 : bool HeapObject::IsSymbolWrapper() const {
298 21154 : return IsJSValue() && JSValue::cast(*this)->value()->IsSymbol();
299 : }
300 :
301 : bool HeapObject::IsJSArrayBufferView() const {
302 282027 : return IsJSDataView() || IsJSTypedArray();
303 : }
304 :
305 : bool HeapObject::IsStringSet() const { return IsHashTable(); }
306 :
307 : bool HeapObject::IsObjectHashSet() const { return IsHashTable(); }
308 :
309 : bool HeapObject::IsCompilationCacheTable() const { return IsHashTable(); }
310 :
311 : bool HeapObject::IsMapCache() const { return IsHashTable(); }
312 :
313 : bool HeapObject::IsObjectHashTable() const { return IsHashTable(); }
314 :
315 : bool Object::IsHashTableBase() const { return IsHashTable(); }
316 :
317 : bool Object::IsSmallOrderedHashTable() const {
318 : return IsSmallOrderedHashSet() || IsSmallOrderedHashMap() ||
319 : IsSmallOrderedNameDictionary();
320 : }
321 :
322 : bool Object::IsPrimitive() const {
323 3993769 : return IsSmi() || HeapObject::cast(*this)->map()->IsPrimitiveMap();
324 : }
325 :
326 : // static
327 : Maybe<bool> Object::IsArray(Handle<Object> object) {
328 1181823 : if (object->IsSmi()) return Just(false);
329 : Handle<HeapObject> heap_object = Handle<HeapObject>::cast(object);
330 1181805 : if (heap_object->IsJSArray()) return Just(true);
331 1152299 : if (!heap_object->IsJSProxy()) return Just(false);
332 1238 : return JSProxy::IsArray(Handle<JSProxy>::cast(object));
333 : }
334 :
335 178987420 : bool HeapObject::IsUndetectable() const { return map()->is_undetectable(); }
336 :
337 : bool HeapObject::IsAccessCheckNeeded() const {
338 34423607 : if (IsJSGlobalProxy()) {
339 74833 : const JSGlobalProxy proxy = JSGlobalProxy::cast(*this);
340 74833 : JSGlobalObject global = proxy->GetIsolate()->context()->global_object();
341 74833 : return proxy->IsDetachedFrom(global);
342 : }
343 284844 : return map()->is_access_check_needed();
344 : }
345 :
346 : bool HeapObject::IsStruct() const {
347 : switch (map()->instance_type()) {
348 : #define MAKE_STRUCT_CASE(TYPE, Name, name) \
349 : case TYPE: \
350 : return true;
351 : STRUCT_LIST(MAKE_STRUCT_CASE)
352 : #undef MAKE_STRUCT_CASE
353 : // It is hard to include ALLOCATION_SITE_TYPE in STRUCT_LIST because
354 : // that macro is used for many things and AllocationSite needs a few
355 : // special cases.
356 : case ALLOCATION_SITE_TYPE:
357 : return true;
358 : case LOAD_HANDLER_TYPE:
359 : case STORE_HANDLER_TYPE:
360 : return true;
361 : case FEEDBACK_CELL_TYPE:
362 : return true;
363 : case CALL_HANDLER_INFO_TYPE:
364 : return true;
365 : default:
366 : return false;
367 : }
368 : }
369 :
370 : #define MAKE_STRUCT_PREDICATE(NAME, Name, name) \
371 : bool Object::Is##Name() const { \
372 : return IsHeapObject() && HeapObject::cast(*this)->Is##Name(); \
373 : } \
374 : TYPE_CHECKER(Name)
375 733346963 : STRUCT_LIST(MAKE_STRUCT_PREDICATE)
376 : #undef MAKE_STRUCT_PREDICATE
377 :
378 : double Object::Number() const {
379 : DCHECK(IsNumber());
380 : return IsSmi() ? static_cast<double>(Smi(this->ptr())->value())
381 349522930 : : HeapNumber::unchecked_cast(*this)->value();
382 : }
383 :
384 : // static
385 39020 : bool Object::SameNumberValue(double value1, double value2) {
386 : // SameNumberValue(NaN, NaN) is true.
387 39020 : if (value1 != value2) {
388 36402 : return std::isnan(value1) && std::isnan(value2);
389 : }
390 : // SameNumberValue(0.0, -0.0) is false.
391 2618 : return (std::signbit(value1) == std::signbit(value2));
392 : }
393 :
394 : bool Object::IsNaN() const {
395 4561 : return this->IsHeapNumber() && std::isnan(HeapNumber::cast(*this)->value());
396 : }
397 :
398 : bool Object::IsMinusZero() const {
399 6860 : return this->IsHeapNumber() &&
400 : i::IsMinusZero(HeapNumber::cast(*this)->value());
401 : }
402 :
403 : OBJECT_CONSTRUCTORS_IMPL(RegExpMatchInfo, FixedArray)
404 : OBJECT_CONSTRUCTORS_IMPL(ScopeInfo, FixedArray)
405 : OBJECT_CONSTRUCTORS_IMPL(BigIntBase, HeapObject)
406 : OBJECT_CONSTRUCTORS_IMPL(BigInt, BigIntBase)
407 : OBJECT_CONSTRUCTORS_IMPL(FreshlyAllocatedBigInt, BigIntBase)
408 :
409 : // ------------------------------------
410 : // Cast operations
411 :
412 : CAST_ACCESSOR(BigInt)
413 : CAST_ACCESSOR(RegExpMatchInfo)
414 : CAST_ACCESSOR(ScopeInfo)
415 :
416 : bool Object::HasValidElements() {
417 : // Dictionary is covered under FixedArray.
418 : return IsFixedArray() || IsFixedDoubleArray() || IsFixedTypedArrayBase();
419 : }
420 :
421 15346609 : bool Object::FilterKey(PropertyFilter filter) {
422 : DCHECK(!IsPropertyCell());
423 15346609 : if (filter == PRIVATE_NAMES_ONLY) {
424 2299410 : if (!IsSymbol()) return true;
425 18502 : return !Symbol::cast(*this)->is_private_name();
426 13047199 : } else if (IsSymbol()) {
427 101250 : if (filter & SKIP_SYMBOLS) return true;
428 :
429 46373 : if (Symbol::cast(*this)->is_private()) return true;
430 : } else {
431 12945949 : if (filter & SKIP_STRINGS) return true;
432 : }
433 : return false;
434 : }
435 :
436 18012685 : Representation Object::OptimalRepresentation() {
437 18012685 : if (!FLAG_track_fields) return Representation::Tagged();
438 18012685 : if (IsSmi()) {
439 : return Representation::Smi();
440 26107651 : } else if (FLAG_track_double_fields && IsHeapNumber()) {
441 : return Representation::Double();
442 26074402 : } else if (FLAG_track_computed_fields && IsUninitialized()) {
443 : return Representation::None();
444 12820458 : } else if (FLAG_track_heap_object_fields) {
445 : DCHECK(IsHeapObject());
446 : return Representation::HeapObject();
447 : } else {
448 : return Representation::Tagged();
449 : }
450 : }
451 :
452 :
453 11757989 : ElementsKind Object::OptimalElementsKind() {
454 11757989 : if (IsSmi()) return PACKED_SMI_ELEMENTS;
455 1550100 : if (IsNumber()) return PACKED_DOUBLE_ELEMENTS;
456 399577 : return PACKED_ELEMENTS;
457 : }
458 :
459 :
460 40442709 : bool Object::FitsRepresentation(Representation representation) {
461 40442709 : if (FLAG_track_fields && representation.IsSmi()) {
462 4805418 : return IsSmi();
463 35637291 : } else if (FLAG_track_double_fields && representation.IsDouble()) {
464 70366 : return IsMutableHeapNumber() || IsNumber();
465 35602108 : } else if (FLAG_track_heap_object_fields && representation.IsHeapObject()) {
466 30848080 : return IsHeapObject();
467 4754028 : } else if (FLAG_track_fields && representation.IsNone()) {
468 : return false;
469 : }
470 4569976 : return true;
471 : }
472 :
473 60308116 : bool Object::ToUint32(uint32_t* value) const {
474 60308116 : if (IsSmi()) {
475 : int num = Smi::ToInt(*this);
476 44737722 : if (num < 0) return false;
477 44718040 : *value = static_cast<uint32_t>(num);
478 44718040 : return true;
479 : }
480 15570396 : if (IsHeapNumber()) {
481 : double num = HeapNumber::cast(*this)->value();
482 5793 : return DoubleToUint32IfEqualToSelf(num, value);
483 : }
484 : return false;
485 : }
486 :
487 : // static
488 16785283 : MaybeHandle<JSReceiver> Object::ToObject(Isolate* isolate,
489 : Handle<Object> object,
490 : const char* method_name) {
491 16785283 : if (object->IsJSReceiver()) return Handle<JSReceiver>::cast(object);
492 6831 : return ToObjectImpl(isolate, object, method_name);
493 : }
494 :
495 :
496 : // static
497 6483216 : MaybeHandle<Name> Object::ToName(Isolate* isolate, Handle<Object> input) {
498 6483216 : if (input->IsName()) return Handle<Name>::cast(input);
499 86618 : return ConvertToName(isolate, input);
500 : }
501 :
502 : // static
503 263106 : MaybeHandle<Object> Object::ToPropertyKey(Isolate* isolate,
504 : Handle<Object> value) {
505 484714 : if (value->IsSmi() || HeapObject::cast(*value)->IsName()) return value;
506 540 : return ConvertToPropertyKey(isolate, value);
507 : }
508 :
509 : // static
510 144297 : MaybeHandle<Object> Object::ToPrimitive(Handle<Object> input,
511 : ToPrimitiveHint hint) {
512 144297 : if (input->IsPrimitive()) return input;
513 6663 : return JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(input), hint);
514 : }
515 :
516 : // static
517 7467612 : MaybeHandle<Object> Object::ToNumber(Isolate* isolate, Handle<Object> input) {
518 7467612 : if (input->IsNumber()) return input; // Shortcut.
519 2677177 : return ConvertToNumberOrNumeric(isolate, input, Conversion::kToNumber);
520 : }
521 :
522 : // static
523 8069 : MaybeHandle<Object> Object::ToNumeric(Isolate* isolate, Handle<Object> input) {
524 11235 : if (input->IsNumber() || input->IsBigInt()) return input; // Shortcut.
525 1375 : return ConvertToNumberOrNumeric(isolate, input, Conversion::kToNumeric);
526 : }
527 :
528 : // static
529 : MaybeHandle<Object> Object::ToInteger(Isolate* isolate, Handle<Object> input) {
530 4178745 : if (input->IsSmi()) return input;
531 105749 : return ConvertToInteger(isolate, input);
532 : }
533 :
534 : // static
535 : MaybeHandle<Object> Object::ToInt32(Isolate* isolate, Handle<Object> input) {
536 2377 : if (input->IsSmi()) return input;
537 1352 : return ConvertToInt32(isolate, input);
538 : }
539 :
540 : // static
541 420108 : MaybeHandle<Object> Object::ToUint32(Isolate* isolate, Handle<Object> input) {
542 420144 : if (input->IsSmi()) return handle(Smi::cast(*input)->ToUint32Smi(), isolate);
543 420072 : return ConvertToUint32(isolate, input);
544 : }
545 :
546 : // static
547 11969522 : MaybeHandle<String> Object::ToString(Isolate* isolate, Handle<Object> input) {
548 11969522 : if (input->IsString()) return Handle<String>::cast(input);
549 4590103 : return ConvertToString(isolate, input);
550 : }
551 :
552 : // static
553 63486 : MaybeHandle<Object> Object::ToLength(Isolate* isolate, Handle<Object> input) {
554 63486 : if (input->IsSmi()) {
555 183654 : int value = std::max(Smi::ToInt(*input), 0);
556 61218 : return handle(Smi::FromInt(value), isolate);
557 : }
558 2268 : return ConvertToLength(isolate, input);
559 : }
560 :
561 : // static
562 10951 : MaybeHandle<Object> Object::ToIndex(Isolate* isolate, Handle<Object> input,
563 : MessageTemplate error_index) {
564 20651 : if (input->IsSmi() && Smi::ToInt(*input) >= 0) return input;
565 1309 : return ConvertToIndex(isolate, input, error_index);
566 : }
567 :
568 2398902 : MaybeHandle<Object> Object::GetProperty(Isolate* isolate, Handle<Object> object,
569 : Handle<Name> name) {
570 2398902 : LookupIterator it(isolate, object, name);
571 2494174 : if (!it.IsFound()) return it.factory()->undefined_value();
572 2303630 : return GetProperty(&it);
573 : }
574 :
575 1184278 : MaybeHandle<Object> Object::GetElement(Isolate* isolate, Handle<Object> object,
576 : uint32_t index) {
577 1184278 : LookupIterator it(isolate, object, index);
578 2363980 : if (!it.IsFound()) return it.factory()->undefined_value();
579 4576 : return GetProperty(&it);
580 : }
581 :
582 22693 : MaybeHandle<Object> Object::SetElement(Isolate* isolate, Handle<Object> object,
583 : uint32_t index, Handle<Object> value,
584 : ShouldThrow should_throw) {
585 22693 : LookupIterator it(isolate, object, index);
586 45386 : MAYBE_RETURN_NULL(
587 : SetProperty(&it, value, StoreOrigin::kMaybeKeyed, Just(should_throw)));
588 22457 : return value;
589 : }
590 :
591 22887763 : ObjectSlot HeapObject::RawField(int byte_offset) const {
592 4597149749 : return ObjectSlot(FIELD_ADDR(*this, byte_offset));
593 : }
594 :
595 : MaybeObjectSlot HeapObject::RawMaybeWeakField(int byte_offset) const {
596 2937569588 : return MaybeObjectSlot(FIELD_ADDR(*this, byte_offset));
597 : }
598 :
599 : MapWord MapWord::FromMap(const Map map) { return MapWord(map.ptr()); }
600 :
601 : Map MapWord::ToMap() const { return Map::unchecked_cast(Object(value_)); }
602 :
603 668940911 : bool MapWord::IsForwardingAddress() const { return HAS_SMI_TAG(value_); }
604 :
605 : MapWord MapWord::FromForwardingAddress(HeapObject object) {
606 27013221 : return MapWord(object->ptr() - kHeapObjectTag);
607 : }
608 :
609 67169 : HeapObject MapWord::ToForwardingAddress() {
610 : DCHECK(IsForwardingAddress());
611 134338 : return HeapObject::FromAddress(value_);
612 : }
613 :
614 : #ifdef VERIFY_HEAP
615 : void HeapObject::VerifyObjectField(Isolate* isolate, int offset) {
616 : VerifyPointer(isolate, READ_FIELD(*this, offset));
617 : STATIC_ASSERT(!COMPRESS_POINTERS_BOOL || kTaggedSize == kInt32Size);
618 : }
619 :
620 : void HeapObject::VerifyMaybeObjectField(Isolate* isolate, int offset) {
621 : MaybeObject::VerifyMaybeObjectPointer(isolate,
622 : READ_WEAK_FIELD(*this, offset));
623 : STATIC_ASSERT(!COMPRESS_POINTERS_BOOL || kTaggedSize == kInt32Size);
624 : }
625 :
626 : void HeapObject::VerifySmiField(int offset) {
627 : CHECK(READ_FIELD(*this, offset)->IsSmi());
628 : STATIC_ASSERT(!COMPRESS_POINTERS_BOOL || kTaggedSize == kInt32Size);
629 : }
630 :
631 : #endif
632 :
633 : ReadOnlyRoots HeapObject::GetReadOnlyRoots() const {
634 : // TODO(v8:7464): When RO_SPACE is embedded, this will access a global
635 : // variable instead.
636 : return ReadOnlyRoots(GetHeapFromWritableObject(*this));
637 : }
638 :
639 5321913478 : Map HeapObject::map() const { return map_word().ToMap(); }
640 :
641 6219171 : void HeapObject::set_map(Map value) {
642 : if (!value.is_null()) {
643 : #ifdef VERIFY_HEAP
644 : GetHeapFromWritableObject(*this)->VerifyObjectLayoutChange(*this, value);
645 : #endif
646 : }
647 : set_map_word(MapWord::FromMap(value));
648 6219171 : if (!value.is_null()) {
649 : // TODO(1600) We are passing kNullAddress as a slot because maps can never
650 : // be on an evacuation candidate.
651 6219171 : MarkingBarrier(*this, ObjectSlot(kNullAddress), value);
652 : }
653 6219171 : }
654 :
655 : Map HeapObject::synchronized_map() const {
656 : return synchronized_map_word().ToMap();
657 : }
658 :
659 60037620 : void HeapObject::synchronized_set_map(Map value) {
660 : if (!value.is_null()) {
661 : #ifdef VERIFY_HEAP
662 : GetHeapFromWritableObject(*this)->VerifyObjectLayoutChange(*this, value);
663 : #endif
664 : }
665 : synchronized_set_map_word(MapWord::FromMap(value));
666 60037620 : if (!value.is_null()) {
667 : // TODO(1600) We are passing kNullAddress as a slot because maps can never
668 : // be on an evacuation candidate.
669 60037636 : MarkingBarrier(*this, ObjectSlot(kNullAddress), value);
670 : }
671 60037618 : }
672 :
673 :
674 : // Unsafe accessor omitting write barrier.
675 : void HeapObject::set_map_no_write_barrier(Map value) {
676 : if (!value.is_null()) {
677 : #ifdef VERIFY_HEAP
678 : GetHeapFromWritableObject(*this)->VerifyObjectLayoutChange(*this, value);
679 : #endif
680 : }
681 : set_map_word(MapWord::FromMap(value));
682 : }
683 :
684 71117360 : void HeapObject::set_map_after_allocation(Map value, WriteBarrierMode mode) {
685 : set_map_word(MapWord::FromMap(value));
686 71117360 : if (mode != SKIP_WRITE_BARRIER) {
687 : DCHECK(!value.is_null());
688 : // TODO(1600) We are passing kNullAddress as a slot because maps can never
689 : // be on an evacuation candidate.
690 27081900 : MarkingBarrier(*this, ObjectSlot(kNullAddress), value);
691 : }
692 71117368 : }
693 :
694 25133687 : MapWordSlot HeapObject::map_slot() const {
695 24384891184 : return MapWordSlot(FIELD_ADDR(*this, kMapOffset));
696 : }
697 :
698 68414 : MapWord HeapObject::map_word() const {
699 68414 : return MapWord(map_slot().Relaxed_Load().ptr());
700 : }
701 :
702 25140855 : void HeapObject::set_map_word(MapWord map_word) {
703 25140855 : map_slot().Relaxed_Store(Object(map_word.value_));
704 25140855 : }
705 :
706 :
707 67169 : MapWord HeapObject::synchronized_map_word() const {
708 67169 : return MapWord(map_slot().Acquire_Load().ptr());
709 : }
710 :
711 : void HeapObject::synchronized_set_map_word(MapWord map_word) {
712 : map_slot().Release_Store(Object(map_word.value_));
713 : }
714 :
715 1562070826 : int HeapObject::Size() const { return SizeFromMap(map()); }
716 :
717 : inline bool IsSpecialReceiverInstanceType(InstanceType instance_type) {
718 : return instance_type <= LAST_SPECIAL_RECEIVER_TYPE;
719 : }
720 :
721 : // This should be in objects/map-inl.h, but can't, because of a cyclic
722 : // dependency.
723 : bool Map::IsSpecialReceiverMap() const {
724 : bool result = IsSpecialReceiverInstanceType(instance_type());
725 : DCHECK_IMPLIES(!result,
726 : !has_named_interceptor() && !is_access_check_needed());
727 : return result;
728 : }
729 :
730 : inline bool IsCustomElementsReceiverInstanceType(InstanceType instance_type) {
731 128121 : return instance_type <= LAST_CUSTOM_ELEMENTS_RECEIVER;
732 : }
733 :
734 : // This should be in objects/map-inl.h, but can't, because of a cyclic
735 : // dependency.
736 128121 : bool Map::IsCustomElementsReceiverMap() const {
737 128121 : return IsCustomElementsReceiverInstanceType(instance_type());
738 : }
739 :
740 6413 : bool Object::ToArrayLength(uint32_t* index) const {
741 5468400 : return Object::ToUint32(index);
742 : }
743 :
744 : bool Object::ToArrayIndex(uint32_t* index) const {
745 54801835 : return Object::ToUint32(index) && *index != kMaxUInt32;
746 : }
747 :
748 3947804 : bool Object::GetHeapObjectIfStrong(HeapObject* result) const {
749 3947804 : return GetHeapObject(result);
750 : }
751 :
752 606515 : bool Object::GetHeapObject(HeapObject* result) const {
753 7307884874 : if (!IsHeapObject()) return false;
754 1950760492 : *result = HeapObject::cast(*this);
755 463935 : return true;
756 : }
757 :
758 0 : HeapObject Object::GetHeapObject() const {
759 : DCHECK(IsHeapObject());
760 0 : return HeapObject::cast(*this);
761 : }
762 :
763 : int RegExpMatchInfo::NumberOfCaptureRegisters() {
764 : DCHECK_GE(length(), kLastMatchOverhead);
765 : Object obj = get(kNumberOfCapturesIndex);
766 : return Smi::ToInt(obj);
767 : }
768 :
769 : void RegExpMatchInfo::SetNumberOfCaptureRegisters(int value) {
770 : DCHECK_GE(length(), kLastMatchOverhead);
771 : set(kNumberOfCapturesIndex, Smi::FromInt(value));
772 : }
773 :
774 : String RegExpMatchInfo::LastSubject() {
775 : DCHECK_GE(length(), kLastMatchOverhead);
776 : return String::cast(get(kLastSubjectIndex));
777 : }
778 :
779 : void RegExpMatchInfo::SetLastSubject(String value) {
780 : DCHECK_GE(length(), kLastMatchOverhead);
781 4256360 : set(kLastSubjectIndex, value);
782 : }
783 :
784 : Object RegExpMatchInfo::LastInput() {
785 : DCHECK_GE(length(), kLastMatchOverhead);
786 : return get(kLastInputIndex);
787 : }
788 :
789 : void RegExpMatchInfo::SetLastInput(Object value) {
790 : DCHECK_GE(length(), kLastMatchOverhead);
791 4256405 : set(kLastInputIndex, value);
792 : }
793 :
794 : int RegExpMatchInfo::Capture(int i) {
795 : DCHECK_LT(i, NumberOfCaptureRegisters());
796 499320 : Object obj = get(kFirstCaptureIndex + i);
797 : return Smi::ToInt(obj);
798 : }
799 :
800 : void RegExpMatchInfo::SetCapture(int i, int value) {
801 : DCHECK_LT(i, NumberOfCaptureRegisters());
802 : set(kFirstCaptureIndex + i, Smi::FromInt(value));
803 : }
804 :
805 : WriteBarrierMode HeapObject::GetWriteBarrierMode(
806 : const DisallowHeapAllocation& promise) {
807 : return GetWriteBarrierModeForObject(*this, &promise);
808 : }
809 :
810 : // static
811 : AllocationAlignment HeapObject::RequiredAlignment(Map map) {
812 : #ifdef V8_COMPRESS_POINTERS
813 : // TODO(ishell, v8:8875): Consider using aligned allocations once the
814 : // allocation alignment inconsistency is fixed. For now we keep using
815 : // unaligned access since both x64 and arm64 architectures (where pointer
816 : // compression is supported) allow unaligned access to doubles and full words.
817 : #endif // V8_COMPRESS_POINTERS
818 : #ifdef V8_HOST_ARCH_32_BIT
819 : int instance_type = map->instance_type();
820 : if (instance_type == FIXED_FLOAT64_ARRAY_TYPE ||
821 : instance_type == FIXED_DOUBLE_ARRAY_TYPE) {
822 : return kDoubleAligned;
823 : }
824 : if (instance_type == HEAP_NUMBER_TYPE) return kDoubleUnaligned;
825 : #endif // V8_HOST_ARCH_32_BIT
826 : return kWordAligned;
827 : }
828 :
829 : Address HeapObject::GetFieldAddress(int field_offset) const {
830 57555 : return FIELD_ADDR(*this, field_offset);
831 : }
832 :
833 : // static
834 239 : Maybe<bool> Object::GreaterThan(Isolate* isolate, Handle<Object> x,
835 : Handle<Object> y) {
836 239 : Maybe<ComparisonResult> result = Compare(isolate, x, y);
837 239 : if (result.IsJust()) {
838 239 : switch (result.FromJust()) {
839 : case ComparisonResult::kGreaterThan:
840 : return Just(true);
841 : case ComparisonResult::kLessThan:
842 : case ComparisonResult::kEqual:
843 : case ComparisonResult::kUndefined:
844 : return Just(false);
845 : }
846 : }
847 : return Nothing<bool>();
848 : }
849 :
850 :
851 : // static
852 221 : Maybe<bool> Object::GreaterThanOrEqual(Isolate* isolate, Handle<Object> x,
853 : Handle<Object> y) {
854 221 : Maybe<ComparisonResult> result = Compare(isolate, x, y);
855 221 : if (result.IsJust()) {
856 221 : switch (result.FromJust()) {
857 : case ComparisonResult::kEqual:
858 : case ComparisonResult::kGreaterThan:
859 : return Just(true);
860 : case ComparisonResult::kLessThan:
861 : case ComparisonResult::kUndefined:
862 : return Just(false);
863 : }
864 : }
865 : return Nothing<bool>();
866 : }
867 :
868 :
869 : // static
870 257 : Maybe<bool> Object::LessThan(Isolate* isolate, Handle<Object> x,
871 : Handle<Object> y) {
872 257 : Maybe<ComparisonResult> result = Compare(isolate, x, y);
873 257 : if (result.IsJust()) {
874 239 : switch (result.FromJust()) {
875 : case ComparisonResult::kLessThan:
876 : return Just(true);
877 : case ComparisonResult::kEqual:
878 : case ComparisonResult::kGreaterThan:
879 : case ComparisonResult::kUndefined:
880 : return Just(false);
881 : }
882 : }
883 : return Nothing<bool>();
884 : }
885 :
886 :
887 : // static
888 212 : Maybe<bool> Object::LessThanOrEqual(Isolate* isolate, Handle<Object> x,
889 : Handle<Object> y) {
890 212 : Maybe<ComparisonResult> result = Compare(isolate, x, y);
891 212 : if (result.IsJust()) {
892 212 : switch (result.FromJust()) {
893 : case ComparisonResult::kEqual:
894 : case ComparisonResult::kLessThan:
895 : return Just(true);
896 : case ComparisonResult::kGreaterThan:
897 : case ComparisonResult::kUndefined:
898 : return Just(false);
899 : }
900 : }
901 : return Nothing<bool>();
902 : }
903 :
904 933855 : MaybeHandle<Object> Object::GetPropertyOrElement(Isolate* isolate,
905 : Handle<Object> object,
906 : Handle<Name> name) {
907 933855 : LookupIterator it = LookupIterator::PropertyOrElement(isolate, object, name);
908 933862 : return GetProperty(&it);
909 : }
910 :
911 1059 : MaybeHandle<Object> Object::SetPropertyOrElement(
912 : Isolate* isolate, Handle<Object> object, Handle<Name> name,
913 : Handle<Object> value, Maybe<ShouldThrow> should_throw,
914 : StoreOrigin store_origin) {
915 1059 : LookupIterator it = LookupIterator::PropertyOrElement(isolate, object, name);
916 2118 : MAYBE_RETURN_NULL(SetProperty(&it, value, store_origin, should_throw));
917 987 : return value;
918 : }
919 :
920 16696 : MaybeHandle<Object> Object::GetPropertyOrElement(Handle<Object> receiver,
921 : Handle<Name> name,
922 : Handle<JSReceiver> holder) {
923 : LookupIterator it = LookupIterator::PropertyOrElement(holder->GetIsolate(),
924 16696 : receiver, name, holder);
925 16696 : return GetProperty(&it);
926 : }
927 :
928 : // static
929 34973803 : Object Object::GetSimpleHash(Object object) {
930 : DisallowHeapAllocation no_gc;
931 34973803 : if (object->IsSmi()) {
932 17540167 : uint32_t hash = ComputeUnseededHash(Smi::ToInt(object));
933 35080334 : return Smi::FromInt(hash & Smi::kMaxValue);
934 : }
935 17433636 : if (object->IsHeapNumber()) {
936 : double num = HeapNumber::cast(object)->value();
937 4382 : if (std::isnan(num)) return Smi::FromInt(Smi::kMaxValue);
938 : // Use ComputeUnseededHash for all values in Signed32 range, including -0,
939 : // which is considered equal to 0 because collections use SameValueZero.
940 : uint32_t hash;
941 : // Check range before conversion to avoid undefined behavior.
942 6856 : if (num >= kMinInt && num <= kMaxInt && FastI2D(FastD2I(num)) == num) {
943 521 : hash = ComputeUnseededHash(FastD2I(num));
944 : } else {
945 : hash = ComputeLongHash(double_to_uint64(num));
946 : }
947 7630 : return Smi::FromInt(hash & Smi::kMaxValue);
948 : }
949 17429254 : if (object->IsName()) {
950 17285862 : uint32_t hash = Name::cast(object)->Hash();
951 34571722 : return Smi::FromInt(hash);
952 : }
953 143392 : if (object->IsOddball()) {
954 2187 : uint32_t hash = Oddball::cast(object)->to_string()->Hash();
955 4374 : return Smi::FromInt(hash);
956 : }
957 141205 : if (object->IsBigInt()) {
958 288 : uint32_t hash = BigInt::cast(object)->Hash();
959 576 : return Smi::FromInt(hash & Smi::kMaxValue);
960 : }
961 140917 : if (object->IsSharedFunctionInfo()) {
962 82381 : uint32_t hash = SharedFunctionInfo::cast(object)->Hash();
963 164762 : return Smi::FromInt(hash & Smi::kMaxValue);
964 : }
965 : DCHECK(object->IsJSReceiver());
966 58536 : return object;
967 : }
968 :
969 24717176 : Object Object::GetHash() {
970 : DisallowHeapAllocation no_gc;
971 24717176 : Object hash = GetSimpleHash(*this);
972 24717174 : if (hash->IsSmi()) return hash;
973 :
974 : DCHECK(IsJSReceiver());
975 38014 : JSReceiver receiver = JSReceiver::cast(*this);
976 38014 : return receiver->GetIdentityHash();
977 : }
978 :
979 : Handle<Object> ObjectHashTableShape::AsHandle(Handle<Object> key) {
980 : return key;
981 : }
982 :
983 7058812 : Relocatable::Relocatable(Isolate* isolate) {
984 7058812 : isolate_ = isolate;
985 7058812 : prev_ = isolate->relocatable_top();
986 : isolate->set_relocatable_top(this);
987 : }
988 :
989 :
990 14117644 : Relocatable::~Relocatable() {
991 : DCHECK_EQ(isolate_->relocatable_top(), this);
992 7058822 : isolate_->set_relocatable_top(prev_);
993 0 : }
994 :
995 : // Predictably converts HeapObject or Address to uint32 by calculating
996 : // offset of the address in respective MemoryChunk.
997 : static inline uint32_t ObjectAddressForHashing(Address object) {
998 13071470 : uint32_t value = static_cast<uint32_t>(object);
999 13071470 : return value & kPageAlignmentMask;
1000 : }
1001 :
1002 810 : static inline Handle<Object> MakeEntryPair(Isolate* isolate, uint32_t index,
1003 : Handle<Object> value) {
1004 810 : Handle<Object> key = isolate->factory()->Uint32ToString(index);
1005 : Handle<FixedArray> entry_storage =
1006 810 : isolate->factory()->NewUninitializedFixedArray(2);
1007 : {
1008 : entry_storage->set(0, *key, SKIP_WRITE_BARRIER);
1009 : entry_storage->set(1, *value, SKIP_WRITE_BARRIER);
1010 : }
1011 : return isolate->factory()->NewJSArrayWithElements(entry_storage,
1012 810 : PACKED_ELEMENTS, 2);
1013 : }
1014 :
1015 816 : static inline Handle<Object> MakeEntryPair(Isolate* isolate, Handle<Object> key,
1016 : Handle<Object> value) {
1017 : Handle<FixedArray> entry_storage =
1018 816 : isolate->factory()->NewUninitializedFixedArray(2);
1019 : {
1020 : entry_storage->set(0, *key, SKIP_WRITE_BARRIER);
1021 : entry_storage->set(1, *value, SKIP_WRITE_BARRIER);
1022 : }
1023 : return isolate->factory()->NewJSArrayWithElements(entry_storage,
1024 816 : PACKED_ELEMENTS, 2);
1025 : }
1026 :
1027 : bool ScopeInfo::IsAsmModule() const {
1028 856287 : return IsAsmModuleField::decode(Flags());
1029 : }
1030 :
1031 : bool ScopeInfo::HasSimpleParameters() const {
1032 64271 : return HasSimpleParametersField::decode(Flags());
1033 : }
1034 :
1035 : #define FIELD_ACCESSORS(name) \
1036 : void ScopeInfo::Set##name(int value) { set(k##name, Smi::FromInt(value)); } \
1037 : int ScopeInfo::name() const { \
1038 : if (length() > 0) { \
1039 : return Smi::ToInt(get(k##name)); \
1040 : } else { \
1041 : return 0; \
1042 : } \
1043 : }
1044 429276285 : FOR_EACH_SCOPE_INFO_NUMERIC_FIELD(FIELD_ACCESSORS)
1045 : #undef FIELD_ACCESSORS
1046 :
1047 : FreshlyAllocatedBigInt FreshlyAllocatedBigInt::cast(Object object) {
1048 : SLOW_DCHECK(object->IsBigInt());
1049 : return FreshlyAllocatedBigInt(object->ptr());
1050 : }
1051 :
1052 : } // namespace internal
1053 : } // namespace v8
1054 :
1055 : #include "src/objects/object-macros-undef.h"
1056 :
1057 : #endif // V8_OBJECTS_INL_H_
|