2021-06-09 19:23:04 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2021-06-20 01:09:39 +01:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2021-06-09 19:23:04 +03:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
#include <LibJS/Runtime/IteratorOperations.h>
|
|
|
|
#include <LibJS/Runtime/WeakSet.h>
|
|
|
|
#include <LibJS/Runtime/WeakSetConstructor.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
WeakSetConstructor::WeakSetConstructor(GlobalObject& global_object)
|
2021-06-28 03:45:49 +03:00
|
|
|
: NativeFunction(vm().names.WeakSet.as_string(), *global_object.function_prototype())
|
2021-06-09 19:23:04 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void WeakSetConstructor::initialize(GlobalObject& global_object)
|
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
NativeFunction::initialize(global_object);
|
2021-06-19 00:38:41 +01:00
|
|
|
|
|
|
|
// 24.4.2.1 WeakSet.prototype, https://tc39.es/ecma262/#sec-weakset.prototype
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.prototype, global_object.weak_set_prototype(), 0);
|
2021-06-19 00:38:41 +01:00
|
|
|
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
2021-06-09 19:23:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WeakSetConstructor::~WeakSetConstructor()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-19 00:38:41 +01:00
|
|
|
// 24.4.1.1 WeakSet ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakset-iterable
|
2021-06-09 19:23:04 +03:00
|
|
|
Value WeakSetConstructor::call()
|
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
|
|
|
vm.throw_exception<TypeError>(global_object(), ErrorType::ConstructorWithoutNew, vm.names.WeakSet);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-06-19 00:38:41 +01:00
|
|
|
// 24.4.1.1 WeakSet ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakset-iterable
|
2021-06-27 21:48:34 +02:00
|
|
|
Value WeakSetConstructor::construct(FunctionObject& new_target)
|
2021-06-09 19:23:04 +03:00
|
|
|
{
|
|
|
|
auto& vm = this->vm();
|
2021-06-20 01:09:39 +01:00
|
|
|
auto& global_object = this->global_object();
|
|
|
|
|
2021-09-16 00:57:38 +03:00
|
|
|
auto* weak_set = TRY_OR_DISCARD(ordinary_create_from_constructor<WeakSet>(global_object, new_target, &GlobalObject::weak_set_prototype));
|
2021-06-20 01:09:39 +01:00
|
|
|
|
2021-06-09 19:23:04 +03:00
|
|
|
if (vm.argument(0).is_nullish())
|
2021-06-20 01:09:39 +01:00
|
|
|
return weak_set;
|
2021-06-09 19:23:04 +03:00
|
|
|
|
2021-10-02 23:52:27 +01:00
|
|
|
auto adder = TRY_OR_DISCARD(weak_set->get(vm.names.add));
|
2021-06-09 19:23:04 +03:00
|
|
|
if (!adder.is_function()) {
|
2021-06-20 01:09:39 +01:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::NotAFunction, "'add' property of WeakSet");
|
2021-06-09 19:23:04 +03:00
|
|
|
return {};
|
|
|
|
}
|
2021-06-20 01:09:39 +01:00
|
|
|
get_iterator_values(global_object, vm.argument(0), [&](Value iterator_value) {
|
2021-06-09 19:23:04 +03:00
|
|
|
if (vm.exception())
|
|
|
|
return IterationDecision::Break;
|
2021-09-23 20:56:28 +03:00
|
|
|
auto result = vm.call(adder.as_function(), Value(weak_set), iterator_value);
|
|
|
|
return result.is_error() ? IterationDecision::Break : IterationDecision::Continue;
|
2021-06-09 19:23:04 +03:00
|
|
|
});
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
return weak_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|