1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
* Copyright (C) 2011, 2012 Google Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "config.h"
#if ENABLE(WORKERS)
#include "WorkerScriptController.h"
#include "JSDOMBinding.h"
#include "JSDedicatedWorkerGlobalScope.h"
#include "ScriptSourceCode.h"
#include "ScriptValue.h"
#include "WebCoreJSClientData.h"
#include "WorkerGlobalScope.h"
#include "WorkerObjectProxy.h"
#include "WorkerScriptDebugServer.h"
#include "WorkerThread.h"
#include <heap/StrongInlines.h>
#include <interpreter/Interpreter.h>
#include <runtime/Completion.h>
#include <runtime/ExceptionHelpers.h>
#include <runtime/Error.h>
#include <runtime/JSLock.h>
#if ENABLE(SHARED_WORKERS)
#include "JSSharedWorkerGlobalScope.h"
#endif
using namespace JSC;
namespace WebCore {
WorkerScriptController::WorkerScriptController(WorkerGlobalScope* workerGlobalScope)
: m_vm(VM::create())
, m_workerGlobalScope(workerGlobalScope)
, m_workerGlobalScopeWrapper(*m_vm)
, m_executionForbidden(false)
{
initNormalWorldClientData(m_vm.get());
}
WorkerScriptController::~WorkerScriptController()
{
JSLockHolder lock(vm());
m_workerGlobalScopeWrapper.clear();
m_vm.clear();
}
void WorkerScriptController::initScript()
{
ASSERT(!m_workerGlobalScopeWrapper);
JSLockHolder lock(m_vm.get());
// Explicitly protect the global object's prototype so it isn't collected
// when we allocate the global object. (Once the global object is fully
// constructed, it can mark its own prototype.)
Structure* workerGlobalScopePrototypeStructure = JSWorkerGlobalScopePrototype::createStructure(*m_vm, 0, jsNull());
Strong<JSWorkerGlobalScopePrototype> workerGlobalScopePrototype(*m_vm, JSWorkerGlobalScopePrototype::create(*m_vm, 0, workerGlobalScopePrototypeStructure));
if (m_workerGlobalScope->isDedicatedWorkerGlobalScope()) {
Structure* dedicatedContextPrototypeStructure = JSDedicatedWorkerGlobalScopePrototype::createStructure(*m_vm, 0, workerGlobalScopePrototype.get());
Strong<JSDedicatedWorkerGlobalScopePrototype> dedicatedContextPrototype(*m_vm, JSDedicatedWorkerGlobalScopePrototype::create(*m_vm, 0, dedicatedContextPrototypeStructure));
Structure* structure = JSDedicatedWorkerGlobalScope::createStructure(*m_vm, 0, dedicatedContextPrototype.get());
m_workerGlobalScopeWrapper.set(*m_vm, JSDedicatedWorkerGlobalScope::create(*m_vm, structure, static_cast<DedicatedWorkerGlobalScope*>(m_workerGlobalScope)));
workerGlobalScopePrototypeStructure->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get());
dedicatedContextPrototypeStructure->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get());
ASSERT(structure->globalObject() == m_workerGlobalScopeWrapper);
ASSERT(m_workerGlobalScopeWrapper->structure()->globalObject() == m_workerGlobalScopeWrapper);
workerGlobalScopePrototype->structure()->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get());
dedicatedContextPrototype->structure()->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get());
#if ENABLE(SHARED_WORKERS)
} else {
ASSERT(m_workerGlobalScope->isSharedWorkerGlobalScope());
Structure* sharedContextPrototypeStructure = JSSharedWorkerGlobalScopePrototype::createStructure(*m_vm, 0, workerGlobalScopePrototype.get());
Strong<JSSharedWorkerGlobalScopePrototype> sharedContextPrototype(*m_vm, JSSharedWorkerGlobalScopePrototype::create(*m_vm, 0, sharedContextPrototypeStructure));
Structure* structure = JSSharedWorkerGlobalScope::createStructure(*m_vm, 0, sharedContextPrototype.get());
m_workerGlobalScopeWrapper.set(*m_vm, JSSharedWorkerGlobalScope::create(*m_vm, structure, static_cast<SharedWorkerGlobalScope*>(m_workerGlobalScope)));
workerGlobalScopePrototype->structure()->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get());
sharedContextPrototype->structure()->setGlobalObject(*m_vm, m_workerGlobalScopeWrapper.get());
#endif
}
ASSERT(m_workerGlobalScopeWrapper->globalObject() == m_workerGlobalScopeWrapper);
ASSERT(asObject(m_workerGlobalScopeWrapper->prototype())->globalObject() == m_workerGlobalScopeWrapper);
}
void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
{
if (isExecutionForbidden())
return;
ScriptValue exception;
evaluate(sourceCode, &exception);
if (exception.jsValue()) {
JSLockHolder lock(vm());
reportException(m_workerGlobalScopeWrapper->globalExec(), exception.jsValue());
}
}
void WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
{
if (isExecutionForbidden())
return;
initScriptIfNeeded();
ExecState* exec = m_workerGlobalScopeWrapper->globalExec();
JSLockHolder lock(exec);
JSValue evaluationException;
JSC::evaluate(exec, sourceCode.jsSourceCode(), m_workerGlobalScopeWrapper.get(), &evaluationException);
if ((evaluationException && isTerminatedExecutionException(evaluationException)) || m_workerGlobalScopeWrapper->vm().watchdog.didFire()) {
forbidExecution();
return;
}
if (evaluationException) {
String errorMessage;
int lineNumber = 0;
int columnNumber = 0;
String sourceURL = sourceCode.url().string();
if (m_workerGlobalScope->sanitizeScriptError(errorMessage, lineNumber, columnNumber, sourceURL, sourceCode.cachedScript()))
*exception = ScriptValue(*m_vm, throwError(exec, createError(exec, errorMessage.impl())));
else
*exception = ScriptValue(*m_vm, evaluationException);
}
}
void WorkerScriptController::setException(const ScriptValue& exception)
{
throwError(m_workerGlobalScopeWrapper->globalExec(), exception.jsValue());
}
void WorkerScriptController::scheduleExecutionTermination()
{
// The mutex provides a memory barrier to ensure that once
// termination is scheduled, isExecutionTerminating will
// accurately reflect that state when called from another thread.
MutexLocker locker(m_scheduledTerminationMutex);
m_vm->watchdog.fire();
}
bool WorkerScriptController::isExecutionTerminating() const
{
// See comments in scheduleExecutionTermination regarding mutex usage.
MutexLocker locker(m_scheduledTerminationMutex);
return m_vm->watchdog.didFire();
}
void WorkerScriptController::forbidExecution()
{
ASSERT(m_workerGlobalScope->isContextThread());
m_executionForbidden = true;
}
bool WorkerScriptController::isExecutionForbidden() const
{
ASSERT(m_workerGlobalScope->isContextThread());
return m_executionForbidden;
}
void WorkerScriptController::disableEval(const String& errorMessage)
{
initScriptIfNeeded();
JSLockHolder lock(vm());
m_workerGlobalScopeWrapper->setEvalEnabled(false, errorMessage);
}
void WorkerScriptController::attachDebugger(JSC::Debugger* debugger)
{
initScriptIfNeeded();
debugger->attach(m_workerGlobalScopeWrapper->globalObject());
}
void WorkerScriptController::detachDebugger(JSC::Debugger* debugger)
{
debugger->detach(m_workerGlobalScopeWrapper->globalObject());
}
} // namespace WebCore
#endif // ENABLE(WORKERS)
|