diff options
Diffstat (limited to 'lbuiltin.c')
| -rw-r--r-- | lbuiltin.c | 144 |
1 files changed, 93 insertions, 51 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.109 2000/05/09 14:50:16 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.110 2000/05/24 13:54:49 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 | */ |
| @@ -106,7 +106,10 @@ void luaB__ALERT (lua_State *L) { | |||
| 106 | ** The library `liolib' redefines _ERRORMESSAGE for better error information. | 106 | ** The library `liolib' redefines _ERRORMESSAGE for better error information. |
| 107 | */ | 107 | */ |
| 108 | void luaB__ERRORMESSAGE (lua_State *L) { | 108 | void luaB__ERRORMESSAGE (lua_State *L) { |
| 109 | lua_Object al = lua_rawgetglobal(L, LUA_ALERT); | 109 | lua_Object al; |
| 110 | lua_pushglobaltable(L); | ||
| 111 | lua_pushstring(L, LUA_ALERT); | ||
| 112 | al = lua_rawget(L); | ||
| 110 | if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */ | 113 | if (lua_isfunction(L, al)) { /* avoid error loop if _ALERT is not defined */ |
| 111 | const char *s = luaL_check_string(L, 1); | 114 | const char *s = luaL_check_string(L, 1); |
| 112 | char *buff = luaL_openspace(L, strlen(s)+sizeof("error: \n")); | 115 | char *buff = luaL_openspace(L, strlen(s)+sizeof("error: \n")); |
| @@ -214,17 +217,17 @@ void luaB_globals (lua_State *L) { | |||
| 214 | lua_setglobaltable(L, luaL_tablearg(L, 1)); | 217 | lua_setglobaltable(L, luaL_tablearg(L, 1)); |
| 215 | } | 218 | } |
| 216 | 219 | ||
| 217 | void luaB_rawgettable (lua_State *L) { | 220 | void luaB_rawget (lua_State *L) { |
| 218 | lua_pushobject(L, luaL_nonnullarg(L, 1)); | 221 | lua_pushobject(L, luaL_nonnullarg(L, 1)); |
| 219 | lua_pushobject(L, luaL_nonnullarg(L, 2)); | 222 | lua_pushobject(L, luaL_nonnullarg(L, 2)); |
| 220 | lua_pushobject(L, lua_rawgettable(L)); | 223 | lua_pushobject(L, lua_rawget(L)); |
| 221 | } | 224 | } |
| 222 | 225 | ||
| 223 | void luaB_rawsettable (lua_State *L) { | 226 | void luaB_rawset (lua_State *L) { |
| 224 | lua_pushobject(L, luaL_nonnullarg(L, 1)); | 227 | lua_pushobject(L, luaL_nonnullarg(L, 1)); |
| 225 | lua_pushobject(L, luaL_nonnullarg(L, 2)); | 228 | lua_pushobject(L, luaL_nonnullarg(L, 2)); |
| 226 | lua_pushobject(L, luaL_nonnullarg(L, 3)); | 229 | lua_pushobject(L, luaL_nonnullarg(L, 3)); |
| 227 | lua_rawsettable(L); | 230 | lua_rawset(L); |
| 228 | } | 231 | } |
| 229 | 232 | ||
| 230 | void luaB_settagmethod (lua_State *L) { | 233 | void luaB_settagmethod (lua_State *L) { |
| @@ -399,44 +402,6 @@ void luaB_assert (lua_State *L) { | |||
| 399 | } | 402 | } |
| 400 | 403 | ||
| 401 | 404 | ||
| 402 | void luaB_foreachi (lua_State *L) { | ||
| 403 | const Hash *t = gettable(L, 1); | ||
| 404 | int n = (int)getnarg(L, t); | ||
| 405 | int i; | ||
| 406 | lua_Object f = luaL_functionarg(L, 2); | ||
| 407 | luaD_checkstack(L, 3); /* for f, key, and val */ | ||
| 408 | for (i=1; i<=n; i++) { | ||
| 409 | *(L->top++) = *f; | ||
| 410 | ttype(L->top) = TAG_NUMBER; nvalue(L->top++) = i; | ||
| 411 | *(L->top++) = *luaH_getnum(t, i); | ||
| 412 | luaD_call(L, L->top-3, 1); | ||
| 413 | if (ttype(L->top-1) != TAG_NIL) | ||
| 414 | return; | ||
| 415 | L->top--; /* remove nil result */ | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | void luaB_foreach (lua_State *L) { | ||
| 421 | const Hash *a = gettable(L, 1); | ||
| 422 | lua_Object f = luaL_functionarg(L, 2); | ||
| 423 | int i; | ||
| 424 | luaD_checkstack(L, 3); /* for f, key, and val */ | ||
| 425 | for (i=0; i<a->size; i++) { | ||
| 426 | const Node *nd = &(a->node[i]); | ||
| 427 | if (ttype(val(nd)) != TAG_NIL) { | ||
| 428 | *(L->top++) = *f; | ||
| 429 | *(L->top++) = *key(nd); | ||
| 430 | *(L->top++) = *val(nd); | ||
| 431 | luaD_call(L, L->top-3, 1); | ||
| 432 | if (ttype(L->top-1) != TAG_NIL) | ||
| 433 | return; | ||
| 434 | L->top--; /* remove result */ | ||
| 435 | } | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | |||
| 440 | void luaB_getn (lua_State *L) { | 405 | void luaB_getn (lua_State *L) { |
| 441 | lua_pushnumber(L, getnarg(L, gettable(L, 1))); | 406 | lua_pushnumber(L, getnarg(L, gettable(L, 1))); |
| 442 | } | 407 | } |
| @@ -562,21 +527,70 @@ void luaB_sort (lua_State *L) { | |||
| 562 | /* }====================================================== */ | 527 | /* }====================================================== */ |
| 563 | 528 | ||
| 564 | 529 | ||
| 530 | |||
| 565 | /* | 531 | /* |
| 566 | ** {====================================================== | 532 | ** {====================================================== |
| 567 | ** Deprecated functions to manipulate global environment: | 533 | ** Deprecated functions to manipulate global environment: |
| 568 | ** all of them can be simulated through table operations | 534 | ** some of them can be simulated through table operations |
| 569 | ** over the global table. | 535 | ** over the global table. |
| 570 | ** ======================================================= | 536 | ** ======================================================= |
| 571 | */ | 537 | */ |
| 572 | 538 | ||
| 539 | |||
| 540 | #ifdef LUA_DEPRECATETFUNCS | ||
| 541 | |||
| 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 | |||
| 573 | #define num_deprecated 4 | 579 | #define num_deprecated 4 |
| 574 | 580 | ||
| 575 | static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { | 581 | static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { |
| 576 | {"foreachvar", luaB_foreach}, | 582 | {"foreachvar", luaB_foreach}, |
| 577 | {"nextvar", luaB_next}, | 583 | {"nextvar", luaB_next}, |
| 578 | {"rawgetglobal", luaB_rawgettable}, | 584 | {"rawgetglobal", luaB_rawget}, |
| 579 | {"rawsetglobal", luaB_rawsettable} | 585 | {"rawsetglobal", luaB_rawset} |
| 586 | }; | ||
| 587 | |||
| 588 | |||
| 589 | static const struct luaL_reg other_deprecated_global_funcs[] = { | ||
| 590 | {"foreach", luaB_foreach}, | ||
| 591 | {"foreachi", luaB_foreachi}, | ||
| 592 | {"rawgettable", luaB_rawget}, | ||
| 593 | {"rawsettable", luaB_rawset} | ||
| 580 | }; | 594 | }; |
| 581 | 595 | ||
| 582 | 596 | ||
| @@ -591,8 +605,38 @@ static void deprecated_funcs (lua_State *L) { | |||
| 591 | lua_pushcclosure(L, deprecated_global_funcs[i].func, 1); | 605 | lua_pushcclosure(L, deprecated_global_funcs[i].func, 1); |
| 592 | lua_setglobal(L, deprecated_global_funcs[i].name); | 606 | lua_setglobal(L, deprecated_global_funcs[i].name); |
| 593 | } | 607 | } |
| 608 | luaL_openl(L, other_deprecated_global_funcs); | ||
| 594 | } | 609 | } |
| 595 | 610 | ||
| 611 | #else | ||
| 612 | |||
| 613 | /* | ||
| 614 | ** gives an explicit error in any attempt to call an obsolet function | ||
| 615 | */ | ||
| 616 | static void obsolete_func (lua_State *L) { | ||
| 617 | luaL_verror(L, "function `%.20s' is obsolete", luaL_check_string(L, 1)); | ||
| 618 | } | ||
| 619 | |||
| 620 | |||
| 621 | #define num_deprecated 8 | ||
| 622 | |||
| 623 | static const char *const obsolete_names [num_deprecated] = { | ||
| 624 | "foreach", "foreachi", "foreachvar", "nextvar", "rawgetglobal", | ||
| 625 | "rawgettable", "rawsetglobal", "rawsettable" | ||
| 626 | }; | ||
| 627 | |||
| 628 | |||
| 629 | static void deprecated_funcs (lua_State *L) { | ||
| 630 | int i; | ||
| 631 | for (i=0; i<num_deprecated; i++) { | ||
| 632 | lua_pushstring(L, obsolete_names[i]); | ||
| 633 | lua_pushcclosure(L, obsolete_func, 1); | ||
| 634 | lua_setglobal(L, obsolete_names[i]); | ||
| 635 | } | ||
| 636 | } | ||
| 637 | |||
| 638 | #endif | ||
| 639 | |||
| 596 | /* }====================================================== */ | 640 | /* }====================================================== */ |
| 597 | 641 | ||
| 598 | static const struct luaL_reg builtin_funcs[] = { | 642 | static const struct luaL_reg builtin_funcs[] = { |
| @@ -610,8 +654,8 @@ static const struct luaL_reg builtin_funcs[] = { | |||
| 610 | {"newtag", luaB_newtag}, | 654 | {"newtag", luaB_newtag}, |
| 611 | {"next", luaB_next}, | 655 | {"next", luaB_next}, |
| 612 | {"print", luaB_print}, | 656 | {"print", luaB_print}, |
| 613 | {"rawgettable", luaB_rawgettable}, | 657 | {"rawget", luaB_rawget}, |
| 614 | {"rawsettable", luaB_rawsettable}, | 658 | {"rawset", luaB_rawset}, |
| 615 | {"setglobal", luaB_setglobal}, | 659 | {"setglobal", luaB_setglobal}, |
| 616 | {"settag", luaB_settag}, | 660 | {"settag", luaB_settag}, |
| 617 | {"settagmethod", luaB_settagmethod}, | 661 | {"settagmethod", luaB_settagmethod}, |
| @@ -621,8 +665,6 @@ static const struct luaL_reg builtin_funcs[] = { | |||
| 621 | {"type", luaB_type}, | 665 | {"type", luaB_type}, |
| 622 | /* "Extra" functions */ | 666 | /* "Extra" functions */ |
| 623 | {"assert", luaB_assert}, | 667 | {"assert", luaB_assert}, |
| 624 | {"foreach", luaB_foreach}, | ||
| 625 | {"foreachi", luaB_foreachi}, | ||
| 626 | {"getn", luaB_getn}, | 668 | {"getn", luaB_getn}, |
| 627 | {"sort", luaB_sort}, | 669 | {"sort", luaB_sort}, |
| 628 | {"tinsert", luaB_tinsert}, | 670 | {"tinsert", luaB_tinsert}, |
