api.lua 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. -- $Id: testes/api.lua $
  2. -- See Copyright Notice in file all.lua
  3. if T==nil then
  4. (Message or print)('\n >>> testC not active: skipping API tests <<<\n')
  5. return
  6. end
  7. local debug = require "debug"
  8. local pack = table.pack
  9. function tcheck (t1, t2)
  10. assert(t1.n == (t2.n or #t2) + 1)
  11. for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
  12. end
  13. local function checkerr (msg, f, ...)
  14. local stat, err = pcall(f, ...)
  15. assert(not stat and string.find(err, msg))
  16. end
  17. print('testing C API')
  18. a = T.testC("pushvalue R; return 1")
  19. assert(a == debug.getregistry())
  20. -- absindex
  21. assert(T.testC("settop 10; absindex -1; return 1") == 10)
  22. assert(T.testC("settop 5; absindex -5; return 1") == 1)
  23. assert(T.testC("settop 10; absindex 1; return 1") == 1)
  24. assert(T.testC("settop 10; absindex R; return 1") < -10)
  25. -- testing alignment
  26. a = T.d2s(12458954321123.0)
  27. assert(a == string.pack("d", 12458954321123.0))
  28. assert(T.s2d(a) == 12458954321123.0)
  29. a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
  30. assert(a == 2 and b == 3 and not c)
  31. f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
  32. a,b,c = f()
  33. assert(a == 2 and b == 3 and not c)
  34. -- test that all trues are equal
  35. a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
  36. assert(a == b and a == true and c == false)
  37. a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\
  38. tobool -3; tobool -3; tobool -3; return 3"
  39. assert(a==false and b==true and c==false)
  40. a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
  41. assert(a == 40 and b == 5 and not c)
  42. t = pack(T.testC("settop 5; return *", 2, 3))
  43. tcheck(t, {n=4,2,3})
  44. t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
  45. assert(t.n == 10 and t[1] == nil and t[10] == nil)
  46. t = pack(T.testC("remove -2; return *", 2, 3, 4))
  47. tcheck(t, {n=2,2,4})
  48. t = pack(T.testC("insert -1; return *", 2, 3))
  49. tcheck(t, {n=2,2,3})
  50. t = pack(T.testC("insert 3; return *", 2, 3, 4, 5))
  51. tcheck(t, {n=4,2,5,3,4})
  52. t = pack(T.testC("replace 2; return *", 2, 3, 4, 5))
  53. tcheck(t, {n=3,5,3,4})
  54. t = pack(T.testC("replace -2; return *", 2, 3, 4, 5))
  55. tcheck(t, {n=3,2,3,5})
  56. t = pack(T.testC("remove 3; return *", 2, 3, 4, 5))
  57. tcheck(t, {n=3,2,4,5})
  58. t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5))
  59. tcheck(t, {n=4,2,3,3,5})
  60. t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5))
  61. tcheck(t, {n=4,2,3,4,3})
  62. do -- testing 'rotate'
  63. local t = {10, 20, 30, 40, 50, 60}
  64. for i = -6, 6 do
  65. local s = string.format("rotate 2 %d; return 7", i)
  66. local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60))
  67. tcheck(t1, t)
  68. table.insert(t, 1, table.remove(t))
  69. end
  70. t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40))
  71. tcheck(t, {10, 20, 40, 30})
  72. t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40))
  73. tcheck(t, {10, 20, 40, 30})
  74. -- some corner cases
  75. t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40))
  76. tcheck(t, {10, 20, 30, 40})
  77. t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40))
  78. tcheck(t, {10, 20, 30, 40})
  79. t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40))
  80. tcheck(t, {10, 20, 30, 40})
  81. end
  82. -- testing warnings
  83. T.testC([[
  84. warningC "#This shold be a"
  85. warningC " single "
  86. warning "warning"
  87. warningC "#This should be "
  88. warning "another one"
  89. ]])
  90. -- testing message handlers
  91. do
  92. local f = T.makeCfunc[[
  93. getglobal error
  94. pushstring bola
  95. pcall 1 1 1 # call 'error' with given handler
  96. pushstatus
  97. return 2 # return error message and status
  98. ]]
  99. local msg, st = f(string.upper) -- function handler
  100. assert(st == "ERRRUN" and msg == "BOLA")
  101. local msg, st = f(string.len) -- function handler
  102. assert(st == "ERRRUN" and msg == 4)
  103. end
  104. t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \
  105. insert 2; pushvalue 1; remove 1; insert 1; \
  106. insert -2; pushvalue -2; remove -3; return *",
  107. 2, 3, 4, 5, 10, 40, 90))
  108. tcheck(t, {n=7,2,3,4,5,10,40,90})
  109. t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
  110. tcheck(t, {n=1,"alo23joao12"})
  111. -- testing MULTRET
  112. t = pack(T.testC("call 2,-1; return *",
  113. function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
  114. tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
  115. do -- test returning more results than fit in the caller stack
  116. local a = {}
  117. for i=1,1000 do a[i] = true end; a[999] = 10
  118. local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]],
  119. table.unpack, a)
  120. assert(b == "10")
  121. end
  122. -- testing globals
  123. _G.a = 14; _G.b = "a31"
  124. local a = {T.testC[[
  125. getglobal a;
  126. getglobal b;
  127. getglobal b;
  128. setglobal a;
  129. return *
  130. ]]}
  131. assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31")
  132. -- testing arith
  133. assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
  134. assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
  135. assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
  136. assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
  137. assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
  138. assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
  139. assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
  140. assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
  141. assert(T.testC("arith /; return 1", 2, 0) == 10.0/0)
  142. a = T.testC("pushnum 10; pushint 3; arith \\; return 1")
  143. assert(a == 3.0 and math.type(a) == "float")
  144. a = T.testC("pushint 10; pushint 3; arith \\; return 1")
  145. assert(a == 3 and math.type(a) == "integer")
  146. a = assert(T.testC("pushint 10; pushint 3; arith +; return 1"))
  147. assert(a == 13 and math.type(a) == "integer")
  148. a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1"))
  149. assert(a == 13 and math.type(a) == "float")
  150. a,b,c = T.testC([[pushnum 1;
  151. pushstring 10; arith _;
  152. pushstring 5; return 3]])
  153. assert(a == 1 and b == -10 and c == "5")
  154. mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
  155. __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
  156. __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
  157. a,b,c = setmetatable({4}, mt),
  158. setmetatable({8}, mt),
  159. setmetatable({-3}, mt)
  160. x,y,z = T.testC("arith +; return 2", 10, a, b)
  161. assert(x == 10 and y[1] == 12 and z == nil)
  162. assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
  163. assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
  164. 8 % (4 + (-3)*2))
  165. -- errors in arithmetic
  166. checkerr("divide by zero", T.testC, "arith \\", 10, 0)
  167. checkerr("%%0", T.testC, "arith %", 10, 0)
  168. -- testing lessthan and lessequal
  169. assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2))
  170. assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2))
  171. assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2))
  172. assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2))
  173. assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2))
  174. assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
  175. assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2"))
  176. -- non-valid indices produce false
  177. assert(not T.testC("compare LT 1 4, return 1"))
  178. assert(not T.testC("compare LE 9 1, return 1"))
  179. assert(not T.testC("compare EQ 9 9, return 1"))
  180. local b = {__lt = function (a,b) return a[1] < b[1] end}
  181. local a1,a3,a4 = setmetatable({1}, b),
  182. setmetatable({3}, b),
  183. setmetatable({4}, b)
  184. assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2))
  185. assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2))
  186. assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2))
  187. a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20)
  188. assert(a == 20 and b == false)
  189. a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20)
  190. assert(a == 20 and b == false)
  191. a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
  192. assert(a == 20 and b == true)
  193. do -- testing lessthan and lessequal with metamethods
  194. local mt = {__lt = function (a,b) return a[1] < b[1] end,
  195. __le = function (a,b) return a[1] <= b[1] end,
  196. __eq = function (a,b) return a[1] == b[1] end}
  197. local function O (x)
  198. return setmetatable({x}, mt)
  199. end
  200. local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2))
  201. assert(a == true and b == 10)
  202. local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2))
  203. assert(a == false and b == 10)
  204. local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3))
  205. assert(a == true and b == 10)
  206. end
  207. -- testing length
  208. local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
  209. a,b,c = T.testC([[
  210. len 2;
  211. Llen 2;
  212. objsize 2;
  213. return 3
  214. ]], t)
  215. assert(a == 20 and b == 20 and c == 0)
  216. t.x = "234"; t[1] = 20
  217. a,b,c = T.testC([[
  218. len 2;
  219. Llen 2;
  220. objsize 2;
  221. return 3
  222. ]], t)
  223. assert(a == "234" and b == 234 and c == 1)
  224. t.x = print; t[1] = 20
  225. a,c = T.testC([[
  226. len 2;
  227. objsize 2;
  228. return 2
  229. ]], t)
  230. assert(a == print and c == 1)
  231. -- testing __concat
  232. a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
  233. x,y = T.testC([[
  234. pushnum 5
  235. pushvalue 2;
  236. pushvalue 2;
  237. concat 2;
  238. pushvalue -2;
  239. return 2;
  240. ]], a, a)
  241. assert(x == a..a and y == 5)
  242. -- concat with 0 elements
  243. assert(T.testC("concat 0; return 1") == "")
  244. -- concat with 1 element
  245. assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
  246. -- testing lua_is
  247. function B(x) return x and 1 or 0 end
  248. function count (x, n)
  249. n = n or 2
  250. local prog = [[
  251. isnumber %d;
  252. isstring %d;
  253. isfunction %d;
  254. iscfunction %d;
  255. istable %d;
  256. isuserdata %d;
  257. isnil %d;
  258. isnull %d;
  259. return 8
  260. ]]
  261. prog = string.format(prog, n, n, n, n, n, n, n, n)
  262. local a,b,c,d,e,f,g,h = T.testC(prog, x)
  263. return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h))
  264. end
  265. assert(count(3) == 2)
  266. assert(count('alo') == 1)
  267. assert(count('32') == 2)
  268. assert(count({}) == 1)
  269. assert(count(print) == 2)
  270. assert(count(function () end) == 1)
  271. assert(count(nil) == 1)
  272. assert(count(io.stdin) == 1)
  273. assert(count(nil, 15) == 100)
  274. -- testing lua_to...
  275. function to (s, x, n)
  276. n = n or 2
  277. return T.testC(string.format("%s %d; return 1", s, n), x)
  278. end
  279. local null = T.pushuserdata(0)
  280. local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues)
  281. assert(debug.getupvalue(hfunc, 1))
  282. assert(to("tostring", {}) == nil)
  283. assert(to("tostring", "alo") == "alo")
  284. assert(to("tostring", 12) == "12")
  285. assert(to("tostring", 12, 3) == nil)
  286. assert(to("objsize", {}) == 0)
  287. assert(to("objsize", {1,2,3}) == 3)
  288. assert(to("objsize", "alo\0\0a") == 6)
  289. assert(to("objsize", T.newuserdata(0)) == 0)
  290. assert(to("objsize", T.newuserdata(101)) == 101)
  291. assert(to("objsize", 124) == 0)
  292. assert(to("objsize", true) == 0)
  293. assert(to("tonumber", {}) == 0)
  294. assert(to("tonumber", "12") == 12)
  295. assert(to("tonumber", "s2") == 0)
  296. assert(to("tonumber", 1, 20) == 0)
  297. assert(to("topointer", 10) == null)
  298. assert(to("topointer", true) == null)
  299. assert(to("topointer", nil) == null)
  300. assert(to("topointer", "abc") ~= null)
  301. assert(to("topointer", string.rep("x", 10)) ==
  302. to("topointer", string.rep("x", 10))) -- short strings
  303. do -- long strings
  304. local s1 = string.rep("x", 300)
  305. local s2 = string.rep("x", 300)
  306. assert(to("topointer", s1) ~= to("topointer", s2))
  307. end
  308. assert(to("topointer", T.pushuserdata(20)) ~= null)
  309. assert(to("topointer", io.read) ~= null) -- light C function
  310. assert(to("topointer", hfunc) ~= null) -- "heavy" C function
  311. assert(to("topointer", function () end) ~= null) -- Lua function
  312. assert(to("topointer", io.stdin) ~= null) -- full userdata
  313. assert(to("func2num", 20) == 0)
  314. assert(to("func2num", T.pushuserdata(10)) == 0)
  315. assert(to("func2num", io.read) ~= 0) -- light C function
  316. assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue)
  317. a = to("tocfunction", math.deg)
  318. assert(a(3) == math.deg(3) and a == math.deg)
  319. print("testing panic function")
  320. do
  321. -- trivial error
  322. assert(T.checkpanic("pushstring hi; error") == "hi")
  323. -- using the stack inside panic
  324. assert(T.checkpanic("pushstring hi; error;",
  325. [[checkstack 5 XX
  326. pushstring ' alo'
  327. pushstring ' mundo'
  328. concat 3]]) == "hi alo mundo")
  329. -- "argerror" without frames
  330. assert(T.checkpanic("loadstring 4") ==
  331. "bad argument #4 (string expected, got no value)")
  332. -- memory error
  333. T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
  334. assert(T.checkpanic("newuserdata 20000") == "not enough memory")
  335. T.totalmem(0) -- restore high limit
  336. -- stack error
  337. if not _soft then
  338. local msg = T.checkpanic[[
  339. pushstring "function f() f() end"
  340. loadstring -1; call 0 0
  341. getglobal f; call 0 0
  342. ]]
  343. assert(string.find(msg, "stack overflow"))
  344. end
  345. -- exit in panic still close to-be-closed variables
  346. assert(T.checkpanic([[
  347. pushstring "return {__close = function () Y = 'ho'; end}"
  348. newtable
  349. loadstring -2
  350. call 0 1
  351. setmetatable -2
  352. toclose -1
  353. pushstring "hi"
  354. error
  355. ]],
  356. [[
  357. getglobal Y
  358. concat 2 # concat original error with global Y
  359. ]]) == "hiho")
  360. end
  361. -- testing deep C stack
  362. if not _soft then
  363. print("testing stack overflow")
  364. collectgarbage("stop")
  365. checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep
  366. -- too deep (with no message)
  367. checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''")
  368. local s = string.rep("pushnil;checkstack 1 XX;", 1000000)
  369. checkerr("overflow", T.testC, s)
  370. collectgarbage("restart")
  371. print'+'
  372. end
  373. local lim = _soft and 500 or 12000
  374. local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
  375. for i = 1,lim do
  376. prog[#prog + 1] = "pushnum " .. i
  377. prog[#prog + 1] = "pushnum " .. i * 10
  378. end
  379. prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
  380. prog[#prog + 1] = "insert " .. -(2*lim + 2)
  381. for i = 1,lim do
  382. prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
  383. end
  384. prog[#prog + 1] = "return 2"
  385. prog = table.concat(prog, ";")
  386. local g, t = T.testC(prog)
  387. assert(g == _G)
  388. for i = 1,lim do assert(t[i] == i*10); t[i] = undef end
  389. assert(next(t) == nil)
  390. prog, g, t = nil
  391. -- testing errors
  392. a = T.testC([[
  393. loadstring 2; pcall 0 1 0;
  394. pushvalue 3; insert -2; pcall 1 1 0;
  395. pcall 0 0 0;
  396. return 1
  397. ]], "x=150", function (a) assert(a==nil); return 3 end)
  398. assert(type(a) == 'string' and x == 150)
  399. function check3(p, ...)
  400. local arg = {...}
  401. assert(#arg == 3)
  402. assert(string.find(arg[3], p))
  403. end
  404. check3(":1:", T.testC("loadstring 2; return *", "x="))
  405. check3("%.", T.testC("loadfile 2; return *", "."))
  406. check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
  407. -- test errors in non protected threads
  408. function checkerrnopro (code, msg)
  409. local th = coroutine.create(function () end) -- create new thread
  410. local stt, err = pcall(T.testC, th, code) -- run code there
  411. assert(not stt and string.find(err, msg))
  412. end
  413. if not _soft then
  414. checkerrnopro("pushnum 3; call 0 0", "attempt to call")
  415. print"testing stack overflow in unprotected thread"
  416. function f () f() end
  417. checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow")
  418. end
  419. print"+"
  420. -- testing table access
  421. do -- getp/setp
  422. local a = {}
  423. local a1 = T.testC("rawsetp 2 1; return 1", a, 20)
  424. assert(a == a1)
  425. assert(a[T.pushuserdata(1)] == 20)
  426. local a1, res = T.testC("rawgetp -1 1; return 2", a)
  427. assert(a == a1 and res == 20)
  428. end
  429. do -- using the table itself as index
  430. local a = {}
  431. a[a] = 10
  432. local prog = "gettable -1; return *"
  433. local res = {T.testC(prog, a)}
  434. assert(#res == 2 and res[1] == prog and res[2] == 10)
  435. local prog = "settable -2; return *"
  436. local res = {T.testC(prog, a, 20)}
  437. assert(a[a] == 20)
  438. assert(#res == 1 and res[1] == prog)
  439. -- raw
  440. a[a] = 10
  441. local prog = "rawget -1; return *"
  442. local res = {T.testC(prog, a)}
  443. assert(#res == 2 and res[1] == prog and res[2] == 10)
  444. local prog = "rawset -2; return *"
  445. local res = {T.testC(prog, a, 20)}
  446. assert(a[a] == 20)
  447. assert(#res == 1 and res[1] == prog)
  448. -- using the table as the value to set
  449. local prog = "rawset -1; return *"
  450. local res = {T.testC(prog, 30, a)}
  451. assert(a[30] == a)
  452. assert(#res == 1 and res[1] == prog)
  453. local prog = "settable -1; return *"
  454. local res = {T.testC(prog, 40, a)}
  455. assert(a[40] == a)
  456. assert(#res == 1 and res[1] == prog)
  457. local prog = "rawseti -1 100; return *"
  458. local res = {T.testC(prog, a)}
  459. assert(a[100] == a)
  460. assert(#res == 1 and res[1] == prog)
  461. local prog = "seti -1 200; return *"
  462. local res = {T.testC(prog, a)}
  463. assert(a[200] == a)
  464. assert(#res == 1 and res[1] == prog)
  465. end
  466. a = {x=0, y=12}
  467. x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
  468. a, 3, "y", 4, "x")
  469. assert(x == 0 and y == 12)
  470. T.testC("settable -5", a, 3, 4, "x", 15)
  471. assert(a.x == 15)
  472. a[a] = print
  473. x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
  474. assert(x == print)
  475. T.testC("settable 2", a, "x") -- table and key are the same object!
  476. assert(a[a] == "x")
  477. b = setmetatable({p = a}, {})
  478. getmetatable(b).__index = function (t, i) return t.p[i] end
  479. k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
  480. assert(x == 15 and k == 35)
  481. k = T.testC("getfield 2 y, return 1", b)
  482. assert(k == 12)
  483. getmetatable(b).__index = function (t, i) return a[i] end
  484. getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
  485. y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
  486. assert(y == 12)
  487. k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
  488. assert(a.x == 16 and k == 4)
  489. a[b] = 'xuxu'
  490. y = T.testC("gettable 2, return 1", b)
  491. assert(y == 'xuxu')
  492. T.testC("settable 2", b, 19)
  493. assert(a[b] == 19)
  494. --
  495. do -- testing getfield/setfield with long keys
  496. local t = {_012345678901234567890123456789012345678901234567890123456789 = 32}
  497. local a = T.testC([[
  498. getfield 2 _012345678901234567890123456789012345678901234567890123456789
  499. return 1
  500. ]], t)
  501. assert(a == 32)
  502. local a = T.testC([[
  503. pushnum 33
  504. setglobal _012345678901234567890123456789012345678901234567890123456789
  505. ]])
  506. assert(_012345678901234567890123456789012345678901234567890123456789 == 33)
  507. _012345678901234567890123456789012345678901234567890123456789 = nil
  508. end
  509. -- testing next
  510. a = {}
  511. t = pack(T.testC("next; return *", a, nil))
  512. tcheck(t, {n=1,a})
  513. a = {a=3}
  514. t = pack(T.testC("next; return *", a, nil))
  515. tcheck(t, {n=3,a,'a',3})
  516. t = pack(T.testC("next; pop 1; next; return *", a, nil))
  517. tcheck(t, {n=1,a})
  518. -- testing upvalues
  519. do
  520. local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  521. t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
  522. assert(b == 10 and c == 20 and type(t) == 'table')
  523. a, b = A([[tostring U3; tonumber U4; return 2]])
  524. assert(a == nil and b == 0)
  525. A([[pushnum 100; pushnum 200; replace U2; replace U1]])
  526. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  527. assert(b == 100 and c == 200)
  528. A([[replace U2; replace U1]], {x=1}, {x=2})
  529. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  530. assert(b.x == 1 and c.x == 2)
  531. T.checkmemory()
  532. end
  533. -- testing absent upvalues from C-function pointers
  534. assert(T.testC[[isnull U1; return 1]] == true)
  535. assert(T.testC[[isnull U100; return 1]] == true)
  536. assert(T.testC[[pushvalue U1; return 1]] == nil)
  537. local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  538. assert(T.upvalue(f, 1) == 10 and
  539. T.upvalue(f, 2) == 20 and
  540. T.upvalue(f, 3) == nil)
  541. T.upvalue(f, 2, "xuxu")
  542. assert(T.upvalue(f, 2) == "xuxu")
  543. -- large closures
  544. do
  545. local A = "checkstack 300 msg;" ..
  546. string.rep("pushnum 10;", 255) ..
  547. "pushcclosure 255; return 1"
  548. A = T.testC(A)
  549. for i=1,255 do
  550. assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
  551. end
  552. assert(A("isnull U256; return 1"))
  553. assert(not A("isnil U256; return 1"))
  554. end
  555. -- testing get/setuservalue
  556. -- bug in 5.1.2
  557. checkerr("got number", debug.setuservalue, 3, {})
  558. checkerr("got nil", debug.setuservalue, nil, {})
  559. checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})
  560. -- testing multiple user values
  561. local b = T.newuserdata(0, 10)
  562. for i = 1, 10 do
  563. local v, p = debug.getuservalue(b, i)
  564. assert(v == nil and p)
  565. end
  566. do -- indices out of range
  567. local v, p = debug.getuservalue(b, -2)
  568. assert(v == nil and not p)
  569. local v, p = debug.getuservalue(b, 11)
  570. assert(v == nil and not p)
  571. end
  572. local t = {true, false, 4.56, print, {}, b, "XYZ"}
  573. for k, v in ipairs(t) do
  574. debug.setuservalue(b, v, k)
  575. end
  576. for k, v in ipairs(t) do
  577. local v1, p = debug.getuservalue(b, k)
  578. assert(v1 == v and p)
  579. end
  580. assert(not debug.getuservalue(4))
  581. debug.setuservalue(b, function () return 10 end, 10)
  582. collectgarbage() -- function should not be collected
  583. assert(debug.getuservalue(b, 10)() == 10)
  584. debug.setuservalue(b, 134)
  585. collectgarbage() -- number should not be a problem for collector
  586. assert(debug.getuservalue(b) == 134)
  587. -- test barrier for uservalues
  588. do
  589. local oldmode = collectgarbage("incremental")
  590. T.gcstate("atomic")
  591. assert(T.gccolor(b) == "black")
  592. debug.setuservalue(b, {x = 100})
  593. T.gcstate("pause") -- complete collection
  594. assert(debug.getuservalue(b).x == 100) -- uvalue should be there
  595. collectgarbage(oldmode)
  596. end
  597. -- long chain of userdata
  598. for i = 1, 1000 do
  599. local bb = T.newuserdata(0, 1)
  600. debug.setuservalue(bb, b)
  601. b = bb
  602. end
  603. collectgarbage() -- nothing should not be collected
  604. for i = 1, 1000 do
  605. b = debug.getuservalue(b)
  606. end
  607. assert(debug.getuservalue(b).x == 100)
  608. b = nil
  609. -- testing locks (refs)
  610. -- reuse of references
  611. local i = T.ref{}
  612. T.unref(i)
  613. assert(T.ref{} == i)
  614. Arr = {}
  615. Lim = 100
  616. for i=1,Lim do -- lock many objects
  617. Arr[i] = T.ref({})
  618. end
  619. assert(T.ref(nil) == -1 and T.getref(-1) == nil)
  620. T.unref(-1); T.unref(-1)
  621. for i=1,Lim do -- unlock all them
  622. T.unref(Arr[i])
  623. end
  624. function printlocks ()
  625. local f = T.makeCfunc("gettable R; return 1")
  626. local n = f("n")
  627. print("n", n)
  628. for i=0,n do
  629. print(i, f(i))
  630. end
  631. end
  632. for i=1,Lim do -- lock many objects
  633. Arr[i] = T.ref({})
  634. end
  635. for i=1,Lim,2 do -- unlock half of them
  636. T.unref(Arr[i])
  637. end
  638. assert(type(T.getref(Arr[2])) == 'table')
  639. assert(T.getref(-1) == nil)
  640. a = T.ref({})
  641. collectgarbage()
  642. assert(type(T.getref(a)) == 'table')
  643. -- colect in cl the `val' of all collected userdata
  644. tt = {}
  645. cl = {n=0}
  646. A = nil; B = nil
  647. local F
  648. F = function (x)
  649. local udval = T.udataval(x)
  650. table.insert(cl, udval)
  651. local d = T.newuserdata(100) -- create garbage
  652. d = nil
  653. assert(debug.getmetatable(x).__gc == F)
  654. assert(load("table.insert({}, {})"))() -- create more garbage
  655. collectgarbage() -- force a GC during GC
  656. assert(debug.getmetatable(x).__gc == F) -- previous GC did not mess this?
  657. local dummy = {} -- create more garbage during GC
  658. if A ~= nil then
  659. assert(type(A) == "userdata")
  660. assert(T.udataval(A) == B)
  661. debug.getmetatable(A) -- just access it
  662. end
  663. A = x -- ressucita userdata
  664. B = udval
  665. return 1,2,3
  666. end
  667. tt.__gc = F
  668. -- test whether udate collection frees memory in the right time
  669. do
  670. collectgarbage();
  671. collectgarbage();
  672. local x = collectgarbage("count");
  673. local a = T.newuserdata(5001)
  674. assert(T.testC("objsize 2; return 1", a) == 5001)
  675. assert(collectgarbage("count") >= x+4)
  676. a = nil
  677. collectgarbage();
  678. assert(collectgarbage("count") <= x+1)
  679. -- udata without finalizer
  680. x = collectgarbage("count")
  681. collectgarbage("stop")
  682. for i=1,1000 do T.newuserdata(0) end
  683. assert(collectgarbage("count") > x+10)
  684. collectgarbage()
  685. assert(collectgarbage("count") <= x+1)
  686. -- udata with finalizer
  687. collectgarbage()
  688. x = collectgarbage("count")
  689. collectgarbage("stop")
  690. a = {__gc = function () end}
  691. for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end
  692. assert(collectgarbage("count") >= x+10)
  693. collectgarbage() -- this collection only calls TM, without freeing memory
  694. assert(collectgarbage("count") >= x+10)
  695. collectgarbage() -- now frees memory
  696. assert(collectgarbage("count") <= x+1)
  697. collectgarbage("restart")
  698. end
  699. collectgarbage("stop")
  700. -- create 3 userdatas with tag `tt'
  701. a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
  702. b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
  703. c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)
  704. -- create userdata without meta table
  705. x = T.newuserdata(4)
  706. y = T.newuserdata(0)
  707. checkerr("FILE%* expected, got userdata", io.input, a)
  708. checkerr("FILE%* expected, got userdata", io.input, x)
  709. assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
  710. d=T.ref(a);
  711. e=T.ref(b);
  712. f=T.ref(c);
  713. t = {T.getref(d), T.getref(e), T.getref(f)}
  714. assert(t[1] == a and t[2] == b and t[3] == c)
  715. t=nil; a=nil; c=nil;
  716. T.unref(e); T.unref(f)
  717. collectgarbage()
  718. -- check that unref objects have been collected
  719. assert(#cl == 1 and cl[1] == nc)
  720. x = T.getref(d)
  721. assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
  722. x =nil
  723. tt.b = b -- create cycle
  724. tt=nil -- frees tt for GC
  725. A = nil
  726. b = nil
  727. T.unref(d);
  728. n5 = T.newuserdata(0)
  729. debug.setmetatable(n5, {__gc=F})
  730. n5 = T.udataval(n5)
  731. collectgarbage()
  732. assert(#cl == 4)
  733. -- check order of collection
  734. assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
  735. collectgarbage"restart"
  736. a, na = {}, {}
  737. for i=30,1,-1 do
  738. a[i] = T.newuserdata(0)
  739. debug.setmetatable(a[i], {__gc=F})
  740. na[i] = T.udataval(a[i])
  741. end
  742. cl = {}
  743. a = nil; collectgarbage()
  744. assert(#cl == 30)
  745. for i=1,30 do assert(cl[i] == na[i]) end
  746. na = nil
  747. for i=2,Lim,2 do -- unlock the other half
  748. T.unref(Arr[i])
  749. end
  750. x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
  751. assert(T.testC("objsize 2; return 1", x) == 41)
  752. cl = {}
  753. a = {[x] = 1}
  754. x = T.udataval(x)
  755. collectgarbage()
  756. -- old `x' cannot be collected (`a' still uses it)
  757. assert(#cl == 0)
  758. for n in pairs(a) do a[n] = undef end
  759. collectgarbage()
  760. assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
  761. -- testing lua_equal
  762. assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20))
  763. assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo"))
  764. assert(T.testC("compare EQ 2 3; return 1", nil, nil))
  765. assert(not T.testC("compare EQ 2 3; return 1", {}, {}))
  766. assert(not T.testC("compare EQ 2 3; return 1"))
  767. assert(not T.testC("compare EQ 2 3; return 1", 3))
  768. -- testing lua_equal with fallbacks
  769. do
  770. local map = {}
  771. local t = {__eq = function (a,b) return map[a] == map[b] end}
  772. local function f(x)
  773. local u = T.newuserdata(0)
  774. debug.setmetatable(u, t)
  775. map[u] = x
  776. return u
  777. end
  778. assert(f(10) == f(10))
  779. assert(f(10) ~= f(11))
  780. assert(T.testC("compare EQ 2 3; return 1", f(10), f(10)))
  781. assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20)))
  782. t.__eq = nil
  783. assert(f(10) ~= f(10))
  784. end
  785. print'+'
  786. -- testing changing hooks during hooks
  787. _G.t = {}
  788. T.sethook([[
  789. # set a line hook after 3 count hooks
  790. sethook 4 0 '
  791. getglobal t;
  792. pushvalue -3; append -2
  793. pushvalue -2; append -2
  794. ']], "c", 3)
  795. local a = 1 -- counting
  796. a = 1 -- counting
  797. a = 1 -- count hook (set line hook)
  798. a = 1 -- line hook
  799. a = 1 -- line hook
  800. debug.sethook()
  801. t = _G.t
  802. assert(t[1] == "line")
  803. line = t[2]
  804. assert(t[3] == "line" and t[4] == line + 1)
  805. assert(t[5] == "line" and t[6] == line + 2)
  806. assert(t[7] == nil)
  807. -------------------------------------------------------------------------
  808. do -- testing errors during GC
  809. warn("@off")
  810. collectgarbage("stop")
  811. local a = {}
  812. for i=1,20 do
  813. a[i] = T.newuserdata(i) -- creates several udata
  814. end
  815. for i=1,20,2 do -- mark half of them to raise errors during GC
  816. debug.setmetatable(a[i],
  817. {__gc = function (x) error("@expected error in gc") end})
  818. end
  819. for i=2,20,2 do -- mark the other half to count and to create more garbage
  820. debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
  821. end
  822. a = nil
  823. _G.A = 0
  824. collectgarbage()
  825. assert(A == 10) -- number of normal collections
  826. collectgarbage("restart")
  827. warn("@on")
  828. end
  829. -------------------------------------------------------------------------
  830. -- test for userdata vals
  831. do
  832. local a = {}; local lim = 30
  833. for i=0,lim do a[i] = T.pushuserdata(i) end
  834. for i=0,lim do assert(T.udataval(a[i]) == i) end
  835. for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
  836. for i=0,lim do a[a[i]] = i end
  837. for i=0,lim do a[T.pushuserdata(i)] = i end
  838. assert(type(tostring(a[1])) == "string")
  839. end
  840. -------------------------------------------------------------------------
  841. -- testing multiple states
  842. T.closestate(T.newstate());
  843. L1 = T.newstate()
  844. assert(L1)
  845. assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
  846. assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
  847. a, b = T.doremote(L1, "return f()")
  848. assert(a == 'alo' and b == '3')
  849. T.doremote(L1, "_ERRORMESSAGE = nil")
  850. -- error: `sin' is not defined
  851. a, _, b = T.doremote(L1, "return sin(1)")
  852. assert(a == nil and b == 2) -- 2 == run-time error
  853. -- error: syntax error
  854. a, b, c = T.doremote(L1, "return a+")
  855. assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
  856. T.loadlib(L1)
  857. a, b, c = T.doremote(L1, [[
  858. string = require'string'
  859. a = require'_G'; assert(a == _G and require("_G") == a)
  860. io = require'io'; assert(type(io.read) == "function")
  861. assert(require("io") == io)
  862. a = require'table'; assert(type(a.insert) == "function")
  863. a = require'debug'; assert(type(a.getlocal) == "function")
  864. a = require'math'; assert(type(a.sin) == "function")
  865. return string.sub('okinama', 1, 2)
  866. ]])
  867. assert(a == "ok")
  868. T.closestate(L1);
  869. L1 = T.newstate()
  870. T.loadlib(L1)
  871. T.doremote(L1, "a = {}")
  872. T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
  873. settable -3]])
  874. assert(T.doremote(L1, "return a.x") == "1")
  875. T.closestate(L1)
  876. L1 = nil
  877. print('+')
  878. -------------------------------------------------------------------------
  879. -- testing to-be-closed variables
  880. -------------------------------------------------------------------------
  881. print"testing to-be-closed variables"
  882. do
  883. local openresource = {}
  884. local function newresource ()
  885. local x = setmetatable({10}, {__close = function(y)
  886. assert(openresource[#openresource] == y)
  887. openresource[#openresource] = nil
  888. y[1] = y[1] + 1
  889. end})
  890. openresource[#openresource + 1] = x
  891. return x
  892. end
  893. local a, b = T.testC([[
  894. call 0 1 # create resource
  895. pushnil
  896. toclose -2 # mark call result to be closed
  897. toclose -1 # mark nil to be closed (will be ignored)
  898. return 2
  899. ]], newresource)
  900. assert(a[1] == 11 and b == nil)
  901. assert(#openresource == 0) -- was closed
  902. -- repeat the test, but calling function in a 'multret' context
  903. local a = {T.testC([[
  904. call 0 1 # create resource
  905. toclose 2 # mark it to be closed
  906. return 2
  907. ]], newresource)}
  908. assert(type(a[1]) == "string" and a[2][1] == 11)
  909. assert(#openresource == 0) -- was closed
  910. -- closing by error
  911. local a, b = pcall(T.makeCfunc[[
  912. call 0 1 # create resource
  913. toclose -1 # mark it to be closed
  914. error # resource is the error object
  915. ]], newresource)
  916. assert(a == false and b[1] == 11)
  917. assert(#openresource == 0) -- was closed
  918. -- non-closable value
  919. local a, b = pcall(T.makeCfunc[[
  920. newtable # create non-closable object
  921. toclose -1 # mark it to be closed (should raise an error)
  922. abort # will not be executed
  923. ]])
  924. assert(a == false and
  925. string.find(b, "non%-closable value"))
  926. local function check (n)
  927. assert(#openresource == n)
  928. end
  929. -- closing resources with 'settop'
  930. local a = T.testC([[
  931. pushvalue 2
  932. call 0 1 # create resource
  933. toclose -1 # mark it to be closed
  934. pushvalue 2
  935. call 0 1 # create another resource
  936. toclose -1 # mark it to be closed
  937. pushvalue 3
  938. pushint 2 # there should be two open resources
  939. call 1 0
  940. pop 1 # pop second resource from the stack
  941. pushvalue 3
  942. pushint 1 # there should be one open resource
  943. call 1 0
  944. pop 1 # pop second resource from the stack
  945. pushint *
  946. return 1 # return stack size
  947. ]], newresource, check)
  948. assert(a == 3) -- no extra items left in the stack
  949. -- non-closable value
  950. local a, b = pcall(T.makeCfunc[[
  951. pushint 32
  952. toclose -1
  953. ]])
  954. assert(not a and string.find(b, "(C temporary)"))
  955. end
  956. -------------------------------------------------------------------------
  957. -- testing memory limits
  958. -------------------------------------------------------------------------
  959. print("memory-allocation errors")
  960. checkerr("block too big", T.newuserdata, math.maxinteger)
  961. collectgarbage()
  962. local f = load"local a={}; for i=1,100000 do a[i]=i end"
  963. T.alloccount(10)
  964. checkerr("not enough memory", f)
  965. T.alloccount() -- remove limit
  966. -- test memory errors; increase limit for number of allocations one
  967. -- by one, so that we get memory errors in all allocations of a given
  968. -- task, until there is enough allocations to complete the task without
  969. -- errors.
  970. function testamem (s, f)
  971. collectgarbage(); collectgarbage()
  972. local M = 0
  973. local a,b = nil
  974. while true do
  975. T.alloccount(M)
  976. a, b = pcall(f)
  977. T.alloccount() -- remove limit
  978. if a and b then break end -- stop when no more errors
  979. if not a and not -- `real' error?
  980. (string.find(b, "memory") or string.find(b, "overflow")) then
  981. error(b, 0) -- propagate it
  982. end
  983. M = M + 1 -- increase allocation limit
  984. end
  985. print(string.format("limit for %s: %d allocations", s, M))
  986. return b
  987. end
  988. -- doing nothing
  989. b = testamem("doing nothing", function () return 10 end)
  990. assert(b == 10)
  991. -- testing memory errors when creating a new state
  992. b = testamem("state creation", T.newstate)
  993. T.closestate(b); -- close new state
  994. testamem("empty-table creation", function ()
  995. return {}
  996. end)
  997. testamem("string creation", function ()
  998. return "XXX" .. "YYY"
  999. end)
  1000. testamem("coroutine creation", function()
  1001. return coroutine.create(print)
  1002. end)
  1003. -- testing to-be-closed variables
  1004. testamem("to-be-closed variables", function()
  1005. local flag
  1006. do
  1007. local x <close> =
  1008. setmetatable({}, {__close = function () flag = true end})
  1009. flag = false
  1010. local x = {}
  1011. end
  1012. return flag
  1013. end)
  1014. -- testing threads
  1015. -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
  1016. mt = T.testC("rawgeti R 1; return 1")
  1017. assert(type(mt) == "thread" and coroutine.running() == mt)
  1018. function expand (n,s)
  1019. if n==0 then return "" end
  1020. local e = string.rep("=", n)
  1021. return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
  1022. e, s, expand(n-1,s), e)
  1023. end
  1024. G=0; collectgarbage(); a =collectgarbage("count")
  1025. load(expand(20,"G=G+1"))()
  1026. assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
  1027. testamem("running code on new thread", function ()
  1028. return T.doonnewstack("x=1") == 0 -- try to create thread
  1029. end)
  1030. -- testing memory x compiler
  1031. testamem("loadstring", function ()
  1032. return load("x=1") -- try to do load a string
  1033. end)
  1034. local testprog = [[
  1035. local function foo () return end
  1036. local t = {"x"}
  1037. a = "aaa"
  1038. for i = 1, #t do a=a..t[i] end
  1039. return true
  1040. ]]
  1041. -- testing memory x dofile
  1042. _G.a = nil
  1043. local t =os.tmpname()
  1044. local f = assert(io.open(t, "w"))
  1045. f:write(testprog)
  1046. f:close()
  1047. testamem("dofile", function ()
  1048. local a = loadfile(t)
  1049. return a and a()
  1050. end)
  1051. assert(os.remove(t))
  1052. assert(_G.a == "aaax")
  1053. -- other generic tests
  1054. testamem("gsub", function ()
  1055. local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
  1056. return (a == 'ablo ablo')
  1057. end)
  1058. testamem("dump/undump", function ()
  1059. local a = load(testprog)
  1060. local b = a and string.dump(a)
  1061. a = b and load(b)
  1062. return a and a()
  1063. end)
  1064. local t = os.tmpname()
  1065. testamem("file creation", function ()
  1066. local f = assert(io.open(t, 'w'))
  1067. assert (not io.open"nomenaoexistente")
  1068. io.close(f);
  1069. return not loadfile'nomenaoexistente'
  1070. end)
  1071. assert(os.remove(t))
  1072. testamem("table creation", function ()
  1073. local a, lim = {}, 10
  1074. for i=1,lim do a[i] = i; a[i..'a'] = {} end
  1075. return (type(a[lim..'a']) == 'table' and a[lim] == lim)
  1076. end)
  1077. testamem("constructors", function ()
  1078. local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
  1079. return (type(a) == 'table' and a.e == 5)
  1080. end)
  1081. local a = 1
  1082. close = nil
  1083. testamem("closure creation", function ()
  1084. function close (b)
  1085. return function (x) return b + x end
  1086. end
  1087. return (close(2)(4) == 6)
  1088. end)
  1089. testamem("using coroutines", function ()
  1090. local a = coroutine.wrap(function ()
  1091. coroutine.yield(string.rep("a", 10))
  1092. return {}
  1093. end)
  1094. assert(string.len(a()) == 10)
  1095. return a()
  1096. end)
  1097. do -- auxiliary buffer
  1098. local lim = 100
  1099. local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
  1100. testamem("auxiliary buffer", function ()
  1101. return (#table.concat(a, ",") == 20*lim + lim - 1)
  1102. end)
  1103. end
  1104. testamem("growing stack", function ()
  1105. local function foo (n)
  1106. if n == 0 then return 1 else return 1 + foo(n - 1) end
  1107. end
  1108. return foo(100)
  1109. end)
  1110. do -- testing failing in 'lua_checkstack'
  1111. local res = T.testC([[rawcheckstack 500000; return 1]])
  1112. assert(res == false)
  1113. local L = T.newstate()
  1114. T.alloccount(0) -- will be unable to reallocate the stack
  1115. res = T.testC(L, [[rawcheckstack 5000; return 1]])
  1116. T.alloccount()
  1117. T.closestate(L)
  1118. assert(res == false)
  1119. end
  1120. do -- closing state with no extra memory
  1121. local L = T.newstate()
  1122. T.alloccount(0)
  1123. T.closestate(L)
  1124. T.alloccount()
  1125. end
  1126. do -- garbage collection with no extra memory
  1127. local L = T.newstate()
  1128. T.loadlib(L)
  1129. local res = (T.doremote(L, [[
  1130. _ENV = require"_G"
  1131. local T = require"T"
  1132. local a = {}
  1133. for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table
  1134. local stsize, stuse = T.querystr()
  1135. assert(stuse > 1000)
  1136. local function foo (n)
  1137. if n > 0 then foo(n - 1) end
  1138. end
  1139. foo(180) -- grow stack
  1140. local _, stksize = T.stacklevel()
  1141. assert(stksize > 180)
  1142. a = nil
  1143. T.alloccount(0)
  1144. collectgarbage()
  1145. T.alloccount()
  1146. -- stack and string table could not be reallocated,
  1147. -- so they kept their sizes (without errors)
  1148. assert(select(2, T.stacklevel()) == stksize)
  1149. assert(T.querystr() == stsize)
  1150. return 'ok'
  1151. ]]))
  1152. assert(res == 'ok')
  1153. T.closestate(L)
  1154. end
  1155. print'+'
  1156. -- testing some auxlib functions
  1157. local function gsub (a, b, c)
  1158. a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
  1159. assert(b == 5)
  1160. return a
  1161. end
  1162. assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
  1163. assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
  1164. assert(gsub("", "alo", "//") == "")
  1165. assert(gsub("...", ".", "/.") == "/././.")
  1166. assert(gsub("...", "...", "") == "")
  1167. -- testing luaL_newmetatable
  1168. local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  1169. assert(type(mt_xuxu) == "table" and res and top == 3)
  1170. local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  1171. assert(mt_xuxu == d and not res and top == 3)
  1172. d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
  1173. assert(mt_xuxu ~= d and res and top == 3)
  1174. x = T.newuserdata(0);
  1175. y = T.newuserdata(0);
  1176. T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
  1177. assert(getmetatable(x) == mt_xuxu)
  1178. -- testing luaL_testudata
  1179. -- correct metatable
  1180. local res1, res2, top = T.testC([[testudata -1 xuxu
  1181. testudata 2 xuxu
  1182. gettop
  1183. return 3]], x)
  1184. assert(res1 and res2 and top == 4)
  1185. -- wrong metatable
  1186. res1, res2, top = T.testC([[testudata -1 xuxu1
  1187. testudata 2 xuxu1
  1188. gettop
  1189. return 3]], x)
  1190. assert(not res1 and not res2 and top == 4)
  1191. -- non-existent type
  1192. res1, res2, top = T.testC([[testudata -1 xuxu2
  1193. testudata 2 xuxu2
  1194. gettop
  1195. return 3]], x)
  1196. assert(not res1 and not res2 and top == 4)
  1197. -- userdata has no metatable
  1198. res1, res2, top = T.testC([[testudata -1 xuxu
  1199. testudata 2 xuxu
  1200. gettop
  1201. return 3]], y)
  1202. assert(not res1 and not res2 and top == 4)
  1203. -- erase metatables
  1204. do
  1205. local r = debug.getregistry()
  1206. assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
  1207. r.xuxu = nil; r.xuxu1 = nil
  1208. end
  1209. print'OK'