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