interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / db.lua

1067 lines · 26644 bytes Raw Blame History
1-- $Id: testes/db.lua $
2-- See Copyright Notice in file lua.h
3
4-- testing debug library
5
6local debug = require "debug"
7
8local function dostring(s) return assert(load(s))() end
9
10print"testing debug library and debug information"
11
12do
13local a=1
14end
15
16assert(not debug.gethook())
17
18local testline = 19 -- line where 'test' is defined
19local function test (s, l, p) -- this must be line 19
20 collectgarbage() -- avoid gc during trace
21 local function f (event, line)
22 assert(event == 'line')
23 local l = table.remove(l, 1)
24 if p then print(l, line) end
25 assert(l == line, "wrong trace!!")
26 end
27 debug.sethook(f,"l"); load(s)(); debug.sethook()
28 assert(#l == 0)
29end
30
31
32do
33 assert(not pcall(debug.getinfo, print, "X")) -- invalid option
34 assert(not pcall(debug.getinfo, 0, ">")) -- invalid option
35 assert(not debug.getinfo(1000)) -- out of range level
36 assert(not debug.getinfo(-1)) -- out of range level
37 local a = debug.getinfo(print)
38 assert(a.what == "C" and a.short_src == "[C]")
39 a = debug.getinfo(print, "L")
40 assert(a.activelines == nil)
41 local b = debug.getinfo(test, "SfL")
42 assert(b.name == nil and b.what == "Lua" and b.linedefined == testline and
43 b.lastlinedefined == b.linedefined + 10 and
44 b.func == test and not string.find(b.short_src, "%["))
45 assert(b.activelines[b.linedefined + 1] and
46 b.activelines[b.lastlinedefined])
47 assert(not b.activelines[b.linedefined] and
48 not b.activelines[b.lastlinedefined + 1])
49end
50
51
52-- bug in 5.4.4-5.4.6: activelines in vararg functions
53-- without debug information
54do
55 local func = load(string.dump(load("print(10)"), true))
56 local actl = debug.getinfo(func, "L").activelines
57 assert(#actl == 0) -- no line info
58end
59
60
61-- test file and string names truncation
62local a = "function f () end"
63local function dostring (s, x) return load(s, x)() end
64dostring(a)
65assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
66dostring(a..string.format("; %s\n=1", string.rep('p', 400)))
67assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
68dostring(a..string.format("; %s=1", string.rep('p', 400)))
69assert(string.find(debug.getinfo(f).short_src, '^%[string [^\n]*%.%.%."%]$'))
70dostring("\n"..a)
71assert(debug.getinfo(f).short_src == '[string "..."]')
72dostring(a, "")
73assert(debug.getinfo(f).short_src == '[string ""]')
74dostring(a, "@xuxu")
75assert(debug.getinfo(f).short_src == "xuxu")
76dostring(a, "@"..string.rep('p', 1000)..'t')
77assert(string.find(debug.getinfo(f).short_src, "^%.%.%.p*t$"))
78dostring(a, "=xuxu")
79assert(debug.getinfo(f).short_src == "xuxu")
80dostring(a, string.format("=%s", string.rep('x', 500)))
81assert(string.find(debug.getinfo(f).short_src, "^x*$"))
82dostring(a, "=")
83assert(debug.getinfo(f).short_src == "")
84_G.a = nil; _G.f = nil;
85_G[string.rep("p", 400)] = nil
86
87
88repeat
89 local g = {x = function ()
90 local a = debug.getinfo(2)
91 assert(a.name == 'f' and a.namewhat == 'local')
92 a = debug.getinfo(1)
93 assert(a.name == 'x' and a.namewhat == 'field')
94 return 'xixi'
95 end}
96 local f = function () return 1+1 and (not 1 or g.x()) end
97 assert(f() == 'xixi')
98 g = debug.getinfo(f)
99 assert(g.what == "Lua" and g.func == f and g.namewhat == "" and not g.name)
100
101 function f (x, name) -- local!
102 name = name or 'f'
103 local a = debug.getinfo(1)
104 assert(a.name == name and a.namewhat == 'local')
105 return x
106 end
107
108 -- breaks in different conditions
109 if 3>4 then break end; f()
110 if 3<4 then a=1 else break end; f()
111 while 1 do local x=10; break end; f()
112 local b = 1
113 if 3>4 then return math.sin(1) end; f()
114 a = 3<4; f()
115 a = 3<4 or 1; f()
116 repeat local x=20; if 4>3 then f() else break end; f() until 1
117 g = {}
118 f(g).x = f(2) and f(10)+f(9)
119 assert(g.x == f(19))
120 function g(x) if not x then return 3 end return (x('a', 'x')) end
121 assert(g(f) == 'a')
122until 1
123
124test([[if
125math.sin(1)
126then
127 a=1
128else
129 a=2
130end
131]], {2,4,7})
132
133
134test([[
135local function foo()
136end
137foo()
138A = 1
139A = 2
140A = 3
141]], {2, 3, 2, 4, 5, 6})
142_G.A = nil
143
144
145test([[--
146if nil then
147 a=1
148else
149 a=2
150end
151]], {2,5,6})
152
153test([[a=1
154repeat
155 a=a+1
156until a==3
157]], {1,3,4,3,4})
158
159test([[ do
160 return
161end
162]], {2})
163
164test([[local a
165a=1
166while a<=3 do
167 a=a+1
168end
169]], {1,2,3,4,3,4,3,4,3,5})
170
171test([[while math.sin(1) do
172 if math.sin(1)
173 then break
174 end
175end
176a=1]], {1,2,3,6})
177
178test([[for i=1,3 do
179 a=i
180end
181]], {1,2,1,2,1,2,1,3})
182
183test([[for i,v in pairs{'a','b'} do
184 a=tostring(i) .. v
185end
186]], {1,2,1,2,1,3})
187
188test([[for i=1,4 do a=1 end]], {1,1,1,1})
189
190_G.a = nil
191
192
193do -- testing line info/trace with large gaps in source
194
195 local a = {1, 2, 3, 10, 124, 125, 126, 127, 128, 129, 130,
196 255, 256, 257, 500, 1000}
197 local s = [[
198 local b = {10}
199 a = b[1] X + Y b[1]
200 b = 4
201 ]]
202 for _, i in ipairs(a) do
203 local subs = {X = string.rep("\n", i)}
204 for _, j in ipairs(a) do
205 subs.Y = string.rep("\n", j)
206 local s = string.gsub(s, "[XY]", subs)
207 test(s, {1, 2 + i, 2 + i + j, 2 + i, 2 + i + j, 3 + i + j})
208 end
209 end
210end
211_G.a = nil
212
213
214do -- testing active lines
215 local function checkactivelines (f, lines)
216 local t = debug.getinfo(f, "SL")
217 for _, l in pairs(lines) do
218 l = l + t.linedefined
219 assert(t.activelines[l])
220 t.activelines[l] = undef
221 end
222 assert(next(t.activelines) == nil) -- no extra lines
223 end
224
225 checkactivelines(function (...) -- vararg function
226 -- 1st line is empty
227 -- 2nd line is empty
228 -- 3th line is empty
229 local a = 20
230 -- 5th line is empty
231 local b = 30
232 -- 7th line is empty
233 end, {4, 6, 8})
234
235 checkactivelines(function (a)
236 -- 1st line is empty
237 -- 2nd line is empty
238 local a = 20
239 local b = 30
240 -- 5th line is empty
241 end, {3, 4, 6})
242
243 checkactivelines(function (a, b, ...) end, {0})
244
245 checkactivelines(function (a, b)
246 end, {1})
247
248 for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do
249 checkactivelines(
250 load(string.format("%s return 1", string.rep("\n", n))),
251 {n + 1})
252 end
253
254end
255
256print'+'
257
258-- invalid levels in [gs]etlocal
259assert(not pcall(debug.getlocal, 20, 1))
260assert(not pcall(debug.setlocal, -1, 1, 10))
261assert(not debug.getlocal(test, -2147483648))
262
263
264-- parameter names
265local function foo (a,b,...) local d, e end
266local co = coroutine.create(foo)
267
268assert(debug.getlocal(foo, 1) == 'a')
269assert(debug.getlocal(foo, 2) == 'b')
270assert(not debug.getlocal(foo, 3))
271assert(debug.getlocal(co, foo, 1) == 'a')
272assert(debug.getlocal(co, foo, 2) == 'b')
273assert(not debug.getlocal(co, foo, 3))
274
275assert(not debug.getlocal(print, 1))
276
277
278local function foo () return (debug.getlocal(1, -1)) end
279assert(not foo(10))
280
281
282-- varargs
283local function foo (a, ...)
284 local t = table.pack(...)
285 for i = 1, t.n do
286 local n, v = debug.getlocal(1, -i)
287 assert(n == "(vararg)" and v == t[i])
288 end
289 assert(not debug.getlocal(1, -(t.n + 1)))
290 assert(not debug.setlocal(1, -(t.n + 1), 30))
291 if t.n > 0 then
292 (function (x)
293 assert(debug.setlocal(2, -1, x) == "(vararg)")
294 assert(debug.setlocal(2, -t.n, x) == "(vararg)")
295 end)(430)
296 assert(... == 430)
297 end
298end
299
300foo()
301foo(print)
302foo(200, 3, 4)
303local a = {}
304for i = 1, (_soft and 100 or 1000) do a[i] = i end
305foo(table.unpack(a))
306
307
308
309do -- test hook presence in debug info
310 assert(not debug.gethook())
311 local count = 0
312 local function f ()
313 assert(debug.getinfo(1).namewhat == "hook")
314 local sndline = string.match(debug.traceback(), "\n(.-)\n")
315 assert(string.find(sndline, "hook"))
316 count = count + 1
317 end
318 debug.sethook(f, "l")
319 local a = 0
320 _ENV.a = a
321 a = 1
322 debug.sethook()
323 assert(count == 4)
324end
325_ENV.a = nil
326
327
328-- hook table has weak keys
329assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
330
331
332a = {}; local L = nil
333local glob = 1
334local oldglob = glob
335debug.sethook(function (e,l)
336 collectgarbage() -- force GC during a hook
337 local f, m, c = debug.gethook()
338 assert(m == 'crl' and c == 0)
339 if e == "line" then
340 if glob ~= oldglob then
341 L = l-1 -- get the first line where "glob" has changed
342 oldglob = glob
343 end
344 elseif e == "call" then
345 local f = debug.getinfo(2, "f").func
346 a[f] = 1
347 else assert(e == "return")
348 end
349end, "crl")
350
351
352function f(a,b)
353 -- declare some globals to check that they don't interfere with 'getlocal'
354 global collectgarbage
355 collectgarbage()
356 local _, x = debug.getlocal(1, 1)
357 global assert, g, string
358 local _, y = debug.getlocal(1, 2)
359 assert(x == a and y == b)
360 assert(debug.setlocal(2, 4, "pera") == "AA".."AA")
361 assert(debug.setlocal(2, 5, "manga") == "B")
362 x = debug.getinfo(2)
363 assert(x.func == g and x.what == "Lua" and x.name == 'g' and
364 x.nups == 2 and string.find(x.source, "^@.*db%.lua$"))
365 glob = glob+1
366 assert(debug.getinfo(1, "l").currentline == L+1)
367 assert(debug.getinfo(1, "l").currentline == L+2)
368end
369
370function foo()
371 glob = glob+1
372 assert(debug.getinfo(1, "l").currentline == L+1)
373end; foo() -- set L
374-- check line counting inside strings and empty lines
375
376local _ = 'alo\
377alo' .. [[
378
379]]
380--[[
381]]
382assert(debug.getinfo(1, "l").currentline == L+11) -- check count of lines
383
384
385function g (...)
386 local arg = {...}
387 do local a,b,c; a=math.sin(40); end
388 local feijao
389 local AAAA,B = "xuxu", "abacate"
390 f(AAAA,B)
391 assert(AAAA == "pera" and B == "manga")
392 do
393 global *
394 local B = 13
395 global<const> assert
396 local x,y = debug.getlocal(1,6)
397 assert(x == 'B' and y == 13)
398 end
399end
400
401g()
402
403
404assert(a[f] and a[g] and a[assert] and a[debug.getlocal] and not a[print])
405
406
407-- tests for manipulating non-registered locals (C and Lua temporaries)
408
409local n, v = debug.getlocal(0, 1)
410assert(v == 0 and n == "(C temporary)")
411local n, v = debug.getlocal(0, 2)
412assert(v == 2 and n == "(C temporary)")
413assert(not debug.getlocal(0, 3))
414assert(not debug.getlocal(0, 0))
415
416function f()
417 assert(select(2, debug.getlocal(2,3)) == 1)
418 assert(not debug.getlocal(2,4))
419 debug.setlocal(2, 3, 10)
420 return 20
421end
422
423function g(a,b) return (a+1) + f() end
424
425assert(g(0,0) == 30)
426
427_G.f, _G.g = nil
428
429debug.sethook(nil);
430assert(not debug.gethook())
431
432
433-- minimal tests for setuservalue/getuservalue
434do
435 assert(not debug.setuservalue(io.stdin, 10))
436 local a, b = debug.getuservalue(io.stdin, 10)
437 assert(a == nil and not b)
438end
439
440-- testing interaction between multiple values x hooks
441do
442 local function f(...) return 3, ... end
443 local count = 0
444 local a = {}
445 for i = 1, 100 do a[i] = i end
446 debug.sethook(function () count = count + 1 end, "", 1)
447 local t = {table.unpack(a)}
448 assert(#t == 100)
449 t = {table.unpack(a, 1, 3)}
450 assert(#t == 3)
451 t = {f(table.unpack(a, 1, 30))}
452 assert(#t == 31)
453end
454
455
456-- testing access to function arguments
457
458local function collectlocals (level)
459 local tab = {}
460 for i = 1, math.huge do
461 local n, v = debug.getlocal(level + 1, i)
462 if not (n and string.find(n, "^[a-zA-Z0-9_]+$") or
463 n == "(vararg table)") then
464 break -- consider only real variables
465 end
466 tab[n] = v
467 end
468 return tab
469end
470
471
472local X = nil
473a = {}
474function a:f (a, b, ...) local arg = {...}; local c = 13 end
475debug.sethook(function (e)
476 assert(e == "call")
477 dostring("XX = 12") -- test dostring inside hooks
478 -- testing errors inside hooks
479 assert(not pcall(load("a='joao'+1")))
480 debug.sethook(function (e, l)
481 assert(debug.getinfo(2, "l").currentline == l)
482 local f,m,c = debug.gethook()
483 assert(e == "line")
484 assert(m == 'l' and c == 0)
485 debug.sethook(nil) -- hook is called only once
486 assert(not X) -- check that
487 X = collectlocals(2)
488 end, "l")
489end, "c")
490
491a:f(1,2,3,4,5)
492assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
493assert(XX == 12)
494assert(not debug.gethook())
495_G.XX = nil
496
497
498-- testing access to local variables in return hook (bug in 5.2)
499do
500 local X = false
501
502 local function foo (a, b, ...)
503 do local x,y,z end
504 local c, d = 10, 20
505 return
506 end
507
508 local function aux ()
509 if debug.getinfo(2).name == "foo" then
510 X = true -- to signal that it found 'foo'
511 local tab = {a = 100, b = 200, c = 10, d = 20}
512 for n, v in pairs(collectlocals(2)) do
513 assert(tab[n] == v)
514 tab[n] = undef
515 end
516 assert(next(tab) == nil) -- 'tab' must be empty
517 end
518 end
519
520 debug.sethook(aux, "r"); foo(100, 200); debug.sethook()
521 assert(X)
522
523end
524
525
526local function eqseq (t1, t2)
527 assert(#t1 == #t2)
528 for i = 1, #t1 do
529 assert(t1[i] == t2[i])
530 end
531end
532
533
534do print("testing inspection of parameters/returned values")
535 local on = false
536 local inp, out
537
538 local function hook (event)
539 if not on then return end
540 local ar = debug.getinfo(2, "ruS")
541 local t = {}
542 for i = ar.ftransfer, ar.ftransfer + ar.ntransfer - 1 do
543 local _, v = debug.getlocal(2, i)
544 t[#t + 1] = v
545 end
546 if event == "return" then
547 out = t
548 else
549 inp = t
550 end
551 end
552
553 debug.sethook(hook, "cr")
554
555 on = true; math.sin(3); on = false
556 eqseq(inp, {3}); eqseq(out, {math.sin(3)})
557
558 on = true; select(2, 10, 20, 30, 40); on = false
559 eqseq(inp, {2, 10, 20, 30, 40}); eqseq(out, {20, 30, 40})
560
561 local function foo (a, ...) return ... end
562 local function foo1 () on = not on; return foo(20, 10, 0) end
563 foo1(); on = false
564 eqseq(inp, {20}); eqseq(out, {10, 0})
565
566 debug.sethook()
567end
568
569
570
571-- testing upvalue access
572local function getupvalues (f)
573 local t = {}
574 local i = 1
575 while true do
576 local name, value = debug.getupvalue(f, i)
577 if not name then break end
578 assert(not t[name])
579 t[name] = value
580 i = i + 1
581 end
582 return t
583end
584
585local a,b,c = 1,2,3
586local function foo1 (a) b = a; return c end
587local function foo2 (x) a = x; return c+b end
588assert(not debug.getupvalue(foo1, 3))
589assert(not debug.getupvalue(foo1, 0))
590assert(not debug.setupvalue(foo1, 3, "xuxu"))
591local t = getupvalues(foo1)
592assert(t.a == nil and t.b == 2 and t.c == 3)
593t = getupvalues(foo2)
594assert(t.a == 1 and t.b == 2 and t.c == 3)
595assert(debug.setupvalue(foo1, 1, "xuxu") == "b")
596assert(({debug.getupvalue(foo2, 3)})[2] == "xuxu")
597-- upvalues of C functions are always named "" (the empty string)
598assert(debug.getupvalue(string.gmatch("x", "x"), 1) == "")
599
600
601-- testing count hooks
602local a=0
603debug.sethook(function (e) a=a+1 end, "", 1)
604a=0; for i=1,1000 do end; assert(1000 < a and a < 1012)
605debug.sethook(function (e) a=a+1 end, "", 4)
606a=0; for i=1,1000 do end; assert(250 < a and a < 255)
607local f,m,c = debug.gethook()
608assert(m == "" and c == 4)
609debug.sethook(function (e) a=a+1 end, "", 4000)
610a=0; for i=1,1000 do end; assert(a == 0)
611
612do
613 debug.sethook(print, "", 2^24 - 1) -- count upperbound
614 local f,m,c = debug.gethook()
615 assert(({debug.gethook()})[3] == 2^24 - 1)
616end
617
618debug.sethook()
619
620local g, g1
621
622-- tests for tail calls
623local function f (x)
624 if x then
625 assert(debug.getinfo(1, "S").what == "Lua")
626 assert(debug.getinfo(1, "t").istailcall == true)
627 local tail = debug.getinfo(2)
628 assert(tail.func == g1 and tail.istailcall == true)
629 assert(debug.getinfo(3, "S").what == "main")
630 print"+"
631 end
632end
633
634assert(debug.getinfo(print, 't').istailcall == false)
635assert(debug.getinfo(print, 't').extraargs == 0)
636
637function g(x) return f(x) end
638
639function g1(x) g(x) end
640
641local function h (x) local f=g1; return f(x) end
642
643h(true)
644
645local b = {}
646debug.sethook(function (e) table.insert(b, e) end, "cr")
647h(false)
648debug.sethook()
649local res = {"return", -- first return (from sethook)
650 "call", "tail call", "call", "tail call",
651 "return", "return",
652 "call", -- last call (to sethook)
653}
654for i = 1, #res do assert(res[i] == table.remove(b, 1)) end
655
656b = 0
657debug.sethook(function (e)
658 if e == "tail call" then
659 b = b + 1
660 assert(debug.getinfo(2, "t").istailcall == true)
661 else
662 assert(debug.getinfo(2, "t").istailcall == false)
663 end
664 end, "c")
665h(false)
666debug.sethook()
667assert(b == 2) -- two tail calls
668
669local lim = _soft and 3000 or 30000
670local function foo (x)
671 if x==0 then
672 assert(debug.getinfo(2).what == "main")
673 local info = debug.getinfo(1)
674 assert(info.istailcall == true and info.func == foo)
675 else return foo(x-1)
676 end
677end
678
679foo(lim)
680
681
682print"+"
683
684
685-- testing local function information
686co = load[[
687 local A = function ()
688 return x
689 end
690 return
691]]
692
693local a = 0
694-- 'A' should be visible to debugger only after its complete definition
695debug.sethook(function (e, l)
696 if l == 3 then a = a + 1; assert(debug.getlocal(2, 1) == "(temporary)")
697 elseif l == 4 then a = a + 1; assert(debug.getlocal(2, 1) == "A")
698 end
699end, "l")
700co() -- run local function definition
701debug.sethook() -- turn off hook
702assert(a == 2) -- ensure all two lines where hooked
703
704-- testing traceback
705
706assert(debug.traceback(print) == print)
707assert(debug.traceback(print, 4) == print)
708assert(string.find(debug.traceback("hi", 4), "^hi\n"))
709assert(string.find(debug.traceback("hi"), "^hi\n"))
710assert(not string.find(debug.traceback("hi"), "'debug.traceback'"))
711assert(string.find(debug.traceback("hi", 0), "'traceback'"))
712assert(string.find(debug.traceback(), "^stack traceback:\n"))
713
714do -- C-function names in traceback
715 local st, msg = (function () return pcall end)()(debug.traceback)
716 assert(st == true and string.find(msg, "pcall"))
717end
718
719
720-- testing nparams, nups e isvararg
721local t = debug.getinfo(print, "u")
722assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
723
724t = debug.getinfo(function (a,b,c) end, "u")
725assert(t.isvararg == false and t.nparams == 3 and t.nups == 0)
726
727t = debug.getinfo(function (a,b,...) return t[a] end, "u")
728assert(t.isvararg == true and t.nparams == 2 and t.nups == 1)
729
730t = debug.getinfo(function (a,b,...t) t.n = 2; return t[a] end, "u")
731assert(t.isvararg == true and t.nparams == 2 and t.nups == 0)
732
733t = debug.getinfo(1) -- main
734assert(t.isvararg == true and t.nparams == 0 and t.nups == 1 and
735 debug.getupvalue(t.func, 1) == "_ENV")
736
737t = debug.getinfo(math.sin) -- C function
738assert(t.isvararg == true and t.nparams == 0 and t.nups == 0)
739
740t = debug.getinfo(string.gmatch("abc", "a")) -- C closure
741assert(t.isvararg == true and t.nparams == 0 and t.nups > 0)
742
743
744
745-- testing debugging of coroutines
746
747local function checktraceback (co, p, level)
748 local tb = debug.traceback(co, nil, level)
749 local i = 0
750 for l in string.gmatch(tb, "[^\n]+\n?") do
751 assert(i == 0 or string.find(l, p[i]))
752 i = i+1
753 end
754 assert(p[i] == undef)
755end
756
757
758local function f (n)
759 if n > 0 then f(n-1)
760 else coroutine.yield() end
761end
762
763local co = coroutine.create(f)
764coroutine.resume(co, 3)
765checktraceback(co, {"yield", "db.lua", "db.lua", "db.lua", "db.lua"})
766checktraceback(co, {"db.lua", "db.lua", "db.lua", "db.lua"}, 1)
767checktraceback(co, {"db.lua", "db.lua", "db.lua"}, 2)
768checktraceback(co, {"db.lua"}, 4)
769checktraceback(co, {}, 40)
770
771
772co = coroutine.create(function (x)
773 local a = 1
774 coroutine.yield(debug.getinfo(1, "l"))
775 coroutine.yield(debug.getinfo(1, "l").currentline)
776 return a
777 end)
778
779local tr = {}
780local foo = function (e, l) if l then table.insert(tr, l) end end
781debug.sethook(co, foo, "lcr")
782
783local _, l = coroutine.resume(co, 10)
784local x = debug.getinfo(co, 1, "lfLS")
785assert(x.currentline == l.currentline and x.activelines[x.currentline])
786assert(type(x.func) == "function")
787for i=x.linedefined + 1, x.lastlinedefined do
788 assert(x.activelines[i])
789 x.activelines[i] = undef
790end
791assert(next(x.activelines) == nil) -- no 'extra' elements
792assert(not debug.getinfo(co, 2))
793local a,b = debug.getlocal(co, 1, 1)
794assert(a == "x" and b == 10)
795a,b = debug.getlocal(co, 1, 2)
796assert(a == "a" and b == 1)
797debug.setlocal(co, 1, 2, "hi")
798assert(debug.gethook(co) == foo)
799assert(#tr == 2 and
800 tr[1] == l.currentline-1 and tr[2] == l.currentline)
801
802a,b,c = pcall(coroutine.resume, co)
803assert(a and b and c == l.currentline+1)
804checktraceback(co, {"yield", "in function <"})
805
806a,b = coroutine.resume(co)
807assert(a and b == "hi")
808assert(#tr == 4 and tr[4] == l.currentline+2)
809assert(debug.gethook(co) == foo)
810assert(not debug.gethook())
811checktraceback(co, {})
812
813
814-- check get/setlocal in coroutines
815co = coroutine.create(function (x)
816 local a, b = coroutine.yield(x)
817 assert(a == 100 and b == nil)
818 return x
819end)
820a, b = coroutine.resume(co, 10)
821assert(a and b == 10)
822a, b = debug.getlocal(co, 1, 1)
823assert(a == "x" and b == 10)
824assert(not debug.getlocal(co, 1, 5))
825assert(debug.setlocal(co, 1, 1, 30) == "x")
826assert(not debug.setlocal(co, 1, 5, 40))
827a, b = coroutine.resume(co, 100)
828assert(a and b == 30)
829
830
831-- check traceback of suspended (or dead with error) coroutines
832
833function f(i)
834 if i == 0 then error(i)
835 else coroutine.yield(); f(i-1)
836 end
837end
838
839
840co = coroutine.create(function (x) f(x) end)
841a, b = coroutine.resume(co, 3)
842t = {"'yield'", "'f'", "in function <"}
843while coroutine.status(co) == "suspended" do
844 checktraceback(co, t)
845 a, b = coroutine.resume(co)
846 table.insert(t, 2, "'f'") -- one more recursive call to 'f'
847end
848t[1] = "'error'"
849checktraceback(co, t)
850
851
852-- test accessing line numbers of a coroutine from a resume inside
853-- a C function (this is a known bug in Lua 5.0)
854
855local function g(x)
856 coroutine.yield(x)
857end
858
859local function f (i)
860 debug.sethook(function () end, "l")
861 for j=1,1000 do
862 g(i+j)
863 end
864end
865
866local co = coroutine.wrap(f)
867co(10)
868pcall(co)
869pcall(co)
870
871
872assert(type(debug.getregistry()) == "table")
873
874
875-- test tagmethod information
876local a = {}
877local function f (t)
878 local info = debug.getinfo(1);
879 assert(info.namewhat == "metamethod")
880 a.op = info.name
881 return info.name
882end
883setmetatable(a, {
884 __index = f; __add = f; __div = f; __mod = f; __concat = f; __pow = f;
885 __mul = f; __idiv = f; __unm = f; __len = f; __sub = f;
886 __shl = f; __shr = f; __bor = f; __bxor = f;
887 __eq = f; __le = f; __lt = f; __unm = f; __len = f; __band = f;
888 __bnot = f;
889})
890
891local b = setmetatable({}, getmetatable(a))
892
893assert(a[3] == "index" and a^3 == "pow" and a..a == "concat")
894assert(a/3 == "div" and 3%a == "mod")
895assert(a+3 == "add" and 3-a == "sub" and a*3 == "mul" and
896 -a == "unm" and #a == "len" and a&3 == "band")
897assert(a + 30000 == "add" and a - 3.0 == "sub" and a * 3.0 == "mul" and
898 -a == "unm" and #a == "len" and a & 3 == "band")
899assert(a|3 == "bor" and 3~a == "bxor" and a<<3 == "shl" and a>>1 == "shr")
900assert (a==b and a.op == "eq")
901assert (a>=b and a.op == "le")
902assert ("x">=a and a.op == "le")
903assert (a>b and a.op == "lt")
904assert (a>10 and a.op == "lt")
905assert(~a == "bnot")
906
907do -- testing for-iterator name
908 local function f()
909 assert(debug.getinfo(1).name == "for iterator")
910 end
911
912 for i in f do end
913end
914
915
916do -- testing debug info for finalizers
917 local name = nil
918
919 -- create a piece of garbage with a finalizer
920 setmetatable({}, {__gc = function ()
921 local t = debug.getinfo(1) -- get function information
922 assert(t.namewhat == "metamethod")
923 name = t.name
924 end})
925
926 -- repeat until previous finalizer runs (setting 'name')
927 repeat local a = {} until name
928 assert(name == "__gc")
929end
930
931
932do
933 print("testing traceback sizes")
934
935 local function countlines (s)
936 return select(2, string.gsub(s, "\n", ""))
937 end
938
939 local function deep (lvl, n)
940 if lvl == 0 then
941 return (debug.traceback("message", n))
942 else
943 return (deep(lvl-1, n))
944 end
945 end
946
947 local function checkdeep (total, start)
948 local s = deep(total, start)
949 local rest = string.match(s, "^message\nstack traceback:\n(.*)$")
950 local cl = countlines(rest)
951 -- at most 10 lines in first part, 11 in second, plus '...'
952 assert(cl <= 10 + 11 + 1)
953 local brk = string.find(rest, "%.%.%.\t%(skip")
954 if brk then -- does message have '...'?
955 local rest1 = string.sub(rest, 1, brk)
956 local rest2 = string.sub(rest, brk, #rest)
957 assert(countlines(rest1) == 10 and countlines(rest2) == 11)
958 else
959 assert(cl == total - start + 2)
960 end
961 end
962
963 for d = 1, 51, 10 do
964 for l = 1, d do
965 -- use coroutines to ensure complete control of the stack
966 coroutine.wrap(checkdeep)(d, l)
967 end
968 end
969
970end
971
972
973print("testing debug functions on chunk without debug info")
974local prog = [[-- program to be loaded without debug information (strip)
975local debug = require'debug'
976local a = 12 -- a local variable
977
978local n, v = debug.getlocal(1, 1)
979assert(n == "(temporary)" and v == debug) -- unknown name but known value
980n, v = debug.getlocal(1, 2)
981assert(n == "(temporary)" and v == 12) -- unknown name but known value
982
983-- a function with an upvalue
984local f = function () local x; return a end
985n, v = debug.getupvalue(f, 1)
986assert(n == "(no name)" and v == 12)
987assert(debug.setupvalue(f, 1, 13) == "(no name)")
988assert(a == 13)
989
990local t = debug.getinfo(f)
991assert(t.name == nil and t.linedefined > 0 and
992 t.lastlinedefined == t.linedefined and
993 t.short_src == "?")
994assert(debug.getinfo(1).currentline == -1)
995
996t = debug.getinfo(f, "L").activelines
997assert(next(t) == nil) -- active lines are empty
998
999-- dump/load a function without debug info
1000f = load(string.dump(f))
1001
1002t = debug.getinfo(f)
1003assert(t.name == nil and t.linedefined > 0 and
1004 t.lastlinedefined == t.linedefined and
1005 t.short_src == "?")
1006assert(debug.getinfo(1).currentline == -1)
1007
1008return a
1009]]
1010
1011
1012-- load 'prog' without debug info
1013local f = assert(load(string.dump(load(prog), true)))
1014
1015assert(f() == 13)
1016
1017do -- bug in 5.4.0: line hooks in stripped code
1018 local function foo ()
1019 local a = 1
1020 local b = 2
1021 return b
1022 end
1023
1024 local s = load(string.dump(foo, true))
1025 local line = true
1026 debug.sethook(function (e, l)
1027 assert(e == "line")
1028 line = l
1029 end, "l")
1030 assert(s() == 2); debug.sethook(nil)
1031 assert(line == nil) -- hook called without debug info for 1st instruction
1032end
1033
1034do -- tests for 'source' in binary dumps
1035 local prog = [[
1036 return function (x)
1037 return function (y)
1038 return x + y
1039 end
1040 end
1041 ]]
1042 local name = string.rep("x", 1000)
1043 local p = assert(load(prog, name))
1044 -- load 'p' as a binary chunk with debug information
1045 local c = string.dump(p)
1046 assert(#c > 1000 and #c < 2000) -- no repetition of 'source' in dump
1047 local f = assert(load(c))
1048 local g = f()
1049 local h = g(3)
1050 assert(h(5) == 8)
1051 assert(debug.getinfo(f).source == name and -- all functions have 'source'
1052 debug.getinfo(g).source == name and
1053 debug.getinfo(h).source == name)
1054 -- again, without debug info
1055 local c = string.dump(p, true)
1056 assert(#c < 500) -- no 'source' in dump
1057 local f = assert(load(c))
1058 local g = f()
1059 local h = g(30)
1060 assert(h(50) == 80)
1061 assert(debug.getinfo(f).source == '=?' and -- no function has 'source'
1062 debug.getinfo(g).source == '=?' and
1063 debug.getinfo(h).source == '=?')
1064end
1065
1066print"OK"
1067