From cab1cac7bb5d7f3f3cac6dd987e1c3fb137576fe Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 30 Jan 2013 16:39:36 +0100 Subject: [PATCH] Convert Math functions to contextless form Change-Id: I49123f4396d64fb5e2a888d339c87c800981f4f3 Reviewed-by: Simon Hausmann --- src/v4/qv4mathobject.cpp | 85 ++++++++++++++++++++++++++---------------------- src/v4/qv4mathobject.h | 34 +++++++++---------- 2 files changed, 64 insertions(+), 55 deletions(-) diff --git a/src/v4/qv4mathobject.cpp b/src/v4/qv4mathobject.cpp index eae50c4..2f92e52 100644 --- a/src/v4/qv4mathobject.cpp +++ b/src/v4/qv4mathobject.cpp @@ -96,49 +96,58 @@ static double copySign(double x, double y) return x; } -Value MathObject::method_abs(ExecutionContext *ctx) +Value MathObject::method_abs(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + if (!argc) + return Value::fromDouble(qSNaN()); + + if (argv[0].isInteger()) { + int i = argv[0].integerValue(); + return Value::fromInt32(i < 0 ? - i : i); + } + + double v = argv[0].toNumber(parentCtx); if (v == 0) // 0 | -0 return Value::fromDouble(0); return Value::fromDouble(v < 0 ? -v : v); } -Value MathObject::method_acos(ExecutionContext *ctx) +Value MathObject::method_acos(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : 2; if (v > 1) return Value::fromDouble(qSNaN()); return Value::fromDouble(::acos(v)); } -Value MathObject::method_asin(ExecutionContext *ctx) +Value MathObject::method_asin(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : 2; if (v > 1) return Value::fromDouble(qSNaN()); else return Value::fromDouble(::asin(v)); } -Value MathObject::method_atan(ExecutionContext *ctx) +Value MathObject::method_atan(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); if (v == 0.0) return Value::fromDouble(v); else return Value::fromDouble(::atan(v)); } -Value MathObject::method_atan2(ExecutionContext *ctx) +Value MathObject::method_atan2(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v1 = ctx->argument(0).toNumber(ctx); - double v2 = ctx->argument(1).toNumber(ctx); - if ((v1 < 0) && qIsFinite(v1) && qIsInf(v2) && (copySign(1.0, v2) == 1.0)) { + double v1 = argc ? argv[0].toNumber(parentCtx) : qSNaN(); + double v2 = argc > 1 ? argv[1].toNumber(parentCtx) : qSNaN(); + + if ((v1 < 0) && qIsFinite(v1) && qIsInf(v2) && (copySign(1.0, v2) == 1.0)) return Value::fromDouble(copySign(0, -1.0)); - } + if ((v1 == 0.0) && (v2 == 0.0)) { if ((copySign(1.0, v1) == 1.0) && (copySign(1.0, v2) == -1.0)) { return Value::fromDouble(qt_PI); @@ -149,24 +158,24 @@ Value MathObject::method_atan2(ExecutionContext *ctx) return Value::fromDouble(::atan2(v1, v2)); } -Value MathObject::method_ceil(ExecutionContext *ctx) +Value MathObject::method_ceil(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); if (v < 0.0 && v > -1.0) return Value::fromDouble(copySign(0, -1.0)); else return Value::fromDouble(::ceil(v)); } -Value MathObject::method_cos(ExecutionContext *ctx) +Value MathObject::method_cos(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); return Value::fromDouble(::cos(v)); } -Value MathObject::method_exp(ExecutionContext *ctx) +Value MathObject::method_exp(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); if (qIsInf(v)) { if (copySign(1.0, v) == -1.0) return Value::fromDouble(0); @@ -177,37 +186,37 @@ Value MathObject::method_exp(ExecutionContext *ctx) } } -Value MathObject::method_floor(ExecutionContext *ctx) +Value MathObject::method_floor(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); return Value::fromDouble(::floor(v)); } -Value MathObject::method_log(ExecutionContext *ctx) +Value MathObject::method_log(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); if (v < 0) return Value::fromDouble(qSNaN()); else return Value::fromDouble(::log(v)); } -Value MathObject::method_max(ExecutionContext *ctx) +Value MathObject::method_max(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { double mx = -qInf(); - for (unsigned i = 0; i < ctx->argumentCount; ++i) { - double x = ctx->argument(i).toNumber(ctx); + for (unsigned i = 0; i < argc; ++i) { + double x = argv[i].toNumber(parentCtx); if (x > mx || std::isnan(x)) mx = x; } return Value::fromDouble(mx); } -Value MathObject::method_min(ExecutionContext *ctx) +Value MathObject::method_min(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { double mx = qInf(); - for (unsigned i = 0; i < ctx->argumentCount; ++i) { - double x = ctx->argument(i).toNumber(ctx); + for (unsigned i = 0; i < argc; ++i) { + double x = argv[i].toNumber(parentCtx); if ((x == 0 && mx == x && copySign(1.0, x) == -1.0) || (x < mx) || std::isnan(x)) { mx = x; @@ -266,33 +275,33 @@ Value MathObject::method_pow(ExecutionContext *parentCtx, Value, Value *argv, in return Value::fromDouble(qSNaN()); } -Value MathObject::method_random(ExecutionContext */*ctx*/) +Value MathObject::method_random(ExecutionContext *, Value, Value *, int) { return Value::fromDouble(qrand() / (double) RAND_MAX); } -Value MathObject::method_round(ExecutionContext *ctx) +Value MathObject::method_round(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); v = copySign(::floor(v + 0.5), v); return Value::fromDouble(v); } -Value MathObject::method_sin(ExecutionContext *ctx) +Value MathObject::method_sin(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); return Value::fromDouble(::sin(v)); } -Value MathObject::method_sqrt(ExecutionContext *ctx) +Value MathObject::method_sqrt(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); return Value::fromDouble(::sqrt(v)); } -Value MathObject::method_tan(ExecutionContext *ctx) +Value MathObject::method_tan(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc) { - double v = ctx->argument(0).toNumber(ctx); + double v = argc ? argv[0].toNumber(parentCtx) : qSNaN(); if (v == 0.0) return Value::fromDouble(v); else diff --git a/src/v4/qv4mathobject.h b/src/v4/qv4mathobject.h index c8428d2..1803137 100644 --- a/src/v4/qv4mathobject.h +++ b/src/v4/qv4mathobject.h @@ -52,24 +52,24 @@ struct MathObject: Object MathObject(ExecutionContext *ctx); virtual QString className() { return QStringLiteral("Math"); } - static Value method_abs(ExecutionContext *ctx); - static Value method_acos(ExecutionContext *ctx); - static Value method_asin(ExecutionContext *ctx); - static Value method_atan(ExecutionContext *ctx); - static Value method_atan2(ExecutionContext *ctx); - static Value method_ceil(ExecutionContext *ctx); - static Value method_cos(ExecutionContext *ctx); - static Value method_exp(ExecutionContext *ctx); - static Value method_floor(ExecutionContext *ctx); - static Value method_log(ExecutionContext *ctx); - static Value method_max(ExecutionContext *ctx); - static Value method_min(ExecutionContext *ctx); + static Value method_abs(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_acos(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_asin(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_atan(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_atan2(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_ceil(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_cos(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_exp(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_floor(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_log(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_max(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_min(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); static Value method_pow(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); - static Value method_random(ExecutionContext *ctx); - static Value method_round(ExecutionContext *ctx); - static Value method_sin(ExecutionContext *ctx); - static Value method_sqrt(ExecutionContext *ctx); - static Value method_tan(ExecutionContext *ctx); + static Value method_random(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_round(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_sin(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_sqrt(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); + static Value method_tan(ExecutionContext *parentCtx, Value thisObject, Value *argv, int argc); }; } // namespace VM -- 2.7.4