WorkerScriptController.cpp [plain text]
#include "config.h"
#if ENABLE(WORKERS)
#include "WorkerScriptController.h"
#include "JSDOMBinding.h"
#include "JSWorkerContext.h"
#include "ScriptSourceCode.h"
#include "ScriptValue.h"
#include "WorkerContext.h"
#include "WorkerObjectProxy.h"
#include "WorkerThread.h"
#include <interpreter/Interpreter.h>
#include <runtime/Completion.h>
#include <runtime/Completion.h>
#include <runtime/JSLock.h>
using namespace JSC;
namespace WebCore {
WorkerScriptController::WorkerScriptController(WorkerContext* workerContext)
: m_globalData(JSGlobalData::create())
, m_workerContext(workerContext)
, m_executionForbidden(false)
{
}
WorkerScriptController::~WorkerScriptController()
{
m_workerContextWrapper = 0;
ASSERT(!m_globalData->heap.protectedObjectCount());
ASSERT(!m_globalData->heap.isBusy());
m_globalData->heap.destroy();
}
void WorkerScriptController::initScript()
{
ASSERT(!m_workerContextWrapper);
JSLock lock(false);
RefPtr<Structure> prototypeStructure = JSWorkerContextPrototype::createStructure(jsNull());
ProtectedPtr<JSWorkerContextPrototype> prototype = new (m_globalData.get()) JSWorkerContextPrototype(prototypeStructure.release());
RefPtr<Structure> structure = JSWorkerContext::createStructure(prototype);
m_workerContextWrapper = new (m_globalData.get()) JSWorkerContext(structure.release(), m_workerContext);
}
ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
{
{
MutexLocker lock(m_sharedDataMutex);
if (m_executionForbidden)
return JSValue();
}
ScriptValue exception;
ScriptValue result = evaluate(sourceCode, &exception);
if (exception.jsValue()) {
JSLock lock(false);
reportException(m_workerContextWrapper->globalExec(), exception.jsValue());
}
return result;
}
ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
{
{
MutexLocker lock(m_sharedDataMutex);
if (m_executionForbidden)
return JSValue();
}
initScriptIfNeeded();
JSLock lock(false);
ExecState* exec = m_workerContextWrapper->globalExec();
m_workerContextWrapper->globalData()->timeoutChecker.start();
Completion comp = JSC::evaluate(exec, exec->dynamicGlobalObject()->globalScopeChain(), sourceCode.jsSourceCode(), m_workerContextWrapper);
m_workerContextWrapper->globalData()->timeoutChecker.stop();
m_workerContext->thread()->workerObjectProxy().reportPendingActivity(m_workerContext->hasPendingActivity());
if (comp.complType() == Normal || comp.complType() == ReturnValue)
return comp.value();
if (comp.complType() == Throw)
*exception = comp.value();
return JSValue();
}
void WorkerScriptController::setException(ScriptValue exception)
{
m_workerContextWrapper->globalExec()->setException(exception.jsValue());
}
void WorkerScriptController::forbidExecution()
{
MutexLocker lock(m_sharedDataMutex);
m_executionForbidden = true;
m_globalData->timeoutChecker.setTimeoutInterval(1); }
}
#endif // ENABLE(WORKERS)