2013-12-16 08:16:57 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
**
|
|
|
|
** This file is part of the QtQml 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 "qv4arraydata_p.h"
|
|
|
|
#include "qv4object_p.h"
|
|
|
|
#include "qv4functionobject_p.h"
|
|
|
|
|
|
|
|
using namespace QV4;
|
|
|
|
|
|
|
|
const ArrayVTable ArrayData::static_vtbl =
|
|
|
|
{
|
|
|
|
ArrayData::Simple,
|
|
|
|
ArrayData::freeData,
|
|
|
|
ArrayData::reserve,
|
|
|
|
ArrayData::get,
|
|
|
|
ArrayData::put,
|
|
|
|
ArrayData::putArray,
|
|
|
|
ArrayData::del,
|
|
|
|
ArrayData::setAttribute,
|
|
|
|
ArrayData::attribute,
|
|
|
|
ArrayData::push_front,
|
|
|
|
ArrayData::pop_front,
|
|
|
|
ArrayData::truncate
|
|
|
|
};
|
|
|
|
|
|
|
|
const ArrayVTable SparseArrayData::static_vtbl =
|
|
|
|
{
|
|
|
|
ArrayData::Sparse,
|
|
|
|
SparseArrayData::freeData,
|
|
|
|
SparseArrayData::reserve,
|
|
|
|
SparseArrayData::get,
|
|
|
|
SparseArrayData::put,
|
|
|
|
SparseArrayData::putArray,
|
|
|
|
SparseArrayData::del,
|
|
|
|
SparseArrayData::setAttribute,
|
|
|
|
SparseArrayData::attribute,
|
|
|
|
SparseArrayData::push_front,
|
|
|
|
SparseArrayData::pop_front,
|
|
|
|
SparseArrayData::truncate
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void ArrayData::getHeadRoom(ArrayData *d)
|
|
|
|
{
|
|
|
|
Q_ASSERT(d);
|
|
|
|
Q_ASSERT(!d->offset);
|
|
|
|
d->offset = qMax(d->len >> 2, (uint)16);
|
2014-01-09 10:05:08 +00:00
|
|
|
SafeValue *newArray = new SafeValue[d->offset + d->alloc];
|
|
|
|
memcpy(newArray + d->offset, d->data, d->len*sizeof(SafeValue));
|
2013-12-16 08:16:57 +00:00
|
|
|
delete [] d->data;
|
|
|
|
d->data = newArray + d->offset;
|
|
|
|
if (d->attrs) {
|
|
|
|
PropertyAttributes *newAttrs = new PropertyAttributes[d->offset + d->alloc];
|
|
|
|
memcpy(newAttrs + d->offset, d->attrs, d->len*sizeof(PropertyAttributes));
|
|
|
|
delete [] d->attrs;
|
|
|
|
d->attrs = newAttrs + d->offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayData::reserve(ArrayData *d, uint n)
|
|
|
|
{
|
|
|
|
if (n < 8)
|
|
|
|
n = 8;
|
|
|
|
if (n <= d->alloc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->alloc = qMax(n, 2*d->alloc);
|
2014-01-09 10:05:08 +00:00
|
|
|
SafeValue *newArrayData = new SafeValue[d->alloc + d->offset];
|
2013-12-16 08:16:57 +00:00
|
|
|
if (d->data) {
|
2014-01-09 10:05:08 +00:00
|
|
|
memcpy(newArrayData + d->offset, d->data, sizeof(SafeValue)*d->len);
|
2013-12-16 08:16:57 +00:00
|
|
|
delete [] (d->data - d->offset);
|
|
|
|
}
|
|
|
|
d->data = newArrayData + d->offset;
|
|
|
|
|
|
|
|
if (d->attrs) {
|
|
|
|
PropertyAttributes *newAttrs = new PropertyAttributes[d->alloc];
|
|
|
|
memcpy(newAttrs, d->attrs, sizeof(PropertyAttributes)*d->len);
|
|
|
|
delete [] (d->attrs - d->offset);
|
|
|
|
|
|
|
|
d->attrs = newAttrs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayData::ensureAttributes()
|
|
|
|
{
|
|
|
|
if (attrs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (type == Simple)
|
|
|
|
type = Complex;
|
|
|
|
attrs = new PropertyAttributes[alloc + offset];
|
|
|
|
attrs += offset;
|
|
|
|
for (uint i = 0; i < len; ++i)
|
|
|
|
attrs[i] = Attr_Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ArrayData::freeData(ArrayData *d)
|
|
|
|
{
|
|
|
|
delete [] (d->data - d->offset);
|
|
|
|
if (d->attrs)
|
|
|
|
delete [] (d->attrs - d->offset);
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnedValue ArrayData::get(const ArrayData *d, uint index)
|
|
|
|
{
|
|
|
|
if (index >= d->len)
|
|
|
|
return Primitive::emptyValue().asReturnedValue();
|
2014-01-09 10:05:08 +00:00
|
|
|
return d->data[index].asReturnedValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ArrayData::put(ArrayData *d, uint index, ValueRef value)
|
|
|
|
{
|
2014-01-09 10:05:08 +00:00
|
|
|
Q_ASSERT(index >= d->len || !d->attrs || !d->attrs[index].isAccessor());
|
2013-12-16 08:16:57 +00:00
|
|
|
// ### honour attributes
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[index] = value;
|
|
|
|
if (index >= d->len) {
|
|
|
|
if (d->attrs)
|
|
|
|
d->attrs[index] = Attr_Data;
|
|
|
|
d->len = index;
|
|
|
|
}
|
2013-12-16 08:16:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArrayData::del(ArrayData *d, uint index)
|
|
|
|
{
|
|
|
|
if (index >= d->len)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!d->attrs || d->attrs[index].isConfigurable()) {
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[index] = Primitive::emptyValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
if (d->attrs)
|
|
|
|
d->attrs[index] = Attr_Data;
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-09 10:05:08 +00:00
|
|
|
if (d->data[index].isEmpty())
|
2013-12-16 08:16:57 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayData::setAttribute(ArrayData *d, uint index, PropertyAttributes attrs)
|
|
|
|
{
|
|
|
|
d->attrs[index] = attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyAttributes ArrayData::attribute(const ArrayData *d, uint index)
|
|
|
|
{
|
|
|
|
return d->attrs[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayData::push_front(ArrayData *d, SafeValue *values, uint n)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!d->attrs);
|
|
|
|
for (int i = n - 1; i >= 0; --i) {
|
|
|
|
if (!d->offset)
|
|
|
|
ArrayData::getHeadRoom(d);
|
|
|
|
|
|
|
|
--d->offset;
|
|
|
|
--d->data;
|
|
|
|
++d->len;
|
|
|
|
++d->alloc;
|
2014-01-09 10:05:08 +00:00
|
|
|
*d->data = values[i].asReturnedValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnedValue ArrayData::pop_front(ArrayData *d)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!d->attrs);
|
|
|
|
if (!d->len)
|
|
|
|
return Encode::undefined();
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
ReturnedValue v = d->data[0].isEmpty() ? Encode::undefined() : d->data[0].asReturnedValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
++d->offset;
|
|
|
|
++d->data;
|
|
|
|
--d->len;
|
|
|
|
--d->alloc;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint ArrayData::truncate(ArrayData *d, uint newLen)
|
|
|
|
{
|
|
|
|
if (d->attrs) {
|
2014-01-09 10:05:08 +00:00
|
|
|
SafeValue *it = d->data + d->len;
|
|
|
|
const SafeValue *begin = d->data + newLen;
|
2013-12-16 08:16:57 +00:00
|
|
|
while (--it >= begin) {
|
2014-01-09 10:05:08 +00:00
|
|
|
if (!it->isEmpty() && !d->attrs[it - d->data].isConfigurable()) {
|
2013-12-16 08:16:57 +00:00
|
|
|
newLen = it - d->data + 1;
|
|
|
|
break;
|
|
|
|
}
|
2014-01-09 10:05:08 +00:00
|
|
|
*it = Primitive::emptyValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
d->len = newLen;
|
|
|
|
return newLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ArrayData::putArray(ArrayData *d, uint index, SafeValue *values, uint n)
|
|
|
|
{
|
|
|
|
if (index + n > d->alloc)
|
|
|
|
reserve(d, index + n + 1);
|
|
|
|
for (uint i = d->len; i < index; ++i)
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[i] = Primitive::emptyValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
for (uint i = 0; i < n; ++i)
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[index + i] = values[i];
|
2013-12-16 08:16:57 +00:00
|
|
|
d->len = qMax(d->len, index + n);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseArrayData::free(ArrayData *d, uint idx)
|
|
|
|
{
|
|
|
|
Q_ASSERT(d && d->type == ArrayData::Sparse);
|
|
|
|
SparseArrayData *dd = static_cast<SparseArrayData *>(d);
|
2014-01-09 10:05:08 +00:00
|
|
|
SafeValue *v = dd->data + idx;
|
|
|
|
if (dd->attrs && dd->attrs[idx].isAccessor()) {
|
|
|
|
// double slot, free both. Order is important, so we have a double slot for allocation again afterwards.
|
|
|
|
v[1].tag = Value::Empty_Type;
|
|
|
|
v[1].uint_32 = dd->freeList;
|
|
|
|
v[0].tag = Value::Empty_Type;
|
|
|
|
v[0].uint_32 = idx + 1;
|
|
|
|
} else {
|
|
|
|
v->tag = Value::Empty_Type;
|
|
|
|
v->uint_32 = dd->freeList;
|
|
|
|
}
|
2013-12-16 08:16:57 +00:00
|
|
|
dd->freeList = idx;
|
|
|
|
if (dd->attrs)
|
|
|
|
dd->attrs[idx].clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SparseArrayData::freeData(ArrayData *d)
|
|
|
|
{
|
|
|
|
delete static_cast<SparseArrayData *>(d)->sparse;
|
|
|
|
ArrayData::freeData(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseArrayData::reserve(ArrayData *d, uint n)
|
|
|
|
{
|
|
|
|
if (n < 8)
|
|
|
|
n = 8;
|
|
|
|
if (n <= d->alloc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SparseArrayData *dd = static_cast<SparseArrayData *>(d);
|
2014-01-09 10:05:08 +00:00
|
|
|
uint oldAlloc = dd->alloc;
|
2013-12-16 08:16:57 +00:00
|
|
|
// ### FIXME
|
|
|
|
dd->len = dd->alloc;
|
|
|
|
dd->alloc = qMax(n, 2*dd->alloc);
|
2014-01-09 10:05:08 +00:00
|
|
|
SafeValue *newArrayData = new SafeValue[dd->alloc];
|
2013-12-16 08:16:57 +00:00
|
|
|
if (dd->data) {
|
2014-01-09 10:05:08 +00:00
|
|
|
memcpy(newArrayData, dd->data, sizeof(SafeValue)*dd->len);
|
2013-12-16 08:16:57 +00:00
|
|
|
delete [] dd->data;
|
|
|
|
}
|
|
|
|
dd->data = newArrayData;
|
|
|
|
if (dd->attrs) {
|
|
|
|
PropertyAttributes *newAttrs = new PropertyAttributes[dd->alloc];
|
|
|
|
memcpy(newAttrs, dd->attrs, sizeof(PropertyAttributes)*dd->len);
|
|
|
|
delete [] dd->attrs;
|
|
|
|
dd->attrs = newAttrs;
|
|
|
|
}
|
2014-01-09 10:05:08 +00:00
|
|
|
for (uint i = oldAlloc; i < dd->alloc; ++i)
|
|
|
|
dd->data[i] = Primitive::fromInt32(i + 1);
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
// double slots are required for accessor properties
|
|
|
|
uint SparseArrayData::allocate(ArrayData *d, bool doubleSlot)
|
2013-12-16 08:16:57 +00:00
|
|
|
{
|
|
|
|
Q_ASSERT(d->type == ArrayData::Sparse);
|
|
|
|
SparseArrayData *dd = static_cast<SparseArrayData *>(d);
|
2014-01-09 10:05:08 +00:00
|
|
|
if (doubleSlot) {
|
|
|
|
uint *last = &dd->freeList;
|
|
|
|
while (1) {
|
|
|
|
if (*last + 1 >= dd->alloc) {
|
|
|
|
reserve(d, d->alloc + 2);
|
|
|
|
last = &dd->freeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dd->data[*last].uint_32 == (*last + 1)) {
|
|
|
|
// found two slots in a row
|
|
|
|
uint idx = *last;
|
|
|
|
*last = dd->data[*last + 1].uint_32;
|
|
|
|
d->attrs[idx] = Attr_Accessor;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
last = &dd->data[*last].uint_32;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dd->alloc == dd->freeList)
|
|
|
|
reserve(d, d->alloc + 2);
|
|
|
|
uint idx = dd->freeList;
|
|
|
|
dd->freeList = dd->data[idx].uint_32;
|
|
|
|
if (dd->attrs)
|
|
|
|
dd->attrs[idx] = Attr_Data;
|
|
|
|
return idx;
|
|
|
|
}
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnedValue SparseArrayData::get(const ArrayData *d, uint index)
|
|
|
|
{
|
|
|
|
SparseArrayNode *n = static_cast<const SparseArrayData *>(d)->sparse->findNode(index);
|
|
|
|
if (!n)
|
|
|
|
return Primitive::emptyValue().asReturnedValue();
|
2014-01-09 10:05:08 +00:00
|
|
|
return d->data[n->value].asReturnedValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SparseArrayData::put(ArrayData *d, uint index, ValueRef value)
|
|
|
|
{
|
2014-01-09 10:05:08 +00:00
|
|
|
if (value->isEmpty())
|
|
|
|
return true;
|
|
|
|
|
2013-12-16 08:16:57 +00:00
|
|
|
SparseArrayNode *n = static_cast<SparseArrayData *>(d)->sparse->insert(index);
|
2014-01-09 10:05:08 +00:00
|
|
|
Q_ASSERT(n->value == UINT_MAX || !d->attrs || !d->attrs[n->value].isAccessor());
|
2013-12-16 08:16:57 +00:00
|
|
|
if (n->value == UINT_MAX)
|
|
|
|
n->value = allocate(d);
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[n->value] = value;
|
|
|
|
if (d->attrs)
|
|
|
|
d->attrs[n->value] = Attr_Data;
|
2013-12-16 08:16:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SparseArrayData::del(ArrayData *d, uint index)
|
|
|
|
{
|
|
|
|
SparseArrayData *dd = static_cast<SparseArrayData *>(d);
|
|
|
|
SparseArrayNode *n = dd->sparse->findNode(index);
|
|
|
|
if (!n)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
uint pidx = n->value;
|
2014-01-09 10:05:08 +00:00
|
|
|
Q_ASSERT(!dd->data[pidx].isEmpty());
|
2013-12-16 08:16:57 +00:00
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
bool isAccessor = false;
|
|
|
|
if (dd->attrs) {
|
|
|
|
if (!dd->attrs[pidx].isConfigurable())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
isAccessor = dd->attrs[pidx].isAccessor();
|
|
|
|
dd->attrs[pidx] = Attr_Data;
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
2014-01-09 10:05:08 +00:00
|
|
|
|
|
|
|
if (isAccessor) {
|
|
|
|
// free up both indices
|
|
|
|
d->data[pidx + 1].tag = Value::Undefined_Type;
|
|
|
|
d->data[pidx + 1].uint_32 = static_cast<SparseArrayData *>(d)->freeList;
|
|
|
|
d->data[pidx].tag = Value::Undefined_Type;
|
|
|
|
d->data[pidx].uint_32 = pidx + 1;
|
|
|
|
} else {
|
|
|
|
d->data[pidx].tag = Value::Undefined_Type;
|
|
|
|
d->data[pidx].uint_32 = static_cast<SparseArrayData *>(d)->freeList;
|
|
|
|
}
|
|
|
|
|
|
|
|
static_cast<SparseArrayData *>(d)->freeList = pidx;
|
|
|
|
static_cast<SparseArrayData *>(d)->sparse->erase(n);
|
|
|
|
return true;
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SparseArrayData::setAttribute(ArrayData *d, uint index, PropertyAttributes attrs)
|
|
|
|
{
|
|
|
|
SparseArrayNode *n = static_cast<SparseArrayData *>(d)->sparse->insert(index);
|
|
|
|
if (n->value == UINT_MAX)
|
2014-01-09 10:05:08 +00:00
|
|
|
n->value = allocate(d, attrs.isAccessor());
|
|
|
|
else if (attrs.isAccessor() != d->attrs[n->value].isAccessor()) {
|
|
|
|
// need to convert the slot
|
|
|
|
free(d, n->value);
|
|
|
|
n->value = allocate(d, attrs.isAccessor());
|
|
|
|
}
|
2013-12-16 08:16:57 +00:00
|
|
|
d->attrs[n->value] = attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyAttributes SparseArrayData::attribute(const ArrayData *d, uint index)
|
|
|
|
{
|
|
|
|
SparseArrayNode *n = static_cast<const SparseArrayData *>(d)->sparse->insert(index);
|
|
|
|
if (!n)
|
|
|
|
return PropertyAttributes();
|
|
|
|
return d->attrs[n->value];
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseArrayData::push_front(ArrayData *d, SafeValue *values, uint n)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!d->attrs);
|
|
|
|
for (int i = n - 1; i >= 0; --i) {
|
|
|
|
uint idx = allocate(d);
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[idx] = values[i];
|
2013-12-16 08:16:57 +00:00
|
|
|
static_cast<SparseArrayData *>(d)->sparse->push_front(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnedValue SparseArrayData::pop_front(ArrayData *d)
|
|
|
|
{
|
|
|
|
Q_ASSERT(!d->attrs);
|
|
|
|
uint idx = static_cast<SparseArrayData *>(d)->sparse->pop_front();
|
|
|
|
ReturnedValue v;
|
|
|
|
if (idx != UINT_MAX) {
|
2014-01-09 10:05:08 +00:00
|
|
|
v = d->data[idx].asReturnedValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
SparseArrayData::free(d, idx);
|
|
|
|
} else {
|
|
|
|
v = Encode::undefined();
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint SparseArrayData::truncate(ArrayData *d, uint newLen)
|
|
|
|
{
|
|
|
|
SparseArrayNode *begin = static_cast<SparseArrayData *>(d)->sparse->lowerBound(newLen);
|
|
|
|
if (begin != static_cast<SparseArrayData *>(d)->sparse->end()) {
|
|
|
|
SparseArrayNode *it = static_cast<SparseArrayData *>(d)->sparse->end()->previousNode();
|
|
|
|
while (1) {
|
|
|
|
if (d->attrs) {
|
|
|
|
if (!d->attrs[it->value].isConfigurable()) {
|
|
|
|
newLen = it->key() + 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 10:05:08 +00:00
|
|
|
free(d, it->value);
|
2013-12-16 08:16:57 +00:00
|
|
|
bool brk = (it == begin);
|
|
|
|
SparseArrayNode *prev = it->previousNode();
|
|
|
|
static_cast<SparseArrayData *>(d)->sparse->erase(it);
|
|
|
|
if (brk)
|
|
|
|
break;
|
|
|
|
it = prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SparseArrayData::putArray(ArrayData *d, uint index, SafeValue *values, uint n)
|
|
|
|
{
|
|
|
|
for (uint i = 0; i < n; ++i)
|
|
|
|
put(d, index + i, values[i]);
|
|
|
|
d->len = qMax(d->len, index + n);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint ArrayData::append(Object *o, const ArrayObject *otherObj, uint n)
|
|
|
|
{
|
|
|
|
ArrayData *d = o->arrayData;
|
|
|
|
if (!n)
|
2014-01-09 10:05:08 +00:00
|
|
|
return o->getLength();
|
2013-12-16 08:16:57 +00:00
|
|
|
|
|
|
|
const ArrayData *other = otherObj->arrayData;
|
|
|
|
|
|
|
|
if (other->isSparse()) {
|
|
|
|
o->initSparseArray();
|
|
|
|
d = o->arrayData;
|
|
|
|
}
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
uint oldSize = o->getLength();
|
2013-12-16 08:16:57 +00:00
|
|
|
|
|
|
|
// ### copy attributes as well!
|
|
|
|
if (d->type == ArrayData::Sparse) {
|
|
|
|
if (other->isSparse()) {
|
2014-01-09 10:05:08 +00:00
|
|
|
if (otherObj->hasAccessorProperty && other->hasAttributes()) {
|
|
|
|
for (const SparseArrayNode *it = static_cast<const SparseArrayData *>(other)->sparse->begin();
|
|
|
|
it != static_cast<const SparseArrayData *>(other)->sparse->end(); it = it->nextNode())
|
|
|
|
o->arraySet(oldSize + it->key(), *reinterpret_cast<Property *>(other->data + it->value), other->attrs[it->value]);
|
|
|
|
} else {
|
|
|
|
for (const SparseArrayNode *it = static_cast<const SparseArrayData *>(other)->sparse->begin();
|
|
|
|
it != static_cast<const SparseArrayData *>(other)->sparse->end(); it = it->nextNode())
|
|
|
|
o->arraySet(oldSize + it->key(), other->data[it->value]);
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
2014-01-09 10:05:08 +00:00
|
|
|
} else {
|
|
|
|
d->put(oldSize, other->data, n);
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
} else if (other->length()) {
|
|
|
|
d->vtable->reserve(d, oldSize + other->length());
|
|
|
|
if (oldSize > d->len) {
|
|
|
|
for (uint i = d->len; i < oldSize; ++i)
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[i] = Primitive::emptyValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
if (other->attrs) {
|
|
|
|
for (uint i = 0; i < other->len; ++i) {
|
|
|
|
bool exists;
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[oldSize + i] = const_cast<ArrayObject *>(otherObj)->getIndexed(i, &exists);
|
2013-12-16 08:16:57 +00:00
|
|
|
d->len = oldSize + i + 1;
|
|
|
|
o->arrayData->setAttributes(oldSize + i, Attr_Data);
|
|
|
|
if (!exists)
|
2014-01-09 10:05:08 +00:00
|
|
|
d->data[oldSize + i] = Primitive::emptyValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
d->len = oldSize + other->len;
|
|
|
|
memcpy(d->data + oldSize, other->data, other->len*sizeof(Property));
|
|
|
|
if (d->attrs)
|
|
|
|
std::fill(d->attrs + oldSize, d->attrs + oldSize + other->len, PropertyAttributes(Attr_Data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldSize + n;
|
|
|
|
}
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
Property *ArrayData::insert(Object *o, uint index, bool isAccessor)
|
2013-12-16 08:16:57 +00:00
|
|
|
{
|
|
|
|
Property *pd;
|
|
|
|
if (o->arrayData->type != ArrayData::Sparse && (index < 0x1000 || index < o->arrayData->len + (o->arrayData->len >> 2))) {
|
2014-01-09 10:05:08 +00:00
|
|
|
Q_ASSERT(!isAccessor);
|
2013-12-16 08:16:57 +00:00
|
|
|
if (index >= o->arrayData->alloc)
|
|
|
|
o->arrayReserve(index + 1);
|
|
|
|
if (index >= o->arrayData->len) {
|
|
|
|
// mark possible hole in the array
|
|
|
|
for (uint i = o->arrayData->len; i < index; ++i)
|
2014-01-09 10:05:08 +00:00
|
|
|
o->arrayData->data[i] = Primitive::emptyValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
o->arrayData->len = index + 1;
|
|
|
|
}
|
2014-01-09 10:05:08 +00:00
|
|
|
pd = reinterpret_cast<Property *>(o->arrayData->data + index);
|
2013-12-16 08:16:57 +00:00
|
|
|
} else {
|
|
|
|
o->initSparseArray();
|
|
|
|
SparseArrayNode *n = static_cast<SparseArrayData *>(o->arrayData)->sparse->insert(index);
|
|
|
|
if (n->value == UINT_MAX)
|
2014-01-09 10:05:08 +00:00
|
|
|
n->value = SparseArrayData::allocate(o->arrayData, isAccessor);
|
|
|
|
pd = reinterpret_cast<Property *>(o->arrayData->data + n->value);
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
return pd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayData::markObjects(ExecutionEngine *e)
|
|
|
|
{
|
2014-01-09 10:05:08 +00:00
|
|
|
for (uint i = 0; i < len; ++i)
|
|
|
|
data[i].mark(e);
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayData::sort(ExecutionContext *context, ObjectRef thisObject, const ValueRef comparefn, uint len)
|
|
|
|
{
|
2014-01-09 10:05:08 +00:00
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
2013-12-16 08:16:57 +00:00
|
|
|
ArrayData *d = thisObject->arrayData;
|
2014-01-09 10:05:08 +00:00
|
|
|
if (!d || (!d->len && d->type != ArrayData::Sparse))
|
2013-12-16 08:16:57 +00:00
|
|
|
return;
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
if (!(comparefn->isUndefined() || comparefn->asObject())) {
|
|
|
|
context->throwTypeError();
|
2013-12-16 08:16:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The spec says the sorting goes through a series of get,put and delete operations.
|
|
|
|
// this implies that the attributes don't get sorted around.
|
2014-01-09 10:05:08 +00:00
|
|
|
|
|
|
|
if (d->type == ArrayData::Sparse) {
|
|
|
|
// since we sort anyway, we can simply iterate over the entries in the sparse
|
|
|
|
// array and append them one by one to a regular one.
|
|
|
|
SparseArrayData *sparse = static_cast<SparseArrayData *>(d);
|
|
|
|
|
|
|
|
if (!sparse->sparse->nEntries())
|
|
|
|
return;
|
|
|
|
|
|
|
|
thisObject->arrayData = new ArrayData;
|
|
|
|
d = thisObject->arrayData;
|
|
|
|
d->vtable->reserve(d, sparse->sparse->nEntries());
|
|
|
|
|
|
|
|
SparseArrayNode *n = sparse->sparse->begin();
|
|
|
|
uint i = 0;
|
|
|
|
if (sparse->attrs) {
|
|
|
|
d->ensureAttributes();
|
|
|
|
while (n != sparse->sparse->end()) {
|
|
|
|
if (n->value >= len)
|
|
|
|
break;
|
|
|
|
|
|
|
|
PropertyAttributes a = sparse->attrs ? sparse->attrs[n->value] : Attr_Data;
|
|
|
|
d->data[i] = thisObject->getValue(reinterpret_cast<Property *>(sparse->data + n->value), a);
|
|
|
|
d->attrs[i] = a.isAccessor() ? Attr_Data : a;
|
|
|
|
|
|
|
|
n = n->nextNode();
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (n != sparse->sparse->end()) {
|
|
|
|
if (n->value >= len)
|
|
|
|
break;
|
|
|
|
d->data[i] = sparse->data[n->value];
|
|
|
|
n = n->nextNode();
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d->len = i;
|
|
|
|
if (len > i)
|
|
|
|
len = i;
|
|
|
|
if (n != sparse->sparse->end()) {
|
|
|
|
// have some entries outside the sort range that we need to ignore when sorting
|
|
|
|
thisObject->initSparseArray();
|
|
|
|
d = thisObject->arrayData;
|
|
|
|
while (n != sparse->sparse->end()) {
|
|
|
|
PropertyAttributes a = sparse->attrs ? sparse->attrs[n->value] : Attr_Data;
|
|
|
|
thisObject->arraySet(n->value, *reinterpret_cast<Property *>(sparse->data + n->value), a);
|
|
|
|
|
|
|
|
n = n->nextNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sparse->ArrayData::free();
|
|
|
|
} else {
|
|
|
|
if (len > d->len)
|
|
|
|
len = d->len;
|
|
|
|
|
|
|
|
// sort empty values to the end
|
2013-12-16 08:16:57 +00:00
|
|
|
for (uint i = 0; i < len; i++) {
|
2014-01-09 10:05:08 +00:00
|
|
|
if (d->data[i].isEmpty()) {
|
2013-12-16 08:16:57 +00:00
|
|
|
while (--len > i)
|
2014-01-09 10:05:08 +00:00
|
|
|
if (!d->data[len].isEmpty())
|
2013-12-16 08:16:57 +00:00
|
|
|
break;
|
2014-01-09 10:05:08 +00:00
|
|
|
Q_ASSERT(!d->attrs || !d->attrs[len].isAccessor());
|
|
|
|
d->data[i] = d->data[len];
|
|
|
|
d->data[len] = Primitive::emptyValue();
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
if (!len)
|
|
|
|
return;
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
|
2013-12-16 08:16:57 +00:00
|
|
|
ArrayElementLessThan lessThan(context, thisObject, comparefn);
|
|
|
|
|
2014-01-09 10:05:08 +00:00
|
|
|
SafeValue *begin = d->data;
|
2013-12-16 08:16:57 +00:00
|
|
|
std::sort(begin, begin + len, lessThan);
|
2014-01-09 10:05:08 +00:00
|
|
|
|
|
|
|
#ifdef CHECK_SPARSE_ARRAYS
|
|
|
|
thisObject->initSparseArray();
|
|
|
|
#endif
|
|
|
|
|
2013-12-16 08:16:57 +00:00
|
|
|
}
|