IteratorOperations.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibJS/Runtime/Error.h>
  27. #include <LibJS/Runtime/GlobalObject.h>
  28. #include <LibJS/Runtime/IteratorOperations.h>
  29. namespace JS {
  30. Object* get_iterator(GlobalObject& global_object, Value value, String hint, Value method)
  31. {
  32. auto& vm = global_object.vm();
  33. ASSERT(hint == "sync" || hint == "async");
  34. if (method.is_empty()) {
  35. if (hint == "async")
  36. TODO();
  37. auto object = value.to_object(global_object);
  38. if (!object)
  39. return {};
  40. method = object->get(global_object.vm().well_known_symbol_iterator());
  41. if (vm.exception())
  42. return {};
  43. }
  44. if (!method.is_function()) {
  45. vm.throw_exception<TypeError>(global_object, ErrorType::NotIterable, value.to_string_without_side_effects());
  46. return nullptr;
  47. }
  48. auto iterator = vm.call(method.as_function(), value);
  49. if (vm.exception())
  50. return {};
  51. if (!iterator.is_object()) {
  52. vm.throw_exception<TypeError>(global_object, ErrorType::NotIterable, value.to_string_without_side_effects());
  53. return nullptr;
  54. }
  55. return &iterator.as_object();
  56. }
  57. Object* iterator_next(Object& iterator, Value value)
  58. {
  59. auto& vm = iterator.vm();
  60. auto& global_object = iterator.global_object();
  61. auto next_method = iterator.get(vm.names.next);
  62. if (vm.exception())
  63. return {};
  64. if (!next_method.is_function()) {
  65. vm.throw_exception<TypeError>(global_object, ErrorType::IterableNextNotAFunction);
  66. return nullptr;
  67. }
  68. Value result;
  69. if (value.is_empty())
  70. result = vm.call(next_method.as_function(), &iterator);
  71. else
  72. result = vm.call(next_method.as_function(), &iterator, value);
  73. if (vm.exception())
  74. return {};
  75. if (!result.is_object()) {
  76. vm.throw_exception<TypeError>(global_object, ErrorType::IterableNextBadReturn);
  77. return nullptr;
  78. }
  79. return &result.as_object();
  80. }
  81. void iterator_close(Object& iterator)
  82. {
  83. (void)iterator;
  84. TODO();
  85. }
  86. Value create_iterator_result_object(GlobalObject& global_object, Value value, bool done)
  87. {
  88. auto& vm = global_object.vm();
  89. auto* object = Object::create_empty(global_object);
  90. object->define_property(vm.names.value, value);
  91. object->define_property(vm.names.done, Value(done));
  92. return object;
  93. }
  94. void get_iterator_values(GlobalObject& global_object, Value value, AK::Function<IterationDecision(Value)> callback)
  95. {
  96. auto& vm = global_object.vm();
  97. auto iterator = get_iterator(global_object, value);
  98. if (!iterator)
  99. return;
  100. while (true) {
  101. auto next_object = iterator_next(*iterator);
  102. if (!next_object)
  103. return;
  104. auto done_property = next_object->get(vm.names.done);
  105. if (vm.exception())
  106. return;
  107. if (!done_property.is_empty() && done_property.to_boolean())
  108. return;
  109. auto next_value = next_object->get(vm.names.value);
  110. if (vm.exception())
  111. return;
  112. auto result = callback(next_value);
  113. if (result == IterationDecision::Break)
  114. return;
  115. ASSERT(result == IterationDecision::Continue);
  116. }
  117. }
  118. }