IteratorOperations.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/IteratorOperations.h>
  28. namespace JS {
  29. Object* get_iterator(Object& obj, String hint, Value method)
  30. {
  31. auto& interpreter = obj.interpreter();
  32. ASSERT(hint == "sync" || hint == "async");
  33. if (method.is_empty()) {
  34. if (hint == "async")
  35. TODO();
  36. method = obj.get(obj.interpreter().well_known_symbol_iterator());
  37. if (interpreter.exception())
  38. return {};
  39. }
  40. if (!method.is_function())
  41. TODO();
  42. auto iterator = interpreter.call(method.as_function(), &obj);
  43. if (interpreter.exception())
  44. return {};
  45. if (!iterator.is_object())
  46. TODO();
  47. return &iterator.as_object();
  48. }
  49. Value iterator_next(Object& iterator, Value value)
  50. {
  51. auto& interpreter = iterator.interpreter();
  52. auto next_method = iterator.get("next");
  53. if (interpreter.exception())
  54. return {};
  55. ASSERT(next_method.is_function());
  56. Value result;
  57. if (value.is_empty()) {
  58. result = interpreter.call(next_method.as_function(), &iterator);
  59. } else {
  60. MarkedValueList arguments(iterator.heap());
  61. arguments.append(value);
  62. result = interpreter.call(next_method.as_function(), &iterator, move(arguments));
  63. }
  64. if (interpreter.exception())
  65. return {};
  66. if (!result.is_object())
  67. TODO();
  68. return result;
  69. }
  70. bool is_iterator_complete(Object& iterator_result)
  71. {
  72. auto done = iterator_result.get("done");
  73. if (iterator_result.interpreter().exception())
  74. return false;
  75. return done.to_boolean();
  76. }
  77. Value iterator_value(Object& iterator_result)
  78. {
  79. return iterator_result.get("value");
  80. }
  81. Value iterator_step(Object& iterator)
  82. {
  83. auto& interpreter = iterator.interpreter();
  84. auto result = iterator_next(iterator);
  85. if (interpreter.exception())
  86. return {};
  87. auto done = is_iterator_complete(result.as_object());
  88. if (interpreter.exception())
  89. return {};
  90. if (done)
  91. return Value(false);
  92. return result;
  93. }
  94. void iterator_close(Object& iterator)
  95. {
  96. (void)iterator;
  97. TODO();
  98. }
  99. Value create_iterator_result_object(Interpreter& interpreter, GlobalObject& global_object, Value value, bool done)
  100. {
  101. auto* object = Object::create_empty(interpreter, global_object);
  102. object->define_property("value", value);
  103. object->define_property("done", Value(done));
  104. return object;
  105. }
  106. }