Skip to content

Conversation

@jiwonz
Copy link
Contributor

@jiwonz jiwonz commented Sep 27, 2024

Closes #203

Examples

Input

for i,v in {1,2,3} do
	print(i,v)
end

Output

do local _DARKLUA_REMOVE_GENERALIZED_ITERATION_iterdf0c36eb99574eba, _DARKLUA_REMOVE_GENERALIZED_ITERATION_invardf0c36eb99574eba, _DARKLUA_REMOVE_GENERALIZED_ITERATION_controldf0c36eb99574eba={1,2,3} if type(_DARKLUA_REMOVE_GENERALIZED_ITERATION_iterdf0c36eb99574eba)=='table'then local _m=getmetatable(_DARKLUA_REMOVE_GENERALIZED_ITERATION_iterdf0c36eb99574eba)if type(_m)=='table'and type(_m.__iter)=='function'then _DARKLUA_REMOVE_GENERALIZED_ITERATION_iterdf0c36eb99574eba, _DARKLUA_REMOVE_GENERALIZED_ITERATION_invardf0c36eb99574eba, _DARKLUA_REMOVE_GENERALIZED_ITERATION_controldf0c36eb99574eba=_m.__iter()else _DARKLUA_REMOVE_GENERALIZED_ITERATION_iterdf0c36eb99574eba, _DARKLUA_REMOVE_GENERALIZED_ITERATION_invardf0c36eb99574eba, _DARKLUA_REMOVE_GENERALIZED_ITERATION_controldf0c36eb99574eba=pairs(_DARKLUA_REMOVE_GENERALIZED_ITERATION_iterdf0c36eb99574eba)end end for i,v in _DARKLUA_REMOVE_GENERALIZED_ITERATION_iterdf0c36eb99574eba,_DARKLUA_REMOVE_GENERALIZED_ITERATION_invardf0c36eb99574eba,_DARKLUA_REMOVE_GENERALIZED_ITERATION_controldf0c36eb99574eba do
	print(i,v)
end
end

Notes

  • Hash function blake3 was used
  • Adds RuntimeIdentifier public struct which is for injecting variables in runtime
  • Adds GenericFor.mutate_expressions()

Limitations (Notes from #203)

  1. Using pairs(input) does not necessarily conform to the order of Luau generic iteration (for example, consecutive array indices starting from 1 go first)
  2. Doesn't handle iterating over userdata (with __iter defined), which is supported by Luau
  3. There may also be issues with accessing __iter if __metatable is set

Credits

TO-DOs

  • Add tests (btw, the output was tested in lua5.3)

@phoriah
Copy link

phoriah commented Nov 28, 2025

Wouldn't next, be a more elegant solution? Though I guess there would be an issue with string.gmatch for example, as it returns an iterator.

@jeparlefrancais Also, any updates on this? I use darklua frequently and this would mark a major step towards the luau->lua conversion

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.

A rule for removing (converting) generalized iteration

2 participants