Skip to content

Commit e387dc4

Browse files
committed
Merge branch 'master' into release-2.6.1
2 parents 6c86bfd + 37e6cfd commit e387dc4

File tree

409 files changed

+166
-78198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

409 files changed

+166
-78198
lines changed

HackPackage/src/main/java/Hack/Controller/HackController.java

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
/**
3939
* A Controller for HackSimulators. Executes scripts written in a special scripting language
40-
* that controlls the features of the simulators.
40+
* that controls the features of the simulators.
4141
* Constructed with a GUI that enables the execution control of the script.
4242
*/
4343
public class HackController
@@ -56,7 +56,7 @@ public class HackController
5656
/**
5757
* The speed function for fast forward mode.
5858
*/
59-
public static final int[] FASTFORWARD_SPEED_FUNCTION = {500, 1000, 2000, 4000, 15000};
59+
private static final int[] FASTFORWARD_SPEED_FUNCTION = {500, 1000, 2000, 4000, 15000};
6060

6161
// ANIMATION MODES:
6262

@@ -127,6 +127,10 @@ public class HackController
127127
// A helper string with spaces
128128
private static final String SPACES = " ";
129129
private static final String DIRECTORY = "directory";
130+
private static final String SPEED = "speed";
131+
private static final String ANIMATION_MODE = "animation_mode";
132+
private static final String NUMERIC_FORMAT = "numeric_format";
133+
private final Preferences preferences;
130134

