calls.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. -- $Id: testes/calls.lua $
  2. -- See Copyright Notice in file all.lua
  3. print("testing functions and calls")
  4. local debug = require "debug"
  5. -- get the opportunity to test 'type' too ;)
  6. assert(type(1<2) == 'boolean')
  7. assert(type(true) == 'boolean' and type(false) == 'boolean')
  8. assert(type(nil) == 'nil'
  9. and type(-3) == 'number'
  10. and type'x' == 'string'
  11. and type{} == 'table'
  12. and type(type) == 'function')
  13. assert(type(assert) == type(print))
  14. local function f (x) return a:x (x) end
  15. assert(type(f) == 'function')
  16. assert(not pcall(type))
  17. -- testing local-function recursion
  18. fact = false
  19. do
  20. local res = 1
  21. local function fact (n)
  22. if n==0 then return res
  23. else return n*fact(n-1)
  24. end
  25. end
  26. assert(fact(5) == 120)
  27. end
  28. assert(fact == false)
  29. fact = nil
  30. -- testing declarations
  31. local a = {i = 10}
  32. local self = 20
  33. function a:x (x) return x+self.i end
  34. function a.y (x) return x+self end
  35. assert(a:x(1)+10 == a.y(1))
  36. a.t = {i=-100}
  37. a["t"].x = function (self, a,b) return self.i+a+b end
  38. assert(a.t:x(2,3) == -95)
  39. do
  40. local a = {x=0}
  41. function a:add (x) self.x, a.y = self.x+x, 20; return self end
  42. assert(a:add(10):add(20):add(30).x == 60 and a.y == 20)
  43. end
  44. local a = {b={c={}}}
  45. function a.b.c.f1 (x) return x+1 end
  46. function a.b.c:f2 (x,y) self[x] = y end
  47. assert(a.b.c.f1(4) == 5)
  48. a.b.c:f2('k', 12); assert(a.b.c.k == 12)
  49. print('+')
  50. t = nil -- 'declare' t
  51. function f(a,b,c) local d = 'a'; t={a,b,c,d} end
  52. f( -- this line change must be valid
  53. 1,2)
  54. assert(t[1] == 1 and t[2] == 2 and t[3] == nil and t[4] == 'a')
  55. f(1,2, -- this one too
  56. 3,4)
  57. assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
  58. t = nil -- delete 't'
  59. function fat(x)
  60. if x <= 1 then return 1
  61. else return x*load("return fat(" .. x-1 .. ")", "")()
  62. end
  63. end
  64. assert(load "load 'assert(fat(6)==720)' () ")()
  65. a = load('return fat(5), 3')
  66. local a,b = a()
  67. assert(a == 120 and b == 3)
  68. fat = nil
  69. print('+')
  70. local function err_on_n (n)
  71. if n==0 then error(); exit(1);
  72. else err_on_n (n-1); exit(1);
  73. end
  74. end
  75. do
  76. local function dummy (n)
  77. if n > 0 then
  78. assert(not pcall(err_on_n, n))
  79. dummy(n-1)
  80. end
  81. end
  82. dummy(10)
  83. end
  84. _G.deep = nil -- "declaration" (used by 'all.lua')
  85. function deep (n)
  86. if n>0 then deep(n-1) end
  87. end
  88. deep(10)
  89. deep(180)
  90. print"testing tail calls"
  91. function deep (n) if n>0 then return deep(n-1) else return 101 end end
  92. assert(deep(30000) == 101)
  93. a = {}
  94. function a:deep (n) if n>0 then return self:deep(n-1) else return 101 end end
  95. assert(a:deep(30000) == 101)
  96. do -- tail calls x varargs
  97. local function foo (x, ...) local a = {...}; return x, a[1], a[2] end
  98. local function foo1 (x) return foo(10, x, x + 1) end
  99. local a, b, c = foo1(-2)
  100. assert(a == 10 and b == -2 and c == -1)
  101. -- tail calls x metamethods
  102. local t = setmetatable({}, {__call = foo})
  103. local function foo2 (x) return t(10, x) end
  104. a, b, c = foo2(100)
  105. assert(a == t and b == 10 and c == 100)
  106. a, b = (function () return foo() end)()
  107. assert(a == nil and b == nil)
  108. local X, Y, A
  109. local function foo (x, y, ...) X = x; Y = y; A = {...} end
  110. local function foo1 (...) return foo(...) end
  111. local a, b, c = foo1()
  112. assert(X == nil and Y == nil and #A == 0)
  113. a, b, c = foo1(10)
  114. assert(X == 10 and Y == nil and #A == 0)
  115. a, b, c = foo1(10, 20)
  116. assert(X == 10 and Y == 20 and #A == 0)
  117. a, b, c = foo1(10, 20, 30)
  118. assert(X == 10 and Y == 20 and #A == 1 and A[1] == 30)
  119. end
  120. do -- C-stack overflow while handling C-stack overflow
  121. local function loop ()
  122. assert(pcall(loop))
  123. end
  124. local err, msg = xpcall(loop, loop)
  125. assert(not err and string.find(msg, "error"))
  126. end
  127. do -- tail calls x chain of __call
  128. local n = 10000 -- depth
  129. local function foo ()
  130. if n == 0 then return 1023
  131. else n = n - 1; return foo()
  132. end
  133. end
  134. -- build a chain of __call metamethods ending in function 'foo'
  135. for i = 1, 15 do
  136. foo = setmetatable({}, {__call = foo})
  137. end
  138. -- call the first one as a tail call in a new coroutine
  139. -- (to ensure stack is not preallocated)
  140. assert(coroutine.wrap(function() return foo() end)() == 1023)
  141. end
  142. print('+')
  143. do print"testing chains of '__call'"
  144. local N = 15
  145. local u = table.pack
  146. for i = 1, N do
  147. u = setmetatable({i}, {__call = u})
  148. end
  149. local Res = u("a", "b", "c")
  150. assert(Res.n == N + 3)
  151. for i = 1, N do
  152. assert(Res[i][1] == i)
  153. end
  154. assert(Res[N + 1] == "a" and Res[N + 2] == "b" and Res[N + 3] == "c")
  155. local function u (...)
  156. local n = debug.getinfo(1, 't').extraargs
  157. assert(select("#", ...) == n)
  158. return n
  159. end
  160. for i = 0, N do
  161. assert(u() == i)
  162. u = setmetatable({}, {__call = u})
  163. end
  164. end
  165. do -- testing chains too long
  166. local a = {}
  167. for i = 1, 16 do -- one too many
  168. a = setmetatable({}, {__call = a})
  169. end
  170. local status, msg = pcall(a)
  171. assert(not status and string.find(msg, "too long"))
  172. setmetatable(a, {__call = a}) -- infinite chain
  173. local status, msg = pcall(a)
  174. assert(not status and string.find(msg, "too long"))
  175. -- again, with a tail call
  176. local status, msg = pcall(function () return a() end)
  177. assert(not status and string.find(msg, "too long"))
  178. end
  179. a = nil
  180. (function (x) a=x end)(23)
  181. assert(a == 23 and (function (x) return x*2 end)(20) == 40)
  182. -- testing closures
  183. -- fixed-point operator
  184. local Z = function (le)
  185. local function a (f)
  186. return le(function (x) return f(f)(x) end)
  187. end
  188. return a(a)
  189. end
  190. -- non-recursive factorial
  191. local F = function (f)
  192. return function (n)
  193. if n == 0 then return 1
  194. else return n*f(n-1) end
  195. end
  196. end
  197. local fat = Z(F)
  198. assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
  199. local function g (z)
  200. local function f (a,b,c,d)
  201. return function (x,y) return a+b+c+d+a+x+y+z end
  202. end
  203. return f(z,z+1,z+2,z+3)
  204. end
  205. local f = g(10)
  206. assert(f(9, 16) == 10+11+12+13+10+9+16+10)
  207. print('+')
  208. -- testing multiple returns
  209. local function unlpack (t, i)
  210. i = i or 1
  211. if (i <= #t) then
  212. return t[i], unlpack(t, i+1)
  213. end
  214. end
  215. local function equaltab (t1, t2)
  216. assert(#t1 == #t2)
  217. for i = 1, #t1 do
  218. assert(t1[i] == t2[i])
  219. end
  220. end
  221. local pack = function (...) return (table.pack(...)) end
  222. local function f() return 1,2,30,4 end
  223. local function ret2 (a,b) return a,b end
  224. local a,b,c,d = unlpack{1,2,3}
  225. assert(a==1 and b==2 and c==3 and d==nil)
  226. a = {1,2,3,4,false,10,'alo',false,assert}
  227. equaltab(pack(unlpack(a)), a)
  228. equaltab(pack(unlpack(a), -1), {1,-1})
  229. a,b,c,d = ret2(f()), ret2(f())
  230. assert(a==1 and b==1 and c==2 and d==nil)
  231. a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
  232. assert(a==1 and b==1 and c==2 and d==nil)
  233. a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
  234. assert(a==1 and b==1 and c==nil and d==nil)
  235. a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
  236. assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
  237. -- testing calls with 'incorrect' arguments
  238. rawget({}, "x", 1)
  239. rawset({}, "x", 1, 2)
  240. assert(math.sin(1,2) == math.sin(1))
  241. table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
  242. -- test for generic load
  243. local x = "-- a comment\0\0\0\n x = 10 + \n23; \
  244. local a = function () x = 'hi' end; \
  245. return '\0'"
  246. local function read1 (x)
  247. local i = 0
  248. return function ()
  249. collectgarbage()
  250. i=i+1
  251. return string.sub(x, i, i)
  252. end
  253. end
  254. local function cannotload (msg, a,b)
  255. assert(not a and string.find(b, msg))
  256. end
  257. a = assert(load(read1(x), "modname", "t", _G))
  258. assert(a() == "\0" and _G.x == 33)
  259. assert(debug.getinfo(a).source == "modname")
  260. -- cannot read text in binary mode
  261. cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {}))
  262. cannotload("attempt to load a text chunk", load(x, "modname", "b"))
  263. a = assert(load(function () return nil end))
  264. a() -- empty chunk
  265. assert(not load(function () return true end))
  266. -- small bug
  267. local t = {nil, "return ", "3"}
  268. f, msg = load(function () return table.remove(t, 1) end)
  269. assert(f() == nil) -- should read the empty chunk
  270. -- another small bug (in 5.2.1)
  271. f = load(string.dump(function () return 1 end), nil, "b", {})
  272. assert(type(f) == "function" and f() == 1)
  273. do -- another bug (in 5.4.0)
  274. -- loading a binary long string interrupted by GC cycles
  275. local f = string.dump(function ()
  276. return '01234567890123456789012345678901234567890123456789'
  277. end)
  278. f = load(read1(f))
  279. assert(f() == '01234567890123456789012345678901234567890123456789')
  280. end
  281. x = string.dump(load("x = 1; return x"))
  282. a = assert(load(read1(x), nil, "b"))
  283. assert(a() == 1 and _G.x == 1)
  284. cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
  285. cannotload("attempt to load a binary chunk", load(x, nil, "t"))
  286. _G.x = nil
  287. assert(not pcall(string.dump, print)) -- no dump of C functions
  288. cannotload("unexpected symbol", load(read1("*a = 123")))
  289. cannotload("unexpected symbol", load("*a = 123"))
  290. cannotload("hhi", load(function () error("hhi") end))
  291. -- any value is valid for _ENV
  292. assert(load("return _ENV", nil, nil, 123)() == 123)
  293. -- load when _ENV is not first upvalue
  294. local x; XX = 123
  295. local function h ()
  296. local y=x -- use 'x', so that it becomes 1st upvalue
  297. return XX -- global name
  298. end
  299. local d = string.dump(h)
  300. x = load(d, "", "b")
  301. assert(debug.getupvalue(x, 2) == '_ENV')
  302. debug.setupvalue(x, 2, _G)
  303. assert(x() == 123)
  304. assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
  305. XX = nil
  306. -- test generic load with nested functions
  307. x = [[
  308. return function (x)
  309. return function (y)
  310. return function (z)
  311. return x+y+z
  312. end
  313. end
  314. end
  315. ]]
  316. a = assert(load(read1(x), "read", "t"))
  317. assert(a()(2)(3)(10) == 15)
  318. -- repeat the test loading a binary chunk
  319. x = string.dump(a)
  320. a = assert(load(read1(x), "read", "b"))
  321. assert(a()(2)(3)(10) == 15)
  322. -- test for dump/undump with upvalues
  323. local a, b = 20, 30
  324. x = load(string.dump(function (x)
  325. if x == "set" then a = 10+b; b = b+1 else
  326. return a
  327. end
  328. end), "", "b", nil)
  329. assert(x() == nil)
  330. assert(debug.setupvalue(x, 1, "hi") == "a")
  331. assert(x() == "hi")
  332. assert(debug.setupvalue(x, 2, 13) == "b")
  333. assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues
  334. x("set")
  335. assert(x() == 23)
  336. x("set")
  337. assert(x() == 24)
  338. -- test for dump/undump with many upvalues
  339. do
  340. local nup = 200 -- maximum number of local variables
  341. local prog = {"local a1"}
  342. for i = 2, nup do prog[#prog + 1] = ", a" .. i end
  343. prog[#prog + 1] = " = 1"
  344. for i = 2, nup do prog[#prog + 1] = ", " .. i end
  345. local sum = 1
  346. prog[#prog + 1] = "; return function () return a1"
  347. for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end
  348. prog[#prog + 1] = " end"
  349. prog = table.concat(prog)
  350. local f = assert(load(prog))()
  351. assert(f() == sum)
  352. f = load(string.dump(f)) -- main chunk now has many upvalues
  353. local a = 10
  354. local h = function () return a end
  355. for i = 1, nup do
  356. debug.upvaluejoin(f, i, h, 1)
  357. end
  358. assert(f() == 10 * nup)
  359. end
  360. -- test for long method names
  361. do
  362. local t = {x = 1}
  363. function t:_012345678901234567890123456789012345678901234567890123456789 ()
  364. return self.x
  365. end
  366. assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1)
  367. end
  368. -- test for bug in parameter adjustment
  369. assert((function () return nil end)(4) == nil)
  370. assert((function () local a; return a end)(4) == nil)
  371. assert((function (a) return a end)() == nil)
  372. print("testing binary chunks")
  373. do
  374. local header = string.pack("c4BBc6BBB",
  375. "\27Lua", -- signature
  376. 0x55, -- version 5.5 (0x55)
  377. 0, -- format
  378. "\x19\x93\r\n\x1a\n", -- data
  379. 4, -- size of instruction
  380. string.packsize("j"), -- sizeof(lua integer)
  381. string.packsize("n") -- sizeof(lua number)
  382. )
  383. local c = string.dump(function ()
  384. local a = 1; local b = 3;
  385. local f = function () return a + b + _ENV.c; end -- upvalues
  386. local s1 = "a constant"
  387. local s2 = "another constant"
  388. return a + b * 3
  389. end)
  390. assert(assert(load(c))() == 10)
  391. -- check header
  392. assert(string.sub(c, 1, #header) == header)
  393. -- check LUAC_INT and LUAC_NUM
  394. local ci, cn = string.unpack("jn", c, #header + 1)
  395. assert(ci == 0x5678 and cn == 370.5)
  396. -- corrupted header
  397. for i = 1, #header do
  398. local s = string.sub(c, 1, i - 1) ..
  399. string.char(string.byte(string.sub(c, i, i)) + 1) ..
  400. string.sub(c, i + 1, -1)
  401. assert(#s == #c)
  402. assert(not load(s))
  403. end
  404. -- loading truncated binary chunks
  405. for i = 1, #c - 1 do
  406. local st, msg = load(string.sub(c, 1, i))
  407. assert(not st and string.find(msg, "truncated"))
  408. end
  409. end
  410. do -- check reuse of strings in dumps
  411. local str = "|" .. string.rep("X", 50) .. "|"
  412. local foo = load(string.format([[
  413. local str <const> = "%s"
  414. return {
  415. function () return str end,
  416. function () return str end,
  417. function () return str end
  418. }
  419. ]], str))
  420. -- count occurrences of 'str' inside the dump
  421. local dump = string.dump(foo)
  422. local _, count = string.gsub(dump, str, {})
  423. -- there should be only two occurrences:
  424. -- one inside the source, other the string itself.
  425. assert(count == 2)
  426. if T then -- check reuse of strings in undump
  427. local funcs = load(dump)()
  428. assert(string.format("%p", T.listk(funcs[1])[1]) ==
  429. string.format("%p", T.listk(funcs[3])[1]))
  430. end
  431. end
  432. do -- test limit of multiple returns (254 values)
  433. local code = "return 10" .. string.rep(",10", 253)
  434. local res = {assert(load(code))()}
  435. assert(#res == 254 and res[254] == 10)
  436. code = code .. ",10"
  437. local status, msg = load(code)
  438. assert(not status and string.find(msg, "too many returns"))
  439. end
  440. print('OK')
  441. return deep