GeneratorObject.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/GeneratorObjectPrototype.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. auto generating_function_prototype = TRY(generating_function->get(global_object.vm().names.prototype));
  17. auto* generating_function_prototype_object = TRY(generating_function_prototype.to_object(global_object));
  18. auto object = global_object.heap().allocate<GeneratorObject>(global_object, global_object, *generating_function_prototype_object, move(execution_context));
  19. object->m_generating_function = generating_function;
  20. object->m_frame = move(frame);
  21. object->m_previous_value = initial_value;
  22. return object;
  23. }
  24. GeneratorObject::GeneratorObject(GlobalObject&, Object& prototype, ExecutionContext context)
  25. : Object(prototype)
  26. , m_execution_context(move(context))
  27. {
  28. }
  29. void GeneratorObject::initialize(GlobalObject&)
  30. {
  31. }
  32. GeneratorObject::~GeneratorObject()
  33. {
  34. }
  35. void GeneratorObject::visit_edges(Cell::Visitor& visitor)
  36. {
  37. Base::visit_edges(visitor);
  38. visitor.visit(m_generating_function);
  39. visitor.visit(m_previous_value);
  40. }
  41. ThrowCompletionOr<Value> GeneratorObject::next_impl(VM& vm, GlobalObject& global_object, Optional<Value> next_argument, Optional<Value> value_to_throw)
  42. {
  43. auto bytecode_interpreter = Bytecode::Interpreter::current();
  44. VERIFY(bytecode_interpreter);
  45. auto generated_value = [](Value value) -> ThrowCompletionOr<Value> {
  46. if (value.is_object())
  47. return TRY(value.as_object().get("result"));
  48. return value.is_empty() ? js_undefined() : value;
  49. };
  50. auto generated_continuation = [&](Value value) -> ThrowCompletionOr<Bytecode::BasicBlock const*> {
  51. if (value.is_object()) {
  52. auto number_value = TRY(value.as_object().get("continuation"));
  53. return reinterpret_cast<Bytecode::BasicBlock const*>(static_cast<u64>(TRY(number_value.to_double(global_object))));
  54. }
  55. return nullptr;
  56. };
  57. auto previous_generated_value = TRY(generated_value(m_previous_value));
  58. auto result = Object::create(global_object, global_object.object_prototype());
  59. result->define_direct_property("value", previous_generated_value, JS::default_attributes);
  60. if (m_done) {
  61. result->define_direct_property("done", Value(true), JS::default_attributes);
  62. return result;
  63. }
  64. // Extract the continuation
  65. auto next_block = TRY(generated_continuation(m_previous_value));
  66. if (!next_block) {
  67. // The generator has terminated, now we can simply return done=true.
  68. m_done = true;
  69. result->define_direct_property("done", Value(true), JS::default_attributes);
  70. return result;
  71. }
  72. // Make sure it's an actual block
  73. VERIFY(!m_generating_function->bytecode_executable()->basic_blocks.find_if([next_block](auto& block) { return block == next_block; }).is_end());
  74. // Restore the snapshot registers
  75. bytecode_interpreter->enter_frame(m_frame);
  76. // Temporarily switch to the captured execution context
  77. vm.push_execution_context(m_execution_context, global_object);
  78. // Pretend that 'yield' returned the passed value, or threw
  79. if (value_to_throw.has_value()) {
  80. vm.throw_exception(global_object, value_to_throw.release_value());
  81. bytecode_interpreter->accumulator() = js_undefined();
  82. } else {
  83. bytecode_interpreter->accumulator() = next_argument.value_or(js_undefined());
  84. }
  85. auto next_result = bytecode_interpreter->run(*m_generating_function->bytecode_executable(), next_block);
  86. m_frame = move(*bytecode_interpreter->pop_frame());
  87. vm.pop_execution_context();
  88. m_done = TRY(generated_continuation(m_previous_value)) == nullptr;
  89. m_previous_value = TRY(next_result);
  90. result->define_direct_property("value", TRY(generated_value(m_previous_value)), JS::default_attributes);
  91. result->define_direct_property("done", Value(m_done), JS::default_attributes);
  92. return result;
  93. }
  94. }