1 // Copyright 2018 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_BUILTINS_GROWABLE_FIXED_ARRAY_GEN_H_
6 #define V8_BUILTINS_GROWABLE_FIXED_ARRAY_GEN_H_
7 
8 #include "src/code-stub-assembler.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 template <class T>
14 using TNode = compiler::TNode<T>;
15 
16 // Utility class implementing a growable fixed array through CSA.
17 class GrowableFixedArray : public CodeStubAssembler {
18  public:
GrowableFixedArray(compiler::CodeAssemblerState * state)19   explicit GrowableFixedArray(compiler::CodeAssemblerState* state)
20       : CodeStubAssembler(state),
21         var_array_(this),
22         var_length_(this),
23         var_capacity_(this) {
24     var_array_ = EmptyFixedArrayConstant();
25     var_capacity_ = IntPtrConstant(0);
26     var_length_ = IntPtrConstant(0);
27   }
28 
length()29   TNode<IntPtrT> length() const { return var_length_.value(); }
30 
var_array()31   TVariable<FixedArray>* var_array() { return &var_array_; }
var_length()32   TVariable<IntPtrT>* var_length() { return &var_length_; }
var_capacity()33   TVariable<IntPtrT>* var_capacity() { return &var_capacity_; }
34 
35   void Push(TNode<Object> const value);
36 
37   TNode<JSArray> ToJSArray(TNode<Context> const context);
38 
39  private:
40   TNode<IntPtrT> NewCapacity(TNode<IntPtrT> current_capacity);
41 
42   // Creates a new array with {new_capacity} and copies the first
43   // {element_count} elements from the current array.
44   TNode<FixedArray> ResizeFixedArray(TNode<IntPtrT> const element_count,
45                                      TNode<IntPtrT> const new_capacity);
46 
47  private:
48   TVariable<FixedArray> var_array_;
49   TVariable<IntPtrT> var_length_;
50   TVariable<IntPtrT> var_capacity_;
51 };
52 
53 }  // namespace internal
54 }  // namespace v8
55 
56 #endif  // V8_BUILTINS_GROWABLE_FIXED_ARRAY_GEN_H_
57