Browse Source

sgoto with invalid label is logged correctly

Kajetan Johannes Hammerle 2 years ago
parent
commit
c950deb271

+ 7 - 2
src/me/hammerle/snuviscript/code/FunctionRegistry.java

@@ -488,12 +488,17 @@ public class FunctionRegistry {
                 throw new IllegalArgumentException("time units can't be negative");
             }
             String label = in[1].getString(sc);
+            int line = sc.getLine();
             sc.getScriptManager().getScheduler().scheduleTask(() -> {
                 if(sc.shouldTerm() || sc.isHolded()) {
                     return;
                 }
-                sc.gotoLabel(label, true, 1);
-                sc.run();
+                try {
+                    sc.gotoLabel(label, true, 1);
+                    sc.run();
+                } catch(Exception ex) {
+                    sc.logException(ex, "sgoto", line);
+                }
                 if(sc.shouldTerm()) {
                     sc.getScriptManager().removeScript(sc);
                 }

+ 16 - 5
src/me/hammerle/snuviscript/code/Script.java

@@ -79,6 +79,21 @@ public final class Script {
         }
     }
 
+    public void logException(Exception ex, String instructionName, int line) {
+        if(stackTrace) {
+            ex.printStackTrace();
+        }
+        scriptManager.getLogger().print(null, ex, instructionName, name, this,
+                new StackTrace(line, returnStack, code));
+    }
+
+    public int getLine() {
+        if(lineIndex < 0 || lineIndex >= code.length) {
+            return -1;
+        }
+        return code[lineIndex].getLine();
+    }
+
     public void run() {
         isWaiting = false;
         // System.out.println("_________________________");
@@ -99,11 +114,7 @@ public final class Script {
                 // System.out.println("AFTER EXECUTE: " + dataStack);
                 lineIndex++;
             } catch(Exception ex) {
-                if(stackTrace) {
-                    ex.printStackTrace();
-                }
-                scriptManager.getLogger().print(null, ex, instr.getName(), name, this,
-                        new StackTrace(instr.getLine(), returnStack, code));
+                logException(ex, instr.getName(), instr.getLine());
                 break;
             }