api.lua 45 KB

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