2
0

api.lua 41 KB

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