1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:58:11 +00:00

LibJS: Implement generator functions (only in bytecode mode)

This commit is contained in:
Ali Mohammad Pur 2021-06-11 01:38:30 +04:30 committed by Andreas Kling
parent c53a86a3fe
commit 3234697eca
21 changed files with 407 additions and 34 deletions

View file

@ -23,11 +23,28 @@ Generator::~Generator()
{
}
Executable Generator::generate(ASTNode const& node)
Executable Generator::generate(ASTNode const& node, bool is_in_generator_function)
{
Generator generator;
generator.switch_to_basic_block(generator.make_block());
if (is_in_generator_function) {
generator.enter_generator_context();
// Immediately yield with no value.
auto& start_block = generator.make_block();
generator.emit<Bytecode::Op::Yield>(Label { start_block });
generator.switch_to_basic_block(start_block);
}
node.generate_bytecode(generator);
if (is_in_generator_function) {
// Terminate all unterminated blocks with yield return
for (auto& block : generator.m_root_basic_blocks) {
if (block.is_terminated())
continue;
generator.switch_to_basic_block(block);
generator.emit<Bytecode::Op::LoadImmediate>(js_undefined());
generator.emit<Bytecode::Op::Yield>(nullptr);
}
}
return { move(generator.m_root_basic_blocks), move(generator.m_string_table), generator.m_next_register };
}