Skip to content

Commit

Permalink
[lua] add lua::state::define_xyz. also expose version info, not sure …
Browse files Browse the repository at this point in the history
…how useful this is yet, probably needs to be re-done
  • Loading branch information
harrand committed Aug 5, 2023
1 parent e92c74e commit 663dd4a
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/tz/lua/state.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "tz/lua/state.hpp"
#include "tz/core/debug.hpp"
#include "tz/core/data/version.hpp"
#include <cstdint>

extern "C"
Expand Down Expand Up @@ -35,6 +36,68 @@ namespace tz::lua
return ret;
}

bool state::define_nil(const char* varname) const
{
bool ret = impl_check_stack(1);
auto* s = static_cast<lua_State*>(this->lstate);
lua_pushnil(s);
lua_setglobal(s, varname);
return ret;
}

bool state::define_bool(const char* varname, bool b) const
{
bool ret = impl_check_stack(1);
auto* s = static_cast<lua_State*>(this->lstate);
lua_pushboolean(s, b);
lua_setglobal(s, varname);
return ret;
}

bool state::define_float(const char* varname, float f) const
{
return this->define_double(varname, f);
}

bool state::define_double(const char* varname, double d) const
{
bool ret = impl_check_stack(1);
auto* s = static_cast<lua_State*>(this->lstate);
lua_pushnumber(s, d);
lua_setglobal(s, varname);
return ret;
}

bool state::define_int(const char* varname, std::int64_t i) const
{
bool ret = impl_check_stack(1);
auto* s = static_cast<lua_State*>(this->lstate);
lua_pushinteger(s, i);
lua_setglobal(s, varname);
return ret;
}

bool state::define_uint(const char* varname, std::uint64_t u) const
{
// ugh...
return this->define_int(varname, static_cast<std::int64_t>(u));
}

bool state::define_func(const char* varname, void* func_ptr) const
{
bool ret = impl_check_stack(1);
auto* s = static_cast<lua_State*>(this->lstate);
lua_pushcfunction(s, reinterpret_cast<lua_CFunction>(func_ptr));
lua_setglobal(s, varname);
return ret;
}

bool state::impl_check_stack(std::size_t sz) const
{
auto* s = static_cast<lua_State*>(this->lstate);
return lua_checkstack(s, sz);
}

state defstate = {};

void tz_inject_state(lua_State* state);
Expand Down Expand Up @@ -65,11 +128,34 @@ namespace tz::lua
{
tz::assert(luaL_dostring(state, R"lua(
tz = {}
tz.version = {}
)lua") == false, "Failed to inject topaz lua state: %s", lua_tostring(state, -1));
lua_getglobal(state, "tz");
// tz.assert
lua_pushcfunction(state, tz_lua_assert);
lua_setfield(state, -2, "assert");

tz::version ver = tz::get_version();

// tz.version = {.major = X, .minor = Y, .patch = Z, .string = "vX.Y.Z-something"}
lua_newtable(state);
lua_pushstring(state, "major");
lua_pushinteger(state, ver.major);
lua_settable(state, -3);

lua_pushstring(state, "minor");
lua_pushinteger(state, ver.minor);
lua_settable(state, -3);

lua_pushstring(state, "patch");
lua_pushinteger(state, ver.patch);
lua_settable(state, -3);

lua_pushstring(state, "string");
lua_pushstring(state, ver.to_string().c_str());
lua_settable(state, -3);

lua_setfield(state, -2, "version");
lua_pop(state, 1);
}
}
11 changes: 11 additions & 0 deletions src/tz/lua/state.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef TZ_LUA_STATE_HPP
#define TZ_LUA_STATE_HPP
#include "tz/core/data/handle.hpp"
#include <string>
#include <cstdint>

namespace tz::lua
{
Expand Down Expand Up @@ -32,7 +34,16 @@ namespace tz::lua
* @return Whether the executed code experienced any errors.
*/
bool execute(const char* lua_src, bool assert_on_failure = true) const;

bool define_nil(const char* varname) const;
bool define_bool(const char* varname, bool b) const;
bool define_float(const char* varname, float f) const;
bool define_double(const char* varname, double d) const;
bool define_int(const char* varname, std::int64_t i) const;
bool define_uint(const char* varname, std::uint64_t u) const;
bool define_func(const char* varname, void* func_ptr) const;
private:
bool impl_check_stack(std::size_t sz) const;
void* lstate = nullptr;
};

Expand Down

0 comments on commit 663dd4a

Please sign in to comment.