lua-users home
lua-l archive

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




2011/1/29 Tony Finch <dot@dotat.at>
On Fri, 28 Jan 2011, Alexander Gladysh wrote:
> On Fri, Jan 28, 2011 at 16:52, Tony Finch <dot@dotat.at> wrote:
>
> > It may be worth allowing obj:[method](args)
>
> I certainly miss this feature often. Is it hard to implement?

Something like this:

diff --git a/src/lcode.c b/src/lcode.c
index 6194749..adbe971 100644
--- a/src/lcode.c
+++ b/src/lcode.c
@@ -500,12 +500,17 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {
 }


-void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
+int luaK_self1 (FuncState *fs, expdesc *e) {
  int func;
  luaK_exp2anyreg(fs, e);
  freeexp(fs, e);
  func = fs->freereg;
  luaK_reserveregs(fs, 2);
+  return func;
+}
+
+
+void luaK_self2 (FuncState *fs, int func, expdesc *e, expdesc *key) {
  luaK_codeABC(fs, OP_SELF, func, e->u.s.info, luaK_exp2RK(fs, key));
  freeexp(fs, key);
  e->u.s.info = func;
diff --git a/src/lcode.h b/src/lcode.h
index b941c60..4aaeb01 100644
--- a/src/lcode.h
+++ b/src/lcode.h
@@ -55,7 +55,8 @@ LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
 LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
-LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
+LUAI_FUNC int luaK_self1 (FuncState *fs, expdesc *e);
+LUAI_FUNC void luaK_self2 (FuncState *fs, int func, expdesc *e, expdesc *key);
 LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
 LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
 LUAI_FUNC void luaK_storevar (FuncState *fs, expdesc *var, expdesc *e);
diff --git a/src/lparser.c b/src/lparser.c
index 7ff0925..c684a78 100644
--- a/src/lparser.c
+++ b/src/lparser.c
@@ -778,10 +778,15 @@ static void primaryexp (LexState *ls, expdesc *v) {
        break;
      }
      case ':': {  /* `:' NAME funcargs */
+        int func;
        expdesc key;
        luaX_next(ls);
-        checkname(ls, &key);
-        luaK_self(fs, v, &key);
+        func = luaK_self1(fs, v);
+        if (ls->t.token == '[')
+          yindex(ls, &key);
+        else
+          checkname(ls, &key);
+        luaK_self2(fs, func, v, &key);
        funcargs(ls, v);
        break;
      }


Tony.
--
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at/
HUMBER THAMES DOVER WIGHT PORTLAND: NORTH BACKING WEST OR NORTHWEST, 5 TO 7,
DECREASING 4 OR 5, OCCASIONALLY 6 LATER IN HUMBER AND THAMES. MODERATE OR
ROUGH. RAIN THEN FAIR. GOOD.

Is it possible merge into official lua sources?