Skip to content

Commit e22bf0d

Browse files
authored
Merge pull request #1 from Sasszem/master
Add lua library imports and improve print() function
2 parents e839a23 + 6126c4c commit e22bf0d

File tree

6 files changed

+33
-62
lines changed

6 files changed

+33
-62
lines changed

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=ESP8266-Arduino-Lua
2-
version=0.0.10
3-
author=François Dugast <[email protected]>
2+
version=0.0.20
3+
author=François Dugast <[email protected]>, Sasszem
44
maintainer=François Dugast <[email protected]>
55
sentence=Lua scripting engine integrated in Arduino for ESP8266
66
paragraph=

src/LuaWrapper.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,26 @@ extern "C" {
1818
delay(a);
1919
}
2020

21-
static int lua_wrapper_print(lua_State *lua) {
22-
String a = String(luaL_checkstring(lua, 1));
23-
Serial.println(a);
24-
}
21+
static int lua_wrapper_print (lua_State *L) {
22+
int n = lua_gettop(L); /* number of arguments */
23+
int i;
24+
lua_getglobal(L, "tostring");
25+
for (i=1; i<=n; i++) {
26+
const char *s;
27+
size_t l;
28+
lua_pushvalue(L, -1); /* function to be called */
29+
lua_pushvalue(L, i); /* value to print */
30+
lua_call(L, 1, 1);
31+
s = lua_tolstring(L, -1, &l); /* get result */
32+
if (s == NULL)
33+
return luaL_error(L, "'tostring' must return a string to 'print'");
34+
if (i>1) Serial.write("\t");
35+
Serial.write(s);
36+
lua_pop(L, 1); /* pop result */
37+
}
38+
Serial.println();
39+
return 0;
40+
}
2541

2642
static int lua_wrapper_millis(lua_State *lua) {
2743
lua_pushnumber(lua, (lua_Number) millis());
@@ -31,6 +47,12 @@ extern "C" {
3147

3248
LuaWrapper::LuaWrapper() {
3349
_state = luaL_newstate();
50+
51+
luaopen_base(_state);
52+
luaopen_table(_state);
53+
luaopen_string(_state);
54+
luaopen_math(_state);
55+
3456
lua_register(_state, "pinMode", lua_wrapper_pinMode);
3557
lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite);
3658
lua_register(_state, "delay", lua_wrapper_delay);

src/lua/lbaselib.c

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,6 @@
2020
#include "lauxlib.h"
2121
#include "lualib.h"
2222

23-
24-
static int luaB_print (lua_State *L) {
25-
int n = lua_gettop(L); /* number of arguments */
26-
int i;
27-
lua_getglobal(L, "tostring");
28-
for (i=1; i<=n; i++) {
29-
const char *s;
30-
size_t l;
31-
lua_pushvalue(L, -1); /* function to be called */
32-
lua_pushvalue(L, i); /* value to print */
33-
lua_call(L, 1, 1);
34-
s = lua_tolstring(L, -1, &l); /* get result */
35-
if (s == NULL)
36-
return luaL_error(L, "'tostring' must return a string to 'print'");
37-
if (i>1) lua_writestring("\t", 1);
38-
lua_writestring(s, l);
39-
lua_pop(L, 1); /* pop result */
40-
}
41-
lua_writeline();
42-
return 0;
43-
}
44-
45-
4623
#define SPACECHARS " \f\n\r\t\v"
4724

4825
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) {
283260
}
284261
}
285262

286-
287-
static int luaB_loadfile (lua_State *L) {
288-
const char *fname = luaL_optstring(L, 1, NULL);
289-
const char *mode = luaL_optstring(L, 2, NULL);
290-
int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
291-
int status = luaL_loadfilex(L, fname, mode);
292-
return load_aux(L, status, env);
293-
}
294-
295-
296263
/*
297264
** {======================================================
298265
** Generic Read function
@@ -353,22 +320,6 @@ static int luaB_load (lua_State *L) {
353320
/* }====================================================== */
354321

355322

356-
static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
357-
(void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
358-
return lua_gettop(L) - 1;
359-
}
360-
361-
362-
static int luaB_dofile (lua_State *L) {
363-
const char *fname = luaL_optstring(L, 1, NULL);
364-
lua_settop(L, 1);
365-
if (luaL_loadfile(L, fname) != LUA_OK)
366-
return lua_error(L);
367-
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
368-
return dofilecont(L, 0, 0);
369-
}
370-
371-
372323
static int luaB_assert (lua_State *L) {
373324
if (lua_toboolean(L, 1)) /* condition is true? */
374325
return lua_gettop(L); /* return all arguments */
@@ -453,19 +404,16 @@ static int luaB_tostring (lua_State *L) {
453404
static const luaL_Reg base_funcs[] = {
454405
{"assert", luaB_assert},
455406
{"collectgarbage", luaB_collectgarbage},
456-
{"dofile", luaB_dofile},
457407
{"error", luaB_error},
458408
{"getmetatable", luaB_getmetatable},
459409
{"ipairs", luaB_ipairs},
460-
{"loadfile", luaB_loadfile},
461410
{"load", luaB_load},
462411
#if defined(LUA_COMPAT_LOADSTRING)
463412
{"loadstring", luaB_load},
464413
#endif
465414
{"next", luaB_next},
466415
{"pairs", luaB_pairs},
467416
{"pcall", luaB_pcall},
468-
{"print", luaB_print},
469417
{"rawequal", luaB_rawequal},
470418
{"rawlen", luaB_rawlen},
471419
{"rawget", luaB_rawget},

src/lua/lmathlib.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ LUAMOD_API int luaopen_math (lua_State *L) {
405405
lua_setfield(L, -2, "maxinteger");
406406
lua_pushinteger(L, LUA_MININTEGER);
407407
lua_setfield(L, -2, "mininteger");
408+
lua_setglobal(L, "math");
408409
return 1;
409410
}
410411

src/lua/lstrlib.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,7 @@ static void createmetatable (lua_State *L) {
15791579
LUAMOD_API int luaopen_string (lua_State *L) {
15801580
luaL_newlib(L, strlib);
15811581
createmetatable(L);
1582+
lua_setglobal(L, "string");
15821583
return 1;
15831584
}
15841585

src/lua/ltablib.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,10 @@ typedef unsigned int IdxT;
256256
** is to copy them to an array of a known type and use the array values.
257257
*/
258258
static unsigned int l_randomizePivot (void) {
259-
clock_t c = clock();
260259
time_t t = time(NULL);
261-
unsigned int buff[sof(c) + sof(t)];
260+
unsigned int buff[sof(t)];
262261
unsigned int i, rnd = 0;
263-
memcpy(buff, &c, sof(c) * sizeof(unsigned int));
264-
memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
262+
memcpy(buff, &t, sof(t) * sizeof(unsigned int));
265263
for (i = 0; i < sof(buff); i++)
266264
rnd += buff[i];
267265
return rnd;
@@ -440,6 +438,7 @@ static const luaL_Reg tab_funcs[] = {
440438

441439
LUAMOD_API int luaopen_table (lua_State *L) {
442440
luaL_newlib(L, tab_funcs);
441+
lua_setglobal(L, "table");
443442
#if defined(LUA_COMPAT_UNPACK)
444443
/* _G.unpack = table.unpack */
445444
lua_getfield(L, -1, "unpack");

0 commit comments

Comments
 (0)