summaryrefslogtreecommitdiff
path: root/src/lua
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2024-01-19 12:52:41 +1100
committerjacqueline <me@jacqueline.id.au>2024-01-19 12:52:41 +1100
commitcd650b30bd3ddfb9f71622278ef5b6f94ae27ac8 (patch)
tree482058e274f0daee21a1e8798cdd39e4b94be17c /src/lua
parent1525afe8aaadfaa877f8987f0ea224263a612b5e (diff)
downloadtangara-fw-cd650b30bd3ddfb9f71622278ef5b6f94ae27ac8.tar.gz
fix some lua stack leaks + add a console func to help debug
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/include/lua_thread.hpp2
-rw-r--r--src/lua/lua_thread.cpp58
-rw-r--r--src/lua/property.cpp4
3 files changed, 64 insertions, 0 deletions
diff --git a/src/lua/include/lua_thread.hpp b/src/lua/include/lua_thread.hpp
index c85ccb91..d10dba3a 100644
--- a/src/lua/include/lua_thread.hpp
+++ b/src/lua/include/lua_thread.hpp
@@ -30,6 +30,8 @@ class LuaThread {
auto RunScript(const std::string& path) -> bool;
auto RunString(const std::string& path) -> bool;
+ auto DumpStack() -> void;
+
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 dc588144..b94b70ab 100644
--- a/src/lua/lua_thread.cpp
+++ b/src/lua/lua_thread.cpp
@@ -6,8 +6,11 @@
#include "lua_thread.hpp"
+#include <iostream>
#include <memory>
+#include "lauxlib.h"
+#include "lua.h"
#include "lua.hpp"
#include "font/lv_font_loader.h"
@@ -123,6 +126,61 @@ auto LuaThread::RunString(const std::string& script) -> bool {
return true;
}
+auto LuaThread::DumpStack() -> void {
+ int top = lua_gettop(state_);
+ std::cout << "stack size: " << top << std::endl;
+ for (size_t i = 1; i <= top; i++) {
+ std::cout << "[" << i << "]\t" << luaL_typename(state_, i);
+ switch (lua_type(state_, i)) {
+ case LUA_TNUMBER:
+ std::cout << "\t(";
+ if (lua_isinteger(state_, i)) {
+ std::cout << lua_tointeger(state_, i);
+ } else {
+ std::cout << lua_tonumber(state_, i);
+ }
+ std::cout << ")";
+ break;
+ case LUA_TSTRING:
+ std::cout << "\t('" << lua_tostring(state_, i) << "')";
+ break;
+ case LUA_TBOOLEAN:
+ std::cout << "\t(" << lua_toboolean(state_, i) << ")";
+ break;
+ case LUA_TNIL:
+ // Value is implied.
+ break;
+ case LUA_TTABLE:
+ lua_pushnil(state_);
+ while (lua_next(state_, i) != 0) {
+ // Keys
+ std::cout << std::endl << "\t\t" << luaL_typename(state_, -2);
+ if (lua_type(state_, -2) == LUA_TSTRING) {
+ std::cout << "\t(" << lua_tostring(state_, -2) << ")";
+ } else if (lua_type(state_, -2) == LUA_TNUMBER) {
+ std::cout << "\t(" << lua_tonumber(state_, -2) << ")";
+ }
+
+ // Values
+ std::cout << "\t\t" << luaL_typename(state_, -1);
+ if (lua_type(state_, -1) == LUA_TSTRING) {
+ std::cout << "\t(" << lua_tostring(state_, -1) << ")";
+ } else if (lua_type(state_, -1) == LUA_TNUMBER) {
+ std::cout << "\t(" << lua_tonumber(state_, -1) << ")";
+ }
+ // Pop the value; we don't care about it. Leave the key on the stack
+ // for the next call to lua_next.
+ lua_pop(state_, 1);
+ }
+ break;
+ default:
+ std::cout << "\t(" << lua_topointer(state_, i) << ")";
+ break;
+ }
+ std::cout << std::endl;
+ }
+}
+
static int msg_handler(lua_State* L) {
if (!lua_isstring(L, 1)) {
return 1;
diff --git a/src/lua/property.cpp b/src/lua/property.cpp
index 33600ee8..5357ccc5 100644
--- a/src/lua/property.cpp
+++ b/src/lua/property.cpp
@@ -113,6 +113,9 @@ PropertyBindings::PropertyBindings(lua_State& s) {
// Add our binding funcs (get, set, bind) to the metatable.
luaL_setfuncs(&s, kPropertyBindingFuncs, 0);
+ // We've finished setting up the metatable, so pop it.
+ lua_pop(&s, 1);
+
// Create a weak table in the registry to hold live bindings.
lua_pushstring(&s, kBindingsTable);
lua_newtable(&s); // bindings = {}
@@ -368,6 +371,7 @@ auto Property::Update(const LuaValue& v) -> void {
PushValue(*b.first); // push the argument
CallProtected(b.first, 1, 0); // invoke the closure
+ lua_pop(b.first, 1); // pop the bindings table
}
}