RegExpObject.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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>, ByteString> regex_flags_from_string(StringView flags);
  15. struct ParseRegexPatternError {
  16. ByteString error;
  17. };
  18. ErrorOr<ByteString, ParseRegexPatternError> parse_regex_pattern(StringView pattern, bool unicode, bool unicode_sets);
  19. ThrowCompletionOr<ByteString> parse_regex_pattern(VM& vm, StringView pattern, bool unicode, bool unicode_sets);
  20. class RegExpObject : public Object {
  21. JS_OBJECT(RegExpObject, Object);
  22. JS_DECLARE_ALLOCATOR(RegExpObject);
  23. public:
  24. // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful".
  25. // FIXME: Enable 'BrowserExtended' only if in a browser context.
  26. static constexpr regex::RegexOptions<ECMAScriptFlags> default_flags {
  27. (regex::ECMAScriptFlags)regex::AllFlags::SingleMatch
  28. | (regex::ECMAScriptFlags)regex::AllFlags::Global
  29. | (regex::ECMAScriptFlags)regex::AllFlags::SkipTrimEmptyMatches
  30. | regex::ECMAScriptFlags::BrowserExtended
  31. };
  32. static NonnullGCPtr<RegExpObject> create(Realm&);
  33. static NonnullGCPtr<RegExpObject> create(Realm&, Regex<ECMA262> regex, ByteString pattern, ByteString flags);
  34. ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_initialize(VM&, Value pattern, Value flags);
  35. ByteString escape_regexp_pattern() const;
  36. virtual void initialize(Realm&) override;
  37. virtual ~RegExpObject() override = default;
  38. ByteString const& pattern() const { return m_pattern; }
  39. ByteString const& flags() const { return m_flags; }
  40. Regex<ECMA262> const& regex() { return *m_regex; }
  41. Regex<ECMA262> const& regex() const { return *m_regex; }
  42. Realm& realm() { return *m_realm; }
  43. Realm const& realm() const { return *m_realm; }
  44. bool legacy_features_enabled() const { return m_legacy_features_enabled; }
  45. void set_legacy_features_enabled(bool legacy_features_enabled) { m_legacy_features_enabled = legacy_features_enabled; }
  46. void set_realm(Realm& realm) { m_realm = &realm; }
  47. private:
  48. RegExpObject(Object& prototype);
  49. RegExpObject(Regex<ECMA262> regex, ByteString pattern, ByteString flags, Object& prototype);
  50. ByteString m_pattern;
  51. ByteString m_flags;
  52. bool m_legacy_features_enabled { false }; // [[LegacyFeaturesEnabled]]
  53. // Note: This is initialized in RegExpAlloc, but will be non-null afterwards
  54. GCPtr<Realm> m_realm; // [[Realm]]
  55. Optional<Regex<ECMA262>> m_regex;
  56. };
  57. }