Fixed initialization of global variables

When calling 'luaK_storevar', the 'expdesc' for the variable must be
created before the one for the expression, to satisfy the assumptions
for register allocation. So, in a statement like 'global a = exp', where
'a' is actually '_ENV.a', this variable must be handled before the
initializing expression 'exp'.
This commit is contained in:
Roberto I
2025-10-29 13:14:48 -03:00
parent fca974486d
commit d4eff00234
3 changed files with 52 additions and 13 deletions

View File

@@ -432,5 +432,27 @@ do print "testing initialization in global declarations"
_ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals
end
do
global table, string
-- global initialization when names don't fit in K
-- to fill constant table
local code = {}
for i = 1, 300 do code[i] = "'" .. i .. "'" end
code = table.concat(code, ",")
code = string.format([[
return function (_ENV)
local dummy = {%s} -- fill initial positions in constant table,
-- so that initialization must use registers for global names
global a, b, c = 10, 20, 30
end]], code)
local fun = assert(load(code))()
local env = {}
fun(env)
assert(env.a == 10 and env.b == 20 and env.c == 30)
end
print'OK'