calls.lua 11 KB

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