ladybird/Userland/Libraries/LibJS/Bytecode/Register.h
Andreas Kling 9f06e130a2 LibJS/Bytecode: Keep saved return value in call frame register
This fixes an issue where returning inside a `try` block and then
calling a function inside `finally` would clobber the saved return
value from the `try` block.

Note that we didn't need to change the base of register allocation,
since it was already 1 too high.

With this fixed, https://microsoft.com/edge loads in bytecode mode. :^)

Thanks to Luke for reducing the issue!
2023-07-21 19:15:33 +02:00

52 lines
1.1 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Format.h>
namespace JS::Bytecode {
class Register {
public:
constexpr static u32 accumulator_index = 0;
static constexpr Register accumulator()
{
return Register(accumulator_index);
}
constexpr static u32 saved_return_value_index = 1;
static constexpr Register saved_return_value()
{
return Register(saved_return_value_index);
}
constexpr explicit Register(u32 index)
: m_index(index)
{
}
constexpr bool operator==(Register reg) const { return m_index == reg.index(); }
constexpr u32 index() const { return m_index; }
private:
u32 m_index;
};
}
template<>
struct AK::Formatter<JS::Bytecode::Register> : AK::Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, JS::Bytecode::Register const& value)
{
if (value.index() == JS::Bytecode::Register::accumulator_index)
return builder.put_string("acc"sv);
return AK::Formatter<FormatString>::format(builder, "${}"sv, value.index());
}
};