/src/serenity/Userland/Libraries/LibJS/Runtime/RegExpObject.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org> |
3 | | * Copyright (c) 2024, Andreas Kling <andreas@ladybird.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/EnumBits.h> |
11 | | #include <AK/Optional.h> |
12 | | #include <AK/Result.h> |
13 | | #include <LibJS/Runtime/Object.h> |
14 | | #include <LibRegex/Regex.h> |
15 | | |
16 | | namespace JS { |
17 | | |
18 | | ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_create(VM&, Value pattern, Value flags); |
19 | | ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_alloc(VM&, FunctionObject& new_target); |
20 | | |
21 | | Result<regex::RegexOptions<ECMAScriptFlags>, ByteString> regex_flags_from_string(StringView flags); |
22 | | struct ParseRegexPatternError { |
23 | | ByteString error; |
24 | | }; |
25 | | ErrorOr<ByteString, ParseRegexPatternError> parse_regex_pattern(StringView pattern, bool unicode, bool unicode_sets); |
26 | | ThrowCompletionOr<ByteString> parse_regex_pattern(VM& vm, StringView pattern, bool unicode, bool unicode_sets); |
27 | | |
28 | | class RegExpObject : public Object { |
29 | | JS_OBJECT(RegExpObject, Object); |
30 | | JS_DECLARE_ALLOCATOR(RegExpObject); |
31 | | |
32 | | public: |
33 | | // JS regexps are all 'global' by default as per our definition, but the "global" flag enables "stateful". |
34 | | // FIXME: Enable 'BrowserExtended' only if in a browser context. |
35 | | static constexpr regex::RegexOptions<ECMAScriptFlags> default_flags { |
36 | | (regex::ECMAScriptFlags)regex::AllFlags::SingleMatch |
37 | | | (regex::ECMAScriptFlags)regex::AllFlags::Global |
38 | | | (regex::ECMAScriptFlags)regex::AllFlags::SkipTrimEmptyMatches |
39 | | | regex::ECMAScriptFlags::BrowserExtended |
40 | | }; |
41 | | |
42 | | enum class Flags { |
43 | | HasIndices = 1 << 0, |
44 | | Global = 1 << 1, |
45 | | IgnoreCase = 1 << 2, |
46 | | Multiline = 1 << 3, |
47 | | DotAll = 1 << 4, |
48 | | UnicodeSets = 1 << 5, |
49 | | Unicode = 1 << 6, |
50 | | Sticky = 1 << 7, |
51 | | }; |
52 | | |
53 | | static NonnullGCPtr<RegExpObject> create(Realm&); |
54 | | static NonnullGCPtr<RegExpObject> create(Realm&, Regex<ECMA262> regex, ByteString pattern, ByteString flags); |
55 | | |
56 | | ThrowCompletionOr<NonnullGCPtr<RegExpObject>> regexp_initialize(VM&, Value pattern, Value flags); |
57 | | ByteString escape_regexp_pattern() const; |
58 | | |
59 | | virtual void initialize(Realm&) override; |
60 | 0 | virtual ~RegExpObject() override = default; |
61 | | |
62 | 0 | ByteString const& pattern() const { return m_pattern; } |
63 | 0 | ByteString const& flags() const { return m_flags; } |
64 | 0 | Flags flag_bits() const { return m_flag_bits; } |
65 | 0 | Regex<ECMA262> const& regex() { return *m_regex; } |
66 | 0 | Regex<ECMA262> const& regex() const { return *m_regex; } |
67 | 0 | Realm& realm() { return *m_realm; } |
68 | 0 | Realm const& realm() const { return *m_realm; } |
69 | 0 | bool legacy_features_enabled() const { return m_legacy_features_enabled; } |
70 | 0 | void set_legacy_features_enabled(bool legacy_features_enabled) { m_legacy_features_enabled = legacy_features_enabled; } |
71 | 0 | void set_realm(Realm& realm) { m_realm = &realm; } |
72 | | |
73 | | private: |
74 | | RegExpObject(Object& prototype); |
75 | | RegExpObject(Regex<ECMA262> regex, ByteString pattern, ByteString flags, Object& prototype); |
76 | | |
77 | | virtual void visit_edges(Visitor&) override; |
78 | | |
79 | | ByteString m_pattern; |
80 | | ByteString m_flags; |
81 | | Flags m_flag_bits { 0 }; |
82 | | bool m_legacy_features_enabled { false }; // [[LegacyFeaturesEnabled]] |
83 | | // Note: This is initialized in RegExpAlloc, but will be non-null afterwards |
84 | | GCPtr<Realm> m_realm; // [[Realm]] |
85 | | Optional<Regex<ECMA262>> m_regex; |
86 | | }; |
87 | | |
88 | | AK_ENUM_BITWISE_OPERATORS(RegExpObject::Flags); |
89 | | |
90 | | } |