Skip to content
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
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ body:
- led_strip
- libpng
- libsodium
- lua
- esp_linenoise
- network_provisioning
- nghttp
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/upload_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
json_parser
led_strip
libsodium
lua
network_provisioning
nghttp
onewire_bus
Expand Down
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@
[submodule "cjson/cJSON"]
path = cjson/cJSON
url = https://github.com/DaveGamble/cJSON.git

[submodule "lua/lua"]
path = lua/lua
url = https://github.com/lua/lua.git
1 change: 1 addition & 0 deletions .idf_build_apps.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ manifest_file = [
"json_parser/.build-test-rules.yml",
"libpng/.build-test-rules.yml",
"libsodium/.build-test-rules.yml",
"lua/.build-test-rules.yml",
"onewire_bus/.build-test-rules.yml",
"pcap/.build-test-rules.yml",
"pid_ctrl/.build-test-rules.yml",
Expand Down
4 changes: 4 additions & 0 deletions lua/.build-test-rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lua/examples/lua_example:
enable:
- if: IDF_TARGET in ["esp32", "esp32c3"]
reason: "Sufficient to test on one Xtensa and one RISC-V target"
45 changes: 45 additions & 0 deletions lua/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
idf_component_register(
SRCS
"lua/lapi.c"
"lua/lauxlib.c"
"lua/lbaselib.c"
"lua/lcode.c"
"lua/lctype.c"
"lua/lcorolib.c"
"lua/ldblib.c"
"lua/ldebug.c"
"lua/ldo.c"
"lua/lfunc.c"
"lua/lgc.c"
"lua/linit.c"
"lua/liolib.c"
"lua/llex.c"
"lua/lmathlib.c"
"lua/lmem.c"
"lua/lopcodes.c"
"lua/loadlib.c"
"lua/loslib.c"
"lua/lstate.c"
"lua/ltable.c"
"lua/ltm.c"
"lua/lvm.c"
"lua/lobject.c"
"lua/lparser.c"
"lua/lstrlib.c"
"lua/lstring.c"
"lua/ltablib.c"
"lua/ldump.c"
"lua/lundump.c"
"lua/lutf8lib.c"
"lua/lzio.c"
INCLUDE_DIRS
"port/include"
"lua"
)

# Enable 32-bit mode for Lua (required for ESP32 and Xtensa/RISC-V architectures)
target_compile_definitions(${COMPONENT_LIB} PRIVATE LUA_32BITS=1)

# Suppress compiler warnings from upstream Lua code
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-function)

11 changes: 11 additions & 0 deletions lua/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
menu "Lua"
config LUA_MAXSTACK
int "Stack size limit"
default 1000000
help
Limits the size of the Lua stack.
Change it if you need a different limit. This limit is arbitrary;
its only purpose is to stop Lua from consuming unlimited stack
space (and to reserve some numbers for pseudo-indices).

endmenu
22 changes: 22 additions & 0 deletions lua/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Lua is licensed under the MIT license.

Copyright (C) 1994-2025 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
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59 changes: 59 additions & 0 deletions lua/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Lua - ESP-IDF Component

ESP-IDF component which wraps Lua from upstream repository - https://www.lua.org/

This component provides Lua, a powerful, efficient, lightweight, embeddable scripting language.

## Features

- Suitable for embedding Lua code inside ESP-IDF applications
- Configurable stack size limit
- MIT license

## Configuration option

- `LUA_MAXSTACK` - default value: 1000000 - Limits the size of the Lua stack.

Modify values:

```shell
idf.py menuconfig
```

## Usage

This component is suitable for embedding Lua scripts in ESP-IDF applications. For a complete example, see the `examples/lua_example` directory.

### Basic Usage

```c
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

void run_lua() {
lua_State *L = luaL_newstate();
luaL_openlibs(L);

// Run a simple Lua script
if (luaL_dostring(L, "print('Hello from Lua!')") != LUA_OK) {
printf("Error: %s\n", lua_tostring(L, -1));
}

lua_close(L);
}
```

## Changes to upstream

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`.

## License

Lua is licensed under the MIT license. See LICENSE file for details.

## Upstream

- Lua: https://www.lua.org/
- Repository: https://github.com/lua/lua

4 changes: 4 additions & 0 deletions lua/examples/lua_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(lua_example)
56 changes: 56 additions & 0 deletions lua/examples/lua_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Lua Example

This example demonstrates how to embed Lua in an ESP-IDF application.

## Features

- **Embedded Lua script execution**: Run Lua code directly from C strings
- **File-based Lua script execution**: Load and execute Lua scripts from LittleFS filesystem
- **Memory tracking**: Monitor memory usage throughout Lua operations
- **Error handling**: Proper error handling and reporting

## Example Contents

1. **Simple Embedded Script**: A basic Lua script that calculates and prints a value (answer = 42)
2. **Fibonacci Script**: A Lua script loaded from filesystem that calculates Fibonacci numbers

## Hardware Requirements

- Any ESP32 series board (ESP32, ESP32-S2, ESP32-S3, ESP32-C3, ESP32-C6, etc.)
- Minimum 4MB flash (for filesystem partition)

## Configuration

- The example uses LittleFS to store Lua scripts
- A custom partition table is included with an "assets" partition for Lua scripts

## Building and Flashing

```bash
idf.py build flash monitor
```

## Expected Output

```
I (xxx) lua_example: Lua Example Starting
I (xxx) lua_example: Initializing LittleFS filesystem
I (xxx) lua_example: Filesystem mounted at /assets
I (xxx) lua_example: Starting Lua test: Simple Embedded Script
The answer is: 42
I (xxx) lua_example: End of Lua test: Simple Embedded Script
I (xxx) lua_example: Starting Lua test from file: Fibonacci Script from File
Fibonacci of 10 is: 55
I (xxx) lua_example: End of Lua test from file: Fibonacci Script from File
I (xxx) lua_example: End of Lua example application.
```

## Customization

You can add your own Lua scripts to the `assets/` directory and execute them from your application code.

## See Also

- [Lua Component Documentation](../../README.md)
- [Lua Official Website](https://www.lua.org/)
- [Lua Reference Manual](https://www.lua.org/manual/5.5/)
11 changes: 11 additions & 0 deletions lua/examples/lua_example/assets/fibonacci.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- fibonacci.lua
-- This script calculates the 10th number in the Fibonacci sequence and prints it.

local function fibonacci(n)
if n <= 1 then return n end
return fibonacci(n - 1) + fibonacci(n - 2)
end

local fib_10 = fibonacci(10)
print('Fibonacci of 10 is: ' .. fib_10)
return fib_10
5 changes: 5 additions & 0 deletions lua/examples/lua_example/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
idf_component_register(SRCS "lua_example.c"
INCLUDE_DIRS ".")

# Note: you must have a partition named "assets" in your partition table
littlefs_create_partition_image(assets ../assets FLASH_IN_PROJECT)
4 changes: 4 additions & 0 deletions lua/examples/lua_example/main/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies:
lua:
override_path: "../../.."
joltwallet/littlefs: "^1.20.3"
Loading
Loading