Get rid of temp files when dealing with assembly results

This commit is contained in:
Brendan Robert
2024-01-25 09:50:48 -06:00
parent 9feb064e83
commit 5e6012dd82
2 changed files with 47 additions and 57 deletions

View File

@@ -7,6 +7,8 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@@ -20,10 +22,10 @@ import java.util.stream.Collectors;
* *
* @author blurry * @author blurry
*/ */
public class AcmeCompiler implements CompileResult<File> { public class AcmeCompiler implements CompileResult<ByteBuffer> {
boolean successful = false; boolean successful = false;
File compiledAsset = null; ByteBuffer compiledAsset = null;
Map<Integer, String> errors = new LinkedHashMap<>(); Map<Integer, String> errors = new LinkedHashMap<>();
Map<Integer, String> warnings = new LinkedHashMap<>(); Map<Integer, String> warnings = new LinkedHashMap<>();
List<String> otherWarnings = new ArrayList<>(); List<String> otherWarnings = new ArrayList<>();
@@ -35,7 +37,7 @@ public class AcmeCompiler implements CompileResult<File> {
} }
@Override @Override
public File getCompiledAsset() { public ByteBuffer getCompiledAsset() {
return compiledAsset; return compiledAsset;
} }
@@ -100,21 +102,26 @@ public class AcmeCompiler implements CompileResult<File> {
private void invokeAcme(File sourceFile, File workingDirectory) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IOException { private void invokeAcme(File sourceFile, File workingDirectory) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IOException {
String oldPath = System.getProperty("user.dir"); String oldPath = System.getProperty("user.dir");
File tempFile = null;
redirectSystemOutput(); redirectSystemOutput();
try { 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()); System.setProperty("user.dir", workingDirectory.getAbsolutePath());
AcmeCrossAssembler acme = new AcmeCrossAssembler(); 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); int status = acme.run("Acme", params);
successful = status == 0; successful = status == 0;
if (!successful) { if (successful) {
compiledAsset.delete(); compiledAsset = ByteBuffer.wrap(Files.readAllBytes(tempFile.toPath()));
compiledAsset = null;
} }
tempFile.delete();
} finally { } finally {
restoreSystemOutput(); restoreSystemOutput();
System.setProperty("user.dir", oldPath); System.setProperty("user.dir", oldPath);
if (tempFile != null && tempFile.exists()) {
tempFile.delete();
}
} }
rawOutput.add("Error output:"); rawOutput.add("Error output:");
extractOutput(baosErr.toString()); extractOutput(baosErr.toString());

View File

@@ -3,6 +3,7 @@ package jace.assembly;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@@ -17,14 +18,14 @@ import jace.ide.Program;
* *
* @author blurry * @author blurry
*/ */
public class AssemblyHandler implements LanguageHandler<File> { public class AssemblyHandler implements LanguageHandler<ByteBuffer> {
@Override @Override
public String getNewDocumentContent() { public String getNewDocumentContent() {
return "\t\t*= $300;\n\t\t!cpu 65c02;\n;--- Insert your code here ---\n"; return "\t\t*= $300;\n\t\t!cpu 65c02;\n;--- Insert your code here ---\n";
} }
@Override @Override
public CompileResult<File> compile(Program proxy) { public CompileResult<ByteBuffer> compile(Program proxy) {
AcmeCompiler compiler = new AcmeCompiler(); AcmeCompiler compiler = new AcmeCompiler();
compiler.compile(proxy); compiler.compile(proxy);
return compiler; return compiler;
@@ -34,69 +35,51 @@ public class AssemblyHandler implements LanguageHandler<File> {
HeadlessProgram prg = new HeadlessProgram(Program.DocumentType.assembly); HeadlessProgram prg = new HeadlessProgram(Program.DocumentType.assembly);
prg.setValue(code); prg.setValue(code);
CompileResult<File> lastResult = compile(prg); CompileResult<ByteBuffer> lastResult = compile(prg);
if (lastResult.isSuccessful()) { if (lastResult.isSuccessful()) {
Emulator.withComputer(c -> { Emulator.withComputer(c -> {
RAM memory = c.getMemory(); RAM memory = c.getMemory();
try { ByteBuffer input = lastResult.getCompiledAsset();
FileInputStream input = new FileInputStream(lastResult.getCompiledAsset()); input.rewind();
int startLSB = input.read(); int startLSB = input.get();
int startMSB = input.read(); int startMSB = input.get();
int start = startLSB + startMSB << 8; int start = startLSB + startMSB << 8;
System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start)); System.out.printf("Storing assembled code to $%s%n", Integer.toHexString(start));
c.getCpu().whileSuspended(() -> { c.getCpu().whileSuspended(() -> {
try { int pos = start;
int pos = start; while (input.hasRemaining()) {
int next; memory.write(pos++, input.get(), false, true);
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);
}
}); });
} }
} }
@Override @Override
public void execute(CompileResult<File> lastResult) { public void execute(CompileResult<ByteBuffer> lastResult) {
if (lastResult.isSuccessful()) { if (lastResult.isSuccessful()) {
Emulator.withComputer(c -> { Emulator.withComputer(c -> {
RAM memory = c.getMemory(); RAM memory = c.getMemory();
try { ByteBuffer input = lastResult.getCompiledAsset();
FileInputStream input = new FileInputStream(lastResult.getCompiledAsset()); input.rewind();
int startLSB = input.read(); int startLSB = input.get();
int startMSB = input.read(); int startMSB = input.get();
int start = startLSB + startMSB << 8; int start = startLSB + startMSB << 8;
System.out.printf("Issuing JSR to $%s%n", Integer.toHexString(start)); System.out.printf("Issuing JSR to $%s%n", Integer.toHexString(start));
c.getCpu().whileSuspended(() -> { c.getCpu().whileSuspended(() -> {
try { int pos = start;
int pos = start; while (input.hasRemaining()) {
int next; memory.write(pos++, input.get(), false, true);
while ((next=input.read()) != -1) { }
memory.write(pos++, (byte) next, false, true); c.getCpu().JSR(start);
} });
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);
}
}); });
} }
clean(lastResult); clean(lastResult);
} }
@Override @Override
public void clean(CompileResult<File> lastResult) { public void clean(CompileResult<ByteBuffer> lastResult) {
if (lastResult.getCompiledAsset() != null) { // Nothing to do here
lastResult.getCompiledAsset().delete();
}
} }
} }