Truly independent web browser
LibJS: Convert a template literal's only constant substitution
Shannon Booth392fb9a9a48a
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rsindex ec635cf674e..dff785472e5 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs@@ -5808,9 +5808,9 @@ fn generate_template_literal(if segments.len() == 1 {let val = generate_expression(segments[0], generator, None)?;- // If it's a constant, return directly.- if val.operand().is_constant() {- return Some(val);+ // OPTIMIZATION: Convert a constant at compile time.+ if let Some(string) = generator.get_constant(&val).and_then(constant_to_string) {+ return Some(generator.add_constant_string(string));}// Otherwise, emit ToString.generator.emit(Instruction::ToString {diff --git a/Tests/LibJS/Runtime/template-literals.js b/Tests/LibJS/Runtime/template-literals.jsindex 4c682cd76d9..45f9f8906d6 100644--- a/Tests/LibJS/Runtime/template-literals.js+++ b/Tests/LibJS/Runtime/template-literals.js@@ -22,6 +22,14 @@ test("literals in expressions", () => {expect(`foo ${"bar"}`).toBe("foo bar");});+test("a single literal substitution becomes a string", () => {+ expect(`${1}`).toBe("1");+ expect(`${null}`).toBe("null");+ expect(`${undefined}`).toBe("undefined");+ expect(`${true}`).toBe("true");+ expect(`${10n}`).toBe("10");+});+test("objects in expressions", () => {expect(`foo ${{}}`).toBe("foo [object Object]");expect(`foo ${{ bar: { baz: "qux" } }}`).toBe("foo [object Object]");