2012-11-17 21:10:52 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-11-17 21:10:52 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
2013-06-24 11:50:51 +00:00
|
|
|
** This file is part of the QtQml module of the Qt Toolkit.
|
2012-11-17 21:10:52 +00:00
|
|
|
**
|
2014-08-22 06:13:59 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL21$
|
2012-11-17 21:10:52 +00:00
|
|
|
** 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
|
2014-08-22 06:13:59 +00:00
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
2012-11-17 21:10:52 +00:00
|
|
|
** 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
|
2014-08-22 06:13:59 +00:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-11-17 21:10:52 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
2014-08-22 06:13:59 +00:00
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2012-11-17 21:10:52 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
2013-04-15 09:50:16 +00:00
|
|
|
#include <qv4engine_p.h>
|
2014-07-25 15:44:14 +00:00
|
|
|
#include <qv4runtime_p.h>
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
2013-04-15 09:50:16 +00:00
|
|
|
#include <qv4object_p.h>
|
|
|
|
#include <qv4objectproto_p.h>
|
|
|
|
#include "qv4mm_p.h"
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
2012-11-17 21:10:52 +00:00
|
|
|
|
2013-02-08 08:23:58 +00:00
|
|
|
#include <wtf/MathExtras.h>
|
|
|
|
|
2013-04-19 11:03:42 +00:00
|
|
|
using namespace QV4;
|
2012-11-17 21:10:52 +00:00
|
|
|
|
2013-04-14 20:01:20 +00:00
|
|
|
int Value::toUInt16() const
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
2013-09-15 13:46:36 +00:00
|
|
|
if (integerCompatible())
|
2013-02-14 22:00:11 +00:00
|
|
|
return (ushort)(uint)integerValue();
|
|
|
|
|
2013-09-09 11:38:10 +00:00
|
|
|
double number = toNumber();
|
2013-02-14 22:00:11 +00:00
|
|
|
|
|
|
|
double D16 = 65536.0;
|
|
|
|
if ((number >= 0 && number < D16))
|
|
|
|
return static_cast<ushort>(number);
|
|
|
|
|
2013-04-12 13:51:47 +00:00
|
|
|
if (!std::isfinite(number))
|
2013-02-14 22:00:11 +00:00
|
|
|
return +0;
|
|
|
|
|
|
|
|
double d = ::floor(::fabs(number));
|
2013-04-12 13:51:47 +00:00
|
|
|
if (std::signbit(number))
|
2013-02-14 22:00:11 +00:00
|
|
|
d = -d;
|
|
|
|
|
|
|
|
number = ::fmod(d , D16);
|
|
|
|
|
|
|
|
if (number < 0)
|
|
|
|
number += D16;
|
|
|
|
|
|
|
|
return (unsigned short)number;
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 20:01:20 +00:00
|
|
|
double Value::toInteger() const
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
2013-09-15 13:46:36 +00:00
|
|
|
if (integerCompatible())
|
2013-02-14 22:00:11 +00:00
|
|
|
return int_32;
|
|
|
|
|
2013-09-25 11:52:15 +00:00
|
|
|
return Primitive::toInteger(toNumber());
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2013-09-14 13:08:11 +00:00
|
|
|
double Value::toNumberImpl() const
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
2013-09-09 12:33:28 +00:00
|
|
|
switch (type()) {
|
2013-09-09 11:38:10 +00:00
|
|
|
case QV4::Value::Undefined_Type:
|
|
|
|
return std::numeric_limits<double>::quiet_NaN();
|
2013-09-15 13:46:36 +00:00
|
|
|
case QV4::Value::Managed_Type:
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifdef V4_BOOTSTRAP
|
|
|
|
Q_UNIMPLEMENTED();
|
|
|
|
#else
|
2013-09-15 13:46:36 +00:00
|
|
|
if (isString())
|
2014-03-10 18:58:05 +00:00
|
|
|
return RuntimeHelpers::stringToNumber(stringValue()->toQString());
|
2013-09-15 13:46:36 +00:00
|
|
|
{
|
2015-01-11 15:30:29 +00:00
|
|
|
Q_ASSERT(isObject());
|
|
|
|
Scope scope(objectValue()->engine());
|
2015-01-15 10:36:57 +00:00
|
|
|
ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, NUMBER_HINT));
|
2014-11-01 20:44:57 +00:00
|
|
|
if (scope.engine->hasException)
|
|
|
|
return 0;
|
2013-09-15 13:46:36 +00:00
|
|
|
return prim->toNumber();
|
|
|
|
}
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif
|
2013-09-14 13:08:11 +00:00
|
|
|
case QV4::Value::Null_Type:
|
|
|
|
case QV4::Value::Boolean_Type:
|
|
|
|
case QV4::Value::Integer_Type:
|
2014-02-07 08:46:02 +00:00
|
|
|
return int_32;
|
2013-09-09 11:38:10 +00:00
|
|
|
default: // double
|
2013-09-14 13:08:11 +00:00
|
|
|
Q_UNREACHABLE();
|
2013-09-09 11:38:10 +00:00
|
|
|
}
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
2013-09-09 12:33:28 +00:00
|
|
|
QString Value::toQStringNoThrow() const
|
2013-04-27 14:23:31 +00:00
|
|
|
{
|
|
|
|
switch (type()) {
|
2013-09-16 13:27:16 +00:00
|
|
|
case Value::Empty_Type:
|
2013-09-17 16:16:35 +00:00
|
|
|
Q_ASSERT(!"empty Value encountered");
|
|
|
|
case Value::Undefined_Type:
|
2013-04-27 14:23:31 +00:00
|
|
|
return QStringLiteral("undefined");
|
|
|
|
case Value::Null_Type:
|
|
|
|
return QStringLiteral("null");
|
|
|
|
case Value::Boolean_Type:
|
|
|
|
if (booleanValue())
|
|
|
|
return QStringLiteral("true");
|
|
|
|
else
|
|
|
|
return QStringLiteral("false");
|
2013-09-15 13:46:36 +00:00
|
|
|
case Value::Managed_Type:
|
|
|
|
if (isString())
|
|
|
|
return stringValue()->toQString();
|
|
|
|
{
|
2015-01-11 15:30:29 +00:00
|
|
|
Q_ASSERT(isObject());
|
|
|
|
Scope scope(objectValue()->engine());
|
2013-09-11 11:23:21 +00:00
|
|
|
ScopedValue ex(scope);
|
|
|
|
bool caughtException = false;
|
2015-01-15 14:01:06 +00:00
|
|
|
ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT));
|
2013-10-22 12:32:03 +00:00
|
|
|
if (scope.hasException()) {
|
2014-11-12 12:55:55 +00:00
|
|
|
ex = scope.engine->catchException();
|
2013-09-11 11:23:21 +00:00
|
|
|
caughtException = true;
|
2013-10-22 12:32:03 +00:00
|
|
|
} else if (prim->isPrimitive()) {
|
|
|
|
return prim->toQStringNoThrow();
|
2013-09-11 11:23:21 +00:00
|
|
|
}
|
|
|
|
// Can't nest try/catch due to CXX ABI limitations for foreign exception nesting.
|
|
|
|
if (caughtException) {
|
2014-03-10 18:58:05 +00:00
|
|
|
ScopedValue prim(scope, RuntimeHelpers::toPrimitive(ex, STRING_HINT));
|
2013-10-22 12:32:03 +00:00
|
|
|
if (scope.hasException()) {
|
2014-11-12 12:55:55 +00:00
|
|
|
ex = scope.engine->catchException();
|
2013-10-22 12:32:03 +00:00
|
|
|
} else if (prim->isPrimitive()) {
|
|
|
|
return prim->toQStringNoThrow();
|
2013-09-15 13:46:36 +00:00
|
|
|
}
|
2013-05-13 16:29:33 +00:00
|
|
|
}
|
2013-09-15 13:46:36 +00:00
|
|
|
return QString();
|
2013-04-27 14:23:31 +00:00
|
|
|
}
|
|
|
|
case Value::Integer_Type: {
|
2013-09-09 12:33:28 +00:00
|
|
|
QString str;
|
2014-03-10 18:58:05 +00:00
|
|
|
RuntimeHelpers::numberToString(&str, (double)int_32, 10);
|
2013-09-09 12:33:28 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
default: { // double
|
|
|
|
QString str;
|
2014-03-10 18:58:05 +00:00
|
|
|
RuntimeHelpers::numberToString(&str, doubleValue(), 10);
|
2013-09-09 12:33:28 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
} // switch
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Value::toQString() const
|
|
|
|
{
|
|
|
|
switch (type()) {
|
2013-09-16 13:27:16 +00:00
|
|
|
case Value::Empty_Type:
|
2013-09-17 16:16:35 +00:00
|
|
|
Q_ASSERT(!"empty Value encountered");
|
|
|
|
case Value::Undefined_Type:
|
2013-09-09 12:33:28 +00:00
|
|
|
return QStringLiteral("undefined");
|
|
|
|
case Value::Null_Type:
|
|
|
|
return QStringLiteral("null");
|
|
|
|
case Value::Boolean_Type:
|
|
|
|
if (booleanValue())
|
|
|
|
return QStringLiteral("true");
|
|
|
|
else
|
|
|
|
return QStringLiteral("false");
|
2013-09-15 13:46:36 +00:00
|
|
|
case Value::Managed_Type:
|
|
|
|
if (isString())
|
|
|
|
return stringValue()->toQString();
|
|
|
|
{
|
2015-01-11 15:30:29 +00:00
|
|
|
Q_ASSERT(isObject());
|
|
|
|
Scope scope(objectValue()->engine());
|
2015-01-15 14:01:06 +00:00
|
|
|
ScopedValue prim(scope, RuntimeHelpers::toPrimitive(*this, STRING_HINT));
|
2013-09-15 13:46:36 +00:00
|
|
|
return prim->toQString();
|
|
|
|
}
|
2013-09-09 12:33:28 +00:00
|
|
|
case Value::Integer_Type: {
|
2013-04-27 14:23:31 +00:00
|
|
|
QString str;
|
2014-03-10 18:58:05 +00:00
|
|
|
RuntimeHelpers::numberToString(&str, (double)int_32, 10);
|
2013-04-27 14:23:31 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
default: { // double
|
|
|
|
QString str;
|
2014-03-10 18:58:05 +00:00
|
|
|
RuntimeHelpers::numberToString(&str, doubleValue(), 10);
|
2013-04-27 14:23:31 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
} // switch
|
|
|
|
}
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif // V4_BOOTSTRAP
|
2013-04-27 14:23:31 +00:00
|
|
|
|
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-09-15 13:46:36 +00:00
|
|
|
if (isInteger() && other.isDouble())
|
|
|
|
return int_32 ? (double(int_32) == other.doubleValue()) : (other.val == 0);
|
|
|
|
if (isDouble() && other.isInteger())
|
|
|
|
return other.int_32 ? (doubleValue() == double(other.int_32)) : (val == 0);
|
2012-11-17 21:10:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-02 19:37:20 +00:00
|
|
|
|
2013-09-25 11:52:15 +00:00
|
|
|
int Primitive::toInt32(double number)
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
|
|
|
const double D32 = 4294967296.0;
|
|
|
|
const double D31 = D32 / 2.0;
|
|
|
|
|
|
|
|
if ((number >= -D31 && number < D31))
|
|
|
|
return static_cast<int>(number);
|
|
|
|
|
|
|
|
|
2013-04-12 13:51:47 +00:00
|
|
|
if (!std::isfinite(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
double d = ::floor(::fabs(number));
|
2013-04-12 13:51:47 +00:00
|
|
|
if (std::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);
|
|
|
|
}
|
|
|
|
|
2013-09-25 11:52:15 +00:00
|
|
|
unsigned int Primitive::toUInt32(double number)
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
|
|
|
const double D32 = 4294967296.0;
|
|
|
|
if ((number >= 0 && number < D32))
|
|
|
|
return static_cast<uint>(number);
|
|
|
|
|
2013-04-12 13:51:47 +00:00
|
|
|
if (!std::isfinite(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return +0;
|
|
|
|
|
|
|
|
double d = ::floor(::fabs(number));
|
2013-04-12 13:51:47 +00:00
|
|
|
if (std::signbit(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
d = -d;
|
|
|
|
|
|
|
|
number = ::fmod(d , D32);
|
|
|
|
|
|
|
|
if (number < 0)
|
|
|
|
number += D32;
|
|
|
|
|
|
|
|
return unsigned(number);
|
|
|
|
}
|
|
|
|
|
2013-09-25 11:52:15 +00:00
|
|
|
double Primitive::toInteger(double number)
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
2013-04-16 15:03:00 +00:00
|
|
|
if (std::isnan(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return +0;
|
2013-04-16 15:03:00 +00:00
|
|
|
else if (! number || std::isinf(number))
|
2012-11-17 21:10:52 +00:00
|
|
|
return number;
|
|
|
|
const double v = floor(fabs(number));
|
2013-04-12 13:51:47 +00:00
|
|
|
return std::signbit(number) ? -v : v;
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#ifndef V4_BOOTSTRAP
|
2014-11-11 14:08:30 +00:00
|
|
|
Heap::String *Value::toString(ExecutionEngine *e) const
|
2014-01-25 20:59:15 +00:00
|
|
|
{
|
2014-11-07 17:51:19 +00:00
|
|
|
if (isString())
|
2014-11-11 14:08:30 +00:00
|
|
|
return stringValue()->d();
|
2015-01-15 14:01:06 +00:00
|
|
|
return RuntimeHelpers::convertToString(e, *this);
|
2014-01-25 20:59:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 12:34:18 +00:00
|
|
|
Heap::Object *Value::toObject(ExecutionEngine *e) const
|
2013-09-09 11:38:10 +00:00
|
|
|
{
|
|
|
|
if (isObject())
|
2014-11-11 12:34:18 +00:00
|
|
|
return objectValue()->d();
|
2015-01-15 14:01:06 +00:00
|
|
|
return RuntimeHelpers::convertToObject(e, *this);
|
2014-11-07 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:55:06 +00:00
|
|
|
#endif // V4_BOOTSTRAP
|