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