LibJS: Add GetById bytecode instruction for object property retrieval

Same as PutById but in the other direction. :^)
This commit is contained in:
Andreas Kling 2021-06-04 21:03:53 +02:00
parent 14cfc44855
commit 32561bb90d
Notes: sideshowbarker 2024-07-18 12:42:15 +09:00
4 changed files with 45 additions and 0 deletions

View file

@ -1119,6 +1119,7 @@ public:
virtual Value execute(Interpreter&, GlobalObject&) const override;
virtual void dump(int indent) const override;
virtual Reference to_reference(Interpreter&, GlobalObject&) const override;
virtual Optional<Bytecode::Register> generate_bytecode(Bytecode::Generator&) const override;
bool is_computed() const { return m_computed; }
const Expression& object() const { return *m_object; }

View file

@ -141,4 +141,18 @@ Optional<Bytecode::Register> ObjectExpression::generate_bytecode(Bytecode::Gener
return reg;
}
Optional<Bytecode::Register> MemberExpression::generate_bytecode(Bytecode::Generator& generator) const
{
auto object_reg = object().generate_bytecode(generator);
if (is_computed()) {
TODO();
} else {
VERIFY(is<Identifier>(property()));
auto dst_reg = generator.allocate_register();
generator.emit<Bytecode::Op::GetById>(dst_reg, *object_reg, static_cast<Identifier const&>(property()).string());
return dst_reg;
}
}
}

View file

@ -56,6 +56,12 @@ void SetVariable::execute(Bytecode::Interpreter& interpreter) const
interpreter.vm().set_variable(m_identifier, interpreter.reg(m_src), interpreter.global_object());
}
void GetById::execute(Bytecode::Interpreter& interpreter) const
{
if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
interpreter.reg(m_dst) = object->get(m_property);
}
void PutById::execute(Bytecode::Interpreter& interpreter) const
{
if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
@ -133,6 +139,11 @@ String PutById::to_string() const
return String::formatted("PutById base:{}, property:{}, src:{}", m_base, m_property, m_src);
}
String GetById::to_string() const
{
return String::formatted("GetById dst:{}, base:{}, property:{}", m_dst, m_base, m_property);
}
String Jump::to_string() const
{
return String::formatted("Jump {}", m_target);

View file

@ -174,6 +174,25 @@ private:
FlyString m_identifier;
};
class GetById final : public Instruction {
public:
GetById(Register dst, Register base, FlyString property)
: m_dst(dst)
, m_base(base)
, m_property(move(property))
{
}
virtual ~GetById() override { }
virtual void execute(Bytecode::Interpreter&) const override;
virtual String to_string() const override;
private:
Register m_dst;
Register m_base;
FlyString m_property;
};
class PutById final : public Instruction {
public:
PutById(Register base, FlyString property, Register src)