Actually include standard Lua modules.

This commit is contained in:
Johny Mattsson 2016-10-04 14:47:41 +11:00
parent 3503a1ec95
commit 991965e242
7 changed files with 85 additions and 38 deletions

View File

@ -15,7 +15,7 @@
#include "lauxlib.h"
#include "luaconf.h"
#include "module.h"
#include "sdkconfig.h"
BUILTIN_LIB_INIT( BASE, "", luaopen_base);
BUILTIN_LIB_INIT( LOADLIB, LUA_LOADLIBNAME, luaopen_package);
@ -24,19 +24,19 @@ BUILTIN_LIB_INIT( LOADLIB, LUA_LOADLIBNAME, luaopen_package);
BUILTIN_LIB_INIT( IO, LUA_IOLIBNAME, luaopen_io);
#endif
#if defined (LUA_USE_BUILTIN_STRING)
#if defined (CONFIG_LUA_BUILTIN_STRING)
extern const luaR_entry strlib[];
BUILTIN_LIB_INIT( STRING, LUA_STRLIBNAME, luaopen_string);
BUILTIN_LIB( STRING, LUA_STRLIBNAME, strlib);
#endif
#if defined(LUA_USE_BUILTIN_TABLE)
#if defined(CONFIG_LUA_BUILTIN_TABLE)
extern const luaR_entry tab_funcs[];
BUILTIN_LIB_INIT( TABLE, LUA_TABLIBNAME, luaopen_table);
BUILTIN_LIB( TABLE, LUA_TABLIBNAME, tab_funcs);
#endif
#if defined(LUA_USE_BUILTIN_DEBUG) || defined(LUA_USE_BUILTIN_DEBUG_MINIMAL)
#if defined(CONFIG_LUA_BUILTIN_DEBUG)
extern const luaR_entry dblib[];
BUILTIN_LIB_INIT( DBG, LUA_DBLIBNAME, luaopen_debug);
BUILTIN_LIB( DBG, LUA_DBLIBNAME, dblib);
@ -47,12 +47,12 @@ extern const luaR_entry syslib[];
BUILTIN_LIB( OS, LUA_OSLIBNAME, syslib);
#endif
#if defined(LUA_USE_BUILTIN_COROUTINE)
#if defined(CONFIG_LUA_BUILTIN_COROUTINE)
extern const luaR_entry co_funcs[];
BUILTIN_LIB( CO, LUA_COLIBNAME, co_funcs);
#endif
#if defined(LUA_USE_BUILTIN_MATH)
#if defined(CONFIG_LUA_BUILTIN_MATH)
extern const luaR_entry math_map[];
BUILTIN_LIB( MATH, LUA_MATHLIBNAME, math_map);
#endif

View File

@ -4,4 +4,5 @@ CFLAGS+=\
-DLUA_OPTIMIZE_MEMORY=2 \
-DMIN_OPT_LEVEL=2 \
-DLUA_OPTIMIZE_DEBUG=$(CONFIG_LUA_OPTIMIZE_DEBUG) \
-DLUA_USE_STDIO \

View File

