| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "vm/PropMap-inl.h" |
| 8 |
|
| 9 |
#include "gc/HashUtil.h" |
| 10 |
#include "js/GCVector.h" |
| 11 |
#include "js/Printer.h" // js::GenericPrinter, js::Fprinter |
| 12 |
#include "vm/JSObject.h" |
| 13 |
#include "vm/JSONPrinter.h" // JSONPrinter |
| 14 |
|
| 15 |
#include "gc/GCContext-inl.h" |
| 16 |
#include "gc/Marking-inl.h" |
| 17 |
#include "vm/JSContext-inl.h" |
| 18 |
#include "vm/ObjectFlags-inl.h" |
| 19 |
|
| 20 |
using namespace js; |
| 21 |
|
| 22 |
void PropMap::addSizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 23 |
size_t* children, size_t* tables) const { |
| 24 |
if (isShared() && asShared()->hasChildrenSet()) { |
| 25 |
auto* set = asShared()->treeDataRef().children.toChildrenSet(); |
| 26 |
*children += set->shallowSizeOfIncludingThis(mallocSizeOf); |
| 27 |
} |
| 28 |
if (canHaveTable() && asLinked()->hasTable()) { |
| 29 |
*tables += asLinked()->data_.table->sizeOfIncludingThis(mallocSizeOf); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// static |
| 34 |
SharedPropMap* SharedPropMap::create(JSContext* cx, Handle<SharedPropMap*> prev, |
| 35 |
HandleId id, PropertyInfo prop) { |
| 36 |
// If the first property has a slot number <= MaxSlotNumber, all properties |
| 37 |
// added later will have a slot number <= CompactPropertyInfo::MaxSlotNumber |
| 38 |
// so we can use a CompactPropMap. |
| 39 |
static constexpr size_t MaxFirstSlot = |
| 40 |
CompactPropertyInfo::MaxSlotNumber - (PropMap::Capacity - 1); |
| 41 |
|
| 42 |
if (!prev && prop.maybeSlot() <= MaxFirstSlot) { |
| 43 |
return cx->newCell<CompactPropMap>(id, prop); |
| 44 |
} |
| 45 |
|
| 46 |
return cx->newCell<NormalPropMap>(prev, id, prop); |
| 47 |
} |
| 48 |
|
| 49 |
// static |
| 50 |
SharedPropMap* SharedPropMap::createInitial(JSContext* cx, HandleId id, |
| 51 |
PropertyInfo prop) { |
| 52 |
// Lookup or create a shared map based on the first property. |
| 53 |
|
| 54 |
using Lookup = InitialPropMapHasher::Lookup; |
| 55 |
|
| 56 |
auto& table = cx->zone()->shapeZone().initialPropMaps; |
| 57 |
|
| 58 |
auto p = MakeDependentAddPtr(cx, table, Lookup(id, prop)); |
| 59 |
if (p) { |
| 60 |
return *p; |
| 61 |
} |
| 62 |
|
| 63 |
SharedPropMap* result = create(cx, /* prev = */ nullptr, id, prop); |
| 64 |
if (!result) { |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
Lookup lookup(id, prop); |
| 69 |
if (!p.add(cx, table, lookup, result)) { |
| 70 |
return nullptr; |
| 71 |
} |
| 72 |
|
| 73 |
return result; |
| 74 |
} |
| 75 |
|
| 76 |
// static |
| 77 |
SharedPropMap* SharedPropMap::clone(JSContext* cx, Handle<SharedPropMap*> map, |
| 78 |
uint32_t length) { |
| 79 |
MOZ_ASSERT(length > 0); |
| 80 |
|
| 81 |
if (map->isCompact()) { |
| 82 |
Rooted<CompactPropMap*> prev(cx, map->asCompact()); |
| 83 |
return cx->newCell<CompactPropMap>(prev, length); |
| 84 |
} |
| 85 |
|
| 86 |
Rooted<NormalPropMap*> prev(cx, map->asNormal()); |
| 87 |
return cx->newCell<NormalPropMap>(prev, length); |
| 88 |
} |
| 89 |
|
| 90 |
// static |
| 91 |
DictionaryPropMap* SharedPropMap::toDictionaryMap(JSContext* cx, |
| 92 |
Handle<SharedPropMap*> map, |
| 93 |
uint32_t length) { |
| 94 |
// Starting at the last map, clone each shared map to a new dictionary map. |
| 95 |
|
| 96 |
Rooted<DictionaryPropMap*> lastDictMap(cx); |
| 97 |
Rooted<DictionaryPropMap*> nextDictMap(cx); |
| 98 |
|
| 99 |
Rooted<SharedPropMap*> sharedMap(cx, map); |
| 100 |
uint32_t sharedLength = length; |
| 101 |
while (true) { |
| 102 |
sharedMap->setHadDictionaryConversion(); |
| 103 |
|
| 104 |
DictionaryPropMap* dictMap; |
| 105 |
if (sharedMap->isCompact()) { |
| 106 |
Rooted<CompactPropMap*> prev(cx, sharedMap->asCompact()); |
| 107 |
dictMap = cx->newCell<DictionaryPropMap>(prev, sharedLength); |
| 108 |
} else { |
| 109 |
Rooted<NormalPropMap*> prev(cx, sharedMap->asNormal()); |
| 110 |
dictMap = cx->newCell<DictionaryPropMap>(prev, sharedLength); |
| 111 |
} |
| 112 |
if (!dictMap) { |
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
|
| 116 |
if (!lastDictMap) { |
| 117 |
lastDictMap = dictMap; |
| 118 |
} |
| 119 |
|
| 120 |
if (nextDictMap) { |
| 121 |
nextDictMap->initPrevious(dictMap); |
| 122 |
} |
| 123 |
nextDictMap = dictMap; |
| 124 |
|
| 125 |
if (!sharedMap->hasPrevious()) { |
| 126 |
break; |
| 127 |
} |
| 128 |
sharedMap = sharedMap->asNormal()->previous(); |
| 129 |
sharedLength = PropMap::Capacity; |
| 130 |
} |
| 131 |
|
| 132 |
return lastDictMap; |
| 133 |
} |
| 134 |
|
| 135 |
static MOZ_ALWAYS_INLINE SharedPropMap* PropMapChildReadBarrier( |
| 136 |
SharedPropMap* parent, SharedPropMap* child) { |
| 137 |
JS::Zone* zone = child->zone(); |
| 138 |
if (zone->needsIncrementalBarrier()) { |
| 139 |
// We need a read barrier for the map tree, since these are weak |
| 140 |
// pointers. |
| 141 |
ReadBarrier(child); |
| 142 |
return child; |
| 143 |
} |
| 144 |
|
| 145 |
if (MOZ_UNLIKELY(zone->isGCSweeping() && |
| 146 |
IsAboutToBeFinalizedUnbarriered(child))) { |
| 147 |
// The map we've found is unreachable and due to be finalized, so |
| 148 |
// remove our weak reference to it and don't use it. |
| 149 |
MOZ_ASSERT(parent->isMarkedAny()); |
| 150 |
parent->removeChild(zone->runtimeFromMainThread()->gcContext(), child); |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
|
| 154 |
// We don't yield to the mutator when the zone is in this state so we don't |
| 155 |
// need to account for it here. |
| 156 |
MOZ_ASSERT(!zone->isGCCompacting()); |
| 157 |
|
| 158 |
return child; |
| 159 |
} |
| 160 |
|
| 161 |
SharedPropMap* SharedPropMap::lookupChild(uint32_t length, HandleId id, |
| 162 |
PropertyInfo prop) { |
| 163 |
MOZ_ASSERT(length > 0); |
| 164 |
|
| 165 |
SharedChildrenPtr children = treeDataRef().children; |
| 166 |
if (children.isNone()) { |
| 167 |
return nullptr; |
| 168 |
} |
| 169 |
|
| 170 |
if (!hasChildrenSet()) { |
| 171 |
SharedPropMapAndIndex prevChild = children.toSingleChild(); |
| 172 |
if (prevChild.index() == length - 1) { |
| 173 |
SharedPropMap* child = prevChild.map(); |
| 174 |
uint32_t newPropIndex = indexOfNextProperty(length - 1); |
| 175 |
if (child->matchProperty(newPropIndex, id, prop)) { |
| 176 |
return PropMapChildReadBarrier(this, child); |
| 177 |
} |
| 178 |
} |
| 179 |
return nullptr; |
| 180 |
} |
| 181 |
|
| 182 |
SharedChildrenSet* set = children.toChildrenSet(); |
| 183 |
SharedChildrenHasher::Lookup lookup(id, prop, length - 1); |
| 184 |
if (auto p = set->lookup(lookup)) { |
| 185 |
MOZ_ASSERT(p->index() == length - 1); |
| 186 |
SharedPropMap* child = p->map(); |
| 187 |
return PropMapChildReadBarrier(this, child); |
| 188 |
} |
| 189 |
return nullptr; |
| 190 |
} |
| 191 |
|
| 192 |
bool SharedPropMap::addChild(JSContext* cx, SharedPropMapAndIndex child, |
| 193 |
HandleId id, PropertyInfo prop) { |
| 194 |
SharedPropMap* childMap = child.map(); |
| 195 |
|
| 196 |
#ifdef DEBUG |
| 197 |
// If the parent map was full, the child map must have the parent as |
| 198 |
// |previous| map. Else, the parent and child have the same |previous| map. |
| 199 |
if (childMap->hasPrevious()) { |
| 200 |
if (child.index() == PropMap::Capacity - 1) { |
| 201 |
MOZ_ASSERT(childMap->asLinked()->previous() == this); |
| 202 |
} else { |
| 203 |
MOZ_ASSERT(childMap->asLinked()->previous() == asLinked()->previous()); |
| 204 |
} |
| 205 |
} else { |
| 206 |
MOZ_ASSERT(!hasPrevious()); |
| 207 |
} |
| 208 |
#endif |
| 209 |
|
| 210 |
SharedChildrenPtr& childrenRef = treeDataRef().children; |
| 211 |
|
| 212 |
if (childrenRef.isNone()) { |
| 213 |
childrenRef.setSingleChild(child); |
| 214 |
childMap->treeDataRef().setParent(this, child.index()); |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
SharedChildrenHasher::Lookup lookup(id, prop, child.index()); |
| 219 |
|
| 220 |
if (hasChildrenSet()) { |
| 221 |
if (!childrenRef.toChildrenSet()->putNew(lookup, child)) { |
| 222 |
ReportOutOfMemory(cx); |
| 223 |
return false; |
| 224 |
} |
| 225 |
} else { |
| 226 |
auto hash = MakeUnique<SharedChildrenSet>(); |
| 227 |
if (!hash || !hash->reserve(2)) { |
| 228 |
ReportOutOfMemory(cx); |
| 229 |
return false; |
| 230 |
} |
| 231 |
SharedPropMapAndIndex firstChild = childrenRef.toSingleChild(); |
| 232 |
SharedPropMap* firstChildMap = firstChild.map(); |
| 233 |
uint32_t firstChildIndex = indexOfNextProperty(firstChild.index()); |
| 234 |
SharedChildrenHasher::Lookup lookupFirst( |
| 235 |
firstChildMap->getPropertyInfoWithKey(firstChildIndex), |
| 236 |
firstChild.index()); |
| 237 |
hash->putNewInfallible(lookupFirst, firstChild); |
| 238 |
hash->putNewInfallible(lookup, child); |
| 239 |
|
| 240 |
childrenRef.setChildrenSet(hash.release()); |
| 241 |
setHasChildrenSet(); |
| 242 |
AddCellMemory(this, sizeof(SharedChildrenSet), MemoryUse::PropMapChildren); |
| 243 |
} |
| 244 |
|
| 245 |
childMap->treeDataRef().setParent(this, child.index()); |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
// static |
| 250 |
bool SharedPropMap::addProperty(JSContext* cx, const JSClass* clasp, |
| 251 |
MutableHandle<SharedPropMap*> map, |
| 252 |
uint32_t* mapLength, HandleId id, |
| 253 |
PropertyFlags flags, ObjectFlags* objectFlags, |
| 254 |
uint32_t* slot) { |
| 255 |
MOZ_ASSERT(!flags.isCustomDataProperty()); |
| 256 |
|
| 257 |
*slot = SharedPropMap::slotSpan(clasp, map, *mapLength); |
| 258 |
|
| 259 |
if (MOZ_UNLIKELY(*slot > SHAPE_MAXIMUM_SLOT)) { |
| 260 |
ReportAllocationOverflow(cx); |
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
*objectFlags = |
| 265 |
GetObjectFlagsForNewProperty(clasp, *objectFlags, id, flags, cx); |
| 266 |
|
| 267 |
PropertyInfo prop = PropertyInfo(flags, *slot); |
| 268 |
return addPropertyInternal(cx, map, mapLength, id, prop); |
| 269 |
} |
| 270 |
|
| 271 |
// static |
| 272 |
bool SharedPropMap::addPropertyInReservedSlot( |
| 273 |
JSContext* cx, const JSClass* clasp, MutableHandle<SharedPropMap*> map, |
| 274 |
uint32_t* mapLength, HandleId id, PropertyFlags flags, uint32_t slot, |
| 275 |
ObjectFlags* objectFlags) { |
| 276 |
MOZ_ASSERT(!flags.isCustomDataProperty()); |
| 277 |
|
| 278 |
MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(clasp)); |
| 279 |
MOZ_ASSERT_IF(map, map->lastUsedSlot(*mapLength) < slot); |
| 280 |
|
| 281 |
*objectFlags = |
| 282 |
GetObjectFlagsForNewProperty(clasp, *objectFlags, id, flags, cx); |
| 283 |
|
| 284 |
PropertyInfo prop = PropertyInfo(flags, slot); |
| 285 |
return addPropertyInternal(cx, map, mapLength, id, prop); |
| 286 |
} |
| 287 |
|
| 288 |
// static |
| 289 |
bool SharedPropMap::addPropertyWithKnownSlot(JSContext* cx, |
| 290 |
const JSClass* clasp, |
| 291 |
MutableHandle<SharedPropMap*> map, |
| 292 |
uint32_t* mapLength, HandleId id, |
| 293 |
PropertyFlags flags, uint32_t slot, |
| 294 |
ObjectFlags* objectFlags) { |
| 295 |
MOZ_ASSERT(!flags.isCustomDataProperty()); |
| 296 |
|
| 297 |
if (MOZ_UNLIKELY(slot < JSCLASS_RESERVED_SLOTS(clasp))) { |
| 298 |
return addPropertyInReservedSlot(cx, clasp, map, mapLength, id, flags, slot, |
| 299 |
objectFlags); |
| 300 |
} |
| 301 |
|
| 302 |
MOZ_ASSERT(slot == SharedPropMap::slotSpan(clasp, map, *mapLength)); |
| 303 |
MOZ_RELEASE_ASSERT(slot <= SHAPE_MAXIMUM_SLOT); |
| 304 |
|
| 305 |
*objectFlags = |
| 306 |
GetObjectFlagsForNewProperty(clasp, *objectFlags, id, flags, cx); |
| 307 |
|
| 308 |
PropertyInfo prop = PropertyInfo(flags, slot); |
| 309 |
return addPropertyInternal(cx, map, mapLength, id, prop); |
| 310 |
} |
| 311 |
|
| 312 |
// static |
| 313 |
bool SharedPropMap::addCustomDataProperty(JSContext* cx, const JSClass* clasp, |
| 314 |
MutableHandle<SharedPropMap*> map, |
| 315 |
uint32_t* mapLength, HandleId id, |
| 316 |
PropertyFlags flags, |
| 317 |
ObjectFlags* objectFlags) { |
| 318 |
MOZ_ASSERT(flags.isCustomDataProperty()); |
| 319 |
|
| 320 |
// Custom data properties don't have a slot. Copy the last property's slot |
| 321 |
// number to simplify the implementation of SharedPropMap::slotSpan. |
| 322 |
uint32_t slot = map ? map->lastUsedSlot(*mapLength) : SHAPE_INVALID_SLOT; |
| 323 |
|
| 324 |
*objectFlags = |
| 325 |
GetObjectFlagsForNewProperty(clasp, *objectFlags, id, flags, cx); |
| 326 |
|
| 327 |
PropertyInfo prop = PropertyInfo(flags, slot); |
| 328 |
return addPropertyInternal(cx, map, mapLength, id, prop); |
| 329 |
} |
| 330 |
|
| 331 |
// static |
| 332 |
bool SharedPropMap::addPropertyInternal(JSContext* cx, |
| 333 |
MutableHandle<SharedPropMap*> map, |
| 334 |
uint32_t* mapLength, HandleId id, |
| 335 |
PropertyInfo prop) { |
| 336 |
if (!map) { |
| 337 |
// Adding the first property. |
| 338 |
MOZ_ASSERT(*mapLength == 0); |
| 339 |
map.set(SharedPropMap::createInitial(cx, id, prop)); |
| 340 |
if (!map) { |
| 341 |
return false; |
| 342 |
} |
| 343 |
*mapLength = 1; |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
MOZ_ASSERT(*mapLength > 0); |
| 348 |
|
| 349 |
if (*mapLength < PropMap::Capacity) { |
| 350 |
// Use the next map entry if possible. |
| 351 |
if (!map->hasKey(*mapLength)) { |
| 352 |
if (map->canHaveTable()) { |
| 353 |
JS::AutoCheckCannotGC nogc; |
| 354 |
if (PropMapTable* table = map->asLinked()->maybeTable(nogc)) { |
| 355 |
if (!table->add(cx, id, PropMapAndIndex(map, *mapLength))) { |
| 356 |
return false; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
map->initProperty(*mapLength, id, prop); |
| 361 |
*mapLength += 1; |
| 362 |
return true; |
| 363 |
} |
| 364 |
if (map->matchProperty(*mapLength, id, prop)) { |
| 365 |
*mapLength += 1; |
| 366 |
return true; |
| 367 |
} |
| 368 |
|
| 369 |
// The next entry can't be used so look up or create a child map. The child |
| 370 |
// map is a clone of this map up to mapLength, with the new property stored |
| 371 |
// as the next entry. |
| 372 |
|
| 373 |
if (SharedPropMap* child = map->lookupChild(*mapLength, id, prop)) { |
| 374 |
map.set(child); |
| 375 |
*mapLength += 1; |
| 376 |
return true; |
| 377 |
} |
| 378 |
|
| 379 |
SharedPropMap* child = SharedPropMap::clone(cx, map, *mapLength); |
| 380 |
if (!child) { |
| 381 |
return false; |
| 382 |
} |
| 383 |
child->initProperty(*mapLength, id, prop); |
| 384 |
|
| 385 |
SharedPropMapAndIndex childEntry(child, *mapLength - 1); |
| 386 |
if (!map->addChild(cx, childEntry, id, prop)) { |
| 387 |
return false; |
| 388 |
} |
| 389 |
|
| 390 |
map.set(child); |
| 391 |
*mapLength += 1; |
| 392 |
return true; |
| 393 |
} |
| 394 |
|
| 395 |
// This map is full so look up or create a child map. |
| 396 |
MOZ_ASSERT(*mapLength == PropMap::Capacity); |
| 397 |
|
| 398 |
if (SharedPropMap* child = map->lookupChild(*mapLength, id, prop)) { |
| 399 |
map.set(child); |
| 400 |
*mapLength = 1; |
| 401 |
return true; |
| 402 |
} |
| 403 |
|
| 404 |
SharedPropMap* child = SharedPropMap::create(cx, map, id, prop); |
| 405 |
if (!child) { |
| 406 |
return false; |
| 407 |
} |
| 408 |
|
| 409 |
SharedPropMapAndIndex childEntry(child, PropMap::Capacity - 1); |
| 410 |
if (!map->addChild(cx, childEntry, id, prop)) { |
| 411 |
return false; |
| 412 |
} |
| 413 |
|
| 414 |
// As an optimization, pass the table to the new child map, unless adding the |
| 415 |
// property to it OOMs. Measurements indicate this gets rid of a large number |
| 416 |
// of PropMapTable allocations because we don't need to create a second table |
| 417 |
// when the parent map won't be used again as last map. |
| 418 |
if (map->canHaveTable()) { |
| 419 |
JS::AutoCheckCannotGC nogc; |
| 420 |
if (PropMapTable* table = map->asLinked()->maybeTable(nogc)) { |
| 421 |
// Trigger a pre-barrier on the parent map to appease the pre-barrier |
| 422 |
// verifier, because edges from the table are disappearing (even though |
| 423 |
// these edges are strictly redundant with the |previous| maps). |
| 424 |
gc::PreWriteBarrier(map.get()); |
| 425 |
if (table->add(cx, id, PropMapAndIndex(child, 0))) { |
| 426 |
map->asLinked()->handOffTableTo(child->asLinked()); |
| 427 |
} else { |
| 428 |
cx->recoverFromOutOfMemory(); |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
map.set(child); |
| 434 |
*mapLength = 1; |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
static PropertyFlags ComputeFlagsForSealOrFreeze(PropertyKey key, |
| 439 |
PropertyFlags flags, |
| 440 |
IntegrityLevel level) { |
| 441 |
// Private fields are not visible to SetIntegrityLevel. |
| 442 |
if (key.isSymbol() && key.toSymbol()->isPrivateName()) { |
| 443 |
return flags; |
| 444 |
} |
| 445 |
|
| 446 |
// Make all properties non-configurable; if freezing, make data properties |
| 447 |
// read-only. |
| 448 |
flags.clearFlag(PropertyFlag::Configurable); |
| 449 |
if (level == IntegrityLevel::Frozen && flags.isDataDescriptor()) { |
| 450 |
flags.clearFlag(PropertyFlag::Writable); |
| 451 |
} |
| 452 |
|
| 453 |
return flags; |
| 454 |
} |
| 455 |
|
| 456 |
// static |
| 457 |
bool SharedPropMap::freezeOrSealProperties(JSContext* cx, IntegrityLevel level, |
| 458 |
const JSClass* clasp, |
| 459 |
MutableHandle<SharedPropMap*> map, |
| 460 |
uint32_t mapLength, |
| 461 |
ObjectFlags* objectFlags) { |
| 462 |
// Add all maps to a Vector so we can iterate over them in reverse order |
| 463 |
// (property definition order). |
| 464 |
JS::RootedVector<SharedPropMap*> maps(cx); |
| 465 |
{ |
| 466 |
SharedPropMap* curMap = map; |
| 467 |
while (true) { |
| 468 |
if (!maps.append(curMap)) { |
| 469 |
return false; |
| 470 |
} |
| 471 |
if (!curMap->hasPrevious()) { |
| 472 |
break; |
| 473 |
} |
| 474 |
curMap = curMap->asNormal()->previous(); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
// Build a new SharedPropMap by adding each property with the changed |
| 479 |
// attributes. |
| 480 |
Rooted<SharedPropMap*> newMap(cx); |
| 481 |
uint32_t newMapLength = 0; |
| 482 |
|
| 483 |
Rooted<PropertyKey> key(cx); |
| 484 |
Rooted<SharedPropMap*> curMap(cx); |
| 485 |
|
| 486 |
for (size_t i = maps.length(); i > 0; i--) { |
| 487 |
curMap = maps[i - 1]; |
| 488 |
uint32_t len = (i == 1) ? mapLength : PropMap::Capacity; |
| 489 |
|
| 490 |
for (uint32_t j = 0; j < len; j++) { |
| 491 |
key = curMap->getKey(j); |
| 492 |
PropertyInfo prop = curMap->getPropertyInfo(j); |
| 493 |
PropertyFlags flags = |
| 494 |
ComputeFlagsForSealOrFreeze(key, prop.flags(), level); |
| 495 |
|
| 496 |
if (prop.isCustomDataProperty()) { |
| 497 |
if (!addCustomDataProperty(cx, clasp, &newMap, &newMapLength, key, |
| 498 |
flags, objectFlags)) { |
| 499 |
return false; |
| 500 |
} |
| 501 |
} else { |
| 502 |
if (!addPropertyWithKnownSlot(cx, clasp, &newMap, &newMapLength, key, |
| 503 |
flags, prop.slot(), objectFlags)) { |
| 504 |
return false; |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
map.set(newMap); |
| 511 |
MOZ_ASSERT(newMapLength == mapLength); |
| 512 |
return true; |
| 513 |
} |
| 514 |
|
| 515 |
void LinkedPropMap::handOffTableTo(LinkedPropMap* next) { |
| 516 |
MOZ_ASSERT(hasTable()); |
| 517 |
MOZ_ASSERT(!next->hasTable()); |
| 518 |
|
| 519 |
next->data_.table = data_.table; |
| 520 |
data_.table = nullptr; |
| 521 |
|
| 522 |
// Note: for tables currently only sizeof(PropMapTable) is tracked. |
| 523 |
RemoveCellMemory(this, sizeof(PropMapTable), MemoryUse::PropMapTable); |
| 524 |
AddCellMemory(next, sizeof(PropMapTable), MemoryUse::PropMapTable); |
| 525 |
} |
| 526 |
|
| 527 |
void DictionaryPropMap::handOffLastMapStateTo(DictionaryPropMap* newLast) { |
| 528 |
// A dictionary object's last map contains the table, slot freeList, and |
| 529 |
// holeCount. These fields always have their initial values for non-last maps. |
| 530 |
|
| 531 |
MOZ_ASSERT(this != newLast); |
| 532 |
|
| 533 |
if (asLinked()->hasTable()) { |
| 534 |
asLinked()->handOffTableTo(newLast->asLinked()); |
| 535 |
} |
| 536 |
|
| 537 |
MOZ_ASSERT(newLast->freeList_ == SHAPE_INVALID_SLOT); |
| 538 |
newLast->freeList_ = freeList_; |
| 539 |
freeList_ = SHAPE_INVALID_SLOT; |
| 540 |
|
| 541 |
MOZ_ASSERT(newLast->holeCount_ == 0); |
| 542 |
newLast->holeCount_ = holeCount_; |
| 543 |
holeCount_ = 0; |
| 544 |
} |
| 545 |
|
| 546 |
// static |
| 547 |
DictionaryPropMap* DictionaryPropMap::createEmpty(JSContext* cx) { |
| 548 |
return cx->newCell<DictionaryPropMap>(nullptr); |
| 549 |
} |
| 550 |
|
| 551 |
// static |
| 552 |
bool DictionaryPropMap::addProperty(JSContext* cx, const JSClass* clasp, |
| 553 |
MutableHandle<DictionaryPropMap*> map, |
| 554 |
uint32_t* mapLength, HandleId id, |
| 555 |
PropertyFlags flags, uint32_t slot, |
| 556 |
ObjectFlags* objectFlags) { |
| 557 |
MOZ_ASSERT(map); |
| 558 |
|
| 559 |
*objectFlags = |
| 560 |
GetObjectFlagsForNewProperty(clasp, *objectFlags, id, flags, cx); |
| 561 |
PropertyInfo prop = PropertyInfo(flags, slot); |
| 562 |
|
| 563 |
if (*mapLength < PropMap::Capacity) { |
| 564 |
JS::AutoCheckCannotGC nogc; |
| 565 |
if (PropMapTable* table = map->asLinked()->maybeTable(nogc)) { |
| 566 |
if (!table->add(cx, id, PropMapAndIndex(map, *mapLength))) { |
| 567 |
return false; |
| 568 |
} |
| 569 |
} |
| 570 |
map->initProperty(*mapLength, id, prop); |
| 571 |
*mapLength += 1; |
| 572 |
return true; |
| 573 |
} |
| 574 |
|
| 575 |
DictionaryPropMap* newMap = cx->newCell<DictionaryPropMap>(map, id, prop); |
| 576 |
if (!newMap) { |
| 577 |
return false; |
| 578 |
} |
| 579 |
|
| 580 |
JS::AutoCheckCannotGC nogc; |
| 581 |
if (PropMapTable* table = map->asLinked()->maybeTable(nogc)) { |
| 582 |
if (!table->add(cx, id, PropMapAndIndex(newMap, 0))) { |
| 583 |
return false; |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
MOZ_ASSERT(newMap->previous() == map); |
| 588 |
map->handOffLastMapStateTo(newMap); |
| 589 |
|
| 590 |
map.set(newMap); |
| 591 |
*mapLength = 1; |
| 592 |
return true; |
| 593 |
} |
| 594 |
|
| 595 |
void DictionaryPropMap::changeProperty(JSContext* cx, const JSClass* clasp, |
| 596 |
uint32_t index, PropertyFlags flags, |
| 597 |
uint32_t slot, |
| 598 |
ObjectFlags* objectFlags) { |
| 599 |
MOZ_ASSERT(hasKey(index)); |
| 600 |
*objectFlags = GetObjectFlagsForNewProperty(clasp, *objectFlags, |
| 601 |
getKey(index), flags, cx); |
| 602 |
linkedData_.propInfos[index] = PropertyInfo(flags, slot); |
| 603 |
} |
| 604 |
|
| 605 |
void DictionaryPropMap::freezeOrSealProperties(JSContext* cx, |
| 606 |
IntegrityLevel level, |
| 607 |
const JSClass* clasp, |
| 608 |
uint32_t mapLength, |
| 609 |
ObjectFlags* objectFlags) { |
| 610 |
DictionaryPropMap* curMap = this; |
| 611 |
do { |
| 612 |
for (uint32_t i = 0; i < mapLength; i++) { |
| 613 |
if (!curMap->hasKey(i)) { |
| 614 |
continue; |
| 615 |
} |
| 616 |
PropertyKey key = curMap->getKey(i); |
| 617 |
PropertyFlags flags = curMap->getPropertyInfo(i).flags(); |
| 618 |
flags = ComputeFlagsForSealOrFreeze(key, flags, level); |
| 619 |
curMap->changePropertyFlags(cx, clasp, i, flags, objectFlags); |
| 620 |
} |
| 621 |
curMap = curMap->previous(); |
| 622 |
mapLength = PropMap::Capacity; |
| 623 |
} while (curMap); |
| 624 |
} |
| 625 |
|
| 626 |
// static |
| 627 |
void DictionaryPropMap::skipTrailingHoles(MutableHandle<DictionaryPropMap*> map, |
| 628 |
uint32_t* mapLength) { |
| 629 |
// After removing a property, rewind map/mapLength so that the last property |
| 630 |
// is not a hole. This ensures accessing the last property of a map can always |
| 631 |
// be done without checking for holes. |
| 632 |
|
| 633 |
while (true) { |
| 634 |
MOZ_ASSERT(*mapLength > 0); |
| 635 |
do { |
| 636 |
if (map->hasKey(*mapLength - 1)) { |
| 637 |
return; |
| 638 |
} |
| 639 |
map->decHoleCount(); |
| 640 |
*mapLength -= 1; |
| 641 |
} while (*mapLength > 0); |
| 642 |
|
| 643 |
if (!map->previous()) { |
| 644 |
// The dictionary map is empty, return the initial map with mapLength 0. |
| 645 |
MOZ_ASSERT(*mapLength == 0); |
| 646 |
MOZ_ASSERT(map->holeCount_ == 0); |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
map->handOffLastMapStateTo(map->previous()); |
| 651 |
map.set(map->previous()); |
| 652 |
*mapLength = PropMap::Capacity; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
// static |
| 657 |
void DictionaryPropMap::removeProperty(JSContext* cx, |
| 658 |
MutableHandle<DictionaryPropMap*> map, |
| 659 |
uint32_t* mapLength, PropMapTable* table, |
| 660 |
PropMapTable::Ptr& ptr) { |
| 661 |
MOZ_ASSERT(map); |
| 662 |
MOZ_ASSERT(*mapLength > 0); |
| 663 |
|
| 664 |
JS::AutoCheckCannotGC nogc; |
| 665 |
MOZ_ASSERT(map->asLinked()->maybeTable(nogc) == table); |
| 666 |
|
| 667 |
bool removingLast = (map == ptr->map() && *mapLength - 1 == ptr->index()); |
| 668 |
ptr->map()->asDictionary()->clearProperty(ptr->index()); |
| 669 |
map->incHoleCount(); |
| 670 |
table->remove(ptr); |
| 671 |
|
| 672 |
if (removingLast) { |
| 673 |
skipTrailingHoles(map, mapLength); |
| 674 |
} |
| 675 |
maybeCompact(cx, map, mapLength); |
| 676 |
} |
| 677 |
|
| 678 |
// static |
| 679 |
void DictionaryPropMap::densifyElements(JSContext* cx, |
| 680 |
MutableHandle<DictionaryPropMap*> map, |
| 681 |
uint32_t* mapLength, |
| 682 |
NativeObject* obj) { |
| 683 |
MOZ_ASSERT(map); |
| 684 |
MOZ_ASSERT(*mapLength > 0); |
| 685 |
|
| 686 |
JS::AutoCheckCannotGC nogc; |
| 687 |
PropMapTable* table = map->asLinked()->maybeTable(nogc); |
| 688 |
|
| 689 |
DictionaryPropMap* currentMap = map; |
| 690 |
uint32_t currentLen = *mapLength; |
| 691 |
do { |
| 692 |
for (uint32_t i = 0; i < currentLen; i++) { |
| 693 |
PropertyKey key = currentMap->getKey(i); |
| 694 |
uint32_t index; |
| 695 |
if (!IdIsIndex(key, &index)) { |
| 696 |
continue; |
| 697 |
} |
| 698 |
|
| 699 |
// The caller must have checked all sparse elements are plain data |
| 700 |
// properties. |
| 701 |
PropertyInfo prop = currentMap->getPropertyInfo(i); |
| 702 |
MOZ_ASSERT(prop.flags() == PropertyFlags::defaultDataPropFlags); |
| 703 |
|
| 704 |
uint32_t slot = prop.slot(); |
| 705 |
Value value = obj->getSlot(slot); |
| 706 |
obj->setDenseElement(index, value); |
| 707 |
obj->freeDictionarySlot(slot); |
| 708 |
|
| 709 |
if (table) { |
| 710 |
PropMapTable::Ptr p = table->lookupRaw(key); |
| 711 |
MOZ_ASSERT(p); |
| 712 |
table->remove(p); |
| 713 |
} |
| 714 |
|
| 715 |
currentMap->clearProperty(i); |
| 716 |
map->incHoleCount(); |
| 717 |
} |
| 718 |
currentMap = currentMap->previous(); |
| 719 |
currentLen = PropMap::Capacity; |
| 720 |
} while (currentMap); |
| 721 |
|
| 722 |
skipTrailingHoles(map, mapLength); |
| 723 |
maybeCompact(cx, map, mapLength); |
| 724 |
} |
| 725 |
|
| 726 |
void DictionaryPropMap::maybeCompact(JSContext* cx, |
| 727 |
MutableHandle<DictionaryPropMap*> map, |
| 728 |
uint32_t* mapLength) { |
| 729 |
// If there are no holes, there's nothing to compact. |
| 730 |
if (map->holeCount_ == 0) { |
| 731 |
return; |
| 732 |
} |
| 733 |
|
| 734 |
JS::AutoCheckCannotGC nogc; |
| 735 |
PropMapTable* table = map->asLinked()->ensureTable(cx, nogc); |
| 736 |
if (!table) { |
| 737 |
// Compacting is optional so just return. |
| 738 |
cx->recoverFromOutOfMemory(); |
| 739 |
return; |
| 740 |
} |
| 741 |
|
| 742 |
// Heuristic: only compact if the number of holes >= the number of (non-hole) |
| 743 |
// entries. |
| 744 |
if (map->holeCount_ < table->entryCount()) { |
| 745 |
return; |
| 746 |
} |
| 747 |
|
| 748 |
// Add all dictionary maps to a Vector so that we can iterate over them in |
| 749 |
// reverse order (property definition order). If appending to the Vector OOMs, |
| 750 |
// just return because compacting is optional. |
| 751 |
Vector<DictionaryPropMap*, 32, SystemAllocPolicy> maps; |
| 752 |
for (DictionaryPropMap* curMap = map; curMap; curMap = curMap->previous()) { |
| 753 |
if (!maps.append(curMap)) { |
| 754 |
return; |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
// Use two cursors: readMapCursor/readIndexCursor iterates over all properties |
| 759 |
// starting at the first one, to search for the next non-hole entry. |
| 760 |
// writeMapCursor/writeIndexCursor is used to write all non-hole keys. |
| 761 |
// |
| 762 |
// At the start of the loop, these cursors point to the next property slot to |
| 763 |
// read/write. |
| 764 |
|
| 765 |
size_t readMapCursorVectorIndex = maps.length() - 1; |
| 766 |
DictionaryPropMap* readMapCursor = maps[readMapCursorVectorIndex]; |
| 767 |
uint32_t readIndexCursor = 0; |
| 768 |
|
| 769 |
size_t writeMapCursorVectorIndex = readMapCursorVectorIndex; |
| 770 |
DictionaryPropMap* writeMapCursor = readMapCursor; |
| 771 |
uint32_t writeIndexCursor = 0; |
| 772 |
|
| 773 |
mozilla::DebugOnly<uint32_t> numHoles = 0; |
| 774 |
|
| 775 |
while (true) { |
| 776 |
if (readMapCursor->hasKey(readIndexCursor)) { |
| 777 |
// Found a non-hole entry, copy it to its new position and update the |
| 778 |
// PropMapTable to point to this new entry. Only do this if the read and |
| 779 |
// write positions are different from each other. |
| 780 |
if (readMapCursor != writeMapCursor || |
| 781 |
readIndexCursor != writeIndexCursor) { |
| 782 |
PropertyKey key = readMapCursor->getKey(readIndexCursor); |
| 783 |
auto p = table->lookupRaw(key); |
| 784 |
MOZ_ASSERT(p); |
| 785 |
MOZ_ASSERT(p->map() == readMapCursor); |
| 786 |
MOZ_ASSERT(p->index() == readIndexCursor); |
| 787 |
|
| 788 |
writeMapCursor->setKey(writeIndexCursor, key); |
| 789 |
writeMapCursor->linkedData_.propInfos[writeIndexCursor] = |
| 790 |
readMapCursor->linkedData_.propInfos[readIndexCursor]; |
| 791 |
|
| 792 |
PropMapAndIndex newEntry(writeMapCursor, writeIndexCursor); |
| 793 |
table->replaceEntry(p, key, newEntry); |
| 794 |
} |
| 795 |
// Advance the write cursor. |
| 796 |
writeIndexCursor++; |
| 797 |
if (writeIndexCursor == PropMap::Capacity) { |
| 798 |
MOZ_ASSERT(writeMapCursorVectorIndex > 0); |
| 799 |
writeMapCursorVectorIndex--; |
| 800 |
writeMapCursor = maps[writeMapCursorVectorIndex]; |
| 801 |
writeIndexCursor = 0; |
| 802 |
} |
| 803 |
} else { |
| 804 |
numHoles++; |
| 805 |
} |
| 806 |
// Advance the read cursor. If there are no more maps to read from, we're |
| 807 |
// done. |
| 808 |
readIndexCursor++; |
| 809 |
if (readIndexCursor == PropMap::Capacity) { |
| 810 |
if (readMapCursorVectorIndex == 0) { |
| 811 |
break; |
| 812 |
} |
| 813 |
readMapCursorVectorIndex--; |
| 814 |
readMapCursor = maps[readMapCursorVectorIndex]; |
| 815 |
readIndexCursor = 0; |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
// Sanity check: the read cursor skipped holes between properties and holes |
| 820 |
// at the end of the last map (these are not included in holeCount_). |
| 821 |
MOZ_ASSERT(map->holeCount_ + (PropMap::Capacity - *mapLength) == numHoles); |
| 822 |
|
| 823 |
// The write cursor points to the next available slot. If this is at the start |
| 824 |
// of a new map, use the previous map (which must be full) instead. |
| 825 |
if (writeIndexCursor == 0 && writeMapCursor->previous()) { |
| 826 |
writeMapCursor = writeMapCursor->previous(); |
| 827 |
*mapLength = PropMap::Capacity; |
| 828 |
} else { |
| 829 |
*mapLength = writeIndexCursor; |
| 830 |
} |
| 831 |
|
| 832 |
// Ensure the last map does not have any keys in [mapLength, Capacity). |
| 833 |
for (uint32_t i = *mapLength; i < PropMap::Capacity; i++) { |
| 834 |
writeMapCursor->clearProperty(i); |
| 835 |
} |
| 836 |
|
| 837 |
if (writeMapCursor != map) { |
| 838 |
map->handOffLastMapStateTo(writeMapCursor); |
| 839 |
map.set(writeMapCursor); |
| 840 |
} |
| 841 |
map->holeCount_ = 0; |
| 842 |
|
| 843 |
MOZ_ASSERT(*mapLength <= PropMap::Capacity); |
| 844 |
MOZ_ASSERT_IF(*mapLength == 0, !map->previous()); |
| 845 |
MOZ_ASSERT_IF(!map->previous(), table->entryCount() == *mapLength); |
| 846 |
} |
| 847 |
|
| 848 |
void SharedPropMap::fixupAfterMovingGC() { |
| 849 |
SharedChildrenPtr& childrenRef = treeDataRef().children; |
| 850 |
if (childrenRef.isNone()) { |
| 851 |
return; |
| 852 |
} |
| 853 |
|
| 854 |
if (!hasChildrenSet()) { |
| 855 |
SharedPropMapAndIndex child = childrenRef.toSingleChild(); |
| 856 |
if (gc::IsForwarded(child.map())) { |
| 857 |
child = SharedPropMapAndIndex(gc::Forwarded(child.map()), child.index()); |
| 858 |
childrenRef.setSingleChild(child); |
| 859 |
} |
| 860 |
return; |
| 861 |
} |
| 862 |
|
| 863 |
SharedChildrenSet* set = childrenRef.toChildrenSet(); |
| 864 |
for (SharedChildrenSet::Enum e(*set); !e.empty(); e.popFront()) { |
| 865 |
SharedPropMapAndIndex child = e.front(); |
| 866 |
if (IsForwarded(child.map())) { |
| 867 |
child = SharedPropMapAndIndex(Forwarded(child.map()), child.index()); |
| 868 |
e.mutableFront() = child; |
| 869 |
} |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
void SharedPropMap::removeChild(JS::GCContext* gcx, SharedPropMap* child) { |
| 874 |
SharedPropMapAndIndex& parentRef = child->treeDataRef().parent; |
| 875 |
MOZ_ASSERT(parentRef.map() == this); |
| 876 |
|
| 877 |
uint32_t index = parentRef.index(); |
| 878 |
parentRef.setNone(); |
| 879 |
|
| 880 |
SharedChildrenPtr& childrenRef = treeDataRef().children; |
| 881 |
MOZ_ASSERT(!childrenRef.isNone()); |
| 882 |
|
| 883 |
if (!hasChildrenSet()) { |
| 884 |
MOZ_ASSERT(childrenRef.toSingleChild().map() == child); |
| 885 |
MOZ_ASSERT(childrenRef.toSingleChild().index() == index); |
| 886 |
childrenRef.setNone(); |
| 887 |
return; |
| 888 |
} |
| 889 |
|
| 890 |
SharedChildrenSet* set = childrenRef.toChildrenSet(); |
| 891 |
{ |
| 892 |
uint32_t nextIndex = SharedPropMap::indexOfNextProperty(index); |
| 893 |
SharedChildrenHasher::Lookup lookup( |
| 894 |
child->getPropertyInfoWithKey(nextIndex), index); |
| 895 |
auto p = set->lookup(lookup); |
| 896 |
MOZ_ASSERT(p, "Child must be in children set"); |
| 897 |
set->remove(p); |
| 898 |
} |
| 899 |
|
| 900 |
MOZ_ASSERT(set->count() >= 1); |
| 901 |
|
| 902 |
if (set->count() == 1) { |
| 903 |
// Convert from set form back to single child form. |
| 904 |
SharedChildrenSet::Range r = set->all(); |
| 905 |
SharedPropMapAndIndex remainingChild = r.front(); |
| 906 |
childrenRef.setSingleChild(remainingChild); |
| 907 |
clearHasChildrenSet(); |
| 908 |
gcx->delete_(this, set, MemoryUse::PropMapChildren); |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
void LinkedPropMap::purgeTable(JS::GCContext* gcx) { |
| 913 |
MOZ_ASSERT(hasTable()); |
| 914 |
gcx->delete_(this, data_.table, MemoryUse::PropMapTable); |
| 915 |
data_.table = nullptr; |
| 916 |
} |
| 917 |
|
| 918 |
uint32_t PropMap::approximateEntryCount() const { |
| 919 |
// Returns a number that's guaranteed to tbe >= the exact number of properties |
| 920 |
// in this map (including previous maps). This is used to reserve space in the |
| 921 |
// HashSet when allocating a table for this map. |
| 922 |
|
| 923 |
const PropMap* map = this; |
| 924 |
uint32_t count = 0; |
| 925 |
JS::AutoCheckCannotGC nogc; |
| 926 |
while (true) { |
| 927 |
if (!map->hasPrevious()) { |
| 928 |
return count + PropMap::Capacity; |
| 929 |
} |
| 930 |
if (PropMapTable* table = map->asLinked()->maybeTable(nogc)) { |
| 931 |
return count + table->entryCount(); |
| 932 |
} |
| 933 |
count += PropMap::Capacity; |
| 934 |
map = map->asLinked()->previous(); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
bool PropMapTable::init(JSContext* cx, LinkedPropMap* map) { |
| 939 |
if (!set_.reserve(map->approximateEntryCount())) { |
| 940 |
ReportOutOfMemory(cx); |
| 941 |
return false; |
| 942 |
} |
| 943 |
|
| 944 |
PropMap* curMap = map; |
| 945 |
while (true) { |
| 946 |
for (uint32_t i = 0; i < PropMap::Capacity; i++) { |
| 947 |
if (curMap->hasKey(i)) { |
| 948 |
PropertyKey key = curMap->getKey(i); |
| 949 |
set_.putNewInfallible(key, PropMapAndIndex(curMap, i)); |
| 950 |
} |
| 951 |
} |
| 952 |
if (!curMap->hasPrevious()) { |
| 953 |
break; |
| 954 |
} |
| 955 |
curMap = curMap->asLinked()->previous(); |
| 956 |
} |
| 957 |
|
| 958 |
return true; |
| 959 |
} |
| 960 |
|
| 961 |
void PropMapTable::trace(JSTracer* trc) { |
| 962 |
purgeCache(); |
| 963 |
|
| 964 |
for (Set::Enum e(set_); !e.empty(); e.popFront()) { |
| 965 |
PropMap* map = e.front().map(); |
| 966 |
TraceManuallyBarrieredEdge(trc, &map, "PropMapTable map"); |
| 967 |
if (map != e.front().map()) { |
| 968 |
e.mutableFront() = PropMapAndIndex(map, e.front().index()); |
| 969 |
} |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 974 |
void PropMapTable::checkAfterMovingGC(JS::Zone* zone) { |
| 975 |
CheckTableAfterMovingGC(set_, [zone](const auto& entry) { |
| 976 |
PropMap* map = entry.map(); |
| 977 |
MOZ_ASSERT(map); |
| 978 |
CheckGCThingAfterMovingGC(map, zone); |
| 979 |
|
| 980 |
PropertyKey key = map->getKey(entry.index()); |
| 981 |
MOZ_RELEASE_ASSERT(!key.isVoid()); |
| 982 |
if (key.isGCThing()) { |
| 983 |
CheckGCThingAfterMovingGC(key.toGCThing(), zone); |
| 984 |
} |
| 985 |
|
| 986 |
return key; |
| 987 |
}); |
| 988 |
} |
| 989 |
#endif |
| 990 |
|
| 991 |
#ifdef DEBUG |
| 992 |
bool LinkedPropMap::canSkipMarkingTable() { |
| 993 |
if (!hasTable()) { |
| 994 |
return true; |
| 995 |
} |
| 996 |
|
| 997 |
PropMapTable* table = data_.table; |
| 998 |
uint32_t count = 0; |
| 999 |
|
| 1000 |
PropMap* map = this; |
| 1001 |
while (true) { |
| 1002 |
for (uint32_t i = 0; i < Capacity; i++) { |
| 1003 |
if (map->hasKey(i)) { |
| 1004 |
PropertyKey key = map->getKey(i); |
| 1005 |
PropMapTable::Ptr p = table->readonlyThreadsafeLookup(key); |
| 1006 |
MOZ_ASSERT(*p == PropMapAndIndex(map, i)); |
| 1007 |
count++; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
if (!map->hasPrevious()) { |
| 1011 |
break; |
| 1012 |
} |
| 1013 |
map = map->asLinked()->previous(); |
| 1014 |
} |
| 1015 |
|
| 1016 |
return count == table->entryCount(); |
| 1017 |
} |
| 1018 |
#endif |
| 1019 |
|
| 1020 |
bool LinkedPropMap::createTable(JSContext* cx) { |
| 1021 |
MOZ_ASSERT(canHaveTable()); |
| 1022 |
MOZ_ASSERT(!hasTable()); |
| 1023 |
|
| 1024 |
UniquePtr<PropMapTable> table = cx->make_unique<PropMapTable>(); |
| 1025 |
if (!table) { |
| 1026 |
return false; |
| 1027 |
} |
| 1028 |
|
| 1029 |
if (!table->init(cx, this)) { |
| 1030 |
return false; |
| 1031 |
} |
| 1032 |
|
| 1033 |
data_.table = table.release(); |
| 1034 |
// TODO: The contents of PropMapTable is not currently tracked, only the |
| 1035 |
// object itself. |
| 1036 |
AddCellMemory(this, sizeof(PropMapTable), MemoryUse::PropMapTable); |
| 1037 |
return true; |
| 1038 |
} |
| 1039 |
|
| 1040 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1041 |
void PropMap::dump() const { |
| 1042 |
Fprinter out(stderr); |
| 1043 |
dump(out); |
| 1044 |
} |
| 1045 |
|
| 1046 |
void PropMap::dump(js::GenericPrinter& out) const { |
| 1047 |
js::JSONPrinter json(out); |
| 1048 |
dump(json); |
| 1049 |
out.put("\n"); |
| 1050 |
} |
| 1051 |
|
| 1052 |
void PropMap::dump(js::JSONPrinter& json) const { |
| 1053 |
json.beginObject(); |
| 1054 |
dumpFields(json); |
| 1055 |
json.endObject(); |
| 1056 |
} |
| 1057 |
|
| 1058 |
template <typename KnownF, typename UnknownF> |
| 1059 |
void ForEachPropertyFlag(PropertyFlags flags, KnownF known, UnknownF unknown) { |
| 1060 |
uint8_t raw = flags.toRaw(); |
| 1061 |
for (uint8_t i = 1; i; i = i << 1) { |
| 1062 |
if (!(raw & i)) { |
| 1063 |
continue; |
| 1064 |
} |
| 1065 |
switch (PropertyFlag(raw & i)) { |
| 1066 |
case PropertyFlag::Configurable: |
| 1067 |
known("Configurable"); |
| 1068 |
break; |
| 1069 |
case PropertyFlag::Enumerable: |
| 1070 |
known("Enumerable"); |
| 1071 |
break; |
| 1072 |
case PropertyFlag::Writable: |
| 1073 |
known("Writable"); |
| 1074 |
break; |
| 1075 |
case PropertyFlag::AccessorProperty: |
| 1076 |
known("AccessorProperty"); |
| 1077 |
break; |
| 1078 |
case PropertyFlag::CustomDataProperty: |
| 1079 |
known("UseWatchtowerTestingCallback"); |
| 1080 |
break; |
| 1081 |
default: |
| 1082 |
unknown(i); |
| 1083 |
break; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
template <typename KnownF, typename UnknownF> |
| 1089 |
/* static */ |
| 1090 |
void PropMap::forEachPropMapFlag(uintptr_t flags, KnownF known, |
| 1091 |
UnknownF unknown) { |
| 1092 |
for (uintptr_t i = 1 << gc::CellFlagBitsReservedForGC; |
| 1093 |
i < 1 << PropMap::NumPreviousMapsShift; i = i << 1) { |
| 1094 |
if (!(flags & i)) { |
| 1095 |
continue; |
| 1096 |
} |
| 1097 |
switch (flags & i) { |
| 1098 |
case PropMap::Flags::IsCompactFlag: |
| 1099 |
known("IsCompactFlag"); |
| 1100 |
break; |
| 1101 |
case PropMap::Flags::HasPrevFlag: |
| 1102 |
known("HasPrevFlag"); |
| 1103 |
break; |
| 1104 |
case PropMap::Flags::IsDictionaryFlag: |
| 1105 |
known("IsDictionaryFlag"); |
| 1106 |
break; |
| 1107 |
case PropMap::Flags::CanHaveTableFlag: |
| 1108 |
known("CanHaveTableFlag"); |
| 1109 |
break; |
| 1110 |
case PropMap::Flags::HasChildrenSetFlag: |
| 1111 |
known("HasChildrenSetFlag"); |
| 1112 |
break; |
| 1113 |
case PropMap::Flags::HadDictionaryConversionFlag: |
| 1114 |
known("HadDictionaryConversionFlag"); |
| 1115 |
break; |
| 1116 |
default: |
| 1117 |
unknown(i); |
| 1118 |
break; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
const char* PropMapTypeToString(const js::PropMap* map) { |
| 1124 |
if (map->isLinked()) { |
| 1125 |
return "js::LinkedPropMap"; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (map->isShared()) { |
| 1129 |
if (map->isCompact()) { |
| 1130 |
return "js::CompactPropMap"; |
| 1131 |
} |
| 1132 |
return "js::NormalPropMap"; |
| 1133 |
} |
| 1134 |
|
| 1135 |
return "js::DictionaryPropMap"; |
| 1136 |
} |
| 1137 |
|
| 1138 |
void PropMap::dumpFields(js::JSONPrinter& json) const { |
| 1139 |
json.formatProperty("address", "(%s*)0x%p", PropMapTypeToString(this), this); |
| 1140 |
|
| 1141 |
json.beginInlineListProperty("flags"); |
| 1142 |
forEachPropMapFlag( |
| 1143 |
flags(), [&](const char* name) { json.value("%s", name); }, |
| 1144 |
[&](uint32_t value) { json.value("Unknown(%08x)", value); }); |
| 1145 |
json.endInlineList(); |
| 1146 |
|
| 1147 |
if (isLinked()) { |
| 1148 |
asLinked()->dumpOwnFields(json); |
| 1149 |
} else if (isShared()) { |
| 1150 |
asShared()->dumpOwnFields(json); |
| 1151 |
} else { |
| 1152 |
asDictionary()->dumpOwnFields(json); |
| 1153 |
} |
| 1154 |
|
| 1155 |
json.beginObjectProperty("properties"); |
| 1156 |
for (uint32_t i = 0; i < Capacity; i++) { |
| 1157 |
char name[64]; |
| 1158 |
SprintfLiteral(name, "%u", i); |
| 1159 |
|
| 1160 |
if (!hasKey(i)) { |
| 1161 |
json.nullProperty(name); |
| 1162 |
return; |
| 1163 |
} |
| 1164 |
|
| 1165 |
json.beginObjectProperty(name); |
| 1166 |
dumpFieldsAt(json, i); |
| 1167 |
json.endObject(); |
| 1168 |
} |
| 1169 |
json.endObject(); |
| 1170 |
} |
| 1171 |
|
| 1172 |
void LinkedPropMap::dumpOwnFields(js::JSONPrinter& json) const { |
| 1173 |
if (hasPrevious()) { |
| 1174 |
json.formatProperty("previous", "(%s*)0x%p", |
| 1175 |
PropMapTypeToString(previous()), previous()); |
| 1176 |
} |
| 1177 |
|
| 1178 |
if (canHaveTable()) { |
| 1179 |
json.formatProperty("table", "(js::PropMapTable*)0x%p", data_.table); |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
void SharedPropMap::dumpOwnFields(js::JSONPrinter& json) const { |
| 1184 |
SharedPropMapAndIndex parent = treeDataRef().parent; |
| 1185 |
if (parent.isNone()) { |
| 1186 |
json.nullProperty("parent"); |
| 1187 |
} else { |
| 1188 |
json.formatProperty("parent", "(%s*)0x%p [%u]", |
| 1189 |
PropMapTypeToString(parent.map()), parent.map(), |
| 1190 |
parent.index()); |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
void DictionaryPropMap::dumpOwnFields(js::JSONPrinter& json) const { |
| 1195 |
json.property("freeList", freeList_); |
| 1196 |
json.property("holeCount", holeCount_); |
| 1197 |
} |
| 1198 |
|
| 1199 |
void PropMap::dumpFieldsAt(js::JSONPrinter& json, uint32_t index) const { |
| 1200 |
PropertyKey key = getKey(index); |
| 1201 |
js::GenericPrinter& out = json.beginStringProperty("key"); |
| 1202 |
key.dumpStringContent(out); |
| 1203 |
json.endStringProperty(); |
| 1204 |
|
| 1205 |
PropertyInfo prop = getPropertyInfo(index); |
| 1206 |
json.beginInlineListProperty("flags"); |
| 1207 |
ForEachPropertyFlag( |
| 1208 |
prop.flags(), [&](const char* name) { json.value("%s", name); }, |
| 1209 |
[&](uint8_t value) { json.value("Unknown(%02x)", value); }); |
| 1210 |
json.endInlineList(); |
| 1211 |
|
| 1212 |
if (prop.hasSlot()) { |
| 1213 |
json.property("slot", prop.slot()); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
|
| 1217 |
void PropMap::dumpDescriptorStringContentAt(js::GenericPrinter& out, |
| 1218 |
uint32_t index) const { |
| 1219 |
PropertyInfo prop = getPropertyInfo(index); |
| 1220 |
|
| 1221 |
out.printf("map=(%s*)0x%p, index=%u", PropMapTypeToString(this), this, index); |
| 1222 |
|
| 1223 |
if (prop.enumerable()) { |
| 1224 |
out.put(", enumerable"); |
| 1225 |
} |
| 1226 |
if (prop.configurable()) { |
| 1227 |
out.put(", configurable"); |
| 1228 |
} |
| 1229 |
if (prop.isDataDescriptor() && prop.writable()) { |
| 1230 |
out.put(", writable"); |
| 1231 |
} |
| 1232 |
|
| 1233 |
if (prop.isCustomDataProperty()) { |
| 1234 |
out.printf(", <custom-data-prop>"); |
| 1235 |
} |
| 1236 |
|
| 1237 |
if (prop.hasSlot()) { |
| 1238 |
out.printf(", slot=%u", prop.slot()); |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
JS::UniqueChars PropMap::getPropertyNameAt(uint32_t index) const { |
| 1243 |
Sprinter sp; |
| 1244 |
if (!sp.init()) { |
| 1245 |
return nullptr; |
| 1246 |
} |
| 1247 |
|
| 1248 |
PropertyKey key = getKey(index); |
| 1249 |
key.dumpPropertyName(sp); |
| 1250 |
|
| 1251 |
return sp.release(); |
| 1252 |
} |
| 1253 |
#endif // defined(DEBUG) || defined(JS_JITSPEW) |
| 1254 |
|
| 1255 |
#ifdef DEBUG |
| 1256 |
void PropMap::checkConsistency(NativeObject* obj) const { |
| 1257 |
const uint32_t mapLength = obj->shape()->propMapLength(); |
| 1258 |
MOZ_ASSERT(mapLength <= PropMap::Capacity); |
| 1259 |
|
| 1260 |
JS::AutoCheckCannotGC nogc; |
| 1261 |
if (isDictionary()) { |
| 1262 |
// Check dictionary slot free list. |
| 1263 |
for (uint32_t fslot = asDictionary()->freeList(); |
| 1264 |
fslot != SHAPE_INVALID_SLOT; |
| 1265 |
fslot = obj->getSlot(fslot).toPrivateUint32()) { |
| 1266 |
MOZ_ASSERT(fslot < obj->slotSpan()); |
| 1267 |
} |
| 1268 |
|
| 1269 |
auto* table = asLinked()->maybeTable(nogc); |
| 1270 |
const DictionaryPropMap* curMap = asDictionary(); |
| 1271 |
uint32_t numHoles = 0; |
| 1272 |
do { |
| 1273 |
// Some fields must only be set for the last dictionary map. |
| 1274 |
if (curMap != this) { |
| 1275 |
MOZ_ASSERT(!curMap->asLinked()->hasTable()); |
| 1276 |
MOZ_ASSERT(curMap->holeCount_ == 0); |
| 1277 |
MOZ_ASSERT(curMap->freeList_ == SHAPE_INVALID_SLOT); |
| 1278 |
} |
| 1279 |
|
| 1280 |
for (uint32_t i = 0; i < PropMap::Capacity; i++) { |
| 1281 |
if (!curMap->hasKey(i)) { |
| 1282 |
if (curMap != this || i < mapLength) { |
| 1283 |
numHoles++; |
| 1284 |
} |
| 1285 |
continue; |
| 1286 |
} |
| 1287 |
|
| 1288 |
// The last dictionary map must only have keys up to mapLength. |
| 1289 |
MOZ_ASSERT_IF(curMap == this, i < mapLength); |
| 1290 |
|
| 1291 |
PropertyInfo prop = curMap->getPropertyInfo(i); |
| 1292 |
MOZ_ASSERT_IF(prop.hasSlot(), prop.slot() < obj->slotSpan()); |
| 1293 |
|
| 1294 |
// All properties must be in the table. |
| 1295 |
if (table) { |
| 1296 |
PropertyKey key = curMap->getKey(i); |
| 1297 |
auto p = table->lookupRaw(key); |
| 1298 |
MOZ_ASSERT(p->map() == curMap); |
| 1299 |
MOZ_ASSERT(p->index() == i); |
| 1300 |
} |
| 1301 |
} |
| 1302 |
curMap = curMap->previous(); |
| 1303 |
} while (curMap); |
| 1304 |
|
| 1305 |
MOZ_ASSERT(asDictionary()->holeCount_ == numHoles); |
| 1306 |
return; |
| 1307 |
} |
| 1308 |
|
| 1309 |
MOZ_ASSERT(mapLength > 0); |
| 1310 |
|
| 1311 |
const SharedPropMap* curMap = asShared(); |
| 1312 |
auto* table = |
| 1313 |
curMap->canHaveTable() ? curMap->asLinked()->maybeTable(nogc) : nullptr; |
| 1314 |
|
| 1315 |
// Shared maps without a previous map never have a table. |
| 1316 |
MOZ_ASSERT_IF(!curMap->hasPrevious(), !curMap->canHaveTable()); |
| 1317 |
|
| 1318 |
const SharedPropMap* nextMap = nullptr; |
| 1319 |
mozilla::Maybe<uint32_t> nextSlot; |
| 1320 |
while (true) { |
| 1321 |
// Verify numPreviousMaps is set correctly. |
| 1322 |
MOZ_ASSERT_IF(nextMap && nextMap->numPreviousMaps() != NumPreviousMapsMax, |
| 1323 |
curMap->numPreviousMaps() + 1 == nextMap->numPreviousMaps()); |
| 1324 |
MOZ_ASSERT(curMap->hasPrevious() == (curMap->numPreviousMaps() > 0)); |
| 1325 |
|
| 1326 |
// If a previous map also has a table, it must have fewer entries than the |
| 1327 |
// last map's table. |
| 1328 |
if (table && curMap != this && curMap->canHaveTable()) { |
| 1329 |
if (auto* table2 = curMap->asLinked()->maybeTable(nogc)) { |
| 1330 |
MOZ_ASSERT(table2->entryCount() < table->entryCount()); |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
for (int32_t i = PropMap::Capacity - 1; i >= 0; i--) { |
| 1335 |
uint32_t index = uint32_t(i); |
| 1336 |
|
| 1337 |
// Only the last map can have holes, for entries following mapLength. |
| 1338 |
if (!curMap->hasKey(index)) { |
| 1339 |
MOZ_ASSERT(index > 0); |
| 1340 |
MOZ_ASSERT(curMap == this); |
| 1341 |
MOZ_ASSERT(index >= mapLength); |
| 1342 |
continue; |
| 1343 |
} |
| 1344 |
|
| 1345 |
// Check slot numbers are within slot span and never decreasing. |
| 1346 |
PropertyInfo prop = curMap->getPropertyInfo(i); |
| 1347 |
if (prop.hasSlot()) { |
| 1348 |
MOZ_ASSERT_IF((curMap != this || index < mapLength), |
| 1349 |
prop.slot() < obj->slotSpan()); |
| 1350 |
MOZ_ASSERT_IF(nextSlot.isSome(), *nextSlot >= prop.slot()); |
| 1351 |
nextSlot = mozilla::Some(prop.slot()); |
| 1352 |
} |
| 1353 |
|
| 1354 |
// All properties must be in the table. |
| 1355 |
if (table) { |
| 1356 |
PropertyKey key = curMap->getKey(index); |
| 1357 |
auto p = table->lookupRaw(key); |
| 1358 |
MOZ_ASSERT(p->map() == curMap); |
| 1359 |
MOZ_ASSERT(p->index() == index); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
if (!curMap->hasPrevious()) { |
| 1364 |
break; |
| 1365 |
} |
| 1366 |
nextMap = curMap; |
| 1367 |
curMap = curMap->asLinked()->previous()->asShared(); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
#endif // DEBUG |
| 1371 |
|
| 1372 |
JS::ubi::Node::Size JS::ubi::Concrete<PropMap>::size( |
| 1373 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 1374 |
Size size = js::gc::Arena::thingSize(get().asTenured().getAllocKind()); |
| 1375 |
size_t children = 0; |
| 1376 |
size_t tables = 0; |
| 1377 |
get().addSizeOfExcludingThis(mallocSizeOf, &children, &tables); |
| 1378 |
return size + children + tables; |
| 1379 |
} |