[V8] Move evaluate() from V8Proxy to ScriptController
authorharaken@chromium.org <haraken@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Aug 2012 03:42:54 +0000 (03:42 +0000)
committerharaken@chromium.org <haraken@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Aug 2012 03:42:54 +0000 (03:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=94768

Reviewed by Adam Barth.

To kill V8Proxy, we can move evaluate() from V8Proxy to ScriptController.

- Renamed evaluate() to compileAndRunScript() to avoid name conflict
with existing ScriptController::evaluate().

- Removed toV8Proxy() from NPV8Object, as it is no longer used.

No tests. No change in behavior.

* bindings/v8/NPV8Object.cpp:
(_NPN_EvaluateHelper):
* bindings/v8/ScheduledAction.cpp:
(WebCore::ScheduledAction::execute):
* bindings/v8/ScriptController.cpp:
(WebCore::ScriptController::compileAndRunScript):
(WebCore):
(WebCore::ScriptController::evaluate):
(WebCore::ScriptController::evaluateInIsolatedWorld):
* bindings/v8/ScriptController.h:
(ScriptController):
* bindings/v8/V8Proxy.cpp:
* bindings/v8/V8Proxy.h:

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

Source/WebCore/ChangeLog
Source/WebCore/bindings/v8/NPV8Object.cpp
Source/WebCore/bindings/v8/ScheduledAction.cpp
Source/WebCore/bindings/v8/ScriptController.cpp
Source/WebCore/bindings/v8/ScriptController.h
Source/WebCore/bindings/v8/V8Proxy.cpp
Source/WebCore/bindings/v8/V8Proxy.h

index ed3649f..e3a70cd 100644 (file)
@@ -1,3 +1,33 @@
+2012-08-22  Kentaro Hara  <haraken@chromium.org>
+
+        [V8] Move evaluate() from V8Proxy to ScriptController
+        https://bugs.webkit.org/show_bug.cgi?id=94768
+
+        Reviewed by Adam Barth.
+
+        To kill V8Proxy, we can move evaluate() from V8Proxy to ScriptController.
+
+        - Renamed evaluate() to compileAndRunScript() to avoid name conflict
+        with existing ScriptController::evaluate().
+
+        - Removed toV8Proxy() from NPV8Object, as it is no longer used.
+
+        No tests. No change in behavior.
+
+        * bindings/v8/NPV8Object.cpp:
+        (_NPN_EvaluateHelper):
+        * bindings/v8/ScheduledAction.cpp:
+        (WebCore::ScheduledAction::execute):
+        * bindings/v8/ScriptController.cpp:
+        (WebCore::ScriptController::compileAndRunScript):
+        (WebCore):
+        (WebCore::ScriptController::evaluate):
+        (WebCore::ScriptController::evaluateInIsolatedWorld):
+        * bindings/v8/ScriptController.h:
+        (ScriptController):
+        * bindings/v8/V8Proxy.cpp:
+        * bindings/v8/V8Proxy.h:
+
 2012-08-22  Vineet Chaudhary  <rgf748@motorola.com>
 
         Consider replacing return type of Clipboard::types() from HashSet<String> to Vector<String>.
index af7d99e..829d9ef 100644 (file)
@@ -69,15 +69,6 @@ static v8::Local<v8::Context> toV8Context(NPP npp, NPObject* npObject)
     return ScriptController::mainWorldContext(object->rootObject->frame());
 }
 
