From 19f657d9957e29034c719d6a6b015f7a78ae1a4a Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Sun, 27 May 2018 15:04:23 +0200 Subject: Add the start of a Set from ES7 Based on top of an ArrayObject for now, which is admittedly a bit of a cheat and not matching the "spirit" of the spec. OTOH, that makes it easy to write, and is presumably quite lightweight, so perhaps this is acceptable as a starting point. Change-Id: Ibc98137965b3e75635b960a2f88c251d45e6e837 Reviewed-by: Lars Knoll --- src/qml/jsruntime/jsruntime.pri | 4 + src/qml/jsruntime/qv4arrayiterator.cpp | 2 +- src/qml/jsruntime/qv4arrayiterator_p.h | 12 +- src/qml/jsruntime/qv4arrayobject.cpp | 6 +- src/qml/jsruntime/qv4engine.cpp | 14 ++ src/qml/jsruntime/qv4engine_p.h | 7 + src/qml/jsruntime/qv4iterator_p.h | 6 + src/qml/jsruntime/qv4managed.cpp | 3 + src/qml/jsruntime/qv4managed_p.h | 1 + src/qml/jsruntime/qv4setiterator.cpp | 100 +++++++++++ src/qml/jsruntime/qv4setiterator_p.h | 103 ++++++++++++ src/qml/jsruntime/qv4setobject.cpp | 297 +++++++++++++++++++++++++++++++++ src/qml/jsruntime/qv4setobject_p.h | 113 +++++++++++++ src/qml/jsruntime/qv4typedarray.cpp | 6 +- src/qml/jsruntime/qv4value.cpp | 5 + 15 files changed, 662 insertions(+), 17 deletions(-) create mode 100644 src/qml/jsruntime/qv4setiterator.cpp create mode 100644 src/qml/jsruntime/qv4setiterator_p.h create mode 100644 src/qml/jsruntime/qv4setobject.cpp create mode 100644 src/qml/jsruntime/qv4setobject_p.h (limited to 'src/qml') diff --git a/src/qml/jsruntime/jsruntime.pri b/src/qml/jsruntime/jsruntime.pri index 96956e2613..91b68f6d79 100644 --- a/src/qml/jsruntime/jsruntime.pri +++ b/src/qml/jsruntime/jsruntime.pri @@ -41,6 +41,8 @@ SOURCES += \ $$PWD/qv4serialize.cpp \ $$PWD/qv4script.cpp \ $$PWD/qv4symbol.cpp \ + $$PWD/qv4setobject.cpp \ + $$PWD/qv4setiterator.cpp \ $$PWD/qv4include.cpp \ $$PWD/qv4qobjectwrapper.cpp \ $$PWD/qv4arraybuffer.cpp \ @@ -96,6 +98,8 @@ HEADERS += \ $$PWD/qv4serialize_p.h \ $$PWD/qv4script_p.h \ $$PWD/qv4symbol_p.h \ + $$PWD/qv4setobject_p.h \ + $$PWD/qv4setiterator_p.h \ $$PWD/qv4scopedvalue_p.h \ $$PWD/qv4executableallocator_p.h \ $$PWD/qv4include_p.h \ diff --git a/src/qml/jsruntime/qv4arrayiterator.cpp b/src/qml/jsruntime/qv4arrayiterator.cpp index fb8c3295bd..d6f787c01d 100644 --- a/src/qml/jsruntime/qv4arrayiterator.cpp +++ b/src/qml/jsruntime/qv4arrayiterator.cpp @@ -70,7 +70,7 @@ ReturnedValue ArrayIteratorPrototype::method_next(const FunctionObject *b, const } quint32 index = thisObject->d()->nextIndex; - ArrayIteratorKind itemKind = thisObject->d()->iterationKind; + IteratorKind itemKind = thisObject->d()->iterationKind; Scoped ta(scope, a->as()); quint32 len = a->getLength(); diff --git a/src/qml/jsruntime/qv4arrayiterator_p.h b/src/qml/jsruntime/qv4arrayiterator_p.h index 2951c9a763..6d6bb466f1 100644 --- a/src/qml/jsruntime/qv4arrayiterator_p.h +++ b/src/qml/jsruntime/qv4arrayiterator_p.h @@ -53,6 +53,7 @@ // #include "qv4object_p.h" +#include "qv4iterator_p.h" #include "qv4arraydata_p.h" QT_BEGIN_NAMESPACE @@ -60,20 +61,11 @@ QT_BEGIN_NAMESPACE namespace QV4 { -// ### spec says that this is a string, but I don't see it exposed anywhere, so -// does that matter? -// ### maybe we should share this with other iterator types in the future. -enum ArrayIteratorKind { - KeyIteratorKind, - ValueIteratorKind, - KeyValueIteratorKind -}; - namespace Heap { #define ArrayIteratorObjectMembers(class, Member) \ Member(class, Pointer, Object *, iteratedObject) \ - Member(class, NoMark, ArrayIteratorKind, iterationKind) \ + Member(class, NoMark, IteratorKind, iterationKind) \ Member(class, NoMark, quint32, nextIndex) DECLARE_HEAP_OBJECT(ArrayIteratorObject, Object) { diff --git a/src/qml/jsruntime/qv4arrayobject.cpp b/src/qml/jsruntime/qv4arrayobject.cpp index c106d5531f..434f6781a8 100644 --- a/src/qml/jsruntime/qv4arrayobject.cpp +++ b/src/qml/jsruntime/qv4arrayobject.cpp @@ -473,7 +473,7 @@ ReturnedValue ArrayPrototype::method_entries(const FunctionObject *b, const Valu RETURN_UNDEFINED(); Scoped ao(scope, scope.engine->newArrayIteratorObject(O)); - ao->d()->iterationKind = ArrayIteratorKind::KeyValueIteratorKind; + ao->d()->iterationKind = IteratorKind::KeyValueIteratorKind; return ao->asReturnedValue(); } @@ -1083,7 +1083,7 @@ ReturnedValue ArrayPrototype::method_keys(const FunctionObject *f, const Value * RETURN_UNDEFINED(); Scoped ao(scope, scope.engine->newArrayIteratorObject(O)); - ao->d()->iterationKind = ArrayIteratorKind::KeyIteratorKind; + ao->d()->iterationKind = IteratorKind::KeyIteratorKind; return ao->asReturnedValue(); } @@ -1446,7 +1446,7 @@ ReturnedValue ArrayPrototype::method_values(const FunctionObject *b, const Value RETURN_UNDEFINED(); Scoped ao(scope, scope.engine->newArrayIteratorObject(O)); - ao->d()->iterationKind = ArrayIteratorKind::ValueIteratorKind; + ao->d()->iterationKind = IteratorKind::ValueIteratorKind; return ao->asReturnedValue(); } diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 87132f64bb..1f326d3ead 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +55,7 @@ #include #include #include "qv4symbol_p.h" +#include "qv4setobject_p.h" #include #include #include @@ -421,6 +423,7 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine) jsObjects[URIError_Ctor] = memoryManager->allocate(global); jsObjects[IteratorProto] = memoryManager->allocate(); jsObjects[ForInIteratorProto] = memoryManager->allocObject(newInternalClass(ForInIteratorPrototype::staticVTable(), iteratorPrototype())); + jsObjects[SetIteratorProto] = memoryManager->allocObject(newInternalClass(SetIteratorPrototype::staticVTable(), iteratorPrototype())); jsObjects[ArrayIteratorProto] = memoryManager->allocObject(newInternalClass(ArrayIteratorPrototype::staticVTable(), iteratorPrototype())); jsObjects[StringIteratorProto] = memoryManager->allocObject(newInternalClass(StringIteratorPrototype::staticVTable(), iteratorPrototype())); @@ -448,6 +451,7 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine) static_cast(iteratorPrototype())->init(this); static_cast(forInIteratorPrototype())->init(this); + static_cast(setIteratorPrototype())->init(this); static_cast(arrayIteratorPrototype())->init(this); static_cast(stringIteratorPrototype())->init(this); @@ -457,6 +461,10 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine) sequencePrototype()->cast()->init(); #endif + jsObjects[Set_Ctor] = memoryManager->allocate(global); + jsObjects[SetProto] = memoryManager->allocate(); + static_cast(setPrototype())->init(this, setCtor()); + // typed arrays jsObjects[ArrayBuffer_Ctor] = memoryManager->allocate(global); @@ -506,6 +514,7 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine) globalObject->defineDefaultProperty(QStringLiteral("ArrayBuffer"), *arrayBufferCtor()); globalObject->defineDefaultProperty(QStringLiteral("DataView"), *dataViewCtor()); + globalObject->defineDefaultProperty(QStringLiteral("Set"), *setCtor()); for (int i = 0; i < Heap::TypedArray::NTypes; ++i) globalObject->defineDefaultProperty((str = typedArrayCtors[i].as()->name())->toQString(), typedArrayCtors[i]); ScopedObject o(scope); @@ -815,6 +824,11 @@ Heap::Object *ExecutionEngine::newForInIteratorObject(Object *o) return obj->d(); } +Heap::Object *ExecutionEngine::newSetIteratorObject(Object *o) +{ + return memoryManager->allocate(o->d(), this); +} + Heap::Object *ExecutionEngine::newArrayIteratorObject(Object *o) { return memoryManager->allocate(o->d(), this); diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h index aa38189cdf..9e5f3fa162 100644 --- a/src/qml/jsruntime/qv4engine_p.h +++ b/src/qml/jsruntime/qv4engine_p.h @@ -186,11 +186,13 @@ public: #endif ArrayBufferProto, DataViewProto, + SetProto, IntrinsicTypedArrayProto, ValueTypeProto, SignalHandlerProto, IteratorProto, ForInIteratorProto, + SetIteratorProto, ArrayIteratorProto, StringIteratorProto, @@ -213,6 +215,7 @@ public: URIError_Ctor, ArrayBuffer_Ctor, DataView_Ctor, + Set_Ctor, IntrinsicTypedArray_Ctor, GetSymbolSpecies, @@ -247,6 +250,7 @@ public: FunctionObject *uRIErrorCtor() const { return reinterpret_cast(jsObjects + URIError_Ctor); } FunctionObject *arrayBufferCtor() const { return reinterpret_cast(jsObjects + ArrayBuffer_Ctor); } FunctionObject *dataViewCtor() const { return reinterpret_cast(jsObjects + DataView_Ctor); } + FunctionObject *setCtor() const { return reinterpret_cast(jsObjects + Set_Ctor); } FunctionObject *intrinsicTypedArrayCtor() const { return reinterpret_cast(jsObjects + IntrinsicTypedArray_Ctor); } FunctionObject *typedArrayCtors; @@ -278,6 +282,7 @@ public: Object *arrayBufferPrototype() const { return reinterpret_cast(jsObjects + ArrayBufferProto); } Object *dataViewPrototype() const { return reinterpret_cast(jsObjects + DataViewProto); } + Object *setPrototype() const { return reinterpret_cast(jsObjects + SetProto); } Object *intrinsicTypedArrayPrototype() const { return reinterpret_cast(jsObjects + IntrinsicTypedArrayProto); } Object *typedArrayPrototype; @@ -285,6 +290,7 @@ public: Object *signalHandlerPrototype() const { return reinterpret_cast(jsObjects + SignalHandlerProto); } Object *iteratorPrototype() const { return reinterpret_cast(jsObjects + IteratorProto); } Object *forInIteratorPrototype() const { return reinterpret_cast(jsObjects + ForInIteratorProto); } + Object *setIteratorPrototype() const { return reinterpret_cast(jsObjects + SetIteratorProto); } Object *arrayIteratorPrototype() const { return reinterpret_cast(jsObjects + ArrayIteratorProto); } Object *stringIteratorPrototype() const { return reinterpret_cast(jsObjects + StringIteratorProto); } @@ -510,6 +516,7 @@ public: Heap::Object *newVariantObject(const QVariant &v); Heap::Object *newForInIteratorObject(Object *o); + Heap::Object *newSetIteratorObject(Object *o); Heap::Object *newArrayIteratorObject(Object *o); Heap::QmlContext *qmlContext() const; diff --git a/src/qml/jsruntime/qv4iterator_p.h b/src/qml/jsruntime/qv4iterator_p.h index d4617166ad..28e337d21b 100644 --- a/src/qml/jsruntime/qv4iterator_p.h +++ b/src/qml/jsruntime/qv4iterator_p.h @@ -59,6 +59,12 @@ QT_BEGIN_NAMESPACE namespace QV4 { +enum IteratorKind { + KeyIteratorKind, + ValueIteratorKind, + KeyValueIteratorKind +}; + struct IteratorPrototype : Object { void init(ExecutionEngine *engine); diff --git a/src/qml/jsruntime/qv4managed.cpp b/src/qml/jsruntime/qv4managed.cpp index bd22e2bc04..304aded44d 100644 --- a/src/qml/jsruntime/qv4managed.cpp +++ b/src/qml/jsruntime/qv4managed.cpp @@ -125,6 +125,9 @@ QString Managed::className() const case Type_ExecutionContext: s = "__ExecutionContext"; break; + case Type_SetIteratorObject: + s = "Set Iterator"; + break; case Type_ArrayIteratorObject: s = "Array Iterator"; break; diff --git a/src/qml/jsruntime/qv4managed_p.h b/src/qml/jsruntime/qv4managed_p.h index 020e61f014..949b4cdcb4 100644 --- a/src/qml/jsruntime/qv4managed_p.h +++ b/src/qml/jsruntime/qv4managed_p.h @@ -199,6 +199,7 @@ public: Type_ExecutionContext, Type_InternalClass, + Type_SetIteratorObject, Type_ArrayIteratorObject, Type_StringIteratorObject, Type_ForInIterator, diff --git a/src/qml/jsruntime/qv4setiterator.cpp b/src/qml/jsruntime/qv4setiterator.cpp new file mode 100644 index 0000000000..715a4e30d5 --- /dev/null +++ b/src/qml/jsruntime/qv4setiterator.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Crimson AS +** 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 +#include +#include +#include + +using namespace QV4; + +DEFINE_OBJECT_VTABLE(SetIteratorObject); + +void SetIteratorPrototype::init(ExecutionEngine *e) +{ + defineDefaultProperty(QStringLiteral("next"), method_next, 0); + + Scope scope(e); + ScopedString val(scope, e->newString(QLatin1String("Set Iterator"))); + defineReadonlyConfigurableProperty(e->symbol_toStringTag(), val); +} + +ReturnedValue SetIteratorPrototype::method_next(const FunctionObject *b, const Value *that, const Value *, int) +{ + Scope scope(b); + const SetIteratorObject *thisObject = that->as(); + if (!thisObject) + return scope.engine->throwTypeError(QLatin1String("Not a Set Iterator instance")); + + Scoped s(scope, thisObject->d()->iteratedSet); + quint32 index = thisObject->d()->setNextIndex; + IteratorKind itemKind = thisObject->d()->iterationKind; + + + if (!s) { + QV4::Value undefined = Primitive::undefinedValue(); + return IteratorPrototype::createIterResultObject(scope.engine, undefined, true); + } + + Scoped arr(scope, s->d()->setArray); + + while (index < arr->getLength()) { + ScopedValue e(scope, arr->getIndexed(index)); + index += 1; + thisObject->d()->setNextIndex = index; + + 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->setArrayLengthUnchecked(2); + + return IteratorPrototype::createIterResultObject(scope.engine, resultArray, false); + } + + return IteratorPrototype::createIterResultObject(scope.engine, e, false); + } + + thisObject->d()->iteratedSet.set(scope.engine, nullptr); + QV4::Value undefined = Primitive::undefinedValue(); + return IteratorPrototype::createIterResultObject(scope.engine, undefined, true); +} + diff --git a/src/qml/jsruntime/qv4setiterator_p.h b/src/qml/jsruntime/qv4setiterator_p.h new file mode 100644 index 0000000000..78eda6d57b --- /dev/null +++ b/src/qml/jsruntime/qv4setiterator_p.h @@ -0,0 +1,103 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Crimson AS +** 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$ +** +****************************************************************************/ + +#ifndef QV4SETITERATOR_P_H +#define QV4SETITERATOR_P_H + +// +// 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. +// + +#include "qv4object_p.h" +#include "qv4iterator_p.h" + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +namespace Heap { + +#define SetIteratorObjectMembers(class, Member) \ + Member(class, Pointer, Object *, iteratedSet) \ + Member(class, NoMark, IteratorKind, iterationKind) \ + Member(class, NoMark, quint32, setNextIndex) + +DECLARE_HEAP_OBJECT(SetIteratorObject, Object) { + DECLARE_MARKOBJECTS(SetIteratorObject); + void init(Object *obj, QV4::ExecutionEngine *engine) + { + Object::init(); + this->iteratedSet.set(engine, obj); + this->setNextIndex = 0; + } +}; + +} + +struct SetIteratorPrototype : Object +{ + V4_PROTOTYPE(iteratorPrototype) + void init(ExecutionEngine *engine); + + static ReturnedValue method_next(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); +}; + +struct SetIteratorObject : Object +{ + V4_OBJECT2(SetIteratorObject, Object) + Q_MANAGED_TYPE(SetIteratorObject) + V4_PROTOTYPE(setIteratorPrototype) + + void init(ExecutionEngine *engine); +}; + + +} + +QT_END_NAMESPACE + +#endif // QV4SETITERATOR_P_H + diff --git a/src/qml/jsruntime/qv4setobject.cpp b/src/qml/jsruntime/qv4setobject.cpp new file mode 100644 index 0000000000..4245cd2ea3 --- /dev/null +++ b/src/qml/jsruntime/qv4setobject.cpp @@ -0,0 +1,297 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Crimson AS +** 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 "qv4setobject_p.h" +#include "qv4setiterator_p.h" +#include "qv4symbol_p.h" + +using namespace QV4; + +DEFINE_OBJECT_VTABLE(SetCtor); +DEFINE_OBJECT_VTABLE(SetObject); + +void Heap::SetCtor::init(QV4::ExecutionContext *scope) +{ + Heap::FunctionObject::init(scope, QStringLiteral("Set")); +} + +ReturnedValue SetCtor::callAsConstructor(const FunctionObject *f, const Value *argv, int argc) +{ + Scope scope(f); + Scoped a(scope, scope.engine->memoryManager->allocate()); + Scoped 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) { + return scope.engine->throwTypeError(); + } + ScopedObject iter(scope, Runtime::method_getIterator(scope.engine, iterable, true)); + CHECK_EXCEPTION(); + 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()) { + return a.asReturnedValue(); + } + + adder->call(a, nextValue, 1); + if (scope.engine->hasException) { + ScopedValue falsey(scope, Encode(false)); + return Runtime::method_iteratorClose(scope.engine, iter, falsey); + } + } + } + } + + return a.asReturnedValue(); +} + +ReturnedValue SetCtor::call(const FunctionObject *f, const Value *, const Value *, int) +{ + Scope scope(f); + return scope.engine->throwTypeError(QString::fromLatin1("Set requires new")); +} + +void SetPrototype::init(ExecutionEngine *engine, Object *ctor) +{ + Scope scope(engine); + ScopedObject o(scope); + ctor->defineReadonlyConfigurableProperty(engine->id_length(), Primitive::fromInt32(0)); + ctor->defineReadonlyProperty(engine->id_prototype(), (o = this)); + ctor->addSymbolSpecies(); + defineDefaultProperty(engine->id_constructor(), (o = ctor)); + + defineDefaultProperty(QStringLiteral("add"), method_add, 1); + defineDefaultProperty(QStringLiteral("clear"), method_clear, 0); + defineDefaultProperty(QStringLiteral("delete"), method_delete, 1); + defineDefaultProperty(QStringLiteral("entries"), method_entries, 0); + defineDefaultProperty(QStringLiteral("forEach"), method_forEach, 1); + defineDefaultProperty(QStringLiteral("has"), method_has, 1); + defineAccessorProperty(QStringLiteral("size"), method_get_size, nullptr); + + // Per the spec, the value for 'keys' is the same as 'values'. + ScopedString valString(scope, scope.engine->newIdentifier(QStringLiteral("values"))); + ScopedFunctionObject valuesFn(scope, FunctionObject::createBuiltinFunction(engine, valString, SetPrototype::method_values, 0)); + defineDefaultProperty(QStringLiteral("keys"), valuesFn); + defineDefaultProperty(QStringLiteral("values"), valuesFn); + + defineDefaultProperty(engine->symbol_iterator(), valuesFn); + + ScopedString val(scope, engine->newString(QLatin1String("Set"))); + defineReadonlyConfigurableProperty(engine->symbol_toStringTag(), val); +} + +ReturnedValue SetPrototype::method_add(const FunctionObject *b, const Value *thisObject, const Value *argv, int) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + Scoped 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); + return that.asReturnedValue(); +} + +ReturnedValue SetPrototype::method_clear(const FunctionObject *b, const Value *thisObject, const Value *, int) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + Scoped arr(scope, scope.engine->newArrayObject()); + that->d()->setArray.set(scope.engine, arr->d()); + return Encode::undefined(); +} + +// delete value +ReturnedValue SetPrototype::method_delete(const FunctionObject *b, const Value *thisObject, const Value *argv, int) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + Scoped 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 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); + } +} + +ReturnedValue SetPrototype::method_entries(const FunctionObject *b, const Value *thisObject, const Value *, int) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + Scoped ao(scope, scope.engine->newSetIteratorObject(that)); + ao->d()->iterationKind = IteratorKind::KeyValueIteratorKind; + return ao->asReturnedValue(); +} + +ReturnedValue SetPrototype::method_forEach(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + ScopedFunctionObject callbackfn(scope, argv[0]); + if (!callbackfn) + return scope.engine->throwTypeError(); + + ScopedValue thisArg(scope, Primitive::undefinedValue()); + if (argc > 1) + thisArg = ScopedValue(scope, argv[1]); + + Scoped 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); + + arguments[0] = sk; + arguments[1] = sk; + arguments[2] = that; + callbackfn->call(thisArg, arguments, 3); + CHECK_EXCEPTION(); + } + return Encode::undefined(); +} + +ReturnedValue SetPrototype::method_has(const FunctionObject *b, const Value *thisObject, const Value *argv, int) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + Scoped 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); +} + +ReturnedValue SetPrototype::method_get_size(const FunctionObject *b, const Value *thisObject, const Value *, int) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + Scoped arr(scope, that->d()->setArray); + qint64 len = arr->getLength(); + return Encode((uint)len); +} + +ReturnedValue SetPrototype::method_values(const FunctionObject *b, const Value *thisObject, const Value *, int) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that) + return scope.engine->throwTypeError(); + + Scoped ao(scope, scope.engine->newSetIteratorObject(that)); + ao->d()->iterationKind = IteratorKind::ValueIteratorKind; + return ao->asReturnedValue(); +} + diff --git a/src/qml/jsruntime/qv4setobject_p.h b/src/qml/jsruntime/qv4setobject_p.h new file mode 100644 index 0000000000..ba0ed6bfe7 --- /dev/null +++ b/src/qml/jsruntime/qv4setobject_p.h @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2018 Crimson AS +** 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$ +** +****************************************************************************/ + +#ifndef QV4SETOBJECT_P_H +#define QV4SETOBJECT_P_H + +// +// 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. +// + +#include "qv4object_p.h" +#include "qv4objectproto_p.h" +#include "qv4functionobject_p.h" +#include "qv4string_p.h" + +QT_BEGIN_NAMESPACE + +namespace QV4 { + +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 SetCtor: FunctionObject +{ + V4_OBJECT2(SetCtor, FunctionObject) + + static ReturnedValue callAsConstructor(const FunctionObject *f, const Value *argv, int argc); + static ReturnedValue call(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); +}; + +struct SetObject : Object +{ + V4_OBJECT2(SetObject, Object) + V4_PROTOTYPE(setPrototype) +}; + +struct SetPrototype : Object +{ + void init(ExecutionEngine *engine, Object *ctor); + + static ReturnedValue method_add(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_clear(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_delete(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_entries(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_forEach(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_has(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_get_size(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); + static ReturnedValue method_values(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); +}; + + +} // namespace QV4 + + +QT_END_NAMESPACE + +#endif // QV4SETOBJECT_P_H diff --git a/src/qml/jsruntime/qv4typedarray.cpp b/src/qml/jsruntime/qv4typedarray.cpp index c62c114e53..b3c6f0e094 100644 --- a/src/qml/jsruntime/qv4typedarray.cpp +++ b/src/qml/jsruntime/qv4typedarray.cpp @@ -465,7 +465,7 @@ ReturnedValue IntrinsicTypedArrayPrototype::method_entries(const FunctionObject THROW_TYPE_ERROR(); Scoped ao(scope, scope.engine->newArrayIteratorObject(O)); - ao->d()->iterationKind = ArrayIteratorKind::KeyValueIteratorKind; + ao->d()->iterationKind = IteratorKind::KeyValueIteratorKind; return ao->asReturnedValue(); } @@ -477,7 +477,7 @@ ReturnedValue IntrinsicTypedArrayPrototype::method_keys(const FunctionObject *b, THROW_TYPE_ERROR(); Scoped ao(scope, scope.engine->newArrayIteratorObject(O)); - ao->d()->iterationKind = ArrayIteratorKind::KeyIteratorKind; + ao->d()->iterationKind = IteratorKind::KeyIteratorKind; return ao->asReturnedValue(); } @@ -489,7 +489,7 @@ ReturnedValue IntrinsicTypedArrayPrototype::method_values(const FunctionObject * THROW_TYPE_ERROR(); Scoped ao(scope, scope.engine->newArrayIteratorObject(O)); - ao->d()->iterationKind = ArrayIteratorKind::ValueIteratorKind; + ao->d()->iterationKind = IteratorKind::ValueIteratorKind; return ao->asReturnedValue(); } diff --git a/src/qml/jsruntime/qv4value.cpp b/src/qml/jsruntime/qv4value.cpp index 85b4297dd6..be406db325 100644 --- a/src/qml/jsruntime/qv4value.cpp +++ b/src/qml/jsruntime/qv4value.cpp @@ -257,6 +257,11 @@ bool Value::sameValueZero(Value other) const { return double(int_32()) == other.doubleValue(); if (isDouble() && other.isInteger()) return other.int_32() == doubleValue(); + if (isDouble() && other.isDouble()) { + if (doubleValue() == 0 && other.doubleValue() == 0) { + return true; + } + } return false; } -- cgit v1.2.3