• 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 "JSWorkerContext.h"
35 #include "ScriptSourceCode.h"
36 #include "ScriptValue.h"
37 #include "WorkerContext.h"
38 #include "WorkerMessagingProxy.h"
39 #include "WorkerThread.h"
40 #include <interpreter/Interpreter.h>
41 #include <runtime/Completion.h>
42 #include <runtime/Completion.h>
43 #include <runtime/JSLock.h>
44 
45 using namespace JSC;
46 
47 namespace WebCore {
48 
WorkerScriptController(WorkerContext * workerContext)49 WorkerScriptController::WorkerScriptController(WorkerContext* workerContext)
50     : m_globalData(JSGlobalData::create())
51     , m_workerContext(workerContext)
52     , m_executionForbidden(false)
53 {
54 }
55 
~WorkerScriptController()56 WorkerScriptController::~WorkerScriptController()
57 {
58     m_workerContextWrapper = 0; // Unprotect the global object.
59 
60     ASSERT(!m_globalData->heap.protectedObjectCount());
61     ASSERT(!m_globalData->heap.isBusy());
62     m_globalData->heap.destroy();
63 }
64 
initScript()65 void WorkerScriptController::initScript()
66 {
67     ASSERT(!m_workerContextWrapper);
68 
69     JSLock lock(false);
70 
71     RefPtr<Structure> prototypeStructure = JSWorkerContextPrototype::createStructure(jsNull());
72     RefPtr<Structure> structure = JSWorkerContext::createStructure(new (m_globalData.get()) JSWorkerContextPrototype(prototypeStructure.release()));
73     m_workerContextWrapper = new (m_globalData.get()) JSWorkerContext(structure.release(), m_workerContext);
74 }
75 
evaluate(const ScriptSourceCode & sourceCode)76 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
77 {
78     {
79         MutexLocker lock(m_sharedDataMutex);
80         if (m_executionForbidden)
81             return noValue();
82     }
83 
84     initScriptIfNeeded();
85     JSLock lock(false);
86 
87     ExecState* exec = m_workerContextWrapper->globalExec();
88     m_workerContextWrapper->startTimeoutCheck();
89     Completion comp = JSC::evaluate(exec, exec->dynamicGlobalObject()->globalScopeChain(), sourceCode.jsSourceCode(), m_workerContextWrapper);
90     m_workerContextWrapper->stopTimeoutCheck();
91 
92     m_workerContext->thread()->messagingProxy()->reportWorkerThreadActivity(m_workerContext->hasPendingActivity());
93 
94     if (comp.complType() == Normal || comp.complType() == ReturnValue)
95         return comp.value();
96 
97     if (comp.complType() == Throw)
98         reportException(exec, comp.value());
99     return noValue();
100 }
101 
forbidExecution()102 void WorkerScriptController::forbidExecution()
103 {
104     // This function is called from another thread.
105     // Mutex protection for m_executionForbidden is needed to guarantee that the value is synchronized between processors, because
106     // if it were not, the worker could re-enter JSC::evaluate(), but with timeout already reset.
107     // 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.
108     MutexLocker lock(m_sharedDataMutex);
109     m_executionForbidden = true;
110     m_globalData->interpreter->setTimeoutTime(1); // 1 ms is the smallest timeout that can be set.
111 }
112 
113 } // namespace WebCore
114 
115 #endif // ENABLE(WORKERS)
116