Skip to content

Conversation

faelys
Copy link

@faelys faelys commented Mar 22, 2025

Fixes #514

Changes proposed in this pull request:

  • In compileRegAssignment, use nvars as the assignment cardinality rather than lennames.
  • Remove no-longer-used names argument

I don't really know what I'm doing, I just tried to infer what things were supposed to be doing based on their names. I started with the test case filed in #514 and compared with the behavior of official Lua 5.1. It turned out that luac has extra LOADNIL which reset the registers that weren't correctly reset by gopher-lua, so I poked around compileGenericForStmt and it took me a while to understand why the iteration variable names were fed to compileRegAssignment when its place in the code and the other arguments suggested it should be about initializing internal generator/state/control triplet rather than iteration variables.

I checked that after this change, the opcodes generated by gopher-lua are similar to those from luac, and the whole test suite passes.

Use `nvars` as the assignment numbers rather than `lennames`.

This makes no difference in `compileLocalAssignStmt` as the numbers are
equal, but in `compileGenericForStmt` the names are the iteration
variables while `nvars` refers to the local generator, state, and
control. The iteration variables feel a bit out of place here.

The whole body of `compileRegAssignment` seem inconsistent, using
`names` only for its length, and mixing `lennames` and `nvars` in
surprising ways.

Moreover the function name `compileRegAssignment` suggests assigning to
registers rather than names, so I dropped `lennames` entirely to use
only `nvars`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

for loop misbehavior

1 participant