diff --git a/Platform/Apple/tools/jace/src/main/java/jace/assembly/AcmeCompiler.java b/Platform/Apple/tools/jace/src/main/java/jace/assembly/AcmeCompiler.java index a25ef1da..f2aade4b 100644 --- a/Platform/Apple/tools/jace/src/main/java/jace/assembly/AcmeCompiler.java +++ b/Platform/Apple/tools/jace/src/main/java/jace/assembly/AcmeCompiler.java @@ -7,6 +7,8 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; +import java.nio.ByteBuffer; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; @@ -20,10 +22,10 @@ import java.util.stream.Collectors; * * @author blurry */ -public class AcmeCompiler implements CompileResult { +public class AcmeCompiler implements CompileResult { boolean successful = false; - File compiledAsset = null; + ByteBuffer compiledAsset = null; Map errors = new LinkedHashMap<>(); Map warnings = new LinkedHashMap<>(); List otherWarnings = new ArrayList<>(); @@ -35,7 +37,7 @@ public class AcmeCompiler implements CompileResult { } @Override - public File getCompiledAsset() { + public ByteBuffer getCompiledAsset() { return compiledAsset; } @@ -100,21 +102,26 @@ public class AcmeCompiler implements CompileResult { private void invokeAcme(File sourceFile, File workingDirectory) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IOException { String oldPath = System.getProperty("user.dir"); + File tempFile = null; redirectSystemOutput(); try { - compiledAsset = File.createTempFile(sourceFile.getName(), "bin", sourceFile.getParentFile()); + tempFile = File.createTempFile(sourceFile.getName(), "bin", sourceFile.getParentFile()); + tempFile.deleteOnExit(); System.setProperty("user.dir", workingDirectory.getAbsolutePath()); AcmeCrossAssembler acme = new AcmeCrossAssembler(); - String[] params = {"--outfile", normalizeWindowsPath(compiledAsset.getAbsolutePath()), "-f", "cbm", "--maxerrors","16",normalizeWindowsPath(sourceFile.getAbsolutePath())}; + String[] params = {"--outfile", normalizeWindowsPath(tempFile.getAbsolutePath()), "-f", "cbm", "--maxerrors","16",normalizeWindowsPath(sourceFile.getAbsolutePath())}; int status = acme.run("Acme", params); successful = status == 0; - if (!successful) { - compiledAsset.delete(); - compiledAsset = null; + if (successful) { + compiledAsset = ByteBuffer.wrap(Files.readAllBytes(tempFile.toPath())); } + tempFile.delete(); } finally { restoreSystemOutput(); System.setProperty("user.dir", oldPath); + if (tempFile != null && tempFile.exists()) { + tempFile.delete(); + } } rawOutput.add("Error output:"); extractOutput(baosErr.toString()); diff --git a/Platform/Apple/tools/jace/src/main/java/jace/assembly/AssemblyHandler.java b/Platform/Apple/tools/jace/src/main/java/jace/assembly/AssemblyHandler.java index bd62aa60..1bae21d7 100644 --- a/Platform/Apple/tools/jace/src/main/java/jace/assembly/AssemblyHandler.java +++ b/Platform/Apple/tools/jace/src/main/java/jace/assembly/AssemblyHandler.java @@ -3,6 +3,7 @@ package jace.assembly; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.logging.Level; import java.util.logging.Logger; @@ -17,14 +18,14 @@ import jace.ide.Program; * * @author blurry */ -public class AssemblyHandler implements LanguageHandler { +public class AssemblyHandler implements LanguageHandler { @Override public String getNewDocumentContent() { return "\t\t*= $300;\n\t\t!cpu 65c02;\n;--- Insert your code here ---\n"; } @Override - public CompileResult compile(Program proxy) { + public CompileResult compile(Program proxy) { AcmeCompiler compiler = new AcmeCompiler(); compiler.compile(proxy); return compiler; @@ -34,69 +35,51 @@ public class AssemblyHandler implements LanguageHandler { HeadlessProgram prg = new HeadlessProgram(Program.DocumentType.assembly); prg.setValue(code); - CompileResult lastResult = compile(prg); + CompileResult lastResult = compile(prg); if (lastResult.isSuccessful()) { Emulator.withComputer(c -> { RAM memory = c.getMemory(); - try { - FileInputStream input = new FileInputStream(lastResult.getCompiledAsset()); - int startLSB = input.read(); - int startMSB = input.read(); - int start = startLSB + startMSB << 8; - System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start)); - c.getCpu().whileSuspended(() -> { - try { - int pos = start; - int next; - while ((next=input.read()) != -1) { - memory.write(pos++, (byte) next, false, true); - } - } catch (IOException ex) { - Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex); - } - }); - } catch (IOException ex) { - Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex); - } + ByteBuffer input = lastResult.getCompiledAsset(); + input.rewind(); + int startLSB = input.get(); + int startMSB = input.get(); + int start = startLSB + startMSB << 8; + System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start)); + c.getCpu().whileSuspended(() -> { + int pos = start; + while (input.hasRemaining()) { + memory.write(pos++, input.get(), false, true); + } + }); }); } } @Override - public void execute(CompileResult lastResult) { + public void execute(CompileResult lastResult) { if (lastResult.isSuccessful()) { Emulator.withComputer(c -> { RAM memory = c.getMemory(); - try { - FileInputStream input = new FileInputStream(lastResult.getCompiledAsset()); - int startLSB = input.read(); - int startMSB = input.read(); - int start = startLSB + startMSB << 8; - System.out.printf("Issuing JSR to $%s%n", Integer.toHexString(start)); - c.getCpu().whileSuspended(() -> { - try { - int pos = start; - int next; - while ((next=input.read()) != -1) { - memory.write(pos++, (byte) next, false, true); - } - c.getCpu().JSR(start); - } catch (IOException ex) { - Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex); - } - }); - } catch (IOException ex) { - Logger.getLogger(AssemblyHandler.class.getName()).log(Level.SEVERE, null, ex); - } + ByteBuffer input = lastResult.getCompiledAsset(); + input.rewind(); + int startLSB = input.get(); + int startMSB = input.get(); + int start = startLSB + startMSB << 8; + System.out.printf("Issuing JSR to $%s%n", Integer.toHexString(start)); + c.getCpu().whileSuspended(() -> { + int pos = start; + while (input.hasRemaining()) { + memory.write(pos++, input.get(), false, true); + } + c.getCpu().JSR(start); + }); }); } clean(lastResult); } @Override - public void clean(CompileResult lastResult) { - if (lastResult.getCompiledAsset() != null) { - lastResult.getCompiledAsset().delete(); - } + public void clean(CompileResult lastResult) { + // Nothing to do here } }