calls.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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, 100 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 -- testing chains of '__call'
  144. local N = 20
  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. end
  156. a = nil
  157. (function (x) a=x end)(23)
  158. assert(a == 23 and (function (x) return x*2 end)(20) == 40)
  159. -- testing closures
  160. -- fixed-point operator
  161. local Z = function (le)
  162. local function a (f)
  163. return le(function (x) return f(f)(x) end)
  164. end
  165. return a(a)
  166. end
  167. -- non-recursive factorial
  168. local F = function (f)
  169. return function (n)
  170. if n == 0 then return 1
  171. else return n*f(n-1) end
  172. end
  173. end
  174. local fat = Z(F)
  175. assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
  176. local function g (z)
  177. local function f (a,b,c,d)
  178. return function (x,y) return a+b+c+d+a+x+y+z end
  179. end
  180. return f(z,z+1,z+2,z+3)
  181. end
  182. local f = g(10)
  183. assert(f(9, 16) == 10+11+12+13+10+9+16+10)
  184. print('+')
  185. -- testing multiple returns
  186. local function unlpack (t, i)
  187. i = i or 1
  188. if (i <= #t) then
  189. return t[i], unlpack(t, i+1)
  190. end
  191. end
  192. local function equaltab (t1, t2)
  193. assert(#t1 == #t2)
  194. for i = 1, #t1 do
  195. assert(t1[i] == t2[i])
  196. end
  197. end
  198. local pack = function (...) return (table.pack(...)) end
  199. local function f() return 1,2,30,4 end
  200. local function ret2 (a,b) return a,b end
  201. local a,b,c,d = unlpack{1,2,3}
  202. assert(a==1 and b==2 and c==3 and d==nil)
  203. a = {1,2,3,4,false,10,'alo',false,assert}
  204. equaltab(pack(unlpack(a)), a)
  205. equaltab(pack(unlpack(a), -1), {1,-1})
  206. a,b,c,d = ret2(f()), ret2(f())
  207. assert(a==1 and b==1 and c==2 and d==nil)
  208. a,b,c,d = unlpack(pack(ret2(f()), ret2(f())))
  209. assert(a==1 and b==1 and c==2 and d==nil)
  210. a,b,c,d = unlpack(pack(ret2(f()), (ret2(f()))))
  211. assert(a==1 and b==1 and c==nil and d==nil)
  212. a = ret2{ unlpack{1,2,3}, unlpack{3,2,1}, unlpack{"a", "b"}}
  213. assert(a[1] == 1 and a[2] == 3 and a[3] == "a" and a[4] == "b")
  214. -- testing calls with 'incorrect' arguments
  215. rawget({}, "x", 1)
  216. rawset({}, "x", 1, 2)
  217. assert(math.sin(1,2) == math.sin(1))
  218. table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
  219. -- test for generic load
  220. local x = "-- a comment\0\0\0\n x = 10 + \n23; \
  221. local a = function () x = 'hi' end; \
  222. return '\0'"
  223. local function read1 (x)
  224. local i = 0
  225. return function ()
  226. collectgarbage()
  227. i=i+1
  228. return string.sub(x, i, i)
  229. end
  230. end
  231. local function cannotload (msg, a,b)
  232. assert(not a and string.find(b, msg))
  233. end
  234. a = assert(load(read1(x), "modname", "t", _G))
  235. assert(a() == "\0" and _G.x == 33)
  236. assert(debug.getinfo(a).source == "modname")
  237. -- cannot read text in binary mode
  238. cannotload("attempt to load a text chunk", load(read1(x), "modname", "b", {}))
  239. cannotload("attempt to load a text chunk", load(x, "modname", "b"))
  240. a = assert(load(function () return nil end))
  241. a() -- empty chunk
  242. assert(not load(function () return true end))
  243. -- small bug
  244. local t = {nil, "return ", "3"}
  245. f, msg = load(function () return table.remove(t, 1) end)
  246. assert(f() == nil) -- should read the empty chunk
  247. -- another small bug (in 5.2.1)
  248. f = load(string.dump(function () return 1 end), nil, "b", {})
  249. assert(type(f) == "function" and f() == 1)
  250. do -- another bug (in 5.4.0)
  251. -- loading a binary long string interrupted by GC cycles
  252. local f = string.dump(function ()
  253. return '01234567890123456789012345678901234567890123456789'
  254. end)
  255. f = load(read1(f))
  256. assert(f() == '01234567890123456789012345678901234567890123456789')
  257. end
  258. x = string.dump(load("x = 1; return x"))
  259. a = assert(load(read1(x), nil, "b"))
  260. assert(a() == 1 and _G.x == 1)
  261. cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
  262. cannotload("attempt to load a binary chunk", load(x, nil, "t"))
  263. _G.x = nil
  264. assert(not pcall(string.dump, print)) -- no dump of C functions
  265. cannotload("unexpected symbol", load(read1("*a = 123")))
  266. cannotload("unexpected symbol", load("*a = 123"))
  267. cannotload("hhi", load(function () error("hhi") end))
  268. -- any value is valid for _ENV
  269. assert(load("return _ENV", nil, nil, 123)() == 123)
  270. -- load when _ENV is not first upvalue
  271. local x; XX = 123
  272. local function h ()
  273. local y=x -- use 'x', so that it becomes 1st upvalue
  274. return XX -- global name
  275. end
  276. local d = string.dump(h)
  277. x = load(d, "", "b")
  278. assert(debug.getupvalue(x, 2) == '_ENV')
  279. debug.setupvalue(x, 2, _G)
  280. assert(x() == 123)
  281. assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
  282. XX = nil
  283. -- test generic load with nested functions
  284. x = [[
  285. return function (x)
  286. return function (y)
  287. return function (z)
  288. return x+y+z
  289. end
  290. end
  291. end
  292. ]]
  293. a = assert(load(read1(x), "read", "t"))
  294. assert(a()(2)(3)(10) == 15)
  295. -- repeat the test loading a binary chunk
  296. x = string.dump(a)
  297. a = assert(load(read1(x), "read", "b"))
  298. assert(a()(2)(3)(10) == 15)
  299. -- test for dump/undump with upvalues
  300. local a, b = 20, 30
  301. x = load(string.dump(function (x)
  302. if x == "set" then a = 10+b; b = b+1 else
  303. return a
  304. end
  305. end), "", "b", nil)
  306. assert(x() == nil)
  307. assert(debug.setupvalue(x, 1, "hi") == "a")
  308. assert(x() == "hi")
  309. assert(debug.setupvalue(x, 2, 13) == "b")
  310. assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues
  311. x("set")
  312. assert(x() == 23)
  313. x("set")
  314. assert(x() == 24)
  315. -- test for dump/undump with many upvalues
  316. do
  317. local nup = 200 -- maximum number of local variables
  318. local prog = {"local a1"}
  319. for i = 2, nup do prog[#prog + 1] = ", a" .. i end
  320. prog[#prog + 1] = " = 1"
  321. for i = 2, nup do prog[#prog + 1] = ", " .. i end
  322. local sum = 1
  323. prog[#prog + 1] = "; return function () return a1"
  324. for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end
  325. prog[#prog + 1] = " end"
  326. prog = table.concat(prog)
  327. local f = assert(load(prog))()
  328. assert(f() == sum)
  329. f = load(string.dump(f)) -- main chunk now has many upvalues
  330. local a = 10
  331. local h = function () return a end
  332. for i = 1, nup do
  333. debug.upvaluejoin(f, i, h, 1)
  334. end
  335. assert(f() == 10 * nup)
  336. end
  337. -- test for long method names
  338. do
  339. local t = {x = 1}
  340. function t:_012345678901234567890123456789012345678901234567890123456789 ()
  341. return self.x
  342. end
  343. assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1)
  344. end
  345. -- test for bug in parameter adjustment
  346. assert((function () return nil end)(4) == nil)
  347. assert((function () local a; return a end)(4) == nil)
  348. assert((function (a) return a end)() == nil)
  349. print("testing binary chunks")
  350. do
  351. local header = string.pack("c4BBc6BBB",
  352. "\27Lua", -- signature
  353. 0x54, -- version 5.4 (0x54)
  354. 0, -- format
  355. "\x19\x93\r\n\x1a\n", -- data
  356. 4, -- size of instruction
  357. string.packsize("j"), -- sizeof(lua integer)
  358. string.packsize("n") -- sizeof(lua number)
  359. )
  360. local c = string.dump(function ()
  361. local a = 1; local b = 3;
  362. local f = function () return a + b + _ENV.c; end -- upvalues
  363. local s1 = "a constant"
  364. local s2 = "another constant"
  365. return a + b * 3
  366. end)
  367. assert(assert(load(c))() == 10)
  368. -- check header
  369. assert(string.sub(c, 1, #header) == header)
  370. -- check LUAC_INT and LUAC_NUM
  371. local ci, cn = string.unpack("jn", c, #header + 1)
  372. assert(ci == 0x5678 and cn == 370.5)
  373. -- corrupted header
  374. for i = 1, #header do
  375. local s = string.sub(c, 1, i - 1) ..
  376. string.char(string.byte(string.sub(c, i, i)) + 1) ..
  377. string.sub(c, i + 1, -1)
  378. assert(#s == #c)
  379. assert(not load(s))
  380. end
  381. -- loading truncated binary chunks
  382. for i = 1, #c - 1 do
  383. local st, msg = load(string.sub(c, 1, i))
  384. assert(not st and string.find(msg, "truncated"))
  385. end
  386. end
  387. print('OK')
  388. return deep