From 64b106c13e18c33be0f2b0de532054e0ed3f731d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 13 Dec 2023 16:10:08 +1100 Subject: add a cool lua repl --- src/lua/CMakeLists.txt | 3 ++- src/lua/bridge.cpp | 11 +++++++++++ src/lua/include/lua_thread.hpp | 1 + src/lua/lua_thread.cpp | 9 +++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) (limited to 'src/lua') 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; -- cgit v1.2.3