| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include <qv4internalclass_p.h> |
| 5 | #include <qv4string_p.h> |
| 6 | #include <qv4engine_p.h> |
| 7 | #include <qv4identifierhash_p.h> |
| 8 | #include "qv4object_p.h" |
| 9 | #include "qv4value_p.h" |
| 10 | #include "qv4mm_p.h" |
| 11 | #include <private/qprimefornumbits_p.h> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | namespace QV4 { |
| 16 | |
| 17 | PropertyHashData::PropertyHashData(int numBits) |
| 18 | : refCount(1) |
| 19 | , size(0) |
| 20 | , numBits(numBits) |
| 21 | { |
| 22 | alloc = qPrimeForNumBits(numBits); |
| 23 | entries = (PropertyHash::Entry *)malloc(size: alloc*sizeof(PropertyHash::Entry)); |
| 24 | memset(s: entries, c: 0, n: alloc*sizeof(PropertyHash::Entry)); |
| 25 | } |
| 26 | |
| 27 | void PropertyHash::addEntry(const PropertyHash::Entry &entry, int classSize) |
| 28 | { |
| 29 | // fill up to max 50% |
| 30 | bool grow = (d->alloc <= d->size*2); |
| 31 | |
| 32 | if (classSize < d->size || grow) |
| 33 | detach(grow, classSize); |
| 34 | |
| 35 | uint idx = entry.identifier.id() % d->alloc; |
| 36 | while (d->entries[idx].identifier.isValid()) { |
| 37 | ++idx; |
| 38 | idx %= d->alloc; |
| 39 | } |
| 40 | d->entries[idx] = entry; |
| 41 | ++d->size; |
| 42 | } |
| 43 | |
| 44 | void PropertyHash::detach(bool grow, int classSize) |
| 45 | { |
| 46 | if (d->refCount == 1 && !grow) |
| 47 | return; |
| 48 | |
| 49 | PropertyHashData *dd = new PropertyHashData(grow ? d->numBits + 1 : d->numBits); |
| 50 | for (int i = 0; i < d->alloc; ++i) { |
| 51 | const Entry &e = d->entries[i]; |
| 52 | if (!e.identifier.isValid() || e.index >= static_cast<unsigned>(classSize)) |
| 53 | continue; |
| 54 | uint idx = e.identifier.id() % dd->alloc; |
| 55 | while (dd->entries[idx].identifier.isValid()) { |
| 56 | ++idx; |
| 57 | idx %= dd->alloc; |
| 58 | } |
| 59 | dd->entries[idx] = e; |
| 60 | } |
| 61 | dd->size = classSize; |
| 62 | if (!--d->refCount) |
| 63 | delete d; |
| 64 | d = dd; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | SharedInternalClassDataPrivate<PropertyKey>::SharedInternalClassDataPrivate(const SharedInternalClassDataPrivate<PropertyKey> &other) |
| 69 | : refcount(1), |
| 70 | engine(other.engine) |
| 71 | { |
| 72 | if (other.alloc()) { |
| 73 | const uint s = other.size(); |
| 74 | data.set(e: engine, newVal: MemberData::allocate(e: engine, n: other.alloc(), old: other.data)); |
| 75 | setSize(s); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | SharedInternalClassDataPrivate<PropertyKey>::SharedInternalClassDataPrivate(const SharedInternalClassDataPrivate<PropertyKey> &other, |
| 80 | uint pos, PropertyKey value) |
| 81 | : refcount(1), |
| 82 | engine(other.engine) |
| 83 | { |
| 84 | data.set(e: engine, newVal: MemberData::allocate(e: engine, n: other.alloc(), old: nullptr)); |
| 85 | memcpy(dest: data, src: other.data, n: sizeof(Heap::MemberData) - sizeof(Value) + pos*sizeof(Value)); |
| 86 | data->values.size = pos + 1; |
| 87 | data->values.set(e: engine, index: pos, v: Value::fromReturnedValue(val: value.id())); |
| 88 | } |
| 89 | |
| 90 | void SharedInternalClassDataPrivate<PropertyKey>::grow() |
| 91 | { |
| 92 | const uint a = alloc() * 2; |
| 93 | const uint s = size(); |
| 94 | data.set(e: engine, newVal: MemberData::allocate(e: engine, n: a, old: data)); |
| 95 | setSize(s); |
| 96 | Q_ASSERT(alloc() >= a); |
| 97 | } |
| 98 | |
| 99 | uint SharedInternalClassDataPrivate<PropertyKey>::alloc() const |
| 100 | { |
| 101 | return data ? data->values.alloc : 0; |
| 102 | } |
| 103 | |
| 104 | uint SharedInternalClassDataPrivate<PropertyKey>::size() const |
| 105 | { |
| 106 | return data ? data->values.size : 0; |
| 107 | } |
| 108 | |
| 109 | void SharedInternalClassDataPrivate<PropertyKey>::setSize(uint s) |
| 110 | { |
| 111 | Q_ASSERT(data && s <= alloc()); |
| 112 | data->values.size = s; |
| 113 | } |
| 114 | |
| 115 | PropertyKey SharedInternalClassDataPrivate<PropertyKey>::at(uint i) const |
| 116 | { |
| 117 | Q_ASSERT(data && i < size()); |
| 118 | return PropertyKey::fromId(id: data->values.values[i].rawValue()); |
| 119 | } |
| 120 | |
| 121 | void SharedInternalClassDataPrivate<PropertyKey>::set(uint i, PropertyKey t) |
| 122 | { |
| 123 | Q_ASSERT(data && i < size()); |
| 124 | QV4::WriteBarrier::markCustom(engine, markFunction: [&](QV4::MarkStack *stack) { |
| 125 | if constexpr (QV4::WriteBarrier::isInsertionBarrier) |
| 126 | if (auto string = t.asStringOrSymbol()) |
| 127 | string->mark(markStack: stack); |
| 128 | }); |
| 129 | data->values.values[i].rawValueRef() = t.id(); |
| 130 | } |
| 131 | |
| 132 | void SharedInternalClassDataPrivate<PropertyKey>::mark(MarkStack *s) |
| 133 | { |
| 134 | if (data) |
| 135 | data->mark(markStack: s); |
| 136 | } |
| 137 | |
| 138 | SharedInternalClassDataPrivate<PropertyAttributes>::SharedInternalClassDataPrivate( |
| 139 | const SharedInternalClassDataPrivate<PropertyAttributes> &other, uint pos, |
| 140 | PropertyAttributes value) |
| 141 | : refcount(1), |
| 142 | m_alloc(qMin(a: other.m_alloc, b: pos + 8)), |
| 143 | m_size(pos + 1), |
| 144 | m_engine(other.m_engine) |
| 145 | { |
| 146 | Q_ASSERT(m_size <= m_alloc); |
| 147 | Q_ASSERT(m_alloc > 0); |
| 148 | |
| 149 | m_engine->memoryManager->changeUnmanagedHeapSizeUsage(delta: m_alloc * sizeof(PropertyAttributes)); |
| 150 | const PropertyAttributes *source = other.m_alloc > NumAttributesInPointer |
| 151 | ? other.m_data |
| 152 | : other.m_inlineData; |
| 153 | PropertyAttributes *target; |
| 154 | if (m_alloc > NumAttributesInPointer) |
| 155 | m_data = target = new PropertyAttributes[m_alloc]; |
| 156 | else |
| 157 | target = m_inlineData; |
| 158 | |
| 159 | memcpy(dest: target, src: source, n: (m_size - 1) * sizeof(PropertyAttributes)); |
| 160 | target[pos] = value; |
| 161 | } |
| 162 | |
| 163 | SharedInternalClassDataPrivate<PropertyAttributes>::SharedInternalClassDataPrivate( |
| 164 | const SharedInternalClassDataPrivate<PropertyAttributes> &other) |
| 165 | : refcount(1), |
| 166 | m_alloc(other.m_alloc), |
| 167 | m_size(other.m_size), |
| 168 | m_engine(other.m_engine) |
| 169 | { |
| 170 | m_engine->memoryManager->changeUnmanagedHeapSizeUsage(delta: m_alloc * sizeof(PropertyAttributes)); |
| 171 | if (m_alloc > NumAttributesInPointer) { |
| 172 | m_data = new PropertyAttributes[m_alloc]; |
| 173 | memcpy(dest: m_data, src: other.m_data, n: m_size*sizeof(PropertyAttributes)); |
| 174 | } else if (m_alloc > 0) { |
| 175 | memcpy(dest: m_inlineData, src: other.m_inlineData, n: m_alloc * sizeof(PropertyAttributes)); |
| 176 | } else { |
| 177 | m_data = nullptr; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | SharedInternalClassDataPrivate<PropertyAttributes>::~SharedInternalClassDataPrivate() |
| 182 | { |
| 183 | m_engine->memoryManager->changeUnmanagedHeapSizeUsage( |
| 184 | delta: -qptrdiff(m_alloc * sizeof(PropertyAttributes))); |
| 185 | if (m_alloc > NumAttributesInPointer) |
| 186 | delete [] m_data; |
| 187 | } |
| 188 | |
| 189 | void SharedInternalClassDataPrivate<PropertyAttributes>::grow() { |
| 190 | uint alloc; |
| 191 | if (!m_alloc) { |
| 192 | alloc = NumAttributesInPointer; |
| 193 | m_engine->memoryManager->changeUnmanagedHeapSizeUsage(delta: alloc * sizeof(PropertyAttributes)); |
| 194 | } else { |
| 195 | // yes, signed. We don't want to deal with stuff > 2G |
| 196 | const uint currentSize = m_alloc * sizeof(PropertyAttributes); |
| 197 | if (currentSize < uint(std::numeric_limits<int>::max() / 2)) |
| 198 | alloc = m_alloc * 2; |
| 199 | else |
| 200 | alloc = std::numeric_limits<int>::max() / sizeof(PropertyAttributes); |
| 201 | |
| 202 | m_engine->memoryManager->changeUnmanagedHeapSizeUsage( |
| 203 | delta: (alloc - m_alloc) * sizeof(PropertyAttributes)); |
| 204 | } |
| 205 | |
| 206 | if (alloc > NumAttributesInPointer) { |
| 207 | auto *n = new PropertyAttributes[alloc]; |
| 208 | if (m_alloc > NumAttributesInPointer) { |
| 209 | memcpy(dest: n, src: m_data, n: m_alloc * sizeof(PropertyAttributes)); |
| 210 | delete [] m_data; |
| 211 | } else if (m_alloc > 0) { |
| 212 | memcpy(dest: n, src: m_inlineData, n: m_alloc * sizeof(PropertyAttributes)); |
| 213 | } |
| 214 | m_data = n; |
| 215 | } |
| 216 | m_alloc = alloc; |
| 217 | } |
| 218 | |
| 219 | namespace Heap { |
| 220 | |
| 221 | void InternalClass::init(ExecutionEngine *engine) |
| 222 | { |
| 223 | // InternalClass is automatically zeroed during allocation: |
| 224 | // prototype = nullptr; |
| 225 | // parent = nullptr; |
| 226 | // size = 0; |
| 227 | // numRedundantTransitions = 0; |
| 228 | // flags = 0; |
| 229 | |
| 230 | Base::init(); |
| 231 | new (&propertyTable) PropertyHash(); |
| 232 | new (&nameMap) SharedInternalClassData<PropertyKey>(engine); |
| 233 | new (&propertyData) SharedInternalClassData<PropertyAttributes>(engine); |
| 234 | new (&transitions) QVarLengthArray<Transition, 1>(); |
| 235 | |
| 236 | this->engine = engine; |
| 237 | vtable = QV4::InternalClass::staticVTable(); |
| 238 | protoId = engine->newProtoId(); |
| 239 | |
| 240 | // Also internal classes need an internal class pointer. Simply make it point to itself |
| 241 | internalClass.set(e: engine, newVal: this); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | void InternalClass::init(Heap::InternalClass *other) |
| 246 | { |
| 247 | Base::init(); |
| 248 | new (&propertyTable) PropertyHash(other->propertyTable); |
| 249 | new (&nameMap) SharedInternalClassData<PropertyKey>(other->nameMap); |
| 250 | new (&propertyData) SharedInternalClassData<PropertyAttributes>(other->propertyData); |
| 251 | new (&transitions) QVarLengthArray<Transition, 1>(); |
| 252 | |
| 253 | engine = other->engine; |
| 254 | vtable = other->vtable; |
| 255 | prototype = other->prototype; |
| 256 | parent = other; |
| 257 | size = other->size; |
| 258 | numRedundantTransitions = other->numRedundantTransitions; |
| 259 | flags = other->flags; |
| 260 | protoId = engine->newProtoId(); |
| 261 | |
| 262 | internalClass.set(e: engine, newVal: other->internalClass); |
| 263 | QV4::WriteBarrier::markCustom(engine, markFunction: [&](QV4::MarkStack *stack) { |
| 264 | if constexpr (QV4::WriteBarrier::isInsertionBarrier) { |
| 265 | other->mark(markStack: stack); |
| 266 | } |
| 267 | }); |
| 268 | } |
| 269 | |
| 270 | void InternalClass::destroy() |
| 271 | { |
| 272 | for (const auto &t : transitions) { |
| 273 | if (t.lookup) { |
| 274 | #ifndef QT_NO_DEBUG |
| 275 | Q_ASSERT(t.lookup->parent == this); |
| 276 | #endif |
| 277 | t.lookup->parent = nullptr; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (parent && parent->engine && parent->isMarked()) |
| 282 | parent->removeChildEntry(child: this); |
| 283 | |
| 284 | propertyTable.~PropertyHash(); |
| 285 | nameMap.~SharedInternalClassData<PropertyKey>(); |
| 286 | propertyData.~SharedInternalClassData<PropertyAttributes>(); |
| 287 | transitions.~QVarLengthArray<Transition, 1>(); |
| 288 | engine = nullptr; |
| 289 | Base::destroy(); |
| 290 | } |
| 291 | |
| 292 | ReturnedValue InternalClass::keyAt(uint index) const |
| 293 | { |
| 294 | PropertyKey key = nameMap.at(i: index); |
| 295 | if (!key.isValid()) |
| 296 | return Encode::undefined(); |
| 297 | if (key.isArrayIndex()) |
| 298 | return Encode(key.asArrayIndex()); |
| 299 | Q_ASSERT(key.isStringOrSymbol()); |
| 300 | return key.asStringOrSymbol()->asReturnedValue(); |
| 301 | } |
| 302 | |
| 303 | void InternalClass::changeMember(QV4::Object *object, PropertyKey id, PropertyAttributes data, InternalClassEntry *entry) |
| 304 | { |
| 305 | Q_ASSERT(id.isStringOrSymbol()); |
| 306 | |
| 307 | Heap::InternalClass *oldClass = object->internalClass(); |
| 308 | Heap::InternalClass *newClass = oldClass->changeMember(identifier: id, data, entry); |
| 309 | object->setInternalClass(newClass); |
| 310 | } |
| 311 | |
| 312 | InternalClassTransition &InternalClass::lookupOrInsertTransition(const InternalClassTransition &t) |
| 313 | { |
| 314 | QVarLengthArray<Transition, 1>::iterator it = std::lower_bound(first: transitions.begin(), last: transitions.end(), val: t); |
| 315 | if (it != transitions.end() && *it == t) { |
| 316 | return *it; |
| 317 | } else { |
| 318 | it = transitions.insert(before: it, x: t); |
| 319 | return *it; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void addDummyEntry(InternalClass *newClass, PropertyHash::Entry e) |
| 324 | { |
| 325 | // add a dummy entry, since we need two entries for accessors |
| 326 | newClass->propertyTable.addEntry(entry: e, classSize: newClass->size); |
| 327 | newClass->nameMap.add(pos: newClass->size, value: PropertyKey::invalid()); |
| 328 | newClass->propertyData.add(pos: newClass->size, value: PropertyAttributes()); |
| 329 | ++newClass->size; |
| 330 | } |
| 331 | |
| 332 | static PropertyAttributes attributesFromFlags(int flags) |
| 333 | { |
| 334 | PropertyAttributes attributes; |
| 335 | attributes.m_all = uchar(flags); |
| 336 | return attributes; |
| 337 | } |
| 338 | |
| 339 | static Heap::InternalClass *cleanInternalClass(Heap::InternalClass *orig) |
| 340 | { |
| 341 | if (++orig->numRedundantTransitions < Heap::InternalClass::MaxRedundantTransitions) |
| 342 | return orig; |
| 343 | |
| 344 | // We will generally add quite a few transitions here. We have 255 redundant ones. |
| 345 | // We can expect at least as many significant ones in addition. |
| 346 | QVarLengthArray<InternalClassTransition, 1> transitions; |
| 347 | |
| 348 | Scope scope(orig->engine); |
| 349 | Scoped<QV4::InternalClass> child(scope, orig); |
| 350 | |
| 351 | { |
| 352 | quint8 remainingRedundantTransitions = orig->numRedundantTransitions; |
| 353 | QSet<PropertyKey> properties; |
| 354 | int structureChanges = 0; |
| 355 | |
| 356 | Scoped<QV4::InternalClass> parent(scope, orig->parent); |
| 357 | while (parent && remainingRedundantTransitions > 0) { |
| 358 | Q_ASSERT(child->d() != scope.engine->classes[ExecutionEngine::Class_Empty]); |
| 359 | const auto it = std::find_if( |
| 360 | first: parent->d()->transitions.begin(), last: parent->d()->transitions.end(), |
| 361 | pred: [&child](const InternalClassTransition &t) { |
| 362 | return child->d() == t.lookup; |
| 363 | }); |
| 364 | Q_ASSERT(it != parent->d()->transitions.end()); |
| 365 | |
| 366 | if (it->flags & InternalClassTransition::StructureChange) { |
| 367 | // A structural change. Each kind of structural change has to be recorded only once. |
| 368 | if ((structureChanges & it->flags) != it->flags) { |
| 369 | transitions.push_back(t: *it); |
| 370 | structureChanges |= it->flags; |
| 371 | } else { |
| 372 | --remainingRedundantTransitions; |
| 373 | } |
| 374 | } else if (!properties.contains(value: it->id)) { |
| 375 | // We only need the final state of the property. |
| 376 | properties.insert(value: it->id); |
| 377 | |
| 378 | // Property removal creates _two_ redundant transitions. |
| 379 | // We don't have to replay either, but numRedundantTransitions only records one. |
| 380 | if (it->flags != 0) |
| 381 | transitions.push_back(t: *it); |
| 382 | } else { |
| 383 | --remainingRedundantTransitions; |
| 384 | } |
| 385 | |
| 386 | child = parent->d(); |
| 387 | parent = child->d()->parent; |
| 388 | Q_ASSERT(child->d() != parent->d()); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | for (auto it = transitions.rbegin(); it != transitions.rend(); ++it) { |
| 393 | switch (it->flags) { |
| 394 | case InternalClassTransition::NotExtensible: |
| 395 | child = child->d()->nonExtensible(); |
| 396 | continue; |
| 397 | case InternalClassTransition::VTableChange: |
| 398 | child = child->d()->changeVTable(vt: it->vtable); |
| 399 | continue; |
| 400 | case InternalClassTransition::PrototypeChange: |
| 401 | child = child->d()->changePrototype(proto: it->prototype); |
| 402 | continue; |
| 403 | case InternalClassTransition::ProtoClass: |
| 404 | child = child->d()->asProtoClass(); |
| 405 | continue; |
| 406 | case InternalClassTransition::Sealed: |
| 407 | child = child->d()->sealed(); |
| 408 | continue; |
| 409 | case InternalClassTransition::Frozen: |
| 410 | child = child->d()->frozen(); |
| 411 | continue; |
| 412 | case InternalClassTransition::Locked: |
| 413 | child = child->d()->locked(); |
| 414 | continue; |
| 415 | default: |
| 416 | Q_ASSERT(it->flags != 0); |
| 417 | Q_ASSERT(it->flags < InternalClassTransition::StructureChange); |
| 418 | child = child->addMember(identifier: it->id, data: attributesFromFlags(flags: it->flags)); |
| 419 | continue; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return child->d(); |
| 424 | } |
| 425 | |
| 426 | Heap::InternalClass *InternalClass::changeMember( |
| 427 | PropertyKey identifier, PropertyAttributes data, InternalClassEntry *entry) |
| 428 | { |
| 429 | if (!data.isEmpty()) |
| 430 | data.resolve(); |
| 431 | PropertyHash::Entry *e = findEntry(id: identifier); |
| 432 | Q_ASSERT(e && e->index != UINT_MAX); |
| 433 | uint idx = e->index; |
| 434 | Q_ASSERT(idx != UINT_MAX); |
| 435 | |
| 436 | if (entry) { |
| 437 | entry->index = idx; |
| 438 | entry->setterIndex = e->setterIndex; |
| 439 | entry->attributes = data; |
| 440 | } |
| 441 | |
| 442 | if (data == propertyData.at(i: idx)) |
| 443 | return this; |
| 444 | |
| 445 | Transition temp = { { .id: identifier }, .lookup: nullptr, .flags: int(data.all()) }; |
| 446 | Transition &t = lookupOrInsertTransition(t: temp); |
| 447 | if (t.lookup) |
| 448 | return t.lookup; |
| 449 | |
| 450 | // create a new class and add it to the tree |
| 451 | Scope scope(engine); |
| 452 | Scoped<QV4::InternalClass> scopedNewClass(scope, engine->newClass(other: this)); |
| 453 | auto newClass = scopedNewClass->d(); |
| 454 | if (data.isAccessor() && e->setterIndex == UINT_MAX) { |
| 455 | Q_ASSERT(!propertyData.at(idx).isAccessor()); |
| 456 | |
| 457 | // add a dummy entry for the accessor |
| 458 | if (entry) |
| 459 | entry->setterIndex = newClass->size; |
| 460 | e->setterIndex = newClass->size; |
| 461 | addDummyEntry(newClass, e: *e); |
| 462 | } |
| 463 | |
| 464 | newClass->propertyData.set(pos: idx, value: data); |
| 465 | |
| 466 | t.lookup = newClass; |
| 467 | Q_ASSERT(t.lookup); |
| 468 | |
| 469 | return cleanInternalClass(orig: newClass); |
| 470 | } |
| 471 | |
| 472 | Heap::InternalClass *InternalClass::changePrototypeImpl(Heap::Object *proto) |
| 473 | { |
| 474 | Scope scope(engine); |
| 475 | ScopedValue protectThis(scope, this); |
| 476 | if (proto) |
| 477 | proto->setUsedAsProto(); |
| 478 | Q_ASSERT(prototype != proto); |
| 479 | Q_ASSERT(!proto || proto->internalClass->isUsedAsProto()); |
| 480 | |
| 481 | Transition temp = { { .id: PropertyKey::invalid() }, .lookup: nullptr, .flags: Transition::PrototypeChange }; |
| 482 | temp.prototype = proto; |
| 483 | |
| 484 | Transition &t = lookupOrInsertTransition(t: temp); |
| 485 | if (t.lookup) |
| 486 | return t.lookup; |
| 487 | |
| 488 | // create a new class and add it to the tree |
| 489 | Scoped<QV4::InternalClass> scopedNewClass(scope, engine->newClass(other: this)); |
| 490 | auto newClass = scopedNewClass->d(); |
| 491 | QV4::WriteBarrier::markCustom(engine, markFunction: [&](QV4::MarkStack *stack) { |
| 492 | if (proto && QV4::WriteBarrier::isInsertionBarrier) |
| 493 | proto->mark(markStack: stack); |
| 494 | }); |
| 495 | newClass->prototype = proto; |
| 496 | |
| 497 | t.lookup = newClass; |
| 498 | return prototype ? cleanInternalClass(orig: newClass) : newClass; |
| 499 | } |
| 500 | |
| 501 | Heap::InternalClass *InternalClass::changeVTableImpl(const VTable *vt) |
| 502 | { |
| 503 | Q_ASSERT(vtable != vt); |
| 504 | |
| 505 | Transition temp = { { .id: PropertyKey::invalid() }, .lookup: nullptr, .flags: Transition::VTableChange }; |
| 506 | temp.vtable = vt; |
| 507 | |
| 508 | Transition &t = lookupOrInsertTransition(t: temp); |
| 509 | if (t.lookup) |
| 510 | return t.lookup; |
| 511 | |
| 512 | // create a new class and add it to the tree |
| 513 | Scope scope(engine); |
| 514 | Scoped<QV4::InternalClass> scopedNewClass(scope, engine->newClass(other: this)); |
| 515 | auto newClass = scopedNewClass->d(); |
| 516 | newClass->vtable = vt; |
| 517 | |
| 518 | t.lookup = newClass; |
| 519 | Q_ASSERT(t.lookup); |
| 520 | Q_ASSERT(newClass->vtable); |
| 521 | return vtable == QV4::InternalClass::staticVTable() |
| 522 | ? newClass |
| 523 | : cleanInternalClass(orig: newClass); |
| 524 | } |
| 525 | |
| 526 | Heap::InternalClass *InternalClass::nonExtensible() |
| 527 | { |
| 528 | if (!isExtensible()) |
| 529 | return this; |
| 530 | |
| 531 | Transition temp = { { .id: PropertyKey::invalid() }, .lookup: nullptr, .flags: Transition::NotExtensible}; |
| 532 | Transition &t = lookupOrInsertTransition(t: temp); |
| 533 | if (t.lookup) |
| 534 | return t.lookup; |
| 535 | |
| 536 | Scope scope(engine); |
| 537 | Scoped<QV4::InternalClass> scopedNewClass(scope, engine->newClass(other: this)); |
| 538 | auto newClass = scopedNewClass->d(); |
| 539 | newClass->flags |= NotExtensible; |
| 540 | |
| 541 | t.lookup = newClass; |
| 542 | Q_ASSERT(t.lookup); |
| 543 | return newClass; |
| 544 | } |
| 545 | |
| 546 | InternalClass *InternalClass::locked() |
| 547 | { |
| 548 | if (isLocked()) |
| 549 | return this; |
| 550 | |
| 551 | Transition temp = { { .id: PropertyKey::invalid() }, .lookup: nullptr, .flags: Transition::Locked}; |
| 552 | Transition &t = lookupOrInsertTransition(t: temp); |
| 553 | if (t.lookup) |
| 554 | return t.lookup; |
| 555 | |
| 556 | Scope scope(engine); |
| 557 | Scoped<QV4::InternalClass> scopedNewClass(scope, engine->newClass(other: this)); |
| 558 | auto newClass = scopedNewClass->d(); |
| 559 | newClass->flags |= Locked; |
| 560 | |
| 561 | t.lookup = newClass; |
| 562 | Q_ASSERT(t.lookup); |
| 563 | return newClass; |
| 564 | } |
| 565 | |
| 566 | void InternalClass::addMember(QV4::Object *object, PropertyKey id, PropertyAttributes data, InternalClassEntry *entry) |
| 567 | { |
| 568 | Heap::InternalClass *oldClass = object->internalClass(); |
| 569 | Heap::InternalClass *newClass = oldClass->addMember(identifier: id, data, entry); |
| 570 | if (newClass != oldClass) |
| 571 | object->setInternalClass(newClass); |
| 572 | } |
| 573 | |
| 574 | Heap::InternalClass *InternalClass::addMember(PropertyKey identifier, PropertyAttributes data, InternalClassEntry *entry) |
| 575 | { |
| 576 | Q_ASSERT(identifier.isStringOrSymbol()); |
| 577 | if (!data.isEmpty()) |
| 578 | data.resolve(); |
| 579 | |
| 580 | PropertyHash::Entry *e = findEntry(id: identifier); |
| 581 | if (e) |
| 582 | return changeMember(identifier, data, entry); |
| 583 | |
| 584 | return addMemberImpl(identifier, data, entry); |
| 585 | } |
| 586 | |
| 587 | Heap::InternalClass *InternalClass::addMemberImpl(PropertyKey identifier, PropertyAttributes data, InternalClassEntry *entry) |
| 588 | { |
| 589 | Transition temp = { { .id: identifier }, .lookup: nullptr, .flags: int(data.all()) }; |
| 590 | Transition &t = lookupOrInsertTransition(t: temp); |
| 591 | |
| 592 | if (entry) { |
| 593 | entry->index = size; |
| 594 | entry->setterIndex = data.isAccessor() ? size + 1 : UINT_MAX; |
| 595 | entry->attributes = data; |
| 596 | } |
| 597 | |
| 598 | if (t.lookup) |
| 599 | return t.lookup; |
| 600 | |
| 601 | // create a new class and add it to the tree |
| 602 | Scope scope(engine); |
| 603 | Scoped<QV4::InternalClass> ic(scope, engine->newClass(other: this)); |
| 604 | InternalClass *newClass = ic->d(); |
| 605 | PropertyHash::Entry e = { .identifier: identifier, .index: newClass->size, .setterIndex: data.isAccessor() ? newClass->size + 1 : UINT_MAX }; |
| 606 | newClass->propertyTable.addEntry(entry: e, classSize: newClass->size); |
| 607 | |
| 608 | newClass->nameMap.add(pos: newClass->size, value: identifier); |
| 609 | newClass->propertyData.add(pos: newClass->size, value: data); |
| 610 | ++newClass->size; |
| 611 | if (data.isAccessor()) |
| 612 | addDummyEntry(newClass, e); |
| 613 | |
| 614 | t.lookup = newClass; |
| 615 | Q_ASSERT(t.lookup); |
| 616 | return newClass; |
| 617 | } |
| 618 | |
| 619 | void InternalClass::removeChildEntry(InternalClass *child) |
| 620 | { |
| 621 | Q_ASSERT(engine); |
| 622 | for (auto &t : transitions) { |
| 623 | if (t.lookup == child) { |
| 624 | t.lookup = nullptr; |
| 625 | return; |
| 626 | } |
| 627 | } |
| 628 | Q_UNREACHABLE(); |
| 629 | |
| 630 | } |
| 631 | |
| 632 | void InternalClass::removeMember(QV4::Object *object, PropertyKey identifier) |
| 633 | { |
| 634 | #ifndef QT_NO_DEBUG |
| 635 | Heap::InternalClass *oldClass = object->internalClass(); |
| 636 | Q_ASSERT(oldClass->findEntry(identifier) != nullptr); |
| 637 | #endif |
| 638 | |
| 639 | changeMember(object, id: identifier, data: Attr_Invalid); |
| 640 | |
| 641 | #ifndef QT_NO_DEBUG |
| 642 | // We didn't remove the data slot, just made it inaccessible. |
| 643 | // ... unless we've rebuilt the whole class. Then all the deleted properties are gone. |
| 644 | Q_ASSERT(object->internalClass()->numRedundantTransitions == 0 |
| 645 | || object->internalClass()->size == oldClass->size); |
| 646 | #endif |
| 647 | } |
| 648 | |
| 649 | Heap::InternalClass *InternalClass::sealed() |
| 650 | { |
| 651 | if (isSealed()) |
| 652 | return this; |
| 653 | |
| 654 | Transition temp = { { .id: PropertyKey::invalid() }, .lookup: nullptr, .flags: InternalClassTransition::Sealed }; |
| 655 | Transition &t = lookupOrInsertTransition(t: temp); |
| 656 | |
| 657 | if (t.lookup) { |
| 658 | Q_ASSERT(t.lookup && t.lookup->isSealed()); |
| 659 | return t.lookup; |
| 660 | } |
| 661 | |
| 662 | Scope scope(engine); |
| 663 | Scoped<QV4::InternalClass> ic(scope, engine->newClass(other: this)); |
| 664 | Heap::InternalClass *s = ic->d(); |
| 665 | |
| 666 | if (!isFrozen()) { // freezing also makes all properties non-configurable |
| 667 | for (uint i = 0; i < size; ++i) { |
| 668 | PropertyAttributes attrs = propertyData.at(i); |
| 669 | if (attrs.isEmpty()) |
| 670 | continue; |
| 671 | attrs.setConfigurable(false); |
| 672 | s->propertyData.set(pos: i, value: attrs); |
| 673 | } |
| 674 | } |
| 675 | s->flags |= Sealed; |
| 676 | |
| 677 | t.lookup = s; |
| 678 | return s; |
| 679 | } |
| 680 | |
| 681 | Heap::InternalClass *InternalClass::frozen() |
| 682 | { |
| 683 | if (isFrozen()) |
| 684 | return this; |
| 685 | |
| 686 | Transition temp = { { .id: PropertyKey::invalid() }, .lookup: nullptr, .flags: InternalClassTransition::Frozen }; |
| 687 | Transition &t = lookupOrInsertTransition(t: temp); |
| 688 | |
| 689 | if (t.lookup) { |
| 690 | Q_ASSERT(t.lookup && t.lookup->isFrozen()); |
| 691 | return t.lookup; |
| 692 | } |
| 693 | |
| 694 | Scope scope(engine); |
| 695 | Scoped<QV4::InternalClass> ic(scope, engine->newClass(other: this)); |
| 696 | Heap::InternalClass *f = ic->d(); |
| 697 | |
| 698 | for (uint i = 0; i < size; ++i) { |
| 699 | PropertyAttributes attrs = propertyData.at(i); |
| 700 | if (attrs.isEmpty()) |
| 701 | continue; |
| 702 | if (attrs.isData()) |
| 703 | attrs.setWritable(false); |
| 704 | attrs.setConfigurable(false); |
| 705 | f->propertyData.set(pos: i, value: attrs); |
| 706 | } |
| 707 | f->flags |= Frozen; |
| 708 | |
| 709 | t.lookup = f; |
| 710 | return f; |
| 711 | } |
| 712 | |
| 713 | InternalClass *InternalClass::canned() |
| 714 | { |
| 715 | // scope the intermediate result to prevent it from getting garbage collected |
| 716 | Scope scope(engine); |
| 717 | Scoped<QV4::InternalClass> ic(scope, sealed()); |
| 718 | return ic->d()->nonExtensible(); |
| 719 | } |
| 720 | |
| 721 | InternalClass *InternalClass::cryopreserved() |
| 722 | { |
| 723 | // scope the intermediate result to prevent it from getting garbage collected |
| 724 | Scope scope(engine); |
| 725 | Scoped<QV4::InternalClass> ic(scope, frozen()); |
| 726 | return ic->d()->canned(); |
| 727 | } |
| 728 | |
| 729 | bool InternalClass::isImplicitlyFrozen() const |
| 730 | { |
| 731 | if (isFrozen()) |
| 732 | return true; |
| 733 | |
| 734 | for (uint i = 0; i < size; ++i) { |
| 735 | const PropertyAttributes attrs = propertyData.at(i); |
| 736 | if (attrs.isEmpty()) |
| 737 | continue; |
| 738 | if ((attrs.isData() && attrs.isWritable()) || attrs.isConfigurable()) |
| 739 | return false; |
| 740 | } |
| 741 | |
| 742 | return true; |
| 743 | } |
| 744 | |
| 745 | Heap::InternalClass *InternalClass::asProtoClass() |
| 746 | { |
| 747 | if (isUsedAsProto()) |
| 748 | return this; |
| 749 | |
| 750 | Transition temp = { { .id: PropertyKey::invalid() }, .lookup: nullptr, .flags: Transition::ProtoClass }; |
| 751 | Transition &t = lookupOrInsertTransition(t: temp); |
| 752 | if (t.lookup) |
| 753 | return t.lookup; |
| 754 | |
| 755 | Scope scope(engine); |
| 756 | Scoped<QV4::InternalClass> scopedNewClass(scope, engine->newClass(other: this)); |
| 757 | auto newClass = scopedNewClass->d(); |
| 758 | newClass->flags |= UsedAsProto; |
| 759 | |
| 760 | t.lookup = newClass; |
| 761 | Q_ASSERT(t.lookup); |
| 762 | return newClass; |
| 763 | } |
| 764 | |
| 765 | static void updateProtoUsage(Heap::Object *o, Heap::InternalClass *ic) |
| 766 | { |
| 767 | if (ic->prototype == o) |
| 768 | ic->protoId = ic->engine->newProtoId(); |
| 769 | for (auto &t : ic->transitions) { |
| 770 | if (t.lookup) |
| 771 | updateProtoUsage(o, ic: t.lookup); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | |
| 776 | void InternalClass::updateProtoUsage(Heap::Object *o) |
| 777 | { |
| 778 | Q_ASSERT(isUsedAsProto()); |
| 779 | Heap::InternalClass *ic = engine->internalClasses(icType: EngineBase::Class_Empty); |
| 780 | Q_ASSERT(!ic->prototype); |
| 781 | |
| 782 | Heap::updateProtoUsage(o, ic); |
| 783 | } |
| 784 | |
| 785 | void InternalClass::markObjects(Heap::Base *b, MarkStack *stack) |
| 786 | { |
| 787 | Heap::InternalClass *ic = static_cast<Heap::InternalClass *>(b); |
| 788 | if (ic->prototype) |
| 789 | ic->prototype->mark(markStack: stack); |
| 790 | |
| 791 | if (ic->parent) |
| 792 | ic->parent->mark(markStack: stack); |
| 793 | |
| 794 | ic->nameMap.mark(s: stack); |
| 795 | } |
| 796 | |
| 797 | } |
| 798 | |
| 799 | } |
| 800 | |
| 801 | QT_END_NAMESPACE |
| 802 | |