1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 13:47:46 +00:00

LibJS: Add all of the WeakSet.prototype methods (add, delete, has)

This commit is contained in:
Idan Horowitz 2021-06-09 19:23:28 +03:00 committed by Linus Groh
parent 8b6beac5ce
commit fb63aeae4d
5 changed files with 85 additions and 0 deletions

View file

@ -18,6 +18,11 @@ void WeakSetPrototype::initialize(GlobalObject& global_object)
{
auto& vm = this->vm();
Object::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.add, add, 1, attr);
define_native_function(vm.names.delete_, delete_, 1, attr);
define_native_function(vm.names.has, has, 1, attr);
define_property(vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), vm.names.WeakSet), Attribute::Configurable);
}
@ -38,4 +43,41 @@ WeakSet* WeakSetPrototype::typed_this(VM& vm, GlobalObject& global_object)
return static_cast<WeakSet*>(this_object);
}
JS_DEFINE_NATIVE_FUNCTION(WeakSetPrototype::add)
{
auto* weak_set = typed_this(vm, global_object);
if (!weak_set)
return {};
auto value = vm.argument(0);
if (!value.is_object()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, value.to_string_without_side_effects());
return {};
}
weak_set->values().set(&value.as_object(), AK::HashSetExistingEntryBehavior::Keep);
return weak_set;
}
JS_DEFINE_NATIVE_FUNCTION(WeakSetPrototype::delete_)
{
auto* weak_set = typed_this(vm, global_object);
if (!weak_set)
return {};
auto value = vm.argument(0);
if (!value.is_object())
return Value(false);
return Value(weak_set->values().remove(&value.as_object()));
}
JS_DEFINE_NATIVE_FUNCTION(WeakSetPrototype::has)
{
auto* weak_set = typed_this(vm, global_object);
if (!weak_set)
return {};
auto value = vm.argument(0);
if (!value.is_object())
return Value(false);
auto& values = weak_set->values();
return Value(values.find(&value.as_object()) != values.end());
}
}

View file

@ -20,4 +20,10 @@ public:
private:
static WeakSet* typed_this(VM&, GlobalObject&);
JS_DECLARE_NATIVE_FUNCTION(add);
JS_DECLARE_NATIVE_FUNCTION(delete_);
JS_DECLARE_NATIVE_FUNCTION(has);
};
}