diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-08 16:01:38 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-06-08 16:01:38 -0300 |
| commit | cbc59592ff684b646b21766a66630df1f7974b25 (patch) | |
| tree | 1e28529cda3b8f6baa63285f6a2571767c3ed05e /lvm.c | |
| parent | 4905fdd1350bde68cd818b9198f28f5a47c208b0 (diff) | |
| download | lua-cbc59592ff684b646b21766a66630df1f7974b25.tar.gz lua-cbc59592ff684b646b21766a66630df1f7974b25.tar.bz2 lua-cbc59592ff684b646b21766a66630df1f7974b25.zip | |
new definition for `luaD_call' and `luaD_adjusttop'
Diffstat (limited to 'lvm.c')
| -rw-r--r-- | lvm.c | 76 |
1 files changed, 40 insertions, 36 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.180 2001/06/05 19:27:32 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.181 2001/06/08 12:29:27 roberto Exp roberto $ |
| 3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -106,33 +106,35 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) { | |||
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | 108 | ||
| 109 | static void callTM (lua_State *L, const l_char *fmt, ...) { | 109 | static void callTM (lua_State *L, Closure *f, const l_char *fmt, ...) { |
| 110 | va_list argp; | 110 | va_list argp; |
| 111 | StkId base = L->top; | 111 | StkId base = L->top; |
| 112 | int has_result = 0; | 112 | StkId result = NULL; /* result position */ |
| 113 | va_start(argp, fmt); | 113 | va_start(argp, fmt); |
| 114 | setclvalue(L->top, f); /* push function */ | ||
| 115 | incr_top; | ||
| 116 | if (*fmt == l_c('r')) { | ||
| 117 | fmt++; | ||
| 118 | result = va_arg(argp, TObject *); /* result position */ | ||
| 119 | } | ||
| 114 | while (*fmt) { | 120 | while (*fmt) { |
| 115 | switch (*fmt++) { | 121 | if (*fmt++ == l_c('o')) { |
| 116 | case l_c('c'): | ||
| 117 | setclvalue(L->top, va_arg(argp, Closure *)); | ||
| 118 | break; | ||
| 119 | case l_c('o'): | ||
| 120 | setobj(L->top, va_arg(argp, TObject *)); | 122 | setobj(L->top, va_arg(argp, TObject *)); |
| 121 | break; | 123 | } |
| 122 | case l_c('s'): | 124 | else { |
| 123 | setsvalue(L->top, va_arg(argp, TString *)); | 125 | lua_assert(*(fmt-1) == l_c('s')); |
| 124 | break; | 126 | setsvalue(L->top, va_arg(argp, TString *)); |
| 125 | case l_c('r'): | ||
| 126 | has_result = 1; | ||
| 127 | continue; | ||
| 128 | } | 127 | } |
| 129 | incr_top; | 128 | incr_top; |
| 130 | } | 129 | } |
| 131 | luaD_call(L, base, has_result); | 130 | luaD_call(L, base); |
| 132 | if (has_result) { | 131 | if (result) { /* need result? */ |
| 133 | L->top--; | 132 | if (L->top == base) /* are there valid results? */ |
| 134 | setobj(va_arg(argp, TObject *), L->top); | 133 | setnilvalue(result); /* function had no results */ |
| 134 | else | ||
| 135 | setobj(result, base); /* get first result */ | ||
| 135 | } | 136 | } |
| 137 | L->top = base; /* restore top */ | ||
| 136 | va_end(argp); | 138 | va_end(argp); |
| 137 | } | 139 | } |
| 138 | 140 | ||
| @@ -162,7 +164,7 @@ void luaV_gettable (lua_State *L, StkId t, TObject *key, StkId res) { | |||
| 162 | if (tm == NULL) /* no tag method? */ | 164 | if (tm == NULL) /* no tag method? */ |
| 163 | luaG_typeerror(L, t, l_s("index")); | 165 | luaG_typeerror(L, t, l_s("index")); |
| 164 | } | 166 | } |
| 165 | callTM(L, l_s("coor"), tm, t, key, res); | 167 | callTM(L, tm, l_s("roo"), res, t, key); |
| 166 | } | 168 | } |
| 167 | 169 | ||
| 168 | 170 | ||
| @@ -185,7 +187,7 @@ void luaV_settable (lua_State *L, StkId t, TObject *key, StkId val) { | |||
| 185 | if (tm == NULL) /* no tag method? */ | 187 | if (tm == NULL) /* no tag method? */ |
| 186 | luaG_typeerror(L, t, l_s("index")); | 188 | luaG_typeerror(L, t, l_s("index")); |
| 187 | } | 189 | } |
| 188 | callTM(L, l_s("cooo"), tm, t, key, val); | 190 | callTM(L, tm, l_s("ooo"), t, key, val); |
| 189 | } | 191 | } |
| 190 | 192 | ||
| 191 | 193 | ||
| @@ -196,7 +198,7 @@ void luaV_getglobal (lua_State *L, TString *name, StkId res) { | |||
| 196 | (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { | 198 | (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL) { |
| 197 | setobj(res, value); /* default behavior */ | 199 | setobj(res, value); /* default behavior */ |
| 198 | } else | 200 | } else |
| 199 | callTM(L, l_s("csor"), tm, name, value, res); | 201 | callTM(L, tm, l_s("rso"), res, name, value); |
| 200 | } | 202 | } |
| 201 | 203 | ||
| 202 | 204 | ||
| @@ -207,7 +209,7 @@ void luaV_setglobal (lua_State *L, TString *name, StkId val) { | |||
| 207 | (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { | 209 | (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) { |
| 208 | setobj(oldvalue, val); /* raw set */ | 210 | setobj(oldvalue, val); /* raw set */ |
| 209 | } else | 211 | } else |
| 210 | callTM(L, l_s("csoo"), tm, name, oldvalue, val); | 212 | callTM(L, tm, l_s("soo"), name, oldvalue, val); |
| 211 | } | 213 | } |
| 212 | 214 | ||
| 213 | 215 | ||
| @@ -224,7 +226,7 @@ static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, | |||
| 224 | } | 226 | } |
| 225 | } | 227 | } |
| 226 | opname = luaS_new(L, luaT_eventname[event]); | 228 | opname = luaS_new(L, luaT_eventname[event]); |
| 227 | callTM(L, l_s("coosr"), tm, p1, p2, opname, res); | 229 | callTM(L, tm, l_s("roos"), res, p1, p2, opname); |
| 228 | return 1; | 230 | return 1; |
| 229 | } | 231 | } |
| 230 | 232 | ||
| @@ -320,9 +322,12 @@ static void luaV_pack (lua_State *L, StkId firstelem) { | |||
| 320 | 322 | ||
| 321 | static void adjust_varargs (lua_State *L, StkId base, int nfixargs) { | 323 | static void adjust_varargs (lua_State *L, StkId base, int nfixargs) { |
| 322 | int nvararg = (L->top-base) - nfixargs; | 324 | int nvararg = (L->top-base) - nfixargs; |
| 323 | if (nvararg < 0) | 325 | StkId firstvar = base + nfixargs; /* position of first vararg */ |
| 324 | luaD_adjusttop(L, base, nfixargs); | 326 | if (nvararg < 0) { |
| 325 | luaV_pack(L, base+nfixargs); | 327 | luaD_checkstack(L, -nvararg); |
| 328 | luaD_adjusttop(L, firstvar); | ||
| 329 | } | ||
| 330 | luaV_pack(L, firstvar); | ||
| 326 | } | 331 | } |
| 327 | 332 | ||
| 328 | 333 | ||
| @@ -368,7 +373,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
| 368 | luaD_stackerror(L); | 373 | luaD_stackerror(L); |
| 369 | while (L->top < base+tf->maxstacksize) | 374 | while (L->top < base+tf->maxstacksize) |
| 370 | setnilvalue(L->top++); | 375 | setnilvalue(L->top++); |
| 371 | L->top = base+tf->maxstacksize; | 376 | L->top = base + tf->maxstacksize; |
| 372 | pc = tf->code; | 377 | pc = tf->code; |
| 373 | L->ci->pc = &pc; | 378 | L->ci->pc = &pc; |
| 374 | linehook = L->linehook; | 379 | linehook = L->linehook; |
| @@ -544,16 +549,15 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
| 544 | break; | 549 | break; |
| 545 | } | 550 | } |
| 546 | case OP_CALL: { | 551 | case OP_CALL: { |
| 547 | int nres; | 552 | int c; |
| 548 | int b = GETARG_B(i); | 553 | int b = GETARG_B(i); |
| 549 | if (b != NO_REG) | 554 | if (b != NO_REG) |
| 550 | L->top = base+b; | 555 | L->top = base+b; |
| 551 | nres = GETARG_C(i); | 556 | luaD_call(L, ra); |
| 552 | if (nres == NO_REG) nres = LUA_MULTRET; | 557 | c = GETARG_C(i); |
| 553 | luaD_call(L, ra, nres); | 558 | if (c != NO_REG) { |
| 554 | if (nres != LUA_MULTRET) { | 559 | while (L->top < base+c) setnilvalue(L->top++); |
| 555 | lua_assert(L->top == ra+nres); | 560 | L->top = base + tf->maxstacksize; |
| 556 | L->top = base+tf->maxstacksize; | ||
| 557 | } | 561 | } |
| 558 | break; | 562 | break; |
| 559 | } | 563 | } |
| @@ -637,7 +641,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
| 637 | luaV_checkGC(L, ra+nup); | 641 | luaV_checkGC(L, ra+nup); |
| 638 | L->top = ra+nup; | 642 | L->top = ra+nup; |
| 639 | luaV_Lclosure(L, p, nup); | 643 | luaV_Lclosure(L, p, nup); |
| 640 | L->top = base+tf->maxstacksize; | 644 | L->top = base + tf->maxstacksize; |
| 641 | break; | 645 | break; |
| 642 | } | 646 | } |
| 643 | } | 647 | } |
