diff options
| -rw-r--r-- | lbuiltin.c | 69 | ||||
| -rw-r--r-- | ltable.c | 9 | ||||
| -rw-r--r-- | ltable.h | 4 |
3 files changed, 43 insertions, 39 deletions
| @@ -34,7 +34,7 @@ | |||
| 34 | */ | 34 | */ |
| 35 | 35 | ||
| 36 | 36 | ||
| 37 | static void pushstring (TaggedString *s) { | 37 | static void pushtagstring (TaggedString *s) { |
| 38 | TObject o; | 38 | TObject o; |
| 39 | o.ttype = LUA_T_STRING; | 39 | o.ttype = LUA_T_STRING; |
| 40 | o.value.ts = s; | 40 | o.value.ts = s; |
| @@ -42,15 +42,14 @@ static void pushstring (TaggedString *s) { | |||
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | 44 | ||
| 45 | static real getsize (TObject *t) { | 45 | static real getsize (Hash *h) { |
| 46 | real max = 0; | 46 | real max = 0; |
| 47 | int i; | 47 | int i; |
| 48 | Hash *h = avalue(t); | ||
| 49 | LUA_ASSERT(ttype(t) == LUA_T_ARRAY, "table expected"); | 48 | LUA_ASSERT(ttype(t) == LUA_T_ARRAY, "table expected"); |
| 50 | for (i = 0; i<nhash(h); i++) { | 49 | for (i = 0; i<nhash(h); i++) { |
| 51 | Node *n = h->node+i; | 50 | Node *n = h->node+i; |
| 52 | if (ttype(ref(n)) == LUA_T_NUMBER && | 51 | if (ttype(ref(n)) == LUA_T_NUMBER && |
| 53 | ttype(val(n)) != LUA_T_NIL && | 52 | ttype(val(n)) != LUA_T_NIL && |
| 54 | nvalue(ref(n)) > max) | 53 | nvalue(ref(n)) > max) |
| 55 | max = nvalue(ref(n)); | 54 | max = nvalue(ref(n)); |
| 56 | } | 55 | } |
| @@ -58,20 +57,32 @@ static real getsize (TObject *t) { | |||
| 58 | } | 57 | } |
| 59 | 58 | ||
| 60 | 59 | ||
| 61 | static real getnarg (lua_Object table) { | 60 | static real getnarg (Hash *a) { |
| 62 | lua_Object temp; | 61 | TObject index; |
| 63 | /* temp = table.n */ | 62 | TObject *value; |
| 64 | lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable(); | 63 | /* value = table.n */ |
| 65 | return (lua_isnumber(temp) ? lua_getnumber(temp) : | 64 | ttype(&index) = LUA_T_STRING; |
| 66 | getsize(luaA_Address(table))); | 65 | tsvalue(&index) = luaS_new("n"); |
| 66 | value = luaH_get(a, &index); | ||
| 67 | return (ttype(value) == LUA_T_NUMBER) ? nvalue(value) : getsize(a); | ||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | static Hash *gethash (int arg) { | ||
| 72 | return avalue(luaA_Address(luaL_tablearg(arg))); | ||
| 67 | } | 73 | } |
| 68 | 74 | ||
| 69 | 75 | ||
| 70 | static void luaB_getn (void) { | 76 | static void luaB_getn (void) { |
| 71 | lua_pushnumber(getnarg(luaL_tablearg(1))); | 77 | lua_pushnumber(getnarg(gethash(1))); |
| 72 | } | 78 | } |
| 73 | 79 | ||
| 74 | 80 | ||
| 81 | static void tablemove (Hash *a, int from, int to) { | ||
| 82 | TObject temp = *luaH_getint(a, from); | ||
| 83 | luaH_setint(a, to, &temp); | ||
| 84 | } | ||
| 85 | |||
| 75 | 86 | ||
| 76 | /* | 87 | /* |
| 77 | ** ======================================================= | 88 | ** ======================================================= |
| @@ -136,8 +147,8 @@ static void luaB_tonumber (void) { | |||
| 136 | int base = luaL_opt_int(2, 10); | 147 | int base = luaL_opt_int(2, 10); |
| 137 | if (base == 10) { /* standard conversion */ | 148 | if (base == 10) { /* standard conversion */ |
| 138 | lua_Object o = lua_getparam(1); | 149 | lua_Object o = lua_getparam(1); |
| 139 | if (lua_isnumber(o)) | 150 | if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o)); |
| 140 | lua_pushnumber(lua_getnumber(o)); | 151 | else lua_pushnil(); /* not a number */ |
| 141 | } | 152 | } |
| 142 | else { | 153 | else { |
| 143 | char *s = luaL_check_string(1); | 154 | char *s = luaL_check_string(1); |
| @@ -262,7 +273,7 @@ static void luaB_dofile (void) { | |||
| 262 | 273 | ||
| 263 | static void luaB_call (void) { | 274 | static void luaB_call (void) { |
| 264 | lua_Object f = luaL_nonnullarg(1); | 275 | lua_Object f = luaL_nonnullarg(1); |
| 265 | lua_Object arg = luaL_tablearg(2); | 276 | Hash *arg = gethash(2); |
| 266 | char *options = luaL_opt_string(3, ""); | 277 | char *options = luaL_opt_string(3, ""); |
| 267 | lua_Object err = lua_getparam(4); | 278 | lua_Object err = lua_getparam(4); |
| 268 | int narg = (int)getnarg(arg); | 279 | int narg = (int)getnarg(arg); |
| @@ -274,7 +285,7 @@ static void luaB_call (void) { | |||
| 274 | /* push arg[1...n] */ | 285 | /* push arg[1...n] */ |
| 275 | luaD_checkstack(narg); | 286 | luaD_checkstack(narg); |
| 276 | for (i=0; i<narg; i++) | 287 | for (i=0; i<narg; i++) |
| 277 | *(L->stack.top++) = *luaH_getint(avalue(luaA_Address(arg)), i+1); | 288 | *(L->stack.top++) = *luaH_getint(arg, i+1); |
| 278 | status = lua_callfunction(f); | 289 | status = lua_callfunction(f); |
| 279 | if (err != LUA_NOOBJECT) { /* restore old error method */ | 290 | if (err != LUA_NOOBJECT) { /* restore old error method */ |
| 280 | lua_pushobject(err); | 291 | lua_pushobject(err); |
| @@ -314,11 +325,10 @@ static void luaB_assert (void) { | |||
| 314 | 325 | ||
| 315 | 326 | ||
| 316 | static void luaB_foreachi (void) { | 327 | static void luaB_foreachi (void) { |
| 317 | lua_Object ot = luaL_tablearg(1); | 328 | Hash *t = gethash(1); |
| 318 | Hash *t = avalue(luaA_Address(ot)); | ||
| 319 | TObject f = *luaA_Address(luaL_functionarg(2)); | 329 | TObject f = *luaA_Address(luaL_functionarg(2)); |
| 320 | int i; | 330 | int i; |
| 321 | int n = (int)getnarg(ot); | 331 | int n = (int)getnarg(t); |
| 322 | luaD_checkstack(3); /* for f, ref, and val */ | 332 | luaD_checkstack(3); /* for f, ref, and val */ |
| 323 | for (i=1; i<=n; i++) { | 333 | for (i=1; i<=n; i++) { |
| 324 | *(L->stack.top++) = f; | 334 | *(L->stack.top++) = f; |
| @@ -334,12 +344,12 @@ static void luaB_foreachi (void) { | |||
| 334 | 344 | ||
| 335 | 345 | ||
| 336 | static void luaB_foreach (void) { | 346 | static void luaB_foreach (void) { |
| 337 | TObject t = *luaA_Address(luaL_tablearg(1)); | 347 | Hash *a = gethash(1); |
| 338 | TObject f = *luaA_Address(luaL_functionarg(2)); | 348 | TObject f = *luaA_Address(luaL_functionarg(2)); |
| 339 | int i; | 349 | int i; |
| 340 | luaD_checkstack(3); /* for f, ref, and val */ | 350 | luaD_checkstack(3); /* for f, ref, and val */ |
| 341 | for (i=0; i<avalue(&t)->nhash; i++) { | 351 | for (i=0; i<a->nhash; i++) { |
| 342 | Node *nd = &(avalue(&t)->node[i]); | 352 | Node *nd = &(a->node[i]); |
| 343 | if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) { | 353 | if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) { |
| 344 | *(L->stack.top++) = f; | 354 | *(L->stack.top++) = f; |
| 345 | *(L->stack.top++) = *ref(nd); | 355 | *(L->stack.top++) = *ref(nd); |
| @@ -366,7 +376,7 @@ static void luaB_foreachvar (void) { | |||
| 366 | ttype(L->stack.stack+name) = LUA_T_STRING; | 376 | ttype(L->stack.stack+name) = LUA_T_STRING; |
| 367 | tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */ | 377 | tsvalue(L->stack.stack+name) = s; /* keep s on stack to avoid GC */ |
| 368 | *(L->stack.top++) = f; | 378 | *(L->stack.top++) = f; |
| 369 | pushstring(s); | 379 | pushtagstring(s); |
| 370 | *(L->stack.top++) = s->u.s.globalval; | 380 | *(L->stack.top++) = s->u.s.globalval; |
| 371 | luaD_calln(2, 1); | 381 | luaD_calln(2, 1); |
| 372 | if (ttype(L->stack.top-1) != LUA_T_NIL) | 382 | if (ttype(L->stack.top-1) != LUA_T_NIL) |
| @@ -377,7 +387,6 @@ static void luaB_foreachvar (void) { | |||
| 377 | } | 387 | } |
| 378 | 388 | ||
| 379 | 389 | ||
| 380 | |||
| 381 | /* | 390 | /* |
| 382 | ** Quicksort algorithm from "Programming Pearls", pg. 112 | 391 | ** Quicksort algorithm from "Programming Pearls", pg. 112 |
| 383 | */ | 392 | */ |
| @@ -386,8 +395,7 @@ static void swap (Hash *a, int i, int j) { | |||
| 386 | /* notice: must use two temporary vars, because luaH_setint may cause a | 395 | /* notice: must use two temporary vars, because luaH_setint may cause a |
| 387 | rehash and change the addresses of values in the array */ | 396 | rehash and change the addresses of values in the array */ |
| 388 | TObject ai = *luaH_getint(a, i); | 397 | TObject ai = *luaH_getint(a, i); |
| 389 | TObject aj = *luaH_getint(a, j); | 398 | tablemove(a, j, i); |
| 390 | luaH_setint(a, i, &aj); | ||
| 391 | luaH_setint(a, j, &ai); | 399 | luaH_setint(a, j, &ai); |
| 392 | } | 400 | } |
| 393 | 401 | ||
| @@ -442,9 +450,9 @@ static void auxsort (Hash *a, int l, int u, TObject *f) { | |||
| 442 | } | 450 | } |
| 443 | 451 | ||
| 444 | static void luaB_sort (void) { | 452 | static void luaB_sort (void) { |
| 445 | lua_Object t = luaL_tablearg(1); | 453 | lua_Object t = lua_getparam(1); |
| 446 | int n = (int)getnarg(t); | 454 | Hash *a = gethash(1); |
| 447 | Hash *a = avalue(luaA_Address(t)); | 455 | int n = (int)getnarg(a); |
| 448 | lua_Object func = lua_getparam(2); | 456 | lua_Object func = lua_getparam(2); |
| 449 | TObject *f = luaA_Address(func); | 457 | TObject *f = luaA_Address(func); |
| 450 | luaL_arg_check(!f || lua_isfunction(func), 2, "function expected"); | 458 | luaL_arg_check(!f || lua_isfunction(func), 2, "function expected"); |
| @@ -478,7 +486,7 @@ static void luaB_nextvar (void) { | |||
| 478 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ | 486 | while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ |
| 479 | g = (TaggedString *)g->head.next; | 487 | g = (TaggedString *)g->head.next; |
| 480 | if (g) { | 488 | if (g) { |
| 481 | pushstring(g); | 489 | pushtagstring(g); |
| 482 | luaA_pushobject(&g->u.s.globalval); | 490 | luaA_pushobject(&g->u.s.globalval); |
| 483 | } | 491 | } |
| 484 | else lua_pushnil(); /* no more globals */ | 492 | else lua_pushnil(); /* no more globals */ |
| @@ -486,8 +494,7 @@ static void luaB_nextvar (void) { | |||
| 486 | 494 | ||
| 487 | 495 | ||
| 488 | static void luaB_next (void) { | 496 | static void luaB_next (void) { |
| 489 | Node *n = luaH_next(luaA_Address(luaL_tablearg(1)), | 497 | Node *n = luaH_next(gethash(1), luaA_Address(luaL_nonnullarg(2))); |
| 490 | luaA_Address(luaL_nonnullarg(2))); | ||
| 491 | if (n) { | 498 | if (n) { |
| 492 | luaA_pushobject(&n->ref); | 499 | luaA_pushobject(&n->ref); |
| 493 | luaA_pushobject(&n->val); | 500 | luaA_pushobject(&n->val); |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.c,v 1.14 1998/08/10 21:36:32 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.15 1998/08/11 16:38:34 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 | */ |
| @@ -181,8 +181,7 @@ TObject *luaH_set (Hash *t, TObject *ref) | |||
| 181 | } | 181 | } |
| 182 | 182 | ||
| 183 | 183 | ||
| 184 | static Node *hashnext (Hash *t, int i) | 184 | static Node *hashnext (Hash *t, int i) { |
| 185 | { | ||
| 186 | Node *n; | 185 | Node *n; |
| 187 | int tsize = nhash(t); | 186 | int tsize = nhash(t); |
| 188 | if (i >= tsize) | 187 | if (i >= tsize) |
| @@ -196,9 +195,7 @@ static Node *hashnext (Hash *t, int i) | |||
| 196 | return node(t, i); | 195 | return node(t, i); |
| 197 | } | 196 | } |
| 198 | 197 | ||
| 199 | Node *luaH_next (TObject *o, TObject *r) | 198 | Node *luaH_next (Hash *t, TObject *r) { |
| 200 | { | ||
| 201 | Hash *t = avalue(o); | ||
| 202 | if (ttype(r) == LUA_T_NIL) | 199 | if (ttype(r) == LUA_T_NIL) |
| 203 | return hashnext(t, 0); | 200 | return hashnext(t, 0); |
| 204 | else { | 201 | else { |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltable.h,v 1.5 1997/11/26 18:53:45 roberto Exp roberto $ | 2 | ** $Id: ltable.h,v 1.6 1998/07/12 16:15:19 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 | */ |
| @@ -19,7 +19,7 @@ Hash *luaH_new (int nhash); | |||
| 19 | void luaH_free (Hash *frees); | 19 | void luaH_free (Hash *frees); |
| 20 | TObject *luaH_get (Hash *t, TObject *ref); | 20 | TObject *luaH_get (Hash *t, TObject *ref); |
| 21 | TObject *luaH_set (Hash *t, TObject *ref); | 21 | TObject *luaH_set (Hash *t, TObject *ref); |
| 22 | Node *luaH_next (TObject *o, TObject *r); | 22 | Node *luaH_next (Hash *t, TObject *r); |
| 23 | void luaH_setint (Hash *t, int ref, TObject *val); | 23 | void luaH_setint (Hash *t, int ref, TObject *val); |
| 24 | TObject *luaH_getint (Hash *t, int ref); | 24 | TObject *luaH_getint (Hash *t, int ref); |
| 25 | 25 | ||
