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