Make LuaStack::Load return special codes for 'slash command', 'white space', and 'truncated lua'

This commit is contained in:
2025-12-18 14:45:57 -05:00
parent f75ff36c3d
commit 63aa5b4c15
4 changed files with 36 additions and 13 deletions

View File

@@ -205,25 +205,21 @@ FString UlxLuaCallLibrary::AllFunctionsWithPrefix(const TCHAR *Prefix)
void UlxLuaCallLibrary::ValidateLuaExpr(
ElxLuaSyntaxCheck &Status, FString &ErrorMessage, UObject *context, const FString &Code)
{
if (Code.StartsWith(TEXT("/")))
{
ErrorMessage = "SlashCommand";
Status = ElxLuaSyntaxCheck::SlashCommand;
return;
}
if (Code.TrimStart().IsEmpty())
{
ErrorMessage = "";
Status = ElxLuaSyntaxCheck::Whitespace;
return;
}
ALuprexGameModeBase *mode = ALuprexGameModeBase::FromContext(context);
ErrorMessage = mode->ValidateLuaExpr(Code);
if (ErrorMessage.IsEmpty())
{
Status = ElxLuaSyntaxCheck::ValidLua;
}
else if (ErrorMessage.Contains(TEXT("<eof>")))
else if (ErrorMessage == TEXT("slash command"))
{
Status = ElxLuaSyntaxCheck::SlashCommand;
}
else if (ErrorMessage == TEXT("white space"))
{
Status = ElxLuaSyntaxCheck::Whitespace;
}
else if (ErrorMessage == TEXT("truncated lua"))
{
Status = ElxLuaSyntaxCheck::TruncatedLua;
}

View File

@@ -333,6 +333,17 @@ eng::string LuaCoreStack::load(LuaSlot result, std::string_view code, std::strin
code = expanded;
}
if (sv::has_prefix(code, "/"))
{
set(result, "slash command");
return "slash command";
}
if (sv::is_whitespace(code))
{
set(result, "white space");
return "white space";
}
eng::string fullcontext = eng::string("=") + eng::string(context);
luaL_loadbuffer(L_, code.data(), code.size(), fullcontext.c_str());
int type = lua_type(L_, -1);
@@ -346,6 +357,10 @@ eng::string LuaCoreStack::load(LuaSlot result, std::string_view code, std::strin
const char *str = lua_tolstring(L_, -1, &len);
eng::string message(str, len);
lua_pop(L_, 1);
if (sv::has_suffix(message, "near <eof>"))
{
message = "truncated lua";
}
if (message.empty()) message = "unknown compiler error";
set(result, message);
return message;

View File

@@ -199,6 +199,15 @@ bool is_lua_comment(string_view s) {
return s.substr(start, 2) == "--";
}
bool is_whitespace(string_view s) {
for (int i = 0; i < int(s.size()); i++) {
if (!ascii_isspace(s[i])) {
return false;
}
}
return true;
}
string_view read_to_sep(string_view &source, char sep) {
size_t pos = source.find(sep);
string_view result;

View File

@@ -110,6 +110,9 @@ bool is_lua_classname(string_view s);
// Return true if the line of code is a lua comment.
bool is_lua_comment(string_view s);
// Return true if the line is entirely whitespace.
bool is_whitespace(string_view s);
// Return the first character, but if the view is empty,
// return zero.
inline char zfront(string_view &s) {