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