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-inl.h" // Needed for write barriers
11 #include "src/objects/string.h"
12
13 // Has to be the last include (doesn't have include guards):
14 #include "src/objects/object-macros.h"
15
16 namespace v8 {
17 namespace internal {
18
19 CAST_ACCESSOR(JSRegExp)
20
ACCESSORS(JSRegExp,data,Object,kDataOffset)21 ACCESSORS(JSRegExp, data, Object, kDataOffset)
22 ACCESSORS(JSRegExp, flags, Object, kFlagsOffset)
23 ACCESSORS(JSRegExp, source, Object, kSourceOffset)
24 ACCESSORS(JSRegExp, last_index, Object, kLastIndexOffset)
25
26 JSRegExp::Type JSRegExp::TypeTag() {
27 Object* data = this->data();
28 if (data->IsUndefined()) return JSRegExp::NOT_COMPILED;
29 Smi* smi = Smi::cast(FixedArray::cast(data)->get(kTagIndex));
30 return static_cast<JSRegExp::Type>(smi->value());
31 }
32
CaptureCount()33 int JSRegExp::CaptureCount() {
34 switch (TypeTag()) {
35 case ATOM:
36 return 0;
37 case IRREGEXP:
38 return Smi::ToInt(DataAt(kIrregexpCaptureCountIndex));
39 default:
40 UNREACHABLE();
41 }
42 }
43
GetFlags()44 JSRegExp::Flags JSRegExp::GetFlags() {
45 DCHECK(this->data()->IsFixedArray());
46 Object* data = this->data();
47 Smi* smi = Smi::cast(FixedArray::cast(data)->get(kFlagsIndex));
48 return Flags(smi->value());
49 }
50
Pattern()51 String* JSRegExp::Pattern() {
52 DCHECK(this->data()->IsFixedArray());
53 Object* data = this->data();
54 String* pattern = String::cast(FixedArray::cast(data)->get(kSourceIndex));
55 return pattern;
56 }
57
CaptureNameMap()58 Object* JSRegExp::CaptureNameMap() {
59 DCHECK(this->data()->IsFixedArray());
60 DCHECK_EQ(TypeTag(), IRREGEXP);
61 Object* value = DataAt(kIrregexpCaptureNameMapIndex);
62 DCHECK_NE(value, Smi::FromInt(JSRegExp::kUninitializedValue));
63 return value;
64 }
65
DataAt(int index)66 Object* JSRegExp::DataAt(int index) {
67 DCHECK(TypeTag() != NOT_COMPILED);
68 return FixedArray::cast(data())->get(index);
69 }
70
SetDataAt(int index,Object * value)71 void JSRegExp::SetDataAt(int index, Object* value) {
72 DCHECK(TypeTag() != NOT_COMPILED);
73 DCHECK_GE(index,
74 kDataIndex); // Only implementation data can be set this way.
75 FixedArray::cast(data())->set(index, value);
76 }
77
78 } // namespace internal
79 } // namespace v8
80
81 #include "src/objects/object-macros-undef.h"
82
83 #endif // V8_OBJECTS_JS_REGEXP_INL_H_
84