| 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/WeakMapObject-inl.h" |
| 8 |
|
| 9 |
#include "builtin/WeakSetObject.h" |
| 10 |
#include "gc/GC.h" |
| 11 |
#include "gc/GCContext.h" |
| 12 |
#include "js/friend/ErrorMessages.h" // JSMSG_* |
| 13 |
#include "js/PropertySpec.h" |
| 14 |
#include "js/WeakMap.h" |
| 15 |
#include "vm/Compartment.h" |
| 16 |
#include "vm/JSContext.h" |
| 17 |
#include "vm/SelfHosting.h" |
| 18 |
|
| 19 |
#include "builtin/MapObject-inl.h" |
| 20 |
#include "gc/GCContext-inl.h" |
| 21 |
#include "gc/WeakMap-inl.h" |
| 22 |
#include "vm/NativeObject-inl.h" |
| 23 |
|
| 24 |
using namespace js; |
| 25 |
|
| 26 |
/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::is(HandleValue v) { |
| 27 |
return v.isObject() && v.toObject().is<WeakMapObject>(); |
| 28 |
} |
| 29 |
|
| 30 |
/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::has_impl( |
| 31 |
JSContext* cx, const CallArgs& args) { |
| 32 |
MOZ_ASSERT(is(args.thisv())); |
| 33 |
|
| 34 |
if (!CanBeHeldWeakly(cx, args.get(0))) { |
| 35 |
args.rval().setBoolean(false); |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
if (ValueValueWeakMap* map = |
| 40 |
args.thisv().toObject().as<WeakMapObject>().getMap()) { |
| 41 |
Value key = args[0]; |
| 42 |
if (map->has(key)) { |
| 43 |
args.rval().setBoolean(true); |
| 44 |
return true; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
args.rval().setBoolean(false); |
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
/* static */ |
| 53 |
bool WeakMapObject::has(JSContext* cx, unsigned argc, Value* vp) { |
| 54 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 55 |
return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::has_impl>(cx, |
| 56 |
args); |
| 57 |
} |
| 58 |
|
| 59 |
/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::get_impl( |
| 60 |
JSContext* cx, const CallArgs& args) { |
| 61 |
MOZ_ASSERT(WeakMapObject::is(args.thisv())); |
| 62 |
|
| 63 |
if (!CanBeHeldWeakly(cx, args.get(0))) { |
| 64 |
args.rval().setUndefined(); |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
if (ValueValueWeakMap* map = |
| 69 |
args.thisv().toObject().as<WeakMapObject>().getMap()) { |
| 70 |
Value key = args[0]; |
| 71 |
if (ValueValueWeakMap::Ptr ptr = map->lookup(key)) { |
| 72 |
args.rval().set(ptr->value()); |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
args.rval().setUndefined(); |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
/* static */ |
| 82 |
bool WeakMapObject::get(JSContext* cx, unsigned argc, Value* vp) { |
| 83 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 84 |
return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::get_impl>(cx, |
| 85 |
args); |
| 86 |
} |
| 87 |
|
| 88 |
/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::delete_impl( |
| 89 |
JSContext* cx, const CallArgs& args) { |
| 90 |
MOZ_ASSERT(WeakMapObject::is(args.thisv())); |
| 91 |
|
| 92 |
if (!CanBeHeldWeakly(cx, args.get(0))) { |
| 93 |
args.rval().setBoolean(false); |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
if (ValueValueWeakMap* map = |
| 98 |
args.thisv().toObject().as<WeakMapObject>().getMap()) { |
| 99 |
Value key = args[0]; |
| 100 |
// The lookup here is only used for the removal, so we can skip the read |
| 101 |
// barrier. This is not very important for performance, but makes it easier |
| 102 |
// to test nonbarriered removal from internal weakmaps (eg Debugger maps.) |
| 103 |
if (ValueValueWeakMap::Ptr ptr = map->lookupUnbarriered(key)) { |
| 104 |
map->remove(ptr); |
| 105 |
args.rval().setBoolean(true); |
| 106 |
return true; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
args.rval().setBoolean(false); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/* static */ |
| 115 |
bool WeakMapObject::delete_(JSContext* cx, unsigned argc, Value* vp) { |
| 116 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 117 |
return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::delete_impl>( |
| 118 |
cx, args); |
| 119 |
} |
| 120 |
|
| 121 |
static bool EnsureValidWeakMapKey(JSContext* cx, Handle<Value> keyVal) { |
| 122 |
if (MOZ_UNLIKELY(!CanBeHeldWeakly(cx, keyVal))) { |
| 123 |
unsigned errorNum = GetErrorNumber(true); |
| 124 |
ReportValueError(cx, errorNum, JSDVG_IGNORE_STACK, keyVal, nullptr); |
| 125 |
return false; |
| 126 |
} |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
static bool SetWeakMapEntryImpl(JSContext* cx, Handle<WeakMapObject*> mapObj, |
| 131 |
Handle<Value> keyVal, Handle<Value> value) { |
| 132 |
if (!EnsureValidWeakMapKey(cx, keyVal)) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
return WeakCollectionPutEntryInternal(cx, mapObj, keyVal, value); |
| 136 |
} |
| 137 |
|
| 138 |
/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::set_impl( |
| 139 |
JSContext* cx, const CallArgs& args) { |
| 140 |
MOZ_ASSERT(WeakMapObject::is(args.thisv())); |
| 141 |
|
| 142 |
Rooted<WeakMapObject*> map(cx, &args.thisv().toObject().as<WeakMapObject>()); |
| 143 |
if (!SetWeakMapEntryImpl(cx, map, args.get(0), args.get(1))) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
args.rval().set(args.thisv()); |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
/* static */ |
| 152 |
bool WeakMapObject::set(JSContext* cx, unsigned argc, Value* vp) { |
| 153 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 154 |
return CallNonGenericMethod<WeakMapObject::is, WeakMapObject::set_impl>(cx, |
| 155 |
args); |
| 156 |
} |
| 157 |
|
| 158 |
#ifdef NIGHTLY_BUILD |
| 159 |
static bool GetOrAddWeakMapEntry(JSContext* cx, Handle<WeakMapObject*> mapObj, |
| 160 |
Handle<Value> key, Handle<Value> value, |
| 161 |
MutableHandleValue rval) { |
| 162 |
if (!EnsureValidWeakMapKey(cx, key)) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
if (!EnsureObjectHasWeakMap(cx, mapObj)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
ValueValueWeakMap* map = mapObj->getMap(); |
| 171 |
ValueValueWeakMap::AddPtr addPtr = map->lookupForAdd(key); |
| 172 |
if (!addPtr) { |
| 173 |
if (!PreserveReflectorAndAssertValidEntry(cx, mapObj, key, value)) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
if (!map->add(addPtr, key, value)) { |
| 177 |
JS_ReportOutOfMemory(cx); |
| 178 |
return false; |
| 179 |
} |
| 180 |
} |
| 181 |
rval.set(addPtr->value()); |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::getOrInsert_impl( |
| 186 |
JSContext* cx, const CallArgs& args) { |
| 187 |
MOZ_ASSERT(WeakMapObject::is(args.thisv())); |
| 188 |
|
| 189 |
Rooted<WeakMapObject*> map(cx, &args.thisv().toObject().as<WeakMapObject>()); |
| 190 |
return GetOrAddWeakMapEntry(cx, map, args.get(0), args.get(1), args.rval()); |
| 191 |
} |
| 192 |
|
| 193 |
/* static */ |
| 194 |
bool WeakMapObject::getOrInsert(JSContext* cx, unsigned argc, Value* vp) { |
| 195 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 196 |
return CallNonGenericMethod<WeakMapObject::is, |
| 197 |
WeakMapObject::getOrInsert_impl>(cx, args); |
| 198 |
} |
| 199 |
#endif // #ifdef NIGHTLY_BUILD |
| 200 |
|
| 201 |
size_t WeakCollectionObject::sizeOfExcludingThis( |
| 202 |
mozilla::MallocSizeOf aMallocSizeOf) { |
| 203 |
ValueValueWeakMap* map = getMap(); |
| 204 |
return map ? map->sizeOfIncludingThis(aMallocSizeOf) : 0; |
| 205 |
} |
| 206 |
|
| 207 |
bool WeakCollectionObject::nondeterministicGetKeys( |
| 208 |
JSContext* cx, Handle<WeakCollectionObject*> obj, MutableHandleObject ret) { |
| 209 |
RootedObject arr(cx, NewDenseEmptyArray(cx)); |
| 210 |
if (!arr) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
if (ValueValueWeakMap* map = obj->getMap()) { |
| 214 |
// Prevent GC from mutating the weakmap while iterating. |
| 215 |
gc::AutoSuppressGC suppress(cx); |
| 216 |
for (ValueValueWeakMap::Range r = map->all(); !r.empty(); r.popFront()) { |
| 217 |
const auto& key = r.front().key(); |
| 218 |
MOZ_ASSERT(key.isObject() || key.isSymbol()); |
| 219 |
JS::ExposeValueToActiveJS(key); |
| 220 |
RootedValue keyVal(cx, key); |
| 221 |
if (!cx->compartment()->wrap(cx, &keyVal)) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
if (!NewbornArrayPush(cx, arr, keyVal)) { |
| 225 |
return false; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
ret.set(arr); |
| 230 |
return true; |
| 231 |
} |
| 232 |
|
| 233 |
JS_PUBLIC_API bool JS_NondeterministicGetWeakMapKeys(JSContext* cx, |
| 234 |
HandleObject objArg, |
| 235 |
MutableHandleObject ret) { |
| 236 |
RootedObject obj(cx, UncheckedUnwrap(objArg)); |
| 237 |
if (!obj || !obj->is<WeakMapObject>()) { |
| 238 |
ret.set(nullptr); |
| 239 |
return true; |
| 240 |
} |
| 241 |
return WeakCollectionObject::nondeterministicGetKeys( |
| 242 |
cx, obj.as<WeakCollectionObject>(), ret); |
| 243 |
} |
| 244 |
|
| 245 |
static void WeakCollection_trace(JSTracer* trc, JSObject* obj) { |
| 246 |
if (ValueValueWeakMap* map = obj->as<WeakCollectionObject>().getMap()) { |
| 247 |
map->trace(trc); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
static void WeakCollection_finalize(JS::GCContext* gcx, JSObject* obj) { |
| 252 |
if (ValueValueWeakMap* map = obj->as<WeakCollectionObject>().getMap()) { |
| 253 |
gcx->delete_(obj, map, MemoryUse::WeakMapObject); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
JS_PUBLIC_API JSObject* JS::NewWeakMapObject(JSContext* cx) { |
| 258 |
return NewBuiltinClassInstance<WeakMapObject>(cx); |
| 259 |
} |
| 260 |
|
| 261 |
JS_PUBLIC_API bool JS::IsWeakMapObject(JSObject* obj) { |
| 262 |
return obj->is<WeakMapObject>(); |
| 263 |
} |
| 264 |
|
| 265 |
JS_PUBLIC_API bool JS::GetWeakMapEntry(JSContext* cx, HandleObject mapObj, |
| 266 |
HandleValue key, |
| 267 |
MutableHandleValue rval) { |
| 268 |
CHECK_THREAD(cx); |
| 269 |
cx->check(key); |
| 270 |
rval.setUndefined(); |
| 271 |
|
| 272 |
if (!CanBeHeldWeakly(cx, key)) { |
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
ValueValueWeakMap* map = mapObj->as<WeakMapObject>().getMap(); |
| 277 |
if (!map) { |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
if (ValueValueWeakMap::Ptr ptr = map->lookup(key)) { |
| 282 |
rval.set(ptr->value()); |
| 283 |
} |
| 284 |
return true; |
| 285 |
} |
| 286 |
|
| 287 |
JS_PUBLIC_API bool JS::SetWeakMapEntry(JSContext* cx, HandleObject mapObj, |
| 288 |
HandleValue key, HandleValue val) { |
| 289 |
CHECK_THREAD(cx); |
| 290 |
cx->check(key, val); |
| 291 |
return SetWeakMapEntryImpl(cx, mapObj.as<WeakMapObject>(), key, val); |
| 292 |
} |
| 293 |
|
| 294 |
// static |
| 295 |
bool WeakMapObject::tryOptimizeCtorWithIterable(JSContext* cx, |
| 296 |
Handle<WeakMapObject*> obj, |
| 297 |
Handle<Value> iterableVal, |
| 298 |
bool* optimized) { |
| 299 |
MOZ_ASSERT(!iterableVal.isNullOrUndefined()); |
| 300 |
MOZ_ASSERT(!*optimized); |
| 301 |
|
| 302 |
if (!CanOptimizeMapOrSetCtorWithIterable<JSProto_WeakMap>(WeakMapObject::set, |
| 303 |
obj, cx)) { |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
if (!iterableVal.isObject()) { |
| 308 |
return true; |
| 309 |
} |
| 310 |
JSObject* iterable = &iterableVal.toObject(); |
| 311 |
|
| 312 |
// Fast path for `new WeakMap(array)`. |
| 313 |
if (IsOptimizableArrayForMapOrSetCtor<MapOrSet::Map>(iterable, cx)) { |
| 314 |
RootedValue keyVal(cx); |
| 315 |
RootedValue value(cx); |
| 316 |
Rooted<ArrayObject*> array(cx, &iterable->as<ArrayObject>()); |
| 317 |
uint32_t len = array->getDenseInitializedLength(); |
| 318 |
|
| 319 |
for (uint32_t index = 0; index < len; index++) { |
| 320 |
Value element = array->getDenseElement(index); |
| 321 |
MOZ_ASSERT(IsPackedArray(&element.toObject())); |
| 322 |
|
| 323 |
auto* elementArray = &element.toObject().as<ArrayObject>(); |
| 324 |
keyVal.set(elementArray->getDenseElement(0)); |
| 325 |
value.set(elementArray->getDenseElement(1)); |
| 326 |
MOZ_ASSERT(!keyVal.isMagic(JS_ELEMENTS_HOLE)); |
| 327 |
MOZ_ASSERT(!value.isMagic(JS_ELEMENTS_HOLE)); |
| 328 |
|
| 329 |
if (!SetWeakMapEntryImpl(cx, obj, keyVal, value)) { |
| 330 |
return false; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
*optimized = true; |
| 335 |
return true; |
| 336 |
} |
| 337 |
|
| 338 |
return true; |
| 339 |
} |
| 340 |
|
| 341 |
/* static */ |
| 342 |
bool WeakMapObject::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 343 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 344 |
|
| 345 |
// ES6 draft rev 31 (15 Jan 2015) 23.3.1.1 step 1. |
| 346 |
if (!ThrowIfNotConstructing(cx, args, "WeakMap")) { |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
RootedObject proto(cx); |
| 351 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_WeakMap, &proto)) { |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
Rooted<WeakMapObject*> obj(cx, |
| 356 |
NewObjectWithClassProto<WeakMapObject>(cx, proto)); |
| 357 |
if (!obj) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
|
| 361 |
// Steps 5-6, 11. |
| 362 |
if (!args.get(0).isNullOrUndefined()) { |
| 363 |
Handle<Value> iterable = args[0]; |
| 364 |
bool optimized = false; |
| 365 |
if (!tryOptimizeCtorWithIterable(cx, obj, iterable, &optimized)) { |
| 366 |
return false; |
| 367 |
} |
| 368 |
if (!optimized) { |
| 369 |
FixedInvokeArgs<1> args2(cx); |
| 370 |
args2[0].set(iterable); |
| 371 |
|
| 372 |
RootedValue thisv(cx, ObjectValue(*obj)); |
| 373 |
if (!CallSelfHostedFunction(cx, cx->names().WeakMapConstructorInit, thisv, |
| 374 |
args2, args2.rval())) { |
| 375 |
return false; |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
args.rval().setObject(*obj); |
| 381 |
return true; |
| 382 |
} |
| 383 |
|
| 384 |
const JSClassOps WeakCollectionObject::classOps_ = { |
| 385 |
nullptr, // addProperty |
| 386 |
nullptr, // delProperty |
| 387 |
nullptr, // enumerate |
| 388 |
nullptr, // newEnumerate |
| 389 |
nullptr, // resolve |
| 390 |
nullptr, // mayResolve |
| 391 |
WeakCollection_finalize, // finalize |
| 392 |
nullptr, // call |
| 393 |
nullptr, // construct |
| 394 |
WeakCollection_trace, // trace |
| 395 |
}; |
| 396 |
|
| 397 |
const ClassSpec WeakMapObject::classSpec_ = { |
| 398 |
GenericCreateConstructor<WeakMapObject::construct, 0, |
| 399 |
gc::AllocKind::FUNCTION>, |
| 400 |
GenericCreatePrototype<WeakMapObject>, |
| 401 |
nullptr, |
| 402 |
nullptr, |
| 403 |
WeakMapObject::methods, |
| 404 |
WeakMapObject::properties, |
| 405 |
GenericFinishInit<WhichHasFuseProperty::Proto>, |
| 406 |
}; |
| 407 |
|
| 408 |
const JSClass WeakMapObject::class_ = { |
| 409 |
"WeakMap", |
| 410 |
JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | |
| 411 |
JSCLASS_HAS_CACHED_PROTO(JSProto_WeakMap) | JSCLASS_BACKGROUND_FINALIZE, |
| 412 |
&WeakCollectionObject::classOps_, |
| 413 |
&WeakMapObject::classSpec_, |
| 414 |
}; |
| 415 |
|
| 416 |
const JSClass WeakMapObject::protoClass_ = { |
| 417 |
"WeakMap.prototype", |
| 418 |
JSCLASS_HAS_CACHED_PROTO(JSProto_WeakMap), |
| 419 |
JS_NULL_CLASS_OPS, |
| 420 |
&WeakMapObject::classSpec_, |
| 421 |
}; |
| 422 |
|
| 423 |
const JSPropertySpec WeakMapObject::properties[] = { |
| 424 |
JS_STRING_SYM_PS(toStringTag, "WeakMap", JSPROP_READONLY), |
| 425 |
JS_PS_END, |
| 426 |
}; |
| 427 |
|
| 428 |
const JSFunctionSpec WeakMapObject::methods[] = { |
| 429 |
JS_FN("has", has, 1, 0), |
| 430 |
JS_FN("get", get, 1, 0), |
| 431 |
JS_FN("delete", delete_, 1, 0), |
| 432 |
JS_FN("set", set, 2, 0), |
| 433 |
#ifdef NIGHTLY_BUILD |
| 434 |
JS_FN("getOrInsert", getOrInsert, 2, 0), |
| 435 |
JS_SELF_HOSTED_FN("getOrInsertComputed", "WeakMapGetOrInsertComputed", 2, |
| 436 |
0), |
| 437 |
#endif |
| 438 |
JS_FS_END, |
| 439 |
}; |