db.lua 24 KB

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