| 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 builtin_MapObject_h |
| 8 |
#define builtin_MapObject_h |
| 9 |
|
| 10 |
#include "mozilla/MemoryReporting.h" |
| 11 |
|
| 12 |
#include "builtin/OrderedHashTableObject.h" |
| 13 |
#include "vm/JSObject.h" |
| 14 |
#include "vm/NativeObject.h" |
| 15 |
|
| 16 |
namespace js { |
| 17 |
|
| 18 |
/* |
| 19 |
* Comparing two ropes for equality can fail. The js::HashTable template |
| 20 |
* requires infallible hash() and match() operations. Therefore we require |
| 21 |
* all values to be converted to hashable form before being used as a key |
| 22 |
* in a Map or Set object. |
| 23 |
* |
| 24 |
* All values except ropes are hashable as-is. |
| 25 |
*/ |
| 26 |
class HashableValue { |
| 27 |
Value value; |
| 28 |
|
| 29 |
public: |
| 30 |
HashableValue() : value(UndefinedValue()) {} |
| 31 |
explicit HashableValue(JSWhyMagic whyMagic) : value(MagicValue(whyMagic)) {} |
| 32 |
|
| 33 |
[[nodiscard]] bool setValue(JSContext* cx, const Value& v); |
| 34 |
HashNumber hash(const mozilla::HashCodeScrambler& hcs) const; |
| 35 |
|
| 36 |
// Value equality. Separate BigInt instances may compare equal. |
| 37 |
bool equals(const HashableValue& other) const; |
| 38 |
|
| 39 |
// Bitwise equality. |
| 40 |
bool operator==(const HashableValue& other) const { |
| 41 |
return value == other.value; |
| 42 |
} |
| 43 |
bool operator!=(const HashableValue& other) const { |
| 44 |
return !(*this == other); |
| 45 |
} |
| 46 |
|
| 47 |
const Value& get() const { return value; } |
| 48 |
operator Value() const { return get(); } |
| 49 |
|
| 50 |
void trace(JSTracer* trc) { |
| 51 |
TraceManuallyBarrieredEdge(trc, &value, "HashableValue"); |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
template <typename Wrapper> |
| 56 |
class WrappedPtrOperations<HashableValue, Wrapper> { |
| 57 |
public: |
| 58 |
Value get() const { return static_cast<const Wrapper*>(this)->get().get(); } |
| 59 |
}; |
| 60 |
|
| 61 |
template <typename Wrapper> |
| 62 |
class MutableWrappedPtrOperations<HashableValue, Wrapper> |
| 63 |
: public WrappedPtrOperations<HashableValue, Wrapper> { |
| 64 |
public: |
| 65 |
[[nodiscard]] bool setValue(JSContext* cx, HandleValue v) { |
| 66 |
return static_cast<Wrapper*>(this)->get().setValue(cx, v); |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
template <> |
| 71 |
struct InternalBarrierMethods<HashableValue> { |
| 72 |
static bool isMarkable(const HashableValue& v) { return v.get().isGCThing(); } |
| 73 |
|
| 74 |
static void preBarrier(const HashableValue& v) { |
| 75 |
if (isMarkable(v)) { |
| 76 |
gc::ValuePreWriteBarrier(v.get()); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#ifdef DEBUG |
| 81 |
static void assertThingIsNotGray(const HashableValue& v) { |
| 82 |
JS::AssertValueIsNotGray(v.get()); |
| 83 |
} |
| 84 |
#endif |
| 85 |
}; |
| 86 |
|
| 87 |
struct HashableValueHasher { |
| 88 |
using Key = PreBarriered<HashableValue>; |
| 89 |
using Lookup = HashableValue; |
| 90 |
|
| 91 |
static HashNumber hash(const Lookup& v, |
| 92 |
const mozilla::HashCodeScrambler& hcs) { |
| 93 |
return v.hash(hcs); |
| 94 |
} |
| 95 |
static bool match(const Key& k, const Lookup& l) { return k.get().equals(l); } |
| 96 |
static bool isEmpty(const Key& v) { |
| 97 |
return v.get().get().isMagic(JS_HASH_KEY_EMPTY); |
| 98 |
} |
| 99 |
static void makeEmpty(Key* vp) { vp->set(HashableValue(JS_HASH_KEY_EMPTY)); } |
| 100 |
}; |
| 101 |
|
| 102 |
template <typename ObjectT> |
| 103 |
class OrderedHashTableRef; |
| 104 |
|
| 105 |
struct UnbarrieredHashPolicy; |
| 106 |
|
| 107 |
class MapObject : public OrderedHashMapObject { |
| 108 |
public: |
| 109 |
using Table = OrderedHashMapImpl<PreBarriered<HashableValue>, HeapPtr<Value>, |
| 110 |
HashableValueHasher>; |
| 111 |
|
| 112 |
// PreBarrieredTable has the same memory layout as Table but doesn't have |
| 113 |
// wrappers that perform post barriers on the keys/values. Used when the |
| 114 |
// MapObject is in the nursery. |
| 115 |
using PreBarrieredTable = |
| 116 |
OrderedHashMapImpl<PreBarriered<HashableValue>, PreBarriered<Value>, |
| 117 |
HashableValueHasher>; |
| 118 |
|
| 119 |
// UnbarrieredTable has the same memory layout as Table but doesn't have any |
| 120 |
// wrappers that perform barriers on the keys/values. Used to allocate and |
| 121 |
// delete the table and when updating the nursery allocated keys map during |
| 122 |
// minor GC. |
| 123 |
using UnbarrieredTable = |
| 124 |
OrderedHashMapImpl<Value, Value, UnbarrieredHashPolicy>; |
| 125 |
|
| 126 |
friend class OrderedHashTableRef<MapObject>; |
| 127 |
|
| 128 |
enum { |
| 129 |
NurseryKeysSlot = Table::SlotCount, |
| 130 |
RegisteredNurseryIteratorsSlot, |
| 131 |
SlotCount |
| 132 |
}; |
| 133 |
|
| 134 |
using IteratorKind = TableIteratorObject::Kind; |
| 135 |
|
| 136 |
static const JSClass class_; |
| 137 |
static const JSClass protoClass_; |
| 138 |
|
| 139 |
[[nodiscard]] bool getKeysAndValuesInterleaved( |
| 140 |
JS::MutableHandle<GCVector<JS::Value>> entries); |
| 141 |
[[nodiscard]] static bool entries(JSContext* cx, unsigned argc, Value* vp); |
| 142 |
|
| 143 |
static MapObject* createWithProto(JSContext* cx, HandleObject proto, |
| 144 |
NewObjectKind newKind); |
| 145 |
static MapObject* create(JSContext* cx, HandleObject proto = nullptr); |
| 146 |
static MapObject* createFromIterable( |
| 147 |
JSContext* cx, Handle<JSObject*> proto, Handle<Value> iterable, |
| 148 |
Handle<MapObject*> allocatedFromJit = nullptr); |
| 149 |
|
| 150 |
// Publicly exposed Map calls for JSAPI access (webidl maplike/setlike |
| 151 |
// interfaces, etc.) |
| 152 |
uint32_t size(); |
| 153 |
[[nodiscard]] bool get(JSContext* cx, const Value& key, |
| 154 |
MutableHandleValue rval); |
| 155 |
[[nodiscard]] bool has(JSContext* cx, const Value& key, bool* rval); |
| 156 |
#ifdef NIGHTLY_BUILD |
| 157 |
[[nodiscard]] bool getOrInsert(JSContext* cx, const Value& key, |
| 158 |
const Value& val, MutableHandleValue rval); |
| 159 |
#endif // #ifdef NIGHTLY_BUILD |
| 160 |
[[nodiscard]] bool delete_(JSContext* cx, const Value& key, bool* rval); |
| 161 |
|
| 162 |
// Set call for public JSAPI exposure. Does not actually return map object |
| 163 |
// as stated in spec, expects caller to return a value. for instance, with |
| 164 |
// webidl maplike/setlike, should return interface object. |
| 165 |
[[nodiscard]] bool set(JSContext* cx, const Value& key, const Value& val); |
| 166 |
void clear(JSContext* cx); |
| 167 |
[[nodiscard]] static bool iterator(JSContext* cx, IteratorKind kind, |
| 168 |
Handle<MapObject*> obj, |
| 169 |
MutableHandleValue iter); |
| 170 |
|
| 171 |
void clearNurseryIteratorsBeforeMinorGC(); |
| 172 |
|
| 173 |
// Sweeps a map that had nursery memory associated with it after a minor |
| 174 |
// GC. This may finalize the map if it was in the nursery and has died. |
| 175 |
// |
| 176 |
// Returns a pointer to the map if it still has nursery memory associated with |
| 177 |
// it, or nullptr. |
| 178 |
static MapObject* sweepAfterMinorGC(JS::GCContext* gcx, MapObject* mapobj); |
| 179 |
|
| 180 |
size_t sizeOfData(mozilla::MallocSizeOf mallocSizeOf); |
| 181 |
|
| 182 |
[[nodiscard]] static bool get(JSContext* cx, unsigned argc, Value* vp); |
| 183 |
[[nodiscard]] static bool set(JSContext* cx, unsigned argc, Value* vp); |
| 184 |
[[nodiscard]] static bool has(JSContext* cx, unsigned argc, Value* vp); |
| 185 |
|
| 186 |
static bool isOriginalSizeGetter(Native native) { |
| 187 |
return native == static_cast<Native>(MapObject::size); |
| 188 |
} |
| 189 |
|
| 190 |
private: |
| 191 |
static const ClassSpec classSpec_; |
| 192 |
static const JSClassOps classOps_; |
| 193 |
static const ClassExtension classExtension_; |
| 194 |
|
| 195 |
static const JSPropertySpec properties[]; |
| 196 |
static const JSFunctionSpec methods[]; |
| 197 |
static const JSPropertySpec staticProperties[]; |
| 198 |
static const JSFunctionSpec staticMethods[]; |
| 199 |
|
| 200 |
[[nodiscard]] bool setWithHashableKey(JSContext* cx, const HashableValue& key, |
| 201 |
const Value& value); |
| 202 |
|
| 203 |
[[nodiscard]] bool tryOptimizeCtorWithIterable(JSContext* cx, |
| 204 |
const Value& iterableVal, |
| 205 |
bool* optimized); |
| 206 |
|
| 207 |
static bool finishInit(JSContext* cx, HandleObject ctor, HandleObject proto); |
| 208 |
|
| 209 |
static void trace(JSTracer* trc, JSObject* obj); |
| 210 |
static void finalize(JS::GCContext* gcx, JSObject* obj); |
| 211 |
static size_t objectMoved(JSObject* obj, JSObject* old); |
| 212 |
|
| 213 |
[[nodiscard]] static bool construct(JSContext* cx, unsigned argc, Value* vp); |
| 214 |
|
| 215 |
static bool is(HandleValue v); |
| 216 |
static bool is(HandleObject o); |
| 217 |
|
| 218 |
[[nodiscard]] static bool iterator_impl(JSContext* cx, const CallArgs& args, |
| 219 |
IteratorKind kind); |
| 220 |
|
| 221 |
[[nodiscard]] static bool size_impl(JSContext* cx, const CallArgs& args); |
| 222 |
[[nodiscard]] static bool size(JSContext* cx, unsigned argc, Value* vp); |
| 223 |
[[nodiscard]] static bool get_impl(JSContext* cx, const CallArgs& args); |
| 224 |
[[nodiscard]] static bool has_impl(JSContext* cx, const CallArgs& args); |
| 225 |
[[nodiscard]] static bool set_impl(JSContext* cx, const CallArgs& args); |
| 226 |
#ifdef NIGHTLY_BUILD |
| 227 |
[[nodiscard]] static bool getOrInsert(JSContext* cx, unsigned argc, |
| 228 |
Value* vp); |
| 229 |
[[nodiscard]] static bool getOrInsert_impl(JSContext* cx, |
| 230 |
const CallArgs& args); |
| 231 |
#endif |
| 232 |
[[nodiscard]] static bool delete_impl(JSContext* cx, const CallArgs& args); |
| 233 |
[[nodiscard]] static bool delete_(JSContext* cx, unsigned argc, Value* vp); |
| 234 |
[[nodiscard]] static bool keys_impl(JSContext* cx, const CallArgs& args); |
| 235 |
[[nodiscard]] static bool keys(JSContext* cx, unsigned argc, Value* vp); |
| 236 |
[[nodiscard]] static bool values_impl(JSContext* cx, const CallArgs& args); |
| 237 |
[[nodiscard]] static bool values(JSContext* cx, unsigned argc, Value* vp); |
| 238 |
[[nodiscard]] static bool entries_impl(JSContext* cx, const CallArgs& args); |
| 239 |
[[nodiscard]] static bool clear_impl(JSContext* cx, const CallArgs& args); |
| 240 |
[[nodiscard]] static bool clear(JSContext* cx, unsigned argc, Value* vp); |
| 241 |
}; |
| 242 |
|
| 243 |
class MapIteratorObject : public TableIteratorObject { |
| 244 |
public: |
| 245 |
static const JSClass class_; |
| 246 |
|
| 247 |
static const JSFunctionSpec methods[]; |
| 248 |
static MapIteratorObject* create(JSContext* cx, Handle<MapObject*> mapobj, |
| 249 |
Kind kind); |
| 250 |
static void finalize(JS::GCContext* gcx, JSObject* obj); |
| 251 |
static size_t objectMoved(JSObject* obj, JSObject* old); |
| 252 |
|
| 253 |
[[nodiscard]] static bool next(MapIteratorObject* mapIterator, |
| 254 |
ArrayObject* resultPairObj); |
| 255 |
|
| 256 |
static JSObject* createResultPair(JSContext* cx); |
| 257 |
|
| 258 |
private: |
| 259 |
MapObject* target() const; |
| 260 |
}; |
| 261 |
|
| 262 |
class SetObject : public OrderedHashSetObject { |
| 263 |
public: |
| 264 |
using Table = |
| 265 |
OrderedHashSetImpl<PreBarriered<HashableValue>, HashableValueHasher>; |
| 266 |
using UnbarrieredTable = OrderedHashSetImpl<Value, UnbarrieredHashPolicy>; |
| 267 |
|
| 268 |
friend class OrderedHashTableRef<SetObject>; |
| 269 |
|
| 270 |
enum { |
| 271 |
NurseryKeysSlot = Table::SlotCount, |
| 272 |
RegisteredNurseryIteratorsSlot, |
| 273 |
SlotCount |
| 274 |
}; |
| 275 |
|
| 276 |
using IteratorKind = TableIteratorObject::Kind; |
| 277 |
|
| 278 |
static const JSClass class_; |
| 279 |
static const JSClass protoClass_; |
| 280 |
|
| 281 |
[[nodiscard]] bool keys(JS::MutableHandle<GCVector<JS::Value>> keys); |
| 282 |
[[nodiscard]] static bool values(JSContext* cx, unsigned argc, Value* vp); |
| 283 |
[[nodiscard]] bool add(JSContext* cx, const Value& key); |
| 284 |
|
| 285 |
// Publicly exposed Set calls for JSAPI access (webidl maplike/setlike |
| 286 |
// interfaces, etc.) |
| 287 |
static SetObject* createWithProto(JSContext* cx, HandleObject proto, |
| 288 |
NewObjectKind newKind); |
| 289 |
static SetObject* create(JSContext* cx, HandleObject proto = nullptr); |
| 290 |
static SetObject* createFromIterable( |
| 291 |
JSContext* cx, Handle<JSObject*> proto, Handle<Value> iterable, |
| 292 |
Handle<SetObject*> allocatedFromJit = nullptr); |
| 293 |
|
| 294 |
uint32_t size(); |
| 295 |
[[nodiscard]] static bool size(JSContext* cx, unsigned argc, Value* vp); |
| 296 |
[[nodiscard]] static bool add(JSContext* cx, unsigned argc, Value* vp); |
| 297 |
[[nodiscard]] static bool has(JSContext* cx, unsigned argc, Value* vp); |
| 298 |
[[nodiscard]] bool has(JSContext* cx, const Value& key, bool* rval); |
| 299 |
void clear(JSContext* cx); |
| 300 |
[[nodiscard]] static bool iterator(JSContext* cx, IteratorKind kind, |
| 301 |
Handle<SetObject*> obj, |
| 302 |
MutableHandleValue iter); |
| 303 |
[[nodiscard]] static bool delete_(JSContext* cx, unsigned argc, Value* vp); |
| 304 |
[[nodiscard]] bool delete_(JSContext* cx, const Value& key, bool* rval); |
| 305 |
|
| 306 |
[[nodiscard]] static bool copy(JSContext* cx, unsigned argc, Value* vp); |
| 307 |
|
| 308 |
void clearNurseryIteratorsBeforeMinorGC(); |
| 309 |
|
| 310 |
// Sweeps a set that had nursery memory associated with it after a minor |
| 311 |
// GC. This may finalize the set if it was in the nursery and has died. |
| 312 |
// |
| 313 |
// Returns a pointer to the set if it still has nursery memory associated with |
| 314 |
// it, or nullptr. |
| 315 |
static SetObject* sweepAfterMinorGC(JS::GCContext* gcx, SetObject* setobj); |
| 316 |
|
| 317 |
size_t sizeOfData(mozilla::MallocSizeOf mallocSizeOf); |
| 318 |
|
| 319 |
static bool isOriginalSizeGetter(Native native) { |
| 320 |
return native == static_cast<Native>(SetObject::size); |
| 321 |
} |
| 322 |
|
| 323 |
private: |
| 324 |
static const ClassSpec classSpec_; |
| 325 |
static const JSClassOps classOps_; |
| 326 |
static const ClassExtension classExtension_; |
| 327 |
|
| 328 |
static const JSPropertySpec properties[]; |
| 329 |
static const JSFunctionSpec methods[]; |
| 330 |
static const JSPropertySpec staticProperties[]; |
| 331 |
|
| 332 |
[[nodiscard]] bool addHashableValue(JSContext* cx, |
| 333 |
const HashableValue& value); |
| 334 |
|
| 335 |
[[nodiscard]] bool tryOptimizeCtorWithIterable(JSContext* cx, |
| 336 |
const Value& iterableVal, |
| 337 |
bool* optimized); |
| 338 |
|
| 339 |
static bool finishInit(JSContext* cx, HandleObject ctor, HandleObject proto); |
| 340 |
|
| 341 |
static void trace(JSTracer* trc, JSObject* obj); |
| 342 |
static void finalize(JS::GCContext* gcx, JSObject* obj); |
| 343 |
static size_t objectMoved(JSObject* obj, JSObject* old); |
| 344 |
|
| 345 |
static bool construct(JSContext* cx, unsigned argc, Value* vp); |
| 346 |
|
| 347 |
static bool is(HandleValue v); |
| 348 |
static bool is(HandleObject o); |
| 349 |
|
| 350 |
[[nodiscard]] static bool iterator_impl(JSContext* cx, const CallArgs& args, |
| 351 |
IteratorKind kind); |
| 352 |
|
| 353 |
[[nodiscard]] static bool size_impl(JSContext* cx, const CallArgs& args); |
| 354 |
[[nodiscard]] static bool has_impl(JSContext* cx, const CallArgs& args); |
| 355 |
[[nodiscard]] static bool add_impl(JSContext* cx, const CallArgs& args); |
| 356 |
[[nodiscard]] static bool delete_impl(JSContext* cx, const CallArgs& args); |
| 357 |
[[nodiscard]] static bool values_impl(JSContext* cx, const CallArgs& args); |
| 358 |
[[nodiscard]] static bool entries_impl(JSContext* cx, const CallArgs& args); |
| 359 |
[[nodiscard]] static bool entries(JSContext* cx, unsigned argc, Value* vp); |
| 360 |
[[nodiscard]] static bool clear_impl(JSContext* cx, const CallArgs& args); |
| 361 |
[[nodiscard]] static bool clear(JSContext* cx, unsigned argc, Value* vp); |
| 362 |
}; |
| 363 |
|
| 364 |
class SetIteratorObject : public TableIteratorObject { |
| 365 |
public: |
| 366 |
static const JSClass class_; |
| 367 |
|
| 368 |
static const JSFunctionSpec methods[]; |
| 369 |
static SetIteratorObject* create(JSContext* cx, Handle<SetObject*> setobj, |
| 370 |
Kind kind); |
| 371 |
static void finalize(JS::GCContext* gcx, JSObject* obj); |
| 372 |
static size_t objectMoved(JSObject* obj, JSObject* old); |
| 373 |
|
| 374 |
[[nodiscard]] static bool next(SetIteratorObject* setIterator, |
| 375 |
ArrayObject* resultObj); |
| 376 |
|
| 377 |
static JSObject* createResult(JSContext* cx); |
| 378 |
|
| 379 |
private: |
| 380 |
SetObject* target() const; |
| 381 |
}; |
| 382 |
|
| 383 |
} /* namespace js */ |
| 384 |
|
| 385 |
#endif /* builtin_MapObject_h */ |