2011-05-25 Caio Marcelo de Oliveira Filho <caio.oliveira@openbossa.org>
authorcaio.oliveira@openbossa.org <caio.oliveira@openbossa.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 25 May 2011 19:28:22 +0000 (19:28 +0000)
committercaio.oliveira@openbossa.org <caio.oliveira@openbossa.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 25 May 2011 19:28:22 +0000 (19:28 +0000)
        Reviewed by Andreas Kling.

        [Qt] JSC bridge: implement __qt_sender__ without using Scope Chain
        https://bugs.webkit.org/show_bug.cgi?id=61343

        Create a stack to keep track of the sender objects. This is simpler than
        the similar mechanism in QObject (C++ API), that keeps a stack per-object.

        Since we do not support multiple threads, one static stack will be enough for
        handling the behavior.

        This behavior is covered by the tst_QWebFrame::connectAndDisconnect() auto test.

        * bridge/qt/qt_instance.cpp:
        (JSC::Bindings::QtInstance::qtSenderStack):
        We have one static stack of QObject*. The top of the stack contains the
        last object that emitted signal that called a JavaScript function.

        * bridge/qt/qt_instance.h:
        (JSC::Bindings::QtInstance::QtSenderStack::top):
        (JSC::Bindings::QtInstance::QtSenderStack::push):
        (JSC::Bindings::QtInstance::QtSenderStack::pop):
        Minimal functionality to manipulate the sender stack.

        * bridge/qt/qt_runtime.cpp:
        (JSC::Bindings::QtConnectionObject::execute):
        Remove the previous code that modified the scope chain. Push the sender object
        to the stack before calling the JavaScript function (the "slot" in Qt-speak) and
        pop it afterwards.
2011-05-25  Caio Marcelo de Oliveira Filho  <caio.oliveira@openbossa.org>

        Reviewed by Andreas Kling.

        [Qt] JSC bridge: implement __qt_sender__ without using Scope Chain
        https://bugs.webkit.org/show_bug.cgi?id=61343

        Create a '__qt_sender__' property in the global object, that returns the top of
        the qtSenderStack. This is an alternative implementation for the feature of
        providing a way for a function (acting as a Qt 'slot') discover which object
        emitted the signal that caused it to be executed.

        This reduces the coupling of the Qt bridge and JSC internal implementation. The
        patch tries to use as much JSC public API as possible.

        This behavior is covered by the tst_QWebFrame::connectAndDisconnect() auto test.

        * WebCoreSupport/FrameLoaderClientQt.cpp:
        (WebCore::FrameLoaderClientQt::dispatchDidClearWindowObjectInWorld):
        Instead of emitting the QWebPage::javaScriptWindowObjectCleared() directly, calls
        a QWebPagePrivate function to do it.

        * Api/qwebframe_p.h:
        * Api/qwebframe.cpp:
        (QWebFramePrivate::didClearedWindowObject):
        Before emitting the signal mentioned, adds the '__qt_sender__' to the fresh
        global object.

        (qtSenderCallback):
        Returns the JSObjectRef corresponding to the top of qtSenderStack.

        (QWebFramePrivate::addQtSenderToGlobalObject):
        Create a property with a qtSenderCallback as getter function in the global object.

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

Source/WebCore/ChangeLog
Source/WebCore/bridge/qt/qt_instance.cpp
Source/WebCore/bridge/qt/qt_instance.h
Source/WebCore/bridge/qt/qt_runtime.cpp
Source/WebKit/qt/Api/qwebframe.cpp
Source/WebKit/qt/Api/qwebframe_p.h
Source/WebKit/qt/ChangeLog
Source/WebKit/qt/WebCoreSupport/FrameLoaderClientQt.cpp

index 663160d..d2ab0d5 100644 (file)
@@ -1,3 +1,35 @@
+2011-05-25  Caio Marcelo de Oliveira Filho  <caio.oliveira@openbossa.org>
+
+        Reviewed by Andreas Kling.
+
+        [Qt] JSC bridge: implement __qt_sender__ without using Scope Chain
+        https://bugs.webkit.org/show_bug.cgi?id=61343
+
+        Create a stack to keep track of the sender objects. This is simpler than
+        the similar mechanism in QObject (C++ API), that keeps a stack per-object.
+
+        Since we do not support multiple threads, one static stack will be enough for
+        handling the behavior.
+
+        This behavior is covered by the tst_QWebFrame::connectAndDisconnect() auto test.
+
+        * bridge/qt/qt_instance.cpp:
+        (JSC::Bindings::QtInstance::qtSenderStack):
+        We have one static stack of QObject*. The top of the stack contains the
+        last object that emitted signal that called a JavaScript function.
+
+        * bridge/qt/qt_instance.h:
+        (JSC::Bindings::QtInstance::QtSenderStack::top):
+        (JSC::Bindings::QtInstance::QtSenderStack::push):
+        (JSC::Bindings::QtInstance::QtSenderStack::pop):
+        Minimal functionality to manipulate the sender stack.
+
+        * bridge/qt/qt_runtime.cpp:
+        (JSC::Bindings::QtConnectionObject::execute):
+        Remove the previous code that modified the scope chain. Push the sender object
+        to the stack before calling the JavaScript function (the "slot" in Qt-speak) and
+        pop it afterwards.
+
 2011-05-25  Sheriff Bot  <webkit.review.bot@gmail.com>
 
         Unreviewed, rolling out r87257.
