kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 1 | /* |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
fpizlo@apple.com | 6ea42db | 2016-03-08 21:15:07 +0000 | [diff] [blame] | 3 | * Copyright (C) 2007, 2008, 2009, 2016 Apple Inc. All rights reserved. |
mjs@apple.com | 0a30b7a | 2009-07-04 14:21:30 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009 Torch Mobile, Inc. |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
| 9 | * version 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
mjs | cdff33b | 2006-01-23 21:41:36 +0000 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
mjs | 6f821c8 | 2002-03-22 00:31:57 +0000 | [diff] [blame] | 19 | * |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 22 | #pragma once |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 23 | |
fpizlo@apple.com | 171d06f | 2016-11-15 23:21:50 +0000 | [diff] [blame] | 24 | #include "ConcurrentJSLock.h" |
barraclough@apple.com | f280e17 | 2012-03-28 22:18:20 +0000 | [diff] [blame] | 25 | #include "MatchResult.h" |
barraclough@apple.com | 1281293 | 2011-03-09 23:04:27 +0000 | [diff] [blame] | 26 | #include "RegExpKey.h" |
barraclough@apple.com | f280e17 | 2012-03-28 22:18:20 +0000 | [diff] [blame] | 27 | #include "Structure.h" |
msaboff@apple.com | 2cc4150 | 2011-09-12 22:17:53 +0000 | [diff] [blame] | 28 | #include "yarr/Yarr.h" |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 29 | #include <wtf/Forward.h> |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 30 | #include <wtf/text/WTFString.h> |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 31 | |
barraclough@apple.com | f280e17 | 2012-03-28 22:18:20 +0000 | [diff] [blame] | 32 | #if ENABLE(YARR_JIT) |
| 33 | #include "yarr/YarrJIT.h" |
| 34 | #endif |
| 35 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 36 | namespace JSC { |
kocienda | 66a6d36 | 2001-08-24 14:24:45 +0000 | [diff] [blame] | 37 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 38 | struct RegExpRepresentation; |
| 39 | class VM; |
darin@apple.com | 8c2bac0 | 2008-10-09 00:40:43 +0000 | [diff] [blame] | 40 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 41 | JS_EXPORT_PRIVATE RegExpFlags regExpFlags(const String&); |
barraclough@apple.com | 1281293 | 2011-03-09 23:04:27 +0000 | [diff] [blame] | 42 | |
akling@apple.com | 4b9e000 | 2015-04-13 19:12:48 +0000 | [diff] [blame] | 43 | class RegExp final : public JSCell { |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 44 | public: |
| 45 | typedef JSCell Base; |
akling@apple.com | 4b9e000 | 2015-04-13 19:12:48 +0000 | [diff] [blame] | 46 | static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal; |
commit-queue@webkit.org | 6c25c52 | 2011-08-09 20:46:17 +0000 | [diff] [blame] | 47 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 48 | JS_EXPORT_PRIVATE static RegExp* create(VM&, const String& pattern, RegExpFlags); |
| 49 | static const bool needsDestruction = true; |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 50 | static void destroy(JSCell*); |
commit-queue@webkit.org | 00eb52f | 2016-03-01 02:07:12 +0000 | [diff] [blame] | 51 | static size_t estimatedSize(JSCell*); |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 52 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 53 | bool global() const { return m_flags & FlagGlobal; } |
| 54 | bool ignoreCase() const { return m_flags & FlagIgnoreCase; } |
| 55 | bool multiline() const { return m_flags & FlagMultiline; } |
msaboff@apple.com | 3f19465 | 2016-03-09 20:11:46 +0000 | [diff] [blame] | 56 | bool sticky() const { return m_flags & FlagSticky; } |
fpizlo@apple.com | 280ef00 | 2016-04-05 22:13:16 +0000 | [diff] [blame] | 57 | bool globalOrSticky() const { return global() || sticky(); } |
msaboff@apple.com | 5e9b065 | 2016-03-02 00:39:01 +0000 | [diff] [blame] | 58 | bool unicode() const { return m_flags & FlagUnicode; } |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 59 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 60 | const String& pattern() const { return m_patternString; } |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 61 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 62 | bool isValid() const { return !m_constructionError && m_flags != InvalidFlags; } |
| 63 | const char* errorMessage() const { return m_constructionError; } |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 64 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 65 | JS_EXPORT_PRIVATE int match(VM&, const String&, unsigned startOffset, Vector<int>& ovector); |
fpizlo@apple.com | 280ef00 | 2016-04-05 22:13:16 +0000 | [diff] [blame] | 66 | |
| 67 | // Returns false if we couldn't run the regular expression for any reason. |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 68 | bool matchConcurrently(VM&, const String&, unsigned startOffset, int& position, Vector<int>& ovector); |
fpizlo@apple.com | 280ef00 | 2016-04-05 22:13:16 +0000 | [diff] [blame] | 69 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 70 | JS_EXPORT_PRIVATE MatchResult match(VM&, const String&, unsigned startOffset); |
fpizlo@apple.com | 6ea42db | 2016-03-08 21:15:07 +0000 | [diff] [blame] | 71 | |
fpizlo@apple.com | 280ef00 | 2016-04-05 22:13:16 +0000 | [diff] [blame] | 72 | bool matchConcurrently(VM&, const String&, unsigned startOffset, MatchResult&); |
| 73 | |
fpizlo@apple.com | 6ea42db | 2016-03-08 21:15:07 +0000 | [diff] [blame] | 74 | // Call these versions of the match functions if you're desperate for performance. |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 75 | template<typename VectorType> |
| 76 | int matchInline(VM&, const String&, unsigned startOffset, VectorType& ovector); |
fpizlo@apple.com | 6ea42db | 2016-03-08 21:15:07 +0000 | [diff] [blame] | 77 | MatchResult matchInline(VM&, const String&, unsigned startOffset); |
| 78 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 79 | unsigned numSubpatterns() const { return m_numSubpatterns; } |
rniwa@webkit.org | 7d76d9b | 2011-05-26 05:19:25 +0000 | [diff] [blame] | 80 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 81 | bool hasCode() |
| 82 | { |
| 83 | return m_state != NotCompiled; |
| 84 | } |
oliver@apple.com | d7523c1 | 2011-05-26 22:58:52 +0000 | [diff] [blame] | 85 | |
fpizlo@apple.com | 280ef00 | 2016-04-05 22:13:16 +0000 | [diff] [blame] | 86 | bool hasCodeFor(Yarr::YarrCharSize); |
| 87 | bool hasMatchOnlyCodeFor(Yarr::YarrCharSize); |
| 88 | |
ggaren@apple.com | 05627c5 | 2015-08-13 20:17:02 +0000 | [diff] [blame] | 89 | void deleteCode(); |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 90 | |
msaboff@apple.com | 02931f0 | 2010-09-10 02:10:37 +0000 | [diff] [blame] | 91 | #if ENABLE(REGEXP_TRACING) |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 92 | void printTraceData(); |
msaboff@apple.com | 02931f0 | 2010-09-10 02:10:37 +0000 | [diff] [blame] | 93 | #endif |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 94 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 95 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 96 | { |
| 97 | return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info()); |
| 98 | } |
oliver@apple.com | d7523c1 | 2011-05-26 22:58:52 +0000 | [diff] [blame] | 99 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 100 | DECLARE_INFO; |
oliver@apple.com | d7523c1 | 2011-05-26 22:58:52 +0000 | [diff] [blame] | 101 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 102 | RegExpKey key() { return RegExpKey(m_flags, m_patternString); } |
mhahnenberg@apple.com | b6f8519 | 2014-02-27 01:27:18 +0000 | [diff] [blame] | 103 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 104 | protected: |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 105 | void finishCreation(VM&); |
oliver@apple.com | fcacd3c | 2011-07-18 17:47:13 +0000 | [diff] [blame] | 106 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 107 | private: |
| 108 | friend class RegExpCache; |
| 109 | RegExp(VM&, const String&, RegExpFlags); |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 110 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 111 | static RegExp* createWithoutCaching(VM&, const String&, RegExpFlags); |
abecsi@webkit.org | 59e1c41 | 2010-12-02 13:36:45 +0000 | [diff] [blame] | 112 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 113 | enum RegExpState { |
| 114 | ParseError, |
| 115 | JITCode, |
| 116 | ByteCode, |
| 117 | NotCompiled |
| 118 | }; |
darin@apple.com | e4ba8cf | 2008-02-09 18:09:42 +0000 | [diff] [blame] | 119 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 120 | RegExpState m_state; |
| 121 | |
| 122 | void compile(VM*, Yarr::YarrCharSize); |
| 123 | void compileIfNecessary(VM&, Yarr::YarrCharSize); |
| 124 | |
| 125 | void compileMatchOnly(VM*, Yarr::YarrCharSize); |
| 126 | void compileIfNecessaryMatchOnly(VM&, Yarr::YarrCharSize); |
barraclough@apple.com | f280e17 | 2012-03-28 22:18:20 +0000 | [diff] [blame] | 127 | |
msaboff@apple.com | fcb0c9f | 2011-01-07 00:17:23 +0000 | [diff] [blame] | 128 | #if ENABLE(YARR_JIT_DEBUG) |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 129 | void matchCompareWithInterpreter(const String&, int startOffset, int* offsetVector, int jitResult); |
msaboff@apple.com | fcb0c9f | 2011-01-07 00:17:23 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 132 | String m_patternString; |
| 133 | RegExpFlags m_flags; |
| 134 | const char* m_constructionError; |
| 135 | unsigned m_numSubpatterns; |
msaboff@apple.com | 02931f0 | 2010-09-10 02:10:37 +0000 | [diff] [blame] | 136 | #if ENABLE(REGEXP_TRACING) |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 137 | double m_rtMatchOnlyTotalSubjectStringLen; |
| 138 | double m_rtMatchTotalSubjectStringLen; |
| 139 | unsigned m_rtMatchOnlyCallCount; |
| 140 | unsigned m_rtMatchOnlyFoundCount; |
| 141 | unsigned m_rtMatchCallCount; |
| 142 | unsigned m_rtMatchFoundCount; |
msaboff@apple.com | 02931f0 | 2010-09-10 02:10:37 +0000 | [diff] [blame] | 143 | #endif |
fpizlo@apple.com | 171d06f | 2016-11-15 23:21:50 +0000 | [diff] [blame] | 144 | ConcurrentJSLock m_lock; |
msaboff@apple.com | 02931f0 | 2010-09-10 02:10:37 +0000 | [diff] [blame] | 145 | |
barraclough@apple.com | f280e17 | 2012-03-28 22:18:20 +0000 | [diff] [blame] | 146 | #if ENABLE(YARR_JIT) |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 147 | Yarr::YarrCodeBlock m_regExpJITCode; |
barraclough@apple.com | f280e17 | 2012-03-28 22:18:20 +0000 | [diff] [blame] | 148 | #endif |
gyuyoung.kim@samsung.com | c6ae179 | 2014-11-28 00:51:32 +0000 | [diff] [blame] | 149 | std::unique_ptr<Yarr::BytecodePattern> m_regExpBytecode; |
mark.lam@apple.com | 188640e | 2014-09-04 19:10:36 +0000 | [diff] [blame] | 150 | }; |
ggaren@apple.com | 8a50ec5 | 2007-11-07 17:18:39 +0000 | [diff] [blame] | 151 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 152 | } // namespace JSC |