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