source: webkit/trunk/Source/JavaScriptCore/jsc.cpp@ 165074

Last change on this file since 165074 was 165074, checked in by [email protected], 11 years ago

Merge API shims and JSLock
https://bugs.webkit.org/show_bug.cgi?id=129650

Reviewed by Mark Lam.

Source/JavaScriptCore:

JSLock is now taking on all of APIEntryShim's responsibilities since there is never a reason
to take just the JSLock. Ditto for DropAllLocks and APICallbackShim.

  • API/APICallbackFunction.h:

(JSC::APICallbackFunction::call):
(JSC::APICallbackFunction::construct):

  • API/APIShims.h: Removed.
  • API/JSBase.cpp:

(JSEvaluateScript):
(JSCheckScriptSyntax):
(JSGarbageCollect):
(JSReportExtraMemoryCost):
(JSSynchronousGarbageCollectForDebugging):

  • API/JSCallbackConstructor.cpp:
  • API/JSCallbackFunction.cpp:
  • API/JSCallbackObjectFunctions.h:

(JSC::JSCallbackObject<Parent>::init):
(JSC::JSCallbackObject<Parent>::getOwnPropertySlot):
(JSC::JSCallbackObject<Parent>::put):
(JSC::JSCallbackObject<Parent>::putByIndex):
(JSC::JSCallbackObject<Parent>::deleteProperty):
(JSC::JSCallbackObject<Parent>::construct):
(JSC::JSCallbackObject<Parent>::customHasInstance):
(JSC::JSCallbackObject<Parent>::call):
(JSC::JSCallbackObject<Parent>::getOwnNonIndexPropertyNames):
(JSC::JSCallbackObject<Parent>::getStaticValue):
(JSC::JSCallbackObject<Parent>::callbackGetter):

  • API/JSContext.mm:

(-[JSContext setException:]):
(-[JSContext wrapperForObjCObject:]):
(-[JSContext wrapperForJSObject:]):

  • API/JSContextRef.cpp:

(JSContextGroupRelease):
(JSContextGroupSetExecutionTimeLimit):
(JSContextGroupClearExecutionTimeLimit):
(JSGlobalContextCreateInGroup):
(JSGlobalContextRetain):
(JSGlobalContextRelease):
(JSContextGetGlobalObject):
(JSContextGetGlobalContext):
(JSGlobalContextCopyName):
(JSGlobalContextSetName):

  • API/JSManagedValue.mm:

(-[JSManagedValue value]):

  • API/JSObjectRef.cpp:

(JSObjectMake):
(JSObjectMakeFunctionWithCallback):
(JSObjectMakeConstructor):
(JSObjectMakeFunction):
(JSObjectMakeArray):
(JSObjectMakeDate):
(JSObjectMakeError):
(JSObjectMakeRegExp):
(JSObjectGetPrototype):
(JSObjectSetPrototype):
(JSObjectHasProperty):
(JSObjectGetProperty):
(JSObjectSetProperty):
(JSObjectGetPropertyAtIndex):
(JSObjectSetPropertyAtIndex):
(JSObjectDeleteProperty):
(JSObjectGetPrivateProperty):
(JSObjectSetPrivateProperty):
(JSObjectDeletePrivateProperty):
(JSObjectIsFunction):
(JSObjectCallAsFunction):
(JSObjectCallAsConstructor):
(JSObjectCopyPropertyNames):
(JSPropertyNameArrayRelease):
(JSPropertyNameAccumulatorAddName):

  • API/JSScriptRef.cpp:
  • API/JSValue.mm:

(isDate):
(isArray):
(containerValueToObject):
(valueToArray):
(valueToDictionary):
(objectToValue):

  • API/JSValueRef.cpp:

(JSValueGetType):
(JSValueIsUndefined):
(JSValueIsNull):
(JSValueIsBoolean):
(JSValueIsNumber):
(JSValueIsString):
(JSValueIsObject):
(JSValueIsObjectOfClass):
(JSValueIsEqual):
(JSValueIsStrictEqual):
(JSValueIsInstanceOfConstructor):
(JSValueMakeUndefined):
(JSValueMakeNull):
(JSValueMakeBoolean):
(JSValueMakeNumber):
(JSValueMakeString):
(JSValueMakeFromJSONString):
(JSValueCreateJSONString):
(JSValueToBoolean):
(JSValueToNumber):
(JSValueToStringCopy):
(JSValueToObject):
(JSValueProtect):
(JSValueUnprotect):

  • API/JSVirtualMachine.mm:

