blob: 868a1934f9291398f82ba7da682c64d9ee4b748b [file] [log] [blame]
kocienda66a6d362001-08-24 14:24:45 +00001/*
kocienda66a6d362001-08-24 14:24:45 +00002 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
fpizlo@apple.com6ea42db2016-03-08 21:15:07 +00003 * Copyright (C) 2007, 2008, 2009, 2016 Apple Inc. All rights reserved.
mjs@apple.com0a30b7a2009-07-04 14:21:30 +00004 * Copyright (C) 2009 Torch Mobile, Inc.
kocienda66a6d362001-08-24 14:24:45 +00005 *
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
mjscdff33b2006-01-23 21:41:36 +000018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
mjs6f821c82002-03-22 00:31:57 +000019 *
kocienda66a6d362001-08-24 14:24:45 +000020 */
21
ryanhaddad@apple.com22104f52016-09-28 17:08:17 +000022#pragma once
kocienda66a6d362001-08-24 14:24:45 +000023
fpizlo@apple.com171d06f2016-11-15 23:21:50 +000024#include "ConcurrentJSLock.h"
barraclough@apple.comf280e172012-03-28 22:18:20 +000025#include "MatchResult.h"
barraclough@apple.com12812932011-03-09 23:04:27 +000026#include "RegExpKey.h"
barraclough@apple.comf280e172012-03-28 22:18:20 +000027#include "Structure.h"
msaboff@apple.com2cc41502011-09-12 22:17:53 +000028#include "yarr/Yarr.h"
darin@apple.come4ba8cf2008-02-09 18:09:42 +000029#include <wtf/Forward.h>
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +000030#include <wtf/text/WTFString.h>
darin@apple.come4ba8cf2008-02-09 18:09:42 +000031
barraclough@apple.comf280e172012-03-28 22:18:20 +000032#if ENABLE(YARR_JIT)
33#include "yarr/YarrJIT.h"
34#endif
35
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000036namespace JSC {
kocienda66a6d362001-08-24 14:24:45 +000037
mark.lam@apple.com188640e2014-09-04 19:10:36 +000038struct RegExpRepresentation;
39class VM;
darin@apple.com8c2bac02008-10-09 00:40:43 +000040
mark.lam@apple.com188640e2014-09-04 19:10:36 +000041JS_EXPORT_PRIVATE RegExpFlags regExpFlags(const String&);
barraclough@apple.com12812932011-03-09 23:04:27 +000042
akling@apple.com4b9e0002015-04-13 19:12:48 +000043class RegExp final : public JSCell {
mark.lam@apple.com188640e2014-09-04 19:10:36 +000044public:
45 typedef JSCell Base;
akling@apple.com4b9e0002015-04-13 19:12:48 +000046 static const unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
commit-queue@webkit.org6c25c522011-08-09 20:46:17 +000047
mark.lam@apple.com188640e2014-09-04 19:10:36 +000048 JS_EXPORT_PRIVATE static RegExp* create(VM&, const String& pattern, RegExpFlags);
49 static const bool needsDestruction = true;
mark.lam@apple.com188640e2014-09-04 19:10:36 +000050 static void destroy(JSCell*);
commit-queue@webkit.org00eb52f2016-03-01 02:07:12 +000051 static size_t estimatedSize(JSCell*);
darin@apple.come4ba8cf2008-02-09 18:09:42 +000052
mark.lam@apple.com188640e2014-09-04 19:10:36 +000053 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.com3f194652016-03-09 20:11:46 +000056 bool sticky() const { return m_flags & FlagSticky; }
fpizlo@apple.com280ef002016-04-05 22:13:16 +000057 bool globalOrSticky() const { return global() || sticky(); }
msaboff@apple.com5e9b0652016-03-02 00:39:01 +000058 bool unicode() const { return m_flags & FlagUnicode; }
darin@apple.come4ba8cf2008-02-09 18:09:42 +000059
mark.lam@apple.com188640e2014-09-04 19:10:36 +000060 const String& pattern() const { return m_patternString; }
darin@apple.come4ba8cf2008-02-09 18:09:42 +000061
mark.lam@apple.com188640e2014-09-04 19:10:36 +000062 bool isValid() const { return !m_constructionError && m_flags != InvalidFlags; }
63 const char* errorMessage() const { return m_constructionError; }
darin@apple.come4ba8cf2008-02-09 18:09:42 +000064
fpizlo@apple.combc16ddb2016-09-06 01:02:22 +000065 JS_EXPORT_PRIVATE int match(VM&, const String&, unsigned startOffset, Vector<int>& ovector);
fpizlo@apple.com280ef002016-04-05 22:13:16 +000066
67 // Returns false if we couldn't run the regular expression for any reason.
fpizlo@apple.combc16ddb2016-09-06 01:02:22 +000068 bool matchConcurrently(VM&, const String&, unsigned startOffset, int& position, Vector<int>& ovector);
fpizlo@apple.com280ef002016-04-05 22:13:16 +000069
mark.lam@apple.com188640e2014-09-04 19:10:36 +000070 JS_EXPORT_PRIVATE MatchResult match(VM&, const String&, unsigned startOffset);
fpizlo@apple.com6ea42db2016-03-08 21:15:07 +000071
fpizlo@apple.com280ef002016-04-05 22:13:16 +000072 bool matchConcurrently(VM&, const String&, unsigned startOffset, MatchResult&);
73
fpizlo@apple.com6ea42db2016-03-08 21:15:07 +000074 // Call these versions of the match functions if you're desperate for performance.
fpizlo@apple.combc16ddb2016-09-06 01:02:22 +000075 template<typename VectorType>
76 int matchInline(VM&, const String&, unsigned startOffset, VectorType& ovector);
fpizlo@apple.com6ea42db2016-03-08 21:15:07 +000077 MatchResult matchInline(VM&, const String&, unsigned startOffset);
78
mark.lam@apple.com188640e2014-09-04 19:10:36 +000079 unsigned numSubpatterns() const { return m_numSubpatterns; }
rniwa@webkit.org7d76d9b2011-05-26 05:19:25 +000080
mark.lam@apple.com188640e2014-09-04 19:10:36 +000081 bool hasCode()
82 {
83 return m_state != NotCompiled;
84 }
oliver@apple.comd7523c12011-05-26 22:58:52 +000085
fpizlo@apple.com280ef002016-04-05 22:13:16 +000086 bool hasCodeFor(Yarr::YarrCharSize);
87 bool hasMatchOnlyCodeFor(Yarr::YarrCharSize);
88
ggaren@apple.com05627c52015-08-13 20:17:02 +000089 void deleteCode();
mark.lam@apple.com188640e2014-09-04 19:10:36 +000090
msaboff@apple.com02931f02010-09-10 02:10:37 +000091#if ENABLE(REGEXP_TRACING)
mark.lam@apple.com188640e2014-09-04 19:10:36 +000092 void printTraceData();
msaboff@apple.com02931f02010-09-10 02:10:37 +000093#endif
darin@apple.come4ba8cf2008-02-09 18:09:42 +000094
mark.lam@apple.com188640e2014-09-04 19:10:36 +000095 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
96 {
97 return Structure::create(vm, globalObject, prototype, TypeInfo(CellType, StructureFlags), info());
98 }
oliver@apple.comd7523c12011-05-26 22:58:52 +000099
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000100 DECLARE_INFO;
oliver@apple.comd7523c12011-05-26 22:58:52 +0000101
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000102 RegExpKey key() { return RegExpKey(m_flags, m_patternString); }
mhahnenberg@apple.comb6f85192014-02-27 01:27:18 +0000103
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000104protected:
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000105 void finishCreation(VM&);
oliver@apple.comfcacd3c2011-07-18 17:47:13 +0000106
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000107private:
108 friend class RegExpCache;
109 RegExp(VM&, const String&, RegExpFlags);
darin@apple.come4ba8cf2008-02-09 18:09:42 +0000110
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000111 static RegExp* createWithoutCaching(VM&, const String&, RegExpFlags);
abecsi@webkit.org59e1c412010-12-02 13:36:45 +0000112
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000113 enum RegExpState {
114 ParseError,
115 JITCode,
116 ByteCode,
117 NotCompiled
118 };
darin@apple.come4ba8cf2008-02-09 18:09:42 +0000119
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000120 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.comf280e172012-03-28 22:18:20 +0000127
msaboff@apple.comfcb0c9f2011-01-07 00:17:23 +0000128#if ENABLE(YARR_JIT_DEBUG)
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000129 void matchCompareWithInterpreter(const String&, int startOffset, int* offsetVector, int jitResult);
msaboff@apple.comfcb0c9f2011-01-07 00:17:23 +0000130#endif
131
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000132 String m_patternString;
133 RegExpFlags m_flags;
134 const char* m_constructionError;
135 unsigned m_numSubpatterns;
msaboff@apple.com02931f02010-09-10 02:10:37 +0000136#if ENABLE(REGEXP_TRACING)
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000137 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.com02931f02010-09-10 02:10:37 +0000143#endif
fpizlo@apple.com171d06f2016-11-15 23:21:50 +0000144 ConcurrentJSLock m_lock;
msaboff@apple.com02931f02010-09-10 02:10:37 +0000145
barraclough@apple.comf280e172012-03-28 22:18:20 +0000146#if ENABLE(YARR_JIT)
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000147 Yarr::YarrCodeBlock m_regExpJITCode;
barraclough@apple.comf280e172012-03-28 22:18:20 +0000148#endif
gyuyoung.kim@samsung.comc6ae1792014-11-28 00:51:32 +0000149 std::unique_ptr<Yarr::BytecodePattern> m_regExpBytecode;
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000150};
ggaren@apple.com8a50ec52007-11-07 17:18:39 +0000151
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000152} // namespace JSC