fix lua assertion on event argument passing

When adding elements to a table, the .add() function does not behave as
expected in various cases, making it really shit and difficult to use.
Instead, we keep our own index and just add one by one. It works, and
it's easy to understand.

This may lead to indices which are nil, i.e. non-fully-sequential
tables, but I can't be asked to worry about it because that shouldn't be
an issue when we use .set() everywhere.
This commit is contained in:
Lion Kortlepel 2024-05-10 14:44:51 +02:00
parent 0c740ccedf
commit c5dff8b913
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B

View File

@ -458,7 +458,8 @@ std::vector<sol::object> TLuaEngine::StateThreadData::JsonStringToArray(JsonStri
sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs) { sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
auto Table = mStateView.create_table(); auto Table = mStateView.create_table();
for (const sol::stack_proxy& Arg : EventArgs) { int i = 1;
for (auto Arg : EventArgs) {
switch (Arg.get_type()) { switch (Arg.get_type()) {
case sol::type::none: case sol::type::none:
case sol::type::userdata: case sol::type::userdata:
@ -466,19 +467,20 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
case sol::type::thread: case sol::type::thread:
case sol::type::function: case sol::type::function:
case sol::type::poly: case sol::type::poly:
Table.add(BEAMMP_INTERNAL_NIL); Table.set(i, BEAMMP_INTERNAL_NIL);
beammp_warnf("Passed a value of type '{}' to TriggerGlobalEvent(\"{}\", ...). This type can not be serialized, and cannot be passed between states. It will arrive as <nil> in handlers.", sol::type_name(EventArgs.lua_state(), Arg.get_type()), EventName); beammp_warnf("Passed a value of type '{}' to TriggerGlobalEvent(\"{}\", ...). This type can not be serialized, and cannot be passed between states. It will arrive as <nil> in handlers.", sol::type_name(EventArgs.lua_state(), Arg.get_type()), EventName);
break; break;
case sol::type::lua_nil: case sol::type::lua_nil:
Table.add(BEAMMP_INTERNAL_NIL); Table.set(i, BEAMMP_INTERNAL_NIL);
break; break;
case sol::type::string: case sol::type::string:
case sol::type::number: case sol::type::number:
case sol::type::boolean: case sol::type::boolean:
case sol::type::table: case sol::type::table:
Table.add(Arg); Table.set(i, Arg);
break; break;
} }
++i;
} }
JsonString Str { LuaAPI::MP::JsonEncode(Table) }; JsonString Str { LuaAPI::MP::JsonEncode(Table) };
beammp_debugf("json: {}", Str.value); beammp_debugf("json: {}", Str.value);
@ -520,11 +522,13 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
sol::state_view StateView(mState); sol::state_view StateView(mState);
sol::table Result = StateView.create_table(); sol::table Result = StateView.create_table();
auto Vector = Self.get<std::vector<std::shared_ptr<TLuaResult>>>("ReturnValueImpl"); auto Vector = Self.get<std::vector<std::shared_ptr<TLuaResult>>>("ReturnValueImpl");
int i = 1;
for (const auto& Value : Vector) { for (const auto& Value : Vector) {
if (!Value->Ready) { if (!Value->Ready) {
return sol::lua_nil; return sol::lua_nil;
} }
Result.add(Value->Result); Result.set(i, Value->Result);
++i;
} }
return Result; return Result;
}); });
@ -534,12 +538,14 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
sol::table TLuaEngine::StateThreadData::Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs) { sol::table TLuaEngine::StateThreadData::Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
// TODO: make asynchronous? // TODO: make asynchronous?
sol::table Result = mStateView.create_table(); sol::table Result = mStateView.create_table();
int i = 1;
for (const auto& Handler : mEngine->GetEventHandlersForState(EventName, mStateId)) { for (const auto& Handler : mEngine->GetEventHandlersForState(EventName, mStateId)) {
auto Fn = mStateView[Handler]; auto Fn = mStateView[Handler];
if (Fn.valid() && Fn.get_type() == sol::type::function) { if (Fn.valid() && Fn.get_type() == sol::type::function) {
auto FnRet = Fn(EventArgs); auto FnRet = Fn(EventArgs);
if (FnRet.valid()) { if (FnRet.valid()) {
Result.add(FnRet); Result.set(i, FnRet);
++i;
} else { } else {
sol::error Err = FnRet; sol::error Err = FnRet;
beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what()); beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what());