IteratorConstructor.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ThrowCompletionOr<void> IteratorConstructor::initialize(Realm& realm)
  20. {
  21. MUST_OR_THROW_OOM(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. return {};
  29. }
  30. // 3.1.1.1.1 Iterator ( ), https://tc39.es/proposal-iterator-helpers/#sec-iterator
  31. ThrowCompletionOr<Value> IteratorConstructor::call()
  32. {
  33. auto& vm = this->vm();
  34. // 1. If NewTarget is undefined or the active function object, throw a TypeError exception.
  35. return vm.throw_completion<TypeError>(ErrorType::ConstructorWithoutNew, "Iterator");
  36. }
  37. // 3.1.1.1.1 Iterator ( ), https://tc39.es/proposal-iterator-helpers/#sec-iterator
  38. ThrowCompletionOr<NonnullGCPtr<Object>> IteratorConstructor::construct(FunctionObject& new_target)
  39. {
  40. auto& vm = this->vm();
  41. // 1. If NewTarget is undefined or the active function object, throw a TypeError exception.
  42. if (&new_target == this)
  43. return vm.throw_completion<TypeError>(ErrorType::ClassIsAbstract, "Iterator");
  44. // 2. Return ? OrdinaryCreateFromConstructor(NewTarget, "%Iterator.prototype%").
  45. return TRY(ordinary_create_from_constructor<Iterator>(vm, new_target, &Intrinsics::iterator_prototype));
  46. }
  47. // 3.1.1.2.2 Iterator.from ( O ), https://tc39.es/proposal-iterator-helpers/#sec-iterator.from
  48. JS_DEFINE_NATIVE_FUNCTION(IteratorConstructor::from)
  49. {
  50. auto& realm = *vm.current_realm();
  51. auto object = vm.argument(0);
  52. // 1. If O is a String, set O to ! ToObject(O).
  53. if (object.is_string())
  54. object = MUST_OR_THROW_OOM(object.to_object(vm));
  55. // 2. Let iteratorRecord be ? GetIteratorFlattenable(O).
  56. auto iterator_record = TRY(get_iterator_flattenable(vm, object));
  57. // 3. Let hasInstance be ? OrdinaryHasInstance(%Iterator%, iteratorRecord.[[Iterator]]).
  58. auto has_instance = TRY(ordinary_has_instance(vm, iterator_record.iterator, realm.intrinsics().iterator_constructor()));
  59. // 4. If hasInstance is true, then
  60. if (has_instance.is_boolean() && has_instance.as_bool()) {
  61. // a. Return iteratorRecord.[[Iterator]].
  62. return iterator_record.iterator;
  63. }
  64. // 5. Let wrapper be OrdinaryObjectCreate(%WrapForValidIteratorPrototype%, « [[Iterated]] »).
  65. // 6. Set wrapper.[[Iterated]] to iteratorRecord.
  66. auto wrapper = MUST_OR_THROW_OOM(Iterator::create(realm, realm.intrinsics().wrap_for_valid_iterator_prototype(), move(iterator_record)));
  67. // 7. Return wrapper.
  68. return wrapper;
  69. }
  70. }