diff options
author | Robin Burchell <robin.burchell@crimson.no> | 2018-06-01 01:27:47 +0200 |
---|---|---|
committer | Robin Burchell <robin.burchell@crimson.no> | 2018-06-27 16:13:42 +0000 |
commit | 3cda7df8ac8fc6f9a5562e89a4b047449dc3b2bc (patch) | |
tree | 199ab0e72318b88b64f9833c6fffd12a94b5db3d | |
parent | b4d31c9ff5f0c5821ea127c663532d9fc2cae43e (diff) |
Map/Set: Introduce QV4::ESTable
This removes the duplication of code between Map and Set by placing it
in a shared location, and will hopefully be a touch more efficient than
using ArrayObject.
In a followup patch, it will get faster, too.
Note: As a bonus, this also fixed a few more test failures: forEach
wasn't handling the object being changed during iteration.
Task-number: QTBUG-68545
Change-Id: I8bf6f9c5b2de030a02ce27a23b8c1da431ffeda4
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r-- | src/qml/jsruntime/jsruntime.pri | 6 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4estable.cpp | 181 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4estable_p.h | 87 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4mapiterator.cpp | 22 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4mapobject.cpp | 139 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4mapobject_p.h | 15 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4setiterator.cpp | 20 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4setobject.cpp | 111 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4setobject_p.h | 14 | ||||
-rw-r--r-- | tests/auto/qml/ecmascripttests/TestExpectations | 2 |
10 files changed, 370 insertions, 227 deletions
diff --git a/src/qml/jsruntime/jsruntime.pri b/src/qml/jsruntime/jsruntime.pri index 34eea36f0a..8b8732590f 100644 --- a/src/qml/jsruntime/jsruntime.pri +++ b/src/qml/jsruntime/jsruntime.pri @@ -52,7 +52,8 @@ SOURCES += \ $$PWD/qv4dataview.cpp \ $$PWD/qv4vme_moth.cpp \ $$PWD/qv4mapobject.cpp \ - $$PWD/qv4mapiterator.cpp + $$PWD/qv4mapiterator.cpp \ + $$PWD/qv4estable.cpp qtConfig(qml-debug): SOURCES += $$PWD/qv4profiling.cpp @@ -116,7 +117,8 @@ HEADERS += \ $$PWD/qv4dataview_p.h \ $$PWD/qv4vme_moth_p.h \ $$PWD/qv4mapobject_p.h \ - $$PWD/qv4mapiterator_p.h + $$PWD/qv4mapiterator_p.h \ + $$PWD/qv4estable_p.h qtConfig(qml-sequence-object) { HEADERS += \ diff --git a/src/qml/jsruntime/qv4estable.cpp b/src/qml/jsruntime/qv4estable.cpp new file mode 100644 index 0000000000..55b7407000 --- /dev/null +++ b/src/qml/jsruntime/qv4estable.cpp @@ -0,0 +1,181 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Crimson AS <info@crimson.no> +** Contact: https://www.qt.io/licensing/ +** +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qv4estable_p.h" + +using namespace QV4; + +// The ES spec requires that Map/Set be implemented using a data structure that +// is a little different from most; it requires nonlinear access, and must also +// preserve the order of insertion of items in a deterministic way. +// +// This class implements those requirements, except for fast access: that +// will be addressed in a followup patch. + +ESTable::ESTable() + : m_capacity(8) +{ + m_keys = (Value*)malloc(m_capacity * sizeof(Value)); + m_values = (Value*)malloc(m_capacity * sizeof(Value)); + memset(m_keys, 0, m_capacity); + memset(m_values, 0, m_capacity); +} + +ESTable::~ESTable() +{ + free(m_keys); + free(m_values); + m_size = 0; + m_capacity = 0; + m_keys = nullptr; + m_values = nullptr; +} + +void ESTable::markObjects(MarkStack *s) +{ + for (uint i = 0; i < m_size; ++i) { + m_keys[i].mark(s); + m_values[i].mark(s); + } +} + +// Pretends that there's nothing in the table. Doesn't actually free memory, as +// it will almost certainly be reused again anyway. +void ESTable::clear() +{ + m_size = 0; +} + +// Update the table to contain \a value for a given \a key. The key is +// normalized, as required by the ES spec. +void ESTable::set(const Value &key, const Value &value) +{ + for (uint i = 0; i < m_size; ++i) { + if (m_keys[i].sameValueZero(key)) { + m_values[i] = value; + return; + } + } + + if (m_capacity == m_size) { + uint oldCap = m_capacity; + m_capacity *= 2; + m_keys = (Value*)realloc(m_keys, m_capacity * sizeof(Value)); + m_values = (Value*)realloc(m_values, m_capacity * sizeof(Value)); + memset(m_keys + oldCap, 0, m_capacity - oldCap); + memset(m_values + oldCap, 0, m_capacity - oldCap); + } + + Value nk = key; + if (nk.isDouble()) { + if (nk.doubleValue() == 0 && std::signbit(nk.doubleValue())) + nk = Primitive::fromDouble(+0); + } + + m_keys[m_size] = nk; + m_values[m_size] = value; + + m_size++; +} + +// Returns true if the table contains \a key, false otherwise. +bool ESTable::has(const Value &key) const +{ + for (uint i = 0; i < m_size; ++i) { + if (m_keys[i].sameValueZero(key)) + return true; + } + + return false; +} + +// Fetches the value for the given \a key, and if \a hasValue is passed in, +// it is set depending on whether or not the given key was found. +ReturnedValue ESTable::get(const Value &key, bool *hasValue) const +{ + for (uint i = 0; i < m_size; ++i) { + if (m_keys[i].sameValueZero(key)) { + if (hasValue) + *hasValue = true; + return m_values[i].asReturnedValue(); + } + } + + if (hasValue) + *hasValue = false; + return Encode::undefined(); +} + +// Removes the given \a key from the table +bool ESTable::remove(const Value &key) +{ + bool found = false; + uint idx = 0; + for (; idx < m_size; ++idx) { + if (m_keys[idx].sameValueZero(key)) { + found = true; + break; + } + } + + if (found == true) { + memmove(m_keys + idx, m_keys + idx + 1, m_size - idx); + memmove(m_values + idx, m_values + idx + 1, m_size - idx); + m_size--; + } + return found; +} + +// Returns the size of the table. Note that the size may not match the underlying allocation. +uint ESTable::size() const +{ + return m_size; +} + +// Retrieves a key and value for a given \a idx, and places them in \a key and +// \a value. They must be valid pointers. +void ESTable::iterate(uint idx, Value *key, Value *value) +{ + Q_ASSERT(idx < m_size); + Q_ASSERT(key); + Q_ASSERT(value); + *key = m_keys[idx]; + *value = m_values[idx]; +} + diff --git a/src/qml/jsruntime/qv4estable_p.h b/src/qml/jsruntime/qv4estable_p.h new file mode 100644 index 0000000000..c665467760 --- /dev/null +++ b/src/qml/jsruntime/qv4estable_p.h @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Crimson AS <info@crimson.no> +** Contact: https://www.qt.io/licensing/ +** +** 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 The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/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 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#ifndef QV4ESTABLE_P_H +#define QV4ESTABLE_P_H + +#include "qv4value_p.h" + +QT_BEGIN_NAMESPACE + +namespace QV4 +{ + +class ESTable +{ +public: + ESTable(); + ~ESTable(); + + void markObjects(MarkStack *s); + void clear(); + void set(const Value &k, const Value &v); + bool has(const Value &k) const; + ReturnedValue get(const Value &k, bool *hasValue = nullptr) const; + bool remove(const Value &k); + uint size() const; + void iterate(uint idx, Value *k, Value *v); + +private: + Value *m_keys = nullptr; + Value *m_values = nullptr; + uint m_size = 0; + uint m_capacity = 0; +}; + +} + +QT_END_NAMESPACE + +#endif diff --git a/src/qml/jsruntime/qv4mapiterator.cpp b/src/qml/jsruntime/qv4mapiterator.cpp index 74b0dda791..7be7416e4a 100644 --- a/src/qml/jsruntime/qv4mapiterator.cpp +++ b/src/qml/jsruntime/qv4mapiterator.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include <private/qv4iterator_p.h> +#include <private/qv4estable_p.h> #include <private/qv4mapiterator_p.h> #include <private/qv4mapobject_p.h> #include <private/qv4symbol_p.h> @@ -63,7 +64,7 @@ ReturnedValue MapIteratorPrototype::method_next(const FunctionObject *b, const V return scope.engine->throwTypeError(QLatin1String("Not a Map Iterator instance")); Scoped<MapObject> s(scope, thisObject->d()->iteratedMap); - quint32 index = thisObject->d()->mapNextIndex; + uint index = thisObject->d()->mapNextIndex; IteratorKind itemKind = thisObject->d()->iterationKind; if (!s) { @@ -71,21 +72,18 @@ ReturnedValue MapIteratorPrototype::method_next(const FunctionObject *b, const V return IteratorPrototype::createIterResultObject(scope.engine, undefined, true); } - Scoped<ArrayObject> keys(scope, s->d()->mapKeys); - Scoped<ArrayObject> values(scope, s->d()->mapValues); + Value *arguments = scope.alloc(2); - while (index < keys->getLength()) { - ScopedValue sk(scope, keys->getIndexed(index)); - ScopedValue sv(scope, values->getIndexed(index)); - index += 1; - thisObject->d()->mapNextIndex = index; + while (index < s->d()->esTable->size()) { + s->d()->esTable->iterate(index, &arguments[0], &arguments[1]); + thisObject->d()->mapNextIndex = index + 1; ScopedValue result(scope); if (itemKind == KeyIteratorKind) { - result = sk; + result = arguments[0]; } else if (itemKind == ValueIteratorKind) { - result = sv; + result = arguments[1]; } else { Q_ASSERT(itemKind == KeyValueIteratorKind); @@ -93,8 +91,8 @@ ReturnedValue MapIteratorPrototype::method_next(const FunctionObject *b, const V Scoped<ArrayObject> resultArray(scope, result); resultArray->arrayReserve(2); - resultArray->arrayPut(0, sk); - resultArray->arrayPut(1, sv); + resultArray->arrayPut(0, arguments[0]); + resultArray->arrayPut(1, arguments[1]); resultArray->setArrayLengthUnchecked(2); } diff --git a/src/qml/jsruntime/qv4mapobject.cpp b/src/qml/jsruntime/qv4mapobject.cpp index e3ca75b951..a311e92402 100644 --- a/src/qml/jsruntime/qv4mapobject.cpp +++ b/src/qml/jsruntime/qv4mapobject.cpp @@ -40,6 +40,7 @@ #include "qv4setobject_p.h" // ### temporary #include "qv4mapobject_p.h" #include "qv4mapiterator_p.h" +#include "qv4estable_p.h" #include "qv4symbol_p.h" using namespace QV4; @@ -56,8 +57,6 @@ ReturnedValue MapCtor::callAsConstructor(const FunctionObject *f, const Value *a { Scope scope(f); Scoped<MapObject> a(scope, scope.engine->memoryManager->allocate<MapObject>()); - a->d()->mapKeys.set(scope.engine, scope.engine->newArrayObject()); - a->d()->mapValues.set(scope.engine, scope.engine->newArrayObject()); if (argc > 0) { ScopedValue iterable(scope, argv[0]); @@ -71,27 +70,22 @@ ReturnedValue MapCtor::callAsConstructor(const FunctionObject *f, const Value *a Scoped<SetObject> setObjectCheck(scope, argv[0]); if (!iterable->isUndefined() && !iterable->isNull() && (mapObjectCheck || setObjectCheck)) { - - ScopedFunctionObject adder(scope, a->get(ScopedString(scope, scope.engine->newString(QString::fromLatin1("set"))))); - if (!adder) { + if (!adder) return scope.engine->throwTypeError(); - } ScopedObject iter(scope, Runtime::method_getIterator(scope.engine, iterable, true)); CHECK_EXCEPTION(); - if (!iter) { + if (!iter) return a.asReturnedValue(); - } Value *nextValue = scope.alloc(1); ScopedValue done(scope); forever { done = Runtime::method_iteratorNext(scope.engine, iter, nextValue); CHECK_EXCEPTION(); - if (done->toBoolean()) { + if (done->toBoolean()) return a.asReturnedValue(); - } adder->call(a, nextValue, 1); if (scope.engine->hasException) { @@ -139,6 +133,25 @@ void MapPrototype::init(ExecutionEngine *engine, Object *ctor) defineReadonlyConfigurableProperty(engine->symbol_toStringTag(), val); } +void Heap::MapObject::init() +{ + Object::init(); + esTable = new ESTable(); +} + +void Heap::MapObject::destroy() +{ + delete esTable; + esTable = 0; +} + +void Heap::MapObject::markObjects(Heap::Base *that, MarkStack *markStack) +{ + MapObject *m = static_cast<MapObject *>(that); + m->esTable->markObjects(markStack); + Object::markObjects(that, markStack); +} + ReturnedValue MapPrototype::method_clear(const FunctionObject *b, const Value *thisObject, const Value *, int) { Scope scope(b); @@ -146,12 +159,10 @@ ReturnedValue MapPrototype::method_clear(const FunctionObject *b, const Value *t if (!that) return scope.engine->throwTypeError(); - that->d()->mapKeys.set(scope.engine, scope.engine->newArrayObject()); - that->d()->mapValues.set(scope.engine, scope.engine->newArrayObject()); + that->d()->esTable->clear(); return Encode::undefined(); } -// delete value ReturnedValue MapPrototype::method_delete(const FunctionObject *b, const Value *thisObject, const Value *argv, int) { Scope scope(b); @@ -159,40 +170,7 @@ ReturnedValue MapPrototype::method_delete(const FunctionObject *b, const Value * if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> keys(scope, that->d()->mapKeys); - qint64 len = keys->getLength(); - - bool found = false; - int idx = 0; - ScopedValue sk(scope); - - for (; idx < len; ++idx) { - sk = keys->getIndexed(idx); - if (sk->sameValueZero(argv[0])) { - found = true; - break; - } - } - - if (found == true) { - Scoped<ArrayObject> values(scope, that->d()->mapValues); - Scoped<ArrayObject> newKeys(scope, scope.engine->newArrayObject()); - Scoped<ArrayObject> newValues(scope, scope.engine->newArrayObject()); - for (int j = 0, newIdx = 0; j < len; ++j, newIdx++) { - if (j == idx) { - newIdx--; // skip the entry - continue; - } - newKeys->putIndexed(newIdx, ScopedValue(scope, keys->getIndexed(j))); - newValues->putIndexed(newIdx, ScopedValue(scope, values->getIndexed(j))); - } - - that->d()->mapKeys.set(scope.engine, newKeys->d()); - that->d()->mapValues.set(scope.engine, newValues->d()); - return Encode(true); - } else { - return Encode(false); - } + return Encode(that->d()->esTable->remove(argv[0])); } ReturnedValue MapPrototype::method_entries(const FunctionObject *b, const Value *thisObject, const Value *, int) @@ -222,19 +200,10 @@ ReturnedValue MapPrototype::method_forEach(const FunctionObject *b, const Value if (argc > 1) thisArg = ScopedValue(scope, argv[1]); - Scoped<ArrayObject> keys(scope, that->d()->mapKeys); - Scoped<ArrayObject> values(scope, that->d()->mapKeys); - qint64 len = keys->getLength(); - Value *arguments = scope.alloc(3); - ScopedValue sk(scope); - ScopedValue sv(scope); - for (int i = 0; i < len; ++i) { - sk = keys->getIndexed(i); - sv = values->getIndexed(i); - - arguments[0] = sv; - arguments[1] = sk; + for (uint i = 0; i < that->d()->esTable->size(); ++i) { + that->d()->esTable->iterate(i, &arguments[0], &arguments[1]); // fill in key (0), value (1) + arguments[2] = that; callbackfn->call(thisArg, arguments, 3); CHECK_EXCEPTION(); @@ -249,19 +218,7 @@ ReturnedValue MapPrototype::method_get(const FunctionObject *b, const Value *thi if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> keys(scope, that->d()->mapKeys); - ScopedValue sk(scope); - qint64 len = keys->getLength(); - - for (int i = 0; i < len; ++i) { - sk = keys->getIndexed(i); - if (sk->sameValueZero(argv[0])) { - Scoped<ArrayObject> values(scope, that->d()->mapValues); - return values->getIndexed(i); - } - } - - return Encode::undefined(); + return that->d()->esTable->get(argv[0]); } ReturnedValue MapPrototype::method_has(const FunctionObject *b, const Value *thisObject, const Value *argv, int) @@ -271,17 +228,7 @@ ReturnedValue MapPrototype::method_has(const FunctionObject *b, const Value *thi if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> keys(scope, that->d()->mapKeys); - ScopedValue sk(scope); - qint64 len = keys->getLength(); - - for (int i = 0; i < len; ++i) { - sk = keys->getIndexed(i); - if (sk->sameValueZero(argv[0])) - return Encode(true); - } - - return Encode(false); + return Encode(that->d()->esTable->has(argv[0])); } ReturnedValue MapPrototype::method_keys(const FunctionObject *b, const Value *thisObject, const Value *, int) @@ -303,27 +250,7 @@ ReturnedValue MapPrototype::method_set(const FunctionObject *b, const Value *thi if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> keys(scope, that->d()->mapKeys); - Scoped<ArrayObject> values(scope, that->d()->mapValues); - ScopedValue sk(scope, argv[1]); - qint64 len = keys->getLength(); - - for (int i = 0; i < len; ++i) { - sk = keys->getIndexed(i); - if (sk->sameValueZero(argv[0])) { - values->putIndexed(len, argv[1]); - return that.asReturnedValue(); - } - } - - sk = argv[0]; - if (sk->isDouble()) { - if (sk->doubleValue() == 0 && std::signbit(sk->doubleValue())) - sk = Primitive::fromDouble(+0); - } - - keys->putIndexed(len, sk); - values->putIndexed(len, argv[1]); + that->d()->esTable->set(argv[0], argv[1]); return that.asReturnedValue(); } @@ -334,9 +261,7 @@ ReturnedValue MapPrototype::method_get_size(const FunctionObject *b, const Value if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> keys(scope, that->d()->mapKeys); - qint64 len = keys->getLength(); - return Encode((uint)len); + return Encode(that->d()->esTable->size()); } ReturnedValue MapPrototype::method_values(const FunctionObject *b, const Value *thisObject, const Value *, int) diff --git a/src/qml/jsruntime/qv4mapobject_p.h b/src/qml/jsruntime/qv4mapobject_p.h index 9c64e25c3d..9543c69928 100644 --- a/src/qml/jsruntime/qv4mapobject_p.h +++ b/src/qml/jsruntime/qv4mapobject_p.h @@ -60,19 +60,19 @@ QT_BEGIN_NAMESPACE namespace QV4 { +class ESTable; + namespace Heap { struct MapCtor : FunctionObject { void init(QV4::ExecutionContext *scope); }; -#define MapObjectMembers(class, Member) \ - Member(class, Pointer, ArrayObject *, mapKeys) \ - Member(class, Pointer, ArrayObject *, mapValues) - -DECLARE_HEAP_OBJECT(MapObject, Object) { - DECLARE_MARKOBJECTS(MapObject); - void init() { Object::init(); } +struct MapObject : Object { + static void markObjects(Heap::Base *that, MarkStack *markStack); + void init(); + void destroy(); + ESTable *esTable; }; } @@ -89,6 +89,7 @@ struct MapObject : Object { V4_OBJECT2(MapObject, Object) V4_PROTOTYPE(mapPrototype) + V4_NEEDS_DESTROY }; struct MapPrototype : Object diff --git a/src/qml/jsruntime/qv4setiterator.cpp b/src/qml/jsruntime/qv4setiterator.cpp index 715a4e30d5..4681a49bd6 100644 --- a/src/qml/jsruntime/qv4setiterator.cpp +++ b/src/qml/jsruntime/qv4setiterator.cpp @@ -38,6 +38,7 @@ ****************************************************************************/ #include <private/qv4iterator_p.h> +#include <private/qv4estable_p.h> #include <private/qv4setiterator_p.h> #include <private/qv4setobject_p.h> #include <private/qv4symbol_p.h> @@ -63,34 +64,31 @@ ReturnedValue SetIteratorPrototype::method_next(const FunctionObject *b, const V return scope.engine->throwTypeError(QLatin1String("Not a Set Iterator instance")); Scoped<SetObject> s(scope, thisObject->d()->iteratedSet); - quint32 index = thisObject->d()->setNextIndex; + uint index = thisObject->d()->setNextIndex; IteratorKind itemKind = thisObject->d()->iterationKind; - if (!s) { QV4::Value undefined = Primitive::undefinedValue(); return IteratorPrototype::createIterResultObject(scope.engine, undefined, true); } - Scoped<ArrayObject> arr(scope, s->d()->setArray); + Value *arguments = scope.alloc(2); - while (index < arr->getLength()) { - ScopedValue e(scope, arr->getIndexed(index)); - index += 1; - thisObject->d()->setNextIndex = index; + while (index < s->d()->esTable->size()) { + s->d()->esTable->iterate(index, &arguments[0], &arguments[1]); + thisObject->d()->setNextIndex = index + 1; if (itemKind == KeyValueIteratorKind) { - // Return CreateIterResultObject(CreateArrayFromList(« e, e »), false). ScopedArrayObject resultArray(scope, scope.engine->newArrayObject()); resultArray->arrayReserve(2); - resultArray->arrayPut(0, e); - resultArray->arrayPut(1, e); + resultArray->arrayPut(0, arguments[0]); + resultArray->arrayPut(1, arguments[0]); // yes, the key is repeated. resultArray->setArrayLengthUnchecked(2); return IteratorPrototype::createIterResultObject(scope.engine, resultArray, false); } - return IteratorPrototype::createIterResultObject(scope.engine, e, false); + return IteratorPrototype::createIterResultObject(scope.engine, arguments[0], false); } thisObject->d()->iteratedSet.set(scope.engine, nullptr); diff --git a/src/qml/jsruntime/qv4setobject.cpp b/src/qml/jsruntime/qv4setobject.cpp index 4245cd2ea3..a4614e7c2e 100644 --- a/src/qml/jsruntime/qv4setobject.cpp +++ b/src/qml/jsruntime/qv4setobject.cpp @@ -40,6 +40,7 @@ #include "qv4setobject_p.h" #include "qv4setiterator_p.h" +#include "qv4estable_p.h" #include "qv4symbol_p.h" using namespace QV4; @@ -56,30 +57,25 @@ ReturnedValue SetCtor::callAsConstructor(const FunctionObject *f, const Value *a { Scope scope(f); Scoped<SetObject> a(scope, scope.engine->memoryManager->allocate<SetObject>()); - Scoped<ArrayObject> arr(scope, scope.engine->newArrayObject()); - a->d()->setArray.set(scope.engine, arr->d()); if (argc > 0) { ScopedValue iterable(scope, argv[0]); if (!iterable->isUndefined() && !iterable->isNull()) { ScopedFunctionObject adder(scope, a->get(ScopedString(scope, scope.engine->newString(QString::fromLatin1("add"))))); - if (!adder) { + if (!adder) return scope.engine->throwTypeError(); - } ScopedObject iter(scope, Runtime::method_getIterator(scope.engine, iterable, true)); CHECK_EXCEPTION(); - if (!iter) { + if (!iter) return a.asReturnedValue(); - } Value *nextValue = scope.alloc(1); ScopedValue done(scope); forever { done = Runtime::method_iteratorNext(scope.engine, iter, nextValue); CHECK_EXCEPTION(); - if (done->toBoolean()) { + if (done->toBoolean()) return a.asReturnedValue(); - } adder->call(a, nextValue, 1); if (scope.engine->hasException) { @@ -128,6 +124,25 @@ void SetPrototype::init(ExecutionEngine *engine, Object *ctor) defineReadonlyConfigurableProperty(engine->symbol_toStringTag(), val); } +void Heap::SetObject::init() +{ + Object::init(); + esTable = new ESTable(); +} + +void Heap::SetObject::destroy() +{ + delete esTable; + esTable = 0; +} + +void Heap::SetObject::markObjects(Heap::Base *that, MarkStack *markStack) +{ + SetObject *s = static_cast<SetObject *>(that); + s->esTable->markObjects(markStack); + Object::markObjects(that, markStack); +} + ReturnedValue SetPrototype::method_add(const FunctionObject *b, const Value *thisObject, const Value *argv, int) { Scope scope(b); @@ -135,23 +150,7 @@ ReturnedValue SetPrototype::method_add(const FunctionObject *b, const Value *thi if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> arr(scope, that->d()->setArray); - ScopedValue sk(scope); - qint64 len = arr->getLength(); - - for (int i = 0; i < len; ++i) { - sk = arr->getIndexed(i); - if (sk->sameValueZero(argv[0])) - return that.asReturnedValue(); - } - - sk = argv[0]; - if (sk->isDouble()) { - if (sk->doubleValue() == 0 && std::signbit(sk->doubleValue())) - sk = Primitive::fromDouble(+0); - } - - arr->putIndexed(len, sk); + that->d()->esTable->set(argv[0], Primitive::undefinedValue()); return that.asReturnedValue(); } @@ -162,12 +161,10 @@ ReturnedValue SetPrototype::method_clear(const FunctionObject *b, const Value *t if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> arr(scope, scope.engine->newArrayObject()); - that->d()->setArray.set(scope.engine, arr->d()); + that->d()->esTable->clear(); return Encode::undefined(); } -// delete value ReturnedValue SetPrototype::method_delete(const FunctionObject *b, const Value *thisObject, const Value *argv, int) { Scope scope(b); @@ -175,36 +172,7 @@ ReturnedValue SetPrototype::method_delete(const FunctionObject *b, const Value * if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> arr(scope, that->d()->setArray); - ScopedValue sk(scope); - qint64 len = arr->getLength(); - - bool found = false; - int idx = 0; - - for (; idx < len; ++idx) { - sk = arr->getIndexed(idx); - if (sk->sameValueZero(argv[0])) { - found = true; - break; - } - } - - if (found == true) { - Scoped<ArrayObject> newArr(scope, scope.engine->newArrayObject()); - for (int j = 0, newIdx = 0; j < len; ++j, newIdx++) { - if (j == idx) { - newIdx--; // skip the entry - continue; - } - newArr->putIndexed(newIdx, ScopedValue(scope, arr->getIndexed(j))); - } - - that->d()->setArray.set(scope.engine, newArr->d()); - return Encode(true); - } else { - return Encode(false); - } + return Encode(that->d()->esTable->remove(argv[0])); } ReturnedValue SetPrototype::method_entries(const FunctionObject *b, const Value *thisObject, const Value *, int) @@ -234,16 +202,11 @@ ReturnedValue SetPrototype::method_forEach(const FunctionObject *b, const Value if (argc > 1) thisArg = ScopedValue(scope, argv[1]); - Scoped<ArrayObject> arr(scope, that->d()->setArray); - ScopedValue sk(scope); - qint64 len = arr->getLength(); - Value *arguments = scope.alloc(3); - for (int i = 0; i < len; ++i) { - sk = arr->getIndexed(i); + for (uint i = 0; i < that->d()->esTable->size(); ++i) { + that->d()->esTable->iterate(i, &arguments[0], &arguments[1]); // fill in key (0), value (1) + arguments[1] = arguments[0]; // but for set, we want to return the key twice; value is always undefined. - arguments[0] = sk; - arguments[1] = sk; arguments[2] = that; callbackfn->call(thisArg, arguments, 3); CHECK_EXCEPTION(); @@ -258,17 +221,7 @@ ReturnedValue SetPrototype::method_has(const FunctionObject *b, const Value *thi if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> arr(scope, that->d()->setArray); - ScopedValue sk(scope); - qint64 len = arr->getLength(); - - for (int i = 0; i < len; ++i) { - sk = arr->getIndexed(i); - if (sk->sameValueZero(argv[0])) - return Encode(true); - } - - return Encode(false); + return Encode(that->d()->esTable->has(argv[0])); } ReturnedValue SetPrototype::method_get_size(const FunctionObject *b, const Value *thisObject, const Value *, int) @@ -278,9 +231,7 @@ ReturnedValue SetPrototype::method_get_size(const FunctionObject *b, const Value if (!that) return scope.engine->throwTypeError(); - Scoped<ArrayObject> arr(scope, that->d()->setArray); - qint64 len = arr->getLength(); - return Encode((uint)len); + return Encode(that->d()->esTable->size()); } ReturnedValue SetPrototype::method_values(const FunctionObject *b, const Value *thisObject, const Value *, int) diff --git a/src/qml/jsruntime/qv4setobject_p.h b/src/qml/jsruntime/qv4setobject_p.h index ba0ed6bfe7..1335a9a712 100644 --- a/src/qml/jsruntime/qv4setobject_p.h +++ b/src/qml/jsruntime/qv4setobject_p.h @@ -60,18 +60,19 @@ QT_BEGIN_NAMESPACE namespace QV4 { +class ESTable; + namespace Heap { struct SetCtor : FunctionObject { void init(QV4::ExecutionContext *scope); }; -#define SetObjectMembers(class, Member) \ - Member(class, Pointer, ArrayObject *, setArray) - -DECLARE_HEAP_OBJECT(SetObject, Object) { - DECLARE_MARKOBJECTS(SetObject); - void init() { Object::init(); } +struct SetObject : Object { + static void markObjects(Heap::Base *that, MarkStack *markStack); + void init(); + void destroy(); + ESTable *esTable; }; } @@ -88,6 +89,7 @@ struct SetObject : Object { V4_OBJECT2(SetObject, Object) V4_PROTOTYPE(setPrototype) + V4_NEEDS_DESTROY }; struct SetPrototype : Object diff --git a/tests/auto/qml/ecmascripttests/TestExpectations b/tests/auto/qml/ecmascripttests/TestExpectations index d03a2913b4..a456d8fa66 100644 --- a/tests/auto/qml/ecmascripttests/TestExpectations +++ b/tests/auto/qml/ecmascripttests/TestExpectations @@ -1106,8 +1106,6 @@ built-ins/Set/prototype/clear/does-not-have-setdata-internal-slot-weakset.js fai built-ins/Set/prototype/delete/does-not-have-setdata-internal-slot-weakset.js fails built-ins/Set/prototype/entries/does-not-have-setdata-internal-slot-weakset.js fails built-ins/Set/prototype/forEach/does-not-have-setdata-internal-slot-weakset.js fails -built-ins/Set/prototype/forEach/iterates-values-added-after-foreach-begins.js fails -built-ins/Set/prototype/forEach/iterates-values-deleted-then-readded.js fails built-ins/Set/prototype/forEach/iterates-values-revisits-after-delete-re-add.js fails built-ins/Set/prototype/forEach/this-arg-explicit-cannot-override-lexical-this-arrow.js fails built-ins/Set/prototype/has/does-not-have-setdata-internal-slot-weakset.js fails |