| 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 |
#ifndef vm_PropMap_h |
| 8 |
#define vm_PropMap_h |
| 9 |
|
| 10 |
#include "gc/Barrier.h" |
| 11 |
#include "gc/Cell.h" |
| 12 |
#include "js/TypeDecls.h" |
| 13 |
#include "js/UbiNode.h" |
| 14 |
#include "js/Utility.h" // JS::UniqueChars |
| 15 |
#include "vm/ObjectFlags.h" |
| 16 |
#include "vm/PropertyInfo.h" |
| 17 |
#include "vm/PropertyKey.h" |
| 18 |
|
| 19 |
// [SMDOC] Property Maps |
| 20 |
// |
| 21 |
// Property maps are used to store information about native object properties. |
| 22 |
// Each property map represents an ordered list of (PropertyKey, PropertyInfo) |
| 23 |
// tuples. |
| 24 |
// |
| 25 |
// Each property map can store up to 8 properties (see PropMap::Capacity). To |
| 26 |
// store more than eight properties, multiple maps must be linked together with |
| 27 |
// the |previous| pointer. |
| 28 |
// |
| 29 |
// Shapes and Property Maps |
| 30 |
// ------------------------ |
| 31 |
// Native object shapes represent property information as a (PropMap*, length) |
| 32 |
// tuple. When there are no properties yet, the shape's map will be nullptr and |
| 33 |
// the length is zero. |
| 34 |
// |
| 35 |
// For example, consider the following objects: |
| 36 |
// |
| 37 |
// o1 = {x: 1, y: 2} |
| 38 |
// o2 = {x: 3, y: 4, z: 5} |
| 39 |
// |
| 40 |
// This is stored as follows: |
| 41 |
// |
| 42 |
// +-------------+ +--------------+ +-------------------+ |
| 43 |
// | JSObject o1 | | Shape S1 | | PropMap M1 | |
| 44 |
// |-------------+ +--------------+ +-------------------+ |
| 45 |
// | shape: S1 -+---> | map: M1 -+--+> | key 0: x (slot 0) | |
| 46 |
// | slot 0: 1 | | mapLength: 2 | | | key 1: y (slot 1) | |
| 47 |
// | slot 1: 2 | +--------------+ | | key 2: z (slot 2) | |
| 48 |
// +-------------+ | | ... | |
| 49 |
// | +-------------------+ |
| 50 |
// | |
| 51 |
// +-------------+ +--------------+ | |
| 52 |
// | JSObject o2 | | Shape S2 | | |
| 53 |
// |-------------+ +--------------+ | |
| 54 |
// | shape: S2 -+---> | map: M1 -+--+ |
| 55 |
// | slot 0: 3 | | mapLength: 3 | |
| 56 |
// | slot 1: 4 | +--------------+ |
| 57 |
// | slot 2: 5 | |
| 58 |
// +-------------+ |
| 59 |
// |
| 60 |
// There's a single map M1 shared by shapes S1 and S2. Shape S1 includes only |
| 61 |
// the first two properties and shape S2 includes all three properties. |
| 62 |
// |
| 63 |
// Class Hierarchy |
| 64 |
// --------------- |
| 65 |
// Property maps have the following C++ class hierarchy: |
| 66 |
// |
| 67 |
// PropMap (abstract) |
| 68 |
// | |
| 69 |
// +-- SharedPropMap (abstract) |
| 70 |
// | | |
| 71 |
// | +-- CompactPropMap |
| 72 |
// | | |
| 73 |
// | +-- NormalPropMap |
| 74 |
// | |
| 75 |
// +-- DictionaryPropMap |
| 76 |
// |
| 77 |
// * PropMap: base class. It has a flags word and an array of PropertyKeys. |
| 78 |
// |
| 79 |
// * SharedPropMap: base class for all shared property maps. See below for more |
| 80 |
// information on shared maps. |
| 81 |
// |
| 82 |
// * CompactPropMap: a shared map that stores its information more compactly |
| 83 |
// than the other maps. It saves four words by not storing a |
| 84 |
// PropMapTable, previous pointer, and by using a more compact |
| 85 |
// PropertyInfo type for slot numbers that fit in one byte. |
| 86 |
// |
| 87 |
// * NormalPropMap: a shared map, used when CompactPropMap can't be used. |
| 88 |
// |
| 89 |
// * DictionaryPropMap: an unshared map (used by a single object/shape). See |
| 90 |
// below for more information on dictionary maps. |
| 91 |
// |
| 92 |
// Secondary hierarchy |
| 93 |
// ------------------- |
| 94 |
// NormalPropMap and DictionaryPropMap store property information in the same |
| 95 |
// way. This means property lookups don't have to distinguish between these two |
| 96 |
// types. This is represented with a second class hierarchy: |
| 97 |
// |
| 98 |
// PropMap (abstract) |
| 99 |
// | |
| 100 |
// +-- CompactPropMap |
| 101 |
// | |
| 102 |
// +-- LinkedPropMap (NormalPropMap or DictionaryPropMap) |
| 103 |
// |
| 104 |
// Property lookup and property iteration are very performance sensitive and use |
| 105 |
// this Compact vs Linked "view" so that they don't have to handle the three map |
| 106 |
// types separately. |
| 107 |
// |
| 108 |
// LinkedPropMap also stores the PropMapTable and a pointer to the |previous| |
| 109 |
// map. Compact maps don't have these fields. |
| 110 |
// |
| 111 |
// To summarize these map types: |
| 112 |
// |
| 113 |
// +-------------------+-------------+--------+ |
| 114 |
// | Concrete type | Shared/tree | Linked | |
| 115 |
// +-------------------+-------------+--------+ |
| 116 |
// | CompactPropMap | yes | no | |
| 117 |
// | NormalPropMap | yes | yes | |
| 118 |
// | DictionaryPropMap | no | yes | |
| 119 |
// +-------------------+-------------+--------+ |
| 120 |
// |
| 121 |
// PropMapTable |
| 122 |
// ------------ |
| 123 |
// Finding the PropertyInfo for a particular PropertyKey requires a linear |
| 124 |
// search if the map is small. For larger maps we can create a PropMapTable, a |
| 125 |
// hash table that maps from PropertyKey to PropMap + index, to speed up |
| 126 |
// property lookups. |
| 127 |
// |
| 128 |
// To save memory, property map tables can be discarded on GC and recreated when |
| 129 |
// needed. AutoKeepPropMapTables can be used to avoid discarding tables in a |
| 130 |
// particular zone. Methods to access a PropMapTable take either an |
| 131 |
// AutoCheckCannotGC or AutoKeepPropMapTables argument, to help ensure tables |
| 132 |
// are not purged while we're using them. |
| 133 |
// |
| 134 |
// Shared Property Maps |
| 135 |
// -------------------- |
| 136 |
// Shared property maps can be shared per-Zone by objects with the same property |
| 137 |
// keys, flags, and slot numbers. To make this work, shared maps form a tree: |
| 138 |
// |
| 139 |
// - Each Zone has a table that maps from first PropertyKey + PropertyInfo to |
| 140 |
// a SharedPropMap that begins with that property. This is used to lookup the |
| 141 |
// the map to use when adding the first property. |
| 142 |
// See ShapeZone::initialPropMaps. |
| 143 |
// |
| 144 |
// - When adding a property other than the first one, the property is stored in |
| 145 |
// the next entry of the same map when possible. If the map is full or the |
| 146 |
// next entry already stores a different property, a child map is created and |
| 147 |
// linked to the parent map. |
| 148 |
// |
| 149 |
// For example, imagine we want to create these objects: |
| 150 |
// |
| 151 |
// o1 = {x: 1, y: 2, z: 3} |
| 152 |
// o2 = {x: 1, y: 2, foo: 4} |
| 153 |
// |
| 154 |
// This will result in the following maps being created: |
| 155 |
// |
| 156 |
// +---------------------+ +---------------------+ |
| 157 |
// | SharedPropMap M1 | | SharedPropMap M2 | |
| 158 |
// +---------------------+ +---------------------+ |
| 159 |
// | Child M2 (index 1) -+--> | Parent M1 (index 1) | |
| 160 |
// +---------------------+ +---------------------+ |
| 161 |
// | 0: x | | 0: x | |
| 162 |
// | 1: y | | 1: y | |
| 163 |
// | 2: z | | 2: foo | |
| 164 |
// | ... | | ... | |
| 165 |
// +---------------------+ +---------------------+ |
| 166 |
// |
| 167 |
// M1 is the map used for initial property "x". Properties "y" and "z" can be |
| 168 |
// stored inline. When later adding "foo" following "y", the map has to be |
| 169 |
// forked: a child map M2 is created and M1 remembers this transition at |
| 170 |
// property index 1 so that M2 will be used the next time properties "x", "y", |
| 171 |
// and "foo" are added to an object. |
| 172 |
// |
| 173 |
// Shared maps contain a TreeData struct that stores the parent and children |
| 174 |
// links for the SharedPropMap tree. The parent link is a tagged pointer that |
| 175 |
// stores both the parent map and the property index of the last used property |
| 176 |
// in the parent map before the branch. The children are stored similarly: the |
| 177 |
// parent map can store a single child map and index, or a set of children. |
| 178 |
// See SharedChildrenPtr. |
| 179 |
// |
| 180 |
// Looking up a child map can then be done based on the index of the last |
| 181 |
// property in the parent map and the new property's key and flags. So for the |
| 182 |
// example above, the lookup key for M1 => M2 is (index 1, "foo", <flags>). |
| 183 |
// |
| 184 |
// Note: shared maps can have both a |previous| map and a |parent| map. They are |
| 185 |
// equal when the previous map was full, but can be different maps when |
| 186 |
// branching in the middle of a map like in the example above: M2 has parent M1 |
| 187 |
// but does not have a |previous| map (because it only has three properties). |
| 188 |
// |
| 189 |
// Dictionary Property Maps |
| 190 |
// ------------------------ |
| 191 |
// Certain operations can't be implemented (efficiently) for shared property |
| 192 |
// maps, for example changing or deleting a property other than the last one. |
| 193 |
// When this happens the map is copied as a DictionaryPropMap. |
| 194 |
// |
| 195 |
// Dictionary maps are unshared so can be mutated in place (after generating a |
| 196 |
// new shape for the object). |
| 197 |
// |
| 198 |
// Unlike shared maps, dictionary maps can have holes between two property keys |
| 199 |
// after removing a property. When there are more holes than properties, the |
| 200 |
// map is compacted. See DictionaryPropMap::maybeCompact. |
| 201 |
|
| 202 |
namespace js { |
| 203 |
|
| 204 |
enum class IntegrityLevel; |
| 205 |
|
| 206 |
class DictionaryPropMap; |
| 207 |
class SharedPropMap; |
| 208 |
class LinkedPropMap; |
| 209 |
class CompactPropMap; |
| 210 |
class NormalPropMap; |
| 211 |
|
| 212 |
class JS_PUBLIC_API GenericPrinter; |
| 213 |
class JSONPrinter; |
| 214 |
|
| 215 |
// Template class for storing a PropMap* and a property index as tagged pointer. |
| 216 |
template <typename T> |
| 217 |
class MapAndIndex { |
| 218 |
uintptr_t data_ = 0; |
| 219 |
|
| 220 |
static constexpr uintptr_t IndexMask = 0b111; |
| 221 |
|
| 222 |
public: |
| 223 |
MapAndIndex() = default; |
| 224 |
|
| 225 |
MapAndIndex(const T* map, uint32_t index) : data_(uintptr_t(map) | index) { |
| 226 |
MOZ_ASSERT((uintptr_t(map) & IndexMask) == 0); |
| 227 |
MOZ_ASSERT(index <= IndexMask); |
| 228 |
} |
| 229 |
explicit MapAndIndex(uintptr_t data) : data_(data) {} |
| 230 |
|
| 231 |
void setNone() { data_ = 0; } |
| 232 |
|
| 233 |
bool isNone() const { return data_ == 0; } |
| 234 |
|
| 235 |
uintptr_t raw() const { return data_; } |
| 236 |
T* maybeMap() const { return reinterpret_cast<T*>(data_ & ~IndexMask); } |
| 237 |
|
| 238 |
uint32_t index() const { |
| 239 |
MOZ_ASSERT(!isNone()); |
| 240 |
return data_ & IndexMask; |
| 241 |
} |
| 242 |
T* map() const { |
| 243 |
MOZ_ASSERT(!isNone()); |
| 244 |
return maybeMap(); |
| 245 |
} |
| 246 |
|
| 247 |
inline PropertyInfo propertyInfo() const; |
| 248 |
|
| 249 |
bool operator==(const MapAndIndex<T>& other) const { |
| 250 |
return data_ == other.data_; |
| 251 |
} |
| 252 |
bool operator!=(const MapAndIndex<T>& other) const { |
| 253 |
return !operator==(other); |
| 254 |
} |
| 255 |
} JS_HAZ_GC_POINTER; |
| 256 |
using PropMapAndIndex = MapAndIndex<PropMap>; |
| 257 |
using SharedPropMapAndIndex = MapAndIndex<SharedPropMap>; |
| 258 |
|
| 259 |
struct SharedChildrenHasher; |
| 260 |
using SharedChildrenSet = |
| 261 |
HashSet<SharedPropMapAndIndex, SharedChildrenHasher, SystemAllocPolicy>; |
| 262 |
|
| 263 |
// Children of shared maps. This is either: |
| 264 |
// |
| 265 |
// - None (no children) |
| 266 |
// - SingleMapAndIndex (one child map, including the property index of the last |
| 267 |
// property before the branch) |
| 268 |
// - SharedChildrenSet (multiple children) |
| 269 |
// |
| 270 |
// Because SingleMapAndIndex use all bits, this relies on the HasChildrenSet |
| 271 |
// flag in the map to distinguish the latter two cases. |
| 272 |
class SharedChildrenPtr { |
| 273 |
uintptr_t data_ = 0; |
| 274 |
|
| 275 |
public: |
| 276 |
bool isNone() const { return data_ == 0; } |
| 277 |
void setNone() { data_ = 0; } |
| 278 |
|
| 279 |
void setSingleChild(SharedPropMapAndIndex child) { data_ = child.raw(); } |
| 280 |
void setChildrenSet(SharedChildrenSet* set) { data_ = uintptr_t(set); } |
| 281 |
|
| 282 |
SharedPropMapAndIndex toSingleChild() const { |
| 283 |
MOZ_ASSERT(!isNone()); |
| 284 |
return SharedPropMapAndIndex(data_); |
| 285 |
} |
| 286 |
|
| 287 |
SharedChildrenSet* toChildrenSet() const { |
| 288 |
MOZ_ASSERT(!isNone()); |
| 289 |
return reinterpret_cast<SharedChildrenSet*>(data_); |
| 290 |
} |
| 291 |
} JS_HAZ_GC_POINTER; |
| 292 |
|
| 293 |
// Ensures no property map tables are purged in the current zone. |
| 294 |
class MOZ_RAII AutoKeepPropMapTables { |
| 295 |
JSContext* cx_; |
| 296 |
bool prev_; |
| 297 |
|
| 298 |
public: |
| 299 |
void operator=(const AutoKeepPropMapTables&) = delete; |
| 300 |
AutoKeepPropMapTables(const AutoKeepPropMapTables&) = delete; |
| 301 |
explicit inline AutoKeepPropMapTables(JSContext* cx); |
| 302 |
inline ~AutoKeepPropMapTables(); |
| 303 |
}; |
| 304 |
|
| 305 |
// Hash table to optimize property lookups on larger maps. This maps from |
| 306 |
// PropertyKey to PropMapAndIndex. |
| 307 |
class PropMapTable { |
| 308 |
struct Hasher { |
| 309 |
using Key = PropMapAndIndex; |
| 310 |
using Lookup = PropertyKey; |
| 311 |
static MOZ_ALWAYS_INLINE HashNumber hash(PropertyKey key); |
| 312 |
static MOZ_ALWAYS_INLINE bool match(PropMapAndIndex, PropertyKey key); |
| 313 |
}; |
| 314 |
|
| 315 |
// Small lookup cache. This has a hit rate of 30-60% on most workloads and is |
| 316 |
// a lot faster than the full HashSet lookup. |
| 317 |
struct CacheEntry { |
| 318 |
PropertyKey key; |
| 319 |
PropMapAndIndex result; |
| 320 |
}; |
| 321 |
static constexpr uint32_t NumCacheEntries = 2; |
| 322 |
CacheEntry cacheEntries_[NumCacheEntries]; |
| 323 |
|
| 324 |
using Set = HashSet<PropMapAndIndex, Hasher, SystemAllocPolicy>; |
| 325 |
Set set_; |
| 326 |
|
| 327 |
void setCacheEntry(PropertyKey key, PropMapAndIndex entry) { |
| 328 |
for (uint32_t i = 0; i < NumCacheEntries; i++) { |
| 329 |
if (cacheEntries_[i].key == key) { |
| 330 |
cacheEntries_[i].result = entry; |
| 331 |
return; |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
bool lookupInCache(PropertyKey key, PropMapAndIndex* result) const { |
| 336 |
for (uint32_t i = 0; i < NumCacheEntries; i++) { |
| 337 |
if (cacheEntries_[i].key == key) { |
| 338 |
*result = cacheEntries_[i].result; |
| 339 |
#ifdef DEBUG |
| 340 |
auto p = lookupRaw(key); |
| 341 |
MOZ_ASSERT(*result == (p ? *p : PropMapAndIndex())); |
| 342 |
#endif |
| 343 |
return true; |
| 344 |
} |
| 345 |
} |
| 346 |
return false; |
| 347 |
} |
| 348 |
void addToCache(PropertyKey key, Set::Ptr p) { |
| 349 |
for (uint32_t i = NumCacheEntries - 1; i > 0; i--) { |
| 350 |
cacheEntries_[i] = cacheEntries_[i - 1]; |
| 351 |
MOZ_ASSERT(cacheEntries_[i].key != key); |
| 352 |
} |
| 353 |
cacheEntries_[0].key = key; |
| 354 |
cacheEntries_[0].result = p ? *p : PropMapAndIndex(); |
| 355 |
} |
| 356 |
|
| 357 |
public: |
| 358 |
using Ptr = Set::Ptr; |
| 359 |
|
| 360 |
PropMapTable() = default; |
| 361 |
~PropMapTable() = default; |
| 362 |
|
| 363 |
uint32_t entryCount() const { return set_.count(); } |
| 364 |
|
| 365 |
// This counts the PropMapTable object itself (which must be heap-allocated) |
| 366 |
// and its HashSet. |
| 367 |
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const { |
| 368 |
return mallocSizeOf(this) + set_.shallowSizeOfExcludingThis(mallocSizeOf); |
| 369 |
} |
| 370 |
|
| 371 |
// init() is fallible and reports OOM to the context. |
| 372 |
bool init(JSContext* cx, LinkedPropMap* map); |
| 373 |
|
| 374 |
MOZ_ALWAYS_INLINE PropMap* lookup(PropMap* map, uint32_t mapLength, |
| 375 |
PropertyKey key, uint32_t* index); |
| 376 |
|
| 377 |
Set::Ptr lookupRaw(PropertyKey key) const { return set_.lookup(key); } |
| 378 |
#ifdef DEBUG |
| 379 |
Set::Ptr readonlyThreadsafeLookup(PropertyKey key) const { |
| 380 |
return set_.readonlyThreadsafeLookup(key); |
| 381 |
} |
| 382 |
#endif |
| 383 |
|
| 384 |
bool add(JSContext* cx, PropertyKey key, PropMapAndIndex entry) { |
| 385 |
if (!set_.putNew(key, entry)) { |
| 386 |
ReportOutOfMemory(cx); |
| 387 |
return false; |
| 388 |
} |
| 389 |
setCacheEntry(key, entry); |
| 390 |
return true; |
| 391 |
} |
| 392 |
|
| 393 |
void purgeCache() { |
| 394 |
for (uint32_t i = 0; i < NumCacheEntries; i++) { |
| 395 |
cacheEntries_[i] = CacheEntry(); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
void remove(Ptr ptr) { |
| 400 |
set_.remove(ptr); |
| 401 |
purgeCache(); |
| 402 |
} |
| 403 |
|
| 404 |
void replaceEntry(Ptr ptr, PropertyKey key, PropMapAndIndex newEntry) { |
| 405 |
MOZ_ASSERT(*ptr != newEntry); |
| 406 |
set_.replaceKey(ptr, key, newEntry); |
| 407 |
setCacheEntry(key, newEntry); |
| 408 |
} |
| 409 |
|
| 410 |
void trace(JSTracer* trc); |
| 411 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 412 |
void checkAfterMovingGC(JS::Zone* zone); |
| 413 |
#endif |
| 414 |
}; |
| 415 |
|
| 416 |
class PropMap : public gc::TenuredCellWithFlags { |
| 417 |
public: |
| 418 |
// Number of properties that can be stored in each map. This must be small |
| 419 |
// enough so that every index fits in a tagged PropMap* pointer (MapAndIndex). |
| 420 |
static constexpr size_t Capacity = 8; |
| 421 |
|
| 422 |
protected: |
| 423 |
static_assert(gc::CellFlagBitsReservedForGC == 3, |
| 424 |
"PropMap must reserve enough bits for Cell"); |
| 425 |
|
| 426 |
enum Flags { |
| 427 |
// Set if this is a CompactPropMap. |
| 428 |
IsCompactFlag = 1 << 3, |
| 429 |
|
| 430 |
// Set if this map has a non-null previous map pointer. Never set for |
| 431 |
// compact maps because they don't have a previous field. |
| 432 |
HasPrevFlag = 1 << 4, |
| 433 |
|
| 434 |
// Set if this is a DictionaryPropMap. |
| 435 |
IsDictionaryFlag = 1 << 5, |
| 436 |
|
| 437 |
// Set if this map can have a table. Never set for compact maps. Always set |
| 438 |
// for dictionary maps. |
| 439 |
CanHaveTableFlag = 1 << 6, |
| 440 |
|
| 441 |
// If set, this SharedPropMap has a SharedChildrenSet. Else it either has no |
| 442 |
// children or a single child. See SharedChildrenPtr. Never set for |
| 443 |
// dictionary maps. |
| 444 |
HasChildrenSetFlag = 1 << 7, |
| 445 |
|
| 446 |
// If set, this SharedPropMap was once converted to dictionary mode. This is |
| 447 |
// only used for heuristics. Never set for dictionary maps. |
| 448 |
HadDictionaryConversionFlag = 1 << 8, |
| 449 |
|
| 450 |
// For SharedPropMap this stores the number of previous maps, clamped to |
| 451 |
// NumPreviousMapsMax. This is used for heuristics. |
| 452 |
NumPreviousMapsMax = 0x7f, |
| 453 |
NumPreviousMapsShift = 9, |
| 454 |
NumPreviousMapsMask = NumPreviousMapsMax << NumPreviousMapsShift, |
| 455 |
}; |
| 456 |
|
| 457 |
template <typename KnownF, typename UnknownF> |
| 458 |
static void forEachPropMapFlag(uintptr_t flags, KnownF known, |
| 459 |
UnknownF unknown); |
| 460 |
|
| 461 |
// Flags word, stored in the cell header. Note that this hides the |
| 462 |
// Cell::flags() method. |
| 463 |
uintptr_t flags() const { return headerFlagsField(); } |
| 464 |
|
| 465 |
private: |
| 466 |
GCPtr<PropertyKey> keys_[Capacity]; |
| 467 |
|
| 468 |
protected: |
| 469 |
PropMap() = default; |
| 470 |
|
| 471 |
void initKey(uint32_t index, PropertyKey key) { |
| 472 |
MOZ_ASSERT(index < Capacity); |
| 473 |
keys_[index].init(key); |
| 474 |
} |
| 475 |
void setKey(uint32_t index, PropertyKey key) { |
| 476 |
MOZ_ASSERT(index < Capacity); |
| 477 |
keys_[index] = key; |
| 478 |
} |
| 479 |
|
| 480 |
public: |
| 481 |
bool isCompact() const { return flags() & IsCompactFlag; } |
| 482 |
bool isLinked() const { return !isCompact(); } |
| 483 |
bool isDictionary() const { return flags() & IsDictionaryFlag; } |
| 484 |
bool isShared() const { return !isDictionary(); } |
| 485 |
bool isNormal() const { return isShared() && !isCompact(); } |
| 486 |
|
| 487 |
bool hasPrevious() const { return flags() & HasPrevFlag; } |
| 488 |
bool canHaveTable() const { return flags() & CanHaveTableFlag; } |
| 489 |
|
| 490 |
inline CompactPropMap* asCompact(); |
| 491 |
inline const CompactPropMap* asCompact() const; |
| 492 |
|
| 493 |
inline LinkedPropMap* asLinked(); |
| 494 |
inline const LinkedPropMap* asLinked() const; |
| 495 |
|
| 496 |
inline NormalPropMap* asNormal(); |
| 497 |
inline const NormalPropMap* asNormal() const; |
| 498 |
|
| 499 |
inline SharedPropMap* asShared(); |
| 500 |
inline const SharedPropMap* asShared() const; |
| 501 |
|
| 502 |
inline DictionaryPropMap* asDictionary(); |
| 503 |
inline const DictionaryPropMap* asDictionary() const; |
| 504 |
|
| 505 |
bool hasKey(uint32_t index) const { |
| 506 |
MOZ_ASSERT(index < Capacity); |
| 507 |
return !keys_[index].isVoid(); |
| 508 |
} |
| 509 |
PropertyKey getKey(uint32_t index) const { |
| 510 |
MOZ_ASSERT(index < Capacity); |
| 511 |
return keys_[index]; |
| 512 |
} |
| 513 |
|
| 514 |
uint32_t approximateEntryCount() const; |
| 515 |
|
| 516 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 517 |
void dump() const; |
| 518 |
void dump(js::GenericPrinter& out) const; |
| 519 |
void dump(js::JSONPrinter& json) const; |
| 520 |
|
| 521 |
void dumpFields(js::JSONPrinter& json) const; |
| 522 |
void dumpFieldsAt(js::JSONPrinter& json, uint32_t index) const; |
| 523 |
void dumpDescriptorStringContentAt(js::GenericPrinter& out, |
| 524 |
uint32_t index) const; |
| 525 |
JS::UniqueChars getPropertyNameAt(uint32_t index) const; |
| 526 |
#endif |
| 527 |
|
| 528 |
#ifdef DEBUG |
| 529 |
void checkConsistency(NativeObject* obj) const; |
| 530 |
#endif |
| 531 |
|
| 532 |
void addSizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 533 |
size_t* children, size_t* tables) const; |
| 534 |
|
| 535 |
inline PropertyInfo getPropertyInfo(uint32_t index) const; |
| 536 |
|
| 537 |
PropertyInfoWithKey getPropertyInfoWithKey(uint32_t index) const { |
| 538 |
return PropertyInfoWithKey(getPropertyInfo(index), getKey(index)); |
| 539 |
} |
| 540 |
|
| 541 |
MOZ_ALWAYS_INLINE PropMap* lookupLinear(uint32_t mapLength, PropertyKey key, |
| 542 |
uint32_t* index); |
| 543 |
|
| 544 |
MOZ_ALWAYS_INLINE PropMap* lookupPure(uint32_t mapLength, PropertyKey key, |
| 545 |
uint32_t* index); |
| 546 |
|
| 547 |
MOZ_ALWAYS_INLINE PropMap* lookup(JSContext* cx, uint32_t mapLength, |
| 548 |
PropertyKey key, uint32_t* index); |
| 549 |
|
| 550 |
static inline bool lookupForRemove(JSContext* cx, PropMap* map, |
| 551 |
uint32_t mapLength, PropertyKey key, |
| 552 |
const AutoKeepPropMapTables& keep, |
| 553 |
PropMap** propMap, uint32_t* propIndex, |
| 554 |
PropMapTable** table, |
| 555 |
PropMapTable::Ptr* ptr); |
| 556 |
|
| 557 |
static const JS::TraceKind TraceKind = JS::TraceKind::PropMap; |
| 558 |
|
| 559 |
void traceChildren(JSTracer* trc); |
| 560 |
}; |
| 561 |
|
| 562 |
class SharedPropMap : public PropMap { |
| 563 |
friend class PropMap; |
| 564 |
|
| 565 |
protected: |
| 566 |
// Shared maps are stored in a tree structure. Each shared map has a TreeData |
| 567 |
// struct linking the map to its parent and children. Initial maps (the ones |
| 568 |
// stored in ShapeZone's initialPropMaps table) don't have a parent. |
| 569 |
struct TreeData { |
| 570 |
SharedChildrenPtr children; |
| 571 |
SharedPropMapAndIndex parent; |
| 572 |
|
| 573 |
void setParent(SharedPropMap* map, uint32_t index) { |
| 574 |
parent = SharedPropMapAndIndex(map, index); |
| 575 |
} |
| 576 |
}; |
| 577 |
|
| 578 |
private: |
| 579 |
static SharedPropMap* create(JSContext* cx, Handle<SharedPropMap*> prev, |
| 580 |
HandleId id, PropertyInfo prop); |
| 581 |
static SharedPropMap* createInitial(JSContext* cx, HandleId id, |
| 582 |
PropertyInfo prop); |
| 583 |
static SharedPropMap* clone(JSContext* cx, Handle<SharedPropMap*> map, |
| 584 |
uint32_t length); |
| 585 |
|
| 586 |
inline void initProperty(uint32_t index, PropertyKey key, PropertyInfo prop); |
| 587 |
|
| 588 |
static bool addPropertyInternal(JSContext* cx, |
| 589 |
MutableHandle<SharedPropMap*> map, |
| 590 |
uint32_t* mapLength, HandleId id, |
| 591 |
PropertyInfo prop); |
| 592 |
|
| 593 |
bool addChild(JSContext* cx, SharedPropMapAndIndex child, HandleId id, |
| 594 |
PropertyInfo prop); |
| 595 |
SharedPropMap* lookupChild(uint32_t length, HandleId id, PropertyInfo prop); |
| 596 |
|
| 597 |
protected: |
| 598 |
void initNumPreviousMaps(uint32_t value) { |
| 599 |
MOZ_ASSERT((flags() >> NumPreviousMapsShift) == 0); |
| 600 |
// Clamp to NumPreviousMapsMax. This is okay because this value is only used |
| 601 |
// for heuristics. |
| 602 |
if (value > NumPreviousMapsMax) { |
| 603 |
value = NumPreviousMapsMax; |
| 604 |
} |
| 605 |
setHeaderFlagBits(value << NumPreviousMapsShift); |
| 606 |
} |
| 607 |
|
| 608 |
bool hasChildrenSet() const { return flags() & HasChildrenSetFlag; } |
| 609 |
void setHasChildrenSet() { setHeaderFlagBits(HasChildrenSetFlag); } |
| 610 |
void clearHasChildrenSet() { clearHeaderFlagBits(HasChildrenSetFlag); } |
| 611 |
|
| 612 |
void setHadDictionaryConversion() { |
| 613 |
setHeaderFlagBits(HadDictionaryConversionFlag); |
| 614 |
} |
| 615 |
|
| 616 |
public: |
| 617 |
// Heuristics used when adding a property via NativeObject::addProperty and |
| 618 |
// friends: |
| 619 |
// |
| 620 |
// * If numPreviousMaps >= NumPrevMapsForAddConsiderDictionary, consider |
| 621 |
// converting the object to a dictionary object based on other heuristics. |
| 622 |
// |
| 623 |
// * If numPreviousMaps >= NumPrevMapsForAddAlwaysDictionary, always convert |
| 624 |
// the object to a dictionary object. |
| 625 |
static constexpr size_t NumPrevMapsConsiderDictionary = 32; |
| 626 |
static constexpr size_t NumPrevMapsAlwaysDictionary = 100; |
| 627 |
|
| 628 |
static_assert(NumPrevMapsConsiderDictionary < NumPreviousMapsMax); |
| 629 |
static_assert(NumPrevMapsAlwaysDictionary < NumPreviousMapsMax); |
| 630 |
|
| 631 |
// The number of properties that can definitely be added to an object without |
| 632 |
// triggering dictionary mode conversion in NativeObject::addProperty. |
| 633 |
static constexpr size_t MaxPropsForNonDictionary = |
| 634 |
NumPrevMapsConsiderDictionary * Capacity; |
| 635 |
|
| 636 |
bool isDictionary() const = delete; |
| 637 |
bool isShared() const = delete; |
| 638 |
SharedPropMap* asShared() = delete; |
| 639 |
const SharedPropMap* asShared() const = delete; |
| 640 |
|
| 641 |
bool hadDictionaryConversion() const { |
| 642 |
return flags() & HadDictionaryConversionFlag; |
| 643 |
} |
| 644 |
|
| 645 |
uint32_t numPreviousMaps() const { |
| 646 |
uint32_t val = (flags() & NumPreviousMapsMask) >> NumPreviousMapsShift; |
| 647 |
MOZ_ASSERT_IF(hasPrevious(), val > 0); |
| 648 |
return val; |
| 649 |
} |
| 650 |
|
| 651 |
MOZ_ALWAYS_INLINE bool shouldConvertToDictionaryForAdd() const; |
| 652 |
|
| 653 |
void fixupAfterMovingGC(); |
| 654 |
inline void sweep(JS::GCContext* gcx); |
| 655 |
inline void finalize(JS::GCContext* gcx); |
| 656 |
|
| 657 |
static inline void getPrevious(MutableHandle<SharedPropMap*> map, |
| 658 |
uint32_t* mapLength); |
| 659 |
|
| 660 |
bool matchProperty(uint32_t index, PropertyKey key, PropertyInfo prop) const { |
| 661 |
return getKey(index) == key && getPropertyInfo(index) == prop; |
| 662 |
} |
| 663 |
|
| 664 |
inline TreeData& treeDataRef(); |
| 665 |
inline const TreeData& treeDataRef() const; |
| 666 |
|
| 667 |
void removeChild(JS::GCContext* gcx, SharedPropMap* child); |
| 668 |
|
| 669 |
uint32_t lastUsedSlot(uint32_t mapLength) const { |
| 670 |
return getPropertyInfo(mapLength - 1).maybeSlot(); |
| 671 |
} |
| 672 |
|
| 673 |
// Number of slots required for objects with this map/mapLength. |
| 674 |
static uint32_t slotSpan(const JSClass* clasp, const SharedPropMap* map, |
| 675 |
uint32_t mapLength) { |
| 676 |
MOZ_ASSERT(clasp->isNativeObject()); |
| 677 |
uint32_t numReserved = JSCLASS_RESERVED_SLOTS(clasp); |
| 678 |
if (!map) { |
| 679 |
MOZ_ASSERT(mapLength == 0); |
| 680 |
return numReserved; |
| 681 |
} |
| 682 |
uint32_t lastSlot = map->lastUsedSlot(mapLength); |
| 683 |
if (lastSlot == SHAPE_INVALID_SLOT) { |
| 684 |
// The object only has custom data properties. |
| 685 |
return numReserved; |
| 686 |
} |
| 687 |
// Some builtin objects store properties in reserved slots. Make sure the |
| 688 |
// slot span >= numReserved. See addPropertyInReservedSlot. |
| 689 |
return std::max(lastSlot + 1, numReserved); |
| 690 |
} |
| 691 |
|
| 692 |
static uint32_t indexOfNextProperty(uint32_t index) { |
| 693 |
MOZ_ASSERT(index < PropMap::Capacity); |
| 694 |
return (index + 1) % PropMap::Capacity; |
| 695 |
} |
| 696 |
|
| 697 |
// Add a new property to this map. Returns the new map/mapLength, slot number, |
| 698 |
// and object flags. |
| 699 |
static bool addProperty(JSContext* cx, const JSClass* clasp, |
| 700 |
MutableHandle<SharedPropMap*> map, |
| 701 |
uint32_t* mapLength, HandleId id, PropertyFlags flags, |
| 702 |
ObjectFlags* objectFlags, uint32_t* slot); |
| 703 |
|
| 704 |
// Like addProperty, but for when the slot number is a reserved slot. A few |
| 705 |
// builtin objects use this for initial properties. |
| 706 |
static bool addPropertyInReservedSlot(JSContext* cx, const JSClass* clasp, |
| 707 |
MutableHandle<SharedPropMap*> map, |
| 708 |
uint32_t* mapLength, HandleId id, |
| 709 |
PropertyFlags flags, uint32_t slot, |
| 710 |
ObjectFlags* objectFlags); |
| 711 |
|
| 712 |
// Like addProperty, but for when the caller already knows the slot number to |
| 713 |
// use (or wants to assert this exact slot number is used). |
| 714 |
static bool addPropertyWithKnownSlot(JSContext* cx, const JSClass* clasp, |
| 715 |
MutableHandle<SharedPropMap*> map, |
| 716 |
uint32_t* mapLength, HandleId id, |
| 717 |
PropertyFlags flags, uint32_t slot, |
| 718 |
ObjectFlags* objectFlags); |
| 719 |
|
| 720 |
// Like addProperty, but for adding a custom data property. |
| 721 |
static bool addCustomDataProperty(JSContext* cx, const JSClass* clasp, |
| 722 |
MutableHandle<SharedPropMap*> map, |
| 723 |
uint32_t* mapLength, HandleId id, |
| 724 |
PropertyFlags flags, |
| 725 |
ObjectFlags* objectFlags); |
| 726 |
|
| 727 |
// Freeze or seal all properties by creating a new shared map. Returns the new |
| 728 |
// map and object flags. |
| 729 |
static bool freezeOrSealProperties(JSContext* cx, IntegrityLevel level, |
| 730 |
const JSClass* clasp, |
| 731 |
MutableHandle<SharedPropMap*> map, |
| 732 |
uint32_t mapLength, |
| 733 |
ObjectFlags* objectFlags); |
| 734 |
|
| 735 |
// Create a new dictionary map as copy of this map. |
| 736 |
static DictionaryPropMap* toDictionaryMap(JSContext* cx, |
| 737 |
Handle<SharedPropMap*> map, |
| 738 |
uint32_t length); |
| 739 |
|
| 740 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 741 |
void dumpOwnFields(js::JSONPrinter& json) const; |
| 742 |
#endif |
| 743 |
}; |
| 744 |
|
| 745 |
class CompactPropMap final : public SharedPropMap { |
| 746 |
CompactPropertyInfo propInfos_[Capacity]; |
| 747 |
TreeData treeData_; |
| 748 |
|
| 749 |
friend class PropMap; |
| 750 |
friend class SharedPropMap; |
| 751 |
friend class DictionaryPropMap; |
| 752 |
friend class js::gc::CellAllocator; |
| 753 |
|
| 754 |
CompactPropMap(JS::Handle<PropertyKey> key, PropertyInfo prop) { |
| 755 |
setHeaderFlagBits(IsCompactFlag); |
| 756 |
initProperty(0, key, prop); |
| 757 |
} |
| 758 |
|
| 759 |
CompactPropMap(JS::Handle<CompactPropMap*> orig, uint32_t length) { |
| 760 |
setHeaderFlagBits(IsCompactFlag); |
| 761 |
for (uint32_t i = 0; i < length; i++) { |
| 762 |
initKey(i, orig->getKey(i)); |
| 763 |
propInfos_[i] = orig->propInfos_[i]; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
void initProperty(uint32_t index, PropertyKey key, PropertyInfo prop) { |
| 768 |
MOZ_ASSERT(!hasKey(index)); |
| 769 |
initKey(index, key); |
| 770 |
propInfos_[index] = CompactPropertyInfo(prop); |
| 771 |
} |
| 772 |
|
| 773 |
TreeData& treeDataRef() { return treeData_; } |
| 774 |
const TreeData& treeDataRef() const { return treeData_; } |
| 775 |
|
| 776 |
public: |
| 777 |
bool isDictionary() const = delete; |
| 778 |
bool isShared() const = delete; |
| 779 |
bool isCompact() const = delete; |
| 780 |
bool isNormal() const = delete; |
| 781 |
bool isLinked() const = delete; |
| 782 |
CompactPropMap* asCompact() = delete; |
| 783 |
const CompactPropMap* asCompact() const = delete; |
| 784 |
|
| 785 |
PropertyInfo getPropertyInfo(uint32_t index) const { |
| 786 |
MOZ_ASSERT(hasKey(index)); |
| 787 |
return PropertyInfo(propInfos_[index]); |
| 788 |
} |
| 789 |
}; |
| 790 |
|
| 791 |
// Layout shared by NormalPropMap and DictionaryPropMap. |
| 792 |
class LinkedPropMap final : public PropMap { |
| 793 |
friend class PropMap; |
| 794 |
friend class SharedPropMap; |
| 795 |
friend class NormalPropMap; |
| 796 |
friend class DictionaryPropMap; |
| 797 |
|
| 798 |
struct Data { |
| 799 |
GCPtr<PropMap*> previous; |
| 800 |
PropMapTable* table = nullptr; |
| 801 |
PropertyInfo propInfos[Capacity]; |
| 802 |
|
| 803 |
explicit Data(PropMap* prev) : previous(prev) {} |
| 804 |
}; |
| 805 |
Data data_; |
| 806 |
|
| 807 |
bool createTable(JSContext* cx); |
| 808 |
void handOffTableTo(LinkedPropMap* next); |
| 809 |
|
| 810 |
public: |
| 811 |
bool isCompact() const = delete; |
| 812 |
bool isLinked() const = delete; |
| 813 |
LinkedPropMap* asLinked() = delete; |
| 814 |
const LinkedPropMap* asLinked() const = delete; |
| 815 |
|
| 816 |
PropMap* previous() const { return data_.previous; } |
| 817 |
|
| 818 |
bool hasTable() const { return data_.table != nullptr; } |
| 819 |
|
| 820 |
PropMapTable* maybeTable(JS::AutoCheckCannotGC& nogc) const { |
| 821 |
return data_.table; |
| 822 |
} |
| 823 |
PropMapTable* ensureTable(JSContext* cx, const JS::AutoCheckCannotGC& nogc) { |
| 824 |
if (!data_.table && MOZ_UNLIKELY(!createTable(cx))) { |
| 825 |
return nullptr; |
| 826 |
} |
| 827 |
return data_.table; |
| 828 |
} |
| 829 |
PropMapTable* ensureTable(JSContext* cx, const AutoKeepPropMapTables& keep) { |
| 830 |
if (!data_.table && MOZ_UNLIKELY(!createTable(cx))) { |
| 831 |
return nullptr; |
| 832 |
} |
| 833 |
return data_.table; |
| 834 |
} |
| 835 |
|
| 836 |
void purgeTable(JS::GCContext* gcx); |
| 837 |
|
| 838 |
void purgeTableCache() { |
| 839 |
if (data_.table) { |
| 840 |
data_.table->purgeCache(); |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
#ifdef DEBUG |
| 845 |
bool canSkipMarkingTable(); |
| 846 |
#endif |
| 847 |
|
| 848 |
PropertyInfo getPropertyInfo(uint32_t index) const { |
| 849 |
MOZ_ASSERT(hasKey(index)); |
| 850 |
return data_.propInfos[index]; |
| 851 |
} |
| 852 |
|
| 853 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 854 |
void dumpOwnFields(js::JSONPrinter& json) const; |
| 855 |
#endif |
| 856 |
}; |
| 857 |
|
| 858 |
class NormalPropMap final : public SharedPropMap { |
| 859 |
friend class PropMap; |
| 860 |
friend class SharedPropMap; |
| 861 |
friend class DictionaryPropMap; |
| 862 |
friend class js::gc::CellAllocator; |
| 863 |
|
| 864 |
LinkedPropMap::Data linkedData_; |
| 865 |
TreeData treeData_; |
| 866 |
|
| 867 |
NormalPropMap(JS::Handle<SharedPropMap*> prev, PropertyKey key, |
| 868 |
PropertyInfo prop) |
| 869 |
: linkedData_(prev) { |
| 870 |
if (prev) { |
| 871 |
setHeaderFlagBits(HasPrevFlag); |
| 872 |
initNumPreviousMaps(prev->numPreviousMaps() + 1); |
| 873 |
if (prev->hasPrevious()) { |
| 874 |
setHeaderFlagBits(CanHaveTableFlag); |
| 875 |
} |
| 876 |
} |
| 877 |
initProperty(0, key, prop); |
| 878 |
} |
| 879 |
|
| 880 |
NormalPropMap(JS::Handle<NormalPropMap*> orig, uint32_t length) |
| 881 |
: linkedData_(orig->previous()) { |
| 882 |
if (orig->hasPrevious()) { |
| 883 |
setHeaderFlagBits(HasPrevFlag); |
| 884 |
} |
| 885 |
if (orig->canHaveTable()) { |
| 886 |
setHeaderFlagBits(CanHaveTableFlag); |
| 887 |
} |
| 888 |
initNumPreviousMaps(orig->numPreviousMaps()); |
| 889 |
for (uint32_t i = 0; i < length; i++) { |
| 890 |
initProperty(i, orig->getKey(i), orig->getPropertyInfo(i)); |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
void initProperty(uint32_t index, PropertyKey key, PropertyInfo prop) { |
| 895 |
MOZ_ASSERT(!hasKey(index)); |
| 896 |
initKey(index, key); |
| 897 |
linkedData_.propInfos[index] = prop; |
| 898 |
} |
| 899 |
|
| 900 |
bool isDictionary() const = delete; |
| 901 |
bool isShared() const = delete; |
| 902 |
bool isCompact() const = delete; |
| 903 |
bool isNormal() const = delete; |
| 904 |
bool isLinked() const = delete; |
| 905 |
NormalPropMap* asNormal() = delete; |
| 906 |
const NormalPropMap* asNormal() const = delete; |
| 907 |
|
| 908 |
SharedPropMap* previous() const { |
| 909 |
return static_cast<SharedPropMap*>(linkedData_.previous.get()); |
| 910 |
} |
| 911 |
|
| 912 |
TreeData& treeDataRef() { return treeData_; } |
| 913 |
const TreeData& treeDataRef() const { return treeData_; } |
| 914 |
|
| 915 |
static void staticAsserts() { |
| 916 |
static_assert(offsetof(NormalPropMap, linkedData_) == |
| 917 |
offsetof(LinkedPropMap, data_)); |
| 918 |
} |
| 919 |
}; |
| 920 |
|
| 921 |
class DictionaryPropMap final : public PropMap { |
| 922 |
friend class PropMap; |
| 923 |
friend class SharedPropMap; |
| 924 |
friend class js::gc::CellAllocator; |
| 925 |
|
| 926 |
LinkedPropMap::Data linkedData_; |
| 927 |
|
| 928 |
// SHAPE_INVALID_SLOT or head of slot freelist in owning dictionary-mode |
| 929 |
// object. |
| 930 |
uint32_t freeList_ = SHAPE_INVALID_SLOT; |
| 931 |
|
| 932 |
// Number of holes for removed properties in this and previous maps. Used by |
| 933 |
// compacting heuristics. |
| 934 |
uint32_t holeCount_ = 0; |
| 935 |
|
| 936 |
// Empty Dictionary prop map used during reshape. |
| 937 |
explicit DictionaryPropMap(std::nullptr_t) : linkedData_(nullptr) { |
| 938 |
setHeaderFlagBits(IsDictionaryFlag | CanHaveTableFlag); |
| 939 |
} |
| 940 |
|
| 941 |
DictionaryPropMap(JS::Handle<DictionaryPropMap*> prev, PropertyKey key, |
| 942 |
PropertyInfo prop) |
| 943 |
: linkedData_(prev) { |
| 944 |
setHeaderFlagBits(IsDictionaryFlag | CanHaveTableFlag | |
| 945 |
(prev ? HasPrevFlag : 0)); |
| 946 |
initProperty(0, key, prop); |
| 947 |
} |
| 948 |
|
| 949 |
DictionaryPropMap(JS::Handle<NormalPropMap*> orig, uint32_t length) |
| 950 |
: linkedData_(nullptr) { |
| 951 |
setHeaderFlagBits(IsDictionaryFlag | CanHaveTableFlag); |
| 952 |
for (uint32_t i = 0; i < length; i++) { |
| 953 |
initProperty(i, orig->getKey(i), orig->getPropertyInfo(i)); |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
DictionaryPropMap(JS::Handle<CompactPropMap*> orig, uint32_t length) |
| 958 |
: linkedData_(nullptr) { |
| 959 |
setHeaderFlagBits(IsDictionaryFlag | CanHaveTableFlag); |
| 960 |
for (uint32_t i = 0; i < length; i++) { |
| 961 |
initProperty(i, orig->getKey(i), orig->getPropertyInfo(i)); |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
void initProperty(uint32_t index, PropertyKey key, PropertyInfo prop) { |
| 966 |
MOZ_ASSERT(!hasKey(index)); |
| 967 |
initKey(index, key); |
| 968 |
linkedData_.propInfos[index] = prop; |
| 969 |
} |
| 970 |
|
| 971 |
void initPrevious(DictionaryPropMap* prev) { |
| 972 |
MOZ_ASSERT(prev); |
| 973 |
linkedData_.previous.init(prev); |
| 974 |
setHeaderFlagBits(HasPrevFlag); |
| 975 |
} |
| 976 |
void clearPrevious() { |
| 977 |
linkedData_.previous = nullptr; |
| 978 |
clearHeaderFlagBits(HasPrevFlag); |
| 979 |
} |
| 980 |
|
| 981 |
void clearProperty(uint32_t index) { setKey(index, PropertyKey::Void()); } |
| 982 |
|
| 983 |
static void skipTrailingHoles(MutableHandle<DictionaryPropMap*> map, |
| 984 |
uint32_t* mapLength); |
| 985 |
|
| 986 |
void handOffLastMapStateTo(DictionaryPropMap* newLast); |
| 987 |
|
| 988 |
void incHoleCount() { holeCount_++; } |
| 989 |
void decHoleCount() { |
| 990 |
MOZ_ASSERT(holeCount_ > 0); |
| 991 |
holeCount_--; |
| 992 |
} |
| 993 |
static void maybeCompact(JSContext* cx, MutableHandle<DictionaryPropMap*> map, |
| 994 |
uint32_t* mapLength); |
| 995 |
|
| 996 |
public: |
| 997 |
bool isDictionary() const = delete; |
| 998 |
bool isShared() const = delete; |
| 999 |
bool isCompact() const = delete; |
| 1000 |
bool isNormal() const = delete; |
| 1001 |
bool isLinked() const = delete; |
| 1002 |
DictionaryPropMap* asDictionary() = delete; |
| 1003 |
const DictionaryPropMap* asDictionary() const = delete; |
| 1004 |
|
| 1005 |
void fixupAfterMovingGC() {} |
| 1006 |
inline void finalize(JS::GCContext* gcx); |
| 1007 |
|
| 1008 |
DictionaryPropMap* previous() const { |
| 1009 |
return static_cast<DictionaryPropMap*>(linkedData_.previous.get()); |
| 1010 |
} |
| 1011 |
|
| 1012 |
uint32_t freeList() const { return freeList_; } |
| 1013 |
void setFreeList(uint32_t slot) { freeList_ = slot; } |
| 1014 |
|
| 1015 |
PropertyInfo getPropertyInfo(uint32_t index) const { |
| 1016 |
MOZ_ASSERT(hasKey(index)); |
| 1017 |
return linkedData_.propInfos[index]; |
| 1018 |
} |
| 1019 |
|
| 1020 |
// Create an empty prop map for dictionary mode teleporting |
| 1021 |
static DictionaryPropMap* createEmpty(JSContext* cx); |
| 1022 |
|
| 1023 |
// Add a new property to this map. Returns the new map/mapLength and object |
| 1024 |
// flags. The caller is responsible for generating a new dictionary shape. |
| 1025 |
static bool addProperty(JSContext* cx, const JSClass* clasp, |
| 1026 |
MutableHandle<DictionaryPropMap*> map, |
| 1027 |
uint32_t* mapLength, HandleId id, PropertyFlags flags, |
| 1028 |
uint32_t slot, ObjectFlags* objectFlags); |
| 1029 |
|
| 1030 |
// Remove the property referenced by the table pointer. Returns the new |
| 1031 |
// map/mapLength. The caller is responsible for generating a new dictionary |
| 1032 |
// shape. |
| 1033 |
static void removeProperty(JSContext* cx, |
| 1034 |
MutableHandle<DictionaryPropMap*> map, |
| 1035 |
uint32_t* mapLength, PropMapTable* table, |
| 1036 |
PropMapTable::Ptr& ptr); |
| 1037 |
|
| 1038 |
// Turn all sparse elements into dense elements. The caller is responsible |
| 1039 |
// for checking all sparse elements are plain data properties and must |
| 1040 |
// generate a new shape for the object. |
| 1041 |
static void densifyElements(JSContext* cx, |
| 1042 |
MutableHandle<DictionaryPropMap*> map, |
| 1043 |
uint32_t* mapLength, NativeObject* obj); |
| 1044 |
|
| 1045 |
// Freeze or seal all properties in this map. Returns the new object flags. |
| 1046 |
// The caller is responsible for generating a new shape for the object. |
| 1047 |
void freezeOrSealProperties(JSContext* cx, IntegrityLevel level, |
| 1048 |
const JSClass* clasp, uint32_t mapLength, |
| 1049 |
ObjectFlags* objectFlags); |
| 1050 |
|
| 1051 |
// Change a property's slot number and/or flags and return the new object |
| 1052 |
// flags. The caller is responsible for generating a new shape. |
| 1053 |
void changeProperty(JSContext* cx, const JSClass* clasp, uint32_t index, |
| 1054 |
PropertyFlags flags, uint32_t slot, |
| 1055 |
ObjectFlags* objectFlags); |
| 1056 |
|
| 1057 |
// Like changeProperty, but doesn't change the slot number. |
| 1058 |
void changePropertyFlags(JSContext* cx, const JSClass* clasp, uint32_t index, |
| 1059 |
PropertyFlags flags, ObjectFlags* objectFlags) { |
| 1060 |
uint32_t slot = getPropertyInfo(index).maybeSlot(); |
| 1061 |
changeProperty(cx, clasp, index, flags, slot, objectFlags); |
| 1062 |
} |
| 1063 |
|
| 1064 |
static void staticAsserts() { |
| 1065 |
static_assert(offsetof(DictionaryPropMap, linkedData_) == |
| 1066 |
offsetof(LinkedPropMap, data_)); |
| 1067 |
} |
| 1068 |
|
| 1069 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1070 |
void dumpOwnFields(js::JSONPrinter& json) const; |
| 1071 |
#endif |
| 1072 |
}; |
| 1073 |
|
| 1074 |
inline CompactPropMap* PropMap::asCompact() { |
| 1075 |
MOZ_ASSERT(isCompact()); |
| 1076 |
return static_cast<CompactPropMap*>(this); |
| 1077 |
} |
| 1078 |
inline const CompactPropMap* PropMap::asCompact() const { |
| 1079 |
MOZ_ASSERT(isCompact()); |
| 1080 |
return static_cast<const CompactPropMap*>(this); |
| 1081 |
} |
| 1082 |
inline LinkedPropMap* PropMap::asLinked() { |
| 1083 |
MOZ_ASSERT(isLinked()); |
| 1084 |
return static_cast<LinkedPropMap*>(this); |
| 1085 |
} |
| 1086 |
inline const LinkedPropMap* PropMap::asLinked() const { |
| 1087 |
MOZ_ASSERT(isLinked()); |
| 1088 |
return static_cast<const LinkedPropMap*>(this); |
| 1089 |
} |
| 1090 |
inline NormalPropMap* PropMap::asNormal() { |
| 1091 |
MOZ_ASSERT(isNormal()); |
| 1092 |
return static_cast<NormalPropMap*>(this); |
| 1093 |
} |
| 1094 |
inline const NormalPropMap* PropMap::asNormal() const { |
| 1095 |
MOZ_ASSERT(isNormal()); |
| 1096 |
return static_cast<const NormalPropMap*>(this); |
| 1097 |
} |
| 1098 |
inline SharedPropMap* PropMap::asShared() { |
| 1099 |
MOZ_ASSERT(isShared()); |
| 1100 |
return static_cast<SharedPropMap*>(this); |
| 1101 |
} |
| 1102 |
inline const SharedPropMap* PropMap::asShared() const { |
| 1103 |
MOZ_ASSERT(isShared()); |
| 1104 |
return static_cast<const SharedPropMap*>(this); |
| 1105 |
} |
| 1106 |
inline DictionaryPropMap* PropMap::asDictionary() { |
| 1107 |
MOZ_ASSERT(isDictionary()); |
| 1108 |
return static_cast<DictionaryPropMap*>(this); |
| 1109 |
} |
| 1110 |
inline const DictionaryPropMap* PropMap::asDictionary() const { |
| 1111 |
MOZ_ASSERT(isDictionary()); |
| 1112 |
return static_cast<const DictionaryPropMap*>(this); |
| 1113 |
} |
| 1114 |
|
| 1115 |
inline PropertyInfo PropMap::getPropertyInfo(uint32_t index) const { |
| 1116 |
return isCompact() ? asCompact()->getPropertyInfo(index) |
| 1117 |
: asLinked()->getPropertyInfo(index); |
| 1118 |
} |
| 1119 |
|
| 1120 |
inline SharedPropMap::TreeData& SharedPropMap::treeDataRef() { |
| 1121 |
return isCompact() ? asCompact()->treeDataRef() : asNormal()->treeDataRef(); |
| 1122 |
} |
| 1123 |
|
| 1124 |
inline const SharedPropMap::TreeData& SharedPropMap::treeDataRef() const { |
| 1125 |
return isCompact() ? asCompact()->treeDataRef() : asNormal()->treeDataRef(); |
| 1126 |
} |
| 1127 |
|
| 1128 |
inline void SharedPropMap::initProperty(uint32_t index, PropertyKey key, |
| 1129 |
PropertyInfo prop) { |
| 1130 |
if (isCompact()) { |
| 1131 |
asCompact()->initProperty(index, key, prop); |
| 1132 |
} else { |
| 1133 |
asNormal()->initProperty(index, key, prop); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
|
| 1137 |
template <typename T> |
| 1138 |
inline PropertyInfo MapAndIndex<T>::propertyInfo() const { |
| 1139 |
MOZ_ASSERT(!isNone()); |
| 1140 |
return map()->getPropertyInfo(index()); |
| 1141 |
} |
| 1142 |
|
| 1143 |
MOZ_ALWAYS_INLINE HashNumber PropMapTable::Hasher::hash(PropertyKey key) { |
| 1144 |
return HashPropertyKey(key); |
| 1145 |
} |
| 1146 |
MOZ_ALWAYS_INLINE bool PropMapTable::Hasher::match(PropMapAndIndex entry, |
| 1147 |
PropertyKey key) { |
| 1148 |
MOZ_ASSERT(entry.map()->hasKey(entry.index())); |
| 1149 |
return entry.map()->getKey(entry.index()) == key; |
| 1150 |
} |
| 1151 |
|
| 1152 |
// Hash policy for SharedPropMap children. |
| 1153 |
struct SharedChildrenHasher { |
| 1154 |
using Key = SharedPropMapAndIndex; |
| 1155 |
|
| 1156 |
struct Lookup { |
| 1157 |
PropertyKey key; |
| 1158 |
PropertyInfo prop; |
| 1159 |
uint8_t index; |
| 1160 |
|
| 1161 |
Lookup(PropertyKey key, PropertyInfo prop, uint8_t index) |
| 1162 |
: key(key), prop(prop), index(index) {} |
| 1163 |
Lookup(PropertyInfoWithKey prop, uint8_t index) |
| 1164 |
: key(prop.key()), prop(prop), index(index) {} |
| 1165 |
}; |
| 1166 |
|
| 1167 |
static HashNumber hash(const Lookup& l) { |
| 1168 |
HashNumber hash = HashPropertyKey(l.key); |
| 1169 |
return mozilla::AddToHash(hash, l.prop.toRaw(), l.index); |
| 1170 |
} |
| 1171 |
static bool match(SharedPropMapAndIndex k, const Lookup& l) { |
| 1172 |
SharedPropMap* map = k.map(); |
| 1173 |
uint32_t index = k.index(); |
| 1174 |
uint32_t newIndex = SharedPropMap::indexOfNextProperty(index); |
| 1175 |
return index == l.index && map->matchProperty(newIndex, l.key, l.prop); |
| 1176 |
} |
| 1177 |
}; |
| 1178 |
|
| 1179 |
} // namespace js |
| 1180 |
|
| 1181 |
// JS::ubi::Nodes can point to PropMaps; they're js::gc::Cell instances |
| 1182 |
// with no associated compartment. |
| 1183 |
namespace JS { |
| 1184 |
namespace ubi { |
| 1185 |
|
| 1186 |
template <> |
| 1187 |
class Concrete<js::PropMap> : TracerConcrete<js::PropMap> { |
| 1188 |
protected: |
| 1189 |
explicit Concrete(js::PropMap* ptr) : TracerConcrete<js::PropMap>(ptr) {} |
| 1190 |
|
| 1191 |
public: |
| 1192 |
static void construct(void* storage, js::PropMap* ptr) { |
| 1193 |
new (storage) Concrete(ptr); |
| 1194 |
} |
| 1195 |
|
| 1196 |
Size size(mozilla::MallocSizeOf mallocSizeOf) const override; |
| 1197 |
|
| 1198 |
const char16_t* typeName() const override { return concreteTypeName; } |
| 1199 |
static const char16_t concreteTypeName[]; |
| 1200 |
}; |
| 1201 |
|
| 1202 |
} // namespace ubi |
| 1203 |
} // namespace JS |
| 1204 |
|
| 1205 |
#endif // vm_PropMap_h |