diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-11-18 17:53:49 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-11-18 17:53:49 -0200 |
| commit | 9be5844713036f89483c8f47dfaa45a1d52eb70f (patch) | |
| tree | d6466081fed22b0641f8543eebb1f9b11409d6db /lbaselib.c | |
| parent | 497f042fadb76bdca15fec27ea087a9995182005 (diff) | |
| download | lua-9be5844713036f89483c8f47dfaa45a1d52eb70f.tar.gz lua-9be5844713036f89483c8f47dfaa45a1d52eb70f.tar.bz2 lua-9be5844713036f89483c8f47dfaa45a1d52eb70f.zip | |
first full implementation of new package system
Diffstat (limited to 'lbaselib.c')
| -rw-r--r-- | lbaselib.c | 122 |
1 files changed, 8 insertions, 114 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbaselib.c,v 1.158 2004/09/15 20:39:42 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.159 2004/09/29 21:03:14 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 | */ |
| @@ -440,105 +440,6 @@ static int luaB_newproxy (lua_State *L) { | |||
| 440 | } | 440 | } |
| 441 | 441 | ||
| 442 | 442 | ||
| 443 | /* | ||
| 444 | ** {====================================================== | ||
| 445 | ** `require' function | ||
| 446 | ** ======================================================= | ||
| 447 | */ | ||
| 448 | |||
| 449 | |||
| 450 | static const char *getpath (lua_State *L) { | ||
| 451 | /* try first `LUA_PATH' for compatibility */ | ||
| 452 | lua_getfield(L, LUA_GLOBALSINDEX, "LUA_PATH"); | ||
| 453 | if (!lua_isstring(L, -1)) { | ||
| 454 | lua_pop(L, 1); | ||
| 455 | lua_getfield(L, LUA_GLOBALSINDEX, "_PATH"); | ||
| 456 | } | ||
| 457 | if (!lua_isstring(L, -1)) | ||
| 458 | luaL_error(L, "global _PATH must be a string"); | ||
| 459 | return lua_tostring(L, -1); | ||
| 460 | } | ||
| 461 | |||
| 462 | |||
| 463 | static int luaB_require (lua_State *L) { | ||
| 464 | const char *name = luaL_checkstring(L, 1); | ||
| 465 | const char *fname; | ||
| 466 | lua_settop(L, 1); | ||
| 467 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
| 468 | lua_getfield(L, 2, name); | ||
| 469 | if (lua_toboolean(L, -1)) /* is it there? */ | ||
| 470 | return 1; /* package is already loaded; return its result */ | ||
| 471 | /* else must load it; first mark it as loaded */ | ||
| 472 | lua_pushboolean(L, 1); | ||
| 473 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ | ||
| 474 | fname = luaL_gsub(L, name, ".", LUA_DIRSEP); | ||
| 475 | fname = luaL_searchpath(L, fname, getpath(L)); | ||
| 476 | if (fname == NULL || luaL_loadfile(L, fname) != 0) | ||
| 477 | return luaL_error(L, "error loading package `%s' (%s)", name, | ||
| 478 | lua_tostring(L, -1)); | ||
| 479 | lua_pushvalue(L, 1); /* pass name as argument to module */ | ||
| 480 | lua_call(L, 1, 1); /* run loaded module */ | ||
| 481 | if (!lua_isnil(L, -1)) /* nil return? */ | ||
| 482 | lua_setfield(L, 2, name); | ||
| 483 | lua_getfield(L, 2, name); /* return _LOADED[name] */ | ||
| 484 | return 1; | ||
| 485 | } | ||
| 486 | |||
| 487 | |||
| 488 | static void setfenv (lua_State *L) { | ||
| 489 | lua_Debug ar; | ||
| 490 | lua_getstack(L, 1, &ar); | ||
| 491 | lua_getinfo(L, "f", &ar); | ||
| 492 | lua_pushvalue(L, -2); | ||
| 493 | lua_setfenv(L, -2); | ||
| 494 | } | ||
| 495 | |||
| 496 | |||
| 497 | static int luaB_module (lua_State *L) { | ||
| 498 | const char *modname = luaL_checkstring(L, 1); | ||
| 499 | const char *dot; | ||
| 500 | lua_settop(L, 1); | ||
| 501 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); | ||
| 502 | /* try to find given table */ | ||
| 503 | luaL_getfield(L, LUA_GLOBALSINDEX, modname); | ||
| 504 | if (lua_isnil(L, -1)) { /* not found? */ | ||
| 505 | lua_pop(L, 1); /* remove previous result */ | ||
| 506 | lua_newtable(L); /* create it */ | ||
| 507 | /* register it with given name */ | ||
| 508 | lua_pushvalue(L, -1); | ||
| 509 | luaL_setfield(L, LUA_GLOBALSINDEX, modname); | ||
| 510 | } | ||
| 511 | else if (!lua_istable(L, -1)) | ||
| 512 | return luaL_error(L, "name conflict for module `%s'", modname); | ||
| 513 | /* check whether table already has a _NAME field */ | ||
| 514 | lua_getfield(L, -1, "_NAME"); | ||
| 515 | if (!lua_isnil(L, -1)) /* is table an initialized module? */ | ||
| 516 | lua_pop(L, 1); | ||
| 517 | else { /* no; initialize it */ | ||
| 518 | lua_pop(L, 1); | ||
| 519 | lua_newtable(L); /* create new metatable */ | ||
| 520 | lua_pushvalue(L, LUA_GLOBALSINDEX); | ||
| 521 | lua_setfield(L, -2, "__index"); /* mt.__index = _G */ | ||
| 522 | lua_setmetatable(L, -2); | ||
| 523 | lua_pushstring(L, modname); | ||
| 524 | lua_setfield(L, -2, "_NAME"); | ||
| 525 | dot = strrchr(modname, '.'); /* look for last dot in module name */ | ||
| 526 | if (dot == NULL) dot = modname; | ||
| 527 | else dot++; | ||
| 528 | /* set _PACK as package name (full module name minus last part) */ | ||
| 529 | lua_pushlstring(L, modname, dot - modname); | ||
| 530 | lua_setfield(L, -2, "_PACK"); | ||
| 531 | } | ||
| 532 | lua_pushvalue(L, -1); | ||
| 533 | lua_setfield(L, 2, modname); /* _LOADED[modname] = new table */ | ||
| 534 | setfenv(L); | ||
| 535 | return 0; | ||
| 536 | } | ||
| 537 | |||
| 538 | |||
| 539 | /* }====================================================== */ | ||
| 540 | |||
| 541 | |||
| 542 | static const luaL_reg base_funcs[] = { | 443 | static const luaL_reg base_funcs[] = { |
| 543 | {"error", luaB_error}, | 444 | {"error", luaB_error}, |
| 544 | {"getmetatable", luaB_getmetatable}, | 445 | {"getmetatable", luaB_getmetatable}, |
| @@ -564,8 +465,6 @@ static const luaL_reg base_funcs[] = { | |||
| 564 | {"dofile", luaB_dofile}, | 465 | {"dofile", luaB_dofile}, |
| 565 | {"loadstring", luaB_loadstring}, | 466 | {"loadstring", luaB_loadstring}, |
| 566 | {"load", luaB_load}, | 467 | {"load", luaB_load}, |
| 567 | {"require", luaB_require}, | ||
| 568 | {"module", luaB_module}, | ||
| 569 | {NULL, NULL} | 468 | {NULL, NULL} |
| 570 | }; | 469 | }; |
| 571 | 470 | ||
| @@ -713,11 +612,10 @@ static void auxopen (lua_State *L, const char *name, | |||
| 713 | 612 | ||
| 714 | 613 | ||
| 715 | static void base_open (lua_State *L) { | 614 | static void base_open (lua_State *L) { |
| 716 | const char *path; | ||
| 717 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 615 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 718 | luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ | 616 | luaL_openlib(L, NULL, base_funcs, 0); /* open lib into global table */ |
| 719 | lua_pushliteral(L, LUA_VERSION); | 617 | lua_pushliteral(L, LUA_VERSION); |
| 720 | lua_setfield(L, LUA_GLOBALSINDEX, "_VERSION"); /* set global _VERSION */ | 618 | lua_setglobal(L, "_VERSION"); /* set global _VERSION */ |
| 721 | /* `ipairs' and `pairs' need auxiliary functions as upvalues */ | 619 | /* `ipairs' and `pairs' need auxiliary functions as upvalues */ |
| 722 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); | 620 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); |
| 723 | auxopen(L, "pairs", luaB_pairs, luaB_next); | 621 | auxopen(L, "pairs", luaB_pairs, luaB_next); |
| @@ -728,20 +626,16 @@ static void base_open (lua_State *L) { | |||
| 728 | lua_pushliteral(L, "kv"); | 626 | lua_pushliteral(L, "kv"); |
| 729 | lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ | 627 | lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */ |
| 730 | lua_pushcclosure(L, luaB_newproxy, 1); | 628 | lua_pushcclosure(L, luaB_newproxy, 1); |
| 731 | lua_setfield(L, LUA_GLOBALSINDEX, "newproxy"); /* set global `newproxy' */ | 629 | lua_setglobal(L, "newproxy"); /* set global `newproxy' */ |
| 732 | /* `require' needs a table to keep loaded chunks */ | 630 | /* create register._LOADED to track loaded modules */ |
| 733 | lua_newtable(L); | 631 | lua_newtable(L); |
| 734 | lua_pushvalue(L, -1); | ||
| 735 | lua_setglobal(L, "_LOADED"); | ||
| 736 | lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED"); | 632 | lua_setfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 633 | /* create register._PRELOAD to allow pre-loaded modules */ | ||
| 634 | lua_newtable(L); | ||
| 635 | lua_setfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); | ||
| 737 | /* set global _G */ | 636 | /* set global _G */ |
| 738 | lua_pushvalue(L, LUA_GLOBALSINDEX); | 637 | lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 739 | lua_setfield(L, LUA_GLOBALSINDEX, "_G"); | 638 | lua_setglobal(L, "_G"); |
| 740 | /* set global _PATH */ | ||
| 741 | path = getenv(LUA_PATH); | ||
| 742 | if (path == NULL) path = LUA_PATH_DEFAULT; | ||
| 743 | lua_pushstring(L, path); | ||
| 744 | lua_setfield(L, LUA_GLOBALSINDEX, "_PATH"); | ||
| 745 | } | 639 | } |
| 746 | 640 | ||
| 747 | 641 | ||
