| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qscriptecmamath_p.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_SCRIPT
|
|---|
| 45 |
|
|---|
| 46 | #include "qscriptengine_p.h"
|
|---|
| 47 | #include "qscriptvalueimpl_p.h"
|
|---|
| 48 | #include "qscriptcontext_p.h"
|
|---|
| 49 | #include "qscriptmember_p.h"
|
|---|
| 50 | #include "qscriptobject_p.h"
|
|---|
| 51 |
|
|---|
| 52 | #include <QtCore/QtDebug>
|
|---|
| 53 | #include <QtCore/qnumeric.h>
|
|---|
| 54 | #include <QtCore/QSysInfo>
|
|---|
| 55 | #include <math.h>
|
|---|
| 56 |
|
|---|
| 57 | QT_BEGIN_NAMESPACE
|
|---|
| 58 |
|
|---|
| 59 | namespace QScript { namespace Ecma {
|
|---|
| 60 |
|
|---|
| 61 | static const qsreal qt_PI = 2.0 * ::asin(1.0);
|
|---|
| 62 |
|
|---|
| 63 | Math::Math(QScriptEnginePrivate *engine, QScriptClassInfo *classInfo):
|
|---|
| 64 | m_engine(engine),
|
|---|
| 65 | m_classInfo(classInfo)
|
|---|
| 66 | {
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | Math::~Math()
|
|---|
| 70 | {
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | void Math::construct(QScriptValueImpl *object, QScriptEnginePrivate *eng)
|
|---|
| 74 | {
|
|---|
| 75 | QScriptClassInfo *classInfo = eng->registerClass(QLatin1String("Math"));
|
|---|
| 76 |
|
|---|
| 77 | Math *instance = new Math(eng, classInfo);
|
|---|
| 78 | eng->newObject(object, classInfo);
|
|---|
| 79 | object->setObjectData(instance);
|
|---|
| 80 |
|
|---|
| 81 | QScriptValue::PropertyFlags flags = QScriptValue::Undeletable
|
|---|
| 82 | | QScriptValue::ReadOnly
|
|---|
| 83 | | QScriptValue::SkipInEnumeration;
|
|---|
| 84 |
|
|---|
| 85 | object->setProperty(QLatin1String("E"),
|
|---|
| 86 | QScriptValueImpl(::exp(1.0)), flags);
|
|---|
| 87 | object->setProperty(QLatin1String("LN2"),
|
|---|
| 88 | QScriptValueImpl(::log(2.0)), flags);
|
|---|
| 89 | object->setProperty(QLatin1String("LN10"),
|
|---|
| 90 | QScriptValueImpl(::log(10.0)), flags);
|
|---|
| 91 | object->setProperty(QLatin1String("LOG2E"),
|
|---|
| 92 | QScriptValueImpl(1.0/::log(2.0)), flags);
|
|---|
| 93 | object->setProperty(QLatin1String("LOG10E"),
|
|---|
| 94 | QScriptValueImpl(1.0/::log(10.0)), flags);
|
|---|
| 95 | object->setProperty(QLatin1String("PI"),
|
|---|
| 96 | QScriptValueImpl(qt_PI), flags);
|
|---|
| 97 | object->setProperty(QLatin1String("SQRT1_2"),
|
|---|
| 98 | QScriptValueImpl(::sqrt(0.5)), flags);
|
|---|
| 99 | object->setProperty(QLatin1String("SQRT2"),
|
|---|
| 100 | QScriptValueImpl(::sqrt(2.0)), flags);
|
|---|
| 101 |
|
|---|
| 102 | flags = QScriptValue::SkipInEnumeration;
|
|---|
| 103 | addFunction(*object, QLatin1String("abs"), method_abs, 1, flags);
|
|---|
| 104 | addFunction(*object, QLatin1String("acos"), method_acos, 1, flags);
|
|---|
| 105 | addFunction(*object, QLatin1String("asin"), method_asin, 0, flags);
|
|---|
| 106 | addFunction(*object, QLatin1String("atan"), method_atan, 1, flags);
|
|---|
| 107 | addFunction(*object, QLatin1String("atan2"), method_atan2, 2, flags);
|
|---|
| 108 | addFunction(*object, QLatin1String("ceil"), method_ceil, 1, flags);
|
|---|
| 109 | addFunction(*object, QLatin1String("cos"), method_cos, 1, flags);
|
|---|
| 110 | addFunction(*object, QLatin1String("exp"), method_exp, 1, flags);
|
|---|
| 111 | addFunction(*object, QLatin1String("floor"), method_floor, 1, flags);
|
|---|
| 112 | addFunction(*object, QLatin1String("log"), method_log, 1, flags);
|
|---|
| 113 | addFunction(*object, QLatin1String("max"), method_max, 2, flags);
|
|---|
| 114 | addFunction(*object, QLatin1String("min"), method_min, 2, flags);
|
|---|
| 115 | addFunction(*object, QLatin1String("pow"), method_pow, 2, flags);
|
|---|
| 116 | addFunction(*object, QLatin1String("random"), method_random, 0, flags);
|
|---|
| 117 | addFunction(*object, QLatin1String("round"), method_round, 1, flags);
|
|---|
| 118 | addFunction(*object, QLatin1String("sin"), method_sin, 1, flags);
|
|---|
| 119 | addFunction(*object, QLatin1String("sqrt"), method_sqrt, 1, flags);
|
|---|
| 120 | addFunction(*object, QLatin1String("tan"), method_tan, 1, flags);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /* copies the sign from y to x and returns the result */
|
|---|
| 124 | static qsreal copySign(qsreal x, qsreal y)
|
|---|
| 125 | {
|
|---|
| 126 | uchar *xch = (uchar *)&x;
|
|---|
| 127 | uchar *ych = (uchar *)&y;
|
|---|
| 128 | if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
|
|---|
| 129 | xch[0] = (xch[0] & 0x7f) | (ych[0] & 0x80);
|
|---|
| 130 | else
|
|---|
| 131 | xch[7] = (xch[7] & 0x7f) | (ych[7] & 0x80);
|
|---|
| 132 | return x;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | QScriptValueImpl Math::method_abs(QScriptContextPrivate *context,
|
|---|
| 136 | QScriptEnginePrivate *,
|
|---|
| 137 | QScriptClassInfo *)
|
|---|
| 138 | {
|
|---|
| 139 | qsreal v = context->argument(0).toNumber();
|
|---|
| 140 | if (v == 0) // 0 | -0
|
|---|
| 141 | return (QScriptValueImpl(0));
|
|---|
| 142 | else
|
|---|
| 143 | return (QScriptValueImpl(v < 0 ? -v : v));
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | QScriptValueImpl Math::method_acos(QScriptContextPrivate *context,
|
|---|
| 147 | QScriptEnginePrivate *,
|
|---|
| 148 | QScriptClassInfo *)
|
|---|
| 149 | {
|
|---|
| 150 | qsreal v = context->argument(0).toNumber();
|
|---|
| 151 | if (v > 1)
|
|---|
| 152 | return QScriptValueImpl(qSNaN());
|
|---|
| 153 | return (QScriptValueImpl(::acos(v)));
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | QScriptValueImpl Math::method_asin(QScriptContextPrivate *context,
|
|---|
| 157 | QScriptEnginePrivate *,
|
|---|
| 158 | QScriptClassInfo *)
|
|---|
| 159 | {
|
|---|
| 160 | qsreal v = context->argument(0).toNumber();
|
|---|
| 161 | if (v > 1)
|
|---|
| 162 | return QScriptValueImpl(qSNaN());
|
|---|
| 163 | return (QScriptValueImpl(::asin(v)));
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | QScriptValueImpl Math::method_atan(QScriptContextPrivate *context,
|
|---|
| 167 | QScriptEnginePrivate *,
|
|---|
| 168 | QScriptClassInfo *)
|
|---|
| 169 | {
|
|---|
| 170 | qsreal v = context->argument(0).toNumber();
|
|---|
| 171 | if (v == 0.0)
|
|---|
| 172 | return QScriptValueImpl(v);
|
|---|
| 173 | return (QScriptValueImpl(::atan(v)));
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | QScriptValueImpl Math::method_atan2(QScriptContextPrivate *context,
|
|---|
| 177 | QScriptEnginePrivate *,
|
|---|
| 178 | QScriptClassInfo *)
|
|---|
| 179 | {
|
|---|
| 180 | qsreal v1 = context->argument(0).toNumber();
|
|---|
| 181 | qsreal v2 = context->argument(1).toNumber();
|
|---|
| 182 | #ifdef Q_OS_WINCE
|
|---|
| 183 | if (v1 == 0.0) {
|
|---|
| 184 | const bool v1MinusZero = _copysign(1.0, v1) < 0.0;
|
|---|
| 185 | const bool v2MinusZero = (v2 == 0 && _copysign(1.0, v2) < 0.0);
|
|---|
| 186 | if ((v1MinusZero && v2MinusZero) || (v1MinusZero && v2 == -1.0))
|
|---|
| 187 | return QScriptValueImpl(-qt_PI);
|
|---|
| 188 | if (v2MinusZero)
|
|---|
| 189 | return QScriptValueImpl(qt_PI);
|
|---|
| 190 | if (v1MinusZero && v2 == 1.0)
|
|---|
| 191 | return QScriptValueImpl(-0.0);
|
|---|
| 192 | #if defined(_X86_)
|
|---|
| 193 | if (v2 == 0.0 && (v1MinusZero || (!v1MinusZero && !v2MinusZero)))
|
|---|
| 194 | return QScriptValueImpl(0.0);
|
|---|
| 195 | #endif
|
|---|
| 196 | }
|
|---|
| 197 | #endif
|
|---|
| 198 | #if defined(Q_OS_WINCE) && defined(_X86_)
|
|---|
| 199 | if (v1 == -1.0 && !_finite(v2) && _copysign(1.0, v2) > 0.0)
|
|---|
| 200 | return QScriptValueImpl(-0.0);
|
|---|
| 201 | #endif
|
|---|
| 202 | if ((v1 < 0) && qIsFinite(v1) && qIsInf(v2) && (copySign(1.0, v2) == 1.0))
|
|---|
| 203 | return QScriptValueImpl(copySign(0, -1.0));
|
|---|
| 204 | if ((v1 == 0.0) && (v2 == 0.0)) {
|
|---|
| 205 | if ((copySign(1.0, v1) == 1.0) && (copySign(1.0, v2) == -1.0))
|
|---|
| 206 | return QScriptValueImpl(qt_PI);
|
|---|
| 207 | else if ((copySign(1.0, v1) == -1.0) && (copySign(1.0, v2) == -1.0))
|
|---|
| 208 | return QScriptValueImpl(-qt_PI);
|
|---|
| 209 | }
|
|---|
| 210 | return (QScriptValueImpl(::atan2(v1, v2)));
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | QScriptValueImpl Math::method_ceil(QScriptContextPrivate *context,
|
|---|
| 214 | QScriptEnginePrivate *,
|
|---|
| 215 | QScriptClassInfo *)
|
|---|
| 216 | {
|
|---|
| 217 | qsreal v = context->argument(0).toNumber();
|
|---|
| 218 | if (v < 0.0 && v > -1.0)
|
|---|
| 219 | return QScriptValueImpl(copySign(0, -1.0));
|
|---|
| 220 | return (QScriptValueImpl(::ceil(v)));
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | QScriptValueImpl Math::method_cos(QScriptContextPrivate *context,
|
|---|
| 224 | QScriptEnginePrivate *,
|
|---|
| 225 | QScriptClassInfo *)
|
|---|
| 226 | {
|
|---|
| 227 | qsreal v = context->argument(0).toNumber();
|
|---|
| 228 | return (QScriptValueImpl(::cos(v)));
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | QScriptValueImpl Math::method_exp(QScriptContextPrivate *context,
|
|---|
| 232 | QScriptEnginePrivate *,
|
|---|
| 233 | QScriptClassInfo *)
|
|---|
| 234 | {
|
|---|
| 235 | qsreal v = context->argument(0).toNumber();
|
|---|
| 236 | if (qIsInf(v)) {
|
|---|
| 237 | if (copySign(1.0, v) == -1.0)
|
|---|
| 238 | return QScriptValueImpl(0);
|
|---|
| 239 | else
|
|---|
| 240 | return QScriptValueImpl(qInf());
|
|---|
| 241 | }
|
|---|
| 242 | return (QScriptValueImpl(::exp(v)));
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | QScriptValueImpl Math::method_floor(QScriptContextPrivate *context,
|
|---|
| 246 | QScriptEnginePrivate *,
|
|---|
| 247 | QScriptClassInfo *)
|
|---|
| 248 | {
|
|---|
| 249 | qsreal v = context->argument(0).toNumber();
|
|---|
| 250 | return (QScriptValueImpl(::floor(v)));
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | QScriptValueImpl Math::method_log(QScriptContextPrivate *context,
|
|---|
| 254 | QScriptEnginePrivate *,
|
|---|
| 255 | QScriptClassInfo *)
|
|---|
| 256 | {
|
|---|
| 257 | qsreal v = context->argument(0).toNumber();
|
|---|
| 258 | if (v < 0)
|
|---|
| 259 | return QScriptValueImpl(qSNaN());
|
|---|
| 260 | return (QScriptValueImpl(::log(v)));
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | QScriptValueImpl Math::method_max(QScriptContextPrivate *context,
|
|---|
| 264 | QScriptEnginePrivate *,
|
|---|
| 265 | QScriptClassInfo *)
|
|---|
| 266 | {
|
|---|
| 267 | qsreal mx = -qInf();
|
|---|
| 268 | for (int i = 0; i < context->argumentCount(); ++i) {
|
|---|
| 269 | qsreal x = context->argument(i).toNumber();
|
|---|
| 270 | if (x > mx || qIsNaN(x))
|
|---|
| 271 | mx = x;
|
|---|
| 272 | }
|
|---|
| 273 | return (QScriptValueImpl(mx));
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | QScriptValueImpl Math::method_min(QScriptContextPrivate *context,
|
|---|
| 277 | QScriptEnginePrivate *,
|
|---|
| 278 | QScriptClassInfo *)
|
|---|
| 279 | {
|
|---|
| 280 | qsreal mx = qInf();
|
|---|
| 281 | for (int i = 0; i < context->argumentCount(); ++i) {
|
|---|
| 282 | qsreal x = context->argument(i).toNumber();
|
|---|
| 283 | if ((x == 0 && mx == x && copySign(1.0, x) == -1.0)
|
|---|
| 284 | || (x < mx) || qIsNaN(x)) {
|
|---|
| 285 | mx = x;
|
|---|
| 286 | }
|
|---|
| 287 | }
|
|---|
| 288 | return (QScriptValueImpl(mx));
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | QScriptValueImpl Math::method_pow(QScriptContextPrivate *context,
|
|---|
| 292 | QScriptEnginePrivate *,
|
|---|
| 293 | QScriptClassInfo *)
|
|---|
| 294 | {
|
|---|
| 295 | qsreal x = context->argument(0).toNumber();
|
|---|
| 296 | qsreal y = context->argument(1).toNumber();
|
|---|
| 297 | if (qIsNaN(y))
|
|---|
| 298 | return QScriptValueImpl(qSNaN());
|
|---|
| 299 | if (y == 0)
|
|---|
| 300 | return QScriptValueImpl(1);
|
|---|
| 301 | if (((x == 1) || (x == -1)) && qIsInf(y))
|
|---|
| 302 | return QScriptValueImpl(qSNaN());
|
|---|
| 303 | if (((x == 0) && copySign(1.0, x) == 1.0) && (y < 0))
|
|---|
| 304 | return QScriptValueImpl(qInf());
|
|---|
| 305 | if ((x == 0) && copySign(1.0, x) == -1.0) {
|
|---|
| 306 | if (y < 0) {
|
|---|
| 307 | if (::fmod(-y, 2.0) == 1.0)
|
|---|
| 308 | return QScriptValueImpl(-qInf());
|
|---|
| 309 | else
|
|---|
| 310 | return QScriptValueImpl(qInf());
|
|---|
| 311 | } else if (y > 0) {
|
|---|
| 312 | if (::fmod(y, 2.0) == 1.0)
|
|---|
| 313 | return QScriptValueImpl(copySign(0, -1.0));
|
|---|
| 314 | else
|
|---|
| 315 | return QScriptValueImpl(0);
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 | #ifdef Q_OS_AIX
|
|---|
| 319 | if (qIsInf(x) && copySign(1.0, x) == -1.0) {
|
|---|
| 320 | if (y > 0) {
|
|---|
| 321 | if (::fmod(y, 2.0) == 1.0)
|
|---|
| 322 | return QScriptValueImpl(-qInf());
|
|---|
| 323 | else
|
|---|
| 324 | return QScriptValueImpl(qInf());
|
|---|
| 325 | } else if (y < 0) {
|
|---|
| 326 | if (::fmod(-y, 2.0) == 1.0)
|
|---|
| 327 | return QScriptValueImpl(copySign(0, -1.0));
|
|---|
| 328 | else
|
|---|
| 329 | return QScriptValueImpl(0);
|
|---|
| 330 | }
|
|---|
| 331 | }
|
|---|
| 332 | #endif
|
|---|
| 333 | return (QScriptValueImpl(::pow(x, y)));
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | QScriptValueImpl Math::method_random(QScriptContextPrivate *,
|
|---|
| 337 | QScriptEnginePrivate *,
|
|---|
| 338 | QScriptClassInfo *)
|
|---|
| 339 | {
|
|---|
| 340 | return (QScriptValueImpl(qrand() / (qsreal) RAND_MAX));
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | QScriptValueImpl Math::method_round(QScriptContextPrivate *context,
|
|---|
| 344 | QScriptEnginePrivate *,
|
|---|
| 345 | QScriptClassInfo *)
|
|---|
| 346 | {
|
|---|
| 347 | qsreal v = context->argument(0).toNumber();
|
|---|
| 348 | v = copySign(::floor(v + 0.5), v);
|
|---|
| 349 | return (QScriptValueImpl(v));
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | QScriptValueImpl Math::method_sin(QScriptContextPrivate *context,
|
|---|
| 353 | QScriptEnginePrivate *,
|
|---|
| 354 | QScriptClassInfo *)
|
|---|
| 355 | {
|
|---|
| 356 | qsreal v = context->argument(0).toNumber();
|
|---|
| 357 | return (QScriptValueImpl(::sin(v)));
|
|---|
| 358 | }
|
|---|
| 359 |
|
|---|
| 360 | QScriptValueImpl Math::method_sqrt(QScriptContextPrivate *context,
|
|---|
| 361 | QScriptEnginePrivate *,
|
|---|
| 362 | QScriptClassInfo *)
|
|---|
| 363 | {
|
|---|
| 364 | qsreal v = context->argument(0).toNumber();
|
|---|
| 365 | return (QScriptValueImpl(::sqrt(v)));
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | QScriptValueImpl Math::method_tan(QScriptContextPrivate *context,
|
|---|
| 369 | QScriptEnginePrivate *,
|
|---|
| 370 | QScriptClassInfo *)
|
|---|
| 371 | {
|
|---|
| 372 | qsreal v = context->argument(0).toNumber();
|
|---|
| 373 | if (v == 0.0)
|
|---|
| 374 | return QScriptValueImpl(v);
|
|---|
| 375 | return (QScriptValueImpl(::tan(v)));
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | void Math::addFunction(QScriptValueImpl &object, const QString &name,
|
|---|
| 379 | QScriptInternalFunctionSignature fun, int length,
|
|---|
| 380 | const QScriptValue::PropertyFlags flags)
|
|---|
| 381 | {
|
|---|
| 382 | QScriptEnginePrivate *eng_p = object.engine();
|
|---|
| 383 | QScriptValueImpl val = eng_p->createFunction(fun, length, object.classInfo(), name);
|
|---|
| 384 | object.setProperty(name, val, flags);
|
|---|
| 385 | }
|
|---|
| 386 |
|
|---|
| 387 | } } // namespace QScript::Ecma
|
|---|
| 388 |
|
|---|
| 389 | QT_END_NAMESPACE
|
|---|
| 390 |
|
|---|
| 391 | #endif // QT_NO_SCRIPT
|
|---|