mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
LibJS: Add conditional expression bytecode generation
Or, by its more common name, the ternary operator :^)
This commit is contained in:
parent
6da587b59b
commit
de3ee701ce
Notes:
sideshowbarker
2024-07-18 12:39:29 +09:00
Author: https://github.com/Lubrsi Commit: https://github.com/SerenityOS/serenity/commit/de3ee701cec Pull-request: https://github.com/SerenityOS/serenity/pull/7907 Reviewed-by: https://github.com/predmond
2 changed files with 20 additions and 0 deletions
|
@ -1176,6 +1176,7 @@ public:
|
|||
|
||||
virtual void dump(int indent) const override;
|
||||
virtual Value execute(Interpreter&, GlobalObject&) const override;
|
||||
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
|
||||
|
||||
private:
|
||||
NonnullRefPtr<Expression> m_test;
|
||||
|
|
|
@ -430,4 +430,23 @@ Optional<Bytecode::Register> DebuggerStatement::generate_bytecode(Bytecode::Gene
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<Bytecode::Register> ConditionalExpression::generate_bytecode(Bytecode::Generator& generator) const
|
||||
{
|
||||
auto result_reg = generator.allocate_register();
|
||||
auto test_reg = m_test->generate_bytecode(generator);
|
||||
auto& alternate_jump = generator.emit<Bytecode::Op::JumpIfFalse>(*test_reg);
|
||||
|
||||
auto consequent_reg = m_consequent->generate_bytecode(generator);
|
||||
generator.emit<Bytecode::Op::LoadRegister>(result_reg, *consequent_reg);
|
||||
auto& end_jump = generator.emit<Bytecode::Op::Jump>();
|
||||
|
||||
alternate_jump.set_target(generator.make_label());
|
||||
auto alternative_reg = m_alternate->generate_bytecode(generator);
|
||||
generator.emit<Bytecode::Op::LoadRegister>(result_reg, *alternative_reg);
|
||||
|
||||
end_jump.set_target(generator.make_label());
|
||||
|
||||
return result_reg;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue