api.lua 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 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 to-be-closed variables
  800. -------------------------------------------------------------------------
  801. print"testing to-be-closed variables"
  802. do
  803. local openresource = {}
  804. local function newresource ()
  805. local x = setmetatable({10}, {__close = function(y)
  806. assert(openresource[#openresource] == y)
  807. openresource[#openresource] = nil
  808. y[1] = y[1] + 1
  809. end})
  810. openresource[#openresource + 1] = x
  811. return x
  812. end
  813. local a, b = T.testC([[
  814. call 0 1 # create resource
  815. pushnil
  816. toclose -2 # mark call result to be closed
  817. toclose -1 # mark nil to be closed (will be ignored)
  818. return 2
  819. ]], newresource)
  820. assert(a[1] == 11 and b == nil)
  821. assert(#openresource == 0) -- was closed
  822. -- repeat the test, but calling function in a 'multret' context
  823. local a = {T.testC([[
  824. call 0 1 # create resource
  825. toclose 2 # mark it to be closed
  826. return 2
  827. ]], newresource)}
  828. assert(type(a[1]) == "string" and a[2][1] == 11)
  829. assert(#openresource == 0) -- was closed
  830. -- error
  831. local a, b = pcall(T.makeCfunc[[
  832. call 0 1 # create resource
  833. toclose -1 # mark it to be closed
  834. error # resource is the error object
  835. ]], newresource)
  836. assert(a == false and b[1] == 11)
  837. assert(#openresource == 0) -- was closed
  838. local function check (n)
  839. assert(#openresource == n)
  840. end
  841. -- closing resources with 'settop'
  842. local a = T.testC([[
  843. pushvalue 2
  844. call 0 1 # create resource
  845. toclose -1 # mark it to be closed
  846. pushvalue 2
  847. call 0 1 # create another resource
  848. toclose -1 # mark it to be closed
  849. pushvalue 3
  850. pushint 2 # there should be two open resources
  851. call 1 0
  852. pop 1 # pop second resource from the stack
  853. pushvalue 3
  854. pushint 1 # there should be one open resource
  855. call 1 0
  856. pop 1 # pop second resource from the stack
  857. pushint *
  858. return 1 # return stack size
  859. ]], newresource, check)
  860. assert(a == 3) -- no extra items left in the stack
  861. -- non-closable value
  862. local a, b = pcall(T.makeCfunc[[
  863. pushint 32
  864. toclose -1
  865. ]])
  866. assert(not a and string.find(b, "(C temporary)"))
  867. end
  868. -------------------------------------------------------------------------
  869. -- testing memory limits
  870. -------------------------------------------------------------------------
  871. print("memory-allocation errors")
  872. checkerr("block too big", T.newuserdata, math.maxinteger)
  873. collectgarbage()
  874. local f = load"local a={}; for i=1,100000 do a[i]=i end"
  875. T.alloccount(10)
  876. checkerr("not enough memory", f)
  877. T.alloccount() -- remove limit
  878. -- test memory errors; increase limit for number of allocations one
  879. -- by one, so that we get memory errors in all allocations of a given
  880. -- task, until there is enough allocations to complete the task without
  881. -- errors.
  882. function testamem (s, f)
  883. collectgarbage(); collectgarbage()
  884. local M = 0
  885. local a,b = nil
  886. while true do
  887. T.alloccount(M)
  888. a, b = pcall(f)
  889. T.alloccount() -- remove limit
  890. if a and b then break end -- stop when no more errors
  891. if not a and not -- `real' error?
  892. (string.find(b, "memory") or string.find(b, "overflow")) then
  893. error(b, 0) -- propagate it
  894. end
  895. M = M + 1 -- increase allocation limit
  896. end
  897. print(string.format("limit for %s: %d allocations", s, M))
  898. return b
  899. end
  900. -- doing nothing
  901. b = testamem("doing nothing", function () return 10 end)
  902. assert(b == 10)
  903. -- testing memory errors when creating a new state
  904. b = testamem("state creation", T.newstate)
  905. T.closestate(b); -- close new state
  906. testamem("empty-table creation", function ()
  907. return {}
  908. end)
  909. testamem("string creation", function ()
  910. return "XXX" .. "YYY"
  911. end)
  912. testamem("coroutine creation", function()
  913. return coroutine.create(print)
  914. end)
  915. -- testing to-be-closed variables
  916. testamem("to-be-closed variables", function()
  917. local flag
  918. do
  919. local *toclose x = function () flag = true end
  920. flag = false
  921. local x = {}
  922. end
  923. return flag
  924. end)
  925. -- testing threads
  926. -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
  927. mt = T.testC("rawgeti R 1; return 1")
  928. assert(type(mt) == "thread" and coroutine.running() == mt)
  929. function expand (n,s)
  930. if n==0 then return "" end
  931. local e = string.rep("=", n)
  932. return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
  933. e, s, expand(n-1,s), e)
  934. end
  935. G=0; collectgarbage(); a =collectgarbage("count")
  936. load(expand(20,"G=G+1"))()
  937. assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
  938. testamem("running code on new thread", function ()
  939. return T.doonnewstack("x=1") == 0 -- try to create thread
  940. end)
  941. -- testing memory x compiler
  942. testamem("loadstring", function ()
  943. return load("x=1") -- try to do load a string
  944. end)
  945. local testprog = [[
  946. local function foo () return end
  947. local t = {"x"}
  948. a = "aaa"
  949. for i = 1, #t do a=a..t[i] end
  950. return true
  951. ]]
  952. -- testing memory x dofile
  953. _G.a = nil
  954. local t =os.tmpname()
  955. local f = assert(io.open(t, "w"))
  956. f:write(testprog)
  957. f:close()
  958. testamem("dofile", function ()
  959. local a = loadfile(t)
  960. return a and a()
  961. end)
  962. assert(os.remove(t))
  963. assert(_G.a == "aaax")
  964. -- other generic tests
  965. testamem("gsub", function ()
  966. local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
  967. return (a == 'ablo ablo')
  968. end)
  969. testamem("dump/undump", function ()
  970. local a = load(testprog)
  971. local b = a and string.dump(a)
  972. a = b and load(b)
  973. return a and a()
  974. end)
  975. local t = os.tmpname()
  976. testamem("file creation", function ()
  977. local f = assert(io.open(t, 'w'))
  978. assert (not io.open"nomenaoexistente")
  979. io.close(f);
  980. return not loadfile'nomenaoexistente'
  981. end)
  982. assert(os.remove(t))
  983. testamem("table creation", function ()
  984. local a, lim = {}, 10
  985. for i=1,lim do a[i] = i; a[i..'a'] = {} end
  986. return (type(a[lim..'a']) == 'table' and a[lim] == lim)
  987. end)
  988. testamem("constructors", function ()
  989. local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
  990. return (type(a) == 'table' and a.e == 5)
  991. end)
  992. local a = 1
  993. close = nil
  994. testamem("closure creation", function ()
  995. function close (b)
  996. return function (x) return b + x end
  997. end
  998. return (close(2)(4) == 6)
  999. end)
  1000. testamem("using coroutines", function ()
  1001. local a = coroutine.wrap(function ()
  1002. coroutine.yield(string.rep("a", 10))
  1003. return {}
  1004. end)
  1005. assert(string.len(a()) == 10)
  1006. return a()
  1007. end)
  1008. do -- auxiliary buffer
  1009. local lim = 100
  1010. local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
  1011. testamem("auxiliary buffer", function ()
  1012. return (#table.concat(a, ",") == 20*lim + lim - 1)
  1013. end)
  1014. end
  1015. testamem("growing stack", function ()
  1016. local function foo (n)
  1017. if n == 0 then return 1 else return 1 + foo(n - 1) end
  1018. end
  1019. return foo(100)
  1020. end)
  1021. do -- testing failing in 'lua_checkstack'
  1022. local res = T.testC([[rawcheckstack 500000; return 1]])
  1023. assert(res == false)
  1024. local L = T.newstate()
  1025. T.alloccount(0) -- will be unable to reallocate the stack
  1026. res = T.testC(L, [[rawcheckstack 5000; return 1]])
  1027. T.alloccount()
  1028. T.closestate(L)
  1029. assert(res == false)
  1030. end
  1031. do -- closing state with no extra memory
  1032. local L = T.newstate()
  1033. T.alloccount(0)
  1034. T.closestate(L)
  1035. T.alloccount()
  1036. end
  1037. do -- garbage collection with no extra memory
  1038. local L = T.newstate()
  1039. T.loadlib(L)
  1040. local res = (T.doremote(L, [[
  1041. _ENV = require"_G"
  1042. local T = require"T"
  1043. local a = {}
  1044. for i = 1, 1000 do a[i] = 'i' .. i end -- grow string table
  1045. local stsize, stuse = T.querystr()
  1046. assert(stuse > 1000)
  1047. local function foo (n)
  1048. if n > 0 then foo(n - 1) end
  1049. end
  1050. foo(180) -- grow stack
  1051. local _, stksize = T.stacklevel()
  1052. assert(stksize > 180)
  1053. a = nil
  1054. T.alloccount(0)
  1055. collectgarbage()
  1056. T.alloccount()
  1057. -- stack and string table could not be reallocated,
  1058. -- so they kept their sizes (without errors)
  1059. assert(select(2, T.stacklevel()) == stksize)
  1060. assert(T.querystr() == stsize)
  1061. return 'ok'
  1062. ]]))
  1063. assert(res == 'ok')
  1064. T.closestate(L)
  1065. end
  1066. print'+'
  1067. -- testing some auxlib functions
  1068. local function gsub (a, b, c)
  1069. a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
  1070. assert(b == 5)
  1071. return a
  1072. end
  1073. assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
  1074. assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
  1075. assert(gsub("", "alo", "//") == "")
  1076. assert(gsub("...", ".", "/.") == "/././.")
  1077. assert(gsub("...", "...", "") == "")
  1078. -- testing luaL_newmetatable
  1079. local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  1080. assert(type(mt_xuxu) == "table" and res and top == 3)
  1081. local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  1082. assert(mt_xuxu == d and not res and top == 3)
  1083. d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
  1084. assert(mt_xuxu ~= d and res and top == 3)
  1085. x = T.newuserdata(0);
  1086. y = T.newuserdata(0);
  1087. T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
  1088. assert(getmetatable(x) == mt_xuxu)
  1089. -- testing luaL_testudata
  1090. -- correct metatable
  1091. local res1, res2, top = T.testC([[testudata -1 xuxu
  1092. testudata 2 xuxu
  1093. gettop
  1094. return 3]], x)
  1095. assert(res1 and res2 and top == 4)
  1096. -- wrong metatable
  1097. res1, res2, top = T.testC([[testudata -1 xuxu1
  1098. testudata 2 xuxu1
  1099. gettop
  1100. return 3]], x)
  1101. assert(not res1 and not res2 and top == 4)
  1102. -- non-existent type
  1103. res1, res2, top = T.testC([[testudata -1 xuxu2
  1104. testudata 2 xuxu2
  1105. gettop
  1106. return 3]], x)
  1107. assert(not res1 and not res2 and top == 4)
  1108. -- userdata has no metatable
  1109. res1, res2, top = T.testC([[testudata -1 xuxu
  1110. testudata 2 xuxu
  1111. gettop
  1112. return 3]], y)
  1113. assert(not res1 and not res2 and top == 4)
  1114. -- erase metatables
  1115. do
  1116. local r = debug.getregistry()
  1117. assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
  1118. r.xuxu = nil; r.xuxu1 = nil
  1119. end
  1120. print'OK'