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

@ -231,6 +231,30 @@ void Return::execute(Bytecode::Interpreter& interpreter) const
interpreter.do_return(interpreter.accumulator().value_or(js_undefined()));
}
void Increment::execute(Bytecode::Interpreter& interpreter) const
{
auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
if (interpreter.vm().exception())
return;
if (old_value.is_number())
interpreter.accumulator() = Value(old_value.as_double() + 1);
else
interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().plus(Crypto::SignedBigInteger { 1 }));
}
void Decrement::execute(Bytecode::Interpreter& interpreter) const
{
auto old_value = interpreter.accumulator().to_numeric(interpreter.global_object());
if (interpreter.vm().exception())
return;
if (old_value.is_number())
interpreter.accumulator() = Value(old_value.as_double() - 1);
else
interpreter.accumulator() = js_bigint(interpreter.vm().heap(), old_value.as_bigint().big_integer().minus(Crypto::SignedBigInteger { 1 }));
}
String Load::to_string() const
{
return String::formatted("Load {}", m_src);
@ -349,4 +373,14 @@ String Return::to_string() const
return "Return";
}
String Increment::to_string() const
{
return "Increment";
}
String Decrement::to_string() const
{
return "Decrement";
}
}