/*1** $Id: linit.c,v 1.39.1.1 2017/04/19 17:20:42 roberto Exp $2** Initialization of libraries for lua.c and other clients3** See Copyright Notice in lua.h4*/567#define linit_c8#define LUA_LIB910/*11** If you embed Lua in your program and need to open the standard12** libraries, call luaL_openlibs in your program. If you need a13** different set of libraries, copy this file to your project and edit14** it to suit your needs.15**16** You can also *preload* libraries, so that a later 'require' can17** open the library, which is already linked to the application.18** For that, do the following code:19**20** luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);21** lua_pushcfunction(L, luaopen_modname);22** lua_setfield(L, -2, modname);23** lua_pop(L, 1); // remove PRELOAD table24*/2526#include "lprefix.h"2728#include <stddef.h>29#include <stdlib.h>3031#include "lua.h"3233#include "lualib.h"34#include "lauxlib.h"35#include "lposix.h"3637#include "bootstrap.h"3839/*40** these libs are loaded by lua.c and are readily available to any Lua41** program42*/43static const luaL_Reg loadedlibs[] = {44{"_G", luaopen_base},45{LUA_LOADLIBNAME, luaopen_package},46{LUA_COLIBNAME, luaopen_coroutine},47{LUA_TABLIBNAME, luaopen_table},48{LUA_IOLIBNAME, luaopen_io},49{LUA_OSLIBNAME, luaopen_os},50{LUA_STRLIBNAME, luaopen_string},51{LUA_MATHLIBNAME, luaopen_math},52{LUA_UTF8LIBNAME, luaopen_utf8},53{LUA_DBLIBNAME, luaopen_debug},54#if defined(LUA_COMPAT_BITLIB)55{LUA_BITLIBNAME, luaopen_bit32},56#endif57/* FreeBSD Extensions */58{"posix", luaopen_posix},59{NULL, NULL}60};6162#ifdef BOOTSTRAPPING63static void __attribute__((constructor)) flua_init_env(void) {64/*65* This happens in the middle of luaopen_package(). We could move it into66* flua_setup_mods(), but it seems better to avoid its timing being so67* important that it would break some of our bootstrap modules if someone68* were to reorder things.69*/70if (getenv("LUA_PATH") == NULL)71setenv("LUA_PATH", BOOTSTRAP_FLUA_PATH, 1);72}7374static void flua_setup_mods (lua_State *L) {75const luaL_Reg **flib;7677SET_FOREACH(flib, FLUA_MODULE_SETNAME) {78luaL_requiref(L, (*flib)->name, (*flib)->func, 1);79lua_pop(L, 1); /* remove lib */80}81};82#endif8384LUALIB_API void luaL_openlibs (lua_State *L) {85const luaL_Reg *lib;86/* "require" functions from 'loadedlibs' and set results to global table */87for (lib = loadedlibs; lib->func; lib++) {88luaL_requiref(L, lib->name, lib->func, 1);89lua_pop(L, 1); /* remove lib */90}91#ifdef BOOTSTRAPPING92flua_setup_mods(L);93#endif94}959697