diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-10-29 13:14:48 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-10-29 13:14:48 -0300 |
| commit | d4eff00234dc55dac4cb86b6187f5607c1254f9b (patch) | |
| tree | 3f080f35cfd23249166a4311946cf0ea766d71d0 /testes/goto.lua | |
| parent | fca974486d12aa29bb6d731fdb5b25055157ece8 (diff) | |
| download | lua-d4eff00234dc55dac4cb86b6187f5607c1254f9b.tar.gz lua-d4eff00234dc55dac4cb86b6187f5607c1254f9b.tar.bz2 lua-d4eff00234dc55dac4cb86b6187f5607c1254f9b.zip | |
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'.
Diffstat (limited to 'testes/goto.lua')
| -rw-r--r-- | testes/goto.lua | 22 |
1 files changed, 22 insertions, 0 deletions
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" | |||
| 432 | _ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals | 432 | _ENV.a, _ENV.b, _ENV.c, _ENV.d = nil -- erase these globals |
| 433 | end | 433 | end |
| 434 | 434 | ||
| 435 | do | ||
| 436 | global table, string | ||
| 437 | -- global initialization when names don't fit in K | ||
| 438 | |||
| 439 | -- to fill constant table | ||
| 440 | local code = {} | ||
| 441 | for i = 1, 300 do code[i] = "'" .. i .. "'" end | ||
| 442 | code = table.concat(code, ",") | ||
| 443 | code = string.format([[ | ||
| 444 | return function (_ENV) | ||
| 445 | local dummy = {%s} -- fill initial positions in constant table, | ||
| 446 | -- so that initialization must use registers for global names | ||
| 447 | global a, b, c = 10, 20, 30 | ||
| 448 | end]], code) | ||
| 449 | |||
| 450 | local fun = assert(load(code))() | ||
| 451 | |||
| 452 | local env = {} | ||
| 453 | fun(env) | ||
| 454 | assert(env.a == 10 and env.b == 20 and env.c == 30) | ||
| 455 | end | ||
| 456 | |||
| 435 | print'OK' | 457 | print'OK' |
| 436 | 458 | ||
