Bug in dofile?

classic Classic list List threaded Threaded
2 messages Options
vc
Reply | Threaded
Open this post in threaded view
|

Bug in dofile?

vc
I write a script call dumpload.lua, it use opentests function(define 
LUA_DEBUG):
-- 
-- dumpcode.lua
-- 
function dumpcode(f) 
	local n,v
	local t = {} 
	local size 
	t = T.listcode(f)
	print("maxstack = "..t["maxstack"])
	print("numparams = "..t["numparams"])
	size = getn(t)
	for n = 1,size,1  do 
		print(n.." "..t[n])
	end
end

function dumptable(t)
	local n,v
	for n,v in t do
		print(n.." "..v)
	end
end


dumpcode(function () local z; z=1+3; z=max(8,9)-min(10,8);end)

-- end
I try :
>dofile("dumpcode.lua")
maxstack = 13
numparams = 1
1     5 - PUSHNIL        2
2     6 - CREATETABLE    0
3     7 - PUSHNIL        1
4     8 - GETGLOBAL      0
5     8 - GETDOTTED      1
6     8 - GETLOCAL       0
7     8 - CALL           5    1
8     8 - SETLOCAL       3
9     9 - GETGLOBAL      2
10     9 - PUSHSTRING     3
11     9 - GETLOCAL       3
12     9 - GETDOTTED      4
13     9 - CONCAT         2
14     9 - CALL           5    0
15    10 - GETGLOBAL      2
16    10 - PUSHSTRING     5
17    10 - GETLOCAL       3
18    10 - GETDOTTED      6
19    10 - CONCAT         2
20    10 - CALL           5    0
21    11 - GETGLOBAL      7
22    11 - GETLOCAL       3
23    11 - CALL           5    1
24    11 - SETLOCAL       4
25    12 - PUSHINT        1
26    12 - GETLOCAL       4
27    12 - PUSHINT        1
28    12 - FORPREP        8
29    13 - GETGLOBAL      2
30    13 - GETLOCAL       5
31    13 - PUSHSTRING     8
32    13 - GETLOCAL       3
33    13 - GETINDEXED     5
34    13 - CONCAT         3
35    13 - CALL           8    0
36    13 - FORLOOP       -8
37    15 - END
>
it just dump dumpcode , not function () local z; z=1+3; z=max(8,9)-min
(10,8);end
but:
> dumpcode(function () local z; z=1+3; z=max(8,9)-min(10,8);end)
maxstack = 5
numparams = 0
1     1 - PUSHNIL        1
2     1 - PUSHINT        1
3     1 - ADDI           3
4     1 - SETLOCAL       0
5     1 - GETGLOBAL      0
6     1 - PUSHINT        8
7     1 - PUSHINT        9
8     1 - CALL           1    1
9     1 - GETGLOBAL      1
10     1 - PUSHINT       10
11     1 - PUSHINT        8
12     1 - CALL           2    1
13     1 - SUB
14     1 - SETLOCAL       0
15     1 - END

it work fine.
Why?

vcc


























Reply | Threaded
Open this post in threaded view
|

Re: Bug in dofile?

Luiz Henrique de Figueiredo
This is not a bug in dofile, it is just how listcode works: it returns a table 
containing the bytecode for a function. In the case of chunks, the function
is the top-level, "main" function.

For a recursive (and nicer) listing of a chunk and its functions, use luac.
listcode is for internal debugging only.
--lhf