api.lua 38 KB

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