source: webkit/trunk/JavaScriptCore/kjs/testkjs.cpp@ 14122

Last change on this file since 14122 was 14122, checked in by sfalken, 19 years ago

2006-04-28 Steve Falkenburg <[email protected]>

Reviewed by kdecker

Suppress error reporting dialog that blocks Javascript tests from completing.


Real error is due to an overflow in the date/time handling functions that needs
to be addressed, but this will prevent the hang running the Javascript tests
on the build bot (along with the related changes).


  • kjs/testkjs.cpp: (main): Suppress C runtime alerts
  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten ([email protected])
5 * Copyright (C) 2004-2006 Apple Computer, Inc.
6 * Copyright (C) 2006 Björn Graf ([email protected])
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#include "config.h"
26#include "collector.h"
27
28#include "HashTraits.h"
29#include "JSLock.h"
30#include "object.h"
31
32#include <math.h>
33#include <stdio.h>
34#include <string.h>
35#if HAVE(SYS_TIME_H)
36#include <sys/time.h>
37#endif
38
39#include "protect.h"
40
41#if PLATFORM(WIN_OS)
42#include <windows.h>
43#include <crtdbg.h>
44#endif
45
46using namespace KJS;
47using namespace KXMLCore;
48
49static void testIsInteger();
50static char* createStringWithContentsOfFile(const char* fileName);
51
52class StopWatch
53{
54public:
55 void start();
56 void stop();
57 long getElapsedMS(); // call stop() first
58
59private:
60#if PLATFORM(WIN_OS)
61 DWORD m_startTime;
62 DWORD m_stopTime;
63#else
64 // Windows does not have timeval, disabling this class for now (bug 7399)
65 timeval m_startTime;
66 timeval m_stopTime;
67#endif
68};
69
70void StopWatch::start()
71{
72#if PLATFORM(WIN_OS)
73 m_startTime = timeGetTime();
74#else
75 gettimeofday(&m_startTime, 0);
76#endif
77}
78
79void StopWatch::stop()
80{
81#if PLATFORM(WIN_OS)
82 m_stopTime = timeGetTime();
83#else
84 gettimeofday(&m_stopTime, 0);
85#endif
86}
87
88long StopWatch::getElapsedMS()
89{
90#if PLATFORM(WIN_OS)
91 return m_stopTime - m_startTime;
92#else
93 timeval elapsedTime;
94 timersub(&m_stopTime, &m_startTime, &elapsedTime);
95
96 return elapsedTime.tv_sec * 1000 + lroundf(elapsedTime.tv_usec / 1000.0);
97#endif
98}
99
100class GlobalImp : public JSObject {
101public:
102 virtual UString className() const { return "global"; }
103};
104
105class TestFunctionImp : public JSObject {
106public:
107 TestFunctionImp(int i, int length);
108 virtual bool implementsCall() const { return true; }
109 virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const List &args);
110
111 enum { Print, Debug, Quit, GC, Version, Run };
112
113private:
114 int id;
115};
116
117TestFunctionImp::TestFunctionImp(int i, int length) : JSObject(), id(i)
118{
119 putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
120}
121
122JSValue* TestFunctionImp::callAsFunction(ExecState* exec, JSObject*, const List &args)
123{
124 switch (id) {
125 case Print:
126 printf("--> %s\n", args[0]->toString(exec).UTF8String().c_str());
127 return jsUndefined();
128 case Debug:
129 fprintf(stderr, "--> %s\n", args[0]->toString(exec).UTF8String().c_str());
130 return jsUndefined();
131 case GC:
132 {
133 JSLock lock;
134 Interpreter::collect();
135 return jsUndefined();
136 }
137 case Version:
138 // We need this function for compatibility with the Mozilla JS tests but for now
139 // we don't actually do any version-specific handling
140 return jsUndefined();
141 case Run:
142 {
143 StopWatch stopWatch;
144 char* fileName = strdup(args[0]->toString(exec).UTF8String().c_str());
145 char* script = createStringWithContentsOfFile(fileName);
146 if (!script)
147 return throwError(exec, GeneralError, "Could not open file.");
148
149 stopWatch.start();
150 exec->dynamicInterpreter()->evaluate(fileName, 0, script);
151 stopWatch.stop();
152
153 free(script);
154 free(fileName);
155
156 return jsNumber(stopWatch.getElapsedMS());
157 }
158 case Quit:
159 exit(0);
160 default:
161 abort();
162 }
163 return 0;
164}
165
166#if PLATFORM(WIN_OS)
167
168// Use SEH for Release builds only to get rid of the crash report dialog
169// (luckyly the same tests fail in Release and Debug builds so far). Need to
170// be in a separate main function because the kjsmain function requires object
171// unwinding.
172
173#if defined(_DEBUG)
174#define TRY
175#define EXCEPT(x)
176#else
177#define TRY __try {
178#define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
179#endif
180
181#else
182
183#define TRY
184#define EXCEPT(x)
185
186#endif
187
188int kjsmain(int argc, char** argv);
189
190int main(int argc, char** argv)
191{
192#if 0
193#if defined(_DEBUG) && PLATFORM(WIN_OS)
194 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
195 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
196 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
197 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
198 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
199 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
200#endif
201#endif
202
203 int res = 0;
204 TRY
205 res = kjsmain(argc, argv);
206 EXCEPT(res = 3)
207 return res;
208}
209
210
211bool doIt(int argc, char** argv)
212{
213 bool success = true;
214 GlobalImp* global = new GlobalImp();
215
216 // create interpreter
217 Interpreter interp(global);
218 // add debug() function
219 global->put(interp.globalExec(), "debug", new TestFunctionImp(TestFunctionImp::Debug, 1));
220 // add "print" for compatibility with the mozilla js shell
221 global->put(interp.globalExec(), "print", new TestFunctionImp(TestFunctionImp::Print, 1));
222 // add "quit" for compatibility with the mozilla js shell
223 global->put(interp.globalExec(), "quit", new TestFunctionImp(TestFunctionImp::Quit, 0));
224 // add "gc" for compatibility with the mozilla js shell
225 global->put(interp.globalExec(), "gc", new TestFunctionImp(TestFunctionImp::GC, 0));
226 // add "version" for compatibility with the mozilla js shell
227 global->put(interp.globalExec(), "version", new TestFunctionImp(TestFunctionImp::Version, 1));
228 global->put(interp.globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1));
229
230 Interpreter::setShouldPrintExceptions(true);
231
232 for (int i = 1; i < argc; i++) {
233 const char* fileName = argv[i];
234 if (strcmp(fileName, "-f") == 0) // mozilla test driver script uses "-f" prefix for files
235 continue;
236
237 char* script = createStringWithContentsOfFile(fileName);
238 if (!script) {
239 success = false;
240 break; // fail early so we can catch missing files
241 }
242
243 Completion completion = interp.evaluate(fileName, 0, script);
244 success = success && completion.complType() != Throw;
245 free(script);
246 }
247
248 return success;
249}
250
251
252int kjsmain(int argc, char** argv)
253{
254 if (argc < 2) {
255 fprintf(stderr, "Usage: testkjs file1 [file2...]\n");
256 return -1;
257 }
258
259 testIsInteger();
260
261 JSLock lock;
262
263 bool success = doIt(argc, argv);
264
265#ifndef NDEBUG
266 Collector::collect();
267#endif
268
269 if (success)
270 fprintf(stderr, "OK.\n");
271
272#ifdef KJS_DEBUG_MEM
273 Interpreter::finalCheck();
274#endif
275 return success ? 0 : 3;
276}
277
278static void testIsInteger()
279{
280 // Unit tests for KXMLCore::IsInteger. Don't have a better place for them now.
281 // FIXME: move these once we create a unit test directory for KXMLCore.
282
283 assert(IsInteger<bool>::value);
284 assert(IsInteger<char>::value);
285 assert(IsInteger<signed char>::value);
286 assert(IsInteger<unsigned char>::value);
287 assert(IsInteger<short>::value);
288 assert(IsInteger<unsigned short>::value);
289 assert(IsInteger<int>::value);
290 assert(IsInteger<unsigned int>::value);
291 assert(IsInteger<long>::value);
292 assert(IsInteger<unsigned long>::value);
293 assert(IsInteger<long long>::value);
294 assert(IsInteger<unsigned long long>::value);
295
296 assert(!IsInteger<char*>::value);
297 assert(!IsInteger<const char* >::value);
298 assert(!IsInteger<volatile char* >::value);
299 assert(!IsInteger<double>::value);
300 assert(!IsInteger<float>::value);
301 assert(!IsInteger<GlobalImp>::value);
302}
303
304static char* createStringWithContentsOfFile(const char* fileName)
305{
306 char* buffer;
307
308 int buffer_size = 0;
309 int buffer_capacity = 1024;
310 buffer = (char*)malloc(buffer_capacity);
311
312 FILE* f = fopen(fileName, "r");
313 if (!f) {
314 fprintf(stderr, "Could not open file: %s\n", fileName);
315 return 0;
316 }
317
318 while (!feof(f) && !ferror(f)) {
319 buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
320 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
321 buffer_capacity *= 2;
322 buffer = (char*)realloc(buffer, buffer_capacity);
323 assert(buffer);
324 }
325
326 assert(buffer_size < buffer_capacity);
327 }
328 fclose(f);
329 buffer[buffer_size] = '\0';
330
331 return buffer;
332}
Note: See TracBrowser for help on using the repository browser.