source: webkit/trunk/JavaScriptCore/jsc.cpp@ 38375

Last change on this file since 38375 was 38375, checked in by [email protected], 17 years ago

2008-11-13 Cameron Zwarich <[email protected]>

Reviewed by Alexey Proskuryakov.

Perform teardown in the 'jsc' shell in order to suppress annoying and
misleading leak messages. There is still a lone JSC::Node leaking when
quit() is called, but hopefully that can be fixed as well.

  • jsc.cpp: (functionQuit): (main):
  • Property svn:eol-style set to native
File size: 15.2 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2006 Bjoern Graf ([email protected])
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 *
21 */
22
23#include "config.h"
24
25#include "Completion.h"
26#include "CodeGenerator.h"
27#include "InitializeThreading.h"
28#include "Interpreter.h"
29#include "JSArray.h"
30#include "JSLock.h"
31#include "PrototypeFunction.h"
32#include "SamplingTool.h"
33#include <math.h>
34#include <stdio.h>
35#include <string.h>
36
37#if !PLATFORM(WIN_OS)
38#include <unistd.h>
39#endif
40
41#if HAVE(READLINE)
42#include <readline/history.h>
43#include <readline/readline.h>
44#endif
45
46#if HAVE(SYS_TIME_H)
47#include <sys/time.h>
48#endif
49
50#if PLATFORM(UNIX)
51#include <signal.h>
52#endif
53
54#if COMPILER(MSVC)
55#include <crtdbg.h>
56#include <windows.h>
57#endif
58
59#if PLATFORM(QT)
60#include <QCoreApplication>
61#include <QDateTime>
62#endif
63
64using namespace JSC;
65using namespace WTF;
66
67static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer);
68
69static JSValue* functionPrint(ExecState*, JSObject*, JSValue*, const ArgList&);
70static JSValue* functionDebug(ExecState*, JSObject*, JSValue*, const ArgList&);
71static JSValue* functionGC(ExecState*, JSObject*, JSValue*, const ArgList&);
72static JSValue* functionVersion(ExecState*, JSObject*, JSValue*, const ArgList&);
73static JSValue* functionRun(ExecState*, JSObject*, JSValue*, const ArgList&);
74static JSValue* functionLoad(ExecState*, JSObject*, JSValue*, const ArgList&);
75static JSValue* functionReadline(ExecState*, JSObject*, JSValue*, const ArgList&);
76static JSValue* functionQuit(ExecState*, JSObject*, JSValue*, const ArgList&);
77
78struct Options {
79 Options()
80 : interactive(false)
81 , dump(false)
82 {
83 }
84
85 bool interactive;
86 bool dump;
87 Vector<UString> fileNames;
88 Vector<UString> arguments;
89};
90
91static const char interactivePrompt[] = "> ";
92static const UString interpreterName("Interpreter");
93
94class StopWatch {
95public:
96 void start();
97 void stop();
98 long getElapsedMS(); // call stop() first
99
100private:
101#if PLATFORM(QT)
102 uint m_startTime;
103 uint m_stopTime;
104#elif PLATFORM(WIN_OS)
105 DWORD m_startTime;
106 DWORD m_stopTime;
107#else
108 // Windows does not have timeval, disabling this class for now (bug 7399)
109 timeval m_startTime;
110 timeval m_stopTime;
111#endif
112};
113
114void StopWatch::start()
115{
116#if PLATFORM(QT)
117 QDateTime t = QDateTime::currentDateTime();
118 m_startTime = t.toTime_t() * 1000 + t.time().msec();
119#elif PLATFORM(WIN_OS)
120 m_startTime = timeGetTime();
121#else
122 gettimeofday(&m_startTime, 0);
123#endif
124}
125
126void StopWatch::stop()
127{
128#if PLATFORM(QT)
129 QDateTime t = QDateTime::currentDateTime();
130 m_stopTime = t.toTime_t() * 1000 + t.time().msec();
131#elif PLATFORM(WIN_OS)
132 m_stopTime = timeGetTime();
133#else
134 gettimeofday(&m_stopTime, 0);
135#endif
136}
137
138long StopWatch::getElapsedMS()
139{
140#if PLATFORM(WIN_OS) || PLATFORM(QT)
141 return m_stopTime - m_startTime;
142#else
143 timeval elapsedTime;
144 timersub(&m_stopTime, &m_startTime, &elapsedTime);
145
146 return elapsedTime.tv_sec * 1000 + lroundf(elapsedTime.tv_usec / 1000.0f);
147#endif
148}
149
150class GlobalObject : public JSGlobalObject {
151public:
152 GlobalObject(const Vector<UString>& arguments);
153 virtual UString className() const { return "global"; }
154};
155COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
156ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
157
158GlobalObject::GlobalObject(const Vector<UString>& arguments)
159 : JSGlobalObject()
160{
161 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "debug"), functionDebug));
162 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "print"), functionPrint));
163 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "quit"), functionQuit));
164 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "gc"), functionGC));
165 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "version"), functionVersion));
166 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "run"), functionRun));
167 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "load"), functionLoad));
168 putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "readline"), functionReadline));
169
170 JSObject* array = constructEmptyArray(globalExec());
171 for (size_t i = 0; i < arguments.size(); ++i)
172 array->put(globalExec(), i, jsString(globalExec(), arguments[i]));
173 putDirect(Identifier(globalExec(), "arguments"), array);
174}
175
176JSValue* functionPrint(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
177{
178 for (unsigned i = 0; i < args.size(); ++i) {
179 if (i != 0)
180 putchar(' ');
181
182 printf("%s", args.at(exec, i)->toString(exec).UTF8String().c_str());
183 }
184
185 putchar('\n');
186 fflush(stdout);
187 return jsUndefined();
188}
189
190JSValue* functionDebug(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
191{
192 fprintf(stderr, "--> %s\n", args.at(exec, 0)->toString(exec).UTF8String().c_str());
193 return jsUndefined();
194}
195
196JSValue* functionGC(ExecState* exec, JSObject*, JSValue*, const ArgList&)
197{
198 JSLock lock(false);
199 exec->heap()->collect();
200 return jsUndefined();
201}
202
203JSValue* functionVersion(ExecState*, JSObject*, JSValue*, const ArgList&)
204{
205 // We need this function for compatibility with the Mozilla JS tests but for now
206 // we don't actually do any version-specific handling
207 return jsUndefined();
208}
209
210JSValue* functionRun(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
211{
212 StopWatch stopWatch;
213 UString fileName = args.at(exec, 0)->toString(exec);
214 Vector<char> script;
215 if (!fillBufferWithContentsOfFile(fileName, script))
216 return throwError(exec, GeneralError, "Could not open file.");
217
218 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
219
220 stopWatch.start();
221 Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
222 stopWatch.stop();
223
224 return jsNumber(globalObject->globalExec(), stopWatch.getElapsedMS());
225}
226
227JSValue* functionLoad(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
228{
229 UString fileName = args.at(exec, 0)->toString(exec);
230 Vector<char> script;
231 if (!fillBufferWithContentsOfFile(fileName, script))
232 return throwError(exec, GeneralError, "Could not open file.");
233
234 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
235 Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
236
237 return jsUndefined();
238}
239
240JSValue* functionReadline(ExecState* exec, JSObject*, JSValue*, const ArgList&)
241{
242 Vector<char, 256> line;
243 int c;
244 while ((c = getchar()) != EOF) {
245 // FIXME: Should we also break on \r?
246 if (c == '\n')
247 break;
248 line.append(c);
249 }
250 line.append('\0');
251 return jsString(exec, line.data());
252}
253
254JSValue* functionQuit(ExecState* exec, JSObject*, JSValue*, const ArgList&)
255{
256 {
257 JSLock lock(false);
258 JSGlobalData& globalData = exec->globalData();
259 globalData.heap.destroy();
260 globalData.deref();
261 }
262
263 exit(0);
264#if !COMPILER(MSVC)
265 // MSVC knows that exit(0) never returns, so it flags this return statement as unreachable.
266 return jsUndefined();
267#endif
268}
269
270// Use SEH for Release builds only to get rid of the crash report dialog
271// (luckily the same tests fail in Release and Debug builds so far). Need to
272// be in a separate main function because the jscmain function requires object
273// unwinding.
274
275#if COMPILER(MSVC) && !defined(_DEBUG)
276#define TRY __try {
277#define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
278#else
279#define TRY
280#define EXCEPT(x)
281#endif
282
283int jscmain(int argc, char** argv, JSGlobalData*);
284
285int main(int argc, char** argv)
286{
287#if defined(_DEBUG) && PLATFORM(WIN_OS)
288 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
289 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
290 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
291 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
292 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
293 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
294#endif
295
296#if PLATFORM(QT)
297 QCoreApplication app(argc, argv);
298#endif
299
300 int res = 0;
301 RefPtr<JSGlobalData> globalData = JSGlobalData::create();
302 TRY
303 res = jscmain(argc, argv, globalData.get());
304 EXCEPT(res = 3)
305 {
306 JSLock lock(false);
307 globalData->heap.destroy();
308 }
309 return res;
310}
311
312static bool runWithScripts(GlobalObject* globalObject, const Vector<UString>& fileNames, bool dump)
313{
314 Vector<char> script;
315
316 if (dump)
317 CodeGenerator::setDumpsGeneratedCode(true);
318
319#if ENABLE(OPCODE_SAMPLING)
320 Machine* machine = globalObject->globalData()->machine;
321 machine->setSampler(new SamplingTool(machine));
322#endif
323
324 bool success = true;
325 for (size_t i = 0; i < fileNames.size(); i++) {
326 UString fileName = fileNames[i];
327
328 if (!fillBufferWithContentsOfFile(fileName, script))
329 return false; // fail early so we can catch missing files
330
331#if ENABLE(OPCODE_SAMPLING)
332 machine->sampler()->start();
333#endif
334 Completion completion = Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
335 success = success && completion.complType() != Throw;
336 if (dump) {
337 if (completion.complType() == Throw)
338 printf("Exception: %s\n", completion.value()->toString(globalObject->globalExec()).ascii());
339 else
340 printf("End: %s\n", completion.value()->toString(globalObject->globalExec()).ascii());
341 }
342
343 globalObject->globalExec()->clearException();
344
345#if ENABLE(OPCODE_SAMPLING)
346 machine->sampler()->stop();
347#endif
348 }
349
350#if ENABLE(OPCODE_SAMPLING)
351 machine->sampler()->dump(globalObject->globalExec());
352 delete machine->sampler();
353#endif
354 return success;
355}
356
357static void runInteractive(GlobalObject* globalObject)
358{
359 while (true) {
360#if HAVE(READLINE)
361 char* line = readline(interactivePrompt);
362 if (!line)
363 break;
364 if (line[0])
365 add_history(line);
366 Completion completion = Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line, interpreterName));
367 free(line);
368#else
369 puts(interactivePrompt);
370 Vector<char, 256> line;
371 int c;
372 while ((c = getchar()) != EOF) {
373 // FIXME: Should we also break on \r?
374 if (c == '\n')
375 break;
376 line.append(c);
377 }
378 line.append('\0');
379 Completion completion = Interpreter::evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line.data(), interpreterName));
380#endif
381 if (completion.complType() == Throw)
382 printf("Exception: %s\n", completion.value()->toString(globalObject->globalExec()).ascii());
383 else
384 printf("%s\n", completion.value()->toString(globalObject->globalExec()).UTF8String().c_str());
385
386 globalObject->globalExec()->clearException();
387 }
388 printf("\n");
389}
390
391static void printUsageStatement()
392{
393 fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
394 fprintf(stderr, " -d Dumps bytecode (debug builds only)\n");
395 fprintf(stderr, " -f Specifies a source file (deprecated)\n");
396 fprintf(stderr, " -h|--help Prints this help message\n");
397 fprintf(stderr, " -i Enables interactive mode (default if no files are specified)\n");
398 fprintf(stderr, " -s Installs signal handlers that exit on a crash (Unix platforms only)\n");
399 exit(-1);
400}
401
402static void parseArguments(int argc, char** argv, Options& options)
403{
404 int i = 1;
405 for (; i < argc; ++i) {
406 const char* arg = argv[i];
407 if (strcmp(arg, "-f") == 0) {
408 if (++i == argc)
409 printUsageStatement();
410 options.fileNames.append(argv[i]);
411 continue;
412 }
413 if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
414 printUsageStatement();
415 }
416 if (strcmp(arg, "-i") == 0) {
417 options.interactive = true;
418 continue;
419 }
420 if (strcmp(arg, "-d") == 0) {
421 options.dump = true;
422 continue;
423 }
424 if (strcmp(arg, "-s") == 0) {
425#if PLATFORM(UNIX)
426 signal(SIGILL, _exit);
427 signal(SIGFPE, _exit);
428 signal(SIGBUS, _exit);
429 signal(SIGSEGV, _exit);
430#endif
431 continue;
432 }
433 if (strcmp(arg, "--") == 0) {
434 ++i;
435 break;
436 }
437 options.fileNames.append(argv[i]);
438 }
439
440 if (options.fileNames.isEmpty())
441 options.interactive = true;
442
443 for (; i < argc; ++i)
444 options.arguments.append(argv[i]);
445}
446
447int jscmain(int argc, char** argv, JSGlobalData* globalData)
448{
449 JSC::initializeThreading();
450
451 JSLock lock(false);
452
453 Options options;
454 parseArguments(argc, argv, options);
455
456 GlobalObject* globalObject = new (globalData) GlobalObject(options.arguments);
457 bool success = runWithScripts(globalObject, options.fileNames, options.dump);
458 if (options.interactive && success)
459 runInteractive(globalObject);
460
461 return success ? 0 : 3;
462}
463
464static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer)
465{
466 FILE* f = fopen(fileName.UTF8String().c_str(), "r");
467 if (!f) {
468 fprintf(stderr, "Could not open file: %s\n", fileName.UTF8String().c_str());
469 return false;
470 }
471
472 size_t buffer_size = 0;
473 size_t buffer_capacity = 1024;
474
475 buffer.resize(buffer_capacity);
476
477 while (!feof(f) && !ferror(f)) {
478 buffer_size += fread(buffer.data() + buffer_size, 1, buffer_capacity - buffer_size, f);
479 if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
480 buffer_capacity *= 2;
481 buffer.resize(buffer_capacity);
482 }
483 }
484 fclose(f);
485 buffer[buffer_size] = '\0';
486
487 return true;
488}
Note: See TracBrowser for help on using the repository browser.