diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-06-06 13:31:41 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-06-06 13:31:41 -0300 |
commit | 8bcf6228765e56be19feb90c8805cc2fb2223188 (patch) | |
tree | ce13072efce7a8aadcdde8de747073f3d7bf0859 | |
parent | dbbf6c073b4203169a0221a082f6d00ccb27754c (diff) | |
download | lua-8bcf6228765e56be19feb90c8805cc2fb2223188.tar.gz lua-8bcf6228765e56be19feb90c8805cc2fb2223188.tar.bz2 lua-8bcf6228765e56be19feb90c8805cc2fb2223188.zip |
new signature for `luaH_set'
-rw-r--r-- | lapi.c | 4 | ||||
-rw-r--r-- | lbuiltin.c | 26 | ||||
-rw-r--r-- | ltable.c | 51 | ||||
-rw-r--r-- | ltable.h | 10 | ||||
-rw-r--r-- | lvm.c | 22 | ||||
-rw-r--r-- | lvm.h | 3 |
6 files changed, 55 insertions, 61 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.81 2000/05/24 13:54:49 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.82 2000/05/26 19:17:57 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 | */ |
@@ -144,7 +144,7 @@ void lua_rawset (lua_State *L) { | |||
144 | luaA_checkCargs(L, 3); | 144 | luaA_checkCargs(L, 3); |
145 | if (ttype(L->top-3) != TAG_TABLE) | 145 | if (ttype(L->top-3) != TAG_TABLE) |
146 | lua_error(L, "indexed expression not a table"); | 146 | lua_error(L, "indexed expression not a table"); |
147 | luaH_set(L, avalue(L->top-3), L->top-2, L->top-1); | 147 | *luaH_set(L, avalue(L->top-3), L->top-2) = *(L->top-1); |
148 | L->top -= 3; | 148 | L->top -= 3; |
149 | } | 149 | } |
150 | 150 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.112 2000/06/02 19:08:56 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.113 2000/06/05 20:15:33 roberto Exp roberto $ |
3 | ** Built-in functions | 3 | ** Built-in functions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -402,6 +402,13 @@ void luaB_getn (lua_State *L) { | |||
402 | } | 402 | } |
403 | 403 | ||
404 | 404 | ||
405 | /* auxiliar function */ | ||
406 | static void t_move (lua_State *L, Hash *t, int from, int to) { | ||
407 | TObject *p = luaH_setint(L, t, to); /* may change following `get' */ | ||
408 | *p = *luaH_getnum(t, from); | ||
409 | } | ||
410 | |||
411 | |||
405 | void luaB_tinsert (lua_State *L) { | 412 | void luaB_tinsert (lua_State *L) { |
406 | Hash *a = gettable(L, 1); | 413 | Hash *a = gettable(L, 1); |
407 | lua_Object v = lua_getparam(L, 3); | 414 | lua_Object v = lua_getparam(L, 3); |
@@ -413,10 +420,10 @@ void luaB_tinsert (lua_State *L) { | |||
413 | v = luaL_nonnullarg(L, 2); | 420 | v = luaL_nonnullarg(L, 2); |
414 | pos = n+1; | 421 | pos = n+1; |
415 | } | 422 | } |
416 | luaV_setn(L, a, n+1); /* a.n = n+1 */ | 423 | luaH_setstrnum(L, a, luaS_new(L, "n"), n+1); /* a.n = n+1 */ |
417 | for (; n>=pos; n--) | 424 | for (; n>=pos; n--) |
418 | luaH_move(L, a, n, n+1); /* a[n+1] = a[n] */ | 425 | t_move(L, a, n, n+1); /* a[n+1] = a[n] */ |
419 | luaH_setint(L, a, pos, v); /* a[pos] = v */ | 426 | *luaH_setint(L, a, pos) = *v; /* a[pos] = v */ |
420 | } | 427 | } |
421 | 428 | ||
422 | 429 | ||
@@ -427,9 +434,9 @@ void luaB_tremove (lua_State *L) { | |||
427 | if (n <= 0) return; /* table is "empty" */ | 434 | if (n <= 0) return; /* table is "empty" */ |
428 | luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */ | 435 | luaA_pushobject(L, luaH_getnum(a, pos)); /* result = a[pos] */ |
429 | for ( ;pos<n; pos++) | 436 | for ( ;pos<n; pos++) |
430 | luaH_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */ | 437 | t_move(L, a, pos+1, pos); /* a[pos] = a[pos+1] */ |
431 | luaV_setn(L, a, n-1); /* a.n = n-1 */ | 438 | luaH_setstrnum(L, a, luaS_new(L, "n"), n-1); /* a.n = n-1 */ |
432 | luaH_setint(L, a, n, &luaO_nilobject); /* a[n] = nil */ | 439 | ttype(luaH_setint(L, a, n)) = TAG_NIL; /* a[n] = nil */ |
433 | } | 440 | } |
434 | 441 | ||
435 | 442 | ||
@@ -478,11 +485,12 @@ static void luaB_foreach (lua_State *L) { | |||
478 | ** Addison-Wesley, 1993.) | 485 | ** Addison-Wesley, 1993.) |
479 | */ | 486 | */ |
480 | 487 | ||
488 | |||
481 | static void swap (lua_State *L, Hash *a, int i, int j) { | 489 | static void swap (lua_State *L, Hash *a, int i, int j) { |
482 | TObject temp; | 490 | TObject temp; |
483 | temp = *luaH_getnum(a, i); | 491 | temp = *luaH_getnum(a, i); |
484 | luaH_move(L, a, j, i); | 492 | t_move(L, a, j, i); |
485 | luaH_setint(L, a, j, &temp); | 493 | *luaH_setint(L, a, j) = temp; |
486 | } | 494 | } |
487 | 495 | ||
488 | static int sort_comp (lua_State *L, lua_Object f, const TObject *a, | 496 | static int sort_comp (lua_State *L, lua_Object f, const TObject *a, |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.44 2000/06/05 20:07:53 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.45 2000/06/05 20:15:33 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -193,12 +193,12 @@ static int numuse (const Hash *t) { | |||
193 | static void rehash (lua_State *L, Hash *t) { | 193 | static void rehash (lua_State *L, Hash *t) { |
194 | int oldsize = t->size; | 194 | int oldsize = t->size; |
195 | Node *nold = t->node; | 195 | Node *nold = t->node; |
196 | int newsize = numuse(t); | 196 | int nelems = numuse(t); |
197 | int i; | 197 | int i; |
198 | LUA_ASSERT(L, newsize<=oldsize, "wrong count"); | 198 | LUA_ASSERT(L, nelems<=oldsize, "wrong count"); |
199 | if (newsize >= oldsize-oldsize/4) /* using more than 3/4? */ | 199 | if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */ |
200 | setnodevector(L, t, (lint32)oldsize*2); | 200 | setnodevector(L, t, (lint32)oldsize*2); |
201 | else if (newsize <= oldsize/4 && /* less than 1/4? */ | 201 | else if (nelems <= oldsize/4 && /* less than 1/4? */ |
202 | oldsize > MINPOWER2) | 202 | oldsize > MINPOWER2) |
203 | setnodevector(L, t, oldsize/2); | 203 | setnodevector(L, t, oldsize/2); |
204 | else | 204 | else |
@@ -207,35 +207,28 @@ static void rehash (lua_State *L, Hash *t) { | |||
207 | for (i=0; i<oldsize; i++) { | 207 | for (i=0; i<oldsize; i++) { |
208 | Node *old = nold+i; | 208 | Node *old = nold+i; |
209 | if (ttype(&old->val) != TAG_NIL) | 209 | if (ttype(&old->val) != TAG_NIL) |
210 | luaH_set(L, t, &old->key, &old->val); | 210 | *luaH_set(L, t, &old->key) = old->val; |
211 | } | 211 | } |
212 | luaM_free(L, nold); /* free old array */ | 212 | luaM_free(L, nold); /* free old array */ |
213 | } | 213 | } |
214 | 214 | ||
215 | 215 | ||
216 | /* | 216 | /* |
217 | ** sets a pair key-value in a hash table; first, check whether key is | 217 | ** inserts a key into a hash table; first, check whether key is |
218 | ** already present; if not, check whether key's main position is free; | 218 | ** already present; if not, check whether key's main position is free; |
219 | ** if not, check whether colliding node is in its main position or not; | 219 | ** if not, check whether colliding node is in its main position or not; |
220 | ** if it is not, move colliding node to an empty place and put new pair | 220 | ** if it is not, move colliding node to an empty place and put new key |
221 | ** in its main position; otherwise (colliding node is in its main position), | 221 | ** in its main position; otherwise (colliding node is in its main position), |
222 | ** new pair goes to an empty position. | 222 | ** new key goes to an empty position. |
223 | ** Tricky point: the only place where an old element is moved is when | ||
224 | ** we move the colliding node to an empty place; nevertheless, its old | ||
225 | ** value is still in that position until we set the value for the new | ||
226 | ** pair; therefore, even when `val' points to an element of this table | ||
227 | ** (this happens when we use `luaH_move'), there is no problem. | ||
228 | */ | 223 | */ |
229 | void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) { | 224 | TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) { |
230 | Node *mp = luaH_mainposition(t, key); | 225 | Node *mp = luaH_mainposition(t, key); |
231 | Node *n = mp; | 226 | Node *n = mp; |
232 | if (!mp) | 227 | if (!mp) |
233 | lua_error(L, "unexpected type to index table"); | 228 | lua_error(L, "unexpected type to index table"); |
234 | do { /* check whether `key' is somewhere in the chain */ | 229 | do { /* check whether `key' is somewhere in the chain */ |
235 | if (luaO_equalObj(key, &n->key)) { | 230 | if (luaO_equalObj(key, &n->key)) |
236 | n->val = *val; /* update value */ | 231 | return &n->val; /* that's all */ |
237 | return; /* that's all */ | ||
238 | } | ||
239 | else n = n->next; | 232 | else n = n->next; |
240 | } while (n); | 233 | } while (n); |
241 | /* `key' not found; must insert it */ | 234 | /* `key' not found; must insert it */ |
@@ -243,7 +236,7 @@ void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) { | |||
243 | Node *othern; /* main position of colliding node */ | 236 | Node *othern; /* main position of colliding node */ |
244 | n = t->firstfree; /* get a free place */ | 237 | n = t->firstfree; /* get a free place */ |
245 | /* is colliding node out of its main position? (can only happens if | 238 | /* is colliding node out of its main position? (can only happens if |
246 | its position if after "firstfree") */ | 239 | its position is after "firstfree") */ |
247 | if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) { | 240 | if (mp > n && (othern=luaH_mainposition(t, &mp->key)) != mp) { |
248 | /* yes; move colliding node into free position */ | 241 | /* yes; move colliding node into free position */ |
249 | while (othern->next != mp) othern = othern->next; /* find previous */ | 242 | while (othern->next != mp) othern = othern->next; /* find previous */ |
@@ -259,30 +252,32 @@ void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val) { | |||
259 | } | 252 | } |
260 | } | 253 | } |
261 | mp->key = *key; | 254 | mp->key = *key; |
262 | mp->val = *val; | 255 | for (;;) { /* correct `firstfree' */ |
263 | for (;;) { /* check free places */ | ||
264 | if (ttype(&t->firstfree->key) == TAG_NIL) | 256 | if (ttype(&t->firstfree->key) == TAG_NIL) |
265 | return; /* OK; table still has a free place */ | 257 | return &mp->val; /* OK; table still has a free place */ |
266 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ | 258 | else if (t->firstfree == t->node) break; /* cannot decrement from here */ |
267 | else (t->firstfree)--; | 259 | else (t->firstfree)--; |
268 | } | 260 | } |
269 | rehash(L, t); /* no more free places */ | 261 | rehash(L, t); /* no more free places */ |
262 | return luaH_set(L, t, key); /* `rehash' invalidates this insertion */ | ||
270 | } | 263 | } |
271 | 264 | ||
272 | 265 | ||
273 | void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) { | 266 | TObject *luaH_setint (lua_State *L, Hash *t, int key) { |
274 | TObject index; | 267 | TObject index; |
275 | ttype(&index) = TAG_NUMBER; | 268 | ttype(&index) = TAG_NUMBER; |
276 | nvalue(&index) = key; | 269 | nvalue(&index) = key; |
277 | luaH_set(L, t, &index, val); | 270 | return luaH_set(L, t, &index); |
278 | } | 271 | } |
279 | 272 | ||
280 | 273 | ||
281 | void luaH_setstr (lua_State *L, Hash *t, TString *key, const TObject *val) { | 274 | void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) { |
282 | TObject index; | 275 | TObject *value, index; |
283 | ttype(&index) = TAG_STRING; | 276 | ttype(&index) = TAG_STRING; |
284 | tsvalue(&index) = key; | 277 | tsvalue(&index) = key; |
285 | luaH_set(L, t, &index, val); | 278 | value = luaH_set(L, t, &index); |
279 | ttype(value) = TAG_NUMBER; | ||
280 | nvalue(value) = val; | ||
286 | } | 281 | } |
287 | 282 | ||
288 | 283 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.h,v 1.21 2000/06/05 20:07:53 roberto Exp roberto $ | 2 | ** $Id: ltable.h,v 1.22 2000/06/05 20:15:33 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -14,18 +14,16 @@ | |||
14 | #define key(n) (&(n)->key) | 14 | #define key(n) (&(n)->key) |
15 | #define val(n) (&(n)->val) | 15 | #define val(n) (&(n)->val) |
16 | 16 | ||
17 | #define luaH_move(L, t,from,to) (luaH_setint(L, t, to, luaH_getnum(t, from))) | ||
18 | |||
19 | Hash *luaH_new (lua_State *L, int nhash); | 17 | Hash *luaH_new (lua_State *L, int nhash); |
20 | void luaH_free (lua_State *L, Hash *t); | 18 | void luaH_free (lua_State *L, Hash *t); |
21 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); | 19 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); |
22 | const TObject *luaH_getnum (const Hash *t, Number key); | 20 | const TObject *luaH_getnum (const Hash *t, Number key); |
23 | const TObject *luaH_getstr (const Hash *t, TString *key); | 21 | const TObject *luaH_getstr (const Hash *t, TString *key); |
24 | void luaH_remove (Hash *t, TObject *key); | 22 | void luaH_remove (Hash *t, TObject *key); |
25 | void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); | 23 | TObject *luaH_set (lua_State *L, Hash *t, const TObject *key); |
26 | int luaH_pos (lua_State *L, const Hash *t, const TObject *r); | 24 | int luaH_pos (lua_State *L, const Hash *t, const TObject *r); |
27 | void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); | 25 | TObject *luaH_setint (lua_State *L, Hash *t, int key); |
28 | void luaH_setstr (lua_State *L, Hash *t, TString *key, const TObject *val); | 26 | void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val); |
29 | unsigned long luaH_hash (lua_State *L, const TObject *key); | 27 | unsigned long luaH_hash (lua_State *L, const TObject *key); |
30 | const TObject *luaH_getglobal (lua_State *L, const char *name); | 28 | const TObject *luaH_getglobal (lua_State *L, const char *name); |
31 | 29 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.111 2000/06/05 14:56:18 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.112 2000/06/05 20:15:33 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 | */ |
@@ -65,13 +65,6 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */ | |||
65 | } | 65 | } |
66 | 66 | ||
67 | 67 | ||
68 | void luaV_setn (lua_State *L, Hash *t, int val) { | ||
69 | TObject value; | ||
70 | ttype(&value) = TAG_NUMBER; nvalue(&value) = val; | ||
71 | luaH_setstr(L, t, luaS_new(L, "n"), &value); | ||
72 | } | ||
73 | |||
74 | |||
75 | static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) { | 68 | static Closure *luaV_closure (lua_State *L, lua_Type t, int nelems) { |
76 | Closure *c = luaF_newclosure(L, nelems); | 69 | Closure *c = luaF_newclosure(L, nelems); |
77 | L->top -= nelems; | 70 | L->top -= nelems; |
@@ -147,7 +140,7 @@ void luaV_settable (lua_State *L, StkId t, StkId top) { | |||
147 | else { /* object is a table... */ | 140 | else { /* object is a table... */ |
148 | im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE); | 141 | im = luaT_getim(L, avalue(t)->htag, IM_SETTABLE); |
149 | if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */ | 142 | if (ttype(im) == TAG_NIL) { /* and does not have a `settable' method */ |
150 | luaH_set(L, avalue(t), t+1, top-1); | 143 | *luaH_set(L, avalue(t), t+1) = *(top-1); |
151 | return; | 144 | return; |
152 | } | 145 | } |
153 | /* else it has a `settable' method, go through to next command */ | 146 | /* else it has a `settable' method, go through to next command */ |
@@ -191,7 +184,7 @@ void luaV_setglobal (lua_State *L, TString *s, StkId top) { | |||
191 | TObject key; | 184 | TObject key; |
192 | ttype(&key) = TAG_STRING; | 185 | ttype(&key) = TAG_STRING; |
193 | tsvalue(&key) = s; | 186 | tsvalue(&key) = s; |
194 | luaH_set(L, L->gt, &key, top-1); | 187 | *luaH_set(L, L->gt, &key) = *(top-1); |
195 | } | 188 | } |
196 | } | 189 | } |
197 | else { | 190 | else { |
@@ -311,8 +304,9 @@ void luaV_pack (lua_State *L, StkId firstelem, int nvararg, TObject *tab) { | |||
311 | htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */ | 304 | htab = avalue(tab) = luaH_new(L, nvararg+1); /* +1 for field `n' */ |
312 | ttype(tab) = TAG_TABLE; | 305 | ttype(tab) = TAG_TABLE; |
313 | for (i=0; i<nvararg; i++) | 306 | for (i=0; i<nvararg; i++) |
314 | luaH_setint(L, htab, i+1, firstelem+i); | 307 | *luaH_setint(L, htab, i+1) = *(firstelem+i); |
315 | luaV_setn(L, htab, nvararg); /* store counter in field `n' */ | 308 | /* store counter in field `n' */ |
309 | luaH_setstrnum(L, htab, luaS_new(L, "n"), nvararg); | ||
316 | } | 310 | } |
317 | 311 | ||
318 | 312 | ||
@@ -476,7 +470,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
476 | Hash *arr = avalue(top-n-1); | 470 | Hash *arr = avalue(top-n-1); |
477 | L->top = top-n; /* final value of `top' (in case of errors) */ | 471 | L->top = top-n; /* final value of `top' (in case of errors) */ |
478 | for (; n; n--) | 472 | for (; n; n--) |
479 | luaH_setint(L, arr, n+aux, --top); | 473 | *luaH_setint(L, arr, n+aux) = *(--top); |
480 | break; | 474 | break; |
481 | } | 475 | } |
482 | 476 | ||
@@ -486,8 +480,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
486 | Hash *arr = avalue(finaltop-1); | 480 | Hash *arr = avalue(finaltop-1); |
487 | L->top = finaltop; /* final value of `top' (in case of errors) */ | 481 | L->top = finaltop; /* final value of `top' (in case of errors) */ |
488 | for (; n; n--) { | 482 | for (; n; n--) { |
489 | luaH_set(L, arr, top-2, top-1); | ||
490 | top-=2; | 483 | top-=2; |
484 | *luaH_set(L, arr, top) = *(top+1); | ||
491 | } | 485 | } |
492 | break; | 486 | break; |
493 | } | 487 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.h,v 1.21 2000/04/19 13:36:25 roberto Exp roberto $ | 2 | ** $Id: lvm.h,v 1.22 2000/05/08 19:32:53 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 | */ |
@@ -20,7 +20,6 @@ | |||
20 | void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab); | 20 | void luaV_pack (lua_State *L, StkId firstel, int nvararg, TObject *tab); |
21 | int luaV_tonumber (TObject *obj); | 21 | int luaV_tonumber (TObject *obj); |
22 | int luaV_tostring (lua_State *L, TObject *obj); | 22 | int luaV_tostring (lua_State *L, TObject *obj); |
23 | void luaV_setn (lua_State *L, Hash *t, int val); | ||
24 | void luaV_gettable (lua_State *L, StkId top); | 23 | void luaV_gettable (lua_State *L, StkId top); |
25 | void luaV_settable (lua_State *L, StkId t, StkId top); | 24 | void luaV_settable (lua_State *L, StkId t, StkId top); |
26 | void luaV_getglobal (lua_State *L, TString *s, StkId top); | 25 | void luaV_getglobal (lua_State *L, TString *s, StkId top); |