diff options
author | Lars Knoll <lars.knoll@digia.com> | 2013-10-21 09:57:58 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-10-29 10:38:59 +0100 |
commit | af22149dd8daf593182fec978f15dc1667c9cf8d (patch) | |
tree | 17334ae83a3015fd6ca535fb9d2e97b40e1da825 /src/qml/qml/qqmllocale.cpp | |
parent | 2b996ca17fbc36029af3900933b6fcc1418afb6a (diff) |
Avoid side effects when en exception has been thrown.
We don't want to check for exceptions after every single
line on our runtime methods. A better way to handle this
is to add the check in all methods that have direct side
effects (as e.g. writing to a property of the JS stack).
We also need to return whereever we throw an exception.
To simplify the code, ExecutionContext::throwXxx methods now
return a ReturnedValue (always undefined) for convenience.
Change-Id: Ide6c804f819c731a3f14c6c43121d08029c9fb90
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src/qml/qml/qqmllocale.cpp')
-rw-r--r-- | src/qml/qml/qqmllocale.cpp | 76 |
1 files changed, 51 insertions, 25 deletions
diff --git a/src/qml/qml/qqmllocale.cpp b/src/qml/qml/qqmllocale.cpp index aca18d8ef4..81a6ffc590 100644 --- a/src/qml/qml/qqmllocale.cpp +++ b/src/qml/qml/qqmllocale.cpp @@ -67,11 +67,13 @@ public: QLocale locale; - static QLocale &getThisLocale(QV4::SimpleCallContext *ctx) { + static QLocale *getThisLocale(QV4::SimpleCallContext *ctx) { QQmlLocaleData *thisObject = ctx->callData->thisObject.asObject()->as<QQmlLocaleData>(); - if (!thisObject) + if (!thisObject) { ctx->throwTypeError(); - return thisObject->locale; + return 0; + } + return &thisObject->locale; } static QV4::ReturnedValue method_currencySymbol(QV4::SimpleCallContext *ctx); @@ -516,8 +518,10 @@ QV4::ReturnedValue QQmlNumberExtension::method_fromLocaleString(QV4::SimpleCallC QV4::ReturnedValue QQmlLocaleData::method_get_firstDayOfWeek(QV4::SimpleCallContext *ctx) { - QLocale locale = getThisLocale(ctx); - int fdow = int(locale.firstDayOfWeek()); + QLocale *locale = getThisLocale(ctx); + if (!locale) + return QV4::Encode::undefined(); + int fdow = int(locale->firstDayOfWeek()); if (fdow == 7) fdow = 0; // Qt::Sunday = 7, but Sunday is 0 in JS Date return QV4::Encode(fdow); @@ -525,21 +529,29 @@ QV4::ReturnedValue QQmlLocaleData::method_get_firstDayOfWeek(QV4::SimpleCallCont QV4::ReturnedValue QQmlLocaleData::method_get_measurementSystem(QV4::SimpleCallContext *ctx) { - QLocale locale = getThisLocale(ctx); - return QV4::Encode(locale.measurementSystem()); + QLocale *locale = getThisLocale(ctx); + if (!locale) + return QV4::Encode::undefined(); + return QV4::Encode(locale->measurementSystem()); } QV4::ReturnedValue QQmlLocaleData::method_get_textDirection(QV4::SimpleCallContext *ctx) { - QLocale locale = getThisLocale(ctx); - return QV4::Encode(locale.textDirection()); + QLocale *locale = getThisLocale(ctx); + if (!locale) + return QV4::Encode::undefined(); + + return QV4::Encode(locale->textDirection()); } QV4::ReturnedValue QQmlLocaleData::method_get_weekDays(QV4::SimpleCallContext *ctx) { QV4::Scope scope(ctx); - QLocale locale = getThisLocale(ctx); - QList<Qt::DayOfWeek> days = locale.weekdays(); + QLocale *locale = getThisLocale(ctx); + if (!locale) + return QV4::Encode::undefined(); + + QList<Qt::DayOfWeek> days = locale->weekdays(); QV4::Scoped<QV4::ArrayObject> result(scope, ctx->engine->newArrayObject()); result->arrayReserve(days.size()); @@ -558,8 +570,11 @@ QV4::ReturnedValue QQmlLocaleData::method_get_weekDays(QV4::SimpleCallContext *c QV4::ReturnedValue QQmlLocaleData::method_get_uiLanguages(QV4::SimpleCallContext *ctx) { QV4::Scope scope(ctx); - QLocale locale = getThisLocale(ctx); - QStringList langs = locale.uiLanguages(); + QLocale *locale = getThisLocale(ctx); + if (!locale) + return QV4::Encode::undefined(); + + QStringList langs = locale->uiLanguages(); QV4::Scoped<QV4::ArrayObject> result(scope, ctx->engine->newArrayObject()); result->arrayReserve(langs.size()); for (int i = 0; i < langs.size(); ++i) { @@ -574,7 +589,10 @@ QV4::ReturnedValue QQmlLocaleData::method_get_uiLanguages(QV4::SimpleCallContext QV4::ReturnedValue QQmlLocaleData::method_currencySymbol(QV4::SimpleCallContext *ctx) { - QLocale locale = getThisLocale(ctx); + QLocale *locale = getThisLocale(ctx); + if (!locale) + return QV4::Encode::undefined(); + if (ctx->callData->argc > 1) V4THROW_ERROR("Locale: currencySymbol(): Invalid arguments"); @@ -584,12 +602,14 @@ QV4::ReturnedValue QQmlLocaleData::method_currencySymbol(QV4::SimpleCallContext format = QLocale::CurrencySymbolFormat(intFormat); } - return ctx->engine->newString(locale.currencySymbol(format))->asReturnedValue(); + return ctx->engine->newString(locale->currencySymbol(format))->asReturnedValue(); } #define LOCALE_FORMAT(FUNC) \ QV4::ReturnedValue QQmlLocaleData::method_ ##FUNC (QV4::SimpleCallContext *ctx) { \ - QLocale locale = getThisLocale(ctx); \ + QLocale *locale = getThisLocale(ctx); \ + if (!locale) \ + return QV4::Encode::undefined(); \ if (ctx->callData->argc > 1) \ V4THROW_ERROR("Locale: " #FUNC "(): Invalid arguments"); \ QLocale::FormatType format = QLocale::LongFormat;\ @@ -597,7 +617,7 @@ QV4::ReturnedValue QQmlLocaleData::method_ ##FUNC (QV4::SimpleCallContext *ctx) quint32 intFormat = ctx->callData->args[0].toUInt32(); \ format = QLocale::FormatType(intFormat); \ } \ - return ctx->engine->newString(locale. FUNC (format))->asReturnedValue(); \ + return ctx->engine->newString(locale-> FUNC (format))->asReturnedValue(); \ } LOCALE_FORMAT(dateTimeFormat) @@ -607,7 +627,9 @@ LOCALE_FORMAT(dateFormat) // +1 added to idx because JS is 0-based, whereas QLocale months begin at 1. #define LOCALE_FORMATTED_MONTHNAME(VARIABLE) \ QV4::ReturnedValue QQmlLocaleData::method_ ## VARIABLE (QV4::SimpleCallContext *ctx) {\ - QLocale locale = getThisLocale(ctx); \ + QLocale *locale = getThisLocale(ctx); \ + if (!locale) \ + return QV4::Encode::undefined(); \ if (ctx->callData->argc < 1 || ctx->callData->argc > 2) \ V4THROW_ERROR("Locale: " #VARIABLE "(): Invalid arguments"); \ QLocale::FormatType enumFormat = QLocale::LongFormat; \ @@ -619,12 +641,12 @@ QV4::ReturnedValue QQmlLocaleData::method_ ## VARIABLE (QV4::SimpleCallContext * if (ctx->callData->args[1].isNumber()) { \ quint32 intFormat = ctx->callData->args[1].toUInt32(); \ QLocale::FormatType format = QLocale::FormatType(intFormat); \ - name = locale. VARIABLE(idx, format); \ + name = locale-> VARIABLE(idx, format); \ } else { \ V4THROW_ERROR("Locale: Invalid datetime format"); \ } \ } else { \ - name = locale. VARIABLE(idx, enumFormat); \ + name = locale-> VARIABLE(idx, enumFormat); \ } \ return ctx->engine->newString(name)->asReturnedValue(); \ } @@ -632,7 +654,9 @@ QV4::ReturnedValue QQmlLocaleData::method_ ## VARIABLE (QV4::SimpleCallContext * // 0 -> 7 as Qt::Sunday is 7, but Sunday is 0 in JS Date #define LOCALE_FORMATTED_DAYNAME(VARIABLE) \ QV4::ReturnedValue QQmlLocaleData::method_ ## VARIABLE (QV4::SimpleCallContext *ctx) {\ - QLocale locale = getThisLocale(ctx); \ + QLocale *locale = getThisLocale(ctx); \ + if (!locale) \ + return QV4::Encode::undefined(); \ if (ctx->callData->argc < 1 || ctx->callData->argc > 2) \ V4THROW_ERROR("Locale: " #VARIABLE "(): Invalid arguments"); \ QLocale::FormatType enumFormat = QLocale::LongFormat; \ @@ -645,12 +669,12 @@ QV4::ReturnedValue QQmlLocaleData::method_ ## VARIABLE (QV4::SimpleCallContext * if (ctx->callData->args[1].isNumber()) { \ quint32 intFormat = ctx->callData->args[1].toUInt32(); \ QLocale::FormatType format = QLocale::FormatType(intFormat); \ - name = locale. VARIABLE(idx, format); \ + name = locale-> VARIABLE(idx, format); \ } else { \ V4THROW_ERROR("Locale: Invalid datetime format"); \ } \ } else { \ - name = locale. VARIABLE(idx, enumFormat); \ + name = locale-> VARIABLE(idx, enumFormat); \ } \ return ctx->engine->newString(name)->asReturnedValue(); \ } @@ -662,8 +686,10 @@ LOCALE_FORMATTED_DAYNAME(standaloneDayName) #define LOCALE_STRING_PROPERTY(VARIABLE) QV4::ReturnedValue QQmlLocaleData::method_get_ ## VARIABLE (QV4::SimpleCallContext* ctx) \ { \ - QLocale locale = getThisLocale(ctx); \ - return ctx->engine->newString(locale. VARIABLE())->asReturnedValue();\ + QLocale *locale = getThisLocale(ctx); \ + if (!locale) \ + return QV4::Encode::undefined(); \ + return ctx->engine->newString(locale-> VARIABLE())->asReturnedValue();\ } LOCALE_STRING_PROPERTY(name) |