lua-users home
lua-l archive

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



On Sep 14, 2009, at 10:58 PM, Mark Hamburg wrote:

In primaryexp (lparser.c), the colon operator case becomes:

     case ':': {  /* `:' NAME funcargs */
       expdesc key;
       luaX_next(ls);
		/* PATCH: Add support for obj:[ exp ] syntax for calling methods. */
		if (ls->t.token == '[') {
		  yindex(ls, &key);
		}
		else {
		  checkname(ls, &key);
		}
       luaK_self(fs, v, &key);
/* Compiles each expression evaluating them into a sequence of registers. */ switch (ls->t.token) { /* PATCH: Check for a call before going to the funcargs case, otherwise curry. */
		  case '(': case TK_STRING: case '{': { /* funcargs */
		    funcargs(ls, v);
			break;
		  }
		  default: {
		    currymethod(ls, v);
			return;
		  }
       }
       break;
     }


I'm afraid I don't have an implementation for currymethod. Without that support, the inner switch just becomes a call to funcargs(ls,v).

But to get the rest of obj:[ method ] working one also needs to revise luaK_self in lcode.c:

void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
 int func;
 luaK_exp2anyreg(fs, e);
 freeexp(fs, e);
 func = fs->freereg;
 luaK_reserveregs(fs, 2);
 luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
 freeexp(fs, key);
fs->freereg = func + 2; /* PATCH: Make sure that freeing the key won't have just cost us a register. Why??? */
 e->u.s.info = func;
 e->k = VNONRELOC;
}

As the comments, suggest it would be good to have someone who knows the register behavior in the Lua code generator better than I review this code.

Negative testing reveals that I probably need to refine getobjname in ldebug.c as well for the OP_SELF case.

Other places that could matter (because they reference OP_SELF):
	PrintCode in print.c
luaP_opmodes in lopcodes.c (the comment in lopcodes.h may also need a tweak)
	symbexec in ldebug.c
The issue being that OP_SELF no longer gets to assume that the method argument is a constant string. I need to study the Lua implementation further to resolve what if anything needs to be done for these cases.

Mark