db.lua 24 KB

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