summaryrefslogtreecommitdiff
path: root/src/lua/include
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-02-29 12:10:44 +1100
committerjacqueline <me@jacqueline.id.au>2024-02-29 12:10:44 +1100
commit7d3ddac0eaea207aee187729e3beec95d8d201dc (patch)
tree4b4545e725697663a4768630c48f49e8bbb8cf59 /src/lua/include
parentd41f9f703375171d5766840c9edec32ff47bb25d (diff)
parent9fca08f8434a05e1fe93a1c4f8133f0e7fc118bf (diff)
downloadtangara-fw-7d3ddac0eaea207aee187729e3beec95d8d201dc.tar.gz
Merge branch 'main' into seek-support
Diffstat (limited to 'src/lua/include')
-rw-r--r--src/lua/include/bridge.hpp27
-rw-r--r--src/lua/include/lua_registry.hpp51
-rw-r--r--src/lua/include/lua_thread.hpp12
-rw-r--r--src/lua/include/property.hpp4
4 files changed, 79 insertions, 15 deletions
diff --git a/src/lua/include/bridge.hpp b/src/lua/include/bridge.hpp
index 62fbc340..64f14e0e 100644
--- a/src/lua/include/bridge.hpp
+++ b/src/lua/include/bridge.hpp
@@ -16,25 +16,38 @@
namespace lua {
+/*
+ * Responsible for adding C/C++ module bindings to Lua threads. This class
+ * keeps no thread-specific internal state, and instead uses the LUA_REGISTRY
+ * table of its host threads to store data.
+ */
class Bridge {
public:
+ /*
+ * Utility for retrieving the Bridge from a Lua thread in which the Bridge's
+ * bindings have been installed. Use by Lua's C callbacks to access the rest
+ * of the system.
+ */
static auto Get(lua_State* state) -> Bridge*;
- Bridge(system_fsm::ServiceLocator&, lua_State& s);
+ Bridge(system_fsm::ServiceLocator& s);
- auto AddPropertyModule(
+ system_fsm::ServiceLocator& services() { return services_; }
+
+ auto installBaseModules(lua_State* L) -> void;
+ auto installLvgl(lua_State* L) -> void;
+ auto installPropertyModule(
+ lua_State* L,
const std::string&,
std::vector<
- std::pair<std::string,
- std::variant<LuaFunction, Property*>>>)
+ std::pair<std::string, std::variant<LuaFunction, Property*>>>&)
-> void;
- system_fsm::ServiceLocator& services() { return services_; }
- PropertyBindings& bindings() { return bindings_; }
+ Bridge(const Bridge&) = delete;
+ Bridge& operator=(const Bridge&) = delete;
private:
system_fsm::ServiceLocator& services_;
- lua_State& state_;
PropertyBindings bindings_;
};
diff --git a/src/lua/include/lua_registry.hpp b/src/lua/include/lua_registry.hpp
new file mode 100644
index 00000000..abc5063e
--- /dev/null
+++ b/src/lua/include/lua_registry.hpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include "lua.hpp"
+
+#include "bridge.hpp"
+#include "lua_thread.hpp"
+#include "service_locator.hpp"
+
+namespace lua {
+
+class Registry {
+ public:
+ static auto instance(system_fsm::ServiceLocator&) -> Registry&;
+
+ auto uiThread() -> std::shared_ptr<LuaThread>;
+ auto newThread() -> std::shared_ptr<LuaThread>;
+
+ auto AddPropertyModule(
+ const std::string&,
+ std::vector<std::pair<std::string, std::variant<LuaFunction, Property*>>>)
+ -> void;
+
+ Registry(const Registry&) = delete;
+ Registry& operator=(const Registry&) = delete;
+
+ private:
+ Registry(system_fsm::ServiceLocator&);
+
+ system_fsm::ServiceLocator& services_;
+ std::unique_ptr<Bridge> bridge_;
+
+ std::shared_ptr<LuaThread> ui_thread_;
+ std::list<std::weak_ptr<LuaThread>> threads_;
+
+ std::vector<
+ std::pair<std::string,
+ std::vector<std::pair<std::string,
+ std::variant<LuaFunction, Property*>>>>>
+ modules_;
+};
+
+} // namespace lua
diff --git a/src/lua/include/lua_thread.hpp b/src/lua/include/lua_thread.hpp
index d10dba3a..384de61d 100644
--- a/src/lua/include/lua_thread.hpp
+++ b/src/lua/include/lua_thread.hpp
@@ -10,9 +10,7 @@
#include <string>
#include "lua.hpp"
-#include "lvgl.h"
-#include "bridge.hpp"
#include "service_locator.hpp"
namespace lua {
@@ -23,8 +21,7 @@ auto CallProtected(lua_State*, int nargs, int nresults) -> int;
class LuaThread {
public:
- static auto Start(system_fsm::ServiceLocator&, lv_obj_t* lvgl_root = nullptr)
- -> LuaThread*;
+ static auto Start(system_fsm::ServiceLocator&) -> LuaThread*;
~LuaThread();
auto RunScript(const std::string& path) -> bool;
@@ -32,14 +29,15 @@ class LuaThread {
auto DumpStack() -> void;
- auto bridge() -> Bridge& { return *bridge_; }
auto state() -> lua_State* { return state_; }
+ LuaThread(const LuaThread&) = delete;
+ LuaThread& operator=(const LuaThread&) = delete;
+
private:
- LuaThread(std::unique_ptr<Allocator>&, std::unique_ptr<Bridge>&, lua_State*);
+ LuaThread(std::unique_ptr<Allocator>&, lua_State*);
std::unique_ptr<Allocator> alloc_;
- std::unique_ptr<Bridge> bridge_;
lua_State* state_;
};
diff --git a/src/lua/include/property.hpp b/src/lua/include/property.hpp
index 03229bc1..7d160fba 100644
--- a/src/lua/include/property.hpp
+++ b/src/lua/include/property.hpp
@@ -53,7 +53,9 @@ class Property {
class PropertyBindings {
public:
- PropertyBindings(lua_State&);
+ PropertyBindings();
+
+ auto install(lua_State*) -> void;
auto Register(lua_State*, Property*) -> void;
auto Register(lua_State*, LuaFunction) -> void;