diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-24 10:08:45 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2002-06-24 10:08:45 -0300 |
commit | e34f282365d72ddbd2000f789cd804dbbd433773 (patch) | |
tree | 9071862c6c42fb2020830da5876f112f4f8f6931 | |
parent | 3941af53adee868e2cccfb9b85783aba9ac311c1 (diff) | |
download | lua-e34f282365d72ddbd2000f789cd804dbbd433773.tar.gz lua-e34f282365d72ddbd2000f789cd804dbbd433773.tar.bz2 lua-e34f282365d72ddbd2000f789cd804dbbd433773.zip |
`luaV_gettable' returns element position
-rw-r--r-- | lapi.c | 6 | ||||
-rw-r--r-- | lobject.h | 4 | ||||
-rw-r--r-- | lvm.c | 62 | ||||
-rw-r--r-- | lvm.h | 4 |
4 files changed, 38 insertions, 38 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.200 2002/06/18 15:19:27 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.201 2002/06/20 20:41:46 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 | */ |
@@ -423,9 +423,11 @@ LUA_API void lua_pushudataval (lua_State *L, void *p) { | |||
423 | 423 | ||
424 | LUA_API void lua_gettable (lua_State *L, int index) { | 424 | LUA_API void lua_gettable (lua_State *L, int index) { |
425 | StkId t; | 425 | StkId t; |
426 | const TObject *v; | ||
426 | lua_lock(L); | 427 | lua_lock(L); |
427 | t = luaA_index(L, index); | 428 | t = luaA_index(L, index); |
428 | luaV_gettable(L, t, L->top-1, L->top-1); | 429 | v = luaV_gettable(L, t, L->top-1); |
430 | setobj(L->top - 1, v); | ||
429 | lua_unlock(L); | 431 | lua_unlock(L); |
430 | } | 432 | } |
431 | 433 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.h,v 1.135 2002/06/13 13:39:55 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 1.136 2002/06/20 20:41:46 roberto Exp roberto $ |
3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -74,7 +74,7 @@ typedef struct lua_TObject { | |||
74 | #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) | 74 | #define setnilvalue(obj) ((obj)->tt=LUA_TNIL) |
75 | 75 | ||
76 | #define setobj(obj1,obj2) \ | 76 | #define setobj(obj1,obj2) \ |
77 | { TObject *o1=(obj1); const TObject *o2=(obj2); \ | 77 | { const TObject *o2=(obj2); TObject *o1=(obj1); \ |
78 | o1->tt=o2->tt; o1->value = o2->value; } | 78 | o1->tt=o2->tt; o1->value = o2->value; } |
79 | 79 | ||
80 | #define setttype(obj, tt) (ttype(obj) = (tt)) | 80 | #define setttype(obj, tt) (ttype(obj) = (tt)) |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.239 2002/06/14 17:21:32 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.240 2002/06/20 20:41:46 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 | */ |
@@ -84,16 +84,14 @@ static void traceexec (lua_State *L) { | |||
84 | 84 | ||
85 | 85 | ||
86 | static void callTMres (lua_State *L, const TObject *f, | 86 | static void callTMres (lua_State *L, const TObject *f, |
87 | const TObject *p1, const TObject *p2, TObject *result ) { | 87 | const TObject *p1, const TObject *p2) { |
88 | ptrdiff_t res = savestack(L, result); | ||
89 | setobj(L->top, f); /* push function */ | 88 | setobj(L->top, f); /* push function */ |
90 | setobj(L->top+1, p1); /* 1st argument */ | 89 | setobj(L->top+1, p1); /* 1st argument */ |
91 | setobj(L->top+2, p2); /* 2nd argument */ | 90 | setobj(L->top+2, p2); /* 2nd argument */ |
92 | luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ | 91 | luaD_checkstack(L, 3); /* cannot check before (could invalidate p1, p2) */ |
93 | L->top += 3; | 92 | L->top += 3; |
94 | luaD_call(L, L->top - 3, 1); | 93 | luaD_call(L, L->top - 3, 1); |
95 | result = restorestack(L, res); /* previous call may change stack */ | 94 | L->top--; /* result will be in L->top */ |
96 | setobj(result, --L->top); /* get result */ | ||
97 | } | 95 | } |
98 | 96 | ||
99 | 97 | ||
@@ -115,32 +113,29 @@ static void callTM (lua_State *L, const TObject *f, | |||
115 | ** Receives the table at `t' and the key at `key'. | 113 | ** Receives the table at `t' and the key at `key'. |
116 | ** leaves the result at `res'. | 114 | ** leaves the result at `res'. |
117 | */ | 115 | */ |
118 | void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res) { | 116 | const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key) { |
119 | const TObject *tm; | 117 | const TObject *tm; |
120 | int loop = 0; | 118 | int loop = 0; |
121 | do { | 119 | do { |
122 | if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ | 120 | if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ |
123 | Table *h = hvalue(t); | 121 | Table *h = hvalue(t); |
124 | Table *et = h->metatable; | 122 | const TObject *v = luaH_get(h, key); /* do a primitive get */ |
125 | if ((tm = fasttm(L, et, TM_GETTABLE)) == NULL) { /* no gettable TM? */ | 123 | if (ttype(v) != LUA_TNIL || /* result is no nil? */ |
126 | const TObject *v = luaH_get(h, key); /* do a primitive get */ | 124 | (tm = fasttm(L, h->metatable, TM_INDEX)) == NULL) { /* or no TM? */ |
127 | if (ttype(v) != LUA_TNIL || /* result is no nil ... */ | 125 | return v; |
128 | (tm = fasttm(L, et, TM_INDEX)) == NULL) { /* ... or no index TM? */ | ||
129 | setobj(res, v); /* default get */ | ||
130 | return; | ||
131 | } | ||
132 | } | 126 | } |
133 | /* else will try the tag method */ | 127 | /* else will try the tag method */ |
134 | } | 128 | } |
135 | else if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) | 129 | else if (ttype(tm = luaT_gettmbyobj(L, t, TM_GETTABLE)) == LUA_TNIL) |
136 | luaG_typeerror(L, t, "index"); | 130 | luaG_typeerror(L, t, "index"); |
137 | if (ttype(tm) == LUA_TFUNCTION) { | 131 | if (ttype(tm) == LUA_TFUNCTION) { |
138 | callTMres(L, tm, t, key, res); | 132 | callTMres(L, tm, t, key); |
139 | return; | 133 | return L->top; |
140 | } | 134 | } |
141 | t = tm; /* else repeat access with `tm' */ | 135 | t = tm; /* else repeat access with `tm' */ |
142 | } while (++loop <= MAXTAGLOOP); | 136 | } while (++loop <= MAXTAGLOOP); |
143 | luaG_runerror(L, "loop in gettable"); | 137 | luaG_runerror(L, "loop in gettable"); |
138 | return NULL; /* to avoid warnings */ | ||
144 | } | 139 | } |
145 | 140 | ||
146 | 141 | ||
@@ -153,14 +148,11 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) { | |||
153 | do { | 148 | do { |
154 | if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ | 149 | if (ttype(t) == LUA_TTABLE) { /* `t' is a table? */ |
155 | Table *h = hvalue(t); | 150 | Table *h = hvalue(t); |
156 | Table *et = h->metatable; | 151 | TObject *oldval = luaH_set(L, h, key); /* do a primitive set */ |
157 | if ((tm = fasttm(L, et, TM_SETTABLE)) == NULL) { /* no settable TM? */ | 152 | if (ttype(oldval) != LUA_TNIL || /* result is no nil? */ |
158 | TObject *oldval = luaH_set(L, h, key); /* do a primitive set */ | 153 | (tm = fasttm(L, h->metatable, TM_NEWINDEX)) == NULL) { /* or no TM? */ |
159 | if (ttype(oldval) != LUA_TNIL || /* result is no nil ... */ | 154 | setobj(oldval, val); |
160 | (tm = fasttm(L, et, TM_NEWINDEX)) == NULL) { /* ... or no TM? */ | 155 | return; |
161 | setobj(oldval, val); | ||
162 | return; | ||
163 | } | ||
164 | } | 156 | } |
165 | /* else will try the tag method */ | 157 | /* else will try the tag method */ |
166 | } | 158 | } |
@@ -178,11 +170,14 @@ void luaV_settable (lua_State *L, const TObject *t, TObject *key, StkId val) { | |||
178 | 170 | ||
179 | static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, | 171 | static int call_binTM (lua_State *L, const TObject *p1, const TObject *p2, |
180 | TObject *res, TMS event) { | 172 | TObject *res, TMS event) { |
173 | ptrdiff_t result = savestack(L, res); | ||
181 | const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ | 174 | const TObject *tm = luaT_gettmbyobj(L, p1, event); /* try first operand */ |
182 | if (ttype(tm) == LUA_TNIL) | 175 | if (ttype(tm) == LUA_TNIL) |
183 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ | 176 | tm = luaT_gettmbyobj(L, p2, event); /* try second operand */ |
184 | if (ttype(tm) != LUA_TFUNCTION) return 0; | 177 | if (ttype(tm) != LUA_TFUNCTION) return 0; |
185 | callTMres(L, tm, p1, p2, res); | 178 | callTMres(L, tm, p1, p2); |
179 | res = restorestack(L, result); /* previous call may change stack */ | ||
180 | setobj(res, L->top); | ||
186 | return 1; | 181 | return 1; |
187 | } | 182 | } |
188 | 183 | ||
@@ -267,7 +262,7 @@ int luaV_equalval (lua_State *L, const TObject *t1, const TObject *t2) { | |||
267 | return 0; /* no TM */ | 262 | return 0; /* no TM */ |
268 | else break; /* will try TM */ | 263 | else break; /* will try TM */ |
269 | } | 264 | } |
270 | callTMres(L, tm, t1, t2, L->top); /* call TM */ | 265 | callTMres(L, tm, t1, t2); /* call TM */ |
271 | return !l_isfalse(L->top); | 266 | return !l_isfalse(L->top); |
272 | } | 267 | } |
273 | 268 | ||
@@ -308,14 +303,17 @@ void luaV_concat (lua_State *L, int total, int last) { | |||
308 | static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) { | 303 | static void powOp (lua_State *L, StkId ra, StkId rb, StkId rc) { |
309 | const TObject *b = rb; | 304 | const TObject *b = rb; |
310 | const TObject *c = rc; | 305 | const TObject *c = rc; |
306 | ptrdiff_t res = savestack(L, ra); | ||
311 | TObject tempb, tempc; | 307 | TObject tempb, tempc; |
312 | if (tonumber(b, &tempb) && tonumber(c, &tempc)) { | 308 | if (tonumber(b, &tempb) && tonumber(c, &tempc)) { |
313 | TObject f, o; | 309 | TObject f, o; |
314 | setsvalue(&o, luaS_newliteral(L, "pow")); | 310 | setsvalue(&o, luaS_newliteral(L, "pow")); |
315 | luaV_gettable(L, gt(L), &o, &f); | 311 | f = *luaV_gettable(L, gt(L), &o); |
316 | if (ttype(&f) != LUA_TFUNCTION) | 312 | if (ttype(&f) != LUA_TFUNCTION) |
317 | luaG_runerror(L, "`pow' (for `^' operator) is not a function"); | 313 | luaG_runerror(L, "`pow' (for `^' operator) is not a function"); |
318 | callTMres(L, &f, b, c, ra); | 314 | callTMres(L, &f, b, c); |
315 | ra = restorestack(L, res); /* previous call may change stack */ | ||
316 | setobj(ra, L->top); | ||
319 | } | 317 | } |
320 | else | 318 | else |
321 | call_arith(L, rb, rc, ra, TM_POW); | 319 | call_arith(L, rb, rc, ra, TM_POW); |
@@ -402,11 +400,11 @@ StkId luaV_execute (lua_State *L) { | |||
402 | } | 400 | } |
403 | case OP_GETGLOBAL: { | 401 | case OP_GETGLOBAL: { |
404 | lua_assert(ttype(KBx(i)) == LUA_TSTRING && ttype(&cl->g) == LUA_TTABLE); | 402 | lua_assert(ttype(KBx(i)) == LUA_TSTRING && ttype(&cl->g) == LUA_TTABLE); |
405 | luaV_gettable(L, &cl->g, KBx(i), ra); | 403 | setobj(RA(i), luaV_gettable(L, &cl->g, KBx(i))); |
406 | break; | 404 | break; |
407 | } | 405 | } |
408 | case OP_GETTABLE: { | 406 | case OP_GETTABLE: { |
409 | luaV_gettable(L, RB(i), RKC(i), ra); | 407 | setobj(RA(i), luaV_gettable(L, RB(i), RKC(i))); |
410 | break; | 408 | break; |
411 | } | 409 | } |
412 | case OP_SETGLOBAL: { | 410 | case OP_SETGLOBAL: { |
@@ -433,7 +431,7 @@ StkId luaV_execute (lua_State *L) { | |||
433 | case OP_SELF: { | 431 | case OP_SELF: { |
434 | StkId rb = RB(i); | 432 | StkId rb = RB(i); |
435 | setobj(ra+1, rb); | 433 | setobj(ra+1, rb); |
436 | luaV_gettable(L, rb, RKC(i), ra); | 434 | setobj(RA(i), luaV_gettable(L, rb, RKC(i))); |
437 | break; | 435 | break; |
438 | } | 436 | } |
439 | case OP_ADD: { | 437 | case OP_ADD: { |
@@ -608,7 +606,7 @@ StkId luaV_execute (lua_State *L) { | |||
608 | if (ttype(ra) == LUA_TTABLE) { | 606 | if (ttype(ra) == LUA_TTABLE) { |
609 | setobj(ra+1, ra); | 607 | setobj(ra+1, ra); |
610 | setsvalue(ra, luaS_new(L, "next")); | 608 | setsvalue(ra, luaS_new(L, "next")); |
611 | luaV_gettable(L, gt(L), ra, ra); | 609 | setobj(RA(i), luaV_gettable(L, gt(L), ra)); |
612 | } | 610 | } |
613 | dojump(pc, GETARG_sBx(i)); | 611 | dojump(pc, GETARG_sBx(i)); |
614 | break; | 612 | break; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 1.41 2002/06/12 14:51:31 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.42 2002/06/13 13:39:55 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,7 +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, TObject *obj); | 28 | int luaV_tostring (lua_State *L, TObject *obj); |
29 | void luaV_gettable (lua_State *L, const TObject *t, TObject *key, StkId res); | 29 | const TObject *luaV_gettable (lua_State *L, const TObject *t, TObject *key); |
30 | 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); |
31 | StkId luaV_execute (lua_State *L); | 31 | StkId luaV_execute (lua_State *L); |
32 | void luaV_concat (lua_State *L, int total, int last); | 32 | void luaV_concat (lua_State *L, int total, int last); |