123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- -- $Id: locals.lua,v 1.37 2016/11/07 13:11:28 roberto Exp $
- -- See Copyright Notice in file all.lua
- print('testing local variables and environments')
- local debug = require"debug"
- -- bug in 5.1:
- local function f(x) x = nil; return x end
- assert(f(10) == nil)
- local function f() local x; return x end
- assert(f(10) == nil)
- local function f(x) x = nil; local y; return x, y end
- assert(f(10) == nil and select(2, f(20)) == nil)
- do
- local i = 10
- do local i = 100; assert(i==100) end
- do local i = 1000; assert(i==1000) end
- assert(i == 10)
- if i ~= 10 then
- local i = 20
- else
- local i = 30
- assert(i == 30)
- end
- end
- f = nil
- local f
- x = 1
- a = nil
- load('local a = {}')()
- assert(a == nil)
- function f (a)
- local _1, _2, _3, _4, _5
- local _6, _7, _8, _9, _10
- local x = 3
- local b = a
- local c,d = a,b
- if (d == b) then
- local x = 'q'
- x = b
- assert(x == 2)
- else
- assert(nil)
- end
- assert(x == 3)
- local f = 10
- end
- local b=10
- local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3
- assert(x == 1)
- f(2)
- assert(type(f) == 'function')
- local function getenv (f)
- local a,b = debug.getupvalue(f, 1)
- assert(a == '_ENV')
- return b
- end
- -- test for global table of loaded chunks
- assert(getenv(load"a=3") == _G)
- local c = {}; local f = load("a = 3", nil, nil, c)
- assert(getenv(f) == c)
- assert(c.a == nil)
- f()
- assert(c.a == 3)
- -- old test for limits for special instructions (now just a generic test)
- do
- local i = 2
- local p = 4 -- p == 2^i
- repeat
- for j=-3,3 do
- assert(load(string.format([[local a=%s;
- a=a+%s;
- assert(a ==2^%s)]], j, p-j, i), '')) ()
- assert(load(string.format([[local a=%s;
- a=a-%s;
- assert(a==-2^%s)]], -j, p-j, i), '')) ()
- assert(load(string.format([[local a,b=0,%s;
- a=b-%s;
- assert(a==-2^%s)]], -j, p-j, i), '')) ()
- end
- p = 2 * p; i = i + 1
- until p <= 0
- end
- print'+'
- if rawget(_G, "querytab") then
- -- testing clearing of dead elements from tables
- collectgarbage("stop") -- stop GC
- local a = {[{}] = 4, [3] = 0, alo = 1,
- a1234567890123456789012345678901234567890 = 10}
- local t = querytab(a)
- for k,_ in pairs(a) do a[k] = nil end
- collectgarbage() -- restore GC and collect dead fiels in `a'
- for i=0,t-1 do
- local k = querytab(a, i)
- assert(k == nil or type(k) == 'number' or k == 'alo')
- end
- end
- -- testing lexical environments
- assert(_ENV == _G)
- do
- local dummy
- local _ENV = (function (...) return ... end)(_G, dummy) -- {
- do local _ENV = {assert=assert}; assert(true) end
- mt = {_G = _G}
- local foo,x
- A = false -- "declare" A
- do local _ENV = mt
- function foo (x)
- A = x
- do local _ENV = _G; A = 1000 end
- return function (x) return A .. x end
- end
- end
- assert(getenv(foo) == mt)
- x = foo('hi'); assert(mt.A == 'hi' and A == 1000)
- assert(x('*') == mt.A .. '*')
- do local _ENV = {assert=assert, A=10};
- do local _ENV = {assert=assert, A=20};
- assert(A==20);x=A
- end
- assert(A==10 and x==20)
- end
- assert(x==20)
- print('OK')
- return 5,f
- end -- }
|