diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-01-13 13:56:03 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-01-13 13:56:03 -0200 |
| commit | 46ec57cbc6dfe8df707be8f1fa52146988013feb (patch) | |
| tree | 9d5a08bbcc0c515f80475f30f3a6e4f933df0dd4 | |
| parent | 62787f1b1f9bb745afbf28b04241c9020a74b7a2 (diff) | |
| download | lua-46ec57cbc6dfe8df707be8f1fa52146988013feb.tar.gz lua-46ec57cbc6dfe8df707be8f1fa52146988013feb.tar.bz2 lua-46ec57cbc6dfe8df707be8f1fa52146988013feb.zip | |
little change when calling tag methods
| -rw-r--r-- | lapi.c | 7 | ||||
| -rw-r--r-- | ldo.c | 36 | ||||
| -rw-r--r-- | lvm.c | 63 |
3 files changed, 58 insertions, 48 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.66 1999/12/27 17:33:22 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.67 1999/12/30 18:27:03 roberto Exp roberto $ |
| 3 | ** Lua API | 3 | ** Lua API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -157,6 +157,7 @@ lua_Object lua_rawgettable (lua_State *L) { | |||
| 157 | 157 | ||
| 158 | void lua_settable (lua_State *L) { | 158 | void lua_settable (lua_State *L) { |
| 159 | luaA_checkCparams(L, 3); | 159 | luaA_checkCparams(L, 3); |
| 160 | luaD_checkstack(L, 3); /* may need that to call a tag method */ | ||
| 160 | luaV_settable(L, L->top-3); | 161 | luaV_settable(L, L->top-3); |
| 161 | L->top -= 2; /* pop table and index */ | 162 | L->top -= 2; /* pop table and index */ |
| 162 | } | 163 | } |
| @@ -178,7 +179,7 @@ lua_Object lua_createtable (lua_State *L) { | |||
| 178 | 179 | ||
| 179 | 180 | ||
| 180 | lua_Object lua_getglobal (lua_State *L, const char *name) { | 181 | lua_Object lua_getglobal (lua_State *L, const char *name) { |
| 181 | luaD_checkstack(L, 2); /* may need that to call a tag method */ | 182 | luaD_checkstack(L, 3); /* may need that to call a tag method */ |
| 182 | luaV_getglobal(L, luaS_assertglobalbyname(L, name)); | 183 | luaV_getglobal(L, luaS_assertglobalbyname(L, name)); |
| 183 | return luaA_putObjectOnTop(L); | 184 | return luaA_putObjectOnTop(L); |
| 184 | } | 185 | } |
| @@ -192,7 +193,7 @@ lua_Object lua_rawgetglobal (lua_State *L, const char *name) { | |||
| 192 | 193 | ||
| 193 | void lua_setglobal (lua_State *L, const char *name) { | 194 | void lua_setglobal (lua_State *L, const char *name) { |
| 194 | luaA_checkCparams(L, 1); | 195 | luaA_checkCparams(L, 1); |
| 195 | luaD_checkstack(L, 2); /* may need that to call a tag method */ | 196 | luaD_checkstack(L, 3); /* may need that to call a tag method */ |
| 196 | luaV_setglobal(L, luaS_assertglobalbyname(L, name)); | 197 | luaV_setglobal(L, luaS_assertglobalbyname(L, name)); |
| 197 | } | 198 | } |
| 198 | 199 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldo.c,v 1.63 1999/12/30 18:28:40 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.64 1999/12/30 18:40:57 roberto Exp roberto $ |
| 3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -353,9 +353,10 @@ static int do_main (lua_State *L, ZIO *z, int bin) { | |||
| 353 | void luaD_gcIM (lua_State *L, const TObject *o) { | 353 | void luaD_gcIM (lua_State *L, const TObject *o) { |
| 354 | const TObject *im = luaT_getimbyObj(L, o, IM_GC); | 354 | const TObject *im = luaT_getimbyObj(L, o, IM_GC); |
| 355 | if (ttype(im) != LUA_T_NIL) { | 355 | if (ttype(im) != LUA_T_NIL) { |
| 356 | *L->top = *o; | 356 | luaD_checkstack(L, 2); |
| 357 | incr_top; | 357 | *(L->top++) = *im; |
| 358 | luaD_callTM(L, im, 1, 0); | 358 | *(L->top++) = *o; |
| 359 | luaD_call(L, L->top-2, 0); | ||
| 359 | } | 360 | } |
| 360 | } | 361 | } |
| 361 | 362 | ||
| @@ -365,25 +366,18 @@ void luaD_gcIM (lua_State *L, const TObject *o) { | |||
| 365 | int lua_dofile (lua_State *L, const char *filename) { | 366 | int lua_dofile (lua_State *L, const char *filename) { |
| 366 | ZIO z; | 367 | ZIO z; |
| 367 | int status; | 368 | int status; |
| 368 | int bin; | 369 | int bin; /* flag for file mode */ |
| 370 | int c; /* look ahead char */ | ||
| 369 | char source[MAXFILENAME]; | 371 | char source[MAXFILENAME]; |
| 370 | FILE *f; | 372 | FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); |
| 373 | if (f == NULL) return 2; /* unable to open file */ | ||
| 371 | luaL_filesource(source, filename, sizeof(source)); | 374 | luaL_filesource(source, filename, sizeof(source)); |
| 372 | if (filename == NULL) { | 375 | c = fgetc(f); |
| 373 | f = stdin; | 376 | ungetc(c, f); |
| 374 | bin = 0; /* cannot handle stdin as a binary file */ | 377 | bin = (c == ID_CHUNK); |
| 375 | } | 378 | if (bin && f != stdin) { |
| 376 | else { | 379 | f = freopen(filename, "rb", f); /* set binary mode */ |
| 377 | int c; | 380 | if (f == NULL) return 2; /* unable to reopen file */ |
| 378 | f = fopen(filename, "r"); | ||
| 379 | if (f == NULL) return 2; /* unable to open file */ | ||
| 380 | c = fgetc(f); | ||
| 381 | ungetc(c, f); | ||
| 382 | bin = (c == ID_CHUNK); | ||
| 383 | if (bin) { | ||
| 384 | f = freopen(filename, "rb", f); /* set binary mode */ | ||
| 385 | if (f == NULL) return 2; /* unable to reopen file */ | ||
| 386 | } | ||
| 387 | } | 381 | } |
| 388 | luaZ_Fopen(&z, f, source); | 382 | luaZ_Fopen(&z, f, source); |
| 389 | status = do_main(L, &z, bin); | 383 | status = do_main(L, &z, bin); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.77 1999/12/29 16:31:15 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.78 1999/12/30 18:28:40 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 | */ |
| @@ -33,8 +33,11 @@ | |||
| 33 | #define highbyte(L, x) ((x)<<8) | 33 | #define highbyte(L, x) ((x)<<8) |
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | /* Extra stack size to run a function: LUA_T_LINE(1), TM calls(2), ... */ | 36 | /* |
| 37 | #define EXTRA_STACK 6 | 37 | ** Extra stack size to run a function: |
| 38 | ** LUA_T_LINE(1), NAME(1), TM calls(3) (plus some extra...) | ||
| 39 | */ | ||
| 40 | #define EXTRA_STACK 8 | ||
| 38 | 41 | ||
| 39 | 42 | ||
| 40 | 43 | ||
| @@ -133,6 +136,7 @@ void luaV_gettable (lua_State *L) { | |||
| 133 | 136 | ||
| 134 | /* | 137 | /* |
| 135 | ** Receives table at *t, index at *(t+1) and value at top. | 138 | ** Receives table at *t, index at *(t+1) and value at top. |
| 139 | ** WARNING: caller must assure 3 extra stack slots (to call a tag method) | ||
| 136 | */ | 140 | */ |
| 137 | void luaV_settable (lua_State *L, StkId t) { | 141 | void luaV_settable (lua_State *L, StkId t) { |
| 138 | const TObject *im; | 142 | const TObject *im; |
| @@ -152,11 +156,12 @@ void luaV_settable (lua_State *L, StkId t) { | |||
| 152 | } | 156 | } |
| 153 | /* object is not a table, or it has a `settable' method */ | 157 | /* object is not a table, or it has a `settable' method */ |
| 154 | /* prepare arguments and call the tag method */ | 158 | /* prepare arguments and call the tag method */ |
| 155 | *(L->top+1) = *(L->top-1); | 159 | *(L->top+2) = *(L->top-1); |
| 156 | *(L->top) = *(t+1); | 160 | *(L->top+1) = *(t+1); |
| 157 | *(L->top-1) = *t; | 161 | *(L->top) = *t; |
| 158 | L->top += 2; /* WARNING: caller must assure stack space */ | 162 | *(L->top-1) = *im; |
| 159 | luaD_callTM(L, im, 3, 0); | 163 | L->top += 3; |
| 164 | luaD_call(L, L->top-4, 0); | ||
| 160 | } | 165 | } |
| 161 | 166 | ||
| 162 | 167 | ||
| @@ -170,36 +175,41 @@ void luaV_rawsettable (lua_State *L, StkId t) { | |||
| 170 | } | 175 | } |
| 171 | 176 | ||
| 172 | 177 | ||
| 178 | /* | ||
| 179 | ** WARNING: caller must assure 3 extra stack slots (to call a tag method) | ||
| 180 | */ | ||
| 173 | void luaV_getglobal (lua_State *L, GlobalVar *gv) { | 181 | void luaV_getglobal (lua_State *L, GlobalVar *gv) { |
| 174 | /* WARNING: caller must assure stack space */ | ||
| 175 | const TObject *value = &gv->value; | 182 | const TObject *value = &gv->value; |
| 176 | TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); | 183 | TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); |
| 177 | if (ttype(im) != LUA_T_NIL) { /* is there a tag method? */ | 184 | if (ttype(im) == LUA_T_NIL) /* is there a tag method? */ |
| 178 | ttype(L->top) = LUA_T_STRING; | ||
| 179 | tsvalue(L->top) = gv->name; /* global name */ | ||
| 180 | L->top++; | ||
| 181 | *L->top++ = *value; | ||
| 182 | luaD_callTM(L, im, 2, 1); | ||
| 183 | } else { /* no tag method */ | ||
| 184 | *L->top++ = *value; /* default behavior */ | 185 | *L->top++ = *value; /* default behavior */ |
| 186 | else { /* tag method */ | ||
| 187 | *L->top = *im; | ||
| 188 | ttype(L->top+1) = LUA_T_STRING; | ||
| 189 | tsvalue(L->top+1) = gv->name; /* global name */ | ||
| 190 | *(L->top+2) = *value; | ||
| 191 | L->top += 3; | ||
| 192 | luaD_call(L, L->top-3, 1); | ||
| 185 | } | 193 | } |
| 186 | } | 194 | } |
| 187 | 195 | ||
| 188 | 196 | ||
| 197 | /* | ||
| 198 | ** WARNING: caller must assure 3 extra stack slots (to call a tag method) | ||
| 199 | */ | ||
| 189 | void luaV_setglobal (lua_State *L, GlobalVar *gv) { | 200 | void luaV_setglobal (lua_State *L, GlobalVar *gv) { |
| 190 | const TObject *oldvalue = &gv->value; | 201 | const TObject *oldvalue = &gv->value; |
| 191 | const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); | 202 | const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); |
| 192 | if (ttype(im) == LUA_T_NIL) /* is there a tag method? */ | 203 | if (ttype(im) == LUA_T_NIL) /* is there a tag method? */ |
| 193 | gv->value = *(--L->top); | 204 | gv->value = *(--L->top); |
| 194 | else { | 205 | else { |
| 195 | /* WARNING: caller must assure stack space */ | 206 | *(L->top+2) = *(L->top-1); /* new value */ |
| 196 | TObject newvalue; | 207 | *(L->top+1) = *oldvalue; |
| 197 | newvalue = *(L->top-1); | 208 | ttype(L->top) = LUA_T_STRING; |
| 198 | ttype(L->top-1) = LUA_T_STRING; | 209 | tsvalue(L->top) = gv->name; |
| 199 | tsvalue(L->top-1) = gv->name; | 210 | *(L->top-1) = *im; |
| 200 | *L->top++ = *oldvalue; | 211 | L->top += 3; |
| 201 | *L->top++ = newvalue; | 212 | luaD_call(L, L->top-4, 0); |
| 202 | luaD_callTM(L, im, 3, 0); | ||
| 203 | } | 213 | } |
| 204 | } | 214 | } |
| 205 | 215 | ||
| @@ -372,12 +382,14 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 372 | L->top = top; | 382 | L->top = top; |
| 373 | luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv); | 383 | luaV_getglobal(L, tsvalue(&consts[aux])->u.s.gv); |
| 374 | top++; | 384 | top++; |
| 385 | LUA_ASSERT(L, top==L->top, "top's not synchronized"); | ||
| 375 | break; | 386 | break; |
| 376 | 387 | ||
| 377 | case GETTABLE: | 388 | case GETTABLE: |
| 378 | L->top = top; | 389 | L->top = top; |
| 379 | luaV_gettable(L); | 390 | luaV_gettable(L); |
| 380 | top--; | 391 | top--; |
| 392 | LUA_ASSERT(L, top==L->top, "top's not synchronized"); | ||
| 381 | break; | 393 | break; |
| 382 | 394 | ||
| 383 | case GETDOTTEDW: aux += highbyte(L, *pc++); | 395 | case GETDOTTEDW: aux += highbyte(L, *pc++); |
| @@ -386,6 +398,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 386 | L->top = top; | 398 | L->top = top; |
| 387 | luaV_gettable(L); | 399 | luaV_gettable(L); |
| 388 | top--; | 400 | top--; |
| 401 | LUA_ASSERT(L, top==L->top, "top's not synchronized"); | ||
| 389 | break; | 402 | break; |
| 390 | 403 | ||
| 391 | case PUSHSELFW: aux += highbyte(L, *pc++); | 404 | case PUSHSELFW: aux += highbyte(L, *pc++); |
| @@ -417,6 +430,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 417 | L->top = top; | 430 | L->top = top; |
| 418 | luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv); | 431 | luaV_setglobal(L, tsvalue(&consts[aux])->u.s.gv); |
| 419 | top--; | 432 | top--; |
| 433 | LUA_ASSERT(L, top==L->top, "top's not synchronized"); | ||
| 420 | break; | 434 | break; |
| 421 | 435 | ||
| 422 | case SETTABLEPOP: | 436 | case SETTABLEPOP: |
| @@ -429,6 +443,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, const TProtoFunc *tf, | |||
| 429 | L->top = top; | 443 | L->top = top; |
| 430 | luaV_settable(L, top-3-(*pc++)); | 444 | luaV_settable(L, top-3-(*pc++)); |
| 431 | top--; /* pop value */ | 445 | top--; /* pop value */ |
| 446 | LUA_ASSERT(L, top==L->top, "top's not synchronized"); | ||
| 432 | break; | 447 | break; |
| 433 | 448 | ||
| 434 | case SETLISTW: aux += highbyte(L, *pc++); | 449 | case SETLISTW: aux += highbyte(L, *pc++); |
