From d4eff00234dc55dac4cb86b6187f5607c1254f9b Mon Sep 17 00:00:00 2001 From: Roberto I Date: Wed, 29 Oct 2025 13:14:48 -0300 Subject: 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'. --- testes/goto.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'testes') diff --git a/testes/goto.lua b/testes/goto.lua index 3310314d..a692a623 100644 --- a/testes/goto.lua +++ b/testes/goto.lua @@ -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' -- cgit v1.2.3-55-g6feb