Debian Codegraph @

src/3rdparty/javascriptcore/JavaScriptCore/runtime/RegExp.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 1999-2001, 2004 Harri Porten ([email protected])
3 * Copyright (c) 2007, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2009 Torch Mobile, Inc.
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
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "config.h"cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/config.h`/
23 #include "RegExp.h"cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/runtime/RegExp.h`/
24 #include "Lexer.h"cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/parser/Lexer.h`/
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <wtf/Assertions.h>cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h`/
29 #include <wtf/OwnArrayPtr.h>cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/wtf/OwnArrayPtr.h`/
30
31
32 #if ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR)
33
34 #include "yarr/RegexCompiler.h"cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/yarr/RegexCompiler.h`/
35 #if ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR_JIT)
36 #include "yarr/RegexJIT.h"cxx . . $ `<file>/src/3rdparty/javascriptcore/JavaScriptCore/yarr/RegexJIT.h`/
37 #else
38 #include "yarr/RegexInterpreter.h"
39 #endif
40
41 #else
42
43 #if ENABLE(WREC)
44 #include "JIT.h"
45 #include "WRECGenerator.h"
46 #endif
47 #include <pcre/pcre.h>
48
49 #endif
50
51 namespace JSCcxx . . $ `<built-in>:463:9`! {
52
53 #if ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(WREC)
54 using namespace WREC;
55 #endif
56
57 inline RegExpcxx . . $ QTJSC/RegExp#::RegExpcxx . . $ QTJSC/RegExp#RegExp(da2e3d018b4db96).(JSGlobalDatacxx . . $ QTJSC/JSGlobalData#* globalData, const UStringcxx . . $ QTJSC/UString#& pattern)
58 : m_pattern(pattern)
59 , m_flagBitscxx . . $ QTJSC/RegExp#m_flagBits.(0)
60 , m_constructionErrorcxx . . $ QTJSC/RegExp#m_constructionError.(0)
61 , m_numSubpatternscxx . . $ QTJSC/RegExp#m_numSubpatterns.(0)
62 {
63 compilecxx . . $ QTJSC/RegExp#compile(71c44650a83bfd83).(globalData);
64 }
65
66 inline RegExpcxx . . $ QTJSC/RegExp#::RegExpcxx . . $ QTJSC/RegExp#RegExp(61d25fb22c59c1f9).(JSGlobalDatacxx . . $ QTJSC/JSGlobalData#* globalData, const UStringcxx . . $ QTJSC/UString#& pattern, const UStringcxx . . $ QTJSC/UString#& flags)
67 : m_pattern(pattern)
68 , m_flagBitscxx . . $ QTJSC/RegExp#m_flagBits.(0)
69 , m_constructionErrorcxx . . $ QTJSC/RegExp#m_constructionError.(0)
70 , m_numSubpatternscxx . . $ QTJSC/RegExp#m_numSubpatterns.(0)
71 {
72 // NOTE: The global flag is handled on a case-by-case basis by functions like
73 // String::match and RegExpObject::match.
74 #ifndef QT_BUILD_SCRIPT_LIBcxx . . $ `<built-in>:481:9`!
75 if (flags.find('g') != -1)
76 m_flagBits |= Global;
77 if (flags.find('i') != -1)
78 m_flagBits |= IgnoreCase;
79 if (flags.find('m') != -1)
80 m_flagBits |= Multiline;
81 #else //Invalid flags should throw a SyntaxError (ECMA Script 15.10.4.1)
82 static const char flagError[] = "invalid regular expression flag";
83 for (int i = 0; i < flags.sizecxx . . $ QTJSC/UString#size(455f465bc33b4cdf).(); i++) {
84 switch (flags.datacxx . . $ QTJSC/UString#data(9865edf6270d80f8).()[i]) {
85 case 'g':
86 m_flagBitscxx . . $ QTJSC/RegExp#m_flagBits. |= Globalcxx . . $ QTJSC/RegExp#Global.;
87 break;
88 case 'i':
89 m_flagBitscxx . . $ QTJSC/RegExp#m_flagBits. |= IgnoreCasecxx . . $ QTJSC/RegExp#IgnoreCase.;
90 break;
91 case 'm':
92 m_flagBitscxx . . $ QTJSC/RegExp#m_flagBits. |= Multilinecxx . . $ QTJSC/RegExp#Multiline.;
93 break;
94 default:
95 m_constructionErrorcxx . . $ QTJSC/RegExp#m_constructionError. = flagError;
96 #if !ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR)
97 m_regExp = 0;
98 #endif
99 return;
100 }
101 }
102 #endif
103
104 compilecxx . . $ QTJSC/RegExp#compile(71c44650a83bfd83).(globalData);
105 }
106
107 #if !ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR)
108 RegExp::~RegExp()
109 {
110 jsRegExpFree(m_regExp);
111 }
112 #endif
113
114 PassRefPtrcxx . . $ PassRefPtr#<RegExpcxx . . $ QTJSC/RegExp#> RegExpcxx . . $ QTJSC/RegExp#::createcxx . . $ QTJSC/RegExp#create(7d204926e6cc58b9).(JSGlobalDatacxx . . $ QTJSC/JSGlobalData#* globalData, const UStringcxx . . $ QTJSC/UString#& pattern)
115 {
116 return adoptRefcxx . . $ adoptRef(e761da608bb8c29c).(new RegExpcxx . . $ QTJSC/RegExp#(globalData, pattern));
117 }
118
119 PassRefPtrcxx . . $ PassRefPtr#<RegExpcxx . . $ QTJSC/RegExp#> RegExpcxx . . $ QTJSC/RegExp#::createcxx . . $ QTJSC/RegExp#create(19bfe90a7859f628).(JSGlobalDatacxx . . $ QTJSC/JSGlobalData#* globalData, const UStringcxx . . $ QTJSC/UString#& pattern, const UStringcxx . . $ QTJSC/UString#& flags)
120 {
121 return adoptRefcxx . . $ adoptRef(e761da608bb8c29c).(new RegExpcxx . . $ QTJSC/RegExp#(globalData, pattern, flags));
122 }
123
124 #if ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR)
125
126 void RegExpcxx . . $ QTJSC/RegExp#::compilecxx . . $ QTJSC/RegExp#compile(71c44650a83bfd83).(JSGlobalDatacxx . . $ QTJSC/JSGlobalData#* globalData)
127 {
128 #if ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR_JIT)
129 Yarrcxx . . $ QTJSC/Yarr/::jitCompileRegexcxx . . $ QTJSC/Yarr/jitCompileRegex(eb7cf78519d4005a).(globalData, m_regExpJITCode, m_pattern, m_numSubpatternscxx . . $ QTJSC/RegExp#m_numSubpatterns., m_constructionErrorcxx . . $ QTJSC/RegExp#m_constructionError., ignoreCasecxx . . $ QTJSC/RegExp#ignoreCase(50ce9a9e25b4a850).(), multilinecxx . . $ QTJSC/RegExp#multiline(50ce9a9e25b4a850).());
130 #else
131 UNUSED_PARAM(globalData);
132 m_regExpBytecode.set(Yarr::byteCompileRegex(m_pattern, m_numSubpatterns, m_constructionError, ignoreCase(), multiline()));
133 #endif
134 }
135
136 int RegExpcxx . . $ QTJSC/RegExp#::matchcxx . . $ QTJSC/RegExp#match(5ecf98ebfa6a0164).(const UStringcxx . . $ QTJSC/UString#& s, int startOffset, Vectorcxx . . $ Vector#<int, 32>* ovector)
137 {
138 if (startOffset < 0)
139 startOffset = 0;
140 if (ovector)
141 ovector->clear();
142
143 if (startOffset > s.sizecxx . . $ QTJSC/UString#size(455f465bc33b4cdf).() || s.isNullcxx . . $ QTJSC/UString#isNull(50ce9a9e25b4a850).())
144 return -1;
145
146 #if ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR_JIT)
147 if (!!m_regExpJITCode) {
148 #else
149 if (m_regExpBytecode) {
150 #endif
151 int offsetVectorSize = (m_numSubpatternscxx . . $ QTJSC/RegExp#m_numSubpatterns. + 1) * 3; // FIXME: should be 2 - but adding temporary fallback to pcre.
152 int* offsetVector;
153 Vectorcxx . . $ Vector#<int, 32> nonReturnedOvector;
154 if (ovector) {
155 ovector->resize(offsetVectorSize);
156 offsetVector = ovector->data();
157 } else {
158 nonReturnedOvector.resize(offsetVectorSize);
159 offsetVector = nonReturnedOvector.data();
160 }
161
162 ASSERTcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Assertions.h:184:9`!(offsetVector);
163 for (int j = 0; j < offsetVectorSize; ++j)
164 offsetVector[j] = -1;
165
166
167 #if ENABLEcxx . . $ `src/3rdparty/javascriptcore/JavaScriptCore/wtf/Platform.h:57:9`!(YARR_JIT)
168 int result = Yarrcxx . . $ QTJSC/Yarr/::executeRegexcxx . . $ QTJSC/Yarr/executeRegex(82a7c76d7837da02).(m_regExpJITCode, s.datacxx . . $ QTJSC/UString#data(9865edf6270d80f8).(), startOffset, s.sizecxx . . $ QTJSC/UString#size(455f465bc33b4cdf).(), offsetVector, offsetVectorSize);
169 #else
170 int result = Yarr::interpretRegex(m_regExpBytecode.get(), s.data(), startOffset, s.size(), offsetVector);
171 #endif
172
173 if (result < 0) {
174 #ifndef NDEBUGcxx . . $ `<built-in>:472:9`!
175 // TODO: define up a symbol, rather than magic -1
176 if (result != -1)
177 fprintf(stderr, "jsRegExpExecute failed with result %d\n", result);
178 #endif
179 if (ovector)
180 ovector->clear();
181 }
182 return result;
183 }
184
185 return -1;
186 }
187
188 #else
189
190 void RegExp::compile(JSGlobalData* globalData)
191 {
192 m_regExp = 0;
193 #if ENABLE(WREC)
194 m_wrecFunction = Generator::compileRegExp(globalData, m_pattern, &m_numSubpatterns, &m_constructionError, m_executablePool, ignoreCase(), multiline());
195 if (m_wrecFunction || m_constructionError)
196 return;
197 // Fall through to non-WREC case.
198 #else
199 UNUSED_PARAM(globalData);
200 #endif
201
202 JSRegExpIgnoreCaseOption ignoreCaseOption = ignoreCase() ? JSRegExpIgnoreCase : JSRegExpDoNotIgnoreCase;
203 JSRegExpMultilineOption multilineOption = multiline() ? JSRegExpMultiline : JSRegExpSingleLine;
204 m_regExp = jsRegExpCompile(reinterpret_cast<const UChar*>(m_pattern.data()), m_pattern.size(), ignoreCaseOption, multilineOption, &m_numSubpatterns, &m_constructionError);
205 }
206
207 int RegExp::match(const UString& s, int startOffset, Vector<int, 32>* ovector)
208 {
209 if (startOffset < 0)
210 startOffset = 0;
211 if (ovector)
212 ovector->clear();
213
214 if (static_cast<unsigned>(startOffset) > s.size() || s.isNull())
215 return -1;
216
217 #if ENABLE(WREC)
218 if (m_wrecFunction) {
219 int offsetVectorSize = (m_numSubpatterns + 1) * 2;
220 int* offsetVector;
221 Vector<int, 32> nonReturnedOvector;
222 if (ovector) {
223 ovector->resize(offsetVectorSize);
224 offsetVector = ovector->data();
225 } else {
226 nonReturnedOvector.resize(offsetVectorSize);
227 offsetVector = nonReturnedOvector.data();
228 }
229 ASSERT(offsetVector);
230 for (int j = 0; j < offsetVectorSize; ++j)
231 offsetVector[j] = -1;
232
233 int result = m_wrecFunction(s.data(), startOffset, s.size(), offsetVector);
234
235 if (result < 0) {
236 #ifndef NDEBUG
237 // TODO: define up a symbol, rather than magic -1
238 if (result != -1)
239 fprintf(stderr, "jsRegExpExecute failed with result %d\n", result);
240 #endif
241 if (ovector)
242 ovector->clear();
243 }
244 return result;
245 } else
246 #endif
247 if (m_regExp) {
248 // Set up the offset vector for the result.
249 // First 2/3 used for result, the last third used by PCRE.
250 int* offsetVector;
251 int offsetVectorSize;
252 int fixedSizeOffsetVector[3];
253 if (!ovector) {
254 offsetVectorSize = 3;
255 offsetVector = fixedSizeOffsetVector;
256 } else {
257 offsetVectorSize = (m_numSubpatterns + 1) * 3;
258 ovector->resize(offsetVectorSize);
259 offsetVector = ovector->data();
260 }
261
262 int numMatches = jsRegExpExecute(m_regExp, reinterpret_cast<const UChar*>(s.data()), s.size(), startOffset, offsetVector, offsetVectorSize);
263
264 if (numMatches < 0) {
265 #ifndef NDEBUG
266 if (numMatches != JSRegExpErrorNoMatch)
267 fprintf(stderr, "jsRegExpExecute failed with result %d\n", numMatches);
268 #endif
269 if (ovector)
270 ovector->clear();
271 return -1;
272 }
273
274 return offsetVector[0];
275 }
276
277 return -1;
278 }
279
280 #endif
281
282 } // namespace JSC