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 {}; }