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 "ButterflyInlines.h"
|
---|
26 | #include "BytecodeGenerator.h"
|
---|
27 | #include "Completion.h"
|
---|
28 | #include "CopiedSpaceInlines.h"
|
---|
29 | #include "ExceptionHelpers.h"
|
---|
30 | #include "HeapStatistics.h"
|
---|
31 | #include "InitializeThreading.h"
|
---|
32 | #include "Interpreter.h"
|
---|
33 | #include "JSArray.h"
|
---|
34 | #include "JSArrayBuffer.h"
|
---|
35 | #include "JSCInlines.h"
|
---|
36 | #include "JSFunction.h"
|
---|
37 | #include "JSLock.h"
|
---|
38 | #include "JSProxy.h"
|
---|
39 | #include "JSString.h"
|
---|
40 | #include "ProfilerDatabase.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 <thread>
|
---|
50 | #include <wtf/CurrentTime.h>
|
---|
51 | #include <wtf/MainThread.h>
|
---|
52 | #include <wtf/StringPrintStream.h>
|
---|
53 | #include <wtf/text/StringBuilder.h>
|
---|
54 |
|
---|
55 | #if !OS(WINDOWS)
|
---|
56 | #include <unistd.h>
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #if HAVE(READLINE)
|
---|
60 | // readline/history.h has a Function typedef which conflicts with the WTF::Function template from WTF/Forward.h
|
---|
61 | // We #define it to something else to avoid this conflict.
|
---|
62 | #define Function ReadlineFunction
|
---|
63 | #include <readline/history.h>
|
---|
64 | #include <readline/readline.h>
|
---|
65 | #undef Function
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #if HAVE(SYS_TIME_H)
|
---|
69 | #include <sys/time.h>
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | #if HAVE(SIGNAL_H)
|
---|
73 | #include <signal.h>
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | #if COMPILER(MSVC) && !OS(WINCE)
|
---|
77 | #include <crtdbg.h>
|
---|
78 | #include <mmsystem.h>
|
---|
79 | #include <windows.h>
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | #if PLATFORM(IOS) && CPU(ARM_THUMB2)
|
---|
83 | #include <fenv.h>
|
---|
84 | #include <arm/arch.h>
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #if PLATFORM(EFL)
|
---|
88 | #include <Ecore.h>
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | using namespace JSC;
|
---|
92 | using namespace WTF;
|
---|
93 |
|
---|
94 | namespace {
|
---|
95 |
|
---|
96 | class Element;
|
---|
97 | class ElementHandleOwner;
|
---|
98 | class Masuqerader;
|
---|
99 | class Root;
|
---|
100 |
|
---|
101 | class Element : public JSNonFinalObject {
|
---|
102 | public:
|
---|
103 | Element(VM& vm, Structure* structure, Root* root)
|
---|
104 | : Base(vm, structure)
|
---|
105 | , m_root(root)
|
---|
106 | {
|
---|
107 | }
|
---|
108 |
|
---|
109 | typedef JSNonFinalObject Base;
|
---|
110 | static const bool needsDestruction = false;
|
---|
111 |
|
---|
112 | Root* root() const { return m_root; }
|
---|
113 | void setRoot(Root* root) { m_root = root; }
|
---|
114 |
|
---|
115 | static Element* create(VM& vm, JSGlobalObject* globalObject, Root* root)
|
---|
116 | {
|
---|
117 | Structure* structure = createStructure(vm, globalObject, jsNull());
|
---|
118 | Element* element = new (NotNull, allocateCell<Element>(vm.heap, sizeof(Element))) Element(vm, structure, root);
|
---|
119 | element->finishCreation(vm);
|
---|
120 | return element;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void finishCreation(VM&);
|
---|
124 |
|
---|
125 | static ElementHandleOwner* handleOwner();
|
---|
126 |
|
---|
127 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
|
---|
128 | {
|
---|
129 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
|
---|
130 | }
|
---|
131 |
|
---|
132 | DECLARE_INFO;
|
---|
133 |
|
---|
134 | private:
|
---|
135 | Root* m_root;
|
---|
136 | };
|
---|
137 |
|
---|
138 | class ElementHandleOwner : public WeakHandleOwner {
|
---|
139 | public:
|
---|
140 | virtual bool isReachableFromOpaqueRoots(Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
|
---|
141 | {
|
---|
142 | Element* element = jsCast<Element*>(handle.slot()->asCell());
|
---|
143 | return visitor.containsOpaqueRoot(element->root());
|
---|
144 | }
|
---|
145 | };
|
---|
146 |
|
---|
147 | class Masquerader : public JSNonFinalObject {
|
---|
148 | public:
|
---|
149 | Masquerader(VM& vm, Structure* structure)
|
---|
150 | : Base(vm, structure)
|
---|
151 | {
|
---|
152 | }
|
---|
153 |
|
---|
154 | typedef JSNonFinalObject Base;
|
---|
155 |
|
---|
156 | static Masquerader* create(VM& vm, JSGlobalObject* globalObject)
|
---|
157 | {
|
---|
158 | globalObject->masqueradesAsUndefinedWatchpoint()->fireAll();
|
---|
159 | Structure* structure = createStructure(vm, globalObject, jsNull());
|
---|
160 | Masquerader* result = new (NotNull, allocateCell<Masquerader>(vm.heap, sizeof(Masquerader))) Masquerader(vm, structure);
|
---|
161 | result->finishCreation(vm);
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 |
|
---|
165 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
|
---|
166 | {
|
---|
167 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
|
---|
168 | }
|
---|
169 |
|
---|
170 | DECLARE_INFO;
|
---|
171 |
|
---|
172 | protected:
|
---|
173 | static const unsigned StructureFlags = JSC::MasqueradesAsUndefined | Base::StructureFlags;
|
---|
174 | };
|
---|
175 |
|
---|
176 | class Root : public JSDestructibleObject {
|
---|
177 | public:
|
---|
178 | Root(VM& vm, Structure* structure)
|
---|
179 | : Base(vm, structure)
|
---|
180 | {
|
---|
181 | }
|
---|
182 |
|
---|
183 | Element* element()
|
---|
184 | {
|
---|
185 | return m_element.get();
|
---|
186 | }
|
---|
187 |
|
---|
188 | void setElement(Element* element)
|
---|
189 | {
|
---|
190 | Weak<Element> newElement(element, Element::handleOwner());
|
---|
191 | m_element.swap(newElement);
|
---|
192 | }
|
---|
193 |
|
---|
194 | static Root* create(VM& vm, JSGlobalObject* globalObject)
|
---|
195 | {
|
---|
196 | Structure* structure = createStructure(vm, globalObject, jsNull());
|
---|
197 | Root* root = new (NotNull, allocateCell<Root>(vm.heap, sizeof(Root))) Root(vm, structure);
|
---|
198 | root->finishCreation(vm);
|
---|
199 | return root;
|
---|
200 | }
|
---|
201 |
|
---|
202 | typedef JSDestructibleObject Base;
|
---|
203 |
|
---|
204 | DECLARE_INFO;
|
---|
205 | static const bool needsDestruction = true;
|
---|
206 |
|
---|
207 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
|
---|
208 | {
|
---|
209 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
|
---|
210 | }
|
---|
211 |
|
---|
212 | static void visitChildren(JSCell* thisObject, SlotVisitor& visitor)
|
---|
213 | {
|
---|
214 | Base::visitChildren(thisObject, visitor);
|
---|
215 | visitor.addOpaqueRoot(thisObject);
|
---|
216 | }
|
---|
217 |
|
---|
218 | private:
|
---|
219 | Weak<Element> m_element;
|
---|
220 | };
|
---|
221 |
|
---|
222 | const ClassInfo Element::s_info = { "Element", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(Element) };
|
---|
223 | const ClassInfo Masquerader::s_info = { "Masquerader", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(Masquerader) };
|
---|
224 | const ClassInfo Root::s_info = { "Root", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(Root) };
|
---|
225 |
|
---|
226 | ElementHandleOwner* Element::handleOwner()
|
---|
227 | {
|
---|
228 | static ElementHandleOwner* owner = 0;
|
---|
229 | if (!owner)
|
---|
230 | owner = new ElementHandleOwner();
|
---|
231 | return owner;
|
---|
232 | }
|
---|
233 |
|
---|
234 | void Element::finishCreation(VM& vm)
|
---|
235 | {
|
---|
236 | Base::finishCreation(vm);
|
---|
237 | m_root->setElement(this);
|
---|
238 | }
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|
242 | static bool fillBufferWithContentsOfFile(const String& fileName, Vector<char>& buffer);
|
---|
243 |
|
---|
244 | static EncodedJSValue JSC_HOST_CALL functionSetElementRoot(ExecState*);
|
---|
245 | static EncodedJSValue JSC_HOST_CALL functionCreateRoot(ExecState*);
|
---|
246 | static EncodedJSValue JSC_HOST_CALL functionCreateElement(ExecState*);
|
---|
247 | static EncodedJSValue JSC_HOST_CALL functionGetElement(ExecState*);
|
---|
248 | static EncodedJSValue JSC_HOST_CALL functionPrint(ExecState*);
|
---|
249 | static EncodedJSValue JSC_HOST_CALL functionDebug(ExecState*);
|
---|
250 | static EncodedJSValue JSC_HOST_CALL functionDescribe(ExecState*);
|
---|
251 | static EncodedJSValue JSC_HOST_CALL functionDescribeArray(ExecState*);
|
---|
252 | static EncodedJSValue JSC_HOST_CALL functionJSCStack(ExecState*);
|
---|
253 | static EncodedJSValue JSC_HOST_CALL functionGCAndSweep(ExecState*);
|
---|
254 | static EncodedJSValue JSC_HOST_CALL functionFullGC(ExecState*);
|
---|
255 | static EncodedJSValue JSC_HOST_CALL functionEdenGC(ExecState*);
|
---|
256 | #ifndef NDEBUG
|
---|
257 | static EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState*);
|
---|
258 | static EncodedJSValue JSC_HOST_CALL functionDumpCallFrame(ExecState*);
|
---|
259 | #endif
|
---|
260 | static EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*);
|
---|
261 | static EncodedJSValue JSC_HOST_CALL functionRun(ExecState*);
|
---|
262 | static EncodedJSValue JSC_HOST_CALL functionLoad(ExecState*);
|
---|
263 | static EncodedJSValue JSC_HOST_CALL functionReadFile(ExecState*);
|
---|
264 | static EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState*);
|
---|
265 | static EncodedJSValue JSC_HOST_CALL functionReadline(ExecState*);
|
---|
266 | static EncodedJSValue JSC_HOST_CALL functionPreciseTime(ExecState*);
|
---|
267 | static EncodedJSValue JSC_HOST_CALL functionNeverInlineFunction(ExecState*);
|
---|
268 | static EncodedJSValue JSC_HOST_CALL functionNumberOfDFGCompiles(ExecState*);
|
---|
269 | static EncodedJSValue JSC_HOST_CALL functionReoptimizationRetryCount(ExecState*);
|
---|
270 | static EncodedJSValue JSC_HOST_CALL functionTransferArrayBuffer(ExecState*);
|
---|
271 | static NO_RETURN_WITH_VALUE EncodedJSValue JSC_HOST_CALL functionQuit(ExecState*);
|
---|
272 | static EncodedJSValue JSC_HOST_CALL functionFalse(ExecState*);
|
---|
273 | static EncodedJSValue JSC_HOST_CALL functionEffectful42(ExecState*);
|
---|
274 | static EncodedJSValue JSC_HOST_CALL functionMakeMasquerader(ExecState*);
|
---|
275 |
|
---|
276 | #if ENABLE(SAMPLING_FLAGS)
|
---|
277 | static EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState*);
|
---|
278 | static EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState*);
|
---|
279 | #endif
|
---|
280 |
|
---|
281 | struct Script {
|
---|
282 | bool isFile;
|
---|
283 | char* argument;
|
---|
284 |
|
---|
285 | Script(bool isFile, char *argument)
|
---|
286 | : isFile(isFile)
|
---|
287 | , argument(argument)
|
---|
288 | {
|
---|
289 | }
|
---|
290 | };
|
---|
291 |
|
---|
292 | class CommandLine {
|
---|
293 | public:
|
---|
294 | CommandLine(int argc, char** argv)
|
---|
295 | : m_interactive(false)
|
---|
296 | , m_dump(false)
|
---|
297 | , m_exitCode(false)
|
---|
298 | , m_profile(false)
|
---|
299 | {
|
---|
300 | parseArguments(argc, argv);
|
---|
301 | }
|
---|
302 |
|
---|
303 | bool m_interactive;
|
---|
304 | bool m_dump;
|
---|
305 | bool m_exitCode;
|
---|
306 | Vector<Script> m_scripts;
|
---|
307 | Vector<String> m_arguments;
|
---|
308 | bool m_profile;
|
---|
309 | String m_profilerOutput;
|
---|
310 |
|
---|
311 | void parseArguments(int, char**);
|
---|
312 | };
|
---|
313 |
|
---|
314 | static const char interactivePrompt[] = ">>> ";
|
---|
315 |
|
---|
316 | class StopWatch {
|
---|
317 | public:
|
---|
318 | void start();
|
---|
319 | void stop();
|
---|
320 | long getElapsedMS(); // call stop() first
|
---|
321 |
|
---|
322 | private:
|
---|
323 | double m_startTime;
|
---|
324 | double m_stopTime;
|
---|
325 | };
|
---|
326 |
|
---|
327 | void StopWatch::start()
|
---|
328 | {
|
---|
329 | m_startTime = monotonicallyIncreasingTime();
|
---|
330 | }
|
---|
331 |
|
---|
332 | void StopWatch::stop()
|
---|
333 | {
|
---|
334 | m_stopTime = monotonicallyIncreasingTime();
|
---|
335 | }
|
---|
336 |
|
---|
337 | long StopWatch::getElapsedMS()
|
---|
338 | {
|
---|
339 | return static_cast<long>((m_stopTime - m_startTime) * 1000);
|
---|
340 | }
|
---|
341 |
|
---|
342 | class GlobalObject : public JSGlobalObject {
|
---|
343 | private:
|
---|
344 | GlobalObject(VM&, Structure*);
|
---|
345 |
|
---|
346 | public:
|
---|
347 | typedef JSGlobalObject Base;
|
---|
348 |
|
---|
349 | static GlobalObject* create(VM& vm, Structure* structure, const Vector<String>& arguments)
|
---|
350 | {
|
---|
351 | GlobalObject* object = new (NotNull, allocateCell<GlobalObject>(vm.heap)) GlobalObject(vm, structure);
|
---|
352 | object->finishCreation(vm, arguments);
|
---|
353 | vm.heap.addFinalizer(object, destroy);
|
---|
354 | object->setGlobalThis(vm, JSProxy::create(vm, JSProxy::createStructure(vm, object, object->prototype()), object));
|
---|
355 | return object;
|
---|
356 | }
|
---|
357 |
|
---|
358 | static const bool needsDestruction = false;
|
---|
359 |
|
---|
360 | DECLARE_INFO;
|
---|
361 | static const GlobalObjectMethodTable s_globalObjectMethodTable;
|
---|
362 |
|
---|
363 | static Structure* createStructure(VM& vm, JSValue prototype)
|
---|
364 | {
|
---|
365 | return Structure::create(vm, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), info());
|
---|
366 | }
|
---|
367 |
|
---|
368 | static bool javaScriptExperimentsEnabled(const JSGlobalObject*) { return true; }
|
---|
369 |
|
---|
370 | protected:
|
---|
371 | void finishCreation(VM& vm, const Vector<String>& arguments)
|
---|
372 | {
|
---|
373 | Base::finishCreation(vm);
|
---|
374 |
|
---|
375 | addFunction(vm, "debug", functionDebug, 1);
|
---|
376 | addFunction(vm, "describe", functionDescribe, 1);
|
---|
377 | addFunction(vm, "describeArray", functionDescribeArray, 1);
|
---|
378 | addFunction(vm, "print", functionPrint, 1);
|
---|
379 | addFunction(vm, "quit", functionQuit, 0);
|
---|
380 | addFunction(vm, "gc", functionGCAndSweep, 0);
|
---|
381 | addFunction(vm, "fullGC", functionFullGC, 0);
|
---|
382 | addFunction(vm, "edenGC", functionEdenGC, 0);
|
---|
383 | #ifndef NDEBUG
|
---|
384 | addFunction(vm, "dumpCallFrame", functionDumpCallFrame, 0);
|
---|
385 | addFunction(vm, "releaseExecutableMemory", functionReleaseExecutableMemory, 0);
|
---|
386 | #endif
|
---|
387 | addFunction(vm, "version", functionVersion, 1);
|
---|
388 | addFunction(vm, "run", functionRun, 1);
|
---|
389 | addFunction(vm, "load", functionLoad, 1);
|
---|
390 | addFunction(vm, "readFile", functionReadFile, 1);
|
---|
391 | addFunction(vm, "checkSyntax", functionCheckSyntax, 1);
|
---|
392 | addFunction(vm, "jscStack", functionJSCStack, 1);
|
---|
393 | addFunction(vm, "readline", functionReadline, 0);
|
---|
394 | addFunction(vm, "preciseTime", functionPreciseTime, 0);
|
---|
395 | addFunction(vm, "neverInlineFunction", functionNeverInlineFunction, 1);
|
---|
396 | addFunction(vm, "noInline", functionNeverInlineFunction, 1);
|
---|
397 | addFunction(vm, "numberOfDFGCompiles", functionNumberOfDFGCompiles, 1);
|
---|
398 | addFunction(vm, "reoptimizationRetryCount", functionReoptimizationRetryCount, 1);
|
---|
399 | addFunction(vm, "transferArrayBuffer", functionTransferArrayBuffer, 1);
|
---|
400 | #if ENABLE(SAMPLING_FLAGS)
|
---|
401 | addFunction(vm, "setSamplingFlags", functionSetSamplingFlags, 1);
|
---|
402 | addFunction(vm, "clearSamplingFlags", functionClearSamplingFlags, 1);
|
---|
403 | #endif
|
---|
404 | addConstructableFunction(vm, "Root", functionCreateRoot, 0);
|
---|
405 | addConstructableFunction(vm, "Element", functionCreateElement, 1);
|
---|
406 | addFunction(vm, "getElement", functionGetElement, 1);
|
---|
407 | addFunction(vm, "setElementRoot", functionSetElementRoot, 2);
|
---|
408 |
|
---|
409 | putDirectNativeFunction(vm, this, Identifier(&vm, "DFGTrue"), 0, functionFalse, DFGTrue, DontEnum | JSC::Function);
|
---|
410 |
|
---|
411 | addFunction(vm, "effectful42", functionEffectful42, 0);
|
---|
412 | addFunction(vm, "makeMasquerader", functionMakeMasquerader, 0);
|
---|
413 |
|
---|
414 | JSArray* array = constructEmptyArray(globalExec(), 0);
|
---|
415 | for (size_t i = 0; i < arguments.size(); ++i)
|
---|
416 | array->putDirectIndex(globalExec(), i, jsString(globalExec(), arguments[i]));
|
---|
417 | putDirect(vm, Identifier(globalExec(), "arguments"), array);
|
---|
418 |
|
---|
419 | putDirect(vm, Identifier(globalExec(), "console"), jsUndefined());
|
---|
420 | }
|
---|
421 |
|
---|
422 | void addFunction(VM& vm, const char* name, NativeFunction function, unsigned arguments)
|
---|
423 | {
|
---|
424 | Identifier identifier(&vm, name);
|
---|
425 | putDirect(vm, identifier, JSFunction::create(vm, this, arguments, identifier.string(), function));
|
---|
426 | }
|
---|
427 |
|
---|
428 | void addConstructableFunction(VM& vm, const char* name, NativeFunction function, unsigned arguments)
|
---|
429 | {
|
---|
430 | Identifier identifier(&vm, name);
|
---|
431 | putDirect(vm, identifier, JSFunction::create(vm, this, arguments, identifier.string(), function, NoIntrinsic, function));
|
---|
432 | }
|
---|
433 | };
|
---|
434 |
|
---|
435 | const ClassInfo GlobalObject::s_info = { "global", &JSGlobalObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(GlobalObject) };
|
---|
436 | const GlobalObjectMethodTable GlobalObject::s_globalObjectMethodTable = { &allowsAccessFrom, &supportsProfiling, &supportsRichSourceInfo, &shouldInterruptScript, &javaScriptExperimentsEnabled, 0, &shouldInterruptScriptBeforeTimeout };
|
---|
437 |
|
---|
438 |
|
---|
439 | GlobalObject::GlobalObject(VM& vm, Structure* structure)
|
---|
440 | : JSGlobalObject(vm, structure, &s_globalObjectMethodTable)
|
---|
441 | {
|
---|
442 | }
|
---|
443 |
|
---|
444 | static inline String stringFromUTF(const char* utf8)
|
---|
445 | {
|
---|
446 | // Find the the first non-ascii character, or nul.
|
---|
447 | const char* pos = utf8;
|
---|
448 | while (*pos > 0)
|
---|
449 | pos++;
|
---|
450 | size_t asciiLength = pos - utf8;
|
---|
451 |
|
---|
452 | // Fast case - string is all ascii.
|
---|
453 | if (!*pos)
|
---|
454 | return String(utf8, asciiLength);
|
---|
455 |
|
---|
456 | // Slow case - contains non-ascii characters, use fromUTF8WithLatin1Fallback.
|
---|
457 | ASSERT(*pos < 0);
|
---|
458 | ASSERT(strlen(utf8) == asciiLength + strlen(pos));
|
---|
459 | return String::fromUTF8WithLatin1Fallback(utf8, asciiLength + strlen(pos));
|
---|
460 | }
|
---|
461 |
|
---|
462 | static inline SourceCode jscSource(const char* utf8, const String& filename)
|
---|
463 | {
|
---|
464 | String str = stringFromUTF(utf8);
|
---|
465 | return makeSource(str, filename);
|
---|
466 | }
|
---|
467 |
|
---|
468 | EncodedJSValue JSC_HOST_CALL functionPrint(ExecState* exec)
|
---|
469 | {
|
---|
470 | for (unsigned i = 0; i < exec->argumentCount(); ++i) {
|
---|
471 | if (i)
|
---|
472 | putchar(' ');
|
---|
473 |
|
---|
474 | printf("%s", exec->uncheckedArgument(i).toString(exec)->value(exec).utf8().data());
|
---|
475 | }
|
---|
476 |
|
---|
477 | putchar('\n');
|
---|
478 | fflush(stdout);
|
---|
479 | return JSValue::encode(jsUndefined());
|
---|
480 | }
|
---|
481 |
|
---|
482 | #ifndef NDEBUG
|
---|
483 | EncodedJSValue JSC_HOST_CALL functionDumpCallFrame(ExecState* exec)
|
---|
484 | {
|
---|
485 | if (!exec->callerFrame()->isVMEntrySentinel())
|
---|
486 | exec->vm().interpreter->dumpCallFrame(exec->callerFrame());
|
---|
487 | return JSValue::encode(jsUndefined());
|
---|
488 | }
|
---|
489 | #endif
|
---|
490 |
|
---|
491 | EncodedJSValue JSC_HOST_CALL functionDebug(ExecState* exec)
|
---|
492 | {
|
---|
493 | fprintf(stderr, "--> %s\n", exec->argument(0).toString(exec)->value(exec).utf8().data());
|
---|
494 | return JSValue::encode(jsUndefined());
|
---|
495 | }
|
---|
496 |
|
---|
497 | EncodedJSValue JSC_HOST_CALL functionDescribe(ExecState* exec)
|
---|
498 | {
|
---|
499 | if (exec->argumentCount() < 1)
|
---|
500 | return JSValue::encode(jsUndefined());
|
---|
501 | return JSValue::encode(jsString(exec, toString(exec->argument(0))));
|
---|
502 | }
|
---|
503 |
|
---|
504 | EncodedJSValue JSC_HOST_CALL functionDescribeArray(ExecState* exec)
|
---|
505 | {
|
---|
506 | if (exec->argumentCount() < 1)
|
---|
507 | return JSValue::encode(jsUndefined());
|
---|
508 | JSObject* object = jsDynamicCast<JSObject*>(exec->argument(0));
|
---|
509 | if (!object)
|
---|
510 | return JSValue::encode(jsString(exec, "<not object>"));
|
---|
511 | return JSValue::encode(jsString(exec, toString("<Public length: ", object->getArrayLength(), "; vector length: ", object->getVectorLength(), ">")));
|
---|
512 | }
|
---|
513 |
|
---|
514 | class FunctionJSCStackFunctor {
|
---|
515 | public:
|
---|
516 | FunctionJSCStackFunctor(StringBuilder& trace)
|
---|
517 | : m_trace(trace)
|
---|
518 | {
|
---|
519 | }
|
---|
520 |
|
---|
521 | StackVisitor::Status operator()(StackVisitor& visitor)
|
---|
522 | {
|
---|
523 | m_trace.append(String::format(" %zu %s\n", visitor->index(), visitor->toString().utf8().data()));
|
---|
524 | return StackVisitor::Continue;
|
---|
525 | }
|
---|
526 |
|
---|
527 | private:
|
---|
528 | StringBuilder& m_trace;
|
---|
529 | };
|
---|
530 |
|
---|
531 | EncodedJSValue JSC_HOST_CALL functionJSCStack(ExecState* exec)
|
---|
532 | {
|
---|
533 | StringBuilder trace;
|
---|
534 | trace.appendLiteral("--> Stack trace:\n");
|
---|
535 |
|
---|
536 | FunctionJSCStackFunctor functor(trace);
|
---|
537 | exec->iterate(functor);
|
---|
538 | fprintf(stderr, "%s", trace.toString().utf8().data());
|
---|
539 | return JSValue::encode(jsUndefined());
|
---|
540 | }
|
---|
541 |
|
---|
542 | EncodedJSValue JSC_HOST_CALL functionCreateRoot(ExecState* exec)
|
---|
543 | {
|
---|
544 | JSLockHolder lock(exec);
|
---|
545 | return JSValue::encode(Root::create(exec->vm(), exec->lexicalGlobalObject()));
|
---|
546 | }
|
---|
547 |
|
---|
548 | EncodedJSValue JSC_HOST_CALL functionCreateElement(ExecState* exec)
|
---|
549 | {
|
---|
550 | JSLockHolder lock(exec);
|
---|
551 | JSValue arg = exec->argument(0);
|
---|
552 | return JSValue::encode(Element::create(exec->vm(), exec->lexicalGlobalObject(), arg.isNull() ? nullptr : jsCast<Root*>(exec->argument(0))));
|
---|
553 | }
|
---|
554 |
|
---|
555 | EncodedJSValue JSC_HOST_CALL functionGetElement(ExecState* exec)
|
---|
556 | {
|
---|
557 | JSLockHolder lock(exec);
|
---|
558 | Element* result = jsCast<Root*>(exec->argument(0).asCell())->element();
|
---|
559 | return JSValue::encode(result ? result : jsUndefined());
|
---|
560 | }
|
---|
561 |
|
---|
562 | EncodedJSValue JSC_HOST_CALL functionSetElementRoot(ExecState* exec)
|
---|
563 | {
|
---|
564 | JSLockHolder lock(exec);
|
---|
565 | Element* element = jsCast<Element*>(exec->argument(0));
|
---|
566 | Root* root = jsCast<Root*>(exec->argument(1));
|
---|
567 | element->setRoot(root);
|
---|
568 | return JSValue::encode(jsUndefined());
|
---|
569 | }
|
---|
570 |
|
---|
571 | EncodedJSValue JSC_HOST_CALL functionGCAndSweep(ExecState* exec)
|
---|
572 | {
|
---|
573 | JSLockHolder lock(exec);
|
---|
574 | exec->heap()->collectAllGarbage();
|
---|
575 | return JSValue::encode(jsUndefined());
|
---|
576 | }
|
---|
577 |
|
---|
578 | EncodedJSValue JSC_HOST_CALL functionFullGC(ExecState* exec)
|
---|
579 | {
|
---|
580 | JSLockHolder lock(exec);
|
---|
581 | exec->heap()->collect(FullCollection);
|
---|
582 | return JSValue::encode(jsUndefined());
|
---|
583 | }
|
---|
584 |
|
---|
585 | EncodedJSValue JSC_HOST_CALL functionEdenGC(ExecState* exec)
|
---|
586 | {
|
---|
587 | JSLockHolder lock(exec);
|
---|
588 | exec->heap()->collect(EdenCollection);
|
---|
589 | return JSValue::encode(jsUndefined());
|
---|
590 | }
|
---|
591 |
|
---|
592 | #ifndef NDEBUG
|
---|
593 | EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState* exec)
|
---|
594 | {
|
---|
595 | JSLockHolder lock(exec);
|
---|
596 | exec->vm().releaseExecutableMemory();
|
---|
597 | return JSValue::encode(jsUndefined());
|
---|
598 | }
|
---|
599 | #endif
|
---|
600 |
|
---|
601 | EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*)
|
---|
602 | {
|
---|
603 | // We need this function for compatibility with the Mozilla JS tests but for now
|
---|
604 | // we don't actually do any version-specific handling
|
---|
605 | return JSValue::encode(jsUndefined());
|
---|
606 | }
|
---|
607 |
|
---|
608 | EncodedJSValue JSC_HOST_CALL functionRun(ExecState* exec)
|
---|
609 | {
|
---|
610 | String fileName = exec->argument(0).toString(exec)->value(exec);
|
---|
611 | Vector<char> script;
|
---|
612 | if (!fillBufferWithContentsOfFile(fileName, script))
|
---|
613 | return JSValue::encode(exec->vm().throwException(exec, createError(exec, "Could not open file.")));
|
---|
614 |
|
---|
615 | GlobalObject* globalObject = GlobalObject::create(exec->vm(), GlobalObject::createStructure(exec->vm(), jsNull()), Vector<String>());
|
---|
616 |
|
---|
617 | JSArray* array = constructEmptyArray(globalObject->globalExec(), 0);
|
---|
618 | for (unsigned i = 1; i < exec->argumentCount(); ++i)
|
---|
619 | array->putDirectIndex(globalObject->globalExec(), i - 1, exec->uncheckedArgument(i));
|
---|
620 | globalObject->putDirect(
|
---|
621 | exec->vm(), Identifier(globalObject->globalExec(), "arguments"), array);
|
---|
622 |
|
---|
623 | JSValue exception;
|
---|
624 | StopWatch stopWatch;
|
---|
625 | stopWatch.start();
|
---|
626 | evaluate(globalObject->globalExec(), jscSource(script.data(), fileName), JSValue(), &exception);
|
---|
627 | stopWatch.stop();
|
---|
628 |
|
---|
629 | if (!!exception) {
|
---|
630 | exec->vm().throwException(globalObject->globalExec(), exception);
|
---|
631 | return JSValue::encode(jsUndefined());
|
---|
632 | }
|
---|
633 |
|
---|
634 | return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
|
---|
635 | }
|
---|
636 |
|
---|
637 | EncodedJSValue JSC_HOST_CALL functionLoad(ExecState* exec)
|
---|
638 | {
|
---|
639 | String fileName = exec->argument(0).toString(exec)->value(exec);
|
---|
640 | Vector<char> script;
|
---|
641 | if (!fillBufferWithContentsOfFile(fileName, script))
|
---|
642 | return JSValue::encode(exec->vm().throwException(exec, createError(exec, "Could not open file.")));
|
---|
643 |
|
---|
644 | JSGlobalObject* globalObject = exec->lexicalGlobalObject();
|
---|
645 |
|
---|
646 | JSValue evaluationException;
|
---|
647 | JSValue result = evaluate(globalObject->globalExec(), jscSource(script.data(), fileName), JSValue(), &evaluationException);
|
---|
648 | if (evaluationException)
|
---|
649 | exec->vm().throwException(exec, evaluationException);
|
---|
650 | return JSValue::encode(result);
|
---|
651 | }
|
---|
652 |
|
---|
653 | EncodedJSValue JSC_HOST_CALL functionReadFile(ExecState* exec)
|
---|
654 | {
|
---|
655 | String fileName = exec->argument(0).toString(exec)->value(exec);
|
---|
656 | Vector<char> script;
|
---|
657 | if (!fillBufferWithContentsOfFile(fileName, script))
|
---|
658 | return JSValue::encode(exec->vm().throwException(exec, createError(exec, "Could not open file.")));
|
---|
659 |
|
---|
660 | return JSValue::encode(jsString(exec, stringFromUTF(script.data())));
|
---|
661 | }
|
---|
662 |
|
---|
663 | EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState* exec)
|
---|
664 | {
|
---|
665 | String fileName = exec->argument(0).toString(exec)->value(exec);
|
---|
666 | Vector<char> script;
|
---|
667 | if (!fillBufferWithContentsOfFile(fileName, script))
|
---|
668 | return JSValue::encode(exec->vm().throwException(exec, createError(exec, "Could not open file.")));
|
---|
669 |
|
---|
670 | JSGlobalObject* globalObject = exec->lexicalGlobalObject();
|
---|
671 |
|
---|
672 | StopWatch stopWatch;
|
---|
673 | stopWatch.start();
|
---|
674 |
|
---|
675 | JSValue syntaxException;
|
---|
676 | bool validSyntax = checkSyntax(globalObject->globalExec(), jscSource(script.data(), fileName), &syntaxException);
|
---|
677 | stopWatch.stop();
|
---|
678 |
|
---|
679 | if (!validSyntax)
|
---|
680 | exec->vm().throwException(exec, syntaxException);
|
---|
681 | return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
|
---|
682 | }
|
---|
683 |
|
---|
684 | #if ENABLE(SAMPLING_FLAGS)
|
---|
685 | EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState* exec)
|
---|
686 | {
|
---|
687 | for (unsigned i = 0; i < exec->argumentCount(); ++i) {
|
---|
688 | unsigned flag = static_cast<unsigned>(exec->uncheckedArgument(i).toNumber(exec));
|
---|
689 | if ((flag >= 1) && (flag <= 32))
|
---|
690 | SamplingFlags::setFlag(flag);
|
---|
691 | }
|
---|
692 | return JSValue::encode(jsNull());
|
---|
693 | }
|
---|
694 |
|
---|
695 | EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState* exec)
|
---|
696 | {
|
---|
697 | for (unsigned i = 0; i < exec->argumentCount(); ++i) {
|
---|
698 | unsigned flag = static_cast<unsigned>(exec->uncheckedArgument(i).toNumber(exec));
|
---|
699 | if ((flag >= 1) && (flag <= 32))
|
---|
700 | SamplingFlags::clearFlag(flag);
|
---|
701 | }
|
---|
702 | return JSValue::encode(jsNull());
|
---|
703 | }
|
---|
704 | #endif
|
---|
705 |
|
---|
706 | EncodedJSValue JSC_HOST_CALL functionReadline(ExecState* exec)
|
---|
707 | {
|
---|
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 | line.append('\0');
|
---|
717 | return JSValue::encode(jsString(exec, line.data()));
|
---|
718 | }
|
---|
719 |
|
---|
720 | EncodedJSValue JSC_HOST_CALL functionPreciseTime(ExecState*)
|
---|
721 | {
|
---|
722 | return JSValue::encode(jsNumber(currentTime()));
|
---|
723 | }
|
---|
724 |
|
---|
725 | EncodedJSValue JSC_HOST_CALL functionNeverInlineFunction(ExecState* exec)
|
---|
726 | {
|
---|
727 | return JSValue::encode(setNeverInline(exec));
|
---|
728 | }
|
---|
729 |
|
---|
730 | EncodedJSValue JSC_HOST_CALL functionNumberOfDFGCompiles(ExecState* exec)
|
---|
731 | {
|
---|
732 | return JSValue::encode(numberOfDFGCompiles(exec));
|
---|
733 | }
|
---|
734 |
|
---|
735 | EncodedJSValue JSC_HOST_CALL functionReoptimizationRetryCount(ExecState* exec)
|
---|
736 | {
|
---|
737 | if (exec->argumentCount() < 1)
|
---|
738 | return JSValue::encode(jsUndefined());
|
---|
739 |
|
---|
740 | CodeBlock* block = getSomeBaselineCodeBlockForFunction(exec->argument(0));
|
---|
741 | if (!block)
|
---|
742 | return JSValue::encode(jsNumber(0));
|
---|
743 |
|
---|
744 | return JSValue::encode(jsNumber(block->reoptimizationRetryCounter()));
|
---|
745 | }
|
---|
746 |
|
---|
747 | EncodedJSValue JSC_HOST_CALL functionTransferArrayBuffer(ExecState* exec)
|
---|
748 | {
|
---|
749 | if (exec->argumentCount() < 1)
|
---|
750 | return JSValue::encode(exec->vm().throwException(exec, createError(exec, "Not enough arguments")));
|
---|
751 |
|
---|
752 | JSArrayBuffer* buffer = jsDynamicCast<JSArrayBuffer*>(exec->argument(0));
|
---|
753 | if (!buffer)
|
---|
754 | return JSValue::encode(exec->vm().throwException(exec, createError(exec, "Expected an array buffer")));
|
---|
755 |
|
---|
756 | ArrayBufferContents dummyContents;
|
---|
757 | buffer->impl()->transfer(dummyContents);
|
---|
758 |
|
---|
759 | return JSValue::encode(jsUndefined());
|
---|
760 | }
|
---|
761 |
|
---|
762 | EncodedJSValue JSC_HOST_CALL functionQuit(ExecState*)
|
---|
763 | {
|
---|
764 | exit(EXIT_SUCCESS);
|
---|
765 |
|
---|
766 | #if COMPILER(MSVC) && OS(WINCE)
|
---|
767 | // Without this, Visual Studio will complain that this method does not return a value.
|
---|
768 | return JSValue::encode(jsUndefined());
|
---|
769 | #endif
|
---|
770 | }
|
---|
771 |
|
---|
772 | EncodedJSValue JSC_HOST_CALL functionFalse(ExecState*)
|
---|
773 | {
|
---|
774 | return JSValue::encode(jsBoolean(false));
|
---|
775 | }
|
---|
776 |
|
---|
777 | EncodedJSValue JSC_HOST_CALL functionEffectful42(ExecState*)
|
---|
778 | {
|
---|
779 | return JSValue::encode(jsNumber(42));
|
---|
780 | }
|
---|
781 |
|
---|
782 | EncodedJSValue JSC_HOST_CALL functionMakeMasquerader(ExecState* exec)
|
---|
783 | {
|
---|
784 | return JSValue::encode(Masquerader::create(exec->vm(), exec->lexicalGlobalObject()));
|
---|
785 | }
|
---|
786 |
|
---|
787 | // Use SEH for Release builds only to get rid of the crash report dialog
|
---|
788 | // (luckily the same tests fail in Release and Debug builds so far). Need to
|
---|
789 | // be in a separate main function because the jscmain function requires object
|
---|
790 | // unwinding.
|
---|
791 |
|
---|
792 | #if COMPILER(MSVC) && !defined(_DEBUG) && !OS(WINCE)
|
---|
793 | #define TRY __try {
|
---|
794 | #define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
|
---|
795 | #else
|
---|
796 | #define TRY
|
---|
797 | #define EXCEPT(x)
|
---|
798 | #endif
|
---|
799 |
|
---|
800 | int jscmain(int argc, char** argv);
|
---|
801 |
|
---|
802 | static double s_desiredTimeout;
|
---|
803 |
|
---|
804 | static NO_RETURN_DUE_TO_CRASH void timeoutThreadMain(void*)
|
---|
805 | {
|
---|
806 | auto timeout = std::chrono::microseconds(static_cast<std::chrono::microseconds::rep>(s_desiredTimeout * 1000000));
|
---|
807 | std::this_thread::sleep_for(timeout);
|
---|
808 |
|
---|
809 | dataLog("Timed out after ", s_desiredTimeout, " seconds!\n");
|
---|
810 | CRASH();
|
---|
811 | }
|
---|
812 |
|
---|
813 | int main(int argc, char** argv)
|
---|
814 | {
|
---|
815 | #if PLATFORM(IOS) && CPU(ARM_THUMB2)
|
---|
816 | // Enabled IEEE754 denormal support.
|
---|
817 | fenv_t env;
|
---|
818 | fegetenv( &env );
|
---|
819 | env.__fpscr &= ~0x01000000u;
|
---|
820 | fesetenv( &env );
|
---|
821 | #endif
|
---|
822 |
|
---|
823 | #if OS(WINDOWS)
|
---|
824 | #if !OS(WINCE)
|
---|
825 | // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
|
---|
826 | // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
|
---|
827 | // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
|
---|
828 | ::SetErrorMode(0);
|
---|
829 |
|
---|
830 | #if defined(_DEBUG)
|
---|
831 | _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
|
---|
832 | _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
|
---|
833 | _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
|
---|
834 | _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
|
---|
835 | _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
|
---|
836 | _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
|
---|
837 | #endif
|
---|
838 | #endif
|
---|
839 |
|
---|
840 | timeBeginPeriod(1);
|
---|
841 | #endif
|
---|
842 |
|
---|
843 | #if PLATFORM(EFL)
|
---|
844 | ecore_init();
|
---|
845 | #endif
|
---|
846 |
|
---|
847 | // Initialize JSC before getting VM.
|
---|
848 | #if ENABLE(SAMPLING_REGIONS)
|
---|
849 | WTF::initializeMainThread();
|
---|
850 | #endif
|
---|
851 | JSC::initializeThreading();
|
---|
852 |
|
---|
853 | #if !OS(WINCE)
|
---|
854 | if (char* timeoutString = getenv("JSC_timeout")) {
|
---|
855 | if (sscanf(timeoutString, "%lf", &s_desiredTimeout) != 1) {
|
---|
856 | dataLog(
|
---|
857 | "WARNING: timeout string is malformed, got ", timeoutString,
|
---|
858 | " but expected a number. Not using a timeout.\n");
|
---|
859 | } else
|
---|
860 | createThread(timeoutThreadMain, 0, "jsc Timeout Thread");
|
---|
861 | }
|
---|
862 | #endif
|
---|
863 |
|
---|
864 | #if PLATFORM(IOS)
|
---|
865 | Options::crashIfCantAllocateJITMemory() = true;
|
---|
866 | #endif
|
---|
867 |
|
---|
868 | // We can't use destructors in the following code because it uses Windows
|
---|
869 | // Structured Exception Handling
|
---|
870 | int res = 0;
|
---|
871 | TRY
|
---|
872 | res = jscmain(argc, argv);
|
---|
873 | EXCEPT(res = 3)
|
---|
874 | if (Options::logHeapStatisticsAtExit())
|
---|
875 | HeapStatistics::reportSuccess();
|
---|
876 |
|
---|
877 | #if PLATFORM(EFL)
|
---|
878 | ecore_shutdown();
|
---|
879 | #endif
|
---|
880 |
|
---|
881 | return res;
|
---|
882 | }
|
---|
883 |
|
---|
884 | static bool runWithScripts(GlobalObject* globalObject, const Vector<Script>& scripts, bool dump)
|
---|
885 | {
|
---|
886 | const char* script;
|
---|
887 | String fileName;
|
---|
888 | Vector<char> scriptBuffer;
|
---|
889 |
|
---|
890 | if (dump)
|
---|
891 | JSC::Options::dumpGeneratedBytecodes() = true;
|
---|
892 |
|
---|
893 | VM& vm = globalObject->vm();
|
---|
894 |
|
---|
895 | #if ENABLE(SAMPLING_FLAGS)
|
---|
896 | SamplingFlags::start();
|
---|
897 | #endif
|
---|
898 |
|
---|
899 | bool success = true;
|
---|
900 | for (size_t i = 0; i < scripts.size(); i++) {
|
---|
901 | if (scripts[i].isFile) {
|
---|
902 | fileName = scripts[i].argument;
|
---|
903 | if (!fillBufferWithContentsOfFile(fileName, scriptBuffer))
|
---|
904 | return false; // fail early so we can catch missing files
|
---|
905 | script = scriptBuffer.data();
|
---|
906 | } else {
|
---|
907 | script = scripts[i].argument;
|
---|
908 | fileName = "[Command Line]";
|
---|
909 | }
|
---|
910 |
|
---|
911 | vm.startSampling();
|
---|
912 |
|
---|
913 | JSValue evaluationException;
|
---|
914 | JSValue returnValue = evaluate(globalObject->globalExec(), jscSource(script, fileName), JSValue(), &evaluationException);
|
---|
915 | success = success && !evaluationException;
|
---|
916 | if (dump && !evaluationException)
|
---|
917 | printf("End: %s\n", returnValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
918 | if (evaluationException) {
|
---|
919 | printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
920 | Identifier stackID(globalObject->globalExec(), "stack");
|
---|
921 | JSValue stackValue = evaluationException.get(globalObject->globalExec(), stackID);
|
---|
922 | if (!stackValue.isUndefinedOrNull())
|
---|
923 | printf("%s\n", stackValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
924 | }
|
---|
925 |
|
---|
926 | vm.stopSampling();
|
---|
927 | globalObject->globalExec()->clearException();
|
---|
928 | }
|
---|
929 |
|
---|
930 | #if ENABLE(SAMPLING_FLAGS)
|
---|
931 | SamplingFlags::stop();
|
---|
932 | #endif
|
---|
933 | #if ENABLE(SAMPLING_REGIONS)
|
---|
934 | SamplingRegion::dump();
|
---|
935 | #endif
|
---|
936 | vm.dumpSampleData(globalObject->globalExec());
|
---|
937 | #if ENABLE(SAMPLING_COUNTERS)
|
---|
938 | AbstractSamplingCounter::dump();
|
---|
939 | #endif
|
---|
940 | #if ENABLE(REGEXP_TRACING)
|
---|
941 | vm.dumpRegExpTrace();
|
---|
942 | #endif
|
---|
943 | return success;
|
---|
944 | }
|
---|
945 |
|
---|
946 | #define RUNNING_FROM_XCODE 0
|
---|
947 |
|
---|
948 | static void runInteractive(GlobalObject* globalObject)
|
---|
949 | {
|
---|
950 | String interpreterName("Interpreter");
|
---|
951 |
|
---|
952 | bool shouldQuit = false;
|
---|
953 | while (!shouldQuit) {
|
---|
954 | #if HAVE(READLINE) && !RUNNING_FROM_XCODE
|
---|
955 | ParserError error;
|
---|
956 | String source;
|
---|
957 | do {
|
---|
958 | error = ParserError();
|
---|
959 | char* line = readline(source.isEmpty() ? interactivePrompt : "... ");
|
---|
960 | shouldQuit = !line;
|
---|
961 | if (!line)
|
---|
962 | break;
|
---|
963 | source = source + line;
|
---|
964 | source = source + '\n';
|
---|
965 | checkSyntax(globalObject->vm(), makeSource(source, interpreterName), error);
|
---|
966 | if (!line[0])
|
---|
967 | break;
|
---|
968 | add_history(line);
|
---|
969 | } while (error.m_syntaxErrorType == ParserError::SyntaxErrorRecoverable);
|
---|
970 |
|
---|
971 | if (error.m_type != ParserError::ErrorNone) {
|
---|
972 | printf("%s:%d\n", error.m_message.utf8().data(), error.m_line);
|
---|
973 | continue;
|
---|
974 | }
|
---|
975 |
|
---|
976 |
|
---|
977 | JSValue evaluationException;
|
---|
978 | JSValue returnValue = evaluate(globalObject->globalExec(), makeSource(source, interpreterName), JSValue(), &evaluationException);
|
---|
979 | #else
|
---|
980 | printf("%s", interactivePrompt);
|
---|
981 | Vector<char, 256> line;
|
---|
982 | int c;
|
---|
983 | while ((c = getchar()) != EOF) {
|
---|
984 | // FIXME: Should we also break on \r?
|
---|
985 | if (c == '\n')
|
---|
986 | break;
|
---|
987 | line.append(c);
|
---|
988 | }
|
---|
989 | if (line.isEmpty())
|
---|
990 | break;
|
---|
991 | line.append('\0');
|
---|
992 |
|
---|
993 | JSValue evaluationException;
|
---|
994 | JSValue returnValue = evaluate(globalObject->globalExec(), jscSource(line.data(), interpreterName), JSValue(), &evaluationException);
|
---|
995 | #endif
|
---|
996 | if (evaluationException)
|
---|
997 | printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
998 | else
|
---|
999 | printf("%s\n", returnValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
|
---|
1000 |
|
---|
1001 | globalObject->globalExec()->clearException();
|
---|
1002 | }
|
---|
1003 | printf("\n");
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | static NO_RETURN void printUsageStatement(bool help = false)
|
---|
1007 | {
|
---|
1008 | fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
|
---|
1009 | fprintf(stderr, " -d Dumps bytecode (debug builds only)\n");
|
---|
1010 | fprintf(stderr, " -e Evaluate argument as script code\n");
|
---|
1011 | fprintf(stderr, " -f Specifies a source file (deprecated)\n");
|
---|
1012 | fprintf(stderr, " -h|--help Prints this help message\n");
|
---|
1013 | fprintf(stderr, " -i Enables interactive mode (default if no files are specified)\n");
|
---|
1014 | #if HAVE(SIGNAL_H)
|
---|
1015 | fprintf(stderr, " -s Installs signal handlers that exit on a crash (Unix platforms only)\n");
|
---|
1016 | #endif
|
---|
1017 | fprintf(stderr, " -p <file> Outputs profiling data to a file\n");
|
---|
1018 | fprintf(stderr, " -x Output exit code before terminating\n");
|
---|
1019 | fprintf(stderr, "\n");
|
---|
1020 | fprintf(stderr, " --options Dumps all JSC VM options and exits\n");
|
---|
1021 | fprintf(stderr, " --dumpOptions Dumps all JSC VM options before continuing\n");
|
---|
1022 | fprintf(stderr, " --<jsc VM option>=<value> Sets the specified JSC VM option\n");
|
---|
1023 | fprintf(stderr, "\n");
|
---|
1024 |
|
---|
1025 | exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | void CommandLine::parseArguments(int argc, char** argv)
|
---|
1029 | {
|
---|
1030 | int i = 1;
|
---|
1031 | bool needToDumpOptions = false;
|
---|
1032 | bool needToExit = false;
|
---|
1033 |
|
---|
1034 | for (; i < argc; ++i) {
|
---|
1035 | const char* arg = argv[i];
|
---|
1036 | if (!strcmp(arg, "-f")) {
|
---|
1037 | if (++i == argc)
|
---|
1038 | printUsageStatement();
|
---|
1039 | m_scripts.append(Script(true, argv[i]));
|
---|
1040 | continue;
|
---|
1041 | }
|
---|
1042 | if (!strcmp(arg, "-e")) {
|
---|
1043 | if (++i == argc)
|
---|
1044 | printUsageStatement();
|
---|
1045 | m_scripts.append(Script(false, argv[i]));
|
---|
1046 | continue;
|
---|
1047 | }
|
---|
1048 | if (!strcmp(arg, "-i")) {
|
---|
1049 | m_interactive = true;
|
---|
1050 | continue;
|
---|
1051 | }
|
---|
1052 | if (!strcmp(arg, "-d")) {
|
---|
1053 | m_dump = true;
|
---|
1054 | continue;
|
---|
1055 | }
|
---|
1056 | if (!strcmp(arg, "-p")) {
|
---|
1057 | if (++i == argc)
|
---|
1058 | printUsageStatement();
|
---|
1059 | m_profile = true;
|
---|
1060 | m_profilerOutput = argv[i];
|
---|
1061 | continue;
|
---|
1062 | }
|
---|
1063 | if (!strcmp(arg, "-s")) {
|
---|
1064 | #if HAVE(SIGNAL_H)
|
---|
1065 | signal(SIGILL, _exit);
|
---|
1066 | signal(SIGFPE, _exit);
|
---|
1067 | signal(SIGBUS, _exit);
|
---|
1068 | signal(SIGSEGV, _exit);
|
---|
1069 | #endif
|
---|
1070 | continue;
|
---|
1071 | }
|
---|
1072 | if (!strcmp(arg, "-x")) {
|
---|
1073 | m_exitCode = true;
|
---|
1074 | continue;
|
---|
1075 | }
|
---|
1076 | if (!strcmp(arg, "--")) {
|
---|
1077 | ++i;
|
---|
1078 | break;
|
---|
1079 | }
|
---|
1080 | if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
|
---|
1081 | printUsageStatement(true);
|
---|
1082 |
|
---|
1083 | if (!strcmp(arg, "--options")) {
|
---|
1084 | needToDumpOptions = true;
|
---|
1085 | needToExit = true;
|
---|
1086 | continue;
|
---|
1087 | }
|
---|
1088 | if (!strcmp(arg, "--dumpOptions")) {
|
---|
1089 | needToDumpOptions = true;
|
---|
1090 | continue;
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | // See if the -- option is a JSC VM option.
|
---|
1094 | // NOTE: At this point, we know that the arg starts with "--". Skip it.
|
---|
1095 | if (JSC::Options::setOption(&arg[2])) {
|
---|
1096 | // The arg was recognized as a VM option and has been parsed.
|
---|
1097 | continue; // Just continue with the next arg.
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | // This arg is not recognized by the VM nor by jsc. Pass it on to the
|
---|
1101 | // script.
|
---|
1102 | m_scripts.append(Script(true, argv[i]));
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | if (m_scripts.isEmpty())
|
---|
1106 | m_interactive = true;
|
---|
1107 |
|
---|
1108 | for (; i < argc; ++i)
|
---|
1109 | m_arguments.append(argv[i]);
|
---|
1110 |
|
---|
1111 | if (needToDumpOptions)
|
---|
1112 | JSC::Options::dumpAllOptions(stderr);
|
---|
1113 | if (needToExit)
|
---|
1114 | exit(EXIT_SUCCESS);
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | int jscmain(int argc, char** argv)
|
---|
1118 | {
|
---|
1119 | // Note that the options parsing can affect VM creation, and thus
|
---|
1120 | // comes first.
|
---|
1121 | CommandLine options(argc, argv);
|
---|
1122 | VM* vm = VM::create(LargeHeap).leakRef();
|
---|
1123 | int result;
|
---|
1124 | {
|
---|
1125 | JSLockHolder locker(vm);
|
---|
1126 |
|
---|
1127 | if (options.m_profile && !vm->m_perBytecodeProfiler)
|
---|
1128 | vm->m_perBytecodeProfiler = adoptPtr(new Profiler::Database(*vm));
|
---|
1129 |
|
---|
1130 | GlobalObject* globalObject = GlobalObject::create(*vm, GlobalObject::createStructure(*vm, jsNull()), options.m_arguments);
|
---|
1131 | bool success = runWithScripts(globalObject, options.m_scripts, options.m_dump);
|
---|
1132 | if (options.m_interactive && success)
|
---|
1133 | runInteractive(globalObject);
|
---|
1134 |
|
---|
1135 | result = success ? 0 : 3;
|
---|
1136 |
|
---|
1137 | if (options.m_exitCode)
|
---|
1138 | printf("jsc exiting %d\n", result);
|
---|
1139 |
|
---|
1140 | if (options.m_profile) {
|
---|
1141 | if (!vm->m_perBytecodeProfiler->save(options.m_profilerOutput.utf8().data()))
|
---|
1142 | fprintf(stderr, "could not save profiler output.\n");
|
---|
1143 | }
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | return result;
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | static bool fillBufferWithContentsOfFile(const String& fileName, Vector<char>& buffer)
|
---|
1150 | {
|
---|
1151 | FILE* f = fopen(fileName.utf8().data(), "r");
|
---|
1152 | if (!f) {
|
---|
1153 | fprintf(stderr, "Could not open file: %s\n", fileName.utf8().data());
|
---|
1154 | return false;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | size_t bufferSize = 0;
|
---|
1158 | size_t bufferCapacity = 1024;
|
---|
1159 |
|
---|
1160 | buffer.resize(bufferCapacity);
|
---|
1161 |
|
---|
1162 | while (!feof(f) && !ferror(f)) {
|
---|
1163 | bufferSize += fread(buffer.data() + bufferSize, 1, bufferCapacity - bufferSize, f);
|
---|
1164 | if (bufferSize == bufferCapacity) { // guarantees space for trailing '\0'
|
---|
1165 | bufferCapacity *= 2;
|
---|
1166 | buffer.resize(bufferCapacity);
|
---|
1167 | }
|
---|
1168 | }
|
---|
1169 | fclose(f);
|
---|
1170 | buffer[bufferSize] = '\0';
|
---|
1171 |
|
---|
1172 | if (buffer[0] == '#' && buffer[1] == '!')
|
---|
1173 | buffer[0] = buffer[1] = '/';
|
---|
1174 |
|
---|
1175 | return true;
|
---|
1176 | }
|
---|