api.lua 36 KB

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