IteratorOperations.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/Interpreter.h>
  27. #include <LibJS/Runtime/Error.h>
  28. #include <LibJS/Runtime/GlobalObject.h>
  29. #include <LibJS/Runtime/IteratorOperations.h>
  30. namespace JS {
  31. Object* get_iterator(GlobalObject& global_object, Value value, String hint, Value method)
  32. {
  33. auto& interpreter = global_object.interpreter();
  34. ASSERT(hint == "sync" || hint == "async");
  35. if (method.is_empty()) {
  36. if (hint == "async")
  37. TODO();
  38. auto object = value.to_object(interpreter, global_object);
  39. if (!object)
  40. return {};
  41. method = object->get(interpreter.well_known_symbol_iterator());
  42. if (interpreter.exception())
  43. return {};
  44. }
  45. if (!method.is_function()) {
  46. interpreter.throw_exception<TypeError>(ErrorType::NotIterable, value.to_string_without_side_effects().characters());
  47. return nullptr;
  48. }
  49. auto iterator = interpreter.call(method.as_function(), value);
  50. if (interpreter.exception())
  51. return {};
  52. if (!iterator.is_object()) {
  53. interpreter.throw_exception<TypeError>(ErrorType::NotIterable, value.to_string_without_side_effects().characters());
  54. return nullptr;
  55. }
  56. return &iterator.as_object();
  57. }
  58. Object* iterator_next(Object& iterator, Value value)
  59. {
  60. auto& interpreter = iterator.interpreter();
  61. auto next_method = iterator.get("next");
  62. if (interpreter.exception())
  63. return {};
  64. if (!next_method.is_function()) {
  65. interpreter.throw_exception<TypeError>(ErrorType::IterableNextNotAFunction);
  66. return nullptr;
  67. }
  68. Value result;
  69. if (value.is_empty())
  70. result = interpreter.call(next_method.as_function(), &iterator);
  71. else
  72. result = interpreter.call(next_method.as_function(), &iterator, value);
  73. if (interpreter.exception())
  74. return {};
  75. if (!result.is_object()) {
  76. interpreter.throw_exception<TypeError>(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* object = Object::create_empty(global_object);
  89. object->define_property("value", value);
  90. object->define_property("done", Value(done));
  91. return object;
  92. }
  93. void get_iterator_values(GlobalObject& global_object, Value value, AK::Function<IterationDecision(Value)> callback)
  94. {
  95. auto& interpreter = global_object.interpreter();
  96. auto iterator = get_iterator(global_object, value);
  97. if (!iterator)
  98. return;
  99. while (true) {
  100. auto next_object = iterator_next(*iterator);
  101. if (!next_object)
  102. return;
  103. auto done_property = next_object->get("done");
  104. if (interpreter.exception())
  105. return;
  106. if (!done_property.is_empty() && done_property.to_boolean())
  107. return;
  108. auto next_value = next_object->get("value");
  109. if (interpreter.exception())
  110. return;
  111. auto result = callback(next_value);
  112. if (result == IterationDecision::Break)
  113. return;
  114. ASSERT(result == IterationDecision::Continue);
  115. }
  116. }
  117. }