Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2009 Google 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 are
      6  * met:
      7  *
      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
     13  * distribution.
     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.
     17  *
     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.
     29  */
     30 
     31 #include "config.h"
     32 
     33 #if ENABLE(WORKERS)
     34 
     35 #include "WorkerScriptController.h"
     36 
     37 #include <v8.h>
     38 
     39 #include "ScriptSourceCode.h"
     40 #include "ScriptValue.h"
     41 #include "DOMTimer.h"
     42 #include "V8DOMMap.h"
     43 #include "V8Proxy.h"
     44 #include "WorkerContext.h"
     45 #include "WorkerContextExecutionProxy.h"
     46 #include "WorkerObjectProxy.h"
     47 #include "WorkerThread.h"
     48 
     49 namespace WebCore {
     50 
     51 WorkerScriptController::WorkerScriptController(WorkerContext* workerContext)
     52     : m_workerContext(workerContext)
     53     , m_proxy(new WorkerContextExecutionProxy(workerContext))
     54     , m_executionForbidden(false)
     55 {
     56 }
     57 
     58 WorkerScriptController::~WorkerScriptController()
     59 {
     60     removeAllDOMObjectsInCurrentThread();
     61 }
     62 
     63 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
     64 {
     65     return evaluate(sourceCode, 0);
     66 }
     67 
     68 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
     69 {
     70     {
     71         MutexLocker lock(m_sharedDataMutex);
     72         if (m_executionForbidden)
     73             return ScriptValue();
     74     }
     75 
     76     WorkerContextExecutionState state;
     77     ScriptValue result = m_proxy->evaluate(sourceCode.source(), sourceCode.url().string(), sourceCode.startLine() - 1, &state);
     78     if (state.hadException) {
     79         if (exception)
     80             *exception = state.exception;
     81         else
     82             m_workerContext->reportException(state.errorMessage, state.lineNumber, state.sourceURL);
     83     }
     84 
     85     return result;
     86 }
     87 
     88 void WorkerScriptController::forbidExecution()
     89 {
     90     // This function is called from another thread.
     91     MutexLocker lock(m_sharedDataMutex);
     92     m_executionForbidden = true;
     93 }
     94 
     95 void WorkerScriptController::setException(ScriptValue exception)
     96 {
     97     throwError(*exception.v8Value());
     98 }
     99 
    100 } // namespace WebCore
    101 
    102 #endif // ENABLE(WORKERS)
    103