lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>>>>> "Joseph" == Joseph C Sible <josephcsible@gmail.com> writes:

So what's happening, from my analysis, is that the "upval" flag on the
BlockStat for the do..end block is not being set, because the code is
not correctly checking which scope var3 is in. This in turn is because
var1 and var2 both incremented nactvar - understandably since they both
brought names into scope - but were then removed from the variable stack
on the basis that as compile-time constants they require no storage.

So I'm suspicious of this logic in localstat:

      toclose = luaY_nvarstack(fs) + nvars;
  ...
  checktoclose(ls, toclose);

Here "toclose" is being set to the actual index of the to-be-closed var,
which does not take into account the elided <const>s. But the parameter
to checktoclose() is being interpreted as being comparable to nactvar,
which was not adjusted for the elisions; so this is wrong.

I _think_ a suitable fix would be as this attached patch, but I have not
tested it extensively yet. (I have not looked at this part of the code
much before.)

--- src/lparser.c.orig	2020-05-22 22:25:11 UTC
+++ src/lparser.c
@@ -1739,7 +1739,7 @@ static void checktoclose (LexState *ls, 
     FuncState *fs = ls->fs;
     markupval(fs, level + 1);
     fs->bl->insidetbc = 1;  /* in the scope of a to-be-closed variable */
-    luaK_codeABC(fs, OP_TBC, level, 0, 0);
+    luaK_codeABC(fs, OP_TBC, stacklevel(fs, level), 0, 0);
   }
 }
 
@@ -1760,7 +1760,7 @@ static void localstat (LexState *ls) {
     if (kind == RDKTOCLOSE) {  /* to-be-closed? */
       if (toclose != -1)  /* one already present? */
         luaK_semerror(ls, "multiple to-be-closed variables in local list");
-      toclose = luaY_nvarstack(fs) + nvars;
+      toclose = fs->nactvar + nvars;
     }
     nvars++;
   } while (testnext(ls, ','));
-- 
Andrew.