diff options
Diffstat (limited to 'lbaselib.c')
| -rw-r--r-- | lbaselib.c | 63 |
1 files changed, 27 insertions, 36 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.140 2004/03/09 17:34:35 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.141 2004/03/26 13:25:17 roberto Exp roberto $ |
| 3 | ** Basic library | 3 | ** Basic library |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -12,6 +12,7 @@ | |||
| 12 | #include <string.h> | 12 | #include <string.h> |
| 13 | 13 | ||
| 14 | #define lbaselib_c | 14 | #define lbaselib_c |
| 15 | #define LUA_LIB | ||
| 15 | 16 | ||
| 16 | #include "lua.h" | 17 | #include "lua.h" |
| 17 | 18 | ||
| @@ -235,28 +236,29 @@ static int luaB_next (lua_State *L) { | |||
| 235 | 236 | ||
| 236 | static int luaB_pairs (lua_State *L) { | 237 | static int luaB_pairs (lua_State *L) { |
| 237 | luaL_checktype(L, 1, LUA_TTABLE); | 238 | luaL_checktype(L, 1, LUA_TTABLE); |
| 238 | lua_getglobal(L, "next"); /* return generator, */ | 239 | lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ |
| 239 | lua_pushvalue(L, 1); /* state, */ | 240 | lua_pushvalue(L, 1); /* state, */ |
| 240 | lua_pushnil(L); /* and initial value */ | 241 | lua_pushnil(L); /* and initial value */ |
| 241 | return 3; | 242 | return 3; |
| 242 | } | 243 | } |
| 243 | 244 | ||
| 244 | 245 | ||
| 246 | static int ipairsaux (lua_State *L) { | ||
| 247 | int i = luaL_checkint(L, 2); | ||
| 248 | luaL_checktype(L, 1, LUA_TTABLE); | ||
| 249 | i++; /* next value */ | ||
| 250 | lua_pushinteger(L, i); | ||
| 251 | lua_rawgeti(L, 1, i); | ||
| 252 | return (lua_isnil(L, -1)) ? 0 : 2; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 245 | static int luaB_ipairs (lua_State *L) { | 256 | static int luaB_ipairs (lua_State *L) { |
| 246 | int i = (int)lua_tointeger(L, 2); | ||
| 247 | luaL_checktype(L, 1, LUA_TTABLE); | 257 | luaL_checktype(L, 1, LUA_TTABLE); |
| 248 | if (i == 0 && lua_isnone(L, 2)) { /* `for' start? */ | 258 | lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */ |
| 249 | lua_getglobal(L, "ipairs"); /* return generator, */ | 259 | lua_pushvalue(L, 1); /* state, */ |
| 250 | lua_pushvalue(L, 1); /* state, */ | 260 | lua_pushinteger(L, 0); /* and initial value */ |
| 251 | lua_pushinteger(L, 0); /* and initial value */ | 261 | return 3; |
| 252 | return 3; | ||
| 253 | } | ||
| 254 | else { /* `for' step */ | ||
| 255 | i++; /* next value */ | ||
| 256 | lua_pushinteger(L, i); | ||
| 257 | lua_rawgeti(L, 1, i); | ||
| 258 | return (lua_isnil(L, -1)) ? 0 : 2; | ||
| 259 | } | ||
| 260 | } | 262 | } |
| 261 | 263 | ||
| 262 | 264 | ||
| @@ -458,25 +460,6 @@ static int luaB_newproxy (lua_State *L) { | |||
| 458 | */ | 460 | */ |
| 459 | 461 | ||
| 460 | 462 | ||
| 461 | /* name of global that holds table with loaded packages */ | ||
| 462 | #define REQTAB "_LOADED" | ||
| 463 | |||
| 464 | /* name of global that holds the search path for packages */ | ||
| 465 | #define LUA_PATH "LUA_PATH" | ||
| 466 | |||
| 467 | #ifndef LUA_PATH_SEP | ||
| 468 | #define LUA_PATH_SEP ';' | ||
| 469 | #endif | ||
| 470 | |||
| 471 | #ifndef LUA_PATH_MARK | ||
| 472 | #define LUA_PATH_MARK '?' | ||
| 473 | #endif | ||
| 474 | |||
| 475 | #ifndef LUA_PATH_DEFAULT | ||
| 476 | #define LUA_PATH_DEFAULT "?;?.lua" | ||
| 477 | #endif | ||
| 478 | |||
| 479 | |||
| 480 | static const char *getpath (lua_State *L) { | 463 | static const char *getpath (lua_State *L) { |
| 481 | const char *path; | 464 | const char *path; |
| 482 | lua_getglobal(L, LUA_PATH); /* try global variable */ | 465 | lua_getglobal(L, LUA_PATH); /* try global variable */ |
| @@ -576,8 +559,6 @@ static const luaL_reg base_funcs[] = { | |||
| 576 | {"getfenv", luaB_getfenv}, | 559 | {"getfenv", luaB_getfenv}, |
| 577 | {"setfenv", luaB_setfenv}, | 560 | {"setfenv", luaB_setfenv}, |
| 578 | {"next", luaB_next}, | 561 | {"next", luaB_next}, |
| 579 | {"ipairs", luaB_ipairs}, | ||
| 580 | {"pairs", luaB_pairs}, | ||
| 581 | {"print", luaB_print}, | 562 | {"print", luaB_print}, |
| 582 | {"tonumber", luaB_tonumber}, | 563 | {"tonumber", luaB_tonumber}, |
| 583 | {"tostring", luaB_tostring}, | 564 | {"tostring", luaB_tostring}, |
| @@ -708,12 +689,22 @@ static const luaL_reg co_funcs[] = { | |||
| 708 | /* }====================================================== */ | 689 | /* }====================================================== */ |
| 709 | 690 | ||
| 710 | 691 | ||
| 692 | static void auxopen (lua_State *L, const char *name, | ||
| 693 | lua_CFunction f, lua_CFunction u) { | ||
| 694 | lua_pushcfunction(L, u); | ||
| 695 | lua_pushcclosure(L, f, 1); | ||
| 696 | lua_setfield(L, -2, name); | ||
| 697 | } | ||
| 698 | |||
| 711 | 699 | ||
| 712 | static void base_open (lua_State *L) { | 700 | static void base_open (lua_State *L) { |
| 713 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 701 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 714 | luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ | 702 | luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ |
| 715 | lua_pushliteral(L, LUA_VERSION); | 703 | lua_pushliteral(L, LUA_VERSION); |
| 716 | lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ | 704 | lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */ |
| 705 | /* `ipairs' and `pairs' need auxiliary functions as upvalues */ | ||
| 706 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); | ||
| 707 | auxopen(L, "pairs", luaB_pairs, luaB_next); | ||
| 717 | /* `newproxy' needs a weaktable as upvalue */ | 708 | /* `newproxy' needs a weaktable as upvalue */ |
| 718 | lua_newtable(L); /* new table `w' */ | 709 | lua_newtable(L); /* new table `w' */ |
| 719 | lua_pushvalue(L, -1); /* `w' will be its own metatable */ | 710 | lua_pushvalue(L, -1); /* `w' will be its own metatable */ |
