api.lua 37 KB

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