2009-07-24 Jian Li <jianli@chromium.org>
authorjianli@chromium.org <jianli@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Jul 2009 17:56:34 +0000 (17:56 +0000)
committerjianli@chromium.org <jianli@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Jul 2009 17:56:34 +0000 (17:56 +0000)
        Reviewed by Adam Barth.

        [V8] Cleanup exception handling in worker evaluation code.
        https://bugs.webkit.org/show_bug.cgi?id=27282

        * bindings/v8/WorkerContextExecutionProxy.cpp:
        (WebCore::WorkerContextExecutionProxy::evaluate):
        * bindings/v8/WorkerContextExecutionProxy.h:
        (WebCore::WorkerContextExecutionState::WorkerContextExecutionState):
        * bindings/v8/WorkerScriptController.cpp:
        (WebCore::WorkerScriptController::evaluate):
        (WebCore::WorkerScriptController::setException):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@46365 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog
WebCore/bindings/v8/WorkerContextExecutionProxy.cpp
WebCore/bindings/v8/WorkerContextExecutionProxy.h
WebCore/bindings/v8/WorkerScriptController.cpp

index 119e540..4e59ea3 100644 (file)
@@ -1,3 +1,18 @@
+2009-07-24  Jian Li  <jianli@chromium.org>
+
+        Reviewed by Adam Barth.
+
+        [V8] Cleanup exception handling in worker evaluation code.
+        https://bugs.webkit.org/show_bug.cgi?id=27282
+
+        * bindings/v8/WorkerContextExecutionProxy.cpp:
+        (WebCore::WorkerContextExecutionProxy::evaluate):
+        * bindings/v8/WorkerContextExecutionProxy.h:
+        (WebCore::WorkerContextExecutionState::WorkerContextExecutionState):
+        * bindings/v8/WorkerScriptController.cpp:
+        (WebCore::WorkerScriptController::evaluate):
+        (WebCore::WorkerScriptController::setException):
+
 2009-07-24  Stephen White  <senorblanco@chromium.org>
 
         Reviewed by David Levin.
index 783eeb5..3749a9b 100644 (file)
@@ -344,16 +344,34 @@ bool WorkerContextExecutionProxy::forgetV8EventObject(Event* event)
         return false;
 }
 
-v8::Local<v8::Value> WorkerContextExecutionProxy::evaluate(const String& script, const String& fileName, int baseLine)
+ScriptValue WorkerContextExecutionProxy::evaluate(const String& script, const String& fileName, int baseLine, WorkerContextExecutionState* state)
 {
     v8::HandleScope hs;
 
     initContextIfNeeded();
     v8::Context::Scope scope(m_context);
 
+    v8::TryCatch exceptionCatcher;
+
     v8::Local<v8::String> scriptString = v8ExternalString(script);
     v8::Handle<v8::Script> compiledScript = V8Proxy::compileScript(scriptString, fileName, baseLine);
-    return runScript(compiledScript);
+    v8::Local<v8::Value> result = runScript(compiledScript);
+
+    if (exceptionCatcher.HasCaught()) {
+        v8::Local<v8::Message> message = exceptionCatcher.Message();
+        state->hadException = true;
+        state->exception = ScriptValue(exceptionCatcher.Exception());
+        state->errorMessage = toWebCoreString(message->Get());
+        state->lineNumber = message->GetLineNumber();
+        state->sourceURL = toWebCoreString(message->GetScriptResourceName());
+        exceptionCatcher.Reset();
+    } else
+        state->hadException = false;
+
+    if (result.IsEmpty() || result->IsUndefined())
+        return ScriptValue();
+
+    return ScriptValue(result);
 }
 
 v8::Local<v8::Value> WorkerContextExecutionProxy::runScript(v8::Handle<v8::Script> script)
index 1265086..387e4c6 100644 (file)
@@ -35,6 +35,7 @@
 #if ENABLE(WORKERS)
 
 #include <v8.h>
+#include "ScriptValue.h"
 #include "V8EventListenerList.h"
 #include "V8Index.h"
 #include <wtf/OwnPtr.h>
@@ -48,6 +49,16 @@ namespace WebCore {
     class V8WorkerContextEventListener;
     class WorkerContext;
 
+    struct WorkerContextExecutionState {
+        WorkerContextExecutionState() : hadException(false), lineNumber(0) { }
+
+        bool hadException;
+        ScriptValue exception;
+        String errorMessage;
+        int lineNumber;
+        String sourceURL;
+    };
+
     class WorkerContextExecutionProxy {
     public:
         WorkerContextExecutionProxy(WorkerContext*);
@@ -73,7 +84,7 @@ namespace WebCore {
         void trackEvent(Event*);
 
         // Evaluate a script file in the current execution environment.
-        v8::Local<v8::Value> evaluate(const String& script, const String& fileName, int baseLine);
+        ScriptValue evaluate(const String& script, const String& fileName, int baseLine, WorkerContextExecutionState*);
 
         // Returns WorkerContext object.
         WorkerContext* workerContext() { return m_workerContext; }
index df1ce9e..99316da 100644 (file)
@@ -62,27 +62,29 @@ WorkerScriptController::~WorkerScriptController()
 
 ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode)
 {
+    return evaluate(sourceCode, 0);
+}
+
+ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
+{
     {
         MutexLocker lock(m_sharedDataMutex);
         if (m_executionForbidden)
             return ScriptValue();
     }
 
-    v8::Local<v8::Value> result = m_proxy->evaluate(sourceCode.source(), sourceCode.url().string(), sourceCode.startLine() - 1);
+    WorkerContextExecutionState state;
+    ScriptValue result = m_proxy->evaluate(sourceCode.source(), sourceCode.url().string(), sourceCode.startLine() - 1, &state);
+    if (state.hadException) {
+        if (exception)
+            *exception = state.exception;
+        else
+            m_workerContext->reportException(state.errorMessage, state.lineNumber, state.sourceURL);
+    }
+
     m_workerContext->thread()->workerObjectProxy().reportPendingActivity(m_workerContext->hasPendingActivity());
-    return ScriptValue();
-}
 
-ScriptValue WorkerScriptController::evaluate(const ScriptSourceCode& sourceCode, ScriptValue* exception)
-{
-    v8::TryCatch exceptionCatcher;
-    ScriptValue result = evaluate(sourceCode);
-    if (exceptionCatcher.HasCaught()) {
-        *exception = ScriptValue(exceptionCatcher.Exception());
-        throwError(exceptionCatcher.Exception());
-        return ScriptValue();
-    } else
-        return result;
+    return result;
 }
 
 void WorkerScriptController::forbidExecution()
@@ -92,9 +94,9 @@ void WorkerScriptController::forbidExecution()
     m_executionForbidden = true;
 }
 
-void WorkerScriptController::setException(ScriptValue /* exception */)
+void WorkerScriptController::setException(ScriptValue exception)
 {
-    notImplemented();
+    throwError(*exception.v8Value());
 }
 
 } // namespace WebCore