GeneratorObject.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. GeneratorObject::~GeneratorObject()
  41. {
  42. }
  43. void GeneratorObject::visit_edges(Cell::Visitor& visitor)
  44. {
  45. Base::visit_edges(visitor);
  46. visitor.visit(m_generating_function);
  47. visitor.visit(m_previous_value);
  48. }
  49. ThrowCompletionOr<Value> GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optional<Value> next_argument, Optional<Value> value_to_throw)
  50. {
  51. auto bytecode_interpreter = Bytecode::Interpreter::current();
  52. VERIFY(bytecode_interpreter);
  53. auto generated_value = [](Value value) -> ThrowCompletionOr<Value> {
  54. if (value.is_object())
  55. return TRY(value.as_object().get("result"));
  56. return value.is_empty() ? js_undefined() : value;
  57. };
  58. auto generated_continuation = [&](Value value) -> ThrowCompletionOr<Bytecode::BasicBlock const*> {
  59. if (value.is_object()) {
  60. auto number_value = TRY(value.as_object().get("continuation"));
  61. return reinterpret_cast<Bytecode::BasicBlock const*>(static_cast<u64>(TRY(number_value.to_double(global_object))));
  62. }
  63. return nullptr;
  64. };
  65. auto previous_generated_value = TRY(generated_value(m_previous_value));
  66. auto result = Object::create(global_object, global_object.object_prototype());
  67. result->define_direct_property("value", previous_generated_value, JS::default_attributes);
  68. if (m_done) {
  69. result->define_direct_property("done", Value(true), JS::default_attributes);
  70. return result;
  71. }
  72. // Extract the continuation
  73. auto next_block = TRY(generated_continuation(m_previous_value));
  74. if (!next_block) {
  75. // The generator has terminated, now we can simply return done=true.
  76. m_done = true;
  77. result->define_direct_property("done", Value(true), JS::default_attributes);
  78. return result;
  79. }
  80. // Make sure it's an actual block
  81. VERIFY(!m_generating_function->bytecode_executable()->basic_blocks.find_if([next_block](auto& block) { return block == next_block; }).is_end());
  82. // Restore the snapshot registers
  83. bytecode_interpreter->enter_frame(m_frame);
  84. // Temporarily switch to the captured execution context
  85. TRY(vm.push_execution_context(m_execution_context, global_object));
  86. // Pretend that 'yield' returned the passed value, or threw
  87. if (value_to_throw.has_value()) {
  88. vm.throw_exception(global_object, value_to_throw.release_value());
  89. bytecode_interpreter->accumulator() = js_undefined();
  90. } else {
  91. bytecode_interpreter->accumulator() = next_argument.value_or(js_undefined());
  92. }
  93. auto next_result = bytecode_interpreter->run(*m_generating_function->bytecode_executable(), next_block);
  94. m_frame = move(*bytecode_interpreter->pop_frame());
  95. vm.pop_execution_context();
  96. m_done = TRY(generated_continuation(m_previous_value)) == nullptr;
  97. m_previous_value = TRY(next_result);
  98. result->define_direct_property("value", TRY(generated_value(m_previous_value)), JS::default_attributes);
  99. result->define_direct_property("done", Value(m_done), JS::default_attributes);
  100. return result;
  101. }
  102. }