api.lua 39 KB

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