Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/lua/blob/master/testes/errors.lua # lua [Ported from github.com/lua/lua] Cast in 'rl_readline_name' Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 4 print("testing errors") 5 6 local debug = require"debug" 7 8 -- avoid problems with 'strict' module (which may generate other error messages) 9 local mt = getmetatable(_G) or {} 10 local oldmm = mt.__index 11 mt.__index = nil 12 13 local function checkerr (msg, f, ...) 14 local st, err = pcall(f, ...) 15 assert(not st and string.find(err, msg)) 16 end 17 18 19 local 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 24 end 25 26 27 local 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)) 31 end 32 33 local 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)) 42 end 43 44 45 -- test error message with no extra info 46 assert(doit("error('hi', 0)") == 'hi') 47 48 -- test nil error message 49 assert(doit("error()") == "") 50 51 52 -- test common errors/errors that crashed in the past 53 assert(doit("table.unpack({}, 1, n=2^30)")) 54 assert(doit("a=math.sin()")) 55 assert(not doit("tostring(1)") and doit("tostring()")) 56 assert(doit"tonumber()") 57 assert(doit"repeat until 1; a") 58 assert(doit"return;;") 59 assert(doit"assert(false)") 60 assert(doit"assert(nil)") 61 assert(doit("function a (... , ...) end")) 62 assert(doit("function a (, ...) end")) 63 assert(doit("local t={}; t = t[#t] + 1")) 64 65 checksyntax([[ 66 local a = {4 67 68 ]], "'}' expected (to close '{' at line 1)", "", 3) 69 70 71 do -- 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 89 end 90 91 92 if not T then 93 (Message or print) 94 ('\n >>> testC not active: skipping tests for messages in C <<<\n') 95 else 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 145 end 146 147 148 -- tests for better error messages 149 150 checkmessage("a = {} + 1", "arithmetic") 151 checkmessage("a = {} | 1", "bitwise operation") 152 checkmessage("a = {} < 1", "attempt to compare") 153 checkmessage("a = {} <= 1", "attempt to compare") 154 155 checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'") 156 checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'") 157 checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'") 158 assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'")) 159 checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number") 160 checkmessage("aaa=(1)..{}", "a table value") 161 162 checkmessage("local function foo (...t) return t.xx + 1 end; foo()", 163 "field 'xx'") 164 165 -- bug in 5.4.6 166 checkmessage("a = {_ENV = {}}; print(a._ENV.x + 1)", "field 'x'") 167 168 -- a similar bug, since 5.4.0 169 checkmessage("print(('_ENV').x + 1)", "field 'x'") 170 171 _G.aaa, _G.bbbb = nil 172 173 -- calls 174 checkmessage("local a; a(13)", "local 'a'") 175 checkmessage([[ 176 local a = setmetatable({}, {__add = 34}) 177 a = a + 1 178 ]], "metamethod 'add'") 179 checkmessage([[ 180 local a = setmetatable({}, {__lt = {}}) 181 a = a > a 182 ]], "metamethod 'lt'") 183 184 -- tail calls 185 checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'") 186 checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'") 187 188 checkmessage("aaa = #print", "length of a function value") 189 checkmessage("aaa = #3", "length of a number value") 190 191 _G.aaa = nil 192 193 checkmessage("aaa.bbb:ddd(9)", "global 'aaa'") 194 checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'") 195 checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'") 196 checkmessage("local a,b,c; (function () a = b+1.1 end)()", "upvalue 'b'") 197 assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)") 198 199 -- upvalues being indexed do not go to the stack 200 checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'") 201 checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'") 202 203 checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'") 204 205 checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'") 206 checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'") 207 checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'") 208 checkmessage("aaa={}; x=-aaa", "global 'aaa'") 209 210 -- short circuit 211 checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)", 212 "local 'bbbb'") 213 checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)", 214 "local 'bbbb'") 215 checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'") 216 checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value") 217 assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'")) 218 assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'")) 219 220 checkmessage("print(print < 10)", "function with number") 221 checkmessage("print(print < print)", "two function values") 222 checkmessage("print('10' < 10)", "string with number") 223 checkmessage("print(10 < '23')", "number with string") 224 225 -- float->integer conversions 226 checkmessage("local a = 2.0^100; x = a << 2", "local a") 227 checkmessage("local a = 1 >> 2.0^100", "has no integer representation") 228 checkmessage("local a = 10.1 << 2.0^100", "has no integer representation") 229 checkmessage("local a = 2.0^100 & 1", "has no integer representation") 230 checkmessage("local a = 2.0^100 & 1e100", "has no integer representation") 231 checkmessage("local a = 2.0 | 1e40", "has no integer representation") 232 checkmessage("local a = 2e100 ~ 1", "has no integer representation") 233 checkmessage("string.sub('a', 2.0^100)", "has no integer representation") 234 checkmessage("string.rep('a', 3.3)", "has no integer representation") 235 checkmessage("return 6e40 & 7", "has no integer representation") 236 checkmessage("return 34 << 7e30", "has no integer representation") 237 checkmessage("return ~-3e40", "has no integer representation") 238 checkmessage("return ~-3.009", "has no integer representation") 239 checkmessage("return 3.009 & 1", "has no integer representation") 240 checkmessage("return 34 >> {}", "table value") 241 checkmessage("aaa = 24 // 0", "divide by zero") 242 checkmessage("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. 248 checkmessage("local a = setmetatable({}, {__index = 10}).x", 249 "attempt to index a number value") 250 251 252 -- numeric for loops 253 checkmessage("for i = {}, 10 do end", "table") 254 checkmessage("for i = io.stdin, 10 do end", "FILE") 255 checkmessage("for i = {}, 10 do end", "initial value") 256 checkmessage("for i = 1, 'x', 10 do end", "string") 257 checkmessage("for i = 1, {}, 10 do end", "limit") 258 checkmessage("for i = 1, {} do end", "limit") 259 checkmessage("for i = 1, 10, print do end", "step") 260 checkmessage("for i = 1, 10, print do end", "function") 261 262 -- passing light userdata instead of full userdata 263 _G.D = debug 264 checkmessage([[ 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 271 do -- 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 298 end 299 300 -- global functions 301 checkmessage("(io.write or print){}", "io.write") 302 checkmessage("(collectgarbage or print){}", "collectgarbage") 303 304 -- errors in functions without debug info 305 do 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 A3+1, 341 {d = x and aaa[x or y]}} 342 ]], "global 'aaa'") 343 344 checkmessage([[ 345 local x,y = {},1 346 if math.sin(1) == 0 then return 3 end -- return 347 x.a()]], "field 'a'") 348 349 checkmessage([[ 350 prefix = nil 351 insert = nil 352 while 1 do 353 local a 354 if nil then break end 355 insert(prefix, a) 356 end]], "global 'insert'") 357 358 checkmessage([[ -- tail call 359 return math.sin("a") 360 ]], "sin") 361 362 checkmessage([[collectgarbage("nooption")]], "invalid option") 363 364 checkmessage([[x = print .. "a"]], "concatenate") 365 checkmessage([[x = "a" .. false]], "concatenate") 366 checkmessage([[x = {} .. 2]], "concatenate") 367 368 checkmessage("getmetatable(io.stdin).__gc()", "no value") 369 370 checkmessage([[ 371 local Var 372 local function main() 373 NoSuchName (function() Var=0 end) 374 end 375 main() 376 ]], "global 'NoSuchName'") 377 print'+' 378 379 aaa = {}; setmetatable(aaa, {__index = string}) 380 checkmessage("aaa:sub()", "bad self") 381 checkmessage("string.sub('a', {})", "#2") 382 checkmessage("('a'):sub{}", "#1") 383 384 checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'") 385 checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'") 386 387 _G.aaa = nil 388 389 390 -- tests for errors in coroutines 391 392 local function f (n) 393 local c = coroutine.create(f) 394 local a,b = coroutine.resume(c) 395 return b 396 end 397 assert(string.find(f(), "C stack overflow")) 398 399 checkmessage("coroutine.yield()", "outside a coroutine") 400 401 f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end) 402 checkerr("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'. 407 local idsize = 60 - 1 408 local 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) 413 end 414 415 for 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 419 end 420 421 422 -- testing line error 423 424 local 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)) 428 end 429 430 lineerror("local a\n for i=1,'a' do \n print(i) \n end", 2, "limit") 431 lineerror("\n local a \n for k,v in 3 \n do \n print(k) \n end", 3, "to call") 432 lineerror("\n\n for k,v in \n 3 \n do \n print(k) \n end", 4, "to call") 433 lineerror("function a.x.y ()\na=a+1\nend", 1, "index") 434 435 lineerror("a = \na\n+\n{}", 3, "arithmetic") 436 lineerror("a = \n3\n+\n(\n4\n/\nprint)", 6, "arithmetic") 437 lineerror("a = \nprint\n+\n(\n4\n/\n7)", 3, "arithmetic") 438 439 lineerror("a\n=\n-\n\nprint\n;", 3, "arithmetic") 440 441 lineerror([[ 442 a 443 ( -- << 444 23) 445 ]], 2, "call") 446 447 lineerror([[ 448 local a = {x = 13} 449 a 450 . 451 x 452 ( -- << 453 23 454 ) 455 ]], 5, "call") 456 457 lineerror([[ 458 local a = {x = 13} 459 a 460 . 461 x 462 ( 463 23 + a 464 ) 465 ]], 6, "arithmetic") 466 467 local p = [[ 468 function g() f() end 469 function f(x) error('a', XX) end 470 g() 471 ]] 472 XX=3;lineerror((p), 3, "a") 473 XX=0;lineerror((p), false, "a") 474 XX=1;lineerror((p), 2, "a") 475 XX=2;lineerror((p), 1, "a") 476 _G.XX, _G.g, _G.f = nil 477 478 479 lineerror([[ 480 local b = false 481 if not b then 482 error 'test' 483 end]], 3, "test") 484 485 lineerror([[ 486 local b = false 487 if not b then 488 if not b then 489 if not b then 490 error 'test' 491 end 492 end 493 end]], 5, "test") 494 495 lineerror([[ 496 _ENV = 1 497 global function foo () 498 local a = 10 499 return a 500 end 501 ]], 2, "index") 502 503 504 -- bug in 5.4.0 505 lineerror([[ 506 local a = 0 507 local b = 1 508 local c = b % a 509 ]], 3, "perform") 510 511 do 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") 517 end 518 519 520 local 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)) 524 end 525 526 stxlineerror([[ 527 ::L1:: 528 ::L1:: 529 ]], 2, "already defined") 530 531 stxlineerror([[ 532 global none 533 local x = b 534 ]], 2, "not declared") 535 536 stxlineerror([[ 537 local a, b 538 ]], 1, "multiple") 539 540 if 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 620 end 621 622 623 do -- 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") 641 end 642 643 644 do 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 == "") 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")) 676 end 677 678 -- xpcall with arguments 679 local a, b, c = xpcall(string.find, error, "alo", "al") 680 assert(a and b == 1 and c == 2) 681 a, b, c = xpcall(string.find, function (x) return {} end, true, "al") 682 assert(not a and type(b) == "table" and c == nil) 683 684 685 print("testing tokens in error messages") 686 checksyntax("syntax error", "", "error", 1) 687 checksyntax("1.000", "", "1.000", 1) 688 checksyntax("[[a]]", "", "[[a]]", 1) 689 checksyntax("'aa'", "", "'aa'", 1) 690 checksyntax("while << do end", "", "<<", 1) 691 checksyntax("for >> do end", "", ">>", 1) 692 693 -- test invalid non-printable char in a chunk 694 checksyntax("a\1a = 1", "", "<\\1>", 1) 695 696 -- test 255 as first char in a chunk 697 checksyntax("\255a = 1", "", "<\\255>", 1) 698 699 doit('I = load("a=9+"); aaa=3') 700 assert(_G.aaa==3 and not _G.I) 701 _G.I,_G.aaa = nil 702 print('+') 703 704 local lim = 1000 705 if _soft then lim = 100 end 706 for i=1,lim do 707 doit('a = ') 708 doit('a = 4+nil') 709 end 710 711 712 -- testing syntax limits 713 714 local 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"))) 726 end 727 728 testrep("local a", ",a", ";", "") -- local variables 729 testrep("local a", ",a", "= 1", ",1") -- local variables initialized 730 testrep("local a", ",a", "= f()", "") -- local variables initialized 731 testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment 732 testrep("local a; a=", "{", "0", "}") -- constructors 733 testrep("return ", "(", "2", ")", 2) -- parentheses 734 -- nested calls (a(a(a(a(...))))) 735 testrep("local function a (x) return x end; return ", "a(", "2.2", ")", 2.2) 736 testrep("", "do ", "", " end") 737 testrep("", "while a do ", "", " end") 738 testrep("local a; ", "if a then else ", "", " end") 739 testrep("", "function foo () ", "", " end") 740 testrep("local a = ''; return ", "a..", "'a'", "", "a") 741 testrep("local a = 1; return ", "a^", "a", "", 1) 742 743 checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers") 744 745 746 -- testing other limits 747 748 -- upvalues 749 local lim = 127 750 local s = "local function fooA ()\n local " 751 for j = 1,lim do 752 s = s.."a"..j..", " 753 end 754 s = s.."b,c\n" 755 s = s.."local function fooB ()\n local " 756 for j = 1,lim do 757 s = s.."b"..j..", " 758 end 759 s = s.."b\n" 760 s = s.."function fooC () return b+c" 761 local c = 1+2 762 for j = 1,lim do 763 s = s.."+a"..j.."+b"..j 764 c = c + 2 765 end 766 s = s.."\nend end end" 767 local a,b = load(s) 768 assert(c > 255 and string.find(b, "too many upvalues") and 769 string.find(b, "line 5")) 770 771 -- local variables 772 s = "\nfunction foo ()\n local " 773 for j = 1,200 do 774 s = s.."a"..j..", " 775 end 776 s = s.."b\n" 777 local a,b = load(s) 778 assert(string.find(b, "line 2") and string.find(b, "too many local variables")) 779 780 mt.__index = oldmm 781 782 print('OK')