diff --git a/library.properties b/library.properties index b23f65b..c9e7305 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP8266-Arduino-Lua -version=0.0.10 -author=François Dugast <1050329+fdu@users.noreply.github.com> +version=0.0.20 +author=François Dugast <1050329+fdu@users.noreply.github.com>, Sasszem maintainer=François Dugast <1050329+fdu@users.noreply.github.com> sentence=Lua scripting engine integrated in Arduino for ESP8266 paragraph= diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index fb9b551..c2e9436 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -18,10 +18,26 @@ extern "C" { delay(a); } - static int lua_wrapper_print(lua_State *lua) { - String a = String(luaL_checkstring(lua, 1)); - Serial.println(a); - } + static int lua_wrapper_print (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int i; + lua_getglobal(L, "tostring"); + for (i=1; i<=n; i++) { + const char *s; + size_t l; + lua_pushvalue(L, -1); /* function to be called */ + lua_pushvalue(L, i); /* value to print */ + lua_call(L, 1, 1); + s = lua_tolstring(L, -1, &l); /* get result */ + if (s == NULL) + return luaL_error(L, "'tostring' must return a string to 'print'"); + if (i>1) Serial.write("\t"); + Serial.write(s); + lua_pop(L, 1); /* pop result */ + } + Serial.println(); + return 0; +} static int lua_wrapper_millis(lua_State *lua) { lua_pushnumber(lua, (lua_Number) millis()); @@ -31,6 +47,12 @@ extern "C" { LuaWrapper::LuaWrapper() { _state = luaL_newstate(); + + luaopen_base(_state); + luaopen_table(_state); + luaopen_string(_state); + luaopen_math(_state); + lua_register(_state, "pinMode", lua_wrapper_pinMode); lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite); lua_register(_state, "delay", lua_wrapper_delay); diff --git a/src/lua/lbaselib.c b/src/lua/lbaselib.c index 6460e4f..1fe8d7d 100644 --- a/src/lua/lbaselib.c +++ b/src/lua/lbaselib.c @@ -20,29 +20,6 @@ #include "lauxlib.h" #include "lualib.h" - -static int luaB_print (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; - size_t l; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &l); /* get result */ - if (s == NULL) - return luaL_error(L, "'tostring' must return a string to 'print'"); - if (i>1) lua_writestring("\t", 1); - lua_writestring(s, l); - lua_pop(L, 1); /* pop result */ - } - lua_writeline(); - return 0; -} - - #define SPACECHARS " \f\n\r\t\v" static const char *b_str2int (const char *s, int base, lua_Integer *pn) { @@ -283,16 +260,6 @@ static int load_aux (lua_State *L, int status, int envidx) { } } - -static int luaB_loadfile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); - int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - int status = luaL_loadfilex(L, fname, mode); - return load_aux(L, status, env); -} - - /* ** {====================================================== ** Generic Read function @@ -353,22 +320,6 @@ static int luaB_load (lua_State *L) { /* }====================================================== */ -static int dofilecont (lua_State *L, int d1, lua_KContext d2) { - (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ - return lua_gettop(L) - 1; -} - - -static int luaB_dofile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - lua_settop(L, 1); - if (luaL_loadfile(L, fname) != LUA_OK) - return lua_error(L); - lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); - return dofilecont(L, 0, 0); -} - - static int luaB_assert (lua_State *L) { if (lua_toboolean(L, 1)) /* condition is true? */ return lua_gettop(L); /* return all arguments */ @@ -453,11 +404,9 @@ static int luaB_tostring (lua_State *L) { static const luaL_Reg base_funcs[] = { {"assert", luaB_assert}, {"collectgarbage", luaB_collectgarbage}, - {"dofile", luaB_dofile}, {"error", luaB_error}, {"getmetatable", luaB_getmetatable}, {"ipairs", luaB_ipairs}, - {"loadfile", luaB_loadfile}, {"load", luaB_load}, #if defined(LUA_COMPAT_LOADSTRING) {"loadstring", luaB_load}, @@ -465,7 +414,6 @@ static const luaL_Reg base_funcs[] = { {"next", luaB_next}, {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, - {"print", luaB_print}, {"rawequal", luaB_rawequal}, {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, diff --git a/src/lua/lmathlib.c b/src/lua/lmathlib.c index 7ef7e59..77e7996 100644 --- a/src/lua/lmathlib.c +++ b/src/lua/lmathlib.c @@ -405,6 +405,7 @@ LUAMOD_API int luaopen_math (lua_State *L) { lua_setfield(L, -2, "maxinteger"); lua_pushinteger(L, LUA_MININTEGER); lua_setfield(L, -2, "mininteger"); + lua_setglobal(L, "math"); return 1; } diff --git a/src/lua/lstrlib.c b/src/lua/lstrlib.c index b4bed7e..e3b5a67 100644 --- a/src/lua/lstrlib.c +++ b/src/lua/lstrlib.c @@ -1579,6 +1579,7 @@ static void createmetatable (lua_State *L) { LUAMOD_API int luaopen_string (lua_State *L) { luaL_newlib(L, strlib); createmetatable(L); + lua_setglobal(L, "string"); return 1; } diff --git a/src/lua/ltablib.c b/src/lua/ltablib.c index c534957..3f4ca3e 100644 --- a/src/lua/ltablib.c +++ b/src/lua/ltablib.c @@ -256,12 +256,10 @@ typedef unsigned int IdxT; ** is to copy them to an array of a known type and use the array values. */ static unsigned int l_randomizePivot (void) { - clock_t c = clock(); time_t t = time(NULL); - unsigned int buff[sof(c) + sof(t)]; + unsigned int buff[sof(t)]; unsigned int i, rnd = 0; - memcpy(buff, &c, sof(c) * sizeof(unsigned int)); - memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); + memcpy(buff, &t, sof(t) * sizeof(unsigned int)); for (i = 0; i < sof(buff); i++) rnd += buff[i]; return rnd; @@ -440,6 +438,7 @@ static const luaL_Reg tab_funcs[] = { LUAMOD_API int luaopen_table (lua_State *L) { luaL_newlib(L, tab_funcs); + lua_setglobal(L, "table"); #if defined(LUA_COMPAT_UNPACK) /* _G.unpack = table.unpack */ lua_getfield(L, -1, "unpack");