Topaz 5.0
Topaz Game Engine
Loading...
Searching...
No Matches
lua.hpp
1#ifndef TOPAZ_LUA_HPP
2#define TOPAZ_LUA_HPP
3#include "tz/topaz.hpp"
4#include "tz/core/error.hpp"
5#include <filesystem>
6#include <expected>
7
8namespace tz
9{
43 using lua_fn = int(*)();
44 struct lua_nil{};
53 tz::error_code lua_execute_file(std::filesystem::path path);
61 tz::error_code lua_execute(std::string_view lua_src);
68 tz::error_code lua_set_nil(std::string_view varname);
75 tz::error_code lua_set_emptytable(std::string_view varname);
83 tz::error_code lua_set_bool(std::string_view varname, bool v);
91 tz::error_code lua_set_int(std::string_view varname, std::int64_t v);
99 tz::error_code lua_set_number(std::string_view varname, double v);
107 tz::error_code lua_set_string(std::string_view varname, std::string str);
115 tz::error_code lua_define_function(std::string_view varname, lua_fn fn);
116
123 std::expected<bool, tz::error_code> lua_get_bool(std::string_view varname);
130 std::expected<std::int64_t, tz::error_code> lua_get_int(std::string_view varname);
137 std::expected<double, tz::error_code> lua_get_number(std::string_view varname);
144 std::expected<std::string, tz::error_code> lua_get_string(std::string_view varname);
145
152 std::expected<bool, tz::error_code> lua_stack_get_bool(std::size_t id);
159 std::expected<std::int64_t, tz::error_code> lua_stack_get_int(std::size_t id);
166 std::expected<double, tz::error_code> lua_stack_get_number(std::size_t id);
173 std::expected<std::string, tz::error_code> lua_stack_get_string(std::size_t id);
174
184 void lua_push_bool(bool v);
189 void lua_push_int(std::int64_t v);
194 void lua_push_number(double v);
199 void lua_push_string(std::string v);
200
205 std::size_t lua_stack_size();
206
213 std::string lua_debug_callstack();
220 std::string lua_debug_stack();
221
222 template<int F, int L>
223 struct static_for_t
224 {
225 template<typename Functor>
226 static inline constexpr void apply(const Functor& f)
227 {
228 if(F < L)
229 {
230 f(std::integral_constant<int, F>{});
231 static_for_t<F + 1, L>::apply(f);
232 }
233 }
234
235 template<typename Functor>
236 inline constexpr void operator()(const Functor& f) const
237 {
238 apply(f);
239 }
240 };
241
242 template<int N>
243 struct static_for_t<N, N>
244 {
245 template<typename Functor>
246 static inline constexpr void apply([[maybe_unused]] const Functor& f){}
247 };
248
249 template<int F, int L>
250 inline constexpr static_for_t<F, L> static_for = {};
251
258 template<typename... Ts>
259 std::tuple<Ts...> lua_parse_args()
260 {
261 std::tuple<Ts...> ret;
262 static_for<0, sizeof...(Ts)>([&ret]([[maybe_unused]] auto i) constexpr
263 {
264 using T = std::decay_t<decltype(std::get<i.value>(std::declval<std::tuple<Ts...>>()))>;
265 auto& v = std::get<i.value>(ret);
266 if constexpr(std::is_same_v<T, bool>)
267 {
268 v = tz_must(lua_stack_get_bool(i.value + 1));
269 }
270 else if constexpr(std::is_same_v<T, float> || std::is_same_v<T, double>)
271 {
272 v = tz_must(lua_stack_get_number(i.value + 1));
273 }
274 else if constexpr(std::is_same_v<T, std::int64_t> || std::is_same_v<T, int>)
275 {
276 v = tz_must(lua_stack_get_int(i.value + 1));
277 }
278 else if constexpr(std::is_same_v<T, std::string>)
279 {
280 v = tz_must(lua_stack_get_string(i.value + 1));
281 }
282 else if constexpr(std::is_same_v<T, lua_nil>)
283 {
284 // do nothing! its whatever
285 }
286 else
287 {
288 static_assert(std::is_void_v<T>, "Unrecognised lua argument type. Is it a supported type?");
289 }
290 });
291 return ret;
292 }
293}
294
295#endif // TOPAZ_LUA_HPP
std::expected< std::string, tz::error_code > lua_stack_get_string(std::size_t id)
Retrieve a string from the stack at the given index.
int(*)() lua_fn
Represents the signature for a function that can be called from lua.
Definition lua.hpp:43
void lua_push_number(double v)
Push a number value onto the stack.
void lua_push_nil()
Push a nil value onto the stack.
std::string lua_debug_stack()
Retreieve a string describing the entire lua stack right now.
std::expected< bool, tz::error_code > lua_stack_get_bool(std::size_t id)
Retrieve a bool from the stack at the given index.
std::expected< std::string, tz::error_code > lua_get_string(std::string_view varname)
Retrieve the value of a string variable.
std::expected< std::int64_t, tz::error_code > lua_stack_get_int(std::size_t id)
Retrieve a int from the stack at the given index.
tz::error_code lua_execute_file(std::filesystem::path path)
Attempt to execute a local lua file on the current thread.
void lua_push_string(std::string v)
Push a string value onto the stack.
tz::error_code lua_set_emptytable(std::string_view varname)
Set a variable in lua to be the empty table "{}".
void lua_push_bool(bool v)
Push a bool value onto the stack.
tz::error_code lua_define_function(std::string_view varname, lua_fn fn)
Define a new function in lua.
tz::error_code lua_set_number(std::string_view varname, double v)
Set a variable in lua to a new number value.
std::expected< double, tz::error_code > lua_stack_get_number(std::size_t id)
Retrieve a number from the stack at the given index.
std::expected< bool, tz::error_code > lua_get_bool(std::string_view varname)
Retrieve the value of a bool variable.
tz::error_code lua_set_string(std::string_view varname, std::string str)
Set a variable in lua to a new string value.
std::expected< double, tz::error_code > lua_get_number(std::string_view varname)
Retrieve the value of a number variable.
std::size_t lua_stack_size()
Retrieve the number of values on the stack currently.
std::tuple< Ts... > lua_parse_args()
Retreve a set of arguments from the stack.
Definition lua.hpp:259
tz::error_code lua_execute(std::string_view lua_src)
Attempt to execute some lua code on the current thread.
std::expected< std::int64_t, tz::error_code > lua_get_int(std::string_view varname)
Retrieve the value of a int variable.
tz::error_code lua_set_bool(std::string_view varname, bool v)
Set a variable in lua to a new bool value.
tz::error_code lua_set_int(std::string_view varname, std::int64_t v)
Set a variable in lua to a new int value.
std::string lua_debug_callstack()
Retreieve a string describing the lua callstack right now.
tz::error_code lua_set_nil(std::string_view varname)
Set a variable in lua to be nil.
void lua_push_int(std::int64_t v)
Push an int value onto the stack.
error_code
Error codes for Topaz.
Definition error.hpp:14
#define tz_must(fnret)
Cause a runtime error if the expected value is erroneous. If not, the unwrapped expected value is ret...
Definition debug.hpp:66