lua-users home
lua-l archive

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


Hello, lua-l. (this time correct way around)

TL;DR: I do 2 proposals, this is the second.
I want to make `self:[key](...)` work.

The reasoning for this is just conveniance and more dynamic self calls.
The dynamic self-calls might be slower, but you can do some more fancy stuff.

--
function run(...)
    local cmd = {
        prime = function(self, ...) print("prime\t", ...) end,
        composite = function(self, ...) print("composite", ...) end,
        ...
    }
    for ip, op in ipairs(cmd) do cmd:[op](ip) end
end
run("prime", "prime", "prime", "composite", "prime", "composite")
--

The patch is also very small:

--- lua-5.4.4/src/lparser.c
+++ lua-5.4.4-self/src/lparser.c
@@ -1120,7 +1120,11 @@
       case ':': {  /* ':' NAME funcargs */
         expdesc key;
         luaX_next(ls);
-        codename(ls, &key);
+        if (ls->t.token == '[') {
+          yindex(ls, &key);
+        } else {
+          codename(ls, &key);
+        }
         luaK_self(fs, v, &key);
         funcargs(ls, v, line);
         break;
--- lua-5.4.4/src/lvm.c
+++ lua-5.4.4-self/src/lvm.c
@@ -1358,9 +1358,8 @@
         const TValue *slot;
         TValue *rb = vRB(i);
         TValue *rc = RKC(i);
-        TString *key = tsvalue(rc);  /* key must be a string */
         setobj2s(L, ra + 1, rb);
-        if (luaV_fastget(L, rb, key, slot, luaH_getstr)) {
+        if (luaV_fastget(L, rb, rc, slot, luaH_get)) {
           setobj2s(L, ra, slot);
         }
         else