calls.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. x = string.dump(load("x = 1; return x"))
  241. a = assert(load(read1(x), nil, "b"))
  242. assert(a() == 1 and _G.x == 1)
  243. cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
  244. cannotload("attempt to load a binary chunk", load(x, nil, "t"))
  245. assert(not pcall(string.dump, print)) -- no dump of C functions
  246. cannotload("unexpected symbol", load(read1("*a = 123")))
  247. cannotload("unexpected symbol", load("*a = 123"))
  248. cannotload("hhi", load(function () error("hhi") end))
  249. -- any value is valid for _ENV
  250. assert(load("return _ENV", nil, nil, 123)() == 123)
  251. -- load when _ENV is not first upvalue
  252. local x; XX = 123
  253. local function h ()
  254. local y=x -- use 'x', so that it becomes 1st upvalue
  255. return XX -- global name
  256. end
  257. local d = string.dump(h)
  258. x = load(d, "", "b")
  259. assert(debug.getupvalue(x, 2) == '_ENV')
  260. debug.setupvalue(x, 2, _G)
  261. assert(x() == 123)
  262. assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
  263. -- test generic load with nested functions
  264. x = [[
  265. return function (x)
  266. return function (y)
  267. return function (z)
  268. return x+y+z
  269. end
  270. end
  271. end
  272. ]]
  273. a = assert(load(read1(x)))
  274. assert(a()(2)(3)(10) == 15)
  275. -- test for dump/undump with upvalues
  276. local a, b = 20, 30
  277. x = load(string.dump(function (x)
  278. if x == "set" then a = 10+b; b = b+1 else
  279. return a
  280. end
  281. end), "", "b", nil)
  282. assert(x() == nil)
  283. assert(debug.setupvalue(x, 1, "hi") == "a")
  284. assert(x() == "hi")
  285. assert(debug.setupvalue(x, 2, 13) == "b")
  286. assert(not debug.setupvalue(x, 3, 10)) -- only 2 upvalues
  287. x("set")
  288. assert(x() == 23)
  289. x("set")
  290. assert(x() == 24)
  291. -- test for dump/undump with many upvalues
  292. do
  293. local nup = 200 -- maximum number of local variables
  294. local prog = {"local a1"}
  295. for i = 2, nup do prog[#prog + 1] = ", a" .. i end
  296. prog[#prog + 1] = " = 1"
  297. for i = 2, nup do prog[#prog + 1] = ", " .. i end
  298. local sum = 1
  299. prog[#prog + 1] = "; return function () return a1"
  300. for i = 2, nup do prog[#prog + 1] = " + a" .. i; sum = sum + i end
  301. prog[#prog + 1] = " end"
  302. prog = table.concat(prog)
  303. local f = assert(load(prog))()
  304. assert(f() == sum)
  305. f = load(string.dump(f)) -- main chunk now has many upvalues
  306. local a = 10
  307. local h = function () return a end
  308. for i = 1, nup do
  309. debug.upvaluejoin(f, i, h, 1)
  310. end
  311. assert(f() == 10 * nup)
  312. end
  313. -- test for long method names
  314. do
  315. local t = {x = 1}
  316. function t:_012345678901234567890123456789012345678901234567890123456789 ()
  317. return self.x
  318. end
  319. assert(t:_012345678901234567890123456789012345678901234567890123456789() == 1)
  320. end
  321. -- test for bug in parameter adjustment
  322. assert((function () return nil end)(4) == nil)
  323. assert((function () local a; return a end)(4) == nil)
  324. assert((function (a) return a end)() == nil)
  325. print("testing binary chunks")
  326. do
  327. local header = string.pack("c4BBc6BBBj",
  328. "\27Lua", -- signature
  329. 0x54, -- version 5.4 (0x54)
  330. 0, -- format
  331. "\x19\x93\r\n\x1a\n", -- data
  332. 4, -- size of instruction
  333. string.packsize("j"), -- sizeof(lua integer)
  334. string.packsize("n"), -- sizeof(lua number)
  335. 0x5678 -- LUAC_INT
  336. -- LUAC_NUM may not have a unique binary representation (padding...)
  337. )
  338. local c = string.dump(function () local a = 1; local b = 3; return a+b*3 end)
  339. assert(string.sub(c, 1, #header) == header)
  340. -- corrupted header
  341. for i = 1, #header do
  342. local s = string.sub(c, 1, i - 1) ..
  343. string.char(string.byte(string.sub(c, i, i)) + 1) ..
  344. string.sub(c, i + 1, -1)
  345. assert(#s == #c)
  346. assert(not load(s))
  347. end
  348. -- loading truncated binary chunks
  349. for i = 1, #c - 1 do
  350. local st, msg = load(string.sub(c, 1, i))
  351. assert(not st and string.find(msg, "truncated"))
  352. end
  353. assert(assert(load(c))() == 10)
  354. end
  355. print('OK')
  356. return deep