1/*
2 * Copyright (C) 2010 University of Szeged
3 * Copyright (C) 2010 Renata Hodovan ([email protected])
4 * All rights reserved.
5 * Copyright (C) 2019 Apple Inc. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY UNIVERSITY OF SZEGED ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF SZEGED OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#include "RegExp.h"
32#include "RegExpKey.h"
33#include "Strong.h"
34#include "Weak.h"
35#include <array>
36#include <wtf/HashMap.h>
37
38namespace JSC {
39
40namespace Yarr {
41enum class Flags : uint8_t;
42}
43
44class RegExpCache : private WeakHandleOwner {
45 WTF_MAKE_FAST_ALLOCATED;
46
47 friend class RegExp;
48 typedef HashMap<RegExpKey, Weak<RegExp>> RegExpCacheMap;
49
50public:
51 RegExpCache(VM* vm);
52 void deleteAllCode();
53
54 RegExp* ensureEmptyRegExp(VM& vm)
55 {
56 if (LIKELY(m_emptyRegExp))
57 return m_emptyRegExp.get();
58 return ensureEmptyRegExpSlow(vm);
59 }
60
61private:
62
63 static constexpr unsigned maxStrongCacheablePatternLength = 256;
64
65 static constexpr int maxStrongCacheableEntries = 32;
66
67 void finalize(Handle<Unknown>, void* context) override;
68
69 RegExp* ensureEmptyRegExpSlow(VM&);
70
71 RegExp* lookupOrCreate(const WTF::String& patternString, OptionSet<Yarr::Flags>);
72 void addToStrongCache(RegExp*);
73 RegExpCacheMap m_weakCache; // Holds all regular expressions currently live.
74 int m_nextEntryInStrongCache;
75 std::array<Strong<RegExp>, maxStrongCacheableEntries> m_strongCache; // Holds a select few regular expressions that have compiled and executed
76 Strong<RegExp> m_emptyRegExp;
77 VM* m_vm;
78};
79
80} // namespace JSC
81