From 991965e2422906e25e5690ca586562a363999052 Mon Sep 17 00:00:00 2001 From: Johny Mattsson Date: Tue, 4 Oct 2016 14:47:41 +1100 Subject: [PATCH] Actually include standard Lua modules. --- components/base_nodemcu/linit.c | 12 ++++---- components/lua/Makefile.projbuild | 1 + components/lua/lauxlib.c | 28 +++++++++---------- components/lua/lbaselib.c | 6 ++-- components/lua/ldblib.c | 16 +++++------ components/lua/lua.c | 14 +++++----- components/modules/Kconfig | 46 +++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 38 deletions(-) diff --git a/components/base_nodemcu/linit.c b/components/base_nodemcu/linit.c index da14b73b..2b751f6a 100644 --- a/components/base_nodemcu/linit.c +++ b/components/base_nodemcu/linit.c @@ -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 diff --git a/components/lua/Makefile.projbuild b/components/lua/Makefile.projbuild index d2d4262d..bc42824a 100644 --- a/components/lua/Makefile.projbuild +++ b/components/lua/Makefile.projbuild @@ -4,4 +4,5 @@ CFLAGS+=\ -DLUA_OPTIMIZE_MEMORY=2 \ -DMIN_OPT_LEVEL=2 \ -DLUA_OPTIMIZE_DEBUG=$(CONFIG_LUA_OPTIMIZE_DEBUG) \ + -DLUA_USE_STDIO \ diff --git a/components/lua/lauxlib.c b/components/lua/lauxlib.c index a22f69b7..b79f5c24 100644 --- a/components/lua/lauxlib.c +++ b/components/lua/lauxlib.c @@ -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", diff --git a/components/lua/lbaselib.c b/components/lua/lbaselib.c index e76296c2..878079a8 100644 --- a/components/lua/lbaselib.c +++ b/components/lua/lbaselib.c @@ -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 diff --git a/components/lua/ldblib.c b/components/lua/ldblib.c index 529e8906..2abd8af8 100644 --- a/components/lua/ldblib.c +++ b/components/lua/ldblib.c @@ -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)}, diff --git a/components/lua/lua.c b/components/lua/lua.c index 5693e60d..b9916b67 100644 --- a/components/lua/lua.c +++ b/components/lua/lua.c @@ -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 diff --git a/components/modules/Kconfig b/components/modules/Kconfig index 411e086a..3bd7954e 100644 --- a/components/modules/Kconfig +++ b/components/modules/Kconfig @@ -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"