diff options
author | Lars Knoll <[email protected]> | 2013-10-21 09:57:58 +0200 |
---|---|---|
committer | The Qt Project <[email protected]> | 2013-10-29 10:38:59 +0100 |
commit | af22149dd8daf593182fec978f15dc1667c9cf8d (patch) | |
tree | 17334ae83a3015fd6ca535fb9d2e97b40e1da825 /src/qml/jsruntime/qv4numberobject.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 <[email protected]>
Diffstat (limited to 'src/qml/jsruntime/qv4numberobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4numberobject.cpp | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/qml/jsruntime/qv4numberobject.cpp b/src/qml/jsruntime/qv4numberobject.cpp index 8a09de5349..dd13f493f7 100644 --- a/src/qml/jsruntime/qv4numberobject.cpp +++ b/src/qml/jsruntime/qv4numberobject.cpp @@ -107,7 +107,7 @@ inline ReturnedValue thisNumberValue(ExecutionContext *ctx) return ctx->callData->thisObject.asReturnedValue(); NumberObject *n = ctx->callData->thisObject.asNumberObject(); if (!n) - ctx->throwTypeError(); + return ctx->throwTypeError(); return n->value.asReturnedValue(); } @@ -117,21 +117,21 @@ inline double thisNumber(ExecutionContext *ctx) return ctx->callData->thisObject.asDouble(); NumberObject *n = ctx->callData->thisObject.asNumberObject(); if (!n) - ctx->throwTypeError(); + return ctx->throwTypeError(); return n->value.asDouble(); } ReturnedValue NumberPrototype::method_toString(SimpleCallContext *ctx) { double num = thisNumber(ctx); + if (ctx->engine->hasException) + return Encode::undefined(); if (ctx->callData->argc && !ctx->callData->args[0].isUndefined()) { int radix = ctx->callData->args[0].toInt32(); - if (radix < 2 || radix > 36) { - ctx->throwError(QString::fromLatin1("Number.prototype.toString: %0 is not a valid radix") + if (radix < 2 || radix > 36) + return ctx->throwError(QString::fromLatin1("Number.prototype.toString: %0 is not a valid radix") .arg(radix)); - return Encode::undefined(); - } if (std::isnan(num)) { return ctx->engine->newString(QStringLiteral("NaN"))->asReturnedValue(); @@ -177,6 +177,8 @@ ReturnedValue NumberPrototype::method_toLocaleString(SimpleCallContext *ctx) { Scope scope(ctx); ScopedValue v(scope, thisNumberValue(ctx)); + if (ctx->engine->hasException) + return Encode::undefined(); ScopedString str(scope, v->toString(ctx)); return str.asReturnedValue(); @@ -190,6 +192,8 @@ ReturnedValue NumberPrototype::method_valueOf(SimpleCallContext *ctx) ReturnedValue NumberPrototype::method_toFixed(SimpleCallContext *ctx) { double v = thisNumber(ctx); + if (ctx->engine->hasException) + return Encode::undefined(); double fdigits = 0; @@ -200,7 +204,7 @@ ReturnedValue NumberPrototype::method_toFixed(SimpleCallContext *ctx) fdigits = 0; if (fdigits < 0 || fdigits > 20) - ctx->throwRangeError(ctx->callData->thisObject); + return ctx->throwRangeError(ctx->callData->thisObject); QString str; if (std::isnan(v)) @@ -218,6 +222,8 @@ ReturnedValue NumberPrototype::method_toExponential(SimpleCallContext *ctx) { Scope scope(ctx); double d = thisNumber(ctx); + if (ctx->engine->hasException) + return Encode::undefined(); int fdigits = -1; @@ -225,7 +231,7 @@ ReturnedValue NumberPrototype::method_toExponential(SimpleCallContext *ctx) int fdigits = ctx->callData->args[0].toInt32(); if (fdigits < 0 || fdigits > 20) { ScopedString error(scope, ctx->engine->newString(QStringLiteral("Number.prototype.toExponential: fractionDigits out of range"))); - ctx->throwRangeError(error); + return ctx->throwRangeError(error); } } @@ -241,6 +247,8 @@ ReturnedValue NumberPrototype::method_toPrecision(SimpleCallContext *ctx) { Scope scope(ctx); ScopedValue v(scope, thisNumberValue(ctx)); + if (ctx->engine->hasException) + return Encode::undefined(); if (!ctx->callData->argc || ctx->callData->args[0].isUndefined()) return __qmljs_to_string(v, ctx); @@ -248,7 +256,7 @@ ReturnedValue NumberPrototype::method_toPrecision(SimpleCallContext *ctx) double precision = ctx->callData->args[0].toInt32(); if (precision < 1 || precision > 21) { ScopedString error(scope, ctx->engine->newString(QStringLiteral("Number.prototype.toPrecision: precision out of range"))); - ctx->throwRangeError(error); + return ctx->throwRangeError(error); } char str[100]; |