Codechange: introduce and use std::string variant of sq_throwerror

pull/562/head
Rubidium 1 year ago committed by rubidium42
parent 3453c03a17
commit e035705239

@ -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);

@ -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;
}

@ -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);

@ -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;

@ -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

@ -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.

Loading…
Cancel
Save