131135
// The controller's GUI
132136
protected ControllerGUI gui;
@@ -229,6 +233,7 @@ public class HackController
229233
* The script will be executed and the final result will be printed.
230234
*/
231235
public HackController(HackSimulator simulator, String scriptFileName) {
236+
this.preferences = Preferences.userNodeForPackage(simulator.getClass());
232237
File file = new File(scriptFileName);
233238
if (!file.exists())
234239
displayMessage(scriptFileName + " doesn't exist", true);
@@ -241,10 +246,8 @@ public HackController(HackSimulator simulator, String scriptFileName) {
241246
try {
242247
loadNewScript(file, false);
243248
saveWorkingDir(file);
244-
} catch (ScriptException se) {
249+
} catch (ScriptException | ControllerException se) {
245250
displayMessage(se.getMessage(), true);
246-
} catch (ControllerException ce) {
247-
displayMessage(ce.getMessage(), true);
248251
}
249252

250253
fastForwardRunning = true;
@@ -260,6 +263,7 @@ public HackController(HackSimulator simulator, String scriptFileName) {
260263
public HackController(ControllerGUI gui, HackSimulator simulator, String defaultScriptName)
261264
throws ScriptException, ControllerException {
262265

266+
this.preferences = Preferences.userNodeForPackage(simulator.getClass());
263267
this.gui = gui;
264268
this.simulator = simulator;
265269
singleStepTask = new SingleStepTask();
@@ -276,26 +280,27 @@ public HackController(ControllerGUI gui, HackSimulator simulator, String default
276280
for (int i = 0; i < NUMBER_OF_SPEED_UNITS; i++)
277281
delays[i] = (int)(MAX_MS - SPEED_FUNCTION[i] * (float)(MAX_MS - MIN_MS));
278282

279-
currentSpeedUnit = INITIAL_SPEED_UNIT;
280-
animationMode = simulator.getInitialAnimationMode();
283+
currentSpeedUnit = preferences.getInt(SPEED, INITIAL_SPEED_UNIT);
284+
animationMode = preferences.getInt(ANIMATION_MODE, simulator.getInitialAnimationMode());
281285
simulator.setAnimationMode(animationMode);
282-
simulator.setAnimationSpeed(INITIAL_SPEED_UNIT);
283-
simulator.setNumericFormat(simulator.getInitialNumericFormat());
286+
simulator.setAnimationSpeed(currentSpeedUnit);
287+
final int numericFormat = preferences.getInt(NUMERIC_FORMAT, simulator.getInitialNumericFormat());
288+
simulator.setNumericFormat(numericFormat);
284289
timer = new Timer(delays[currentSpeedUnit - 1], this);
285290

286291
// adds the simulator component to the controller component
287292
gui.setSimulator(simulator.getGUI());
288293
gui.setTitle(simulator.getName() + getVersionString());
289294

290295
// load and set working dir
291-
File file = loadWorkingDir();
296+
File file = new File(preferences.get(DIRECTORY, "."));
292297
simulator.setWorkingDir(file);
293298
gui.setWorkingDir(file);
294299

295300
gui.addControllerListener(this);
296301
gui.setSpeed(currentSpeedUnit);
297302
gui.setAnimationMode(animationMode);
298-
gui.setNumericFormat(simulator.getInitialNumericFormat());
303+
gui.setNumericFormat(numericFormat);
299304
gui.setAdditionalDisplay(simulator.getInitialAdditionalDisplay());
300305
gui.setVariables(simulator.getVariables());
301306

@@ -443,14 +448,8 @@ else if (breakpoint.isReached()) {
443448
if (breakpointReached)
444449
tempBreakpoints.clear();
445450

446-
} catch (ControllerException ce) {
451+
} catch (ControllerException | ProgramException | CommandException | VariableException ce) {
447452
stopWithError(ce);
448-
} catch (ProgramException pe) {
449-
stopWithError(pe);
450-
} catch (CommandException ce) {
451-
stopWithError(ce);
452-
} catch (VariableException ve) {
453-
stopWithError(ve);
454453
}
455454

456455
singleStepLocked = false;
@@ -736,7 +735,7 @@ private void outputAndCompare(String line) throws ControllerException {
736735
}
737736

738737
// loads the given script file and restarts the GUI.
739-
protected void loadNewScript(File file, boolean displayMessage)
738+
private void loadNewScript(File file, boolean displayMessage)
740739
throws ControllerException, ScriptException {
741740
currentScriptFile = file;
742741
script = new Script(file.getPath());
@@ -793,6 +792,8 @@ private void setSpeed(int newSpeedUnit) {
793792
currentSpeedUnit = newSpeedUnit;
794793
timer.setDelay(delays[currentSpeedUnit - 1]);
795794
simulator.setAnimationSpeed(newSpeedUnit);
795+
preferences.putInt(SPEED, newSpeedUnit);
796+
savePreferences();
796797
}
797798

798799
// Sets the animation mode with the given one.
@@ -806,12 +807,16 @@ private void setAnimationMode(int newAnimationMode) {
806807

807808
gui.setAnimationMode(newAnimationMode);
808809
animationMode = newAnimationMode;
810+
preferences.putInt(ANIMATION_MODE, newAnimationMode);
811+
savePreferences();
809812
}
810813

811814
// Sets the numeric format with the given code.
812815
private void setNumericFormat(int formatCode) {
813816
simulator.setNumericFormat(formatCode);
814817
gui.setNumericFormat(formatCode);
818+
preferences.putInt(NUMERIC_FORMAT, formatCode);
819+
savePreferences();
815820
}
816821

817822
// Sets the additional display with the given code.
@@ -865,15 +870,8 @@ private void displayMessage(String message, boolean error) {
865870
}
866871
}
867872

868-
// Returns the working dir that is saved in the data file, or "" if data file doesn't exist.
869-
protected File loadWorkingDir() {
870-
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
871-
final String dir = preferences.get(DIRECTORY, ".");
872-
return new File(dir);
873-
}
874-
875873
// Saves the given working dir into the data file and gui's.
876-
protected void saveWorkingDir(File file) {
874+
private void saveWorkingDir(File file) {
877875
final File parent = file.getParentFile();
878876

879877
if (gui != null)
@@ -883,8 +881,11 @@ protected void saveWorkingDir(File file) {
883881

884882
final File dir = file.isDirectory() ? file : parent;
885883

886-
final Preferences preferences = Preferences.userNodeForPackage(simulator.getClass());
887884
preferences.put(DIRECTORY, dir.toString());
885+
savePreferences();
886+
}
887+
888+
private void savePreferences() {
888889
try {
889890
preferences.sync();
890891
} catch (BackingStoreException ignored) {
@@ -904,8 +905,7 @@ protected void reloadDefaultScript() {
904905
try {
905906
loadNewScript(defaultScriptFile, false);
906907
rewind();
907-
} catch (ScriptException ignored) {
908-
} catch (ControllerException ignored) {
908+
} catch (ScriptException | ControllerException ignored) {
909909
}
910910
}
911911
}
@@ -1061,10 +1061,7 @@ public void actionPerformed(ControllerEvent event) {
10611061
doUnknownAction(event.getAction(), event.getData());
10621062
break;
10631063
}
1064-
} catch (ScriptException e) {
1065-
displayMessage(e.getMessage(), true);
1066-
stopMode();
1067-
} catch (ControllerException e) {
1064+
} catch (ScriptException | ControllerException e) {
10681065
displayMessage(e.getMessage(), true);
10691066
stopMode();
10701067
}
@@ -1077,7 +1074,7 @@ protected void doUnknownAction(byte action, Object data) throws ControllerExcept
10771074
}
10781075

10791076
// Performs the single step task
1080-
class SingleStepTask implements Runnable {
1077+
private class SingleStepTask implements Runnable {
10811078

10821079
public void run() {
10831080
singleStep();
@@ -1100,7 +1097,7 @@ public void run() {
11001097
}
11011098

11021099
// Performs the fast forward task
1103-
class FastForwardTask implements Runnable {
1100+
private class FastForwardTask implements Runnable {
11041101
public synchronized void run() {
11051102
try {
11061103
System.runFinalization();
@@ -1132,7 +1129,7 @@ public synchronized void run() {
11321129
}
11331130

11341131
// Sets the animation mode
1135-
class SetAnimationModeTask implements Runnable {
1132+
private class SetAnimationModeTask implements Runnable {
11361133

11371134
private int animationMode;
11381135

@@ -1146,7 +1143,7 @@ public void run() {
11461143
}
11471144

11481145
// Sets the numeric format
1149-
class SetNumericFormatTask implements Runnable {
1146+
private class SetNumericFormatTask implements Runnable {
11501147

11511148
private int numericFormat;
11521149

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) {

0 commit comments

Comments
 (0)