IteratorConstructor.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Intrinsics.h>
  8. #include <LibJS/Runtime/Iterator.h>
  9. #include <LibJS/Runtime/IteratorConstructor.h>
  10. #include <LibJS/Runtime/IteratorPrototype.h>
  11. #include <LibJS/Runtime/Realm.h>
  12. #include <LibJS/Runtime/VM.h>
  13. namespace JS {
  14. // 3.1.1.1 The Iterator Constructor, https://tc39.es/proposal-iterator-helpers/#sec-iterator-constructor
  15. IteratorConstructor::IteratorConstructor(Realm& realm)
  16. : Base(realm.vm().names.Iterator.as_string(), realm.intrinsics().function_prototype())
  17. {
  18. }
  19. void IteratorConstructor::initialize(Realm& realm)
  20. {
  21. Base::initialize(realm);
  22. auto& vm = this->vm();
  23. // 3.1.1.2.1 Iterator.prototype, https://tc39.es/proposal-iterator-helpers/#sec-iterator.prototype
  24. define_direct_property(vm.names.prototype, realm.intrinsics().iterator_prototype(), 0);
  25. u8 attr = Attribute::Writable | Attribute::Configurable;
  26. define_native_function(realm, vm.names.from, from, 1, attr);
  27. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  28. }
  29. // 3.1.1.1.1 Iterator ( ), https://tc39.es/proposal-iterator-helpers/#sec-iterator
  30. ThrowCompletionOr<Value> IteratorConstructor::call()
  31. {
  32. auto& vm = this->vm();
  33. // 1. If NewTarget is undefined or the active function object, throw a TypeError exception.
  34. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Iterator");
  35. }
  36. // 3.1.1.1.1 Iterator ( ), https://tc39.es/proposal-iterator-helpers/#sec-iterator
  37. ThrowCompletionOr<NonnullGCPtr<Object>> IteratorConstructor::construct(FunctionObject& new_target)
  38. {
  39. auto& vm = this->vm();
  40. // 1. If NewTarget is undefined or the active function object, throw a TypeError exception.
  41. if (&new_target == this)
  42. return vm.throw_completion<TypeError>(ErrorType::ClassIsAbstract, "Iterator");
  43. // 2. Return ? OrdinaryCreateFromConstructor(NewTarget, "%Iterator.prototype%").
  44. return TRY(ordinary_create_from_constructor<Iterator>(vm, new_target, &Intrinsics::iterator_prototype));
  45. }
  46. // 3.1.1.2.2 Iterator.from ( O ), https://tc39.es/proposal-iterator-helpers/#sec-iterator.from
  47. JS_DEFINE_NATIVE_FUNCTION(IteratorConstructor::from)
  48. {
  49. auto& realm = *vm.current_realm();
  50. auto object = vm.argument(0);
  51. // 1. Let iteratorRecord be ? GetIteratorFlattenable(O, iterate-strings).
  52. auto iterator_record = TRY(get_iterator_flattenable(vm, object, StringHandling::IterateStrings));
  53. // 2. Let hasInstance be ? OrdinaryHasInstance(%Iterator%, iteratorRecord.[[Iterator]]).
  54. auto has_instance = TRY(ordinary_has_instance(vm, iterator_record.iterator, realm.intrinsics().iterator_constructor()));
  55. // 3. If hasInstance is true, then
  56. if (has_instance.is_boolean() && has_instance.as_bool()) {
  57. // a. Return iteratorRecord.[[Iterator]].
  58. return iterator_record.iterator;
  59. }
  60. // 4. Let wrapper be OrdinaryObjectCreate(%WrapForValidIteratorPrototype%, « [[Iterated]] »).
  61. // 5. Set wrapper.[[Iterated]] to iteratorRecord.
  62. auto wrapper = Iterator::create(realm, realm.intrinsics().wrap_for_valid_iterator_prototype(), move(iterator_record));
  63. // 6. Return wrapper.
  64. return wrapper;
  65. }
  66. }