diff options
Diffstat (limited to 'src/qml/jsruntime/qv4mathobject.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4mathobject.cpp | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/qml/jsruntime/qv4mathobject.cpp b/src/qml/jsruntime/qv4mathobject.cpp index 473e05bf88..3d3ac84576 100644 --- a/src/qml/jsruntime/qv4mathobject.cpp +++ b/src/qml/jsruntime/qv4mathobject.cpp @@ -47,10 +47,9 @@ DEFINE_OBJECT_VTABLE(MathObject); static const double qt_PI = 2.0 * ::asin(1.0); -Heap::MathObject::MathObject(ExecutionEngine *e) - : Heap::Object(e->emptyClass, e->objectPrototype.asObject()) +Heap::MathObject::MathObject() { - Scope scope(e); + Scope scope(internalClass->engine); ScopedObject m(scope, this); m->defineReadonlyProperty(QStringLiteral("E"), Primitive::fromDouble(M_E)); @@ -278,10 +277,15 @@ Q_GLOBAL_STATIC(QThreadStorage<bool *>, seedCreatedStorage); ReturnedValue MathObject::method_random(CallContext *context) { if (!seedCreatedStorage()->hasLocalData()) { - qsrand(QTime(0,0,0).msecsTo(QTime::currentTime()) ^ reinterpret_cast<quintptr>(context)); + int msecs = QTime(0,0,0).msecsTo(QTime::currentTime()); + Q_ASSERT(msecs >= 0); + qsrand(uint(uint(msecs) ^ reinterpret_cast<quintptr>(context))); seedCreatedStorage()->setLocalData(new bool(true)); } - return Encode(qrand() / (double) RAND_MAX); + // rand()/qrand() return a value where the upperbound is RAND_MAX inclusive. So, instead of + // dividing by RAND_MAX (which would return 0..RAND_MAX inclusive), we divide by RAND_MAX + 1. + qint64 upperLimit = qint64(RAND_MAX) + 1; + return Encode(qrand() / double(upperLimit)); } ReturnedValue MathObject::method_round(CallContext *context) |