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 <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 PLATFORM(IOS)
|
---|
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.ustring(), 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.ustring(), 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 | JSLock lock(SilenceAssertionsOnly);
|
---|
298 | exec->heap()->collectAllGarbage();
|
---|
299 | return JSValue::encode(jsUndefined());
|
---|
300 | }
|
---|
301 |
|
---|
302 | #ifndef NDEBUG
|
---|
303 | EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState* exec)
|
---|
304 | {
|
---|
305 | JSLock lock(SilenceAssertionsOnly);
|
---|
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 PLATFORM(IOS)
|
---|
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 | #if ENABLE(SAMPLING_REGIONS)
|
---|
480 | WTF::initializeMainThread();
|
---|
481 | #endif
|
---|
482 | JSC::initializeThreading();
|
---|
483 |
|
---|
484 | // We can't use destructors in the following code because it uses Windows
|
---|
485 | // Structured Exception Handling
|
---|
486 | int res = 0;
|
---|
487 | TRY
|
---|
488 | res = jscmain(argc, argv);
|
---|
489 | EXCEPT(res = 3)
|
---|
490 | return res;
|
---|
491 | }
|
---|
492 |
|
---|
493 | static bool runWithScripts(GlobalObject* globalObject, const Vector<Script>& scripts, bool dump)
|
---|
494 | {
|
---|
495 | const char* script;
|
---|
496 | UString fileName;
|
---|
497 | Vector<char> scriptBuffer;
|
---|
498 |
|
---|
499 | if (dump)
|
---|
500 | BytecodeGenerator::setDumpsGeneratedCode(true);
|
---|
501 |
|
---|
502 | JSGlobalData& globalData = globalObject->globalData();
|
---|
503 |
|
---|
504 | #if ENABLE(SAMPLING_FLAGS)
|
---|
505 | SamplingFlags::start();
|
---|
506 | #endif
|
---|
507 |
|
---|
508 | bool success = true;
|
---|
509 | for (size_t i = 0; i < scripts.size(); i++) {
|
---|
510 | if (scripts[i].isFile) {
|
---|
511 | fileName = scripts[i].argument;
|
---|
512 | if (!fillBufferWithContentsOfFile(fileName, scriptBuffer))
|
---|
513 | return false; // fail early so we can catch missing files
|
---|
514 | script = scriptBuffer.data();
|
---|
515 | } else {
|
---|
516 | script = scripts[i].argument;
|
---|
517 | fileName = "[Command Line]";
|
---|
518 | }
|
---|
519 |
|
---|
520 | globalData.startSampling();
|
---|
521 |
|
---|
522 | JSValue evaluationException;
|
---|
523 | JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(script, fileName), JSValue(), &evaluationException);
|
---|
524 | success = success && !evaluationException;
|
---|
525 | if (dump && !evaluationException)
|
---|
526 | printf("End: %s\n", returnValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
527 | if (evaluationException) {
|
---|
528 | printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
529 | Identifier stackID(globalObject->globalExec(), "stack");
|
---|
530 | JSValue stackValue = evaluationException.get(globalObject->globalExec(), stackID);
|
---|
531 | if (!stackValue.isUndefinedOrNull())
|
---|
532 | printf("%s\n", stackValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
533 | }
|
---|
534 |
|
---|
535 | globalData.stopSampling();
|
---|
536 | globalObject->globalExec()->clearException();
|
---|
537 | }
|
---|
538 |
|
---|
539 | #if ENABLE(SAMPLING_FLAGS)
|
---|
540 | SamplingFlags::stop();
|
---|
541 | #endif
|
---|
542 | #if ENABLE(SAMPLING_REGIONS)
|
---|
543 | SamplingRegion::dump();
|
---|
544 | #endif
|
---|
545 | globalData.dumpSampleData(globalObject->globalExec());
|
---|
546 | #if ENABLE(SAMPLING_COUNTERS)
|
---|
547 | AbstractSamplingCounter::dump();
|
---|
548 | #endif
|
---|
549 | #if ENABLE(REGEXP_TRACING)
|
---|
550 | globalData.dumpRegExpTrace();
|
---|
551 | #endif
|
---|
552 | return success;
|
---|
553 | }
|
---|
554 |
|
---|
555 | #define RUNNING_FROM_XCODE 0
|
---|
556 |
|
---|
557 | static void runInteractive(GlobalObject* globalObject)
|
---|
558 | {
|
---|
559 | UString interpreterName("Interpreter");
|
---|
560 |
|
---|
561 | while (true) {
|
---|
562 | #if HAVE(READLINE) && !RUNNING_FROM_XCODE
|
---|
563 | char* line = readline(interactivePrompt);
|
---|
564 | if (!line)
|
---|
565 | break;
|
---|
566 | if (line[0])
|
---|
567 | add_history(line);
|
---|
568 | JSValue evaluationException;
|
---|
569 | JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(line, interpreterName), JSValue(), &evaluationException);
|
---|
570 | free(line);
|
---|
571 | #else
|
---|
572 | printf("%s", interactivePrompt);
|
---|
573 | Vector<char, 256> line;
|
---|
574 | int c;
|
---|
575 | while ((c = getchar()) != EOF) {
|
---|
576 | // FIXME: Should we also break on \r?
|
---|
577 | if (c == '\n')
|
---|
578 | break;
|
---|
579 | line.append(c);
|
---|
580 | }
|
---|
581 | if (line.isEmpty())
|
---|
582 | break;
|
---|
583 | line.append('\0');
|
---|
584 |
|
---|
585 | JSValue evaluationException;
|
---|
586 | JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(line.data(), interpreterName), JSValue(), &evaluationException);
|
---|
587 | #endif
|
---|
588 | if (evaluationException)
|
---|
589 | printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
590 | else
|
---|
591 | printf("%s\n", returnValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
592 |
|
---|
593 | globalObject->globalExec()->clearException();
|
---|
594 | }
|
---|
595 | printf("\n");
|
---|
596 | }
|
---|
597 |
|
---|
598 | static NO_RETURN void printUsageStatement(bool help = false)
|
---|
599 | {
|
---|
600 | fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
|
---|
601 | fprintf(stderr, " -d Dumps bytecode (debug builds only)\n");
|
---|
602 | fprintf(stderr, " -e Evaluate argument as script code\n");
|
---|
603 | fprintf(stderr, " -f Specifies a source file (deprecated)\n");
|
---|
604 | fprintf(stderr, " -h|--help Prints this help message\n");
|
---|
605 | fprintf(stderr, " -i Enables interactive mode (default if no files are specified)\n");
|
---|
606 | #if HAVE(SIGNAL_H)
|
---|
607 | fprintf(stderr, " -s Installs signal handlers that exit on a crash (Unix platforms only)\n");
|
---|
608 | #endif
|
---|
609 |
|
---|
610 | exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
|
---|
611 | }
|
---|
612 |
|
---|
613 | static void parseArguments(int argc, char** argv, CommandLine& options)
|
---|
614 | {
|
---|
615 | int i = 1;
|
---|
616 | for (; i < argc; ++i) {
|
---|
617 | const char* arg = argv[i];
|
---|
618 | if (!strcmp(arg, "-f")) {
|
---|
619 | if (++i == argc)
|
---|
620 | printUsageStatement();
|
---|
621 | options.scripts.append(Script(true, argv[i]));
|
---|
622 | continue;
|
---|
623 | }
|
---|
624 | if (!strcmp(arg, "-e")) {
|
---|
625 | if (++i == argc)
|
---|
626 | printUsageStatement();
|
---|
627 | options.scripts.append(Script(false, argv[i]));
|
---|
628 | continue;
|
---|
629 | }
|
---|
630 | if (!strcmp(arg, "-i")) {
|
---|
631 | options.interactive = true;
|
---|
632 | continue;
|
---|
633 | }
|
---|
634 | if (!strcmp(arg, "-d")) {
|
---|
635 | options.dump = true;
|
---|
636 | continue;
|
---|
637 | }
|
---|
638 | if (!strcmp(arg, "-s")) {
|
---|
639 | #if HAVE(SIGNAL_H)
|
---|
640 | signal(SIGILL, _exit);
|
---|
641 | signal(SIGFPE, _exit);
|
---|
642 | signal(SIGBUS, _exit);
|
---|
643 | signal(SIGSEGV, _exit);
|
---|
644 | #endif
|
---|
645 | continue;
|
---|
646 | }
|
---|
647 | if (!strcmp(arg, "--")) {
|
---|
648 | ++i;
|
---|
649 | break;
|
---|
650 | }
|
---|
651 | if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
|
---|
652 | printUsageStatement(true);
|
---|
653 | options.scripts.append(Script(true, argv[i]));
|
---|
654 | }
|
---|
655 |
|
---|
656 | if (options.scripts.isEmpty())
|
---|
657 | options.interactive = true;
|
---|
658 |
|
---|
659 | for (; i < argc; ++i)
|
---|
660 | options.arguments.append(argv[i]);
|
---|
661 | }
|
---|
662 |
|
---|
663 | int jscmain(int argc, char** argv)
|
---|
664 | {
|
---|
665 | JSLock lock(SilenceAssertionsOnly);
|
---|
666 |
|
---|
667 | RefPtr<JSGlobalData> globalData = JSGlobalData::create(ThreadStackTypeLarge, LargeHeap);
|
---|
668 |
|
---|
669 | CommandLine options;
|
---|
670 | parseArguments(argc, argv, options);
|
---|
671 |
|
---|
672 | GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);
|
---|
673 | bool success = runWithScripts(globalObject, options.scripts, options.dump);
|
---|
674 | if (options.interactive && success)
|
---|
675 | runInteractive(globalObject);
|
---|
676 |
|
---|
677 | return success ? 0 : 3;
|
---|
678 | }
|
---|
679 |
|
---|
680 | static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer)
|
---|
681 | {
|
---|
682 | FILE* f = fopen(fileName.utf8().data(), "r");
|
---|
683 | if (!f) {
|
---|
684 | fprintf(stderr, "Could not open file: %s\n", fileName.utf8().data());
|
---|
685 | return false;
|
---|
686 | }
|
---|
687 |
|
---|
688 | size_t bufferSize = 0;
|
---|
689 | size_t bufferCapacity = 1024;
|
---|
690 |
|
---|
691 | buffer.resize(bufferCapacity);
|
---|
692 |
|
---|
693 | while (!feof(f) && !ferror(f)) {
|
---|
694 | bufferSize += fread(buffer.data() + bufferSize, 1, bufferCapacity - bufferSize, f);
|
---|
695 | if (bufferSize == bufferCapacity) { // guarantees space for trailing '\0'
|
---|
696 | bufferCapacity *= 2;
|
---|
697 | buffer.resize(bufferCapacity);
|
---|
698 | }
|
---|
699 | }
|
---|
700 | fclose(f);
|
---|
701 | buffer[bufferSize] = '\0';
|
---|
702 |
|
---|
703 | if (buffer[0] == '#' && buffer[1] == '!')
|
---|
704 | buffer[0] = buffer[1] = '/';
|
---|
705 |
|
---|
706 | return true;
|
---|
707 | }
|
---|