| 1 | // Copyright (C) 2018 Crimson AS <info@crimson.no> |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include "qv4setobject_p.h" |
| 6 | #include "qv4setiterator_p.h" |
| 7 | #include "qv4estable_p.h" |
| 8 | #include "qv4symbol_p.h" |
| 9 | |
| 10 | using namespace QV4; |
| 11 | using namespace Qt::Literals::StringLiterals; |
| 12 | |
| 13 | DEFINE_OBJECT_VTABLE(SetCtor); |
| 14 | DEFINE_OBJECT_VTABLE(WeakSetCtor); |
| 15 | DEFINE_OBJECT_VTABLE(SetObject); |
| 16 | |
| 17 | void Heap::WeakSetCtor::init(QV4::ExecutionEngine *engine) |
| 18 | { |
| 19 | Heap::FunctionObject::init(engine, QStringLiteral("WeakSet" )); |
| 20 | } |
| 21 | |
| 22 | void Heap::SetCtor::init(QV4::ExecutionEngine *engine) |
| 23 | { |
| 24 | Heap::FunctionObject::init(engine, QStringLiteral("Set" )); |
| 25 | } |
| 26 | |
| 27 | ReturnedValue WeakSetCtor::construct(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget, bool isWeak) |
| 28 | { |
| 29 | Scope scope(f); |
| 30 | Scoped<SetObject> a(scope, scope.engine->memoryManager->allocate<SetObject>()); |
| 31 | bool protoSet = false; |
| 32 | if (newTarget) |
| 33 | protoSet = a->setProtoFromNewTarget(newTarget); |
| 34 | if (!protoSet && isWeak) |
| 35 | a->setPrototypeOf(scope.engine->weakSetPrototype()); |
| 36 | a->d()->isWeakSet = isWeak; |
| 37 | |
| 38 | if (argc > 0) { |
| 39 | ScopedValue iterable(scope, argv[0]); |
| 40 | if (!iterable->isUndefined() && !iterable->isNull()) { |
| 41 | ScopedFunctionObject adder(scope, a->get(name: ScopedString(scope, scope.engine->newString(s: u"add"_s )))); |
| 42 | if (!adder) |
| 43 | return scope.engine->throwTypeError(); |
| 44 | ScopedObject iter(scope, Runtime::GetIterator::call(scope.engine, iterable, true)); |
| 45 | CHECK_EXCEPTION(); |
| 46 | if (!iter) |
| 47 | return a.asReturnedValue(); |
| 48 | |
| 49 | Value *nextValue = scope.alloc(nValues: 1); |
| 50 | ScopedValue done(scope); |
| 51 | forever { |
| 52 | done = Runtime::IteratorNext::call(scope.engine, iter, nextValue); |
| 53 | CHECK_EXCEPTION(); |
| 54 | if (done->toBoolean()) |
| 55 | return a.asReturnedValue(); |
| 56 | |
| 57 | adder->call(thisObject: a, argv: nextValue, argc: 1); |
| 58 | if (scope.hasException()) |
| 59 | return Runtime::IteratorClose::call(scope.engine, iter); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return a.asReturnedValue(); |
| 65 | } |
| 66 | |
| 67 | ReturnedValue WeakSetCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget) |
| 68 | { |
| 69 | return construct(f, argv, argc, newTarget, isWeak: true); |
| 70 | } |
| 71 | |
| 72 | ReturnedValue WeakSetCtor::virtualCall(const FunctionObject *f, const Value *, const Value *, int) |
| 73 | { |
| 74 | Scope scope(f); |
| 75 | return scope.engine->throwTypeError(message: QString::fromLatin1(ba: "Set requires new" )); |
| 76 | } |
| 77 | |
| 78 | ReturnedValue SetCtor::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget) |
| 79 | { |
| 80 | return construct(f, argv, argc, newTarget, isWeak: false); |
| 81 | } |
| 82 | |
| 83 | void WeakSetPrototype::init(ExecutionEngine *engine, Object *ctor) |
| 84 | { |
| 85 | Scope scope(engine); |
| 86 | ScopedObject o(scope); |
| 87 | ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 0)); |
| 88 | ctor->defineReadonlyProperty(name: engine->id_prototype(), value: (o = this)); |
| 89 | defineDefaultProperty(name: engine->id_constructor(), value: (o = ctor)); |
| 90 | |
| 91 | defineDefaultProperty(QStringLiteral("add" ), code: method_add, argumentCount: 1); |
| 92 | defineDefaultProperty(QStringLiteral("delete" ), code: method_delete, argumentCount: 1); |
| 93 | defineDefaultProperty(QStringLiteral("has" ), code: method_has, argumentCount: 1); |
| 94 | |
| 95 | ScopedString val(scope, engine->newString(s: QLatin1String("WeakSet" ))); |
| 96 | defineReadonlyConfigurableProperty(name: engine->symbol_toStringTag(), value: val); |
| 97 | } |
| 98 | |
| 99 | ReturnedValue WeakSetPrototype::method_add(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 100 | { |
| 101 | Scope scope(b); |
| 102 | Scoped<SetObject> that(scope, thisObject); |
| 103 | if ((!that || !that->d()->isWeakSet) || |
| 104 | (!argc || !argv[0].isObject())) |
| 105 | return scope.engine->throwTypeError(); |
| 106 | |
| 107 | QV4::WriteBarrier::markCustom(engine: scope.engine, markFunction: [&](QV4::MarkStack *ms) { |
| 108 | if (scope.engine->memoryManager->gcStateMachine->state <= GCState::FreeWeakSets) |
| 109 | return; |
| 110 | argv[0].heapObject()->mark(markStack: ms); |
| 111 | }); |
| 112 | |
| 113 | that->d()->esTable->set(k: argv[0], v: Value::undefinedValue()); |
| 114 | return that.asReturnedValue(); |
| 115 | } |
| 116 | |
| 117 | ReturnedValue WeakSetPrototype::method_delete(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 118 | { |
| 119 | Scope scope(b); |
| 120 | Scoped<SetObject> that(scope, thisObject); |
| 121 | if (!that || !that->d()->isWeakSet) |
| 122 | return scope.engine->throwTypeError(); |
| 123 | if (!argc || !argv[0].isObject()) |
| 124 | return Encode(false); |
| 125 | |
| 126 | return Encode(that->d()->esTable->remove(k: argv[0])); |
| 127 | } |
| 128 | |
| 129 | ReturnedValue WeakSetPrototype::method_has(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 130 | { |
| 131 | Scope scope(b); |
| 132 | Scoped<SetObject> that(scope, thisObject); |
| 133 | if (!that || !that->d()->isWeakSet) |
| 134 | return scope.engine->throwTypeError(); |
| 135 | if (!argc || !argv[0].isObject()) |
| 136 | return Encode(false); |
| 137 | |
| 138 | return Encode(that->d()->esTable->has(k: argv[0])); |
| 139 | } |
| 140 | |
| 141 | void SetPrototype::init(ExecutionEngine *engine, Object *ctor) |
| 142 | { |
| 143 | Scope scope(engine); |
| 144 | ScopedObject o(scope); |
| 145 | ctor->defineReadonlyConfigurableProperty(name: engine->id_length(), value: Value::fromInt32(i: 0)); |
| 146 | ctor->defineReadonlyProperty(name: engine->id_prototype(), value: (o = this)); |
| 147 | ctor->addSymbolSpecies(); |
| 148 | defineDefaultProperty(name: engine->id_constructor(), value: (o = ctor)); |
| 149 | |
| 150 | defineDefaultProperty(QStringLiteral("add" ), code: method_add, argumentCount: 1); |
| 151 | defineDefaultProperty(QStringLiteral("clear" ), code: method_clear, argumentCount: 0); |
| 152 | defineDefaultProperty(QStringLiteral("delete" ), code: method_delete, argumentCount: 1); |
| 153 | defineDefaultProperty(QStringLiteral("entries" ), code: method_entries, argumentCount: 0); |
| 154 | defineDefaultProperty(QStringLiteral("forEach" ), code: method_forEach, argumentCount: 1); |
| 155 | defineDefaultProperty(QStringLiteral("has" ), code: method_has, argumentCount: 1); |
| 156 | defineAccessorProperty(QStringLiteral("size" ), getter: method_get_size, setter: nullptr); |
| 157 | |
| 158 | // Per the spec, the value for 'keys' is the same as 'values'. |
| 159 | ScopedString valString(scope, scope.engine->newIdentifier(QStringLiteral("values" ))); |
| 160 | ScopedFunctionObject valuesFn(scope, FunctionObject::createBuiltinFunction(engine, nameOrSymbol: valString, code: SetPrototype::method_values, argumentCount: 0)); |
| 161 | defineDefaultProperty(QStringLiteral("keys" ), value: valuesFn); |
| 162 | defineDefaultProperty(QStringLiteral("values" ), value: valuesFn); |
| 163 | |
| 164 | defineDefaultProperty(name: engine->symbol_iterator(), value: valuesFn); |
| 165 | |
| 166 | ScopedString val(scope, engine->newString(s: QLatin1String("Set" ))); |
| 167 | defineReadonlyConfigurableProperty(name: engine->symbol_toStringTag(), value: val); |
| 168 | } |
| 169 | |
| 170 | void Heap::SetObject::init() |
| 171 | { |
| 172 | Object::init(); |
| 173 | esTable = new ESTable(); |
| 174 | } |
| 175 | |
| 176 | void Heap::SetObject::destroy() |
| 177 | { |
| 178 | delete esTable; |
| 179 | esTable = 0; |
| 180 | } |
| 181 | |
| 182 | void Heap::SetObject::removeUnmarkedKeys() |
| 183 | { |
| 184 | esTable->removeUnmarkedKeys(); |
| 185 | } |
| 186 | |
| 187 | void Heap::SetObject::markObjects(Heap::Base *that, MarkStack *markStack) |
| 188 | { |
| 189 | SetObject *s = static_cast<SetObject *>(that); |
| 190 | s->esTable->markObjects(s: markStack, isWeakMap: s->isWeakSet); |
| 191 | Object::markObjects(base: that, stack: markStack); |
| 192 | } |
| 193 | |
| 194 | ReturnedValue SetPrototype::method_add(const FunctionObject *b, const Value *thisObject, const Value *argv, int) |
| 195 | { |
| 196 | Scope scope(b); |
| 197 | Scoped<SetObject> that(scope, thisObject); |
| 198 | if (!that || that->d()->isWeakSet) |
| 199 | return scope.engine->throwTypeError(); |
| 200 | |
| 201 | QV4::WriteBarrier::markCustom(engine: scope.engine, markFunction: [&](QV4::MarkStack *ms) { |
| 202 | if (auto *h = argv[0].heapObject()) |
| 203 | h->mark(markStack: ms); |
| 204 | }); |
| 205 | |
| 206 | that->d()->esTable->set(k: argv[0], v: Value::undefinedValue()); |
| 207 | return that.asReturnedValue(); |
| 208 | } |
| 209 | |
| 210 | ReturnedValue SetPrototype::method_clear(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 211 | { |
| 212 | Scope scope(b); |
| 213 | Scoped<SetObject> that(scope, thisObject); |
| 214 | if (!that || that->d()->isWeakSet) |
| 215 | return scope.engine->throwTypeError(); |
| 216 | |
| 217 | that->d()->esTable->clear(); |
| 218 | return Encode::undefined(); |
| 219 | } |
| 220 | |
| 221 | ReturnedValue SetPrototype::method_delete(const FunctionObject *b, const Value *thisObject, const Value *argv, int) |
| 222 | { |
| 223 | Scope scope(b); |
| 224 | Scoped<SetObject> that(scope, thisObject); |
| 225 | if (!that || that->d()->isWeakSet) |
| 226 | return scope.engine->throwTypeError(); |
| 227 | |
| 228 | return Encode(that->d()->esTable->remove(k: argv[0])); |
| 229 | } |
| 230 | |
| 231 | ReturnedValue SetPrototype::method_entries(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 232 | { |
| 233 | Scope scope(b); |
| 234 | Scoped<SetObject> that(scope, thisObject); |
| 235 | if (!that || that->d()->isWeakSet) |
| 236 | return scope.engine->throwTypeError(); |
| 237 | |
| 238 | Scoped<SetIteratorObject> ao(scope, scope.engine->newSetIteratorObject(o: that)); |
| 239 | ao->d()->iterationKind = IteratorKind::KeyValueIteratorKind; |
| 240 | return ao->asReturnedValue(); |
| 241 | } |
| 242 | |
| 243 | ReturnedValue SetPrototype::method_forEach(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) |
| 244 | { |
| 245 | Scope scope(b); |
| 246 | Scoped<SetObject> that(scope, thisObject); |
| 247 | if (!that || that->d()->isWeakSet) |
| 248 | return scope.engine->throwTypeError(); |
| 249 | |
| 250 | ScopedFunctionObject callbackfn(scope, argv[0]); |
| 251 | if (!callbackfn) |
| 252 | return scope.engine->throwTypeError(); |
| 253 | |
| 254 | ScopedValue thisArg(scope, Value::undefinedValue()); |
| 255 | if (argc > 1) |
| 256 | thisArg = ScopedValue(scope, argv[1]); |
| 257 | |
| 258 | ESTable::ShiftObserver observer{}; |
| 259 | that->d()->esTable->observeShifts(observer); |
| 260 | |
| 261 | Value *arguments = scope.alloc(nValues: 3); |
| 262 | while (observer.pivot < that->d()->esTable->size()) { |
| 263 | that->d()->esTable->iterate(idx: observer.pivot, k: &arguments[0], v: &arguments[1]); // fill in key (0), value (1) |
| 264 | arguments[1] = arguments[0]; // but for set, we want to return the key twice; value is always undefined. |
| 265 | |
| 266 | arguments[2] = that; |
| 267 | callbackfn->call(thisObject: thisArg, argv: arguments, argc: 3); |
| 268 | CHECK_EXCEPTION(); |
| 269 | |
| 270 | observer.next(); |
| 271 | } |
| 272 | |
| 273 | that->d()->esTable->stopObservingShifts(observer); |
| 274 | |
| 275 | return Encode::undefined(); |
| 276 | } |
| 277 | |
| 278 | ReturnedValue SetPrototype::method_has(const FunctionObject *b, const Value *thisObject, const Value *argv, int) |
| 279 | { |
| 280 | Scope scope(b); |
| 281 | Scoped<SetObject> that(scope, thisObject); |
| 282 | if (!that || that->d()->isWeakSet) |
| 283 | return scope.engine->throwTypeError(); |
| 284 | |
| 285 | return Encode(that->d()->esTable->has(k: argv[0])); |
| 286 | } |
| 287 | |
| 288 | ReturnedValue SetPrototype::method_get_size(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 289 | { |
| 290 | Scope scope(b); |
| 291 | Scoped<SetObject> that(scope, thisObject); |
| 292 | if (!that || that->d()->isWeakSet) |
| 293 | return scope.engine->throwTypeError(); |
| 294 | |
| 295 | return Encode(that->d()->esTable->size()); |
| 296 | } |
| 297 | |
| 298 | ReturnedValue SetPrototype::method_values(const FunctionObject *b, const Value *thisObject, const Value *, int) |
| 299 | { |
| 300 | Scope scope(b); |
| 301 | Scoped<SetObject> that(scope, thisObject); |
| 302 | if (!that || that->d()->isWeakSet) |
| 303 | return scope.engine->throwTypeError(); |
| 304 | |
| 305 | Scoped<SetIteratorObject> ao(scope, scope.engine->newSetIteratorObject(o: that)); |
| 306 | ao->d()->iterationKind = IteratorKind::ValueIteratorKind; |
| 307 | return ao->asReturnedValue(); |
| 308 | } |
| 309 | |