Skip to content

Truly independent web browser

LibJS: Fold strict equality of well-known symbols

Shannon Boothf45fc34a711b
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
index 5f8d098edbe..8f6408d483e 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
@@ -10388,6 +10388,7 @@ fn try_constant_fold_binary(
(ConstantValue::Boolean(a), ConstantValue::Boolean(b)) => a == b,
(ConstantValue::Null, ConstantValue::Null) => true,
(ConstantValue::Undefined, ConstantValue::Undefined) => true,
+ (ConstantValue::WellKnownSymbol(a), ConstantValue::WellKnownSymbol(b)) => a == b,
// Different types are never strictly equal.
_ => false,
};
@@ -10571,6 +10572,21 @@ mod tests {
generator
}
+ #[test]
+ fn constant_fold_compares_well_known_symbols_by_identity() {
+ let mut generator = Generator::new();
+ let lhs = generator.add_constant_well_known_symbol(WellKnownSymbolKind::SymbolIterator);
+ let rhs = generator.add_constant_well_known_symbol(WellKnownSymbolKind::SymbolIterator);
+
+ let result = try_constant_fold_binary(&mut generator, BinaryOp::StrictlyEquals, &lhs, &rhs)
+ .expect("well-known symbol equality should fold");
+
+ assert!(matches!(
+ generator.get_constant(&result),
+ Some(ConstantValue::Boolean(true))
+ ));
+ }
+
#[test]
fn builtin_intrinsic_lowering_respects_declared_global_shadows() {
let (parsed, expression) = parse_builtin_expression("let ToBoolean; ToBoolean(0);", 1);
diff --git a/Libraries/LibJS/Rust/src/bytecode/constant.rs b/Libraries/LibJS/Rust/src/bytecode/constant.rs
index bc1720c4b3a..98576e130bd 100644
--- a/Libraries/LibJS/Rust/src/bytecode/constant.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/constant.rs
@@ -9,7 +9,7 @@
/// Well-known symbol IDs that the runtime resolves when materializing an Executable.
#[repr(u8)]
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WellKnownSymbolKind {
SymbolIterator = 0,
SymbolAsyncIterator = 1,