Serenity Operating System
at master 63 lines 2.0 kB view raw
1/* 2 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibJS/Runtime/AbstractOperations.h> 8#include <LibJS/Runtime/Error.h> 9#include <LibJS/Runtime/GlobalObject.h> 10#include <LibJS/Runtime/IteratorOperations.h> 11#include <LibJS/Runtime/WeakSet.h> 12#include <LibJS/Runtime/WeakSetConstructor.h> 13 14namespace JS { 15 16WeakSetConstructor::WeakSetConstructor(Realm& realm) 17 : NativeFunction(realm.vm().names.WeakSet.as_string(), *realm.intrinsics().function_prototype()) 18{ 19} 20 21ThrowCompletionOr<void> WeakSetConstructor::initialize(Realm& realm) 22{ 23 auto& vm = this->vm(); 24 MUST_OR_THROW_OOM(NativeFunction::initialize(realm)); 25 26 // 24.4.2.1 WeakSet.prototype, https://tc39.es/ecma262/#sec-weakset.prototype 27 define_direct_property(vm.names.prototype, realm.intrinsics().weak_set_prototype(), 0); 28 29 define_direct_property(vm.names.length, Value(0), Attribute::Configurable); 30 31 return {}; 32} 33 34// 24.4.1.1 WeakSet ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakset-iterable 35ThrowCompletionOr<Value> WeakSetConstructor::call() 36{ 37 auto& vm = this->vm(); 38 return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.WeakSet); 39} 40 41// 24.4.1.1 WeakSet ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakset-iterable 42ThrowCompletionOr<NonnullGCPtr<Object>> WeakSetConstructor::construct(FunctionObject& new_target) 43{ 44 auto& vm = this->vm(); 45 46 auto weak_set = TRY(ordinary_create_from_constructor<WeakSet>(vm, new_target, &Intrinsics::weak_set_prototype)); 47 48 if (vm.argument(0).is_nullish()) 49 return weak_set; 50 51 auto adder = TRY(weak_set->get(vm.names.add)); 52 if (!adder.is_function()) 53 return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "'add' property of WeakSet"); 54 55 (void)TRY(get_iterator_values(vm, vm.argument(0), [&](Value iterator_value) -> Optional<Completion> { 56 TRY(JS::call(vm, adder.as_function(), weak_set, iterator_value)); 57 return {}; 58 })); 59 60 return weak_set; 61} 62 63}