From c860d1399b4ff5f3a5b1b2630bc33112c88c13e8 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 11 Sep 2013 13:23:21 +0200 Subject: Change exception handling API This patch changes the exception handling API in the engine slightly, encapsulating any use of direct throw statements and catch blocks with concrete types. In the future we need to be able to change the way these are implemented, in order to ensure that the correct stack unwinding code is triggered for throw and re-throw. This patch separates the C++ exception object thrown from the V4 exception (that includes value, throwing context pointer) and stores the latter inside the engine. In order for that to compile, ExecutionEngine::StackTrace and StackFrame had to move into the QV4 namespace directly. In addition the syntax for catching exceptions changes from try { ... } catch (QV4::Exception &ex) { ex.accept(context); QV4::ScopedValue exceptionValue(scope, ex.value()); } to try { ... } catch (...) { QV4::ScopedValue exception(scope, context->catchException()); } Context::catchException() checks if there's a "current" exception in the engine, and if not assumes that we caught an unrelated exception and consequently re-throws. partiallyUnwind() is also gone and replaced with rethrowException(), in order to encapsulate the re-throw. Lastly, in the future nesting try/catch blocks isn't going to be possible due to limitations in the common C++ ABI with regards to foreign exceptions. Change-Id: Ic81c75b057a2147e3176d8e0b4d326c14278b47d Reviewed-by: Lars Knoll --- src/qml/jsapi/qjsengine.cpp | 5 ++-- src/qml/jsapi/qjsvalue.cpp | 49 +++++++++++++++++--------------------- src/qml/jsapi/qjsvalueiterator.cpp | 4 ++-- 3 files changed, 26 insertions(+), 32 deletions(-) (limited to 'src/qml/jsapi') diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index 57647a49d3..d42cabd81c 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -269,9 +269,8 @@ QJSValue QJSEngine::evaluate(const QString& program, const QString& fileName, in script.inheritContext = true; script.parse(); result = script.run(); - } catch (QV4::Exception& ex) { - ex.accept(ctx); - result = ex.value(); + } catch (...) { + result = ctx->catchException(); } return new QJSValuePrivate(d->m_v4Engine, result); } diff --git a/src/qml/jsapi/qjsvalue.cpp b/src/qml/jsapi/qjsvalue.cpp index 8bcdccc507..ba94afadc6 100644 --- a/src/qml/jsapi/qjsvalue.cpp +++ b/src/qml/jsapi/qjsvalue.cpp @@ -380,8 +380,8 @@ double QJSValue::toNumber() const QV4::ExecutionContext *ctx = d->engine ? d->engine->current : 0; try { return d->value.toNumber(); - } catch (Exception &e) { - e.accept(ctx); + } catch (...) { + ctx->catchException(); return 0; } } @@ -403,8 +403,8 @@ bool QJSValue::toBool() const QV4::ExecutionContext *ctx = d->engine ? d->engine->current : 0; try { return d->value.toBoolean(); - } catch (Exception &e) { - e.accept(ctx); + } catch (...) { + ctx->catchException(); return false; } } @@ -426,8 +426,8 @@ qint32 QJSValue::toInt() const QV4::ExecutionContext *ctx = d->engine ? d->engine->current : 0; try { return d->value.toInt32(); - } catch (Exception &e) { - e.accept(ctx); + } catch (...) { + ctx->catchException(); return 0; } } @@ -449,8 +449,8 @@ quint32 QJSValue::toUInt() const QV4::ExecutionContext *ctx = d->engine ? d->engine->current : 0; try { return d->value.toUInt32(); - } catch (Exception &e) { - e.accept(ctx); + } catch (...) { + ctx->catchException(); return 0; } } @@ -521,9 +521,8 @@ QJSValue QJSValue::call(const QJSValueList &args) QV4::ExecutionContext *ctx = engine->current; try { result = f->call(callData); - } catch (Exception &e) { - e.accept(ctx); - result = e.value(); + } catch (...) { + result = ctx->catchException(); } return new QJSValuePrivate(engine, result); @@ -578,9 +577,8 @@ QJSValue QJSValue::callWithInstance(const QJSValue &instance, const QJSValueList QV4::ExecutionContext *ctx = engine->current; try { result = f->call(callData); - } catch (Exception &e) { - e.accept(ctx); - result = e.value(); + } catch (...) { + result = ctx->catchException(); } return new QJSValuePrivate(engine, result); @@ -627,9 +625,8 @@ QJSValue QJSValue::callAsConstructor(const QJSValueList &args) QV4::ExecutionContext *ctx = engine->current; try { result = f->construct(callData); - } catch (Exception &e) { - e.accept(ctx); - result = e.value(); + } catch (...) { + result = ctx->catchException(); } return new QJSValuePrivate(engine, result); @@ -819,9 +816,8 @@ QJSValue QJSValue::property(const QString& name) const QV4::ScopedValue result(scope); try { result = o->get(s); - } catch (QV4::Exception &e) { - e.accept(ctx); - result = e.value(); + } catch (...) { + result = ctx->catchException(); } return new QJSValuePrivate(engine, result); } @@ -853,9 +849,8 @@ QJSValue QJSValue::property(quint32 arrayIndex) const QV4::ScopedValue result(scope); try { result = arrayIndex == UINT_MAX ? o->get(engine->id_uintMax) : o->getIndexed(arrayIndex); - } catch (QV4::Exception &e) { - e.accept(ctx); - result = e.value(); + } catch (...) { + result = ctx->catchException(); } return new QJSValuePrivate(engine, result); } @@ -899,8 +894,8 @@ void QJSValue::setProperty(const QString& name, const QJSValue& value) try { QV4::ScopedValue v(scope, value.d->getValue(engine)); o->put(s, v); - } catch (QV4::Exception &e) { - e.accept(ctx); + } catch (...) { + ctx->catchException(); } } @@ -934,8 +929,8 @@ void QJSValue::setProperty(quint32 arrayIndex, const QJSValue& value) o->putIndexed(arrayIndex, v); else o->put(engine->id_uintMax, v); - } catch (QV4::Exception &e) { - e.accept(ctx); + } catch (...) { + ctx->catchException(); } } diff --git a/src/qml/jsapi/qjsvalueiterator.cpp b/src/qml/jsapi/qjsvalueiterator.cpp index e1786f06cd..fd5f709e14 100644 --- a/src/qml/jsapi/qjsvalueiterator.cpp +++ b/src/qml/jsapi/qjsvalueiterator.cpp @@ -211,8 +211,8 @@ QJSValue QJSValueIterator::value() const return QJSValue(); } return new QJSValuePrivate(engine, v); - } catch (QV4::Exception &e) { - e.accept(ctx); + } catch (...) { + ctx->catchException(); return QJSValue(); } } -- cgit v1.2.3