aboutsummaryrefslogtreecommitdiff
path: root/testes/locals.lua
diff options
context:
space:
mode:
Diffstat (limited to 'testes/locals.lua')
-rw-r--r--testes/locals.lua42
1 files changed, 34 insertions, 8 deletions
diff --git a/testes/locals.lua b/testes/locals.lua
index ccea0a14..02f41980 100644
--- a/testes/locals.lua
+++ b/testes/locals.lua
@@ -1,6 +1,8 @@
1-- $Id: testes/locals.lua $ 1-- $Id: testes/locals.lua $
2-- See Copyright Notice in file lua.h 2-- See Copyright Notice in file lua.h
3 3
4global <const> *
5
4print('testing local variables and environments') 6print('testing local variables and environments')
5 7
6local debug = require"debug" 8local debug = require"debug"
@@ -39,9 +41,11 @@ f = nil
39local f 41local f
40local x = 1 42local x = 1
41 43
42a = nil 44do
43load('local a = {}')() 45 global a; a = nil
44assert(a == nil) 46 load('local a = {}')()
47 assert(a == nil)
48end
45 49
46function f (a) 50function f (a)
47 local _1, _2, _3, _4, _5 51 local _1, _2, _3, _4, _5
@@ -154,7 +158,7 @@ local _ENV = (function (...) return ... end)(_G, dummy) -- {
154do local _ENV = {assert=assert}; assert(true) end 158do local _ENV = {assert=assert}; assert(true) end
155local mt = {_G = _G} 159local mt = {_G = _G}
156local foo,x 160local foo,x
157A = false -- "declare" A 161global A; A = false -- "declare" A
158do local _ENV = mt 162do local _ENV = mt
159 function foo (x) 163 function foo (x)
160 A = x 164 A = x
@@ -177,20 +181,27 @@ assert(x==20)
177A = nil 181A = nil
178 182
179 183
180do -- constants 184do print("testing local constants")
185 global assert<const>, load, string, X
186 X = 1 -- not a constant
181 local a<const>, b, c<const> = 10, 20, 30 187 local a<const>, b, c<const> = 10, 20, 30
182 b = a + c + b -- 'b' is not constant 188 b = a + c + b -- 'b' is not constant
183 assert(a == 10 and b == 60 and c == 30) 189 assert(a == 10 and b == 60 and c == 30)
190
184 local function checkro (name, code) 191 local function checkro (name, code)
185 local st, msg = load(code) 192 local st, msg = load(code)
186 local gab = string.format("attempt to assign to const variable '%s'", name) 193 local gab = string.format("attempt to assign to const variable '%s'", name)
187 assert(not st and string.find(msg, gab)) 194 assert(not st and string.find(msg, gab))
188 end 195 end
196
189 checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12") 197 checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12")
190 checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11") 198 checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11")
191 checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11") 199 checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11")
192 checkro("foo", "local foo <const> = 10; function foo() end") 200 checkro("foo", "local<const> foo = 10; function foo() end")
193 checkro("foo", "local foo <const> = {}; function foo() end") 201 checkro("foo", "local<const> foo <const> = {}; function foo() end")
202 checkro("foo", "global<const> foo <const>; function foo() end")
203 checkro("XX", "global XX <const>; XX = 10")
204 checkro("XX", "local _ENV; global XX <const>; XX = 10")
194 205
195 checkro("z", [[ 206 checkro("z", [[
196 local a, z <const>, b = 10; 207 local a, z <const>, b = 10;
@@ -201,11 +212,26 @@ do -- constants
201 local a, var1 <const> = 10; 212 local a, var1 <const> = 10;
202 function foo() a = 20; z = function () var1 = 12; end end 213 function foo() a = 20; z = function () var1 = 12; end end
203 ]]) 214 ]])
215
216 checkro("var1", [[
217 global a, var1 <const>, z;
218 local function foo() a = 20; z = function () var1 = 12; end end
219 ]])
204end 220end
205 221
206 222
223
207print"testing to-be-closed variables" 224print"testing to-be-closed variables"
208 225
226
227do
228 local st, msg = load("local <close> a, b")
229 assert(not st and string.find(msg, "multiple"))
230
231 local st, msg = load("local a<close>, b<close>")
232 assert(not st and string.find(msg, "multiple"))
233end
234
209local function stack(n) n = ((n == 0) or stack(n - 1)) end 235local function stack(n) n = ((n == 0) or stack(n - 1)) end
210 236
211local function func2close (f, x, y) 237local function func2close (f, x, y)
@@ -1162,7 +1188,7 @@ do
1162 local function open (x) 1188 local function open (x)
1163 numopen = numopen + 1 1189 numopen = numopen + 1
1164 return 1190 return
1165 function () -- iteraction function 1191 function () -- iteration function
1166 x = x - 1 1192 x = x - 1
1167 if x > 0 then return x end 1193 if x > 0 then return x end
1168 end, 1194 end,