IteratorConstructor.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. JS_DEFINE_ALLOCATOR(IteratorConstructor);
  15. // 3.1.1.1 The Iterator Constructor, https://tc39.es/proposal-iterator-helpers/#sec-iterator-constructor
  16. IteratorConstructor::IteratorConstructor(Realm& realm)
  17. : Base(realm.vm().names.Iterator.as_string(), realm.intrinsics().function_prototype())
  18. {
  19. }
  20. void IteratorConstructor::initialize(Realm& realm)
  21. {
  22. Base::initialize(realm);
  23. auto& vm = this->vm();
  24. // 3.1.1.2.1 Iterator.prototype, https://tc39.es/proposal-iterator-helpers/#sec-iterator.prototype
  25. define_direct_property(vm.names.prototype, realm.intrinsics().iterator_prototype(), 0);
  26. u8 attr = Attribute::Writable | Attribute::Configurable;
  27. define_native_function(realm, vm.names.from, from, 1, attr);
  28. define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
  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. Let iteratorRecord be ? GetIteratorFlattenable(O, iterate-strings).
  53. auto iterator_record = TRY(get_iterator_flattenable(vm, object, StringHandling::IterateStrings));
  54. // 2. Let hasInstance be ? OrdinaryHasInstance(%Iterator%, iteratorRecord.[[Iterator]]).
  55. auto has_instance = TRY(ordinary_has_instance(vm, iterator_record->iterator, realm.intrinsics().iterator_constructor()));
  56. // 3. If hasInstance is true, then
  57. if (has_instance.is_boolean() && has_instance.as_bool()) {
  58. // a. Return iteratorRecord.[[Iterator]].
  59. return iterator_record->iterator;
  60. }
  61. // 4. Let wrapper be OrdinaryObjectCreate(%WrapForValidIteratorPrototype%, « [[Iterated]] »).
  62. // 5. Set wrapper.[[Iterated]] to iteratorRecord.
  63. auto wrapper = Iterator::create(realm, realm.intrinsics().wrap_for_valid_iterator_prototype(), move(iterator_record));
  64. // 6. Return wrapper.
  65. return wrapper;
  66. }
  67. }