From effac1917a615660bf76b35b3605ac2d3eeabd2f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 20 Nov 2023 14:43:20 +1100 Subject: Use C functions for the backstack, instead of a lua module Working with the default group and root kinda sucks if you have to do it from lua! --- src/lua/property.cpp | 12 +++++++----- src/lua/stubs/backstack.lua | 13 +++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 src/lua/stubs/backstack.lua (limited to 'src/lua') diff --git a/src/lua/property.cpp b/src/lua/property.cpp index 353e01ae..f0383dd8 100644 --- a/src/lua/property.cpp +++ b/src/lua/property.cpp @@ -5,13 +5,10 @@ */ #include "property.hpp" -#include #include #include -#include "lauxlib.h" -#include "lua.h" #include "lua.hpp" #include "lvgl.h" #include "service_locator.hpp" @@ -19,7 +16,7 @@ namespace lua { static const char kPropertyMetatable[] = "property"; -static const char kFunctionMetatable[] = "function"; +static const char kFunctionMetatable[] = "c_func"; static const char kBindingsTable[] = "bindings"; static const char kBinderKey[] = "binder"; @@ -63,7 +60,7 @@ static auto property_bind(lua_State* state) -> int { p->AddLuaBinding(state, ref); - // Pop the bindings table, leaving one of the copiesw of the callback fn at + // Pop the bindings table, leaving one of the copies of the callback fn at // the top of the stack. lua_pop(state, 1); @@ -84,6 +81,11 @@ static auto generic_function_cb(lua_State* state) -> int { size_t* index = reinterpret_cast(luaL_checkudata(state, 1, kFunctionMetatable)); const LuaFunction& fn = binder->GetFunction(*index); + + // Ensure the C++ function is called with a clean stack; we don't want it to + // see the index we just used. + lua_remove(state, 1); + return std::invoke(fn, state); } diff --git a/src/lua/stubs/backstack.lua b/src/lua/stubs/backstack.lua new file mode 100644 index 00000000..d4807d37 --- /dev/null +++ b/src/lua/stubs/backstack.lua @@ -0,0 +1,13 @@ +--- Module for adding and removing screens from the system's backstack. +-- @module backstack + +local backstack = {} + +--- Pushes a new screen onto the backstack. +-- @tparam function constructor Called to create the UI for the new screen. A new default root object and group will be set before calling this function. The function provided should return a table holding any bindings used by this screen; the returned value is retained so long as this screen is present in the backstack. +function backstack.push(constructor) end + +--- Removes the currently active screen, and instead shows the screen underneath it on the backstack. Does nothing if this is the only existing screen. +function backstack.pop() end + +return backstack -- cgit v1.2.3