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