LibJS: Implement bytecode generation for UpdateExpression :^)

Added Increment and Decrement bytecode ops to support this. Postfix
updates use a temporary register to preserve the original value.

Note that this patch only implements Identifier updates. Member
expression updates are a TODO.
This commit is contained in:
Andreas Kling 2021-06-09 11:40:38 +02:00
parent 5c98f979c6
commit 59eedd6de0
Notes: sideshowbarker 2024-07-18 12:34:29 +09:00
5 changed files with 88 additions and 1 deletions

View file

@ -638,4 +638,32 @@ void TemplateLiteral::generate_bytecode(Bytecode::Generator& generator) const
}
}
}
void UpdateExpression::generate_bytecode(Bytecode::Generator& generator) const
{
if (is<Identifier>(*m_argument)) {
auto& identifier = static_cast<Identifier const&>(*m_argument);
generator.emit<Bytecode::Op::GetVariable>(identifier.string());
Optional<Bytecode::Register> previous_value_for_postfix_reg;
if (!m_prefixed) {
previous_value_for_postfix_reg = generator.allocate_register();
generator.emit<Bytecode::Op::Store>(*previous_value_for_postfix_reg);
}
if (m_op == UpdateOp::Increment)
generator.emit<Bytecode::Op::Increment>();
else
generator.emit<Bytecode::Op::Decrement>();
generator.emit<Bytecode::Op::SetVariable>(identifier.string());
if (!m_prefixed)
generator.emit<Bytecode::Op::Load>(*previous_value_for_postfix_reg);
return;
}
TODO();
}
}