RegExpObject.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Optional.h>
  8. #include <AK/Result.h>
  9. #include <LibJS/Runtime/Object.h>
  10. #include <LibRegex/Regex.h>
  11. namespace JS {
  12. ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_create(VM&, Value pattern, Value flags);
  13. ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_alloc(VM&, FunctionObject& new_target);
  14. Result<regex::RegexOptions<ECMAScriptFlags>, DeprecatedString> regex_flags_from_string(StringView flags);
  15. struct ParseRegexPatternError {
  16. DeprecatedString error;
  17. };
  18. ErrorOr<DeprecatedString, ParseRegexPatternError> parse_regex_pattern(StringView pattern, bool unicode, bool unicode_sets);
  19. ThrowCompletionOr<DeprecatedString> parse_regex_pattern(VM& vm, StringView pattern, bool unicode, bool unicode_sets);
  20. class RegExpObject : public Object {
  21. JS_OBJECT(RegExpObject, Object);
  22. public:
  23. // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful".
  24. // FIXME: Enable 'BrowserExtended' only if in a browser context.
  25. static constexpr regex::RegexOptions<ECMAScriptFlags> default_flags {
  26. (regex::ECMAScriptFlags)regex::AllFlags::SingleMatch
  27. | (regex::ECMAScriptFlags)regex::AllFlags::Global
  28. | (regex::ECMAScriptFlags)regex::AllFlags::SkipTrimEmptyMatches
  29. | regex::ECMAScriptFlags::BrowserExtended
  30. };
  31. static NonnullGCPtr<RegExpObject> create(Realm&);
  32. static NonnullGCPtr<RegExpObject> create(Realm&, Regex<ECMA262> regex, DeprecatedString pattern, DeprecatedString flags);
  33. ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_initialize(VM&, Value pattern, Value flags);
  34. DeprecatedString escape_regexp_pattern() const;
  35. virtual void initialize(Realm&) override;
  36. virtual ~RegExpObject() override = default;
  37. DeprecatedString const& pattern() const { return m_pattern; }
  38. DeprecatedString const& flags() const { return m_flags; }
  39. Regex<ECMA262> const& regex() { return *m_regex; }
  40. Regex<ECMA262> const& regex() const { return *m_regex; }
  41. Realm& realm() { return *m_realm; }
  42. Realm const& realm() const { return *m_realm; }
  43. bool legacy_features_enabled() const { return m_legacy_features_enabled; }
  44. void set_legacy_features_enabled(bool legacy_features_enabled) { m_legacy_features_enabled = legacy_features_enabled; }
  45. void set_realm(Realm& realm) { m_realm = &realm; }
  46. private:
  47. RegExpObject(Object& prototype);
  48. RegExpObject(Regex<ECMA262> regex, DeprecatedString pattern, DeprecatedString flags, Object& prototype);
  49. DeprecatedString m_pattern;
  50. DeprecatedString m_flags;
  51. bool m_legacy_features_enabled { false }; // [[LegacyFeaturesEnabled]]
  52. // Note: This is initialized in RegExpAlloc, but will be non-null afterwards
  53. GCPtr<Realm> m_realm; // [[Realm]]
  54. Optional<Regex<ECMA262>> m_regex;
  55. };
  56. }