From f3a7266cb4793f933ea8c7d2c73c736e022b754a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Mon, 3 Aug 2009 20:03:35 +0200 Subject: Implement QScriptEngine::setProcessEventsInterval() We're using a wrapper around TimeoutChecker, where we re-implement didTimeout() to call processEvents(). --- src/script/api/qscriptengine.cpp | 40 ++++++++++++++++++++++---- tests/auto/qscriptengine/tst_qscriptengine.cpp | 3 +- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index f80c605..f60d0af 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -68,6 +68,7 @@ #include "ObjectPrototype.h" #include "SourceCode.h" #include "FunctionPrototype.h" +#include "TimeoutChecker.h" #include "JSFunction.h" #include "Parser.h" #include "Operations.h" @@ -410,6 +411,31 @@ private: JSC::JSGlobalObject *originalGlobalObject; }; +class TimeoutCheckerProxy : public JSC::TimeoutChecker +{ +public: + TimeoutCheckerProxy(const JSC::TimeoutChecker& originalChecker) + : JSC::TimeoutChecker(originalChecker) + , m_shouldProcessEvents(false) + {} + + void setShouldProcessEvents(bool shouldProcess) { m_shouldProcessEvents = shouldProcess; } + + virtual bool didTimeOut(JSC::ExecState* exec) + { + if (JSC::TimeoutChecker::didTimeOut(exec)) + return true; + + if (m_shouldProcessEvents) + QCoreApplication::processEvents(); + + return false; + } + +private: + bool m_shouldProcessEvents; +}; + static int toDigit(char c) { if ((c >= '0') && (c <= '9')) @@ -931,6 +957,10 @@ QScriptEnginePrivate::QScriptEnginePrivate() : idGenerator(1) globalObject->functionPrototype()->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, globalObject->prototypeFunctionStructure(), 1, JSC::Identifier(exec, "disconnect"), QScript::functionDisconnect)); globalObject->functionPrototype()->putDirectFunction(exec, new (exec)JSC::NativeFunctionWrapper(exec, globalObject->prototypeFunctionStructure(), 1, JSC::Identifier(exec, "connect"), QScript::functionConnect)); + JSC::TimeoutChecker* originalChecker = globalData->timeoutChecker; + globalData->timeoutChecker = new QScript::TimeoutCheckerProxy(*originalChecker); + delete originalChecker; + currentFrame = exec; originalGlobalObjectProxy = 0; @@ -3458,12 +3488,12 @@ void QScriptEngine::collectGarbage() void QScriptEngine::setProcessEventsInterval(int interval) { Q_D(QScriptEngine); - qWarning("QScriptEngine::setProcessEventsInterval() not implemented"); - // is it possible with JSC? - // JSC has some code for detecting timeouts but not for getting - // a callback at fixed intervals. - // ### we can install a JSC "debugger client" d->processEventsInterval = interval; + + if (interval > 0) + d->globalData->timeoutChecker->setCheckInterval(interval); + + dynamic_cast(d->globalData->timeoutChecker)->setShouldProcessEvents(interval > 0); } /*! diff --git a/tests/auto/qscriptengine/tst_qscriptengine.cpp b/tests/auto/qscriptengine/tst_qscriptengine.cpp index 7b019c1..04d7d21 100644 --- a/tests/auto/qscriptengine/tst_qscriptengine.cpp +++ b/tests/auto/qscriptengine/tst_qscriptengine.cpp @@ -2095,7 +2095,7 @@ void tst_QScriptEngine::processEventsWhileRunning() eng.pushContext(); QString script = QString::fromLatin1( - "var end = Number(new Date()) + 1000;" + "var end = Number(new Date()) + 2000;" "var x = 0;" "while (Number(new Date()) < end) {" " ++x;" @@ -2112,7 +2112,6 @@ void tst_QScriptEngine::processEventsWhileRunning() eng.setProcessEventsInterval(100); eng.evaluate(script); QVERIFY(!eng.hasUncaughtException()); - QEXPECT_FAIL("", "", Continue); QVERIFY(receiver.received); if (x == 0) -- cgit v0.12