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