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

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

JSC should be able to sample itself in a more flexible way than just sampling flags
https://bugs.webkit.org/show_bug.cgi?id=71522

Source/JavaScriptCore:

Reviewed by Gavin Barraclough.

Added a construct that looks like SamplingRegion samplingRegion("name").

(JSC::SamplingRegion::Locker::Locker):
(JSC::SamplingRegion::Locker::~Locker):
(JSC::SamplingRegion::sample):
(JSC::SamplingRegion::dump):
(JSC::SamplingRegion::dumpInternal):
(JSC::SamplingThread::threadStartFunc):

  • bytecode/SamplingTool.h:

(JSC::SamplingRegion::SamplingRegion):
(JSC::SamplingRegion::~SamplingRegion):
(JSC::SamplingRegion::exchangeCurrent):

  • bytecompiler/BytecodeGenerator.cpp:

(JSC::BytecodeGenerator::generate):

  • dfg/DFGDriver.cpp:

(JSC::DFG::compile):

  • heap/Heap.cpp:

(JSC::Heap::markRoots):
(JSC::Heap::collect):

  • heap/VTableSpectrum.cpp:

(JSC::VTableSpectrum::countVPtr):
(JSC::VTableSpectrum::dump):

  • heap/VTableSpectrum.h:
  • jsc.cpp:

(main):
(runWithScripts):

  • parser/Parser.h:

(JSC::parse):

  • runtime/Executable.cpp:

(JSC::EvalExecutable::compileInternal):
(JSC::ProgramExecutable::compileInternal):
(JSC::FunctionExecutable::compileForCallInternal):
(JSC::FunctionExecutable::compileForConstructInternal):

  • wtf/Atomics.h:

(WTF::weakCompareAndSwap):

  • wtf/Platform.h:
  • wtf/Spectrum.h: Added.

(WTF::Spectrum::Spectrum):
(WTF::Spectrum::add):
(WTF::Spectrum::get):
(WTF::Spectrum::begin):
(WTF::Spectrum::end):
(WTF::Spectrum::KeyAndCount::KeyAndCount):
(WTF::Spectrum::KeyAndCount::operator<):
(WTF::Spectrum::buildList):

  • wtf/wtf.pri:

Source/JavaScriptGlue:

Reviewed by Gavin Barraclough.

  • ForwardingHeaders/wtf/Spectrum.h: Added.

Source/WebCore:

Reviewed by Gavin Barraclough.

No new tests, since no functionality changed.

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