diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-08-26 17:50:44 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2018-08-28 21:45:48 +0000 |
commit | 94f0d86b5d4c52a6af4843d05d47e7dcf2d1acaa (patch) | |
tree | 60e7d8615999d854e365c4caf1fc003ab83540c9 /src/qml/jsruntime | |
parent | 464b878b973710077b2b92b1682d6f38c83554dd (diff) |
Add support for WeakSet
Change-Id: I5cee2bf0c6a45ad2c14b52e1a4fc5ef015e01042
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime')
-rw-r--r-- | src/qml/jsruntime/qv4engine.cpp | 5 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4engine_p.h | 4 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4global_p.h | 2 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4setobject.cpp | 99 | ||||
-rw-r--r-- | src/qml/jsruntime/qv4setobject_p.h | 37 |
5 files changed, 131 insertions, 16 deletions
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<MapPrototype>(); static_cast<MapPrototype *>(mapPrototype())->init(this, mapCtor()); + jsObjects[WeakSet_Ctor] = memoryManager->allocate<WeakSetCtor>(global); + jsObjects[WeakSetProto] = memoryManager->allocate<WeakSetPrototype>(); + static_cast<WeakSetPrototype *>(weakSetPrototype())->init(this, weakSetCtor()); + jsObjects[Set_Ctor] = memoryManager->allocate<SetCtor>(global); jsObjects[SetProto] = memoryManager->allocate<SetPrototype>(); static_cast<SetPrototype *>(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<FunctionObject *>(jsObjects + SharedArrayBuffer_Ctor); } FunctionObject *arrayBufferCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + ArrayBuffer_Ctor); } FunctionObject *dataViewCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + DataView_Ctor); } + FunctionObject *weakSetCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + WeakSet_Ctor); } FunctionObject *setCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Set_Ctor); } FunctionObject *weakMapCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + WeakMap_Ctor); } FunctionObject *mapCtor() const { return reinterpret_cast<FunctionObject *>(jsObjects + Map_Ctor); } @@ -271,6 +274,7 @@ public: Object *sharedArrayBufferPrototype() const { return reinterpret_cast<Object *>(jsObjects + SharedArrayBufferProto); } Object *arrayBufferPrototype() const { return reinterpret_cast<Object *>(jsObjects + ArrayBufferProto); } Object *dataViewPrototype() const { return reinterpret_cast<Object *>(jsObjects + DataViewProto); } + Object *weakSetPrototype() const { return reinterpret_cast<Object *>(jsObjects + WeakSetProto); } Object *setPrototype() const { return reinterpret_cast<Object *>(jsObjects + SetProto); } Object *weakMapPrototype() const { return reinterpret_cast<Object *>(jsObjects + WeakMapProto); } Object *mapPrototype() const { return reinterpret_cast<Object *>(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 <typename T, size_t> 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<SetObject> a(scope, scope.engine->memoryManager->allocate<SetObject>()); + 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<SetObject> 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<SetObject> 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<SetObject> 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<SetObject *>(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<SetObject> 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<SetObject> 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<SetObject> 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<SetObject> that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); Scoped<SetIteratorObject> ao(scope, scope.engine->newSetIteratorObject(that)); @@ -191,7 +267,7 @@ ReturnedValue SetPrototype::method_forEach(const FunctionObject *b, const Value { Scope scope(b); Scoped<SetObject> 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<SetObject> 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<SetObject> 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<SetObject> that(scope, thisObject); - if (!that) + if (!that || that->d()->isWeakSet) return scope.engine->throwTypeError(); Scoped<SetIteratorObject> 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); |