2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "WorkerScriptController.h"
38 #include "ScriptCallStack.h"
39 #include "ScriptSourceCode.h"
40 #include "ScriptValue.h"
43 #include "V8WorkerContext.h"
44 #include "WorkerContext.h"
45 #include "WorkerContextExecutionProxy.h"
46 #include "WorkerObjectProxy.h"
47 #include "WorkerThread.h"
52 WorkerScriptController::WorkerScriptController(WorkerContext* workerContext)
53 : m_workerContext(workerContext)
54 , m_isolate(v8::Isolate::New())
55 , m_executionForbidden(false)
57 V8BindingPerIsolateData* data = V8BindingPerIsolateData::create(m_isolate);
58 data->allStores().append(&m_DOMDataStore);
59 data->setDOMDataStore(&m_DOMDataStore);
61 m_proxy = adoptPtr(new WorkerContextExecutionProxy(workerContext));
64 WorkerScriptController::~WorkerScriptController()
66 removeAllDOMObjects();
69 V8BindingPerIsolateData::dispose(m_isolate);
73 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
75 return evaluate(sourceCode, 0);
78 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
80 if (isExecutionForbidden())
83 WorkerContextExecutionState state;
84 ScriptValue result = m_proxy->evaluate(sourceCode.source(), sourceCode.url().string(), WTF::toZeroBasedTextPosition(sourceCode.startPosition()), &state);
85 if (state.hadException) {
87 *exception = state.exception;
89 m_workerContext->reportException(state.errorMessage, state.lineNumber, state.sourceURL, 0);
95 void WorkerScriptController::scheduleExecutionTermination()
97 v8::V8::TerminateExecution(m_isolate);
100 void WorkerScriptController::forbidExecution()
102 ASSERT(m_workerContext->isContextThread());
103 m_executionForbidden = true;
106 bool WorkerScriptController::isExecutionForbidden() const
108 ASSERT(m_workerContext->isContextThread());
109 return m_executionForbidden;
112 void WorkerScriptController::setException(ScriptValue exception)
114 throwError(*exception.v8Value());
117 WorkerScriptController* WorkerScriptController::controllerForContext()
119 // Happens on frame destruction, check otherwise GetCurrent() will crash.
120 if (!v8::Context::InContext())
122 v8::Handle<v8::Context> context = v8::Context::GetCurrent();
123 v8::Handle<v8::Object> global = context->Global();
124 global = V8DOMWrapper::lookupDOMWrapper(V8WorkerContext::GetTemplate(), global);
125 // Return 0 if the current executing context is not the worker context.
126 if (global.IsEmpty())
128 WorkerContext* workerContext = V8WorkerContext::toNative(global);
129 return workerContext->script();
132 } // namespace WebCore
134 #endif // ENABLE(WORKERS)