Thread And Algorithms
March 13, 2026 ยท View on GitHub
This page pulls together the higher-level usage notes that used to be spread
across thread.hpp and algorithm.hpp.
lua::thread
lua::thread is a lightweight C++ wrapper around lua_State*.
Its main jobs are:
- provide convenient global access through
env["name"] - optionally own the underlying Lua state
- convert back to
lua_State*when you need to call raw Lua APIs
Typical usage:
int luaopen_Foo(lua_State* const state)
{
lua::thread env(state);
env["Foo"] = lua::value::table;
return 0;
}
lua::create() constructs a new state with standard libraries and returns a
lua::thread that owns it by default.
Common Algorithms
algorithm.hpp contains the convenience helpers that tie the rest of the
library together.
Important ones include:
lua::dump(state)for stack diagnosticslua::memory_address(index)for readable identity/debug outputlua::assert_typefor better type errorslua::top,lua::size,lua::clear,lua::remove, andlua::swaplua::storeandlua::getfor generic conversion throughStore<T>andGet<T>lua::make<T>for userdata constructionlua::get_allfor collecting stack values into astd::vector
Calling Lua From C++
Luacxx provides two main helpers here:
lua::invoke(...)lua::call<RV>(...)
invoke executes a callable already present on the Lua stack. call is the
higher-level helper that:
- pushes the callable and arguments
- invokes it
- converts the result back to C++
- restores the stack shape for you
These helpers are meant to coexist with raw Lua API calls, not replace them in every situation.
Constructing Userdata
lua::make<T>(state, args...) constructs a userdata value of type T directly
on the Lua stack. That is the usual Luacxx path for exposing C++ objects to
Lua while preserving native object layout and destruction semantics.