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 Apple Computer, Inc.
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Library General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Library General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Library General Public License
|
---|
18 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #include "config.h"
|
---|
25 |
|
---|
26 | #include "HashTraits.h"
|
---|
27 | #include "JSLock.h"
|
---|
28 | #include "interpreter.h"
|
---|
29 | #include "object.h"
|
---|
30 | #include "types.h"
|
---|
31 | #include "value.h"
|
---|
32 |
|
---|
33 | #include <math.h>
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <stdlib.h>
|
---|
36 | #include <string.h>
|
---|
37 | #if HAVE(SYS_TIME_H)
|
---|
38 | #include <sys/time.h>
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | using namespace KJS;
|
---|
42 | using namespace KXMLCore;
|
---|
43 |
|
---|
44 | static void testIsInteger();
|
---|
45 | static char* createStringWithContentsOfFile(const char* fileName);
|
---|
46 |
|
---|
47 | class StopWatch
|
---|
48 | {
|
---|
49 | public:
|
---|
50 | void start();
|
---|
51 | void stop();
|
---|
52 | long getElapsedMS(); // call stop() first
|
---|
53 |
|
---|
54 | private:
|
---|
55 | #if !PLATFORM(WIN_OS)
|
---|
56 | // Windows does not have timeval, disabling this class for now (bug 7399)
|
---|
57 | timeval m_startTime;
|
---|
58 | timeval m_stopTime;
|
---|
59 | #endif
|
---|
60 | };
|
---|
61 |
|
---|
62 | void StopWatch::start()
|
---|
63 | {
|
---|
64 | #if !PLATFORM(WIN_OS)
|
---|
65 | gettimeofday(&m_startTime, 0);
|
---|
66 | #endif
|
---|
67 | }
|
---|
68 |
|
---|
69 | void StopWatch::stop()
|
---|
70 | {
|
---|
71 | #if !PLATFORM(WIN_OS)
|
---|
72 | gettimeofday(&m_stopTime, 0);
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 |
|
---|
76 | long StopWatch::getElapsedMS()
|
---|
77 | {
|
---|
78 | #if !PLATFORM(WIN_OS)
|
---|
79 | timeval elapsedTime;
|
---|
80 | timersub(&m_stopTime, &m_startTime, &elapsedTime);
|
---|
81 |
|
---|
82 | return elapsedTime.tv_sec * 1000 + lroundf(elapsedTime.tv_usec / 1000.0);
|
---|
83 | #else
|
---|
84 | return 0;
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 | class GlobalImp : public JSObject {
|
---|
89 | public:
|
---|
90 | virtual UString className() const { return "global"; }
|
---|
91 | };
|
---|
92 |
|
---|
93 | class TestFunctionImp : public JSObject {
|
---|
94 | public:
|
---|
95 | TestFunctionImp(int i, int length);
|
---|
96 | virtual bool implementsCall() const { return true; }
|
---|
97 | virtual JSValue* callAsFunction(ExecState* exec, JSObject* thisObj, const List &args);
|
---|
98 |
|
---|
99 | enum { Print, Debug, Quit, GC, Version, Run };
|
---|
100 |
|
---|
101 | private:
|
---|
102 | int id;
|
---|
103 | };
|
---|
104 |
|
---|
105 | TestFunctionImp::TestFunctionImp(int i, int length) : JSObject(), id(i)
|
---|
106 | {
|
---|
107 | putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
|
---|
108 | }
|
---|
109 |
|
---|
110 | JSValue* TestFunctionImp::callAsFunction(ExecState* exec, JSObject*, const List &args)
|
---|
111 | {
|
---|
112 | switch (id) {
|
---|
113 | case Print:
|
---|
114 | printf("--> %s\n", args[0]->toString(exec).UTF8String().c_str());
|
---|
115 | return jsUndefined();
|
---|
116 | case Debug:
|
---|
117 | fprintf(stderr, "--> %s\n", args[0]->toString(exec).UTF8String().c_str());
|
---|
118 | return jsUndefined();
|
---|
119 | case GC:
|
---|
120 | {
|
---|
121 | JSLock lock;
|
---|
122 | Interpreter::collect();
|
---|
123 | return jsUndefined();
|
---|
124 | }
|
---|
125 | case Version:
|
---|
126 | // We need this function for compatibility with the Mozilla JS tests but for now
|
---|
127 | // we don't actually do any version-specific handling
|
---|
128 | return jsUndefined();
|
---|
129 | case Run:
|
---|
130 | {
|
---|
131 | StopWatch stopWatch;
|
---|
132 | char* fileName = strdup(args[0]->toString(exec).UTF8String().c_str());
|
---|
133 | char* script = createStringWithContentsOfFile(fileName);
|
---|
134 | if (!script)
|
---|
135 | return throwError(exec, GeneralError, "Could not open file.");
|
---|
136 |
|
---|
137 | stopWatch.start();
|
---|
138 | exec->dynamicInterpreter()->evaluate(fileName, 0, script);
|
---|
139 | stopWatch.stop();
|
---|
140 |
|
---|
141 | free(script);
|
---|
142 | free(fileName);
|
---|
143 |
|
---|
144 | return jsNumber(stopWatch.getElapsedMS());
|
---|
145 | }
|
---|
146 | case Quit:
|
---|
147 | exit(0);
|
---|
148 | default:
|
---|
149 | abort();
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | int main(int argc, char** argv)
|
---|
154 | {
|
---|
155 | if (argc < 2) {
|
---|
156 | fprintf(stderr, "Usage: testkjs file1 [file2...]\n");
|
---|
157 | return -1;
|
---|
158 | }
|
---|
159 |
|
---|
160 | testIsInteger();
|
---|
161 |
|
---|
162 | bool success = true;
|
---|
163 | {
|
---|
164 | JSLock lock;
|
---|
165 |
|
---|
166 | GlobalImp* global = new GlobalImp();
|
---|
167 |
|
---|
168 | // create interpreter
|
---|
169 | Interpreter interp(global);
|
---|
170 | // add debug() function
|
---|
171 | global->put(interp.globalExec(), "debug", new TestFunctionImp(TestFunctionImp::Debug, 1));
|
---|
172 | // add "print" for compatibility with the mozilla js shell
|
---|
173 | global->put(interp.globalExec(), "print", new TestFunctionImp(TestFunctionImp::Print, 1));
|
---|
174 | // add "quit" for compatibility with the mozilla js shell
|
---|
175 | global->put(interp.globalExec(), "quit", new TestFunctionImp(TestFunctionImp::Quit, 0));
|
---|
176 | // add "gc" for compatibility with the mozilla js shell
|
---|
177 | global->put(interp.globalExec(), "gc", new TestFunctionImp(TestFunctionImp::GC, 0));
|
---|
178 | // add "version" for compatibility with the mozilla js shell
|
---|
179 | global->put(interp.globalExec(), "version", new TestFunctionImp(TestFunctionImp::Version, 1));
|
---|
180 | global->put(interp.globalExec(), "run", new TestFunctionImp(TestFunctionImp::Run, 1));
|
---|
181 |
|
---|
182 | Interpreter::setShouldPrintExceptions(true);
|
---|
183 |
|
---|
184 | for (int i = 1; i < argc; i++) {
|
---|
185 | const char* fileName = argv[i];
|
---|
186 | if (strcmp(fileName, "-f") == 0) // mozilla test driver script uses "-f" prefix for files
|
---|
187 | continue;
|
---|
188 |
|
---|
189 | char* script = createStringWithContentsOfFile(fileName);
|
---|
190 | if (!script) {
|
---|
191 | success = false;
|
---|
192 | break; // fail early so we can catch missing files
|
---|
193 | }
|
---|
194 |
|
---|
195 | Completion completion = interp.evaluate(fileName, 0, script);
|
---|
196 | success = success && completion.complType() != Throw;
|
---|
197 | free(script);
|
---|
198 | }
|
---|
199 |
|
---|
200 | delete global;
|
---|
201 | } // end block, so that interpreter gets deleted
|
---|
202 |
|
---|
203 | if (success)
|
---|
204 | fprintf(stderr, "OK.\n");
|
---|
205 |
|
---|
206 | #ifdef KJS_DEBUG_MEM
|
---|
207 | Interpreter::finalCheck();
|
---|
208 | #endif
|
---|
209 | return success ? 0 : 3;
|
---|
210 | }
|
---|
211 |
|
---|
212 | static void testIsInteger()
|
---|
213 | {
|
---|
214 | // Unit tests for KXMLCore::IsInteger. Don't have a better place for them now.
|
---|
215 | // FIXME: move these once we create a unit test directory for KXMLCore.
|
---|
216 |
|
---|
217 | assert(IsInteger<bool>::value);
|
---|
218 | assert(IsInteger<char>::value);
|
---|
219 | assert(IsInteger<signed char>::value);
|
---|
220 | assert(IsInteger<unsigned char>::value);
|
---|
221 | assert(IsInteger<short>::value);
|
---|
222 | assert(IsInteger<unsigned short>::value);
|
---|
223 | assert(IsInteger<int>::value);
|
---|
224 | assert(IsInteger<unsigned int>::value);
|
---|
225 | assert(IsInteger<long>::value);
|
---|
226 | assert(IsInteger<unsigned long>::value);
|
---|
227 | assert(IsInteger<long long>::value);
|
---|
228 | assert(IsInteger<unsigned long long>::value);
|
---|
229 |
|
---|
230 | assert(!IsInteger<char*>::value);
|
---|
231 | assert(!IsInteger<const char* >::value);
|
---|
232 | assert(!IsInteger<volatile char* >::value);
|
---|
233 | assert(!IsInteger<double>::value);
|
---|
234 | assert(!IsInteger<float>::value);
|
---|
235 | assert(!IsInteger<GlobalImp>::value);
|
---|
236 | }
|
---|
237 |
|
---|
238 | static char* createStringWithContentsOfFile(const char* fileName)
|
---|
239 | {
|
---|
240 | char* buffer;
|
---|
241 |
|
---|
242 | int buffer_size = 0;
|
---|
243 | int buffer_capacity = 1024;
|
---|
244 | buffer = (char*)malloc(buffer_capacity);
|
---|
245 |
|
---|
246 | FILE* f = fopen(fileName, "r");
|
---|
247 | if (!f) {
|
---|
248 | fprintf(stderr, "Could not open file: %s\n", fileName);
|
---|
249 | return 0;
|
---|
250 | }
|
---|
251 |
|
---|
252 | while (!feof(f) && !ferror(f)) {
|
---|
253 | buffer_size += fread(buffer + buffer_size, 1, buffer_capacity - buffer_size, f);
|
---|
254 | if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
|
---|
255 | buffer_capacity *= 2;
|
---|
256 | buffer = (char*)realloc(buffer, buffer_capacity);
|
---|
257 | assert(buffer);
|
---|
258 | }
|
---|
259 |
|
---|
260 | assert(buffer_size < buffer_capacity);
|
---|
261 | }
|
---|
262 | fclose(f);
|
---|
263 | buffer[buffer_size] = '\0';
|
---|
264 |
|
---|
265 | return buffer;
|
---|
266 | }
|
---|