2
0

api.lua 43 KB

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