1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +00:00

LibJS: Implement bytecode generation for all ObjectExpression properties

This commit is contained in:
Ali Mohammad Pur 2022-03-31 00:59:58 +04:30 committed by Andreas Kling
parent 698fb3957a
commit 007ffcd763
3 changed files with 94 additions and 17 deletions

View file

@ -817,25 +817,42 @@ Bytecode::CodeGenerationErrorOr<void> ObjectExpression::generate_bytecode(Byteco
generator.emit<Bytecode::Op::Store>(object_reg);
for (auto& property : m_properties) {
if (property.type() != ObjectProperty::Type::KeyValue)
return Bytecode::CodeGenerationError {
this,
"Unimplemented property kind"sv,
};
Bytecode::Op::PropertyKind property_kind;
switch (property.type()) {
case ObjectProperty::Type::KeyValue:
property_kind = Bytecode::Op::PropertyKind::KeyValue;
break;
case ObjectProperty::Type::Getter:
property_kind = Bytecode::Op::PropertyKind::Getter;
break;
case ObjectProperty::Type::Setter:
property_kind = Bytecode::Op::PropertyKind::Setter;
break;
case ObjectProperty::Type::Spread:
property_kind = Bytecode::Op::PropertyKind::Spread;
break;
case ObjectProperty::Type::ProtoSetter:
property_kind = Bytecode::Op::PropertyKind::ProtoSetter;
break;
}
if (is<StringLiteral>(property.key())) {
auto& string_literal = static_cast<StringLiteral const&>(property.key());
Bytecode::IdentifierTableIndex key_name = generator.intern_identifier(string_literal.value());
TRY(property.value().generate_bytecode(generator));
generator.emit<Bytecode::Op::PutById>(object_reg, key_name);
if (property_kind != Bytecode::Op::PropertyKind::Spread)
TRY(property.value().generate_bytecode(generator));
generator.emit<Bytecode::Op::PutById>(object_reg, key_name, property_kind);
} else {
TRY(property.key().generate_bytecode(generator));
auto property_reg = generator.allocate_register();
generator.emit<Bytecode::Op::Store>(property_reg);
TRY(property.value().generate_bytecode(generator));
generator.emit<Bytecode::Op::PutByValue>(object_reg, property_reg);
if (property_kind != Bytecode::Op::PropertyKind::Spread)
TRY(property.value().generate_bytecode(generator));
generator.emit<Bytecode::Op::PutByValue>(object_reg, property_reg, property_kind);
}
}