db.lua 24 KB

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