SetConstructor.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/Set.h>
  11. #include <LibJS/Runtime/SetConstructor.h>
  12. namespace JS {
  13. SetConstructor::SetConstructor(Realm& realm)
  14. : NativeFunction(realm.vm().names.Set.as_string(), realm.intrinsics().function_prototype())
  15. {
  16. }
  17. void SetConstructor::initialize(Realm& realm)
  18. {
  19. auto& vm = this->vm();
  20. Base::initialize(realm);
  21. // 24.2.2.1 Set.prototype, https://tc39.es/ecma262/#sec-set.prototype
  22. define_direct_property(vm.names.prototype, realm.intrinsics().set_prototype(), 0);
  23. define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
  24. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  25. }
  26. // 24.2.1.1 Set ( [ iterable ] ), https://tc39.es/ecma262/#sec-set-iterable
  27. ThrowCompletionOr<Value> SetConstructor::call()
  28. {
  29. auto& vm = this->vm();
  30. // 1. If NewTarget is undefined, throw a TypeError exception.
  31. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, vm.names.Set);
  32. }
  33. // 24.2.1.1 Set ( [ iterable ] ), https://tc39.es/ecma262/#sec-set-iterable
  34. ThrowCompletionOr<NonnullGCPtr<Object>> SetConstructor::construct(FunctionObject& new_target)
  35. {
  36. auto& vm = this->vm();
  37. auto iterable = vm.argument(0);
  38. // 2. Let set be ? OrdinaryCreateFromConstructor(NewTarget, "%Set.prototype%", « [[SetData]] »).
  39. auto set = TRY(ordinary_create_from_constructor<Set>(vm, new_target, &Intrinsics::set_prototype));
  40. // 3. Set set.[[SetData]] to a new empty List.
  41. // 4. If iterable is either undefined or null, return set.
  42. if (iterable.is_nullish())
  43. return set;
  44. // 5. Let adder be ? Get(set, "add").
  45. auto adder = TRY(set->get(vm.names.add));
  46. // 6. If IsCallable(adder) is false, throw a TypeError exception.
  47. if (!adder.is_function())
  48. return vm.throw_completion<TypeError>(ErrorType::NotAFunction, "'add' property of Set");
  49. // 7. Let iteratorRecord be ? GetIterator(iterable, sync).
  50. // 8. Repeat,
  51. // a. Let next be ? IteratorStep(iteratorRecord).
  52. // c. Let nextValue be ? IteratorValue(next).
  53. (void)TRY(get_iterator_values(vm, iterable, [&](Value next_value) -> Optional<Completion> {
  54. // d. Let status be Completion(Call(adder, set, « nextValue »)).
  55. // e. IfAbruptCloseIterator(status, iteratorRecord).
  56. TRY(JS::call(vm, adder.as_function(), set, next_value));
  57. return {};
  58. }));
  59. // b. If next is false, return set.
  60. return set;
  61. }
  62. // 24.2.2.2 get Set [ @@species ], https://tc39.es/ecma262/#sec-get-set-@@species
  63. JS_DEFINE_NATIVE_FUNCTION(SetConstructor::symbol_species_getter)
  64. {
  65. // 1. Return the this value.
  66. return vm.this_value();
  67. }
  68. }