-static V8Proxy* toV8Proxy(NPObject* npObject)
-{
-    V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject);
-    Frame* frame = object->rootObject->frame();
-    if (!frame)
-        return 0;
-    return frame->script()->proxy();
-}
-
 static V8NPObjectMap* staticV8NPObjectMap()
 {
     DEFINE_STATIC_LOCAL(V8NPObjectMap, v8npObjectMap, ());
@@ -335,9 +326,6 @@ bool _NPN_EvaluateHelper(NPP npp, bool popupsAllowed, NPObject* npObject, NPStri
     if (context.IsEmpty())
         return false;
 
-    V8Proxy* proxy = toV8Proxy(npObject);
-    ASSERT(proxy);
-
     v8::Context::Scope scope(context);
     ExceptionCatcher exceptionCatcher;
 
@@ -346,10 +334,14 @@ bool _NPN_EvaluateHelper(NPP npp, bool popupsAllowed, NPObject* npObject, NPStri
     if (!popupsAllowed)
         filename = "npscript";
 
+    V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject);
+    Frame* frame = v8NpObject->rootObject->frame();
+    ASSERT(frame);
+
     String script = String::fromUTF8(npScript->UTF8Characters, npScript->UTF8Length);
 
     UserGestureIndicator gestureIndicator(popupsAllowed ? DefinitelyProcessingUserGesture : PossiblyProcessingUserGesture);
-    v8::Local<v8::Value> v8result = proxy->evaluate(ScriptSourceCode(script, KURL(ParsedURLString, filename)), 0);
+    v8::Local<v8::Value> v8result = frame->script()->compileAndRunScript(ScriptSourceCode(script, KURL(ParsedURLString, filename)));
 
     if (v8result.IsEmpty())
         return false;
index d56f677..b0542e2 100644 (file)
@@ -132,7 +132,7 @@ void ScheduledAction::execute(ScriptController* script)
     if (!m_function.IsEmpty() && m_function->IsFunction())
         script->callFunction(v8::Persistent<v8::Function>::Cast(m_function), v8Context->Global(), m_argc, m_argv);
     else
-        script->proxy()->evaluate(m_code, 0);
+        script->compileAndRunScript(m_code);
 
     // The 'proxy' may be invalid at this point since JS could have released the owning Frame.
 }
index af5ad69..058b873 100644 (file)
@@ -50,6 +50,7 @@
 #include "PlatformSupport.h"
 #include "ScriptCallStack.h"
 #include "ScriptCallStackFactory.h"
+#include "ScriptRunner.h"
 #include "ScriptSourceCode.h"
 #include "ScriptableDocumentParser.h"
 #include "SecurityOrigin.h"
@@ -256,6 +257,73 @@ ScriptValue ScriptController::callFunctionEvenIfScriptDisabled(v8::Handle<v8::Fu
     return ScriptValue(callFunction(function, receiver, argc, argv));
 }
 
+v8::Local<v8::Value> ScriptController::compileAndRunScript(const ScriptSourceCode& source)
+{
+    ASSERT(v8::Context::InContext());
+
+    V8GCController::checkMemoryUsage();
+
+    InspectorInstrumentationCookie cookie = InspectorInstrumentation::willEvaluateScript(m_frame, source.url().isNull() ? String() : source.url().string(), source.startLine());
+
+    v8::Local<v8::Value> result;
+    {
+        // Isolate exceptions that occur when compiling and executing
+        // the code. These exceptions should not interfere with
+        // javascript code we might evaluate from C++ when returning
+        // from here.
+        v8::TryCatch tryCatch;
+        tryCatch.SetVerbose(true);
+
+        // Compile the script.
+        v8::Local<v8::String> code = v8ExternalString(source.source());
+#if PLATFORM(CHROMIUM)
+        TRACE_EVENT_BEGIN0("v8", "v8.compile");
+#endif
+        OwnPtr<v8::ScriptData> scriptData = ScriptSourceCode::precompileScript(code, source.cachedScript());
+
+        // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at
+        // 1, whereas v8 starts at 0.
+        v8::Handle<v8::Script> script = ScriptSourceCode::compileScript(code, source.url(), source.startPosition(), scriptData.get());
+#if PLATFORM(CHROMIUM)
+        TRACE_EVENT_END0("v8", "v8.compile");
+        TRACE_EVENT0("v8", "v8.run");
+#endif
+
+        // Keep Frame (and therefore ScriptController) alive.
+        RefPtr<Frame> protect(m_frame);
+        result = ScriptRunner::runCompiledScript(script, m_frame->document());
+    }
+
+    InspectorInstrumentation::didEvaluateScript(cookie);
+
+    return result;
+}
+
+ScriptValue ScriptController::evaluate(const ScriptSourceCode& sourceCode)
+{
+    String sourceURL = sourceCode.url();
+    const String* savedSourceURL = m_sourceURL;
+    m_sourceURL = &sourceURL;
+
+    v8::HandleScope handleScope;
+    v8::Handle<v8::Context> v8Context = ScriptController::mainWorldContext(m_frame);
+    if (v8Context.IsEmpty())
+        return ScriptValue();
+
+    v8::Context::Scope scope(v8Context);
+
+    RefPtr<Frame> protect(m_frame);
+
+    v8::Local<v8::Value> object = compileAndRunScript(sourceCode);
+
+    m_sourceURL = savedSourceURL;
+
+    if (object.IsEmpty())
+        return ScriptValue();
+
+    return ScriptValue(object);
+}
+
 void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>& sources, Vector<ScriptValue>* results)
 {
     evaluateInIsolatedWorld(worldID, sources, 0, results);
@@ -304,7 +372,7 @@ void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<Sc
         v8::Local<v8::Array> resultArray = v8::Array::New(sources.size());
 
         for (size_t i = 0; i < sources.size(); ++i) {
-            v8::Local<v8::Value> evaluationResult = m_proxy->evaluate(sources[i], 0);
+            v8::Local<v8::Value> evaluationResult = compileAndRunScript(sources[i]);
             if (evaluationResult.IsEmpty())
                 evaluationResult = v8::Local<v8::Value>::New(v8::Undefined());
             resultArray->Set(i, evaluationResult);
@@ -331,32 +399,6 @@ void ScriptController::setIsolatedWorldSecurityOrigin(int worldID, PassRefPtr<Se
         iter->second->setSecurityOrigin(securityOrigin);
 }
 
-// Evaluate a script file in the environment of this proxy.
-ScriptValue ScriptController::evaluate(const ScriptSourceCode& sourceCode)
-{
-    String sourceURL = sourceCode.url();
-    const String* savedSourceURL = m_sourceURL;
-    m_sourceURL = &sourceURL;
-
-    v8::HandleScope handleScope;
-    v8::Handle<v8::Context> v8Context = ScriptController::mainWorldContext(m_proxy->frame());
-    if (v8Context.IsEmpty())
-        return ScriptValue();
-
-    v8::Context::Scope scope(v8Context);
-
-    RefPtr<Frame> protect(m_frame);
-
-    v8::Local<v8::Value> object = m_proxy->evaluate(sourceCode, 0);
-
-    m_sourceURL = savedSourceURL;
-
-    if (object.IsEmpty())
-        return ScriptValue();
-
-    return ScriptValue(object);
-}
-
 TextPosition ScriptController::eventHandlerPosition() const
 {
     ScriptableDocumentParser* parser = m_frame->document()->scriptableDocumentParser();
index 1e4bba3..5f9b67f 100644 (file)
@@ -88,7 +88,10 @@ public:
     // This function must be called from the main thread. It is safe to call it repeatedly.
     static void initializeThreading();
 
-    // Evaluate a script file in the environment of this proxy.
+    v8::Local<v8::Value> compileAndRunScript(const ScriptSourceCode&);
+
+    // Evaluate JavaScript in the main world.
+    // The caller must hold an execution context.
     ScriptValue evaluate(const ScriptSourceCode&);
 
     // Evaluate JavaScript in a new isolated world. The script gets its own
index 1a7462b..ed36d86 100644 (file)
@@ -88,48 +88,6 @@ V8Proxy::~V8Proxy()
     windowShell()->destroyGlobal();
 }
 
-v8::Local<v8::Value> V8Proxy::evaluate(const ScriptSourceCode& source, Node* node)
-{
-    ASSERT(v8::Context::InContext());
-
-    V8GCController::checkMemoryUsage();
-
-    InspectorInstrumentationCookie cookie = InspectorInstrumentation::willEvaluateScript(m_frame, source.url().isNull() ? String() : source.url().string(), source.startLine());
-
-    v8::Local<v8::Value> result;
-    {
-        // Isolate exceptions that occur when compiling and executing
-        // the code. These exceptions should not interfere with
-        // javascript code we might evaluate from C++ when returning
-        // from here.
-        v8::TryCatch tryCatch;
-        tryCatch.SetVerbose(true);
-
-        // Compile the script.
-        v8::Local<v8::String> code = v8ExternalString(source.source());
-#if PLATFORM(CHROMIUM)
-        TRACE_EVENT_BEGIN0("v8", "v8.compile");
-#endif
-        OwnPtr<v8::ScriptData> scriptData = ScriptSourceCode::precompileScript(code, source.cachedScript());
-
-        // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at
-        // 1, whereas v8 starts at 0.
-        v8::Handle<v8::Script> script = ScriptSourceCode::compileScript(code, source.url(), source.startPosition(), scriptData.get());
-#if PLATFORM(CHROMIUM)
-        TRACE_EVENT_END0("v8", "v8.compile");
-        TRACE_EVENT0("v8", "v8.run");
-#endif
-
-        // Keep Frame (and therefore ScriptController) alive.
-        RefPtr<Frame> protect(frame());
-        result = ScriptRunner::runCompiledScript(script, frame()->document());
-    }
-
-    InspectorInstrumentation::didEvaluateScript(cookie);
-
-    return result;
-}
-
 V8DOMWindowShell* V8Proxy::windowShell() const
 {
     return frame()->script()->windowShell();
index 6300a19..92340b8 100644 (file)
@@ -87,11 +87,6 @@ namespace WebCore {
 
         Frame* frame() const { return m_frame; }
 
-        // Evaluate a script file in the current execution environment.
-        // The caller must hold an execution context.
-        // If cannot evalute the script, it returns an error.
-        v8::Local<v8::Value> evaluate(const ScriptSourceCode&, Node*);
-
         // FIXME: This should eventually take DOMWrapperWorld argument!
         // FIXME: This method will be soon removed, as all methods that access windowShell()
         // will be moved to ScriptController.