| 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 "builtin/MapObject-inl.h" |
| 8 |
|
| 9 |
#include "jsapi.h" |
| 10 |
|
| 11 |
#include "builtin/OrderedHashTableObject.h" |
| 12 |
#include "gc/GCContext.h" |
| 13 |
#include "jit/InlinableNatives.h" |
| 14 |
#include "js/MapAndSet.h" |
| 15 |
#include "js/PropertyAndElement.h" // JS_DefineFunctions |
| 16 |
#include "js/PropertySpec.h" |
| 17 |
#include "js/Utility.h" |
| 18 |
#include "vm/BigIntType.h" |
| 19 |
#include "vm/EqualityOperations.h" // js::SameValue |
| 20 |
#include "vm/GlobalObject.h" |
| 21 |
#include "vm/Interpreter.h" |
| 22 |
#include "vm/JSContext.h" |
| 23 |
#include "vm/JSObject.h" |
| 24 |
#include "vm/SelfHosting.h" |
| 25 |
#include "vm/SymbolType.h" |
| 26 |
|
| 27 |
#include "builtin/OrderedHashTableObject-inl.h" |
| 28 |
#include "gc/GCContext-inl.h" |
| 29 |
#include "gc/Marking-inl.h" |
| 30 |
#include "gc/ObjectKind-inl.h" |
| 31 |
#include "vm/GeckoProfiler-inl.h" |
| 32 |
#include "vm/NativeObject-inl.h" |
| 33 |
|
| 34 |
using namespace js; |
| 35 |
|
| 36 |
using mozilla::NumberEqualsInt32; |
| 37 |
|
| 38 |
/*** HashableValue **********************************************************/ |
| 39 |
|
| 40 |
static PreBarriered<Value> NormalizeDoubleValue(double d) { |
| 41 |
int32_t i; |
| 42 |
if (NumberEqualsInt32(d, &i)) { |
| 43 |
// Normalize int32_t-valued doubles to int32_t for faster hashing and |
| 44 |
// testing. Note: we use NumberEqualsInt32 here instead of NumberIsInt32 |
| 45 |
// because we want -0 and 0 to be normalized to the same thing. |
| 46 |
return Int32Value(i); |
| 47 |
} |
| 48 |
|
| 49 |
// Normalize the sign bit of a NaN. |
| 50 |
return JS::CanonicalizedDoubleValue(d); |
| 51 |
} |
| 52 |
|
| 53 |
bool HashableValue::setValue(JSContext* cx, const Value& v) { |
| 54 |
if (v.isString()) { |
| 55 |
// Atomize so that hash() and operator==() are fast and infallible. |
| 56 |
JSString* str = AtomizeString(cx, v.toString()); |
| 57 |
if (!str) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
value = StringValue(str); |
| 61 |
} else if (v.isDouble()) { |
| 62 |
value = NormalizeDoubleValue(v.toDouble()); |
| 63 |
} else { |
| 64 |
value = v; |
| 65 |
} |
| 66 |
|
| 67 |
MOZ_ASSERT(value.isUndefined() || value.isNull() || value.isBoolean() || |
| 68 |
value.isNumber() || value.isString() || value.isSymbol() || |
| 69 |
value.isObject() || value.isBigInt()); |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
static HashNumber HashValue(const Value& v, |
| 74 |
const mozilla::HashCodeScrambler& hcs) { |
| 75 |
// HashableValue::setValue normalizes values so that the SameValue relation |
| 76 |
// on HashableValues is the same as the == relationship on |
| 77 |
// value.asRawBits(). So why not just return that? Security. |
| 78 |
// |
| 79 |
// To avoid revealing GC of atoms, string-based hash codes are computed |
| 80 |
// from the string contents rather than any pointer; to avoid revealing |
| 81 |
// addresses, pointer-based hash codes are computed using the |
| 82 |
// HashCodeScrambler. |
| 83 |
|
| 84 |
if (v.isString()) { |
| 85 |
return v.toString()->asAtom().hash(); |
| 86 |
} |
| 87 |
if (v.isSymbol()) { |
| 88 |
return v.toSymbol()->hash(); |
| 89 |
} |
| 90 |
if (v.isBigInt()) { |
| 91 |
return MaybeForwarded(v.toBigInt())->hash(); |
| 92 |
} |
| 93 |
if (v.isObject()) { |
| 94 |
return hcs.scramble(v.asRawBits()); |
| 95 |
} |
| 96 |
|
| 97 |
MOZ_ASSERT(!v.isGCThing(), "do not reveal pointers via hash codes"); |
| 98 |
return mozilla::HashGeneric(v.asRawBits()); |
| 99 |
} |
| 100 |
|
| 101 |
HashNumber HashableValue::hash(const mozilla::HashCodeScrambler& hcs) const { |
| 102 |
return HashValue(value, hcs); |
| 103 |
} |
| 104 |
|
| 105 |
bool HashableValue::equals(const HashableValue& other) const { |
| 106 |
// Two HashableValues are equal if they have equal bits. |
| 107 |
bool b = (value.asRawBits() == other.value.asRawBits()); |
| 108 |
|
| 109 |
if (!b && (value.type() == other.value.type())) { |
| 110 |
if (value.isBigInt()) { |
| 111 |
// BigInt values are considered equal if they represent the same |
| 112 |
// mathematical value. |
| 113 |
b = BigInt::equal(value.toBigInt(), other.value.toBigInt()); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#ifdef DEBUG |
| 118 |
bool same; |
| 119 |
JSContext* cx = TlsContext.get(); |
| 120 |
RootedValue valueRoot(cx, value); |
| 121 |
RootedValue otherRoot(cx, other.value); |
| 122 |
MOZ_ASSERT(SameValueZero(cx, valueRoot, otherRoot, &same)); |
| 123 |
MOZ_ASSERT(same == b); |
| 124 |
#endif |
| 125 |
return b; |
| 126 |
} |
| 127 |
|
| 128 |
/*** MapIterator ************************************************************/ |
| 129 |
|
| 130 |
namespace {} /* anonymous namespace */ |
| 131 |
|
| 132 |
static const JSClassOps MapIteratorObjectClassOps = { |
| 133 |
nullptr, // addProperty |
| 134 |
nullptr, // delProperty |
| 135 |
nullptr, // enumerate |
| 136 |
nullptr, // newEnumerate |
| 137 |
nullptr, // resolve |
| 138 |
nullptr, // mayResolve |
| 139 |
MapIteratorObject::finalize, // finalize |
| 140 |
nullptr, // call |
| 141 |
nullptr, // construct |
| 142 |
nullptr, // trace |
| 143 |
}; |
| 144 |
|
| 145 |
static const ClassExtension MapIteratorObjectClassExtension = { |
| 146 |
MapIteratorObject::objectMoved, // objectMovedOp |
| 147 |
}; |
| 148 |
|
| 149 |
const JSClass MapIteratorObject::class_ = { |
| 150 |
"Map Iterator", |
| 151 |
JSCLASS_HAS_RESERVED_SLOTS(MapIteratorObject::SlotCount) | |
| 152 |
JSCLASS_FOREGROUND_FINALIZE | JSCLASS_SKIP_NURSERY_FINALIZE, |
| 153 |
&MapIteratorObjectClassOps, |
| 154 |
JS_NULL_CLASS_SPEC, |
| 155 |
&MapIteratorObjectClassExtension, |
| 156 |
}; |
| 157 |
|
| 158 |
const JSFunctionSpec MapIteratorObject::methods[] = { |
| 159 |
JS_SELF_HOSTED_FN("next", "MapIteratorNext", 0, 0), |
| 160 |
JS_FS_END, |
| 161 |
}; |
| 162 |
|
| 163 |
/* static */ |
| 164 |
bool GlobalObject::initMapIteratorProto(JSContext* cx, |
| 165 |
Handle<GlobalObject*> global) { |
| 166 |
Rooted<JSObject*> base( |
| 167 |
cx, GlobalObject::getOrCreateIteratorPrototype(cx, global)); |
| 168 |
if (!base) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
Rooted<PlainObject*> proto( |
| 172 |
cx, GlobalObject::createBlankPrototypeInheriting<PlainObject>(cx, base)); |
| 173 |
if (!proto) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
if (!JS_DefineFunctions(cx, proto, MapIteratorObject::methods) || |
| 177 |
!DefineToStringTag(cx, proto, cx->names().Map_Iterator_)) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
if (!JSObject::setHasFuseProperty(cx, proto)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
global->initBuiltinProto(ProtoKind::MapIteratorProto, proto); |
| 184 |
return true; |
| 185 |
} |
| 186 |
|
| 187 |
template <typename TableObject> |
| 188 |
static inline bool HasRegisteredNurseryIterators(TableObject* t) { |
| 189 |
Value v = t->getReservedSlot(TableObject::RegisteredNurseryIteratorsSlot); |
| 190 |
return v.toBoolean(); |
| 191 |
} |
| 192 |
|
| 193 |
template <typename TableObject> |
| 194 |
static inline void SetRegisteredNurseryIterators(TableObject* t, bool b) { |
| 195 |
t->setReservedSlot(TableObject::RegisteredNurseryIteratorsSlot, |
| 196 |
JS::BooleanValue(b)); |
| 197 |
} |
| 198 |
|
| 199 |
MapIteratorObject* MapIteratorObject::create(JSContext* cx, |
| 200 |
Handle<MapObject*> mapobj, |
| 201 |
Kind kind) { |
| 202 |
Rooted<GlobalObject*> global(cx, &mapobj->global()); |
| 203 |
Rooted<JSObject*> proto( |
| 204 |
cx, GlobalObject::getOrCreateMapIteratorPrototype(cx, global)); |
| 205 |
if (!proto) { |
| 206 |
return nullptr; |
| 207 |
} |
| 208 |
|
| 209 |
MapIteratorObject* iterobj = |
| 210 |
NewObjectWithGivenProto<MapIteratorObject>(cx, proto); |
| 211 |
if (!iterobj) { |
| 212 |
return nullptr; |
| 213 |
} |
| 214 |
|
| 215 |
if (IsInsideNursery(iterobj) && |
| 216 |
!HasRegisteredNurseryIterators(mapobj.get())) { |
| 217 |
if (!cx->nursery().addMapWithNurseryIterators(mapobj)) { |
| 218 |
ReportOutOfMemory(cx); |
| 219 |
return nullptr; |
| 220 |
} |
| 221 |
SetRegisteredNurseryIterators(mapobj.get(), true); |
| 222 |
} |
| 223 |
|
| 224 |
MapObject::Table(mapobj).initIterator(iterobj, kind); |
| 225 |
|
| 226 |
return iterobj; |
| 227 |
} |
| 228 |
|
| 229 |
void MapIteratorObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 230 |
MOZ_ASSERT(gcx->onMainThread()); |
| 231 |
MOZ_ASSERT(!IsInsideNursery(obj)); |
| 232 |
if (obj->as<MapIteratorObject>().isActive()) { |
| 233 |
obj->as<MapIteratorObject>().unlink(); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
size_t MapIteratorObject::objectMoved(JSObject* obj, JSObject* old) { |
| 238 |
MapIteratorObject* iter = &obj->as<MapIteratorObject>(); |
| 239 |
if (!iter->isActive()) { |
| 240 |
return 0; |
| 241 |
} |
| 242 |
if (IsInsideNursery(old)) { |
| 243 |
MapObject* mapObj = iter->target(); |
| 244 |
MapObject::Table(mapObj).relinkNurseryIterator(iter); |
| 245 |
} else { |
| 246 |
iter->updateListAfterMove(&old->as<MapIteratorObject>()); |
| 247 |
} |
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
MapObject* MapIteratorObject::target() const { |
| 252 |
MOZ_ASSERT(isActive(), "only active iterators have a target object"); |
| 253 |
Value value = getFixedSlot(TargetSlot); |
| 254 |
return &MaybeForwarded(&value.toObject())->as<MapObject>(); |
| 255 |
} |
| 256 |
|
| 257 |
bool MapIteratorObject::next(MapIteratorObject* mapIterator, |
| 258 |
ArrayObject* resultPairObj) { |
| 259 |
// IC code calls this directly. |
| 260 |
AutoUnsafeCallWithABI unsafe; |
| 261 |
|
| 262 |
// Check invariants for inlined GetNextMapEntryForIterator. |
| 263 |
|
| 264 |
// The array should be tenured, so that post-barrier can be done simply. |
| 265 |
MOZ_ASSERT(resultPairObj->isTenured()); |
| 266 |
|
| 267 |
// The array elements should be fixed. |
| 268 |
MOZ_ASSERT(resultPairObj->hasFixedElements()); |
| 269 |
MOZ_ASSERT(resultPairObj->getDenseInitializedLength() == 2); |
| 270 |
MOZ_ASSERT(resultPairObj->getDenseCapacity() >= 2); |
| 271 |
|
| 272 |
if (!mapIterator->isActive()) { |
| 273 |
// Already done. |
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
auto storeResult = [resultPairObj](Kind kind, const auto& element) { |
| 278 |
switch (kind) { |
| 279 |
case Kind::Keys: |
| 280 |
resultPairObj->setDenseElement(0, element.key.get()); |
| 281 |
break; |
| 282 |
|
| 283 |
case Kind::Values: |
| 284 |
resultPairObj->setDenseElement(1, element.value); |
| 285 |
break; |
| 286 |
|
| 287 |
case Kind::Entries: { |
| 288 |
resultPairObj->setDenseElement(0, element.key.get()); |
| 289 |
resultPairObj->setDenseElement(1, element.value); |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
}; |
| 294 |
MapObject* mapObj = mapIterator->target(); |
| 295 |
return MapObject::Table(mapObj).iteratorNext(mapIterator, storeResult); |
| 296 |
} |
| 297 |
|
| 298 |
/* static */ |
| 299 |
JSObject* MapIteratorObject::createResultPair(JSContext* cx) { |
| 300 |
Rooted<ArrayObject*> resultPairObj( |
| 301 |
cx, NewDenseFullyAllocatedArray(cx, 2, TenuredObject)); |
| 302 |
if (!resultPairObj) { |
| 303 |
return nullptr; |
| 304 |
} |
| 305 |
|
| 306 |
resultPairObj->setDenseInitializedLength(2); |
| 307 |
resultPairObj->initDenseElement(0, NullValue()); |
| 308 |
resultPairObj->initDenseElement(1, NullValue()); |
| 309 |
|
| 310 |
return resultPairObj; |
| 311 |
} |
| 312 |
|
| 313 |
/*** Map ********************************************************************/ |
| 314 |
|
| 315 |
struct js::UnbarrieredHashPolicy { |
| 316 |
using Lookup = Value; |
| 317 |
static HashNumber hash(const Lookup& v, |
| 318 |
const mozilla::HashCodeScrambler& hcs) { |
| 319 |
return HashValue(v, hcs); |
| 320 |
} |
| 321 |
static bool match(const Value& k, const Lookup& l) { return k == l; } |
| 322 |
static bool isEmpty(const Value& v) { return v.isMagic(JS_HASH_KEY_EMPTY); } |
| 323 |
static void makeEmpty(Value* vp) { vp->setMagic(JS_HASH_KEY_EMPTY); } |
| 324 |
}; |
| 325 |
|
| 326 |
// MapObject::Table, ::UnbarrieredTable and ::PreBarrieredTable must all have |
| 327 |
// the same memory layout. |
| 328 |
static_assert(sizeof(MapObject::Table::Entry) == |
| 329 |
sizeof(MapObject::UnbarrieredTable::Entry)); |
| 330 |
static_assert(sizeof(MapObject::Table::Entry) == |
| 331 |
sizeof(MapObject::PreBarrieredTable::Entry)); |
| 332 |
|
| 333 |
const JSClassOps MapObject::classOps_ = { |
| 334 |
nullptr, // addProperty |
| 335 |
nullptr, // delProperty |
| 336 |
nullptr, // enumerate |
| 337 |
nullptr, // newEnumerate |
| 338 |
nullptr, // resolve |
| 339 |
nullptr, // mayResolve |
| 340 |
finalize, // finalize |
| 341 |
nullptr, // call |
| 342 |
nullptr, // construct |
| 343 |
trace, // trace |
| 344 |
}; |
| 345 |
|
| 346 |
const ClassSpec MapObject::classSpec_ = { |
| 347 |
GenericCreateConstructor<MapObject::construct, 0, gc::AllocKind::FUNCTION, |
| 348 |
&jit::JitInfo_MapConstructor>, |
| 349 |
GenericCreatePrototype<MapObject>, |
| 350 |
MapObject::staticMethods, |
| 351 |
MapObject::staticProperties, |
| 352 |
MapObject::methods, |
| 353 |
MapObject::properties, |
| 354 |
MapObject::finishInit, |
| 355 |
}; |
| 356 |
|
| 357 |
const ClassExtension MapObject::classExtension_ = { |
| 358 |
MapObject::objectMoved, // objectMovedOp |
| 359 |
}; |
| 360 |
|
| 361 |
const JSClass MapObject::class_ = { |
| 362 |
"Map", |
| 363 |
JSCLASS_DELAY_METADATA_BUILDER | |
| 364 |
JSCLASS_HAS_RESERVED_SLOTS(MapObject::SlotCount) | |
| 365 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Map) | JSCLASS_BACKGROUND_FINALIZE | |
| 366 |
JSCLASS_SKIP_NURSERY_FINALIZE, |
| 367 |
&MapObject::classOps_, &MapObject::classSpec_, &MapObject::classExtension_}; |
| 368 |
|
| 369 |
const JSClass MapObject::protoClass_ = { |
| 370 |
"Map.prototype", |
| 371 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Map), |
| 372 |
JS_NULL_CLASS_OPS, |
| 373 |
&MapObject::classSpec_, |
| 374 |
}; |
| 375 |
|
| 376 |
const JSPropertySpec MapObject::properties[] = { |
| 377 |
JS_PSG("size", size, 0), |
| 378 |
JS_STRING_SYM_PS(toStringTag, "Map", JSPROP_READONLY), |
| 379 |
JS_PS_END, |
| 380 |
}; |
| 381 |
|
| 382 |
const JSFunctionSpec MapObject::methods[] = { |
| 383 |
JS_INLINABLE_FN("get", get, 1, 0, MapGet), |
| 384 |
JS_INLINABLE_FN("has", has, 1, 0, MapHas), |
| 385 |
JS_INLINABLE_FN("set", set, 2, 0, MapSet), |
| 386 |
JS_INLINABLE_FN("delete", delete_, 1, 0, MapDelete), |
| 387 |
JS_FN("keys", keys, 0, 0), |
| 388 |
JS_FN("values", values, 0, 0), |
| 389 |
JS_FN("clear", clear, 0, 0), |
| 390 |
JS_SELF_HOSTED_FN("forEach", "MapForEach", 2, 0), |
| 391 |
#ifdef NIGHTLY_BUILD |
| 392 |
JS_FN("getOrInsert", getOrInsert, 2, 0), |
| 393 |
JS_SELF_HOSTED_FN("getOrInsertComputed", "MapGetOrInsertComputed", 2, 0), |
| 394 |
#endif |
| 395 |
JS_FN("entries", entries, 0, 0), |
| 396 |
// @@iterator is re-defined in finishInit so that it has the |
| 397 |
// same identity as |entries|. |
| 398 |
JS_SYM_FN(iterator, entries, 0, 0), |
| 399 |
JS_FS_END, |
| 400 |
}; |
| 401 |
|
| 402 |
const JSPropertySpec MapObject::staticProperties[] = { |
| 403 |
JS_SELF_HOSTED_SYM_GET(species, "$MapSpecies", 0), |
| 404 |
JS_PS_END, |
| 405 |
}; |
| 406 |
|
| 407 |
const JSFunctionSpec MapObject::staticMethods[] = { |
| 408 |
JS_SELF_HOSTED_FN("groupBy", "MapGroupBy", 2, 0), |
| 409 |
JS_FS_END, |
| 410 |
}; |
| 411 |
|
| 412 |
/* static */ bool MapObject::finishInit(JSContext* cx, HandleObject ctor, |
| 413 |
HandleObject proto) { |
| 414 |
Handle<NativeObject*> nativeProto = proto.as<NativeObject>(); |
| 415 |
|
| 416 |
RootedValue entriesFn(cx); |
| 417 |
RootedId entriesId(cx, NameToId(cx->names().entries)); |
| 418 |
if (!NativeGetProperty(cx, nativeProto, entriesId, &entriesFn)) { |
| 419 |
return false; |
| 420 |
} |
| 421 |
|
| 422 |
// 23.1.3.12 Map.prototype[@@iterator]() |
| 423 |
// The initial value of the @@iterator property is the same function object |
| 424 |
// as the initial value of the "entries" property. |
| 425 |
RootedId iteratorId(cx, PropertyKey::Symbol(cx->wellKnownSymbols().iterator)); |
| 426 |
if (!NativeDefineDataProperty(cx, nativeProto, iteratorId, entriesFn, 0)) { |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
return JSObject::setHasFuseProperty(cx, nativeProto); |
| 431 |
} |
| 432 |
|
| 433 |
void MapObject::trace(JSTracer* trc, JSObject* obj) { |
| 434 |
MapObject* mapObj = &obj->as<MapObject>(); |
| 435 |
Table(mapObj).trace(trc); |
| 436 |
} |
| 437 |
|
| 438 |
using NurseryKeysVector = GCVector<Value, 4, SystemAllocPolicy>; |
| 439 |
|
| 440 |
template <typename TableObject> |
| 441 |
static NurseryKeysVector* GetNurseryKeys(TableObject* t) { |
| 442 |
Value value = t->getReservedSlot(TableObject::NurseryKeysSlot); |
| 443 |
return reinterpret_cast<NurseryKeysVector*>(value.toPrivate()); |
| 444 |
} |
| 445 |
|
| 446 |
template <typename TableObject> |
| 447 |
static NurseryKeysVector* AllocNurseryKeys(TableObject* t) { |
| 448 |
MOZ_ASSERT(!GetNurseryKeys(t)); |
| 449 |
auto keys = js_new<NurseryKeysVector>(); |
| 450 |
if (!keys) { |
| 451 |
return nullptr; |
| 452 |
} |
| 453 |
|
| 454 |
t->setReservedSlot(TableObject::NurseryKeysSlot, PrivateValue(keys)); |
| 455 |
return keys; |
| 456 |
} |
| 457 |
|
| 458 |
template <typename TableObject> |
| 459 |
static void DeleteNurseryKeys(TableObject* t) { |
| 460 |
auto keys = GetNurseryKeys(t); |
| 461 |
MOZ_ASSERT(keys); |
| 462 |
js_delete(keys); |
| 463 |
t->setReservedSlot(TableObject::NurseryKeysSlot, PrivateValue(nullptr)); |
| 464 |
} |
| 465 |
|
| 466 |
// A generic store buffer entry that traces all nursery keys for an ordered hash |
| 467 |
// map or set. |
| 468 |
template <typename ObjectT> |
| 469 |
class js::OrderedHashTableRef : public gc::BufferableRef { |
| 470 |
ObjectT* object; |
| 471 |
|
| 472 |
public: |
| 473 |
explicit OrderedHashTableRef(ObjectT* obj) : object(obj) {} |
| 474 |
|
| 475 |
void trace(JSTracer* trc) override { |
| 476 |
MOZ_ASSERT(!IsInsideNursery(object)); |
| 477 |
NurseryKeysVector* keys = GetNurseryKeys(object); |
| 478 |
MOZ_ASSERT(keys); |
| 479 |
|
| 480 |
keys->mutableEraseIf([&](Value& key) { |
| 481 |
MOZ_ASSERT(typename ObjectT::UnbarrieredTable(object).hash(key) == |
| 482 |
typename ObjectT::Table(object).hash( |
| 483 |
*reinterpret_cast<const HashableValue*>(&key))); |
| 484 |
MOZ_ASSERT(IsInsideNursery(key.toGCThing())); |
| 485 |
|
| 486 |
auto result = typename ObjectT::UnbarrieredTable(object).rekeyOneEntry( |
| 487 |
key, [trc](const Value& prior) { |
| 488 |
Value key = prior; |
| 489 |
TraceManuallyBarrieredEdge(trc, &key, "ordered hash table key"); |
| 490 |
return key; |
| 491 |
}); |
| 492 |
|
| 493 |
if (result.isNothing()) { |
| 494 |
return true; // Key removed. |
| 495 |
} |
| 496 |
|
| 497 |
key = result.value(); |
| 498 |
return !IsInsideNursery(key.toGCThing()); |
| 499 |
}); |
| 500 |
|
| 501 |
if (!keys->empty()) { |
| 502 |
trc->runtime()->gc.storeBuffer().putGeneric( |
| 503 |
OrderedHashTableRef<ObjectT>(object)); |
| 504 |
return; |
| 505 |
} |
| 506 |
|
| 507 |
DeleteNurseryKeys(object); |
| 508 |
} |
| 509 |
}; |
| 510 |
|
| 511 |
template <typename ObjectT> |
| 512 |
[[nodiscard]] inline static bool PostWriteBarrier(ObjectT* obj, |
| 513 |
const Value& keyValue) { |
| 514 |
MOZ_ASSERT(!IsInsideNursery(obj)); |
| 515 |
|
| 516 |
if (MOZ_LIKELY(!keyValue.isObject() && !keyValue.isBigInt())) { |
| 517 |
MOZ_ASSERT_IF(keyValue.isGCThing(), !IsInsideNursery(keyValue.toGCThing())); |
| 518 |
return true; |
| 519 |
} |
| 520 |
|
| 521 |
if (!IsInsideNursery(keyValue.toGCThing())) { |
| 522 |
return true; |
| 523 |
} |
| 524 |
|
| 525 |
NurseryKeysVector* keys = GetNurseryKeys(obj); |
| 526 |
if (!keys) { |
| 527 |
keys = AllocNurseryKeys(obj); |
| 528 |
if (!keys) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
keyValue.toGCThing()->storeBuffer()->putGeneric( |
| 533 |
OrderedHashTableRef<ObjectT>(obj)); |
| 534 |
} |
| 535 |
|
| 536 |
return keys->append(keyValue); |
| 537 |
} |
| 538 |
|
| 539 |
bool MapObject::getKeysAndValuesInterleaved( |
| 540 |
JS::MutableHandle<GCVector<JS::Value>> entries) { |
| 541 |
auto appendEntry = [&entries](auto& entry) { |
| 542 |
return entries.append(entry.key.get()) && entries.append(entry.value); |
| 543 |
}; |
| 544 |
return Table(this).forEachEntry(appendEntry); |
| 545 |
} |
| 546 |
|
| 547 |
bool MapObject::set(JSContext* cx, const Value& key, const Value& val) { |
| 548 |
HashableValue k; |
| 549 |
if (!k.setValue(cx, key)) { |
| 550 |
return false; |
| 551 |
} |
| 552 |
return setWithHashableKey(cx, k, val); |
| 553 |
} |
| 554 |
|
| 555 |
bool MapObject::setWithHashableKey(JSContext* cx, const HashableValue& key, |
| 556 |
const Value& value) { |
| 557 |
bool needsPostBarriers = isTenured(); |
| 558 |
if (needsPostBarriers) { |
| 559 |
// Use the Table representation which has post barriers. |
| 560 |
if (!PostWriteBarrier(this, key)) { |
| 561 |
ReportOutOfMemory(cx); |
| 562 |
return false; |
| 563 |
} |
| 564 |
if (!Table(this).put(cx, key, value)) { |
| 565 |
return false; |
| 566 |
} |
| 567 |
} else { |
| 568 |
// Use the PreBarrieredTable representation which does not. |
| 569 |
if (!PreBarrieredTable(this).put(cx, key, value)) { |
| 570 |
return false; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
#ifdef NIGHTLY_BUILD |
| 578 |
bool MapObject::getOrInsert(JSContext* cx, const Value& key, const Value& val, |
| 579 |
MutableHandleValue rval) { |
| 580 |
HashableValue k; |
| 581 |
if (!k.setValue(cx, key)) { |
| 582 |
return false; |
| 583 |
} |
| 584 |
|
| 585 |
bool needsPostBarriers = isTenured(); |
| 586 |
if (needsPostBarriers) { |
| 587 |
if (!PostWriteBarrier(this, k)) { |
| 588 |
ReportOutOfMemory(cx); |
| 589 |
return false; |
| 590 |
} |
| 591 |
// Use the Table representation which has post barriers. |
| 592 |
if (const Table::Entry* p = Table(this).getOrAdd(cx, k, val)) { |
| 593 |
rval.set(p->value); |
| 594 |
} else { |
| 595 |
return false; |
| 596 |
} |
| 597 |
} else { |
| 598 |
// Use the PreBarrieredTable representation which does not. |
| 599 |
if (const PreBarrieredTable::Entry* p = |
| 600 |
PreBarrieredTable(this).getOrAdd(cx, k, val)) { |
| 601 |
rval.set(p->value); |
| 602 |
} else { |
| 603 |
return false; |
| 604 |
} |
| 605 |
} |
| 606 |
return true; |
| 607 |
} |
| 608 |
#endif // #ifdef NIGHTLY_BUILD |
| 609 |
|
| 610 |
MapObject* MapObject::createWithProto(JSContext* cx, HandleObject proto, |
| 611 |
NewObjectKind newKind) { |
| 612 |
MOZ_ASSERT(proto); |
| 613 |
|
| 614 |
gc::AllocKind allocKind = gc::GetGCObjectKind(SlotCount); |
| 615 |
|
| 616 |
AutoSetNewObjectMetadata metadata(cx); |
| 617 |
auto* mapObj = |
| 618 |
NewObjectWithGivenProtoAndKinds<MapObject>(cx, proto, allocKind, newKind); |
| 619 |
if (!mapObj) { |
| 620 |
return nullptr; |
| 621 |
} |
| 622 |
|
| 623 |
UnbarrieredTable(mapObj).initSlots(); |
| 624 |
mapObj->initReservedSlot(NurseryKeysSlot, PrivateValue(nullptr)); |
| 625 |
mapObj->initReservedSlot(RegisteredNurseryIteratorsSlot, BooleanValue(false)); |
| 626 |
return mapObj; |
| 627 |
} |
| 628 |
|
| 629 |
MapObject* MapObject::create(JSContext* cx, |
| 630 |
HandleObject proto /* = nullptr */) { |
| 631 |
if (proto) { |
| 632 |
return createWithProto(cx, proto, GenericObject); |
| 633 |
} |
| 634 |
|
| 635 |
// This is the common case so use the template object's shape to optimize the |
| 636 |
// allocation. |
| 637 |
MapObject* templateObj = GlobalObject::getOrCreateMapTemplateObject(cx); |
| 638 |
if (!templateObj) { |
| 639 |
return nullptr; |
| 640 |
} |
| 641 |
|
| 642 |
gc::AllocKind allocKind = templateObj->asTenured().getAllocKind(); |
| 643 |
MOZ_ASSERT(gc::GetGCKindSlots(allocKind) >= SlotCount); |
| 644 |
MOZ_ASSERT(gc::IsBackgroundFinalized(allocKind)); |
| 645 |
|
| 646 |
AutoSetNewObjectMetadata metadata(cx); |
| 647 |
Rooted<SharedShape*> shape(cx, templateObj->sharedShape()); |
| 648 |
auto* mapObj = |
| 649 |
NativeObject::create<MapObject>(cx, allocKind, gc::Heap::Default, shape); |
| 650 |
if (!mapObj) { |
| 651 |
return nullptr; |
| 652 |
} |
| 653 |
|
| 654 |
UnbarrieredTable(mapObj).initSlots(); |
| 655 |
mapObj->initReservedSlot(NurseryKeysSlot, PrivateValue(nullptr)); |
| 656 |
mapObj->initReservedSlot(RegisteredNurseryIteratorsSlot, BooleanValue(false)); |
| 657 |
return mapObj; |
| 658 |
} |
| 659 |
|
| 660 |
// static |
| 661 |
MapObject* GlobalObject::getOrCreateMapTemplateObject(JSContext* cx) { |
| 662 |
GlobalObjectData& data = cx->global()->data(); |
| 663 |
if (MapObject* obj = data.mapObjectTemplate) { |
| 664 |
return obj; |
| 665 |
} |
| 666 |
|
| 667 |
Rooted<JSObject*> proto(cx, |
| 668 |
GlobalObject::getOrCreatePrototype(cx, JSProto_Map)); |
| 669 |
if (!proto) { |
| 670 |
return nullptr; |
| 671 |
} |
| 672 |
auto* mapObj = MapObject::createWithProto(cx, proto, TenuredObject); |
| 673 |
if (!mapObj) { |
| 674 |
return nullptr; |
| 675 |
} |
| 676 |
|
| 677 |
data.mapObjectTemplate.init(mapObj); |
| 678 |
return mapObj; |
| 679 |
} |
| 680 |
|
| 681 |
size_t MapObject::sizeOfData(mozilla::MallocSizeOf mallocSizeOf) { |
| 682 |
size_t size = 0; |
| 683 |
size += Table(this).sizeOfExcludingObject(mallocSizeOf); |
| 684 |
if (NurseryKeysVector* nurseryKeys = GetNurseryKeys(this)) { |
| 685 |
size += nurseryKeys->sizeOfIncludingThis(mallocSizeOf); |
| 686 |
} |
| 687 |
return size; |
| 688 |
} |
| 689 |
|
| 690 |
void MapObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 691 |
MapObject* mapObj = &obj->as<MapObject>(); |
| 692 |
MOZ_ASSERT(!IsInsideNursery(mapObj)); |
| 693 |
MOZ_ASSERT(!UnbarrieredTable(mapObj).hasNurseryIterators()); |
| 694 |
|
| 695 |
#ifdef DEBUG |
| 696 |
// If we're finalizing a tenured map then it cannot contain nursery things, |
| 697 |
// because we evicted the nursery at the start of collection and writing a |
| 698 |
// nursery thing into the table would require it to be live, which means it |
| 699 |
// would have been marked. |
| 700 |
UnbarrieredTable(mapObj).forEachEntryUpTo(1000, [](auto& entry) { |
| 701 |
Value key = entry.key; |
| 702 |
MOZ_ASSERT_IF(key.isGCThing(), !IsInsideNursery(key.toGCThing())); |
| 703 |
Value value = entry.value; |
| 704 |
MOZ_ASSERT_IF(value.isGCThing(), !IsInsideNursery(value.toGCThing())); |
| 705 |
}); |
| 706 |
#endif |
| 707 |
|
| 708 |
// Finalized tenured maps do not contain nursery GC things, so do not require |
| 709 |
// post barriers. Pre barriers are not required for finalization. |
| 710 |
UnbarrieredTable(mapObj).destroy(gcx); |
| 711 |
} |
| 712 |
|
| 713 |
size_t MapObject::objectMoved(JSObject* obj, JSObject* old) { |
| 714 |
auto* mapObj = &obj->as<MapObject>(); |
| 715 |
|
| 716 |
Table(mapObj).updateIteratorsAfterMove(&old->as<MapObject>()); |
| 717 |
|
| 718 |
if (IsInsideNursery(old)) { |
| 719 |
Nursery& nursery = mapObj->runtimeFromMainThread()->gc.nursery(); |
| 720 |
Table(mapObj).maybeMoveBufferOnPromotion(nursery); |
| 721 |
} |
| 722 |
|
| 723 |
return 0; |
| 724 |
} |
| 725 |
|
| 726 |
void MapObject::clearNurseryIteratorsBeforeMinorGC() { |
| 727 |
Table(this).clearNurseryIterators(); |
| 728 |
} |
| 729 |
|
| 730 |
/* static */ |
| 731 |
MapObject* MapObject::sweepAfterMinorGC(JS::GCContext* gcx, MapObject* mapobj) { |
| 732 |
Nursery& nursery = gcx->runtime()->gc.nursery(); |
| 733 |
bool wasInCollectedRegion = nursery.inCollectedRegion(mapobj); |
| 734 |
if (wasInCollectedRegion && !IsForwarded(mapobj)) { |
| 735 |
// This MapObject is dead. |
| 736 |
return nullptr; |
| 737 |
} |
| 738 |
|
| 739 |
mapobj = MaybeForwarded(mapobj); |
| 740 |
|
| 741 |
// Keep |mapobj| registered with the nursery if it still has nursery |
| 742 |
// iterators. |
| 743 |
bool hasNurseryIterators = Table(mapobj).hasNurseryIterators(); |
| 744 |
SetRegisteredNurseryIterators(mapobj, hasNurseryIterators); |
| 745 |
return hasNurseryIterators ? mapobj : nullptr; |
| 746 |
} |
| 747 |
|
| 748 |
bool MapObject::tryOptimizeCtorWithIterable(JSContext* cx, |
| 749 |
const Value& iterableVal, |
| 750 |
bool* optimized) { |
| 751 |
MOZ_ASSERT(!iterableVal.isNullOrUndefined()); |
| 752 |
MOZ_ASSERT(!*optimized); |
| 753 |
|
| 754 |
if (!CanOptimizeMapOrSetCtorWithIterable<JSProto_Map>(MapObject::set, this, |
| 755 |
cx)) { |
| 756 |
return true; |
| 757 |
} |
| 758 |
|
| 759 |
if (!iterableVal.isObject()) { |
| 760 |
return true; |
| 761 |
} |
| 762 |
JSObject* iterable = &iterableVal.toObject(); |
| 763 |
|
| 764 |
// Fast path for `new Map(array)`. |
| 765 |
if (IsOptimizableArrayForMapOrSetCtor<MapOrSet::Map>(iterable, cx)) { |
| 766 |
ArrayObject* array = &iterable->as<ArrayObject>(); |
| 767 |
uint32_t len = array->getDenseInitializedLength(); |
| 768 |
|
| 769 |
for (uint32_t index = 0; index < len; index++) { |
| 770 |
Value element = array->getDenseElement(index); |
| 771 |
MOZ_ASSERT(IsPackedArray(&element.toObject())); |
| 772 |
|
| 773 |
auto* elementArray = &element.toObject().as<ArrayObject>(); |
| 774 |
Value key = elementArray->getDenseElement(0); |
| 775 |
Value value = elementArray->getDenseElement(1); |
| 776 |
|
| 777 |
MOZ_ASSERT(!key.isMagic(JS_ELEMENTS_HOLE)); |
| 778 |
MOZ_ASSERT(!value.isMagic(JS_ELEMENTS_HOLE)); |
| 779 |
|
| 780 |
if (!set(cx, key, value)) { |
| 781 |
return false; |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
*optimized = true; |
| 786 |
return true; |
| 787 |
} |
| 788 |
|
| 789 |
// Fast path for `new Map(map)`. |
| 790 |
if (IsMapObjectWithDefaultIterator(iterable, cx)) { |
| 791 |
auto* iterableMap = &iterable->as<MapObject>(); |
| 792 |
auto addEntry = [cx, this](auto& entry) { |
| 793 |
return setWithHashableKey(cx, entry.key, entry.value); |
| 794 |
}; |
| 795 |
if (!Table(iterableMap).forEachEntry(addEntry)) { |
| 796 |
return false; |
| 797 |
} |
| 798 |
*optimized = true; |
| 799 |
return true; |
| 800 |
} |
| 801 |
|
| 802 |
return true; |
| 803 |
} |
| 804 |
|
| 805 |
// static |
| 806 |
MapObject* MapObject::createFromIterable(JSContext* cx, Handle<JSObject*> proto, |
| 807 |
Handle<Value> iterable, |
| 808 |
Handle<MapObject*> allocatedFromJit) { |
| 809 |
// A null-or-undefined |iterable| is quite common and we check for this in JIT |
| 810 |
// code. |
| 811 |
MOZ_ASSERT_IF(allocatedFromJit, !iterable.isNullOrUndefined()); |
| 812 |
|
| 813 |
Rooted<MapObject*> obj(cx, allocatedFromJit); |
| 814 |
if (!obj) { |
| 815 |
obj = MapObject::create(cx, proto); |
| 816 |
if (!obj) { |
| 817 |
return nullptr; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
if (!iterable.isNullOrUndefined()) { |
| 822 |
bool optimized = false; |
| 823 |
if (!obj->tryOptimizeCtorWithIterable(cx, iterable, &optimized)) { |
| 824 |
return nullptr; |
| 825 |
} |
| 826 |
if (!optimized) { |
| 827 |
FixedInvokeArgs<1> args(cx); |
| 828 |
args[0].set(iterable); |
| 829 |
|
| 830 |
RootedValue thisv(cx, ObjectValue(*obj)); |
| 831 |
if (!CallSelfHostedFunction(cx, cx->names().MapConstructorInit, thisv, |
| 832 |
args, args.rval())) { |
| 833 |
return nullptr; |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
return obj; |
| 839 |
} |
| 840 |
|
| 841 |
bool MapObject::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 842 |
AutoJSConstructorProfilerEntry pseudoFrame(cx, "Map"); |
| 843 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 844 |
|
| 845 |
if (!ThrowIfNotConstructing(cx, args, "Map")) { |
| 846 |
return false; |
| 847 |
} |
| 848 |
|
| 849 |
RootedObject proto(cx); |
| 850 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_Map, &proto)) { |
| 851 |
return false; |
| 852 |
} |
| 853 |
|
| 854 |
MapObject* obj = MapObject::createFromIterable(cx, proto, args.get(0)); |
| 855 |
if (!obj) { |
| 856 |
return false; |
| 857 |
} |
| 858 |
|
| 859 |
args.rval().setObject(*obj); |
| 860 |
return true; |
| 861 |
} |
| 862 |
|
| 863 |
bool MapObject::is(HandleValue v) { |
| 864 |
return v.isObject() && v.toObject().hasClass(&class_); |
| 865 |
} |
| 866 |
|
| 867 |
bool MapObject::is(HandleObject o) { return o->hasClass(&class_); } |
| 868 |
|
| 869 |
uint32_t MapObject::size() { |
| 870 |
static_assert(sizeof(Table(this).count()) <= sizeof(uint32_t), |
| 871 |
"map count must be precisely representable as a JS number"); |
| 872 |
return Table(this).count(); |
| 873 |
} |
| 874 |
|
| 875 |
bool MapObject::size_impl(JSContext* cx, const CallArgs& args) { |
| 876 |
auto* mapObj = &args.thisv().toObject().as<MapObject>(); |
| 877 |
args.rval().setNumber(mapObj->size()); |
| 878 |
return true; |
| 879 |
} |
| 880 |
|
| 881 |
bool MapObject::size(JSContext* cx, unsigned argc, Value* vp) { |
| 882 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "size"); |
| 883 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 884 |
return CallNonGenericMethod<MapObject::is, MapObject::size_impl>(cx, args); |
| 885 |
} |
| 886 |
|
| 887 |
bool MapObject::get(JSContext* cx, const Value& key, MutableHandleValue rval) { |
| 888 |
HashableValue k; |
| 889 |
if (!k.setValue(cx, key)) { |
| 890 |
return false; |
| 891 |
} |
| 892 |
|
| 893 |
if (const Table::Entry* p = Table(this).get(k)) { |
| 894 |
rval.set(p->value); |
| 895 |
} else { |
| 896 |
rval.setUndefined(); |
| 897 |
} |
| 898 |
|
| 899 |
return true; |
| 900 |
} |
| 901 |
|
| 902 |
bool MapObject::get_impl(JSContext* cx, const CallArgs& args) { |
| 903 |
auto* mapObj = &args.thisv().toObject().as<MapObject>(); |
| 904 |
return mapObj->get(cx, args.get(0), args.rval()); |
| 905 |
} |
| 906 |
|
| 907 |
bool MapObject::get(JSContext* cx, unsigned argc, Value* vp) { |
| 908 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "get"); |
| 909 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 910 |
return CallNonGenericMethod<MapObject::is, MapObject::get_impl>(cx, args); |
| 911 |
} |
| 912 |
|
| 913 |
bool MapObject::has(JSContext* cx, const Value& key, bool* rval) { |
| 914 |
HashableValue k; |
| 915 |
if (!k.setValue(cx, key)) { |
| 916 |
return false; |
| 917 |
} |
| 918 |
|
| 919 |
*rval = Table(this).has(k); |
| 920 |
return true; |
| 921 |
} |
| 922 |
|
| 923 |
bool MapObject::has_impl(JSContext* cx, const CallArgs& args) { |
| 924 |
auto* mapObj = &args.thisv().toObject().as<MapObject>(); |
| 925 |
bool found; |
| 926 |
if (!mapObj->has(cx, args.get(0), &found)) { |
| 927 |
return false; |
| 928 |
} |
| 929 |
args.rval().setBoolean(found); |
| 930 |
return true; |
| 931 |
} |
| 932 |
|
| 933 |
bool MapObject::has(JSContext* cx, unsigned argc, Value* vp) { |
| 934 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "has"); |
| 935 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 936 |
return CallNonGenericMethod<MapObject::is, MapObject::has_impl>(cx, args); |
| 937 |
} |
| 938 |
|
| 939 |
bool MapObject::set_impl(JSContext* cx, const CallArgs& args) { |
| 940 |
auto* mapObj = &args.thisv().toObject().as<MapObject>(); |
| 941 |
if (!mapObj->set(cx, args.get(0), args.get(1))) { |
| 942 |
return false; |
| 943 |
} |
| 944 |
args.rval().set(args.thisv()); |
| 945 |
return true; |
| 946 |
} |
| 947 |
|
| 948 |
bool MapObject::set(JSContext* cx, unsigned argc, Value* vp) { |
| 949 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "set"); |
| 950 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 951 |
return CallNonGenericMethod<MapObject::is, MapObject::set_impl>(cx, args); |
| 952 |
} |
| 953 |
|
| 954 |
#ifdef NIGHTLY_BUILD |
| 955 |
bool MapObject::getOrInsert_impl(JSContext* cx, const CallArgs& args) { |
| 956 |
auto* mapObj = &args.thisv().toObject().as<MapObject>(); |
| 957 |
return mapObj->getOrInsert(cx, args.get(0), args.get(1), args.rval()); |
| 958 |
} |
| 959 |
|
| 960 |
bool MapObject::getOrInsert(JSContext* cx, unsigned argc, Value* vp) { |
| 961 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "getOrInsert"); |
| 962 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 963 |
return CallNonGenericMethod<MapObject::is, MapObject::getOrInsert_impl>(cx, |
| 964 |
args); |
| 965 |
} |
| 966 |
#endif // #ifdef NIGHTLY_BUILD |
| 967 |
|
| 968 |
bool MapObject::delete_(JSContext* cx, const Value& key, bool* rval) { |
| 969 |
HashableValue k; |
| 970 |
if (!k.setValue(cx, key)) { |
| 971 |
return false; |
| 972 |
} |
| 973 |
|
| 974 |
if (isTenured()) { |
| 975 |
*rval = Table(this).remove(cx, k); |
| 976 |
} else { |
| 977 |
*rval = PreBarrieredTable(this).remove(cx, k); |
| 978 |
} |
| 979 |
return true; |
| 980 |
} |
| 981 |
|
| 982 |
bool MapObject::delete_impl(JSContext* cx, const CallArgs& args) { |
| 983 |
// MapObject::trace does not trace deleted entries. Incremental GC therefore |
| 984 |
// requires that no HeapPtr<Value> objects pointing to heap values be left |
| 985 |
// alive in the hash table. |
| 986 |
// |
| 987 |
// OrderedHashMapImpl::remove() doesn't destroy the removed entry. It merely |
| 988 |
// calls OrderedHashMapImpl::MapOps::makeEmpty. But that is sufficient, |
| 989 |
// because makeEmpty clears the value by doing e->value = Value(), and in the |
| 990 |
// case of Table, Value() means HeapPtr<Value>(), which is the same as |
| 991 |
// HeapPtr<Value>(UndefinedValue()). |
| 992 |
|
| 993 |
auto* mapObj = &args.thisv().toObject().as<MapObject>(); |
| 994 |
bool found; |
| 995 |
if (!mapObj->delete_(cx, args.get(0), &found)) { |
| 996 |
return false; |
| 997 |
} |
| 998 |
args.rval().setBoolean(found); |
| 999 |
return true; |
| 1000 |
} |
| 1001 |
|
| 1002 |
bool MapObject::delete_(JSContext* cx, unsigned argc, Value* vp) { |
| 1003 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "delete"); |
| 1004 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1005 |
return CallNonGenericMethod<MapObject::is, MapObject::delete_impl>(cx, args); |
| 1006 |
} |
| 1007 |
|
| 1008 |
bool MapObject::iterator(JSContext* cx, IteratorKind kind, |
| 1009 |
Handle<MapObject*> obj, MutableHandleValue iter) { |
| 1010 |
JSObject* iterobj = MapIteratorObject::create(cx, obj, kind); |
| 1011 |
if (!iterobj) { |
| 1012 |
return false; |
| 1013 |
} |
| 1014 |
iter.setObject(*iterobj); |
| 1015 |
return true; |
| 1016 |
} |
| 1017 |
|
| 1018 |
bool MapObject::iterator_impl(JSContext* cx, const CallArgs& args, |
| 1019 |
IteratorKind kind) { |
| 1020 |
Rooted<MapObject*> mapObj(cx, &args.thisv().toObject().as<MapObject>()); |
| 1021 |
return iterator(cx, kind, mapObj, args.rval()); |
| 1022 |
} |
| 1023 |
|
| 1024 |
bool MapObject::keys_impl(JSContext* cx, const CallArgs& args) { |
| 1025 |
return iterator_impl(cx, args, IteratorKind::Keys); |
| 1026 |
} |
| 1027 |
|
| 1028 |
bool MapObject::keys(JSContext* cx, unsigned argc, Value* vp) { |
| 1029 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "keys"); |
| 1030 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1031 |
return CallNonGenericMethod(cx, is, keys_impl, args); |
| 1032 |
} |
| 1033 |
|
| 1034 |
bool MapObject::values_impl(JSContext* cx, const CallArgs& args) { |
| 1035 |
return iterator_impl(cx, args, IteratorKind::Values); |
| 1036 |
} |
| 1037 |
|
| 1038 |
bool MapObject::values(JSContext* cx, unsigned argc, Value* vp) { |
| 1039 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "values"); |
| 1040 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1041 |
return CallNonGenericMethod(cx, is, values_impl, args); |
| 1042 |
} |
| 1043 |
|
| 1044 |
bool MapObject::entries_impl(JSContext* cx, const CallArgs& args) { |
| 1045 |
return iterator_impl(cx, args, IteratorKind::Entries); |
| 1046 |
} |
| 1047 |
|
| 1048 |
bool MapObject::entries(JSContext* cx, unsigned argc, Value* vp) { |
| 1049 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "entries"); |
| 1050 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1051 |
return CallNonGenericMethod(cx, is, entries_impl, args); |
| 1052 |
} |
| 1053 |
|
| 1054 |
bool MapObject::clear_impl(JSContext* cx, const CallArgs& args) { |
| 1055 |
auto* mapObj = &args.thisv().toObject().as<MapObject>(); |
| 1056 |
mapObj->clear(cx); |
| 1057 |
args.rval().setUndefined(); |
| 1058 |
return true; |
| 1059 |
} |
| 1060 |
|
| 1061 |
bool MapObject::clear(JSContext* cx, unsigned argc, Value* vp) { |
| 1062 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Map.prototype", "clear"); |
| 1063 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1064 |
return CallNonGenericMethod(cx, is, clear_impl, args); |
| 1065 |
} |
| 1066 |
|
| 1067 |
void MapObject::clear(JSContext* cx) { |
| 1068 |
if (isTenured()) { |
| 1069 |
Table(this).clear(cx); |
| 1070 |
} else { |
| 1071 |
PreBarrieredTable(this).clear(cx); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
/*** SetIterator ************************************************************/ |
| 1076 |
|
| 1077 |
static const JSClassOps SetIteratorObjectClassOps = { |
| 1078 |
nullptr, // addProperty |
| 1079 |
nullptr, // delProperty |
| 1080 |
nullptr, // enumerate |
| 1081 |
nullptr, // newEnumerate |
| 1082 |
nullptr, // resolve |
| 1083 |
nullptr, // mayResolve |
| 1084 |
SetIteratorObject::finalize, // finalize |
| 1085 |
nullptr, // call |
| 1086 |
nullptr, // construct |
| 1087 |
nullptr, // trace |
| 1088 |
}; |
| 1089 |
|
| 1090 |
static const ClassExtension SetIteratorObjectClassExtension = { |
| 1091 |
SetIteratorObject::objectMoved, // objectMovedOp |
| 1092 |
}; |
| 1093 |
|
| 1094 |
const JSClass SetIteratorObject::class_ = { |
| 1095 |
"Set Iterator", |
| 1096 |
JSCLASS_HAS_RESERVED_SLOTS(SetIteratorObject::SlotCount) | |
| 1097 |
JSCLASS_FOREGROUND_FINALIZE | JSCLASS_SKIP_NURSERY_FINALIZE, |
| 1098 |
&SetIteratorObjectClassOps, |
| 1099 |
JS_NULL_CLASS_SPEC, |
| 1100 |
&SetIteratorObjectClassExtension, |
| 1101 |
}; |
| 1102 |
|
| 1103 |
const JSFunctionSpec SetIteratorObject::methods[] = { |
| 1104 |
JS_SELF_HOSTED_FN("next", "SetIteratorNext", 0, 0), |
| 1105 |
JS_FS_END, |
| 1106 |
}; |
| 1107 |
|
| 1108 |
/* static */ |
| 1109 |
bool GlobalObject::initSetIteratorProto(JSContext* cx, |
| 1110 |
Handle<GlobalObject*> global) { |
| 1111 |
Rooted<JSObject*> base( |
| 1112 |
cx, GlobalObject::getOrCreateIteratorPrototype(cx, global)); |
| 1113 |
if (!base) { |
| 1114 |
return false; |
| 1115 |
} |
| 1116 |
Rooted<PlainObject*> proto( |
| 1117 |
cx, GlobalObject::createBlankPrototypeInheriting<PlainObject>(cx, base)); |
| 1118 |
if (!proto) { |
| 1119 |
return false; |
| 1120 |
} |
| 1121 |
if (!JS_DefineFunctions(cx, proto, SetIteratorObject::methods) || |
| 1122 |
!DefineToStringTag(cx, proto, cx->names().Set_Iterator_)) { |
| 1123 |
return false; |
| 1124 |
} |
| 1125 |
if (!JSObject::setHasFuseProperty(cx, proto)) { |
| 1126 |
return false; |
| 1127 |
} |
| 1128 |
global->initBuiltinProto(ProtoKind::SetIteratorProto, proto); |
| 1129 |
return true; |
| 1130 |
} |
| 1131 |
|
| 1132 |
SetIteratorObject* SetIteratorObject::create(JSContext* cx, |
| 1133 |
Handle<SetObject*> setobj, |
| 1134 |
Kind kind) { |
| 1135 |
MOZ_ASSERT(kind != Kind::Keys); |
| 1136 |
|
| 1137 |
Rooted<GlobalObject*> global(cx, &setobj->global()); |
| 1138 |
Rooted<JSObject*> proto( |
| 1139 |
cx, GlobalObject::getOrCreateSetIteratorPrototype(cx, global)); |
| 1140 |
if (!proto) { |
| 1141 |
return nullptr; |
| 1142 |
} |
| 1143 |
|
| 1144 |
SetIteratorObject* iterobj = |
| 1145 |
NewObjectWithGivenProto<SetIteratorObject>(cx, proto); |
| 1146 |
if (!iterobj) { |
| 1147 |
return nullptr; |
| 1148 |
} |
| 1149 |
|
| 1150 |
if (IsInsideNursery(iterobj) && |
| 1151 |
!HasRegisteredNurseryIterators(setobj.get())) { |
| 1152 |
if (!cx->nursery().addSetWithNurseryIterators(setobj)) { |
| 1153 |
ReportOutOfMemory(cx); |
| 1154 |
return nullptr; |
| 1155 |
} |
| 1156 |
SetRegisteredNurseryIterators(setobj.get(), true); |
| 1157 |
} |
| 1158 |
|
| 1159 |
SetObject::Table(setobj).initIterator(iterobj, kind); |
| 1160 |
|
| 1161 |
return iterobj; |
| 1162 |
} |
| 1163 |
|
| 1164 |
void SetIteratorObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 1165 |
MOZ_ASSERT(gcx->onMainThread()); |
| 1166 |
MOZ_ASSERT(!IsInsideNursery(obj)); |
| 1167 |
if (obj->as<SetIteratorObject>().isActive()) { |
| 1168 |
obj->as<SetIteratorObject>().unlink(); |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
size_t SetIteratorObject::objectMoved(JSObject* obj, JSObject* old) { |
| 1173 |
SetIteratorObject* iter = &obj->as<SetIteratorObject>(); |
| 1174 |
if (!iter->isActive()) { |
| 1175 |
return 0; |
| 1176 |
} |
| 1177 |
if (IsInsideNursery(old)) { |
| 1178 |
SetObject* setObj = iter->target(); |
| 1179 |
SetObject::Table(setObj).relinkNurseryIterator(iter); |
| 1180 |
} else { |
| 1181 |
iter->updateListAfterMove(&old->as<SetIteratorObject>()); |
| 1182 |
} |
| 1183 |
return 0; |
| 1184 |
} |
| 1185 |
|
| 1186 |
SetObject* SetIteratorObject::target() const { |
| 1187 |
MOZ_ASSERT(isActive(), "only active iterators have a target object"); |
| 1188 |
Value value = getFixedSlot(TargetSlot); |
| 1189 |
return &MaybeForwarded(&value.toObject())->as<SetObject>(); |
| 1190 |
} |
| 1191 |
|
| 1192 |
bool SetIteratorObject::next(SetIteratorObject* setIterator, |
| 1193 |
ArrayObject* resultObj) { |
| 1194 |
// IC code calls this directly. |
| 1195 |
AutoUnsafeCallWithABI unsafe; |
| 1196 |
|
| 1197 |
// Check invariants for inlined _GetNextSetEntryForIterator. |
| 1198 |
|
| 1199 |
// The array should be tenured, so that post-barrier can be done simply. |
| 1200 |
MOZ_ASSERT(resultObj->isTenured()); |
| 1201 |
|
| 1202 |
// The array elements should be fixed. |
| 1203 |
MOZ_ASSERT(resultObj->hasFixedElements()); |
| 1204 |
MOZ_ASSERT(resultObj->getDenseInitializedLength() == 1); |
| 1205 |
MOZ_ASSERT(resultObj->getDenseCapacity() >= 1); |
| 1206 |
|
| 1207 |
if (!setIterator->isActive()) { |
| 1208 |
// Already done. |
| 1209 |
return true; |
| 1210 |
} |
| 1211 |
|
| 1212 |
auto storeResult = [resultObj](Kind kind, const auto& element) { |
| 1213 |
resultObj->setDenseElement(0, element.get()); |
| 1214 |
}; |
| 1215 |
SetObject* setObj = setIterator->target(); |
| 1216 |
return SetObject::Table(setObj).iteratorNext(setIterator, storeResult); |
| 1217 |
} |
| 1218 |
|
| 1219 |
/* static */ |
| 1220 |
JSObject* SetIteratorObject::createResult(JSContext* cx) { |
| 1221 |
Rooted<ArrayObject*> resultObj( |
| 1222 |
cx, NewDenseFullyAllocatedArray(cx, 1, TenuredObject)); |
| 1223 |
if (!resultObj) { |
| 1224 |
return nullptr; |
| 1225 |
} |
| 1226 |
|
| 1227 |
resultObj->setDenseInitializedLength(1); |
| 1228 |
resultObj->initDenseElement(0, NullValue()); |
| 1229 |
|
| 1230 |
return resultObj; |
| 1231 |
} |
| 1232 |
|
| 1233 |
/*** Set ********************************************************************/ |
| 1234 |
|
| 1235 |
const JSClassOps SetObject::classOps_ = { |
| 1236 |
nullptr, // addProperty |
| 1237 |
nullptr, // delProperty |
| 1238 |
nullptr, // enumerate |
| 1239 |
nullptr, // newEnumerate |
| 1240 |
nullptr, // resolve |
| 1241 |
nullptr, // mayResolve |
| 1242 |
finalize, // finalize |
| 1243 |
nullptr, // call |
| 1244 |
nullptr, // construct |
| 1245 |
trace, // trace |
| 1246 |
}; |
| 1247 |
|
| 1248 |
const ClassSpec SetObject::classSpec_ = { |
| 1249 |
GenericCreateConstructor<SetObject::construct, 0, gc::AllocKind::FUNCTION, |
| 1250 |
&jit::JitInfo_SetConstructor>, |
| 1251 |
GenericCreatePrototype<SetObject>, |
| 1252 |
nullptr, |
| 1253 |
SetObject::staticProperties, |
| 1254 |
SetObject::methods, |
| 1255 |
SetObject::properties, |
| 1256 |
SetObject::finishInit, |
| 1257 |
}; |
| 1258 |
|
| 1259 |
const ClassExtension SetObject::classExtension_ = { |
| 1260 |
SetObject::objectMoved, // objectMovedOp |
| 1261 |
}; |
| 1262 |
|
| 1263 |
const JSClass SetObject::class_ = { |
| 1264 |
"Set", |
| 1265 |
JSCLASS_DELAY_METADATA_BUILDER | |
| 1266 |
JSCLASS_HAS_RESERVED_SLOTS(SetObject::SlotCount) | |
| 1267 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Set) | JSCLASS_BACKGROUND_FINALIZE | |
| 1268 |
JSCLASS_SKIP_NURSERY_FINALIZE, |
| 1269 |
&SetObject::classOps_, &SetObject::classSpec_, &SetObject::classExtension_}; |
| 1270 |
|
| 1271 |
const JSClass SetObject::protoClass_ = { |
| 1272 |
"Set.prototype", |
| 1273 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Set), |
| 1274 |
JS_NULL_CLASS_OPS, |
| 1275 |
&SetObject::classSpec_, |
| 1276 |
}; |
| 1277 |
|
| 1278 |
const JSPropertySpec SetObject::properties[] = { |
| 1279 |
JS_PSG("size", size, 0), |
| 1280 |
JS_STRING_SYM_PS(toStringTag, "Set", JSPROP_READONLY), |
| 1281 |
JS_PS_END, |
| 1282 |
}; |
| 1283 |
|
| 1284 |
const JSFunctionSpec SetObject::methods[] = { |
| 1285 |
JS_INLINABLE_FN("has", has, 1, 0, SetHas), |
| 1286 |
JS_INLINABLE_FN("add", add, 1, 0, SetAdd), |
| 1287 |
JS_INLINABLE_FN("delete", delete_, 1, 0, SetDelete), |
| 1288 |
JS_FN("entries", entries, 0, 0), |
| 1289 |
JS_FN("clear", clear, 0, 0), |
| 1290 |
JS_SELF_HOSTED_FN("forEach", "SetForEach", 2, 0), |
| 1291 |
JS_SELF_HOSTED_FN("union", "SetUnion", 1, 0), |
| 1292 |
JS_SELF_HOSTED_FN("difference", "SetDifference", 1, 0), |
| 1293 |
JS_SELF_HOSTED_FN("intersection", "SetIntersection", 1, 0), |
| 1294 |
JS_SELF_HOSTED_FN("symmetricDifference", "SetSymmetricDifference", 1, 0), |
| 1295 |
JS_SELF_HOSTED_FN("isSubsetOf", "SetIsSubsetOf", 1, 0), |
| 1296 |
JS_SELF_HOSTED_FN("isSupersetOf", "SetIsSupersetOf", 1, 0), |
| 1297 |
JS_SELF_HOSTED_FN("isDisjointFrom", "SetIsDisjointFrom", 1, 0), |
| 1298 |
JS_FN("values", values, 0, 0), |
| 1299 |
// @@iterator and |keys| re-defined in finishInit so that they have the |
| 1300 |
// same identity as |values|. |
| 1301 |
JS_FN("keys", values, 0, 0), |
| 1302 |
JS_SYM_FN(iterator, values, 0, 0), |
| 1303 |
JS_FS_END, |
| 1304 |
}; |
| 1305 |
// clang-format on |
| 1306 |
|
| 1307 |
const JSPropertySpec SetObject::staticProperties[] = { |
| 1308 |
JS_SELF_HOSTED_SYM_GET(species, "$SetSpecies", 0), |
| 1309 |
JS_PS_END, |
| 1310 |
}; |
| 1311 |
|
| 1312 |
/* static */ bool SetObject::finishInit(JSContext* cx, HandleObject ctor, |
| 1313 |
HandleObject proto) { |
| 1314 |
Handle<NativeObject*> nativeProto = proto.as<NativeObject>(); |
| 1315 |
|
| 1316 |
RootedValue valuesFn(cx); |
| 1317 |
RootedId valuesId(cx, NameToId(cx->names().values)); |
| 1318 |
if (!NativeGetProperty(cx, nativeProto, valuesId, &valuesFn)) { |
| 1319 |
return false; |
| 1320 |
} |
| 1321 |
|
| 1322 |
// 23.2.3.8 Set.prototype.keys() |
| 1323 |
// The initial value of the "keys" property is the same function object |
| 1324 |
// as the initial value of the "values" property. |
| 1325 |
RootedId keysId(cx, NameToId(cx->names().keys)); |
| 1326 |
if (!NativeDefineDataProperty(cx, nativeProto, keysId, valuesFn, 0)) { |
| 1327 |
return false; |
| 1328 |
} |
| 1329 |
|
| 1330 |
// 23.2.3.11 Set.prototype[@@iterator]() |
| 1331 |
// See above. |
| 1332 |
RootedId iteratorId(cx, PropertyKey::Symbol(cx->wellKnownSymbols().iterator)); |
| 1333 |
if (!NativeDefineDataProperty(cx, nativeProto, iteratorId, valuesFn, 0)) { |
| 1334 |
return false; |
| 1335 |
} |
| 1336 |
|
| 1337 |
return JSObject::setHasFuseProperty(cx, nativeProto); |
| 1338 |
} |
| 1339 |
|
| 1340 |
bool SetObject::keys(JS::MutableHandle<GCVector<JS::Value>> keys) { |
| 1341 |
auto appendEntry = [&keys](auto& entry) { return keys.append(entry.get()); }; |
| 1342 |
return Table(this).forEachEntry(appendEntry); |
| 1343 |
} |
| 1344 |
|
| 1345 |
bool SetObject::add(JSContext* cx, const Value& key) { |
| 1346 |
HashableValue k; |
| 1347 |
if (!k.setValue(cx, key)) { |
| 1348 |
return false; |
| 1349 |
} |
| 1350 |
return addHashableValue(cx, k); |
| 1351 |
} |
| 1352 |
|
| 1353 |
bool SetObject::addHashableValue(JSContext* cx, const HashableValue& value) { |
| 1354 |
bool needsPostBarriers = isTenured(); |
| 1355 |
if (needsPostBarriers && !PostWriteBarrier(this, value)) { |
| 1356 |
ReportOutOfMemory(cx); |
| 1357 |
return false; |
| 1358 |
} |
| 1359 |
return Table(this).put(cx, value); |
| 1360 |
} |
| 1361 |
|
| 1362 |
SetObject* SetObject::createWithProto(JSContext* cx, HandleObject proto, |
| 1363 |
NewObjectKind newKind) { |
| 1364 |
MOZ_ASSERT(proto); |
| 1365 |
|
| 1366 |
gc::AllocKind allocKind = gc::GetGCObjectKind(SlotCount); |
| 1367 |
|
| 1368 |
AutoSetNewObjectMetadata metadata(cx); |
| 1369 |
auto* setObj = |
| 1370 |
NewObjectWithGivenProtoAndKinds<SetObject>(cx, proto, allocKind, newKind); |
| 1371 |
if (!setObj) { |
| 1372 |
return nullptr; |
| 1373 |
} |
| 1374 |
|
| 1375 |
UnbarrieredTable(setObj).initSlots(); |
| 1376 |
setObj->initReservedSlot(NurseryKeysSlot, PrivateValue(nullptr)); |
| 1377 |
setObj->initReservedSlot(RegisteredNurseryIteratorsSlot, BooleanValue(false)); |
| 1378 |
return setObj; |
| 1379 |
} |
| 1380 |
|
| 1381 |
SetObject* SetObject::create(JSContext* cx, |
| 1382 |
HandleObject proto /* = nullptr */) { |
| 1383 |
if (proto) { |
| 1384 |
return createWithProto(cx, proto, GenericObject); |
| 1385 |
} |
| 1386 |
|
| 1387 |
// This is the common case so use the template object's shape to optimize the |
| 1388 |
// allocation. |
| 1389 |
SetObject* templateObj = GlobalObject::getOrCreateSetTemplateObject(cx); |
| 1390 |
if (!templateObj) { |
| 1391 |
return nullptr; |
| 1392 |
} |
| 1393 |
|
| 1394 |
gc::AllocKind allocKind = templateObj->asTenured().getAllocKind(); |
| 1395 |
MOZ_ASSERT(gc::GetGCKindSlots(allocKind) >= SlotCount); |
| 1396 |
MOZ_ASSERT(gc::IsBackgroundFinalized(allocKind)); |
| 1397 |
|
| 1398 |
AutoSetNewObjectMetadata metadata(cx); |
| 1399 |
Rooted<SharedShape*> shape(cx, templateObj->sharedShape()); |
| 1400 |
auto* setObj = |
| 1401 |
NativeObject::create<SetObject>(cx, allocKind, gc::Heap::Default, shape); |
| 1402 |
if (!setObj) { |
| 1403 |
return nullptr; |
| 1404 |
} |
| 1405 |
|
| 1406 |
UnbarrieredTable(setObj).initSlots(); |
| 1407 |
setObj->initReservedSlot(NurseryKeysSlot, PrivateValue(nullptr)); |
| 1408 |
setObj->initReservedSlot(RegisteredNurseryIteratorsSlot, BooleanValue(false)); |
| 1409 |
return setObj; |
| 1410 |
} |
| 1411 |
|
| 1412 |
// static |
| 1413 |
SetObject* GlobalObject::getOrCreateSetTemplateObject(JSContext* cx) { |
| 1414 |
GlobalObjectData& data = cx->global()->data(); |
| 1415 |
if (SetObject* obj = data.setObjectTemplate) { |
| 1416 |
return obj; |
| 1417 |
} |
| 1418 |
|
| 1419 |
Rooted<JSObject*> proto(cx, |
| 1420 |
GlobalObject::getOrCreatePrototype(cx, JSProto_Set)); |
| 1421 |
if (!proto) { |
| 1422 |
return nullptr; |
| 1423 |
} |
| 1424 |
auto* setObj = SetObject::createWithProto(cx, proto, TenuredObject); |
| 1425 |
if (!setObj) { |
| 1426 |
return nullptr; |
| 1427 |
} |
| 1428 |
|
| 1429 |
data.setObjectTemplate.init(setObj); |
| 1430 |
return setObj; |
| 1431 |
} |
| 1432 |
|
| 1433 |
void SetObject::trace(JSTracer* trc, JSObject* obj) { |
| 1434 |
SetObject* setobj = static_cast<SetObject*>(obj); |
| 1435 |
Table(setobj).trace(trc); |
| 1436 |
} |
| 1437 |
|
| 1438 |
size_t SetObject::sizeOfData(mozilla::MallocSizeOf mallocSizeOf) { |
| 1439 |
size_t size = 0; |
| 1440 |
size += Table(this).sizeOfExcludingObject(mallocSizeOf); |
| 1441 |
if (NurseryKeysVector* nurseryKeys = GetNurseryKeys(this)) { |
| 1442 |
size += nurseryKeys->sizeOfIncludingThis(mallocSizeOf); |
| 1443 |
} |
| 1444 |
return size; |
| 1445 |
} |
| 1446 |
|
| 1447 |
void SetObject::finalize(JS::GCContext* gcx, JSObject* obj) { |
| 1448 |
SetObject* setObj = &obj->as<SetObject>(); |
| 1449 |
MOZ_ASSERT(!IsInsideNursery(setObj)); |
| 1450 |
MOZ_ASSERT(!UnbarrieredTable(setObj).hasNurseryIterators()); |
| 1451 |
|
| 1452 |
#ifdef DEBUG |
| 1453 |
// If we're finalizing a tenured set then it cannot contain nursery things, |
| 1454 |
// because we evicted the nursery at the start of collection and writing a |
| 1455 |
// nursery thing into the set would require it to be live, which means it |
| 1456 |
// would have been marked. |
| 1457 |
UnbarrieredTable(setObj).forEachEntryUpTo(1000, [](auto& entry) { |
| 1458 |
Value key = entry; |
| 1459 |
MOZ_ASSERT_IF(key.isGCThing(), !IsInsideNursery(key.toGCThing())); |
| 1460 |
}); |
| 1461 |
#endif |
| 1462 |
|
| 1463 |
// Finalized tenured sets do not contain nursery GC things, so do not require |
| 1464 |
// post barriers. Pre barriers are not required for finalization. |
| 1465 |
UnbarrieredTable(setObj).destroy(gcx); |
| 1466 |
} |
| 1467 |
|
| 1468 |
size_t SetObject::objectMoved(JSObject* obj, JSObject* old) { |
| 1469 |
auto* setObj = &obj->as<SetObject>(); |
| 1470 |
|
| 1471 |
Table(setObj).updateIteratorsAfterMove(&old->as<SetObject>()); |
| 1472 |
|
| 1473 |
if (IsInsideNursery(old)) { |
| 1474 |
Nursery& nursery = setObj->runtimeFromMainThread()->gc.nursery(); |
| 1475 |
Table(setObj).maybeMoveBufferOnPromotion(nursery); |
| 1476 |
} |
| 1477 |
|
| 1478 |
return 0; |
| 1479 |
} |
| 1480 |
|
| 1481 |
void SetObject::clearNurseryIteratorsBeforeMinorGC() { |
| 1482 |
Table(this).clearNurseryIterators(); |
| 1483 |
} |
| 1484 |
|
| 1485 |
/* static */ |
| 1486 |
SetObject* SetObject::sweepAfterMinorGC(JS::GCContext* gcx, SetObject* setobj) { |
| 1487 |
Nursery& nursery = gcx->runtime()->gc.nursery(); |
| 1488 |
bool wasInCollectedRegion = nursery.inCollectedRegion(setobj); |
| 1489 |
if (wasInCollectedRegion && !IsForwarded(setobj)) { |
| 1490 |
// This SetObject is dead. |
| 1491 |
return nullptr; |
| 1492 |
} |
| 1493 |
|
| 1494 |
setobj = MaybeForwarded(setobj); |
| 1495 |
|
| 1496 |
// Keep |setobj| registered with the nursery if it still has nursery |
| 1497 |
// iterators. |
| 1498 |
bool hasNurseryIterators = Table(setobj).hasNurseryIterators(); |
| 1499 |
SetRegisteredNurseryIterators(setobj, hasNurseryIterators); |
| 1500 |
return hasNurseryIterators ? setobj : nullptr; |
| 1501 |
} |
| 1502 |
|
| 1503 |
bool SetObject::tryOptimizeCtorWithIterable(JSContext* cx, |
| 1504 |
const Value& iterableVal, |
| 1505 |
bool* optimized) { |
| 1506 |
MOZ_ASSERT(!iterableVal.isNullOrUndefined()); |
| 1507 |
MOZ_ASSERT(!*optimized); |
| 1508 |
|
| 1509 |
if (!CanOptimizeMapOrSetCtorWithIterable<JSProto_Set>(SetObject::add, this, |
| 1510 |
cx)) { |
| 1511 |
return true; |
| 1512 |
} |
| 1513 |
|
| 1514 |
if (!iterableVal.isObject()) { |
| 1515 |
return true; |
| 1516 |
} |
| 1517 |
JSObject* iterable = &iterableVal.toObject(); |
| 1518 |
|
| 1519 |
// Fast path for `new Set(array)`. |
| 1520 |
if (IsOptimizableArrayForMapOrSetCtor<MapOrSet::Set>(iterable, cx)) { |
| 1521 |
ArrayObject* array = &iterable->as<ArrayObject>(); |
| 1522 |
uint32_t len = array->getDenseInitializedLength(); |
| 1523 |
|
| 1524 |
for (uint32_t index = 0; index < len; index++) { |
| 1525 |
Value keyVal = array->getDenseElement(index); |
| 1526 |
MOZ_ASSERT(!keyVal.isMagic(JS_ELEMENTS_HOLE)); |
| 1527 |
if (!add(cx, keyVal)) { |
| 1528 |
return false; |
| 1529 |
} |
| 1530 |
} |
| 1531 |
|
| 1532 |
*optimized = true; |
| 1533 |
return true; |
| 1534 |
} |
| 1535 |
|
| 1536 |
// Fast path for `new Set(set)`. |
| 1537 |
if (IsSetObjectWithDefaultIterator(iterable, cx)) { |
| 1538 |
auto* iterableSet = &iterable->as<SetObject>(); |
| 1539 |
if (!IsSetObjectWithDefaultIterator(iterableSet, cx)) { |
| 1540 |
return true; |
| 1541 |
} |
| 1542 |
auto addEntry = [cx, this](auto& entry) { |
| 1543 |
return addHashableValue(cx, entry); |
| 1544 |
}; |
| 1545 |
if (!Table(iterableSet).forEachEntry(addEntry)) { |
| 1546 |
return false; |
| 1547 |
} |
| 1548 |
*optimized = true; |
| 1549 |
return true; |
| 1550 |
} |
| 1551 |
|
| 1552 |
return true; |
| 1553 |
} |
| 1554 |
|
| 1555 |
// static |
| 1556 |
SetObject* SetObject::createFromIterable(JSContext* cx, Handle<JSObject*> proto, |
| 1557 |
Handle<Value> iterable, |
| 1558 |
Handle<SetObject*> allocatedFromJit) { |
| 1559 |
// A null-or-undefined |iterable| is quite common and we check for this in JIT |
| 1560 |
// code. |
| 1561 |
MOZ_ASSERT_IF(allocatedFromJit, !iterable.isNullOrUndefined()); |
| 1562 |
|
| 1563 |
Rooted<SetObject*> obj(cx, allocatedFromJit); |
| 1564 |
if (!obj) { |
| 1565 |
obj = SetObject::create(cx, proto); |
| 1566 |
if (!obj) { |
| 1567 |
return nullptr; |
| 1568 |
} |
| 1569 |
} |
| 1570 |
|
| 1571 |
if (!iterable.isNullOrUndefined()) { |
| 1572 |
bool optimized = false; |
| 1573 |
if (!obj->tryOptimizeCtorWithIterable(cx, iterable, &optimized)) { |
| 1574 |
return nullptr; |
| 1575 |
} |
| 1576 |
if (!optimized) { |
| 1577 |
FixedInvokeArgs<1> args(cx); |
| 1578 |
args[0].set(iterable); |
| 1579 |
|
| 1580 |
RootedValue thisv(cx, ObjectValue(*obj)); |
| 1581 |
if (!CallSelfHostedFunction(cx, cx->names().SetConstructorInit, thisv, |
| 1582 |
args, args.rval())) { |
| 1583 |
return nullptr; |
| 1584 |
} |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
return obj; |
| 1589 |
} |
| 1590 |
|
| 1591 |
bool SetObject::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 1592 |
AutoJSConstructorProfilerEntry pseudoFrame(cx, "Set"); |
| 1593 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1594 |
|
| 1595 |
if (!ThrowIfNotConstructing(cx, args, "Set")) { |
| 1596 |
return false; |
| 1597 |
} |
| 1598 |
|
| 1599 |
RootedObject proto(cx); |
| 1600 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_Set, &proto)) { |
| 1601 |
return false; |
| 1602 |
} |
| 1603 |
|
| 1604 |
SetObject* obj = SetObject::createFromIterable(cx, proto, args.get(0)); |
| 1605 |
if (!obj) { |
| 1606 |
return false; |
| 1607 |
} |
| 1608 |
|
| 1609 |
args.rval().setObject(*obj); |
| 1610 |
return true; |
| 1611 |
} |
| 1612 |
|
| 1613 |
bool SetObject::is(HandleValue v) { |
| 1614 |
return v.isObject() && v.toObject().hasClass(&class_); |
| 1615 |
} |
| 1616 |
|
| 1617 |
bool SetObject::is(HandleObject o) { return o->hasClass(&class_); } |
| 1618 |
|
| 1619 |
uint32_t SetObject::size() { |
| 1620 |
static_assert(sizeof(Table(this).count()) <= sizeof(uint32_t), |
| 1621 |
"set count must be precisely representable as a JS number"); |
| 1622 |
return Table(this).count(); |
| 1623 |
} |
| 1624 |
|
| 1625 |
bool SetObject::size_impl(JSContext* cx, const CallArgs& args) { |
| 1626 |
auto* setObj = &args.thisv().toObject().as<SetObject>(); |
| 1627 |
args.rval().setNumber(setObj->size()); |
| 1628 |
return true; |
| 1629 |
} |
| 1630 |
|
| 1631 |
bool SetObject::size(JSContext* cx, unsigned argc, Value* vp) { |
| 1632 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Set.prototype", "size"); |
| 1633 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1634 |
return CallNonGenericMethod<SetObject::is, SetObject::size_impl>(cx, args); |
| 1635 |
} |
| 1636 |
|
| 1637 |
bool SetObject::has_impl(JSContext* cx, const CallArgs& args) { |
| 1638 |
auto* setObj = &args.thisv().toObject().as<SetObject>(); |
| 1639 |
bool found; |
| 1640 |
if (!setObj->has(cx, args.get(0), &found)) { |
| 1641 |
return false; |
| 1642 |
} |
| 1643 |
args.rval().setBoolean(found); |
| 1644 |
return true; |
| 1645 |
} |
| 1646 |
|
| 1647 |
bool SetObject::has(JSContext* cx, const Value& key, bool* rval) { |
| 1648 |
HashableValue k; |
| 1649 |
if (!k.setValue(cx, key)) { |
| 1650 |
return false; |
| 1651 |
} |
| 1652 |
|
| 1653 |
*rval = Table(this).has(k); |
| 1654 |
return true; |
| 1655 |
} |
| 1656 |
|
| 1657 |
bool SetObject::has(JSContext* cx, unsigned argc, Value* vp) { |
| 1658 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Set.prototype", "has"); |
| 1659 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1660 |
return CallNonGenericMethod<SetObject::is, SetObject::has_impl>(cx, args); |
| 1661 |
} |
| 1662 |
|
| 1663 |
bool SetObject::add_impl(JSContext* cx, const CallArgs& args) { |
| 1664 |
auto* setObj = &args.thisv().toObject().as<SetObject>(); |
| 1665 |
if (!setObj->add(cx, args.get(0))) { |
| 1666 |
return false; |
| 1667 |
} |
| 1668 |
args.rval().set(args.thisv()); |
| 1669 |
return true; |
| 1670 |
} |
| 1671 |
|
| 1672 |
bool SetObject::add(JSContext* cx, unsigned argc, Value* vp) { |
| 1673 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Set.prototype", "add"); |
| 1674 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1675 |
return CallNonGenericMethod<SetObject::is, SetObject::add_impl>(cx, args); |
| 1676 |
} |
| 1677 |
|
| 1678 |
bool SetObject::delete_(JSContext* cx, const Value& key, bool* rval) { |
| 1679 |
HashableValue k; |
| 1680 |
if (!k.setValue(cx, key)) { |
| 1681 |
return false; |
| 1682 |
} |
| 1683 |
|
| 1684 |
*rval = Table(this).remove(cx, k); |
| 1685 |
return true; |
| 1686 |
} |
| 1687 |
|
| 1688 |
bool SetObject::delete_impl(JSContext* cx, const CallArgs& args) { |
| 1689 |
auto* setObj = &args.thisv().toObject().as<SetObject>(); |
| 1690 |
bool found; |
| 1691 |
if (!setObj->delete_(cx, args.get(0), &found)) { |
| 1692 |
return false; |
| 1693 |
} |
| 1694 |
args.rval().setBoolean(found); |
| 1695 |
return true; |
| 1696 |
} |
| 1697 |
|
| 1698 |
bool SetObject::delete_(JSContext* cx, unsigned argc, Value* vp) { |
| 1699 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Set.prototype", "delete"); |
| 1700 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1701 |
return CallNonGenericMethod<SetObject::is, SetObject::delete_impl>(cx, args); |
| 1702 |
} |
| 1703 |
|
| 1704 |
bool SetObject::iterator(JSContext* cx, IteratorKind kind, |
| 1705 |
Handle<SetObject*> obj, MutableHandleValue iter) { |
| 1706 |
JSObject* iterobj = SetIteratorObject::create(cx, obj, kind); |
| 1707 |
if (!iterobj) { |
| 1708 |
return false; |
| 1709 |
} |
| 1710 |
iter.setObject(*iterobj); |
| 1711 |
return true; |
| 1712 |
} |
| 1713 |
|
| 1714 |
bool SetObject::iterator_impl(JSContext* cx, const CallArgs& args, |
| 1715 |
IteratorKind kind) { |
| 1716 |
Rooted<SetObject*> setObj(cx, &args.thisv().toObject().as<SetObject>()); |
| 1717 |
return iterator(cx, kind, setObj, args.rval()); |
| 1718 |
} |
| 1719 |
|
| 1720 |
bool SetObject::values_impl(JSContext* cx, const CallArgs& args) { |
| 1721 |
return iterator_impl(cx, args, IteratorKind::Values); |
| 1722 |
} |
| 1723 |
|
| 1724 |
bool SetObject::values(JSContext* cx, unsigned argc, Value* vp) { |
| 1725 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Set.prototype", "values"); |
| 1726 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1727 |
return CallNonGenericMethod(cx, is, values_impl, args); |
| 1728 |
} |
| 1729 |
|
| 1730 |
bool SetObject::entries_impl(JSContext* cx, const CallArgs& args) { |
| 1731 |
return iterator_impl(cx, args, IteratorKind::Entries); |
| 1732 |
} |
| 1733 |
|
| 1734 |
bool SetObject::entries(JSContext* cx, unsigned argc, Value* vp) { |
| 1735 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Set.prototype", "entries"); |
| 1736 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1737 |
return CallNonGenericMethod(cx, is, entries_impl, args); |
| 1738 |
} |
| 1739 |
|
| 1740 |
void SetObject::clear(JSContext* cx) { Table(this).clear(cx); } |
| 1741 |
|
| 1742 |
bool SetObject::clear_impl(JSContext* cx, const CallArgs& args) { |
| 1743 |
auto* setObj = &args.thisv().toObject().as<SetObject>(); |
| 1744 |
setObj->clear(cx); |
| 1745 |
args.rval().setUndefined(); |
| 1746 |
return true; |
| 1747 |
} |
| 1748 |
|
| 1749 |
bool SetObject::clear(JSContext* cx, unsigned argc, Value* vp) { |
| 1750 |
AutoJSMethodProfilerEntry pseudoFrame(cx, "Set.prototype", "clear"); |
| 1751 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1752 |
return CallNonGenericMethod(cx, is, clear_impl, args); |
| 1753 |
} |
| 1754 |
|
| 1755 |
bool SetObject::copy(JSContext* cx, unsigned argc, Value* vp) { |
| 1756 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1757 |
MOZ_ASSERT(args.length() == 1); |
| 1758 |
MOZ_ASSERT(SetObject::is(args[0])); |
| 1759 |
|
| 1760 |
auto* result = SetObject::create(cx); |
| 1761 |
if (!result) { |
| 1762 |
return false; |
| 1763 |
} |
| 1764 |
|
| 1765 |
auto* from = &args[0].toObject().as<SetObject>(); |
| 1766 |
|
| 1767 |
auto addToResult = [cx, result](auto& entry) { |
| 1768 |
return result->addHashableValue(cx, entry); |
| 1769 |
}; |
| 1770 |
if (!Table(from).forEachEntry(addToResult)) { |
| 1771 |
return false; |
| 1772 |
} |
| 1773 |
|
| 1774 |
args.rval().setObject(*result); |
| 1775 |
return true; |
| 1776 |
} |
| 1777 |
|
| 1778 |
/*** JS static utility functions ********************************************/ |
| 1779 |
|
| 1780 |
static bool forEach(const char* funcName, JSContext* cx, HandleObject obj, |
| 1781 |
HandleValue callbackFn, HandleValue thisArg) { |
| 1782 |
CHECK_THREAD(cx); |
| 1783 |
|
| 1784 |
RootedId forEachId(cx, NameToId(cx->names().forEach)); |
| 1785 |
RootedFunction forEachFunc( |
| 1786 |
cx, JS::GetSelfHostedFunction(cx, funcName, forEachId, 2)); |
| 1787 |
if (!forEachFunc) { |
| 1788 |
return false; |
| 1789 |
} |
| 1790 |
|
| 1791 |
RootedValue fval(cx, ObjectValue(*forEachFunc)); |
| 1792 |
return Call(cx, fval, obj, callbackFn, thisArg, &fval); |
| 1793 |
} |
| 1794 |
|
| 1795 |
// RAII class that unwraps a wrapped Map or Set object and then enters its |
| 1796 |
// realm. |
| 1797 |
template <typename TableObject> |
| 1798 |
class MOZ_RAII AutoEnterTableRealm { |
| 1799 |
mozilla::Maybe<AutoRealm> ar_; |
| 1800 |
Rooted<TableObject*> unwrapped_; |
| 1801 |
|
| 1802 |
public: |
| 1803 |
AutoEnterTableRealm(JSContext* cx, JSObject* obj) : unwrapped_(cx) { |
| 1804 |
JSObject* unwrapped = UncheckedUnwrap(obj); |
| 1805 |
MOZ_ASSERT(unwrapped != obj); |
| 1806 |
MOZ_RELEASE_ASSERT(unwrapped->is<TableObject>()); |
| 1807 |
unwrapped_ = &unwrapped->as<TableObject>(); |
| 1808 |
ar_.emplace(cx, unwrapped_); |
| 1809 |
} |
| 1810 |
Handle<TableObject*> unwrapped() const { return unwrapped_; } |
| 1811 |
}; |
| 1812 |
|
| 1813 |
/*** JS public APIs *********************************************************/ |
| 1814 |
|
| 1815 |
JS_PUBLIC_API JSObject* JS::NewMapObject(JSContext* cx) { |
| 1816 |
return MapObject::create(cx); |
| 1817 |
} |
| 1818 |
|
| 1819 |
JS_PUBLIC_API uint32_t JS::MapSize(JSContext* cx, HandleObject obj) { |
| 1820 |
CHECK_THREAD(cx); |
| 1821 |
cx->check(obj); |
| 1822 |
|
| 1823 |
if (obj->is<MapObject>()) { |
| 1824 |
return obj.as<MapObject>()->size(); |
| 1825 |
} |
| 1826 |
|
| 1827 |
AutoEnterTableRealm<MapObject> enter(cx, obj); |
| 1828 |
return enter.unwrapped()->size(); |
| 1829 |
} |
| 1830 |
|
| 1831 |
JS_PUBLIC_API bool JS::MapGet(JSContext* cx, HandleObject obj, HandleValue key, |
| 1832 |
MutableHandleValue rval) { |
| 1833 |
CHECK_THREAD(cx); |
| 1834 |
cx->check(obj, key, rval); |
| 1835 |
|
| 1836 |
if (obj->is<MapObject>()) { |
| 1837 |
return obj.as<MapObject>()->get(cx, key, rval); |
| 1838 |
} |
| 1839 |
|
| 1840 |
{ |
| 1841 |
AutoEnterTableRealm<MapObject> enter(cx, obj); |
| 1842 |
Rooted<Value> wrappedKey(cx, key); |
| 1843 |
if (!JS_WrapValue(cx, &wrappedKey)) { |
| 1844 |
return false; |
| 1845 |
} |
| 1846 |
if (!enter.unwrapped()->get(cx, wrappedKey, rval)) { |
| 1847 |
return false; |
| 1848 |
} |
| 1849 |
} |
| 1850 |
return JS_WrapValue(cx, rval); |
| 1851 |
} |
| 1852 |
|
| 1853 |
JS_PUBLIC_API bool JS::MapSet(JSContext* cx, HandleObject obj, HandleValue key, |
| 1854 |
HandleValue val) { |
| 1855 |
CHECK_THREAD(cx); |
| 1856 |
cx->check(obj, key, val); |
| 1857 |
|
| 1858 |
if (obj->is<MapObject>()) { |
| 1859 |
return obj.as<MapObject>()->set(cx, key, val); |
| 1860 |
} |
| 1861 |
|
| 1862 |
AutoEnterTableRealm<MapObject> enter(cx, obj); |
| 1863 |
Rooted<Value> wrappedKey(cx, key); |
| 1864 |
Rooted<Value> wrappedValue(cx, val); |
| 1865 |
if (!JS_WrapValue(cx, &wrappedKey) || !JS_WrapValue(cx, &wrappedValue)) { |
| 1866 |
return false; |
| 1867 |
} |
| 1868 |
return enter.unwrapped()->set(cx, wrappedKey, wrappedValue); |
| 1869 |
} |
| 1870 |
|
| 1871 |
JS_PUBLIC_API bool JS::MapHas(JSContext* cx, HandleObject obj, HandleValue key, |
| 1872 |
bool* rval) { |
| 1873 |
CHECK_THREAD(cx); |
| 1874 |
cx->check(obj, key); |
| 1875 |
|
| 1876 |
if (obj->is<MapObject>()) { |
| 1877 |
return obj.as<MapObject>()->has(cx, key, rval); |
| 1878 |
} |
| 1879 |
|
| 1880 |
AutoEnterTableRealm<MapObject> enter(cx, obj); |
| 1881 |
Rooted<Value> wrappedKey(cx, key); |
| 1882 |
if (!JS_WrapValue(cx, &wrappedKey)) { |
| 1883 |
return false; |
| 1884 |
} |
| 1885 |
return enter.unwrapped()->has(cx, wrappedKey, rval); |
| 1886 |
} |
| 1887 |
|
| 1888 |
#ifdef NIGHTLY_BUILD |
| 1889 |
JS_PUBLIC_API bool JS::MapGetOrInsert(JSContext* cx, HandleObject obj, |
| 1890 |
HandleValue key, HandleValue val, |
| 1891 |
MutableHandleValue rval) { |
| 1892 |
CHECK_THREAD(cx); |
| 1893 |
cx->check(obj, key, val); |
| 1894 |
|
| 1895 |
if (obj->is<MapObject>()) { |
| 1896 |
return obj.as<MapObject>()->getOrInsert(cx, key, val, rval); |
| 1897 |
} |
| 1898 |
{ |
| 1899 |
AutoEnterTableRealm<MapObject> enter(cx, obj); |
| 1900 |
Rooted<Value> wrappedKey(cx, key); |
| 1901 |
Rooted<Value> wrappedValue(cx, val); |
| 1902 |
if (!JS_WrapValue(cx, &wrappedKey) || !JS_WrapValue(cx, &wrappedValue)) { |
| 1903 |
return false; |
| 1904 |
} |
| 1905 |
if (!enter.unwrapped()->getOrInsert(cx, wrappedKey, wrappedValue, rval)) { |
| 1906 |
return false; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
return JS_WrapValue(cx, rval); |
| 1910 |
} |
| 1911 |
#endif // #ifdef NIGHTLY_BUILD |
| 1912 |
|
| 1913 |
JS_PUBLIC_API bool JS::MapDelete(JSContext* cx, HandleObject obj, |
| 1914 |
HandleValue key, bool* rval) { |
| 1915 |
CHECK_THREAD(cx); |
| 1916 |
cx->check(obj, key); |
| 1917 |
|
| 1918 |
if (obj->is<MapObject>()) { |
| 1919 |
return obj.as<MapObject>()->delete_(cx, key, rval); |
| 1920 |
} |
| 1921 |
|
| 1922 |
AutoEnterTableRealm<MapObject> enter(cx, obj); |
| 1923 |
Rooted<Value> wrappedKey(cx, key); |
| 1924 |
if (!JS_WrapValue(cx, &wrappedKey)) { |
| 1925 |
return false; |
| 1926 |
} |
| 1927 |
return enter.unwrapped()->delete_(cx, wrappedKey, rval); |
| 1928 |
} |
| 1929 |
|
| 1930 |
JS_PUBLIC_API bool JS::MapClear(JSContext* cx, HandleObject obj) { |
| 1931 |
CHECK_THREAD(cx); |
| 1932 |
cx->check(obj); |
| 1933 |
|
| 1934 |
if (obj->is<MapObject>()) { |
| 1935 |
obj.as<MapObject>()->clear(cx); |
| 1936 |
return true; |
| 1937 |
} |
| 1938 |
|
| 1939 |
AutoEnterTableRealm<MapObject> enter(cx, obj); |
| 1940 |
enter.unwrapped()->clear(cx); |
| 1941 |
return true; |
| 1942 |
} |
| 1943 |
|
| 1944 |
template <typename TableObject> |
| 1945 |
[[nodiscard]] static bool CreateIterator( |
| 1946 |
JSContext* cx, typename TableObject::IteratorKind kind, |
| 1947 |
Handle<JSObject*> obj, MutableHandle<Value> rval) { |
| 1948 |
CHECK_THREAD(cx); |
| 1949 |
cx->check(obj); |
| 1950 |
|
| 1951 |
if (obj->is<TableObject>()) { |
| 1952 |
return TableObject::iterator(cx, kind, obj.as<TableObject>(), rval); |
| 1953 |
} |
| 1954 |
|
| 1955 |
{ |
| 1956 |
AutoEnterTableRealm<TableObject> enter(cx, obj); |
| 1957 |
if (!TableObject::iterator(cx, kind, enter.unwrapped(), rval)) { |
| 1958 |
return false; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
return JS_WrapValue(cx, rval); |
| 1962 |
} |
| 1963 |
|
| 1964 |
JS_PUBLIC_API bool JS::MapKeys(JSContext* cx, HandleObject obj, |
| 1965 |
MutableHandleValue rval) { |
| 1966 |
return CreateIterator<MapObject>(cx, MapObject::IteratorKind::Keys, obj, |
| 1967 |
rval); |
| 1968 |
} |
| 1969 |
|
| 1970 |
JS_PUBLIC_API bool JS::MapValues(JSContext* cx, HandleObject obj, |
| 1971 |
MutableHandleValue rval) { |
| 1972 |
return CreateIterator<MapObject>(cx, MapObject::IteratorKind::Values, obj, |
| 1973 |
rval); |
| 1974 |
} |
| 1975 |
|
| 1976 |
JS_PUBLIC_API bool JS::MapEntries(JSContext* cx, HandleObject obj, |
| 1977 |
MutableHandleValue rval) { |
| 1978 |
return CreateIterator<MapObject>(cx, MapObject::IteratorKind::Entries, obj, |
| 1979 |
rval); |
| 1980 |
} |
| 1981 |
|
| 1982 |
JS_PUBLIC_API bool JS::MapForEach(JSContext* cx, HandleObject obj, |
| 1983 |
HandleValue callbackFn, HandleValue thisVal) { |
| 1984 |
return forEach("MapForEach", cx, obj, callbackFn, thisVal); |
| 1985 |
} |
| 1986 |
|
| 1987 |
JS_PUBLIC_API JSObject* JS::NewSetObject(JSContext* cx) { |
| 1988 |
return SetObject::create(cx); |
| 1989 |
} |
| 1990 |
|
| 1991 |
JS_PUBLIC_API uint32_t JS::SetSize(JSContext* cx, HandleObject obj) { |
| 1992 |
CHECK_THREAD(cx); |
| 1993 |
cx->check(obj); |
| 1994 |
|
| 1995 |
if (obj->is<SetObject>()) { |
| 1996 |
return obj.as<SetObject>()->size(); |
| 1997 |
} |
| 1998 |
|
| 1999 |
AutoEnterTableRealm<SetObject> enter(cx, obj); |
| 2000 |
return enter.unwrapped()->size(); |
| 2001 |
} |
| 2002 |
|
| 2003 |
JS_PUBLIC_API bool JS::SetAdd(JSContext* cx, HandleObject obj, |
| 2004 |
HandleValue key) { |
| 2005 |
CHECK_THREAD(cx); |
| 2006 |
cx->check(obj, key); |
| 2007 |
|
| 2008 |
if (obj->is<SetObject>()) { |
| 2009 |
return obj.as<SetObject>()->add(cx, key); |
| 2010 |
} |
| 2011 |
|
| 2012 |
AutoEnterTableRealm<SetObject> enter(cx, obj); |
| 2013 |
Rooted<Value> wrappedKey(cx, key); |
| 2014 |
if (!JS_WrapValue(cx, &wrappedKey)) { |
| 2015 |
return false; |
| 2016 |
} |
| 2017 |
return enter.unwrapped()->add(cx, wrappedKey); |
| 2018 |
} |
| 2019 |
|
| 2020 |
JS_PUBLIC_API bool JS::SetHas(JSContext* cx, HandleObject obj, HandleValue key, |
| 2021 |
bool* rval) { |
| 2022 |
CHECK_THREAD(cx); |
| 2023 |
cx->check(obj, key); |
| 2024 |
|
| 2025 |
if (obj->is<SetObject>()) { |
| 2026 |
return obj.as<SetObject>()->has(cx, key, rval); |
| 2027 |
} |
| 2028 |
|
| 2029 |
AutoEnterTableRealm<SetObject> enter(cx, obj); |
| 2030 |
Rooted<Value> wrappedKey(cx, key); |
| 2031 |
if (!JS_WrapValue(cx, &wrappedKey)) { |
| 2032 |
return false; |
| 2033 |
} |
| 2034 |
return enter.unwrapped()->has(cx, wrappedKey, rval); |
| 2035 |
} |
| 2036 |
|
| 2037 |
JS_PUBLIC_API bool JS::SetDelete(JSContext* cx, HandleObject obj, |
| 2038 |
HandleValue key, bool* rval) { |
| 2039 |
CHECK_THREAD(cx); |
| 2040 |
cx->check(obj, key); |
| 2041 |
|
| 2042 |
if (obj->is<SetObject>()) { |
| 2043 |
return obj.as<SetObject>()->delete_(cx, key, rval); |
| 2044 |
} |
| 2045 |
|
| 2046 |
AutoEnterTableRealm<SetObject> enter(cx, obj); |
| 2047 |
Rooted<Value> wrappedKey(cx, key); |
| 2048 |
if (!JS_WrapValue(cx, &wrappedKey)) { |
| 2049 |
return false; |
| 2050 |
} |
| 2051 |
return enter.unwrapped()->delete_(cx, wrappedKey, rval); |
| 2052 |
} |
| 2053 |
|
| 2054 |
JS_PUBLIC_API bool JS::SetClear(JSContext* cx, HandleObject obj) { |
| 2055 |
CHECK_THREAD(cx); |
| 2056 |
cx->check(obj); |
| 2057 |
|
| 2058 |
if (obj->is<SetObject>()) { |
| 2059 |
obj.as<SetObject>()->clear(cx); |
| 2060 |
return true; |
| 2061 |
} |
| 2062 |
|
| 2063 |
AutoEnterTableRealm<SetObject> enter(cx, obj); |
| 2064 |
enter.unwrapped()->clear(cx); |
| 2065 |
return true; |
| 2066 |
} |
| 2067 |
|
| 2068 |
JS_PUBLIC_API bool JS::SetKeys(JSContext* cx, HandleObject obj, |
| 2069 |
MutableHandleValue rval) { |
| 2070 |
return SetValues(cx, obj, rval); |
| 2071 |
} |
| 2072 |
|
| 2073 |
JS_PUBLIC_API bool JS::SetValues(JSContext* cx, HandleObject obj, |
| 2074 |
MutableHandleValue rval) { |
| 2075 |
return CreateIterator<SetObject>(cx, SetObject::IteratorKind::Values, obj, |
| 2076 |
rval); |
| 2077 |
} |
| 2078 |
|
| 2079 |
JS_PUBLIC_API bool JS::SetEntries(JSContext* cx, HandleObject obj, |
| 2080 |
MutableHandleValue rval) { |
| 2081 |
return CreateIterator<SetObject>(cx, SetObject::IteratorKind::Entries, obj, |
| 2082 |
rval); |
| 2083 |
} |
| 2084 |
|
| 2085 |
JS_PUBLIC_API bool JS::SetForEach(JSContext* cx, HandleObject obj, |
| 2086 |
HandleValue callbackFn, HandleValue thisVal) { |
| 2087 |
return forEach("SetForEach", cx, obj, callbackFn, thisVal); |
| 2088 |
} |