api.lua 37 KB

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