1
2// Copyright 2021 the V8 project authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6#ifndef INCLUDE_V8_REGEXP_H_
7#define INCLUDE_V8_REGEXP_H_
8
9#include "v8-local-handle.h"  // NOLINT(build/include_directory)
10#include "v8-object.h"        // NOLINT(build/include_directory)
11#include "v8config.h"         // NOLINT(build/include_directory)
12
13namespace v8 {
14
15class Context;
16
17/**
18 * An instance of the built-in RegExp constructor (ECMA-262, 15.10).
19 */
20class V8_EXPORT RegExp : public Object {
21 public:
22  /**
23   * Regular expression flag bits. They can be or'ed to enable a set
24   * of flags.
25   * The kLinear value ('l') is experimental and can only be used with
26   * --enable-experimental-regexp-engine.  RegExps with kLinear flag are
27   *  guaranteed to be executed in asymptotic linear time wrt. the length of
28   *  the subject string.
29   */
30  enum Flags {
31    kNone = 0,
32    kGlobal = 1 << 0,
33    kIgnoreCase = 1 << 1,
34    kMultiline = 1 << 2,
35    kSticky = 1 << 3,
36    kUnicode = 1 << 4,
37    kDotAll = 1 << 5,
38    kLinear = 1 << 6,
39    kHasIndices = 1 << 7,
40    kUnicodeSets = 1 << 8,
41  };
42
43  static constexpr int kFlagCount = 9;
44
45  /**
46   * Creates a regular expression from the given pattern string and
47   * the flags bit field. May throw a JavaScript exception as
48   * described in ECMA-262, 15.10.4.1.
49   *
50   * For example,
51   *   RegExp::New(v8::String::New("foo"),
52   *               static_cast<RegExp::Flags>(kGlobal | kMultiline))
53   * is equivalent to evaluating "/foo/gm".
54   */
55  static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> New(Local<Context> context,
56                                                      Local<String> pattern,
57                                                      Flags flags);
58
59  /**
60   * Like New, but additionally specifies a backtrack limit. If the number of
61   * backtracks done in one Exec call hits the limit, a match failure is
62   * immediately returned.
63   */
64  static V8_WARN_UNUSED_RESULT MaybeLocal<RegExp> NewWithBacktrackLimit(
65      Local<Context> context, Local<String> pattern, Flags flags,
66      uint32_t backtrack_limit);
67
68  /**
69   * Executes the current RegExp instance on the given subject string.
70   * Equivalent to RegExp.prototype.exec as described in
71   *
72   *   https://tc39.es/ecma262/#sec-regexp.prototype.exec
73   *
74   * On success, an Array containing the matched strings is returned. On
75   * failure, returns Null.
76   *
77   * Note: modifies global context state, accessible e.g. through RegExp.input.
78   */
79  V8_WARN_UNUSED_RESULT MaybeLocal<Object> Exec(Local<Context> context,
80                                                Local<String> subject);
81
82  /**
83   * Returns the value of the source property: a string representing
84   * the regular expression.
85   */
86  Local<String> GetSource() const;
87
88  /**
89   * Returns the flags bit field.
90   */
91  Flags GetFlags() const;
92
93  V8_INLINE static RegExp* Cast(Value* value) {
94#ifdef V8_ENABLE_CHECKS
95    CheckCast(value);
96#endif
97    return static_cast<RegExp*>(value);
98  }
99
100 private:
101  static void CheckCast(Value* obj);
102};
103
104}  // namespace v8
105
106#endif  // INCLUDE_V8_REGEXP_H_
107