lua-users home
lua-l archive

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


On 5/21/2014 8:27 PM, Luiz Henrique de Figueiredo wrote:
I think there are patchs around for the t:[var]() syntax.
Actually, this one seems simple. A quick hack is to just change
suffixedexp in lparser.c (from Lua 5.2.3):

        case ':': {  /* `:' NAME funcargs */
          expdesc key;
          luaX_next(ls);
-        checkname(ls, &key);
+        if (ls->t.token=='[') {
+           luaK_exp2anyregup(fs, v);
+           yindex(ls, &key);
+        }
+        else
+           checkname(ls, &key);
          luaK_self(fs, v, &key);
          funcargs(ls, v, line);
          break;

I tested it only on this program. It works in some cases but not in others :-(

       local t={}
       t.f=print
       t.ff=print
       local v="f"
       t:f(1)
       t:[v](2)
       t:[string.char(string.byte("f"))](3,32,33)
       t:["f".."f"](4)
       t:["f"..string.sub("afun",2,2)](5,52,53)
       t:[(string.sub("afun",2,2))](6,62,63)

       table: 0x107e04e40	1
       table: 0x107e04e40	2
       3	32	33
       table: 0x107e04e40	4
       table: 0x107e04e40	5	52	53
       6	62	63

It's been some time since I last hacked the parser and I must have
overlooked something. Happy hacking.


The pathological cases in this patch can be corrected by concatenating the subscript with an empty string in the Lua script (NOT what a happy hacker wants).

This C code appears to detect these cases:
if (key.k==0x06) {/* I don't know what to do here */}
when inserted immediately after yindex(ls, &key);


The luac listing of these cases are all of this form:
    22    [10]    CALL         3 0 2
    23    [10]    SELF         4 0 3
    24    [10]    LOADK        5 -10    ; 3
    25    [10]    LOADK        6 -11    ; 32
    26    [10]    LOADK        7 -12    ; 33
    27    [10]    CALL         4 4 1

The C argument of SELF is always a register, the same register as the the A argument of the previous CALL.
Correct bytecode for the case above  should be like this:
    22    [10]    CALL         3 0 2
    23    [10]    SELF         4 0 3
    24    [10]    LOADK        6 -10    ; 3
    25    [10]    LOADK        7 -11    ; 32
    26    [10]    LOADK        8 -12    ; 33
    27    [10]    CALL         4 5 1

This should be useful for anybody attempting this parser hack, but I am at sea about how to fix it.

-- Mike