GeneratorObject.cpp 4.9 KB

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