Add persistent script storage + hot-reload, verified live
lua_runtime now loads the running script from a new LittleFS-backed drivers::storage (joltwallet/littlefs, the project's fifth external managed component -- mounts the "storage" partition already reserved in partitions.csv), falling back to the compiled-in default script only on first boot. transport exposes GET/POST /api/script -- raw Lua text, not JSON, since json_helpers.h can't round-trip scripts safely -- relayed to lua_runtime via the same callback-registration pattern already used for LedColorHandler, keeping transport free of a dependency on lua_runtime. lua_runtime_reload() persists and hot-swaps to a fresh Lua state immediately, no reboot. Verified on real ESP32-C6 hardware: script loads via kvida-sdk, an edited script hot-reloads within one tick, and the edit survives a power cycle (proving LittleFS persistence, not just an in-RAM change). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace kvida {
|
||||
|
||||
// Creates the sandboxed Lua state and loads the default script (see
|
||||
// src/default_script.h). The script only *defines* the on_tick()
|
||||
// function -- call lua_runtime_tick() to actually run it. Call once at
|
||||
// boot.
|
||||
// Mounts storage and loads the script from there (see
|
||||
// components/drivers/include/storage.h), falling back to the compiled-in
|
||||
// default (src/default_script.h) if none is stored yet. The script only
|
||||
// *defines* the on_tick() function -- call lua_runtime_tick() to actually
|
||||
// run it. Call once at boot.
|
||||
void lua_runtime_init();
|
||||
|
||||
// Calls the script's on_tick() global function -- one "wake, execute
|
||||
@@ -14,4 +17,14 @@ void lua_runtime_init();
|
||||
// down the device.
|
||||
void lua_runtime_tick();
|
||||
|
||||
// Persists `script` to storage and hot-reloads it into a fresh Lua
|
||||
// state, replacing whatever's currently running -- no reboot needed.
|
||||
// Wired to transport::set_script_change_handler() by main.cpp.
|
||||
void lua_runtime_reload(const char *script, size_t len);
|
||||
|
||||
// Copies the currently-running script into buf (capacity cap), returns
|
||||
// the number of bytes written. Wired to transport::set_script_provider()
|
||||
// by main.cpp.
|
||||
size_t lua_runtime_current_script(char *buf, size_t cap);
|
||||
|
||||
} // namespace kvida
|
||||
|
||||
Reference in New Issue
Block a user