| 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/Shape-inl.h" |
| 8 |
|
| 9 |
#include "mozilla/MathAlgorithms.h" |
| 10 |
|
| 11 |
#include "gc/HashUtil.h" |
| 12 |
#include "js/friend/WindowProxy.h" // js::IsWindow |
| 13 |
#include "js/HashTable.h" |
| 14 |
#include "js/Printer.h" // js::GenericPrinter, js::Fprinter |
| 15 |
#include "js/UniquePtr.h" |
| 16 |
#include "vm/JSObject.h" |
| 17 |
#include "vm/JSONPrinter.h" // js::JSONPrinter |
| 18 |
#include "vm/ShapeZone.h" |
| 19 |
#include "vm/Watchtower.h" |
| 20 |
|
| 21 |
#include "gc/StableCellHasher-inl.h" |
| 22 |
#include "vm/JSContext-inl.h" |
| 23 |
#include "vm/JSObject-inl.h" |
| 24 |
#include "vm/NativeObject-inl.h" |
| 25 |
|
| 26 |
using namespace js; |
| 27 |
|
| 28 |
/* static */ |
| 29 |
bool Shape::replaceShape(JSContext* cx, HandleObject obj, |
| 30 |
ObjectFlags objectFlags, TaggedProto proto, |
| 31 |
uint32_t nfixed) { |
| 32 |
Shape* newShape; |
| 33 |
switch (obj->shape()->kind()) { |
| 34 |
case Kind::Shared: { |
| 35 |
Handle<NativeObject*> nobj = obj.as<NativeObject>(); |
| 36 |
if (nobj->shape()->propMap()) { |
| 37 |
Rooted<BaseShape*> base(cx, obj->shape()->base()); |
| 38 |
if (proto != base->proto()) { |
| 39 |
Rooted<TaggedProto> protoRoot(cx, proto); |
| 40 |
base = BaseShape::get(cx, base->clasp(), base->realm(), protoRoot); |
| 41 |
if (!base) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
} |
| 45 |
Rooted<SharedPropMap*> map(cx, nobj->sharedShape()->propMap()); |
| 46 |
uint32_t mapLength = nobj->shape()->propMapLength(); |
| 47 |
newShape = SharedShape::getPropMapShape(cx, base, nfixed, map, |
| 48 |
mapLength, objectFlags); |
| 49 |
} else { |
| 50 |
newShape = SharedShape::getInitialShape( |
| 51 |
cx, obj->shape()->getObjectClass(), obj->shape()->realm(), proto, |
| 52 |
nfixed, objectFlags); |
| 53 |
} |
| 54 |
break; |
| 55 |
} |
| 56 |
case Kind::Dictionary: { |
| 57 |
Handle<NativeObject*> nobj = obj.as<NativeObject>(); |
| 58 |
|
| 59 |
Rooted<BaseShape*> base(cx, nobj->shape()->base()); |
| 60 |
if (proto != base->proto()) { |
| 61 |
Rooted<TaggedProto> protoRoot(cx, proto); |
| 62 |
base = BaseShape::get(cx, nobj->getClass(), nobj->realm(), protoRoot); |
| 63 |
if (!base) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
Rooted<DictionaryPropMap*> map(cx, nobj->dictionaryShape()->propMap()); |
| 69 |
uint32_t mapLength = nobj->shape()->propMapLength(); |
| 70 |
newShape = |
| 71 |
DictionaryShape::new_(cx, base, objectFlags, nfixed, map, mapLength); |
| 72 |
break; |
| 73 |
} |
| 74 |
case Kind::Proxy: |
| 75 |
MOZ_ASSERT(nfixed == 0); |
| 76 |
newShape = |
| 77 |
ProxyShape::getShape(cx, obj->shape()->getObjectClass(), |
| 78 |
obj->shape()->realm(), proto, objectFlags); |
| 79 |
break; |
| 80 |
case Kind::WasmGC: |
| 81 |
MOZ_ASSERT(nfixed == 0); |
| 82 |
const wasm::RecGroup* recGroup = obj->shape()->asWasmGC().recGroup(); |
| 83 |
newShape = WasmGCShape::getShape(cx, obj->shape()->getObjectClass(), |
| 84 |
obj->shape()->realm(), proto, recGroup, |
| 85 |
objectFlags); |
| 86 |
break; |
| 87 |
} |
| 88 |
if (!newShape) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
obj->setShape(newShape); |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
/* static */ |
| 97 |
bool js::NativeObject::toDictionaryMode(JSContext* cx, |
| 98 |
Handle<NativeObject*> obj) { |
| 99 |
MOZ_ASSERT(!obj->inDictionaryMode()); |
| 100 |
MOZ_ASSERT(cx->isInsideCurrentCompartment(obj)); |
| 101 |
|
| 102 |
RootedTuple<NativeShape*, SharedPropMap*, DictionaryPropMap*, BaseShape*> |
| 103 |
roots(cx); |
| 104 |
|
| 105 |
RootedField<NativeShape*> shape(roots, obj->shape()); |
| 106 |
uint32_t span = obj->slotSpan(); |
| 107 |
|
| 108 |
uint32_t mapLength = shape->propMapLength(); |
| 109 |
|
| 110 |
// Clone the shared property map to an unshared dictionary map. |
| 111 |
RootedField<DictionaryPropMap*> dictMap(roots); |
| 112 |
|
| 113 |
if (mapLength > 0) { |
| 114 |
RootedField<SharedPropMap*> map(roots, shape->propMap()->asShared()); |
| 115 |
dictMap = SharedPropMap::toDictionaryMap(cx, map, mapLength); |
| 116 |
} else { |
| 117 |
// Create an empty dictMap |
| 118 |
dictMap = DictionaryPropMap::createEmpty(cx); |
| 119 |
} |
| 120 |
|
| 121 |
if (!dictMap) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
// Allocate and use a new dictionary shape. |
| 126 |
RootedField<BaseShape*> base(roots, shape->base()); |
| 127 |
shape = DictionaryShape::new_(cx, base, shape->objectFlags(), |
| 128 |
shape->numFixedSlots(), dictMap, mapLength); |
| 129 |
if (!shape) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
obj->setShape(shape); |
| 134 |
|
| 135 |
MOZ_ASSERT(obj->inDictionaryMode()); |
| 136 |
obj->setDictionaryModeSlotSpan(span); |
| 137 |
|
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
bool JSObject::reshapeForTeleporting(JSContext* cx, JS::HandleObject obj) { |
| 142 |
MOZ_ASSERT(obj->isUsedAsPrototype()); |
| 143 |
MOZ_ASSERT(obj->hasStaticPrototype(), |
| 144 |
"teleporting as a concept is only applicable to static " |
| 145 |
"(not dynamically-computed) prototypes"); |
| 146 |
MOZ_ASSERT(obj->is<NativeObject>()); |
| 147 |
|
| 148 |
Handle<NativeObject*> nobj = obj.as<NativeObject>(); |
| 149 |
if (!nobj->inDictionaryMode()) { |
| 150 |
return NativeObject::toDictionaryMode(cx, nobj); |
| 151 |
} |
| 152 |
|
| 153 |
return NativeObject::generateNewDictionaryShape(cx, nobj); |
| 154 |
} |
| 155 |
|
| 156 |
namespace js { |
| 157 |
|
| 158 |
class MOZ_RAII AutoCheckShapeConsistency { |
| 159 |
#ifdef DEBUG |
| 160 |
Handle<NativeObject*> obj_; |
| 161 |
#endif |
| 162 |
|
| 163 |
public: |
| 164 |
explicit AutoCheckShapeConsistency(Handle<NativeObject*> obj) |
| 165 |
#ifdef DEBUG |
| 166 |
: obj_(obj) |
| 167 |
#endif |
| 168 |
{ |
| 169 |
} |
| 170 |
|
| 171 |
#ifdef DEBUG |
| 172 |
~AutoCheckShapeConsistency() { obj_->checkShapeConsistency(); } |
| 173 |
#endif |
| 174 |
}; |
| 175 |
|
| 176 |
} // namespace js |
| 177 |
|
| 178 |
/* static */ MOZ_ALWAYS_INLINE bool |
| 179 |
NativeObject::maybeConvertToDictionaryForAdd(JSContext* cx, |
| 180 |
Handle<NativeObject*> obj) { |
| 181 |
if (obj->inDictionaryMode()) { |
| 182 |
return true; |
| 183 |
} |
| 184 |
SharedPropMap* map = obj->sharedShape()->propMap(); |
| 185 |
if (!map) { |
| 186 |
return true; |
| 187 |
} |
| 188 |
if (MOZ_LIKELY(!map->shouldConvertToDictionaryForAdd())) { |
| 189 |
return true; |
| 190 |
} |
| 191 |
return toDictionaryMode(cx, obj); |
| 192 |
} |
| 193 |
|
| 194 |
static void AssertValidCustomDataProp(NativeObject* obj, PropertyFlags flags) { |
| 195 |
// We only support custom data properties on ArrayObject and ArgumentsObject. |
| 196 |
// The mechanism is deprecated so we don't want to add new uses. |
| 197 |
MOZ_ASSERT(flags.isCustomDataProperty()); |
| 198 |
MOZ_ASSERT(!flags.isAccessorProperty()); |
| 199 |
MOZ_ASSERT(obj->is<ArrayObject>() || obj->is<ArgumentsObject>()); |
| 200 |
} |
| 201 |
|
| 202 |
/* static */ |
| 203 |
bool NativeObject::addCustomDataProperty(JSContext* cx, |
| 204 |
Handle<NativeObject*> obj, HandleId id, |
| 205 |
PropertyFlags flags) { |
| 206 |
MOZ_ASSERT(!id.isVoid()); |
| 207 |
MOZ_ASSERT(!id.isPrivateName()); |
| 208 |
MOZ_ASSERT(!obj->containsPure(id)); |
| 209 |
|
| 210 |
AutoCheckShapeConsistency check(obj); |
| 211 |
AssertValidCustomDataProp(obj, flags); |
| 212 |
|
| 213 |
if (!Watchtower::watchPropertyAdd(cx, obj, id)) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
if (!maybeConvertToDictionaryForAdd(cx, obj)) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 222 |
const JSClass* clasp = obj->shape()->getObjectClass(); |
| 223 |
|
| 224 |
if (obj->inDictionaryMode()) { |
| 225 |
// First generate a new dictionary shape so that the map can be mutated |
| 226 |
// without having to worry about OOM conditions. |
| 227 |
if (!NativeObject::generateNewDictionaryShape(cx, obj)) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
Rooted<DictionaryPropMap*> map(cx, obj->dictionaryShape()->propMap()); |
| 232 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 233 |
if (!DictionaryPropMap::addProperty(cx, clasp, &map, &mapLength, id, flags, |
| 234 |
SHAPE_INVALID_SLOT, &objectFlags)) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
|
| 238 |
obj->dictionaryShape()->updateNewShape(objectFlags, map, mapLength); |
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
Rooted<SharedPropMap*> map(cx, obj->sharedShape()->propMap()); |
| 243 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 244 |
if (!SharedPropMap::addCustomDataProperty(cx, clasp, &map, &mapLength, id, |
| 245 |
flags, &objectFlags)) { |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
Shape* shape = SharedShape::getPropMapShape(cx, obj->shape()->base(), |
| 250 |
obj->shape()->numFixedSlots(), |
| 251 |
map, mapLength, objectFlags); |
| 252 |
if (!shape) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
obj->setShape(shape); |
| 257 |
return true; |
| 258 |
} |
| 259 |
|
| 260 |
static ShapeSetForAdd* MakeShapeSetForAdd(SharedShape* shape1, |
| 261 |
SharedShape* shape2) { |
| 262 |
MOZ_ASSERT(shape1 != shape2); |
| 263 |
MOZ_ASSERT(shape1->propMapLength() == shape2->propMapLength()); |
| 264 |
|
| 265 |
auto hash = MakeUnique<ShapeSetForAdd>(); |
| 266 |
if (!hash || !hash->reserve(2)) { |
| 267 |
return nullptr; |
| 268 |
} |
| 269 |
|
| 270 |
PropertyInfoWithKey prop = shape1->lastProperty(); |
| 271 |
hash->putNewInfallible(ShapeForAddHasher::Lookup(prop.key(), prop.flags()), |
| 272 |
shape1); |
| 273 |
|
| 274 |
prop = shape2->lastProperty(); |
| 275 |
hash->putNewInfallible(ShapeForAddHasher::Lookup(prop.key(), prop.flags()), |
| 276 |
shape2); |
| 277 |
|
| 278 |
return hash.release(); |
| 279 |
} |
| 280 |
|
| 281 |
static MOZ_ALWAYS_INLINE SharedShape* LookupShapeForAdd(Shape* shape, |
| 282 |
PropertyKey key, |
| 283 |
PropertyFlags flags, |
| 284 |
uint32_t* slot) { |
| 285 |
ShapeCachePtr cache = shape->cache(); |
| 286 |
|
| 287 |
if (cache.isSingleShapeForAdd()) { |
| 288 |
SharedShape* newShape = cache.toSingleShapeForAdd(); |
| 289 |
if (newShape->lastPropertyMatchesForAdd(key, flags, slot)) { |
| 290 |
return newShape; |
| 291 |
} |
| 292 |
return nullptr; |
| 293 |
} |
| 294 |
|
| 295 |
if (cache.isShapeSetForAdd()) { |
| 296 |
ShapeSetForAdd* set = cache.toShapeSetForAdd(); |
| 297 |
ShapeForAddHasher::Lookup lookup(key, flags); |
| 298 |
if (auto p = set->lookup(lookup)) { |
| 299 |
SharedShape* newShape = *p; |
| 300 |
*slot = newShape->lastProperty().slot(); |
| 301 |
return newShape; |
| 302 |
} |
| 303 |
return nullptr; |
| 304 |
} |
| 305 |
|
| 306 |
MOZ_ASSERT(!cache.isForAdd()); |
| 307 |
return nullptr; |
| 308 |
} |
| 309 |
|
| 310 |
// Add shapes with a non-None ShapeCachePtr to the shapesWithCache list so that |
| 311 |
// these caches can be discarded on GC. |
| 312 |
static bool RegisterShapeCache(JSContext* cx, Shape* shape) { |
| 313 |
ShapeCachePtr cache = shape->cache(); |
| 314 |
if (!cache.isNone()) { |
| 315 |
// Already registered this shape. |
| 316 |
return true; |
| 317 |
} |
| 318 |
return cx->zone()->shapeZone().shapesWithCache.append(shape); |
| 319 |
} |
| 320 |
|
| 321 |
/* static */ |
| 322 |
bool NativeObject::addProperty(JSContext* cx, Handle<NativeObject*> obj, |
| 323 |
HandleId id, PropertyFlags flags, |
| 324 |
uint32_t* slot) { |
| 325 |
AutoCheckShapeConsistency check(obj); |
| 326 |
MOZ_ASSERT(!flags.isCustomDataProperty(), |
| 327 |
"Use addCustomDataProperty for custom data properties"); |
| 328 |
|
| 329 |
// The object must not contain a property named |id|. The object must be |
| 330 |
// extensible, but allow private fields and sparsifying dense elements. |
| 331 |
MOZ_ASSERT(!id.isVoid()); |
| 332 |
MOZ_ASSERT(!obj->containsPure(id)); |
| 333 |
MOZ_ASSERT_IF(!id.isPrivateName(), |
| 334 |
obj->isExtensible() || |
| 335 |
(id.isInt() && obj->containsDenseElement(id.toInt()))); |
| 336 |
if (!Watchtower::watchPropertyAdd(cx, obj, id)) { |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
if (!maybeConvertToDictionaryForAdd(cx, obj)) { |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
if (auto* shape = LookupShapeForAdd(obj->shape(), id, flags, slot)) { |
| 345 |
return obj->setShapeAndAddNewSlot(cx, shape, *slot); |
| 346 |
} |
| 347 |
|
| 348 |
if (obj->inDictionaryMode()) { |
| 349 |
// First generate a new dictionary shape so that the map and shape can be |
| 350 |
// mutated without having to worry about OOM conditions. |
| 351 |
if (!NativeObject::generateNewDictionaryShape(cx, obj)) { |
| 352 |
return false; |
| 353 |
} |
| 354 |
if (!allocDictionarySlot(cx, obj, slot)) { |
| 355 |
return false; |
| 356 |
} |
| 357 |
|
| 358 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 359 |
const JSClass* clasp = obj->shape()->getObjectClass(); |
| 360 |
|
| 361 |
Rooted<DictionaryPropMap*> map(cx, obj->shape()->propMap()->asDictionary()); |
| 362 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 363 |
if (!DictionaryPropMap::addProperty(cx, clasp, &map, &mapLength, id, flags, |
| 364 |
*slot, &objectFlags)) { |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
obj->dictionaryShape()->updateNewShape(objectFlags, map, mapLength); |
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 373 |
const JSClass* clasp = obj->shape()->getObjectClass(); |
| 374 |
|
| 375 |
Rooted<SharedPropMap*> map(cx, obj->sharedShape()->propMap()); |
| 376 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 377 |
|
| 378 |
if (!SharedPropMap::addProperty(cx, clasp, &map, &mapLength, id, flags, |
| 379 |
&objectFlags, slot)) { |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
bool allocatedNewShape; |
| 384 |
SharedShape* newShape = SharedShape::getPropMapShape( |
| 385 |
cx, obj->shape()->base(), obj->shape()->numFixedSlots(), map, mapLength, |
| 386 |
objectFlags, &allocatedNewShape); |
| 387 |
if (!newShape) { |
| 388 |
return false; |
| 389 |
} |
| 390 |
|
| 391 |
Shape* oldShape = obj->shape(); |
| 392 |
if (!obj->setShapeAndAddNewSlot(cx, newShape, *slot)) { |
| 393 |
return false; |
| 394 |
} |
| 395 |
|
| 396 |
// Add the new shape to the old shape's shape cache, to optimize this shape |
| 397 |
// transition. Don't do this if we just allocated a new shape, because that |
| 398 |
// suggests this may not be a hot transition that would benefit from the |
| 399 |
// cache. |
| 400 |
|
| 401 |
if (allocatedNewShape) { |
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
if (!RegisterShapeCache(cx, oldShape)) { |
| 406 |
// Ignore OOM, the cache is just an optimization. |
| 407 |
return true; |
| 408 |
} |
| 409 |
|
| 410 |
ShapeCachePtr& cache = oldShape->cacheRef(); |
| 411 |
if (!cache.isForAdd()) { |
| 412 |
cache.setSingleShapeForAdd(newShape); |
| 413 |
} else if (cache.isSingleShapeForAdd()) { |
| 414 |
SharedShape* prevShape = cache.toSingleShapeForAdd(); |
| 415 |
if (ShapeSetForAdd* set = MakeShapeSetForAdd(prevShape, newShape)) { |
| 416 |
cache.setShapeSetForAdd(set); |
| 417 |
AddCellMemory(oldShape, sizeof(ShapeSetForAdd), |
| 418 |
MemoryUse::ShapeSetForAdd); |
| 419 |
} |
| 420 |
} else { |
| 421 |
ShapeForAddHasher::Lookup lookup(id, flags); |
| 422 |
(void)cache.toShapeSetForAdd()->putNew(lookup, newShape); |
| 423 |
} |
| 424 |
|
| 425 |
return true; |
| 426 |
} |
| 427 |
|
| 428 |
void Shape::maybeCacheIterator(JSContext* cx, PropertyIteratorObject* iter) { |
| 429 |
if (!cache().isNone() && !cache().isIterator()) { |
| 430 |
// If we're already caching other shape data, skip caching the iterator. |
| 431 |
return; |
| 432 |
} |
| 433 |
if (MOZ_UNLIKELY(!RegisterShapeCache(cx, this))) { |
| 434 |
// Ignore OOM. The cache is just an optimization. |
| 435 |
return; |
| 436 |
} |
| 437 |
cacheRef().setIterator(iter); |
| 438 |
} |
| 439 |
|
| 440 |
/* static */ |
| 441 |
bool NativeObject::addPropertyInReservedSlot(JSContext* cx, |
| 442 |
Handle<NativeObject*> obj, |
| 443 |
HandleId id, uint32_t slot, |
| 444 |
PropertyFlags flags) { |
| 445 |
AutoCheckShapeConsistency check(obj); |
| 446 |
MOZ_ASSERT(!flags.isCustomDataProperty(), |
| 447 |
"Use addCustomDataProperty for custom data properties"); |
| 448 |
|
| 449 |
// The slot must be a reserved slot. |
| 450 |
MOZ_ASSERT(slot < JSCLASS_RESERVED_SLOTS(obj->getClass())); |
| 451 |
|
| 452 |
// The object must not contain a property named |id| and must be extensible. |
| 453 |
MOZ_ASSERT(!id.isVoid()); |
| 454 |
MOZ_ASSERT(!obj->containsPure(id)); |
| 455 |
MOZ_ASSERT(!id.isPrivateName()); |
| 456 |
MOZ_ASSERT(obj->isExtensible()); |
| 457 |
|
| 458 |
// The object must not be in dictionary mode. This simplifies the code below. |
| 459 |
MOZ_ASSERT(!obj->inDictionaryMode()); |
| 460 |
|
| 461 |
// We don't need to call Watchtower::watchPropertyAdd here because this isn't |
| 462 |
// used for any watched objects. |
| 463 |
MOZ_ASSERT(!Watchtower::watchesPropertyAdd(obj)); |
| 464 |
|
| 465 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 466 |
const JSClass* clasp = obj->shape()->getObjectClass(); |
| 467 |
|
| 468 |
Rooted<SharedPropMap*> map(cx, obj->sharedShape()->propMap()); |
| 469 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 470 |
if (!SharedPropMap::addPropertyInReservedSlot(cx, clasp, &map, &mapLength, id, |
| 471 |
flags, slot, &objectFlags)) { |
| 472 |
return false; |
| 473 |
} |
| 474 |
|
| 475 |
Shape* shape = SharedShape::getPropMapShape(cx, obj->shape()->base(), |
| 476 |
obj->shape()->numFixedSlots(), |
| 477 |
map, mapLength, objectFlags); |
| 478 |
if (!shape) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
obj->setShape(shape); |
| 482 |
|
| 483 |
MOZ_ASSERT(obj->getLastProperty().slot() == slot); |
| 484 |
return true; |
| 485 |
} |
| 486 |
|
| 487 |
/* |
| 488 |
* Assert some invariants that should hold when changing properties. It's the |
| 489 |
* responsibility of the callers to ensure these hold. |
| 490 |
*/ |
| 491 |
static void AssertCanChangeFlags(PropertyInfo prop, PropertyFlags flags) { |
| 492 |
#ifdef DEBUG |
| 493 |
if (prop.configurable()) { |
| 494 |
return; |
| 495 |
} |
| 496 |
|
| 497 |
// A non-configurable property must stay non-configurable. |
| 498 |
MOZ_ASSERT(!flags.configurable()); |
| 499 |
|
| 500 |
// Reject attempts to turn a non-configurable data property into an accessor |
| 501 |
// or custom data property. |
| 502 |
MOZ_ASSERT_IF(prop.isDataProperty(), flags.isDataProperty()); |
| 503 |
|
| 504 |
// Reject attempts to turn a non-configurable accessor property into a data |
| 505 |
// property or custom data property. |
| 506 |
MOZ_ASSERT_IF(prop.isAccessorProperty(), flags.isAccessorProperty()); |
| 507 |
#endif |
| 508 |
} |
| 509 |
|
| 510 |
static void AssertValidArrayIndex(NativeObject* obj, jsid id) { |
| 511 |
#ifdef DEBUG |
| 512 |
if (obj->is<ArrayObject>()) { |
| 513 |
ArrayObject* arr = &obj->as<ArrayObject>(); |
| 514 |
uint32_t index; |
| 515 |
if (IdIsIndex(id, &index)) { |
| 516 |
MOZ_ASSERT(index < arr->length() || arr->lengthIsWritable()); |
| 517 |
} |
| 518 |
} |
| 519 |
#endif |
| 520 |
} |
| 521 |
|
| 522 |
/* static */ |
| 523 |
bool NativeObject::changeProperty(JSContext* cx, Handle<NativeObject*> obj, |
| 524 |
HandleId id, PropertyFlags flags, |
| 525 |
uint32_t* slotOut) { |
| 526 |
MOZ_ASSERT(!id.isVoid()); |
| 527 |
|
| 528 |
AutoCheckShapeConsistency check(obj); |
| 529 |
AssertValidArrayIndex(obj, id); |
| 530 |
MOZ_ASSERT(!flags.isCustomDataProperty(), |
| 531 |
"Use changeCustomDataPropAttributes for custom data properties"); |
| 532 |
|
| 533 |
Rooted<PropMap*> map(cx, obj->shape()->propMap()); |
| 534 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 535 |
|
| 536 |
uint32_t propIndex; |
| 537 |
Rooted<PropMap*> propMap(cx, map->lookup(cx, mapLength, id, &propIndex)); |
| 538 |
MOZ_ASSERT(propMap); |
| 539 |
|
| 540 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 541 |
|
| 542 |
PropertyInfo oldProp = propMap->getPropertyInfo(propIndex); |
| 543 |
AssertCanChangeFlags(oldProp, flags); |
| 544 |
|
| 545 |
if (oldProp.flags() != flags) { |
| 546 |
if (!Watchtower::watchPropertyFlagsChange(cx, obj, id, oldProp, flags)) { |
| 547 |
return false; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
if (oldProp.isAccessorProperty()) { |
| 552 |
objectFlags.setFlag(ObjectFlag::HadGetterSetterChange); |
| 553 |
} |
| 554 |
|
| 555 |
// If the property flags are not changing, the only thing we have to do is |
| 556 |
// update the object flags. This prevents a dictionary mode conversion below. |
| 557 |
if (oldProp.flags() == flags) { |
| 558 |
*slotOut = oldProp.slot(); |
| 559 |
if (objectFlags == obj->shape()->objectFlags()) { |
| 560 |
return true; |
| 561 |
} |
| 562 |
return Shape::replaceShape(cx, obj, objectFlags, obj->shape()->proto(), |
| 563 |
obj->shape()->numFixedSlots()); |
| 564 |
} |
| 565 |
|
| 566 |
const JSClass* clasp = obj->shape()->getObjectClass(); |
| 567 |
|
| 568 |
if (map->isShared()) { |
| 569 |
// Fast path for changing the last property in a SharedPropMap. Call |
| 570 |
// getPrevious to "remove" the last property and then call addProperty |
| 571 |
// to re-add the last property with the new flags. |
| 572 |
if (propMap == map && propIndex == mapLength - 1) { |
| 573 |
MOZ_ASSERT(obj->getLastProperty().key() == id); |
| 574 |
|
| 575 |
Rooted<SharedPropMap*> sharedMap(cx, map->asShared()); |
| 576 |
SharedPropMap::getPrevious(&sharedMap, &mapLength); |
| 577 |
|
| 578 |
if (MOZ_LIKELY(oldProp.hasSlot())) { |
| 579 |
*slotOut = oldProp.slot(); |
| 580 |
if (!SharedPropMap::addPropertyWithKnownSlot(cx, clasp, &sharedMap, |
| 581 |
&mapLength, id, flags, |
| 582 |
*slotOut, &objectFlags)) { |
| 583 |
return false; |
| 584 |
} |
| 585 |
} else { |
| 586 |
if (!SharedPropMap::addProperty(cx, clasp, &sharedMap, &mapLength, id, |
| 587 |
flags, &objectFlags, slotOut)) { |
| 588 |
return false; |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
SharedShape* newShape = SharedShape::getPropMapShape( |
| 593 |
cx, obj->shape()->base(), obj->shape()->numFixedSlots(), sharedMap, |
| 594 |
mapLength, objectFlags); |
| 595 |
if (!newShape) { |
| 596 |
return false; |
| 597 |
} |
| 598 |
|
| 599 |
if (MOZ_LIKELY(oldProp.hasSlot())) { |
| 600 |
MOZ_ASSERT(obj->sharedShape()->slotSpan() == newShape->slotSpan()); |
| 601 |
obj->setShape(newShape); |
| 602 |
return true; |
| 603 |
} |
| 604 |
return obj->setShapeAndAddNewSlot(cx, newShape, *slotOut); |
| 605 |
} |
| 606 |
|
| 607 |
// Changing a non-last property. Switch to dictionary mode and relookup |
| 608 |
// pointers for the new dictionary map. |
| 609 |
if (!NativeObject::toDictionaryMode(cx, obj)) { |
| 610 |
return false; |
| 611 |
} |
| 612 |
map = obj->shape()->propMap(); |
| 613 |
propMap = map->lookup(cx, mapLength, id, &propIndex); |
| 614 |
MOZ_ASSERT(propMap); |
| 615 |
} else { |
| 616 |
if (!NativeObject::generateNewDictionaryShape(cx, obj)) { |
| 617 |
return false; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
// The object has a new dictionary shape (see toDictionaryMode and |
| 622 |
// generateNewDictionaryShape calls above), so we can mutate the map and shape |
| 623 |
// in place. |
| 624 |
|
| 625 |
MOZ_ASSERT(map->isDictionary()); |
| 626 |
MOZ_ASSERT(propMap->isDictionary()); |
| 627 |
|
| 628 |
uint32_t slot = oldProp.hasSlot() ? oldProp.slot() : SHAPE_INVALID_SLOT; |
| 629 |
if (slot == SHAPE_INVALID_SLOT) { |
| 630 |
if (!allocDictionarySlot(cx, obj, &slot)) { |
| 631 |
return false; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
propMap->asDictionary()->changeProperty(cx, clasp, propIndex, flags, slot, |
| 636 |
&objectFlags); |
| 637 |
obj->dictionaryShape()->setObjectFlagsOfNewShape(objectFlags); |
| 638 |
|
| 639 |
*slotOut = slot; |
| 640 |
return true; |
| 641 |
} |
| 642 |
|
| 643 |
/* static */ |
| 644 |
bool NativeObject::changeCustomDataPropAttributes(JSContext* cx, |
| 645 |
Handle<NativeObject*> obj, |
| 646 |
HandleId id, |
| 647 |
PropertyFlags flags) { |
| 648 |
MOZ_ASSERT(!id.isVoid()); |
| 649 |
|
| 650 |
AutoCheckShapeConsistency check(obj); |
| 651 |
AssertValidArrayIndex(obj, id); |
| 652 |
AssertValidCustomDataProp(obj, flags); |
| 653 |
|
| 654 |
Rooted<PropMap*> map(cx, obj->shape()->propMap()); |
| 655 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 656 |
|
| 657 |
uint32_t propIndex; |
| 658 |
Rooted<PropMap*> propMap(cx, map->lookup(cx, mapLength, id, &propIndex)); |
| 659 |
MOZ_ASSERT(propMap); |
| 660 |
|
| 661 |
PropertyInfo oldProp = propMap->getPropertyInfo(propIndex); |
| 662 |
MOZ_ASSERT(oldProp.isCustomDataProperty()); |
| 663 |
AssertCanChangeFlags(oldProp, flags); |
| 664 |
|
| 665 |
// If the property flags are not changing, we're done. |
| 666 |
if (oldProp.flags() == flags) { |
| 667 |
return true; |
| 668 |
} |
| 669 |
|
| 670 |
if (!Watchtower::watchPropertyFlagsChange(cx, obj, id, oldProp, flags)) { |
| 671 |
return false; |
| 672 |
} |
| 673 |
|
| 674 |
const JSClass* clasp = obj->shape()->getObjectClass(); |
| 675 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 676 |
|
| 677 |
if (map->isShared()) { |
| 678 |
// Fast path for changing the last property in a SharedPropMap. Call |
| 679 |
// getPrevious to "remove" the last property and then call |
| 680 |
// addCustomDataProperty to re-add the last property with the new flags. |
| 681 |
if (propMap == map && propIndex == mapLength - 1) { |
| 682 |
MOZ_ASSERT(obj->getLastProperty().key() == id); |
| 683 |
|
| 684 |
Rooted<SharedPropMap*> sharedMap(cx, map->asShared()); |
| 685 |
SharedPropMap::getPrevious(&sharedMap, &mapLength); |
| 686 |
|
| 687 |
if (!SharedPropMap::addCustomDataProperty( |
| 688 |
cx, clasp, &sharedMap, &mapLength, id, flags, &objectFlags)) { |
| 689 |
return false; |
| 690 |
} |
| 691 |
|
| 692 |
Shape* newShape = SharedShape::getPropMapShape( |
| 693 |
cx, obj->shape()->base(), obj->shape()->numFixedSlots(), sharedMap, |
| 694 |
mapLength, objectFlags); |
| 695 |
if (!newShape) { |
| 696 |
return false; |
| 697 |
} |
| 698 |
obj->setShape(newShape); |
| 699 |
return true; |
| 700 |
} |
| 701 |
|
| 702 |
// Changing a non-last property. Switch to dictionary mode and relookup |
| 703 |
// pointers for the new dictionary map. |
| 704 |
if (!NativeObject::toDictionaryMode(cx, obj)) { |
| 705 |
return false; |
| 706 |
} |
| 707 |
map = obj->shape()->propMap(); |
| 708 |
propMap = map->lookup(cx, mapLength, id, &propIndex); |
| 709 |
MOZ_ASSERT(propMap); |
| 710 |
} else { |
| 711 |
if (!NativeObject::generateNewDictionaryShape(cx, obj)) { |
| 712 |
return false; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
// The object has a new dictionary shape (see toDictionaryMode and |
| 717 |
// generateNewDictionaryShape calls above), so we can mutate the map and shape |
| 718 |
// in place. |
| 719 |
|
| 720 |
MOZ_ASSERT(map->isDictionary()); |
| 721 |
MOZ_ASSERT(propMap->isDictionary()); |
| 722 |
|
| 723 |
propMap->asDictionary()->changePropertyFlags(cx, clasp, propIndex, flags, |
| 724 |
&objectFlags); |
| 725 |
obj->dictionaryShape()->setObjectFlagsOfNewShape(objectFlags); |
| 726 |
return true; |
| 727 |
} |
| 728 |
|
| 729 |
void NativeObject::maybeFreeDictionaryPropSlots(JSContext* cx, |
| 730 |
DictionaryPropMap* map, |
| 731 |
uint32_t mapLength) { |
| 732 |
// We can free all non-reserved slots if there are no properties left. We also |
| 733 |
// handle the case where there's a single slotless property, to support arrays |
| 734 |
// (array.length is a custom data property). |
| 735 |
|
| 736 |
MOZ_ASSERT(dictionaryShape()->propMap() == map); |
| 737 |
MOZ_ASSERT(shape()->propMapLength() == mapLength); |
| 738 |
|
| 739 |
if (mapLength > 1 || map->previous()) { |
| 740 |
return; |
| 741 |
} |
| 742 |
if (mapLength == 1 && map->getPropertyInfo(0).hasSlot()) { |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
uint32_t oldSpan = dictionaryModeSlotSpan(); |
| 747 |
uint32_t newSpan = JSCLASS_RESERVED_SLOTS(getClass()); |
| 748 |
if (oldSpan == newSpan) { |
| 749 |
return; |
| 750 |
} |
| 751 |
|
| 752 |
MOZ_ASSERT(newSpan < oldSpan); |
| 753 |
|
| 754 |
// Trigger write barriers on the old slots before reallocating. |
| 755 |
prepareSlotRangeForOverwrite(newSpan, oldSpan); |
| 756 |
invalidateSlotRange(newSpan, oldSpan); |
| 757 |
|
| 758 |
uint32_t oldCapacity = numDynamicSlots(); |
| 759 |
uint32_t newCapacity = |
| 760 |
calculateDynamicSlots(numFixedSlots(), newSpan, getClass()); |
| 761 |
if (newCapacity < oldCapacity) { |
| 762 |
shrinkSlots(cx, oldCapacity, newCapacity); |
| 763 |
} |
| 764 |
|
| 765 |
setDictionaryModeSlotSpan(newSpan); |
| 766 |
map->setFreeList(SHAPE_INVALID_SLOT); |
| 767 |
} |
| 768 |
|
| 769 |
void NativeObject::setShapeAndRemoveLastSlot(JSContext* cx, |
| 770 |
SharedShape* newShape, |
| 771 |
uint32_t slot) { |
| 772 |
MOZ_ASSERT(!inDictionaryMode()); |
| 773 |
MOZ_ASSERT(newShape->isShared()); |
| 774 |
MOZ_ASSERT(newShape->slotSpan() == slot); |
| 775 |
|
| 776 |
uint32_t numFixed = newShape->numFixedSlots(); |
| 777 |
if (slot < numFixed) { |
| 778 |
setFixedSlot(slot, UndefinedValue()); |
| 779 |
} else { |
| 780 |
setDynamicSlot(numFixed, slot, UndefinedValue()); |
| 781 |
uint32_t oldCapacity = numDynamicSlots(); |
| 782 |
uint32_t newCapacity = calculateDynamicSlots(numFixed, slot, getClass()); |
| 783 |
MOZ_ASSERT(newCapacity <= oldCapacity); |
| 784 |
if (newCapacity < oldCapacity) { |
| 785 |
shrinkSlots(cx, oldCapacity, newCapacity); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
setShape(newShape); |
| 790 |
} |
| 791 |
|
| 792 |
/* static */ |
| 793 |
bool NativeObject::removeProperty(JSContext* cx, Handle<NativeObject*> obj, |
| 794 |
HandleId id) { |
| 795 |
AutoCheckShapeConsistency check(obj); |
| 796 |
|
| 797 |
Rooted<PropMap*> map(cx, obj->shape()->propMap()); |
| 798 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 799 |
|
| 800 |
AutoKeepPropMapTables keep(cx); |
| 801 |
PropMapTable* table; |
| 802 |
PropMapTable::Ptr ptr; |
| 803 |
Rooted<PropMap*> propMap(cx); |
| 804 |
uint32_t propIndex; |
| 805 |
if (!PropMap::lookupForRemove(cx, map, mapLength, id, keep, propMap.address(), |
| 806 |
&propIndex, &table, &ptr)) { |
| 807 |
return false; |
| 808 |
} |
| 809 |
|
| 810 |
if (!propMap) { |
| 811 |
return true; |
| 812 |
} |
| 813 |
|
| 814 |
if (!Watchtower::watchPropertyRemove(cx, obj, id)) { |
| 815 |
return false; |
| 816 |
} |
| 817 |
|
| 818 |
PropertyInfo prop = propMap->getPropertyInfo(propIndex); |
| 819 |
|
| 820 |
// If we're removing an accessor property, ensure the HadGetterSetterChange |
| 821 |
// object flag is set. This is necessary because the slot holding the |
| 822 |
// GetterSetter can be changed indirectly by removing the property and then |
| 823 |
// adding it back with a different GetterSetter value but the same shape. |
| 824 |
if (prop.isAccessorProperty() && !obj->hadGetterSetterChange()) { |
| 825 |
if (!NativeObject::setHadGetterSetterChange(cx, obj)) { |
| 826 |
return false; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
if (map->isShared()) { |
| 831 |
// Fast path for removing the last property from a SharedPropMap. In this |
| 832 |
// case we can just call getPrevious and then look up a shape for the |
| 833 |
// resulting map/mapLength. |
| 834 |
if (propMap == map && propIndex == mapLength - 1) { |
| 835 |
MOZ_ASSERT(obj->getLastProperty().key() == id); |
| 836 |
|
| 837 |
Rooted<SharedPropMap*> sharedMap(cx, map->asShared()); |
| 838 |
SharedPropMap::getPrevious(&sharedMap, &mapLength); |
| 839 |
|
| 840 |
SharedShape* shape = obj->sharedShape(); |
| 841 |
SharedShape* newShape; |
| 842 |
if (sharedMap) { |
| 843 |
newShape = SharedShape::getPropMapShape( |
| 844 |
cx, shape->base(), shape->numFixedSlots(), sharedMap, mapLength, |
| 845 |
shape->objectFlags()); |
| 846 |
} else { |
| 847 |
newShape = SharedShape::getInitialShape( |
| 848 |
cx, shape->getObjectClass(), shape->realm(), shape->proto(), |
| 849 |
shape->numFixedSlots(), shape->objectFlags()); |
| 850 |
} |
| 851 |
if (!newShape) { |
| 852 |
return false; |
| 853 |
} |
| 854 |
|
| 855 |
if (MOZ_LIKELY(prop.hasSlot())) { |
| 856 |
if (MOZ_LIKELY(prop.slot() == newShape->slotSpan())) { |
| 857 |
obj->setShapeAndRemoveLastSlot(cx, newShape, prop.slot()); |
| 858 |
return true; |
| 859 |
} |
| 860 |
// Uncommon case: the property is stored in a reserved slot. |
| 861 |
// See NativeObject::addPropertyInReservedSlot. |
| 862 |
MOZ_ASSERT(prop.slot() < JSCLASS_RESERVED_SLOTS(obj->getClass())); |
| 863 |
obj->setSlot(prop.slot(), UndefinedValue()); |
| 864 |
} |
| 865 |
obj->setShape(newShape); |
| 866 |
return true; |
| 867 |
} |
| 868 |
|
| 869 |
// Removing a non-last property. Switch to dictionary mode and relookup |
| 870 |
// pointers for the new dictionary map. |
| 871 |
if (!NativeObject::toDictionaryMode(cx, obj)) { |
| 872 |
return false; |
| 873 |
} |
| 874 |
map = obj->shape()->propMap(); |
| 875 |
if (!PropMap::lookupForRemove(cx, map, mapLength, id, keep, |
| 876 |
propMap.address(), &propIndex, &table, |
| 877 |
&ptr)) { |
| 878 |
return false; |
| 879 |
} |
| 880 |
} else { |
| 881 |
if (!NativeObject::generateNewDictionaryShape(cx, obj)) { |
| 882 |
return false; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
// The object has a new dictionary shape (see toDictionaryMode and |
| 887 |
// generateNewDictionaryShape calls above), so we can mutate the map and shape |
| 888 |
// in place. |
| 889 |
|
| 890 |
MOZ_ASSERT(map->isDictionary()); |
| 891 |
MOZ_ASSERT(table); |
| 892 |
MOZ_ASSERT(prop == ptr->propertyInfo()); |
| 893 |
|
| 894 |
Rooted<DictionaryPropMap*> dictMap(cx, map->asDictionary()); |
| 895 |
|
| 896 |
// If the property has a slot, free its slot number. |
| 897 |
if (prop.hasSlot()) { |
| 898 |
obj->freeDictionarySlot(prop.slot()); |
| 899 |
} |
| 900 |
|
| 901 |
DictionaryPropMap::removeProperty(cx, &dictMap, &mapLength, table, ptr); |
| 902 |
|
| 903 |
obj->dictionaryShape()->updateNewShape(obj->shape()->objectFlags(), dictMap, |
| 904 |
mapLength); |
| 905 |
|
| 906 |
// If we just deleted the last property, consider shrinking the slots. We only |
| 907 |
// do this if there are a lot of slots, to avoid allocating/freeing dynamic |
| 908 |
// slots repeatedly. |
| 909 |
static constexpr size_t MinSlotSpanForFree = 64; |
| 910 |
if (obj->dictionaryModeSlotSpan() >= MinSlotSpanForFree) { |
| 911 |
obj->maybeFreeDictionaryPropSlots(cx, dictMap, mapLength); |
| 912 |
} |
| 913 |
|
| 914 |
return true; |
| 915 |
} |
| 916 |
|
| 917 |
/* static */ |
| 918 |
bool NativeObject::densifySparseElements(JSContext* cx, |
| 919 |
Handle<NativeObject*> obj) { |
| 920 |
AutoCheckShapeConsistency check(obj); |
| 921 |
MOZ_ASSERT(obj->inDictionaryMode()); |
| 922 |
|
| 923 |
// First generate a new dictionary shape so that the shape and map can then |
| 924 |
// be updated infallibly. |
| 925 |
if (!NativeObject::generateNewDictionaryShape(cx, obj)) { |
| 926 |
return false; |
| 927 |
} |
| 928 |
|
| 929 |
Rooted<DictionaryPropMap*> map(cx, obj->shape()->propMap()->asDictionary()); |
| 930 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 931 |
|
| 932 |
DictionaryPropMap::densifyElements(cx, &map, &mapLength, obj); |
| 933 |
|
| 934 |
// All indexed properties on the object are now dense. Clear the indexed |
| 935 |
// flag so that we will not start using sparse indexes again if we need |
| 936 |
// to grow the object. |
| 937 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 938 |
objectFlags.clearFlag(ObjectFlag::Indexed); |
| 939 |
|
| 940 |
obj->dictionaryShape()->updateNewShape(objectFlags, map, mapLength); |
| 941 |
|
| 942 |
obj->maybeFreeDictionaryPropSlots(cx, map, mapLength); |
| 943 |
|
| 944 |
return true; |
| 945 |
} |
| 946 |
|
| 947 |
// static |
| 948 |
bool NativeObject::freezeOrSealProperties(JSContext* cx, |
| 949 |
Handle<NativeObject*> obj, |
| 950 |
IntegrityLevel level) { |
| 951 |
AutoCheckShapeConsistency check(obj); |
| 952 |
|
| 953 |
if (!Watchtower::watchFreezeOrSeal(cx, obj, level)) { |
| 954 |
return false; |
| 955 |
} |
| 956 |
|
| 957 |
uint32_t mapLength = obj->shape()->propMapLength(); |
| 958 |
MOZ_ASSERT(mapLength > 0); |
| 959 |
|
| 960 |
const JSClass* clasp = obj->shape()->getObjectClass(); |
| 961 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 962 |
|
| 963 |
if (obj->inDictionaryMode()) { |
| 964 |
// First generate a new dictionary shape so that the map and shape can be |
| 965 |
// updated infallibly. |
| 966 |
if (!generateNewDictionaryShape(cx, obj)) { |
| 967 |
return false; |
| 968 |
} |
| 969 |
DictionaryPropMap* map = obj->dictionaryShape()->propMap(); |
| 970 |
map->freezeOrSealProperties(cx, level, clasp, mapLength, &objectFlags); |
| 971 |
obj->dictionaryShape()->updateNewShape(objectFlags, map, mapLength); |
| 972 |
return true; |
| 973 |
} |
| 974 |
|
| 975 |
Rooted<SharedPropMap*> map(cx, obj->sharedShape()->propMap()); |
| 976 |
if (!SharedPropMap::freezeOrSealProperties(cx, level, clasp, &map, mapLength, |
| 977 |
&objectFlags)) { |
| 978 |
return false; |
| 979 |
} |
| 980 |
|
| 981 |
SharedShape* newShape = SharedShape::getPropMapShape( |
| 982 |
cx, obj->shape()->base(), obj->numFixedSlots(), map, mapLength, |
| 983 |
objectFlags); |
| 984 |
if (!newShape) { |
| 985 |
return false; |
| 986 |
} |
| 987 |
MOZ_ASSERT(obj->sharedShape()->slotSpan() == newShape->slotSpan()); |
| 988 |
|
| 989 |
obj->setShape(newShape); |
| 990 |
return true; |
| 991 |
} |
| 992 |
|
| 993 |
/* static */ |
| 994 |
bool NativeObject::generateNewDictionaryShape(JSContext* cx, |
| 995 |
Handle<NativeObject*> obj) { |
| 996 |
// Clone the current dictionary shape to a new shape. This ensures ICs and |
| 997 |
// other shape guards are properly invalidated before we start mutating the |
| 998 |
// map or new shape. |
| 999 |
|
| 1000 |
MOZ_ASSERT(obj->inDictionaryMode()); |
| 1001 |
|
| 1002 |
Shape* shape = DictionaryShape::new_(cx, obj); |
| 1003 |
if (!shape) { |
| 1004 |
return false; |
| 1005 |
} |
| 1006 |
|
| 1007 |
obj->setShape(shape); |
| 1008 |
return true; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* static */ |
| 1012 |
bool JSObject::setFlag(JSContext* cx, HandleObject obj, ObjectFlag flag) { |
| 1013 |
MOZ_ASSERT(cx->compartment() == obj->compartment()); |
| 1014 |
|
| 1015 |
if (obj->hasFlag(flag)) { |
| 1016 |
return true; |
| 1017 |
} |
| 1018 |
|
| 1019 |
ObjectFlags objectFlags = obj->shape()->objectFlags(); |
| 1020 |
objectFlags.setFlag(flag); |
| 1021 |
|
| 1022 |
uint32_t numFixed = |
| 1023 |
obj->is<NativeObject>() ? obj->as<NativeObject>().numFixedSlots() : 0; |
| 1024 |
return Shape::replaceShape(cx, obj, objectFlags, obj->shape()->proto(), |
| 1025 |
numFixed); |
| 1026 |
} |
| 1027 |
|
| 1028 |
static bool SetObjectIsUsedAsPrototype(JSContext* cx, Handle<JSObject*> proto) { |
| 1029 |
MOZ_ASSERT(!proto->isUsedAsPrototype()); |
| 1030 |
|
| 1031 |
// Ensure the proto object has a unique id to prevent OOM crashes below. |
| 1032 |
uint64_t unused; |
| 1033 |
if (!gc::GetOrCreateUniqueId(proto, &unused)) { |
| 1034 |
ReportOutOfMemory(cx); |
| 1035 |
return false; |
| 1036 |
} |
| 1037 |
|
| 1038 |
return JSObject::setIsUsedAsPrototype(cx, proto); |
| 1039 |
} |
| 1040 |
|
| 1041 |
/* static */ |
| 1042 |
bool JSObject::setProtoUnchecked(JSContext* cx, HandleObject obj, |
| 1043 |
Handle<TaggedProto> proto) { |
| 1044 |
MOZ_ASSERT(cx->compartment() == obj->compartment()); |
| 1045 |
MOZ_ASSERT(!obj->staticPrototypeIsImmutable()); |
| 1046 |
MOZ_ASSERT_IF(!obj->is<ProxyObject>(), obj->nonProxyIsExtensible()); |
| 1047 |
MOZ_ASSERT(obj->shape()->proto() != proto); |
| 1048 |
|
| 1049 |
// Notify Watchtower of this proto change, so it can properly invalidate shape |
| 1050 |
// teleporting and other optimizations. |
| 1051 |
if (!Watchtower::watchProtoChange(cx, obj)) { |
| 1052 |
return false; |
| 1053 |
} |
| 1054 |
|
| 1055 |
if (proto.isObject() && !proto.toObject()->isUsedAsPrototype()) { |
| 1056 |
RootedObject protoObj(cx, proto.toObject()); |
| 1057 |
if (!SetObjectIsUsedAsPrototype(cx, protoObj)) { |
| 1058 |
return false; |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
uint32_t numFixed = |
| 1063 |
obj->is<NativeObject>() ? obj->as<NativeObject>().numFixedSlots() : 0; |
| 1064 |
return Shape::replaceShape(cx, obj, obj->shape()->objectFlags(), proto, |
| 1065 |
numFixed); |
| 1066 |
} |
| 1067 |
|
| 1068 |
/* static */ |
| 1069 |
bool NativeObject::changeNumFixedSlotsAfterSwap(JSContext* cx, |
| 1070 |
Handle<NativeObject*> obj, |
| 1071 |
uint32_t nfixed) { |
| 1072 |
MOZ_ASSERT(nfixed != obj->shape()->numFixedSlots()); |
| 1073 |
|
| 1074 |
return Shape::replaceShape(cx, obj, obj->shape()->objectFlags(), |
| 1075 |
obj->shape()->proto(), nfixed); |
| 1076 |
} |
| 1077 |
|
| 1078 |
BaseShape::BaseShape(JSContext* cx, const JSClass* clasp, JS::Realm* realm, |
| 1079 |
TaggedProto proto) |
| 1080 |
: TenuredCellWithNonGCPointer(clasp), realm_(realm), proto_(proto) { |
| 1081 |
#ifdef DEBUG |
| 1082 |
AssertJSClassInvariants(clasp); |
| 1083 |
#endif |
| 1084 |
|
| 1085 |
MOZ_ASSERT_IF(proto.isObject(), |
| 1086 |
compartment() == proto.toObject()->compartment()); |
| 1087 |
MOZ_ASSERT_IF(proto.isObject(), proto.toObject()->isUsedAsPrototype()); |
| 1088 |
|
| 1089 |
// Windows may not appear on prototype chains. |
| 1090 |
MOZ_ASSERT_IF(proto.isObject(), !IsWindow(proto.toObject())); |
| 1091 |
|
| 1092 |
if (MOZ_UNLIKELY(clasp->emulatesUndefined())) { |
| 1093 |
cx->runtime()->hasSeenObjectEmulateUndefinedFuse.ref().popFuse(cx); |
| 1094 |
} |
| 1095 |
|
| 1096 |
#ifdef DEBUG |
| 1097 |
if (GlobalObject* global = realm->unsafeUnbarrieredMaybeGlobal()) { |
| 1098 |
AssertTargetIsNotGray(global); |
| 1099 |
} |
| 1100 |
#endif |
| 1101 |
} |
| 1102 |
|
| 1103 |
/* static */ |
| 1104 |
BaseShape* BaseShape::get(JSContext* cx, const JSClass* clasp, JS::Realm* realm, |
| 1105 |
Handle<TaggedProto> proto) { |
| 1106 |
auto& table = cx->zone()->shapeZone().baseShapes; |
| 1107 |
|
| 1108 |
using Lookup = BaseShapeHasher::Lookup; |
| 1109 |
|
| 1110 |
auto p = MakeDependentAddPtr(cx, table, Lookup(clasp, realm, proto)); |
| 1111 |
if (p) { |
| 1112 |
return *p; |
| 1113 |
} |
| 1114 |
|
| 1115 |
BaseShape* nbase = cx->newCell<BaseShape>(cx, clasp, realm, proto); |
| 1116 |
if (!nbase) { |
| 1117 |
return nullptr; |
| 1118 |
} |
| 1119 |
|
| 1120 |
if (!p.add(cx, table, Lookup(clasp, realm, proto), nbase)) { |
| 1121 |
return nullptr; |
| 1122 |
} |
| 1123 |
|
| 1124 |
return nbase; |
| 1125 |
} |
| 1126 |
|
| 1127 |
// static |
| 1128 |
SharedShape* SharedShape::new_(JSContext* cx, Handle<BaseShape*> base, |
| 1129 |
ObjectFlags objectFlags, uint32_t nfixed, |
| 1130 |
Handle<SharedPropMap*> map, uint32_t mapLength) { |
| 1131 |
return cx->newCell<SharedShape>(base, objectFlags, nfixed, map, mapLength); |
| 1132 |
} |
| 1133 |
|
| 1134 |
// static |
| 1135 |
DictionaryShape* DictionaryShape::new_(JSContext* cx, Handle<BaseShape*> base, |
| 1136 |
ObjectFlags objectFlags, uint32_t nfixed, |
| 1137 |
Handle<DictionaryPropMap*> map, |
| 1138 |
uint32_t mapLength) { |
| 1139 |
return cx->newCell<DictionaryShape>(base, objectFlags, nfixed, map, |
| 1140 |
mapLength); |
| 1141 |
} |
| 1142 |
|
| 1143 |
DictionaryShape::DictionaryShape(NativeObject* nobj) |
| 1144 |
: DictionaryShape(nobj->shape()->base(), nobj->shape()->objectFlags(), |
| 1145 |
nobj->shape()->numFixedSlots(), |
| 1146 |
nobj->dictionaryShape()->propMap(), |
| 1147 |
nobj->shape()->propMapLength()) {} |
| 1148 |
|
| 1149 |
// static |
| 1150 |
DictionaryShape* DictionaryShape::new_(JSContext* cx, |
| 1151 |
Handle<NativeObject*> obj) { |
| 1152 |
return cx->newCell<DictionaryShape>(obj); |
| 1153 |
} |
| 1154 |
|
| 1155 |
// static |
| 1156 |
ProxyShape* ProxyShape::new_(JSContext* cx, Handle<BaseShape*> base, |
| 1157 |
ObjectFlags objectFlags) { |
| 1158 |
return cx->newCell<ProxyShape>(base, objectFlags); |
| 1159 |
} |
| 1160 |
|
| 1161 |
// static |
| 1162 |
WasmGCShape* WasmGCShape::new_(JSContext* cx, Handle<BaseShape*> base, |
| 1163 |
const wasm::RecGroup* recGroup, |
| 1164 |
ObjectFlags objectFlags) { |
| 1165 |
WasmGCShape* shape = cx->newCell<WasmGCShape>(base, recGroup, objectFlags); |
| 1166 |
if (shape) { |
| 1167 |
shape->init(); |
| 1168 |
} |
| 1169 |
return shape; |
| 1170 |
} |
| 1171 |
|
| 1172 |
MOZ_ALWAYS_INLINE HashNumber ShapeForAddHasher::hash(const Lookup& l) { |
| 1173 |
HashNumber hash = HashPropertyKey(l.key); |
| 1174 |
return mozilla::AddToHash(hash, l.flags.toRaw()); |
| 1175 |
} |
| 1176 |
|
| 1177 |
MOZ_ALWAYS_INLINE bool ShapeForAddHasher::match(SharedShape* shape, |
| 1178 |
const Lookup& l) { |
| 1179 |
uint32_t slot; |
| 1180 |
return shape->lastPropertyMatchesForAdd(l.key, l.flags, &slot); |
| 1181 |
} |
| 1182 |
|
| 1183 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1184 |
void BaseShape::dump() const { |
| 1185 |
Fprinter out(stderr); |
| 1186 |
dump(out); |
| 1187 |
} |
| 1188 |
|
| 1189 |
void BaseShape::dump(js::GenericPrinter& out) const { |
| 1190 |
js::JSONPrinter json(out); |
| 1191 |
dump(json); |
| 1192 |
out.put("\n"); |
| 1193 |
} |
| 1194 |
|
| 1195 |
void BaseShape::dump(js::JSONPrinter& json) const { |
| 1196 |
json.beginObject(); |
| 1197 |
dumpFields(json); |
| 1198 |
json.endObject(); |
| 1199 |
} |
| 1200 |
|
| 1201 |
void BaseShape::dumpFields(js::JSONPrinter& json) const { |
| 1202 |
json.formatProperty("address", "(js::BaseShape*)0x%p", this); |
| 1203 |
|
| 1204 |
json.formatProperty("realm", "(JS::Realm*)0x%p", realm()); |
| 1205 |
|
| 1206 |
if (proto().isDynamic()) { |
| 1207 |
json.property("proto", "<dynamic>"); |
| 1208 |
} else { |
| 1209 |
JSObject* protoObj = proto().toObjectOrNull(); |
| 1210 |
if (protoObj) { |
| 1211 |
json.formatProperty("proto", "(JSObject*)0x%p", protoObj); |
| 1212 |
} else { |
| 1213 |
json.nullProperty("proto"); |
| 1214 |
} |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
void Shape::dump() const { |
| 1219 |
Fprinter out(stderr); |
| 1220 |
dump(out); |
| 1221 |
} |
| 1222 |
|
| 1223 |
void Shape::dump(js::GenericPrinter& out) const { |
| 1224 |
js::JSONPrinter json(out); |
| 1225 |
dump(json); |
| 1226 |
out.put("\n"); |
| 1227 |
} |
| 1228 |
|
| 1229 |
void Shape::dump(js::JSONPrinter& json) const { |
| 1230 |
json.beginObject(); |
| 1231 |
dumpFields(json); |
| 1232 |
json.endObject(); |
| 1233 |
} |
| 1234 |
|
| 1235 |
template <typename KnownF, typename UnknownF> |
| 1236 |
void ForEachObjectFlag(ObjectFlags flags, KnownF known, UnknownF unknown) { |
| 1237 |
uint16_t raw = flags.toRaw(); |
| 1238 |
for (uint16_t i = 1; i; i = i << 1) { |
| 1239 |
if (!(raw & i)) { |
| 1240 |
continue; |
| 1241 |
} |
| 1242 |
switch (ObjectFlag(raw & i)) { |
| 1243 |
case ObjectFlag::IsUsedAsPrototype: |
| 1244 |
known("IsUsedAsPrototype"); |
| 1245 |
break; |
| 1246 |
case ObjectFlag::NotExtensible: |
| 1247 |
known("NotExtensible"); |
| 1248 |
break; |
| 1249 |
case ObjectFlag::Indexed: |
| 1250 |
known("Indexed"); |
| 1251 |
break; |
| 1252 |
case ObjectFlag::HasInterestingSymbol: |
| 1253 |
known("HasInterestingSymbol"); |
| 1254 |
break; |
| 1255 |
case ObjectFlag::HasEnumerable: |
| 1256 |
known("HasEnumerable"); |
| 1257 |
break; |
| 1258 |
case ObjectFlag::FrozenElements: |
| 1259 |
known("FrozenElements"); |
| 1260 |
break; |
| 1261 |
case ObjectFlag::InvalidatedTeleporting: |
| 1262 |
known("InvalidatedTeleporting"); |
| 1263 |
break; |
| 1264 |
case ObjectFlag::ImmutablePrototype: |
| 1265 |
known("ImmutablePrototype"); |
| 1266 |
break; |
| 1267 |
case ObjectFlag::QualifiedVarObj: |
| 1268 |
known("QualifiedVarObj"); |
| 1269 |
break; |
| 1270 |
case ObjectFlag::HasNonWritableOrAccessorPropExclProto: |
| 1271 |
known("HasNonWritableOrAccessorPropExclProto"); |
| 1272 |
break; |
| 1273 |
case ObjectFlag::HadGetterSetterChange: |
| 1274 |
known("HadGetterSetterChange"); |
| 1275 |
break; |
| 1276 |
case ObjectFlag::UseWatchtowerTestingLog: |
| 1277 |
known("UseWatchtowerTestingLog"); |
| 1278 |
break; |
| 1279 |
case ObjectFlag::GenerationCountedGlobal: |
| 1280 |
known("GenerationCountedGlobal"); |
| 1281 |
break; |
| 1282 |
case ObjectFlag::NeedsProxyGetSetResultValidation: |
| 1283 |
known("NeedsProxyGetSetResultValidation"); |
| 1284 |
break; |
| 1285 |
case ObjectFlag::HasFuseProperty: |
| 1286 |
known("HasFuseProperty"); |
| 1287 |
break; |
| 1288 |
default: |
| 1289 |
unknown(i); |
| 1290 |
break; |
| 1291 |
} |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
void Shape::dumpFields(js::JSONPrinter& json) const { |
| 1296 |
json.formatProperty("address", "(js::Shape*)0x%p", this); |
| 1297 |
|
| 1298 |
json.beginObjectProperty("base"); |
| 1299 |
base()->dumpFields(json); |
| 1300 |
json.endObject(); |
| 1301 |
|
| 1302 |
switch (kind()) { |
| 1303 |
case Kind::Shared: |
| 1304 |
json.property("kind", "Shared"); |
| 1305 |
break; |
| 1306 |
case Kind::Dictionary: |
| 1307 |
json.property("kind", "Dictionary"); |
| 1308 |
break; |
| 1309 |
case Kind::Proxy: |
| 1310 |
json.property("kind", "Proxy"); |
| 1311 |
break; |
| 1312 |
case Kind::WasmGC: |
| 1313 |
json.property("kind", "WasmGC"); |
| 1314 |
break; |
| 1315 |
} |
| 1316 |
|
| 1317 |
json.beginInlineListProperty("objectFlags"); |
| 1318 |
ForEachObjectFlag( |
| 1319 |
objectFlags(), [&](const char* name) { json.value("%s", name); }, |
| 1320 |
[&](uint16_t value) { json.value("Unknown(%04x)", value); }); |
| 1321 |
json.endInlineList(); |
| 1322 |
|
| 1323 |
if (isNative()) { |
| 1324 |
json.property("numFixedSlots", asNative().numFixedSlots()); |
| 1325 |
json.property("propMapLength", asNative().propMapLength()); |
| 1326 |
|
| 1327 |
if (asNative().propMap()) { |
| 1328 |
json.beginObjectProperty("propMap"); |
| 1329 |
asNative().propMap()->dumpFields(json); |
| 1330 |
json.endObject(); |
| 1331 |
} else { |
| 1332 |
json.nullProperty("propMap"); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (isShared()) { |
| 1337 |
if (getObjectClass()->isNativeObject()) { |
| 1338 |
json.property("slotSpan", asShared().slotSpan()); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
if (isWasmGC()) { |
| 1343 |
json.formatProperty("recGroup", "(js::wasm::RecGroup*)0x%p", |
| 1344 |
asWasmGC().recGroup()); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
void Shape::dumpStringContent(js::GenericPrinter& out) const { |
| 1349 |
out.printf("<(js::Shape*)0x%p", this); |
| 1350 |
|
| 1351 |
if (isDictionary()) { |
| 1352 |
out.put(", dictionary"); |
| 1353 |
} |
| 1354 |
|
| 1355 |
out.put(", objectFlags=["); |
| 1356 |
bool first = true; |
| 1357 |
ForEachObjectFlag( |
| 1358 |
objectFlags(), |
| 1359 |
[&](const char* name) { |
| 1360 |
if (!first) { |
| 1361 |
out.put(", "); |
| 1362 |
} |
| 1363 |
first = false; |
| 1364 |
|
| 1365 |
out.put(name); |
| 1366 |
}, |
| 1367 |
[&](uint16_t value) { |
| 1368 |
if (!first) { |
| 1369 |
out.put(", "); |
| 1370 |
} |
| 1371 |
first = false; |
| 1372 |
|
| 1373 |
out.printf("Unknown(%04x)", value); |
| 1374 |
}); |
| 1375 |
out.put("]>"); |
| 1376 |
} |
| 1377 |
#endif // defined(DEBUG) || defined(JS_JITSPEW) |
| 1378 |
|
| 1379 |
/* static */ |
| 1380 |
SharedShape* SharedShape::getInitialShape(JSContext* cx, const JSClass* clasp, |
| 1381 |
JS::Realm* realm, TaggedProto proto, |
| 1382 |
size_t nfixed, |
| 1383 |
ObjectFlags objectFlags) { |
| 1384 |
MOZ_ASSERT(cx->compartment() == realm->compartment()); |
| 1385 |
MOZ_ASSERT_IF(proto.isObject(), |
| 1386 |
cx->isInsideCurrentCompartment(proto.toObject())); |
| 1387 |
|
| 1388 |
if (proto.isObject()) { |
| 1389 |
if (proto.toObject()->isUsedAsPrototype()) { |
| 1390 |
// Use the cache on the prototype's shape to get to the initial shape. |
| 1391 |
// This cache has a hit rate of 80-90% on typical workloads and is faster |
| 1392 |
// than the HashSet lookup below. |
| 1393 |
JSObject* protoObj = proto.toObject(); |
| 1394 |
Shape* protoObjShape = protoObj->shape(); |
| 1395 |
if (protoObjShape->cache().isShapeWithProto()) { |
| 1396 |
SharedShape* shape = protoObjShape->cache().toShapeWithProto(); |
| 1397 |
if (shape->numFixedSlots() == nfixed && |
| 1398 |
shape->objectFlags() == objectFlags && |
| 1399 |
shape->getObjectClass() == clasp && shape->realm() == realm && |
| 1400 |
shape->proto() == proto) { |
| 1401 |
#ifdef DEBUG |
| 1402 |
// Verify the table lookup below would have resulted in the same |
| 1403 |
// shape. |
| 1404 |
using Lookup = InitialShapeHasher::Lookup; |
| 1405 |
Lookup lookup(clasp, realm, proto, nfixed, objectFlags); |
| 1406 |
auto p = realm->zone()->shapeZone().initialShapes.lookup(lookup); |
| 1407 |
MOZ_ASSERT(*p == shape); |
| 1408 |
#endif |
| 1409 |
return shape; |
| 1410 |
} |
| 1411 |
} |
| 1412 |
} else { |
| 1413 |
RootedObject protoObj(cx, proto.toObject()); |
| 1414 |
if (!SetObjectIsUsedAsPrototype(cx, protoObj)) { |
| 1415 |
return nullptr; |
| 1416 |
} |
| 1417 |
proto = TaggedProto(protoObj); |
| 1418 |
} |
| 1419 |
} |
| 1420 |
|
| 1421 |
auto& table = realm->zone()->shapeZone().initialShapes; |
| 1422 |
|
| 1423 |
using Lookup = InitialShapeHasher::Lookup; |
| 1424 |
auto ptr = MakeDependentAddPtr( |
| 1425 |
cx, table, Lookup(clasp, realm, proto, nfixed, objectFlags)); |
| 1426 |
if (ptr) { |
| 1427 |
// Cache the result of this lookup on the prototype's shape. |
| 1428 |
if (proto.isObject()) { |
| 1429 |
JSObject* protoObj = proto.toObject(); |
| 1430 |
Shape* protoShape = protoObj->shape(); |
| 1431 |
if (!protoShape->cache().isForAdd() && |
| 1432 |
RegisterShapeCache(cx, protoShape)) { |
| 1433 |
protoShape->cacheRef().setShapeWithProto(*ptr); |
| 1434 |
} |
| 1435 |
} |
| 1436 |
return *ptr; |
| 1437 |
} |
| 1438 |
|
| 1439 |
Rooted<TaggedProto> protoRoot(cx, proto); |
| 1440 |
Rooted<BaseShape*> nbase(cx, BaseShape::get(cx, clasp, realm, protoRoot)); |
| 1441 |
if (!nbase) { |
| 1442 |
return nullptr; |
| 1443 |
} |
| 1444 |
|
| 1445 |
Rooted<SharedShape*> shape( |
| 1446 |
cx, SharedShape::new_(cx, nbase, objectFlags, nfixed, nullptr, 0)); |
| 1447 |
if (!shape) { |
| 1448 |
return nullptr; |
| 1449 |
} |
| 1450 |
|
| 1451 |
Lookup lookup(clasp, realm, protoRoot, nfixed, objectFlags); |
| 1452 |
if (!ptr.add(cx, table, lookup, shape)) { |
| 1453 |
return nullptr; |
| 1454 |
} |
| 1455 |
|
| 1456 |
return shape; |
| 1457 |
} |
| 1458 |
|
| 1459 |
/* static */ |
| 1460 |
SharedShape* SharedShape::getInitialShape(JSContext* cx, const JSClass* clasp, |
| 1461 |
JS::Realm* realm, TaggedProto proto, |
| 1462 |
gc::AllocKind kind, |
| 1463 |
ObjectFlags objectFlags) { |
| 1464 |
return getInitialShape(cx, clasp, realm, proto, GetGCKindSlots(kind), |
| 1465 |
objectFlags); |
| 1466 |
} |
| 1467 |
|
| 1468 |
/* static */ |
| 1469 |
SharedShape* SharedShape::getPropMapShape( |
| 1470 |
JSContext* cx, BaseShape* base, size_t nfixed, Handle<SharedPropMap*> map, |
| 1471 |
uint32_t mapLength, ObjectFlags objectFlags, bool* allocatedNewShape) { |
| 1472 |
MOZ_ASSERT(cx->compartment() == base->compartment()); |
| 1473 |
MOZ_ASSERT_IF(base->proto().isObject(), |
| 1474 |
cx->isInsideCurrentCompartment(base->proto().toObject())); |
| 1475 |
MOZ_ASSERT_IF(base->proto().isObject(), |
| 1476 |
base->proto().toObject()->isUsedAsPrototype()); |
| 1477 |
MOZ_ASSERT(map); |
| 1478 |
MOZ_ASSERT(mapLength > 0); |
| 1479 |
|
| 1480 |
auto& table = cx->zone()->shapeZone().propMapShapes; |
| 1481 |
|
| 1482 |
using Lookup = PropMapShapeHasher::Lookup; |
| 1483 |
auto ptr = MakeDependentAddPtr( |
| 1484 |
cx, table, Lookup(base, nfixed, map, mapLength, objectFlags)); |
| 1485 |
if (ptr) { |
| 1486 |
if (allocatedNewShape) { |
| 1487 |
*allocatedNewShape = false; |
| 1488 |
} |
| 1489 |
return *ptr; |
| 1490 |
} |
| 1491 |
|
| 1492 |
Rooted<BaseShape*> baseRoot(cx, base); |
| 1493 |
Rooted<SharedShape*> shape( |
| 1494 |
cx, SharedShape::new_(cx, baseRoot, objectFlags, nfixed, map, mapLength)); |
| 1495 |
if (!shape) { |
| 1496 |
return nullptr; |
| 1497 |
} |
| 1498 |
|
| 1499 |
Lookup lookup(baseRoot, nfixed, map, mapLength, objectFlags); |
| 1500 |
if (!ptr.add(cx, table, lookup, shape)) { |
| 1501 |
return nullptr; |
| 1502 |
} |
| 1503 |
|
| 1504 |
if (allocatedNewShape) { |
| 1505 |
*allocatedNewShape = true; |
| 1506 |
} |
| 1507 |
|
| 1508 |
return shape; |
| 1509 |
} |
| 1510 |
|
| 1511 |
/* static */ |
| 1512 |
SharedShape* SharedShape::getInitialOrPropMapShape( |
| 1513 |
JSContext* cx, const JSClass* clasp, JS::Realm* realm, TaggedProto proto, |
| 1514 |
size_t nfixed, Handle<SharedPropMap*> map, uint32_t mapLength, |
| 1515 |
ObjectFlags objectFlags) { |
| 1516 |
if (!map) { |
| 1517 |
MOZ_ASSERT(mapLength == 0); |
| 1518 |
return getInitialShape(cx, clasp, realm, proto, nfixed, objectFlags); |
| 1519 |
} |
| 1520 |
|
| 1521 |
Rooted<TaggedProto> protoRoot(cx, proto); |
| 1522 |
BaseShape* nbase = BaseShape::get(cx, clasp, realm, protoRoot); |
| 1523 |
if (!nbase) { |
| 1524 |
return nullptr; |
| 1525 |
} |
| 1526 |
|
| 1527 |
return getPropMapShape(cx, nbase, nfixed, map, mapLength, objectFlags); |
| 1528 |
} |
| 1529 |
|
| 1530 |
/* static */ |
| 1531 |
void SharedShape::insertInitialShape(JSContext* cx, |
| 1532 |
Handle<SharedShape*> shape) { |
| 1533 |
using Lookup = InitialShapeHasher::Lookup; |
| 1534 |
Lookup lookup(shape->getObjectClass(), shape->realm(), shape->proto(), |
| 1535 |
shape->numFixedSlots(), shape->objectFlags()); |
| 1536 |
|
| 1537 |
auto& table = cx->zone()->shapeZone().initialShapes; |
| 1538 |
InitialShapeSet::Ptr p = table.lookup(lookup); |
| 1539 |
MOZ_ASSERT(p); |
| 1540 |
|
| 1541 |
// The metadata callback can end up causing redundant changes of the initial |
| 1542 |
// shape. |
| 1543 |
SharedShape* initialShape = *p; |
| 1544 |
if (initialShape == shape) { |
| 1545 |
return; |
| 1546 |
} |
| 1547 |
|
| 1548 |
MOZ_ASSERT(initialShape->numFixedSlots() == shape->numFixedSlots()); |
| 1549 |
MOZ_ASSERT(initialShape->base() == shape->base()); |
| 1550 |
MOZ_ASSERT(initialShape->objectFlags() == shape->objectFlags()); |
| 1551 |
|
| 1552 |
table.replaceKey(p, lookup, shape.get()); |
| 1553 |
|
| 1554 |
// Purge the prototype's shape cache entry. |
| 1555 |
if (shape->proto().isObject()) { |
| 1556 |
JSObject* protoObj = shape->proto().toObject(); |
| 1557 |
if (protoObj->shape()->cache().isShapeWithProto()) { |
| 1558 |
protoObj->shape()->cacheRef().setNone(); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
} |
| 1562 |
|
| 1563 |
/* static */ |
| 1564 |
ProxyShape* ProxyShape::getShape(JSContext* cx, const JSClass* clasp, |
| 1565 |
JS::Realm* realm, TaggedProto proto, |
| 1566 |
ObjectFlags objectFlags) { |
| 1567 |
MOZ_ASSERT(cx->compartment() == realm->compartment()); |
| 1568 |
MOZ_ASSERT_IF(proto.isObject(), |
| 1569 |
cx->isInsideCurrentCompartment(proto.toObject())); |
| 1570 |
|
| 1571 |
if (proto.isObject() && !proto.toObject()->isUsedAsPrototype()) { |
| 1572 |
RootedObject protoObj(cx, proto.toObject()); |
| 1573 |
if (!SetObjectIsUsedAsPrototype(cx, protoObj)) { |
| 1574 |
return nullptr; |
| 1575 |
} |
| 1576 |
proto = TaggedProto(protoObj); |
| 1577 |
} |
| 1578 |
|
| 1579 |
auto& table = realm->zone()->shapeZone().proxyShapes; |
| 1580 |
|
| 1581 |
using Lookup = ProxyShapeHasher::Lookup; |
| 1582 |
auto ptr = |
| 1583 |
MakeDependentAddPtr(cx, table, Lookup(clasp, realm, proto, objectFlags)); |
| 1584 |
if (ptr) { |
| 1585 |
return *ptr; |
| 1586 |
} |
| 1587 |
|
| 1588 |
Rooted<TaggedProto> protoRoot(cx, proto); |
| 1589 |
Rooted<BaseShape*> nbase(cx, BaseShape::get(cx, clasp, realm, protoRoot)); |
| 1590 |
if (!nbase) { |
| 1591 |
return nullptr; |
| 1592 |
} |
| 1593 |
|
| 1594 |
Rooted<ProxyShape*> shape(cx, ProxyShape::new_(cx, nbase, objectFlags)); |
| 1595 |
if (!shape) { |
| 1596 |
return nullptr; |
| 1597 |
} |
| 1598 |
|
| 1599 |
Lookup lookup(clasp, realm, protoRoot, objectFlags); |
| 1600 |
if (!ptr.add(cx, table, lookup, shape)) { |
| 1601 |
return nullptr; |
| 1602 |
} |
| 1603 |
|
| 1604 |
return shape; |
| 1605 |
} |
| 1606 |
|
| 1607 |
/* static */ |
| 1608 |
WasmGCShape* WasmGCShape::getShape(JSContext* cx, const JSClass* clasp, |
| 1609 |
JS::Realm* realm, TaggedProto proto, |
| 1610 |
const wasm::RecGroup* recGroup, |
| 1611 |
ObjectFlags objectFlags) { |
| 1612 |
MOZ_ASSERT(cx->compartment() == realm->compartment()); |
| 1613 |
MOZ_ASSERT_IF(proto.isObject(), |
| 1614 |
cx->isInsideCurrentCompartment(proto.toObject())); |
| 1615 |
|
| 1616 |
if (proto.isObject() && !proto.toObject()->isUsedAsPrototype()) { |
| 1617 |
RootedObject protoObj(cx, proto.toObject()); |
| 1618 |
if (!SetObjectIsUsedAsPrototype(cx, protoObj)) { |
| 1619 |
return nullptr; |
| 1620 |
} |
| 1621 |
proto = TaggedProto(protoObj); |
| 1622 |
} |
| 1623 |
|
| 1624 |
auto& table = realm->zone()->shapeZone().wasmGCShapes; |
| 1625 |
|
| 1626 |
using Lookup = WasmGCShapeHasher::Lookup; |
| 1627 |
auto ptr = MakeDependentAddPtr( |
| 1628 |
cx, table, Lookup(clasp, realm, proto, recGroup, objectFlags)); |
| 1629 |
if (ptr) { |
| 1630 |
return *ptr; |
| 1631 |
} |
| 1632 |
|
| 1633 |
Rooted<TaggedProto> protoRoot(cx, proto); |
| 1634 |
Rooted<BaseShape*> nbase(cx, BaseShape::get(cx, clasp, realm, protoRoot)); |
| 1635 |
if (!nbase) { |
| 1636 |
return nullptr; |
| 1637 |
} |
| 1638 |
|
| 1639 |
Rooted<WasmGCShape*> shape( |
| 1640 |
cx, WasmGCShape::new_(cx, nbase, recGroup, objectFlags)); |
| 1641 |
if (!shape) { |
| 1642 |
return nullptr; |
| 1643 |
} |
| 1644 |
|
| 1645 |
Lookup lookup(clasp, realm, protoRoot, recGroup, objectFlags); |
| 1646 |
if (!ptr.add(cx, table, lookup, shape)) { |
| 1647 |
return nullptr; |
| 1648 |
} |
| 1649 |
|
| 1650 |
return shape; |
| 1651 |
} |
| 1652 |
|
| 1653 |
JS::ubi::Node::Size JS::ubi::Concrete<js::Shape>::size( |
| 1654 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 1655 |
Size size = js::gc::Arena::thingSize(get().asTenured().getAllocKind()); |
| 1656 |
|
| 1657 |
if (get().cache().isShapeSetForAdd()) { |
| 1658 |
ShapeSetForAdd* set = get().cache().toShapeSetForAdd(); |
| 1659 |
size += set->shallowSizeOfIncludingThis(mallocSizeOf); |
| 1660 |
} |
| 1661 |
|
| 1662 |
return size; |
| 1663 |
} |
| 1664 |
|
| 1665 |
JS::ubi::Node::Size JS::ubi::Concrete<js::BaseShape>::size( |
| 1666 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 1667 |
return js::gc::Arena::thingSize(get().asTenured().getAllocKind()); |
| 1668 |
} |