api.lua 39 KB

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