changeset 471:54c9e0385bf0

Strict scoping for while
author tehtmi
date Sun, 26 Dec 2021 16:28:32 -0800
parents 42661a45ad6f
children 666dcdce3bc2
files src/unluac/decompile/ControlFlowHandler.java src/unluac/decompile/block/WhileBlock.java src/unluac/decompile/block/WhileBlock50.java src/unluac/decompile/block/WhileBlock51.java src/unluac/test/TestFiles.java test/src/close08.lua
diffstat 6 files changed, 22 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/unluac/decompile/ControlFlowHandler.java	Sun Dec 26 15:44:09 2021 -0800
+++ b/src/unluac/decompile/ControlFlowHandler.java	Sun Dec 26 16:28:32 2021 -0800
@@ -637,7 +637,10 @@
           b.targetSecond = end;
           remove_branch(state, b);
           //System.err.println("while " + b.targetFirst + " " + b.targetSecond);
-          loop = new WhileBlock51(state.function, b.cond, b.targetFirst, b.targetSecond, loopback);
+          loop = new WhileBlock51(
+            state.function, b.cond, b.targetFirst, b.targetSecond, loopback,
+            get_close_type(state, end - 2), end - 2
+          );
           unredirect(state, loopback, end, j.line, loopback);
         }
         if(loop == null && j.line - 5 >= 1 && state.code.op(j.line - 3) == Op.CLOSE
@@ -703,7 +706,10 @@
                   headb = null;
                 }
                 if(headb != null) {
-                  block = new WhileBlock50(state.function, b.cond.inverse(), head + 1, b.targetFirst, headb.targetFirst);
+                  block = new WhileBlock50(
+                    state.function, b.cond.inverse(), head + 1, b.targetFirst, headb.targetFirst,
+                    get_close_type(state, headb.targetFirst - 1), headb.targetFirst - 1
+                  );
                   remove_branch(state, headb);
                   unredirect(state, 1, headb.line, headb.line, headb.targetSecond);
                 }
--- a/src/unluac/decompile/block/WhileBlock.java	Sun Dec 26 15:44:09 2021 -0800
+++ b/src/unluac/decompile/block/WhileBlock.java	Sun Dec 26 16:28:32 2021 -0800
@@ -16,8 +16,8 @@
   
   private Expression condexpr;
   
-  public WhileBlock(LFunction function, Condition cond, int begin, int end, int closeLine) {
-    super(function, begin, end, CloseType.NONE, closeLine, -1);
+  public WhileBlock(LFunction function, Condition cond, int begin, int end, CloseType closeType, int closeLine) {
+    super(function, begin, end, closeType, closeLine, -1);
     this.cond = cond;
   }
   
--- a/src/unluac/decompile/block/WhileBlock50.java	Sun Dec 26 15:44:09 2021 -0800
+++ b/src/unluac/decompile/block/WhileBlock50.java	Sun Dec 26 16:28:32 2021 -0800
@@ -1,5 +1,6 @@
 package unluac.decompile.block;
 
+import unluac.decompile.CloseType;
 import unluac.decompile.condition.Condition;
 import unluac.parse.LFunction;
 
@@ -7,14 +8,14 @@
 
   private final int enterTarget;
   
-  public WhileBlock50(LFunction function, Condition cond, int begin, int end, int enterTarget) {
-    super(function, cond, begin, end, -1);
+  public WhileBlock50(LFunction function, Condition cond, int begin, int end, int enterTarget, CloseType closeType, int closeLine) {
+    super(function, cond, begin, end, closeType, closeLine);
     this.enterTarget = enterTarget;
   }
   
   @Override
   public int scopeEnd() {
-    return enterTarget - 1;
+    return usingClose && closeType != CloseType.NONE ? closeLine - 1 : enterTarget - 1;
   }
   
   @Override
--- a/src/unluac/decompile/block/WhileBlock51.java	Sun Dec 26 15:44:09 2021 -0800
+++ b/src/unluac/decompile/block/WhileBlock51.java	Sun Dec 26 16:28:32 2021 -0800
@@ -8,14 +8,14 @@
 
   private final int unprotectedTarget;
   
-  public WhileBlock51(LFunction function, Condition cond, int begin, int end, int unprotectedTarget) {
-    super(function, cond, begin, end, -1);
+  public WhileBlock51(LFunction function, Condition cond, int begin, int end, int unprotectedTarget, CloseType closeType, int closeLine) {
+    super(function, cond, begin, end, closeType, closeLine);
     this.unprotectedTarget = unprotectedTarget;
   }
   
   @Override
   public int scopeEnd() {
-    return end - 2;
+    return usingClose && closeType == CloseType.CLOSE ? end - 3 : end - 2;
   }
   
   @Override
--- a/src/unluac/test/TestFiles.java	Sun Dec 26 15:44:09 2021 -0800
+++ b/src/unluac/test/TestFiles.java	Sun Dec 26 16:28:32 2021 -0800
@@ -212,6 +212,7 @@
     new TestFile("close05"),
     new TestFile("close06"),
     new TestFile("close07"),
+    new TestFile("close08"),
     new TestFile("always01"),
     new TestFile("always02"),
     new TestFile("always03"),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/src/close08.lua	Sun Dec 26 16:28:32 2021 -0800
@@ -0,0 +1,4 @@
+while a do
+  local x
+  function f() return x end
+end