api.lua 44 KB

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