diff options
| -rw-r--r-- | lapi.c | 4 | ||||
| -rw-r--r-- | lvm.c | 123 | ||||
| -rw-r--r-- | lvm.h | 5 |
3 files changed, 43 insertions, 89 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lapi.c,v 1.235 2003/04/07 14:36:08 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.236 2003/04/28 19:58:06 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 | */ |
| @@ -485,7 +485,7 @@ LUA_API void lua_gettable (lua_State *L, int idx) { | |||
| 485 | StkId t; | 485 | StkId t; |
| 486 | lua_lock(L); | 486 | lua_lock(L); |
| 487 | t = luaA_index(L, idx); | 487 | t = luaA_index(L, idx); |
| 488 | setobj2s(L->top - 1, luaV_gettable(L, t, L->top - 1, 0)); | 488 | luaV_gettable(L, t, L->top - 1, L->top - 1); |
| 489 | lua_unlock(L); | 489 | lua_unlock(L); |
| 490 | } | 490 | } |
| 491 | 491 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.c,v 1.283 2003/03/31 13:00:25 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.284 2003/04/03 13:35:34 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 | */ |
| @@ -96,14 +96,17 @@ static void traceexec (lua_State *L) { | |||
| 96 | 96 | ||
| 97 | 97 | ||
| 98 | static void callTMres (lua_State *L, const TObject *f, | 98 | static void callTMres (lua_State *L, const TObject *f, |
| 99 | const TObject *p1, const TObject *p2) { | 99 | const TObject *p1, const TObject *p2, StkId res) { |
| 100 | ptrdiff_t result = savestack(L, res); | ||
| 100 | setobj2s(L->top, f); /* push function */ | 101 | setobj2s(L->top, f); /* push function */ |
| 101 | setobj2s(L->top+1, p1); /* 1st argument */ | 102 | setobj2s(L->top+1, p1); /* 1st argument */ |
| 102 | setobj2s(L->top+2, p2); /* 2nd argument */ | 103 | setobj2s(L->top+2, p2); /* 2nd argument */ |
| 103 | luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ | 104 | luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ |
| 104 | L->top += 3; | 105 | L->top += 3; |
| 105 | luaD_call(L, L->top - 3, 1); | 106 | luaD_call(L, L->top - 3, 1); |
| 106 | L->top--; /* result will be in L->top */ | 107 | res = restorestack(L, result); |
| 108 | L->top--; | ||
| 109 | setobjs2s(res, L->top); | ||
| 107 | } | 110 | } |
| 108 | 111 | ||
| 109 | 112 | ||
| @@ -120,56 +123,36 @@ static void callTM (lua_State *L, const TObject *f, | |||
| 120 | } | 123 | } |
| 121 | 124 | ||
| 122 | 125 | ||
| 123 | static const TObject *luaV_index (lua_State *L, const TObject *t, | 126 | void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId val) { |
| 124 | TObject *key, int loop) { | 127 | int loop; |
| 125 | const TObject *tm = fasttm(L, hvalue(t)->metatable, TM_INDEX); | 128 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
| 126 | if (tm == NULL) return &luaO_nilobject; /* no TM */ | 129 | const TObject *tm; |
| 127 | if (ttisfunction(tm)) { | 130 | if (ttistable(t)) { /* `t' is a table? */ |
| 128 | callTMres(L, tm, t, key); | 131 | Table *h = hvalue(t); |
| 129 | return L->top; | 132 | const TObject *res = luaH_get(h, key); /* do a primitive set */ |
| 130 | } | 133 | if (!ttisnil(res) || /* result is no nil? */ |
| 131 | else return luaV_gettable(L, tm, key, loop); | 134 | (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ |
| 132 | } | 135 | setobj2s(val, res); |
| 133 | 136 | return; | |
| 134 | static const TObject *luaV_getnotable (lua_State *L, const TObject *t, | 137 | } |
| 135 | TObject *key, int loop) { | 138 | /* else will try the tag method */ |
| 136 | const TObject *tm = luaT_gettmbyobj(L, t, TM_INDEX); | 139 | } |
| 137 | if (ttisnil(tm)) | 140 | else if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_INDEX))) |
| 138 | luaG_typeerror(L, t, "index"); | 141 | luaG_typeerror(L, t, "index"); |
| 139 | if (ttisfunction(tm)) { | 142 | if (ttisfunction(tm)) { |
| 140 | callTMres(L, tm, t, key); | 143 | callTMres(L, tm, t, key, val); |
| 141 | return L->top; | 144 | return; |
| 142 | } | 145 | } |
| 143 | else return luaV_gettable(L, tm, key, loop); | 146 | t = tm; /* else repeat with `tm' */ |
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | /* | ||
| 148 | ** Function to index a table. | ||
| 149 | ** Receives the table at `t' and the key at `key'. | ||
| 150 | ** leaves the result at `res'. | ||
| 151 | */ | ||
| 152 | const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key, | ||
| 153 | int loop) { | ||
| 154 | if (loop > MAXTAGLOOP) | ||
| 155 | luaG_runerror(L, "loop in gettable"); | ||
| 156 | if (ttistable(t)) { /* `t' is a table? */ | ||
| 157 | Table *h = hvalue(t); | ||
| 158 | const TObject *v = luaH_get(h, key); /* do a primitive get */ | ||
| 159 | if (!ttisnil(v)) return v; | ||
| 160 | else return luaV_index(L, t, key, loop+1); | ||
| 161 | } | 147 | } |
| 162 | else return luaV_getnotable(L, t, key, loop+1); | 148 | luaG_runerror(L, "loop in gettable"); |
| 163 | } | 149 | } |
| 164 | 150 | ||
| 165 | 151 | ||
| 166 | /* | ||
| 167 | ** Receives table at `t', key at `key' and value at `val'. | ||
| 168 | */ | ||
| 169 | void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) { | 152 | void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) { |
| 170 | const TObject *tm; | 153 | int loop; |
| 171 | int loop = 0; | 154 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
| 172 | do { | 155 | const TObject *tm; |
| 173 | if (ttistable(t)) { /* `t' is a table? */ | 156 | if (ttistable(t)) { /* `t' is a table? */ |
| 174 | Table *h = hvalue(t); | 157 | Table *h = hvalue(t); |
| 175 | TObject *oldval = luaH_set(L, h, key); /* do a primitive set */ | 158 | TObject *oldval = luaH_set(L, h, key); /* do a primitive set */ |
| @@ -187,21 +170,18 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) { | |||
| 187 | return; | 170 | return; |
| 188 | } | 171 | } |
| 189 | t = tm; /* else repeat with `tm' */ | 172 | t = tm; /* else repeat with `tm' */ |
| 190 | } while (++loop <= MAXTAGLOOP); | 173 | } |
| 191 | luaG_runerror(L, "loop in settable"); | 174 | luaG_runerror(L, "loop in settable"); |
| 192 | } | 175 | } |
| 193 | 176 | ||
| 194 | 177 | ||
| 195 | static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, | 178 | static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, |
| 196 | StkId res, TMS event) { | 179 | StkId res, TMS event) { |
| 197 | ptrdiff_t result = savestack(L, res); | ||
| 198 | const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ | 180 | const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ |
| 199 | if (ttisnil(tm)) | 181 | if (ttisnil(tm)) |
| 200 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ | 182 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ |
| 201 | if (!ttisfunction(tm)) return 0; | 183 | if (!ttisfunction(tm)) return 0; |
| 202 | callTMres(L, tm, p1, p2); | 184 | callTMres(L, tm, p1, p2, res); |
| 203 | res = restorestack(L, result); /* previous call may change stack */ | ||
| 204 | setobjs2s(res, L->top); | ||
| 205 | return 1; | 185 | return 1; |
| 206 | } | 186 | } |
| 207 | 187 | ||
| @@ -228,7 +208,7 @@ static int call_orderTM (lua_State *L, const TObject *p1, const TObject *p2, | |||
| 228 | tm2 = luaT_gettmbyobj(L, p2, event); | 208 | tm2 = luaT_gettmbyobj(L, p2, event); |
| 229 | if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ | 209 | if (!luaO_rawequalObj(tm1, tm2)) /* different metamethods? */ |
| 230 | return -1; | 210 | return -1; |
| 231 | callTMres(L, tm1, p1, p2); | 211 | callTMres(L, tm1, p1, p2, L->top); |
| 232 | return !l_isfalse(L->top); | 212 | return !l_isfalse(L->top); |
| 233 | } | 213 | } |
| 234 | 214 | ||
| @@ -307,7 +287,7 @@ int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2) { | |||
| 307 | default: return gcvalue(t1) == gcvalue(t2); | 287 | default: return gcvalue(t1) == gcvalue(t2); |
| 308 | } | 288 | } |
| 309 | if (tm == NULL) return 0; /* no TM? */ | 289 | if (tm == NULL) return 0; /* no TM? */ |
| 310 | callTMres(L, tm, t1, t2); /* call TM */ | 290 | callTMres(L, tm, t1, t2, L->top); /* call TM */ |
| 311 | return !l_isfalse(L->top); | 291 | return !l_isfalse(L->top); |
| 312 | } | 292 | } |
| 313 | 293 | ||
| @@ -358,12 +338,9 @@ static void Arith (lua_State *L, StkId ra, | |||
| 358 | case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break; | 338 | case TM_DIV: setnvalue(ra, nvalue(b) / nvalue(c)); break; |
| 359 | case TM_POW: { | 339 | case TM_POW: { |
| 360 | const TObject *f = luaH_getstr(hvalue(gt(L)), G(L)->tmname[TM_POW]); | 340 | const TObject *f = luaH_getstr(hvalue(gt(L)), G(L)->tmname[TM_POW]); |
| 361 | ptrdiff_t res = savestack(L, ra); | ||
| 362 | if (!ttisfunction(f)) | 341 | if (!ttisfunction(f)) |
| 363 | luaG_runerror(L, "`__pow' (`^' operator) is not a function"); | 342 | luaG_runerror(L, "`__pow' (`^' operator) is not a function"); |
| 364 | callTMres(L, f, b, c); | 343 | callTMres(L, f, b, c, ra); |
| 365 | ra = restorestack(L, res); /* previous call may change stack */ | ||
| 366 | setobjs2s(ra, L->top); | ||
| 367 | break; | 344 | break; |
| 368 | } | 345 | } |
| 369 | default: lua_assert(0); break; | 346 | default: lua_assert(0); break; |
| @@ -459,25 +436,12 @@ StkId luaV_execute (lua_State *L) { | |||
| 459 | } | 436 | } |
| 460 | case OP_GETGLOBAL: { | 437 | case OP_GETGLOBAL: { |
| 461 | TObject *rb = KBx(i); | 438 | TObject *rb = KBx(i); |
| 462 | const TObject *v; | ||
| 463 | lua_assert(ttisstring(rb) && ttistable(&cl->g)); | 439 | lua_assert(ttisstring(rb) && ttistable(&cl->g)); |
| 464 | v = luaH_getstr(hvalue(&cl->g), tsvalue(rb)); | 440 | luaV_gettable(L, &cl->g, rb, ra); |
| 465 | if (!ttisnil(v)) { setobj2s(ra, v); } | ||
| 466 | else | ||
| 467 | setobj2s(XRA(i), luaV_index(L, &cl->g, rb, 0)); | ||
| 468 | break; | 441 | break; |
| 469 | } | 442 | } |
| 470 | case OP_GETTABLE: { | 443 | case OP_GETTABLE: { |
| 471 | StkId rb = RB(i); | 444 | luaV_gettable(L, RB(i), RKC(i), ra); |
| 472 | TObject *rc = RKC(i); | ||
| 473 | if (ttistable(rb)) { | ||
| 474 | const TObject *v = luaH_get(hvalue(rb), rc); | ||
| 475 | if (!ttisnil(v)) { setobj2s(ra, v); } | ||
| 476 | else | ||
| 477 | setobj2s(XRA(i), luaV_index(L, rb, rc, 0)); | ||
| 478 | } | ||
| 479 | else | ||
| 480 | setobj2s(XRA(i), luaV_getnotable(L, rb, rc, 0)); | ||
| 481 | break; | 445 | break; |
| 482 | } | 446 | } |
| 483 | case OP_SETGLOBAL: { | 447 | case OP_SETGLOBAL: { |
| @@ -503,17 +467,8 @@ StkId luaV_execute (lua_State *L) { | |||
| 503 | } | 467 | } |
| 504 | case OP_SELF: { | 468 | case OP_SELF: { |
| 505 | StkId rb = RB(i); | 469 | StkId rb = RB(i); |
| 506 | TObject *rc = RKC(i); | ||
| 507 | runtime_check(L, ttisstring(rc)); | ||
| 508 | setobjs2s(ra+1, rb); | 470 | setobjs2s(ra+1, rb); |
| 509 | if (ttistable(rb)) { | 471 | luaV_gettable(L, rb, RKC(i), ra); |
| 510 | const TObject *v = luaH_getstr(hvalue(rb), tsvalue(rc)); | ||
| 511 | if (!ttisnil(v)) { setobj2s(ra, v); } | ||
| 512 | else | ||
| 513 | setobj2s(XRA(i), luaV_index(L, rb, rc, 0)); | ||
| 514 | } | ||
| 515 | else | ||
| 516 | setobj2s(XRA(i), luaV_getnotable(L, rb, rc, 0)); | ||
| 517 | break; | 472 | break; |
| 518 | } | 473 | } |
| 519 | case OP_ADD: { | 474 | case OP_ADD: { |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lvm.h,v 1.46 2002/08/07 19:22:39 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.47 2002/11/14 16:16:21 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 | */ |
| @@ -26,8 +26,7 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r); | |||
| 26 | int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2); | 26 | int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2); |
| 27 | const TObject *luaV_tonumber (const TObject *obj, TObject *n); | 27 | const TObject *luaV_tonumber (const TObject *obj, TObject *n); |
| 28 | int luaV_tostring (lua_State *L, StkId obj); | 28 | int luaV_tostring (lua_State *L, StkId obj); |
| 29 | const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key, | 29 | void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId val); |
| 30 | int loop); | ||
| 31 | void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val); | 30 | void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val); |
| 32 | StkId luaV_execute (lua_State *L); | 31 | StkId luaV_execute (lua_State *L); |
| 33 | void luaV_concat (lua_State *L, int total, int last); | 32 | void luaV_concat (lua_State *L, int total, int last); |
