• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 #include "config.h"
28 
29 #if ENABLE(WORKERS)
30 
31 #include "WorkerScriptController.h"
32 
33 #include "JSDOMBinding.h"
34 #include "JSDedicatedWorkerContext.h"
35 #include "JSSharedWorkerContext.h"
36 #include "ScriptSourceCode.h"
37 #include "ScriptValue.h"
38 #include "WorkerContext.h"
39 #include "WorkerObjectProxy.h"
40 #include "WorkerThread.h"
41 #include <interpreter/Interpreter.h>
42 #include <runtime/Completion.h>
43 #include <runtime/Completion.h>
44 #include <runtime/JSLock.h>
45 
46 using namespace JSC;
47 
48 namespace WebCore {
49 
WorkerScriptController(WorkerContext * workerContext)50 WorkerScriptController::WorkerScriptController(WorkerContext* workerContext)
51     : m_globalData(JSGlobalData::create())
52     , m_workerContext(workerContext)
53     , m_executionForbidden(false)
54 {
55 }
56 
~WorkerScriptController()57 WorkerScriptController::~WorkerScriptController()
58 {
59     m_workerContextWrapper = 0; // Unprotect the global object.
60 
61     ASSERT(!m_globalData->heap.protectedObjectCount());
62     ASSERT(!m_globalData->heap.isBusy());
63     m_globalData->heap.destroy();
64 }
65 
initScript()66 void WorkerScriptController::initScript()
67 {
68     ASSERT(!m_workerContextWrapper);
69 
70     JSLock lock(SilenceAssertionsOnly);
71 
72     // Explicitly protect the global object's prototype so it isn't collected
73     // when we allocate the global object. (Once the global object is fully
74     // constructed, it can mark its own prototype.)
75     RefPtr<Structure> workerContextPrototypeStructure = JSWorkerContextPrototype::createStructure(jsNull());
76     ProtectedPtr<JSWorkerContextPrototype> workerContextPrototype = new (m_globalData.get()) JSWorkerContextPrototype(workerContextPrototypeStructure.release());
77 
78     if (m_workerContext->isDedicatedWorkerContext()) {
79         RefPtr<Structure> dedicatedContextPrototypeStructure = JSDedicatedWorkerContextPrototype::createStructure(workerContextPrototype);
80         ProtectedPtr<JSDedicatedWorkerContextPrototype> dedicatedContextPrototype = new (m_globalData.get()) JSDedicatedWorkerContextPrototype(dedicatedContextPrototypeStructure.release());
81         RefPtr<Structure> structure = JSDedicatedWorkerContext::createStructure(dedicatedContextPrototype);
82 
83         m_workerContextWrapper = new (m_globalData.get()) JSDedicatedWorkerContext(structure.release(), m_workerContext->toDedicatedWorkerContext());
84 #if ENABLE(SHARED_WORKERS)
85     } else {
86         ASSERT(m_workerContext->isSharedWorkerContext());
87         RefPtr<Structure> sharedContextPrototypeStructure = JSSharedWorkerContextPrototype::createStructure(workerContextPrototype);
88         ProtectedPtr<JSSharedWorkerContextPrototype> sharedContextPrototype = new (m_globalData.get()) JSSharedWorkerContextPrototype(sharedContextPrototypeStructure.release());
89         RefPtr<Structure> structure = JSSharedWorkerContext::createStructure(sharedContextPrototype);
90 
91         m_workerContextWrapper = new (m_globalData.get()) JSSharedWorkerContext(structure.release(), m_workerContext->toSharedWorkerContext());
92 #endif
93     }
94 }
95 
evaluate(const ScriptSourceCode & sourceCode)96 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
97 {
98     {
99         MutexLocker lock(m_sharedDataMutex);
100         if (m_executionForbidden)
101             return JSValue();
102     }
103     ScriptValue exception;
104     ScriptValue result = evaluate(sourceCode, &exception);
105     if (exception.jsValue()) {
106         JSLock lock(SilenceAssertionsOnly);
107         reportException(m_workerContextWrapper->globalExec(), exception.jsValue());
108     }
109     return result;
110 }
111 
evaluate(const ScriptSourceCode & sourceCode,ScriptValue * exception)112 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
113 {
114     {
115         MutexLocker lock(m_sharedDataMutex);
116         if (m_executionForbidden)
117             return JSValue();
118     }
119 
120     initScriptIfNeeded();
121     JSLock lock(SilenceAssertionsOnly);
122 
123     ExecState* exec = m_workerContextWrapper->globalExec();
124     m_workerContextWrapper->globalData()->timeoutChecker.start();
125     Completion comp = JSC::evaluate(exec, exec->dynamicGlobalObject()->globalScopeChain(), sourceCode.jsSourceCode(), m_workerContextWrapper);
126     m_workerContextWrapper->globalData()->timeoutChecker.stop();
127 
128     if (comp.complType() == Normal || comp.complType() == ReturnValue)
129         return comp.value();
130 
131     if (comp.complType() == Throw)
132         *exception = comp.value();
133     return JSValue();
134 }
135 
setException(ScriptValue exception)136 void WorkerScriptController::setException(ScriptValue exception)
137 {
138     m_workerContextWrapper->globalExec()->setException(exception.jsValue());
139 }
140 
forbidExecution()141 void WorkerScriptController::forbidExecution()
142 {
143     // This function is called from another thread.
144     // Mutex protection for m_executionForbidden is needed to guarantee that the value is synchronized between processors, because
145     // if it were not, the worker could re-enter JSC::evaluate(), but with timeout already reset.
146     // It is not critical for Interpreter::m_timeoutTime to be synchronized, we just rely on it reaching the worker thread's processor sooner or later.
147     MutexLocker lock(m_sharedDataMutex);
148     m_executionForbidden = true;
149     m_globalData->timeoutChecker.setTimeoutInterval(1); // 1ms is the smallest timeout that can be set.
150 }
151 
152 } // namespace WebCore
153 
154 #endif // ENABLE(WORKERS)
155