• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CAST_ACCESSOR(JSArray)
CAST_ACCESSOR(JSArrayIterator)19 CAST_ACCESSOR(JSArrayIterator)
20 
21 ACCESSORS(JSArray, length, Object, kLengthOffset)
22 
23 void JSArray::set_length(Smi* length) {
24   // Don't need a write barrier for a Smi.
25   set_length(static_cast<Object*>(length), SKIP_WRITE_BARRIER);
26 }
27 
SetLengthWouldNormalize(Heap * heap,uint32_t new_length)28 bool JSArray::SetLengthWouldNormalize(Heap* heap, uint32_t new_length) {
29   return new_length > kMaxFastArrayLength;
30 }
31 
AllowsSetLength()32 bool JSArray::AllowsSetLength() {
33   bool result = elements()->IsFixedArray() || elements()->IsFixedDoubleArray();
34   DCHECK(result == !HasFixedTypedArrayElements());
35   return result;
36 }
37 
SetContent(Handle<JSArray> array,Handle<FixedArrayBase> storage)38 void JSArray::SetContent(Handle<JSArray> array,
39                          Handle<FixedArrayBase> storage) {
40   EnsureCanContainElements(array, storage, storage->length(),
41                            ALLOW_COPIED_DOUBLE_ELEMENTS);
42 
43   DCHECK(
44       (storage->map() == array->GetReadOnlyRoots().fixed_double_array_map() &&
45        IsDoubleElementsKind(array->GetElementsKind())) ||
46       ((storage->map() != array->GetReadOnlyRoots().fixed_double_array_map()) &&
47        (IsObjectElementsKind(array->GetElementsKind()) ||
48         (IsSmiElementsKind(array->GetElementsKind()) &&
49          Handle<FixedArray>::cast(storage)->ContainsOnlySmisOrHoles()))));
50   array->set_elements(*storage);
51   array->set_length(Smi::FromInt(storage->length()));
52 }
53 
HasArrayPrototype(Isolate * isolate)54 bool JSArray::HasArrayPrototype(Isolate* isolate) {
55   return map()->prototype() == *isolate->initial_array_prototype();
56 }
57 
ACCESSORS(JSArrayIterator,iterated_object,Object,kIteratedObjectOffset)58 ACCESSORS(JSArrayIterator, iterated_object, Object, kIteratedObjectOffset)
59 ACCESSORS(JSArrayIterator, next_index, Object, kNextIndexOffset)
60 
61 IterationKind JSArrayIterator::kind() const {
62   return static_cast<IterationKind>(
63       Smi::cast(READ_FIELD(this, kKindOffset))->value());
64 }
65 
set_kind(IterationKind kind)66 void JSArrayIterator::set_kind(IterationKind kind) {
67   WRITE_FIELD(this, kKindOffset, Smi::FromInt(static_cast<int>(kind)));
68 }
69 
70 }  // namespace internal
71 }  // namespace v8
72 
73 #include "src/objects/object-macros-undef.h"
74 
75 #endif  // V8_OBJECTS_JS_ARRAY_INL_H_
76