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

Last change on this file since 15310 was 14457, checked in by staikos, 19 years ago

Reviewed by Maciej, Alexey, and Eric.

  • Use // in .c files to compile with non-C99 and non-GCC compilers.
  • Change include to <wtf/HashTraits.h> from "HashTraits.h" to avoid -I
  • Use correct parentheses and correct mask for utf-32 support.
  • 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 <wtf/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 WTF;
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 defined(_DEBUG) && PLATFORM(WIN_OS)
193 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
194 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
195 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
196 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
197 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
198 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
199#endif
200
201 int res = 0;
202 TRY
203 res = kjsmain(argc, argv);
204 EXCEPT(res = 3)
205 return res;
206}
207
208
209bool doIt(int argc, char** argv)
210{
211 bool success = true;
212 GlobalImp* global = new GlobalImp();
213
214 // create interpreter
215 Interpreter interp(global);
216 // add debug() function
217 global->put(interp.globalExec(), "debug", new TestFunctionImp(TestFunctionImp::Debug, 1));
218 // add "print" for compatibility with the mozilla js shell
219 global->put(interp.globalExec(), "print", new TestFunctionImp(TestFunctionImp::Print, 1));
220 // add "quit" for compatibility with the mozilla js shell
221 global->put(interp.globalExec(), "quit", new TestFunctionImp(TestFunctionImp::Quit, 0));
222 // add "gc" for compatibility with the mozilla js shell
223 global->put(interp.globalExec(), "gc", new TestFunctionImp(TestFunctionImp::GC, 0));
224 // add "version" for compatibility with the mozilla js shell
225 global->put(interp.globalExec(), "version", new TestFunctionImp(TestFunctionImp::Version, 1));
226 global->put(interp.globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1));
227
228 Interpreter::setShouldPrintExceptions(true);
229
230 for (int i = 1; i < argc; i++) {
231 const char* fileName = argv[i];
232 if (strcmp(fileName, "-f") == 0) // mozilla test driver script uses "-f" prefix for files
233 continue;
234
235 char* script = createStringWithContentsOfFile(fileName);
236 if (!script) {
237 success = false;
238 break; // fail early so we can catch missing files
239 }
240
241 Completion completion = interp.evaluate(fileName, 0, script);
242 success = success && completion.complType() != Throw;
243 free(script);
244 }
245
246 return success;
247}
248
249
250int kjsmain(int argc, char** argv)
251{
252 if (argc < 2) {
253 fprintf(stderr, "Usage: testkjs file1 [file2...]\n");
254 return -1;
255 }
256
257 testIsInteger();
258
259 JSLock lock;
260
261 bool success = doIt(argc, argv);
262
263#ifndef NDEBUG
264 Collector::collect();
265#endif
266
267 if (success)
268 fprintf(stderr, "OK.\n");
269
270#ifdef KJS_DEBUG_MEM
271 Interpreter::finalCheck();
272#endif
273 return success ? 0 : 3;
274}
275
276static void testIsInteger()
277{
278 // Unit tests for WTF::IsInteger. Don't have a better place for them now.
279 // FIXME: move these once we create a unit test directory for WTF.
280
281 assert(IsInteger<bool>::value);
282 assert(IsInteger<char>::value);
283 assert(IsInteger<signed char>::value);
284 assert(IsInteger<unsigned char>::value);
285 assert(IsInteger<short>::value);
286 assert(IsInteger<unsigned short>::value);
287 assert(IsInteger<int>::value);
288 assert(IsInteger<unsigned int>::value);
289 assert(IsInteger<long>::value);
290 assert(IsInteger<unsigned long>::value);
291 assert(IsInteger<long long>::value);
292 assert(IsInteger<unsigned long long>::value);
293
294 assert(!IsInteger<char*>::value);
295 assert(!IsInteger<const char* >::value);
296 assert(!IsInteger<volatile char* >::value);
297 assert(!IsInteger<double>::value);
298 assert(!IsInteger<float>::value);
299 assert(!IsInteger<GlobalImp>::value);
300}
301
302static char* createStringWithContentsOfFile(const char* fileName)
303{
304 char* buffer;
305
306 int buffer_size = 0;
307 int buffer_capacity = 1024;
308 buffer = (char*)malloc(buffer_capacity);
309
310 FILE* f = fopen(fileName, "r");
311 if (!f) {
312 fprintf(stderr, "Could not open file: %s\n", fileName);
313 return 0;
314 }
315
316 while (!feof(f) && !ferror(f)) {
317 buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
318 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
319 buffer_capacity *= 2;
320 buffer = (char*)realloc(buffer, buffer_capacity);
321 assert(buffer);
322 }
323
324 assert(buffer_size < buffer_capacity);
325 }
326 fclose(f);
327 buffer[buffer_size] = '\0';
328
329 return buffer;
330}
Note: See TracBrowser for help on using the repository browser.