summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/script/api/qscriptengine.cpp12
-rw-r--r--src/script/api/qscriptengine_p.h5
-rw-r--r--src/script/api/qscriptengineagent.cpp2
-rw-r--r--src/script/api/qscriptvalue.h3
-rw-r--r--tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp38
5 files changed, 57 insertions, 3 deletions
diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp
index b1f36be..360036a 100644
--- a/src/script/api/qscriptengine.cpp
+++ b/src/script/api/qscriptengine.cpp
@@ -2164,7 +2164,7 @@ QScriptValue QScriptEngine::evaluate(const QString &program, const QString &file
if (debugger)
debugger->evaluateStart(sourceId);
- exec->clearException();
+ clearExceptions();
JSC::DynamicGlobalObjectScope dynamicGlobalObjectScope(exec, exec->scopeChain()->globalObject());
JSC::EvalExecutable executable(exec, source);
@@ -2381,7 +2381,7 @@ bool QScriptEngine::hasUncaughtException() const
{
Q_D(const QScriptEngine);
JSC::ExecState* exec = d->globalExec();
- return exec->hadException();
+ return exec->hadException() || d->currentException().isValid();
}
/*!
@@ -2398,8 +2398,13 @@ bool QScriptEngine::hasUncaughtException() const
QScriptValue QScriptEngine::uncaughtException() const
{
Q_D(const QScriptEngine);
+ QScriptValue result;
JSC::ExecState* exec = d->globalExec();
- return const_cast<QScriptEnginePrivate*>(d)->scriptValueFromJSCValue(exec->exception());
+ if (exec->hadException())
+ result = const_cast<QScriptEnginePrivate*>(d)->scriptValueFromJSCValue(exec->exception());
+ else
+ result = d->currentException();
+ return result;
}
/*!
@@ -2452,6 +2457,7 @@ void QScriptEngine::clearExceptions()
Q_D(QScriptEngine);
JSC::ExecState* exec = d->currentFrame;
exec->clearException();
+ d->clearCurrentException();
}
/*!
diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h
index f1fc135..cde116d 100644
--- a/src/script/api/qscriptengine_p.h
+++ b/src/script/api/qscriptengine_p.h
@@ -183,6 +183,10 @@ public:
void agentDeleted(QScriptEngineAgent *agent);
+ void setCurrentException(QScriptValue exception) { m_currentException = exception; }
+ QScriptValue currentException() const { return m_currentException; }
+ void clearCurrentException() { m_currentException.d_ptr.reset(); }
+
#ifndef QT_NO_QOBJECT
JSC::JSValue newQObject(QObject *object,
QScriptEngine::ValueOwnership ownership = QScriptEngine::QtOwnership,
@@ -263,6 +267,7 @@ public:
QSet<QString> extensionsBeingImported;
QHash<intptr_t, QScript::UStringSourceProviderWithFeedback*> loadedScripts;
+ QScriptValue m_currentException;
#ifndef QT_NO_QOBJECT
QHash<QObject*, QScript::QObjectData*> m_qobjectData;
diff --git a/src/script/api/qscriptengineagent.cpp b/src/script/api/qscriptengineagent.cpp
index bc2eea2..0ca7ecc 100644
--- a/src/script/api/qscriptengineagent.cpp
+++ b/src/script/api/qscriptengineagent.cpp
@@ -156,6 +156,7 @@ void QScriptEngineAgentPrivate::exceptionThrow(const JSC::DebuggerCallFrame& fra
QScriptValue value(engine->scriptValueFromJSCValue(frame.exception()));
q_ptr->exceptionThrow(sourceID, value, hasHandler);
engine->currentFrame = oldFrame;
+ engine->setCurrentException(value);
};
void QScriptEngineAgentPrivate::exceptionCatch(const JSC::DebuggerCallFrame& frame, intptr_t sourceID)
@@ -165,6 +166,7 @@ void QScriptEngineAgentPrivate::exceptionCatch(const JSC::DebuggerCallFrame& fra
QScriptValue value(engine->scriptValueFromJSCValue(frame.exception()));
q_ptr->exceptionCatch(sourceID, value);
engine->currentFrame = oldFrame;
+ engine->clearCurrentException();
}
void QScriptEngineAgentPrivate::atStatement(const JSC::DebuggerCallFrame& frame, intptr_t sourceID, int lineno, int column)
diff --git a/src/script/api/qscriptvalue.h b/src/script/api/qscriptvalue.h
index 32f7a43..aba3327 100644
--- a/src/script/api/qscriptvalue.h
+++ b/src/script/api/qscriptvalue.h
@@ -70,6 +70,7 @@ typedef QList<QScriptValue> QScriptValueList;
typedef double qsreal;
class QScriptValuePrivate;
+class QScriptEnginePrivate;
struct QScriptValuePrivatePointerDeleter;
class Q_SCRIPT_EXPORT QScriptValue
{
@@ -226,6 +227,8 @@ private:
QExplicitlySharedDataPointer<QScriptValuePrivate> d_ptr;
Q_DECLARE_PRIVATE(QScriptValue)
+
+ friend class QScriptEnginePrivate;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QScriptValue::ResolveFlags)
diff --git a/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp b/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp
index 283e489..82c8ccd 100644
--- a/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp
+++ b/tests/auto/qscriptengineagent/tst_qscriptengineagent.cpp
@@ -109,6 +109,7 @@ private slots:
void extension_invoctaion();
void extension();
void isEvaluatingInExtension();
+ void hasUncaughtException();
private:
double m_testProperty;
@@ -2182,5 +2183,42 @@ void tst_QScriptEngineAgent::isEvaluatingInExtension()
QVERIFY(spy->wasEvaluating);
}
+class NewSpy :public QScriptEngineAgent
+{
+ bool m_result;
+public:
+ NewSpy(QScriptEngine* eng) : QScriptEngineAgent(eng), m_result(false) {}
+ void functionExit (qint64, const QScriptValue &scriptValue)
+ {
+ if (engine()->hasUncaughtException()) m_result = true;
+ }
+
+ bool isPass() { return m_result; }
+ void reset() { m_result = false; }
+};
+
+void tst_QScriptEngineAgent::hasUncaughtException()
+{
+ QScriptEngine eng;
+ NewSpy* spy = new NewSpy(&eng);
+ eng.setAgent(spy);
+ QScriptValue scriptValue;
+
+ // Check unhandled exception.
+ eng.evaluate("function init () {Unknown.doSth ();}");
+ scriptValue = QScriptValue(eng.globalObject().property("init")).call();
+ QVERIFY(eng.hasUncaughtException());
+ QVERIFY2(spy->isPass(), "At least one of a functionExit event should set hasUncaughtException flag.");
+ spy->reset();
+
+ // Check catched exception.
+ eng.evaluate("function innerFoo() { throw new Error('ciao') }");
+ eng.evaluate("function foo() {try { innerFoo() } catch (e) {} }");
+ scriptValue = QScriptValue(eng.globalObject().property("foo")).call();
+ QVERIFY(!eng.hasUncaughtException());
+ QVERIFY2(spy->isPass(), "At least one of a functionExit event should set hasUncaughtException flag.");
+}
+
+
QTEST_MAIN(tst_QScriptEngineAgent)
#include "tst_qscriptengineagent.moc"