@ -592,14 +592,14 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {
*size = 1;
return "\n";
}
if (c_feof(lf->f)) return NULL;
*size = c_fread(lf->buff, 1, sizeof(lf->buff), lf->f);
if (feof(lf->f)) return NULL;
*size = fread(lf->buff, 1, sizeof(lf->buff), lf->f);
return (*size > 0) ? lf->buff : NULL;
}
static int errfile (lua_State *L, const char *what, int fnameindex) {
const char *serr = c_strerror(errno);
const char *serr = strerror(errno);
const char *filename = lua_tostring(L, fnameindex) + 1;
lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
lua_remove(L, fnameindex);
@ -615,30 +615,30 @@ LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {
lf.extraline = 0;
if (filename == NULL) {
lua_pushliteral(L, "=stdin");
lf.f = c_stdin;
lf.f = stdin;
}
else {
lua_pushfstring(L, "@%s", filename);
lf.f = c_fopen(filename, "r");
lf.f = fopen(filename, "r");
if (lf.f == NULL) return errfile(L, "open", fnameindex);
}
c = c_getc(lf.f);
c = getc(lf.f);
if (c == '#') { /* Unix exec. file? */
lf.extraline = 1;
while ((c = c_getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
if (c == '\n') c = c_getc(lf.f);
while ((c = getc(lf.f)) != EOF && c != '\n') ; /* skip first line */
if (c == '\n') c = getc(lf.f);
}
if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */
lf.f = c_freopen(filename, "rb", lf.f); /* reopen in binary mode */
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
/* skip eventual `#!...' */
while ((c = c_getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
while ((c = getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ;
lf.extraline = 0;
}
c_ungetc(c, lf.f);
ungetc(c, lf.f);
status = lua_load(L, getF, &lf, lua_tostring(L, -1));
readstatus = c_ferror(lf.f);
if (filename) c_fclose(lf.f); /* close file (even in case of errors) */
readstatus = ferror(lf.f);
if (filename) fclose(lf.f); /* close file (even in case of errors) */
if (readstatus) {
lua_settop(L, fnameindex); /* ignore results from `lua_load' */
return errfile(L, "read", fnameindex);
@ -813,7 +813,7 @@ LUALIB_API void luaL_assertfail(const char *file, int line, const char *message)
static int panic (lua_State *L) {
(void)L; /* to avoid warnings */
#if defined(LUA_USE_STDIO)
c_fprintf(c_stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
lua_tostring(L, -1));
#else
luai_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",

View File

@ -41,8 +41,8 @@ static int luaB_print (lua_State *L) {
return luaL_error(L, LUA_QL("tostring") " must return a string to "
LUA_QL("print"));
#if defined(LUA_USE_STDIO)
if (i>1) c_fputs("\t", c_stdout);
c_fputs(s, c_stdout);
if (i>1) fputs("\t", stdout);
fputs(s, stdout);
#else
if (i>1) luai_writestring("\t", 1);
luai_writestring(s, strlen(s));
@ -50,7 +50,7 @@ static int luaB_print (lua_State *L) {
lua_pop(L, 1); /* pop result */
}
#if defined(LUA_USE_STDIO)
c_fputs("\n", c_stdout);
fputs("\n", stdout);
#else
luai_writeline();
#endif

View File

@ -25,7 +25,7 @@ static int db_getregistry (lua_State *L) {
return 1;
}
#ifndef LUA_USE_BUILTIN_DEBUG_MINIMAL
#ifndef CONFIG_LUA_USE_BUILTIN_DEBUG_MINIMAL
static int db_getmetatable (lua_State *L) {
luaL_checkany(L, 1);
@ -85,7 +85,7 @@ static lua_State *getthread (lua_State *L, int *arg) {
return L;
}
}
#ifndef LUA_USE_BUILTIN_DEBUG_MINIMAL
#ifndef CONFIG_LUA_USE_BUILTIN_DEBUG_MINIMAL
static void treatstackoption (lua_State *L, lua_State *L1, const char *fname) {
if (L == L1) {
@ -305,8 +305,8 @@ static int db_debug (lua_State *L) {
for (;;) {
char buffer[LUA_MAXINPUT];
#if defined(LUA_USE_STDIO)
c_fputs("lua_debug> ", c_stderr);
if (c_fgets(buffer, sizeof(buffer), c_stdin) == 0 ||
fputs("lua_debug> ", stderr);
if (fgets(buffer, sizeof(buffer), stdin) == 0 ||
#else
// luai_writestringerror("%s", "lua_debug>");
if (lua_readline(L, buffer, "lua_debug>") == 0 ||
@ -316,8 +316,8 @@ static int db_debug (lua_State *L) {
if (luaL_loadbuffer(L, buffer, strlen(buffer), "=(debug command)") ||
lua_pcall(L, 0, 0, 0)) {
#if defined(LUA_USE_STDIO)
c_fputs(lua_tostring(L, -1), c_stderr);
c_fputs("\n", c_stderr);
fputs(lua_tostring(L, -1), stderr);
fputs("\n", stderr);
#else
luai_writestringerror("%s\n", lua_tostring(L, -1));
#endif
@ -386,7 +386,7 @@ static int db_errorfb (lua_State *L) {
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE dblib[] = {
#ifndef LUA_USE_BUILTIN_DEBUG_MINIMAL
#ifndef CONFIG_LUA_USE_BUILTIN_DEBUG_MINIMAL
{LSTRKEY("debug"), LFUNCVAL(db_debug)},
{LSTRKEY("getfenv"), LFUNCVAL(db_getfenv)},
{LSTRKEY("gethook"), LFUNCVAL(db_gethook)},
@ -394,7 +394,7 @@ const LUA_REG_TYPE dblib[] = {
{LSTRKEY("getlocal"), LFUNCVAL(db_getlocal)},
#endif
{LSTRKEY("getregistry"), LFUNCVAL(db_getregistry)},
#ifndef LUA_USE_BUILTIN_DEBUG_MINIMAL
#ifndef CONFIG_LUA_USE_BUILTIN_DEBUG_MINIMAL
{LSTRKEY("getmetatable"), LFUNCVAL(db_getmetatable)},
{LSTRKEY("getupvalue"), LFUNCVAL(db_getupvalue)},
{LSTRKEY("setfenv"), LFUNCVAL(db_setfenv)},

View File

@ -48,7 +48,7 @@ static void laction (int i) {
static void print_usage (void) {
#if defined(LUA_USE_STDIO)
c_fprintf(c_stderr,
fprintf(stderr,
#else
luai_writestringerror(
#endif
@ -64,16 +64,16 @@ static void print_usage (void) {
,
progname);
#if defined(LUA_USE_STDIO)
c_fflush(c_stderr);
fflush(stderr);
#endif
}
#endif
static void l_message (const char *pname, const char *msg) {
#if defined(LUA_USE_STDIO)
if (pname) c_fprintf(c_stderr, "%s: ", pname);
c_fprintf(c_stderr, "%s\n", msg);
c_fflush(c_stderr);
if (pname) fprintf(stderr, "%s: ", pname);
fprintf(stderr, "%s\n", msg);
fflush(stderr);
#else
if (pname) luai_writestringerror("%s: ", pname);
luai_writestringerror("%s\n", msg);
@ -260,8 +260,8 @@ static void dotty (lua_State *L) {
lua_settop(L, 0); /* clear stack */
#if defined(LUA_USE_STDIO)
c_fputs("\n", c_stdout);
c_fflush(c_stdout);
fputs("\n", stdout);
fflush(stdout);
#else
luai_writeline();
#endif

View File

@ -1,5 +1,51 @@
menu "NodeMCU modules"
menu "Core Lua modules"
config LUA_BUILTIN_STRING
bool "String module"
default "y"
help
Includes the string module (recommended).
config LUA_BUILTIN_TABLE
bool "Table module"
default "y"
help
Includes the table module (recommended).
config LUA_BUILTIN_COROUTINE
bool "Coroutine module"
default "y"
help
Includes the coroutine module (recommended).
config LUA_BUILTIN_MATH
bool "Math module"
default "y"
help
Includes the math module (recommended).
config LUA_BUILTIN_DEBUG
bool "Debug module"
default "n"
help
Includes the debug module.
config LUA_BUILTIN_DEBUG_EXTENDED
depends on LUA_BUILTIN_DEBUG
bool "Extended debug support
default "n"
help
Includes the full debug module, rather than just getregistry and traceback.
config LUA_BUILTIN_DEBUG_MINIMAL
depends on LUA_BUILTIN_DEBUG
bool
default !LUA_BUILTIN_DEBUG_EXTENDED
endmenu
config LUA_MODULE_NODE
bool "Node module"
default "y"