2012-11-17 21:10:52 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
|
|
|
** This file is part of the V4VM module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
|
|
|
**
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
#include <qmljs_engine.h>
|
2013-01-21 20:54:53 +00:00
|
|
|
#include <qv4object.h>
|
2013-01-21 21:17:55 +00:00
|
|
|
#include <qv4objectproto.h>
|
2012-11-17 21:10:52 +00:00
|
|
|
|
2013-02-08 08:23:58 +00:00
|
|
|
#include <wtf/MathExtras.h>
|
|
|
|
|
2012-11-17 21:10:52 +00:00
|
|
|
namespace QQmlJS {
|
|
|
|
namespace VM {
|
|
|
|
|
|
|
|
|
2013-01-28 11:54:07 +00:00
|
|
|
int Value::toUInt16(ExecutionContext *ctx) const
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
2013-02-14 22:00:11 +00:00
|
|
|
if (isConvertibleToInt())
|
|
|
|
return (ushort)(uint)integerValue();
|
|
|
|
|
|
|
|
double number = __qmljs_to_number(*this, ctx);
|
|
|
|
|
|
|
|
double D16 = 65536.0;
|
|
|
|
if ((number >= 0 && number < D16))
|
|
|
|
return static_cast<ushort>(number);
|
|
|
|
|
|
|
|
if (!isfinite(number))
|
|
|
|
return +0;
|
|
|
|
|
|
|
|
double d = ::floor(::fabs(number));
|
|
|
|
if (signbit(number))
|
|
|
|
d = -d;
|
|
|
|
|
|
|
|
number = ::fmod(d , D16);
|
|
|
|
|
|
|
|
if (number < 0)
|
|
|
|
number += D16;
|
|
|
|
|
|
|
|
return (unsigned short)number;
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Value::toInteger(ExecutionContext *ctx) const
|
|
|
|
{
|
2013-02-14 22:00:11 +00:00
|
|
|
if (isConvertibleToInt())
|
|
|
|
return int_32;
|
|
|
|
|
|
|
|
return Value::toInteger(__qmljs_to_number(*this, ctx));
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double Value::toNumber(ExecutionContext *ctx) const
|
|
|
|
{
|
|
|
|
return __qmljs_to_number(*this, ctx);
|
|
|
|
}
|
|
|
|
|
2013-01-18 11:47:43 +00:00
|
|
|
bool Value::sameValue(Value other) const {
|
2012-11-17 21:10:52 +00:00
|
|
|
if (val == other.val)
|
|
|
|
return true;
|
|
|
|
if (isString() && other.isString())
|
2012-12-17 09:03:37 +00:00
|
|
|
return stringValue()->isEqualTo(other.stringValue());
|
2013-02-16 22:26:16 +00:00
|
|
|
if (isInteger())
|
|
|
|
return int_32 ? (double(int_32) == other.dbl) : (other.val == 0);
|
|
|
|
if (other.isInteger())
|
|
|
|
return other.int_32 ? (dbl == double(other.int_32)) : (val == 0);
|
2012-11-17 21:10:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::fromString(ExecutionContext *ctx, const QString &s)
|
|
|
|
{
|
|
|
|
return fromString(ctx->engine->newString(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
int Value::toInt32(double number)
|
|
|
|
{
|
|
|
|
const double D32 = 4294967296.0;
|
|
|
|
const double D31 = D32 / 2.0;
|
|
|
|
|
|
|
|
if ((number >= -D31 && number < D31))
|
|
|
|
return static_cast<int>(number);
|
|
|
|
|
|
|
|
|
2013-02-08 08:23:58 +00:00
|
|
|
if (!isfinite(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
double d = ::floor(::fabs(number));
|
2013-02-08 08:23:58 +00:00
|
|
|
if (signbit(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
d = -d;
|
|
|
|
|
|
|
|
number = ::fmod(d , D32);
|
|
|
|
|
|
|
|
if (number < -D31)
|
|
|
|
number += D32;
|
|
|
|
else if (number >= D31)
|
|
|
|
number -= D32;
|
|
|
|
|
|
|
|
return int(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Value::toUInt32(double number)
|
|
|
|
{
|
|
|
|
const double D32 = 4294967296.0;
|
|
|
|
if ((number >= 0 && number < D32))
|
|
|
|
return static_cast<uint>(number);
|
|
|
|
|
2013-02-08 08:23:58 +00:00
|
|
|
if (!isfinite(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return +0;
|
|
|
|
|
|
|
|
double d = ::floor(::fabs(number));
|
2013-02-08 08:23:58 +00:00
|
|
|
if (signbit(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
d = -d;
|
|
|
|
|
|
|
|
number = ::fmod(d , D32);
|
|
|
|
|
|
|
|
if (number < 0)
|
|
|
|
number += D32;
|
|
|
|
|
|
|
|
return unsigned(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Value::toInteger(double number)
|
|
|
|
{
|
2013-02-08 08:23:58 +00:00
|
|
|
if (isnan(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return +0;
|
2013-02-08 08:23:58 +00:00
|
|
|
else if (! number || isinf(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return number;
|
|
|
|
const double v = floor(fabs(number));
|
2013-02-08 08:23:58 +00:00
|
|
|
return signbit(number) ? -v : v;
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Value Value::property(ExecutionContext *ctx, String *name) const
|
|
|
|
{
|
|
|
|
return isObject() ? objectValue()->__get__(ctx, name) : undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace VM
|
|
|
|
} // namespace QQmlJS
|