2012-11-17 21:10:52 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2013-06-24 11:50:51 +00:00
|
|
|
** Copyright (C) 2013 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
|
|
|
**
|
|
|
|
** $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$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
2013-04-15 09:50:16 +00:00
|
|
|
#include <qv4engine_p.h>
|
|
|
|
#include <qv4object_p.h>
|
|
|
|
#include <qv4objectproto_p.h>
|
|
|
|
#include "qv4mm_p.h"
|
2013-07-10 13:44:11 +00:00
|
|
|
#include "qv4exception_p.h"
|
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-02-14 22:00:11 +00:00
|
|
|
if (isConvertibleToInt())
|
|
|
|
return (ushort)(uint)integerValue();
|
|
|
|
|
2013-04-14 20:01:20 +00:00
|
|
|
double number = __qmljs_to_number(*this);
|
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-02-14 22:00:11 +00:00
|
|
|
if (isConvertibleToInt())
|
|
|
|
return int_32;
|
|
|
|
|
2013-04-14 20:01:20 +00:00
|
|
|
return Value::toInteger(__qmljs_to_number(*this));
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 20:01:20 +00:00
|
|
|
double Value::toNumber() const
|
2012-11-17 21:10:52 +00:00
|
|
|
{
|
2013-04-14 20:01:20 +00:00
|
|
|
return __qmljs_to_number(*this);
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2013-04-27 14:23:31 +00:00
|
|
|
QString Value::toQString() const
|
|
|
|
{
|
|
|
|
switch (type()) {
|
|
|
|
case Value::Undefined_Type:
|
|
|
|
return QStringLiteral("undefined");
|
|
|
|
case Value::Null_Type:
|
|
|
|
return QStringLiteral("null");
|
|
|
|
case Value::Boolean_Type:
|
|
|
|
if (booleanValue())
|
|
|
|
return QStringLiteral("true");
|
|
|
|
else
|
|
|
|
return QStringLiteral("false");
|
|
|
|
case Value::String_Type:
|
|
|
|
return stringValue()->toQString();
|
|
|
|
case Value::Object_Type: {
|
|
|
|
ExecutionContext *ctx = objectValue()->internalClass->engine->current;
|
|
|
|
try {
|
|
|
|
Value prim = __qmljs_to_primitive(*this, STRING_HINT);
|
|
|
|
if (prim.isPrimitive())
|
|
|
|
return prim.toQString();
|
|
|
|
} catch (Exception &e) {
|
|
|
|
e.accept(ctx);
|
2013-05-13 16:29:33 +00:00
|
|
|
try {
|
|
|
|
Value prim = __qmljs_to_primitive(e.value(), STRING_HINT);
|
|
|
|
if (prim.isPrimitive())
|
|
|
|
return prim.toQString();
|
|
|
|
} catch(Exception &e) {
|
|
|
|
e.accept(ctx);
|
|
|
|
}
|
2013-04-27 14:23:31 +00:00
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
case Value::Integer_Type: {
|
|
|
|
QString str;
|
|
|
|
__qmljs_numberToString(&str, (double)int_32, 10);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
default: { // double
|
|
|
|
QString str;
|
|
|
|
__qmljs_numberToString(&str, doubleValue(), 10);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
} // switch
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2013-05-02 19:37:20 +00:00
|
|
|
Value Value::fromString(ExecutionEngine *engine, const QString &s)
|
|
|
|
{
|
|
|
|
return fromString(engine->newString(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-17 21:10:52 +00:00
|
|
|
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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int Value::toUInt32(double number)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Value::toInteger(double number)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-04-19 13:36:30 +00:00
|
|
|
String *Value::toString(ExecutionContext *ctx) const
|
|
|
|
{
|
|
|
|
if (isString())
|
|
|
|
return stringValue();
|
|
|
|
return __qmljs_convert_to_string(ctx, *this);
|
|
|
|
}
|
|
|
|
|
2012-11-17 21:10:52 +00:00
|
|
|
Value Value::property(ExecutionContext *ctx, String *name) const
|
|
|
|
{
|
2013-06-21 21:26:11 +00:00
|
|
|
return isObject() ? objectValue()->get(name) : undefinedValue();
|
2012-11-17 21:10:52 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 19:04:43 +00:00
|
|
|
|
2013-05-06 09:37:53 +00:00
|
|
|
PersistentValue::PersistentValue(const Value &val)
|
|
|
|
: d(new PersistentValuePrivate(val))
|
2013-03-06 19:04:43 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentValue::PersistentValue(const PersistentValue &other)
|
2013-04-16 09:36:56 +00:00
|
|
|
: d(other.d)
|
2013-03-06 19:04:43 +00:00
|
|
|
{
|
2013-05-07 11:17:57 +00:00
|
|
|
if (d)
|
|
|
|
d->ref();
|
2013-03-06 19:04:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PersistentValue &PersistentValue::operator=(const PersistentValue &other)
|
|
|
|
{
|
2013-04-16 09:36:56 +00:00
|
|
|
if (d == other.d)
|
2013-03-06 19:04:43 +00:00
|
|
|
return *this;
|
2013-04-16 09:36:56 +00:00
|
|
|
|
|
|
|
// the memory manager cleans up those with a refcount of 0
|
2013-05-07 11:17:57 +00:00
|
|
|
|
|
|
|
if (d)
|
|
|
|
d->deref();
|
2013-04-16 09:36:56 +00:00
|
|
|
d = other.d;
|
2013-05-07 11:17:57 +00:00
|
|
|
if (d)
|
|
|
|
d->ref();
|
2013-05-29 12:58:52 +00:00
|
|
|
|
|
|
|
return *this;
|
2013-03-06 19:04:43 +00:00
|
|
|
}
|
|
|
|
|
2013-05-06 09:37:53 +00:00
|
|
|
PersistentValue &PersistentValue::operator =(const Value &other)
|
|
|
|
{
|
2013-05-07 11:17:57 +00:00
|
|
|
if (!d) {
|
|
|
|
d = new PersistentValuePrivate(other);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-05-29 12:58:52 +00:00
|
|
|
d = d->detach(other);
|
|
|
|
return *this;
|
2013-05-06 09:37:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 19:04:43 +00:00
|
|
|
PersistentValue::~PersistentValue()
|
|
|
|
{
|
2013-05-07 11:17:57 +00:00
|
|
|
if (d)
|
|
|
|
d->deref();
|
2013-04-16 09:36:56 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 20:13:42 +00:00
|
|
|
WeakValue::WeakValue(const Value &val)
|
2013-06-21 13:19:20 +00:00
|
|
|
: d(new PersistentValuePrivate(val, /*engine*/0, /*weak*/true))
|
2013-05-23 20:13:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakValue::WeakValue(const WeakValue &other)
|
|
|
|
: d(other.d)
|
|
|
|
{
|
|
|
|
if (d)
|
|
|
|
d->ref();
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakValue &WeakValue::operator=(const WeakValue &other)
|
|
|
|
{
|
|
|
|
if (d == other.d)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
// the memory manager cleans up those with a refcount of 0
|
|
|
|
|
|
|
|
if (d)
|
|
|
|
d->deref();
|
|
|
|
d = other.d;
|
|
|
|
if (d)
|
|
|
|
d->ref();
|
2013-05-29 12:58:52 +00:00
|
|
|
|
|
|
|
return *this;
|
2013-05-23 20:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WeakValue &WeakValue::operator =(const Value &other)
|
|
|
|
{
|
|
|
|
if (!d) {
|
2013-06-21 13:19:20 +00:00
|
|
|
d = new PersistentValuePrivate(other, /*engine*/0, /*weak*/true);
|
2013-05-23 20:13:42 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-05-29 12:58:52 +00:00
|
|
|
d = d->detach(other, /*weak*/true);
|
|
|
|
return *this;
|
2013-05-23 20:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WeakValue::~WeakValue()
|
|
|
|
{
|
|
|
|
if (d)
|
|
|
|
d->deref();
|
|
|
|
}
|
|
|
|
|
2013-05-24 15:12:57 +00:00
|
|
|
void WeakValue::markOnce()
|
|
|
|
{
|
|
|
|
if (!d)
|
|
|
|
return;
|
|
|
|
Managed *m = d->value.asManaged();
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
m->mark();
|
|
|
|
}
|
2013-05-23 20:13:42 +00:00
|
|
|
|
2013-06-21 13:19:20 +00:00
|
|
|
PersistentValuePrivate::PersistentValuePrivate(const Value &v, ExecutionEngine *e, bool weak)
|
2013-04-17 16:17:30 +00:00
|
|
|
: value(v)
|
|
|
|
, refcount(1)
|
2013-05-06 13:11:01 +00:00
|
|
|
, prev(0)
|
2013-04-17 16:17:30 +00:00
|
|
|
, next(0)
|
2013-06-21 13:19:20 +00:00
|
|
|
, engine(e)
|
2013-04-16 09:36:56 +00:00
|
|
|
{
|
2013-06-21 13:19:20 +00:00
|
|
|
if (!engine) {
|
|
|
|
Managed *m = v.asManaged();
|
|
|
|
if (!m)
|
|
|
|
return;
|
2013-05-08 08:51:22 +00:00
|
|
|
|
2013-06-21 13:19:20 +00:00
|
|
|
engine = m->engine();
|
|
|
|
}
|
2013-05-08 08:51:22 +00:00
|
|
|
if (engine) {
|
2013-05-23 20:13:42 +00:00
|
|
|
PersistentValuePrivate **listRoot = weak ? &engine->memoryManager->m_weakValues : &engine->memoryManager->m_persistentValues;
|
|
|
|
|
|
|
|
prev = listRoot;
|
|
|
|
next = *listRoot;
|
2013-05-08 08:51:22 +00:00
|
|
|
*prev = this;
|
|
|
|
if (next)
|
|
|
|
next->prev = &this->next;
|
2013-05-06 09:37:53 +00:00
|
|
|
}
|
2013-04-16 09:36:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 11:54:36 +00:00
|
|
|
PersistentValuePrivate::~PersistentValuePrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-23 20:13:42 +00:00
|
|
|
void PersistentValuePrivate::removeFromList()
|
|
|
|
{
|
|
|
|
if (prev) {
|
|
|
|
if (next)
|
|
|
|
next->prev = prev;
|
|
|
|
*prev = next;
|
2013-05-29 12:58:52 +00:00
|
|
|
next = 0;
|
2013-05-23 20:13:42 +00:00
|
|
|
prev = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-16 09:36:56 +00:00
|
|
|
void PersistentValuePrivate::deref()
|
|
|
|
{
|
|
|
|
// if engine is not 0, they are registered with the memory manager
|
|
|
|
// and will get cleaned up in the next gc run
|
2013-05-06 13:11:01 +00:00
|
|
|
if (!--refcount) {
|
2013-05-23 20:13:42 +00:00
|
|
|
removeFromList();
|
2013-05-06 13:11:01 +00:00
|
|
|
delete this;
|
2013-05-06 09:37:53 +00:00
|
|
|
}
|
2013-03-06 19:04:43 +00:00
|
|
|
}
|
2013-05-23 20:13:42 +00:00
|
|
|
|
2013-05-29 12:58:52 +00:00
|
|
|
PersistentValuePrivate *PersistentValuePrivate::detach(const QV4::Value &value, bool weak)
|
|
|
|
{
|
|
|
|
if (refcount == 1) {
|
|
|
|
this->value = value;
|
|
|
|
|
|
|
|
Managed *m = value.asManaged();
|
|
|
|
if (!prev) {
|
|
|
|
if (m) {
|
|
|
|
ExecutionEngine *engine = m->engine();
|
|
|
|
if (engine) {
|
|
|
|
PersistentValuePrivate **listRoot = weak ? &engine->memoryManager->m_weakValues : &engine->memoryManager->m_persistentValues;
|
|
|
|
prev = listRoot;
|
|
|
|
next = *listRoot;
|
|
|
|
*prev = this;
|
|
|
|
if (next)
|
|
|
|
next->prev = &this->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!m)
|
|
|
|
removeFromList();
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
--refcount;
|
2013-06-21 13:19:20 +00:00
|
|
|
return new PersistentValuePrivate(value, engine, weak);
|
2013-05-29 12:58:52 +00:00
|
|
|
}
|
|
|
|
|