errors.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. -- $Id: testes/errors.lua $
  2. -- See Copyright Notice in file all.lua
  3. print("testing errors")
  4. local debug = require"debug"
  5. -- avoid problems with 'strict' module (which may generate other error messages)
  6. local mt = getmetatable(_G) or {}
  7. local oldmm = mt.__index
  8. mt.__index = nil
  9. local function checkerr (msg, f, ...)
  10. local st, err = pcall(f, ...)
  11. assert(not st and string.find(err, msg))
  12. end
  13. local function doit (s)
  14. local f, msg = load(s)
  15. if not f then return msg end
  16. local cond, msg = pcall(f)
  17. return (not cond) and msg
  18. end
  19. local function checkmessage (prog, msg, debug)
  20. local m = doit(prog)
  21. if debug then print(m, msg) end
  22. assert(string.find(m, msg, 1, true))
  23. end
  24. local function checksyntax (prog, extra, token, line)
  25. local msg = doit(prog)
  26. if not string.find(token, "^<%a") and not string.find(token, "^char%(")
  27. then token = "'"..token.."'" end
  28. token = string.gsub(token, "(%p)", "%%%1")
  29. local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]],
  30. line, token)
  31. assert(string.find(msg, pt))
  32. assert(string.find(msg, msg, 1, true))
  33. end
  34. -- test error message with no extra info
  35. assert(doit("error('hi', 0)") == 'hi')
  36. -- test error message with no info
  37. assert(doit("error()") == nil)
  38. -- test common errors/errors that crashed in the past
  39. assert(doit("table.unpack({}, 1, n=2^30)"))
  40. assert(doit("a=math.sin()"))
  41. assert(not doit("tostring(1)") and doit("tostring()"))
  42. assert(doit"tonumber()")
  43. assert(doit"repeat until 1; a")
  44. assert(doit"return;;")
  45. assert(doit"assert(false)")
  46. assert(doit"assert(nil)")
  47. assert(doit("function a (... , ...) end"))
  48. assert(doit("function a (, ...) end"))
  49. assert(doit("local t={}; t = t[#t] + 1"))
  50. checksyntax([[
  51. local a = {4
  52. ]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
  53. do -- testing errors in goto/break
  54. local function checksyntax (prog, msg, line)
  55. local st, err = load(prog)
  56. assert(string.find(err, "line " .. line))
  57. assert(string.find(err, msg, 1, true))
  58. end
  59. checksyntax([[
  60. ::A:: a = 1
  61. ::A::
  62. ]], "label 'A' already defined", 1)
  63. checksyntax([[
  64. a = 1
  65. goto A
  66. do ::A:: end
  67. ]], "no visible label 'A'", 2)
  68. end
  69. if not T then
  70. (Message or print)
  71. ('\n >>> testC not active: skipping memory message test <<<\n')
  72. else
  73. print "testing memory error message"
  74. local a = {}
  75. for i = 1, 10000 do a[i] = true end -- preallocate array
  76. collectgarbage()
  77. T.totalmem(T.totalmem() + 10000)
  78. -- force a memory error (by a small margin)
  79. local st, msg = pcall(function()
  80. for i = 1, 100000 do a[i] = tostring(i) end
  81. end)
  82. T.totalmem(0)
  83. assert(not st and msg == "not enough" .. " memory")
  84. end
  85. -- tests for better error messages
  86. checkmessage("a = {} + 1", "arithmetic")
  87. checkmessage("a = {} | 1", "bitwise operation")
  88. checkmessage("a = {} < 1", "attempt to compare")
  89. checkmessage("a = {} <= 1", "attempt to compare")
  90. checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'")
  91. checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'")
  92. checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
  93. assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
  94. checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
  95. checkmessage("aaa=(1)..{}", "a table value")
  96. _G.aaa, _G.bbbb = nil
  97. -- calls
  98. checkmessage("local a; a(13)", "local 'a'")
  99. checkmessage([[
  100. local a = setmetatable({}, {__add = 34})
  101. a = a + 1
  102. ]], "metamethod 'add'")
  103. checkmessage([[
  104. local a = setmetatable({}, {__lt = {}})
  105. a = a > a
  106. ]], "metamethod 'lt'")
  107. -- tail calls
  108. checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
  109. checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'")
  110. checkmessage("aaa = #print", "length of a function value")
  111. checkmessage("aaa = #3", "length of a number value")
  112. _G.aaa = nil
  113. checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
  114. checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
  115. checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
  116. checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'")
  117. assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
  118. -- upvalues being indexed do not go to the stack
  119. checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'")
  120. checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
  121. checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
  122. checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'")
  123. checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
  124. checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'")
  125. checkmessage("aaa={}; x=-aaa", "global 'aaa'")
  126. -- short circuit
  127. checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)",
  128. "local 'bbbb'")
  129. checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)",
  130. "local 'bbbb'")
  131. checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
  132. checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
  133. assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
  134. assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'"))
  135. checkmessage("print(print < 10)", "function with number")
  136. checkmessage("print(print < print)", "two function values")
  137. checkmessage("print('10' < 10)", "string with number")
  138. checkmessage("print(10 < '23')", "number with string")
  139. -- float->integer conversions
  140. checkmessage("local a = 2.0^100; x = a << 2", "local a")
  141. checkmessage("local a = 1 >> 2.0^100", "has no integer representation")
  142. checkmessage("local a = 10.1 << 2.0^100", "has no integer representation")
  143. checkmessage("local a = 2.0^100 & 1", "has no integer representation")
  144. checkmessage("local a = 2.0^100 & 1e100", "has no integer representation")
  145. checkmessage("local a = 2.0 | 1e40", "has no integer representation")
  146. checkmessage("local a = 2e100 ~ 1", "has no integer representation")
  147. checkmessage("string.sub('a', 2.0^100)", "has no integer representation")
  148. checkmessage("string.rep('a', 3.3)", "has no integer representation")
  149. checkmessage("return 6e40 & 7", "has no integer representation")
  150. checkmessage("return 34 << 7e30", "has no integer representation")
  151. checkmessage("return ~-3e40", "has no integer representation")
  152. checkmessage("return ~-3.009", "has no integer representation")
  153. checkmessage("return 3.009 & 1", "has no integer representation")
  154. checkmessage("return 34 >> {}", "table value")
  155. checkmessage("aaa = 24 // 0", "divide by zero")
  156. checkmessage("aaa = 1 % 0", "'n%0'")
  157. -- type error for an object which is neither in an upvalue nor a register.
  158. -- The following code will try to index the value 10 that is stored in
  159. -- the metatable, without moving it to a register.
  160. checkmessage("local a = setmetatable({}, {__index = 10}).x",
  161. "attempt to index a number value")
  162. -- numeric for loops
  163. checkmessage("for i = {}, 10 do end", "table")
  164. checkmessage("for i = io.stdin, 10 do end", "FILE")
  165. checkmessage("for i = {}, 10 do end", "initial value")
  166. checkmessage("for i = 1, 'x', 10 do end", "string")
  167. checkmessage("for i = 1, {}, 10 do end", "limit")
  168. checkmessage("for i = 1, {} do end", "limit")
  169. checkmessage("for i = 1, 10, print do end", "step")
  170. checkmessage("for i = 1, 10, print do end", "function")
  171. -- passing light userdata instead of full userdata
  172. _G.D = debug
  173. checkmessage([[
  174. -- create light udata
  175. local x = D.upvalueid(function () return debug end, 1)
  176. D.setuservalue(x, {})
  177. ]], "light userdata")
  178. _G.D = nil
  179. do -- named objects (field '__name')
  180. checkmessage("math.sin(io.input())", "(number expected, got FILE*)")
  181. _G.XX = setmetatable({}, {__name = "My Type"})
  182. assert(string.find(tostring(XX), "^My Type"))
  183. checkmessage("io.input(XX)", "(FILE* expected, got My Type)")
  184. checkmessage("return XX + 1", "on a My Type value")
  185. checkmessage("return ~io.stdin", "on a FILE* value")
  186. checkmessage("return XX < XX", "two My Type values")
  187. checkmessage("return {} < XX", "table with My Type")
  188. checkmessage("return XX < io.stdin", "My Type with FILE*")
  189. _G.XX = nil
  190. if T then -- extra tests for 'luaL_tolstring'
  191. -- bug in 5.4.3; 'luaL_tolstring' with negative indices
  192. local x = setmetatable({}, {__name="TABLE"})
  193. assert(T.testC("Ltolstring -1; return 1", x) == tostring(x))
  194. local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x)
  195. assert(a == 10 and b == tostring(x))
  196. setmetatable(x, {__tostring=function (o)
  197. assert(o == x)
  198. return "ABC"
  199. end})
  200. local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x)
  201. assert(a == x and b == 10 and c == "ABC")
  202. end
  203. end
  204. -- global functions
  205. checkmessage("(io.write or print){}", "io.write")
  206. checkmessage("(collectgarbage or print){}", "collectgarbage")
  207. -- errors in functions without debug info
  208. do
  209. local f = function (a) return a + 1 end
  210. f = assert(load(string.dump(f, true)))
  211. assert(f(3) == 4)
  212. checkerr("^%?:%-1:", f, {})
  213. -- code with a move to a local var ('OP_MOV A B' with A<B)
  214. f = function () local a; a = {}; return a + 2 end
  215. -- no debug info (so that 'a' is unknown)
  216. f = assert(load(string.dump(f, true)))
  217. -- symbolic execution should not get lost
  218. checkerr("^%?:%-1:.*table value", f)
  219. end
  220. -- tests for field accesses after RK limit
  221. local t = {}
  222. for i = 1, 1000 do
  223. t[i] = "aaa = x" .. i
  224. end
  225. local s = table.concat(t, "; ")
  226. t = nil
  227. checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
  228. checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
  229. checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
  230. checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
  231. checkmessage([[aaa=9
  232. repeat until 3==3
  233. local x=math.sin(math.cos(3))
  234. if math.sin(1) == x then return math.sin(1) end -- tail call
  235. local a,b = 1, {
  236. {x='a'..'b'..'c', y='b', z=x},
  237. {1,2,3,4,5} or 3+3<=3+3,
  238. 3+1>3+1,
  239. {d = x and aaa[x or y]}}
  240. ]], "global 'aaa'")
  241. checkmessage([[
  242. local x,y = {},1
  243. if math.sin(1) == 0 then return 3 end -- return
  244. x.a()]], "field 'a'")
  245. checkmessage([[
  246. prefix = nil
  247. insert = nil
  248. while 1 do
  249. local a
  250. if nil then break end
  251. insert(prefix, a)
  252. end]], "global 'insert'")
  253. checkmessage([[ -- tail call
  254. return math.sin("a")
  255. ]], "sin")
  256. checkmessage([[collectgarbage("nooption")]], "invalid option")
  257. checkmessage([[x = print .. "a"]], "concatenate")
  258. checkmessage([[x = "a" .. false]], "concatenate")
  259. checkmessage([[x = {} .. 2]], "concatenate")
  260. checkmessage("getmetatable(io.stdin).__gc()", "no value")
  261. checkmessage([[
  262. local Var
  263. local function main()
  264. NoSuchName (function() Var=0 end)
  265. end
  266. main()
  267. ]], "global 'NoSuchName'")
  268. print'+'
  269. aaa = {}; setmetatable(aaa, {__index = string})
  270. checkmessage("aaa:sub()", "bad self")
  271. checkmessage("string.sub('a', {})", "#2")
  272. checkmessage("('a'):sub{}", "#1")
  273. checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
  274. checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
  275. _G.aaa = nil
  276. -- tests for errors in coroutines
  277. local function f (n)
  278. local c = coroutine.create(f)
  279. local a,b = coroutine.resume(c)
  280. return b
  281. end
  282. assert(string.find(f(), "C stack overflow"))
  283. checkmessage("coroutine.yield()", "outside a coroutine")
  284. f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end)
  285. checkerr("yield across", f)
  286. -- testing size of 'source' info; size of buffer for that info is
  287. -- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
  288. local idsize = 60 - 1
  289. local function checksize (source)
  290. -- syntax error
  291. local _, msg = load("x", source)
  292. msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':')
  293. assert(msg:len() <= idsize)
  294. end
  295. for i = 60 - 10, 60 + 10 do -- check border cases around 60
  296. checksize("@" .. string.rep("x", i)) -- file names
  297. checksize(string.rep("x", i - 10)) -- string sources
  298. checksize("=" .. string.rep("x", i)) -- exact sources
  299. end
  300. -- testing line error
  301. local function lineerror (s, l)
  302. local err,msg = pcall(load(s))
  303. local line = tonumber(string.match(msg, ":(%d+):"))
  304. assert(line == l or (not line and not l))
  305. end
  306. lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2)
  307. lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3)
  308. lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4)
  309. lineerror("function a.x.y ()\na=a+1\nend", 1)
  310. lineerror("a = \na\n+\n{}", 3)
  311. lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6)
  312. lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3)
  313. lineerror("a\n=\n-\n\nprint\n;", 3)
  314. lineerror([[
  315. a
  316. (
  317. 23)
  318. ]], 1)
  319. lineerror([[
  320. local a = {x = 13}
  321. a
  322. .
  323. x
  324. (
  325. 23
  326. )
  327. ]], 2)
  328. lineerror([[
  329. local a = {x = 13}
  330. a
  331. .
  332. x
  333. (
  334. 23 + a
  335. )
  336. ]], 6)
  337. local p = [[
  338. function g() f() end
  339. function f(x) error('a', XX) end
  340. g()
  341. ]]
  342. XX=3;lineerror((p), 3)
  343. XX=0;lineerror((p), false)
  344. XX=1;lineerror((p), 2)
  345. XX=2;lineerror((p), 1)
  346. _G.XX, _G.g, _G.f = nil
  347. lineerror([[
  348. local b = false
  349. if not b then
  350. error 'test'
  351. end]], 3)
  352. lineerror([[
  353. local b = false
  354. if not b then
  355. if not b then
  356. if not b then
  357. error 'test'
  358. end
  359. end
  360. end]], 5)
  361. -- bug in 5.4.0
  362. lineerror([[
  363. local a = 0
  364. local b = 1
  365. local c = b % a
  366. ]], 3)
  367. do
  368. -- Force a negative estimate for base line. Error in instruction 2
  369. -- (after VARARGPREP, GETGLOBAL), with first absolute line information
  370. -- (forced by too many lines) in instruction 0.
  371. local s = string.format("%s return __A.x", string.rep("\n", 300))
  372. lineerror(s, 301)
  373. end
  374. if not _soft then
  375. -- several tests that exaust the Lua stack
  376. collectgarbage()
  377. print"testing stack overflow"
  378. local C = 0
  379. -- get line where stack overflow will happen
  380. local l = debug.getinfo(1, "l").currentline + 1
  381. local function auxy () C=C+1; auxy() end -- produce a stack overflow
  382. function YY ()
  383. collectgarbage("stop") -- avoid running finalizers without stack space
  384. auxy()
  385. collectgarbage("restart")
  386. end
  387. local function checkstackmessage (m)
  388. print("(expected stack overflow after " .. C .. " calls)")
  389. C = 0 -- prepare next count
  390. return (string.find(m, "stack overflow"))
  391. end
  392. -- repeated stack overflows (to check stack recovery)
  393. assert(checkstackmessage(doit('YY()')))
  394. assert(checkstackmessage(doit('YY()')))
  395. assert(checkstackmessage(doit('YY()')))
  396. _G.YY = nil
  397. -- error lines in stack overflow
  398. local l1
  399. local function g(x)
  400. l1 = debug.getinfo(x, "l").currentline + 2
  401. collectgarbage("stop") -- avoid running finalizers without stack space
  402. auxy()
  403. collectgarbage("restart")
  404. end
  405. local _, stackmsg = xpcall(g, debug.traceback, 1)
  406. print('+')
  407. local stack = {}
  408. for line in string.gmatch(stackmsg, "[^\n]*") do
  409. local curr = string.match(line, ":(%d+):")
  410. if curr then table.insert(stack, tonumber(curr)) end
  411. end
  412. local i=1
  413. while stack[i] ~= l1 do
  414. assert(stack[i] == l)
  415. i = i+1
  416. end
  417. assert(i > 15)
  418. -- error in error handling
  419. local res, msg = xpcall(error, error)
  420. assert(not res and type(msg) == 'string')
  421. print('+')
  422. local function f (x)
  423. if x==0 then error('a\n')
  424. else
  425. local aux = function () return f(x-1) end
  426. local a,b = xpcall(aux, aux)
  427. return a,b
  428. end
  429. end
  430. f(3)
  431. local function loop (x,y,z) return 1 + loop(x, y, z) end
  432. local res, msg = xpcall(loop, function (m)
  433. assert(string.find(m, "stack overflow"))
  434. checkerr("error handling", loop)
  435. assert(math.sin(0) == 0)
  436. return 15
  437. end)
  438. assert(msg == 15)
  439. local f = function ()
  440. for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end
  441. end
  442. checkerr("too many results", f)
  443. end
  444. do
  445. -- non string messages
  446. local t = {}
  447. local res, msg = pcall(function () error(t) end)
  448. assert(not res and msg == t)
  449. res, msg = pcall(function () error(nil) end)
  450. assert(not res and msg == nil)
  451. local function f() error{msg='x'} end
  452. res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
  453. assert(msg.msg == 'xy')
  454. -- 'assert' with extra arguments
  455. res, msg = pcall(assert, false, "X", t)
  456. assert(not res and msg == "X")
  457. -- 'assert' with no message
  458. res, msg = pcall(function () assert(false) end)
  459. local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$")
  460. assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2)
  461. -- 'assert' with non-string messages
  462. res, msg = pcall(assert, false, t)
  463. assert(not res and msg == t)
  464. res, msg = pcall(assert, nil, nil)
  465. assert(not res and msg == nil)
  466. -- 'assert' without arguments
  467. res, msg = pcall(assert)
  468. assert(not res and string.find(msg, "value expected"))
  469. end
  470. -- xpcall with arguments
  471. local a, b, c = xpcall(string.find, error, "alo", "al")
  472. assert(a and b == 1 and c == 2)
  473. a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
  474. assert(not a and type(b) == "table" and c == nil)
  475. print("testing tokens in error messages")
  476. checksyntax("syntax error", "", "error", 1)
  477. checksyntax("1.000", "", "1.000", 1)
  478. checksyntax("[[a]]", "", "[[a]]", 1)
  479. checksyntax("'aa'", "", "'aa'", 1)
  480. checksyntax("while << do end", "", "<<", 1)
  481. checksyntax("for >> do end", "", ">>", 1)
  482. -- test invalid non-printable char in a chunk
  483. checksyntax("a\1a = 1", "", "<\\1>", 1)
  484. -- test 255 as first char in a chunk
  485. checksyntax("\255a = 1", "", "<\\255>", 1)
  486. doit('I = load("a=9+"); aaa=3')
  487. assert(_G.aaa==3 and not _G.I)
  488. _G.I,_G.aaa = nil
  489. print('+')
  490. local lim = 1000
  491. if _soft then lim = 100 end
  492. for i=1,lim do
  493. doit('a = ')
  494. doit('a = 4+nil')
  495. end
  496. -- testing syntax limits
  497. local function testrep (init, rep, close, repc, finalresult)
  498. local s = init .. string.rep(rep, 100) .. close .. string.rep(repc, 100)
  499. local res, msg = load(s)
  500. assert(res) -- 100 levels is OK
  501. if (finalresult) then
  502. assert(res() == finalresult)
  503. end
  504. s = init .. string.rep(rep, 500)
  505. local res, msg = load(s) -- 500 levels not ok
  506. assert(not res and (string.find(msg, "too many") or
  507. string.find(msg, "overflow")))
  508. end
  509. testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
  510. testrep("local a; a=", "{", "0", "}")
  511. testrep("return ", "(", "2", ")", 2)
  512. testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2)
  513. testrep("", "do ", "", " end")
  514. testrep("", "while a do ", "", " end")
  515. testrep("local a; ", "if a then else ", "", " end")
  516. testrep("", "function foo () ", "", " end")
  517. testrep("local a = ''; return ", "a..", "'a'", "", "a")
  518. testrep("local a = 1; return ", "a^", "a", "", 1)
  519. checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
  520. -- testing other limits
  521. -- upvalues
  522. local lim = 127
  523. local s = "local function fooA ()\n local "
  524. for j = 1,lim do
  525. s = s.."a"..j..", "
  526. end
  527. s = s.."b,c\n"
  528. s = s.."local function fooB ()\n local "
  529. for j = 1,lim do
  530. s = s.."b"..j..", "
  531. end
  532. s = s.."b\n"
  533. s = s.."function fooC () return b+c"
  534. local c = 1+2
  535. for j = 1,lim do
  536. s = s.."+a"..j.."+b"..j
  537. c = c + 2
  538. end
  539. s = s.."\nend end end"
  540. local a,b = load(s)
  541. assert(c > 255 and string.find(b, "too many upvalues") and
  542. string.find(b, "line 5"))
  543. -- local variables
  544. s = "\nfunction foo ()\n local "
  545. for j = 1,300 do
  546. s = s.."a"..j..", "
  547. end
  548. s = s.."b\n"
  549. local a,b = load(s)
  550. assert(string.find(b, "line 2") and string.find(b, "too many local variables"))
  551. mt.__index = oldmm
  552. print('OK')