api.lua 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. -- $Id: api.lua,v 1.147 2016/11/07 13:06:25 roberto Exp $
  2. -- See Copyright Notice in file all.lua
  3. if T==nil then
  4. (Message or print)('\n >>> testC not active: skipping API tests <<<\n')
  5. return
  6. end
  7. local debug = require "debug"
  8. local pack = table.pack
  9. function tcheck (t1, t2)
  10. assert(t1.n == (t2.n or #t2) + 1)
  11. for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
  12. end
  13. local function checkerr (msg, f, ...)
  14. local stat, err = pcall(f, ...)
  15. assert(not stat and string.find(err, msg))
  16. end
  17. print('testing C API')
  18. a = T.testC("pushvalue R; return 1")
  19. assert(a == debug.getregistry())
  20. -- absindex
  21. assert(T.testC("settop 10; absindex -1; return 1") == 10)
  22. assert(T.testC("settop 5; absindex -5; return 1") == 1)
  23. assert(T.testC("settop 10; absindex 1; return 1") == 1)
  24. assert(T.testC("settop 10; absindex R; return 1") < -10)
  25. -- testing alignment
  26. a = T.d2s(12458954321123.0)
  27. assert(a == string.pack("d", 12458954321123.0))
  28. assert(T.s2d(a) == 12458954321123.0)
  29. a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
  30. assert(a == 2 and b == 3 and not c)
  31. f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
  32. a,b,c = f()
  33. assert(a == 2 and b == 3 and not c)
  34. -- test that all trues are equal
  35. a,b,c = T.testC("pushbool 1; pushbool 2; pushbool 0; return 3")
  36. assert(a == b and a == true and c == false)
  37. a,b,c = T.testC"pushbool 0; pushbool 10; pushnil;\
  38. tobool -3; tobool -3; tobool -3; return 3"
  39. assert(a==false and b==true and c==false)
  40. a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
  41. assert(a == 40 and b == 5 and not c)
  42. t = pack(T.testC("settop 5; return *", 2, 3))
  43. tcheck(t, {n=4,2,3})
  44. t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
  45. assert(t.n == 10 and t[1] == nil and t[10] == nil)
  46. t = pack(T.testC("remove -2; return *", 2, 3, 4))
  47. tcheck(t, {n=2,2,4})
  48. t = pack(T.testC("insert -1; return *", 2, 3))
  49. tcheck(t, {n=2,2,3})
  50. t = pack(T.testC("insert 3; return *", 2, 3, 4, 5))
  51. tcheck(t, {n=4,2,5,3,4})
  52. t = pack(T.testC("replace 2; return *", 2, 3, 4, 5))
  53. tcheck(t, {n=3,5,3,4})
  54. t = pack(T.testC("replace -2; return *", 2, 3, 4, 5))
  55. tcheck(t, {n=3,2,3,5})
  56. t = pack(T.testC("remove 3; return *", 2, 3, 4, 5))
  57. tcheck(t, {n=3,2,4,5})
  58. t = pack(T.testC("copy 3 4; return *", 2, 3, 4, 5))
  59. tcheck(t, {n=4,2,3,3,5})
  60. t = pack(T.testC("copy -3 -1; return *", 2, 3, 4, 5))
  61. tcheck(t, {n=4,2,3,4,3})
  62. do -- testing 'rotate'
  63. local t = {10, 20, 30, 40, 50, 60}
  64. for i = -6, 6 do
  65. local s = string.format("rotate 2 %d; return 7", i)
  66. local t1 = pack(T.testC(s, 10, 20, 30, 40, 50, 60))
  67. tcheck(t1, t)
  68. table.insert(t, 1, table.remove(t))
  69. end
  70. t = pack(T.testC("rotate -2 1; return *", 10, 20, 30, 40))
  71. tcheck(t, {10, 20, 40, 30})
  72. t = pack(T.testC("rotate -2 -1; return *", 10, 20, 30, 40))
  73. tcheck(t, {10, 20, 40, 30})
  74. -- some corner cases
  75. t = pack(T.testC("rotate -1 0; return *", 10, 20, 30, 40))
  76. tcheck(t, {10, 20, 30, 40})
  77. t = pack(T.testC("rotate -1 1; return *", 10, 20, 30, 40))
  78. tcheck(t, {10, 20, 30, 40})
  79. t = pack(T.testC("rotate 5 -1; return *", 10, 20, 30, 40))
  80. tcheck(t, {10, 20, 30, 40})
  81. end
  82. -- testing non-function message handlers
  83. do
  84. local f = T.makeCfunc[[
  85. getglobal error
  86. pushstring bola
  87. pcall 1 1 1 # call 'error' with given handler
  88. pushstatus
  89. return 2 # return error message and status
  90. ]]
  91. local msg, st = f({}) -- invalid handler
  92. assert(st == "ERRERR" and string.find(msg, "error handling"))
  93. local msg, st = f(nil) -- invalid handler
  94. assert(st == "ERRERR" and string.find(msg, "error handling"))
  95. local a = setmetatable({}, {__call = function (_, x) return x:upper() end})
  96. local msg, st = f(a) -- callable handler
  97. assert(st == "ERRRUN" and msg == "BOLA")
  98. end
  99. t = pack(T.testC("insert 3; pushvalue 3; remove 3; pushvalue 2; remove 2; \
  100. insert 2; pushvalue 1; remove 1; insert 1; \
  101. insert -2; pushvalue -2; remove -3; return *",
  102. 2, 3, 4, 5, 10, 40, 90))
  103. tcheck(t, {n=7,2,3,4,5,10,40,90})
  104. t = pack(T.testC("concat 5; return *", "alo", 2, 3, "joao", 12))
  105. tcheck(t, {n=1,"alo23joao12"})
  106. -- testing MULTRET
  107. t = pack(T.testC("call 2,-1; return *",
  108. function (a,b) return 1,2,3,4,a,b end, "alo", "joao"))
  109. tcheck(t, {n=6,1,2,3,4,"alo", "joao"})
  110. do -- test returning more results than fit in the caller stack
  111. local a = {}
  112. for i=1,1000 do a[i] = true end; a[999] = 10
  113. local b = T.testC([[pcall 1 -1 0; pop 1; tostring -1; return 1]],
  114. table.unpack, a)
  115. assert(b == "10")
  116. end
  117. -- testing globals
  118. _G.a = 14; _G.b = "a31"
  119. local a = {T.testC[[
  120. getglobal a;
  121. getglobal b;
  122. getglobal b;
  123. setglobal a;
  124. return *
  125. ]]}
  126. assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31")
  127. -- testing arith
  128. assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
  129. assert(T.testC("pushnum 10; pushnum 20; arith -; return 1") == -10)
  130. assert(T.testC("pushnum 10; pushnum -20; arith *; return 1") == -200)
  131. assert(T.testC("pushnum 10; pushnum 3; arith ^; return 1") == 1000)
  132. assert(T.testC("pushnum 10; pushstring 20; arith /; return 1") == 0.5)
  133. assert(T.testC("pushstring 10; pushnum 20; arith -; return 1") == -10)
  134. assert(T.testC("pushstring 10; pushstring -20; arith *; return 1") == -200)
  135. assert(T.testC("pushstring 10; pushstring 3; arith ^; return 1") == 1000)
  136. assert(T.testC("arith /; return 1", 2, 0) == 10.0/0)
  137. a = T.testC("pushnum 10; pushint 3; arith \\; return 1")
  138. assert(a == 3.0 and math.type(a) == "float")
  139. a = T.testC("pushint 10; pushint 3; arith \\; return 1")
  140. assert(a == 3 and math.type(a) == "integer")
  141. a = assert(T.testC("pushint 10; pushint 3; arith +; return 1"))
  142. assert(a == 13 and math.type(a) == "integer")
  143. a = assert(T.testC("pushnum 10; pushint 3; arith +; return 1"))
  144. assert(a == 13 and math.type(a) == "float")
  145. a,b,c = T.testC([[pushnum 1;
  146. pushstring 10; arith _;
  147. pushstring 5; return 3]])
  148. assert(a == 1 and b == -10 and c == "5")
  149. mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
  150. __mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
  151. __unm = function (a) return setmetatable({a[1]* 2}, mt) end}
  152. a,b,c = setmetatable({4}, mt),
  153. setmetatable({8}, mt),
  154. setmetatable({-3}, mt)
  155. x,y,z = T.testC("arith +; return 2", 10, a, b)
  156. assert(x == 10 and y[1] == 12 and z == nil)
  157. assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
  158. assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
  159. 8 % (4 + (-3)*2))
  160. -- errors in arithmetic
  161. checkerr("divide by zero", T.testC, "arith \\", 10, 0)
  162. checkerr("%%0", T.testC, "arith %", 10, 0)
  163. -- testing lessthan and lessequal
  164. assert(T.testC("compare LT 2 5, return 1", 3, 2, 2, 4, 2, 2))
  165. assert(T.testC("compare LE 2 5, return 1", 3, 2, 2, 4, 2, 2))
  166. assert(not T.testC("compare LT 3 4, return 1", 3, 2, 2, 4, 2, 2))
  167. assert(T.testC("compare LE 3 4, return 1", 3, 2, 2, 4, 2, 2))
  168. assert(T.testC("compare LT 5 2, return 1", 4, 2, 2, 3, 2, 2))
  169. assert(not T.testC("compare LT 2 -3, return 1", "4", "2", "2", "3", "2", "2"))
  170. assert(not T.testC("compare LT -3 2, return 1", "3", "2", "2", "4", "2", "2"))
  171. -- non-valid indices produce false
  172. assert(not T.testC("compare LT 1 4, return 1"))
  173. assert(not T.testC("compare LE 9 1, return 1"))
  174. assert(not T.testC("compare EQ 9 9, return 1"))
  175. local b = {__lt = function (a,b) return a[1] < b[1] end}
  176. local a1,a3,a4 = setmetatable({1}, b),
  177. setmetatable({3}, b),
  178. setmetatable({4}, b)
  179. assert(T.testC("compare LT 2 5, return 1", a3, 2, 2, a4, 2, 2))
  180. assert(T.testC("compare LE 2 5, return 1", a3, 2, 2, a4, 2, 2))
  181. assert(T.testC("compare LT 5 -6, return 1", a4, 2, 2, a3, 2, 2))
  182. a,b = T.testC("compare LT 5 -6, return 2", a1, 2, 2, a3, 2, 20)
  183. assert(a == 20 and b == false)
  184. a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a3, 2, 20)
  185. assert(a == 20 and b == false)
  186. a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20)
  187. assert(a == 20 and b == true)
  188. -- testing length
  189. local t = setmetatable({x = 20}, {__len = function (t) return t.x end})
  190. a,b,c = T.testC([[
  191. len 2;
  192. Llen 2;
  193. objsize 2;
  194. return 3
  195. ]], t)
  196. assert(a == 20 and b == 20 and c == 0)
  197. t.x = "234"; t[1] = 20
  198. a,b,c = T.testC([[
  199. len 2;
  200. Llen 2;
  201. objsize 2;
  202. return 3
  203. ]], t)
  204. assert(a == "234" and b == 234 and c == 1)
  205. t.x = print; t[1] = 20
  206. a,c = T.testC([[
  207. len 2;
  208. objsize 2;
  209. return 2
  210. ]], t)
  211. assert(a == print and c == 1)
  212. -- testing __concat
  213. a = setmetatable({x="u"}, {__concat = function (a,b) return a.x..'.'..b.x end})
  214. x,y = T.testC([[
  215. pushnum 5
  216. pushvalue 2;
  217. pushvalue 2;
  218. concat 2;
  219. pushvalue -2;
  220. return 2;
  221. ]], a, a)
  222. assert(x == a..a and y == 5)
  223. -- concat with 0 elements
  224. assert(T.testC("concat 0; return 1") == "")
  225. -- concat with 1 element
  226. assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
  227. -- testing lua_is
  228. function B(x) return x and 1 or 0 end
  229. function count (x, n)
  230. n = n or 2
  231. local prog = [[
  232. isnumber %d;
  233. isstring %d;
  234. isfunction %d;
  235. iscfunction %d;
  236. istable %d;
  237. isuserdata %d;
  238. isnil %d;
  239. isnull %d;
  240. return 8
  241. ]]
  242. prog = string.format(prog, n, n, n, n, n, n, n, n)
  243. local a,b,c,d,e,f,g,h = T.testC(prog, x)
  244. return B(a)+B(b)+B(c)+B(d)+B(e)+B(f)+B(g)+(100*B(h))
  245. end
  246. assert(count(3) == 2)
  247. assert(count('alo') == 1)
  248. assert(count('32') == 2)
  249. assert(count({}) == 1)
  250. assert(count(print) == 2)
  251. assert(count(function () end) == 1)
  252. assert(count(nil) == 1)
  253. assert(count(io.stdin) == 1)
  254. assert(count(nil, 15) == 100)
  255. -- testing lua_to...
  256. function to (s, x, n)
  257. n = n or 2
  258. return T.testC(string.format("%s %d; return 1", s, n), x)
  259. end
  260. local hfunc = string.gmatch("", "") -- a "heavy C function" (with upvalues)
  261. assert(debug.getupvalue(hfunc, 1))
  262. assert(to("tostring", {}) == nil)
  263. assert(to("tostring", "alo") == "alo")
  264. assert(to("tostring", 12) == "12")
  265. assert(to("tostring", 12, 3) == nil)
  266. assert(to("objsize", {}) == 0)
  267. assert(to("objsize", {1,2,3}) == 3)
  268. assert(to("objsize", "alo\0\0a") == 6)
  269. assert(to("objsize", T.newuserdata(0)) == 0)
  270. assert(to("objsize", T.newuserdata(101)) == 101)
  271. assert(to("objsize", 124) == 0)
  272. assert(to("objsize", true) == 0)
  273. assert(to("tonumber", {}) == 0)
  274. assert(to("tonumber", "12") == 12)
  275. assert(to("tonumber", "s2") == 0)
  276. assert(to("tonumber", 1, 20) == 0)
  277. assert(to("topointer", 10) == 0)
  278. assert(to("topointer", true) == 0)
  279. assert(to("topointer", T.pushuserdata(20)) == 20)
  280. assert(to("topointer", io.read) ~= 0) -- light C function
  281. assert(to("topointer", hfunc) ~= 0) -- "heavy" C function
  282. assert(to("topointer", function () end) ~= 0) -- Lua function
  283. assert(to("topointer", io.stdin) ~= 0) -- full userdata
  284. assert(to("func2num", 20) == 0)
  285. assert(to("func2num", T.pushuserdata(10)) == 0)
  286. assert(to("func2num", io.read) ~= 0) -- light C function
  287. assert(to("func2num", hfunc) ~= 0) -- "heavy" C function (with upvalue)
  288. a = to("tocfunction", math.deg)
  289. assert(a(3) == math.deg(3) and a == math.deg)
  290. print("testing panic function")
  291. do
  292. -- trivial error
  293. assert(T.checkpanic("pushstring hi; error") == "hi")
  294. -- using the stack inside panic
  295. assert(T.checkpanic("pushstring hi; error;",
  296. [[checkstack 5 XX
  297. pushstring ' alo'
  298. pushstring ' mundo'
  299. concat 3]]) == "hi alo mundo")
  300. -- "argerror" without frames
  301. assert(T.checkpanic("loadstring 4") ==
  302. "bad argument #4 (string expected, got no value)")
  303. -- memory error
  304. T.totalmem(T.totalmem()+10000) -- set low memory limit (+10k)
  305. assert(T.checkpanic("newuserdata 20000") == "not enough memory")
  306. T.totalmem(0) -- restore high limit
  307. -- stack error
  308. if not _soft then
  309. local msg = T.checkpanic[[
  310. pushstring "function f() f() end"
  311. loadstring -1; call 0 0
  312. getglobal f; call 0 0
  313. ]]
  314. assert(string.find(msg, "stack overflow"))
  315. end
  316. end
  317. -- testing deep C stack
  318. if not _soft then
  319. print("testing stack overflow")
  320. collectgarbage("stop")
  321. checkerr("XXXX", T.testC, "checkstack 1000023 XXXX") -- too deep
  322. -- too deep (with no message)
  323. checkerr("^stack overflow$", T.testC, "checkstack 1000023 ''")
  324. local s = string.rep("pushnil;checkstack 1 XX;", 1000000)
  325. checkerr("overflow", T.testC, s)
  326. collectgarbage("restart")
  327. print'+'
  328. end
  329. local lim = _soft and 500 or 12000
  330. local prog = {"checkstack " .. (lim * 2 + 100) .. "msg", "newtable"}
  331. for i = 1,lim do
  332. prog[#prog + 1] = "pushnum " .. i
  333. prog[#prog + 1] = "pushnum " .. i * 10
  334. end
  335. prog[#prog + 1] = "rawgeti R 2" -- get global table in registry
  336. prog[#prog + 1] = "insert " .. -(2*lim + 2)
  337. for i = 1,lim do
  338. prog[#prog + 1] = "settable " .. -(2*(lim - i + 1) + 1)
  339. end
  340. prog[#prog + 1] = "return 2"
  341. prog = table.concat(prog, ";")
  342. local g, t = T.testC(prog)
  343. assert(g == _G)
  344. for i = 1,lim do assert(t[i] == i*10); t[i] = nil end
  345. assert(next(t) == nil)
  346. prog, g, t = nil
  347. -- testing errors
  348. a = T.testC([[
  349. loadstring 2; pcall 0 1 0;
  350. pushvalue 3; insert -2; pcall 1 1 0;
  351. pcall 0 0 0;
  352. return 1
  353. ]], "x=150", function (a) assert(a==nil); return 3 end)
  354. assert(type(a) == 'string' and x == 150)
  355. function check3(p, ...)
  356. local arg = {...}
  357. assert(#arg == 3)
  358. assert(string.find(arg[3], p))
  359. end
  360. check3(":1:", T.testC("loadstring 2; return *", "x="))
  361. check3("%.", T.testC("loadfile 2; return *", "."))
  362. check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
  363. -- test errors in non protected threads
  364. function checkerrnopro (code, msg)
  365. local th = coroutine.create(function () end) -- create new thread
  366. local stt, err = pcall(T.testC, th, code) -- run code there
  367. assert(not stt and string.find(err, msg))
  368. end
  369. if not _soft then
  370. checkerrnopro("pushnum 3; call 0 0", "attempt to call")
  371. print"testing stack overflow in unprotected thread"
  372. function f () f() end
  373. checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow")
  374. end
  375. print"+"
  376. -- testing table access
  377. do -- getp/setp
  378. local a = {}
  379. T.testC("rawsetp 2 1", a, 20)
  380. assert(a[T.pushuserdata(1)] == 20)
  381. assert(T.testC("rawgetp 2 1; return 1", a) == 20)
  382. end
  383. a = {x=0, y=12}
  384. x, y = T.testC("gettable 2; pushvalue 4; gettable 2; return 2",
  385. a, 3, "y", 4, "x")
  386. assert(x == 0 and y == 12)
  387. T.testC("settable -5", a, 3, 4, "x", 15)
  388. assert(a.x == 15)
  389. a[a] = print
  390. x = T.testC("gettable 2; return 1", a) -- table and key are the same object!
  391. assert(x == print)
  392. T.testC("settable 2", a, "x") -- table and key are the same object!
  393. assert(a[a] == "x")
  394. b = setmetatable({p = a}, {})
  395. getmetatable(b).__index = function (t, i) return t.p[i] end
  396. k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
  397. assert(x == 15 and k == 35)
  398. k = T.testC("getfield 2 y, return 1", b)
  399. assert(k == 12)
  400. getmetatable(b).__index = function (t, i) return a[i] end
  401. getmetatable(b).__newindex = function (t, i,v ) a[i] = v end
  402. y = T.testC("insert 2; gettable -5; return 1", 2, 3, 4, "y", b)
  403. assert(y == 12)
  404. k = T.testC("settable -5, return 1", b, 3, 4, "x", 16)
  405. assert(a.x == 16 and k == 4)
  406. a[b] = 'xuxu'
  407. y = T.testC("gettable 2, return 1", b)
  408. assert(y == 'xuxu')
  409. T.testC("settable 2", b, 19)
  410. assert(a[b] == 19)
  411. --
  412. do -- testing getfield/setfield with long keys
  413. local t = {_012345678901234567890123456789012345678901234567890123456789 = 32}
  414. local a = T.testC([[
  415. getfield 2 _012345678901234567890123456789012345678901234567890123456789
  416. return 1
  417. ]], t)
  418. assert(a == 32)
  419. local a = T.testC([[
  420. pushnum 33
  421. setglobal _012345678901234567890123456789012345678901234567890123456789
  422. ]])
  423. assert(_012345678901234567890123456789012345678901234567890123456789 == 33)
  424. _012345678901234567890123456789012345678901234567890123456789 = nil
  425. end
  426. -- testing next
  427. a = {}
  428. t = pack(T.testC("next; return *", a, nil))
  429. tcheck(t, {n=1,a})
  430. a = {a=3}
  431. t = pack(T.testC("next; return *", a, nil))
  432. tcheck(t, {n=3,a,'a',3})
  433. t = pack(T.testC("next; pop 1; next; return *", a, nil))
  434. tcheck(t, {n=1,a})
  435. -- testing upvalues
  436. do
  437. local A = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  438. t, b, c = A([[pushvalue U0; pushvalue U1; pushvalue U2; return 3]])
  439. assert(b == 10 and c == 20 and type(t) == 'table')
  440. a, b = A([[tostring U3; tonumber U4; return 2]])
  441. assert(a == nil and b == 0)
  442. A([[pushnum 100; pushnum 200; replace U2; replace U1]])
  443. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  444. assert(b == 100 and c == 200)
  445. A([[replace U2; replace U1]], {x=1}, {x=2})
  446. b, c = A([[pushvalue U1; pushvalue U2; return 2]])
  447. assert(b.x == 1 and c.x == 2)
  448. T.checkmemory()
  449. end
  450. -- testing absent upvalues from C-function pointers
  451. assert(T.testC[[isnull U1; return 1]] == true)
  452. assert(T.testC[[isnull U100; return 1]] == true)
  453. assert(T.testC[[pushvalue U1; return 1]] == nil)
  454. local f = T.testC[[ pushnum 10; pushnum 20; pushcclosure 2; return 1]]
  455. assert(T.upvalue(f, 1) == 10 and
  456. T.upvalue(f, 2) == 20 and
  457. T.upvalue(f, 3) == nil)
  458. T.upvalue(f, 2, "xuxu")
  459. assert(T.upvalue(f, 2) == "xuxu")
  460. -- large closures
  461. do
  462. local A = "checkstack 300 msg;" ..
  463. string.rep("pushnum 10;", 255) ..
  464. "pushcclosure 255; return 1"
  465. A = T.testC(A)
  466. for i=1,255 do
  467. assert(A(("pushvalue U%d; return 1"):format(i)) == 10)
  468. end
  469. assert(A("isnull U256; return 1"))
  470. assert(not A("isnil U256; return 1"))
  471. end
  472. -- testing get/setuservalue
  473. -- bug in 5.1.2
  474. checkerr("got number", debug.setuservalue, 3, {})
  475. checkerr("got nil", debug.setuservalue, nil, {})
  476. checkerr("got light userdata", debug.setuservalue, T.pushuserdata(1), {})
  477. local b = T.newuserdata(0)
  478. assert(debug.getuservalue(b) == nil)
  479. for _, v in pairs{true, false, 4.56, print, {}, b, "XYZ"} do
  480. assert(debug.setuservalue(b, v) == b)
  481. assert(debug.getuservalue(b) == v)
  482. end
  483. assert(debug.getuservalue(4) == nil)
  484. debug.setuservalue(b, function () return 10 end)
  485. collectgarbage() -- function should not be collected
  486. assert(debug.getuservalue(b)() == 10)
  487. debug.setuservalue(b, 134)
  488. collectgarbage() -- number should not be a problem for collector
  489. assert(debug.getuservalue(b) == 134)
  490. -- test barrier for uservalues
  491. T.gcstate("atomic")
  492. assert(T.gccolor(b) == "black")
  493. debug.setuservalue(b, {x = 100})
  494. T.gcstate("pause") -- complete collection
  495. assert(debug.getuservalue(b).x == 100) -- uvalue should be there
  496. -- long chain of userdata
  497. for i = 1, 1000 do
  498. local bb = T.newuserdata(0)
  499. debug.setuservalue(bb, b)
  500. b = bb
  501. end
  502. collectgarbage() -- nothing should not be collected
  503. for i = 1, 1000 do
  504. b = debug.getuservalue(b)
  505. end
  506. assert(debug.getuservalue(b).x == 100)
  507. b = nil
  508. -- testing locks (refs)
  509. -- reuse of references
  510. local i = T.ref{}
  511. T.unref(i)
  512. assert(T.ref{} == i)
  513. Arr = {}
  514. Lim = 100
  515. for i=1,Lim do -- lock many objects
  516. Arr[i] = T.ref({})
  517. end
  518. assert(T.ref(nil) == -1 and T.getref(-1) == nil)
  519. T.unref(-1); T.unref(-1)
  520. for i=1,Lim do -- unlock all them
  521. T.unref(Arr[i])
  522. end
  523. function printlocks ()
  524. local f = T.makeCfunc("gettable R; return 1")
  525. local n = f("n")
  526. print("n", n)
  527. for i=0,n do
  528. print(i, f(i))
  529. end
  530. end
  531. for i=1,Lim do -- lock many objects
  532. Arr[i] = T.ref({})
  533. end
  534. for i=1,Lim,2 do -- unlock half of them
  535. T.unref(Arr[i])
  536. end
  537. assert(type(T.getref(Arr[2])) == 'table')
  538. assert(T.getref(-1) == nil)
  539. a = T.ref({})
  540. collectgarbage()
  541. assert(type(T.getref(a)) == 'table')
  542. -- colect in cl the `val' of all collected userdata
  543. tt = {}
  544. cl = {n=0}
  545. A = nil; B = nil
  546. local F
  547. F = function (x)
  548. local udval = T.udataval(x)
  549. table.insert(cl, udval)
  550. local d = T.newuserdata(100) -- cria lixo
  551. d = nil
  552. assert(debug.getmetatable(x).__gc == F)
  553. assert(load("table.insert({}, {})"))() -- cria mais lixo
  554. collectgarbage() -- forca coleta de lixo durante coleta!
  555. assert(debug.getmetatable(x).__gc == F) -- coleta anterior nao melou isso?
  556. local dummy = {} -- cria lixo durante coleta
  557. if A ~= nil then
  558. assert(type(A) == "userdata")
  559. assert(T.udataval(A) == B)
  560. debug.getmetatable(A) -- just acess it
  561. end
  562. A = x -- ressucita userdata
  563. B = udval
  564. return 1,2,3
  565. end
  566. tt.__gc = F
  567. -- test whether udate collection frees memory in the right time
  568. do
  569. collectgarbage();
  570. collectgarbage();
  571. local x = collectgarbage("count");
  572. local a = T.newuserdata(5001)
  573. assert(T.testC("objsize 2; return 1", a) == 5001)
  574. assert(collectgarbage("count") >= x+4)
  575. a = nil
  576. collectgarbage();
  577. assert(collectgarbage("count") <= x+1)
  578. -- udata without finalizer
  579. x = collectgarbage("count")
  580. collectgarbage("stop")
  581. for i=1,1000 do T.newuserdata(0) end
  582. assert(collectgarbage("count") > x+10)
  583. collectgarbage()
  584. assert(collectgarbage("count") <= x+1)
  585. -- udata with finalizer
  586. collectgarbage()
  587. x = collectgarbage("count")
  588. collectgarbage("stop")
  589. a = {__gc = function () end}
  590. for i=1,1000 do debug.setmetatable(T.newuserdata(0), a) end
  591. assert(collectgarbage("count") >= x+10)
  592. collectgarbage() -- this collection only calls TM, without freeing memory
  593. assert(collectgarbage("count") >= x+10)
  594. collectgarbage() -- now frees memory
  595. assert(collectgarbage("count") <= x+1)
  596. collectgarbage("restart")
  597. end
  598. collectgarbage("stop")
  599. -- create 3 userdatas with tag `tt'
  600. a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
  601. b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
  602. c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)
  603. -- create userdata without meta table
  604. x = T.newuserdata(4)
  605. y = T.newuserdata(0)
  606. checkerr("FILE%* expected, got userdata", io.input, a)
  607. checkerr("FILE%* expected, got userdata", io.input, x)
  608. assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
  609. d=T.ref(a);
  610. e=T.ref(b);
  611. f=T.ref(c);
  612. t = {T.getref(d), T.getref(e), T.getref(f)}
  613. assert(t[1] == a and t[2] == b and t[3] == c)
  614. t=nil; a=nil; c=nil;
  615. T.unref(e); T.unref(f)
  616. collectgarbage()
  617. -- check that unref objects have been collected
  618. assert(#cl == 1 and cl[1] == nc)
  619. x = T.getref(d)
  620. assert(type(x) == 'userdata' and debug.getmetatable(x) == tt)
  621. x =nil
  622. tt.b = b -- create cycle
  623. tt=nil -- frees tt for GC
  624. A = nil
  625. b = nil
  626. T.unref(d);
  627. n5 = T.newuserdata(0)
  628. debug.setmetatable(n5, {__gc=F})
  629. n5 = T.udataval(n5)
  630. collectgarbage()
  631. assert(#cl == 4)
  632. -- check order of collection
  633. assert(cl[2] == n5 and cl[3] == nb and cl[4] == na)
  634. collectgarbage"restart"
  635. a, na = {}, {}
  636. for i=30,1,-1 do
  637. a[i] = T.newuserdata(0)
  638. debug.setmetatable(a[i], {__gc=F})
  639. na[i] = T.udataval(a[i])
  640. end
  641. cl = {}
  642. a = nil; collectgarbage()
  643. assert(#cl == 30)
  644. for i=1,30 do assert(cl[i] == na[i]) end
  645. na = nil
  646. for i=2,Lim,2 do -- unlock the other half
  647. T.unref(Arr[i])
  648. end
  649. x = T.newuserdata(41); debug.setmetatable(x, {__gc=F})
  650. assert(T.testC("objsize 2; return 1", x) == 41)
  651. cl = {}
  652. a = {[x] = 1}
  653. x = T.udataval(x)
  654. collectgarbage()
  655. -- old `x' cannot be collected (`a' still uses it)
  656. assert(#cl == 0)
  657. for n in pairs(a) do a[n] = nil end
  658. collectgarbage()
  659. assert(#cl == 1 and cl[1] == x) -- old `x' must be collected
  660. -- testing lua_equal
  661. assert(T.testC("compare EQ 2 4; return 1", print, 1, print, 20))
  662. assert(T.testC("compare EQ 3 2; return 1", 'alo', "alo"))
  663. assert(T.testC("compare EQ 2 3; return 1", nil, nil))
  664. assert(not T.testC("compare EQ 2 3; return 1", {}, {}))
  665. assert(not T.testC("compare EQ 2 3; return 1"))
  666. assert(not T.testC("compare EQ 2 3; return 1", 3))
  667. -- testing lua_equal with fallbacks
  668. do
  669. local map = {}
  670. local t = {__eq = function (a,b) return map[a] == map[b] end}
  671. local function f(x)
  672. local u = T.newuserdata(0)
  673. debug.setmetatable(u, t)
  674. map[u] = x
  675. return u
  676. end
  677. assert(f(10) == f(10))
  678. assert(f(10) ~= f(11))
  679. assert(T.testC("compare EQ 2 3; return 1", f(10), f(10)))
  680. assert(not T.testC("compare EQ 2 3; return 1", f(10), f(20)))
  681. t.__eq = nil
  682. assert(f(10) ~= f(10))
  683. end
  684. print'+'
  685. -- testing changing hooks during hooks
  686. _G.t = {}
  687. T.sethook([[
  688. # set a line hook after 3 count hooks
  689. sethook 4 0 '
  690. getglobal t;
  691. pushvalue -3; append -2
  692. pushvalue -2; append -2
  693. ']], "c", 3)
  694. local a = 1 -- counting
  695. a = 1 -- counting
  696. a = 1 -- count hook (set line hook)
  697. a = 1 -- line hook
  698. a = 1 -- line hook
  699. debug.sethook()
  700. t = _G.t
  701. assert(t[1] == "line")
  702. line = t[2]
  703. assert(t[3] == "line" and t[4] == line + 1)
  704. assert(t[5] == "line" and t[6] == line + 2)
  705. assert(t[7] == nil)
  706. -------------------------------------------------------------------------
  707. do -- testing errors during GC
  708. local a = {}
  709. for i=1,20 do
  710. a[i] = T.newuserdata(i) -- creates several udata
  711. end
  712. for i=1,20,2 do -- mark half of them to raise errors during GC
  713. debug.setmetatable(a[i], {__gc = function (x) error("error inside gc") end})
  714. end
  715. for i=2,20,2 do -- mark the other half to count and to create more garbage
  716. debug.setmetatable(a[i], {__gc = function (x) load("A=A+1")() end})
  717. end
  718. _G.A = 0
  719. a = 0
  720. while 1 do
  721. local stat, msg = pcall(collectgarbage)
  722. if stat then
  723. break -- stop when no more errors
  724. else
  725. a = a + 1
  726. assert(string.find(msg, "__gc"))
  727. end
  728. end
  729. assert(a == 10) -- number of errors
  730. assert(A == 10) -- number of normal collections
  731. end
  732. -------------------------------------------------------------------------
  733. -- test for userdata vals
  734. do
  735. local a = {}; local lim = 30
  736. for i=0,lim do a[i] = T.pushuserdata(i) end
  737. for i=0,lim do assert(T.udataval(a[i]) == i) end
  738. for i=0,lim do assert(T.pushuserdata(i) == a[i]) end
  739. for i=0,lim do a[a[i]] = i end
  740. for i=0,lim do a[T.pushuserdata(i)] = i end
  741. assert(type(tostring(a[1])) == "string")
  742. end
  743. -------------------------------------------------------------------------
  744. -- testing multiple states
  745. T.closestate(T.newstate());
  746. L1 = T.newstate()
  747. assert(L1)
  748. assert(T.doremote(L1, "X='a'; return 'a'") == 'a')
  749. assert(#pack(T.doremote(L1, "function f () return 'alo', 3 end; f()")) == 0)
  750. a, b = T.doremote(L1, "return f()")
  751. assert(a == 'alo' and b == '3')
  752. T.doremote(L1, "_ERRORMESSAGE = nil")
  753. -- error: `sin' is not defined
  754. a, _, b = T.doremote(L1, "return sin(1)")
  755. assert(a == nil and b == 2) -- 2 == run-time error
  756. -- error: syntax error
  757. a, b, c = T.doremote(L1, "return a+")
  758. assert(a == nil and c == 3 and type(b) == "string") -- 3 == syntax error
  759. T.loadlib(L1)
  760. a, b, c = T.doremote(L1, [[
  761. string = require'string'
  762. a = require'_G'; assert(a == _G and require("_G") == a)
  763. io = require'io'; assert(type(io.read) == "function")
  764. assert(require("io") == io)
  765. a = require'table'; assert(type(a.insert) == "function")
  766. a = require'debug'; assert(type(a.getlocal) == "function")
  767. a = require'math'; assert(type(a.sin) == "function")
  768. return string.sub('okinama', 1, 2)
  769. ]])
  770. assert(a == "ok")
  771. T.closestate(L1);
  772. L1 = T.newstate()
  773. T.loadlib(L1)
  774. T.doremote(L1, "a = {}")
  775. T.testC(L1, [[getglobal "a"; pushstring "x"; pushint 1;
  776. settable -3]])
  777. assert(T.doremote(L1, "return a.x") == "1")
  778. T.closestate(L1)
  779. L1 = nil
  780. print('+')
  781. -------------------------------------------------------------------------
  782. -- testing memory limits
  783. -------------------------------------------------------------------------
  784. checkerr("block too big", T.newuserdata, math.maxinteger)
  785. collectgarbage()
  786. T.totalmem(T.totalmem()+5000) -- set low memory limit (+5k)
  787. checkerr("not enough memory", load"local a={}; for i=1,100000 do a[i]=i end")
  788. T.totalmem(0) -- restore high limit
  789. -- test memory errors; increase memory limit in small steps, so that
  790. -- we get memory errors in different parts of a given task, up to there
  791. -- is enough memory to complete the task without errors
  792. function testamem (s, f)
  793. collectgarbage(); collectgarbage()
  794. local M = T.totalmem()
  795. local oldM = M
  796. local a,b = nil
  797. while 1 do
  798. M = M+7 -- increase memory limit in small steps
  799. T.totalmem(M)
  800. a, b = pcall(f)
  801. T.totalmem(0) -- restore high limit
  802. if a and b then break end -- stop when no more errors
  803. collectgarbage()
  804. if not a and not -- `real' error?
  805. (string.find(b, "memory") or string.find(b, "overflow")) then
  806. error(b, 0) -- propagate it
  807. end
  808. end
  809. print("\nlimit for " .. s .. ": " .. M-oldM)
  810. return b
  811. end
  812. -- testing memory errors when creating a new state
  813. b = testamem("state creation", T.newstate)
  814. T.closestate(b); -- close new state
  815. -- testing threads
  816. -- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
  817. mt = T.testC("rawgeti R 1; return 1")
  818. assert(type(mt) == "thread" and coroutine.running() == mt)
  819. function expand (n,s)
  820. if n==0 then return "" end
  821. local e = string.rep("=", n)
  822. return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
  823. e, s, expand(n-1,s), e)
  824. end
  825. G=0; collectgarbage(); a =collectgarbage("count")
  826. load(expand(20,"G=G+1"))()
  827. assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
  828. testamem("thread creation", function ()
  829. return T.doonnewstack("x=1") == 0 -- try to create thread
  830. end)
  831. -- testing memory x compiler
  832. testamem("loadstring", function ()
  833. return load("x=1") -- try to do load a string
  834. end)
  835. local testprog = [[
  836. local function foo () return end
  837. local t = {"x"}
  838. a = "aaa"
  839. for i = 1, #t do a=a..t[i] end
  840. return true
  841. ]]
  842. -- testing memory x dofile
  843. _G.a = nil
  844. local t =os.tmpname()
  845. local f = assert(io.open(t, "w"))
  846. f:write(testprog)
  847. f:close()
  848. testamem("dofile", function ()
  849. local a = loadfile(t)
  850. return a and a()
  851. end)
  852. assert(os.remove(t))
  853. assert(_G.a == "aaax")
  854. -- other generic tests
  855. testamem("string creation", function ()
  856. local a, b = string.gsub("alo alo", "(a)", function (x) return x..'b' end)
  857. return (a == 'ablo ablo')
  858. end)
  859. testamem("dump/undump", function ()
  860. local a = load(testprog)
  861. local b = a and string.dump(a)
  862. a = b and load(b)
  863. return a and a()
  864. end)
  865. local t = os.tmpname()
  866. testamem("file creation", function ()
  867. local f = assert(io.open(t, 'w'))
  868. assert (not io.open"nomenaoexistente")
  869. io.close(f);
  870. return not loadfile'nomenaoexistente'
  871. end)
  872. assert(os.remove(t))
  873. testamem("table creation", function ()
  874. local a, lim = {}, 10
  875. for i=1,lim do a[i] = i; a[i..'a'] = {} end
  876. return (type(a[lim..'a']) == 'table' and a[lim] == lim)
  877. end)
  878. testamem("constructors", function ()
  879. local a = {10, 20, 30, 40, 50; a=1, b=2, c=3, d=4, e=5}
  880. return (type(a) == 'table' and a.e == 5)
  881. end)
  882. local a = 1
  883. close = nil
  884. testamem("closure creation", function ()
  885. function close (b,c)
  886. return function (x) return a+b+c+x end
  887. end
  888. return (close(2,3)(4) == 10)
  889. end)
  890. testamem("coroutines", function ()
  891. local a = coroutine.wrap(function ()
  892. coroutine.yield(string.rep("a", 10))
  893. return {}
  894. end)
  895. assert(string.len(a()) == 10)
  896. return a()
  897. end)
  898. do -- auxiliary buffer
  899. local lim = 100
  900. local a = {}; for i = 1, lim do a[i] = "01234567890123456789" end
  901. testamem("auxiliary buffer", function ()
  902. return (#table.concat(a, ",") == 20*lim + lim - 1)
  903. end)
  904. end
  905. print'+'
  906. -- testing some auxlib functions
  907. local function gsub (a, b, c)
  908. a, b = T.testC("gsub 2 3 4; gettop; return 2", a, b, c)
  909. assert(b == 5)
  910. return a
  911. end
  912. assert(gsub("alo.alo.uhuh.", ".", "//") == "alo//alo//uhuh//")
  913. assert(gsub("alo.alo.uhuh.", "alo", "//") == "//.//.uhuh.")
  914. assert(gsub("", "alo", "//") == "")
  915. assert(gsub("...", ".", "/.") == "/././.")
  916. assert(gsub("...", "...", "") == "")
  917. -- testing luaL_newmetatable
  918. local mt_xuxu, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  919. assert(type(mt_xuxu) == "table" and res and top == 3)
  920. local d, res, top = T.testC("newmetatable xuxu; gettop; return 3")
  921. assert(mt_xuxu == d and not res and top == 3)
  922. d, res, top = T.testC("newmetatable xuxu1; gettop; return 3")
  923. assert(mt_xuxu ~= d and res and top == 3)
  924. x = T.newuserdata(0);
  925. y = T.newuserdata(0);
  926. T.testC("pushstring xuxu; gettable R; setmetatable 2", x)
  927. assert(getmetatable(x) == mt_xuxu)
  928. -- testing luaL_testudata
  929. -- correct metatable
  930. local res1, res2, top = T.testC([[testudata -1 xuxu
  931. testudata 2 xuxu
  932. gettop
  933. return 3]], x)
  934. assert(res1 and res2 and top == 4)
  935. -- wrong metatable
  936. res1, res2, top = T.testC([[testudata -1 xuxu1
  937. testudata 2 xuxu1
  938. gettop
  939. return 3]], x)
  940. assert(not res1 and not res2 and top == 4)
  941. -- non-existent type
  942. res1, res2, top = T.testC([[testudata -1 xuxu2
  943. testudata 2 xuxu2
  944. gettop
  945. return 3]], x)
  946. assert(not res1 and not res2 and top == 4)
  947. -- userdata has no metatable
  948. res1, res2, top = T.testC([[testudata -1 xuxu
  949. testudata 2 xuxu
  950. gettop
  951. return 3]], y)
  952. assert(not res1 and not res2 and top == 4)
  953. -- erase metatables
  954. do
  955. local r = debug.getregistry()
  956. assert(r.xuxu == mt_xuxu and r.xuxu1 == d)
  957. r.xuxu = nil; r.xuxu1 = nil
  958. end
  959. print'OK'