1 // Copyright 2017 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 #ifndef V8_OBJECTS_JS_ARRAY_INL_H_
6 #define V8_OBJECTS_JS_ARRAY_INL_H_
7
8 #include "src/objects/js-array.h"
9
10 #include "src/objects/objects-inl.h" // Needed for write barriers
11
12 // Has to be the last include (doesn't have include guards):
13 #include "src/objects/object-macros.h"
14
15 namespace v8 {
16 namespace internal {
17
OBJECT_CONSTRUCTORS_IMPL(JSArray,JSObject)18 OBJECT_CONSTRUCTORS_IMPL(JSArray, JSObject)
19 OBJECT_CONSTRUCTORS_IMPL(JSArrayIterator, JSObject)
20
21 CAST_ACCESSOR(JSArray)
22 CAST_ACCESSOR(JSArrayIterator)
23
24 ACCESSORS(JSArray, length, Object, kLengthOffset)
25
26 void JSArray::set_length(Smi length) {
27 // Don't need a write barrier for a Smi.
28 set_length(Object(length.ptr()), SKIP_WRITE_BARRIER);
29 }
30
SetLengthWouldNormalize(Heap * heap,uint32_t new_length)31 bool JSArray::SetLengthWouldNormalize(Heap* heap, uint32_t new_length) {
32 return new_length > kMaxFastArrayLength;
33 }
34
AllowsSetLength()35 bool JSArray::AllowsSetLength() {
36 bool result = elements().IsFixedArray() || elements().IsFixedDoubleArray();
37 DCHECK(result == !HasTypedArrayElements());
38 return result;
39 }
40
SetContent(Handle<JSArray> array,Handle<FixedArrayBase> storage)41 void JSArray::SetContent(Handle<JSArray> array,
42 Handle<FixedArrayBase> storage) {
43 EnsureCanContainElements(array, storage, storage->length(),
44 ALLOW_COPIED_DOUBLE_ELEMENTS);
45
46 DCHECK(
47 (storage->map() == array->GetReadOnlyRoots().fixed_double_array_map() &&
48 IsDoubleElementsKind(array->GetElementsKind())) ||
49 ((storage->map() != array->GetReadOnlyRoots().fixed_double_array_map()) &&
50 (IsObjectElementsKind(array->GetElementsKind()) ||
51 (IsSmiElementsKind(array->GetElementsKind()) &&
52 Handle<FixedArray>::cast(storage)->ContainsOnlySmisOrHoles()))));
53 array->set_elements(*storage);
54 array->set_length(Smi::FromInt(storage->length()));
55 }
56
HasArrayPrototype(Isolate * isolate)57 bool JSArray::HasArrayPrototype(Isolate* isolate) {
58 return map().prototype() == *isolate->initial_array_prototype();
59 }
60
ACCESSORS(JSArrayIterator,iterated_object,Object,kIteratedObjectOffset)61 ACCESSORS(JSArrayIterator, iterated_object, Object, kIteratedObjectOffset)
62 ACCESSORS(JSArrayIterator, next_index, Object, kNextIndexOffset)
63
64 SMI_ACCESSORS(JSArrayIterator, raw_kind, kKindOffset)
65
66 IterationKind JSArrayIterator::kind() const {
67 return static_cast<IterationKind>(raw_kind());
68 }
69
set_kind(IterationKind kind)70 void JSArrayIterator::set_kind(IterationKind kind) {
71 set_raw_kind(static_cast<int>(kind));
72 }
73
74 } // namespace internal
75 } // namespace v8
76
77 #include "src/objects/object-macros-undef.h"
78
79 #endif // V8_OBJECTS_JS_ARRAY_INL_H_
80