1 // -*- c-basic-offset: 2 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
5 * Copyright (C) 2004-2006 Apple Computer, Inc.
6 * Copyright (C) 2006 Bjoern Graf (bjoern.graf@gmail.com)
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.
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.
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.
26 #include "collector.h"
28 #include <wtf/HashTraits.h>
55 static void testIsInteger();
56 static char* createStringWithContentsOfFile(const char* fileName);
63 long getElapsedMS(); // call stop() first
69 #elif PLATFORM(WIN_OS)
73 // Windows does not have timeval, disabling this class for now (bug 7399)
79 void StopWatch::start()
82 QDateTime t = QDateTime::currentDateTime();
83 m_startTime = t.toTime_t() * 1000 + t.time().msec();
84 #elif PLATFORM(WIN_OS)
85 m_startTime = timeGetTime();
87 gettimeofday(&m_startTime, 0);
91 void StopWatch::stop()
94 QDateTime t = QDateTime::currentDateTime();
95 m_stopTime = t.toTime_t() * 1000 + t.time().msec();
96 #elif PLATFORM(WIN_OS)
97 m_stopTime = timeGetTime();
99 gettimeofday(&m_stopTime, 0);
103 long StopWatch::getElapsedMS()
105 #if PLATFORM(WIN_OS) || PLATFORM(QT)
106 return m_stopTime - m_startTime;
109 timersub(&m_stopTime, &m_startTime, &elapsedTime);
111 return elapsedTime.tv_sec * 1000 + lroundf(elapsedTime.tv_usec / 1000.0f);
115 class GlobalImp : public JSObject {
117 virtual UString className() const { return "global"; }
120 class TestFunctionImp : public JSObject {
122 TestFunctionImp(int i, int length);
123 virtual bool implementsCall() const { return true; }
124 virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const List &args);
126 enum { Print, Debug, Quit, GC, Version, Run };
132 TestFunctionImp::TestFunctionImp(int i, int length) : JSObject(), id(i)
134 putDirect(Identifier("length"), length, DontDelete | ReadOnly | DontEnum);
137 JSValue* TestFunctionImp::callAsFunction(ExecState* exec, JSObject*, const List &args)
141 printf("--> %s\n", args[0]->toString(exec).UTF8String().c_str());
142 return jsUndefined();
144 fprintf(stderr, "--> %s\n", args[0]->toString(exec).UTF8String().c_str());
145 return jsUndefined();
149 Collector::collect();
150 return jsUndefined();
153 // We need this function for compatibility with the Mozilla JS tests but for now
154 // we don't actually do any version-specific handling
155 return jsUndefined();
159 char* fileName = strdup(args[0]->toString(exec).UTF8String().c_str());
160 char* script = createStringWithContentsOfFile(fileName);
162 return throwError(exec, GeneralError, "Could not open file.");
165 exec->dynamicInterpreter()->evaluate(fileName, 0, script);
171 return jsNumber(stopWatch.getElapsedMS());
183 // Use SEH for Release builds only to get rid of the crash report dialog
184 // (luckyly the same tests fail in Release and Debug builds so far). Need to
185 // be in a separate main function because the kjsmain function requires object
193 #define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
203 int kjsmain(int argc, char** argv);
205 int main(int argc, char** argv)
207 #if defined(_DEBUG) && PLATFORM(WIN_OS)
208 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
209 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
210 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
211 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
212 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
213 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
218 res = kjsmain(argc, argv);
224 bool doIt(int argc, char** argv)
227 bool prettyPrint = false;
228 GlobalImp* global = new GlobalImp();
230 // create interpreter
231 RefPtr<Interpreter> interp = new Interpreter(global);
232 // add debug() function
233 global->put(interp->globalExec(), "debug", new TestFunctionImp(TestFunctionImp::Debug, 1));
234 // add "print" for compatibility with the mozilla js shell
235 global->put(interp->globalExec(), "print", new TestFunctionImp(TestFunctionImp::Print, 1));
236 // add "quit" for compatibility with the mozilla js shell
237 global->put(interp->globalExec(), "quit", new TestFunctionImp(TestFunctionImp::Quit, 0));
238 // add "gc" for compatibility with the mozilla js shell
239 global->put(interp->globalExec(), "gc", new TestFunctionImp(TestFunctionImp::GC, 0));
240 // add "version" for compatibility with the mozilla js shell
241 global->put(interp->globalExec(), "version", new TestFunctionImp(TestFunctionImp::Version, 1));
242 global->put(interp->globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1));
244 Interpreter::setShouldPrintExceptions(true);
246 for (int i = 1; i < argc; i++) {
247 const char* fileName = argv[i];
248 if (strcmp(fileName, "-f") == 0) // mozilla test driver script uses "-f" prefix for files
250 if (strcmp(fileName, "-p") == 0) {
255 char* script = createStringWithContentsOfFile(fileName);
258 break; // fail early so we can catch missing files
264 UString s = Parser::prettyPrint(script, &errLine, &errMsg);
266 fprintf(stderr, "%s:%d: %s.\n", fileName, errLine, errMsg.UTF8String().c_str());
272 printf("%s\n", s.UTF8String().c_str());
275 Completion completion = interp->evaluate(fileName, 0, script);
276 success = success && completion.complType() != Throw;
286 int kjsmain(int argc, char** argv)
289 fprintf(stderr, "Usage: testkjs file1 [file2...]\n");
297 bool success = doIt(argc, argv);
300 Collector::collect();
304 fprintf(stderr, "OK.\n");
307 Interpreter::finalCheck();
309 return success ? 0 : 3;
312 static void testIsInteger()
314 // Unit tests for WTF::IsInteger. Don't have a better place for them now.
315 // FIXME: move these once we create a unit test directory for WTF.
317 assert(IsInteger<bool>::value);
318 assert(IsInteger<char>::value);
319 assert(IsInteger<signed char>::value);
320 assert(IsInteger<unsigned char>::value);
321 assert(IsInteger<short>::value);
322 assert(IsInteger<unsigned short>::value);
323 assert(IsInteger<int>::value);
324 assert(IsInteger<unsigned int>::value);
325 assert(IsInteger<long>::value);
326 assert(IsInteger<unsigned long>::value);
327 assert(IsInteger<long long>::value);
328 assert(IsInteger<unsigned long long>::value);
330 assert(!IsInteger<char*>::value);
331 assert(!IsInteger<const char* >::value);
332 assert(!IsInteger<volatile char* >::value);
333 assert(!IsInteger<double>::value);
334 assert(!IsInteger<float>::value);
335 assert(!IsInteger<GlobalImp>::value);
338 static char* createStringWithContentsOfFile(const char* fileName)
342 size_t buffer_size = 0;
343 size_t buffer_capacity = 1024;
344 buffer = (char*)malloc(buffer_capacity);
346 FILE* f = fopen(fileName, "r");
348 fprintf(stderr, "Could not open file: %s\n", fileName);
353 while (!feof(f) && !ferror(f)) {
354 buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
355 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
356 buffer_capacity *= 2;
357 buffer = (char*)realloc(buffer, buffer_capacity);
361 assert(buffer_size < buffer_capacity);
364 buffer[buffer_size] = '\0';