Skip to content

Commit 57b3d33

Browse files
committed
Set the BuiltInFunctions thread as deamon
1 parent 28fa49e commit 57b3d33

File tree

5 files changed

+93
-73
lines changed

5 files changed

+93
-73
lines changed

InstallDir/VMEmulator.sh

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
#!/bin/sh
2-
cd `dirname $0`
3-
java -jar ../VMEmulator/target/VMEmulator-*.jar $@
1+
#!/bin/bash
2+
cd $(dirname $0)
3+
if [[ $# -eq 0 ]]; then
4+
java -jar ../VMEmulator/target/VMEmulator-*.jar
5+
elif [[ $# -eq 1 ]]; then
6+
[ -z "$N2T_VM_USE_BUILTINS" ] && export N2T_VM_USE_BUILTINS="yes"
7+
java -jar ../VMEmulator/target/VMEmulator-*.jar "$1"
8+
else
9+
echo "The program expects 0 or 1 arguments!"
10+
exit 1
11+
fi

SimulatorsPackage/src/main/java/Hack/VMEmulator/BuiltInFunctionsRunner.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
import Hack.Controller.ProgramException;
2121
import Hack.Utilities.Definitions;
22+
2223
import java.io.File;
23-
import java.lang.reflect.*;
24+
import java.lang.reflect.InvocationTargetException;
25+
import java.lang.reflect.Method;
2426

2527
/**
2628
* A class that runs built-in VM code as a coroutine so that
@@ -89,7 +91,9 @@ public BuiltInFunctionsRunner(CPU cpu, File builtInDir) {
8991
builtInToProgram = new BuiltInToProgramRequest();
9092
programToBuiltIn = new ProgramToBuiltInRequest();
9193
synchronized (this) {
92-
new Thread(this).start();
94+
final Thread thread = new Thread(this, "built-in-functions");
95+
thread.setDaemon(true);
96+
thread.start();
9397
continueOtherThread(); // Let the built-in code runner init itself
9498
// The notify part of this call does nothing
9599
}

SimulatorsPackage/src/main/java/Hack/VMEmulator/VMEmulator.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -451,28 +451,30 @@ public void doCommand(String[] command)
451451
hideHighlights();
452452

453453
// execute the appropriate command
454-
if (command[0].equals(COMMAND_VMSTEP)) {
455-
if (command.length != 1)
456-
throw new CommandException("Illegal number of arguments to command", command);
457-
458-
cpu.executeInstruction();
459-
}
460-
else if (command[0].equals(COMMAND_SETVAR)) {
461-
if (command.length != 3)
462-
throw new CommandException("Illegal number of arguments to command", command);
463-
setValue(command[1], command[2]);
464-
}
465-
else if (command[0].equals(COMMAND_ROMLOAD)) {
466-
if (command.length != 1 && command.length != 2)
467-
throw new CommandException("Illegal number of arguments to command", command);
468-
469-
String fileName = workingDir + (command.length == 1 ? "" : "/" + command[1]);
470-
471-
cpu.getProgram().loadProgram(fileName);
472-
cpu.boot();
454+
switch (command[0]) {
455+
case COMMAND_VMSTEP:
456+
if (command.length != 1)
457+
throw new CommandException("Illegal number of arguments to command", command);
458+
459+
cpu.executeInstruction();
460+
break;
461+
case COMMAND_SETVAR:
462+
if (command.length != 3)
463+
throw new CommandException("Illegal number of arguments to command", command);
464+
setValue(command[1], command[2]);
465+
break;
466+
case COMMAND_ROMLOAD:
467+
if (command.length != 1 && command.length != 2)
468+
throw new CommandException("Illegal number of arguments to command", command);
469+
470+
String fileName = workingDir + (command.length == 1 ? "" : "/" + command[1]);
471+
472+
cpu.getProgram().loadProgram(fileName);
473+
cpu.boot();
474+
break;
475+
default:
476+
throw new CommandException("Unknown simulator command", command);
473477
}
474-
else
475-
throw new CommandException("Unknown simulator command", command);
476478
}
477479

478480
// Hides all highlights in GUIs.
@@ -635,7 +637,7 @@ public Breakpoint genStepOverBreakpoint() {
635637
}
636638

637639
/**
638-
* Called when an error occured in a computer part.
640+
* Called when an error occurred in a computer part.
639641
* The event contains the source object and the error message.
640642
*/
641643
public void computerPartErrorOccured(ComputerPartErrorEvent event) {

SimulatorsPackage/src/main/java/Hack/VMEmulator/VMProgram.java

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@
3535
/**
3636
* A list of VM instructions, with a program counter.
3737
*/
38-
public class VMProgram extends InteractiveComputerPart
39-
implements ProgramEventListener {
38+
class VMProgram extends InteractiveComputerPart implements ProgramEventListener {
4039

4140
// pseudo address for returning to built-in functions
42-
public static final short BUILTIN_FUNCTION_ADDRESS = -1;
41+
static final short BUILTIN_FUNCTION_ADDRESS = -1;
4342

4443
// Possible values for the current status - has the user allowed
4544
// access to built-in vm functions?
@@ -86,12 +85,12 @@ public class VMProgram extends InteractiveComputerPart
8685
/**
8786
* Constructs a new empty program with the given GUI.
8887
*/
89-
public VMProgram(VMProgramGUI gui) {
88+
VMProgram(VMProgramGUI gui) {
9089
super(gui != null);
9190
this.gui = gui;
92-
listeners = new Vector<ProgramEventListener>();
93-
staticRange = new Hashtable<String, Object>();
94-
functions = new Hashtable<String, Short>();
91+
listeners = new Vector<>();
92+
staticRange = new Hashtable<>();
93+
functions = new Hashtable<>();
9594

9695
if (hasGUI) {
9796
assert gui != null;
@@ -132,7 +131,7 @@ public void loadProgram(String fileName) throws ProgramException {
132131
staticRange.clear();
133132
functions.clear();
134133
builtInAccessStatus = BUILTIN_ACCESS_UNDECIDED;
135-
Hashtable<String, Short> symbols = new Hashtable<String, Short>();
134+
Hashtable<String, Short> symbols = new Hashtable<>();
136135
nextPC = 0;
137136
for (File f : files) {
138137
String name = f.getName();
@@ -534,7 +533,7 @@ private String unCommentLine(String line) {
534533
* form of a 2-elements array {startAddress, endAddress}.
535534
* If unknown class name, returns null.
536535
*/
537-
public int[] getStaticRange(String className) {
536+
int[] getStaticRange(String className) {
538537
return (int[])staticRange.get(className);
539538
}
540539

@@ -545,7 +544,7 @@ public int getSize() {
545544
return instructionsLength;
546545
}
547546

548-
public short getAddress(String functionName) throws ProgramException {
547+
short getAddress(String functionName) throws ProgramException {
549548
Short address = functions.get(functionName);
550549
if (address != null) {
551550
return address;
@@ -580,28 +579,28 @@ public short getAddress(String functionName) throws ProgramException {
580579
/**
581580
* Returns the next program counter.
582581
*/
583-
public short getPC() {
582+
short getPC() {
584583
return nextPC;
585584
}
586585

587586
/**
588587
* Returns the current value of the program counter.
589588
*/
590-
public short getCurrentPC() {
589+
short getCurrentPC() {
591590
return currentPC;
592591
}
593592

594593
/**
595594
* Returns the previous value of the program counter.
596595
*/
597-
public short getPreviousPC() {
596+
short getPreviousPC() {
598597
return prevPC;
599598
}
600599

601600
/**
602601
* Sets the program counter with the given address.
603602
*/
604-
public void setPC(short address) {
603+
void setPC(short address) {
605604
prevPC = currentPC;
606605
currentPC = nextPC;
607606
nextPC = address;
@@ -618,7 +617,7 @@ public void setPC(short address) {
618617
* stop an infinite loop in a built-in jack class.
619618
* A message containing information may be provided (can be null).
620619
*/
621-
public void setPCToInfiniteLoopForBuiltIns(String message) {
620+
void setPCToInfiniteLoopForBuiltIns(String message) {
622621
if (hasGUI) {
623622
gui.notify(message);
624623
}
@@ -629,7 +628,7 @@ public void setPCToInfiniteLoopForBuiltIns(String message) {
629628
* Returns the next VMEmulatorInstruction and increments the PC by one.
630629
* The PC will be incremented by more if the next instruction is a label.
631630
*/
632-
public VMEmulatorInstruction getNextInstruction() {
631+
VMEmulatorInstruction getNextInstruction() {
633632
VMEmulatorInstruction result = null;
634633

635634
if (nextPC < instructionsLength) {
@@ -645,7 +644,7 @@ public VMEmulatorInstruction getNextInstruction() {
645644
return result;
646645
}
647646

648-
public short getNextInstructionAddress(short pc) {
647+
short getNextInstructionAddress(short pc) {
649648
do {
650649
pc++;
651650
} while (pc < instructionsLength && instructions[pc].getOpCode() == HVMInstructionSet.LABEL_CODE);
@@ -655,7 +654,7 @@ public short getNextInstructionAddress(short pc) {
655654
/**
656655
* Restarts the program from the beginning.
657656
*/
658-
public void restartProgram() {
657+
void restartProgram() {
659658
currentPC = -999;
660659
prevPC = -999;
661660
nextPC = startAddress;
@@ -713,16 +712,16 @@ private void setGUIPC() {
713712
gui.setCurrentInstruction(nextPC);
714713
}
715714

716-
public VMEmulatorInstruction getInstructionAt(short pc) {
715+
VMEmulatorInstruction getInstructionAt(short pc) {
717716
return pc >= 0 && pc < instructions.length ? instructions[pc] : null;
718717
}
719718

720719
// The task that loads a new program into the emulator
721-
class LoadProgramTask implements Runnable {
720+
private class LoadProgramTask implements Runnable {
722721

723722
private String fileName;
724723

725-
public LoadProgramTask(String fileName) {
724+
LoadProgramTask(String fileName) {
726725
this.fileName = fileName;
727726
}
728727

@@ -746,7 +745,7 @@ public void refreshGUI() {
746745
/**
747746
* Registers the given ProgramEventListener as a listener to this GUI.
748747
*/
749-
public void addProgramListener(ProgramEventListener listener) {
748+
void addProgramListener(ProgramEventListener listener) {
750749
listeners.add(listener);
751750
}
752751

@@ -755,7 +754,7 @@ public void addProgramListener(ProgramEventListener listener) {
755754
* a ProgramEvent (with the new event type and program's file name) and sending it using the
756755
* programChanged function to all the listeners.
757756
*/
758-
protected void notifyProgramListeners(byte eventType, String programFileName) {
757+
private void notifyProgramListeners(byte eventType, String programFileName) {
759758
ProgramEvent event = new ProgramEvent(this, eventType, programFileName);
760759

761760
for (ProgramEventListener listener : listeners)

VMEmulator/src/main/java/VMEmulatorMain.java

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,44 @@
1515
* mark your changes clearly, for the benefit of others. *
1616
********************************************************************************/
1717

18-
import Hack.Controller.*;
19-
import Hack.VMEmulator.*;
20-
import HackGUI.*;
21-
import SimulatorsGUI.*;
18+
import Hack.Controller.ControllerGUI;
19+
import Hack.Controller.HackController;
20+
import Hack.VMEmulator.VMEmulator;
21+
import Hack.VMEmulator.VMEmulatorApplication;
22+
import Hack.VMEmulator.VMEmulatorGUI;
23+
import HackGUI.ControllerComponent;
24+
import SimulatorsGUI.VMEmulatorComponent;
25+
2226
import javax.swing.*;
2327

2428
/**
2529
* The VM Emulator.
2630
*/
27-
public class VMEmulatorMain
28-
{
29-
/**
30-
* The command line VM Emulator program.
31-
*/
32-
public static void main(String[] args) {
33-
if (args.length > 1)
34-
System.err.println("Usage: java CPUEmulatorMain [script name]");
35-
else if (args.length == 0) {
36-
try {
37-
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
38-
} catch (Exception e) {
39-
}
31+
public class VMEmulatorMain {
32+
/**
33+
* The command line VM Emulator program.
34+
*/
35+
public static void main(String[] args) {
36+
switch (args.length) {
37+
case 0:
38+
try {
39+
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
40+
} catch (Exception ignored) {
41+
}
4042

41-
VMEmulatorGUI simulatorGUI = new VMEmulatorComponent();
42-
ControllerGUI controllerGUI = new ControllerComponent();
43-
VMEmulatorApplication application =
43+
final VMEmulatorGUI simulatorGUI = new VMEmulatorComponent();
44+
final ControllerGUI controllerGUI = new ControllerComponent();
4445
new VMEmulatorApplication(controllerGUI, simulatorGUI, "bin/scripts/defaultVM.txt",
45-
"bin/help/vmUsage.html", "bin/help/vmAbout.html");
46+
"bin/help/vmUsage.html", "bin/help/vmAbout.html");
47+
break;
48+
49+
case 1:
50+
new HackController(new VMEmulator(), args[0]);
51+
break;
52+
53+
default:
54+
System.err.println("Usage: java CPUEmulatorMain [script name]");
4655
}
47-
else
48-
new HackController(new VMEmulator(), args[0]);
4956
}
5057
}
5158

0 commit comments

Comments
 (0)