WeakSetConstructor.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Error.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Iterator.h>
  10. #include <LibJS/Runtime/WeakSet.h>
  11. #include <LibJS/Runtime/WeakSetConstructor.h>
  12. namespace JS {
  13. WeakSetConstructor::WeakSetConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.WeakSet.as_string(), realm.intrinsics().function_prototype())
  15. {
  16. }
  17. void WeakSetConstructor::initialize(Realm& realm)
  18. {
  19. auto& vm = this->vm();
  20. Base::initialize(realm);
  21. // 24.4.2.1 WeakSet.prototype, https://tc39.es/ecma262/#sec-weakset.prototype
  22. define_direct_property(vm.names.prototype, realm.intrinsics().weak_set_prototype(), 0);
  23. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  24. }
  25. // 24.4.1.1 WeakSet ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakset-iterable
  26. ThrowCompletionOr<Value> WeakSetConstructor::call()
  27. {
  28. auto& vm = this->vm();
  29. // 1. If NewTarget is undefined, throw a TypeError exception.
  30. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.WeakSet);
  31. }
  32. // 24.4.1.1 WeakSet ( [ iterable ] ), https://tc39.es/ecma262/#sec-weakset-iterable
  33. ThrowCompletionOr<NonnullGCPtr<Object>> WeakSetConstructor::construct(FunctionObject& new_target)
  34. {
  35. auto& vm = this->vm();
  36. auto iterable = vm.argument(0);
  37. // 2. Let set be ? OrdinaryCreateFromConstructor(NewTarget, "%WeakSet.prototype%", « [[WeakSetData]] »).
  38. // 3. Set set.[[WeakSetData]] to a new empty List.
  39. auto set = TRY(ordinary_create_from_constructor<WeakSet>(vm, new_target, &Intrinsics::weak_set_prototype));
  40. // 4. If iterable is either undefined or null, return set.
  41. if (iterable.is_nullish())
  42. return set;
  43. // 5. Let adder be ? Get(set, "add").
  44. auto adder = TRY(set->get(vm.names.add));
  45. // 6. If IsCallable(adder) is false, throw a TypeError exception.
  46. if (!adder.is_function())
  47. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "'add' property of WeakSet");
  48. // 7. Let iteratorRecord be ? GetIterator(iterable, sync).
  49. // 8. Repeat,
  50. // a. Let next be ? IteratorStep(iteratorRecord).
  51. // c. Let nextValue be ? IteratorValue(next).
  52. (void)TRY(get_iterator_values(vm, iterable, [&](Value next_value) -> Optional<Completion> {
  53. // d. Let status be Completion(Call(adder, set, « nextValue »)).
  54. // e. IfAbruptCloseIterator(status, iteratorRecord).
  55. TRY(JS::call(vm, adder.as_function(), set, next_value));
  56. return {};
  57. }));
  58. // b. If next is false, return set.
  59. return set;
  60. }
  61. }