index 68e2954..df39bbc 100644 (file)
@@ -44,6 +44,9 @@ namespace Bindings {
 typedef QMultiHash<void*, QtInstance*> QObjectInstanceMap;
 static QObjectInstanceMap cachedInstances;
 
+// Used for implementing '__qt_sender__'.
+Q_GLOBAL_STATIC(QtInstance::QtSenderStack, senderStack)
+
 // Derived RuntimeObject
 class QtRuntimeObject : public RuntimeObject {
 public:
@@ -321,6 +324,11 @@ JSValue QtInstance::valueOf(ExecState* exec) const
     return stringValue(exec);
 }
 
+QtInstance::QtSenderStack* QtInstance::qtSenderStack()
+{
+    return senderStack();
+}
+
 // In qt_runtime.cpp
 JSValue convertQVariantToValue(ExecState*, PassRefPtr<RootObject> root, const QVariant& variant);
 QVariant convertValueToQVariant(ExecState*, JSValue, QMetaType::Type hint, int *distance);
index 1d51ca2..6935022 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "BridgeJSC.h"
 #include "runtime_root.h"
+#include <QStack>
 #include <QtScript/qscriptengine.h>
 #include <qhash.h>
 #include <qpointer.h>
@@ -71,6 +72,18 @@ public:
 
     static QtInstance* getInstance(JSObject*);
 
+    class QtSenderStack {
+    public:
+        QObject* top() const { return m_stack.isEmpty() ? 0 : m_stack.top(); }
+        void push(QObject* object) { m_stack.push(object); }
+        void pop() { Q_ASSERT(!m_stack.isEmpty()); m_stack.pop(); }
+    private:
+        QStack<QObject*> m_stack;
+    };
+
+    // Used to implement '__qt_sender__'.
+    static QtSenderStack* qtSenderStack();
+
 private:
     static PassRefPtr<QtInstance> create(QObject *instance, PassRefPtr<RootObject> rootObject, QScriptEngine::ValueOwnership ownership)
     {
index 62d4417..a17eb31 100644 (file)
@@ -1838,26 +1838,17 @@ void QtConnectionObject::execute(void **argv)
                             l.append(jsUndefined());
                         }
                     }
-                    // Stuff in the __qt_sender property, if we can
-                    ScopeChainNode* oldsc = 0;
-                    JSFunction* fimp = 0;
-                    if (m_funcObject->inherits(&JSFunction::s_info)) {
-                        fimp = static_cast<JSFunction*>(m_funcObject.get());
-
-                        JSObject* qt_sender = QtInstance::getQtInstance(sender(), ro, QScriptEngine::QtOwnership)->createRuntimeObject(exec);
-                        JSObject* wrapper = constructEmptyObject(exec, createEmptyObjectStructure(exec->globalData(), jsNull()));
-                        PutPropertySlot slot;
-                        wrapper->put(exec, Identifier(exec, "__qt_sender__"), qt_sender, slot);
-                        oldsc = fimp->scope();
-                        fimp->setScope(exec->globalData(), oldsc->push(wrapper));
-                    }
+
+                    const bool withQtSenderStack = m_funcObject->inherits(&JSFunction::s_info);
+                    if (withQtSenderStack)
+                        QtInstance::qtSenderStack()->push(QObject::sender());
 
                     CallData callData;
                     CallType callType = m_funcObject->getCallData(callData);
                     call(exec, m_funcObject.get(), callType, callData, m_thisObject.get(), l);
 
-                    if (fimp)
-                        fimp->setScope(exec->globalData(), oldsc);
+                    if (withQtSenderStack)
+                        QtInstance::qtSenderStack()->pop();
                 }
             }
         }
index bc87dd1..cc2ef0d 100644 (file)
@@ -22,6 +22,7 @@
 #include "qwebframe.h"
 
 #if USE(JSC)
+#include "APICast.h"
 #include "BridgeJSC.h"
 #include "CallFrame.h"
 #elif USE(V8)
 #include "IconDatabase.h"
 #include "InspectorController.h"
 #if USE(JSC)
+#include "JavaScript.h"
 #include "JSDOMBinding.h"
 #include "JSDOMWindowBase.h"
 #include "JSLock.h"
 #include "JSObject.h"
+#include "JSRetainPtr.h"
+#include "OpaqueJSString.h"
 #elif USE(V8)
 #include "V8DOMWrapper.h"
 #include "V8DOMWindowShell.h"
@@ -471,6 +475,49 @@ void QWebFramePrivate::_q_orientationChanged()
     frame->sendOrientationChangeEvent(orientation);
 #endif
 }
