From 94f0d86b5d4c52a6af4843d05d47e7dcf2d1acaa Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Sun, 26 Aug 2018 17:50:44 +0200 Subject: Add support for WeakSet Change-Id: I5cee2bf0c6a45ad2c14b52e1a4fc5ef015e01042 Reviewed-by: Simon Hausmann --- src/qml/jsruntime/qv4engine.cpp | 5 ++ src/qml/jsruntime/qv4engine_p.h | 4 ++ src/qml/jsruntime/qv4global_p.h | 2 + src/qml/jsruntime/qv4setobject.cpp | 99 +++++++++++++++++++++++++++++++++----- src/qml/jsruntime/qv4setobject_p.h | 37 ++++++++++++-- src/qml/memory/qv4mm.cpp | 18 +++++++ src/qml/memory/qv4mm_p.h | 2 + 7 files changed, 151 insertions(+), 16 deletions(-) (limited to 'src/qml') diff --git a/src/qml/jsruntime/qv4engine.cpp b/src/qml/jsruntime/qv4engine.cpp index 7087e49b30..c40d6414ff 100644 --- a/src/qml/jsruntime/qv4engine.cpp +++ b/src/qml/jsruntime/qv4engine.cpp @@ -493,6 +493,10 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine) jsObjects[MapProto] = memoryManager->allocate(); static_cast(mapPrototype())->init(this, mapCtor()); + jsObjects[WeakSet_Ctor] = memoryManager->allocate(global); + jsObjects[WeakSetProto] = memoryManager->allocate(); + static_cast(weakSetPrototype())->init(this, weakSetCtor()); + jsObjects[Set_Ctor] = memoryManager->allocate(global); jsObjects[SetProto] = memoryManager->allocate(); static_cast(setPrototype())->init(this, setCtor()); @@ -551,6 +555,7 @@ ExecutionEngine::ExecutionEngine(QJSEngine *jsEngine) globalObject->defineDefaultProperty(QStringLiteral("SharedArrayBuffer"), *sharedArrayBufferCtor()); globalObject->defineDefaultProperty(QStringLiteral("ArrayBuffer"), *arrayBufferCtor()); globalObject->defineDefaultProperty(QStringLiteral("DataView"), *dataViewCtor()); + globalObject->defineDefaultProperty(QStringLiteral("WeakSet"), *weakSetCtor()); globalObject->defineDefaultProperty(QStringLiteral("Set"), *setCtor()); globalObject->defineDefaultProperty(QStringLiteral("WeakMap"), *weakMapCtor()); globalObject->defineDefaultProperty(QStringLiteral("Map"), *mapCtor()); diff --git a/src/qml/jsruntime/qv4engine_p.h b/src/qml/jsruntime/qv4engine_p.h index 028615abfb..0b5d8663eb 100644 --- a/src/qml/jsruntime/qv4engine_p.h +++ b/src/qml/jsruntime/qv4engine_p.h @@ -165,6 +165,7 @@ public: SharedArrayBufferProto, ArrayBufferProto, DataViewProto, + WeakSetProto, SetProto, WeakMapProto, MapProto, @@ -198,6 +199,7 @@ public: SharedArrayBuffer_Ctor, ArrayBuffer_Ctor, DataView_Ctor, + WeakSet_Ctor, Set_Ctor, WeakMap_Ctor, Map_Ctor, @@ -236,6 +238,7 @@ public: FunctionObject *sharedArrayBufferCtor() const { return reinterpret_cast(jsObjects + SharedArrayBuffer_Ctor); } FunctionObject *arrayBufferCtor() const { return reinterpret_cast(jsObjects + ArrayBuffer_Ctor); } FunctionObject *dataViewCtor() const { return reinterpret_cast(jsObjects + DataView_Ctor); } + FunctionObject *weakSetCtor() const { return reinterpret_cast(jsObjects + WeakSet_Ctor); } FunctionObject *setCtor() const { return reinterpret_cast(jsObjects + Set_Ctor); } FunctionObject *weakMapCtor() const { return reinterpret_cast(jsObjects + WeakMap_Ctor); } FunctionObject *mapCtor() const { return reinterpret_cast(jsObjects + Map_Ctor); } @@ -271,6 +274,7 @@ public: Object *sharedArrayBufferPrototype() const { return reinterpret_cast(jsObjects + SharedArrayBufferProto); } Object *arrayBufferPrototype() const { return reinterpret_cast(jsObjects + ArrayBufferProto); } Object *dataViewPrototype() const { return reinterpret_cast(jsObjects + DataViewProto); } + Object *weakSetPrototype() const { return reinterpret_cast(jsObjects + WeakSetProto); } Object *setPrototype() const { return reinterpret_cast(jsObjects + SetProto); } Object *weakMapPrototype() const { return reinterpret_cast(jsObjects + WeakMapProto); } Object *mapPrototype() const { return reinterpret_cast(jsObjects + MapProto); } diff --git a/src/qml/jsruntime/qv4global_p.h b/src/qml/jsruntime/qv4global_p.h index 5019d4af3a..f89a4b9909 100644 --- a/src/qml/jsruntime/qv4global_p.h +++ b/src/qml/jsruntime/qv4global_p.h @@ -196,6 +196,7 @@ namespace Heap { struct TypedArray; struct MapObject; + struct SetObject; template struct Pointer; } @@ -245,6 +246,7 @@ struct DataView; struct TypedArray; struct MapObject; +struct SetMapObject; // ReturnedValue is used to return values from runtime methods // the type has to be a primitive type (no struct or union), so that the compiler diff --git a/src/qml/jsruntime/qv4setobject.cpp b/src/qml/jsruntime/qv4setobject.cpp index a03cc3ddf8..65824926b9 100644 --- a/src/qml/jsruntime/qv4setobject.cpp +++ b/src/qml/jsruntime/qv4setobject.cpp @@ -46,17 +46,26 @@ using namespace QV4; DEFINE_OBJECT_VTABLE(SetCtor); +DEFINE_OBJECT_VTABLE(WeakSetCtor); DEFINE_OBJECT_VTABLE(SetObject); +void Heap::WeakSetCtor::init(QV4::ExecutionContext *scope) +{ + Heap::FunctionObject::init(scope, QStringLiteral("WeakSet")); +} + void Heap::SetCtor::init(QV4::ExecutionContext *scope) { Heap::FunctionObject::init(scope, QStringLiteral("Set")); } -ReturnedValue SetCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *) +ReturnedValue WeakSetCtor::construct(const FunctionObject *f, const Value *argv, int argc, const Value *, bool isWeak) { Scope scope(f); Scoped a(scope, scope.engine->memoryManager->allocate()); + if (isWeak) + a->setPrototypeOf(scope.engine->weakSetPrototype()); + a->d()->isWeakSet = isWeak; if (argc > 0) { ScopedValue iterable(scope, argv[0]); @@ -89,12 +98,74 @@ ReturnedValue SetCtor::virtualCallAsConstructor(const FunctionObject *f, const V return a.asReturnedValue(); } -ReturnedValue SetCtor::virtualCall(const FunctionObject *f, const Value *, const Value *, int) +ReturnedValue WeakSetCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget) +{ + return construct(f, argv, argc, newTarget, true); +} + +ReturnedValue WeakSetCtor::virtualCall(const FunctionObject *f, const Value *, const Value *, int) { Scope scope(f); return scope.engine->throwTypeError(QString::fromLatin1("Set requires new")); } +ReturnedValue SetCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget) +{ + return construct(f, argv, argc, newTarget, false); +} + +void WeakSetPrototype::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)); + defineDefaultProperty(engine->id_constructor(), (o = ctor)); + + defineDefaultProperty(QStringLiteral("add"), method_add, 1); + defineDefaultProperty(QStringLiteral("delete"), method_delete, 1); + defineDefaultProperty(QStringLiteral("has"), method_has, 1); + + ScopedString val(scope, engine->newString(QLatin1String("WeakSet"))); + defineReadonlyConfigurableProperty(engine->symbol_toStringTag(), val); +} + +ReturnedValue WeakSetPrototype::method_add(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if ((!that || !that->d()->isWeakSet) || + (!argc || !argv[0].isObject())) + return scope.engine->throwTypeError(); + + that->d()->esTable->set(argv[0], Primitive::undefinedValue()); + return that.asReturnedValue(); +} + +ReturnedValue WeakSetPrototype::method_delete(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that || !that->d()->isWeakSet) + return scope.engine->throwTypeError(); + if (!argc || !argv[0].isObject()) + return Encode(false); + + return Encode(that->d()->esTable->remove(argv[0])); +} + +ReturnedValue WeakSetPrototype::method_has(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) +{ + Scope scope(b); + Scoped that(scope, thisObject); + if (!that || !that->d()->isWeakSet) + return scope.engine->throwTypeError(); + if (!argc || !argv[0].isObject()) + return Encode(false); + + return Encode(that->d()->esTable->has(argv[0])); +} + void SetPrototype::init(ExecutionEngine *engine, Object *ctor) { Scope scope(engine); @@ -136,10 +207,15 @@ void Heap::SetObject::destroy() esTable = 0; } +void Heap::SetObject::removeUnmarkedKeys() +{ + esTable->removeUnmarkedKeys(); +} + void Heap::SetObject::markObjects(Heap::Base *that, MarkStack *markStack) { SetObject *s = static_cast(that); - s->esTable->markObjects(markStack, false); + s->esTable->markObjects(markStack, s->isWeakSet); Object::markObjects(that, markStack); } @@ -147,7 +223,7 @@ ReturnedValue SetPrototype::method_add(const FunctionObject *b, const Value *thi { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); that->d()->esTable->set(argv[0], Primitive::undefinedValue()); @@ -158,7 +234,7 @@ ReturnedValue SetPrototype::method_clear(const FunctionObject *b, const Value *t { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); that->d()->esTable->clear(); @@ -169,7 +245,7 @@ ReturnedValue SetPrototype::method_delete(const FunctionObject *b, const Value * { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); return Encode(that->d()->esTable->remove(argv[0])); @@ -179,7 +255,7 @@ ReturnedValue SetPrototype::method_entries(const FunctionObject *b, const Value { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); Scoped ao(scope, scope.engine->newSetIteratorObject(that)); @@ -191,7 +267,7 @@ ReturnedValue SetPrototype::method_forEach(const FunctionObject *b, const Value { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); ScopedFunctionObject callbackfn(scope, argv[0]); @@ -218,7 +294,7 @@ ReturnedValue SetPrototype::method_has(const FunctionObject *b, const Value *thi { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); return Encode(that->d()->esTable->has(argv[0])); @@ -228,7 +304,7 @@ ReturnedValue SetPrototype::method_get_size(const FunctionObject *b, const Value { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); return Encode(that->d()->esTable->size()); @@ -238,11 +314,10 @@ ReturnedValue SetPrototype::method_values(const FunctionObject *b, const Value * { Scope scope(b); Scoped that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) 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 index 34649c3f01..21584e2132 100644 --- a/src/qml/jsruntime/qv4setobject_p.h +++ b/src/qml/jsruntime/qv4setobject_p.h @@ -64,7 +64,12 @@ class ESTable; namespace Heap { -struct SetCtor : FunctionObject { +struct WeakSetCtor : FunctionObject { + void init(QV4::ExecutionContext *scope); +}; + + +struct SetCtor : WeakSetCtor { void init(QV4::ExecutionContext *scope); }; @@ -72,19 +77,33 @@ struct SetObject : Object { static void markObjects(Heap::Base *that, MarkStack *markStack); void init(); void destroy(); + void removeUnmarkedKeys(); + ESTable *esTable; + SetObject *nextWeakSet; + bool isWeakSet; }; } -struct SetCtor: FunctionObject + +struct WeakSetCtor: FunctionObject { - V4_OBJECT2(SetCtor, FunctionObject) + V4_OBJECT2(WeakSetCtor, FunctionObject) + + static ReturnedValue construct(const FunctionObject *f, const Value *argv, int argc, const Value *, bool weakSet); static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *); static ReturnedValue virtualCall(const FunctionObject *f, const Value *thisObject, const Value *argv, int argc); }; +struct SetCtor : WeakSetCtor +{ + V4_OBJECT2(SetCtor, WeakSetCtor) + + static ReturnedValue virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *); +}; + struct SetObject : Object { V4_OBJECT2(SetObject, Object) @@ -92,7 +111,17 @@ struct SetObject : Object V4_NEEDS_DESTROY }; -struct SetPrototype : Object +struct WeakSetPrototype : Object +{ + void init(ExecutionEngine *engine, Object *ctor); + + static ReturnedValue method_add(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_has(const FunctionObject *, const Value *thisObject, const Value *argv, int argc); +}; + + +struct SetPrototype : WeakSetPrototype { void init(ExecutionEngine *engine, Object *ctor); diff --git a/src/qml/memory/qv4mm.cpp b/src/qml/memory/qv4mm.cpp index 065204b5d3..b9e6327ea2 100644 --- a/src/qml/memory/qv4mm.cpp +++ b/src/qml/memory/qv4mm.cpp @@ -62,6 +62,7 @@ #include "qv4alloca_p.h" #include "qv4profiling_p.h" #include "qv4mapobject_p.h" +#include "qv4setobject_p.h" //#define MM_STATS @@ -989,6 +990,17 @@ void MemoryManager::sweep(bool lastSweep, ClassDestroyStatsCallback classCountPt map = map->nextWeakMap; } + Heap::SetObject *set = weakSets; + Heap::SetObject **lastSet = &weakSets; + while (set) { + if (set->isMarked()) { + set->removeUnmarkedKeys(); + *lastSet = set; + lastSet = &set->nextWeakSet; + } + set = set->nextWeakSet; + } + // onDestruction handlers may have accessed other QObject wrappers and reset their value, so ensure // that they are all set to undefined. for (PersistentValueStorage::Iterator it = m_weakValues->begin(); it != m_weakValues->end(); ++it) { @@ -1198,6 +1210,12 @@ void MemoryManager::registerWeakMap(Heap::MapObject *map) weakMaps = map; } +void MemoryManager::registerWeakSet(Heap::SetObject *set) +{ + set->nextWeakSet = weakSets; + weakSets = set; +} + MemoryManager::~MemoryManager() { delete m_persistentValues; diff --git a/src/qml/memory/qv4mm_p.h b/src/qml/memory/qv4mm_p.h index da162d2dfe..bbbbb1aef6 100644 --- a/src/qml/memory/qv4mm_p.h +++ b/src/qml/memory/qv4mm_p.h @@ -275,6 +275,7 @@ public: } void registerWeakMap(Heap::MapObject *map); + void registerWeakSet(Heap::SetObject *set); protected: /// expects size to be aligned @@ -299,6 +300,7 @@ public: PersistentValueStorage *m_weakValues; QVector m_pendingFreedObjectWrapperValue; Heap::MapObject *weakMaps = nullptr; + Heap::SetObject *weakSets = nullptr; std::size_t unmanagedHeapSize = 0; // the amount of bytes of heap that is not managed by the memory manager, but which is held onto by managed items. std::size_t unmanagedHeapSizeGCLimit; -- cgit v1.2.3