(-[JSVirtualMachine addManagedReference:withOwner:]):
(-[JSVirtualMachine removeManagedReference:withOwner:]):

  • API/JSWeakObjectMapRefPrivate.cpp:
  • API/JSWrapperMap.mm:

(constructorHasInstance):
(makeWrapper):
(tryUnwrapObjcObject):

  • API/ObjCCallbackFunction.mm:

(JSC::objCCallbackFunctionCallAsFunction):
(JSC::objCCallbackFunctionCallAsConstructor):
(objCCallbackFunctionForInvocation):

  • CMakeLists.txt:
  • ForwardingHeaders/JavaScriptCore/APIShims.h: Removed.
  • GNUmakefile.list.am:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • dfg/DFGWorklist.cpp:
  • heap/DelayedReleaseScope.h:

(JSC::DelayedReleaseScope::~DelayedReleaseScope):

  • heap/HeapTimer.cpp:

(JSC::HeapTimer::timerDidFire):
(JSC::HeapTimer::timerEvent):

  • heap/IncrementalSweeper.cpp:
  • inspector/InjectedScriptModule.cpp:

(Inspector::InjectedScriptModule::ensureInjected):

  • jsc.cpp:

(jscmain):

  • runtime/GCActivityCallback.cpp:

(JSC::DefaultGCActivityCallback::doWork):

  • runtime/JSGlobalObjectDebuggable.cpp:

(JSC::JSGlobalObjectDebuggable::connect):
(JSC::JSGlobalObjectDebuggable::disconnect):
(JSC::JSGlobalObjectDebuggable::dispatchMessageFromRemoteFrontend):

  • runtime/JSLock.cpp:

(JSC::JSLock::lock):
(JSC::JSLock::didAcquireLock):
(JSC::JSLock::unlock):
(JSC::JSLock::willReleaseLock):
(JSC::JSLock::DropAllLocks::DropAllLocks):
(JSC::JSLock::DropAllLocks::~DropAllLocks):

  • runtime/JSLock.h:
  • testRegExp.cpp:

(realMain):

Source/WebCore:

No new tests.

JSLock is now taking on all of APIEntryShim's responsibilities since there is never a reason
to take just the JSLock. Ditto for DropAllLocks and APICallbackShim.

  • bindings/js/DOMRequestState.h:

(WebCore::DOMRequestState::Scope::Scope):

  • bindings/js/JSDOMPromise.h:

(WebCore::DeferredWrapper::resolve):
(WebCore::DeferredWrapper::reject):
(WebCore::DeferredWrapper::resolve<String>):
(WebCore::DeferredWrapper::resolve<bool>):
(WebCore::char>>):
(WebCore::DeferredWrapper::reject<String>):

  • bindings/js/ScriptController.cpp:

(WebCore::ScriptController::evaluateInWorld):

  • bindings/js/SerializedScriptValue.cpp:

(WebCore::SerializedScriptValue::create):
(WebCore::SerializedScriptValue::deserialize):

Source/WebKit/mac:

JSLock is now taking on all of APIEntryShim's responsibilities since there is never a reason
to take just the JSLock. Ditto for DropAllLocks and APICallbackShim.

  • WebView/WebFrame.mm:

(-[WebFrame _stringByEvaluatingJavaScriptFromString:forceUserGesture:]):

Source/WebKit2:

JSLock is now taking on all of APIEntryShim's responsibilities since there is never a reason
to take just the JSLock. Ditto for DropAllLocks and APICallbackShim.

  • DatabaseProcess/IndexedDB/sqlite/UniqueIDBDatabaseBackingStoreSQLite.cpp:

(WebKit::UniqueIDBDatabaseBackingStoreSQLite::~UniqueIDBDatabaseBackingStoreSQLite):
(WebKit::UniqueIDBDatabaseBackingStoreSQLite::createIndex):

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