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-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
15namespace v8 {
16namespace internal {
17
18OBJECT_CONSTRUCTORS_IMPL(JSArray, JSObject)
19OBJECT_CONSTRUCTORS_IMPL(JSArrayIterator, JSObject)
20
21CAST_ACCESSOR(JSArray)
22CAST_ACCESSOR(JSArrayIterator)
23
24ACCESSORS(JSArray, length, Object, kLengthOffset)
25
26void 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
31bool JSArray::SetLengthWouldNormalize(Heap* heap, uint32_t new_length) {
32 return new_length > kMaxFastArrayLength;
33}
34
35bool JSArray::AllowsSetLength() {
36 bool result = elements()->IsFixedArray() || elements()->IsFixedDoubleArray();
37 DCHECK(result == !HasFixedTypedArrayElements());
38 return result;
39}
40
41void 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
57bool JSArray::HasArrayPrototype(Isolate* isolate) {
58 return map()->prototype() == *isolate->initial_array_prototype();
59}
60
61ACCESSORS(JSArrayIterator, iterated_object, Object, kIteratedObjectOffset)
62ACCESSORS(JSArrayIterator, next_index, Object, kNextIndexOffset)
63
64IterationKind JSArrayIterator::kind() const {
65 return static_cast<IterationKind>(
66 Smi::cast(READ_FIELD(*this, kKindOffset))->value());
67}
68
69void JSArrayIterator::set_kind(IterationKind kind) {
70 WRITE_FIELD(*this, kKindOffset, Smi::FromInt(static_cast<int>(kind)));
71}
72
73} // namespace internal
74} // namespace v8
75
76#include "src/objects/object-macros-undef.h"
77
78#endif // V8_OBJECTS_JS_ARRAY_INL_H_
79