github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/testdata/lua-5.3.3-tests/locals.lua (about) 1 -- $Id: locals.lua,v 1.36 2015/03/04 13:09:38 roberto Exp $ 2 3 print('testing local variables and environments') 4 5 local debug = require"debug" 6 7 8 -- bug in 5.1: 9 10 local function f(x) x = nil; return x end 11 assert(f(10) == nil) 12 13 local function f() local x; return x end 14 assert(f(10) == nil) 15 16 local function f(x) x = nil; local y; return x, y end 17 assert(f(10) == nil and select(2, f(20)) == nil) 18 19 do 20 local i = 10 21 do local i = 100; assert(i==100) end 22 do local i = 1000; assert(i==1000) end 23 assert(i == 10) 24 if i ~= 10 then 25 local i = 20 26 else 27 local i = 30 28 assert(i == 30) 29 end 30 end 31 32 33 34 f = nil 35 36 local f 37 x = 1 38 39 a = nil 40 load('local a = {}')() 41 assert(a == nil) 42 43 function f (a) 44 local _1, _2, _3, _4, _5 45 local _6, _7, _8, _9, _10 46 local x = 3 47 local b = a 48 local c,d = a,b 49 if (d == b) then 50 local x = 'q' 51 x = b 52 assert(x == 2) 53 else 54 assert(nil) 55 end 56 assert(x == 3) 57 local f = 10 58 end 59 60 local b=10 61 local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3 62 63 64 assert(x == 1) 65 66 f(2) 67 assert(type(f) == 'function') 68 69 70 local function getenv (f) 71 local a,b = debug.getupvalue(f, 1) 72 assert(a == '_ENV') 73 return b 74 end 75 76 -- test for global table of loaded chunks 77 assert(getenv(load"a=3") == _G) 78 local c = {}; local f = load("a = 3", nil, nil, c) 79 assert(getenv(f) == c) 80 assert(c.a == nil) 81 f() 82 assert(c.a == 3) 83 84 -- old test for limits for special instructions (now just a generic test) 85 do 86 local i = 2 87 local p = 4 -- p == 2^i 88 repeat 89 for j=-3,3 do 90 assert(load(string.format([[local a=%s; 91 a=a+%s; 92 assert(a ==2^%s)]], j, p-j, i), '')) () 93 assert(load(string.format([[local a=%s; 94 a=a-%s; 95 assert(a==-2^%s)]], -j, p-j, i), '')) () 96 assert(load(string.format([[local a,b=0,%s; 97 a=b-%s; 98 assert(a==-2^%s)]], -j, p-j, i), '')) () 99 end 100 p = 2 * p; i = i + 1 101 until p <= 0 102 end 103 104 print'+' 105 106 107 if rawget(_G, "querytab") then 108 -- testing clearing of dead elements from tables 109 collectgarbage("stop") -- stop GC 110 local a = {[{}] = 4, [3] = 0, alo = 1, 111 a1234567890123456789012345678901234567890 = 10} 112 113 local t = querytab(a) 114 115 for k,_ in pairs(a) do a[k] = nil end 116 collectgarbage() -- restore GC and collect dead fiels in `a' 117 for i=0,t-1 do 118 local k = querytab(a, i) 119 assert(k == nil or type(k) == 'number' or k == 'alo') 120 end 121 end 122 123 124 -- testing lexical environments 125 126 assert(_ENV == _G) 127 128 do 129 local dummy 130 local _ENV = (function (...) return ... end)(_G, dummy) -- { 131 132 do local _ENV = {assert=assert}; assert(true) end 133 mt = {_G = _G} 134 local foo,x 135 A = false -- "declare" A 136 do local _ENV = mt 137 function foo (x) 138 A = x 139 do local _ENV = _G; A = 1000 end 140 return function (x) return A .. x end 141 end 142 end 143 assert(getenv(foo) == mt) 144 x = foo('hi'); assert(mt.A == 'hi' and A == 1000) 145 assert(x('*') == mt.A .. '*') 146 147 do local _ENV = {assert=assert, A=10}; 148 do local _ENV = {assert=assert, A=20}; 149 assert(A==20);x=A 150 end 151 assert(A==10 and x==20) 152 end 153 assert(x==20) 154 155 156 print('OK') 157 158 return 5,f 159 160 end -- } 161