api.lua 40 KB

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