lua-users home
lua-l archive

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

On 2017-09-03 11:10 AM, Soni L. wrote:

On 2017-09-02 02:28 PM, Soni L. wrote:

On 2014-05-24 06:24 PM, Mike Nelson wrote:
The bytecode hack that works:

replace the case ':' clause in suffixedexp in lparser.c with:

case ':': {  /* `:' NAME funcargs */
        expdesc key;
        int flag=0;
        if (ls->t.token=='[') {
           luaK_exp2anyregup(fs, v);
           yindex(ls, &key);
           if (key.k==VNONRELOC){flag=1;}

            checkname(ls, &key);
        luaK_self(fs, v, &key);
        if (flag) ls->fs->freereg++;
        funcargs(ls, v, line);

This will detect and correct the pathological cases of bytecode generation.

Special thanks to Luiz for the original patch and to Roberto for pointing me in the right direction.

Is some kind of error checking in order for the line if (flag) ls->fs->freereg++;? I don't see the need based on the source code of yindex, etc., but I could be overlooking something.

If anyone finds a case in which this fails, please let me know, the detection clause may need to be more complex.

Sorry to bump an old thread, but this appears to no longer work, in Lua 5.3.4.

Would anyone know of a fix?

Update: I noticed this

But even after reverting the commit, I still get "Assertion `reg == fs->freereg' failed".

Update: "<Soni> All I [could] think of [was] just getting rid of luaK_self entirely":

(bonus: includes component syntax)

/* lparser.c:suffixedexp: */

      case ':': {  /* ':' NAME funcargs */
        expdesc key;
        int ereg;
        luaK_exp2anyreg(fs, v);
        ereg = v->;  /* register where 'e' was placed */
        luaK_freeexp(fs, v);
        v-> = fs->freereg;  /* base register for op_self */
        v->k = VNONRELOC;  /* self expression has a fixed register */
luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
        if (ls->t.token == '[') {
          yindex(ls, &key);
          checkname(ls, &key);
        luaK_codeABC(fs, OP_SELF, v->, ereg, luaK_exp2RK(fs, &key));
        luaK_freeexp(fs, &key);
        if (ls->t.token == '.') { /* component syntax */
          expdesc key2;
          int rg;
          luaX_next(ls);  /* skip the dot or colon */
          checkname(ls, &key2);
          rg = luaK_exp2RK(fs, &key2);
          luaK_codeABC(fs, OP_GETTABLE, v->, v->, rg);
          if (!ISK(rg) && rg >= fs->nactvar) {
            lua_assert(rg == fs->freereg);
        funcargs(ls, v, line);

/* lcode.c:freeexp: */

** Free register used by expression 'e' (if any)
static void freeexp (FuncState *fs, expdesc *e) {
  if (e->k == VNONRELOC)
    freereg(fs, e->;
void luaK_freeexp (FuncState *fs, expdesc *e) {
  freeexp(fs, e);

/* lcode.h:anywhere: */

LUAI_FUNC void luaK_freeexp (FuncState *fs, expdesc *e);

This compiles, doesn't throw any asserts, and handles all edge-cases gracefully!

-- tests.lua

local t = setmetatable({}, { __tostring=function()return"t"end})
local F = {}
local T = {}
t.t = t = t
t[T] = t
t.f = print
t.ff = print
t[F] = print
local _f="f"
local _t="t"

print("------ t:[k]()")
t:f(1) -- plain old lua
t:[_f](2) -- simple string key in register
t:[string.char(string.byte("f"))](3,32,33) -- string key from function
t:["f".."f"](4) -- string key from concatenation
t:["f"..string.sub("afun",2,2)](5,52,53) -- concatenation with function result
t:[(string.sub("afun",2,2))](6,62,63) -- function result in parentheses
t:[(function()return"f"end)()](7) -- closure in key
-- be careful with the ambiguous function call!!!
;(function()return t end)():[(function()return"f"end)()](8) -- closure in object and in key
t:[F](9) -- object key

print("------ t:[k].f()")
t:t.f(1) -- string identifier
t:[_t].f(2) -- string key in register
t:[string.char(string.byte("t"))].f(3,32,33) -- string key from function
t:["t".."t"].f(4) -- string key from concatenation
t:["t"..string.sub("atable",2,2)].f(5,52,53) -- concatenation with function result
t:[(string.sub("atable",2,2))].f(6,62,63) -- function result in parentheses
t:[(function()return"t"end)()].f(7) -- closure in key
-- be careful with the ambiguous function call!!!
;(function()return t end)():[(function()return"t"end)()].f(8) -- closure in object and in key
t:[T].f(9) -- object key

-- some fun with ECS/runtime traits
entity = {}

inventory = {get=false, set=false, size=false}
  local t = {size=function() return size end}
  function t.set(e, i, o)
    if i <= 0 or i > e:[inventory].size() then error() end
    e[inventory][i] = o
  function t.get(e, i)
    if i <= 0 or i > e:[inventory].size() then error() end
    return e[inventory][i]
  return t
inventory.of=function(e) -- helper for passing standalone inventories around
return {get=function(...)return e:[inventory].get(...)end, set=function(...)return e:[inventory].set(...)end, size=function(...)return e:[inventory].size(...)end}

entity[inventory] =

entity:[inventory].set(1, "Hello World!")


for i=1, entity:[inventory].size() do
  print(i, entity:[inventory].get(i))

local myinv = inventory.of(entity)

for i=1, myinv.size() do
  print("wrapped", i, myinv.get(i))

-- Mike

Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.