2018-03-08 01:20:59 +01:00
|
|
|
/*
|
|
|
|
** $Id: linit.c,v 1.14.1.1 2007/12/27 13:02:25 roberto Exp $
|
|
|
|
** Initialization of libraries for lua.c
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define linit_c
|
|
|
|
#define LUA_LIB
|
|
|
|
#define LUAC_CROSS_FILE
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lualib.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "luaconf.h"
|
|
|
|
#include "module.h"
|
2018-10-13 16:14:33 +02:00
|
|
|
|
|
|
|
#if !defined(LUA_CROSS_COMPILER) && !(MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2)
|
|
|
|
# error "NodeMCU modules must be built with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)"
|
2018-03-08 01:20:59 +01:00
|
|
|
#endif
|
2018-10-13 16:14:33 +02:00
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
extern const luaR_entry strlib[], tab_funcs[], dblib[],
|
2018-03-08 01:20:59 +01:00
|
|
|
co_funcs[], math_map[], syslib[];
|
2018-10-13 16:14:33 +02:00
|
|
|
extern const luaR_entry syslib[], io_funcs[]; // Only used on cross-compile builds
|
2018-03-08 01:20:59 +01:00
|
|
|
|
|
|
|
/*
|
2019-02-17 19:26:29 +01:00
|
|
|
* The NodeMCU Lua initalisation has been adapted to use linker-based module
|
2018-10-13 16:14:33 +02:00
|
|
|
* registration. This uses a PSECT naming convention to allow the lib and rotab
|
|
|
|
* entries to be collected by the linker into consoliated tables. The linker
|
|
|
|
* defines lua_libs_base and lua_rotable_base.
|
|
|
|
*
|
|
|
|
* This is not practical on Posix builds which use a standard loader declaration
|
|
|
|
* so for cross compiler builds, separate ROTables are used for the base functions
|
2019-02-17 19:26:29 +01:00
|
|
|
* and library ROTables, with the latter chained from the former using its __index
|
2018-10-13 16:14:33 +02:00
|
|
|
* meta-method. In this case all library ROTables are defined here, avoiding the
|
2019-02-17 19:26:29 +01:00
|
|
|
* need for linker magic is avoided on host builds.
|
2018-03-08 01:20:59 +01:00
|
|
|
*/
|
2018-10-13 16:14:33 +02:00
|
|
|
|
|
|
|
#if defined(LUA_CROSS_COMPILER)
|
|
|
|
#define LUA_ROTABLES lua_rotable_base
|
|
|
|
#define LUA_LIBS lua_libs_base
|
2019-02-17 19:26:29 +01:00
|
|
|
#else /* declare Xtensa toolchain linker defined constants */
|
2018-03-08 01:20:59 +01:00
|
|
|
extern const luaL_Reg lua_libs_base[];
|
2018-10-13 16:14:33 +02:00
|
|
|
extern const luaR_entry lua_rotable_base[];
|
|
|
|
#define LUA_ROTABLES lua_rotable_core
|
|
|
|
#define LUA_LIBS lua_libs_core
|
2018-03-08 01:20:59 +01:00
|
|
|
#endif
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2018-10-13 16:14:33 +02:00
|
|
|
static const LOCK_IN_SECTION(libs) luaL_reg LUA_LIBS[] = {
|
|
|
|
{"", luaopen_base},
|
|
|
|
{LUA_LOADLIBNAME, luaopen_package},
|
|
|
|
{LUA_STRLIBNAME, luaopen_string},
|
|
|
|
{LUA_TABLIBNAME, luaopen_table},
|
|
|
|
{LUA_DBLIBNAME, luaopen_debug}
|
|
|
|
#if defined(LUA_CROSS_COMPILER)
|
|
|
|
,{LUA_IOLIBNAME, luaopen_io},
|
|
|
|
{NULL, NULL}
|
|
|
|
#endif
|
|
|
|
};
|
2018-03-08 01:20:59 +01:00
|
|
|
|
2018-10-13 16:14:33 +02:00
|
|
|
#define ENTRY(n,t) {LSTRKEY(n), LRO_ROVAL(t)}
|
|
|
|
|
|
|
|
const LOCK_IN_SECTION(rotable) ROTable LUA_ROTABLES[] = {
|
|
|
|
ENTRY("ROM", LUA_ROTABLES),
|
|
|
|
ENTRY(LUA_STRLIBNAME, strlib),
|
|
|
|
ENTRY(LUA_TABLIBNAME, tab_funcs),
|
|
|
|
ENTRY(LUA_DBLIBNAME, dblib),
|
|
|
|
ENTRY(LUA_COLIBNAME, co_funcs),
|
|
|
|
ENTRY(LUA_MATHLIBNAME, math_map)
|
2018-03-08 01:20:59 +01:00
|
|
|
#if defined(LUA_CROSS_COMPILER)
|
2018-10-13 16:14:33 +02:00
|
|
|
,ENTRY(LUA_OSLIBNAME, syslib),
|
|
|
|
LROT_END
|
2018-03-08 01:20:59 +01:00
|
|
|
#endif
|
2018-10-13 16:14:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void luaL_openlibs (lua_State *L) {
|
|
|
|
const luaL_Reg *lib = lua_libs_base;
|
|
|
|
|
|
|
|
/* loop round and open libraries */
|
2018-03-08 01:20:59 +01:00
|
|
|
for (; lib->name; lib++) {
|
2018-10-13 16:14:33 +02:00
|
|
|
if (lib->func) {
|
2018-03-08 01:20:59 +01:00
|
|
|
lua_pushcfunction(L, lib->func);
|
|
|
|
lua_pushstring(L, lib->name);
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|