From 4d46289331395a845c5de1f6c0e0fe873c50db4f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 3 Jul 2019 14:18:07 -0300 Subject: Local attributes can be used in list of local variables The syntax for local attributes ('const'/'toclose') was unified with the regular syntax for local variables, so that we can have variables with attributes in local definitions with multiple names; for instance: local f, err = io.open(fname) This new syntax does not implement constant propagation, yet. This commit also has some small improvements to the manual. --- testes/locals.lua | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'testes') diff --git a/testes/locals.lua b/testes/locals.lua index a41b6f0e..50230a27 100644 --- a/testes/locals.lua +++ b/testes/locals.lua @@ -173,12 +173,32 @@ end assert(x==20) +do -- constants + local a, b, c = 10, 20, 30 + b = a + c + b -- 'b' is not constant + assert(a == 10 and b == 60 and c == 30) + local function checkro (code, name) + local st, msg = load(code) + local gab = string.format("attempt to assign to const variable '%s'", name) + assert(not st and string.find(msg, gab)) + end + checkro("local x, y, z = 10, 20, 30; x = 11; y = 12", "y") + checkro("local x, y, z = 10, 20, 30; x = 11", "x") + checkro("local x, y, z = 10, 20, 30; y = 10; z = 11", "z") +end + + print"testing to-be-closed variables" local function stack(n) n = ((n == 0) or stack(n - 1)) end -local function func2close (f) - return setmetatable({}, {__close = f}) +local function func2close (f, x, y) + local obj = setmetatable({}, {__close = f}) + if x then + return x, obj, y + else + return obj + end end @@ -187,10 +207,11 @@ do do local x = setmetatable({"x"}, {__close = function (self) a[#a + 1] = self[1] end}) - local y = func2close(function (self, err) - assert(err == nil); a[#a + 1] = "y" - end) + local w, y, z = func2close(function (self, err) + assert(err == nil); a[#a + 1] = "y" + end, 10, 20) a[#a + 1] = "in" + assert(w == 10 and z == 20) end a[#a + 1] = "out" assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out") @@ -199,7 +220,8 @@ end do local X = false - local closescope = func2close(function () stack(10); X = true end) + local x, closescope = func2close(function () stack(10); X = true end, 100) + assert(x == 100); x = 101; -- 'x' is not read-only -- closing functions do not corrupt returning values local function foo (x) -- cgit v1.2.3-55-g6feb