GeneratorObject.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TemporaryChange.h>
  7. #include <LibJS/Bytecode/Generator.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibJS/Runtime/GeneratorObject.h>
  10. #include <LibJS/Runtime/GeneratorPrototype.h>
  11. #include <LibJS/Runtime/GlobalObject.h>
  12. namespace JS {
  13. ThrowCompletionOr<GeneratorObject*> GeneratorObject::create(GlobalObject& global_object, Value initial_value, ECMAScriptFunctionObject* generating_function, ExecutionContext execution_context, Bytecode::RegisterWindow frame)
  14. {
  15. auto& realm = *global_object.associated_realm();
  16. // This is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
  17. Value generating_function_prototype;
  18. if (generating_function->kind() == FunctionKind::Async) {
  19. // We implement async functions by transforming them to generator function in the bytecode
  20. // interpreter. However an async function does not have a prototype and should not be
  21. // changed thus we hardcode the prototype.
  22. generating_function_prototype = global_object.generator_prototype();
  23. } else {
  24. generating_function_prototype = TRY(generating_function->get(global_object.vm().names.prototype));
  25. }
  26. auto* generating_function_prototype_object = TRY(generating_function_prototype.to_object(global_object));
  27. auto object = global_object.heap().allocate<GeneratorObject>(global_object, realm, *generating_function_prototype_object, move(execution_context));
  28. object->m_generating_function = generating_function;
  29. object->m_frame = move(frame);
  30. object->m_previous_value = initial_value;
  31. return object;
  32. }
  33. GeneratorObject::GeneratorObject(Realm&, Object& prototype, ExecutionContext context)
  34. : Object(prototype)
  35. , m_execution_context(move(context))
  36. {
  37. }
  38. void GeneratorObject::initialize(Realm&)
  39. {
  40. }
  41. void GeneratorObject::visit_edges(Cell::Visitor& visitor)
  42. {
  43. Base::visit_edges(visitor);
  44. visitor.visit(m_generating_function);
  45. visitor.visit(m_previous_value);
  46. }
  47. ThrowCompletionOr<Value> GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optional<Value> next_argument, Optional<Value> value_to_throw)
  48. {
  49. auto bytecode_interpreter = Bytecode::Interpreter::current();
  50. VERIFY(bytecode_interpreter);
  51. auto generated_value = [](Value value) -> ThrowCompletionOr<Value> {
  52. if (value.is_object())
  53. return TRY(value.as_object().get("result"));
  54. return value.is_empty() ? js_undefined() : value;
  55. };
  56. auto generated_continuation = [&](Value value) -> ThrowCompletionOr<Bytecode::BasicBlock const*> {
  57. if (value.is_object()) {
  58. auto number_value = TRY(value.as_object().get("continuation"));
  59. return reinterpret_cast<Bytecode::BasicBlock const*>(static_cast<u64>(TRY(number_value.to_double(global_object))));
  60. }
  61. return nullptr;
  62. };
  63. auto previous_generated_value = TRY(generated_value(m_previous_value));
  64. auto result = Object::create(global_object, global_object.object_prototype());
  65. result->define_direct_property("value", previous_generated_value, default_attributes);
  66. if (m_done) {
  67. result->define_direct_property("done", Value(true), default_attributes);
  68. return result;
  69. }
  70. // Extract the continuation
  71. auto next_block = TRY(generated_continuation(m_previous_value));
  72. if (!next_block) {
  73. // The generator has terminated, now we can simply return done=true.
  74. m_done = true;
  75. result->define_direct_property("done", Value(true), default_attributes);
  76. return result;
  77. }
  78. // Make sure it's an actual block
  79. VERIFY(!m_generating_function->bytecode_executable()->basic_blocks.find_if([next_block](auto& block) { return block == next_block; }).is_end());
  80. // Temporarily switch to the captured execution context
  81. TRY(vm.push_execution_context(m_execution_context, global_object));
  82. // Pretend that 'yield' returned the passed value, or threw
  83. if (value_to_throw.has_value()) {
  84. bytecode_interpreter->accumulator() = js_undefined();
  85. return throw_completion(value_to_throw.release_value());
  86. }
  87. Bytecode::RegisterWindow* frame = nullptr;
  88. if (m_frame.has_value())
  89. frame = &m_frame.value();
  90. auto next_value = next_argument.value_or(js_undefined());
  91. if (frame)
  92. frame->registers[0] = next_value;
  93. else
  94. bytecode_interpreter->accumulator() = next_value;
  95. auto next_result = bytecode_interpreter->run_and_return_frame(*m_generating_function->bytecode_executable(), next_block, frame);
  96. vm.pop_execution_context();
  97. if (!m_frame.has_value())
  98. m_frame = move(*next_result.frame);
  99. m_previous_value = TRY(move(next_result.value));
  100. m_done = TRY(generated_continuation(m_previous_value)) == nullptr;
  101. result->define_direct_property("value", TRY(generated_value(m_previous_value)), default_attributes);
  102. result->define_direct_property("done", Value(m_done), default_attributes);
  103. return result;
  104. }
  105. }