lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/goto.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | global<const> require |
| 5 | global<const> print, load, assert, string, setmetatable |
| 6 | global<const> collectgarbage, error |
| 7 | |
| 8 | print("testing goto and global declarations") |
| 9 | |
| 10 | collectgarbage() |
| 11 | |
| 12 | local function errmsg (code, m) |
| 13 | local st, msg = load(code) |
| 14 | assert(not st and string.find(msg, m)) |
| 15 | end |
| 16 | |
| 17 | -- cannot see label inside block |
| 18 | errmsg([[ goto l1; do ::l1:: end ]], "label 'l1'") |
| 19 | errmsg([[ do ::l1:: end goto l1; ]], "label 'l1'") |
| 20 | |
| 21 | -- repeated label |
| 22 | errmsg([[ ::l1:: ::l1:: ]], "label 'l1'") |
| 23 | errmsg([[ ::l1:: do ::l1:: end]], "label 'l1'") |
| 24 | |
| 25 | |
| 26 | |
| 27 | -- jumping over variable declaration |
| 28 | errmsg([[ goto l1; local aa ::l1:: ::l2:: print(3) ]], "scope of 'aa'") |
| 29 | |
| 30 | errmsg([[ goto l2; global *; ::l1:: ::l2:: print(3) ]], "scope of '*'") |
| 31 | |
| 32 | errmsg([[ |
| 33 | do local bb, cc; goto l1; end |
| 34 | local aa |
| 35 | ::l1:: print(3) |
| 36 | ]], "scope of 'aa'") |
| 37 | |
| 38 | |
| 39 | -- jumping into a block |
| 40 | errmsg([[ do ::l1:: end goto l1 ]], "label 'l1'") |
| 41 | errmsg([[ goto l1 do ::l1:: end ]], "label 'l1'") |
| 42 | |
| 43 | -- cannot continue a repeat-until with variables |
| 44 | errmsg([[ |
| 45 | repeat |
| 46 | if x then goto cont end |
| 47 | local xuxu = 10 |
| 48 | ::cont:: |
| 49 | until xuxu < x |
| 50 | ]], "scope of 'xuxu'") |
| 51 | |
| 52 | -- simple gotos |
| 53 | local x |
| 54 | do |
| 55 | local y = 12 |
| 56 | goto l1 |
| 57 | ::l2:: x = x + 1; goto l3 |
| 58 | ::l1:: x = y; goto l2 |
| 59 | end |
| 60 | ::l3:: ::l3_1:: assert(x == 13) |
| 61 | |
| 62 | |
| 63 | -- long labels |
| 64 | do |
| 65 | local prog = [[ |
| 66 | do |
| 67 | local a = 1 |
| 68 | goto l%sa; a = a + 1 |
| 69 | ::l%sa:: a = a + 10 |
| 70 | goto l%sb; a = a + 2 |
| 71 | ::l%sb:: a = a + 20 |
| 72 | return a |
| 73 | end |
| 74 | ]] |
| 75 | local label = string.rep("0123456789", 40) |
| 76 | prog = string.format(prog, label, label, label, label) |
| 77 | assert(assert(load(prog))() == 31) |
| 78 | end |
| 79 | |
| 80 | |
| 81 | -- ok to jump over local dec. to end of block |
| 82 | do |
| 83 | goto l1 |
| 84 | local a = 23 |
| 85 | x = a |
| 86 | ::l1::; |
| 87 | end |
| 88 | |
| 89 | while true do |
| 90 | goto l4 |
| 91 | goto l1 -- ok to jump over local dec. to end of block |
| 92 | goto l1 -- multiple uses of same label |
| 93 | local x = 45 |
| 94 | ::l1:: ;;; |
| 95 | end |
| 96 | ::l4:: assert(x == 13) |
| 97 | |
| 98 | if print then |
| 99 | goto l1 -- ok to jump over local dec. to end of block |
| 100 | error("should not be here") |
| 101 | goto l2 -- ok to jump over local dec. to end of block |
| 102 | local x |
| 103 | ::l1:: ; ::l2:: ;; |
| 104 | else end |
| 105 | |
| 106 | -- to repeat a label in a different function is OK |
| 107 | local function foo () |
| 108 | local a = {} |
| 109 | goto l3 |
| 110 | ::l1:: a[#a + 1] = 1; goto l2; |
| 111 | ::l2:: a[#a + 1] = 2; goto l5; |
| 112 | ::l3:: |
| 113 | ::l3a:: a[#a + 1] = 3; goto l1; |
| 114 | ::l4:: a[#a + 1] = 4; goto l6; |
| 115 | ::l5:: a[#a + 1] = 5; goto l4; |
| 116 | ::l6:: assert(a[1] == 3 and a[2] == 1 and a[3] == 2 and |
| 117 | a[4] == 5 and a[5] == 4) |
| 118 | if not a[6] then a[6] = true; goto l3a end -- do it twice |
| 119 | end |
| 120 | |
| 121 | ::l6:: foo() |
| 122 | |
| 123 | |
| 124 | do -- bug in 5.2 -> 5.3.2 |
| 125 | local x |
| 126 | ::L1:: |
| 127 | local y -- cannot join this SETNIL with previous one |
| 128 | assert(y == nil) |
| 129 | y = true |
| 130 | if x == nil then |
| 131 | x = 1 |
| 132 | goto L1 |
| 133 | else |
| 134 | x = x + 1 |
| 135 | end |
| 136 | assert(x == 2 and y == true) |
| 137 | end |
| 138 | |
| 139 | -- bug in 5.3 |
| 140 | do |
| 141 | local first = true |
| 142 | local a = false |
| 143 | if true then |
| 144 | goto LBL |
| 145 | ::loop:: |
| 146 | a = true |
| 147 | ::LBL:: |
| 148 | if first then |
| 149 | first = false |
| 150 | goto loop |
| 151 | end |
| 152 | end |
| 153 | assert(a) |
| 154 | end |
| 155 | |
| 156 | do -- compiling infinite loops |
| 157 | goto escape -- do not run the infinite loops |
| 158 | ::a:: goto a |
| 159 | ::b:: goto c |
| 160 | ::c:: goto b |
| 161 | end |
| 162 | ::escape:: |
| 163 | -------------------------------------------------------------------------------- |
| 164 | -- testing closing of upvalues |
| 165 | |
| 166 | local debug = require 'debug' |
| 167 | |
| 168 | local function foo () |
| 169 | local t = {} |
| 170 | do |
| 171 | local i = 1 |
| 172 | local a, b, c, d |
| 173 | t[1] = function () return a, b, c, d end |
| 174 | ::l1:: |
| 175 | local b |
| 176 | do |
| 177 | local c |
| 178 | t[#t + 1] = function () return a, b, c, d end -- t[2], t[4], t[6] |
| 179 | if i > 2 then goto l2 end |
| 180 | do |
| 181 | local d |
| 182 | t[#t + 1] = function () return a, b, c, d end -- t[3], t[5] |
| 183 | i = i + 1 |
| 184 | local a |
| 185 | goto l1 |
| 186 | end |
| 187 | end |
| 188 | end |
| 189 | ::l2:: return t |
| 190 | end |
| 191 | |
| 192 | local a = foo() |
| 193 | assert(#a == 6) |
| 194 | |
| 195 | -- all functions share same 'a' |
| 196 | for i = 2, 6 do |
| 197 | assert(debug.upvalueid(a[1], 1) == debug.upvalueid(a[i], 1)) |
| 198 | end |
| 199 | |
| 200 | -- 'b' and 'c' are shared among some of them |
| 201 | for i = 2, 6 do |
| 202 | -- only a[1] uses external 'b'/'b' |
| 203 | assert(debug.upvalueid(a[1], 2) ~= debug.upvalueid(a[i], 2)) |
| 204 | assert(debug.upvalueid(a[1], 3) ~= debug.upvalueid(a[i], 3)) |
| 205 | end |
| 206 | |
| 207 | for i = 3, 5, 2 do |
| 208 | -- inner functions share 'b'/'c' with previous ones |
| 209 | assert(debug.upvalueid(a[i], 2) == debug.upvalueid(a[i - 1], 2)) |
| 210 | assert(debug.upvalueid(a[i], 3) == debug.upvalueid(a[i - 1], 3)) |
| 211 | -- but not with next ones |
| 212 | assert(debug.upvalueid(a[i], 2) ~= debug.upvalueid(a[i + 1], 2)) |
| 213 | assert(debug.upvalueid(a[i], 3) ~= debug.upvalueid(a[i + 1], 3)) |
| 214 | end |
| 215 | |
| 216 | -- only external 'd' is shared |
| 217 | for i = 2, 6, 2 do |
| 218 | assert(debug.upvalueid(a[1], 4) == debug.upvalueid(a[i], 4)) |
| 219 | end |
| 220 | |
| 221 | -- internal 'd's are all different |
| 222 | for i = 3, 5, 2 do |
| 223 | for j = 1, 6 do |
| 224 | assert((debug.upvalueid(a[i], 4) == debug.upvalueid(a[j], 4)) |
| 225 | == (i == j)) |
| 226 | end |
| 227 | end |
| 228 | |
| 229 | -------------------------------------------------------------------------------- |
| 230 | -- testing if x goto optimizations |
| 231 | |
| 232 | local function testG (a) |
| 233 | if a == 1 then |
| 234 | goto l1 |
| 235 | error("should never be here!") |
| 236 | elseif a == 2 then goto l2 |
| 237 | elseif a == 3 then goto l3 |
| 238 | elseif a == 4 then |
| 239 | goto l1 -- go to inside the block |
| 240 | error("should never be here!") |
| 241 | ::l1:: a = a + 1 -- must go to 'if' end |
| 242 | else |
| 243 | goto l4 |
| 244 | ::l4a:: a = a * 2; goto l4b |
| 245 | error("should never be here!") |
| 246 | ::l4:: goto l4a |
| 247 | error("should never be here!") |
| 248 | ::l4b:: |
| 249 | end |
| 250 | do return a end |
| 251 | ::l2:: do return "2" end |
| 252 | ::l3:: do return "3" end |
| 253 | ::l1:: return "1" |
| 254 | end |
| 255 | |
| 256 | assert(testG(1) == "1") |
| 257 | assert(testG(2) == "2") |
| 258 | assert(testG(3) == "3") |
| 259 | assert(testG(4) == 5) |
| 260 | assert(testG(5) == 10) |
| 261 | |
| 262 | do -- test goto's around to-be-closed variable |
| 263 | |
| 264 | global * |
| 265 | |
| 266 | -- set 'var' and return an object that will reset 'var' when |
| 267 | -- it goes out of scope |
| 268 | local function newobj (var) |
| 269 | _ENV[var] = true |
| 270 | return setmetatable({}, {__close = function () |
| 271 | _ENV[var] = nil |
| 272 | end}) |
| 273 | end |
| 274 | |
| 275 | goto L1 |
| 276 | |
| 277 | ::L4:: assert(not varX); goto L5 -- varX dead here |
| 278 | |
| 279 | ::L1:: |
| 280 | local varX <close> = newobj("X") |
| 281 | assert(varX); goto L2 -- varX alive here |
| 282 | |
| 283 | ::L3:: |
| 284 | assert(varX); goto L4 -- varX alive here |
| 285 | |
| 286 | ::L2:: assert(varX); goto L3 -- varX alive here |
| 287 | |
| 288 | ::L5:: -- return |
| 289 | end |
| 290 | |
| 291 | |
| 292 | |
| 293 | foo() |
| 294 | -------------------------------------------------------------------------- |
| 295 | |
| 296 | -- check for compilation errors |
| 297 | local function checkerr (code, err) |
| 298 | local st, msg = load(code) |
| 299 | assert(not st and string.find(msg, err)) |
| 300 | end |
| 301 | |
| 302 | do |
| 303 | global T<const> |
| 304 | |
| 305 | -- globals must be declared, after a global declaration |
| 306 | checkerr("global none; X = 1", "variable 'X'") |
| 307 | checkerr("global none; function XX() end", "variable 'XX'") |
| 308 | |
| 309 | -- global variables cannot be to-be-closed |
| 310 | checkerr("global X<close>", "cannot be") |
| 311 | checkerr("global <close> *", "cannot be") |
| 312 | |
| 313 | do |
| 314 | local X = 10 |
| 315 | do global X; X = 20 end |
| 316 | assert(X == 10) -- local X |
| 317 | end |
| 318 | assert(_ENV.X == 20) -- global X |
| 319 | |
| 320 | -- '_ENV' cannot be global |
| 321 | checkerr("global _ENV, a; a = 10", "variable 'a'") |
| 322 | |
| 323 | -- global declarations inside functions |
| 324 | checkerr([[ |
| 325 | global none |
| 326 | local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'") |
| 327 | |
| 328 | if not T then -- when not in "test mode", "global" isn't reserved |
| 329 | assert(load("global = 1; return global")() == 1) |
| 330 | print " ('global' is not a reserved word)" |
| 331 | else |
| 332 | -- "global" reserved, cannot be used as a variable |
| 333 | assert(not load("global = 1; return global")) |
| 334 | end |
| 335 | |
| 336 | local foo = 20 |
| 337 | do |
| 338 | global function foo (x) |
| 339 | if x == 0 then return 1 else return 2 * foo(x - 1) end |
| 340 | end |
| 341 | assert(foo == _ENV.foo and foo(4) == 16) |
| 342 | end |
| 343 | assert(_ENV.foo(4) == 16) |
| 344 | assert(foo == 20) -- local one is in context here |
| 345 | |
| 346 | do |
| 347 | global foo; |
| 348 | function foo (x) return end -- Ok after declaration |
| 349 | end |
| 350 | |
| 351 | checkerr([[ |
| 352 | global<const> foo; |
| 353 | function foo (x) return end -- ERROR: foo is read-only |
| 354 | ]], "assign to const variable 'foo'") |
| 355 | |
| 356 | checkerr([[ |
| 357 | global foo <const>; |
| 358 | function foo (x) -- ERROR: foo is read-only |
| 359 | return |
| 360 | end |
| 361 | ]], "%:2%:") -- correct line in error message |
| 362 | |
| 363 | checkerr([[ |
| 364 | global<const> *; |
| 365 | print(X) -- Ok to use |
| 366 | Y = 1 -- ERROR |
| 367 | ]], "assign to const variable 'Y'") |
| 368 | |
| 369 | checkerr([[ |
| 370 | global *; |
| 371 | Y = X -- Ok to use |
| 372 | global<const> *; |
| 373 | Y = 1 -- ERROR |
| 374 | ]], "assign to const variable 'Y'") |
| 375 | |
| 376 | global * |
| 377 | Y = 10 |
| 378 | assert(_ENV.Y == 10) |
| 379 | global<const> * |
| 380 | local x = Y |
| 381 | global * |
| 382 | Y = x + Y |
| 383 | assert(_ENV.Y == 20) |
| 384 | Y = nil |
| 385 | end |
| 386 | |
| 387 | |
| 388 | do -- Ok to declare hundreds of globals |
| 389 | global table |
| 390 | local code = {} |
| 391 | for i = 1, 1000 do |
| 392 | code[#code + 1] = ";global x" .. i |
| 393 | end |
| 394 | code[#code + 1] = "; return x990" |
| 395 | code = table.concat(code) |
| 396 | _ENV.x990 = 11 |
| 397 | assert(load(code)() == 11) |
| 398 | _ENV.x990 = nil |
| 399 | end |
| 400 | |
| 401 | do -- mixing lots of global/local declarations |
| 402 | global table |
| 403 | local code = {} |
| 404 | for i = 1, 200 do |
| 405 | code[#code + 1] = ";global x" .. i |
| 406 | code[#code + 1] = ";local y" .. i .. "=" .. (2*i) |
| 407 | end |
| 408 | code[#code + 1] = "; return x200 + y200" |
| 409 | code = table.concat(code) |
| 410 | _ENV.x200 = 11 |
| 411 | assert(assert(load(code))() == 2*200 + 11) |
| 412 | _ENV.x200 = nil |
| 413 | end |
| 414 | |
| 415 | do print "testing initialization in global declarations" |
| 416 | global<const> a, b, c = 10, 20, 30 |
| 417 | assert(_ENV.a == 10 and b == 20 and c == 30) |
| 418 | _ENV.a = nil; _ENV.b = nil; _ENV.c = nil; |
| 419 | |
| 420 | global<const> a, b, c = 10 |
| 421 | assert(_ENV.a == 10 and b == nil and c == nil) |
| 422 | _ENV.a = nil; _ENV.b = nil; _ENV.c = nil; |
| 423 | |
| 424 | global table |
| 425 | global a, b, c, d = table.unpack{1, 2, 3, 6, 5} |
| 426 | assert(_ENV.a == 1 and b == 2 and c == 3 and d == 6) |
| 427 | a = nil; b = nil; c = nil; d = nil |
| 428 | |
| 429 | local a, b = 100, 200 |
| 430 | do |
| 431 | global a, b = a, b |
| 432 | end |
| 433 | assert(_ENV.a == 100 and _ENV.b == 200) |
| 434 | _ENV.a = nil; _ENV.b = nil |
| 435 | |
| 436 | |
| 437 | assert(_ENV.a == nil and _ENV.b == nil and _ENV.c == nil and _ENV.d == nil) |
| 438 | end |
| 439 | |
| 440 | do |
| 441 | global table, string |
| 442 | -- global initialization when names don't fit in K |
| 443 | |
| 444 | -- to fill constant table |
| 445 | local code = {} |
| 446 | for i = 1, 300 do code[i] = "'" .. i .. "'" end |
| 447 | code = table.concat(code, ",") |
| 448 | code = string.format([[ |
| 449 | return function (_ENV) |
| 450 | local dummy = {%s} -- fill initial positions in constant table, |
| 451 | -- so that initialization must use registers for global names |
| 452 | global a, b, c = 10, 20, 30 |
| 453 | end]], code) |
| 454 | |
| 455 | local fun = assert(load(code))() |
| 456 | |
| 457 | local env = {} |
| 458 | fun(env) |
| 459 | assert(env.a == 10 and env.b == 20 and env.c == 30) |
| 460 | end |
| 461 | |
| 462 | |
| 463 | do -- testing global redefinitions |
| 464 | -- cannot use 'checkerr' as errors are not compile time |
| 465 | global pcall |
| 466 | local f = assert(load("global print = 10")) |
| 467 | local st, msg = pcall(f) |
| 468 | assert(string.find(msg, "global 'print' already defined")) |
| 469 | |
| 470 | local f = assert(load("local _ENV = {AA = false}; global AA = 10")) |
| 471 | local st, msg = pcall(f) |
| 472 | assert(string.find(msg, "global 'AA' already defined")) |
| 473 | |
| 474 | end |
| 475 | |
| 476 | print'OK' |
| 477 |