lua

A copy of the Lua development repository
Log | Files | Refs | README

linit.c (1529B)


      1 /*
      2 ** $Id: linit.c $
      3 ** Initialization of libraries for lua.c and other clients
      4 ** See Copyright Notice in lua.h
      5 */
      6 
      7 
      8 #define linit_c
      9 #define LUA_LIB
     10 
     11 
     12 #include "lprefix.h"
     13 
     14 
     15 #include <stddef.h>
     16 
     17 #include "lua.h"
     18 
     19 #include "lualib.h"
     20 #include "lauxlib.h"
     21 #include "llimits.h"
     22 
     23 
     24 /*
     25 ** Standard Libraries. (Must be listed in the same ORDER of their
     26 ** respective constants LUA_<libname>K.)
     27 */
     28 static const luaL_Reg stdlibs[] = {
     29   {LUA_GNAME, luaopen_base},
     30   {LUA_LOADLIBNAME, luaopen_package},
     31   {LUA_COLIBNAME, luaopen_coroutine},
     32   {LUA_DBLIBNAME, luaopen_debug},
     33   {LUA_IOLIBNAME, luaopen_io},
     34   {LUA_MATHLIBNAME, luaopen_math},
     35   {LUA_OSLIBNAME, luaopen_os},
     36   {LUA_STRLIBNAME, luaopen_string},
     37   {LUA_TABLIBNAME, luaopen_table},
     38   {LUA_UTF8LIBNAME, luaopen_utf8},
     39   {NULL, NULL}
     40 };
     41 
     42 
     43 /*
     44 ** require and preload selected standard libraries
     45 */
     46 LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
     47   int mask;
     48   const luaL_Reg *lib;
     49   luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
     50   for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
     51     if (load & mask) {  /* selected? */
     52       luaL_requiref(L, lib->name, lib->func, 1);  /* require library */
     53       lua_pop(L, 1);  /* remove result from the stack */
     54     }
     55     else if (preload & mask) {  /* selected? */
     56       lua_pushcfunction(L, lib->func);
     57       lua_setfield(L, -2, lib->name);  /* add library to PRELOAD table */
     58     }
     59   }
     60   lua_assert((mask >> 1) == LUA_UTF8LIBK);
     61   lua_pop(L, 1);  /* remove PRELOAD table */
     62 }
     63