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_REGEXP_INL_H_
6#define V8_OBJECTS_JS_REGEXP_INL_H_
7
8#include "src/objects/js-regexp.h"
9
10#include "src/objects/js-array-inl.h"
11#include "src/objects/objects-inl.h"  // Needed for write barriers
12#include "src/objects/smi.h"
13#include "src/objects/string.h"
14
15// Has to be the last include (doesn't have include guards):
16#include "src/objects/object-macros.h"
17
18namespace v8 {
19namespace internal {
20
21#include "torque-generated/src/objects/js-regexp-tq-inl.inc"
22
23TQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExp)
24TQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExpResult)
25TQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExpResultIndices)
26TQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExpResultWithIndices)
27
28ACCESSORS(JSRegExp, last_index, Object, kLastIndexOffset)
29
30JSRegExp::Type JSRegExp::type_tag() const {
31  Object data = this->data();
32  if (data.IsUndefined()) return JSRegExp::NOT_COMPILED;
33  Smi smi = Smi::cast(FixedArray::cast(data).get(kTagIndex));
34  return static_cast<JSRegExp::Type>(smi.value());
35}
36
37int JSRegExp::capture_count() const {
38  switch (type_tag()) {
39    case ATOM:
40      return 0;
41    case EXPERIMENTAL:
42    case IRREGEXP:
43      return Smi::ToInt(DataAt(kIrregexpCaptureCountIndex));
44    default:
45      UNREACHABLE();
46  }
47}
48
49int JSRegExp::max_register_count() const {
50  CHECK_EQ(type_tag(), IRREGEXP);
51  return Smi::ToInt(DataAt(kIrregexpMaxRegisterCountIndex));
52}
53
54String JSRegExp::atom_pattern() const {
55  DCHECK_EQ(type_tag(), ATOM);
56  return String::cast(DataAt(JSRegExp::kAtomPatternIndex));
57}
58
59String JSRegExp::source() const {
60  return String::cast(TorqueGeneratedClass::source());
61}
62
63JSRegExp::Flags JSRegExp::flags() const {
64  Smi smi = Smi::cast(TorqueGeneratedClass::flags());
65  return Flags(smi.value());
66}
67
68String JSRegExp::EscapedPattern() {
69  DCHECK(this->source().IsString());
70  return String::cast(source());
71}
72
73Object JSRegExp::capture_name_map() {
74  DCHECK(TypeSupportsCaptures(type_tag()));
75  Object value = DataAt(kIrregexpCaptureNameMapIndex);
76  DCHECK_NE(value, Smi::FromInt(JSRegExp::kUninitializedValue));
77  return value;
78}
79
80void JSRegExp::set_capture_name_map(Handle<FixedArray> capture_name_map) {
81  if (capture_name_map.is_null()) {
82    SetDataAt(JSRegExp::kIrregexpCaptureNameMapIndex, Smi::zero());
83  } else {
84    SetDataAt(JSRegExp::kIrregexpCaptureNameMapIndex, *capture_name_map);
85  }
86}
87
88Object JSRegExp::DataAt(int index) const {
89  DCHECK(type_tag() != NOT_COMPILED);
90  return FixedArray::cast(data()).get(index);
91}
92
93void JSRegExp::SetDataAt(int index, Object value) {
94  DCHECK(type_tag() != NOT_COMPILED);
95  // Only implementation data can be set this way.
96  DCHECK_GE(index, kFirstTypeSpecificIndex);
97  FixedArray::cast(data()).set(index, value);
98}
99
100bool JSRegExp::HasCompiledCode() const {
101  if (type_tag() != IRREGEXP) return false;
102  Smi uninitialized = Smi::FromInt(kUninitializedValue);
103#ifdef DEBUG
104  DCHECK(DataAt(kIrregexpLatin1CodeIndex).IsCodeT() ||
105         DataAt(kIrregexpLatin1CodeIndex) == uninitialized);
106  DCHECK(DataAt(kIrregexpUC16CodeIndex).IsCodeT() ||
107         DataAt(kIrregexpUC16CodeIndex) == uninitialized);
108  DCHECK(DataAt(kIrregexpLatin1BytecodeIndex).IsByteArray() ||
109         DataAt(kIrregexpLatin1BytecodeIndex) == uninitialized);
110  DCHECK(DataAt(kIrregexpUC16BytecodeIndex).IsByteArray() ||
111         DataAt(kIrregexpUC16BytecodeIndex) == uninitialized);
112#endif  // DEBUG
113  return (DataAt(kIrregexpLatin1CodeIndex) != uninitialized ||
114          DataAt(kIrregexpUC16CodeIndex) != uninitialized);
115}
116
117void JSRegExp::DiscardCompiledCodeForSerialization() {
118  DCHECK(HasCompiledCode());
119  Smi uninitialized = Smi::FromInt(kUninitializedValue);
120  SetDataAt(kIrregexpLatin1CodeIndex, uninitialized);
121  SetDataAt(kIrregexpUC16CodeIndex, uninitialized);
122  SetDataAt(kIrregexpLatin1BytecodeIndex, uninitialized);
123  SetDataAt(kIrregexpUC16BytecodeIndex, uninitialized);
124}
125
126}  // namespace internal
127}  // namespace v8
128
129#include "src/objects/object-macros-undef.h"
130
131#endif  // V8_OBJECTS_JS_REGEXP_INL_H_
132