From e035705239059245720fe784cb618fa8d5d2b56b Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 6 May 2023 00:04:10 +0200 Subject: [PATCH] Codechange: introduce and use std::string variant of sq_throwerror --- src/3rdparty/squirrel/include/squirrel.h | 3 ++- src/3rdparty/squirrel/squirrel/sqapi.cpp | 4 ++-- src/script/api/script_controller.cpp | 12 +++--------- src/script/script_info.cpp | 21 ++++++--------------- src/script/script_instance.cpp | 8 ++++---- src/script/squirrel.hpp | 2 +- 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/3rdparty/squirrel/include/squirrel.h b/src/3rdparty/squirrel/include/squirrel.h index 7dfdaed9ad..d5ec13c119 100644 --- a/src/3rdparty/squirrel/include/squirrel.h +++ b/src/3rdparty/squirrel/include/squirrel.h @@ -307,7 +307,8 @@ SQRESULT sq_call(HSQUIRRELVM v,SQInteger params,SQBool retval,SQBool raiseerror, SQRESULT sq_resume(HSQUIRRELVM v,SQBool retval,SQBool raiseerror); const SQChar *sq_getlocal(HSQUIRRELVM v,SQUnsignedInteger level,SQUnsignedInteger idx); const SQChar *sq_getfreevariable(HSQUIRRELVM v,SQInteger idx,SQUnsignedInteger nval); -SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err); +SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err, SQInteger len = -1); +static inline SQRESULT sq_throwerror(HSQUIRRELVM v, const std::string_view err) { return sq_throwerror(v, err.data(), err.size()); } void sq_reseterror(HSQUIRRELVM v); void sq_getlasterror(HSQUIRRELVM v); diff --git a/src/3rdparty/squirrel/squirrel/sqapi.cpp b/src/3rdparty/squirrel/squirrel/sqapi.cpp index 0be573bcc4..d5fd9dd38b 100644 --- a/src/3rdparty/squirrel/squirrel/sqapi.cpp +++ b/src/3rdparty/squirrel/squirrel/sqapi.cpp @@ -933,9 +933,9 @@ void sq_resetobject(HSQOBJECT *po) po->_unVal.pUserPointer=nullptr;po->_type=OT_NULL; } -SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err) +SQRESULT sq_throwerror(HSQUIRRELVM v,const SQChar *err, SQInteger len) { - v->_lasterror=SQString::Create(_ss(v),err); + v->_lasterror=SQString::Create(_ss(v),err, len); return -1; } diff --git a/src/script/api/script_controller.cpp b/src/script/api/script_controller.cpp index b226362376..b083fccddb 100644 --- a/src/script/api/script_controller.cpp +++ b/src/script/api/script_controller.cpp @@ -104,9 +104,7 @@ ScriptController::ScriptController(CompanyID company) : ScriptInfo *lib = ScriptObject::GetActiveInstance()->FindLibrary(library, version); if (lib == nullptr) { - char error[1024]; - seprintf(error, lastof(error), "couldn't find library '%s' with version %d", library, version); - throw sq_throwerror(vm, error); + throw sq_throwerror(vm, fmt::format("couldn't find library '{}' with version {}", library, version)); } /* Internally we store libraries as 'library.version' */ @@ -133,9 +131,7 @@ ScriptController::ScriptController(CompanyID company) : sq_newclass(vm, SQFalse); /* Load the library */ if (!engine->LoadScript(vm, lib->GetMainScript(), false)) { - char error[1024]; - seprintf(error, lastof(error), "there was a compile error when importing '%s' version %d", library, version); - throw sq_throwerror(vm, error); + throw sq_throwerror(vm, fmt::format("there was a compile error when importing '{}' version {}", library, version)); } /* Create the fake class */ sq_newslot(vm, -3, SQFalse); @@ -152,9 +148,7 @@ ScriptController::ScriptController(CompanyID company) : } sq_pushstring(vm, lib->GetInstanceName(), -1); if (SQ_FAILED(sq_get(vm, -2))) { - char error[1024]; - seprintf(error, lastof(error), "unable to find class '%s' in the library '%s' version %d", lib->GetInstanceName(), library, version); - throw sq_throwerror(vm, error); + throw sq_throwerror(vm, fmt::format("unable to find class '{}' in the library '{}' version {}", lib->GetInstanceName(), library, version)); } HSQOBJECT obj; sq_getstackobj(vm, -1, &obj); diff --git a/src/script/script_info.cpp b/src/script/script_info.cpp index 9f815f3e21..77097ddd21 100644 --- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -14,6 +14,7 @@ #include "script_info.hpp" #include "script_scanner.hpp" +#include "../3rdparty/fmt/format.h" #include "../safeguards.h" @@ -32,9 +33,7 @@ ScriptInfo::~ScriptInfo() bool ScriptInfo::CheckMethod(const char *name) const { if (!this->engine->MethodExists(*this->SQ_instance, name)) { - char error[1024]; - seprintf(error, lastof(error), "your info.nut/library.nut doesn't have the method '%s'", name); - this->engine->ThrowError(error); + this->engine->ThrowError(fmt::format("your info.nut/library.nut doesn't have the method '{}'", name)); return false; } return true; @@ -168,9 +167,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) config.flags = (ScriptConfigFlags)res; items |= 0x100; } else { - char error[1024]; - seprintf(error, lastof(error), "unknown setting property '%s'", key); - this->engine->ThrowError(error); + this->engine->ThrowError(fmt::format("unknown setting property '{}'", key)); return SQ_ERROR; } @@ -181,9 +178,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) /* Don't allow both random_deviation and SCRIPTCONFIG_RANDOM to * be set for the same config item. */ if ((items & 0x200) != 0 && (config.flags & SCRIPTCONFIG_RANDOM) != 0) { - char error[1024]; - seprintf(error, lastof(error), "Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed"); - this->engine->ThrowError(error); + this->engine->ThrowError("Setting both random_deviation and SCRIPTCONFIG_RANDOM is not allowed"); return SQ_ERROR; } /* Reset the bit for random_deviation as it's optional. */ @@ -192,9 +187,7 @@ SQInteger ScriptInfo::AddSetting(HSQUIRRELVM vm) /* Make sure all properties are defined */ uint mask = (config.flags & SCRIPTCONFIG_BOOLEAN) ? 0x1F3 : 0x1FF; if (items != mask) { - char error[1024]; - seprintf(error, lastof(error), "please define all properties of a setting (min/max not allowed for booleans)"); - this->engine->ThrowError(error); + this->engine->ThrowError("please define all properties of a setting (min/max not allowed for booleans)"); return SQ_ERROR; } @@ -214,9 +207,7 @@ SQInteger ScriptInfo::AddLabels(HSQUIRRELVM vm) } if (config == nullptr) { - char error[1024]; - seprintf(error, lastof(error), "Trying to add labels for non-defined setting '%s'", setting_name); - this->engine->ThrowError(error); + this->engine->ThrowError(fmt::format("Trying to add labels for non-defined setting '{}'", setting_name)); return SQ_ERROR; } if (!config->labels.empty()) return SQ_ERROR; diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index c04d3c2fec..b970aa7094 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -101,7 +101,7 @@ void ScriptInstance::Initialize(const char *main_script, const char *instance_na ScriptObject::SetAllowDoCommand(true); } catch (Script_FatalError &e) { this->is_dead = true; - this->engine->ThrowError(e.GetErrorMessage().c_str()); + this->engine->ThrowError(e.GetErrorMessage()); this->engine->ResumeError(); this->Died(); } @@ -226,7 +226,7 @@ void ScriptInstance::GameLoop() this->callback = e.GetSuspendCallback(); } catch (Script_FatalError &e) { this->is_dead = true; - this->engine->ThrowError(e.GetErrorMessage().c_str()); + this->engine->ThrowError(e.GetErrorMessage()); this->engine->ResumeError(); this->Died(); } @@ -247,7 +247,7 @@ void ScriptInstance::GameLoop() this->callback = e.GetSuspendCallback(); } catch (Script_FatalError &e) { this->is_dead = true; - this->engine->ThrowError(e.GetErrorMessage().c_str()); + this->engine->ThrowError(e.GetErrorMessage()); this->engine->ResumeError(); this->Died(); } @@ -505,7 +505,7 @@ void ScriptInstance::Save() /* If we don't mark the script as dead here cleaning up the squirrel * stack could throw Script_FatalError again. */ this->is_dead = true; - this->engine->ThrowError(e.GetErrorMessage().c_str()); + this->engine->ThrowError(e.GetErrorMessage()); this->engine->ResumeError(); SaveEmpty(); /* We can't kill the script here, so mark it as crashed (not dead) and diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp index f9471ff14b..34325c7a3d 100644 --- a/src/script/squirrel.hpp +++ b/src/script/squirrel.hpp @@ -234,7 +234,7 @@ public: /** * Throw a Squirrel error that will be nicely displayed to the user. */ - void ThrowError(const char *error) { sq_throwerror(this->vm, error); } + void ThrowError(const std::string_view error) { sq_throwerror(this->vm, error); } /** * Release a SQ object.