db.lua 25 KB

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