api.lua 39 KB

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