Fix Lua assert error when adding values to tables (e.g. in event arguments) (#318)

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 2024-05-11 12:18:56 +02:00 committed by GitHub
commit c9d926f9e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 8 deletions

View File

@ -150,7 +150,7 @@ private:
static inline std::mutex mShutdownHandlersMutex {};
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
static inline Version mVersion { 3, 4, 0 };
static inline Version mVersion { 3, 4, 1 };
};
void SplitString(std::string const& str, const char delim, std::vector<std::string>& out);

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) {
auto Table = mStateView.create_table();
for (const sol::stack_proxy& Arg : EventArgs) {
int i = 1;
for (auto Arg : EventArgs) {
switch (Arg.get_type()) {
case sol::type::none:
case sol::type::userdata:
@ -466,19 +467,20 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
case sol::type::thread:
case sol::type::function:
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);
break;
case sol::type::lua_nil:
Table.add(BEAMMP_INTERNAL_NIL);
Table.set(i, BEAMMP_INTERNAL_NIL);
break;
case sol::type::string:
case sol::type::number:
case sol::type::boolean:
case sol::type::table:
Table.add(Arg);
Table.set(i, Arg);
break;
}
++i;
}
JsonString Str { LuaAPI::MP::JsonEncode(Table) };
beammp_debugf("json: {}", Str.value);
@ -520,11 +522,13 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
sol::state_view StateView(mState);
sol::table Result = StateView.create_table();
auto Vector = Self.get<std::vector<std::shared_ptr<TLuaResult>>>("ReturnValueImpl");
int i = 1;
for (const auto& Value : Vector) {
if (!Value->Ready) {
return sol::lua_nil;
}
Result.add(Value->Result);
Result.set(i, Value->Result);
++i;
}
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) {
// TODO: make asynchronous?
sol::table Result = mStateView.create_table();
int i = 1;
for (const auto& Handler : mEngine->GetEventHandlersForState(EventName, mStateId)) {
auto Fn = mStateView[Handler];
if (Fn.valid() && Fn.get_type() == sol::type::function) {
auto FnRet = Fn(EventArgs);
if (FnRet.valid()) {
Result.add(FnRet);
Result.set(i, FnRet);
++i;
} else {
sol::error Err = FnRet;
beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what());

2
vcpkg

@ -1 +1 @@
Subproject commit 326d8b43e365352ba3ccadf388d989082fe0f2a6
Subproject commit 6978381401d33a5ad6a3385895d12e383083712a