mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 12:49:19 +00:00
LibJS: Implement generator functions (only in bytecode mode)
This commit is contained in:
parent
c53a86a3fe
commit
3234697eca
Notes:
sideshowbarker
2024-07-18 12:27:59 +09:00
Author: https://github.com/alimpfard
Commit: 3234697eca
Pull-request: https://github.com/SerenityOS/serenity/pull/7977
Reviewed-by: https://github.com/awesomekling
21 changed files with 407 additions and 34 deletions
|
@ -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 };
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue