api.lua 34 KB

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