#include "config.h"
#include "interpreter.h"
#include "ExecState.h"
#include "JSGlobalObject.h"
#include "Parser.h"
#include "SavedBuiltins.h"
#include "array_object.h"
#include "bool_object.h"
#include "collector.h"
#include "date_object.h"
#include "debugger.h"
#include "error_object.h"
#include "function_object.h"
#include "internal.h"
#include "math_object.h"
#include "nodes.h"
#include "number_object.h"
#include "object.h"
#include "object_object.h"
#include "operations.h"
#include "regexp_object.h"
#include "runtime.h"
#include "string_object.h"
#include "types.h"
#include "value.h"
#include <math.h>
#include <stdio.h>
#include <wtf/Assertions.h>
namespace KJS {
Completion Interpreter::checkSyntax(ExecState* exec, const SourceCode& source)
{
JSLock lock;
int errLine;
UString errMsg;
RefPtr<ProgramNode> progNode = parser().parse<ProgramNode>(source, &errLine, &errMsg);
if (!progNode)
return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, source.provider()->asID(), source.provider()->url()));
return Completion(Normal);
}
Completion Interpreter::evaluate(ExecState* exec, const SourceCode& source, JSValue* thisV)
{
JSLock lock;
JSGlobalObject* globalObject = exec->dynamicGlobalObject();
if (globalObject->recursion() >= 20)
return Completion(Throw, Error::create(exec, GeneralError, "Recursion too deep"));
int errLine;
UString errMsg;
RefPtr<ProgramNode> progNode = parser().parse<ProgramNode>(source, &errLine, &errMsg);
if (!progNode)
return Completion(Throw, Error::create(exec, SyntaxError, errMsg, errLine, source.provider()->asID(), source.provider()->url()));
exec->clearException();
globalObject->incRecursion();
JSObject* thisObj = globalObject;
if (thisV && !thisV->isUndefinedOrNull())
thisObj = thisV->toObject(exec);
Completion res;
if (exec->hadException())
res = Completion(Throw, exec->exception());
else {
InterpreterExecState newExec(globalObject, thisObj, progNode.get());
JSValue* value = progNode->execute(&newExec);
res = Completion(newExec.completionType(), value);
}
globalObject->decRecursion();
if (shouldPrintExceptions() && res.complType() == Throw) {
JSLock lock;
ExecState* exec = globalObject->globalExec();
CString f = source.provider()->url().UTF8String();
CString message = res.value()->toObject(exec)->toString(exec).UTF8String();
int line = res.value()->toObject(exec)->get(exec, "line")->toUInt32(exec);
#if PLATFORM(WIN_OS)
printf("%s line %d: %s\n", f.c_str(), line, message.c_str());
#else
printf("[%d] %s line %d: %s\n", getpid(), f.c_str(), line, message.c_str());
#endif
}
return res;
}
static bool printExceptions = false;
bool Interpreter::shouldPrintExceptions()
{
return printExceptions;
}
void Interpreter::setShouldPrintExceptions(bool print)
{
printExceptions = print;
}
}