From 7005a91a1e4be702e860c117fc5d5d27ab0a2233 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Sun, 25 Jun 2023 12:31:49 +0200 Subject: [PATCH] LibJS: Replace invalid escapes in TemplateLiterals with undefined in BC Also adds a big FIXME, to outline what is still wrong --- Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 3ae5528a20b..7dfcd480c4b 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -1902,6 +1902,12 @@ Bytecode::CodeGenerationErrorOr TaggedTemplateLiteral::generate_bytecode(B // FIXME: We only need to record the first and last register, // due to packing everything in an array, same goes for argument_regs + // FIXME: Follow + // 13.2.8.3 GetTemplateObject ( templateLiteral ), https://tc39.es/ecma262/#sec-gettemplateobject + // more closely, namely: + // * cache this somehow + // * add a raw object accessor + // * freeze array and raw member Vector string_regs; auto& expressions = m_template_literal->expressions(); for (size_t i = 0; i < expressions.size(); ++i) { @@ -1914,8 +1920,14 @@ Bytecode::CodeGenerationErrorOr TaggedTemplateLiteral::generate_bytecode(B for (size_t i = 0; i < expressions.size(); ++i) { if (i % 2 != 0) continue; + // NOTE: If the string contains invalid escapes we get a null expression here, + // which we then convert to the expected `undefined` TV. See + // 12.9.6.1 Static Semantics: TV, https://tc39.es/ecma262/#sec-static-semantics-tv + if (is(expressions[i])) + generator.emit(js_undefined()); + else + TRY(expressions[i]->generate_bytecode(generator)); - TRY(expressions[i]->generate_bytecode(generator)); auto string_reg = string_regs[reg_index++]; generator.emit(string_reg); }