summaryrefslogtreecommitdiff
path: root/src/lua
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-12-13 16:10:08 +1100
committerjacqueline <me@jacqueline.id.au>2023-12-13 16:10:08 +1100
commit64b106c13e18c33be0f2b0de532054e0ed3f731d (patch)
treeb54b1c90d941bc456b4d51e864970720bdf2d648 /src/lua
parent5a2f0b08e0e3f20cda977b510b680d5843ae7283 (diff)
downloadtangara-fw-64b106c13e18c33be0f2b0de532054e0ed3f731d.tar.gz
add a cool lua repl
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/CMakeLists.txt3
-rw-r--r--src/lua/bridge.cpp11
-rw-r--r--src/lua/include/lua_thread.hpp1
-rw-r--r--src/lua/lua_thread.cpp9
4 files changed, 23 insertions, 1 deletions
diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt
index 654e2763..5c67a57e 100644
--- a/src/lua/CMakeLists.txt
+++ b/src/lua/CMakeLists.txt
@@ -5,5 +5,6 @@
idf_component_register(
SRCS "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp" "lua_queue.cpp"
INCLUDE_DIRS "include"
- REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database" "esp_timer" "battery" "esp-idf-lua" "luavgl")
+ REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database"
+ "esp_timer" "battery" "esp-idf-lua" "luavgl" "lua-linenoise" "lua-term")
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/lua/bridge.cpp b/src/lua/bridge.cpp
index 8d7b4fd0..1063cfbf 100644
--- a/src/lua/bridge.cpp
+++ b/src/lua/bridge.cpp
@@ -25,6 +25,11 @@
#include "service_locator.hpp"
#include "ui_events.hpp"
+extern "C" {
+int luaopen_linenoise(lua_State* L);
+int luaopen_term_core(lua_State* L);
+}
+
namespace lua {
[[maybe_unused]] static constexpr char kTag[] = "lua_bridge";
@@ -61,6 +66,12 @@ Bridge::Bridge(system_fsm::ServiceLocator& services, lua_State& s)
luaL_requiref(&s, "legacy_ui", lua_legacy_ui, true);
lua_pop(&s, 1);
+ luaL_requiref(&s, "linenoise", luaopen_linenoise, true);
+ lua_pop(&s, 1);
+
+ luaL_requiref(&s, "term.core", luaopen_term_core, true);
+ lua_pop(&s, 1);
+
RegisterDatabaseModule(&s);
RegisterQueueModule(&s);
}
diff --git a/src/lua/include/lua_thread.hpp b/src/lua/include/lua_thread.hpp
index b10fdadf..c85ccb91 100644
--- a/src/lua/include/lua_thread.hpp
+++ b/src/lua/include/lua_thread.hpp
@@ -28,6 +28,7 @@ class LuaThread {
~LuaThread();
auto RunScript(const std::string& path) -> bool;
+ auto RunString(const std::string& path) -> bool;
auto bridge() -> Bridge& { return *bridge_; }
auto state() -> lua_State* { return state_; }
diff --git a/src/lua/lua_thread.cpp b/src/lua/lua_thread.cpp
index 4704c3e8..dc588144 100644
--- a/src/lua/lua_thread.cpp
+++ b/src/lua/lua_thread.cpp
@@ -114,6 +114,15 @@ auto LuaThread::RunScript(const std::string& path) -> bool {
return true;
}
+auto LuaThread::RunString(const std::string& script) -> bool {
+ int res = luaL_loadstring(state_, script.c_str());
+ if (res != LUA_OK) {
+ return false;
+ }
+ CallProtected(state_, 0, 0);
+ return true;
+}
+
static int msg_handler(lua_State* L) {
if (!lua_isstring(L, 1)) {
return 1;