api.lua 39 KB

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