v8-regexp.h 3.1 KB

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