diff options
Diffstat (limited to 'lbaselib.c')
| -rw-r--r-- | lbaselib.c | 83 |
1 files changed, 74 insertions, 9 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.29 2001/03/06 20:09:38 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.30 2001/03/07 12:43:52 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 | */ |
| @@ -11,6 +11,7 @@ | |||
| 11 | #include <stdlib.h> | 11 | #include <stdlib.h> |
| 12 | #include <string.h> | 12 | #include <string.h> |
| 13 | 13 | ||
| 14 | #define LUA_PRIVATE | ||
| 14 | #include "lua.h" | 15 | #include "lua.h" |
| 15 | 16 | ||
| 16 | #include "lauxlib.h" | 17 | #include "lauxlib.h" |
| @@ -42,7 +43,7 @@ static int luaB__ALERT (lua_State *L) { | |||
| 42 | */ | 43 | */ |
| 43 | static int luaB__ERRORMESSAGE (lua_State *L) { | 44 | static int luaB__ERRORMESSAGE (lua_State *L) { |
| 44 | luaL_checktype(L, 1, LUA_TSTRING); | 45 | luaL_checktype(L, 1, LUA_TSTRING); |
| 45 | lua_getglobal(L, LUA_ALERT); | 46 | lua_getglobal(L, l_s(LUA_ALERT)); |
| 46 | if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */ | 47 | if (lua_isfunction(L, -1)) { /* avoid error loop if _ALERT is not defined */ |
| 47 | lua_Debug ar; | 48 | lua_Debug ar; |
| 48 | lua_pushliteral(L, l_s("error: ")); | 49 | lua_pushliteral(L, l_s("error: ")); |
| @@ -303,6 +304,68 @@ static int luaB_dofile (lua_State *L) { | |||
| 303 | } | 304 | } |
| 304 | 305 | ||
| 305 | 306 | ||
| 307 | #define LUA_PATH l_s("LUA_PATH") | ||
| 308 | |||
| 309 | #define LUA_PATH_SEP l_s(";") | ||
| 310 | |||
| 311 | #ifndef LUA_PATH_DEFAULT | ||
| 312 | #define LUA_PATH_DEFAULT l_s("./") | ||
| 313 | #endif | ||
| 314 | |||
| 315 | static int luaB_require (lua_State *L) { | ||
| 316 | const l_char *path; | ||
| 317 | luaL_check_string(L, 1); | ||
| 318 | lua_settop(L, 1); | ||
| 319 | lua_getglobal(L, LUA_PATH); /* get path */ | ||
| 320 | if (lua_isstring(L, 2)) /* is LUA_PATH defined? */ | ||
| 321 | path = lua_tostring(L, 2); | ||
| 322 | else { /* LUA_PATH not defined */ | ||
| 323 | lua_pop(L, 1); /* pop old global value */ | ||
| 324 | path = getenv(LUA_PATH); /* try environment variable */ | ||
| 325 | if (path == NULL) path = LUA_PATH_DEFAULT; /* else use default */ | ||
| 326 | lua_pushstring(L, path); | ||
| 327 | lua_pushvalue(L, -1); /* duplicate to leave a copy on stack */ | ||
| 328 | lua_setglobal(L, LUA_PATH); | ||
| 329 | } | ||
| 330 | lua_getregistry(L); | ||
| 331 | lua_pushliteral(L, LUA_PATH); | ||
| 332 | lua_gettable(L, 3); /* get book-keeping table */ | ||
| 333 | if (lua_isnil(L, 4)) { /* no book-keeping table? */ | ||
| 334 | lua_pop(L, 1); /* pop the `nil' */ | ||
| 335 | lua_newtable(L); /* create book-keeping table */ | ||
| 336 | lua_pushliteral(L, LUA_PATH); | ||
| 337 | lua_pushvalue(L, -2); /* duplicate table to leave a copy on stack */ | ||
| 338 | lua_settable(L, 3); /* store book-keeping table in registry */ | ||
| 339 | } | ||
| 340 | lua_pushvalue(L, 1); | ||
| 341 | lua_gettable(L, 4); /* check package's name in book-keeping table */ | ||
| 342 | if (!lua_isnil(L, -1)) /* is it there? */ | ||
| 343 | return 0; /* package is already loaded */ | ||
| 344 | else { /* must load it */ | ||
| 345 | for (;;) { /* traverse path */ | ||
| 346 | int res; | ||
| 347 | int l = strcspn(path, LUA_PATH_SEP); /* find separator */ | ||
| 348 | lua_pushlstring(L, path, l); /* directory name */ | ||
| 349 | lua_pushvalue(L, 1); /* package name */ | ||
| 350 | lua_concat(L, 2); /* concat directory with package name */ | ||
| 351 | res = lua_dofile(L, lua_tostring(L, -1)); /* try to load it */ | ||
| 352 | lua_settop(L, 4); /* pop string and eventual results from dofile */ | ||
| 353 | if (res == 0) break; /* ok; file done */ | ||
| 354 | else if (res != LUA_ERRFILE) | ||
| 355 | lua_error(L, NULL); /* error running package; propagate it */ | ||
| 356 | if (*(path+l) == l_c('\0')) /* no more directories? */ | ||
| 357 | luaL_verror(L, l_s("could not load package `%.20s' from path `%.200s'"), | ||
| 358 | lua_tostring(L, 1), lua_tostring(L, 2)); | ||
| 359 | path += l+1; /* try next directory */ | ||
| 360 | } | ||
| 361 | } | ||
| 362 | lua_pushvalue(L, 1); | ||
| 363 | lua_pushnumber(L, 1); | ||
| 364 | lua_settable(L, 4); /* mark it as loaded */ | ||
| 365 | return 0; | ||
| 366 | } | ||
| 367 | |||
| 368 | |||
| 306 | static int luaB_pack (lua_State *L) { | 369 | static int luaB_pack (lua_State *L) { |
| 307 | int n = lua_gettop(L); | 370 | int n = lua_gettop(L); |
| 308 | lua_newtable(L); | 371 | lua_newtable(L); |
| @@ -337,10 +400,10 @@ static int luaB_call (lua_State *L) { | |||
| 337 | int status; | 400 | int status; |
| 338 | int n; | 401 | int n; |
| 339 | if (!lua_isnull(L, 4)) { /* set new error method */ | 402 | if (!lua_isnull(L, 4)) { /* set new error method */ |
| 340 | lua_getglobal(L, LUA_ERRORMESSAGE); | 403 | lua_getglobal(L, l_s(LUA_ERRORMESSAGE)); |
| 341 | err = lua_gettop(L); /* get index */ | 404 | err = lua_gettop(L); /* get index */ |
| 342 | lua_pushvalue(L, 4); | 405 | lua_pushvalue(L, 4); |
| 343 | lua_setglobal(L, LUA_ERRORMESSAGE); | 406 | lua_setglobal(L, l_s(LUA_ERRORMESSAGE)); |
| 344 | } | 407 | } |
| 345 | oldtop = lua_gettop(L); /* top before function-call preparation */ | 408 | oldtop = lua_gettop(L); /* top before function-call preparation */ |
| 346 | /* push function */ | 409 | /* push function */ |
| @@ -349,7 +412,7 @@ static int luaB_call (lua_State *L) { | |||
| 349 | status = lua_call(L, n, LUA_MULTRET); | 412 | status = lua_call(L, n, LUA_MULTRET); |
| 350 | if (err != 0) { /* restore old error method */ | 413 | if (err != 0) { /* restore old error method */ |
| 351 | lua_pushvalue(L, err); | 414 | lua_pushvalue(L, err); |
| 352 | lua_setglobal(L, LUA_ERRORMESSAGE); | 415 | lua_setglobal(L, l_s(LUA_ERRORMESSAGE)); |
| 353 | } | 416 | } |
| 354 | if (status != 0) { /* error in call? */ | 417 | if (status != 0) { /* error in call? */ |
| 355 | if (strchr(options, l_c('x'))) | 418 | if (strchr(options, l_c('x'))) |
| @@ -382,7 +445,8 @@ static int luaB_tostring (lua_State *L) { | |||
| 382 | case LUA_TUSERDATA: { | 445 | case LUA_TUSERDATA: { |
| 383 | const l_char *t = lua_xtype(L, 1); | 446 | const l_char *t = lua_xtype(L, 1); |
| 384 | if (strcmp(t, l_s("userdata")) == 0) | 447 | if (strcmp(t, l_s("userdata")) == 0) |
| 385 | sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1), lua_touserdata(L, 1)); | 448 | sprintf(buff, l_s("userdata(%d): %p"), lua_tag(L, 1), |
| 449 | lua_touserdata(L, 1)); | ||
| 386 | else | 450 | else |
| 387 | sprintf(buff, l_s("%.40s: %p"), t, lua_touserdata(L, 1)); | 451 | sprintf(buff, l_s("%.40s: %p"), t, lua_touserdata(L, 1)); |
| 388 | break; | 452 | break; |
| @@ -663,8 +727,8 @@ static void deprecated_funcs (lua_State *L) { | |||
| 663 | /* }====================================================== */ | 727 | /* }====================================================== */ |
| 664 | 728 | ||
| 665 | static const luaL_reg base_funcs[] = { | 729 | static const luaL_reg base_funcs[] = { |
| 666 | {LUA_ALERT, luaB__ALERT}, | 730 | {l_s(LUA_ALERT), luaB__ALERT}, |
| 667 | {LUA_ERRORMESSAGE, luaB__ERRORMESSAGE}, | 731 | {l_s(LUA_ERRORMESSAGE), luaB__ERRORMESSAGE}, |
| 668 | {l_s("call"), luaB_call}, | 732 | {l_s("call"), luaB_call}, |
| 669 | {l_s("collectgarbage"), luaB_collectgarbage}, | 733 | {l_s("collectgarbage"), luaB_collectgarbage}, |
| 670 | {l_s("copytagmethods"), luaB_copytagmethods}, | 734 | {l_s("copytagmethods"), luaB_copytagmethods}, |
| @@ -685,6 +749,7 @@ static const luaL_reg base_funcs[] = { | |||
| 685 | {l_s("rawset"), luaB_rawset}, | 749 | {l_s("rawset"), luaB_rawset}, |
| 686 | {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */ | 750 | {l_s("rawgettable"), luaB_rawget}, /* for compatibility 3.2 */ |
| 687 | {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */ | 751 | {l_s("rawsettable"), luaB_rawset}, /* for compatibility 3.2 */ |
| 752 | {l_s("require"), luaB_require}, | ||
| 688 | {l_s("setglobal"), luaB_setglobal}, | 753 | {l_s("setglobal"), luaB_setglobal}, |
| 689 | {l_s("settag"), luaB_settag}, | 754 | {l_s("settag"), luaB_settag}, |
| 690 | {l_s("settagmethod"), luaB_settagmethod}, | 755 | {l_s("settagmethod"), luaB_settagmethod}, |
| @@ -706,7 +771,7 @@ static const luaL_reg base_funcs[] = { | |||
| 706 | 771 | ||
| 707 | LUALIB_API int lua_baselibopen (lua_State *L) { | 772 | LUALIB_API int lua_baselibopen (lua_State *L) { |
| 708 | luaL_openl(L, base_funcs); | 773 | luaL_openl(L, base_funcs); |
| 709 | lua_pushliteral(L, LUA_VERSION); | 774 | lua_pushliteral(L, l_s(LUA_VERSION)); |
| 710 | lua_setglobal(L, l_s("_VERSION")); | 775 | lua_setglobal(L, l_s("_VERSION")); |
| 711 | deprecated_funcs(L); | 776 | deprecated_funcs(L); |
| 712 | return 0; | 777 | return 0; |
