Bug 1932305 part 7 - Improve MapObject::delete_ and SetObject::delete_. r=iain

Differential Revision: https://phabricator.services.mozilla.com/D229603
This commit is contained in:
Jan de Mooij 2024-11-22 13:40:18 +00:00
parent 188a1749b7
commit a711989ddf
3 changed files with 22 additions and 37 deletions

View File

@ -888,19 +888,16 @@ bool MapObject::set(JSContext* cx, unsigned argc, Value* vp) {
return CallNonGenericMethod<MapObject::is, MapObject::set_impl>(cx, args); return CallNonGenericMethod<MapObject::is, MapObject::set_impl>(cx, args);
} }
bool MapObject::delete_(JSContext* cx, HandleObject obj, HandleValue key, bool MapObject::delete_(JSContext* cx, const Value& key, bool* rval) {
bool* rval) { HashableValue k;
MapObject* mapObject = &obj->as<MapObject>();
Rooted<HashableValue> k(cx);
if (!k.setValue(cx, key)) { if (!k.setValue(cx, key)) {
return false; return false;
} }
if (mapObject->isTenured()) { if (isTenured()) {
*rval = Table(mapObject).remove(cx, k); *rval = Table(this).remove(cx, k);
} else { } else {
*rval = PreBarrieredTable(mapObject).remove(cx, k); *rval = PreBarrieredTable(this).remove(cx, k);
} }
return true; return true;
} }
@ -915,14 +912,12 @@ bool MapObject::delete_impl(JSContext* cx, const CallArgs& args) {
// because makeEmpty clears the value by doing e->value = Value(), and in the // because makeEmpty clears the value by doing e->value = Value(), and in the
// case of Table, Value() means HeapPtr<Value>(), which is the same as // case of Table, Value() means HeapPtr<Value>(), which is the same as
// HeapPtr<Value>(UndefinedValue()). // HeapPtr<Value>(UndefinedValue()).
MOZ_ASSERT(MapObject::is(args.thisv()));
RootedObject obj(cx, &args.thisv().toObject());
auto* mapObj = &args.thisv().toObject().as<MapObject>();
bool found; bool found;
if (!delete_(cx, obj, args.get(0), &found)) { if (!mapObj->delete_(cx, args.get(0), &found)) {
return false; return false;
} }
args.rval().setBoolean(found); args.rval().setBoolean(found);
return true; return true;
} }
@ -1551,29 +1546,22 @@ bool SetObject::add(JSContext* cx, unsigned argc, Value* vp) {
return CallNonGenericMethod<SetObject::is, SetObject::add_impl>(cx, args); return CallNonGenericMethod<SetObject::is, SetObject::add_impl>(cx, args);
} }
bool SetObject::delete_(JSContext* cx, HandleObject obj, HandleValue key, bool SetObject::delete_(JSContext* cx, const Value& key, bool* rval) {
bool* rval) { HashableValue k;
MOZ_ASSERT(SetObject::is(obj));
Rooted<HashableValue> k(cx);
if (!k.setValue(cx, key)) { if (!k.setValue(cx, key)) {
return false; return false;
} }
SetObject* setObj = &obj->as<SetObject>(); *rval = Table(this).remove(cx, k);
*rval = Table(setObj).remove(cx, k);
return true; return true;
} }
bool SetObject::delete_impl(JSContext* cx, const CallArgs& args) { bool SetObject::delete_impl(JSContext* cx, const CallArgs& args) {
MOZ_ASSERT(is(args.thisv())); auto* setObj = &args.thisv().toObject().as<SetObject>();
bool found;
ARG0_KEY(cx, args, key); if (!setObj->delete_(cx, args.get(0), &found)) {
return false;
SetObject* setObj = &args.thisv().toObject().as<SetObject>(); }
bool found = Table(setObj).remove(cx, key);
args.rval().setBoolean(found); args.rval().setBoolean(found);
return true; return true;
} }
@ -1780,7 +1768,7 @@ JS_PUBLIC_API bool JS::MapDelete(JSContext* cx, HandleObject obj,
cx->check(obj, key); cx->check(obj, key);
if (obj->is<MapObject>()) { if (obj->is<MapObject>()) {
return MapObject::delete_(cx, obj.as<MapObject>(), key, rval); return obj.as<MapObject>()->delete_(cx, key, rval);
} }
AutoEnterTableRealm<MapObject> enter(cx, obj); AutoEnterTableRealm<MapObject> enter(cx, obj);
@ -1788,7 +1776,7 @@ JS_PUBLIC_API bool JS::MapDelete(JSContext* cx, HandleObject obj,
if (!JS_WrapValue(cx, &wrappedKey)) { if (!JS_WrapValue(cx, &wrappedKey)) {
return false; return false;
} }
return MapObject::delete_(cx, enter.unwrapped(), wrappedKey, rval); return enter.unwrapped()->delete_(cx, wrappedKey, rval);
} }
JS_PUBLIC_API bool JS::MapClear(JSContext* cx, HandleObject obj) { JS_PUBLIC_API bool JS::MapClear(JSContext* cx, HandleObject obj) {
@ -1904,7 +1892,7 @@ JS_PUBLIC_API bool JS::SetDelete(JSContext* cx, HandleObject obj,
cx->check(obj, key); cx->check(obj, key);
if (obj->is<SetObject>()) { if (obj->is<SetObject>()) {
return SetObject::delete_(cx, obj.as<SetObject>(), key, rval); return obj.as<SetObject>()->delete_(cx, key, rval);
} }
AutoEnterTableRealm<SetObject> enter(cx, obj); AutoEnterTableRealm<SetObject> enter(cx, obj);
@ -1912,7 +1900,7 @@ JS_PUBLIC_API bool JS::SetDelete(JSContext* cx, HandleObject obj,
if (!JS_WrapValue(cx, &wrappedKey)) { if (!JS_WrapValue(cx, &wrappedKey)) {
return false; return false;
} }
return SetObject::delete_(cx, enter.unwrapped(), wrappedKey, rval); return enter.unwrapped()->delete_(cx, wrappedKey, rval);
} }
JS_PUBLIC_API bool JS::SetClear(JSContext* cx, HandleObject obj) { JS_PUBLIC_API bool JS::SetClear(JSContext* cx, HandleObject obj) {

View File

@ -154,8 +154,7 @@ class MapObject : public OrderedHashMapObject {
[[nodiscard]] bool get(JSContext* cx, const Value& key, [[nodiscard]] bool get(JSContext* cx, const Value& key,
MutableHandleValue rval); MutableHandleValue rval);
[[nodiscard]] bool has(JSContext* cx, const Value& key, bool* rval); [[nodiscard]] bool has(JSContext* cx, const Value& key, bool* rval);
[[nodiscard]] static bool delete_(JSContext* cx, HandleObject obj, [[nodiscard]] bool delete_(JSContext* cx, const Value& key, bool* rval);
HandleValue key, bool* rval);
// Set call for public JSAPI exposure. Does not actually return map object // Set call for public JSAPI exposure. Does not actually return map object
// as stated in spec, expects caller to return a value. for instance, with // as stated in spec, expects caller to return a value. for instance, with
@ -290,8 +289,7 @@ class SetObject : public OrderedHashSetObject {
[[nodiscard]] static bool iterator(JSContext* cx, IteratorKind kind, [[nodiscard]] static bool iterator(JSContext* cx, IteratorKind kind,
HandleObject obj, MutableHandleValue iter); HandleObject obj, MutableHandleValue iter);
[[nodiscard]] static bool delete_(JSContext* cx, unsigned argc, Value* vp); [[nodiscard]] static bool delete_(JSContext* cx, unsigned argc, Value* vp);
[[nodiscard]] static bool delete_(JSContext* cx, HandleObject obj, [[nodiscard]] bool delete_(JSContext* cx, const Value& key, bool* rval);
HandleValue key, bool* rval);
[[nodiscard]] static bool copy(JSContext* cx, unsigned argc, Value* vp); [[nodiscard]] static bool copy(JSContext* cx, unsigned argc, Value* vp);

View File

@ -1502,8 +1502,7 @@ static bool TrackUnhandledRejections(JSContext* cx, JS::HandleObject promise,
break; break;
case JS::PromiseRejectionHandlingState::Handled: case JS::PromiseRejectionHandlingState::Handled:
bool deleted = false; bool deleted = false;
if (!SetObject::delete_(cx, sc->unhandledRejectedPromises, promiseVal, if (!sc->unhandledRejectedPromises->delete_(cx, promiseVal, &deleted)) {
&deleted)) {
return false; return false;
} }
// We can't MOZ_ASSERT(deleted) here, because it's possible we failed to // We can't MOZ_ASSERT(deleted) here, because it's possible we failed to