V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
js-regexp.h
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_H_
6 #define V8_OBJECTS_JS_REGEXP_H_
7 
8 #include "src/objects/js-array.h"
9 
10 // Has to be the last include (doesn't have include guards):
11 #include "src/objects/object-macros.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 // Regular expressions
17 // The regular expression holds a single reference to a FixedArray in
18 // the kDataOffset field.
19 // The FixedArray contains the following data:
20 // - tag : type of regexp implementation (not compiled yet, atom or irregexp)
21 // - reference to the original source string
22 // - reference to the original flag string
23 // If it is an atom regexp
24 // - a reference to a literal string to search for
25 // If it is an irregexp regexp:
26 // - a reference to code for Latin1 inputs (bytecode or compiled), or a smi
27 // used for tracking the last usage (used for regexp code flushing).
28 // - a reference to code for UC16 inputs (bytecode or compiled), or a smi
29 // used for tracking the last usage (used for regexp code flushing).
30 // - max number of registers used by irregexp implementations.
31 // - number of capture registers (output values) of the regexp.
32 class JSRegExp : public JSObject {
33  public:
34  // Meaning of Type:
35  // NOT_COMPILED: Initial value. No data has been stored in the JSRegExp yet.
36  // ATOM: A simple string to match against using an indexOf operation.
37  // IRREGEXP: Compiled with Irregexp.
38  enum Type { NOT_COMPILED, ATOM, IRREGEXP };
39  enum Flag {
40  kNone = 0,
41  kGlobal = 1 << 0,
42  kIgnoreCase = 1 << 1,
43  kMultiline = 1 << 2,
44  kSticky = 1 << 3,
45  kUnicode = 1 << 4,
46  kDotAll = 1 << 5,
47  // Update FlagCount when adding new flags.
48  };
49  typedef base::Flags<Flag> Flags;
50 
51  static constexpr int FlagCount() { return 6; }
52 
53  static int FlagShiftBits(Flag flag) {
54  switch (flag) {
55  case kGlobal:
56  STATIC_ASSERT(kGlobal == (1 << 0));
57  return 0;
58  case kIgnoreCase:
59  STATIC_ASSERT(kIgnoreCase == (1 << 1));
60  return 1;
61  case kMultiline:
62  STATIC_ASSERT(kMultiline == (1 << 2));
63  return 2;
64  case kSticky:
65  STATIC_ASSERT(kSticky == (1 << 3));
66  return 3;
67  case kUnicode:
68  STATIC_ASSERT(kUnicode == (1 << 4));
69  return 4;
70  case kDotAll:
71  STATIC_ASSERT(kDotAll == (1 << 5));
72  return 5;
73  default:
74  STATIC_ASSERT(FlagCount() == 6);
75  UNREACHABLE();
76  }
77  }
78 
79  DECL_ACCESSORS(data, Object)
80  DECL_ACCESSORS(flags, Object)
81  DECL_ACCESSORS(last_index, Object)
82  DECL_ACCESSORS(source, Object)
83 
84  V8_EXPORT_PRIVATE static MaybeHandle<JSRegExp> New(Isolate* isolate,
85  Handle<String> source,
86  Flags flags);
87  static Handle<JSRegExp> Copy(Handle<JSRegExp> regexp);
88 
89  static MaybeHandle<JSRegExp> Initialize(Handle<JSRegExp> regexp,
90  Handle<String> source, Flags flags);
91  static MaybeHandle<JSRegExp> Initialize(Handle<JSRegExp> regexp,
92  Handle<String> source,
93  Handle<String> flags_string);
94 
95  inline Type TypeTag();
96  // Number of captures (without the match itself).
97  inline int CaptureCount();
98  inline Flags GetFlags();
99  inline String Pattern();
100  inline Object* CaptureNameMap();
101  inline Object* DataAt(int index);
102  // Set implementation data after the object has been prepared.
103  inline void SetDataAt(int index, Object* value);
104 
105  static int code_index(bool is_latin1) {
106  if (is_latin1) {
107  return kIrregexpLatin1CodeIndex;
108  } else {
109  return kIrregexpUC16CodeIndex;
110  }
111  }
112 
113  DECL_CAST(JSRegExp)
114 
115  // Dispatched behavior.
116  DECL_PRINTER(JSRegExp)
117  DECL_VERIFIER(JSRegExp)
118 
119 // Layout description.
120 #define JS_REGEXP_FIELDS(V) \
121  V(kDataOffset, kTaggedSize) \
122  V(kSourceOffset, kTaggedSize) \
123  V(kFlagsOffset, kTaggedSize) \
124  /* Total size. */ \
125  V(kSize, 0) \
126  /* This is already an in-object field. */ \
127  V(kLastIndexOffset, 0)
128 
129  DEFINE_FIELD_OFFSET_CONSTANTS(JSObject::kHeaderSize, JS_REGEXP_FIELDS)
130 #undef JS_REGEXP_FIELDS
131 
132  // Indices in the data array.
133  static const int kTagIndex = 0;
134  static const int kSourceIndex = kTagIndex + 1;
135  static const int kFlagsIndex = kSourceIndex + 1;
136  static const int kDataIndex = kFlagsIndex + 1;
137  // The data fields are used in different ways depending on the
138  // value of the tag.
139  // Atom regexps (literal strings).
140  static const int kAtomPatternIndex = kDataIndex;
141 
142  static const int kAtomDataSize = kAtomPatternIndex + 1;
143 
144  // Irregexp compiled code or bytecode for Latin1. If compilation
145  // fails, this fields hold an exception object that should be
146  // thrown if the regexp is used again.
147  static const int kIrregexpLatin1CodeIndex = kDataIndex;
148  // Irregexp compiled code or bytecode for UC16. If compilation
149  // fails, this fields hold an exception object that should be
150  // thrown if the regexp is used again.
151  static const int kIrregexpUC16CodeIndex = kDataIndex + 1;
152  // Maximal number of registers used by either Latin1 or UC16.
153  // Only used to check that there is enough stack space
154  static const int kIrregexpMaxRegisterCountIndex = kDataIndex + 2;
155  // Number of captures in the compiled regexp.
156  static const int kIrregexpCaptureCountIndex = kDataIndex + 3;
157  // Maps names of named capture groups (at indices 2i) to their corresponding
158  // (1-based) capture group indices (at indices 2i + 1).
159  static const int kIrregexpCaptureNameMapIndex = kDataIndex + 4;
160 
161  static const int kIrregexpDataSize = kIrregexpCaptureNameMapIndex + 1;
162 
163  // In-object fields.
164  static const int kLastIndexFieldIndex = 0;
165  static const int kInObjectFieldCount = 1;
166 
167  // Descriptor array index to important methods in the prototype.
168  static const int kExecFunctionDescriptorIndex = 1;
169  static const int kSymbolMatchFunctionDescriptorIndex = 13;
170  static const int kSymbolReplaceFunctionDescriptorIndex = 14;
171  static const int kSymbolSearchFunctionDescriptorIndex = 15;
172  static const int kSymbolSplitFunctionDescriptorIndex = 16;
173  static const int kSymbolMatchAllFunctionDescriptorIndex = 17;
174 
175  // The uninitialized value for a regexp code object.
176  static const int kUninitializedValue = -1;
177 };
178 
179 DEFINE_OPERATORS_FOR_FLAGS(JSRegExp::Flags)
180 
181 // JSRegExpResult is just a JSArray with a specific initial map.
182 // This initial map adds in-object properties for "index" and "input"
183 // properties, as assigned by RegExp.prototype.exec, which allows
184 // faster creation of RegExp exec results.
185 // This class just holds constants used when creating the result.
186 // After creation the result must be treated as a JSArray in all regards.
187 class JSRegExpResult : public JSArray {
188  public:
189 // Layout description.
190 #define REG_EXP_RESULT_FIELDS(V) \
191  V(kIndexOffset, kTaggedSize) \
192  V(kInputOffset, kTaggedSize) \
193  V(kGroupsOffset, kTaggedSize) \
194  /* Total size. */ \
195  V(kSize, 0)
196 
197  DEFINE_FIELD_OFFSET_CONSTANTS(JSArray::kSize, REG_EXP_RESULT_FIELDS)
198 #undef REG_EXP_RESULT_FIELDS
199 
200  // Indices of in-object properties.
201  static const int kIndexIndex = 0;
202  static const int kInputIndex = 1;
203  static const int kGroupsIndex = 2;
204  static const int kInObjectPropertyCount = 3;
205 
206  private:
207  DISALLOW_IMPLICIT_CONSTRUCTORS(JSRegExpResult);
208 };
209 
210 } // namespace internal
211 } // namespace v8
212 
213 #include "src/objects/object-macros-undef.h"
214 
215 #endif // V8_OBJECTS_JS_REGEXP_H_
Definition: libplatform.h:13