source: webkit/trunk/Source/JavaScriptCore/jsc.cpp@ 83955

Last change on this file since 83955 was 83955, checked in by Philippe Normand, 14 years ago

2011-04-15 Philippe Normand <[email protected]>

Unreviewed, rollout r83894 r83827 r83810 r83809 r83808.
r83808 and its follow-up commits broke GTK Release builds.
https://bugs.webkit.org/show_bug.cgi?id=58483

  • Property svn:eol-style set to native
File size: 18.7 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 "BytecodeGenerator.h"
26#include "Completion.h"
27#include "CurrentTime.h"
28#include "ExceptionHelpers.h"
29#include "InitializeThreading.h"
30#include "JSArray.h"
31#include "JSFunction.h"
32#include "JSLock.h"
33#include "JSString.h"
34#include "SamplingTool.h"
35#include <math.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#if !OS(WINDOWS)
41#include <unistd.h>
42#endif
43
44#if HAVE(READLINE)
45#include <readline/history.h>
46#include <readline/readline.h>
47#endif
48
49#if HAVE(SYS_TIME_H)
50#include <sys/time.h>
51#endif
52
53#if HAVE(SIGNAL_H)
54#include <signal.h>
55#endif
56
57#if COMPILER(MSVC) && !OS(WINCE)
58#include <crtdbg.h>
59#include <mmsystem.h>
60#include <windows.h>
61#endif
62
63#if PLATFORM(QT)
64#include <QCoreApplication>
65#include <QDateTime>
66#endif
67
68using namespace JSC;
69using namespace WTF;
70
71static void cleanupGlobalData(JSGlobalData*);
72static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer);
73
74static EncodedJSValue JSC_HOST_CALL functionPrint(ExecState*);
75static EncodedJSValue JSC_HOST_CALL functionDebug(ExecState*);
76static EncodedJSValue JSC_HOST_CALL functionGC(ExecState*);
77static EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*);
78static EncodedJSValue JSC_HOST_CALL functionRun(ExecState*);
79static EncodedJSValue JSC_HOST_CALL functionLoad(ExecState*);
80static EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState*);
81static EncodedJSValue JSC_HOST_CALL functionReadline(ExecState*);
82static NO_RETURN_WITH_VALUE EncodedJSValue JSC_HOST_CALL functionQuit(ExecState*);
83
84#if ENABLE(SAMPLING_FLAGS)
85static EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState*);
86static EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState*);
87#endif
88
89struct Script {
90 bool isFile;
91 char* argument;
92
93 Script(bool isFile, char *argument)
94 : isFile(isFile)
95 , argument(argument)
96 {
97 }
98};
99
100struct Options {
101 Options()
102 : interactive(false)
103 , dump(false)
104 {
105 }
106
107 bool interactive;
108 bool dump;
109 Vector<Script> scripts;
110 Vector<UString> arguments;
111};
112
113static const char interactivePrompt[] = "> ";
114static const UString interpreterName("Interpreter");
115
116class StopWatch {
117public:
118 void start();
119 void stop();
120 long getElapsedMS(); // call stop() first
121
122private:
123 double m_startTime;
124 double m_stopTime;
125};
126
127void StopWatch::start()
128{
129 m_startTime = currentTime();
130}
131
132void StopWatch::stop()
133{
134 m_stopTime = currentTime();
135}
136
137long StopWatch::getElapsedMS()
138{
139 return static_cast<long>((m_stopTime - m_startTime) * 1000);
140}
141
142class GlobalObject : public JSGlobalObject {
143public:
144 GlobalObject(JSGlobalData&, const Vector<UString>& arguments);
145 virtual UString className() const { return "global"; }
146};
147COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
148ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
149
150GlobalObject::GlobalObject(JSGlobalData& globalData, const Vector<UString>& arguments)
151 : JSGlobalObject(globalData)
152{
153 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "debug"), functionDebug));
154 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "print"), functionPrint));
155 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 0, Identifier(globalExec(), "quit"), functionQuit));
156 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 0, Identifier(globalExec(), "gc"), functionGC));
157 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "version"), functionVersion));
158 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "run"), functionRun));
159 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "load"), functionLoad));
160 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "checkSyntax"), functionCheckSyntax));
161 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 0, Identifier(globalExec(), "readline"), functionReadline));
162
163#if ENABLE(SAMPLING_FLAGS)
164 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "setSamplingFlags"), functionSetSamplingFlags));
165 putDirectFunction(globalExec(), new (globalExec()) JSFunction(globalExec(), this, functionStructure(), 1, Identifier(globalExec(), "clearSamplingFlags"), functionClearSamplingFlags));
166#endif
167
168 JSObject* array = constructEmptyArray(globalExec());
169 for (size_t i = 0; i < arguments.size(); ++i)
170 array->put(globalExec(), i, jsString(globalExec(), arguments[i]));
171 putDirect(globalExec()->globalData(), Identifier(globalExec(), "arguments"), array);
172}
173
174EncodedJSValue JSC_HOST_CALL functionPrint(ExecState* exec)
175{
176 for (unsigned i = 0; i < exec->argumentCount(); ++i) {
177 if (i)
178 putchar(' ');
179
180 printf("%s", exec->argument(i).toString(exec).utf8().data());
181 }
182
183 putchar('\n');
184 fflush(stdout);
185 return JSValue::encode(jsUndefined());
186}
187
188EncodedJSValue JSC_HOST_CALL functionDebug(ExecState* exec)
189{
190 fprintf(stderr, "--> %s\n", exec->argument(0).toString(exec).utf8().data());
191 return JSValue::encode(jsUndefined());
192}
193
194EncodedJSValue JSC_HOST_CALL functionGC(ExecState* exec)
195{
196 JSLock lock(SilenceAssertionsOnly);
197 exec->heap()->collectAllGarbage();
198 return JSValue::encode(jsUndefined());
199}
200
201EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*)
202{
203 // We need this function for compatibility with the Mozilla JS tests but for now
204 // we don't actually do any version-specific handling
205 return JSValue::encode(jsUndefined());
206}
207
208EncodedJSValue JSC_HOST_CALL functionRun(ExecState* exec)
209{
210 UString fileName = exec->argument(0).toString(exec);
211 Vector<char> script;
212 if (!fillBufferWithContentsOfFile(fileName, script))
213 return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
214
215 GlobalObject* globalObject = new (&exec->globalData()) GlobalObject(exec->globalData(), Vector<UString>());
216
217 StopWatch stopWatch;
218 stopWatch.start();
219 evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
220 stopWatch.stop();
221
222 return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
223}
224
225EncodedJSValue JSC_HOST_CALL functionLoad(ExecState* exec)
226{
227 UString fileName = exec->argument(0).toString(exec);
228 Vector<char> script;
229 if (!fillBufferWithContentsOfFile(fileName, script))
230 return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
231
232 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
233 Completion result = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
234 if (result.complType() == Throw)
235 throwError(exec, result.value());
236 return JSValue::encode(result.value());
237}
238
239EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState* exec)
240{
241 UString fileName = exec->argument(0).toString(exec);
242 Vector<char> script;
243 if (!fillBufferWithContentsOfFile(fileName, script))
244 return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
245
246 JSGlobalObject* globalObject = exec->lexicalGlobalObject();
247
248 StopWatch stopWatch;
249 stopWatch.start();
250 Completion result = checkSyntax(globalObject->globalExec(), makeSource(script.data(), fileName));
251 stopWatch.stop();
252
253 if (result.complType() == Throw)
254 throwError(exec, result.value());
255 return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
256}
257
258#if ENABLE(SAMPLING_FLAGS)
259EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState* exec)
260{
261 for (unsigned i = 0; i < exec->argumentCount(); ++i) {
262 unsigned flag = static_cast<unsigned>(exec->argument(i).toNumber(exec));
263 if ((flag >= 1) && (flag <= 32))
264 SamplingFlags::setFlag(flag);
265 }
266 return JSValue::encode(jsNull());
267}
268
269EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState* exec)
270{
271 for (unsigned i = 0; i < exec->argumentCount(); ++i) {
272 unsigned flag = static_cast<unsigned>(exec->argument(i).toNumber(exec));
273 if ((flag >= 1) && (flag <= 32))
274 SamplingFlags::clearFlag(flag);
275 }
276 return JSValue::encode(jsNull());
277}
278#endif
279
280EncodedJSValue JSC_HOST_CALL functionReadline(ExecState* exec)
281{
282 Vector<char, 256> line;
283 int c;
284 while ((c = getchar()) != EOF) {
285 // FIXME: Should we also break on \r?
286 if (c == '\n')
287 break;
288 line.append(c);
289 }
290 line.append('\0');
291 return JSValue::encode(jsString(exec, line.data()));
292}
293
294EncodedJSValue JSC_HOST_CALL functionQuit(ExecState* exec)
295{
296 // Technically, destroying the heap in the middle of JS execution is a no-no,
297 // but we want to maintain compatibility with the Mozilla test suite, so
298 // we pretend that execution has terminated to avoid ASSERTs, then tear down the heap.
299 exec->globalData().dynamicGlobalObject = 0;
300
301 cleanupGlobalData(&exec->globalData());
302 exit(EXIT_SUCCESS);
303
304#if COMPILER(MSVC) && OS(WINCE)
305 // Without this, Visual Studio will complain that this method does not return a value.
306 return JSValue::encode(jsUndefined());
307#endif
308}
309
310// Use SEH for Release builds only to get rid of the crash report dialog
311// (luckily the same tests fail in Release and Debug builds so far). Need to
312// be in a separate main function because the jscmain function requires object
313// unwinding.
314
315#if COMPILER(MSVC) && !defined(_DEBUG) && !OS(WINCE)
316#define TRY __try {
317#define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
318#else
319#define TRY
320#define EXCEPT(x)
321#endif
322
323int jscmain(int argc, char** argv, JSGlobalData*);
324
325int main(int argc, char** argv)
326{
327#if OS(WINDOWS)
328#if !OS(WINCE)
329 // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
330 // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
331 // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
332 ::SetErrorMode(0);
333#endif
334
335#if defined(_DEBUG)
336 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
337 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
338 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
339 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
340 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
341 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
342#endif
343
344 timeBeginPeriod(1);
345#endif
346
347#if PLATFORM(QT)
348 QCoreApplication app(argc, argv);
349#endif
350
351 // Initialize JSC before getting JSGlobalData.
352 JSC::initializeThreading();
353
354 // We can't use destructors in the following code because it uses Windows
355 // Structured Exception Handling
356 int res = 0;
357 JSGlobalData* globalData = JSGlobalData::create(ThreadStackTypeLarge).leakRef();
358 TRY
359 res = jscmain(argc, argv, globalData);
360 EXCEPT(res = 3)
361
362 cleanupGlobalData(globalData);
363 return res;
364}
365
366static void cleanupGlobalData(JSGlobalData* globalData)
367{
368 JSLock lock(SilenceAssertionsOnly);
369 globalData->heap.destroy();
370 globalData->deref();
371}
372
373static bool runWithScripts(GlobalObject* globalObject, const Vector<Script>& scripts, bool dump)
374{
375 UString script;
376 UString fileName;
377 Vector<char> scriptBuffer;
378
379 if (dump)
380 BytecodeGenerator::setDumpsGeneratedCode(true);
381
382 JSGlobalData& globalData = globalObject->globalData();
383
384#if ENABLE(SAMPLING_FLAGS)
385 SamplingFlags::start();
386#endif
387
388 bool success = true;
389 for (size_t i = 0; i < scripts.size(); i++) {
390 if (scripts[i].isFile) {
391 fileName = scripts[i].argument;
392 if (!fillBufferWithContentsOfFile(fileName, scriptBuffer))
393 return false; // fail early so we can catch missing files
394 script = scriptBuffer.data();
395 } else {
396 script = scripts[i].argument;
397 fileName = "[Command Line]";
398 }
399
400 globalData.startSampling();
401
402 Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script, fileName));
403 success = success && completion.complType() != Throw;
404 if (dump) {
405 if (completion.complType() == Throw)
406 printf("Exception: %s\n", completion.value().toString(globalObject->globalExec()).utf8().data());
407 else
408 printf("End: %s\n", completion.value().toString(globalObject->globalExec()).utf8().data());
409 }
410
411 globalData.stopSampling();
412 globalObject->globalExec()->clearException();
413 }
414
415#if ENABLE(SAMPLING_FLAGS)
416 SamplingFlags::stop();
417#endif
418 globalData.dumpSampleData(globalObject->globalExec());
419#if ENABLE(SAMPLING_COUNTERS)
420 AbstractSamplingCounter::dump();
421#endif
422#if ENABLE(REGEXP_TRACING)
423 globalData.dumpRegExpTrace();
424#endif
425 return success;
426}
427
428#define RUNNING_FROM_XCODE 0
429
430static void runInteractive(GlobalObject* globalObject)
431{
432 while (true) {
433#if HAVE(READLINE) && !RUNNING_FROM_XCODE
434 char* line = readline(interactivePrompt);
435 if (!line)
436 break;
437 if (line[0])
438 add_history(line);
439 Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line, interpreterName));
440 free(line);
441#else
442 printf("%s", interactivePrompt);
443 Vector<char, 256> line;
444 int c;
445 while ((c = getchar()) != EOF) {
446 // FIXME: Should we also break on \r?
447 if (c == '\n')
448 break;
449 line.append(c);
450 }
451 if (line.isEmpty())
452 break;
453 line.append('\0');
454 Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line.data(), interpreterName));
455#endif
456 if (completion.complType() == Throw)
457 printf("Exception: %s\n", completion.value().toString(globalObject->globalExec()).utf8().data());
458 else
459 printf("%s\n", completion.value().toString(globalObject->globalExec()).utf8().data());
460
461 globalObject->globalExec()->clearException();
462 }
463 printf("\n");
464}
465
466static NO_RETURN void printUsageStatement(JSGlobalData* globalData, bool help = false)
467{
468 fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
469 fprintf(stderr, " -d Dumps bytecode (debug builds only)\n");
470 fprintf(stderr, " -e Evaluate argument as script code\n");
471 fprintf(stderr, " -f Specifies a source file (deprecated)\n");
472 fprintf(stderr, " -h|--help Prints this help message\n");
473 fprintf(stderr, " -i Enables interactive mode (default if no files are specified)\n");
474#if HAVE(SIGNAL_H)
475 fprintf(stderr, " -s Installs signal handlers that exit on a crash (Unix platforms only)\n");
476#endif
477
478 cleanupGlobalData(globalData);
479 exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
480}
481
482static void parseArguments(int argc, char** argv, Options& options, JSGlobalData* globalData)
483{
484 int i = 1;
485 for (; i < argc; ++i) {
486 const char* arg = argv[i];
487 if (!strcmp(arg, "-f")) {
488 if (++i == argc)
489 printUsageStatement(globalData);
490 options.scripts.append(Script(true, argv[i]));
491 continue;
492 }
493 if (!strcmp(arg, "-e")) {
494 if (++i == argc)
495 printUsageStatement(globalData);
496 options.scripts.append(Script(false, argv[i]));
497 continue;
498 }
499 if (!strcmp(arg, "-i")) {
500 options.interactive = true;
501 continue;
502 }
503 if (!strcmp(arg, "-d")) {
504 options.dump = true;
505 continue;
506 }
507 if (!strcmp(arg, "-s")) {
508#if HAVE(SIGNAL_H)
509 signal(SIGILL, _exit);
510 signal(SIGFPE, _exit);
511 signal(SIGBUS, _exit);
512 signal(SIGSEGV, _exit);
513#endif
514 continue;
515 }
516 if (!strcmp(arg, "--")) {
517 ++i;
518 break;
519 }
520 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
521 printUsageStatement(globalData, true);
522 options.scripts.append(Script(true, argv[i]));
523 }
524
525 if (options.scripts.isEmpty())
526 options.interactive = true;
527
528 for (; i < argc; ++i)
529 options.arguments.append(argv[i]);
530}
531
532int jscmain(int argc, char** argv, JSGlobalData* globalData)
533{
534 JSLock lock(SilenceAssertionsOnly);
535
536 Options options;
537 parseArguments(argc, argv, options, globalData);
538
539 GlobalObject* globalObject = new (globalData) GlobalObject(*globalData, options.arguments);
540 bool success = runWithScripts(globalObject, options.scripts, options.dump);
541 if (options.interactive && success)
542 runInteractive(globalObject);
543
544 return success ? 0 : 3;
545}
546
547static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer)
548{
549 FILE* f = fopen(fileName.utf8().data(), "r");
550 if (!f) {
551 fprintf(stderr, "Could not open file: %s\n", fileName.utf8().data());
552 return false;
553 }
554
555 size_t bufferSize = 0;
556 size_t bufferCapacity = 1024;
557
558 buffer.resize(bufferCapacity);
559
560 while (!feof(f) && !ferror(f)) {
561 bufferSize += fread(buffer.data() + bufferSize, 1, bufferCapacity - bufferSize, f);
562 if (bufferSize == bufferCapacity) { // guarantees space for trailing '\0'
563 bufferCapacity *= 2;
564 buffer.resize(bufferCapacity);
565 }
566 }
567 fclose(f);
568 buffer[bufferSize] = '\0';
569
570 if (buffer[0] == '#' && buffer[1] == '!')
571 buffer[0] = buffer[1] = '/';
572
573 return true;
574}
Note: See TracBrowser for help on using the repository browser.