db.lua 23 KB

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