Skip to content

Commit

Permalink
Preserve ordering of generated Lua code
Browse files Browse the repository at this point in the history
This fixes #1645, and (partially) reverts 68ac28.
  • Loading branch information
brndnmtthws committed Oct 13, 2023
1 parent 23a1ad3 commit 59da849
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
45 changes: 45 additions & 0 deletions 3rdparty/toluapp/src/bin/lua/orderedPairs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- borrowed from the Lua users wiki, at http://lua-users.org/wiki/SortedIteration

function __genOrderedIndex( t )
local orderedIndex = {}
for key in pairs(t) do
table.insert( orderedIndex, key )
end
table.sort( orderedIndex )
return orderedIndex
end

function orderedNext(t, state)
-- Equivalent of the next function, but returns the keys in the alphabetic
-- order. We use a temporary ordered key table that is stored in the
-- table being iterated.

local key = nil
--print("orderedNext: state = "..tostring(state) )
if state == nil then
-- the first time, generate the index
t.__orderedIndex = __genOrderedIndex( t )
key = t.__orderedIndex[1]
else
-- fetch the next value
for i = 1,table.getn(t.__orderedIndex) do
if t.__orderedIndex[i] == state then
key = t.__orderedIndex[i+1]
end
end
end

if key then
return key, t[key]
end

-- no more value to return, cleanup
t.__orderedIndex = nil
return
end

function orderedPairs(t)
-- Equivalent of the pairs() function on tables. Allows to iterate
-- in order
return orderedNext, t, nil
end
6 changes: 4 additions & 2 deletions 3rdparty/toluapp/src/bin/lua/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ classPackage = {
classPackage.__index = classPackage
setmetatable(classPackage,classContainer)

require 'orderedPairs'

-- Print method
function classPackage:print ()
print("Package: "..self.name)
Expand Down Expand Up @@ -133,7 +135,7 @@ function classPackage:preamble ()
output('\n')
output('/* function to release collected object via destructor */')
output('#ifdef __cplusplus\n')
for i,v in ipairs(_collect) do
for i,v in orderedPairs(_collect) do
output('\nstatic int '..v..' (lua_State* tolua_S)')
output('{')
output(' '..i..'* self = ('..i..'*) tolua_tousertype(tolua_S,1,0);')
Expand All @@ -152,7 +154,7 @@ function classPackage:preamble ()
if flags.t then
output("#ifndef Mtolua_typeid\n#define Mtolua_typeid(L,TI,T)\n#endif\n")
end
for n,v in ipairs(_usertype) do
for n,v in orderedPairs(_usertype) do
if (not _global_classes[v]) or _global_classes[v]:check_public_access() then
output(' tolua_usertype(tolua_S,"',v,'");')
if flags.t then
Expand Down

0 comments on commit 59da849

Please sign in to comment.