16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-21 17:09:19 +02:00

Refactor Lua bindings to not use FFI anymore

- Lua bindings are no longer using FFI in favor of the "classic" Lua C API.
  - This makes it work properly on native Linux builds.
- Changed the Lua API slightly:
  - Removed the `api.event_preparefunc()` function because it is no longer necessary. Instead, you can pass any function you want without preparing it first.
  - Added an `api.event_exists()` function. Takes a single `name` argument.
  - Any `api.find_*()` functions return `nil` instead of an userdata representing a null pointer when the requested object is not found.
This commit is contained in:
jakubg1
2026-04-01 01:42:54 +02:00
parent 84f86e725c
commit ca8e4aae71
5 changed files with 370 additions and 294 deletions

View File

@@ -2110,44 +2110,44 @@ friction_event::export_as_text_( std::ostream &Output ) const {
}
#ifdef WITH_LUA
lua_event::lua_event(lua::eventhandler_t func) {
lua_func = func;
lua_event::lua_event(lua_State *L, const int ref) {
lua_state = L;
lua_func = ref;
}
lua_event::~lua_event() {
lua::unref(lua_state, lua_func);
}
// prepares event for use
void
lua_event::init() {
// nothing to do here
void lua_event::init() {
// nothing to do here
}
// event type string
std::string
lua_event::type() const {
return "lua";
std::string lua_event::type() const {
return "lua";
}
// deserialize() subclass details
void
lua_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
// preload next token
Input.getTokens();
void lua_event::deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) {
// preload next token
Input.getTokens();
}
// run() subclass details
void
lua_event::run_() {
void lua_event::run_() {
try {
if (lua_func)
lua_func(this, m_activator);
if (lua_func != LUA_NOREF)
lua::dispatch_event(lua_state, lua_func, this, m_activator);
} catch (...) {
ErrorLog(simulation::Lua.get_error());
}
}
// export_as_text() subclass details
void
lua_event::export_as_text_( std::ostream &Output ) const {
// nothing to do here
void lua_event::export_as_text_( std::ostream &Output ) const {
// nothing to do here
}
bool lua_event::is_instant() const {

View File

@@ -591,17 +591,19 @@ private:
#ifdef WITH_LUA
class lua_event : public basic_event {
public:
lua_event(lua::eventhandler_t func);
void init() override;
lua_event(lua_State *L, int ref);
~lua_event() override;
void init() override;
private:
std::string type() const override;
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
void run_() override;
void export_as_text_( std::ostream &Output ) const override;
bool is_instant() const override;
std::string type() const override;
void deserialize_( cParser &Input, scene::scratch_data &Scratchpad ) override;
void run_() override;
void export_as_text_( std::ostream &Output ) const override;
bool is_instant() const override;
lua::eventhandler_t lua_func = nullptr;
lua_State *lua_state = nullptr;
int lua_func = LUA_NOREF;
};
#endif