#include "BytecodeGenerator.h"
#include "Completion.h"
+#include <wtf/CurrentTime.h>
+#include "ExceptionHelpers.h"
#include "InitializeThreading.h"
+#include "Interpreter.h"
#include "JSArray.h"
+#include "JSCTypedArrayStubs.h"
+#include "JSFunction.h"
#include "JSLock.h"
-#include "PrototypeFunction.h"
+#include "JSString.h"
+#include <wtf/MainThread.h>
#include "SamplingTool.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#if !PLATFORM(WIN_OS)
+#if !OS(WINDOWS)
#include <unistd.h>
#endif
#if HAVE(READLINE)
+// readline/history.h has a Function typedef which conflicts with the WTF::Function template from WTF/Forward.h
+// We #define it to something else to avoid this conflict.
+#define Function ReadlineFunction
#include <readline/history.h>
#include <readline/readline.h>
+#undef Function
#endif
#if HAVE(SYS_TIME_H)
#include <sys/time.h>
#endif
-#if PLATFORM(UNIX)
+#if HAVE(SIGNAL_H)
#include <signal.h>
#endif
-#if COMPILER(MSVC) && !PLATFORM(WIN_CE)
+#if COMPILER(MSVC) && !OS(WINCE)
#include <crtdbg.h>
+#include <mmsystem.h>
#include <windows.h>
#endif
#include <QDateTime>
#endif
+#if CPU(ARM_THUMB2)
+#include <fenv.h>
+#include <arm/arch.h>
+#endif
+
using namespace JSC;
using namespace WTF;
-static void cleanupGlobalData(JSGlobalData*);
static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer);
-static JSValuePtr functionPrint(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-static JSValuePtr functionDebug(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-static JSValuePtr functionGC(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-static JSValuePtr functionVersion(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-static JSValuePtr functionRun(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-static JSValuePtr functionLoad(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-static JSValuePtr functionReadline(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-static NO_RETURN JSValuePtr functionQuit(ExecState*, JSObject*, JSValuePtr, const ArgList&);
-
-struct Options {
- Options()
+static EncodedJSValue JSC_HOST_CALL functionPrint(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionDebug(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionJSCStack(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionGC(ExecState*);
+#ifndef NDEBUG
+static EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState*);
+#endif
+static EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionRun(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionLoad(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionReadline(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionPreciseTime(ExecState*);
+static NO_RETURN_WITH_VALUE EncodedJSValue JSC_HOST_CALL functionQuit(ExecState*);
+
+#if ENABLE(SAMPLING_FLAGS)
+static EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState*);
+static EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState*);
+#endif
+
+struct Script {
+ bool isFile;
+ char* argument;
+
+ Script(bool isFile, char *argument)
+ : isFile(isFile)
+ , argument(argument)
+ {
+ }
+};
+
+struct CommandLine {
+ CommandLine()
: interactive(false)
, dump(false)
{
bool interactive;
bool dump;
- Vector<UString> fileNames;
+ Vector<Script> scripts;
Vector<UString> arguments;
};
static const char interactivePrompt[] = "> ";
-static const UString interpreterName("Interpreter");
class StopWatch {
public:
long getElapsedMS(); // call stop() first
private:
-#if PLATFORM(QT)
- uint m_startTime;
- uint m_stopTime;
-#elif PLATFORM(WIN_OS)
- DWORD m_startTime;
- DWORD m_stopTime;
-#else
- // Windows does not have timeval, disabling this class for now (bug 7399)
- timeval m_startTime;
- timeval m_stopTime;
-#endif
+ double m_startTime;
+ double m_stopTime;
};
void StopWatch::start()
{
-#if PLATFORM(QT)
- QDateTime t = QDateTime::currentDateTime();
- m_startTime = t.toTime_t() * 1000 + t.time().msec();
-#elif PLATFORM(WIN_OS)
- m_startTime = timeGetTime();
-#else
- gettimeofday(&m_startTime, 0);
-#endif
+ m_startTime = currentTime();
}
void StopWatch::stop()
{
-#if PLATFORM(QT)
- QDateTime t = QDateTime::currentDateTime();
- m_stopTime = t.toTime_t() * 1000 + t.time().msec();
-#elif PLATFORM(WIN_OS)
- m_stopTime = timeGetTime();
-#else
- gettimeofday(&m_stopTime, 0);
-#endif
+ m_stopTime = currentTime();
}
long StopWatch::getElapsedMS()
{
-#if PLATFORM(WIN_OS) || PLATFORM(QT)
- return m_stopTime - m_startTime;
-#else
- timeval elapsedTime;
- timersub(&m_stopTime, &m_startTime, &elapsedTime);
-
- return elapsedTime.tv_sec * 1000 + lroundf(elapsedTime.tv_usec / 1000.0f);
-#endif
+ return static_cast<long>((m_stopTime - m_startTime) * 1000);
}
class GlobalObject : public JSGlobalObject {
+private:
+ GlobalObject(JSGlobalData&, Structure*);
+
public:
- GlobalObject(const Vector<UString>& arguments);
- virtual UString className() const { return "global"; }
+ typedef JSGlobalObject Base;
+
+ static GlobalObject* create(JSGlobalData& globalData, Structure* structure, const Vector<UString>& arguments)
+ {
+ GlobalObject* object = new (NotNull, allocateCell<GlobalObject>(globalData.heap)) GlobalObject(globalData, structure);
+ object->finishCreation(globalData, arguments);
+ return object;
+ }
+
+ static const ClassInfo s_info;
+
+ static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
+ {
+ return Structure::create(globalData, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), &s_info);
+ }
+
+protected:
+ void finishCreation(JSGlobalData& globalData, const Vector<UString>& arguments)
+ {
+ Base::finishCreation(globalData);
+
+ addFunction(globalData, "debug", functionDebug, 1);
+ addFunction(globalData, "print", functionPrint, 1);
+ addFunction(globalData, "quit", functionQuit, 0);
+ addFunction(globalData, "gc", functionGC, 0);
+#ifndef NDEBUG
+ addFunction(globalData, "releaseExecutableMemory", functionReleaseExecutableMemory, 0);
+#endif
+ addFunction(globalData, "version", functionVersion, 1);
+ addFunction(globalData, "run", functionRun, 1);
+ addFunction(globalData, "load", functionLoad, 1);
+ addFunction(globalData, "checkSyntax", functionCheckSyntax, 1);
+ addFunction(globalData, "jscStack", functionJSCStack, 1);
+ addFunction(globalData, "readline", functionReadline, 0);
+ addFunction(globalData, "preciseTime", functionPreciseTime, 0);
+#if ENABLE(SAMPLING_FLAGS)
+ addFunction(globalData, "setSamplingFlags", functionSetSamplingFlags, 1);
+ addFunction(globalData, "clearSamplingFlags", functionClearSamplingFlags, 1);
+#endif
+
+ addConstructableFunction(globalData, "Uint8Array", constructJSUint8Array, 1);
+ addConstructableFunction(globalData, "Uint8ClampedArray", constructJSUint8ClampedArray, 1);
+ addConstructableFunction(globalData, "Uint16Array", constructJSUint16Array, 1);
+ addConstructableFunction(globalData, "Uint32Array", constructJSUint32Array, 1);
+ addConstructableFunction(globalData, "Int8Array", constructJSInt8Array, 1);
+ addConstructableFunction(globalData, "Int16Array", constructJSInt16Array, 1);
+ addConstructableFunction(globalData, "Int32Array", constructJSInt32Array, 1);
+ addConstructableFunction(globalData, "Float32Array", constructJSFloat32Array, 1);
+ addConstructableFunction(globalData, "Float64Array", constructJSFloat64Array, 1);
+
+ JSArray* array = constructEmptyArray(globalExec());
+ for (size_t i = 0; i < arguments.size(); ++i)
+ array->putDirectIndex(globalExec(), i, jsString(globalExec(), arguments[i]), false);
+ putDirect(globalData, Identifier(globalExec(), "arguments"), array);
+ }
+
+ void addFunction(JSGlobalData& globalData, const char* name, NativeFunction function, unsigned arguments)
+ {
+ Identifier identifier(globalExec(), name);
+ putDirect(globalData, identifier, JSFunction::create(globalExec(), this, arguments, identifier, function));
+ }
+
+ void addConstructableFunction(JSGlobalData& globalData, const char* name, NativeFunction function, unsigned arguments)
+ {
+ Identifier identifier(globalExec(), name);
+ putDirect(globalData, identifier, JSFunction::create(globalExec(), this, arguments, identifier, function, NoIntrinsic, function));
+ }
};
COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
-GlobalObject::GlobalObject(const Vector<UString>& arguments)
- : JSGlobalObject()
+const ClassInfo GlobalObject::s_info = { "global", &JSGlobalObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(GlobalObject) };
+
+GlobalObject::GlobalObject(JSGlobalData& globalData, Structure* structure)
+ : JSGlobalObject(globalData, structure)
+{
+}
+
+static inline SourceCode jscSource(const char* utf8, const UString& filename)
{
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "debug"), functionDebug));
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "print"), functionPrint));
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "quit"), functionQuit));
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "gc"), functionGC));
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "version"), functionVersion));
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "run"), functionRun));
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 1, Identifier(globalExec(), "load"), functionLoad));
- putDirectFunction(globalExec(), new (globalExec()) PrototypeFunction(globalExec(), prototypeFunctionStructure(), 0, Identifier(globalExec(), "readline"), functionReadline));
-
- JSObject* array = constructEmptyArray(globalExec());
- for (size_t i = 0; i < arguments.size(); ++i)
- array->put(globalExec(), i, jsString(globalExec(), arguments[i]));
- putDirect(Identifier(globalExec(), "arguments"), array);
+ // Find the the first non-ascii character, or nul.
+ const char* pos = utf8;
+ while (*pos > 0)
+ pos++;
+ size_t asciiLength = pos - utf8;
+
+ // Fast case - string is all ascii.
+ if (!*pos)
+ return makeSource(UString(utf8, asciiLength), filename);
+
+ // Slow case - contains non-ascii characters, use fromUTF8WithLatin1Fallback.
+ ASSERT(*pos < 0);
+ ASSERT(strlen(utf8) == asciiLength + strlen(pos));
+ String source = String::fromUTF8WithLatin1Fallback(utf8, asciiLength + strlen(pos));
+ return makeSource(source.impl(), filename);
}
-JSValuePtr functionPrint(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+EncodedJSValue JSC_HOST_CALL functionPrint(ExecState* exec)
{
- for (unsigned i = 0; i < args.size(); ++i) {
- if (i != 0)
+ for (unsigned i = 0; i < exec->argumentCount(); ++i) {
+ if (i)
putchar(' ');
-
- printf("%s", args.at(exec, i).toString(exec).UTF8String().c_str());
+
+ printf("%s", exec->argument(i).toString(exec)->value(exec).utf8().data());
}
-
+
putchar('\n');
fflush(stdout);
- return jsUndefined();
+ return JSValue::encode(jsUndefined());
+}
+
+EncodedJSValue JSC_HOST_CALL functionDebug(ExecState* exec)
+{
+ fprintf(stderr, "--> %s\n", exec->argument(0).toString(exec)->value(exec).utf8().data());
+ return JSValue::encode(jsUndefined());
+}
+
+EncodedJSValue JSC_HOST_CALL functionJSCStack(ExecState* exec)
+{
+ String trace = "--> Stack trace:\n";
+ Vector<StackFrame> stackTrace;
+ Interpreter::getStackTrace(&exec->globalData(), stackTrace);
+ int i = 0;
+
+ for (Vector<StackFrame>::iterator iter = stackTrace.begin(); iter < stackTrace.end(); iter++) {
+ StackFrame level = *iter;
+ trace += String::format(" %i %s\n", i, level.toString(exec).utf8().data());
+ i++;
+ }
+ fprintf(stderr, "%s", trace.utf8().data());
+ return JSValue::encode(jsUndefined());
}
-JSValuePtr functionDebug(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+EncodedJSValue JSC_HOST_CALL functionGC(ExecState* exec)
{
- fprintf(stderr, "--> %s\n", args.at(exec, 0).toString(exec).UTF8String().c_str());
- return jsUndefined();
+ JSLockHolder lock(exec);
+ exec->heap()->collectAllGarbage();
+ return JSValue::encode(jsUndefined());
}
-JSValuePtr functionGC(ExecState* exec, JSObject*, JSValuePtr, const ArgList&)
+#ifndef NDEBUG
+EncodedJSValue JSC_HOST_CALL functionReleaseExecutableMemory(ExecState* exec)
{
- JSLock lock(false);
- exec->heap()->collect();
- return jsUndefined();
+ JSLockHolder lock(exec);
+ exec->globalData().releaseExecutableMemory();
+ return JSValue::encode(jsUndefined());
}
+#endif
-JSValuePtr functionVersion(ExecState*, JSObject*, JSValuePtr, const ArgList&)
+EncodedJSValue JSC_HOST_CALL functionVersion(ExecState*)
{
// We need this function for compatibility with the Mozilla JS tests but for now
// we don't actually do any version-specific handling
- return jsUndefined();
+ return JSValue::encode(jsUndefined());
}
-JSValuePtr functionRun(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+EncodedJSValue JSC_HOST_CALL functionRun(ExecState* exec)
{
- StopWatch stopWatch;
- UString fileName = args.at(exec, 0).toString(exec);
+ UString fileName = exec->argument(0).toString(exec)->value(exec);
Vector<char> script;
if (!fillBufferWithContentsOfFile(fileName, script))
- return throwError(exec, GeneralError, "Could not open file.");
+ return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
- JSGlobalObject* globalObject = exec->lexicalGlobalObject();
+ GlobalObject* globalObject = GlobalObject::create(exec->globalData(), GlobalObject::createStructure(exec->globalData(), jsNull()), Vector<UString>());
+ JSValue exception;
+ StopWatch stopWatch;
stopWatch.start();
- evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
+ evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(script.data(), fileName), JSValue(), &exception);
stopWatch.stop();
- return jsNumber(globalObject->globalExec(), stopWatch.getElapsedMS());
+ if (!!exception) {
+ throwError(globalObject->globalExec(), exception);
+ return JSValue::encode(jsUndefined());
+ }
+
+ return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
+}
+
+EncodedJSValue JSC_HOST_CALL functionLoad(ExecState* exec)
+{
+ UString fileName = exec->argument(0).toString(exec)->value(exec);
+ Vector<char> script;
+ if (!fillBufferWithContentsOfFile(fileName, script))
+ return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
+
+ JSGlobalObject* globalObject = exec->lexicalGlobalObject();
+
+ JSValue evaluationException;
+ JSValue result = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(script.data(), fileName), JSValue(), &evaluationException);
+ if (evaluationException)
+ throwError(exec, evaluationException);
+ return JSValue::encode(result);
}
-JSValuePtr functionLoad(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
+EncodedJSValue JSC_HOST_CALL functionCheckSyntax(ExecState* exec)
{
- UString fileName = args.at(exec, 0).toString(exec);
+ UString fileName = exec->argument(0).toString(exec)->value(exec);
Vector<char> script;
if (!fillBufferWithContentsOfFile(fileName, script))
- return throwError(exec, GeneralError, "Could not open file.");
+ return JSValue::encode(throwError(exec, createError(exec, "Could not open file.")));
JSGlobalObject* globalObject = exec->lexicalGlobalObject();
- evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
- return jsUndefined();
+ StopWatch stopWatch;
+ stopWatch.start();
+
+ JSValue syntaxException;
+ bool validSyntax = checkSyntax(globalObject->globalExec(), jscSource(script.data(), fileName), &syntaxException);
+ stopWatch.stop();
+
+ if (!validSyntax)
+ throwError(exec, syntaxException);
+ return JSValue::encode(jsNumber(stopWatch.getElapsedMS()));
+}
+
+#if ENABLE(SAMPLING_FLAGS)
+EncodedJSValue JSC_HOST_CALL functionSetSamplingFlags(ExecState* exec)
+{
+ for (unsigned i = 0; i < exec->argumentCount(); ++i) {
+ unsigned flag = static_cast<unsigned>(exec->argument(i).toNumber(exec));
+ if ((flag >= 1) && (flag <= 32))
+ SamplingFlags::setFlag(flag);
+ }
+ return JSValue::encode(jsNull());
+}
+
+EncodedJSValue JSC_HOST_CALL functionClearSamplingFlags(ExecState* exec)
+{
+ for (unsigned i = 0; i < exec->argumentCount(); ++i) {
+ unsigned flag = static_cast<unsigned>(exec->argument(i).toNumber(exec));
+ if ((flag >= 1) && (flag <= 32))
+ SamplingFlags::clearFlag(flag);
+ }
+ return JSValue::encode(jsNull());
}
+#endif
-JSValuePtr functionReadline(ExecState* exec, JSObject*, JSValuePtr, const ArgList&)
+EncodedJSValue JSC_HOST_CALL functionReadline(ExecState* exec)
{
Vector<char, 256> line;
int c;
line.append(c);
}
line.append('\0');
- return jsString(exec, line.data());
+ return JSValue::encode(jsString(exec, line.data()));
+}
+
+EncodedJSValue JSC_HOST_CALL functionPreciseTime(ExecState*)
+{
+ return JSValue::encode(jsNumber(currentTime()));
}
-JSValuePtr functionQuit(ExecState* exec, JSObject*, JSValuePtr, const ArgList&)
+EncodedJSValue JSC_HOST_CALL functionQuit(ExecState*)
{
- cleanupGlobalData(&exec->globalData());
exit(EXIT_SUCCESS);
+
+#if COMPILER(MSVC) && OS(WINCE)
+ // Without this, Visual Studio will complain that this method does not return a value.
+ return JSValue::encode(jsUndefined());
+#endif
}
// Use SEH for Release builds only to get rid of the crash report dialog
// be in a separate main function because the jscmain function requires object
// unwinding.
-#if COMPILER(MSVC) && !defined(_DEBUG)
+#if COMPILER(MSVC) && !COMPILER(INTEL) && !defined(_DEBUG) && !OS(WINCE)
#define TRY __try {
#define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
#else
#define EXCEPT(x)
#endif
-int jscmain(int argc, char** argv, JSGlobalData*);
+int jscmain(int argc, char** argv);
int main(int argc, char** argv)
{
-#if defined(_DEBUG) && PLATFORM(WIN_OS)
+#if CPU(ARM_THUMB2)
+ // Enabled IEEE754 denormal support.
+ fenv_t env;
+ fegetenv( &env );
+ env.__fpscr &= ~0x01000000u;
+ fesetenv( &env );
+#endif
+
+#if OS(WINDOWS)
+#if !OS(WINCE)
+ // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
+ // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
+ // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
+ ::SetErrorMode(0);
+#endif
+
+#if defined(_DEBUG)
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
#endif
+ timeBeginPeriod(1);
+#endif
+
#if PLATFORM(QT)
QCoreApplication app(argc, argv);
#endif
// Initialize JSC before getting JSGlobalData.
+ WTF::initializeMainThread();
JSC::initializeThreading();
// We can't use destructors in the following code because it uses Windows
// Structured Exception Handling
int res = 0;
- JSGlobalData* globalData = JSGlobalData::create().releaseRef();
TRY
- res = jscmain(argc, argv, globalData);
+ res = jscmain(argc, argv);
EXCEPT(res = 3)
-
- cleanupGlobalData(globalData);
return res;
}
-static void cleanupGlobalData(JSGlobalData* globalData)
-{
- JSLock lock(false);
- globalData->heap.destroy();
- globalData->deref();
-}
-
-static bool runWithScripts(GlobalObject* globalObject, const Vector<UString>& fileNames, bool dump)
+static bool runWithScripts(GlobalObject* globalObject, const Vector<Script>& scripts, bool dump)
{
- Vector<char> script;
+ const char* script;
+ UString fileName;
+ Vector<char> scriptBuffer;
if (dump)
BytecodeGenerator::setDumpsGeneratedCode(true);
-#if ENABLE(OPCODE_SAMPLING)
- Interpreter* interpreter = globalObject->globalData()->interpreter;
- interpreter->setSampler(new SamplingTool(interpreter));
+ JSGlobalData& globalData = globalObject->globalData();
+
+#if ENABLE(SAMPLING_FLAGS)
+ SamplingFlags::start();
#endif
bool success = true;
- for (size_t i = 0; i < fileNames.size(); i++) {
- UString fileName = fileNames[i];
-
- if (!fillBufferWithContentsOfFile(fileName, script))
- return false; // fail early so we can catch missing files
+ for (size_t i = 0; i < scripts.size(); i++) {
+ if (scripts[i].isFile) {
+ fileName = scripts[i].argument;
+ if (!fillBufferWithContentsOfFile(fileName, scriptBuffer))
+ return false; // fail early so we can catch missing files
+ script = scriptBuffer.data();
+ } else {
+ script = scripts[i].argument;
+ fileName = "[Command Line]";
+ }
-#if ENABLE(OPCODE_SAMPLING)
- interpreter->sampler()->start();
-#endif
- Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(script.data(), fileName));
- success = success && completion.complType() != Throw;
- if (dump) {
- if (completion.complType() == Throw)
- printf("Exception: %s\n", completion.value().toString(globalObject->globalExec()).ascii());
- else
- printf("End: %s\n", completion.value().toString(globalObject->globalExec()).ascii());
+ globalData.startSampling();
+
+ JSValue evaluationException;
+ JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(script, fileName), JSValue(), &evaluationException);
+ success = success && !evaluationException;
+ if (dump && !evaluationException)
+ printf("End: %s\n", returnValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
+ if (evaluationException) {
+ printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
+ Identifier stackID(globalObject->globalExec(), "stack");
+ JSValue stackValue = evaluationException.get(globalObject->globalExec(), stackID);
+ if (!stackValue.isUndefinedOrNull())
+ printf("%s\n", stackValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
}
+ globalData.stopSampling();
globalObject->globalExec()->clearException();
-
-#if ENABLE(OPCODE_SAMPLING)
- interpreter->sampler()->stop();
-#endif
}
-#if ENABLE(OPCODE_SAMPLING)
- interpreter->sampler()->dump(globalObject->globalExec());
- delete interpreter->sampler();
+#if ENABLE(SAMPLING_FLAGS)
+ SamplingFlags::stop();
+#endif
+#if ENABLE(SAMPLING_REGIONS)
+ SamplingRegion::dump();
+#endif
+ globalData.dumpSampleData(globalObject->globalExec());
+#if ENABLE(SAMPLING_COUNTERS)
+ AbstractSamplingCounter::dump();
+#endif
+#if ENABLE(REGEXP_TRACING)
+ globalData.dumpRegExpTrace();
#endif
return success;
}
+#define RUNNING_FROM_XCODE 0
+
static void runInteractive(GlobalObject* globalObject)
{
+ UString interpreterName("Interpreter");
+
while (true) {
-#if HAVE(READLINE)
+#if HAVE(READLINE) && !RUNNING_FROM_XCODE
char* line = readline(interactivePrompt);
if (!line)
break;
if (line[0])
add_history(line);
- Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line, interpreterName));
+ JSValue evaluationException;
+ JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(line, interpreterName), JSValue(), &evaluationException);
free(line);
#else
- puts(interactivePrompt);
+ printf("%s", interactivePrompt);
Vector<char, 256> line;
int c;
while ((c = getchar()) != EOF) {
break;
line.append(c);
}
+ if (line.isEmpty())
+ break;
line.append('\0');
- Completion completion = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), makeSource(line.data(), interpreterName));
+
+ JSValue evaluationException;
+ JSValue returnValue = evaluate(globalObject->globalExec(), globalObject->globalScopeChain(), jscSource(line.data(), interpreterName), JSValue(), &evaluationException);
#endif
- if (completion.complType() == Throw)
- printf("Exception: %s\n", completion.value().toString(globalObject->globalExec()).ascii());
+ if (evaluationException)
+ printf("Exception: %s\n", evaluationException.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
else
- printf("%s\n", completion.value().toString(globalObject->globalExec()).UTF8String().c_str());
+ printf("%s\n", returnValue.toString(globalObject->globalExec())->value(globalObject->globalExec()).utf8().data());
globalObject->globalExec()->clearException();
}
printf("\n");
}
-static NO_RETURN void printUsageStatement()
+static NO_RETURN void printUsageStatement(bool help = false)
{
fprintf(stderr, "Usage: jsc [options] [files] [-- arguments]\n");
fprintf(stderr, " -d Dumps bytecode (debug builds only)\n");
+ fprintf(stderr, " -e Evaluate argument as script code\n");
fprintf(stderr, " -f Specifies a source file (deprecated)\n");
fprintf(stderr, " -h|--help Prints this help message\n");
fprintf(stderr, " -i Enables interactive mode (default if no files are specified)\n");
+#if HAVE(SIGNAL_H)
fprintf(stderr, " -s Installs signal handlers that exit on a crash (Unix platforms only)\n");
- exit(EXIT_FAILURE);
+#endif
+
+ exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
}
-static void parseArguments(int argc, char** argv, Options& options)
+static void parseArguments(int argc, char** argv, CommandLine& options)
{
int i = 1;
for (; i < argc; ++i) {
const char* arg = argv[i];
- if (strcmp(arg, "-f") == 0) {
+ if (!strcmp(arg, "-f")) {
if (++i == argc)
printUsageStatement();
- options.fileNames.append(argv[i]);
+ options.scripts.append(Script(true, argv[i]));
continue;
}
- if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
- printUsageStatement();
+ if (!strcmp(arg, "-e")) {
+ if (++i == argc)
+ printUsageStatement();
+ options.scripts.append(Script(false, argv[i]));
+ continue;
}
- if (strcmp(arg, "-i") == 0) {
+ if (!strcmp(arg, "-i")) {
options.interactive = true;
continue;
}
- if (strcmp(arg, "-d") == 0) {
+ if (!strcmp(arg, "-d")) {
options.dump = true;
continue;
}
- if (strcmp(arg, "-s") == 0) {
-#if PLATFORM(UNIX)
+ if (!strcmp(arg, "-s")) {
+#if HAVE(SIGNAL_H)
signal(SIGILL, _exit);
signal(SIGFPE, _exit);
signal(SIGBUS, _exit);
#endif
continue;
}
- if (strcmp(arg, "--") == 0) {
+ if (!strcmp(arg, "--")) {
++i;
break;
}
- options.fileNames.append(argv[i]);
+ if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
+ printUsageStatement(true);
+ options.scripts.append(Script(true, argv[i]));
}
-
- if (options.fileNames.isEmpty())
+
+ if (options.scripts.isEmpty())
options.interactive = true;
-
+
for (; i < argc; ++i)
options.arguments.append(argv[i]);
}
-int jscmain(int argc, char** argv, JSGlobalData* globalData)
+int jscmain(int argc, char** argv)
{
- JSLock lock(false);
+
+ RefPtr<JSGlobalData> globalData = JSGlobalData::create(ThreadStackTypeLarge, LargeHeap);
+ JSLockHolder lock(globalData.get());
- Options options;
+ CommandLine options;
parseArguments(argc, argv, options);
- GlobalObject* globalObject = new (globalData) GlobalObject(options.arguments);
- bool success = runWithScripts(globalObject, options.fileNames, options.dump);
+ GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);
+ bool success = runWithScripts(globalObject, options.scripts, options.dump);
if (options.interactive && success)
runInteractive(globalObject);
static bool fillBufferWithContentsOfFile(const UString& fileName, Vector<char>& buffer)
{
- FILE* f = fopen(fileName.UTF8String().c_str(), "r");
+ FILE* f = fopen(fileName.utf8().data(), "r");
if (!f) {
- fprintf(stderr, "Could not open file: %s\n", fileName.UTF8String().c_str());
+ fprintf(stderr, "Could not open file: %s\n", fileName.utf8().data());
return false;
}
- size_t buffer_size = 0;
- size_t buffer_capacity = 1024;
+ size_t bufferSize = 0;
+ size_t bufferCapacity = 1024;
- buffer.resize(buffer_capacity);
+ buffer.resize(bufferCapacity);
while (!feof(f) && !ferror(f)) {
- buffer_size += fread(buffer.data() + buffer_size, 1, buffer_capacity - buffer_size, f);
- if (buffer_size == buffer_capacity) { // guarantees space for trailing '\0'
- buffer_capacity *= 2;
- buffer.resize(buffer_capacity);
+ bufferSize += fread(buffer.data() + bufferSize, 1, bufferCapacity - bufferSize, f);
+ if (bufferSize == bufferCapacity) { // guarantees space for trailing '\0'
+ bufferCapacity *= 2;
+ buffer.resize(bufferCapacity);
}
}
fclose(f);
- buffer[buffer_size] = '\0';
+ buffer[bufferSize] = '\0';
+
+ if (buffer[0] == '#' && buffer[1] == '!')
+ buffer[0] = buffer[1] = '/';
return true;
}