1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 21:27:45 +00:00

LibJS: Implement WeakSet changes from 'Symbol as WeakMap Keys Proposal'

This commit is contained in:
Idan Horowitz 2022-06-22 23:09:31 +03:00
parent a80d3fdf49
commit dbd0110721
5 changed files with 32 additions and 16 deletions

View file

@ -1,11 +1,12 @@
/*
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
* Copyright (c) 2021-2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashTable.h>
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/WeakSetPrototype.h>
namespace JS {
@ -34,9 +35,9 @@ JS_DEFINE_NATIVE_FUNCTION(WeakSetPrototype::add)
{
auto* weak_set = TRY(typed_this_object(global_object));
auto value = vm.argument(0);
if (!value.is_object())
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAnObject, value.to_string_without_side_effects());
weak_set->values().set(&value.as_object(), AK::HashSetExistingEntryBehavior::Keep);
if (!can_be_held_weakly(value))
return vm.throw_completion<TypeError>(global_object, ErrorType::CannotBeHeldWeakly, value.to_string_without_side_effects());
weak_set->values().set(&value.as_cell(), AK::HashSetExistingEntryBehavior::Keep);
return weak_set;
}
@ -45,9 +46,9 @@ JS_DEFINE_NATIVE_FUNCTION(WeakSetPrototype::delete_)
{
auto* weak_set = TRY(typed_this_object(global_object));
auto value = vm.argument(0);
if (!value.is_object())
if (!can_be_held_weakly(value))
return Value(false);
return Value(weak_set->values().remove(&value.as_object()));
return Value(weak_set->values().remove(&value.as_cell()));
}
// 24.4.3.4 WeakSet.prototype.has ( value ), https://tc39.es/ecma262/#sec-weakset.prototype.has
@ -55,10 +56,10 @@ JS_DEFINE_NATIVE_FUNCTION(WeakSetPrototype::has)
{
auto* weak_set = TRY(typed_this_object(global_object));
auto value = vm.argument(0);
if (!value.is_object())
if (!can_be_held_weakly(value))
return Value(false);
auto& values = weak_set->values();
return Value(values.find(&value.as_object()) != values.end());
return Value(values.find(&value.as_cell()) != values.end());
}
}