diff options
Diffstat (limited to 'lbuiltin.c')
-rw-r--r-- | lbuiltin.c | 88 |
1 files changed, 45 insertions, 43 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.110 2000/05/24 13:54:49 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.111 2000/05/26 19:17:57 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 | */ |
@@ -438,10 +438,49 @@ void luaB_tremove (lua_State *L) { | |||
438 | } | 438 | } |
439 | 439 | ||
440 | 440 | ||
441 | static void luaB_foreachi (lua_State *L) { | ||
442 | const Hash *t = gettable(L, 1); | ||
443 | int n = (int)getnarg(L, t); | ||
444 | int i; | ||
445 | lua_Object f = luaL_functionarg(L, 2); | ||
446 | luaD_checkstack(L, 3); /* for f, key, and val */ | ||
447 | for (i=1; i<=n; i++) { | ||
448 | *(L->top++) = *f; | ||
449 | ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; | ||
450 | *(L->top++) = *luaH_getnum(t, i); | ||
451 | luaD_call(L, L->top-3, 1); | ||
452 | if (ttype(L->top-1) != TAG_NIL) | ||
453 | return; | ||
454 | L->top--; /* remove nil result */ | ||
455 | } | ||
456 | } | ||
457 | |||
458 | |||
459 | static void luaB_foreach (lua_State *L) { | ||
460 | const Hash *a = gettable(L, 1); | ||
461 | lua_Object f = luaL_functionarg(L, 2); | ||
462 | int i; | ||
463 | luaD_checkstack(L, 3); /* for f, key, and val */ | ||
464 | for (i=0; i<a->size; i++) { | ||
465 | const Node *nd = &(a->node[i]); | ||
466 | if (ttype(val(nd)) != TAG_NIL) { | ||
467 | *(L->top++) = *f; | ||
468 | *(L->top++) = *key(nd); | ||
469 | *(L->top++) = *val(nd); | ||
470 | luaD_call(L, L->top-3, 1); | ||
471 | if (ttype(L->top-1) != TAG_NIL) | ||
472 | return; | ||
473 | L->top--; /* remove result */ | ||
474 | } | ||
475 | } | ||
476 | } | ||
477 | |||
478 | |||
441 | /* | 479 | /* |
442 | ** {====================================================== | 480 | ** {====================================================== |
443 | ** Quicksort | 481 | ** Quicksort |
444 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; Addison-Wesley, 1993.) | 482 | ** (based on `Algorithms in MODULA-3', Robert Sedgewick; |
483 | ** Addison-Wesley, 1993.) | ||
445 | */ | 484 | */ |
446 | 485 | ||
447 | static void swap (lua_State *L, Hash *a, int i, int j) { | 486 | static void swap (lua_State *L, Hash *a, int i, int j) { |
@@ -539,43 +578,6 @@ void luaB_sort (lua_State *L) { | |||
539 | 578 | ||
540 | #ifdef LUA_DEPRECATETFUNCS | 579 | #ifdef LUA_DEPRECATETFUNCS |
541 | 580 | ||
542 | static void luaB_foreachi (lua_State *L) { | ||
543 | const Hash *t = gettable(L, 1); | ||
544 | int n = (int)getnarg(L, t); | ||
545 | int i; | ||
546 | lua_Object f = luaL_functionarg(L, 2); | ||
547 | luaD_checkstack(L, 3); /* for f, key, and val */ | ||
548 | for (i=1; i<=n; i++) { | ||
549 | *(L->top++) = *f; | ||
550 | ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; | ||
551 | *(L->top++) = *luaH_getnum(t, i); | ||
552 | luaD_call(L, L->top-3, 1); | ||
553 | if (ttype(L->top-1) != TAG_NIL) | ||
554 | return; | ||
555 | L->top--; /* remove nil result */ | ||
556 | } | ||
557 | } | ||
558 | |||
559 | |||
560 | static void luaB_foreach (lua_State *L) { | ||
561 | const Hash *a = gettable(L, 1); | ||
562 | lua_Object f = luaL_functionarg(L, 2); | ||
563 | int i; | ||
564 | luaD_checkstack(L, 3); /* for f, key, and val */ | ||
565 | for (i=0; i<a->size; i++) { | ||
566 | const Node *nd = &(a->node[i]); | ||
567 | if (ttype(val(nd)) != TAG_NIL) { | ||
568 | *(L->top++) = *f; | ||
569 | *(L->top++) = *key(nd); | ||
570 | *(L->top++) = *val(nd); | ||
571 | luaD_call(L, L->top-3, 1); | ||
572 | if (ttype(L->top-1) != TAG_NIL) | ||
573 | return; | ||
574 | L->top--; /* remove result */ | ||
575 | } | ||
576 | } | ||
577 | } | ||
578 | |||
579 | #define num_deprecated 4 | 581 | #define num_deprecated 4 |
580 | 582 | ||
581 | static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { | 583 | static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { |
@@ -587,8 +589,6 @@ static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { | |||
587 | 589 | ||
588 | 590 | ||
589 | static const struct luaL_reg other_deprecated_global_funcs[] = { | 591 | static const struct luaL_reg other_deprecated_global_funcs[] = { |
590 | {"foreach", luaB_foreach}, | ||
591 | {"foreachi", luaB_foreachi}, | ||
592 | {"rawgettable", luaB_rawget}, | 592 | {"rawgettable", luaB_rawget}, |
593 | {"rawsettable", luaB_rawset} | 593 | {"rawsettable", luaB_rawset} |
594 | }; | 594 | }; |
@@ -618,10 +618,10 @@ static void obsolete_func (lua_State *L) { | |||
618 | } | 618 | } |
619 | 619 | ||
620 | 620 | ||
621 | #define num_deprecated 8 | 621 | #define num_deprecated 6 |
622 | 622 | ||
623 | static const char *const obsolete_names [num_deprecated] = { | 623 | static const char *const obsolete_names [num_deprecated] = { |
624 | "foreach", "foreachi", "foreachvar", "nextvar", "rawgetglobal", | 624 | "foreachvar", "nextvar", "rawgetglobal", |
625 | "rawgettable", "rawsetglobal", "rawsettable" | 625 | "rawgettable", "rawsetglobal", "rawsettable" |
626 | }; | 626 | }; |
627 | 627 | ||
@@ -648,6 +648,8 @@ static const struct luaL_reg builtin_funcs[] = { | |||
648 | {"dofile", luaB_dofile}, | 648 | {"dofile", luaB_dofile}, |
649 | {"dostring", luaB_dostring}, | 649 | {"dostring", luaB_dostring}, |
650 | {"error", luaB_error}, | 650 | {"error", luaB_error}, |
651 | {"foreach", luaB_foreach}, | ||
652 | {"foreachi", luaB_foreachi}, | ||
651 | {"getglobal", luaB_getglobal}, | 653 | {"getglobal", luaB_getglobal}, |
652 | {"gettagmethod", luaB_gettagmethod}, | 654 | {"gettagmethod", luaB_gettagmethod}, |
653 | {"globals", luaB_globals}, | 655 | {"globals", luaB_globals}, |