calls.lua 14 KB

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