api.lua 44 KB

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