From 9eb5ae6e946651bdbe532b66700bb1ed6944584f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 22 Nov 2023 15:46:46 +1100 Subject: Use protected mode for lua callbacks wherever possible --- src/ui/include/ui_events.hpp | 4 ++++ src/ui/include/ui_fsm.hpp | 3 +++ src/ui/ui_fsm.cpp | 7 +++++-- 3 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src/ui') diff --git a/src/ui/include/ui_events.hpp b/src/ui/include/ui_events.hpp index b8dd459c..6a6be304 100644 --- a/src/ui/include/ui_events.hpp +++ b/src/ui/include/ui_events.hpp @@ -24,6 +24,10 @@ struct OnStorageChange : tinyfsm::Event { struct OnSystemError : tinyfsm::Event {}; + struct OnLuaError : tinyfsm::Event { + std::string message; + }; + namespace internal { struct RecordSelected : tinyfsm::Event { diff --git a/src/ui/include/ui_fsm.hpp b/src/ui/include/ui_fsm.hpp index d3ea7eac..9e81259a 100644 --- a/src/ui/include/ui_fsm.hpp +++ b/src/ui/include/ui_fsm.hpp @@ -64,6 +64,7 @@ class UiState : public tinyfsm::Fsm { void react(const audio::QueueUpdate&); virtual void react(const system_fsm::KeyLockChanged&); + virtual void react(const OnLuaError&) {} virtual void react(const internal::RecordSelected&) {} virtual void react(const internal::IndexSelected&) {} @@ -124,6 +125,8 @@ class Lua : public UiState { void entry() override; void exit() override; + void react(const OnLuaError&) override; + void react(const internal::IndexSelected&) override; void react(const internal::ShowNowPlaying&) override; void react(const internal::ShowSettingsPage&) override; diff --git a/src/ui/ui_fsm.cpp b/src/ui/ui_fsm.cpp index ed0624df..5b4ea2a7 100644 --- a/src/ui/ui_fsm.cpp +++ b/src/ui/ui_fsm.cpp @@ -238,8 +238,7 @@ auto Lua::PushLuaScreen(lua_State* s) -> int { // Call the constructor for this screen. lua_settop(s, 1); // Make sure the function is actually at top of stack - // FIXME: This should ideally be lua_pcall, for safety. - lua_call(s, 0, 1); + lua::CallProtected(s, 0, 1); // Store the reference for the table the constructor returned. new_screen->SetObjRef(s); @@ -262,6 +261,10 @@ void Lua::exit() { lv_group_set_default(NULL); } +void Lua::react(const OnLuaError& err) { + ESP_LOGE("lua", "%s", err.message.c_str()); +} + void Lua::react(const internal::IndexSelected& ev) { auto db = sServices->database().lock(); if (!db) { -- cgit v1.2.3