mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 00:58:12 +00:00
LibJS: Add GetById bytecode instruction for object property retrieval
Same as PutById but in the other direction. :^)
This commit is contained in:
parent
14cfc44855
commit
32561bb90d
4 changed files with 45 additions and 0 deletions
|
@ -1119,6 +1119,7 @@ public:
|
||||||
virtual Value execute(Interpreter&, GlobalObject&) const override;
|
virtual Value execute(Interpreter&, GlobalObject&) const override;
|
||||||
virtual void dump(int indent) const override;
|
virtual void dump(int indent) const override;
|
||||||
virtual Reference to_reference(Interpreter&, GlobalObject&) 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; }
|
bool is_computed() const { return m_computed; }
|
||||||
const Expression& object() const { return *m_object; }
|
const Expression& object() const { return *m_object; }
|
||||||
|
|
|
@ -141,4 +141,18 @@ Optional<Bytecode::Register> ObjectExpression::generate_bytecode(Bytecode::Gener
|
||||||
return reg;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,12 @@ void SetVariable::execute(Bytecode::Interpreter& interpreter) const
|
||||||
interpreter.vm().set_variable(m_identifier, interpreter.reg(m_src), interpreter.global_object());
|
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
|
void PutById::execute(Bytecode::Interpreter& interpreter) const
|
||||||
{
|
{
|
||||||
if (auto* object = interpreter.reg(m_base).to_object(interpreter.global_object()))
|
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);
|
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
|
String Jump::to_string() const
|
||||||
{
|
{
|
||||||
return String::formatted("Jump {}", m_target);
|
return String::formatted("Jump {}", m_target);
|
||||||
|
|
|
@ -174,6 +174,25 @@ private:
|
||||||
FlyString m_identifier;
|
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 {
|
class PutById final : public Instruction {
|
||||||
public:
|
public:
|
||||||
PutById(Register base, FlyString property, Register src)
|
PutById(Register base, FlyString property, Register src)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue