[Refactor] Streamline error message creation#4593
Conversation
|
Hey there! Thanks for helping Mudlet improve. 🌟 Test versionsYou can directly test the changes here:
No need to install anything - just unzip and run. |
|
May need to refactor into macro, because the function should return the object, but may also return int upon failure. |
src/TLuaInterpreter.cpp
Outdated
| // The "isOptional" parameter is optional, and will default to not-optional parameters! :) | ||
| // | ||
| // See also: verifyString, verifyInt, verifyFloat, announceWrongArgumentType | ||
| bool TLuaInterpreter::verifyBoolean(lua_State* L, const int pos, const QString& functionName, const QString& publicName, const bool isOptional) |
There was a problem hiding this comment.
As the strings are squirted straight into a lua_pushfstring(...) you do not need to dress them up as QStrings - const char* is perfectly fine in this particular circumstances...
There was a problem hiding this comment.
That might help with all those errors further down! 😜
There was a problem hiding this comment.
Which errors are you referring? Is this wrong or merely inconvenient?
I for one know QString better than char* so I used what I knew already.
There was a problem hiding this comment.
cannot pass object of non-trivial type 'const QString' through variadic function; call will abort at runtime [-Wnon-pod-varargs]
You cannot use a QString as a parameter to go into a %s in a lua_pushfstring(...) call, you need to convert it back to a const char* with a QString::toUft8() followed by a QByteArray::constData() call - so you might just as well avoid a round trip from const char* ==> QString ==> const char* ...
src/TLuaInterpreter.cpp
Outdated
| { | ||
| if (!lua_isstring(L, pos)) { | ||
| announceWrongArgumentType(L, pos, functionName, publicName, "string", isOptional); | ||
| return lua_error(L); |
There was a problem hiding this comment.
Guess you just have to use:
lua_error(L);
Q_UNREACHABLE();
return {};here.
There was a problem hiding this comment.
Thanks for the suggestion!
vadi2
left a comment
There was a problem hiding this comment.
Yep, it looks good! I'd rename the verify suffix to something else, since the primary purpose is not to verify, but to retrieve the data value. Verification is the secondary purpose.
Will you be removing the old code / doing up the rest of the function?
|
I actually found verify a short and exact description of what happened, but am open to suggestions. Maybe getVerifiedString instead? I will complete createMapLabel and remove comments as part of this PR. The other functions are part of the linked issue instead. |
|
To me verify just means that, verify. I was confused as to where the actual data retrieval comes in then. |
vadi2
left a comment
There was a problem hiding this comment.
With this improvement the API would be like "static argument, dynamic argument, static argument, dynamic argument" - making it a bit of a pain to copy/paste around - might be worthwhile to change it to "static argument, static argument, dynamic argument, dynamic argument"
|
#4593 (comment) yeah, great improvement for sure! |
I actually had it like that before: You argument holds merit, too. Not sure which is worse.. 🤔 edit: Let's try it out again! |
Co-authored-by: Vadim Peretokin <vperetokin@gmail.com>
|
Very nice solution to something we've been stuck on for a long while! |
Co-authored-by: Vadim Peretokin <vperetokin@gmail.com>


Brief overview of PR changes/additions
Use dedicated functions to verify lua argument types given by users
Motivation for adding to Mudlet
Less repetition, more standardisation, easier readability and writability
Other info (issues closed, discussion etc)
Helpful tool to tackle a probably large part of #2091
Current draft as proof of concept can be tested with createMapLabel()
https://wiki.mudlet.org/w/Manual:Lua_Functions#createMapLabel
edit: All arguments of this function's have been ported to new style messages.
The following arguments have been ported to new style messages for testing of many possible style combinations:areaID - integertext - stringposX - floatzoom - optional floatshowOnTop - optional booleanTry giving wrong argument types to any of these
(and the non-listed unchanged ones)and see what happens.