From 0df7255fe726e0fb3366f27712011a7a27f7fb11 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Sun, 2 Jul 2023 10:48:17 +0200 Subject: [PATCH] LibJS/Bytecode: Do not clobber completion value with VariableDeclaration While the completion value of a variable declaration is specified to be empty, we might already have a completion value in the accumulator from a previous statement. Preserve it so as to avoid clobbering it. This fixes 6 tests on test262. --- Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 3a555b10a6..5e962af277 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -1353,6 +1353,11 @@ static Bytecode::CodeGenerationErrorOr assign_accumulator_to_variable_decl Bytecode::CodeGenerationErrorOr VariableDeclaration::generate_bytecode(Bytecode::Generator& generator) const { + // The completion value of a VariableDeclaration is empty, but there might already be a non-empty + // completion value in the accumulator. We need to save it and restore it after the declaration executed. + auto saved_accumulator = generator.allocate_register(); + generator.emit(saved_accumulator); + for (auto& declarator : m_declarations) { if (declarator->init()) { if (auto const* lhs = declarator->target().get_pointer>()) { @@ -1367,6 +1372,7 @@ Bytecode::CodeGenerationErrorOr VariableDeclaration::generate_bytecode(Byt } } + generator.emit(saved_accumulator); return {}; }