mirror of
https://github.com/lua/lua
synced 2024-11-25 14:20:41 +03:00
d738c8d18b
Makes it easier to start Lua with only some standard libraries.
65 lines
1.4 KiB
C
65 lines
1.4 KiB
C
/*
|
|
** $Id: linit.c $
|
|
** Initialization of libraries for lua.c and other clients
|
|
** See Copyright Notice in lua.h
|
|
*/
|
|
|
|
|
|
#define linit_c
|
|
#define LUA_LIB
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lualib.h"
|
|
#include "lauxlib.h"
|
|
|
|
|
|
/*
|
|
** Standard Libraries
|
|
*/
|
|
static const luaL_Reg stdlibs[] = {
|
|
{LUA_GNAME, luaopen_base},
|
|
{LUA_LOADLIBNAME, luaopen_package},
|
|
|
|
{LUA_COLIBNAME, luaopen_coroutine},
|
|
{LUA_DBLIBNAME, luaopen_debug},
|
|
{LUA_IOLIBNAME, luaopen_io},
|
|
{LUA_MATHLIBNAME, luaopen_math},
|
|
{LUA_OSLIBNAME, luaopen_os},
|
|
{LUA_STRLIBNAME, luaopen_string},
|
|
{LUA_TABLIBNAME, luaopen_table},
|
|
{LUA_UTF8LIBNAME, luaopen_utf8},
|
|
|
|
{NULL, NULL}
|
|
};
|
|
|
|
|
|
/*
|
|
** require selected standard libraries and add the others to the
|
|
** preload table.
|
|
*/
|
|
LUALIB_API void luaL_openselectedlibs (lua_State *L, int what) {
|
|
int mask = 1;
|
|
const luaL_Reg *lib;
|
|
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
|
|
for (lib = stdlibs; lib->func; (lib++, mask <<= 1)) {
|
|
if (what & mask) { /* selected? */
|
|
luaL_requiref(L, lib->name, lib->func, 1); /* require library */
|
|
lua_pop(L, 1); /* remove result from the stack */
|
|
}
|
|
else { /* add library to PRELOAD table */
|
|
lua_pushcfunction(L, lib->func);
|
|
lua_setfield(L, -2, lib->name);
|
|
}
|
|
}
|
|
lua_assert((mask >> 1) == LUA_UTF8LIBK);
|
|
lua_pop(L, 1); // remove PRELOAD table
|
|
}
|
|
|