From 416d4e853030d29da9fde1634ea23a0520bb7083 Mon Sep 17 00:00:00 2001 From: Wagyourtail Date: Sun, 14 Jul 2024 12:30:32 -0500 Subject: [PATCH] fix print writer ctor stack fixes #10 --- .../downgradetest/TestPrintWriter.java | 27 +++++++++++++++++++ .../j10/stub/java_base/J_I_PrintWriter.java | 6 ++--- .../wagyourtail/jvmdg/test/ClassRunner.java | 2 +- 3 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestPrintWriter.java diff --git a/downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestPrintWriter.java b/downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestPrintWriter.java new file mode 100644 index 00000000..13766036 --- /dev/null +++ b/downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestPrintWriter.java @@ -0,0 +1,27 @@ +package xyz.wagyourtail.downgradetest; + +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +public class TestPrintWriter { + + + public static void main(String[] args) throws IOException { + PrintWriter out = new PrintWriter(System.out, false, StandardCharsets.UTF_8); + out.println("Hello World!"); + + var temp = Files.createTempFile("temp", "txt"); + out = new PrintWriter(temp.toAbsolutePath().toString(), StandardCharsets.UTF_8); + out.println("Hello World!"); + out.close(); + + out = new PrintWriter(temp.toFile(), StandardCharsets.UTF_8); + out.println("Goodbye World!"); + out.close(); + + Files.deleteIfExists(temp); + } + +} diff --git a/java-api/src/java10/java/xyz/wagyourtail/jvmdg/j10/stub/java_base/J_I_PrintWriter.java b/java-api/src/java10/java/xyz/wagyourtail/jvmdg/j10/stub/java_base/J_I_PrintWriter.java index 6030ab56..b0a9a58e 100644 --- a/java-api/src/java10/java/xyz/wagyourtail/jvmdg/j10/stub/java_base/J_I_PrintWriter.java +++ b/java-api/src/java10/java/xyz/wagyourtail/jvmdg/j10/stub/java_base/J_I_PrintWriter.java @@ -36,12 +36,10 @@ public static void init(MethodNode mnode, int i) { list.add(new TypeInsnNode(Opcodes.NEW, "java/io/BufferedWriter")); // stack: PrintWriter, boolean, OutputStreamWriter, (U) BufferedWriter list.add(new InsnNode(Opcodes.DUP_X2)); - // stack: PrintWriter, boolean, (U) BufferedWriter, OutputStreamWriter, (U) BufferedWriter + // stack: PrintWriter, (U) BufferedWriter, boolean, OutputStreamWriter, (U) BufferedWriter list.add(new InsnNode(Opcodes.SWAP)); - // stack: PrintWriter, boolean, (U) BufferedWriter, (U) BufferedWriter, OutputStreamWriter + // stack: PrintWriter, (U) BufferedWriter, boolean, (U) BufferedWriter, OutputStreamWriter list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/io/BufferedWriter", "", "(Ljava/io/Writer;)V", false)); - // stack: PrintWriter, boolean, BufferedWriter - list.add(new InsnNode(Opcodes.SWAP)); // stack: PrintWriter, BufferedWriter, boolean list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/io/PrintWriter", "", "(Ljava/io/Writer;Z)V", false)); diff --git a/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java b/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java index 8af8ee98..1a001eb7 100644 --- a/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java +++ b/src/test/java/xyz/wagyourtail/jvmdg/test/ClassRunner.java @@ -389,8 +389,8 @@ public void testRuntime(String mainClass, FlagsAndRunner javaVersion) throws IOE } public static void compareResults(String mainClass, FlagsAndRunner javaVersion, Map.Entry originalResult, Map.Entry downgradedResult) { - assertEquals(originalResult.getKey(), downgradedResult.getKey(), "Exit code mismatch for " + mainClass + " on " + javaVersion.readableSlug()); assertEquals(originalResult.getValue(), downgradedResult.getValue(), "Output mismatch for " + mainClass + " on " + javaVersion.readableSlug()); + assertEquals(originalResult.getKey(), downgradedResult.getKey(), "Exit code mismatch for " + mainClass + " on " + javaVersion.readableSlug()); } public record FlagsAndRunner(Flags flags, JavaRunner.JavaVersion targetVersion) {