interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / errors.lua

782 lines · 22344 bytes Raw Blame History
1-- $Id: testes/errors.lua $
2-- See Copyright Notice in file lua.h
3
4print("testing errors")
5
6local debug = require"debug"
7
8-- avoid problems with 'strict' module (which may generate other error messages)
9local mt = getmetatable(_G) or {}
10local oldmm = mt.__index
11mt.__index = nil
12
13local function checkerr (msg, f, ...)
14 local st, err = pcall(f, ...)
15 assert(not st and string.find(err, msg))
16end
17
18
19local function doit (s)
20 local f, msg = load(s)
21 if not f then return msg end
22 local cond, msg = pcall(f)
23 return (not cond) and msg
24end
25
26
27local function checkmessage (prog, msg, debug)
28 local m = doit(prog)
29 if debug then print(m, msg) end
30 assert(string.find(m, msg, 1, true))
31end
32
33local function checksyntax (prog, extra, token, line)
34 local msg = doit(prog)
35 if not string.find(token, "^<%a") and not string.find(token, "^char%(")
36 then token = "'"..token.."'" end
37 token = string.gsub(token, "(%p)", "%%%1")
38 local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]],
39 line, token)
40 assert(string.find(msg, pt))
41 assert(string.find(msg, msg, 1, true))
42end
43
44
45-- test error message with no extra info
46assert(doit("error('hi', 0)") == 'hi')
47
48-- test nil error message
49assert(doit("error()") == "<no error object>")
50
51
52-- test common errors/errors that crashed in the past
53assert(doit("table.unpack({}, 1, n=2^30)"))
54assert(doit("a=math.sin()"))
55assert(not doit("tostring(1)") and doit("tostring()"))
56assert(doit"tonumber()")
57assert(doit"repeat until 1; a")
58assert(doit"return;;")
59assert(doit"assert(false)")
60assert(doit"assert(nil)")
61assert(doit("function a (... , ...) end"))
62assert(doit("function a (, ...) end"))
63assert(doit("local t={}; t = t[#t] + 1"))
64
65checksyntax([[
66 local a = {4
67
68]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
69
70
71do -- testing errors in goto/break
72 local function checksyntax (prog, msg, line)
73 local st, err = load(prog)
74 assert(string.find(err, "line " .. line))
75 assert(string.find(err, msg, 1, true))
76 end
77
78 checksyntax([[
79 ::A:: a = 1
80 ::A::
81 ]], "label 'A' already defined", 1)
82
83 checksyntax([[
84 a = 1
85 goto A
86 do ::A:: end
87 ]], "no visible label 'A'", 2)
88
89end
90
91
92if not T then
93 (Message or print)
94 ('\n >>> testC not active: skipping tests for messages in C <<<\n')
95else
96 print "testing memory error message"
97 local a = {}
98 for i = 1, 10000 do a[i] = true end -- preallocate array
99 collectgarbage()
100 T.totalmem(T.totalmem() + 10000)
101 -- force a memory error (by a small margin)
102 local st, msg = pcall(function()
103 for i = 1, 100000 do a[i] = tostring(i) end
104 end)
105 T.totalmem(0)
106 assert(not st and msg == "not enough" .. " memory")
107
108 -- stack space for luaL_traceback (bug in 5.4.6)
109 local res = T.testC[[
110 # push 16 elements on the stack
111 pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
112 pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
113 pushnum 1; pushnum 1; pushnum 1; pushnum 1; pushnum 1;
114 pushnum 1;
115 # traceback should work with 4 remaining slots
116 traceback xuxu 1;
117 return 1
118 ]]
119 assert(string.find(res, "xuxu.-main chunk"))
120
121 do -- tests for error messages about extra arguments from __call
122 local function createobj (n)
123 -- function that raises an error on its n-th argument
124 local code = string.format("argerror %d 'msg'", n)
125 local func = T.makeCfunc(code)
126 -- create a chain of 2 __call objects
127 local M = setmetatable({}, {__call = func})
128 M = setmetatable({}, {__call = M})
129 -- put it as a method for a new object
130 return {foo = M}
131 end
132
133 _G.a = createobj(1) -- error in first (extra) argument
134 checkmessage("a:foo()", "bad extra argument #1")
135
136 _G.a = createobj(2) -- error in second (extra) argument
137 checkmessage("a:foo()", "bad extra argument #2")
138
139 _G.a = createobj(3) -- error in self (after two extra arguments)
140 checkmessage("a:foo()", "bad self")
141
142 _G.a = createobj(4) -- error in first regular argument (after self)
143 checkmessage("a:foo()", "bad argument #1")
144 end
145end
146
147
148-- tests for better error messages
149
150checkmessage("a = {} + 1", "arithmetic")
151checkmessage("a = {} | 1", "bitwise operation")
152checkmessage("a = {} < 1", "attempt to compare")
153checkmessage("a = {} <= 1", "attempt to compare")
154
155checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'")
156checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'")
157checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
158assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
159checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
160checkmessage("aaa=(1)..{}", "a table value")
161
162checkmessage("local function foo (...t) return t.xx + 1 end; foo()",
163 "field 'xx'")
164
165-- bug in 5.4.6
166checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'")
167
168-- a similar bug, since 5.4.0
169checkmessage("print(('_ENV').x + 1)", "field 'x'")
170
171_G.aaa, _G.bbbb = nil
172
173-- calls
174checkmessage("local a; a(13)", "local 'a'")
175checkmessage([[
176 local a = setmetatable({}, {__add = 34})
177 a = a + 1
178]], "metamethod 'add'")
179checkmessage([[
180 local a = setmetatable({}, {__lt = {}})
181 a = a > a
182]], "metamethod 'lt'")
183
184-- tail calls
185checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
186checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'")
187
188checkmessage("aaa = #print", "length of a function value")
189checkmessage("aaa = #3", "length of a number value")
190
191_G.aaa = nil
192
193checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
194checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
195checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
196checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'")
197assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
198
199-- upvalues being indexed do not go to the stack
200checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'")
201checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
202
203checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
204
205checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'")
206checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
207checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'")
208checkmessage("aaa={}; x=-aaa", "global 'aaa'")
209
210-- short circuit
211checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)",
212 "local 'bbbb'")
213checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)",
214 "local 'bbbb'")
215checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
216checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
217assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
218assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'"))
219
220checkmessage("print(print < 10)", "function with number")
221checkmessage("print(print < print)", "two function values")
222checkmessage("print('10' < 10)", "string with number")
223checkmessage("print(10 < '23')", "number with string")
224
225-- float->integer conversions
226checkmessage("local a = 2.0^100; x = a << 2", "local a")
227checkmessage("local a = 1 >> 2.0^100", "has no integer representation")
228checkmessage("local a = 10.1 << 2.0^100", "has no integer representation")
229checkmessage("local a = 2.0^100 & 1", "has no integer representation")
230checkmessage("local a = 2.0^100 & 1e100", "has no integer representation")
231checkmessage("local a = 2.0 | 1e40", "has no integer representation")
232checkmessage("local a = 2e100 ~ 1", "has no integer representation")
233checkmessage("string.sub('a', 2.0^100)", "has no integer representation")
234checkmessage("string.rep('a', 3.3)", "has no integer representation")
235checkmessage("return 6e40 & 7", "has no integer representation")
236checkmessage("return 34 << 7e30", "has no integer representation")
237checkmessage("return ~-3e40", "has no integer representation")
238checkmessage("return ~-3.009", "has no integer representation")
239checkmessage("return 3.009 & 1", "has no integer representation")
240checkmessage("return 34 >> {}", "table value")
241checkmessage("aaa = 24 // 0", "divide by zero")
242checkmessage("aaa = 1 % 0", "'n%0'")
243
244
245-- type error for an object which is neither in an upvalue nor a register.
246-- The following code will try to index the value 10 that is stored in
247-- the metatable, without moving it to a register.
248checkmessage("local a = setmetatable({}, {__index = 10}).x",
249 "attempt to index a number value")
250
251
252-- numeric for loops
253checkmessage("for i = {}, 10 do end", "table")
254checkmessage("for i = io.stdin, 10 do end", "FILE")
255checkmessage("for i = {}, 10 do end", "initial value")
256checkmessage("for i = 1, 'x', 10 do end", "string")
257checkmessage("for i = 1, {}, 10 do end", "limit")
258checkmessage("for i = 1, {} do end", "limit")
259checkmessage("for i = 1, 10, print do end", "step")
260checkmessage("for i = 1, 10, print do end", "function")
261
262-- passing light userdata instead of full userdata
263_G.D = debug
264checkmessage([[
265 -- create light udata
266 local x = D.upvalueid(function () return debug end, 1)
267 D.setuservalue(x, {})
268]], "light userdata")
269_G.D = nil
270
271do -- named objects (field '__name')
272 checkmessage("math.sin(io.input())", "(number expected, got FILE*)")
273 _G.XX = setmetatable({}, {__name = "My Type"})
274 assert(string.find(tostring(XX), "^My Type"))
275 checkmessage("io.input(XX)", "(FILE* expected, got My Type)")
276 checkmessage("return XX + 1", "on a My Type value")
277 checkmessage("return ~io.stdin", "on a FILE* value")
278 checkmessage("return XX < XX", "two My Type values")
279 checkmessage("return {} < XX", "table with My Type")
280 checkmessage("return XX < io.stdin", "My Type with FILE*")
281 _G.XX = nil
282
283 if T then -- extra tests for 'luaL_tolstring'
284 -- bug in 5.4.3; 'luaL_tolstring' with negative indices
285 local x = setmetatable({}, {__name="TABLE"})
286 assert(T.testC("Ltolstring -1; return 1", x) == tostring(x))
287
288 local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x)
289 assert(a == 10 and b == tostring(x))
290
291 setmetatable(x, {__tostring=function (o)
292 assert(o == x)
293 return "ABC"
294 end})
295 local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x)
296 assert(a == x and b == 10 and c == "ABC")
297 end
298end
299
300-- global functions
301checkmessage("(io.write or print){}", "io.write")
302checkmessage("(collectgarbage or print){}", "collectgarbage")
303
304-- errors in functions without debug info
305do
306 local f = function (a) return a + 1 end
307 f = assert(load(string.dump(f, true)))
308 assert(f(3) == 4)
309 checkerr("^%?:%?:", f, {})
310
311 -- code with a move to a local var ('OP_MOV A B' with A<B)
312 f = function () local a; a = {}; return a + 2 end
313 -- no debug info (so that 'a' is unknown)
314 f = assert(load(string.dump(f, true)))
315 -- symbolic execution should not get lost
316 checkerr("^%?:%?:.*table value", f)
317end
318
319
320-- tests for field accesses after RK limit
321local t = {}
322for i = 1, 1000 do
323 t[i] = "aaa = x" .. i
324end
325local s = table.concat(t, "; ")
326t = nil
327checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
328checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
329checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
330-- cannot use 'self' opcode
331checkmessage(s.."; local t = {}; t:bbb()", "field 'bbb'")
332
333checkmessage([[aaa=9
334repeat until 3==3
335local x=math.sin(math.cos(3))
336if math.sin(1) == x then return math.sin(1) end -- tail call
337local a,b = 1, {
338 {x='a'..'b'..'c', y='b', z=x},
339 {1,2,3,4,5} or 3+3<=3+3,
340 3+1>3+1,
341 {d = x and aaa[x or y]}}
342]], "global 'aaa'")
343
344checkmessage([[
345local x,y = {},1
346if math.sin(1) == 0 then return 3 end -- return
347x.a()]], "field 'a'")
348
349checkmessage([[
350prefix = nil
351insert = nil
352while 1 do
353 local a
354 if nil then break end
355 insert(prefix, a)
356end]], "global 'insert'")
357
358checkmessage([[ -- tail call
359 return math.sin("a")
360]], "sin")
361
362checkmessage([[collectgarbage("nooption")]], "invalid option")
363
364checkmessage([[x = print .. "a"]], "concatenate")
365checkmessage([[x = "a" .. false]], "concatenate")
366checkmessage([[x = {} .. 2]], "concatenate")
367
368checkmessage("getmetatable(io.stdin).__gc()", "no value")
369
370checkmessage([[
371local Var
372local function main()
373 NoSuchName (function() Var=0 end)
374end
375main()
376]], "global 'NoSuchName'")
377print'+'
378
379aaa = {}; setmetatable(aaa, {__index = string})
380checkmessage("aaa:sub()", "bad self")
381checkmessage("string.sub('a', {})", "#2")
382checkmessage("('a'):sub{}", "#1")
383
384checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
385checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
386
387_G.aaa = nil
388
389
390-- tests for errors in coroutines
391
392local function f (n)
393 local c = coroutine.create(f)
394 local a,b = coroutine.resume(c)
395 return b
396end
397assert(string.find(f(), "C stack overflow"))
398
399checkmessage("coroutine.yield()", "outside a coroutine")
400
401f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end)
402checkerr("yield across", f)
403
404
405-- testing size of 'source' info; size of buffer for that info is
406-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
407local idsize = 60 - 1
408local function checksize (source)
409 -- syntax error
410 local _, msg = load("x", source)
411 msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':')
412 assert(msg:len() <= idsize)
413end
414
415for i = 60 - 10, 60 + 10 do -- check border cases around 60
416 checksize("@" .. string.rep("x", i)) -- file names
417 checksize(string.rep("x", i - 10)) -- string sources
418 checksize("=" .. string.rep("x", i)) -- exact sources
419end
420
421
422-- testing line error
423
424local function lineerror (s, l, w)
425 local err,msg = pcall(load(s))
426 local line = tonumber(string.match(msg, ":(%d+):"))
427 assert((line == l or (not line and not l)) and string.find(msg, w))
428end
429
430lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2, "limit")
431lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3, "to call")
432lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4, "to call")
433lineerror("function a.x.y ()\na=a+1\nend", 1, "index")
434
435lineerror("a = \na\n+\n{}", 3, "arithmetic")
436lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6, "arithmetic")
437lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3, "arithmetic")
438
439lineerror("a\n=\n-\n\nprint\n;", 3, "arithmetic")
440
441lineerror([[
442a
443( -- <<
44423)
445]], 2, "call")
446
447lineerror([[
448local a = {x = 13}
449a
450.
451x
452( -- <<
45323
454)
455]], 5, "call")
456
457lineerror([[
458local a = {x = 13}
459a
460.
461x
462(
46323 + a
464)
465]], 6, "arithmetic")
466
467local p = [[
468 function g() f() end
469 function f(x) error('a', XX) end
470g()
471]]
472XX=3;lineerror((p), 3, "a")
473XX=0;lineerror((p), false, "a")
474XX=1;lineerror((p), 2, "a")
475XX=2;lineerror((p), 1, "a")
476_G.XX, _G.g, _G.f = nil
477
478
479lineerror([[
480local b = false
481if not b then
482 error 'test'
483end]], 3, "test")
484
485lineerror([[
486local b = false
487if not b then
488 if not b then
489 if not b then
490 error 'test'
491 end
492 end
493end]], 5, "test")
494
495lineerror([[
496_ENV = 1
497global function foo ()
498 local a = 10
499 return a
500end
501]], 2, "index")
502
503
504-- bug in 5.4.0
505lineerror([[
506 local a = 0
507 local b = 1
508 local c = b % a
509]], 3, "perform")
510
511do
512 -- Force a negative estimate for base line. Error in instruction 2
513 -- (after VARARGPREP, GETGLOBAL), with first absolute line information
514 -- (forced by too many lines) in instruction 0.
515 local s = string.format("%s return __A.x", string.rep("\n", 300))
516 lineerror(s, 301, "index")
517end
518
519
520local function stxlineerror (s, l, w)
521 local err,msg = load(s)
522 local line = tonumber(string.match(msg, ":(%d+):"))
523 assert((line == l or (not line and not l)) and string.find(msg, w, 1, true))
524end
525
526stxlineerror([[
527::L1::
528::L1::
529]], 2, "already defined")
530
531stxlineerror([[
532global none
533local x = b
534]], 2, "not declared")
535
536stxlineerror([[
537local <close> a, b
538]], 1, "multiple")
539
540if not _soft then
541 -- several tests that exhaust the Lua stack
542 collectgarbage()
543 print"testing stack overflow"
544 local C = 0
545 -- get line where stack overflow will happen
546 local l = debug.getinfo(1, "l").currentline + 1
547 local function auxy () C=C+1; auxy() end -- produce a stack overflow
548 function YY ()
549 collectgarbage("stop") -- avoid running finalizers without stack space
550 auxy()
551 collectgarbage("restart")
552 end
553
554 local function checkstackmessage (m)
555 print("(expected stack overflow after " .. C .. " calls)")
556 C = 0 -- prepare next count
557 return (string.find(m, "stack overflow"))
558 end
559 -- repeated stack overflows (to check stack recovery)
560 assert(checkstackmessage(doit('YY()')))
561 assert(checkstackmessage(doit('YY()')))
562 assert(checkstackmessage(doit('YY()')))
563
564 _G.YY = nil
565
566
567 -- error lines in stack overflow
568 local l1
569 local function g(x)
570 l1 = debug.getinfo(x, "l").currentline + 2
571 collectgarbage("stop") -- avoid running finalizers without stack space
572 auxy()
573 collectgarbage("restart")
574 end
575 local _, stackmsg = xpcall(g, debug.traceback, 1)
576 print('+')
577 local stack = {}
578 for line in string.gmatch(stackmsg, "[^\n]*") do
579 local curr = string.match(line, ":(%d+):")
580 if curr then table.insert(stack, tonumber(curr)) end
581 end
582 local i=1
583 while stack[i] ~= l1 do
584 assert(stack[i] == l)
585 i = i+1
586 end
587 assert(i > 15)
588
589
590 -- error in error handling
591 local res, msg = xpcall(error, error)
592 assert(not res and msg == 'error in error handling')
593 print('+')
594
595 local function f (x)
596 if x==0 then error('a\n')
597 else
598 local aux = function () return f(x-1) end
599 local a,b = xpcall(aux, aux)
600 return a,b
601 end
602 end
603 f(3)
604
605 local function loop (x,y,z) return 1 + loop(x, y, z) end
606
607 local res, msg = xpcall(loop, function (m)
608 assert(string.find(m, "stack overflow"))
609 checkerr("error handling", loop)
610 assert(math.sin(0) == 0)
611 return 15
612 end)
613 assert(msg == 15)
614
615 local f = function ()
616 for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end
617 end
618 checkerr("too many results", f)
619
620end
621
622
623do -- errors in error handle that not necessarily go forever
624 local function err (n) -- function to be used as message handler
625 -- generate an error unless n is zero, so that there is a limited
626 -- loop of errors
627 if type(n) ~= "number" then -- some other error?
628 return n -- report it
629 elseif n == 0 then
630 return "END" -- that will be the final message
631 else error(n - 1) -- does the loop
632 end
633 end
634
635 local res, msg = xpcall(error, err, 170)
636 assert(not res and msg == "END")
637
638 -- too many levels
639 local res, msg = xpcall(error, err, 300)
640 assert(not res and msg == "C stack overflow")
641end
642
643
644do
645 -- non string messages
646 local t = {}
647 local res, msg = pcall(function () error(t) end)
648 assert(not res and msg == t)
649
650 res, msg = pcall(function () error(nil) end)
651 assert(not res and msg == "<no error object>")
652
653 local function f() error{msg='x'} end
654 res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
655 assert(msg.msg == 'xy')
656
657 -- 'assert' with extra arguments
658 res, msg = pcall(assert, false, "X", t)
659 assert(not res and msg == "X")
660
661 -- 'assert' with no message
662 res, msg = pcall(function () assert(false) end)
663 local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$")
664 assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2)
665
666 -- 'assert' with non-string messages
667 res, msg = pcall(assert, false, t)
668 assert(not res and msg == t)
669
670 res, msg = pcall(assert, nil, nil)
671 assert(not res and type(msg) == "string")
672
673 -- 'assert' without arguments
674 res, msg = pcall(assert)
675 assert(not res and string.find(msg, "value expected"))
676end
677
678-- xpcall with arguments
679local a, b, c = xpcall(string.find, error, "alo", "al")
680assert(a and b == 1 and c == 2)
681a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
682assert(not a and type(b) == "table" and c == nil)
683
684
685print("testing tokens in error messages")
686checksyntax("syntax error", "", "error", 1)
687checksyntax("1.000", "", "1.000", 1)
688checksyntax("[[a]]", "", "[[a]]", 1)
689checksyntax("'aa'", "", "'aa'", 1)
690checksyntax("while << do end", "", "<<", 1)
691checksyntax("for >> do end", "", ">>", 1)
692
693-- test invalid non-printable char in a chunk
694checksyntax("a\1a = 1", "", "<\\1>", 1)
695
696-- test 255 as first char in a chunk
697checksyntax("\255a = 1", "", "<\\255>", 1)
698
699doit('I = load("a=9+"); aaa=3')
700assert(_G.aaa==3 and not _G.I)
701_G.I,_G.aaa = nil
702print('+')
703
704local lim = 1000
705if _soft then lim = 100 end
706for i=1,lim do
707 doit('a = ')
708 doit('a = 4+nil')
709end
710
711
712-- testing syntax limits
713
714local function testrep (init, rep, close, repc, finalresult)
715 local function gencode (n)
716 return init .. string.rep(rep, n) .. close .. string.rep(repc, n)
717 end
718 local res, msg = load(gencode(100)) -- 100 levels is OK
719 assert(res)
720 if (finalresult) then
721 assert(res() == finalresult)
722 end
723 local res, msg = load(gencode(500)) -- 500 levels not ok
724 assert(not res and (string.find(msg, "too many") or
725 string.find(msg, "overflow")))
726end
727
728testrep("local a", ",a", ";", "") -- local variables
729testrep("local a", ",a", "= 1", ",1") -- local variables initialized
730testrep("local a", ",a", "= f()", "") -- local variables initialized
731testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
732testrep("local a; a=", "{", "0", "}") -- constructors
733testrep("return ", "(", "2", ")", 2) -- parentheses
734-- nested calls (a(a(a(a(...)))))
735testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2)
736testrep("", "do ", "", " end")
737testrep("", "while a do ", "", " end")
738testrep("local a; ", "if a then else ", "", " end")
739testrep("", "function foo () ", "", " end")
740testrep("local a = ''; return ", "a..", "'a'", "", "a")
741testrep("local a = 1; return ", "a^", "a", "", 1)
742
743checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
744
745
746-- testing other limits
747
748-- upvalues
749local lim = 127
750local s = "local function fooA ()\n local "
751for j = 1,lim do
752 s = s.."a"..j..", "
753end
754s = s.."b,c\n"
755s = s.."local function fooB ()\n local "
756for j = 1,lim do
757 s = s.."b"..j..", "
758end
759s = s.."b\n"
760s = s.."function fooC () return b+c"
761local c = 1+2
762for j = 1,lim do
763 s = s.."+a"..j.."+b"..j
764 c = c + 2
765end
766s = s.."\nend end end"
767local a,b = load(s)
768assert(c > 255 and string.find(b, "too many upvalues") and
769 string.find(b, "line 5"))
770
771-- local variables
772s = "\nfunction foo ()\n local "
773for j = 1,200 do
774 s = s.."a"..j..", "
775end
776s = s.."b\n"
777local a,b = load(s)
778assert(string.find(b, "line 2") and string.find(b, "too many local variables"))
779
780mt.__index = oldmm
781
782print('OK')