LibJS: Emit GetById instead of GetByValue when key is string literal

Basically convert o["foo"] into o.foo when emitting bytecode. These are
effectively the same thing, and the latter format opts into using an
inline cache for the property lookups.
This commit is contained in:
Andreas Kling 2025-04-03 14:18:24 +02:00
parent ac39343ab5
commit 537b686f3e
3 changed files with 33 additions and 6 deletions

View file

@ -1349,7 +1349,7 @@ static Bytecode::CodeGenerationErrorOr<void> generate_object_binding_pattern_byt
excluded_property_names.append(excluded_name);
}
generator.emit<Bytecode::Op::GetByValue>(value, object, property_name);
generator.emit_get_by_value(value, object, property_name);
}
if (initializer) {
@ -1683,7 +1683,7 @@ static Bytecode::CodeGenerationErrorOr<BaseAndValue> get_base_and_value_from_mem
if (computed_property.has_value()) {
// 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
// FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive!
generator.emit<Bytecode::Op::GetByValueWithThis>(value, super_base, *computed_property, this_value);
generator.emit_get_by_value_with_this(value, super_base, *computed_property, this_value);
} else {
// 3. Let propertyKey be StringValue of IdentifierName.
auto identifier_table_ref = generator.intern_identifier(as<Identifier>(member_expression.property()).string());
@ -1697,7 +1697,7 @@ static Bytecode::CodeGenerationErrorOr<BaseAndValue> get_base_and_value_from_mem
auto value = generator.allocate_register();
if (member_expression.is_computed()) {
auto property = TRY(member_expression.property().generate_bytecode(generator)).value();
generator.emit<Bytecode::Op::GetByValue>(value, base, property);
generator.emit_get_by_value(value, base, property);
} else if (is<PrivateIdentifier>(member_expression.property())) {
generator.emit<Bytecode::Op::GetPrivateById>(
value,
@ -3482,7 +3482,7 @@ static Bytecode::CodeGenerationErrorOr<void> generate_optional_chain(Bytecode::G
[&](OptionalChain::ComputedReference const& ref) -> Bytecode::CodeGenerationErrorOr<void> {
generator.emit_mov(current_base, current_value);
auto property = TRY(ref.expression->generate_bytecode(generator)).value();
generator.emit<Bytecode::Op::GetByValue>(current_value, current_value, property);
generator.emit_get_by_value(current_value, current_value, property);
return {};
},
[&](OptionalChain::MemberReference const& ref) -> Bytecode::CodeGenerationErrorOr<void> {

View file

@ -687,7 +687,7 @@ CodeGenerationErrorOr<Generator::ReferenceOperands> Generator::emit_load_from_re
if (super_reference.referenced_name.has_value()) {
// 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
// FIXME: This does ToPropertyKey out of order, which is observable by Symbol.toPrimitive!
emit<Bytecode::Op::GetByValueWithThis>(dst, *super_reference.base, *super_reference.referenced_name, *super_reference.this_value);
emit_get_by_value_with_this(dst, *super_reference.base, *super_reference.referenced_name, *super_reference.this_value);
} else {
// 3. Let propertyKey be StringValue of IdentifierName.
auto identifier_table_ref = intern_identifier(as<Identifier>(expression.property()).string());
@ -706,7 +706,7 @@ CodeGenerationErrorOr<Generator::ReferenceOperands> Generator::emit_load_from_re
auto saved_property = allocate_register();
emit<Bytecode::Op::Mov>(saved_property, property);
auto dst = preferred_dst.has_value() ? preferred_dst.value() : allocate_register();
emit<Bytecode::Op::GetByValue>(dst, base, property, move(base_identifier));
emit_get_by_value(dst, base, property, move(base_identifier));
return ReferenceOperands {
.base = base,
.referenced_name = saved_property,
@ -1122,6 +1122,30 @@ void Generator::emit_get_by_id_with_this(ScopedOperand dst, ScopedOperand base,
emit<Op::GetByIdWithThis>(dst, base, id, this_value, m_next_property_lookup_cache++);
}
void Generator::emit_get_by_value(ScopedOperand dst, ScopedOperand base, ScopedOperand property, Optional<IdentifierTableIndex> base_identifier)
{
if (property.operand().is_constant() && get_constant(property).is_string()) {
auto property_key = MUST(get_constant(property).to_property_key(vm()));
if (property_key.is_string()) {
emit_get_by_id(dst, base, intern_identifier(property_key.as_string()), base_identifier);
return;
}
}
emit<Op::GetByValue>(dst, base, property, base_identifier);
}
void Generator::emit_get_by_value_with_this(ScopedOperand dst, ScopedOperand base, ScopedOperand property, ScopedOperand this_value)
{
if (property.operand().is_constant() && get_constant(property).is_string()) {
auto property_key = MUST(get_constant(property).to_property_key(vm()));
if (property_key.is_string()) {
emit_get_by_id_with_this(dst, base, intern_identifier(property_key.as_string()), this_value);
return;
}
}
emit<Op::GetByValueWithThis>(dst, base, property, this_value);
}
void Generator::emit_iterator_value(ScopedOperand dst, ScopedOperand result)
{
emit_get_by_id(dst, result, intern_identifier("value"_fly_string));

View file

@ -324,6 +324,9 @@ public:
void emit_get_by_id_with_this(ScopedOperand dst, ScopedOperand base, IdentifierTableIndex, ScopedOperand this_value);
void emit_get_by_value(ScopedOperand dst, ScopedOperand base, ScopedOperand property, Optional<IdentifierTableIndex> base_identifier = {});
void emit_get_by_value_with_this(ScopedOperand dst, ScopedOperand base, ScopedOperand property, ScopedOperand this_value);
void emit_iterator_value(ScopedOperand dst, ScopedOperand result);
void emit_iterator_complete(ScopedOperand dst, ScopedOperand result);