lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, 30 Sep 2009 14:06:41 +0300, steve donovan <steve.j.donovan@gmail.com> wrote:

Well, one option is simple lexical macros

Hmm, turning off upvalues sounds like a nice useful feature.

What about this (intentionally ugly syntax, so that nobody would use it :-) ):

Test:

a = "global a"
b = "global b"
local a = "local a"

function x()
    local b = "local b"
    local function f#()
        return a, b
    end
    local function g()
        return a, b
    end
    print(f())
    print(g())
end

x()

-->global a        global b
-->local a local b

Patch:

--- lua-5.1.4/src/lparser.c     2007-12-28 17:32:23.000000000 +0200
+++ lua-5.1.4-namelid/src/lparser.c     2009-09-30 16:11:44.000000000 +0300
@@ -235,7 +235,7 @@
       return VLOCAL;
     }
     else {  /* not found at current level; try upper one */
-      if (singlevaraux(fs->prev, n, var, 0) == VGLOBAL)
+      if (singlevaraux(fs->namelid ? 0 : fs->prev, n, var, 0) == VGLOBAL)
         return VGLOBAL;
var->u.s.info = indexupvalue(fs, n, var); /* else was LOCAL or UPVAL */
       var->k = VUPVAL;  /* upvalue in this level */
@@ -578,6 +578,7 @@
   FuncState new_fs;
   open_func(ls, &new_fs);
   new_fs.f->linedefined = line;
+  new_fs.namelid = testnext(ls, '#');
   checknext(ls, '(');
   if (needself) {
     new_localvarliteral(ls, "self", 0);
diff -BubNrd lua-5.1.4/src/lparser.h lua-5.1.4-namelid/src/lparser.h
--- lua-5.1.4/src/lparser.h     2007-12-27 15:02:25.000000000 +0200
+++ lua-5.1.4-namelid/src/lparser.h     2009-09-30 16:12:02.000000000 +0300
@@ -70,6 +70,7 @@
   int np;  /* number of elements in `p' */
   short nlocvars;  /* number of elements in `locvars' */
   lu_byte nactvar;  /* number of active local variables */
+  lu_byte namelid;  /* name lookup cannot proceed above this function */
   upvaldesc upvalues[LUAI_MAXUPVALUES];  /* upvalues */
   unsigned short actvar[LUAI_MAXVARS];  /* declared-variable stack */
 } FuncState;