SetConstructor.cpp 2.9 KB

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