api.lua 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. if T==nil then
  2. (Message or print)('\a\n >>> testC not active: skipping API tests <<<\n\a')
  3. return
  4. end
  5. local debug = require "debug"
  6. local pack = table.pack
  7. function tcheck (t1, t2)
  8. assert(t1.n == (t2.n or #t2) + 1)
  9. for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
  10. end
  11. print('testing C API')
  12. a = T.testC("pushvalue R; return 1")
  13. assert(a == debug.getregistry())
  14. -- absindex
  15. assert(T.testC("settop 10; absindex -1; return 1") == 10)
  16. assert(T.testC("settop 5; absindex -5; return 1") == 1)
  17. assert(T.testC("settop 10; absindex 1; return 1") == 1)
  18. assert(T.testC("settop 10; absindex R; return 1") < -10)
  19. -- testing allignment
  20. a = T.d2s(12458954321123)
  21. assert(string.len(a) == 8) -- sizeof(double)
  22. assert(T.s2d(a) == 12458954321123)
  23. a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
  24. assert(a == 2 and b == 3 and not c)
  25. f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
  26. a,b,c = f()
  27. assert(a == 2 and b == 3 and not c)
  28. -- test that all trues are equal
  29. a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
  30. assert(a == b and a == true and c == false)
  31. a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\
  32. tobool -3; tobool -3; tobool -3; return 3"
  33. assert(a==false and b==true and c==false)
  34. a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
  35. assert(a == 40 and b == 5 and not c)
  36. t = pack(T.testC("settop 5; gettop; return .", 2, 3))
  37. tcheck(t, {n=4,2,3})
  38. t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
  39. assert(t.n == 10 and t[1] == nil and t[10] == nil)
  40. t = pack(T.testC("remove -2; gettop; return .", 2, 3, 4))
  41. tcheck(t, {n=2,2,4})
  42. t = pack(T.testC("insert -1; gettop; return .", 2, 3))
  43. tcheck(t, {n=2,2,3})
  44. t = pack(T.testC("insert 3; gettop; return .", 2, 3, 4, 5))
  45. tcheck(t, {n=4,2,5,3,4})
  46. t = pack(T.testC("replace 2; gettop; return .", 2, 3, 4, 5))
  47. tcheck(t, {n=3,5,3,4})
  48. t = pack(T.testC("replace -2; gettop; return .", 2, 3, 4, 5))
  49. tcheck(t, {n=3,2,3,5})
  50. t = pack(T.testC("remove 3; gettop; return .", 2, 3, 4, 5))
  51. tcheck(t, {n=3,2,4,5})
  52. t = pack(T.testC("copy 3 4; gettop; return .", 2, 3, 4, 5))
  53. tcheck(t, {n=4,2,3,3,5})
  54. t = pack(T.testC("copy -3 -1; gettop; return .", 2, 3, 4, 5))
  55. tcheck(t, {n=4,2,3,4,3})
  56. t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \
  57. insert 2; pushvalue 1; remove 1; insert 1; \
  58. insert -2; pushvalue -2; remove -3; gettop; return .",
  59. 2, 3, 4, 5, 10, 40, 90))
  60. tcheck(t, {n=7,2,3,4,5,10,40,90})
  61. t = pack(T.testC("concat 5; gettop; return .", "alo", 2, 3, "joao", 12))
  62. tcheck(t, {n=1,"alo23joao12"})
  63. -- testing MULTRET
  64. t = pack(T.testC("call 2,-1; gettop; return .",
  65. function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
  66. tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
  67. do -- test returning more results than fit in the caller stack
  68. local a = {}
  69. for i=1,1000 do a[i] = true end; a[999] = 10
  70. local b = T.testC([[pcall 1 -1; pop 1; tostring -1; return 1]],
  71. table.unpack, a)
  72. assert(b == "10")
  73. end
  74. -- testing globals
  75. _G.a = 14; _G.b = "a31"
  76. local a = {T.testC[[
  77. getglobal a;
  78. getglobal b;
  79. getglobal b;
  80. setglobal a;
  81. gettop;
  82. return .
  83. ]]}
  84. assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31")
  85. -- testing arith
  86. assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
  87. assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
  88. assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
  89. assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
  90. assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
  91. assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
  92. assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
  93. assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
  94. a,b,c = T.testC([[pushnum 1;
  95. pushstring 10; arith _;
  96. pushstring 5; return 3]])
  97. assert(a == 1 and b == -10 and c == "5")
  98. mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
  99. __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
  100. __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
  101. a,b,c = setmetatable({4}, mt),
  102. setmetatable({8}, mt),
  103. setmetatable({-3}, mt)
  104. x,y,z = T.testC("arith +; return 2", 10, a, b)
  105. assert(x == 10 and y[1] == 12 and z == nil)
  106. assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
  107. assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
  108. 8 % (4 + (-3)*2))
  109. -- testing compare
  110. -- EQ = 0; LT = 1; LE = 2
  111. -- testing lessthan and lessequal
  112. assert(T.testC("compare 2 5 1, return 1", 3, 2, 2, 4, 2, 2))
  113. assert(T.testC("compare 2 5 2, return 1", 3, 2, 2, 4, 2, 2))
  114. assert(not T.testC("compare 3 4 1, return 1", 3, 2, 2, 4, 2, 2))
  115. assert(T.testC("compare 3 4 2, return 1", 3, 2, 2, 4, 2, 2))
  116. assert(T.testC("compare 5 2 1, return 1", 4, 2, 2, 3, 2, 2))
  117. assert(not T.testC("compare 2 -3 1, return 1", "4", "2", "2", "3", "2", "2"))
  118. assert(not T.testC("compare -3 2 1, return 1", "3", "2", "2", "4", "2", "2"))
  119. -- non-valid indices produce false
  120. assert(not T.testC("compare 1 4 1, return 1"))
  121. assert(not T.testC("compare 9 1 2, return 1"))
  122. assert(not T.testC("compare 9 9 0, return 1"))
  123. local b = {__lt = function (a,b) return a[1] < b[1] end}
  124. local a1,a3,a4 = setmetatable({1}, b),
  125. setmetatable({3}, b),
  126. setmetatable({4}, b)
  127. assert(T.testC("compare 2 5 1, return 1", a3, 2, 2, a4, 2, 2))
  128. assert(T.testC("compare 2 5 2, return 1", a3, 2, 2, a4, 2, 2))
  129. assert(T.testC("compare 5 -6 1, return 1", a4, 2, 2, a3, 2, 2))
  130. a,b = T.testC("compare 5 -6 1, return 2", a1, 2, 2, a3, 2, 20)
  131. assert(a == 20 and b == false)
  132. a,b = T.testC("compare 5 -6 2, return 2", a1, 2, 2, a3, 2, 20)
  133. assert(a == 20 and b == false)
  134. a,b = T.testC("compare 5 -6 2, return 2", a1, 2, 2, a1, 2, 20)
  135. assert(a == 20 and b == true)
  136. -- testing length
  137. local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
  138. a,b,c = T.testC([[
  139. len 2;
  140. Llen 2;
  141. objsize 2;
  142. return 3
  143. ]], t)
  144. assert(a == 20 and b == 20 and c == 0)
  145. t.x = "234"; t[1] = 20
  146. a,b,c = T.testC([[
  147. len 2;
  148. Llen 2;
  149. objsize 2;
  150. return 3
  151. ]], t)
  152. assert(a == "234" and b == 234 and c == 1)
  153. t.x = print; t[1] = 20
  154. a,c = T.testC([[
  155. len 2;
  156. objsize 2;
  157. return 2
  158. ]], t)
  159. assert(a == print and c == 1)
  160. -- testing __concat
  161. a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
  162. x,y = T.testC([[
  163. pushnum 5
  164. pushvalue 2;
  165. pushvalue 2;
  166. concat 2;
  167. pushvalue -2;
  168. return 2;
  169. ]], a, a)
  170. assert(x == a..a and y == 5)
  171. -- concat with 0 elements
  172. assert(T.testC("concat 0; return 1") == "")
  173. -- concat with 1 element
  174. assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
  175. -- testing lua_is
  176. function B(x) return x and 1 or 0 end
  177. function count (x, n)
  178. n = n or 2
  179. local prog = [[
  180. isnumber %d;
  181. isstring %d;
  182. isfunction %d;
  183. iscfunction %d;
  184. istable %d;
  185. isuserdata %d;
  186. isnil %d;
  187. isnull %d;
  188. return 8
  189. ]]
  190. prog = string.format(prog, n, n, n, n, n, n, n, n)
  191. local a,b,c,d,e,f,g,h = T.testC(prog, x)
  192. return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h))
  193. end
  194. assert(count(3) == 2)
  195. assert(count('alo') == 1)
  196. assert(count('32') == 2)
  197. assert(count({}) == 1)
  198. assert(count(print) == 2)
  199. assert(count(function () end) == 1)
  200. assert(count(nil) == 1)
  201. assert(count(io.stdin) == 1)
  202. assert(count(nil, 15) == 100)
  203. -- testing lua_to...
  204. function to (s, x, n)
  205. n = n or 2
  206. return T.testC(string.format("%s %d; return 1", s, n), x)
  207. end
  208. assert(to("tostring", {}) == nil)
  209. assert(to("tostring", "alo") == "alo")
  210. assert(to("tostring", 12) == "12")
  211. assert(to("tostring", 12, 3) == nil)
  212. assert(to("objsize", {}) == 0)
  213. assert(to("objsize", {1,2,3}) == 3)
  214. assert(to("objsize", "alo\0\0a") == 6)
  215. assert(to("objsize", T.newuserdata(0)) == 0)
  216. assert(to("objsize", T.newuserdata(101)) == 101)
  217. assert(to("objsize", 124) == 0)
  218. assert(to("objsize", true) == 0)
  219. assert(to("tonumber", {}) == 0)
  220. assert(to("tonumber", "12") == 12)
  221. assert(to("tonumber", "s2") == 0)
  222. assert(to("tonumber", 1, 20) == 0)
  223. assert(to("topointer", 10) == 0)
  224. assert(to("topointer", true) == 0)
  225. assert(to("topointer", T.pushuserdata(20)) == 20)
  226. assert(to("topointer", io.read) ~= 0)
  227. assert(to("func2num", 20) == 0)
  228. assert(to("func2num", T.pushuserdata(10)) == 0)
  229. assert(to("func2num", io.read) ~= 0)
  230. a = to("tocfunction", math.deg)
  231. assert(a(3) == math.deg(3) and a == math.deg)
  232. -- testing deep C stack
  233. do
  234. collectgarbage("stop")
  235. local s, msg = pcall(T.testC, "checkstack 1000023 XXXX") -- too deep
  236. assert(not s and string.find(msg, "XXXX"))
  237. s = string.rep("pushnil;checkstack 1 XX;", 1000000)
  238. s, msg = pcall(T.testC, s)
  239. assert(not s and string.find(msg, "XX"))
  240. collectgarbage("restart")
  241. end
  242. local prog = {"checkstack 30000 msg", "newtable"}
  243. for i = 1,12000 do
  244. prog[#prog + 1] = "pushnum " .. i
  245. prog[#prog + 1] = "pushnum " .. i * 10
  246. end
  247. prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
  248. prog[#prog + 1] = "insert " .. -(2*12000 + 2)
  249. for i = 1,12000 do
  250. prog[#prog + 1] = "settable " .. -(2*(12000 - i + 1) + 1)
  251. end
  252. prog[#prog + 1] = "return 2"
  253. prog = table.concat(prog, ";")
  254. local g, t = T.testC(prog)
  255. assert(g == _G)
  256. for i = 1,12000 do assert(t[i] == i*10); t[i] = nil end
  257. assert(next(t) == nil)
  258. prog, g, t = nil
  259. -- testing errors
  260. a = T.testC([[
  261. loadstring 2; pcall 0,1;
  262. pushvalue 3; insert -2; pcall 1, 1;
  263. pcall 0, 0;
  264. return 1
  265. ]], "x=150", function (a) assert(a==nil); return 3 end)
  266. assert(type(a) == 'string' and x == 150)
  267. function check3(p, ...)
  268. local arg = {...}
  269. assert(#arg == 3)
  270. assert(string.find(arg[3], p))
  271. end
  272. check3(":1:", T.testC("loadstring 2; gettop; return .", "x="))
  273. check3("cannot read", T.testC("loadfile 2; gettop; return .", "."))
  274. check3("cannot open xxxx", T.testC("loadfile 2; gettop; return .", "xxxx"))
  275. -- test errors in non protected threads
  276. function checkerrnopro (code, msg)
  277. L = coroutine.create(function () end)
  278. local stt, err = pcall(T.testC, code)
  279. assert(not stt and string.find(err, msg))
  280. end
  281. checkerrnopro("pushnum 3; call 0 0", "attempt to call")
  282. function f () f() end
  283. checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow")
  284. -- testing table access
  285. a = {x=0, y=12}
  286. x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
  287. a, 3, "y", 4, "x")
  288. assert(x == 0 and y == 12)
  289. T.testC("settable -5", a, 3, 4, "x", 15)
  290. assert(a.x == 15)
  291. a[a] = print
  292. x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
  293. assert(x == print)
  294. T.testC("settable 2", a, "x") -- table and key are the same object!
  295. assert(a[a] == "x")
  296. b = setmetatable({p = a}, {})
  297. getmetatable(b).__index = function (t, i) return t.p[i] end
  298. k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
  299. assert(x == 15 and k == 35)
  300. getmetatable(b).__index = function (t, i) return a[i] end
  301. getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
  302. y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
  303. assert(y == 12)
  304. k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
  305. assert(a.x == 16 and k == 4)
  306. a[b] = 'xuxu'
  307. y = T.testC("gettable 2, return 1", b)
  308. assert(y == 'xuxu')
  309. T.testC("settable 2", b, 19)
  310. assert(a[b] == 19)
  311. -- testing next
  312. a = {}
  313. t = pack(T.testC("next; gettop; return .", a, nil))
  314. tcheck(t, {n=1,a})
  315. a = {a=3}
  316. t = pack(T.testC("next; gettop; return .", a, nil))
  317. tcheck(t, {n=3,a,'a',3})
  318. t = pack(T.testC("next; pop 1; next; gettop; return .", a, nil))
  319. tcheck(t, {n=1,a})
  320. -- testing upvalues
  321. do
  322. local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  323. t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
  324. assert(b == 10 and c == 20 and type(t) == 'table')
  325. a, b = A([[tostring U3; tonumber U4; return 2]])
  326. assert(a == nil and b == 0)
  327. A([[pushnum 100; pushnum 200; replace U2; replace U1]])
  328. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  329. assert(b == 100 and c == 200)
  330. A([[replace U2; replace U1]], {x=1}, {x=2})
  331. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  332. assert(b.x == 1 and c.x == 2)
  333. T.checkmemory()
  334. end
  335. -- testing absent upvalues from C-function pointers
  336. assert(T.testC[[isnull U1; return 1]] == true)
  337. assert(T.testC[[isnull U100; return 1]] == true)
  338. assert(T.testC[[pushvalue U1; return 1]] == nil)
  339. local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  340. assert(T.upvalue(f, 1) == 10 and
  341. T.upvalue(f, 2) == 20 and
  342. T.upvalue(f, 3) == nil)
  343. T.upvalue(f, 2, "xuxu")
  344. assert(T.upvalue(f, 2) == "xuxu")
  345. -- large closures
  346. do
  347. local A = "checkstack 300 msg;" ..
  348. string.rep("pushnum 10;", 255) ..
  349. "pushcclosure 255; return 1"
  350. A = T.testC(A)
  351. for i=1,255 do
  352. assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
  353. end
  354. assert(A("isnull U256; return 1"))
  355. assert(not A("isnil U256; return 1"))
  356. end
  357. -- bug in 5.1.2
  358. assert(not pcall(debug.setuservalue, 3, {}))
  359. assert(not pcall(debug.setuservalue, nil, {}))
  360. assert(not pcall(debug.setuservalue, T.pushuserdata(1), {}))
  361. local b = T.newuserdata(0)
  362. local a = {}
  363. assert(debug.getuservalue(b) == nil)
  364. assert(debug.setuservalue(b, a))
  365. assert(debug.getuservalue(b) == a)
  366. assert(debug.setuservalue(b, nil))
  367. assert(debug.getuservalue(b) == nil)
  368. assert(debug.getuservalue(4) == nil)
  369. -- testing locks (refs)
  370. -- reuse of references
  371. local i = T.ref{}
  372. T.unref(i)
  373. assert(T.ref{} == i)
  374. Arr = {}
  375. Lim = 100
  376. for i=1,Lim do -- lock many objects
  377. Arr[i] = T.ref({})
  378. end
  379. assert(T.ref(nil) == -1 and T.getref(-1) == nil)
  380. T.unref(-1); T.unref(-1)
  381. for i=1,Lim do -- unlock all them
  382. T.unref(Arr[i])
  383. end
  384. function printlocks ()
  385. local f = T.makeCfunc("gettable R; return 1")
  386. local n = f("n")
  387. print("n", n)
  388. for i=0,n do
  389. print(i, f(i))
  390. end
  391. end
  392. for i=1,Lim do -- lock many objects
  393. Arr[i] = T.ref({})
  394. end
  395. for i=1,Lim,2 do -- unlock half of them
  396. T.unref(Arr[i])
  397. end
  398. assert(type(T.getref(Arr[2])) == 'table')
  399. assert(T.getref(-1) == nil)
  400. a = T.ref({})
  401. collectgarbage()
  402. assert(type(T.getref(a)) == 'table')
  403. -- colect in cl the `val' of all collected userdata
  404. tt = {}
  405. cl = {n=0}
  406. A = nil; B = nil
  407. local F
  408. F = function (x)
  409. local udval = T.udataval(x)
  410. table.insert(cl, udval)
  411. local d = T.newuserdata(100) -- cria lixo
  412. d = nil
  413. assert(debug.getmetatable(x).__gc == F)
  414. assert(load("table.insert({}, {})"))() -- cria mais lixo
  415. collectgarbage() -- forca coleta de lixo durante coleta!
  416. assert(debug.getmetatable(x).__gc == F) -- coleta anterior nao melou isso?
  417. local dummy = {} -- cria lixo durante coleta
  418. if A ~= nil then
  419. assert(type(A) == "userdata")
  420. assert(T.udataval(A) == B)
  421. debug.getmetatable(A) -- just acess it
  422. end
  423. A = x -- ressucita userdata
  424. B = udval
  425. return 1,2,3
  426. end
  427. tt.__gc = F
  428. -- test whether udate collection frees memory in the right time
  429. do
  430. collectgarbage();
  431. collectgarbage();
  432. local x = collectgarbage("count");
  433. local a = T.newuserdata(5001)
  434. assert(T.testC("objsize 2; return 1", a) == 5001)
  435. assert(collectgarbage("count") >= x+4)
  436. a = nil
  437. collectgarbage();
  438. assert(collectgarbage("count") <= x+1)
  439. -- udata without finalizer
  440. x = collectgarbage("count")
  441. collectgarbage("stop")
  442. for i=1,1000 do T.newuserdata(0) end
  443. assert(collectgarbage("count") > x+10)
  444. collectgarbage()
  445. assert(collectgarbage("count") <= x+1)
  446. -- udata with finalizer
  447. x = collectgarbage("count")
  448. collectgarbage()
  449. collectgarbage("stop")
  450. a = {__gc = function () end}
  451. for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end
  452. assert(collectgarbage("count") >= x+10)
  453. collectgarbage() -- this collection only calls TM, without freeing memory
  454. assert(collectgarbage("count") >= x+10)
  455. collectgarbage() -- now frees memory
  456. assert(collectgarbage("count") <= x+1)
  457. collectgarbage("restart")
  458. end
  459. collectgarbage("stop")
  460. -- create 3 userdatas with tag `tt'
  461. a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
  462. b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
  463. c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)
  464. -- create userdata without meta table
  465. x = T.newuserdata(4)
  466. y = T.newuserdata(0)
  467. assert(not pcall(io.input, a))
  468. assert(not pcall(io.input, x))
  469. assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
  470. d=T.ref(a);
  471. e=T.ref(b);
  472. f=T.ref(c);
  473. t = {T.getref(d), T.getref(e), T.getref(f)}
  474. assert(t[1] == a and t[2] == b and t[3] == c)
  475. t=nil; a=nil; c=nil;
  476. T.unref(e); T.unref(f)
  477. collectgarbage()
  478. -- check that unref objects have been collected
  479. assert(#cl == 1 and cl[1] == nc)
  480. x = T.getref(d)
  481. assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
  482. x =nil
  483. tt.b = b -- create cycle
  484. tt=nil -- frees tt for GC
  485. A = nil
  486. b = nil
  487. T.unref(d);
  488. n5 = T.newuserdata(0)
  489. debug.setmetatable(n5, {__gc=F})
  490. n5 = T.udataval(n5)
  491. collectgarbage()
  492. assert(#cl == 4)
  493. -- check order of collection
  494. assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
  495. collectgarbage"restart"
  496. a, na = {}, {}
  497. for i=30,1,-1 do
  498. a[i] = T.newuserdata(0)
  499. debug.setmetatable(a[i], {__gc=F})
  500. na[i] = T.udataval(a[i])
  501. end
  502. cl = {}
  503. a = nil; collectgarbage()
  504. assert(#cl == 30)
  505. for i=1,30 do assert(cl[i] == na[i]) end
  506. na = nil
  507. for i=2,Lim,2 do -- unlock the other half
  508. T.unref(Arr[i])
  509. end
  510. x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
  511. assert(T.testC("objsize 2; return 1", x) == 41)
  512. cl = {}
  513. a = {[x] = 1}
  514. x = T.udataval(x)
  515. collectgarbage()
  516. -- old `x' cannot be collected (`a' still uses it)
  517. assert(#cl == 0)
  518. for n in pairs(a) do a[n] = nil end
  519. collectgarbage()
  520. assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
  521. -- testing lua_equal
  522. assert(T.testC("compare 2 4 0; return 1", print, 1, print, 20))
  523. assert(T.testC("compare 3 2 0; return 1", 'alo', "alo"))
  524. assert(T.testC("compare 2 3 0; return 1", nil, nil))
  525. assert(not T.testC("compare 2 3 0; return 1", {}, {}))
  526. assert(not T.testC("compare 2 3 0; return 1"))
  527. assert(not T.testC("compare 2 3 0; return 1", 3))
  528. -- testing lua_equal with fallbacks
  529. do
  530. local map = {}
  531. local t = {__eq = function (a,b) return map[a] == map[b] end}
  532. local function f(x)
  533. local u = T.newuserdata(0)
  534. debug.setmetatable(u, t)
  535. map[u] = x
  536. return u
  537. end
  538. assert(f(10) == f(10))
  539. assert(f(10) ~= f(11))
  540. assert(T.testC("compare 2 3 0; return 1", f(10), f(10)))
  541. assert(not T.testC("compare 2 3 0; return 1", f(10), f(20)))
  542. t.__eq = nil
  543. assert(f(10) ~= f(10))
  544. end
  545. print'+'
  546. -- testing changing hooks during hooks
  547. _G.t = {}
  548. T.sethook([[
  549. # set a line hook after 3 count hooks
  550. sethook 4 0 '
  551. getglobal t;
  552. pushvalue -3; append -2
  553. pushvalue -2; append -2
  554. ']], "c", 3)
  555. local a = 1 -- counting
  556. a = 1 -- counting
  557. a = 1 -- count hook (set line hook)
  558. a = 1 -- line hook
  559. a = 1 -- line hook
  560. debug.sethook()
  561. t = _G.t
  562. assert(t[1] == "line")
  563. line = t[2]
  564. assert(t[3] == "line" and t[4] == line + 1)
  565. assert(t[5] == "line" and t[6] == line + 2)
  566. assert(t[7] == nil)
  567. -------------------------------------------------------------------------
  568. do -- testing errors during GC
  569. local a = {}
  570. for i=1,20 do
  571. a[i] = T.newuserdata(i) -- creates several udata
  572. end
  573. for i=1,20,2 do -- mark half of them to raise errors during GC
  574. debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end})
  575. end
  576. for i=2,20,2 do -- mark the other half to count and to create more garbage
  577. debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
  578. end
  579. _G.A = 0
  580. a = 0
  581. while 1 do
  582. local stat, msg = pcall(collectgarbage)
  583. if stat then
  584. break -- stop when no more errors
  585. else
  586. a = a + 1
  587. assert(string.find(msg, "__gc"))
  588. end
  589. end
  590. assert(a == 10) -- number of errors
  591. assert(A == 10) -- number of normal collections
  592. end
  593. -------------------------------------------------------------------------
  594. -- test for userdata vals
  595. do
  596. local a = {}; local lim = 30
  597. for i=0,lim do a[i] = T.pushuserdata(i) end
  598. for i=0,lim do assert(T.udataval(a[i]) == i) end
  599. for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
  600. for i=0,lim do a[a[i]] = i end
  601. for i=0,lim do a[T.pushuserdata(i)] = i end
  602. assert(type(tostring(a[1])) == "string")
  603. end
  604. -------------------------------------------------------------------------
  605. -- testing multiple states
  606. T.closestate(T.newstate());
  607. L1 = T.newstate()
  608. assert(L1)
  609. assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
  610. assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
  611. a, b = T.doremote(L1, "return f()")
  612. assert(a == 'alo' and b == '3')
  613. T.doremote(L1, "_ERRORMESSAGE = nil")
  614. -- error: `sin' is not defined
  615. a, _, b = T.doremote(L1, "return sin(1)")
  616. assert(a == nil and b == 2) -- 2 == run-time error
  617. -- error: syntax error
  618. a, b, c = T.doremote(L1, "return a+")
  619. assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
  620. T.loadlib(L1)
  621. a, b, c = T.doremote(L1, [[
  622. string = require'string'
  623. a = require'_G'; assert(a == _G and require("_G") == a)
  624. io = require'io'; assert(type(io.read) == "function")
  625. assert(require("io") == io)
  626. a = require'table'; assert(type(a.insert) == "function")
  627. a = require'debug'; assert(type(a.getlocal) == "function")
  628. a = require'math'; assert(type(a.sin) == "function")
  629. return string.sub('okinama', 1, 2)
  630. ]])
  631. assert(a == "ok")
  632. T.closestate(L1);
  633. L1 = T.newstate()
  634. T.loadlib(L1)
  635. T.doremote(L1, "a = {}")
  636. T.testC(L1, [[getglobal "a"; pushstring "x"; pushnum 1;
  637. settable -3]])
  638. assert(T.doremote(L1, "return a.x") == "1")
  639. T.closestate(L1)
  640. L1 = nil
  641. print('+')
  642. -------------------------------------------------------------------------
  643. -- testing memory limits
  644. -------------------------------------------------------------------------
  645. assert(not pcall(T.newuserdata, 2^32-4))
  646. collectgarbage()
  647. T.totalmem(T.totalmem()+5000) -- set low memory limit (+5k)
  648. assert(not pcall(load"local a={}; for i=1,100000 do a[i]=i end"))
  649. T.totalmem(1000000000) -- restore high limit
  650. -- test memory errors; increase memory limit in small steps, so that
  651. -- we get memory errors in different parts of a given task, up to there
  652. -- is enough memory to complete the task without errors
  653. function testamem (s, f)
  654. collectgarbage(); collectgarbage()
  655. local M = T.totalmem()
  656. local oldM = M
  657. local a,b = nil
  658. while 1 do
  659. M = M+7 -- increase memory limit in small steps
  660. T.totalmem(M)
  661. a, b = pcall(f)
  662. T.totalmem(1000000000) -- restore high limit
  663. if a and b then break end -- stop when no more errors
  664. collectgarbage()
  665. if not a and not -- `real' error?
  666. (string.find(b, "memory") or string.find(b, "overflow")) then
  667. error(b, 0) -- propagate it
  668. end
  669. end
  670. print("\nlimit for " .. s .. ": " .. M-oldM)
  671. return b
  672. end
  673. -- testing memory errors when creating a new state
  674. b = testamem("state creation", T.newstate)
  675. T.closestate(b); -- close new state
  676. -- testing threads
  677. -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
  678. mt = T.testC("rawgeti R 1; return 1")
  679. assert(type(mt) == "thread" and coroutine.running() == mt)
  680. function expand (n,s)
  681. if n==0 then return "" end
  682. local e = string.rep("=", n)
  683. return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
  684. e, s, expand(n-1,s), e)
  685. end
  686. G=0; collectgarbage(); a =collectgarbage("count")
  687. load(expand(20,"G=G+1"))()
  688. assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
  689. testamem("thread creation", function ()
  690. return T.doonnewstack("x=1") == 0 -- try to create thread
  691. end)
  692. -- testing memory x compiler
  693. testamem("loadstring", function ()
  694. return load("x=1") -- try to do load a string
  695. end)
  696. local testprog = [[
  697. local function foo () return end
  698. local t = {"x"}
  699. a = "aaa"
  700. for i = 1, #t do a=a..t[i] end
  701. return true
  702. ]]
  703. -- testing memory x dofile
  704. _G.a = nil
  705. local t =os.tmpname()
  706. local f = assert(io.open(t, "w"))
  707. f:write(testprog)
  708. f:close()
  709. testamem("dofile", function ()
  710. local a = loadfile(t)
  711. return a and a()
  712. end)
  713. assert(os.remove(t))
  714. assert(_G.a == "aaax")
  715. -- other generic tests
  716. testamem("string creation", function ()
  717. local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
  718. return (a == 'ablo ablo')
  719. end)
  720. testamem("dump/undump", function ()
  721. local a = load(testprog)
  722. local b = a and string.dump(a)
  723. a = b and load(b)
  724. return a and a()
  725. end)
  726. local t = os.tmpname()
  727. testamem("file creation", function ()
  728. local f = assert(io.open(t, 'w'))
  729. assert (not io.open"nomenaoexistente")
  730. io.close(f);
  731. return not loadfile'nomenaoexistente'
  732. end)
  733. assert(os.remove(t))
  734. testamem("table creation", function ()
  735. local a, lim = {}, 10
  736. for i=1,lim do a[i] = i; a[i..'a'] = {} end
  737. return (type(a[lim..'a']) == 'table' and a[lim] == lim)
  738. end)
  739. testamem("constructors", function ()
  740. local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
  741. return (type(a) == 'table' and a.e == 5)
  742. end)
  743. local a = 1
  744. close = nil
  745. testamem("closure creation", function ()
  746. function close (b,c)
  747. return function (x) return a+b+c+x end
  748. end
  749. return (close(2,3)(4) == 10)
  750. end)
  751. testamem("coroutines", function ()
  752. local a = coroutine.wrap(function ()
  753. coroutine.yield(string.rep("a", 10))
  754. return {}
  755. end)
  756. assert(string.len(a()) == 10)
  757. return a()
  758. end)
  759. print'+'
  760. -- testing some auxlib functions
  761. local function gsub (a, b, c)
  762. a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
  763. assert(b == 5)
  764. return a
  765. end
  766. assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
  767. assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
  768. assert(gsub("", "alo", "//") == "")
  769. assert(gsub("...", ".", "/.") == "/././.")
  770. assert(gsub("...", "...", "") == "")
  771. -- testing luaL_newmetatable
  772. local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  773. assert(type(mt_xuxu) == "table" and res and top == 3)
  774. local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  775. assert(mt_xuxu == d and not res and top == 3)
  776. d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
  777. assert(mt_xuxu ~= d and res and top == 3)
  778. x = T.newuserdata(0);
  779. y = T.newuserdata(0);
  780. T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
  781. assert(getmetatable(x) == mt_xuxu)
  782. -- testing luaL_testudata
  783. -- correct metatable
  784. local res1, res2, top = T.testC([[testudata -1 xuxu
  785. testudata 2 xuxu
  786. gettop
  787. return 3]], x)
  788. assert(res1 and res2 and top == 4)
  789. -- wrong metatable
  790. res1, res2, top = T.testC([[testudata -1 xuxu1
  791. testudata 2 xuxu1
  792. gettop
  793. return 3]], x)
  794. assert(not res1 and not res2 and top == 4)
  795. -- non-existent type
  796. res1, res2, top = T.testC([[testudata -1 xuxu2
  797. testudata 2 xuxu2
  798. gettop
  799. return 3]], x)
  800. assert(not res1 and not res2 and top == 4)
  801. -- userdata has no metatable
  802. res1, res2, top = T.testC([[testudata -1 xuxu
  803. testudata 2 xuxu
  804. gettop
  805. return 3]], y)
  806. assert(not res1 and not res2 and top == 4)
  807. -- erase metatables
  808. do
  809. local r = debug.getregistry()
  810. assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
  811. r.xuxu = nil; r.xuxu1 = nil
  812. end
  813. print'OK'