+
+void QWebFramePrivate::didClearWindowObject()
+{
+#if USE(JSC)
+    if (page->settings()->testAttribute(QWebSettings::JavascriptEnabled))
+        addQtSenderToGlobalObject();
+#endif
+    emit q->javaScriptWindowObjectCleared();
+}
+
+#if USE(JSC)
+static JSValueRef qtSenderCallback(JSContextRef context, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef*)
+{
+    QObject* sender = JSC::Bindings::QtInstance::qtSenderStack()->top();
+    if (!sender)
+        return JSValueMakeUndefined(context);
+
+    JSC::ExecState* exec = ::toJS(context);
+    RefPtr<JSC::Bindings::RootObject> rootObject = JSC::Bindings::findRootObject(exec->dynamicGlobalObject());
+    JSC::JSObject* jsSender = JSC::Bindings::QtInstance::getQtInstance(sender, rootObject, QScriptEngine::QtOwnership)->createRuntimeObject(exec);
+    return ::toRef(jsSender);
+}
+
+void QWebFramePrivate::addQtSenderToGlobalObject()
+{
+    JSC::JSLock lock(JSC::SilenceAssertionsOnly);
+
+    JSDOMWindow* window = toJSDOMWindow(frame, mainThreadNormalWorld());
+    Q_ASSERT(window);
+
+    JSC::ExecState* exec = window->globalExec();
+    Q_ASSERT(exec);
+
+    JSContextRef context = ::toRef(exec);
+    JSRetainPtr<JSStringRef> propertyName(Adopt, JSStringCreateWithUTF8CString("__qt_sender__"));
+    JSObjectRef function = JSObjectMakeFunctionWithCallback(context, propertyName.get(), qtSenderCallback);
+
+    // JSC public API doesn't support setting a Getter for a property of a given object, https://bugs.webkit.org/show_bug.cgi?id=61374.
+    window->defineGetter(exec, propertyName.get()->identifier(&exec->globalData()), ::toJS(function),
+                         JSC::ReadOnly | JSC::DontEnum | JSC::DontDelete);
+}
+#endif
+
 /*!
     \class QWebFrame
     \since 4.4
index 771e68a..dbdbc34 100644 (file)
@@ -108,6 +108,8 @@ public:
     void emitUrlChanged();
     void _q_orientationChanged();
 
+    void didClearWindowObject();
+
     QWebFrame *q;
     Qt::ScrollBarPolicy horizontalScrollBarPolicy;
     Qt::ScrollBarPolicy verticalScrollBarPolicy;
@@ -127,6 +129,11 @@ public:
 #if ENABLE(ORIENTATION_EVENTS) && ENABLE(DEVICE_ORIENTATION)
     QtMobility::QOrientationSensor m_orientation;
 #endif
+
+private:
+#if USE(JSC)
+    void addQtSenderToGlobalObject();
+#endif
 };
 
 class QWebHitTestResultPrivate {
index 3cf10f1..8370d99 100644 (file)
@@ -1,3 +1,37 @@
+2011-05-25  Caio Marcelo de Oliveira Filho  <caio.oliveira@openbossa.org>
+
+        Reviewed by Andreas Kling.
+
+        [Qt] JSC bridge: implement __qt_sender__ without using Scope Chain
+        https://bugs.webkit.org/show_bug.cgi?id=61343
+
+        Create a '__qt_sender__' property in the global object, that returns the top of
+        the qtSenderStack. This is an alternative implementation for the feature of
+        providing a way for a function (acting as a Qt 'slot') discover which object
+        emitted the signal that caused it to be executed.
+
+        This reduces the coupling of the Qt bridge and JSC internal implementation. The
+        patch tries to use as much JSC public API as possible.
+
+        This behavior is covered by the tst_QWebFrame::connectAndDisconnect() auto test.
+
+        * WebCoreSupport/FrameLoaderClientQt.cpp:
+        (WebCore::FrameLoaderClientQt::dispatchDidClearWindowObjectInWorld):
+        Instead of emitting the QWebPage::javaScriptWindowObjectCleared() directly, calls
+        a QWebPagePrivate function to do it.
+
+        * Api/qwebframe_p.h:
+        * Api/qwebframe.cpp:
+        (QWebFramePrivate::didClearedWindowObject):
+        Before emitting the signal mentioned, adds the '__qt_sender__' to the fresh
+        global object.
+
+        (qtSenderCallback):
+        Returns the JSObjectRef corresponding to the top of qtSenderStack.
+
+        (QWebFramePrivate::addQtSenderToGlobalObject):
+        Create a property with a qtSenderCallback as getter function in the global object.
+
 2011-05-25  Alexis Menard  <alexis.menard@openbossa.org>
 
         Reviewed by Eric Carlson.
index 71d21ad..8dd8085 100644 (file)
@@ -763,7 +763,7 @@ void FrameLoaderClientQt::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld* w
         return;
 
     if (m_webFrame)
-        emit m_webFrame->javaScriptWindowObjectCleared();
+        m_webFrame->d->didClearWindowObject();
 }
 
 void FrameLoaderClientQt::documentElementAvailable()