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

use __index metamethod in ipairs #519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matthias314
Copy link

@matthias314 matthias314 commented Jan 4, 2025

With this PR, GopherLua respects the __index metamethod in ipairs. This change was introduced in Lua 5.3. (Lua 5.2 had an __ipairs metamethod, which was deprecated in 5.3.)

This would be particularly useful in the context of gopher-luar (CC @layeh). To iterate over a Go array or slice t from Lua, one currently has to access the iterator via t() (__call metamethod). This means that one has to write separate code for iterating over Lua lists on the one hand and Go arrays/slices on the other. With this PR, the same code would work for both cases.

Performance: On my machine, summing up the values of an honest Lua list with one million integer entries becomes about 15% slower with this PR. For non-trivial loop bodies the slow-down would obviously be smaller.

EDIT: Iterating over a Go array/slice doesn't work yet because gopher-luar doesn't return nil if one tries to access a non-existing entry, but instead errors. Iterating over a Go map with indices 1, 2, ... does work.

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.

1 participant