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 | |
15 | namespace v8 { |
16 | namespace internal { |
17 | |
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 | |
31 | bool JSArray::SetLengthWouldNormalize(Heap* heap, uint32_t new_length) { |
32 | return new_length > kMaxFastArrayLength; |
33 | } |
34 | |
35 | bool JSArray::AllowsSetLength() { |
36 | bool result = elements()->IsFixedArray() || elements()->IsFixedDoubleArray(); |
37 | DCHECK(result == !HasFixedTypedArrayElements()); |
38 | return result; |
39 | } |
40 | |
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 | |
57 | bool JSArray::HasArrayPrototype(Isolate* isolate) { |
58 | return map()->prototype() == *isolate->initial_array_prototype(); |
59 | } |
60 | |
61 | ACCESSORS(JSArrayIterator, iterated_object, Object, kIteratedObjectOffset) |
62 | ACCESSORS(JSArrayIterator, next_index, Object, kNextIndexOffset) |
63 | |
64 | IterationKind JSArrayIterator::kind() const { |
65 | return static_cast<IterationKind>( |
66 | Smi::cast(READ_FIELD(*this, kKindOffset))->value()); |
67 | } |
68 | |
69 | void 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 | |