Skip to content

Commit 4eccbc4

Browse files
authored
Merge pull request #658 from georgik/feature/lua
Add Lua component (IEC-470)
2 parents b51d72e + 8a1c793 commit 4eccbc4

22 files changed

Lines changed: 471 additions & 0 deletions

.github/ISSUE_TEMPLATE/bug-report.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ body:
5454
- led_strip
5555
- libpng
5656
- libsodium
57+
- lua
5758
- esp_linenoise
5859
- network_provisioning
5960
- nghttp

.github/workflows/upload_component.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ jobs:
5757
json_parser
5858
led_strip
5959
libsodium
60+
lua
6061
network_provisioning
6162
nghttp
6263
onewire_bus

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@
7777
[submodule "cjson/cJSON"]
7878
path = cjson/cJSON
7979
url = https://github.com/DaveGamble/cJSON.git
80+
81+
[submodule "lua/lua"]
82+
path = lua/lua
83+
url = https://github.com/lua/lua.git

.idf_build_apps.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ manifest_file = [
2828
"json_parser/.build-test-rules.yml",
2929
"libpng/.build-test-rules.yml",
3030
"libsodium/.build-test-rules.yml",
31+
"lua/.build-test-rules.yml",
3132
"onewire_bus/.build-test-rules.yml",
3233
"pcap/.build-test-rules.yml",
3334
"pid_ctrl/.build-test-rules.yml",

lua/.build-test-rules.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lua/examples/lua_example:
2+
enable:
3+
- if: IDF_TARGET in ["esp32", "esp32c3"]
4+
reason: "Sufficient to test on one Xtensa and one RISC-V target"

lua/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
idf_component_register(
2+
SRCS
3+
"lua/lapi.c"
4+
"lua/lauxlib.c"
5+
"lua/lbaselib.c"
6+
"lua/lcode.c"
7+
"lua/lctype.c"
8+
"lua/lcorolib.c"
9+
"lua/ldblib.c"
10+
"lua/ldebug.c"
11+
"lua/ldo.c"
12+
"lua/lfunc.c"
13+
"lua/lgc.c"
14+
"lua/linit.c"
15+
"lua/liolib.c"
16+
"lua/llex.c"
17+
"lua/lmathlib.c"
18+
"lua/lmem.c"
19+
"lua/lopcodes.c"
20+
"lua/loadlib.c"
21+
"lua/loslib.c"
22+
"lua/lstate.c"
23+
"lua/ltable.c"
24+
"lua/ltm.c"
25+
"lua/lvm.c"
26+
"lua/lobject.c"
27+
"lua/lparser.c"
28+
"lua/lstrlib.c"
29+
"lua/lstring.c"
30+
"lua/ltablib.c"
31+
"lua/ldump.c"
32+
"lua/lundump.c"
33+
"lua/lutf8lib.c"
34+
"lua/lzio.c"
35+
INCLUDE_DIRS
36+
"port/include"
37+
"lua"
38+
)
39+
40+
# Enable 32-bit mode for Lua (required for ESP32 and Xtensa/RISC-V architectures)
41+
target_compile_definitions(${COMPONENT_LIB} PRIVATE LUA_32BITS=1)
42+
43+
# Suppress compiler warnings from upstream Lua code
44+
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-function)
45+

lua/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
menu "Lua"
2+
config LUA_MAXSTACK
3+
int "Stack size limit"
4+
default 1000000
5+
help
6+
Limits the size of the Lua stack.
7+
Change it if you need a different limit. This limit is arbitrary;
8+
its only purpose is to stop Lua from consuming unlimited stack
9+
space (and to reserve some numbers for pseudo-indices).
10+
11+
endmenu

lua/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Lua is licensed under the MIT license.
2+
3+
Copyright (C) 1994-2025 Lua.org, PUC-Rio.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lua/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Lua - ESP-IDF Component
2+
3+
ESP-IDF component which wraps Lua from upstream repository - https://www.lua.org/
4+
5+
This component provides Lua, a powerful, efficient, lightweight, embeddable scripting language.
6+
7+
## Features
8+
9+
- Suitable for embedding Lua code inside ESP-IDF applications
10+
- Configurable stack size limit
11+
- MIT license
12+
13+
## Configuration option
14+
15+
- `LUA_MAXSTACK` - default value: 1000000 - Limits the size of the Lua stack.
16+
17+
Modify values:
18+
19+
```shell
20+
idf.py menuconfig
21+
```
22+
23+
## Usage
24+
25+
This component is suitable for embedding Lua scripts in ESP-IDF applications. For a complete example, see the `examples/lua_example` directory.
26+
27+
### Basic Usage
28+
29+
```c
30+
#include "lua.h"
31+
#include "lualib.h"
32+
#include "lauxlib.h"
33+
34+
void run_lua() {
35+
lua_State *L = luaL_newstate();
36+
luaL_openlibs(L);
37+
38+
// Run a simple Lua script
39+
if (luaL_dostring(L, "print('Hello from Lua!')") != LUA_OK) {
40+
printf("Error: %s\n", lua_tostring(L, -1));
41+
}
42+
43+
lua_close(L);
44+
}
45+
```
46+
47+
## Changes to upstream
48+
49+
This component uses header injection to provide ESP-IDF specific configuration overrides without modifying the upstream Lua source code. The platform-specific settings (such as `LUA_32BITS` for ESP32 and Xtensa/RISC-V architectures) are defined in `lua/port/include/luaconf.h`.
50+
51+
## License
52+
53+
Lua is licensed under the MIT license. See LICENSE file for details.
54+
55+
## Upstream
56+
57+
- Lua: https://www.lua.org/
58+
- Repository: https://github.com/lua/lua
59+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
4+
project(lua_example)

0 commit comments

Comments
 (0)