From e0791aa1e23c916962b1ec990725c3e875c8c17f Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Wed, 30 Oct 2019 14:08:45 -0600 Subject: [PATCH 01/44] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index ad9a66c..b8e41fd 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,12 @@ This Arduino library provides the [lua](https://www.lua.org/) 5.3.4 scripting en Download the content of this Github repository as a ZIP archive by clicking on *Clone or download* then *Download ZIP*. Follow the instructions on [installing additional Arduino libraries](https://www.arduino.cc/en/Guide/Libraries#toc4) and navigate to the file downloaded previously. +## Resources used (minimal sketch, 4MB (no spiffs) + +Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. +Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. + + ## Arduino sketch examples After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP8266-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it: From f7e45c36ab707150b47d83e9afeb7064e33051a3 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 13:09:05 -0600 Subject: [PATCH 02/44] Update README.md --- README.md | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b8e41fd..2b690ce 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,8 @@ # ESP8266 Arduino Lua -This Arduino library provides the [lua](https://www.lua.org/) 5.3.4 scripting engine for ESP8266 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. As an example, the following standard Arduino functions are available in lua scripts as bindings: -* *pinMode()* -* *digitalWrite()* -* *delay()* -* *millis()* -* *print()* which redirects to *Serial.println()* +This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 scripting engine for ESP8266 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. -## Installation into the Arduino IDE - -Download the content of this Github repository as a ZIP archive by clicking on *Clone or download* then *Download ZIP*. Follow the instructions on [installing additional Arduino libraries](https://www.arduino.cc/en/Guide/Libraries#toc4) and navigate to the file downloaded previously. - -## Resources used (minimal sketch, 4MB (no spiffs) +## Used Resources (minimal sketch, 4MB (no spiffs)) Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. From 03673a21badf9b6f3351a24eb1a112f90445c9ce Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 13:34:11 -0600 Subject: [PATCH 03/44] Update README.md --- README.md | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2b690ce..abf8ec0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ESP8266 Arduino Lua -This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 scripting engine for ESP8266 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. +This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 (release) scripting engine for ESP8266 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. ## Used Resources (minimal sketch, 4MB (no spiffs)) @@ -23,21 +23,15 @@ My first test! print("Current uptime: "..millis()) # Executing script: Current uptime: 159926.0 -``` - -## Lua script examples - -The lua language syntax is described in the [reference manual](https://www.lua.org/manual/). It is extended by the Arduino functions listed above. -### Hello world -``` +# Enter the lua script and press Control-D when finished: print("Hello world!") -``` +# Executing script: +Hello world! -### Blinking LED -``` +# Enter the lua script and press Control-D when finished: pinLED = 2 period = 500 pinMode(pinLED, OUTPUT) @@ -50,4 +44,6 @@ do digitalWrite(pinLED, HIGH) delay(period) end +# Executing script: + ``` From 4c20b7117ced00702a46596bf35389e155cbb15a Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 13:43:04 -0600 Subject: [PATCH 04/44] Update LuaWrapper.cpp strip builtins ... passoff to sketch --- src/LuaWrapper.cpp | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index fb9b551..0ca795a 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -1,41 +1,16 @@ #include "LuaWrapper.h" extern "C" { - static int lua_wrapper_pinMode(lua_State *lua) { - int a = luaL_checkinteger(lua, 1); - int b = luaL_checkinteger(lua, 2); - pinMode(a, b); - } - - static int lua_wrapper_digitalWrite(lua_State *lua) { - int a = luaL_checkinteger(lua, 1); - int b = luaL_checkinteger(lua, 2); - digitalWrite(a, b); - } - - static int lua_wrapper_delay(lua_State *lua) { - int a = luaL_checkinteger(lua, 1); - delay(a); - } static int lua_wrapper_print(lua_State *lua) { String a = String(luaL_checkstring(lua, 1)); Serial.println(a); } - - static int lua_wrapper_millis(lua_State *lua) { - lua_pushnumber(lua, (lua_Number) millis()); - return 1; - } } LuaWrapper::LuaWrapper() { _state = luaL_newstate(); - lua_register(_state, "pinMode", lua_wrapper_pinMode); - lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite); - lua_register(_state, "delay", lua_wrapper_delay); lua_register(_state, "print", lua_wrapper_print); - lua_register(_state, "millis", lua_wrapper_millis); } String LuaWrapper::Lua_dostring(const String *script) { From e8a3743e3da0c89043906bef979ab80322428105 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 14:04:41 -0600 Subject: [PATCH 05/44] Update ExecuteScriptFromSerial.ino strip builtins ... passoff to sketch --- .../ExecuteScriptFromSerial.ino | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino index 81b7465..c21f424 100644 --- a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino +++ b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino @@ -2,7 +2,39 @@ LuaWrapper lua; +static int lua_wrapper_pinMode(lua_State *lua) { + int a = luaL_checkinteger(lua, 1); + int b = luaL_checkinteger(lua, 2); + pinMode(a, b); +} + +static int lua_wrapper_digitalWrite(lua_State *lua) { + int a = luaL_checkinteger(lua, 1); + int b = luaL_checkinteger(lua, 2); + digitalWrite(a, b); +} + +static int lua_wrapper_delay(lua_State *lua) { + int a = luaL_checkinteger(lua, 1); + delay(a); +} +/* +static int lua_wrapper_print(lua_State *lua) { + String a = String(luaL_checkstring(lua, 1)); + Serial.println(a); +} +*/ +static int lua_wrapper_millis(lua_State *lua) { + lua_pushnumber(lua, (lua_Number) millis()); + return 1; +} + void setup() { + lua.Lua_register("pinMode", (const lua_CFunction) &lua_wrapper_pinMoe); + lua.Lua_register("digitalWrite", (const lua_CFunction) &lua_wrapper_digitalWrite); + lua.Lua_register("delay", (const lua_CFunction) &lua_wrapper_delay); + //lua.Lua_register("print", (const lua_CFunction) &lua_wrapper_print); + lua.Lua_register("millis", (const lua_CFunction) &lua_wrapper_millis); Serial.begin(115200); } From c2cc19795c23ca245c22aac6ea4497ce17adf855 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 14:16:10 -0600 Subject: [PATCH 06/44] Update ExecuteScriptFromSerial.ino is this right? --- .../ExecuteScriptFromSerial.ino | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino index c21f424..ad21f00 100644 --- a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino +++ b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino @@ -2,30 +2,30 @@ LuaWrapper lua; -static int lua_wrapper_pinMode(lua_State *lua) { - int a = luaL_checkinteger(lua, 1); - int b = luaL_checkinteger(lua, 2); +static int lua_wrapper_pinMode(lua_State *lua_state) { + int a = luaL_checkinteger(lua_state, 1); + int b = luaL_checkinteger(lua_state, 2); pinMode(a, b); } -static int lua_wrapper_digitalWrite(lua_State *lua) { - int a = luaL_checkinteger(lua, 1); - int b = luaL_checkinteger(lua, 2); +static int lua_wrapper_digitalWrite(lua_State *lua_state) { + int a = luaL_checkinteger(lua_state, 1); + int b = luaL_checkinteger(lua_state, 2); digitalWrite(a, b); } -static int lua_wrapper_delay(lua_State *lua) { - int a = luaL_checkinteger(lua, 1); +static int lua_wrapper_delay(lua_State *lua_state) { + int a = luaL_checkinteger(lua_state, 1); delay(a); } /* -static int lua_wrapper_print(lua_State *lua) { - String a = String(luaL_checkstring(lua, 1)); +static int lua_wrapper_print(lua_State *lua_state) { + String a = String(luaL_checkstring(lua_state, 1)); Serial.println(a); } */ -static int lua_wrapper_millis(lua_State *lua) { - lua_pushnumber(lua, (lua_Number) millis()); +static int lua_wrapper_millis(lua_State *lua_state) { + lua_pushnumber(lua_state, (lua_Number) millis()); return 1; } From ae1762f0694102f16dc3be31259dd6618a1e5862 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 14:29:31 -0600 Subject: [PATCH 07/44] Update library.properties --- library.properties | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library.properties b/library.properties index b23f65b..92f8af6 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=ESP8266-Arduino-Lua -version=0.0.10 +version=0.0.20 author=François Dugast <1050329+fdu@users.noreply.github.com> -maintainer=François Dugast <1050329+fdu@users.noreply.github.com> -sentence=Lua scripting engine integrated in Arduino for ESP8266 +maintainer=Scotty Franzyshen +sentence=Lua scripting engine integrated in Arduino IDE for ESP8266 paragraph= category=Other -url=https://github.com/fdu/ESP8266-Arduino-Lua +url=https://github.com/sfranzyshen/ESP8266-Arduino-Lua architectures=esp8266 includes=LuaWrapper.h From 7a1db83f03d79a0f2584f3b4c30935fbafe6f746 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 15:59:03 -0600 Subject: [PATCH 08/44] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index abf8ec0..e64a73c 100644 --- a/README.md +++ b/README.md @@ -45,5 +45,6 @@ do delay(period) end # Executing script: - +LED on +LED off ``` From 3ffeb0a5f1a7d1037423ed46aff28b442d01b47a Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 16:03:13 -0600 Subject: [PATCH 09/44] Update LuaWrapper.cpp --- src/LuaWrapper.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index 0ca795a..d780871 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -1,16 +1,7 @@ #include "LuaWrapper.h" -extern "C" { - - static int lua_wrapper_print(lua_State *lua) { - String a = String(luaL_checkstring(lua, 1)); - Serial.println(a); - } -} - LuaWrapper::LuaWrapper() { _state = luaL_newstate(); - lua_register(_state, "print", lua_wrapper_print); } String LuaWrapper::Lua_dostring(const String *script) { From 640fcf8cf28a4f0a283dd7d90ce0e60bcd7da4a0 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 16:04:53 -0600 Subject: [PATCH 10/44] Update ExecuteScriptFromSerial.ino --- .../ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino index ad21f00..9ed60f3 100644 --- a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino +++ b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino @@ -18,22 +18,22 @@ static int lua_wrapper_delay(lua_State *lua_state) { int a = luaL_checkinteger(lua_state, 1); delay(a); } -/* + static int lua_wrapper_print(lua_State *lua_state) { String a = String(luaL_checkstring(lua_state, 1)); Serial.println(a); } -*/ + static int lua_wrapper_millis(lua_State *lua_state) { lua_pushnumber(lua_state, (lua_Number) millis()); return 1; } void setup() { - lua.Lua_register("pinMode", (const lua_CFunction) &lua_wrapper_pinMoe); + lua.Lua_register("pinMode", (const lua_CFunction) &lua_wrapper_pinMode); lua.Lua_register("digitalWrite", (const lua_CFunction) &lua_wrapper_digitalWrite); lua.Lua_register("delay", (const lua_CFunction) &lua_wrapper_delay); - //lua.Lua_register("print", (const lua_CFunction) &lua_wrapper_print); + lua.Lua_register("print", (const lua_CFunction) &lua_wrapper_print); lua.Lua_register("millis", (const lua_CFunction) &lua_wrapper_millis); Serial.begin(115200); } From 1dfdf0baeaf85a475e4f19a971262f7e8e848a69 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 16:13:56 -0600 Subject: [PATCH 11/44] Update HelloWorld.ino --- examples/HelloWorld/HelloWorld.ino | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/examples/HelloWorld/HelloWorld.ino b/examples/HelloWorld/HelloWorld.ino index 341db63..3a4212f 100644 --- a/examples/HelloWorld/HelloWorld.ino +++ b/examples/HelloWorld/HelloWorld.ino @@ -2,7 +2,13 @@ LuaWrapper lua; +static int lua_wrapper_print(lua_State *lua_state) { + String a = String(luaL_checkstring(lua_state, 1)); + Serial.println(a); +} + void setup() { + lua.Lua_register("print", (const lua_CFunction) &lua_wrapper_print); Serial.begin(115200); String script = String("print('Hello world!')"); Serial.println(lua.Lua_dostring(&script)); From dca72afafd53c14ca94647b51a891504f86e6005 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 16:33:18 -0600 Subject: [PATCH 12/44] Update README.md --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e64a73c..d18d053 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,13 @@ Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes fo ## Arduino sketch examples -After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP8266-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it: +After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP8266-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: + +. pinMode() +. digitalWrite() +. delay() +. millis() +. print() ``` # Enter the lua script and press Control-D when finished: From 5c67c506b477f262de43ce9f76dfc75083367456 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Fri, 1 Nov 2019 16:35:34 -0600 Subject: [PATCH 13/44] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index d18d053..072d47e 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,11 @@ Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes fo After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP8266-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: -. pinMode() -. digitalWrite() -. delay() -. millis() -. print() +- pinMode() +- digitalWrite() +- delay() +- millis() +- print() ``` # Enter the lua script and press Control-D when finished: @@ -25,7 +25,7 @@ print("My first test!") My first test! -# Enter the lua script and press Control-D when finished: +# Enter the lua script and press Control-D when finished- print("Current uptime: "..millis()) # Executing script: Current uptime: 159926.0 From 0b7ec4ec5c51a380e6c1693b313ef0575851f75c Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 10:00:57 -0600 Subject: [PATCH 14/44] Update README.md --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 072d47e..9d9b251 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# ESP8266 Arduino Lua +# ESP Arduino Lua -This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 (release) scripting engine for ESP8266 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. +This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 (release) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. -## Used Resources (minimal sketch, 4MB (no spiffs)) +## Used Resources (ESP8266 minimal sketch, 4MB (no spiffs)) Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. @@ -10,7 +10,7 @@ Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes fo ## Arduino sketch examples -After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP8266-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: +After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: - pinMode() - digitalWrite() From 07c9e9fa8a930ec8d7167d28dc853b995ecfd358 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 10:02:37 -0600 Subject: [PATCH 15/44] Update library.properties --- library.properties | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library.properties b/library.properties index 92f8af6..bc714f0 100644 --- a/library.properties +++ b/library.properties @@ -2,9 +2,9 @@ name=ESP8266-Arduino-Lua version=0.0.20 author=François Dugast <1050329+fdu@users.noreply.github.com> maintainer=Scotty Franzyshen -sentence=Lua scripting engine integrated in Arduino IDE for ESP8266 +sentence=Lua scripting engine integrated in Arduino IDE for ESP8266/ESP32 paragraph= category=Other -url=https://github.com/sfranzyshen/ESP8266-Arduino-Lua -architectures=esp8266 +url=https://github.com/sfranzyshen/ESP-Arduino-Lua +architectures=esp8266, esp32 includes=LuaWrapper.h From 39b12959acd376fb34497fbc0acb4ca8f3a5df5e Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 10:02:55 -0600 Subject: [PATCH 16/44] Update library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index bc714f0..4486712 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP8266-Arduino-Lua -version=0.0.20 +version=0.0.50 author=François Dugast <1050329+fdu@users.noreply.github.com> maintainer=Scotty Franzyshen sentence=Lua scripting engine integrated in Arduino IDE for ESP8266/ESP32 From 2838d60764b6281993390716f6fd97995bbd6895 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 10:23:27 -0600 Subject: [PATCH 17/44] Update LuaWrapper.cpp Added import for lua standard libraries: - base (tostring(), `tonumber(), etc.) - math - table (insert(), sort(), remove(), ...) - string (len(), match(), ...) Replaced print() function with the original version modified with Serial --- src/LuaWrapper.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index d780871..3f61fca 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -1,7 +1,35 @@ #include "LuaWrapper.h" +extern "C" { + 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; + } +} + LuaWrapper::LuaWrapper() { _state = luaL_newstate(); + luaopen_base(_state); + luaopen_table(_state); + luaopen_string(_state); + luaopen_math(_state); + lua_register(_state, "print", lua_wrapper_print); } String LuaWrapper::Lua_dostring(const String *script) { From c90ee2cf00448768bc7878b356031c000d4b79d4 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 10:45:07 -0600 Subject: [PATCH 18/44] Update lbaselib.c Added import for lua standard libraries: base (tostring(), `tonumber(), etc.) math table (insert(), sort(), remove(), ...) string (len(), match(), ...) Replaced print() function with the original version modified with Serial --- src/lua/lbaselib.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/lua/lbaselib.c b/src/lua/lbaselib.c index 6460e4f..a1a8bfb 100644 --- a/src/lua/lbaselib.c +++ b/src/lua/lbaselib.c @@ -20,28 +20,28 @@ #include "lauxlib.h" #include "lualib.h" - +/* static int luaB_print (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ + 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_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 */ + 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_pop(L, 1); // pop result } lua_writeline(); return 0; } - +*/ #define SPACECHARS " \f\n\r\t\v" @@ -283,15 +283,15 @@ 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 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); } - +*/ /* ** {====================================================== @@ -352,9 +352,9 @@ 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 */ + (void)d1; (void)d2; // only to match 'lua_Kfunction' prototype return lua_gettop(L) - 1; } @@ -367,7 +367,7 @@ static int luaB_dofile (lua_State *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? */ @@ -453,11 +453,11 @@ static int luaB_tostring (lua_State *L) { static const luaL_Reg base_funcs[] = { {"assert", luaB_assert}, {"collectgarbage", luaB_collectgarbage}, - {"dofile", luaB_dofile}, +// {"dofile", luaB_dofile}, {"error", luaB_error}, {"getmetatable", luaB_getmetatable}, {"ipairs", luaB_ipairs}, - {"loadfile", luaB_loadfile}, +// {"loadfile", luaB_loadfile}, {"load", luaB_load}, #if defined(LUA_COMPAT_LOADSTRING) {"loadstring", luaB_load}, @@ -465,7 +465,7 @@ static const luaL_Reg base_funcs[] = { {"next", luaB_next}, {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, - {"print", luaB_print}, +// {"print", luaB_print}, {"rawequal", luaB_rawequal}, {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, From 785b08285a6b567cb681773ffaa3036f049cee25 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 10:48:45 -0600 Subject: [PATCH 19/44] Update lmathlib.c Added import for lua standard libraries: base (tostring(), `tonumber(), etc.) math table (insert(), sort(), remove(), ...) string (len(), match(), ...) Replaced print() function with the original version modified with Serial --- src/lua/lmathlib.c | 1 + 1 file changed, 1 insertion(+) 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; } From d4b79886192bb101437e53a3489e14927b2559d6 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 10:50:53 -0600 Subject: [PATCH 20/44] Update lstrlib.c Added import for lua standard libraries: base (tostring(), `tonumber(), etc.) math table (insert(), sort(), remove(), ...) string (len(), match(), ...) Replaced print() function with the original version modified with Serial --- src/lua/lstrlib.c | 1 + 1 file changed, 1 insertion(+) 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; } From 6406afcb69f71a34acf1fe97f896ab9898e04793 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 11:36:12 -0600 Subject: [PATCH 21/44] Update ltablib.c Added import for lua standard libraries: base (tostring(), `tonumber(), etc.) math table (insert(), sort(), remove(), ...) string (len(), match(), ...) Replaced print() function with the original version modified with Serial --- src/lua/ltablib.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lua/ltablib.c b/src/lua/ltablib.c index c534957..c13715a 100644 --- a/src/lua/ltablib.c +++ b/src/lua/ltablib.c @@ -256,12 +256,14 @@ 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(); +// clock_t c = clock(); time_t t = time(NULL); - unsigned int buff[sof(c) + sof(t)]; +// 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, &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 +442,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"); From 5da9a7b84c0569a35cae27d3a2a781fd72981b75 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 11:49:00 -0600 Subject: [PATCH 22/44] Update luaconf.h fix compilation error `Error while detecting libraries included` --- src/lua/luaconf.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lua/luaconf.h b/src/lua/luaconf.h index 9eeeea6..4dc62b0 100644 --- a/src/lua/luaconf.h +++ b/src/lua/luaconf.h @@ -33,7 +33,7 @@ ** ensure that all software connected to Lua will be compiled with the ** same configuration. */ -/* #define LUA_32BITS */ +#define LUA_32BITS /* @@ -41,7 +41,7 @@ ** Define it if you want Lua to avoid the use of a few C99 features ** or Windows-specific features on Windows. */ -/* #define LUA_USE_C89 */ +#define LUA_USE_C89 /* From 977c3e68ade80649e91ba961e54e86b1b2bbd6d0 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 11:53:26 -0600 Subject: [PATCH 23/44] Update RegisterFunction.ino Fix some warnings --- examples/RegisterFunction/RegisterFunction.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/RegisterFunction/RegisterFunction.ino b/examples/RegisterFunction/RegisterFunction.ino index 8f96309..438c6d2 100644 --- a/examples/RegisterFunction/RegisterFunction.ino +++ b/examples/RegisterFunction/RegisterFunction.ino @@ -3,7 +3,9 @@ LuaWrapper lua; static int myFunction(lua_State *lua_state) { + (void*)lua_state; Serial.println("Hi from my C function"); + return 0; } void setup() { From 919361a77dd7ce71a513eb7bf1f027828f2fab6b Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 11:56:15 -0600 Subject: [PATCH 24/44] Update HelloWorld.ino --- examples/HelloWorld/HelloWorld.ino | 6 ------ 1 file changed, 6 deletions(-) diff --git a/examples/HelloWorld/HelloWorld.ino b/examples/HelloWorld/HelloWorld.ino index 3a4212f..341db63 100644 --- a/examples/HelloWorld/HelloWorld.ino +++ b/examples/HelloWorld/HelloWorld.ino @@ -2,13 +2,7 @@ LuaWrapper lua; -static int lua_wrapper_print(lua_State *lua_state) { - String a = String(luaL_checkstring(lua_state, 1)); - Serial.println(a); -} - void setup() { - lua.Lua_register("print", (const lua_CFunction) &lua_wrapper_print); Serial.begin(115200); String script = String("print('Hello world!')"); Serial.println(lua.Lua_dostring(&script)); From b097fd153eefe2dfe9ab1af7727f0c60775812c4 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 11:59:25 -0600 Subject: [PATCH 25/44] Update ExecuteScriptFromSerial.ino --- .../ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino index 9ed60f3..907bac0 100644 --- a/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino +++ b/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino @@ -6,22 +6,20 @@ static int lua_wrapper_pinMode(lua_State *lua_state) { int a = luaL_checkinteger(lua_state, 1); int b = luaL_checkinteger(lua_state, 2); pinMode(a, b); + return 0; } static int lua_wrapper_digitalWrite(lua_State *lua_state) { int a = luaL_checkinteger(lua_state, 1); int b = luaL_checkinteger(lua_state, 2); digitalWrite(a, b); + return 0; } static int lua_wrapper_delay(lua_State *lua_state) { int a = luaL_checkinteger(lua_state, 1); delay(a); -} - -static int lua_wrapper_print(lua_State *lua_state) { - String a = String(luaL_checkstring(lua_state, 1)); - Serial.println(a); + return 0; } static int lua_wrapper_millis(lua_State *lua_state) { @@ -33,7 +31,6 @@ void setup() { lua.Lua_register("pinMode", (const lua_CFunction) &lua_wrapper_pinMode); lua.Lua_register("digitalWrite", (const lua_CFunction) &lua_wrapper_digitalWrite); lua.Lua_register("delay", (const lua_CFunction) &lua_wrapper_delay); - lua.Lua_register("print", (const lua_CFunction) &lua_wrapper_print); lua.Lua_register("millis", (const lua_CFunction) &lua_wrapper_millis); Serial.begin(115200); } From 98596895134e1cfa70c6219f3336ca338a48a20e Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:14:04 -0600 Subject: [PATCH 26/44] Update README.md --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d9b251..fe285ed 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,13 @@ This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 (release) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. +Along with the Lua 5.3.5 Core the following Lua standard libraries are included: + +- base (print(), tostring(), tonumber(), etc.) +- math +- table (insert(), sort(), remove(), ...) +- string (len(), match(), ...) + ## Used Resources (ESP8266 minimal sketch, 4MB (no spiffs)) Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. @@ -16,7 +23,7 @@ After installing the library, some sketch examples are available from the *File* - digitalWrite() - delay() - millis() -- print() +- print() *(only builtin binding)* ``` # Enter the lua script and press Control-D when finished: From 64e8bdd79ebe6c3f505a237851072f711fc0f7d0 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:15:24 -0600 Subject: [PATCH 27/44] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fe285ed..c0a22c2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ESP Arduino Lua -This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 (release) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. +This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 (release) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. Along with the Lua 5.3.5 Core the following Lua standard libraries are included: From 5c18e5c4baa33b7a34313d0b7c1e04d00a1c95e5 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:21:36 -0600 Subject: [PATCH 28/44] Update README.md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index c0a22c2..e5b8efb 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,23 @@ end LED on LED off ``` + +## Arduino IDE Library examples +``` +#include + +LuaWrapper lua; + +void setup() { + Serial.begin(115200); + String script = String("print('Hello world!')"); + Serial.println(lua.Lua_dostring(&script)); +} + +void loop() { + +} +``` + + + From 80efce653c0ff93715d85a9d7e93866c4ba2158c Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:25:12 -0600 Subject: [PATCH 29/44] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e5b8efb..63ee1e5 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,7 @@ Along with the Lua 5.3.5 Core the following Lua standard libraries are included: Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. - -## Arduino sketch examples +## Sample sketch example: ExecuteScriptFromSerial.ino After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: @@ -62,7 +61,7 @@ LED on LED off ``` -## Arduino IDE Library examples +## Arduino IDE Library example: HelloWorld.ino ``` #include From 3cf0fe4aa6718d4ab3df377ebd77de204e7cdf0d Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:30:12 -0600 Subject: [PATCH 30/44] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 63ee1e5..743200d 100644 --- a/README.md +++ b/README.md @@ -78,5 +78,6 @@ void loop() { } ``` - +## The Lua Language: +[Lua 5.3 Reference Manual](https://www.lua.org/manual/5.3/) From 087a37a62a74098f7f269ddcee506660694c2436 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:32:49 -0600 Subject: [PATCH 31/44] Update RegisterFunction.ino --- examples/RegisterFunction/RegisterFunction.ino | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/RegisterFunction/RegisterFunction.ino b/examples/RegisterFunction/RegisterFunction.ino index 438c6d2..60da684 100644 --- a/examples/RegisterFunction/RegisterFunction.ino +++ b/examples/RegisterFunction/RegisterFunction.ino @@ -3,7 +3,6 @@ LuaWrapper lua; static int myFunction(lua_State *lua_state) { - (void*)lua_state; Serial.println("Hi from my C function"); return 0; } From 82e9729db6a7b10631431d4f9dcd9a8eaf81d4d7 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:35:25 -0600 Subject: [PATCH 32/44] Update library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 4486712..8f39bc8 100644 --- a/library.properties +++ b/library.properties @@ -2,7 +2,7 @@ name=ESP8266-Arduino-Lua version=0.0.50 author=François Dugast <1050329+fdu@users.noreply.github.com> maintainer=Scotty Franzyshen -sentence=Lua scripting engine integrated in Arduino IDE for ESP8266/ESP32 +sentence=Lua scripting engine integrated in Arduino IDE as a Library for ESP8266/ESP32 paragraph= category=Other url=https://github.com/sfranzyshen/ESP-Arduino-Lua From f39373d5e38686855079d240e80519e093143140 Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:45:25 -0600 Subject: [PATCH 33/44] Update library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 8f39bc8..d7b46ba 100644 --- a/library.properties +++ b/library.properties @@ -1,4 +1,4 @@ -name=ESP8266-Arduino-Lua +name=ESP-Arduino-Lua version=0.0.50 author=François Dugast <1050329+fdu@users.noreply.github.com> maintainer=Scotty Franzyshen From 467806e18fc8faecf1a9e3d7fbbac3f0b3f9511b Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 12:55:24 -0600 Subject: [PATCH 34/44] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 743200d..b8d7935 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ My first test! # Enter the lua script and press Control-D when finished- -print("Current uptime: "..millis()) +print("Current uptime: " .. millis()) # Executing script: Current uptime: 159926.0 From 3991bd65f9b76bf1eda7a3ebb1b6c3a90873975b Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 13:21:35 -0600 Subject: [PATCH 35/44] Update README.md --- README.md | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b8d7935..40054b2 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,6 @@ Along with the Lua 5.3.5 Core the following Lua standard libraries are included: - table (insert(), sort(), remove(), ...) - string (len(), match(), ...) -## Used Resources (ESP8266 minimal sketch, 4MB (no spiffs)) - -Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. -Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. - ## Sample sketch example: ExecuteScriptFromSerial.ino After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: @@ -60,6 +55,15 @@ end LED on LED off ``` +## Resources Used (ExecuteScriptFromSerial.ino) + +**ESP8266:** +Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. +Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. + +**ESP32:** +Sketch uses 310749 bytes (23%) of program storage space. Maximum is 1310720 bytes. +Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. ## Arduino IDE Library example: HelloWorld.ino ``` @@ -77,6 +81,15 @@ void loop() { } ``` +## Resources Used (HelloWorld.ino) + +**ESP8266:** +Sketch uses 365276 bytes (34%) of program storage space. Maximum is 1044464 bytes. +Global variables use 34712 bytes (42%) of dynamic memory, leaving 47208 bytes for local variables. Maximum is 81920 bytes. + +**ESP32:** +Sketch uses 309913 bytes (23%) of program storage space. Maximum is 1310720 bytes. +Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. ## The Lua Language: [Lua 5.3 Reference Manual](https://www.lua.org/manual/5.3/) From 0b3de1e0ac81fc4c08b024e36f517b6dc37d790d Mon Sep 17 00:00:00 2001 From: Scotty Franzyshen Date: Sat, 2 Nov 2019 13:26:48 -0600 Subject: [PATCH 36/44] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 40054b2..edf8628 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ESP Arduino Lua -This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 (release) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. +This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 ( [release](https://www.lua.org/ftp/lua-5.3.5.tar.gz) ) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. Along with the Lua 5.3.5 Core the following Lua standard libraries are included: From c6cc8104bdba414a65ad6e3db08e180b227ddd0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=8E=E4=BE=A8?= Date: Fri, 14 Jul 2023 20:12:24 +0800 Subject: [PATCH 37/44] Upgrade lua engine from 5.3.5 to 5.3.6 --- src/lua/lapi.c | 12 +++--- src/lua/lauxlib.c | 9 ++++- src/lua/lbaselib.c | 96 +++++++++++++++++++++++----------------------- src/lua/lcode.c | 2 +- src/lua/ldebug.c | 7 ++-- src/lua/liolib.c | 2 + src/lua/llex.c | 31 ++++++++------- src/lua/lobject.c | 2 +- src/lua/lparser.c | 3 ++ src/lua/ltablib.c | 8 ++-- src/lua/lua.c | 32 ++++++++-------- src/lua/lua.h | 7 ++-- src/lua/luac.c | 30 +++++++-------- src/lua/lundump.c | 24 ++++++++---- 14 files changed, 143 insertions(+), 122 deletions(-) diff --git a/src/lua/lapi.c b/src/lua/lapi.c index 02b7fab..711895b 100644 --- a/src/lua/lapi.c +++ b/src/lua/lapi.c @@ -1254,13 +1254,12 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { } -static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) { +static UpVal **getupvalref (lua_State *L, int fidx, int n) { LClosure *f; StkId fi = index2addr(L, fidx); api_check(L, ttisLclosure(fi), "Lua function expected"); f = clLvalue(fi); api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index"); - if (pf) *pf = f; return &f->upvals[n - 1]; /* get its upvalue pointer */ } @@ -1269,7 +1268,7 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { StkId fi = index2addr(L, fidx); switch (ttype(fi)) { case LUA_TLCL: { /* lua closure */ - return *getupvalref(L, fidx, n, NULL); + return *getupvalref(L, fidx, n); } case LUA_TCCL: { /* C closure */ CClosure *f = clCvalue(fi); @@ -1286,9 +1285,10 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) { LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1, int fidx2, int n2) { - LClosure *f1; - UpVal **up1 = getupvalref(L, fidx1, n1, &f1); - UpVal **up2 = getupvalref(L, fidx2, n2, NULL); + UpVal **up1 = getupvalref(L, fidx1, n1); + UpVal **up2 = getupvalref(L, fidx2, n2); + if (*up1 == *up2) + return; luaC_upvdeccount(L, *up1); *up1 = *up2; (*up1)->refcount++; diff --git a/src/lua/lauxlib.c b/src/lua/lauxlib.c index 8bdada5..ac68bd3 100644 --- a/src/lua/lauxlib.c +++ b/src/lua/lauxlib.c @@ -1011,8 +1011,13 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { free(ptr); return NULL; } - else - return realloc(ptr, nsize); + else { /* cannot fail when shrinking a block */ + void *newptr = realloc(ptr, nsize); + if (newptr == NULL && ptr != NULL && nsize <= osize) + return ptr; /* keep the original block */ + else /* no fail or not shrinking */ + return newptr; /* use the new block */ + } } diff --git a/src/lua/lbaselib.c b/src/lua/lbaselib.c index a1a8bfb..2eb303b 100644 --- a/src/lua/lbaselib.c +++ b/src/lua/lbaselib.c @@ -20,28 +20,28 @@ #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; -} -*/ + +// 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" @@ -283,15 +283,15 @@ 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); -} -*/ + +// 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); +// } + /* ** {====================================================== @@ -352,22 +352,22 @@ 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 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_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? */ @@ -453,11 +453,11 @@ static int luaB_tostring (lua_State *L) { static const luaL_Reg base_funcs[] = { {"assert", luaB_assert}, {"collectgarbage", luaB_collectgarbage}, -// {"dofile", luaB_dofile}, + // {"dofile", luaB_dofile}, {"error", luaB_error}, {"getmetatable", luaB_getmetatable}, {"ipairs", luaB_ipairs}, -// {"loadfile", luaB_loadfile}, + // {"loadfile", luaB_loadfile}, {"load", luaB_load}, #if defined(LUA_COMPAT_LOADSTRING) {"loadstring", luaB_load}, @@ -465,7 +465,7 @@ static const luaL_Reg base_funcs[] = { {"next", luaB_next}, {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, -// {"print", luaB_print}, + // {"print", luaB_print}, {"rawequal", luaB_rawequal}, {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, diff --git a/src/lua/lcode.c b/src/lua/lcode.c index 12619f5..dc7271d 100644 --- a/src/lua/lcode.c +++ b/src/lua/lcode.c @@ -1061,7 +1061,7 @@ static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) { /* -** Aplly prefix operation 'op' to expression 'e'. +** Apply prefix operation 'op' to expression 'e'. */ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP}; diff --git a/src/lua/ldebug.c b/src/lua/ldebug.c index e138929..bb0e1d4 100644 --- a/src/lua/ldebug.c +++ b/src/lua/ldebug.c @@ -133,10 +133,11 @@ static const char *upvalname (Proto *p, int uv) { static const char *findvararg (CallInfo *ci, int n, StkId *pos) { int nparams = clLvalue(ci->func)->p->numparams; - if (n >= cast_int(ci->u.l.base - ci->func) - nparams) + int nvararg = cast_int(ci->u.l.base - ci->func) - nparams; + if (n <= -nvararg) return NULL; /* no such vararg */ else { - *pos = ci->func + nparams + n; + *pos = ci->func + nparams - n; return "(*vararg)"; /* generic name for any vararg */ } } @@ -148,7 +149,7 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, StkId base; if (isLua(ci)) { if (n < 0) /* access to vararg values? */ - return findvararg(ci, -n, pos); + return findvararg(ci, n, pos); else { base = ci->u.l.base; name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci)); diff --git a/src/lua/liolib.c b/src/lua/liolib.c index 8a9e75c..027d4bd 100644 --- a/src/lua/liolib.c +++ b/src/lua/liolib.c @@ -277,6 +277,8 @@ static int io_popen (lua_State *L) { const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); LStream *p = newprefile(L); + luaL_argcheck(L, ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '\0'), + 2, "invalid mode"); p->f = l_popen(L, filename, mode); p->closef = &io_pclose; return (p->f == NULL) ? luaL_fileresult(L, 0, filename) : 1; diff --git a/src/lua/llex.c b/src/lua/llex.c index 66fd411..b6d9a46 100644 --- a/src/lua/llex.c +++ b/src/lua/llex.c @@ -244,12 +244,12 @@ static int read_numeral (LexState *ls, SemInfo *seminfo) { /* -** skip a sequence '[=*[' or ']=*]'; if sequence is well formed, return -** its number of '='s; otherwise, return a negative number (-1 iff there -** are no '='s after initial bracket) +** reads a sequence '[=*[' or ']=*]', leaving the last bracket. +** If sequence is well formed, return its number of '='s + 2; otherwise, +** return 1 if there is no '='s or 0 otherwise (an unfinished '[==...'). */ -static int skip_sep (LexState *ls) { - int count = 0; +static size_t skip_sep (LexState *ls) { + size_t count = 0; int s = ls->current; lua_assert(s == '[' || s == ']'); save_and_next(ls); @@ -257,11 +257,14 @@ static int skip_sep (LexState *ls) { save_and_next(ls); count++; } - return (ls->current == s) ? count : (-count) - 1; + return (ls->current == s) ? count + 2 + : (count == 0) ? 1 + : 0; + } -static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { +static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { int line = ls->linenumber; /* initial line (for error message) */ save_and_next(ls); /* skip 2nd '[' */ if (currIsNewline(ls)) /* string starts with a newline? */ @@ -295,8 +298,8 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { } } endloop: if (seminfo) - seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), - luaZ_bufflen(ls->buff) - 2*(2 + sep)); + seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, + luaZ_bufflen(ls->buff) - 2 * sep); } @@ -444,9 +447,9 @@ static int llex (LexState *ls, SemInfo *seminfo) { /* else is a comment */ next(ls); if (ls->current == '[') { /* long comment? */ - int sep = skip_sep(ls); + size_t sep = skip_sep(ls); luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ - if (sep >= 0) { + if (sep >= 2) { read_long_string(ls, NULL, sep); /* skip long comment */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ break; @@ -458,12 +461,12 @@ static int llex (LexState *ls, SemInfo *seminfo) { break; } case '[': { /* long string or simply '[' */ - int sep = skip_sep(ls); - if (sep >= 0) { + size_t sep = skip_sep(ls); + if (sep >= 2) { read_long_string(ls, seminfo, sep); return TK_STRING; } - else if (sep != -1) /* '[=...' missing second bracket */ + else if (sep == 0) /* '[=...' missing second bracket */ lexerror(ls, "invalid long string delimiter", TK_STRING); return '['; } diff --git a/src/lua/lobject.c b/src/lua/lobject.c index 2218c8c..355bf58 100644 --- a/src/lua/lobject.c +++ b/src/lua/lobject.c @@ -266,7 +266,7 @@ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) { ** - 'n'/'N' means 'inf' or 'nan' (which should be rejected) ** - '.' just optimizes the search for the common case (nothing special) ** This function accepts both the current locale or a dot as the radix -** mark. If the convertion fails, it may mean number has a dot but +** mark. If the conversion fails, it may mean number has a dot but ** locale accepts something else. In that case, the code copies 's' ** to a buffer (because 's' is read-only), changes the dot to the ** current locale radix mark, and tries to convert again. diff --git a/src/lua/lparser.c b/src/lua/lparser.c index cc54de4..2f41e00 100644 --- a/src/lua/lparser.c +++ b/src/lua/lparser.c @@ -544,6 +544,7 @@ static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) { fs->bl = NULL; f = fs->f; f->source = ls->source; + luaC_objbarrier(ls->L, f, f->source); f->maxstacksize = 2; /* registers 0/1 are always valid */ enterblock(fs, bl, 0); } @@ -1616,6 +1617,7 @@ static void mainfunc (LexState *ls, FuncState *fs) { fs->f->is_vararg = 1; /* main function is always declared vararg */ init_exp(&v, VLOCAL, 0); /* create and... */ newupvalue(fs, ls->envn, &v); /* ...set environment upvalue */ + luaC_objbarrier(ls->L, fs->f, ls->envn); luaX_next(ls); /* read first token */ statlist(ls); /* parse main body */ check(ls, TK_EOS); @@ -1634,6 +1636,7 @@ LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, sethvalue(L, L->top, lexstate.h); /* anchor it */ luaD_inctop(L); funcstate.f = cl->p = luaF_newproto(L); + luaC_objbarrier(L, cl, cl->p); funcstate.f->source = luaS_new(L, name); /* create and anchor TString */ lua_assert(iswhite(funcstate.f)); /* do not need barrier here */ lexstate.buff = buff; diff --git a/src/lua/ltablib.c b/src/lua/ltablib.c index c13715a..b516a9b 100644 --- a/src/lua/ltablib.c +++ b/src/lua/ltablib.c @@ -256,13 +256,13 @@ 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(); + // clock_t c = clock(); time_t t = time(NULL); -// unsigned int buff[sof(c) + sof(t)]; + // 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, &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]; diff --git a/src/lua/lua.c b/src/lua/lua.c index 114711a..8c90642 100644 --- a/src/lua/lua.c +++ b/src/lua/lua.c @@ -593,20 +593,20 @@ static int pmain (lua_State *L) { } -//int main (int argc, char **argv) { -// int status, result; -// lua_State *L = luaL_newstate(); /* create state */ -// if (L == NULL) { -// l_message(argv[0], "cannot create state: not enough memory"); -// return EXIT_FAILURE; -// } -// lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ -// lua_pushinteger(L, argc); /* 1st argument */ -// lua_pushlightuserdata(L, argv); /* 2nd argument */ -// status = lua_pcall(L, 2, 1, 0); /* do the call */ -// result = lua_toboolean(L, -1); /* get result */ -// report(L, status); -// lua_close(L); -// return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; -//} +// int main (int argc, char **argv) { +// int status, result; +// lua_State *L = luaL_newstate(); /* create state */ +// if (L == NULL) { +// l_message(argv[0], "cannot create state: not enough memory"); +// return EXIT_FAILURE; +// } +// lua_pushcfunction(L, &pmain); /* to call 'pmain' in protected mode */ +// lua_pushinteger(L, argc); /* 1st argument */ +// lua_pushlightuserdata(L, argv); /* 2nd argument */ +// status = lua_pcall(L, 2, 1, 0); /* do the call */ +// result = lua_toboolean(L, -1); /* get result */ +// report(L, status); +// lua_close(L); +// return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE; +// } diff --git a/src/lua/lua.h b/src/lua/lua.h index c236e36..9394c5e 100644 --- a/src/lua/lua.h +++ b/src/lua/lua.h @@ -1,5 +1,4 @@ /* -** $Id: lua.h,v 1.332.1.2 2018/06/13 16:58:17 roberto Exp $ ** Lua - A Scripting Language ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) ** See Copyright Notice at the end of this file @@ -19,11 +18,11 @@ #define LUA_VERSION_MAJOR "5" #define LUA_VERSION_MINOR "3" #define LUA_VERSION_NUM 503 -#define LUA_VERSION_RELEASE "5" +#define LUA_VERSION_RELEASE "6" #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE -#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2018 Lua.org, PUC-Rio" +#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2020 Lua.org, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes" @@ -460,7 +459,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2018 Lua.org, PUC-Rio. +* Copyright (C) 1994-2020 Lua.org, PUC-Rio. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/src/lua/luac.c b/src/lua/luac.c index 549ad39..4aab871 100644 --- a/src/lua/luac.c +++ b/src/lua/luac.c @@ -189,21 +189,21 @@ static int pmain(lua_State* L) return 0; } -int main(int argc, char* argv[]) -{ - lua_State* L; - int i=doargs(argc,argv); - argc-=i; argv+=i; - if (argc<=0) usage("no input files given"); - L=luaL_newstate(); - if (L==NULL) fatal("cannot create state: not enough memory"); - lua_pushcfunction(L,&pmain); - lua_pushinteger(L,argc); - lua_pushlightuserdata(L,argv); - if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); - lua_close(L); - return EXIT_SUCCESS; -} +// int main(int argc, char* argv[]) +// { +// lua_State* L; +// int i=doargs(argc,argv); +// argc-=i; argv+=i; +// if (argc<=0) usage("no input files given"); +// L=luaL_newstate(); +// if (L==NULL) fatal("cannot create state: not enough memory"); +// lua_pushcfunction(L,&pmain); +// lua_pushinteger(L,argc); +// lua_pushlightuserdata(L,argv); +// if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1)); +// lua_close(L); +// return EXIT_SUCCESS; +// } /* ** $Id: luac.c,v 1.76 2018/06/19 01:32:02 lhf Exp $ diff --git a/src/lua/lundump.c b/src/lua/lundump.c index 7a67d75..edf9eb8 100644 --- a/src/lua/lundump.c +++ b/src/lua/lundump.c @@ -85,8 +85,10 @@ static lua_Integer LoadInteger (LoadState *S) { } -static TString *LoadString (LoadState *S) { +static TString *LoadString (LoadState *S, Proto *p) { + lua_State *L = S->L; size_t size = LoadByte(S); + TString *ts; if (size == 0xFF) LoadVar(S, size); if (size == 0) @@ -94,13 +96,17 @@ static TString *LoadString (LoadState *S) { else if (--size <= LUAI_MAXSHORTLEN) { /* short string? */ char buff[LUAI_MAXSHORTLEN]; LoadVector(S, buff, size); - return luaS_newlstr(S->L, buff, size); + ts = luaS_newlstr(L, buff, size); } else { /* long string */ - TString *ts = luaS_createlngstrobj(S->L, size); + ts = luaS_createlngstrobj(L, size); + setsvalue2s(L, L->top, ts); /* anchor it ('loadVector' can GC) */ + luaD_inctop(L); LoadVector(S, getstr(ts), size); /* load directly in final place */ - return ts; + L->top--; /* pop string */ } + luaC_objbarrier(L, p, ts); + return ts; } @@ -140,7 +146,7 @@ static void LoadConstants (LoadState *S, Proto *f) { break; case LUA_TSHRSTR: case LUA_TLNGSTR: - setsvalue2n(S->L, o, LoadString(S)); + setsvalue2n(S->L, o, LoadString(S, f)); break; default: lua_assert(0); @@ -158,6 +164,7 @@ static void LoadProtos (LoadState *S, Proto *f) { f->p[i] = NULL; for (i = 0; i < n; i++) { f->p[i] = luaF_newproto(S->L); + luaC_objbarrier(S->L, f, f->p[i]); LoadFunction(S, f->p[i], f->source); } } @@ -189,18 +196,18 @@ static void LoadDebug (LoadState *S, Proto *f) { for (i = 0; i < n; i++) f->locvars[i].varname = NULL; for (i = 0; i < n; i++) { - f->locvars[i].varname = LoadString(S); + f->locvars[i].varname = LoadString(S, f); f->locvars[i].startpc = LoadInt(S); f->locvars[i].endpc = LoadInt(S); } n = LoadInt(S); for (i = 0; i < n; i++) - f->upvalues[i].name = LoadString(S); + f->upvalues[i].name = LoadString(S, f); } static void LoadFunction (LoadState *S, Proto *f, TString *psource) { - f->source = LoadString(S); + f->source = LoadString(S, f); if (f->source == NULL) /* no source in dump? */ f->source = psource; /* reuse parent's source */ f->linedefined = LoadInt(S); @@ -271,6 +278,7 @@ LClosure *luaU_undump(lua_State *L, ZIO *Z, const char *name) { setclLvalue(L, L->top, cl); luaD_inctop(L); cl->p = luaF_newproto(L); + luaC_objbarrier(L, cl, cl->p); LoadFunction(&S, cl->p, NULL); lua_assert(cl->nupvalues == cl->p->sizeupvalues); luai_verifycode(L, buff, cl->p); From 66c0a61f8da8253d5b73ec1f340fca2b9092d953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=8E=E4=BE=A8?= Date: Fri, 14 Jul 2023 20:13:20 +0800 Subject: [PATCH 38/44] Add lua script demo --- examples/ExecuteScriptFromSerial/test.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 examples/ExecuteScriptFromSerial/test.lua diff --git a/examples/ExecuteScriptFromSerial/test.lua b/examples/ExecuteScriptFromSerial/test.lua new file mode 100644 index 0000000..ad7f9bb --- /dev/null +++ b/examples/ExecuteScriptFromSerial/test.lua @@ -0,0 +1,20 @@ +print(string.format("Lua version is %s", _VERSION)) + +INPUT = 0 +OUTPUT = 1 +HIGH = 1 +LOW = 0 +LED_BUILTIN = 13 + +pinMode(LED_BUILTIN, OUTPUT) + +for i=1,5,1 +do + print(i) + print("LED ON") + digitalWrite(LED_BUILTIN, HIGH) + delay(1000) + print("LED OFF") + digitalWrite(LED_BUILTIN, LOW) + delay(1000) +end From c812fb6df06adade0e50ffeb4a8706edb9271917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=8E=E4=BE=A8?= Date: Fri, 14 Jul 2023 20:13:49 +0800 Subject: [PATCH 39/44] Upgrade config from ESP-Arduino-Lua to Arduin-Lua --- library.properties | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/library.properties b/library.properties index d7b46ba..03bc89a 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,11 @@ -name=ESP-Arduino-Lua -version=0.0.50 +name=Arduino-Lua +version=0.1.0 author=François Dugast <1050329+fdu@users.noreply.github.com> maintainer=Scotty Franzyshen -sentence=Lua scripting engine integrated in Arduino IDE as a Library for ESP8266/ESP32 +maintainer=HonestQiao +sentence=Lua scripting engine integrated in Arduino IDE as a Library for ESP8266/ESP32/ArduinoUnoR4 paragraph= category=Other -url=https://github.com/sfranzyshen/ESP-Arduino-Lua -architectures=esp8266, esp32 +url=https://github.com/HonestQiao/Arduino-Lua +architectures=esp8266, esp32, renesas_uno includes=LuaWrapper.h From 234aca5568086d6b97d8cebd265f00841c3ba877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=8E=E4=BE=A8?= Date: Fri, 14 Jul 2023 20:17:20 +0800 Subject: [PATCH 40/44] update README and add Simplified Chinese README --- README.md | 16 +++++--- README_CN.md | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 README_CN.md diff --git a/README.md b/README.md index edf8628..169c2ce 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ -# ESP Arduino Lua +* [中文版本](README_cn.md) -This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 ( [release](https://www.lua.org/ftp/lua-5.3.5.tar.gz) ) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. +# Arduino Lua -Along with the Lua 5.3.5 Core the following Lua standard libraries are included: +This Arduino library provides the [lua](https://www.lua.org/) 5.3.6 ( [release](https://www.lua.org/ftp/lua-5.3.6.tar.gz) ) scripting engine for ESP8266/ESP32/ArduinoUnoR4 sketches. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. + +Along with the Lua 5.3.6 Core the following Lua standard libraries are included: - base (print(), tostring(), tonumber(), etc.) - math @@ -11,7 +13,7 @@ Along with the Lua 5.3.5 Core the following Lua standard libraries are included: ## Sample sketch example: ExecuteScriptFromSerial.ino -After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: +After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: - pinMode() - digitalWrite() @@ -65,8 +67,12 @@ Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes fo Sketch uses 310749 bytes (23%) of program storage space. Maximum is 1310720 bytes. Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. +**Arduino Uno R4:** +Sketch uses 133756 bytes (51%) of program storage space. Maximum is 262144 bytes. +Global variables use 2836 bytes (8%) of dynamic memory, leaving 29932 bytes for local variables. Maximum is 32768 bytes. + ## Arduino IDE Library example: HelloWorld.ino -``` +``` #include LuaWrapper lua; diff --git a/README_CN.md b/README_CN.md new file mode 100644 index 0000000..7175e3d --- /dev/null +++ b/README_CN.md @@ -0,0 +1,102 @@ +# Arduino Lua + +本Arduino扩展库为ESP8266/ESP32/ArduinoUnoR4提供 [lua](https://www.lua.org/) 5.3.6 ([正式版](https://www.lua.org/ftp/lua-5.3.6.tar.gz)) 的脚本运行引擎。 使用该扩展库,可以在Arduino上动态运行Lua代码,而不必重新编译和烧录固件。 + +除了 Lua 5.3.6 核心功能以外,还包含了如下的 Lua 标准库: + +- base (print(), tostring(), tonumber(), etc.) +- math +- table (insert(), sort(), remove(), ...) +- string (len(), match(), ...) + +## 示例: ExecuteScriptFromSerial.ino + +安装本扩展后,在 *File* 菜单中的 *Examples* 下的 *Arduino-Lua* 中 会提供部分示例。示例中包含 **ExecuteScriptFromSerial** ,可以通过串口输入 Lua 脚本并执行。例如,如下的标准 Arduino 函数,可以被绑定从而在 lua 脚本中使用: + +- pinMode() +- digitalWrite() +- delay() +- millis() +- print() *(only builtin binding)* + +``` +# Enter the lua script and press Control-D when finished: +# 输入如下的 lua脚本,然后按 Ctrl+D 运行: +print("My first test!") +# Executing script: +# 运行脚本: +My first test! + + +# Enter the lua script and press Control-D when finished- +print("Current uptime: " .. millis()) +# Executing script: +Current uptime: 159926.0 + + +# Enter the lua script and press Control-D when finished: +print("Hello world!") +# Executing script: +Hello world! + + +# Enter the lua script and press Control-D when finished: +pinLED = 2 +period = 500 +pinMode(pinLED, OUTPUT) +while(true) +do + print("LED on") + digitalWrite(pinLED, LOW) + delay(period) + print("LED off") + digitalWrite(pinLED, HIGH) + delay(period) +end +# Executing script: +LED on +LED off +``` +## 资源使用 (ExecuteScriptFromSerial.ino) + +**ESP8266:** +Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. +Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. + +**ESP32:** +Sketch uses 310749 bytes (23%) of program storage space. Maximum is 1310720 bytes. +Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. + +**Arduino Uno R4:** +Sketch uses 133756 bytes (51%) of program storage space. Maximum is 262144 bytes. +Global variables use 2836 bytes (8%) of dynamic memory, leaving 29932 bytes for local variables. Maximum is 32768 bytes. + +## 示例: HelloWorld.ino +``` +#include + +LuaWrapper lua; + +void setup() { + Serial.begin(115200); + String script = String("print('Hello world!')"); + Serial.println(lua.Lua_dostring(&script)); +} + +void loop() { + +} +``` +## 资源使用 (HelloWorld.ino) + +**ESP8266:** +Sketch uses 365276 bytes (34%) of program storage space. Maximum is 1044464 bytes. +Global variables use 34712 bytes (42%) of dynamic memory, leaving 47208 bytes for local variables. Maximum is 81920 bytes. + +**ESP32:** +Sketch uses 309913 bytes (23%) of program storage space. Maximum is 1310720 bytes. +Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. + +## Lua 语言: +[Lua 5.3 参考手册](https://www.lua.org/manual/5.3/) + From 088f5ef18f921acba3aaa4d1f40737dcd2edae3a Mon Sep 17 00:00:00 2001 From: HonestQiao Date: Fri, 14 Jul 2023 20:19:37 +0800 Subject: [PATCH 41/44] Update README.md update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 169c2ce..b13bd88 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -* [中文版本](README_cn.md) +* [中文版本](README_CN.md) # Arduino Lua From f07cd5bb3a7f3ba3abf06565bf3393b841a339bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=8E=E4=BE=A8?= Date: Fri, 14 Jul 2023 20:26:31 +0800 Subject: [PATCH 42/44] update test.lua --- README_CN.md | 2 ++ examples/ExecuteScriptFromSerial/test.lua | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/README_CN.md b/README_CN.md index 7175e3d..87b5097 100644 --- a/README_CN.md +++ b/README_CN.md @@ -1,3 +1,5 @@ +* [English Version](README.md) + # Arduino Lua 本Arduino扩展库为ESP8266/ESP32/ArduinoUnoR4提供 [lua](https://www.lua.org/) 5.3.6 ([正式版](https://www.lua.org/ftp/lua-5.3.6.tar.gz)) 的脚本运行引擎。 使用该扩展库,可以在Arduino上动态运行Lua代码,而不必重新编译和烧录固件。 diff --git a/examples/ExecuteScriptFromSerial/test.lua b/examples/ExecuteScriptFromSerial/test.lua index ad7f9bb..9a313e4 100644 --- a/examples/ExecuteScriptFromSerial/test.lua +++ b/examples/ExecuteScriptFromSerial/test.lua @@ -1,9 +1,5 @@ print(string.format("Lua version is %s", _VERSION)) -INPUT = 0 -OUTPUT = 1 -HIGH = 1 -LOW = 0 LED_BUILTIN = 13 pinMode(LED_BUILTIN, OUTPUT) From ec5194dd54b6ef6991460df0ed0585abf71845d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=8E=E4=BE=A8?= Date: Fri, 14 Jul 2023 22:01:53 +0800 Subject: [PATCH 43/44] update supported devices --- README.md | 8 +++++++- README_CN.md | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b13bd88..097bf2a 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,13 @@ # Arduino Lua -This Arduino library provides the [lua](https://www.lua.org/) 5.3.6 ( [release](https://www.lua.org/ftp/lua-5.3.6.tar.gz) ) scripting engine for ESP8266/ESP32/ArduinoUnoR4 sketches. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. +This Arduino library provides the [lua](https://www.lua.org/) 5.3.6 ( [release](https://www.lua.org/ftp/lua-5.3.6.tar.gz) ) scripting engine for supported devices. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. + +Supported devices: +* ESP8266 +* ESP32/ESP32S2/ESP32C3/ESP32S3 +* Arduino Uno R4 Minima/WiFi +* Raspberry Pi Pico/Pico W Along with the Lua 5.3.6 Core the following Lua standard libraries are included: diff --git a/README_CN.md b/README_CN.md index 87b5097..2e4bb49 100644 --- a/README_CN.md +++ b/README_CN.md @@ -2,7 +2,13 @@ # Arduino Lua -本Arduino扩展库为ESP8266/ESP32/ArduinoUnoR4提供 [lua](https://www.lua.org/) 5.3.6 ([正式版](https://www.lua.org/ftp/lua-5.3.6.tar.gz)) 的脚本运行引擎。 使用该扩展库,可以在Arduino上动态运行Lua代码,而不必重新编译和烧录固件。 +本Arduino扩展库为支持的设备提供 [lua](https://www.lua.org/) 5.3.6 ([正式版](https://www.lua.org/ftp/lua-5.3.6.tar.gz)) 的脚本运行引擎。 使用该扩展库,可以在Arduino上动态运行Lua代码,而不必重新编译和烧录固件。 + +支持的设备: +* ESP8266 +* ESP32/ESP32S2/ESP32C3/ESP32S3 +* Arduino Uno R4 Minima/WiFi +* Raspberry Pi Pico/Pico W 除了 Lua 5.3.6 核心功能以外,还包含了如下的 Lua 标准库: From f127c8e95ea51fb8cb69ea4d882c43bc7e861d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8D=8E=E4=BE=A8?= Date: Fri, 14 Jul 2023 22:09:31 +0800 Subject: [PATCH 44/44] update resources Used on pico --- README.md | 4 ++++ README_CN.md | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 097bf2a..6807ce4 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,10 @@ Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes fo Sketch uses 133756 bytes (51%) of program storage space. Maximum is 262144 bytes. Global variables use 2836 bytes (8%) of dynamic memory, leaving 29932 bytes for local variables. Maximum is 32768 bytes. +**Raspberry Pi Pico:** +Sketch uses 124736 bytes (5%) of program storage space. Maximum is 2093056 bytes. +Global variables use 10352 bytes (3%) of dynamic memory, leaving 251792 bytes for local variables. Maximum is 262144 bytes. + ## Arduino IDE Library example: HelloWorld.ino ``` #include diff --git a/README_CN.md b/README_CN.md index 2e4bb49..d94ad90 100644 --- a/README_CN.md +++ b/README_CN.md @@ -79,6 +79,10 @@ Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes fo Sketch uses 133756 bytes (51%) of program storage space. Maximum is 262144 bytes. Global variables use 2836 bytes (8%) of dynamic memory, leaving 29932 bytes for local variables. Maximum is 32768 bytes. +**Raspberry Pi Pico:** +Sketch uses 124736 bytes (5%) of program storage space. Maximum is 2093056 bytes. +Global variables use 10352 bytes (3%) of dynamic memory, leaving 251792 bytes for local variables. Maximum is 262144 bytes. + ## 示例: HelloWorld.ino ``` #include