Skip to content

Add lua library imports and improve print() function #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP8266-Arduino-Lua
version=0.0.10
author=François Dugast <[email protected]>
version=0.0.20
author=François Dugast <[email protected]>, Sasszem
maintainer=François Dugast <[email protected]>
sentence=Lua scripting engine integrated in Arduino for ESP8266
paragraph=
Expand Down
30 changes: 26 additions & 4 deletions src/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
Expand Down
52 changes: 0 additions & 52 deletions src/lua/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -453,19 +404,16 @@ 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},
#endif
{"next", luaB_next},
{"pairs", luaB_pairs},
{"pcall", luaB_pcall},
{"print", luaB_print},
{"rawequal", luaB_rawequal},
{"rawlen", luaB_rawlen},
{"rawget", luaB_rawget},
Expand Down
1 change: 1 addition & 0 deletions src/lua/lmathlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

1 change: 1 addition & 0 deletions src/lua/lstrlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

7 changes: 3 additions & 4 deletions src/lua/ltablib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down