David Kastrup
Why? At \endgroup, tex.count.stackIndex would be reset to the value it had at the beginning of the group (since it is a tex \count register, not a Lua variable). Then, after \begingroup, setting a variable would notice that tex.count.groupLevel (which would also have been reset to its previous value at \endgroup) is lower than tex.currentgrouplevel, and a new stack element would be created, replacing the previous element for this grouping level.
Ok, the group housekeeping of the TeX counters should do the trick here.
Anyway, I am not convinced that this poor man's equivalence table for Lua is a sane approach to the problem, regardless whether tex.currentgrouplevel might be nice to have or not. -- David Kastrup, Kriemhildstr. 15, 44793 Bochum