66#ifndef NNGN_LUA_STATE_H
67#define NNGN_LUA_STATE_H
95 operator lua_State*(void)
const {
return this->
L; }
109 int top(
void)
const {
return lua_gettop(*
this); }
113 template<
typename T = lua_Integer>
T len(
int i)
const;
115 std::pair<lua_Alloc, void*>
allocator(
void)
const;
130 bool doarg(std::string_view
s)
const;
132 bool dofile(std::string_view filename)
const;
134 bool dostring(std::string_view src)
const;
146 template<
typename T = value_view>
T get(
int i)
const;
148 template<
typename T = value_view>
T push(
auto &&x)
const;
150 void pop(
int n = 1)
const { lua_pop(*
this,
n); }
154 void call(
auto &&
f,
auto &&...args)
const;
156 int pcall(
auto &&h,
auto &&
f,
auto &&...args)
const;
158 std::pair<value, std::string_view>
to_string(
int i)
const;
162 lua_State *
L =
nullptr;
185template<
typename T = value_view>
187 [[maybe_unused]]
const auto i = lua.
top() + 1;
189 if constexpr(!std::is_void_v<T>)
190 return lua.
get<
T>(
i);
194 const auto ret =
static_cast<type>(lua_type(*
this,
i));
198 Log::l() <<
"invalid type at index " <<
i <<
'\n';
212 auto *
const f = lua_getallocf(*
this, &p);
218 lua_setmetatable(*
this,
i);
223 return static_cast<T*
>(
225 lua_newuserdatauv(*
this,
sizeof(
T) +
alignof(
T), nv),
240 [[maybe_unused]]
const int i = this->
top() + 1;
242 lua_pushcfunction(*
this,
msgh);
245 constexpr auto n_args =
static_cast<int>(
sizeof...(args));
247 lua_pcall(*
this, n_args, LUA_MULTRET,
i);
248 lua_remove(*
this,
i);
250 lua_call(*
this, n_args, LUA_MULTRET);
254 const int h_idx = h.index();
258 constexpr auto n_args =
static_cast<int>(
sizeof...(args));
259 return lua_pcall(*
this, n_args, LUA_MULTRET, h_idx);
263 state_view::operator=(std::move(rhs).
release());
static std::ostream & l()
Definition log.cpp:56
static void * align_ptr(void *p, std::size_t a)
Definition math.h:570
Pops n values from the Lua stack at scope exit.
Definition utils.h:24
Table interface to the global environment.
Definition table.h:181
T push(auto &&x) const
Pushes a value onto the stack.
Definition state.h:235
T get(int i) const
Reads a value from the stack.
Definition state.h:230
void pop(int n=1) const
Definition state.h:150
void call(auto &&f, auto &&...args) const
Function call, protected in debug mode.
Definition state.h:239
lua_State * L
Definition state.h:162
bool dostring(std::string_view src) const
Definition state.cpp:83
table new_user_type(void) const
Registers a new user type and returns its meta table.
Definition table.h:356
void set_top(int i) const
Definition state.h:119
global_table globals(void) const
Definition table.h:367
bool dofunction(std::string_view f) const
Executes the global function f.
Definition state.cpp:95
T * new_user_data(int nv=0) const
Definition state.h:222
void destroy(void)
Destroys the associated lua_State.
Definition state.cpp:56
state_view release(void)
Definition state.h:105
bool dofile(std::string_view filename) const
Definition state.cpp:71
int top(void) const
Definition state.h:109
std::pair< value, std::string_view > to_string(int i) const
Pushes a string representation of a value onto the stack.
Definition value.h:94
bool doarg(std::string_view s) const
Calls dofile or dostring depending on s[0] == '@'.
Definition state.cpp:67
std::pair< lua_Alloc, void * > allocator(void) const
Get memory allocator data.
Definition state.h:210
int pcall(auto &&h, auto &&f, auto &&...args) const
Protected function call with error handler.
Definition state.h:253
void print_stack(void) const
Non-static version.
Definition state.h:123
void set_meta_table(auto &&mt, int i)
Definition state.h:216
void print_traceback(void) const
Non-static version.
Definition state.h:125
state_view(void)=default
Empty state, must call init before any other function is called.
bool heartbeat(void) const
Execute the global heartbeat function.
Definition state.cpp:106
T len(int i) const
Definition state.h:204
type get_type(int i) const
Definition state.h:193
table create_table(void) const
Creates a table and pushes it on the stack.
Definition table.h:371
void remove(int i) const
Definition state.h:152
state_view(lua_State *L_)
Wraps an existing luaState.
Definition state.h:93
assert
Definition debug.lua:3
function DEMO text end
Definition demo0.lua:6
for i
Definition font.lua:5
local n
Definition dump_lights.lua:5
init
Definition img_common.lua:34
#define NNGN_LOG_CONTEXT_CF(c)
Definition log.h:11
nngn::lua::table_view alloc_info(nngn::lua::state_view lua)
Definition lua_state.cpp:10
Definition lua_audio.cpp:19
void print_stack(lua_State *L)
Logs the current data stack.
Definition lua.cpp:26
int msgh(lua_State *L)
Default message handler for lua_pcall.
Definition lua.cpp:15
T push(nngn::lua::state_view lua, auto &&v)
Definition state.h:186
type
LUA_T* constants as a scoped enumeration.
Definition lua.h:77
T get(nngn::lua::state_view L, int i)
Definition state.h:181
void print_traceback(lua_State *L)
Logs the current call stack.
Definition lua.cpp:59
constexpr const R * begin(const T< R > &v)
Definition vec.h:207
get
Definition camera.lua:46
v[1]
Definition math.lua:22
General utilities for stack manipulation.
#define NNGN_NO_COPY(x)
Definition utils.h:35
#define FWD(...)
Definition utils.h:18
#define NNGN_ANON_DECL(...)
Definition utils.h:53
local mt
Definition strict.lua:4
Tracks state allocations.
Definition alloc.h:29
static state_view get(lua_State *L, int)
Definition state.h:269
Reads a value from the Lua stack.
Definition lua.h:118
Pushes a value onto the Lua stack.
Definition lua.h:120
Owning lua_State wrapper.
Definition state.h:166
state & operator=(const state &)=delete
state(state &&rhs) noexcept
Transfers the lua_State from another object.
Definition state.h:173
~state(void)
Destroys the associated lua_State.
Definition state.h:177
Owning reference to a table on the stack, popped when destroyed.
Definition table.h:172
std::chrono::seconds s
Definition timing.cpp:6