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
18 namespace v8 {
19 namespace internal {
20
21 #include "torque-generated/src/objects/js-regexp-tq-inl.inc"
22
23 TQ_OBJECT_CONSTRUCTORS_IMPL(JSRegExp)
OBJECT_CONSTRUCTORS_IMPL_CHECK_SUPER(JSRegExpResult,JSArray)24 OBJECT_CONSTRUCTORS_IMPL_CHECK_SUPER(JSRegExpResult, JSArray)
25 OBJECT_CONSTRUCTORS_IMPL_CHECK_SUPER(JSRegExpResultIndices, JSArray)
26
27 CAST_ACCESSOR(JSRegExpResult)
28 CAST_ACCESSOR(JSRegExpResultIndices)
29
30 ACCESSORS(JSRegExp, last_index, Object, kLastIndexOffset)
31
32 JSRegExp::Type JSRegExp::TypeTag() const {
33 Object data = this->data();
34 if (data.IsUndefined()) return JSRegExp::NOT_COMPILED;
35 Smi smi = Smi::cast(FixedArray::cast(data).get(kTagIndex));
36 return static_cast<JSRegExp::Type>(smi.value());
37 }
38
CaptureCount()39 int JSRegExp::CaptureCount() const {
40 switch (TypeTag()) {
41 case ATOM:
42 return 0;
43 case EXPERIMENTAL:
44 case IRREGEXP:
45 return Smi::ToInt(DataAt(kIrregexpCaptureCountIndex));
46 default:
47 UNREACHABLE();
48 }
49 }
50
MaxRegisterCount()51 int JSRegExp::MaxRegisterCount() const {
52 CHECK_EQ(TypeTag(), IRREGEXP);
53 return Smi::ToInt(DataAt(kIrregexpMaxRegisterCountIndex));
54 }
55
GetFlags()56 JSRegExp::Flags JSRegExp::GetFlags() {
57 DCHECK(this->data().IsFixedArray());
58 Object data = this->data();
59 Smi smi = Smi::cast(FixedArray::cast(data).get(kFlagsIndex));
60 return Flags(smi.value());
61 }
62
Pattern()63 String JSRegExp::Pattern() {
64 DCHECK(this->data().IsFixedArray());
65 Object data = this->data();
66 String pattern = String::cast(FixedArray::cast(data).get(kSourceIndex));
67 return pattern;
68 }
69
CaptureNameMap()70 Object JSRegExp::CaptureNameMap() {
71 DCHECK(this->data().IsFixedArray());
72 DCHECK(TypeSupportsCaptures(TypeTag()));
73 Object value = DataAt(kIrregexpCaptureNameMapIndex);
74 DCHECK_NE(value, Smi::FromInt(JSRegExp::kUninitializedValue));
75 return value;
76 }
77
DataAt(int index)78 Object JSRegExp::DataAt(int index) const {
79 DCHECK(TypeTag() != NOT_COMPILED);
80 return FixedArray::cast(data()).get(index);
81 }
82
SetDataAt(int index,Object value)83 void JSRegExp::SetDataAt(int index, Object value) {
84 DCHECK(TypeTag() != NOT_COMPILED);
85 DCHECK_GE(index,
86 kDataIndex); // Only implementation data can be set this way.
87 FixedArray::cast(data()).set(index, value);
88 }
89
SetCaptureNameMap(Handle<FixedArray> capture_name_map)90 void JSRegExp::SetCaptureNameMap(Handle<FixedArray> capture_name_map) {
91 if (capture_name_map.is_null()) {
92 SetDataAt(JSRegExp::kIrregexpCaptureNameMapIndex, Smi::zero());
93 } else {
94 SetDataAt(JSRegExp::kIrregexpCaptureNameMapIndex, *capture_name_map);
95 }
96 }
97
HasCompiledCode()98 bool JSRegExp::HasCompiledCode() const {
99 if (TypeTag() != IRREGEXP) return false;
100 Smi uninitialized = Smi::FromInt(kUninitializedValue);
101 #ifdef DEBUG
102 DCHECK(DataAt(kIrregexpLatin1CodeIndex).IsCode() ||
103 DataAt(kIrregexpLatin1CodeIndex) == uninitialized);
104 DCHECK(DataAt(kIrregexpUC16CodeIndex).IsCode() ||
105 DataAt(kIrregexpUC16CodeIndex) == uninitialized);
106 DCHECK(DataAt(kIrregexpLatin1BytecodeIndex).IsByteArray() ||
107 DataAt(kIrregexpLatin1BytecodeIndex) == uninitialized);
108 DCHECK(DataAt(kIrregexpUC16BytecodeIndex).IsByteArray() ||
109 DataAt(kIrregexpUC16BytecodeIndex) == uninitialized);
110 #endif // DEBUG
111 return (DataAt(kIrregexpLatin1CodeIndex) != uninitialized ||
112 DataAt(kIrregexpUC16CodeIndex) != uninitialized);
113 }
114
DiscardCompiledCodeForSerialization()115 void JSRegExp::DiscardCompiledCodeForSerialization() {
116 DCHECK(HasCompiledCode());
117 Smi uninitialized = Smi::FromInt(kUninitializedValue);
118 SetDataAt(kIrregexpLatin1CodeIndex, uninitialized);
119 SetDataAt(kIrregexpUC16CodeIndex, uninitialized);
120 SetDataAt(kIrregexpLatin1BytecodeIndex, uninitialized);
121 SetDataAt(kIrregexpUC16BytecodeIndex, uninitialized);
122 }
123
124 } // namespace internal
125 } // namespace v8
126
127 #include "src/objects/object-macros-undef.h"
128
129 #endif // V8_OBJECTS_JS_REGEXP_INL_H_
130