59void ArrayData::realloc(Object *o, Type newType, uint requested,
bool enforceAttributes)
61 Scope scope(o->engine());
62 Scoped<ArrayData> d(scope, o->arrayData());
69 bool hasAttrs = d->attrs();
70 enforceAttributes |= hasAttrs;
72 if (requested <= d->alloc() && newType == d->type() && hasAttrs == enforceAttributes)
74 if (alloc < d->alloc())
77 if (d->type() < Heap::ArrayData::Sparse) {
78 offset = d->d()->offset;
79 toCopy = d->d()->values.size;
83 if (d->type() > newType)
87 while (alloc < requested)
89 size_t size =
sizeof(Heap::ArrayData) + (alloc - 1)*
sizeof(Value);
90 if (enforceAttributes)
91 size += alloc*
sizeof(PropertyAttributes);
93 Scoped<ArrayData> newData(scope);
94 if (newType < Heap::ArrayData::Sparse) {
95 Heap::SimpleArrayData *n = scope.engine->memoryManager->allocManaged<SimpleArrayData>(size);
98 n->values.size = d ? d->d()->values.size : 0;
101 Heap::SparseArrayData *n = scope.engine->memoryManager->allocManaged<SparseArrayData>(size);
105 newData->setAlloc(alloc);
106 newData->setType(newType);
107 newData->setAttrs(enforceAttributes ?
reinterpret_cast<PropertyAttributes *>(newData->d()->values.values + alloc) :
nullptr);
108 o->setArrayData(newData);
111 if (enforceAttributes) {
113 memcpy(newData->attrs(), d->attrs(),
sizeof(PropertyAttributes)*toCopy);
115 for (uint i = 0; i < toCopy; ++i)
116 newData->attrs()[i] = Attr_Data;
119 if (toCopy > d->d()->values.alloc - offset) {
120 uint copyFromStart = toCopy - (d->d()->values.alloc - offset);
122 memcpy(newData->d()->values.values + toCopy - copyFromStart, d->d()->values.values,
sizeof(Value)*copyFromStart);
123 toCopy -= copyFromStart;
126 memcpy(newData->d()->values.values, d->d()->values.values + offset,
sizeof(Value)*toCopy);
129 if (newType != Heap::ArrayData::Sparse)
132 Heap::SparseArrayData *sparse =
static_cast<Heap::SparseArrayData *>(newData->d());
135 if (d && d->type() == Heap::ArrayData::Sparse) {
136 Heap::SparseArrayData *old =
static_cast<Heap::SparseArrayData *>(d->d());
137 sparse->sparse = old->sparse;
138 old->sparse =
nullptr;
139 lastFree = &sparse->sparse->freeList;
141 sparse->sparse =
new SparseArray;
142 lastFree = &sparse->sparse->freeList;
143 *lastFree = Encode(0);
144 for (uint i = 0; i < toCopy; ++i) {
145 if (!sparse->values[i].isEmpty()) {
146 SparseArrayNode *n = sparse->sparse->insert(i);
149 *lastFree = Encode(i);
150 sparse->values.values[i].setEmpty();
151 lastFree = &sparse->values.values[i];
156 if (toCopy < sparse->values.alloc) {
157 for (uint i = toCopy; i < sparse->values.alloc; ++i) {
158 *lastFree = Encode(i);
159 sparse->values.values[i].setEmpty();
160 lastFree = &sparse->values.values[i];
163 *lastFree = Encode(-1);
165 Q_ASSERT(sparse->sparse->freeList.isInteger());
191bool SimpleArrayData::put(Object *o, uint index,
const Value &value)
193 Heap::SimpleArrayData *dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
194 Q_ASSERT(index >= dd->values.size || !dd->attrs || !dd->attrs[index].isAccessor());
196 dd->setData(o->engine(), index, value);
197 if (index >= dd->values.size) {
199 dd->attrs[index] = Attr_Data;
200 dd->values.size = index + 1;
205bool SimpleArrayData::del(Object *o, uint index)
207 Heap::SimpleArrayData *dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
208 if (index >= dd->values.size)
211 if (!dd->attrs || dd->attrs[index].isConfigurable()) {
212 dd->setData(o->engine(), index, Value::emptyValue());
214 dd->attrs[index] = Attr_Data;
217 if (dd->data(index).isEmpty())
227void SimpleArrayData::push_front(Object *o,
const Value *values, uint n)
229 Heap::SimpleArrayData *dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
230 Q_ASSERT(!dd->attrs);
231 if (dd->values.size + n > dd->values.alloc) {
232 realloc(o, Heap::ArrayData::Simple, dd->values.size + n,
false);
233 Q_ASSERT(o->d()->arrayData->type == Heap::ArrayData::Simple);
234 dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
236 if (n <= dd->offset) {
240 dd->offset = dd->values.alloc -
243 dd->values.size += n;
244 for (uint i = 0; i < n; ++i)
245 dd->setData(o->engine(), i, values[i]);
261uint SimpleArrayData::truncate(Object *o, uint newLen)
263 Heap::SimpleArrayData *dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
264 if (dd->values.size < newLen)
268 for (uint i = newLen; i < dd->values.size; ++i)
269 dd->setData(dd->internalClass->engine, i, Value::emptyValue());
270 dd->values.size = newLen;
274 while (dd->values.size > newLen) {
275 const uint lastIndex = dd->values.size - 1;
276 if (!dd->data(lastIndex).isEmpty() && !dd->attrs[lastIndex].isConfigurable())
277 return dd->values.size;
279 dd->setData(dd->internalClass->engine, lastIndex, Value::emptyValue());
280 dd->values.size = lastIndex;
282 return dd->values.size;
290bool SimpleArrayData::putArray(Object *o, uint index,
const Value *values, uint n)
292 Heap::SimpleArrayData *dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
293 if (index + n > dd->values.alloc) {
294 reallocate(o, index + n + 1,
false);
295 dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
297 QV4::ExecutionEngine *e = o->engine();
298 for (uint i = dd->values.size; i < index; ++i)
299 dd->setData(e, i, Value::emptyValue());
300 for (uint i = 0; i < n; ++i)
301 dd->setData(e, index + i, values[i]);
302 dd->values.size = qMax(dd->values.size, index + n);
329uint SparseArrayData::allocate(Object *o,
bool doubleSlot)
331 Q_ASSERT(o->d()->arrayData->type == Heap::ArrayData::Sparse);
332 Heap::SimpleArrayData *dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
334 Value *last = &dd->sparse->freeList;
336 if (last->int_32() == -1) {
337 reallocate(o, dd->values.alloc + 2,
true);
338 dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
339 last = &dd->sparse->freeList;
340 Q_ASSERT(last->int_32() != -1);
343 Q_ASSERT(dd->values[
static_cast<uint>(last->int_32())].int_32() != last->int_32());
344 if (dd->values[
static_cast<uint>(last->int_32())].int_32() == last->int_32() + 1) {
346 uint idx =
static_cast<uint>(last->int_32());
347 *last = Encode(dd->values[
static_cast<uint>(last->int_32()) + 1].int_32());
348 dd->attrs[idx] = Attr_Accessor;
351 last = &dd->values.values[last->int_32()];
354 if (dd->sparse->freeList.int_32() == -1) {
355 reallocate(o, dd->values.alloc + 1,
false);
356 dd = o->d()->arrayData.cast<Heap::SimpleArrayData>();
358 Q_ASSERT(dd->sparse->freeList.int_32() != -1);
359 uint idx =
static_cast<uint>(dd->sparse->freeList.int_32());
360 dd->sparse->freeList = dd->values[idx];
361 Q_ASSERT(dd->sparse->freeList.isInteger());
363 dd->attrs[idx] = Attr_Data;
377bool SparseArrayData::put(Object *o, uint index,
const Value &value)
382 Heap::SparseArrayData *s = o->d()->arrayData.cast<Heap::SparseArrayData>();
383 SparseArrayNode *n = s->sparse->insert(index);
384 Q_ASSERT(n->value == UINT_MAX || !s->attrs || !s->attrs[n->value].isAccessor());
385 if (n->value == UINT_MAX)
386 n->value = allocate(o);
387 s = o->d()->arrayData.cast<Heap::SparseArrayData>();
388 s->setArrayData(o->engine(), n->value, value);
390 s->attrs[n->value] = Attr_Data;
394bool SparseArrayData::del(Object *o, uint index)
396 Heap::SparseArrayData *dd = o->d()->arrayData.cast<Heap::SparseArrayData>();
398 SparseArrayNode *n = dd->sparse->findNode(index);
402 uint pidx = n->value;
403 Q_ASSERT(!dd->values[pidx].isEmpty());
405 bool isAccessor =
false;
407 if (!dd->attrs[pidx].isConfigurable())
410 isAccessor = dd->attrs[pidx].isAccessor();
411 dd->attrs[pidx] = Attr_Data;
416 dd->values.values[pidx + 1] = dd->sparse->freeList;
417 dd->values.values[pidx] = Encode(pidx + 1);
419 Q_ASSERT(dd->type == Heap::ArrayData::Sparse);
420 dd->values.values[pidx] = dd->sparse->freeList;
423 dd->sparse->freeList = Encode(pidx);
424 dd->sparse->erase(n);
428void SparseArrayData::setAttribute(Object *o, uint index, PropertyAttributes attrs)
430 Heap::SparseArrayData *d = o->d()->arrayData.cast<Heap::SparseArrayData>();
431 SparseArrayNode *n = d->sparse->insert(index);
432 if (n->value == UINT_MAX) {
433 n->value = allocate(o, attrs.isAccessor());
434 d = o->d()->arrayData.cast<Heap::SparseArrayData>();
436 else if (attrs.isAccessor() != d->attrs[n->value].isAccessor()) {
438 free(o->arrayData(), n->value);
439 n->value = allocate(o, attrs.isAccessor());
440 d = o->d()->arrayData.cast<Heap::SparseArrayData>();
442 d->attrs[n->value] = attrs;
472uint SparseArrayData::truncate(Object *o, uint newLen)
474 Heap::SparseArrayData *d = o->d()->arrayData.cast<Heap::SparseArrayData>();
475 SparseArrayNode *begin = d->sparse->lowerBound(newLen);
476 if (begin != d->sparse->end()) {
477 SparseArrayNode *it = d->sparse->end()->previousNode();
480 if (!d->attrs[it->value].isConfigurable()) {
481 newLen = it->key() + 1;
485 free(o->arrayData(), it->value);
486 bool brk = (it == begin);
487 SparseArrayNode *prev = it->previousNode();
488 d->sparse->erase(it);
515uint ArrayData::append(Object *obj, ArrayObject *otherObj, uint n)
517 Q_ASSERT(!obj->d()->arrayData || !obj->d()->arrayData->attrs);
520 return obj->getLength();
522 Scope scope(obj->engine());
523 Scoped<ArrayData> other(scope, otherObj->arrayData());
525 if (other && other->isSparse())
526 obj->initSparseArray();
530 uint oldSize = obj->getLength();
532 if (!other || ArgumentsObject::isNonStrictArgumentsObject(otherObj)) {
533 ScopedValue v(scope);
534 for (uint i = 0; i < n; ++i)
535 obj->arraySet(oldSize + i, (v = otherObj->get(i)));
536 }
else if (other->isSparse()) {
537 Heap::SparseArrayData *os =
static_cast<Heap::SparseArrayData *>(other->d());
538 if (other->hasAttributes()) {
539 ScopedValue v(scope);
540 for (
const SparseArrayNode *it = os->sparse->begin();
541 it != os->sparse->end(); it = it->nextNode()) {
542 v = otherObj->getValue(os->values[it->value], other->d()->attrs[it->value]);
543 obj->arraySet(oldSize + it->key(), v);
546 for (
const SparseArrayNode *it = other->d()->sparse->begin();
547 it != os->sparse->end(); it = it->nextNode())
548 obj->arraySet(oldSize + it->key(), os->values[it->value]);
551 Heap::SimpleArrayData *os =
static_cast<Heap::SimpleArrayData *>(other->d());
554 if (chunk > os->values.alloc - os->offset)
555 chunk = os->values.alloc - os->offset;
556 obj->arrayPut(oldSize, os->values.data() + os->offset, chunk);
559 obj->arrayPut(oldSize + chunk, os->values.data(), toCopy);
565void ArrayData::insert(Object *o, uint index,
const Value *v,
bool isAccessor)
567 if (!isAccessor && o->d()->arrayData->type != Heap::ArrayData::Sparse) {
568 Heap::SimpleArrayData *d = o->d()->arrayData.cast<Heap::SimpleArrayData>();
569 if (index < 0x1000 || index < d->values.size + (d->values.size >> 2)) {
570 if (index >= d->values.alloc) {
571 o->arrayReserve(index + 1);
572 d = o->d()->arrayData.cast<Heap::SimpleArrayData>();
574 if (index >= d->values.size) {
576 for (uint i = d->values.size; i < index; ++i)
577 d->setData(o->engine(), i, Value::emptyValue());
578 d->values.size = index + 1;
580 d->setData(o->engine(), index, *v);
585 o->initSparseArray();
586 Heap::SparseArrayData *s = o->d()->arrayData.cast<Heap::SparseArrayData>();
587 SparseArrayNode *n = s->sparse->insert(index);
588 if (n->value == UINT_MAX)
589 n->value = SparseArrayData::allocate(o, isAccessor);
590 s = o->d()->arrayData.cast<Heap::SparseArrayData>();
591 s->setArrayData(o->engine(), n->value, *v);
593 s->setArrayData(o->engine(), n->value + Object::SetterOffset, v[Object::SetterOffset]);
596bool ArrayElementLessThan::operator()(Value v1, Value v2)
const
598 Scope scope(m_engine);
600 if (v1.isUndefined() || v1.isEmpty())
602 if (v2.isUndefined() || v2.isEmpty())
604 ScopedFunctionObject o(scope, m_comparefn);
606 Scope scope(o->engine());
607 ScopedValue result(scope);
608 JSCallArguments jsCallData(scope, 2);
609 jsCallData.args[0] = v1;
610 jsCallData.args[1] = v2;
611 result = o->call(jsCallData);
612 if (scope.hasException())
615 return result->toNumber() < 0;
617 ScopedString p1s(scope, v1.toString(scope.engine));
618 ScopedString p2s(scope, v2.toString(scope.engine));
625 return p1s->toQString() < p2s->toQString();
628void ArrayData::sort(ExecutionEngine *engine, Object *thisObject,
const Value &comparefn, uint len)
634 Scoped<ArrayData> arrayData(scope, thisObject->arrayData());
636 if (!arrayData || !arrayData->length())
642 if (arrayData->type() == Heap::ArrayData::Sparse) {
645 Scoped<SparseArrayData> sparse(scope,
static_cast<Heap::SparseArrayData *>(arrayData->d()));
647 if (!sparse->sparse()->nEntries())
650 thisObject->setArrayData(
nullptr);
651 ArrayData::realloc(thisObject, Heap::ArrayData::Simple, sparse->sparse()->nEntries(), sparse->attrs() ?
true :
false);
652 Heap::SimpleArrayData *d = thisObject->d()->arrayData.cast<Heap::SimpleArrayData>();
654 SparseArrayNode *n = sparse->sparse()->begin();
656 if (sparse->attrs()) {
657 while (n != sparse->sparse()->end()) {
661 PropertyAttributes a = sparse->attrs() ? sparse->attrs()[n->value] : Attr_Data;
662 d->setData(engine, i, Value::fromReturnedValue(thisObject->getValue(sparse->arrayData()[n->value], a)));
663 d->attrs[i] = a.isAccessor() ? Attr_Data : a;
669 while (n != sparse->sparse()->end()) {
672 d->setData(engine, i, sparse->arrayData()[n->value]);
680 if (n != sparse->sparse()->end()) {
682 thisObject->initSparseArray();
683 while (n != sparse->sparse()->end()) {
684 PropertyAttributes a = sparse->attrs() ? sparse->attrs()[n->value] : Attr_Data;
685 thisObject->arraySet(n->value,
reinterpret_cast<
const Property *>(sparse->arrayData() + n->value), a);
692 Heap::SimpleArrayData *d = thisObject->d()->arrayData.cast<Heap::SimpleArrayData>();
693 if (len > d->values.size)
694 len = d->values.size;
697 for (uint i = 0; i < len; i++) {
698 if (d->data(i).isEmpty()) {
700 if (!d->data(len).isEmpty())
702 Q_ASSERT(!d->attrs || !d->attrs[len].isAccessor());
703 d->setData(engine, i, d->data(len));
704 d->setData(engine, len, Value::emptyValue());
713 ArrayElementLessThan lessThan(engine, comparefn);
715 const auto thisArrayData = thisObject->arrayData();
716 uint startIndex = thisArrayData->mappedIndex(0);
717 uint endIndex = thisArrayData->mappedIndex(len - 1) + 1;
718 if (startIndex < endIndex) {
721 thisArrayData->values.values + startIndex,
722 thisArrayData->values.values + endIndex,
731 const uint gap = startIndex - endIndex;
732 const uint allocEnd = thisArrayData->values.alloc - 1;
733 for (uint i = 0; i < gap; ++i) {
734 const uint from = allocEnd - i;
735 const uint to = endIndex + i;
736 if (from < startIndex)
739 std::swap(thisArrayData->values.values[from], thisArrayData->values.values[to]);
742 thisArrayData->offset = 0;
743 sortHelper(thisArrayData->values.values, thisArrayData->values.values + len, lessThan);
746#ifdef CHECK_SPARSE_ARRAYS
747 thisObject->initSparseArray();