Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using next as a iterator is broken in the custom lua VM #52

Open
RaspyPiStyLiS opened this issue Sep 11, 2021 · 1 comment
Open

Using next as a iterator is broken in the custom lua VM #52

RaspyPiStyLiS opened this issue Sep 11, 2021 · 1 comment
Labels
bug Something isn't working glua Regarding gopher-lua

Comments

@RaspyPiStyLiS
Copy link

RaspyPiStyLiS commented Sep 11, 2021

This script

local tbl = {1,2,3,4,5}
for i,v in next, tbl do
	print(i,v)
end

Will produce the following output

4       4
5       5

Expected output:

  1 1
  2 2
  3 3
  4 4
  5 5
@Anaminus Anaminus added bug Something isn't working glua Regarding gopher-lua labels Sep 12, 2021
@Anaminus
Copy link
Owner

When happens here is that the construction of tbl leaves 1, 2, 3, 4, and 5 on the stack. Then, to prepare for the generic-for operation, 1 is replaced by next, 2 is replaced by tbl, while 3, 4, and 5 are left alone. The problem is that the generic-for always looks for three values, which in this case will be next, tbl and 3, which turns into next(tbl, 3). To fix this, the compiler needs to ensure that, if a third expression is not specified, it is filled in with nil.

For now, you can work around this by specifying the nil explicitly:

local tbl = {1,2,3,4,5}
for i,v in next, tbl, nil do
	print(i,v)
end
--> 1	1
--> 2	2
--> 3	3
--> 4	4
--> 5	5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working glua Regarding gopher-lua
Projects
None yet
Development

No branches or pull requests

2 participants