Skip to content

Commit eb2deab

Browse files
authored
Merge pull request #498 from Pieter12345/master
Fix set_uncaught_exception_handler closure return behaviour
2 parents 992c273 + 8f6aea5 commit eb2deab

File tree

4 files changed

+661
-657
lines changed

4 files changed

+661
-657
lines changed

src/main/java/com/laytonsmith/PureUtilities/ZipReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ private void initList() throws IOException {
339339
}
340340
if(this.zipEntries == null) {
341341
zipEntries = new ArrayList<>();
342-
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(topZip))){
342+
try(ZipInputStream zis = new ZipInputStream(new FileInputStream(topZip))) {
343343
ZipEntry entry;
344344
while((entry = zis.getNextEntry()) != null) {
345345
File f = new File(topZip, entry.getName());
@@ -440,8 +440,8 @@ public void recursiveCopy(File dstFolder, boolean create) throws IOException {
440440
} else {
441441
File newFile = new File(dstFolder, r.file.getName());
442442
newFile.getParentFile().mkdir();
443-
try (FileOutputStream fos = new FileOutputStream(newFile, false);
444-
InputStream fis = r.getInputStream()){
443+
try(FileOutputStream fos = new FileOutputStream(newFile, false);
444+
InputStream fis = r.getInputStream()) {
445445
StreamUtils.Copy(fis, fos);
446446
}
447447
}

src/main/java/com/laytonsmith/core/exceptions/ConfigRuntimeException.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,35 @@ public static enum Reaction {
8686
* @return
8787
*/
8888
public static Reaction GetReaction(ConfigRuntimeException e, Environment env) {
89-
//If there is an exception handler, call it to see what it says.
90-
Reaction reaction = Reaction.REPORT;
89+
90+
// If there is an exception handler, call it to see what it says.
9191
if(env.getEnv(GlobalEnv.class).GetExceptionHandler() != null) {
9292
CClosure c = env.getEnv(GlobalEnv.class).GetExceptionHandler();
9393
CArray ex = ObjectGenerator.GetGenerator().exception(e, env, Target.UNKNOWN);
9494
if(e.getEnv() != null) {
9595
MCCommandSender sender = e.getEnv().getEnv(CommandHelperEnvironment.class).GetCommandSender();
9696
c.getEnv().getEnv(CommandHelperEnvironment.class).SetCommandSender(sender);
9797
}
98-
Construct ret = CNull.NULL;
9998
try {
10099
c.execute(new Construct[]{ex});
100+
return Reaction.REPORT; // Closure returned nothing -> REPORT.
101101
} catch (FunctionReturnException retException) {
102-
ret = retException.getReturn();
103-
}
104-
if(ret instanceof CNull || Prefs.ScreamErrors()) {
105-
reaction = Reaction.REPORT;
106-
} else {
107-
if(Static.getBoolean(ret, Target.UNKNOWN)) {
108-
reaction = Reaction.IGNORE;
102+
Construct ret = retException.getReturn();
103+
if(ret instanceof CNull || Prefs.ScreamErrors()) {
104+
return Reaction.REPORT; // Closure returned null or scream-errors was set in the config.
109105
} else {
110-
reaction = Reaction.FATAL;
106+
// Closure returned a boolean. TRUE -> IGNORE and FALSE -> FATAL.
107+
return (Static.getBoolean(ret, Target.UNKNOWN) ? Reaction.IGNORE : Reaction.FATAL);
111108
}
109+
} catch (ConfigRuntimeException cre) {
110+
111+
// A CRE occurred in the exception handler. Report both exceptions.
112+
HandleUncaughtException(cre, env, Reaction.REPORT);
113+
return Reaction.REPORT;
112114
}
115+
} else {
116+
return Reaction.REPORT; // No exception handler set -> REPORT.
113117
}
114-
return reaction;
115118
}
116119

117120
/**
@@ -166,8 +169,6 @@ private static void HandleUncaughtException(ConfigRuntimeException e, Environmen
166169
ConfigRuntimeException.DoReport(e, env);
167170
} else if(r == ConfigRuntimeException.Reaction.FATAL) {
168171
ConfigRuntimeException.DoReport(e, env);
169-
//Well, here goes nothing
170-
throw e;
171172
}
172173
}
173174

@@ -529,7 +530,7 @@ public CArray getObjectFor() {
529530
if(getDefinedAt().file() != null) {
530531
name = getDefinedAt().file().getAbsolutePath();
531532
}
532-
element.set("file", getDefinedAt().file().getAbsolutePath());
533+
element.set("file", name);
533534
}
534535
element.set("line", new CInt(getDefinedAt().line(), Target.UNKNOWN), Target.UNKNOWN);
535536
return element;

src/main/java/com/laytonsmith/tools/Interpreter.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,8 @@ public static void install() {
850850
if(null == OSUtils.GetOS()) {
851851
StreamUtils.GetSystemErr().println("Cmdline MethodScript is only supported on Unix and Windows");
852852
return;
853-
} else switch(OSUtils.GetOS()) {
853+
}
854+
switch(OSUtils.GetOS()) {
854855
case LINUX:
855856
case MAC:
856857
try {
@@ -883,7 +884,8 @@ public static void install() {
883884
} catch (IOException e) {
884885
StreamUtils.GetSystemErr().println("Cannot install. You must run the command with sudo for it to succeed, however, did you do that?");
885886
return;
886-
} break;
887+
}
888+
break;
887889
case WINDOWS:
888890
Path tmp = null;
889891
try {
@@ -892,13 +894,13 @@ public static void install() {
892894
ZipReader zReader = new ZipReader(root);
893895
tmp = Files.createTempDirectory("methodscript-installer", new FileAttribute[]{});
894896
zReader.recursiveCopy(tmp.toFile(), false);
895-
897+
896898
// 2. Write the location of this jar to the registry
897899
String me = ClassDiscovery.GetClassContainer(Interpreter.class).toExternalForm().substring(6);
898900
String keyName = "Software\\MethodScript";
899901
WinRegistry.createKey(WinRegistry.HKEY_CURRENT_USER, keyName);
900902
WinRegistry.writeStringValue(WinRegistry.HKEY_CURRENT_USER, keyName, "JarLocation", me);
901-
903+
902904
// 3. Execute the setup.exe file
903905
File setup = new File(tmp.toFile(), "setup.exe");
904906
int setupResult = new CommandExecutor(new String[]{setup.getAbsolutePath()}).start().waitFor();
@@ -908,10 +910,11 @@ public static void install() {
908910
} else {
909911
StreamUtils.GetSystemOut().println("Setup has begun. Finish the installation in the GUI.");
910912
}
911-
} catch(IOException | InterruptedException | IllegalAccessException | InvocationTargetException ex) {
913+
} catch (IOException | InterruptedException | IllegalAccessException | InvocationTargetException ex) {
912914
ex.printStackTrace(StreamUtils.GetSystemErr());
913915
System.exit(1);
914-
} break;
916+
}
917+
break;
915918
}
916919
StreamUtils.GetSystemOut().println("MethodScript has successfully been installed on your system. Note that you may need to rerun the install command"
917920
+ " if you change locations of the jar, or rename it. Be sure to put \"#!" + INTERPRETER_INSTALLATION_LOCATION + "\" at the top of all your scripts,"
@@ -924,7 +927,8 @@ public static void uninstall() {
924927
if(null == OSUtils.GetOS()) {
925928
StreamUtils.GetSystemErr().println("Sorry, cmdline functionality is currently only supported on unix systems! Check back soon though!");
926929
return;
927-
} else switch(OSUtils.GetOS()) {
930+
}
931+
switch(OSUtils.GetOS()) {
928932
case LINUX:
929933
case MAC:
930934
try {
@@ -935,7 +939,8 @@ public static void uninstall() {
935939
} catch (IOException e) {
936940
StreamUtils.GetSystemErr().println("Cannot uninstall. You must run the command with sudo for it to succeed, however, did you do that?");
937941
return;
938-
} break;
942+
}
943+
break;
939944
case WINDOWS:
940945
StreamUtils.GetSystemOut().println("To uninstall on windows, please uninstall from the Add or Remove Programs application.");
941946
return;

0 commit comments

Comments
 (0)