1 | /*
|
---|
2 | * Copyright (C) 2011 Apple Inc. All rights reserved.
|
---|
3 | *
|
---|
4 | * This library is free software; you can redistribute it and/or
|
---|
5 | * modify it under the terms of the GNU Library General Public
|
---|
6 | * License as published by the Free Software Foundation; either
|
---|
7 | * version 2 of the License, or (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This library is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
12 | * Library General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU Library General Public License
|
---|
15 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
17 | * Boston, MA 02110-1301, USA.
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "config.h"
|
---|
22 | #include "RegExp.h"
|
---|
23 |
|
---|
24 | #include <wtf/CurrentTime.h>
|
---|
25 | #include "InitializeThreading.h"
|
---|
26 | #include "JSGlobalObject.h"
|
---|
27 | #include <errno.h>
|
---|
28 | #include <stdio.h>
|
---|
29 | #include <stdlib.h>
|
---|
30 | #include <string.h>
|
---|
31 | #include <wtf/text/StringBuilder.h>
|
---|
32 |
|
---|
33 | #if !OS(WINDOWS)
|
---|
34 | #include <unistd.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #if HAVE(SYS_TIME_H)
|
---|
38 | #include <sys/time.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #if COMPILER(MSVC) && !OS(WINCE)
|
---|
42 | #include <crtdbg.h>
|
---|
43 | #include <mmsystem.h>
|
---|
44 | #include <windows.h>
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | #if PLATFORM(QT)
|
---|
48 | #include <QCoreApplication>
|
---|
49 | #include <QDateTime>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | const int MaxLineLength = 100 * 1024;
|
---|
53 |
|
---|
54 | using namespace JSC;
|
---|
55 | using namespace WTF;
|
---|
56 |
|
---|
57 | struct CommandLine {
|
---|
58 | CommandLine()
|
---|
59 | : interactive(false)
|
---|
60 | , verbose(false)
|
---|
61 | {
|
---|
62 | }
|
---|
63 |
|
---|
64 | bool interactive;
|
---|
65 | bool verbose;
|
---|
66 | Vector<String> arguments;
|
---|
67 | Vector<String> files;
|
---|
68 | };
|
---|
69 |
|
---|
70 | class StopWatch {
|
---|
71 | public:
|
---|
72 | void start();
|
---|
73 | void stop();
|
---|
74 | long getElapsedMS(); // call stop() first
|
---|
75 |
|
---|
76 | private:
|
---|
77 | double m_startTime;
|
---|
78 | double m_stopTime;
|
---|
79 | };
|
---|
80 |
|
---|
81 | void StopWatch::start()
|
---|
82 | {
|
---|
83 | m_startTime = currentTime();
|
---|
84 | }
|
---|
85 |
|
---|
86 | void StopWatch::stop()
|
---|
87 | {
|
---|
88 | m_stopTime = currentTime();
|
---|
89 | }
|
---|
90 |
|
---|
91 | long StopWatch::getElapsedMS()
|
---|
92 | {
|
---|
93 | return static_cast<long>((m_stopTime - m_startTime) * 1000);
|
---|
94 | }
|
---|
95 |
|
---|
96 | struct RegExpTest {
|
---|
97 | RegExpTest()
|
---|
98 | : offset(0)
|
---|
99 | , result(0)
|
---|
100 | {
|
---|
101 | }
|
---|
102 |
|
---|
103 | String subject;
|
---|
104 | int offset;
|
---|
105 | int result;
|
---|
106 | Vector<int, 32> expectVector;
|
---|
107 | };
|
---|
108 |
|
---|
109 | class GlobalObject : public JSGlobalObject {
|
---|
110 | private:
|
---|
111 | GlobalObject(JSGlobalData&, Structure*, const Vector<String>& arguments);
|
---|
112 |
|
---|
113 | public:
|
---|
114 | typedef JSGlobalObject Base;
|
---|
115 |
|
---|
116 | static GlobalObject* create(JSGlobalData& globalData, Structure* structure, const Vector<String>& arguments)
|
---|
117 | {
|
---|
118 | GlobalObject* globalObject = new (NotNull, allocateCell<GlobalObject>(globalData.heap)) GlobalObject(globalData, structure, arguments);
|
---|
119 | globalData.heap.addFinalizer(globalObject, destroy);
|
---|
120 | return globalObject;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static const ClassInfo s_info;
|
---|
124 |
|
---|
125 | static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
|
---|
126 | {
|
---|
127 | return Structure::create(globalData, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), &s_info);
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected:
|
---|
131 | void finishCreation(JSGlobalData& globalData, const Vector<String>& arguments)
|
---|
132 | {
|
---|
133 | Base::finishCreation(globalData);
|
---|
134 | UNUSED_PARAM(arguments);
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 | namespace JSC {
|
---|
139 | NEEDS_DESTRUCTOR(GlobalObject, false);
|
---|
140 | }
|
---|
141 |
|
---|
142 | COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
|
---|
143 | ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
|
---|
144 |
|
---|
145 | const ClassInfo GlobalObject::s_info = { "global", &JSGlobalObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(GlobalObject) };
|
---|
146 |
|
---|
147 | GlobalObject::GlobalObject(JSGlobalData& globalData, Structure* structure, const Vector<String>& arguments)
|
---|
148 | : JSGlobalObject(globalData, structure)
|
---|
149 | {
|
---|
150 | finishCreation(globalData, arguments);
|
---|
151 | }
|
---|
152 |
|
---|
153 | // Use SEH for Release builds only to get rid of the crash report dialog
|
---|
154 | // (luckily the same tests fail in Release and Debug builds so far). Need to
|
---|
155 | // be in a separate main function because the realMain function requires object
|
---|
156 | // unwinding.
|
---|
157 |
|
---|
158 | #if COMPILER(MSVC) && !COMPILER(INTEL) && !defined(_DEBUG) && !OS(WINCE)
|
---|
159 | #define TRY __try {
|
---|
160 | #define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
|
---|
161 | #else
|
---|
162 | #define TRY
|
---|
163 | #define EXCEPT(x)
|
---|
164 | #endif
|
---|
165 |
|
---|
166 | int realMain(int argc, char** argv);
|
---|
167 |
|
---|
168 | int main(int argc, char** argv)
|
---|
169 | {
|
---|
170 | #if OS(WINDOWS)
|
---|
171 | #if !OS(WINCE)
|
---|
172 | // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
|
---|
173 | // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
|
---|
174 | // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
|
---|
175 | ::SetErrorMode(0);
|
---|
176 | #endif
|
---|
177 |
|
---|
178 | #if defined(_DEBUG)
|
---|
179 | _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
|
---|
180 | _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
|
---|
181 | _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
|
---|
182 | _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
|
---|
183 | _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
|
---|
184 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | timeBeginPeriod(1);
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | #if PLATFORM(QT)
|
---|
191 | QCoreApplication app(argc, argv);
|
---|
192 | #endif
|
---|
193 |
|
---|
194 | // Initialize JSC before getting JSGlobalData.
|
---|
195 | JSC::initializeThreading();
|
---|
196 |
|
---|
197 | // We can't use destructors in the following code because it uses Windows
|
---|
198 | // Structured Exception Handling
|
---|
199 | int res = 0;
|
---|
200 | TRY
|
---|
201 | res = realMain(argc, argv);
|
---|
202 | EXCEPT(res = 3)
|
---|
203 | return res;
|
---|
204 | }
|
---|
205 |
|
---|
206 | static bool testOneRegExp(JSGlobalData& globalData, RegExp* regexp, RegExpTest* regExpTest, bool verbose, unsigned int lineNumber)
|
---|
207 | {
|
---|
208 | bool result = true;
|
---|
209 | Vector<int, 32> outVector;
|
---|
210 | outVector.resize(regExpTest->expectVector.size());
|
---|
211 | int matchResult = regexp->match(globalData, regExpTest->subject, regExpTest->offset, outVector);
|
---|
212 |
|
---|
213 | if (matchResult != regExpTest->result) {
|
---|
214 | result = false;
|
---|
215 | if (verbose)
|
---|
216 | printf("Line %d: results mismatch - expected %d got %d\n", lineNumber, regExpTest->result, matchResult);
|
---|
217 | } else if (matchResult != -1) {
|
---|
218 | if (outVector.size() != regExpTest->expectVector.size()) {
|
---|
219 | result = false;
|
---|
220 | if (verbose)
|
---|
221 | printf("Line %d: output vector size mismatch - expected %lu got %lu\n", lineNumber, regExpTest->expectVector.size(), outVector.size());
|
---|
222 | } else if (outVector.size() % 2) {
|
---|
223 | result = false;
|
---|
224 | if (verbose)
|
---|
225 | printf("Line %d: output vector size is odd (%lu), should be even\n", lineNumber, outVector.size());
|
---|
226 | } else {
|
---|
227 | // Check in pairs since the first value of the pair could be -1 in which case the second doesn't matter.
|
---|
228 | size_t pairCount = outVector.size() / 2;
|
---|
229 | for (size_t i = 0; i < pairCount; ++i) {
|
---|
230 | size_t startIndex = i*2;
|
---|
231 | if (outVector[startIndex] != regExpTest->expectVector[startIndex]) {
|
---|
232 | result = false;
|
---|
233 | if (verbose)
|
---|
234 | printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex, regExpTest->expectVector[startIndex], outVector[startIndex]);
|
---|
235 | }
|
---|
236 | if ((i > 0) && (regExpTest->expectVector[startIndex] != -1) && (outVector[startIndex+1] != regExpTest->expectVector[startIndex+1])) {
|
---|
237 | result = false;
|
---|
238 | if (verbose)
|
---|
239 | printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex+1, regExpTest->expectVector[startIndex+1], outVector[startIndex+1]);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | return result;
|
---|
246 | }
|
---|
247 |
|
---|
248 | static int scanString(char* buffer, int bufferLength, StringBuilder& builder, char termChar)
|
---|
249 | {
|
---|
250 | bool escape = false;
|
---|
251 |
|
---|
252 | for (int i = 0; i < bufferLength; ++i) {
|
---|
253 | UChar c = buffer[i];
|
---|
254 |
|
---|
255 | if (escape) {
|
---|
256 | switch (c) {
|
---|
257 | case '0':
|
---|
258 | c = '\0';
|
---|
259 | break;
|
---|
260 | case 'a':
|
---|
261 | c = '\a';
|
---|
262 | break;
|
---|
263 | case 'b':
|
---|
264 | c = '\b';
|
---|
265 | break;
|
---|
266 | case 'f':
|
---|
267 | c = '\f';
|
---|
268 | break;
|
---|
269 | case 'n':
|
---|
270 | c = '\n';
|
---|
271 | break;
|
---|
272 | case 'r':
|
---|
273 | c = '\r';
|
---|
274 | break;
|
---|
275 | case 't':
|
---|
276 | c = '\t';
|
---|
277 | break;
|
---|
278 | case 'v':
|
---|
279 | c = '\v';
|
---|
280 | break;
|
---|
281 | case '\\':
|
---|
282 | c = '\\';
|
---|
283 | break;
|
---|
284 | case '?':
|
---|
285 | c = '\?';
|
---|
286 | break;
|
---|
287 | case 'u':
|
---|
288 | if ((i + 4) >= bufferLength)
|
---|
289 | return -1;
|
---|
290 | unsigned int charValue;
|
---|
291 | if (sscanf(buffer+i+1, "%04x", &charValue) != 1)
|
---|
292 | return -1;
|
---|
293 | c = static_cast<UChar>(charValue);
|
---|
294 | i += 4;
|
---|
295 | break;
|
---|
296 | }
|
---|
297 |
|
---|
298 | builder.append(c);
|
---|
299 | escape = false;
|
---|
300 | } else {
|
---|
301 | if (c == termChar)
|
---|
302 | return i;
|
---|
303 |
|
---|
304 | if (c == '\\')
|
---|
305 | escape = true;
|
---|
306 | else
|
---|
307 | builder.append(c);
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | return -1;
|
---|
312 | }
|
---|
313 |
|
---|
314 | static RegExp* parseRegExpLine(JSGlobalData& globalData, char* line, int lineLength)
|
---|
315 | {
|
---|
316 | StringBuilder pattern;
|
---|
317 |
|
---|
318 | if (line[0] != '/')
|
---|
319 | return 0;
|
---|
320 |
|
---|
321 | int i = scanString(line + 1, lineLength - 1, pattern, '/') + 1;
|
---|
322 |
|
---|
323 | if ((i >= lineLength) || (line[i] != '/'))
|
---|
324 | return 0;
|
---|
325 |
|
---|
326 | ++i;
|
---|
327 |
|
---|
328 | return RegExp::create(globalData, pattern.toString(), regExpFlags(line + i));
|
---|
329 | }
|
---|
330 |
|
---|
331 | static RegExpTest* parseTestLine(char* line, int lineLength)
|
---|
332 | {
|
---|
333 | StringBuilder subjectString;
|
---|
334 |
|
---|
335 | if ((line[0] != ' ') || (line[1] != '"'))
|
---|
336 | return 0;
|
---|
337 |
|
---|
338 | int i = scanString(line + 2, lineLength - 2, subjectString, '"') + 2;
|
---|
339 |
|
---|
340 | if ((i >= (lineLength - 2)) || (line[i] != '"') || (line[i+1] != ',') || (line[i+2] != ' '))
|
---|
341 | return 0;
|
---|
342 |
|
---|
343 | i += 3;
|
---|
344 |
|
---|
345 | int offset;
|
---|
346 |
|
---|
347 | if (sscanf(line + i, "%d, ", &offset) != 1)
|
---|
348 | return 0;
|
---|
349 |
|
---|
350 | while (line[i] && line[i] != ' ')
|
---|
351 | ++i;
|
---|
352 |
|
---|
353 | ++i;
|
---|
354 |
|
---|
355 | int matchResult;
|
---|
356 |
|
---|
357 | if (sscanf(line + i, "%d, ", &matchResult) != 1)
|
---|
358 | return 0;
|
---|
359 |
|
---|
360 | while (line[i] && line[i] != ' ')
|
---|
361 | ++i;
|
---|
362 |
|
---|
363 | ++i;
|
---|
364 |
|
---|
365 | if (line[i++] != '(')
|
---|
366 | return 0;
|
---|
367 |
|
---|
368 | int start, end;
|
---|
369 |
|
---|
370 | RegExpTest* result = new RegExpTest();
|
---|
371 |
|
---|
372 | result->subject = subjectString.toString();
|
---|
373 | result->offset = offset;
|
---|
374 | result->result = matchResult;
|
---|
375 |
|
---|
376 | while (line[i] && line[i] != ')') {
|
---|
377 | if (sscanf(line + i, "%d, %d", &start, &end) != 2) {
|
---|
378 | delete result;
|
---|
379 | return 0;
|
---|
380 | }
|
---|
381 |
|
---|
382 | result->expectVector.append(start);
|
---|
383 | result->expectVector.append(end);
|
---|
384 |
|
---|
385 | while (line[i] && (line[i] != ',') && (line[i] != ')'))
|
---|
386 | i++;
|
---|
387 | i++;
|
---|
388 | while (line[i] && (line[i] != ',') && (line[i] != ')'))
|
---|
389 | i++;
|
---|
390 |
|
---|
391 | if (line[i] == ')')
|
---|
392 | break;
|
---|
393 | if (!line[i] || (line[i] != ',')) {
|
---|
394 | delete result;
|
---|
395 | return 0;
|
---|
396 | }
|
---|
397 | i++;
|
---|
398 | }
|
---|
399 |
|
---|
400 | return result;
|
---|
401 | }
|
---|
402 |
|
---|
403 | static bool runFromFiles(GlobalObject* globalObject, const Vector<String>& files, bool verbose)
|
---|
404 | {
|
---|
405 | String script;
|
---|
406 | String fileName;
|
---|
407 | Vector<char> scriptBuffer;
|
---|
408 | unsigned tests = 0;
|
---|
409 | unsigned failures = 0;
|
---|
410 | char* lineBuffer = new char[MaxLineLength + 1];
|
---|
411 |
|
---|
412 | JSGlobalData& globalData = globalObject->globalData();
|
---|
413 |
|
---|
414 | bool success = true;
|
---|
415 | for (size_t i = 0; i < files.size(); i++) {
|
---|
416 | FILE* testCasesFile = fopen(files[i].utf8().data(), "rb");
|
---|
417 |
|
---|
418 | if (!testCasesFile) {
|
---|
419 | printf("Unable to open test data file \"%s\"\n", files[i].utf8().data());
|
---|
420 | continue;
|
---|
421 | }
|
---|
422 |
|
---|
423 | RegExp* regexp = 0;
|
---|
424 | size_t lineLength = 0;
|
---|
425 | char* linePtr = 0;
|
---|
426 | unsigned int lineNumber = 0;
|
---|
427 |
|
---|
428 | while ((linePtr = fgets(&lineBuffer[0], MaxLineLength, testCasesFile))) {
|
---|
429 | lineLength = strlen(linePtr);
|
---|
430 | if (linePtr[lineLength - 1] == '\n') {
|
---|
431 | linePtr[lineLength - 1] = '\0';
|
---|
432 | --lineLength;
|
---|
433 | }
|
---|
434 | ++lineNumber;
|
---|
435 |
|
---|
436 | if (linePtr[0] == '#')
|
---|
437 | continue;
|
---|
438 |
|
---|
439 | if (linePtr[0] == '/') {
|
---|
440 | regexp = parseRegExpLine(globalData, linePtr, lineLength);
|
---|
441 | } else if (linePtr[0] == ' ') {
|
---|
442 | RegExpTest* regExpTest = parseTestLine(linePtr, lineLength);
|
---|
443 |
|
---|
444 | if (regexp && regExpTest) {
|
---|
445 | ++tests;
|
---|
446 | if (!testOneRegExp(globalData, regexp, regExpTest, verbose, lineNumber)) {
|
---|
447 | failures++;
|
---|
448 | printf("Failure on line %u\n", lineNumber);
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | if (regExpTest)
|
---|
453 | delete regExpTest;
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 | fclose(testCasesFile);
|
---|
458 | }
|
---|
459 |
|
---|
460 | if (failures)
|
---|
461 | printf("%u tests run, %u failures\n", tests, failures);
|
---|
462 | else
|
---|
463 | printf("%u tests passed\n", tests);
|
---|
464 |
|
---|
465 | delete[] lineBuffer;
|
---|
466 |
|
---|
467 | globalData.dumpSampleData(globalObject->globalExec());
|
---|
468 | #if ENABLE(REGEXP_TRACING)
|
---|
469 | globalData.dumpRegExpTrace();
|
---|
470 | #endif
|
---|
471 | return success;
|
---|
472 | }
|
---|
473 |
|
---|
474 | #define RUNNING_FROM_XCODE 0
|
---|
475 |
|
---|
476 | static NO_RETURN void printUsageStatement(bool help = false)
|
---|
477 | {
|
---|
478 | fprintf(stderr, "Usage: regexp_test [options] file\n");
|
---|
479 | fprintf(stderr, " -h|--help Prints this help message\n");
|
---|
480 | fprintf(stderr, " -v|--verbose Verbose output\n");
|
---|
481 |
|
---|
482 | exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
|
---|
483 | }
|
---|
484 |
|
---|
485 | static void parseArguments(int argc, char** argv, CommandLine& options)
|
---|
486 | {
|
---|
487 | int i = 1;
|
---|
488 | for (; i < argc; ++i) {
|
---|
489 | const char* arg = argv[i];
|
---|
490 | if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
|
---|
491 | printUsageStatement(true);
|
---|
492 | if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose"))
|
---|
493 | options.verbose = true;
|
---|
494 | else
|
---|
495 | options.files.append(argv[i]);
|
---|
496 | }
|
---|
497 |
|
---|
498 | for (; i < argc; ++i)
|
---|
499 | options.arguments.append(argv[i]);
|
---|
500 | }
|
---|
501 |
|
---|
502 | int realMain(int argc, char** argv)
|
---|
503 | {
|
---|
504 | RefPtr<JSGlobalData> globalData = JSGlobalData::create(ThreadStackTypeLarge, LargeHeap);
|
---|
505 | JSLockHolder lock(globalData.get());
|
---|
506 |
|
---|
507 | CommandLine options;
|
---|
508 | parseArguments(argc, argv, options);
|
---|
509 |
|
---|
510 | GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);
|
---|
511 | bool success = runFromFiles(globalObject, options.files, options.verbose);
|
---|
512 |
|
---|
513 | return success ? 0 : 3;
|
---|
514 | }
|
---|