api.lua 42 KB

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