| 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/WeakSetObject.h" |
| 8 |
|
| 9 |
#include "builtin/MapObject.h" |
| 10 |
#include "js/friend/ErrorMessages.h" // JSMSG_* |
| 11 |
#include "js/PropertySpec.h" |
| 12 |
#include "vm/GlobalObject.h" |
| 13 |
#include "vm/JSContext.h" |
| 14 |
#include "vm/SelfHosting.h" |
| 15 |
|
| 16 |
#include "builtin/MapObject-inl.h" |
| 17 |
#include "builtin/WeakMapObject-inl.h" |
| 18 |
#include "vm/JSObject-inl.h" |
| 19 |
#include "vm/NativeObject-inl.h" |
| 20 |
|
| 21 |
using namespace js; |
| 22 |
|
| 23 |
/* static */ MOZ_ALWAYS_INLINE bool WeakSetObject::is(HandleValue v) { |
| 24 |
return v.isObject() && v.toObject().is<WeakSetObject>(); |
| 25 |
} |
| 26 |
|
| 27 |
static bool AddWeakSetEntryImpl(JSContext* cx, Handle<WeakSetObject*> setObj, |
| 28 |
Handle<Value> keyVal) { |
| 29 |
if (MOZ_UNLIKELY(!CanBeHeldWeakly(cx, keyVal))) { |
| 30 |
unsigned errorNum = GetErrorNumber(false); |
| 31 |
ReportValueError(cx, errorNum, JSDVG_IGNORE_STACK, keyVal, nullptr); |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
return WeakCollectionPutEntryInternal(cx, setObj, keyVal, TrueHandleValue); |
| 36 |
} |
| 37 |
|
| 38 |
// ES2018 draft rev 7a2d3f053ecc2336fc19f377c55d52d78b11b296 |
| 39 |
// 23.4.3.1 WeakSet.prototype.add ( value ) |
| 40 |
/* static */ MOZ_ALWAYS_INLINE bool WeakSetObject::add_impl( |
| 41 |
JSContext* cx, const CallArgs& args) { |
| 42 |
MOZ_ASSERT(is(args.thisv())); |
| 43 |
|
| 44 |
// Steps 4-7. |
| 45 |
Rooted<WeakSetObject*> setObj(cx, |
| 46 |
&args.thisv().toObject().as<WeakSetObject>()); |
| 47 |
if (!AddWeakSetEntryImpl(cx, setObj, args.get(0))) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
// Steps 6.a.i, 8. |
| 52 |
args.rval().set(args.thisv()); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
/* static */ |
| 57 |
bool WeakSetObject::add(JSContext* cx, unsigned argc, Value* vp) { |
| 58 |
// Steps 1-3. |
| 59 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 60 |
return CallNonGenericMethod<WeakSetObject::is, WeakSetObject::add_impl>(cx, |
| 61 |
args); |
| 62 |
} |
| 63 |
|
| 64 |
// ES2018 draft rev 7a2d3f053ecc2336fc19f377c55d52d78b11b296 |
| 65 |
// 23.4.3.3 WeakSet.prototype.delete ( value ) |
| 66 |
/* static */ MOZ_ALWAYS_INLINE bool WeakSetObject::delete_impl( |
| 67 |
JSContext* cx, const CallArgs& args) { |
| 68 |
MOZ_ASSERT(is(args.thisv())); |
| 69 |
|
| 70 |
// Step 4. |
| 71 |
if (!CanBeHeldWeakly(cx, args.get(0))) { |
| 72 |
args.rval().setBoolean(false); |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
// Steps 5-6. |
| 77 |
if (ValueValueWeakMap* map = |
| 78 |
args.thisv().toObject().as<WeakSetObject>().getMap()) { |
| 79 |
Value value = args[0]; |
| 80 |
if (ValueValueWeakMap::Ptr ptr = map->lookup(value)) { |
| 81 |
map->remove(ptr); |
| 82 |
args.rval().setBoolean(true); |
| 83 |
return true; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Step 7. |
| 88 |
args.rval().setBoolean(false); |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
/* static */ |
| 93 |
bool WeakSetObject::delete_(JSContext* cx, unsigned argc, Value* vp) { |
| 94 |
// Steps 1-3. |
| 95 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 96 |
return CallNonGenericMethod<WeakSetObject::is, WeakSetObject::delete_impl>( |
| 97 |
cx, args); |
| 98 |
} |
| 99 |
|
| 100 |
// ES2018 draft rev 7a2d3f053ecc2336fc19f377c55d52d78b11b296 |
| 101 |
// 23.4.3.4 WeakSet.prototype.has ( value ) |
| 102 |
/* static */ MOZ_ALWAYS_INLINE bool WeakSetObject::has_impl( |
| 103 |
JSContext* cx, const CallArgs& args) { |
| 104 |
MOZ_ASSERT(is(args.thisv())); |
| 105 |
|
| 106 |
// Step 5. |
| 107 |
if (!CanBeHeldWeakly(cx, args.get(0))) { |
| 108 |
args.rval().setBoolean(false); |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
// Steps 4, 6. |
| 113 |
if (ValueValueWeakMap* map = |
| 114 |
args.thisv().toObject().as<WeakSetObject>().getMap()) { |
| 115 |
Value value = args[0]; |
| 116 |
if (map->has(value)) { |
| 117 |
args.rval().setBoolean(true); |
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// Step 7. |
| 123 |
args.rval().setBoolean(false); |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
/* static */ |
| 128 |
bool WeakSetObject::has(JSContext* cx, unsigned argc, Value* vp) { |
| 129 |
// Steps 1-3. |
| 130 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 131 |
return CallNonGenericMethod<WeakSetObject::is, WeakSetObject::has_impl>(cx, |
| 132 |
args); |
| 133 |
} |
| 134 |
|
| 135 |
const ClassSpec WeakSetObject::classSpec_ = { |
| 136 |
GenericCreateConstructor<WeakSetObject::construct, 0, |
| 137 |
gc::AllocKind::FUNCTION>, |
| 138 |
GenericCreatePrototype<WeakSetObject>, |
| 139 |
nullptr, |
| 140 |
nullptr, |
| 141 |
WeakSetObject::methods, |
| 142 |
WeakSetObject::properties, |
| 143 |
GenericFinishInit<WhichHasFuseProperty::Proto>, |
| 144 |
}; |
| 145 |
|
| 146 |
const JSClass WeakSetObject::class_ = { |
| 147 |
"WeakSet", |
| 148 |
JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | |
| 149 |
JSCLASS_HAS_CACHED_PROTO(JSProto_WeakSet) | JSCLASS_BACKGROUND_FINALIZE, |
| 150 |
&WeakCollectionObject::classOps_, |
| 151 |
&WeakSetObject::classSpec_, |
| 152 |
}; |
| 153 |
|
| 154 |
const JSClass WeakSetObject::protoClass_ = { |
| 155 |
"WeakSet.prototype", |
| 156 |
JSCLASS_HAS_CACHED_PROTO(JSProto_WeakSet), |
| 157 |
JS_NULL_CLASS_OPS, |
| 158 |
&WeakSetObject::classSpec_, |
| 159 |
}; |
| 160 |
|
| 161 |
const JSPropertySpec WeakSetObject::properties[] = { |
| 162 |
JS_STRING_SYM_PS(toStringTag, "WeakSet", JSPROP_READONLY), |
| 163 |
JS_PS_END, |
| 164 |
}; |
| 165 |
|
| 166 |
const JSFunctionSpec WeakSetObject::methods[] = { |
| 167 |
JS_FN("add", add, 1, 0), |
| 168 |
JS_FN("delete", delete_, 1, 0), |
| 169 |
JS_FN("has", has, 1, 0), |
| 170 |
JS_FS_END, |
| 171 |
}; |
| 172 |
|
| 173 |
WeakSetObject* WeakSetObject::create(JSContext* cx, |
| 174 |
HandleObject proto /* = nullptr */) { |
| 175 |
return NewObjectWithClassProto<WeakSetObject>(cx, proto); |
| 176 |
} |
| 177 |
|
| 178 |
// static |
| 179 |
bool WeakSetObject::tryOptimizeCtorWithIterable(JSContext* cx, |
| 180 |
Handle<WeakSetObject*> obj, |
| 181 |
Handle<Value> iterableVal, |
| 182 |
bool* optimized) { |
| 183 |
MOZ_ASSERT(!iterableVal.isNullOrUndefined()); |
| 184 |
MOZ_ASSERT(!*optimized); |
| 185 |
|
| 186 |
if (!CanOptimizeMapOrSetCtorWithIterable<JSProto_WeakSet>(WeakSetObject::add, |
| 187 |
obj, cx)) { |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
if (!iterableVal.isObject()) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
JSObject* iterable = &iterableVal.toObject(); |
| 195 |
|
| 196 |
// Fast path for `new WeakSet(array)`. |
| 197 |
if (IsOptimizableArrayForMapOrSetCtor<MapOrSet::Set>(iterable, cx)) { |
| 198 |
RootedValue keyVal(cx); |
| 199 |
Rooted<ArrayObject*> array(cx, &iterable->as<ArrayObject>()); |
| 200 |
uint32_t len = array->getDenseInitializedLength(); |
| 201 |
|
| 202 |
for (uint32_t index = 0; index < len; index++) { |
| 203 |
keyVal.set(array->getDenseElement(index)); |
| 204 |
MOZ_ASSERT(!keyVal.isMagic(JS_ELEMENTS_HOLE)); |
| 205 |
|
| 206 |
if (!AddWeakSetEntryImpl(cx, obj, keyVal)) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
*optimized = true; |
| 212 |
return true; |
| 213 |
} |
| 214 |
|
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
bool WeakSetObject::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 219 |
// Based on our "Set" implementation instead of the more general ES6 steps. |
| 220 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 221 |
|
| 222 |
if (!ThrowIfNotConstructing(cx, args, "WeakSet")) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
RootedObject proto(cx); |
| 227 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_WeakSet, &proto)) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
Rooted<WeakSetObject*> obj(cx, WeakSetObject::create(cx, proto)); |
| 232 |
if (!obj) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
if (!args.get(0).isNullOrUndefined()) { |
| 237 |
Handle<Value> iterable = args[0]; |
| 238 |
bool optimized = false; |
| 239 |
if (!tryOptimizeCtorWithIterable(cx, obj, iterable, &optimized)) { |
| 240 |
return false; |
| 241 |
} |
| 242 |
if (!optimized) { |
| 243 |
FixedInvokeArgs<1> args2(cx); |
| 244 |
args2[0].set(iterable); |
| 245 |
|
| 246 |
RootedValue thisv(cx, ObjectValue(*obj)); |
| 247 |
if (!CallSelfHostedFunction(cx, cx->names().WeakSetConstructorInit, thisv, |
| 248 |
args2, args2.rval())) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
args.rval().setObject(*obj); |
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
JS_PUBLIC_API bool JS_NondeterministicGetWeakSetKeys(JSContext* cx, |
| 259 |
HandleObject objArg, |
| 260 |
MutableHandleObject ret) { |
| 261 |
RootedObject obj(cx, UncheckedUnwrap(objArg)); |
| 262 |
if (!obj || !obj->is<WeakSetObject>()) { |
| 263 |
ret.set(nullptr); |
| 264 |
return true; |
| 265 |
} |
| 266 |
return WeakCollectionObject::nondeterministicGetKeys( |
| 267 |
cx, obj.as<WeakCollectionObject>(), ret); |
| 268 |
} |