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