// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_OBJECTS_REGEXP_MATCH_INFO_H_
#define V8_OBJECTS_REGEXP_MATCH_INFO_H_

#include "src/base/compiler-specific.h"
#include "src/objects/fixed-array.h"
#include "src/objects/objects.h"

// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

namespace v8 {
namespace internal {

class Object;
class String;

// The property RegExpMatchInfo includes the matchIndices array of the last
// successful regexp match (an array of start/end index pairs for the match and
// all the captured substrings), the invariant is that there are at least two
// capture indices.  The array also contains the subject string for the last
// successful match.
V8_OBJECT class RegExpMatchInfo : public TaggedArrayBase<RegExpMatchInfo, Smi> {
  using Super = TaggedArrayBase<RegExpMatchInfo, Smi>;

 public:
  static constexpr RootIndex kMapRootIndex = RootIndex::kRegExpMatchInfoMap;

  V8_EXPORT_PRIVATE static DirectHandle<RegExpMatchInfo> New(
      Isolate* isolate, int capture_count,
      AllocationType allocation = AllocationType::kYoung);

  static DirectHandle<RegExpMatchInfo> ReserveCaptures(
      Isolate* isolate, DirectHandle<RegExpMatchInfo> match_info,
      int capture_count);

  // Returns the number of captures, which is defined as the length of the
  // matchIndices objects of the last match. matchIndices contains two indices
  // for each capture (including the match itself), i.e. 2 * #captures + 2.
  inline int number_of_capture_registers() const;
  inline void set_number_of_capture_registers(int value);

  // Returns the subject string of the last match.
  inline Tagged<String> last_subject() const;
  inline void set_last_subject(Tagged<String> value,
                               WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

  // Like |last_subject|, but modifiable by the user.
  inline Tagged<Object> last_input() const;
  inline void set_last_input(Tagged<Object> value,
                             WriteBarrierMode mode = UPDATE_WRITE_BARRIER);

  inline int capture(int index) const;
  inline void set_capture(int index, int value);

  static constexpr int capture_start_index(int capture_index) {
    return capture_index * 2;
  }
  static constexpr int capture_end_index(int capture_index) {
    return capture_index * 2 + 1;
  }

  DECL_PRINTER(RegExpMatchInfo)
  DECL_VERIFIER(RegExpMatchInfo)

  class BodyDescriptor;

  static constexpr uint32_t kMinCapacity = 2;
  static constexpr uint32_t kLengthOffset = sizeof(HeapObject);
  static constexpr uint32_t kHeaderSize =
      kLengthOffset + (TAGGED_SIZE_8_BYTES ? kTaggedSize : kApiInt32Size) +
      3 * kTaggedSize;

 public:
  // length_ / optional_padding_ live in FixedArrayBase.
  TaggedMember<Smi> number_of_capture_registers_;
  TaggedMember<String> last_subject_;
  TaggedMember<Object> last_input_;
  FLEXIBLE_ARRAY_MEMBER(typename Super::ElementMemberT, objects);
} V8_OBJECT_END;

}  // namespace internal
}  // namespace v8

#include "src/objects/object-macros-undef.h"

#endif  // V8_OBJECTS_REGEXP_MATCH_INFO_H_
