lua/linit.c

64 lines
1.5 KiB
C
Raw Normal View History

/*
** $Id: linit.c $
2011-01-26 19:30:02 +03:00
** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h
*/
2004-07-09 18:29:29 +04:00
#define linit_c
#define LUA_LIB
2009-09-05 16:39:29 +04:00
2015-01-05 16:48:33 +03:00
#include "lprefix.h"
#include <stddef.h>
2004-07-09 18:29:29 +04:00
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "llimits.h"
2004-07-09 18:29:29 +04:00
/*
** Standard Libraries. (Must be listed in the same ORDER of their
** respective constants LUA_<libname>K.)
*/
static const luaL_Reg stdlibs[] = {
{LUA_GNAME, luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_DBLIBNAME, luaopen_debug},
2004-07-09 18:29:29 +04:00
{LUA_IOLIBNAME, luaopen_io},
{LUA_MATHLIBNAME, luaopen_math},
2004-07-09 19:47:48 +04:00
{LUA_OSLIBNAME, luaopen_os},
2004-07-09 18:29:29 +04:00
{LUA_STRLIBNAME, luaopen_string},
{LUA_TABLIBNAME, luaopen_table},
{LUA_UTF8LIBNAME, luaopen_utf8},
{NULL, NULL}
};
/*
** require and preload selected standard libraries
*/
LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
int mask;
2009-09-05 16:39:29 +04:00
const luaL_Reg *lib;
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
if (load & mask) { /* selected? */
luaL_requiref(L, lib->name, lib->func, 1); /* require library */
lua_pop(L, 1); /* remove result from the stack */
}
else if (preload & mask) { /* selected? */
lua_pushcfunction(L, lib->func);
lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
}
2004-07-09 18:29:29 +04:00
}
lua_assert((mask >> 1) == LUA_UTF8LIBK);
lua_pop(L, 1); /* remove PRELOAD table */
2004-07-09 18:29:29 +04:00
}